vserver 1.9.5.x5
[linux-2.6.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 super_block *, 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
108 locking rules:
109         All may block.
110                         BKL     s_lock  s_umount
111 alloc_inode:            no      no      no
112 destroy_inode:          no
113 read_inode:             no                              (see below)
114 dirty_inode:            no                              (must not sleep)
115 write_inode:            no
116 put_inode:              no
117 drop_inode:             no                              !!!inode_lock!!!
118 delete_inode:           no
119 put_super:              yes     yes     no
120 write_super:            no      yes     read
121 sync_fs:                no      no      read
122 write_super_lockfs:     ?
123 unlockfs:               ?
124 statfs:                 no      no      no
125 remount_fs:             no      yes     maybe           (see below)
126 clear_inode:            no
127 umount_begin:           yes     no      no
128 show_options:           no                              (vfsmount->sem)
129
130 ->read_inode() is not a method - it's a callback used in iget().
131 ->remount_fs() will have the s_umount lock if it's already mounted.
132 When called from get_sb_single, it does NOT have the s_umount lock.
133
134 --------------------------- file_system_type ---------------------------
135 prototypes:
136         struct super_block *(*get_sb) (struct file_system_type *, int,
137                         const char *, void *);
138         void (*kill_sb) (struct super_block *);
139 locking rules:
140                 may block       BKL
141 get_sb          yes             yes
142 kill_sb         yes             yes
143
144 ->get_sb() returns error or a locked superblock (exclusive on ->s_umount).
145 ->kill_sb() takes a write-locked superblock, does all shutdown work on it,
146 unlocks and drops the reference.
147
148 --------------------------- address_space_operations --------------------------
149 prototypes:
150         int (*writepage)(struct page *page, struct writeback_control *wbc);
151         int (*readpage)(struct file *, struct page *);
152         int (*sync_page)(struct page *);
153         int (*writepages)(struct address_space *, struct writeback_control *);
154         int (*set_page_dirty)(struct page *page);
155         int (*readpages)(struct file *filp, struct address_space *mapping,
156                         struct list_head *pages, unsigned nr_pages);
157         int (*prepare_write)(struct file *, struct page *, unsigned, unsigned);
158         int (*commit_write)(struct file *, struct page *, unsigned, unsigned);
159         sector_t (*bmap)(struct address_space *, sector_t);
160         int (*invalidatepage) (struct page *, unsigned long);
161         int (*releasepage) (struct page *, int);
162         int (*direct_IO)(int, struct kiocb *, const struct iovec *iov,
163                         loff_t offset, unsigned long nr_segs);
164
165 locking rules:
166         All except set_page_dirty may block
167
168                         BKL     PageLocked(page)
169 writepage:              no      yes, unlocks (see below)
170 readpage:               no      yes, unlocks
171 sync_page:              no      maybe
172 writepages:             no
173 set_page_dirty          no      no
174 readpages:              no
175 prepare_write:          no      yes
176 commit_write:           no      yes
177 bmap:                   yes
178 invalidatepage:         no      yes
179 releasepage:            no      yes
180 direct_IO:              no
181
182         ->prepare_write(), ->commit_write(), ->sync_page() and ->readpage()
183 may be called from the request handler (/dev/loop).
184
185         ->readpage() unlocks the page, either synchronously or via I/O
186 completion.
187
188         ->readpages() populates the pagecache with the passed pages and starts
189 I/O against them.  They come unlocked upon I/O completion.
190
191         ->writepage() is used for two purposes: for "memory cleansing" and for
192 "sync".  These are quite different operations and the behaviour may differ
193 depending upon the mode.
194
195 If writepage is called for sync (wbc->sync_mode != WBC_SYNC_NONE) then
196 it *must* start I/O against the page, even if that would involve
197 blocking on in-progress I/O.
198
199 If writepage is called for memory cleansing (sync_mode ==
200 WBC_SYNC_NONE) then its role is to get as much writeout underway as
201 possible.  So writepage should try to avoid blocking against
202 currently-in-progress I/O.
203
204 If the filesystem is not called for "sync" and it determines that it
205 would need to block against in-progress I/O to be able to start new I/O
206 against the page the filesystem should redirty the page with
207 redirty_page_for_writepage(), then unlock the page and return zero.
208 This may also be done to avoid internal deadlocks, but rarely.
209
210 If the filesytem is called for sync then it must wait on any
211 in-progress I/O and then start new I/O.
212
213 The filesystem should unlock the page synchronously, before returning
214 to the caller.
215
216 Unless the filesystem is going to redirty_page_for_writepage(), unlock the page
217 and return zero, writepage *must* run set_page_writeback() against the page,
218 followed by unlocking it.  Once set_page_writeback() has been run against the
219 page, write I/O can be submitted and the write I/O completion handler must run
220 end_page_writeback() once the I/O is complete.  If no I/O is submitted, the
221 filesystem must run end_page_writeback() against the page before returning from
222 writepage.
223
224 That is: after 2.5.12, pages which are under writeout are *not* locked.  Note,
225 if the filesystem needs the page to be locked during writeout, that is ok, too,
226 the page is allowed to be unlocked at any point in time between the calls to
227 set_page_writeback() and end_page_writeback().
228
229 Note, failure to run either redirty_page_for_writepage() or the combination of
230 set_page_writeback()/end_page_writeback() on a page submitted to writepage
231 will leave the page itself marked clean but it will be tagged as dirty in the
232 radix tree.  This incoherency can lead to all sorts of hard-to-debug problems
233 in the filesystem like having dirty inodes at umount and losing written data.
234
235         ->sync_page() locking rules are not well-defined - usually it is called
236 with lock on page, but that is not guaranteed. Considering the currently
237 existing instances of this method ->sync_page() itself doesn't look
238 well-defined...
239
240         ->writepages() is used for periodic writeback and for syscall-initiated
241 sync operations.  The address_space should start I/O against at least
242 *nr_to_write pages.  *nr_to_write must be decremented for each page which is
243 written.  The address_space implementation may write more (or less) pages
244 than *nr_to_write asks for, but it should try to be reasonably close.  If
245 nr_to_write is NULL, all dirty pages must be written.
246
247 writepages should _only_ write pages which are present on
248 mapping->io_pages.
249
250         ->set_page_dirty() is called from various places in the kernel
251 when the target page is marked as needing writeback.  It may be called
252 under spinlock (it cannot block) and is sometimes called with the page
253 not locked.
254
255         ->bmap() is currently used by legacy ioctl() (FIBMAP) provided by some
256 filesystems and by the swapper. The latter will eventually go away. All
257 instances do not actually need the BKL. Please, keep it that way and don't
258 breed new callers.
259
260         ->invalidatepage() is called when the filesystem must attempt to drop
261 some or all of the buffers from the page when it is being truncated.  It
262 returns zero on success.  If ->invalidatepage is zero, the kernel uses
263 block_invalidatepage() instead.
264
265         ->releasepage() is called when the kernel is about to try to drop the
266 buffers from the page in preparation for freeing it.  It returns zero to
267 indicate that the buffers are (or may be) freeable.  If ->releasepage is zero,
268 the kernel assumes that the fs has no private interest in the buffers.
269
270         Note: currently almost all instances of address_space methods are
271 using BKL for internal serialization and that's one of the worst sources
272 of contention. Normally they are calling library functions (in fs/buffer.c)
273 and pass foo_get_block() as a callback (on local block-based filesystems,
274 indeed). BKL is not needed for library stuff and is usually taken by
275 foo_get_block(). It's an overkill, since block bitmaps can be protected by
276 internal fs locking and real critical areas are much smaller than the areas
277 filesystems protect now.
278
279 ----------------------- file_lock_operations ------------------------------
280 prototypes:
281         void (*fl_insert)(struct file_lock *);  /* lock insertion callback */
282         void (*fl_remove)(struct file_lock *);  /* lock removal callback */
283         void (*fl_copy_lock)(struct file_lock *, struct file_lock *);
284         void (*fl_release_private)(struct file_lock *);
285
286
287 locking rules:
288                         BKL     may block
289 fl_insert:              yes     no
290 fl_remove:              yes     no
291 fl_copy_lock:           yes     no
292 fl_release_private:     yes     yes
293
294 ----------------------- lock_manager_operations ---------------------------
295 prototypes:
296         int (*fl_compare_owner)(struct file_lock *, struct file_lock *);
297         void (*fl_notify)(struct file_lock *);  /* unblock callback */
298         void (*fl_copy_lock)(struct file_lock *, struct file_lock *);
299         void (*fl_release_private)(struct file_lock *);
300         void (*fl_break)(struct file_lock *); /* break_lease callback */
301
302 locking rules:
303                         BKL     may block
304 fl_compare_owner:       yes     no
305 fl_notify:              yes     no
306 fl_copy_lock:           yes     no
307 fl_release_private:     yes     yes
308 fl_break:               yes     no
309
310         Currently only NFSD and NLM provide instances of this class. None of the
311 them block. If you have out-of-tree instances - please, show up. Locking
312 in that area will change.
313 --------------------------- buffer_head -----------------------------------
314 prototypes:
315         void (*b_end_io)(struct buffer_head *bh, int uptodate);
316
317 locking rules:
318         called from interrupts. In other words, extreme care is needed here.
319 bh is locked, but that's all warranties we have here. Currently only RAID1,
320 highmem, fs/buffer.c, and fs/ntfs/aops.c are providing these. Block devices
321 call this method upon the IO completion.
322
323 --------------------------- block_device_operations -----------------------
324 prototypes:
325         int (*open) (struct inode *, struct file *);
326         int (*release) (struct inode *, struct file *);
327         int (*ioctl) (struct inode *, struct file *, unsigned, unsigned long);
328         int (*media_changed) (struct gendisk *);
329         int (*revalidate_disk) (struct gendisk *);
330
331 locking rules:
332                         BKL     bd_sem
333 open:                   yes     yes
334 release:                yes     yes
335 ioctl:                  yes     no
336 media_changed:          no      no
337 revalidate_disk:        no      no
338
339 The last two are called only from check_disk_change().
340
341 --------------------------- file_operations -------------------------------
342 prototypes:
343         loff_t (*llseek) (struct file *, loff_t, int);
344         ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
345         ssize_t (*aio_read) (struct kiocb *, char __user *, size_t, loff_t);
346         ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
347         ssize_t (*aio_write) (struct kiocb *, const char __user *, size_t,
348                         loff_t);
349         int (*readdir) (struct file *, void *, filldir_t);
350         unsigned int (*poll) (struct file *, struct poll_table_struct *);
351         int (*ioctl) (struct inode *, struct file *, unsigned int,
352                         unsigned long);
353         long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
354         long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
355         int (*mmap) (struct file *, struct vm_area_struct *);
356         int (*open) (struct inode *, struct file *);
357         int (*flush) (struct file *);
358         int (*release) (struct inode *, struct file *);
359         int (*fsync) (struct file *, struct dentry *, int datasync);
360         int (*aio_fsync) (struct kiocb *, int datasync);
361         int (*fasync) (int, struct file *, int);
362         int (*lock) (struct file *, int, struct file_lock *);
363         ssize_t (*readv) (struct file *, const struct iovec *, unsigned long,
364                         loff_t *);
365         ssize_t (*writev) (struct file *, const struct iovec *, unsigned long,
366                         loff_t *);
367         ssize_t (*sendfile) (struct file *, loff_t *, size_t, read_actor_t,
368                         void __user *);
369         ssize_t (*sendpage) (struct file *, struct page *, int, size_t,
370                         loff_t *, int);
371         unsigned long (*get_unmapped_area)(struct file *, unsigned long,
372                         unsigned long, unsigned long, unsigned long);
373         int (*check_flags)(int);
374         int (*dir_notify)(struct file *, unsigned long);
375 };
376
377 locking rules:
378         All except ->poll() may block.
379                         BKL
380 llseek:                 no      (see below)
381 read:                   no
382 aio_read:               no
383 write:                  no
384 aio_write:              no
385 readdir:                no
386 poll:                   no
387 ioctl:                  yes     (see below)
388 unlocked_ioctl:         no      (see below)
389 compat_ioctl:           no
390 mmap:                   no
391 open:                   maybe   (see below)
392 flush:                  no
393 release:                no
394 fsync:                  no      (see below)
395 aio_fsync:              no
396 fasync:                 yes     (see below)
397 lock:                   yes
398 readv:                  no
399 writev:                 no
400 sendfile:               no
401 sendpage:               no
402 get_unmapped_area:      no
403 check_flags:            no
404 dir_notify:             no
405
406 ->llseek() locking has moved from llseek to the individual llseek
407 implementations.  If your fs is not using generic_file_llseek, you
408 need to acquire and release the appropriate locks in your ->llseek().
409 For many filesystems, it is probably safe to acquire the inode
410 semaphore.  Note some filesystems (i.e. remote ones) provide no
411 protection for i_size so you will need to use the BKL.
412
413 ->open() locking is in-transit: big lock partially moved into the methods.
414 The only exception is ->open() in the instances of file_operations that never
415 end up in ->i_fop/->proc_fops, i.e. ones that belong to character devices
416 (chrdev_open() takes lock before replacing ->f_op and calling the secondary
417 method. As soon as we fix the handling of module reference counters all
418 instances of ->open() will be called without the BKL.
419
420 Note: ext2_release() was *the* source of contention on fs-intensive
421 loads and dropping BKL on ->release() helps to get rid of that (we still
422 grab BKL for cases when we close a file that had been opened r/w, but that
423 can and should be done using the internal locking with smaller critical areas).
424 Current worst offender is ext2_get_block()...
425
426 ->fasync() is a mess. This area needs a big cleanup and that will probably
427 affect locking.
428
429 ->readdir() and ->ioctl() on directories must be changed. Ideally we would
430 move ->readdir() to inode_operations and use a separate method for directory
431 ->ioctl() or kill the latter completely. One of the problems is that for
432 anything that resembles union-mount we won't have a struct file for all
433 components. And there are other reasons why the current interface is a mess...
434
435 ->ioctl() on regular files is superceded by the ->unlocked_ioctl() that
436 doesn't take the BKL.
437
438 ->read on directories probably must go away - we should just enforce -EISDIR
439 in sys_read() and friends.
440
441 ->fsync() has i_sem on inode.
442
443 --------------------------- dquot_operations -------------------------------
444 prototypes:
445         void (*initialize) (struct inode *, short);
446         void (*drop) (struct inode *);
447         int (*alloc_block) (const struct inode *, unsigned long, char);
448         int (*alloc_inode) (const struct inode *, unsigned long);
449         void (*free_block) (const struct inode *, unsigned long);
450         void (*free_inode) (const struct inode *, unsigned long);
451         int (*transfer) (struct dentry *, struct iattr *);
452
453 locking rules:
454                 BKL
455 initialize:     no
456 drop:           no
457 alloc_block:    yes
458 alloc_inode:    yes
459 free_block:     yes
460 free_inode:     yes
461 transfer:       no
462
463 --------------------------- vm_operations_struct -----------------------------
464 prototypes:
465         void (*open)(struct vm_area_struct*);
466         void (*close)(struct vm_area_struct*);
467         struct page *(*nopage)(struct vm_area_struct*, unsigned long, int *);
468
469 locking rules:
470                 BKL     mmap_sem
471 open:           no      yes
472 close:          no      yes
473 nopage:         no      yes
474
475 ================================================================================
476                         Dubious stuff
477
478 (if you break something or notice that it is broken and do not fix it yourself
479 - at least put it here)
480
481 ipc/shm.c::shm_delete() - may need BKL.
482 ->read() and ->write() in many drivers are (probably) missing BKL.
483 drivers/sgi/char/graphics.c::sgi_graphics_nopage() - may need BKL.