plock.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Copyright (C) 2005 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 version 2.
  7. */
  8. #include <linux/miscdevice.h>
  9. #include <linux/lock_dlm_plock.h>
  10. #include <linux/poll.h>
  11. #include "lock_dlm.h"
  12. static spinlock_t ops_lock;
  13. static struct list_head send_list;
  14. static struct list_head recv_list;
  15. static wait_queue_head_t send_wq;
  16. static wait_queue_head_t recv_wq;
  17. struct plock_op {
  18. struct list_head list;
  19. int done;
  20. struct gdlm_plock_info info;
  21. };
  22. struct plock_xop {
  23. struct plock_op xop;
  24. void *callback;
  25. void *fl;
  26. void *file;
  27. struct file_lock flc;
  28. };
  29. static inline void set_version(struct gdlm_plock_info *info)
  30. {
  31. info->version[0] = GDLM_PLOCK_VERSION_MAJOR;
  32. info->version[1] = GDLM_PLOCK_VERSION_MINOR;
  33. info->version[2] = GDLM_PLOCK_VERSION_PATCH;
  34. }
  35. static int check_version(struct gdlm_plock_info *info)
  36. {
  37. if ((GDLM_PLOCK_VERSION_MAJOR != info->version[0]) ||
  38. (GDLM_PLOCK_VERSION_MINOR < info->version[1])) {
  39. log_error("plock device version mismatch: "
  40. "kernel (%u.%u.%u), user (%u.%u.%u)",
  41. GDLM_PLOCK_VERSION_MAJOR,
  42. GDLM_PLOCK_VERSION_MINOR,
  43. GDLM_PLOCK_VERSION_PATCH,
  44. info->version[0],
  45. info->version[1],
  46. info->version[2]);
  47. return -EINVAL;
  48. }
  49. return 0;
  50. }
  51. static void send_op(struct plock_op *op)
  52. {
  53. set_version(&op->info);
  54. INIT_LIST_HEAD(&op->list);
  55. spin_lock(&ops_lock);
  56. list_add_tail(&op->list, &send_list);
  57. spin_unlock(&ops_lock);
  58. wake_up(&send_wq);
  59. }
  60. int gdlm_plock(void *lockspace, struct lm_lockname *name,
  61. struct file *file, int cmd, struct file_lock *fl)
  62. {
  63. struct gdlm_ls *ls = lockspace;
  64. struct plock_op *op;
  65. struct plock_xop *xop;
  66. int rv;
  67. xop = kzalloc(sizeof(*xop), GFP_KERNEL);
  68. if (!xop)
  69. return -ENOMEM;
  70. op = &xop->xop;
  71. op->info.optype = GDLM_PLOCK_OP_LOCK;
  72. op->info.pid = fl->fl_pid;
  73. op->info.ex = (fl->fl_type == F_WRLCK);
  74. op->info.wait = IS_SETLKW(cmd);
  75. op->info.fsid = ls->id;
  76. op->info.number = name->ln_number;
  77. op->info.start = fl->fl_start;
  78. op->info.end = fl->fl_end;
  79. if (fl->fl_lmops && fl->fl_lmops->fl_grant) {
  80. /* fl_owner is lockd which doesn't distinguish
  81. processes on the nfs client */
  82. op->info.owner = (__u64) fl->fl_pid;
  83. xop->callback = fl->fl_lmops->fl_grant;
  84. locks_init_lock(&xop->flc);
  85. locks_copy_lock(&xop->flc, fl);
  86. xop->fl = fl;
  87. xop->file = file;
  88. } else {
  89. op->info.owner = (__u64)(long) fl->fl_owner;
  90. xop->callback = NULL;
  91. }
  92. send_op(op);
  93. if (xop->callback == NULL)
  94. wait_event(recv_wq, (op->done != 0));
  95. else
  96. return -EINPROGRESS;
  97. spin_lock(&ops_lock);
  98. if (!list_empty(&op->list)) {
  99. printk(KERN_INFO "plock op on list\n");
  100. list_del(&op->list);
  101. }
  102. spin_unlock(&ops_lock);
  103. rv = op->info.rv;
  104. if (!rv) {
  105. if (posix_lock_file_wait(file, fl) < 0)
  106. log_error("gdlm_plock: vfs lock error %x,%llx",
  107. name->ln_type,
  108. (unsigned long long)name->ln_number);
  109. }
  110. kfree(xop);
  111. return rv;
  112. }
  113. /* Returns failure iff a succesful lock operation should be canceled */
  114. static int gdlm_plock_callback(struct plock_op *op)
  115. {
  116. struct file *file;
  117. struct file_lock *fl;
  118. struct file_lock *flc;
  119. int (*notify)(void *, void *, int) = NULL;
  120. struct plock_xop *xop = (struct plock_xop *)op;
  121. int rv = 0;
  122. spin_lock(&ops_lock);
  123. if (!list_empty(&op->list)) {
  124. printk(KERN_INFO "plock op on list\n");
  125. list_del(&op->list);
  126. }
  127. spin_unlock(&ops_lock);
  128. /* check if the following 2 are still valid or make a copy */
  129. file = xop->file;
  130. flc = &xop->flc;
  131. fl = xop->fl;
  132. notify = xop->callback;
  133. if (op->info.rv) {
  134. notify(flc, NULL, op->info.rv);
  135. goto out;
  136. }
  137. /* got fs lock; bookkeep locally as well: */
  138. flc->fl_flags &= ~FL_SLEEP;
  139. if (posix_lock_file(file, flc, NULL)) {
  140. /*
  141. * This can only happen in the case of kmalloc() failure.
  142. * The filesystem's own lock is the authoritative lock,
  143. * so a failure to get the lock locally is not a disaster.
  144. * As long as GFS cannot reliably cancel locks (especially
  145. * in a low-memory situation), we're better off ignoring
  146. * this failure than trying to recover.
  147. */
  148. log_error("gdlm_plock: vfs lock error file %p fl %p",
  149. file, fl);
  150. }
  151. rv = notify(flc, NULL, 0);
  152. if (rv) {
  153. /* XXX: We need to cancel the fs lock here: */
  154. printk("gfs2 lock granted after lock request failed;"
  155. " dangling lock!\n");
  156. goto out;
  157. }
  158. out:
  159. kfree(xop);
  160. return rv;
  161. }
  162. int gdlm_punlock(void *lockspace, struct lm_lockname *name,
  163. struct file *file, struct file_lock *fl)
  164. {
  165. struct gdlm_ls *ls = lockspace;
  166. struct plock_op *op;
  167. int rv;
  168. op = kzalloc(sizeof(*op), GFP_KERNEL);
  169. if (!op)
  170. return -ENOMEM;
  171. if (posix_lock_file_wait(file, fl) < 0)
  172. log_error("gdlm_punlock: vfs unlock error %x,%llx",
  173. name->ln_type, (unsigned long long)name->ln_number);
  174. op->info.optype = GDLM_PLOCK_OP_UNLOCK;
  175. op->info.pid = fl->fl_pid;
  176. op->info.fsid = ls->id;
  177. op->info.number = name->ln_number;
  178. op->info.start = fl->fl_start;
  179. op->info.end = fl->fl_end;
  180. if (fl->fl_lmops && fl->fl_lmops->fl_grant)
  181. op->info.owner = (__u64) fl->fl_pid;
  182. else
  183. op->info.owner = (__u64)(long) fl->fl_owner;
  184. send_op(op);
  185. wait_event(recv_wq, (op->done != 0));
  186. spin_lock(&ops_lock);
  187. if (!list_empty(&op->list)) {
  188. printk(KERN_INFO "punlock op on list\n");
  189. list_del(&op->list);
  190. }
  191. spin_unlock(&ops_lock);
  192. rv = op->info.rv;
  193. if (rv == -ENOENT)
  194. rv = 0;
  195. kfree(op);
  196. return rv;
  197. }
  198. int gdlm_plock_get(void *lockspace, struct lm_lockname *name,
  199. struct file *file, struct file_lock *fl)
  200. {
  201. struct gdlm_ls *ls = lockspace;
  202. struct plock_op *op;
  203. int rv;
  204. op = kzalloc(sizeof(*op), GFP_KERNEL);
  205. if (!op)
  206. return -ENOMEM;
  207. op->info.optype = GDLM_PLOCK_OP_GET;
  208. op->info.pid = fl->fl_pid;
  209. op->info.ex = (fl->fl_type == F_WRLCK);
  210. op->info.fsid = ls->id;
  211. op->info.number = name->ln_number;
  212. op->info.start = fl->fl_start;
  213. op->info.end = fl->fl_end;
  214. if (fl->fl_lmops && fl->fl_lmops->fl_grant)
  215. op->info.owner = (__u64) fl->fl_pid;
  216. else
  217. op->info.owner = (__u64)(long) fl->fl_owner;
  218. send_op(op);
  219. wait_event(recv_wq, (op->done != 0));
  220. spin_lock(&ops_lock);
  221. if (!list_empty(&op->list)) {
  222. printk(KERN_INFO "plock_get op on list\n");
  223. list_del(&op->list);
  224. }
  225. spin_unlock(&ops_lock);
  226. /* info.rv from userspace is 1 for conflict, 0 for no-conflict,
  227. -ENOENT if there are no locks on the file */
  228. rv = op->info.rv;
  229. fl->fl_type = F_UNLCK;
  230. if (rv == -ENOENT)
  231. rv = 0;
  232. else if (rv > 0) {
  233. fl->fl_type = (op->info.ex) ? F_WRLCK : F_RDLCK;
  234. fl->fl_pid = op->info.pid;
  235. fl->fl_start = op->info.start;
  236. fl->fl_end = op->info.end;
  237. rv = 0;
  238. }
  239. kfree(op);
  240. return rv;
  241. }
  242. /* a read copies out one plock request from the send list */
  243. static ssize_t dev_read(struct file *file, char __user *u, size_t count,
  244. loff_t *ppos)
  245. {
  246. struct gdlm_plock_info info;
  247. struct plock_op *op = NULL;
  248. if (count < sizeof(info))
  249. return -EINVAL;
  250. spin_lock(&ops_lock);
  251. if (!list_empty(&send_list)) {
  252. op = list_entry(send_list.next, struct plock_op, list);
  253. list_move(&op->list, &recv_list);
  254. memcpy(&info, &op->info, sizeof(info));
  255. }
  256. spin_unlock(&ops_lock);
  257. if (!op)
  258. return -EAGAIN;
  259. if (copy_to_user(u, &info, sizeof(info)))
  260. return -EFAULT;
  261. return sizeof(info);
  262. }
  263. /* a write copies in one plock result that should match a plock_op
  264. on the recv list */
  265. static ssize_t dev_write(struct file *file, const char __user *u, size_t count,
  266. loff_t *ppos)
  267. {
  268. struct gdlm_plock_info info;
  269. struct plock_op *op;
  270. int found = 0;
  271. if (count != sizeof(info))
  272. return -EINVAL;
  273. if (copy_from_user(&info, u, sizeof(info)))
  274. return -EFAULT;
  275. if (check_version(&info))
  276. return -EINVAL;
  277. spin_lock(&ops_lock);
  278. list_for_each_entry(op, &recv_list, list) {
  279. if (op->info.fsid == info.fsid && op->info.number == info.number &&
  280. op->info.owner == info.owner) {
  281. list_del_init(&op->list);
  282. found = 1;
  283. op->done = 1;
  284. memcpy(&op->info, &info, sizeof(info));
  285. break;
  286. }
  287. }
  288. spin_unlock(&ops_lock);
  289. if (found) {
  290. struct plock_xop *xop;
  291. xop = (struct plock_xop *)op;
  292. if (xop->callback)
  293. count = gdlm_plock_callback(op);
  294. else
  295. wake_up(&recv_wq);
  296. } else
  297. printk(KERN_INFO "gdlm dev_write no op %x %llx\n", info.fsid,
  298. (unsigned long long)info.number);
  299. return count;
  300. }
  301. static unsigned int dev_poll(struct file *file, poll_table *wait)
  302. {
  303. unsigned int mask = 0;
  304. poll_wait(file, &send_wq, wait);
  305. spin_lock(&ops_lock);
  306. if (!list_empty(&send_list))
  307. mask = POLLIN | POLLRDNORM;
  308. spin_unlock(&ops_lock);
  309. return mask;
  310. }
  311. static const struct file_operations dev_fops = {
  312. .read = dev_read,
  313. .write = dev_write,
  314. .poll = dev_poll,
  315. .owner = THIS_MODULE
  316. };
  317. static struct miscdevice plock_dev_misc = {
  318. .minor = MISC_DYNAMIC_MINOR,
  319. .name = GDLM_PLOCK_MISC_NAME,
  320. .fops = &dev_fops
  321. };
  322. int gdlm_plock_init(void)
  323. {
  324. int rv;
  325. spin_lock_init(&ops_lock);
  326. INIT_LIST_HEAD(&send_list);
  327. INIT_LIST_HEAD(&recv_list);
  328. init_waitqueue_head(&send_wq);
  329. init_waitqueue_head(&recv_wq);
  330. rv = misc_register(&plock_dev_misc);
  331. if (rv)
  332. printk(KERN_INFO "gdlm_plock_init: misc_register failed %d",
  333. rv);
  334. return rv;
  335. }
  336. void gdlm_plock_exit(void)
  337. {
  338. if (misc_deregister(&plock_dev_misc) < 0)
  339. printk(KERN_INFO "gdlm_plock_exit: misc_deregister failed");
  340. }