delegation.c 14 KB

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