xfs_icache.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418
  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. /* get the perag structure and ensure that it's inode capable */
  365. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ino));
  366. agino = XFS_INO_TO_AGINO(mp, ino);
  367. again:
  368. error = 0;
  369. rcu_read_lock();
  370. ip = radix_tree_lookup(&pag->pag_ici_root, agino);
  371. if (ip) {
  372. error = xfs_iget_cache_hit(pag, ip, ino, flags, lock_flags);
  373. if (error)
  374. goto out_error_or_again;
  375. } else {
  376. rcu_read_unlock();
  377. XFS_STATS_INC(xs_ig_missed);
  378. error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip,
  379. flags, lock_flags);
  380. if (error)
  381. goto out_error_or_again;
  382. }
  383. xfs_perag_put(pag);
  384. *ipp = ip;
  385. /*
  386. * If we have a real type for an on-disk inode, we can set ops(&unlock)
  387. * now. If it's a new inode being created, xfs_ialloc will handle it.
  388. */
  389. if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0)
  390. xfs_setup_inode(ip);
  391. return 0;
  392. out_error_or_again:
  393. if (error == -EAGAIN) {
  394. delay(1);
  395. goto again;
  396. }
  397. xfs_perag_put(pag);
  398. return error;
  399. }
  400. /*
  401. * The inode lookup is done in batches to keep the amount of lock traffic and
  402. * radix tree lookups to a minimum. The batch size is a trade off between
  403. * lookup reduction and stack usage. This is in the reclaim path, so we can't
  404. * be too greedy.
  405. */
  406. #define XFS_LOOKUP_BATCH 32
  407. STATIC int
  408. xfs_inode_ag_walk_grab(
  409. struct xfs_inode *ip)
  410. {
  411. struct inode *inode = VFS_I(ip);
  412. ASSERT(rcu_read_lock_held());
  413. /*
  414. * check for stale RCU freed inode
  415. *
  416. * If the inode has been reallocated, it doesn't matter if it's not in
  417. * the AG we are walking - we are walking for writeback, so if it
  418. * passes all the "valid inode" checks and is dirty, then we'll write
  419. * it back anyway. If it has been reallocated and still being
  420. * initialised, the XFS_INEW check below will catch it.
  421. */
  422. spin_lock(&ip->i_flags_lock);
  423. if (!ip->i_ino)
  424. goto out_unlock_noent;
  425. /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
  426. if (__xfs_iflags_test(ip, XFS_INEW | XFS_IRECLAIMABLE | XFS_IRECLAIM))
  427. goto out_unlock_noent;
  428. spin_unlock(&ip->i_flags_lock);
  429. /* nothing to sync during shutdown */
  430. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  431. return -EFSCORRUPTED;
  432. /* If we can't grab the inode, it must on it's way to reclaim. */
  433. if (!igrab(inode))
  434. return -ENOENT;
  435. /* inode is valid */
  436. return 0;
  437. out_unlock_noent:
  438. spin_unlock(&ip->i_flags_lock);
  439. return -ENOENT;
  440. }
  441. STATIC int
  442. xfs_inode_ag_walk(
  443. struct xfs_mount *mp,
  444. struct xfs_perag *pag,
  445. int (*execute)(struct xfs_inode *ip, int flags,
  446. void *args),
  447. int flags,
  448. void *args,
  449. int tag)
  450. {
  451. uint32_t first_index;
  452. int last_error = 0;
  453. int skipped;
  454. int done;
  455. int nr_found;
  456. restart:
  457. done = 0;
  458. skipped = 0;
  459. first_index = 0;
  460. nr_found = 0;
  461. do {
  462. struct xfs_inode *batch[XFS_LOOKUP_BATCH];
  463. int error = 0;
  464. int i;
  465. rcu_read_lock();
  466. if (tag == -1)
  467. nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
  468. (void **)batch, first_index,
  469. XFS_LOOKUP_BATCH);
  470. else
  471. nr_found = radix_tree_gang_lookup_tag(
  472. &pag->pag_ici_root,
  473. (void **) batch, first_index,
  474. XFS_LOOKUP_BATCH, tag);
  475. if (!nr_found) {
  476. rcu_read_unlock();
  477. break;
  478. }
  479. /*
  480. * Grab the inodes before we drop the lock. if we found
  481. * nothing, nr == 0 and the loop will be skipped.
  482. */
  483. for (i = 0; i < nr_found; i++) {
  484. struct xfs_inode *ip = batch[i];
  485. if (done || xfs_inode_ag_walk_grab(ip))
  486. batch[i] = NULL;
  487. /*
  488. * Update the index for the next lookup. Catch
  489. * overflows into the next AG range which can occur if
  490. * we have inodes in the last block of the AG and we
  491. * are currently pointing to the last inode.
  492. *
  493. * Because we may see inodes that are from the wrong AG
  494. * due to RCU freeing and reallocation, only update the
  495. * index if it lies in this AG. It was a race that lead
  496. * us to see this inode, so another lookup from the
  497. * same index will not find it again.
  498. */
  499. if (XFS_INO_TO_AGNO(mp, ip->i_ino) != pag->pag_agno)
  500. continue;
  501. first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
  502. if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
  503. done = 1;
  504. }
  505. /* unlock now we've grabbed the inodes. */
  506. rcu_read_unlock();
  507. for (i = 0; i < nr_found; i++) {
  508. if (!batch[i])
  509. continue;
  510. error = execute(batch[i], flags, args);
  511. IRELE(batch[i]);
  512. if (error == -EAGAIN) {
  513. skipped++;
  514. continue;
  515. }
  516. if (error && last_error != -EFSCORRUPTED)
  517. last_error = error;
  518. }
  519. /* bail out if the filesystem is corrupted. */
  520. if (error == -EFSCORRUPTED)
  521. break;
  522. cond_resched();
  523. } while (nr_found && !done);
  524. if (skipped) {
  525. delay(1);
  526. goto restart;
  527. }
  528. return last_error;
  529. }
  530. /*
  531. * Background scanning to trim post-EOF preallocated space. This is queued
  532. * based on the 'speculative_prealloc_lifetime' tunable (5m by default).
  533. */
  534. STATIC void
  535. xfs_queue_eofblocks(
  536. struct xfs_mount *mp)
  537. {
  538. rcu_read_lock();
  539. if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_EOFBLOCKS_TAG))
  540. queue_delayed_work(mp->m_eofblocks_workqueue,
  541. &mp->m_eofblocks_work,
  542. msecs_to_jiffies(xfs_eofb_secs * 1000));
  543. rcu_read_unlock();
  544. }
  545. void
  546. xfs_eofblocks_worker(
  547. struct work_struct *work)
  548. {
  549. struct xfs_mount *mp = container_of(to_delayed_work(work),
  550. struct xfs_mount, m_eofblocks_work);
  551. xfs_icache_free_eofblocks(mp, NULL);
  552. xfs_queue_eofblocks(mp);
  553. }
  554. int
  555. xfs_inode_ag_iterator(
  556. struct xfs_mount *mp,
  557. int (*execute)(struct xfs_inode *ip, 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 last_error;
  578. }
  579. int
  580. xfs_inode_ag_iterator_tag(
  581. struct xfs_mount *mp,
  582. int (*execute)(struct xfs_inode *ip, int flags,
  583. void *args),
  584. int flags,
  585. void *args,
  586. int tag)
  587. {
  588. struct xfs_perag *pag;
  589. int error = 0;
  590. int last_error = 0;
  591. xfs_agnumber_t ag;
  592. ag = 0;
  593. while ((pag = xfs_perag_get_tag(mp, ag, tag))) {
  594. ag = pag->pag_agno + 1;
  595. error = xfs_inode_ag_walk(mp, pag, execute, flags, args, tag);
  596. xfs_perag_put(pag);
  597. if (error) {
  598. last_error = error;
  599. if (error == -EFSCORRUPTED)
  600. break;
  601. }
  602. }
  603. return last_error;
  604. }
  605. /*
  606. * Queue a new inode reclaim pass if there are reclaimable inodes and there
  607. * isn't a reclaim pass already in progress. By default it runs every 5s based
  608. * on the xfs periodic sync default of 30s. Perhaps this should have it's own
  609. * tunable, but that can be done if this method proves to be ineffective or too
  610. * aggressive.
  611. */
  612. static void
  613. xfs_reclaim_work_queue(
  614. struct xfs_mount *mp)
  615. {
  616. rcu_read_lock();
  617. if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_RECLAIM_TAG)) {
  618. queue_delayed_work(mp->m_reclaim_workqueue, &mp->m_reclaim_work,
  619. msecs_to_jiffies(xfs_syncd_centisecs / 6 * 10));
  620. }
  621. rcu_read_unlock();
  622. }
  623. /*
  624. * This is a fast pass over the inode cache to try to get reclaim moving on as
  625. * many inodes as possible in a short period of time. It kicks itself every few
  626. * seconds, as well as being kicked by the inode cache shrinker when memory
  627. * goes low. It scans as quickly as possible avoiding locked inodes or those
  628. * already being flushed, and once done schedules a future pass.
  629. */
  630. void
  631. xfs_reclaim_worker(
  632. struct work_struct *work)
  633. {
  634. struct xfs_mount *mp = container_of(to_delayed_work(work),
  635. struct xfs_mount, m_reclaim_work);
  636. xfs_reclaim_inodes(mp, SYNC_TRYLOCK);
  637. xfs_reclaim_work_queue(mp);
  638. }
  639. static void
  640. __xfs_inode_set_reclaim_tag(
  641. struct xfs_perag *pag,
  642. struct xfs_inode *ip)
  643. {
  644. radix_tree_tag_set(&pag->pag_ici_root,
  645. XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
  646. XFS_ICI_RECLAIM_TAG);
  647. if (!pag->pag_ici_reclaimable) {
  648. /* propagate the reclaim tag up into the perag radix tree */
  649. spin_lock(&ip->i_mount->m_perag_lock);
  650. radix_tree_tag_set(&ip->i_mount->m_perag_tree,
  651. XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
  652. XFS_ICI_RECLAIM_TAG);
  653. spin_unlock(&ip->i_mount->m_perag_lock);
  654. /* schedule periodic background inode reclaim */
  655. xfs_reclaim_work_queue(ip->i_mount);
  656. trace_xfs_perag_set_reclaim(ip->i_mount, pag->pag_agno,
  657. -1, _RET_IP_);
  658. }
  659. pag->pag_ici_reclaimable++;
  660. }
  661. /*
  662. * We set the inode flag atomically with the radix tree tag.
  663. * Once we get tag lookups on the radix tree, this inode flag
  664. * can go away.
  665. */
  666. void
  667. xfs_inode_set_reclaim_tag(
  668. xfs_inode_t *ip)
  669. {
  670. struct xfs_mount *mp = ip->i_mount;
  671. struct xfs_perag *pag;
  672. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
  673. spin_lock(&pag->pag_ici_lock);
  674. spin_lock(&ip->i_flags_lock);
  675. __xfs_inode_set_reclaim_tag(pag, ip);
  676. __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
  677. spin_unlock(&ip->i_flags_lock);
  678. spin_unlock(&pag->pag_ici_lock);
  679. xfs_perag_put(pag);
  680. }
  681. STATIC void
  682. __xfs_inode_clear_reclaim(
  683. xfs_perag_t *pag,
  684. xfs_inode_t *ip)
  685. {
  686. pag->pag_ici_reclaimable--;
  687. if (!pag->pag_ici_reclaimable) {
  688. /* clear the reclaim tag from the perag radix tree */
  689. spin_lock(&ip->i_mount->m_perag_lock);
  690. radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
  691. XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
  692. XFS_ICI_RECLAIM_TAG);
  693. spin_unlock(&ip->i_mount->m_perag_lock);
  694. trace_xfs_perag_clear_reclaim(ip->i_mount, pag->pag_agno,
  695. -1, _RET_IP_);
  696. }
  697. }
  698. STATIC void
  699. __xfs_inode_clear_reclaim_tag(
  700. xfs_mount_t *mp,
  701. xfs_perag_t *pag,
  702. xfs_inode_t *ip)
  703. {
  704. radix_tree_tag_clear(&pag->pag_ici_root,
  705. XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
  706. __xfs_inode_clear_reclaim(pag, ip);
  707. }
  708. /*
  709. * Grab the inode for reclaim exclusively.
  710. * Return 0 if we grabbed it, non-zero otherwise.
  711. */
  712. STATIC int
  713. xfs_reclaim_inode_grab(
  714. struct xfs_inode *ip,
  715. int flags)
  716. {
  717. ASSERT(rcu_read_lock_held());
  718. /* quick check for stale RCU freed inode */
  719. if (!ip->i_ino)
  720. return 1;
  721. /*
  722. * If we are asked for non-blocking operation, do unlocked checks to
  723. * see if the inode already is being flushed or in reclaim to avoid
  724. * lock traffic.
  725. */
  726. if ((flags & SYNC_TRYLOCK) &&
  727. __xfs_iflags_test(ip, XFS_IFLOCK | XFS_IRECLAIM))
  728. return 1;
  729. /*
  730. * The radix tree lock here protects a thread in xfs_iget from racing
  731. * with us starting reclaim on the inode. Once we have the
  732. * XFS_IRECLAIM flag set it will not touch us.
  733. *
  734. * Due to RCU lookup, we may find inodes that have been freed and only
  735. * have XFS_IRECLAIM set. Indeed, we may see reallocated inodes that
  736. * aren't candidates for reclaim at all, so we must check the
  737. * XFS_IRECLAIMABLE is set first before proceeding to reclaim.
  738. */
  739. spin_lock(&ip->i_flags_lock);
  740. if (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) ||
  741. __xfs_iflags_test(ip, XFS_IRECLAIM)) {
  742. /* not a reclaim candidate. */
  743. spin_unlock(&ip->i_flags_lock);
  744. return 1;
  745. }
  746. __xfs_iflags_set(ip, XFS_IRECLAIM);
  747. spin_unlock(&ip->i_flags_lock);
  748. return 0;
  749. }
  750. /*
  751. * Inodes in different states need to be treated differently. The following
  752. * table lists the inode states and the reclaim actions necessary:
  753. *
  754. * inode state iflush ret required action
  755. * --------------- ---------- ---------------
  756. * bad - reclaim
  757. * shutdown EIO unpin and reclaim
  758. * clean, unpinned 0 reclaim
  759. * stale, unpinned 0 reclaim
  760. * clean, pinned(*) 0 requeue
  761. * stale, pinned EAGAIN requeue
  762. * dirty, async - requeue
  763. * dirty, sync 0 reclaim
  764. *
  765. * (*) dgc: I don't think the clean, pinned state is possible but it gets
  766. * handled anyway given the order of checks implemented.
  767. *
  768. * Also, because we get the flush lock first, we know that any inode that has
  769. * been flushed delwri has had the flush completed by the time we check that
  770. * the inode is clean.
  771. *
  772. * Note that because the inode is flushed delayed write by AIL pushing, the
  773. * flush lock may already be held here and waiting on it can result in very
  774. * long latencies. Hence for sync reclaims, where we wait on the flush lock,
  775. * the caller should push the AIL first before trying to reclaim inodes to
  776. * minimise the amount of time spent waiting. For background relaim, we only
  777. * bother to reclaim clean inodes anyway.
  778. *
  779. * Hence the order of actions after gaining the locks should be:
  780. * bad => reclaim
  781. * shutdown => unpin and reclaim
  782. * pinned, async => requeue
  783. * pinned, sync => unpin
  784. * stale => reclaim
  785. * clean => reclaim
  786. * dirty, async => requeue
  787. * dirty, sync => flush, wait and reclaim
  788. */
  789. STATIC int
  790. xfs_reclaim_inode(
  791. struct xfs_inode *ip,
  792. struct xfs_perag *pag,
  793. int sync_mode)
  794. {
  795. struct xfs_buf *bp = NULL;
  796. int error;
  797. restart:
  798. error = 0;
  799. xfs_ilock(ip, XFS_ILOCK_EXCL);
  800. if (!xfs_iflock_nowait(ip)) {
  801. if (!(sync_mode & SYNC_WAIT))
  802. goto out;
  803. xfs_iflock(ip);
  804. }
  805. if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  806. xfs_iunpin_wait(ip);
  807. xfs_iflush_abort(ip, false);
  808. goto reclaim;
  809. }
  810. if (xfs_ipincount(ip)) {
  811. if (!(sync_mode & SYNC_WAIT))
  812. goto out_ifunlock;
  813. xfs_iunpin_wait(ip);
  814. }
  815. if (xfs_iflags_test(ip, XFS_ISTALE))
  816. goto reclaim;
  817. if (xfs_inode_clean(ip))
  818. goto reclaim;
  819. /*
  820. * Never flush out dirty data during non-blocking reclaim, as it would
  821. * just contend with AIL pushing trying to do the same job.
  822. */
  823. if (!(sync_mode & SYNC_WAIT))
  824. goto out_ifunlock;
  825. /*
  826. * Now we have an inode that needs flushing.
  827. *
  828. * Note that xfs_iflush will never block on the inode buffer lock, as
  829. * xfs_ifree_cluster() can lock the inode buffer before it locks the
  830. * ip->i_lock, and we are doing the exact opposite here. As a result,
  831. * doing a blocking xfs_imap_to_bp() to get the cluster buffer would
  832. * result in an ABBA deadlock with xfs_ifree_cluster().
  833. *
  834. * As xfs_ifree_cluser() must gather all inodes that are active in the
  835. * cache to mark them stale, if we hit this case we don't actually want
  836. * to do IO here - we want the inode marked stale so we can simply
  837. * reclaim it. Hence if we get an EAGAIN error here, just unlock the
  838. * inode, back off and try again. Hopefully the next pass through will
  839. * see the stale flag set on the inode.
  840. */
  841. error = xfs_iflush(ip, &bp);
  842. if (error == -EAGAIN) {
  843. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  844. /* backoff longer than in xfs_ifree_cluster */
  845. delay(2);
  846. goto restart;
  847. }
  848. if (!error) {
  849. error = xfs_bwrite(bp);
  850. xfs_buf_relse(bp);
  851. }
  852. xfs_iflock(ip);
  853. reclaim:
  854. xfs_ifunlock(ip);
  855. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  856. XFS_STATS_INC(xs_ig_reclaims);
  857. /*
  858. * Remove the inode from the per-AG radix tree.
  859. *
  860. * Because radix_tree_delete won't complain even if the item was never
  861. * added to the tree assert that it's been there before to catch
  862. * problems with the inode life time early on.
  863. */
  864. spin_lock(&pag->pag_ici_lock);
  865. if (!radix_tree_delete(&pag->pag_ici_root,
  866. XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino)))
  867. ASSERT(0);
  868. __xfs_inode_clear_reclaim(pag, ip);
  869. spin_unlock(&pag->pag_ici_lock);
  870. /*
  871. * Here we do an (almost) spurious inode lock in order to coordinate
  872. * with inode cache radix tree lookups. This is because the lookup
  873. * can reference the inodes in the cache without taking references.
  874. *
  875. * We make that OK here by ensuring that we wait until the inode is
  876. * unlocked after the lookup before we go ahead and free it.
  877. */
  878. xfs_ilock(ip, XFS_ILOCK_EXCL);
  879. xfs_qm_dqdetach(ip);
  880. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  881. xfs_inode_free(ip);
  882. return error;
  883. out_ifunlock:
  884. xfs_ifunlock(ip);
  885. out:
  886. xfs_iflags_clear(ip, XFS_IRECLAIM);
  887. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  888. /*
  889. * We could return -EAGAIN here to make reclaim rescan the inode tree in
  890. * a short while. However, this just burns CPU time scanning the tree
  891. * waiting for IO to complete and the reclaim work never goes back to
  892. * the idle state. Instead, return 0 to let the next scheduled
  893. * background reclaim attempt to reclaim the inode again.
  894. */
  895. return 0;
  896. }
  897. /*
  898. * Walk the AGs and reclaim the inodes in them. Even if the filesystem is
  899. * corrupted, we still want to try to reclaim all the inodes. If we don't,
  900. * then a shut down during filesystem unmount reclaim walk leak all the
  901. * unreclaimed inodes.
  902. */
  903. STATIC int
  904. xfs_reclaim_inodes_ag(
  905. struct xfs_mount *mp,
  906. int flags,
  907. int *nr_to_scan)
  908. {
  909. struct xfs_perag *pag;
  910. int error = 0;
  911. int last_error = 0;
  912. xfs_agnumber_t ag;
  913. int trylock = flags & SYNC_TRYLOCK;
  914. int skipped;
  915. restart:
  916. ag = 0;
  917. skipped = 0;
  918. while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
  919. unsigned long first_index = 0;
  920. int done = 0;
  921. int nr_found = 0;
  922. ag = pag->pag_agno + 1;
  923. if (trylock) {
  924. if (!mutex_trylock(&pag->pag_ici_reclaim_lock)) {
  925. skipped++;
  926. xfs_perag_put(pag);
  927. continue;
  928. }
  929. first_index = pag->pag_ici_reclaim_cursor;
  930. } else
  931. mutex_lock(&pag->pag_ici_reclaim_lock);
  932. do {
  933. struct xfs_inode *batch[XFS_LOOKUP_BATCH];
  934. int i;
  935. rcu_read_lock();
  936. nr_found = radix_tree_gang_lookup_tag(
  937. &pag->pag_ici_root,
  938. (void **)batch, first_index,
  939. XFS_LOOKUP_BATCH,
  940. XFS_ICI_RECLAIM_TAG);
  941. if (!nr_found) {
  942. done = 1;
  943. rcu_read_unlock();
  944. break;
  945. }
  946. /*
  947. * Grab the inodes before we drop the lock. if we found
  948. * nothing, nr == 0 and the loop will be skipped.
  949. */
  950. for (i = 0; i < nr_found; i++) {
  951. struct xfs_inode *ip = batch[i];
  952. if (done || xfs_reclaim_inode_grab(ip, flags))
  953. batch[i] = NULL;
  954. /*
  955. * Update the index for the next lookup. Catch
  956. * overflows into the next AG range which can
  957. * occur if we have inodes in the last block of
  958. * the AG and we are currently pointing to the
  959. * last inode.
  960. *
  961. * Because we may see inodes that are from the
  962. * wrong AG due to RCU freeing and
  963. * reallocation, only update the index if it
  964. * lies in this AG. It was a race that lead us
  965. * to see this inode, so another lookup from
  966. * the same index will not find it again.
  967. */
  968. if (XFS_INO_TO_AGNO(mp, ip->i_ino) !=
  969. pag->pag_agno)
  970. continue;
  971. first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
  972. if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
  973. done = 1;
  974. }
  975. /* unlock now we've grabbed the inodes. */
  976. rcu_read_unlock();
  977. for (i = 0; i < nr_found; i++) {
  978. if (!batch[i])
  979. continue;
  980. error = xfs_reclaim_inode(batch[i], pag, flags);
  981. if (error && last_error != -EFSCORRUPTED)
  982. last_error = error;
  983. }
  984. *nr_to_scan -= XFS_LOOKUP_BATCH;
  985. cond_resched();
  986. } while (nr_found && !done && *nr_to_scan > 0);
  987. if (trylock && !done)
  988. pag->pag_ici_reclaim_cursor = first_index;
  989. else
  990. pag->pag_ici_reclaim_cursor = 0;
  991. mutex_unlock(&pag->pag_ici_reclaim_lock);
  992. xfs_perag_put(pag);
  993. }
  994. /*
  995. * if we skipped any AG, and we still have scan count remaining, do
  996. * another pass this time using blocking reclaim semantics (i.e
  997. * waiting on the reclaim locks and ignoring the reclaim cursors). This
  998. * ensure that when we get more reclaimers than AGs we block rather
  999. * than spin trying to execute reclaim.
  1000. */
  1001. if (skipped && (flags & SYNC_WAIT) && *nr_to_scan > 0) {
  1002. trylock = 0;
  1003. goto restart;
  1004. }
  1005. return last_error;
  1006. }
  1007. int
  1008. xfs_reclaim_inodes(
  1009. xfs_mount_t *mp,
  1010. int mode)
  1011. {
  1012. int nr_to_scan = INT_MAX;
  1013. return xfs_reclaim_inodes_ag(mp, mode, &nr_to_scan);
  1014. }
  1015. /*
  1016. * Scan a certain number of inodes for reclaim.
  1017. *
  1018. * When called we make sure that there is a background (fast) inode reclaim in
  1019. * progress, while we will throttle the speed of reclaim via doing synchronous
  1020. * reclaim of inodes. That means if we come across dirty inodes, we wait for
  1021. * them to be cleaned, which we hope will not be very long due to the
  1022. * background walker having already kicked the IO off on those dirty inodes.
  1023. */
  1024. long
  1025. xfs_reclaim_inodes_nr(
  1026. struct xfs_mount *mp,
  1027. int nr_to_scan)
  1028. {
  1029. /* kick background reclaimer and push the AIL */
  1030. xfs_reclaim_work_queue(mp);
  1031. xfs_ail_push_all(mp->m_ail);
  1032. return xfs_reclaim_inodes_ag(mp, SYNC_TRYLOCK | SYNC_WAIT, &nr_to_scan);
  1033. }
  1034. /*
  1035. * Return the number of reclaimable inodes in the filesystem for
  1036. * the shrinker to determine how much to reclaim.
  1037. */
  1038. int
  1039. xfs_reclaim_inodes_count(
  1040. struct xfs_mount *mp)
  1041. {
  1042. struct xfs_perag *pag;
  1043. xfs_agnumber_t ag = 0;
  1044. int reclaimable = 0;
  1045. while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
  1046. ag = pag->pag_agno + 1;
  1047. reclaimable += pag->pag_ici_reclaimable;
  1048. xfs_perag_put(pag);
  1049. }
  1050. return reclaimable;
  1051. }
  1052. STATIC int
  1053. xfs_inode_match_id(
  1054. struct xfs_inode *ip,
  1055. struct xfs_eofblocks *eofb)
  1056. {
  1057. if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
  1058. !uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
  1059. return 0;
  1060. if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
  1061. !gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
  1062. return 0;
  1063. if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
  1064. xfs_get_projid(ip) != eofb->eof_prid)
  1065. return 0;
  1066. return 1;
  1067. }
  1068. /*
  1069. * A union-based inode filtering algorithm. Process the inode if any of the
  1070. * criteria match. This is for global/internal scans only.
  1071. */
  1072. STATIC int
  1073. xfs_inode_match_id_union(
  1074. struct xfs_inode *ip,
  1075. struct xfs_eofblocks *eofb)
  1076. {
  1077. if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
  1078. uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
  1079. return 1;
  1080. if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
  1081. gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
  1082. return 1;
  1083. if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
  1084. xfs_get_projid(ip) == eofb->eof_prid)
  1085. return 1;
  1086. return 0;
  1087. }
  1088. STATIC int
  1089. xfs_inode_free_eofblocks(
  1090. struct xfs_inode *ip,
  1091. int flags,
  1092. void *args)
  1093. {
  1094. int ret;
  1095. struct xfs_eofblocks *eofb = args;
  1096. bool need_iolock = true;
  1097. int match;
  1098. ASSERT(!eofb || (eofb && eofb->eof_scan_owner != 0));
  1099. if (!xfs_can_free_eofblocks(ip, false)) {
  1100. /* inode could be preallocated or append-only */
  1101. trace_xfs_inode_free_eofblocks_invalid(ip);
  1102. xfs_inode_clear_eofblocks_tag(ip);
  1103. return 0;
  1104. }
  1105. /*
  1106. * If the mapping is dirty the operation can block and wait for some
  1107. * time. Unless we are waiting, skip it.
  1108. */
  1109. if (!(flags & SYNC_WAIT) &&
  1110. mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY))
  1111. return 0;
  1112. if (eofb) {
  1113. if (eofb->eof_flags & XFS_EOF_FLAGS_UNION)
  1114. match = xfs_inode_match_id_union(ip, eofb);
  1115. else
  1116. match = xfs_inode_match_id(ip, eofb);
  1117. if (!match)
  1118. return 0;
  1119. /* skip the inode if the file size is too small */
  1120. if (eofb->eof_flags & XFS_EOF_FLAGS_MINFILESIZE &&
  1121. XFS_ISIZE(ip) < eofb->eof_min_file_size)
  1122. return 0;
  1123. /*
  1124. * A scan owner implies we already hold the iolock. Skip it in
  1125. * xfs_free_eofblocks() to avoid deadlock. This also eliminates
  1126. * the possibility of EAGAIN being returned.
  1127. */
  1128. if (eofb->eof_scan_owner == ip->i_ino)
  1129. need_iolock = false;
  1130. }
  1131. ret = xfs_free_eofblocks(ip->i_mount, ip, need_iolock);
  1132. /* don't revisit the inode if we're not waiting */
  1133. if (ret == -EAGAIN && !(flags & SYNC_WAIT))
  1134. ret = 0;
  1135. return ret;
  1136. }
  1137. int
  1138. xfs_icache_free_eofblocks(
  1139. struct xfs_mount *mp,
  1140. struct xfs_eofblocks *eofb)
  1141. {
  1142. int flags = SYNC_TRYLOCK;
  1143. if (eofb && (eofb->eof_flags & XFS_EOF_FLAGS_SYNC))
  1144. flags = SYNC_WAIT;
  1145. return xfs_inode_ag_iterator_tag(mp, xfs_inode_free_eofblocks, flags,
  1146. eofb, XFS_ICI_EOFBLOCKS_TAG);
  1147. }
  1148. /*
  1149. * Run eofblocks scans on the quotas applicable to the inode. For inodes with
  1150. * multiple quotas, we don't know exactly which quota caused an allocation
  1151. * failure. We make a best effort by including each quota under low free space
  1152. * conditions (less than 1% free space) in the scan.
  1153. */
  1154. int
  1155. xfs_inode_free_quota_eofblocks(
  1156. struct xfs_inode *ip)
  1157. {
  1158. int scan = 0;
  1159. struct xfs_eofblocks eofb = {0};
  1160. struct xfs_dquot *dq;
  1161. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  1162. /*
  1163. * Set the scan owner to avoid a potential livelock. Otherwise, the scan
  1164. * can repeatedly trylock on the inode we're currently processing. We
  1165. * run a sync scan to increase effectiveness and use the union filter to
  1166. * cover all applicable quotas in a single scan.
  1167. */
  1168. eofb.eof_scan_owner = ip->i_ino;
  1169. eofb.eof_flags = XFS_EOF_FLAGS_UNION|XFS_EOF_FLAGS_SYNC;
  1170. if (XFS_IS_UQUOTA_ENFORCED(ip->i_mount)) {
  1171. dq = xfs_inode_dquot(ip, XFS_DQ_USER);
  1172. if (dq && xfs_dquot_lowsp(dq)) {
  1173. eofb.eof_uid = VFS_I(ip)->i_uid;
  1174. eofb.eof_flags |= XFS_EOF_FLAGS_UID;
  1175. scan = 1;
  1176. }
  1177. }
  1178. if (XFS_IS_GQUOTA_ENFORCED(ip->i_mount)) {
  1179. dq = xfs_inode_dquot(ip, XFS_DQ_GROUP);
  1180. if (dq && xfs_dquot_lowsp(dq)) {
  1181. eofb.eof_gid = VFS_I(ip)->i_gid;
  1182. eofb.eof_flags |= XFS_EOF_FLAGS_GID;
  1183. scan = 1;
  1184. }
  1185. }
  1186. if (scan)
  1187. xfs_icache_free_eofblocks(ip->i_mount, &eofb);
  1188. return scan;
  1189. }
  1190. void
  1191. xfs_inode_set_eofblocks_tag(
  1192. xfs_inode_t *ip)
  1193. {
  1194. struct xfs_mount *mp = ip->i_mount;
  1195. struct xfs_perag *pag;
  1196. int tagged;
  1197. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
  1198. spin_lock(&pag->pag_ici_lock);
  1199. trace_xfs_inode_set_eofblocks_tag(ip);
  1200. tagged = radix_tree_tagged(&pag->pag_ici_root,
  1201. XFS_ICI_EOFBLOCKS_TAG);
  1202. radix_tree_tag_set(&pag->pag_ici_root,
  1203. XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
  1204. XFS_ICI_EOFBLOCKS_TAG);
  1205. if (!tagged) {
  1206. /* propagate the eofblocks tag up into the perag radix tree */
  1207. spin_lock(&ip->i_mount->m_perag_lock);
  1208. radix_tree_tag_set(&ip->i_mount->m_perag_tree,
  1209. XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
  1210. XFS_ICI_EOFBLOCKS_TAG);
  1211. spin_unlock(&ip->i_mount->m_perag_lock);
  1212. /* kick off background trimming */
  1213. xfs_queue_eofblocks(ip->i_mount);
  1214. trace_xfs_perag_set_eofblocks(ip->i_mount, pag->pag_agno,
  1215. -1, _RET_IP_);
  1216. }
  1217. spin_unlock(&pag->pag_ici_lock);
  1218. xfs_perag_put(pag);
  1219. }
  1220. void
  1221. xfs_inode_clear_eofblocks_tag(
  1222. xfs_inode_t *ip)
  1223. {
  1224. struct xfs_mount *mp = ip->i_mount;
  1225. struct xfs_perag *pag;
  1226. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
  1227. spin_lock(&pag->pag_ici_lock);
  1228. trace_xfs_inode_clear_eofblocks_tag(ip);
  1229. radix_tree_tag_clear(&pag->pag_ici_root,
  1230. XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
  1231. XFS_ICI_EOFBLOCKS_TAG);
  1232. if (!radix_tree_tagged(&pag->pag_ici_root, XFS_ICI_EOFBLOCKS_TAG)) {
  1233. /* clear the eofblocks tag from the perag radix tree */
  1234. spin_lock(&ip->i_mount->m_perag_lock);
  1235. radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
  1236. XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
  1237. XFS_ICI_EOFBLOCKS_TAG);
  1238. spin_unlock(&ip->i_mount->m_perag_lock);
  1239. trace_xfs_perag_clear_eofblocks(ip->i_mount, pag->pag_agno,
  1240. -1, _RET_IP_);
  1241. }
  1242. spin_unlock(&pag->pag_ici_lock);
  1243. xfs_perag_put(pag);
  1244. }