user.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. /*
  2. * Copyright (C) 2006-2007 Red Hat, Inc. All rights reserved.
  3. *
  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.
  7. */
  8. #include <linux/miscdevice.h>
  9. #include <linux/init.h>
  10. #include <linux/wait.h>
  11. #include <linux/module.h>
  12. #include <linux/file.h>
  13. #include <linux/fs.h>
  14. #include <linux/poll.h>
  15. #include <linux/signal.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/dlm.h>
  18. #include <linux/dlm_device.h>
  19. #include "dlm_internal.h"
  20. #include "lockspace.h"
  21. #include "lock.h"
  22. #include "lvb_table.h"
  23. #include "user.h"
  24. static const char *name_prefix="dlm";
  25. static struct miscdevice ctl_device;
  26. static const struct file_operations device_fops;
  27. #ifdef CONFIG_COMPAT
  28. struct dlm_lock_params32 {
  29. __u8 mode;
  30. __u8 namelen;
  31. __u16 flags;
  32. __u32 lkid;
  33. __u32 parent;
  34. __u32 castparam;
  35. __u32 castaddr;
  36. __u32 bastparam;
  37. __u32 bastaddr;
  38. __u32 lksb;
  39. char lvb[DLM_USER_LVB_LEN];
  40. char name[0];
  41. };
  42. struct dlm_write_request32 {
  43. __u32 version[3];
  44. __u8 cmd;
  45. __u8 is64bit;
  46. __u8 unused[2];
  47. union {
  48. struct dlm_lock_params32 lock;
  49. struct dlm_lspace_params lspace;
  50. struct dlm_purge_params purge;
  51. } i;
  52. };
  53. struct dlm_lksb32 {
  54. __u32 sb_status;
  55. __u32 sb_lkid;
  56. __u8 sb_flags;
  57. __u32 sb_lvbptr;
  58. };
  59. struct dlm_lock_result32 {
  60. __u32 length;
  61. __u32 user_astaddr;
  62. __u32 user_astparam;
  63. __u32 user_lksb;
  64. struct dlm_lksb32 lksb;
  65. __u8 bast_mode;
  66. __u8 unused[3];
  67. /* Offsets may be zero if no data is present */
  68. __u32 lvb_offset;
  69. };
  70. static void compat_input(struct dlm_write_request *kb,
  71. struct dlm_write_request32 *kb32)
  72. {
  73. kb->version[0] = kb32->version[0];
  74. kb->version[1] = kb32->version[1];
  75. kb->version[2] = kb32->version[2];
  76. kb->cmd = kb32->cmd;
  77. kb->is64bit = kb32->is64bit;
  78. if (kb->cmd == DLM_USER_CREATE_LOCKSPACE ||
  79. kb->cmd == DLM_USER_REMOVE_LOCKSPACE) {
  80. kb->i.lspace.flags = kb32->i.lspace.flags;
  81. kb->i.lspace.minor = kb32->i.lspace.minor;
  82. strcpy(kb->i.lspace.name, kb32->i.lspace.name);
  83. } else if (kb->cmd == DLM_USER_PURGE) {
  84. kb->i.purge.nodeid = kb32->i.purge.nodeid;
  85. kb->i.purge.pid = kb32->i.purge.pid;
  86. } else {
  87. kb->i.lock.mode = kb32->i.lock.mode;
  88. kb->i.lock.namelen = kb32->i.lock.namelen;
  89. kb->i.lock.flags = kb32->i.lock.flags;
  90. kb->i.lock.lkid = kb32->i.lock.lkid;
  91. kb->i.lock.parent = kb32->i.lock.parent;
  92. kb->i.lock.castparam = (void *)(long)kb32->i.lock.castparam;
  93. kb->i.lock.castaddr = (void *)(long)kb32->i.lock.castaddr;
  94. kb->i.lock.bastparam = (void *)(long)kb32->i.lock.bastparam;
  95. kb->i.lock.bastaddr = (void *)(long)kb32->i.lock.bastaddr;
  96. kb->i.lock.lksb = (void *)(long)kb32->i.lock.lksb;
  97. memcpy(kb->i.lock.lvb, kb32->i.lock.lvb, DLM_USER_LVB_LEN);
  98. memcpy(kb->i.lock.name, kb32->i.lock.name, kb->i.lock.namelen);
  99. }
  100. }
  101. static void compat_output(struct dlm_lock_result *res,
  102. struct dlm_lock_result32 *res32)
  103. {
  104. res32->user_astaddr = (__u32)(long)res->user_astaddr;
  105. res32->user_astparam = (__u32)(long)res->user_astparam;
  106. res32->user_lksb = (__u32)(long)res->user_lksb;
  107. res32->bast_mode = res->bast_mode;
  108. res32->lvb_offset = res->lvb_offset;
  109. res32->length = res->length;
  110. res32->lksb.sb_status = res->lksb.sb_status;
  111. res32->lksb.sb_flags = res->lksb.sb_flags;
  112. res32->lksb.sb_lkid = res->lksb.sb_lkid;
  113. res32->lksb.sb_lvbptr = (__u32)(long)res->lksb.sb_lvbptr;
  114. }
  115. #endif
  116. /* we could possibly check if the cancel of an orphan has resulted in the lkb
  117. being removed and then remove that lkb from the orphans list and free it */
  118. void dlm_user_add_ast(struct dlm_lkb *lkb, int type)
  119. {
  120. struct dlm_ls *ls;
  121. struct dlm_user_args *ua;
  122. struct dlm_user_proc *proc;
  123. int eol = 0, ast_type;
  124. if (lkb->lkb_flags & (DLM_IFL_ORPHAN | DLM_IFL_DEAD))
  125. return;
  126. ls = lkb->lkb_resource->res_ls;
  127. mutex_lock(&ls->ls_clear_proc_locks);
  128. /* If ORPHAN/DEAD flag is set, it means the process is dead so an ast
  129. can't be delivered. For ORPHAN's, dlm_clear_proc_locks() freed
  130. lkb->ua so we can't try to use it. This second check is necessary
  131. for cases where a completion ast is received for an operation that
  132. began before clear_proc_locks did its cancel/unlock. */
  133. if (lkb->lkb_flags & (DLM_IFL_ORPHAN | DLM_IFL_DEAD))
  134. goto out;
  135. DLM_ASSERT(lkb->lkb_astparam, dlm_print_lkb(lkb););
  136. ua = (struct dlm_user_args *)lkb->lkb_astparam;
  137. proc = ua->proc;
  138. if (type == AST_BAST && ua->bastaddr == NULL)
  139. goto out;
  140. spin_lock(&proc->asts_spin);
  141. ast_type = lkb->lkb_ast_type;
  142. lkb->lkb_ast_type |= type;
  143. if (!ast_type) {
  144. kref_get(&lkb->lkb_ref);
  145. list_add_tail(&lkb->lkb_astqueue, &proc->asts);
  146. wake_up_interruptible(&proc->wait);
  147. }
  148. if (type == AST_COMP && (ast_type & AST_COMP))
  149. log_debug(ls, "ast overlap %x status %x %x",
  150. lkb->lkb_id, ua->lksb.sb_status, lkb->lkb_flags);
  151. /* Figure out if this lock is at the end of its life and no longer
  152. available for the application to use. The lkb still exists until
  153. the final ast is read. A lock becomes EOL in three situations:
  154. 1. a noqueue request fails with EAGAIN
  155. 2. an unlock completes with EUNLOCK
  156. 3. a cancel of a waiting request completes with ECANCEL
  157. An EOL lock needs to be removed from the process's list of locks.
  158. And we can't allow any new operation on an EOL lock. This is
  159. not related to the lifetime of the lkb struct which is managed
  160. entirely by refcount. */
  161. if (type == AST_COMP &&
  162. lkb->lkb_grmode == DLM_LOCK_IV &&
  163. ua->lksb.sb_status == -EAGAIN)
  164. eol = 1;
  165. else if (ua->lksb.sb_status == -DLM_EUNLOCK ||
  166. (ua->lksb.sb_status == -DLM_ECANCEL &&
  167. lkb->lkb_grmode == DLM_LOCK_IV))
  168. eol = 1;
  169. if (eol) {
  170. lkb->lkb_ast_type &= ~AST_BAST;
  171. lkb->lkb_flags |= DLM_IFL_ENDOFLIFE;
  172. }
  173. /* We want to copy the lvb to userspace when the completion
  174. ast is read if the status is 0, the lock has an lvb and
  175. lvb_ops says we should. We could probably have set_lvb_lock()
  176. set update_user_lvb instead and not need old_mode */
  177. if ((lkb->lkb_ast_type & AST_COMP) &&
  178. (lkb->lkb_lksb->sb_status == 0) &&
  179. lkb->lkb_lksb->sb_lvbptr &&
  180. dlm_lvb_operations[ua->old_mode + 1][lkb->lkb_grmode + 1])
  181. ua->update_user_lvb = 1;
  182. else
  183. ua->update_user_lvb = 0;
  184. spin_unlock(&proc->asts_spin);
  185. if (eol) {
  186. spin_lock(&ua->proc->locks_spin);
  187. if (!list_empty(&lkb->lkb_ownqueue)) {
  188. list_del_init(&lkb->lkb_ownqueue);
  189. dlm_put_lkb(lkb);
  190. }
  191. spin_unlock(&ua->proc->locks_spin);
  192. }
  193. out:
  194. mutex_unlock(&ls->ls_clear_proc_locks);
  195. }
  196. static int device_user_lock(struct dlm_user_proc *proc,
  197. struct dlm_lock_params *params)
  198. {
  199. struct dlm_ls *ls;
  200. struct dlm_user_args *ua;
  201. int error = -ENOMEM;
  202. ls = dlm_find_lockspace_local(proc->lockspace);
  203. if (!ls)
  204. return -ENOENT;
  205. if (!params->castaddr || !params->lksb) {
  206. error = -EINVAL;
  207. goto out;
  208. }
  209. ua = kzalloc(sizeof(struct dlm_user_args), GFP_KERNEL);
  210. if (!ua)
  211. goto out;
  212. ua->proc = proc;
  213. ua->user_lksb = params->lksb;
  214. ua->castparam = params->castparam;
  215. ua->castaddr = params->castaddr;
  216. ua->bastparam = params->bastparam;
  217. 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);
  222. else {
  223. error = dlm_user_request(ls, ua,
  224. params->mode, params->flags,
  225. params->name, params->namelen,
  226. params->parent);
  227. if (!error)
  228. error = ua->lksb.sb_lkid;
  229. }
  230. out:
  231. dlm_put_lockspace(ls);
  232. return error;
  233. }
  234. static int device_user_unlock(struct dlm_user_proc *proc,
  235. struct dlm_lock_params *params)
  236. {
  237. struct dlm_ls *ls;
  238. struct dlm_user_args *ua;
  239. int error = -ENOMEM;
  240. ls = dlm_find_lockspace_local(proc->lockspace);
  241. if (!ls)
  242. return -ENOENT;
  243. ua = kzalloc(sizeof(struct dlm_user_args), GFP_KERNEL);
  244. if (!ua)
  245. goto out;
  246. ua->proc = proc;
  247. ua->user_lksb = params->lksb;
  248. ua->castparam = params->castparam;
  249. ua->castaddr = params->castaddr;
  250. if (params->flags & DLM_LKF_CANCEL)
  251. error = dlm_user_cancel(ls, ua, params->flags, params->lkid);
  252. else
  253. error = dlm_user_unlock(ls, ua, params->flags, params->lkid,
  254. params->lvb);
  255. out:
  256. dlm_put_lockspace(ls);
  257. return error;
  258. }
  259. static int create_misc_device(struct dlm_ls *ls, char *name)
  260. {
  261. int error, len;
  262. error = -ENOMEM;
  263. len = strlen(name) + strlen(name_prefix) + 2;
  264. ls->ls_device.name = kzalloc(len, GFP_KERNEL);
  265. if (!ls->ls_device.name)
  266. goto fail;
  267. snprintf((char *)ls->ls_device.name, len, "%s_%s", name_prefix,
  268. name);
  269. ls->ls_device.fops = &device_fops;
  270. ls->ls_device.minor = MISC_DYNAMIC_MINOR;
  271. error = misc_register(&ls->ls_device);
  272. if (error) {
  273. kfree(ls->ls_device.name);
  274. }
  275. fail:
  276. return error;
  277. }
  278. static int device_user_purge(struct dlm_user_proc *proc,
  279. struct dlm_purge_params *params)
  280. {
  281. struct dlm_ls *ls;
  282. int error;
  283. ls = dlm_find_lockspace_local(proc->lockspace);
  284. if (!ls)
  285. return -ENOENT;
  286. error = dlm_user_purge(ls, proc, params->nodeid, params->pid);
  287. dlm_put_lockspace(ls);
  288. return error;
  289. }
  290. static int device_create_lockspace(struct dlm_lspace_params *params)
  291. {
  292. dlm_lockspace_t *lockspace;
  293. struct dlm_ls *ls;
  294. int error;
  295. if (!capable(CAP_SYS_ADMIN))
  296. return -EPERM;
  297. error = dlm_new_lockspace(params->name, strlen(params->name),
  298. &lockspace, 0, DLM_USER_LVB_LEN);
  299. if (error)
  300. return error;
  301. ls = dlm_find_lockspace_local(lockspace);
  302. if (!ls)
  303. return -ENOENT;
  304. error = create_misc_device(ls, params->name);
  305. dlm_put_lockspace(ls);
  306. if (error)
  307. dlm_release_lockspace(lockspace, 0);
  308. else
  309. error = ls->ls_device.minor;
  310. return error;
  311. }
  312. static int device_remove_lockspace(struct dlm_lspace_params *params)
  313. {
  314. dlm_lockspace_t *lockspace;
  315. struct dlm_ls *ls;
  316. int error, force = 0;
  317. if (!capable(CAP_SYS_ADMIN))
  318. return -EPERM;
  319. ls = dlm_find_lockspace_device(params->minor);
  320. if (!ls)
  321. return -ENOENT;
  322. /* Deregister the misc device first, so we don't have
  323. * a device that's not attached to a lockspace. If
  324. * dlm_release_lockspace fails then we can recreate it
  325. */
  326. error = misc_deregister(&ls->ls_device);
  327. if (error) {
  328. dlm_put_lockspace(ls);
  329. goto out;
  330. }
  331. kfree(ls->ls_device.name);
  332. if (params->flags & DLM_USER_LSFLG_FORCEFREE)
  333. force = 2;
  334. lockspace = ls->ls_local_handle;
  335. /* dlm_release_lockspace waits for references to go to zero,
  336. so all processes will need to close their device for the ls
  337. before the release will procede */
  338. dlm_put_lockspace(ls);
  339. error = dlm_release_lockspace(lockspace, force);
  340. if (error)
  341. create_misc_device(ls, ls->ls_name);
  342. out:
  343. return error;
  344. }
  345. /* Check the user's version matches ours */
  346. static int check_version(struct dlm_write_request *req)
  347. {
  348. if (req->version[0] != DLM_DEVICE_VERSION_MAJOR ||
  349. (req->version[0] == DLM_DEVICE_VERSION_MAJOR &&
  350. req->version[1] > DLM_DEVICE_VERSION_MINOR)) {
  351. printk(KERN_DEBUG "dlm: process %s (%d) version mismatch "
  352. "user (%d.%d.%d) kernel (%d.%d.%d)\n",
  353. current->comm,
  354. current->pid,
  355. req->version[0],
  356. req->version[1],
  357. req->version[2],
  358. DLM_DEVICE_VERSION_MAJOR,
  359. DLM_DEVICE_VERSION_MINOR,
  360. DLM_DEVICE_VERSION_PATCH);
  361. return -EINVAL;
  362. }
  363. return 0;
  364. }
  365. /*
  366. * device_write
  367. *
  368. * device_user_lock
  369. * dlm_user_request -> request_lock
  370. * dlm_user_convert -> convert_lock
  371. *
  372. * device_user_unlock
  373. * dlm_user_unlock -> unlock_lock
  374. * dlm_user_cancel -> cancel_lock
  375. *
  376. * device_create_lockspace
  377. * dlm_new_lockspace
  378. *
  379. * device_remove_lockspace
  380. * dlm_release_lockspace
  381. */
  382. /* a write to a lockspace device is a lock or unlock request, a write
  383. to the control device is to create/remove a lockspace */
  384. static ssize_t device_write(struct file *file, const char __user *buf,
  385. size_t count, loff_t *ppos)
  386. {
  387. struct dlm_user_proc *proc = file->private_data;
  388. struct dlm_write_request *kbuf;
  389. sigset_t tmpsig, allsigs;
  390. int error;
  391. #ifdef CONFIG_COMPAT
  392. if (count < sizeof(struct dlm_write_request32))
  393. #else
  394. if (count < sizeof(struct dlm_write_request))
  395. #endif
  396. return -EINVAL;
  397. kbuf = kmalloc(count, GFP_KERNEL);
  398. if (!kbuf)
  399. return -ENOMEM;
  400. if (copy_from_user(kbuf, buf, count)) {
  401. error = -EFAULT;
  402. goto out_free;
  403. }
  404. if (check_version(kbuf)) {
  405. error = -EBADE;
  406. goto out_free;
  407. }
  408. #ifdef CONFIG_COMPAT
  409. if (!kbuf->is64bit) {
  410. struct dlm_write_request32 *k32buf;
  411. k32buf = (struct dlm_write_request32 *)kbuf;
  412. kbuf = kmalloc(count + (sizeof(struct dlm_write_request) -
  413. sizeof(struct dlm_write_request32)), GFP_KERNEL);
  414. if (!kbuf)
  415. return -ENOMEM;
  416. if (proc)
  417. set_bit(DLM_PROC_FLAGS_COMPAT, &proc->flags);
  418. compat_input(kbuf, k32buf);
  419. kfree(k32buf);
  420. }
  421. #endif
  422. /* do we really need this? can a write happen after a close? */
  423. if ((kbuf->cmd == DLM_USER_LOCK || kbuf->cmd == DLM_USER_UNLOCK) &&
  424. test_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags))
  425. return -EINVAL;
  426. sigfillset(&allsigs);
  427. sigprocmask(SIG_BLOCK, &allsigs, &tmpsig);
  428. error = -EINVAL;
  429. switch (kbuf->cmd)
  430. {
  431. case DLM_USER_LOCK:
  432. if (!proc) {
  433. log_print("no locking on control device");
  434. goto out_sig;
  435. }
  436. error = device_user_lock(proc, &kbuf->i.lock);
  437. break;
  438. case DLM_USER_UNLOCK:
  439. if (!proc) {
  440. log_print("no locking on control device");
  441. goto out_sig;
  442. }
  443. error = device_user_unlock(proc, &kbuf->i.lock);
  444. break;
  445. case DLM_USER_CREATE_LOCKSPACE:
  446. if (proc) {
  447. log_print("create/remove only on control device");
  448. goto out_sig;
  449. }
  450. error = device_create_lockspace(&kbuf->i.lspace);
  451. break;
  452. case DLM_USER_REMOVE_LOCKSPACE:
  453. if (proc) {
  454. log_print("create/remove only on control device");
  455. goto out_sig;
  456. }
  457. error = device_remove_lockspace(&kbuf->i.lspace);
  458. break;
  459. case DLM_USER_PURGE:
  460. if (!proc) {
  461. log_print("no locking on control device");
  462. goto out_sig;
  463. }
  464. error = device_user_purge(proc, &kbuf->i.purge);
  465. break;
  466. default:
  467. log_print("Unknown command passed to DLM device : %d\n",
  468. kbuf->cmd);
  469. }
  470. out_sig:
  471. sigprocmask(SIG_SETMASK, &tmpsig, NULL);
  472. recalc_sigpending();
  473. out_free:
  474. kfree(kbuf);
  475. return error;
  476. }
  477. /* Every process that opens the lockspace device has its own "proc" structure
  478. hanging off the open file that's used to keep track of locks owned by the
  479. process and asts that need to be delivered to the process. */
  480. static int device_open(struct inode *inode, struct file *file)
  481. {
  482. struct dlm_user_proc *proc;
  483. struct dlm_ls *ls;
  484. ls = dlm_find_lockspace_device(iminor(inode));
  485. if (!ls)
  486. return -ENOENT;
  487. proc = kzalloc(sizeof(struct dlm_user_proc), GFP_KERNEL);
  488. if (!proc) {
  489. dlm_put_lockspace(ls);
  490. return -ENOMEM;
  491. }
  492. proc->lockspace = ls->ls_local_handle;
  493. INIT_LIST_HEAD(&proc->asts);
  494. INIT_LIST_HEAD(&proc->locks);
  495. INIT_LIST_HEAD(&proc->unlocking);
  496. spin_lock_init(&proc->asts_spin);
  497. spin_lock_init(&proc->locks_spin);
  498. init_waitqueue_head(&proc->wait);
  499. file->private_data = proc;
  500. return 0;
  501. }
  502. static int device_close(struct inode *inode, struct file *file)
  503. {
  504. struct dlm_user_proc *proc = file->private_data;
  505. struct dlm_ls *ls;
  506. sigset_t tmpsig, allsigs;
  507. ls = dlm_find_lockspace_local(proc->lockspace);
  508. if (!ls)
  509. return -ENOENT;
  510. sigfillset(&allsigs);
  511. sigprocmask(SIG_BLOCK, &allsigs, &tmpsig);
  512. set_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags);
  513. dlm_clear_proc_locks(ls, proc);
  514. /* at this point no more lkb's should exist for this lockspace,
  515. so there's no chance of dlm_user_add_ast() being called and
  516. looking for lkb->ua->proc */
  517. kfree(proc);
  518. file->private_data = NULL;
  519. dlm_put_lockspace(ls);
  520. dlm_put_lockspace(ls); /* for the find in device_open() */
  521. /* FIXME: AUTOFREE: if this ls is no longer used do
  522. device_remove_lockspace() */
  523. sigprocmask(SIG_SETMASK, &tmpsig, NULL);
  524. recalc_sigpending();
  525. return 0;
  526. }
  527. static int copy_result_to_user(struct dlm_user_args *ua, int compat, int type,
  528. int bmode, char __user *buf, size_t count)
  529. {
  530. #ifdef CONFIG_COMPAT
  531. struct dlm_lock_result32 result32;
  532. #endif
  533. struct dlm_lock_result result;
  534. void *resultptr;
  535. int error=0;
  536. int len;
  537. int struct_len;
  538. memset(&result, 0, sizeof(struct dlm_lock_result));
  539. memcpy(&result.lksb, &ua->lksb, sizeof(struct dlm_lksb));
  540. result.user_lksb = ua->user_lksb;
  541. /* FIXME: dlm1 provides for the user's bastparam/addr to not be updated
  542. in a conversion unless the conversion is successful. See code
  543. in dlm_user_convert() for updating ua from ua_tmp. OpenVMS, though,
  544. notes that a new blocking AST address and parameter are set even if
  545. the conversion fails, so maybe we should just do that. */
  546. if (type == AST_BAST) {
  547. result.user_astaddr = ua->bastaddr;
  548. result.user_astparam = ua->bastparam;
  549. result.bast_mode = bmode;
  550. } else {
  551. result.user_astaddr = ua->castaddr;
  552. result.user_astparam = ua->castparam;
  553. }
  554. #ifdef CONFIG_COMPAT
  555. if (compat)
  556. len = sizeof(struct dlm_lock_result32);
  557. else
  558. #endif
  559. len = sizeof(struct dlm_lock_result);
  560. struct_len = len;
  561. /* copy lvb to userspace if there is one, it's been updated, and
  562. the user buffer has space for it */
  563. if (ua->update_user_lvb && ua->lksb.sb_lvbptr &&
  564. count >= len + DLM_USER_LVB_LEN) {
  565. if (copy_to_user(buf+len, ua->lksb.sb_lvbptr,
  566. DLM_USER_LVB_LEN)) {
  567. error = -EFAULT;
  568. goto out;
  569. }
  570. result.lvb_offset = len;
  571. len += DLM_USER_LVB_LEN;
  572. }
  573. result.length = len;
  574. resultptr = &result;
  575. #ifdef CONFIG_COMPAT
  576. if (compat) {
  577. compat_output(&result, &result32);
  578. resultptr = &result32;
  579. }
  580. #endif
  581. if (copy_to_user(buf, resultptr, struct_len))
  582. error = -EFAULT;
  583. else
  584. error = len;
  585. out:
  586. return error;
  587. }
  588. /* a read returns a single ast described in a struct dlm_lock_result */
  589. static ssize_t device_read(struct file *file, char __user *buf, size_t count,
  590. loff_t *ppos)
  591. {
  592. struct dlm_user_proc *proc = file->private_data;
  593. struct dlm_lkb *lkb;
  594. struct dlm_user_args *ua;
  595. DECLARE_WAITQUEUE(wait, current);
  596. int error, type=0, bmode=0, removed = 0;
  597. #ifdef CONFIG_COMPAT
  598. if (count < sizeof(struct dlm_lock_result32))
  599. #else
  600. if (count < sizeof(struct dlm_lock_result))
  601. #endif
  602. return -EINVAL;
  603. /* do we really need this? can a read happen after a close? */
  604. if (test_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags))
  605. return -EINVAL;
  606. spin_lock(&proc->asts_spin);
  607. if (list_empty(&proc->asts)) {
  608. if (file->f_flags & O_NONBLOCK) {
  609. spin_unlock(&proc->asts_spin);
  610. return -EAGAIN;
  611. }
  612. add_wait_queue(&proc->wait, &wait);
  613. repeat:
  614. set_current_state(TASK_INTERRUPTIBLE);
  615. if (list_empty(&proc->asts) && !signal_pending(current)) {
  616. spin_unlock(&proc->asts_spin);
  617. schedule();
  618. spin_lock(&proc->asts_spin);
  619. goto repeat;
  620. }
  621. set_current_state(TASK_RUNNING);
  622. remove_wait_queue(&proc->wait, &wait);
  623. if (signal_pending(current)) {
  624. spin_unlock(&proc->asts_spin);
  625. return -ERESTARTSYS;
  626. }
  627. }
  628. if (list_empty(&proc->asts)) {
  629. spin_unlock(&proc->asts_spin);
  630. return -EAGAIN;
  631. }
  632. /* there may be both completion and blocking asts to return for
  633. the lkb, don't remove lkb from asts list unless no asts remain */
  634. lkb = list_entry(proc->asts.next, struct dlm_lkb, lkb_astqueue);
  635. if (lkb->lkb_ast_type & AST_COMP) {
  636. lkb->lkb_ast_type &= ~AST_COMP;
  637. type = AST_COMP;
  638. } else if (lkb->lkb_ast_type & AST_BAST) {
  639. lkb->lkb_ast_type &= ~AST_BAST;
  640. type = AST_BAST;
  641. bmode = lkb->lkb_bastmode;
  642. }
  643. if (!lkb->lkb_ast_type) {
  644. list_del(&lkb->lkb_astqueue);
  645. removed = 1;
  646. }
  647. spin_unlock(&proc->asts_spin);
  648. ua = (struct dlm_user_args *)lkb->lkb_astparam;
  649. error = copy_result_to_user(ua,
  650. test_bit(DLM_PROC_FLAGS_COMPAT, &proc->flags),
  651. type, bmode, buf, count);
  652. /* removes reference for the proc->asts lists added by
  653. dlm_user_add_ast() and may result in the lkb being freed */
  654. if (removed)
  655. dlm_put_lkb(lkb);
  656. return error;
  657. }
  658. static unsigned int device_poll(struct file *file, poll_table *wait)
  659. {
  660. struct dlm_user_proc *proc = file->private_data;
  661. poll_wait(file, &proc->wait, wait);
  662. spin_lock(&proc->asts_spin);
  663. if (!list_empty(&proc->asts)) {
  664. spin_unlock(&proc->asts_spin);
  665. return POLLIN | POLLRDNORM;
  666. }
  667. spin_unlock(&proc->asts_spin);
  668. return 0;
  669. }
  670. static int ctl_device_open(struct inode *inode, struct file *file)
  671. {
  672. file->private_data = NULL;
  673. return 0;
  674. }
  675. static int ctl_device_close(struct inode *inode, struct file *file)
  676. {
  677. return 0;
  678. }
  679. static const struct file_operations device_fops = {
  680. .open = device_open,
  681. .release = device_close,
  682. .read = device_read,
  683. .write = device_write,
  684. .poll = device_poll,
  685. .owner = THIS_MODULE,
  686. };
  687. static const struct file_operations ctl_device_fops = {
  688. .open = ctl_device_open,
  689. .release = ctl_device_close,
  690. .write = device_write,
  691. .owner = THIS_MODULE,
  692. };
  693. int dlm_user_init(void)
  694. {
  695. int error;
  696. ctl_device.name = "dlm-control";
  697. ctl_device.fops = &ctl_device_fops;
  698. ctl_device.minor = MISC_DYNAMIC_MINOR;
  699. error = misc_register(&ctl_device);
  700. if (error)
  701. log_print("misc_register failed for control device");
  702. return error;
  703. }
  704. void dlm_user_exit(void)
  705. {
  706. misc_deregister(&ctl_device);
  707. }