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