1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2005-2008 Red Hat, Inc. All rights reserved.
4 */
5
6 #include <linux/fs.h>
7 #include <linux/miscdevice.h>
8 #include <linux/poll.h>
9 #include <linux/dlm.h>
10 #include <linux/dlm_plock.h>
11 #include <linux/slab.h>
12
13 #include "dlm_internal.h"
14 #include "lockspace.h"
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;
25 struct dlm_plock_info info;
26 int (*callback)(struct file_lock *fl, int result);
27 };
28
29 struct plock_xop {
30 struct plock_op xop;
31 void *fl;
32 void *file;
33 struct file_lock flc;
34 };
35
36
set_version(struct dlm_plock_info * info)37 static inline void set_version(struct dlm_plock_info *info)
38 {
39 info->version[0] = DLM_PLOCK_VERSION_MAJOR;
40 info->version[1] = DLM_PLOCK_VERSION_MINOR;
41 info->version[2] = DLM_PLOCK_VERSION_PATCH;
42 }
43
check_version(struct dlm_plock_info * info)44 static int check_version(struct dlm_plock_info *info)
45 {
46 if ((DLM_PLOCK_VERSION_MAJOR != info->version[0]) ||
47 (DLM_PLOCK_VERSION_MINOR < info->version[1])) {
48 log_print("plock device version mismatch: "
49 "kernel (%u.%u.%u), user (%u.%u.%u)",
50 DLM_PLOCK_VERSION_MAJOR,
51 DLM_PLOCK_VERSION_MINOR,
52 DLM_PLOCK_VERSION_PATCH,
53 info->version[0],
54 info->version[1],
55 info->version[2]);
56 return -EINVAL;
57 }
58 return 0;
59 }
60
send_op(struct plock_op * op)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
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
do_unlock_close(struct dlm_ls * ls,u64 number,struct file * file,struct file_lock * fl)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;
92 if (fl->fl_lmops && fl->fl_lmops->lm_grant)
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
dlm_posix_lock(dlm_lockspace_t * lockspace,u64 number,struct file * file,int cmd,struct file_lock * fl)101 int dlm_posix_lock(dlm_lockspace_t *lockspace, u64 number, struct file *file,
102 int cmd, struct file_lock *fl)
103 {
104 struct dlm_ls *ls;
105 struct plock_op *op;
106 struct plock_xop *xop;
107 int rv;
108
109 ls = dlm_find_lockspace_local(lockspace);
110 if (!ls)
111 return -EINVAL;
112
113 xop = kzalloc(sizeof(*xop), GFP_NOFS);
114 if (!xop) {
115 rv = -ENOMEM;
116 goto out;
117 }
118
119 op = &xop->xop;
120 op->info.optype = DLM_PLOCK_OP_LOCK;
121 op->info.pid = fl->fl_pid;
122 op->info.ex = (fl->fl_type == F_WRLCK);
123 op->info.wait = IS_SETLKW(cmd);
124 op->info.fsid = ls->ls_global_id;
125 op->info.number = number;
126 op->info.start = fl->fl_start;
127 op->info.end = fl->fl_end;
128 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
129 /* fl_owner is lockd which doesn't distinguish
130 processes on the nfs client */
131 op->info.owner = (__u64) fl->fl_pid;
132 op->callback = fl->fl_lmops->lm_grant;
133 locks_init_lock(&xop->flc);
134 locks_copy_lock(&xop->flc, fl);
135 xop->fl = fl;
136 xop->file = file;
137 } else {
138 op->info.owner = (__u64)(long) fl->fl_owner;
139 }
140
141 send_op(op);
142
143 if (!op->callback) {
144 rv = wait_event_interruptible(recv_wq, (op->done != 0));
145 if (rv == -ERESTARTSYS) {
146 log_debug(ls, "dlm_posix_lock: wait killed %llx",
147 (unsigned long long)number);
148 spin_lock(&ops_lock);
149 list_del(&op->list);
150 spin_unlock(&ops_lock);
151 kfree(xop);
152 do_unlock_close(ls, number, file, fl);
153 goto out;
154 }
155 } else {
156 rv = FILE_LOCK_DEFERRED;
157 goto out;
158 }
159
160 spin_lock(&ops_lock);
161 if (!list_empty(&op->list)) {
162 log_error(ls, "dlm_posix_lock: op on list %llx",
163 (unsigned long long)number);
164 list_del(&op->list);
165 }
166 spin_unlock(&ops_lock);
167
168 rv = op->info.rv;
169
170 if (!rv) {
171 if (locks_lock_file_wait(file, fl) < 0)
172 log_error(ls, "dlm_posix_lock: vfs lock error %llx",
173 (unsigned long long)number);
174 }
175
176 kfree(xop);
177 out:
178 dlm_put_lockspace(ls);
179 return rv;
180 }
181 EXPORT_SYMBOL_GPL(dlm_posix_lock);
182
183 /* Returns failure iff a successful lock operation should be canceled */
dlm_plock_callback(struct plock_op * op)184 static int dlm_plock_callback(struct plock_op *op)
185 {
186 struct file *file;
187 struct file_lock *fl;
188 struct file_lock *flc;
189 int (*notify)(struct file_lock *fl, int result) = NULL;
190 struct plock_xop *xop = (struct plock_xop *)op;
191 int rv = 0;
192
193 spin_lock(&ops_lock);
194 if (!list_empty(&op->list)) {
195 log_print("dlm_plock_callback: op on list %llx",
196 (unsigned long long)op->info.number);
197 list_del(&op->list);
198 }
199 spin_unlock(&ops_lock);
200
201 /* check if the following 2 are still valid or make a copy */
202 file = xop->file;
203 flc = &xop->flc;
204 fl = xop->fl;
205 notify = op->callback;
206
207 if (op->info.rv) {
208 notify(fl, op->info.rv);
209 goto out;
210 }
211
212 /* got fs lock; bookkeep locally as well: */
213 flc->fl_flags &= ~FL_SLEEP;
214 if (posix_lock_file(file, flc, NULL)) {
215 /*
216 * This can only happen in the case of kmalloc() failure.
217 * The filesystem's own lock is the authoritative lock,
218 * so a failure to get the lock locally is not a disaster.
219 * As long as the fs cannot reliably cancel locks (especially
220 * in a low-memory situation), we're better off ignoring
221 * this failure than trying to recover.
222 */
223 log_print("dlm_plock_callback: vfs lock error %llx file %p fl %p",
224 (unsigned long long)op->info.number, file, fl);
225 }
226
227 rv = notify(fl, 0);
228 if (rv) {
229 /* XXX: We need to cancel the fs lock here: */
230 log_print("dlm_plock_callback: lock granted after lock request "
231 "failed; dangling lock!\n");
232 goto out;
233 }
234
235 out:
236 kfree(xop);
237 return rv;
238 }
239
dlm_posix_unlock(dlm_lockspace_t * lockspace,u64 number,struct file * file,struct file_lock * fl)240 int dlm_posix_unlock(dlm_lockspace_t *lockspace, u64 number, struct file *file,
241 struct file_lock *fl)
242 {
243 struct dlm_ls *ls;
244 struct plock_op *op;
245 int rv;
246 unsigned char fl_flags = fl->fl_flags;
247
248 ls = dlm_find_lockspace_local(lockspace);
249 if (!ls)
250 return -EINVAL;
251
252 op = kzalloc(sizeof(*op), GFP_NOFS);
253 if (!op) {
254 rv = -ENOMEM;
255 goto out;
256 }
257
258 /* cause the vfs unlock to return ENOENT if lock is not found */
259 fl->fl_flags |= FL_EXISTS;
260
261 rv = locks_lock_file_wait(file, fl);
262 if (rv == -ENOENT) {
263 rv = 0;
264 goto out_free;
265 }
266 if (rv < 0) {
267 log_error(ls, "dlm_posix_unlock: vfs unlock error %d %llx",
268 rv, (unsigned long long)number);
269 }
270
271 op->info.optype = DLM_PLOCK_OP_UNLOCK;
272 op->info.pid = fl->fl_pid;
273 op->info.fsid = ls->ls_global_id;
274 op->info.number = number;
275 op->info.start = fl->fl_start;
276 op->info.end = fl->fl_end;
277 if (fl->fl_lmops && fl->fl_lmops->lm_grant)
278 op->info.owner = (__u64) fl->fl_pid;
279 else
280 op->info.owner = (__u64)(long) fl->fl_owner;
281
282 if (fl->fl_flags & FL_CLOSE) {
283 op->info.flags |= DLM_PLOCK_FL_CLOSE;
284 send_op(op);
285 rv = 0;
286 goto out;
287 }
288
289 send_op(op);
290 wait_event(recv_wq, (op->done != 0));
291
292 spin_lock(&ops_lock);
293 if (!list_empty(&op->list)) {
294 log_error(ls, "dlm_posix_unlock: op on list %llx",
295 (unsigned long long)number);
296 list_del(&op->list);
297 }
298 spin_unlock(&ops_lock);
299
300 rv = op->info.rv;
301
302 if (rv == -ENOENT)
303 rv = 0;
304
305 out_free:
306 kfree(op);
307 out:
308 dlm_put_lockspace(ls);
309 fl->fl_flags = fl_flags;
310 return rv;
311 }
312 EXPORT_SYMBOL_GPL(dlm_posix_unlock);
313
dlm_posix_get(dlm_lockspace_t * lockspace,u64 number,struct file * file,struct file_lock * fl)314 int dlm_posix_get(dlm_lockspace_t *lockspace, u64 number, struct file *file,
315 struct file_lock *fl)
316 {
317 struct dlm_ls *ls;
318 struct plock_op *op;
319 int rv;
320
321 ls = dlm_find_lockspace_local(lockspace);
322 if (!ls)
323 return -EINVAL;
324
325 op = kzalloc(sizeof(*op), GFP_NOFS);
326 if (!op) {
327 rv = -ENOMEM;
328 goto out;
329 }
330
331 op->info.optype = DLM_PLOCK_OP_GET;
332 op->info.pid = fl->fl_pid;
333 op->info.ex = (fl->fl_type == F_WRLCK);
334 op->info.fsid = ls->ls_global_id;
335 op->info.number = number;
336 op->info.start = fl->fl_start;
337 op->info.end = fl->fl_end;
338 if (fl->fl_lmops && fl->fl_lmops->lm_grant)
339 op->info.owner = (__u64) fl->fl_pid;
340 else
341 op->info.owner = (__u64)(long) fl->fl_owner;
342
343 send_op(op);
344 wait_event(recv_wq, (op->done != 0));
345
346 spin_lock(&ops_lock);
347 if (!list_empty(&op->list)) {
348 log_error(ls, "dlm_posix_get: op on list %llx",
349 (unsigned long long)number);
350 list_del(&op->list);
351 }
352 spin_unlock(&ops_lock);
353
354 /* info.rv from userspace is 1 for conflict, 0 for no-conflict,
355 -ENOENT if there are no locks on the file */
356
357 rv = op->info.rv;
358
359 fl->fl_type = F_UNLCK;
360 if (rv == -ENOENT)
361 rv = 0;
362 else if (rv > 0) {
363 locks_init_lock(fl);
364 fl->fl_type = (op->info.ex) ? F_WRLCK : F_RDLCK;
365 fl->fl_flags = FL_POSIX;
366 fl->fl_pid = -op->info.pid;
367 fl->fl_start = op->info.start;
368 fl->fl_end = op->info.end;
369 rv = 0;
370 }
371
372 kfree(op);
373 out:
374 dlm_put_lockspace(ls);
375 return rv;
376 }
377 EXPORT_SYMBOL_GPL(dlm_posix_get);
378
379 /* a read copies out one plock request from the send list */
dev_read(struct file * file,char __user * u,size_t count,loff_t * ppos)380 static ssize_t dev_read(struct file *file, char __user *u, size_t count,
381 loff_t *ppos)
382 {
383 struct dlm_plock_info info;
384 struct plock_op *op = NULL;
385
386 if (count < sizeof(info))
387 return -EINVAL;
388
389 spin_lock(&ops_lock);
390 if (!list_empty(&send_list)) {
391 op = list_entry(send_list.next, struct plock_op, list);
392 if (op->info.flags & DLM_PLOCK_FL_CLOSE)
393 list_del(&op->list);
394 else
395 list_move(&op->list, &recv_list);
396 memcpy(&info, &op->info, sizeof(info));
397 }
398 spin_unlock(&ops_lock);
399
400 if (!op)
401 return -EAGAIN;
402
403 /* there is no need to get a reply from userspace for unlocks
404 that were generated by the vfs cleaning up for a close
405 (the process did not make an unlock call). */
406
407 if (op->info.flags & DLM_PLOCK_FL_CLOSE)
408 kfree(op);
409
410 if (copy_to_user(u, &info, sizeof(info)))
411 return -EFAULT;
412 return sizeof(info);
413 }
414
415 /* a write copies in one plock result that should match a plock_op
416 on the recv list */
dev_write(struct file * file,const char __user * u,size_t count,loff_t * ppos)417 static ssize_t dev_write(struct file *file, const char __user *u, size_t count,
418 loff_t *ppos)
419 {
420 struct dlm_plock_info info;
421 struct plock_op *op;
422 int found = 0, do_callback = 0;
423
424 if (count != sizeof(info))
425 return -EINVAL;
426
427 if (copy_from_user(&info, u, sizeof(info)))
428 return -EFAULT;
429
430 if (check_version(&info))
431 return -EINVAL;
432
433 spin_lock(&ops_lock);
434 list_for_each_entry(op, &recv_list, list) {
435 if (op->info.fsid == info.fsid &&
436 op->info.number == info.number &&
437 op->info.owner == info.owner) {
438 list_del_init(&op->list);
439 memcpy(&op->info, &info, sizeof(info));
440 if (op->callback)
441 do_callback = 1;
442 else
443 op->done = 1;
444 found = 1;
445 break;
446 }
447 }
448 spin_unlock(&ops_lock);
449
450 if (found) {
451 if (do_callback)
452 dlm_plock_callback(op);
453 else
454 wake_up(&recv_wq);
455 } else
456 log_print("dev_write no op %x %llx", info.fsid,
457 (unsigned long long)info.number);
458 return count;
459 }
460
dev_poll(struct file * file,poll_table * wait)461 static __poll_t dev_poll(struct file *file, poll_table *wait)
462 {
463 __poll_t mask = 0;
464
465 poll_wait(file, &send_wq, wait);
466
467 spin_lock(&ops_lock);
468 if (!list_empty(&send_list))
469 mask = EPOLLIN | EPOLLRDNORM;
470 spin_unlock(&ops_lock);
471
472 return mask;
473 }
474
475 static const struct file_operations dev_fops = {
476 .read = dev_read,
477 .write = dev_write,
478 .poll = dev_poll,
479 .owner = THIS_MODULE,
480 .llseek = noop_llseek,
481 };
482
483 static struct miscdevice plock_dev_misc = {
484 .minor = MISC_DYNAMIC_MINOR,
485 .name = DLM_PLOCK_MISC_NAME,
486 .fops = &dev_fops
487 };
488
dlm_plock_init(void)489 int dlm_plock_init(void)
490 {
491 int rv;
492
493 spin_lock_init(&ops_lock);
494 INIT_LIST_HEAD(&send_list);
495 INIT_LIST_HEAD(&recv_list);
496 init_waitqueue_head(&send_wq);
497 init_waitqueue_head(&recv_wq);
498
499 rv = misc_register(&plock_dev_misc);
500 if (rv)
501 log_print("dlm_plock_init: misc_register failed %d", rv);
502 return rv;
503 }
504
dlm_plock_exit(void)505 void dlm_plock_exit(void)
506 {
507 misc_deregister(&plock_dev_misc);
508 }
509
510