xfs_icache.c 46 KB

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