plock.c 6.6 KB

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