Pull motherboard into test branch
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / Documentation / filesystems / Locking
1 The text below describes the locking rules for VFS-related methods.
2 It is (believed to be) up-to-date. *Please*, if you change anything in
3 prototypes or locking protocols - update this file. And update the relevant
4 instances in the tree, don't leave that to maintainers of filesystems/devices/
5 etc. At the very least, put the list of dubious cases in the end of this file.
6 Don't turn it into log - maintainers of out-of-the-tree code are supposed to
7 be able to use diff(1).
8 Thing currently missing here: socket operations. Alexey?
9
10 --------------------------- dentry_operations --------------------------
11 prototypes:
12 int (*d_revalidate)(struct dentry *, int);
13 int (*d_hash) (struct dentry *, struct qstr *);
14 int (*d_compare) (struct dentry *, struct qstr *, struct qstr *);
15 int (*d_delete)(struct dentry *);
16 void (*d_release)(struct dentry *);
17 void (*d_iput)(struct dentry *, struct inode *);
18
19 locking rules:
20 none have BKL
21 dcache_lock rename_lock ->d_lock may block
22 d_revalidate: no no no yes
23 d_hash no no no yes
24 d_compare: no yes no no
25 d_delete: yes no yes no
26 d_release: no no no yes
27 d_iput: no no no yes
28
29 --------------------------- inode_operations ---------------------------
30 prototypes:
31 int (*create) (struct inode *,struct dentry *,int, struct nameidata *);
32 struct dentry * (*lookup) (struct inode *,struct dentry *, struct nameid
33 ata *);
34 int (*link) (struct dentry *,struct inode *,struct dentry *);
35 int (*unlink) (struct inode *,struct dentry *);
36 int (*symlink) (struct inode *,struct dentry *,const char *);
37 int (*mkdir) (struct inode *,struct dentry *,int);
38 int (*rmdir) (struct inode *,struct dentry *);
39 int (*mknod) (struct inode *,struct dentry *,int,dev_t);
40 int (*rename) (struct inode *, struct dentry *,
41 struct inode *, struct dentry *);
42 int (*readlink) (struct dentry *, char __user *,int);
43 int (*follow_link) (struct dentry *, struct nameidata *);
44 void (*truncate) (struct inode *);
45 int (*permission) (struct inode *, int, struct nameidata *);
46 int (*setattr) (struct dentry *, struct iattr *);
47 int (*getattr) (struct vfsmount *, struct dentry *, struct kstat *);
48 int (*setxattr) (struct dentry *, const char *,const void *,size_t,int);
49 ssize_t (*getxattr) (struct dentry *, const char *, void *, size_t);
50 ssize_t (*listxattr) (struct dentry *, char *, size_t);
51 int (*removexattr) (struct dentry *, const char *);
52
53 locking rules:
54 all may block, none have BKL
55 i_sem(inode)
56 lookup: yes
57 create: yes
58 link: yes (both)
59 mknod: yes
60 symlink: yes
61 mkdir: yes
62 unlink: yes (both)
63 rmdir: yes (both) (see below)
64 rename: yes (all) (see below)
65 readlink: no
66 follow_link: no
67 truncate: yes (see below)
68 setattr: yes
69 permission: no
70 getattr: no
71 setxattr: yes
72 getxattr: no
73 listxattr: no
74 removexattr: yes
75 Additionally, ->rmdir(), ->unlink() and ->rename() have ->i_sem on
76 victim.
77 cross-directory ->rename() has (per-superblock) ->s_vfs_rename_sem.
78 ->truncate() is never called directly - it's a callback, not a
79 method. It's called by vmtruncate() - library function normally used by
80 ->setattr(). Locking information above applies to that call (i.e. is
81 inherited from ->setattr() - vmtruncate() is used when ATTR_SIZE had been
82 passed).
83
84 See Documentation/filesystems/directory-locking for more detailed discussion
85 of the locking scheme for directory operations.
86
87 --------------------------- super_operations ---------------------------
88 prototypes:
89 struct inode *(*alloc_inode)(struct super_block *sb);
90 void (*destroy_inode)(struct inode *);
91 void (*read_inode) (struct inode *);
92 void (*dirty_inode) (struct inode *);
93 int (*write_inode) (struct inode *, int);
94 void (*put_inode) (struct inode *);
95 void (*drop_inode) (struct inode *);
96 void (*delete_inode) (struct inode *);
97 void (*put_super) (struct super_block *);
98 void (*write_super) (struct super_block *);
99 int (*sync_fs)(struct super_block *sb, int wait);
100 void (*write_super_lockfs) (struct super_block *);
101 void (*unlockfs) (struct super_block *);
102 int (*statfs) (struct dentry *, struct kstatfs *);
103 int (*remount_fs) (struct super_block *, int *, char *);
104 void (*clear_inode) (struct inode *);
105 void (*umount_begin) (struct super_block *);
106 int (*show_options)(struct seq_file *, struct vfsmount *);
107 ssize_t (*quota_read)(struct super_block *, int, char *, size_t, loff_t);
108 ssize_t (*quota_write)(struct super_block *, int, const char *, size_t, loff_t);
109
110 locking rules:
111 All may block.
112 BKL s_lock s_umount
113 alloc_inode: no no no
114 destroy_inode: no
115 read_inode: no (see below)
116 dirty_inode: no (must not sleep)
117 write_inode: no
118 put_inode: no
119 drop_inode: no !!!inode_lock!!!
120 delete_inode: no
121 put_super: yes yes no
122 write_super: no yes read
123 sync_fs: no no read
124 write_super_lockfs: ?
125 unlockfs: ?
126 statfs: no no no
127 remount_fs: yes yes maybe (see below)
128 clear_inode: no
129 umount_begin: yes no no
130 show_options: no (vfsmount->sem)
131 quota_read: no no no (see below)
132 quota_write: no no no (see below)
133
134 ->read_inode() is not a method - it's a callback used in iget().
135 ->remount_fs() will have the s_umount lock if it's already mounted.
136 When called from get_sb_single, it does NOT have the s_umount lock.
137 ->quota_read() and ->quota_write() functions are both guaranteed to
138 be the only ones operating on the quota file by the quota code (via
139 dqio_sem) (unless an admin really wants to screw up something and
140 writes to quota files with quotas on). For other details about locking
141 see also dquot_operations section.
142
143 --------------------------- file_system_type ---------------------------
144 prototypes:
145 int (*get_sb) (struct file_system_type *, int,
146 const char *, void *, struct vfsmount *);
147 void (*kill_sb) (struct super_block *);
148 locking rules:
149 may block BKL
150 get_sb yes yes
151 kill_sb yes yes
152
153 ->get_sb() returns error or 0 with locked superblock attached to the vfsmount
154 (exclusive on ->s_umount).
155 ->kill_sb() takes a write-locked superblock, does all shutdown work on it,
156 unlocks and drops the reference.
157
158 --------------------------- address_space_operations --------------------------
159 prototypes:
160 int (*writepage)(struct page *page, struct writeback_control *wbc);
161 int (*readpage)(struct file *, struct page *);
162 int (*sync_page)(struct page *);
163 int (*writepages)(struct address_space *, struct writeback_control *);
164 int (*set_page_dirty)(struct page *page);
165 int (*readpages)(struct file *filp, struct address_space *mapping,
166 struct list_head *pages, unsigned nr_pages);
167 int (*prepare_write)(struct file *, struct page *, unsigned, unsigned);
168 int (*commit_write)(struct file *, struct page *, unsigned, unsigned);
169 sector_t (*bmap)(struct address_space *, sector_t);
170 int (*invalidatepage) (struct page *, unsigned long);
171 int (*releasepage) (struct page *, int);
172 int (*direct_IO)(int, struct kiocb *, const struct iovec *iov,
173 loff_t offset, unsigned long nr_segs);
174 int (*launder_page) (struct page *);
175
176 locking rules:
177 All except set_page_dirty may block
178
179 BKL PageLocked(page)
180 writepage: no yes, unlocks (see below)
181 readpage: no yes, unlocks
182 sync_page: no maybe
183 writepages: no
184 set_page_dirty no no
185 readpages: no
186 prepare_write: no yes
187 commit_write: no yes
188 bmap: yes
189 invalidatepage: no yes
190 releasepage: no yes
191 direct_IO: no
192 launder_page: no yes
193
194 ->prepare_write(), ->commit_write(), ->sync_page() and ->readpage()
195 may be called from the request handler (/dev/loop).
196
197 ->readpage() unlocks the page, either synchronously or via I/O
198 completion.
199
200 ->readpages() populates the pagecache with the passed pages and starts
201 I/O against them. They come unlocked upon I/O completion.
202
203 ->writepage() is used for two purposes: for "memory cleansing" and for
204 "sync". These are quite different operations and the behaviour may differ
205 depending upon the mode.
206
207 If writepage is called for sync (wbc->sync_mode != WBC_SYNC_NONE) then
208 it *must* start I/O against the page, even if that would involve
209 blocking on in-progress I/O.
210
211 If writepage is called for memory cleansing (sync_mode ==
212 WBC_SYNC_NONE) then its role is to get as much writeout underway as
213 possible. So writepage should try to avoid blocking against
214 currently-in-progress I/O.
215
216 If the filesystem is not called for "sync" and it determines that it
217 would need to block against in-progress I/O to be able to start new I/O
218 against the page the filesystem should redirty the page with
219 redirty_page_for_writepage(), then unlock the page and return zero.
220 This may also be done to avoid internal deadlocks, but rarely.
221
222 If the filesytem is called for sync then it must wait on any
223 in-progress I/O and then start new I/O.
224
225 The filesystem should unlock the page synchronously, before returning to the
226 caller, unless ->writepage() returns special WRITEPAGE_ACTIVATE
227 value. WRITEPAGE_ACTIVATE means that page cannot really be written out
228 currently, and VM should stop calling ->writepage() on this page for some
229 time. VM does this by moving page to the head of the active list, hence the
230 name.
231
232 Unless the filesystem is going to redirty_page_for_writepage(), unlock the page
233 and return zero, writepage *must* run set_page_writeback() against the page,
234 followed by unlocking it. Once set_page_writeback() has been run against the
235 page, write I/O can be submitted and the write I/O completion handler must run
236 end_page_writeback() once the I/O is complete. If no I/O is submitted, the
237 filesystem must run end_page_writeback() against the page before returning from
238 writepage.
239
240 That is: after 2.5.12, pages which are under writeout are *not* locked. Note,
241 if the filesystem needs the page to be locked during writeout, that is ok, too,
242 the page is allowed to be unlocked at any point in time between the calls to
243 set_page_writeback() and end_page_writeback().
244
245 Note, failure to run either redirty_page_for_writepage() or the combination of
246 set_page_writeback()/end_page_writeback() on a page submitted to writepage
247 will leave the page itself marked clean but it will be tagged as dirty in the
248 radix tree. This incoherency can lead to all sorts of hard-to-debug problems
249 in the filesystem like having dirty inodes at umount and losing written data.
250
251 ->sync_page() locking rules are not well-defined - usually it is called
252 with lock on page, but that is not guaranteed. Considering the currently
253 existing instances of this method ->sync_page() itself doesn't look
254 well-defined...
255
256 ->writepages() is used for periodic writeback and for syscall-initiated
257 sync operations. The address_space should start I/O against at least
258 *nr_to_write pages. *nr_to_write must be decremented for each page which is
259 written. The address_space implementation may write more (or less) pages
260 than *nr_to_write asks for, but it should try to be reasonably close. If
261 nr_to_write is NULL, all dirty pages must be written.
262
263 writepages should _only_ write pages which are present on
264 mapping->io_pages.
265
266 ->set_page_dirty() is called from various places in the kernel
267 when the target page is marked as needing writeback. It may be called
268 under spinlock (it cannot block) and is sometimes called with the page
269 not locked.
270
271 ->bmap() is currently used by legacy ioctl() (FIBMAP) provided by some
272 filesystems and by the swapper. The latter will eventually go away. All
273 instances do not actually need the BKL. Please, keep it that way and don't
274 breed new callers.
275
276 ->invalidatepage() is called when the filesystem must attempt to drop
277 some or all of the buffers from the page when it is being truncated. It
278 returns zero on success. If ->invalidatepage is zero, the kernel uses
279 block_invalidatepage() instead.
280
281 ->releasepage() is called when the kernel is about to try to drop the
282 buffers from the page in preparation for freeing it. It returns zero to
283 indicate that the buffers are (or may be) freeable. If ->releasepage is zero,
284 the kernel assumes that the fs has no private interest in the buffers.
285
286 ->launder_page() may be called prior to releasing a page if
287 it is still found to be dirty. It returns zero if the page was successfully
288 cleaned, or an error value if not. Note that in order to prevent the page
289 getting mapped back in and redirtied, it needs to be kept locked
290 across the entire operation.
291
292 Note: currently almost all instances of address_space methods are
293 using BKL for internal serialization and that's one of the worst sources
294 of contention. Normally they are calling library functions (in fs/buffer.c)
295 and pass foo_get_block() as a callback (on local block-based filesystems,
296 indeed). BKL is not needed for library stuff and is usually taken by
297 foo_get_block(). It's an overkill, since block bitmaps can be protected by
298 internal fs locking and real critical areas are much smaller than the areas
299 filesystems protect now.
300
301 ----------------------- file_lock_operations ------------------------------
302 prototypes:
303 void (*fl_insert)(struct file_lock *); /* lock insertion callback */
304 void (*fl_remove)(struct file_lock *); /* lock removal callback */
305 void (*fl_copy_lock)(struct file_lock *, struct file_lock *);
306 void (*fl_release_private)(struct file_lock *);
307
308
309 locking rules:
310 BKL may block
311 fl_insert: yes no
312 fl_remove: yes no
313 fl_copy_lock: yes no
314 fl_release_private: yes yes
315
316 ----------------------- lock_manager_operations ---------------------------
317 prototypes:
318 int (*fl_compare_owner)(struct file_lock *, struct file_lock *);
319 void (*fl_notify)(struct file_lock *); /* unblock callback */
320 void (*fl_copy_lock)(struct file_lock *, struct file_lock *);
321 void (*fl_release_private)(struct file_lock *);
322 void (*fl_break)(struct file_lock *); /* break_lease callback */
323
324 locking rules:
325 BKL may block
326 fl_compare_owner: yes no
327 fl_notify: yes no
328 fl_copy_lock: yes no
329 fl_release_private: yes yes
330 fl_break: yes no
331
332 Currently only NFSD and NLM provide instances of this class. None of the
333 them block. If you have out-of-tree instances - please, show up. Locking
334 in that area will change.
335 --------------------------- buffer_head -----------------------------------
336 prototypes:
337 void (*b_end_io)(struct buffer_head *bh, int uptodate);
338
339 locking rules:
340 called from interrupts. In other words, extreme care is needed here.
341 bh is locked, but that's all warranties we have here. Currently only RAID1,
342 highmem, fs/buffer.c, and fs/ntfs/aops.c are providing these. Block devices
343 call this method upon the IO completion.
344
345 --------------------------- block_device_operations -----------------------
346 prototypes:
347 int (*open) (struct inode *, struct file *);
348 int (*release) (struct inode *, struct file *);
349 int (*ioctl) (struct inode *, struct file *, unsigned, unsigned long);
350 int (*media_changed) (struct gendisk *);
351 int (*revalidate_disk) (struct gendisk *);
352
353 locking rules:
354 BKL bd_sem
355 open: yes yes
356 release: yes yes
357 ioctl: yes no
358 media_changed: no no
359 revalidate_disk: no no
360
361 The last two are called only from check_disk_change().
362
363 --------------------------- file_operations -------------------------------
364 prototypes:
365 loff_t (*llseek) (struct file *, loff_t, int);
366 ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
367 ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
368 ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
369 ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
370 int (*readdir) (struct file *, void *, filldir_t);
371 unsigned int (*poll) (struct file *, struct poll_table_struct *);
372 int (*ioctl) (struct inode *, struct file *, unsigned int,
373 unsigned long);
374 long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
375 long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
376 int (*mmap) (struct file *, struct vm_area_struct *);
377 int (*open) (struct inode *, struct file *);
378 int (*flush) (struct file *);
379 int (*release) (struct inode *, struct file *);
380 int (*fsync) (struct file *, struct dentry *, int datasync);
381 int (*aio_fsync) (struct kiocb *, int datasync);
382 int (*fasync) (int, struct file *, int);
383 int (*lock) (struct file *, int, struct file_lock *);
384 ssize_t (*readv) (struct file *, const struct iovec *, unsigned long,
385 loff_t *);
386 ssize_t (*writev) (struct file *, const struct iovec *, unsigned long,
387 loff_t *);
388 ssize_t (*sendfile) (struct file *, loff_t *, size_t, read_actor_t,
389 void __user *);
390 ssize_t (*sendpage) (struct file *, struct page *, int, size_t,
391 loff_t *, int);
392 unsigned long (*get_unmapped_area)(struct file *, unsigned long,
393 unsigned long, unsigned long, unsigned long);
394 int (*check_flags)(int);
395 int (*dir_notify)(struct file *, unsigned long);
396 };
397
398 locking rules:
399 All except ->poll() may block.
400 BKL
401 llseek: no (see below)
402 read: no
403 aio_read: no
404 write: no
405 aio_write: no
406 readdir: no
407 poll: no
408 ioctl: yes (see below)
409 unlocked_ioctl: no (see below)
410 compat_ioctl: no
411 mmap: no
412 open: maybe (see below)
413 flush: no
414 release: no
415 fsync: no (see below)
416 aio_fsync: no
417 fasync: yes (see below)
418 lock: yes
419 readv: no
420 writev: no
421 sendfile: no
422 sendpage: no
423 get_unmapped_area: no
424 check_flags: no
425 dir_notify: no
426
427 ->llseek() locking has moved from llseek to the individual llseek
428 implementations. If your fs is not using generic_file_llseek, you
429 need to acquire and release the appropriate locks in your ->llseek().
430 For many filesystems, it is probably safe to acquire the inode
431 semaphore. Note some filesystems (i.e. remote ones) provide no
432 protection for i_size so you will need to use the BKL.
433
434 ->open() locking is in-transit: big lock partially moved into the methods.
435 The only exception is ->open() in the instances of file_operations that never
436 end up in ->i_fop/->proc_fops, i.e. ones that belong to character devices
437 (chrdev_open() takes lock before replacing ->f_op and calling the secondary
438 method. As soon as we fix the handling of module reference counters all
439 instances of ->open() will be called without the BKL.
440
441 Note: ext2_release() was *the* source of contention on fs-intensive
442 loads and dropping BKL on ->release() helps to get rid of that (we still
443 grab BKL for cases when we close a file that had been opened r/w, but that
444 can and should be done using the internal locking with smaller critical areas).
445 Current worst offender is ext2_get_block()...
446
447 ->fasync() is a mess. This area needs a big cleanup and that will probably
448 affect locking.
449
450 ->readdir() and ->ioctl() on directories must be changed. Ideally we would
451 move ->readdir() to inode_operations and use a separate method for directory
452 ->ioctl() or kill the latter completely. One of the problems is that for
453 anything that resembles union-mount we won't have a struct file for all
454 components. And there are other reasons why the current interface is a mess...
455
456 ->ioctl() on regular files is superceded by the ->unlocked_ioctl() that
457 doesn't take the BKL.
458
459 ->read on directories probably must go away - we should just enforce -EISDIR
460 in sys_read() and friends.
461
462 ->fsync() has i_sem on inode.
463
464 --------------------------- dquot_operations -------------------------------
465 prototypes:
466 int (*initialize) (struct inode *, int);
467 int (*drop) (struct inode *);
468 int (*alloc_space) (struct inode *, qsize_t, int);
469 int (*alloc_inode) (const struct inode *, unsigned long);
470 int (*free_space) (struct inode *, qsize_t);
471 int (*free_inode) (const struct inode *, unsigned long);
472 int (*transfer) (struct inode *, struct iattr *);
473 int (*write_dquot) (struct dquot *);
474 int (*acquire_dquot) (struct dquot *);
475 int (*release_dquot) (struct dquot *);
476 int (*mark_dirty) (struct dquot *);
477 int (*write_info) (struct super_block *, int);
478
479 These operations are intended to be more or less wrapping functions that ensure
480 a proper locking wrt the filesystem and call the generic quota operations.
481
482 What filesystem should expect from the generic quota functions:
483
484 FS recursion Held locks when called
485 initialize: yes maybe dqonoff_sem
486 drop: yes -
487 alloc_space: ->mark_dirty() -
488 alloc_inode: ->mark_dirty() -
489 free_space: ->mark_dirty() -
490 free_inode: ->mark_dirty() -
491 transfer: yes -
492 write_dquot: yes dqonoff_sem or dqptr_sem
493 acquire_dquot: yes dqonoff_sem or dqptr_sem
494 release_dquot: yes dqonoff_sem or dqptr_sem
495 mark_dirty: no -
496 write_info: yes dqonoff_sem
497
498 FS recursion means calling ->quota_read() and ->quota_write() from superblock
499 operations.
500
501 ->alloc_space(), ->alloc_inode(), ->free_space(), ->free_inode() are called
502 only directly by the filesystem and do not call any fs functions only
503 the ->mark_dirty() operation.
504
505 More details about quota locking can be found in fs/dquot.c.
506
507 --------------------------- vm_operations_struct -----------------------------
508 prototypes:
509 void (*open)(struct vm_area_struct*);
510 void (*close)(struct vm_area_struct*);
511 struct page *(*nopage)(struct vm_area_struct*, unsigned long, int *);
512
513 locking rules:
514 BKL mmap_sem
515 open: no yes
516 close: no yes
517 nopage: no yes
518
519 ================================================================================
520 Dubious stuff
521
522 (if you break something or notice that it is broken and do not fix it yourself
523 - at least put it here)
524
525 ipc/shm.c::shm_delete() - may need BKL.
526 ->read() and ->write() in many drivers are (probably) missing BKL.
527 drivers/sgi/char/graphics.c::sgi_graphics_nopage() - may need BKL.