plock.c 6.6 KB

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