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