xfs_icache.c 34 KB

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