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;
  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. continue;
  69. switch (status) {
  70. default:
  71. printk(KERN_ERR "%s: unhandled error %d.\n",
  72. __func__, status);
  73. case -NFS4ERR_EXPIRED:
  74. /* kill_proc(fl->fl_pid, SIGLOST, 1); */
  75. case -NFS4ERR_STALE_CLIENTID:
  76. nfs4_schedule_state_recovery(NFS_SERVER(inode)->nfs_client);
  77. goto out_err;
  78. }
  79. }
  80. return 0;
  81. out_err:
  82. return status;
  83. }
  84. static void nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
  85. {
  86. struct nfs_inode *nfsi = NFS_I(inode);
  87. struct nfs_open_context *ctx;
  88. struct nfs4_state *state;
  89. int err;
  90. again:
  91. spin_lock(&inode->i_lock);
  92. list_for_each_entry(ctx, &nfsi->open_files, list) {
  93. state = ctx->state;
  94. if (state == NULL)
  95. continue;
  96. if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
  97. continue;
  98. if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0)
  99. continue;
  100. get_nfs_open_context(ctx);
  101. spin_unlock(&inode->i_lock);
  102. err = nfs4_open_delegation_recall(ctx, state, stateid);
  103. if (err >= 0)
  104. err = nfs_delegation_claim_locks(ctx, state);
  105. put_nfs_open_context(ctx);
  106. if (err != 0)
  107. return;
  108. goto again;
  109. }
  110. spin_unlock(&inode->i_lock);
  111. }
  112. /*
  113. * Set up a delegation on an inode
  114. */
  115. void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
  116. {
  117. struct nfs_delegation *delegation = NFS_I(inode)->delegation;
  118. struct rpc_cred *oldcred;
  119. if (delegation == NULL)
  120. return;
  121. memcpy(delegation->stateid.data, res->delegation.data,
  122. sizeof(delegation->stateid.data));
  123. delegation->type = res->delegation_type;
  124. delegation->maxsize = res->maxsize;
  125. oldcred = delegation->cred;
  126. delegation->cred = get_rpccred(cred);
  127. clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
  128. NFS_I(inode)->delegation_state = delegation->type;
  129. smp_wmb();
  130. put_rpccred(oldcred);
  131. }
  132. static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
  133. {
  134. int res = 0;
  135. res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid, issync);
  136. nfs_free_delegation(delegation);
  137. return res;
  138. }
  139. static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
  140. {
  141. struct inode *inode = NULL;
  142. spin_lock(&delegation->lock);
  143. if (delegation->inode != NULL)
  144. inode = igrab(delegation->inode);
  145. spin_unlock(&delegation->lock);
  146. return inode;
  147. }
  148. static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid)
  149. {
  150. struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
  151. if (delegation == NULL)
  152. goto nomatch;
  153. spin_lock(&delegation->lock);
  154. if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data,
  155. sizeof(delegation->stateid.data)) != 0)
  156. goto nomatch_unlock;
  157. list_del_rcu(&delegation->super_list);
  158. delegation->inode = NULL;
  159. nfsi->delegation_state = 0;
  160. rcu_assign_pointer(nfsi->delegation, NULL);
  161. spin_unlock(&delegation->lock);
  162. return delegation;
  163. nomatch_unlock:
  164. spin_unlock(&delegation->lock);
  165. nomatch:
  166. return NULL;
  167. }
  168. /*
  169. * Set up a delegation on an inode
  170. */
  171. int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
  172. {
  173. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  174. struct nfs_inode *nfsi = NFS_I(inode);
  175. struct nfs_delegation *delegation;
  176. struct nfs_delegation *freeme = NULL;
  177. int status = 0;
  178. delegation = kmalloc(sizeof(*delegation), GFP_KERNEL);
  179. if (delegation == NULL)
  180. return -ENOMEM;
  181. memcpy(delegation->stateid.data, res->delegation.data,
  182. sizeof(delegation->stateid.data));
  183. delegation->type = res->delegation_type;
  184. delegation->maxsize = res->maxsize;
  185. delegation->change_attr = nfsi->change_attr;
  186. delegation->cred = get_rpccred(cred);
  187. delegation->inode = inode;
  188. delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
  189. spin_lock_init(&delegation->lock);
  190. spin_lock(&clp->cl_lock);
  191. if (rcu_dereference(nfsi->delegation) != NULL) {
  192. if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
  193. sizeof(delegation->stateid)) == 0 &&
  194. delegation->type == nfsi->delegation->type) {
  195. goto out;
  196. }
  197. /*
  198. * Deal with broken servers that hand out two
  199. * delegations for the same file.
  200. */
  201. dfprintk(FILE, "%s: server %s handed out "
  202. "a duplicate delegation!\n",
  203. __func__, clp->cl_hostname);
  204. if (delegation->type <= nfsi->delegation->type) {
  205. freeme = delegation;
  206. delegation = NULL;
  207. goto out;
  208. }
  209. freeme = nfs_detach_delegation_locked(nfsi, NULL);
  210. }
  211. list_add_rcu(&delegation->super_list, &clp->cl_delegations);
  212. nfsi->delegation_state = delegation->type;
  213. rcu_assign_pointer(nfsi->delegation, delegation);
  214. delegation = NULL;
  215. /* Ensure we revalidate the attributes and page cache! */
  216. spin_lock(&inode->i_lock);
  217. nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
  218. spin_unlock(&inode->i_lock);
  219. out:
  220. spin_unlock(&clp->cl_lock);
  221. if (delegation != NULL)
  222. nfs_free_delegation(delegation);
  223. if (freeme != NULL)
  224. nfs_do_return_delegation(inode, freeme, 0);
  225. return status;
  226. }
  227. /* Sync all data to disk upon delegation return */
  228. static void nfs_msync_inode(struct inode *inode)
  229. {
  230. filemap_fdatawrite(inode->i_mapping);
  231. nfs_wb_all(inode);
  232. filemap_fdatawait(inode->i_mapping);
  233. }
  234. /*
  235. * Basic procedure for returning a delegation to the server
  236. */
  237. static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
  238. {
  239. struct nfs_inode *nfsi = NFS_I(inode);
  240. nfs_msync_inode(inode);
  241. /* Guard against new delegated open calls */
  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. }