0001
0002
0003
0004
0005
0006 #include <linux/miscdevice.h>
0007 #include <linux/init.h>
0008 #include <linux/wait.h>
0009 #include <linux/file.h>
0010 #include <linux/fs.h>
0011 #include <linux/poll.h>
0012 #include <linux/signal.h>
0013 #include <linux/spinlock.h>
0014 #include <linux/dlm.h>
0015 #include <linux/dlm_device.h>
0016 #include <linux/slab.h>
0017 #include <linux/sched/signal.h>
0018
0019 #include "dlm_internal.h"
0020 #include "lockspace.h"
0021 #include "lock.h"
0022 #include "lvb_table.h"
0023 #include "user.h"
0024 #include "ast.h"
0025 #include "config.h"
0026
0027 static const char name_prefix[] = "dlm";
0028 static const struct file_operations device_fops;
0029 static atomic_t dlm_monitor_opened;
0030 static int dlm_monitor_unused = 1;
0031
0032 #ifdef CONFIG_COMPAT
0033
0034 struct dlm_lock_params32 {
0035 __u8 mode;
0036 __u8 namelen;
0037 __u16 unused;
0038 __u32 flags;
0039 __u32 lkid;
0040 __u32 parent;
0041 __u64 xid;
0042 __u64 timeout;
0043 __u32 castparam;
0044 __u32 castaddr;
0045 __u32 bastparam;
0046 __u32 bastaddr;
0047 __u32 lksb;
0048 char lvb[DLM_USER_LVB_LEN];
0049 char name[];
0050 };
0051
0052 struct dlm_write_request32 {
0053 __u32 version[3];
0054 __u8 cmd;
0055 __u8 is64bit;
0056 __u8 unused[2];
0057
0058 union {
0059 struct dlm_lock_params32 lock;
0060 struct dlm_lspace_params lspace;
0061 struct dlm_purge_params purge;
0062 } i;
0063 };
0064
0065 struct dlm_lksb32 {
0066 __u32 sb_status;
0067 __u32 sb_lkid;
0068 __u8 sb_flags;
0069 __u32 sb_lvbptr;
0070 };
0071
0072 struct dlm_lock_result32 {
0073 __u32 version[3];
0074 __u32 length;
0075 __u32 user_astaddr;
0076 __u32 user_astparam;
0077 __u32 user_lksb;
0078 struct dlm_lksb32 lksb;
0079 __u8 bast_mode;
0080 __u8 unused[3];
0081
0082 __u32 lvb_offset;
0083 };
0084
0085 static void compat_input(struct dlm_write_request *kb,
0086 struct dlm_write_request32 *kb32,
0087 int namelen)
0088 {
0089 kb->version[0] = kb32->version[0];
0090 kb->version[1] = kb32->version[1];
0091 kb->version[2] = kb32->version[2];
0092
0093 kb->cmd = kb32->cmd;
0094 kb->is64bit = kb32->is64bit;
0095 if (kb->cmd == DLM_USER_CREATE_LOCKSPACE ||
0096 kb->cmd == DLM_USER_REMOVE_LOCKSPACE) {
0097 kb->i.lspace.flags = kb32->i.lspace.flags;
0098 kb->i.lspace.minor = kb32->i.lspace.minor;
0099 memcpy(kb->i.lspace.name, kb32->i.lspace.name, namelen);
0100 } else if (kb->cmd == DLM_USER_PURGE) {
0101 kb->i.purge.nodeid = kb32->i.purge.nodeid;
0102 kb->i.purge.pid = kb32->i.purge.pid;
0103 } else {
0104 kb->i.lock.mode = kb32->i.lock.mode;
0105 kb->i.lock.namelen = kb32->i.lock.namelen;
0106 kb->i.lock.flags = kb32->i.lock.flags;
0107 kb->i.lock.lkid = kb32->i.lock.lkid;
0108 kb->i.lock.parent = kb32->i.lock.parent;
0109 kb->i.lock.xid = kb32->i.lock.xid;
0110 kb->i.lock.timeout = kb32->i.lock.timeout;
0111 kb->i.lock.castparam = (__user void *)(long)kb32->i.lock.castparam;
0112 kb->i.lock.castaddr = (__user void *)(long)kb32->i.lock.castaddr;
0113 kb->i.lock.bastparam = (__user void *)(long)kb32->i.lock.bastparam;
0114 kb->i.lock.bastaddr = (__user void *)(long)kb32->i.lock.bastaddr;
0115 kb->i.lock.lksb = (__user void *)(long)kb32->i.lock.lksb;
0116 memcpy(kb->i.lock.lvb, kb32->i.lock.lvb, DLM_USER_LVB_LEN);
0117 memcpy(kb->i.lock.name, kb32->i.lock.name, namelen);
0118 }
0119 }
0120
0121 static void compat_output(struct dlm_lock_result *res,
0122 struct dlm_lock_result32 *res32)
0123 {
0124 memset(res32, 0, sizeof(*res32));
0125
0126 res32->version[0] = res->version[0];
0127 res32->version[1] = res->version[1];
0128 res32->version[2] = res->version[2];
0129
0130 res32->user_astaddr = (__u32)(__force long)res->user_astaddr;
0131 res32->user_astparam = (__u32)(__force long)res->user_astparam;
0132 res32->user_lksb = (__u32)(__force long)res->user_lksb;
0133 res32->bast_mode = res->bast_mode;
0134
0135 res32->lvb_offset = res->lvb_offset;
0136 res32->length = res->length;
0137
0138 res32->lksb.sb_status = res->lksb.sb_status;
0139 res32->lksb.sb_flags = res->lksb.sb_flags;
0140 res32->lksb.sb_lkid = res->lksb.sb_lkid;
0141 res32->lksb.sb_lvbptr = (__u32)(long)res->lksb.sb_lvbptr;
0142 }
0143 #endif
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156 static int lkb_is_endoflife(int mode, int status)
0157 {
0158 switch (status) {
0159 case -DLM_EUNLOCK:
0160 return 1;
0161 case -DLM_ECANCEL:
0162 case -ETIMEDOUT:
0163 case -EDEADLK:
0164 case -EAGAIN:
0165 if (mode == DLM_LOCK_IV)
0166 return 1;
0167 break;
0168 }
0169 return 0;
0170 }
0171
0172
0173
0174
0175 void dlm_user_add_ast(struct dlm_lkb *lkb, uint32_t flags, int mode,
0176 int status, uint32_t sbflags, uint64_t seq)
0177 {
0178 struct dlm_ls *ls;
0179 struct dlm_user_args *ua;
0180 struct dlm_user_proc *proc;
0181 int rv;
0182
0183 if (lkb->lkb_flags & (DLM_IFL_ORPHAN | DLM_IFL_DEAD))
0184 return;
0185
0186 ls = lkb->lkb_resource->res_ls;
0187 mutex_lock(&ls->ls_clear_proc_locks);
0188
0189
0190
0191
0192
0193
0194
0195 if (lkb->lkb_flags & (DLM_IFL_ORPHAN | DLM_IFL_DEAD))
0196 goto out;
0197
0198 DLM_ASSERT(lkb->lkb_ua, dlm_print_lkb(lkb););
0199 ua = lkb->lkb_ua;
0200 proc = ua->proc;
0201
0202 if ((flags & DLM_CB_BAST) && ua->bastaddr == NULL)
0203 goto out;
0204
0205 if ((flags & DLM_CB_CAST) && lkb_is_endoflife(mode, status))
0206 lkb->lkb_flags |= DLM_IFL_ENDOFLIFE;
0207
0208 spin_lock(&proc->asts_spin);
0209
0210 rv = dlm_add_lkb_callback(lkb, flags, mode, status, sbflags, seq);
0211 if (rv < 0) {
0212 spin_unlock(&proc->asts_spin);
0213 goto out;
0214 }
0215
0216 if (list_empty(&lkb->lkb_cb_list)) {
0217 kref_get(&lkb->lkb_ref);
0218 list_add_tail(&lkb->lkb_cb_list, &proc->asts);
0219 wake_up_interruptible(&proc->wait);
0220 }
0221 spin_unlock(&proc->asts_spin);
0222
0223 if (lkb->lkb_flags & DLM_IFL_ENDOFLIFE) {
0224
0225 spin_lock(&proc->locks_spin);
0226 if (!list_empty(&lkb->lkb_ownqueue)) {
0227 list_del_init(&lkb->lkb_ownqueue);
0228 dlm_put_lkb(lkb);
0229 }
0230 spin_unlock(&proc->locks_spin);
0231 }
0232 out:
0233 mutex_unlock(&ls->ls_clear_proc_locks);
0234 }
0235
0236 static int device_user_lock(struct dlm_user_proc *proc,
0237 struct dlm_lock_params *params)
0238 {
0239 struct dlm_ls *ls;
0240 struct dlm_user_args *ua;
0241 uint32_t lkid;
0242 int error = -ENOMEM;
0243
0244 ls = dlm_find_lockspace_local(proc->lockspace);
0245 if (!ls)
0246 return -ENOENT;
0247
0248 if (!params->castaddr || !params->lksb) {
0249 error = -EINVAL;
0250 goto out;
0251 }
0252
0253 #ifdef CONFIG_DLM_DEPRECATED_API
0254 if (params->timeout)
0255 pr_warn_once("========================================================\n"
0256 "WARNING: the lkb timeout feature is being deprecated and\n"
0257 " will be removed in v6.2!\n"
0258 "========================================================\n");
0259 #endif
0260
0261 ua = kzalloc(sizeof(struct dlm_user_args), GFP_NOFS);
0262 if (!ua)
0263 goto out;
0264 ua->proc = proc;
0265 ua->user_lksb = params->lksb;
0266 ua->castparam = params->castparam;
0267 ua->castaddr = params->castaddr;
0268 ua->bastparam = params->bastparam;
0269 ua->bastaddr = params->bastaddr;
0270 ua->xid = params->xid;
0271
0272 if (params->flags & DLM_LKF_CONVERT) {
0273 #ifdef CONFIG_DLM_DEPRECATED_API
0274 error = dlm_user_convert(ls, ua,
0275 params->mode, params->flags,
0276 params->lkid, params->lvb,
0277 (unsigned long) params->timeout);
0278 #else
0279 error = dlm_user_convert(ls, ua,
0280 params->mode, params->flags,
0281 params->lkid, params->lvb);
0282 #endif
0283 } else if (params->flags & DLM_LKF_ORPHAN) {
0284 error = dlm_user_adopt_orphan(ls, ua,
0285 params->mode, params->flags,
0286 params->name, params->namelen,
0287 &lkid);
0288 if (!error)
0289 error = lkid;
0290 } else {
0291 #ifdef CONFIG_DLM_DEPRECATED_API
0292 error = dlm_user_request(ls, ua,
0293 params->mode, params->flags,
0294 params->name, params->namelen,
0295 (unsigned long) params->timeout);
0296 #else
0297 error = dlm_user_request(ls, ua,
0298 params->mode, params->flags,
0299 params->name, params->namelen);
0300 #endif
0301 if (!error)
0302 error = ua->lksb.sb_lkid;
0303 }
0304 out:
0305 dlm_put_lockspace(ls);
0306 return error;
0307 }
0308
0309 static int device_user_unlock(struct dlm_user_proc *proc,
0310 struct dlm_lock_params *params)
0311 {
0312 struct dlm_ls *ls;
0313 struct dlm_user_args *ua;
0314 int error = -ENOMEM;
0315
0316 ls = dlm_find_lockspace_local(proc->lockspace);
0317 if (!ls)
0318 return -ENOENT;
0319
0320 ua = kzalloc(sizeof(struct dlm_user_args), GFP_NOFS);
0321 if (!ua)
0322 goto out;
0323 ua->proc = proc;
0324 ua->user_lksb = params->lksb;
0325 ua->castparam = params->castparam;
0326 ua->castaddr = params->castaddr;
0327
0328 if (params->flags & DLM_LKF_CANCEL)
0329 error = dlm_user_cancel(ls, ua, params->flags, params->lkid);
0330 else
0331 error = dlm_user_unlock(ls, ua, params->flags, params->lkid,
0332 params->lvb);
0333 out:
0334 dlm_put_lockspace(ls);
0335 return error;
0336 }
0337
0338 static int device_user_deadlock(struct dlm_user_proc *proc,
0339 struct dlm_lock_params *params)
0340 {
0341 struct dlm_ls *ls;
0342 int error;
0343
0344 ls = dlm_find_lockspace_local(proc->lockspace);
0345 if (!ls)
0346 return -ENOENT;
0347
0348 error = dlm_user_deadlock(ls, params->flags, params->lkid);
0349
0350 dlm_put_lockspace(ls);
0351 return error;
0352 }
0353
0354 static int dlm_device_register(struct dlm_ls *ls, char *name)
0355 {
0356 int error, len;
0357
0358
0359
0360 if (ls->ls_device.name)
0361 return 0;
0362
0363 error = -ENOMEM;
0364 len = strlen(name) + strlen(name_prefix) + 2;
0365 ls->ls_device.name = kzalloc(len, GFP_NOFS);
0366 if (!ls->ls_device.name)
0367 goto fail;
0368
0369 snprintf((char *)ls->ls_device.name, len, "%s_%s", name_prefix,
0370 name);
0371 ls->ls_device.fops = &device_fops;
0372 ls->ls_device.minor = MISC_DYNAMIC_MINOR;
0373
0374 error = misc_register(&ls->ls_device);
0375 if (error) {
0376 kfree(ls->ls_device.name);
0377
0378
0379
0380 ls->ls_device.name = NULL;
0381 }
0382 fail:
0383 return error;
0384 }
0385
0386 int dlm_device_deregister(struct dlm_ls *ls)
0387 {
0388
0389
0390
0391 if (!ls->ls_device.name)
0392 return 0;
0393
0394 misc_deregister(&ls->ls_device);
0395 kfree(ls->ls_device.name);
0396 return 0;
0397 }
0398
0399 static int device_user_purge(struct dlm_user_proc *proc,
0400 struct dlm_purge_params *params)
0401 {
0402 struct dlm_ls *ls;
0403 int error;
0404
0405 ls = dlm_find_lockspace_local(proc->lockspace);
0406 if (!ls)
0407 return -ENOENT;
0408
0409 error = dlm_user_purge(ls, proc, params->nodeid, params->pid);
0410
0411 dlm_put_lockspace(ls);
0412 return error;
0413 }
0414
0415 static int device_create_lockspace(struct dlm_lspace_params *params)
0416 {
0417 dlm_lockspace_t *lockspace;
0418 struct dlm_ls *ls;
0419 int error;
0420
0421 if (!capable(CAP_SYS_ADMIN))
0422 return -EPERM;
0423
0424 error = dlm_new_lockspace(params->name, dlm_config.ci_cluster_name, params->flags,
0425 DLM_USER_LVB_LEN, NULL, NULL, NULL,
0426 &lockspace);
0427 if (error)
0428 return error;
0429
0430 ls = dlm_find_lockspace_local(lockspace);
0431 if (!ls)
0432 return -ENOENT;
0433
0434 error = dlm_device_register(ls, params->name);
0435 dlm_put_lockspace(ls);
0436
0437 if (error)
0438 dlm_release_lockspace(lockspace, 0);
0439 else
0440 error = ls->ls_device.minor;
0441
0442 return error;
0443 }
0444
0445 static int device_remove_lockspace(struct dlm_lspace_params *params)
0446 {
0447 dlm_lockspace_t *lockspace;
0448 struct dlm_ls *ls;
0449 int error, force = 0;
0450
0451 if (!capable(CAP_SYS_ADMIN))
0452 return -EPERM;
0453
0454 ls = dlm_find_lockspace_device(params->minor);
0455 if (!ls)
0456 return -ENOENT;
0457
0458 if (params->flags & DLM_USER_LSFLG_FORCEFREE)
0459 force = 2;
0460
0461 lockspace = ls->ls_local_handle;
0462 dlm_put_lockspace(ls);
0463
0464
0465
0466
0467
0468
0469
0470
0471 error = dlm_release_lockspace(lockspace, force);
0472 if (error > 0)
0473 error = 0;
0474 return error;
0475 }
0476
0477
0478 static int check_version(struct dlm_write_request *req)
0479 {
0480 if (req->version[0] != DLM_DEVICE_VERSION_MAJOR ||
0481 (req->version[0] == DLM_DEVICE_VERSION_MAJOR &&
0482 req->version[1] > DLM_DEVICE_VERSION_MINOR)) {
0483
0484 printk(KERN_DEBUG "dlm: process %s (%d) version mismatch "
0485 "user (%d.%d.%d) kernel (%d.%d.%d)\n",
0486 current->comm,
0487 task_pid_nr(current),
0488 req->version[0],
0489 req->version[1],
0490 req->version[2],
0491 DLM_DEVICE_VERSION_MAJOR,
0492 DLM_DEVICE_VERSION_MINOR,
0493 DLM_DEVICE_VERSION_PATCH);
0494 return -EINVAL;
0495 }
0496 return 0;
0497 }
0498
0499
0500
0501
0502
0503
0504
0505
0506
0507
0508
0509
0510
0511
0512
0513
0514
0515
0516
0517
0518
0519
0520 static ssize_t device_write(struct file *file, const char __user *buf,
0521 size_t count, loff_t *ppos)
0522 {
0523 struct dlm_user_proc *proc = file->private_data;
0524 struct dlm_write_request *kbuf;
0525 int error;
0526
0527 #ifdef CONFIG_COMPAT
0528 if (count < sizeof(struct dlm_write_request32))
0529 #else
0530 if (count < sizeof(struct dlm_write_request))
0531 #endif
0532 return -EINVAL;
0533
0534
0535
0536
0537
0538 if (count > sizeof(struct dlm_write_request) + DLM_RESNAME_MAXLEN)
0539 return -EINVAL;
0540
0541 kbuf = memdup_user_nul(buf, count);
0542 if (IS_ERR(kbuf))
0543 return PTR_ERR(kbuf);
0544
0545 if (check_version(kbuf)) {
0546 error = -EBADE;
0547 goto out_free;
0548 }
0549
0550 #ifdef CONFIG_COMPAT
0551 if (!kbuf->is64bit) {
0552 struct dlm_write_request32 *k32buf;
0553 int namelen = 0;
0554
0555 if (count > sizeof(struct dlm_write_request32))
0556 namelen = count - sizeof(struct dlm_write_request32);
0557
0558 k32buf = (struct dlm_write_request32 *)kbuf;
0559
0560
0561 kbuf = kzalloc(sizeof(struct dlm_write_request) + namelen + 1,
0562 GFP_NOFS);
0563 if (!kbuf) {
0564 kfree(k32buf);
0565 return -ENOMEM;
0566 }
0567
0568 if (proc)
0569 set_bit(DLM_PROC_FLAGS_COMPAT, &proc->flags);
0570
0571 compat_input(kbuf, k32buf, namelen);
0572 kfree(k32buf);
0573 }
0574 #endif
0575
0576
0577 if ((kbuf->cmd == DLM_USER_LOCK || kbuf->cmd == DLM_USER_UNLOCK) &&
0578 (proc && test_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags))) {
0579 error = -EINVAL;
0580 goto out_free;
0581 }
0582
0583 error = -EINVAL;
0584
0585 switch (kbuf->cmd)
0586 {
0587 case DLM_USER_LOCK:
0588 if (!proc) {
0589 log_print("no locking on control device");
0590 goto out_free;
0591 }
0592 error = device_user_lock(proc, &kbuf->i.lock);
0593 break;
0594
0595 case DLM_USER_UNLOCK:
0596 if (!proc) {
0597 log_print("no locking on control device");
0598 goto out_free;
0599 }
0600 error = device_user_unlock(proc, &kbuf->i.lock);
0601 break;
0602
0603 case DLM_USER_DEADLOCK:
0604 if (!proc) {
0605 log_print("no locking on control device");
0606 goto out_free;
0607 }
0608 error = device_user_deadlock(proc, &kbuf->i.lock);
0609 break;
0610
0611 case DLM_USER_CREATE_LOCKSPACE:
0612 if (proc) {
0613 log_print("create/remove only on control device");
0614 goto out_free;
0615 }
0616 error = device_create_lockspace(&kbuf->i.lspace);
0617 break;
0618
0619 case DLM_USER_REMOVE_LOCKSPACE:
0620 if (proc) {
0621 log_print("create/remove only on control device");
0622 goto out_free;
0623 }
0624 error = device_remove_lockspace(&kbuf->i.lspace);
0625 break;
0626
0627 case DLM_USER_PURGE:
0628 if (!proc) {
0629 log_print("no locking on control device");
0630 goto out_free;
0631 }
0632 error = device_user_purge(proc, &kbuf->i.purge);
0633 break;
0634
0635 default:
0636 log_print("Unknown command passed to DLM device : %d\n",
0637 kbuf->cmd);
0638 }
0639
0640 out_free:
0641 kfree(kbuf);
0642 return error;
0643 }
0644
0645
0646
0647
0648
0649 static int device_open(struct inode *inode, struct file *file)
0650 {
0651 struct dlm_user_proc *proc;
0652 struct dlm_ls *ls;
0653
0654 ls = dlm_find_lockspace_device(iminor(inode));
0655 if (!ls)
0656 return -ENOENT;
0657
0658 proc = kzalloc(sizeof(struct dlm_user_proc), GFP_NOFS);
0659 if (!proc) {
0660 dlm_put_lockspace(ls);
0661 return -ENOMEM;
0662 }
0663
0664 proc->lockspace = ls->ls_local_handle;
0665 INIT_LIST_HEAD(&proc->asts);
0666 INIT_LIST_HEAD(&proc->locks);
0667 INIT_LIST_HEAD(&proc->unlocking);
0668 spin_lock_init(&proc->asts_spin);
0669 spin_lock_init(&proc->locks_spin);
0670 init_waitqueue_head(&proc->wait);
0671 file->private_data = proc;
0672
0673 return 0;
0674 }
0675
0676 static int device_close(struct inode *inode, struct file *file)
0677 {
0678 struct dlm_user_proc *proc = file->private_data;
0679 struct dlm_ls *ls;
0680
0681 ls = dlm_find_lockspace_local(proc->lockspace);
0682 if (!ls)
0683 return -ENOENT;
0684
0685 set_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags);
0686
0687 dlm_clear_proc_locks(ls, proc);
0688
0689
0690
0691
0692
0693 kfree(proc);
0694 file->private_data = NULL;
0695
0696 dlm_put_lockspace(ls);
0697 dlm_put_lockspace(ls);
0698
0699
0700
0701
0702 return 0;
0703 }
0704
0705 static int copy_result_to_user(struct dlm_user_args *ua, int compat,
0706 uint32_t flags, int mode, int copy_lvb,
0707 char __user *buf, size_t count)
0708 {
0709 #ifdef CONFIG_COMPAT
0710 struct dlm_lock_result32 result32;
0711 #endif
0712 struct dlm_lock_result result;
0713 void *resultptr;
0714 int error=0;
0715 int len;
0716 int struct_len;
0717
0718 memset(&result, 0, sizeof(struct dlm_lock_result));
0719 result.version[0] = DLM_DEVICE_VERSION_MAJOR;
0720 result.version[1] = DLM_DEVICE_VERSION_MINOR;
0721 result.version[2] = DLM_DEVICE_VERSION_PATCH;
0722 memcpy(&result.lksb, &ua->lksb, offsetof(struct dlm_lksb, sb_lvbptr));
0723 result.user_lksb = ua->user_lksb;
0724
0725
0726
0727
0728
0729
0730
0731 if (flags & DLM_CB_BAST) {
0732 result.user_astaddr = ua->bastaddr;
0733 result.user_astparam = ua->bastparam;
0734 result.bast_mode = mode;
0735 } else {
0736 result.user_astaddr = ua->castaddr;
0737 result.user_astparam = ua->castparam;
0738 }
0739
0740 #ifdef CONFIG_COMPAT
0741 if (compat)
0742 len = sizeof(struct dlm_lock_result32);
0743 else
0744 #endif
0745 len = sizeof(struct dlm_lock_result);
0746 struct_len = len;
0747
0748
0749
0750
0751 if (copy_lvb && ua->lksb.sb_lvbptr && count >= len + DLM_USER_LVB_LEN) {
0752 if (copy_to_user(buf+len, ua->lksb.sb_lvbptr,
0753 DLM_USER_LVB_LEN)) {
0754 error = -EFAULT;
0755 goto out;
0756 }
0757
0758 result.lvb_offset = len;
0759 len += DLM_USER_LVB_LEN;
0760 }
0761
0762 result.length = len;
0763 resultptr = &result;
0764 #ifdef CONFIG_COMPAT
0765 if (compat) {
0766 compat_output(&result, &result32);
0767 resultptr = &result32;
0768 }
0769 #endif
0770
0771 if (copy_to_user(buf, resultptr, struct_len))
0772 error = -EFAULT;
0773 else
0774 error = len;
0775 out:
0776 return error;
0777 }
0778
0779 static int copy_version_to_user(char __user *buf, size_t count)
0780 {
0781 struct dlm_device_version ver;
0782
0783 memset(&ver, 0, sizeof(struct dlm_device_version));
0784 ver.version[0] = DLM_DEVICE_VERSION_MAJOR;
0785 ver.version[1] = DLM_DEVICE_VERSION_MINOR;
0786 ver.version[2] = DLM_DEVICE_VERSION_PATCH;
0787
0788 if (copy_to_user(buf, &ver, sizeof(struct dlm_device_version)))
0789 return -EFAULT;
0790 return sizeof(struct dlm_device_version);
0791 }
0792
0793
0794
0795 static ssize_t device_read(struct file *file, char __user *buf, size_t count,
0796 loff_t *ppos)
0797 {
0798 struct dlm_user_proc *proc = file->private_data;
0799 struct dlm_lkb *lkb;
0800 DECLARE_WAITQUEUE(wait, current);
0801 struct dlm_callback cb;
0802 int rv, resid, copy_lvb = 0;
0803 int old_mode, new_mode;
0804
0805 if (count == sizeof(struct dlm_device_version)) {
0806 rv = copy_version_to_user(buf, count);
0807 return rv;
0808 }
0809
0810 if (!proc) {
0811 log_print("non-version read from control device %zu", count);
0812 return -EINVAL;
0813 }
0814
0815 #ifdef CONFIG_COMPAT
0816 if (count < sizeof(struct dlm_lock_result32))
0817 #else
0818 if (count < sizeof(struct dlm_lock_result))
0819 #endif
0820 return -EINVAL;
0821
0822 try_another:
0823
0824
0825 if (test_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags))
0826 return -EINVAL;
0827
0828 spin_lock(&proc->asts_spin);
0829 if (list_empty(&proc->asts)) {
0830 if (file->f_flags & O_NONBLOCK) {
0831 spin_unlock(&proc->asts_spin);
0832 return -EAGAIN;
0833 }
0834
0835 add_wait_queue(&proc->wait, &wait);
0836
0837 repeat:
0838 set_current_state(TASK_INTERRUPTIBLE);
0839 if (list_empty(&proc->asts) && !signal_pending(current)) {
0840 spin_unlock(&proc->asts_spin);
0841 schedule();
0842 spin_lock(&proc->asts_spin);
0843 goto repeat;
0844 }
0845 set_current_state(TASK_RUNNING);
0846 remove_wait_queue(&proc->wait, &wait);
0847
0848 if (signal_pending(current)) {
0849 spin_unlock(&proc->asts_spin);
0850 return -ERESTARTSYS;
0851 }
0852 }
0853
0854
0855
0856
0857
0858 lkb = list_entry(proc->asts.next, struct dlm_lkb, lkb_cb_list);
0859
0860
0861 old_mode = lkb->lkb_last_cast.mode;
0862
0863 rv = dlm_rem_lkb_callback(lkb->lkb_resource->res_ls, lkb, &cb, &resid);
0864 if (rv < 0) {
0865
0866
0867 log_print("dlm_rem_lkb_callback empty %x", lkb->lkb_id);
0868 list_del_init(&lkb->lkb_cb_list);
0869 spin_unlock(&proc->asts_spin);
0870
0871 dlm_put_lkb(lkb);
0872 goto try_another;
0873 }
0874 if (!resid)
0875 list_del_init(&lkb->lkb_cb_list);
0876 spin_unlock(&proc->asts_spin);
0877
0878 if (cb.flags & DLM_CB_SKIP) {
0879
0880 if (!resid)
0881 dlm_put_lkb(lkb);
0882 goto try_another;
0883 }
0884
0885 if (cb.flags & DLM_CB_CAST) {
0886 new_mode = cb.mode;
0887
0888 if (!cb.sb_status && lkb->lkb_lksb->sb_lvbptr &&
0889 dlm_lvb_operations[old_mode + 1][new_mode + 1])
0890 copy_lvb = 1;
0891
0892 lkb->lkb_lksb->sb_status = cb.sb_status;
0893 lkb->lkb_lksb->sb_flags = cb.sb_flags;
0894 }
0895
0896 rv = copy_result_to_user(lkb->lkb_ua,
0897 test_bit(DLM_PROC_FLAGS_COMPAT, &proc->flags),
0898 cb.flags, cb.mode, copy_lvb, buf, count);
0899
0900
0901 if (!resid)
0902 dlm_put_lkb(lkb);
0903
0904 return rv;
0905 }
0906
0907 static __poll_t device_poll(struct file *file, poll_table *wait)
0908 {
0909 struct dlm_user_proc *proc = file->private_data;
0910
0911 poll_wait(file, &proc->wait, wait);
0912
0913 spin_lock(&proc->asts_spin);
0914 if (!list_empty(&proc->asts)) {
0915 spin_unlock(&proc->asts_spin);
0916 return EPOLLIN | EPOLLRDNORM;
0917 }
0918 spin_unlock(&proc->asts_spin);
0919 return 0;
0920 }
0921
0922 int dlm_user_daemon_available(void)
0923 {
0924
0925
0926
0927 if (!dlm_our_nodeid())
0928 return 0;
0929
0930
0931
0932
0933
0934
0935
0936 if (dlm_monitor_unused)
0937 return 1;
0938
0939 return atomic_read(&dlm_monitor_opened) ? 1 : 0;
0940 }
0941
0942 static int ctl_device_open(struct inode *inode, struct file *file)
0943 {
0944 file->private_data = NULL;
0945 return 0;
0946 }
0947
0948 static int ctl_device_close(struct inode *inode, struct file *file)
0949 {
0950 return 0;
0951 }
0952
0953 static int monitor_device_open(struct inode *inode, struct file *file)
0954 {
0955 atomic_inc(&dlm_monitor_opened);
0956 dlm_monitor_unused = 0;
0957 return 0;
0958 }
0959
0960 static int monitor_device_close(struct inode *inode, struct file *file)
0961 {
0962 if (atomic_dec_and_test(&dlm_monitor_opened))
0963 dlm_stop_lockspaces();
0964 return 0;
0965 }
0966
0967 static const struct file_operations device_fops = {
0968 .open = device_open,
0969 .release = device_close,
0970 .read = device_read,
0971 .write = device_write,
0972 .poll = device_poll,
0973 .owner = THIS_MODULE,
0974 .llseek = noop_llseek,
0975 };
0976
0977 static const struct file_operations ctl_device_fops = {
0978 .open = ctl_device_open,
0979 .release = ctl_device_close,
0980 .read = device_read,
0981 .write = device_write,
0982 .owner = THIS_MODULE,
0983 .llseek = noop_llseek,
0984 };
0985
0986 static struct miscdevice ctl_device = {
0987 .name = "dlm-control",
0988 .fops = &ctl_device_fops,
0989 .minor = MISC_DYNAMIC_MINOR,
0990 };
0991
0992 static const struct file_operations monitor_device_fops = {
0993 .open = monitor_device_open,
0994 .release = monitor_device_close,
0995 .owner = THIS_MODULE,
0996 .llseek = noop_llseek,
0997 };
0998
0999 static struct miscdevice monitor_device = {
1000 .name = "dlm-monitor",
1001 .fops = &monitor_device_fops,
1002 .minor = MISC_DYNAMIC_MINOR,
1003 };
1004
1005 int __init dlm_user_init(void)
1006 {
1007 int error;
1008
1009 atomic_set(&dlm_monitor_opened, 0);
1010
1011 error = misc_register(&ctl_device);
1012 if (error) {
1013 log_print("misc_register failed for control device");
1014 goto out;
1015 }
1016
1017 error = misc_register(&monitor_device);
1018 if (error) {
1019 log_print("misc_register failed for monitor device");
1020 misc_deregister(&ctl_device);
1021 }
1022 out:
1023 return error;
1024 }
1025
1026 void dlm_user_exit(void)
1027 {
1028 misc_deregister(&ctl_device);
1029 misc_deregister(&monitor_device);
1030 }
1031