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