delegation.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/fs/nfs/delegation.c
  4. *
  5. * Copyright (C) 2004 Trond Myklebust
  6. *
  7. * NFS file delegation management
  8. *
  9. */
  10. #include <linux/completion.h>
  11. #include <linux/kthread.h>
  12. #include <linux/module.h>
  13. #include <linux/sched.h>
  14. #include <linux/slab.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/iversion.h>
  17. #include <linux/nfs4.h>
  18. #include <linux/nfs_fs.h>
  19. #include <linux/nfs_xdr.h>
  20. #include "nfs4_fs.h"
  21. #include "nfs4session.h"
  22. #include "delegation.h"
  23. #include "internal.h"
  24. #include "nfs4trace.h"
  25. #define NFS_DEFAULT_DELEGATION_WATERMARK (5000U)
  26. static unsigned nfs_delegation_watermark = NFS_DEFAULT_DELEGATION_WATERMARK;
  27. module_param_named(delegation_watermark, nfs_delegation_watermark, uint, 0644);
  28. bool directory_delegations = true;
  29. module_param(directory_delegations, bool, 0644);
  30. MODULE_PARM_DESC(directory_delegations,
  31. "Enable the use of directory delegations, defaults to on.");
  32. static struct hlist_head *nfs_delegation_hash(struct nfs_server *server,
  33. const struct nfs_fh *fhandle)
  34. {
  35. return server->delegation_hash_table +
  36. (nfs_fhandle_hash(fhandle) & server->delegation_hash_mask);
  37. }
  38. static void __nfs_free_delegation(struct nfs_delegation *delegation)
  39. {
  40. put_cred(delegation->cred);
  41. delegation->cred = NULL;
  42. kfree_rcu(delegation, rcu);
  43. }
  44. static void nfs_mark_delegation_revoked(struct nfs_server *server,
  45. struct nfs_delegation *delegation)
  46. {
  47. bool put_ref = false;
  48. if (test_and_set_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
  49. return;
  50. delegation->stateid.type = NFS4_INVALID_STATEID_TYPE;
  51. atomic_long_dec(&server->nr_active_delegations);
  52. if (!test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
  53. nfs_clear_verifier_delegated(delegation->inode);
  54. spin_lock(&server->delegations_lock);
  55. if (!list_empty(&delegation->entry)) {
  56. list_del_init(&delegation->entry);
  57. put_ref = true;
  58. }
  59. spin_unlock(&server->delegations_lock);
  60. if (put_ref)
  61. nfs_put_delegation(delegation);
  62. }
  63. void nfs_put_delegation(struct nfs_delegation *delegation)
  64. {
  65. if (refcount_dec_and_test(&delegation->refcount))
  66. __nfs_free_delegation(delegation);
  67. }
  68. /**
  69. * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
  70. * @delegation: delegation to process
  71. *
  72. */
  73. void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
  74. {
  75. set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
  76. }
  77. static void nfs_mark_return_delegation(struct nfs_server *server,
  78. struct nfs_delegation *delegation)
  79. {
  80. spin_lock(&server->delegations_lock);
  81. if (list_empty(&delegation->entry))
  82. refcount_inc(&delegation->refcount);
  83. list_move_tail(&delegation->entry, &server->delegations_return);
  84. spin_unlock(&server->delegations_lock);
  85. set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
  86. }
  87. static bool nfs4_is_valid_delegation(const struct nfs_delegation *delegation,
  88. fmode_t type)
  89. {
  90. if (delegation != NULL && (delegation->type & type) == type &&
  91. !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
  92. !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
  93. return true;
  94. return false;
  95. }
  96. struct nfs_delegation *nfs4_get_valid_delegation(const struct inode *inode)
  97. {
  98. struct nfs_delegation *delegation;
  99. rcu_read_lock();
  100. delegation = rcu_dereference(NFS_I(inode)->delegation);
  101. if (!nfs4_is_valid_delegation(delegation, 0) ||
  102. !refcount_inc_not_zero(&delegation->refcount))
  103. delegation = NULL;
  104. rcu_read_unlock();
  105. return delegation;
  106. }
  107. static int nfs4_do_check_delegation(struct inode *inode, fmode_t type,
  108. int flags, bool mark)
  109. {
  110. struct nfs_delegation *delegation;
  111. int ret = 0;
  112. type &= FMODE_READ|FMODE_WRITE;
  113. rcu_read_lock();
  114. delegation = rcu_dereference(NFS_I(inode)->delegation);
  115. if (nfs4_is_valid_delegation(delegation, type)) {
  116. if (mark)
  117. nfs_mark_delegation_referenced(delegation);
  118. ret = 1;
  119. if ((flags & NFS_DELEGATION_FLAG_TIME) &&
  120. !test_bit(NFS_DELEGATION_DELEGTIME, &delegation->flags))
  121. ret = 0;
  122. }
  123. rcu_read_unlock();
  124. return ret;
  125. }
  126. /**
  127. * nfs4_have_delegation - check if inode has a delegation, mark it
  128. * NFS_DELEGATION_REFERENCED if there is one.
  129. * @inode: inode to check
  130. * @type: delegation types to check for
  131. * @flags: various modifiers
  132. *
  133. * Returns one if inode has the indicated delegation, otherwise zero.
  134. */
  135. int nfs4_have_delegation(struct inode *inode, fmode_t type, int flags)
  136. {
  137. if (S_ISDIR(inode->i_mode) && !directory_delegations)
  138. nfs4_inode_set_return_delegation_on_close(inode);
  139. return nfs4_do_check_delegation(inode, type, flags, true);
  140. }
  141. /*
  142. * nfs4_check_delegation - check if inode has a delegation, do not mark
  143. * NFS_DELEGATION_REFERENCED if it has one.
  144. */
  145. int nfs4_check_delegation(struct inode *inode, fmode_t type)
  146. {
  147. return nfs4_do_check_delegation(inode, type, 0, false);
  148. }
  149. static int nfs_delegation_claim_locks(struct nfs4_state *state, const nfs4_stateid *stateid)
  150. {
  151. struct inode *inode = state->inode;
  152. struct file_lock *fl;
  153. struct file_lock_context *flctx = locks_inode_context(inode);
  154. struct list_head *list;
  155. int status = 0;
  156. if (flctx == NULL)
  157. goto out;
  158. list = &flctx->flc_posix;
  159. spin_lock(&flctx->flc_lock);
  160. restart:
  161. for_each_file_lock(fl, list) {
  162. if (nfs_file_open_context(fl->c.flc_file)->state != state)
  163. continue;
  164. spin_unlock(&flctx->flc_lock);
  165. status = nfs4_lock_delegation_recall(fl, state, stateid);
  166. if (status < 0)
  167. goto out;
  168. spin_lock(&flctx->flc_lock);
  169. }
  170. if (list == &flctx->flc_posix) {
  171. list = &flctx->flc_flock;
  172. goto restart;
  173. }
  174. spin_unlock(&flctx->flc_lock);
  175. out:
  176. return status;
  177. }
  178. static int nfs_delegation_claim_opens(struct inode *inode,
  179. const nfs4_stateid *stateid, fmode_t type)
  180. {
  181. struct nfs_inode *nfsi = NFS_I(inode);
  182. struct nfs_open_context *ctx;
  183. struct nfs4_state_owner *sp;
  184. struct nfs4_state *state;
  185. int err;
  186. again:
  187. rcu_read_lock();
  188. list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
  189. state = ctx->state;
  190. if (state == NULL)
  191. continue;
  192. if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
  193. continue;
  194. if (!nfs4_valid_open_stateid(state))
  195. continue;
  196. if (!nfs4_stateid_match(&state->stateid, stateid))
  197. continue;
  198. if (!get_nfs_open_context(ctx))
  199. continue;
  200. rcu_read_unlock();
  201. sp = state->owner;
  202. /* Block nfs4_proc_unlck */
  203. mutex_lock(&sp->so_delegreturn_mutex);
  204. err = nfs4_open_delegation_recall(ctx, state, stateid);
  205. if (!err)
  206. err = nfs_delegation_claim_locks(state, stateid);
  207. mutex_unlock(&sp->so_delegreturn_mutex);
  208. put_nfs_open_context(ctx);
  209. if (err != 0)
  210. return err;
  211. goto again;
  212. }
  213. rcu_read_unlock();
  214. return 0;
  215. }
  216. /**
  217. * nfs_inode_reclaim_delegation - process a delegation reclaim request
  218. * @inode: inode to process
  219. * @cred: credential to use for request
  220. * @type: delegation type
  221. * @stateid: delegation stateid
  222. * @pagemod_limit: write delegation "space_limit"
  223. * @deleg_type: raw delegation type
  224. *
  225. */
  226. void nfs_inode_reclaim_delegation(struct inode *inode, const struct cred *cred,
  227. fmode_t type, const nfs4_stateid *stateid,
  228. unsigned long pagemod_limit, u32 deleg_type)
  229. {
  230. struct nfs_delegation *delegation;
  231. const struct cred *oldcred = NULL;
  232. rcu_read_lock();
  233. delegation = rcu_dereference(NFS_I(inode)->delegation);
  234. if (!delegation) {
  235. rcu_read_unlock();
  236. nfs_inode_set_delegation(inode, cred, type, stateid,
  237. pagemod_limit, deleg_type);
  238. return;
  239. }
  240. spin_lock(&delegation->lock);
  241. nfs4_stateid_copy(&delegation->stateid, stateid);
  242. delegation->type = type;
  243. delegation->pagemod_limit = pagemod_limit;
  244. oldcred = delegation->cred;
  245. delegation->cred = get_cred(cred);
  246. switch (deleg_type) {
  247. case NFS4_OPEN_DELEGATE_READ_ATTRS_DELEG:
  248. case NFS4_OPEN_DELEGATE_WRITE_ATTRS_DELEG:
  249. set_bit(NFS_DELEGATION_DELEGTIME, &delegation->flags);
  250. break;
  251. default:
  252. clear_bit(NFS_DELEGATION_DELEGTIME, &delegation->flags);
  253. }
  254. clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
  255. if (test_and_clear_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
  256. atomic_long_inc(&NFS_SERVER(inode)->nr_active_delegations);
  257. spin_unlock(&delegation->lock);
  258. rcu_read_unlock();
  259. put_cred(oldcred);
  260. trace_nfs4_reclaim_delegation(inode, type);
  261. }
  262. static int nfs_do_return_delegation(struct inode *inode,
  263. struct nfs_delegation *delegation,
  264. int issync)
  265. {
  266. const struct cred *cred;
  267. int res = 0;
  268. if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
  269. spin_lock(&delegation->lock);
  270. cred = get_cred(delegation->cred);
  271. spin_unlock(&delegation->lock);
  272. res = nfs4_proc_delegreturn(inode, cred, &delegation->stateid,
  273. delegation, issync);
  274. put_cred(cred);
  275. }
  276. return res;
  277. }
  278. static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
  279. {
  280. struct inode *inode = NULL;
  281. spin_lock(&delegation->lock);
  282. if (delegation->inode != NULL)
  283. inode = igrab(delegation->inode);
  284. spin_unlock(&delegation->lock);
  285. return inode;
  286. }
  287. static struct nfs_delegation *
  288. nfs_start_delegation_return(struct nfs_inode *nfsi)
  289. {
  290. struct nfs_delegation *delegation;
  291. bool return_now = false;
  292. rcu_read_lock();
  293. delegation = rcu_dereference(nfsi->delegation);
  294. if (!delegation || !refcount_inc_not_zero(&delegation->refcount)) {
  295. rcu_read_unlock();
  296. return NULL;
  297. }
  298. rcu_read_unlock();
  299. spin_lock(&delegation->lock);
  300. if (delegation->inode &&
  301. !test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
  302. return_now = true;
  303. spin_unlock(&delegation->lock);
  304. if (!return_now) {
  305. nfs_put_delegation(delegation);
  306. return NULL;
  307. }
  308. nfs_clear_verifier_delegated(&nfsi->vfs_inode);
  309. return delegation;
  310. }
  311. static bool
  312. nfs_detach_delegations_locked(struct nfs_inode *nfsi,
  313. struct nfs_delegation *delegation,
  314. struct nfs_client *clp)
  315. {
  316. lockdep_assert_held(&clp->cl_lock);
  317. trace_nfs4_detach_delegation(&nfsi->vfs_inode, delegation->type);
  318. spin_lock(&delegation->lock);
  319. if (!delegation->inode) {
  320. spin_unlock(&delegation->lock);
  321. return false;
  322. }
  323. hlist_del_init_rcu(&delegation->hash);
  324. list_del_rcu(&delegation->super_list);
  325. delegation->inode = NULL;
  326. rcu_assign_pointer(nfsi->delegation, NULL);
  327. spin_unlock(&delegation->lock);
  328. clear_bit(NFS_INO_REQ_DIR_DELEG, &nfsi->flags);
  329. return true;
  330. }
  331. static bool nfs_detach_delegation(struct nfs_inode *nfsi,
  332. struct nfs_delegation *delegation,
  333. struct nfs_server *server)
  334. {
  335. struct nfs_client *clp = server->nfs_client;
  336. struct nfs_delegation *deleg_cur;
  337. bool ret = false;
  338. spin_lock(&clp->cl_lock);
  339. deleg_cur = rcu_dereference_protected(nfsi->delegation,
  340. lockdep_is_held(&clp->cl_lock));
  341. if (delegation == deleg_cur)
  342. ret = nfs_detach_delegations_locked(nfsi, delegation, clp);
  343. spin_unlock(&clp->cl_lock);
  344. return ret;
  345. }
  346. static void
  347. nfs_update_delegation_cred(struct nfs_delegation *delegation,
  348. const struct cred *cred)
  349. {
  350. const struct cred *old;
  351. if (cred_fscmp(delegation->cred, cred) != 0) {
  352. old = xchg(&delegation->cred, get_cred(cred));
  353. put_cred(old);
  354. }
  355. }
  356. static void
  357. nfs_update_inplace_delegation(struct nfs_server *server,
  358. struct nfs_delegation *delegation,
  359. const struct nfs_delegation *update)
  360. {
  361. if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) {
  362. delegation->stateid.seqid = update->stateid.seqid;
  363. smp_wmb();
  364. delegation->type = update->type;
  365. delegation->pagemod_limit = update->pagemod_limit;
  366. if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
  367. delegation->change_attr = update->change_attr;
  368. nfs_update_delegation_cred(delegation, update->cred);
  369. /* smp_mb__before_atomic() is implicit due to xchg() */
  370. clear_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
  371. atomic_long_inc(&server->nr_active_delegations);
  372. }
  373. }
  374. }
  375. /**
  376. * nfs_inode_set_delegation - set up a delegation on an inode
  377. * @inode: inode to which delegation applies
  378. * @cred: cred to use for subsequent delegation processing
  379. * @type: delegation type
  380. * @stateid: delegation stateid
  381. * @pagemod_limit: write delegation "space_limit"
  382. * @deleg_type: raw delegation type
  383. *
  384. * Returns zero on success, or a negative errno value.
  385. */
  386. int nfs_inode_set_delegation(struct inode *inode, const struct cred *cred,
  387. fmode_t type, const nfs4_stateid *stateid,
  388. unsigned long pagemod_limit, u32 deleg_type)
  389. {
  390. struct nfs_server *server = NFS_SERVER(inode);
  391. struct nfs_client *clp = server->nfs_client;
  392. struct nfs_inode *nfsi = NFS_I(inode);
  393. struct nfs_delegation *delegation, *old_delegation;
  394. struct nfs_delegation *freeme = NULL;
  395. int status = 0;
  396. delegation = kmalloc_obj(*delegation, GFP_KERNEL_ACCOUNT);
  397. if (delegation == NULL)
  398. return -ENOMEM;
  399. nfs4_stateid_copy(&delegation->stateid, stateid);
  400. refcount_set(&delegation->refcount, 1);
  401. delegation->type = type;
  402. delegation->pagemod_limit = pagemod_limit;
  403. delegation->change_attr = inode_peek_iversion_raw(inode);
  404. delegation->cred = get_cred(cred);
  405. delegation->inode = inode;
  406. delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
  407. INIT_LIST_HEAD(&delegation->entry);
  408. switch (deleg_type) {
  409. case NFS4_OPEN_DELEGATE_READ_ATTRS_DELEG:
  410. case NFS4_OPEN_DELEGATE_WRITE_ATTRS_DELEG:
  411. delegation->flags |= BIT(NFS_DELEGATION_DELEGTIME);
  412. }
  413. delegation->test_gen = 0;
  414. spin_lock_init(&delegation->lock);
  415. spin_lock(&clp->cl_lock);
  416. old_delegation = rcu_dereference_protected(nfsi->delegation,
  417. lockdep_is_held(&clp->cl_lock));
  418. if (old_delegation == NULL)
  419. goto add_new;
  420. /* Is this an update of the existing delegation? */
  421. if (nfs4_stateid_match_other(&old_delegation->stateid,
  422. &delegation->stateid)) {
  423. spin_lock(&old_delegation->lock);
  424. nfs_update_inplace_delegation(server, old_delegation,
  425. delegation);
  426. spin_unlock(&old_delegation->lock);
  427. goto out;
  428. }
  429. if (!test_bit(NFS_DELEGATION_REVOKED, &old_delegation->flags)) {
  430. /*
  431. * Deal with broken servers that hand out two
  432. * delegations for the same file.
  433. * Allow for upgrades to a WRITE delegation, but
  434. * nothing else.
  435. */
  436. dfprintk(FILE, "%s: server %s handed out "
  437. "a duplicate delegation!\n",
  438. __func__, clp->cl_hostname);
  439. if (delegation->type == old_delegation->type ||
  440. !(delegation->type & FMODE_WRITE)) {
  441. freeme = delegation;
  442. delegation = NULL;
  443. goto out;
  444. }
  445. if (test_and_set_bit(NFS_DELEGATION_RETURNING,
  446. &old_delegation->flags))
  447. goto out;
  448. }
  449. if (!nfs_detach_delegations_locked(nfsi, old_delegation, clp))
  450. goto out;
  451. freeme = old_delegation;
  452. add_new:
  453. /*
  454. * If we didn't revalidate the change attribute before setting
  455. * the delegation, then pre-emptively ask for a full attribute
  456. * cache revalidation.
  457. */
  458. spin_lock(&inode->i_lock);
  459. if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_CHANGE)
  460. nfs_set_cache_invalid(inode,
  461. NFS_INO_INVALID_ATIME | NFS_INO_INVALID_CTIME |
  462. NFS_INO_INVALID_MTIME | NFS_INO_INVALID_SIZE |
  463. NFS_INO_INVALID_BLOCKS | NFS_INO_INVALID_NLINK |
  464. NFS_INO_INVALID_OTHER | NFS_INO_INVALID_DATA |
  465. NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL |
  466. NFS_INO_INVALID_XATTR);
  467. spin_unlock(&inode->i_lock);
  468. list_add_tail_rcu(&delegation->super_list, &server->delegations);
  469. hlist_add_head_rcu(&delegation->hash,
  470. nfs_delegation_hash(server, &NFS_I(inode)->fh));
  471. rcu_assign_pointer(nfsi->delegation, delegation);
  472. delegation = NULL;
  473. atomic_long_inc(&server->nr_active_delegations);
  474. trace_nfs4_set_delegation(inode, type);
  475. /* If we hold writebacks and have delegated mtime then update */
  476. if (deleg_type == NFS4_OPEN_DELEGATE_WRITE_ATTRS_DELEG &&
  477. nfs_have_writebacks(inode))
  478. nfs_update_delegated_mtime(inode);
  479. out:
  480. spin_unlock(&clp->cl_lock);
  481. if (delegation != NULL)
  482. __nfs_free_delegation(delegation);
  483. if (freeme != NULL) {
  484. nfs_do_return_delegation(inode, freeme, 0);
  485. nfs_mark_delegation_revoked(server, freeme);
  486. nfs_put_delegation(freeme);
  487. }
  488. return status;
  489. }
  490. /*
  491. * Basic procedure for returning a delegation to the server.
  492. * If @issync is set, wait until state recovery has finished. Otherwise
  493. * return -EAGAIN to the caller if we need more time.
  494. */
  495. static int nfs_end_delegation_return(struct inode *inode,
  496. struct nfs_delegation *delegation, bool issync)
  497. {
  498. struct nfs_server *server = NFS_SERVER(inode);
  499. unsigned int mode = O_WRONLY | O_RDWR;
  500. int err = 0;
  501. /* Directory delegations don't require any state recovery */
  502. if (!S_ISREG(inode->i_mode))
  503. goto out_return;
  504. if (!issync)
  505. mode |= O_NONBLOCK;
  506. /* Recall of any remaining application leases */
  507. err = break_lease(inode, mode);
  508. while (err == 0) {
  509. if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
  510. break;
  511. err = nfs_delegation_claim_opens(inode, &delegation->stateid,
  512. delegation->type);
  513. if (!err)
  514. break;
  515. if (err != -EAGAIN)
  516. goto abort;
  517. if (!issync)
  518. goto delay;
  519. /*
  520. * Guard against state recovery
  521. */
  522. err = nfs4_wait_clnt_recover(server->nfs_client);
  523. }
  524. out_return:
  525. return nfs_do_return_delegation(inode, delegation, issync);
  526. delay:
  527. spin_lock(&server->delegations_lock);
  528. if (list_empty(&delegation->entry))
  529. refcount_inc(&delegation->refcount);
  530. list_move_tail(&delegation->entry, &server->delegations_return);
  531. spin_unlock(&server->delegations_lock);
  532. set_bit(NFS4CLNT_DELEGRETURN_DELAYED, &server->nfs_client->cl_state);
  533. abort:
  534. clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
  535. return err;
  536. }
  537. static int nfs_return_one_delegation(struct nfs_server *server)
  538. {
  539. struct nfs_delegation *delegation;
  540. struct inode *inode;
  541. int err = 0;
  542. spin_lock(&server->delegations_lock);
  543. delegation = list_first_entry_or_null(&server->delegations_return,
  544. struct nfs_delegation, entry);
  545. if (!delegation) {
  546. spin_unlock(&server->delegations_lock);
  547. return 0; /* no more delegations */
  548. }
  549. list_del_init(&delegation->entry);
  550. spin_unlock(&server->delegations_lock);
  551. spin_lock(&delegation->lock);
  552. inode = delegation->inode;
  553. if (!inode || !igrab(inode)) {
  554. spin_unlock(&delegation->lock);
  555. goto out_put_delegation;
  556. }
  557. if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
  558. test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
  559. spin_unlock(&delegation->lock);
  560. goto out_put_inode;
  561. }
  562. spin_unlock(&delegation->lock);
  563. nfs_clear_verifier_delegated(inode);
  564. err = nfs_end_delegation_return(inode, delegation, false);
  565. out_put_inode:
  566. iput(inode);
  567. out_put_delegation:
  568. nfs_put_delegation(delegation);
  569. if (err)
  570. return err;
  571. return 1; /* keep going */
  572. }
  573. static int nfs_server_return_marked_delegations(struct nfs_server *server,
  574. void __always_unused *data)
  575. {
  576. int err;
  577. while ((err = nfs_return_one_delegation(server)) > 0)
  578. cond_resched();
  579. return err;
  580. }
  581. static inline bool nfs_delegations_over_limit(struct nfs_server *server)
  582. {
  583. return !list_empty_careful(&server->delegations_lru) &&
  584. atomic_long_read(&server->nr_active_delegations) >
  585. nfs_delegation_watermark;
  586. }
  587. static void nfs_delegations_return_from_lru(struct nfs_server *server)
  588. {
  589. struct nfs_delegation *d, *n;
  590. unsigned int pass = 0;
  591. bool moved = false;
  592. retry:
  593. spin_lock(&server->delegations_lock);
  594. list_for_each_entry_safe(d, n, &server->delegations_lru, entry) {
  595. if (!nfs_delegations_over_limit(server))
  596. break;
  597. if (pass == 0 && test_bit(NFS_DELEGATION_REFERENCED, &d->flags))
  598. continue;
  599. list_move_tail(&d->entry, &server->delegations_return);
  600. moved = true;
  601. }
  602. spin_unlock(&server->delegations_lock);
  603. /*
  604. * If we are still over the limit, try to reclaim referenced delegations
  605. * as well.
  606. */
  607. if (pass == 0 && nfs_delegations_over_limit(server)) {
  608. pass++;
  609. goto retry;
  610. }
  611. if (moved) {
  612. set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
  613. nfs4_schedule_state_manager(server->nfs_client);
  614. }
  615. }
  616. static void nfs_delegation_add_lru(struct nfs_server *server,
  617. struct nfs_delegation *delegation)
  618. {
  619. spin_lock(&server->delegations_lock);
  620. if (list_empty(&delegation->entry)) {
  621. list_add_tail(&delegation->entry, &server->delegations_lru);
  622. refcount_inc(&delegation->refcount);
  623. }
  624. spin_unlock(&server->delegations_lock);
  625. if (nfs_delegations_over_limit(server))
  626. nfs_delegations_return_from_lru(server);
  627. }
  628. static bool nfs_server_clear_delayed_delegations(struct nfs_server *server)
  629. {
  630. bool ret = false;
  631. if (list_empty_careful(&server->delegations_delayed))
  632. return false;
  633. spin_lock(&server->delegations_lock);
  634. if (!list_empty(&server->delegations_delayed)) {
  635. list_splice_tail_init(&server->delegations_delayed,
  636. &server->delegations_return);
  637. ret = true;
  638. }
  639. spin_unlock(&server->delegations_lock);
  640. return ret;
  641. }
  642. static bool nfs_client_clear_delayed_delegations(struct nfs_client *clp)
  643. {
  644. struct nfs_server *server;
  645. bool ret = false;
  646. if (!test_and_clear_bit(NFS4CLNT_DELEGRETURN_DELAYED, &clp->cl_state))
  647. return false;
  648. rcu_read_lock();
  649. list_for_each_entry_rcu (server, &clp->cl_superblocks, client_link) {
  650. if (nfs_server_clear_delayed_delegations(server))
  651. ret = true;
  652. }
  653. rcu_read_unlock();
  654. if (ret)
  655. set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
  656. return ret;
  657. }
  658. /**
  659. * nfs_client_return_marked_delegations - return previously marked delegations
  660. * @clp: nfs_client to process
  661. *
  662. * Note that this function is designed to be called by the state
  663. * manager thread. For this reason, it cannot flush the dirty data,
  664. * since that could deadlock in case of a state recovery error.
  665. *
  666. * Returns zero on success, or a negative errno value.
  667. */
  668. int nfs_client_return_marked_delegations(struct nfs_client *clp)
  669. {
  670. int err = nfs_client_for_each_server(
  671. clp, nfs_server_return_marked_delegations, NULL);
  672. if (err)
  673. return err;
  674. /* If a return was delayed, sleep to prevent hard looping */
  675. if (nfs_client_clear_delayed_delegations(clp))
  676. ssleep(1);
  677. return 0;
  678. }
  679. /**
  680. * nfs_inode_evict_delegation - return delegation, don't reclaim opens
  681. * @inode: inode to process
  682. *
  683. * Does not protect against delegation reclaims, therefore really only safe
  684. * to be called from nfs4_clear_inode(). Guaranteed to always free
  685. * the delegation structure.
  686. */
  687. void nfs_inode_evict_delegation(struct inode *inode)
  688. {
  689. struct nfs_inode *nfsi = NFS_I(inode);
  690. struct nfs_server *server = NFS_SERVER(inode);
  691. struct nfs_delegation *delegation;
  692. rcu_read_lock();
  693. delegation = rcu_dereference(nfsi->delegation);
  694. if (delegation && !nfs_detach_delegation(nfsi, delegation, server))
  695. delegation = NULL;
  696. rcu_read_unlock();
  697. if (!delegation)
  698. return;
  699. set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
  700. nfs_do_return_delegation(inode, delegation, 1);
  701. nfs_mark_delegation_revoked(server, delegation);
  702. nfs_put_delegation(delegation);
  703. }
  704. /**
  705. * nfs4_inode_return_delegation - synchronously return a delegation
  706. * @inode: inode to process
  707. *
  708. * This routine will always flush any dirty data to disk on the
  709. * assumption that if we need to return the delegation, then
  710. * we should stop caching.
  711. *
  712. * Returns zero on success, or a negative errno value.
  713. */
  714. void nfs4_inode_return_delegation(struct inode *inode)
  715. {
  716. struct nfs_inode *nfsi = NFS_I(inode);
  717. struct nfs_delegation *delegation;
  718. delegation = nfs_start_delegation_return(nfsi);
  719. if (!delegation)
  720. return;
  721. /* Synchronous recall of any application leases */
  722. break_lease(inode, O_WRONLY | O_RDWR);
  723. if (S_ISREG(inode->i_mode))
  724. nfs_wb_all(inode);
  725. nfs_end_delegation_return(inode, delegation, true);
  726. nfs_put_delegation(delegation);
  727. }
  728. /**
  729. * nfs4_inode_set_return_delegation_on_close - asynchronously return a delegation
  730. * @inode: inode to process
  731. *
  732. * This routine is called to request that the delegation be returned as soon
  733. * as the file is closed. If the file is already closed, the delegation is
  734. * immediately returned.
  735. */
  736. void nfs4_inode_set_return_delegation_on_close(struct inode *inode)
  737. {
  738. struct nfs_delegation *delegation;
  739. bool return_now = false;
  740. if (!inode)
  741. return;
  742. delegation = nfs4_get_valid_delegation(inode);
  743. if (!delegation)
  744. return;
  745. spin_lock(&delegation->lock);
  746. if (!delegation->inode)
  747. goto out_unlock;
  748. if (list_empty(&NFS_I(inode)->open_files) &&
  749. !test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
  750. return_now = true;
  751. else
  752. set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
  753. out_unlock:
  754. spin_unlock(&delegation->lock);
  755. if (return_now) {
  756. nfs_clear_verifier_delegated(inode);
  757. nfs_end_delegation_return(inode, delegation, false);
  758. }
  759. nfs_put_delegation(delegation);
  760. }
  761. /**
  762. * nfs4_inode_return_delegation_on_close - asynchronously return a delegation
  763. * @inode: inode to process
  764. *
  765. * This routine is called on file close in order to determine if the
  766. * inode delegation needs to be returned immediately.
  767. */
  768. void nfs4_inode_return_delegation_on_close(struct inode *inode)
  769. {
  770. struct nfs_server *server = NFS_SERVER(inode);
  771. struct nfs_delegation *delegation;
  772. bool return_now = false;
  773. delegation = nfs4_get_valid_delegation(inode);
  774. if (!delegation)
  775. return;
  776. if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags)) {
  777. spin_lock(&delegation->lock);
  778. if (delegation->inode &&
  779. list_empty(&NFS_I(inode)->open_files) &&
  780. !test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
  781. clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
  782. return_now = true;
  783. }
  784. spin_unlock(&delegation->lock);
  785. }
  786. if (return_now) {
  787. nfs_clear_verifier_delegated(inode);
  788. nfs_end_delegation_return(inode, delegation, false);
  789. } else {
  790. nfs_delegation_add_lru(server, delegation);
  791. }
  792. nfs_put_delegation(delegation);
  793. }
  794. /**
  795. * nfs4_inode_make_writeable
  796. * @inode: pointer to inode
  797. *
  798. * Make the inode writeable by returning the delegation if necessary
  799. */
  800. void nfs4_inode_make_writeable(struct inode *inode)
  801. {
  802. struct nfs_delegation *delegation;
  803. delegation = nfs4_get_valid_delegation(inode);
  804. if (!delegation)
  805. return;
  806. if (!nfs4_has_session(NFS_SERVER(inode)->nfs_client) ||
  807. !(delegation->type & FMODE_WRITE))
  808. nfs4_inode_return_delegation(inode);
  809. nfs_put_delegation(delegation);
  810. }
  811. static void
  812. nfs_mark_return_if_closed_delegation(struct nfs_server *server,
  813. struct nfs_delegation *delegation)
  814. {
  815. struct inode *inode;
  816. if (!list_empty_careful(&server->delegations_return) ||
  817. test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags))
  818. return;
  819. spin_lock(&delegation->lock);
  820. inode = delegation->inode;
  821. if (!inode)
  822. goto out;
  823. if (list_empty(&NFS_I(inode)->open_files))
  824. nfs_mark_return_delegation(server, delegation);
  825. else
  826. set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
  827. out:
  828. spin_unlock(&delegation->lock);
  829. }
  830. static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
  831. {
  832. struct nfs_delegation *delegation;
  833. bool ret = false;
  834. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  835. nfs_mark_return_delegation(server, delegation);
  836. ret = true;
  837. }
  838. return ret;
  839. }
  840. static void nfs_delegation_run_state_manager(struct nfs_client *clp)
  841. {
  842. if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
  843. nfs4_schedule_state_manager(clp);
  844. }
  845. /**
  846. * nfs_expire_all_delegations
  847. * @clp: client to process
  848. *
  849. */
  850. void nfs_expire_all_delegations(struct nfs_client *clp)
  851. {
  852. struct nfs_server *server;
  853. rcu_read_lock();
  854. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  855. nfs_server_mark_return_all_delegations(server);
  856. rcu_read_unlock();
  857. nfs_delegation_run_state_manager(clp);
  858. }
  859. /**
  860. * nfs_server_return_all_delegations - return delegations for one superblock
  861. * @server: pointer to nfs_server to process
  862. *
  863. */
  864. void nfs_server_return_all_delegations(struct nfs_server *server)
  865. {
  866. struct nfs_client *clp = server->nfs_client;
  867. bool need_wait;
  868. if (clp == NULL)
  869. return;
  870. rcu_read_lock();
  871. need_wait = nfs_server_mark_return_all_delegations(server);
  872. rcu_read_unlock();
  873. if (need_wait) {
  874. nfs4_schedule_state_manager(clp);
  875. nfs4_wait_clnt_recover(clp);
  876. }
  877. }
  878. static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
  879. fmode_t flags)
  880. {
  881. struct nfs_delegation *delegation;
  882. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  883. if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
  884. continue;
  885. if (delegation->type & flags)
  886. nfs_mark_return_if_closed_delegation(server, delegation);
  887. }
  888. }
  889. void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
  890. {
  891. struct nfs_server *server;
  892. rcu_read_lock();
  893. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  894. nfs_mark_return_unused_delegation_types(server, flags);
  895. rcu_read_unlock();
  896. nfs_delegation_run_state_manager(clp);
  897. }
  898. static void nfs_revoke_delegation(struct inode *inode,
  899. const nfs4_stateid *stateid)
  900. {
  901. struct nfs_delegation *delegation;
  902. nfs4_stateid tmp;
  903. bool ret = false;
  904. rcu_read_lock();
  905. delegation = rcu_dereference(NFS_I(inode)->delegation);
  906. if (delegation == NULL)
  907. goto out;
  908. if (stateid == NULL) {
  909. nfs4_stateid_copy(&tmp, &delegation->stateid);
  910. stateid = &tmp;
  911. } else {
  912. if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
  913. goto out;
  914. spin_lock(&delegation->lock);
  915. if (stateid->seqid) {
  916. if (nfs4_stateid_is_newer(&delegation->stateid, stateid)) {
  917. spin_unlock(&delegation->lock);
  918. goto out;
  919. }
  920. delegation->stateid.seqid = stateid->seqid;
  921. }
  922. spin_unlock(&delegation->lock);
  923. }
  924. nfs_mark_delegation_revoked(NFS_SERVER(inode), delegation);
  925. ret = true;
  926. out:
  927. rcu_read_unlock();
  928. if (ret)
  929. nfs_inode_find_state_and_recover(inode, stateid);
  930. }
  931. void nfs_delegation_mark_returned(struct inode *inode,
  932. const nfs4_stateid *stateid)
  933. {
  934. struct nfs_delegation *delegation;
  935. if (!inode)
  936. return;
  937. rcu_read_lock();
  938. delegation = rcu_dereference(NFS_I(inode)->delegation);
  939. if (!delegation)
  940. goto out_rcu_unlock;
  941. spin_lock(&delegation->lock);
  942. if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
  943. goto out_spin_unlock;
  944. if (stateid->seqid) {
  945. /* If delegation->stateid is newer, dont mark as returned */
  946. if (nfs4_stateid_is_newer(&delegation->stateid, stateid))
  947. goto out_clear_returning;
  948. if (delegation->stateid.seqid != stateid->seqid)
  949. delegation->stateid.seqid = stateid->seqid;
  950. }
  951. nfs_mark_delegation_revoked(NFS_SERVER(inode), delegation);
  952. clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
  953. spin_unlock(&delegation->lock);
  954. if (nfs_detach_delegation(NFS_I(inode), delegation, NFS_SERVER(inode)))
  955. nfs_put_delegation(delegation);
  956. goto out_rcu_unlock;
  957. out_clear_returning:
  958. clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
  959. out_spin_unlock:
  960. spin_unlock(&delegation->lock);
  961. out_rcu_unlock:
  962. rcu_read_unlock();
  963. nfs_inode_find_state_and_recover(inode, stateid);
  964. }
  965. /**
  966. * nfs_remove_bad_delegation - handle delegations that are unusable
  967. * @inode: inode to process
  968. * @stateid: the delegation's stateid
  969. *
  970. * If the server ACK-ed our FREE_STATEID then clean
  971. * up the delegation, else mark and keep the revoked state.
  972. */
  973. void nfs_remove_bad_delegation(struct inode *inode,
  974. const nfs4_stateid *stateid)
  975. {
  976. if (stateid && stateid->type == NFS4_FREED_STATEID_TYPE)
  977. nfs_delegation_mark_returned(inode, stateid);
  978. else
  979. nfs_revoke_delegation(inode, stateid);
  980. }
  981. EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
  982. static bool nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
  983. {
  984. struct nfs_delegation *d, *n;
  985. bool marked = false;
  986. spin_lock(&server->delegations_lock);
  987. list_for_each_entry_safe(d, n, &server->delegations_lru, entry) {
  988. if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &d->flags))
  989. continue;
  990. list_move_tail(&d->entry, &server->delegations_return);
  991. marked = true;
  992. }
  993. spin_unlock(&server->delegations_lock);
  994. return marked;
  995. }
  996. /**
  997. * nfs_expire_unreferenced_delegations - Eliminate unused delegations
  998. * @clp: nfs_client to process
  999. *
  1000. */
  1001. void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
  1002. {
  1003. struct nfs_server *server;
  1004. bool marked = false;
  1005. rcu_read_lock();
  1006. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  1007. marked |= nfs_mark_return_unreferenced_delegations(server);
  1008. rcu_read_unlock();
  1009. if (marked) {
  1010. set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
  1011. nfs4_schedule_state_manager(clp);
  1012. }
  1013. }
  1014. /**
  1015. * nfs_async_inode_return_delegation - asynchronously return a delegation
  1016. * @inode: inode to process
  1017. * @stateid: state ID information
  1018. *
  1019. * Returns zero on success, or a negative errno value.
  1020. */
  1021. int nfs_async_inode_return_delegation(struct inode *inode,
  1022. const nfs4_stateid *stateid)
  1023. {
  1024. struct nfs_server *server = NFS_SERVER(inode);
  1025. struct nfs_client *clp = server->nfs_client;
  1026. struct nfs_delegation *delegation;
  1027. delegation = nfs4_get_valid_delegation(inode);
  1028. if (!delegation)
  1029. return -ENOENT;
  1030. if (stateid != NULL &&
  1031. !clp->cl_mvops->match_stateid(&delegation->stateid, stateid)) {
  1032. nfs_put_delegation(delegation);
  1033. return -ENOENT;
  1034. }
  1035. nfs_mark_return_delegation(server, delegation);
  1036. nfs_put_delegation(delegation);
  1037. /* If there are any application leases or delegations, recall them */
  1038. break_lease(inode, O_WRONLY | O_RDWR | O_NONBLOCK);
  1039. nfs_delegation_run_state_manager(clp);
  1040. return 0;
  1041. }
  1042. static struct inode *
  1043. nfs_delegation_find_inode_server(struct nfs_server *server,
  1044. const struct nfs_fh *fhandle)
  1045. {
  1046. struct hlist_head *head = nfs_delegation_hash(server, fhandle);
  1047. struct nfs_delegation *delegation;
  1048. struct super_block *freeme = NULL;
  1049. struct inode *res = NULL;
  1050. hlist_for_each_entry_rcu(delegation, head, hash) {
  1051. spin_lock(&delegation->lock);
  1052. if (delegation->inode != NULL &&
  1053. !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
  1054. nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
  1055. if (nfs_sb_active(server->super)) {
  1056. freeme = server->super;
  1057. res = igrab(delegation->inode);
  1058. }
  1059. spin_unlock(&delegation->lock);
  1060. if (res != NULL)
  1061. return res;
  1062. if (freeme) {
  1063. rcu_read_unlock();
  1064. nfs_sb_deactive(freeme);
  1065. rcu_read_lock();
  1066. }
  1067. return ERR_PTR(-EAGAIN);
  1068. }
  1069. spin_unlock(&delegation->lock);
  1070. }
  1071. return ERR_PTR(-ENOENT);
  1072. }
  1073. /**
  1074. * nfs_delegation_find_inode - retrieve the inode associated with a delegation
  1075. * @clp: client state handle
  1076. * @fhandle: filehandle from a delegation recall
  1077. *
  1078. * Returns pointer to inode matching "fhandle," or NULL if a matching inode
  1079. * cannot be found.
  1080. */
  1081. struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
  1082. const struct nfs_fh *fhandle)
  1083. {
  1084. struct nfs_server *server;
  1085. struct inode *res;
  1086. rcu_read_lock();
  1087. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  1088. res = nfs_delegation_find_inode_server(server, fhandle);
  1089. if (res != ERR_PTR(-ENOENT)) {
  1090. rcu_read_unlock();
  1091. return res;
  1092. }
  1093. }
  1094. rcu_read_unlock();
  1095. return ERR_PTR(-ENOENT);
  1096. }
  1097. static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
  1098. {
  1099. struct nfs_delegation *delegation;
  1100. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  1101. /*
  1102. * If the delegation may have been admin revoked, then we
  1103. * cannot reclaim it.
  1104. */
  1105. if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags))
  1106. continue;
  1107. set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
  1108. }
  1109. }
  1110. /**
  1111. * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
  1112. * @clp: nfs_client to process
  1113. *
  1114. */
  1115. void nfs_delegation_mark_reclaim(struct nfs_client *clp)
  1116. {
  1117. struct nfs_server *server;
  1118. rcu_read_lock();
  1119. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  1120. nfs_delegation_mark_reclaim_server(server);
  1121. rcu_read_unlock();
  1122. }
  1123. static int nfs_server_reap_unclaimed_delegations(struct nfs_server *server,
  1124. void __always_unused *data)
  1125. {
  1126. struct nfs_delegation *delegation;
  1127. struct inode *inode;
  1128. restart:
  1129. rcu_read_lock();
  1130. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  1131. if (test_bit(NFS_DELEGATION_RETURNING,
  1132. &delegation->flags) ||
  1133. test_bit(NFS_DELEGATION_NEED_RECLAIM,
  1134. &delegation->flags) == 0)
  1135. continue;
  1136. inode = nfs_delegation_grab_inode(delegation);
  1137. if (inode == NULL)
  1138. continue;
  1139. delegation = nfs_start_delegation_return(NFS_I(inode));
  1140. rcu_read_unlock();
  1141. if (delegation != NULL) {
  1142. if (nfs_detach_delegation(NFS_I(inode), delegation,
  1143. server)) {
  1144. nfs_mark_delegation_revoked(server, delegation);
  1145. nfs_put_delegation(delegation);
  1146. }
  1147. /* Match nfs_start_delegation_return */
  1148. nfs_put_delegation(delegation);
  1149. }
  1150. iput(inode);
  1151. cond_resched();
  1152. goto restart;
  1153. }
  1154. rcu_read_unlock();
  1155. return 0;
  1156. }
  1157. /**
  1158. * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
  1159. * @clp: nfs_client to process
  1160. *
  1161. */
  1162. void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
  1163. {
  1164. nfs_client_for_each_server(clp, nfs_server_reap_unclaimed_delegations,
  1165. NULL);
  1166. }
  1167. static inline bool nfs4_server_rebooted(const struct nfs_client *clp)
  1168. {
  1169. return (clp->cl_state & (BIT(NFS4CLNT_CHECK_LEASE) |
  1170. BIT(NFS4CLNT_LEASE_EXPIRED) |
  1171. BIT(NFS4CLNT_SESSION_RESET))) != 0;
  1172. }
  1173. static void nfs_mark_test_expired_delegation(struct nfs_server *server,
  1174. struct nfs_delegation *delegation)
  1175. {
  1176. if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE)
  1177. return;
  1178. clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
  1179. set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
  1180. set_bit(NFS4SERV_DELEGATION_EXPIRED, &server->delegation_flags);
  1181. set_bit(NFS4CLNT_DELEGATION_EXPIRED, &server->nfs_client->cl_state);
  1182. }
  1183. static void nfs_inode_mark_test_expired_delegation(struct nfs_server *server,
  1184. struct inode *inode)
  1185. {
  1186. struct nfs_delegation *delegation;
  1187. rcu_read_lock();
  1188. delegation = rcu_dereference(NFS_I(inode)->delegation);
  1189. if (delegation)
  1190. nfs_mark_test_expired_delegation(server, delegation);
  1191. rcu_read_unlock();
  1192. }
  1193. static void nfs_delegation_mark_test_expired_server(struct nfs_server *server)
  1194. {
  1195. struct nfs_delegation *delegation;
  1196. list_for_each_entry_rcu(delegation, &server->delegations, super_list)
  1197. nfs_mark_test_expired_delegation(server, delegation);
  1198. }
  1199. /**
  1200. * nfs_mark_test_expired_all_delegations - mark all delegations for testing
  1201. * @clp: nfs_client to process
  1202. *
  1203. * Iterates through all the delegations associated with this server and
  1204. * marks them as needing to be checked for validity.
  1205. */
  1206. void nfs_mark_test_expired_all_delegations(struct nfs_client *clp)
  1207. {
  1208. struct nfs_server *server;
  1209. rcu_read_lock();
  1210. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  1211. nfs_delegation_mark_test_expired_server(server);
  1212. rcu_read_unlock();
  1213. }
  1214. /**
  1215. * nfs_test_expired_all_delegations - test all delegations for a client
  1216. * @clp: nfs_client to process
  1217. *
  1218. * Helper for handling "recallable state revoked" status from server.
  1219. */
  1220. void nfs_test_expired_all_delegations(struct nfs_client *clp)
  1221. {
  1222. nfs_mark_test_expired_all_delegations(clp);
  1223. nfs4_schedule_state_manager(clp);
  1224. }
  1225. static void
  1226. nfs_delegation_test_free_expired(struct inode *inode,
  1227. nfs4_stateid *stateid,
  1228. const struct cred *cred)
  1229. {
  1230. struct nfs_server *server = NFS_SERVER(inode);
  1231. const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
  1232. int status;
  1233. if (!cred)
  1234. return;
  1235. status = ops->test_and_free_expired(server, stateid, cred);
  1236. if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
  1237. nfs_remove_bad_delegation(inode, stateid);
  1238. }
  1239. static int nfs_server_reap_expired_delegations(struct nfs_server *server,
  1240. void __always_unused *data)
  1241. {
  1242. struct nfs_delegation *delegation;
  1243. struct inode *inode;
  1244. const struct cred *cred;
  1245. nfs4_stateid stateid;
  1246. unsigned long gen = ++server->delegation_gen;
  1247. if (!test_and_clear_bit(NFS4SERV_DELEGATION_EXPIRED,
  1248. &server->delegation_flags))
  1249. return 0;
  1250. restart:
  1251. rcu_read_lock();
  1252. list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
  1253. if (test_bit(NFS_DELEGATION_RETURNING,
  1254. &delegation->flags) ||
  1255. test_bit(NFS_DELEGATION_TEST_EXPIRED,
  1256. &delegation->flags) == 0 ||
  1257. delegation->test_gen == gen)
  1258. continue;
  1259. inode = nfs_delegation_grab_inode(delegation);
  1260. if (inode == NULL)
  1261. continue;
  1262. spin_lock(&delegation->lock);
  1263. cred = get_cred_rcu(delegation->cred);
  1264. nfs4_stateid_copy(&stateid, &delegation->stateid);
  1265. spin_unlock(&delegation->lock);
  1266. delegation->test_gen = gen;
  1267. clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
  1268. rcu_read_unlock();
  1269. nfs_delegation_test_free_expired(inode, &stateid, cred);
  1270. put_cred(cred);
  1271. if (!nfs4_server_rebooted(server->nfs_client)) {
  1272. iput(inode);
  1273. cond_resched();
  1274. goto restart;
  1275. }
  1276. nfs_inode_mark_test_expired_delegation(server,inode);
  1277. set_bit(NFS4SERV_DELEGATION_EXPIRED, &server->delegation_flags);
  1278. set_bit(NFS4CLNT_DELEGATION_EXPIRED,
  1279. &server->nfs_client->cl_state);
  1280. iput(inode);
  1281. return -EAGAIN;
  1282. }
  1283. rcu_read_unlock();
  1284. return 0;
  1285. }
  1286. /**
  1287. * nfs_reap_expired_delegations - reap expired delegations
  1288. * @clp: nfs_client to process
  1289. *
  1290. * Iterates through all the delegations associated with this server and
  1291. * checks if they have may have been revoked. This function is usually
  1292. * expected to be called in cases where the server may have lost its
  1293. * lease.
  1294. */
  1295. void nfs_reap_expired_delegations(struct nfs_client *clp)
  1296. {
  1297. nfs_client_for_each_server(clp, nfs_server_reap_expired_delegations,
  1298. NULL);
  1299. }
  1300. void nfs_inode_find_delegation_state_and_recover(struct inode *inode,
  1301. const nfs4_stateid *stateid)
  1302. {
  1303. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  1304. struct nfs_delegation *delegation;
  1305. bool found = false;
  1306. rcu_read_lock();
  1307. delegation = rcu_dereference(NFS_I(inode)->delegation);
  1308. if (delegation &&
  1309. nfs4_stateid_match_or_older(&delegation->stateid, stateid) &&
  1310. !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
  1311. nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation);
  1312. found = true;
  1313. }
  1314. rcu_read_unlock();
  1315. if (found)
  1316. nfs4_schedule_state_manager(clp);
  1317. }
  1318. /**
  1319. * nfs_delegations_present - check for existence of delegations
  1320. * @clp: client state handle
  1321. *
  1322. * Returns one if there are any nfs_delegation structures attached
  1323. * to this nfs_client.
  1324. */
  1325. int nfs_delegations_present(struct nfs_client *clp)
  1326. {
  1327. struct nfs_server *server;
  1328. int ret = 0;
  1329. rcu_read_lock();
  1330. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  1331. if (atomic_long_read(&server->nr_active_delegations) > 0) {
  1332. ret = 1;
  1333. break;
  1334. }
  1335. rcu_read_unlock();
  1336. return ret;
  1337. }
  1338. /**
  1339. * nfs4_refresh_delegation_stateid - Update delegation stateid seqid
  1340. * @dst: stateid to refresh
  1341. * @inode: inode to check
  1342. *
  1343. * Returns "true" and updates "dst->seqid" * if inode had a delegation
  1344. * that matches our delegation stateid. Otherwise "false" is returned.
  1345. */
  1346. bool nfs4_refresh_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
  1347. {
  1348. struct nfs_delegation *delegation;
  1349. bool ret = false;
  1350. if (!inode)
  1351. goto out;
  1352. rcu_read_lock();
  1353. delegation = rcu_dereference(NFS_I(inode)->delegation);
  1354. if (delegation != NULL &&
  1355. nfs4_stateid_match_other(dst, &delegation->stateid) &&
  1356. nfs4_stateid_is_newer(&delegation->stateid, dst) &&
  1357. !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
  1358. dst->seqid = delegation->stateid.seqid;
  1359. ret = true;
  1360. }
  1361. rcu_read_unlock();
  1362. out:
  1363. return ret;
  1364. }
  1365. /**
  1366. * nfs4_copy_delegation_stateid - Copy inode's state ID information
  1367. * @inode: inode to check
  1368. * @flags: delegation type requirement
  1369. * @dst: stateid data structure to fill in
  1370. * @cred: optional argument to retrieve credential
  1371. *
  1372. * Returns "true" and fills in "dst->data" * if inode had a delegation,
  1373. * otherwise "false" is returned.
  1374. */
  1375. bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags,
  1376. nfs4_stateid *dst, const struct cred **cred)
  1377. {
  1378. struct nfs_inode *nfsi = NFS_I(inode);
  1379. struct nfs_delegation *delegation;
  1380. bool ret = false;
  1381. flags &= FMODE_READ|FMODE_WRITE;
  1382. rcu_read_lock();
  1383. delegation = rcu_dereference(nfsi->delegation);
  1384. if (!delegation)
  1385. goto out;
  1386. spin_lock(&delegation->lock);
  1387. ret = nfs4_is_valid_delegation(delegation, flags);
  1388. if (ret) {
  1389. nfs4_stateid_copy(dst, &delegation->stateid);
  1390. nfs_mark_delegation_referenced(delegation);
  1391. if (cred)
  1392. *cred = get_cred(delegation->cred);
  1393. }
  1394. spin_unlock(&delegation->lock);
  1395. out:
  1396. rcu_read_unlock();
  1397. return ret;
  1398. }
  1399. /**
  1400. * nfs4_delegation_flush_on_close - Check if we must flush file on close
  1401. * @inode: inode to check
  1402. *
  1403. * This function checks the number of outstanding writes to the file
  1404. * against the delegation 'space_limit' field to see if
  1405. * the spec requires us to flush the file on close.
  1406. */
  1407. bool nfs4_delegation_flush_on_close(const struct inode *inode)
  1408. {
  1409. struct nfs_inode *nfsi = NFS_I(inode);
  1410. struct nfs_delegation *delegation;
  1411. bool ret = true;
  1412. rcu_read_lock();
  1413. delegation = rcu_dereference(nfsi->delegation);
  1414. if (delegation == NULL || !(delegation->type & FMODE_WRITE))
  1415. goto out;
  1416. if (atomic_long_read(&nfsi->nrequests) < delegation->pagemod_limit)
  1417. ret = false;
  1418. out:
  1419. rcu_read_unlock();
  1420. return ret;
  1421. }
  1422. int nfs4_delegation_hash_alloc(struct nfs_server *server)
  1423. {
  1424. int delegation_buckets, i;
  1425. delegation_buckets = roundup_pow_of_two(nfs_delegation_watermark / 16);
  1426. server->delegation_hash_mask = delegation_buckets - 1;
  1427. server->delegation_hash_table = kmalloc_objs(*server->delegation_hash_table,
  1428. delegation_buckets);
  1429. if (!server->delegation_hash_table)
  1430. return -ENOMEM;
  1431. for (i = 0; i < delegation_buckets; i++)
  1432. INIT_HLIST_HEAD(&server->delegation_hash_table[i]);
  1433. return 0;
  1434. }