xfs_icache.c 43 KB

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