xref: /linux-6.15/include/linux/filelock.h (revision a69ce85e)
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 
29 struct file_lock;
30 
31 struct file_lock_operations {
32 	void (*fl_copy_lock)(struct file_lock *, struct file_lock *);
33 	void (*fl_release_private)(struct file_lock *);
34 };
35 
36 struct lock_manager_operations {
37 	void *lm_mod_owner;
38 	fl_owner_t (*lm_get_owner)(fl_owner_t);
39 	void (*lm_put_owner)(fl_owner_t);
40 	void (*lm_notify)(struct file_lock *);	/* unblock callback */
41 	int (*lm_grant)(struct file_lock *, int);
42 	bool (*lm_break)(struct file_lock *);
43 	int (*lm_change)(struct file_lock *, int, struct list_head *);
44 	void (*lm_setup)(struct file_lock *, void **);
45 	bool (*lm_breaker_owns_lease)(struct file_lock *);
46 	bool (*lm_lock_expirable)(struct file_lock *cfl);
47 	void (*lm_expire_lock)(void);
48 };
49 
50 struct lock_manager {
51 	struct list_head list;
52 	/*
53 	 * NFSv4 and up also want opens blocked during the grace period;
54 	 * NLM doesn't care:
55 	 */
56 	bool block_opens;
57 };
58 
59 struct net;
60 void locks_start_grace(struct net *, struct lock_manager *);
61 void locks_end_grace(struct lock_manager *);
62 bool locks_in_grace(struct net *);
63 bool opens_in_grace(struct net *);
64 
65 /*
66  * struct file_lock has a union that some filesystems use to track
67  * their own private info. The NFS side of things is defined here:
68  */
69 #include <linux/nfs_fs_i.h>
70 
71 /*
72  * struct file_lock represents a generic "file lock". It's used to represent
73  * POSIX byte range locks, BSD (flock) locks, and leases. It's important to
74  * note that the same struct is used to represent both a request for a lock and
75  * the lock itself, but the same object is never used for both.
76  *
77  * FIXME: should we create a separate "struct lock_request" to help distinguish
78  * these two uses?
79  *
80  * The varous i_flctx lists are ordered by:
81  *
82  * 1) lock owner
83  * 2) lock range start
84  * 3) lock range end
85  *
86  * Obviously, the last two criteria only matter for POSIX locks.
87  */
88 
89 struct file_lock_core {
90 	struct file_lock *flc_blocker;	/* The lock that is blocking us */
91 	struct list_head flc_list;	/* link into file_lock_context */
92 	struct hlist_node flc_link;	/* node in global lists */
93 	struct list_head flc_blocked_requests;	/* list of requests with
94 						 * ->fl_blocker pointing here
95 						 */
96 	struct list_head flc_blocked_member;	/* node in
97 						 * ->fl_blocker->fl_blocked_requests
98 						 */
99 	fl_owner_t flc_owner;
100 	unsigned int flc_flags;
101 	unsigned char flc_type;
102 	pid_t flc_pid;
103 	int flc_link_cpu;		/* what cpu's list is this on? */
104 	wait_queue_head_t flc_wait;
105 	struct file *flc_file;
106 };
107 
108 struct file_lock {
109 	struct file_lock_core c;
110 	loff_t fl_start;
111 	loff_t fl_end;
112 
113 	struct fasync_struct *	fl_fasync; /* for lease break notifications */
114 	/* for lease breaks: */
115 	unsigned long fl_break_time;
116 	unsigned long fl_downgrade_time;
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 
134 /* Temporary macros to allow building during coccinelle conversion */
135 #ifdef _NEED_FILE_LOCK_FIELD_MACROS
136 #define fl_list c.flc_list
137 #define fl_blocker c.flc_blocker
138 #define fl_link c.flc_link
139 #define fl_blocked_requests c.flc_blocked_requests
140 #define fl_blocked_member c.flc_blocked_member
141 #define fl_owner c.flc_owner
142 #define fl_flags c.flc_flags
143 #define fl_type c.flc_type
144 #define fl_pid c.flc_pid
145 #define fl_link_cpu c.flc_link_cpu
146 #define fl_wait c.flc_wait
147 #define fl_file c.flc_file
148 #endif
149 
150 struct file_lock_context {
151 	spinlock_t		flc_lock;
152 	struct list_head	flc_flock;
153 	struct list_head	flc_posix;
154 	struct list_head	flc_lease;
155 };
156 
157 #ifdef CONFIG_FILE_LOCKING
158 int fcntl_getlk(struct file *, unsigned int, struct flock *);
159 int fcntl_setlk(unsigned int, struct file *, unsigned int,
160 			struct flock *);
161 
162 #if BITS_PER_LONG == 32
163 int fcntl_getlk64(struct file *, unsigned int, struct flock64 *);
164 int fcntl_setlk64(unsigned int, struct file *, unsigned int,
165 			struct flock64 *);
166 #endif
167 
168 int fcntl_setlease(unsigned int fd, struct file *filp, int arg);
169 int fcntl_getlease(struct file *filp);
170 
171 static inline bool lock_is_unlock(struct file_lock *fl)
172 {
173 	return fl->c.flc_type == F_UNLCK;
174 }
175 
176 static inline bool lock_is_read(struct file_lock *fl)
177 {
178 	return fl->c.flc_type == F_RDLCK;
179 }
180 
181 static inline bool lock_is_write(struct file_lock *fl)
182 {
183 	return fl->c.flc_type == F_WRLCK;
184 }
185 
186 static inline void locks_wake_up(struct file_lock *fl)
187 {
188 	wake_up(&fl->c.flc_wait);
189 }
190 
191 /* for walking lists of file_locks linked by fl_list */
192 #define for_each_file_lock(_fl, _head)	list_for_each_entry(_fl, _head, c.flc_list)
193 
194 /* fs/locks.c */
195 void locks_free_lock_context(struct inode *inode);
196 void locks_free_lock(struct file_lock *fl);
197 void locks_init_lock(struct file_lock *);
198 struct file_lock * locks_alloc_lock(void);
199 void locks_copy_lock(struct file_lock *, struct file_lock *);
200 void locks_copy_conflock(struct file_lock *, struct file_lock *);
201 void locks_remove_posix(struct file *, fl_owner_t);
202 void locks_remove_file(struct file *);
203 void locks_release_private(struct file_lock *);
204 void posix_test_lock(struct file *, struct file_lock *);
205 int posix_lock_file(struct file *, struct file_lock *, struct file_lock *);
206 int locks_delete_block(struct file_lock *);
207 int vfs_test_lock(struct file *, struct file_lock *);
208 int vfs_lock_file(struct file *, unsigned int, struct file_lock *, struct file_lock *);
209 int vfs_cancel_lock(struct file *filp, struct file_lock *fl);
210 bool vfs_inode_has_locks(struct inode *inode);
211 int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl);
212 int __break_lease(struct inode *inode, unsigned int flags, unsigned int type);
213 void lease_get_mtime(struct inode *, struct timespec64 *time);
214 int generic_setlease(struct file *, int, struct file_lock **, void **priv);
215 int vfs_setlease(struct file *, int, struct file_lock **, void **);
216 int lease_modify(struct file_lock *, int, struct list_head *);
217 
218 struct notifier_block;
219 int lease_register_notifier(struct notifier_block *);
220 void lease_unregister_notifier(struct notifier_block *);
221 
222 struct files_struct;
223 void show_fd_locks(struct seq_file *f,
224 			 struct file *filp, struct files_struct *files);
225 bool locks_owner_has_blockers(struct file_lock_context *flctx,
226 			fl_owner_t owner);
227 
228 static inline struct file_lock_context *
229 locks_inode_context(const struct inode *inode)
230 {
231 	return smp_load_acquire(&inode->i_flctx);
232 }
233 
234 #else /* !CONFIG_FILE_LOCKING */
235 static inline int fcntl_getlk(struct file *file, unsigned int cmd,
236 			      struct flock __user *user)
237 {
238 	return -EINVAL;
239 }
240 
241 static inline int fcntl_setlk(unsigned int fd, struct file *file,
242 			      unsigned int cmd, struct flock __user *user)
243 {
244 	return -EACCES;
245 }
246 
247 #if BITS_PER_LONG == 32
248 static inline int fcntl_getlk64(struct file *file, unsigned int cmd,
249 				struct flock64 *user)
250 {
251 	return -EINVAL;
252 }
253 
254 static inline int fcntl_setlk64(unsigned int fd, struct file *file,
255 				unsigned int cmd, struct flock64 *user)
256 {
257 	return -EACCES;
258 }
259 #endif
260 static inline int fcntl_setlease(unsigned int fd, struct file *filp, int arg)
261 {
262 	return -EINVAL;
263 }
264 
265 static inline int fcntl_getlease(struct file *filp)
266 {
267 	return F_UNLCK;
268 }
269 
270 static inline bool lock_is_unlock(struct file_lock *fl)
271 {
272 	return false;
273 }
274 
275 static inline bool lock_is_read(struct file_lock *fl)
276 {
277 	return false;
278 }
279 
280 static inline bool lock_is_write(struct file_lock *fl)
281 {
282 	return false;
283 }
284 
285 static inline void locks_wake_up(struct file_lock *fl)
286 {
287 }
288 
289 #define for_each_file_lock(_fl, _head)	while(false)
290 
291 static inline void
292 locks_free_lock_context(struct inode *inode)
293 {
294 }
295 
296 static inline void locks_init_lock(struct file_lock *fl)
297 {
298 	return;
299 }
300 
301 static inline void locks_copy_conflock(struct file_lock *new, struct file_lock *fl)
302 {
303 	return;
304 }
305 
306 static inline void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
307 {
308 	return;
309 }
310 
311 static inline void locks_remove_posix(struct file *filp, fl_owner_t owner)
312 {
313 	return;
314 }
315 
316 static inline void locks_remove_file(struct file *filp)
317 {
318 	return;
319 }
320 
321 static inline void posix_test_lock(struct file *filp, struct file_lock *fl)
322 {
323 	return;
324 }
325 
326 static inline int posix_lock_file(struct file *filp, struct file_lock *fl,
327 				  struct file_lock *conflock)
328 {
329 	return -ENOLCK;
330 }
331 
332 static inline int locks_delete_block(struct file_lock *waiter)
333 {
334 	return -ENOENT;
335 }
336 
337 static inline int vfs_test_lock(struct file *filp, struct file_lock *fl)
338 {
339 	return 0;
340 }
341 
342 static inline int vfs_lock_file(struct file *filp, unsigned int cmd,
343 				struct file_lock *fl, struct file_lock *conf)
344 {
345 	return -ENOLCK;
346 }
347 
348 static inline int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
349 {
350 	return 0;
351 }
352 
353 static inline bool vfs_inode_has_locks(struct inode *inode)
354 {
355 	return false;
356 }
357 
358 static inline int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl)
359 {
360 	return -ENOLCK;
361 }
362 
363 static inline int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
364 {
365 	return 0;
366 }
367 
368 static inline void lease_get_mtime(struct inode *inode,
369 				   struct timespec64 *time)
370 {
371 	return;
372 }
373 
374 static inline int generic_setlease(struct file *filp, int arg,
375 				    struct file_lock **flp, void **priv)
376 {
377 	return -EINVAL;
378 }
379 
380 static inline int vfs_setlease(struct file *filp, int arg,
381 			       struct file_lock **lease, void **priv)
382 {
383 	return -EINVAL;
384 }
385 
386 static inline int lease_modify(struct file_lock *fl, int arg,
387 			       struct list_head *dispose)
388 {
389 	return -EINVAL;
390 }
391 
392 struct files_struct;
393 static inline void show_fd_locks(struct seq_file *f,
394 			struct file *filp, struct files_struct *files) {}
395 static inline bool locks_owner_has_blockers(struct file_lock_context *flctx,
396 			fl_owner_t owner)
397 {
398 	return false;
399 }
400 
401 static inline struct file_lock_context *
402 locks_inode_context(const struct inode *inode)
403 {
404 	return NULL;
405 }
406 
407 #endif /* !CONFIG_FILE_LOCKING */
408 
409 static inline int locks_lock_file_wait(struct file *filp, struct file_lock *fl)
410 {
411 	return locks_lock_inode_wait(file_inode(filp), fl);
412 }
413 
414 #ifdef CONFIG_FILE_LOCKING
415 static inline int break_lease(struct inode *inode, unsigned int mode)
416 {
417 	/*
418 	 * Since this check is lockless, we must ensure that any refcounts
419 	 * taken are done before checking i_flctx->flc_lease. Otherwise, we
420 	 * could end up racing with tasks trying to set a new lease on this
421 	 * file.
422 	 */
423 	smp_mb();
424 	if (inode->i_flctx && !list_empty_careful(&inode->i_flctx->flc_lease))
425 		return __break_lease(inode, mode, FL_LEASE);
426 	return 0;
427 }
428 
429 static inline int break_deleg(struct inode *inode, unsigned int mode)
430 {
431 	/*
432 	 * Since this check is lockless, we must ensure that any refcounts
433 	 * taken are done before checking i_flctx->flc_lease. Otherwise, we
434 	 * could end up racing with tasks trying to set a new lease on this
435 	 * file.
436 	 */
437 	smp_mb();
438 	if (inode->i_flctx && !list_empty_careful(&inode->i_flctx->flc_lease))
439 		return __break_lease(inode, mode, FL_DELEG);
440 	return 0;
441 }
442 
443 static inline int try_break_deleg(struct inode *inode, struct inode **delegated_inode)
444 {
445 	int ret;
446 
447 	ret = break_deleg(inode, O_WRONLY|O_NONBLOCK);
448 	if (ret == -EWOULDBLOCK && delegated_inode) {
449 		*delegated_inode = inode;
450 		ihold(inode);
451 	}
452 	return ret;
453 }
454 
455 static inline int break_deleg_wait(struct inode **delegated_inode)
456 {
457 	int ret;
458 
459 	ret = break_deleg(*delegated_inode, O_WRONLY);
460 	iput(*delegated_inode);
461 	*delegated_inode = NULL;
462 	return ret;
463 }
464 
465 static inline int break_layout(struct inode *inode, bool wait)
466 {
467 	smp_mb();
468 	if (inode->i_flctx && !list_empty_careful(&inode->i_flctx->flc_lease))
469 		return __break_lease(inode,
470 				wait ? O_WRONLY : O_WRONLY | O_NONBLOCK,
471 				FL_LAYOUT);
472 	return 0;
473 }
474 
475 #else /* !CONFIG_FILE_LOCKING */
476 static inline int break_lease(struct inode *inode, unsigned int mode)
477 {
478 	return 0;
479 }
480 
481 static inline int break_deleg(struct inode *inode, unsigned int mode)
482 {
483 	return 0;
484 }
485 
486 static inline int try_break_deleg(struct inode *inode, struct inode **delegated_inode)
487 {
488 	return 0;
489 }
490 
491 static inline int break_deleg_wait(struct inode **delegated_inode)
492 {
493 	BUG();
494 	return 0;
495 }
496 
497 static inline int break_layout(struct inode *inode, bool wait)
498 {
499 	return 0;
500 }
501 
502 #endif /* CONFIG_FILE_LOCKING */
503 
504 #endif /* _LINUX_FILELOCK_H */
505