delegation.c 12 KB

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