clntlock.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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/time.h>
  11. #include <linux/nfs_fs.h>
  12. #include <linux/sunrpc/clnt.h>
  13. #include <linux/sunrpc/svc.h>
  14. #include <linux/lockd/lockd.h>
  15. #include <linux/smp_lock.h>
  16. #define NLMDBG_FACILITY NLMDBG_CLIENT
  17. /*
  18. * Local function prototypes
  19. */
  20. static int reclaimer(void *ptr);
  21. /*
  22. * The following functions handle blocking and granting from the
  23. * client perspective.
  24. */
  25. /*
  26. * This is the representation of a blocked client lock.
  27. */
  28. struct nlm_wait {
  29. struct list_head b_list; /* linked list */
  30. wait_queue_head_t b_wait; /* where to wait on */
  31. struct nlm_host * b_host;
  32. struct file_lock * b_lock; /* local file lock */
  33. unsigned short b_reclaim; /* got to reclaim lock */
  34. __be32 b_status; /* grant callback status */
  35. };
  36. static LIST_HEAD(nlm_blocked);
  37. /*
  38. * Queue up a lock for blocking so that the GRANTED request can see it
  39. */
  40. struct nlm_wait *nlmclnt_prepare_block(struct nlm_host *host, struct file_lock *fl)
  41. {
  42. struct nlm_wait *block;
  43. block = kmalloc(sizeof(*block), GFP_KERNEL);
  44. if (block != NULL) {
  45. block->b_host = host;
  46. block->b_lock = fl;
  47. init_waitqueue_head(&block->b_wait);
  48. block->b_status = nlm_lck_blocked;
  49. list_add(&block->b_list, &nlm_blocked);
  50. }
  51. return block;
  52. }
  53. void nlmclnt_finish_block(struct nlm_wait *block)
  54. {
  55. if (block == NULL)
  56. return;
  57. list_del(&block->b_list);
  58. kfree(block);
  59. }
  60. /*
  61. * Block on a lock
  62. */
  63. int nlmclnt_block(struct nlm_wait *block, struct nlm_rqst *req, long timeout)
  64. {
  65. long ret;
  66. /* A borken server might ask us to block even if we didn't
  67. * request it. Just say no!
  68. */
  69. if (block == NULL)
  70. return -EAGAIN;
  71. /* Go to sleep waiting for GRANT callback. Some servers seem
  72. * to lose callbacks, however, so we're going to poll from
  73. * time to time just to make sure.
  74. *
  75. * For now, the retry frequency is pretty high; normally
  76. * a 1 minute timeout would do. See the comment before
  77. * nlmclnt_lock for an explanation.
  78. */
  79. ret = wait_event_interruptible_timeout(block->b_wait,
  80. block->b_status != nlm_lck_blocked,
  81. timeout);
  82. if (ret < 0)
  83. return -ERESTARTSYS;
  84. req->a_res.status = block->b_status;
  85. return 0;
  86. }
  87. /*
  88. * The server lockd has called us back to tell us the lock was granted
  89. */
  90. __be32 nlmclnt_grant(const struct sockaddr_in *addr, const struct nlm_lock *lock)
  91. {
  92. const struct file_lock *fl = &lock->fl;
  93. const struct nfs_fh *fh = &lock->fh;
  94. struct nlm_wait *block;
  95. __be32 res = nlm_lck_denied;
  96. /*
  97. * Look up blocked request based on arguments.
  98. * Warning: must not use cookie to match it!
  99. */
  100. list_for_each_entry(block, &nlm_blocked, b_list) {
  101. struct file_lock *fl_blocked = block->b_lock;
  102. if (fl_blocked->fl_start != fl->fl_start)
  103. continue;
  104. if (fl_blocked->fl_end != fl->fl_end)
  105. continue;
  106. /*
  107. * Careful! The NLM server will return the 32-bit "pid" that
  108. * we put on the wire: in this case the lockowner "pid".
  109. */
  110. if (fl_blocked->fl_u.nfs_fl.owner->pid != lock->svid)
  111. continue;
  112. if (!nlm_cmp_addr(&block->b_host->h_addr, addr))
  113. continue;
  114. if (nfs_compare_fh(NFS_FH(fl_blocked->fl_file->f_path.dentry->d_inode) ,fh) != 0)
  115. continue;
  116. /* Alright, we found a lock. Set the return status
  117. * and wake up the caller
  118. */
  119. block->b_status = nlm_granted;
  120. wake_up(&block->b_wait);
  121. res = nlm_granted;
  122. }
  123. return res;
  124. }
  125. /*
  126. * The following procedures deal with the recovery of locks after a
  127. * server crash.
  128. */
  129. /*
  130. * Reclaim all locks on server host. We do this by spawning a separate
  131. * reclaimer thread.
  132. */
  133. void
  134. nlmclnt_recovery(struct nlm_host *host)
  135. {
  136. if (!host->h_reclaiming++) {
  137. nlm_get_host(host);
  138. __module_get(THIS_MODULE);
  139. if (kernel_thread(reclaimer, host, CLONE_KERNEL) < 0)
  140. module_put(THIS_MODULE);
  141. }
  142. }
  143. static int
  144. reclaimer(void *ptr)
  145. {
  146. struct nlm_host *host = (struct nlm_host *) ptr;
  147. struct nlm_wait *block;
  148. struct file_lock *fl, *next;
  149. u32 nsmstate;
  150. daemonize("%s-reclaim", host->h_name);
  151. allow_signal(SIGKILL);
  152. down_write(&host->h_rwsem);
  153. /* This one ensures that our parent doesn't terminate while the
  154. * reclaim is in progress */
  155. lock_kernel();
  156. lockd_up(0); /* note: this cannot fail as lockd is already running */
  157. dprintk("lockd: reclaiming locks for host %s\n", host->h_name);
  158. restart:
  159. nsmstate = host->h_nsmstate;
  160. /* Force a portmap getport - the peer's lockd will
  161. * most likely end up on a different port.
  162. */
  163. host->h_nextrebind = jiffies;
  164. nlm_rebind_host(host);
  165. /* First, reclaim all locks that have been granted. */
  166. list_splice_init(&host->h_granted, &host->h_reclaim);
  167. list_for_each_entry_safe(fl, next, &host->h_reclaim, fl_u.nfs_fl.list) {
  168. list_del_init(&fl->fl_u.nfs_fl.list);
  169. /* Why are we leaking memory here? --okir */
  170. if (signalled())
  171. continue;
  172. if (nlmclnt_reclaim(host, fl) != 0)
  173. continue;
  174. list_add_tail(&fl->fl_u.nfs_fl.list, &host->h_granted);
  175. if (host->h_nsmstate != nsmstate) {
  176. /* Argh! The server rebooted again! */
  177. goto restart;
  178. }
  179. }
  180. host->h_reclaiming = 0;
  181. up_write(&host->h_rwsem);
  182. dprintk("NLM: done reclaiming locks for host %s\n", host->h_name);
  183. /* Now, wake up all processes that sleep on a blocked lock */
  184. list_for_each_entry(block, &nlm_blocked, b_list) {
  185. if (block->b_host == host) {
  186. block->b_status = nlm_lck_denied_grace_period;
  187. wake_up(&block->b_wait);
  188. }
  189. }
  190. /* Release host handle after use */
  191. nlm_release_host(host);
  192. lockd_down();
  193. unlock_kernel();
  194. module_put_and_exit(0);
  195. }