delegation.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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_inode *nfsi = NFS_I(inode);
  221. nfs_msync_inode(inode);
  222. /* Guard against new delegated open calls */
  223. down_write(&nfsi->rwsem);
  224. nfs_delegation_claim_opens(inode, &delegation->stateid);
  225. up_write(&nfsi->rwsem);
  226. nfs_msync_inode(inode);
  227. return nfs_do_return_delegation(inode, delegation, 1);
  228. }
  229. /*
  230. * Return all delegations that have been marked for return
  231. */
  232. static void nfs_client_return_marked_delegations(struct nfs_client *clp)
  233. {
  234. struct nfs_delegation *delegation;
  235. struct inode *inode;
  236. restart:
  237. rcu_read_lock();
  238. list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
  239. if (!test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
  240. continue;
  241. inode = nfs_delegation_grab_inode(delegation);
  242. if (inode == NULL)
  243. continue;
  244. spin_lock(&clp->cl_lock);
  245. delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
  246. spin_unlock(&clp->cl_lock);
  247. rcu_read_unlock();
  248. if (delegation != NULL)
  249. __nfs_inode_return_delegation(inode, delegation);
  250. iput(inode);
  251. goto restart;
  252. }
  253. rcu_read_unlock();
  254. }
  255. /*
  256. * This function returns the delegation without reclaiming opens
  257. * or protecting against delegation reclaims.
  258. * It is therefore really only safe to be called from
  259. * nfs4_clear_inode()
  260. */
  261. void nfs_inode_return_delegation_noreclaim(struct inode *inode)
  262. {
  263. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  264. struct nfs_inode *nfsi = NFS_I(inode);
  265. struct nfs_delegation *delegation;
  266. if (rcu_dereference(nfsi->delegation) != NULL) {
  267. spin_lock(&clp->cl_lock);
  268. delegation = nfs_detach_delegation_locked(nfsi, NULL);
  269. spin_unlock(&clp->cl_lock);
  270. if (delegation != NULL)
  271. nfs_do_return_delegation(inode, delegation, 0);
  272. }
  273. }
  274. int nfs_inode_return_delegation(struct inode *inode)
  275. {
  276. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  277. struct nfs_inode *nfsi = NFS_I(inode);
  278. struct nfs_delegation *delegation;
  279. int err = 0;
  280. if (rcu_dereference(nfsi->delegation) != NULL) {
  281. spin_lock(&clp->cl_lock);
  282. delegation = nfs_detach_delegation_locked(nfsi, NULL);
  283. spin_unlock(&clp->cl_lock);
  284. if (delegation != NULL)
  285. err = __nfs_inode_return_delegation(inode, delegation);
  286. }
  287. return err;
  288. }
  289. /*
  290. * Return all delegations associated to a super block
  291. */
  292. void nfs_super_return_all_delegations(struct super_block *sb)
  293. {
  294. struct nfs_client *clp = NFS_SB(sb)->nfs_client;
  295. struct nfs_delegation *delegation;
  296. if (clp == NULL)
  297. return;
  298. rcu_read_lock();
  299. list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
  300. spin_lock(&delegation->lock);
  301. if (delegation->inode != NULL && delegation->inode->i_sb == sb)
  302. set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
  303. spin_unlock(&delegation->lock);
  304. }
  305. rcu_read_unlock();
  306. nfs_client_return_marked_delegations(clp);
  307. }
  308. static void nfs_client_return_all_delegations(struct nfs_client *clp)
  309. {
  310. struct nfs_delegation *delegation;
  311. rcu_read_lock();
  312. list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
  313. set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
  314. rcu_read_unlock();
  315. nfs_client_return_marked_delegations(clp);
  316. }
  317. static int nfs_do_expire_all_delegations(void *ptr)
  318. {
  319. struct nfs_client *clp = ptr;
  320. allow_signal(SIGKILL);
  321. if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) != 0)
  322. goto out;
  323. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0)
  324. goto out;
  325. nfs_client_return_all_delegations(clp);
  326. out:
  327. nfs_put_client(clp);
  328. module_put_and_exit(0);
  329. }
  330. void nfs_expire_all_delegations(struct nfs_client *clp)
  331. {
  332. struct task_struct *task;
  333. __module_get(THIS_MODULE);
  334. atomic_inc(&clp->cl_count);
  335. task = kthread_run(nfs_do_expire_all_delegations, clp,
  336. "%s-delegreturn",
  337. rpc_peeraddr2str(clp->cl_rpcclient,
  338. RPC_DISPLAY_ADDR));
  339. if (!IS_ERR(task))
  340. return;
  341. nfs_put_client(clp);
  342. module_put(THIS_MODULE);
  343. }
  344. /*
  345. * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
  346. */
  347. void nfs_handle_cb_pathdown(struct nfs_client *clp)
  348. {
  349. if (clp == NULL)
  350. return;
  351. nfs_client_return_all_delegations(clp);
  352. }
  353. struct recall_threadargs {
  354. struct inode *inode;
  355. struct nfs_client *clp;
  356. const nfs4_stateid *stateid;
  357. struct completion started;
  358. int result;
  359. };
  360. static int recall_thread(void *data)
  361. {
  362. struct recall_threadargs *args = (struct recall_threadargs *)data;
  363. struct inode *inode = igrab(args->inode);
  364. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  365. struct nfs_inode *nfsi = NFS_I(inode);
  366. struct nfs_delegation *delegation;
  367. daemonize("nfsv4-delegreturn");
  368. nfs_msync_inode(inode);
  369. down_write(&nfsi->rwsem);
  370. spin_lock(&clp->cl_lock);
  371. delegation = nfs_detach_delegation_locked(nfsi, args->stateid);
  372. if (delegation != NULL)
  373. args->result = 0;
  374. else
  375. args->result = -ENOENT;
  376. spin_unlock(&clp->cl_lock);
  377. complete(&args->started);
  378. nfs_delegation_claim_opens(inode, args->stateid);
  379. up_write(&nfsi->rwsem);
  380. nfs_msync_inode(inode);
  381. if (delegation != NULL)
  382. nfs_do_return_delegation(inode, delegation, 1);
  383. iput(inode);
  384. module_put_and_exit(0);
  385. }
  386. /*
  387. * Asynchronous delegation recall!
  388. */
  389. int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
  390. {
  391. struct recall_threadargs data = {
  392. .inode = inode,
  393. .stateid = stateid,
  394. };
  395. int status;
  396. init_completion(&data.started);
  397. __module_get(THIS_MODULE);
  398. status = kernel_thread(recall_thread, &data, CLONE_KERNEL);
  399. if (status < 0)
  400. goto out_module_put;
  401. wait_for_completion(&data.started);
  402. return data.result;
  403. out_module_put:
  404. module_put(THIS_MODULE);
  405. return status;
  406. }
  407. /*
  408. * Retrieve the inode associated with a delegation
  409. */
  410. struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
  411. {
  412. struct nfs_delegation *delegation;
  413. struct inode *res = NULL;
  414. rcu_read_lock();
  415. list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
  416. spin_lock(&delegation->lock);
  417. if (delegation->inode != NULL &&
  418. nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
  419. res = igrab(delegation->inode);
  420. }
  421. spin_unlock(&delegation->lock);
  422. if (res != NULL)
  423. break;
  424. }
  425. rcu_read_unlock();
  426. return res;
  427. }
  428. /*
  429. * Mark all delegations as needing to be reclaimed
  430. */
  431. void nfs_delegation_mark_reclaim(struct nfs_client *clp)
  432. {
  433. struct nfs_delegation *delegation;
  434. rcu_read_lock();
  435. list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
  436. set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
  437. rcu_read_unlock();
  438. }
  439. /*
  440. * Reap all unclaimed delegations after reboot recovery is done
  441. */
  442. void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
  443. {
  444. struct nfs_delegation *delegation;
  445. struct inode *inode;
  446. restart:
  447. rcu_read_lock();
  448. list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
  449. if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0)
  450. continue;
  451. inode = nfs_delegation_grab_inode(delegation);
  452. if (inode == NULL)
  453. continue;
  454. spin_lock(&clp->cl_lock);
  455. delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
  456. spin_unlock(&clp->cl_lock);
  457. rcu_read_unlock();
  458. if (delegation != NULL)
  459. nfs_free_delegation(delegation);
  460. iput(inode);
  461. goto restart;
  462. }
  463. rcu_read_unlock();
  464. }
  465. int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
  466. {
  467. struct nfs_inode *nfsi = NFS_I(inode);
  468. struct nfs_delegation *delegation;
  469. int ret = 0;
  470. rcu_read_lock();
  471. delegation = rcu_dereference(nfsi->delegation);
  472. if (delegation != NULL) {
  473. memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
  474. ret = 1;
  475. }
  476. rcu_read_unlock();
  477. return ret;
  478. }