plock.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. op->info.owner = (__u64)(long) fl->fl_owner;
  80. if (fl->fl_lmops && fl->fl_lmops->fl_grant) {
  81. xop->callback = fl->fl_lmops->fl_grant;
  82. locks_init_lock(&xop->flc);
  83. locks_copy_lock(&xop->flc, fl);
  84. xop->fl = fl;
  85. xop->file = file;
  86. } else
  87. xop->callback = NULL;
  88. send_op(op);
  89. if (xop->callback == NULL)
  90. wait_event(recv_wq, (op->done != 0));
  91. else
  92. return -EINPROGRESS;
  93. spin_lock(&ops_lock);
  94. if (!list_empty(&op->list)) {
  95. printk(KERN_INFO "plock op on list\n");
  96. list_del(&op->list);
  97. }
  98. spin_unlock(&ops_lock);
  99. rv = op->info.rv;
  100. if (!rv) {
  101. if (posix_lock_file_wait(file, fl) < 0)
  102. log_error("gdlm_plock: vfs lock error %x,%llx",
  103. name->ln_type,
  104. (unsigned long long)name->ln_number);
  105. }
  106. kfree(xop);
  107. return rv;
  108. }
  109. /* Returns failure iff a succesful lock operation should be canceled */
  110. static int gdlm_plock_callback(struct plock_op *op)
  111. {
  112. struct file *file;
  113. struct file_lock *fl;
  114. struct file_lock *flc;
  115. int (*notify)(void *, void *, int) = NULL;
  116. struct plock_xop *xop = (struct plock_xop *)op;
  117. int rv = 0;
  118. spin_lock(&ops_lock);
  119. if (!list_empty(&op->list)) {
  120. printk(KERN_INFO "plock op on list\n");
  121. list_del(&op->list);
  122. }
  123. spin_unlock(&ops_lock);
  124. /* check if the following 2 are still valid or make a copy */
  125. file = xop->file;
  126. flc = &xop->flc;
  127. fl = xop->fl;
  128. notify = xop->callback;
  129. if (op->info.rv) {
  130. notify(flc, NULL, op->info.rv);
  131. goto out;
  132. }
  133. /* got fs lock; bookkeep locally as well: */
  134. flc->fl_flags &= ~FL_SLEEP;
  135. if (posix_lock_file(file, flc, NULL)) {
  136. /*
  137. * This can only happen in the case of kmalloc() failure.
  138. * The filesystem's own lock is the authoritative lock,
  139. * so a failure to get the lock locally is not a disaster.
  140. * As long as GFS cannot reliably cancel locks (especially
  141. * in a low-memory situation), we're better off ignoring
  142. * this failure than trying to recover.
  143. */
  144. log_error("gdlm_plock: vfs lock error file %p fl %p",
  145. file, fl);
  146. }
  147. rv = notify(flc, NULL, 0);
  148. if (rv) {
  149. /* XXX: We need to cancel the fs lock here: */
  150. printk("gfs2 lock granted after lock request failed;"
  151. " dangling lock!\n");
  152. goto out;
  153. }
  154. out:
  155. kfree(xop);
  156. return rv;
  157. }
  158. int gdlm_punlock(void *lockspace, struct lm_lockname *name,
  159. struct file *file, struct file_lock *fl)
  160. {
  161. struct gdlm_ls *ls = lockspace;
  162. struct plock_op *op;
  163. int rv;
  164. op = kzalloc(sizeof(*op), GFP_KERNEL);
  165. if (!op)
  166. return -ENOMEM;
  167. if (posix_lock_file_wait(file, fl) < 0)
  168. log_error("gdlm_punlock: vfs unlock error %x,%llx",
  169. name->ln_type, (unsigned long long)name->ln_number);
  170. op->info.optype = GDLM_PLOCK_OP_UNLOCK;
  171. op->info.pid = fl->fl_pid;
  172. op->info.fsid = ls->id;
  173. op->info.number = name->ln_number;
  174. op->info.start = fl->fl_start;
  175. op->info.end = fl->fl_end;
  176. op->info.owner = (__u64)(long) fl->fl_owner;
  177. send_op(op);
  178. wait_event(recv_wq, (op->done != 0));
  179. spin_lock(&ops_lock);
  180. if (!list_empty(&op->list)) {
  181. printk(KERN_INFO "punlock op on list\n");
  182. list_del(&op->list);
  183. }
  184. spin_unlock(&ops_lock);
  185. rv = op->info.rv;
  186. if (rv == -ENOENT)
  187. rv = 0;
  188. kfree(op);
  189. return rv;
  190. }
  191. int gdlm_plock_get(void *lockspace, struct lm_lockname *name,
  192. struct file *file, struct file_lock *fl)
  193. {
  194. struct gdlm_ls *ls = lockspace;
  195. struct plock_op *op;
  196. int rv;
  197. op = kzalloc(sizeof(*op), GFP_KERNEL);
  198. if (!op)
  199. return -ENOMEM;
  200. op->info.optype = GDLM_PLOCK_OP_GET;
  201. op->info.pid = fl->fl_pid;
  202. op->info.ex = (fl->fl_type == F_WRLCK);
  203. op->info.fsid = ls->id;
  204. op->info.number = name->ln_number;
  205. op->info.start = fl->fl_start;
  206. op->info.end = fl->fl_end;
  207. send_op(op);
  208. wait_event(recv_wq, (op->done != 0));
  209. spin_lock(&ops_lock);
  210. if (!list_empty(&op->list)) {
  211. printk(KERN_INFO "plock_get op on list\n");
  212. list_del(&op->list);
  213. }
  214. spin_unlock(&ops_lock);
  215. rv = op->info.rv;
  216. fl->fl_type = F_UNLCK;
  217. if (rv == -ENOENT)
  218. rv = 0;
  219. else if (rv == 0 && op->info.pid != fl->fl_pid) {
  220. fl->fl_type = (op->info.ex) ? F_WRLCK : F_RDLCK;
  221. fl->fl_pid = op->info.pid;
  222. fl->fl_start = op->info.start;
  223. fl->fl_end = op->info.end;
  224. }
  225. kfree(op);
  226. return rv;
  227. }
  228. /* a read copies out one plock request from the send list */
  229. static ssize_t dev_read(struct file *file, char __user *u, size_t count,
  230. loff_t *ppos)
  231. {
  232. struct gdlm_plock_info info;
  233. struct plock_op *op = NULL;
  234. if (count < sizeof(info))
  235. return -EINVAL;
  236. spin_lock(&ops_lock);
  237. if (!list_empty(&send_list)) {
  238. op = list_entry(send_list.next, struct plock_op, list);
  239. list_move(&op->list, &recv_list);
  240. memcpy(&info, &op->info, sizeof(info));
  241. }
  242. spin_unlock(&ops_lock);
  243. if (!op)
  244. return -EAGAIN;
  245. if (copy_to_user(u, &info, sizeof(info)))
  246. return -EFAULT;
  247. return sizeof(info);
  248. }
  249. /* a write copies in one plock result that should match a plock_op
  250. on the recv list */
  251. static ssize_t dev_write(struct file *file, const char __user *u, size_t count,
  252. loff_t *ppos)
  253. {
  254. struct gdlm_plock_info info;
  255. struct plock_op *op;
  256. int found = 0;
  257. if (count != sizeof(info))
  258. return -EINVAL;
  259. if (copy_from_user(&info, u, sizeof(info)))
  260. return -EFAULT;
  261. if (check_version(&info))
  262. return -EINVAL;
  263. spin_lock(&ops_lock);
  264. list_for_each_entry(op, &recv_list, list) {
  265. if (op->info.fsid == info.fsid && op->info.number == info.number &&
  266. op->info.owner == info.owner) {
  267. list_del_init(&op->list);
  268. found = 1;
  269. op->done = 1;
  270. memcpy(&op->info, &info, sizeof(info));
  271. break;
  272. }
  273. }
  274. spin_unlock(&ops_lock);
  275. if (found) {
  276. struct plock_xop *xop;
  277. xop = (struct plock_xop *)op;
  278. if (xop->callback)
  279. count = gdlm_plock_callback(op);
  280. else
  281. wake_up(&recv_wq);
  282. } else
  283. printk(KERN_INFO "gdlm dev_write no op %x %llx\n", info.fsid,
  284. (unsigned long long)info.number);
  285. return count;
  286. }
  287. static unsigned int dev_poll(struct file *file, poll_table *wait)
  288. {
  289. poll_wait(file, &send_wq, wait);
  290. spin_lock(&ops_lock);
  291. if (!list_empty(&send_list)) {
  292. spin_unlock(&ops_lock);
  293. return POLLIN | POLLRDNORM;
  294. }
  295. spin_unlock(&ops_lock);
  296. return 0;
  297. }
  298. static const struct file_operations dev_fops = {
  299. .read = dev_read,
  300. .write = dev_write,
  301. .poll = dev_poll,
  302. .owner = THIS_MODULE
  303. };
  304. static struct miscdevice plock_dev_misc = {
  305. .minor = MISC_DYNAMIC_MINOR,
  306. .name = GDLM_PLOCK_MISC_NAME,
  307. .fops = &dev_fops
  308. };
  309. int gdlm_plock_init(void)
  310. {
  311. int rv;
  312. spin_lock_init(&ops_lock);
  313. INIT_LIST_HEAD(&send_list);
  314. INIT_LIST_HEAD(&recv_list);
  315. init_waitqueue_head(&send_wq);
  316. init_waitqueue_head(&recv_wq);
  317. rv = misc_register(&plock_dev_misc);
  318. if (rv)
  319. printk(KERN_INFO "gdlm_plock_init: misc_register failed %d",
  320. rv);
  321. return rv;
  322. }
  323. void gdlm_plock_exit(void)
  324. {
  325. if (misc_deregister(&plock_dev_misc) < 0)
  326. printk(KERN_INFO "gdlm_plock_exit: misc_deregister failed");
  327. }