fs: dlm: add deprecation Kconfig and warnings for timeouts
[linux-block.git] / fs / dlm / user.c
CommitLineData
2522fe45 1// SPDX-License-Identifier: GPL-2.0-only
597d0cae 2/*
7fe2b319 3 * Copyright (C) 2006-2010 Red Hat, Inc. All rights reserved.
597d0cae
DT
4 */
5
6#include <linux/miscdevice.h>
7#include <linux/init.h>
8#include <linux/wait.h>
597d0cae
DT
9#include <linux/file.h>
10#include <linux/fs.h>
11#include <linux/poll.h>
12#include <linux/signal.h>
13#include <linux/spinlock.h>
14#include <linux/dlm.h>
15#include <linux/dlm_device.h>
5a0e3ad6 16#include <linux/slab.h>
174cd4b1 17#include <linux/sched/signal.h>
597d0cae
DT
18
19#include "dlm_internal.h"
20#include "lockspace.h"
21#include "lock.h"
22#include "lvb_table.h"
84c6e8cd 23#include "user.h"
8304d6f2 24#include "ast.h"
3595c559 25#include "config.h"
597d0cae 26
0fe410d3 27static const char name_prefix[] = "dlm";
00977a59 28static const struct file_operations device_fops;
dc68c7ed
DT
29static atomic_t dlm_monitor_opened;
30static int dlm_monitor_unused = 1;
597d0cae
DT
31
32#ifdef CONFIG_COMPAT
33
34struct dlm_lock_params32 {
35 __u8 mode;
36 __u8 namelen;
d7db923e
DT
37 __u16 unused;
38 __u32 flags;
597d0cae
DT
39 __u32 lkid;
40 __u32 parent;
d7db923e
DT
41 __u64 xid;
42 __u64 timeout;
597d0cae
DT
43 __u32 castparam;
44 __u32 castaddr;
45 __u32 bastparam;
46 __u32 bastaddr;
47 __u32 lksb;
597d0cae 48 char lvb[DLM_USER_LVB_LEN];
3c80d379 49 char name[];
597d0cae
DT
50};
51
52struct dlm_write_request32 {
53 __u32 version[3];
54 __u8 cmd;
55 __u8 is64bit;
56 __u8 unused[2];
57
58 union {
59 struct dlm_lock_params32 lock;
60 struct dlm_lspace_params lspace;
72c2be77 61 struct dlm_purge_params purge;
597d0cae
DT
62 } i;
63};
64
65struct dlm_lksb32 {
66 __u32 sb_status;
67 __u32 sb_lkid;
68 __u8 sb_flags;
69 __u32 sb_lvbptr;
70};
71
72struct dlm_lock_result32 {
d7db923e 73 __u32 version[3];
597d0cae
DT
74 __u32 length;
75 __u32 user_astaddr;
76 __u32 user_astparam;
77 __u32 user_lksb;
78 struct dlm_lksb32 lksb;
79 __u8 bast_mode;
80 __u8 unused[3];
81 /* Offsets may be zero if no data is present */
82 __u32 lvb_offset;
83};
84
85static void compat_input(struct dlm_write_request *kb,
2a79289e 86 struct dlm_write_request32 *kb32,
1fecb1c4 87 int namelen)
597d0cae
DT
88{
89 kb->version[0] = kb32->version[0];
90 kb->version[1] = kb32->version[1];
91 kb->version[2] = kb32->version[2];
92
93 kb->cmd = kb32->cmd;
94 kb->is64bit = kb32->is64bit;
95 if (kb->cmd == DLM_USER_CREATE_LOCKSPACE ||
96 kb->cmd == DLM_USER_REMOVE_LOCKSPACE) {
97 kb->i.lspace.flags = kb32->i.lspace.flags;
98 kb->i.lspace.minor = kb32->i.lspace.minor;
1fecb1c4 99 memcpy(kb->i.lspace.name, kb32->i.lspace.name, namelen);
72c2be77
DT
100 } else if (kb->cmd == DLM_USER_PURGE) {
101 kb->i.purge.nodeid = kb32->i.purge.nodeid;
102 kb->i.purge.pid = kb32->i.purge.pid;
597d0cae
DT
103 } else {
104 kb->i.lock.mode = kb32->i.lock.mode;
105 kb->i.lock.namelen = kb32->i.lock.namelen;
106 kb->i.lock.flags = kb32->i.lock.flags;
107 kb->i.lock.lkid = kb32->i.lock.lkid;
108 kb->i.lock.parent = kb32->i.lock.parent;
d7db923e
DT
109 kb->i.lock.xid = kb32->i.lock.xid;
110 kb->i.lock.timeout = kb32->i.lock.timeout;
c087eabd
AA
111 kb->i.lock.castparam = (__user void *)(long)kb32->i.lock.castparam;
112 kb->i.lock.castaddr = (__user void *)(long)kb32->i.lock.castaddr;
113 kb->i.lock.bastparam = (__user void *)(long)kb32->i.lock.bastparam;
114 kb->i.lock.bastaddr = (__user void *)(long)kb32->i.lock.bastaddr;
115 kb->i.lock.lksb = (__user void *)(long)kb32->i.lock.lksb;
597d0cae 116 memcpy(kb->i.lock.lvb, kb32->i.lock.lvb, DLM_USER_LVB_LEN);
1fecb1c4 117 memcpy(kb->i.lock.name, kb32->i.lock.name, namelen);
597d0cae
DT
118 }
119}
120
121static void compat_output(struct dlm_lock_result *res,
122 struct dlm_lock_result32 *res32)
123{
8286d6b1
VT
124 memset(res32, 0, sizeof(*res32));
125
d7db923e
DT
126 res32->version[0] = res->version[0];
127 res32->version[1] = res->version[1];
128 res32->version[2] = res->version[2];
129
c087eabd
AA
130 res32->user_astaddr = (__u32)(__force long)res->user_astaddr;
131 res32->user_astparam = (__u32)(__force long)res->user_astparam;
132 res32->user_lksb = (__u32)(__force long)res->user_lksb;
597d0cae
DT
133 res32->bast_mode = res->bast_mode;
134
135 res32->lvb_offset = res->lvb_offset;
136 res32->length = res->length;
137
138 res32->lksb.sb_status = res->lksb.sb_status;
139 res32->lksb.sb_flags = res->lksb.sb_flags;
140 res32->lksb.sb_lkid = res->lksb.sb_lkid;
141 res32->lksb.sb_lvbptr = (__u32)(long)res->lksb.sb_lvbptr;
142}
143#endif
144
84d8cd69
DT
145/* Figure out if this lock is at the end of its life and no longer
146 available for the application to use. The lkb still exists until
147 the final ast is read. A lock becomes EOL in three situations:
148 1. a noqueue request fails with EAGAIN
149 2. an unlock completes with EUNLOCK
150 3. a cancel of a waiting request completes with ECANCEL/EDEADLK
151 An EOL lock needs to be removed from the process's list of locks.
152 And we can't allow any new operation on an EOL lock. This is
153 not related to the lifetime of the lkb struct which is managed
154 entirely by refcount. */
155
8304d6f2 156static int lkb_is_endoflife(int mode, int status)
84d8cd69 157{
8304d6f2 158 switch (status) {
84d8cd69
DT
159 case -DLM_EUNLOCK:
160 return 1;
161 case -DLM_ECANCEL:
162 case -ETIMEDOUT:
8b4021fa 163 case -EDEADLK:
84d8cd69 164 case -EAGAIN:
8304d6f2 165 if (mode == DLM_LOCK_IV)
84d8cd69
DT
166 return 1;
167 break;
168 }
169 return 0;
170}
171
ef0c2bb0
DT
172/* we could possibly check if the cancel of an orphan has resulted in the lkb
173 being removed and then remove that lkb from the orphans list and free it */
597d0cae 174
8304d6f2
DT
175void dlm_user_add_ast(struct dlm_lkb *lkb, uint32_t flags, int mode,
176 int status, uint32_t sbflags, uint64_t seq)
597d0cae
DT
177{
178 struct dlm_ls *ls;
179 struct dlm_user_args *ua;
180 struct dlm_user_proc *proc;
8304d6f2 181 int rv;
597d0cae 182
ef0c2bb0 183 if (lkb->lkb_flags & (DLM_IFL_ORPHAN | DLM_IFL_DEAD))
597d0cae 184 return;
597d0cae
DT
185
186 ls = lkb->lkb_resource->res_ls;
187 mutex_lock(&ls->ls_clear_proc_locks);
188
189 /* If ORPHAN/DEAD flag is set, it means the process is dead so an ast
190 can't be delivered. For ORPHAN's, dlm_clear_proc_locks() freed
ef0c2bb0
DT
191 lkb->ua so we can't try to use it. This second check is necessary
192 for cases where a completion ast is received for an operation that
193 began before clear_proc_locks did its cancel/unlock. */
597d0cae 194
ef0c2bb0 195 if (lkb->lkb_flags & (DLM_IFL_ORPHAN | DLM_IFL_DEAD))
597d0cae 196 goto out;
597d0cae 197
d292c0cc
DT
198 DLM_ASSERT(lkb->lkb_ua, dlm_print_lkb(lkb););
199 ua = lkb->lkb_ua;
597d0cae
DT
200 proc = ua->proc;
201
8304d6f2 202 if ((flags & DLM_CB_BAST) && ua->bastaddr == NULL)
597d0cae
DT
203 goto out;
204
8304d6f2
DT
205 if ((flags & DLM_CB_CAST) && lkb_is_endoflife(mode, status))
206 lkb->lkb_flags |= DLM_IFL_ENDOFLIFE;
207
597d0cae 208 spin_lock(&proc->asts_spin);
ef0c2bb0 209
8304d6f2
DT
210 rv = dlm_add_lkb_callback(lkb, flags, mode, status, sbflags, seq);
211 if (rv < 0) {
212 spin_unlock(&proc->asts_spin);
213 goto out;
214 }
ef0c2bb0 215
23e8e1aa 216 if (list_empty(&lkb->lkb_cb_list)) {
597d0cae 217 kref_get(&lkb->lkb_ref);
23e8e1aa 218 list_add_tail(&lkb->lkb_cb_list, &proc->asts);
597d0cae
DT
219 wake_up_interruptible(&proc->wait);
220 }
597d0cae 221 spin_unlock(&proc->asts_spin);
34e22bed 222
8304d6f2
DT
223 if (lkb->lkb_flags & DLM_IFL_ENDOFLIFE) {
224 /* N.B. spin_lock locks_spin, not asts_spin */
ce5246b9 225 spin_lock(&proc->locks_spin);
ef0c2bb0
DT
226 if (!list_empty(&lkb->lkb_ownqueue)) {
227 list_del_init(&lkb->lkb_ownqueue);
228 dlm_put_lkb(lkb);
229 }
ce5246b9 230 spin_unlock(&proc->locks_spin);
34e22bed 231 }
597d0cae
DT
232 out:
233 mutex_unlock(&ls->ls_clear_proc_locks);
234}
235
236static int device_user_lock(struct dlm_user_proc *proc,
237 struct dlm_lock_params *params)
238{
239 struct dlm_ls *ls;
240 struct dlm_user_args *ua;
2ab4bd8e 241 uint32_t lkid;
597d0cae
DT
242 int error = -ENOMEM;
243
244 ls = dlm_find_lockspace_local(proc->lockspace);
245 if (!ls)
246 return -ENOENT;
247
248 if (!params->castaddr || !params->lksb) {
249 error = -EINVAL;
250 goto out;
251 }
252
81eeb82f
AA
253#ifdef CONFIG_DLM_DEPRECATED_API
254 if (params->timeout)
255 pr_warn_once("========================================================\n"
256 "WARNING: the lkb timeout feature is being deprecated and\n"
257 " will be removed in v6.2!\n"
258 "========================================================\n");
259#endif
260
573c24c4 261 ua = kzalloc(sizeof(struct dlm_user_args), GFP_NOFS);
597d0cae
DT
262 if (!ua)
263 goto out;
264 ua->proc = proc;
265 ua->user_lksb = params->lksb;
266 ua->castparam = params->castparam;
267 ua->castaddr = params->castaddr;
268 ua->bastparam = params->bastparam;
269 ua->bastaddr = params->bastaddr;
d7db923e 270 ua->xid = params->xid;
597d0cae 271
2ab4bd8e 272 if (params->flags & DLM_LKF_CONVERT) {
597d0cae
DT
273 error = dlm_user_convert(ls, ua,
274 params->mode, params->flags,
d7db923e
DT
275 params->lkid, params->lvb,
276 (unsigned long) params->timeout);
2ab4bd8e
DT
277 } else if (params->flags & DLM_LKF_ORPHAN) {
278 error = dlm_user_adopt_orphan(ls, ua,
279 params->mode, params->flags,
280 params->name, params->namelen,
2ab4bd8e
DT
281 &lkid);
282 if (!error)
283 error = lkid;
284 } else {
597d0cae
DT
285 error = dlm_user_request(ls, ua,
286 params->mode, params->flags,
287 params->name, params->namelen,
d7db923e 288 (unsigned long) params->timeout);
597d0cae
DT
289 if (!error)
290 error = ua->lksb.sb_lkid;
291 }
292 out:
293 dlm_put_lockspace(ls);
294 return error;
295}
296
297static int device_user_unlock(struct dlm_user_proc *proc,
298 struct dlm_lock_params *params)
299{
300 struct dlm_ls *ls;
301 struct dlm_user_args *ua;
302 int error = -ENOMEM;
303
304 ls = dlm_find_lockspace_local(proc->lockspace);
305 if (!ls)
306 return -ENOENT;
307
573c24c4 308 ua = kzalloc(sizeof(struct dlm_user_args), GFP_NOFS);
597d0cae
DT
309 if (!ua)
310 goto out;
311 ua->proc = proc;
312 ua->user_lksb = params->lksb;
313 ua->castparam = params->castparam;
314 ua->castaddr = params->castaddr;
315
316 if (params->flags & DLM_LKF_CANCEL)
317 error = dlm_user_cancel(ls, ua, params->flags, params->lkid);
318 else
319 error = dlm_user_unlock(ls, ua, params->flags, params->lkid,
320 params->lvb);
321 out:
322 dlm_put_lockspace(ls);
323 return error;
324}
325
8b4021fa
DT
326static int device_user_deadlock(struct dlm_user_proc *proc,
327 struct dlm_lock_params *params)
328{
329 struct dlm_ls *ls;
330 int error;
331
332 ls = dlm_find_lockspace_local(proc->lockspace);
333 if (!ls)
334 return -ENOENT;
335
336 error = dlm_user_deadlock(ls, params->flags, params->lkid);
337
338 dlm_put_lockspace(ls);
339 return error;
340}
341
0f8e0d9a 342static int dlm_device_register(struct dlm_ls *ls, char *name)
254da030
PC
343{
344 int error, len;
345
0f8e0d9a
DT
346 /* The device is already registered. This happens when the
347 lockspace is created multiple times from userspace. */
348 if (ls->ls_device.name)
349 return 0;
350
254da030
PC
351 error = -ENOMEM;
352 len = strlen(name) + strlen(name_prefix) + 2;
573c24c4 353 ls->ls_device.name = kzalloc(len, GFP_NOFS);
254da030
PC
354 if (!ls->ls_device.name)
355 goto fail;
356
357 snprintf((char *)ls->ls_device.name, len, "%s_%s", name_prefix,
358 name);
359 ls->ls_device.fops = &device_fops;
360 ls->ls_device.minor = MISC_DYNAMIC_MINOR;
361
362 error = misc_register(&ls->ls_device);
363 if (error) {
364 kfree(ls->ls_device.name);
55acdd92
ET
365 /* this has to be set to NULL
366 * to avoid a double-free in dlm_device_deregister
367 */
368 ls->ls_device.name = NULL;
254da030
PC
369 }
370fail:
371 return error;
372}
373
0f8e0d9a
DT
374int dlm_device_deregister(struct dlm_ls *ls)
375{
0f8e0d9a
DT
376 /* The device is not registered. This happens when the lockspace
377 was never used from userspace, or when device_create_lockspace()
378 calls dlm_release_lockspace() after the register fails. */
379 if (!ls->ls_device.name)
380 return 0;
381
f368ed60
GKH
382 misc_deregister(&ls->ls_device);
383 kfree(ls->ls_device.name);
384 return 0;
0f8e0d9a
DT
385}
386
72c2be77
DT
387static int device_user_purge(struct dlm_user_proc *proc,
388 struct dlm_purge_params *params)
389{
390 struct dlm_ls *ls;
391 int error;
392
393 ls = dlm_find_lockspace_local(proc->lockspace);
394 if (!ls)
395 return -ENOENT;
396
397 error = dlm_user_purge(ls, proc, params->nodeid, params->pid);
398
399 dlm_put_lockspace(ls);
400 return error;
401}
402
597d0cae
DT
403static int device_create_lockspace(struct dlm_lspace_params *params)
404{
405 dlm_lockspace_t *lockspace;
406 struct dlm_ls *ls;
254da030 407 int error;
597d0cae
DT
408
409 if (!capable(CAP_SYS_ADMIN))
410 return -EPERM;
411
3595c559 412 error = dlm_new_lockspace(params->name, dlm_config.ci_cluster_name, params->flags,
60f98d18
DT
413 DLM_USER_LVB_LEN, NULL, NULL, NULL,
414 &lockspace);
597d0cae
DT
415 if (error)
416 return error;
417
418 ls = dlm_find_lockspace_local(lockspace);
419 if (!ls)
420 return -ENOENT;
421
0f8e0d9a 422 error = dlm_device_register(ls, params->name);
597d0cae 423 dlm_put_lockspace(ls);
597d0cae 424
254da030
PC
425 if (error)
426 dlm_release_lockspace(lockspace, 0);
427 else
428 error = ls->ls_device.minor;
429
597d0cae
DT
430 return error;
431}
432
433static int device_remove_lockspace(struct dlm_lspace_params *params)
434{
435 dlm_lockspace_t *lockspace;
436 struct dlm_ls *ls;
c6e6f0ba 437 int error, force = 0;
597d0cae
DT
438
439 if (!capable(CAP_SYS_ADMIN))
440 return -EPERM;
441
442 ls = dlm_find_lockspace_device(params->minor);
443 if (!ls)
444 return -ENOENT;
445
c6e6f0ba
DT
446 if (params->flags & DLM_USER_LSFLG_FORCEFREE)
447 force = 2;
448
597d0cae 449 lockspace = ls->ls_local_handle;
0f8e0d9a 450 dlm_put_lockspace(ls);
597d0cae 451
0f8e0d9a
DT
452 /* The final dlm_release_lockspace waits for references to go to
453 zero, so all processes will need to close their device for the
454 ls before the release will proceed. release also calls the
455 device_deregister above. Converting a positive return value
456 from release to zero means that userspace won't know when its
457 release was the final one, but it shouldn't need to know. */
597d0cae 458
c6e6f0ba 459 error = dlm_release_lockspace(lockspace, force);
0f8e0d9a
DT
460 if (error > 0)
461 error = 0;
597d0cae
DT
462 return error;
463}
464
465/* Check the user's version matches ours */
466static int check_version(struct dlm_write_request *req)
467{
468 if (req->version[0] != DLM_DEVICE_VERSION_MAJOR ||
469 (req->version[0] == DLM_DEVICE_VERSION_MAJOR &&
470 req->version[1] > DLM_DEVICE_VERSION_MINOR)) {
471
472 printk(KERN_DEBUG "dlm: process %s (%d) version mismatch "
473 "user (%d.%d.%d) kernel (%d.%d.%d)\n",
474 current->comm,
ba25f9dc 475 task_pid_nr(current),
597d0cae
DT
476 req->version[0],
477 req->version[1],
478 req->version[2],
479 DLM_DEVICE_VERSION_MAJOR,
480 DLM_DEVICE_VERSION_MINOR,
481 DLM_DEVICE_VERSION_PATCH);
482 return -EINVAL;
483 }
484 return 0;
485}
486
487/*
488 * device_write
489 *
490 * device_user_lock
491 * dlm_user_request -> request_lock
492 * dlm_user_convert -> convert_lock
493 *
494 * device_user_unlock
495 * dlm_user_unlock -> unlock_lock
496 * dlm_user_cancel -> cancel_lock
497 *
498 * device_create_lockspace
499 * dlm_new_lockspace
500 *
501 * device_remove_lockspace
502 * dlm_release_lockspace
503 */
504
505/* a write to a lockspace device is a lock or unlock request, a write
506 to the control device is to create/remove a lockspace */
507
508static ssize_t device_write(struct file *file, const char __user *buf,
509 size_t count, loff_t *ppos)
510{
511 struct dlm_user_proc *proc = file->private_data;
512 struct dlm_write_request *kbuf;
597d0cae
DT
513 int error;
514
515#ifdef CONFIG_COMPAT
516 if (count < sizeof(struct dlm_write_request32))
517#else
518 if (count < sizeof(struct dlm_write_request))
519#endif
520 return -EINVAL;
521
d4b0bcf3
DT
522 /*
523 * can't compare against COMPAT/dlm_write_request32 because
524 * we don't yet know if is64bit is zero
525 */
2b75bc91 526 if (count > sizeof(struct dlm_write_request) + DLM_RESNAME_MAXLEN)
2b75bc91
SL
527 return -EINVAL;
528
16e5c1fc 529 kbuf = memdup_user_nul(buf, count);
117aa41e 530 if (IS_ERR(kbuf))
16e5c1fc 531 return PTR_ERR(kbuf);
597d0cae
DT
532
533 if (check_version(kbuf)) {
534 error = -EBADE;
535 goto out_free;
536 }
537
538#ifdef CONFIG_COMPAT
539 if (!kbuf->is64bit) {
540 struct dlm_write_request32 *k32buf;
1fecb1c4
DT
541 int namelen = 0;
542
543 if (count > sizeof(struct dlm_write_request32))
544 namelen = count - sizeof(struct dlm_write_request32);
545
597d0cae 546 k32buf = (struct dlm_write_request32 *)kbuf;
1fecb1c4
DT
547
548 /* add 1 after namelen so that the name string is terminated */
549 kbuf = kzalloc(sizeof(struct dlm_write_request) + namelen + 1,
573c24c4 550 GFP_NOFS);
cb980d9a
DT
551 if (!kbuf) {
552 kfree(k32buf);
597d0cae 553 return -ENOMEM;
cb980d9a 554 }
597d0cae
DT
555
556 if (proc)
557 set_bit(DLM_PROC_FLAGS_COMPAT, &proc->flags);
1fecb1c4
DT
558
559 compat_input(kbuf, k32buf, namelen);
597d0cae
DT
560 kfree(k32buf);
561 }
562#endif
563
564 /* do we really need this? can a write happen after a close? */
565 if ((kbuf->cmd == DLM_USER_LOCK || kbuf->cmd == DLM_USER_UNLOCK) &&
cb980d9a
DT
566 (proc && test_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags))) {
567 error = -EINVAL;
568 goto out_free;
569 }
597d0cae 570
597d0cae
DT
571 error = -EINVAL;
572
573 switch (kbuf->cmd)
574 {
575 case DLM_USER_LOCK:
576 if (!proc) {
577 log_print("no locking on control device");
c6ca7bc9 578 goto out_free;
597d0cae
DT
579 }
580 error = device_user_lock(proc, &kbuf->i.lock);
581 break;
582
583 case DLM_USER_UNLOCK:
584 if (!proc) {
585 log_print("no locking on control device");
c6ca7bc9 586 goto out_free;
597d0cae
DT
587 }
588 error = device_user_unlock(proc, &kbuf->i.lock);
589 break;
590
8b4021fa
DT
591 case DLM_USER_DEADLOCK:
592 if (!proc) {
593 log_print("no locking on control device");
c6ca7bc9 594 goto out_free;
8b4021fa
DT
595 }
596 error = device_user_deadlock(proc, &kbuf->i.lock);
597 break;
598
597d0cae
DT
599 case DLM_USER_CREATE_LOCKSPACE:
600 if (proc) {
601 log_print("create/remove only on control device");
c6ca7bc9 602 goto out_free;
597d0cae
DT
603 }
604 error = device_create_lockspace(&kbuf->i.lspace);
605 break;
606
607 case DLM_USER_REMOVE_LOCKSPACE:
608 if (proc) {
609 log_print("create/remove only on control device");
c6ca7bc9 610 goto out_free;
597d0cae
DT
611 }
612 error = device_remove_lockspace(&kbuf->i.lspace);
613 break;
614
72c2be77
DT
615 case DLM_USER_PURGE:
616 if (!proc) {
617 log_print("no locking on control device");
c6ca7bc9 618 goto out_free;
72c2be77
DT
619 }
620 error = device_user_purge(proc, &kbuf->i.purge);
621 break;
622
597d0cae
DT
623 default:
624 log_print("Unknown command passed to DLM device : %d\n",
625 kbuf->cmd);
626 }
627
597d0cae
DT
628 out_free:
629 kfree(kbuf);
630 return error;
631}
632
633/* Every process that opens the lockspace device has its own "proc" structure
634 hanging off the open file that's used to keep track of locks owned by the
635 process and asts that need to be delivered to the process. */
636
637static int device_open(struct inode *inode, struct file *file)
638{
639 struct dlm_user_proc *proc;
640 struct dlm_ls *ls;
641
642 ls = dlm_find_lockspace_device(iminor(inode));
f9f2ed48 643 if (!ls)
597d0cae
DT
644 return -ENOENT;
645
573c24c4 646 proc = kzalloc(sizeof(struct dlm_user_proc), GFP_NOFS);
597d0cae
DT
647 if (!proc) {
648 dlm_put_lockspace(ls);
649 return -ENOMEM;
650 }
651
652 proc->lockspace = ls->ls_local_handle;
653 INIT_LIST_HEAD(&proc->asts);
654 INIT_LIST_HEAD(&proc->locks);
a1bc86e6 655 INIT_LIST_HEAD(&proc->unlocking);
597d0cae
DT
656 spin_lock_init(&proc->asts_spin);
657 spin_lock_init(&proc->locks_spin);
658 init_waitqueue_head(&proc->wait);
659 file->private_data = proc;
660
661 return 0;
662}
663
664static int device_close(struct inode *inode, struct file *file)
665{
666 struct dlm_user_proc *proc = file->private_data;
667 struct dlm_ls *ls;
597d0cae
DT
668
669 ls = dlm_find_lockspace_local(proc->lockspace);
670 if (!ls)
671 return -ENOENT;
672
597d0cae
DT
673 set_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags);
674
675 dlm_clear_proc_locks(ls, proc);
676
677 /* at this point no more lkb's should exist for this lockspace,
678 so there's no chance of dlm_user_add_ast() being called and
679 looking for lkb->ua->proc */
680
681 kfree(proc);
682 file->private_data = NULL;
683
684 dlm_put_lockspace(ls);
685 dlm_put_lockspace(ls); /* for the find in device_open() */
686
687 /* FIXME: AUTOFREE: if this ls is no longer used do
688 device_remove_lockspace() */
689
597d0cae
DT
690 return 0;
691}
692
8304d6f2
DT
693static int copy_result_to_user(struct dlm_user_args *ua, int compat,
694 uint32_t flags, int mode, int copy_lvb,
695 char __user *buf, size_t count)
597d0cae
DT
696{
697#ifdef CONFIG_COMPAT
698 struct dlm_lock_result32 result32;
699#endif
700 struct dlm_lock_result result;
701 void *resultptr;
702 int error=0;
703 int len;
704 int struct_len;
705
706 memset(&result, 0, sizeof(struct dlm_lock_result));
d7db923e
DT
707 result.version[0] = DLM_DEVICE_VERSION_MAJOR;
708 result.version[1] = DLM_DEVICE_VERSION_MINOR;
709 result.version[2] = DLM_DEVICE_VERSION_PATCH;
9de30f3f 710 memcpy(&result.lksb, &ua->lksb, offsetof(struct dlm_lksb, sb_lvbptr));
597d0cae
DT
711 result.user_lksb = ua->user_lksb;
712
713 /* FIXME: dlm1 provides for the user's bastparam/addr to not be updated
714 in a conversion unless the conversion is successful. See code
715 in dlm_user_convert() for updating ua from ua_tmp. OpenVMS, though,
716 notes that a new blocking AST address and parameter are set even if
717 the conversion fails, so maybe we should just do that. */
718
8304d6f2 719 if (flags & DLM_CB_BAST) {
597d0cae
DT
720 result.user_astaddr = ua->bastaddr;
721 result.user_astparam = ua->bastparam;
89d799d0 722 result.bast_mode = mode;
597d0cae
DT
723 } else {
724 result.user_astaddr = ua->castaddr;
725 result.user_astparam = ua->castparam;
726 }
727
728#ifdef CONFIG_COMPAT
729 if (compat)
730 len = sizeof(struct dlm_lock_result32);
731 else
732#endif
733 len = sizeof(struct dlm_lock_result);
734 struct_len = len;
735
736 /* copy lvb to userspace if there is one, it's been updated, and
737 the user buffer has space for it */
738
8304d6f2 739 if (copy_lvb && ua->lksb.sb_lvbptr && count >= len + DLM_USER_LVB_LEN) {
597d0cae
DT
740 if (copy_to_user(buf+len, ua->lksb.sb_lvbptr,
741 DLM_USER_LVB_LEN)) {
742 error = -EFAULT;
743 goto out;
744 }
745
746 result.lvb_offset = len;
747 len += DLM_USER_LVB_LEN;
748 }
749
750 result.length = len;
751 resultptr = &result;
752#ifdef CONFIG_COMPAT
753 if (compat) {
754 compat_output(&result, &result32);
755 resultptr = &result32;
756 }
757#endif
758
759 if (copy_to_user(buf, resultptr, struct_len))
760 error = -EFAULT;
761 else
762 error = len;
763 out:
764 return error;
765}
766
d7db923e
DT
767static int copy_version_to_user(char __user *buf, size_t count)
768{
769 struct dlm_device_version ver;
770
771 memset(&ver, 0, sizeof(struct dlm_device_version));
772 ver.version[0] = DLM_DEVICE_VERSION_MAJOR;
773 ver.version[1] = DLM_DEVICE_VERSION_MINOR;
774 ver.version[2] = DLM_DEVICE_VERSION_PATCH;
775
776 if (copy_to_user(buf, &ver, sizeof(struct dlm_device_version)))
777 return -EFAULT;
778 return sizeof(struct dlm_device_version);
779}
780
597d0cae
DT
781/* a read returns a single ast described in a struct dlm_lock_result */
782
783static ssize_t device_read(struct file *file, char __user *buf, size_t count,
784 loff_t *ppos)
785{
786 struct dlm_user_proc *proc = file->private_data;
787 struct dlm_lkb *lkb;
597d0cae 788 DECLARE_WAITQUEUE(wait, current);
8304d6f2
DT
789 struct dlm_callback cb;
790 int rv, resid, copy_lvb = 0;
b96f4650 791 int old_mode, new_mode;
597d0cae 792
d7db923e 793 if (count == sizeof(struct dlm_device_version)) {
8304d6f2
DT
794 rv = copy_version_to_user(buf, count);
795 return rv;
d7db923e
DT
796 }
797
798 if (!proc) {
799 log_print("non-version read from control device %zu", count);
800 return -EINVAL;
801 }
802
597d0cae
DT
803#ifdef CONFIG_COMPAT
804 if (count < sizeof(struct dlm_lock_result32))
805#else
806 if (count < sizeof(struct dlm_lock_result))
807#endif
808 return -EINVAL;
809
89d799d0
DT
810 try_another:
811
597d0cae
DT
812 /* do we really need this? can a read happen after a close? */
813 if (test_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags))
814 return -EINVAL;
815
816 spin_lock(&proc->asts_spin);
817 if (list_empty(&proc->asts)) {
818 if (file->f_flags & O_NONBLOCK) {
819 spin_unlock(&proc->asts_spin);
820 return -EAGAIN;
821 }
822
823 add_wait_queue(&proc->wait, &wait);
824
825 repeat:
826 set_current_state(TASK_INTERRUPTIBLE);
827 if (list_empty(&proc->asts) && !signal_pending(current)) {
828 spin_unlock(&proc->asts_spin);
829 schedule();
830 spin_lock(&proc->asts_spin);
831 goto repeat;
832 }
833 set_current_state(TASK_RUNNING);
834 remove_wait_queue(&proc->wait, &wait);
835
836 if (signal_pending(current)) {
837 spin_unlock(&proc->asts_spin);
838 return -ERESTARTSYS;
839 }
840 }
841
8304d6f2 842 /* if we empty lkb_callbacks, we don't want to unlock the spinlock
23e8e1aa 843 without removing lkb_cb_list; so empty lkb_cb_list is always
8304d6f2 844 consistent with empty lkb_callbacks */
597d0cae 845
23e8e1aa 846 lkb = list_entry(proc->asts.next, struct dlm_lkb, lkb_cb_list);
597d0cae 847
b96f4650
DT
848 /* rem_lkb_callback sets a new lkb_last_cast */
849 old_mode = lkb->lkb_last_cast.mode;
850
8304d6f2
DT
851 rv = dlm_rem_lkb_callback(lkb->lkb_resource->res_ls, lkb, &cb, &resid);
852 if (rv < 0) {
853 /* this shouldn't happen; lkb should have been removed from
854 list when resid was zero */
855 log_print("dlm_rem_lkb_callback empty %x", lkb->lkb_id);
23e8e1aa 856 list_del_init(&lkb->lkb_cb_list);
8304d6f2
DT
857 spin_unlock(&proc->asts_spin);
858 /* removes ref for proc->asts, may cause lkb to be freed */
859 dlm_put_lkb(lkb);
860 goto try_another;
89d799d0 861 }
8304d6f2 862 if (!resid)
23e8e1aa 863 list_del_init(&lkb->lkb_cb_list);
8304d6f2 864 spin_unlock(&proc->asts_spin);
89d799d0 865
8304d6f2
DT
866 if (cb.flags & DLM_CB_SKIP) {
867 /* removes ref for proc->asts, may cause lkb to be freed */
868 if (!resid)
869 dlm_put_lkb(lkb);
870 goto try_another;
89d799d0
DT
871 }
872
8304d6f2 873 if (cb.flags & DLM_CB_CAST) {
8304d6f2 874 new_mode = cb.mode;
597d0cae 875
8304d6f2
DT
876 if (!cb.sb_status && lkb->lkb_lksb->sb_lvbptr &&
877 dlm_lvb_operations[old_mode + 1][new_mode + 1])
878 copy_lvb = 1;
89d799d0 879
8304d6f2
DT
880 lkb->lkb_lksb->sb_status = cb.sb_status;
881 lkb->lkb_lksb->sb_flags = cb.sb_flags;
89d799d0 882 }
597d0cae 883
8304d6f2
DT
884 rv = copy_result_to_user(lkb->lkb_ua,
885 test_bit(DLM_PROC_FLAGS_COMPAT, &proc->flags),
886 cb.flags, cb.mode, copy_lvb, buf, count);
89d799d0 887
8304d6f2
DT
888 /* removes ref for proc->asts, may cause lkb to be freed */
889 if (!resid)
597d0cae
DT
890 dlm_put_lkb(lkb);
891
8304d6f2 892 return rv;
597d0cae
DT
893}
894
076ccb76 895static __poll_t device_poll(struct file *file, poll_table *wait)
597d0cae
DT
896{
897 struct dlm_user_proc *proc = file->private_data;
898
899 poll_wait(file, &proc->wait, wait);
900
901 spin_lock(&proc->asts_spin);
902 if (!list_empty(&proc->asts)) {
903 spin_unlock(&proc->asts_spin);
a9a08845 904 return EPOLLIN | EPOLLRDNORM;
597d0cae
DT
905 }
906 spin_unlock(&proc->asts_spin);
907 return 0;
908}
909
dc68c7ed
DT
910int dlm_user_daemon_available(void)
911{
912 /* dlm_controld hasn't started (or, has started, but not
913 properly populated configfs) */
914
915 if (!dlm_our_nodeid())
916 return 0;
917
918 /* This is to deal with versions of dlm_controld that don't
919 know about the monitor device. We assume that if the
920 dlm_controld was started (above), but the monitor device
921 was never opened, that it's an old version. dlm_controld
922 should open the monitor device before populating configfs. */
923
924 if (dlm_monitor_unused)
925 return 1;
926
927 return atomic_read(&dlm_monitor_opened) ? 1 : 0;
928}
929
597d0cae
DT
930static int ctl_device_open(struct inode *inode, struct file *file)
931{
932 file->private_data = NULL;
933 return 0;
934}
935
936static int ctl_device_close(struct inode *inode, struct file *file)
937{
938 return 0;
939}
940
dc68c7ed
DT
941static int monitor_device_open(struct inode *inode, struct file *file)
942{
943 atomic_inc(&dlm_monitor_opened);
944 dlm_monitor_unused = 0;
945 return 0;
946}
947
948static int monitor_device_close(struct inode *inode, struct file *file)
949{
950 if (atomic_dec_and_test(&dlm_monitor_opened))
951 dlm_stop_lockspaces();
952 return 0;
953}
954
00977a59 955static const struct file_operations device_fops = {
597d0cae
DT
956 .open = device_open,
957 .release = device_close,
958 .read = device_read,
959 .write = device_write,
960 .poll = device_poll,
961 .owner = THIS_MODULE,
6038f373 962 .llseek = noop_llseek,
597d0cae
DT
963};
964
00977a59 965static const struct file_operations ctl_device_fops = {
597d0cae
DT
966 .open = ctl_device_open,
967 .release = ctl_device_close,
d7db923e 968 .read = device_read,
597d0cae
DT
969 .write = device_write,
970 .owner = THIS_MODULE,
6038f373 971 .llseek = noop_llseek,
597d0cae
DT
972};
973
0fe410d3
DC
974static struct miscdevice ctl_device = {
975 .name = "dlm-control",
976 .fops = &ctl_device_fops,
977 .minor = MISC_DYNAMIC_MINOR,
978};
979
dc68c7ed
DT
980static const struct file_operations monitor_device_fops = {
981 .open = monitor_device_open,
982 .release = monitor_device_close,
983 .owner = THIS_MODULE,
6038f373 984 .llseek = noop_llseek,
dc68c7ed
DT
985};
986
987static struct miscdevice monitor_device = {
988 .name = "dlm-monitor",
989 .fops = &monitor_device_fops,
990 .minor = MISC_DYNAMIC_MINOR,
991};
992
30727174 993int __init dlm_user_init(void)
597d0cae
DT
994{
995 int error;
996
dc68c7ed
DT
997 atomic_set(&dlm_monitor_opened, 0);
998
597d0cae 999 error = misc_register(&ctl_device);
dc68c7ed 1000 if (error) {
597d0cae 1001 log_print("misc_register failed for control device");
dc68c7ed
DT
1002 goto out;
1003 }
597d0cae 1004
dc68c7ed
DT
1005 error = misc_register(&monitor_device);
1006 if (error) {
1007 log_print("misc_register failed for monitor device");
1008 misc_deregister(&ctl_device);
1009 }
1010 out:
597d0cae
DT
1011 return error;
1012}
1013
1014void dlm_user_exit(void)
1015{
1016 misc_deregister(&ctl_device);
dc68c7ed 1017 misc_deregister(&monitor_device);
597d0cae
DT
1018}
1019