delegation.c 14 KB

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