user.c 22 KB

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