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