xfs_icache.c 34 KB

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