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