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