clntlock.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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/clnt.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();
  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. if (host == NULL) {
  58. lockd_down();
  59. return ERR_PTR(-ENOLCK);
  60. }
  61. return host;
  62. }
  63. EXPORT_SYMBOL_GPL(nlmclnt_init);
  64. /**
  65. * nlmclnt_done - Release resources allocated by nlmclnt_init()
  66. * @host: nlm_host structure reserved by nlmclnt_init()
  67. *
  68. */
  69. void nlmclnt_done(struct nlm_host *host)
  70. {
  71. nlmclnt_release_host(host);
  72. lockd_down();
  73. }
  74. EXPORT_SYMBOL_GPL(nlmclnt_done);
  75. /*
  76. * Queue up a lock for blocking so that the GRANTED request can see it
  77. */
  78. struct nlm_wait *nlmclnt_prepare_block(struct nlm_host *host, struct file_lock *fl)
  79. {
  80. struct nlm_wait *block;
  81. block = kmalloc(sizeof(*block), GFP_KERNEL);
  82. if (block != NULL) {
  83. block->b_host = host;
  84. block->b_lock = fl;
  85. init_waitqueue_head(&block->b_wait);
  86. block->b_status = nlm_lck_blocked;
  87. spin_lock(&nlm_blocked_lock);
  88. list_add(&block->b_list, &nlm_blocked);
  89. spin_unlock(&nlm_blocked_lock);
  90. }
  91. return block;
  92. }
  93. void nlmclnt_finish_block(struct nlm_wait *block)
  94. {
  95. if (block == NULL)
  96. return;
  97. spin_lock(&nlm_blocked_lock);
  98. list_del(&block->b_list);
  99. spin_unlock(&nlm_blocked_lock);
  100. kfree(block);
  101. }
  102. /*
  103. * Block on a lock
  104. */
  105. int nlmclnt_block(struct nlm_wait *block, struct nlm_rqst *req, long timeout)
  106. {
  107. long ret;
  108. /* A borken server might ask us to block even if we didn't
  109. * request it. Just say no!
  110. */
  111. if (block == NULL)
  112. return -EAGAIN;
  113. /* Go to sleep waiting for GRANT callback. Some servers seem
  114. * to lose callbacks, however, so we're going to poll from
  115. * time to time just to make sure.
  116. *
  117. * For now, the retry frequency is pretty high; normally
  118. * a 1 minute timeout would do. See the comment before
  119. * nlmclnt_lock for an explanation.
  120. */
  121. ret = wait_event_interruptible_timeout(block->b_wait,
  122. block->b_status != nlm_lck_blocked,
  123. timeout);
  124. if (ret < 0)
  125. return -ERESTARTSYS;
  126. req->a_res.status = block->b_status;
  127. return 0;
  128. }
  129. /*
  130. * The server lockd has called us back to tell us the lock was granted
  131. */
  132. __be32 nlmclnt_grant(const struct sockaddr *addr, const struct nlm_lock *lock)
  133. {
  134. const struct file_lock *fl = &lock->fl;
  135. const struct nfs_fh *fh = &lock->fh;
  136. struct nlm_wait *block;
  137. __be32 res = nlm_lck_denied;
  138. /*
  139. * Look up blocked request based on arguments.
  140. * Warning: must not use cookie to match it!
  141. */
  142. spin_lock(&nlm_blocked_lock);
  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 (!rpc_cmp_addr(nlm_addr(block->b_host), 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. spin_unlock(&nlm_blocked_lock);
  167. return res;
  168. }
  169. /*
  170. * The following procedures deal with the recovery of locks after a
  171. * server crash.
  172. */
  173. /*
  174. * Reclaim all locks on server host. We do this by spawning a separate
  175. * reclaimer thread.
  176. */
  177. void
  178. nlmclnt_recovery(struct nlm_host *host)
  179. {
  180. struct task_struct *task;
  181. if (!host->h_reclaiming++) {
  182. nlm_get_host(host);
  183. task = kthread_run(reclaimer, host, "%s-reclaim", host->h_name);
  184. if (IS_ERR(task))
  185. printk(KERN_ERR "lockd: unable to spawn reclaimer "
  186. "thread. Locks for %s won't be reclaimed! "
  187. "(%ld)\n", host->h_name, PTR_ERR(task));
  188. }
  189. }
  190. static int
  191. reclaimer(void *ptr)
  192. {
  193. struct nlm_host *host = (struct nlm_host *) ptr;
  194. struct nlm_wait *block;
  195. struct file_lock *fl, *next;
  196. u32 nsmstate;
  197. allow_signal(SIGKILL);
  198. down_write(&host->h_rwsem);
  199. lockd_up(); /* 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. /*
  213. * sending this thread a SIGKILL will result in any unreclaimed
  214. * locks being removed from the h_granted list. This means that
  215. * the kernel will not attempt to reclaim them again if a new
  216. * reclaimer thread is spawned for this host.
  217. */
  218. if (signalled())
  219. continue;
  220. if (nlmclnt_reclaim(host, fl) != 0)
  221. continue;
  222. list_add_tail(&fl->fl_u.nfs_fl.list, &host->h_granted);
  223. if (host->h_nsmstate != nsmstate) {
  224. /* Argh! The server rebooted again! */
  225. goto restart;
  226. }
  227. }
  228. host->h_reclaiming = 0;
  229. up_write(&host->h_rwsem);
  230. dprintk("NLM: done reclaiming locks for host %s\n", host->h_name);
  231. /* Now, wake up all processes that sleep on a blocked lock */
  232. spin_lock(&nlm_blocked_lock);
  233. list_for_each_entry(block, &nlm_blocked, b_list) {
  234. if (block->b_host == host) {
  235. block->b_status = nlm_lck_denied_grace_period;
  236. wake_up(&block->b_wait);
  237. }
  238. }
  239. spin_unlock(&nlm_blocked_lock);
  240. /* Release host handle after use */
  241. nlmclnt_release_host(host);
  242. lockd_down();
  243. return 0;
  244. }