xfs_icache.c 42 KB

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