delegation.c 14 KB

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