clntlock.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. __be32 b_status; /* grant callback status */
  35. };
  36. static LIST_HEAD(nlm_blocked);
  37. /**
  38. * nlmclnt_init - Set up per-NFS mount point lockd data structures
  39. * @nlm_init: pointer to arguments structure
  40. *
  41. * Returns pointer to an appropriate nlm_host struct,
  42. * or an ERR_PTR value.
  43. */
  44. struct nlm_host *nlmclnt_init(const struct nlmclnt_initdata *nlm_init)
  45. {
  46. struct nlm_host *host;
  47. u32 nlm_version = (nlm_init->nfs_version == 2) ? 1 : 4;
  48. int status;
  49. status = lockd_up();
  50. if (status < 0)
  51. return ERR_PTR(status);
  52. host = nlmclnt_lookup_host(nlm_init->address, nlm_init->addrlen,
  53. nlm_init->protocol, nlm_version,
  54. nlm_init->hostname);
  55. if (host == NULL) {
  56. lockd_down();
  57. return ERR_PTR(-ENOLCK);
  58. }
  59. return host;
  60. }
  61. EXPORT_SYMBOL_GPL(nlmclnt_init);
  62. /**
  63. * nlmclnt_done - Release resources allocated by nlmclnt_init()
  64. * @host: nlm_host structure reserved by nlmclnt_init()
  65. *
  66. */
  67. void nlmclnt_done(struct nlm_host *host)
  68. {
  69. nlm_release_host(host);
  70. lockd_down();
  71. }
  72. EXPORT_SYMBOL_GPL(nlmclnt_done);
  73. /*
  74. * Queue up a lock for blocking so that the GRANTED request can see it
  75. */
  76. struct nlm_wait *nlmclnt_prepare_block(struct nlm_host *host, struct file_lock *fl)
  77. {
  78. struct nlm_wait *block;
  79. block = kmalloc(sizeof(*block), GFP_KERNEL);
  80. if (block != NULL) {
  81. block->b_host = host;
  82. block->b_lock = fl;
  83. init_waitqueue_head(&block->b_wait);
  84. block->b_status = nlm_lck_blocked;
  85. list_add(&block->b_list, &nlm_blocked);
  86. }
  87. return block;
  88. }
  89. void nlmclnt_finish_block(struct nlm_wait *block)
  90. {
  91. if (block == NULL)
  92. return;
  93. list_del(&block->b_list);
  94. kfree(block);
  95. }
  96. /*
  97. * Block on a lock
  98. */
  99. int nlmclnt_block(struct nlm_wait *block, struct nlm_rqst *req, long timeout)
  100. {
  101. long ret;
  102. /* A borken server might ask us to block even if we didn't
  103. * request it. Just say no!
  104. */
  105. if (block == NULL)
  106. return -EAGAIN;
  107. /* Go to sleep waiting for GRANT callback. Some servers seem
  108. * to lose callbacks, however, so we're going to poll from
  109. * time to time just to make sure.
  110. *
  111. * For now, the retry frequency is pretty high; normally
  112. * a 1 minute timeout would do. See the comment before
  113. * nlmclnt_lock for an explanation.
  114. */
  115. ret = wait_event_interruptible_timeout(block->b_wait,
  116. block->b_status != nlm_lck_blocked,
  117. timeout);
  118. if (ret < 0)
  119. return -ERESTARTSYS;
  120. req->a_res.status = block->b_status;
  121. return 0;
  122. }
  123. /*
  124. * The server lockd has called us back to tell us the lock was granted
  125. */
  126. __be32 nlmclnt_grant(const struct sockaddr *addr, const struct nlm_lock *lock)
  127. {
  128. const struct file_lock *fl = &lock->fl;
  129. const struct nfs_fh *fh = &lock->fh;
  130. struct nlm_wait *block;
  131. __be32 res = nlm_lck_denied;
  132. /*
  133. * Look up blocked request based on arguments.
  134. * Warning: must not use cookie to match it!
  135. */
  136. list_for_each_entry(block, &nlm_blocked, b_list) {
  137. struct file_lock *fl_blocked = block->b_lock;
  138. if (fl_blocked->fl_start != fl->fl_start)
  139. continue;
  140. if (fl_blocked->fl_end != fl->fl_end)
  141. continue;
  142. /*
  143. * Careful! The NLM server will return the 32-bit "pid" that
  144. * we put on the wire: in this case the lockowner "pid".
  145. */
  146. if (fl_blocked->fl_u.nfs_fl.owner->pid != lock->svid)
  147. continue;
  148. if (!nlm_cmp_addr(nlm_addr(block->b_host), addr))
  149. continue;
  150. if (nfs_compare_fh(NFS_FH(fl_blocked->fl_file->f_path.dentry->d_inode) ,fh) != 0)
  151. continue;
  152. /* Alright, we found a lock. Set the return status
  153. * and wake up the caller
  154. */
  155. block->b_status = nlm_granted;
  156. wake_up(&block->b_wait);
  157. res = nlm_granted;
  158. }
  159. return res;
  160. }
  161. /*
  162. * The following procedures deal with the recovery of locks after a
  163. * server crash.
  164. */
  165. /*
  166. * Reclaim all locks on server host. We do this by spawning a separate
  167. * reclaimer thread.
  168. */
  169. void
  170. nlmclnt_recovery(struct nlm_host *host)
  171. {
  172. if (!host->h_reclaiming++) {
  173. nlm_get_host(host);
  174. __module_get(THIS_MODULE);
  175. if (kernel_thread(reclaimer, host, CLONE_FS | CLONE_FILES) < 0)
  176. module_put(THIS_MODULE);
  177. }
  178. }
  179. static int
  180. reclaimer(void *ptr)
  181. {
  182. struct nlm_host *host = (struct nlm_host *) ptr;
  183. struct nlm_wait *block;
  184. struct file_lock *fl, *next;
  185. u32 nsmstate;
  186. daemonize("%s-reclaim", host->h_name);
  187. allow_signal(SIGKILL);
  188. down_write(&host->h_rwsem);
  189. /* This one ensures that our parent doesn't terminate while the
  190. * reclaim is in progress */
  191. lock_kernel();
  192. lockd_up(); /* note: this cannot fail as lockd is already running */
  193. dprintk("lockd: reclaiming locks for host %s\n", host->h_name);
  194. restart:
  195. nsmstate = host->h_nsmstate;
  196. /* Force a portmap getport - the peer's lockd will
  197. * most likely end up on a different port.
  198. */
  199. host->h_nextrebind = jiffies;
  200. nlm_rebind_host(host);
  201. /* First, reclaim all locks that have been granted. */
  202. list_splice_init(&host->h_granted, &host->h_reclaim);
  203. list_for_each_entry_safe(fl, next, &host->h_reclaim, fl_u.nfs_fl.list) {
  204. list_del_init(&fl->fl_u.nfs_fl.list);
  205. /* Why are we leaking memory here? --okir */
  206. if (signalled())
  207. continue;
  208. if (nlmclnt_reclaim(host, fl) != 0)
  209. continue;
  210. list_add_tail(&fl->fl_u.nfs_fl.list, &host->h_granted);
  211. if (host->h_nsmstate != nsmstate) {
  212. /* Argh! The server rebooted again! */
  213. goto restart;
  214. }
  215. }
  216. host->h_reclaiming = 0;
  217. up_write(&host->h_rwsem);
  218. dprintk("NLM: done reclaiming locks for host %s\n", host->h_name);
  219. /* Now, wake up all processes that sleep on a blocked lock */
  220. list_for_each_entry(block, &nlm_blocked, b_list) {
  221. if (block->b_host == host) {
  222. block->b_status = nlm_lck_denied_grace_period;
  223. wake_up(&block->b_wait);
  224. }
  225. }
  226. /* Release host handle after use */
  227. nlm_release_host(host);
  228. lockd_down();
  229. unlock_kernel();
  230. module_put_and_exit(0);
  231. }