xfs_icache.c 33 KB

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