clntlock.c 7.5 KB

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