xfs_icache.c 34 KB

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