clntlock.c 7.5 KB

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