xfs_icache.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  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_inum.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_inode.h"
  28. #include "xfs_error.h"
  29. #include "xfs_trans.h"
  30. #include "xfs_trans_priv.h"
  31. #include "xfs_inode_item.h"
  32. #include "xfs_quota.h"
  33. #include "xfs_trace.h"
  34. #include "xfs_icache.h"
  35. #include "xfs_bmap_util.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. ASSERT(atomic_read(&ip->i_pincount) == 0);
  62. ASSERT(!spin_is_locked(&ip->i_flags_lock));
  63. ASSERT(!xfs_isiflocked(ip));
  64. ASSERT(ip->i_ino == 0);
  65. mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
  66. /* initialise the xfs inode */
  67. ip->i_ino = ino;
  68. ip->i_mount = mp;
  69. memset(&ip->i_imap, 0, sizeof(struct xfs_imap));
  70. ip->i_afp = NULL;
  71. memset(&ip->i_df, 0, sizeof(xfs_ifork_t));
  72. ip->i_flags = 0;
  73. ip->i_delayed_blks = 0;
  74. memset(&ip->i_d, 0, sizeof(xfs_icdinode_t));
  75. return ip;
  76. }
  77. STATIC void
  78. xfs_inode_free_callback(
  79. struct rcu_head *head)
  80. {
  81. struct inode *inode = container_of(head, struct inode, i_rcu);
  82. struct xfs_inode *ip = XFS_I(inode);
  83. kmem_zone_free(xfs_inode_zone, ip);
  84. }
  85. void
  86. xfs_inode_free(
  87. struct xfs_inode *ip)
  88. {
  89. switch (ip->i_d.di_mode & S_IFMT) {
  90. case S_IFREG:
  91. case S_IFDIR:
  92. case S_IFLNK:
  93. xfs_idestroy_fork(ip, XFS_DATA_FORK);
  94. break;
  95. }
  96. if (ip->i_afp)
  97. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  98. if (ip->i_itemp) {
  99. ASSERT(!(ip->i_itemp->ili_item.li_flags & XFS_LI_IN_AIL));
  100. xfs_inode_item_destroy(ip);
  101. ip->i_itemp = NULL;
  102. }
  103. /*
  104. * Because we use RCU freeing we need to ensure the inode always
  105. * appears to be reclaimed with an invalid inode number when in the
  106. * free state. The ip->i_flags_lock provides the barrier against lookup
  107. * races.
  108. */
  109. spin_lock(&ip->i_flags_lock);
  110. ip->i_flags = XFS_IRECLAIM;
  111. ip->i_ino = 0;
  112. spin_unlock(&ip->i_flags_lock);
  113. /* asserts to verify all state is correct here */
  114. ASSERT(atomic_read(&ip->i_pincount) == 0);
  115. ASSERT(!xfs_isiflocked(ip));
  116. call_rcu(&VFS_I(ip)->i_rcu, xfs_inode_free_callback);
  117. }
  118. /*
  119. * Check the validity of the inode we just found it the cache
  120. */
  121. static int
  122. xfs_iget_cache_hit(
  123. struct xfs_perag *pag,
  124. struct xfs_inode *ip,
  125. xfs_ino_t ino,
  126. int flags,
  127. int lock_flags) __releases(RCU)
  128. {
  129. struct inode *inode = VFS_I(ip);
  130. struct xfs_mount *mp = ip->i_mount;
  131. int error;
  132. /*
  133. * check for re-use of an inode within an RCU grace period due to the
  134. * radix tree nodes not being updated yet. We monitor for this by
  135. * setting the inode number to zero before freeing the inode structure.
  136. * If the inode has been reallocated and set up, then the inode number
  137. * will not match, so check for that, too.
  138. */
  139. spin_lock(&ip->i_flags_lock);
  140. if (ip->i_ino != ino) {
  141. trace_xfs_iget_skip(ip);
  142. XFS_STATS_INC(xs_ig_frecycle);
  143. error = EAGAIN;
  144. goto out_error;
  145. }
  146. /*
  147. * If we are racing with another cache hit that is currently
  148. * instantiating this inode or currently recycling it out of
  149. * reclaimabe state, wait for the initialisation to complete
  150. * before continuing.
  151. *
  152. * XXX(hch): eventually we should do something equivalent to
  153. * wait_on_inode to wait for these flags to be cleared
  154. * instead of polling for it.
  155. */
  156. if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) {
  157. trace_xfs_iget_skip(ip);
  158. XFS_STATS_INC(xs_ig_frecycle);
  159. error = EAGAIN;
  160. goto out_error;
  161. }
  162. /*
  163. * If lookup is racing with unlink return an error immediately.
  164. */
  165. if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) {
  166. error = ENOENT;
  167. goto out_error;
  168. }
  169. /*
  170. * If IRECLAIMABLE is set, we've torn down the VFS inode already.
  171. * Need to carefully get it back into useable state.
  172. */
  173. if (ip->i_flags & XFS_IRECLAIMABLE) {
  174. trace_xfs_iget_reclaim(ip);
  175. /*
  176. * We need to set XFS_IRECLAIM to prevent xfs_reclaim_inode
  177. * from stomping over us while we recycle the inode. We can't
  178. * clear the radix tree reclaimable tag yet as it requires
  179. * pag_ici_lock to be held exclusive.
  180. */
  181. ip->i_flags |= XFS_IRECLAIM;
  182. spin_unlock(&ip->i_flags_lock);
  183. rcu_read_unlock();
  184. error = -inode_init_always(mp->m_super, inode);
  185. if (error) {
  186. /*
  187. * Re-initializing the inode failed, and we are in deep
  188. * trouble. Try to re-add it to the reclaim list.
  189. */
  190. rcu_read_lock();
  191. spin_lock(&ip->i_flags_lock);
  192. ip->i_flags &= ~(XFS_INEW | XFS_IRECLAIM);
  193. ASSERT(ip->i_flags & XFS_IRECLAIMABLE);
  194. trace_xfs_iget_reclaim_fail(ip);
  195. goto out_error;
  196. }
  197. spin_lock(&pag->pag_ici_lock);
  198. spin_lock(&ip->i_flags_lock);
  199. /*
  200. * Clear the per-lifetime state in the inode as we are now
  201. * effectively a new inode and need to return to the initial
  202. * state before reuse occurs.
  203. */
  204. ip->i_flags &= ~XFS_IRECLAIM_RESET_FLAGS;
  205. ip->i_flags |= XFS_INEW;
  206. __xfs_inode_clear_reclaim_tag(mp, pag, ip);
  207. inode->i_state = I_NEW;
  208. ASSERT(!rwsem_is_locked(&ip->i_iolock.mr_lock));
  209. mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
  210. spin_unlock(&ip->i_flags_lock);
  211. spin_unlock(&pag->pag_ici_lock);
  212. } else {
  213. /* If the VFS inode is being torn down, pause and try again. */
  214. if (!igrab(inode)) {
  215. trace_xfs_iget_skip(ip);
  216. error = EAGAIN;
  217. goto out_error;
  218. }
  219. /* We've got a live one. */
  220. spin_unlock(&ip->i_flags_lock);
  221. rcu_read_unlock();
  222. trace_xfs_iget_hit(ip);
  223. }
  224. if (lock_flags != 0)
  225. xfs_ilock(ip, lock_flags);
  226. xfs_iflags_clear(ip, XFS_ISTALE | XFS_IDONTCACHE);
  227. XFS_STATS_INC(xs_ig_found);
  228. return 0;
  229. out_error:
  230. spin_unlock(&ip->i_flags_lock);
  231. rcu_read_unlock();
  232. return error;
  233. }
  234. static int
  235. xfs_iget_cache_miss(
  236. struct xfs_mount *mp,
  237. struct xfs_perag *pag,
  238. xfs_trans_t *tp,
  239. xfs_ino_t ino,
  240. struct xfs_inode **ipp,
  241. int flags,
  242. int lock_flags)
  243. {
  244. struct xfs_inode *ip;
  245. int error;
  246. xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
  247. int iflags;
  248. ip = xfs_inode_alloc(mp, ino);
  249. if (!ip)
  250. return ENOMEM;
  251. error = xfs_iread(mp, tp, ip, flags);
  252. if (error)
  253. goto out_destroy;
  254. trace_xfs_iget_miss(ip);
  255. if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
  256. error = ENOENT;
  257. goto out_destroy;
  258. }
  259. /*
  260. * Preload the radix tree so we can insert safely under the
  261. * write spinlock. Note that we cannot sleep inside the preload
  262. * region. Since we can be called from transaction context, don't
  263. * recurse into the file system.
  264. */
  265. if (radix_tree_preload(GFP_NOFS)) {
  266. error = EAGAIN;
  267. goto out_destroy;
  268. }
  269. /*
  270. * Because the inode hasn't been added to the radix-tree yet it can't
  271. * be found by another thread, so we can do the non-sleeping lock here.
  272. */
  273. if (lock_flags) {
  274. if (!xfs_ilock_nowait(ip, lock_flags))
  275. BUG();
  276. }
  277. /*
  278. * These values must be set before inserting the inode into the radix
  279. * tree as the moment it is inserted a concurrent lookup (allowed by the
  280. * RCU locking mechanism) can find it and that lookup must see that this
  281. * is an inode currently under construction (i.e. that XFS_INEW is set).
  282. * The ip->i_flags_lock that protects the XFS_INEW flag forms the
  283. * memory barrier that ensures this detection works correctly at lookup
  284. * time.
  285. */
  286. iflags = XFS_INEW;
  287. if (flags & XFS_IGET_DONTCACHE)
  288. iflags |= XFS_IDONTCACHE;
  289. ip->i_udquot = NULL;
  290. ip->i_gdquot = NULL;
  291. ip->i_pdquot = NULL;
  292. xfs_iflags_set(ip, iflags);
  293. /* insert the new inode */
  294. spin_lock(&pag->pag_ici_lock);
  295. error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
  296. if (unlikely(error)) {
  297. WARN_ON(error != -EEXIST);
  298. XFS_STATS_INC(xs_ig_dup);
  299. error = EAGAIN;
  300. goto out_preload_end;
  301. }
  302. spin_unlock(&pag->pag_ici_lock);
  303. radix_tree_preload_end();
  304. *ipp = ip;
  305. return 0;
  306. out_preload_end:
  307. spin_unlock(&pag->pag_ici_lock);
  308. radix_tree_preload_end();
  309. if (lock_flags)
  310. xfs_iunlock(ip, lock_flags);
  311. out_destroy:
  312. __destroy_inode(VFS_I(ip));
  313. xfs_inode_free(ip);
  314. return error;
  315. }
  316. /*
  317. * Look up an inode by number in the given file system.
  318. * The inode is looked up in the cache held in each AG.
  319. * If the inode is found in the cache, initialise the vfs inode
  320. * if necessary.
  321. *
  322. * If it is not in core, read it in from the file system's device,
  323. * add it to the cache and initialise the vfs inode.
  324. *
  325. * The inode is locked according to the value of the lock_flags parameter.
  326. * This flag parameter indicates how and if the inode's IO lock and inode lock
  327. * should be taken.
  328. *
  329. * mp -- the mount point structure for the current file system. It points
  330. * to the inode hash table.
  331. * tp -- a pointer to the current transaction if there is one. This is
  332. * simply passed through to the xfs_iread() call.
  333. * ino -- the number of the inode desired. This is the unique identifier
  334. * within the file system for the inode being requested.
  335. * lock_flags -- flags indicating how to lock the inode. See the comment
  336. * for xfs_ilock() for a list of valid values.
  337. */
  338. int
  339. xfs_iget(
  340. xfs_mount_t *mp,
  341. xfs_trans_t *tp,
  342. xfs_ino_t ino,
  343. uint flags,
  344. uint lock_flags,
  345. xfs_inode_t **ipp)
  346. {
  347. xfs_inode_t *ip;
  348. int error;
  349. xfs_perag_t *pag;
  350. xfs_agino_t agino;
  351. /*
  352. * xfs_reclaim_inode() uses the ILOCK to ensure an inode
  353. * doesn't get freed while it's being referenced during a
  354. * radix tree traversal here. It assumes this function
  355. * aqcuires only the ILOCK (and therefore it has no need to
  356. * involve the IOLOCK in this synchronization).
  357. */
  358. ASSERT((lock_flags & (XFS_IOLOCK_EXCL | XFS_IOLOCK_SHARED)) == 0);
  359. /* reject inode numbers outside existing AGs */
  360. if (!ino || XFS_INO_TO_AGNO(mp, ino) >= mp->m_sb.sb_agcount)
  361. return EINVAL;
  362. /* get the perag structure and ensure that it's inode capable */
  363. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ino));
  364. agino = XFS_INO_TO_AGINO(mp, ino);
  365. again:
  366. error = 0;
  367. rcu_read_lock();
  368. ip = radix_tree_lookup(&pag->pag_ici_root, agino);
  369. if (ip) {
  370. error = xfs_iget_cache_hit(pag, ip, ino, flags, lock_flags);
  371. if (error)
  372. goto out_error_or_again;
  373. } else {
  374. rcu_read_unlock();
  375. XFS_STATS_INC(xs_ig_missed);
  376. error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip,
  377. flags, lock_flags);
  378. if (error)
  379. goto out_error_or_again;
  380. }
  381. xfs_perag_put(pag);
  382. *ipp = ip;
  383. /*
  384. * If we have a real type for an on-disk inode, we can set ops(&unlock)
  385. * now. If it's a new inode being created, xfs_ialloc will handle it.
  386. */
  387. if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0)
  388. xfs_setup_inode(ip);
  389. return 0;
  390. out_error_or_again:
  391. if (error == EAGAIN) {
  392. delay(1);
  393. goto again;
  394. }
  395. xfs_perag_put(pag);
  396. return error;
  397. }
  398. /*
  399. * The inode lookup is done in batches to keep the amount of lock traffic and
  400. * radix tree lookups to a minimum. The batch size is a trade off between
  401. * lookup reduction and stack usage. This is in the reclaim path, so we can't
  402. * be too greedy.
  403. */
  404. #define XFS_LOOKUP_BATCH 32
  405. STATIC int
  406. xfs_inode_ag_walk_grab(
  407. struct xfs_inode *ip)
  408. {
  409. struct inode *inode = VFS_I(ip);
  410. ASSERT(rcu_read_lock_held());
  411. /*
  412. * check for stale RCU freed inode
  413. *
  414. * If the inode has been reallocated, it doesn't matter if it's not in
  415. * the AG we are walking - we are walking for writeback, so if it
  416. * passes all the "valid inode" checks and is dirty, then we'll write
  417. * it back anyway. If it has been reallocated and still being
  418. * initialised, the XFS_INEW check below will catch it.
  419. */
  420. spin_lock(&ip->i_flags_lock);
  421. if (!ip->i_ino)
  422. goto out_unlock_noent;
  423. /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
  424. if (__xfs_iflags_test(ip, XFS_INEW | XFS_IRECLAIMABLE | XFS_IRECLAIM))
  425. goto out_unlock_noent;
  426. spin_unlock(&ip->i_flags_lock);
  427. /* nothing to sync during shutdown */
  428. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  429. return EFSCORRUPTED;
  430. /* If we can't grab the inode, it must on it's way to reclaim. */
  431. if (!igrab(inode))
  432. return ENOENT;
  433. /* inode is valid */
  434. return 0;
  435. out_unlock_noent:
  436. spin_unlock(&ip->i_flags_lock);
  437. return ENOENT;
  438. }
  439. STATIC int
  440. xfs_inode_ag_walk(
  441. struct xfs_mount *mp,
  442. struct xfs_perag *pag,
  443. int (*execute)(struct xfs_inode *ip,
  444. struct xfs_perag *pag, int flags,
  445. void *args),
  446. int flags,
  447. void *args,
  448. int tag)
  449. {
  450. uint32_t first_index;
  451. int last_error = 0;
  452. int skipped;
  453. int done;
  454. int nr_found;
  455. restart:
  456. done = 0;
  457. skipped = 0;
  458. first_index = 0;
  459. nr_found = 0;
  460. do {
  461. struct xfs_inode *batch[XFS_LOOKUP_BATCH];
  462. int error = 0;
  463. int i;
  464. rcu_read_lock();
  465. if (tag == -1)
  466. nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
  467. (void **)batch, first_index,
  468. XFS_LOOKUP_BATCH);
  469. else
  470. nr_found = radix_tree_gang_lookup_tag(
  471. &pag->pag_ici_root,
  472. (void **) batch, first_index,
  473. XFS_LOOKUP_BATCH, tag);
  474. if (!nr_found) {
  475. rcu_read_unlock();
  476. break;
  477. }
  478. /*
  479. * Grab the inodes before we drop the lock. if we found
  480. * nothing, nr == 0 and the loop will be skipped.
  481. */
  482. for (i = 0; i < nr_found; i++) {
  483. struct xfs_inode *ip = batch[i];
  484. if (done || xfs_inode_ag_walk_grab(ip))
  485. batch[i] = NULL;
  486. /*
  487. * Update the index for the next lookup. Catch
  488. * overflows into the next AG range which can occur if
  489. * we have inodes in the last block of the AG and we
  490. * are currently pointing to the last inode.
  491. *
  492. * Because we may see inodes that are from the wrong AG
  493. * due to RCU freeing and reallocation, only update the
  494. * index if it lies in this AG. It was a race that lead
  495. * us to see this inode, so another lookup from the
  496. * same index will not find it again.
  497. */
  498. if (XFS_INO_TO_AGNO(mp, ip->i_ino) != pag->pag_agno)
  499. continue;
  500. first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
  501. if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
  502. done = 1;
  503. }
  504. /* unlock now we've grabbed the inodes. */
  505. rcu_read_unlock();
  506. for (i = 0; i < nr_found; i++) {
  507. if (!batch[i])
  508. continue;
  509. error = execute(batch[i], pag, flags, args);
  510. IRELE(batch[i]);
  511. if (error == EAGAIN) {
  512. skipped++;
  513. continue;
  514. }
  515. if (error && last_error != EFSCORRUPTED)
  516. last_error = error;
  517. }
  518. /* bail out if the filesystem is corrupted. */
  519. if (error == EFSCORRUPTED)
  520. break;
  521. cond_resched();
  522. } while (nr_found && !done);
  523. if (skipped) {
  524. delay(1);
  525. goto restart;
  526. }
  527. return last_error;
  528. }
  529. /*
  530. * Background scanning to trim post-EOF preallocated space. This is queued
  531. * based on the 'speculative_prealloc_lifetime' tunable (5m by default).
  532. */
  533. STATIC void
  534. xfs_queue_eofblocks(
  535. struct xfs_mount *mp)
  536. {
  537. rcu_read_lock();
  538. if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_EOFBLOCKS_TAG))
  539. queue_delayed_work(mp->m_eofblocks_workqueue,
  540. &mp->m_eofblocks_work,
  541. msecs_to_jiffies(xfs_eofb_secs * 1000));
  542. rcu_read_unlock();
  543. }
  544. void
  545. xfs_eofblocks_worker(
  546. struct work_struct *work)
  547. {
  548. struct xfs_mount *mp = container_of(to_delayed_work(work),
  549. struct xfs_mount, m_eofblocks_work);
  550. xfs_icache_free_eofblocks(mp, NULL);
  551. xfs_queue_eofblocks(mp);
  552. }
  553. int
  554. xfs_inode_ag_iterator(
  555. struct xfs_mount *mp,
  556. int (*execute)(struct xfs_inode *ip,
  557. struct xfs_perag *pag, int flags,
  558. void *args),
  559. int flags,
  560. void *args)
  561. {
  562. struct xfs_perag *pag;
  563. int error = 0;
  564. int last_error = 0;
  565. xfs_agnumber_t ag;
  566. ag = 0;
  567. while ((pag = xfs_perag_get(mp, ag))) {
  568. ag = pag->pag_agno + 1;
  569. error = xfs_inode_ag_walk(mp, pag, execute, flags, args, -1);
  570. xfs_perag_put(pag);
  571. if (error) {
  572. last_error = error;
  573. if (error == EFSCORRUPTED)
  574. break;
  575. }
  576. }
  577. return XFS_ERROR(last_error);
  578. }
  579. int
  580. xfs_inode_ag_iterator_tag(
  581. struct xfs_mount *mp,
  582. int (*execute)(struct xfs_inode *ip,
  583. struct xfs_perag *pag, 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 XFS_ERROR(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 XFS_ERROR(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. STATIC int
  1070. xfs_inode_free_eofblocks(
  1071. struct xfs_inode *ip,
  1072. struct xfs_perag *pag,
  1073. int flags,
  1074. void *args)
  1075. {
  1076. int ret;
  1077. struct xfs_eofblocks *eofb = args;
  1078. if (!xfs_can_free_eofblocks(ip, false)) {
  1079. /* inode could be preallocated or append-only */
  1080. trace_xfs_inode_free_eofblocks_invalid(ip);
  1081. xfs_inode_clear_eofblocks_tag(ip);
  1082. return 0;
  1083. }
  1084. /*
  1085. * If the mapping is dirty the operation can block and wait for some
  1086. * time. Unless we are waiting, skip it.
  1087. */
  1088. if (!(flags & SYNC_WAIT) &&
  1089. mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY))
  1090. return 0;
  1091. if (eofb) {
  1092. if (!xfs_inode_match_id(ip, eofb))
  1093. return 0;
  1094. /* skip the inode if the file size is too small */
  1095. if (eofb->eof_flags & XFS_EOF_FLAGS_MINFILESIZE &&
  1096. XFS_ISIZE(ip) < eofb->eof_min_file_size)
  1097. return 0;
  1098. }
  1099. ret = xfs_free_eofblocks(ip->i_mount, ip, true);
  1100. /* don't revisit the inode if we're not waiting */
  1101. if (ret == EAGAIN && !(flags & SYNC_WAIT))
  1102. ret = 0;
  1103. return ret;
  1104. }
  1105. int
  1106. xfs_icache_free_eofblocks(
  1107. struct xfs_mount *mp,
  1108. struct xfs_eofblocks *eofb)
  1109. {
  1110. int flags = SYNC_TRYLOCK;
  1111. if (eofb && (eofb->eof_flags & XFS_EOF_FLAGS_SYNC))
  1112. flags = SYNC_WAIT;
  1113. return xfs_inode_ag_iterator_tag(mp, xfs_inode_free_eofblocks, flags,
  1114. eofb, XFS_ICI_EOFBLOCKS_TAG);
  1115. }
  1116. void
  1117. xfs_inode_set_eofblocks_tag(
  1118. xfs_inode_t *ip)
  1119. {
  1120. struct xfs_mount *mp = ip->i_mount;
  1121. struct xfs_perag *pag;
  1122. int tagged;
  1123. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
  1124. spin_lock(&pag->pag_ici_lock);
  1125. trace_xfs_inode_set_eofblocks_tag(ip);
  1126. tagged = radix_tree_tagged(&pag->pag_ici_root,
  1127. XFS_ICI_EOFBLOCKS_TAG);
  1128. radix_tree_tag_set(&pag->pag_ici_root,
  1129. XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
  1130. XFS_ICI_EOFBLOCKS_TAG);
  1131. if (!tagged) {
  1132. /* propagate the eofblocks tag up into the perag radix tree */
  1133. spin_lock(&ip->i_mount->m_perag_lock);
  1134. radix_tree_tag_set(&ip->i_mount->m_perag_tree,
  1135. XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
  1136. XFS_ICI_EOFBLOCKS_TAG);
  1137. spin_unlock(&ip->i_mount->m_perag_lock);
  1138. /* kick off background trimming */
  1139. xfs_queue_eofblocks(ip->i_mount);
  1140. trace_xfs_perag_set_eofblocks(ip->i_mount, pag->pag_agno,
  1141. -1, _RET_IP_);
  1142. }
  1143. spin_unlock(&pag->pag_ici_lock);
  1144. xfs_perag_put(pag);
  1145. }
  1146. void
  1147. xfs_inode_clear_eofblocks_tag(
  1148. xfs_inode_t *ip)
  1149. {
  1150. struct xfs_mount *mp = ip->i_mount;
  1151. struct xfs_perag *pag;
  1152. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
  1153. spin_lock(&pag->pag_ici_lock);
  1154. trace_xfs_inode_clear_eofblocks_tag(ip);
  1155. radix_tree_tag_clear(&pag->pag_ici_root,
  1156. XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
  1157. XFS_ICI_EOFBLOCKS_TAG);
  1158. if (!radix_tree_tagged(&pag->pag_ici_root, XFS_ICI_EOFBLOCKS_TAG)) {
  1159. /* clear the eofblocks tag from the perag radix tree */
  1160. spin_lock(&ip->i_mount->m_perag_lock);
  1161. radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
  1162. XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
  1163. XFS_ICI_EOFBLOCKS_TAG);
  1164. spin_unlock(&ip->i_mount->m_perag_lock);
  1165. trace_xfs_perag_clear_eofblocks(ip->i_mount, pag->pag_agno,
  1166. -1, _RET_IP_);
  1167. }
  1168. spin_unlock(&pag->pag_ici_lock);
  1169. xfs_perag_put(pag);
  1170. }