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