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