Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/fs/locks.c |
| 4 | * |
J. Bruce Fields | e9728cc | 2021-10-19 13:38:35 -0400 | [diff] [blame] | 5 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * |
NeilBrown | fd7732e | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 10 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include <linux/capability.h> |
| 52 | #include <linux/file.h> |
Al Viro | 9f3acc3 | 2008-04-24 07:44:08 -0400 | [diff] [blame] | 53 | #include <linux/fdtable.h> |
Jeff Layton | 5970e15 | 2022-11-20 09:15:34 -0500 | [diff] [blame] | 54 | #include <linux/filelock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #include <linux/fs.h> |
| 56 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | #include <linux/security.h> |
| 58 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #include <linux/syscalls.h> |
| 60 | #include <linux/time.h> |
Dipankar Sarma | 4fb3a53 | 2005-09-16 19:28:13 -0700 | [diff] [blame] | 61 | #include <linux/rcupdate.h> |
Vitaliy Gusev | ab1f161 | 2008-01-17 00:07:08 +0000 | [diff] [blame] | 62 | #include <linux/pid_namespace.h> |
Jeff Layton | 48f7418 | 2013-06-21 08:58:18 -0400 | [diff] [blame] | 63 | #include <linux/hashtable.h> |
Jeff Layton | 7012b02 | 2013-06-21 08:58:22 -0400 | [diff] [blame] | 64 | #include <linux/percpu.h> |
Luis Chamberlain | dd81faa | 2022-01-21 22:13:10 -0800 | [diff] [blame] | 65 | #include <linux/sysctl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Jeff Layton | 62af4f1 | 2014-05-09 14:13:05 -0400 | [diff] [blame] | 67 | #define CREATE_TRACE_POINTS |
| 68 | #include <trace/events/filelock.h> |
| 69 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 70 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Jeff Layton | 1a62c22 | 2024-01-31 18:02:05 -0500 | [diff] [blame] | 72 | static struct file_lock *file_lock(struct file_lock_core *flc) |
| 73 | { |
| 74 | return container_of(flc, struct file_lock, c); |
| 75 | } |
| 76 | |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 77 | static struct file_lease *file_lease(struct file_lock_core *flc) |
| 78 | { |
| 79 | return container_of(flc, struct file_lease, c); |
| 80 | } |
| 81 | |
| 82 | static bool lease_breaking(struct file_lease *fl) |
J. Bruce Fields | ab83fa4 | 2011-07-26 20:10:51 -0400 | [diff] [blame] | 83 | { |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 84 | return fl->c.flc_flags & (FL_UNLOCK_PENDING | FL_DOWNGRADE_PENDING); |
J. Bruce Fields | 778fc54 | 2011-07-26 18:25:49 -0400 | [diff] [blame] | 85 | } |
| 86 | |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 87 | static int target_leasetype(struct file_lease *fl) |
J. Bruce Fields | 778fc54 | 2011-07-26 18:25:49 -0400 | [diff] [blame] | 88 | { |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 89 | if (fl->c.flc_flags & FL_UNLOCK_PENDING) |
J. Bruce Fields | 778fc54 | 2011-07-26 18:25:49 -0400 | [diff] [blame] | 90 | return F_UNLCK; |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 91 | if (fl->c.flc_flags & FL_DOWNGRADE_PENDING) |
J. Bruce Fields | 778fc54 | 2011-07-26 18:25:49 -0400 | [diff] [blame] | 92 | return F_RDLCK; |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 93 | return fl->c.flc_type; |
J. Bruce Fields | ab83fa4 | 2011-07-26 20:10:51 -0400 | [diff] [blame] | 94 | } |
| 95 | |
Luis Chamberlain | dd81faa | 2022-01-21 22:13:10 -0800 | [diff] [blame] | 96 | static int leases_enable = 1; |
| 97 | static int lease_break_time = 45; |
| 98 | |
| 99 | #ifdef CONFIG_SYSCTL |
| 100 | static 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 Chamberlain | dd81faa | 2022-01-21 22:13:10 -0800 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | static int __init init_fs_locks_sysctls(void) |
| 120 | { |
| 121 | register_sysctl_init("fs", locks_sysctls); |
| 122 | return 0; |
| 123 | } |
| 124 | early_initcall(init_fs_locks_sysctls); |
| 125 | #endif /* CONFIG_SYSCTL */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 127 | /* |
Jeff Layton | 7012b02 | 2013-06-21 08:58:22 -0400 | [diff] [blame] | 128 | * The global file_lock_list is only used for displaying /proc/locks, so we |
Peter Zijlstra | 7c3f654 | 2015-06-22 14:16:34 +0200 | [diff] [blame] | 129 | * 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 Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 134 | */ |
Peter Zijlstra | 7c3f654 | 2015-06-22 14:16:34 +0200 | [diff] [blame] | 135 | struct file_lock_list_struct { |
| 136 | spinlock_t lock; |
| 137 | struct hlist_head hlist; |
| 138 | }; |
| 139 | static DEFINE_PER_CPU(struct file_lock_list_struct, file_lock_list); |
Peter Zijlstra | aba3766 | 2015-06-22 14:16:33 +0200 | [diff] [blame] | 140 | DEFINE_STATIC_PERCPU_RWSEM(file_rwsem); |
Jeff Layton | 8897469 | 2013-06-21 08:58:14 -0400 | [diff] [blame] | 141 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 142 | |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 143 | /* |
Jeff Layton | 48f7418 | 2013-06-21 08:58:18 -0400 | [diff] [blame] | 144 | * The blocked_hash is used to find POSIX lock loops for deadlock detection. |
Jeff Layton | 7b2296a | 2013-06-21 08:58:20 -0400 | [diff] [blame] | 145 | * It is protected by blocked_lock_lock. |
Jeff Layton | 48f7418 | 2013-06-21 08:58:18 -0400 | [diff] [blame] | 146 | * |
| 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 Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 153 | */ |
Jeff Layton | 48f7418 | 2013-06-21 08:58:18 -0400 | [diff] [blame] | 154 | #define BLOCKED_HASH_BITS 7 |
| 155 | static DEFINE_HASHTABLE(blocked_hash, BLOCKED_HASH_BITS); |
Jeff Layton | 8897469 | 2013-06-21 08:58:14 -0400 | [diff] [blame] | 156 | |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 157 | /* |
Jeff Layton | 7b2296a | 2013-06-21 08:58:20 -0400 | [diff] [blame] | 158 | * This lock protects the blocked_hash. Generally, if you're accessing it, you |
| 159 | * want to be holding this lock. |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 160 | * |
NeilBrown | ada5c1d | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 161 | * 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 Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 164 | * |
| 165 | * Note that when we acquire this lock in order to change the above fields, |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 166 | * we often hold the flc_lock as well. In certain cases, when reading the fields |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 167 | * protected by this lock, we can skip acquiring it iff we already hold the |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 168 | * flc_lock. |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 169 | */ |
Jeff Layton | 7b2296a | 2013-06-21 08:58:20 -0400 | [diff] [blame] | 170 | static DEFINE_SPINLOCK(blocked_lock_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
Alexey Dobriyan | 68279f9 | 2023-10-11 19:55:00 +0300 | [diff] [blame] | 172 | static struct kmem_cache *flctx_cache __ro_after_init; |
| 173 | static struct kmem_cache *filelock_cache __ro_after_init; |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 174 | static struct kmem_cache *filelease_cache __ro_after_init; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
Jeff Layton | 4a075e3 | 2015-01-16 15:05:54 -0500 | [diff] [blame] | 176 | static struct file_lock_context * |
Jeff Layton | 5c1c669 | 2015-04-03 09:04:03 -0400 | [diff] [blame] | 177 | locks_get_lock_context(struct inode *inode, int type) |
Jeff Layton | 4a075e3 | 2015-01-16 15:05:54 -0500 | [diff] [blame] | 178 | { |
Dmitry Vyukov | 128a378 | 2015-09-21 09:43:06 +0200 | [diff] [blame] | 179 | struct file_lock_context *ctx; |
Jeff Layton | 4a075e3 | 2015-01-16 15:05:54 -0500 | [diff] [blame] | 180 | |
Dmitry Vyukov | 128a378 | 2015-09-21 09:43:06 +0200 | [diff] [blame] | 181 | /* paired with cmpxchg() below */ |
Jeff Layton | 401a8b8 | 2022-11-16 09:02:30 -0500 | [diff] [blame] | 182 | ctx = locks_inode_context(inode); |
Dmitry Vyukov | 128a378 | 2015-09-21 09:43:06 +0200 | [diff] [blame] | 183 | if (likely(ctx) || type == F_UNLCK) |
Jeff Layton | 4a075e3 | 2015-01-16 15:05:54 -0500 | [diff] [blame] | 184 | goto out; |
| 185 | |
Dmitry Vyukov | 128a378 | 2015-09-21 09:43:06 +0200 | [diff] [blame] | 186 | ctx = kmem_cache_alloc(flctx_cache, GFP_KERNEL); |
| 187 | if (!ctx) |
Jeff Layton | 4a075e3 | 2015-01-16 15:05:54 -0500 | [diff] [blame] | 188 | goto out; |
| 189 | |
Dmitry Vyukov | 128a378 | 2015-09-21 09:43:06 +0200 | [diff] [blame] | 190 | 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 Layton | 4a075e3 | 2015-01-16 15:05:54 -0500 | [diff] [blame] | 194 | |
| 195 | /* |
| 196 | * Assign the pointer if it's not already assigned. If it is, then |
| 197 | * free the context we just allocated. |
| 198 | */ |
Dmitry Vyukov | 128a378 | 2015-09-21 09:43:06 +0200 | [diff] [blame] | 199 | if (cmpxchg(&inode->i_flctx, NULL, ctx)) { |
| 200 | kmem_cache_free(flctx_cache, ctx); |
Jeff Layton | 401a8b8 | 2022-11-16 09:02:30 -0500 | [diff] [blame] | 201 | ctx = locks_inode_context(inode); |
Dmitry Vyukov | 128a378 | 2015-09-21 09:43:06 +0200 | [diff] [blame] | 202 | } |
Jeff Layton | 4a075e3 | 2015-01-16 15:05:54 -0500 | [diff] [blame] | 203 | out: |
Jeff Layton | 1890910 | 2016-01-06 21:26:10 -0500 | [diff] [blame] | 204 | trace_locks_get_lock_context(inode, type, ctx); |
Dmitry Vyukov | 128a378 | 2015-09-21 09:43:06 +0200 | [diff] [blame] | 205 | return ctx; |
Jeff Layton | 4a075e3 | 2015-01-16 15:05:54 -0500 | [diff] [blame] | 206 | } |
| 207 | |
Jeff Layton | e24dada | 2016-01-06 21:28:41 -0500 | [diff] [blame] | 208 | static void |
| 209 | locks_dump_ctx_list(struct list_head *list, char *list_type) |
| 210 | { |
Jeff Layton | fde4951 | 2024-01-31 18:02:00 -0500 | [diff] [blame] | 211 | struct file_lock_core *flc; |
Jeff Layton | e24dada | 2016-01-06 21:28:41 -0500 | [diff] [blame] | 212 | |
Jeff Layton | fde4951 | 2024-01-31 18:02:00 -0500 | [diff] [blame] | 213 | 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 Layton | e24dada | 2016-01-06 21:28:41 -0500 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | static void |
| 220 | locks_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 Coddington | 3953704 | 2017-07-21 13:36:25 -0400 | [diff] [blame] | 236 | static void |
Jeff Layton | fde4951 | 2024-01-31 18:02:00 -0500 | [diff] [blame] | 237 | locks_check_ctx_file_list(struct file *filp, struct list_head *list, char *list_type) |
Benjamin Coddington | 3953704 | 2017-07-21 13:36:25 -0400 | [diff] [blame] | 238 | { |
Jeff Layton | fde4951 | 2024-01-31 18:02:00 -0500 | [diff] [blame] | 239 | struct file_lock_core *flc; |
Jeff Layton | c65454a | 2022-11-25 08:48:37 -0500 | [diff] [blame] | 240 | struct inode *inode = file_inode(filp); |
Benjamin Coddington | 3953704 | 2017-07-21 13:36:25 -0400 | [diff] [blame] | 241 | |
Jeff Layton | fde4951 | 2024-01-31 18:02:00 -0500 | [diff] [blame] | 242 | list_for_each_entry(flc, list, flc_list) |
| 243 | if (flc->flc_file == filp) |
Benjamin Coddington | 3953704 | 2017-07-21 13:36:25 -0400 | [diff] [blame] | 244 | 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 Layton | fde4951 | 2024-01-31 18:02:00 -0500 | [diff] [blame] | 248 | flc->flc_owner, flc->flc_flags, |
| 249 | flc->flc_type, flc->flc_pid); |
Benjamin Coddington | 3953704 | 2017-07-21 13:36:25 -0400 | [diff] [blame] | 250 | } |
| 251 | |
Jeff Layton | 4a075e3 | 2015-01-16 15:05:54 -0500 | [diff] [blame] | 252 | void |
Jeff Layton | f27a0fe | 2016-01-07 15:08:51 -0500 | [diff] [blame] | 253 | locks_free_lock_context(struct inode *inode) |
Jeff Layton | 4a075e3 | 2015-01-16 15:05:54 -0500 | [diff] [blame] | 254 | { |
Jeff Layton | 401a8b8 | 2022-11-16 09:02:30 -0500 | [diff] [blame] | 255 | struct file_lock_context *ctx = locks_inode_context(inode); |
Jeff Layton | f27a0fe | 2016-01-07 15:08:51 -0500 | [diff] [blame] | 256 | |
Jeff Layton | e24dada | 2016-01-06 21:28:41 -0500 | [diff] [blame] | 257 | if (unlikely(ctx)) { |
| 258 | locks_check_ctx_lists(inode); |
Jeff Layton | 4a075e3 | 2015-01-16 15:05:54 -0500 | [diff] [blame] | 259 | kmem_cache_free(flctx_cache, ctx); |
| 260 | } |
| 261 | } |
| 262 | |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 263 | static void locks_init_lock_heads(struct file_lock_core *flc) |
Miklos Szeredi | a51cb91d | 2011-07-06 12:33:55 +0200 | [diff] [blame] | 264 | { |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 265 | 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 Szeredi | a51cb91d | 2011-07-06 12:33:55 +0200 | [diff] [blame] | 270 | } |
| 271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | /* Allocate an empty lock structure. */ |
Arnd Bergmann | c5b1f0d | 2010-10-27 15:46:08 +0200 | [diff] [blame] | 273 | struct file_lock *locks_alloc_lock(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | { |
Miklos Szeredi | ee19cc4 | 2011-07-07 13:06:09 +0200 | [diff] [blame] | 275 | struct file_lock *fl = kmem_cache_zalloc(filelock_cache, GFP_KERNEL); |
Miklos Szeredi | a51cb91d | 2011-07-06 12:33:55 +0200 | [diff] [blame] | 276 | |
| 277 | if (fl) |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 278 | locks_init_lock_heads(&fl->c); |
Miklos Szeredi | a51cb91d | 2011-07-06 12:33:55 +0200 | [diff] [blame] | 279 | |
| 280 | return fl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | } |
Arnd Bergmann | c5b1f0d | 2010-10-27 15:46:08 +0200 | [diff] [blame] | 282 | EXPORT_SYMBOL_GPL(locks_alloc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 284 | /* Allocate an empty lock structure. */ |
| 285 | struct 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 | } |
| 294 | EXPORT_SYMBOL_GPL(locks_alloc_lease); |
| 295 | |
Felix Blyakher | a9e61e2 | 2009-03-31 15:12:56 -0500 | [diff] [blame] | 296 | void locks_release_private(struct file_lock *fl) |
Trond Myklebust | 47831f3 | 2006-03-20 13:44:05 -0500 | [diff] [blame] | 297 | { |
Jeff Layton | fde4951 | 2024-01-31 18:02:00 -0500 | [diff] [blame] | 298 | 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)); |
NeilBrown | 5926459 | 2019-04-24 12:00:08 +1000 | [diff] [blame] | 305 | |
Trond Myklebust | 47831f3 | 2006-03-20 13:44:05 -0500 | [diff] [blame] | 306 | 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 Myklebust | 47831f3 | 2006-03-20 13:44:05 -0500 | [diff] [blame] | 311 | |
Kinglong Mee | 5c97d7b | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 312 | if (fl->fl_lmops) { |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 313 | if (fl->fl_lmops->lm_put_owner) { |
Jeff Layton | fde4951 | 2024-01-31 18:02:00 -0500 | [diff] [blame] | 314 | fl->fl_lmops->lm_put_owner(flc->flc_owner); |
| 315 | flc->flc_owner = NULL; |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 316 | } |
Kinglong Mee | 5c97d7b | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 317 | fl->fl_lmops = NULL; |
| 318 | } |
Trond Myklebust | 47831f3 | 2006-03-20 13:44:05 -0500 | [diff] [blame] | 319 | } |
Felix Blyakher | a9e61e2 | 2009-03-31 15:12:56 -0500 | [diff] [blame] | 320 | EXPORT_SYMBOL_GPL(locks_release_private); |
Trond Myklebust | 47831f3 | 2006-03-20 13:44:05 -0500 | [diff] [blame] | 321 | |
Dai Ngo | 591502c5 | 2022-05-02 14:19:24 -0700 | [diff] [blame] | 322 | /** |
| 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 Layton | fde4951 | 2024-01-31 18:02:00 -0500 | [diff] [blame] | 331 | bool locks_owner_has_blockers(struct file_lock_context *flctx, fl_owner_t owner) |
Dai Ngo | 591502c5 | 2022-05-02 14:19:24 -0700 | [diff] [blame] | 332 | { |
Jeff Layton | fde4951 | 2024-01-31 18:02:00 -0500 | [diff] [blame] | 333 | struct file_lock_core *flc; |
Dai Ngo | 591502c5 | 2022-05-02 14:19:24 -0700 | [diff] [blame] | 334 | |
| 335 | spin_lock(&flctx->flc_lock); |
Jeff Layton | fde4951 | 2024-01-31 18:02:00 -0500 | [diff] [blame] | 336 | list_for_each_entry(flc, &flctx->flc_posix, flc_list) { |
| 337 | if (flc->flc_owner != owner) |
Dai Ngo | 591502c5 | 2022-05-02 14:19:24 -0700 | [diff] [blame] | 338 | continue; |
Jeff Layton | fde4951 | 2024-01-31 18:02:00 -0500 | [diff] [blame] | 339 | if (!list_empty(&flc->flc_blocked_requests)) { |
Dai Ngo | 591502c5 | 2022-05-02 14:19:24 -0700 | [diff] [blame] | 340 | spin_unlock(&flctx->flc_lock); |
| 341 | return true; |
| 342 | } |
| 343 | } |
| 344 | spin_unlock(&flctx->flc_lock); |
| 345 | return false; |
| 346 | } |
| 347 | EXPORT_SYMBOL_GPL(locks_owner_has_blockers); |
| 348 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | /* Free a lock which is not in use. */ |
J. Bruce Fields | 05fa313 | 2010-10-30 17:31:15 -0400 | [diff] [blame] | 350 | void locks_free_lock(struct file_lock *fl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | { |
Trond Myklebust | 47831f3 | 2006-03-20 13:44:05 -0500 | [diff] [blame] | 352 | locks_release_private(fl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | kmem_cache_free(filelock_cache, fl); |
| 354 | } |
J. Bruce Fields | 05fa313 | 2010-10-30 17:31:15 -0400 | [diff] [blame] | 355 | EXPORT_SYMBOL(locks_free_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 357 | /* Free a lease which is not in use. */ |
| 358 | void locks_free_lease(struct file_lease *fl) |
| 359 | { |
| 360 | kmem_cache_free(filelease_cache, fl); |
| 361 | } |
| 362 | EXPORT_SYMBOL(locks_free_lease); |
| 363 | |
Jeff Layton | ed9814d | 2014-08-11 14:20:31 -0400 | [diff] [blame] | 364 | static void |
| 365 | locks_dispose_list(struct list_head *dispose) |
| 366 | { |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 367 | struct file_lock_core *flc; |
Jeff Layton | ed9814d | 2014-08-11 14:20:31 -0400 | [diff] [blame] | 368 | |
| 369 | while (!list_empty(dispose)) { |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 370 | 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 Layton | ed9814d | 2014-08-11 14:20:31 -0400 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | void locks_init_lock(struct file_lock *fl) |
| 380 | { |
Miklos Szeredi | ee19cc4 | 2011-07-07 13:06:09 +0200 | [diff] [blame] | 381 | memset(fl, 0, sizeof(struct file_lock)); |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 382 | locks_init_lock_heads(&fl->c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | EXPORT_SYMBOL(locks_init_lock); |
| 385 | |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 386 | void locks_init_lease(struct file_lease *fl) |
| 387 | { |
| 388 | memset(fl, 0, sizeof(*fl)); |
| 389 | locks_init_lock_heads(&fl->c); |
| 390 | } |
| 391 | EXPORT_SYMBOL(locks_init_lease); |
| 392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | /* |
| 394 | * Initialize a new lock from an existing file_lock structure. |
| 395 | */ |
Kinglong Mee | 3fe0fff1 | 2014-08-22 10:18:42 -0400 | [diff] [blame] | 396 | void locks_copy_conflock(struct file_lock *new, struct file_lock *fl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | { |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 398 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | new->fl_start = fl->fl_start; |
| 404 | new->fl_end = fl->fl_end; |
Kinglong Mee | f328296 | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 405 | new->fl_lmops = fl->fl_lmops; |
Trond Myklebust | 0996905 | 2006-03-20 13:44:38 -0500 | [diff] [blame] | 406 | new->fl_ops = NULL; |
Kinglong Mee | f328296 | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 407 | |
| 408 | if (fl->fl_lmops) { |
| 409 | if (fl->fl_lmops->lm_get_owner) |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 410 | fl->fl_lmops->lm_get_owner(fl->c.flc_owner); |
Kinglong Mee | f328296 | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 411 | } |
Trond Myklebust | 0996905 | 2006-03-20 13:44:38 -0500 | [diff] [blame] | 412 | } |
Kinglong Mee | 3fe0fff1 | 2014-08-22 10:18:42 -0400 | [diff] [blame] | 413 | EXPORT_SYMBOL(locks_copy_conflock); |
Trond Myklebust | 0996905 | 2006-03-20 13:44:38 -0500 | [diff] [blame] | 414 | |
| 415 | void locks_copy_lock(struct file_lock *new, struct file_lock *fl) |
| 416 | { |
Jeff Layton | 566709b | 2014-08-11 14:09:35 -0400 | [diff] [blame] | 417 | /* "new" must be a freshly-initialized lock */ |
| 418 | WARN_ON_ONCE(new->fl_ops); |
Trond Myklebust | 0996905 | 2006-03-20 13:44:38 -0500 | [diff] [blame] | 419 | |
Kinglong Mee | 3fe0fff1 | 2014-08-22 10:18:42 -0400 | [diff] [blame] | 420 | locks_copy_conflock(new, fl); |
Kinglong Mee | f328296 | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 421 | |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 422 | new->c.flc_file = fl->c.flc_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | new->fl_ops = fl->fl_ops; |
Trond Myklebust | 47831f3 | 2006-03-20 13:44:05 -0500 | [diff] [blame] | 424 | |
Kinglong Mee | f328296 | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 425 | if (fl->fl_ops) { |
| 426 | if (fl->fl_ops->fl_copy_lock) |
| 427 | fl->fl_ops->fl_copy_lock(new, fl); |
| 428 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | EXPORT_SYMBOL(locks_copy_lock); |
| 431 | |
NeilBrown | 5946c43 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 432 | static 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 Layton | b6aaba5 | 2024-01-31 18:02:07 -0500 | [diff] [blame] | 438 | * ->flc_blocked_requests, so we don't need a lock to check if it |
NeilBrown | 5946c43 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 439 | * is empty. |
| 440 | */ |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 441 | if (list_empty(&fl->c.flc_blocked_requests)) |
NeilBrown | 5946c43 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 442 | return; |
| 443 | spin_lock(&blocked_lock_lock); |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 444 | 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 Layton | b6aaba5 | 2024-01-31 18:02:07 -0500 | [diff] [blame] | 448 | f->c.flc_blocker = &new->c; |
NeilBrown | 5946c43 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 449 | spin_unlock(&blocked_lock_lock); |
| 450 | } |
| 451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | static inline int flock_translate_cmd(int cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | 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 Iwashima | 4149be7 | 2022-07-16 21:35:31 -0700 | [diff] [blame] | 465 | static void flock_make_lock(struct file *filp, struct file_lock *fl, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | { |
Kuniyuki Iwashima | 4149be7 | 2022-07-16 21:35:31 -0700 | [diff] [blame] | 467 | locks_init_lock(fl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 469 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | fl->fl_end = OFFSET_MAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Jeff Layton | d9077f7 | 2024-01-31 18:02:10 -0500 | [diff] [blame] | 477 | static int assign_type(struct file_lock_core *flc, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | { |
| 479 | switch (type) { |
| 480 | case F_RDLCK: |
| 481 | case F_WRLCK: |
| 482 | case F_UNLCK: |
Jeff Layton | d9077f7 | 2024-01-31 18:02:10 -0500 | [diff] [blame] | 483 | flc->flc_type = type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | break; |
| 485 | default: |
| 486 | return -EINVAL; |
| 487 | } |
| 488 | return 0; |
| 489 | } |
| 490 | |
J. Bruce Fields | ef12e72 | 2014-02-03 12:13:08 -0500 | [diff] [blame] | 491 | static 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 Meng | 1623841 | 2020-10-23 14:20:05 +0800 | [diff] [blame] | 518 | fl->fl_end = fl->fl_start + (l->l_len - 1); |
J. Bruce Fields | ef12e72 | 2014-02-03 12:13:08 -0500 | [diff] [blame] | 519 | |
| 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 Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 528 | 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 Fields | ef12e72 | 2014-02-03 12:13:08 -0500 | [diff] [blame] | 532 | fl->fl_ops = NULL; |
| 533 | fl->fl_lmops = NULL; |
| 534 | |
Jeff Layton | d9077f7 | 2024-01-31 18:02:10 -0500 | [diff] [blame] | 535 | return assign_type(&fl->c, l->l_type); |
J. Bruce Fields | ef12e72 | 2014-02-03 12:13:08 -0500 | [diff] [blame] | 536 | } |
| 537 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX |
| 539 | * style lock. |
| 540 | */ |
| 541 | static int flock_to_posix_lock(struct file *filp, struct file_lock *fl, |
| 542 | struct flock *l) |
| 543 | { |
J. Bruce Fields | ef12e72 | 2014-02-03 12:13:08 -0500 | [diff] [blame] | 544 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
J. Bruce Fields | ef12e72 | 2014-02-03 12:13:08 -0500 | [diff] [blame] | 551 | return flock64_to_posix_lock(filp, fl, &ll); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | /* default lease lock manager operations */ |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 555 | static bool |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 556 | lease_break_callback(struct file_lease *fl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | { |
| 558 | kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG); |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 559 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Jeff Layton | 1c7dd2f | 2014-08-22 10:55:47 -0400 | [diff] [blame] | 562 | static void |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 563 | lease_setup(struct file_lease *fl, void **priv) |
Jeff Layton | 1c7dd2f | 2014-08-22 10:55:47 -0400 | [diff] [blame] | 564 | { |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 565 | struct file *filp = fl->c.flc_file; |
Jeff Layton | 1c7dd2f | 2014-08-22 10:55:47 -0400 | [diff] [blame] | 566 | 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. Biederman | 0191913 | 2017-07-16 22:05:57 -0500 | [diff] [blame] | 576 | __f_setown(filp, task_pid(current), PIDTYPE_TGID, 0); |
Jeff Layton | 1c7dd2f | 2014-08-22 10:55:47 -0400 | [diff] [blame] | 577 | } |
| 578 | |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 579 | static const struct lease_manager_operations lease_manager_ops = { |
J. Bruce Fields | 8fb47a4 | 2011-07-20 20:21:59 -0400 | [diff] [blame] | 580 | .lm_break = lease_break_callback, |
J. Bruce Fields | 8fb47a4 | 2011-07-20 20:21:59 -0400 | [diff] [blame] | 581 | .lm_change = lease_modify, |
Jeff Layton | 1c7dd2f | 2014-08-22 10:55:47 -0400 | [diff] [blame] | 582 | .lm_setup = lease_setup, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | }; |
| 584 | |
| 585 | /* |
| 586 | * Initialize a lease, use the default lock manager operations |
| 587 | */ |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 588 | static int lease_init(struct file *filp, int type, struct file_lease *fl) |
Joe Perches | 447a564 | 2018-03-21 15:09:32 -0700 | [diff] [blame] | 589 | { |
Jeff Layton | d9077f7 | 2024-01-31 18:02:10 -0500 | [diff] [blame] | 590 | if (assign_type(&fl->c, type) != 0) |
Trond Myklebust | 75dff55 | 2006-05-07 23:02:42 -0400 | [diff] [blame] | 591 | return -EINVAL; |
| 592 | |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 593 | fl->c.flc_owner = filp; |
| 594 | fl->c.flc_pid = current->tgid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 596 | fl->c.flc_file = filp; |
| 597 | fl->c.flc_flags = FL_LEASE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | fl->fl_lmops = &lease_manager_ops; |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | /* Allocate a file_lock initialised to this type of lease */ |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 603 | static struct file_lease *lease_alloc(struct file *filp, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | { |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 605 | struct file_lease *fl = locks_alloc_lease(); |
Trond Myklebust | 75dff55 | 2006-05-07 23:02:42 -0400 | [diff] [blame] | 606 | int error = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | |
| 608 | if (fl == NULL) |
J. Bruce Fields | e32b8ee | 2007-03-01 14:34:35 -0500 | [diff] [blame] | 609 | return ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | |
| 611 | error = lease_init(filp, type, fl); |
Trond Myklebust | 75dff55 | 2006-05-07 23:02:42 -0400 | [diff] [blame] | 612 | if (error) { |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 613 | locks_free_lease(fl); |
J. Bruce Fields | e32b8ee | 2007-03-01 14:34:35 -0500 | [diff] [blame] | 614 | return ERR_PTR(error); |
Trond Myklebust | 75dff55 | 2006-05-07 23:02:42 -0400 | [diff] [blame] | 615 | } |
J. Bruce Fields | e32b8ee | 2007-03-01 14:34:35 -0500 | [diff] [blame] | 616 | return fl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | /* Check if two locks overlap each other. |
| 620 | */ |
| 621 | static 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 Layton | 9bb430a | 2024-01-31 18:02:01 -0500 | [diff] [blame] | 630 | static int posix_same_owner(struct file_lock_core *fl1, struct file_lock_core *fl2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | { |
Jeff Layton | 9bb430a | 2024-01-31 18:02:01 -0500 | [diff] [blame] | 632 | return fl1->flc_owner == fl2->flc_owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | } |
| 634 | |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 635 | /* Must be called with the flc_lock held! */ |
Jeff Layton | 4629172 | 2024-01-31 18:02:03 -0500 | [diff] [blame] | 636 | static void locks_insert_global_locks(struct file_lock_core *flc) |
Jeff Layton | 8897469 | 2013-06-21 08:58:14 -0400 | [diff] [blame] | 637 | { |
Peter Zijlstra | 7c3f654 | 2015-06-22 14:16:34 +0200 | [diff] [blame] | 638 | struct file_lock_list_struct *fll = this_cpu_ptr(&file_lock_list); |
| 639 | |
Peter Zijlstra | aba3766 | 2015-06-22 14:16:33 +0200 | [diff] [blame] | 640 | percpu_rwsem_assert_held(&file_rwsem); |
| 641 | |
Peter Zijlstra | 7c3f654 | 2015-06-22 14:16:34 +0200 | [diff] [blame] | 642 | spin_lock(&fll->lock); |
Jeff Layton | 4629172 | 2024-01-31 18:02:03 -0500 | [diff] [blame] | 643 | flc->flc_link_cpu = smp_processor_id(); |
| 644 | hlist_add_head(&flc->flc_link, &fll->hlist); |
Peter Zijlstra | 7c3f654 | 2015-06-22 14:16:34 +0200 | [diff] [blame] | 645 | spin_unlock(&fll->lock); |
Jeff Layton | 8897469 | 2013-06-21 08:58:14 -0400 | [diff] [blame] | 646 | } |
| 647 | |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 648 | /* Must be called with the flc_lock held! */ |
Jeff Layton | 4629172 | 2024-01-31 18:02:03 -0500 | [diff] [blame] | 649 | static void locks_delete_global_locks(struct file_lock_core *flc) |
Jeff Layton | 8897469 | 2013-06-21 08:58:14 -0400 | [diff] [blame] | 650 | { |
Peter Zijlstra | 7c3f654 | 2015-06-22 14:16:34 +0200 | [diff] [blame] | 651 | struct file_lock_list_struct *fll; |
| 652 | |
Peter Zijlstra | aba3766 | 2015-06-22 14:16:33 +0200 | [diff] [blame] | 653 | percpu_rwsem_assert_held(&file_rwsem); |
| 654 | |
Jeff Layton | 7012b02 | 2013-06-21 08:58:22 -0400 | [diff] [blame] | 655 | /* |
| 656 | * Avoid taking lock if already unhashed. This is safe since this check |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 657 | * is done while holding the flc_lock, and new insertions into the list |
Jeff Layton | 7012b02 | 2013-06-21 08:58:22 -0400 | [diff] [blame] | 658 | * also require that it be held. |
| 659 | */ |
Jeff Layton | 4629172 | 2024-01-31 18:02:03 -0500 | [diff] [blame] | 660 | if (hlist_unhashed(&flc->flc_link)) |
Jeff Layton | 7012b02 | 2013-06-21 08:58:22 -0400 | [diff] [blame] | 661 | return; |
Peter Zijlstra | 7c3f654 | 2015-06-22 14:16:34 +0200 | [diff] [blame] | 662 | |
Jeff Layton | 4629172 | 2024-01-31 18:02:03 -0500 | [diff] [blame] | 663 | fll = per_cpu_ptr(&file_lock_list, flc->flc_link_cpu); |
Peter Zijlstra | 7c3f654 | 2015-06-22 14:16:34 +0200 | [diff] [blame] | 664 | spin_lock(&fll->lock); |
Jeff Layton | 4629172 | 2024-01-31 18:02:03 -0500 | [diff] [blame] | 665 | hlist_del_init(&flc->flc_link); |
Peter Zijlstra | 7c3f654 | 2015-06-22 14:16:34 +0200 | [diff] [blame] | 666 | spin_unlock(&fll->lock); |
Jeff Layton | 8897469 | 2013-06-21 08:58:14 -0400 | [diff] [blame] | 667 | } |
| 668 | |
Jeff Layton | 3999e49 | 2013-06-21 08:58:19 -0400 | [diff] [blame] | 669 | static unsigned long |
Jeff Layton | ad39974 | 2024-01-31 18:02:02 -0500 | [diff] [blame] | 670 | posix_owner_key(struct file_lock_core *flc) |
Jeff Layton | 3999e49 | 2013-06-21 08:58:19 -0400 | [diff] [blame] | 671 | { |
Jeff Layton | ad39974 | 2024-01-31 18:02:02 -0500 | [diff] [blame] | 672 | return (unsigned long) flc->flc_owner; |
Jeff Layton | 3999e49 | 2013-06-21 08:58:19 -0400 | [diff] [blame] | 673 | } |
| 674 | |
Jeff Layton | 1a6c75d | 2024-01-31 18:02:04 -0500 | [diff] [blame] | 675 | static void locks_insert_global_blocked(struct file_lock_core *waiter) |
Jeff Layton | 8897469 | 2013-06-21 08:58:14 -0400 | [diff] [blame] | 676 | { |
Daniel Wagner | 663d5af | 2015-04-03 09:04:03 -0400 | [diff] [blame] | 677 | lockdep_assert_held(&blocked_lock_lock); |
| 678 | |
Jeff Layton | 1a6c75d | 2024-01-31 18:02:04 -0500 | [diff] [blame] | 679 | hash_add(blocked_hash, &waiter->flc_link, posix_owner_key(waiter)); |
Jeff Layton | 8897469 | 2013-06-21 08:58:14 -0400 | [diff] [blame] | 680 | } |
| 681 | |
Jeff Layton | 1a6c75d | 2024-01-31 18:02:04 -0500 | [diff] [blame] | 682 | static void locks_delete_global_blocked(struct file_lock_core *waiter) |
Jeff Layton | 8897469 | 2013-06-21 08:58:14 -0400 | [diff] [blame] | 683 | { |
Daniel Wagner | 663d5af | 2015-04-03 09:04:03 -0400 | [diff] [blame] | 684 | lockdep_assert_held(&blocked_lock_lock); |
| 685 | |
Jeff Layton | 1a6c75d | 2024-01-31 18:02:04 -0500 | [diff] [blame] | 686 | hash_del(&waiter->flc_link); |
Jeff Layton | 8897469 | 2013-06-21 08:58:14 -0400 | [diff] [blame] | 687 | } |
| 688 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | /* Remove waiter from blocker's block list. |
| 690 | * When blocker ends up pointing to itself then the list is empty. |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 691 | * |
Jeff Layton | 7b2296a | 2013-06-21 08:58:20 -0400 | [diff] [blame] | 692 | * Must be called with blocked_lock_lock held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | */ |
Jeff Layton | 269a619 | 2024-01-31 18:02:09 -0500 | [diff] [blame] | 694 | static void __locks_unlink_block(struct file_lock_core *waiter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | { |
Jeff Layton | 1a62c22 | 2024-01-31 18:02:05 -0500 | [diff] [blame] | 696 | locks_delete_global_blocked(waiter); |
| 697 | list_del_init(&waiter->flc_blocked_member); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | } |
| 699 | |
Jeff Layton | 1a62c22 | 2024-01-31 18:02:05 -0500 | [diff] [blame] | 700 | static void __locks_wake_up_blocks(struct file_lock_core *blocker) |
NeilBrown | ad6bbd8 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 701 | { |
Jeff Layton | 1a62c22 | 2024-01-31 18:02:05 -0500 | [diff] [blame] | 702 | while (!list_empty(&blocker->flc_blocked_requests)) { |
| 703 | struct file_lock_core *waiter; |
| 704 | struct file_lock *fl; |
NeilBrown | ad6bbd8 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 705 | |
Jeff Layton | 1a62c22 | 2024-01-31 18:02:05 -0500 | [diff] [blame] | 706 | waiter = list_first_entry(&blocker->flc_blocked_requests, |
| 707 | struct file_lock_core, flc_blocked_member); |
| 708 | |
| 709 | fl = file_lock(waiter); |
Jeff Layton | 269a619 | 2024-01-31 18:02:09 -0500 | [diff] [blame] | 710 | __locks_unlink_block(waiter); |
Jeff Layton | 1a62c22 | 2024-01-31 18:02:05 -0500 | [diff] [blame] | 711 | if ((waiter->flc_flags & (FL_POSIX | FL_FLOCK)) && |
| 712 | fl->fl_lmops && fl->fl_lmops->lm_notify) |
| 713 | fl->fl_lmops->lm_notify(fl); |
NeilBrown | ad6bbd8 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 714 | else |
Jeff Layton | 1a62c22 | 2024-01-31 18:02:05 -0500 | [diff] [blame] | 715 | locks_wake_up(fl); |
Linus Torvalds | dcf23ac | 2020-03-18 07:52:21 -0400 | [diff] [blame] | 716 | |
| 717 | /* |
Jeff Layton | 1a62c22 | 2024-01-31 18:02:05 -0500 | [diff] [blame] | 718 | * The setting of flc_blocker to NULL marks the "done" |
Linus Torvalds | dcf23ac | 2020-03-18 07:52:21 -0400 | [diff] [blame] | 719 | * point in deleting a block. Paired with acquire at the top |
| 720 | * of locks_delete_block(). |
| 721 | */ |
Jeff Layton | 1a62c22 | 2024-01-31 18:02:05 -0500 | [diff] [blame] | 722 | smp_store_release(&waiter->flc_blocker, NULL); |
NeilBrown | ad6bbd8 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | |
Jeff Layton | 269a619 | 2024-01-31 18:02:09 -0500 | [diff] [blame] | 726 | static int __locks_delete_block(struct file_lock_core *waiter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | { |
NeilBrown | cb03f94 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 728 | int status = -ENOENT; |
| 729 | |
Linus Torvalds | dcf23ac | 2020-03-18 07:52:21 -0400 | [diff] [blame] | 730 | /* |
| 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 Layton | e8a166c | 2024-01-31 18:02:08 -0500 | [diff] [blame] | 751 | if (!smp_load_acquire(&waiter->flc_blocker) && |
| 752 | list_empty(&waiter->flc_blocked_requests)) |
Linus Torvalds | dcf23ac | 2020-03-18 07:52:21 -0400 | [diff] [blame] | 753 | return status; |
| 754 | |
Jeff Layton | 7b2296a | 2013-06-21 08:58:20 -0400 | [diff] [blame] | 755 | spin_lock(&blocked_lock_lock); |
Jeff Layton | e8a166c | 2024-01-31 18:02:08 -0500 | [diff] [blame] | 756 | if (waiter->flc_blocker) |
NeilBrown | cb03f94 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 757 | status = 0; |
Jeff Layton | e8a166c | 2024-01-31 18:02:08 -0500 | [diff] [blame] | 758 | __locks_wake_up_blocks(waiter); |
Jeff Layton | 269a619 | 2024-01-31 18:02:09 -0500 | [diff] [blame] | 759 | __locks_unlink_block(waiter); |
Linus Torvalds | dcf23ac | 2020-03-18 07:52:21 -0400 | [diff] [blame] | 760 | |
| 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 Layton | e8a166c | 2024-01-31 18:02:08 -0500 | [diff] [blame] | 765 | smp_store_release(&waiter->flc_blocker, NULL); |
Jeff Layton | 7b2296a | 2013-06-21 08:58:20 -0400 | [diff] [blame] | 766 | spin_unlock(&blocked_lock_lock); |
NeilBrown | cb03f94 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 767 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | } |
Jeff Layton | 269a619 | 2024-01-31 18:02:09 -0500 | [diff] [blame] | 769 | |
| 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 | */ |
| 776 | int locks_delete_block(struct file_lock *waiter) |
| 777 | { |
| 778 | return __locks_delete_block(&waiter->c); |
| 779 | } |
NeilBrown | cb03f94 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 780 | EXPORT_SYMBOL(locks_delete_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | |
| 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 Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 786 | * |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 787 | * Must be called with both the flc_lock and blocked_lock_lock held. The |
NeilBrown | ada5c1d | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 788 | * 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. |
NeilBrown | fd7732e | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 792 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | */ |
Jeff Layton | 269a619 | 2024-01-31 18:02:09 -0500 | [diff] [blame] | 797 | static void __locks_insert_block(struct file_lock_core *blocker, |
| 798 | struct file_lock_core *waiter, |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 799 | bool conflict(struct file_lock_core *, |
| 800 | struct file_lock_core *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | { |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 802 | struct file_lock_core *flc; |
NeilBrown | fd7732e | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 803 | |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 804 | BUG_ON(!list_empty(&waiter->flc_blocked_member)); |
NeilBrown | fd7732e | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 805 | new_blocker: |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 806 | list_for_each_entry(flc, &blocker->flc_blocked_requests, flc_blocked_member) |
| 807 | if (conflict(flc, waiter)) { |
| 808 | blocker = flc; |
NeilBrown | fd7732e | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 809 | goto new_blocker; |
| 810 | } |
Jeff Layton | b6aaba5 | 2024-01-31 18:02:07 -0500 | [diff] [blame] | 811 | waiter->flc_blocker = blocker; |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 812 | list_add_tail(&waiter->flc_blocked_member, |
| 813 | &blocker->flc_blocked_requests); |
NeilBrown | 5946c43 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 814 | |
Jeff Layton | 14786d9 | 2024-02-18 08:33:28 -0500 | [diff] [blame] | 815 | if ((blocker->flc_flags & (FL_POSIX|FL_OFDLCK)) == FL_POSIX) |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 816 | locks_insert_global_blocked(waiter); |
| 817 | |
| 818 | /* The requests in waiter->flc_blocked are known to conflict with |
NeilBrown | 5946c43 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 819 | * 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 Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 822 | __locks_wake_up_blocks(waiter); |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 823 | } |
| 824 | |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 825 | /* Must be called with flc_lock held. */ |
Jeff Layton | 269a619 | 2024-01-31 18:02:09 -0500 | [diff] [blame] | 826 | static void locks_insert_block(struct file_lock_core *blocker, |
| 827 | struct file_lock_core *waiter, |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 828 | bool conflict(struct file_lock_core *, |
| 829 | struct file_lock_core *)) |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 830 | { |
Jeff Layton | 7b2296a | 2013-06-21 08:58:20 -0400 | [diff] [blame] | 831 | spin_lock(&blocked_lock_lock); |
NeilBrown | fd7732e | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 832 | __locks_insert_block(blocker, waiter, conflict); |
Jeff Layton | 7b2296a | 2013-06-21 08:58:20 -0400 | [diff] [blame] | 833 | spin_unlock(&blocked_lock_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | } |
| 835 | |
Jeff Layton | 1cb3601 | 2013-06-21 08:58:12 -0400 | [diff] [blame] | 836 | /* |
| 837 | * Wake up processes blocked waiting for blocker. |
| 838 | * |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 839 | * Must be called with the inode->flc_lock held! |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | */ |
Jeff Layton | 347d49f | 2024-01-31 18:02:11 -0500 | [diff] [blame] | 841 | static void locks_wake_up_blocks(struct file_lock_core *blocker) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | { |
Jeff Layton | 4e8c765 | 2013-06-21 08:58:16 -0400 | [diff] [blame] | 843 | /* |
| 844 | * Avoid taking global lock if list is empty. This is safe since new |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 845 | * blocked requests are only added to the list under the flc_lock, and |
NeilBrown | ada5c1d | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 846 | * 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 Layton | 4e8c765 | 2013-06-21 08:58:16 -0400 | [diff] [blame] | 849 | */ |
Jeff Layton | 347d49f | 2024-01-31 18:02:11 -0500 | [diff] [blame] | 850 | if (list_empty(&blocker->flc_blocked_requests)) |
Jeff Layton | 4e8c765 | 2013-06-21 08:58:16 -0400 | [diff] [blame] | 851 | return; |
| 852 | |
Jeff Layton | 7b2296a | 2013-06-21 08:58:20 -0400 | [diff] [blame] | 853 | spin_lock(&blocked_lock_lock); |
Jeff Layton | 347d49f | 2024-01-31 18:02:11 -0500 | [diff] [blame] | 854 | __locks_wake_up_blocks(blocker); |
Jeff Layton | 7b2296a | 2013-06-21 08:58:20 -0400 | [diff] [blame] | 855 | spin_unlock(&blocked_lock_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | } |
| 857 | |
Jeff Layton | 5263e31 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 858 | static void |
Jeff Layton | 7c18509 | 2024-01-31 18:02:12 -0500 | [diff] [blame] | 859 | locks_insert_lock_ctx(struct file_lock_core *fl, struct list_head *before) |
Jeff Layton | 5263e31 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 860 | { |
Jeff Layton | 7c18509 | 2024-01-31 18:02:12 -0500 | [diff] [blame] | 861 | list_add_tail(&fl->flc_list, before); |
| 862 | locks_insert_global_locks(fl); |
Jeff Layton | 5263e31 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 863 | } |
| 864 | |
Jeff Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 865 | static void |
Jeff Layton | 7c18509 | 2024-01-31 18:02:12 -0500 | [diff] [blame] | 866 | locks_unlink_lock_ctx(struct file_lock_core *fl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | { |
Jeff Layton | 7c18509 | 2024-01-31 18:02:12 -0500 | [diff] [blame] | 868 | locks_delete_global_locks(fl); |
| 869 | list_del_init(&fl->flc_list); |
| 870 | locks_wake_up_blocks(fl); |
Jeff Layton | 24cbe78 | 2014-02-03 12:13:06 -0500 | [diff] [blame] | 871 | } |
| 872 | |
Jeff Layton | 5263e31 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 873 | static void |
Jeff Layton | 7c18509 | 2024-01-31 18:02:12 -0500 | [diff] [blame] | 874 | locks_delete_lock_ctx(struct file_lock_core *fl, struct list_head *dispose) |
Jeff Layton | 5263e31 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 875 | { |
Jeff Layton | e084c1b | 2015-02-16 14:32:03 -0500 | [diff] [blame] | 876 | locks_unlink_lock_ctx(fl); |
Jeff Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 877 | if (dispose) |
Jeff Layton | 7c18509 | 2024-01-31 18:02:12 -0500 | [diff] [blame] | 878 | list_add(&fl->flc_list, dispose); |
Jeff Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 879 | else |
Jeff Layton | 7c18509 | 2024-01-31 18:02:12 -0500 | [diff] [blame] | 880 | locks_free_lock(file_lock(fl)); |
Jeff Layton | 5263e31 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 881 | } |
| 882 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | /* Determine if lock sys_fl blocks lock caller_fl. Common functionality |
| 884 | * checks for shared/exclusive status of overlapping locks. |
| 885 | */ |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 886 | static bool locks_conflict(struct file_lock_core *caller_flc, |
| 887 | struct file_lock_core *sys_flc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | { |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 889 | if (sys_flc->flc_type == F_WRLCK) |
NeilBrown | c0e1590 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 890 | return true; |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 891 | if (caller_flc->flc_type == F_WRLCK) |
NeilBrown | c0e1590 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 892 | return true; |
| 893 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific |
| 897 | * checking before calling the locks_conflict(). |
| 898 | */ |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 899 | static bool posix_locks_conflict(struct file_lock_core *caller_flc, |
| 900 | struct file_lock_core *sys_flc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | { |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 902 | struct file_lock *caller_fl = file_lock(caller_flc); |
| 903 | struct file_lock *sys_fl = file_lock(sys_flc); |
| 904 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | /* POSIX locks owned by the same process do not conflict with |
| 906 | * each other. |
| 907 | */ |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 908 | if (posix_same_owner(caller_flc, sys_flc)) |
NeilBrown | c0e1590 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 909 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | |
| 911 | /* Check whether they overlap */ |
| 912 | if (!locks_overlap(caller_fl, sys_fl)) |
NeilBrown | c0e1590 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 913 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 915 | return locks_conflict(caller_flc, sys_flc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | } |
| 917 | |
Stas Sergeev | 6c9007f | 2023-06-22 21:52:23 +0500 | [diff] [blame] | 918 | /* 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 | */ |
| 921 | static bool posix_test_locks_conflict(struct file_lock *caller_fl, |
| 922 | struct file_lock *sys_fl) |
| 923 | { |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 924 | struct file_lock_core *caller = &caller_fl->c; |
| 925 | struct file_lock_core *sys = &sys_fl->c; |
| 926 | |
Stas Sergeev | 6c9007f | 2023-06-22 21:52:23 +0500 | [diff] [blame] | 927 | /* F_UNLCK checks any locks on the same fd. */ |
Jeff Layton | 75cabec | 2024-01-31 18:01:45 -0500 | [diff] [blame] | 928 | if (lock_is_unlock(caller_fl)) { |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 929 | if (!posix_same_owner(caller, sys)) |
Stas Sergeev | 6c9007f | 2023-06-22 21:52:23 +0500 | [diff] [blame] | 930 | return false; |
| 931 | return locks_overlap(caller_fl, sys_fl); |
| 932 | } |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 933 | return posix_locks_conflict(caller, sys); |
Stas Sergeev | 6c9007f | 2023-06-22 21:52:23 +0500 | [diff] [blame] | 934 | } |
| 935 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific |
| 937 | * checking before calling the locks_conflict(). |
| 938 | */ |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 939 | static bool flock_locks_conflict(struct file_lock_core *caller_flc, |
| 940 | struct file_lock_core *sys_flc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | { |
| 942 | /* FLOCK locks referring to the same filp do not conflict with |
| 943 | * each other. |
| 944 | */ |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 945 | if (caller_flc->flc_file == sys_flc->flc_file) |
NeilBrown | c0e1590 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 946 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 948 | return locks_conflict(caller_flc, sys_flc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | } |
| 950 | |
J. Bruce Fields | 6d34ac1 | 2007-05-11 16:09:32 -0400 | [diff] [blame] | 951 | void |
Marc Eshel | 9d6a8c5 | 2007-02-21 00:55:18 -0500 | [diff] [blame] | 952 | posix_test_lock(struct file *filp, struct file_lock *fl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | { |
| 954 | struct file_lock *cfl; |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 955 | struct file_lock_context *ctx; |
Jeff Layton | c65454a | 2022-11-25 08:48:37 -0500 | [diff] [blame] | 956 | struct inode *inode = file_inode(filp); |
Dai Ngo | 2443da2 | 2022-05-02 14:19:25 -0700 | [diff] [blame] | 957 | void *owner; |
| 958 | void (*func)(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | |
Jeff Layton | 401a8b8 | 2022-11-16 09:02:30 -0500 | [diff] [blame] | 960 | ctx = locks_inode_context(inode); |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 961 | if (!ctx || list_empty_careful(&ctx->flc_posix)) { |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 962 | fl->c.flc_type = F_UNLCK; |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 963 | return; |
| 964 | } |
| 965 | |
Dai Ngo | 2443da2 | 2022-05-02 14:19:25 -0700 | [diff] [blame] | 966 | retry: |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 967 | spin_lock(&ctx->flc_lock); |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 968 | list_for_each_entry(cfl, &ctx->flc_posix, c.flc_list) { |
Stas Sergeev | 6c9007f | 2023-06-22 21:52:23 +0500 | [diff] [blame] | 969 | if (!posix_test_locks_conflict(fl, cfl)) |
Dai Ngo | 2443da2 | 2022-05-02 14:19:25 -0700 | [diff] [blame] | 970 | 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 Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 980 | } |
Dai Ngo | 2443da2 | 2022-05-02 14:19:25 -0700 | [diff] [blame] | 981 | locks_copy_conflock(fl, cfl); |
| 982 | goto out; |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 983 | } |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 984 | fl->c.flc_type = F_UNLCK; |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 985 | out: |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 986 | spin_unlock(&ctx->flc_lock); |
J. Bruce Fields | 6d34ac1 | 2007-05-11 16:09:32 -0400 | [diff] [blame] | 987 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | EXPORT_SYMBOL(posix_test_lock); |
| 990 | |
J. Bruce Fields | b533184 | 2007-10-26 18:05:40 -0400 | [diff] [blame] | 991 | /* |
| 992 | * Deadlock detection: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | * |
J. Bruce Fields | b533184 | 2007-10-26 18:05:40 -0400 | [diff] [blame] | 994 | * We attempt to detect deadlocks that are due purely to posix file |
| 995 | * locks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | * |
J. Bruce Fields | b533184 | 2007-10-26 18:05:40 -0400 | [diff] [blame] | 997 | * 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 Fields | 97855b4 | 2007-10-30 11:20:02 -0400 | [diff] [blame] | 1004 | * |
J. Bruce Fields | b533184 | 2007-10-26 18:05:40 -0400 | [diff] [blame] | 1005 | * 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 Fields | b533184 | 2007-10-26 18:05:40 -0400 | [diff] [blame] | 1012 | * To handle those cases, we just bail out after a few iterations. |
Jeff Layton | 57b6532 | 2014-02-03 12:13:09 -0500 | [diff] [blame] | 1013 | * |
Jeff Layton | cff2fce | 2014-04-22 08:24:32 -0400 | [diff] [blame] | 1014 | * For FL_OFDLCK locks, the owner is the filp, not the files_struct. |
Jeff Layton | 57b6532 | 2014-02-03 12:13:09 -0500 | [diff] [blame] | 1015 | * 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 Layton | cff2fce | 2014-04-22 08:24:32 -0400 | [diff] [blame] | 1019 | * In principle, we could do a more limited deadlock detection on FL_OFDLCK |
Jeff Layton | 57b6532 | 2014-02-03 12:13:09 -0500 | [diff] [blame] | 1020 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | */ |
J. Bruce Fields | 97855b4 | 2007-10-30 11:20:02 -0400 | [diff] [blame] | 1023 | |
| 1024 | #define MAX_DEADLK_ITERATIONS 10 |
| 1025 | |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 1026 | /* Find a lock that the owner of the given @blocker is blocking on. */ |
| 1027 | static struct file_lock_core *what_owner_is_waiting_for(struct file_lock_core *blocker) |
J. Bruce Fields | b533184 | 2007-10-26 18:05:40 -0400 | [diff] [blame] | 1028 | { |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 1029 | struct file_lock_core *flc; |
J. Bruce Fields | b533184 | 2007-10-26 18:05:40 -0400 | [diff] [blame] | 1030 | |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 1031 | 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 Layton | b6aaba5 | 2024-01-31 18:02:07 -0500 | [diff] [blame] | 1034 | flc = flc->flc_blocker; |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 1035 | return flc; |
NeilBrown | 5946c43 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 1036 | } |
J. Bruce Fields | b533184 | 2007-10-26 18:05:40 -0400 | [diff] [blame] | 1037 | } |
| 1038 | return NULL; |
| 1039 | } |
| 1040 | |
Jeff Layton | 7b2296a | 2013-06-21 08:58:20 -0400 | [diff] [blame] | 1041 | /* Must be called with the blocked_lock_lock held! */ |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 1042 | static bool posix_locks_deadlock(struct file_lock *caller_fl, |
| 1043 | struct file_lock *block_fl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | { |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 1045 | struct file_lock_core *caller = &caller_fl->c; |
| 1046 | struct file_lock_core *blocker = &block_fl->c; |
J. Bruce Fields | 97855b4 | 2007-10-30 11:20:02 -0400 | [diff] [blame] | 1047 | int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | |
Daniel Wagner | 663d5af | 2015-04-03 09:04:03 -0400 | [diff] [blame] | 1049 | lockdep_assert_held(&blocked_lock_lock); |
| 1050 | |
Jeff Layton | 57b6532 | 2014-02-03 12:13:09 -0500 | [diff] [blame] | 1051 | /* |
| 1052 | * This deadlock detector can't reasonably detect deadlocks with |
Jeff Layton | cff2fce | 2014-04-22 08:24:32 -0400 | [diff] [blame] | 1053 | * FL_OFDLCK locks, since they aren't owned by a process, per-se. |
Jeff Layton | 57b6532 | 2014-02-03 12:13:09 -0500 | [diff] [blame] | 1054 | */ |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 1055 | if (caller->flc_flags & FL_OFDLCK) |
| 1056 | return false; |
Jeff Layton | 57b6532 | 2014-02-03 12:13:09 -0500 | [diff] [blame] | 1057 | |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 1058 | while ((blocker = what_owner_is_waiting_for(blocker))) { |
J. Bruce Fields | b533184 | 2007-10-26 18:05:40 -0400 | [diff] [blame] | 1059 | if (i++ > MAX_DEADLK_ITERATIONS) |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 1060 | return false; |
| 1061 | if (posix_same_owner(caller, blocker)) |
| 1062 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | } |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 1064 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | } |
| 1066 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks |
J. Bruce Fields | 02888f4 | 2007-09-12 15:45:07 -0400 | [diff] [blame] | 1068 | * after any leases, but before any posix locks. |
Trond Myklebust | f475ae9 | 2006-06-29 16:38:32 -0400 | [diff] [blame] | 1069 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | */ |
Jeff Layton | bcd7f78d | 2015-07-11 06:43:02 -0400 | [diff] [blame] | 1074 | static int flock_lock_inode(struct inode *inode, struct file_lock *request) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | { |
Trond Myklebust | 993dfa8 | 2006-03-31 02:30:55 -0800 | [diff] [blame] | 1076 | struct file_lock *new_fl = NULL; |
Jeff Layton | 5263e31 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1077 | struct file_lock *fl; |
| 1078 | struct file_lock_context *ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | int error = 0; |
Jeff Layton | 5263e31 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1080 | bool found = false; |
Jeff Layton | ed9814d | 2014-08-11 14:20:31 -0400 | [diff] [blame] | 1081 | LIST_HEAD(dispose); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1083 | ctx = locks_get_lock_context(inode, request->c.flc_type); |
Jeff Layton | 5c1c669 | 2015-04-03 09:04:03 -0400 | [diff] [blame] | 1084 | if (!ctx) { |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1085 | if (request->c.flc_type != F_UNLCK) |
Jeff Layton | 5c1c669 | 2015-04-03 09:04:03 -0400 | [diff] [blame] | 1086 | return -ENOMEM; |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1087 | return (request->c.flc_flags & FL_EXISTS) ? -ENOENT : 0; |
Jeff Layton | 5c1c669 | 2015-04-03 09:04:03 -0400 | [diff] [blame] | 1088 | } |
Jeff Layton | 5263e31 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1089 | |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1090 | if (!(request->c.flc_flags & FL_ACCESS) && (request->c.flc_type != F_UNLCK)) { |
Arnd Bergmann | b89f432 | 2010-09-18 15:09:31 +0200 | [diff] [blame] | 1091 | new_fl = locks_alloc_lock(); |
| 1092 | if (!new_fl) |
| 1093 | return -ENOMEM; |
| 1094 | } |
| 1095 | |
Peter Zijlstra | 02e525b2 | 2019-02-21 15:38:40 +0100 | [diff] [blame] | 1096 | percpu_down_read(&file_rwsem); |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 1097 | spin_lock(&ctx->flc_lock); |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1098 | if (request->c.flc_flags & FL_ACCESS) |
Trond Myklebust | f07f18d | 2006-06-29 16:38:37 -0400 | [diff] [blame] | 1099 | goto find_conflict; |
Pavel Emelyanov | 84d535a | 2007-09-11 16:38:13 +0400 | [diff] [blame] | 1100 | |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1101 | list_for_each_entry(fl, &ctx->flc_flock, c.flc_list) { |
| 1102 | if (request->c.flc_file != fl->c.flc_file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | continue; |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1104 | if (request->c.flc_type == fl->c.flc_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | goto out; |
Jeff Layton | 5263e31 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1106 | found = true; |
Jeff Layton | 7c18509 | 2024-01-31 18:02:12 -0500 | [diff] [blame] | 1107 | locks_delete_lock_ctx(&fl->c, &dispose); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | break; |
| 1109 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | |
Jeff Layton | 75cabec | 2024-01-31 18:01:45 -0500 | [diff] [blame] | 1111 | if (lock_is_unlock(request)) { |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1112 | if ((request->c.flc_flags & FL_EXISTS) && !found) |
Trond Myklebust | f475ae9 | 2006-06-29 16:38:32 -0400 | [diff] [blame] | 1113 | error = -ENOENT; |
Trond Myklebust | 993dfa8 | 2006-03-31 02:30:55 -0800 | [diff] [blame] | 1114 | goto out; |
Trond Myklebust | f475ae9 | 2006-06-29 16:38:32 -0400 | [diff] [blame] | 1115 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | |
Trond Myklebust | f07f18d | 2006-06-29 16:38:37 -0400 | [diff] [blame] | 1117 | find_conflict: |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1118 | list_for_each_entry(fl, &ctx->flc_flock, c.flc_list) { |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 1119 | if (!flock_locks_conflict(&request->c, &fl->c)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | continue; |
| 1121 | error = -EAGAIN; |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1122 | if (!(request->c.flc_flags & FL_SLEEP)) |
Miklos Szeredi | bde74e4 | 2008-07-25 01:48:57 -0700 | [diff] [blame] | 1123 | goto out; |
| 1124 | error = FILE_LOCK_DEFERRED; |
Jeff Layton | 269a619 | 2024-01-31 18:02:09 -0500 | [diff] [blame] | 1125 | locks_insert_block(&fl->c, &request->c, flock_locks_conflict); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | goto out; |
| 1127 | } |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1128 | if (request->c.flc_flags & FL_ACCESS) |
Trond Myklebust | f07f18d | 2006-06-29 16:38:37 -0400 | [diff] [blame] | 1129 | goto out; |
Trond Myklebust | 993dfa8 | 2006-03-31 02:30:55 -0800 | [diff] [blame] | 1130 | locks_copy_lock(new_fl, request); |
NeilBrown | 5946c43 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 1131 | locks_move_blocks(new_fl, request); |
Jeff Layton | 7c18509 | 2024-01-31 18:02:12 -0500 | [diff] [blame] | 1132 | locks_insert_lock_ctx(&new_fl->c, &ctx->flc_flock); |
Trond Myklebust | 993dfa8 | 2006-03-31 02:30:55 -0800 | [diff] [blame] | 1133 | new_fl = NULL; |
Kirill Korotaev | 9cedc19 | 2006-06-14 17:59:35 +0400 | [diff] [blame] | 1134 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | |
| 1136 | out: |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 1137 | spin_unlock(&ctx->flc_lock); |
Peter Zijlstra | 02e525b2 | 2019-02-21 15:38:40 +0100 | [diff] [blame] | 1138 | percpu_up_read(&file_rwsem); |
Trond Myklebust | 993dfa8 | 2006-03-31 02:30:55 -0800 | [diff] [blame] | 1139 | if (new_fl) |
| 1140 | locks_free_lock(new_fl); |
Jeff Layton | ed9814d | 2014-08-11 14:20:31 -0400 | [diff] [blame] | 1141 | locks_dispose_list(&dispose); |
Jeff Layton | c883da3 | 2018-07-30 07:54:56 -0400 | [diff] [blame] | 1142 | trace_flock_lock_inode(inode, request, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | return error; |
| 1144 | } |
| 1145 | |
Jeff Layton | b4d629a | 2016-01-07 18:27:42 -0500 | [diff] [blame] | 1146 | static int posix_lock_inode(struct inode *inode, struct file_lock *request, |
| 1147 | struct file_lock *conflock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | { |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1149 | struct file_lock *fl, *tmp; |
Miklos Szeredi | 39005d0 | 2006-06-23 02:05:10 -0700 | [diff] [blame] | 1150 | struct file_lock *new_fl = NULL; |
| 1151 | struct file_lock *new_fl2 = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | struct file_lock *left = NULL; |
| 1153 | struct file_lock *right = NULL; |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1154 | struct file_lock_context *ctx; |
Jeff Layton | b9746ef | 2013-06-21 08:58:13 -0400 | [diff] [blame] | 1155 | int error; |
| 1156 | bool added = false; |
Jeff Layton | ed9814d | 2014-08-11 14:20:31 -0400 | [diff] [blame] | 1157 | LIST_HEAD(dispose); |
Dai Ngo | 2443da2 | 2022-05-02 14:19:25 -0700 | [diff] [blame] | 1158 | void *owner; |
| 1159 | void (*func)(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1161 | ctx = locks_get_lock_context(inode, request->c.flc_type); |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1162 | if (!ctx) |
Jeff Layton | 75cabec | 2024-01-31 18:01:45 -0500 | [diff] [blame] | 1163 | return lock_is_unlock(request) ? 0 : -ENOMEM; |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | /* |
| 1166 | * We may need two file_lock structures for this operation, |
| 1167 | * so we get them in advance to avoid races. |
Miklos Szeredi | 39005d0 | 2006-06-23 02:05:10 -0700 | [diff] [blame] | 1168 | * |
| 1169 | * In some cases we can be sure, that no new locks will be needed |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | */ |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1171 | if (!(request->c.flc_flags & FL_ACCESS) && |
| 1172 | (request->c.flc_type != F_UNLCK || |
Miklos Szeredi | 39005d0 | 2006-06-23 02:05:10 -0700 | [diff] [blame] | 1173 | request->fl_start != 0 || request->fl_end != OFFSET_MAX)) { |
| 1174 | new_fl = locks_alloc_lock(); |
| 1175 | new_fl2 = locks_alloc_lock(); |
| 1176 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | |
Dai Ngo | 2443da2 | 2022-05-02 14:19:25 -0700 | [diff] [blame] | 1178 | retry: |
Peter Zijlstra | 02e525b2 | 2019-02-21 15:38:40 +0100 | [diff] [blame] | 1179 | percpu_down_read(&file_rwsem); |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 1180 | spin_lock(&ctx->flc_lock); |
Jeff Layton | 1cb3601 | 2013-06-21 08:58:12 -0400 | [diff] [blame] | 1181 | /* |
| 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 Layton | 48f7418 | 2013-06-21 08:58:18 -0400 | [diff] [blame] | 1184 | * blocker's list of waiters and the global blocked_hash. |
Jeff Layton | 1cb3601 | 2013-06-21 08:58:12 -0400 | [diff] [blame] | 1185 | */ |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1186 | if (request->c.flc_type != F_UNLCK) { |
| 1187 | list_for_each_entry(fl, &ctx->flc_posix, c.flc_list) { |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 1188 | if (!posix_locks_conflict(&request->c, &fl->c)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | continue; |
Dai Ngo | 2443da2 | 2022-05-02 14:19:25 -0700 | [diff] [blame] | 1190 | 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 Adamson | 5842add | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 1201 | if (conflock) |
Kinglong Mee | 3fe0fff1 | 2014-08-22 10:18:42 -0400 | [diff] [blame] | 1202 | locks_copy_conflock(conflock, fl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | error = -EAGAIN; |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1204 | if (!(request->c.flc_flags & FL_SLEEP)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | goto out; |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 1206 | /* |
| 1207 | * Deadlock detection and insertion into the blocked |
| 1208 | * locks list must be done while holding the same lock! |
| 1209 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | error = -EDEADLK; |
Jeff Layton | 7b2296a | 2013-06-21 08:58:20 -0400 | [diff] [blame] | 1211 | spin_lock(&blocked_lock_lock); |
Jeff Layton | 945ab8f | 2019-03-25 08:15:14 -0400 | [diff] [blame] | 1212 | /* |
| 1213 | * Ensure that we don't find any locks blocked on this |
| 1214 | * request during deadlock detection. |
| 1215 | */ |
Jeff Layton | 1a62c22 | 2024-01-31 18:02:05 -0500 | [diff] [blame] | 1216 | __locks_wake_up_blocks(&request->c); |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 1217 | if (likely(!posix_locks_deadlock(request, fl))) { |
| 1218 | error = FILE_LOCK_DEFERRED; |
Jeff Layton | 269a619 | 2024-01-31 18:02:09 -0500 | [diff] [blame] | 1219 | __locks_insert_block(&fl->c, &request->c, |
NeilBrown | fd7732e | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 1220 | posix_locks_conflict); |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 1221 | } |
Jeff Layton | 7b2296a | 2013-06-21 08:58:20 -0400 | [diff] [blame] | 1222 | spin_unlock(&blocked_lock_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | goto out; |
NeilBrown | 7bbd1fc | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 1224 | } |
| 1225 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | |
| 1227 | /* If we're just looking for a conflict, we're done. */ |
| 1228 | error = 0; |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1229 | if (request->c.flc_flags & FL_ACCESS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | goto out; |
| 1231 | |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1232 | /* Find the first old lock with the same owner as the new lock */ |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1233 | list_for_each_entry(fl, &ctx->flc_posix, c.flc_list) { |
Jeff Layton | 9bb430a | 2024-01-31 18:02:01 -0500 | [diff] [blame] | 1234 | if (posix_same_owner(&request->c, &fl->c)) |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1235 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | } |
| 1237 | |
Jeff Layton | 1cb3601 | 2013-06-21 08:58:12 -0400 | [diff] [blame] | 1238 | /* Process locks with this owner. */ |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1239 | list_for_each_entry_safe_from(fl, tmp, &ctx->flc_posix, c.flc_list) { |
Jeff Layton | 9bb430a | 2024-01-31 18:02:01 -0500 | [diff] [blame] | 1240 | if (!posix_same_owner(&request->c, &fl->c)) |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1241 | break; |
| 1242 | |
| 1243 | /* Detect adjacent or overlapping regions (if same lock type) */ |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1244 | if (request->c.flc_type == fl->c.flc_type) { |
Olaf Kirch | 449231d | 2005-08-25 16:25:35 -0700 | [diff] [blame] | 1245 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | if (fl->fl_end < request->fl_start - 1) |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1250 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | /* If the next lock in the list has entirely bigger |
| 1252 | * addresses than the new one, insert the lock here. |
| 1253 | */ |
Olaf Kirch | 449231d | 2005-08-25 16:25:35 -0700 | [diff] [blame] | 1254 | if (fl->fl_start - 1 > request->fl_end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | 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 Layton | 7c18509 | 2024-01-31 18:02:12 -0500 | [diff] [blame] | 1271 | locks_delete_lock_ctx(&fl->c, &dispose); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | continue; |
| 1273 | } |
| 1274 | request = fl; |
Jeff Layton | b9746ef | 2013-06-21 08:58:13 -0400 | [diff] [blame] | 1275 | added = true; |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1276 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | /* Processing for different lock types is a bit |
| 1278 | * more complex. |
| 1279 | */ |
| 1280 | if (fl->fl_end < request->fl_start) |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1281 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | if (fl->fl_start > request->fl_end) |
| 1283 | break; |
Jeff Layton | 75cabec | 2024-01-31 18:01:45 -0500 | [diff] [blame] | 1284 | if (lock_is_unlock(request)) |
Jeff Layton | b9746ef | 2013-06-21 08:58:13 -0400 | [diff] [blame] | 1285 | added = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | 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 Layton | 7c18509 | 2024-01-31 18:02:12 -0500 | [diff] [blame] | 1300 | locks_delete_lock_ctx(&fl->c, &dispose); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | continue; |
| 1302 | } |
Jeff Layton | b84d49f | 2014-08-12 08:03:49 -0400 | [diff] [blame] | 1303 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | */ |
Jeff Layton | b84d49f | 2014-08-12 08:03:49 -0400 | [diff] [blame] | 1310 | error = -ENOLCK; |
| 1311 | if (!new_fl) |
| 1312 | goto out; |
| 1313 | locks_copy_lock(new_fl, request); |
yangerkun | 5ef1596 | 2020-06-01 17:16:16 +0800 | [diff] [blame] | 1314 | locks_move_blocks(new_fl, request); |
Jeff Layton | b84d49f | 2014-08-12 08:03:49 -0400 | [diff] [blame] | 1315 | request = new_fl; |
| 1316 | new_fl = NULL; |
Jeff Layton | 7c18509 | 2024-01-31 18:02:12 -0500 | [diff] [blame] | 1317 | locks_insert_lock_ctx(&request->c, |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1318 | &fl->c.flc_list); |
Jeff Layton | 7c18509 | 2024-01-31 18:02:12 -0500 | [diff] [blame] | 1319 | locks_delete_lock_ctx(&fl->c, &dispose); |
Jeff Layton | b9746ef | 2013-06-21 08:58:13 -0400 | [diff] [blame] | 1320 | added = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | } |
| 1322 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | } |
| 1324 | |
Miklos Szeredi | 0d9a490 | 2006-06-23 02:05:09 -0700 | [diff] [blame] | 1325 | /* |
Jeff Layton | 1cb3601 | 2013-06-21 08:58:12 -0400 | [diff] [blame] | 1326 | * 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 Szeredi | 0d9a490 | 2006-06-23 02:05:09 -0700 | [diff] [blame] | 1329 | */ |
| 1330 | error = -ENOLCK; /* "no luck" */ |
| 1331 | if (right && left == right && !new_fl2) |
| 1332 | goto out; |
| 1333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | error = 0; |
| 1335 | if (!added) { |
Jeff Layton | 75cabec | 2024-01-31 18:01:45 -0500 | [diff] [blame] | 1336 | if (lock_is_unlock(request)) { |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1337 | if (request->c.flc_flags & FL_EXISTS) |
Trond Myklebust | f475ae9 | 2006-06-29 16:38:32 -0400 | [diff] [blame] | 1338 | error = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | goto out; |
Trond Myklebust | f475ae9 | 2006-06-29 16:38:32 -0400 | [diff] [blame] | 1340 | } |
Miklos Szeredi | 0d9a490 | 2006-06-23 02:05:09 -0700 | [diff] [blame] | 1341 | |
| 1342 | if (!new_fl) { |
| 1343 | error = -ENOLCK; |
| 1344 | goto out; |
| 1345 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | locks_copy_lock(new_fl, request); |
NeilBrown | 5946c43 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 1347 | locks_move_blocks(new_fl, request); |
Jeff Layton | 7c18509 | 2024-01-31 18:02:12 -0500 | [diff] [blame] | 1348 | locks_insert_lock_ctx(&new_fl->c, &fl->c.flc_list); |
Jeff Layton | 2e2f756f | 2015-02-17 17:08:23 -0500 | [diff] [blame] | 1349 | fl = new_fl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | 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 Layton | 7c18509 | 2024-01-31 18:02:12 -0500 | [diff] [blame] | 1360 | locks_insert_lock_ctx(&left->c, &fl->c.flc_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | } |
| 1362 | right->fl_start = request->fl_end + 1; |
Jeff Layton | 347d49f | 2024-01-31 18:02:11 -0500 | [diff] [blame] | 1363 | locks_wake_up_blocks(&right->c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | } |
| 1365 | if (left) { |
| 1366 | left->fl_end = request->fl_start - 1; |
Jeff Layton | 347d49f | 2024-01-31 18:02:11 -0500 | [diff] [blame] | 1367 | locks_wake_up_blocks(&left->c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | } |
| 1369 | out: |
Jeff Layton | 1b3ec4f | 2024-07-02 18:44:48 -0400 | [diff] [blame] | 1370 | trace_posix_lock_inode(inode, request, error); |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 1371 | spin_unlock(&ctx->flc_lock); |
Peter Zijlstra | 02e525b2 | 2019-02-21 15:38:40 +0100 | [diff] [blame] | 1372 | percpu_up_read(&file_rwsem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | /* |
| 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 Layton | ed9814d | 2014-08-11 14:20:31 -0400 | [diff] [blame] | 1380 | locks_dispose_list(&dispose); |
Jeff Layton | 1890910 | 2016-01-06 21:26:10 -0500 | [diff] [blame] | 1381 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | 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 Eshel | 150b393 | 2007-01-18 16:15:35 -0500 | [diff] [blame] | 1389 | * @conflock: Place to return a copy of the conflicting lock, if found. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | * |
| 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 Myklebust | f475ae9 | 2006-06-29 16:38:32 -0400 | [diff] [blame] | 1394 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | */ |
Marc Eshel | 150b393 | 2007-01-18 16:15:35 -0500 | [diff] [blame] | 1399 | int posix_lock_file(struct file *filp, struct file_lock *fl, |
Andy Adamson | 5842add | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 1400 | struct file_lock *conflock) |
| 1401 | { |
Jeff Layton | c65454a | 2022-11-25 08:48:37 -0500 | [diff] [blame] | 1402 | return posix_lock_inode(file_inode(filp), fl, conflock); |
Andy Adamson | 5842add | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 1403 | } |
Marc Eshel | 150b393 | 2007-01-18 16:15:35 -0500 | [diff] [blame] | 1404 | EXPORT_SYMBOL(posix_lock_file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | |
| 1406 | /** |
Jeff Layton | 29d01b2 | 2015-07-11 06:43:02 -0400 | [diff] [blame] | 1407 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | * @fl: The lock to be applied |
| 1410 | * |
Benjamin Coddington | 616fb38 | 2015-10-22 13:38:15 -0400 | [diff] [blame] | 1411 | * Apply a POSIX style lock request to an inode. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | */ |
Benjamin Coddington | 616fb38 | 2015-10-22 13:38:15 -0400 | [diff] [blame] | 1413 | static int posix_lock_inode_wait(struct inode *inode, struct file_lock *fl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | { |
| 1415 | int error; |
| 1416 | might_sleep (); |
| 1417 | for (;;) { |
Jeff Layton | b4d629a | 2016-01-07 18:27:42 -0500 | [diff] [blame] | 1418 | error = posix_lock_inode(inode, fl, NULL); |
Miklos Szeredi | bde74e4 | 2008-07-25 01:48:57 -0700 | [diff] [blame] | 1419 | if (error != FILE_LOCK_DEFERRED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | break; |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1421 | error = wait_event_interruptible(fl->c.flc_wait, |
| 1422 | list_empty(&fl->c.flc_blocked_member)); |
NeilBrown | 16306a6 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 1423 | if (error) |
| 1424 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | } |
NeilBrown | 16306a6 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 1426 | locks_delete_block(fl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | return error; |
| 1428 | } |
Jeff Layton | 29d01b2 | 2015-07-11 06:43:02 -0400 | [diff] [blame] | 1429 | |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 1430 | static void lease_clear_pending(struct file_lease *fl, int arg) |
J. Bruce Fields | 778fc54 | 2011-07-26 18:25:49 -0400 | [diff] [blame] | 1431 | { |
| 1432 | switch (arg) { |
| 1433 | case F_UNLCK: |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1434 | fl->c.flc_flags &= ~FL_UNLOCK_PENDING; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 1435 | fallthrough; |
J. Bruce Fields | 778fc54 | 2011-07-26 18:25:49 -0400 | [diff] [blame] | 1436 | case F_RDLCK: |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1437 | fl->c.flc_flags &= ~FL_DOWNGRADE_PENDING; |
J. Bruce Fields | 778fc54 | 2011-07-26 18:25:49 -0400 | [diff] [blame] | 1438 | } |
| 1439 | } |
| 1440 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | /* We already had a lease on this file; just change its type */ |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 1442 | int lease_modify(struct file_lease *fl, int arg, struct list_head *dispose) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | { |
Jeff Layton | d9077f7 | 2024-01-31 18:02:10 -0500 | [diff] [blame] | 1444 | int error = assign_type(&fl->c, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | |
| 1446 | if (error) |
| 1447 | return error; |
J. Bruce Fields | 778fc54 | 2011-07-26 18:25:49 -0400 | [diff] [blame] | 1448 | lease_clear_pending(fl, arg); |
Jeff Layton | 347d49f | 2024-01-31 18:02:11 -0500 | [diff] [blame] | 1449 | locks_wake_up_blocks(&fl->c); |
Filipe Brandenburger | 3b6e2723 | 2012-07-27 00:42:52 -0400 | [diff] [blame] | 1450 | if (arg == F_UNLCK) { |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1451 | struct file *filp = fl->c.flc_file; |
Filipe Brandenburger | 3b6e2723 | 2012-07-27 00:42:52 -0400 | [diff] [blame] | 1452 | |
| 1453 | f_delown(filp); |
| 1454 | filp->f_owner.signum = 0; |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1455 | fasync_helper(0, fl->c.flc_file, 0, &fl->fl_fasync); |
J. Bruce Fields | 96d6d59 | 2012-07-27 16:18:00 -0400 | [diff] [blame] | 1456 | 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 Layton | 7c18509 | 2024-01-31 18:02:12 -0500 | [diff] [blame] | 1460 | locks_delete_lock_ctx(&fl->c, dispose); |
Filipe Brandenburger | 3b6e2723 | 2012-07-27 00:42:52 -0400 | [diff] [blame] | 1461 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | return 0; |
| 1463 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | EXPORT_SYMBOL(lease_modify); |
| 1465 | |
J. Bruce Fields | 778fc54 | 2011-07-26 18:25:49 -0400 | [diff] [blame] | 1466 | static 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 Layton | c45198e | 2014-09-01 07:12:07 -0400 | [diff] [blame] | 1474 | static void time_out_leases(struct inode *inode, struct list_head *dispose) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | { |
Jeff Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1476 | struct file_lock_context *ctx = inode->i_flctx; |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 1477 | struct file_lease *fl, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 1479 | lockdep_assert_held(&ctx->flc_lock); |
Jeff Layton | f82b4b6 | 2014-08-22 18:50:48 -0400 | [diff] [blame] | 1480 | |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1481 | list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, c.flc_list) { |
Jeff Layton | 62af4f1 | 2014-05-09 14:13:05 -0400 | [diff] [blame] | 1482 | trace_time_out_leases(inode, fl); |
J. Bruce Fields | 778fc54 | 2011-07-26 18:25:49 -0400 | [diff] [blame] | 1483 | if (past_time(fl->fl_downgrade_time)) |
Jeff Layton | 7448cc3 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 1484 | lease_modify(fl, F_RDLCK, dispose); |
J. Bruce Fields | 778fc54 | 2011-07-26 18:25:49 -0400 | [diff] [blame] | 1485 | if (past_time(fl->fl_break_time)) |
Jeff Layton | 7448cc3 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 1486 | lease_modify(fl, F_UNLCK, dispose); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | } |
| 1488 | } |
| 1489 | |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 1490 | static bool leases_conflict(struct file_lock_core *lc, struct file_lock_core *bc) |
J. Bruce Fields | df4e8d2 | 2012-03-05 13:18:59 -0500 | [diff] [blame] | 1491 | { |
Ira Weiny | d51f527 | 2019-06-05 18:45:34 -0700 | [diff] [blame] | 1492 | bool rc; |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 1493 | struct file_lease *lease = file_lease(lc); |
| 1494 | struct file_lease *breaker = file_lease(bc); |
Ira Weiny | d51f527 | 2019-06-05 18:45:34 -0700 | [diff] [blame] | 1495 | |
J. Bruce Fields | 28df3d1 | 2017-07-28 16:35:15 -0400 | [diff] [blame] | 1496 | if (lease->fl_lmops->lm_breaker_owns_lease |
| 1497 | && lease->fl_lmops->lm_breaker_owns_lease(lease)) |
| 1498 | return false; |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 1499 | if ((bc->flc_flags & FL_LAYOUT) != (lc->flc_flags & FL_LAYOUT)) { |
Ira Weiny | d51f527 | 2019-06-05 18:45:34 -0700 | [diff] [blame] | 1500 | rc = false; |
| 1501 | goto trace; |
| 1502 | } |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 1503 | if ((bc->flc_flags & FL_DELEG) && (lc->flc_flags & FL_LEASE)) { |
Ira Weiny | d51f527 | 2019-06-05 18:45:34 -0700 | [diff] [blame] | 1504 | rc = false; |
| 1505 | goto trace; |
| 1506 | } |
| 1507 | |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 1508 | rc = locks_conflict(bc, lc); |
Ira Weiny | d51f527 | 2019-06-05 18:45:34 -0700 | [diff] [blame] | 1509 | trace: |
| 1510 | trace_leases_conflict(rc, lease, breaker); |
| 1511 | return rc; |
J. Bruce Fields | df4e8d2 | 2012-03-05 13:18:59 -0500 | [diff] [blame] | 1512 | } |
| 1513 | |
Jeff Layton | 03d12dd | 2014-09-01 14:53:41 -0400 | [diff] [blame] | 1514 | static bool |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 1515 | any_leases_conflict(struct inode *inode, struct file_lease *breaker) |
Jeff Layton | 03d12dd | 2014-09-01 14:53:41 -0400 | [diff] [blame] | 1516 | { |
Jeff Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1517 | struct file_lock_context *ctx = inode->i_flctx; |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 1518 | struct file_lock_core *flc; |
Jeff Layton | 03d12dd | 2014-09-01 14:53:41 -0400 | [diff] [blame] | 1519 | |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 1520 | lockdep_assert_held(&ctx->flc_lock); |
Jeff Layton | 03d12dd | 2014-09-01 14:53:41 -0400 | [diff] [blame] | 1521 | |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 1522 | list_for_each_entry(flc, &ctx->flc_lease, flc_list) { |
| 1523 | if (leases_conflict(flc, &breaker->c)) |
Jeff Layton | 03d12dd | 2014-09-01 14:53:41 -0400 | [diff] [blame] | 1524 | return true; |
| 1525 | } |
| 1526 | return false; |
| 1527 | } |
| 1528 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | /** |
| 1530 | * __break_lease - revoke all outstanding leases on file |
| 1531 | * @inode: the inode of the file to return |
J. Bruce Fields | df4e8d2 | 2012-03-05 13:18:59 -0500 | [diff] [blame] | 1532 | * @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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | * |
david m. richter | 87250dd | 2007-05-09 16:10:27 -0400 | [diff] [blame] | 1537 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | * specified %O_NONBLOCK to your open(). |
| 1541 | */ |
J. Bruce Fields | df4e8d2 | 2012-03-05 13:18:59 -0500 | [diff] [blame] | 1542 | int __break_lease(struct inode *inode, unsigned int mode, unsigned int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | { |
J. Bruce Fields | 778fc54 | 2011-07-26 18:25:49 -0400 | [diff] [blame] | 1544 | int error = 0; |
Dmitry Vyukov | 128a378 | 2015-09-21 09:43:06 +0200 | [diff] [blame] | 1545 | struct file_lock_context *ctx; |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 1546 | struct file_lease *new_fl, *fl, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | unsigned long break_time; |
Al Viro | 8737c93 | 2009-12-24 06:47:55 -0500 | [diff] [blame] | 1548 | int want_write = (mode & O_ACCMODE) != O_RDONLY; |
Jeff Layton | c45198e | 2014-09-01 07:12:07 -0400 | [diff] [blame] | 1549 | LIST_HEAD(dispose); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | |
Al Viro | 8737c93 | 2009-12-24 06:47:55 -0500 | [diff] [blame] | 1551 | new_fl = lease_alloc(NULL, want_write ? F_WRLCK : F_RDLCK); |
Linus Torvalds | 6d4b9e3 | 2011-12-26 10:25:26 -0800 | [diff] [blame] | 1552 | if (IS_ERR(new_fl)) |
| 1553 | return PTR_ERR(new_fl); |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1554 | new_fl->c.flc_flags = type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | |
Jeff Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1556 | /* typically we will check that ctx is non-NULL before calling */ |
Jeff Layton | 401a8b8 | 2022-11-16 09:02:30 -0500 | [diff] [blame] | 1557 | ctx = locks_inode_context(inode); |
Jeff Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1558 | if (!ctx) { |
| 1559 | WARN_ON_ONCE(1); |
Wenwen Wang | cfddf9f | 2019-08-19 18:47:34 -0500 | [diff] [blame] | 1560 | goto free_lock; |
Jeff Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1561 | } |
| 1562 | |
Peter Zijlstra | 02e525b2 | 2019-02-21 15:38:40 +0100 | [diff] [blame] | 1563 | percpu_down_read(&file_rwsem); |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 1564 | spin_lock(&ctx->flc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | |
Jeff Layton | c45198e | 2014-09-01 07:12:07 -0400 | [diff] [blame] | 1566 | time_out_leases(inode, &dispose); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | |
Jeff Layton | 03d12dd | 2014-09-01 14:53:41 -0400 | [diff] [blame] | 1568 | if (!any_leases_conflict(inode, new_fl)) |
J. Bruce Fields | 778fc54 | 2011-07-26 18:25:49 -0400 | [diff] [blame] | 1569 | goto out; |
| 1570 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | 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 Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1578 | list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, c.flc_list) { |
Jeff Layton | b6be371 | 2024-01-31 18:02:06 -0500 | [diff] [blame] | 1579 | if (!leases_conflict(&fl->c, &new_fl->c)) |
J. Bruce Fields | df4e8d2 | 2012-03-05 13:18:59 -0500 | [diff] [blame] | 1580 | continue; |
J. Bruce Fields | 778fc54 | 2011-07-26 18:25:49 -0400 | [diff] [blame] | 1581 | if (want_write) { |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1582 | if (fl->c.flc_flags & FL_UNLOCK_PENDING) |
J. Bruce Fields | 778fc54 | 2011-07-26 18:25:49 -0400 | [diff] [blame] | 1583 | continue; |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1584 | fl->c.flc_flags |= FL_UNLOCK_PENDING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | fl->fl_break_time = break_time; |
J. Bruce Fields | 778fc54 | 2011-07-26 18:25:49 -0400 | [diff] [blame] | 1586 | } else { |
Jeff Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1587 | if (lease_breaking(fl)) |
J. Bruce Fields | 778fc54 | 2011-07-26 18:25:49 -0400 | [diff] [blame] | 1588 | continue; |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1589 | fl->c.flc_flags |= FL_DOWNGRADE_PENDING; |
J. Bruce Fields | 778fc54 | 2011-07-26 18:25:49 -0400 | [diff] [blame] | 1590 | fl->fl_downgrade_time = break_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | } |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 1592 | if (fl->fl_lmops->lm_break(fl)) |
Jeff Layton | 7c18509 | 2024-01-31 18:02:12 -0500 | [diff] [blame] | 1593 | locks_delete_lock_ctx(&fl->c, &dispose); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | } |
| 1595 | |
Jeff Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1596 | if (list_empty(&ctx->flc_lease)) |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 1597 | goto out; |
| 1598 | |
Jeff Layton | 843c6b2 | 2014-09-01 14:27:43 -0400 | [diff] [blame] | 1599 | if (mode & O_NONBLOCK) { |
Jeff Layton | 62af4f1 | 2014-05-09 14:13:05 -0400 | [diff] [blame] | 1600 | trace_break_lease_noblock(inode, new_fl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | error = -EWOULDBLOCK; |
| 1602 | goto out; |
| 1603 | } |
| 1604 | |
| 1605 | restart: |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 1606 | fl = list_first_entry(&ctx->flc_lease, struct file_lease, c.flc_list); |
Jeff Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1607 | break_time = fl->fl_break_time; |
Jeff Layton | f1c6bb2 | 2014-04-15 06:17:49 -0400 | [diff] [blame] | 1608 | if (break_time != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | break_time -= jiffies; |
Jeff Layton | f1c6bb2 | 2014-04-15 06:17:49 -0400 | [diff] [blame] | 1610 | if (break_time == 0) |
| 1611 | break_time++; |
Jeff Layton | 269a619 | 2024-01-31 18:02:09 -0500 | [diff] [blame] | 1612 | locks_insert_block(&fl->c, &new_fl->c, leases_conflict); |
Jeff Layton | 62af4f1 | 2014-05-09 14:13:05 -0400 | [diff] [blame] | 1613 | trace_break_lease_block(inode, new_fl); |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 1614 | spin_unlock(&ctx->flc_lock); |
Peter Zijlstra | 02e525b2 | 2019-02-21 15:38:40 +0100 | [diff] [blame] | 1615 | percpu_up_read(&file_rwsem); |
Peter Zijlstra | aba3766 | 2015-06-22 14:16:33 +0200 | [diff] [blame] | 1616 | |
Jeff Layton | c45198e | 2014-09-01 07:12:07 -0400 | [diff] [blame] | 1617 | locks_dispose_list(&dispose); |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1618 | error = wait_event_interruptible_timeout(new_fl->c.flc_wait, |
| 1619 | list_empty(&new_fl->c.flc_blocked_member), |
| 1620 | break_time); |
Peter Zijlstra | aba3766 | 2015-06-22 14:16:33 +0200 | [diff] [blame] | 1621 | |
Peter Zijlstra | 02e525b2 | 2019-02-21 15:38:40 +0100 | [diff] [blame] | 1622 | percpu_down_read(&file_rwsem); |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 1623 | spin_lock(&ctx->flc_lock); |
Jeff Layton | 62af4f1 | 2014-05-09 14:13:05 -0400 | [diff] [blame] | 1624 | trace_break_lease_unblock(inode, new_fl); |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 1625 | __locks_delete_block(&new_fl->c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | if (error >= 0) { |
J. Bruce Fields | 778fc54 | 2011-07-26 18:25:49 -0400 | [diff] [blame] | 1627 | /* |
| 1628 | * Wait for the next conflicting lease that has not been |
| 1629 | * broken yet |
| 1630 | */ |
Jeff Layton | 03d12dd | 2014-09-01 14:53:41 -0400 | [diff] [blame] | 1631 | if (error == 0) |
| 1632 | time_out_leases(inode, &dispose); |
| 1633 | if (any_leases_conflict(inode, new_fl)) |
| 1634 | goto restart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | error = 0; |
| 1636 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | out: |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 1638 | spin_unlock(&ctx->flc_lock); |
Peter Zijlstra | 02e525b2 | 2019-02-21 15:38:40 +0100 | [diff] [blame] | 1639 | percpu_up_read(&file_rwsem); |
Jeff Layton | c45198e | 2014-09-01 07:12:07 -0400 | [diff] [blame] | 1640 | locks_dispose_list(&dispose); |
Wenwen Wang | cfddf9f | 2019-08-19 18:47:34 -0500 | [diff] [blame] | 1641 | free_lock: |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 1642 | locks_free_lease(new_fl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | return error; |
| 1644 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | EXPORT_SYMBOL(__break_lease); |
| 1646 | |
| 1647 | /** |
Amir Goldstein | 76c4794 | 2018-01-03 17:14:34 +0200 | [diff] [blame] | 1648 | * lease_get_mtime - update modified time of an inode with exclusive lease |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | * @inode: the inode |
Amir Goldstein | 76c4794 | 2018-01-03 17:14:34 +0200 | [diff] [blame] | 1650 | * @time: pointer to a timespec which contains the last modified time |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | * |
| 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 Dunlap | a6b9191 | 2008-03-19 17:01:00 -0700 | [diff] [blame] | 1654 | * exclusive lease, then they could be modifying it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | */ |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 1656 | void lease_get_mtime(struct inode *inode, struct timespec64 *time) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | { |
Jeff Layton | bfe8602 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 1658 | bool has_lease = false; |
Dmitry Vyukov | 128a378 | 2015-09-21 09:43:06 +0200 | [diff] [blame] | 1659 | struct file_lock_context *ctx; |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 1660 | struct file_lock_core *flc; |
Jeff Layton | bfe8602 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 1661 | |
Jeff Layton | 401a8b8 | 2022-11-16 09:02:30 -0500 | [diff] [blame] | 1662 | ctx = locks_inode_context(inode); |
Jeff Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1663 | if (ctx && !list_empty_careful(&ctx->flc_lease)) { |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 1664 | spin_lock(&ctx->flc_lock); |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 1665 | 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 Tang | 8ace5df | 2015-11-18 21:40:33 +0800 | [diff] [blame] | 1668 | has_lease = true; |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 1669 | spin_unlock(&ctx->flc_lock); |
Jeff Layton | bfe8602 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 1670 | } |
| 1671 | |
| 1672 | if (has_lease) |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 1673 | *time = current_time(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | EXPORT_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 | */ |
| 1700 | int fcntl_getlease(struct file *filp) |
| 1701 | { |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 1702 | struct file_lease *fl; |
Jeff Layton | c65454a | 2022-11-25 08:48:37 -0500 | [diff] [blame] | 1703 | struct inode *inode = file_inode(filp); |
Dmitry Vyukov | 128a378 | 2015-09-21 09:43:06 +0200 | [diff] [blame] | 1704 | struct file_lock_context *ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | int type = F_UNLCK; |
Jeff Layton | c45198e | 2014-09-01 07:12:07 -0400 | [diff] [blame] | 1706 | LIST_HEAD(dispose); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | |
Jeff Layton | 401a8b8 | 2022-11-16 09:02:30 -0500 | [diff] [blame] | 1708 | ctx = locks_inode_context(inode); |
Jeff Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1709 | if (ctx && !list_empty_careful(&ctx->flc_lease)) { |
Peter Zijlstra | 02e525b2 | 2019-02-21 15:38:40 +0100 | [diff] [blame] | 1710 | percpu_down_read(&file_rwsem); |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 1711 | spin_lock(&ctx->flc_lock); |
Miklos Szeredi | c568d68 | 2016-09-16 12:44:20 +0200 | [diff] [blame] | 1712 | time_out_leases(inode, &dispose); |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1713 | list_for_each_entry(fl, &ctx->flc_lease, c.flc_list) { |
| 1714 | if (fl->c.flc_file != filp) |
Jeff Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1715 | continue; |
J. Bruce Fields | 778fc54 | 2011-07-26 18:25:49 -0400 | [diff] [blame] | 1716 | type = target_leasetype(fl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | break; |
| 1718 | } |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 1719 | spin_unlock(&ctx->flc_lock); |
Peter Zijlstra | 02e525b2 | 2019-02-21 15:38:40 +0100 | [diff] [blame] | 1720 | percpu_up_read(&file_rwsem); |
Peter Zijlstra | 5f43086 | 2016-10-08 10:12:28 +0200 | [diff] [blame] | 1721 | |
Jeff Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1722 | locks_dispose_list(&dispose); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | return type; |
| 1725 | } |
| 1726 | |
Jeff Layton | 24cbe78 | 2014-02-03 12:13:06 -0500 | [diff] [blame] | 1727 | /** |
Amir Goldstein | 387e374 | 2019-06-07 17:24:38 +0300 | [diff] [blame] | 1728 | * check_conflicting_open - see if the given file points to an inode that has |
NeilBrown | 7bbd1fc | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 1729 | * an existing open that would conflict with the |
| 1730 | * desired lease. |
Amir Goldstein | 387e374 | 2019-06-07 17:24:38 +0300 | [diff] [blame] | 1731 | * @filp: file to check |
Jeff Layton | 24cbe78 | 2014-02-03 12:13:06 -0500 | [diff] [blame] | 1732 | * @arg: type of lease that we're trying to acquire |
Randy Dunlap | 7fadc59 | 2015-08-09 18:43:17 -0700 | [diff] [blame] | 1733 | * @flags: current lock flags |
Jeff Layton | 24cbe78 | 2014-02-03 12:13:06 -0500 | [diff] [blame] | 1734 | * |
| 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 | */ |
| 1738 | static int |
Luca Vizzarro | ed5f17f | 2023-02-01 15:05:33 +0000 | [diff] [blame] | 1739 | check_conflicting_open(struct file *filp, const int arg, int flags) |
Jeff Layton | 24cbe78 | 2014-02-03 12:13:06 -0500 | [diff] [blame] | 1740 | { |
Jeff Layton | c65454a | 2022-11-25 08:48:37 -0500 | [diff] [blame] | 1741 | struct inode *inode = file_inode(filp); |
Amir Goldstein | 387e374 | 2019-06-07 17:24:38 +0300 | [diff] [blame] | 1742 | int self_wcount = 0, self_rcount = 0; |
Jeff Layton | 24cbe78 | 2014-02-03 12:13:06 -0500 | [diff] [blame] | 1743 | |
Christoph Hellwig | 11afe9f | 2015-01-21 19:17:03 +0100 | [diff] [blame] | 1744 | if (flags & FL_LAYOUT) |
| 1745 | return 0; |
J. Bruce Fields | aba2072 | 2021-04-16 14:00:18 -0400 | [diff] [blame] | 1746 | if (flags & FL_DELEG) |
| 1747 | /* We leave these checks to the caller */ |
| 1748 | return 0; |
Christoph Hellwig | 11afe9f | 2015-01-21 19:17:03 +0100 | [diff] [blame] | 1749 | |
Amir Goldstein | 387e374 | 2019-06-07 17:24:38 +0300 | [diff] [blame] | 1750 | 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 Layton | 24cbe78 | 2014-02-03 12:13:06 -0500 | [diff] [blame] | 1768 | return -EAGAIN; |
| 1769 | |
Amir Goldstein | 387e374 | 2019-06-07 17:24:38 +0300 | [diff] [blame] | 1770 | return 0; |
Jeff Layton | 24cbe78 | 2014-02-03 12:13:06 -0500 | [diff] [blame] | 1771 | } |
| 1772 | |
Jeff Layton | e6f5c78 | 2014-08-22 10:40:25 -0400 | [diff] [blame] | 1773 | static int |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 1774 | generic_add_lease(struct file *filp, int arg, struct file_lease **flp, void **priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | { |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 1776 | struct file_lease *fl, *my_fl = NULL, *lease; |
Jeff Layton | c65454a | 2022-11-25 08:48:37 -0500 | [diff] [blame] | 1777 | struct inode *inode = file_inode(filp); |
Jeff Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1778 | struct file_lock_context *ctx; |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1779 | bool is_deleg = (*flp)->c.flc_flags & FL_DELEG; |
J. Bruce Fields | c1f24ef | 2011-08-19 10:59:49 -0400 | [diff] [blame] | 1780 | int error; |
Jeff Layton | c45198e | 2014-09-01 07:12:07 -0400 | [diff] [blame] | 1781 | LIST_HEAD(dispose); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | |
J. Bruce Fields | 096657b | 2010-10-30 17:31:14 -0400 | [diff] [blame] | 1783 | lease = *flp; |
Jeff Layton | 62af4f1 | 2014-05-09 14:13:05 -0400 | [diff] [blame] | 1784 | trace_generic_add_lease(inode, lease); |
| 1785 | |
Jeff Layton | 5c1c669 | 2015-04-03 09:04:03 -0400 | [diff] [blame] | 1786 | /* Note that arg is never F_UNLCK here */ |
| 1787 | ctx = locks_get_lock_context(inode, arg); |
Jeff Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1788 | if (!ctx) |
| 1789 | return -ENOMEM; |
| 1790 | |
J. Bruce Fields | df4e8d2 | 2012-03-05 13:18:59 -0500 | [diff] [blame] | 1791 | /* |
| 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 Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 1799 | if (is_deleg && !inode_trylock(inode)) |
J. Bruce Fields | df4e8d2 | 2012-03-05 13:18:59 -0500 | [diff] [blame] | 1800 | return -EAGAIN; |
| 1801 | |
Peter Zijlstra | 02e525b2 | 2019-02-21 15:38:40 +0100 | [diff] [blame] | 1802 | percpu_down_read(&file_rwsem); |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 1803 | spin_lock(&ctx->flc_lock); |
Jeff Layton | c45198e | 2014-09-01 07:12:07 -0400 | [diff] [blame] | 1804 | time_out_leases(inode, &dispose); |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1805 | error = check_conflicting_open(filp, arg, lease->c.flc_flags); |
Jeff Layton | 24cbe78 | 2014-02-03 12:13:06 -0500 | [diff] [blame] | 1806 | if (error) |
J. Bruce Fields | 096657b | 2010-10-30 17:31:14 -0400 | [diff] [blame] | 1807 | goto out; |
Pavel Emelyanov | 85c5958 | 2007-09-20 12:45:02 +0400 | [diff] [blame] | 1808 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | /* |
| 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 Fields | c1f24ef | 2011-08-19 10:59:49 -0400 | [diff] [blame] | 1817 | error = -EAGAIN; |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1818 | 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 Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1821 | my_fl = fl; |
J. Bruce Fields | c1f24ef | 2011-08-19 10:59:49 -0400 | [diff] [blame] | 1822 | continue; |
| 1823 | } |
Jeff Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1824 | |
J. Bruce Fields | c1f24ef | 2011-08-19 10:59:49 -0400 | [diff] [blame] | 1825 | /* |
| 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 Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1835 | if (fl->c.flc_flags & FL_UNLOCK_PENDING) |
J. Bruce Fields | c1f24ef | 2011-08-19 10:59:49 -0400 | [diff] [blame] | 1836 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | } |
| 1838 | |
Jeff Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1839 | if (my_fl != NULL) { |
Jeff Layton | 0164bf0 | 2015-03-04 17:34:32 -0500 | [diff] [blame] | 1840 | lease = my_fl; |
| 1841 | error = lease->fl_lmops->lm_change(lease, arg, &dispose); |
Jeff Layton | 1c7dd2f | 2014-08-22 10:55:47 -0400 | [diff] [blame] | 1842 | if (error) |
| 1843 | goto out; |
| 1844 | goto out_setup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1845 | } |
| 1846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | error = -EINVAL; |
| 1848 | if (!leases_enable) |
| 1849 | goto out; |
| 1850 | |
Jeff Layton | 7c18509 | 2024-01-31 18:02:12 -0500 | [diff] [blame] | 1851 | locks_insert_lock_ctx(&lease->c, &ctx->flc_lease); |
Jeff Layton | 24cbe78 | 2014-02-03 12:13:06 -0500 | [diff] [blame] | 1852 | /* |
| 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 Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1862 | error = check_conflicting_open(filp, arg, lease->c.flc_flags); |
Jeff Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1863 | if (error) { |
Jeff Layton | 7c18509 | 2024-01-31 18:02:12 -0500 | [diff] [blame] | 1864 | locks_unlink_lock_ctx(&lease->c); |
Jeff Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1865 | goto out; |
| 1866 | } |
Jeff Layton | 1c7dd2f | 2014-08-22 10:55:47 -0400 | [diff] [blame] | 1867 | |
| 1868 | out_setup: |
| 1869 | if (lease->fl_lmops->lm_setup) |
| 1870 | lease->fl_lmops->lm_setup(lease, priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | out: |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 1872 | spin_unlock(&ctx->flc_lock); |
Peter Zijlstra | 02e525b2 | 2019-02-21 15:38:40 +0100 | [diff] [blame] | 1873 | percpu_up_read(&file_rwsem); |
Jeff Layton | c45198e | 2014-09-01 07:12:07 -0400 | [diff] [blame] | 1874 | locks_dispose_list(&dispose); |
J. Bruce Fields | df4e8d2 | 2012-03-05 13:18:59 -0500 | [diff] [blame] | 1875 | if (is_deleg) |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 1876 | inode_unlock(inode); |
Jeff Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1877 | if (!error && !my_fl) |
Jeff Layton | 1c7dd2f | 2014-08-22 10:55:47 -0400 | [diff] [blame] | 1878 | *flp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1879 | return error; |
| 1880 | } |
J. Bruce Fields | 8335ebd | 2011-09-21 08:34:32 -0400 | [diff] [blame] | 1881 | |
Christoph Hellwig | 2ab99ee | 2015-01-21 19:14:02 +0100 | [diff] [blame] | 1882 | static int generic_delete_lease(struct file *filp, void *owner) |
J. Bruce Fields | 8335ebd | 2011-09-21 08:34:32 -0400 | [diff] [blame] | 1883 | { |
Jeff Layton | 0efaa7e | 2014-08-22 10:18:45 -0400 | [diff] [blame] | 1884 | int error = -EAGAIN; |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 1885 | struct file_lease *fl, *victim = NULL; |
Jeff Layton | c65454a | 2022-11-25 08:48:37 -0500 | [diff] [blame] | 1886 | struct inode *inode = file_inode(filp); |
Dmitry Vyukov | 128a378 | 2015-09-21 09:43:06 +0200 | [diff] [blame] | 1887 | struct file_lock_context *ctx; |
Jeff Layton | c45198e | 2014-09-01 07:12:07 -0400 | [diff] [blame] | 1888 | LIST_HEAD(dispose); |
J. Bruce Fields | 8335ebd | 2011-09-21 08:34:32 -0400 | [diff] [blame] | 1889 | |
Jeff Layton | 401a8b8 | 2022-11-16 09:02:30 -0500 | [diff] [blame] | 1890 | ctx = locks_inode_context(inode); |
Jeff Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1891 | if (!ctx) { |
| 1892 | trace_generic_delete_lease(inode, NULL); |
| 1893 | return error; |
| 1894 | } |
| 1895 | |
Peter Zijlstra | 02e525b2 | 2019-02-21 15:38:40 +0100 | [diff] [blame] | 1896 | percpu_down_read(&file_rwsem); |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 1897 | spin_lock(&ctx->flc_lock); |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 1898 | 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 Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1901 | victim = fl; |
Jeff Layton | 0efaa7e | 2014-08-22 10:18:45 -0400 | [diff] [blame] | 1902 | break; |
Jeff Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1903 | } |
J. Bruce Fields | 8335ebd | 2011-09-21 08:34:32 -0400 | [diff] [blame] | 1904 | } |
Jeff Layton | a9b1b45 | 2015-03-14 09:45:35 -0400 | [diff] [blame] | 1905 | trace_generic_delete_lease(inode, victim); |
Jeff Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1906 | if (victim) |
Jeff Layton | 7448cc3 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 1907 | error = fl->fl_lmops->lm_change(victim, F_UNLCK, &dispose); |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 1908 | spin_unlock(&ctx->flc_lock); |
Peter Zijlstra | 02e525b2 | 2019-02-21 15:38:40 +0100 | [diff] [blame] | 1909 | percpu_up_read(&file_rwsem); |
Jeff Layton | c45198e | 2014-09-01 07:12:07 -0400 | [diff] [blame] | 1910 | locks_dispose_list(&dispose); |
Jeff Layton | 0efaa7e | 2014-08-22 10:18:45 -0400 | [diff] [blame] | 1911 | return error; |
J. Bruce Fields | 8335ebd | 2011-09-21 08:34:32 -0400 | [diff] [blame] | 1912 | } |
| 1913 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | /** |
| 1915 | * generic_setlease - sets a lease on an open file |
Jeff Layton | 1c7dd2f | 2014-08-22 10:55:47 -0400 | [diff] [blame] | 1916 | * @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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | * |
| 1922 | * The (input) flp->fl_lmops->lm_break function is required |
| 1923 | * by break_lease(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | */ |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 1925 | int generic_setlease(struct file *filp, int arg, struct file_lease **flp, |
Jeff Layton | e6f5c78 | 2014-08-22 10:40:25 -0400 | [diff] [blame] | 1926 | void **priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | { |
J. Bruce Fields | 8335ebd | 2011-09-21 08:34:32 -0400 | [diff] [blame] | 1928 | switch (arg) { |
| 1929 | case F_UNLCK: |
Christoph Hellwig | 2ab99ee | 2015-01-21 19:14:02 +0100 | [diff] [blame] | 1930 | return generic_delete_lease(filp, *priv); |
J. Bruce Fields | 8335ebd | 2011-09-21 08:34:32 -0400 | [diff] [blame] | 1931 | case F_RDLCK: |
| 1932 | case F_WRLCK: |
Jeff Layton | 0efaa7e | 2014-08-22 10:18:45 -0400 | [diff] [blame] | 1933 | if (!(*flp)->fl_lmops->lm_break) { |
| 1934 | WARN_ON_ONCE(1); |
| 1935 | return -ENOLCK; |
| 1936 | } |
Christoph Hellwig | 11afe9f | 2015-01-21 19:17:03 +0100 | [diff] [blame] | 1937 | |
Jeff Layton | e6f5c78 | 2014-08-22 10:40:25 -0400 | [diff] [blame] | 1938 | return generic_add_lease(filp, arg, flp, priv); |
J. Bruce Fields | 8335ebd | 2011-09-21 08:34:32 -0400 | [diff] [blame] | 1939 | default: |
Dave Jones | 8d657eb | 2012-07-13 13:35:36 -0400 | [diff] [blame] | 1940 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | } |
Christoph Hellwig | 0af1a45 | 2007-07-31 00:39:22 -0700 | [diff] [blame] | 1943 | EXPORT_SYMBOL(generic_setlease); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | |
Jeff Layton | 18f6622 | 2019-08-18 14:18:45 -0400 | [diff] [blame] | 1945 | /* |
| 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 | */ |
| 1951 | static struct srcu_notifier_head lease_notifier_chain; |
| 1952 | |
| 1953 | static inline void |
| 1954 | lease_notifier_chain_init(void) |
| 1955 | { |
| 1956 | srcu_init_notifier_head(&lease_notifier_chain); |
| 1957 | } |
| 1958 | |
| 1959 | static inline void |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 1960 | setlease_notifier(int arg, struct file_lease *lease) |
Jeff Layton | 18f6622 | 2019-08-18 14:18:45 -0400 | [diff] [blame] | 1961 | { |
| 1962 | if (arg != F_UNLCK) |
| 1963 | srcu_notifier_call_chain(&lease_notifier_chain, arg, lease); |
| 1964 | } |
| 1965 | |
| 1966 | int lease_register_notifier(struct notifier_block *nb) |
| 1967 | { |
| 1968 | return srcu_notifier_chain_register(&lease_notifier_chain, nb); |
| 1969 | } |
| 1970 | EXPORT_SYMBOL_GPL(lease_register_notifier); |
| 1971 | |
| 1972 | void lease_unregister_notifier(struct notifier_block *nb) |
| 1973 | { |
| 1974 | srcu_notifier_chain_unregister(&lease_notifier_chain, nb); |
| 1975 | } |
| 1976 | EXPORT_SYMBOL_GPL(lease_unregister_notifier); |
| 1977 | |
Jeff Layton | 7b80010 | 2024-02-05 07:09:31 -0500 | [diff] [blame] | 1978 | |
| 1979 | int |
| 1980 | kernel_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 | } |
| 1989 | EXPORT_SYMBOL_GPL(kernel_setlease); |
| 1990 | |
Arnd Bergmann | b89f432 | 2010-09-18 15:09:31 +0200 | [diff] [blame] | 1991 | /** |
Jeff Layton | e51673a | 2014-08-22 18:13:28 -0400 | [diff] [blame] | 1992 | * vfs_setlease - sets a lease on an open file |
Jeff Layton | 1c7dd2f | 2014-08-22 10:55:47 -0400 | [diff] [blame] | 1993 | * @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 |
NeilBrown | 7bbd1fc | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 1997 | * NULL if lm_setup doesn't require it) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 | * |
Jeff Layton | e51673a | 2014-08-22 18:13:28 -0400 | [diff] [blame] | 1999 | * 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 Chehab | 80b79dd | 2017-05-27 06:07:18 -0400 | [diff] [blame] | 2001 | * 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 Layton | e51673a | 2014-08-22 18:13:28 -0400 | [diff] [blame] | 2003 | * stack trace). |
Jeff Layton | 1c7dd2f | 2014-08-22 10:55:47 -0400 | [diff] [blame] | 2004 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2007 | */ |
Jeff Layton | e6f5c78 | 2014-08-22 10:40:25 -0400 | [diff] [blame] | 2008 | int |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 2009 | vfs_setlease(struct file *filp, int arg, struct file_lease **lease, void **priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | { |
Jeff Layton | 7b80010 | 2024-02-05 07:09:31 -0500 | [diff] [blame] | 2011 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2023 | } |
J. Bruce Fields | a9933ce | 2007-06-07 17:09:49 -0400 | [diff] [blame] | 2024 | EXPORT_SYMBOL_GPL(vfs_setlease); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | |
Luca Vizzarro | ed5f17f | 2023-02-01 15:05:33 +0000 | [diff] [blame] | 2026 | static int do_fcntl_add_lease(unsigned int fd, struct file *filp, int arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | { |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 2028 | struct file_lease *fl; |
Linus Torvalds | f7347ce | 2010-10-27 12:38:12 -0400 | [diff] [blame] | 2029 | struct fasync_struct *new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2030 | int error; |
| 2031 | |
Arnd Bergmann | c5b1f0d | 2010-10-27 15:46:08 +0200 | [diff] [blame] | 2032 | fl = lease_alloc(filp, arg); |
| 2033 | if (IS_ERR(fl)) |
| 2034 | return PTR_ERR(fl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | |
Linus Torvalds | f7347ce | 2010-10-27 12:38:12 -0400 | [diff] [blame] | 2036 | new = fasync_alloc(); |
| 2037 | if (!new) { |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 2038 | locks_free_lease(fl); |
Linus Torvalds | f7347ce | 2010-10-27 12:38:12 -0400 | [diff] [blame] | 2039 | return -ENOMEM; |
| 2040 | } |
Jeff Layton | 1c7dd2f | 2014-08-22 10:55:47 -0400 | [diff] [blame] | 2041 | new->fa_fd = fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2042 | |
Jeff Layton | 1c7dd2f | 2014-08-22 10:55:47 -0400 | [diff] [blame] | 2043 | error = vfs_setlease(filp, arg, &fl, (void **)&new); |
Jeff Layton | 2dfb928 | 2014-08-11 18:14:12 -0400 | [diff] [blame] | 2044 | if (fl) |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 2045 | locks_free_lease(fl); |
Linus Torvalds | f7347ce | 2010-10-27 12:38:12 -0400 | [diff] [blame] | 2046 | if (new) |
| 2047 | fasync_free(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | return error; |
| 2049 | } |
| 2050 | |
| 2051 | /** |
J. Bruce Fields | 0ceaf6c | 2010-10-30 17:31:13 -0400 | [diff] [blame] | 2052 | * 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 Vizzarro | ed5f17f | 2023-02-01 15:05:33 +0000 | [diff] [blame] | 2061 | int fcntl_setlease(unsigned int fd, struct file *filp, int arg) |
J. Bruce Fields | 0ceaf6c | 2010-10-30 17:31:13 -0400 | [diff] [blame] | 2062 | { |
| 2063 | if (arg == F_UNLCK) |
Christoph Hellwig | 2ab99ee | 2015-01-21 19:14:02 +0100 | [diff] [blame] | 2064 | return vfs_setlease(filp, F_UNLCK, NULL, (void **)&filp); |
J. Bruce Fields | 0ceaf6c | 2010-10-30 17:31:13 -0400 | [diff] [blame] | 2065 | return do_fcntl_add_lease(fd, filp, arg); |
| 2066 | } |
| 2067 | |
| 2068 | /** |
Jeff Layton | 29d01b2 | 2015-07-11 06:43:02 -0400 | [diff] [blame] | 2069 | * flock_lock_inode_wait - Apply a FLOCK-style lock to a file |
| 2070 | * @inode: inode of the file to apply to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | * @fl: The lock to be applied |
| 2072 | * |
Jeff Layton | 29d01b2 | 2015-07-11 06:43:02 -0400 | [diff] [blame] | 2073 | * Apply a FLOCK style lock request to an inode. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | */ |
Benjamin Coddington | 616fb38 | 2015-10-22 13:38:15 -0400 | [diff] [blame] | 2075 | static int flock_lock_inode_wait(struct inode *inode, struct file_lock *fl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | { |
| 2077 | int error; |
| 2078 | might_sleep(); |
| 2079 | for (;;) { |
Jeff Layton | 29d01b2 | 2015-07-11 06:43:02 -0400 | [diff] [blame] | 2080 | error = flock_lock_inode(inode, fl); |
Miklos Szeredi | bde74e4 | 2008-07-25 01:48:57 -0700 | [diff] [blame] | 2081 | if (error != FILE_LOCK_DEFERRED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2082 | break; |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 2083 | error = wait_event_interruptible(fl->c.flc_wait, |
| 2084 | list_empty(&fl->c.flc_blocked_member)); |
NeilBrown | 16306a6 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 2085 | if (error) |
| 2086 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2087 | } |
NeilBrown | 16306a6 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 2088 | locks_delete_block(fl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | return error; |
| 2090 | } |
| 2091 | |
Jeff Layton | 29d01b2 | 2015-07-11 06:43:02 -0400 | [diff] [blame] | 2092 | /** |
Benjamin Coddington | e55c34a | 2015-10-22 13:38:13 -0400 | [diff] [blame] | 2093 | * 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 | */ |
| 2099 | int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl) |
| 2100 | { |
| 2101 | int res = 0; |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 2102 | switch (fl->c.flc_flags & (FL_POSIX|FL_FLOCK)) { |
Benjamin Coddington | e55c34a | 2015-10-22 13:38:13 -0400 | [diff] [blame] | 2103 | 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 | } |
| 2114 | EXPORT_SYMBOL(locks_lock_inode_wait); |
| 2115 | |
| 2116 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | * 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 Chehab | 80b79dd | 2017-05-27 06:07:18 -0400 | [diff] [blame] | 2122 | * The @cmd can be one of: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2123 | * |
Mauro Carvalho Chehab | 80b79dd | 2017-05-27 06:07:18 -0400 | [diff] [blame] | 2124 | * - %LOCK_SH -- a shared lock. |
| 2125 | * - %LOCK_EX -- an exclusive lock. |
| 2126 | * - %LOCK_UN -- remove an existing lock. |
Jeff Layton | 90f7d7a | 2021-09-10 15:36:29 -0400 | [diff] [blame] | 2127 | * - %LOCK_MAND -- a 'mandatory' flock. (DEPRECATED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2128 | * |
Jeff Layton | 90f7d7a | 2021-09-10 15:36:29 -0400 | [diff] [blame] | 2129 | * %LOCK_MAND support has been removed from the kernel. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2130 | */ |
Heiko Carstens | 002c897 | 2009-01-14 14:14:18 +0100 | [diff] [blame] | 2131 | SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2132 | { |
Kuniyuki Iwashima | db4abb4 | 2022-07-16 21:35:32 -0700 | [diff] [blame] | 2133 | int can_sleep, error, type; |
Kuniyuki Iwashima | 4149be7 | 2022-07-16 21:35:31 -0700 | [diff] [blame] | 2134 | struct file_lock fl; |
Kuniyuki Iwashima | db4abb4 | 2022-07-16 21:35:32 -0700 | [diff] [blame] | 2135 | struct fd f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | |
Jeff Layton | 90f7d7a | 2021-09-10 15:36:29 -0400 | [diff] [blame] | 2137 | /* |
| 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 Kleen | f2f2494c | 2022-11-18 15:43:57 -0800 | [diff] [blame] | 2146 | 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 Iwashima | db4abb4 | 2022-07-16 21:35:32 -0700 | [diff] [blame] | 2147 | return 0; |
Jeff Layton | 90f7d7a | 2021-09-10 15:36:29 -0400 | [diff] [blame] | 2148 | } |
| 2149 | |
Kuniyuki Iwashima | db4abb4 | 2022-07-16 21:35:32 -0700 | [diff] [blame] | 2150 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2160 | goto out_putf; |
Jeff Layton | 6e129d00 | 2014-09-04 10:25:06 -0400 | [diff] [blame] | 2161 | |
Kuniyuki Iwashima | 4149be7 | 2022-07-16 21:35:31 -0700 | [diff] [blame] | 2162 | flock_make_lock(f.file, &fl, type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 2164 | error = security_file_lock(f.file, fl.c.flc_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | if (error) |
Kuniyuki Iwashima | 4149be7 | 2022-07-16 21:35:31 -0700 | [diff] [blame] | 2166 | goto out_putf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 | |
Kuniyuki Iwashima | db4abb4 | 2022-07-16 21:35:32 -0700 | [diff] [blame] | 2168 | can_sleep = !(cmd & LOCK_NB); |
| 2169 | if (can_sleep) |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 2170 | fl.c.flc_flags |= FL_SLEEP; |
Kuniyuki Iwashima | db4abb4 | 2022-07-16 21:35:32 -0700 | [diff] [blame] | 2171 | |
Miklos Szeredi | de2a4a5 | 2018-07-18 15:44:43 +0200 | [diff] [blame] | 2172 | if (f.file->f_op->flock) |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 2173 | error = f.file->f_op->flock(f.file, |
Kuniyuki Iwashima | db4abb4 | 2022-07-16 21:35:32 -0700 | [diff] [blame] | 2174 | (can_sleep) ? F_SETLKW : F_SETLK, |
Kuniyuki Iwashima | 4149be7 | 2022-07-16 21:35:31 -0700 | [diff] [blame] | 2175 | &fl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | else |
Kuniyuki Iwashima | 4149be7 | 2022-07-16 21:35:31 -0700 | [diff] [blame] | 2177 | error = locks_lock_file_wait(f.file, &fl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2178 | |
David Howells | 932c29a | 2022-08-17 19:41:27 +0100 | [diff] [blame] | 2179 | locks_release_private(&fl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | out_putf: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 2181 | fdput(f); |
Kuniyuki Iwashima | db4abb4 | 2022-07-16 21:35:32 -0700 | [diff] [blame] | 2182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | return error; |
| 2184 | } |
| 2185 | |
J. Bruce Fields | 3ee17ab | 2007-02-21 00:58:50 -0500 | [diff] [blame] | 2186 | /** |
| 2187 | * vfs_test_lock - test file byte range lock |
| 2188 | * @filp: The file to test lock for |
J. Bruce Fields | 6924c55 | 2007-05-11 16:22:50 -0400 | [diff] [blame] | 2189 | * @fl: The lock to test; also used to hold result |
J. Bruce Fields | 3ee17ab | 2007-02-21 00:58:50 -0500 | [diff] [blame] | 2190 | * |
| 2191 | * Returns -ERRNO on failure. Indicates presence of conflicting lock by |
| 2192 | * setting conf->fl_type to something other than F_UNLCK. |
| 2193 | */ |
| 2194 | int vfs_test_lock(struct file *filp, struct file_lock *fl) |
| 2195 | { |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 2196 | WARN_ON_ONCE(filp != fl->c.flc_file); |
Miklos Szeredi | de2a4a5 | 2018-07-18 15:44:43 +0200 | [diff] [blame] | 2197 | if (filp->f_op->lock) |
J. Bruce Fields | 3ee17ab | 2007-02-21 00:58:50 -0500 | [diff] [blame] | 2198 | return filp->f_op->lock(filp, F_GETLK, fl); |
| 2199 | posix_test_lock(filp, fl); |
| 2200 | return 0; |
| 2201 | } |
| 2202 | EXPORT_SYMBOL_GPL(vfs_test_lock); |
| 2203 | |
Benjamin Coddington | 9d5b86a | 2017-07-16 10:28:22 -0400 | [diff] [blame] | 2204 | /** |
| 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 Wilk | bd4c468 | 2023-07-21 11:21:47 +0200 | [diff] [blame] | 2209 | * Used to translate a fl_pid into a namespace virtual pid number |
Benjamin Coddington | 9d5b86a | 2017-07-16 10:28:22 -0400 | [diff] [blame] | 2210 | */ |
Jeff Layton | ae7eb16 | 2024-01-31 18:02:13 -0500 | [diff] [blame] | 2211 | static pid_t locks_translate_pid(struct file_lock_core *fl, struct pid_namespace *ns) |
Benjamin Coddington | 9d5b86a | 2017-07-16 10:28:22 -0400 | [diff] [blame] | 2212 | { |
| 2213 | pid_t vnr; |
| 2214 | struct pid *pid; |
| 2215 | |
Jeff Layton | ae7eb16 | 2024-01-31 18:02:13 -0500 | [diff] [blame] | 2216 | if (fl->flc_flags & FL_OFDLCK) |
Benjamin Coddington | 9d5b86a | 2017-07-16 10:28:22 -0400 | [diff] [blame] | 2217 | return -1; |
Jeff Layton | 3d40f78 | 2024-01-31 18:01:57 -0500 | [diff] [blame] | 2218 | |
| 2219 | /* Remote locks report a negative pid value */ |
Jeff Layton | ae7eb16 | 2024-01-31 18:02:13 -0500 | [diff] [blame] | 2220 | if (fl->flc_pid <= 0) |
| 2221 | return fl->flc_pid; |
Jeff Layton | 3d40f78 | 2024-01-31 18:01:57 -0500 | [diff] [blame] | 2222 | |
Konstantin Khorenko | 826d7bc | 2018-06-08 17:27:11 +0300 | [diff] [blame] | 2223 | /* |
| 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 Layton | ae7eb16 | 2024-01-31 18:02:13 -0500 | [diff] [blame] | 2229 | return (pid_t) fl->flc_pid; |
Benjamin Coddington | 9d5b86a | 2017-07-16 10:28:22 -0400 | [diff] [blame] | 2230 | |
| 2231 | rcu_read_lock(); |
Jeff Layton | ae7eb16 | 2024-01-31 18:02:13 -0500 | [diff] [blame] | 2232 | pid = find_pid_ns(fl->flc_pid, &init_pid_ns); |
Benjamin Coddington | 9d5b86a | 2017-07-16 10:28:22 -0400 | [diff] [blame] | 2233 | vnr = pid_nr_ns(pid, ns); |
| 2234 | rcu_read_unlock(); |
| 2235 | return vnr; |
| 2236 | } |
| 2237 | |
J. Bruce Fields | c2fa1b8 | 2007-02-20 16:10:11 -0500 | [diff] [blame] | 2238 | static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl) |
| 2239 | { |
Jeff Layton | ae7eb16 | 2024-01-31 18:02:13 -0500 | [diff] [blame] | 2240 | flock->l_pid = locks_translate_pid(&fl->c, task_active_pid_ns(current)); |
J. Bruce Fields | c2fa1b8 | 2007-02-20 16:10:11 -0500 | [diff] [blame] | 2241 | #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 Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 2255 | flock->l_type = fl->c.flc_type; |
J. Bruce Fields | c2fa1b8 | 2007-02-20 16:10:11 -0500 | [diff] [blame] | 2256 | return 0; |
| 2257 | } |
| 2258 | |
| 2259 | #if BITS_PER_LONG == 32 |
| 2260 | static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl) |
| 2261 | { |
Jeff Layton | ae7eb16 | 2024-01-31 18:02:13 -0500 | [diff] [blame] | 2262 | flock->l_pid = locks_translate_pid(&fl->c, task_active_pid_ns(current)); |
J. Bruce Fields | c2fa1b8 | 2007-02-20 16:10:11 -0500 | [diff] [blame] | 2263 | 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 Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 2267 | flock->l_type = fl->c.flc_type; |
J. Bruce Fields | c2fa1b8 | 2007-02-20 16:10:11 -0500 | [diff] [blame] | 2268 | } |
| 2269 | #endif |
| 2270 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2271 | /* Report the first existing lock that would conflict with l. |
| 2272 | * This implements the F_GETLK command of fcntl(). |
| 2273 | */ |
Christoph Hellwig | a75d30c | 2017-05-27 06:07:19 -0400 | [diff] [blame] | 2274 | int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock *flock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | { |
Benjamin Coddington | 52306e8 | 2017-07-16 10:28:21 -0400 | [diff] [blame] | 2276 | struct file_lock *fl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2277 | int error; |
| 2278 | |
Benjamin Coddington | 52306e8 | 2017-07-16 10:28:21 -0400 | [diff] [blame] | 2279 | fl = locks_alloc_lock(); |
| 2280 | if (fl == NULL) |
| 2281 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 | error = -EINVAL; |
Stas Sergeev | 6c9007f | 2023-06-22 21:52:23 +0500 | [diff] [blame] | 2283 | if (cmd != F_OFD_GETLK && flock->l_type != F_RDLCK |
| 2284 | && flock->l_type != F_WRLCK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2285 | goto out; |
| 2286 | |
Benjamin Coddington | 52306e8 | 2017-07-16 10:28:21 -0400 | [diff] [blame] | 2287 | error = flock_to_posix_lock(filp, fl, flock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2288 | if (error) |
| 2289 | goto out; |
| 2290 | |
Jeff Layton | 0d3f7a2 | 2014-04-22 08:23:58 -0400 | [diff] [blame] | 2291 | if (cmd == F_OFD_GETLK) { |
Jeff Layton | 9047893 | 2014-03-04 10:30:23 -0500 | [diff] [blame] | 2292 | error = -EINVAL; |
Christoph Hellwig | a75d30c | 2017-05-27 06:07:19 -0400 | [diff] [blame] | 2293 | if (flock->l_pid != 0) |
Jeff Layton | 9047893 | 2014-03-04 10:30:23 -0500 | [diff] [blame] | 2294 | goto out; |
| 2295 | |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 2296 | fl->c.flc_flags |= FL_OFDLCK; |
| 2297 | fl->c.flc_owner = filp; |
Jeff Layton | 5d50ffd | 2014-02-03 12:13:10 -0500 | [diff] [blame] | 2298 | } |
| 2299 | |
Benjamin Coddington | 52306e8 | 2017-07-16 10:28:21 -0400 | [diff] [blame] | 2300 | error = vfs_test_lock(filp, fl); |
J. Bruce Fields | 3ee17ab | 2007-02-21 00:58:50 -0500 | [diff] [blame] | 2301 | if (error) |
| 2302 | goto out; |
NeilBrown | 7bbd1fc | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 2303 | |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 2304 | flock->l_type = fl->c.flc_type; |
| 2305 | if (fl->c.flc_type != F_UNLCK) { |
Benjamin Coddington | 52306e8 | 2017-07-16 10:28:21 -0400 | [diff] [blame] | 2306 | error = posix_lock_to_flock(flock, fl); |
J. Bruce Fields | c2fa1b8 | 2007-02-20 16:10:11 -0500 | [diff] [blame] | 2307 | if (error) |
Benjamin Coddington | 52306e8 | 2017-07-16 10:28:21 -0400 | [diff] [blame] | 2308 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2309 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | out: |
Benjamin Coddington | 52306e8 | 2017-07-16 10:28:21 -0400 | [diff] [blame] | 2311 | locks_free_lock(fl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | return error; |
| 2313 | } |
| 2314 | |
Marc Eshel | 7723ec9 | 2007-01-18 15:08:55 -0500 | [diff] [blame] | 2315 | /** |
| 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 Eshel | 150b393 | 2007-01-18 16:15:35 -0500 | [diff] [blame] | 2320 | * @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 Eshel | 2beb661 | 2006-12-05 23:31:28 -0500 | [diff] [blame] | 2328 | * |
| 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 Aring | e70da17 | 2023-09-12 17:53:21 -0400 | [diff] [blame] | 2332 | * 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 Eshel | 2beb661 | 2006-12-05 23:31:28 -0500 | [diff] [blame] | 2339 | * If the request is for non-blocking lock the file system should return |
Miklos Szeredi | bde74e4 | 2008-07-25 01:48:57 -0700 | [diff] [blame] | 2340 | * 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 Eshel | 2beb661 | 2006-12-05 23:31:28 -0500 | [diff] [blame] | 2342 | * 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 Fields | 8fb47a4 | 2011-07-20 20:21:59 -0400 | [diff] [blame] | 2347 | * ->lm_grant() before returning to the caller with a FILE_LOCK_DEFERRED |
Marc Eshel | 2beb661 | 2006-12-05 23:31:28 -0500 | [diff] [blame] | 2348 | * return code. |
Marc Eshel | 7723ec9 | 2007-01-18 15:08:55 -0500 | [diff] [blame] | 2349 | */ |
Marc Eshel | 150b393 | 2007-01-18 16:15:35 -0500 | [diff] [blame] | 2350 | int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf) |
Marc Eshel | 7723ec9 | 2007-01-18 15:08:55 -0500 | [diff] [blame] | 2351 | { |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 2352 | WARN_ON_ONCE(filp != fl->c.flc_file); |
Miklos Szeredi | de2a4a5 | 2018-07-18 15:44:43 +0200 | [diff] [blame] | 2353 | if (filp->f_op->lock) |
Marc Eshel | 7723ec9 | 2007-01-18 15:08:55 -0500 | [diff] [blame] | 2354 | return filp->f_op->lock(filp, cmd, fl); |
| 2355 | else |
Marc Eshel | 150b393 | 2007-01-18 16:15:35 -0500 | [diff] [blame] | 2356 | return posix_lock_file(filp, fl, conf); |
Marc Eshel | 7723ec9 | 2007-01-18 15:08:55 -0500 | [diff] [blame] | 2357 | } |
| 2358 | EXPORT_SYMBOL_GPL(vfs_lock_file); |
| 2359 | |
Miklos Szeredi | b648a6d | 2008-07-25 01:48:57 -0700 | [diff] [blame] | 2360 | static int do_lock_file_wait(struct file *filp, unsigned int cmd, |
| 2361 | struct file_lock *fl) |
| 2362 | { |
| 2363 | int error; |
| 2364 | |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 2365 | error = security_file_lock(filp, fl->c.flc_type); |
Miklos Szeredi | b648a6d | 2008-07-25 01:48:57 -0700 | [diff] [blame] | 2366 | if (error) |
| 2367 | return error; |
| 2368 | |
Miklos Szeredi | 764c76b | 2008-07-25 01:48:58 -0700 | [diff] [blame] | 2369 | for (;;) { |
| 2370 | error = vfs_lock_file(filp, cmd, fl, NULL); |
| 2371 | if (error != FILE_LOCK_DEFERRED) |
Miklos Szeredi | b648a6d | 2008-07-25 01:48:57 -0700 | [diff] [blame] | 2372 | break; |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 2373 | error = wait_event_interruptible(fl->c.flc_wait, |
| 2374 | list_empty(&fl->c.flc_blocked_member)); |
NeilBrown | 16306a6 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 2375 | if (error) |
| 2376 | break; |
Miklos Szeredi | b648a6d | 2008-07-25 01:48:57 -0700 | [diff] [blame] | 2377 | } |
NeilBrown | 16306a6 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 2378 | locks_delete_block(fl); |
Miklos Szeredi | b648a6d | 2008-07-25 01:48:57 -0700 | [diff] [blame] | 2379 | |
| 2380 | return error; |
| 2381 | } |
| 2382 | |
Benjamin Coddington | 6ca7d91 | 2015-10-15 09:07:07 -0400 | [diff] [blame] | 2383 | /* Ensure that fl->fl_file has compatible f_mode for F_SETLK calls */ |
Jeff Layton | cf01f4e | 2014-05-09 11:41:54 -0400 | [diff] [blame] | 2384 | static int |
| 2385 | check_fmode_for_setlk(struct file_lock *fl) |
| 2386 | { |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 2387 | switch (fl->c.flc_type) { |
Jeff Layton | cf01f4e | 2014-05-09 11:41:54 -0400 | [diff] [blame] | 2388 | case F_RDLCK: |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 2389 | if (!(fl->c.flc_file->f_mode & FMODE_READ)) |
Jeff Layton | cf01f4e | 2014-05-09 11:41:54 -0400 | [diff] [blame] | 2390 | return -EBADF; |
| 2391 | break; |
| 2392 | case F_WRLCK: |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 2393 | if (!(fl->c.flc_file->f_mode & FMODE_WRITE)) |
Jeff Layton | cf01f4e | 2014-05-09 11:41:54 -0400 | [diff] [blame] | 2394 | return -EBADF; |
| 2395 | } |
| 2396 | return 0; |
| 2397 | } |
| 2398 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2399 | /* 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 Staubach | c293621 | 2005-07-27 11:45:09 -0700 | [diff] [blame] | 2402 | int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd, |
Christoph Hellwig | a75d30c | 2017-05-27 06:07:19 -0400 | [diff] [blame] | 2403 | struct flock *flock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | { |
| 2405 | struct file_lock *file_lock = locks_alloc_lock(); |
Jeff Layton | c65454a | 2022-11-25 08:48:37 -0500 | [diff] [blame] | 2406 | struct inode *inode = file_inode(filp); |
Al Viro | 0b2bac2 | 2008-05-06 13:58:34 -0400 | [diff] [blame] | 2407 | struct file *f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2408 | int error; |
| 2409 | |
| 2410 | if (file_lock == NULL) |
| 2411 | return -ENOLCK; |
| 2412 | |
Christoph Hellwig | a75d30c | 2017-05-27 06:07:19 -0400 | [diff] [blame] | 2413 | error = flock_to_posix_lock(filp, file_lock, flock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2414 | if (error) |
| 2415 | goto out; |
Jeff Layton | 5d50ffd | 2014-02-03 12:13:10 -0500 | [diff] [blame] | 2416 | |
Jeff Layton | cf01f4e | 2014-05-09 11:41:54 -0400 | [diff] [blame] | 2417 | error = check_fmode_for_setlk(file_lock); |
| 2418 | if (error) |
| 2419 | goto out; |
| 2420 | |
Jeff Layton | 5d50ffd | 2014-02-03 12:13:10 -0500 | [diff] [blame] | 2421 | /* |
| 2422 | * If the cmd is requesting file-private locks, then set the |
Jeff Layton | cff2fce | 2014-04-22 08:24:32 -0400 | [diff] [blame] | 2423 | * FL_OFDLCK flag and override the owner. |
Jeff Layton | 5d50ffd | 2014-02-03 12:13:10 -0500 | [diff] [blame] | 2424 | */ |
| 2425 | switch (cmd) { |
Jeff Layton | 0d3f7a2 | 2014-04-22 08:23:58 -0400 | [diff] [blame] | 2426 | case F_OFD_SETLK: |
Jeff Layton | 9047893 | 2014-03-04 10:30:23 -0500 | [diff] [blame] | 2427 | error = -EINVAL; |
Christoph Hellwig | a75d30c | 2017-05-27 06:07:19 -0400 | [diff] [blame] | 2428 | if (flock->l_pid != 0) |
Jeff Layton | 9047893 | 2014-03-04 10:30:23 -0500 | [diff] [blame] | 2429 | goto out; |
| 2430 | |
Jeff Layton | 5d50ffd | 2014-02-03 12:13:10 -0500 | [diff] [blame] | 2431 | cmd = F_SETLK; |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 2432 | file_lock->c.flc_flags |= FL_OFDLCK; |
| 2433 | file_lock->c.flc_owner = filp; |
Jeff Layton | 5d50ffd | 2014-02-03 12:13:10 -0500 | [diff] [blame] | 2434 | break; |
Jeff Layton | 0d3f7a2 | 2014-04-22 08:23:58 -0400 | [diff] [blame] | 2435 | case F_OFD_SETLKW: |
Jeff Layton | 9047893 | 2014-03-04 10:30:23 -0500 | [diff] [blame] | 2436 | error = -EINVAL; |
Christoph Hellwig | a75d30c | 2017-05-27 06:07:19 -0400 | [diff] [blame] | 2437 | if (flock->l_pid != 0) |
Jeff Layton | 9047893 | 2014-03-04 10:30:23 -0500 | [diff] [blame] | 2438 | goto out; |
| 2439 | |
Jeff Layton | 5d50ffd | 2014-02-03 12:13:10 -0500 | [diff] [blame] | 2440 | cmd = F_SETLKW; |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 2441 | file_lock->c.flc_flags |= FL_OFDLCK; |
| 2442 | file_lock->c.flc_owner = filp; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 2443 | fallthrough; |
Jeff Layton | 5d50ffd | 2014-02-03 12:13:10 -0500 | [diff] [blame] | 2444 | case F_SETLKW: |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 2445 | file_lock->c.flc_flags |= FL_SLEEP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2446 | } |
Jeff Layton | 5d50ffd | 2014-02-03 12:13:10 -0500 | [diff] [blame] | 2447 | |
Miklos Szeredi | b648a6d | 2008-07-25 01:48:57 -0700 | [diff] [blame] | 2448 | error = do_lock_file_wait(filp, cmd, file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2449 | |
Peter Staubach | c293621 | 2005-07-27 11:45:09 -0700 | [diff] [blame] | 2450 | /* |
Jann Horn | 3cad1bc | 2024-07-02 18:26:52 +0200 | [diff] [blame] | 2451 | * 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 Layton | 0752ba80 | 2016-01-08 07:30:43 -0500 | [diff] [blame] | 2454 | * unlocking though, or for OFD locks. |
Peter Staubach | c293621 | 2005-07-27 11:45:09 -0700 | [diff] [blame] | 2455 | */ |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 2456 | if (!error && file_lock->c.flc_type != F_UNLCK && |
| 2457 | !(file_lock->c.flc_flags & FL_OFDLCK)) { |
Eric W. Biederman | 120ce2b | 2020-11-20 17:14:25 -0600 | [diff] [blame] | 2458 | struct files_struct *files = current->files; |
Jeff Layton | 7f3697e | 2016-01-07 16:38:10 -0500 | [diff] [blame] | 2459 | /* |
| 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. Biederman | 120ce2b | 2020-11-20 17:14:25 -0600 | [diff] [blame] | 2464 | spin_lock(&files->file_lock); |
| 2465 | f = files_lookup_fd_locked(files, fd); |
| 2466 | spin_unlock(&files->file_lock); |
Jeff Layton | 7f3697e | 2016-01-07 16:38:10 -0500 | [diff] [blame] | 2467 | if (f != filp) { |
Jann Horn | 3cad1bc | 2024-07-02 18:26:52 +0200 | [diff] [blame] | 2468 | locks_remove_posix(filp, files); |
Jeff Layton | 7f3697e | 2016-01-07 16:38:10 -0500 | [diff] [blame] | 2469 | error = -EBADF; |
| 2470 | } |
Peter Staubach | c293621 | 2005-07-27 11:45:09 -0700 | [diff] [blame] | 2471 | } |
Peter Staubach | c293621 | 2005-07-27 11:45:09 -0700 | [diff] [blame] | 2472 | out: |
Jeff Layton | 1890910 | 2016-01-06 21:26:10 -0500 | [diff] [blame] | 2473 | trace_fcntl_setlk(inode, file_lock, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2474 | 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 Hellwig | a75d30c | 2017-05-27 06:07:19 -0400 | [diff] [blame] | 2482 | int fcntl_getlk64(struct file *filp, unsigned int cmd, struct flock64 *flock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2483 | { |
Benjamin Coddington | 52306e8 | 2017-07-16 10:28:21 -0400 | [diff] [blame] | 2484 | struct file_lock *fl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2485 | int error; |
| 2486 | |
Benjamin Coddington | 52306e8 | 2017-07-16 10:28:21 -0400 | [diff] [blame] | 2487 | fl = locks_alloc_lock(); |
| 2488 | if (fl == NULL) |
| 2489 | return -ENOMEM; |
| 2490 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2491 | error = -EINVAL; |
Stas Sergeev | 6c9007f | 2023-06-22 21:52:23 +0500 | [diff] [blame] | 2492 | if (cmd != F_OFD_GETLK && flock->l_type != F_RDLCK |
| 2493 | && flock->l_type != F_WRLCK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2494 | goto out; |
| 2495 | |
Benjamin Coddington | 52306e8 | 2017-07-16 10:28:21 -0400 | [diff] [blame] | 2496 | error = flock64_to_posix_lock(filp, fl, flock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2497 | if (error) |
| 2498 | goto out; |
| 2499 | |
Jeff Layton | 0d3f7a2 | 2014-04-22 08:23:58 -0400 | [diff] [blame] | 2500 | if (cmd == F_OFD_GETLK) { |
Jeff Layton | 9047893 | 2014-03-04 10:30:23 -0500 | [diff] [blame] | 2501 | error = -EINVAL; |
Christoph Hellwig | a75d30c | 2017-05-27 06:07:19 -0400 | [diff] [blame] | 2502 | if (flock->l_pid != 0) |
Jeff Layton | 9047893 | 2014-03-04 10:30:23 -0500 | [diff] [blame] | 2503 | goto out; |
| 2504 | |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 2505 | fl->c.flc_flags |= FL_OFDLCK; |
| 2506 | fl->c.flc_owner = filp; |
Jeff Layton | 5d50ffd | 2014-02-03 12:13:10 -0500 | [diff] [blame] | 2507 | } |
| 2508 | |
Benjamin Coddington | 52306e8 | 2017-07-16 10:28:21 -0400 | [diff] [blame] | 2509 | error = vfs_test_lock(filp, fl); |
J. Bruce Fields | 3ee17ab | 2007-02-21 00:58:50 -0500 | [diff] [blame] | 2510 | if (error) |
| 2511 | goto out; |
| 2512 | |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 2513 | flock->l_type = fl->c.flc_type; |
| 2514 | if (fl->c.flc_type != F_UNLCK) |
Benjamin Coddington | 52306e8 | 2017-07-16 10:28:21 -0400 | [diff] [blame] | 2515 | posix_lock_to_flock64(flock, fl); |
Kinglong Mee | f328296 | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 2516 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2517 | out: |
Benjamin Coddington | 52306e8 | 2017-07-16 10:28:21 -0400 | [diff] [blame] | 2518 | locks_free_lock(fl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2519 | 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 Staubach | c293621 | 2005-07-27 11:45:09 -0700 | [diff] [blame] | 2525 | int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd, |
Christoph Hellwig | a75d30c | 2017-05-27 06:07:19 -0400 | [diff] [blame] | 2526 | struct flock64 *flock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2527 | { |
| 2528 | struct file_lock *file_lock = locks_alloc_lock(); |
Al Viro | 0b2bac2 | 2008-05-06 13:58:34 -0400 | [diff] [blame] | 2529 | struct file *f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2530 | int error; |
| 2531 | |
| 2532 | if (file_lock == NULL) |
| 2533 | return -ENOLCK; |
| 2534 | |
Christoph Hellwig | a75d30c | 2017-05-27 06:07:19 -0400 | [diff] [blame] | 2535 | error = flock64_to_posix_lock(filp, file_lock, flock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2536 | if (error) |
| 2537 | goto out; |
Jeff Layton | 5d50ffd | 2014-02-03 12:13:10 -0500 | [diff] [blame] | 2538 | |
Jeff Layton | cf01f4e | 2014-05-09 11:41:54 -0400 | [diff] [blame] | 2539 | error = check_fmode_for_setlk(file_lock); |
| 2540 | if (error) |
| 2541 | goto out; |
| 2542 | |
Jeff Layton | 5d50ffd | 2014-02-03 12:13:10 -0500 | [diff] [blame] | 2543 | /* |
| 2544 | * If the cmd is requesting file-private locks, then set the |
Jeff Layton | cff2fce | 2014-04-22 08:24:32 -0400 | [diff] [blame] | 2545 | * FL_OFDLCK flag and override the owner. |
Jeff Layton | 5d50ffd | 2014-02-03 12:13:10 -0500 | [diff] [blame] | 2546 | */ |
| 2547 | switch (cmd) { |
Jeff Layton | 0d3f7a2 | 2014-04-22 08:23:58 -0400 | [diff] [blame] | 2548 | case F_OFD_SETLK: |
Jeff Layton | 9047893 | 2014-03-04 10:30:23 -0500 | [diff] [blame] | 2549 | error = -EINVAL; |
Christoph Hellwig | a75d30c | 2017-05-27 06:07:19 -0400 | [diff] [blame] | 2550 | if (flock->l_pid != 0) |
Jeff Layton | 9047893 | 2014-03-04 10:30:23 -0500 | [diff] [blame] | 2551 | goto out; |
| 2552 | |
Jeff Layton | 5d50ffd | 2014-02-03 12:13:10 -0500 | [diff] [blame] | 2553 | cmd = F_SETLK64; |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 2554 | file_lock->c.flc_flags |= FL_OFDLCK; |
| 2555 | file_lock->c.flc_owner = filp; |
Jeff Layton | 5d50ffd | 2014-02-03 12:13:10 -0500 | [diff] [blame] | 2556 | break; |
Jeff Layton | 0d3f7a2 | 2014-04-22 08:23:58 -0400 | [diff] [blame] | 2557 | case F_OFD_SETLKW: |
Jeff Layton | 9047893 | 2014-03-04 10:30:23 -0500 | [diff] [blame] | 2558 | error = -EINVAL; |
Christoph Hellwig | a75d30c | 2017-05-27 06:07:19 -0400 | [diff] [blame] | 2559 | if (flock->l_pid != 0) |
Jeff Layton | 9047893 | 2014-03-04 10:30:23 -0500 | [diff] [blame] | 2560 | goto out; |
| 2561 | |
Jeff Layton | 5d50ffd | 2014-02-03 12:13:10 -0500 | [diff] [blame] | 2562 | cmd = F_SETLKW64; |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 2563 | file_lock->c.flc_flags |= FL_OFDLCK; |
| 2564 | file_lock->c.flc_owner = filp; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 2565 | fallthrough; |
Jeff Layton | 5d50ffd | 2014-02-03 12:13:10 -0500 | [diff] [blame] | 2566 | case F_SETLKW64: |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 2567 | file_lock->c.flc_flags |= FL_SLEEP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2568 | } |
Jeff Layton | 5d50ffd | 2014-02-03 12:13:10 -0500 | [diff] [blame] | 2569 | |
Miklos Szeredi | b648a6d | 2008-07-25 01:48:57 -0700 | [diff] [blame] | 2570 | error = do_lock_file_wait(filp, cmd, file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2571 | |
Peter Staubach | c293621 | 2005-07-27 11:45:09 -0700 | [diff] [blame] | 2572 | /* |
Jann Horn | f8138f2 | 2024-07-23 17:03:56 +0200 | [diff] [blame] | 2573 | * 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 Layton | 0752ba80 | 2016-01-08 07:30:43 -0500 | [diff] [blame] | 2576 | * unlocking though, or for OFD locks. |
Peter Staubach | c293621 | 2005-07-27 11:45:09 -0700 | [diff] [blame] | 2577 | */ |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 2578 | if (!error && file_lock->c.flc_type != F_UNLCK && |
| 2579 | !(file_lock->c.flc_flags & FL_OFDLCK)) { |
Eric W. Biederman | 120ce2b | 2020-11-20 17:14:25 -0600 | [diff] [blame] | 2580 | struct files_struct *files = current->files; |
Jeff Layton | 7f3697e | 2016-01-07 16:38:10 -0500 | [diff] [blame] | 2581 | /* |
| 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. Biederman | 120ce2b | 2020-11-20 17:14:25 -0600 | [diff] [blame] | 2586 | spin_lock(&files->file_lock); |
| 2587 | f = files_lookup_fd_locked(files, fd); |
| 2588 | spin_unlock(&files->file_lock); |
Jeff Layton | 7f3697e | 2016-01-07 16:38:10 -0500 | [diff] [blame] | 2589 | if (f != filp) { |
Jann Horn | f8138f2 | 2024-07-23 17:03:56 +0200 | [diff] [blame] | 2590 | locks_remove_posix(filp, files); |
Jeff Layton | 7f3697e | 2016-01-07 16:38:10 -0500 | [diff] [blame] | 2591 | error = -EBADF; |
| 2592 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2593 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2594 | out: |
| 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 | */ |
| 2605 | void locks_remove_posix(struct file *filp, fl_owner_t owner) |
| 2606 | { |
Jeff Layton | 1890910 | 2016-01-06 21:26:10 -0500 | [diff] [blame] | 2607 | int error; |
Jeff Layton | c65454a | 2022-11-25 08:48:37 -0500 | [diff] [blame] | 2608 | struct inode *inode = file_inode(filp); |
Miklos Szeredi | ff7b86b | 2006-06-23 02:05:11 -0700 | [diff] [blame] | 2609 | struct file_lock lock; |
Dmitry Vyukov | 128a378 | 2015-09-21 09:43:06 +0200 | [diff] [blame] | 2610 | struct file_lock_context *ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2611 | |
| 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 Layton | 401a8b8 | 2022-11-16 09:02:30 -0500 | [diff] [blame] | 2617 | ctx = locks_inode_context(inode); |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 2618 | if (!ctx || list_empty(&ctx->flc_posix)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2619 | return; |
| 2620 | |
NeilBrown | d6367d6 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 2621 | locks_init_lock(&lock); |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 2622 | lock.c.flc_type = F_UNLCK; |
| 2623 | lock.c.flc_flags = FL_POSIX | FL_CLOSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2624 | lock.fl_start = 0; |
| 2625 | lock.fl_end = OFFSET_MAX; |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 2626 | lock.c.flc_owner = owner; |
| 2627 | lock.c.flc_pid = current->tgid; |
| 2628 | lock.c.flc_file = filp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2629 | lock.fl_ops = NULL; |
| 2630 | lock.fl_lmops = NULL; |
| 2631 | |
Jeff Layton | 1890910 | 2016-01-06 21:26:10 -0500 | [diff] [blame] | 2632 | error = vfs_lock_file(filp, F_SETLK, &lock, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2633 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2634 | if (lock.fl_ops && lock.fl_ops->fl_release_private) |
| 2635 | lock.fl_ops->fl_release_private(&lock); |
Miklos Szeredi | c568d68 | 2016-09-16 12:44:20 +0200 | [diff] [blame] | 2636 | trace_locks_remove_posix(inode, &lock, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2637 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2638 | EXPORT_SYMBOL(locks_remove_posix); |
| 2639 | |
Jeff Layton | 3d8e560 | 2015-01-16 15:05:58 -0500 | [diff] [blame] | 2640 | /* The i_flctx must be valid when calling into here */ |
Jeff Layton | dd459bb | 2015-01-16 15:05:54 -0500 | [diff] [blame] | 2641 | static void |
Dmitry Vyukov | 128a378 | 2015-09-21 09:43:06 +0200 | [diff] [blame] | 2642 | locks_remove_flock(struct file *filp, struct file_lock_context *flctx) |
Jeff Layton | dd459bb | 2015-01-16 15:05:54 -0500 | [diff] [blame] | 2643 | { |
NeilBrown | d6367d6 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 2644 | struct file_lock fl; |
Jeff Layton | c65454a | 2022-11-25 08:48:37 -0500 | [diff] [blame] | 2645 | struct inode *inode = file_inode(filp); |
Jeff Layton | dd459bb | 2015-01-16 15:05:54 -0500 | [diff] [blame] | 2646 | |
Jeff Layton | 3d8e560 | 2015-01-16 15:05:58 -0500 | [diff] [blame] | 2647 | if (list_empty(&flctx->flc_flock)) |
Jeff Layton | dd459bb | 2015-01-16 15:05:54 -0500 | [diff] [blame] | 2648 | return; |
| 2649 | |
Kuniyuki Iwashima | 4149be7 | 2022-07-16 21:35:31 -0700 | [diff] [blame] | 2650 | flock_make_lock(filp, &fl, F_UNLCK); |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 2651 | fl.c.flc_flags |= FL_CLOSE; |
NeilBrown | d6367d6 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 2652 | |
Miklos Szeredi | de2a4a5 | 2018-07-18 15:44:43 +0200 | [diff] [blame] | 2653 | if (filp->f_op->flock) |
Jeff Layton | dd459bb | 2015-01-16 15:05:54 -0500 | [diff] [blame] | 2654 | filp->f_op->flock(filp, F_SETLKW, &fl); |
| 2655 | else |
Jeff Layton | bcd7f78d | 2015-07-11 06:43:02 -0400 | [diff] [blame] | 2656 | flock_lock_inode(inode, &fl); |
Jeff Layton | dd459bb | 2015-01-16 15:05:54 -0500 | [diff] [blame] | 2657 | |
| 2658 | if (fl.fl_ops && fl.fl_ops->fl_release_private) |
| 2659 | fl.fl_ops->fl_release_private(&fl); |
| 2660 | } |
| 2661 | |
Jeff Layton | 3d8e560 | 2015-01-16 15:05:58 -0500 | [diff] [blame] | 2662 | /* The i_flctx must be valid when calling into here */ |
Jeff Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 2663 | static void |
Dmitry Vyukov | 128a378 | 2015-09-21 09:43:06 +0200 | [diff] [blame] | 2664 | locks_remove_lease(struct file *filp, struct file_lock_context *ctx) |
Jeff Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 2665 | { |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 2666 | struct file_lease *fl, *tmp; |
Jeff Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 2667 | LIST_HEAD(dispose); |
| 2668 | |
Jeff Layton | 3d8e560 | 2015-01-16 15:05:58 -0500 | [diff] [blame] | 2669 | if (list_empty(&ctx->flc_lease)) |
Jeff Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 2670 | return; |
| 2671 | |
Peter Zijlstra | 02e525b2 | 2019-02-21 15:38:40 +0100 | [diff] [blame] | 2672 | percpu_down_read(&file_rwsem); |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 2673 | spin_lock(&ctx->flc_lock); |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 2674 | list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, c.flc_list) |
| 2675 | if (filp == fl->c.flc_file) |
Jeff Layton | c4e136c | 2015-02-16 19:37:42 -0500 | [diff] [blame] | 2676 | lease_modify(fl, F_UNLCK, &dispose); |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 2677 | spin_unlock(&ctx->flc_lock); |
Peter Zijlstra | 02e525b2 | 2019-02-21 15:38:40 +0100 | [diff] [blame] | 2678 | percpu_up_read(&file_rwsem); |
Peter Zijlstra | 5f43086 | 2016-10-08 10:12:28 +0200 | [diff] [blame] | 2679 | |
Jeff Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 2680 | locks_dispose_list(&dispose); |
| 2681 | } |
| 2682 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2683 | /* |
| 2684 | * This function is called on the last close of an open file. |
| 2685 | */ |
Jeff Layton | 78ed8a1 | 2014-02-03 12:13:08 -0500 | [diff] [blame] | 2686 | void locks_remove_file(struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2687 | { |
Dmitry Vyukov | 128a378 | 2015-09-21 09:43:06 +0200 | [diff] [blame] | 2688 | struct file_lock_context *ctx; |
| 2689 | |
Jeff Layton | c65454a | 2022-11-25 08:48:37 -0500 | [diff] [blame] | 2690 | ctx = locks_inode_context(file_inode(filp)); |
Dmitry Vyukov | 128a378 | 2015-09-21 09:43:06 +0200 | [diff] [blame] | 2691 | if (!ctx) |
Jeff Layton | 3d8e560 | 2015-01-16 15:05:58 -0500 | [diff] [blame] | 2692 | return; |
| 2693 | |
Jeff Layton | dd459bb | 2015-01-16 15:05:54 -0500 | [diff] [blame] | 2694 | /* remove any OFD locks */ |
Christoph Hellwig | 73a8f5f | 2014-07-13 17:00:38 +0200 | [diff] [blame] | 2695 | locks_remove_posix(filp, filp); |
Jeff Layton | 5d50ffd | 2014-02-03 12:13:10 -0500 | [diff] [blame] | 2696 | |
Jeff Layton | dd459bb | 2015-01-16 15:05:54 -0500 | [diff] [blame] | 2697 | /* remove flock locks */ |
Dmitry Vyukov | 128a378 | 2015-09-21 09:43:06 +0200 | [diff] [blame] | 2698 | locks_remove_flock(filp, ctx); |
Jeff Layton | dd459bb | 2015-01-16 15:05:54 -0500 | [diff] [blame] | 2699 | |
Jeff Layton | 8634b51 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 2700 | /* remove any leases */ |
Dmitry Vyukov | 128a378 | 2015-09-21 09:43:06 +0200 | [diff] [blame] | 2701 | locks_remove_lease(filp, ctx); |
Benjamin Coddington | 3953704 | 2017-07-21 13:36:25 -0400 | [diff] [blame] | 2702 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2708 | } |
| 2709 | |
| 2710 | /** |
Marc Eshel | 9b9d2ab | 2007-01-18 17:52:58 -0500 | [diff] [blame] | 2711 | * 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 | */ |
| 2717 | int vfs_cancel_lock(struct file *filp, struct file_lock *fl) |
| 2718 | { |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 2719 | WARN_ON_ONCE(filp != fl->c.flc_file); |
Miklos Szeredi | de2a4a5 | 2018-07-18 15:44:43 +0200 | [diff] [blame] | 2720 | if (filp->f_op->lock) |
Marc Eshel | 9b9d2ab | 2007-01-18 17:52:58 -0500 | [diff] [blame] | 2721 | return filp->f_op->lock(filp, F_CANCELLK, fl); |
| 2722 | return 0; |
| 2723 | } |
Marc Eshel | 9b9d2ab | 2007-01-18 17:52:58 -0500 | [diff] [blame] | 2724 | EXPORT_SYMBOL_GPL(vfs_cancel_lock); |
| 2725 | |
Jeff Layton | ab1ddef | 2022-11-14 08:33:09 -0500 | [diff] [blame] | 2726 | /** |
| 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 | */ |
| 2733 | bool vfs_inode_has_locks(struct inode *inode) |
| 2734 | { |
| 2735 | struct file_lock_context *ctx; |
| 2736 | bool ret; |
| 2737 | |
Jeff Layton | 401a8b8 | 2022-11-16 09:02:30 -0500 | [diff] [blame] | 2738 | ctx = locks_inode_context(inode); |
Jeff Layton | ab1ddef | 2022-11-14 08:33:09 -0500 | [diff] [blame] | 2739 | 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 | } |
| 2747 | EXPORT_SYMBOL_GPL(vfs_inode_has_locks); |
| 2748 | |
Pavel Emelyanov | 7f8ada9 | 2007-10-01 14:41:15 -0700 | [diff] [blame] | 2749 | #ifdef CONFIG_PROC_FS |
Alexey Dobriyan | d8ba7a3 | 2008-10-04 22:34:18 +0400 | [diff] [blame] | 2750 | #include <linux/proc_fs.h> |
Pavel Emelyanov | 7f8ada9 | 2007-10-01 14:41:15 -0700 | [diff] [blame] | 2751 | #include <linux/seq_file.h> |
| 2752 | |
Jeff Layton | 7012b02 | 2013-06-21 08:58:22 -0400 | [diff] [blame] | 2753 | struct locks_iterator { |
| 2754 | int li_cpu; |
| 2755 | loff_t li_pos; |
| 2756 | }; |
| 2757 | |
Jeff Layton | a1c2af3 | 2024-01-31 18:02:14 -0500 | [diff] [blame] | 2758 | static void lock_get_status(struct seq_file *f, struct file_lock_core *flc, |
Luo Longjun | b8da9b1 | 2021-02-25 22:58:29 -0500 | [diff] [blame] | 2759 | loff_t id, char *pfx, int repeat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2760 | { |
| 2761 | struct inode *inode = NULL; |
Jeff Layton | 6021d62 | 2024-01-31 18:01:44 -0500 | [diff] [blame] | 2762 | unsigned int pid; |
Alexey Gladkov | 9d78ede | 2020-05-18 20:07:38 +0200 | [diff] [blame] | 2763 | struct pid_namespace *proc_pidns = proc_pid_ns(file_inode(f->file)->i_sb); |
Jeff Layton | a1c2af3 | 2024-01-31 18:02:14 -0500 | [diff] [blame] | 2764 | int type = flc->flc_type; |
| 2765 | struct file_lock *fl = file_lock(flc); |
Vitaliy Gusev | ab1f161 | 2008-01-17 00:07:08 +0000 | [diff] [blame] | 2766 | |
Jeff Layton | a1c2af3 | 2024-01-31 18:02:14 -0500 | [diff] [blame] | 2767 | pid = locks_translate_pid(flc, proc_pidns); |
| 2768 | |
Benjamin Coddington | 9d5b86a | 2017-07-16 10:28:22 -0400 | [diff] [blame] | 2769 | /* |
Konstantin Khorenko | 1cf8e5d | 2018-06-08 17:27:12 +0300 | [diff] [blame] | 2770 | * 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 Coddington | 9d5b86a | 2017-07-16 10:28:22 -0400 | [diff] [blame] | 2773 | */ |
Jeff Layton | a1c2af3 | 2024-01-31 18:02:14 -0500 | [diff] [blame] | 2774 | if (flc->flc_file != NULL) |
| 2775 | inode = file_inode(flc->flc_file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2776 | |
Luo Longjun | b8da9b1 | 2021-02-25 22:58:29 -0500 | [diff] [blame] | 2777 | seq_printf(f, "%lld: ", id); |
| 2778 | |
| 2779 | if (repeat) |
| 2780 | seq_printf(f, "%*s", repeat - 1 + (int)strlen(pfx), pfx); |
| 2781 | |
Jeff Layton | a1c2af3 | 2024-01-31 18:02:14 -0500 | [diff] [blame] | 2782 | if (flc->flc_flags & FL_POSIX) { |
| 2783 | if (flc->flc_flags & FL_ACCESS) |
Fabian Frederick | 5315c26 | 2014-05-09 14:13:05 -0400 | [diff] [blame] | 2784 | seq_puts(f, "ACCESS"); |
Jeff Layton | a1c2af3 | 2024-01-31 18:02:14 -0500 | [diff] [blame] | 2785 | else if (flc->flc_flags & FL_OFDLCK) |
Fabian Frederick | 5315c26 | 2014-05-09 14:13:05 -0400 | [diff] [blame] | 2786 | seq_puts(f, "OFDLCK"); |
Jeff Layton | c918d42 | 2014-02-03 12:13:09 -0500 | [diff] [blame] | 2787 | else |
Fabian Frederick | 5315c26 | 2014-05-09 14:13:05 -0400 | [diff] [blame] | 2788 | seq_puts(f, "POSIX "); |
Jeff Layton | c918d42 | 2014-02-03 12:13:09 -0500 | [diff] [blame] | 2789 | |
| 2790 | seq_printf(f, " %s ", |
Jeff Layton | f7e33bd | 2021-08-19 14:56:38 -0400 | [diff] [blame] | 2791 | (inode == NULL) ? "*NOINODE*" : "ADVISORY "); |
Jeff Layton | a1c2af3 | 2024-01-31 18:02:14 -0500 | [diff] [blame] | 2792 | } else if (flc->flc_flags & FL_FLOCK) { |
Jeff Layton | 90f7d7a | 2021-09-10 15:36:29 -0400 | [diff] [blame] | 2793 | seq_puts(f, "FLOCK ADVISORY "); |
Jeff Layton | a1c2af3 | 2024-01-31 18:02:14 -0500 | [diff] [blame] | 2794 | } else if (flc->flc_flags & (FL_LEASE|FL_DELEG|FL_LAYOUT)) { |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 2795 | struct file_lease *lease = file_lease(flc); |
| 2796 | |
| 2797 | type = target_leasetype(lease); |
Jeff Layton | 3d40f78 | 2024-01-31 18:01:57 -0500 | [diff] [blame] | 2798 | |
Jeff Layton | a1c2af3 | 2024-01-31 18:02:14 -0500 | [diff] [blame] | 2799 | if (flc->flc_flags & FL_DELEG) |
Jeff Layton | 8144f1f | 2014-08-11 13:36:54 -0400 | [diff] [blame] | 2800 | seq_puts(f, "DELEG "); |
| 2801 | else |
| 2802 | seq_puts(f, "LEASE "); |
| 2803 | |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 2804 | if (lease_breaking(lease)) |
Fabian Frederick | 5315c26 | 2014-05-09 14:13:05 -0400 | [diff] [blame] | 2805 | seq_puts(f, "BREAKING "); |
Jeff Layton | a1c2af3 | 2024-01-31 18:02:14 -0500 | [diff] [blame] | 2806 | else if (flc->flc_file) |
Fabian Frederick | 5315c26 | 2014-05-09 14:13:05 -0400 | [diff] [blame] | 2807 | seq_puts(f, "ACTIVE "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2808 | else |
Fabian Frederick | 5315c26 | 2014-05-09 14:13:05 -0400 | [diff] [blame] | 2809 | seq_puts(f, "BREAKER "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2810 | } else { |
Fabian Frederick | 5315c26 | 2014-05-09 14:13:05 -0400 | [diff] [blame] | 2811 | seq_puts(f, "UNKNOWN UNKNOWN "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2812 | } |
Pavel Begunkov | 43e4cb9 | 2019-07-24 20:16:31 +0300 | [diff] [blame] | 2813 | |
Jeff Layton | 90f7d7a | 2021-09-10 15:36:29 -0400 | [diff] [blame] | 2814 | seq_printf(f, "%s ", (type == F_WRLCK) ? "WRITE" : |
| 2815 | (type == F_RDLCK) ? "READ" : "UNLCK"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2816 | if (inode) { |
Jeff Layton | 3648888 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 2817 | /* userspace relies on this representation of dev_t */ |
Jeff Layton | 6021d62 | 2024-01-31 18:01:44 -0500 | [diff] [blame] | 2818 | seq_printf(f, "%d %02x:%02x:%lu ", pid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2819 | MAJOR(inode->i_sb->s_dev), |
| 2820 | MINOR(inode->i_sb->s_dev), inode->i_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2821 | } else { |
Jeff Layton | 6021d62 | 2024-01-31 18:01:44 -0500 | [diff] [blame] | 2822 | seq_printf(f, "%d <none>:0 ", pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2823 | } |
Jeff Layton | a1c2af3 | 2024-01-31 18:02:14 -0500 | [diff] [blame] | 2824 | if (flc->flc_flags & FL_POSIX) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2825 | if (fl->fl_end == OFFSET_MAX) |
Pavel Emelyanov | 7f8ada9 | 2007-10-01 14:41:15 -0700 | [diff] [blame] | 2826 | seq_printf(f, "%Ld EOF\n", fl->fl_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2827 | else |
Pavel Emelyanov | 7f8ada9 | 2007-10-01 14:41:15 -0700 | [diff] [blame] | 2828 | seq_printf(f, "%Ld %Ld\n", fl->fl_start, fl->fl_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2829 | } else { |
Fabian Frederick | 5315c26 | 2014-05-09 14:13:05 -0400 | [diff] [blame] | 2830 | seq_puts(f, "0 EOF\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2831 | } |
| 2832 | } |
| 2833 | |
Jeff Layton | a1c2af3 | 2024-01-31 18:02:14 -0500 | [diff] [blame] | 2834 | static struct file_lock_core *get_next_blocked_member(struct file_lock_core *node) |
Luo Longjun | b8da9b1 | 2021-02-25 22:58:29 -0500 | [diff] [blame] | 2835 | { |
Jeff Layton | a1c2af3 | 2024-01-31 18:02:14 -0500 | [diff] [blame] | 2836 | struct file_lock_core *tmp; |
Luo Longjun | b8da9b1 | 2021-02-25 22:58:29 -0500 | [diff] [blame] | 2837 | |
| 2838 | /* NULL node or root node */ |
Jeff Layton | a1c2af3 | 2024-01-31 18:02:14 -0500 | [diff] [blame] | 2839 | if (node == NULL || node->flc_blocker == NULL) |
Luo Longjun | b8da9b1 | 2021-02-25 22:58:29 -0500 | [diff] [blame] | 2840 | return NULL; |
| 2841 | |
| 2842 | /* Next member in the linked list could be itself */ |
Jeff Layton | a1c2af3 | 2024-01-31 18:02:14 -0500 | [diff] [blame] | 2843 | 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 Layton | b6aaba5 | 2024-01-31 18:02:07 -0500 | [diff] [blame] | 2846 | || tmp == node) { |
Luo Longjun | b8da9b1 | 2021-02-25 22:58:29 -0500 | [diff] [blame] | 2847 | return NULL; |
| 2848 | } |
| 2849 | |
| 2850 | return tmp; |
| 2851 | } |
| 2852 | |
Pavel Emelyanov | 7f8ada9 | 2007-10-01 14:41:15 -0700 | [diff] [blame] | 2853 | static int locks_show(struct seq_file *f, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2854 | { |
Jeff Layton | 7012b02 | 2013-06-21 08:58:22 -0400 | [diff] [blame] | 2855 | struct locks_iterator *iter = f->private; |
Jeff Layton | a1c2af3 | 2024-01-31 18:02:14 -0500 | [diff] [blame] | 2856 | struct file_lock_core *cur, *tmp; |
Alexey Gladkov | 9d78ede | 2020-05-18 20:07:38 +0200 | [diff] [blame] | 2857 | struct pid_namespace *proc_pidns = proc_pid_ns(file_inode(f->file)->i_sb); |
Luo Longjun | b8da9b1 | 2021-02-25 22:58:29 -0500 | [diff] [blame] | 2858 | int level = 0; |
Pavel Emelyanov | 7f8ada9 | 2007-10-01 14:41:15 -0700 | [diff] [blame] | 2859 | |
Jeff Layton | a1c2af3 | 2024-01-31 18:02:14 -0500 | [diff] [blame] | 2860 | cur = hlist_entry(v, struct file_lock_core, flc_link); |
Pavel Emelyanov | 7f8ada9 | 2007-10-01 14:41:15 -0700 | [diff] [blame] | 2861 | |
Jeff Layton | a1c2af3 | 2024-01-31 18:02:14 -0500 | [diff] [blame] | 2862 | if (locks_translate_pid(cur, proc_pidns) == 0) |
Nikolay Borisov | d67fd44 | 2016-08-17 16:18:46 -0400 | [diff] [blame] | 2863 | return 0; |
| 2864 | |
Jeff Layton | a1c2af3 | 2024-01-31 18:02:14 -0500 | [diff] [blame] | 2865 | /* View this crossed linked list as a binary tree, the first member of flc_blocked_requests |
Jeff Layton | 4ca52f5 | 2024-01-31 18:01:59 -0500 | [diff] [blame] | 2866 | * is the left child of current node, the next silibing in flc_blocked_member is the |
Jeff Layton | a1c2af3 | 2024-01-31 18:02:14 -0500 | [diff] [blame] | 2867 | * right child, we can alse get the parent of current node from flc_blocker, so this |
Luo Longjun | b8da9b1 | 2021-02-25 22:58:29 -0500 | [diff] [blame] | 2868 | * 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 Emelyanov | 7f8ada9 | 2007-10-01 14:41:15 -0700 | [diff] [blame] | 2875 | |
Jeff Layton | a1c2af3 | 2024-01-31 18:02:14 -0500 | [diff] [blame] | 2876 | if (!list_empty(&cur->flc_blocked_requests)) { |
Luo Longjun | b8da9b1 | 2021-02-25 22:58:29 -0500 | [diff] [blame] | 2877 | /* Turn left */ |
Jeff Layton | a1c2af3 | 2024-01-31 18:02:14 -0500 | [diff] [blame] | 2878 | cur = list_first_entry_or_null(&cur->flc_blocked_requests, |
| 2879 | struct file_lock_core, |
| 2880 | flc_blocked_member); |
Luo Longjun | b8da9b1 | 2021-02-25 22:58:29 -0500 | [diff] [blame] | 2881 | level++; |
| 2882 | } else { |
| 2883 | /* Turn right */ |
| 2884 | tmp = get_next_blocked_member(cur); |
| 2885 | /* Fall back to parent node */ |
Jeff Layton | a1c2af3 | 2024-01-31 18:02:14 -0500 | [diff] [blame] | 2886 | while (tmp == NULL && cur->flc_blocker != NULL) { |
| 2887 | cur = cur->flc_blocker; |
Luo Longjun | b8da9b1 | 2021-02-25 22:58:29 -0500 | [diff] [blame] | 2888 | level--; |
| 2889 | tmp = get_next_blocked_member(cur); |
| 2890 | } |
| 2891 | cur = tmp; |
| 2892 | } |
| 2893 | } |
Pavel Emelyanov | 7f8ada9 | 2007-10-01 14:41:15 -0700 | [diff] [blame] | 2894 | |
Pavel Emelyanov | 7f8ada9 | 2007-10-01 14:41:15 -0700 | [diff] [blame] | 2895 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2896 | } |
| 2897 | |
Andrey Vagin | 6c8c903 | 2015-04-16 12:49:38 -0700 | [diff] [blame] | 2898 | static 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 Layton | a1c2af3 | 2024-01-31 18:02:14 -0500 | [diff] [blame] | 2902 | struct file_lock_core *fl; |
Andrey Vagin | 6c8c903 | 2015-04-16 12:49:38 -0700 | [diff] [blame] | 2903 | |
Jeff Layton | a1c2af3 | 2024-01-31 18:02:14 -0500 | [diff] [blame] | 2904 | list_for_each_entry(fl, head, flc_list) { |
Andrey Vagin | 6c8c903 | 2015-04-16 12:49:38 -0700 | [diff] [blame] | 2905 | |
Jeff Layton | a1c2af3 | 2024-01-31 18:02:14 -0500 | [diff] [blame] | 2906 | if (filp != fl->flc_file) |
Andrey Vagin | 6c8c903 | 2015-04-16 12:49:38 -0700 | [diff] [blame] | 2907 | continue; |
Jeff Layton | a1c2af3 | 2024-01-31 18:02:14 -0500 | [diff] [blame] | 2908 | if (fl->flc_owner != files && fl->flc_owner != filp) |
Andrey Vagin | 6c8c903 | 2015-04-16 12:49:38 -0700 | [diff] [blame] | 2909 | continue; |
| 2910 | |
| 2911 | (*id)++; |
| 2912 | seq_puts(f, "lock:\t"); |
Luo Longjun | b8da9b1 | 2021-02-25 22:58:29 -0500 | [diff] [blame] | 2913 | lock_get_status(f, fl, *id, "", 0); |
Andrey Vagin | 6c8c903 | 2015-04-16 12:49:38 -0700 | [diff] [blame] | 2914 | } |
| 2915 | } |
| 2916 | |
| 2917 | void show_fd_locks(struct seq_file *f, |
| 2918 | struct file *filp, struct files_struct *files) |
| 2919 | { |
Jeff Layton | c65454a | 2022-11-25 08:48:37 -0500 | [diff] [blame] | 2920 | struct inode *inode = file_inode(filp); |
Andrey Vagin | 6c8c903 | 2015-04-16 12:49:38 -0700 | [diff] [blame] | 2921 | struct file_lock_context *ctx; |
| 2922 | int id = 0; |
| 2923 | |
Jeff Layton | 401a8b8 | 2022-11-16 09:02:30 -0500 | [diff] [blame] | 2924 | ctx = locks_inode_context(inode); |
Andrey Vagin | 6c8c903 | 2015-04-16 12:49:38 -0700 | [diff] [blame] | 2925 | 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 Emelyanov | 7f8ada9 | 2007-10-01 14:41:15 -0700 | [diff] [blame] | 2935 | static void *locks_start(struct seq_file *f, loff_t *pos) |
Jeff Layton | b03dfdec | 2014-02-03 12:13:07 -0500 | [diff] [blame] | 2936 | __acquires(&blocked_lock_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2937 | { |
Jeff Layton | 7012b02 | 2013-06-21 08:58:22 -0400 | [diff] [blame] | 2938 | struct locks_iterator *iter = f->private; |
Jerome Marchand | 99dc829 | 2010-10-26 14:22:33 -0700 | [diff] [blame] | 2939 | |
Jeff Layton | 7012b02 | 2013-06-21 08:58:22 -0400 | [diff] [blame] | 2940 | iter->li_pos = *pos + 1; |
Peter Zijlstra | aba3766 | 2015-06-22 14:16:33 +0200 | [diff] [blame] | 2941 | percpu_down_write(&file_rwsem); |
Jeff Layton | 7b2296a | 2013-06-21 08:58:20 -0400 | [diff] [blame] | 2942 | spin_lock(&blocked_lock_lock); |
Peter Zijlstra | 7c3f654 | 2015-06-22 14:16:34 +0200 | [diff] [blame] | 2943 | return seq_hlist_start_percpu(&file_lock_list.hlist, &iter->li_cpu, *pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2944 | } |
| 2945 | |
Pavel Emelyanov | 7f8ada9 | 2007-10-01 14:41:15 -0700 | [diff] [blame] | 2946 | static void *locks_next(struct seq_file *f, void *v, loff_t *pos) |
| 2947 | { |
Jeff Layton | 7012b02 | 2013-06-21 08:58:22 -0400 | [diff] [blame] | 2948 | struct locks_iterator *iter = f->private; |
| 2949 | |
| 2950 | ++iter->li_pos; |
Peter Zijlstra | 7c3f654 | 2015-06-22 14:16:34 +0200 | [diff] [blame] | 2951 | return seq_hlist_next_percpu(v, &file_lock_list.hlist, &iter->li_cpu, pos); |
Pavel Emelyanov | 7f8ada9 | 2007-10-01 14:41:15 -0700 | [diff] [blame] | 2952 | } |
| 2953 | |
| 2954 | static void locks_stop(struct seq_file *f, void *v) |
Jeff Layton | b03dfdec | 2014-02-03 12:13:07 -0500 | [diff] [blame] | 2955 | __releases(&blocked_lock_lock) |
Pavel Emelyanov | 7f8ada9 | 2007-10-01 14:41:15 -0700 | [diff] [blame] | 2956 | { |
Jeff Layton | 7b2296a | 2013-06-21 08:58:20 -0400 | [diff] [blame] | 2957 | spin_unlock(&blocked_lock_lock); |
Peter Zijlstra | aba3766 | 2015-06-22 14:16:33 +0200 | [diff] [blame] | 2958 | percpu_up_write(&file_rwsem); |
Pavel Emelyanov | 7f8ada9 | 2007-10-01 14:41:15 -0700 | [diff] [blame] | 2959 | } |
| 2960 | |
Alexey Dobriyan | d8ba7a3 | 2008-10-04 22:34:18 +0400 | [diff] [blame] | 2961 | static const struct seq_operations locks_seq_operations = { |
Pavel Emelyanov | 7f8ada9 | 2007-10-01 14:41:15 -0700 | [diff] [blame] | 2962 | .start = locks_start, |
| 2963 | .next = locks_next, |
| 2964 | .stop = locks_stop, |
| 2965 | .show = locks_show, |
| 2966 | }; |
Alexey Dobriyan | d8ba7a3 | 2008-10-04 22:34:18 +0400 | [diff] [blame] | 2967 | |
Alexey Dobriyan | d8ba7a3 | 2008-10-04 22:34:18 +0400 | [diff] [blame] | 2968 | static int __init proc_locks_init(void) |
| 2969 | { |
Christoph Hellwig | 44414d8 | 2018-04-24 17:05:17 +0200 | [diff] [blame] | 2970 | proc_create_seq_private("locks", 0, NULL, &locks_seq_operations, |
| 2971 | sizeof(struct locks_iterator), NULL); |
Alexey Dobriyan | d8ba7a3 | 2008-10-04 22:34:18 +0400 | [diff] [blame] | 2972 | return 0; |
| 2973 | } |
Paul Gortmaker | 9189922 | 2015-12-17 14:11:03 -0500 | [diff] [blame] | 2974 | fs_initcall(proc_locks_init); |
Pavel Emelyanov | 7f8ada9 | 2007-10-01 14:41:15 -0700 | [diff] [blame] | 2975 | #endif |
| 2976 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2977 | static int __init filelock_init(void) |
| 2978 | { |
Jeff Layton | 7012b02 | 2013-06-21 08:58:22 -0400 | [diff] [blame] | 2979 | int i; |
| 2980 | |
Jeff Layton | 4a075e3 | 2015-01-16 15:05:54 -0500 | [diff] [blame] | 2981 | flctx_cache = kmem_cache_create("file_lock_ctx", |
Linus Torvalds | 3754707 | 2021-09-07 11:21:48 -0700 | [diff] [blame] | 2982 | sizeof(struct file_lock_context), 0, SLAB_PANIC, NULL); |
Jeff Layton | 4a075e3 | 2015-01-16 15:05:54 -0500 | [diff] [blame] | 2983 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2984 | filelock_cache = kmem_cache_create("file_lock_cache", |
Linus Torvalds | 3754707 | 2021-09-07 11:21:48 -0700 | [diff] [blame] | 2985 | sizeof(struct file_lock), 0, SLAB_PANIC, NULL); |
Miklos Szeredi | ee19cc4 | 2011-07-07 13:06:09 +0200 | [diff] [blame] | 2986 | |
Jeff Layton | c69ff40 | 2024-01-31 18:02:28 -0500 | [diff] [blame] | 2987 | filelease_cache = kmem_cache_create("file_lock_cache", |
| 2988 | sizeof(struct file_lease), 0, SLAB_PANIC, NULL); |
| 2989 | |
Peter Zijlstra | 7c3f654 | 2015-06-22 14:16:34 +0200 | [diff] [blame] | 2990 | 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 Layton | 7012b02 | 2013-06-21 08:58:22 -0400 | [diff] [blame] | 2996 | |
Jeff Layton | 18f6622 | 2019-08-18 14:18:45 -0400 | [diff] [blame] | 2997 | lease_notifier_chain_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2998 | return 0; |
| 2999 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3000 | core_initcall(filelock_init); |