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?
10 --------------------------- dentry_operations --------------------------
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 *);
29 --------------------------- inode_operations ---------------------------
31 int (*create) (struct inode *,struct dentry *,int);
32 struct dentry * (*lookup) (struct inode *,struct dentry *);
33 int (*link) (struct dentry *,struct inode *,struct dentry *);
34 int (*unlink) (struct inode *,struct dentry *);
35 int (*symlink) (struct inode *,struct dentry *,const char *);
36 int (*mkdir) (struct inode *,struct dentry *,int);
37 int (*rmdir) (struct inode *,struct dentry *);
38 int (*mknod) (struct inode *,struct dentry *,int,int);
39 int (*rename) (struct inode *, struct dentry *,
40 struct inode *, struct dentry *);
41 int (*readlink) (struct dentry *, char *,int);
42 int (*follow_link) (struct dentry *, struct nameidata *);
43 void (*truncate) (struct inode *);
44 int (*permission) (struct inode *, int);
45 int (*revalidate) (struct dentry *);
46 int (*setattr) (struct dentry *, struct iattr *);
47 int (*getattr) (struct dentry *, struct iattr *);
48 int (*setxattr) (struct dentry *, const char *, 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 *);
55 BKL i_sem(inode) i_zombie(inode)
62 rmdir: yes yes yes (see below)
63 rename: yes yes (both) yes (both) (see below)
66 truncate: yes yes no (see below)
67 setattr: yes if ATTR_SIZE no
70 revalidate: no (see below)
74 removexattr: yes yes no
75 Additionally, ->rmdir() has i_zombie on victim and so does ->rename()
76 in case when target exists and is a directory.
77 ->rename() on directories has (per-superblock) ->s_vfs_rename_sem.
78 ->revalidate(), it may be called both with and without the i_sem
79 on dentry->d_inode. VFS never calls it with i_zombie on dentry->d_inode,
80 but watch for other methods directly calling this one...
81 ->truncate() is never called directly - it's a callback, not a
82 method. It's called by vmtruncate() - library function normally used by
83 ->setattr(). Locking information above applies to that call (i.e. is
84 inherited from ->setattr() - vmtruncate() is used when ATTR_SIZE had been
86 ->getattr() is currently unused.
88 --------------------------- super_operations ---------------------------
90 void (*read_inode) (struct inode *);
91 void (*write_inode) (struct inode *, int);
92 void (*put_inode) (struct inode *);
93 void (*delete_inode) (struct inode *);
94 void (*put_super) (struct super_block *);
95 void (*write_super) (struct super_block *);
96 int (*sync_fs) (struct super_block *);
97 int (*statfs) (struct super_block *, struct statfs *);
98 int (*remount_fs) (struct super_block *, int *, char *);
99 void (*clear_inode) (struct inode *);
100 void (*umount_begin) (struct super_block *);
105 read_inode: yes (see below)
110 put_super: yes yes maybe (see below)
111 write_super: yes yes maybe (see below)
112 sync_fs: yes no maybe (see below)
114 remount_fs: yes yes maybe (see below)
115 umount_begin: yes no maybe (see below)
117 ->read_inode() is not a method - it's a callback used in iget()/iget4().
118 rules for mount_sem are not too nice - it is going to die and be replaced
119 by better scheme anyway.
121 --------------------------- file_system_type ---------------------------
123 struct super_block *(*read_super) (struct super_block *, void *, int);
125 may block BKL ->s_lock mount_sem
128 --------------------------- address_space_operations --------------------------
130 int (*writepage)(struct page *);
131 int (*readpage)(struct file *, struct page *);
132 int (*sync_page)(struct page *);
133 int (*prepare_write)(struct file *, struct page *, unsigned, unsigned);
134 int (*commit_write)(struct file *, struct page *, unsigned, unsigned);
135 int (*bmap)(struct address_space *, long);
136 int (*flushpage) (struct page *, unsigned long);
137 int (*releasepage) (struct page *, int);
138 int (*direct_IO)(int, struct inode *, struct kiobuf *, unsigned long, int);
143 writepage: no yes, unlocks
144 readpage: no yes, unlocks
146 prepare_write: no yes
152 ->prepare_write(), ->commit_write(), ->sync_page() and ->readpage()
153 may be called from the request handler (/dev/loop).
154 ->readpage() and ->writepage() unlock the page.
155 ->sync_page() locking rules are not well-defined - usually it is called
156 with lock on page, but that is not guaranteed. Considering the currently
157 existing instances of this method ->sync_page() itself doesn't look
159 ->bmap() is currently used by legacy ioctl() (FIBMAP) provided by some
160 filesystems and by the swapper. The latter will eventually go away. All
161 instances do not actually need the BKL. Please, keep it that way and don't
163 ->flushpage() is called when the filesystem must attempt to drop
164 some or all of the buffers from the page when it is being truncated. It
165 returns zero on success. If ->flushpage is zero, the kernel uses
166 block_flushpage() instead.
167 ->releasepage() is called when the kernel is about to try to drop the
168 buffers from the page in preparation for freeing it. It returns zero to
169 indicate that the buffers are (or may be) freeable. If ->releasepage is zero,
170 the kernel assumes that the fs has no private interest in the buffers.
172 Note: currently almost all instances of address_space methods are
173 using BKL for internal serialization and that's one of the worst sources
174 of contention. Normally they are calling library functions (in fs/buffer.c)
175 and pass foo_get_block() as a callback (on local block-based filesystems,
176 indeed). BKL is not needed for library stuff and is usually taken by
177 foo_get_block(). It's an overkill, since block bitmaps can be protected by
178 internal fs locking and real critical areas are much smaller than the areas
179 filesystems protect now.
181 --------------------------- file_lock ------------------------------------
183 void (*fl_notify)(struct file_lock *); /* unblock callback */
184 void (*fl_insert)(struct file_lock *); /* lock insertion callback */
185 void (*fl_remove)(struct file_lock *); /* lock removal callback */
192 Currently only NLM provides instances of this class. None of the
193 them block. If you have out-of-tree instances - please, show up. Locking
194 in that area will change.
196 --------------------------- buffer_head -----------------------------------
198 void (*b_end_io)(struct buffer_head *bh, int uptodate);
201 called from interrupts. In other words, extreme care is needed here.
202 bh is locked, but that's all warranties we have here. Currently only RAID1,
203 highmem and fs/buffer.c are providing these. Block devices call this method
204 upon the IO completion.
206 --------------------------- block_device_operations -----------------------
208 int (*open) (struct inode *, struct file *);
209 int (*release) (struct inode *, struct file *);
210 int (*ioctl) (struct inode *, struct file *, unsigned, unsigned long);
211 int (*check_media_change) (kdev_t);
212 int (*revalidate) (kdev_t);
218 check_media_change: yes no
221 The last two are called only from check_disk_change(). Prototypes are very
222 bad - as soon as we'll get disk_struct they will change (and methods will
223 become per-disk instead of per-partition).
225 --------------------------- file_operations -------------------------------
227 loff_t (*llseek) (struct file *, loff_t, int);
228 ssize_t (*read) (struct file *, char *, size_t, loff_t *);
229 ssize_t (*write) (struct file *, const char *, size_t, loff_t *);
230 int (*readdir) (struct file *, void *, filldir_t);
231 unsigned int (*poll) (struct file *, struct poll_table_struct *);
232 int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
233 int (*mmap) (struct file *, struct vm_area_struct *);
234 int (*open) (struct inode *, struct file *);
235 int (*flush) (struct file *);
236 int (*release) (struct inode *, struct file *);
237 int (*fsync) (struct file *, struct dentry *, int datasync);
238 int (*fasync) (int, struct file *, int);
239 int (*lock) (struct file *, int, struct file_lock *);
240 ssize_t (*readv) (struct file *, const struct iovec *, unsigned long, loff_t *);
241 ssize_t (*writev) (struct file *, const struct iovec *, unsigned long, loff_t *);
245 All except ->poll() may block.
250 readdir: yes (see below)
252 ioctl: yes (see below)
254 open: maybe (see below)
257 fsync: yes (see below)
258 fasync: yes (see below)
263 ->open() locking is in-transit: big lock partially moved into the methods.
264 The only exception is ->open() in the instances of file_operations that never
265 end up in ->i_fop/->proc_fops, i.e. ones that belong to character devices
266 (chrdev_open() takes lock before replacing ->f_op and calling the secondary
267 method. As soon as we fix the handling of module reference counters all
268 instances of ->open() will be called without the BKL.
270 Note: ext2_release() was *the* source of contention on fs-intensive
271 loads and dropping BKL on ->release() helps to get rid of that (we still
272 grab BKL for cases when we close a file that had been opened r/w, but that
273 can and should be done using the internal locking with smaller critical areas).
274 Current worst offender is ext2_get_block()...
276 ->fasync() is a mess. This area needs a big cleanup and that will probably
279 ->readdir() and ->ioctl() on directories must be changed. Ideally we would
280 move ->readdir() to inode_operations and use a separate method for directory
281 ->ioctl() or kill the latter completely. One of the problems is that for
282 anything that resembles union-mount we won't have a struct file for all
283 components. And there are other reasons why the current interface is a mess...
285 ->read on directories probably must go away - we should just enforce -EISDIR
286 in sys_read() and friends.
288 ->fsync() has i_sem on inode.
290 --------------------------- dquot_operations -------------------------------
292 void (*initialize) (struct inode *, short);
293 void (*drop) (struct inode *);
294 int (*alloc_block) (const struct inode *, unsigned long, char);
295 int (*alloc_inode) (const struct inode *, unsigned long);
296 void (*free_block) (const struct inode *, unsigned long);
297 void (*free_inode) (const struct inode *, unsigned long);
298 int (*transfer) (struct dentry *, struct iattr *);
310 --------------------------- vm_operations_struct -----------------------------
312 void (*open)(struct vm_area_struct*);
313 void (*close)(struct vm_area_struct*);
314 struct page *(*nopage)(struct vm_area_struct*, unsigned long, int);
322 ================================================================================
325 (if you break something or notice that it is broken and do not fix it yourself
326 - at least put it here)
328 ipc/shm.c::shm_delete() - may need BKL.
329 ->read() and ->write() in many drivers are (probably) missing BKL.
330 drivers/sgi/char/graphics.c::sgi_graphics_nopage() - may need BKL.