xfs_icache.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_format.h"
  21. #include "xfs_log_format.h"
  22. #include "xfs_trans_resv.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_inode.h"
  28. #include "xfs_error.h"
  29. #include "xfs_trans.h"
  30. #include "xfs_trans_priv.h"
  31. #include "xfs_inode_item.h"
  32. #include "xfs_quota.h"
  33. #include "xfs_trace.h"
  34. #include "xfs_icache.h"
  35. #include "xfs_bmap_util.h"
  36. #include <linux/kthread.h>
  37. #include <linux/freezer.h>
  38. STATIC void __xfs_inode_clear_reclaim_tag(struct xfs_mount *mp,
  39. struct xfs_perag *pag, struct xfs_inode *ip);
  40. /*
  41. * Allocate and initialise an xfs_inode.
  42. */
  43. struct xfs_inode *
  44. xfs_inode_alloc(
  45. struct xfs_mount *mp,
  46. xfs_ino_t ino)
  47. {
  48. struct xfs_inode *ip;
  49. /*
  50. * if this didn't occur in transactions, we could use
  51. * KM_MAYFAIL and return NULL here on ENOMEM. Set the
  52. * code up to do this anyway.
  53. */
  54. ip = kmem_zone_alloc(xfs_inode_zone, KM_SLEEP);
  55. if (!ip)
  56. return NULL;
  57. if (inode_init_always(mp->m_super, VFS_I(ip))) {
  58. kmem_zone_free(xfs_inode_zone, ip);
  59. return NULL;
  60. }
  61. ASSERT(atomic_read(&ip->i_pincount) == 0);
  62. ASSERT(!spin_is_locked(&ip->i_flags_lock));
  63. ASSERT(!xfs_isiflocked(ip));
  64. ASSERT(ip->i_ino == 0);
  65. mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
  66. /* initialise the xfs inode */
  67. ip->i_ino = ino;
  68. ip->i_mount = mp;
  69. memset(&ip->i_imap, 0, sizeof(struct xfs_imap));
  70. ip->i_afp = NULL;
  71. memset(&ip->i_df, 0, sizeof(xfs_ifork_t));
  72. ip->i_flags = 0;
  73. ip->i_delayed_blks = 0;
  74. memset(&ip->i_d, 0, sizeof(xfs_icdinode_t));
  75. return ip;
  76. }
  77. STATIC void
  78. xfs_inode_free_callback(
  79. struct rcu_head *head)
  80. {
  81. struct inode *inode = container_of(head, struct inode, i_rcu);
  82. struct xfs_inode *ip = XFS_I(inode);
  83. kmem_zone_free(xfs_inode_zone, ip);
  84. }
  85. void
  86. xfs_inode_free(
  87. struct xfs_inode *ip)
  88. {
  89. switch (ip->i_d.di_mode & S_IFMT) {
  90. case S_IFREG:
  91. case S_IFDIR:
  92. case S_IFLNK:
  93. xfs_idestroy_fork(ip, XFS_DATA_FORK);
  94. break;
  95. }
  96. if (ip->i_afp)
  97. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  98. if (ip->i_itemp) {
  99. ASSERT(!(ip->i_itemp->ili_item.li_flags & XFS_LI_IN_AIL));
  100. xfs_inode_item_destroy(ip);
  101. ip->i_itemp = NULL;
  102. }
  103. /*
  104. * Because we use RCU freeing we need to ensure the inode always
  105. * appears to be reclaimed with an invalid inode number when in the
  106. * free state. The ip->i_flags_lock provides the barrier against lookup
  107. * races.
  108. */
  109. spin_lock(&ip->i_flags_lock);
  110. ip->i_flags = XFS_IRECLAIM;
  111. ip->i_ino = 0;
  112. spin_unlock(&ip->i_flags_lock);
  113. /* asserts to verify all state is correct here */
  114. ASSERT(atomic_read(&ip->i_pincount) == 0);
  115. ASSERT(!xfs_isiflocked(ip));
  116. call_rcu(&VFS_I(ip)->i_rcu, xfs_inode_free_callback);
  117. }
  118. /*
  119. * Check the validity of the inode we just found it the cache
  120. */
  121. static int
  122. xfs_iget_cache_hit(
  123. struct xfs_perag *pag,
  124. struct xfs_inode *ip,
  125. xfs_ino_t ino,
  126. int flags,
  127. int lock_flags) __releases(RCU)
  128. {
  129. struct inode *inode = VFS_I(ip);
  130. struct xfs_mount *mp = ip->i_mount;
  131. int error;
  132. /*
  133. * check for re-use of an inode within an RCU grace period due to the
  134. * radix tree nodes not being updated yet. We monitor for this by
  135. * setting the inode number to zero before freeing the inode structure.
  136. * If the inode has been reallocated and set up, then the inode number
  137. * will not match, so check for that, too.
  138. */
  139. spin_lock(&ip->i_flags_lock);
  140. if (ip->i_ino != ino) {
  141. trace_xfs_iget_skip(ip);
  142. XFS_STATS_INC(xs_ig_frecycle);
  143. error = EAGAIN;
  144. goto out_error;
  145. }
  146. /*
  147. * If we are racing with another cache hit that is currently
  148. * instantiating this inode or currently recycling it out of
  149. * reclaimabe state, wait for the initialisation to complete
  150. * before continuing.
  151. *
  152. * XXX(hch): eventually we should do something equivalent to
  153. * wait_on_inode to wait for these flags to be cleared
  154. * instead of polling for it.
  155. */
  156. if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) {
  157. trace_xfs_iget_skip(ip);
  158. XFS_STATS_INC(xs_ig_frecycle);
  159. error = EAGAIN;
  160. goto out_error;
  161. }
  162. /*
  163. * If lookup is racing with unlink return an error immediately.
  164. */
  165. if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) {
  166. error = ENOENT;
  167. goto out_error;
  168. }
  169. /*
  170. * If IRECLAIMABLE is set, we've torn down the VFS inode already.
  171. * Need to carefully get it back into useable state.
  172. */
  173. if (ip->i_flags & XFS_IRECLAIMABLE) {
  174. trace_xfs_iget_reclaim(ip);
  175. /*
  176. * We need to set XFS_IRECLAIM to prevent xfs_reclaim_inode
  177. * from stomping over us while we recycle the inode. We can't
  178. * clear the radix tree reclaimable tag yet as it requires
  179. * pag_ici_lock to be held exclusive.
  180. */
  181. ip->i_flags |= XFS_IRECLAIM;
  182. spin_unlock(&ip->i_flags_lock);
  183. rcu_read_unlock();
  184. error = -inode_init_always(mp->m_super, inode);
  185. if (error) {
  186. /*
  187. * Re-initializing the inode failed, and we are in deep
  188. * trouble. Try to re-add it to the reclaim list.
  189. */
  190. rcu_read_lock();
  191. spin_lock(&ip->i_flags_lock);
  192. ip->i_flags &= ~(XFS_INEW | XFS_IRECLAIM);
  193. ASSERT(ip->i_flags & XFS_IRECLAIMABLE);
  194. trace_xfs_iget_reclaim_fail(ip);
  195. goto out_error;
  196. }
  197. spin_lock(&pag->pag_ici_lock);
  198. spin_lock(&ip->i_flags_lock);
  199. /*
  200. * Clear the per-lifetime state in the inode as we are now
  201. * effectively a new inode and need to return to the initial
  202. * state before reuse occurs.
  203. */
  204. ip->i_flags &= ~XFS_IRECLAIM_RESET_FLAGS;
  205. ip->i_flags |= XFS_INEW;
  206. __xfs_inode_clear_reclaim_tag(mp, pag, ip);
  207. inode->i_state = I_NEW;
  208. ASSERT(!rwsem_is_locked(&ip->i_iolock.mr_lock));
  209. mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
  210. spin_unlock(&ip->i_flags_lock);
  211. spin_unlock(&pag->pag_ici_lock);
  212. } else {
  213. /* If the VFS inode is being torn down, pause and try again. */
  214. if (!igrab(inode)) {
  215. trace_xfs_iget_skip(ip);
  216. error = EAGAIN;
  217. goto out_error;
  218. }
  219. /* We've got a live one. */
  220. spin_unlock(&ip->i_flags_lock);
  221. rcu_read_unlock();
  222. trace_xfs_iget_hit(ip);
  223. }
  224. if (lock_flags != 0)
  225. xfs_ilock(ip, lock_flags);
  226. xfs_iflags_clear(ip, XFS_ISTALE | XFS_IDONTCACHE);
  227. XFS_STATS_INC(xs_ig_found);
  228. return 0;
  229. out_error:
  230. spin_unlock(&ip->i_flags_lock);
  231. rcu_read_unlock();
  232. return error;
  233. }
  234. static int
  235. xfs_iget_cache_miss(
  236. struct xfs_mount *mp,
  237. struct xfs_perag *pag,
  238. xfs_trans_t *tp,
  239. xfs_ino_t ino,
  240. struct xfs_inode **ipp,
  241. int flags,
  242. int lock_flags)
  243. {
  244. struct xfs_inode *ip;
  245. int error;
  246. xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
  247. int iflags;
  248. ip = xfs_inode_alloc(mp, ino);
  249. if (!ip)
  250. return ENOMEM;
  251. error = xfs_iread(mp, tp, ip, flags);
  252. if (error)
  253. goto out_destroy;
  254. trace_xfs_iget_miss(ip);
  255. if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
  256. error = ENOENT;
  257. goto out_destroy;
  258. }
  259. /*
  260. * Preload the radix tree so we can insert safely under the
  261. * write spinlock. Note that we cannot sleep inside the preload
  262. * region. Since we can be called from transaction context, don't
  263. * recurse into the file system.
  264. */
  265. if (radix_tree_preload(GFP_NOFS)) {
  266. error = EAGAIN;
  267. goto out_destroy;
  268. }
  269. /*
  270. * Because the inode hasn't been added to the radix-tree yet it can't
  271. * be found by another thread, so we can do the non-sleeping lock here.
  272. */
  273. if (lock_flags) {
  274. if (!xfs_ilock_nowait(ip, lock_flags))
  275. BUG();
  276. }
  277. /*
  278. * These values must be set before inserting the inode into the radix
  279. * tree as the moment it is inserted a concurrent lookup (allowed by the
  280. * RCU locking mechanism) can find it and that lookup must see that this
  281. * is an inode currently under construction (i.e. that XFS_INEW is set).
  282. * The ip->i_flags_lock that protects the XFS_INEW flag forms the
  283. * memory barrier that ensures this detection works correctly at lookup
  284. * time.
  285. */
  286. iflags = XFS_INEW;
  287. if (flags & XFS_IGET_DONTCACHE)
  288. iflags |= XFS_IDONTCACHE;
  289. ip->i_udquot = NULL;
  290. ip->i_gdquot = NULL;
  291. ip->i_pdquot = NULL;
  292. xfs_iflags_set(ip, iflags);
  293. /* insert the new inode */
  294. spin_lock(&pag->pag_ici_lock);
  295. error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
  296. if (unlikely(error)) {
  297. WARN_ON(error != -EEXIST);
  298. XFS_STATS_INC(xs_ig_dup);
  299. error = EAGAIN;
  300. goto out_preload_end;
  301. }
  302. spin_unlock(&pag->pag_ici_lock);
  303. radix_tree_preload_end();
  304. *ipp = ip;
  305. return 0;
  306. out_preload_end:
  307. spin_unlock(&pag->pag_ici_lock);
  308. radix_tree_preload_end();
  309. if (lock_flags)
  310. xfs_iunlock(ip, lock_flags);
  311. out_destroy:
  312. __destroy_inode(VFS_I(ip));
  313. xfs_inode_free(ip);
  314. return error;
  315. }
  316. /*
  317. * Look up an inode by number in the given file system.
  318. * The inode is looked up in the cache held in each AG.
  319. * If the inode is found in the cache, initialise the vfs inode
  320. * if necessary.
  321. *
  322. * If it is not in core, read it in from the file system's device,
  323. * add it to the cache and initialise the vfs inode.
  324. *
  325. * The inode is locked according to the value of the lock_flags parameter.
  326. * This flag parameter indicates how and if the inode's IO lock and inode lock
  327. * should be taken.
  328. *
  329. * mp -- the mount point structure for the current file system. It points
  330. * to the inode hash table.
  331. * tp -- a pointer to the current transaction if there is one. This is
  332. * simply passed through to the xfs_iread() call.
  333. * ino -- the number of the inode desired. This is the unique identifier
  334. * within the file system for the inode being requested.
  335. * lock_flags -- flags indicating how to lock the inode. See the comment
  336. * for xfs_ilock() for a list of valid values.
  337. */
  338. int
  339. xfs_iget(
  340. xfs_mount_t *mp,
  341. xfs_trans_t *tp,
  342. xfs_ino_t ino,
  343. uint flags,
  344. uint lock_flags,
  345. xfs_inode_t **ipp)
  346. {
  347. xfs_inode_t *ip;
  348. int error;
  349. xfs_perag_t *pag;
  350. xfs_agino_t agino;
  351. /*
  352. * xfs_reclaim_inode() uses the ILOCK to ensure an inode
  353. * doesn't get freed while it's being referenced during a
  354. * radix tree traversal here. It assumes this function
  355. * aqcuires only the ILOCK (and therefore it has no need to
  356. * involve the IOLOCK in this synchronization).
  357. */
  358. ASSERT((lock_flags & (XFS_IOLOCK_EXCL | XFS_IOLOCK_SHARED)) == 0);
  359. /* reject inode numbers outside existing AGs */
  360. if (!ino || XFS_INO_TO_AGNO(mp, ino) >= mp->m_sb.sb_agcount)
  361. return EINVAL;
  362. /* get the perag structure and ensure that it's inode capable */
  363. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ino));
  364. agino = XFS_INO_TO_AGINO(mp, ino);
  365. again:
  366. error = 0;
  367. rcu_read_lock();
  368. ip = radix_tree_lookup(&pag->pag_ici_root, agino);
  369. if (ip) {
  370. error = xfs_iget_cache_hit(pag, ip, ino, flags, lock_flags);
  371. if (error)
  372. goto out_error_or_again;
  373. } else {
  374. rcu_read_unlock();
  375. XFS_STATS_INC(xs_ig_missed);
  376. error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip,
  377. flags, lock_flags);
  378. if (error)
  379. goto out_error_or_again;
  380. }
  381. xfs_perag_put(pag);
  382. *ipp = ip;
  383. /*
  384. * If we have a real type for an on-disk inode, we can set ops(&unlock)
  385. * now. If it's a new inode being created, xfs_ialloc will handle it.
  386. */
  387. if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0)
  388. xfs_setup_inode(ip);
  389. return 0;
  390. out_error_or_again:
  391. if (error == EAGAIN) {
  392. delay(1);
  393. goto again;
  394. }
  395. xfs_perag_put(pag);
  396. return error;
  397. }
  398. /*
  399. * The inode lookup is done in batches to keep the amount of lock traffic and
  400. * radix tree lookups to a minimum. The batch size is a trade off between
  401. * lookup reduction and stack usage. This is in the reclaim path, so we can't
  402. * be too greedy.
  403. */
  404. #define XFS_LOOKUP_BATCH 32
  405. STATIC int
  406. xfs_inode_ag_walk_grab(
  407. struct xfs_inode *ip)
  408. {
  409. struct inode *inode = VFS_I(ip);
  410. ASSERT(rcu_read_lock_held());
  411. /*
  412. * check for stale RCU freed inode
  413. *
  414. * If the inode has been reallocated, it doesn't matter if it's not in
  415. * the AG we are walking - we are walking for writeback, so if it
  416. * passes all the "valid inode" checks and is dirty, then we'll write
  417. * it back anyway. If it has been reallocated and still being
  418. * initialised, the XFS_INEW check below will catch it.
  419. */
  420. spin_lock(&ip->i_flags_lock);
  421. if (!ip->i_ino)
  422. goto out_unlock_noent;
  423. /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
  424. if (__xfs_iflags_test(ip, XFS_INEW | XFS_IRECLAIMABLE | XFS_IRECLAIM))
  425. goto out_unlock_noent;
  426. spin_unlock(&ip->i_flags_lock);
  427. /* nothing to sync during shutdown */
  428. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  429. return EFSCORRUPTED;
  430. /* If we can't grab the inode, it must on it's way to reclaim. */
  431. if (!igrab(inode))
  432. return ENOENT;
  433. /* inode is valid */
  434. return 0;
  435. out_unlock_noent:
  436. spin_unlock(&ip->i_flags_lock);
  437. return ENOENT;
  438. }
  439. STATIC int
  440. xfs_inode_ag_walk(
  441. struct xfs_mount *mp,
  442. struct xfs_perag *pag,
  443. int (*execute)(struct xfs_inode *ip, int flags,
  444. void *args),
  445. int flags,
  446. void *args,
  447. int tag)
  448. {
  449. uint32_t first_index;
  450. int last_error = 0;
  451. int skipped;
  452. int done;
  453. int nr_found;
  454. restart:
  455. done = 0;
  456. skipped = 0;
  457. first_index = 0;
  458. nr_found = 0;
  459. do {
  460. struct xfs_inode *batch[XFS_LOOKUP_BATCH];
  461. int error = 0;
  462. int i;
  463. rcu_read_lock();
  464. if (tag == -1)
  465. nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
  466. (void **)batch, first_index,
  467. XFS_LOOKUP_BATCH);
  468. else
  469. nr_found = radix_tree_gang_lookup_tag(
  470. &pag->pag_ici_root,
  471. (void **) batch, first_index,
  472. XFS_LOOKUP_BATCH, tag);
  473. if (!nr_found) {
  474. rcu_read_unlock();
  475. break;
  476. }
  477. /*
  478. * Grab the inodes before we drop the lock. if we found
  479. * nothing, nr == 0 and the loop will be skipped.
  480. */
  481. for (i = 0; i < nr_found; i++) {
  482. struct xfs_inode *ip = batch[i];
  483. if (done || xfs_inode_ag_walk_grab(ip))
  484. batch[i] = NULL;
  485. /*
  486. * Update the index for the next lookup. Catch
  487. * overflows into the next AG range which can occur if
  488. * we have inodes in the last block of the AG and we
  489. * are currently pointing to the last inode.
  490. *
  491. * Because we may see inodes that are from the wrong AG
  492. * due to RCU freeing and reallocation, only update the
  493. * index if it lies in this AG. It was a race that lead
  494. * us to see this inode, so another lookup from the
  495. * same index will not find it again.
  496. */
  497. if (XFS_INO_TO_AGNO(mp, ip->i_ino) != pag->pag_agno)
  498. continue;
  499. first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
  500. if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
  501. done = 1;
  502. }
  503. /* unlock now we've grabbed the inodes. */
  504. rcu_read_unlock();
  505. for (i = 0; i < nr_found; i++) {
  506. if (!batch[i])
  507. continue;
  508. error = execute(batch[i], flags, args);
  509. IRELE(batch[i]);
  510. if (error == EAGAIN) {
  511. skipped++;
  512. continue;
  513. }
  514. if (error && last_error != EFSCORRUPTED)
  515. last_error = error;
  516. }
  517. /* bail out if the filesystem is corrupted. */
  518. if (error == EFSCORRUPTED)
  519. break;
  520. cond_resched();
  521. } while (nr_found && !done);
  522. if (skipped) {
  523. delay(1);
  524. goto restart;
  525. }
  526. return last_error;
  527. }
  528. /*
  529. * Background scanning to trim post-EOF preallocated space. This is queued
  530. * based on the 'speculative_prealloc_lifetime' tunable (5m by default).
  531. */
  532. STATIC void
  533. xfs_queue_eofblocks(
  534. struct xfs_mount *mp)
  535. {
  536. rcu_read_lock();
  537. if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_EOFBLOCKS_TAG))
  538. queue_delayed_work(mp->m_eofblocks_workqueue,
  539. &mp->m_eofblocks_work,
  540. msecs_to_jiffies(xfs_eofb_secs * 1000));
  541. rcu_read_unlock();
  542. }
  543. void
  544. xfs_eofblocks_worker(
  545. struct work_struct *work)
  546. {
  547. struct xfs_mount *mp = container_of(to_delayed_work(work),
  548. struct xfs_mount, m_eofblocks_work);
  549. xfs_icache_free_eofblocks(mp, NULL);
  550. xfs_queue_eofblocks(mp);
  551. }
  552. int
  553. xfs_inode_ag_iterator(
  554. struct xfs_mount *mp,
  555. int (*execute)(struct xfs_inode *ip, int flags,
  556. void *args),
  557. int flags,
  558. void *args)
  559. {
  560. struct xfs_perag *pag;
  561. int error = 0;
  562. int last_error = 0;
  563. xfs_agnumber_t ag;
  564. ag = 0;
  565. while ((pag = xfs_perag_get(mp, ag))) {
  566. ag = pag->pag_agno + 1;
  567. error = xfs_inode_ag_walk(mp, pag, execute, flags, args, -1);
  568. xfs_perag_put(pag);
  569. if (error) {
  570. last_error = error;
  571. if (error == EFSCORRUPTED)
  572. break;
  573. }
  574. }
  575. return XFS_ERROR(last_error);
  576. }
  577. int
  578. xfs_inode_ag_iterator_tag(
  579. struct xfs_mount *mp,
  580. int (*execute)(struct xfs_inode *ip, int flags,
  581. void *args),
  582. int flags,
  583. void *args,
  584. int tag)
  585. {
  586. struct xfs_perag *pag;
  587. int error = 0;
  588. int last_error = 0;
  589. xfs_agnumber_t ag;
  590. ag = 0;
  591. while ((pag = xfs_perag_get_tag(mp, ag, tag))) {
  592. ag = pag->pag_agno + 1;
  593. error = xfs_inode_ag_walk(mp, pag, execute, flags, args, tag);
  594. xfs_perag_put(pag);
  595. if (error) {
  596. last_error = error;
  597. if (error == EFSCORRUPTED)
  598. break;
  599. }
  600. }
  601. return XFS_ERROR(last_error);
  602. }
  603. /*
  604. * Queue a new inode reclaim pass if there are reclaimable inodes and there
  605. * isn't a reclaim pass already in progress. By default it runs every 5s based
  606. * on the xfs periodic sync default of 30s. Perhaps this should have it's own
  607. * tunable, but that can be done if this method proves to be ineffective or too
  608. * aggressive.
  609. */
  610. static void
  611. xfs_reclaim_work_queue(
  612. struct xfs_mount *mp)
  613. {
  614. rcu_read_lock();
  615. if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_RECLAIM_TAG)) {
  616. queue_delayed_work(mp->m_reclaim_workqueue, &mp->m_reclaim_work,
  617. msecs_to_jiffies(xfs_syncd_centisecs / 6 * 10));
  618. }
  619. rcu_read_unlock();
  620. }
  621. /*
  622. * This is a fast pass over the inode cache to try to get reclaim moving on as
  623. * many inodes as possible in a short period of time. It kicks itself every few
  624. * seconds, as well as being kicked by the inode cache shrinker when memory
  625. * goes low. It scans as quickly as possible avoiding locked inodes or those
  626. * already being flushed, and once done schedules a future pass.
  627. */
  628. void
  629. xfs_reclaim_worker(
  630. struct work_struct *work)
  631. {
  632. struct xfs_mount *mp = container_of(to_delayed_work(work),
  633. struct xfs_mount, m_reclaim_work);
  634. xfs_reclaim_inodes(mp, SYNC_TRYLOCK);
  635. xfs_reclaim_work_queue(mp);
  636. }
  637. static void
  638. __xfs_inode_set_reclaim_tag(
  639. struct xfs_perag *pag,
  640. struct xfs_inode *ip)
  641. {
  642. radix_tree_tag_set(&pag->pag_ici_root,
  643. XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
  644. XFS_ICI_RECLAIM_TAG);
  645. if (!pag->pag_ici_reclaimable) {
  646. /* propagate the reclaim tag up into the perag radix tree */
  647. spin_lock(&ip->i_mount->m_perag_lock);
  648. radix_tree_tag_set(&ip->i_mount->m_perag_tree,
  649. XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
  650. XFS_ICI_RECLAIM_TAG);
  651. spin_unlock(&ip->i_mount->m_perag_lock);
  652. /* schedule periodic background inode reclaim */
  653. xfs_reclaim_work_queue(ip->i_mount);
  654. trace_xfs_perag_set_reclaim(ip->i_mount, pag->pag_agno,
  655. -1, _RET_IP_);
  656. }
  657. pag->pag_ici_reclaimable++;
  658. }
  659. /*
  660. * We set the inode flag atomically with the radix tree tag.
  661. * Once we get tag lookups on the radix tree, this inode flag
  662. * can go away.
  663. */
  664. void
  665. xfs_inode_set_reclaim_tag(
  666. xfs_inode_t *ip)
  667. {
  668. struct xfs_mount *mp = ip->i_mount;
  669. struct xfs_perag *pag;
  670. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
  671. spin_lock(&pag->pag_ici_lock);
  672. spin_lock(&ip->i_flags_lock);
  673. __xfs_inode_set_reclaim_tag(pag, ip);
  674. __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
  675. spin_unlock(&ip->i_flags_lock);
  676. spin_unlock(&pag->pag_ici_lock);
  677. xfs_perag_put(pag);
  678. }
  679. STATIC void
  680. __xfs_inode_clear_reclaim(
  681. xfs_perag_t *pag,
  682. xfs_inode_t *ip)
  683. {
  684. pag->pag_ici_reclaimable--;
  685. if (!pag->pag_ici_reclaimable) {
  686. /* clear the reclaim tag from the perag radix tree */
  687. spin_lock(&ip->i_mount->m_perag_lock);
  688. radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
  689. XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
  690. XFS_ICI_RECLAIM_TAG);
  691. spin_unlock(&ip->i_mount->m_perag_lock);
  692. trace_xfs_perag_clear_reclaim(ip->i_mount, pag->pag_agno,
  693. -1, _RET_IP_);
  694. }
  695. }
  696. STATIC void
  697. __xfs_inode_clear_reclaim_tag(
  698. xfs_mount_t *mp,
  699. xfs_perag_t *pag,
  700. xfs_inode_t *ip)
  701. {
  702. radix_tree_tag_clear(&pag->pag_ici_root,
  703. XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
  704. __xfs_inode_clear_reclaim(pag, ip);
  705. }
  706. /*
  707. * Grab the inode for reclaim exclusively.
  708. * Return 0 if we grabbed it, non-zero otherwise.
  709. */
  710. STATIC int
  711. xfs_reclaim_inode_grab(
  712. struct xfs_inode *ip,
  713. int flags)
  714. {
  715. ASSERT(rcu_read_lock_held());
  716. /* quick check for stale RCU freed inode */
  717. if (!ip->i_ino)
  718. return 1;
  719. /*
  720. * If we are asked for non-blocking operation, do unlocked checks to
  721. * see if the inode already is being flushed or in reclaim to avoid
  722. * lock traffic.
  723. */
  724. if ((flags & SYNC_TRYLOCK) &&
  725. __xfs_iflags_test(ip, XFS_IFLOCK | XFS_IRECLAIM))
  726. return 1;
  727. /*
  728. * The radix tree lock here protects a thread in xfs_iget from racing
  729. * with us starting reclaim on the inode. Once we have the
  730. * XFS_IRECLAIM flag set it will not touch us.
  731. *
  732. * Due to RCU lookup, we may find inodes that have been freed and only
  733. * have XFS_IRECLAIM set. Indeed, we may see reallocated inodes that
  734. * aren't candidates for reclaim at all, so we must check the
  735. * XFS_IRECLAIMABLE is set first before proceeding to reclaim.
  736. */
  737. spin_lock(&ip->i_flags_lock);
  738. if (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) ||
  739. __xfs_iflags_test(ip, XFS_IRECLAIM)) {
  740. /* not a reclaim candidate. */
  741. spin_unlock(&ip->i_flags_lock);
  742. return 1;
  743. }
  744. __xfs_iflags_set(ip, XFS_IRECLAIM);
  745. spin_unlock(&ip->i_flags_lock);
  746. return 0;
  747. }
  748. /*
  749. * Inodes in different states need to be treated differently. The following
  750. * table lists the inode states and the reclaim actions necessary:
  751. *
  752. * inode state iflush ret required action
  753. * --------------- ---------- ---------------
  754. * bad - reclaim
  755. * shutdown EIO unpin and reclaim
  756. * clean, unpinned 0 reclaim
  757. * stale, unpinned 0 reclaim
  758. * clean, pinned(*) 0 requeue
  759. * stale, pinned EAGAIN requeue
  760. * dirty, async - requeue
  761. * dirty, sync 0 reclaim
  762. *
  763. * (*) dgc: I don't think the clean, pinned state is possible but it gets
  764. * handled anyway given the order of checks implemented.
  765. *
  766. * Also, because we get the flush lock first, we know that any inode that has
  767. * been flushed delwri has had the flush completed by the time we check that
  768. * the inode is clean.
  769. *
  770. * Note that because the inode is flushed delayed write by AIL pushing, the
  771. * flush lock may already be held here and waiting on it can result in very
  772. * long latencies. Hence for sync reclaims, where we wait on the flush lock,
  773. * the caller should push the AIL first before trying to reclaim inodes to
  774. * minimise the amount of time spent waiting. For background relaim, we only
  775. * bother to reclaim clean inodes anyway.
  776. *
  777. * Hence the order of actions after gaining the locks should be:
  778. * bad => reclaim
  779. * shutdown => unpin and reclaim
  780. * pinned, async => requeue
  781. * pinned, sync => unpin
  782. * stale => reclaim
  783. * clean => reclaim
  784. * dirty, async => requeue
  785. * dirty, sync => flush, wait and reclaim
  786. */
  787. STATIC int
  788. xfs_reclaim_inode(
  789. struct xfs_inode *ip,
  790. struct xfs_perag *pag,
  791. int sync_mode)
  792. {
  793. struct xfs_buf *bp = NULL;
  794. int error;
  795. restart:
  796. error = 0;
  797. xfs_ilock(ip, XFS_ILOCK_EXCL);
  798. if (!xfs_iflock_nowait(ip)) {
  799. if (!(sync_mode & SYNC_WAIT))
  800. goto out;
  801. xfs_iflock(ip);
  802. }
  803. if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  804. xfs_iunpin_wait(ip);
  805. xfs_iflush_abort(ip, false);
  806. goto reclaim;
  807. }
  808. if (xfs_ipincount(ip)) {
  809. if (!(sync_mode & SYNC_WAIT))
  810. goto out_ifunlock;
  811. xfs_iunpin_wait(ip);
  812. }
  813. if (xfs_iflags_test(ip, XFS_ISTALE))
  814. goto reclaim;
  815. if (xfs_inode_clean(ip))
  816. goto reclaim;
  817. /*
  818. * Never flush out dirty data during non-blocking reclaim, as it would
  819. * just contend with AIL pushing trying to do the same job.
  820. */
  821. if (!(sync_mode & SYNC_WAIT))
  822. goto out_ifunlock;
  823. /*
  824. * Now we have an inode that needs flushing.
  825. *
  826. * Note that xfs_iflush will never block on the inode buffer lock, as
  827. * xfs_ifree_cluster() can lock the inode buffer before it locks the
  828. * ip->i_lock, and we are doing the exact opposite here. As a result,
  829. * doing a blocking xfs_imap_to_bp() to get the cluster buffer would
  830. * result in an ABBA deadlock with xfs_ifree_cluster().
  831. *
  832. * As xfs_ifree_cluser() must gather all inodes that are active in the
  833. * cache to mark them stale, if we hit this case we don't actually want
  834. * to do IO here - we want the inode marked stale so we can simply
  835. * reclaim it. Hence if we get an EAGAIN error here, just unlock the
  836. * inode, back off and try again. Hopefully the next pass through will
  837. * see the stale flag set on the inode.
  838. */
  839. error = xfs_iflush(ip, &bp);
  840. if (error == EAGAIN) {
  841. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  842. /* backoff longer than in xfs_ifree_cluster */
  843. delay(2);
  844. goto restart;
  845. }
  846. if (!error) {
  847. error = xfs_bwrite(bp);
  848. xfs_buf_relse(bp);
  849. }
  850. xfs_iflock(ip);
  851. reclaim:
  852. xfs_ifunlock(ip);
  853. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  854. XFS_STATS_INC(xs_ig_reclaims);
  855. /*
  856. * Remove the inode from the per-AG radix tree.
  857. *
  858. * Because radix_tree_delete won't complain even if the item was never
  859. * added to the tree assert that it's been there before to catch
  860. * problems with the inode life time early on.
  861. */
  862. spin_lock(&pag->pag_ici_lock);
  863. if (!radix_tree_delete(&pag->pag_ici_root,
  864. XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino)))
  865. ASSERT(0);
  866. __xfs_inode_clear_reclaim(pag, ip);
  867. spin_unlock(&pag->pag_ici_lock);
  868. /*
  869. * Here we do an (almost) spurious inode lock in order to coordinate
  870. * with inode cache radix tree lookups. This is because the lookup
  871. * can reference the inodes in the cache without taking references.
  872. *
  873. * We make that OK here by ensuring that we wait until the inode is
  874. * unlocked after the lookup before we go ahead and free it.
  875. */
  876. xfs_ilock(ip, XFS_ILOCK_EXCL);
  877. xfs_qm_dqdetach(ip);
  878. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  879. xfs_inode_free(ip);
  880. return error;
  881. out_ifunlock:
  882. xfs_ifunlock(ip);
  883. out:
  884. xfs_iflags_clear(ip, XFS_IRECLAIM);
  885. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  886. /*
  887. * We could return EAGAIN here to make reclaim rescan the inode tree in
  888. * a short while. However, this just burns CPU time scanning the tree
  889. * waiting for IO to complete and the reclaim work never goes back to
  890. * the idle state. Instead, return 0 to let the next scheduled
  891. * background reclaim attempt to reclaim the inode again.
  892. */
  893. return 0;
  894. }
  895. /*
  896. * Walk the AGs and reclaim the inodes in them. Even if the filesystem is
  897. * corrupted, we still want to try to reclaim all the inodes. If we don't,
  898. * then a shut down during filesystem unmount reclaim walk leak all the
  899. * unreclaimed inodes.
  900. */
  901. STATIC int
  902. xfs_reclaim_inodes_ag(
  903. struct xfs_mount *mp,
  904. int flags,
  905. int *nr_to_scan)
  906. {
  907. struct xfs_perag *pag;
  908. int error = 0;
  909. int last_error = 0;
  910. xfs_agnumber_t ag;
  911. int trylock = flags & SYNC_TRYLOCK;
  912. int skipped;
  913. restart:
  914. ag = 0;
  915. skipped = 0;
  916. while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
  917. unsigned long first_index = 0;
  918. int done = 0;
  919. int nr_found = 0;
  920. ag = pag->pag_agno + 1;
  921. if (trylock) {
  922. if (!mutex_trylock(&pag->pag_ici_reclaim_lock)) {
  923. skipped++;
  924. xfs_perag_put(pag);
  925. continue;
  926. }
  927. first_index = pag->pag_ici_reclaim_cursor;
  928. } else
  929. mutex_lock(&pag->pag_ici_reclaim_lock);
  930. do {
  931. struct xfs_inode *batch[XFS_LOOKUP_BATCH];
  932. int i;
  933. rcu_read_lock();
  934. nr_found = radix_tree_gang_lookup_tag(
  935. &pag->pag_ici_root,
  936. (void **)batch, first_index,
  937. XFS_LOOKUP_BATCH,
  938. XFS_ICI_RECLAIM_TAG);
  939. if (!nr_found) {
  940. done = 1;
  941. rcu_read_unlock();
  942. break;
  943. }
  944. /*
  945. * Grab the inodes before we drop the lock. if we found
  946. * nothing, nr == 0 and the loop will be skipped.
  947. */
  948. for (i = 0; i < nr_found; i++) {
  949. struct xfs_inode *ip = batch[i];
  950. if (done || xfs_reclaim_inode_grab(ip, flags))
  951. batch[i] = NULL;
  952. /*
  953. * Update the index for the next lookup. Catch
  954. * overflows into the next AG range which can
  955. * occur if we have inodes in the last block of
  956. * the AG and we are currently pointing to the
  957. * last inode.
  958. *
  959. * Because we may see inodes that are from the
  960. * wrong AG due to RCU freeing and
  961. * reallocation, only update the index if it
  962. * lies in this AG. It was a race that lead us
  963. * to see this inode, so another lookup from
  964. * the same index will not find it again.
  965. */
  966. if (XFS_INO_TO_AGNO(mp, ip->i_ino) !=
  967. pag->pag_agno)
  968. continue;
  969. first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
  970. if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
  971. done = 1;
  972. }
  973. /* unlock now we've grabbed the inodes. */
  974. rcu_read_unlock();
  975. for (i = 0; i < nr_found; i++) {
  976. if (!batch[i])
  977. continue;
  978. error = xfs_reclaim_inode(batch[i], pag, flags);
  979. if (error && last_error != EFSCORRUPTED)
  980. last_error = error;
  981. }
  982. *nr_to_scan -= XFS_LOOKUP_BATCH;
  983. cond_resched();
  984. } while (nr_found && !done && *nr_to_scan > 0);
  985. if (trylock && !done)
  986. pag->pag_ici_reclaim_cursor = first_index;
  987. else
  988. pag->pag_ici_reclaim_cursor = 0;
  989. mutex_unlock(&pag->pag_ici_reclaim_lock);
  990. xfs_perag_put(pag);
  991. }
  992. /*
  993. * if we skipped any AG, and we still have scan count remaining, do
  994. * another pass this time using blocking reclaim semantics (i.e
  995. * waiting on the reclaim locks and ignoring the reclaim cursors). This
  996. * ensure that when we get more reclaimers than AGs we block rather
  997. * than spin trying to execute reclaim.
  998. */
  999. if (skipped && (flags & SYNC_WAIT) && *nr_to_scan > 0) {
  1000. trylock = 0;
  1001. goto restart;
  1002. }
  1003. return XFS_ERROR(last_error);
  1004. }
  1005. int
  1006. xfs_reclaim_inodes(
  1007. xfs_mount_t *mp,
  1008. int mode)
  1009. {
  1010. int nr_to_scan = INT_MAX;
  1011. return xfs_reclaim_inodes_ag(mp, mode, &nr_to_scan);
  1012. }
  1013. /*
  1014. * Scan a certain number of inodes for reclaim.
  1015. *
  1016. * When called we make sure that there is a background (fast) inode reclaim in
  1017. * progress, while we will throttle the speed of reclaim via doing synchronous
  1018. * reclaim of inodes. That means if we come across dirty inodes, we wait for
  1019. * them to be cleaned, which we hope will not be very long due to the
  1020. * background walker having already kicked the IO off on those dirty inodes.
  1021. */
  1022. long
  1023. xfs_reclaim_inodes_nr(
  1024. struct xfs_mount *mp,
  1025. int nr_to_scan)
  1026. {
  1027. /* kick background reclaimer and push the AIL */
  1028. xfs_reclaim_work_queue(mp);
  1029. xfs_ail_push_all(mp->m_ail);
  1030. return xfs_reclaim_inodes_ag(mp, SYNC_TRYLOCK | SYNC_WAIT, &nr_to_scan);
  1031. }
  1032. /*
  1033. * Return the number of reclaimable inodes in the filesystem for
  1034. * the shrinker to determine how much to reclaim.
  1035. */
  1036. int
  1037. xfs_reclaim_inodes_count(
  1038. struct xfs_mount *mp)
  1039. {
  1040. struct xfs_perag *pag;
  1041. xfs_agnumber_t ag = 0;
  1042. int reclaimable = 0;
  1043. while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
  1044. ag = pag->pag_agno + 1;
  1045. reclaimable += pag->pag_ici_reclaimable;
  1046. xfs_perag_put(pag);
  1047. }
  1048. return reclaimable;
  1049. }
  1050. STATIC int
  1051. xfs_inode_match_id(
  1052. struct xfs_inode *ip,
  1053. struct xfs_eofblocks *eofb)
  1054. {
  1055. if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
  1056. !uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
  1057. return 0;
  1058. if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
  1059. !gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
  1060. return 0;
  1061. if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
  1062. xfs_get_projid(ip) != eofb->eof_prid)
  1063. return 0;
  1064. return 1;
  1065. }
  1066. STATIC int
  1067. xfs_inode_free_eofblocks(
  1068. struct xfs_inode *ip,
  1069. int flags,
  1070. void *args)
  1071. {
  1072. int ret;
  1073. struct xfs_eofblocks *eofb = args;
  1074. if (!xfs_can_free_eofblocks(ip, false)) {
  1075. /* inode could be preallocated or append-only */
  1076. trace_xfs_inode_free_eofblocks_invalid(ip);
  1077. xfs_inode_clear_eofblocks_tag(ip);
  1078. return 0;
  1079. }
  1080. /*
  1081. * If the mapping is dirty the operation can block and wait for some
  1082. * time. Unless we are waiting, skip it.
  1083. */
  1084. if (!(flags & SYNC_WAIT) &&
  1085. mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY))
  1086. return 0;
  1087. if (eofb) {
  1088. if (!xfs_inode_match_id(ip, eofb))
  1089. return 0;
  1090. /* skip the inode if the file size is too small */
  1091. if (eofb->eof_flags & XFS_EOF_FLAGS_MINFILESIZE &&
  1092. XFS_ISIZE(ip) < eofb->eof_min_file_size)
  1093. return 0;
  1094. }
  1095. ret = xfs_free_eofblocks(ip->i_mount, ip, true);
  1096. /* don't revisit the inode if we're not waiting */
  1097. if (ret == EAGAIN && !(flags & SYNC_WAIT))
  1098. ret = 0;
  1099. return ret;
  1100. }
  1101. int
  1102. xfs_icache_free_eofblocks(
  1103. struct xfs_mount *mp,
  1104. struct xfs_eofblocks *eofb)
  1105. {
  1106. int flags = SYNC_TRYLOCK;
  1107. if (eofb && (eofb->eof_flags & XFS_EOF_FLAGS_SYNC))
  1108. flags = SYNC_WAIT;
  1109. return xfs_inode_ag_iterator_tag(mp, xfs_inode_free_eofblocks, flags,
  1110. eofb, XFS_ICI_EOFBLOCKS_TAG);
  1111. }
  1112. void
  1113. xfs_inode_set_eofblocks_tag(
  1114. xfs_inode_t *ip)
  1115. {
  1116. struct xfs_mount *mp = ip->i_mount;
  1117. struct xfs_perag *pag;
  1118. int tagged;
  1119. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
  1120. spin_lock(&pag->pag_ici_lock);
  1121. trace_xfs_inode_set_eofblocks_tag(ip);
  1122. tagged = radix_tree_tagged(&pag->pag_ici_root,
  1123. XFS_ICI_EOFBLOCKS_TAG);
  1124. radix_tree_tag_set(&pag->pag_ici_root,
  1125. XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
  1126. XFS_ICI_EOFBLOCKS_TAG);
  1127. if (!tagged) {
  1128. /* propagate the eofblocks tag up into the perag radix tree */
  1129. spin_lock(&ip->i_mount->m_perag_lock);
  1130. radix_tree_tag_set(&ip->i_mount->m_perag_tree,
  1131. XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
  1132. XFS_ICI_EOFBLOCKS_TAG);
  1133. spin_unlock(&ip->i_mount->m_perag_lock);
  1134. /* kick off background trimming */
  1135. xfs_queue_eofblocks(ip->i_mount);
  1136. trace_xfs_perag_set_eofblocks(ip->i_mount, pag->pag_agno,
  1137. -1, _RET_IP_);
  1138. }
  1139. spin_unlock(&pag->pag_ici_lock);
  1140. xfs_perag_put(pag);
  1141. }
  1142. void
  1143. xfs_inode_clear_eofblocks_tag(
  1144. xfs_inode_t *ip)
  1145. {
  1146. struct xfs_mount *mp = ip->i_mount;
  1147. struct xfs_perag *pag;
  1148. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
  1149. spin_lock(&pag->pag_ici_lock);
  1150. trace_xfs_inode_clear_eofblocks_tag(ip);
  1151. radix_tree_tag_clear(&pag->pag_ici_root,
  1152. XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
  1153. XFS_ICI_EOFBLOCKS_TAG);
  1154. if (!radix_tree_tagged(&pag->pag_ici_root, XFS_ICI_EOFBLOCKS_TAG)) {
  1155. /* clear the eofblocks tag from the perag radix tree */
  1156. spin_lock(&ip->i_mount->m_perag_lock);
  1157. radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
  1158. XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
  1159. XFS_ICI_EOFBLOCKS_TAG);
  1160. spin_unlock(&ip->i_mount->m_perag_lock);
  1161. trace_xfs_perag_clear_eofblocks(ip->i_mount, pag->pag_agno,
  1162. -1, _RET_IP_);
  1163. }
  1164. spin_unlock(&pag->pag_ici_lock);
  1165. xfs_perag_put(pag);
  1166. }