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 struct file_lease;
31
32 struct file_lock_operations {
33 void (*fl_copy_lock)(struct file_lock *, struct file_lock *);
34 void (*fl_release_private)(struct file_lock *);
35 };
36
37 struct lock_manager_operations {
38 void *lm_mod_owner;
39 fl_owner_t (*lm_get_owner)(fl_owner_t);
40 void (*lm_put_owner)(fl_owner_t);
41 void (*lm_notify)(struct file_lock *); /* unblock callback */
42 int (*lm_grant)(struct file_lock *, int);
43 bool (*lm_lock_expirable)(struct file_lock *cfl);
44 void (*lm_expire_lock)(void);
45 };
46
47 struct lease_manager_operations {
48 bool (*lm_break)(struct file_lease *);
49 int (*lm_change)(struct file_lease *, int, struct list_head *);
50 void (*lm_setup)(struct file_lease *, void **);
51 bool (*lm_breaker_owns_lease)(struct file_lease *);
52 int (*lm_open_conflict)(struct file *, int);
53 };
54
55 struct lock_manager {
56 struct list_head list;
57 /*
58 * NFSv4 and up also want opens blocked during the grace period;
59 * NLM doesn't care:
60 */
61 bool block_opens;
62 };
63
64 struct net;
65 void locks_start_grace(struct net *, struct lock_manager *);
66 void locks_end_grace(struct lock_manager *);
67 bool locks_in_grace(struct net *);
68 bool opens_in_grace(struct net *);
69
70 /*
71 * struct file_lock has a union that some filesystems use to track
72 * their own private info. The NFS side of things is defined here:
73 */
74 #include <linux/nfs_fs_i.h>
75
76 /*
77 * struct file_lock represents a generic "file lock". It's used to represent
78 * POSIX byte range locks, BSD (flock) locks, and leases. It's important to
79 * note that the same struct is used to represent both a request for a lock and
80 * the lock itself, but the same object is never used for both.
81 *
82 * FIXME: should we create a separate "struct lock_request" to help distinguish
83 * these two uses?
84 *
85 * The varous i_flctx lists are ordered by:
86 *
87 * 1) lock owner
88 * 2) lock range start
89 * 3) lock range end
90 *
91 * Obviously, the last two criteria only matter for POSIX locks.
92 */
93
94 struct file_lock_core {
95 struct file_lock_core *flc_blocker; /* The lock that is blocking us */
96 struct list_head flc_list; /* link into file_lock_context */
97 struct hlist_node flc_link; /* node in global lists */
98 struct list_head flc_blocked_requests; /* list of requests with
99 * ->fl_blocker pointing here
100 */
101 struct list_head flc_blocked_member; /* node in
102 * ->fl_blocker->fl_blocked_requests
103 */
104 fl_owner_t flc_owner;
105 unsigned int flc_flags;
106 unsigned char flc_type;
107 pid_t flc_pid;
108 int flc_link_cpu; /* what cpu's list is this on? */
109 wait_queue_head_t flc_wait;
110 struct file *flc_file;
111 };
112
113 struct file_lock {
114 struct file_lock_core c;
115 loff_t fl_start;
116 loff_t fl_end;
117
118 const struct file_lock_operations *fl_ops; /* Callbacks for filesystems */
119 const struct lock_manager_operations *fl_lmops; /* Callbacks for lockmanagers */
120 union {
121 struct nfs_lock_info nfs_fl;
122 struct nfs4_lock_info nfs4_fl;
123 struct {
124 struct list_head link; /* link in AFS vnode's pending_locks list */
125 int state; /* state of grant or error if -ve */
126 unsigned int debug_id;
127 } afs;
128 struct {
129 struct inode *inode;
130 } ceph;
131 } fl_u;
132 } __randomize_layout;
133
134 struct file_lease {
135 struct file_lock_core c;
136 struct fasync_struct * fl_fasync; /* for lease break notifications */
137 /* for lease breaks: */
138 unsigned long fl_break_time;
139 unsigned long fl_downgrade_time;
140 const struct lease_manager_operations *fl_lmops; /* Callbacks for lease managers */
141 } __randomize_layout;
142
143 struct file_lock_context {
144 spinlock_t flc_lock;
145 struct list_head flc_flock;
146 struct list_head flc_posix;
147 struct list_head flc_lease;
148 };
149
150 #ifdef CONFIG_FILE_LOCKING
151 int fcntl_getlk(struct file *, unsigned int, struct flock *);
152 int fcntl_setlk(unsigned int, struct file *, unsigned int,
153 struct flock *);
154
155 #if BITS_PER_LONG == 32
156 int fcntl_getlk64(struct file *, unsigned int, struct flock64 *);
157 int fcntl_setlk64(unsigned int, struct file *, unsigned int,
158 struct flock64 *);
159 #endif
160
161 int fcntl_setlease(unsigned int fd, struct file *filp, int arg);
162 int fcntl_getlease(struct file *filp);
163 int fcntl_setdeleg(unsigned int fd, struct file *filp, struct delegation *deleg);
164 int fcntl_getdeleg(struct file *filp, struct delegation *deleg);
165
lock_is_unlock(struct file_lock * fl)166 static inline bool lock_is_unlock(struct file_lock *fl)
167 {
168 return fl->c.flc_type == F_UNLCK;
169 }
170
lock_is_read(struct file_lock * fl)171 static inline bool lock_is_read(struct file_lock *fl)
172 {
173 return fl->c.flc_type == F_RDLCK;
174 }
175
lock_is_write(struct file_lock * fl)176 static inline bool lock_is_write(struct file_lock *fl)
177 {
178 return fl->c.flc_type == F_WRLCK;
179 }
180
locks_wake_up_waiter(struct file_lock_core * flc)181 static inline void locks_wake_up_waiter(struct file_lock_core *flc)
182 {
183 wake_up(&flc->flc_wait);
184 }
185
locks_wake_up(struct file_lock * fl)186 static inline void locks_wake_up(struct file_lock *fl)
187 {
188 locks_wake_up_waiter(&fl->c);
189 }
190
locks_can_async_lock(const struct file_operations * fops)191 static inline bool locks_can_async_lock(const struct file_operations *fops)
192 {
193 return !fops->lock || fops->fop_flags & FOP_ASYNC_LOCK;
194 }
195
196 /* fs/locks.c */
197 void locks_free_lock_context(struct inode *inode);
198 void locks_free_lock(struct file_lock *fl);
199 void locks_init_lock(struct file_lock *);
200 struct file_lock *locks_alloc_lock(void);
201 void locks_copy_lock(struct file_lock *, struct file_lock *);
202 void locks_copy_conflock(struct file_lock *, struct file_lock *);
203 void locks_remove_posix(struct file *, fl_owner_t);
204 void locks_remove_file(struct file *);
205 void locks_release_private(struct file_lock *);
206 void posix_test_lock(struct file *, struct file_lock *);
207 int posix_lock_file(struct file *, struct file_lock *, struct file_lock *);
208 int locks_delete_block(struct file_lock *);
209 int vfs_test_lock(struct file *, struct file_lock *);
210 int vfs_lock_file(struct file *, unsigned int, struct file_lock *, struct file_lock *);
211 int vfs_cancel_lock(struct file *filp, struct file_lock *fl);
212 bool vfs_inode_has_locks(struct inode *inode);
213 int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl);
214
215 void locks_init_lease(struct file_lease *);
216 void locks_free_lease(struct file_lease *fl);
217 struct file_lease *locks_alloc_lease(void);
218
219 #define LEASE_BREAK_LEASE BIT(0) // break leases and delegations
220 #define LEASE_BREAK_DELEG BIT(1) // break delegations only
221 #define LEASE_BREAK_LAYOUT BIT(2) // break layouts only
222 #define LEASE_BREAK_NONBLOCK BIT(3) // non-blocking break
223 #define LEASE_BREAK_OPEN_RDONLY BIT(4) // readonly open event
224
225 int __break_lease(struct inode *inode, unsigned int flags);
226 void lease_get_mtime(struct inode *, struct timespec64 *time);
227 int generic_setlease(struct file *, int, struct file_lease **, void **priv);
228 int kernel_setlease(struct file *, int, struct file_lease **, void **);
229 int vfs_setlease(struct file *, int, struct file_lease **, void **);
230 int lease_modify(struct file_lease *, int, struct list_head *);
231
232 struct notifier_block;
233 int lease_register_notifier(struct notifier_block *);
234 void lease_unregister_notifier(struct notifier_block *);
235
236 struct files_struct;
237 void show_fd_locks(struct seq_file *f,
238 struct file *filp, struct files_struct *files);
239 bool locks_owner_has_blockers(struct file_lock_context *flctx,
240 fl_owner_t owner);
241
242 static inline struct file_lock_context *
locks_inode_context(const struct inode * inode)243 locks_inode_context(const struct inode *inode)
244 {
245 return smp_load_acquire(&inode->i_flctx);
246 }
247
248 #else /* !CONFIG_FILE_LOCKING */
fcntl_getlk(struct file * file,unsigned int cmd,struct flock __user * user)249 static inline int fcntl_getlk(struct file *file, unsigned int cmd,
250 struct flock __user *user)
251 {
252 return -EINVAL;
253 }
254
fcntl_setlk(unsigned int fd,struct file * file,unsigned int cmd,struct flock __user * user)255 static inline int fcntl_setlk(unsigned int fd, struct file *file,
256 unsigned int cmd, struct flock __user *user)
257 {
258 return -EACCES;
259 }
260
261 #if BITS_PER_LONG == 32
fcntl_getlk64(struct file * file,unsigned int cmd,struct flock64 * user)262 static inline int fcntl_getlk64(struct file *file, unsigned int cmd,
263 struct flock64 *user)
264 {
265 return -EINVAL;
266 }
267
fcntl_setlk64(unsigned int fd,struct file * file,unsigned int cmd,struct flock64 * user)268 static inline int fcntl_setlk64(unsigned int fd, struct file *file,
269 unsigned int cmd, struct flock64 *user)
270 {
271 return -EACCES;
272 }
273 #endif
fcntl_setlease(unsigned int fd,struct file * filp,int arg)274 static inline int fcntl_setlease(unsigned int fd, struct file *filp, int arg)
275 {
276 return -EINVAL;
277 }
278
fcntl_getlease(struct file * filp)279 static inline int fcntl_getlease(struct file *filp)
280 {
281 return F_UNLCK;
282 }
283
fcntl_setdeleg(unsigned int fd,struct file * filp,struct delegation * deleg)284 static inline int fcntl_setdeleg(unsigned int fd, struct file *filp, struct delegation *deleg)
285 {
286 return -EINVAL;
287 }
288
fcntl_getdeleg(struct file * filp,struct delegation * deleg)289 static inline int fcntl_getdeleg(struct file *filp, struct delegation *deleg)
290 {
291 return -EINVAL;
292 }
293
lock_is_unlock(struct file_lock * fl)294 static inline bool lock_is_unlock(struct file_lock *fl)
295 {
296 return false;
297 }
298
lock_is_read(struct file_lock * fl)299 static inline bool lock_is_read(struct file_lock *fl)
300 {
301 return false;
302 }
303
lock_is_write(struct file_lock * fl)304 static inline bool lock_is_write(struct file_lock *fl)
305 {
306 return false;
307 }
308
locks_wake_up(struct file_lock * fl)309 static inline void locks_wake_up(struct file_lock *fl)
310 {
311 }
312
313 static inline void
locks_free_lock_context(struct inode * inode)314 locks_free_lock_context(struct inode *inode)
315 {
316 }
317
locks_init_lock(struct file_lock * fl)318 static inline void locks_init_lock(struct file_lock *fl)
319 {
320 return;
321 }
322
locks_init_lease(struct file_lease * fl)323 static inline void locks_init_lease(struct file_lease *fl)
324 {
325 return;
326 }
327
locks_copy_conflock(struct file_lock * new,struct file_lock * fl)328 static inline void locks_copy_conflock(struct file_lock *new, struct file_lock *fl)
329 {
330 return;
331 }
332
locks_copy_lock(struct file_lock * new,struct file_lock * fl)333 static inline void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
334 {
335 return;
336 }
337
locks_remove_posix(struct file * filp,fl_owner_t owner)338 static inline void locks_remove_posix(struct file *filp, fl_owner_t owner)
339 {
340 return;
341 }
342
locks_remove_file(struct file * filp)343 static inline void locks_remove_file(struct file *filp)
344 {
345 return;
346 }
347
posix_test_lock(struct file * filp,struct file_lock * fl)348 static inline void posix_test_lock(struct file *filp, struct file_lock *fl)
349 {
350 return;
351 }
352
posix_lock_file(struct file * filp,struct file_lock * fl,struct file_lock * conflock)353 static inline int posix_lock_file(struct file *filp, struct file_lock *fl,
354 struct file_lock *conflock)
355 {
356 return -ENOLCK;
357 }
358
locks_delete_block(struct file_lock * waiter)359 static inline int locks_delete_block(struct file_lock *waiter)
360 {
361 return -ENOENT;
362 }
363
vfs_test_lock(struct file * filp,struct file_lock * fl)364 static inline int vfs_test_lock(struct file *filp, struct file_lock *fl)
365 {
366 return 0;
367 }
368
vfs_lock_file(struct file * filp,unsigned int cmd,struct file_lock * fl,struct file_lock * conf)369 static inline int vfs_lock_file(struct file *filp, unsigned int cmd,
370 struct file_lock *fl, struct file_lock *conf)
371 {
372 return -ENOLCK;
373 }
374
vfs_cancel_lock(struct file * filp,struct file_lock * fl)375 static inline int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
376 {
377 return 0;
378 }
379
vfs_inode_has_locks(struct inode * inode)380 static inline bool vfs_inode_has_locks(struct inode *inode)
381 {
382 return false;
383 }
384
locks_lock_inode_wait(struct inode * inode,struct file_lock * fl)385 static inline int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl)
386 {
387 return -ENOLCK;
388 }
389
__break_lease(struct inode * inode,unsigned int flags)390 static inline int __break_lease(struct inode *inode, unsigned int flags)
391 {
392 return 0;
393 }
394
lease_get_mtime(struct inode * inode,struct timespec64 * time)395 static inline void lease_get_mtime(struct inode *inode,
396 struct timespec64 *time)
397 {
398 return;
399 }
400
generic_setlease(struct file * filp,int arg,struct file_lease ** flp,void ** priv)401 static inline int generic_setlease(struct file *filp, int arg,
402 struct file_lease **flp, void **priv)
403 {
404 return -EINVAL;
405 }
406
kernel_setlease(struct file * filp,int arg,struct file_lease ** lease,void ** priv)407 static inline int kernel_setlease(struct file *filp, int arg,
408 struct file_lease **lease, void **priv)
409 {
410 return -EINVAL;
411 }
412
vfs_setlease(struct file * filp,int arg,struct file_lease ** lease,void ** priv)413 static inline int vfs_setlease(struct file *filp, int arg,
414 struct file_lease **lease, void **priv)
415 {
416 return -EINVAL;
417 }
418
lease_modify(struct file_lease * fl,int arg,struct list_head * dispose)419 static inline int lease_modify(struct file_lease *fl, int arg,
420 struct list_head *dispose)
421 {
422 return -EINVAL;
423 }
424
425 struct files_struct;
show_fd_locks(struct seq_file * f,struct file * filp,struct files_struct * files)426 static inline void show_fd_locks(struct seq_file *f,
427 struct file *filp, struct files_struct *files) {}
locks_owner_has_blockers(struct file_lock_context * flctx,fl_owner_t owner)428 static inline bool locks_owner_has_blockers(struct file_lock_context *flctx,
429 fl_owner_t owner)
430 {
431 return false;
432 }
433
434 static inline struct file_lock_context *
locks_inode_context(const struct inode * inode)435 locks_inode_context(const struct inode *inode)
436 {
437 return NULL;
438 }
439
440 #endif /* !CONFIG_FILE_LOCKING */
441
442 /* for walking lists of file_locks linked by fl_list */
443 #define for_each_file_lock(_fl, _head) list_for_each_entry(_fl, _head, c.flc_list)
444
locks_lock_file_wait(struct file * filp,struct file_lock * fl)445 static inline int locks_lock_file_wait(struct file *filp, struct file_lock *fl)
446 {
447 return locks_lock_inode_wait(file_inode(filp), fl);
448 }
449
450 #ifdef CONFIG_FILE_LOCKING
openmode_to_lease_flags(unsigned int mode)451 static inline unsigned int openmode_to_lease_flags(unsigned int mode)
452 {
453 unsigned int flags = 0;
454
455 if ((mode & O_ACCMODE) == O_RDONLY)
456 flags |= LEASE_BREAK_OPEN_RDONLY;
457 if (mode & O_NONBLOCK)
458 flags |= LEASE_BREAK_NONBLOCK;
459 return flags;
460 }
461
break_lease(struct inode * inode,unsigned int mode)462 static inline int break_lease(struct inode *inode, unsigned int mode)
463 {
464 struct file_lock_context *flctx;
465
466 /*
467 * Since this check is lockless, we must ensure that any refcounts
468 * taken are done before checking i_flctx->flc_lease. Otherwise, we
469 * could end up racing with tasks trying to set a new lease on this
470 * file.
471 */
472 flctx = READ_ONCE(inode->i_flctx);
473 if (!flctx)
474 return 0;
475 smp_mb();
476 if (!list_empty_careful(&flctx->flc_lease))
477 return __break_lease(inode, LEASE_BREAK_LEASE | openmode_to_lease_flags(mode));
478 return 0;
479 }
480
break_deleg(struct inode * inode,unsigned int flags)481 static inline int break_deleg(struct inode *inode, unsigned int flags)
482 {
483 struct file_lock_context *flctx;
484
485 /*
486 * Since this check is lockless, we must ensure that any refcounts
487 * taken are done before checking i_flctx->flc_lease. Otherwise, we
488 * could end up racing with tasks trying to set a new lease on this
489 * file.
490 */
491 flctx = READ_ONCE(inode->i_flctx);
492 if (!flctx)
493 return 0;
494 smp_mb();
495 if (!list_empty_careful(&flctx->flc_lease)) {
496 flags |= LEASE_BREAK_DELEG;
497 return __break_lease(inode, flags);
498 }
499 return 0;
500 }
501
502 struct delegated_inode {
503 struct inode *di_inode;
504 };
505
is_delegated(struct delegated_inode * di)506 static inline bool is_delegated(struct delegated_inode *di)
507 {
508 return di->di_inode;
509 }
510
try_break_deleg(struct inode * inode,struct delegated_inode * di)511 static inline int try_break_deleg(struct inode *inode,
512 struct delegated_inode *di)
513 {
514 int ret;
515
516 ret = break_deleg(inode, LEASE_BREAK_NONBLOCK);
517 if (ret == -EWOULDBLOCK && di) {
518 di->di_inode = inode;
519 ihold(inode);
520 }
521 return ret;
522 }
523
break_deleg_wait(struct delegated_inode * di)524 static inline int break_deleg_wait(struct delegated_inode *di)
525 {
526 int ret;
527
528 ret = break_deleg(di->di_inode, 0);
529 iput(di->di_inode);
530 di->di_inode = NULL;
531 return ret;
532 }
533
break_layout(struct inode * inode,bool wait)534 static inline int break_layout(struct inode *inode, bool wait)
535 {
536 smp_mb();
537 if (inode->i_flctx && !list_empty_careful(&inode->i_flctx->flc_lease)) {
538 unsigned int flags = LEASE_BREAK_LAYOUT;
539
540 if (!wait)
541 flags |= LEASE_BREAK_NONBLOCK;
542
543 return __break_lease(inode, flags);
544 }
545 return 0;
546 }
547
548 #else /* !CONFIG_FILE_LOCKING */
549 struct delegated_inode { };
550
is_delegated(struct delegated_inode * di)551 static inline bool is_delegated(struct delegated_inode *di)
552 {
553 return false;
554 }
555
break_lease(struct inode * inode,bool wait)556 static inline int break_lease(struct inode *inode, bool wait)
557 {
558 return 0;
559 }
560
break_deleg(struct inode * inode,unsigned int flags)561 static inline int break_deleg(struct inode *inode, unsigned int flags)
562 {
563 return 0;
564 }
565
try_break_deleg(struct inode * inode,struct delegated_inode * delegated_inode)566 static inline int try_break_deleg(struct inode *inode,
567 struct delegated_inode *delegated_inode)
568 {
569 return 0;
570 }
571
break_deleg_wait(struct delegated_inode * delegated_inode)572 static inline int break_deleg_wait(struct delegated_inode *delegated_inode)
573 {
574 BUG();
575 return 0;
576 }
577
break_layout(struct inode * inode,bool wait)578 static inline int break_layout(struct inode *inode, bool wait)
579 {
580 return 0;
581 }
582
583 #endif /* CONFIG_FILE_LOCKING */
584
585 #endif /* _LINUX_FILELOCK_H */
586