clntlock.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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/slab.h>
  11. #include <linux/time.h>
  12. #include <linux/nfs_fs.h>
  13. #include <linux/sunrpc/addr.h>
  14. #include <linux/sunrpc/svc.h>
  15. #include <linux/lockd/lockd.h>
  16. #include <linux/kthread.h>
  17. #define NLMDBG_FACILITY NLMDBG_CLIENT
  18. /*
  19. * Local function prototypes
  20. */
  21. static int reclaimer(void *ptr);
  22. /*
  23. * The following functions handle blocking and granting from the
  24. * client perspective.
  25. */
  26. /*
  27. * This is the representation of a blocked client lock.
  28. */
  29. struct nlm_wait {
  30. struct list_head b_list; /* linked list */
  31. wait_queue_head_t b_wait; /* where to wait on */
  32. struct nlm_host * b_host;
  33. struct file_lock * b_lock; /* local file lock */
  34. unsigned short b_reclaim; /* got to reclaim lock */
  35. __be32 b_status; /* grant callback status */
  36. };
  37. static LIST_HEAD(nlm_blocked);
  38. static DEFINE_SPINLOCK(nlm_blocked_lock);
  39. /**
  40. * nlmclnt_init - Set up per-NFS mount point lockd data structures
  41. * @nlm_init: pointer to arguments structure
  42. *
  43. * Returns pointer to an appropriate nlm_host struct,
  44. * or an ERR_PTR value.
  45. */
  46. struct nlm_host *nlmclnt_init(const struct nlmclnt_initdata *nlm_init)
  47. {
  48. struct nlm_host *host;
  49. u32 nlm_version = (nlm_init->nfs_version == 2) ? 1 : 4;
  50. int status;
  51. status = lockd_up(nlm_init->net);
  52. if (status < 0)
  53. return ERR_PTR(status);
  54. host = nlmclnt_lookup_host(nlm_init->address, nlm_init->addrlen,
  55. nlm_init->protocol, nlm_version,
  56. nlm_init->hostname, nlm_init->noresvport,
  57. nlm_init->net);
  58. if (host == NULL)
  59. goto out_nohost;
  60. if (host->h_rpcclnt == NULL && nlm_bind_host(host) == NULL)
  61. goto out_nobind;
  62. host->h_nlmclnt_ops = nlm_init->nlmclnt_ops;
  63. return host;
  64. out_nobind:
  65. nlmclnt_release_host(host);
  66. out_nohost:
  67. lockd_down(nlm_init->net);
  68. return ERR_PTR(-ENOLCK);
  69. }
  70. EXPORT_SYMBOL_GPL(nlmclnt_init);
  71. /**
  72. * nlmclnt_done - Release resources allocated by nlmclnt_init()
  73. * @host: nlm_host structure reserved by nlmclnt_init()
  74. *
  75. */
  76. void nlmclnt_done(struct nlm_host *host)
  77. {
  78. struct net *net = host->net;
  79. nlmclnt_release_host(host);
  80. lockd_down(net);
  81. }
  82. EXPORT_SYMBOL_GPL(nlmclnt_done);
  83. /*
  84. * Queue up a lock for blocking so that the GRANTED request can see it
  85. */
  86. struct nlm_wait *nlmclnt_prepare_block(struct nlm_host *host, struct file_lock *fl)
  87. {
  88. struct nlm_wait *block;
  89. block = kmalloc(sizeof(*block), GFP_KERNEL);
  90. if (block != NULL) {
  91. block->b_host = host;
  92. block->b_lock = fl;
  93. init_waitqueue_head(&block->b_wait);
  94. block->b_status = nlm_lck_blocked;
  95. spin_lock(&nlm_blocked_lock);
  96. list_add(&block->b_list, &nlm_blocked);
  97. spin_unlock(&nlm_blocked_lock);
  98. }
  99. return block;
  100. }
  101. void nlmclnt_finish_block(struct nlm_wait *block)
  102. {
  103. if (block == NULL)
  104. return;
  105. spin_lock(&nlm_blocked_lock);
  106. list_del(&block->b_list);
  107. spin_unlock(&nlm_blocked_lock);
  108. kfree(block);
  109. }
  110. /*
  111. * Block on a lock
  112. */
  113. int nlmclnt_block(struct nlm_wait *block, struct nlm_rqst *req, long timeout)
  114. {
  115. long ret;
  116. /* A borken server might ask us to block even if we didn't
  117. * request it. Just say no!
  118. */
  119. if (block == NULL)
  120. return -EAGAIN;
  121. /* Go to sleep waiting for GRANT callback. Some servers seem
  122. * to lose callbacks, however, so we're going to poll from
  123. * time to time just to make sure.
  124. *
  125. * For now, the retry frequency is pretty high; normally
  126. * a 1 minute timeout would do. See the comment before
  127. * nlmclnt_lock for an explanation.
  128. */
  129. ret = wait_event_interruptible_timeout(block->b_wait,
  130. block->b_status != nlm_lck_blocked,
  131. timeout);
  132. if (ret < 0)
  133. return -ERESTARTSYS;
  134. /* Reset the lock status after a server reboot so we resend */
  135. if (block->b_status == nlm_lck_denied_grace_period)
  136. block->b_status = nlm_lck_blocked;
  137. req->a_res.status = block->b_status;
  138. return 0;
  139. }
  140. /*
  141. * The server lockd has called us back to tell us the lock was granted
  142. */
  143. __be32 nlmclnt_grant(const struct sockaddr *addr, const struct nlm_lock *lock)
  144. {
  145. const struct file_lock *fl = &lock->fl;
  146. const struct nfs_fh *fh = &lock->fh;
  147. struct nlm_wait *block;
  148. __be32 res = nlm_lck_denied;
  149. /*
  150. * Look up blocked request based on arguments.
  151. * Warning: must not use cookie to match it!
  152. */
  153. spin_lock(&nlm_blocked_lock);
  154. list_for_each_entry(block, &nlm_blocked, b_list) {
  155. struct file_lock *fl_blocked = block->b_lock;
  156. if (fl_blocked->fl_start != fl->fl_start)
  157. continue;
  158. if (fl_blocked->fl_end != fl->fl_end)
  159. continue;
  160. /*
  161. * Careful! The NLM server will return the 32-bit "pid" that
  162. * we put on the wire: in this case the lockowner "pid".
  163. */
  164. if (fl_blocked->fl_u.nfs_fl.owner->pid != lock->svid)
  165. continue;
  166. if (!rpc_cmp_addr(nlm_addr(block->b_host), addr))
  167. continue;
  168. if (nfs_compare_fh(NFS_FH(locks_inode(fl_blocked->fl_file)), fh) != 0)
  169. continue;
  170. /* Alright, we found a lock. Set the return status
  171. * and wake up the caller
  172. */
  173. block->b_status = nlm_granted;
  174. wake_up(&block->b_wait);
  175. res = nlm_granted;
  176. }
  177. spin_unlock(&nlm_blocked_lock);
  178. return res;
  179. }
  180. /*
  181. * The following procedures deal with the recovery of locks after a
  182. * server crash.
  183. */
  184. /*
  185. * Reclaim all locks on server host. We do this by spawning a separate
  186. * reclaimer thread.
  187. */
  188. void
  189. nlmclnt_recovery(struct nlm_host *host)
  190. {
  191. struct task_struct *task;
  192. if (!host->h_reclaiming++) {
  193. nlm_get_host(host);
  194. task = kthread_run(reclaimer, host, "%s-reclaim", host->h_name);
  195. if (IS_ERR(task))
  196. printk(KERN_ERR "lockd: unable to spawn reclaimer "
  197. "thread. Locks for %s won't be reclaimed! "
  198. "(%ld)\n", host->h_name, PTR_ERR(task));
  199. }
  200. }
  201. static int
  202. reclaimer(void *ptr)
  203. {
  204. struct nlm_host *host = (struct nlm_host *) ptr;
  205. struct nlm_wait *block;
  206. struct nlm_rqst *req;
  207. struct file_lock *fl, *next;
  208. u32 nsmstate;
  209. struct net *net = host->net;
  210. req = kmalloc(sizeof(*req), GFP_KERNEL);
  211. if (!req)
  212. return 0;
  213. allow_signal(SIGKILL);
  214. down_write(&host->h_rwsem);
  215. lockd_up(net); /* note: this cannot fail as lockd is already running */
  216. dprintk("lockd: reclaiming locks for host %s\n", host->h_name);
  217. restart:
  218. nsmstate = host->h_nsmstate;
  219. /* Force a portmap getport - the peer's lockd will
  220. * most likely end up on a different port.
  221. */
  222. host->h_nextrebind = jiffies;
  223. nlm_rebind_host(host);
  224. /* First, reclaim all locks that have been granted. */
  225. list_splice_init(&host->h_granted, &host->h_reclaim);
  226. list_for_each_entry_safe(fl, next, &host->h_reclaim, fl_u.nfs_fl.list) {
  227. list_del_init(&fl->fl_u.nfs_fl.list);
  228. /*
  229. * sending this thread a SIGKILL will result in any unreclaimed
  230. * locks being removed from the h_granted list. This means that
  231. * the kernel will not attempt to reclaim them again if a new
  232. * reclaimer thread is spawned for this host.
  233. */
  234. if (signalled())
  235. continue;
  236. if (nlmclnt_reclaim(host, fl, req) != 0)
  237. continue;
  238. list_add_tail(&fl->fl_u.nfs_fl.list, &host->h_granted);
  239. if (host->h_nsmstate != nsmstate) {
  240. /* Argh! The server rebooted again! */
  241. goto restart;
  242. }
  243. }
  244. host->h_reclaiming = 0;
  245. up_write(&host->h_rwsem);
  246. dprintk("NLM: done reclaiming locks for host %s\n", host->h_name);
  247. /* Now, wake up all processes that sleep on a blocked lock */
  248. spin_lock(&nlm_blocked_lock);
  249. list_for_each_entry(block, &nlm_blocked, b_list) {
  250. if (block->b_host == host) {
  251. block->b_status = nlm_lck_denied_grace_period;
  252. wake_up(&block->b_wait);
  253. }
  254. }
  255. spin_unlock(&nlm_blocked_lock);
  256. /* Release host handle after use */
  257. nlmclnt_release_host(host);
  258. lockd_down(net);
  259. kfree(req);
  260. return 0;
  261. }