📄 user.c
字号:
req->version[1], req->version[2], DLM_DEVICE_VERSION_MAJOR, DLM_DEVICE_VERSION_MINOR, DLM_DEVICE_VERSION_PATCH); return -EINVAL; } return 0;}/* * device_write * * device_user_lock * dlm_user_request -> request_lock * dlm_user_convert -> convert_lock * * device_user_unlock * dlm_user_unlock -> unlock_lock * dlm_user_cancel -> cancel_lock * * device_create_lockspace * dlm_new_lockspace * * device_remove_lockspace * dlm_release_lockspace *//* a write to a lockspace device is a lock or unlock request, a write to the control device is to create/remove a lockspace */static ssize_t device_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos){ struct dlm_user_proc *proc = file->private_data; struct dlm_write_request *kbuf; sigset_t tmpsig, allsigs; int error;#ifdef CONFIG_COMPAT if (count < sizeof(struct dlm_write_request32))#else if (count < sizeof(struct dlm_write_request))#endif return -EINVAL; kbuf = kmalloc(count, GFP_KERNEL); if (!kbuf) return -ENOMEM; if (copy_from_user(kbuf, buf, count)) { error = -EFAULT; goto out_free; } if (check_version(kbuf)) { error = -EBADE; goto out_free; }#ifdef CONFIG_COMPAT if (!kbuf->is64bit) { struct dlm_write_request32 *k32buf; k32buf = (struct dlm_write_request32 *)kbuf; kbuf = kmalloc(count + (sizeof(struct dlm_write_request) - sizeof(struct dlm_write_request32)), GFP_KERNEL); if (!kbuf) return -ENOMEM; if (proc) set_bit(DLM_PROC_FLAGS_COMPAT, &proc->flags); compat_input(kbuf, k32buf); kfree(k32buf); }#endif /* do we really need this? can a write happen after a close? */ if ((kbuf->cmd == DLM_USER_LOCK || kbuf->cmd == DLM_USER_UNLOCK) && test_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags)) return -EINVAL; sigfillset(&allsigs); sigprocmask(SIG_BLOCK, &allsigs, &tmpsig); error = -EINVAL; switch (kbuf->cmd) { case DLM_USER_LOCK: if (!proc) { log_print("no locking on control device"); goto out_sig; } error = device_user_lock(proc, &kbuf->i.lock); break; case DLM_USER_UNLOCK: if (!proc) { log_print("no locking on control device"); goto out_sig; } error = device_user_unlock(proc, &kbuf->i.lock); break; case DLM_USER_DEADLOCK: if (!proc) { log_print("no locking on control device"); goto out_sig; } error = device_user_deadlock(proc, &kbuf->i.lock); break; case DLM_USER_CREATE_LOCKSPACE: if (proc) { log_print("create/remove only on control device"); goto out_sig; } error = device_create_lockspace(&kbuf->i.lspace); break; case DLM_USER_REMOVE_LOCKSPACE: if (proc) { log_print("create/remove only on control device"); goto out_sig; } error = device_remove_lockspace(&kbuf->i.lspace); break; case DLM_USER_PURGE: if (!proc) { log_print("no locking on control device"); goto out_sig; } error = device_user_purge(proc, &kbuf->i.purge); break; default: log_print("Unknown command passed to DLM device : %d\n", kbuf->cmd); } out_sig: sigprocmask(SIG_SETMASK, &tmpsig, NULL); recalc_sigpending(); out_free: kfree(kbuf); return error;}/* Every process that opens the lockspace device has its own "proc" structure hanging off the open file that's used to keep track of locks owned by the process and asts that need to be delivered to the process. */static int device_open(struct inode *inode, struct file *file){ struct dlm_user_proc *proc; struct dlm_ls *ls; ls = dlm_find_lockspace_device(iminor(inode)); if (!ls) return -ENOENT; proc = kzalloc(sizeof(struct dlm_user_proc), GFP_KERNEL); if (!proc) { dlm_put_lockspace(ls); return -ENOMEM; } proc->lockspace = ls->ls_local_handle; INIT_LIST_HEAD(&proc->asts); INIT_LIST_HEAD(&proc->locks); INIT_LIST_HEAD(&proc->unlocking); spin_lock_init(&proc->asts_spin); spin_lock_init(&proc->locks_spin); init_waitqueue_head(&proc->wait); file->private_data = proc; return 0;}static int device_close(struct inode *inode, struct file *file){ struct dlm_user_proc *proc = file->private_data; struct dlm_ls *ls; sigset_t tmpsig, allsigs; ls = dlm_find_lockspace_local(proc->lockspace); if (!ls) return -ENOENT; sigfillset(&allsigs); sigprocmask(SIG_BLOCK, &allsigs, &tmpsig); set_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags); dlm_clear_proc_locks(ls, proc); /* at this point no more lkb's should exist for this lockspace, so there's no chance of dlm_user_add_ast() being called and looking for lkb->ua->proc */ kfree(proc); file->private_data = NULL; dlm_put_lockspace(ls); dlm_put_lockspace(ls); /* for the find in device_open() */ /* FIXME: AUTOFREE: if this ls is no longer used do device_remove_lockspace() */ sigprocmask(SIG_SETMASK, &tmpsig, NULL); recalc_sigpending(); return 0;}static int copy_result_to_user(struct dlm_user_args *ua, int compat, int type, int bmode, char __user *buf, size_t count){#ifdef CONFIG_COMPAT struct dlm_lock_result32 result32;#endif struct dlm_lock_result result; void *resultptr; int error=0; int len; int struct_len; memset(&result, 0, sizeof(struct dlm_lock_result)); result.version[0] = DLM_DEVICE_VERSION_MAJOR; result.version[1] = DLM_DEVICE_VERSION_MINOR; result.version[2] = DLM_DEVICE_VERSION_PATCH; memcpy(&result.lksb, &ua->lksb, sizeof(struct dlm_lksb)); result.user_lksb = ua->user_lksb; /* FIXME: dlm1 provides for the user's bastparam/addr to not be updated in a conversion unless the conversion is successful. See code in dlm_user_convert() for updating ua from ua_tmp. OpenVMS, though, notes that a new blocking AST address and parameter are set even if the conversion fails, so maybe we should just do that. */ if (type == AST_BAST) { result.user_astaddr = ua->bastaddr; result.user_astparam = ua->bastparam; result.bast_mode = bmode; } else { result.user_astaddr = ua->castaddr; result.user_astparam = ua->castparam; }#ifdef CONFIG_COMPAT if (compat) len = sizeof(struct dlm_lock_result32); else#endif len = sizeof(struct dlm_lock_result); struct_len = len; /* copy lvb to userspace if there is one, it's been updated, and the user buffer has space for it */ if (ua->update_user_lvb && ua->lksb.sb_lvbptr && count >= len + DLM_USER_LVB_LEN) { if (copy_to_user(buf+len, ua->lksb.sb_lvbptr, DLM_USER_LVB_LEN)) { error = -EFAULT; goto out; } result.lvb_offset = len; len += DLM_USER_LVB_LEN; } result.length = len; resultptr = &result;#ifdef CONFIG_COMPAT if (compat) { compat_output(&result, &result32); resultptr = &result32; }#endif if (copy_to_user(buf, resultptr, struct_len)) error = -EFAULT; else error = len; out: return error;}static int copy_version_to_user(char __user *buf, size_t count){ struct dlm_device_version ver; memset(&ver, 0, sizeof(struct dlm_device_version)); ver.version[0] = DLM_DEVICE_VERSION_MAJOR; ver.version[1] = DLM_DEVICE_VERSION_MINOR; ver.version[2] = DLM_DEVICE_VERSION_PATCH; if (copy_to_user(buf, &ver, sizeof(struct dlm_device_version))) return -EFAULT; return sizeof(struct dlm_device_version);}/* a read returns a single ast described in a struct dlm_lock_result */static ssize_t device_read(struct file *file, char __user *buf, size_t count, loff_t *ppos){ struct dlm_user_proc *proc = file->private_data; struct dlm_lkb *lkb; struct dlm_user_args *ua; DECLARE_WAITQUEUE(wait, current); int error, type=0, bmode=0, removed = 0; if (count == sizeof(struct dlm_device_version)) { error = copy_version_to_user(buf, count); return error; } if (!proc) { log_print("non-version read from control device %zu", count); return -EINVAL; }#ifdef CONFIG_COMPAT if (count < sizeof(struct dlm_lock_result32))#else if (count < sizeof(struct dlm_lock_result))#endif return -EINVAL; /* do we really need this? can a read happen after a close? */ if (test_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags)) return -EINVAL; spin_lock(&proc->asts_spin); if (list_empty(&proc->asts)) { if (file->f_flags & O_NONBLOCK) { spin_unlock(&proc->asts_spin); return -EAGAIN; } add_wait_queue(&proc->wait, &wait); repeat: set_current_state(TASK_INTERRUPTIBLE); if (list_empty(&proc->asts) && !signal_pending(current)) { spin_unlock(&proc->asts_spin); schedule(); spin_lock(&proc->asts_spin); goto repeat; } set_current_state(TASK_RUNNING); remove_wait_queue(&proc->wait, &wait); if (signal_pending(current)) { spin_unlock(&proc->asts_spin); return -ERESTARTSYS; } } /* there may be both completion and blocking asts to return for the lkb, don't remove lkb from asts list unless no asts remain */ lkb = list_entry(proc->asts.next, struct dlm_lkb, lkb_astqueue); if (lkb->lkb_ast_type & AST_COMP) { lkb->lkb_ast_type &= ~AST_COMP; type = AST_COMP; } else if (lkb->lkb_ast_type & AST_BAST) { lkb->lkb_ast_type &= ~AST_BAST; type = AST_BAST; bmode = lkb->lkb_bastmode; } if (!lkb->lkb_ast_type) { list_del(&lkb->lkb_astqueue); removed = 1; } spin_unlock(&proc->asts_spin); ua = (struct dlm_user_args *)lkb->lkb_astparam; error = copy_result_to_user(ua, test_bit(DLM_PROC_FLAGS_COMPAT, &proc->flags), type, bmode, buf, count); /* removes reference for the proc->asts lists added by dlm_user_add_ast() and may result in the lkb being freed */ if (removed) dlm_put_lkb(lkb); return error;}static unsigned int device_poll(struct file *file, poll_table *wait){ struct dlm_user_proc *proc = file->private_data; poll_wait(file, &proc->wait, wait); spin_lock(&proc->asts_spin); if (!list_empty(&proc->asts)) { spin_unlock(&proc->asts_spin); return POLLIN | POLLRDNORM; } spin_unlock(&proc->asts_spin); return 0;}static int ctl_device_open(struct inode *inode, struct file *file){ file->private_data = NULL; return 0;}static int ctl_device_close(struct inode *inode, struct file *file){ return 0;}static const struct file_operations device_fops = { .open = device_open, .release = device_close, .read = device_read, .write = device_write, .poll = device_poll, .owner = THIS_MODULE,};static const struct file_operations ctl_device_fops = { .open = ctl_device_open, .release = ctl_device_close, .read = device_read, .write = device_write, .owner = THIS_MODULE,};int dlm_user_init(void){ int error; ctl_device.name = "dlm-control"; ctl_device.fops = &ctl_device_fops; ctl_device.minor = MISC_DYNAMIC_MINOR; error = misc_register(&ctl_device); if (error) log_print("misc_register failed for control device"); return error;}void dlm_user_exit(void){ misc_deregister(&ctl_device);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -