plock.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 = fl->fl_pid;
  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(KERN_INFO "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,
  82. (unsigned long long)name->ln_number);
  83. }
  84. kfree(op);
  85. return rv;
  86. }
  87. int gdlm_punlock(lm_lockspace_t *lockspace, struct lm_lockname *name,
  88. struct file *file, struct file_lock *fl)
  89. {
  90. struct gdlm_ls *ls = (struct gdlm_ls *) lockspace;
  91. struct plock_op *op;
  92. int rv;
  93. op = kzalloc(sizeof(*op), GFP_KERNEL);
  94. if (!op)
  95. return -ENOMEM;
  96. if (posix_lock_file_wait(file, fl) < 0)
  97. log_error("gdlm_punlock: vfs unlock error %x,%llx",
  98. name->ln_type, (unsigned long long)name->ln_number);
  99. op->info.optype = GDLM_PLOCK_OP_UNLOCK;
  100. op->info.pid = fl->fl_pid;
  101. op->info.fsid = ls->id;
  102. op->info.number = name->ln_number;
  103. op->info.start = fl->fl_start;
  104. op->info.end = fl->fl_end;
  105. send_op(op);
  106. wait_event(recv_wq, (op->done != 0));
  107. spin_lock(&ops_lock);
  108. if (!list_empty(&op->list)) {
  109. printk(KERN_INFO "punlock op on list\n");
  110. list_del(&op->list);
  111. }
  112. spin_unlock(&ops_lock);
  113. rv = op->info.rv;
  114. kfree(op);
  115. return rv;
  116. }
  117. int gdlm_plock_get(lm_lockspace_t *lockspace, struct lm_lockname *name,
  118. struct file *file, struct file_lock *fl)
  119. {
  120. struct gdlm_ls *ls = (struct gdlm_ls *) lockspace;
  121. struct plock_op *op;
  122. int rv;
  123. op = kzalloc(sizeof(*op), GFP_KERNEL);
  124. if (!op)
  125. return -ENOMEM;
  126. op->info.optype = GDLM_PLOCK_OP_GET;
  127. op->info.pid = fl->fl_pid;
  128. op->info.ex = (fl->fl_type == F_WRLCK);
  129. op->info.fsid = ls->id;
  130. op->info.number = name->ln_number;
  131. op->info.start = fl->fl_start;
  132. op->info.end = fl->fl_end;
  133. send_op(op);
  134. wait_event(recv_wq, (op->done != 0));
  135. spin_lock(&ops_lock);
  136. if (!list_empty(&op->list)) {
  137. printk(KERN_INFO "plock_get op on list\n");
  138. list_del(&op->list);
  139. }
  140. spin_unlock(&ops_lock);
  141. rv = op->info.rv;
  142. if (rv == 0)
  143. fl->fl_type = F_UNLCK;
  144. else if (rv > 0) {
  145. fl->fl_type = (op->info.ex) ? F_WRLCK : F_RDLCK;
  146. fl->fl_pid = op->info.pid;
  147. fl->fl_start = op->info.start;
  148. fl->fl_end = op->info.end;
  149. }
  150. kfree(op);
  151. return rv;
  152. }
  153. /* a read copies out one plock request from the send list */
  154. static ssize_t dev_read(struct file *file, char __user *u, size_t count,
  155. loff_t *ppos)
  156. {
  157. struct gdlm_plock_info info;
  158. struct plock_op *op = NULL;
  159. if (count < sizeof(info))
  160. return -EINVAL;
  161. spin_lock(&ops_lock);
  162. if (!list_empty(&send_list)) {
  163. op = list_entry(send_list.next, struct plock_op, list);
  164. list_move(&op->list, &recv_list);
  165. memcpy(&info, &op->info, sizeof(info));
  166. }
  167. spin_unlock(&ops_lock);
  168. if (!op)
  169. return -EAGAIN;
  170. if (copy_to_user(u, &info, sizeof(info)))
  171. return -EFAULT;
  172. return sizeof(info);
  173. }
  174. /* a write copies in one plock result that should match a plock_op
  175. on the recv list */
  176. static ssize_t dev_write(struct file *file, const char __user *u, size_t count,
  177. loff_t *ppos)
  178. {
  179. struct gdlm_plock_info info;
  180. struct plock_op *op;
  181. int found = 0;
  182. if (count != sizeof(info))
  183. return -EINVAL;
  184. if (copy_from_user(&info, u, sizeof(info)))
  185. return -EFAULT;
  186. if (check_version(&info))
  187. return -EINVAL;
  188. spin_lock(&ops_lock);
  189. list_for_each_entry(op, &recv_list, list) {
  190. if (op->info.fsid == info.fsid &&
  191. op->info.number == info.number) {
  192. list_del_init(&op->list);
  193. found = 1;
  194. op->done = 1;
  195. memcpy(&op->info, &info, sizeof(info));
  196. break;
  197. }
  198. }
  199. spin_unlock(&ops_lock);
  200. if (found)
  201. wake_up(&recv_wq);
  202. else
  203. printk(KERN_INFO "gdlm dev_write no op %x %llx\n", info.fsid,
  204. (unsigned long long)info.number);
  205. return count;
  206. }
  207. static unsigned int dev_poll(struct file *file, poll_table *wait)
  208. {
  209. poll_wait(file, &send_wq, wait);
  210. spin_lock(&ops_lock);
  211. if (!list_empty(&send_list)) {
  212. spin_unlock(&ops_lock);
  213. return POLLIN | POLLRDNORM;
  214. }
  215. spin_unlock(&ops_lock);
  216. return 0;
  217. }
  218. static struct file_operations dev_fops = {
  219. .read = dev_read,
  220. .write = dev_write,
  221. .poll = dev_poll,
  222. .owner = THIS_MODULE
  223. };
  224. static struct miscdevice plock_dev_misc = {
  225. .minor = MISC_DYNAMIC_MINOR,
  226. .name = GDLM_PLOCK_MISC_NAME,
  227. .fops = &dev_fops
  228. };
  229. int gdlm_plock_init(void)
  230. {
  231. int rv;
  232. spin_lock_init(&ops_lock);
  233. INIT_LIST_HEAD(&send_list);
  234. INIT_LIST_HEAD(&recv_list);
  235. init_waitqueue_head(&send_wq);
  236. init_waitqueue_head(&recv_wq);
  237. rv = misc_register(&plock_dev_misc);
  238. if (rv)
  239. printk(KERN_INFO "gdlm_plock_init: misc_register failed %d",
  240. rv);
  241. return rv;
  242. }
  243. void gdlm_plock_exit(void)
  244. {
  245. if (misc_deregister(&plock_dev_misc) < 0)
  246. printk(KERN_INFO "gdlm_plock_exit: misc_deregister failed");
  247. }