delegation.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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 != NULL; 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. __func__, 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. clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
  110. NFS_I(inode)->delegation_state = delegation->type;
  111. smp_wmb();
  112. put_rpccred(oldcred);
  113. }
  114. static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
  115. {
  116. int res = 0;
  117. res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid, issync);
  118. nfs_free_delegation(delegation);
  119. return res;
  120. }
  121. static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
  122. {
  123. struct inode *inode = NULL;
  124. spin_lock(&delegation->lock);
  125. if (delegation->inode != NULL)
  126. inode = igrab(delegation->inode);
  127. spin_unlock(&delegation->lock);
  128. return inode;
  129. }
  130. static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid)
  131. {
  132. struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
  133. if (delegation == NULL)
  134. goto nomatch;
  135. spin_lock(&delegation->lock);
  136. if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data,
  137. sizeof(delegation->stateid.data)) != 0)
  138. goto nomatch_unlock;
  139. list_del_rcu(&delegation->super_list);
  140. delegation->inode = NULL;
  141. nfsi->delegation_state = 0;
  142. rcu_assign_pointer(nfsi->delegation, NULL);
  143. spin_unlock(&delegation->lock);
  144. return delegation;
  145. nomatch_unlock:
  146. spin_unlock(&delegation->lock);
  147. nomatch:
  148. return NULL;
  149. }
  150. /*
  151. * Set up a delegation on an inode
  152. */
  153. int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
  154. {
  155. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  156. struct nfs_inode *nfsi = NFS_I(inode);
  157. struct nfs_delegation *delegation;
  158. struct nfs_delegation *freeme = NULL;
  159. int status = 0;
  160. delegation = kmalloc(sizeof(*delegation), GFP_KERNEL);
  161. if (delegation == NULL)
  162. return -ENOMEM;
  163. memcpy(delegation->stateid.data, res->delegation.data,
  164. sizeof(delegation->stateid.data));
  165. delegation->type = res->delegation_type;
  166. delegation->maxsize = res->maxsize;
  167. delegation->change_attr = nfsi->change_attr;
  168. delegation->cred = get_rpccred(cred);
  169. delegation->inode = inode;
  170. spin_lock_init(&delegation->lock);
  171. spin_lock(&clp->cl_lock);
  172. if (rcu_dereference(nfsi->delegation) != NULL) {
  173. if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
  174. sizeof(delegation->stateid)) == 0 &&
  175. delegation->type == nfsi->delegation->type) {
  176. goto out;
  177. }
  178. /*
  179. * Deal with broken servers that hand out two
  180. * delegations for the same file.
  181. */
  182. dfprintk(FILE, "%s: server %s handed out "
  183. "a duplicate delegation!\n",
  184. __func__, clp->cl_hostname);
  185. if (delegation->type <= nfsi->delegation->type) {
  186. freeme = delegation;
  187. delegation = NULL;
  188. goto out;
  189. }
  190. freeme = nfs_detach_delegation_locked(nfsi, NULL);
  191. }
  192. list_add_rcu(&delegation->super_list, &clp->cl_delegations);
  193. nfsi->delegation_state = delegation->type;
  194. rcu_assign_pointer(nfsi->delegation, delegation);
  195. delegation = NULL;
  196. /* Ensure we revalidate the attributes and page cache! */
  197. spin_lock(&inode->i_lock);
  198. nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
  199. spin_unlock(&inode->i_lock);
  200. out:
  201. spin_unlock(&clp->cl_lock);
  202. if (delegation != NULL)
  203. nfs_free_delegation(delegation);
  204. if (freeme != NULL)
  205. nfs_do_return_delegation(inode, freeme, 0);
  206. return status;
  207. }
  208. /* Sync all data to disk upon delegation return */
  209. static void nfs_msync_inode(struct inode *inode)
  210. {
  211. filemap_fdatawrite(inode->i_mapping);
  212. nfs_wb_all(inode);
  213. filemap_fdatawait(inode->i_mapping);
  214. }
  215. /*
  216. * Basic procedure for returning a delegation to the server
  217. */
  218. static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
  219. {
  220. struct nfs_inode *nfsi = NFS_I(inode);
  221. nfs_msync_inode(inode);
  222. /* Guard against new delegated open calls */
  223. down_write(&nfsi->rwsem);
  224. nfs_delegation_claim_opens(inode, &delegation->stateid);
  225. up_write(&nfsi->rwsem);
  226. nfs_msync_inode(inode);
  227. return nfs_do_return_delegation(inode, delegation, 1);
  228. }
  229. /*
  230. * This function returns the delegation without reclaiming opens
  231. * or protecting against delegation reclaims.
  232. * It is therefore really only safe to be called from
  233. * nfs4_clear_inode()
  234. */
  235. void nfs_inode_return_delegation_noreclaim(struct inode *inode)
  236. {
  237. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  238. struct nfs_inode *nfsi = NFS_I(inode);
  239. struct nfs_delegation *delegation;
  240. if (rcu_dereference(nfsi->delegation) != NULL) {
  241. spin_lock(&clp->cl_lock);
  242. delegation = nfs_detach_delegation_locked(nfsi, NULL);
  243. spin_unlock(&clp->cl_lock);
  244. if (delegation != NULL)
  245. nfs_do_return_delegation(inode, delegation, 0);
  246. }
  247. }
  248. int nfs_inode_return_delegation(struct inode *inode)
  249. {
  250. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  251. struct nfs_inode *nfsi = NFS_I(inode);
  252. struct nfs_delegation *delegation;
  253. int err = 0;
  254. if (rcu_dereference(nfsi->delegation) != NULL) {
  255. spin_lock(&clp->cl_lock);
  256. delegation = nfs_detach_delegation_locked(nfsi, NULL);
  257. spin_unlock(&clp->cl_lock);
  258. if (delegation != NULL)
  259. err = __nfs_inode_return_delegation(inode, delegation);
  260. }
  261. return err;
  262. }
  263. /*
  264. * Return all delegations associated to a super block
  265. */
  266. void nfs_return_all_delegations(struct super_block *sb)
  267. {
  268. struct nfs_client *clp = NFS_SB(sb)->nfs_client;
  269. struct nfs_delegation *delegation;
  270. struct inode *inode;
  271. if (clp == NULL)
  272. return;
  273. restart:
  274. rcu_read_lock();
  275. list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
  276. inode = NULL;
  277. spin_lock(&delegation->lock);
  278. if (delegation->inode != NULL && delegation->inode->i_sb == sb)
  279. inode = igrab(delegation->inode);
  280. spin_unlock(&delegation->lock);
  281. if (inode == NULL)
  282. continue;
  283. spin_lock(&clp->cl_lock);
  284. delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
  285. spin_unlock(&clp->cl_lock);
  286. rcu_read_unlock();
  287. if (delegation != NULL)
  288. __nfs_inode_return_delegation(inode, delegation);
  289. iput(inode);
  290. goto restart;
  291. }
  292. rcu_read_unlock();
  293. }
  294. static int nfs_do_expire_all_delegations(void *ptr)
  295. {
  296. struct nfs_client *clp = ptr;
  297. struct nfs_delegation *delegation;
  298. struct inode *inode;
  299. allow_signal(SIGKILL);
  300. restart:
  301. if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) != 0)
  302. goto out;
  303. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0)
  304. goto out;
  305. rcu_read_lock();
  306. list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
  307. inode = nfs_delegation_grab_inode(delegation);
  308. if (inode == NULL)
  309. continue;
  310. spin_lock(&clp->cl_lock);
  311. delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
  312. spin_unlock(&clp->cl_lock);
  313. rcu_read_unlock();
  314. if (delegation)
  315. __nfs_inode_return_delegation(inode, delegation);
  316. iput(inode);
  317. goto restart;
  318. }
  319. rcu_read_unlock();
  320. out:
  321. nfs_put_client(clp);
  322. module_put_and_exit(0);
  323. }
  324. void nfs_expire_all_delegations(struct nfs_client *clp)
  325. {
  326. struct task_struct *task;
  327. __module_get(THIS_MODULE);
  328. atomic_inc(&clp->cl_count);
  329. task = kthread_run(nfs_do_expire_all_delegations, clp,
  330. "%s-delegreturn",
  331. rpc_peeraddr2str(clp->cl_rpcclient,
  332. RPC_DISPLAY_ADDR));
  333. if (!IS_ERR(task))
  334. return;
  335. nfs_put_client(clp);
  336. module_put(THIS_MODULE);
  337. }
  338. /*
  339. * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
  340. */
  341. void nfs_handle_cb_pathdown(struct nfs_client *clp)
  342. {
  343. struct nfs_delegation *delegation;
  344. struct inode *inode;
  345. if (clp == NULL)
  346. return;
  347. restart:
  348. rcu_read_lock();
  349. list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
  350. inode = nfs_delegation_grab_inode(delegation);
  351. if (inode == NULL)
  352. continue;
  353. spin_lock(&clp->cl_lock);
  354. delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
  355. spin_unlock(&clp->cl_lock);
  356. rcu_read_unlock();
  357. if (delegation != NULL)
  358. __nfs_inode_return_delegation(inode, delegation);
  359. iput(inode);
  360. goto restart;
  361. }
  362. rcu_read_unlock();
  363. }
  364. struct recall_threadargs {
  365. struct inode *inode;
  366. struct nfs_client *clp;
  367. const nfs4_stateid *stateid;
  368. struct completion started;
  369. int result;
  370. };
  371. static int recall_thread(void *data)
  372. {
  373. struct recall_threadargs *args = (struct recall_threadargs *)data;
  374. struct inode *inode = igrab(args->inode);
  375. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  376. struct nfs_inode *nfsi = NFS_I(inode);
  377. struct nfs_delegation *delegation;
  378. daemonize("nfsv4-delegreturn");
  379. nfs_msync_inode(inode);
  380. down_write(&nfsi->rwsem);
  381. spin_lock(&clp->cl_lock);
  382. delegation = nfs_detach_delegation_locked(nfsi, args->stateid);
  383. if (delegation != NULL)
  384. args->result = 0;
  385. else
  386. args->result = -ENOENT;
  387. spin_unlock(&clp->cl_lock);
  388. complete(&args->started);
  389. nfs_delegation_claim_opens(inode, args->stateid);
  390. up_write(&nfsi->rwsem);
  391. nfs_msync_inode(inode);
  392. if (delegation != NULL)
  393. nfs_do_return_delegation(inode, delegation, 1);
  394. iput(inode);
  395. module_put_and_exit(0);
  396. }
  397. /*
  398. * Asynchronous delegation recall!
  399. */
  400. int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
  401. {
  402. struct recall_threadargs data = {
  403. .inode = inode,
  404. .stateid = stateid,
  405. };
  406. int status;
  407. init_completion(&data.started);
  408. __module_get(THIS_MODULE);
  409. status = kernel_thread(recall_thread, &data, CLONE_KERNEL);
  410. if (status < 0)
  411. goto out_module_put;
  412. wait_for_completion(&data.started);
  413. return data.result;
  414. out_module_put:
  415. module_put(THIS_MODULE);
  416. return status;
  417. }
  418. /*
  419. * Retrieve the inode associated with a delegation
  420. */
  421. struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
  422. {
  423. struct nfs_delegation *delegation;
  424. struct inode *res = NULL;
  425. rcu_read_lock();
  426. list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
  427. spin_lock(&delegation->lock);
  428. if (delegation->inode != NULL &&
  429. nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
  430. res = igrab(delegation->inode);
  431. }
  432. spin_unlock(&delegation->lock);
  433. if (res != NULL)
  434. break;
  435. }
  436. rcu_read_unlock();
  437. return res;
  438. }
  439. /*
  440. * Mark all delegations as needing to be reclaimed
  441. */
  442. void nfs_delegation_mark_reclaim(struct nfs_client *clp)
  443. {
  444. struct nfs_delegation *delegation;
  445. rcu_read_lock();
  446. list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
  447. set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
  448. rcu_read_unlock();
  449. }
  450. /*
  451. * Reap all unclaimed delegations after reboot recovery is done
  452. */
  453. void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
  454. {
  455. struct nfs_delegation *delegation;
  456. struct inode *inode;
  457. restart:
  458. rcu_read_lock();
  459. list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
  460. if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0)
  461. continue;
  462. inode = nfs_delegation_grab_inode(delegation);
  463. if (inode == NULL)
  464. continue;
  465. spin_lock(&clp->cl_lock);
  466. delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
  467. spin_unlock(&clp->cl_lock);
  468. rcu_read_unlock();
  469. if (delegation != NULL)
  470. nfs_free_delegation(delegation);
  471. iput(inode);
  472. goto restart;
  473. }
  474. rcu_read_unlock();
  475. }
  476. int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
  477. {
  478. struct nfs_inode *nfsi = NFS_I(inode);
  479. struct nfs_delegation *delegation;
  480. int ret = 0;
  481. rcu_read_lock();
  482. delegation = rcu_dereference(nfsi->delegation);
  483. if (delegation != NULL) {
  484. memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
  485. ret = 1;
  486. }
  487. rcu_read_unlock();
  488. return ret;
  489. }