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 "dlm_internal.h"
15 #include "lockspace.h"
17 static DEFINE_SPINLOCK(ops_lock);
18 static LIST_HEAD(send_list);
19 static LIST_HEAD(recv_list);
20 static DECLARE_WAIT_QUEUE_HEAD(send_wq);
21 static DECLARE_WAIT_QUEUE_HEAD(recv_wq);
23 struct plock_async_data {
27 int (*callback)(struct file_lock *fl, int result);
31 struct list_head list;
33 /* if lock op got interrupted while waiting dlm_controld reply */
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 int check_version(struct dlm_plock_info *info)
49 if ((DLM_PLOCK_VERSION_MAJOR != info->version[0]) ||
50 (DLM_PLOCK_VERSION_MINOR < info->version[1])) {
51 log_print("plock device version mismatch: "
52 "kernel (%u.%u.%u), user (%u.%u.%u)",
53 DLM_PLOCK_VERSION_MAJOR,
54 DLM_PLOCK_VERSION_MINOR,
55 DLM_PLOCK_VERSION_PATCH,
64 static void dlm_release_plock_op(struct plock_op *op)
70 static void send_op(struct plock_op *op)
72 set_version(&op->info);
74 list_add_tail(&op->list, &send_list);
75 spin_unlock(&ops_lock);
79 /* If a process was killed while waiting for the only plock on a file,
80 locks_remove_posix will not see any lock on the file so it won't
81 send an unlock-close to us to pass on to userspace to clean up the
82 abandoned waiter. So, we have to insert the unlock-close when the
83 lock call is interrupted. */
85 static void do_unlock_close(const struct dlm_plock_info *info)
89 op = kzalloc(sizeof(*op), GFP_NOFS);
93 op->info.optype = DLM_PLOCK_OP_UNLOCK;
94 op->info.pid = info->pid;
95 op->info.fsid = info->fsid;
96 op->info.number = info->number;
98 op->info.end = OFFSET_MAX;
99 op->info.owner = info->owner;
101 op->info.flags |= DLM_PLOCK_FL_CLOSE;
105 int dlm_posix_lock(dlm_lockspace_t *lockspace, u64 number, struct file *file,
106 int cmd, struct file_lock *fl)
108 struct plock_async_data *op_data;
113 ls = dlm_find_lockspace_local(lockspace);
117 op = kzalloc(sizeof(*op), GFP_NOFS);
123 op->info.optype = DLM_PLOCK_OP_LOCK;
124 op->info.pid = fl->fl_pid;
125 op->info.ex = (fl->fl_type == F_WRLCK);
126 op->info.wait = IS_SETLKW(cmd);
127 op->info.fsid = ls->ls_global_id;
128 op->info.number = number;
129 op->info.start = fl->fl_start;
130 op->info.end = fl->fl_end;
132 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
133 op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
135 dlm_release_plock_op(op);
140 /* fl_owner is lockd which doesn't distinguish
141 processes on the nfs client */
142 op->info.owner = (__u64) fl->fl_pid;
143 op_data->callback = fl->fl_lmops->lm_grant;
144 locks_init_lock(&op_data->flc);
145 locks_copy_lock(&op_data->flc, fl);
147 op_data->file = file;
152 rv = FILE_LOCK_DEFERRED;
155 op->info.owner = (__u64)(long) fl->fl_owner;
160 rv = wait_event_interruptible(recv_wq, (op->done != 0));
161 if (rv == -ERESTARTSYS) {
162 spin_lock(&ops_lock);
163 /* recheck under ops_lock if we got a done != 0,
164 * if so this interrupt case should be ignored
167 spin_unlock(&ops_lock);
172 spin_unlock(&ops_lock);
173 log_debug(ls, "%s: wait interrupted %x %llx pid %d",
174 __func__, ls->ls_global_id,
175 (unsigned long long)number, op->info.pid);
181 WARN_ON(!list_empty(&op->list));
186 if (locks_lock_file_wait(file, fl) < 0)
187 log_error(ls, "dlm_posix_lock: vfs lock error %llx",
188 (unsigned long long)number);
191 dlm_release_plock_op(op);
193 dlm_put_lockspace(ls);
196 EXPORT_SYMBOL_GPL(dlm_posix_lock);
198 /* Returns failure iff a successful lock operation should be canceled */
199 static int dlm_plock_callback(struct plock_op *op)
201 struct plock_async_data *op_data = op->data;
203 struct file_lock *fl;
204 struct file_lock *flc;
205 int (*notify)(struct file_lock *fl, int result) = NULL;
208 WARN_ON(!list_empty(&op->list));
210 /* check if the following 2 are still valid or make a copy */
211 file = op_data->file;
214 notify = op_data->callback;
217 notify(fl, op->info.rv);
221 /* got fs lock; bookkeep locally as well: */
222 flc->fl_flags &= ~FL_SLEEP;
223 if (posix_lock_file(file, flc, NULL)) {
225 * This can only happen in the case of kmalloc() failure.
226 * The filesystem's own lock is the authoritative lock,
227 * so a failure to get the lock locally is not a disaster.
228 * As long as the fs cannot reliably cancel locks (especially
229 * in a low-memory situation), we're better off ignoring
230 * this failure than trying to recover.
232 log_print("dlm_plock_callback: vfs lock error %llx file %p fl %p",
233 (unsigned long long)op->info.number, file, fl);
238 /* XXX: We need to cancel the fs lock here: */
239 log_print("dlm_plock_callback: lock granted after lock request "
240 "failed; dangling lock!\n");
245 dlm_release_plock_op(op);
249 int dlm_posix_unlock(dlm_lockspace_t *lockspace, u64 number, struct file *file,
250 struct file_lock *fl)
255 unsigned char fl_flags = fl->fl_flags;
257 ls = dlm_find_lockspace_local(lockspace);
261 op = kzalloc(sizeof(*op), GFP_NOFS);
267 /* cause the vfs unlock to return ENOENT if lock is not found */
268 fl->fl_flags |= FL_EXISTS;
270 rv = locks_lock_file_wait(file, fl);
276 log_error(ls, "dlm_posix_unlock: vfs unlock error %d %llx",
277 rv, (unsigned long long)number);
280 op->info.optype = DLM_PLOCK_OP_UNLOCK;
281 op->info.pid = fl->fl_pid;
282 op->info.fsid = ls->ls_global_id;
283 op->info.number = number;
284 op->info.start = fl->fl_start;
285 op->info.end = fl->fl_end;
286 if (fl->fl_lmops && fl->fl_lmops->lm_grant)
287 op->info.owner = (__u64) fl->fl_pid;
289 op->info.owner = (__u64)(long) fl->fl_owner;
291 if (fl->fl_flags & FL_CLOSE) {
292 op->info.flags |= DLM_PLOCK_FL_CLOSE;
299 wait_event(recv_wq, (op->done != 0));
301 WARN_ON(!list_empty(&op->list));
309 dlm_release_plock_op(op);
311 dlm_put_lockspace(ls);
312 fl->fl_flags = fl_flags;
315 EXPORT_SYMBOL_GPL(dlm_posix_unlock);
317 int dlm_posix_get(dlm_lockspace_t *lockspace, u64 number, struct file *file,
318 struct file_lock *fl)
324 ls = dlm_find_lockspace_local(lockspace);
328 op = kzalloc(sizeof(*op), GFP_NOFS);
334 op->info.optype = DLM_PLOCK_OP_GET;
335 op->info.pid = fl->fl_pid;
336 op->info.ex = (fl->fl_type == F_WRLCK);
337 op->info.fsid = ls->ls_global_id;
338 op->info.number = number;
339 op->info.start = fl->fl_start;
340 op->info.end = fl->fl_end;
341 if (fl->fl_lmops && fl->fl_lmops->lm_grant)
342 op->info.owner = (__u64) fl->fl_pid;
344 op->info.owner = (__u64)(long) fl->fl_owner;
347 wait_event(recv_wq, (op->done != 0));
349 WARN_ON(!list_empty(&op->list));
351 /* info.rv from userspace is 1 for conflict, 0 for no-conflict,
352 -ENOENT if there are no locks on the file */
356 fl->fl_type = F_UNLCK;
361 fl->fl_type = (op->info.ex) ? F_WRLCK : F_RDLCK;
362 fl->fl_flags = FL_POSIX;
363 fl->fl_pid = -op->info.pid;
364 fl->fl_start = op->info.start;
365 fl->fl_end = op->info.end;
369 dlm_release_plock_op(op);
371 dlm_put_lockspace(ls);
374 EXPORT_SYMBOL_GPL(dlm_posix_get);
376 /* a read copies out one plock request from the send list */
377 static ssize_t dev_read(struct file *file, char __user *u, size_t count,
380 struct dlm_plock_info info;
381 struct plock_op *op = NULL;
383 if (count < sizeof(info))
386 spin_lock(&ops_lock);
387 if (!list_empty(&send_list)) {
388 op = list_first_entry(&send_list, struct plock_op, list);
389 if (op->info.flags & DLM_PLOCK_FL_CLOSE)
392 list_move(&op->list, &recv_list);
393 memcpy(&info, &op->info, sizeof(info));
395 spin_unlock(&ops_lock);
400 /* there is no need to get a reply from userspace for unlocks
401 that were generated by the vfs cleaning up for a close
402 (the process did not make an unlock call). */
404 if (op->info.flags & DLM_PLOCK_FL_CLOSE)
405 dlm_release_plock_op(op);
407 if (copy_to_user(u, &info, sizeof(info)))
412 /* a write copies in one plock result that should match a plock_op
414 static ssize_t dev_write(struct file *file, const char __user *u, size_t count,
417 struct plock_op *op = NULL, *iter;
418 struct dlm_plock_info info;
421 if (count != sizeof(info))
424 if (copy_from_user(&info, u, sizeof(info)))
427 if (check_version(&info))
430 spin_lock(&ops_lock);
431 list_for_each_entry(iter, &recv_list, list) {
432 if (iter->info.fsid == info.fsid &&
433 iter->info.number == info.number &&
434 iter->info.owner == info.owner) {
436 list_del(&iter->list);
437 spin_unlock(&ops_lock);
439 pr_debug("%s: sigint cleanup %x %llx pid %d",
440 __func__, iter->info.fsid,
441 (unsigned long long)iter->info.number,
443 do_unlock_close(&iter->info);
444 memcpy(&iter->info, &info, sizeof(info));
445 dlm_release_plock_op(iter);
448 list_del_init(&iter->list);
449 memcpy(&iter->info, &info, sizeof(info));
458 spin_unlock(&ops_lock);
462 dlm_plock_callback(op);
466 log_print("%s: no op %x %llx", __func__,
467 info.fsid, (unsigned long long)info.number);
471 static __poll_t dev_poll(struct file *file, poll_table *wait)
475 poll_wait(file, &send_wq, wait);
477 spin_lock(&ops_lock);
478 if (!list_empty(&send_list))
479 mask = EPOLLIN | EPOLLRDNORM;
480 spin_unlock(&ops_lock);
485 static const struct file_operations dev_fops = {
489 .owner = THIS_MODULE,
490 .llseek = noop_llseek,
493 static struct miscdevice plock_dev_misc = {
494 .minor = MISC_DYNAMIC_MINOR,
495 .name = DLM_PLOCK_MISC_NAME,
499 int dlm_plock_init(void)
503 rv = misc_register(&plock_dev_misc);
505 log_print("dlm_plock_init: misc_register failed %d", rv);
509 void dlm_plock_exit(void)
511 misc_deregister(&plock_dev_misc);