delegation.c 14 KB

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