xfs_icache.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694
  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 "xfs_reflink.h"
  37. #include <linux/kthread.h>
  38. #include <linux/freezer.h>
  39. /*
  40. * Allocate and initialise an xfs_inode.
  41. */
  42. struct xfs_inode *
  43. xfs_inode_alloc(
  44. struct xfs_mount *mp,
  45. xfs_ino_t ino)
  46. {
  47. struct xfs_inode *ip;
  48. /*
  49. * if this didn't occur in transactions, we could use
  50. * KM_MAYFAIL and return NULL here on ENOMEM. Set the
  51. * code up to do this anyway.
  52. */
  53. ip = kmem_zone_alloc(xfs_inode_zone, KM_SLEEP);
  54. if (!ip)
  55. return NULL;
  56. if (inode_init_always(mp->m_super, VFS_I(ip))) {
  57. kmem_zone_free(xfs_inode_zone, ip);
  58. return NULL;
  59. }
  60. /* VFS doesn't initialise i_mode! */
  61. VFS_I(ip)->i_mode = 0;
  62. XFS_STATS_INC(mp, vn_active);
  63. ASSERT(atomic_read(&ip->i_pincount) == 0);
  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. ip->i_cowfp = NULL;
  73. ip->i_cnextents = 0;
  74. ip->i_cformat = XFS_DINODE_FMT_EXTENTS;
  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(ip->i_d));
  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. switch (VFS_I(ip)->i_mode & S_IFMT) {
  88. case S_IFREG:
  89. case S_IFDIR:
  90. case S_IFLNK:
  91. xfs_idestroy_fork(ip, XFS_DATA_FORK);
  92. break;
  93. }
  94. if (ip->i_afp)
  95. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  96. if (ip->i_cowfp)
  97. xfs_idestroy_fork(ip, XFS_COW_FORK);
  98. if (ip->i_itemp) {
  99. ASSERT(!(ip->i_itemp->ili_item.li_flags & XFS_LI_IN_AIL));
  100. xfs_inode_item_destroy(ip);
  101. ip->i_itemp = NULL;
  102. }
  103. kmem_zone_free(xfs_inode_zone, ip);
  104. }
  105. static void
  106. __xfs_inode_free(
  107. struct xfs_inode *ip)
  108. {
  109. /* asserts to verify all state is correct here */
  110. ASSERT(atomic_read(&ip->i_pincount) == 0);
  111. XFS_STATS_DEC(ip->i_mount, vn_active);
  112. call_rcu(&VFS_I(ip)->i_rcu, xfs_inode_free_callback);
  113. }
  114. void
  115. xfs_inode_free(
  116. struct xfs_inode *ip)
  117. {
  118. ASSERT(!xfs_isiflocked(ip));
  119. /*
  120. * Because we use RCU freeing we need to ensure the inode always
  121. * appears to be reclaimed with an invalid inode number when in the
  122. * free state. The ip->i_flags_lock provides the barrier against lookup
  123. * races.
  124. */
  125. spin_lock(&ip->i_flags_lock);
  126. ip->i_flags = XFS_IRECLAIM;
  127. ip->i_ino = 0;
  128. spin_unlock(&ip->i_flags_lock);
  129. __xfs_inode_free(ip);
  130. }
  131. /*
  132. * Queue a new inode reclaim pass if there are reclaimable inodes and there
  133. * isn't a reclaim pass already in progress. By default it runs every 5s based
  134. * on the xfs periodic sync default of 30s. Perhaps this should have it's own
  135. * tunable, but that can be done if this method proves to be ineffective or too
  136. * aggressive.
  137. */
  138. static void
  139. xfs_reclaim_work_queue(
  140. struct xfs_mount *mp)
  141. {
  142. rcu_read_lock();
  143. if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_RECLAIM_TAG)) {
  144. queue_delayed_work(mp->m_reclaim_workqueue, &mp->m_reclaim_work,
  145. msecs_to_jiffies(xfs_syncd_centisecs / 6 * 10));
  146. }
  147. rcu_read_unlock();
  148. }
  149. /*
  150. * This is a fast pass over the inode cache to try to get reclaim moving on as
  151. * many inodes as possible in a short period of time. It kicks itself every few
  152. * seconds, as well as being kicked by the inode cache shrinker when memory
  153. * goes low. It scans as quickly as possible avoiding locked inodes or those
  154. * already being flushed, and once done schedules a future pass.
  155. */
  156. void
  157. xfs_reclaim_worker(
  158. struct work_struct *work)
  159. {
  160. struct xfs_mount *mp = container_of(to_delayed_work(work),
  161. struct xfs_mount, m_reclaim_work);
  162. xfs_reclaim_inodes(mp, SYNC_TRYLOCK);
  163. xfs_reclaim_work_queue(mp);
  164. }
  165. static void
  166. xfs_perag_set_reclaim_tag(
  167. struct xfs_perag *pag)
  168. {
  169. struct xfs_mount *mp = pag->pag_mount;
  170. lockdep_assert_held(&pag->pag_ici_lock);
  171. if (pag->pag_ici_reclaimable++)
  172. return;
  173. /* propagate the reclaim tag up into the perag radix tree */
  174. spin_lock(&mp->m_perag_lock);
  175. radix_tree_tag_set(&mp->m_perag_tree, pag->pag_agno,
  176. XFS_ICI_RECLAIM_TAG);
  177. spin_unlock(&mp->m_perag_lock);
  178. /* schedule periodic background inode reclaim */
  179. xfs_reclaim_work_queue(mp);
  180. trace_xfs_perag_set_reclaim(mp, pag->pag_agno, -1, _RET_IP_);
  181. }
  182. static void
  183. xfs_perag_clear_reclaim_tag(
  184. struct xfs_perag *pag)
  185. {
  186. struct xfs_mount *mp = pag->pag_mount;
  187. lockdep_assert_held(&pag->pag_ici_lock);
  188. if (--pag->pag_ici_reclaimable)
  189. return;
  190. /* clear the reclaim tag from the perag radix tree */
  191. spin_lock(&mp->m_perag_lock);
  192. radix_tree_tag_clear(&mp->m_perag_tree, pag->pag_agno,
  193. XFS_ICI_RECLAIM_TAG);
  194. spin_unlock(&mp->m_perag_lock);
  195. trace_xfs_perag_clear_reclaim(mp, pag->pag_agno, -1, _RET_IP_);
  196. }
  197. /*
  198. * We set the inode flag atomically with the radix tree tag.
  199. * Once we get tag lookups on the radix tree, this inode flag
  200. * can go away.
  201. */
  202. void
  203. xfs_inode_set_reclaim_tag(
  204. struct xfs_inode *ip)
  205. {
  206. struct xfs_mount *mp = ip->i_mount;
  207. struct xfs_perag *pag;
  208. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
  209. spin_lock(&pag->pag_ici_lock);
  210. spin_lock(&ip->i_flags_lock);
  211. radix_tree_tag_set(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino),
  212. XFS_ICI_RECLAIM_TAG);
  213. xfs_perag_set_reclaim_tag(pag);
  214. __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
  215. spin_unlock(&ip->i_flags_lock);
  216. spin_unlock(&pag->pag_ici_lock);
  217. xfs_perag_put(pag);
  218. }
  219. STATIC void
  220. xfs_inode_clear_reclaim_tag(
  221. struct xfs_perag *pag,
  222. xfs_ino_t ino)
  223. {
  224. radix_tree_tag_clear(&pag->pag_ici_root,
  225. XFS_INO_TO_AGINO(pag->pag_mount, ino),
  226. XFS_ICI_RECLAIM_TAG);
  227. xfs_perag_clear_reclaim_tag(pag);
  228. }
  229. static void
  230. xfs_inew_wait(
  231. struct xfs_inode *ip)
  232. {
  233. wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_INEW_BIT);
  234. DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_INEW_BIT);
  235. do {
  236. prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
  237. if (!xfs_iflags_test(ip, XFS_INEW))
  238. break;
  239. schedule();
  240. } while (true);
  241. finish_wait(wq, &wait.wait);
  242. }
  243. /*
  244. * When we recycle a reclaimable inode, we need to re-initialise the VFS inode
  245. * part of the structure. This is made more complex by the fact we store
  246. * information about the on-disk values in the VFS inode and so we can't just
  247. * overwrite the values unconditionally. Hence we save the parameters we
  248. * need to retain across reinitialisation, and rewrite them into the VFS inode
  249. * after reinitialisation even if it fails.
  250. */
  251. static int
  252. xfs_reinit_inode(
  253. struct xfs_mount *mp,
  254. struct inode *inode)
  255. {
  256. int error;
  257. uint32_t nlink = inode->i_nlink;
  258. uint32_t generation = inode->i_generation;
  259. uint64_t version = inode->i_version;
  260. umode_t mode = inode->i_mode;
  261. error = inode_init_always(mp->m_super, inode);
  262. set_nlink(inode, nlink);
  263. inode->i_generation = generation;
  264. inode->i_version = version;
  265. inode->i_mode = mode;
  266. return error;
  267. }
  268. /*
  269. * Check the validity of the inode we just found it the cache
  270. */
  271. static int
  272. xfs_iget_cache_hit(
  273. struct xfs_perag *pag,
  274. struct xfs_inode *ip,
  275. xfs_ino_t ino,
  276. int flags,
  277. int lock_flags) __releases(RCU)
  278. {
  279. struct inode *inode = VFS_I(ip);
  280. struct xfs_mount *mp = ip->i_mount;
  281. int error;
  282. /*
  283. * check for re-use of an inode within an RCU grace period due to the
  284. * radix tree nodes not being updated yet. We monitor for this by
  285. * setting the inode number to zero before freeing the inode structure.
  286. * If the inode has been reallocated and set up, then the inode number
  287. * will not match, so check for that, too.
  288. */
  289. spin_lock(&ip->i_flags_lock);
  290. if (ip->i_ino != ino) {
  291. trace_xfs_iget_skip(ip);
  292. XFS_STATS_INC(mp, xs_ig_frecycle);
  293. error = -EAGAIN;
  294. goto out_error;
  295. }
  296. /*
  297. * If we are racing with another cache hit that is currently
  298. * instantiating this inode or currently recycling it out of
  299. * reclaimabe state, wait for the initialisation to complete
  300. * before continuing.
  301. *
  302. * XXX(hch): eventually we should do something equivalent to
  303. * wait_on_inode to wait for these flags to be cleared
  304. * instead of polling for it.
  305. */
  306. if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) {
  307. trace_xfs_iget_skip(ip);
  308. XFS_STATS_INC(mp, xs_ig_frecycle);
  309. error = -EAGAIN;
  310. goto out_error;
  311. }
  312. /*
  313. * If lookup is racing with unlink return an error immediately.
  314. */
  315. if (VFS_I(ip)->i_mode == 0 && !(flags & XFS_IGET_CREATE)) {
  316. error = -ENOENT;
  317. goto out_error;
  318. }
  319. /*
  320. * If IRECLAIMABLE is set, we've torn down the VFS inode already.
  321. * Need to carefully get it back into useable state.
  322. */
  323. if (ip->i_flags & XFS_IRECLAIMABLE) {
  324. trace_xfs_iget_reclaim(ip);
  325. /*
  326. * We need to set XFS_IRECLAIM to prevent xfs_reclaim_inode
  327. * from stomping over us while we recycle the inode. We can't
  328. * clear the radix tree reclaimable tag yet as it requires
  329. * pag_ici_lock to be held exclusive.
  330. */
  331. ip->i_flags |= XFS_IRECLAIM;
  332. spin_unlock(&ip->i_flags_lock);
  333. rcu_read_unlock();
  334. error = xfs_reinit_inode(mp, inode);
  335. if (error) {
  336. bool wake;
  337. /*
  338. * Re-initializing the inode failed, and we are in deep
  339. * trouble. Try to re-add it to the reclaim list.
  340. */
  341. rcu_read_lock();
  342. spin_lock(&ip->i_flags_lock);
  343. wake = !!__xfs_iflags_test(ip, XFS_INEW);
  344. ip->i_flags &= ~(XFS_INEW | XFS_IRECLAIM);
  345. if (wake)
  346. wake_up_bit(&ip->i_flags, __XFS_INEW_BIT);
  347. ASSERT(ip->i_flags & XFS_IRECLAIMABLE);
  348. trace_xfs_iget_reclaim_fail(ip);
  349. goto out_error;
  350. }
  351. spin_lock(&pag->pag_ici_lock);
  352. spin_lock(&ip->i_flags_lock);
  353. /*
  354. * Clear the per-lifetime state in the inode as we are now
  355. * effectively a new inode and need to return to the initial
  356. * state before reuse occurs.
  357. */
  358. ip->i_flags &= ~XFS_IRECLAIM_RESET_FLAGS;
  359. ip->i_flags |= XFS_INEW;
  360. xfs_inode_clear_reclaim_tag(pag, ip->i_ino);
  361. inode->i_state = I_NEW;
  362. ASSERT(!rwsem_is_locked(&ip->i_iolock.mr_lock));
  363. mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
  364. spin_unlock(&ip->i_flags_lock);
  365. spin_unlock(&pag->pag_ici_lock);
  366. } else {
  367. /* If the VFS inode is being torn down, pause and try again. */
  368. if (!igrab(inode)) {
  369. trace_xfs_iget_skip(ip);
  370. error = -EAGAIN;
  371. goto out_error;
  372. }
  373. /* We've got a live one. */
  374. spin_unlock(&ip->i_flags_lock);
  375. rcu_read_unlock();
  376. trace_xfs_iget_hit(ip);
  377. }
  378. if (lock_flags != 0)
  379. xfs_ilock(ip, lock_flags);
  380. xfs_iflags_clear(ip, XFS_ISTALE | XFS_IDONTCACHE);
  381. XFS_STATS_INC(mp, xs_ig_found);
  382. return 0;
  383. out_error:
  384. spin_unlock(&ip->i_flags_lock);
  385. rcu_read_unlock();
  386. return error;
  387. }
  388. static int
  389. xfs_iget_cache_miss(
  390. struct xfs_mount *mp,
  391. struct xfs_perag *pag,
  392. xfs_trans_t *tp,
  393. xfs_ino_t ino,
  394. struct xfs_inode **ipp,
  395. int flags,
  396. int lock_flags)
  397. {
  398. struct xfs_inode *ip;
  399. int error;
  400. xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
  401. int iflags;
  402. ip = xfs_inode_alloc(mp, ino);
  403. if (!ip)
  404. return -ENOMEM;
  405. error = xfs_iread(mp, tp, ip, flags);
  406. if (error)
  407. goto out_destroy;
  408. trace_xfs_iget_miss(ip);
  409. if ((VFS_I(ip)->i_mode == 0) && !(flags & XFS_IGET_CREATE)) {
  410. error = -ENOENT;
  411. goto out_destroy;
  412. }
  413. /*
  414. * Preload the radix tree so we can insert safely under the
  415. * write spinlock. Note that we cannot sleep inside the preload
  416. * region. Since we can be called from transaction context, don't
  417. * recurse into the file system.
  418. */
  419. if (radix_tree_preload(GFP_NOFS)) {
  420. error = -EAGAIN;
  421. goto out_destroy;
  422. }
  423. /*
  424. * Because the inode hasn't been added to the radix-tree yet it can't
  425. * be found by another thread, so we can do the non-sleeping lock here.
  426. */
  427. if (lock_flags) {
  428. if (!xfs_ilock_nowait(ip, lock_flags))
  429. BUG();
  430. }
  431. /*
  432. * These values must be set before inserting the inode into the radix
  433. * tree as the moment it is inserted a concurrent lookup (allowed by the
  434. * RCU locking mechanism) can find it and that lookup must see that this
  435. * is an inode currently under construction (i.e. that XFS_INEW is set).
  436. * The ip->i_flags_lock that protects the XFS_INEW flag forms the
  437. * memory barrier that ensures this detection works correctly at lookup
  438. * time.
  439. */
  440. iflags = XFS_INEW;
  441. if (flags & XFS_IGET_DONTCACHE)
  442. iflags |= XFS_IDONTCACHE;
  443. ip->i_udquot = NULL;
  444. ip->i_gdquot = NULL;
  445. ip->i_pdquot = NULL;
  446. xfs_iflags_set(ip, iflags);
  447. /* insert the new inode */
  448. spin_lock(&pag->pag_ici_lock);
  449. error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
  450. if (unlikely(error)) {
  451. WARN_ON(error != -EEXIST);
  452. XFS_STATS_INC(mp, xs_ig_dup);
  453. error = -EAGAIN;
  454. goto out_preload_end;
  455. }
  456. spin_unlock(&pag->pag_ici_lock);
  457. radix_tree_preload_end();
  458. *ipp = ip;
  459. return 0;
  460. out_preload_end:
  461. spin_unlock(&pag->pag_ici_lock);
  462. radix_tree_preload_end();
  463. if (lock_flags)
  464. xfs_iunlock(ip, lock_flags);
  465. out_destroy:
  466. __destroy_inode(VFS_I(ip));
  467. xfs_inode_free(ip);
  468. return error;
  469. }
  470. /*
  471. * Look up an inode by number in the given file system.
  472. * The inode is looked up in the cache held in each AG.
  473. * If the inode is found in the cache, initialise the vfs inode
  474. * if necessary.
  475. *
  476. * If it is not in core, read it in from the file system's device,
  477. * add it to the cache and initialise the vfs inode.
  478. *
  479. * The inode is locked according to the value of the lock_flags parameter.
  480. * This flag parameter indicates how and if the inode's IO lock and inode lock
  481. * should be taken.
  482. *
  483. * mp -- the mount point structure for the current file system. It points
  484. * to the inode hash table.
  485. * tp -- a pointer to the current transaction if there is one. This is
  486. * simply passed through to the xfs_iread() call.
  487. * ino -- the number of the inode desired. This is the unique identifier
  488. * within the file system for the inode being requested.
  489. * lock_flags -- flags indicating how to lock the inode. See the comment
  490. * for xfs_ilock() for a list of valid values.
  491. */
  492. int
  493. xfs_iget(
  494. xfs_mount_t *mp,
  495. xfs_trans_t *tp,
  496. xfs_ino_t ino,
  497. uint flags,
  498. uint lock_flags,
  499. xfs_inode_t **ipp)
  500. {
  501. xfs_inode_t *ip;
  502. int error;
  503. xfs_perag_t *pag;
  504. xfs_agino_t agino;
  505. /*
  506. * xfs_reclaim_inode() uses the ILOCK to ensure an inode
  507. * doesn't get freed while it's being referenced during a
  508. * radix tree traversal here. It assumes this function
  509. * aqcuires only the ILOCK (and therefore it has no need to
  510. * involve the IOLOCK in this synchronization).
  511. */
  512. ASSERT((lock_flags & (XFS_IOLOCK_EXCL | XFS_IOLOCK_SHARED)) == 0);
  513. /* reject inode numbers outside existing AGs */
  514. if (!ino || XFS_INO_TO_AGNO(mp, ino) >= mp->m_sb.sb_agcount)
  515. return -EINVAL;
  516. XFS_STATS_INC(mp, xs_ig_attempts);
  517. /* get the perag structure and ensure that it's inode capable */
  518. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ino));
  519. agino = XFS_INO_TO_AGINO(mp, ino);
  520. again:
  521. error = 0;
  522. rcu_read_lock();
  523. ip = radix_tree_lookup(&pag->pag_ici_root, agino);
  524. if (ip) {
  525. error = xfs_iget_cache_hit(pag, ip, ino, flags, lock_flags);
  526. if (error)
  527. goto out_error_or_again;
  528. } else {
  529. rcu_read_unlock();
  530. XFS_STATS_INC(mp, xs_ig_missed);
  531. error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip,
  532. flags, lock_flags);
  533. if (error)
  534. goto out_error_or_again;
  535. }
  536. xfs_perag_put(pag);
  537. *ipp = ip;
  538. /*
  539. * If we have a real type for an on-disk inode, we can setup the inode
  540. * now. If it's a new inode being created, xfs_ialloc will handle it.
  541. */
  542. if (xfs_iflags_test(ip, XFS_INEW) && VFS_I(ip)->i_mode != 0)
  543. xfs_setup_existing_inode(ip);
  544. return 0;
  545. out_error_or_again:
  546. if (error == -EAGAIN) {
  547. delay(1);
  548. goto again;
  549. }
  550. xfs_perag_put(pag);
  551. return error;
  552. }
  553. /*
  554. * The inode lookup is done in batches to keep the amount of lock traffic and
  555. * radix tree lookups to a minimum. The batch size is a trade off between
  556. * lookup reduction and stack usage. This is in the reclaim path, so we can't
  557. * be too greedy.
  558. */
  559. #define XFS_LOOKUP_BATCH 32
  560. STATIC int
  561. xfs_inode_ag_walk_grab(
  562. struct xfs_inode *ip,
  563. int flags)
  564. {
  565. struct inode *inode = VFS_I(ip);
  566. bool newinos = !!(flags & XFS_AGITER_INEW_WAIT);
  567. ASSERT(rcu_read_lock_held());
  568. /*
  569. * check for stale RCU freed inode
  570. *
  571. * If the inode has been reallocated, it doesn't matter if it's not in
  572. * the AG we are walking - we are walking for writeback, so if it
  573. * passes all the "valid inode" checks and is dirty, then we'll write
  574. * it back anyway. If it has been reallocated and still being
  575. * initialised, the XFS_INEW check below will catch it.
  576. */
  577. spin_lock(&ip->i_flags_lock);
  578. if (!ip->i_ino)
  579. goto out_unlock_noent;
  580. /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
  581. if ((!newinos && __xfs_iflags_test(ip, XFS_INEW)) ||
  582. __xfs_iflags_test(ip, XFS_IRECLAIMABLE | XFS_IRECLAIM))
  583. goto out_unlock_noent;
  584. spin_unlock(&ip->i_flags_lock);
  585. /* nothing to sync during shutdown */
  586. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  587. return -EFSCORRUPTED;
  588. /* If we can't grab the inode, it must on it's way to reclaim. */
  589. if (!igrab(inode))
  590. return -ENOENT;
  591. /* inode is valid */
  592. return 0;
  593. out_unlock_noent:
  594. spin_unlock(&ip->i_flags_lock);
  595. return -ENOENT;
  596. }
  597. STATIC int
  598. xfs_inode_ag_walk(
  599. struct xfs_mount *mp,
  600. struct xfs_perag *pag,
  601. int (*execute)(struct xfs_inode *ip, int flags,
  602. void *args),
  603. int flags,
  604. void *args,
  605. int tag,
  606. int iter_flags)
  607. {
  608. uint32_t first_index;
  609. int last_error = 0;
  610. int skipped;
  611. int done;
  612. int nr_found;
  613. restart:
  614. done = 0;
  615. skipped = 0;
  616. first_index = 0;
  617. nr_found = 0;
  618. do {
  619. struct xfs_inode *batch[XFS_LOOKUP_BATCH];
  620. int error = 0;
  621. int i;
  622. rcu_read_lock();
  623. if (tag == -1)
  624. nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
  625. (void **)batch, first_index,
  626. XFS_LOOKUP_BATCH);
  627. else
  628. nr_found = radix_tree_gang_lookup_tag(
  629. &pag->pag_ici_root,
  630. (void **) batch, first_index,
  631. XFS_LOOKUP_BATCH, tag);
  632. if (!nr_found) {
  633. rcu_read_unlock();
  634. break;
  635. }
  636. /*
  637. * Grab the inodes before we drop the lock. if we found
  638. * nothing, nr == 0 and the loop will be skipped.
  639. */
  640. for (i = 0; i < nr_found; i++) {
  641. struct xfs_inode *ip = batch[i];
  642. if (done || xfs_inode_ag_walk_grab(ip, iter_flags))
  643. batch[i] = NULL;
  644. /*
  645. * Update the index for the next lookup. Catch
  646. * overflows into the next AG range which can occur if
  647. * we have inodes in the last block of the AG and we
  648. * are currently pointing to the last inode.
  649. *
  650. * Because we may see inodes that are from the wrong AG
  651. * due to RCU freeing and reallocation, only update the
  652. * index if it lies in this AG. It was a race that lead
  653. * us to see this inode, so another lookup from the
  654. * same index will not find it again.
  655. */
  656. if (XFS_INO_TO_AGNO(mp, ip->i_ino) != pag->pag_agno)
  657. continue;
  658. first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
  659. if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
  660. done = 1;
  661. }
  662. /* unlock now we've grabbed the inodes. */
  663. rcu_read_unlock();
  664. for (i = 0; i < nr_found; i++) {
  665. if (!batch[i])
  666. continue;
  667. if ((iter_flags & XFS_AGITER_INEW_WAIT) &&
  668. xfs_iflags_test(batch[i], XFS_INEW))
  669. xfs_inew_wait(batch[i]);
  670. error = execute(batch[i], flags, args);
  671. IRELE(batch[i]);
  672. if (error == -EAGAIN) {
  673. skipped++;
  674. continue;
  675. }
  676. if (error && last_error != -EFSCORRUPTED)
  677. last_error = error;
  678. }
  679. /* bail out if the filesystem is corrupted. */
  680. if (error == -EFSCORRUPTED)
  681. break;
  682. cond_resched();
  683. } while (nr_found && !done);
  684. if (skipped) {
  685. delay(1);
  686. goto restart;
  687. }
  688. return last_error;
  689. }
  690. /*
  691. * Background scanning to trim post-EOF preallocated space. This is queued
  692. * based on the 'speculative_prealloc_lifetime' tunable (5m by default).
  693. */
  694. void
  695. xfs_queue_eofblocks(
  696. struct xfs_mount *mp)
  697. {
  698. rcu_read_lock();
  699. if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_EOFBLOCKS_TAG))
  700. queue_delayed_work(mp->m_eofblocks_workqueue,
  701. &mp->m_eofblocks_work,
  702. msecs_to_jiffies(xfs_eofb_secs * 1000));
  703. rcu_read_unlock();
  704. }
  705. void
  706. xfs_eofblocks_worker(
  707. struct work_struct *work)
  708. {
  709. struct xfs_mount *mp = container_of(to_delayed_work(work),
  710. struct xfs_mount, m_eofblocks_work);
  711. xfs_icache_free_eofblocks(mp, NULL);
  712. xfs_queue_eofblocks(mp);
  713. }
  714. /*
  715. * Background scanning to trim preallocated CoW space. This is queued
  716. * based on the 'speculative_cow_prealloc_lifetime' tunable (5m by default).
  717. * (We'll just piggyback on the post-EOF prealloc space workqueue.)
  718. */
  719. STATIC void
  720. xfs_queue_cowblocks(
  721. struct xfs_mount *mp)
  722. {
  723. rcu_read_lock();
  724. if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_COWBLOCKS_TAG))
  725. queue_delayed_work(mp->m_eofblocks_workqueue,
  726. &mp->m_cowblocks_work,
  727. msecs_to_jiffies(xfs_cowb_secs * 1000));
  728. rcu_read_unlock();
  729. }
  730. void
  731. xfs_cowblocks_worker(
  732. struct work_struct *work)
  733. {
  734. struct xfs_mount *mp = container_of(to_delayed_work(work),
  735. struct xfs_mount, m_cowblocks_work);
  736. xfs_icache_free_cowblocks(mp, NULL);
  737. xfs_queue_cowblocks(mp);
  738. }
  739. int
  740. xfs_inode_ag_iterator_flags(
  741. struct xfs_mount *mp,
  742. int (*execute)(struct xfs_inode *ip, int flags,
  743. void *args),
  744. int flags,
  745. void *args,
  746. int iter_flags)
  747. {
  748. struct xfs_perag *pag;
  749. int error = 0;
  750. int last_error = 0;
  751. xfs_agnumber_t ag;
  752. ag = 0;
  753. while ((pag = xfs_perag_get(mp, ag))) {
  754. ag = pag->pag_agno + 1;
  755. error = xfs_inode_ag_walk(mp, pag, execute, flags, args, -1,
  756. iter_flags);
  757. xfs_perag_put(pag);
  758. if (error) {
  759. last_error = error;
  760. if (error == -EFSCORRUPTED)
  761. break;
  762. }
  763. }
  764. return last_error;
  765. }
  766. int
  767. xfs_inode_ag_iterator(
  768. struct xfs_mount *mp,
  769. int (*execute)(struct xfs_inode *ip, int flags,
  770. void *args),
  771. int flags,
  772. void *args)
  773. {
  774. return xfs_inode_ag_iterator_flags(mp, execute, flags, args, 0);
  775. }
  776. int
  777. xfs_inode_ag_iterator_tag(
  778. struct xfs_mount *mp,
  779. int (*execute)(struct xfs_inode *ip, int flags,
  780. void *args),
  781. int flags,
  782. void *args,
  783. int tag)
  784. {
  785. struct xfs_perag *pag;
  786. int error = 0;
  787. int last_error = 0;
  788. xfs_agnumber_t ag;
  789. ag = 0;
  790. while ((pag = xfs_perag_get_tag(mp, ag, tag))) {
  791. ag = pag->pag_agno + 1;
  792. error = xfs_inode_ag_walk(mp, pag, execute, flags, args, tag,
  793. 0);
  794. xfs_perag_put(pag);
  795. if (error) {
  796. last_error = error;
  797. if (error == -EFSCORRUPTED)
  798. break;
  799. }
  800. }
  801. return last_error;
  802. }
  803. /*
  804. * Grab the inode for reclaim exclusively.
  805. * Return 0 if we grabbed it, non-zero otherwise.
  806. */
  807. STATIC int
  808. xfs_reclaim_inode_grab(
  809. struct xfs_inode *ip,
  810. int flags)
  811. {
  812. ASSERT(rcu_read_lock_held());
  813. /* quick check for stale RCU freed inode */
  814. if (!ip->i_ino)
  815. return 1;
  816. /*
  817. * If we are asked for non-blocking operation, do unlocked checks to
  818. * see if the inode already is being flushed or in reclaim to avoid
  819. * lock traffic.
  820. */
  821. if ((flags & SYNC_TRYLOCK) &&
  822. __xfs_iflags_test(ip, XFS_IFLOCK | XFS_IRECLAIM))
  823. return 1;
  824. /*
  825. * The radix tree lock here protects a thread in xfs_iget from racing
  826. * with us starting reclaim on the inode. Once we have the
  827. * XFS_IRECLAIM flag set it will not touch us.
  828. *
  829. * Due to RCU lookup, we may find inodes that have been freed and only
  830. * have XFS_IRECLAIM set. Indeed, we may see reallocated inodes that
  831. * aren't candidates for reclaim at all, so we must check the
  832. * XFS_IRECLAIMABLE is set first before proceeding to reclaim.
  833. */
  834. spin_lock(&ip->i_flags_lock);
  835. if (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) ||
  836. __xfs_iflags_test(ip, XFS_IRECLAIM)) {
  837. /* not a reclaim candidate. */
  838. spin_unlock(&ip->i_flags_lock);
  839. return 1;
  840. }
  841. __xfs_iflags_set(ip, XFS_IRECLAIM);
  842. spin_unlock(&ip->i_flags_lock);
  843. return 0;
  844. }
  845. /*
  846. * Inodes in different states need to be treated differently. The following
  847. * table lists the inode states and the reclaim actions necessary:
  848. *
  849. * inode state iflush ret required action
  850. * --------------- ---------- ---------------
  851. * bad - reclaim
  852. * shutdown EIO unpin and reclaim
  853. * clean, unpinned 0 reclaim
  854. * stale, unpinned 0 reclaim
  855. * clean, pinned(*) 0 requeue
  856. * stale, pinned EAGAIN requeue
  857. * dirty, async - requeue
  858. * dirty, sync 0 reclaim
  859. *
  860. * (*) dgc: I don't think the clean, pinned state is possible but it gets
  861. * handled anyway given the order of checks implemented.
  862. *
  863. * Also, because we get the flush lock first, we know that any inode that has
  864. * been flushed delwri has had the flush completed by the time we check that
  865. * the inode is clean.
  866. *
  867. * Note that because the inode is flushed delayed write by AIL pushing, the
  868. * flush lock may already be held here and waiting on it can result in very
  869. * long latencies. Hence for sync reclaims, where we wait on the flush lock,
  870. * the caller should push the AIL first before trying to reclaim inodes to
  871. * minimise the amount of time spent waiting. For background relaim, we only
  872. * bother to reclaim clean inodes anyway.
  873. *
  874. * Hence the order of actions after gaining the locks should be:
  875. * bad => reclaim
  876. * shutdown => unpin and reclaim
  877. * pinned, async => requeue
  878. * pinned, sync => unpin
  879. * stale => reclaim
  880. * clean => reclaim
  881. * dirty, async => requeue
  882. * dirty, sync => flush, wait and reclaim
  883. */
  884. STATIC int
  885. xfs_reclaim_inode(
  886. struct xfs_inode *ip,
  887. struct xfs_perag *pag,
  888. int sync_mode)
  889. {
  890. struct xfs_buf *bp = NULL;
  891. xfs_ino_t ino = ip->i_ino; /* for radix_tree_delete */
  892. int error;
  893. restart:
  894. error = 0;
  895. xfs_ilock(ip, XFS_ILOCK_EXCL);
  896. if (!xfs_iflock_nowait(ip)) {
  897. if (!(sync_mode & SYNC_WAIT))
  898. goto out;
  899. xfs_iflock(ip);
  900. }
  901. if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  902. xfs_iunpin_wait(ip);
  903. /* xfs_iflush_abort() drops the flush lock */
  904. xfs_iflush_abort(ip, false);
  905. goto reclaim;
  906. }
  907. if (xfs_ipincount(ip)) {
  908. if (!(sync_mode & SYNC_WAIT))
  909. goto out_ifunlock;
  910. xfs_iunpin_wait(ip);
  911. }
  912. if (xfs_iflags_test(ip, XFS_ISTALE) || xfs_inode_clean(ip)) {
  913. xfs_ifunlock(ip);
  914. goto reclaim;
  915. }
  916. /*
  917. * Never flush out dirty data during non-blocking reclaim, as it would
  918. * just contend with AIL pushing trying to do the same job.
  919. */
  920. if (!(sync_mode & SYNC_WAIT))
  921. goto out_ifunlock;
  922. /*
  923. * Now we have an inode that needs flushing.
  924. *
  925. * Note that xfs_iflush will never block on the inode buffer lock, as
  926. * xfs_ifree_cluster() can lock the inode buffer before it locks the
  927. * ip->i_lock, and we are doing the exact opposite here. As a result,
  928. * doing a blocking xfs_imap_to_bp() to get the cluster buffer would
  929. * result in an ABBA deadlock with xfs_ifree_cluster().
  930. *
  931. * As xfs_ifree_cluser() must gather all inodes that are active in the
  932. * cache to mark them stale, if we hit this case we don't actually want
  933. * to do IO here - we want the inode marked stale so we can simply
  934. * reclaim it. Hence if we get an EAGAIN error here, just unlock the
  935. * inode, back off and try again. Hopefully the next pass through will
  936. * see the stale flag set on the inode.
  937. */
  938. error = xfs_iflush(ip, &bp);
  939. if (error == -EAGAIN) {
  940. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  941. /* backoff longer than in xfs_ifree_cluster */
  942. delay(2);
  943. goto restart;
  944. }
  945. if (!error) {
  946. error = xfs_bwrite(bp);
  947. xfs_buf_relse(bp);
  948. }
  949. reclaim:
  950. ASSERT(!xfs_isiflocked(ip));
  951. /*
  952. * Because we use RCU freeing we need to ensure the inode always appears
  953. * to be reclaimed with an invalid inode number when in the free state.
  954. * We do this as early as possible under the ILOCK so that
  955. * xfs_iflush_cluster() and xfs_ifree_cluster() can be guaranteed to
  956. * detect races with us here. By doing this, we guarantee that once
  957. * xfs_iflush_cluster() or xfs_ifree_cluster() has locked XFS_ILOCK that
  958. * it will see either a valid inode that will serialise correctly, or it
  959. * will see an invalid inode that it can skip.
  960. */
  961. spin_lock(&ip->i_flags_lock);
  962. ip->i_flags = XFS_IRECLAIM;
  963. ip->i_ino = 0;
  964. spin_unlock(&ip->i_flags_lock);
  965. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  966. XFS_STATS_INC(ip->i_mount, xs_ig_reclaims);
  967. /*
  968. * Remove the inode from the per-AG radix tree.
  969. *
  970. * Because radix_tree_delete won't complain even if the item was never
  971. * added to the tree assert that it's been there before to catch
  972. * problems with the inode life time early on.
  973. */
  974. spin_lock(&pag->pag_ici_lock);
  975. if (!radix_tree_delete(&pag->pag_ici_root,
  976. XFS_INO_TO_AGINO(ip->i_mount, ino)))
  977. ASSERT(0);
  978. xfs_perag_clear_reclaim_tag(pag);
  979. spin_unlock(&pag->pag_ici_lock);
  980. /*
  981. * Here we do an (almost) spurious inode lock in order to coordinate
  982. * with inode cache radix tree lookups. This is because the lookup
  983. * can reference the inodes in the cache without taking references.
  984. *
  985. * We make that OK here by ensuring that we wait until the inode is
  986. * unlocked after the lookup before we go ahead and free it.
  987. */
  988. xfs_ilock(ip, XFS_ILOCK_EXCL);
  989. xfs_qm_dqdetach(ip);
  990. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  991. __xfs_inode_free(ip);
  992. return error;
  993. out_ifunlock:
  994. xfs_ifunlock(ip);
  995. out:
  996. xfs_iflags_clear(ip, XFS_IRECLAIM);
  997. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  998. /*
  999. * We could return -EAGAIN here to make reclaim rescan the inode tree in
  1000. * a short while. However, this just burns CPU time scanning the tree
  1001. * waiting for IO to complete and the reclaim work never goes back to
  1002. * the idle state. Instead, return 0 to let the next scheduled
  1003. * background reclaim attempt to reclaim the inode again.
  1004. */
  1005. return 0;
  1006. }
  1007. /*
  1008. * Walk the AGs and reclaim the inodes in them. Even if the filesystem is
  1009. * corrupted, we still want to try to reclaim all the inodes. If we don't,
  1010. * then a shut down during filesystem unmount reclaim walk leak all the
  1011. * unreclaimed inodes.
  1012. */
  1013. STATIC int
  1014. xfs_reclaim_inodes_ag(
  1015. struct xfs_mount *mp,
  1016. int flags,
  1017. int *nr_to_scan)
  1018. {
  1019. struct xfs_perag *pag;
  1020. int error = 0;
  1021. int last_error = 0;
  1022. xfs_agnumber_t ag;
  1023. int trylock = flags & SYNC_TRYLOCK;
  1024. int skipped;
  1025. restart:
  1026. ag = 0;
  1027. skipped = 0;
  1028. while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
  1029. unsigned long first_index = 0;
  1030. int done = 0;
  1031. int nr_found = 0;
  1032. ag = pag->pag_agno + 1;
  1033. if (trylock) {
  1034. if (!mutex_trylock(&pag->pag_ici_reclaim_lock)) {
  1035. skipped++;
  1036. xfs_perag_put(pag);
  1037. continue;
  1038. }
  1039. first_index = pag->pag_ici_reclaim_cursor;
  1040. } else
  1041. mutex_lock(&pag->pag_ici_reclaim_lock);
  1042. do {
  1043. struct xfs_inode *batch[XFS_LOOKUP_BATCH];
  1044. int i;
  1045. rcu_read_lock();
  1046. nr_found = radix_tree_gang_lookup_tag(
  1047. &pag->pag_ici_root,
  1048. (void **)batch, first_index,
  1049. XFS_LOOKUP_BATCH,
  1050. XFS_ICI_RECLAIM_TAG);
  1051. if (!nr_found) {
  1052. done = 1;
  1053. rcu_read_unlock();
  1054. break;
  1055. }
  1056. /*
  1057. * Grab the inodes before we drop the lock. if we found
  1058. * nothing, nr == 0 and the loop will be skipped.
  1059. */
  1060. for (i = 0; i < nr_found; i++) {
  1061. struct xfs_inode *ip = batch[i];
  1062. if (done || xfs_reclaim_inode_grab(ip, flags))
  1063. batch[i] = NULL;
  1064. /*
  1065. * Update the index for the next lookup. Catch
  1066. * overflows into the next AG range which can
  1067. * occur if we have inodes in the last block of
  1068. * the AG and we are currently pointing to the
  1069. * last inode.
  1070. *
  1071. * Because we may see inodes that are from the
  1072. * wrong AG due to RCU freeing and
  1073. * reallocation, only update the index if it
  1074. * lies in this AG. It was a race that lead us
  1075. * to see this inode, so another lookup from
  1076. * the same index will not find it again.
  1077. */
  1078. if (XFS_INO_TO_AGNO(mp, ip->i_ino) !=
  1079. pag->pag_agno)
  1080. continue;
  1081. first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
  1082. if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
  1083. done = 1;
  1084. }
  1085. /* unlock now we've grabbed the inodes. */
  1086. rcu_read_unlock();
  1087. for (i = 0; i < nr_found; i++) {
  1088. if (!batch[i])
  1089. continue;
  1090. error = xfs_reclaim_inode(batch[i], pag, flags);
  1091. if (error && last_error != -EFSCORRUPTED)
  1092. last_error = error;
  1093. }
  1094. *nr_to_scan -= XFS_LOOKUP_BATCH;
  1095. cond_resched();
  1096. } while (nr_found && !done && *nr_to_scan > 0);
  1097. if (trylock && !done)
  1098. pag->pag_ici_reclaim_cursor = first_index;
  1099. else
  1100. pag->pag_ici_reclaim_cursor = 0;
  1101. mutex_unlock(&pag->pag_ici_reclaim_lock);
  1102. xfs_perag_put(pag);
  1103. }
  1104. /*
  1105. * if we skipped any AG, and we still have scan count remaining, do
  1106. * another pass this time using blocking reclaim semantics (i.e
  1107. * waiting on the reclaim locks and ignoring the reclaim cursors). This
  1108. * ensure that when we get more reclaimers than AGs we block rather
  1109. * than spin trying to execute reclaim.
  1110. */
  1111. if (skipped && (flags & SYNC_WAIT) && *nr_to_scan > 0) {
  1112. trylock = 0;
  1113. goto restart;
  1114. }
  1115. return last_error;
  1116. }
  1117. int
  1118. xfs_reclaim_inodes(
  1119. xfs_mount_t *mp,
  1120. int mode)
  1121. {
  1122. int nr_to_scan = INT_MAX;
  1123. return xfs_reclaim_inodes_ag(mp, mode, &nr_to_scan);
  1124. }
  1125. /*
  1126. * Scan a certain number of inodes for reclaim.
  1127. *
  1128. * When called we make sure that there is a background (fast) inode reclaim in
  1129. * progress, while we will throttle the speed of reclaim via doing synchronous
  1130. * reclaim of inodes. That means if we come across dirty inodes, we wait for
  1131. * them to be cleaned, which we hope will not be very long due to the
  1132. * background walker having already kicked the IO off on those dirty inodes.
  1133. */
  1134. long
  1135. xfs_reclaim_inodes_nr(
  1136. struct xfs_mount *mp,
  1137. int nr_to_scan)
  1138. {
  1139. /* kick background reclaimer and push the AIL */
  1140. xfs_reclaim_work_queue(mp);
  1141. xfs_ail_push_all(mp->m_ail);
  1142. return xfs_reclaim_inodes_ag(mp, SYNC_TRYLOCK | SYNC_WAIT, &nr_to_scan);
  1143. }
  1144. /*
  1145. * Return the number of reclaimable inodes in the filesystem for
  1146. * the shrinker to determine how much to reclaim.
  1147. */
  1148. int
  1149. xfs_reclaim_inodes_count(
  1150. struct xfs_mount *mp)
  1151. {
  1152. struct xfs_perag *pag;
  1153. xfs_agnumber_t ag = 0;
  1154. int reclaimable = 0;
  1155. while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
  1156. ag = pag->pag_agno + 1;
  1157. reclaimable += pag->pag_ici_reclaimable;
  1158. xfs_perag_put(pag);
  1159. }
  1160. return reclaimable;
  1161. }
  1162. STATIC int
  1163. xfs_inode_match_id(
  1164. struct xfs_inode *ip,
  1165. struct xfs_eofblocks *eofb)
  1166. {
  1167. if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
  1168. !uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
  1169. return 0;
  1170. if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
  1171. !gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
  1172. return 0;
  1173. if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
  1174. xfs_get_projid(ip) != eofb->eof_prid)
  1175. return 0;
  1176. return 1;
  1177. }
  1178. /*
  1179. * A union-based inode filtering algorithm. Process the inode if any of the
  1180. * criteria match. This is for global/internal scans only.
  1181. */
  1182. STATIC int
  1183. xfs_inode_match_id_union(
  1184. struct xfs_inode *ip,
  1185. struct xfs_eofblocks *eofb)
  1186. {
  1187. if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
  1188. uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
  1189. return 1;
  1190. if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
  1191. gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
  1192. return 1;
  1193. if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
  1194. xfs_get_projid(ip) == eofb->eof_prid)
  1195. return 1;
  1196. return 0;
  1197. }
  1198. STATIC int
  1199. xfs_inode_free_eofblocks(
  1200. struct xfs_inode *ip,
  1201. int flags,
  1202. void *args)
  1203. {
  1204. int ret = 0;
  1205. struct xfs_eofblocks *eofb = args;
  1206. int match;
  1207. if (!xfs_can_free_eofblocks(ip, false)) {
  1208. /* inode could be preallocated or append-only */
  1209. trace_xfs_inode_free_eofblocks_invalid(ip);
  1210. xfs_inode_clear_eofblocks_tag(ip);
  1211. return 0;
  1212. }
  1213. /*
  1214. * If the mapping is dirty the operation can block and wait for some
  1215. * time. Unless we are waiting, skip it.
  1216. */
  1217. if (!(flags & SYNC_WAIT) &&
  1218. mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY))
  1219. return 0;
  1220. if (eofb) {
  1221. if (eofb->eof_flags & XFS_EOF_FLAGS_UNION)
  1222. match = xfs_inode_match_id_union(ip, eofb);
  1223. else
  1224. match = xfs_inode_match_id(ip, eofb);
  1225. if (!match)
  1226. return 0;
  1227. /* skip the inode if the file size is too small */
  1228. if (eofb->eof_flags & XFS_EOF_FLAGS_MINFILESIZE &&
  1229. XFS_ISIZE(ip) < eofb->eof_min_file_size)
  1230. return 0;
  1231. }
  1232. /*
  1233. * If the caller is waiting, return -EAGAIN to keep the background
  1234. * scanner moving and revisit the inode in a subsequent pass.
  1235. */
  1236. if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
  1237. if (flags & SYNC_WAIT)
  1238. ret = -EAGAIN;
  1239. return ret;
  1240. }
  1241. ret = xfs_free_eofblocks(ip);
  1242. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1243. return ret;
  1244. }
  1245. static int
  1246. __xfs_icache_free_eofblocks(
  1247. struct xfs_mount *mp,
  1248. struct xfs_eofblocks *eofb,
  1249. int (*execute)(struct xfs_inode *ip, int flags,
  1250. void *args),
  1251. int tag)
  1252. {
  1253. int flags = SYNC_TRYLOCK;
  1254. if (eofb && (eofb->eof_flags & XFS_EOF_FLAGS_SYNC))
  1255. flags = SYNC_WAIT;
  1256. return xfs_inode_ag_iterator_tag(mp, execute, flags,
  1257. eofb, tag);
  1258. }
  1259. int
  1260. xfs_icache_free_eofblocks(
  1261. struct xfs_mount *mp,
  1262. struct xfs_eofblocks *eofb)
  1263. {
  1264. return __xfs_icache_free_eofblocks(mp, eofb, xfs_inode_free_eofblocks,
  1265. XFS_ICI_EOFBLOCKS_TAG);
  1266. }
  1267. /*
  1268. * Run eofblocks scans on the quotas applicable to the inode. For inodes with
  1269. * multiple quotas, we don't know exactly which quota caused an allocation
  1270. * failure. We make a best effort by including each quota under low free space
  1271. * conditions (less than 1% free space) in the scan.
  1272. */
  1273. static int
  1274. __xfs_inode_free_quota_eofblocks(
  1275. struct xfs_inode *ip,
  1276. int (*execute)(struct xfs_mount *mp,
  1277. struct xfs_eofblocks *eofb))
  1278. {
  1279. int scan = 0;
  1280. struct xfs_eofblocks eofb = {0};
  1281. struct xfs_dquot *dq;
  1282. /*
  1283. * Run a sync scan to increase effectiveness and use the union filter to
  1284. * cover all applicable quotas in a single scan.
  1285. */
  1286. eofb.eof_flags = XFS_EOF_FLAGS_UNION|XFS_EOF_FLAGS_SYNC;
  1287. if (XFS_IS_UQUOTA_ENFORCED(ip->i_mount)) {
  1288. dq = xfs_inode_dquot(ip, XFS_DQ_USER);
  1289. if (dq && xfs_dquot_lowsp(dq)) {
  1290. eofb.eof_uid = VFS_I(ip)->i_uid;
  1291. eofb.eof_flags |= XFS_EOF_FLAGS_UID;
  1292. scan = 1;
  1293. }
  1294. }
  1295. if (XFS_IS_GQUOTA_ENFORCED(ip->i_mount)) {
  1296. dq = xfs_inode_dquot(ip, XFS_DQ_GROUP);
  1297. if (dq && xfs_dquot_lowsp(dq)) {
  1298. eofb.eof_gid = VFS_I(ip)->i_gid;
  1299. eofb.eof_flags |= XFS_EOF_FLAGS_GID;
  1300. scan = 1;
  1301. }
  1302. }
  1303. if (scan)
  1304. execute(ip->i_mount, &eofb);
  1305. return scan;
  1306. }
  1307. int
  1308. xfs_inode_free_quota_eofblocks(
  1309. struct xfs_inode *ip)
  1310. {
  1311. return __xfs_inode_free_quota_eofblocks(ip, xfs_icache_free_eofblocks);
  1312. }
  1313. static void
  1314. __xfs_inode_set_eofblocks_tag(
  1315. xfs_inode_t *ip,
  1316. void (*execute)(struct xfs_mount *mp),
  1317. void (*set_tp)(struct xfs_mount *mp, xfs_agnumber_t agno,
  1318. int error, unsigned long caller_ip),
  1319. int tag)
  1320. {
  1321. struct xfs_mount *mp = ip->i_mount;
  1322. struct xfs_perag *pag;
  1323. int tagged;
  1324. /*
  1325. * Don't bother locking the AG and looking up in the radix trees
  1326. * if we already know that we have the tag set.
  1327. */
  1328. if (ip->i_flags & XFS_IEOFBLOCKS)
  1329. return;
  1330. spin_lock(&ip->i_flags_lock);
  1331. ip->i_flags |= XFS_IEOFBLOCKS;
  1332. spin_unlock(&ip->i_flags_lock);
  1333. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
  1334. spin_lock(&pag->pag_ici_lock);
  1335. tagged = radix_tree_tagged(&pag->pag_ici_root, tag);
  1336. radix_tree_tag_set(&pag->pag_ici_root,
  1337. XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino), tag);
  1338. if (!tagged) {
  1339. /* propagate the eofblocks tag up into the perag radix tree */
  1340. spin_lock(&ip->i_mount->m_perag_lock);
  1341. radix_tree_tag_set(&ip->i_mount->m_perag_tree,
  1342. XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
  1343. tag);
  1344. spin_unlock(&ip->i_mount->m_perag_lock);
  1345. /* kick off background trimming */
  1346. execute(ip->i_mount);
  1347. set_tp(ip->i_mount, pag->pag_agno, -1, _RET_IP_);
  1348. }
  1349. spin_unlock(&pag->pag_ici_lock);
  1350. xfs_perag_put(pag);
  1351. }
  1352. void
  1353. xfs_inode_set_eofblocks_tag(
  1354. xfs_inode_t *ip)
  1355. {
  1356. trace_xfs_inode_set_eofblocks_tag(ip);
  1357. return __xfs_inode_set_eofblocks_tag(ip, xfs_queue_eofblocks,
  1358. trace_xfs_perag_set_eofblocks,
  1359. XFS_ICI_EOFBLOCKS_TAG);
  1360. }
  1361. static void
  1362. __xfs_inode_clear_eofblocks_tag(
  1363. xfs_inode_t *ip,
  1364. void (*clear_tp)(struct xfs_mount *mp, xfs_agnumber_t agno,
  1365. int error, unsigned long caller_ip),
  1366. int tag)
  1367. {
  1368. struct xfs_mount *mp = ip->i_mount;
  1369. struct xfs_perag *pag;
  1370. spin_lock(&ip->i_flags_lock);
  1371. ip->i_flags &= ~XFS_IEOFBLOCKS;
  1372. spin_unlock(&ip->i_flags_lock);
  1373. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
  1374. spin_lock(&pag->pag_ici_lock);
  1375. radix_tree_tag_clear(&pag->pag_ici_root,
  1376. XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino), tag);
  1377. if (!radix_tree_tagged(&pag->pag_ici_root, tag)) {
  1378. /* clear the eofblocks tag from the perag radix tree */
  1379. spin_lock(&ip->i_mount->m_perag_lock);
  1380. radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
  1381. XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
  1382. tag);
  1383. spin_unlock(&ip->i_mount->m_perag_lock);
  1384. clear_tp(ip->i_mount, pag->pag_agno, -1, _RET_IP_);
  1385. }
  1386. spin_unlock(&pag->pag_ici_lock);
  1387. xfs_perag_put(pag);
  1388. }
  1389. void
  1390. xfs_inode_clear_eofblocks_tag(
  1391. xfs_inode_t *ip)
  1392. {
  1393. trace_xfs_inode_clear_eofblocks_tag(ip);
  1394. return __xfs_inode_clear_eofblocks_tag(ip,
  1395. trace_xfs_perag_clear_eofblocks, XFS_ICI_EOFBLOCKS_TAG);
  1396. }
  1397. /*
  1398. * Automatic CoW Reservation Freeing
  1399. *
  1400. * These functions automatically garbage collect leftover CoW reservations
  1401. * that were made on behalf of a cowextsize hint when we start to run out
  1402. * of quota or when the reservations sit around for too long. If the file
  1403. * has dirty pages or is undergoing writeback, its CoW reservations will
  1404. * be retained.
  1405. *
  1406. * The actual garbage collection piggybacks off the same code that runs
  1407. * the speculative EOF preallocation garbage collector.
  1408. */
  1409. STATIC int
  1410. xfs_inode_free_cowblocks(
  1411. struct xfs_inode *ip,
  1412. int flags,
  1413. void *args)
  1414. {
  1415. int ret;
  1416. struct xfs_eofblocks *eofb = args;
  1417. int match;
  1418. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  1419. /*
  1420. * Just clear the tag if we have an empty cow fork or none at all. It's
  1421. * possible the inode was fully unshared since it was originally tagged.
  1422. */
  1423. if (!xfs_is_reflink_inode(ip) || !ifp->if_bytes) {
  1424. trace_xfs_inode_free_cowblocks_invalid(ip);
  1425. xfs_inode_clear_cowblocks_tag(ip);
  1426. return 0;
  1427. }
  1428. /*
  1429. * If the mapping is dirty or under writeback we cannot touch the
  1430. * CoW fork. Leave it alone if we're in the midst of a directio.
  1431. */
  1432. if ((VFS_I(ip)->i_state & I_DIRTY_PAGES) ||
  1433. mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY) ||
  1434. mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_WRITEBACK) ||
  1435. atomic_read(&VFS_I(ip)->i_dio_count))
  1436. return 0;
  1437. if (eofb) {
  1438. if (eofb->eof_flags & XFS_EOF_FLAGS_UNION)
  1439. match = xfs_inode_match_id_union(ip, eofb);
  1440. else
  1441. match = xfs_inode_match_id(ip, eofb);
  1442. if (!match)
  1443. return 0;
  1444. /* skip the inode if the file size is too small */
  1445. if (eofb->eof_flags & XFS_EOF_FLAGS_MINFILESIZE &&
  1446. XFS_ISIZE(ip) < eofb->eof_min_file_size)
  1447. return 0;
  1448. }
  1449. /* Free the CoW blocks */
  1450. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  1451. xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
  1452. ret = xfs_reflink_cancel_cow_range(ip, 0, NULLFILEOFF, false);
  1453. xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
  1454. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1455. return ret;
  1456. }
  1457. int
  1458. xfs_icache_free_cowblocks(
  1459. struct xfs_mount *mp,
  1460. struct xfs_eofblocks *eofb)
  1461. {
  1462. return __xfs_icache_free_eofblocks(mp, eofb, xfs_inode_free_cowblocks,
  1463. XFS_ICI_COWBLOCKS_TAG);
  1464. }
  1465. int
  1466. xfs_inode_free_quota_cowblocks(
  1467. struct xfs_inode *ip)
  1468. {
  1469. return __xfs_inode_free_quota_eofblocks(ip, xfs_icache_free_cowblocks);
  1470. }
  1471. void
  1472. xfs_inode_set_cowblocks_tag(
  1473. xfs_inode_t *ip)
  1474. {
  1475. trace_xfs_inode_set_cowblocks_tag(ip);
  1476. return __xfs_inode_set_eofblocks_tag(ip, xfs_queue_cowblocks,
  1477. trace_xfs_perag_set_cowblocks,
  1478. XFS_ICI_COWBLOCKS_TAG);
  1479. }
  1480. void
  1481. xfs_inode_clear_cowblocks_tag(
  1482. xfs_inode_t *ip)
  1483. {
  1484. trace_xfs_inode_clear_cowblocks_tag(ip);
  1485. return __xfs_inode_clear_eofblocks_tag(ip,
  1486. trace_xfs_perag_clear_cowblocks, XFS_ICI_COWBLOCKS_TAG);
  1487. }