clntlock.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * linux/fs/lockd/clntlock.c
  3. *
  4. * Lock handling for the client side NLM implementation
  5. *
  6. * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/types.h>
  10. #include <linux/time.h>
  11. #include <linux/nfs_fs.h>
  12. #include <linux/sunrpc/clnt.h>
  13. #include <linux/sunrpc/svc.h>
  14. #include <linux/lockd/lockd.h>
  15. #include <linux/smp_lock.h>
  16. #define NLMDBG_FACILITY NLMDBG_CLIENT
  17. /*
  18. * Local function prototypes
  19. */
  20. static int reclaimer(void *ptr);
  21. /*
  22. * The following functions handle blocking and granting from the
  23. * client perspective.
  24. */
  25. /*
  26. * This is the representation of a blocked client lock.
  27. */
  28. struct nlm_wait {
  29. struct list_head b_list; /* linked list */
  30. wait_queue_head_t b_wait; /* where to wait on */
  31. struct nlm_host * b_host;
  32. struct file_lock * b_lock; /* local file lock */
  33. unsigned short b_reclaim; /* got to reclaim lock */
  34. u32 b_status; /* grant callback status */
  35. };
  36. static LIST_HEAD(nlm_blocked);
  37. /*
  38. * Queue up a lock for blocking so that the GRANTED request can see it
  39. */
  40. int nlmclnt_prepare_block(struct nlm_rqst *req, struct nlm_host *host, struct file_lock *fl)
  41. {
  42. struct nlm_wait *block;
  43. BUG_ON(req->a_block != NULL);
  44. block = kmalloc(sizeof(*block), GFP_KERNEL);
  45. if (block == NULL)
  46. return -ENOMEM;
  47. block->b_host = host;
  48. block->b_lock = fl;
  49. init_waitqueue_head(&block->b_wait);
  50. block->b_status = NLM_LCK_BLOCKED;
  51. list_add(&block->b_list, &nlm_blocked);
  52. req->a_block = block;
  53. return 0;
  54. }
  55. void nlmclnt_finish_block(struct nlm_rqst *req)
  56. {
  57. struct nlm_wait *block = req->a_block;
  58. if (block == NULL)
  59. return;
  60. req->a_block = NULL;
  61. list_del(&block->b_list);
  62. kfree(block);
  63. }
  64. /*
  65. * Block on a lock
  66. */
  67. long nlmclnt_block(struct nlm_rqst *req, long timeout)
  68. {
  69. struct nlm_wait *block = req->a_block;
  70. long ret;
  71. /* A borken server might ask us to block even if we didn't
  72. * request it. Just say no!
  73. */
  74. if (!req->a_args.block)
  75. return -EAGAIN;
  76. /* Go to sleep waiting for GRANT callback. Some servers seem
  77. * to lose callbacks, however, so we're going to poll from
  78. * time to time just to make sure.
  79. *
  80. * For now, the retry frequency is pretty high; normally
  81. * a 1 minute timeout would do. See the comment before
  82. * nlmclnt_lock for an explanation.
  83. */
  84. ret = wait_event_interruptible_timeout(block->b_wait,
  85. block->b_status != NLM_LCK_BLOCKED,
  86. timeout);
  87. if (block->b_status != NLM_LCK_BLOCKED) {
  88. req->a_res.status = block->b_status;
  89. block->b_status = NLM_LCK_BLOCKED;
  90. }
  91. return ret;
  92. }
  93. /*
  94. * The server lockd has called us back to tell us the lock was granted
  95. */
  96. u32 nlmclnt_grant(const struct sockaddr_in *addr, const struct nlm_lock *lock)
  97. {
  98. const struct file_lock *fl = &lock->fl;
  99. const struct nfs_fh *fh = &lock->fh;
  100. struct nlm_wait *block;
  101. u32 res = nlm_lck_denied;
  102. /*
  103. * Look up blocked request based on arguments.
  104. * Warning: must not use cookie to match it!
  105. */
  106. list_for_each_entry(block, &nlm_blocked, b_list) {
  107. struct file_lock *fl_blocked = block->b_lock;
  108. if (fl_blocked->fl_start != fl->fl_start)
  109. continue;
  110. if (fl_blocked->fl_end != fl->fl_end)
  111. continue;
  112. /*
  113. * Careful! The NLM server will return the 32-bit "pid" that
  114. * we put on the wire: in this case the lockowner "pid".
  115. */
  116. if (fl_blocked->fl_u.nfs_fl.owner->pid != lock->svid)
  117. continue;
  118. if (!nlm_cmp_addr(&block->b_host->h_addr, addr))
  119. continue;
  120. if (nfs_compare_fh(NFS_FH(fl_blocked->fl_file->f_dentry->d_inode) ,fh) != 0)
  121. continue;
  122. /* Alright, we found a lock. Set the return status
  123. * and wake up the caller
  124. */
  125. block->b_status = NLM_LCK_GRANTED;
  126. wake_up(&block->b_wait);
  127. res = nlm_granted;
  128. }
  129. return res;
  130. }
  131. /*
  132. * The following procedures deal with the recovery of locks after a
  133. * server crash.
  134. */
  135. /*
  136. * Mark the locks for reclaiming.
  137. * FIXME: In 2.5 we don't want to iterate through any global file_lock_list.
  138. * Maintain NLM lock reclaiming lists in the nlm_host instead.
  139. */
  140. static
  141. void nlmclnt_mark_reclaim(struct nlm_host *host)
  142. {
  143. struct file_lock *fl;
  144. struct inode *inode;
  145. struct list_head *tmp;
  146. list_for_each(tmp, &file_lock_list) {
  147. fl = list_entry(tmp, struct file_lock, fl_link);
  148. inode = fl->fl_file->f_dentry->d_inode;
  149. if (inode->i_sb->s_magic != NFS_SUPER_MAGIC)
  150. continue;
  151. if (fl->fl_u.nfs_fl.owner == NULL)
  152. continue;
  153. if (fl->fl_u.nfs_fl.owner->host != host)
  154. continue;
  155. if (!(fl->fl_u.nfs_fl.flags & NFS_LCK_GRANTED))
  156. continue;
  157. fl->fl_u.nfs_fl.flags |= NFS_LCK_RECLAIM;
  158. }
  159. }
  160. /*
  161. * Someone has sent us an SM_NOTIFY. Ensure we bind to the new port number,
  162. * that we mark locks for reclaiming, and that we bump the pseudo NSM state.
  163. */
  164. static inline
  165. void nlmclnt_prepare_reclaim(struct nlm_host *host, u32 newstate)
  166. {
  167. host->h_monitored = 0;
  168. host->h_nsmstate = newstate;
  169. host->h_state++;
  170. host->h_nextrebind = 0;
  171. nlm_rebind_host(host);
  172. nlmclnt_mark_reclaim(host);
  173. dprintk("NLM: reclaiming locks for host %s", host->h_name);
  174. }
  175. /*
  176. * Reclaim all locks on server host. We do this by spawning a separate
  177. * reclaimer thread.
  178. */
  179. void
  180. nlmclnt_recovery(struct nlm_host *host, u32 newstate)
  181. {
  182. if (host->h_reclaiming++) {
  183. if (host->h_nsmstate == newstate)
  184. return;
  185. nlmclnt_prepare_reclaim(host, newstate);
  186. } else {
  187. nlmclnt_prepare_reclaim(host, newstate);
  188. nlm_get_host(host);
  189. __module_get(THIS_MODULE);
  190. if (kernel_thread(reclaimer, host, CLONE_KERNEL) < 0)
  191. module_put(THIS_MODULE);
  192. }
  193. }
  194. static int
  195. reclaimer(void *ptr)
  196. {
  197. struct nlm_host *host = (struct nlm_host *) ptr;
  198. struct nlm_wait *block;
  199. struct list_head *tmp;
  200. struct file_lock *fl;
  201. struct inode *inode;
  202. daemonize("%s-reclaim", host->h_name);
  203. allow_signal(SIGKILL);
  204. /* This one ensures that our parent doesn't terminate while the
  205. * reclaim is in progress */
  206. lock_kernel();
  207. lockd_up();
  208. /* First, reclaim all locks that have been marked. */
  209. restart:
  210. list_for_each(tmp, &file_lock_list) {
  211. fl = list_entry(tmp, struct file_lock, fl_link);
  212. inode = fl->fl_file->f_dentry->d_inode;
  213. if (inode->i_sb->s_magic != NFS_SUPER_MAGIC)
  214. continue;
  215. if (fl->fl_u.nfs_fl.owner == NULL)
  216. continue;
  217. if (fl->fl_u.nfs_fl.owner->host != host)
  218. continue;
  219. if (!(fl->fl_u.nfs_fl.flags & NFS_LCK_RECLAIM))
  220. continue;
  221. fl->fl_u.nfs_fl.flags &= ~NFS_LCK_RECLAIM;
  222. nlmclnt_reclaim(host, fl);
  223. if (signalled())
  224. break;
  225. goto restart;
  226. }
  227. host->h_reclaiming = 0;
  228. /* Now, wake up all processes that sleep on a blocked lock */
  229. list_for_each_entry(block, &nlm_blocked, b_list) {
  230. if (block->b_host == host) {
  231. block->b_status = NLM_LCK_DENIED_GRACE_PERIOD;
  232. wake_up(&block->b_wait);
  233. }
  234. }
  235. /* Release host handle after use */
  236. nlm_release_host(host);
  237. lockd_down();
  238. unlock_kernel();
  239. module_put_and_exit(0);
  240. }