delegation.c 14 KB

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