xfs_icache.c 29 KB

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