blob: 9afb16e0683ffd0ff0430849686dc6ea8b8640f9 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/locks.c
4 *
J. Bruce Fieldse9728cc2021-10-19 13:38:35 -04005 * We implement four types of file locks: BSD locks, posix locks, open
6 * file description locks, and leases. For details about BSD locks,
7 * see the flock(2) man page; for details about the other three, see
8 * fcntl(2).
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
NeilBrownfd7732e2018-11-30 10:04:08 +110010 *
11 * Locking conflicts and dependencies:
12 * If multiple threads attempt to lock the same byte (or flock the same file)
13 * only one can be granted the lock, and other must wait their turn.
14 * The first lock has been "applied" or "granted", the others are "waiting"
15 * and are "blocked" by the "applied" lock..
16 *
17 * Waiting and applied locks are all kept in trees whose properties are:
18 *
19 * - the root of a tree may be an applied or waiting lock.
20 * - every other node in the tree is a waiting lock that
21 * conflicts with every ancestor of that node.
22 *
23 * Every such tree begins life as a waiting singleton which obviously
24 * satisfies the above properties.
25 *
26 * The only ways we modify trees preserve these properties:
27 *
28 * 1. We may add a new leaf node, but only after first verifying that it
29 * conflicts with all of its ancestors.
30 * 2. We may remove the root of a tree, creating a new singleton
31 * tree from the root and N new trees rooted in the immediate
32 * children.
33 * 3. If the root of a tree is not currently an applied lock, we may
34 * apply it (if possible).
35 * 4. We may upgrade the root of the tree (either extend its range,
36 * or upgrade its entire range from read to write).
37 *
38 * When an applied lock is modified in a way that reduces or downgrades any
39 * part of its range, we remove all its children (2 above). This particularly
40 * happens when a lock is unlocked.
41 *
42 * For each of those child trees we "wake up" the thread which is
43 * waiting for the lock so it can continue handling as follows: if the
44 * root of the tree applies, we do so (3). If it doesn't, it must
45 * conflict with some applied lock. We remove (wake up) all of its children
46 * (2), and add it is a new leaf to the tree rooted in the applied
47 * lock (1). We then repeat the process recursively with those
48 * children.
49 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/capability.h>
52#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040053#include <linux/fdtable.h>
Jeff Layton5970e152022-11-20 09:15:34 -050054#include <linux/filelock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/fs.h>
56#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/security.h>
58#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/syscalls.h>
60#include <linux/time.h>
Dipankar Sarma4fb3a532005-09-16 19:28:13 -070061#include <linux/rcupdate.h>
Vitaliy Gusevab1f1612008-01-17 00:07:08 +000062#include <linux/pid_namespace.h>
Jeff Layton48f74182013-06-21 08:58:18 -040063#include <linux/hashtable.h>
Jeff Layton7012b022013-06-21 08:58:22 -040064#include <linux/percpu.h>
Luis Chamberlaindd81faa2022-01-21 22:13:10 -080065#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Jeff Layton62af4f12014-05-09 14:13:05 -040067#define CREATE_TRACE_POINTS
68#include <trace/events/filelock.h>
69
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080070#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Jeff Layton1a62c222024-01-31 18:02:05 -050072static struct file_lock *file_lock(struct file_lock_core *flc)
73{
74 return container_of(flc, struct file_lock, c);
75}
76
Jeff Laytonc69ff402024-01-31 18:02:28 -050077static struct file_lease *file_lease(struct file_lock_core *flc)
78{
79 return container_of(flc, struct file_lease, c);
80}
81
82static bool lease_breaking(struct file_lease *fl)
J. Bruce Fieldsab83fa42011-07-26 20:10:51 -040083{
Jeff Layton4ca52f52024-01-31 18:01:59 -050084 return fl->c.flc_flags & (FL_UNLOCK_PENDING | FL_DOWNGRADE_PENDING);
J. Bruce Fields778fc542011-07-26 18:25:49 -040085}
86
Jeff Laytonc69ff402024-01-31 18:02:28 -050087static int target_leasetype(struct file_lease *fl)
J. Bruce Fields778fc542011-07-26 18:25:49 -040088{
Jeff Layton4ca52f52024-01-31 18:01:59 -050089 if (fl->c.flc_flags & FL_UNLOCK_PENDING)
J. Bruce Fields778fc542011-07-26 18:25:49 -040090 return F_UNLCK;
Jeff Layton4ca52f52024-01-31 18:01:59 -050091 if (fl->c.flc_flags & FL_DOWNGRADE_PENDING)
J. Bruce Fields778fc542011-07-26 18:25:49 -040092 return F_RDLCK;
Jeff Layton4ca52f52024-01-31 18:01:59 -050093 return fl->c.flc_type;
J. Bruce Fieldsab83fa42011-07-26 20:10:51 -040094}
95
Luis Chamberlaindd81faa2022-01-21 22:13:10 -080096static int leases_enable = 1;
97static int lease_break_time = 45;
98
99#ifdef CONFIG_SYSCTL
100static struct ctl_table locks_sysctls[] = {
101 {
102 .procname = "leases-enable",
103 .data = &leases_enable,
104 .maxlen = sizeof(int),
105 .mode = 0644,
106 .proc_handler = proc_dointvec,
107 },
108#ifdef CONFIG_MMU
109 {
110 .procname = "lease-break-time",
111 .data = &lease_break_time,
112 .maxlen = sizeof(int),
113 .mode = 0644,
114 .proc_handler = proc_dointvec,
115 },
116#endif /* CONFIG_MMU */
Luis Chamberlaindd81faa2022-01-21 22:13:10 -0800117};
118
119static int __init init_fs_locks_sysctls(void)
120{
121 register_sysctl_init("fs", locks_sysctls);
122 return 0;
123}
124early_initcall(init_fs_locks_sysctls);
125#endif /* CONFIG_SYSCTL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Jeff Layton1c8c6012013-06-21 08:58:15 -0400127/*
Jeff Layton7012b022013-06-21 08:58:22 -0400128 * The global file_lock_list is only used for displaying /proc/locks, so we
Peter Zijlstra7c3f6542015-06-22 14:16:34 +0200129 * keep a list on each CPU, with each list protected by its own spinlock.
130 * Global serialization is done using file_rwsem.
131 *
132 * Note that alterations to the list also require that the relevant flc_lock is
133 * held.
Jeff Layton1c8c6012013-06-21 08:58:15 -0400134 */
Peter Zijlstra7c3f6542015-06-22 14:16:34 +0200135struct file_lock_list_struct {
136 spinlock_t lock;
137 struct hlist_head hlist;
138};
139static DEFINE_PER_CPU(struct file_lock_list_struct, file_lock_list);
Peter Zijlstraaba37662015-06-22 14:16:33 +0200140DEFINE_STATIC_PERCPU_RWSEM(file_rwsem);
Jeff Layton88974692013-06-21 08:58:14 -0400141
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400142
Jeff Layton1c8c6012013-06-21 08:58:15 -0400143/*
Jeff Layton48f74182013-06-21 08:58:18 -0400144 * The blocked_hash is used to find POSIX lock loops for deadlock detection.
Jeff Layton7b2296a2013-06-21 08:58:20 -0400145 * It is protected by blocked_lock_lock.
Jeff Layton48f74182013-06-21 08:58:18 -0400146 *
147 * We hash locks by lockowner in order to optimize searching for the lock a
148 * particular lockowner is waiting on.
149 *
150 * FIXME: make this value scale via some heuristic? We generally will want more
151 * buckets when we have more lockowners holding locks, but that's a little
152 * difficult to determine without knowing what the workload will look like.
Jeff Layton1c8c6012013-06-21 08:58:15 -0400153 */
Jeff Layton48f74182013-06-21 08:58:18 -0400154#define BLOCKED_HASH_BITS 7
155static DEFINE_HASHTABLE(blocked_hash, BLOCKED_HASH_BITS);
Jeff Layton88974692013-06-21 08:58:14 -0400156
Jeff Layton1c8c6012013-06-21 08:58:15 -0400157/*
Jeff Layton7b2296a2013-06-21 08:58:20 -0400158 * This lock protects the blocked_hash. Generally, if you're accessing it, you
159 * want to be holding this lock.
Jeff Layton1c8c6012013-06-21 08:58:15 -0400160 *
NeilBrownada5c1d2018-11-30 10:04:08 +1100161 * In addition, it also protects the fl->fl_blocked_requests list, and the
162 * fl->fl_blocker pointer for file_lock structures that are acting as lock
163 * requests (in contrast to those that are acting as records of acquired locks).
Jeff Layton1c8c6012013-06-21 08:58:15 -0400164 *
165 * Note that when we acquire this lock in order to change the above fields,
Jeff Layton6109c852015-01-16 15:05:57 -0500166 * we often hold the flc_lock as well. In certain cases, when reading the fields
Jeff Layton1c8c6012013-06-21 08:58:15 -0400167 * protected by this lock, we can skip acquiring it iff we already hold the
Jeff Layton6109c852015-01-16 15:05:57 -0500168 * flc_lock.
Jeff Layton1c8c6012013-06-21 08:58:15 -0400169 */
Jeff Layton7b2296a2013-06-21 08:58:20 -0400170static DEFINE_SPINLOCK(blocked_lock_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Alexey Dobriyan68279f92023-10-11 19:55:00 +0300172static struct kmem_cache *flctx_cache __ro_after_init;
173static struct kmem_cache *filelock_cache __ro_after_init;
Jeff Laytonc69ff402024-01-31 18:02:28 -0500174static struct kmem_cache *filelease_cache __ro_after_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Jeff Layton4a075e32015-01-16 15:05:54 -0500176static struct file_lock_context *
Jeff Layton5c1c6692015-04-03 09:04:03 -0400177locks_get_lock_context(struct inode *inode, int type)
Jeff Layton4a075e32015-01-16 15:05:54 -0500178{
Dmitry Vyukov128a3782015-09-21 09:43:06 +0200179 struct file_lock_context *ctx;
Jeff Layton4a075e32015-01-16 15:05:54 -0500180
Dmitry Vyukov128a3782015-09-21 09:43:06 +0200181 /* paired with cmpxchg() below */
Jeff Layton401a8b82022-11-16 09:02:30 -0500182 ctx = locks_inode_context(inode);
Dmitry Vyukov128a3782015-09-21 09:43:06 +0200183 if (likely(ctx) || type == F_UNLCK)
Jeff Layton4a075e32015-01-16 15:05:54 -0500184 goto out;
185
Dmitry Vyukov128a3782015-09-21 09:43:06 +0200186 ctx = kmem_cache_alloc(flctx_cache, GFP_KERNEL);
187 if (!ctx)
Jeff Layton4a075e32015-01-16 15:05:54 -0500188 goto out;
189
Dmitry Vyukov128a3782015-09-21 09:43:06 +0200190 spin_lock_init(&ctx->flc_lock);
191 INIT_LIST_HEAD(&ctx->flc_flock);
192 INIT_LIST_HEAD(&ctx->flc_posix);
193 INIT_LIST_HEAD(&ctx->flc_lease);
Jeff Layton4a075e32015-01-16 15:05:54 -0500194
195 /*
196 * Assign the pointer if it's not already assigned. If it is, then
197 * free the context we just allocated.
198 */
Dmitry Vyukov128a3782015-09-21 09:43:06 +0200199 if (cmpxchg(&inode->i_flctx, NULL, ctx)) {
200 kmem_cache_free(flctx_cache, ctx);
Jeff Layton401a8b82022-11-16 09:02:30 -0500201 ctx = locks_inode_context(inode);
Dmitry Vyukov128a3782015-09-21 09:43:06 +0200202 }
Jeff Layton4a075e32015-01-16 15:05:54 -0500203out:
Jeff Layton18909102016-01-06 21:26:10 -0500204 trace_locks_get_lock_context(inode, type, ctx);
Dmitry Vyukov128a3782015-09-21 09:43:06 +0200205 return ctx;
Jeff Layton4a075e32015-01-16 15:05:54 -0500206}
207
Jeff Laytone24dada2016-01-06 21:28:41 -0500208static void
209locks_dump_ctx_list(struct list_head *list, char *list_type)
210{
Jeff Laytonfde49512024-01-31 18:02:00 -0500211 struct file_lock_core *flc;
Jeff Laytone24dada2016-01-06 21:28:41 -0500212
Jeff Laytonfde49512024-01-31 18:02:00 -0500213 list_for_each_entry(flc, list, flc_list)
214 pr_warn("%s: fl_owner=%p fl_flags=0x%x fl_type=0x%x fl_pid=%u\n",
215 list_type, flc->flc_owner, flc->flc_flags,
216 flc->flc_type, flc->flc_pid);
Jeff Laytone24dada2016-01-06 21:28:41 -0500217}
218
219static void
220locks_check_ctx_lists(struct inode *inode)
221{
222 struct file_lock_context *ctx = inode->i_flctx;
223
224 if (unlikely(!list_empty(&ctx->flc_flock) ||
225 !list_empty(&ctx->flc_posix) ||
226 !list_empty(&ctx->flc_lease))) {
227 pr_warn("Leaked locks on dev=0x%x:0x%x ino=0x%lx:\n",
228 MAJOR(inode->i_sb->s_dev), MINOR(inode->i_sb->s_dev),
229 inode->i_ino);
230 locks_dump_ctx_list(&ctx->flc_flock, "FLOCK");
231 locks_dump_ctx_list(&ctx->flc_posix, "POSIX");
232 locks_dump_ctx_list(&ctx->flc_lease, "LEASE");
233 }
234}
235
Benjamin Coddington39537042017-07-21 13:36:25 -0400236static void
Jeff Laytonfde49512024-01-31 18:02:00 -0500237locks_check_ctx_file_list(struct file *filp, struct list_head *list, char *list_type)
Benjamin Coddington39537042017-07-21 13:36:25 -0400238{
Jeff Laytonfde49512024-01-31 18:02:00 -0500239 struct file_lock_core *flc;
Jeff Laytonc65454a2022-11-25 08:48:37 -0500240 struct inode *inode = file_inode(filp);
Benjamin Coddington39537042017-07-21 13:36:25 -0400241
Jeff Laytonfde49512024-01-31 18:02:00 -0500242 list_for_each_entry(flc, list, flc_list)
243 if (flc->flc_file == filp)
Benjamin Coddington39537042017-07-21 13:36:25 -0400244 pr_warn("Leaked %s lock on dev=0x%x:0x%x ino=0x%lx "
245 " fl_owner=%p fl_flags=0x%x fl_type=0x%x fl_pid=%u\n",
246 list_type, MAJOR(inode->i_sb->s_dev),
247 MINOR(inode->i_sb->s_dev), inode->i_ino,
Jeff Laytonfde49512024-01-31 18:02:00 -0500248 flc->flc_owner, flc->flc_flags,
249 flc->flc_type, flc->flc_pid);
Benjamin Coddington39537042017-07-21 13:36:25 -0400250}
251
Jeff Layton4a075e32015-01-16 15:05:54 -0500252void
Jeff Laytonf27a0fe2016-01-07 15:08:51 -0500253locks_free_lock_context(struct inode *inode)
Jeff Layton4a075e32015-01-16 15:05:54 -0500254{
Jeff Layton401a8b82022-11-16 09:02:30 -0500255 struct file_lock_context *ctx = locks_inode_context(inode);
Jeff Laytonf27a0fe2016-01-07 15:08:51 -0500256
Jeff Laytone24dada2016-01-06 21:28:41 -0500257 if (unlikely(ctx)) {
258 locks_check_ctx_lists(inode);
Jeff Layton4a075e32015-01-16 15:05:54 -0500259 kmem_cache_free(flctx_cache, ctx);
260 }
261}
262
Jeff Layton4ca52f52024-01-31 18:01:59 -0500263static void locks_init_lock_heads(struct file_lock_core *flc)
Miklos Szeredia51cb91d2011-07-06 12:33:55 +0200264{
Jeff Layton4ca52f52024-01-31 18:01:59 -0500265 INIT_HLIST_NODE(&flc->flc_link);
266 INIT_LIST_HEAD(&flc->flc_list);
267 INIT_LIST_HEAD(&flc->flc_blocked_requests);
268 INIT_LIST_HEAD(&flc->flc_blocked_member);
269 init_waitqueue_head(&flc->flc_wait);
Miklos Szeredia51cb91d2011-07-06 12:33:55 +0200270}
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272/* Allocate an empty lock structure. */
Arnd Bergmannc5b1f0d2010-10-27 15:46:08 +0200273struct file_lock *locks_alloc_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Miklos Szerediee19cc42011-07-07 13:06:09 +0200275 struct file_lock *fl = kmem_cache_zalloc(filelock_cache, GFP_KERNEL);
Miklos Szeredia51cb91d2011-07-06 12:33:55 +0200276
277 if (fl)
Jeff Layton4ca52f52024-01-31 18:01:59 -0500278 locks_init_lock_heads(&fl->c);
Miklos Szeredia51cb91d2011-07-06 12:33:55 +0200279
280 return fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
Arnd Bergmannc5b1f0d2010-10-27 15:46:08 +0200282EXPORT_SYMBOL_GPL(locks_alloc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Jeff Laytonc69ff402024-01-31 18:02:28 -0500284/* Allocate an empty lock structure. */
285struct file_lease *locks_alloc_lease(void)
286{
287 struct file_lease *fl = kmem_cache_zalloc(filelease_cache, GFP_KERNEL);
288
289 if (fl)
290 locks_init_lock_heads(&fl->c);
291
292 return fl;
293}
294EXPORT_SYMBOL_GPL(locks_alloc_lease);
295
Felix Blyakhera9e61e22009-03-31 15:12:56 -0500296void locks_release_private(struct file_lock *fl)
Trond Myklebust47831f32006-03-20 13:44:05 -0500297{
Jeff Laytonfde49512024-01-31 18:02:00 -0500298 struct file_lock_core *flc = &fl->c;
299
300 BUG_ON(waitqueue_active(&flc->flc_wait));
301 BUG_ON(!list_empty(&flc->flc_list));
302 BUG_ON(!list_empty(&flc->flc_blocked_requests));
303 BUG_ON(!list_empty(&flc->flc_blocked_member));
304 BUG_ON(!hlist_unhashed(&flc->flc_link));
NeilBrown59264592019-04-24 12:00:08 +1000305
Trond Myklebust47831f32006-03-20 13:44:05 -0500306 if (fl->fl_ops) {
307 if (fl->fl_ops->fl_release_private)
308 fl->fl_ops->fl_release_private(fl);
309 fl->fl_ops = NULL;
310 }
Trond Myklebust47831f32006-03-20 13:44:05 -0500311
Kinglong Mee5c97d7b2014-08-22 10:18:43 -0400312 if (fl->fl_lmops) {
Jeff Laytoncae80b32015-04-03 09:04:04 -0400313 if (fl->fl_lmops->lm_put_owner) {
Jeff Laytonfde49512024-01-31 18:02:00 -0500314 fl->fl_lmops->lm_put_owner(flc->flc_owner);
315 flc->flc_owner = NULL;
Jeff Laytoncae80b32015-04-03 09:04:04 -0400316 }
Kinglong Mee5c97d7b2014-08-22 10:18:43 -0400317 fl->fl_lmops = NULL;
318 }
Trond Myklebust47831f32006-03-20 13:44:05 -0500319}
Felix Blyakhera9e61e22009-03-31 15:12:56 -0500320EXPORT_SYMBOL_GPL(locks_release_private);
Trond Myklebust47831f32006-03-20 13:44:05 -0500321
Dai Ngo591502c52022-05-02 14:19:24 -0700322/**
323 * locks_owner_has_blockers - Check for blocking lock requests
324 * @flctx: file lock context
325 * @owner: lock owner
326 *
327 * Return values:
328 * %true: @owner has at least one blocker
329 * %false: @owner has no blockers
330 */
Jeff Laytonfde49512024-01-31 18:02:00 -0500331bool locks_owner_has_blockers(struct file_lock_context *flctx, fl_owner_t owner)
Dai Ngo591502c52022-05-02 14:19:24 -0700332{
Jeff Laytonfde49512024-01-31 18:02:00 -0500333 struct file_lock_core *flc;
Dai Ngo591502c52022-05-02 14:19:24 -0700334
335 spin_lock(&flctx->flc_lock);
Jeff Laytonfde49512024-01-31 18:02:00 -0500336 list_for_each_entry(flc, &flctx->flc_posix, flc_list) {
337 if (flc->flc_owner != owner)
Dai Ngo591502c52022-05-02 14:19:24 -0700338 continue;
Jeff Laytonfde49512024-01-31 18:02:00 -0500339 if (!list_empty(&flc->flc_blocked_requests)) {
Dai Ngo591502c52022-05-02 14:19:24 -0700340 spin_unlock(&flctx->flc_lock);
341 return true;
342 }
343 }
344 spin_unlock(&flctx->flc_lock);
345 return false;
346}
347EXPORT_SYMBOL_GPL(locks_owner_has_blockers);
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349/* Free a lock which is not in use. */
J. Bruce Fields05fa3132010-10-30 17:31:15 -0400350void locks_free_lock(struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Trond Myklebust47831f32006-03-20 13:44:05 -0500352 locks_release_private(fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 kmem_cache_free(filelock_cache, fl);
354}
J. Bruce Fields05fa3132010-10-30 17:31:15 -0400355EXPORT_SYMBOL(locks_free_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Jeff Laytonc69ff402024-01-31 18:02:28 -0500357/* Free a lease which is not in use. */
358void locks_free_lease(struct file_lease *fl)
359{
360 kmem_cache_free(filelease_cache, fl);
361}
362EXPORT_SYMBOL(locks_free_lease);
363
Jeff Laytoned9814d2014-08-11 14:20:31 -0400364static void
365locks_dispose_list(struct list_head *dispose)
366{
Jeff Laytonc69ff402024-01-31 18:02:28 -0500367 struct file_lock_core *flc;
Jeff Laytoned9814d2014-08-11 14:20:31 -0400368
369 while (!list_empty(dispose)) {
Jeff Laytonc69ff402024-01-31 18:02:28 -0500370 flc = list_first_entry(dispose, struct file_lock_core, flc_list);
371 list_del_init(&flc->flc_list);
372 if (flc->flc_flags & (FL_LEASE|FL_DELEG|FL_LAYOUT))
373 locks_free_lease(file_lease(flc));
374 else
375 locks_free_lock(file_lock(flc));
Jeff Laytoned9814d2014-08-11 14:20:31 -0400376 }
377}
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379void locks_init_lock(struct file_lock *fl)
380{
Miklos Szerediee19cc42011-07-07 13:06:09 +0200381 memset(fl, 0, sizeof(struct file_lock));
Jeff Layton4ca52f52024-01-31 18:01:59 -0500382 locks_init_lock_heads(&fl->c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384EXPORT_SYMBOL(locks_init_lock);
385
Jeff Laytonc69ff402024-01-31 18:02:28 -0500386void locks_init_lease(struct file_lease *fl)
387{
388 memset(fl, 0, sizeof(*fl));
389 locks_init_lock_heads(&fl->c);
390}
391EXPORT_SYMBOL(locks_init_lease);
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393/*
394 * Initialize a new lock from an existing file_lock structure.
395 */
Kinglong Mee3fe0fff12014-08-22 10:18:42 -0400396void locks_copy_conflock(struct file_lock *new, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Jeff Layton4ca52f52024-01-31 18:01:59 -0500398 new->c.flc_owner = fl->c.flc_owner;
399 new->c.flc_pid = fl->c.flc_pid;
400 new->c.flc_file = NULL;
401 new->c.flc_flags = fl->c.flc_flags;
402 new->c.flc_type = fl->c.flc_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 new->fl_start = fl->fl_start;
404 new->fl_end = fl->fl_end;
Kinglong Meef3282962014-08-22 10:18:43 -0400405 new->fl_lmops = fl->fl_lmops;
Trond Myklebust09969052006-03-20 13:44:38 -0500406 new->fl_ops = NULL;
Kinglong Meef3282962014-08-22 10:18:43 -0400407
408 if (fl->fl_lmops) {
409 if (fl->fl_lmops->lm_get_owner)
Jeff Layton4ca52f52024-01-31 18:01:59 -0500410 fl->fl_lmops->lm_get_owner(fl->c.flc_owner);
Kinglong Meef3282962014-08-22 10:18:43 -0400411 }
Trond Myklebust09969052006-03-20 13:44:38 -0500412}
Kinglong Mee3fe0fff12014-08-22 10:18:42 -0400413EXPORT_SYMBOL(locks_copy_conflock);
Trond Myklebust09969052006-03-20 13:44:38 -0500414
415void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
416{
Jeff Layton566709b2014-08-11 14:09:35 -0400417 /* "new" must be a freshly-initialized lock */
418 WARN_ON_ONCE(new->fl_ops);
Trond Myklebust09969052006-03-20 13:44:38 -0500419
Kinglong Mee3fe0fff12014-08-22 10:18:42 -0400420 locks_copy_conflock(new, fl);
Kinglong Meef3282962014-08-22 10:18:43 -0400421
Jeff Layton4ca52f52024-01-31 18:01:59 -0500422 new->c.flc_file = fl->c.flc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 new->fl_ops = fl->fl_ops;
Trond Myklebust47831f32006-03-20 13:44:05 -0500424
Kinglong Meef3282962014-08-22 10:18:43 -0400425 if (fl->fl_ops) {
426 if (fl->fl_ops->fl_copy_lock)
427 fl->fl_ops->fl_copy_lock(new, fl);
428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430EXPORT_SYMBOL(locks_copy_lock);
431
NeilBrown5946c432018-11-30 10:04:08 +1100432static void locks_move_blocks(struct file_lock *new, struct file_lock *fl)
433{
434 struct file_lock *f;
435
436 /*
437 * As ctx->flc_lock is held, new requests cannot be added to
Jeff Laytonb6aaba52024-01-31 18:02:07 -0500438 * ->flc_blocked_requests, so we don't need a lock to check if it
NeilBrown5946c432018-11-30 10:04:08 +1100439 * is empty.
440 */
Jeff Layton4ca52f52024-01-31 18:01:59 -0500441 if (list_empty(&fl->c.flc_blocked_requests))
NeilBrown5946c432018-11-30 10:04:08 +1100442 return;
443 spin_lock(&blocked_lock_lock);
Jeff Layton4ca52f52024-01-31 18:01:59 -0500444 list_splice_init(&fl->c.flc_blocked_requests,
445 &new->c.flc_blocked_requests);
446 list_for_each_entry(f, &new->c.flc_blocked_requests,
447 c.flc_blocked_member)
Jeff Laytonb6aaba52024-01-31 18:02:07 -0500448 f->c.flc_blocker = &new->c;
NeilBrown5946c432018-11-30 10:04:08 +1100449 spin_unlock(&blocked_lock_lock);
450}
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452static inline int flock_translate_cmd(int cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 switch (cmd) {
454 case LOCK_SH:
455 return F_RDLCK;
456 case LOCK_EX:
457 return F_WRLCK;
458 case LOCK_UN:
459 return F_UNLCK;
460 }
461 return -EINVAL;
462}
463
464/* Fill in a file_lock structure with an appropriate FLOCK lock. */
Kuniyuki Iwashima4149be72022-07-16 21:35:31 -0700465static void flock_make_lock(struct file *filp, struct file_lock *fl, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Kuniyuki Iwashima4149be72022-07-16 21:35:31 -0700467 locks_init_lock(fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Jeff Layton4ca52f52024-01-31 18:01:59 -0500469 fl->c.flc_file = filp;
470 fl->c.flc_owner = filp;
471 fl->c.flc_pid = current->tgid;
472 fl->c.flc_flags = FL_FLOCK;
473 fl->c.flc_type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 fl->fl_end = OFFSET_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
Jeff Laytond9077f72024-01-31 18:02:10 -0500477static int assign_type(struct file_lock_core *flc, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 switch (type) {
480 case F_RDLCK:
481 case F_WRLCK:
482 case F_UNLCK:
Jeff Laytond9077f72024-01-31 18:02:10 -0500483 flc->flc_type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 break;
485 default:
486 return -EINVAL;
487 }
488 return 0;
489}
490
J. Bruce Fieldsef12e722014-02-03 12:13:08 -0500491static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl,
492 struct flock64 *l)
493{
494 switch (l->l_whence) {
495 case SEEK_SET:
496 fl->fl_start = 0;
497 break;
498 case SEEK_CUR:
499 fl->fl_start = filp->f_pos;
500 break;
501 case SEEK_END:
502 fl->fl_start = i_size_read(file_inode(filp));
503 break;
504 default:
505 return -EINVAL;
506 }
507 if (l->l_start > OFFSET_MAX - fl->fl_start)
508 return -EOVERFLOW;
509 fl->fl_start += l->l_start;
510 if (fl->fl_start < 0)
511 return -EINVAL;
512
513 /* POSIX-1996 leaves the case l->l_len < 0 undefined;
514 POSIX-2001 defines it. */
515 if (l->l_len > 0) {
516 if (l->l_len - 1 > OFFSET_MAX - fl->fl_start)
517 return -EOVERFLOW;
Luo Meng16238412020-10-23 14:20:05 +0800518 fl->fl_end = fl->fl_start + (l->l_len - 1);
J. Bruce Fieldsef12e722014-02-03 12:13:08 -0500519
520 } else if (l->l_len < 0) {
521 if (fl->fl_start + l->l_len < 0)
522 return -EINVAL;
523 fl->fl_end = fl->fl_start - 1;
524 fl->fl_start += l->l_len;
525 } else
526 fl->fl_end = OFFSET_MAX;
527
Jeff Layton4ca52f52024-01-31 18:01:59 -0500528 fl->c.flc_owner = current->files;
529 fl->c.flc_pid = current->tgid;
530 fl->c.flc_file = filp;
531 fl->c.flc_flags = FL_POSIX;
J. Bruce Fieldsef12e722014-02-03 12:13:08 -0500532 fl->fl_ops = NULL;
533 fl->fl_lmops = NULL;
534
Jeff Laytond9077f72024-01-31 18:02:10 -0500535 return assign_type(&fl->c, l->l_type);
J. Bruce Fieldsef12e722014-02-03 12:13:08 -0500536}
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538/* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
539 * style lock.
540 */
541static int flock_to_posix_lock(struct file *filp, struct file_lock *fl,
542 struct flock *l)
543{
J. Bruce Fieldsef12e722014-02-03 12:13:08 -0500544 struct flock64 ll = {
545 .l_type = l->l_type,
546 .l_whence = l->l_whence,
547 .l_start = l->l_start,
548 .l_len = l->l_len,
549 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
J. Bruce Fieldsef12e722014-02-03 12:13:08 -0500551 return flock64_to_posix_lock(filp, fl, &ll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554/* default lease lock manager operations */
Jeff Layton4d01b7f2014-09-01 15:06:54 -0400555static bool
Jeff Laytonc69ff402024-01-31 18:02:28 -0500556lease_break_callback(struct file_lease *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG);
Jeff Layton4d01b7f2014-09-01 15:06:54 -0400559 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
Jeff Layton1c7dd2f2014-08-22 10:55:47 -0400562static void
Jeff Laytonc69ff402024-01-31 18:02:28 -0500563lease_setup(struct file_lease *fl, void **priv)
Jeff Layton1c7dd2f2014-08-22 10:55:47 -0400564{
Jeff Layton4ca52f52024-01-31 18:01:59 -0500565 struct file *filp = fl->c.flc_file;
Jeff Layton1c7dd2f2014-08-22 10:55:47 -0400566 struct fasync_struct *fa = *priv;
567
568 /*
569 * fasync_insert_entry() returns the old entry if any. If there was no
570 * old entry, then it used "priv" and inserted it into the fasync list.
571 * Clear the pointer to indicate that it shouldn't be freed.
572 */
573 if (!fasync_insert_entry(fa->fa_fd, filp, &fl->fl_fasync, fa))
574 *priv = NULL;
575
Eric W. Biederman01919132017-07-16 22:05:57 -0500576 __f_setown(filp, task_pid(current), PIDTYPE_TGID, 0);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -0400577}
578
Jeff Laytonc69ff402024-01-31 18:02:28 -0500579static const struct lease_manager_operations lease_manager_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -0400580 .lm_break = lease_break_callback,
J. Bruce Fields8fb47a42011-07-20 20:21:59 -0400581 .lm_change = lease_modify,
Jeff Layton1c7dd2f2014-08-22 10:55:47 -0400582 .lm_setup = lease_setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583};
584
585/*
586 * Initialize a lease, use the default lock manager operations
587 */
Jeff Laytonc69ff402024-01-31 18:02:28 -0500588static int lease_init(struct file *filp, int type, struct file_lease *fl)
Joe Perches447a5642018-03-21 15:09:32 -0700589{
Jeff Laytond9077f72024-01-31 18:02:10 -0500590 if (assign_type(&fl->c, type) != 0)
Trond Myklebust75dff552006-05-07 23:02:42 -0400591 return -EINVAL;
592
Jeff Layton4ca52f52024-01-31 18:01:59 -0500593 fl->c.flc_owner = filp;
594 fl->c.flc_pid = current->tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Jeff Layton4ca52f52024-01-31 18:01:59 -0500596 fl->c.flc_file = filp;
597 fl->c.flc_flags = FL_LEASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 fl->fl_lmops = &lease_manager_ops;
599 return 0;
600}
601
602/* Allocate a file_lock initialised to this type of lease */
Jeff Laytonc69ff402024-01-31 18:02:28 -0500603static struct file_lease *lease_alloc(struct file *filp, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Jeff Laytonc69ff402024-01-31 18:02:28 -0500605 struct file_lease *fl = locks_alloc_lease();
Trond Myklebust75dff552006-05-07 23:02:42 -0400606 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 if (fl == NULL)
J. Bruce Fieldse32b8ee2007-03-01 14:34:35 -0500609 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 error = lease_init(filp, type, fl);
Trond Myklebust75dff552006-05-07 23:02:42 -0400612 if (error) {
Jeff Laytonc69ff402024-01-31 18:02:28 -0500613 locks_free_lease(fl);
J. Bruce Fieldse32b8ee2007-03-01 14:34:35 -0500614 return ERR_PTR(error);
Trond Myklebust75dff552006-05-07 23:02:42 -0400615 }
J. Bruce Fieldse32b8ee2007-03-01 14:34:35 -0500616 return fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
619/* Check if two locks overlap each other.
620 */
621static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
622{
623 return ((fl1->fl_end >= fl2->fl_start) &&
624 (fl2->fl_end >= fl1->fl_start));
625}
626
627/*
628 * Check whether two locks have the same owner.
629 */
Jeff Layton9bb430a2024-01-31 18:02:01 -0500630static int posix_same_owner(struct file_lock_core *fl1, struct file_lock_core *fl2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Jeff Layton9bb430a2024-01-31 18:02:01 -0500632 return fl1->flc_owner == fl2->flc_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
Jeff Layton6109c852015-01-16 15:05:57 -0500635/* Must be called with the flc_lock held! */
Jeff Layton46291722024-01-31 18:02:03 -0500636static void locks_insert_global_locks(struct file_lock_core *flc)
Jeff Layton88974692013-06-21 08:58:14 -0400637{
Peter Zijlstra7c3f6542015-06-22 14:16:34 +0200638 struct file_lock_list_struct *fll = this_cpu_ptr(&file_lock_list);
639
Peter Zijlstraaba37662015-06-22 14:16:33 +0200640 percpu_rwsem_assert_held(&file_rwsem);
641
Peter Zijlstra7c3f6542015-06-22 14:16:34 +0200642 spin_lock(&fll->lock);
Jeff Layton46291722024-01-31 18:02:03 -0500643 flc->flc_link_cpu = smp_processor_id();
644 hlist_add_head(&flc->flc_link, &fll->hlist);
Peter Zijlstra7c3f6542015-06-22 14:16:34 +0200645 spin_unlock(&fll->lock);
Jeff Layton88974692013-06-21 08:58:14 -0400646}
647
Jeff Layton6109c852015-01-16 15:05:57 -0500648/* Must be called with the flc_lock held! */
Jeff Layton46291722024-01-31 18:02:03 -0500649static void locks_delete_global_locks(struct file_lock_core *flc)
Jeff Layton88974692013-06-21 08:58:14 -0400650{
Peter Zijlstra7c3f6542015-06-22 14:16:34 +0200651 struct file_lock_list_struct *fll;
652
Peter Zijlstraaba37662015-06-22 14:16:33 +0200653 percpu_rwsem_assert_held(&file_rwsem);
654
Jeff Layton7012b022013-06-21 08:58:22 -0400655 /*
656 * Avoid taking lock if already unhashed. This is safe since this check
Jeff Layton6109c852015-01-16 15:05:57 -0500657 * is done while holding the flc_lock, and new insertions into the list
Jeff Layton7012b022013-06-21 08:58:22 -0400658 * also require that it be held.
659 */
Jeff Layton46291722024-01-31 18:02:03 -0500660 if (hlist_unhashed(&flc->flc_link))
Jeff Layton7012b022013-06-21 08:58:22 -0400661 return;
Peter Zijlstra7c3f6542015-06-22 14:16:34 +0200662
Jeff Layton46291722024-01-31 18:02:03 -0500663 fll = per_cpu_ptr(&file_lock_list, flc->flc_link_cpu);
Peter Zijlstra7c3f6542015-06-22 14:16:34 +0200664 spin_lock(&fll->lock);
Jeff Layton46291722024-01-31 18:02:03 -0500665 hlist_del_init(&flc->flc_link);
Peter Zijlstra7c3f6542015-06-22 14:16:34 +0200666 spin_unlock(&fll->lock);
Jeff Layton88974692013-06-21 08:58:14 -0400667}
668
Jeff Layton3999e492013-06-21 08:58:19 -0400669static unsigned long
Jeff Laytonad399742024-01-31 18:02:02 -0500670posix_owner_key(struct file_lock_core *flc)
Jeff Layton3999e492013-06-21 08:58:19 -0400671{
Jeff Laytonad399742024-01-31 18:02:02 -0500672 return (unsigned long) flc->flc_owner;
Jeff Layton3999e492013-06-21 08:58:19 -0400673}
674
Jeff Layton1a6c75d2024-01-31 18:02:04 -0500675static void locks_insert_global_blocked(struct file_lock_core *waiter)
Jeff Layton88974692013-06-21 08:58:14 -0400676{
Daniel Wagner663d5af2015-04-03 09:04:03 -0400677 lockdep_assert_held(&blocked_lock_lock);
678
Jeff Layton1a6c75d2024-01-31 18:02:04 -0500679 hash_add(blocked_hash, &waiter->flc_link, posix_owner_key(waiter));
Jeff Layton88974692013-06-21 08:58:14 -0400680}
681
Jeff Layton1a6c75d2024-01-31 18:02:04 -0500682static void locks_delete_global_blocked(struct file_lock_core *waiter)
Jeff Layton88974692013-06-21 08:58:14 -0400683{
Daniel Wagner663d5af2015-04-03 09:04:03 -0400684 lockdep_assert_held(&blocked_lock_lock);
685
Jeff Layton1a6c75d2024-01-31 18:02:04 -0500686 hash_del(&waiter->flc_link);
Jeff Layton88974692013-06-21 08:58:14 -0400687}
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689/* Remove waiter from blocker's block list.
690 * When blocker ends up pointing to itself then the list is empty.
Jeff Layton1c8c6012013-06-21 08:58:15 -0400691 *
Jeff Layton7b2296a2013-06-21 08:58:20 -0400692 * Must be called with blocked_lock_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 */
Jeff Layton269a6192024-01-31 18:02:09 -0500694static void __locks_unlink_block(struct file_lock_core *waiter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Jeff Layton1a62c222024-01-31 18:02:05 -0500696 locks_delete_global_blocked(waiter);
697 list_del_init(&waiter->flc_blocked_member);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
Jeff Layton1a62c222024-01-31 18:02:05 -0500700static void __locks_wake_up_blocks(struct file_lock_core *blocker)
NeilBrownad6bbd82018-11-30 10:04:08 +1100701{
Jeff Layton1a62c222024-01-31 18:02:05 -0500702 while (!list_empty(&blocker->flc_blocked_requests)) {
703 struct file_lock_core *waiter;
704 struct file_lock *fl;
NeilBrownad6bbd82018-11-30 10:04:08 +1100705
Jeff Layton1a62c222024-01-31 18:02:05 -0500706 waiter = list_first_entry(&blocker->flc_blocked_requests,
707 struct file_lock_core, flc_blocked_member);
708
709 fl = file_lock(waiter);
Jeff Layton269a6192024-01-31 18:02:09 -0500710 __locks_unlink_block(waiter);
Jeff Layton1a62c222024-01-31 18:02:05 -0500711 if ((waiter->flc_flags & (FL_POSIX | FL_FLOCK)) &&
712 fl->fl_lmops && fl->fl_lmops->lm_notify)
713 fl->fl_lmops->lm_notify(fl);
NeilBrownad6bbd82018-11-30 10:04:08 +1100714 else
Jeff Layton1a62c222024-01-31 18:02:05 -0500715 locks_wake_up(fl);
Linus Torvaldsdcf23ac2020-03-18 07:52:21 -0400716
717 /*
Jeff Layton1a62c222024-01-31 18:02:05 -0500718 * The setting of flc_blocker to NULL marks the "done"
Linus Torvaldsdcf23ac2020-03-18 07:52:21 -0400719 * point in deleting a block. Paired with acquire at the top
720 * of locks_delete_block().
721 */
Jeff Layton1a62c222024-01-31 18:02:05 -0500722 smp_store_release(&waiter->flc_blocker, NULL);
NeilBrownad6bbd82018-11-30 10:04:08 +1100723 }
724}
725
Jeff Layton269a6192024-01-31 18:02:09 -0500726static int __locks_delete_block(struct file_lock_core *waiter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
NeilBrowncb03f942018-11-30 10:04:08 +1100728 int status = -ENOENT;
729
Linus Torvaldsdcf23ac2020-03-18 07:52:21 -0400730 /*
731 * If fl_blocker is NULL, it won't be set again as this thread "owns"
732 * the lock and is the only one that might try to claim the lock.
733 *
734 * We use acquire/release to manage fl_blocker so that we can
735 * optimize away taking the blocked_lock_lock in many cases.
736 *
737 * The smp_load_acquire guarantees two things:
738 *
739 * 1/ that fl_blocked_requests can be tested locklessly. If something
740 * was recently added to that list it must have been in a locked region
741 * *before* the locked region when fl_blocker was set to NULL.
742 *
743 * 2/ that no other thread is accessing 'waiter', so it is safe to free
744 * it. __locks_wake_up_blocks is careful not to touch waiter after
745 * fl_blocker is released.
746 *
747 * If a lockless check of fl_blocker shows it to be NULL, we know that
748 * no new locks can be inserted into its fl_blocked_requests list, and
749 * can avoid doing anything further if the list is empty.
750 */
Jeff Laytone8a166c2024-01-31 18:02:08 -0500751 if (!smp_load_acquire(&waiter->flc_blocker) &&
752 list_empty(&waiter->flc_blocked_requests))
Linus Torvaldsdcf23ac2020-03-18 07:52:21 -0400753 return status;
754
Jeff Layton7b2296a2013-06-21 08:58:20 -0400755 spin_lock(&blocked_lock_lock);
Jeff Laytone8a166c2024-01-31 18:02:08 -0500756 if (waiter->flc_blocker)
NeilBrowncb03f942018-11-30 10:04:08 +1100757 status = 0;
Jeff Laytone8a166c2024-01-31 18:02:08 -0500758 __locks_wake_up_blocks(waiter);
Jeff Layton269a6192024-01-31 18:02:09 -0500759 __locks_unlink_block(waiter);
Linus Torvaldsdcf23ac2020-03-18 07:52:21 -0400760
761 /*
762 * The setting of fl_blocker to NULL marks the "done" point in deleting
763 * a block. Paired with acquire at the top of this function.
764 */
Jeff Laytone8a166c2024-01-31 18:02:08 -0500765 smp_store_release(&waiter->flc_blocker, NULL);
Jeff Layton7b2296a2013-06-21 08:58:20 -0400766 spin_unlock(&blocked_lock_lock);
NeilBrowncb03f942018-11-30 10:04:08 +1100767 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
Jeff Layton269a6192024-01-31 18:02:09 -0500769
770/**
771 * locks_delete_block - stop waiting for a file lock
772 * @waiter: the lock which was waiting
773 *
774 * lockd/nfsd need to disconnect the lock while working on it.
775 */
776int locks_delete_block(struct file_lock *waiter)
777{
778 return __locks_delete_block(&waiter->c);
779}
NeilBrowncb03f942018-11-30 10:04:08 +1100780EXPORT_SYMBOL(locks_delete_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782/* Insert waiter into blocker's block list.
783 * We use a circular list so that processes can be easily woken up in
784 * the order they blocked. The documentation doesn't require this but
785 * it seems like the reasonable thing to do.
Jeff Layton1c8c6012013-06-21 08:58:15 -0400786 *
Jeff Layton6109c852015-01-16 15:05:57 -0500787 * Must be called with both the flc_lock and blocked_lock_lock held. The
NeilBrownada5c1d2018-11-30 10:04:08 +1100788 * fl_blocked_requests list itself is protected by the blocked_lock_lock,
789 * but by ensuring that the flc_lock is also held on insertions we can avoid
790 * taking the blocked_lock_lock in some cases when we see that the
791 * fl_blocked_requests list is empty.
NeilBrownfd7732e2018-11-30 10:04:08 +1100792 *
793 * Rather than just adding to the list, we check for conflicts with any existing
794 * waiters, and add beneath any waiter that blocks the new waiter.
795 * Thus wakeups don't happen until needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 */
Jeff Layton269a6192024-01-31 18:02:09 -0500797static void __locks_insert_block(struct file_lock_core *blocker,
798 struct file_lock_core *waiter,
Jeff Laytonb6be3712024-01-31 18:02:06 -0500799 bool conflict(struct file_lock_core *,
800 struct file_lock_core *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Jeff Laytonb6be3712024-01-31 18:02:06 -0500802 struct file_lock_core *flc;
NeilBrownfd7732e2018-11-30 10:04:08 +1100803
Jeff Laytonb6be3712024-01-31 18:02:06 -0500804 BUG_ON(!list_empty(&waiter->flc_blocked_member));
NeilBrownfd7732e2018-11-30 10:04:08 +1100805new_blocker:
Jeff Laytonb6be3712024-01-31 18:02:06 -0500806 list_for_each_entry(flc, &blocker->flc_blocked_requests, flc_blocked_member)
807 if (conflict(flc, waiter)) {
808 blocker = flc;
NeilBrownfd7732e2018-11-30 10:04:08 +1100809 goto new_blocker;
810 }
Jeff Laytonb6aaba52024-01-31 18:02:07 -0500811 waiter->flc_blocker = blocker;
Jeff Laytonb6be3712024-01-31 18:02:06 -0500812 list_add_tail(&waiter->flc_blocked_member,
813 &blocker->flc_blocked_requests);
NeilBrown5946c432018-11-30 10:04:08 +1100814
Jeff Layton14786d92024-02-18 08:33:28 -0500815 if ((blocker->flc_flags & (FL_POSIX|FL_OFDLCK)) == FL_POSIX)
Jeff Laytonb6be3712024-01-31 18:02:06 -0500816 locks_insert_global_blocked(waiter);
817
818 /* The requests in waiter->flc_blocked are known to conflict with
NeilBrown5946c432018-11-30 10:04:08 +1100819 * waiter, but might not conflict with blocker, or the requests
820 * and lock which block it. So they all need to be woken.
821 */
Jeff Laytonb6be3712024-01-31 18:02:06 -0500822 __locks_wake_up_blocks(waiter);
Jeff Layton1c8c6012013-06-21 08:58:15 -0400823}
824
Jeff Layton6109c852015-01-16 15:05:57 -0500825/* Must be called with flc_lock held. */
Jeff Layton269a6192024-01-31 18:02:09 -0500826static void locks_insert_block(struct file_lock_core *blocker,
827 struct file_lock_core *waiter,
Jeff Laytonb6be3712024-01-31 18:02:06 -0500828 bool conflict(struct file_lock_core *,
829 struct file_lock_core *))
Jeff Layton1c8c6012013-06-21 08:58:15 -0400830{
Jeff Layton7b2296a2013-06-21 08:58:20 -0400831 spin_lock(&blocked_lock_lock);
NeilBrownfd7732e2018-11-30 10:04:08 +1100832 __locks_insert_block(blocker, waiter, conflict);
Jeff Layton7b2296a2013-06-21 08:58:20 -0400833 spin_unlock(&blocked_lock_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
Jeff Layton1cb36012013-06-21 08:58:12 -0400836/*
837 * Wake up processes blocked waiting for blocker.
838 *
Jeff Layton6109c852015-01-16 15:05:57 -0500839 * Must be called with the inode->flc_lock held!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 */
Jeff Layton347d49f2024-01-31 18:02:11 -0500841static void locks_wake_up_blocks(struct file_lock_core *blocker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Jeff Layton4e8c7652013-06-21 08:58:16 -0400843 /*
844 * Avoid taking global lock if list is empty. This is safe since new
Jeff Layton6109c852015-01-16 15:05:57 -0500845 * blocked requests are only added to the list under the flc_lock, and
NeilBrownada5c1d2018-11-30 10:04:08 +1100846 * the flc_lock is always held here. Note that removal from the
847 * fl_blocked_requests list does not require the flc_lock, so we must
848 * recheck list_empty() after acquiring the blocked_lock_lock.
Jeff Layton4e8c7652013-06-21 08:58:16 -0400849 */
Jeff Layton347d49f2024-01-31 18:02:11 -0500850 if (list_empty(&blocker->flc_blocked_requests))
Jeff Layton4e8c7652013-06-21 08:58:16 -0400851 return;
852
Jeff Layton7b2296a2013-06-21 08:58:20 -0400853 spin_lock(&blocked_lock_lock);
Jeff Layton347d49f2024-01-31 18:02:11 -0500854 __locks_wake_up_blocks(blocker);
Jeff Layton7b2296a2013-06-21 08:58:20 -0400855 spin_unlock(&blocked_lock_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
Jeff Layton5263e312015-01-16 15:05:55 -0500858static void
Jeff Layton7c185092024-01-31 18:02:12 -0500859locks_insert_lock_ctx(struct file_lock_core *fl, struct list_head *before)
Jeff Layton5263e312015-01-16 15:05:55 -0500860{
Jeff Layton7c185092024-01-31 18:02:12 -0500861 list_add_tail(&fl->flc_list, before);
862 locks_insert_global_locks(fl);
Jeff Layton5263e312015-01-16 15:05:55 -0500863}
864
Jeff Layton8634b512015-01-16 15:05:55 -0500865static void
Jeff Layton7c185092024-01-31 18:02:12 -0500866locks_unlink_lock_ctx(struct file_lock_core *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Jeff Layton7c185092024-01-31 18:02:12 -0500868 locks_delete_global_locks(fl);
869 list_del_init(&fl->flc_list);
870 locks_wake_up_blocks(fl);
Jeff Layton24cbe782014-02-03 12:13:06 -0500871}
872
Jeff Layton5263e312015-01-16 15:05:55 -0500873static void
Jeff Layton7c185092024-01-31 18:02:12 -0500874locks_delete_lock_ctx(struct file_lock_core *fl, struct list_head *dispose)
Jeff Layton5263e312015-01-16 15:05:55 -0500875{
Jeff Laytone084c1b2015-02-16 14:32:03 -0500876 locks_unlink_lock_ctx(fl);
Jeff Layton8634b512015-01-16 15:05:55 -0500877 if (dispose)
Jeff Layton7c185092024-01-31 18:02:12 -0500878 list_add(&fl->flc_list, dispose);
Jeff Layton8634b512015-01-16 15:05:55 -0500879 else
Jeff Layton7c185092024-01-31 18:02:12 -0500880 locks_free_lock(file_lock(fl));
Jeff Layton5263e312015-01-16 15:05:55 -0500881}
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883/* Determine if lock sys_fl blocks lock caller_fl. Common functionality
884 * checks for shared/exclusive status of overlapping locks.
885 */
Jeff Laytonb6be3712024-01-31 18:02:06 -0500886static bool locks_conflict(struct file_lock_core *caller_flc,
887 struct file_lock_core *sys_flc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Jeff Laytonb6be3712024-01-31 18:02:06 -0500889 if (sys_flc->flc_type == F_WRLCK)
NeilBrownc0e15902018-11-30 10:04:08 +1100890 return true;
Jeff Laytonb6be3712024-01-31 18:02:06 -0500891 if (caller_flc->flc_type == F_WRLCK)
NeilBrownc0e15902018-11-30 10:04:08 +1100892 return true;
893 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
896/* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
897 * checking before calling the locks_conflict().
898 */
Jeff Laytonb6be3712024-01-31 18:02:06 -0500899static bool posix_locks_conflict(struct file_lock_core *caller_flc,
900 struct file_lock_core *sys_flc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Jeff Laytonb6be3712024-01-31 18:02:06 -0500902 struct file_lock *caller_fl = file_lock(caller_flc);
903 struct file_lock *sys_fl = file_lock(sys_flc);
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 /* POSIX locks owned by the same process do not conflict with
906 * each other.
907 */
Jeff Laytonb6be3712024-01-31 18:02:06 -0500908 if (posix_same_owner(caller_flc, sys_flc))
NeilBrownc0e15902018-11-30 10:04:08 +1100909 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 /* Check whether they overlap */
912 if (!locks_overlap(caller_fl, sys_fl))
NeilBrownc0e15902018-11-30 10:04:08 +1100913 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Jeff Laytonb6be3712024-01-31 18:02:06 -0500915 return locks_conflict(caller_flc, sys_flc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
Stas Sergeev6c9007f2023-06-22 21:52:23 +0500918/* Determine if lock sys_fl blocks lock caller_fl. Used on xx_GETLK
919 * path so checks for additional GETLK-specific things like F_UNLCK.
920 */
921static bool posix_test_locks_conflict(struct file_lock *caller_fl,
922 struct file_lock *sys_fl)
923{
Jeff Laytonb6be3712024-01-31 18:02:06 -0500924 struct file_lock_core *caller = &caller_fl->c;
925 struct file_lock_core *sys = &sys_fl->c;
926
Stas Sergeev6c9007f2023-06-22 21:52:23 +0500927 /* F_UNLCK checks any locks on the same fd. */
Jeff Layton75cabec2024-01-31 18:01:45 -0500928 if (lock_is_unlock(caller_fl)) {
Jeff Laytonb6be3712024-01-31 18:02:06 -0500929 if (!posix_same_owner(caller, sys))
Stas Sergeev6c9007f2023-06-22 21:52:23 +0500930 return false;
931 return locks_overlap(caller_fl, sys_fl);
932 }
Jeff Laytonb6be3712024-01-31 18:02:06 -0500933 return posix_locks_conflict(caller, sys);
Stas Sergeev6c9007f2023-06-22 21:52:23 +0500934}
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936/* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
937 * checking before calling the locks_conflict().
938 */
Jeff Laytonb6be3712024-01-31 18:02:06 -0500939static bool flock_locks_conflict(struct file_lock_core *caller_flc,
940 struct file_lock_core *sys_flc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
942 /* FLOCK locks referring to the same filp do not conflict with
943 * each other.
944 */
Jeff Laytonb6be3712024-01-31 18:02:06 -0500945 if (caller_flc->flc_file == sys_flc->flc_file)
NeilBrownc0e15902018-11-30 10:04:08 +1100946 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Jeff Laytonb6be3712024-01-31 18:02:06 -0500948 return locks_conflict(caller_flc, sys_flc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950
J. Bruce Fields6d34ac12007-05-11 16:09:32 -0400951void
Marc Eshel9d6a8c52007-02-21 00:55:18 -0500952posix_test_lock(struct file *filp, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
954 struct file_lock *cfl;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500955 struct file_lock_context *ctx;
Jeff Laytonc65454a2022-11-25 08:48:37 -0500956 struct inode *inode = file_inode(filp);
Dai Ngo2443da22022-05-02 14:19:25 -0700957 void *owner;
958 void (*func)(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Jeff Layton401a8b82022-11-16 09:02:30 -0500960 ctx = locks_inode_context(inode);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500961 if (!ctx || list_empty_careful(&ctx->flc_posix)) {
Jeff Layton4ca52f52024-01-31 18:01:59 -0500962 fl->c.flc_type = F_UNLCK;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500963 return;
964 }
965
Dai Ngo2443da22022-05-02 14:19:25 -0700966retry:
Jeff Layton6109c852015-01-16 15:05:57 -0500967 spin_lock(&ctx->flc_lock);
Jeff Layton4ca52f52024-01-31 18:01:59 -0500968 list_for_each_entry(cfl, &ctx->flc_posix, c.flc_list) {
Stas Sergeev6c9007f2023-06-22 21:52:23 +0500969 if (!posix_test_locks_conflict(fl, cfl))
Dai Ngo2443da22022-05-02 14:19:25 -0700970 continue;
971 if (cfl->fl_lmops && cfl->fl_lmops->lm_lock_expirable
972 && (*cfl->fl_lmops->lm_lock_expirable)(cfl)) {
973 owner = cfl->fl_lmops->lm_mod_owner;
974 func = cfl->fl_lmops->lm_expire_lock;
975 __module_get(owner);
976 spin_unlock(&ctx->flc_lock);
977 (*func)();
978 module_put(owner);
979 goto retry;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500980 }
Dai Ngo2443da22022-05-02 14:19:25 -0700981 locks_copy_conflock(fl, cfl);
982 goto out;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500983 }
Jeff Layton4ca52f52024-01-31 18:01:59 -0500984 fl->c.flc_type = F_UNLCK;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500985out:
Jeff Layton6109c852015-01-16 15:05:57 -0500986 spin_unlock(&ctx->flc_lock);
J. Bruce Fields6d34ac12007-05-11 16:09:32 -0400987 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989EXPORT_SYMBOL(posix_test_lock);
990
J. Bruce Fieldsb5331842007-10-26 18:05:40 -0400991/*
992 * Deadlock detection:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 *
J. Bruce Fieldsb5331842007-10-26 18:05:40 -0400994 * We attempt to detect deadlocks that are due purely to posix file
995 * locks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 *
J. Bruce Fieldsb5331842007-10-26 18:05:40 -0400997 * We assume that a task can be waiting for at most one lock at a time.
998 * So for any acquired lock, the process holding that lock may be
999 * waiting on at most one other lock. That lock in turns may be held by
1000 * someone waiting for at most one other lock. Given a requested lock
1001 * caller_fl which is about to wait for a conflicting lock block_fl, we
1002 * follow this chain of waiters to ensure we are not about to create a
1003 * cycle.
J. Bruce Fields97855b42007-10-30 11:20:02 -04001004 *
J. Bruce Fieldsb5331842007-10-26 18:05:40 -04001005 * Since we do this before we ever put a process to sleep on a lock, we
1006 * are ensured that there is never a cycle; that is what guarantees that
1007 * the while() loop in posix_locks_deadlock() eventually completes.
1008 *
1009 * Note: the above assumption may not be true when handling lock
1010 * requests from a broken NFS client. It may also fail in the presence
1011 * of tasks (such as posix threads) sharing the same open file table.
J. Bruce Fieldsb5331842007-10-26 18:05:40 -04001012 * To handle those cases, we just bail out after a few iterations.
Jeff Layton57b65322014-02-03 12:13:09 -05001013 *
Jeff Laytoncff2fce2014-04-22 08:24:32 -04001014 * For FL_OFDLCK locks, the owner is the filp, not the files_struct.
Jeff Layton57b65322014-02-03 12:13:09 -05001015 * Because the owner is not even nominally tied to a thread of
1016 * execution, the deadlock detection below can't reasonably work well. Just
1017 * skip it for those.
1018 *
Jeff Laytoncff2fce2014-04-22 08:24:32 -04001019 * In principle, we could do a more limited deadlock detection on FL_OFDLCK
Jeff Layton57b65322014-02-03 12:13:09 -05001020 * locks that just checks for the case where two tasks are attempting to
1021 * upgrade from read to write locks on the same inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 */
J. Bruce Fields97855b42007-10-30 11:20:02 -04001023
1024#define MAX_DEADLK_ITERATIONS 10
1025
Jeff Laytonb6be3712024-01-31 18:02:06 -05001026/* Find a lock that the owner of the given @blocker is blocking on. */
1027static struct file_lock_core *what_owner_is_waiting_for(struct file_lock_core *blocker)
J. Bruce Fieldsb5331842007-10-26 18:05:40 -04001028{
Jeff Laytonb6be3712024-01-31 18:02:06 -05001029 struct file_lock_core *flc;
J. Bruce Fieldsb5331842007-10-26 18:05:40 -04001030
Jeff Laytonb6be3712024-01-31 18:02:06 -05001031 hash_for_each_possible(blocked_hash, flc, flc_link, posix_owner_key(blocker)) {
1032 if (posix_same_owner(flc, blocker)) {
1033 while (flc->flc_blocker)
Jeff Laytonb6aaba52024-01-31 18:02:07 -05001034 flc = flc->flc_blocker;
Jeff Laytonb6be3712024-01-31 18:02:06 -05001035 return flc;
NeilBrown5946c432018-11-30 10:04:08 +11001036 }
J. Bruce Fieldsb5331842007-10-26 18:05:40 -04001037 }
1038 return NULL;
1039}
1040
Jeff Layton7b2296a2013-06-21 08:58:20 -04001041/* Must be called with the blocked_lock_lock held! */
Jeff Laytonb6be3712024-01-31 18:02:06 -05001042static bool posix_locks_deadlock(struct file_lock *caller_fl,
1043 struct file_lock *block_fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Jeff Laytonb6be3712024-01-31 18:02:06 -05001045 struct file_lock_core *caller = &caller_fl->c;
1046 struct file_lock_core *blocker = &block_fl->c;
J. Bruce Fields97855b42007-10-30 11:20:02 -04001047 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Daniel Wagner663d5af2015-04-03 09:04:03 -04001049 lockdep_assert_held(&blocked_lock_lock);
1050
Jeff Layton57b65322014-02-03 12:13:09 -05001051 /*
1052 * This deadlock detector can't reasonably detect deadlocks with
Jeff Laytoncff2fce2014-04-22 08:24:32 -04001053 * FL_OFDLCK locks, since they aren't owned by a process, per-se.
Jeff Layton57b65322014-02-03 12:13:09 -05001054 */
Jeff Laytonb6be3712024-01-31 18:02:06 -05001055 if (caller->flc_flags & FL_OFDLCK)
1056 return false;
Jeff Layton57b65322014-02-03 12:13:09 -05001057
Jeff Laytonb6be3712024-01-31 18:02:06 -05001058 while ((blocker = what_owner_is_waiting_for(blocker))) {
J. Bruce Fieldsb5331842007-10-26 18:05:40 -04001059 if (i++ > MAX_DEADLK_ITERATIONS)
Jeff Laytonb6be3712024-01-31 18:02:06 -05001060 return false;
1061 if (posix_same_owner(caller, blocker))
1062 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 }
Jeff Laytonb6be3712024-01-31 18:02:06 -05001064 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067/* Try to create a FLOCK lock on filp. We always insert new FLOCK locks
J. Bruce Fields02888f42007-09-12 15:45:07 -04001068 * after any leases, but before any posix locks.
Trond Myklebustf475ae92006-06-29 16:38:32 -04001069 *
1070 * Note that if called with an FL_EXISTS argument, the caller may determine
1071 * whether or not a lock was successfully freed by testing the return
1072 * value for -ENOENT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 */
Jeff Laytonbcd7f78d2015-07-11 06:43:02 -04001074static int flock_lock_inode(struct inode *inode, struct file_lock *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
Trond Myklebust993dfa82006-03-31 02:30:55 -08001076 struct file_lock *new_fl = NULL;
Jeff Layton5263e312015-01-16 15:05:55 -05001077 struct file_lock *fl;
1078 struct file_lock_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 int error = 0;
Jeff Layton5263e312015-01-16 15:05:55 -05001080 bool found = false;
Jeff Laytoned9814d2014-08-11 14:20:31 -04001081 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Jeff Layton4ca52f52024-01-31 18:01:59 -05001083 ctx = locks_get_lock_context(inode, request->c.flc_type);
Jeff Layton5c1c6692015-04-03 09:04:03 -04001084 if (!ctx) {
Jeff Layton4ca52f52024-01-31 18:01:59 -05001085 if (request->c.flc_type != F_UNLCK)
Jeff Layton5c1c6692015-04-03 09:04:03 -04001086 return -ENOMEM;
Jeff Layton4ca52f52024-01-31 18:01:59 -05001087 return (request->c.flc_flags & FL_EXISTS) ? -ENOENT : 0;
Jeff Layton5c1c6692015-04-03 09:04:03 -04001088 }
Jeff Layton5263e312015-01-16 15:05:55 -05001089
Jeff Layton4ca52f52024-01-31 18:01:59 -05001090 if (!(request->c.flc_flags & FL_ACCESS) && (request->c.flc_type != F_UNLCK)) {
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001091 new_fl = locks_alloc_lock();
1092 if (!new_fl)
1093 return -ENOMEM;
1094 }
1095
Peter Zijlstra02e525b22019-02-21 15:38:40 +01001096 percpu_down_read(&file_rwsem);
Jeff Layton6109c852015-01-16 15:05:57 -05001097 spin_lock(&ctx->flc_lock);
Jeff Layton4ca52f52024-01-31 18:01:59 -05001098 if (request->c.flc_flags & FL_ACCESS)
Trond Myklebustf07f18d2006-06-29 16:38:37 -04001099 goto find_conflict;
Pavel Emelyanov84d535a2007-09-11 16:38:13 +04001100
Jeff Layton4ca52f52024-01-31 18:01:59 -05001101 list_for_each_entry(fl, &ctx->flc_flock, c.flc_list) {
1102 if (request->c.flc_file != fl->c.flc_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 continue;
Jeff Layton4ca52f52024-01-31 18:01:59 -05001104 if (request->c.flc_type == fl->c.flc_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 goto out;
Jeff Layton5263e312015-01-16 15:05:55 -05001106 found = true;
Jeff Layton7c185092024-01-31 18:02:12 -05001107 locks_delete_lock_ctx(&fl->c, &dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 break;
1109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Jeff Layton75cabec2024-01-31 18:01:45 -05001111 if (lock_is_unlock(request)) {
Jeff Layton4ca52f52024-01-31 18:01:59 -05001112 if ((request->c.flc_flags & FL_EXISTS) && !found)
Trond Myklebustf475ae92006-06-29 16:38:32 -04001113 error = -ENOENT;
Trond Myklebust993dfa82006-03-31 02:30:55 -08001114 goto out;
Trond Myklebustf475ae92006-06-29 16:38:32 -04001115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Trond Myklebustf07f18d2006-06-29 16:38:37 -04001117find_conflict:
Jeff Layton4ca52f52024-01-31 18:01:59 -05001118 list_for_each_entry(fl, &ctx->flc_flock, c.flc_list) {
Jeff Laytonb6be3712024-01-31 18:02:06 -05001119 if (!flock_locks_conflict(&request->c, &fl->c))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 continue;
1121 error = -EAGAIN;
Jeff Layton4ca52f52024-01-31 18:01:59 -05001122 if (!(request->c.flc_flags & FL_SLEEP))
Miklos Szeredibde74e42008-07-25 01:48:57 -07001123 goto out;
1124 error = FILE_LOCK_DEFERRED;
Jeff Layton269a6192024-01-31 18:02:09 -05001125 locks_insert_block(&fl->c, &request->c, flock_locks_conflict);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 goto out;
1127 }
Jeff Layton4ca52f52024-01-31 18:01:59 -05001128 if (request->c.flc_flags & FL_ACCESS)
Trond Myklebustf07f18d2006-06-29 16:38:37 -04001129 goto out;
Trond Myklebust993dfa82006-03-31 02:30:55 -08001130 locks_copy_lock(new_fl, request);
NeilBrown5946c432018-11-30 10:04:08 +11001131 locks_move_blocks(new_fl, request);
Jeff Layton7c185092024-01-31 18:02:12 -05001132 locks_insert_lock_ctx(&new_fl->c, &ctx->flc_flock);
Trond Myklebust993dfa82006-03-31 02:30:55 -08001133 new_fl = NULL;
Kirill Korotaev9cedc192006-06-14 17:59:35 +04001134 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136out:
Jeff Layton6109c852015-01-16 15:05:57 -05001137 spin_unlock(&ctx->flc_lock);
Peter Zijlstra02e525b22019-02-21 15:38:40 +01001138 percpu_up_read(&file_rwsem);
Trond Myklebust993dfa82006-03-31 02:30:55 -08001139 if (new_fl)
1140 locks_free_lock(new_fl);
Jeff Laytoned9814d2014-08-11 14:20:31 -04001141 locks_dispose_list(&dispose);
Jeff Laytonc883da32018-07-30 07:54:56 -04001142 trace_flock_lock_inode(inode, request, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 return error;
1144}
1145
Jeff Laytonb4d629a2016-01-07 18:27:42 -05001146static int posix_lock_inode(struct inode *inode, struct file_lock *request,
1147 struct file_lock *conflock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001149 struct file_lock *fl, *tmp;
Miklos Szeredi39005d02006-06-23 02:05:10 -07001150 struct file_lock *new_fl = NULL;
1151 struct file_lock *new_fl2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 struct file_lock *left = NULL;
1153 struct file_lock *right = NULL;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001154 struct file_lock_context *ctx;
Jeff Laytonb9746ef2013-06-21 08:58:13 -04001155 int error;
1156 bool added = false;
Jeff Laytoned9814d2014-08-11 14:20:31 -04001157 LIST_HEAD(dispose);
Dai Ngo2443da22022-05-02 14:19:25 -07001158 void *owner;
1159 void (*func)(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Jeff Layton4ca52f52024-01-31 18:01:59 -05001161 ctx = locks_get_lock_context(inode, request->c.flc_type);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001162 if (!ctx)
Jeff Layton75cabec2024-01-31 18:01:45 -05001163 return lock_is_unlock(request) ? 0 : -ENOMEM;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 /*
1166 * We may need two file_lock structures for this operation,
1167 * so we get them in advance to avoid races.
Miklos Szeredi39005d02006-06-23 02:05:10 -07001168 *
1169 * In some cases we can be sure, that no new locks will be needed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 */
Jeff Layton4ca52f52024-01-31 18:01:59 -05001171 if (!(request->c.flc_flags & FL_ACCESS) &&
1172 (request->c.flc_type != F_UNLCK ||
Miklos Szeredi39005d02006-06-23 02:05:10 -07001173 request->fl_start != 0 || request->fl_end != OFFSET_MAX)) {
1174 new_fl = locks_alloc_lock();
1175 new_fl2 = locks_alloc_lock();
1176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Dai Ngo2443da22022-05-02 14:19:25 -07001178retry:
Peter Zijlstra02e525b22019-02-21 15:38:40 +01001179 percpu_down_read(&file_rwsem);
Jeff Layton6109c852015-01-16 15:05:57 -05001180 spin_lock(&ctx->flc_lock);
Jeff Layton1cb36012013-06-21 08:58:12 -04001181 /*
1182 * New lock request. Walk all POSIX locks and look for conflicts. If
1183 * there are any, either return error or put the request on the
Jeff Layton48f74182013-06-21 08:58:18 -04001184 * blocker's list of waiters and the global blocked_hash.
Jeff Layton1cb36012013-06-21 08:58:12 -04001185 */
Jeff Layton4ca52f52024-01-31 18:01:59 -05001186 if (request->c.flc_type != F_UNLCK) {
1187 list_for_each_entry(fl, &ctx->flc_posix, c.flc_list) {
Jeff Laytonb6be3712024-01-31 18:02:06 -05001188 if (!posix_locks_conflict(&request->c, &fl->c))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 continue;
Dai Ngo2443da22022-05-02 14:19:25 -07001190 if (fl->fl_lmops && fl->fl_lmops->lm_lock_expirable
1191 && (*fl->fl_lmops->lm_lock_expirable)(fl)) {
1192 owner = fl->fl_lmops->lm_mod_owner;
1193 func = fl->fl_lmops->lm_expire_lock;
1194 __module_get(owner);
1195 spin_unlock(&ctx->flc_lock);
1196 percpu_up_read(&file_rwsem);
1197 (*func)();
1198 module_put(owner);
1199 goto retry;
1200 }
Andy Adamson5842add2006-03-26 01:37:26 -08001201 if (conflock)
Kinglong Mee3fe0fff12014-08-22 10:18:42 -04001202 locks_copy_conflock(conflock, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 error = -EAGAIN;
Jeff Layton4ca52f52024-01-31 18:01:59 -05001204 if (!(request->c.flc_flags & FL_SLEEP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 goto out;
Jeff Layton1c8c6012013-06-21 08:58:15 -04001206 /*
1207 * Deadlock detection and insertion into the blocked
1208 * locks list must be done while holding the same lock!
1209 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 error = -EDEADLK;
Jeff Layton7b2296a2013-06-21 08:58:20 -04001211 spin_lock(&blocked_lock_lock);
Jeff Layton945ab8f2019-03-25 08:15:14 -04001212 /*
1213 * Ensure that we don't find any locks blocked on this
1214 * request during deadlock detection.
1215 */
Jeff Layton1a62c222024-01-31 18:02:05 -05001216 __locks_wake_up_blocks(&request->c);
Jeff Layton1c8c6012013-06-21 08:58:15 -04001217 if (likely(!posix_locks_deadlock(request, fl))) {
1218 error = FILE_LOCK_DEFERRED;
Jeff Layton269a6192024-01-31 18:02:09 -05001219 __locks_insert_block(&fl->c, &request->c,
NeilBrownfd7732e2018-11-30 10:04:08 +11001220 posix_locks_conflict);
Jeff Layton1c8c6012013-06-21 08:58:15 -04001221 }
Jeff Layton7b2296a2013-06-21 08:58:20 -04001222 spin_unlock(&blocked_lock_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 goto out;
NeilBrown7bbd1fc2018-11-30 10:04:08 +11001224 }
1225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 /* If we're just looking for a conflict, we're done. */
1228 error = 0;
Jeff Layton4ca52f52024-01-31 18:01:59 -05001229 if (request->c.flc_flags & FL_ACCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 goto out;
1231
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001232 /* Find the first old lock with the same owner as the new lock */
Jeff Layton4ca52f52024-01-31 18:01:59 -05001233 list_for_each_entry(fl, &ctx->flc_posix, c.flc_list) {
Jeff Layton9bb430a2024-01-31 18:02:01 -05001234 if (posix_same_owner(&request->c, &fl->c))
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001235 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
1237
Jeff Layton1cb36012013-06-21 08:58:12 -04001238 /* Process locks with this owner. */
Jeff Layton4ca52f52024-01-31 18:01:59 -05001239 list_for_each_entry_safe_from(fl, tmp, &ctx->flc_posix, c.flc_list) {
Jeff Layton9bb430a2024-01-31 18:02:01 -05001240 if (!posix_same_owner(&request->c, &fl->c))
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001241 break;
1242
1243 /* Detect adjacent or overlapping regions (if same lock type) */
Jeff Layton4ca52f52024-01-31 18:01:59 -05001244 if (request->c.flc_type == fl->c.flc_type) {
Olaf Kirch449231d2005-08-25 16:25:35 -07001245 /* In all comparisons of start vs end, use
1246 * "start - 1" rather than "end + 1". If end
1247 * is OFFSET_MAX, end + 1 will become negative.
1248 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (fl->fl_end < request->fl_start - 1)
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001250 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 /* If the next lock in the list has entirely bigger
1252 * addresses than the new one, insert the lock here.
1253 */
Olaf Kirch449231d2005-08-25 16:25:35 -07001254 if (fl->fl_start - 1 > request->fl_end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 break;
1256
1257 /* If we come here, the new and old lock are of the
1258 * same type and adjacent or overlapping. Make one
1259 * lock yielding from the lower start address of both
1260 * locks to the higher end address.
1261 */
1262 if (fl->fl_start > request->fl_start)
1263 fl->fl_start = request->fl_start;
1264 else
1265 request->fl_start = fl->fl_start;
1266 if (fl->fl_end < request->fl_end)
1267 fl->fl_end = request->fl_end;
1268 else
1269 request->fl_end = fl->fl_end;
1270 if (added) {
Jeff Layton7c185092024-01-31 18:02:12 -05001271 locks_delete_lock_ctx(&fl->c, &dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 continue;
1273 }
1274 request = fl;
Jeff Laytonb9746ef2013-06-21 08:58:13 -04001275 added = true;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001276 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 /* Processing for different lock types is a bit
1278 * more complex.
1279 */
1280 if (fl->fl_end < request->fl_start)
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001281 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 if (fl->fl_start > request->fl_end)
1283 break;
Jeff Layton75cabec2024-01-31 18:01:45 -05001284 if (lock_is_unlock(request))
Jeff Laytonb9746ef2013-06-21 08:58:13 -04001285 added = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (fl->fl_start < request->fl_start)
1287 left = fl;
1288 /* If the next lock in the list has a higher end
1289 * address than the new one, insert the new one here.
1290 */
1291 if (fl->fl_end > request->fl_end) {
1292 right = fl;
1293 break;
1294 }
1295 if (fl->fl_start >= request->fl_start) {
1296 /* The new lock completely replaces an old
1297 * one (This may happen several times).
1298 */
1299 if (added) {
Jeff Layton7c185092024-01-31 18:02:12 -05001300 locks_delete_lock_ctx(&fl->c, &dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 continue;
1302 }
Jeff Laytonb84d49f2014-08-12 08:03:49 -04001303 /*
1304 * Replace the old lock with new_fl, and
1305 * remove the old one. It's safe to do the
1306 * insert here since we know that we won't be
1307 * using new_fl later, and that the lock is
1308 * just replacing an existing lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 */
Jeff Laytonb84d49f2014-08-12 08:03:49 -04001310 error = -ENOLCK;
1311 if (!new_fl)
1312 goto out;
1313 locks_copy_lock(new_fl, request);
yangerkun5ef15962020-06-01 17:16:16 +08001314 locks_move_blocks(new_fl, request);
Jeff Laytonb84d49f2014-08-12 08:03:49 -04001315 request = new_fl;
1316 new_fl = NULL;
Jeff Layton7c185092024-01-31 18:02:12 -05001317 locks_insert_lock_ctx(&request->c,
Jeff Layton4ca52f52024-01-31 18:01:59 -05001318 &fl->c.flc_list);
Jeff Layton7c185092024-01-31 18:02:12 -05001319 locks_delete_lock_ctx(&fl->c, &dispose);
Jeff Laytonb9746ef2013-06-21 08:58:13 -04001320 added = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
1322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 }
1324
Miklos Szeredi0d9a4902006-06-23 02:05:09 -07001325 /*
Jeff Layton1cb36012013-06-21 08:58:12 -04001326 * The above code only modifies existing locks in case of merging or
1327 * replacing. If new lock(s) need to be inserted all modifications are
1328 * done below this, so it's safe yet to bail out.
Miklos Szeredi0d9a4902006-06-23 02:05:09 -07001329 */
1330 error = -ENOLCK; /* "no luck" */
1331 if (right && left == right && !new_fl2)
1332 goto out;
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 error = 0;
1335 if (!added) {
Jeff Layton75cabec2024-01-31 18:01:45 -05001336 if (lock_is_unlock(request)) {
Jeff Layton4ca52f52024-01-31 18:01:59 -05001337 if (request->c.flc_flags & FL_EXISTS)
Trond Myklebustf475ae92006-06-29 16:38:32 -04001338 error = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 goto out;
Trond Myklebustf475ae92006-06-29 16:38:32 -04001340 }
Miklos Szeredi0d9a4902006-06-23 02:05:09 -07001341
1342 if (!new_fl) {
1343 error = -ENOLCK;
1344 goto out;
1345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 locks_copy_lock(new_fl, request);
NeilBrown5946c432018-11-30 10:04:08 +11001347 locks_move_blocks(new_fl, request);
Jeff Layton7c185092024-01-31 18:02:12 -05001348 locks_insert_lock_ctx(&new_fl->c, &fl->c.flc_list);
Jeff Layton2e2f756f2015-02-17 17:08:23 -05001349 fl = new_fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 new_fl = NULL;
1351 }
1352 if (right) {
1353 if (left == right) {
1354 /* The new lock breaks the old one in two pieces,
1355 * so we have to use the second new lock.
1356 */
1357 left = new_fl2;
1358 new_fl2 = NULL;
1359 locks_copy_lock(left, right);
Jeff Layton7c185092024-01-31 18:02:12 -05001360 locks_insert_lock_ctx(&left->c, &fl->c.flc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 }
1362 right->fl_start = request->fl_end + 1;
Jeff Layton347d49f2024-01-31 18:02:11 -05001363 locks_wake_up_blocks(&right->c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365 if (left) {
1366 left->fl_end = request->fl_start - 1;
Jeff Layton347d49f2024-01-31 18:02:11 -05001367 locks_wake_up_blocks(&left->c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
1369 out:
Jeff Layton1b3ec4f2024-07-02 18:44:48 -04001370 trace_posix_lock_inode(inode, request, error);
Jeff Layton6109c852015-01-16 15:05:57 -05001371 spin_unlock(&ctx->flc_lock);
Peter Zijlstra02e525b22019-02-21 15:38:40 +01001372 percpu_up_read(&file_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 /*
1374 * Free any unused locks.
1375 */
1376 if (new_fl)
1377 locks_free_lock(new_fl);
1378 if (new_fl2)
1379 locks_free_lock(new_fl2);
Jeff Laytoned9814d2014-08-11 14:20:31 -04001380 locks_dispose_list(&dispose);
Jeff Layton18909102016-01-06 21:26:10 -05001381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 return error;
1383}
1384
1385/**
1386 * posix_lock_file - Apply a POSIX-style lock to a file
1387 * @filp: The file to apply the lock to
1388 * @fl: The lock to be applied
Marc Eshel150b3932007-01-18 16:15:35 -05001389 * @conflock: Place to return a copy of the conflicting lock, if found.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 *
1391 * Add a POSIX style lock to a file.
1392 * We merge adjacent & overlapping locks whenever possible.
1393 * POSIX locks are sorted by owner task, then by starting address
Trond Myklebustf475ae92006-06-29 16:38:32 -04001394 *
1395 * Note that if called with an FL_EXISTS argument, the caller may determine
1396 * whether or not a lock was successfully freed by testing the return
1397 * value for -ENOENT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 */
Marc Eshel150b3932007-01-18 16:15:35 -05001399int posix_lock_file(struct file *filp, struct file_lock *fl,
Andy Adamson5842add2006-03-26 01:37:26 -08001400 struct file_lock *conflock)
1401{
Jeff Laytonc65454a2022-11-25 08:48:37 -05001402 return posix_lock_inode(file_inode(filp), fl, conflock);
Andy Adamson5842add2006-03-26 01:37:26 -08001403}
Marc Eshel150b3932007-01-18 16:15:35 -05001404EXPORT_SYMBOL(posix_lock_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406/**
Jeff Layton29d01b22015-07-11 06:43:02 -04001407 * posix_lock_inode_wait - Apply a POSIX-style lock to a file
1408 * @inode: inode of file to which lock request should be applied
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 * @fl: The lock to be applied
1410 *
Benjamin Coddington616fb382015-10-22 13:38:15 -04001411 * Apply a POSIX style lock request to an inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 */
Benjamin Coddington616fb382015-10-22 13:38:15 -04001413static int posix_lock_inode_wait(struct inode *inode, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
1415 int error;
1416 might_sleep ();
1417 for (;;) {
Jeff Laytonb4d629a2016-01-07 18:27:42 -05001418 error = posix_lock_inode(inode, fl, NULL);
Miklos Szeredibde74e42008-07-25 01:48:57 -07001419 if (error != FILE_LOCK_DEFERRED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 break;
Jeff Layton4ca52f52024-01-31 18:01:59 -05001421 error = wait_event_interruptible(fl->c.flc_wait,
1422 list_empty(&fl->c.flc_blocked_member));
NeilBrown16306a62018-11-30 10:04:08 +11001423 if (error)
1424 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 }
NeilBrown16306a62018-11-30 10:04:08 +11001426 locks_delete_block(fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 return error;
1428}
Jeff Layton29d01b22015-07-11 06:43:02 -04001429
Jeff Laytonc69ff402024-01-31 18:02:28 -05001430static void lease_clear_pending(struct file_lease *fl, int arg)
J. Bruce Fields778fc542011-07-26 18:25:49 -04001431{
1432 switch (arg) {
1433 case F_UNLCK:
Jeff Layton4ca52f52024-01-31 18:01:59 -05001434 fl->c.flc_flags &= ~FL_UNLOCK_PENDING;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001435 fallthrough;
J. Bruce Fields778fc542011-07-26 18:25:49 -04001436 case F_RDLCK:
Jeff Layton4ca52f52024-01-31 18:01:59 -05001437 fl->c.flc_flags &= ~FL_DOWNGRADE_PENDING;
J. Bruce Fields778fc542011-07-26 18:25:49 -04001438 }
1439}
1440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441/* We already had a lease on this file; just change its type */
Jeff Laytonc69ff402024-01-31 18:02:28 -05001442int lease_modify(struct file_lease *fl, int arg, struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
Jeff Laytond9077f72024-01-31 18:02:10 -05001444 int error = assign_type(&fl->c, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 if (error)
1447 return error;
J. Bruce Fields778fc542011-07-26 18:25:49 -04001448 lease_clear_pending(fl, arg);
Jeff Layton347d49f2024-01-31 18:02:11 -05001449 locks_wake_up_blocks(&fl->c);
Filipe Brandenburger3b6e27232012-07-27 00:42:52 -04001450 if (arg == F_UNLCK) {
Jeff Layton4ca52f52024-01-31 18:01:59 -05001451 struct file *filp = fl->c.flc_file;
Filipe Brandenburger3b6e27232012-07-27 00:42:52 -04001452
1453 f_delown(filp);
1454 filp->f_owner.signum = 0;
Jeff Layton4ca52f52024-01-31 18:01:59 -05001455 fasync_helper(0, fl->c.flc_file, 0, &fl->fl_fasync);
J. Bruce Fields96d6d592012-07-27 16:18:00 -04001456 if (fl->fl_fasync != NULL) {
1457 printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync);
1458 fl->fl_fasync = NULL;
1459 }
Jeff Layton7c185092024-01-31 18:02:12 -05001460 locks_delete_lock_ctx(&fl->c, dispose);
Filipe Brandenburger3b6e27232012-07-27 00:42:52 -04001461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return 0;
1463}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464EXPORT_SYMBOL(lease_modify);
1465
J. Bruce Fields778fc542011-07-26 18:25:49 -04001466static bool past_time(unsigned long then)
1467{
1468 if (!then)
1469 /* 0 is a special value meaning "this never expires": */
1470 return false;
1471 return time_after(jiffies, then);
1472}
1473
Jeff Laytonc45198e2014-09-01 07:12:07 -04001474static void time_out_leases(struct inode *inode, struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
Jeff Layton8634b512015-01-16 15:05:55 -05001476 struct file_lock_context *ctx = inode->i_flctx;
Jeff Laytonc69ff402024-01-31 18:02:28 -05001477 struct file_lease *fl, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Jeff Layton6109c852015-01-16 15:05:57 -05001479 lockdep_assert_held(&ctx->flc_lock);
Jeff Laytonf82b4b62014-08-22 18:50:48 -04001480
Jeff Layton4ca52f52024-01-31 18:01:59 -05001481 list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, c.flc_list) {
Jeff Layton62af4f12014-05-09 14:13:05 -04001482 trace_time_out_leases(inode, fl);
J. Bruce Fields778fc542011-07-26 18:25:49 -04001483 if (past_time(fl->fl_downgrade_time))
Jeff Layton7448cc32015-01-16 15:05:57 -05001484 lease_modify(fl, F_RDLCK, dispose);
J. Bruce Fields778fc542011-07-26 18:25:49 -04001485 if (past_time(fl->fl_break_time))
Jeff Layton7448cc32015-01-16 15:05:57 -05001486 lease_modify(fl, F_UNLCK, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
1488}
1489
Jeff Laytonb6be3712024-01-31 18:02:06 -05001490static bool leases_conflict(struct file_lock_core *lc, struct file_lock_core *bc)
J. Bruce Fieldsdf4e8d22012-03-05 13:18:59 -05001491{
Ira Weinyd51f5272019-06-05 18:45:34 -07001492 bool rc;
Jeff Laytonc69ff402024-01-31 18:02:28 -05001493 struct file_lease *lease = file_lease(lc);
1494 struct file_lease *breaker = file_lease(bc);
Ira Weinyd51f5272019-06-05 18:45:34 -07001495
J. Bruce Fields28df3d12017-07-28 16:35:15 -04001496 if (lease->fl_lmops->lm_breaker_owns_lease
1497 && lease->fl_lmops->lm_breaker_owns_lease(lease))
1498 return false;
Jeff Laytonb6be3712024-01-31 18:02:06 -05001499 if ((bc->flc_flags & FL_LAYOUT) != (lc->flc_flags & FL_LAYOUT)) {
Ira Weinyd51f5272019-06-05 18:45:34 -07001500 rc = false;
1501 goto trace;
1502 }
Jeff Laytonb6be3712024-01-31 18:02:06 -05001503 if ((bc->flc_flags & FL_DELEG) && (lc->flc_flags & FL_LEASE)) {
Ira Weinyd51f5272019-06-05 18:45:34 -07001504 rc = false;
1505 goto trace;
1506 }
1507
Jeff Laytonb6be3712024-01-31 18:02:06 -05001508 rc = locks_conflict(bc, lc);
Ira Weinyd51f5272019-06-05 18:45:34 -07001509trace:
1510 trace_leases_conflict(rc, lease, breaker);
1511 return rc;
J. Bruce Fieldsdf4e8d22012-03-05 13:18:59 -05001512}
1513
Jeff Layton03d12dd2014-09-01 14:53:41 -04001514static bool
Jeff Laytonc69ff402024-01-31 18:02:28 -05001515any_leases_conflict(struct inode *inode, struct file_lease *breaker)
Jeff Layton03d12dd2014-09-01 14:53:41 -04001516{
Jeff Layton8634b512015-01-16 15:05:55 -05001517 struct file_lock_context *ctx = inode->i_flctx;
Jeff Laytonb6be3712024-01-31 18:02:06 -05001518 struct file_lock_core *flc;
Jeff Layton03d12dd2014-09-01 14:53:41 -04001519
Jeff Layton6109c852015-01-16 15:05:57 -05001520 lockdep_assert_held(&ctx->flc_lock);
Jeff Layton03d12dd2014-09-01 14:53:41 -04001521
Jeff Laytonb6be3712024-01-31 18:02:06 -05001522 list_for_each_entry(flc, &ctx->flc_lease, flc_list) {
1523 if (leases_conflict(flc, &breaker->c))
Jeff Layton03d12dd2014-09-01 14:53:41 -04001524 return true;
1525 }
1526 return false;
1527}
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529/**
1530 * __break_lease - revoke all outstanding leases on file
1531 * @inode: the inode of the file to return
J. Bruce Fieldsdf4e8d22012-03-05 13:18:59 -05001532 * @mode: O_RDONLY: break only write leases; O_WRONLY or O_RDWR:
1533 * break all leases
1534 * @type: FL_LEASE: break leases and delegations; FL_DELEG: break
1535 * only delegations
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 *
david m. richter87250dd2007-05-09 16:10:27 -04001537 * break_lease (inlined for speed) has checked there already is at least
1538 * some kind of lock (maybe a lease) on this file. Leases are broken on
1539 * a call to open() or truncate(). This function can sleep unless you
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 * specified %O_NONBLOCK to your open().
1541 */
J. Bruce Fieldsdf4e8d22012-03-05 13:18:59 -05001542int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
J. Bruce Fields778fc542011-07-26 18:25:49 -04001544 int error = 0;
Dmitry Vyukov128a3782015-09-21 09:43:06 +02001545 struct file_lock_context *ctx;
Jeff Laytonc69ff402024-01-31 18:02:28 -05001546 struct file_lease *new_fl, *fl, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 unsigned long break_time;
Al Viro8737c932009-12-24 06:47:55 -05001548 int want_write = (mode & O_ACCMODE) != O_RDONLY;
Jeff Laytonc45198e2014-09-01 07:12:07 -04001549 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Al Viro8737c932009-12-24 06:47:55 -05001551 new_fl = lease_alloc(NULL, want_write ? F_WRLCK : F_RDLCK);
Linus Torvalds6d4b9e32011-12-26 10:25:26 -08001552 if (IS_ERR(new_fl))
1553 return PTR_ERR(new_fl);
Jeff Layton4ca52f52024-01-31 18:01:59 -05001554 new_fl->c.flc_flags = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Jeff Layton8634b512015-01-16 15:05:55 -05001556 /* typically we will check that ctx is non-NULL before calling */
Jeff Layton401a8b82022-11-16 09:02:30 -05001557 ctx = locks_inode_context(inode);
Jeff Layton8634b512015-01-16 15:05:55 -05001558 if (!ctx) {
1559 WARN_ON_ONCE(1);
Wenwen Wangcfddf9f2019-08-19 18:47:34 -05001560 goto free_lock;
Jeff Layton8634b512015-01-16 15:05:55 -05001561 }
1562
Peter Zijlstra02e525b22019-02-21 15:38:40 +01001563 percpu_down_read(&file_rwsem);
Jeff Layton6109c852015-01-16 15:05:57 -05001564 spin_lock(&ctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Jeff Laytonc45198e2014-09-01 07:12:07 -04001566 time_out_leases(inode, &dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Jeff Layton03d12dd2014-09-01 14:53:41 -04001568 if (!any_leases_conflict(inode, new_fl))
J. Bruce Fields778fc542011-07-26 18:25:49 -04001569 goto out;
1570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 break_time = 0;
1572 if (lease_break_time > 0) {
1573 break_time = jiffies + lease_break_time * HZ;
1574 if (break_time == 0)
1575 break_time++; /* so that 0 means no break time */
1576 }
1577
Jeff Layton4ca52f52024-01-31 18:01:59 -05001578 list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, c.flc_list) {
Jeff Laytonb6be3712024-01-31 18:02:06 -05001579 if (!leases_conflict(&fl->c, &new_fl->c))
J. Bruce Fieldsdf4e8d22012-03-05 13:18:59 -05001580 continue;
J. Bruce Fields778fc542011-07-26 18:25:49 -04001581 if (want_write) {
Jeff Layton4ca52f52024-01-31 18:01:59 -05001582 if (fl->c.flc_flags & FL_UNLOCK_PENDING)
J. Bruce Fields778fc542011-07-26 18:25:49 -04001583 continue;
Jeff Layton4ca52f52024-01-31 18:01:59 -05001584 fl->c.flc_flags |= FL_UNLOCK_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 fl->fl_break_time = break_time;
J. Bruce Fields778fc542011-07-26 18:25:49 -04001586 } else {
Jeff Layton8634b512015-01-16 15:05:55 -05001587 if (lease_breaking(fl))
J. Bruce Fields778fc542011-07-26 18:25:49 -04001588 continue;
Jeff Layton4ca52f52024-01-31 18:01:59 -05001589 fl->c.flc_flags |= FL_DOWNGRADE_PENDING;
J. Bruce Fields778fc542011-07-26 18:25:49 -04001590 fl->fl_downgrade_time = break_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 }
Jeff Layton4d01b7f2014-09-01 15:06:54 -04001592 if (fl->fl_lmops->lm_break(fl))
Jeff Layton7c185092024-01-31 18:02:12 -05001593 locks_delete_lock_ctx(&fl->c, &dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 }
1595
Jeff Layton8634b512015-01-16 15:05:55 -05001596 if (list_empty(&ctx->flc_lease))
Jeff Layton4d01b7f2014-09-01 15:06:54 -04001597 goto out;
1598
Jeff Layton843c6b22014-09-01 14:27:43 -04001599 if (mode & O_NONBLOCK) {
Jeff Layton62af4f12014-05-09 14:13:05 -04001600 trace_break_lease_noblock(inode, new_fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 error = -EWOULDBLOCK;
1602 goto out;
1603 }
1604
1605restart:
Jeff Laytonc69ff402024-01-31 18:02:28 -05001606 fl = list_first_entry(&ctx->flc_lease, struct file_lease, c.flc_list);
Jeff Layton8634b512015-01-16 15:05:55 -05001607 break_time = fl->fl_break_time;
Jeff Laytonf1c6bb22014-04-15 06:17:49 -04001608 if (break_time != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 break_time -= jiffies;
Jeff Laytonf1c6bb22014-04-15 06:17:49 -04001610 if (break_time == 0)
1611 break_time++;
Jeff Layton269a6192024-01-31 18:02:09 -05001612 locks_insert_block(&fl->c, &new_fl->c, leases_conflict);
Jeff Layton62af4f12014-05-09 14:13:05 -04001613 trace_break_lease_block(inode, new_fl);
Jeff Layton6109c852015-01-16 15:05:57 -05001614 spin_unlock(&ctx->flc_lock);
Peter Zijlstra02e525b22019-02-21 15:38:40 +01001615 percpu_up_read(&file_rwsem);
Peter Zijlstraaba37662015-06-22 14:16:33 +02001616
Jeff Laytonc45198e2014-09-01 07:12:07 -04001617 locks_dispose_list(&dispose);
Jeff Layton4ca52f52024-01-31 18:01:59 -05001618 error = wait_event_interruptible_timeout(new_fl->c.flc_wait,
1619 list_empty(&new_fl->c.flc_blocked_member),
1620 break_time);
Peter Zijlstraaba37662015-06-22 14:16:33 +02001621
Peter Zijlstra02e525b22019-02-21 15:38:40 +01001622 percpu_down_read(&file_rwsem);
Jeff Layton6109c852015-01-16 15:05:57 -05001623 spin_lock(&ctx->flc_lock);
Jeff Layton62af4f12014-05-09 14:13:05 -04001624 trace_break_lease_unblock(inode, new_fl);
Jeff Laytonc69ff402024-01-31 18:02:28 -05001625 __locks_delete_block(&new_fl->c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 if (error >= 0) {
J. Bruce Fields778fc542011-07-26 18:25:49 -04001627 /*
1628 * Wait for the next conflicting lease that has not been
1629 * broken yet
1630 */
Jeff Layton03d12dd2014-09-01 14:53:41 -04001631 if (error == 0)
1632 time_out_leases(inode, &dispose);
1633 if (any_leases_conflict(inode, new_fl))
1634 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 error = 0;
1636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637out:
Jeff Layton6109c852015-01-16 15:05:57 -05001638 spin_unlock(&ctx->flc_lock);
Peter Zijlstra02e525b22019-02-21 15:38:40 +01001639 percpu_up_read(&file_rwsem);
Jeff Laytonc45198e2014-09-01 07:12:07 -04001640 locks_dispose_list(&dispose);
Wenwen Wangcfddf9f2019-08-19 18:47:34 -05001641free_lock:
Jeff Laytonc69ff402024-01-31 18:02:28 -05001642 locks_free_lease(new_fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 return error;
1644}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645EXPORT_SYMBOL(__break_lease);
1646
1647/**
Amir Goldstein76c47942018-01-03 17:14:34 +02001648 * lease_get_mtime - update modified time of an inode with exclusive lease
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 * @inode: the inode
Amir Goldstein76c47942018-01-03 17:14:34 +02001650 * @time: pointer to a timespec which contains the last modified time
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 *
1652 * This is to force NFS clients to flush their caches for files with
1653 * exclusive leases. The justification is that if someone has an
Randy Dunlapa6b91912008-03-19 17:01:00 -07001654 * exclusive lease, then they could be modifying it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07001656void lease_get_mtime(struct inode *inode, struct timespec64 *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
Jeff Laytonbfe86022014-08-22 10:18:44 -04001658 bool has_lease = false;
Dmitry Vyukov128a3782015-09-21 09:43:06 +02001659 struct file_lock_context *ctx;
Jeff Laytonc69ff402024-01-31 18:02:28 -05001660 struct file_lock_core *flc;
Jeff Laytonbfe86022014-08-22 10:18:44 -04001661
Jeff Layton401a8b82022-11-16 09:02:30 -05001662 ctx = locks_inode_context(inode);
Jeff Layton8634b512015-01-16 15:05:55 -05001663 if (ctx && !list_empty_careful(&ctx->flc_lease)) {
Jeff Layton6109c852015-01-16 15:05:57 -05001664 spin_lock(&ctx->flc_lock);
Jeff Laytonc69ff402024-01-31 18:02:28 -05001665 flc = list_first_entry_or_null(&ctx->flc_lease,
1666 struct file_lock_core, flc_list);
1667 if (flc && flc->flc_type == F_WRLCK)
Geliang Tang8ace5df2015-11-18 21:40:33 +08001668 has_lease = true;
Jeff Layton6109c852015-01-16 15:05:57 -05001669 spin_unlock(&ctx->flc_lock);
Jeff Laytonbfe86022014-08-22 10:18:44 -04001670 }
1671
1672 if (has_lease)
Deepa Dinamanic2050a42016-09-14 07:48:06 -07001673 *time = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675EXPORT_SYMBOL(lease_get_mtime);
1676
1677/**
1678 * fcntl_getlease - Enquire what lease is currently active
1679 * @filp: the file
1680 *
1681 * The value returned by this function will be one of
1682 * (if no lease break is pending):
1683 *
1684 * %F_RDLCK to indicate a shared lease is held.
1685 *
1686 * %F_WRLCK to indicate an exclusive lease is held.
1687 *
1688 * %F_UNLCK to indicate no lease is held.
1689 *
1690 * (if a lease break is pending):
1691 *
1692 * %F_RDLCK to indicate an exclusive lease needs to be
1693 * changed to a shared lease (or removed).
1694 *
1695 * %F_UNLCK to indicate the lease needs to be removed.
1696 *
1697 * XXX: sfr & willy disagree over whether F_INPROGRESS
1698 * should be returned to userspace.
1699 */
1700int fcntl_getlease(struct file *filp)
1701{
Jeff Laytonc69ff402024-01-31 18:02:28 -05001702 struct file_lease *fl;
Jeff Laytonc65454a2022-11-25 08:48:37 -05001703 struct inode *inode = file_inode(filp);
Dmitry Vyukov128a3782015-09-21 09:43:06 +02001704 struct file_lock_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 int type = F_UNLCK;
Jeff Laytonc45198e2014-09-01 07:12:07 -04001706 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Jeff Layton401a8b82022-11-16 09:02:30 -05001708 ctx = locks_inode_context(inode);
Jeff Layton8634b512015-01-16 15:05:55 -05001709 if (ctx && !list_empty_careful(&ctx->flc_lease)) {
Peter Zijlstra02e525b22019-02-21 15:38:40 +01001710 percpu_down_read(&file_rwsem);
Jeff Layton6109c852015-01-16 15:05:57 -05001711 spin_lock(&ctx->flc_lock);
Miklos Szeredic568d682016-09-16 12:44:20 +02001712 time_out_leases(inode, &dispose);
Jeff Layton4ca52f52024-01-31 18:01:59 -05001713 list_for_each_entry(fl, &ctx->flc_lease, c.flc_list) {
1714 if (fl->c.flc_file != filp)
Jeff Layton8634b512015-01-16 15:05:55 -05001715 continue;
J. Bruce Fields778fc542011-07-26 18:25:49 -04001716 type = target_leasetype(fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 break;
1718 }
Jeff Layton6109c852015-01-16 15:05:57 -05001719 spin_unlock(&ctx->flc_lock);
Peter Zijlstra02e525b22019-02-21 15:38:40 +01001720 percpu_up_read(&file_rwsem);
Peter Zijlstra5f430862016-10-08 10:12:28 +02001721
Jeff Layton8634b512015-01-16 15:05:55 -05001722 locks_dispose_list(&dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 return type;
1725}
1726
Jeff Layton24cbe782014-02-03 12:13:06 -05001727/**
Amir Goldstein387e3742019-06-07 17:24:38 +03001728 * check_conflicting_open - see if the given file points to an inode that has
NeilBrown7bbd1fc2018-11-30 10:04:08 +11001729 * an existing open that would conflict with the
1730 * desired lease.
Amir Goldstein387e3742019-06-07 17:24:38 +03001731 * @filp: file to check
Jeff Layton24cbe782014-02-03 12:13:06 -05001732 * @arg: type of lease that we're trying to acquire
Randy Dunlap7fadc592015-08-09 18:43:17 -07001733 * @flags: current lock flags
Jeff Layton24cbe782014-02-03 12:13:06 -05001734 *
1735 * Check to see if there's an existing open fd on this file that would
1736 * conflict with the lease we're trying to set.
1737 */
1738static int
Luca Vizzarroed5f17f2023-02-01 15:05:33 +00001739check_conflicting_open(struct file *filp, const int arg, int flags)
Jeff Layton24cbe782014-02-03 12:13:06 -05001740{
Jeff Laytonc65454a2022-11-25 08:48:37 -05001741 struct inode *inode = file_inode(filp);
Amir Goldstein387e3742019-06-07 17:24:38 +03001742 int self_wcount = 0, self_rcount = 0;
Jeff Layton24cbe782014-02-03 12:13:06 -05001743
Christoph Hellwig11afe9f2015-01-21 19:17:03 +01001744 if (flags & FL_LAYOUT)
1745 return 0;
J. Bruce Fieldsaba20722021-04-16 14:00:18 -04001746 if (flags & FL_DELEG)
1747 /* We leave these checks to the caller */
1748 return 0;
Christoph Hellwig11afe9f2015-01-21 19:17:03 +01001749
Amir Goldstein387e3742019-06-07 17:24:38 +03001750 if (arg == F_RDLCK)
1751 return inode_is_open_for_write(inode) ? -EAGAIN : 0;
1752 else if (arg != F_WRLCK)
1753 return 0;
1754
1755 /*
1756 * Make sure that only read/write count is from lease requestor.
1757 * Note that this will result in denying write leases when i_writecount
1758 * is negative, which is what we want. (We shouldn't grant write leases
1759 * on files open for execution.)
1760 */
1761 if (filp->f_mode & FMODE_WRITE)
1762 self_wcount = 1;
1763 else if (filp->f_mode & FMODE_READ)
1764 self_rcount = 1;
1765
1766 if (atomic_read(&inode->i_writecount) != self_wcount ||
1767 atomic_read(&inode->i_readcount) != self_rcount)
Jeff Layton24cbe782014-02-03 12:13:06 -05001768 return -EAGAIN;
1769
Amir Goldstein387e3742019-06-07 17:24:38 +03001770 return 0;
Jeff Layton24cbe782014-02-03 12:13:06 -05001771}
1772
Jeff Laytone6f5c782014-08-22 10:40:25 -04001773static int
Jeff Laytonc69ff402024-01-31 18:02:28 -05001774generic_add_lease(struct file *filp, int arg, struct file_lease **flp, void **priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
Jeff Laytonc69ff402024-01-31 18:02:28 -05001776 struct file_lease *fl, *my_fl = NULL, *lease;
Jeff Laytonc65454a2022-11-25 08:48:37 -05001777 struct inode *inode = file_inode(filp);
Jeff Layton8634b512015-01-16 15:05:55 -05001778 struct file_lock_context *ctx;
Jeff Layton4ca52f52024-01-31 18:01:59 -05001779 bool is_deleg = (*flp)->c.flc_flags & FL_DELEG;
J. Bruce Fieldsc1f24ef2011-08-19 10:59:49 -04001780 int error;
Jeff Laytonc45198e2014-09-01 07:12:07 -04001781 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
J. Bruce Fields096657b2010-10-30 17:31:14 -04001783 lease = *flp;
Jeff Layton62af4f12014-05-09 14:13:05 -04001784 trace_generic_add_lease(inode, lease);
1785
Jeff Layton5c1c6692015-04-03 09:04:03 -04001786 /* Note that arg is never F_UNLCK here */
1787 ctx = locks_get_lock_context(inode, arg);
Jeff Layton8634b512015-01-16 15:05:55 -05001788 if (!ctx)
1789 return -ENOMEM;
1790
J. Bruce Fieldsdf4e8d22012-03-05 13:18:59 -05001791 /*
1792 * In the delegation case we need mutual exclusion with
1793 * a number of operations that take the i_mutex. We trylock
1794 * because delegations are an optional optimization, and if
1795 * there's some chance of a conflict--we'd rather not
1796 * bother, maybe that's a sign this just isn't a good file to
1797 * hand out a delegation on.
1798 */
Al Viro59551022016-01-22 15:40:57 -05001799 if (is_deleg && !inode_trylock(inode))
J. Bruce Fieldsdf4e8d22012-03-05 13:18:59 -05001800 return -EAGAIN;
1801
Peter Zijlstra02e525b22019-02-21 15:38:40 +01001802 percpu_down_read(&file_rwsem);
Jeff Layton6109c852015-01-16 15:05:57 -05001803 spin_lock(&ctx->flc_lock);
Jeff Laytonc45198e2014-09-01 07:12:07 -04001804 time_out_leases(inode, &dispose);
Jeff Layton4ca52f52024-01-31 18:01:59 -05001805 error = check_conflicting_open(filp, arg, lease->c.flc_flags);
Jeff Layton24cbe782014-02-03 12:13:06 -05001806 if (error)
J. Bruce Fields096657b2010-10-30 17:31:14 -04001807 goto out;
Pavel Emelyanov85c59582007-09-20 12:45:02 +04001808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 /*
1810 * At this point, we know that if there is an exclusive
1811 * lease on this file, then we hold it on this filp
1812 * (otherwise our open of this file would have blocked).
1813 * And if we are trying to acquire an exclusive lease,
1814 * then the file is not open by anyone (including us)
1815 * except for this filp.
1816 */
J. Bruce Fieldsc1f24ef2011-08-19 10:59:49 -04001817 error = -EAGAIN;
Jeff Layton4ca52f52024-01-31 18:01:59 -05001818 list_for_each_entry(fl, &ctx->flc_lease, c.flc_list) {
1819 if (fl->c.flc_file == filp &&
1820 fl->c.flc_owner == lease->c.flc_owner) {
Jeff Layton8634b512015-01-16 15:05:55 -05001821 my_fl = fl;
J. Bruce Fieldsc1f24ef2011-08-19 10:59:49 -04001822 continue;
1823 }
Jeff Layton8634b512015-01-16 15:05:55 -05001824
J. Bruce Fieldsc1f24ef2011-08-19 10:59:49 -04001825 /*
1826 * No exclusive leases if someone else has a lease on
1827 * this file:
1828 */
1829 if (arg == F_WRLCK)
1830 goto out;
1831 /*
1832 * Modifying our existing lease is OK, but no getting a
1833 * new lease if someone else is opening for write:
1834 */
Jeff Layton4ca52f52024-01-31 18:01:59 -05001835 if (fl->c.flc_flags & FL_UNLOCK_PENDING)
J. Bruce Fieldsc1f24ef2011-08-19 10:59:49 -04001836 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 }
1838
Jeff Layton8634b512015-01-16 15:05:55 -05001839 if (my_fl != NULL) {
Jeff Layton0164bf02015-03-04 17:34:32 -05001840 lease = my_fl;
1841 error = lease->fl_lmops->lm_change(lease, arg, &dispose);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04001842 if (error)
1843 goto out;
1844 goto out_setup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 }
1846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 error = -EINVAL;
1848 if (!leases_enable)
1849 goto out;
1850
Jeff Layton7c185092024-01-31 18:02:12 -05001851 locks_insert_lock_ctx(&lease->c, &ctx->flc_lease);
Jeff Layton24cbe782014-02-03 12:13:06 -05001852 /*
1853 * The check in break_lease() is lockless. It's possible for another
1854 * open to race in after we did the earlier check for a conflicting
1855 * open but before the lease was inserted. Check again for a
1856 * conflicting open and cancel the lease if there is one.
1857 *
1858 * We also add a barrier here to ensure that the insertion of the lock
1859 * precedes these checks.
1860 */
1861 smp_mb();
Jeff Layton4ca52f52024-01-31 18:01:59 -05001862 error = check_conflicting_open(filp, arg, lease->c.flc_flags);
Jeff Layton8634b512015-01-16 15:05:55 -05001863 if (error) {
Jeff Layton7c185092024-01-31 18:02:12 -05001864 locks_unlink_lock_ctx(&lease->c);
Jeff Layton8634b512015-01-16 15:05:55 -05001865 goto out;
1866 }
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04001867
1868out_setup:
1869 if (lease->fl_lmops->lm_setup)
1870 lease->fl_lmops->lm_setup(lease, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871out:
Jeff Layton6109c852015-01-16 15:05:57 -05001872 spin_unlock(&ctx->flc_lock);
Peter Zijlstra02e525b22019-02-21 15:38:40 +01001873 percpu_up_read(&file_rwsem);
Jeff Laytonc45198e2014-09-01 07:12:07 -04001874 locks_dispose_list(&dispose);
J. Bruce Fieldsdf4e8d22012-03-05 13:18:59 -05001875 if (is_deleg)
Al Viro59551022016-01-22 15:40:57 -05001876 inode_unlock(inode);
Jeff Layton8634b512015-01-16 15:05:55 -05001877 if (!error && !my_fl)
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04001878 *flp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 return error;
1880}
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001881
Christoph Hellwig2ab99ee2015-01-21 19:14:02 +01001882static int generic_delete_lease(struct file *filp, void *owner)
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001883{
Jeff Layton0efaa7e2014-08-22 10:18:45 -04001884 int error = -EAGAIN;
Jeff Laytonc69ff402024-01-31 18:02:28 -05001885 struct file_lease *fl, *victim = NULL;
Jeff Laytonc65454a2022-11-25 08:48:37 -05001886 struct inode *inode = file_inode(filp);
Dmitry Vyukov128a3782015-09-21 09:43:06 +02001887 struct file_lock_context *ctx;
Jeff Laytonc45198e2014-09-01 07:12:07 -04001888 LIST_HEAD(dispose);
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001889
Jeff Layton401a8b82022-11-16 09:02:30 -05001890 ctx = locks_inode_context(inode);
Jeff Layton8634b512015-01-16 15:05:55 -05001891 if (!ctx) {
1892 trace_generic_delete_lease(inode, NULL);
1893 return error;
1894 }
1895
Peter Zijlstra02e525b22019-02-21 15:38:40 +01001896 percpu_down_read(&file_rwsem);
Jeff Layton6109c852015-01-16 15:05:57 -05001897 spin_lock(&ctx->flc_lock);
Jeff Layton4ca52f52024-01-31 18:01:59 -05001898 list_for_each_entry(fl, &ctx->flc_lease, c.flc_list) {
1899 if (fl->c.flc_file == filp &&
1900 fl->c.flc_owner == owner) {
Jeff Layton8634b512015-01-16 15:05:55 -05001901 victim = fl;
Jeff Layton0efaa7e2014-08-22 10:18:45 -04001902 break;
Jeff Layton8634b512015-01-16 15:05:55 -05001903 }
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001904 }
Jeff Laytona9b1b452015-03-14 09:45:35 -04001905 trace_generic_delete_lease(inode, victim);
Jeff Layton8634b512015-01-16 15:05:55 -05001906 if (victim)
Jeff Layton7448cc32015-01-16 15:05:57 -05001907 error = fl->fl_lmops->lm_change(victim, F_UNLCK, &dispose);
Jeff Layton6109c852015-01-16 15:05:57 -05001908 spin_unlock(&ctx->flc_lock);
Peter Zijlstra02e525b22019-02-21 15:38:40 +01001909 percpu_up_read(&file_rwsem);
Jeff Laytonc45198e2014-09-01 07:12:07 -04001910 locks_dispose_list(&dispose);
Jeff Layton0efaa7e2014-08-22 10:18:45 -04001911 return error;
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001912}
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914/**
1915 * generic_setlease - sets a lease on an open file
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04001916 * @filp: file pointer
1917 * @arg: type of lease to obtain
1918 * @flp: input - file_lock to use, output - file_lock inserted
1919 * @priv: private data for lm_setup (may be NULL if lm_setup
1920 * doesn't require it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 *
1922 * The (input) flp->fl_lmops->lm_break function is required
1923 * by break_lease().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 */
Jeff Laytonc69ff402024-01-31 18:02:28 -05001925int generic_setlease(struct file *filp, int arg, struct file_lease **flp,
Jeff Laytone6f5c782014-08-22 10:40:25 -04001926 void **priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927{
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001928 switch (arg) {
1929 case F_UNLCK:
Christoph Hellwig2ab99ee2015-01-21 19:14:02 +01001930 return generic_delete_lease(filp, *priv);
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001931 case F_RDLCK:
1932 case F_WRLCK:
Jeff Layton0efaa7e2014-08-22 10:18:45 -04001933 if (!(*flp)->fl_lmops->lm_break) {
1934 WARN_ON_ONCE(1);
1935 return -ENOLCK;
1936 }
Christoph Hellwig11afe9f2015-01-21 19:17:03 +01001937
Jeff Laytone6f5c782014-08-22 10:40:25 -04001938 return generic_add_lease(filp, arg, flp, priv);
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001939 default:
Dave Jones8d657eb2012-07-13 13:35:36 -04001940 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942}
Christoph Hellwig0af1a452007-07-31 00:39:22 -07001943EXPORT_SYMBOL(generic_setlease);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Jeff Layton18f66222019-08-18 14:18:45 -04001945/*
1946 * Kernel subsystems can register to be notified on any attempt to set
1947 * a new lease with the lease_notifier_chain. This is used by (e.g.) nfsd
1948 * to close files that it may have cached when there is an attempt to set a
1949 * conflicting lease.
1950 */
1951static struct srcu_notifier_head lease_notifier_chain;
1952
1953static inline void
1954lease_notifier_chain_init(void)
1955{
1956 srcu_init_notifier_head(&lease_notifier_chain);
1957}
1958
1959static inline void
Jeff Laytonc69ff402024-01-31 18:02:28 -05001960setlease_notifier(int arg, struct file_lease *lease)
Jeff Layton18f66222019-08-18 14:18:45 -04001961{
1962 if (arg != F_UNLCK)
1963 srcu_notifier_call_chain(&lease_notifier_chain, arg, lease);
1964}
1965
1966int lease_register_notifier(struct notifier_block *nb)
1967{
1968 return srcu_notifier_chain_register(&lease_notifier_chain, nb);
1969}
1970EXPORT_SYMBOL_GPL(lease_register_notifier);
1971
1972void lease_unregister_notifier(struct notifier_block *nb)
1973{
1974 srcu_notifier_chain_unregister(&lease_notifier_chain, nb);
1975}
1976EXPORT_SYMBOL_GPL(lease_unregister_notifier);
1977
Jeff Layton7b800102024-02-05 07:09:31 -05001978
1979int
1980kernel_setlease(struct file *filp, int arg, struct file_lease **lease, void **priv)
1981{
1982 if (lease)
1983 setlease_notifier(arg, *lease);
1984 if (filp->f_op->setlease)
1985 return filp->f_op->setlease(filp, arg, lease, priv);
1986 else
1987 return generic_setlease(filp, arg, lease, priv);
1988}
1989EXPORT_SYMBOL_GPL(kernel_setlease);
1990
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001991/**
Jeff Laytone51673a2014-08-22 18:13:28 -04001992 * vfs_setlease - sets a lease on an open file
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04001993 * @filp: file pointer
1994 * @arg: type of lease to obtain
1995 * @lease: file_lock to use when adding a lease
1996 * @priv: private info for lm_setup when adding a lease (may be
NeilBrown7bbd1fc2018-11-30 10:04:08 +11001997 * NULL if lm_setup doesn't require it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 *
Jeff Laytone51673a2014-08-22 18:13:28 -04001999 * Call this to establish a lease on the file. The "lease" argument is not
2000 * used for F_UNLCK requests and may be NULL. For commands that set or alter
Mauro Carvalho Chehab80b79dd2017-05-27 06:07:18 -04002001 * an existing lease, the ``(*lease)->fl_lmops->lm_break`` operation must be
2002 * set; if not, this function will return -ENOLCK (and generate a scary-looking
Jeff Laytone51673a2014-08-22 18:13:28 -04002003 * stack trace).
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04002004 *
2005 * The "priv" pointer is passed directly to the lm_setup function as-is. It
2006 * may be NULL if the lm_setup operation doesn't require it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 */
Jeff Laytone6f5c782014-08-22 10:40:25 -04002008int
Jeff Laytonc69ff402024-01-31 18:02:28 -05002009vfs_setlease(struct file *filp, int arg, struct file_lease **lease, void **priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
Jeff Layton7b800102024-02-05 07:09:31 -05002011 struct inode *inode = file_inode(filp);
2012 vfsuid_t vfsuid = i_uid_into_vfsuid(file_mnt_idmap(filp), inode);
2013 int error;
2014
2015 if ((!vfsuid_eq_kuid(vfsuid, current_fsuid())) && !capable(CAP_LEASE))
2016 return -EACCES;
2017 if (!S_ISREG(inode->i_mode))
2018 return -EINVAL;
2019 error = security_file_lock(filp, arg);
2020 if (error)
2021 return error;
2022 return kernel_setlease(filp, arg, lease, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023}
J. Bruce Fieldsa9933ce2007-06-07 17:09:49 -04002024EXPORT_SYMBOL_GPL(vfs_setlease);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Luca Vizzarroed5f17f2023-02-01 15:05:33 +00002026static int do_fcntl_add_lease(unsigned int fd, struct file *filp, int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
Jeff Laytonc69ff402024-01-31 18:02:28 -05002028 struct file_lease *fl;
Linus Torvaldsf7347ce2010-10-27 12:38:12 -04002029 struct fasync_struct *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 int error;
2031
Arnd Bergmannc5b1f0d2010-10-27 15:46:08 +02002032 fl = lease_alloc(filp, arg);
2033 if (IS_ERR(fl))
2034 return PTR_ERR(fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Linus Torvaldsf7347ce2010-10-27 12:38:12 -04002036 new = fasync_alloc();
2037 if (!new) {
Jeff Laytonc69ff402024-01-31 18:02:28 -05002038 locks_free_lease(fl);
Linus Torvaldsf7347ce2010-10-27 12:38:12 -04002039 return -ENOMEM;
2040 }
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04002041 new->fa_fd = fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04002043 error = vfs_setlease(filp, arg, &fl, (void **)&new);
Jeff Layton2dfb9282014-08-11 18:14:12 -04002044 if (fl)
Jeff Laytonc69ff402024-01-31 18:02:28 -05002045 locks_free_lease(fl);
Linus Torvaldsf7347ce2010-10-27 12:38:12 -04002046 if (new)
2047 fasync_free(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 return error;
2049}
2050
2051/**
J. Bruce Fields0ceaf6c2010-10-30 17:31:13 -04002052 * fcntl_setlease - sets a lease on an open file
2053 * @fd: open file descriptor
2054 * @filp: file pointer
2055 * @arg: type of lease to obtain
2056 *
2057 * Call this fcntl to establish a lease on the file.
2058 * Note that you also need to call %F_SETSIG to
2059 * receive a signal when the lease is broken.
2060 */
Luca Vizzarroed5f17f2023-02-01 15:05:33 +00002061int fcntl_setlease(unsigned int fd, struct file *filp, int arg)
J. Bruce Fields0ceaf6c2010-10-30 17:31:13 -04002062{
2063 if (arg == F_UNLCK)
Christoph Hellwig2ab99ee2015-01-21 19:14:02 +01002064 return vfs_setlease(filp, F_UNLCK, NULL, (void **)&filp);
J. Bruce Fields0ceaf6c2010-10-30 17:31:13 -04002065 return do_fcntl_add_lease(fd, filp, arg);
2066}
2067
2068/**
Jeff Layton29d01b22015-07-11 06:43:02 -04002069 * flock_lock_inode_wait - Apply a FLOCK-style lock to a file
2070 * @inode: inode of the file to apply to
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 * @fl: The lock to be applied
2072 *
Jeff Layton29d01b22015-07-11 06:43:02 -04002073 * Apply a FLOCK style lock request to an inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 */
Benjamin Coddington616fb382015-10-22 13:38:15 -04002075static int flock_lock_inode_wait(struct inode *inode, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076{
2077 int error;
2078 might_sleep();
2079 for (;;) {
Jeff Layton29d01b22015-07-11 06:43:02 -04002080 error = flock_lock_inode(inode, fl);
Miklos Szeredibde74e42008-07-25 01:48:57 -07002081 if (error != FILE_LOCK_DEFERRED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 break;
Jeff Layton4ca52f52024-01-31 18:01:59 -05002083 error = wait_event_interruptible(fl->c.flc_wait,
2084 list_empty(&fl->c.flc_blocked_member));
NeilBrown16306a62018-11-30 10:04:08 +11002085 if (error)
2086 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 }
NeilBrown16306a62018-11-30 10:04:08 +11002088 locks_delete_block(fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 return error;
2090}
2091
Jeff Layton29d01b22015-07-11 06:43:02 -04002092/**
Benjamin Coddingtone55c34a2015-10-22 13:38:13 -04002093 * locks_lock_inode_wait - Apply a lock to an inode
2094 * @inode: inode of the file to apply to
2095 * @fl: The lock to be applied
2096 *
2097 * Apply a POSIX or FLOCK style lock request to an inode.
2098 */
2099int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl)
2100{
2101 int res = 0;
Jeff Layton4ca52f52024-01-31 18:01:59 -05002102 switch (fl->c.flc_flags & (FL_POSIX|FL_FLOCK)) {
Benjamin Coddingtone55c34a2015-10-22 13:38:13 -04002103 case FL_POSIX:
2104 res = posix_lock_inode_wait(inode, fl);
2105 break;
2106 case FL_FLOCK:
2107 res = flock_lock_inode_wait(inode, fl);
2108 break;
2109 default:
2110 BUG();
2111 }
2112 return res;
2113}
2114EXPORT_SYMBOL(locks_lock_inode_wait);
2115
2116/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 * sys_flock: - flock() system call.
2118 * @fd: the file descriptor to lock.
2119 * @cmd: the type of lock to apply.
2120 *
2121 * Apply a %FL_FLOCK style lock to an open file descriptor.
Mauro Carvalho Chehab80b79dd2017-05-27 06:07:18 -04002122 * The @cmd can be one of:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 *
Mauro Carvalho Chehab80b79dd2017-05-27 06:07:18 -04002124 * - %LOCK_SH -- a shared lock.
2125 * - %LOCK_EX -- an exclusive lock.
2126 * - %LOCK_UN -- remove an existing lock.
Jeff Layton90f7d7a2021-09-10 15:36:29 -04002127 * - %LOCK_MAND -- a 'mandatory' flock. (DEPRECATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 *
Jeff Layton90f7d7a2021-09-10 15:36:29 -04002129 * %LOCK_MAND support has been removed from the kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 */
Heiko Carstens002c8972009-01-14 14:14:18 +01002131SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
Kuniyuki Iwashimadb4abb42022-07-16 21:35:32 -07002133 int can_sleep, error, type;
Kuniyuki Iwashima4149be72022-07-16 21:35:31 -07002134 struct file_lock fl;
Kuniyuki Iwashimadb4abb42022-07-16 21:35:32 -07002135 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Jeff Layton90f7d7a2021-09-10 15:36:29 -04002137 /*
2138 * LOCK_MAND locks were broken for a long time in that they never
2139 * conflicted with one another and didn't prevent any sort of open,
2140 * read or write activity.
2141 *
2142 * Just ignore these requests now, to preserve legacy behavior, but
2143 * throw a warning to let people know that they don't actually work.
2144 */
2145 if (cmd & LOCK_MAND) {
Andi Kleenf2f2494c2022-11-18 15:43:57 -08002146 pr_warn_once("%s(%d): Attempt to set a LOCK_MAND lock via flock(2). This support has been removed and the request ignored.\n", current->comm, current->pid);
Kuniyuki Iwashimadb4abb42022-07-16 21:35:32 -07002147 return 0;
Jeff Layton90f7d7a2021-09-10 15:36:29 -04002148 }
2149
Kuniyuki Iwashimadb4abb42022-07-16 21:35:32 -07002150 type = flock_translate_cmd(cmd & ~LOCK_NB);
2151 if (type < 0)
2152 return type;
2153
2154 error = -EBADF;
2155 f = fdget(fd);
2156 if (!f.file)
2157 return error;
2158
2159 if (type != F_UNLCK && !(f.file->f_mode & (FMODE_READ | FMODE_WRITE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 goto out_putf;
Jeff Layton6e129d002014-09-04 10:25:06 -04002161
Kuniyuki Iwashima4149be72022-07-16 21:35:31 -07002162 flock_make_lock(f.file, &fl, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Jeff Layton4ca52f52024-01-31 18:01:59 -05002164 error = security_file_lock(f.file, fl.c.flc_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 if (error)
Kuniyuki Iwashima4149be72022-07-16 21:35:31 -07002166 goto out_putf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Kuniyuki Iwashimadb4abb42022-07-16 21:35:32 -07002168 can_sleep = !(cmd & LOCK_NB);
2169 if (can_sleep)
Jeff Layton4ca52f52024-01-31 18:01:59 -05002170 fl.c.flc_flags |= FL_SLEEP;
Kuniyuki Iwashimadb4abb42022-07-16 21:35:32 -07002171
Miklos Szeredide2a4a52018-07-18 15:44:43 +02002172 if (f.file->f_op->flock)
Al Viro2903ff02012-08-28 12:52:22 -04002173 error = f.file->f_op->flock(f.file,
Kuniyuki Iwashimadb4abb42022-07-16 21:35:32 -07002174 (can_sleep) ? F_SETLKW : F_SETLK,
Kuniyuki Iwashima4149be72022-07-16 21:35:31 -07002175 &fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 else
Kuniyuki Iwashima4149be72022-07-16 21:35:31 -07002177 error = locks_lock_file_wait(f.file, &fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
David Howells932c29a2022-08-17 19:41:27 +01002179 locks_release_private(&fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 out_putf:
Al Viro2903ff02012-08-28 12:52:22 -04002181 fdput(f);
Kuniyuki Iwashimadb4abb42022-07-16 21:35:32 -07002182
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return error;
2184}
2185
J. Bruce Fields3ee17ab2007-02-21 00:58:50 -05002186/**
2187 * vfs_test_lock - test file byte range lock
2188 * @filp: The file to test lock for
J. Bruce Fields6924c552007-05-11 16:22:50 -04002189 * @fl: The lock to test; also used to hold result
J. Bruce Fields3ee17ab2007-02-21 00:58:50 -05002190 *
2191 * Returns -ERRNO on failure. Indicates presence of conflicting lock by
2192 * setting conf->fl_type to something other than F_UNLCK.
2193 */
2194int vfs_test_lock(struct file *filp, struct file_lock *fl)
2195{
Jeff Layton4ca52f52024-01-31 18:01:59 -05002196 WARN_ON_ONCE(filp != fl->c.flc_file);
Miklos Szeredide2a4a52018-07-18 15:44:43 +02002197 if (filp->f_op->lock)
J. Bruce Fields3ee17ab2007-02-21 00:58:50 -05002198 return filp->f_op->lock(filp, F_GETLK, fl);
2199 posix_test_lock(filp, fl);
2200 return 0;
2201}
2202EXPORT_SYMBOL_GPL(vfs_test_lock);
2203
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002204/**
2205 * locks_translate_pid - translate a file_lock's fl_pid number into a namespace
2206 * @fl: The file_lock who's fl_pid should be translated
2207 * @ns: The namespace into which the pid should be translated
2208 *
Jakub Wilkbd4c4682023-07-21 11:21:47 +02002209 * Used to translate a fl_pid into a namespace virtual pid number
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002210 */
Jeff Laytonae7eb162024-01-31 18:02:13 -05002211static pid_t locks_translate_pid(struct file_lock_core *fl, struct pid_namespace *ns)
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002212{
2213 pid_t vnr;
2214 struct pid *pid;
2215
Jeff Laytonae7eb162024-01-31 18:02:13 -05002216 if (fl->flc_flags & FL_OFDLCK)
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002217 return -1;
Jeff Layton3d40f782024-01-31 18:01:57 -05002218
2219 /* Remote locks report a negative pid value */
Jeff Laytonae7eb162024-01-31 18:02:13 -05002220 if (fl->flc_pid <= 0)
2221 return fl->flc_pid;
Jeff Layton3d40f782024-01-31 18:01:57 -05002222
Konstantin Khorenko826d7bc2018-06-08 17:27:11 +03002223 /*
2224 * If the flock owner process is dead and its pid has been already
2225 * freed, the translation below won't work, but we still want to show
2226 * flock owner pid number in init pidns.
2227 */
2228 if (ns == &init_pid_ns)
Jeff Laytonae7eb162024-01-31 18:02:13 -05002229 return (pid_t) fl->flc_pid;
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002230
2231 rcu_read_lock();
Jeff Laytonae7eb162024-01-31 18:02:13 -05002232 pid = find_pid_ns(fl->flc_pid, &init_pid_ns);
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002233 vnr = pid_nr_ns(pid, ns);
2234 rcu_read_unlock();
2235 return vnr;
2236}
2237
J. Bruce Fieldsc2fa1b82007-02-20 16:10:11 -05002238static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl)
2239{
Jeff Laytonae7eb162024-01-31 18:02:13 -05002240 flock->l_pid = locks_translate_pid(&fl->c, task_active_pid_ns(current));
J. Bruce Fieldsc2fa1b82007-02-20 16:10:11 -05002241#if BITS_PER_LONG == 32
2242 /*
2243 * Make sure we can represent the posix lock via
2244 * legacy 32bit flock.
2245 */
2246 if (fl->fl_start > OFFT_OFFSET_MAX)
2247 return -EOVERFLOW;
2248 if (fl->fl_end != OFFSET_MAX && fl->fl_end > OFFT_OFFSET_MAX)
2249 return -EOVERFLOW;
2250#endif
2251 flock->l_start = fl->fl_start;
2252 flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
2253 fl->fl_end - fl->fl_start + 1;
2254 flock->l_whence = 0;
Jeff Layton4ca52f52024-01-31 18:01:59 -05002255 flock->l_type = fl->c.flc_type;
J. Bruce Fieldsc2fa1b82007-02-20 16:10:11 -05002256 return 0;
2257}
2258
2259#if BITS_PER_LONG == 32
2260static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl)
2261{
Jeff Laytonae7eb162024-01-31 18:02:13 -05002262 flock->l_pid = locks_translate_pid(&fl->c, task_active_pid_ns(current));
J. Bruce Fieldsc2fa1b82007-02-20 16:10:11 -05002263 flock->l_start = fl->fl_start;
2264 flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
2265 fl->fl_end - fl->fl_start + 1;
2266 flock->l_whence = 0;
Jeff Layton4ca52f52024-01-31 18:01:59 -05002267 flock->l_type = fl->c.flc_type;
J. Bruce Fieldsc2fa1b82007-02-20 16:10:11 -05002268}
2269#endif
2270
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271/* Report the first existing lock that would conflict with l.
2272 * This implements the F_GETLK command of fcntl().
2273 */
Christoph Hellwiga75d30c2017-05-27 06:07:19 -04002274int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock *flock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275{
Benjamin Coddington52306e82017-07-16 10:28:21 -04002276 struct file_lock *fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 int error;
2278
Benjamin Coddington52306e82017-07-16 10:28:21 -04002279 fl = locks_alloc_lock();
2280 if (fl == NULL)
2281 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 error = -EINVAL;
Stas Sergeev6c9007f2023-06-22 21:52:23 +05002283 if (cmd != F_OFD_GETLK && flock->l_type != F_RDLCK
2284 && flock->l_type != F_WRLCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 goto out;
2286
Benjamin Coddington52306e82017-07-16 10:28:21 -04002287 error = flock_to_posix_lock(filp, fl, flock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 if (error)
2289 goto out;
2290
Jeff Layton0d3f7a22014-04-22 08:23:58 -04002291 if (cmd == F_OFD_GETLK) {
Jeff Layton90478932014-03-04 10:30:23 -05002292 error = -EINVAL;
Christoph Hellwiga75d30c2017-05-27 06:07:19 -04002293 if (flock->l_pid != 0)
Jeff Layton90478932014-03-04 10:30:23 -05002294 goto out;
2295
Jeff Layton4ca52f52024-01-31 18:01:59 -05002296 fl->c.flc_flags |= FL_OFDLCK;
2297 fl->c.flc_owner = filp;
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002298 }
2299
Benjamin Coddington52306e82017-07-16 10:28:21 -04002300 error = vfs_test_lock(filp, fl);
J. Bruce Fields3ee17ab2007-02-21 00:58:50 -05002301 if (error)
2302 goto out;
NeilBrown7bbd1fc2018-11-30 10:04:08 +11002303
Jeff Layton4ca52f52024-01-31 18:01:59 -05002304 flock->l_type = fl->c.flc_type;
2305 if (fl->c.flc_type != F_UNLCK) {
Benjamin Coddington52306e82017-07-16 10:28:21 -04002306 error = posix_lock_to_flock(flock, fl);
J. Bruce Fieldsc2fa1b82007-02-20 16:10:11 -05002307 if (error)
Benjamin Coddington52306e82017-07-16 10:28:21 -04002308 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310out:
Benjamin Coddington52306e82017-07-16 10:28:21 -04002311 locks_free_lock(fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 return error;
2313}
2314
Marc Eshel7723ec92007-01-18 15:08:55 -05002315/**
2316 * vfs_lock_file - file byte range lock
2317 * @filp: The file to apply the lock to
2318 * @cmd: type of locking operation (F_SETLK, F_GETLK, etc.)
2319 * @fl: The lock to be applied
Marc Eshel150b3932007-01-18 16:15:35 -05002320 * @conf: Place to return a copy of the conflicting lock, if found.
2321 *
2322 * A caller that doesn't care about the conflicting lock may pass NULL
2323 * as the final argument.
2324 *
2325 * If the filesystem defines a private ->lock() method, then @conf will
2326 * be left unchanged; so a caller that cares should initialize it to
2327 * some acceptable default.
Marc Eshel2beb6612006-12-05 23:31:28 -05002328 *
2329 * To avoid blocking kernel daemons, such as lockd, that need to acquire POSIX
2330 * locks, the ->lock() interface may return asynchronously, before the lock has
2331 * been granted or denied by the underlying filesystem, if (and only if)
Alexander Aringe70da172023-09-12 17:53:21 -04002332 * lm_grant is set. Additionally EXPORT_OP_ASYNC_LOCK in export_operations
2333 * flags need to be set.
2334 *
2335 * Callers expecting ->lock() to return asynchronously will only use F_SETLK,
2336 * not F_SETLKW; they will set FL_SLEEP if (and only if) the request is for a
2337 * blocking lock. When ->lock() does return asynchronously, it must return
2338 * FILE_LOCK_DEFERRED, and call ->lm_grant() when the lock request completes.
Marc Eshel2beb6612006-12-05 23:31:28 -05002339 * If the request is for non-blocking lock the file system should return
Miklos Szeredibde74e42008-07-25 01:48:57 -07002340 * FILE_LOCK_DEFERRED then try to get the lock and call the callback routine
2341 * with the result. If the request timed out the callback routine will return a
Marc Eshel2beb6612006-12-05 23:31:28 -05002342 * nonzero return code and the file system should release the lock. The file
2343 * system is also responsible to keep a corresponding posix lock when it
2344 * grants a lock so the VFS can find out which locks are locally held and do
2345 * the correct lock cleanup when required.
2346 * The underlying filesystem must not drop the kernel lock or call
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002347 * ->lm_grant() before returning to the caller with a FILE_LOCK_DEFERRED
Marc Eshel2beb6612006-12-05 23:31:28 -05002348 * return code.
Marc Eshel7723ec92007-01-18 15:08:55 -05002349 */
Marc Eshel150b3932007-01-18 16:15:35 -05002350int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf)
Marc Eshel7723ec92007-01-18 15:08:55 -05002351{
Jeff Layton4ca52f52024-01-31 18:01:59 -05002352 WARN_ON_ONCE(filp != fl->c.flc_file);
Miklos Szeredide2a4a52018-07-18 15:44:43 +02002353 if (filp->f_op->lock)
Marc Eshel7723ec92007-01-18 15:08:55 -05002354 return filp->f_op->lock(filp, cmd, fl);
2355 else
Marc Eshel150b3932007-01-18 16:15:35 -05002356 return posix_lock_file(filp, fl, conf);
Marc Eshel7723ec92007-01-18 15:08:55 -05002357}
2358EXPORT_SYMBOL_GPL(vfs_lock_file);
2359
Miklos Szeredib648a6d2008-07-25 01:48:57 -07002360static int do_lock_file_wait(struct file *filp, unsigned int cmd,
2361 struct file_lock *fl)
2362{
2363 int error;
2364
Jeff Layton4ca52f52024-01-31 18:01:59 -05002365 error = security_file_lock(filp, fl->c.flc_type);
Miklos Szeredib648a6d2008-07-25 01:48:57 -07002366 if (error)
2367 return error;
2368
Miklos Szeredi764c76b2008-07-25 01:48:58 -07002369 for (;;) {
2370 error = vfs_lock_file(filp, cmd, fl, NULL);
2371 if (error != FILE_LOCK_DEFERRED)
Miklos Szeredib648a6d2008-07-25 01:48:57 -07002372 break;
Jeff Layton4ca52f52024-01-31 18:01:59 -05002373 error = wait_event_interruptible(fl->c.flc_wait,
2374 list_empty(&fl->c.flc_blocked_member));
NeilBrown16306a62018-11-30 10:04:08 +11002375 if (error)
2376 break;
Miklos Szeredib648a6d2008-07-25 01:48:57 -07002377 }
NeilBrown16306a62018-11-30 10:04:08 +11002378 locks_delete_block(fl);
Miklos Szeredib648a6d2008-07-25 01:48:57 -07002379
2380 return error;
2381}
2382
Benjamin Coddington6ca7d912015-10-15 09:07:07 -04002383/* Ensure that fl->fl_file has compatible f_mode for F_SETLK calls */
Jeff Laytoncf01f4e2014-05-09 11:41:54 -04002384static int
2385check_fmode_for_setlk(struct file_lock *fl)
2386{
Jeff Layton4ca52f52024-01-31 18:01:59 -05002387 switch (fl->c.flc_type) {
Jeff Laytoncf01f4e2014-05-09 11:41:54 -04002388 case F_RDLCK:
Jeff Layton4ca52f52024-01-31 18:01:59 -05002389 if (!(fl->c.flc_file->f_mode & FMODE_READ))
Jeff Laytoncf01f4e2014-05-09 11:41:54 -04002390 return -EBADF;
2391 break;
2392 case F_WRLCK:
Jeff Layton4ca52f52024-01-31 18:01:59 -05002393 if (!(fl->c.flc_file->f_mode & FMODE_WRITE))
Jeff Laytoncf01f4e2014-05-09 11:41:54 -04002394 return -EBADF;
2395 }
2396 return 0;
2397}
2398
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399/* Apply the lock described by l to an open file descriptor.
2400 * This implements both the F_SETLK and F_SETLKW commands of fcntl().
2401 */
Peter Staubachc2936212005-07-27 11:45:09 -07002402int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd,
Christoph Hellwiga75d30c2017-05-27 06:07:19 -04002403 struct flock *flock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404{
2405 struct file_lock *file_lock = locks_alloc_lock();
Jeff Laytonc65454a2022-11-25 08:48:37 -05002406 struct inode *inode = file_inode(filp);
Al Viro0b2bac22008-05-06 13:58:34 -04002407 struct file *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 int error;
2409
2410 if (file_lock == NULL)
2411 return -ENOLCK;
2412
Christoph Hellwiga75d30c2017-05-27 06:07:19 -04002413 error = flock_to_posix_lock(filp, file_lock, flock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 if (error)
2415 goto out;
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002416
Jeff Laytoncf01f4e2014-05-09 11:41:54 -04002417 error = check_fmode_for_setlk(file_lock);
2418 if (error)
2419 goto out;
2420
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002421 /*
2422 * If the cmd is requesting file-private locks, then set the
Jeff Laytoncff2fce2014-04-22 08:24:32 -04002423 * FL_OFDLCK flag and override the owner.
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002424 */
2425 switch (cmd) {
Jeff Layton0d3f7a22014-04-22 08:23:58 -04002426 case F_OFD_SETLK:
Jeff Layton90478932014-03-04 10:30:23 -05002427 error = -EINVAL;
Christoph Hellwiga75d30c2017-05-27 06:07:19 -04002428 if (flock->l_pid != 0)
Jeff Layton90478932014-03-04 10:30:23 -05002429 goto out;
2430
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002431 cmd = F_SETLK;
Jeff Layton4ca52f52024-01-31 18:01:59 -05002432 file_lock->c.flc_flags |= FL_OFDLCK;
2433 file_lock->c.flc_owner = filp;
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002434 break;
Jeff Layton0d3f7a22014-04-22 08:23:58 -04002435 case F_OFD_SETLKW:
Jeff Layton90478932014-03-04 10:30:23 -05002436 error = -EINVAL;
Christoph Hellwiga75d30c2017-05-27 06:07:19 -04002437 if (flock->l_pid != 0)
Jeff Layton90478932014-03-04 10:30:23 -05002438 goto out;
2439
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002440 cmd = F_SETLKW;
Jeff Layton4ca52f52024-01-31 18:01:59 -05002441 file_lock->c.flc_flags |= FL_OFDLCK;
2442 file_lock->c.flc_owner = filp;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002443 fallthrough;
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002444 case F_SETLKW:
Jeff Layton4ca52f52024-01-31 18:01:59 -05002445 file_lock->c.flc_flags |= FL_SLEEP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 }
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002447
Miklos Szeredib648a6d2008-07-25 01:48:57 -07002448 error = do_lock_file_wait(filp, cmd, file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
Peter Staubachc2936212005-07-27 11:45:09 -07002450 /*
Jann Horn3cad1bc2024-07-02 18:26:52 +02002451 * Detect close/fcntl races and recover by zapping all POSIX locks
2452 * associated with this file and our files_struct, just like on
2453 * filp_flush(). There is no need to do that when we're
Jeff Layton0752ba802016-01-08 07:30:43 -05002454 * unlocking though, or for OFD locks.
Peter Staubachc2936212005-07-27 11:45:09 -07002455 */
Jeff Layton4ca52f52024-01-31 18:01:59 -05002456 if (!error && file_lock->c.flc_type != F_UNLCK &&
2457 !(file_lock->c.flc_flags & FL_OFDLCK)) {
Eric W. Biederman120ce2b2020-11-20 17:14:25 -06002458 struct files_struct *files = current->files;
Jeff Layton7f3697e2016-01-07 16:38:10 -05002459 /*
2460 * We need that spin_lock here - it prevents reordering between
2461 * update of i_flctx->flc_posix and check for it done in
2462 * close(). rcu_read_lock() wouldn't do.
2463 */
Eric W. Biederman120ce2b2020-11-20 17:14:25 -06002464 spin_lock(&files->file_lock);
2465 f = files_lookup_fd_locked(files, fd);
2466 spin_unlock(&files->file_lock);
Jeff Layton7f3697e2016-01-07 16:38:10 -05002467 if (f != filp) {
Jann Horn3cad1bc2024-07-02 18:26:52 +02002468 locks_remove_posix(filp, files);
Jeff Layton7f3697e2016-01-07 16:38:10 -05002469 error = -EBADF;
2470 }
Peter Staubachc2936212005-07-27 11:45:09 -07002471 }
Peter Staubachc2936212005-07-27 11:45:09 -07002472out:
Jeff Layton18909102016-01-06 21:26:10 -05002473 trace_fcntl_setlk(inode, file_lock, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 locks_free_lock(file_lock);
2475 return error;
2476}
2477
2478#if BITS_PER_LONG == 32
2479/* Report the first existing lock that would conflict with l.
2480 * This implements the F_GETLK command of fcntl().
2481 */
Christoph Hellwiga75d30c2017-05-27 06:07:19 -04002482int fcntl_getlk64(struct file *filp, unsigned int cmd, struct flock64 *flock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483{
Benjamin Coddington52306e82017-07-16 10:28:21 -04002484 struct file_lock *fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 int error;
2486
Benjamin Coddington52306e82017-07-16 10:28:21 -04002487 fl = locks_alloc_lock();
2488 if (fl == NULL)
2489 return -ENOMEM;
2490
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 error = -EINVAL;
Stas Sergeev6c9007f2023-06-22 21:52:23 +05002492 if (cmd != F_OFD_GETLK && flock->l_type != F_RDLCK
2493 && flock->l_type != F_WRLCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 goto out;
2495
Benjamin Coddington52306e82017-07-16 10:28:21 -04002496 error = flock64_to_posix_lock(filp, fl, flock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 if (error)
2498 goto out;
2499
Jeff Layton0d3f7a22014-04-22 08:23:58 -04002500 if (cmd == F_OFD_GETLK) {
Jeff Layton90478932014-03-04 10:30:23 -05002501 error = -EINVAL;
Christoph Hellwiga75d30c2017-05-27 06:07:19 -04002502 if (flock->l_pid != 0)
Jeff Layton90478932014-03-04 10:30:23 -05002503 goto out;
2504
Jeff Layton4ca52f52024-01-31 18:01:59 -05002505 fl->c.flc_flags |= FL_OFDLCK;
2506 fl->c.flc_owner = filp;
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002507 }
2508
Benjamin Coddington52306e82017-07-16 10:28:21 -04002509 error = vfs_test_lock(filp, fl);
J. Bruce Fields3ee17ab2007-02-21 00:58:50 -05002510 if (error)
2511 goto out;
2512
Jeff Layton4ca52f52024-01-31 18:01:59 -05002513 flock->l_type = fl->c.flc_type;
2514 if (fl->c.flc_type != F_UNLCK)
Benjamin Coddington52306e82017-07-16 10:28:21 -04002515 posix_lock_to_flock64(flock, fl);
Kinglong Meef3282962014-08-22 10:18:43 -04002516
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517out:
Benjamin Coddington52306e82017-07-16 10:28:21 -04002518 locks_free_lock(fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 return error;
2520}
2521
2522/* Apply the lock described by l to an open file descriptor.
2523 * This implements both the F_SETLK and F_SETLKW commands of fcntl().
2524 */
Peter Staubachc2936212005-07-27 11:45:09 -07002525int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd,
Christoph Hellwiga75d30c2017-05-27 06:07:19 -04002526 struct flock64 *flock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527{
2528 struct file_lock *file_lock = locks_alloc_lock();
Al Viro0b2bac22008-05-06 13:58:34 -04002529 struct file *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 int error;
2531
2532 if (file_lock == NULL)
2533 return -ENOLCK;
2534
Christoph Hellwiga75d30c2017-05-27 06:07:19 -04002535 error = flock64_to_posix_lock(filp, file_lock, flock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 if (error)
2537 goto out;
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002538
Jeff Laytoncf01f4e2014-05-09 11:41:54 -04002539 error = check_fmode_for_setlk(file_lock);
2540 if (error)
2541 goto out;
2542
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002543 /*
2544 * If the cmd is requesting file-private locks, then set the
Jeff Laytoncff2fce2014-04-22 08:24:32 -04002545 * FL_OFDLCK flag and override the owner.
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002546 */
2547 switch (cmd) {
Jeff Layton0d3f7a22014-04-22 08:23:58 -04002548 case F_OFD_SETLK:
Jeff Layton90478932014-03-04 10:30:23 -05002549 error = -EINVAL;
Christoph Hellwiga75d30c2017-05-27 06:07:19 -04002550 if (flock->l_pid != 0)
Jeff Layton90478932014-03-04 10:30:23 -05002551 goto out;
2552
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002553 cmd = F_SETLK64;
Jeff Layton4ca52f52024-01-31 18:01:59 -05002554 file_lock->c.flc_flags |= FL_OFDLCK;
2555 file_lock->c.flc_owner = filp;
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002556 break;
Jeff Layton0d3f7a22014-04-22 08:23:58 -04002557 case F_OFD_SETLKW:
Jeff Layton90478932014-03-04 10:30:23 -05002558 error = -EINVAL;
Christoph Hellwiga75d30c2017-05-27 06:07:19 -04002559 if (flock->l_pid != 0)
Jeff Layton90478932014-03-04 10:30:23 -05002560 goto out;
2561
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002562 cmd = F_SETLKW64;
Jeff Layton4ca52f52024-01-31 18:01:59 -05002563 file_lock->c.flc_flags |= FL_OFDLCK;
2564 file_lock->c.flc_owner = filp;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002565 fallthrough;
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002566 case F_SETLKW64:
Jeff Layton4ca52f52024-01-31 18:01:59 -05002567 file_lock->c.flc_flags |= FL_SLEEP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 }
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002569
Miklos Szeredib648a6d2008-07-25 01:48:57 -07002570 error = do_lock_file_wait(filp, cmd, file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Peter Staubachc2936212005-07-27 11:45:09 -07002572 /*
Jann Hornf8138f22024-07-23 17:03:56 +02002573 * Detect close/fcntl races and recover by zapping all POSIX locks
2574 * associated with this file and our files_struct, just like on
2575 * filp_flush(). There is no need to do that when we're
Jeff Layton0752ba802016-01-08 07:30:43 -05002576 * unlocking though, or for OFD locks.
Peter Staubachc2936212005-07-27 11:45:09 -07002577 */
Jeff Layton4ca52f52024-01-31 18:01:59 -05002578 if (!error && file_lock->c.flc_type != F_UNLCK &&
2579 !(file_lock->c.flc_flags & FL_OFDLCK)) {
Eric W. Biederman120ce2b2020-11-20 17:14:25 -06002580 struct files_struct *files = current->files;
Jeff Layton7f3697e2016-01-07 16:38:10 -05002581 /*
2582 * We need that spin_lock here - it prevents reordering between
2583 * update of i_flctx->flc_posix and check for it done in
2584 * close(). rcu_read_lock() wouldn't do.
2585 */
Eric W. Biederman120ce2b2020-11-20 17:14:25 -06002586 spin_lock(&files->file_lock);
2587 f = files_lookup_fd_locked(files, fd);
2588 spin_unlock(&files->file_lock);
Jeff Layton7f3697e2016-01-07 16:38:10 -05002589 if (f != filp) {
Jann Hornf8138f22024-07-23 17:03:56 +02002590 locks_remove_posix(filp, files);
Jeff Layton7f3697e2016-01-07 16:38:10 -05002591 error = -EBADF;
2592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594out:
2595 locks_free_lock(file_lock);
2596 return error;
2597}
2598#endif /* BITS_PER_LONG == 32 */
2599
2600/*
2601 * This function is called when the file is being removed
2602 * from the task's fd array. POSIX locks belonging to this task
2603 * are deleted at this time.
2604 */
2605void locks_remove_posix(struct file *filp, fl_owner_t owner)
2606{
Jeff Layton18909102016-01-06 21:26:10 -05002607 int error;
Jeff Laytonc65454a2022-11-25 08:48:37 -05002608 struct inode *inode = file_inode(filp);
Miklos Szerediff7b86b2006-06-23 02:05:11 -07002609 struct file_lock lock;
Dmitry Vyukov128a3782015-09-21 09:43:06 +02002610 struct file_lock_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
2612 /*
2613 * If there are no locks held on this file, we don't need to call
2614 * posix_lock_file(). Another process could be setting a lock on this
2615 * file at the same time, but we wouldn't remove that lock anyway.
2616 */
Jeff Layton401a8b82022-11-16 09:02:30 -05002617 ctx = locks_inode_context(inode);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05002618 if (!ctx || list_empty(&ctx->flc_posix))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 return;
2620
NeilBrownd6367d62018-11-30 10:04:08 +11002621 locks_init_lock(&lock);
Jeff Layton4ca52f52024-01-31 18:01:59 -05002622 lock.c.flc_type = F_UNLCK;
2623 lock.c.flc_flags = FL_POSIX | FL_CLOSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 lock.fl_start = 0;
2625 lock.fl_end = OFFSET_MAX;
Jeff Layton4ca52f52024-01-31 18:01:59 -05002626 lock.c.flc_owner = owner;
2627 lock.c.flc_pid = current->tgid;
2628 lock.c.flc_file = filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 lock.fl_ops = NULL;
2630 lock.fl_lmops = NULL;
2631
Jeff Layton18909102016-01-06 21:26:10 -05002632 error = vfs_lock_file(filp, F_SETLK, &lock, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 if (lock.fl_ops && lock.fl_ops->fl_release_private)
2635 lock.fl_ops->fl_release_private(&lock);
Miklos Szeredic568d682016-09-16 12:44:20 +02002636 trace_locks_remove_posix(inode, &lock, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638EXPORT_SYMBOL(locks_remove_posix);
2639
Jeff Layton3d8e5602015-01-16 15:05:58 -05002640/* The i_flctx must be valid when calling into here */
Jeff Laytondd459bb2015-01-16 15:05:54 -05002641static void
Dmitry Vyukov128a3782015-09-21 09:43:06 +02002642locks_remove_flock(struct file *filp, struct file_lock_context *flctx)
Jeff Laytondd459bb2015-01-16 15:05:54 -05002643{
NeilBrownd6367d62018-11-30 10:04:08 +11002644 struct file_lock fl;
Jeff Laytonc65454a2022-11-25 08:48:37 -05002645 struct inode *inode = file_inode(filp);
Jeff Laytondd459bb2015-01-16 15:05:54 -05002646
Jeff Layton3d8e5602015-01-16 15:05:58 -05002647 if (list_empty(&flctx->flc_flock))
Jeff Laytondd459bb2015-01-16 15:05:54 -05002648 return;
2649
Kuniyuki Iwashima4149be72022-07-16 21:35:31 -07002650 flock_make_lock(filp, &fl, F_UNLCK);
Jeff Layton4ca52f52024-01-31 18:01:59 -05002651 fl.c.flc_flags |= FL_CLOSE;
NeilBrownd6367d62018-11-30 10:04:08 +11002652
Miklos Szeredide2a4a52018-07-18 15:44:43 +02002653 if (filp->f_op->flock)
Jeff Laytondd459bb2015-01-16 15:05:54 -05002654 filp->f_op->flock(filp, F_SETLKW, &fl);
2655 else
Jeff Laytonbcd7f78d2015-07-11 06:43:02 -04002656 flock_lock_inode(inode, &fl);
Jeff Laytondd459bb2015-01-16 15:05:54 -05002657
2658 if (fl.fl_ops && fl.fl_ops->fl_release_private)
2659 fl.fl_ops->fl_release_private(&fl);
2660}
2661
Jeff Layton3d8e5602015-01-16 15:05:58 -05002662/* The i_flctx must be valid when calling into here */
Jeff Layton8634b512015-01-16 15:05:55 -05002663static void
Dmitry Vyukov128a3782015-09-21 09:43:06 +02002664locks_remove_lease(struct file *filp, struct file_lock_context *ctx)
Jeff Layton8634b512015-01-16 15:05:55 -05002665{
Jeff Laytonc69ff402024-01-31 18:02:28 -05002666 struct file_lease *fl, *tmp;
Jeff Layton8634b512015-01-16 15:05:55 -05002667 LIST_HEAD(dispose);
2668
Jeff Layton3d8e5602015-01-16 15:05:58 -05002669 if (list_empty(&ctx->flc_lease))
Jeff Layton8634b512015-01-16 15:05:55 -05002670 return;
2671
Peter Zijlstra02e525b22019-02-21 15:38:40 +01002672 percpu_down_read(&file_rwsem);
Jeff Layton6109c852015-01-16 15:05:57 -05002673 spin_lock(&ctx->flc_lock);
Jeff Layton4ca52f52024-01-31 18:01:59 -05002674 list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, c.flc_list)
2675 if (filp == fl->c.flc_file)
Jeff Laytonc4e136c2015-02-16 19:37:42 -05002676 lease_modify(fl, F_UNLCK, &dispose);
Jeff Layton6109c852015-01-16 15:05:57 -05002677 spin_unlock(&ctx->flc_lock);
Peter Zijlstra02e525b22019-02-21 15:38:40 +01002678 percpu_up_read(&file_rwsem);
Peter Zijlstra5f430862016-10-08 10:12:28 +02002679
Jeff Layton8634b512015-01-16 15:05:55 -05002680 locks_dispose_list(&dispose);
2681}
2682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683/*
2684 * This function is called on the last close of an open file.
2685 */
Jeff Layton78ed8a12014-02-03 12:13:08 -05002686void locks_remove_file(struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687{
Dmitry Vyukov128a3782015-09-21 09:43:06 +02002688 struct file_lock_context *ctx;
2689
Jeff Laytonc65454a2022-11-25 08:48:37 -05002690 ctx = locks_inode_context(file_inode(filp));
Dmitry Vyukov128a3782015-09-21 09:43:06 +02002691 if (!ctx)
Jeff Layton3d8e5602015-01-16 15:05:58 -05002692 return;
2693
Jeff Laytondd459bb2015-01-16 15:05:54 -05002694 /* remove any OFD locks */
Christoph Hellwig73a8f5f2014-07-13 17:00:38 +02002695 locks_remove_posix(filp, filp);
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002696
Jeff Laytondd459bb2015-01-16 15:05:54 -05002697 /* remove flock locks */
Dmitry Vyukov128a3782015-09-21 09:43:06 +02002698 locks_remove_flock(filp, ctx);
Jeff Laytondd459bb2015-01-16 15:05:54 -05002699
Jeff Layton8634b512015-01-16 15:05:55 -05002700 /* remove any leases */
Dmitry Vyukov128a3782015-09-21 09:43:06 +02002701 locks_remove_lease(filp, ctx);
Benjamin Coddington39537042017-07-21 13:36:25 -04002702
2703 spin_lock(&ctx->flc_lock);
2704 locks_check_ctx_file_list(filp, &ctx->flc_posix, "POSIX");
2705 locks_check_ctx_file_list(filp, &ctx->flc_flock, "FLOCK");
2706 locks_check_ctx_file_list(filp, &ctx->flc_lease, "LEASE");
2707 spin_unlock(&ctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708}
2709
2710/**
Marc Eshel9b9d2ab2007-01-18 17:52:58 -05002711 * vfs_cancel_lock - file byte range unblock lock
2712 * @filp: The file to apply the unblock to
2713 * @fl: The lock to be unblocked
2714 *
2715 * Used by lock managers to cancel blocked requests
2716 */
2717int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
2718{
Jeff Layton4ca52f52024-01-31 18:01:59 -05002719 WARN_ON_ONCE(filp != fl->c.flc_file);
Miklos Szeredide2a4a52018-07-18 15:44:43 +02002720 if (filp->f_op->lock)
Marc Eshel9b9d2ab2007-01-18 17:52:58 -05002721 return filp->f_op->lock(filp, F_CANCELLK, fl);
2722 return 0;
2723}
Marc Eshel9b9d2ab2007-01-18 17:52:58 -05002724EXPORT_SYMBOL_GPL(vfs_cancel_lock);
2725
Jeff Laytonab1ddef2022-11-14 08:33:09 -05002726/**
2727 * vfs_inode_has_locks - are any file locks held on @inode?
2728 * @inode: inode to check for locks
2729 *
2730 * Return true if there are any FL_POSIX or FL_FLOCK locks currently
2731 * set on @inode.
2732 */
2733bool vfs_inode_has_locks(struct inode *inode)
2734{
2735 struct file_lock_context *ctx;
2736 bool ret;
2737
Jeff Layton401a8b82022-11-16 09:02:30 -05002738 ctx = locks_inode_context(inode);
Jeff Laytonab1ddef2022-11-14 08:33:09 -05002739 if (!ctx)
2740 return false;
2741
2742 spin_lock(&ctx->flc_lock);
2743 ret = !list_empty(&ctx->flc_posix) || !list_empty(&ctx->flc_flock);
2744 spin_unlock(&ctx->flc_lock);
2745 return ret;
2746}
2747EXPORT_SYMBOL_GPL(vfs_inode_has_locks);
2748
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002749#ifdef CONFIG_PROC_FS
Alexey Dobriyand8ba7a32008-10-04 22:34:18 +04002750#include <linux/proc_fs.h>
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002751#include <linux/seq_file.h>
2752
Jeff Layton7012b022013-06-21 08:58:22 -04002753struct locks_iterator {
2754 int li_cpu;
2755 loff_t li_pos;
2756};
2757
Jeff Laytona1c2af32024-01-31 18:02:14 -05002758static void lock_get_status(struct seq_file *f, struct file_lock_core *flc,
Luo Longjunb8da9b12021-02-25 22:58:29 -05002759 loff_t id, char *pfx, int repeat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760{
2761 struct inode *inode = NULL;
Jeff Layton6021d622024-01-31 18:01:44 -05002762 unsigned int pid;
Alexey Gladkov9d78ede2020-05-18 20:07:38 +02002763 struct pid_namespace *proc_pidns = proc_pid_ns(file_inode(f->file)->i_sb);
Jeff Laytona1c2af32024-01-31 18:02:14 -05002764 int type = flc->flc_type;
2765 struct file_lock *fl = file_lock(flc);
Vitaliy Gusevab1f1612008-01-17 00:07:08 +00002766
Jeff Laytona1c2af32024-01-31 18:02:14 -05002767 pid = locks_translate_pid(flc, proc_pidns);
2768
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002769 /*
Konstantin Khorenko1cf8e5d2018-06-08 17:27:12 +03002770 * If lock owner is dead (and pid is freed) or not visible in current
2771 * pidns, zero is shown as a pid value. Check lock info from
2772 * init_pid_ns to get saved lock pid value.
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002773 */
Jeff Laytona1c2af32024-01-31 18:02:14 -05002774 if (flc->flc_file != NULL)
2775 inode = file_inode(flc->flc_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
Luo Longjunb8da9b12021-02-25 22:58:29 -05002777 seq_printf(f, "%lld: ", id);
2778
2779 if (repeat)
2780 seq_printf(f, "%*s", repeat - 1 + (int)strlen(pfx), pfx);
2781
Jeff Laytona1c2af32024-01-31 18:02:14 -05002782 if (flc->flc_flags & FL_POSIX) {
2783 if (flc->flc_flags & FL_ACCESS)
Fabian Frederick5315c262014-05-09 14:13:05 -04002784 seq_puts(f, "ACCESS");
Jeff Laytona1c2af32024-01-31 18:02:14 -05002785 else if (flc->flc_flags & FL_OFDLCK)
Fabian Frederick5315c262014-05-09 14:13:05 -04002786 seq_puts(f, "OFDLCK");
Jeff Laytonc918d422014-02-03 12:13:09 -05002787 else
Fabian Frederick5315c262014-05-09 14:13:05 -04002788 seq_puts(f, "POSIX ");
Jeff Laytonc918d422014-02-03 12:13:09 -05002789
2790 seq_printf(f, " %s ",
Jeff Laytonf7e33bd2021-08-19 14:56:38 -04002791 (inode == NULL) ? "*NOINODE*" : "ADVISORY ");
Jeff Laytona1c2af32024-01-31 18:02:14 -05002792 } else if (flc->flc_flags & FL_FLOCK) {
Jeff Layton90f7d7a2021-09-10 15:36:29 -04002793 seq_puts(f, "FLOCK ADVISORY ");
Jeff Laytona1c2af32024-01-31 18:02:14 -05002794 } else if (flc->flc_flags & (FL_LEASE|FL_DELEG|FL_LAYOUT)) {
Jeff Laytonc69ff402024-01-31 18:02:28 -05002795 struct file_lease *lease = file_lease(flc);
2796
2797 type = target_leasetype(lease);
Jeff Layton3d40f782024-01-31 18:01:57 -05002798
Jeff Laytona1c2af32024-01-31 18:02:14 -05002799 if (flc->flc_flags & FL_DELEG)
Jeff Layton8144f1f2014-08-11 13:36:54 -04002800 seq_puts(f, "DELEG ");
2801 else
2802 seq_puts(f, "LEASE ");
2803
Jeff Laytonc69ff402024-01-31 18:02:28 -05002804 if (lease_breaking(lease))
Fabian Frederick5315c262014-05-09 14:13:05 -04002805 seq_puts(f, "BREAKING ");
Jeff Laytona1c2af32024-01-31 18:02:14 -05002806 else if (flc->flc_file)
Fabian Frederick5315c262014-05-09 14:13:05 -04002807 seq_puts(f, "ACTIVE ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 else
Fabian Frederick5315c262014-05-09 14:13:05 -04002809 seq_puts(f, "BREAKER ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 } else {
Fabian Frederick5315c262014-05-09 14:13:05 -04002811 seq_puts(f, "UNKNOWN UNKNOWN ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 }
Pavel Begunkov43e4cb92019-07-24 20:16:31 +03002813
Jeff Layton90f7d7a2021-09-10 15:36:29 -04002814 seq_printf(f, "%s ", (type == F_WRLCK) ? "WRITE" :
2815 (type == F_RDLCK) ? "READ" : "UNLCK");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 if (inode) {
Jeff Layton36488882015-04-03 09:04:04 -04002817 /* userspace relies on this representation of dev_t */
Jeff Layton6021d622024-01-31 18:01:44 -05002818 seq_printf(f, "%d %02x:%02x:%lu ", pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 MAJOR(inode->i_sb->s_dev),
2820 MINOR(inode->i_sb->s_dev), inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 } else {
Jeff Layton6021d622024-01-31 18:01:44 -05002822 seq_printf(f, "%d <none>:0 ", pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 }
Jeff Laytona1c2af32024-01-31 18:02:14 -05002824 if (flc->flc_flags & FL_POSIX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 if (fl->fl_end == OFFSET_MAX)
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002826 seq_printf(f, "%Ld EOF\n", fl->fl_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 else
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002828 seq_printf(f, "%Ld %Ld\n", fl->fl_start, fl->fl_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 } else {
Fabian Frederick5315c262014-05-09 14:13:05 -04002830 seq_puts(f, "0 EOF\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 }
2832}
2833
Jeff Laytona1c2af32024-01-31 18:02:14 -05002834static struct file_lock_core *get_next_blocked_member(struct file_lock_core *node)
Luo Longjunb8da9b12021-02-25 22:58:29 -05002835{
Jeff Laytona1c2af32024-01-31 18:02:14 -05002836 struct file_lock_core *tmp;
Luo Longjunb8da9b12021-02-25 22:58:29 -05002837
2838 /* NULL node or root node */
Jeff Laytona1c2af32024-01-31 18:02:14 -05002839 if (node == NULL || node->flc_blocker == NULL)
Luo Longjunb8da9b12021-02-25 22:58:29 -05002840 return NULL;
2841
2842 /* Next member in the linked list could be itself */
Jeff Laytona1c2af32024-01-31 18:02:14 -05002843 tmp = list_next_entry(node, flc_blocked_member);
2844 if (list_entry_is_head(tmp, &node->flc_blocker->flc_blocked_requests,
2845 flc_blocked_member)
Jeff Laytonb6aaba52024-01-31 18:02:07 -05002846 || tmp == node) {
Luo Longjunb8da9b12021-02-25 22:58:29 -05002847 return NULL;
2848 }
2849
2850 return tmp;
2851}
2852
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002853static int locks_show(struct seq_file *f, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854{
Jeff Layton7012b022013-06-21 08:58:22 -04002855 struct locks_iterator *iter = f->private;
Jeff Laytona1c2af32024-01-31 18:02:14 -05002856 struct file_lock_core *cur, *tmp;
Alexey Gladkov9d78ede2020-05-18 20:07:38 +02002857 struct pid_namespace *proc_pidns = proc_pid_ns(file_inode(f->file)->i_sb);
Luo Longjunb8da9b12021-02-25 22:58:29 -05002858 int level = 0;
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002859
Jeff Laytona1c2af32024-01-31 18:02:14 -05002860 cur = hlist_entry(v, struct file_lock_core, flc_link);
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002861
Jeff Laytona1c2af32024-01-31 18:02:14 -05002862 if (locks_translate_pid(cur, proc_pidns) == 0)
Nikolay Borisovd67fd442016-08-17 16:18:46 -04002863 return 0;
2864
Jeff Laytona1c2af32024-01-31 18:02:14 -05002865 /* View this crossed linked list as a binary tree, the first member of flc_blocked_requests
Jeff Layton4ca52f52024-01-31 18:01:59 -05002866 * is the left child of current node, the next silibing in flc_blocked_member is the
Jeff Laytona1c2af32024-01-31 18:02:14 -05002867 * right child, we can alse get the parent of current node from flc_blocker, so this
Luo Longjunb8da9b12021-02-25 22:58:29 -05002868 * question becomes traversal of a binary tree
2869 */
2870 while (cur != NULL) {
2871 if (level)
2872 lock_get_status(f, cur, iter->li_pos, "-> ", level);
2873 else
2874 lock_get_status(f, cur, iter->li_pos, "", level);
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002875
Jeff Laytona1c2af32024-01-31 18:02:14 -05002876 if (!list_empty(&cur->flc_blocked_requests)) {
Luo Longjunb8da9b12021-02-25 22:58:29 -05002877 /* Turn left */
Jeff Laytona1c2af32024-01-31 18:02:14 -05002878 cur = list_first_entry_or_null(&cur->flc_blocked_requests,
2879 struct file_lock_core,
2880 flc_blocked_member);
Luo Longjunb8da9b12021-02-25 22:58:29 -05002881 level++;
2882 } else {
2883 /* Turn right */
2884 tmp = get_next_blocked_member(cur);
2885 /* Fall back to parent node */
Jeff Laytona1c2af32024-01-31 18:02:14 -05002886 while (tmp == NULL && cur->flc_blocker != NULL) {
2887 cur = cur->flc_blocker;
Luo Longjunb8da9b12021-02-25 22:58:29 -05002888 level--;
2889 tmp = get_next_blocked_member(cur);
2890 }
2891 cur = tmp;
2892 }
2893 }
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002894
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002895 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896}
2897
Andrey Vagin6c8c9032015-04-16 12:49:38 -07002898static void __show_fd_locks(struct seq_file *f,
2899 struct list_head *head, int *id,
2900 struct file *filp, struct files_struct *files)
2901{
Jeff Laytona1c2af32024-01-31 18:02:14 -05002902 struct file_lock_core *fl;
Andrey Vagin6c8c9032015-04-16 12:49:38 -07002903
Jeff Laytona1c2af32024-01-31 18:02:14 -05002904 list_for_each_entry(fl, head, flc_list) {
Andrey Vagin6c8c9032015-04-16 12:49:38 -07002905
Jeff Laytona1c2af32024-01-31 18:02:14 -05002906 if (filp != fl->flc_file)
Andrey Vagin6c8c9032015-04-16 12:49:38 -07002907 continue;
Jeff Laytona1c2af32024-01-31 18:02:14 -05002908 if (fl->flc_owner != files && fl->flc_owner != filp)
Andrey Vagin6c8c9032015-04-16 12:49:38 -07002909 continue;
2910
2911 (*id)++;
2912 seq_puts(f, "lock:\t");
Luo Longjunb8da9b12021-02-25 22:58:29 -05002913 lock_get_status(f, fl, *id, "", 0);
Andrey Vagin6c8c9032015-04-16 12:49:38 -07002914 }
2915}
2916
2917void show_fd_locks(struct seq_file *f,
2918 struct file *filp, struct files_struct *files)
2919{
Jeff Laytonc65454a2022-11-25 08:48:37 -05002920 struct inode *inode = file_inode(filp);
Andrey Vagin6c8c9032015-04-16 12:49:38 -07002921 struct file_lock_context *ctx;
2922 int id = 0;
2923
Jeff Layton401a8b82022-11-16 09:02:30 -05002924 ctx = locks_inode_context(inode);
Andrey Vagin6c8c9032015-04-16 12:49:38 -07002925 if (!ctx)
2926 return;
2927
2928 spin_lock(&ctx->flc_lock);
2929 __show_fd_locks(f, &ctx->flc_flock, &id, filp, files);
2930 __show_fd_locks(f, &ctx->flc_posix, &id, filp, files);
2931 __show_fd_locks(f, &ctx->flc_lease, &id, filp, files);
2932 spin_unlock(&ctx->flc_lock);
2933}
2934
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002935static void *locks_start(struct seq_file *f, loff_t *pos)
Jeff Laytonb03dfdec2014-02-03 12:13:07 -05002936 __acquires(&blocked_lock_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937{
Jeff Layton7012b022013-06-21 08:58:22 -04002938 struct locks_iterator *iter = f->private;
Jerome Marchand99dc8292010-10-26 14:22:33 -07002939
Jeff Layton7012b022013-06-21 08:58:22 -04002940 iter->li_pos = *pos + 1;
Peter Zijlstraaba37662015-06-22 14:16:33 +02002941 percpu_down_write(&file_rwsem);
Jeff Layton7b2296a2013-06-21 08:58:20 -04002942 spin_lock(&blocked_lock_lock);
Peter Zijlstra7c3f6542015-06-22 14:16:34 +02002943 return seq_hlist_start_percpu(&file_lock_list.hlist, &iter->li_cpu, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944}
2945
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002946static void *locks_next(struct seq_file *f, void *v, loff_t *pos)
2947{
Jeff Layton7012b022013-06-21 08:58:22 -04002948 struct locks_iterator *iter = f->private;
2949
2950 ++iter->li_pos;
Peter Zijlstra7c3f6542015-06-22 14:16:34 +02002951 return seq_hlist_next_percpu(v, &file_lock_list.hlist, &iter->li_cpu, pos);
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002952}
2953
2954static void locks_stop(struct seq_file *f, void *v)
Jeff Laytonb03dfdec2014-02-03 12:13:07 -05002955 __releases(&blocked_lock_lock)
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002956{
Jeff Layton7b2296a2013-06-21 08:58:20 -04002957 spin_unlock(&blocked_lock_lock);
Peter Zijlstraaba37662015-06-22 14:16:33 +02002958 percpu_up_write(&file_rwsem);
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002959}
2960
Alexey Dobriyand8ba7a32008-10-04 22:34:18 +04002961static const struct seq_operations locks_seq_operations = {
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002962 .start = locks_start,
2963 .next = locks_next,
2964 .stop = locks_stop,
2965 .show = locks_show,
2966};
Alexey Dobriyand8ba7a32008-10-04 22:34:18 +04002967
Alexey Dobriyand8ba7a32008-10-04 22:34:18 +04002968static int __init proc_locks_init(void)
2969{
Christoph Hellwig44414d82018-04-24 17:05:17 +02002970 proc_create_seq_private("locks", 0, NULL, &locks_seq_operations,
2971 sizeof(struct locks_iterator), NULL);
Alexey Dobriyand8ba7a32008-10-04 22:34:18 +04002972 return 0;
2973}
Paul Gortmaker91899222015-12-17 14:11:03 -05002974fs_initcall(proc_locks_init);
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002975#endif
2976
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977static int __init filelock_init(void)
2978{
Jeff Layton7012b022013-06-21 08:58:22 -04002979 int i;
2980
Jeff Layton4a075e32015-01-16 15:05:54 -05002981 flctx_cache = kmem_cache_create("file_lock_ctx",
Linus Torvalds37547072021-09-07 11:21:48 -07002982 sizeof(struct file_lock_context), 0, SLAB_PANIC, NULL);
Jeff Layton4a075e32015-01-16 15:05:54 -05002983
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 filelock_cache = kmem_cache_create("file_lock_cache",
Linus Torvalds37547072021-09-07 11:21:48 -07002985 sizeof(struct file_lock), 0, SLAB_PANIC, NULL);
Miklos Szerediee19cc42011-07-07 13:06:09 +02002986
Jeff Laytonc69ff402024-01-31 18:02:28 -05002987 filelease_cache = kmem_cache_create("file_lock_cache",
2988 sizeof(struct file_lease), 0, SLAB_PANIC, NULL);
2989
Peter Zijlstra7c3f6542015-06-22 14:16:34 +02002990 for_each_possible_cpu(i) {
2991 struct file_lock_list_struct *fll = per_cpu_ptr(&file_lock_list, i);
2992
2993 spin_lock_init(&fll->lock);
2994 INIT_HLIST_HEAD(&fll->hlist);
2995 }
Jeff Layton7012b022013-06-21 08:58:22 -04002996
Jeff Layton18f66222019-08-18 14:18:45 -04002997 lease_notifier_chain_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 return 0;
2999}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000core_initcall(filelock_init);