clntlock.c 5.5 KB

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