2 * Copyright (C) 2006 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>
21 #include "dlm_internal.h"
22 #include "lockspace.h"
24 #include "lvb_table.h"
27 static const char *name_prefix="dlm";
28 static struct miscdevice ctl_device;
29 static const struct file_operations device_fops;
33 struct dlm_lock_params32 {
46 char lvb[DLM_USER_LVB_LEN];
50 struct dlm_write_request32 {
57 struct dlm_lock_params32 lock;
58 struct dlm_lspace_params lspace;
69 struct dlm_lock_result32 {
74 struct dlm_lksb32 lksb;
77 /* Offsets may be zero if no data is present */
81 static void compat_input(struct dlm_write_request *kb,
82 struct dlm_write_request32 *kb32)
84 kb->version[0] = kb32->version[0];
85 kb->version[1] = kb32->version[1];
86 kb->version[2] = kb32->version[2];
89 kb->is64bit = kb32->is64bit;
90 if (kb->cmd == DLM_USER_CREATE_LOCKSPACE ||
91 kb->cmd == DLM_USER_REMOVE_LOCKSPACE) {
92 kb->i.lspace.flags = kb32->i.lspace.flags;
93 kb->i.lspace.minor = kb32->i.lspace.minor;
94 strcpy(kb->i.lspace.name, kb32->i.lspace.name);
96 kb->i.lock.mode = kb32->i.lock.mode;
97 kb->i.lock.namelen = kb32->i.lock.namelen;
98 kb->i.lock.flags = kb32->i.lock.flags;
99 kb->i.lock.lkid = kb32->i.lock.lkid;
100 kb->i.lock.parent = kb32->i.lock.parent;
101 kb->i.lock.castparam = (void *)(long)kb32->i.lock.castparam;
102 kb->i.lock.castaddr = (void *)(long)kb32->i.lock.castaddr;
103 kb->i.lock.bastparam = (void *)(long)kb32->i.lock.bastparam;
104 kb->i.lock.bastaddr = (void *)(long)kb32->i.lock.bastaddr;
105 kb->i.lock.lksb = (void *)(long)kb32->i.lock.lksb;
106 memcpy(kb->i.lock.lvb, kb32->i.lock.lvb, DLM_USER_LVB_LEN);
107 memcpy(kb->i.lock.name, kb32->i.lock.name, kb->i.lock.namelen);
111 static void compat_output(struct dlm_lock_result *res,
112 struct dlm_lock_result32 *res32)
114 res32->length = res->length - (sizeof(struct dlm_lock_result) -
115 sizeof(struct dlm_lock_result32));
116 res32->user_astaddr = (__u32)(long)res->user_astaddr;
117 res32->user_astparam = (__u32)(long)res->user_astparam;
118 res32->user_lksb = (__u32)(long)res->user_lksb;
119 res32->bast_mode = res->bast_mode;
121 res32->lvb_offset = res->lvb_offset;
122 res32->length = res->length;
124 res32->lksb.sb_status = res->lksb.sb_status;
125 res32->lksb.sb_flags = res->lksb.sb_flags;
126 res32->lksb.sb_lkid = res->lksb.sb_lkid;
127 res32->lksb.sb_lvbptr = (__u32)(long)res->lksb.sb_lvbptr;
132 void dlm_user_add_ast(struct dlm_lkb *lkb, int type)
135 struct dlm_user_args *ua;
136 struct dlm_user_proc *proc;
137 int remove_ownqueue = 0;
139 /* dlm_clear_proc_locks() sets ORPHAN/DEAD flag on each
140 lkb before dealing with it. We need to check this
141 flag before taking ls_clear_proc_locks mutex because if
142 it's set, dlm_clear_proc_locks() holds the mutex. */
144 if (lkb->lkb_flags & (DLM_IFL_ORPHAN | DLM_IFL_DEAD)) {
145 /* log_print("user_add_ast skip1 %x", lkb->lkb_flags); */
149 ls = lkb->lkb_resource->res_ls;
150 mutex_lock(&ls->ls_clear_proc_locks);
152 /* If ORPHAN/DEAD flag is set, it means the process is dead so an ast
153 can't be delivered. For ORPHAN's, dlm_clear_proc_locks() freed
154 lkb->ua so we can't try to use it. */
156 if (lkb->lkb_flags & (DLM_IFL_ORPHAN | DLM_IFL_DEAD)) {
157 /* log_print("user_add_ast skip2 %x", lkb->lkb_flags); */
161 DLM_ASSERT(lkb->lkb_astparam, dlm_print_lkb(lkb););
162 ua = (struct dlm_user_args *)lkb->lkb_astparam;
165 if (type == AST_BAST && ua->bastaddr == NULL)
168 spin_lock(&proc->asts_spin);
169 if (!(lkb->lkb_ast_type & (AST_COMP | AST_BAST))) {
170 kref_get(&lkb->lkb_ref);
171 list_add_tail(&lkb->lkb_astqueue, &proc->asts);
172 lkb->lkb_ast_type |= type;
173 wake_up_interruptible(&proc->wait);
176 /* noqueue requests that fail may need to be removed from the
177 proc's locks list, there should be a better way of detecting
178 this situation than checking all these things... */
180 if (type == AST_COMP && lkb->lkb_grmode == DLM_LOCK_IV &&
181 ua->lksb.sb_status == -EAGAIN && !list_empty(&lkb->lkb_ownqueue))
184 /* unlocks or cancels of waiting requests need to be removed from the
185 proc's unlocking list, again there must be a better way... */
187 if (ua->lksb.sb_status == -DLM_EUNLOCK ||
188 (ua->lksb.sb_status == -DLM_ECANCEL &&
189 lkb->lkb_grmode == DLM_LOCK_IV))
192 /* We want to copy the lvb to userspace when the completion
193 ast is read if the status is 0, the lock has an lvb and
194 lvb_ops says we should. We could probably have set_lvb_lock()
195 set update_user_lvb instead and not need old_mode */
197 if ((lkb->lkb_ast_type & AST_COMP) &&
198 (lkb->lkb_lksb->sb_status == 0) &&
199 lkb->lkb_lksb->sb_lvbptr &&
200 dlm_lvb_operations[ua->old_mode + 1][lkb->lkb_grmode + 1])
201 ua->update_user_lvb = 1;
203 ua->update_user_lvb = 0;
205 spin_unlock(&proc->asts_spin);
207 if (remove_ownqueue) {
208 spin_lock(&ua->proc->locks_spin);
209 list_del_init(&lkb->lkb_ownqueue);
210 spin_unlock(&ua->proc->locks_spin);
214 mutex_unlock(&ls->ls_clear_proc_locks);
217 static int device_user_lock(struct dlm_user_proc *proc,
218 struct dlm_lock_params *params)
221 struct dlm_user_args *ua;
224 ls = dlm_find_lockspace_local(proc->lockspace);
228 if (!params->castaddr || !params->lksb) {
233 ua = kzalloc(sizeof(struct dlm_user_args), GFP_KERNEL);
237 ua->user_lksb = params->lksb;
238 ua->castparam = params->castparam;
239 ua->castaddr = params->castaddr;
240 ua->bastparam = params->bastparam;
241 ua->bastaddr = params->bastaddr;
243 if (params->flags & DLM_LKF_CONVERT)
244 error = dlm_user_convert(ls, ua,
245 params->mode, params->flags,
246 params->lkid, params->lvb);
248 error = dlm_user_request(ls, ua,
249 params->mode, params->flags,
250 params->name, params->namelen,
253 error = ua->lksb.sb_lkid;
256 dlm_put_lockspace(ls);
260 static int device_user_unlock(struct dlm_user_proc *proc,
261 struct dlm_lock_params *params)
264 struct dlm_user_args *ua;
267 ls = dlm_find_lockspace_local(proc->lockspace);
271 ua = kzalloc(sizeof(struct dlm_user_args), GFP_KERNEL);
275 ua->user_lksb = params->lksb;
276 ua->castparam = params->castparam;
277 ua->castaddr = params->castaddr;
279 if (params->flags & DLM_LKF_CANCEL)
280 error = dlm_user_cancel(ls, ua, params->flags, params->lkid);
282 error = dlm_user_unlock(ls, ua, params->flags, params->lkid,
285 dlm_put_lockspace(ls);
289 static int device_create_lockspace(struct dlm_lspace_params *params)
291 dlm_lockspace_t *lockspace;
295 if (!capable(CAP_SYS_ADMIN))
298 error = dlm_new_lockspace(params->name, strlen(params->name),
299 &lockspace, 0, DLM_USER_LVB_LEN);
303 ls = dlm_find_lockspace_local(lockspace);
308 len = strlen(params->name) + strlen(name_prefix) + 2;
309 ls->ls_device.name = kzalloc(len, GFP_KERNEL);
310 if (!ls->ls_device.name)
312 snprintf((char *)ls->ls_device.name, len, "%s_%s", name_prefix,
314 ls->ls_device.fops = &device_fops;
315 ls->ls_device.minor = MISC_DYNAMIC_MINOR;
317 error = misc_register(&ls->ls_device);
319 kfree(ls->ls_device.name);
323 error = ls->ls_device.minor;
324 dlm_put_lockspace(ls);
328 dlm_put_lockspace(ls);
329 dlm_release_lockspace(lockspace, 0);
333 static int device_remove_lockspace(struct dlm_lspace_params *params)
335 dlm_lockspace_t *lockspace;
337 int error, force = 0;
339 if (!capable(CAP_SYS_ADMIN))
342 ls = dlm_find_lockspace_device(params->minor);
346 error = misc_deregister(&ls->ls_device);
348 dlm_put_lockspace(ls);
351 kfree(ls->ls_device.name);
353 if (params->flags & DLM_USER_LSFLG_FORCEFREE)
356 lockspace = ls->ls_local_handle;
358 /* dlm_release_lockspace waits for references to go to zero,
359 so all processes will need to close their device for the ls
360 before the release will procede */
362 dlm_put_lockspace(ls);
363 error = dlm_release_lockspace(lockspace, force);
368 /* Check the user's version matches ours */
369 static int check_version(struct dlm_write_request *req)
371 if (req->version[0] != DLM_DEVICE_VERSION_MAJOR ||
372 (req->version[0] == DLM_DEVICE_VERSION_MAJOR &&
373 req->version[1] > DLM_DEVICE_VERSION_MINOR)) {
375 printk(KERN_DEBUG "dlm: process %s (%d) version mismatch "
376 "user (%d.%d.%d) kernel (%d.%d.%d)\n",
382 DLM_DEVICE_VERSION_MAJOR,
383 DLM_DEVICE_VERSION_MINOR,
384 DLM_DEVICE_VERSION_PATCH);
394 * dlm_user_request -> request_lock
395 * dlm_user_convert -> convert_lock
398 * dlm_user_unlock -> unlock_lock
399 * dlm_user_cancel -> cancel_lock
401 * device_create_lockspace
404 * device_remove_lockspace
405 * dlm_release_lockspace
408 /* a write to a lockspace device is a lock or unlock request, a write
409 to the control device is to create/remove a lockspace */
411 static ssize_t device_write(struct file *file, const char __user *buf,
412 size_t count, loff_t *ppos)
414 struct dlm_user_proc *proc = file->private_data;
415 struct dlm_write_request *kbuf;
416 sigset_t tmpsig, allsigs;
420 if (count < sizeof(struct dlm_write_request32))
422 if (count < sizeof(struct dlm_write_request))
426 kbuf = kmalloc(count, GFP_KERNEL);
430 if (copy_from_user(kbuf, buf, count)) {
435 if (check_version(kbuf)) {
441 if (!kbuf->is64bit) {
442 struct dlm_write_request32 *k32buf;
443 k32buf = (struct dlm_write_request32 *)kbuf;
444 kbuf = kmalloc(count + (sizeof(struct dlm_write_request) -
445 sizeof(struct dlm_write_request32)), GFP_KERNEL);
450 set_bit(DLM_PROC_FLAGS_COMPAT, &proc->flags);
451 compat_input(kbuf, k32buf);
456 /* do we really need this? can a write happen after a close? */
457 if ((kbuf->cmd == DLM_USER_LOCK || kbuf->cmd == DLM_USER_UNLOCK) &&
458 test_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags))
461 sigfillset(&allsigs);
462 sigprocmask(SIG_BLOCK, &allsigs, &tmpsig);
470 log_print("no locking on control device");
473 error = device_user_lock(proc, &kbuf->i.lock);
476 case DLM_USER_UNLOCK:
478 log_print("no locking on control device");
481 error = device_user_unlock(proc, &kbuf->i.lock);
484 case DLM_USER_CREATE_LOCKSPACE:
486 log_print("create/remove only on control device");
489 error = device_create_lockspace(&kbuf->i.lspace);
492 case DLM_USER_REMOVE_LOCKSPACE:
494 log_print("create/remove only on control device");
497 error = device_remove_lockspace(&kbuf->i.lspace);
501 log_print("Unknown command passed to DLM device : %d\n",
506 sigprocmask(SIG_SETMASK, &tmpsig, NULL);
513 /* Every process that opens the lockspace device has its own "proc" structure
514 hanging off the open file that's used to keep track of locks owned by the
515 process and asts that need to be delivered to the process. */
517 static int device_open(struct inode *inode, struct file *file)
519 struct dlm_user_proc *proc;
522 ls = dlm_find_lockspace_device(iminor(inode));
526 proc = kzalloc(sizeof(struct dlm_user_proc), GFP_KERNEL);
528 dlm_put_lockspace(ls);
532 proc->lockspace = ls->ls_local_handle;
533 INIT_LIST_HEAD(&proc->asts);
534 INIT_LIST_HEAD(&proc->locks);
535 INIT_LIST_HEAD(&proc->unlocking);
536 spin_lock_init(&proc->asts_spin);
537 spin_lock_init(&proc->locks_spin);
538 init_waitqueue_head(&proc->wait);
539 file->private_data = proc;
544 static int device_close(struct inode *inode, struct file *file)
546 struct dlm_user_proc *proc = file->private_data;
548 sigset_t tmpsig, allsigs;
550 ls = dlm_find_lockspace_local(proc->lockspace);
554 sigfillset(&allsigs);
555 sigprocmask(SIG_BLOCK, &allsigs, &tmpsig);
557 set_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags);
559 dlm_clear_proc_locks(ls, proc);
561 /* at this point no more lkb's should exist for this lockspace,
562 so there's no chance of dlm_user_add_ast() being called and
563 looking for lkb->ua->proc */
566 file->private_data = NULL;
568 dlm_put_lockspace(ls);
569 dlm_put_lockspace(ls); /* for the find in device_open() */
571 /* FIXME: AUTOFREE: if this ls is no longer used do
572 device_remove_lockspace() */
574 sigprocmask(SIG_SETMASK, &tmpsig, NULL);
580 static int copy_result_to_user(struct dlm_user_args *ua, int compat, int type,
581 int bmode, char __user *buf, size_t count)
584 struct dlm_lock_result32 result32;
586 struct dlm_lock_result result;
592 memset(&result, 0, sizeof(struct dlm_lock_result));
593 memcpy(&result.lksb, &ua->lksb, sizeof(struct dlm_lksb));
594 result.user_lksb = ua->user_lksb;
596 /* FIXME: dlm1 provides for the user's bastparam/addr to not be updated
597 in a conversion unless the conversion is successful. See code
598 in dlm_user_convert() for updating ua from ua_tmp. OpenVMS, though,
599 notes that a new blocking AST address and parameter are set even if
600 the conversion fails, so maybe we should just do that. */
602 if (type == AST_BAST) {
603 result.user_astaddr = ua->bastaddr;
604 result.user_astparam = ua->bastparam;
605 result.bast_mode = bmode;
607 result.user_astaddr = ua->castaddr;
608 result.user_astparam = ua->castparam;
613 len = sizeof(struct dlm_lock_result32);
616 len = sizeof(struct dlm_lock_result);
619 /* copy lvb to userspace if there is one, it's been updated, and
620 the user buffer has space for it */
622 if (ua->update_user_lvb && ua->lksb.sb_lvbptr &&
623 count >= len + DLM_USER_LVB_LEN) {
624 if (copy_to_user(buf+len, ua->lksb.sb_lvbptr,
630 result.lvb_offset = len;
631 len += DLM_USER_LVB_LEN;
638 compat_output(&result, &result32);
639 resultptr = &result32;
643 if (copy_to_user(buf, resultptr, struct_len))
651 /* a read returns a single ast described in a struct dlm_lock_result */
653 static ssize_t device_read(struct file *file, char __user *buf, size_t count,
656 struct dlm_user_proc *proc = file->private_data;
658 struct dlm_user_args *ua;
659 DECLARE_WAITQUEUE(wait, current);
660 int error, type=0, bmode=0, removed = 0;
663 if (count < sizeof(struct dlm_lock_result32))
665 if (count < sizeof(struct dlm_lock_result))
669 /* do we really need this? can a read happen after a close? */
670 if (test_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags))
673 spin_lock(&proc->asts_spin);
674 if (list_empty(&proc->asts)) {
675 if (file->f_flags & O_NONBLOCK) {
676 spin_unlock(&proc->asts_spin);
680 add_wait_queue(&proc->wait, &wait);
683 set_current_state(TASK_INTERRUPTIBLE);
684 if (list_empty(&proc->asts) && !signal_pending(current)) {
685 spin_unlock(&proc->asts_spin);
687 spin_lock(&proc->asts_spin);
690 set_current_state(TASK_RUNNING);
691 remove_wait_queue(&proc->wait, &wait);
693 if (signal_pending(current)) {
694 spin_unlock(&proc->asts_spin);
699 if (list_empty(&proc->asts)) {
700 spin_unlock(&proc->asts_spin);
704 /* there may be both completion and blocking asts to return for
705 the lkb, don't remove lkb from asts list unless no asts remain */
707 lkb = list_entry(proc->asts.next, struct dlm_lkb, lkb_astqueue);
709 if (lkb->lkb_ast_type & AST_COMP) {
710 lkb->lkb_ast_type &= ~AST_COMP;
712 } else if (lkb->lkb_ast_type & AST_BAST) {
713 lkb->lkb_ast_type &= ~AST_BAST;
715 bmode = lkb->lkb_bastmode;
718 if (!lkb->lkb_ast_type) {
719 list_del(&lkb->lkb_astqueue);
722 spin_unlock(&proc->asts_spin);
724 ua = (struct dlm_user_args *)lkb->lkb_astparam;
725 error = copy_result_to_user(ua,
726 test_bit(DLM_PROC_FLAGS_COMPAT, &proc->flags),
727 type, bmode, buf, count);
729 /* removes reference for the proc->asts lists added by
730 dlm_user_add_ast() and may result in the lkb being freed */
737 static unsigned int device_poll(struct file *file, poll_table *wait)
739 struct dlm_user_proc *proc = file->private_data;
741 poll_wait(file, &proc->wait, wait);
743 spin_lock(&proc->asts_spin);
744 if (!list_empty(&proc->asts)) {
745 spin_unlock(&proc->asts_spin);
746 return POLLIN | POLLRDNORM;
748 spin_unlock(&proc->asts_spin);
752 static int ctl_device_open(struct inode *inode, struct file *file)
754 file->private_data = NULL;
758 static int ctl_device_close(struct inode *inode, struct file *file)
763 static const struct file_operations device_fops = {
765 .release = device_close,
767 .write = device_write,
769 .owner = THIS_MODULE,
772 static const struct file_operations ctl_device_fops = {
773 .open = ctl_device_open,
774 .release = ctl_device_close,
775 .write = device_write,
776 .owner = THIS_MODULE,
779 int dlm_user_init(void)
783 ctl_device.name = "dlm-control";
784 ctl_device.fops = &ctl_device_fops;
785 ctl_device.minor = MISC_DYNAMIC_MINOR;
787 error = misc_register(&ctl_device);
789 log_print("misc_register failed for control device");
794 void dlm_user_exit(void)
796 misc_deregister(&ctl_device);