filelock: move file locking definitions to separate header file
[linux-block.git] / include / linux / filelock.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_FILELOCK_H
3 #define _LINUX_FILELOCK_H
4
5 #include <linux/fs.h>
6
7 #define FL_POSIX        1
8 #define FL_FLOCK        2
9 #define FL_DELEG        4       /* NFSv4 delegation */
10 #define FL_ACCESS       8       /* not trying to lock, just looking */
11 #define FL_EXISTS       16      /* when unlocking, test for existence */
12 #define FL_LEASE        32      /* lease held on this file */
13 #define FL_CLOSE        64      /* unlock on close */
14 #define FL_SLEEP        128     /* A blocking lock */
15 #define FL_DOWNGRADE_PENDING    256 /* Lease is being downgraded */
16 #define FL_UNLOCK_PENDING       512 /* Lease is being broken */
17 #define FL_OFDLCK       1024    /* lock is "owned" by struct file */
18 #define FL_LAYOUT       2048    /* outstanding pNFS layout */
19 #define FL_RECLAIM      4096    /* reclaiming from a reboot server */
20
21 #define FL_CLOSE_POSIX (FL_POSIX | FL_CLOSE)
22
23 /*
24  * Special return value from posix_lock_file() and vfs_lock_file() for
25  * asynchronous locking.
26  */
27 #define FILE_LOCK_DEFERRED 1
28
29 struct file_lock;
30
31 struct file_lock_operations {
32         void (*fl_copy_lock)(struct file_lock *, struct file_lock *);
33         void (*fl_release_private)(struct file_lock *);
34 };
35
36 struct lock_manager_operations {
37         void *lm_mod_owner;
38         fl_owner_t (*lm_get_owner)(fl_owner_t);
39         void (*lm_put_owner)(fl_owner_t);
40         void (*lm_notify)(struct file_lock *);  /* unblock callback */
41         int (*lm_grant)(struct file_lock *, int);
42         bool (*lm_break)(struct file_lock *);
43         int (*lm_change)(struct file_lock *, int, struct list_head *);
44         void (*lm_setup)(struct file_lock *, void **);
45         bool (*lm_breaker_owns_lease)(struct file_lock *);
46         bool (*lm_lock_expirable)(struct file_lock *cfl);
47         void (*lm_expire_lock)(void);
48 };
49
50 struct lock_manager {
51         struct list_head list;
52         /*
53          * NFSv4 and up also want opens blocked during the grace period;
54          * NLM doesn't care:
55          */
56         bool block_opens;
57 };
58
59 struct net;
60 void locks_start_grace(struct net *, struct lock_manager *);
61 void locks_end_grace(struct lock_manager *);
62 bool locks_in_grace(struct net *);
63 bool opens_in_grace(struct net *);
64
65 /*
66  * struct file_lock has a union that some filesystems use to track
67  * their own private info. The NFS side of things is defined here:
68  */
69 #include <linux/nfs_fs_i.h>
70
71 /*
72  * struct file_lock represents a generic "file lock". It's used to represent
73  * POSIX byte range locks, BSD (flock) locks, and leases. It's important to
74  * note that the same struct is used to represent both a request for a lock and
75  * the lock itself, but the same object is never used for both.
76  *
77  * FIXME: should we create a separate "struct lock_request" to help distinguish
78  * these two uses?
79  *
80  * The varous i_flctx lists are ordered by:
81  *
82  * 1) lock owner
83  * 2) lock range start
84  * 3) lock range end
85  *
86  * Obviously, the last two criteria only matter for POSIX locks.
87  */
88 struct file_lock {
89         struct file_lock *fl_blocker;   /* The lock, that is blocking us */
90         struct list_head fl_list;       /* link into file_lock_context */
91         struct hlist_node fl_link;      /* node in global lists */
92         struct list_head fl_blocked_requests;   /* list of requests with
93                                                  * ->fl_blocker pointing here
94                                                  */
95         struct list_head fl_blocked_member;     /* node in
96                                                  * ->fl_blocker->fl_blocked_requests
97                                                  */
98         fl_owner_t fl_owner;
99         unsigned int fl_flags;
100         unsigned char fl_type;
101         unsigned int fl_pid;
102         int fl_link_cpu;                /* what cpu's list is this on? */
103         wait_queue_head_t fl_wait;
104         struct file *fl_file;
105         loff_t fl_start;
106         loff_t fl_end;
107
108         struct fasync_struct *  fl_fasync; /* for lease break notifications */
109         /* for lease breaks: */
110         unsigned long fl_break_time;
111         unsigned long fl_downgrade_time;
112
113         const struct file_lock_operations *fl_ops;      /* Callbacks for filesystems */
114         const struct lock_manager_operations *fl_lmops; /* Callbacks for lockmanagers */
115         union {
116                 struct nfs_lock_info    nfs_fl;
117                 struct nfs4_lock_info   nfs4_fl;
118                 struct {
119                         struct list_head link;  /* link in AFS vnode's pending_locks list */
120                         int state;              /* state of grant or error if -ve */
121                         unsigned int    debug_id;
122                 } afs;
123                 struct {
124                         struct inode *inode;
125                 } ceph;
126         } fl_u;
127 } __randomize_layout;
128
129 struct file_lock_context {
130         spinlock_t              flc_lock;
131         struct list_head        flc_flock;
132         struct list_head        flc_posix;
133         struct list_head        flc_lease;
134 };
135
136 #define locks_inode(f) file_inode(f)
137
138 #ifdef CONFIG_FILE_LOCKING
139 int fcntl_getlk(struct file *, unsigned int, struct flock *);
140 int fcntl_setlk(unsigned int, struct file *, unsigned int,
141                         struct flock *);
142
143 #if BITS_PER_LONG == 32
144 int fcntl_getlk64(struct file *, unsigned int, struct flock64 *);
145 int fcntl_setlk64(unsigned int, struct file *, unsigned int,
146                         struct flock64 *);
147 #endif
148
149 int fcntl_setlease(unsigned int fd, struct file *filp, long arg);
150 int fcntl_getlease(struct file *filp);
151
152 /* fs/locks.c */
153 void locks_free_lock_context(struct inode *inode);
154 void locks_free_lock(struct file_lock *fl);
155 void locks_init_lock(struct file_lock *);
156 struct file_lock * locks_alloc_lock(void);
157 void locks_copy_lock(struct file_lock *, struct file_lock *);
158 void locks_copy_conflock(struct file_lock *, struct file_lock *);
159 void locks_remove_posix(struct file *, fl_owner_t);
160 void locks_remove_file(struct file *);
161 void locks_release_private(struct file_lock *);
162 void posix_test_lock(struct file *, struct file_lock *);
163 int posix_lock_file(struct file *, struct file_lock *, struct file_lock *);
164 int locks_delete_block(struct file_lock *);
165 int vfs_test_lock(struct file *, struct file_lock *);
166 int vfs_lock_file(struct file *, unsigned int, struct file_lock *, struct file_lock *);
167 int vfs_cancel_lock(struct file *filp, struct file_lock *fl);
168 bool vfs_inode_has_locks(struct inode *inode);
169 int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl);
170 int __break_lease(struct inode *inode, unsigned int flags, unsigned int type);
171 void lease_get_mtime(struct inode *, struct timespec64 *time);
172 int generic_setlease(struct file *, long, struct file_lock **, void **priv);
173 int vfs_setlease(struct file *, long, struct file_lock **, void **);
174 int lease_modify(struct file_lock *, int, struct list_head *);
175
176 struct notifier_block;
177 int lease_register_notifier(struct notifier_block *);
178 void lease_unregister_notifier(struct notifier_block *);
179
180 struct files_struct;
181 void show_fd_locks(struct seq_file *f,
182                          struct file *filp, struct files_struct *files);
183 bool locks_owner_has_blockers(struct file_lock_context *flctx,
184                         fl_owner_t owner);
185
186 static inline struct file_lock_context *
187 locks_inode_context(const struct inode *inode)
188 {
189         return smp_load_acquire(&inode->i_flctx);
190 }
191
192 #else /* !CONFIG_FILE_LOCKING */
193 static inline int fcntl_getlk(struct file *file, unsigned int cmd,
194                               struct flock __user *user)
195 {
196         return -EINVAL;
197 }
198
199 static inline int fcntl_setlk(unsigned int fd, struct file *file,
200                               unsigned int cmd, struct flock __user *user)
201 {
202         return -EACCES;
203 }
204
205 #if BITS_PER_LONG == 32
206 static inline int fcntl_getlk64(struct file *file, unsigned int cmd,
207                                 struct flock64 *user)
208 {
209         return -EINVAL;
210 }
211
212 static inline int fcntl_setlk64(unsigned int fd, struct file *file,
213                                 unsigned int cmd, struct flock64 *user)
214 {
215         return -EACCES;
216 }
217 #endif
218 static inline int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
219 {
220         return -EINVAL;
221 }
222
223 static inline int fcntl_getlease(struct file *filp)
224 {
225         return F_UNLCK;
226 }
227
228 static inline void
229 locks_free_lock_context(struct inode *inode)
230 {
231 }
232
233 static inline void locks_init_lock(struct file_lock *fl)
234 {
235         return;
236 }
237
238 static inline void locks_copy_conflock(struct file_lock *new, struct file_lock *fl)
239 {
240         return;
241 }
242
243 static inline void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
244 {
245         return;
246 }
247
248 static inline void locks_remove_posix(struct file *filp, fl_owner_t owner)
249 {
250         return;
251 }
252
253 static inline void locks_remove_file(struct file *filp)
254 {
255         return;
256 }
257
258 static inline void posix_test_lock(struct file *filp, struct file_lock *fl)
259 {
260         return;
261 }
262
263 static inline int posix_lock_file(struct file *filp, struct file_lock *fl,
264                                   struct file_lock *conflock)
265 {
266         return -ENOLCK;
267 }
268
269 static inline int locks_delete_block(struct file_lock *waiter)
270 {
271         return -ENOENT;
272 }
273
274 static inline int vfs_test_lock(struct file *filp, struct file_lock *fl)
275 {
276         return 0;
277 }
278
279 static inline int vfs_lock_file(struct file *filp, unsigned int cmd,
280                                 struct file_lock *fl, struct file_lock *conf)
281 {
282         return -ENOLCK;
283 }
284
285 static inline int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
286 {
287         return 0;
288 }
289
290 static inline bool vfs_inode_has_locks(struct inode *inode)
291 {
292         return false;
293 }
294
295 static inline int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl)
296 {
297         return -ENOLCK;
298 }
299
300 static inline int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
301 {
302         return 0;
303 }
304
305 static inline void lease_get_mtime(struct inode *inode,
306                                    struct timespec64 *time)
307 {
308         return;
309 }
310
311 static inline int generic_setlease(struct file *filp, long arg,
312                                     struct file_lock **flp, void **priv)
313 {
314         return -EINVAL;
315 }
316
317 static inline int vfs_setlease(struct file *filp, long arg,
318                                struct file_lock **lease, void **priv)
319 {
320         return -EINVAL;
321 }
322
323 static inline int lease_modify(struct file_lock *fl, int arg,
324                                struct list_head *dispose)
325 {
326         return -EINVAL;
327 }
328
329 struct files_struct;
330 static inline void show_fd_locks(struct seq_file *f,
331                         struct file *filp, struct files_struct *files) {}
332 static inline bool locks_owner_has_blockers(struct file_lock_context *flctx,
333                         fl_owner_t owner)
334 {
335         return false;
336 }
337
338 static inline struct file_lock_context *
339 locks_inode_context(const struct inode *inode)
340 {
341         return NULL;
342 }
343
344 #endif /* !CONFIG_FILE_LOCKING */
345
346 static inline int locks_lock_file_wait(struct file *filp, struct file_lock *fl)
347 {
348         return locks_lock_inode_wait(locks_inode(filp), fl);
349 }
350
351 #ifdef CONFIG_FILE_LOCKING
352 static inline int break_lease(struct inode *inode, unsigned int mode)
353 {
354         /*
355          * Since this check is lockless, we must ensure that any refcounts
356          * taken are done before checking i_flctx->flc_lease. Otherwise, we
357          * could end up racing with tasks trying to set a new lease on this
358          * file.
359          */
360         smp_mb();
361         if (inode->i_flctx && !list_empty_careful(&inode->i_flctx->flc_lease))
362                 return __break_lease(inode, mode, FL_LEASE);
363         return 0;
364 }
365
366 static inline int break_deleg(struct inode *inode, unsigned int mode)
367 {
368         /*
369          * Since this check is lockless, we must ensure that any refcounts
370          * taken are done before checking i_flctx->flc_lease. Otherwise, we
371          * could end up racing with tasks trying to set a new lease on this
372          * file.
373          */
374         smp_mb();
375         if (inode->i_flctx && !list_empty_careful(&inode->i_flctx->flc_lease))
376                 return __break_lease(inode, mode, FL_DELEG);
377         return 0;
378 }
379
380 static inline int try_break_deleg(struct inode *inode, struct inode **delegated_inode)
381 {
382         int ret;
383
384         ret = break_deleg(inode, O_WRONLY|O_NONBLOCK);
385         if (ret == -EWOULDBLOCK && delegated_inode) {
386                 *delegated_inode = inode;
387                 ihold(inode);
388         }
389         return ret;
390 }
391
392 static inline int break_deleg_wait(struct inode **delegated_inode)
393 {
394         int ret;
395
396         ret = break_deleg(*delegated_inode, O_WRONLY);
397         iput(*delegated_inode);
398         *delegated_inode = NULL;
399         return ret;
400 }
401
402 static inline int break_layout(struct inode *inode, bool wait)
403 {
404         smp_mb();
405         if (inode->i_flctx && !list_empty_careful(&inode->i_flctx->flc_lease))
406                 return __break_lease(inode,
407                                 wait ? O_WRONLY : O_WRONLY | O_NONBLOCK,
408                                 FL_LAYOUT);
409         return 0;
410 }
411
412 #else /* !CONFIG_FILE_LOCKING */
413 static inline int break_lease(struct inode *inode, unsigned int mode)
414 {
415         return 0;
416 }
417
418 static inline int break_deleg(struct inode *inode, unsigned int mode)
419 {
420         return 0;
421 }
422
423 static inline int try_break_deleg(struct inode *inode, struct inode **delegated_inode)
424 {
425         return 0;
426 }
427
428 static inline int break_deleg_wait(struct inode **delegated_inode)
429 {
430         BUG();
431         return 0;
432 }
433
434 static inline int break_layout(struct inode *inode, bool wait)
435 {
436         return 0;
437 }
438
439 #endif /* CONFIG_FILE_LOCKING */
440
441 #endif /* _LINUX_FILELOCK_H */