1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2005-2008 Red Hat, Inc. All rights reserved.
7 #include <linux/miscdevice.h>
8 #include <linux/poll.h>
10 #include <linux/dlm_plock.h>
11 #include <linux/slab.h>
13 #include "dlm_internal.h"
14 #include "lockspace.h"
16 static DEFINE_SPINLOCK(ops_lock);
17 static LIST_HEAD(send_list);
18 static LIST_HEAD(recv_list);
19 static DECLARE_WAIT_QUEUE_HEAD(send_wq);
20 static DECLARE_WAIT_QUEUE_HEAD(recv_wq);
22 struct plock_async_data {
26 int (*callback)(struct file_lock *fl, int result);
30 struct list_head list;
32 struct dlm_plock_info info;
33 /* if set indicates async handling */
34 struct plock_async_data *data;
37 static inline void set_version(struct dlm_plock_info *info)
39 info->version[0] = DLM_PLOCK_VERSION_MAJOR;
40 info->version[1] = DLM_PLOCK_VERSION_MINOR;
41 info->version[2] = DLM_PLOCK_VERSION_PATCH;
44 static int check_version(struct dlm_plock_info *info)
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,
61 static void dlm_release_plock_op(struct plock_op *op)
67 static void send_op(struct plock_op *op)
69 set_version(&op->info);
71 list_add_tail(&op->list, &send_list);
72 spin_unlock(&ops_lock);
76 /* If a process was killed while waiting for the only plock on a file,
77 locks_remove_posix will not see any lock on the file so it won't
78 send an unlock-close to us to pass on to userspace to clean up the
79 abandoned waiter. So, we have to insert the unlock-close when the
80 lock call is interrupted. */
82 static void do_unlock_close(struct dlm_ls *ls, u64 number,
83 struct file *file, struct file_lock *fl)
87 op = kzalloc(sizeof(*op), GFP_NOFS);
91 op->info.optype = DLM_PLOCK_OP_UNLOCK;
92 op->info.pid = fl->fl_pid;
93 op->info.fsid = ls->ls_global_id;
94 op->info.number = number;
96 op->info.end = OFFSET_MAX;
97 if (fl->fl_lmops && fl->fl_lmops->lm_grant)
98 op->info.owner = (__u64) fl->fl_pid;
100 op->info.owner = (__u64)(long) fl->fl_owner;
102 op->info.flags |= DLM_PLOCK_FL_CLOSE;
106 int dlm_posix_lock(dlm_lockspace_t *lockspace, u64 number, struct file *file,
107 int cmd, struct file_lock *fl)
109 struct plock_async_data *op_data;
114 ls = dlm_find_lockspace_local(lockspace);
118 op = kzalloc(sizeof(*op), GFP_NOFS);
124 op->info.optype = DLM_PLOCK_OP_LOCK;
125 op->info.pid = fl->fl_pid;
126 op->info.ex = (fl->fl_type == F_WRLCK);
127 op->info.wait = IS_SETLKW(cmd);
128 op->info.fsid = ls->ls_global_id;
129 op->info.number = number;
130 op->info.start = fl->fl_start;
131 op->info.end = fl->fl_end;
133 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
134 op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
136 dlm_release_plock_op(op);
141 /* fl_owner is lockd which doesn't distinguish
142 processes on the nfs client */
143 op->info.owner = (__u64) fl->fl_pid;
144 op_data->callback = fl->fl_lmops->lm_grant;
145 locks_init_lock(&op_data->flc);
146 locks_copy_lock(&op_data->flc, fl);
148 op_data->file = file;
153 rv = FILE_LOCK_DEFERRED;
156 op->info.owner = (__u64)(long) fl->fl_owner;
161 rv = wait_event_interruptible(recv_wq, (op->done != 0));
162 if (rv == -ERESTARTSYS) {
163 spin_lock(&ops_lock);
165 spin_unlock(&ops_lock);
166 log_print("%s: wait interrupted %x %llx, op removed",
167 __func__, ls->ls_global_id,
168 (unsigned long long)number);
169 dlm_release_plock_op(op);
170 do_unlock_close(ls, number, file, fl);
174 WARN_ON(!list_empty(&op->list));
179 if (locks_lock_file_wait(file, fl) < 0)
180 log_error(ls, "dlm_posix_lock: vfs lock error %llx",
181 (unsigned long long)number);
184 dlm_release_plock_op(op);
186 dlm_put_lockspace(ls);
189 EXPORT_SYMBOL_GPL(dlm_posix_lock);
191 /* Returns failure iff a successful lock operation should be canceled */
192 static int dlm_plock_callback(struct plock_op *op)
194 struct plock_async_data *op_data = op->data;
196 struct file_lock *fl;
197 struct file_lock *flc;
198 int (*notify)(struct file_lock *fl, int result) = NULL;
201 WARN_ON(!list_empty(&op->list));
203 /* check if the following 2 are still valid or make a copy */
204 file = op_data->file;
207 notify = op_data->callback;
210 notify(fl, op->info.rv);
214 /* got fs lock; bookkeep locally as well: */
215 flc->fl_flags &= ~FL_SLEEP;
216 if (posix_lock_file(file, flc, NULL)) {
218 * This can only happen in the case of kmalloc() failure.
219 * The filesystem's own lock is the authoritative lock,
220 * so a failure to get the lock locally is not a disaster.
221 * As long as the fs cannot reliably cancel locks (especially
222 * in a low-memory situation), we're better off ignoring
223 * this failure than trying to recover.
225 log_print("dlm_plock_callback: vfs lock error %llx file %p fl %p",
226 (unsigned long long)op->info.number, file, fl);
231 /* XXX: We need to cancel the fs lock here: */
232 log_print("dlm_plock_callback: lock granted after lock request "
233 "failed; dangling lock!\n");
238 dlm_release_plock_op(op);
242 int dlm_posix_unlock(dlm_lockspace_t *lockspace, u64 number, struct file *file,
243 struct file_lock *fl)
248 unsigned char fl_flags = fl->fl_flags;
250 ls = dlm_find_lockspace_local(lockspace);
254 op = kzalloc(sizeof(*op), GFP_NOFS);
260 /* cause the vfs unlock to return ENOENT if lock is not found */
261 fl->fl_flags |= FL_EXISTS;
263 rv = locks_lock_file_wait(file, fl);
269 log_error(ls, "dlm_posix_unlock: vfs unlock error %d %llx",
270 rv, (unsigned long long)number);
273 op->info.optype = DLM_PLOCK_OP_UNLOCK;
274 op->info.pid = fl->fl_pid;
275 op->info.fsid = ls->ls_global_id;
276 op->info.number = number;
277 op->info.start = fl->fl_start;
278 op->info.end = fl->fl_end;
279 if (fl->fl_lmops && fl->fl_lmops->lm_grant)
280 op->info.owner = (__u64) fl->fl_pid;
282 op->info.owner = (__u64)(long) fl->fl_owner;
284 if (fl->fl_flags & FL_CLOSE) {
285 op->info.flags |= DLM_PLOCK_FL_CLOSE;
292 wait_event(recv_wq, (op->done != 0));
294 WARN_ON(!list_empty(&op->list));
302 dlm_release_plock_op(op);
304 dlm_put_lockspace(ls);
305 fl->fl_flags = fl_flags;
308 EXPORT_SYMBOL_GPL(dlm_posix_unlock);
310 int dlm_posix_get(dlm_lockspace_t *lockspace, u64 number, struct file *file,
311 struct file_lock *fl)
317 ls = dlm_find_lockspace_local(lockspace);
321 op = kzalloc(sizeof(*op), GFP_NOFS);
327 op->info.optype = DLM_PLOCK_OP_GET;
328 op->info.pid = fl->fl_pid;
329 op->info.ex = (fl->fl_type == F_WRLCK);
330 op->info.fsid = ls->ls_global_id;
331 op->info.number = number;
332 op->info.start = fl->fl_start;
333 op->info.end = fl->fl_end;
334 if (fl->fl_lmops && fl->fl_lmops->lm_grant)
335 op->info.owner = (__u64) fl->fl_pid;
337 op->info.owner = (__u64)(long) fl->fl_owner;
340 wait_event(recv_wq, (op->done != 0));
342 WARN_ON(!list_empty(&op->list));
344 /* info.rv from userspace is 1 for conflict, 0 for no-conflict,
345 -ENOENT if there are no locks on the file */
349 fl->fl_type = F_UNLCK;
354 fl->fl_type = (op->info.ex) ? F_WRLCK : F_RDLCK;
355 fl->fl_flags = FL_POSIX;
356 fl->fl_pid = -op->info.pid;
357 fl->fl_start = op->info.start;
358 fl->fl_end = op->info.end;
362 dlm_release_plock_op(op);
364 dlm_put_lockspace(ls);
367 EXPORT_SYMBOL_GPL(dlm_posix_get);
369 /* a read copies out one plock request from the send list */
370 static ssize_t dev_read(struct file *file, char __user *u, size_t count,
373 struct dlm_plock_info info;
374 struct plock_op *op = NULL;
376 if (count < sizeof(info))
379 spin_lock(&ops_lock);
380 if (!list_empty(&send_list)) {
381 op = list_entry(send_list.next, struct plock_op, list);
382 if (op->info.flags & DLM_PLOCK_FL_CLOSE)
385 list_move(&op->list, &recv_list);
386 memcpy(&info, &op->info, sizeof(info));
388 spin_unlock(&ops_lock);
393 /* there is no need to get a reply from userspace for unlocks
394 that were generated by the vfs cleaning up for a close
395 (the process did not make an unlock call). */
397 if (op->info.flags & DLM_PLOCK_FL_CLOSE)
398 dlm_release_plock_op(op);
400 if (copy_to_user(u, &info, sizeof(info)))
405 /* a write copies in one plock result that should match a plock_op
407 static ssize_t dev_write(struct file *file, const char __user *u, size_t count,
410 struct plock_op *op = NULL, *iter;
411 struct dlm_plock_info info;
414 if (count != sizeof(info))
417 if (copy_from_user(&info, u, sizeof(info)))
420 if (check_version(&info))
423 spin_lock(&ops_lock);
424 list_for_each_entry(iter, &recv_list, list) {
425 if (iter->info.fsid == info.fsid &&
426 iter->info.number == info.number &&
427 iter->info.owner == info.owner) {
428 list_del_init(&iter->list);
429 memcpy(&iter->info, &info, sizeof(info));
438 spin_unlock(&ops_lock);
442 dlm_plock_callback(op);
446 log_print("%s: no op %x %llx - may got interrupted?", __func__,
447 info.fsid, (unsigned long long)info.number);
451 static __poll_t dev_poll(struct file *file, poll_table *wait)
455 poll_wait(file, &send_wq, wait);
457 spin_lock(&ops_lock);
458 if (!list_empty(&send_list))
459 mask = EPOLLIN | EPOLLRDNORM;
460 spin_unlock(&ops_lock);
465 static const struct file_operations dev_fops = {
469 .owner = THIS_MODULE,
470 .llseek = noop_llseek,
473 static struct miscdevice plock_dev_misc = {
474 .minor = MISC_DYNAMIC_MINOR,
475 .name = DLM_PLOCK_MISC_NAME,
479 int dlm_plock_init(void)
483 rv = misc_register(&plock_dev_misc);
485 log_print("dlm_plock_init: misc_register failed %d", rv);
489 void dlm_plock_exit(void)
491 misc_deregister(&plock_dev_misc);