2 * Copyright (C) 2006-2007 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;
131 /* we could possibly check if the cancel of an orphan has resulted in the lkb
132 being removed and then remove that lkb from the orphans list and free it */
134 void dlm_user_add_ast(struct dlm_lkb *lkb, int type)
137 struct dlm_user_args *ua;
138 struct dlm_user_proc *proc;
139 int eol = 0, ast_type;
141 if (lkb->lkb_flags & (DLM_IFL_ORPHAN | DLM_IFL_DEAD))
144 ls = lkb->lkb_resource->res_ls;
145 mutex_lock(&ls->ls_clear_proc_locks);
147 /* If ORPHAN/DEAD flag is set, it means the process is dead so an ast
148 can't be delivered. For ORPHAN's, dlm_clear_proc_locks() freed
149 lkb->ua so we can't try to use it. This second check is necessary
150 for cases where a completion ast is received for an operation that
151 began before clear_proc_locks did its cancel/unlock. */
153 if (lkb->lkb_flags & (DLM_IFL_ORPHAN | DLM_IFL_DEAD))
156 DLM_ASSERT(lkb->lkb_astparam, dlm_print_lkb(lkb););
157 ua = (struct dlm_user_args *)lkb->lkb_astparam;
160 if (type == AST_BAST && ua->bastaddr == NULL)
163 spin_lock(&proc->asts_spin);
165 ast_type = lkb->lkb_ast_type;
166 lkb->lkb_ast_type |= type;
169 kref_get(&lkb->lkb_ref);
170 list_add_tail(&lkb->lkb_astqueue, &proc->asts);
171 wake_up_interruptible(&proc->wait);
173 if (type == AST_COMP && (ast_type & AST_COMP))
174 log_debug(ls, "ast overlap %x status %x %x",
175 lkb->lkb_id, ua->lksb.sb_status, lkb->lkb_flags);
177 /* Figure out if this lock is at the end of its life and no longer
178 available for the application to use. The lkb still exists until
179 the final ast is read. A lock becomes EOL in three situations:
180 1. a noqueue request fails with EAGAIN
181 2. an unlock completes with EUNLOCK
182 3. a cancel of a waiting request completes with ECANCEL
183 An EOL lock needs to be removed from the process's list of locks.
184 And we can't allow any new operation on an EOL lock. This is
185 not related to the lifetime of the lkb struct which is managed
186 entirely by refcount. */
188 if (type == AST_COMP &&
189 lkb->lkb_grmode == DLM_LOCK_IV &&
190 ua->lksb.sb_status == -EAGAIN)
192 else if (ua->lksb.sb_status == -DLM_EUNLOCK ||
193 (ua->lksb.sb_status == -DLM_ECANCEL &&
194 lkb->lkb_grmode == DLM_LOCK_IV))
197 lkb->lkb_ast_type &= ~AST_BAST;
198 lkb->lkb_flags |= DLM_IFL_ENDOFLIFE;
201 /* We want to copy the lvb to userspace when the completion
202 ast is read if the status is 0, the lock has an lvb and
203 lvb_ops says we should. We could probably have set_lvb_lock()
204 set update_user_lvb instead and not need old_mode */
206 if ((lkb->lkb_ast_type & AST_COMP) &&
207 (lkb->lkb_lksb->sb_status == 0) &&
208 lkb->lkb_lksb->sb_lvbptr &&
209 dlm_lvb_operations[ua->old_mode + 1][lkb->lkb_grmode + 1])
210 ua->update_user_lvb = 1;
212 ua->update_user_lvb = 0;
214 spin_unlock(&proc->asts_spin);
217 spin_lock(&ua->proc->locks_spin);
218 if (!list_empty(&lkb->lkb_ownqueue)) {
219 list_del_init(&lkb->lkb_ownqueue);
222 spin_unlock(&ua->proc->locks_spin);
225 mutex_unlock(&ls->ls_clear_proc_locks);
228 static int device_user_lock(struct dlm_user_proc *proc,
229 struct dlm_lock_params *params)
232 struct dlm_user_args *ua;
235 ls = dlm_find_lockspace_local(proc->lockspace);
239 if (!params->castaddr || !params->lksb) {
244 ua = kzalloc(sizeof(struct dlm_user_args), GFP_KERNEL);
248 ua->user_lksb = params->lksb;
249 ua->castparam = params->castparam;
250 ua->castaddr = params->castaddr;
251 ua->bastparam = params->bastparam;
252 ua->bastaddr = params->bastaddr;
254 if (params->flags & DLM_LKF_CONVERT)
255 error = dlm_user_convert(ls, ua,
256 params->mode, params->flags,
257 params->lkid, params->lvb);
259 error = dlm_user_request(ls, ua,
260 params->mode, params->flags,
261 params->name, params->namelen,
264 error = ua->lksb.sb_lkid;
267 dlm_put_lockspace(ls);
271 static int device_user_unlock(struct dlm_user_proc *proc,
272 struct dlm_lock_params *params)
275 struct dlm_user_args *ua;
278 ls = dlm_find_lockspace_local(proc->lockspace);
282 ua = kzalloc(sizeof(struct dlm_user_args), GFP_KERNEL);
286 ua->user_lksb = params->lksb;
287 ua->castparam = params->castparam;
288 ua->castaddr = params->castaddr;
290 if (params->flags & DLM_LKF_CANCEL)
291 error = dlm_user_cancel(ls, ua, params->flags, params->lkid);
293 error = dlm_user_unlock(ls, ua, params->flags, params->lkid,
296 dlm_put_lockspace(ls);
300 static int create_misc_device(struct dlm_ls *ls, char *name)
305 len = strlen(name) + strlen(name_prefix) + 2;
306 ls->ls_device.name = kzalloc(len, GFP_KERNEL);
307 if (!ls->ls_device.name)
310 snprintf((char *)ls->ls_device.name, len, "%s_%s", name_prefix,
312 ls->ls_device.fops = &device_fops;
313 ls->ls_device.minor = MISC_DYNAMIC_MINOR;
315 error = misc_register(&ls->ls_device);
317 kfree(ls->ls_device.name);
323 static int device_create_lockspace(struct dlm_lspace_params *params)
325 dlm_lockspace_t *lockspace;
329 if (!capable(CAP_SYS_ADMIN))
332 error = dlm_new_lockspace(params->name, strlen(params->name),
333 &lockspace, 0, DLM_USER_LVB_LEN);
337 ls = dlm_find_lockspace_local(lockspace);
341 error = create_misc_device(ls, params->name);
342 dlm_put_lockspace(ls);
345 dlm_release_lockspace(lockspace, 0);
347 error = ls->ls_device.minor;
352 static int device_remove_lockspace(struct dlm_lspace_params *params)
354 dlm_lockspace_t *lockspace;
356 int error, force = 0;
358 if (!capable(CAP_SYS_ADMIN))
361 ls = dlm_find_lockspace_device(params->minor);
365 /* Deregister the misc device first, so we don't have
366 * a device that's not attached to a lockspace. If
367 * dlm_release_lockspace fails then we can recreate it
369 error = misc_deregister(&ls->ls_device);
371 dlm_put_lockspace(ls);
374 kfree(ls->ls_device.name);
376 if (params->flags & DLM_USER_LSFLG_FORCEFREE)
379 lockspace = ls->ls_local_handle;
381 /* dlm_release_lockspace waits for references to go to zero,
382 so all processes will need to close their device for the ls
383 before the release will procede */
385 dlm_put_lockspace(ls);
386 error = dlm_release_lockspace(lockspace, force);
388 create_misc_device(ls, ls->ls_name);
393 /* Check the user's version matches ours */
394 static int check_version(struct dlm_write_request *req)
396 if (req->version[0] != DLM_DEVICE_VERSION_MAJOR ||
397 (req->version[0] == DLM_DEVICE_VERSION_MAJOR &&
398 req->version[1] > DLM_DEVICE_VERSION_MINOR)) {
400 printk(KERN_DEBUG "dlm: process %s (%d) version mismatch "
401 "user (%d.%d.%d) kernel (%d.%d.%d)\n",
407 DLM_DEVICE_VERSION_MAJOR,
408 DLM_DEVICE_VERSION_MINOR,
409 DLM_DEVICE_VERSION_PATCH);
419 * dlm_user_request -> request_lock
420 * dlm_user_convert -> convert_lock
423 * dlm_user_unlock -> unlock_lock
424 * dlm_user_cancel -> cancel_lock
426 * device_create_lockspace
429 * device_remove_lockspace
430 * dlm_release_lockspace
433 /* a write to a lockspace device is a lock or unlock request, a write
434 to the control device is to create/remove a lockspace */
436 static ssize_t device_write(struct file *file, const char __user *buf,
437 size_t count, loff_t *ppos)
439 struct dlm_user_proc *proc = file->private_data;
440 struct dlm_write_request *kbuf;
441 sigset_t tmpsig, allsigs;
445 if (count < sizeof(struct dlm_write_request32))
447 if (count < sizeof(struct dlm_write_request))
451 kbuf = kmalloc(count, GFP_KERNEL);
455 if (copy_from_user(kbuf, buf, count)) {
460 if (check_version(kbuf)) {
466 if (!kbuf->is64bit) {
467 struct dlm_write_request32 *k32buf;
468 k32buf = (struct dlm_write_request32 *)kbuf;
469 kbuf = kmalloc(count + (sizeof(struct dlm_write_request) -
470 sizeof(struct dlm_write_request32)), GFP_KERNEL);
475 set_bit(DLM_PROC_FLAGS_COMPAT, &proc->flags);
476 compat_input(kbuf, k32buf);
481 /* do we really need this? can a write happen after a close? */
482 if ((kbuf->cmd == DLM_USER_LOCK || kbuf->cmd == DLM_USER_UNLOCK) &&
483 test_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags))
486 sigfillset(&allsigs);
487 sigprocmask(SIG_BLOCK, &allsigs, &tmpsig);
495 log_print("no locking on control device");
498 error = device_user_lock(proc, &kbuf->i.lock);
501 case DLM_USER_UNLOCK:
503 log_print("no locking on control device");
506 error = device_user_unlock(proc, &kbuf->i.lock);
509 case DLM_USER_CREATE_LOCKSPACE:
511 log_print("create/remove only on control device");
514 error = device_create_lockspace(&kbuf->i.lspace);
517 case DLM_USER_REMOVE_LOCKSPACE:
519 log_print("create/remove only on control device");
522 error = device_remove_lockspace(&kbuf->i.lspace);
526 log_print("Unknown command passed to DLM device : %d\n",
531 sigprocmask(SIG_SETMASK, &tmpsig, NULL);
538 /* Every process that opens the lockspace device has its own "proc" structure
539 hanging off the open file that's used to keep track of locks owned by the
540 process and asts that need to be delivered to the process. */
542 static int device_open(struct inode *inode, struct file *file)
544 struct dlm_user_proc *proc;
547 ls = dlm_find_lockspace_device(iminor(inode));
551 proc = kzalloc(sizeof(struct dlm_user_proc), GFP_KERNEL);
553 dlm_put_lockspace(ls);
557 proc->lockspace = ls->ls_local_handle;
558 INIT_LIST_HEAD(&proc->asts);
559 INIT_LIST_HEAD(&proc->locks);
560 INIT_LIST_HEAD(&proc->unlocking);
561 spin_lock_init(&proc->asts_spin);
562 spin_lock_init(&proc->locks_spin);
563 init_waitqueue_head(&proc->wait);
564 file->private_data = proc;
569 static int device_close(struct inode *inode, struct file *file)
571 struct dlm_user_proc *proc = file->private_data;
573 sigset_t tmpsig, allsigs;
575 ls = dlm_find_lockspace_local(proc->lockspace);
579 sigfillset(&allsigs);
580 sigprocmask(SIG_BLOCK, &allsigs, &tmpsig);
582 set_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags);
584 dlm_clear_proc_locks(ls, proc);
586 /* at this point no more lkb's should exist for this lockspace,
587 so there's no chance of dlm_user_add_ast() being called and
588 looking for lkb->ua->proc */
591 file->private_data = NULL;
593 dlm_put_lockspace(ls);
594 dlm_put_lockspace(ls); /* for the find in device_open() */
596 /* FIXME: AUTOFREE: if this ls is no longer used do
597 device_remove_lockspace() */
599 sigprocmask(SIG_SETMASK, &tmpsig, NULL);
605 static int copy_result_to_user(struct dlm_user_args *ua, int compat, int type,
606 int bmode, char __user *buf, size_t count)
609 struct dlm_lock_result32 result32;
611 struct dlm_lock_result result;
617 memset(&result, 0, sizeof(struct dlm_lock_result));
618 memcpy(&result.lksb, &ua->lksb, sizeof(struct dlm_lksb));
619 result.user_lksb = ua->user_lksb;
621 /* FIXME: dlm1 provides for the user's bastparam/addr to not be updated
622 in a conversion unless the conversion is successful. See code
623 in dlm_user_convert() for updating ua from ua_tmp. OpenVMS, though,
624 notes that a new blocking AST address and parameter are set even if
625 the conversion fails, so maybe we should just do that. */
627 if (type == AST_BAST) {
628 result.user_astaddr = ua->bastaddr;
629 result.user_astparam = ua->bastparam;
630 result.bast_mode = bmode;
632 result.user_astaddr = ua->castaddr;
633 result.user_astparam = ua->castparam;
638 len = sizeof(struct dlm_lock_result32);
641 len = sizeof(struct dlm_lock_result);
644 /* copy lvb to userspace if there is one, it's been updated, and
645 the user buffer has space for it */
647 if (ua->update_user_lvb && ua->lksb.sb_lvbptr &&
648 count >= len + DLM_USER_LVB_LEN) {
649 if (copy_to_user(buf+len, ua->lksb.sb_lvbptr,
655 result.lvb_offset = len;
656 len += DLM_USER_LVB_LEN;
663 compat_output(&result, &result32);
664 resultptr = &result32;
668 if (copy_to_user(buf, resultptr, struct_len))
676 /* a read returns a single ast described in a struct dlm_lock_result */
678 static ssize_t device_read(struct file *file, char __user *buf, size_t count,
681 struct dlm_user_proc *proc = file->private_data;
683 struct dlm_user_args *ua;
684 DECLARE_WAITQUEUE(wait, current);
685 int error, type=0, bmode=0, removed = 0;
688 if (count < sizeof(struct dlm_lock_result32))
690 if (count < sizeof(struct dlm_lock_result))
694 /* do we really need this? can a read happen after a close? */
695 if (test_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags))
698 spin_lock(&proc->asts_spin);
699 if (list_empty(&proc->asts)) {
700 if (file->f_flags & O_NONBLOCK) {
701 spin_unlock(&proc->asts_spin);
705 add_wait_queue(&proc->wait, &wait);
708 set_current_state(TASK_INTERRUPTIBLE);
709 if (list_empty(&proc->asts) && !signal_pending(current)) {
710 spin_unlock(&proc->asts_spin);
712 spin_lock(&proc->asts_spin);
715 set_current_state(TASK_RUNNING);
716 remove_wait_queue(&proc->wait, &wait);
718 if (signal_pending(current)) {
719 spin_unlock(&proc->asts_spin);
724 if (list_empty(&proc->asts)) {
725 spin_unlock(&proc->asts_spin);
729 /* there may be both completion and blocking asts to return for
730 the lkb, don't remove lkb from asts list unless no asts remain */
732 lkb = list_entry(proc->asts.next, struct dlm_lkb, lkb_astqueue);
734 if (lkb->lkb_ast_type & AST_COMP) {
735 lkb->lkb_ast_type &= ~AST_COMP;
737 } else if (lkb->lkb_ast_type & AST_BAST) {
738 lkb->lkb_ast_type &= ~AST_BAST;
740 bmode = lkb->lkb_bastmode;
743 if (!lkb->lkb_ast_type) {
744 list_del(&lkb->lkb_astqueue);
747 spin_unlock(&proc->asts_spin);
749 ua = (struct dlm_user_args *)lkb->lkb_astparam;
750 error = copy_result_to_user(ua,
751 test_bit(DLM_PROC_FLAGS_COMPAT, &proc->flags),
752 type, bmode, buf, count);
754 /* removes reference for the proc->asts lists added by
755 dlm_user_add_ast() and may result in the lkb being freed */
762 static unsigned int device_poll(struct file *file, poll_table *wait)
764 struct dlm_user_proc *proc = file->private_data;
766 poll_wait(file, &proc->wait, wait);
768 spin_lock(&proc->asts_spin);
769 if (!list_empty(&proc->asts)) {
770 spin_unlock(&proc->asts_spin);
771 return POLLIN | POLLRDNORM;
773 spin_unlock(&proc->asts_spin);
777 static int ctl_device_open(struct inode *inode, struct file *file)
779 file->private_data = NULL;
783 static int ctl_device_close(struct inode *inode, struct file *file)
788 static const struct file_operations device_fops = {
790 .release = device_close,
792 .write = device_write,
794 .owner = THIS_MODULE,
797 static const struct file_operations ctl_device_fops = {
798 .open = ctl_device_open,
799 .release = ctl_device_close,
800 .write = device_write,
801 .owner = THIS_MODULE,
804 int dlm_user_init(void)
808 ctl_device.name = "dlm-control";
809 ctl_device.fops = &ctl_device_fops;
810 ctl_device.minor = MISC_DYNAMIC_MINOR;
812 error = misc_register(&ctl_device);
814 log_print("misc_register failed for control device");
819 void dlm_user_exit(void)
821 misc_deregister(&ctl_device);