clntlock.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. struct nlm_wait *nlmclnt_prepare_block(struct nlm_host *host, struct file_lock *fl)
  41. {
  42. struct nlm_wait *block;
  43. block = kmalloc(sizeof(*block), GFP_KERNEL);
  44. if (block != NULL) {
  45. block->b_host = host;
  46. block->b_lock = fl;
  47. init_waitqueue_head(&block->b_wait);
  48. block->b_status = NLM_LCK_BLOCKED;
  49. list_add(&block->b_list, &nlm_blocked);
  50. }
  51. return block;
  52. }
  53. void nlmclnt_finish_block(struct nlm_wait *block)
  54. {
  55. if (block == NULL)
  56. return;
  57. list_del(&block->b_list);
  58. kfree(block);
  59. }
  60. /*
  61. * Block on a lock
  62. */
  63. int nlmclnt_block(struct nlm_wait *block, struct nlm_rqst *req, long timeout)
  64. {
  65. long ret;
  66. /* A borken server might ask us to block even if we didn't
  67. * request it. Just say no!
  68. */
  69. if (block == NULL)
  70. return -EAGAIN;
  71. /* Go to sleep waiting for GRANT callback. Some servers seem
  72. * to lose callbacks, however, so we're going to poll from
  73. * time to time just to make sure.
  74. *
  75. * For now, the retry frequency is pretty high; normally
  76. * a 1 minute timeout would do. See the comment before
  77. * nlmclnt_lock for an explanation.
  78. */
  79. ret = wait_event_interruptible_timeout(block->b_wait,
  80. block->b_status != NLM_LCK_BLOCKED,
  81. timeout);
  82. if (ret < 0)
  83. return -ERESTARTSYS;
  84. req->a_res.status = block->b_status;
  85. return 0;
  86. }
  87. /*
  88. * The server lockd has called us back to tell us the lock was granted
  89. */
  90. u32 nlmclnt_grant(const struct sockaddr_in *addr, const struct nlm_lock *lock)
  91. {
  92. const struct file_lock *fl = &lock->fl;
  93. const struct nfs_fh *fh = &lock->fh;
  94. struct nlm_wait *block;
  95. u32 res = nlm_lck_denied;
  96. /*
  97. * Look up blocked request based on arguments.
  98. * Warning: must not use cookie to match it!
  99. */
  100. list_for_each_entry(block, &nlm_blocked, b_list) {
  101. struct file_lock *fl_blocked = block->b_lock;
  102. if (fl_blocked->fl_start != fl->fl_start)
  103. continue;
  104. if (fl_blocked->fl_end != fl->fl_end)
  105. continue;
  106. /*
  107. * Careful! The NLM server will return the 32-bit "pid" that
  108. * we put on the wire: in this case the lockowner "pid".
  109. */
  110. if (fl_blocked->fl_u.nfs_fl.owner->pid != lock->svid)
  111. continue;
  112. if (!nlm_cmp_addr(&block->b_host->h_addr, addr))
  113. continue;
  114. if (nfs_compare_fh(NFS_FH(fl_blocked->fl_file->f_dentry->d_inode) ,fh) != 0)
  115. continue;
  116. /* Alright, we found a lock. Set the return status
  117. * and wake up the caller
  118. */
  119. block->b_status = NLM_LCK_GRANTED;
  120. wake_up(&block->b_wait);
  121. res = nlm_granted;
  122. }
  123. return res;
  124. }
  125. /*
  126. * The following procedures deal with the recovery of locks after a
  127. * server crash.
  128. */
  129. /*
  130. * Someone has sent us an SM_NOTIFY. Ensure we bind to the new port number,
  131. * that we mark locks for reclaiming, and that we bump the pseudo NSM state.
  132. */
  133. static void nlmclnt_prepare_reclaim(struct nlm_host *host)
  134. {
  135. down_write(&host->h_rwsem);
  136. host->h_monitored = 0;
  137. host->h_state++;
  138. host->h_nextrebind = 0;
  139. nlm_rebind_host(host);
  140. /*
  141. * Mark the locks for reclaiming.
  142. */
  143. list_splice_init(&host->h_granted, &host->h_reclaim);
  144. dprintk("NLM: reclaiming locks for host %s\n", host->h_name);
  145. }
  146. static void nlmclnt_finish_reclaim(struct nlm_host *host)
  147. {
  148. host->h_reclaiming = 0;
  149. up_write(&host->h_rwsem);
  150. dprintk("NLM: done reclaiming locks for host %s", host->h_name);
  151. }
  152. /*
  153. * Reclaim all locks on server host. We do this by spawning a separate
  154. * reclaimer thread.
  155. */
  156. void
  157. nlmclnt_recovery(struct nlm_host *host, u32 newstate)
  158. {
  159. if (host->h_nsmstate == newstate)
  160. return;
  161. host->h_nsmstate = newstate;
  162. if (!host->h_reclaiming++) {
  163. nlm_get_host(host);
  164. __module_get(THIS_MODULE);
  165. if (kernel_thread(reclaimer, host, CLONE_KERNEL) < 0)
  166. module_put(THIS_MODULE);
  167. }
  168. }
  169. static int
  170. reclaimer(void *ptr)
  171. {
  172. struct nlm_host *host = (struct nlm_host *) ptr;
  173. struct nlm_wait *block;
  174. struct file_lock *fl, *next;
  175. u32 nsmstate;
  176. daemonize("%s-reclaim", host->h_name);
  177. allow_signal(SIGKILL);
  178. /* This one ensures that our parent doesn't terminate while the
  179. * reclaim is in progress */
  180. lock_kernel();
  181. lockd_up();
  182. nlmclnt_prepare_reclaim(host);
  183. /* First, reclaim all locks that have been marked. */
  184. restart:
  185. nsmstate = host->h_nsmstate;
  186. list_for_each_entry_safe(fl, next, &host->h_reclaim, fl_u.nfs_fl.list) {
  187. list_del_init(&fl->fl_u.nfs_fl.list);
  188. if (signalled())
  189. continue;
  190. if (nlmclnt_reclaim(host, fl) != 0)
  191. continue;
  192. list_add_tail(&fl->fl_u.nfs_fl.list, &host->h_granted);
  193. if (host->h_nsmstate != nsmstate) {
  194. /* Argh! The server rebooted again! */
  195. list_splice_init(&host->h_granted, &host->h_reclaim);
  196. goto restart;
  197. }
  198. }
  199. nlmclnt_finish_reclaim(host);
  200. /* Now, wake up all processes that sleep on a blocked lock */
  201. list_for_each_entry(block, &nlm_blocked, b_list) {
  202. if (block->b_host == host) {
  203. block->b_status = NLM_LCK_DENIED_GRACE_PERIOD;
  204. wake_up(&block->b_wait);
  205. }
  206. }
  207. /* Release host handle after use */
  208. nlm_release_host(host);
  209. lockd_down();
  210. unlock_kernel();
  211. module_put_and_exit(0);
  212. }