blob: 16241fe6ac3ce773c4511bec7e3ab390ca4a14a5 [file] [log] [blame]
Thomas Gleixner7336d0e2019-05-31 01:09:56 -07001// SPDX-License-Identifier: GPL-2.0-only
David Teigland869d81d2006-01-17 08:47:12 +00002/*
David Teigland24022112008-03-14 15:09:15 -05003 * Copyright (C) 2005-2008 Red Hat, Inc. All rights reserved.
David Teigland869d81d2006-01-17 08:47:12 +00004 */
5
David Teigland24022112008-03-14 15:09:15 -05006#include <linux/fs.h>
David Teigland869d81d2006-01-17 08:47:12 +00007#include <linux/miscdevice.h>
Al Virobd01f842006-10-19 17:23:57 -04008#include <linux/poll.h>
David Teigland24022112008-03-14 15:09:15 -05009#include <linux/dlm.h>
10#include <linux/dlm_plock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
David Teigland869d81d2006-01-17 08:47:12 +000012
David Teigland24022112008-03-14 15:09:15 -050013#include "dlm_internal.h"
14#include "lockspace.h"
David Teigland869d81d2006-01-17 08:47:12 +000015
Alexander Aring314a5542022-04-04 16:06:36 -040016static DEFINE_SPINLOCK(ops_lock);
17static LIST_HEAD(send_list);
18static LIST_HEAD(recv_list);
19static DECLARE_WAIT_QUEUE_HEAD(send_wq);
20static DECLARE_WAIT_QUEUE_HEAD(recv_wq);
David Teigland869d81d2006-01-17 08:47:12 +000021
Alexander Aringbcbb4ba2022-04-04 16:06:32 -040022struct plock_async_data {
23 void *fl;
24 void *file;
25 struct file_lock flc;
26 int (*callback)(struct file_lock *fl, int result);
27};
28
David Teigland869d81d2006-01-17 08:47:12 +000029struct plock_op {
30 struct list_head list;
31 int done;
David Teigland24022112008-03-14 15:09:15 -050032 struct dlm_plock_info info;
Alexander Aringbcbb4ba2022-04-04 16:06:32 -040033 /* if set indicates async handling */
34 struct plock_async_data *data;
David Teigland869d81d2006-01-17 08:47:12 +000035};
36
David Teigland24022112008-03-14 15:09:15 -050037static inline void set_version(struct dlm_plock_info *info)
David Teigland869d81d2006-01-17 08:47:12 +000038{
David Teigland24022112008-03-14 15:09:15 -050039 info->version[0] = DLM_PLOCK_VERSION_MAJOR;
40 info->version[1] = DLM_PLOCK_VERSION_MINOR;
41 info->version[2] = DLM_PLOCK_VERSION_PATCH;
David Teigland869d81d2006-01-17 08:47:12 +000042}
43
David Teigland24022112008-03-14 15:09:15 -050044static int check_version(struct dlm_plock_info *info)
David Teigland869d81d2006-01-17 08:47:12 +000045{
David Teigland24022112008-03-14 15:09:15 -050046 if ((DLM_PLOCK_VERSION_MAJOR != info->version[0]) ||
47 (DLM_PLOCK_VERSION_MINOR < info->version[1])) {
48 log_print("plock device version mismatch: "
David Teigland869d81d2006-01-17 08:47:12 +000049 "kernel (%u.%u.%u), user (%u.%u.%u)",
David Teigland24022112008-03-14 15:09:15 -050050 DLM_PLOCK_VERSION_MAJOR,
51 DLM_PLOCK_VERSION_MINOR,
52 DLM_PLOCK_VERSION_PATCH,
David Teigland869d81d2006-01-17 08:47:12 +000053 info->version[0],
54 info->version[1],
55 info->version[2]);
56 return -EINVAL;
57 }
58 return 0;
59}
60
Alexander Aringbcbb4ba2022-04-04 16:06:32 -040061static void dlm_release_plock_op(struct plock_op *op)
62{
63 kfree(op->data);
64 kfree(op);
65}
66
David Teigland869d81d2006-01-17 08:47:12 +000067static void send_op(struct plock_op *op)
68{
69 set_version(&op->info);
David Teigland869d81d2006-01-17 08:47:12 +000070 spin_lock(&ops_lock);
71 list_add_tail(&op->list, &send_list);
72 spin_unlock(&ops_lock);
73 wake_up(&send_wq);
74}
75
David Teigland901025d2011-03-02 14:20:04 -060076/* 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. */
81
82static void do_unlock_close(struct dlm_ls *ls, u64 number,
83 struct file *file, struct file_lock *fl)
84{
85 struct plock_op *op;
86
87 op = kzalloc(sizeof(*op), GFP_NOFS);
88 if (!op)
89 return;
90
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;
95 op->info.start = 0;
96 op->info.end = OFFSET_MAX;
J. Bruce Fields8fb47a42011-07-20 20:21:59 -040097 if (fl->fl_lmops && fl->fl_lmops->lm_grant)
David Teigland901025d2011-03-02 14:20:04 -060098 op->info.owner = (__u64) fl->fl_pid;
99 else
100 op->info.owner = (__u64)(long) fl->fl_owner;
101
102 op->info.flags |= DLM_PLOCK_FL_CLOSE;
103 send_op(op);
104}
105
David Teigland24022112008-03-14 15:09:15 -0500106int dlm_posix_lock(dlm_lockspace_t *lockspace, u64 number, struct file *file,
107 int cmd, struct file_lock *fl)
David Teigland869d81d2006-01-17 08:47:12 +0000108{
Alexander Aringbcbb4ba2022-04-04 16:06:32 -0400109 struct plock_async_data *op_data;
David Teigland24022112008-03-14 15:09:15 -0500110 struct dlm_ls *ls;
David Teigland869d81d2006-01-17 08:47:12 +0000111 struct plock_op *op;
112 int rv;
113
David Teigland24022112008-03-14 15:09:15 -0500114 ls = dlm_find_lockspace_local(lockspace);
115 if (!ls)
116 return -EINVAL;
117
Alexander Aringbcbb4ba2022-04-04 16:06:32 -0400118 op = kzalloc(sizeof(*op), GFP_NOFS);
119 if (!op) {
David Teigland24022112008-03-14 15:09:15 -0500120 rv = -ENOMEM;
121 goto out;
122 }
David Teigland869d81d2006-01-17 08:47:12 +0000123
David Teigland24022112008-03-14 15:09:15 -0500124 op->info.optype = DLM_PLOCK_OP_LOCK;
David Teigland3a2a9c92006-04-25 15:45:51 -0400125 op->info.pid = fl->fl_pid;
David Teigland869d81d2006-01-17 08:47:12 +0000126 op->info.ex = (fl->fl_type == F_WRLCK);
127 op->info.wait = IS_SETLKW(cmd);
David Teigland24022112008-03-14 15:09:15 -0500128 op->info.fsid = ls->ls_global_id;
129 op->info.number = number;
David Teigland869d81d2006-01-17 08:47:12 +0000130 op->info.start = fl->fl_start;
131 op->info.end = fl->fl_end;
Alexander Aringbcbb4ba2022-04-04 16:06:32 -0400132 /* async handling */
J. Bruce Fields8fb47a42011-07-20 20:21:59 -0400133 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Alexander Aringbcbb4ba2022-04-04 16:06:32 -0400134 op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
135 if (!op_data) {
136 dlm_release_plock_op(op);
137 rv = -ENOMEM;
138 goto out;
139 }
140
David Teigland2066b582007-12-06 09:35:25 -0600141 /* fl_owner is lockd which doesn't distinguish
142 processes on the nfs client */
143 op->info.owner = (__u64) fl->fl_pid;
Alexander Aringbcbb4ba2022-04-04 16:06:32 -0400144 op_data->callback = fl->fl_lmops->lm_grant;
145 locks_init_lock(&op_data->flc);
146 locks_copy_lock(&op_data->flc, fl);
147 op_data->fl = fl;
148 op_data->file = file;
149
150 op->data = op_data;
Alexander Aringa800ba72022-04-04 16:06:33 -0400151
152 send_op(op);
153 rv = FILE_LOCK_DEFERRED;
154 goto out;
David Teigland2066b582007-12-06 09:35:25 -0600155 } else {
156 op->info.owner = (__u64)(long) fl->fl_owner;
David Teigland2066b582007-12-06 09:35:25 -0600157 }
David Teigland869d81d2006-01-17 08:47:12 +0000158
159 send_op(op);
Marc Eshel586759f2006-11-14 16:37:25 -0500160
Alexander Aringa800ba72022-04-04 16:06:33 -0400161 rv = wait_event_interruptible(recv_wq, (op->done != 0));
162 if (rv == -ERESTARTSYS) {
Alexander Aringa800ba72022-04-04 16:06:33 -0400163 spin_lock(&ops_lock);
164 list_del(&op->list);
165 spin_unlock(&ops_lock);
Alexander Aringbcfad422022-04-04 16:06:34 -0400166 log_print("%s: wait interrupted %x %llx, op removed",
167 __func__, ls->ls_global_id,
168 (unsigned long long)number);
Alexander Aringa800ba72022-04-04 16:06:33 -0400169 dlm_release_plock_op(op);
170 do_unlock_close(ls, number, file, fl);
David Teigland24022112008-03-14 15:09:15 -0500171 goto out;
172 }
David Teigland869d81d2006-01-17 08:47:12 +0000173
Alexander Aringa5597902022-04-04 16:06:31 -0400174 WARN_ON(!list_empty(&op->list));
David Teigland869d81d2006-01-17 08:47:12 +0000175
176 rv = op->info.rv;
177
178 if (!rv) {
Benjamin Coddington4f656362015-10-22 13:38:14 -0400179 if (locks_lock_file_wait(file, fl) < 0)
David Teigland24022112008-03-14 15:09:15 -0500180 log_error(ls, "dlm_posix_lock: vfs lock error %llx",
181 (unsigned long long)number);
David Teigland869d81d2006-01-17 08:47:12 +0000182 }
183
Alexander Aringbcbb4ba2022-04-04 16:06:32 -0400184 dlm_release_plock_op(op);
David Teigland24022112008-03-14 15:09:15 -0500185out:
186 dlm_put_lockspace(ls);
Marc Eshel586759f2006-11-14 16:37:25 -0500187 return rv;
188}
David Teigland24022112008-03-14 15:09:15 -0500189EXPORT_SYMBOL_GPL(dlm_posix_lock);
Marc Eshel586759f2006-11-14 16:37:25 -0500190
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200191/* Returns failure iff a successful lock operation should be canceled */
David Teigland24022112008-03-14 15:09:15 -0500192static int dlm_plock_callback(struct plock_op *op)
Marc Eshel586759f2006-11-14 16:37:25 -0500193{
Alexander Aringbcbb4ba2022-04-04 16:06:32 -0400194 struct plock_async_data *op_data = op->data;
Marc Eshel586759f2006-11-14 16:37:25 -0500195 struct file *file;
196 struct file_lock *fl;
197 struct file_lock *flc;
Joe Perchesd0449b92014-08-22 10:18:42 -0400198 int (*notify)(struct file_lock *fl, int result) = NULL;
Marc Eshel586759f2006-11-14 16:37:25 -0500199 int rv = 0;
200
Alexander Aringa5597902022-04-04 16:06:31 -0400201 WARN_ON(!list_empty(&op->list));
Marc Eshel586759f2006-11-14 16:37:25 -0500202
203 /* check if the following 2 are still valid or make a copy */
Alexander Aringbcbb4ba2022-04-04 16:06:32 -0400204 file = op_data->file;
205 flc = &op_data->flc;
206 fl = op_data->fl;
207 notify = op_data->callback;
Marc Eshel586759f2006-11-14 16:37:25 -0500208
209 if (op->info.rv) {
Joe Perchesd0449b92014-08-22 10:18:42 -0400210 notify(fl, op->info.rv);
Marc Eshel586759f2006-11-14 16:37:25 -0500211 goto out;
212 }
213
214 /* got fs lock; bookkeep locally as well: */
215 flc->fl_flags &= ~FL_SLEEP;
216 if (posix_lock_file(file, flc, NULL)) {
217 /*
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.
David Teigland24022112008-03-14 15:09:15 -0500221 * As long as the fs cannot reliably cancel locks (especially
Marc Eshel586759f2006-11-14 16:37:25 -0500222 * in a low-memory situation), we're better off ignoring
223 * this failure than trying to recover.
224 */
David Teigland24022112008-03-14 15:09:15 -0500225 log_print("dlm_plock_callback: vfs lock error %llx file %p fl %p",
226 (unsigned long long)op->info.number, file, fl);
Marc Eshel586759f2006-11-14 16:37:25 -0500227 }
228
Joe Perchesd0449b92014-08-22 10:18:42 -0400229 rv = notify(fl, 0);
Marc Eshel586759f2006-11-14 16:37:25 -0500230 if (rv) {
231 /* XXX: We need to cancel the fs lock here: */
David Teigland24022112008-03-14 15:09:15 -0500232 log_print("dlm_plock_callback: lock granted after lock request "
233 "failed; dangling lock!\n");
Marc Eshel586759f2006-11-14 16:37:25 -0500234 goto out;
235 }
236
237out:
Alexander Aringbcbb4ba2022-04-04 16:06:32 -0400238 dlm_release_plock_op(op);
David Teigland869d81d2006-01-17 08:47:12 +0000239 return rv;
240}
241
David Teigland24022112008-03-14 15:09:15 -0500242int dlm_posix_unlock(dlm_lockspace_t *lockspace, u64 number, struct file *file,
243 struct file_lock *fl)
David Teigland869d81d2006-01-17 08:47:12 +0000244{
David Teigland24022112008-03-14 15:09:15 -0500245 struct dlm_ls *ls;
David Teigland869d81d2006-01-17 08:47:12 +0000246 struct plock_op *op;
247 int rv;
David Teigland90008312013-04-05 10:57:15 +0100248 unsigned char fl_flags = fl->fl_flags;
David Teigland869d81d2006-01-17 08:47:12 +0000249
David Teigland24022112008-03-14 15:09:15 -0500250 ls = dlm_find_lockspace_local(lockspace);
251 if (!ls)
252 return -EINVAL;
253
David Teigland573c24c2009-11-30 16:34:43 -0600254 op = kzalloc(sizeof(*op), GFP_NOFS);
David Teigland24022112008-03-14 15:09:15 -0500255 if (!op) {
256 rv = -ENOMEM;
257 goto out;
258 }
David Teigland869d81d2006-01-17 08:47:12 +0000259
David Teigland90008312013-04-05 10:57:15 +0100260 /* cause the vfs unlock to return ENOENT if lock is not found */
261 fl->fl_flags |= FL_EXISTS;
262
Benjamin Coddington4f656362015-10-22 13:38:14 -0400263 rv = locks_lock_file_wait(file, fl);
David Teigland90008312013-04-05 10:57:15 +0100264 if (rv == -ENOENT) {
265 rv = 0;
266 goto out_free;
267 }
268 if (rv < 0) {
269 log_error(ls, "dlm_posix_unlock: vfs unlock error %d %llx",
270 rv, (unsigned long long)number);
271 }
David Teigland869d81d2006-01-17 08:47:12 +0000272
David Teigland24022112008-03-14 15:09:15 -0500273 op->info.optype = DLM_PLOCK_OP_UNLOCK;
David Teigland3a2a9c92006-04-25 15:45:51 -0400274 op->info.pid = fl->fl_pid;
David Teigland24022112008-03-14 15:09:15 -0500275 op->info.fsid = ls->ls_global_id;
276 op->info.number = number;
David Teigland869d81d2006-01-17 08:47:12 +0000277 op->info.start = fl->fl_start;
278 op->info.end = fl->fl_end;
J. Bruce Fields8fb47a42011-07-20 20:21:59 -0400279 if (fl->fl_lmops && fl->fl_lmops->lm_grant)
David Teigland2066b582007-12-06 09:35:25 -0600280 op->info.owner = (__u64) fl->fl_pid;
281 else
282 op->info.owner = (__u64)(long) fl->fl_owner;
David Teigland869d81d2006-01-17 08:47:12 +0000283
David Teigland901025d2011-03-02 14:20:04 -0600284 if (fl->fl_flags & FL_CLOSE) {
285 op->info.flags |= DLM_PLOCK_FL_CLOSE;
286 send_op(op);
287 rv = 0;
288 goto out;
289 }
290
David Teigland869d81d2006-01-17 08:47:12 +0000291 send_op(op);
292 wait_event(recv_wq, (op->done != 0));
293
Alexander Aringa5597902022-04-04 16:06:31 -0400294 WARN_ON(!list_empty(&op->list));
David Teigland869d81d2006-01-17 08:47:12 +0000295
296 rv = op->info.rv;
297
Marc Eshel586759f2006-11-14 16:37:25 -0500298 if (rv == -ENOENT)
299 rv = 0;
300
David Teigland90008312013-04-05 10:57:15 +0100301out_free:
Alexander Aringbcbb4ba2022-04-04 16:06:32 -0400302 dlm_release_plock_op(op);
David Teigland24022112008-03-14 15:09:15 -0500303out:
304 dlm_put_lockspace(ls);
David Teigland90008312013-04-05 10:57:15 +0100305 fl->fl_flags = fl_flags;
David Teigland869d81d2006-01-17 08:47:12 +0000306 return rv;
307}
David Teigland24022112008-03-14 15:09:15 -0500308EXPORT_SYMBOL_GPL(dlm_posix_unlock);
David Teigland869d81d2006-01-17 08:47:12 +0000309
David Teigland24022112008-03-14 15:09:15 -0500310int dlm_posix_get(dlm_lockspace_t *lockspace, u64 number, struct file *file,
311 struct file_lock *fl)
David Teigland869d81d2006-01-17 08:47:12 +0000312{
David Teigland24022112008-03-14 15:09:15 -0500313 struct dlm_ls *ls;
David Teigland869d81d2006-01-17 08:47:12 +0000314 struct plock_op *op;
315 int rv;
316
David Teigland24022112008-03-14 15:09:15 -0500317 ls = dlm_find_lockspace_local(lockspace);
318 if (!ls)
319 return -EINVAL;
David Teigland869d81d2006-01-17 08:47:12 +0000320
David Teigland573c24c2009-11-30 16:34:43 -0600321 op = kzalloc(sizeof(*op), GFP_NOFS);
David Teigland24022112008-03-14 15:09:15 -0500322 if (!op) {
323 rv = -ENOMEM;
324 goto out;
325 }
326
327 op->info.optype = DLM_PLOCK_OP_GET;
David Teigland3a2a9c92006-04-25 15:45:51 -0400328 op->info.pid = fl->fl_pid;
David Teigland869d81d2006-01-17 08:47:12 +0000329 op->info.ex = (fl->fl_type == F_WRLCK);
David Teigland24022112008-03-14 15:09:15 -0500330 op->info.fsid = ls->ls_global_id;
331 op->info.number = number;
David Teigland869d81d2006-01-17 08:47:12 +0000332 op->info.start = fl->fl_start;
333 op->info.end = fl->fl_end;
J. Bruce Fields8fb47a42011-07-20 20:21:59 -0400334 if (fl->fl_lmops && fl->fl_lmops->lm_grant)
David Teigland2066b582007-12-06 09:35:25 -0600335 op->info.owner = (__u64) fl->fl_pid;
336 else
337 op->info.owner = (__u64)(long) fl->fl_owner;
Marc Eshel586759f2006-11-14 16:37:25 -0500338
David Teigland869d81d2006-01-17 08:47:12 +0000339 send_op(op);
340 wait_event(recv_wq, (op->done != 0));
341
Alexander Aringa5597902022-04-04 16:06:31 -0400342 WARN_ON(!list_empty(&op->list));
David Teigland869d81d2006-01-17 08:47:12 +0000343
David Teiglanda7a2ff82007-06-08 17:01:40 -0500344 /* info.rv from userspace is 1 for conflict, 0 for no-conflict,
345 -ENOENT if there are no locks on the file */
346
David Teigland869d81d2006-01-17 08:47:12 +0000347 rv = op->info.rv;
348
Marc Eshel586759f2006-11-14 16:37:25 -0500349 fl->fl_type = F_UNLCK;
350 if (rv == -ENOENT)
351 rv = 0;
David Teiglanda7a2ff82007-06-08 17:01:40 -0500352 else if (rv > 0) {
Jeff Layton20d5a392009-01-21 11:34:50 -0500353 locks_init_lock(fl);
David Teigland869d81d2006-01-17 08:47:12 +0000354 fl->fl_type = (op->info.ex) ? F_WRLCK : F_RDLCK;
Jeff Layton20d5a392009-01-21 11:34:50 -0500355 fl->fl_flags = FL_POSIX;
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -0400356 fl->fl_pid = -op->info.pid;
David Teigland869d81d2006-01-17 08:47:12 +0000357 fl->fl_start = op->info.start;
358 fl->fl_end = op->info.end;
David Teiglanda7a2ff82007-06-08 17:01:40 -0500359 rv = 0;
David Teigland869d81d2006-01-17 08:47:12 +0000360 }
361
Alexander Aringbcbb4ba2022-04-04 16:06:32 -0400362 dlm_release_plock_op(op);
David Teigland24022112008-03-14 15:09:15 -0500363out:
364 dlm_put_lockspace(ls);
David Teigland869d81d2006-01-17 08:47:12 +0000365 return rv;
366}
David Teigland24022112008-03-14 15:09:15 -0500367EXPORT_SYMBOL_GPL(dlm_posix_get);
David Teigland869d81d2006-01-17 08:47:12 +0000368
369/* a read copies out one plock request from the send list */
370static ssize_t dev_read(struct file *file, char __user *u, size_t count,
371 loff_t *ppos)
372{
David Teigland24022112008-03-14 15:09:15 -0500373 struct dlm_plock_info info;
David Teigland869d81d2006-01-17 08:47:12 +0000374 struct plock_op *op = NULL;
375
376 if (count < sizeof(info))
377 return -EINVAL;
378
379 spin_lock(&ops_lock);
380 if (!list_empty(&send_list)) {
381 op = list_entry(send_list.next, struct plock_op, list);
David Teigland901025d2011-03-02 14:20:04 -0600382 if (op->info.flags & DLM_PLOCK_FL_CLOSE)
383 list_del(&op->list);
384 else
385 list_move(&op->list, &recv_list);
David Teigland869d81d2006-01-17 08:47:12 +0000386 memcpy(&info, &op->info, sizeof(info));
387 }
388 spin_unlock(&ops_lock);
389
390 if (!op)
391 return -EAGAIN;
392
David Teigland901025d2011-03-02 14:20:04 -0600393 /* 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). */
396
397 if (op->info.flags & DLM_PLOCK_FL_CLOSE)
Alexander Aringbcbb4ba2022-04-04 16:06:32 -0400398 dlm_release_plock_op(op);
David Teigland901025d2011-03-02 14:20:04 -0600399
David Teigland869d81d2006-01-17 08:47:12 +0000400 if (copy_to_user(u, &info, sizeof(info)))
401 return -EFAULT;
402 return sizeof(info);
403}
404
405/* a write copies in one plock result that should match a plock_op
406 on the recv list */
407static ssize_t dev_write(struct file *file, const char __user *u, size_t count,
408 loff_t *ppos)
409{
David Teigland24022112008-03-14 15:09:15 -0500410 struct dlm_plock_info info;
David Teigland869d81d2006-01-17 08:47:12 +0000411 struct plock_op *op;
David Teiglandc78a87d2009-06-18 13:20:24 -0500412 int found = 0, do_callback = 0;
David Teigland869d81d2006-01-17 08:47:12 +0000413
414 if (count != sizeof(info))
415 return -EINVAL;
416
417 if (copy_from_user(&info, u, sizeof(info)))
418 return -EFAULT;
419
420 if (check_version(&info))
421 return -EINVAL;
422
423 spin_lock(&ops_lock);
424 list_for_each_entry(op, &recv_list, list) {
David Teiglandc78a87d2009-06-18 13:20:24 -0500425 if (op->info.fsid == info.fsid &&
426 op->info.number == info.number &&
David Teigland08eac932006-08-04 16:19:20 -0500427 op->info.owner == info.owner) {
David Teigland869d81d2006-01-17 08:47:12 +0000428 list_del_init(&op->list);
David Teigland869d81d2006-01-17 08:47:12 +0000429 memcpy(&op->info, &info, sizeof(info));
Alexander Aringbcbb4ba2022-04-04 16:06:32 -0400430 if (op->data)
David Teiglandc78a87d2009-06-18 13:20:24 -0500431 do_callback = 1;
432 else
433 op->done = 1;
434 found = 1;
David Teigland869d81d2006-01-17 08:47:12 +0000435 break;
436 }
437 }
438 spin_unlock(&ops_lock);
439
Marc Eshel586759f2006-11-14 16:37:25 -0500440 if (found) {
David Teiglandc78a87d2009-06-18 13:20:24 -0500441 if (do_callback)
David Teigland817d10b2008-05-13 14:28:26 -0500442 dlm_plock_callback(op);
Marc Eshel586759f2006-11-14 16:37:25 -0500443 else
444 wake_up(&recv_wq);
445 } else
Alexander Aringbcfad422022-04-04 16:06:34 -0400446 log_print("%s: no op %x %llx - may got interrupted?", __func__,
447 info.fsid, (unsigned long long)info.number);
David Teigland869d81d2006-01-17 08:47:12 +0000448 return count;
449}
450
Al Viro076ccb72017-07-03 01:02:18 -0400451static __poll_t dev_poll(struct file *file, poll_table *wait)
David Teigland869d81d2006-01-17 08:47:12 +0000452{
Al Viro076ccb72017-07-03 01:02:18 -0400453 __poll_t mask = 0;
Denis Chengcee23c72007-07-25 17:53:58 +0800454
David Teigland869d81d2006-01-17 08:47:12 +0000455 poll_wait(file, &send_wq, wait);
456
457 spin_lock(&ops_lock);
Denis Chengcee23c72007-07-25 17:53:58 +0800458 if (!list_empty(&send_list))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800459 mask = EPOLLIN | EPOLLRDNORM;
David Teigland869d81d2006-01-17 08:47:12 +0000460 spin_unlock(&ops_lock);
Denis Chengcee23c72007-07-25 17:53:58 +0800461
462 return mask;
David Teigland869d81d2006-01-17 08:47:12 +0000463}
464
Arjan van de Ven00977a52007-02-12 00:55:34 -0800465static const struct file_operations dev_fops = {
David Teigland869d81d2006-01-17 08:47:12 +0000466 .read = dev_read,
467 .write = dev_write,
468 .poll = dev_poll,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200469 .owner = THIS_MODULE,
470 .llseek = noop_llseek,
David Teigland869d81d2006-01-17 08:47:12 +0000471};
472
473static struct miscdevice plock_dev_misc = {
474 .minor = MISC_DYNAMIC_MINOR,
David Teigland24022112008-03-14 15:09:15 -0500475 .name = DLM_PLOCK_MISC_NAME,
David Teigland869d81d2006-01-17 08:47:12 +0000476 .fops = &dev_fops
477};
478
David Teigland24022112008-03-14 15:09:15 -0500479int dlm_plock_init(void)
David Teigland869d81d2006-01-17 08:47:12 +0000480{
481 int rv;
482
David Teigland869d81d2006-01-17 08:47:12 +0000483 rv = misc_register(&plock_dev_misc);
484 if (rv)
David Teigland24022112008-03-14 15:09:15 -0500485 log_print("dlm_plock_init: misc_register failed %d", rv);
David Teigland869d81d2006-01-17 08:47:12 +0000486 return rv;
487}
488
David Teigland24022112008-03-14 15:09:15 -0500489void dlm_plock_exit(void)
David Teigland869d81d2006-01-17 08:47:12 +0000490{
Greg Kroah-Hartmanf368ed62015-07-30 15:59:57 -0700491 misc_deregister(&plock_dev_misc);
David Teigland869d81d2006-01-17 08:47:12 +0000492}
493