delegation.c 12 KB

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