1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2005-2008 Red Hat, Inc. All rights reserved.
7 #include <linux/filelock.h>
8 #include <linux/miscdevice.h>
9 #include <linux/poll.h>
10 #include <linux/dlm.h>
11 #include <linux/dlm_plock.h>
12 #include <linux/slab.h>
14 #include <trace/events/dlm.h>
16 #include "dlm_internal.h"
17 #include "lockspace.h"
19 static DEFINE_SPINLOCK(ops_lock);
20 static LIST_HEAD(send_list);
21 static LIST_HEAD(recv_list);
22 static DECLARE_WAIT_QUEUE_HEAD(send_wq);
23 static DECLARE_WAIT_QUEUE_HEAD(recv_wq);
25 struct plock_async_data {
29 int (*callback)(struct file_lock *fl, int result);
33 struct list_head list;
35 struct dlm_plock_info info;
36 /* if set indicates async handling */
37 struct plock_async_data *data;
40 static inline void set_version(struct dlm_plock_info *info)
42 info->version[0] = DLM_PLOCK_VERSION_MAJOR;
43 info->version[1] = DLM_PLOCK_VERSION_MINOR;
44 info->version[2] = DLM_PLOCK_VERSION_PATCH;
47 static struct plock_op *plock_lookup_waiter(const struct dlm_plock_info *info)
49 struct plock_op *op = NULL, *iter;
51 list_for_each_entry(iter, &recv_list, list) {
52 if (iter->info.fsid == info->fsid &&
53 iter->info.number == info->number &&
54 iter->info.owner == info->owner &&
55 iter->info.pid == info->pid &&
56 iter->info.start == info->start &&
57 iter->info.end == info->end &&
58 iter->info.ex == info->ex &&
68 static int check_version(struct dlm_plock_info *info)
70 if ((DLM_PLOCK_VERSION_MAJOR != info->version[0]) ||
71 (DLM_PLOCK_VERSION_MINOR < info->version[1])) {
72 log_print("plock device version mismatch: "
73 "kernel (%u.%u.%u), user (%u.%u.%u)",
74 DLM_PLOCK_VERSION_MAJOR,
75 DLM_PLOCK_VERSION_MINOR,
76 DLM_PLOCK_VERSION_PATCH,
85 static void dlm_release_plock_op(struct plock_op *op)
91 static void send_op(struct plock_op *op)
93 set_version(&op->info);
95 list_add_tail(&op->list, &send_list);
96 spin_unlock(&ops_lock);
100 static int do_lock_cancel(const struct dlm_plock_info *orig_info)
105 op = kzalloc(sizeof(*op), GFP_NOFS);
109 op->info = *orig_info;
110 op->info.optype = DLM_PLOCK_OP_CANCEL;
114 wait_event(recv_wq, (op->done != 0));
118 dlm_release_plock_op(op);
122 int dlm_posix_lock(dlm_lockspace_t *lockspace, u64 number, struct file *file,
123 int cmd, struct file_lock *fl)
125 struct plock_async_data *op_data;
130 ls = dlm_find_lockspace_local(lockspace);
134 op = kzalloc(sizeof(*op), GFP_NOFS);
140 op->info.optype = DLM_PLOCK_OP_LOCK;
141 op->info.pid = fl->fl_pid;
142 op->info.ex = (fl->fl_type == F_WRLCK);
143 op->info.wait = IS_SETLKW(cmd);
144 op->info.fsid = ls->ls_global_id;
145 op->info.number = number;
146 op->info.start = fl->fl_start;
147 op->info.end = fl->fl_end;
149 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
150 op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
152 dlm_release_plock_op(op);
157 /* fl_owner is lockd which doesn't distinguish
158 processes on the nfs client */
159 op->info.owner = (__u64) fl->fl_pid;
160 op_data->callback = fl->fl_lmops->lm_grant;
161 locks_init_lock(&op_data->flc);
162 locks_copy_lock(&op_data->flc, fl);
164 op_data->file = file;
169 rv = FILE_LOCK_DEFERRED;
172 op->info.owner = (__u64)(long) fl->fl_owner;
178 rv = wait_event_interruptible(recv_wq, (op->done != 0));
179 if (rv == -ERESTARTSYS) {
180 spin_lock(&ops_lock);
181 /* recheck under ops_lock if we got a done != 0,
182 * if so this interrupt case should be ignored
185 spin_unlock(&ops_lock);
188 spin_unlock(&ops_lock);
190 rv = do_lock_cancel(&op->info);
193 /* waiter was deleted in user space, answer will never come
194 * remove original request. The original request must be
195 * on recv_list because the answer of do_lock_cancel()
198 spin_lock(&ops_lock);
200 spin_unlock(&ops_lock);
204 /* cancellation wasn't successful but op should be done */
207 /* internal error doing cancel we need to wait */
211 log_debug(ls, "%s: wait interrupted %x %llx pid %d",
212 __func__, ls->ls_global_id,
213 (unsigned long long)number, op->info.pid);
214 dlm_release_plock_op(op);
219 wait_event(recv_wq, (op->done != 0));
224 WARN_ON(!list_empty(&op->list));
229 if (locks_lock_file_wait(file, fl) < 0)
230 log_error(ls, "dlm_posix_lock: vfs lock error %llx",
231 (unsigned long long)number);
234 dlm_release_plock_op(op);
236 dlm_put_lockspace(ls);
239 EXPORT_SYMBOL_GPL(dlm_posix_lock);
241 /* Returns failure iff a successful lock operation should be canceled */
242 static int dlm_plock_callback(struct plock_op *op)
244 struct plock_async_data *op_data = op->data;
246 struct file_lock *fl;
247 struct file_lock *flc;
248 int (*notify)(struct file_lock *fl, int result) = NULL;
251 WARN_ON(!list_empty(&op->list));
253 /* check if the following 2 are still valid or make a copy */
254 file = op_data->file;
257 notify = op_data->callback;
260 notify(fl, op->info.rv);
264 /* got fs lock; bookkeep locally as well: */
265 flc->fl_flags &= ~FL_SLEEP;
266 if (posix_lock_file(file, flc, NULL)) {
268 * This can only happen in the case of kmalloc() failure.
269 * The filesystem's own lock is the authoritative lock,
270 * so a failure to get the lock locally is not a disaster.
271 * As long as the fs cannot reliably cancel locks (especially
272 * in a low-memory situation), we're better off ignoring
273 * this failure than trying to recover.
275 log_print("dlm_plock_callback: vfs lock error %llx file %p fl %p",
276 (unsigned long long)op->info.number, file, fl);
281 /* XXX: We need to cancel the fs lock here: */
282 log_print("%s: lock granted after lock request failed; dangling lock!",
288 dlm_release_plock_op(op);
292 int dlm_posix_unlock(dlm_lockspace_t *lockspace, u64 number, struct file *file,
293 struct file_lock *fl)
298 unsigned char fl_flags = fl->fl_flags;
300 ls = dlm_find_lockspace_local(lockspace);
304 op = kzalloc(sizeof(*op), GFP_NOFS);
310 /* cause the vfs unlock to return ENOENT if lock is not found */
311 fl->fl_flags |= FL_EXISTS;
313 rv = locks_lock_file_wait(file, fl);
319 log_error(ls, "dlm_posix_unlock: vfs unlock error %d %llx",
320 rv, (unsigned long long)number);
323 op->info.optype = DLM_PLOCK_OP_UNLOCK;
324 op->info.pid = fl->fl_pid;
325 op->info.fsid = ls->ls_global_id;
326 op->info.number = number;
327 op->info.start = fl->fl_start;
328 op->info.end = fl->fl_end;
329 if (fl->fl_lmops && fl->fl_lmops->lm_grant)
330 op->info.owner = (__u64) fl->fl_pid;
332 op->info.owner = (__u64)(long) fl->fl_owner;
334 if (fl->fl_flags & FL_CLOSE) {
335 op->info.flags |= DLM_PLOCK_FL_CLOSE;
342 wait_event(recv_wq, (op->done != 0));
344 WARN_ON(!list_empty(&op->list));
352 dlm_release_plock_op(op);
354 dlm_put_lockspace(ls);
355 fl->fl_flags = fl_flags;
358 EXPORT_SYMBOL_GPL(dlm_posix_unlock);
361 * NOTE: This implementation can only handle async lock requests as nfs
362 * do it. It cannot handle cancellation of a pending lock request sitting
363 * in wait_event(), but for now only nfs is the only user local kernel
366 int dlm_posix_cancel(dlm_lockspace_t *lockspace, u64 number, struct file *file,
367 struct file_lock *fl)
369 struct dlm_plock_info info;
374 /* this only works for async request for now and nfs is the only
375 * kernel user right now.
377 if (WARN_ON_ONCE(!fl->fl_lmops || !fl->fl_lmops->lm_grant))
380 ls = dlm_find_lockspace_local(lockspace);
384 memset(&info, 0, sizeof(info));
385 info.pid = fl->fl_pid;
386 info.ex = (fl->fl_type == F_WRLCK);
387 info.fsid = ls->ls_global_id;
388 dlm_put_lockspace(ls);
389 info.number = number;
390 info.start = fl->fl_start;
391 info.end = fl->fl_end;
392 info.owner = (__u64)fl->fl_pid;
394 rv = do_lock_cancel(&info);
397 spin_lock(&ops_lock);
398 /* lock request to cancel must be on recv_list because
399 * do_lock_cancel() synchronizes it.
401 op = plock_lookup_waiter(&info);
402 if (WARN_ON_ONCE(!op)) {
403 spin_unlock(&ops_lock);
409 spin_unlock(&ops_lock);
410 WARN_ON(op->info.optype != DLM_PLOCK_OP_LOCK);
411 op->data->callback(op->data->fl, -EINTR);
412 dlm_release_plock_op(op);
416 /* if cancel wasn't successful we probably were to late
417 * or it was a non-blocking lock request, so just unlock it.
419 rv = dlm_posix_unlock(lockspace, number, file, fl);
427 EXPORT_SYMBOL_GPL(dlm_posix_cancel);
429 int dlm_posix_get(dlm_lockspace_t *lockspace, u64 number, struct file *file,
430 struct file_lock *fl)
436 ls = dlm_find_lockspace_local(lockspace);
440 op = kzalloc(sizeof(*op), GFP_NOFS);
446 op->info.optype = DLM_PLOCK_OP_GET;
447 op->info.pid = fl->fl_pid;
448 op->info.ex = (fl->fl_type == F_WRLCK);
449 op->info.fsid = ls->ls_global_id;
450 op->info.number = number;
451 op->info.start = fl->fl_start;
452 op->info.end = fl->fl_end;
453 if (fl->fl_lmops && fl->fl_lmops->lm_grant)
454 op->info.owner = (__u64) fl->fl_pid;
456 op->info.owner = (__u64)(long) fl->fl_owner;
459 wait_event(recv_wq, (op->done != 0));
461 WARN_ON(!list_empty(&op->list));
463 /* info.rv from userspace is 1 for conflict, 0 for no-conflict,
464 -ENOENT if there are no locks on the file */
468 fl->fl_type = F_UNLCK;
473 fl->fl_type = (op->info.ex) ? F_WRLCK : F_RDLCK;
474 fl->fl_flags = FL_POSIX;
475 fl->fl_pid = op->info.pid;
476 if (op->info.nodeid != dlm_our_nodeid())
477 fl->fl_pid = -fl->fl_pid;
478 fl->fl_start = op->info.start;
479 fl->fl_end = op->info.end;
483 dlm_release_plock_op(op);
485 dlm_put_lockspace(ls);
488 EXPORT_SYMBOL_GPL(dlm_posix_get);
490 /* a read copies out one plock request from the send list */
491 static ssize_t dev_read(struct file *file, char __user *u, size_t count,
494 struct dlm_plock_info info;
495 struct plock_op *op = NULL;
497 if (count < sizeof(info))
500 spin_lock(&ops_lock);
501 if (!list_empty(&send_list)) {
502 op = list_first_entry(&send_list, struct plock_op, list);
503 if (op->info.flags & DLM_PLOCK_FL_CLOSE)
506 list_move_tail(&op->list, &recv_list);
507 memcpy(&info, &op->info, sizeof(info));
509 spin_unlock(&ops_lock);
514 trace_dlm_plock_read(&info);
516 /* there is no need to get a reply from userspace for unlocks
517 that were generated by the vfs cleaning up for a close
518 (the process did not make an unlock call). */
520 if (op->info.flags & DLM_PLOCK_FL_CLOSE)
521 dlm_release_plock_op(op);
523 if (copy_to_user(u, &info, sizeof(info)))
528 /* a write copies in one plock result that should match a plock_op
530 static ssize_t dev_write(struct file *file, const char __user *u, size_t count,
533 struct plock_op *op = NULL, *iter;
534 struct dlm_plock_info info;
537 if (count != sizeof(info))
540 if (copy_from_user(&info, u, sizeof(info)))
543 trace_dlm_plock_write(&info);
545 if (check_version(&info))
549 * The results for waiting ops (SETLKW) can be returned in any
550 * order, so match all fields to find the op. The results for
551 * non-waiting ops are returned in the order that they were sent
552 * to userspace, so match the result with the first non-waiting op.
554 spin_lock(&ops_lock);
556 op = plock_lookup_waiter(&info);
558 list_for_each_entry(iter, &recv_list, list) {
559 if (!iter->info.wait &&
560 iter->info.fsid == info.fsid) {
568 /* Sanity check that op and info match. */
570 WARN_ON(op->info.optype != DLM_PLOCK_OP_LOCK);
572 WARN_ON(op->info.number != info.number ||
573 op->info.owner != info.owner ||
574 op->info.optype != info.optype);
576 list_del_init(&op->list);
577 memcpy(&op->info, &info, sizeof(info));
583 spin_unlock(&ops_lock);
587 dlm_plock_callback(op);
591 pr_debug("%s: no op %x %llx", __func__,
592 info.fsid, (unsigned long long)info.number);
596 static __poll_t dev_poll(struct file *file, poll_table *wait)
600 poll_wait(file, &send_wq, wait);
602 spin_lock(&ops_lock);
603 if (!list_empty(&send_list))
604 mask = EPOLLIN | EPOLLRDNORM;
605 spin_unlock(&ops_lock);
610 static const struct file_operations dev_fops = {
614 .owner = THIS_MODULE,
615 .llseek = noop_llseek,
618 static struct miscdevice plock_dev_misc = {
619 .minor = MISC_DYNAMIC_MINOR,
620 .name = DLM_PLOCK_MISC_NAME,
624 int dlm_plock_init(void)
628 rv = misc_register(&plock_dev_misc);
630 log_print("dlm_plock_init: misc_register failed %d", rv);
634 void dlm_plock_exit(void)
636 misc_deregister(&plock_dev_misc);
637 WARN_ON(!list_empty(&send_list));
638 WARN_ON(!list_empty(&recv_list));