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"
26 static const char *name_prefix="dlm";
27 static struct miscdevice ctl_device;
28 static struct file_operations device_fops;
32 struct dlm_lock_params32 {
45 char lvb[DLM_USER_LVB_LEN];
49 struct dlm_write_request32 {
56 struct dlm_lock_params32 lock;
57 struct dlm_lspace_params lspace;
68 struct dlm_lock_result32 {
73 struct dlm_lksb32 lksb;
76 /* Offsets may be zero if no data is present */
80 static void compat_input(struct dlm_write_request *kb,
81 struct dlm_write_request32 *kb32)
83 kb->version[0] = kb32->version[0];
84 kb->version[1] = kb32->version[1];
85 kb->version[2] = kb32->version[2];
88 kb->is64bit = kb32->is64bit;
89 if (kb->cmd == DLM_USER_CREATE_LOCKSPACE ||
90 kb->cmd == DLM_USER_REMOVE_LOCKSPACE) {
91 kb->i.lspace.flags = kb32->i.lspace.flags;
92 kb->i.lspace.minor = kb32->i.lspace.minor;
93 strcpy(kb->i.lspace.name, kb32->i.lspace.name);
95 kb->i.lock.mode = kb32->i.lock.mode;
96 kb->i.lock.namelen = kb32->i.lock.namelen;
97 kb->i.lock.flags = kb32->i.lock.flags;
98 kb->i.lock.lkid = kb32->i.lock.lkid;
99 kb->i.lock.parent = kb32->i.lock.parent;
100 kb->i.lock.castparam = (void *)(long)kb32->i.lock.castparam;
101 kb->i.lock.castaddr = (void *)(long)kb32->i.lock.castaddr;
102 kb->i.lock.bastparam = (void *)(long)kb32->i.lock.bastparam;
103 kb->i.lock.bastaddr = (void *)(long)kb32->i.lock.bastaddr;
104 kb->i.lock.lksb = (void *)(long)kb32->i.lock.lksb;
105 memcpy(kb->i.lock.lvb, kb32->i.lock.lvb, DLM_USER_LVB_LEN);
106 memcpy(kb->i.lock.name, kb32->i.lock.name, kb->i.lock.namelen);
110 static void compat_output(struct dlm_lock_result *res,
111 struct dlm_lock_result32 *res32)
113 res32->length = res->length - (sizeof(struct dlm_lock_result) -
114 sizeof(struct dlm_lock_result32));
115 res32->user_astaddr = (__u32)(long)res->user_astaddr;
116 res32->user_astparam = (__u32)(long)res->user_astparam;
117 res32->user_lksb = (__u32)(long)res->user_lksb;
118 res32->bast_mode = res->bast_mode;
120 res32->lvb_offset = res->lvb_offset;
121 res32->length = res->length;
123 res32->lksb.sb_status = res->lksb.sb_status;
124 res32->lksb.sb_flags = res->lksb.sb_flags;
125 res32->lksb.sb_lkid = res->lksb.sb_lkid;
126 res32->lksb.sb_lvbptr = (__u32)(long)res->lksb.sb_lvbptr;
131 void dlm_user_add_ast(struct dlm_lkb *lkb, int type)
134 struct dlm_user_args *ua;
135 struct dlm_user_proc *proc;
137 /* dlm_clear_proc_locks() sets ORPHAN/DEAD flag on each
138 lkb before dealing with it. We need to check this
139 flag before taking ls_clear_proc_locks mutex because if
140 it's set, dlm_clear_proc_locks() holds the mutex. */
142 if (lkb->lkb_flags & (DLM_IFL_ORPHAN | DLM_IFL_DEAD)) {
143 /* log_print("user_add_ast skip1 %x", lkb->lkb_flags); */
147 ls = lkb->lkb_resource->res_ls;
148 mutex_lock(&ls->ls_clear_proc_locks);
150 /* If ORPHAN/DEAD flag is set, it means the process is dead so an ast
151 can't be delivered. For ORPHAN's, dlm_clear_proc_locks() freed
152 lkb->ua so we can't try to use it. */
154 if (lkb->lkb_flags & (DLM_IFL_ORPHAN | DLM_IFL_DEAD)) {
155 /* log_print("user_add_ast skip2 %x", lkb->lkb_flags); */
159 DLM_ASSERT(lkb->lkb_astparam, dlm_print_lkb(lkb););
160 ua = (struct dlm_user_args *)lkb->lkb_astparam;
163 if (type == AST_BAST && ua->bastaddr == NULL)
166 spin_lock(&proc->asts_spin);
167 if (!(lkb->lkb_ast_type & (AST_COMP | AST_BAST))) {
168 kref_get(&lkb->lkb_ref);
169 list_add_tail(&lkb->lkb_astqueue, &proc->asts);
170 lkb->lkb_ast_type |= type;
171 wake_up_interruptible(&proc->wait);
174 /* We want to copy the lvb to userspace when the completion
175 ast is read if the status is 0, the lock has an lvb and
176 lvb_ops says we should. We could probably have set_lvb_lock()
177 set update_user_lvb instead and not need old_mode */
179 if ((lkb->lkb_ast_type & AST_COMP) &&
180 (lkb->lkb_lksb->sb_status == 0) &&
181 lkb->lkb_lksb->sb_lvbptr &&
182 dlm_lvb_operations[ua->old_mode + 1][lkb->lkb_grmode + 1])
183 ua->update_user_lvb = 1;
185 ua->update_user_lvb = 0;
187 spin_unlock(&proc->asts_spin);
189 mutex_unlock(&ls->ls_clear_proc_locks);
192 static int device_user_lock(struct dlm_user_proc *proc,
193 struct dlm_lock_params *params)
196 struct dlm_user_args *ua;
199 ls = dlm_find_lockspace_local(proc->lockspace);
203 if (!params->castaddr || !params->lksb) {
208 ua = kzalloc(sizeof(struct dlm_user_args), GFP_KERNEL);
212 ua->user_lksb = params->lksb;
213 ua->castparam = params->castparam;
214 ua->castaddr = params->castaddr;
215 ua->bastparam = params->bastparam;
216 ua->bastaddr = params->bastaddr;
218 if (params->flags & DLM_LKF_CONVERT)
219 error = dlm_user_convert(ls, ua,
220 params->mode, params->flags,
221 params->lkid, params->lvb);
223 error = dlm_user_request(ls, ua,
224 params->mode, params->flags,
225 params->name, params->namelen,
228 error = ua->lksb.sb_lkid;
231 dlm_put_lockspace(ls);
235 static int device_user_unlock(struct dlm_user_proc *proc,
236 struct dlm_lock_params *params)
239 struct dlm_user_args *ua;
242 ls = dlm_find_lockspace_local(proc->lockspace);
246 ua = kzalloc(sizeof(struct dlm_user_args), GFP_KERNEL);
250 ua->user_lksb = params->lksb;
251 ua->castparam = params->castparam;
252 ua->castaddr = params->castaddr;
254 if (params->flags & DLM_LKF_CANCEL)
255 error = dlm_user_cancel(ls, ua, params->flags, params->lkid);
257 error = dlm_user_unlock(ls, ua, params->flags, params->lkid,
260 dlm_put_lockspace(ls);
264 static int device_create_lockspace(struct dlm_lspace_params *params)
266 dlm_lockspace_t *lockspace;
270 if (!capable(CAP_SYS_ADMIN))
273 error = dlm_new_lockspace(params->name, strlen(params->name),
274 &lockspace, 0, DLM_USER_LVB_LEN);
278 ls = dlm_find_lockspace_local(lockspace);
283 len = strlen(params->name) + strlen(name_prefix) + 2;
284 ls->ls_device.name = kzalloc(len, GFP_KERNEL);
285 if (!ls->ls_device.name)
287 snprintf((char *)ls->ls_device.name, len, "%s_%s", name_prefix,
289 ls->ls_device.fops = &device_fops;
290 ls->ls_device.minor = MISC_DYNAMIC_MINOR;
292 error = misc_register(&ls->ls_device);
294 kfree(ls->ls_device.name);
298 error = ls->ls_device.minor;
299 dlm_put_lockspace(ls);
303 dlm_put_lockspace(ls);
304 dlm_release_lockspace(lockspace, 0);
308 static int device_remove_lockspace(struct dlm_lspace_params *params)
310 dlm_lockspace_t *lockspace;
314 if (!capable(CAP_SYS_ADMIN))
317 ls = dlm_find_lockspace_device(params->minor);
321 error = misc_deregister(&ls->ls_device);
323 dlm_put_lockspace(ls);
326 kfree(ls->ls_device.name);
328 lockspace = ls->ls_local_handle;
330 /* dlm_release_lockspace waits for references to go to zero,
331 so all processes will need to close their device for the ls
332 before the release will procede */
334 dlm_put_lockspace(ls);
335 error = dlm_release_lockspace(lockspace, 0);
340 /* Check the user's version matches ours */
341 static int check_version(struct dlm_write_request *req)
343 if (req->version[0] != DLM_DEVICE_VERSION_MAJOR ||
344 (req->version[0] == DLM_DEVICE_VERSION_MAJOR &&
345 req->version[1] > DLM_DEVICE_VERSION_MINOR)) {
347 printk(KERN_DEBUG "dlm: process %s (%d) version mismatch "
348 "user (%d.%d.%d) kernel (%d.%d.%d)\n",
354 DLM_DEVICE_VERSION_MAJOR,
355 DLM_DEVICE_VERSION_MINOR,
356 DLM_DEVICE_VERSION_PATCH);
366 * dlm_user_request -> request_lock
367 * dlm_user_convert -> convert_lock
370 * dlm_user_unlock -> unlock_lock
371 * dlm_user_cancel -> cancel_lock
373 * device_create_lockspace
376 * device_remove_lockspace
377 * dlm_release_lockspace
380 /* a write to a lockspace device is a lock or unlock request, a write
381 to the control device is to create/remove a lockspace */
383 static ssize_t device_write(struct file *file, const char __user *buf,
384 size_t count, loff_t *ppos)
386 struct dlm_user_proc *proc = file->private_data;
387 struct dlm_write_request *kbuf;
388 sigset_t tmpsig, allsigs;
392 if (count < sizeof(struct dlm_write_request32))
394 if (count < sizeof(struct dlm_write_request))
398 kbuf = kmalloc(count, GFP_KERNEL);
402 if (copy_from_user(kbuf, buf, count)) {
407 if (check_version(kbuf)) {
413 if (!kbuf->is64bit) {
414 struct dlm_write_request32 *k32buf;
415 k32buf = (struct dlm_write_request32 *)kbuf;
416 kbuf = kmalloc(count + (sizeof(struct dlm_write_request) -
417 sizeof(struct dlm_write_request32)), GFP_KERNEL);
422 set_bit(DLM_PROC_FLAGS_COMPAT, &proc->flags);
423 compat_input(kbuf, k32buf);
428 /* do we really need this? can a write happen after a close? */
429 if ((kbuf->cmd == DLM_USER_LOCK || kbuf->cmd == DLM_USER_UNLOCK) &&
430 test_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags))
433 sigfillset(&allsigs);
434 sigprocmask(SIG_BLOCK, &allsigs, &tmpsig);
442 log_print("no locking on control device");
445 error = device_user_lock(proc, &kbuf->i.lock);
448 case DLM_USER_UNLOCK:
450 log_print("no locking on control device");
453 error = device_user_unlock(proc, &kbuf->i.lock);
456 case DLM_USER_CREATE_LOCKSPACE:
458 log_print("create/remove only on control device");
461 error = device_create_lockspace(&kbuf->i.lspace);
464 case DLM_USER_REMOVE_LOCKSPACE:
466 log_print("create/remove only on control device");
469 error = device_remove_lockspace(&kbuf->i.lspace);
473 log_print("Unknown command passed to DLM device : %d\n",
478 sigprocmask(SIG_SETMASK, &tmpsig, NULL);
485 /* Every process that opens the lockspace device has its own "proc" structure
486 hanging off the open file that's used to keep track of locks owned by the
487 process and asts that need to be delivered to the process. */
489 static int device_open(struct inode *inode, struct file *file)
491 struct dlm_user_proc *proc;
494 ls = dlm_find_lockspace_device(iminor(inode));
498 proc = kzalloc(sizeof(struct dlm_user_proc), GFP_KERNEL);
500 dlm_put_lockspace(ls);
504 proc->lockspace = ls->ls_local_handle;
505 INIT_LIST_HEAD(&proc->asts);
506 INIT_LIST_HEAD(&proc->locks);
507 spin_lock_init(&proc->asts_spin);
508 spin_lock_init(&proc->locks_spin);
509 init_waitqueue_head(&proc->wait);
510 file->private_data = proc;
515 static int device_close(struct inode *inode, struct file *file)
517 struct dlm_user_proc *proc = file->private_data;
519 sigset_t tmpsig, allsigs;
521 ls = dlm_find_lockspace_local(proc->lockspace);
525 sigfillset(&allsigs);
526 sigprocmask(SIG_BLOCK, &allsigs, &tmpsig);
528 set_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags);
530 dlm_clear_proc_locks(ls, proc);
532 /* at this point no more lkb's should exist for this lockspace,
533 so there's no chance of dlm_user_add_ast() being called and
534 looking for lkb->ua->proc */
537 file->private_data = NULL;
539 dlm_put_lockspace(ls);
540 dlm_put_lockspace(ls); /* for the find in device_open() */
542 /* FIXME: AUTOFREE: if this ls is no longer used do
543 device_remove_lockspace() */
545 sigprocmask(SIG_SETMASK, &tmpsig, NULL);
551 static int copy_result_to_user(struct dlm_user_args *ua, int compat, int type,
552 int bmode, char __user *buf, size_t count)
555 struct dlm_lock_result32 result32;
557 struct dlm_lock_result result;
563 memset(&result, 0, sizeof(struct dlm_lock_result));
564 memcpy(&result.lksb, &ua->lksb, sizeof(struct dlm_lksb));
565 result.user_lksb = ua->user_lksb;
567 /* FIXME: dlm1 provides for the user's bastparam/addr to not be updated
568 in a conversion unless the conversion is successful. See code
569 in dlm_user_convert() for updating ua from ua_tmp. OpenVMS, though,
570 notes that a new blocking AST address and parameter are set even if
571 the conversion fails, so maybe we should just do that. */
573 if (type == AST_BAST) {
574 result.user_astaddr = ua->bastaddr;
575 result.user_astparam = ua->bastparam;
576 result.bast_mode = bmode;
578 result.user_astaddr = ua->castaddr;
579 result.user_astparam = ua->castparam;
584 len = sizeof(struct dlm_lock_result32);
587 len = sizeof(struct dlm_lock_result);
590 /* copy lvb to userspace if there is one, it's been updated, and
591 the user buffer has space for it */
593 if (ua->update_user_lvb && ua->lksb.sb_lvbptr &&
594 count >= len + DLM_USER_LVB_LEN) {
595 if (copy_to_user(buf+len, ua->lksb.sb_lvbptr,
601 result.lvb_offset = len;
602 len += DLM_USER_LVB_LEN;
609 compat_output(&result, &result32);
610 resultptr = &result32;
614 if (copy_to_user(buf, resultptr, struct_len))
622 /* a read returns a single ast described in a struct dlm_lock_result */
624 static ssize_t device_read(struct file *file, char __user *buf, size_t count,
627 struct dlm_user_proc *proc = file->private_data;
629 struct dlm_user_args *ua;
630 DECLARE_WAITQUEUE(wait, current);
631 int error, type=0, bmode=0, removed = 0;
634 if (count < sizeof(struct dlm_lock_result32))
636 if (count < sizeof(struct dlm_lock_result))
640 /* do we really need this? can a read happen after a close? */
641 if (test_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags))
644 spin_lock(&proc->asts_spin);
645 if (list_empty(&proc->asts)) {
646 if (file->f_flags & O_NONBLOCK) {
647 spin_unlock(&proc->asts_spin);
651 add_wait_queue(&proc->wait, &wait);
654 set_current_state(TASK_INTERRUPTIBLE);
655 if (list_empty(&proc->asts) && !signal_pending(current)) {
656 spin_unlock(&proc->asts_spin);
658 spin_lock(&proc->asts_spin);
661 set_current_state(TASK_RUNNING);
662 remove_wait_queue(&proc->wait, &wait);
664 if (signal_pending(current)) {
665 spin_unlock(&proc->asts_spin);
670 if (list_empty(&proc->asts)) {
671 spin_unlock(&proc->asts_spin);
675 /* there may be both completion and blocking asts to return for
676 the lkb, don't remove lkb from asts list unless no asts remain */
678 lkb = list_entry(proc->asts.next, struct dlm_lkb, lkb_astqueue);
680 if (lkb->lkb_ast_type & AST_COMP) {
681 lkb->lkb_ast_type &= ~AST_COMP;
683 } else if (lkb->lkb_ast_type & AST_BAST) {
684 lkb->lkb_ast_type &= ~AST_BAST;
686 bmode = lkb->lkb_bastmode;
689 if (!lkb->lkb_ast_type) {
690 list_del(&lkb->lkb_astqueue);
693 spin_unlock(&proc->asts_spin);
695 ua = (struct dlm_user_args *)lkb->lkb_astparam;
696 error = copy_result_to_user(ua,
697 test_bit(DLM_PROC_FLAGS_COMPAT, &proc->flags),
698 type, bmode, buf, count);
700 /* removes reference for the proc->asts lists added by
701 dlm_user_add_ast() and may result in the lkb being freed */
708 static unsigned int device_poll(struct file *file, poll_table *wait)
710 struct dlm_user_proc *proc = file->private_data;
712 poll_wait(file, &proc->wait, wait);
714 spin_lock(&proc->asts_spin);
715 if (!list_empty(&proc->asts)) {
716 spin_unlock(&proc->asts_spin);
717 return POLLIN | POLLRDNORM;
719 spin_unlock(&proc->asts_spin);
723 static int ctl_device_open(struct inode *inode, struct file *file)
725 file->private_data = NULL;
729 static int ctl_device_close(struct inode *inode, struct file *file)
734 static struct file_operations device_fops = {
736 .release = device_close,
738 .write = device_write,
740 .owner = THIS_MODULE,
743 static struct file_operations ctl_device_fops = {
744 .open = ctl_device_open,
745 .release = ctl_device_close,
746 .write = device_write,
747 .owner = THIS_MODULE,
750 int dlm_user_init(void)
754 ctl_device.name = "dlm-control";
755 ctl_device.fops = &ctl_device_fops;
756 ctl_device.minor = MISC_DYNAMIC_MINOR;
758 error = misc_register(&ctl_device);
760 log_print("misc_register failed for control device");
765 void dlm_user_exit(void)
767 misc_deregister(&ctl_device);