xref: /openbmc/linux/include/linux/filelock.h (revision ed5f17f6)
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;
305970e15dSJeff Layton 
315970e15dSJeff Layton struct file_lock_operations {
325970e15dSJeff Layton 	void (*fl_copy_lock)(struct file_lock *, struct file_lock *);
335970e15dSJeff Layton 	void (*fl_release_private)(struct file_lock *);
345970e15dSJeff Layton };
355970e15dSJeff Layton 
365970e15dSJeff Layton struct lock_manager_operations {
375970e15dSJeff Layton 	void *lm_mod_owner;
385970e15dSJeff Layton 	fl_owner_t (*lm_get_owner)(fl_owner_t);
395970e15dSJeff Layton 	void (*lm_put_owner)(fl_owner_t);
405970e15dSJeff Layton 	void (*lm_notify)(struct file_lock *);	/* unblock callback */
415970e15dSJeff Layton 	int (*lm_grant)(struct file_lock *, int);
425970e15dSJeff Layton 	bool (*lm_break)(struct file_lock *);
435970e15dSJeff Layton 	int (*lm_change)(struct file_lock *, int, struct list_head *);
445970e15dSJeff Layton 	void (*lm_setup)(struct file_lock *, void **);
455970e15dSJeff Layton 	bool (*lm_breaker_owns_lease)(struct file_lock *);
465970e15dSJeff Layton 	bool (*lm_lock_expirable)(struct file_lock *cfl);
475970e15dSJeff Layton 	void (*lm_expire_lock)(void);
485970e15dSJeff Layton };
495970e15dSJeff Layton 
505970e15dSJeff Layton struct lock_manager {
515970e15dSJeff Layton 	struct list_head list;
525970e15dSJeff Layton 	/*
535970e15dSJeff Layton 	 * NFSv4 and up also want opens blocked during the grace period;
545970e15dSJeff Layton 	 * NLM doesn't care:
555970e15dSJeff Layton 	 */
565970e15dSJeff Layton 	bool block_opens;
575970e15dSJeff Layton };
585970e15dSJeff Layton 
595970e15dSJeff Layton struct net;
605970e15dSJeff Layton void locks_start_grace(struct net *, struct lock_manager *);
615970e15dSJeff Layton void locks_end_grace(struct lock_manager *);
625970e15dSJeff Layton bool locks_in_grace(struct net *);
635970e15dSJeff Layton bool opens_in_grace(struct net *);
645970e15dSJeff Layton 
655970e15dSJeff Layton /*
665970e15dSJeff Layton  * struct file_lock has a union that some filesystems use to track
675970e15dSJeff Layton  * their own private info. The NFS side of things is defined here:
685970e15dSJeff Layton  */
695970e15dSJeff Layton #include <linux/nfs_fs_i.h>
705970e15dSJeff Layton 
715970e15dSJeff Layton /*
725970e15dSJeff Layton  * struct file_lock represents a generic "file lock". It's used to represent
735970e15dSJeff Layton  * POSIX byte range locks, BSD (flock) locks, and leases. It's important to
745970e15dSJeff Layton  * note that the same struct is used to represent both a request for a lock and
755970e15dSJeff Layton  * the lock itself, but the same object is never used for both.
765970e15dSJeff Layton  *
775970e15dSJeff Layton  * FIXME: should we create a separate "struct lock_request" to help distinguish
785970e15dSJeff Layton  * these two uses?
795970e15dSJeff Layton  *
805970e15dSJeff Layton  * The varous i_flctx lists are ordered by:
815970e15dSJeff Layton  *
825970e15dSJeff Layton  * 1) lock owner
835970e15dSJeff Layton  * 2) lock range start
845970e15dSJeff Layton  * 3) lock range end
855970e15dSJeff Layton  *
865970e15dSJeff Layton  * Obviously, the last two criteria only matter for POSIX locks.
875970e15dSJeff Layton  */
885970e15dSJeff Layton struct file_lock {
895970e15dSJeff Layton 	struct file_lock *fl_blocker;	/* The lock, that is blocking us */
905970e15dSJeff Layton 	struct list_head fl_list;	/* link into file_lock_context */
915970e15dSJeff Layton 	struct hlist_node fl_link;	/* node in global lists */
925970e15dSJeff Layton 	struct list_head fl_blocked_requests;	/* list of requests with
935970e15dSJeff Layton 						 * ->fl_blocker pointing here
945970e15dSJeff Layton 						 */
955970e15dSJeff Layton 	struct list_head fl_blocked_member;	/* node in
965970e15dSJeff Layton 						 * ->fl_blocker->fl_blocked_requests
975970e15dSJeff Layton 						 */
985970e15dSJeff Layton 	fl_owner_t fl_owner;
995970e15dSJeff Layton 	unsigned int fl_flags;
1005970e15dSJeff Layton 	unsigned char fl_type;
1015970e15dSJeff Layton 	unsigned int fl_pid;
1025970e15dSJeff Layton 	int fl_link_cpu;		/* what cpu's list is this on? */
1035970e15dSJeff Layton 	wait_queue_head_t fl_wait;
1045970e15dSJeff Layton 	struct file *fl_file;
1055970e15dSJeff Layton 	loff_t fl_start;
1065970e15dSJeff Layton 	loff_t fl_end;
1075970e15dSJeff Layton 
1085970e15dSJeff Layton 	struct fasync_struct *	fl_fasync; /* for lease break notifications */
1095970e15dSJeff Layton 	/* for lease breaks: */
1105970e15dSJeff Layton 	unsigned long fl_break_time;
1115970e15dSJeff Layton 	unsigned long fl_downgrade_time;
1125970e15dSJeff Layton 
1135970e15dSJeff Layton 	const struct file_lock_operations *fl_ops;	/* Callbacks for filesystems */
1145970e15dSJeff Layton 	const struct lock_manager_operations *fl_lmops;	/* Callbacks for lockmanagers */
1155970e15dSJeff Layton 	union {
1165970e15dSJeff Layton 		struct nfs_lock_info	nfs_fl;
1175970e15dSJeff Layton 		struct nfs4_lock_info	nfs4_fl;
1185970e15dSJeff Layton 		struct {
1195970e15dSJeff Layton 			struct list_head link;	/* link in AFS vnode's pending_locks list */
1205970e15dSJeff Layton 			int state;		/* state of grant or error if -ve */
1215970e15dSJeff Layton 			unsigned int	debug_id;
1225970e15dSJeff Layton 		} afs;
1235970e15dSJeff Layton 		struct {
1245970e15dSJeff Layton 			struct inode *inode;
1255970e15dSJeff Layton 		} ceph;
1265970e15dSJeff Layton 	} fl_u;
1275970e15dSJeff Layton } __randomize_layout;
1285970e15dSJeff Layton 
1295970e15dSJeff Layton struct file_lock_context {
1305970e15dSJeff Layton 	spinlock_t		flc_lock;
1315970e15dSJeff Layton 	struct list_head	flc_flock;
1325970e15dSJeff Layton 	struct list_head	flc_posix;
1335970e15dSJeff Layton 	struct list_head	flc_lease;
1345970e15dSJeff Layton };
1355970e15dSJeff Layton 
1365970e15dSJeff Layton #ifdef CONFIG_FILE_LOCKING
1375970e15dSJeff Layton int fcntl_getlk(struct file *, unsigned int, struct flock *);
1385970e15dSJeff Layton int fcntl_setlk(unsigned int, struct file *, unsigned int,
1395970e15dSJeff Layton 			struct flock *);
1405970e15dSJeff Layton 
1415970e15dSJeff Layton #if BITS_PER_LONG == 32
1425970e15dSJeff Layton int fcntl_getlk64(struct file *, unsigned int, struct flock64 *);
1435970e15dSJeff Layton int fcntl_setlk64(unsigned int, struct file *, unsigned int,
1445970e15dSJeff Layton 			struct flock64 *);
1455970e15dSJeff Layton #endif
1465970e15dSJeff Layton 
147*ed5f17f6SLuca Vizzarro int fcntl_setlease(unsigned int fd, struct file *filp, int arg);
1485970e15dSJeff Layton int fcntl_getlease(struct file *filp);
1495970e15dSJeff Layton 
1505970e15dSJeff Layton /* fs/locks.c */
1515970e15dSJeff Layton void locks_free_lock_context(struct inode *inode);
1525970e15dSJeff Layton void locks_free_lock(struct file_lock *fl);
1535970e15dSJeff Layton void locks_init_lock(struct file_lock *);
1545970e15dSJeff Layton struct file_lock * locks_alloc_lock(void);
1555970e15dSJeff Layton void locks_copy_lock(struct file_lock *, struct file_lock *);
1565970e15dSJeff Layton void locks_copy_conflock(struct file_lock *, struct file_lock *);
1575970e15dSJeff Layton void locks_remove_posix(struct file *, fl_owner_t);
1585970e15dSJeff Layton void locks_remove_file(struct file *);
1595970e15dSJeff Layton void locks_release_private(struct file_lock *);
1605970e15dSJeff Layton void posix_test_lock(struct file *, struct file_lock *);
1615970e15dSJeff Layton int posix_lock_file(struct file *, struct file_lock *, struct file_lock *);
1625970e15dSJeff Layton int locks_delete_block(struct file_lock *);
1635970e15dSJeff Layton int vfs_test_lock(struct file *, struct file_lock *);
1645970e15dSJeff Layton int vfs_lock_file(struct file *, unsigned int, struct file_lock *, struct file_lock *);
1655970e15dSJeff Layton int vfs_cancel_lock(struct file *filp, struct file_lock *fl);
1665970e15dSJeff Layton bool vfs_inode_has_locks(struct inode *inode);
1675970e15dSJeff Layton int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl);
1685970e15dSJeff Layton int __break_lease(struct inode *inode, unsigned int flags, unsigned int type);
1695970e15dSJeff Layton void lease_get_mtime(struct inode *, struct timespec64 *time);
170*ed5f17f6SLuca Vizzarro int generic_setlease(struct file *, int, struct file_lock **, void **priv);
171*ed5f17f6SLuca Vizzarro int vfs_setlease(struct file *, int, struct file_lock **, void **);
1725970e15dSJeff Layton int lease_modify(struct file_lock *, int, struct list_head *);
1735970e15dSJeff Layton 
1745970e15dSJeff Layton struct notifier_block;
1755970e15dSJeff Layton int lease_register_notifier(struct notifier_block *);
1765970e15dSJeff Layton void lease_unregister_notifier(struct notifier_block *);
1775970e15dSJeff Layton 
1785970e15dSJeff Layton struct files_struct;
1795970e15dSJeff Layton void show_fd_locks(struct seq_file *f,
1805970e15dSJeff Layton 			 struct file *filp, struct files_struct *files);
1815970e15dSJeff Layton bool locks_owner_has_blockers(struct file_lock_context *flctx,
1825970e15dSJeff Layton 			fl_owner_t owner);
1835970e15dSJeff Layton 
1845970e15dSJeff Layton static inline struct file_lock_context *
locks_inode_context(const struct inode * inode)1855970e15dSJeff Layton locks_inode_context(const struct inode *inode)
1865970e15dSJeff Layton {
1875970e15dSJeff Layton 	return smp_load_acquire(&inode->i_flctx);
1885970e15dSJeff Layton }
1895970e15dSJeff Layton 
1905970e15dSJeff Layton #else /* !CONFIG_FILE_LOCKING */
fcntl_getlk(struct file * file,unsigned int cmd,struct flock __user * user)1915970e15dSJeff Layton static inline int fcntl_getlk(struct file *file, unsigned int cmd,
1925970e15dSJeff Layton 			      struct flock __user *user)
1935970e15dSJeff Layton {
1945970e15dSJeff Layton 	return -EINVAL;
1955970e15dSJeff Layton }
1965970e15dSJeff Layton 
fcntl_setlk(unsigned int fd,struct file * file,unsigned int cmd,struct flock __user * user)1975970e15dSJeff Layton static inline int fcntl_setlk(unsigned int fd, struct file *file,
1985970e15dSJeff Layton 			      unsigned int cmd, struct flock __user *user)
1995970e15dSJeff Layton {
2005970e15dSJeff Layton 	return -EACCES;
2015970e15dSJeff Layton }
2025970e15dSJeff Layton 
2035970e15dSJeff Layton #if BITS_PER_LONG == 32
fcntl_getlk64(struct file * file,unsigned int cmd,struct flock64 * user)2045970e15dSJeff Layton static inline int fcntl_getlk64(struct file *file, unsigned int cmd,
2055970e15dSJeff Layton 				struct flock64 *user)
2065970e15dSJeff Layton {
2075970e15dSJeff Layton 	return -EINVAL;
2085970e15dSJeff Layton }
2095970e15dSJeff Layton 
fcntl_setlk64(unsigned int fd,struct file * file,unsigned int cmd,struct flock64 * user)2105970e15dSJeff Layton static inline int fcntl_setlk64(unsigned int fd, struct file *file,
2115970e15dSJeff Layton 				unsigned int cmd, struct flock64 *user)
2125970e15dSJeff Layton {
2135970e15dSJeff Layton 	return -EACCES;
2145970e15dSJeff Layton }
2155970e15dSJeff Layton #endif
fcntl_setlease(unsigned int fd,struct file * filp,int arg)216*ed5f17f6SLuca Vizzarro static inline int fcntl_setlease(unsigned int fd, struct file *filp, int arg)
2175970e15dSJeff Layton {
2185970e15dSJeff Layton 	return -EINVAL;
2195970e15dSJeff Layton }
2205970e15dSJeff Layton 
fcntl_getlease(struct file * filp)2215970e15dSJeff Layton static inline int fcntl_getlease(struct file *filp)
2225970e15dSJeff Layton {
2235970e15dSJeff Layton 	return F_UNLCK;
2245970e15dSJeff Layton }
2255970e15dSJeff Layton 
2265970e15dSJeff Layton static inline void
locks_free_lock_context(struct inode * inode)2275970e15dSJeff Layton locks_free_lock_context(struct inode *inode)
2285970e15dSJeff Layton {
2295970e15dSJeff Layton }
2305970e15dSJeff Layton 
locks_init_lock(struct file_lock * fl)2315970e15dSJeff Layton static inline void locks_init_lock(struct file_lock *fl)
2325970e15dSJeff Layton {
2335970e15dSJeff Layton 	return;
2345970e15dSJeff Layton }
2355970e15dSJeff Layton 
locks_copy_conflock(struct file_lock * new,struct file_lock * fl)2365970e15dSJeff Layton static inline void locks_copy_conflock(struct file_lock *new, struct file_lock *fl)
2375970e15dSJeff Layton {
2385970e15dSJeff Layton 	return;
2395970e15dSJeff Layton }
2405970e15dSJeff Layton 
locks_copy_lock(struct file_lock * new,struct file_lock * fl)2415970e15dSJeff Layton static inline void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
2425970e15dSJeff Layton {
2435970e15dSJeff Layton 	return;
2445970e15dSJeff Layton }
2455970e15dSJeff Layton 
locks_remove_posix(struct file * filp,fl_owner_t owner)2465970e15dSJeff Layton static inline void locks_remove_posix(struct file *filp, fl_owner_t owner)
2475970e15dSJeff Layton {
2485970e15dSJeff Layton 	return;
2495970e15dSJeff Layton }
2505970e15dSJeff Layton 
locks_remove_file(struct file * filp)2515970e15dSJeff Layton static inline void locks_remove_file(struct file *filp)
2525970e15dSJeff Layton {
2535970e15dSJeff Layton 	return;
2545970e15dSJeff Layton }
2555970e15dSJeff Layton 
posix_test_lock(struct file * filp,struct file_lock * fl)2565970e15dSJeff Layton static inline void posix_test_lock(struct file *filp, struct file_lock *fl)
2575970e15dSJeff Layton {
2585970e15dSJeff Layton 	return;
2595970e15dSJeff Layton }
2605970e15dSJeff Layton 
posix_lock_file(struct file * filp,struct file_lock * fl,struct file_lock * conflock)2615970e15dSJeff Layton static inline int posix_lock_file(struct file *filp, struct file_lock *fl,
2625970e15dSJeff Layton 				  struct file_lock *conflock)
2635970e15dSJeff Layton {
2645970e15dSJeff Layton 	return -ENOLCK;
2655970e15dSJeff Layton }
2665970e15dSJeff Layton 
locks_delete_block(struct file_lock * waiter)2675970e15dSJeff Layton static inline int locks_delete_block(struct file_lock *waiter)
2685970e15dSJeff Layton {
2695970e15dSJeff Layton 	return -ENOENT;
2705970e15dSJeff Layton }
2715970e15dSJeff Layton 
vfs_test_lock(struct file * filp,struct file_lock * fl)2725970e15dSJeff Layton static inline int vfs_test_lock(struct file *filp, struct file_lock *fl)
2735970e15dSJeff Layton {
2745970e15dSJeff Layton 	return 0;
2755970e15dSJeff Layton }
2765970e15dSJeff Layton 
vfs_lock_file(struct file * filp,unsigned int cmd,struct file_lock * fl,struct file_lock * conf)2775970e15dSJeff Layton static inline int vfs_lock_file(struct file *filp, unsigned int cmd,
2785970e15dSJeff Layton 				struct file_lock *fl, struct file_lock *conf)
2795970e15dSJeff Layton {
2805970e15dSJeff Layton 	return -ENOLCK;
2815970e15dSJeff Layton }
2825970e15dSJeff Layton 
vfs_cancel_lock(struct file * filp,struct file_lock * fl)2835970e15dSJeff Layton static inline int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
2845970e15dSJeff Layton {
2855970e15dSJeff Layton 	return 0;
2865970e15dSJeff Layton }
2875970e15dSJeff Layton 
vfs_inode_has_locks(struct inode * inode)2885970e15dSJeff Layton static inline bool vfs_inode_has_locks(struct inode *inode)
2895970e15dSJeff Layton {
2905970e15dSJeff Layton 	return false;
2915970e15dSJeff Layton }
2925970e15dSJeff Layton 
locks_lock_inode_wait(struct inode * inode,struct file_lock * fl)2935970e15dSJeff Layton static inline int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl)
2945970e15dSJeff Layton {
2955970e15dSJeff Layton 	return -ENOLCK;
2965970e15dSJeff Layton }
2975970e15dSJeff Layton 
__break_lease(struct inode * inode,unsigned int mode,unsigned int type)2985970e15dSJeff Layton static inline int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
2995970e15dSJeff Layton {
3005970e15dSJeff Layton 	return 0;
3015970e15dSJeff Layton }
3025970e15dSJeff Layton 
lease_get_mtime(struct inode * inode,struct timespec64 * time)3035970e15dSJeff Layton static inline void lease_get_mtime(struct inode *inode,
3045970e15dSJeff Layton 				   struct timespec64 *time)
3055970e15dSJeff Layton {
3065970e15dSJeff Layton 	return;
3075970e15dSJeff Layton }
3085970e15dSJeff Layton 
generic_setlease(struct file * filp,int arg,struct file_lock ** flp,void ** priv)309*ed5f17f6SLuca Vizzarro static inline int generic_setlease(struct file *filp, int arg,
3105970e15dSJeff Layton 				    struct file_lock **flp, void **priv)
3115970e15dSJeff Layton {
3125970e15dSJeff Layton 	return -EINVAL;
3135970e15dSJeff Layton }
3145970e15dSJeff Layton 
vfs_setlease(struct file * filp,int arg,struct file_lock ** lease,void ** priv)315*ed5f17f6SLuca Vizzarro static inline int vfs_setlease(struct file *filp, int arg,
3165970e15dSJeff Layton 			       struct file_lock **lease, void **priv)
3175970e15dSJeff Layton {
3185970e15dSJeff Layton 	return -EINVAL;
3195970e15dSJeff Layton }
3205970e15dSJeff Layton 
lease_modify(struct file_lock * fl,int arg,struct list_head * dispose)3215970e15dSJeff Layton static inline int lease_modify(struct file_lock *fl, int arg,
3225970e15dSJeff Layton 			       struct list_head *dispose)
3235970e15dSJeff Layton {
3245970e15dSJeff Layton 	return -EINVAL;
3255970e15dSJeff Layton }
3265970e15dSJeff Layton 
3275970e15dSJeff Layton struct files_struct;
show_fd_locks(struct seq_file * f,struct file * filp,struct files_struct * files)3285970e15dSJeff Layton static inline void show_fd_locks(struct seq_file *f,
3295970e15dSJeff Layton 			struct file *filp, struct files_struct *files) {}
locks_owner_has_blockers(struct file_lock_context * flctx,fl_owner_t owner)3305970e15dSJeff Layton static inline bool locks_owner_has_blockers(struct file_lock_context *flctx,
3315970e15dSJeff Layton 			fl_owner_t owner)
3325970e15dSJeff Layton {
3335970e15dSJeff Layton 	return false;
3345970e15dSJeff Layton }
3355970e15dSJeff Layton 
3365970e15dSJeff Layton static inline struct file_lock_context *
locks_inode_context(const struct inode * inode)3375970e15dSJeff Layton locks_inode_context(const struct inode *inode)
3385970e15dSJeff Layton {
3395970e15dSJeff Layton 	return NULL;
3405970e15dSJeff Layton }
3415970e15dSJeff Layton 
3425970e15dSJeff Layton #endif /* !CONFIG_FILE_LOCKING */
3435970e15dSJeff Layton 
locks_lock_file_wait(struct file * filp,struct file_lock * fl)3445970e15dSJeff Layton static inline int locks_lock_file_wait(struct file *filp, struct file_lock *fl)
3455970e15dSJeff Layton {
346c65454a9SJeff Layton 	return locks_lock_inode_wait(file_inode(filp), fl);
3475970e15dSJeff Layton }
3485970e15dSJeff Layton 
3495970e15dSJeff Layton #ifdef CONFIG_FILE_LOCKING
break_lease(struct inode * inode,unsigned int mode)3505970e15dSJeff Layton static inline int break_lease(struct inode *inode, unsigned int mode)
3515970e15dSJeff Layton {
3525970e15dSJeff Layton 	/*
3535970e15dSJeff Layton 	 * Since this check is lockless, we must ensure that any refcounts
3545970e15dSJeff Layton 	 * taken are done before checking i_flctx->flc_lease. Otherwise, we
3555970e15dSJeff Layton 	 * could end up racing with tasks trying to set a new lease on this
3565970e15dSJeff Layton 	 * file.
3575970e15dSJeff Layton 	 */
3585970e15dSJeff Layton 	smp_mb();
3595970e15dSJeff Layton 	if (inode->i_flctx && !list_empty_careful(&inode->i_flctx->flc_lease))
3605970e15dSJeff Layton 		return __break_lease(inode, mode, FL_LEASE);
3615970e15dSJeff Layton 	return 0;
3625970e15dSJeff Layton }
3635970e15dSJeff Layton 
break_deleg(struct inode * inode,unsigned int mode)3645970e15dSJeff Layton static inline int break_deleg(struct inode *inode, unsigned int mode)
3655970e15dSJeff Layton {
3665970e15dSJeff Layton 	/*
3675970e15dSJeff Layton 	 * Since this check is lockless, we must ensure that any refcounts
3685970e15dSJeff Layton 	 * taken are done before checking i_flctx->flc_lease. Otherwise, we
3695970e15dSJeff Layton 	 * could end up racing with tasks trying to set a new lease on this
3705970e15dSJeff Layton 	 * file.
3715970e15dSJeff Layton 	 */
3725970e15dSJeff Layton 	smp_mb();
3735970e15dSJeff Layton 	if (inode->i_flctx && !list_empty_careful(&inode->i_flctx->flc_lease))
3745970e15dSJeff Layton 		return __break_lease(inode, mode, FL_DELEG);
3755970e15dSJeff Layton 	return 0;
3765970e15dSJeff Layton }
3775970e15dSJeff Layton 
try_break_deleg(struct inode * inode,struct inode ** delegated_inode)3785970e15dSJeff Layton static inline int try_break_deleg(struct inode *inode, struct inode **delegated_inode)
3795970e15dSJeff Layton {
3805970e15dSJeff Layton 	int ret;
3815970e15dSJeff Layton 
3825970e15dSJeff Layton 	ret = break_deleg(inode, O_WRONLY|O_NONBLOCK);
3835970e15dSJeff Layton 	if (ret == -EWOULDBLOCK && delegated_inode) {
3845970e15dSJeff Layton 		*delegated_inode = inode;
3855970e15dSJeff Layton 		ihold(inode);
3865970e15dSJeff Layton 	}
3875970e15dSJeff Layton 	return ret;
3885970e15dSJeff Layton }
3895970e15dSJeff Layton 
break_deleg_wait(struct inode ** delegated_inode)3905970e15dSJeff Layton static inline int break_deleg_wait(struct inode **delegated_inode)
3915970e15dSJeff Layton {
3925970e15dSJeff Layton 	int ret;
3935970e15dSJeff Layton 
3945970e15dSJeff Layton 	ret = break_deleg(*delegated_inode, O_WRONLY);
3955970e15dSJeff Layton 	iput(*delegated_inode);
3965970e15dSJeff Layton 	*delegated_inode = NULL;
3975970e15dSJeff Layton 	return ret;
3985970e15dSJeff Layton }
3995970e15dSJeff Layton 
break_layout(struct inode * inode,bool wait)4005970e15dSJeff Layton static inline int break_layout(struct inode *inode, bool wait)
4015970e15dSJeff Layton {
4025970e15dSJeff Layton 	smp_mb();
4035970e15dSJeff Layton 	if (inode->i_flctx && !list_empty_careful(&inode->i_flctx->flc_lease))
4045970e15dSJeff Layton 		return __break_lease(inode,
4055970e15dSJeff Layton 				wait ? O_WRONLY : O_WRONLY | O_NONBLOCK,
4065970e15dSJeff Layton 				FL_LAYOUT);
4075970e15dSJeff Layton 	return 0;
4085970e15dSJeff Layton }
4095970e15dSJeff Layton 
4105970e15dSJeff Layton #else /* !CONFIG_FILE_LOCKING */
break_lease(struct inode * inode,unsigned int mode)4115970e15dSJeff Layton static inline int break_lease(struct inode *inode, unsigned int mode)
4125970e15dSJeff Layton {
4135970e15dSJeff Layton 	return 0;
4145970e15dSJeff Layton }
4155970e15dSJeff Layton 
break_deleg(struct inode * inode,unsigned int mode)4165970e15dSJeff Layton static inline int break_deleg(struct inode *inode, unsigned int mode)
4175970e15dSJeff Layton {
4185970e15dSJeff Layton 	return 0;
4195970e15dSJeff Layton }
4205970e15dSJeff Layton 
try_break_deleg(struct inode * inode,struct inode ** delegated_inode)4215970e15dSJeff Layton static inline int try_break_deleg(struct inode *inode, struct inode **delegated_inode)
4225970e15dSJeff Layton {
4235970e15dSJeff Layton 	return 0;
4245970e15dSJeff Layton }
4255970e15dSJeff Layton 
break_deleg_wait(struct inode ** delegated_inode)4265970e15dSJeff Layton static inline int break_deleg_wait(struct inode **delegated_inode)
4275970e15dSJeff Layton {
4285970e15dSJeff Layton 	BUG();
4295970e15dSJeff Layton 	return 0;
4305970e15dSJeff Layton }
4315970e15dSJeff Layton 
break_layout(struct inode * inode,bool wait)4325970e15dSJeff Layton static inline int break_layout(struct inode *inode, bool wait)
4335970e15dSJeff Layton {
4345970e15dSJeff Layton 	return 0;
4355970e15dSJeff Layton }
4365970e15dSJeff Layton 
4375970e15dSJeff Layton #endif /* CONFIG_FILE_LOCKING */
4385970e15dSJeff Layton 
4395970e15dSJeff Layton #endif /* _LINUX_FILELOCK_H */
440