1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_FILELOCK_H
3 #define _LINUX_FILELOCK_H
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 */
21 #define FL_CLOSE_POSIX (FL_POSIX | FL_CLOSE)
24 * Special return value from posix_lock_file() and vfs_lock_file() for
25 * asynchronous locking.
27 #define FILE_LOCK_DEFERRED 1
32 struct file_lock_operations
{
33 void (*fl_copy_lock
)(struct file_lock
*, struct file_lock
*);
34 void (*fl_release_private
)(struct file_lock
*);
37 struct lock_manager_operations
{
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);
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
*);
55 struct list_head list
;
57 * NFSv4 and up also want opens blocked during the grace period;
64 void locks_start_grace(struct net
*, struct lock_manager
*);
65 void locks_end_grace(struct lock_manager
*);
66 bool locks_in_grace(struct net
*);
67 bool opens_in_grace(struct net
*);
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:
73 #include <linux/nfs_fs_i.h>
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.
81 * FIXME: should we create a separate "struct lock_request" to help distinguish
84 * The varous i_flctx lists are ordered by:
90 * Obviously, the last two criteria only matter for POSIX locks.
93 struct 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
100 struct list_head flc_blocked_member
; /* node in
101 * ->fl_blocker->fl_blocked_requests
103 fl_owner_t flc_owner
;
104 unsigned int flc_flags
;
105 unsigned char flc_type
;
107 int flc_link_cpu
; /* what cpu's list is this on? */
108 wait_queue_head_t flc_wait
;
109 struct file
*flc_file
;
113 struct file_lock_core c
;
117 const struct file_lock_operations
*fl_ops
; /* Callbacks for filesystems */
118 const struct lock_manager_operations
*fl_lmops
; /* Callbacks for lockmanagers */
120 struct nfs_lock_info nfs_fl
;
121 struct nfs4_lock_info nfs4_fl
;
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
;
131 } __randomize_layout
;
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
;
142 struct file_lock_context
{
144 struct list_head flc_flock
;
145 struct list_head flc_posix
;
146 struct list_head flc_lease
;
149 #ifdef CONFIG_FILE_LOCKING
150 int fcntl_getlk(struct file
*, unsigned int, struct flock
*);
151 int fcntl_setlk(unsigned int, struct file
*, unsigned int,
154 #if BITS_PER_LONG == 32
155 int fcntl_getlk64(struct file
*, unsigned int, struct flock64
*);
156 int fcntl_setlk64(unsigned int, struct file
*, unsigned int,
160 int fcntl_setlease(unsigned int fd
, struct file
*filp
, int arg
);
161 int fcntl_getlease(struct file
*filp
);
163 static inline bool lock_is_unlock(struct file_lock
*fl
)
165 return fl
->c
.flc_type
== F_UNLCK
;
168 static inline bool lock_is_read(struct file_lock
*fl
)
170 return fl
->c
.flc_type
== F_RDLCK
;
173 static inline bool lock_is_write(struct file_lock
*fl
)
175 return fl
->c
.flc_type
== F_WRLCK
;
178 static inline void locks_wake_up(struct file_lock
*fl
)
180 wake_up(&fl
->c
.flc_wait
);
183 static inline bool locks_can_async_lock(const struct file_operations
*fops
)
185 return !fops
->lock
|| fops
->fop_flags
& FOP_ASYNC_LOCK
;
189 void locks_free_lock_context(struct inode
*inode
);
190 void locks_free_lock(struct file_lock
*fl
);
191 void locks_init_lock(struct file_lock
*);
192 struct file_lock
*locks_alloc_lock(void);
193 void locks_copy_lock(struct file_lock
*, struct file_lock
*);
194 void locks_copy_conflock(struct file_lock
*, struct file_lock
*);
195 void locks_remove_posix(struct file
*, fl_owner_t
);
196 void locks_remove_file(struct file
*);
197 void locks_release_private(struct file_lock
*);
198 void posix_test_lock(struct file
*, struct file_lock
*);
199 int posix_lock_file(struct file
*, struct file_lock
*, struct file_lock
*);
200 int locks_delete_block(struct file_lock
*);
201 int vfs_test_lock(struct file
*, struct file_lock
*);
202 int vfs_lock_file(struct file
*, unsigned int, struct file_lock
*, struct file_lock
*);
203 int vfs_cancel_lock(struct file
*filp
, struct file_lock
*fl
);
204 bool vfs_inode_has_locks(struct inode
*inode
);
205 int locks_lock_inode_wait(struct inode
*inode
, struct file_lock
*fl
);
207 void locks_init_lease(struct file_lease
*);
208 void locks_free_lease(struct file_lease
*fl
);
209 struct file_lease
*locks_alloc_lease(void);
210 int __break_lease(struct inode
*inode
, unsigned int flags
, unsigned int type
);
211 void lease_get_mtime(struct inode
*, struct timespec64
*time
);
212 int generic_setlease(struct file
*, int, struct file_lease
**, void **priv
);
213 int kernel_setlease(struct file
*, int, struct file_lease
**, void **);
214 int vfs_setlease(struct file
*, int, struct file_lease
**, void **);
215 int lease_modify(struct file_lease
*, int, struct list_head
*);
217 struct notifier_block
;
218 int lease_register_notifier(struct notifier_block
*);
219 void lease_unregister_notifier(struct notifier_block
*);
222 void show_fd_locks(struct seq_file
*f
,
223 struct file
*filp
, struct files_struct
*files
);
224 bool locks_owner_has_blockers(struct file_lock_context
*flctx
,
227 static inline struct file_lock_context
*
228 locks_inode_context(const struct inode
*inode
)
230 return smp_load_acquire(&inode
->i_flctx
);
233 #else /* !CONFIG_FILE_LOCKING */
234 static inline int fcntl_getlk(struct file
*file
, unsigned int cmd
,
235 struct flock __user
*user
)
240 static inline int fcntl_setlk(unsigned int fd
, struct file
*file
,
241 unsigned int cmd
, struct flock __user
*user
)
246 #if BITS_PER_LONG == 32
247 static inline int fcntl_getlk64(struct file
*file
, unsigned int cmd
,
248 struct flock64
*user
)
253 static inline int fcntl_setlk64(unsigned int fd
, struct file
*file
,
254 unsigned int cmd
, struct flock64
*user
)
259 static inline int fcntl_setlease(unsigned int fd
, struct file
*filp
, int arg
)
264 static inline int fcntl_getlease(struct file
*filp
)
269 static inline bool lock_is_unlock(struct file_lock
*fl
)
274 static inline bool lock_is_read(struct file_lock
*fl
)
279 static inline bool lock_is_write(struct file_lock
*fl
)
284 static inline void locks_wake_up(struct file_lock
*fl
)
289 locks_free_lock_context(struct inode
*inode
)
293 static inline void locks_init_lock(struct file_lock
*fl
)
298 static inline void locks_init_lease(struct file_lease
*fl
)
303 static inline void locks_copy_conflock(struct file_lock
*new, struct file_lock
*fl
)
308 static inline void locks_copy_lock(struct file_lock
*new, struct file_lock
*fl
)
313 static inline void locks_remove_posix(struct file
*filp
, fl_owner_t owner
)
318 static inline void locks_remove_file(struct file
*filp
)
323 static inline void posix_test_lock(struct file
*filp
, struct file_lock
*fl
)
328 static inline int posix_lock_file(struct file
*filp
, struct file_lock
*fl
,
329 struct file_lock
*conflock
)
334 static inline int locks_delete_block(struct file_lock
*waiter
)
339 static inline int vfs_test_lock(struct file
*filp
, struct file_lock
*fl
)
344 static inline int vfs_lock_file(struct file
*filp
, unsigned int cmd
,
345 struct file_lock
*fl
, struct file_lock
*conf
)
350 static inline int vfs_cancel_lock(struct file
*filp
, struct file_lock
*fl
)
355 static inline bool vfs_inode_has_locks(struct inode
*inode
)
360 static inline int locks_lock_inode_wait(struct inode
*inode
, struct file_lock
*fl
)
365 static inline int __break_lease(struct inode
*inode
, unsigned int mode
, unsigned int type
)
370 static inline void lease_get_mtime(struct inode
*inode
,
371 struct timespec64
*time
)
376 static inline int generic_setlease(struct file
*filp
, int arg
,
377 struct file_lease
**flp
, void **priv
)
382 static inline int kernel_setlease(struct file
*filp
, int arg
,
383 struct file_lease
**lease
, void **priv
)
388 static inline int vfs_setlease(struct file
*filp
, int arg
,
389 struct file_lease
**lease
, void **priv
)
394 static inline int lease_modify(struct file_lease
*fl
, int arg
,
395 struct list_head
*dispose
)
401 static inline void show_fd_locks(struct seq_file
*f
,
402 struct file
*filp
, struct files_struct
*files
) {}
403 static inline bool locks_owner_has_blockers(struct file_lock_context
*flctx
,
409 static inline struct file_lock_context
*
410 locks_inode_context(const struct inode
*inode
)
415 #endif /* !CONFIG_FILE_LOCKING */
417 /* for walking lists of file_locks linked by fl_list */
418 #define for_each_file_lock(_fl, _head) list_for_each_entry(_fl, _head, c.flc_list)
420 static inline int locks_lock_file_wait(struct file
*filp
, struct file_lock
*fl
)
422 return locks_lock_inode_wait(file_inode(filp
), fl
);
425 #ifdef CONFIG_FILE_LOCKING
426 static inline int break_lease(struct inode
*inode
, unsigned int mode
)
428 struct file_lock_context
*flctx
;
431 * Since this check is lockless, we must ensure that any refcounts
432 * taken are done before checking i_flctx->flc_lease. Otherwise, we
433 * could end up racing with tasks trying to set a new lease on this
436 flctx
= READ_ONCE(inode
->i_flctx
);
440 if (!list_empty_careful(&flctx
->flc_lease
))
441 return __break_lease(inode
, mode
, FL_LEASE
);
445 static inline int break_deleg(struct inode
*inode
, unsigned int mode
)
447 struct file_lock_context
*flctx
;
450 * Since this check is lockless, we must ensure that any refcounts
451 * taken are done before checking i_flctx->flc_lease. Otherwise, we
452 * could end up racing with tasks trying to set a new lease on this
455 flctx
= READ_ONCE(inode
->i_flctx
);
459 if (!list_empty_careful(&flctx
->flc_lease
))
460 return __break_lease(inode
, mode
, FL_DELEG
);
464 static inline int try_break_deleg(struct inode
*inode
, struct inode
**delegated_inode
)
468 ret
= break_deleg(inode
, O_WRONLY
|O_NONBLOCK
);
469 if (ret
== -EWOULDBLOCK
&& delegated_inode
) {
470 *delegated_inode
= inode
;
476 static inline int break_deleg_wait(struct inode
**delegated_inode
)
480 ret
= break_deleg(*delegated_inode
, O_WRONLY
);
481 iput(*delegated_inode
);
482 *delegated_inode
= NULL
;
486 static inline int break_layout(struct inode
*inode
, bool wait
)
489 if (inode
->i_flctx
&& !list_empty_careful(&inode
->i_flctx
->flc_lease
))
490 return __break_lease(inode
,
491 wait
? O_WRONLY
: O_WRONLY
| O_NONBLOCK
,
496 #else /* !CONFIG_FILE_LOCKING */
497 static inline int break_lease(struct inode
*inode
, unsigned int mode
)
502 static inline int break_deleg(struct inode
*inode
, unsigned int mode
)
507 static inline int try_break_deleg(struct inode
*inode
, struct inode
**delegated_inode
)
512 static inline int break_deleg_wait(struct inode
**delegated_inode
)
518 static inline int break_layout(struct inode
*inode
, bool wait
)
523 #endif /* CONFIG_FILE_LOCKING */
525 #endif /* _LINUX_FILELOCK_H */