clntlock.c 7.4 KB

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