plock.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2005-2008 Red Hat, Inc. All rights reserved.
  4. */
  5. #include <linux/fs.h>
  6. #include <linux/filelock.h>
  7. #include <linux/miscdevice.h>
  8. #include <linux/poll.h>
  9. #include <linux/dlm.h>
  10. #include <linux/dlm_plock.h>
  11. #include <linux/slab.h>
  12. #include <trace/events/dlm.h>
  13. #include "dlm_internal.h"
  14. #include "lockspace.h"
  15. static DEFINE_SPINLOCK(ops_lock);
  16. static LIST_HEAD(send_list);
  17. static LIST_HEAD(recv_list);
  18. static DECLARE_WAIT_QUEUE_HEAD(send_wq);
  19. static DECLARE_WAIT_QUEUE_HEAD(recv_wq);
  20. struct plock_async_data {
  21. void *fl;
  22. void *file;
  23. struct file_lock flc;
  24. int (*callback)(struct file_lock *fl, int result);
  25. };
  26. struct plock_op {
  27. struct list_head list;
  28. int done;
  29. struct dlm_plock_info info;
  30. /* if set indicates async handling */
  31. struct plock_async_data *data;
  32. };
  33. static inline void set_version(struct dlm_plock_info *info)
  34. {
  35. info->version[0] = DLM_PLOCK_VERSION_MAJOR;
  36. info->version[1] = DLM_PLOCK_VERSION_MINOR;
  37. info->version[2] = DLM_PLOCK_VERSION_PATCH;
  38. }
  39. static struct plock_op *plock_lookup_waiter(const struct dlm_plock_info *info)
  40. {
  41. struct plock_op *op = NULL, *iter;
  42. list_for_each_entry(iter, &recv_list, list) {
  43. if (iter->info.fsid == info->fsid &&
  44. iter->info.number == info->number &&
  45. iter->info.owner == info->owner &&
  46. iter->info.pid == info->pid &&
  47. iter->info.start == info->start &&
  48. iter->info.end == info->end &&
  49. iter->info.ex == info->ex &&
  50. iter->info.wait) {
  51. op = iter;
  52. break;
  53. }
  54. }
  55. return op;
  56. }
  57. static int check_version(struct dlm_plock_info *info)
  58. {
  59. if ((DLM_PLOCK_VERSION_MAJOR != info->version[0]) ||
  60. (DLM_PLOCK_VERSION_MINOR < info->version[1])) {
  61. log_print("plock device version mismatch: "
  62. "kernel (%u.%u.%u), user (%u.%u.%u)",
  63. DLM_PLOCK_VERSION_MAJOR,
  64. DLM_PLOCK_VERSION_MINOR,
  65. DLM_PLOCK_VERSION_PATCH,
  66. info->version[0],
  67. info->version[1],
  68. info->version[2]);
  69. return -EINVAL;
  70. }
  71. return 0;
  72. }
  73. static void dlm_release_plock_op(struct plock_op *op)
  74. {
  75. kfree(op->data);
  76. kfree(op);
  77. }
  78. static void send_op(struct plock_op *op)
  79. {
  80. set_version(&op->info);
  81. spin_lock(&ops_lock);
  82. list_add_tail(&op->list, &send_list);
  83. spin_unlock(&ops_lock);
  84. wake_up(&send_wq);
  85. }
  86. static int do_lock_cancel(const struct dlm_plock_info *orig_info)
  87. {
  88. struct plock_op *op;
  89. int rv;
  90. op = kzalloc_obj(*op, GFP_NOFS);
  91. if (!op)
  92. return -ENOMEM;
  93. op->info = *orig_info;
  94. op->info.optype = DLM_PLOCK_OP_CANCEL;
  95. op->info.wait = 0;
  96. send_op(op);
  97. wait_event(recv_wq, (op->done != 0));
  98. rv = op->info.rv;
  99. dlm_release_plock_op(op);
  100. return rv;
  101. }
  102. int dlm_posix_lock(dlm_lockspace_t *lockspace, u64 number, struct file *file,
  103. int cmd, struct file_lock *fl)
  104. {
  105. struct plock_async_data *op_data;
  106. struct dlm_ls *ls;
  107. struct plock_op *op;
  108. int rv;
  109. ls = dlm_find_lockspace_local(lockspace);
  110. if (!ls)
  111. return -EINVAL;
  112. op = kzalloc_obj(*op, GFP_NOFS);
  113. if (!op) {
  114. rv = -ENOMEM;
  115. goto out;
  116. }
  117. op->info.optype = DLM_PLOCK_OP_LOCK;
  118. op->info.pid = fl->c.flc_pid;
  119. op->info.ex = lock_is_write(fl);
  120. op->info.wait = !!(fl->c.flc_flags & FL_SLEEP);
  121. op->info.fsid = ls->ls_global_id;
  122. op->info.number = number;
  123. op->info.start = fl->fl_start;
  124. op->info.end = fl->fl_end;
  125. op->info.owner = (__u64)(long) fl->c.flc_owner;
  126. /* async handling */
  127. if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
  128. op_data = kzalloc_obj(*op_data, GFP_NOFS);
  129. if (!op_data) {
  130. dlm_release_plock_op(op);
  131. rv = -ENOMEM;
  132. goto out;
  133. }
  134. op_data->callback = fl->fl_lmops->lm_grant;
  135. locks_init_lock(&op_data->flc);
  136. locks_copy_lock(&op_data->flc, fl);
  137. op_data->fl = fl;
  138. op_data->file = file;
  139. op->data = op_data;
  140. send_op(op);
  141. rv = FILE_LOCK_DEFERRED;
  142. goto out;
  143. }
  144. send_op(op);
  145. if (op->info.wait) {
  146. rv = wait_event_interruptible(recv_wq, (op->done != 0));
  147. if (rv == -ERESTARTSYS) {
  148. spin_lock(&ops_lock);
  149. /* recheck under ops_lock if we got a done != 0,
  150. * if so this interrupt case should be ignored
  151. */
  152. if (op->done != 0) {
  153. spin_unlock(&ops_lock);
  154. goto do_lock_wait;
  155. }
  156. spin_unlock(&ops_lock);
  157. rv = do_lock_cancel(&op->info);
  158. switch (rv) {
  159. case 0:
  160. /* waiter was deleted in user space, answer will never come
  161. * remove original request. The original request must be
  162. * on recv_list because the answer of do_lock_cancel()
  163. * synchronized it.
  164. */
  165. spin_lock(&ops_lock);
  166. list_del(&op->list);
  167. spin_unlock(&ops_lock);
  168. rv = -EINTR;
  169. break;
  170. case -ENOENT:
  171. /* cancellation wasn't successful but op should be done */
  172. fallthrough;
  173. default:
  174. /* internal error doing cancel we need to wait */
  175. goto wait;
  176. }
  177. log_debug(ls, "%s: wait interrupted %x %llx pid %d",
  178. __func__, ls->ls_global_id,
  179. (unsigned long long)number, op->info.pid);
  180. dlm_release_plock_op(op);
  181. goto out;
  182. }
  183. } else {
  184. wait:
  185. wait_event(recv_wq, (op->done != 0));
  186. }
  187. do_lock_wait:
  188. WARN_ON(!list_empty(&op->list));
  189. rv = op->info.rv;
  190. if (!rv) {
  191. if (locks_lock_file_wait(file, fl) < 0)
  192. log_error(ls, "dlm_posix_lock: vfs lock error %llx",
  193. (unsigned long long)number);
  194. }
  195. dlm_release_plock_op(op);
  196. out:
  197. dlm_put_lockspace(ls);
  198. return rv;
  199. }
  200. EXPORT_SYMBOL_GPL(dlm_posix_lock);
  201. /* Returns failure iff a successful lock operation should be canceled */
  202. static int dlm_plock_callback(struct plock_op *op)
  203. {
  204. struct plock_async_data *op_data = op->data;
  205. struct file *file;
  206. struct file_lock *fl;
  207. struct file_lock *flc;
  208. int (*notify)(struct file_lock *fl, int result) = NULL;
  209. int rv = 0;
  210. WARN_ON(!list_empty(&op->list));
  211. /* check if the following 2 are still valid or make a copy */
  212. file = op_data->file;
  213. flc = &op_data->flc;
  214. fl = op_data->fl;
  215. notify = op_data->callback;
  216. if (op->info.rv) {
  217. notify(fl, op->info.rv);
  218. goto out;
  219. }
  220. /* got fs lock; bookkeep locally as well: */
  221. flc->c.flc_flags &= ~FL_SLEEP;
  222. if (posix_lock_file(file, flc, NULL)) {
  223. /*
  224. * This can only happen in the case of kmalloc() failure.
  225. * The filesystem's own lock is the authoritative lock,
  226. * so a failure to get the lock locally is not a disaster.
  227. * As long as the fs cannot reliably cancel locks (especially
  228. * in a low-memory situation), we're better off ignoring
  229. * this failure than trying to recover.
  230. */
  231. log_print("dlm_plock_callback: vfs lock error %llx file %p fl %p",
  232. (unsigned long long)op->info.number, file, fl);
  233. }
  234. rv = notify(fl, 0);
  235. if (rv) {
  236. /* XXX: We need to cancel the fs lock here: */
  237. log_print("%s: lock granted after lock request failed; dangling lock!",
  238. __func__);
  239. goto out;
  240. }
  241. out:
  242. dlm_release_plock_op(op);
  243. return rv;
  244. }
  245. int dlm_posix_unlock(dlm_lockspace_t *lockspace, u64 number, struct file *file,
  246. struct file_lock *fl)
  247. {
  248. struct dlm_ls *ls;
  249. struct plock_op *op;
  250. int rv;
  251. unsigned char saved_flags = fl->c.flc_flags;
  252. ls = dlm_find_lockspace_local(lockspace);
  253. if (!ls)
  254. return -EINVAL;
  255. op = kzalloc_obj(*op, GFP_NOFS);
  256. if (!op) {
  257. rv = -ENOMEM;
  258. goto out;
  259. }
  260. /* cause the vfs unlock to return ENOENT if lock is not found */
  261. fl->c.flc_flags |= FL_EXISTS;
  262. rv = locks_lock_file_wait(file, fl);
  263. if (rv == -ENOENT) {
  264. rv = 0;
  265. goto out_free;
  266. }
  267. if (rv < 0) {
  268. log_error(ls, "dlm_posix_unlock: vfs unlock error %d %llx",
  269. rv, (unsigned long long)number);
  270. }
  271. op->info.optype = DLM_PLOCK_OP_UNLOCK;
  272. op->info.pid = fl->c.flc_pid;
  273. op->info.fsid = ls->ls_global_id;
  274. op->info.number = number;
  275. op->info.start = fl->fl_start;
  276. op->info.end = fl->fl_end;
  277. op->info.owner = (__u64)(long) fl->c.flc_owner;
  278. if (fl->c.flc_flags & FL_CLOSE) {
  279. op->info.flags |= DLM_PLOCK_FL_CLOSE;
  280. send_op(op);
  281. rv = 0;
  282. goto out;
  283. }
  284. send_op(op);
  285. wait_event(recv_wq, (op->done != 0));
  286. WARN_ON(!list_empty(&op->list));
  287. rv = op->info.rv;
  288. if (rv == -ENOENT)
  289. rv = 0;
  290. out_free:
  291. dlm_release_plock_op(op);
  292. out:
  293. dlm_put_lockspace(ls);
  294. fl->c.flc_flags = saved_flags;
  295. return rv;
  296. }
  297. EXPORT_SYMBOL_GPL(dlm_posix_unlock);
  298. /*
  299. * NOTE: This implementation can only handle async lock requests as nfs
  300. * do it. It cannot handle cancellation of a pending lock request sitting
  301. * in wait_event(), but for now only nfs is the only user local kernel
  302. * user.
  303. */
  304. int dlm_posix_cancel(dlm_lockspace_t *lockspace, u64 number, struct file *file,
  305. struct file_lock *fl)
  306. {
  307. struct dlm_plock_info info;
  308. struct plock_op *op;
  309. struct dlm_ls *ls;
  310. int rv;
  311. /* this only works for async request for now and nfs is the only
  312. * kernel user right now.
  313. */
  314. if (WARN_ON_ONCE(!fl->fl_lmops || !fl->fl_lmops->lm_grant))
  315. return -EOPNOTSUPP;
  316. ls = dlm_find_lockspace_local(lockspace);
  317. if (!ls)
  318. return -EINVAL;
  319. memset(&info, 0, sizeof(info));
  320. info.pid = fl->c.flc_pid;
  321. info.ex = lock_is_write(fl);
  322. info.fsid = ls->ls_global_id;
  323. dlm_put_lockspace(ls);
  324. info.number = number;
  325. info.start = fl->fl_start;
  326. info.end = fl->fl_end;
  327. info.owner = (__u64)(long) fl->c.flc_owner;
  328. rv = do_lock_cancel(&info);
  329. switch (rv) {
  330. case 0:
  331. spin_lock(&ops_lock);
  332. /* lock request to cancel must be on recv_list because
  333. * do_lock_cancel() synchronizes it.
  334. */
  335. op = plock_lookup_waiter(&info);
  336. if (WARN_ON_ONCE(!op)) {
  337. spin_unlock(&ops_lock);
  338. rv = -ENOLCK;
  339. break;
  340. }
  341. list_del(&op->list);
  342. spin_unlock(&ops_lock);
  343. WARN_ON(op->info.optype != DLM_PLOCK_OP_LOCK);
  344. op->data->callback(op->data->fl, -EINTR);
  345. dlm_release_plock_op(op);
  346. rv = -EINTR;
  347. break;
  348. case -ENOENT:
  349. /* if cancel wasn't successful we probably were to late
  350. * or it was a non-blocking lock request, so just unlock it.
  351. */
  352. rv = dlm_posix_unlock(lockspace, number, file, fl);
  353. break;
  354. default:
  355. break;
  356. }
  357. return rv;
  358. }
  359. EXPORT_SYMBOL_GPL(dlm_posix_cancel);
  360. int dlm_posix_get(dlm_lockspace_t *lockspace, u64 number, struct file *file,
  361. struct file_lock *fl)
  362. {
  363. struct dlm_ls *ls;
  364. struct plock_op *op;
  365. int rv;
  366. ls = dlm_find_lockspace_local(lockspace);
  367. if (!ls)
  368. return -EINVAL;
  369. op = kzalloc_obj(*op, GFP_NOFS);
  370. if (!op) {
  371. rv = -ENOMEM;
  372. goto out;
  373. }
  374. op->info.optype = DLM_PLOCK_OP_GET;
  375. op->info.pid = fl->c.flc_pid;
  376. op->info.ex = lock_is_write(fl);
  377. op->info.fsid = ls->ls_global_id;
  378. op->info.number = number;
  379. op->info.start = fl->fl_start;
  380. op->info.end = fl->fl_end;
  381. op->info.owner = (__u64)(long) fl->c.flc_owner;
  382. send_op(op);
  383. wait_event(recv_wq, (op->done != 0));
  384. WARN_ON(!list_empty(&op->list));
  385. /* info.rv from userspace is 1 for conflict, 0 for no-conflict,
  386. -ENOENT if there are no locks on the file */
  387. rv = op->info.rv;
  388. fl->c.flc_type = F_UNLCK;
  389. if (rv == -ENOENT)
  390. rv = 0;
  391. else if (rv > 0) {
  392. locks_init_lock(fl);
  393. fl->c.flc_type = (op->info.ex) ? F_WRLCK : F_RDLCK;
  394. fl->c.flc_flags = FL_POSIX;
  395. fl->c.flc_pid = op->info.pid;
  396. if (op->info.nodeid != dlm_our_nodeid())
  397. fl->c.flc_pid = -fl->c.flc_pid;
  398. fl->fl_start = op->info.start;
  399. fl->fl_end = op->info.end;
  400. rv = 0;
  401. }
  402. dlm_release_plock_op(op);
  403. out:
  404. dlm_put_lockspace(ls);
  405. return rv;
  406. }
  407. EXPORT_SYMBOL_GPL(dlm_posix_get);
  408. /* a read copies out one plock request from the send list */
  409. static ssize_t dev_read(struct file *file, char __user *u, size_t count,
  410. loff_t *ppos)
  411. {
  412. struct dlm_plock_info info;
  413. struct plock_op *op = NULL;
  414. if (count < sizeof(info))
  415. return -EINVAL;
  416. spin_lock(&ops_lock);
  417. if (!list_empty(&send_list)) {
  418. op = list_first_entry(&send_list, struct plock_op, list);
  419. if (op->info.flags & DLM_PLOCK_FL_CLOSE)
  420. list_del(&op->list);
  421. else
  422. list_move_tail(&op->list, &recv_list);
  423. memcpy(&info, &op->info, sizeof(info));
  424. }
  425. spin_unlock(&ops_lock);
  426. if (!op)
  427. return -EAGAIN;
  428. trace_dlm_plock_read(&info);
  429. /* there is no need to get a reply from userspace for unlocks
  430. that were generated by the vfs cleaning up for a close
  431. (the process did not make an unlock call). */
  432. if (op->info.flags & DLM_PLOCK_FL_CLOSE)
  433. dlm_release_plock_op(op);
  434. if (copy_to_user(u, &info, sizeof(info)))
  435. return -EFAULT;
  436. return sizeof(info);
  437. }
  438. /* a write copies in one plock result that should match a plock_op
  439. on the recv list */
  440. static ssize_t dev_write(struct file *file, const char __user *u, size_t count,
  441. loff_t *ppos)
  442. {
  443. struct plock_op *op = NULL, *iter;
  444. struct dlm_plock_info info;
  445. int do_callback = 0;
  446. if (count != sizeof(info))
  447. return -EINVAL;
  448. if (copy_from_user(&info, u, sizeof(info)))
  449. return -EFAULT;
  450. trace_dlm_plock_write(&info);
  451. if (check_version(&info))
  452. return -EINVAL;
  453. /*
  454. * The results for waiting ops (SETLKW) can be returned in any
  455. * order, so match all fields to find the op. The results for
  456. * non-waiting ops are returned in the order that they were sent
  457. * to userspace, so match the result with the first non-waiting op.
  458. */
  459. spin_lock(&ops_lock);
  460. if (info.wait) {
  461. op = plock_lookup_waiter(&info);
  462. } else {
  463. list_for_each_entry(iter, &recv_list, list) {
  464. if (!iter->info.wait &&
  465. iter->info.fsid == info.fsid) {
  466. op = iter;
  467. break;
  468. }
  469. }
  470. }
  471. if (op) {
  472. /* Sanity check that op and info match. */
  473. if (info.wait)
  474. WARN_ON(op->info.optype != DLM_PLOCK_OP_LOCK);
  475. else
  476. WARN_ON(op->info.number != info.number ||
  477. op->info.owner != info.owner ||
  478. op->info.optype != info.optype);
  479. list_del_init(&op->list);
  480. memcpy(&op->info, &info, sizeof(info));
  481. if (op->data)
  482. do_callback = 1;
  483. else
  484. op->done = 1;
  485. }
  486. spin_unlock(&ops_lock);
  487. if (op) {
  488. if (do_callback)
  489. dlm_plock_callback(op);
  490. else
  491. wake_up(&recv_wq);
  492. } else
  493. pr_debug("%s: no op %x %llx", __func__,
  494. info.fsid, (unsigned long long)info.number);
  495. return count;
  496. }
  497. static __poll_t dev_poll(struct file *file, poll_table *wait)
  498. {
  499. __poll_t mask = 0;
  500. poll_wait(file, &send_wq, wait);
  501. spin_lock(&ops_lock);
  502. if (!list_empty(&send_list))
  503. mask = EPOLLIN | EPOLLRDNORM;
  504. spin_unlock(&ops_lock);
  505. return mask;
  506. }
  507. static const struct file_operations dev_fops = {
  508. .read = dev_read,
  509. .write = dev_write,
  510. .poll = dev_poll,
  511. .owner = THIS_MODULE,
  512. .llseek = noop_llseek,
  513. };
  514. static struct miscdevice plock_dev_misc = {
  515. .minor = MISC_DYNAMIC_MINOR,
  516. .name = DLM_PLOCK_MISC_NAME,
  517. .fops = &dev_fops
  518. };
  519. int dlm_plock_init(void)
  520. {
  521. int rv;
  522. rv = misc_register(&plock_dev_misc);
  523. if (rv)
  524. log_print("dlm_plock_init: misc_register failed %d", rv);
  525. return rv;
  526. }
  527. void dlm_plock_exit(void)
  528. {
  529. misc_deregister(&plock_dev_misc);
  530. WARN_ON(!list_empty(&send_list));
  531. WARN_ON(!list_empty(&recv_list));
  532. }