xfs_icache.c 36 KB

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