clntlock.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 nlm_wait * b_next; /* 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. u32 b_status; /* grant callback status */
  35. };
  36. static struct nlm_wait * nlm_blocked;
  37. /*
  38. * Block on a lock
  39. */
  40. int
  41. nlmclnt_block(struct nlm_host *host, struct file_lock *fl, u32 *statp)
  42. {
  43. struct nlm_wait block, **head;
  44. int err;
  45. u32 pstate;
  46. block.b_host = host;
  47. block.b_lock = fl;
  48. init_waitqueue_head(&block.b_wait);
  49. block.b_status = NLM_LCK_BLOCKED;
  50. block.b_next = nlm_blocked;
  51. nlm_blocked = &block;
  52. /* Remember pseudo nsm state */
  53. pstate = host->h_state;
  54. /* Go to sleep waiting for GRANT callback. Some servers seem
  55. * to lose callbacks, however, so we're going to poll from
  56. * time to time just to make sure.
  57. *
  58. * For now, the retry frequency is pretty high; normally
  59. * a 1 minute timeout would do. See the comment before
  60. * nlmclnt_lock for an explanation.
  61. */
  62. sleep_on_timeout(&block.b_wait, 30*HZ);
  63. for (head = &nlm_blocked; *head; head = &(*head)->b_next) {
  64. if (*head == &block) {
  65. *head = block.b_next;
  66. break;
  67. }
  68. }
  69. if (!signalled()) {
  70. *statp = block.b_status;
  71. return 0;
  72. }
  73. /* Okay, we were interrupted. Cancel the pending request
  74. * unless the server has rebooted.
  75. */
  76. if (pstate == host->h_state && (err = nlmclnt_cancel(host, fl)) < 0)
  77. printk(KERN_NOTICE
  78. "lockd: CANCEL call failed (errno %d)\n", -err);
  79. return -ERESTARTSYS;
  80. }
  81. /*
  82. * The server lockd has called us back to tell us the lock was granted
  83. */
  84. u32
  85. nlmclnt_grant(struct nlm_lock *lock)
  86. {
  87. struct nlm_wait *block;
  88. /*
  89. * Look up blocked request based on arguments.
  90. * Warning: must not use cookie to match it!
  91. */
  92. for (block = nlm_blocked; block; block = block->b_next) {
  93. if (nlm_compare_locks(block->b_lock, &lock->fl))
  94. break;
  95. }
  96. /* Ooops, no blocked request found. */
  97. if (block == NULL)
  98. return nlm_lck_denied;
  99. /* Alright, we found the lock. Set the return status and
  100. * wake up the caller.
  101. */
  102. block->b_status = NLM_LCK_GRANTED;
  103. wake_up(&block->b_wait);
  104. return nlm_granted;
  105. }
  106. /*
  107. * The following procedures deal with the recovery of locks after a
  108. * server crash.
  109. */
  110. /*
  111. * Mark the locks for reclaiming.
  112. * FIXME: In 2.5 we don't want to iterate through any global file_lock_list.
  113. * Maintain NLM lock reclaiming lists in the nlm_host instead.
  114. */
  115. static
  116. void nlmclnt_mark_reclaim(struct nlm_host *host)
  117. {
  118. struct file_lock *fl;
  119. struct inode *inode;
  120. struct list_head *tmp;
  121. list_for_each(tmp, &file_lock_list) {
  122. fl = list_entry(tmp, struct file_lock, fl_link);
  123. inode = fl->fl_file->f_dentry->d_inode;
  124. if (inode->i_sb->s_magic != NFS_SUPER_MAGIC)
  125. continue;
  126. if (fl->fl_u.nfs_fl.owner->host != host)
  127. continue;
  128. if (!(fl->fl_u.nfs_fl.flags & NFS_LCK_GRANTED))
  129. continue;
  130. fl->fl_u.nfs_fl.flags |= NFS_LCK_RECLAIM;
  131. }
  132. }
  133. /*
  134. * Someone has sent us an SM_NOTIFY. Ensure we bind to the new port number,
  135. * that we mark locks for reclaiming, and that we bump the pseudo NSM state.
  136. */
  137. static inline
  138. void nlmclnt_prepare_reclaim(struct nlm_host *host, u32 newstate)
  139. {
  140. host->h_monitored = 0;
  141. host->h_nsmstate = newstate;
  142. host->h_state++;
  143. host->h_nextrebind = 0;
  144. nlm_rebind_host(host);
  145. nlmclnt_mark_reclaim(host);
  146. dprintk("NLM: reclaiming locks for host %s", host->h_name);
  147. }
  148. /*
  149. * Reclaim all locks on server host. We do this by spawning a separate
  150. * reclaimer thread.
  151. */
  152. void
  153. nlmclnt_recovery(struct nlm_host *host, u32 newstate)
  154. {
  155. if (host->h_reclaiming++) {
  156. if (host->h_nsmstate == newstate)
  157. return;
  158. nlmclnt_prepare_reclaim(host, newstate);
  159. } else {
  160. nlmclnt_prepare_reclaim(host, newstate);
  161. nlm_get_host(host);
  162. __module_get(THIS_MODULE);
  163. if (kernel_thread(reclaimer, host, CLONE_KERNEL) < 0)
  164. module_put(THIS_MODULE);
  165. }
  166. }
  167. static int
  168. reclaimer(void *ptr)
  169. {
  170. struct nlm_host *host = (struct nlm_host *) ptr;
  171. struct nlm_wait *block;
  172. struct list_head *tmp;
  173. struct file_lock *fl;
  174. struct inode *inode;
  175. daemonize("%s-reclaim", host->h_name);
  176. allow_signal(SIGKILL);
  177. /* This one ensures that our parent doesn't terminate while the
  178. * reclaim is in progress */
  179. lock_kernel();
  180. lockd_up();
  181. /* First, reclaim all locks that have been marked. */
  182. restart:
  183. list_for_each(tmp, &file_lock_list) {
  184. fl = list_entry(tmp, struct file_lock, fl_link);
  185. inode = fl->fl_file->f_dentry->d_inode;
  186. if (inode->i_sb->s_magic != NFS_SUPER_MAGIC)
  187. continue;
  188. if (fl->fl_u.nfs_fl.owner->host != host)
  189. continue;
  190. if (!(fl->fl_u.nfs_fl.flags & NFS_LCK_RECLAIM))
  191. continue;
  192. fl->fl_u.nfs_fl.flags &= ~NFS_LCK_RECLAIM;
  193. nlmclnt_reclaim(host, fl);
  194. if (signalled())
  195. break;
  196. goto restart;
  197. }
  198. host->h_reclaiming = 0;
  199. /* Now, wake up all processes that sleep on a blocked lock */
  200. for (block = nlm_blocked; block; block = block->b_next) {
  201. if (block->b_host == host) {
  202. block->b_status = NLM_LCK_DENIED_GRACE_PERIOD;
  203. wake_up(&block->b_wait);
  204. }
  205. }
  206. /* Release host handle after use */
  207. nlm_release_host(host);
  208. lockd_down();
  209. unlock_kernel();
  210. module_put_and_exit(0);
  211. }