clntlock.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. #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. /**
  39. * nlmclnt_init - Set up per-NFS mount point lockd data structures
  40. * @nlm_init: pointer to arguments structure
  41. *
  42. * Returns pointer to an appropriate nlm_host struct,
  43. * or an ERR_PTR value.
  44. */
  45. struct nlm_host *nlmclnt_init(const struct nlmclnt_initdata *nlm_init)
  46. {
  47. struct nlm_host *host;
  48. u32 nlm_version = (nlm_init->nfs_version == 2) ? 1 : 4;
  49. int status;
  50. status = lockd_up();
  51. if (status < 0)
  52. return ERR_PTR(status);
  53. host = nlmclnt_lookup_host(nlm_init->address, nlm_init->addrlen,
  54. nlm_init->protocol, nlm_version,
  55. nlm_init->hostname, nlm_init->noresvport);
  56. if (host == NULL) {
  57. lockd_down();
  58. return ERR_PTR(-ENOLCK);
  59. }
  60. return host;
  61. }
  62. EXPORT_SYMBOL_GPL(nlmclnt_init);
  63. /**
  64. * nlmclnt_done - Release resources allocated by nlmclnt_init()
  65. * @host: nlm_host structure reserved by nlmclnt_init()
  66. *
  67. */
  68. void nlmclnt_done(struct nlm_host *host)
  69. {
  70. nlm_release_host(host);
  71. lockd_down();
  72. }
  73. EXPORT_SYMBOL_GPL(nlmclnt_done);
  74. /*
  75. * Queue up a lock for blocking so that the GRANTED request can see it
  76. */
  77. struct nlm_wait *nlmclnt_prepare_block(struct nlm_host *host, struct file_lock *fl)
  78. {
  79. struct nlm_wait *block;
  80. block = kmalloc(sizeof(*block), GFP_KERNEL);
  81. if (block != NULL) {
  82. block->b_host = host;
  83. block->b_lock = fl;
  84. init_waitqueue_head(&block->b_wait);
  85. block->b_status = nlm_lck_blocked;
  86. list_add(&block->b_list, &nlm_blocked);
  87. }
  88. return block;
  89. }
  90. void nlmclnt_finish_block(struct nlm_wait *block)
  91. {
  92. if (block == NULL)
  93. return;
  94. list_del(&block->b_list);
  95. kfree(block);
  96. }
  97. /*
  98. * Block on a lock
  99. */
  100. int nlmclnt_block(struct nlm_wait *block, struct nlm_rqst *req, long timeout)
  101. {
  102. long ret;
  103. /* A borken server might ask us to block even if we didn't
  104. * request it. Just say no!
  105. */
  106. if (block == NULL)
  107. return -EAGAIN;
  108. /* Go to sleep waiting for GRANT callback. Some servers seem
  109. * to lose callbacks, however, so we're going to poll from
  110. * time to time just to make sure.
  111. *
  112. * For now, the retry frequency is pretty high; normally
  113. * a 1 minute timeout would do. See the comment before
  114. * nlmclnt_lock for an explanation.
  115. */
  116. ret = wait_event_interruptible_timeout(block->b_wait,
  117. block->b_status != nlm_lck_blocked,
  118. timeout);
  119. if (ret < 0)
  120. return -ERESTARTSYS;
  121. req->a_res.status = block->b_status;
  122. return 0;
  123. }
  124. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  125. static const struct in6_addr *nlmclnt_map_v4addr(const struct sockaddr *sap,
  126. struct in6_addr *addr_mapped)
  127. {
  128. const struct sockaddr_in *sin = (const struct sockaddr_in *)sap;
  129. switch (sap->sa_family) {
  130. case AF_INET6:
  131. return &((const struct sockaddr_in6 *)sap)->sin6_addr;
  132. case AF_INET:
  133. ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, addr_mapped);
  134. return addr_mapped;
  135. }
  136. return NULL;
  137. }
  138. /*
  139. * If lockd is using a PF_INET6 listener, all incoming requests appear
  140. * to come from AF_INET6 remotes. The address of AF_INET remotes are
  141. * mapped to AF_INET6 automatically by the network layer. In case the
  142. * user passed an AF_INET server address at mount time, ensure both
  143. * addresses are AF_INET6 before comparing them.
  144. */
  145. static int nlmclnt_cmp_addr(const struct nlm_host *host,
  146. const struct sockaddr *sap)
  147. {
  148. const struct in6_addr *addr1;
  149. const struct in6_addr *addr2;
  150. struct in6_addr addr1_mapped;
  151. struct in6_addr addr2_mapped;
  152. addr1 = nlmclnt_map_v4addr(nlm_addr(host), &addr1_mapped);
  153. if (likely(addr1 != NULL)) {
  154. addr2 = nlmclnt_map_v4addr(sap, &addr2_mapped);
  155. if (likely(addr2 != NULL))
  156. return ipv6_addr_equal(addr1, addr2);
  157. }
  158. return 0;
  159. }
  160. #else /* !(CONFIG_IPV6 || CONFIG_IPV6_MODULE) */
  161. static int nlmclnt_cmp_addr(const struct nlm_host *host,
  162. const struct sockaddr *sap)
  163. {
  164. return nlm_cmp_addr(nlm_addr(host), sap);
  165. }
  166. #endif /* !(CONFIG_IPV6 || CONFIG_IPV6_MODULE) */
  167. /*
  168. * The server lockd has called us back to tell us the lock was granted
  169. */
  170. __be32 nlmclnt_grant(const struct sockaddr *addr, const struct nlm_lock *lock)
  171. {
  172. const struct file_lock *fl = &lock->fl;
  173. const struct nfs_fh *fh = &lock->fh;
  174. struct nlm_wait *block;
  175. __be32 res = nlm_lck_denied;
  176. /*
  177. * Look up blocked request based on arguments.
  178. * Warning: must not use cookie to match it!
  179. */
  180. list_for_each_entry(block, &nlm_blocked, b_list) {
  181. struct file_lock *fl_blocked = block->b_lock;
  182. if (fl_blocked->fl_start != fl->fl_start)
  183. continue;
  184. if (fl_blocked->fl_end != fl->fl_end)
  185. continue;
  186. /*
  187. * Careful! The NLM server will return the 32-bit "pid" that
  188. * we put on the wire: in this case the lockowner "pid".
  189. */
  190. if (fl_blocked->fl_u.nfs_fl.owner->pid != lock->svid)
  191. continue;
  192. if (!nlmclnt_cmp_addr(block->b_host, addr))
  193. continue;
  194. if (nfs_compare_fh(NFS_FH(fl_blocked->fl_file->f_path.dentry->d_inode) ,fh) != 0)
  195. continue;
  196. /* Alright, we found a lock. Set the return status
  197. * and wake up the caller
  198. */
  199. block->b_status = nlm_granted;
  200. wake_up(&block->b_wait);
  201. res = nlm_granted;
  202. }
  203. return res;
  204. }
  205. /*
  206. * The following procedures deal with the recovery of locks after a
  207. * server crash.
  208. */
  209. /*
  210. * Reclaim all locks on server host. We do this by spawning a separate
  211. * reclaimer thread.
  212. */
  213. void
  214. nlmclnt_recovery(struct nlm_host *host)
  215. {
  216. struct task_struct *task;
  217. if (!host->h_reclaiming++) {
  218. nlm_get_host(host);
  219. task = kthread_run(reclaimer, host, "%s-reclaim", host->h_name);
  220. if (IS_ERR(task))
  221. printk(KERN_ERR "lockd: unable to spawn reclaimer "
  222. "thread. Locks for %s won't be reclaimed! "
  223. "(%ld)\n", host->h_name, PTR_ERR(task));
  224. }
  225. }
  226. static int
  227. reclaimer(void *ptr)
  228. {
  229. struct nlm_host *host = (struct nlm_host *) ptr;
  230. struct nlm_wait *block;
  231. struct file_lock *fl, *next;
  232. u32 nsmstate;
  233. allow_signal(SIGKILL);
  234. down_write(&host->h_rwsem);
  235. /* This one ensures that our parent doesn't terminate while the
  236. * reclaim is in progress */
  237. lock_kernel();
  238. lockd_up(); /* note: this cannot fail as lockd is already running */
  239. dprintk("lockd: reclaiming locks for host %s\n", host->h_name);
  240. restart:
  241. nsmstate = host->h_nsmstate;
  242. /* Force a portmap getport - the peer's lockd will
  243. * most likely end up on a different port.
  244. */
  245. host->h_nextrebind = jiffies;
  246. nlm_rebind_host(host);
  247. /* First, reclaim all locks that have been granted. */
  248. list_splice_init(&host->h_granted, &host->h_reclaim);
  249. list_for_each_entry_safe(fl, next, &host->h_reclaim, fl_u.nfs_fl.list) {
  250. list_del_init(&fl->fl_u.nfs_fl.list);
  251. /*
  252. * sending this thread a SIGKILL will result in any unreclaimed
  253. * locks being removed from the h_granted list. This means that
  254. * the kernel will not attempt to reclaim them again if a new
  255. * reclaimer thread is spawned for this host.
  256. */
  257. if (signalled())
  258. continue;
  259. if (nlmclnt_reclaim(host, fl) != 0)
  260. continue;
  261. list_add_tail(&fl->fl_u.nfs_fl.list, &host->h_granted);
  262. if (host->h_nsmstate != nsmstate) {
  263. /* Argh! The server rebooted again! */
  264. goto restart;
  265. }
  266. }
  267. host->h_reclaiming = 0;
  268. up_write(&host->h_rwsem);
  269. dprintk("NLM: done reclaiming locks for host %s\n", host->h_name);
  270. /* Now, wake up all processes that sleep on a blocked lock */
  271. list_for_each_entry(block, &nlm_blocked, b_list) {
  272. if (block->b_host == host) {
  273. block->b_status = nlm_lck_denied_grace_period;
  274. wake_up(&block->b_wait);
  275. }
  276. }
  277. /* Release host handle after use */
  278. nlm_release_host(host);
  279. lockd_down();
  280. unlock_kernel();
  281. return 0;
  282. }