xfs_icache.c 34 KB

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