delegation.c 14 KB

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