2 * Copyright (C) 2006-2010 Red Hat, Inc. All rights reserved.
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.
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>
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 #include <linux/slab.h>
22 #include "dlm_internal.h"
23 #include "lockspace.h"
25 #include "lvb_table.h"
28 static const char name_prefix
[] = "dlm";
29 static const struct file_operations device_fops
;
30 static atomic_t dlm_monitor_opened
;
31 static int dlm_monitor_unused
= 1;
35 struct dlm_lock_params32
{
49 char lvb
[DLM_USER_LVB_LEN
];
53 struct dlm_write_request32
{
60 struct dlm_lock_params32 lock
;
61 struct dlm_lspace_params lspace
;
62 struct dlm_purge_params purge
;
73 struct dlm_lock_result32
{
79 struct dlm_lksb32 lksb
;
82 /* Offsets may be zero if no data is present */
86 static void compat_input(struct dlm_write_request
*kb
,
87 struct dlm_write_request32
*kb32
,
90 kb
->version
[0] = kb32
->version
[0];
91 kb
->version
[1] = kb32
->version
[1];
92 kb
->version
[2] = kb32
->version
[2];
95 kb
->is64bit
= kb32
->is64bit
;
96 if (kb
->cmd
== DLM_USER_CREATE_LOCKSPACE
||
97 kb
->cmd
== DLM_USER_REMOVE_LOCKSPACE
) {
98 kb
->i
.lspace
.flags
= kb32
->i
.lspace
.flags
;
99 kb
->i
.lspace
.minor
= kb32
->i
.lspace
.minor
;
100 memcpy(kb
->i
.lspace
.name
, kb32
->i
.lspace
.name
, namelen
);
101 } else if (kb
->cmd
== DLM_USER_PURGE
) {
102 kb
->i
.purge
.nodeid
= kb32
->i
.purge
.nodeid
;
103 kb
->i
.purge
.pid
= kb32
->i
.purge
.pid
;
105 kb
->i
.lock
.mode
= kb32
->i
.lock
.mode
;
106 kb
->i
.lock
.namelen
= kb32
->i
.lock
.namelen
;
107 kb
->i
.lock
.flags
= kb32
->i
.lock
.flags
;
108 kb
->i
.lock
.lkid
= kb32
->i
.lock
.lkid
;
109 kb
->i
.lock
.parent
= kb32
->i
.lock
.parent
;
110 kb
->i
.lock
.xid
= kb32
->i
.lock
.xid
;
111 kb
->i
.lock
.timeout
= kb32
->i
.lock
.timeout
;
112 kb
->i
.lock
.castparam
= (void *)(long)kb32
->i
.lock
.castparam
;
113 kb
->i
.lock
.castaddr
= (void *)(long)kb32
->i
.lock
.castaddr
;
114 kb
->i
.lock
.bastparam
= (void *)(long)kb32
->i
.lock
.bastparam
;
115 kb
->i
.lock
.bastaddr
= (void *)(long)kb32
->i
.lock
.bastaddr
;
116 kb
->i
.lock
.lksb
= (void *)(long)kb32
->i
.lock
.lksb
;
117 memcpy(kb
->i
.lock
.lvb
, kb32
->i
.lock
.lvb
, DLM_USER_LVB_LEN
);
118 memcpy(kb
->i
.lock
.name
, kb32
->i
.lock
.name
, namelen
);
122 static void compat_output(struct dlm_lock_result
*res
,
123 struct dlm_lock_result32
*res32
)
125 res32
->version
[0] = res
->version
[0];
126 res32
->version
[1] = res
->version
[1];
127 res32
->version
[2] = res
->version
[2];
129 res32
->user_astaddr
= (__u32
)(long)res
->user_astaddr
;
130 res32
->user_astparam
= (__u32
)(long)res
->user_astparam
;
131 res32
->user_lksb
= (__u32
)(long)res
->user_lksb
;
132 res32
->bast_mode
= res
->bast_mode
;
134 res32
->lvb_offset
= res
->lvb_offset
;
135 res32
->length
= res
->length
;
137 res32
->lksb
.sb_status
= res
->lksb
.sb_status
;
138 res32
->lksb
.sb_flags
= res
->lksb
.sb_flags
;
139 res32
->lksb
.sb_lkid
= res
->lksb
.sb_lkid
;
140 res32
->lksb
.sb_lvbptr
= (__u32
)(long)res
->lksb
.sb_lvbptr
;
144 /* Figure out if this lock is at the end of its life and no longer
145 available for the application to use. The lkb still exists until
146 the final ast is read. A lock becomes EOL in three situations:
147 1. a noqueue request fails with EAGAIN
148 2. an unlock completes with EUNLOCK
149 3. a cancel of a waiting request completes with ECANCEL/EDEADLK
150 An EOL lock needs to be removed from the process's list of locks.
151 And we can't allow any new operation on an EOL lock. This is
152 not related to the lifetime of the lkb struct which is managed
153 entirely by refcount. */
155 static int lkb_is_endoflife(struct dlm_lkb
*lkb
, int sb_status
, int type
)
163 if (lkb
->lkb_grmode
== DLM_LOCK_IV
)
167 if (type
== AST_COMP
&& lkb
->lkb_grmode
== DLM_LOCK_IV
)
174 /* we could possibly check if the cancel of an orphan has resulted in the lkb
175 being removed and then remove that lkb from the orphans list and free it */
177 void dlm_user_add_ast(struct dlm_lkb
*lkb
, int type
, int mode
)
180 struct dlm_user_args
*ua
;
181 struct dlm_user_proc
*proc
;
182 int eol
= 0, ast_type
;
184 if (lkb
->lkb_flags
& (DLM_IFL_ORPHAN
| DLM_IFL_DEAD
))
187 ls
= lkb
->lkb_resource
->res_ls
;
188 mutex_lock(&ls
->ls_clear_proc_locks
);
190 /* If ORPHAN/DEAD flag is set, it means the process is dead so an ast
191 can't be delivered. For ORPHAN's, dlm_clear_proc_locks() freed
192 lkb->ua so we can't try to use it. This second check is necessary
193 for cases where a completion ast is received for an operation that
194 began before clear_proc_locks did its cancel/unlock. */
196 if (lkb
->lkb_flags
& (DLM_IFL_ORPHAN
| DLM_IFL_DEAD
))
199 DLM_ASSERT(lkb
->lkb_ua
, dlm_print_lkb(lkb
););
203 if (type
== AST_BAST
&& ua
->bastaddr
== NULL
)
206 spin_lock(&proc
->asts_spin
);
208 ast_type
= lkb
->lkb_ast_type
;
209 lkb
->lkb_ast_type
|= type
;
210 if (type
== AST_BAST
)
211 lkb
->lkb_bastmode
= mode
;
213 lkb
->lkb_castmode
= mode
;
216 kref_get(&lkb
->lkb_ref
);
217 list_add_tail(&lkb
->lkb_astqueue
, &proc
->asts
);
218 lkb
->lkb_ast_first
= type
;
219 wake_up_interruptible(&proc
->wait
);
221 if (type
== AST_COMP
&& (ast_type
& AST_COMP
))
222 log_debug(ls
, "ast overlap %x status %x %x",
223 lkb
->lkb_id
, ua
->lksb
.sb_status
, lkb
->lkb_flags
);
225 eol
= lkb_is_endoflife(lkb
, ua
->lksb
.sb_status
, type
);
227 lkb
->lkb_flags
|= DLM_IFL_ENDOFLIFE
;
230 /* We want to copy the lvb to userspace when the completion
231 ast is read if the status is 0, the lock has an lvb and
232 lvb_ops says we should. We could probably have set_lvb_lock()
233 set update_user_lvb instead and not need old_mode */
235 if ((lkb
->lkb_ast_type
& AST_COMP
) &&
236 (lkb
->lkb_lksb
->sb_status
== 0) &&
237 lkb
->lkb_lksb
->sb_lvbptr
&&
238 dlm_lvb_operations
[ua
->old_mode
+ 1][lkb
->lkb_grmode
+ 1])
239 ua
->update_user_lvb
= 1;
241 ua
->update_user_lvb
= 0;
243 spin_unlock(&proc
->asts_spin
);
246 spin_lock(&proc
->locks_spin
);
247 if (!list_empty(&lkb
->lkb_ownqueue
)) {
248 list_del_init(&lkb
->lkb_ownqueue
);
251 spin_unlock(&proc
->locks_spin
);
254 mutex_unlock(&ls
->ls_clear_proc_locks
);
257 static int device_user_lock(struct dlm_user_proc
*proc
,
258 struct dlm_lock_params
*params
)
261 struct dlm_user_args
*ua
;
264 ls
= dlm_find_lockspace_local(proc
->lockspace
);
268 if (!params
->castaddr
|| !params
->lksb
) {
273 ua
= kzalloc(sizeof(struct dlm_user_args
), GFP_NOFS
);
277 ua
->user_lksb
= params
->lksb
;
278 ua
->castparam
= params
->castparam
;
279 ua
->castaddr
= params
->castaddr
;
280 ua
->bastparam
= params
->bastparam
;
281 ua
->bastaddr
= params
->bastaddr
;
282 ua
->xid
= params
->xid
;
284 if (params
->flags
& DLM_LKF_CONVERT
)
285 error
= dlm_user_convert(ls
, ua
,
286 params
->mode
, params
->flags
,
287 params
->lkid
, params
->lvb
,
288 (unsigned long) params
->timeout
);
290 error
= dlm_user_request(ls
, ua
,
291 params
->mode
, params
->flags
,
292 params
->name
, params
->namelen
,
293 (unsigned long) params
->timeout
);
295 error
= ua
->lksb
.sb_lkid
;
298 dlm_put_lockspace(ls
);
302 static int device_user_unlock(struct dlm_user_proc
*proc
,
303 struct dlm_lock_params
*params
)
306 struct dlm_user_args
*ua
;
309 ls
= dlm_find_lockspace_local(proc
->lockspace
);
313 ua
= kzalloc(sizeof(struct dlm_user_args
), GFP_NOFS
);
317 ua
->user_lksb
= params
->lksb
;
318 ua
->castparam
= params
->castparam
;
319 ua
->castaddr
= params
->castaddr
;
321 if (params
->flags
& DLM_LKF_CANCEL
)
322 error
= dlm_user_cancel(ls
, ua
, params
->flags
, params
->lkid
);
324 error
= dlm_user_unlock(ls
, ua
, params
->flags
, params
->lkid
,
327 dlm_put_lockspace(ls
);
331 static int device_user_deadlock(struct dlm_user_proc
*proc
,
332 struct dlm_lock_params
*params
)
337 ls
= dlm_find_lockspace_local(proc
->lockspace
);
341 error
= dlm_user_deadlock(ls
, params
->flags
, params
->lkid
);
343 dlm_put_lockspace(ls
);
347 static int dlm_device_register(struct dlm_ls
*ls
, char *name
)
351 /* The device is already registered. This happens when the
352 lockspace is created multiple times from userspace. */
353 if (ls
->ls_device
.name
)
357 len
= strlen(name
) + strlen(name_prefix
) + 2;
358 ls
->ls_device
.name
= kzalloc(len
, GFP_NOFS
);
359 if (!ls
->ls_device
.name
)
362 snprintf((char *)ls
->ls_device
.name
, len
, "%s_%s", name_prefix
,
364 ls
->ls_device
.fops
= &device_fops
;
365 ls
->ls_device
.minor
= MISC_DYNAMIC_MINOR
;
367 error
= misc_register(&ls
->ls_device
);
369 kfree(ls
->ls_device
.name
);
375 int dlm_device_deregister(struct dlm_ls
*ls
)
379 /* The device is not registered. This happens when the lockspace
380 was never used from userspace, or when device_create_lockspace()
381 calls dlm_release_lockspace() after the register fails. */
382 if (!ls
->ls_device
.name
)
385 error
= misc_deregister(&ls
->ls_device
);
387 kfree(ls
->ls_device
.name
);
391 static int device_user_purge(struct dlm_user_proc
*proc
,
392 struct dlm_purge_params
*params
)
397 ls
= dlm_find_lockspace_local(proc
->lockspace
);
401 error
= dlm_user_purge(ls
, proc
, params
->nodeid
, params
->pid
);
403 dlm_put_lockspace(ls
);
407 static int device_create_lockspace(struct dlm_lspace_params
*params
)
409 dlm_lockspace_t
*lockspace
;
413 if (!capable(CAP_SYS_ADMIN
))
416 error
= dlm_new_lockspace(params
->name
, strlen(params
->name
),
417 &lockspace
, params
->flags
, DLM_USER_LVB_LEN
);
421 ls
= dlm_find_lockspace_local(lockspace
);
425 error
= dlm_device_register(ls
, params
->name
);
426 dlm_put_lockspace(ls
);
429 dlm_release_lockspace(lockspace
, 0);
431 error
= ls
->ls_device
.minor
;
436 static int device_remove_lockspace(struct dlm_lspace_params
*params
)
438 dlm_lockspace_t
*lockspace
;
440 int error
, force
= 0;
442 if (!capable(CAP_SYS_ADMIN
))
445 ls
= dlm_find_lockspace_device(params
->minor
);
449 if (params
->flags
& DLM_USER_LSFLG_FORCEFREE
)
452 lockspace
= ls
->ls_local_handle
;
453 dlm_put_lockspace(ls
);
455 /* The final dlm_release_lockspace waits for references to go to
456 zero, so all processes will need to close their device for the
457 ls before the release will proceed. release also calls the
458 device_deregister above. Converting a positive return value
459 from release to zero means that userspace won't know when its
460 release was the final one, but it shouldn't need to know. */
462 error
= dlm_release_lockspace(lockspace
, force
);
468 /* Check the user's version matches ours */
469 static int check_version(struct dlm_write_request
*req
)
471 if (req
->version
[0] != DLM_DEVICE_VERSION_MAJOR
||
472 (req
->version
[0] == DLM_DEVICE_VERSION_MAJOR
&&
473 req
->version
[1] > DLM_DEVICE_VERSION_MINOR
)) {
475 printk(KERN_DEBUG
"dlm: process %s (%d) version mismatch "
476 "user (%d.%d.%d) kernel (%d.%d.%d)\n",
478 task_pid_nr(current
),
482 DLM_DEVICE_VERSION_MAJOR
,
483 DLM_DEVICE_VERSION_MINOR
,
484 DLM_DEVICE_VERSION_PATCH
);
494 * dlm_user_request -> request_lock
495 * dlm_user_convert -> convert_lock
498 * dlm_user_unlock -> unlock_lock
499 * dlm_user_cancel -> cancel_lock
501 * device_create_lockspace
504 * device_remove_lockspace
505 * dlm_release_lockspace
508 /* a write to a lockspace device is a lock or unlock request, a write
509 to the control device is to create/remove a lockspace */
511 static ssize_t
device_write(struct file
*file
, const char __user
*buf
,
512 size_t count
, loff_t
*ppos
)
514 struct dlm_user_proc
*proc
= file
->private_data
;
515 struct dlm_write_request
*kbuf
;
516 sigset_t tmpsig
, allsigs
;
520 if (count
< sizeof(struct dlm_write_request32
))
522 if (count
< sizeof(struct dlm_write_request
))
526 kbuf
= kzalloc(count
+ 1, GFP_NOFS
);
530 if (copy_from_user(kbuf
, buf
, count
)) {
535 if (check_version(kbuf
)) {
541 if (!kbuf
->is64bit
) {
542 struct dlm_write_request32
*k32buf
;
545 if (count
> sizeof(struct dlm_write_request32
))
546 namelen
= count
- sizeof(struct dlm_write_request32
);
548 k32buf
= (struct dlm_write_request32
*)kbuf
;
550 /* add 1 after namelen so that the name string is terminated */
551 kbuf
= kzalloc(sizeof(struct dlm_write_request
) + namelen
+ 1,
559 set_bit(DLM_PROC_FLAGS_COMPAT
, &proc
->flags
);
561 compat_input(kbuf
, k32buf
, namelen
);
566 /* do we really need this? can a write happen after a close? */
567 if ((kbuf
->cmd
== DLM_USER_LOCK
|| kbuf
->cmd
== DLM_USER_UNLOCK
) &&
568 (proc
&& test_bit(DLM_PROC_FLAGS_CLOSING
, &proc
->flags
))) {
573 sigfillset(&allsigs
);
574 sigprocmask(SIG_BLOCK
, &allsigs
, &tmpsig
);
582 log_print("no locking on control device");
585 error
= device_user_lock(proc
, &kbuf
->i
.lock
);
588 case DLM_USER_UNLOCK
:
590 log_print("no locking on control device");
593 error
= device_user_unlock(proc
, &kbuf
->i
.lock
);
596 case DLM_USER_DEADLOCK
:
598 log_print("no locking on control device");
601 error
= device_user_deadlock(proc
, &kbuf
->i
.lock
);
604 case DLM_USER_CREATE_LOCKSPACE
:
606 log_print("create/remove only on control device");
609 error
= device_create_lockspace(&kbuf
->i
.lspace
);
612 case DLM_USER_REMOVE_LOCKSPACE
:
614 log_print("create/remove only on control device");
617 error
= device_remove_lockspace(&kbuf
->i
.lspace
);
622 log_print("no locking on control device");
625 error
= device_user_purge(proc
, &kbuf
->i
.purge
);
629 log_print("Unknown command passed to DLM device : %d\n",
634 sigprocmask(SIG_SETMASK
, &tmpsig
, NULL
);
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. */
645 static int device_open(struct inode
*inode
, struct file
*file
)
647 struct dlm_user_proc
*proc
;
650 ls
= dlm_find_lockspace_device(iminor(inode
));
654 proc
= kzalloc(sizeof(struct dlm_user_proc
), GFP_NOFS
);
656 dlm_put_lockspace(ls
);
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
;
672 static int device_close(struct inode
*inode
, struct file
*file
)
674 struct dlm_user_proc
*proc
= file
->private_data
;
676 sigset_t tmpsig
, allsigs
;
678 ls
= dlm_find_lockspace_local(proc
->lockspace
);
682 sigfillset(&allsigs
);
683 sigprocmask(SIG_BLOCK
, &allsigs
, &tmpsig
);
685 set_bit(DLM_PROC_FLAGS_CLOSING
, &proc
->flags
);
687 dlm_clear_proc_locks(ls
, proc
);
689 /* at this point no more lkb's should exist for this lockspace,
690 so there's no chance of dlm_user_add_ast() being called and
691 looking for lkb->ua->proc */
694 file
->private_data
= NULL
;
696 dlm_put_lockspace(ls
);
697 dlm_put_lockspace(ls
); /* for the find in device_open() */
700 sigprocmask(SIG_SETMASK
, &tmpsig
, NULL
);
706 static int copy_result_to_user(struct dlm_user_args
*ua
, int compat
, int type
,
707 int mode
, char __user
*buf
, size_t count
)
710 struct dlm_lock_result32 result32
;
712 struct dlm_lock_result result
;
718 memset(&result
, 0, sizeof(struct dlm_lock_result
));
719 result
.version
[0] = DLM_DEVICE_VERSION_MAJOR
;
720 result
.version
[1] = DLM_DEVICE_VERSION_MINOR
;
721 result
.version
[2] = DLM_DEVICE_VERSION_PATCH
;
722 memcpy(&result
.lksb
, &ua
->lksb
, sizeof(struct dlm_lksb
));
723 result
.user_lksb
= ua
->user_lksb
;
726 if (type
== AST_BAST
) {
727 result
.user_astaddr
= ua
->bastaddr
;
728 result
.user_astparam
= ua
->bastparam
;
729 result
.bast_mode
= mode
;
731 result
.user_astaddr
= ua
->castaddr
;
732 result
.user_astparam
= ua
->castparam
;
737 len
= sizeof(struct dlm_lock_result32
);
740 len
= sizeof(struct dlm_lock_result
);
743 /* copy lvb to userspace if there is one, it's been updated, and
744 the user buffer has space for it */
746 if (ua
->update_user_lvb
&& ua
->lksb
.sb_lvbptr
&&
747 count
>= len
+ DLM_USER_LVB_LEN
) {
748 if (copy_to_user(buf
+len
, ua
->lksb
.sb_lvbptr
,
754 result
.lvb_offset
= len
;
755 len
+= DLM_USER_LVB_LEN
;
762 compat_output(&result
, &result32
);
763 resultptr
= &result32
;
767 if (copy_to_user(buf
, resultptr
, struct_len
))
775 static int copy_version_to_user(char __user
*buf
, size_t count
)
777 struct dlm_device_version ver
;
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
;
784 if (copy_to_user(buf
, &ver
, sizeof(struct dlm_device_version
)))
786 return sizeof(struct dlm_device_version
);
789 /* a read returns a single ast described in a struct dlm_lock_result */
791 static ssize_t
device_read(struct file
*file
, char __user
*buf
, size_t count
,
794 struct dlm_user_proc
*proc
= file
->private_data
;
796 DECLARE_WAITQUEUE(wait
, current
);
797 int error
= 0, removed
;
798 int ret_type
, ret_mode
;
799 int bastmode
, castmode
, do_bast
, do_cast
;
801 if (count
== sizeof(struct dlm_device_version
)) {
802 error
= copy_version_to_user(buf
, count
);
807 log_print("non-version read from control device %zu", count
);
812 if (count
< sizeof(struct dlm_lock_result32
))
814 if (count
< sizeof(struct dlm_lock_result
))
820 /* do we really need this? can a read happen after a close? */
821 if (test_bit(DLM_PROC_FLAGS_CLOSING
, &proc
->flags
))
824 spin_lock(&proc
->asts_spin
);
825 if (list_empty(&proc
->asts
)) {
826 if (file
->f_flags
& O_NONBLOCK
) {
827 spin_unlock(&proc
->asts_spin
);
831 add_wait_queue(&proc
->wait
, &wait
);
834 set_current_state(TASK_INTERRUPTIBLE
);
835 if (list_empty(&proc
->asts
) && !signal_pending(current
)) {
836 spin_unlock(&proc
->asts_spin
);
838 spin_lock(&proc
->asts_spin
);
841 set_current_state(TASK_RUNNING
);
842 remove_wait_queue(&proc
->wait
, &wait
);
844 if (signal_pending(current
)) {
845 spin_unlock(&proc
->asts_spin
);
850 /* there may be both completion and blocking asts to return for
851 the lkb, don't remove lkb from asts list unless no asts remain */
853 lkb
= list_entry(proc
->asts
.next
, struct dlm_lkb
, lkb_astqueue
);
858 do_bast
= lkb
->lkb_ast_type
& AST_BAST
;
859 do_cast
= lkb
->lkb_ast_type
& AST_COMP
;
860 bastmode
= lkb
->lkb_bastmode
;
861 castmode
= lkb
->lkb_castmode
;
863 /* when both are queued figure out which to do first and
864 switch first so the other goes in the next read */
866 if (do_cast
&& do_bast
) {
867 if (lkb
->lkb_ast_first
== AST_COMP
) {
870 lkb
->lkb_ast_type
&= ~AST_COMP
;
871 lkb
->lkb_ast_first
= AST_BAST
;
875 lkb
->lkb_ast_type
&= ~AST_BAST
;
876 lkb
->lkb_ast_first
= AST_COMP
;
879 ret_type
= lkb
->lkb_ast_first
;
880 ret_mode
= (ret_type
== AST_COMP
) ? castmode
: bastmode
;
881 lkb
->lkb_ast_type
&= ~ret_type
;
882 lkb
->lkb_ast_first
= 0;
885 /* if we're doing a bast but the bast is unnecessary, then
886 switch to do nothing or do a cast if that was needed next */
888 if ((ret_type
== AST_BAST
) &&
889 dlm_modes_compat(bastmode
, lkb
->lkb_castmode_done
)) {
896 lkb
->lkb_ast_type
&= ~AST_COMP
;
897 lkb
->lkb_ast_first
= 0;
901 if (lkb
->lkb_ast_first
!= lkb
->lkb_ast_type
) {
902 log_print("device_read %x ast_first %x ast_type %x",
903 lkb
->lkb_id
, lkb
->lkb_ast_first
, lkb
->lkb_ast_type
);
906 if (!lkb
->lkb_ast_type
) {
907 list_del(&lkb
->lkb_astqueue
);
910 spin_unlock(&proc
->asts_spin
);
913 error
= copy_result_to_user(lkb
->lkb_ua
,
914 test_bit(DLM_PROC_FLAGS_COMPAT
, &proc
->flags
),
915 ret_type
, ret_mode
, buf
, count
);
917 if (ret_type
== AST_COMP
)
918 lkb
->lkb_castmode_done
= castmode
;
919 if (ret_type
== AST_BAST
)
920 lkb
->lkb_bastmode_done
= bastmode
;
923 /* removes reference for the proc->asts lists added by
924 dlm_user_add_ast() and may result in the lkb being freed */
929 /* the bast that was queued was eliminated (see unnecessary above),
930 leaving nothing to return */
938 static unsigned int device_poll(struct file
*file
, poll_table
*wait
)
940 struct dlm_user_proc
*proc
= file
->private_data
;
942 poll_wait(file
, &proc
->wait
, wait
);
944 spin_lock(&proc
->asts_spin
);
945 if (!list_empty(&proc
->asts
)) {
946 spin_unlock(&proc
->asts_spin
);
947 return POLLIN
| POLLRDNORM
;
949 spin_unlock(&proc
->asts_spin
);
953 int dlm_user_daemon_available(void)
955 /* dlm_controld hasn't started (or, has started, but not
956 properly populated configfs) */
958 if (!dlm_our_nodeid())
961 /* This is to deal with versions of dlm_controld that don't
962 know about the monitor device. We assume that if the
963 dlm_controld was started (above), but the monitor device
964 was never opened, that it's an old version. dlm_controld
965 should open the monitor device before populating configfs. */
967 if (dlm_monitor_unused
)
970 return atomic_read(&dlm_monitor_opened
) ? 1 : 0;
973 static int ctl_device_open(struct inode
*inode
, struct file
*file
)
975 file
->private_data
= NULL
;
979 static int ctl_device_close(struct inode
*inode
, struct file
*file
)
984 static int monitor_device_open(struct inode
*inode
, struct file
*file
)
986 atomic_inc(&dlm_monitor_opened
);
987 dlm_monitor_unused
= 0;
991 static int monitor_device_close(struct inode
*inode
, struct file
*file
)
993 if (atomic_dec_and_test(&dlm_monitor_opened
))
994 dlm_stop_lockspaces();
998 static const struct file_operations device_fops
= {
1000 .release
= device_close
,
1001 .read
= device_read
,
1002 .write
= device_write
,
1003 .poll
= device_poll
,
1004 .owner
= THIS_MODULE
,
1007 static const struct file_operations ctl_device_fops
= {
1008 .open
= ctl_device_open
,
1009 .release
= ctl_device_close
,
1010 .read
= device_read
,
1011 .write
= device_write
,
1012 .owner
= THIS_MODULE
,
1015 static struct miscdevice ctl_device
= {
1016 .name
= "dlm-control",
1017 .fops
= &ctl_device_fops
,
1018 .minor
= MISC_DYNAMIC_MINOR
,
1021 static const struct file_operations monitor_device_fops
= {
1022 .open
= monitor_device_open
,
1023 .release
= monitor_device_close
,
1024 .owner
= THIS_MODULE
,
1027 static struct miscdevice monitor_device
= {
1028 .name
= "dlm-monitor",
1029 .fops
= &monitor_device_fops
,
1030 .minor
= MISC_DYNAMIC_MINOR
,
1033 int __init
dlm_user_init(void)
1037 atomic_set(&dlm_monitor_opened
, 0);
1039 error
= misc_register(&ctl_device
);
1041 log_print("misc_register failed for control device");
1045 error
= misc_register(&monitor_device
);
1047 log_print("misc_register failed for monitor device");
1048 misc_deregister(&ctl_device
);
1054 void dlm_user_exit(void)
1056 misc_deregister(&ctl_device
);
1057 misc_deregister(&monitor_device
);