xfs_icache.c 38 KB

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