plock.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. op->info.owner = (__u64)(long) fl->fl_owner;
  208. send_op(op);
  209. wait_event(recv_wq, (op->done != 0));
  210. spin_lock(&ops_lock);
  211. if (!list_empty(&op->list)) {
  212. printk(KERN_INFO "plock_get op on list\n");
  213. list_del(&op->list);
  214. }
  215. spin_unlock(&ops_lock);
  216. /* info.rv from userspace is 1 for conflict, 0 for no-conflict,
  217. -ENOENT if there are no locks on the file */
  218. rv = op->info.rv;
  219. fl->fl_type = F_UNLCK;
  220. if (rv == -ENOENT)
  221. rv = 0;
  222. else if (rv > 0) {
  223. fl->fl_type = (op->info.ex) ? F_WRLCK : F_RDLCK;
  224. fl->fl_pid = op->info.pid;
  225. fl->fl_start = op->info.start;
  226. fl->fl_end = op->info.end;
  227. rv = 0;
  228. }
  229. kfree(op);
  230. return rv;
  231. }
  232. /* a read copies out one plock request from the send list */
  233. static ssize_t dev_read(struct file *file, char __user *u, size_t count,
  234. loff_t *ppos)
  235. {
  236. struct gdlm_plock_info info;
  237. struct plock_op *op = NULL;
  238. if (count < sizeof(info))
  239. return -EINVAL;
  240. spin_lock(&ops_lock);
  241. if (!list_empty(&send_list)) {
  242. op = list_entry(send_list.next, struct plock_op, list);
  243. list_move(&op->list, &recv_list);
  244. memcpy(&info, &op->info, sizeof(info));
  245. }
  246. spin_unlock(&ops_lock);
  247. if (!op)
  248. return -EAGAIN;
  249. if (copy_to_user(u, &info, sizeof(info)))
  250. return -EFAULT;
  251. return sizeof(info);
  252. }
  253. /* a write copies in one plock result that should match a plock_op
  254. on the recv list */
  255. static ssize_t dev_write(struct file *file, const char __user *u, size_t count,
  256. loff_t *ppos)
  257. {
  258. struct gdlm_plock_info info;
  259. struct plock_op *op;
  260. int found = 0;
  261. if (count != sizeof(info))
  262. return -EINVAL;
  263. if (copy_from_user(&info, u, sizeof(info)))
  264. return -EFAULT;
  265. if (check_version(&info))
  266. return -EINVAL;
  267. spin_lock(&ops_lock);
  268. list_for_each_entry(op, &recv_list, list) {
  269. if (op->info.fsid == info.fsid && op->info.number == info.number &&
  270. op->info.owner == info.owner) {
  271. list_del_init(&op->list);
  272. found = 1;
  273. op->done = 1;
  274. memcpy(&op->info, &info, sizeof(info));
  275. break;
  276. }
  277. }
  278. spin_unlock(&ops_lock);
  279. if (found) {
  280. struct plock_xop *xop;
  281. xop = (struct plock_xop *)op;
  282. if (xop->callback)
  283. count = gdlm_plock_callback(op);
  284. else
  285. wake_up(&recv_wq);
  286. } else
  287. printk(KERN_INFO "gdlm dev_write no op %x %llx\n", info.fsid,
  288. (unsigned long long)info.number);
  289. return count;
  290. }
  291. static unsigned int dev_poll(struct file *file, poll_table *wait)
  292. {
  293. unsigned int mask = 0;
  294. poll_wait(file, &send_wq, wait);
  295. spin_lock(&ops_lock);
  296. if (!list_empty(&send_list))
  297. mask = POLLIN | POLLRDNORM;
  298. spin_unlock(&ops_lock);
  299. return mask;
  300. }
  301. static const struct file_operations dev_fops = {
  302. .read = dev_read,
  303. .write = dev_write,
  304. .poll = dev_poll,
  305. .owner = THIS_MODULE
  306. };
  307. static struct miscdevice plock_dev_misc = {
  308. .minor = MISC_DYNAMIC_MINOR,
  309. .name = GDLM_PLOCK_MISC_NAME,
  310. .fops = &dev_fops
  311. };
  312. int gdlm_plock_init(void)
  313. {
  314. int rv;
  315. spin_lock_init(&ops_lock);
  316. INIT_LIST_HEAD(&send_list);
  317. INIT_LIST_HEAD(&recv_list);
  318. init_waitqueue_head(&send_wq);
  319. init_waitqueue_head(&recv_wq);
  320. rv = misc_register(&plock_dev_misc);
  321. if (rv)
  322. printk(KERN_INFO "gdlm_plock_init: misc_register failed %d",
  323. rv);
  324. return rv;
  325. }
  326. void gdlm_plock_exit(void)
  327. {
  328. if (misc_deregister(&plock_dev_misc) < 0)
  329. printk(KERN_INFO "gdlm_plock_exit: misc_deregister failed");
  330. }