user.c 25 KB

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