delegation.c 13 KB

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