delegation.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * linux/fs/nfs/delegation.c
  3. *
  4. * Copyright (C) 2004 Trond Myklebust
  5. *
  6. * NFS file delegation management
  7. *
  8. */
  9. #include <linux/completion.h>
  10. #include <linux/kthread.h>
  11. #include <linux/module.h>
  12. #include <linux/sched.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/nfs4.h>
  15. #include <linux/nfs_fs.h>
  16. #include <linux/nfs_xdr.h>
  17. #include "nfs4_fs.h"
  18. #include "delegation.h"
  19. #include "internal.h"
  20. static void nfs_do_free_delegation(struct nfs_delegation *delegation)
  21. {
  22. kfree(delegation);
  23. }
  24. static void nfs_free_delegation_callback(struct rcu_head *head)
  25. {
  26. struct nfs_delegation *delegation = container_of(head, struct nfs_delegation, rcu);
  27. nfs_do_free_delegation(delegation);
  28. }
  29. static void nfs_free_delegation(struct nfs_delegation *delegation)
  30. {
  31. struct rpc_cred *cred;
  32. cred = rcu_dereference(delegation->cred);
  33. rcu_assign_pointer(delegation->cred, NULL);
  34. call_rcu(&delegation->rcu, nfs_free_delegation_callback);
  35. if (cred)
  36. put_rpccred(cred);
  37. }
  38. static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state)
  39. {
  40. struct inode *inode = state->inode;
  41. struct file_lock *fl;
  42. int status;
  43. for (fl = inode->i_flock; fl != 0; fl = fl->fl_next) {
  44. if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
  45. continue;
  46. if (nfs_file_open_context(fl->fl_file) != ctx)
  47. continue;
  48. status = nfs4_lock_delegation_recall(state, fl);
  49. if (status >= 0)
  50. continue;
  51. switch (status) {
  52. default:
  53. printk(KERN_ERR "%s: unhandled error %d.\n",
  54. __FUNCTION__, status);
  55. case -NFS4ERR_EXPIRED:
  56. /* kill_proc(fl->fl_pid, SIGLOST, 1); */
  57. case -NFS4ERR_STALE_CLIENTID:
  58. nfs4_schedule_state_recovery(NFS_SERVER(inode)->nfs_client);
  59. goto out_err;
  60. }
  61. }
  62. return 0;
  63. out_err:
  64. return status;
  65. }
  66. static void nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
  67. {
  68. struct nfs_inode *nfsi = NFS_I(inode);
  69. struct nfs_open_context *ctx;
  70. struct nfs4_state *state;
  71. int err;
  72. again:
  73. spin_lock(&inode->i_lock);
  74. list_for_each_entry(ctx, &nfsi->open_files, list) {
  75. state = ctx->state;
  76. if (state == NULL)
  77. continue;
  78. if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
  79. continue;
  80. if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0)
  81. continue;
  82. get_nfs_open_context(ctx);
  83. spin_unlock(&inode->i_lock);
  84. err = nfs4_open_delegation_recall(ctx, state, stateid);
  85. if (err >= 0)
  86. err = nfs_delegation_claim_locks(ctx, state);
  87. put_nfs_open_context(ctx);
  88. if (err != 0)
  89. return;
  90. goto again;
  91. }
  92. spin_unlock(&inode->i_lock);
  93. }
  94. /*
  95. * Set up a delegation on an inode
  96. */
  97. void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
  98. {
  99. struct nfs_delegation *delegation = NFS_I(inode)->delegation;
  100. struct rpc_cred *oldcred;
  101. if (delegation == NULL)
  102. return;
  103. memcpy(delegation->stateid.data, res->delegation.data,
  104. sizeof(delegation->stateid.data));
  105. delegation->type = res->delegation_type;
  106. delegation->maxsize = res->maxsize;
  107. oldcred = delegation->cred;
  108. delegation->cred = get_rpccred(cred);
  109. delegation->flags &= ~NFS_DELEGATION_NEED_RECLAIM;
  110. NFS_I(inode)->delegation_state = delegation->type;
  111. smp_wmb();
  112. put_rpccred(oldcred);
  113. }
  114. /*
  115. * Set up a delegation on an inode
  116. */
  117. int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
  118. {
  119. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  120. struct nfs_inode *nfsi = NFS_I(inode);
  121. struct nfs_delegation *delegation;
  122. int status = 0;
  123. delegation = kmalloc(sizeof(*delegation), GFP_KERNEL);
  124. if (delegation == NULL)
  125. return -ENOMEM;
  126. memcpy(delegation->stateid.data, res->delegation.data,
  127. sizeof(delegation->stateid.data));
  128. delegation->type = res->delegation_type;
  129. delegation->maxsize = res->maxsize;
  130. delegation->change_attr = nfsi->change_attr;
  131. delegation->cred = get_rpccred(cred);
  132. delegation->inode = inode;
  133. spin_lock(&clp->cl_lock);
  134. if (rcu_dereference(nfsi->delegation) == NULL) {
  135. list_add_rcu(&delegation->super_list, &clp->cl_delegations);
  136. nfsi->delegation_state = delegation->type;
  137. rcu_assign_pointer(nfsi->delegation, delegation);
  138. delegation = NULL;
  139. } else {
  140. if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
  141. sizeof(delegation->stateid)) != 0 ||
  142. delegation->type != nfsi->delegation->type) {
  143. printk("%s: server %u.%u.%u.%u, handed out a duplicate delegation!\n",
  144. __FUNCTION__, NIPQUAD(clp->cl_addr.sin_addr));
  145. status = -EIO;
  146. }
  147. }
  148. /* Ensure we revalidate the attributes and page cache! */
  149. spin_lock(&inode->i_lock);
  150. nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
  151. spin_unlock(&inode->i_lock);
  152. spin_unlock(&clp->cl_lock);
  153. kfree(delegation);
  154. return status;
  155. }
  156. static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
  157. {
  158. int res = 0;
  159. res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid);
  160. nfs_free_delegation(delegation);
  161. return res;
  162. }
  163. /* Sync all data to disk upon delegation return */
  164. static void nfs_msync_inode(struct inode *inode)
  165. {
  166. filemap_fdatawrite(inode->i_mapping);
  167. nfs_wb_all(inode);
  168. filemap_fdatawait(inode->i_mapping);
  169. }
  170. /*
  171. * Basic procedure for returning a delegation to the server
  172. */
  173. static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
  174. {
  175. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  176. struct nfs_inode *nfsi = NFS_I(inode);
  177. nfs_msync_inode(inode);
  178. down_read(&clp->cl_sem);
  179. /* Guard against new delegated open calls */
  180. down_write(&nfsi->rwsem);
  181. nfs_delegation_claim_opens(inode, &delegation->stateid);
  182. up_write(&nfsi->rwsem);
  183. up_read(&clp->cl_sem);
  184. nfs_msync_inode(inode);
  185. return nfs_do_return_delegation(inode, delegation);
  186. }
  187. static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid)
  188. {
  189. struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
  190. if (delegation == NULL)
  191. goto nomatch;
  192. if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data,
  193. sizeof(delegation->stateid.data)) != 0)
  194. goto nomatch;
  195. list_del_rcu(&delegation->super_list);
  196. nfsi->delegation_state = 0;
  197. rcu_assign_pointer(nfsi->delegation, NULL);
  198. return delegation;
  199. nomatch:
  200. return NULL;
  201. }
  202. int nfs_inode_return_delegation(struct inode *inode)
  203. {
  204. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  205. struct nfs_inode *nfsi = NFS_I(inode);
  206. struct nfs_delegation *delegation;
  207. int err = 0;
  208. if (rcu_dereference(nfsi->delegation) != NULL) {
  209. spin_lock(&clp->cl_lock);
  210. delegation = nfs_detach_delegation_locked(nfsi, NULL);
  211. spin_unlock(&clp->cl_lock);
  212. if (delegation != NULL)
  213. err = __nfs_inode_return_delegation(inode, delegation);
  214. }
  215. return err;
  216. }
  217. /*
  218. * Return all delegations associated to a super block
  219. */
  220. void nfs_return_all_delegations(struct super_block *sb)
  221. {
  222. struct nfs_client *clp = NFS_SB(sb)->nfs_client;
  223. struct nfs_delegation *delegation;
  224. struct inode *inode;
  225. if (clp == NULL)
  226. return;
  227. restart:
  228. rcu_read_lock();
  229. list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
  230. if (delegation->inode->i_sb != sb)
  231. continue;
  232. inode = igrab(delegation->inode);
  233. if (inode == NULL)
  234. continue;
  235. spin_lock(&clp->cl_lock);
  236. delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
  237. spin_unlock(&clp->cl_lock);
  238. rcu_read_unlock();
  239. if (delegation != NULL)
  240. __nfs_inode_return_delegation(inode, delegation);
  241. iput(inode);
  242. goto restart;
  243. }
  244. rcu_read_unlock();
  245. }
  246. static int nfs_do_expire_all_delegations(void *ptr)
  247. {
  248. struct nfs_client *clp = ptr;
  249. struct nfs_delegation *delegation;
  250. struct inode *inode;
  251. allow_signal(SIGKILL);
  252. restart:
  253. if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) != 0)
  254. goto out;
  255. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0)
  256. goto out;
  257. rcu_read_lock();
  258. list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
  259. inode = igrab(delegation->inode);
  260. if (inode == NULL)
  261. continue;
  262. spin_lock(&clp->cl_lock);
  263. delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
  264. spin_unlock(&clp->cl_lock);
  265. rcu_read_unlock();
  266. if (delegation)
  267. __nfs_inode_return_delegation(inode, delegation);
  268. iput(inode);
  269. goto restart;
  270. }
  271. rcu_read_unlock();
  272. out:
  273. nfs_put_client(clp);
  274. module_put_and_exit(0);
  275. }
  276. void nfs_expire_all_delegations(struct nfs_client *clp)
  277. {
  278. struct task_struct *task;
  279. __module_get(THIS_MODULE);
  280. atomic_inc(&clp->cl_count);
  281. task = kthread_run(nfs_do_expire_all_delegations, clp,
  282. "%u.%u.%u.%u-delegreturn",
  283. NIPQUAD(clp->cl_addr.sin_addr));
  284. if (!IS_ERR(task))
  285. return;
  286. nfs_put_client(clp);
  287. module_put(THIS_MODULE);
  288. }
  289. /*
  290. * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
  291. */
  292. void nfs_handle_cb_pathdown(struct nfs_client *clp)
  293. {
  294. struct nfs_delegation *delegation;
  295. struct inode *inode;
  296. if (clp == NULL)
  297. return;
  298. restart:
  299. rcu_read_lock();
  300. list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
  301. inode = igrab(delegation->inode);
  302. if (inode == NULL)
  303. continue;
  304. spin_lock(&clp->cl_lock);
  305. delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
  306. spin_unlock(&clp->cl_lock);
  307. rcu_read_unlock();
  308. if (delegation != NULL)
  309. __nfs_inode_return_delegation(inode, delegation);
  310. iput(inode);
  311. goto restart;
  312. }
  313. rcu_read_unlock();
  314. }
  315. struct recall_threadargs {
  316. struct inode *inode;
  317. struct nfs_client *clp;
  318. const nfs4_stateid *stateid;
  319. struct completion started;
  320. int result;
  321. };
  322. static int recall_thread(void *data)
  323. {
  324. struct recall_threadargs *args = (struct recall_threadargs *)data;
  325. struct inode *inode = igrab(args->inode);
  326. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  327. struct nfs_inode *nfsi = NFS_I(inode);
  328. struct nfs_delegation *delegation;
  329. daemonize("nfsv4-delegreturn");
  330. nfs_msync_inode(inode);
  331. down_read(&clp->cl_sem);
  332. down_write(&nfsi->rwsem);
  333. spin_lock(&clp->cl_lock);
  334. delegation = nfs_detach_delegation_locked(nfsi, args->stateid);
  335. if (delegation != NULL)
  336. args->result = 0;
  337. else
  338. args->result = -ENOENT;
  339. spin_unlock(&clp->cl_lock);
  340. complete(&args->started);
  341. nfs_delegation_claim_opens(inode, args->stateid);
  342. up_write(&nfsi->rwsem);
  343. up_read(&clp->cl_sem);
  344. nfs_msync_inode(inode);
  345. if (delegation != NULL)
  346. nfs_do_return_delegation(inode, delegation);
  347. iput(inode);
  348. module_put_and_exit(0);
  349. }
  350. /*
  351. * Asynchronous delegation recall!
  352. */
  353. int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
  354. {
  355. struct recall_threadargs data = {
  356. .inode = inode,
  357. .stateid = stateid,
  358. };
  359. int status;
  360. init_completion(&data.started);
  361. __module_get(THIS_MODULE);
  362. status = kernel_thread(recall_thread, &data, CLONE_KERNEL);
  363. if (status < 0)
  364. goto out_module_put;
  365. wait_for_completion(&data.started);
  366. return data.result;
  367. out_module_put:
  368. module_put(THIS_MODULE);
  369. return status;
  370. }
  371. /*
  372. * Retrieve the inode associated with a delegation
  373. */
  374. struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
  375. {
  376. struct nfs_delegation *delegation;
  377. struct inode *res = NULL;
  378. rcu_read_lock();
  379. list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
  380. if (nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
  381. res = igrab(delegation->inode);
  382. break;
  383. }
  384. }
  385. rcu_read_unlock();
  386. return res;
  387. }
  388. /*
  389. * Mark all delegations as needing to be reclaimed
  390. */
  391. void nfs_delegation_mark_reclaim(struct nfs_client *clp)
  392. {
  393. struct nfs_delegation *delegation;
  394. rcu_read_lock();
  395. list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
  396. delegation->flags |= NFS_DELEGATION_NEED_RECLAIM;
  397. rcu_read_unlock();
  398. }
  399. /*
  400. * Reap all unclaimed delegations after reboot recovery is done
  401. */
  402. void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
  403. {
  404. struct nfs_delegation *delegation;
  405. restart:
  406. rcu_read_lock();
  407. list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
  408. if ((delegation->flags & NFS_DELEGATION_NEED_RECLAIM) == 0)
  409. continue;
  410. spin_lock(&clp->cl_lock);
  411. delegation = nfs_detach_delegation_locked(NFS_I(delegation->inode), NULL);
  412. spin_unlock(&clp->cl_lock);
  413. rcu_read_unlock();
  414. if (delegation != NULL)
  415. nfs_free_delegation(delegation);
  416. goto restart;
  417. }
  418. rcu_read_unlock();
  419. }
  420. int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
  421. {
  422. struct nfs_inode *nfsi = NFS_I(inode);
  423. struct nfs_delegation *delegation;
  424. int ret = 0;
  425. rcu_read_lock();
  426. delegation = rcu_dereference(nfsi->delegation);
  427. if (delegation != NULL) {
  428. memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
  429. ret = 1;
  430. }
  431. rcu_read_unlock();
  432. return ret;
  433. }