xfs_icache.c 34 KB

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