make oldconfig will rebuild these...
[linux-2.4.21-pre4.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     may block
22 d_revalidate:   no              yes
23 d_hash          no              yes
24 d_compare:      yes             no
25 d_delete:       yes             no
26 d_release:      no              yes
27 d_iput:         no              yes
28
29 --------------------------- inode_operations --------------------------- 
30 prototypes:
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 *);
52
53 locking rules:
54         all may block
55                 BKL     i_sem(inode)    i_zombie(inode)
56 lookup:         yes     yes             no
57 create:         yes     yes             yes
58 link:           yes     yes             yes
59 mknod:          yes     yes             yes
60 mkdir:          yes     yes             yes
61 unlink:         yes     yes             yes
62 rmdir:          yes     yes             yes             (see below)
63 rename:         yes     yes (both)      yes (both)      (see below)
64 readlink:       no      no              no
65 follow_link:    no      no              no
66 truncate:       yes     yes             no              (see below)
67 setattr:        yes     if ATTR_SIZE    no
68 permission:     yes     no              no
69 getattr:                                                (see below)
70 revalidate:     no                                      (see below)
71 setxattr:       yes     yes             no
72 getxattr:       yes     yes             no
73 listxattr:      yes     yes             no
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
85 passed).
86         ->getattr() is currently unused.
87
88 --------------------------- super_operations ---------------------------
89 prototypes:
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 *);
101
102 locking rules:
103         All may block.
104                 BKL     s_lock  mount_sem
105 read_inode:     yes                             (see below)
106 write_inode:    no      
107 put_inode:      no      
108 delete_inode:   no      
109 clear_inode:    no      
110 put_super:      yes     yes     maybe           (see below)
111 write_super:    yes     yes     maybe           (see below)
112 sync_fs:        yes     no      maybe           (see below)
113 statfs:         yes     no      no
114 remount_fs:     yes     yes     maybe           (see below)
115 umount_begin:   yes     no      maybe           (see below)
116
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.
120
121 --------------------------- file_system_type ---------------------------
122 prototypes:
123         struct super_block *(*read_super) (struct super_block *, void *, int);
124 locking rules:
125 may block       BKL     ->s_lock        mount_sem
126 yes             yes     yes             maybe
127
128 --------------------------- address_space_operations --------------------------
129 prototypes:
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);
139
140 locking rules:
141         All may block
142                 BKL     PageLocked(page)
143 writepage:      no      yes, unlocks
144 readpage:       no      yes, unlocks
145 sync_page:      no      maybe
146 prepare_write:  no      yes
147 commit_write:   no      yes
148 bmap:           yes
149 flushpage:      no      yes
150 releasepage:    no      yes
151
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
158 well-defined...
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
162 breed new callers.
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.
171
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.
180
181 --------------------------- file_lock ------------------------------------
182 prototypes:
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 */
186
187 locking rules:
188                 BKL     may block
189 fl_notify:      yes     no
190 fl_insert:      yes     maybe
191 fl_remove:      yes     maybe
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.
195
196 --------------------------- buffer_head -----------------------------------
197 prototypes:
198         void (*b_end_io)(struct buffer_head *bh, int uptodate);
199
200 locking rules:
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.
205
206 --------------------------- block_device_operations -----------------------
207 prototypes:
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);
213 locking rules:
214                         BKL     bd_sem
215 open:                   yes     yes
216 release:                yes     yes
217 ioctl:                  yes     no
218 check_media_change:     yes     no
219 revalidate:             yes     no
220
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).
224
225 --------------------------- file_operations -------------------------------
226 prototypes:
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 *);
242 };
243
244 locking rules:
245         All except ->poll() may block.
246                 BKL
247 llseek:         yes
248 read:           no
249 write:          no
250 readdir:        yes     (see below)
251 poll:           no
252 ioctl:          yes     (see below)
253 mmap:           no
254 open:           maybe   (see below)
255 flush:          yes
256 release:        no
257 fsync:          yes     (see below)
258 fasync:         yes     (see below)
259 lock:           yes
260 readv:          no
261 writev:         no
262
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.
269
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()...
275
276 ->fasync() is a mess. This area needs a big cleanup and that will probably
277 affect locking.
278
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...
284
285 ->read on directories probably must go away - we should just enforce -EISDIR
286 in sys_read() and friends.
287
288 ->fsync() has i_sem on inode.
289
290 --------------------------- dquot_operations -------------------------------
291 prototypes:
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 *);
299
300 locking rules:
301                 BKL
302 initialize:     no
303 drop:           no
304 alloc_block:    yes
305 alloc_inode:    yes
306 free_block:     yes
307 free_inode:     yes
308 transfer:       no
309
310 --------------------------- vm_operations_struct -----------------------------
311 prototypes:
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);
315
316 locking rules:
317                 BKL     mmap_sem
318 open:           no      yes
319 close:          no      yes
320 nopage:         no      yes
321
322 ================================================================================
323                         Dubious stuff
324
325 (if you break something or notice that it is broken and do not fix it yourself
326 - at least put it here)
327
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.