xfs_icache.c 37 KB

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