clntlock.c 6.7 KB

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