plock.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 v.2.
  7. */
  8. #include <linux/miscdevice.h>
  9. #include <linux/lock_dlm_plock.h>
  10. #include "lock_dlm.h"
  11. static spinlock_t ops_lock;
  12. static struct list_head send_list;
  13. static struct list_head recv_list;
  14. static wait_queue_head_t send_wq;
  15. static wait_queue_head_t recv_wq;
  16. struct plock_op {
  17. struct list_head list;
  18. int done;
  19. struct gdlm_plock_info info;
  20. };
  21. static inline void set_version(struct gdlm_plock_info *info)
  22. {
  23. info->version[0] = GDLM_PLOCK_VERSION_MAJOR;
  24. info->version[1] = GDLM_PLOCK_VERSION_MINOR;
  25. info->version[2] = GDLM_PLOCK_VERSION_PATCH;
  26. }
  27. static int check_version(struct gdlm_plock_info *info)
  28. {
  29. if ((GDLM_PLOCK_VERSION_MAJOR != info->version[0]) ||
  30. (GDLM_PLOCK_VERSION_MINOR < info->version[1])) {
  31. log_error("plock device version mismatch: "
  32. "kernel (%u.%u.%u), user (%u.%u.%u)",
  33. GDLM_PLOCK_VERSION_MAJOR,
  34. GDLM_PLOCK_VERSION_MINOR,
  35. GDLM_PLOCK_VERSION_PATCH,
  36. info->version[0],
  37. info->version[1],
  38. info->version[2]);
  39. return -EINVAL;
  40. }
  41. return 0;
  42. }
  43. static void send_op(struct plock_op *op)
  44. {
  45. set_version(&op->info);
  46. INIT_LIST_HEAD(&op->list);
  47. spin_lock(&ops_lock);
  48. list_add_tail(&op->list, &send_list);
  49. spin_unlock(&ops_lock);
  50. wake_up(&send_wq);
  51. }
  52. int gdlm_plock(lm_lockspace_t *lockspace, struct lm_lockname *name,
  53. struct file *file, int cmd, struct file_lock *fl)
  54. {
  55. struct gdlm_ls *ls = (struct gdlm_ls *) lockspace;
  56. struct plock_op *op;
  57. int rv;
  58. op = kzalloc(sizeof(*op), GFP_KERNEL);
  59. if (!op)
  60. return -ENOMEM;
  61. op->info.optype = GDLM_PLOCK_OP_LOCK;
  62. op->info.pid = (uint32_t) fl->fl_owner;
  63. op->info.ex = (fl->fl_type == F_WRLCK);
  64. op->info.wait = IS_SETLKW(cmd);
  65. op->info.fsid = ls->id;
  66. op->info.number = name->ln_number;
  67. op->info.start = fl->fl_start;
  68. op->info.end = fl->fl_end;
  69. send_op(op);
  70. wait_event(recv_wq, (op->done != 0));
  71. spin_lock(&ops_lock);
  72. if (!list_empty(&op->list)) {
  73. printk("plock op on list\n");
  74. list_del(&op->list);
  75. }
  76. spin_unlock(&ops_lock);
  77. rv = op->info.rv;
  78. if (!rv) {
  79. if (posix_lock_file_wait(file, fl) < 0)
  80. log_error("gdlm_plock: vfs lock error %x,%llx",
  81. name->ln_type, name->ln_number);
  82. }
  83. kfree(op);
  84. return rv;
  85. }
  86. int gdlm_punlock(lm_lockspace_t *lockspace, struct lm_lockname *name,
  87. struct file *file, struct file_lock *fl)
  88. {
  89. struct gdlm_ls *ls = (struct gdlm_ls *) lockspace;
  90. struct plock_op *op;
  91. int rv;
  92. op = kzalloc(sizeof(*op), GFP_KERNEL);
  93. if (!op)
  94. return -ENOMEM;
  95. if (posix_lock_file_wait(file, fl) < 0)
  96. log_error("gdlm_punlock: vfs unlock error %x,%llx",
  97. name->ln_type, name->ln_number);
  98. op->info.optype = GDLM_PLOCK_OP_UNLOCK;
  99. op->info.pid = (uint32_t) fl->fl_owner;
  100. op->info.fsid = ls->id;
  101. op->info.number = name->ln_number;
  102. op->info.start = fl->fl_start;
  103. op->info.end = fl->fl_end;
  104. send_op(op);
  105. wait_event(recv_wq, (op->done != 0));
  106. spin_lock(&ops_lock);
  107. if (!list_empty(&op->list)) {
  108. printk("punlock op on list\n");
  109. list_del(&op->list);
  110. }
  111. spin_unlock(&ops_lock);
  112. rv = op->info.rv;
  113. kfree(op);
  114. return rv;
  115. }
  116. int gdlm_plock_get(lm_lockspace_t *lockspace, struct lm_lockname *name,
  117. struct file *file, struct file_lock *fl)
  118. {
  119. struct gdlm_ls *ls = (struct gdlm_ls *) lockspace;
  120. struct plock_op *op;
  121. int rv;
  122. op = kzalloc(sizeof(*op), GFP_KERNEL);
  123. if (!op)
  124. return -ENOMEM;
  125. op->info.optype = GDLM_PLOCK_OP_GET;
  126. op->info.pid = (uint32_t) fl->fl_owner;
  127. op->info.ex = (fl->fl_type == F_WRLCK);
  128. op->info.fsid = ls->id;
  129. op->info.number = name->ln_number;
  130. op->info.start = fl->fl_start;
  131. op->info.end = fl->fl_end;
  132. send_op(op);
  133. wait_event(recv_wq, (op->done != 0));
  134. spin_lock(&ops_lock);
  135. if (!list_empty(&op->list)) {
  136. printk("plock_get op on list\n");
  137. list_del(&op->list);
  138. }
  139. spin_unlock(&ops_lock);
  140. rv = op->info.rv;
  141. if (rv == 0)
  142. fl->fl_type = F_UNLCK;
  143. else if (rv > 0) {
  144. fl->fl_type = (op->info.ex) ? F_WRLCK : F_RDLCK;
  145. fl->fl_pid = op->info.pid;
  146. fl->fl_start = op->info.start;
  147. fl->fl_end = op->info.end;
  148. }
  149. kfree(op);
  150. return rv;
  151. }
  152. /* a read copies out one plock request from the send list */
  153. static ssize_t dev_read(struct file *file, char __user *u, size_t count,
  154. loff_t *ppos)
  155. {
  156. struct gdlm_plock_info info;
  157. struct plock_op *op = NULL;
  158. if (count < sizeof(info))
  159. return -EINVAL;
  160. spin_lock(&ops_lock);
  161. if (!list_empty(&send_list)) {
  162. op = list_entry(send_list.next, struct plock_op, list);
  163. list_move(&op->list, &recv_list);
  164. memcpy(&info, &op->info, sizeof(info));
  165. }
  166. spin_unlock(&ops_lock);
  167. if (!op)
  168. return -EAGAIN;
  169. if (copy_to_user(u, &info, sizeof(info)))
  170. return -EFAULT;
  171. return sizeof(info);
  172. }
  173. /* a write copies in one plock result that should match a plock_op
  174. on the recv list */
  175. static ssize_t dev_write(struct file *file, const char __user *u, size_t count,
  176. loff_t *ppos)
  177. {
  178. struct gdlm_plock_info info;
  179. struct plock_op *op;
  180. int found = 0;
  181. if (count != sizeof(info))
  182. return -EINVAL;
  183. if (copy_from_user(&info, u, sizeof(info)))
  184. return -EFAULT;
  185. if (check_version(&info))
  186. return -EINVAL;
  187. spin_lock(&ops_lock);
  188. list_for_each_entry(op, &recv_list, list) {
  189. if (op->info.fsid == info.fsid &&
  190. op->info.number == info.number) {
  191. list_del_init(&op->list);
  192. found = 1;
  193. op->done = 1;
  194. memcpy(&op->info, &info, sizeof(info));
  195. break;
  196. }
  197. }
  198. spin_unlock(&ops_lock);
  199. if (found)
  200. wake_up(&recv_wq);
  201. else
  202. printk("gdlm dev_write no op %x %llx\n", info.fsid,
  203. info.number);
  204. return count;
  205. }
  206. static unsigned int dev_poll(struct file *file, poll_table *wait)
  207. {
  208. poll_wait(file, &send_wq, wait);
  209. spin_lock(&ops_lock);
  210. if (!list_empty(&send_list)) {
  211. spin_unlock(&ops_lock);
  212. return POLLIN | POLLRDNORM;
  213. }
  214. spin_unlock(&ops_lock);
  215. return 0;
  216. }
  217. static struct file_operations dev_fops = {
  218. .read = dev_read,
  219. .write = dev_write,
  220. .poll = dev_poll,
  221. .owner = THIS_MODULE
  222. };
  223. static struct miscdevice plock_dev_misc = {
  224. .minor = MISC_DYNAMIC_MINOR,
  225. .name = GDLM_PLOCK_MISC_NAME,
  226. .fops = &dev_fops
  227. };
  228. int gdlm_plock_init(void)
  229. {
  230. int rv;
  231. spin_lock_init(&ops_lock);
  232. INIT_LIST_HEAD(&send_list);
  233. INIT_LIST_HEAD(&recv_list);
  234. init_waitqueue_head(&send_wq);
  235. init_waitqueue_head(&recv_wq);
  236. rv = misc_register(&plock_dev_misc);
  237. if (rv)
  238. printk("gdlm_plock_init: misc_register failed %d", rv);
  239. return rv;
  240. }
  241. void gdlm_plock_exit(void)
  242. {
  243. if (misc_deregister(&plock_dev_misc) < 0)
  244. printk("gdlm_plock_exit: misc_deregister failed");
  245. }