delegation.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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/slab.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/nfs4.h>
  16. #include <linux/nfs_fs.h>
  17. #include <linux/nfs_xdr.h>
  18. #include "nfs4_fs.h"
  19. #include "delegation.h"
  20. #include "internal.h"
  21. static void nfs_do_free_delegation(struct nfs_delegation *delegation)
  22. {
  23. if (delegation->cred)
  24. put_rpccred(delegation->cred);
  25. kfree(delegation);
  26. }
  27. static void nfs_free_delegation_callback(struct rcu_head *head)
  28. {
  29. struct nfs_delegation *delegation = container_of(head, struct nfs_delegation, rcu);
  30. nfs_do_free_delegation(delegation);
  31. }
  32. static void nfs_free_delegation(struct nfs_delegation *delegation)
  33. {
  34. call_rcu(&delegation->rcu, nfs_free_delegation_callback);
  35. }
  36. void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
  37. {
  38. set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
  39. }
  40. int nfs_have_delegation(struct inode *inode, fmode_t flags)
  41. {
  42. struct nfs_delegation *delegation;
  43. int ret = 0;
  44. flags &= FMODE_READ|FMODE_WRITE;
  45. rcu_read_lock();
  46. delegation = rcu_dereference(NFS_I(inode)->delegation);
  47. if (delegation != NULL && (delegation->type & flags) == flags) {
  48. nfs_mark_delegation_referenced(delegation);
  49. ret = 1;
  50. }
  51. rcu_read_unlock();
  52. return ret;
  53. }
  54. static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state)
  55. {
  56. struct inode *inode = state->inode;
  57. struct file_lock *fl;
  58. int status = 0;
  59. if (inode->i_flock == NULL)
  60. goto out;
  61. /* Protect inode->i_flock using the file locks lock */
  62. lock_flocks();
  63. for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
  64. if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
  65. continue;
  66. if (nfs_file_open_context(fl->fl_file) != ctx)
  67. continue;
  68. unlock_flocks();
  69. status = nfs4_lock_delegation_recall(state, fl);
  70. if (status < 0)
  71. goto out;
  72. lock_flocks();
  73. }
  74. unlock_flocks();
  75. out:
  76. return status;
  77. }
  78. static int nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
  79. {
  80. struct nfs_inode *nfsi = NFS_I(inode);
  81. struct nfs_open_context *ctx;
  82. struct nfs4_state *state;
  83. int err;
  84. again:
  85. spin_lock(&inode->i_lock);
  86. list_for_each_entry(ctx, &nfsi->open_files, list) {
  87. state = ctx->state;
  88. if (state == NULL)
  89. continue;
  90. if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
  91. continue;
  92. if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0)
  93. continue;
  94. get_nfs_open_context(ctx);
  95. spin_unlock(&inode->i_lock);
  96. err = nfs4_open_delegation_recall(ctx, state, stateid);
  97. if (err >= 0)
  98. err = nfs_delegation_claim_locks(ctx, state);
  99. put_nfs_open_context(ctx);
  100. if (err != 0)
  101. return err;
  102. goto again;
  103. }
  104. spin_unlock(&inode->i_lock);
  105. return 0;
  106. }
  107. /*
  108. * Set up a delegation on an inode
  109. */
  110. void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
  111. {
  112. struct nfs_delegation *delegation;
  113. struct rpc_cred *oldcred = NULL;
  114. rcu_read_lock();
  115. delegation = rcu_dereference(NFS_I(inode)->delegation);
  116. if (delegation != NULL) {
  117. spin_lock(&delegation->lock);
  118. if (delegation->inode != NULL) {
  119. memcpy(delegation->stateid.data, res->delegation.data,
  120. sizeof(delegation->stateid.data));
  121. delegation->type = res->delegation_type;
  122. delegation->maxsize = res->maxsize;
  123. oldcred = delegation->cred;
  124. delegation->cred = get_rpccred(cred);
  125. clear_bit(NFS_DELEGATION_NEED_RECLAIM,
  126. &delegation->flags);
  127. NFS_I(inode)->delegation_state = delegation->type;
  128. spin_unlock(&delegation->lock);
  129. put_rpccred(oldcred);
  130. rcu_read_unlock();
  131. } else {
  132. /* We appear to have raced with a delegation return. */
  133. spin_unlock(&delegation->lock);
  134. rcu_read_unlock();
  135. nfs_inode_set_delegation(inode, cred, res);
  136. }
  137. } else {
  138. rcu_read_unlock();
  139. }
  140. }
  141. static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
  142. {
  143. int res = 0;
  144. res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid, issync);
  145. nfs_free_delegation(delegation);
  146. return res;
  147. }
  148. static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
  149. {
  150. struct inode *inode = NULL;
  151. spin_lock(&delegation->lock);
  152. if (delegation->inode != NULL)
  153. inode = igrab(delegation->inode);
  154. spin_unlock(&delegation->lock);
  155. return inode;
  156. }
  157. static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi,
  158. const nfs4_stateid *stateid,
  159. struct nfs_client *clp)
  160. {
  161. struct nfs_delegation *delegation =
  162. rcu_dereference_protected(nfsi->delegation,
  163. lockdep_is_held(&clp->cl_lock));
  164. if (delegation == NULL)
  165. goto nomatch;
  166. spin_lock(&delegation->lock);
  167. if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data,
  168. sizeof(delegation->stateid.data)) != 0)
  169. goto nomatch_unlock;
  170. list_del_rcu(&delegation->super_list);
  171. delegation->inode = NULL;
  172. nfsi->delegation_state = 0;
  173. rcu_assign_pointer(nfsi->delegation, NULL);
  174. spin_unlock(&delegation->lock);
  175. return delegation;
  176. nomatch_unlock:
  177. spin_unlock(&delegation->lock);
  178. nomatch:
  179. return NULL;
  180. }
  181. /*
  182. * Set up a delegation on an inode
  183. */
  184. int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
  185. {
  186. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  187. struct nfs_inode *nfsi = NFS_I(inode);
  188. struct nfs_delegation *delegation, *old_delegation;
  189. struct nfs_delegation *freeme = NULL;
  190. int status = 0;
  191. delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
  192. if (delegation == NULL)
  193. return -ENOMEM;
  194. memcpy(delegation->stateid.data, res->delegation.data,
  195. sizeof(delegation->stateid.data));
  196. delegation->type = res->delegation_type;
  197. delegation->maxsize = res->maxsize;
  198. delegation->change_attr = nfsi->change_attr;
  199. delegation->cred = get_rpccred(cred);
  200. delegation->inode = inode;
  201. delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
  202. spin_lock_init(&delegation->lock);
  203. spin_lock(&clp->cl_lock);
  204. old_delegation = rcu_dereference_protected(nfsi->delegation,
  205. lockdep_is_held(&clp->cl_lock));
  206. if (old_delegation != NULL) {
  207. if (memcmp(&delegation->stateid, &old_delegation->stateid,
  208. sizeof(old_delegation->stateid)) == 0 &&
  209. delegation->type == old_delegation->type) {
  210. goto out;
  211. }
  212. /*
  213. * Deal with broken servers that hand out two
  214. * delegations for the same file.
  215. */
  216. dfprintk(FILE, "%s: server %s handed out "
  217. "a duplicate delegation!\n",
  218. __func__, clp->cl_hostname);
  219. if (delegation->type <= old_delegation->type) {
  220. freeme = delegation;
  221. delegation = NULL;
  222. goto out;
  223. }
  224. freeme = nfs_detach_delegation_locked(nfsi, NULL, clp);
  225. }
  226. list_add_rcu(&delegation->super_list, &clp->cl_delegations);
  227. nfsi->delegation_state = delegation->type;
  228. rcu_assign_pointer(nfsi->delegation, delegation);
  229. delegation = NULL;
  230. /* Ensure we revalidate the attributes and page cache! */
  231. spin_lock(&inode->i_lock);
  232. nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
  233. spin_unlock(&inode->i_lock);
  234. out:
  235. spin_unlock(&clp->cl_lock);
  236. if (delegation != NULL)
  237. nfs_free_delegation(delegation);
  238. if (freeme != NULL)
  239. nfs_do_return_delegation(inode, freeme, 0);
  240. return status;
  241. }
  242. /*
  243. * Basic procedure for returning a delegation to the server
  244. */
  245. static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
  246. {
  247. struct nfs_inode *nfsi = NFS_I(inode);
  248. int err;
  249. /*
  250. * Guard against new delegated open/lock/unlock calls and against
  251. * state recovery
  252. */
  253. down_write(&nfsi->rwsem);
  254. err = nfs_delegation_claim_opens(inode, &delegation->stateid);
  255. up_write(&nfsi->rwsem);
  256. if (err)
  257. goto out;
  258. err = nfs_do_return_delegation(inode, delegation, issync);
  259. out:
  260. return err;
  261. }
  262. /*
  263. * Return all delegations that have been marked for return
  264. */
  265. int nfs_client_return_marked_delegations(struct nfs_client *clp)
  266. {
  267. struct nfs_delegation *delegation;
  268. struct inode *inode;
  269. int err = 0;
  270. restart:
  271. rcu_read_lock();
  272. list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
  273. if (!test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
  274. continue;
  275. inode = nfs_delegation_grab_inode(delegation);
  276. if (inode == NULL)
  277. continue;
  278. spin_lock(&clp->cl_lock);
  279. delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL, clp);
  280. spin_unlock(&clp->cl_lock);
  281. rcu_read_unlock();
  282. if (delegation != NULL) {
  283. filemap_flush(inode->i_mapping);
  284. err = __nfs_inode_return_delegation(inode, delegation, 0);
  285. }
  286. iput(inode);
  287. if (!err)
  288. goto restart;
  289. set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
  290. return err;
  291. }
  292. rcu_read_unlock();
  293. return 0;
  294. }
  295. /*
  296. * This function returns the delegation without reclaiming opens
  297. * or protecting against delegation reclaims.
  298. * It is therefore really only safe to be called from
  299. * nfs4_clear_inode()
  300. */
  301. void nfs_inode_return_delegation_noreclaim(struct inode *inode)
  302. {
  303. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  304. struct nfs_inode *nfsi = NFS_I(inode);
  305. struct nfs_delegation *delegation;
  306. if (rcu_access_pointer(nfsi->delegation) != NULL) {
  307. spin_lock(&clp->cl_lock);
  308. delegation = nfs_detach_delegation_locked(nfsi, NULL, clp);
  309. spin_unlock(&clp->cl_lock);
  310. if (delegation != NULL)
  311. nfs_do_return_delegation(inode, delegation, 0);
  312. }
  313. }
  314. int nfs_inode_return_delegation(struct inode *inode)
  315. {
  316. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  317. struct nfs_inode *nfsi = NFS_I(inode);
  318. struct nfs_delegation *delegation;
  319. int err = 0;
  320. if (rcu_access_pointer(nfsi->delegation) != NULL) {
  321. spin_lock(&clp->cl_lock);
  322. delegation = nfs_detach_delegation_locked(nfsi, NULL, clp);
  323. spin_unlock(&clp->cl_lock);
  324. if (delegation != NULL) {
  325. nfs_wb_all(inode);
  326. err = __nfs_inode_return_delegation(inode, delegation, 1);
  327. }
  328. }
  329. return err;
  330. }
  331. static void nfs_mark_return_delegation(struct nfs_client *clp, struct nfs_delegation *delegation)
  332. {
  333. set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
  334. set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
  335. }
  336. /*
  337. * Return all delegations associated to a super block
  338. */
  339. void nfs_super_return_all_delegations(struct super_block *sb)
  340. {
  341. struct nfs_client *clp = NFS_SB(sb)->nfs_client;
  342. struct nfs_delegation *delegation;
  343. if (clp == NULL)
  344. return;
  345. rcu_read_lock();
  346. list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
  347. spin_lock(&delegation->lock);
  348. if (delegation->inode != NULL && delegation->inode->i_sb == sb)
  349. set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
  350. spin_unlock(&delegation->lock);
  351. }
  352. rcu_read_unlock();
  353. if (nfs_client_return_marked_delegations(clp) != 0)
  354. nfs4_schedule_state_manager(clp);
  355. }
  356. static
  357. void nfs_client_mark_return_all_delegation_types(struct nfs_client *clp, fmode_t flags)
  358. {
  359. struct nfs_delegation *delegation;
  360. rcu_read_lock();
  361. list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
  362. if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
  363. continue;
  364. if (delegation->type & flags)
  365. nfs_mark_return_delegation(clp, delegation);
  366. }
  367. rcu_read_unlock();
  368. }
  369. static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
  370. {
  371. nfs_client_mark_return_all_delegation_types(clp, FMODE_READ|FMODE_WRITE);
  372. }
  373. static void nfs_delegation_run_state_manager(struct nfs_client *clp)
  374. {
  375. if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
  376. nfs4_schedule_state_manager(clp);
  377. }
  378. void nfs_expire_all_delegation_types(struct nfs_client *clp, fmode_t flags)
  379. {
  380. nfs_client_mark_return_all_delegation_types(clp, flags);
  381. nfs_delegation_run_state_manager(clp);
  382. }
  383. void nfs_expire_all_delegations(struct nfs_client *clp)
  384. {
  385. nfs_expire_all_delegation_types(clp, FMODE_READ|FMODE_WRITE);
  386. }
  387. /*
  388. * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
  389. */
  390. void nfs_handle_cb_pathdown(struct nfs_client *clp)
  391. {
  392. if (clp == NULL)
  393. return;
  394. nfs_client_mark_return_all_delegations(clp);
  395. }
  396. static void nfs_client_mark_return_unreferenced_delegations(struct nfs_client *clp)
  397. {
  398. struct nfs_delegation *delegation;
  399. rcu_read_lock();
  400. list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
  401. if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
  402. continue;
  403. nfs_mark_return_delegation(clp, delegation);
  404. }
  405. rcu_read_unlock();
  406. }
  407. void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
  408. {
  409. nfs_client_mark_return_unreferenced_delegations(clp);
  410. nfs_delegation_run_state_manager(clp);
  411. }
  412. /*
  413. * Asynchronous delegation recall!
  414. */
  415. int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
  416. {
  417. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  418. struct nfs_delegation *delegation;
  419. rcu_read_lock();
  420. delegation = rcu_dereference(NFS_I(inode)->delegation);
  421. if (!clp->cl_mvops->validate_stateid(delegation, stateid)) {
  422. rcu_read_unlock();
  423. return -ENOENT;
  424. }
  425. nfs_mark_return_delegation(clp, delegation);
  426. rcu_read_unlock();
  427. nfs_delegation_run_state_manager(clp);
  428. return 0;
  429. }
  430. /*
  431. * Retrieve the inode associated with a delegation
  432. */
  433. struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
  434. {
  435. struct nfs_delegation *delegation;
  436. struct inode *res = NULL;
  437. rcu_read_lock();
  438. list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
  439. spin_lock(&delegation->lock);
  440. if (delegation->inode != NULL &&
  441. nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
  442. res = igrab(delegation->inode);
  443. }
  444. spin_unlock(&delegation->lock);
  445. if (res != NULL)
  446. break;
  447. }
  448. rcu_read_unlock();
  449. return res;
  450. }
  451. /*
  452. * Mark all delegations as needing to be reclaimed
  453. */
  454. void nfs_delegation_mark_reclaim(struct nfs_client *clp)
  455. {
  456. struct nfs_delegation *delegation;
  457. rcu_read_lock();
  458. list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
  459. set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
  460. rcu_read_unlock();
  461. }
  462. /*
  463. * Reap all unclaimed delegations after reboot recovery is done
  464. */
  465. void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
  466. {
  467. struct nfs_delegation *delegation;
  468. struct inode *inode;
  469. restart:
  470. rcu_read_lock();
  471. list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
  472. if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0)
  473. continue;
  474. inode = nfs_delegation_grab_inode(delegation);
  475. if (inode == NULL)
  476. continue;
  477. spin_lock(&clp->cl_lock);
  478. delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL, clp);
  479. spin_unlock(&clp->cl_lock);
  480. rcu_read_unlock();
  481. if (delegation != NULL)
  482. nfs_free_delegation(delegation);
  483. iput(inode);
  484. goto restart;
  485. }
  486. rcu_read_unlock();
  487. }
  488. int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
  489. {
  490. struct nfs_inode *nfsi = NFS_I(inode);
  491. struct nfs_delegation *delegation;
  492. int ret = 0;
  493. rcu_read_lock();
  494. delegation = rcu_dereference(nfsi->delegation);
  495. if (delegation != NULL) {
  496. memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
  497. ret = 1;
  498. }
  499. rcu_read_unlock();
  500. return ret;
  501. }