1 /******************************************************************************
2 *******************************************************************************
4 ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
5 ** Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
7 ** This copyrighted material is made available to anyone wishing to use,
8 ** modify, copy, or redistribute it subject to the terms and conditions
9 ** of the GNU General Public License v.2.
11 *******************************************************************************
12 ******************************************************************************/
17 * This is the userland interface to the DLM.
19 * The locking is done via a misc char device (find the
20 * registered minor number in /proc/misc).
22 * User code should not use this interface directly but
23 * call the library routines in libdlm.a instead.
27 #include <linux/miscdevice.h>
28 #include <linux/init.h>
29 #include <linux/wait.h>
30 #include <linux/module.h>
31 #include <linux/file.h>
33 #include <linux/poll.h>
34 #include <linux/signal.h>
35 #include <linux/spinlock.h>
36 #include <linux/idr.h>
38 #include <linux/dlm.h>
39 #include <linux/dlm_device.h>
41 #include "lvb_table.h"
43 static struct file_operations _dlm_fops;
44 static const char *name_prefix="dlm";
45 static struct list_head user_ls_list;
46 static struct mutex user_ls_lock;
48 /* Lock infos are stored in here indexed by lock ID */
49 static DEFINE_IDR(lockinfo_idr);
50 static rwlock_t lockinfo_lock;
52 /* Flags in li_flags */
53 #define LI_FLAG_COMPLETE 1
54 #define LI_FLAG_FIRSTLOCK 2
55 #define LI_FLAG_PERSISTENT 3
56 #define LI_FLAG_ONLIST 4
58 /* flags in ls_flags*/
59 #define LS_FLAG_DELETED 1
60 #define LS_FLAG_AUTOFREE 2
62 /* flags in ls_flags*/
63 #define FI_FLAG_OPEN 1
64 #define FI_FLAG_COMPAT 2
66 #define LOCKINFO_MAGIC 0x53595324
73 struct dlm_lksb li_lksb;
74 wait_queue_head_t li_waitq;
75 unsigned long li_flags;
76 void __user *li_castparam;
77 void __user *li_castaddr;
78 void __user *li_bastparam;
79 void __user *li_bastaddr;
80 void __user *li_pend_bastparam;
81 void __user *li_pend_bastaddr;
82 struct list_head li_ownerqueue;
83 struct file_info *li_file;
84 struct dlm_lksb __user *li_user_lksb;
85 struct completion li_firstcomp;
88 /* A queued AST no less */
90 struct dlm_lock_result result;
91 struct list_head list;
93 uint32_t progress; /* How much has been read */
96 /* One of these per userland lockspace */
102 /* Passed into misc_register() */
103 struct miscdevice ls_miscinfo;
104 struct list_head ls_list;
107 /* misc_device info for the control device */
108 static struct miscdevice ctl_device;
111 * Stuff we hang off the file struct.
112 * The first two are to cope with unlocking all the
113 * locks help by a process when it dies.
116 struct list_head fi_li_list; /* List of active lock_infos */
117 spinlock_t fi_li_lock;
118 struct list_head fi_ast_list; /* Queue of ASTs to be delivered */
119 spinlock_t fi_ast_lock;
120 wait_queue_head_t fi_wait;
121 struct user_ls *fi_ls;
122 atomic_t fi_refcnt; /* Number of users */
123 unsigned long fi_flags;
128 struct dlm_lock_params32 {
141 char lvb[DLM_USER_LVB_LEN];
145 struct dlm_write_request32 {
152 struct dlm_lock_params32 lock;
153 struct dlm_lspace_params lspace;
164 struct dlm_lock_result32 {
169 struct dlm_lksb32 lksb;
172 /* Offsets may be zero if no data is present */
177 static void compat_input(struct dlm_write_request *kparams, struct dlm_write_request32 *k32params)
180 kparams->version[0] = k32params->version[0];
181 kparams->version[1] = k32params->version[1];
182 kparams->version[2] = k32params->version[2];
184 kparams->cmd = k32params->cmd;
185 kparams->is64bit = k32params->is64bit;
186 if (kparams->cmd == DLM_USER_CREATE_LOCKSPACE ||
187 kparams->cmd == DLM_USER_REMOVE_LOCKSPACE) {
189 kparams->i.lspace.flags = k32params->i.lspace.flags;
190 kparams->i.lspace.minor = k32params->i.lspace.minor;
191 strcpy(kparams->i.lspace.name, k32params->i.lspace.name);
194 kparams->i.lock.mode = k32params->i.lock.mode;
195 kparams->i.lock.namelen = k32params->i.lock.namelen;
196 kparams->i.lock.flags = k32params->i.lock.flags;
197 kparams->i.lock.lkid = k32params->i.lock.lkid;
198 kparams->i.lock.parent = k32params->i.lock.parent;
199 kparams->i.lock.castparam = (void *)(long)k32params->i.lock.castparam;
200 kparams->i.lock.castaddr = (void *)(long)k32params->i.lock.castaddr;
201 kparams->i.lock.bastparam = (void *)(long)k32params->i.lock.bastparam;
202 kparams->i.lock.bastaddr = (void *)(long)k32params->i.lock.bastaddr;
203 kparams->i.lock.lksb = (void *)(long)k32params->i.lock.lksb;
204 memcpy(kparams->i.lock.lvb, k32params->i.lock.lvb, DLM_USER_LVB_LEN);
205 memcpy(kparams->i.lock.name, k32params->i.lock.name, kparams->i.lock.namelen);
209 void compat_output(struct dlm_lock_result *res, struct dlm_lock_result32 *res32)
211 res32->length = res->length - (sizeof(struct dlm_lock_result) - sizeof(struct dlm_lock_result32));
212 res32->user_astaddr = (__u32)(long)res->user_astaddr;
213 res32->user_astparam = (__u32)(long)res->user_astparam;
214 res32->user_lksb = (__u32)(long)res->user_lksb;
215 res32->bast_mode = res->bast_mode;
217 res32->lvb_offset = res->lvb_offset;
218 res32->length = res->length;
220 res32->lksb.sb_status = res->lksb.sb_status;
221 res32->lksb.sb_flags = res->lksb.sb_flags;
222 res32->lksb.sb_lkid = res->lksb.sb_lkid;
223 res32->lksb.sb_lvbptr = (__u32)(long)res->lksb.sb_lvbptr;
228 /* get and put ops for file_info.
229 Actually I don't really like "get" and "put", but everyone
230 else seems to use them and I can't think of anything
231 nicer at the moment */
232 static void get_file_info(struct file_info *f)
234 atomic_inc(&f->fi_refcnt);
237 static void put_file_info(struct file_info *f)
239 if (atomic_dec_and_test(&f->fi_refcnt))
243 static void release_lockinfo(struct lock_info *li)
245 put_file_info(li->li_file);
247 write_lock(&lockinfo_lock);
248 idr_remove(&lockinfo_idr, li->li_lksb.sb_lkid);
249 write_unlock(&lockinfo_lock);
251 if (li->li_lksb.sb_lvbptr)
252 kfree(li->li_lksb.sb_lvbptr);
255 module_put(THIS_MODULE);
258 static struct lock_info *get_lockinfo(uint32_t lockid)
260 struct lock_info *li;
262 read_lock(&lockinfo_lock);
263 li = idr_find(&lockinfo_idr, lockid);
264 read_unlock(&lockinfo_lock);
269 static int add_lockinfo(struct lock_info *li)
275 write_lock(&lockinfo_lock);
277 if (idr_find(&lockinfo_idr, li->li_lksb.sb_lkid))
281 r = idr_pre_get(&lockinfo_idr, GFP_KERNEL);
285 r = idr_get_new_above(&lockinfo_idr, li, li->li_lksb.sb_lkid, &n);
289 if (n != li->li_lksb.sb_lkid) {
290 idr_remove(&lockinfo_idr, n);
297 write_unlock(&lockinfo_lock);
303 static struct user_ls *__find_lockspace(int minor)
305 struct user_ls *lsinfo;
307 list_for_each_entry(lsinfo, &user_ls_list, ls_list) {
308 if (lsinfo->ls_miscinfo.minor == minor)
314 /* Find a lockspace struct given the device minor number */
315 static struct user_ls *find_lockspace(int minor)
317 struct user_ls *lsinfo;
319 mutex_lock(&user_ls_lock);
320 lsinfo = __find_lockspace(minor);
321 mutex_unlock(&user_ls_lock);
326 static void add_lockspace_to_list(struct user_ls *lsinfo)
328 mutex_lock(&user_ls_lock);
329 list_add(&lsinfo->ls_list, &user_ls_list);
330 mutex_unlock(&user_ls_lock);
333 /* Register a lockspace with the DLM and create a misc
334 device for userland to access it */
335 static int register_lockspace(char *name, struct user_ls **ls, int flags)
337 struct user_ls *newls;
341 namelen = strlen(name)+strlen(name_prefix)+2;
343 newls = kzalloc(sizeof(struct user_ls), GFP_KERNEL);
347 newls->ls_miscinfo.name = kzalloc(namelen, GFP_KERNEL);
348 if (!newls->ls_miscinfo.name) {
353 status = dlm_new_lockspace(name, strlen(name), &newls->ls_lockspace, 0,
356 kfree(newls->ls_miscinfo.name);
361 snprintf((char*)newls->ls_miscinfo.name, namelen, "%s_%s",
364 newls->ls_miscinfo.fops = &_dlm_fops;
365 newls->ls_miscinfo.minor = MISC_DYNAMIC_MINOR;
367 status = misc_register(&newls->ls_miscinfo);
369 printk(KERN_ERR "dlm: misc register failed for %s\n", name);
370 dlm_release_lockspace(newls->ls_lockspace, 0);
371 kfree(newls->ls_miscinfo.name);
376 if (flags & DLM_USER_LSFLG_AUTOFREE)
377 set_bit(LS_FLAG_AUTOFREE, &newls->ls_flags);
379 add_lockspace_to_list(newls);
384 /* Called with the user_ls_lock mutex held */
385 static int unregister_lockspace(struct user_ls *lsinfo, int force)
389 status = dlm_release_lockspace(lsinfo->ls_lockspace, force);
393 status = misc_deregister(&lsinfo->ls_miscinfo);
397 list_del(&lsinfo->ls_list);
398 set_bit(LS_FLAG_DELETED, &lsinfo->ls_flags);
399 lsinfo->ls_lockspace = NULL;
400 if (atomic_read(&lsinfo->ls_refcnt) == 0) {
401 kfree(lsinfo->ls_miscinfo.name);
408 /* Add it to userland's AST queue */
409 static void add_to_astqueue(struct lock_info *li, void *astaddr, void *astparam,
412 struct ast_info *ast = kzalloc(sizeof(struct ast_info), GFP_KERNEL);
416 ast->result.user_astparam = astparam;
417 ast->result.user_astaddr = astaddr;
418 ast->result.user_lksb = li->li_user_lksb;
419 memcpy(&ast->result.lksb, &li->li_lksb, sizeof(struct dlm_lksb));
420 ast->lvb_updated = lvb_updated;
422 spin_lock(&li->li_file->fi_ast_lock);
423 list_add_tail(&ast->list, &li->li_file->fi_ast_list);
424 spin_unlock(&li->li_file->fi_ast_lock);
425 wake_up_interruptible(&li->li_file->fi_wait);
428 static void bast_routine(void *param, int mode)
430 struct lock_info *li = param;
432 if (li && li->li_bastaddr)
433 add_to_astqueue(li, li->li_bastaddr, li->li_bastparam, 0);
437 * This is the kernel's AST routine.
438 * All lock, unlock & query operations complete here.
439 * The only syncronous ops are those done during device close.
441 static void ast_routine(void *param)
443 struct lock_info *li = param;
445 /* Param may be NULL if a persistent lock is unlocked by someone else */
449 /* If this is a succesful conversion then activate the blocking ast
450 * args from the conversion request */
451 if (!test_bit(LI_FLAG_FIRSTLOCK, &li->li_flags) &&
452 li->li_lksb.sb_status == 0) {
454 li->li_bastparam = li->li_pend_bastparam;
455 li->li_bastaddr = li->li_pend_bastaddr;
456 li->li_pend_bastaddr = NULL;
459 /* If it's an async request then post data to the user's AST queue. */
460 if (li->li_castaddr) {
463 /* See if the lvb has been updated */
464 if (dlm_lvb_operations[li->li_grmode+1][li->li_rqmode+1] == 1)
467 if (li->li_lksb.sb_status == 0)
468 li->li_grmode = li->li_rqmode;
470 /* Only queue AST if the device is still open */
471 if (test_bit(FI_FLAG_OPEN, &li->li_file->fi_flags))
472 add_to_astqueue(li, li->li_castaddr, li->li_castparam,
475 /* If it's a new lock operation that failed, then
476 * remove it from the owner queue and free the
479 if (test_and_clear_bit(LI_FLAG_FIRSTLOCK, &li->li_flags) &&
480 li->li_lksb.sb_status != 0) {
482 /* Wait till dlm_lock() has finished */
483 wait_for_completion(&li->li_firstcomp);
485 spin_lock(&li->li_file->fi_li_lock);
486 list_del(&li->li_ownerqueue);
487 clear_bit(LI_FLAG_ONLIST, &li->li_flags);
488 spin_unlock(&li->li_file->fi_li_lock);
489 release_lockinfo(li);
492 /* Free unlocks & queries */
493 if (li->li_lksb.sb_status == -DLM_EUNLOCK ||
494 li->li_cmd == DLM_USER_QUERY) {
495 release_lockinfo(li);
498 /* Synchronous request, just wake up the caller */
499 set_bit(LI_FLAG_COMPLETE, &li->li_flags);
500 wake_up_interruptible(&li->li_waitq);
505 * Wait for the lock op to complete and return the status.
507 static int wait_for_ast(struct lock_info *li)
509 /* Wait for the AST routine to complete */
510 set_task_state(current, TASK_INTERRUPTIBLE);
511 while (!test_bit(LI_FLAG_COMPLETE, &li->li_flags))
514 set_task_state(current, TASK_RUNNING);
516 return li->li_lksb.sb_status;
520 /* Open on control device */
521 static int dlm_ctl_open(struct inode *inode, struct file *file)
523 file->private_data = NULL;
527 /* Close on control device */
528 static int dlm_ctl_close(struct inode *inode, struct file *file)
533 /* Open on lockspace device */
534 static int dlm_open(struct inode *inode, struct file *file)
537 struct user_ls *lsinfo;
539 lsinfo = find_lockspace(iminor(inode));
543 f = kzalloc(sizeof(struct file_info), GFP_KERNEL);
547 atomic_inc(&lsinfo->ls_refcnt);
548 INIT_LIST_HEAD(&f->fi_li_list);
549 INIT_LIST_HEAD(&f->fi_ast_list);
550 spin_lock_init(&f->fi_li_lock);
551 spin_lock_init(&f->fi_ast_lock);
552 init_waitqueue_head(&f->fi_wait);
556 set_bit(FI_FLAG_OPEN, &f->fi_flags);
558 file->private_data = f;
563 /* Check the user's version matches ours */
564 static int check_version(struct dlm_write_request *req)
566 if (req->version[0] != DLM_DEVICE_VERSION_MAJOR ||
567 (req->version[0] == DLM_DEVICE_VERSION_MAJOR &&
568 req->version[1] > DLM_DEVICE_VERSION_MINOR)) {
570 printk(KERN_DEBUG "dlm: process %s (%d) version mismatch "
571 "user (%d.%d.%d) kernel (%d.%d.%d)\n",
577 DLM_DEVICE_VERSION_MAJOR,
578 DLM_DEVICE_VERSION_MINOR,
579 DLM_DEVICE_VERSION_PATCH);
585 /* Close on lockspace device */
586 static int dlm_close(struct inode *inode, struct file *file)
588 struct file_info *f = file->private_data;
590 struct lock_info *old_li, *safe;
593 struct user_ls *lsinfo;
594 DECLARE_WAITQUEUE(wq, current);
596 lsinfo = find_lockspace(iminor(inode));
600 /* Mark this closed so that ASTs will not be delivered any more */
601 clear_bit(FI_FLAG_OPEN, &f->fi_flags);
603 /* Block signals while we are doing this */
604 sigfillset(&allsigs);
605 sigprocmask(SIG_BLOCK, &allsigs, &tmpsig);
607 /* We use our own lock_info struct here, so that any
608 * outstanding "real" ASTs will be delivered with the
609 * corresponding "real" params, thus freeing the lock_info
610 * that belongs the lock. This catches the corner case where
611 * a lock is BUSY when we try to unlock it here
613 memset(&li, 0, sizeof(li));
614 clear_bit(LI_FLAG_COMPLETE, &li.li_flags);
615 init_waitqueue_head(&li.li_waitq);
616 add_wait_queue(&li.li_waitq, &wq);
619 * Free any outstanding locks, they are on the
620 * list in LIFO order so there should be no problems
621 * about unlocking parents before children.
623 list_for_each_entry_safe(old_li, safe, &f->fi_li_list, li_ownerqueue) {
627 /* Don't unlock persistent locks, just mark them orphaned */
628 if (test_bit(LI_FLAG_PERSISTENT, &old_li->li_flags)) {
629 list_del(&old_li->li_ownerqueue);
631 /* Update master copy */
632 /* TODO: Check locking core updates the local and
633 remote ORPHAN flags */
634 li.li_lksb.sb_lkid = old_li->li_lksb.sb_lkid;
635 status = dlm_lock(f->fi_ls->ls_lockspace,
636 old_li->li_grmode, &li.li_lksb,
637 DLM_LKF_CONVERT|DLM_LKF_ORPHAN,
638 NULL, 0, 0, ast_routine, NULL, NULL);
640 printk("dlm: Error orphaning lock %x: %d\n",
641 old_li->li_lksb.sb_lkid, status);
643 /* But tidy our references in it */
644 release_lockinfo(old_li);
648 clear_bit(LI_FLAG_COMPLETE, &li.li_flags);
650 flags = DLM_LKF_FORCEUNLOCK;
651 if (old_li->li_grmode >= DLM_LOCK_PW)
652 flags |= DLM_LKF_IVVALBLK;
654 status = dlm_unlock(f->fi_ls->ls_lockspace,
655 old_li->li_lksb.sb_lkid, flags,
658 /* Must wait for it to complete as the next lock could be its
663 /* Unlock suceeded, free the lock_info struct. */
665 release_lockinfo(old_li);
668 remove_wait_queue(&li.li_waitq, &wq);
671 * If this is the last reference to the lockspace
672 * then free the struct. If it's an AUTOFREE lockspace
673 * then free the whole thing.
675 mutex_lock(&user_ls_lock);
676 if (atomic_dec_and_test(&lsinfo->ls_refcnt)) {
678 if (lsinfo->ls_lockspace) {
679 if (test_bit(LS_FLAG_AUTOFREE, &lsinfo->ls_flags)) {
680 unregister_lockspace(lsinfo, 1);
683 kfree(lsinfo->ls_miscinfo.name);
687 mutex_unlock(&user_ls_lock);
690 /* Restore signals */
691 sigprocmask(SIG_SETMASK, &tmpsig, NULL);
697 static int do_user_create_lockspace(struct file_info *fi, uint8_t cmd,
698 struct dlm_lspace_params *kparams)
701 struct user_ls *lsinfo;
703 if (!capable(CAP_SYS_ADMIN))
706 status = register_lockspace(kparams->name, &lsinfo, kparams->flags);
708 /* If it succeeded then return the minor number */
710 status = lsinfo->ls_miscinfo.minor;
715 static int do_user_remove_lockspace(struct file_info *fi, uint8_t cmd,
716 struct dlm_lspace_params *kparams)
720 struct user_ls *lsinfo;
722 if (!capable(CAP_SYS_ADMIN))
725 mutex_lock(&user_ls_lock);
726 lsinfo = __find_lockspace(kparams->minor);
728 mutex_unlock(&user_ls_lock);
732 if (kparams->flags & DLM_USER_LSFLG_FORCEFREE)
735 status = unregister_lockspace(lsinfo, force);
736 mutex_unlock(&user_ls_lock);
741 /* Read call, might block if no ASTs are waiting.
742 * It will only ever return one message at a time, regardless
743 * of how many are pending.
745 static ssize_t dlm_read(struct file *file, char __user *buffer, size_t count,
748 struct file_info *fi = file->private_data;
749 struct ast_info *ast;
754 DECLARE_WAITQUEUE(wait, current);
756 struct dlm_lock_result32 result32;
758 if (count < sizeof(struct dlm_lock_result32))
760 if (count < sizeof(struct dlm_lock_result))
764 spin_lock(&fi->fi_ast_lock);
765 if (list_empty(&fi->fi_ast_list)) {
768 * Return EOF if the lockspace been deleted.
770 if (test_bit(LS_FLAG_DELETED, &fi->fi_ls->ls_flags))
773 if (file->f_flags & O_NONBLOCK) {
774 spin_unlock(&fi->fi_ast_lock);
778 add_wait_queue(&fi->fi_wait, &wait);
781 set_current_state(TASK_INTERRUPTIBLE);
782 if (list_empty(&fi->fi_ast_list) &&
783 !signal_pending(current)) {
785 spin_unlock(&fi->fi_ast_lock);
787 spin_lock(&fi->fi_ast_lock);
791 current->state = TASK_RUNNING;
792 remove_wait_queue(&fi->fi_wait, &wait);
794 if (signal_pending(current)) {
795 spin_unlock(&fi->fi_ast_lock);
800 ast = list_entry(fi->fi_ast_list.next, struct ast_info, list);
801 list_del(&ast->list);
802 spin_unlock(&fi->fi_ast_lock);
804 /* Work out the size of the returned data */
806 if (test_bit(FI_FLAG_COMPAT, &fi->fi_flags)) {
807 data_size = struct_size = sizeof(struct dlm_lock_result32);
813 data_size = struct_size = sizeof(struct dlm_lock_result);
816 if (ast->lvb_updated && ast->result.lksb.sb_lvbptr)
817 data_size += DLM_USER_LVB_LEN;
819 offset = struct_size;
821 /* Room for the extended data ? */
822 if (count >= data_size) {
824 if (ast->lvb_updated && ast->result.lksb.sb_lvbptr) {
825 if (copy_to_user(buffer+offset,
826 ast->result.lksb.sb_lvbptr,
829 ast->result.lvb_offset = offset;
830 offset += DLM_USER_LVB_LEN;
834 ast->result.length = data_size;
837 compat_output(&ast->result, &result32);
840 /* Copy the header now it has all the offsets in it */
841 if (copy_to_user(buffer, data, struct_size))
844 /* If we only returned a header and there's more to come then put it
846 if (count < data_size) {
847 spin_lock(&fi->fi_ast_lock);
848 list_add(&ast->list, &fi->fi_ast_list);
849 spin_unlock(&fi->fi_ast_lock);
855 static unsigned int dlm_poll(struct file *file, poll_table *wait)
857 struct file_info *fi = file->private_data;
859 poll_wait(file, &fi->fi_wait, wait);
861 spin_lock(&fi->fi_ast_lock);
862 if (!list_empty(&fi->fi_ast_list)) {
863 spin_unlock(&fi->fi_ast_lock);
864 return POLLIN | POLLRDNORM;
867 spin_unlock(&fi->fi_ast_lock);
871 static struct lock_info *allocate_lockinfo(struct file_info *fi, uint8_t cmd,
872 struct dlm_lock_params *kparams)
874 struct lock_info *li;
876 if (!try_module_get(THIS_MODULE))
879 li = kzalloc(sizeof(struct lock_info), GFP_KERNEL);
881 li->li_magic = LOCKINFO_MAGIC;
887 li->li_pend_bastparam = NULL;
888 li->li_pend_bastaddr = NULL;
889 li->li_castaddr = NULL;
890 li->li_castparam = NULL;
891 li->li_lksb.sb_lvbptr = NULL;
892 li->li_bastaddr = kparams->bastaddr;
893 li->li_bastparam = kparams->bastparam;
900 static int do_user_lock(struct file_info *fi, uint8_t cmd,
901 struct dlm_lock_params *kparams)
903 struct lock_info *li;
907 * Validate things that we need to have correct.
909 if (!kparams->castaddr)
915 /* Persistent child locks are not available yet */
916 if ((kparams->flags & DLM_LKF_PERSISTENT) && kparams->parent)
919 /* For conversions, there should already be a lockinfo struct,
920 unless we are adopting an orphaned persistent lock */
921 if (kparams->flags & DLM_LKF_CONVERT) {
923 li = get_lockinfo(kparams->lkid);
925 /* If this is a persistent lock we will have to create a
927 if (!li && (kparams->flags & DLM_LKF_PERSISTENT)) {
928 li = allocate_lockinfo(fi, cmd, kparams);
932 li->li_lksb.sb_lkid = kparams->lkid;
933 li->li_castaddr = kparams->castaddr;
934 li->li_castparam = kparams->castparam;
936 /* OK, this isn't exactly a FIRSTLOCK but it is the
937 first time we've used this lockinfo, and if things
938 fail we want rid of it */
939 init_completion(&li->li_firstcomp);
940 set_bit(LI_FLAG_FIRSTLOCK, &li->li_flags);
943 /* TODO: do a query to get the current state ?? */
948 if (li->li_magic != LOCKINFO_MAGIC)
951 /* For conversions don't overwrite the current blocking AST
953 a) if a blocking AST fires before the conversion is queued
954 it runs the current handler
955 b) if the conversion is cancelled, the original blocking AST
956 declaration is active
957 The pend_ info is made active when the conversion
960 li->li_pend_bastaddr = kparams->bastaddr;
961 li->li_pend_bastparam = kparams->bastparam;
963 li = allocate_lockinfo(fi, cmd, kparams);
967 /* Allow us to complete our work before
968 the AST routine runs. In fact we only need (and use) this
969 when the initial lock fails */
970 init_completion(&li->li_firstcomp);
971 set_bit(LI_FLAG_FIRSTLOCK, &li->li_flags);
974 li->li_user_lksb = kparams->lksb;
975 li->li_castaddr = kparams->castaddr;
976 li->li_castparam = kparams->castparam;
977 li->li_lksb.sb_lkid = kparams->lkid;
978 li->li_rqmode = kparams->mode;
979 if (kparams->flags & DLM_LKF_PERSISTENT)
980 set_bit(LI_FLAG_PERSISTENT, &li->li_flags);
982 /* Copy in the value block */
983 if (kparams->flags & DLM_LKF_VALBLK) {
984 if (!li->li_lksb.sb_lvbptr) {
985 li->li_lksb.sb_lvbptr = kmalloc(DLM_USER_LVB_LEN,
987 if (!li->li_lksb.sb_lvbptr) {
993 memcpy(li->li_lksb.sb_lvbptr, kparams->lvb, DLM_USER_LVB_LEN);
997 status = dlm_lock(fi->fi_ls->ls_lockspace,
998 kparams->mode, &li->li_lksb,
1000 kparams->name, kparams->namelen,
1004 (li->li_pend_bastaddr || li->li_bastaddr) ?
1005 bast_routine : NULL);
1009 /* If it succeeded (this far) with a new lock then keep track of
1010 it on the file's lockinfo list */
1011 if (!status && test_bit(LI_FLAG_FIRSTLOCK, &li->li_flags)) {
1013 spin_lock(&fi->fi_li_lock);
1014 list_add(&li->li_ownerqueue, &fi->fi_li_list);
1015 set_bit(LI_FLAG_ONLIST, &li->li_flags);
1016 spin_unlock(&fi->fi_li_lock);
1017 if (add_lockinfo(li))
1018 printk(KERN_WARNING "Add lockinfo failed\n");
1020 complete(&li->li_firstcomp);
1023 /* Return the lockid as the user needs it /now/ */
1024 return li->li_lksb.sb_lkid;
1027 if (test_bit(LI_FLAG_FIRSTLOCK, &li->li_flags))
1028 release_lockinfo(li);
1033 static int do_user_unlock(struct file_info *fi, uint8_t cmd,
1034 struct dlm_lock_params *kparams)
1036 struct lock_info *li;
1038 int convert_cancel = 0;
1040 li = get_lockinfo(kparams->lkid);
1042 li = allocate_lockinfo(fi, cmd, kparams);
1045 spin_lock(&fi->fi_li_lock);
1046 list_add(&li->li_ownerqueue, &fi->fi_li_list);
1047 set_bit(LI_FLAG_ONLIST, &li->li_flags);
1048 spin_unlock(&fi->fi_li_lock);
1051 if (li->li_magic != LOCKINFO_MAGIC)
1054 li->li_user_lksb = kparams->lksb;
1055 li->li_castparam = kparams->castparam;
1058 /* Cancelling a conversion doesn't remove the lock...*/
1059 if (kparams->flags & DLM_LKF_CANCEL && li->li_grmode != -1)
1062 /* Wait until dlm_lock() has completed */
1063 if (!test_bit(LI_FLAG_ONLIST, &li->li_flags)) {
1064 wait_for_completion(&li->li_firstcomp);
1067 /* dlm_unlock() passes a 0 for castaddr which means don't overwrite
1068 the existing li_castaddr as that's the completion routine for
1069 unlocks. dlm_unlock_wait() specifies a new AST routine to be
1070 executed when the unlock completes. */
1071 if (kparams->castaddr)
1072 li->li_castaddr = kparams->castaddr;
1074 /* Use existing lksb & astparams */
1075 status = dlm_unlock(fi->fi_ls->ls_lockspace,
1077 kparams->flags, &li->li_lksb, li);
1079 if (!status && !convert_cancel) {
1080 spin_lock(&fi->fi_li_lock);
1081 list_del(&li->li_ownerqueue);
1082 clear_bit(LI_FLAG_ONLIST, &li->li_flags);
1083 spin_unlock(&fi->fi_li_lock);
1089 /* Write call, submit a locking request */
1090 static ssize_t dlm_write(struct file *file, const char __user *buffer,
1091 size_t count, loff_t *ppos)
1093 struct file_info *fi = file->private_data;
1094 struct dlm_write_request *kparams;
1099 #ifdef CONFIG_COMPAT
1100 if (count < sizeof(struct dlm_write_request32))
1102 if (count < sizeof(struct dlm_write_request))
1106 if (count > sizeof(struct dlm_write_request) + DLM_RESNAME_MAXLEN)
1109 /* Has the lockspace been deleted */
1110 if (fi && test_bit(LS_FLAG_DELETED, &fi->fi_ls->ls_flags))
1113 kparams = kmalloc(count, GFP_KERNEL);
1118 /* Get the command info */
1119 if (copy_from_user(kparams, buffer, count))
1123 if (check_version(kparams))
1126 #ifdef CONFIG_COMPAT
1127 if (!kparams->is64bit) {
1128 struct dlm_write_request32 *k32params = (struct dlm_write_request32 *)kparams;
1129 kparams = kmalloc(count + (sizeof(struct dlm_write_request) - sizeof(struct dlm_write_request32)), GFP_KERNEL);
1134 set_bit(FI_FLAG_COMPAT, &fi->fi_flags);
1135 compat_input(kparams, k32params);
1140 /* Block signals while we are doing this */
1141 sigfillset(&allsigs);
1142 sigprocmask(SIG_BLOCK, &allsigs, &tmpsig);
1145 switch (kparams->cmd)
1148 if (!fi) goto out_sig;
1149 status = do_user_lock(fi, kparams->cmd, &kparams->i.lock);
1152 case DLM_USER_UNLOCK:
1153 if (!fi) goto out_sig;
1154 status = do_user_unlock(fi, kparams->cmd, &kparams->i.lock);
1157 case DLM_USER_CREATE_LOCKSPACE:
1158 if (fi) goto out_sig;
1159 status = do_user_create_lockspace(fi, kparams->cmd,
1160 &kparams->i.lspace);
1163 case DLM_USER_REMOVE_LOCKSPACE:
1164 if (fi) goto out_sig;
1165 status = do_user_remove_lockspace(fi, kparams->cmd,
1166 &kparams->i.lspace);
1169 printk("Unknown command passed to DLM device : %d\n",
1175 /* Restore signals */
1176 sigprocmask(SIG_SETMASK, &tmpsig, NULL);
1177 recalc_sigpending();
1187 static struct file_operations _dlm_fops = {
1189 .release = dlm_close,
1193 .owner = THIS_MODULE,
1196 static struct file_operations _dlm_ctl_fops = {
1197 .open = dlm_ctl_open,
1198 .release = dlm_ctl_close,
1200 .owner = THIS_MODULE,
1204 * Create control device
1206 static int __init dlm_device_init(void)
1210 INIT_LIST_HEAD(&user_ls_list);
1211 mutex_init(&user_ls_lock);
1212 rwlock_init(&lockinfo_lock);
1214 ctl_device.name = "dlm-control";
1215 ctl_device.fops = &_dlm_ctl_fops;
1216 ctl_device.minor = MISC_DYNAMIC_MINOR;
1218 r = misc_register(&ctl_device);
1220 printk(KERN_ERR "dlm: misc_register failed for control dev\n");
1227 static void __exit dlm_device_exit(void)
1229 misc_deregister(&ctl_device);
1232 MODULE_DESCRIPTION("Distributed Lock Manager device interface");
1233 MODULE_AUTHOR("Red Hat, Inc.");
1234 MODULE_LICENSE("GPL");
1236 module_init(dlm_device_init);
1237 module_exit(dlm_device_exit);