xref: /linux-6.15/include/linux/filelock.h (revision 8cf9a01e)
15970e15dSJeff Layton /* SPDX-License-Identifier: GPL-2.0 */
25970e15dSJeff Layton #ifndef _LINUX_FILELOCK_H
35970e15dSJeff Layton #define _LINUX_FILELOCK_H
45970e15dSJeff Layton 
55970e15dSJeff Layton #include <linux/fs.h>
65970e15dSJeff Layton 
75970e15dSJeff Layton #define FL_POSIX	1
85970e15dSJeff Layton #define FL_FLOCK	2
95970e15dSJeff Layton #define FL_DELEG	4	/* NFSv4 delegation */
105970e15dSJeff Layton #define FL_ACCESS	8	/* not trying to lock, just looking */
115970e15dSJeff Layton #define FL_EXISTS	16	/* when unlocking, test for existence */
125970e15dSJeff Layton #define FL_LEASE	32	/* lease held on this file */
135970e15dSJeff Layton #define FL_CLOSE	64	/* unlock on close */
145970e15dSJeff Layton #define FL_SLEEP	128	/* A blocking lock */
155970e15dSJeff Layton #define FL_DOWNGRADE_PENDING	256 /* Lease is being downgraded */
165970e15dSJeff Layton #define FL_UNLOCK_PENDING	512 /* Lease is being broken */
175970e15dSJeff Layton #define FL_OFDLCK	1024	/* lock is "owned" by struct file */
185970e15dSJeff Layton #define FL_LAYOUT	2048	/* outstanding pNFS layout */
195970e15dSJeff Layton #define FL_RECLAIM	4096	/* reclaiming from a reboot server */
205970e15dSJeff Layton 
215970e15dSJeff Layton #define FL_CLOSE_POSIX (FL_POSIX | FL_CLOSE)
225970e15dSJeff Layton 
235970e15dSJeff Layton /*
245970e15dSJeff Layton  * Special return value from posix_lock_file() and vfs_lock_file() for
255970e15dSJeff Layton  * asynchronous locking.
265970e15dSJeff Layton  */
275970e15dSJeff Layton #define FILE_LOCK_DEFERRED 1
285970e15dSJeff Layton 
295970e15dSJeff Layton struct file_lock;
30c69ff407SJeff Layton struct file_lease;
315970e15dSJeff Layton 
325970e15dSJeff Layton struct file_lock_operations {
335970e15dSJeff Layton 	void (*fl_copy_lock)(struct file_lock *, struct file_lock *);
345970e15dSJeff Layton 	void (*fl_release_private)(struct file_lock *);
355970e15dSJeff Layton };
365970e15dSJeff Layton 
375970e15dSJeff Layton struct lock_manager_operations {
385970e15dSJeff Layton 	void *lm_mod_owner;
395970e15dSJeff Layton 	fl_owner_t (*lm_get_owner)(fl_owner_t);
405970e15dSJeff Layton 	void (*lm_put_owner)(fl_owner_t);
415970e15dSJeff Layton 	void (*lm_notify)(struct file_lock *);	/* unblock callback */
425970e15dSJeff Layton 	int (*lm_grant)(struct file_lock *, int);
435970e15dSJeff Layton 	bool (*lm_lock_expirable)(struct file_lock *cfl);
445970e15dSJeff Layton 	void (*lm_expire_lock)(void);
455970e15dSJeff Layton };
465970e15dSJeff Layton 
47c69ff407SJeff Layton struct lease_manager_operations {
48c69ff407SJeff Layton 	bool (*lm_break)(struct file_lease *);
49c69ff407SJeff Layton 	int (*lm_change)(struct file_lease *, int, struct list_head *);
50c69ff407SJeff Layton 	void (*lm_setup)(struct file_lease *, void **);
51c69ff407SJeff Layton 	bool (*lm_breaker_owns_lease)(struct file_lease *);
52c69ff407SJeff Layton };
53c69ff407SJeff Layton 
545970e15dSJeff Layton struct lock_manager {
555970e15dSJeff Layton 	struct list_head list;
565970e15dSJeff Layton 	/*
575970e15dSJeff Layton 	 * NFSv4 and up also want opens blocked during the grace period;
585970e15dSJeff Layton 	 * NLM doesn't care:
595970e15dSJeff Layton 	 */
605970e15dSJeff Layton 	bool block_opens;
615970e15dSJeff Layton };
625970e15dSJeff Layton 
635970e15dSJeff Layton struct net;
645970e15dSJeff Layton void locks_start_grace(struct net *, struct lock_manager *);
655970e15dSJeff Layton void locks_end_grace(struct lock_manager *);
665970e15dSJeff Layton bool locks_in_grace(struct net *);
675970e15dSJeff Layton bool opens_in_grace(struct net *);
685970e15dSJeff Layton 
695970e15dSJeff Layton /*
705970e15dSJeff Layton  * struct file_lock has a union that some filesystems use to track
715970e15dSJeff Layton  * their own private info. The NFS side of things is defined here:
725970e15dSJeff Layton  */
735970e15dSJeff Layton #include <linux/nfs_fs_i.h>
745970e15dSJeff Layton 
755970e15dSJeff Layton /*
765970e15dSJeff Layton  * struct file_lock represents a generic "file lock". It's used to represent
775970e15dSJeff Layton  * POSIX byte range locks, BSD (flock) locks, and leases. It's important to
785970e15dSJeff Layton  * note that the same struct is used to represent both a request for a lock and
795970e15dSJeff Layton  * the lock itself, but the same object is never used for both.
805970e15dSJeff Layton  *
815970e15dSJeff Layton  * FIXME: should we create a separate "struct lock_request" to help distinguish
825970e15dSJeff Layton  * these two uses?
835970e15dSJeff Layton  *
845970e15dSJeff Layton  * The varous i_flctx lists are ordered by:
855970e15dSJeff Layton  *
865970e15dSJeff Layton  * 1) lock owner
875970e15dSJeff Layton  * 2) lock range start
885970e15dSJeff Layton  * 3) lock range end
895970e15dSJeff Layton  *
905970e15dSJeff Layton  * Obviously, the last two criteria only matter for POSIX locks.
915970e15dSJeff Layton  */
92a69ce85eSJeff Layton 
93a69ce85eSJeff Layton struct file_lock_core {
94b6aaba5bSJeff Layton 	struct file_lock_core *flc_blocker;	/* The lock that is blocking us */
95a69ce85eSJeff Layton 	struct list_head flc_list;	/* link into file_lock_context */
96a69ce85eSJeff Layton 	struct hlist_node flc_link;	/* node in global lists */
97a69ce85eSJeff Layton 	struct list_head flc_blocked_requests;	/* list of requests with
985970e15dSJeff Layton 						 * ->fl_blocker pointing here
995970e15dSJeff Layton 						 */
100a69ce85eSJeff Layton 	struct list_head flc_blocked_member;	/* node in
1015970e15dSJeff Layton 						 * ->fl_blocker->fl_blocked_requests
1025970e15dSJeff Layton 						 */
103a69ce85eSJeff Layton 	fl_owner_t flc_owner;
104a69ce85eSJeff Layton 	unsigned int flc_flags;
105a69ce85eSJeff Layton 	unsigned char flc_type;
106a69ce85eSJeff Layton 	pid_t flc_pid;
107a69ce85eSJeff Layton 	int flc_link_cpu;		/* what cpu's list is this on? */
108a69ce85eSJeff Layton 	wait_queue_head_t flc_wait;
109a69ce85eSJeff Layton 	struct file *flc_file;
110a69ce85eSJeff Layton };
111a69ce85eSJeff Layton 
112a69ce85eSJeff Layton struct file_lock {
113a69ce85eSJeff Layton 	struct file_lock_core c;
1145970e15dSJeff Layton 	loff_t fl_start;
1155970e15dSJeff Layton 	loff_t fl_end;
1165970e15dSJeff Layton 
1175970e15dSJeff Layton 	const struct file_lock_operations *fl_ops;	/* Callbacks for filesystems */
1185970e15dSJeff Layton 	const struct lock_manager_operations *fl_lmops;	/* Callbacks for lockmanagers */
1195970e15dSJeff Layton 	union {
1205970e15dSJeff Layton 		struct nfs_lock_info	nfs_fl;
1215970e15dSJeff Layton 		struct nfs4_lock_info	nfs4_fl;
1225970e15dSJeff Layton 		struct {
1235970e15dSJeff Layton 			struct list_head link;	/* link in AFS vnode's pending_locks list */
1245970e15dSJeff Layton 			int state;		/* state of grant or error if -ve */
1255970e15dSJeff Layton 			unsigned int	debug_id;
1265970e15dSJeff Layton 		} afs;
1275970e15dSJeff Layton 		struct {
1285970e15dSJeff Layton 			struct inode *inode;
1295970e15dSJeff Layton 		} ceph;
1305970e15dSJeff Layton 	} fl_u;
1315970e15dSJeff Layton } __randomize_layout;
1325970e15dSJeff Layton 
133c69ff407SJeff Layton struct file_lease {
134c69ff407SJeff Layton 	struct file_lock_core c;
135c69ff407SJeff Layton 	struct fasync_struct *	fl_fasync; /* for lease break notifications */
136c69ff407SJeff Layton 	/* for lease breaks: */
137c69ff407SJeff Layton 	unsigned long fl_break_time;
138c69ff407SJeff Layton 	unsigned long fl_downgrade_time;
139c69ff407SJeff Layton 	const struct lease_manager_operations *fl_lmops; /* Callbacks for lease managers */
140c69ff407SJeff Layton } __randomize_layout;
141c69ff407SJeff Layton 
1425970e15dSJeff Layton struct file_lock_context {
1435970e15dSJeff Layton 	spinlock_t		flc_lock;
1445970e15dSJeff Layton 	struct list_head	flc_flock;
1455970e15dSJeff Layton 	struct list_head	flc_posix;
1465970e15dSJeff Layton 	struct list_head	flc_lease;
1475970e15dSJeff Layton };
1485970e15dSJeff Layton 
1495970e15dSJeff Layton #ifdef CONFIG_FILE_LOCKING
1505970e15dSJeff Layton int fcntl_getlk(struct file *, unsigned int, struct flock *);
1515970e15dSJeff Layton int fcntl_setlk(unsigned int, struct file *, unsigned int,
1525970e15dSJeff Layton 			struct flock *);
1535970e15dSJeff Layton 
1545970e15dSJeff Layton #if BITS_PER_LONG == 32
1555970e15dSJeff Layton int fcntl_getlk64(struct file *, unsigned int, struct flock64 *);
1565970e15dSJeff Layton int fcntl_setlk64(unsigned int, struct file *, unsigned int,
1575970e15dSJeff Layton 			struct flock64 *);
1585970e15dSJeff Layton #endif
1595970e15dSJeff Layton 
160ed5f17f6SLuca Vizzarro int fcntl_setlease(unsigned int fd, struct file *filp, int arg);
1615970e15dSJeff Layton int fcntl_getlease(struct file *filp);
1625970e15dSJeff Layton 
lock_is_unlock(struct file_lock * fl)16375cabec0SJeff Layton static inline bool lock_is_unlock(struct file_lock *fl)
16475cabec0SJeff Layton {
165a69ce85eSJeff Layton 	return fl->c.flc_type == F_UNLCK;
16675cabec0SJeff Layton }
16775cabec0SJeff Layton 
lock_is_read(struct file_lock * fl)16875cabec0SJeff Layton static inline bool lock_is_read(struct file_lock *fl)
16975cabec0SJeff Layton {
170a69ce85eSJeff Layton 	return fl->c.flc_type == F_RDLCK;
17175cabec0SJeff Layton }
17275cabec0SJeff Layton 
lock_is_write(struct file_lock * fl)17375cabec0SJeff Layton static inline bool lock_is_write(struct file_lock *fl)
17475cabec0SJeff Layton {
175a69ce85eSJeff Layton 	return fl->c.flc_type == F_WRLCK;
17675cabec0SJeff Layton }
17775cabec0SJeff Layton 
locks_wake_up(struct file_lock * fl)17875cabec0SJeff Layton static inline void locks_wake_up(struct file_lock *fl)
17975cabec0SJeff Layton {
180a69ce85eSJeff Layton 	wake_up(&fl->c.flc_wait);
18175cabec0SJeff Layton }
18275cabec0SJeff Layton 
locks_can_async_lock(const struct file_operations * fops)183*8cf9a01eSBenjamin Coddington static inline bool locks_can_async_lock(const struct file_operations *fops)
184*8cf9a01eSBenjamin Coddington {
185*8cf9a01eSBenjamin Coddington 	return !fops->lock || fops->fop_flags & FOP_ASYNC_LOCK;
186*8cf9a01eSBenjamin Coddington }
187*8cf9a01eSBenjamin Coddington 
1885970e15dSJeff Layton /* fs/locks.c */
1895970e15dSJeff Layton void locks_free_lock_context(struct inode *inode);
1905970e15dSJeff Layton void locks_free_lock(struct file_lock *fl);
1915970e15dSJeff Layton void locks_init_lock(struct file_lock *);
1925970e15dSJeff Layton struct file_lock *locks_alloc_lock(void);
1935970e15dSJeff Layton void locks_copy_lock(struct file_lock *, struct file_lock *);
1945970e15dSJeff Layton void locks_copy_conflock(struct file_lock *, struct file_lock *);
1955970e15dSJeff Layton void locks_remove_posix(struct file *, fl_owner_t);
1965970e15dSJeff Layton void locks_remove_file(struct file *);
1975970e15dSJeff Layton void locks_release_private(struct file_lock *);
1985970e15dSJeff Layton void posix_test_lock(struct file *, struct file_lock *);
1995970e15dSJeff Layton int posix_lock_file(struct file *, struct file_lock *, struct file_lock *);
2005970e15dSJeff Layton int locks_delete_block(struct file_lock *);
2015970e15dSJeff Layton int vfs_test_lock(struct file *, struct file_lock *);
2025970e15dSJeff Layton int vfs_lock_file(struct file *, unsigned int, struct file_lock *, struct file_lock *);
2035970e15dSJeff Layton int vfs_cancel_lock(struct file *filp, struct file_lock *fl);
2045970e15dSJeff Layton bool vfs_inode_has_locks(struct inode *inode);
2055970e15dSJeff Layton int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl);
206c69ff407SJeff Layton 
207c69ff407SJeff Layton void locks_init_lease(struct file_lease *);
208c69ff407SJeff Layton void locks_free_lease(struct file_lease *fl);
209c69ff407SJeff Layton struct file_lease *locks_alloc_lease(void);
2105970e15dSJeff Layton int __break_lease(struct inode *inode, unsigned int flags, unsigned int type);
2115970e15dSJeff Layton void lease_get_mtime(struct inode *, struct timespec64 *time);
212c69ff407SJeff Layton int generic_setlease(struct file *, int, struct file_lease **, void **priv);
2137b800101SJeff Layton int kernel_setlease(struct file *, int, struct file_lease **, void **);
214c69ff407SJeff Layton int vfs_setlease(struct file *, int, struct file_lease **, void **);
215c69ff407SJeff Layton int lease_modify(struct file_lease *, int, struct list_head *);
2165970e15dSJeff Layton 
2175970e15dSJeff Layton struct notifier_block;
2185970e15dSJeff Layton int lease_register_notifier(struct notifier_block *);
2195970e15dSJeff Layton void lease_unregister_notifier(struct notifier_block *);
2205970e15dSJeff Layton 
2215970e15dSJeff Layton struct files_struct;
2225970e15dSJeff Layton void show_fd_locks(struct seq_file *f,
2235970e15dSJeff Layton 			 struct file *filp, struct files_struct *files);
2245970e15dSJeff Layton bool locks_owner_has_blockers(struct file_lock_context *flctx,
2255970e15dSJeff Layton 			fl_owner_t owner);
2265970e15dSJeff Layton 
2275970e15dSJeff Layton static inline struct file_lock_context *
locks_inode_context(const struct inode * inode)2285970e15dSJeff Layton locks_inode_context(const struct inode *inode)
2295970e15dSJeff Layton {
2305970e15dSJeff Layton 	return smp_load_acquire(&inode->i_flctx);
2315970e15dSJeff Layton }
2325970e15dSJeff Layton 
2335970e15dSJeff Layton #else /* !CONFIG_FILE_LOCKING */
fcntl_getlk(struct file * file,unsigned int cmd,struct flock __user * user)2345970e15dSJeff Layton static inline int fcntl_getlk(struct file *file, unsigned int cmd,
2355970e15dSJeff Layton 			      struct flock __user *user)
2365970e15dSJeff Layton {
2375970e15dSJeff Layton 	return -EINVAL;
2385970e15dSJeff Layton }
2395970e15dSJeff Layton 
fcntl_setlk(unsigned int fd,struct file * file,unsigned int cmd,struct flock __user * user)2405970e15dSJeff Layton static inline int fcntl_setlk(unsigned int fd, struct file *file,
2415970e15dSJeff Layton 			      unsigned int cmd, struct flock __user *user)
2425970e15dSJeff Layton {
2435970e15dSJeff Layton 	return -EACCES;
2445970e15dSJeff Layton }
2455970e15dSJeff Layton 
2465970e15dSJeff Layton #if BITS_PER_LONG == 32
fcntl_getlk64(struct file * file,unsigned int cmd,struct flock64 * user)2475970e15dSJeff Layton static inline int fcntl_getlk64(struct file *file, unsigned int cmd,
2485970e15dSJeff Layton 				struct flock64 *user)
2495970e15dSJeff Layton {
2505970e15dSJeff Layton 	return -EINVAL;
2515970e15dSJeff Layton }
2525970e15dSJeff Layton 
fcntl_setlk64(unsigned int fd,struct file * file,unsigned int cmd,struct flock64 * user)2535970e15dSJeff Layton static inline int fcntl_setlk64(unsigned int fd, struct file *file,
2545970e15dSJeff Layton 				unsigned int cmd, struct flock64 *user)
2555970e15dSJeff Layton {
2565970e15dSJeff Layton 	return -EACCES;
2575970e15dSJeff Layton }
2585970e15dSJeff Layton #endif
fcntl_setlease(unsigned int fd,struct file * filp,int arg)259ed5f17f6SLuca Vizzarro static inline int fcntl_setlease(unsigned int fd, struct file *filp, int arg)
2605970e15dSJeff Layton {
2615970e15dSJeff Layton 	return -EINVAL;
2625970e15dSJeff Layton }
2635970e15dSJeff Layton 
fcntl_getlease(struct file * filp)2645970e15dSJeff Layton static inline int fcntl_getlease(struct file *filp)
2655970e15dSJeff Layton {
2665970e15dSJeff Layton 	return F_UNLCK;
2675970e15dSJeff Layton }
2685970e15dSJeff Layton 
lock_is_unlock(struct file_lock * fl)26975cabec0SJeff Layton static inline bool lock_is_unlock(struct file_lock *fl)
27075cabec0SJeff Layton {
27175cabec0SJeff Layton 	return false;
27275cabec0SJeff Layton }
27375cabec0SJeff Layton 
lock_is_read(struct file_lock * fl)27475cabec0SJeff Layton static inline bool lock_is_read(struct file_lock *fl)
27575cabec0SJeff Layton {
27675cabec0SJeff Layton 	return false;
27775cabec0SJeff Layton }
27875cabec0SJeff Layton 
lock_is_write(struct file_lock * fl)27975cabec0SJeff Layton static inline bool lock_is_write(struct file_lock *fl)
28075cabec0SJeff Layton {
28175cabec0SJeff Layton 	return false;
28275cabec0SJeff Layton }
28375cabec0SJeff Layton 
locks_wake_up(struct file_lock * fl)28475cabec0SJeff Layton static inline void locks_wake_up(struct file_lock *fl)
28575cabec0SJeff Layton {
28675cabec0SJeff Layton }
28775cabec0SJeff Layton 
2885970e15dSJeff Layton static inline void
locks_free_lock_context(struct inode * inode)2895970e15dSJeff Layton locks_free_lock_context(struct inode *inode)
2905970e15dSJeff Layton {
2915970e15dSJeff Layton }
2925970e15dSJeff Layton 
locks_init_lock(struct file_lock * fl)2935970e15dSJeff Layton static inline void locks_init_lock(struct file_lock *fl)
2945970e15dSJeff Layton {
2955970e15dSJeff Layton 	return;
2965970e15dSJeff Layton }
2975970e15dSJeff Layton 
locks_init_lease(struct file_lease * fl)298c69ff407SJeff Layton static inline void locks_init_lease(struct file_lease *fl)
299c69ff407SJeff Layton {
300c69ff407SJeff Layton 	return;
301c69ff407SJeff Layton }
302c69ff407SJeff Layton 
locks_copy_conflock(struct file_lock * new,struct file_lock * fl)3035970e15dSJeff Layton static inline void locks_copy_conflock(struct file_lock *new, struct file_lock *fl)
3045970e15dSJeff Layton {
3055970e15dSJeff Layton 	return;
3065970e15dSJeff Layton }
3075970e15dSJeff Layton 
locks_copy_lock(struct file_lock * new,struct file_lock * fl)3085970e15dSJeff Layton static inline void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
3095970e15dSJeff Layton {
3105970e15dSJeff Layton 	return;
3115970e15dSJeff Layton }
3125970e15dSJeff Layton 
locks_remove_posix(struct file * filp,fl_owner_t owner)3135970e15dSJeff Layton static inline void locks_remove_posix(struct file *filp, fl_owner_t owner)
3145970e15dSJeff Layton {
3155970e15dSJeff Layton 	return;
3165970e15dSJeff Layton }
3175970e15dSJeff Layton 
locks_remove_file(struct file * filp)3185970e15dSJeff Layton static inline void locks_remove_file(struct file *filp)
3195970e15dSJeff Layton {
3205970e15dSJeff Layton 	return;
3215970e15dSJeff Layton }
3225970e15dSJeff Layton 
posix_test_lock(struct file * filp,struct file_lock * fl)3235970e15dSJeff Layton static inline void posix_test_lock(struct file *filp, struct file_lock *fl)
3245970e15dSJeff Layton {
3255970e15dSJeff Layton 	return;
3265970e15dSJeff Layton }
3275970e15dSJeff Layton 
posix_lock_file(struct file * filp,struct file_lock * fl,struct file_lock * conflock)3285970e15dSJeff Layton static inline int posix_lock_file(struct file *filp, struct file_lock *fl,
3295970e15dSJeff Layton 				  struct file_lock *conflock)
3305970e15dSJeff Layton {
3315970e15dSJeff Layton 	return -ENOLCK;
3325970e15dSJeff Layton }
3335970e15dSJeff Layton 
locks_delete_block(struct file_lock * waiter)3345970e15dSJeff Layton static inline int locks_delete_block(struct file_lock *waiter)
3355970e15dSJeff Layton {
3365970e15dSJeff Layton 	return -ENOENT;
3375970e15dSJeff Layton }
3385970e15dSJeff Layton 
vfs_test_lock(struct file * filp,struct file_lock * fl)3395970e15dSJeff Layton static inline int vfs_test_lock(struct file *filp, struct file_lock *fl)
3405970e15dSJeff Layton {
3415970e15dSJeff Layton 	return 0;
3425970e15dSJeff Layton }
3435970e15dSJeff Layton 
vfs_lock_file(struct file * filp,unsigned int cmd,struct file_lock * fl,struct file_lock * conf)3445970e15dSJeff Layton static inline int vfs_lock_file(struct file *filp, unsigned int cmd,
3455970e15dSJeff Layton 				struct file_lock *fl, struct file_lock *conf)
3465970e15dSJeff Layton {
3475970e15dSJeff Layton 	return -ENOLCK;
3485970e15dSJeff Layton }
3495970e15dSJeff Layton 
vfs_cancel_lock(struct file * filp,struct file_lock * fl)3505970e15dSJeff Layton static inline int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
3515970e15dSJeff Layton {
3525970e15dSJeff Layton 	return 0;
3535970e15dSJeff Layton }
3545970e15dSJeff Layton 
vfs_inode_has_locks(struct inode * inode)3555970e15dSJeff Layton static inline bool vfs_inode_has_locks(struct inode *inode)
3565970e15dSJeff Layton {
3575970e15dSJeff Layton 	return false;
3585970e15dSJeff Layton }
3595970e15dSJeff Layton 
locks_lock_inode_wait(struct inode * inode,struct file_lock * fl)3605970e15dSJeff Layton static inline int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl)
3615970e15dSJeff Layton {
3625970e15dSJeff Layton 	return -ENOLCK;
3635970e15dSJeff Layton }
3645970e15dSJeff Layton 
__break_lease(struct inode * inode,unsigned int mode,unsigned int type)3655970e15dSJeff Layton static inline int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
3665970e15dSJeff Layton {
3675970e15dSJeff Layton 	return 0;
3685970e15dSJeff Layton }
3695970e15dSJeff Layton 
lease_get_mtime(struct inode * inode,struct timespec64 * time)3705970e15dSJeff Layton static inline void lease_get_mtime(struct inode *inode,
3715970e15dSJeff Layton 				   struct timespec64 *time)
3725970e15dSJeff Layton {
3735970e15dSJeff Layton 	return;
3745970e15dSJeff Layton }
3755970e15dSJeff Layton 
generic_setlease(struct file * filp,int arg,struct file_lease ** flp,void ** priv)376ed5f17f6SLuca Vizzarro static inline int generic_setlease(struct file *filp, int arg,
377c69ff407SJeff Layton 				    struct file_lease **flp, void **priv)
3785970e15dSJeff Layton {
3795970e15dSJeff Layton 	return -EINVAL;
3805970e15dSJeff Layton }
3815970e15dSJeff Layton 
kernel_setlease(struct file * filp,int arg,struct file_lease ** lease,void ** priv)3827b800101SJeff Layton static inline int kernel_setlease(struct file *filp, int arg,
3837b800101SJeff Layton 			       struct file_lease **lease, void **priv)
3847b800101SJeff Layton {
3857b800101SJeff Layton 	return -EINVAL;
3867b800101SJeff Layton }
3877b800101SJeff Layton 
vfs_setlease(struct file * filp,int arg,struct file_lease ** lease,void ** priv)388ed5f17f6SLuca Vizzarro static inline int vfs_setlease(struct file *filp, int arg,
389c69ff407SJeff Layton 			       struct file_lease **lease, void **priv)
3905970e15dSJeff Layton {
3915970e15dSJeff Layton 	return -EINVAL;
3925970e15dSJeff Layton }
3935970e15dSJeff Layton 
lease_modify(struct file_lease * fl,int arg,struct list_head * dispose)394c69ff407SJeff Layton static inline int lease_modify(struct file_lease *fl, int arg,
3955970e15dSJeff Layton 			       struct list_head *dispose)
3965970e15dSJeff Layton {
3975970e15dSJeff Layton 	return -EINVAL;
3985970e15dSJeff Layton }
3995970e15dSJeff Layton 
4005970e15dSJeff Layton struct files_struct;
show_fd_locks(struct seq_file * f,struct file * filp,struct files_struct * files)4015970e15dSJeff Layton static inline void show_fd_locks(struct seq_file *f,
4025970e15dSJeff Layton 			struct file *filp, struct files_struct *files) {}
locks_owner_has_blockers(struct file_lock_context * flctx,fl_owner_t owner)4035970e15dSJeff Layton static inline bool locks_owner_has_blockers(struct file_lock_context *flctx,
4045970e15dSJeff Layton 			fl_owner_t owner)
4055970e15dSJeff Layton {
4065970e15dSJeff Layton 	return false;
4075970e15dSJeff Layton }
4085970e15dSJeff Layton 
4095970e15dSJeff Layton static inline struct file_lock_context *
locks_inode_context(const struct inode * inode)4105970e15dSJeff Layton locks_inode_context(const struct inode *inode)
4115970e15dSJeff Layton {
4125970e15dSJeff Layton 	return NULL;
4135970e15dSJeff Layton }
4145970e15dSJeff Layton 
4155970e15dSJeff Layton #endif /* !CONFIG_FILE_LOCKING */
4165970e15dSJeff Layton 
417582a3bf9SJeff Layton /* for walking lists of file_locks linked by fl_list */
418582a3bf9SJeff Layton #define for_each_file_lock(_fl, _head)	list_for_each_entry(_fl, _head, c.flc_list)
419582a3bf9SJeff Layton 
locks_lock_file_wait(struct file * filp,struct file_lock * fl)4205970e15dSJeff Layton static inline int locks_lock_file_wait(struct file *filp, struct file_lock *fl)
4215970e15dSJeff Layton {
422c65454a9SJeff Layton 	return locks_lock_inode_wait(file_inode(filp), fl);
4235970e15dSJeff Layton }
4245970e15dSJeff Layton 
4255970e15dSJeff Layton #ifdef CONFIG_FILE_LOCKING
break_lease(struct inode * inode,unsigned int mode)4265970e15dSJeff Layton static inline int break_lease(struct inode *inode, unsigned int mode)
4275970e15dSJeff Layton {
4285970e15dSJeff Layton 	struct file_lock_context *flctx;
4295970e15dSJeff Layton 
4305970e15dSJeff Layton 	/*
4315970e15dSJeff Layton 	 * Since this check is lockless, we must ensure that any refcounts
4325970e15dSJeff Layton 	 * taken are done before checking i_flctx->flc_lease. Otherwise, we
4335970e15dSJeff Layton 	 * could end up racing with tasks trying to set a new lease on this
4345970e15dSJeff Layton 	 * file.
4355970e15dSJeff Layton 	 */
4365970e15dSJeff Layton 	flctx = READ_ONCE(inode->i_flctx);
4375970e15dSJeff Layton 	if (!flctx)
4385970e15dSJeff Layton 		return 0;
4395970e15dSJeff Layton 	smp_mb();
4405970e15dSJeff Layton 	if (!list_empty_careful(&flctx->flc_lease))
4415970e15dSJeff Layton 		return __break_lease(inode, mode, FL_LEASE);
4425970e15dSJeff Layton 	return 0;
4435970e15dSJeff Layton }
4445970e15dSJeff Layton 
break_deleg(struct inode * inode,unsigned int mode)4455970e15dSJeff Layton static inline int break_deleg(struct inode *inode, unsigned int mode)
4465970e15dSJeff Layton {
4475970e15dSJeff Layton 	struct file_lock_context *flctx;
4485970e15dSJeff Layton 
4495970e15dSJeff Layton 	/*
4505970e15dSJeff Layton 	 * Since this check is lockless, we must ensure that any refcounts
4515970e15dSJeff Layton 	 * taken are done before checking i_flctx->flc_lease. Otherwise, we
4525970e15dSJeff Layton 	 * could end up racing with tasks trying to set a new lease on this
4535970e15dSJeff Layton 	 * file.
4545970e15dSJeff Layton 	 */
4555970e15dSJeff Layton 	flctx = READ_ONCE(inode->i_flctx);
4565970e15dSJeff Layton 	if (!flctx)
4575970e15dSJeff Layton 		return 0;
4585970e15dSJeff Layton 	smp_mb();
4595970e15dSJeff Layton 	if (!list_empty_careful(&flctx->flc_lease))
4605970e15dSJeff Layton 		return __break_lease(inode, mode, FL_DELEG);
4615970e15dSJeff Layton 	return 0;
4625970e15dSJeff Layton }
4635970e15dSJeff Layton 
try_break_deleg(struct inode * inode,struct inode ** delegated_inode)4645970e15dSJeff Layton static inline int try_break_deleg(struct inode *inode, struct inode **delegated_inode)
4655970e15dSJeff Layton {
4665970e15dSJeff Layton 	int ret;
4675970e15dSJeff Layton 
4685970e15dSJeff Layton 	ret = break_deleg(inode, O_WRONLY|O_NONBLOCK);
4695970e15dSJeff Layton 	if (ret == -EWOULDBLOCK && delegated_inode) {
4705970e15dSJeff Layton 		*delegated_inode = inode;
4715970e15dSJeff Layton 		ihold(inode);
4725970e15dSJeff Layton 	}
4735970e15dSJeff Layton 	return ret;
4745970e15dSJeff Layton }
4755970e15dSJeff Layton 
break_deleg_wait(struct inode ** delegated_inode)4765970e15dSJeff Layton static inline int break_deleg_wait(struct inode **delegated_inode)
4775970e15dSJeff Layton {
4785970e15dSJeff Layton 	int ret;
4795970e15dSJeff Layton 
4805970e15dSJeff Layton 	ret = break_deleg(*delegated_inode, O_WRONLY);
4815970e15dSJeff Layton 	iput(*delegated_inode);
4825970e15dSJeff Layton 	*delegated_inode = NULL;
4835970e15dSJeff Layton 	return ret;
4845970e15dSJeff Layton }
4855970e15dSJeff Layton 
break_layout(struct inode * inode,bool wait)4865970e15dSJeff Layton static inline int break_layout(struct inode *inode, bool wait)
4875970e15dSJeff Layton {
4885970e15dSJeff Layton 	smp_mb();
4895970e15dSJeff Layton 	if (inode->i_flctx && !list_empty_careful(&inode->i_flctx->flc_lease))
4905970e15dSJeff Layton 		return __break_lease(inode,
4915970e15dSJeff Layton 				wait ? O_WRONLY : O_WRONLY | O_NONBLOCK,
4925970e15dSJeff Layton 				FL_LAYOUT);
4935970e15dSJeff Layton 	return 0;
4945970e15dSJeff Layton }
4955970e15dSJeff Layton 
4965970e15dSJeff Layton #else /* !CONFIG_FILE_LOCKING */
break_lease(struct inode * inode,unsigned int mode)4975970e15dSJeff Layton static inline int break_lease(struct inode *inode, unsigned int mode)
4985970e15dSJeff Layton {
4995970e15dSJeff Layton 	return 0;
5005970e15dSJeff Layton }
5015970e15dSJeff Layton 
break_deleg(struct inode * inode,unsigned int mode)5025970e15dSJeff Layton static inline int break_deleg(struct inode *inode, unsigned int mode)
5035970e15dSJeff Layton {
5045970e15dSJeff Layton 	return 0;
5055970e15dSJeff Layton }
5065970e15dSJeff Layton 
try_break_deleg(struct inode * inode,struct inode ** delegated_inode)5075970e15dSJeff Layton static inline int try_break_deleg(struct inode *inode, struct inode **delegated_inode)
5085970e15dSJeff Layton {
5095970e15dSJeff Layton 	return 0;
5105970e15dSJeff Layton }
5115970e15dSJeff Layton 
break_deleg_wait(struct inode ** delegated_inode)5125970e15dSJeff Layton static inline int break_deleg_wait(struct inode **delegated_inode)
5135970e15dSJeff Layton {
5145970e15dSJeff Layton 	BUG();
5155970e15dSJeff Layton 	return 0;
516 }
517 
break_layout(struct inode * inode,bool wait)518 static inline int break_layout(struct inode *inode, bool wait)
519 {
520 	return 0;
521 }
522 
523 #endif /* CONFIG_FILE_LOCKING */
524 
525 #endif /* _LINUX_FILELOCK_H */
526