delegation.c 15 KB

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