xref: /openbmc/linux/fs/dlm/user.c (revision e868d61272caa648214046a096e5a6bfc068dc8c)
1 /*
2  * Copyright (C) 2006-2007 Red Hat, Inc.  All rights reserved.
3  *
4  * This copyrighted material is made available to anyone wishing to use,
5  * modify, copy, or redistribute it subject to the terms and conditions
6  * of the GNU General Public License v.2.
7  */
8 
9 #include <linux/miscdevice.h>
10 #include <linux/init.h>
11 #include <linux/wait.h>
12 #include <linux/module.h>
13 #include <linux/file.h>
14 #include <linux/fs.h>
15 #include <linux/poll.h>
16 #include <linux/signal.h>
17 #include <linux/spinlock.h>
18 #include <linux/dlm.h>
19 #include <linux/dlm_device.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 
27 static const char *name_prefix="dlm";
28 static struct miscdevice ctl_device;
29 static const struct file_operations device_fops;
30 
31 #ifdef CONFIG_COMPAT
32 
33 struct dlm_lock_params32 {
34 	__u8 mode;
35 	__u8 namelen;
36 	__u16 flags;
37 	__u32 lkid;
38 	__u32 parent;
39 
40 	__u32 castparam;
41 	__u32 castaddr;
42 	__u32 bastparam;
43 	__u32 bastaddr;
44 	__u32 lksb;
45 
46 	char lvb[DLM_USER_LVB_LEN];
47 	char name[0];
48 };
49 
50 struct dlm_write_request32 {
51 	__u32 version[3];
52 	__u8 cmd;
53 	__u8 is64bit;
54 	__u8 unused[2];
55 
56 	union  {
57 		struct dlm_lock_params32 lock;
58 		struct dlm_lspace_params lspace;
59 		struct dlm_purge_params purge;
60 	} i;
61 };
62 
63 struct dlm_lksb32 {
64 	__u32 sb_status;
65 	__u32 sb_lkid;
66 	__u8 sb_flags;
67 	__u32 sb_lvbptr;
68 };
69 
70 struct dlm_lock_result32 {
71 	__u32 length;
72 	__u32 user_astaddr;
73 	__u32 user_astparam;
74 	__u32 user_lksb;
75 	struct dlm_lksb32 lksb;
76 	__u8 bast_mode;
77 	__u8 unused[3];
78 	/* Offsets may be zero if no data is present */
79 	__u32 lvb_offset;
80 };
81 
82 static void compat_input(struct dlm_write_request *kb,
83 			 struct dlm_write_request32 *kb32)
84 {
85 	kb->version[0] = kb32->version[0];
86 	kb->version[1] = kb32->version[1];
87 	kb->version[2] = kb32->version[2];
88 
89 	kb->cmd = kb32->cmd;
90 	kb->is64bit = kb32->is64bit;
91 	if (kb->cmd == DLM_USER_CREATE_LOCKSPACE ||
92 	    kb->cmd == DLM_USER_REMOVE_LOCKSPACE) {
93 		kb->i.lspace.flags = kb32->i.lspace.flags;
94 		kb->i.lspace.minor = kb32->i.lspace.minor;
95 		strcpy(kb->i.lspace.name, kb32->i.lspace.name);
96 	} else if (kb->cmd == DLM_USER_PURGE) {
97 		kb->i.purge.nodeid = kb32->i.purge.nodeid;
98 		kb->i.purge.pid = kb32->i.purge.pid;
99 	} else {
100 		kb->i.lock.mode = kb32->i.lock.mode;
101 		kb->i.lock.namelen = kb32->i.lock.namelen;
102 		kb->i.lock.flags = kb32->i.lock.flags;
103 		kb->i.lock.lkid = kb32->i.lock.lkid;
104 		kb->i.lock.parent = kb32->i.lock.parent;
105 		kb->i.lock.castparam = (void *)(long)kb32->i.lock.castparam;
106 		kb->i.lock.castaddr = (void *)(long)kb32->i.lock.castaddr;
107 		kb->i.lock.bastparam = (void *)(long)kb32->i.lock.bastparam;
108 		kb->i.lock.bastaddr = (void *)(long)kb32->i.lock.bastaddr;
109 		kb->i.lock.lksb = (void *)(long)kb32->i.lock.lksb;
110 		memcpy(kb->i.lock.lvb, kb32->i.lock.lvb, DLM_USER_LVB_LEN);
111 		memcpy(kb->i.lock.name, kb32->i.lock.name, kb->i.lock.namelen);
112 	}
113 }
114 
115 static void compat_output(struct dlm_lock_result *res,
116 			  struct dlm_lock_result32 *res32)
117 {
118 	res32->user_astaddr = (__u32)(long)res->user_astaddr;
119 	res32->user_astparam = (__u32)(long)res->user_astparam;
120 	res32->user_lksb = (__u32)(long)res->user_lksb;
121 	res32->bast_mode = res->bast_mode;
122 
123 	res32->lvb_offset = res->lvb_offset;
124 	res32->length = res->length;
125 
126 	res32->lksb.sb_status = res->lksb.sb_status;
127 	res32->lksb.sb_flags = res->lksb.sb_flags;
128 	res32->lksb.sb_lkid = res->lksb.sb_lkid;
129 	res32->lksb.sb_lvbptr = (__u32)(long)res->lksb.sb_lvbptr;
130 }
131 #endif
132 
133 /* we could possibly check if the cancel of an orphan has resulted in the lkb
134    being removed and then remove that lkb from the orphans list and free it */
135 
136 void dlm_user_add_ast(struct dlm_lkb *lkb, int type)
137 {
138 	struct dlm_ls *ls;
139 	struct dlm_user_args *ua;
140 	struct dlm_user_proc *proc;
141 	int eol = 0, ast_type;
142 
143 	if (lkb->lkb_flags & (DLM_IFL_ORPHAN | DLM_IFL_DEAD))
144 		return;
145 
146 	ls = lkb->lkb_resource->res_ls;
147 	mutex_lock(&ls->ls_clear_proc_locks);
148 
149 	/* If ORPHAN/DEAD flag is set, it means the process is dead so an ast
150 	   can't be delivered.  For ORPHAN's, dlm_clear_proc_locks() freed
151 	   lkb->ua so we can't try to use it.  This second check is necessary
152 	   for cases where a completion ast is received for an operation that
153 	   began before clear_proc_locks did its cancel/unlock. */
154 
155 	if (lkb->lkb_flags & (DLM_IFL_ORPHAN | DLM_IFL_DEAD))
156 		goto out;
157 
158 	DLM_ASSERT(lkb->lkb_astparam, dlm_print_lkb(lkb););
159 	ua = (struct dlm_user_args *)lkb->lkb_astparam;
160 	proc = ua->proc;
161 
162 	if (type == AST_BAST && ua->bastaddr == NULL)
163 		goto out;
164 
165 	spin_lock(&proc->asts_spin);
166 
167 	ast_type = lkb->lkb_ast_type;
168 	lkb->lkb_ast_type |= type;
169 
170 	if (!ast_type) {
171 		kref_get(&lkb->lkb_ref);
172 		list_add_tail(&lkb->lkb_astqueue, &proc->asts);
173 		wake_up_interruptible(&proc->wait);
174 	}
175 	if (type == AST_COMP && (ast_type & AST_COMP))
176 		log_debug(ls, "ast overlap %x status %x %x",
177 			  lkb->lkb_id, ua->lksb.sb_status, lkb->lkb_flags);
178 
179 	/* Figure out if this lock is at the end of its life and no longer
180 	   available for the application to use.  The lkb still exists until
181 	   the final ast is read.  A lock becomes EOL in three situations:
182 	     1. a noqueue request fails with EAGAIN
183 	     2. an unlock completes with EUNLOCK
184 	     3. a cancel of a waiting request completes with ECANCEL
185 	   An EOL lock needs to be removed from the process's list of locks.
186 	   And we can't allow any new operation on an EOL lock.  This is
187 	   not related to the lifetime of the lkb struct which is managed
188 	   entirely by refcount. */
189 
190 	if (type == AST_COMP &&
191 	    lkb->lkb_grmode == DLM_LOCK_IV &&
192 	    ua->lksb.sb_status == -EAGAIN)
193 		eol = 1;
194 	else if (ua->lksb.sb_status == -DLM_EUNLOCK ||
195 	    (ua->lksb.sb_status == -DLM_ECANCEL &&
196 	     lkb->lkb_grmode == DLM_LOCK_IV))
197 		eol = 1;
198 	if (eol) {
199 		lkb->lkb_ast_type &= ~AST_BAST;
200 		lkb->lkb_flags |= DLM_IFL_ENDOFLIFE;
201 	}
202 
203 	/* We want to copy the lvb to userspace when the completion
204 	   ast is read if the status is 0, the lock has an lvb and
205 	   lvb_ops says we should.  We could probably have set_lvb_lock()
206 	   set update_user_lvb instead and not need old_mode */
207 
208 	if ((lkb->lkb_ast_type & AST_COMP) &&
209 	    (lkb->lkb_lksb->sb_status == 0) &&
210 	    lkb->lkb_lksb->sb_lvbptr &&
211 	    dlm_lvb_operations[ua->old_mode + 1][lkb->lkb_grmode + 1])
212 		ua->update_user_lvb = 1;
213 	else
214 		ua->update_user_lvb = 0;
215 
216 	spin_unlock(&proc->asts_spin);
217 
218 	if (eol) {
219 		spin_lock(&ua->proc->locks_spin);
220 		if (!list_empty(&lkb->lkb_ownqueue)) {
221 			list_del_init(&lkb->lkb_ownqueue);
222 			dlm_put_lkb(lkb);
223 		}
224 		spin_unlock(&ua->proc->locks_spin);
225 	}
226  out:
227 	mutex_unlock(&ls->ls_clear_proc_locks);
228 }
229 
230 static int device_user_lock(struct dlm_user_proc *proc,
231 			    struct dlm_lock_params *params)
232 {
233 	struct dlm_ls *ls;
234 	struct dlm_user_args *ua;
235 	int error = -ENOMEM;
236 
237 	ls = dlm_find_lockspace_local(proc->lockspace);
238 	if (!ls)
239 		return -ENOENT;
240 
241 	if (!params->castaddr || !params->lksb) {
242 		error = -EINVAL;
243 		goto out;
244 	}
245 
246 	ua = kzalloc(sizeof(struct dlm_user_args), GFP_KERNEL);
247 	if (!ua)
248 		goto out;
249 	ua->proc = proc;
250 	ua->user_lksb = params->lksb;
251 	ua->castparam = params->castparam;
252 	ua->castaddr = params->castaddr;
253 	ua->bastparam = params->bastparam;
254 	ua->bastaddr = params->bastaddr;
255 
256 	if (params->flags & DLM_LKF_CONVERT)
257 		error = dlm_user_convert(ls, ua,
258 				         params->mode, params->flags,
259 				         params->lkid, params->lvb);
260 	else {
261 		error = dlm_user_request(ls, ua,
262 					 params->mode, params->flags,
263 					 params->name, params->namelen,
264 					 params->parent);
265 		if (!error)
266 			error = ua->lksb.sb_lkid;
267 	}
268  out:
269 	dlm_put_lockspace(ls);
270 	return error;
271 }
272 
273 static int device_user_unlock(struct dlm_user_proc *proc,
274 			      struct dlm_lock_params *params)
275 {
276 	struct dlm_ls *ls;
277 	struct dlm_user_args *ua;
278 	int error = -ENOMEM;
279 
280 	ls = dlm_find_lockspace_local(proc->lockspace);
281 	if (!ls)
282 		return -ENOENT;
283 
284 	ua = kzalloc(sizeof(struct dlm_user_args), GFP_KERNEL);
285 	if (!ua)
286 		goto out;
287 	ua->proc = proc;
288 	ua->user_lksb = params->lksb;
289 	ua->castparam = params->castparam;
290 	ua->castaddr = params->castaddr;
291 
292 	if (params->flags & DLM_LKF_CANCEL)
293 		error = dlm_user_cancel(ls, ua, params->flags, params->lkid);
294 	else
295 		error = dlm_user_unlock(ls, ua, params->flags, params->lkid,
296 					params->lvb);
297  out:
298 	dlm_put_lockspace(ls);
299 	return error;
300 }
301 
302 static int create_misc_device(struct dlm_ls *ls, char *name)
303 {
304 	int error, len;
305 
306 	error = -ENOMEM;
307 	len = strlen(name) + strlen(name_prefix) + 2;
308 	ls->ls_device.name = kzalloc(len, GFP_KERNEL);
309 	if (!ls->ls_device.name)
310 		goto fail;
311 
312 	snprintf((char *)ls->ls_device.name, len, "%s_%s", name_prefix,
313 		 name);
314 	ls->ls_device.fops = &device_fops;
315 	ls->ls_device.minor = MISC_DYNAMIC_MINOR;
316 
317 	error = misc_register(&ls->ls_device);
318 	if (error) {
319 		kfree(ls->ls_device.name);
320 	}
321 fail:
322 	return error;
323 }
324 
325 static int device_user_purge(struct dlm_user_proc *proc,
326 			     struct dlm_purge_params *params)
327 {
328 	struct dlm_ls *ls;
329 	int error;
330 
331 	ls = dlm_find_lockspace_local(proc->lockspace);
332 	if (!ls)
333 		return -ENOENT;
334 
335 	error = dlm_user_purge(ls, proc, params->nodeid, params->pid);
336 
337 	dlm_put_lockspace(ls);
338 	return error;
339 }
340 
341 static int device_create_lockspace(struct dlm_lspace_params *params)
342 {
343 	dlm_lockspace_t *lockspace;
344 	struct dlm_ls *ls;
345 	int error;
346 
347 	if (!capable(CAP_SYS_ADMIN))
348 		return -EPERM;
349 
350 	error = dlm_new_lockspace(params->name, strlen(params->name),
351 				  &lockspace, 0, DLM_USER_LVB_LEN);
352 	if (error)
353 		return error;
354 
355 	ls = dlm_find_lockspace_local(lockspace);
356 	if (!ls)
357 		return -ENOENT;
358 
359 	error = create_misc_device(ls, params->name);
360 	dlm_put_lockspace(ls);
361 
362 	if (error)
363 		dlm_release_lockspace(lockspace, 0);
364 	else
365 		error = ls->ls_device.minor;
366 
367 	return error;
368 }
369 
370 static int device_remove_lockspace(struct dlm_lspace_params *params)
371 {
372 	dlm_lockspace_t *lockspace;
373 	struct dlm_ls *ls;
374 	int error, force = 0;
375 
376 	if (!capable(CAP_SYS_ADMIN))
377 		return -EPERM;
378 
379 	ls = dlm_find_lockspace_device(params->minor);
380 	if (!ls)
381 		return -ENOENT;
382 
383 	/* Deregister the misc device first, so we don't have
384 	 * a device that's not attached to a lockspace. If
385 	 * dlm_release_lockspace fails then we can recreate it
386 	 */
387 	error = misc_deregister(&ls->ls_device);
388 	if (error) {
389 		dlm_put_lockspace(ls);
390 		goto out;
391 	}
392 	kfree(ls->ls_device.name);
393 
394 	if (params->flags & DLM_USER_LSFLG_FORCEFREE)
395 		force = 2;
396 
397 	lockspace = ls->ls_local_handle;
398 
399 	/* dlm_release_lockspace waits for references to go to zero,
400 	   so all processes will need to close their device for the ls
401 	   before the release will procede */
402 
403 	dlm_put_lockspace(ls);
404 	error = dlm_release_lockspace(lockspace, force);
405 	if (error)
406 		create_misc_device(ls, ls->ls_name);
407  out:
408 	return error;
409 }
410 
411 /* Check the user's version matches ours */
412 static int check_version(struct dlm_write_request *req)
413 {
414 	if (req->version[0] != DLM_DEVICE_VERSION_MAJOR ||
415 	    (req->version[0] == DLM_DEVICE_VERSION_MAJOR &&
416 	     req->version[1] > DLM_DEVICE_VERSION_MINOR)) {
417 
418 		printk(KERN_DEBUG "dlm: process %s (%d) version mismatch "
419 		       "user (%d.%d.%d) kernel (%d.%d.%d)\n",
420 		       current->comm,
421 		       current->pid,
422 		       req->version[0],
423 		       req->version[1],
424 		       req->version[2],
425 		       DLM_DEVICE_VERSION_MAJOR,
426 		       DLM_DEVICE_VERSION_MINOR,
427 		       DLM_DEVICE_VERSION_PATCH);
428 		return -EINVAL;
429 	}
430 	return 0;
431 }
432 
433 /*
434  * device_write
435  *
436  *   device_user_lock
437  *     dlm_user_request -> request_lock
438  *     dlm_user_convert -> convert_lock
439  *
440  *   device_user_unlock
441  *     dlm_user_unlock -> unlock_lock
442  *     dlm_user_cancel -> cancel_lock
443  *
444  *   device_create_lockspace
445  *     dlm_new_lockspace
446  *
447  *   device_remove_lockspace
448  *     dlm_release_lockspace
449  */
450 
451 /* a write to a lockspace device is a lock or unlock request, a write
452    to the control device is to create/remove a lockspace */
453 
454 static ssize_t device_write(struct file *file, const char __user *buf,
455 			    size_t count, loff_t *ppos)
456 {
457 	struct dlm_user_proc *proc = file->private_data;
458 	struct dlm_write_request *kbuf;
459 	sigset_t tmpsig, allsigs;
460 	int error;
461 
462 #ifdef CONFIG_COMPAT
463 	if (count < sizeof(struct dlm_write_request32))
464 #else
465 	if (count < sizeof(struct dlm_write_request))
466 #endif
467 		return -EINVAL;
468 
469 	kbuf = kmalloc(count, GFP_KERNEL);
470 	if (!kbuf)
471 		return -ENOMEM;
472 
473 	if (copy_from_user(kbuf, buf, count)) {
474 		error = -EFAULT;
475 		goto out_free;
476 	}
477 
478 	if (check_version(kbuf)) {
479 		error = -EBADE;
480 		goto out_free;
481 	}
482 
483 #ifdef CONFIG_COMPAT
484 	if (!kbuf->is64bit) {
485 		struct dlm_write_request32 *k32buf;
486 		k32buf = (struct dlm_write_request32 *)kbuf;
487 		kbuf = kmalloc(count + (sizeof(struct dlm_write_request) -
488 			       sizeof(struct dlm_write_request32)), GFP_KERNEL);
489 		if (!kbuf)
490 			return -ENOMEM;
491 
492 		if (proc)
493 			set_bit(DLM_PROC_FLAGS_COMPAT, &proc->flags);
494 		compat_input(kbuf, k32buf);
495 		kfree(k32buf);
496 	}
497 #endif
498 
499 	/* do we really need this? can a write happen after a close? */
500 	if ((kbuf->cmd == DLM_USER_LOCK || kbuf->cmd == DLM_USER_UNLOCK) &&
501 	    test_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags))
502 		return -EINVAL;
503 
504 	sigfillset(&allsigs);
505 	sigprocmask(SIG_BLOCK, &allsigs, &tmpsig);
506 
507 	error = -EINVAL;
508 
509 	switch (kbuf->cmd)
510 	{
511 	case DLM_USER_LOCK:
512 		if (!proc) {
513 			log_print("no locking on control device");
514 			goto out_sig;
515 		}
516 		error = device_user_lock(proc, &kbuf->i.lock);
517 		break;
518 
519 	case DLM_USER_UNLOCK:
520 		if (!proc) {
521 			log_print("no locking on control device");
522 			goto out_sig;
523 		}
524 		error = device_user_unlock(proc, &kbuf->i.lock);
525 		break;
526 
527 	case DLM_USER_CREATE_LOCKSPACE:
528 		if (proc) {
529 			log_print("create/remove only on control device");
530 			goto out_sig;
531 		}
532 		error = device_create_lockspace(&kbuf->i.lspace);
533 		break;
534 
535 	case DLM_USER_REMOVE_LOCKSPACE:
536 		if (proc) {
537 			log_print("create/remove only on control device");
538 			goto out_sig;
539 		}
540 		error = device_remove_lockspace(&kbuf->i.lspace);
541 		break;
542 
543 	case DLM_USER_PURGE:
544 		if (!proc) {
545 			log_print("no locking on control device");
546 			goto out_sig;
547 		}
548 		error = device_user_purge(proc, &kbuf->i.purge);
549 		break;
550 
551 	default:
552 		log_print("Unknown command passed to DLM device : %d\n",
553 			  kbuf->cmd);
554 	}
555 
556  out_sig:
557 	sigprocmask(SIG_SETMASK, &tmpsig, NULL);
558 	recalc_sigpending();
559  out_free:
560 	kfree(kbuf);
561 	return error;
562 }
563 
564 /* Every process that opens the lockspace device has its own "proc" structure
565    hanging off the open file that's used to keep track of locks owned by the
566    process and asts that need to be delivered to the process. */
567 
568 static int device_open(struct inode *inode, struct file *file)
569 {
570 	struct dlm_user_proc *proc;
571 	struct dlm_ls *ls;
572 
573 	ls = dlm_find_lockspace_device(iminor(inode));
574 	if (!ls)
575 		return -ENOENT;
576 
577 	proc = kzalloc(sizeof(struct dlm_user_proc), GFP_KERNEL);
578 	if (!proc) {
579 		dlm_put_lockspace(ls);
580 		return -ENOMEM;
581 	}
582 
583 	proc->lockspace = ls->ls_local_handle;
584 	INIT_LIST_HEAD(&proc->asts);
585 	INIT_LIST_HEAD(&proc->locks);
586 	INIT_LIST_HEAD(&proc->unlocking);
587 	spin_lock_init(&proc->asts_spin);
588 	spin_lock_init(&proc->locks_spin);
589 	init_waitqueue_head(&proc->wait);
590 	file->private_data = proc;
591 
592 	return 0;
593 }
594 
595 static int device_close(struct inode *inode, struct file *file)
596 {
597 	struct dlm_user_proc *proc = file->private_data;
598 	struct dlm_ls *ls;
599 	sigset_t tmpsig, allsigs;
600 
601 	ls = dlm_find_lockspace_local(proc->lockspace);
602 	if (!ls)
603 		return -ENOENT;
604 
605 	sigfillset(&allsigs);
606 	sigprocmask(SIG_BLOCK, &allsigs, &tmpsig);
607 
608 	set_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags);
609 
610 	dlm_clear_proc_locks(ls, proc);
611 
612 	/* at this point no more lkb's should exist for this lockspace,
613 	   so there's no chance of dlm_user_add_ast() being called and
614 	   looking for lkb->ua->proc */
615 
616 	kfree(proc);
617 	file->private_data = NULL;
618 
619 	dlm_put_lockspace(ls);
620 	dlm_put_lockspace(ls);  /* for the find in device_open() */
621 
622 	/* FIXME: AUTOFREE: if this ls is no longer used do
623 	   device_remove_lockspace() */
624 
625 	sigprocmask(SIG_SETMASK, &tmpsig, NULL);
626 	recalc_sigpending();
627 
628 	return 0;
629 }
630 
631 static int copy_result_to_user(struct dlm_user_args *ua, int compat, int type,
632 			       int bmode, char __user *buf, size_t count)
633 {
634 #ifdef CONFIG_COMPAT
635 	struct dlm_lock_result32 result32;
636 #endif
637 	struct dlm_lock_result result;
638 	void *resultptr;
639 	int error=0;
640 	int len;
641 	int struct_len;
642 
643 	memset(&result, 0, sizeof(struct dlm_lock_result));
644 	memcpy(&result.lksb, &ua->lksb, sizeof(struct dlm_lksb));
645 	result.user_lksb = ua->user_lksb;
646 
647 	/* FIXME: dlm1 provides for the user's bastparam/addr to not be updated
648 	   in a conversion unless the conversion is successful.  See code
649 	   in dlm_user_convert() for updating ua from ua_tmp.  OpenVMS, though,
650 	   notes that a new blocking AST address and parameter are set even if
651 	   the conversion fails, so maybe we should just do that. */
652 
653 	if (type == AST_BAST) {
654 		result.user_astaddr = ua->bastaddr;
655 		result.user_astparam = ua->bastparam;
656 		result.bast_mode = bmode;
657 	} else {
658 		result.user_astaddr = ua->castaddr;
659 		result.user_astparam = ua->castparam;
660 	}
661 
662 #ifdef CONFIG_COMPAT
663 	if (compat)
664 		len = sizeof(struct dlm_lock_result32);
665 	else
666 #endif
667 		len = sizeof(struct dlm_lock_result);
668 	struct_len = len;
669 
670 	/* copy lvb to userspace if there is one, it's been updated, and
671 	   the user buffer has space for it */
672 
673 	if (ua->update_user_lvb && ua->lksb.sb_lvbptr &&
674 	    count >= len + DLM_USER_LVB_LEN) {
675 		if (copy_to_user(buf+len, ua->lksb.sb_lvbptr,
676 				 DLM_USER_LVB_LEN)) {
677 			error = -EFAULT;
678 			goto out;
679 		}
680 
681 		result.lvb_offset = len;
682 		len += DLM_USER_LVB_LEN;
683 	}
684 
685 	result.length = len;
686 	resultptr = &result;
687 #ifdef CONFIG_COMPAT
688 	if (compat) {
689 		compat_output(&result, &result32);
690 		resultptr = &result32;
691 	}
692 #endif
693 
694 	if (copy_to_user(buf, resultptr, struct_len))
695 		error = -EFAULT;
696 	else
697 		error = len;
698  out:
699 	return error;
700 }
701 
702 /* a read returns a single ast described in a struct dlm_lock_result */
703 
704 static ssize_t device_read(struct file *file, char __user *buf, size_t count,
705 			   loff_t *ppos)
706 {
707 	struct dlm_user_proc *proc = file->private_data;
708 	struct dlm_lkb *lkb;
709 	struct dlm_user_args *ua;
710 	DECLARE_WAITQUEUE(wait, current);
711 	int error, type=0, bmode=0, removed = 0;
712 
713 #ifdef CONFIG_COMPAT
714 	if (count < sizeof(struct dlm_lock_result32))
715 #else
716 	if (count < sizeof(struct dlm_lock_result))
717 #endif
718 		return -EINVAL;
719 
720 	/* do we really need this? can a read happen after a close? */
721 	if (test_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags))
722 		return -EINVAL;
723 
724 	spin_lock(&proc->asts_spin);
725 	if (list_empty(&proc->asts)) {
726 		if (file->f_flags & O_NONBLOCK) {
727 			spin_unlock(&proc->asts_spin);
728 			return -EAGAIN;
729 		}
730 
731 		add_wait_queue(&proc->wait, &wait);
732 
733 	repeat:
734 		set_current_state(TASK_INTERRUPTIBLE);
735 		if (list_empty(&proc->asts) && !signal_pending(current)) {
736 			spin_unlock(&proc->asts_spin);
737 			schedule();
738 			spin_lock(&proc->asts_spin);
739 			goto repeat;
740 		}
741 		set_current_state(TASK_RUNNING);
742 		remove_wait_queue(&proc->wait, &wait);
743 
744 		if (signal_pending(current)) {
745 			spin_unlock(&proc->asts_spin);
746 			return -ERESTARTSYS;
747 		}
748 	}
749 
750 	if (list_empty(&proc->asts)) {
751 		spin_unlock(&proc->asts_spin);
752 		return -EAGAIN;
753 	}
754 
755 	/* there may be both completion and blocking asts to return for
756 	   the lkb, don't remove lkb from asts list unless no asts remain */
757 
758 	lkb = list_entry(proc->asts.next, struct dlm_lkb, lkb_astqueue);
759 
760 	if (lkb->lkb_ast_type & AST_COMP) {
761 		lkb->lkb_ast_type &= ~AST_COMP;
762 		type = AST_COMP;
763 	} else if (lkb->lkb_ast_type & AST_BAST) {
764 		lkb->lkb_ast_type &= ~AST_BAST;
765 		type = AST_BAST;
766 		bmode = lkb->lkb_bastmode;
767 	}
768 
769 	if (!lkb->lkb_ast_type) {
770 		list_del(&lkb->lkb_astqueue);
771 		removed = 1;
772 	}
773 	spin_unlock(&proc->asts_spin);
774 
775 	ua = (struct dlm_user_args *)lkb->lkb_astparam;
776 	error = copy_result_to_user(ua,
777 			 	test_bit(DLM_PROC_FLAGS_COMPAT, &proc->flags),
778 				type, bmode, buf, count);
779 
780 	/* removes reference for the proc->asts lists added by
781 	   dlm_user_add_ast() and may result in the lkb being freed */
782 	if (removed)
783 		dlm_put_lkb(lkb);
784 
785 	return error;
786 }
787 
788 static unsigned int device_poll(struct file *file, poll_table *wait)
789 {
790 	struct dlm_user_proc *proc = file->private_data;
791 
792 	poll_wait(file, &proc->wait, wait);
793 
794 	spin_lock(&proc->asts_spin);
795 	if (!list_empty(&proc->asts)) {
796 		spin_unlock(&proc->asts_spin);
797 		return POLLIN | POLLRDNORM;
798 	}
799 	spin_unlock(&proc->asts_spin);
800 	return 0;
801 }
802 
803 static int ctl_device_open(struct inode *inode, struct file *file)
804 {
805 	file->private_data = NULL;
806 	return 0;
807 }
808 
809 static int ctl_device_close(struct inode *inode, struct file *file)
810 {
811 	return 0;
812 }
813 
814 static const struct file_operations device_fops = {
815 	.open    = device_open,
816 	.release = device_close,
817 	.read    = device_read,
818 	.write   = device_write,
819 	.poll    = device_poll,
820 	.owner   = THIS_MODULE,
821 };
822 
823 static const struct file_operations ctl_device_fops = {
824 	.open    = ctl_device_open,
825 	.release = ctl_device_close,
826 	.write   = device_write,
827 	.owner   = THIS_MODULE,
828 };
829 
830 int dlm_user_init(void)
831 {
832 	int error;
833 
834 	ctl_device.name = "dlm-control";
835 	ctl_device.fops = &ctl_device_fops;
836 	ctl_device.minor = MISC_DYNAMIC_MINOR;
837 
838 	error = misc_register(&ctl_device);
839 	if (error)
840 		log_print("misc_register failed for control device");
841 
842 	return error;
843 }
844 
845 void dlm_user_exit(void)
846 {
847 	misc_deregister(&ctl_device);
848 }
849 
850