clntlock.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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
  97. nlmclnt_grant(struct nlm_lock *lock)
  98. {
  99. struct nlm_wait *block;
  100. u32 res = nlm_lck_denied;
  101. /*
  102. * Look up blocked request based on arguments.
  103. * Warning: must not use cookie to match it!
  104. */
  105. list_for_each_entry(block, &nlm_blocked, b_list) {
  106. if (nlm_compare_locks(block->b_lock, &lock->fl)) {
  107. /* Alright, we found a lock. Set the return status
  108. * and wake up the caller
  109. */
  110. block->b_status = NLM_LCK_GRANTED;
  111. wake_up(&block->b_wait);
  112. res = nlm_granted;
  113. }
  114. }
  115. return res;
  116. }
  117. /*
  118. * The following procedures deal with the recovery of locks after a
  119. * server crash.
  120. */
  121. /*
  122. * Mark the locks for reclaiming.
  123. * FIXME: In 2.5 we don't want to iterate through any global file_lock_list.
  124. * Maintain NLM lock reclaiming lists in the nlm_host instead.
  125. */
  126. static
  127. void nlmclnt_mark_reclaim(struct nlm_host *host)
  128. {
  129. struct file_lock *fl;
  130. struct inode *inode;
  131. struct list_head *tmp;
  132. list_for_each(tmp, &file_lock_list) {
  133. fl = list_entry(tmp, struct file_lock, fl_link);
  134. inode = fl->fl_file->f_dentry->d_inode;
  135. if (inode->i_sb->s_magic != NFS_SUPER_MAGIC)
  136. continue;
  137. if (fl->fl_u.nfs_fl.owner->host != host)
  138. continue;
  139. if (!(fl->fl_u.nfs_fl.flags & NFS_LCK_GRANTED))
  140. continue;
  141. fl->fl_u.nfs_fl.flags |= NFS_LCK_RECLAIM;
  142. }
  143. }
  144. /*
  145. * Someone has sent us an SM_NOTIFY. Ensure we bind to the new port number,
  146. * that we mark locks for reclaiming, and that we bump the pseudo NSM state.
  147. */
  148. static inline
  149. void nlmclnt_prepare_reclaim(struct nlm_host *host, u32 newstate)
  150. {
  151. host->h_monitored = 0;
  152. host->h_nsmstate = newstate;
  153. host->h_state++;
  154. host->h_nextrebind = 0;
  155. nlm_rebind_host(host);
  156. nlmclnt_mark_reclaim(host);
  157. dprintk("NLM: reclaiming locks for host %s", host->h_name);
  158. }
  159. /*
  160. * Reclaim all locks on server host. We do this by spawning a separate
  161. * reclaimer thread.
  162. */
  163. void
  164. nlmclnt_recovery(struct nlm_host *host, u32 newstate)
  165. {
  166. if (host->h_reclaiming++) {
  167. if (host->h_nsmstate == newstate)
  168. return;
  169. nlmclnt_prepare_reclaim(host, newstate);
  170. } else {
  171. nlmclnt_prepare_reclaim(host, newstate);
  172. nlm_get_host(host);
  173. __module_get(THIS_MODULE);
  174. if (kernel_thread(reclaimer, host, CLONE_KERNEL) < 0)
  175. module_put(THIS_MODULE);
  176. }
  177. }
  178. static int
  179. reclaimer(void *ptr)
  180. {
  181. struct nlm_host *host = (struct nlm_host *) ptr;
  182. struct nlm_wait *block;
  183. struct list_head *tmp;
  184. struct file_lock *fl;
  185. struct inode *inode;
  186. daemonize("%s-reclaim", host->h_name);
  187. allow_signal(SIGKILL);
  188. /* This one ensures that our parent doesn't terminate while the
  189. * reclaim is in progress */
  190. lock_kernel();
  191. lockd_up();
  192. /* First, reclaim all locks that have been marked. */
  193. restart:
  194. list_for_each(tmp, &file_lock_list) {
  195. fl = list_entry(tmp, struct file_lock, fl_link);
  196. inode = fl->fl_file->f_dentry->d_inode;
  197. if (inode->i_sb->s_magic != NFS_SUPER_MAGIC)
  198. continue;
  199. if (fl->fl_u.nfs_fl.owner->host != host)
  200. continue;
  201. if (!(fl->fl_u.nfs_fl.flags & NFS_LCK_RECLAIM))
  202. continue;
  203. fl->fl_u.nfs_fl.flags &= ~NFS_LCK_RECLAIM;
  204. nlmclnt_reclaim(host, fl);
  205. if (signalled())
  206. break;
  207. goto restart;
  208. }
  209. host->h_reclaiming = 0;
  210. /* Now, wake up all processes that sleep on a blocked lock */
  211. list_for_each_entry(block, &nlm_blocked, b_list) {
  212. if (block->b_host == host) {
  213. block->b_status = NLM_LCK_DENIED_GRACE_PERIOD;
  214. wake_up(&block->b_wait);
  215. }
  216. }
  217. /* Release host handle after use */
  218. nlm_release_host(host);
  219. lockd_down();
  220. unlock_kernel();
  221. module_put_and_exit(0);
  222. }