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
27static const char *name_prefix="dlm";
28static struct miscdevice ctl_device;
29static const struct file_operations device_fops;
30
31#ifdef CONFIG_COMPAT
32
33struct 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
50struct 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
63struct dlm_lksb32 {
64	__u32 sb_status;
65	__u32 sb_lkid;
66	__u8 sb_flags;
67	__u32 sb_lvbptr;
68};
69
70struct 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
82static 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
115static 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
136void 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
230static 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
273static 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
302static 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	}
321fail:
322	return error;
323}
324
325static 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
341static 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
370static 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 */
412static 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
454static 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
568static 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
595static 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
623	sigprocmask(SIG_SETMASK, &tmpsig, NULL);
624	recalc_sigpending();
625
626	return 0;
627}
628
629static int copy_result_to_user(struct dlm_user_args *ua, int compat, int type,
630			       int bmode, char __user *buf, size_t count)
631{
632#ifdef CONFIG_COMPAT
633	struct dlm_lock_result32 result32;
634#endif
635	struct dlm_lock_result result;
636	void *resultptr;
637	int error=0;
638	int len;
639	int struct_len;
640
641	memset(&result, 0, sizeof(struct dlm_lock_result));
642	memcpy(&result.lksb, &ua->lksb, sizeof(struct dlm_lksb));
643	result.user_lksb = ua->user_lksb;
644
645
646	if (type == AST_BAST) {
647		result.user_astaddr = ua->bastaddr;
648		result.user_astparam = ua->bastparam;
649		result.bast_mode = bmode;
650	} else {
651		result.user_astaddr = ua->castaddr;
652		result.user_astparam = ua->castparam;
653	}
654
655#ifdef CONFIG_COMPAT
656	if (compat)
657		len = sizeof(struct dlm_lock_result32);
658	else
659#endif
660		len = sizeof(struct dlm_lock_result);
661	struct_len = len;
662
663	/* copy lvb to userspace if there is one, it's been updated, and
664	   the user buffer has space for it */
665
666	if (ua->update_user_lvb && ua->lksb.sb_lvbptr &&
667	    count >= len + DLM_USER_LVB_LEN) {
668		if (copy_to_user(buf+len, ua->lksb.sb_lvbptr,
669				 DLM_USER_LVB_LEN)) {
670			error = -EFAULT;
671			goto out;
672		}
673
674		result.lvb_offset = len;
675		len += DLM_USER_LVB_LEN;
676	}
677
678	result.length = len;
679	resultptr = &result;
680#ifdef CONFIG_COMPAT
681	if (compat) {
682		compat_output(&result, &result32);
683		resultptr = &result32;
684	}
685#endif
686
687	if (copy_to_user(buf, resultptr, struct_len))
688		error = -EFAULT;
689	else
690		error = len;
691 out:
692	return error;
693}
694
695/* a read returns a single ast described in a struct dlm_lock_result */
696
697static ssize_t device_read(struct file *file, char __user *buf, size_t count,
698			   loff_t *ppos)
699{
700	struct dlm_user_proc *proc = file->private_data;
701	struct dlm_lkb *lkb;
702	struct dlm_user_args *ua;
703	DECLARE_WAITQUEUE(wait, current);
704	int error, type=0, bmode=0, removed = 0;
705
706#ifdef CONFIG_COMPAT
707	if (count < sizeof(struct dlm_lock_result32))
708#else
709	if (count < sizeof(struct dlm_lock_result))
710#endif
711		return -EINVAL;
712
713	/* do we really need this? can a read happen after a close? */
714	if (test_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags))
715		return -EINVAL;
716
717	spin_lock(&proc->asts_spin);
718	if (list_empty(&proc->asts)) {
719		if (file->f_flags & O_NONBLOCK) {
720			spin_unlock(&proc->asts_spin);
721			return -EAGAIN;
722		}
723
724		add_wait_queue(&proc->wait, &wait);
725
726	repeat:
727		set_current_state(TASK_INTERRUPTIBLE);
728		if (list_empty(&proc->asts) && !signal_pending(current)) {
729			spin_unlock(&proc->asts_spin);
730			schedule();
731			spin_lock(&proc->asts_spin);
732			goto repeat;
733		}
734		set_current_state(TASK_RUNNING);
735		remove_wait_queue(&proc->wait, &wait);
736
737		if (signal_pending(current)) {
738			spin_unlock(&proc->asts_spin);
739			return -ERESTARTSYS;
740		}
741	}
742
743	if (list_empty(&proc->asts)) {
744		spin_unlock(&proc->asts_spin);
745		return -EAGAIN;
746	}
747
748	/* there may be both completion and blocking asts to return for
749	   the lkb, don't remove lkb from asts list unless no asts remain */
750
751	lkb = list_entry(proc->asts.next, struct dlm_lkb, lkb_astqueue);
752
753	if (lkb->lkb_ast_type & AST_COMP) {
754		lkb->lkb_ast_type &= ~AST_COMP;
755		type = AST_COMP;
756	} else if (lkb->lkb_ast_type & AST_BAST) {
757		lkb->lkb_ast_type &= ~AST_BAST;
758		type = AST_BAST;
759		bmode = lkb->lkb_bastmode;
760	}
761
762	if (!lkb->lkb_ast_type) {
763		list_del(&lkb->lkb_astqueue);
764		removed = 1;
765	}
766	spin_unlock(&proc->asts_spin);
767
768	ua = (struct dlm_user_args *)lkb->lkb_astparam;
769	error = copy_result_to_user(ua,
770			 	test_bit(DLM_PROC_FLAGS_COMPAT, &proc->flags),
771				type, bmode, buf, count);
772
773	/* removes reference for the proc->asts lists added by
774	   dlm_user_add_ast() and may result in the lkb being freed */
775	if (removed)
776		dlm_put_lkb(lkb);
777
778	return error;
779}
780
781static unsigned int device_poll(struct file *file, poll_table *wait)
782{
783	struct dlm_user_proc *proc = file->private_data;
784
785	poll_wait(file, &proc->wait, wait);
786
787	spin_lock(&proc->asts_spin);
788	if (!list_empty(&proc->asts)) {
789		spin_unlock(&proc->asts_spin);
790		return POLLIN | POLLRDNORM;
791	}
792	spin_unlock(&proc->asts_spin);
793	return 0;
794}
795
796static int ctl_device_open(struct inode *inode, struct file *file)
797{
798	file->private_data = NULL;
799	return 0;
800}
801
802static int ctl_device_close(struct inode *inode, struct file *file)
803{
804	return 0;
805}
806
807static const struct file_operations device_fops = {
808	.open    = device_open,
809	.release = device_close,
810	.read    = device_read,
811	.write   = device_write,
812	.poll    = device_poll,
813	.owner   = THIS_MODULE,
814};
815
816static const struct file_operations ctl_device_fops = {
817	.open    = ctl_device_open,
818	.release = ctl_device_close,
819	.write   = device_write,
820	.owner   = THIS_MODULE,
821};
822
823int dlm_user_init(void)
824{
825	int error;
826
827	ctl_device.name = "dlm-control";
828	ctl_device.fops = &ctl_device_fops;
829	ctl_device.minor = MISC_DYNAMIC_MINOR;
830
831	error = misc_register(&ctl_device);
832	if (error)
833		log_print("misc_register failed for control device");
834
835	return error;
836}
837
838void dlm_user_exit(void)
839{
840	misc_deregister(&ctl_device);
841}
842