user.c 25 KB

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