Thomas Gleixner | 7336d0e | 2019-05-31 01:09:56 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 2 | /* |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 3 | * Copyright (C) 2005-2008 Red Hat, Inc. All rights reserved. |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 6 | #include <linux/fs.h> |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 7 | #include <linux/miscdevice.h> |
Al Viro | bd01f84 | 2006-10-19 17:23:57 -0400 | [diff] [blame] | 8 | #include <linux/poll.h> |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 9 | #include <linux/dlm.h> |
| 10 | #include <linux/dlm_plock.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/slab.h> |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 12 | |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 13 | #include "dlm_internal.h" |
| 14 | #include "lockspace.h" |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 15 | |
Alexander Aring | 314a554 | 2022-04-04 16:06:36 -0400 | [diff] [blame^] | 16 | static DEFINE_SPINLOCK(ops_lock); |
| 17 | static LIST_HEAD(send_list); |
| 18 | static LIST_HEAD(recv_list); |
| 19 | static DECLARE_WAIT_QUEUE_HEAD(send_wq); |
| 20 | static DECLARE_WAIT_QUEUE_HEAD(recv_wq); |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 21 | |
Alexander Aring | bcbb4ba | 2022-04-04 16:06:32 -0400 | [diff] [blame] | 22 | struct plock_async_data { |
| 23 | void *fl; |
| 24 | void *file; |
| 25 | struct file_lock flc; |
| 26 | int (*callback)(struct file_lock *fl, int result); |
| 27 | }; |
| 28 | |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 29 | struct plock_op { |
| 30 | struct list_head list; |
| 31 | int done; |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 32 | struct dlm_plock_info info; |
Alexander Aring | bcbb4ba | 2022-04-04 16:06:32 -0400 | [diff] [blame] | 33 | /* if set indicates async handling */ |
| 34 | struct plock_async_data *data; |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 37 | static inline void set_version(struct dlm_plock_info *info) |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 38 | { |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 39 | info->version[0] = DLM_PLOCK_VERSION_MAJOR; |
| 40 | info->version[1] = DLM_PLOCK_VERSION_MINOR; |
| 41 | info->version[2] = DLM_PLOCK_VERSION_PATCH; |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 42 | } |
| 43 | |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 44 | static int check_version(struct dlm_plock_info *info) |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 45 | { |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 46 | if ((DLM_PLOCK_VERSION_MAJOR != info->version[0]) || |
| 47 | (DLM_PLOCK_VERSION_MINOR < info->version[1])) { |
| 48 | log_print("plock device version mismatch: " |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 49 | "kernel (%u.%u.%u), user (%u.%u.%u)", |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 50 | DLM_PLOCK_VERSION_MAJOR, |
| 51 | DLM_PLOCK_VERSION_MINOR, |
| 52 | DLM_PLOCK_VERSION_PATCH, |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 53 | info->version[0], |
| 54 | info->version[1], |
| 55 | info->version[2]); |
| 56 | return -EINVAL; |
| 57 | } |
| 58 | return 0; |
| 59 | } |
| 60 | |
Alexander Aring | bcbb4ba | 2022-04-04 16:06:32 -0400 | [diff] [blame] | 61 | static void dlm_release_plock_op(struct plock_op *op) |
| 62 | { |
| 63 | kfree(op->data); |
| 64 | kfree(op); |
| 65 | } |
| 66 | |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 67 | static void send_op(struct plock_op *op) |
| 68 | { |
| 69 | set_version(&op->info); |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 70 | spin_lock(&ops_lock); |
| 71 | list_add_tail(&op->list, &send_list); |
| 72 | spin_unlock(&ops_lock); |
| 73 | wake_up(&send_wq); |
| 74 | } |
| 75 | |
David Teigland | 901025d | 2011-03-02 14:20:04 -0600 | [diff] [blame] | 76 | /* If a process was killed while waiting for the only plock on a file, |
| 77 | locks_remove_posix will not see any lock on the file so it won't |
| 78 | send an unlock-close to us to pass on to userspace to clean up the |
| 79 | abandoned waiter. So, we have to insert the unlock-close when the |
| 80 | lock call is interrupted. */ |
| 81 | |
| 82 | static void do_unlock_close(struct dlm_ls *ls, u64 number, |
| 83 | struct file *file, struct file_lock *fl) |
| 84 | { |
| 85 | struct plock_op *op; |
| 86 | |
| 87 | op = kzalloc(sizeof(*op), GFP_NOFS); |
| 88 | if (!op) |
| 89 | return; |
| 90 | |
| 91 | op->info.optype = DLM_PLOCK_OP_UNLOCK; |
| 92 | op->info.pid = fl->fl_pid; |
| 93 | op->info.fsid = ls->ls_global_id; |
| 94 | op->info.number = number; |
| 95 | op->info.start = 0; |
| 96 | op->info.end = OFFSET_MAX; |
J. Bruce Fields | 8fb47a4 | 2011-07-20 20:21:59 -0400 | [diff] [blame] | 97 | if (fl->fl_lmops && fl->fl_lmops->lm_grant) |
David Teigland | 901025d | 2011-03-02 14:20:04 -0600 | [diff] [blame] | 98 | op->info.owner = (__u64) fl->fl_pid; |
| 99 | else |
| 100 | op->info.owner = (__u64)(long) fl->fl_owner; |
| 101 | |
| 102 | op->info.flags |= DLM_PLOCK_FL_CLOSE; |
| 103 | send_op(op); |
| 104 | } |
| 105 | |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 106 | int dlm_posix_lock(dlm_lockspace_t *lockspace, u64 number, struct file *file, |
| 107 | int cmd, struct file_lock *fl) |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 108 | { |
Alexander Aring | bcbb4ba | 2022-04-04 16:06:32 -0400 | [diff] [blame] | 109 | struct plock_async_data *op_data; |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 110 | struct dlm_ls *ls; |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 111 | struct plock_op *op; |
| 112 | int rv; |
| 113 | |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 114 | ls = dlm_find_lockspace_local(lockspace); |
| 115 | if (!ls) |
| 116 | return -EINVAL; |
| 117 | |
Alexander Aring | bcbb4ba | 2022-04-04 16:06:32 -0400 | [diff] [blame] | 118 | op = kzalloc(sizeof(*op), GFP_NOFS); |
| 119 | if (!op) { |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 120 | rv = -ENOMEM; |
| 121 | goto out; |
| 122 | } |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 123 | |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 124 | op->info.optype = DLM_PLOCK_OP_LOCK; |
David Teigland | 3a2a9c9 | 2006-04-25 15:45:51 -0400 | [diff] [blame] | 125 | op->info.pid = fl->fl_pid; |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 126 | op->info.ex = (fl->fl_type == F_WRLCK); |
| 127 | op->info.wait = IS_SETLKW(cmd); |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 128 | op->info.fsid = ls->ls_global_id; |
| 129 | op->info.number = number; |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 130 | op->info.start = fl->fl_start; |
| 131 | op->info.end = fl->fl_end; |
Alexander Aring | bcbb4ba | 2022-04-04 16:06:32 -0400 | [diff] [blame] | 132 | /* async handling */ |
J. Bruce Fields | 8fb47a4 | 2011-07-20 20:21:59 -0400 | [diff] [blame] | 133 | if (fl->fl_lmops && fl->fl_lmops->lm_grant) { |
Alexander Aring | bcbb4ba | 2022-04-04 16:06:32 -0400 | [diff] [blame] | 134 | op_data = kzalloc(sizeof(*op_data), GFP_NOFS); |
| 135 | if (!op_data) { |
| 136 | dlm_release_plock_op(op); |
| 137 | rv = -ENOMEM; |
| 138 | goto out; |
| 139 | } |
| 140 | |
David Teigland | 2066b58 | 2007-12-06 09:35:25 -0600 | [diff] [blame] | 141 | /* fl_owner is lockd which doesn't distinguish |
| 142 | processes on the nfs client */ |
| 143 | op->info.owner = (__u64) fl->fl_pid; |
Alexander Aring | bcbb4ba | 2022-04-04 16:06:32 -0400 | [diff] [blame] | 144 | op_data->callback = fl->fl_lmops->lm_grant; |
| 145 | locks_init_lock(&op_data->flc); |
| 146 | locks_copy_lock(&op_data->flc, fl); |
| 147 | op_data->fl = fl; |
| 148 | op_data->file = file; |
| 149 | |
| 150 | op->data = op_data; |
Alexander Aring | a800ba7 | 2022-04-04 16:06:33 -0400 | [diff] [blame] | 151 | |
| 152 | send_op(op); |
| 153 | rv = FILE_LOCK_DEFERRED; |
| 154 | goto out; |
David Teigland | 2066b58 | 2007-12-06 09:35:25 -0600 | [diff] [blame] | 155 | } else { |
| 156 | op->info.owner = (__u64)(long) fl->fl_owner; |
David Teigland | 2066b58 | 2007-12-06 09:35:25 -0600 | [diff] [blame] | 157 | } |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 158 | |
| 159 | send_op(op); |
Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 160 | |
Alexander Aring | a800ba7 | 2022-04-04 16:06:33 -0400 | [diff] [blame] | 161 | rv = wait_event_interruptible(recv_wq, (op->done != 0)); |
| 162 | if (rv == -ERESTARTSYS) { |
Alexander Aring | a800ba7 | 2022-04-04 16:06:33 -0400 | [diff] [blame] | 163 | spin_lock(&ops_lock); |
| 164 | list_del(&op->list); |
| 165 | spin_unlock(&ops_lock); |
Alexander Aring | bcfad42 | 2022-04-04 16:06:34 -0400 | [diff] [blame] | 166 | log_print("%s: wait interrupted %x %llx, op removed", |
| 167 | __func__, ls->ls_global_id, |
| 168 | (unsigned long long)number); |
Alexander Aring | a800ba7 | 2022-04-04 16:06:33 -0400 | [diff] [blame] | 169 | dlm_release_plock_op(op); |
| 170 | do_unlock_close(ls, number, file, fl); |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 171 | goto out; |
| 172 | } |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 173 | |
Alexander Aring | a559790 | 2022-04-04 16:06:31 -0400 | [diff] [blame] | 174 | WARN_ON(!list_empty(&op->list)); |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 175 | |
| 176 | rv = op->info.rv; |
| 177 | |
| 178 | if (!rv) { |
Benjamin Coddington | 4f65636 | 2015-10-22 13:38:14 -0400 | [diff] [blame] | 179 | if (locks_lock_file_wait(file, fl) < 0) |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 180 | log_error(ls, "dlm_posix_lock: vfs lock error %llx", |
| 181 | (unsigned long long)number); |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Alexander Aring | bcbb4ba | 2022-04-04 16:06:32 -0400 | [diff] [blame] | 184 | dlm_release_plock_op(op); |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 185 | out: |
| 186 | dlm_put_lockspace(ls); |
Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 187 | return rv; |
| 188 | } |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 189 | EXPORT_SYMBOL_GPL(dlm_posix_lock); |
Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 190 | |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 191 | /* Returns failure iff a successful lock operation should be canceled */ |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 192 | static int dlm_plock_callback(struct plock_op *op) |
Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 193 | { |
Alexander Aring | bcbb4ba | 2022-04-04 16:06:32 -0400 | [diff] [blame] | 194 | struct plock_async_data *op_data = op->data; |
Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 195 | struct file *file; |
| 196 | struct file_lock *fl; |
| 197 | struct file_lock *flc; |
Joe Perches | d0449b9 | 2014-08-22 10:18:42 -0400 | [diff] [blame] | 198 | int (*notify)(struct file_lock *fl, int result) = NULL; |
Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 199 | int rv = 0; |
| 200 | |
Alexander Aring | a559790 | 2022-04-04 16:06:31 -0400 | [diff] [blame] | 201 | WARN_ON(!list_empty(&op->list)); |
Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 202 | |
| 203 | /* check if the following 2 are still valid or make a copy */ |
Alexander Aring | bcbb4ba | 2022-04-04 16:06:32 -0400 | [diff] [blame] | 204 | file = op_data->file; |
| 205 | flc = &op_data->flc; |
| 206 | fl = op_data->fl; |
| 207 | notify = op_data->callback; |
Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 208 | |
| 209 | if (op->info.rv) { |
Joe Perches | d0449b9 | 2014-08-22 10:18:42 -0400 | [diff] [blame] | 210 | notify(fl, op->info.rv); |
Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 211 | goto out; |
| 212 | } |
| 213 | |
| 214 | /* got fs lock; bookkeep locally as well: */ |
| 215 | flc->fl_flags &= ~FL_SLEEP; |
| 216 | if (posix_lock_file(file, flc, NULL)) { |
| 217 | /* |
| 218 | * This can only happen in the case of kmalloc() failure. |
| 219 | * The filesystem's own lock is the authoritative lock, |
| 220 | * so a failure to get the lock locally is not a disaster. |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 221 | * As long as the fs cannot reliably cancel locks (especially |
Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 222 | * in a low-memory situation), we're better off ignoring |
| 223 | * this failure than trying to recover. |
| 224 | */ |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 225 | log_print("dlm_plock_callback: vfs lock error %llx file %p fl %p", |
| 226 | (unsigned long long)op->info.number, file, fl); |
Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 227 | } |
| 228 | |
Joe Perches | d0449b9 | 2014-08-22 10:18:42 -0400 | [diff] [blame] | 229 | rv = notify(fl, 0); |
Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 230 | if (rv) { |
| 231 | /* XXX: We need to cancel the fs lock here: */ |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 232 | log_print("dlm_plock_callback: lock granted after lock request " |
| 233 | "failed; dangling lock!\n"); |
Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 234 | goto out; |
| 235 | } |
| 236 | |
| 237 | out: |
Alexander Aring | bcbb4ba | 2022-04-04 16:06:32 -0400 | [diff] [blame] | 238 | dlm_release_plock_op(op); |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 239 | return rv; |
| 240 | } |
| 241 | |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 242 | int dlm_posix_unlock(dlm_lockspace_t *lockspace, u64 number, struct file *file, |
| 243 | struct file_lock *fl) |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 244 | { |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 245 | struct dlm_ls *ls; |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 246 | struct plock_op *op; |
| 247 | int rv; |
David Teigland | 9000831 | 2013-04-05 10:57:15 +0100 | [diff] [blame] | 248 | unsigned char fl_flags = fl->fl_flags; |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 249 | |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 250 | ls = dlm_find_lockspace_local(lockspace); |
| 251 | if (!ls) |
| 252 | return -EINVAL; |
| 253 | |
David Teigland | 573c24c | 2009-11-30 16:34:43 -0600 | [diff] [blame] | 254 | op = kzalloc(sizeof(*op), GFP_NOFS); |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 255 | if (!op) { |
| 256 | rv = -ENOMEM; |
| 257 | goto out; |
| 258 | } |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 259 | |
David Teigland | 9000831 | 2013-04-05 10:57:15 +0100 | [diff] [blame] | 260 | /* cause the vfs unlock to return ENOENT if lock is not found */ |
| 261 | fl->fl_flags |= FL_EXISTS; |
| 262 | |
Benjamin Coddington | 4f65636 | 2015-10-22 13:38:14 -0400 | [diff] [blame] | 263 | rv = locks_lock_file_wait(file, fl); |
David Teigland | 9000831 | 2013-04-05 10:57:15 +0100 | [diff] [blame] | 264 | if (rv == -ENOENT) { |
| 265 | rv = 0; |
| 266 | goto out_free; |
| 267 | } |
| 268 | if (rv < 0) { |
| 269 | log_error(ls, "dlm_posix_unlock: vfs unlock error %d %llx", |
| 270 | rv, (unsigned long long)number); |
| 271 | } |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 272 | |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 273 | op->info.optype = DLM_PLOCK_OP_UNLOCK; |
David Teigland | 3a2a9c9 | 2006-04-25 15:45:51 -0400 | [diff] [blame] | 274 | op->info.pid = fl->fl_pid; |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 275 | op->info.fsid = ls->ls_global_id; |
| 276 | op->info.number = number; |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 277 | op->info.start = fl->fl_start; |
| 278 | op->info.end = fl->fl_end; |
J. Bruce Fields | 8fb47a4 | 2011-07-20 20:21:59 -0400 | [diff] [blame] | 279 | if (fl->fl_lmops && fl->fl_lmops->lm_grant) |
David Teigland | 2066b58 | 2007-12-06 09:35:25 -0600 | [diff] [blame] | 280 | op->info.owner = (__u64) fl->fl_pid; |
| 281 | else |
| 282 | op->info.owner = (__u64)(long) fl->fl_owner; |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 283 | |
David Teigland | 901025d | 2011-03-02 14:20:04 -0600 | [diff] [blame] | 284 | if (fl->fl_flags & FL_CLOSE) { |
| 285 | op->info.flags |= DLM_PLOCK_FL_CLOSE; |
| 286 | send_op(op); |
| 287 | rv = 0; |
| 288 | goto out; |
| 289 | } |
| 290 | |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 291 | send_op(op); |
| 292 | wait_event(recv_wq, (op->done != 0)); |
| 293 | |
Alexander Aring | a559790 | 2022-04-04 16:06:31 -0400 | [diff] [blame] | 294 | WARN_ON(!list_empty(&op->list)); |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 295 | |
| 296 | rv = op->info.rv; |
| 297 | |
Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 298 | if (rv == -ENOENT) |
| 299 | rv = 0; |
| 300 | |
David Teigland | 9000831 | 2013-04-05 10:57:15 +0100 | [diff] [blame] | 301 | out_free: |
Alexander Aring | bcbb4ba | 2022-04-04 16:06:32 -0400 | [diff] [blame] | 302 | dlm_release_plock_op(op); |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 303 | out: |
| 304 | dlm_put_lockspace(ls); |
David Teigland | 9000831 | 2013-04-05 10:57:15 +0100 | [diff] [blame] | 305 | fl->fl_flags = fl_flags; |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 306 | return rv; |
| 307 | } |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 308 | EXPORT_SYMBOL_GPL(dlm_posix_unlock); |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 309 | |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 310 | int dlm_posix_get(dlm_lockspace_t *lockspace, u64 number, struct file *file, |
| 311 | struct file_lock *fl) |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 312 | { |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 313 | struct dlm_ls *ls; |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 314 | struct plock_op *op; |
| 315 | int rv; |
| 316 | |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 317 | ls = dlm_find_lockspace_local(lockspace); |
| 318 | if (!ls) |
| 319 | return -EINVAL; |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 320 | |
David Teigland | 573c24c | 2009-11-30 16:34:43 -0600 | [diff] [blame] | 321 | op = kzalloc(sizeof(*op), GFP_NOFS); |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 322 | if (!op) { |
| 323 | rv = -ENOMEM; |
| 324 | goto out; |
| 325 | } |
| 326 | |
| 327 | op->info.optype = DLM_PLOCK_OP_GET; |
David Teigland | 3a2a9c9 | 2006-04-25 15:45:51 -0400 | [diff] [blame] | 328 | op->info.pid = fl->fl_pid; |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 329 | op->info.ex = (fl->fl_type == F_WRLCK); |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 330 | op->info.fsid = ls->ls_global_id; |
| 331 | op->info.number = number; |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 332 | op->info.start = fl->fl_start; |
| 333 | op->info.end = fl->fl_end; |
J. Bruce Fields | 8fb47a4 | 2011-07-20 20:21:59 -0400 | [diff] [blame] | 334 | if (fl->fl_lmops && fl->fl_lmops->lm_grant) |
David Teigland | 2066b58 | 2007-12-06 09:35:25 -0600 | [diff] [blame] | 335 | op->info.owner = (__u64) fl->fl_pid; |
| 336 | else |
| 337 | op->info.owner = (__u64)(long) fl->fl_owner; |
Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 338 | |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 339 | send_op(op); |
| 340 | wait_event(recv_wq, (op->done != 0)); |
| 341 | |
Alexander Aring | a559790 | 2022-04-04 16:06:31 -0400 | [diff] [blame] | 342 | WARN_ON(!list_empty(&op->list)); |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 343 | |
David Teigland | a7a2ff8 | 2007-06-08 17:01:40 -0500 | [diff] [blame] | 344 | /* info.rv from userspace is 1 for conflict, 0 for no-conflict, |
| 345 | -ENOENT if there are no locks on the file */ |
| 346 | |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 347 | rv = op->info.rv; |
| 348 | |
Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 349 | fl->fl_type = F_UNLCK; |
| 350 | if (rv == -ENOENT) |
| 351 | rv = 0; |
David Teigland | a7a2ff8 | 2007-06-08 17:01:40 -0500 | [diff] [blame] | 352 | else if (rv > 0) { |
Jeff Layton | 20d5a39 | 2009-01-21 11:34:50 -0500 | [diff] [blame] | 353 | locks_init_lock(fl); |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 354 | fl->fl_type = (op->info.ex) ? F_WRLCK : F_RDLCK; |
Jeff Layton | 20d5a39 | 2009-01-21 11:34:50 -0500 | [diff] [blame] | 355 | fl->fl_flags = FL_POSIX; |
Benjamin Coddington | 9d5b86a | 2017-07-16 10:28:22 -0400 | [diff] [blame] | 356 | fl->fl_pid = -op->info.pid; |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 357 | fl->fl_start = op->info.start; |
| 358 | fl->fl_end = op->info.end; |
David Teigland | a7a2ff8 | 2007-06-08 17:01:40 -0500 | [diff] [blame] | 359 | rv = 0; |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Alexander Aring | bcbb4ba | 2022-04-04 16:06:32 -0400 | [diff] [blame] | 362 | dlm_release_plock_op(op); |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 363 | out: |
| 364 | dlm_put_lockspace(ls); |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 365 | return rv; |
| 366 | } |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 367 | EXPORT_SYMBOL_GPL(dlm_posix_get); |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 368 | |
| 369 | /* a read copies out one plock request from the send list */ |
| 370 | static ssize_t dev_read(struct file *file, char __user *u, size_t count, |
| 371 | loff_t *ppos) |
| 372 | { |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 373 | struct dlm_plock_info info; |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 374 | struct plock_op *op = NULL; |
| 375 | |
| 376 | if (count < sizeof(info)) |
| 377 | return -EINVAL; |
| 378 | |
| 379 | spin_lock(&ops_lock); |
| 380 | if (!list_empty(&send_list)) { |
| 381 | op = list_entry(send_list.next, struct plock_op, list); |
David Teigland | 901025d | 2011-03-02 14:20:04 -0600 | [diff] [blame] | 382 | if (op->info.flags & DLM_PLOCK_FL_CLOSE) |
| 383 | list_del(&op->list); |
| 384 | else |
| 385 | list_move(&op->list, &recv_list); |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 386 | memcpy(&info, &op->info, sizeof(info)); |
| 387 | } |
| 388 | spin_unlock(&ops_lock); |
| 389 | |
| 390 | if (!op) |
| 391 | return -EAGAIN; |
| 392 | |
David Teigland | 901025d | 2011-03-02 14:20:04 -0600 | [diff] [blame] | 393 | /* there is no need to get a reply from userspace for unlocks |
| 394 | that were generated by the vfs cleaning up for a close |
| 395 | (the process did not make an unlock call). */ |
| 396 | |
| 397 | if (op->info.flags & DLM_PLOCK_FL_CLOSE) |
Alexander Aring | bcbb4ba | 2022-04-04 16:06:32 -0400 | [diff] [blame] | 398 | dlm_release_plock_op(op); |
David Teigland | 901025d | 2011-03-02 14:20:04 -0600 | [diff] [blame] | 399 | |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 400 | if (copy_to_user(u, &info, sizeof(info))) |
| 401 | return -EFAULT; |
| 402 | return sizeof(info); |
| 403 | } |
| 404 | |
| 405 | /* a write copies in one plock result that should match a plock_op |
| 406 | on the recv list */ |
| 407 | static ssize_t dev_write(struct file *file, const char __user *u, size_t count, |
| 408 | loff_t *ppos) |
| 409 | { |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 410 | struct dlm_plock_info info; |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 411 | struct plock_op *op; |
David Teigland | c78a87d | 2009-06-18 13:20:24 -0500 | [diff] [blame] | 412 | int found = 0, do_callback = 0; |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 413 | |
| 414 | if (count != sizeof(info)) |
| 415 | return -EINVAL; |
| 416 | |
| 417 | if (copy_from_user(&info, u, sizeof(info))) |
| 418 | return -EFAULT; |
| 419 | |
| 420 | if (check_version(&info)) |
| 421 | return -EINVAL; |
| 422 | |
| 423 | spin_lock(&ops_lock); |
| 424 | list_for_each_entry(op, &recv_list, list) { |
David Teigland | c78a87d | 2009-06-18 13:20:24 -0500 | [diff] [blame] | 425 | if (op->info.fsid == info.fsid && |
| 426 | op->info.number == info.number && |
David Teigland | 08eac93 | 2006-08-04 16:19:20 -0500 | [diff] [blame] | 427 | op->info.owner == info.owner) { |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 428 | list_del_init(&op->list); |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 429 | memcpy(&op->info, &info, sizeof(info)); |
Alexander Aring | bcbb4ba | 2022-04-04 16:06:32 -0400 | [diff] [blame] | 430 | if (op->data) |
David Teigland | c78a87d | 2009-06-18 13:20:24 -0500 | [diff] [blame] | 431 | do_callback = 1; |
| 432 | else |
| 433 | op->done = 1; |
| 434 | found = 1; |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 435 | break; |
| 436 | } |
| 437 | } |
| 438 | spin_unlock(&ops_lock); |
| 439 | |
Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 440 | if (found) { |
David Teigland | c78a87d | 2009-06-18 13:20:24 -0500 | [diff] [blame] | 441 | if (do_callback) |
David Teigland | 817d10b | 2008-05-13 14:28:26 -0500 | [diff] [blame] | 442 | dlm_plock_callback(op); |
Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 443 | else |
| 444 | wake_up(&recv_wq); |
| 445 | } else |
Alexander Aring | bcfad42 | 2022-04-04 16:06:34 -0400 | [diff] [blame] | 446 | log_print("%s: no op %x %llx - may got interrupted?", __func__, |
| 447 | info.fsid, (unsigned long long)info.number); |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 448 | return count; |
| 449 | } |
| 450 | |
Al Viro | 076ccb7 | 2017-07-03 01:02:18 -0400 | [diff] [blame] | 451 | static __poll_t dev_poll(struct file *file, poll_table *wait) |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 452 | { |
Al Viro | 076ccb7 | 2017-07-03 01:02:18 -0400 | [diff] [blame] | 453 | __poll_t mask = 0; |
Denis Cheng | cee23c7 | 2007-07-25 17:53:58 +0800 | [diff] [blame] | 454 | |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 455 | poll_wait(file, &send_wq, wait); |
| 456 | |
| 457 | spin_lock(&ops_lock); |
Denis Cheng | cee23c7 | 2007-07-25 17:53:58 +0800 | [diff] [blame] | 458 | if (!list_empty(&send_list)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 459 | mask = EPOLLIN | EPOLLRDNORM; |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 460 | spin_unlock(&ops_lock); |
Denis Cheng | cee23c7 | 2007-07-25 17:53:58 +0800 | [diff] [blame] | 461 | |
| 462 | return mask; |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 465 | static const struct file_operations dev_fops = { |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 466 | .read = dev_read, |
| 467 | .write = dev_write, |
| 468 | .poll = dev_poll, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 469 | .owner = THIS_MODULE, |
| 470 | .llseek = noop_llseek, |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 471 | }; |
| 472 | |
| 473 | static struct miscdevice plock_dev_misc = { |
| 474 | .minor = MISC_DYNAMIC_MINOR, |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 475 | .name = DLM_PLOCK_MISC_NAME, |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 476 | .fops = &dev_fops |
| 477 | }; |
| 478 | |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 479 | int dlm_plock_init(void) |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 480 | { |
| 481 | int rv; |
| 482 | |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 483 | rv = misc_register(&plock_dev_misc); |
| 484 | if (rv) |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 485 | log_print("dlm_plock_init: misc_register failed %d", rv); |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 486 | return rv; |
| 487 | } |
| 488 | |
David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 489 | void dlm_plock_exit(void) |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 490 | { |
Greg Kroah-Hartman | f368ed6 | 2015-07-30 15:59:57 -0700 | [diff] [blame] | 491 | misc_deregister(&plock_dev_misc); |
David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 492 | } |
| 493 | |