Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
29struct file_lock;
30struct file_lease;
31
32struct file_lock_operations {
33 void (*fl_copy_lock)(struct file_lock *, struct file_lock *);
34 void (*fl_release_private)(struct file_lock *);
35};
36
37struct 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
47struct 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
55struct 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
64struct net;
65void locks_start_grace(struct net *, struct lock_manager *);
66void locks_end_grace(struct lock_manager *);
67bool locks_in_grace(struct net *);
68bool 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
94struct 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
113struct 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
134struct 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
143struct 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
151int fcntl_getlk(struct file *, unsigned int, struct flock *);
152int fcntl_setlk(unsigned int, struct file *, unsigned int,
153 struct flock *);
154
155#if BITS_PER_LONG == 32
156int fcntl_getlk64(struct file *, unsigned int, struct flock64 *);
157int fcntl_setlk64(unsigned int, struct file *, unsigned int,
158 struct flock64 *);
159#endif
160
161int fcntl_setlease(unsigned int fd, struct file *filp, int arg);
162int fcntl_getlease(struct file *filp);
163int fcntl_setdeleg(unsigned int fd, struct file *filp, struct delegation *deleg);
164int fcntl_getdeleg(struct file *filp, struct delegation *deleg);
165
166static inline bool lock_is_unlock(struct file_lock *fl)
167{
168 return fl->c.flc_type == F_UNLCK;
169}
170
171static inline bool lock_is_read(struct file_lock *fl)
172{
173 return fl->c.flc_type == F_RDLCK;
174}
175
176static inline bool lock_is_write(struct file_lock *fl)
177{
178 return fl->c.flc_type == F_WRLCK;
179}
180
181static inline void locks_wake_up_waiter(struct file_lock_core *flc)
182{
183 wake_up(&flc->flc_wait);
184}
185
186static inline void locks_wake_up(struct file_lock *fl)
187{
188 locks_wake_up_waiter(&fl->c);
189}
190
191static 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 */
197void locks_free_lock_context(struct inode *inode);
198void locks_free_lock(struct file_lock *fl);
199void locks_init_lock(struct file_lock *);
200struct file_lock *locks_alloc_lock(void);
201void locks_copy_lock(struct file_lock *, struct file_lock *);
202void locks_copy_conflock(struct file_lock *, struct file_lock *);
203void locks_remove_posix(struct file *, fl_owner_t);
204void locks_remove_file(struct file *);
205void locks_release_private(struct file_lock *);
206void posix_test_lock(struct file *, struct file_lock *);
207int posix_lock_file(struct file *, struct file_lock *, struct file_lock *);
208int locks_delete_block(struct file_lock *);
209int vfs_test_lock(struct file *, struct file_lock *);
210int vfs_lock_file(struct file *, unsigned int, struct file_lock *, struct file_lock *);
211int vfs_cancel_lock(struct file *filp, struct file_lock *fl);
212bool vfs_inode_has_locks(struct inode *inode);
213int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl);
214
215void locks_init_lease(struct file_lease *);
216void locks_free_lease(struct file_lease *fl);
217struct 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
225int __break_lease(struct inode *inode, unsigned int flags);
226void lease_get_mtime(struct inode *, struct timespec64 *time);
227int generic_setlease(struct file *, int, struct file_lease **, void **priv);
228int kernel_setlease(struct file *, int, struct file_lease **, void **);
229int vfs_setlease(struct file *, int, struct file_lease **, void **);
230int lease_modify(struct file_lease *, int, struct list_head *);
231
232struct notifier_block;
233int lease_register_notifier(struct notifier_block *);
234void lease_unregister_notifier(struct notifier_block *);
235
236struct files_struct;
237void show_fd_locks(struct seq_file *f,
238 struct file *filp, struct files_struct *files);
239bool locks_owner_has_blockers(struct file_lock_context *flctx,
240 fl_owner_t owner);
241
242static inline struct file_lock_context *
243locks_inode_context(const struct inode *inode)
244{
245 return smp_load_acquire(&inode->i_flctx);
246}
247
248#else /* !CONFIG_FILE_LOCKING */
249static inline int fcntl_getlk(struct file *file, unsigned int cmd,
250 struct flock __user *user)
251{
252 return -EINVAL;
253}
254
255static 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
262static inline int fcntl_getlk64(struct file *file, unsigned int cmd,
263 struct flock64 *user)
264{
265 return -EINVAL;
266}
267
268static inline int fcntl_setlk64(unsigned int fd, struct file *file,
269 unsigned int cmd, struct flock64 *user)
270{
271 return -EACCES;
272}
273#endif
274static inline int fcntl_setlease(unsigned int fd, struct file *filp, int arg)
275{
276 return -EINVAL;
277}
278
279static inline int fcntl_getlease(struct file *filp)
280{
281 return F_UNLCK;
282}
283
284static inline int fcntl_setdeleg(unsigned int fd, struct file *filp, struct delegation *deleg)
285{
286 return -EINVAL;
287}
288
289static inline int fcntl_getdeleg(struct file *filp, struct delegation *deleg)
290{
291 return -EINVAL;
292}
293
294static inline bool lock_is_unlock(struct file_lock *fl)
295{
296 return false;
297}
298
299static inline bool lock_is_read(struct file_lock *fl)
300{
301 return false;
302}
303
304static inline bool lock_is_write(struct file_lock *fl)
305{
306 return false;
307}
308
309static inline void locks_wake_up(struct file_lock *fl)
310{
311}
312
313static inline void
314locks_free_lock_context(struct inode *inode)
315{
316}
317
318static inline void locks_init_lock(struct file_lock *fl)
319{
320 return;
321}
322
323static inline void locks_init_lease(struct file_lease *fl)
324{
325 return;
326}
327
328static inline void locks_copy_conflock(struct file_lock *new, struct file_lock *fl)
329{
330 return;
331}
332
333static inline void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
334{
335 return;
336}
337
338static inline void locks_remove_posix(struct file *filp, fl_owner_t owner)
339{
340 return;
341}
342
343static inline void locks_remove_file(struct file *filp)
344{
345 return;
346}
347
348static inline void posix_test_lock(struct file *filp, struct file_lock *fl)
349{
350 return;
351}
352
353static inline int posix_lock_file(struct file *filp, struct file_lock *fl,
354 struct file_lock *conflock)
355{
356 return -ENOLCK;
357}
358
359static inline int locks_delete_block(struct file_lock *waiter)
360{
361 return -ENOENT;
362}
363
364static inline int vfs_test_lock(struct file *filp, struct file_lock *fl)
365{
366 return 0;
367}
368
369static 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
375static inline int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
376{
377 return 0;
378}
379
380static inline bool vfs_inode_has_locks(struct inode *inode)
381{
382 return false;
383}
384
385static inline int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl)
386{
387 return -ENOLCK;
388}
389
390static inline int __break_lease(struct inode *inode, unsigned int flags)
391{
392 return 0;
393}
394
395static inline void lease_get_mtime(struct inode *inode,
396 struct timespec64 *time)
397{
398 return;
399}
400
401static inline int generic_setlease(struct file *filp, int arg,
402 struct file_lease **flp, void **priv)
403{
404 return -EINVAL;
405}
406
407static inline int kernel_setlease(struct file *filp, int arg,
408 struct file_lease **lease, void **priv)
409{
410 return -EINVAL;
411}
412
413static inline int vfs_setlease(struct file *filp, int arg,
414 struct file_lease **lease, void **priv)
415{
416 return -EINVAL;
417}
418
419static inline int lease_modify(struct file_lease *fl, int arg,
420 struct list_head *dispose)
421{
422 return -EINVAL;
423}
424
425struct files_struct;
426static inline void show_fd_locks(struct seq_file *f,
427 struct file *filp, struct files_struct *files) {}
428static inline bool locks_owner_has_blockers(struct file_lock_context *flctx,
429 fl_owner_t owner)
430{
431 return false;
432}
433
434static inline struct file_lock_context *
435locks_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
445static 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
451static 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
462static 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
481static 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
502struct delegated_inode {
503 struct inode *di_inode;
504};
505
506static inline bool is_delegated(struct delegated_inode *di)
507{
508 return di->di_inode;
509}
510
511static 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
524static 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
534static 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 */
549struct delegated_inode { };
550
551static inline bool is_delegated(struct delegated_inode *di)
552{
553 return false;
554}
555
556static inline int break_lease(struct inode *inode, bool wait)
557{
558 return 0;
559}
560
561static inline int break_deleg(struct inode *inode, unsigned int flags)
562{
563 return 0;
564}
565
566static inline int try_break_deleg(struct inode *inode,
567 struct delegated_inode *delegated_inode)
568{
569 return 0;
570}
571
572static inline int break_deleg_wait(struct delegated_inode *delegated_inode)
573{
574 BUG();
575 return 0;
576}
577
578static 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 */