xfs_zone_alloc.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2023-2025 Christoph Hellwig.
  4. * Copyright (c) 2024-2025, Western Digital Corporation or its affiliates.
  5. */
  6. #include "xfs_platform.h"
  7. #include "xfs_shared.h"
  8. #include "xfs_format.h"
  9. #include "xfs_log_format.h"
  10. #include "xfs_error.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_mount.h"
  13. #include "xfs_inode.h"
  14. #include "xfs_iomap.h"
  15. #include "xfs_trans.h"
  16. #include "xfs_alloc.h"
  17. #include "xfs_bmap.h"
  18. #include "xfs_bmap_btree.h"
  19. #include "xfs_trans_space.h"
  20. #include "xfs_refcount.h"
  21. #include "xfs_rtbitmap.h"
  22. #include "xfs_rtrmap_btree.h"
  23. #include "xfs_zone_alloc.h"
  24. #include "xfs_zone_priv.h"
  25. #include "xfs_zones.h"
  26. #include "xfs_trace.h"
  27. #include "xfs_mru_cache.h"
  28. static void
  29. xfs_open_zone_free_rcu(
  30. struct callback_head *cb)
  31. {
  32. struct xfs_open_zone *oz = container_of(cb, typeof(*oz), oz_rcu);
  33. xfs_rtgroup_rele(oz->oz_rtg);
  34. kfree(oz);
  35. }
  36. void
  37. xfs_open_zone_put(
  38. struct xfs_open_zone *oz)
  39. {
  40. if (atomic_dec_and_test(&oz->oz_ref))
  41. call_rcu(&oz->oz_rcu, xfs_open_zone_free_rcu);
  42. }
  43. static inline uint32_t
  44. xfs_zone_bucket(
  45. struct xfs_mount *mp,
  46. uint32_t used_blocks)
  47. {
  48. return XFS_ZONE_USED_BUCKETS * used_blocks /
  49. mp->m_groups[XG_TYPE_RTG].blocks;
  50. }
  51. static inline void
  52. xfs_zone_add_to_bucket(
  53. struct xfs_zone_info *zi,
  54. xfs_rgnumber_t rgno,
  55. uint32_t to_bucket)
  56. {
  57. __set_bit(rgno, zi->zi_used_bucket_bitmap[to_bucket]);
  58. zi->zi_used_bucket_entries[to_bucket]++;
  59. }
  60. static inline void
  61. xfs_zone_remove_from_bucket(
  62. struct xfs_zone_info *zi,
  63. xfs_rgnumber_t rgno,
  64. uint32_t from_bucket)
  65. {
  66. __clear_bit(rgno, zi->zi_used_bucket_bitmap[from_bucket]);
  67. zi->zi_used_bucket_entries[from_bucket]--;
  68. }
  69. static void
  70. xfs_zone_account_reclaimable(
  71. struct xfs_rtgroup *rtg,
  72. uint32_t freed)
  73. {
  74. struct xfs_group *xg = rtg_group(rtg);
  75. struct xfs_mount *mp = rtg_mount(rtg);
  76. struct xfs_zone_info *zi = mp->m_zone_info;
  77. uint32_t used = rtg_rmap(rtg)->i_used_blocks;
  78. xfs_rgnumber_t rgno = rtg_rgno(rtg);
  79. uint32_t from_bucket = xfs_zone_bucket(mp, used + freed);
  80. uint32_t to_bucket = xfs_zone_bucket(mp, used);
  81. bool was_full = (used + freed == rtg_blocks(rtg));
  82. /*
  83. * This can be called from log recovery, where the zone_info structure
  84. * hasn't been allocated yet. Skip all work as xfs_mount_zones will
  85. * add the zones to the right buckets before the file systems becomes
  86. * active.
  87. */
  88. if (!zi)
  89. return;
  90. if (!used) {
  91. /*
  92. * The zone is now empty, remove it from the bottom bucket and
  93. * trigger a reset.
  94. */
  95. trace_xfs_zone_emptied(rtg);
  96. spin_lock(&zi->zi_used_buckets_lock);
  97. if (!was_full)
  98. xfs_zone_remove_from_bucket(zi, rgno, from_bucket);
  99. spin_unlock(&zi->zi_used_buckets_lock);
  100. spin_lock(&zi->zi_reset_list_lock);
  101. xg->xg_next_reset = zi->zi_reset_list;
  102. zi->zi_reset_list = xg;
  103. spin_unlock(&zi->zi_reset_list_lock);
  104. if (zi->zi_gc_thread)
  105. wake_up_process(zi->zi_gc_thread);
  106. } else if (was_full) {
  107. /*
  108. * The zone transitioned from full, mark it up as reclaimable
  109. * and wake up GC which might be waiting for zones to reclaim.
  110. */
  111. spin_lock(&zi->zi_used_buckets_lock);
  112. xfs_zone_add_to_bucket(zi, rgno, to_bucket);
  113. spin_unlock(&zi->zi_used_buckets_lock);
  114. if (zi->zi_gc_thread && xfs_zoned_need_gc(mp))
  115. wake_up_process(zi->zi_gc_thread);
  116. } else if (to_bucket != from_bucket) {
  117. /*
  118. * Move the zone to a new bucket if it dropped below the
  119. * threshold.
  120. */
  121. spin_lock(&zi->zi_used_buckets_lock);
  122. xfs_zone_add_to_bucket(zi, rgno, to_bucket);
  123. xfs_zone_remove_from_bucket(zi, rgno, from_bucket);
  124. spin_unlock(&zi->zi_used_buckets_lock);
  125. }
  126. }
  127. /*
  128. * Check if we have any zones that can be reclaimed by looking at the entry
  129. * counters for the zone buckets.
  130. */
  131. bool
  132. xfs_zoned_have_reclaimable(
  133. struct xfs_zone_info *zi)
  134. {
  135. int i;
  136. spin_lock(&zi->zi_used_buckets_lock);
  137. for (i = 0; i < XFS_ZONE_USED_BUCKETS; i++) {
  138. if (zi->zi_used_bucket_entries[i]) {
  139. spin_unlock(&zi->zi_used_buckets_lock);
  140. return true;
  141. }
  142. }
  143. spin_unlock(&zi->zi_used_buckets_lock);
  144. return false;
  145. }
  146. static void
  147. xfs_open_zone_mark_full(
  148. struct xfs_open_zone *oz)
  149. {
  150. struct xfs_rtgroup *rtg = oz->oz_rtg;
  151. struct xfs_mount *mp = rtg_mount(rtg);
  152. struct xfs_zone_info *zi = mp->m_zone_info;
  153. uint32_t used = rtg_rmap(rtg)->i_used_blocks;
  154. trace_xfs_zone_full(rtg);
  155. WRITE_ONCE(rtg->rtg_open_zone, NULL);
  156. spin_lock(&zi->zi_open_zones_lock);
  157. if (oz->oz_is_gc) {
  158. ASSERT(current == zi->zi_gc_thread);
  159. zi->zi_open_gc_zone = NULL;
  160. } else {
  161. zi->zi_nr_open_zones--;
  162. list_del_init(&oz->oz_entry);
  163. }
  164. spin_unlock(&zi->zi_open_zones_lock);
  165. xfs_open_zone_put(oz);
  166. wake_up_all(&zi->zi_zone_wait);
  167. if (used < rtg_blocks(rtg))
  168. xfs_zone_account_reclaimable(rtg, rtg_blocks(rtg) - used);
  169. }
  170. static void
  171. xfs_zone_record_blocks(
  172. struct xfs_trans *tp,
  173. struct xfs_open_zone *oz,
  174. xfs_fsblock_t fsbno,
  175. xfs_filblks_t len)
  176. {
  177. struct xfs_mount *mp = tp->t_mountp;
  178. struct xfs_rtgroup *rtg = oz->oz_rtg;
  179. struct xfs_inode *rmapip = rtg_rmap(rtg);
  180. trace_xfs_zone_record_blocks(oz, xfs_rtb_to_rgbno(mp, fsbno), len);
  181. xfs_rtgroup_lock(rtg, XFS_RTGLOCK_RMAP);
  182. xfs_rtgroup_trans_join(tp, rtg, XFS_RTGLOCK_RMAP);
  183. rmapip->i_used_blocks += len;
  184. ASSERT(rmapip->i_used_blocks <= rtg_blocks(rtg));
  185. oz->oz_written += len;
  186. if (oz->oz_written == rtg_blocks(rtg))
  187. xfs_open_zone_mark_full(oz);
  188. xfs_trans_log_inode(tp, rmapip, XFS_ILOG_CORE);
  189. }
  190. /*
  191. * Called for blocks that have been written to disk, but not actually linked to
  192. * an inode, which can happen when garbage collection races with user data
  193. * writes to a file.
  194. */
  195. static void
  196. xfs_zone_skip_blocks(
  197. struct xfs_open_zone *oz,
  198. xfs_filblks_t len)
  199. {
  200. struct xfs_rtgroup *rtg = oz->oz_rtg;
  201. trace_xfs_zone_skip_blocks(oz, 0, len);
  202. xfs_rtgroup_lock(rtg, XFS_RTGLOCK_RMAP);
  203. oz->oz_written += len;
  204. if (oz->oz_written == rtg_blocks(rtg))
  205. xfs_open_zone_mark_full(oz);
  206. xfs_rtgroup_unlock(rtg, XFS_RTGLOCK_RMAP);
  207. xfs_add_frextents(rtg_mount(rtg), len);
  208. }
  209. static int
  210. xfs_zoned_map_extent(
  211. struct xfs_trans *tp,
  212. struct xfs_inode *ip,
  213. struct xfs_bmbt_irec *new,
  214. struct xfs_open_zone *oz,
  215. xfs_fsblock_t old_startblock)
  216. {
  217. struct xfs_bmbt_irec data;
  218. int nmaps = 1;
  219. int error;
  220. /* Grab the corresponding mapping in the data fork. */
  221. error = xfs_bmapi_read(ip, new->br_startoff, new->br_blockcount, &data,
  222. &nmaps, 0);
  223. if (error)
  224. return error;
  225. /*
  226. * Cap the update to the existing extent in the data fork because we can
  227. * only overwrite one extent at a time.
  228. */
  229. ASSERT(new->br_blockcount >= data.br_blockcount);
  230. new->br_blockcount = data.br_blockcount;
  231. /*
  232. * If a data write raced with this GC write, keep the existing data in
  233. * the data fork, mark our newly written GC extent as reclaimable, then
  234. * move on to the next extent.
  235. *
  236. * Note that this can also happen when racing with operations that do
  237. * not actually invalidate the data, but just move it to a different
  238. * inode (XFS_IOC_EXCHANGE_RANGE), or to a different offset inside the
  239. * inode (FALLOC_FL_COLLAPSE_RANGE / FALLOC_FL_INSERT_RANGE). If the
  240. * data was just moved around, GC fails to free the zone, but the zone
  241. * becomes a GC candidate again as soon as all previous GC I/O has
  242. * finished and these blocks will be moved out eventually.
  243. */
  244. if (old_startblock != NULLFSBLOCK &&
  245. old_startblock != data.br_startblock)
  246. goto skip;
  247. trace_xfs_reflink_cow_remap_from(ip, new);
  248. trace_xfs_reflink_cow_remap_to(ip, &data);
  249. error = xfs_iext_count_extend(tp, ip, XFS_DATA_FORK,
  250. XFS_IEXT_REFLINK_END_COW_CNT);
  251. if (error)
  252. return error;
  253. if (data.br_startblock != HOLESTARTBLOCK) {
  254. ASSERT(data.br_startblock != DELAYSTARTBLOCK);
  255. ASSERT(!isnullstartblock(data.br_startblock));
  256. xfs_bmap_unmap_extent(tp, ip, XFS_DATA_FORK, &data);
  257. if (xfs_is_reflink_inode(ip)) {
  258. xfs_refcount_decrease_extent(tp, true, &data);
  259. } else {
  260. error = xfs_free_extent_later(tp, data.br_startblock,
  261. data.br_blockcount, NULL,
  262. XFS_AG_RESV_NONE,
  263. XFS_FREE_EXTENT_REALTIME);
  264. if (error)
  265. return error;
  266. }
  267. }
  268. xfs_zone_record_blocks(tp, oz, new->br_startblock, new->br_blockcount);
  269. /* Map the new blocks into the data fork. */
  270. xfs_bmap_map_extent(tp, ip, XFS_DATA_FORK, new);
  271. return 0;
  272. skip:
  273. trace_xfs_reflink_cow_remap_skip(ip, new);
  274. xfs_zone_skip_blocks(oz, new->br_blockcount);
  275. return 0;
  276. }
  277. int
  278. xfs_zoned_end_io(
  279. struct xfs_inode *ip,
  280. xfs_off_t offset,
  281. xfs_off_t count,
  282. xfs_daddr_t daddr,
  283. struct xfs_open_zone *oz,
  284. xfs_fsblock_t old_startblock)
  285. {
  286. struct xfs_mount *mp = ip->i_mount;
  287. xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count);
  288. struct xfs_bmbt_irec new = {
  289. .br_startoff = XFS_B_TO_FSBT(mp, offset),
  290. .br_startblock = xfs_daddr_to_rtb(mp, daddr),
  291. .br_state = XFS_EXT_NORM,
  292. };
  293. unsigned int resblks =
  294. XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
  295. struct xfs_trans *tp;
  296. int error;
  297. if (xfs_is_shutdown(mp))
  298. return -EIO;
  299. while (new.br_startoff < end_fsb) {
  300. new.br_blockcount = end_fsb - new.br_startoff;
  301. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0,
  302. XFS_TRANS_RESERVE | XFS_TRANS_RES_FDBLKS, &tp);
  303. if (error)
  304. return error;
  305. xfs_ilock(ip, XFS_ILOCK_EXCL);
  306. xfs_trans_ijoin(tp, ip, 0);
  307. error = xfs_zoned_map_extent(tp, ip, &new, oz, old_startblock);
  308. if (error)
  309. xfs_trans_cancel(tp);
  310. else
  311. error = xfs_trans_commit(tp);
  312. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  313. if (error)
  314. return error;
  315. new.br_startoff += new.br_blockcount;
  316. new.br_startblock += new.br_blockcount;
  317. if (old_startblock != NULLFSBLOCK)
  318. old_startblock += new.br_blockcount;
  319. }
  320. return 0;
  321. }
  322. /*
  323. * "Free" blocks allocated in a zone.
  324. *
  325. * Just decrement the used blocks counter and report the space as freed.
  326. */
  327. int
  328. xfs_zone_free_blocks(
  329. struct xfs_trans *tp,
  330. struct xfs_rtgroup *rtg,
  331. xfs_fsblock_t fsbno,
  332. xfs_filblks_t len)
  333. {
  334. struct xfs_mount *mp = tp->t_mountp;
  335. struct xfs_inode *rmapip = rtg_rmap(rtg);
  336. xfs_assert_ilocked(rmapip, XFS_ILOCK_EXCL);
  337. if (len > rmapip->i_used_blocks) {
  338. xfs_err(mp,
  339. "trying to free more blocks (%lld) than used counter (%u).",
  340. len, rmapip->i_used_blocks);
  341. ASSERT(len <= rmapip->i_used_blocks);
  342. xfs_rtginode_mark_sick(rtg, XFS_RTGI_RMAP);
  343. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  344. return -EFSCORRUPTED;
  345. }
  346. trace_xfs_zone_free_blocks(rtg, xfs_rtb_to_rgbno(mp, fsbno), len);
  347. rmapip->i_used_blocks -= len;
  348. /*
  349. * Don't add open zones to the reclaimable buckets. The I/O completion
  350. * for writing the last block will take care of accounting for already
  351. * unused blocks instead.
  352. */
  353. if (!READ_ONCE(rtg->rtg_open_zone))
  354. xfs_zone_account_reclaimable(rtg, len);
  355. xfs_add_frextents(mp, len);
  356. xfs_trans_log_inode(tp, rmapip, XFS_ILOG_CORE);
  357. return 0;
  358. }
  359. static struct xfs_open_zone *
  360. xfs_init_open_zone(
  361. struct xfs_rtgroup *rtg,
  362. xfs_rgblock_t write_pointer,
  363. enum rw_hint write_hint,
  364. bool is_gc)
  365. {
  366. struct xfs_open_zone *oz;
  367. oz = kzalloc_obj(*oz, GFP_NOFS | __GFP_NOFAIL);
  368. spin_lock_init(&oz->oz_alloc_lock);
  369. atomic_set(&oz->oz_ref, 1);
  370. oz->oz_rtg = rtg;
  371. oz->oz_allocated = write_pointer;
  372. oz->oz_written = write_pointer;
  373. oz->oz_write_hint = write_hint;
  374. oz->oz_is_gc = is_gc;
  375. /*
  376. * All dereferences of rtg->rtg_open_zone hold the ILOCK for the rmap
  377. * inode, but we don't really want to take that here because we are
  378. * under the zone_list_lock. Ensure the pointer is only set for a fully
  379. * initialized open zone structure so that a racy lookup finding it is
  380. * fine.
  381. */
  382. WRITE_ONCE(rtg->rtg_open_zone, oz);
  383. return oz;
  384. }
  385. /*
  386. * Find a completely free zone, open it, and return a reference.
  387. */
  388. struct xfs_open_zone *
  389. xfs_open_zone(
  390. struct xfs_mount *mp,
  391. enum rw_hint write_hint,
  392. bool is_gc)
  393. {
  394. struct xfs_zone_info *zi = mp->m_zone_info;
  395. XA_STATE (xas, &mp->m_groups[XG_TYPE_RTG].xa, 0);
  396. struct xfs_group *xg;
  397. /*
  398. * Pick the free zone with lowest index. Zones in the beginning of the
  399. * address space typically provides higher bandwidth than those at the
  400. * end of the address space on HDDs.
  401. */
  402. xas_lock(&xas);
  403. xas_for_each_marked(&xas, xg, ULONG_MAX, XFS_RTG_FREE)
  404. if (atomic_inc_not_zero(&xg->xg_active_ref))
  405. goto found;
  406. xas_unlock(&xas);
  407. return NULL;
  408. found:
  409. xas_clear_mark(&xas, XFS_RTG_FREE);
  410. atomic_dec(&zi->zi_nr_free_zones);
  411. xas_unlock(&xas);
  412. set_current_state(TASK_RUNNING);
  413. return xfs_init_open_zone(to_rtg(xg), 0, write_hint, is_gc);
  414. }
  415. static struct xfs_open_zone *
  416. xfs_try_open_zone(
  417. struct xfs_mount *mp,
  418. enum rw_hint write_hint)
  419. {
  420. struct xfs_zone_info *zi = mp->m_zone_info;
  421. struct xfs_open_zone *oz;
  422. if (zi->zi_nr_open_zones >= mp->m_max_open_zones - XFS_OPEN_GC_ZONES)
  423. return NULL;
  424. if (atomic_read(&zi->zi_nr_free_zones) <
  425. XFS_GC_ZONES - XFS_OPEN_GC_ZONES)
  426. return NULL;
  427. /*
  428. * Increment the open zone count to reserve our slot before dropping
  429. * zi_open_zones_lock.
  430. */
  431. zi->zi_nr_open_zones++;
  432. spin_unlock(&zi->zi_open_zones_lock);
  433. oz = xfs_open_zone(mp, write_hint, false);
  434. spin_lock(&zi->zi_open_zones_lock);
  435. if (!oz) {
  436. zi->zi_nr_open_zones--;
  437. return NULL;
  438. }
  439. atomic_inc(&oz->oz_ref);
  440. list_add_tail(&oz->oz_entry, &zi->zi_open_zones);
  441. /*
  442. * If this was the last free zone, other waiters might be waiting
  443. * on us to write to it as well.
  444. */
  445. wake_up_all(&zi->zi_zone_wait);
  446. if (xfs_zoned_need_gc(mp))
  447. wake_up_process(zi->zi_gc_thread);
  448. trace_xfs_zone_opened(oz->oz_rtg);
  449. return oz;
  450. }
  451. enum xfs_zone_alloc_score {
  452. /* Any open zone will do it, we're desperate */
  453. XFS_ZONE_ALLOC_ANY = 0,
  454. /* It better fit somehow */
  455. XFS_ZONE_ALLOC_OK = 1,
  456. /* Only reuse a zone if it fits really well. */
  457. XFS_ZONE_ALLOC_GOOD = 2,
  458. };
  459. /*
  460. * Life time hint co-location matrix. Fields not set default to 0
  461. * aka XFS_ZONE_ALLOC_ANY.
  462. */
  463. static const unsigned int
  464. xfs_zoned_hint_score[WRITE_LIFE_HINT_NR][WRITE_LIFE_HINT_NR] = {
  465. [WRITE_LIFE_NOT_SET] = {
  466. [WRITE_LIFE_NOT_SET] = XFS_ZONE_ALLOC_OK,
  467. },
  468. [WRITE_LIFE_NONE] = {
  469. [WRITE_LIFE_NONE] = XFS_ZONE_ALLOC_OK,
  470. },
  471. [WRITE_LIFE_SHORT] = {
  472. [WRITE_LIFE_SHORT] = XFS_ZONE_ALLOC_GOOD,
  473. },
  474. [WRITE_LIFE_MEDIUM] = {
  475. [WRITE_LIFE_MEDIUM] = XFS_ZONE_ALLOC_GOOD,
  476. },
  477. [WRITE_LIFE_LONG] = {
  478. [WRITE_LIFE_LONG] = XFS_ZONE_ALLOC_OK,
  479. [WRITE_LIFE_EXTREME] = XFS_ZONE_ALLOC_OK,
  480. },
  481. [WRITE_LIFE_EXTREME] = {
  482. [WRITE_LIFE_LONG] = XFS_ZONE_ALLOC_OK,
  483. [WRITE_LIFE_EXTREME] = XFS_ZONE_ALLOC_OK,
  484. },
  485. };
  486. static bool
  487. xfs_try_use_zone(
  488. struct xfs_zone_info *zi,
  489. enum rw_hint file_hint,
  490. struct xfs_open_zone *oz,
  491. unsigned int goodness)
  492. {
  493. if (oz->oz_allocated == rtg_blocks(oz->oz_rtg))
  494. return false;
  495. if (xfs_zoned_hint_score[oz->oz_write_hint][file_hint] < goodness)
  496. return false;
  497. if (!atomic_inc_not_zero(&oz->oz_ref))
  498. return false;
  499. /*
  500. * If we have a hint set for the data, use that for the zone even if
  501. * some data was written already without any hint set, but don't change
  502. * the temperature after that as that would make little sense without
  503. * tracking per-temperature class written block counts, which is
  504. * probably overkill anyway.
  505. */
  506. if (file_hint != WRITE_LIFE_NOT_SET &&
  507. oz->oz_write_hint == WRITE_LIFE_NOT_SET)
  508. oz->oz_write_hint = file_hint;
  509. /*
  510. * If we couldn't match by inode or life time we just pick the first
  511. * zone with enough space above. For that we want the least busy zone
  512. * for some definition of "least" busy. For now this simple LRU
  513. * algorithm that rotates every zone to the end of the list will do it,
  514. * even if it isn't exactly cache friendly.
  515. */
  516. if (!list_is_last(&oz->oz_entry, &zi->zi_open_zones))
  517. list_move_tail(&oz->oz_entry, &zi->zi_open_zones);
  518. return true;
  519. }
  520. static struct xfs_open_zone *
  521. xfs_select_open_zone_lru(
  522. struct xfs_zone_info *zi,
  523. enum rw_hint file_hint,
  524. unsigned int goodness)
  525. {
  526. struct xfs_open_zone *oz;
  527. lockdep_assert_held(&zi->zi_open_zones_lock);
  528. list_for_each_entry(oz, &zi->zi_open_zones, oz_entry)
  529. if (xfs_try_use_zone(zi, file_hint, oz, goodness))
  530. return oz;
  531. cond_resched_lock(&zi->zi_open_zones_lock);
  532. return NULL;
  533. }
  534. static struct xfs_open_zone *
  535. xfs_select_open_zone_mru(
  536. struct xfs_zone_info *zi,
  537. enum rw_hint file_hint)
  538. {
  539. struct xfs_open_zone *oz;
  540. lockdep_assert_held(&zi->zi_open_zones_lock);
  541. list_for_each_entry_reverse(oz, &zi->zi_open_zones, oz_entry)
  542. if (xfs_try_use_zone(zi, file_hint, oz, XFS_ZONE_ALLOC_OK))
  543. return oz;
  544. cond_resched_lock(&zi->zi_open_zones_lock);
  545. return NULL;
  546. }
  547. static inline enum rw_hint xfs_inode_write_hint(struct xfs_inode *ip)
  548. {
  549. if (xfs_has_nolifetime(ip->i_mount))
  550. return WRITE_LIFE_NOT_SET;
  551. return VFS_I(ip)->i_write_hint;
  552. }
  553. /*
  554. * Try to tightly pack small files that are written back after they were closed
  555. * instead of trying to open new zones for them or spread them to the least
  556. * recently used zone. This optimizes the data layout for workloads that untar
  557. * or copy a lot of small files. Right now this does not separate multiple such
  558. * streams.
  559. */
  560. static inline bool xfs_zoned_pack_tight(struct xfs_inode *ip)
  561. {
  562. struct xfs_mount *mp = ip->i_mount;
  563. size_t zone_capacity =
  564. XFS_FSB_TO_B(mp, mp->m_groups[XG_TYPE_RTG].blocks);
  565. /*
  566. * Do not pack write files that are already using a full zone to avoid
  567. * fragmentation.
  568. */
  569. if (i_size_read(VFS_I(ip)) >= zone_capacity)
  570. return false;
  571. return !inode_is_open_for_write(VFS_I(ip)) &&
  572. !(ip->i_diflags & XFS_DIFLAG_APPEND);
  573. }
  574. static struct xfs_open_zone *
  575. xfs_select_zone_nowait(
  576. struct xfs_mount *mp,
  577. enum rw_hint write_hint,
  578. bool pack_tight)
  579. {
  580. struct xfs_zone_info *zi = mp->m_zone_info;
  581. struct xfs_open_zone *oz = NULL;
  582. if (xfs_is_shutdown(mp))
  583. return NULL;
  584. /*
  585. * Try to fill up open zones with matching temperature if available. It
  586. * is better to try to co-locate data when this is favorable, so we can
  587. * activate empty zones when it is statistically better to separate
  588. * data.
  589. */
  590. spin_lock(&zi->zi_open_zones_lock);
  591. oz = xfs_select_open_zone_lru(zi, write_hint, XFS_ZONE_ALLOC_GOOD);
  592. if (oz)
  593. goto out_unlock;
  594. if (pack_tight)
  595. oz = xfs_select_open_zone_mru(zi, write_hint);
  596. if (oz)
  597. goto out_unlock;
  598. /*
  599. * See if we can open a new zone and use that so that data for different
  600. * files is mixed as little as possible.
  601. */
  602. oz = xfs_try_open_zone(mp, write_hint);
  603. if (oz)
  604. goto out_unlock;
  605. /*
  606. * Try to find an zone that is an ok match to colocate data with.
  607. */
  608. oz = xfs_select_open_zone_lru(zi, write_hint, XFS_ZONE_ALLOC_OK);
  609. if (oz)
  610. goto out_unlock;
  611. /*
  612. * Pick the least recently used zone, regardless of hint match
  613. */
  614. oz = xfs_select_open_zone_lru(zi, write_hint, XFS_ZONE_ALLOC_ANY);
  615. out_unlock:
  616. spin_unlock(&zi->zi_open_zones_lock);
  617. return oz;
  618. }
  619. static struct xfs_open_zone *
  620. xfs_select_zone(
  621. struct xfs_mount *mp,
  622. enum rw_hint write_hint,
  623. bool pack_tight)
  624. {
  625. struct xfs_zone_info *zi = mp->m_zone_info;
  626. DEFINE_WAIT (wait);
  627. struct xfs_open_zone *oz;
  628. oz = xfs_select_zone_nowait(mp, write_hint, pack_tight);
  629. if (oz)
  630. return oz;
  631. for (;;) {
  632. prepare_to_wait(&zi->zi_zone_wait, &wait, TASK_UNINTERRUPTIBLE);
  633. oz = xfs_select_zone_nowait(mp, write_hint, pack_tight);
  634. if (oz || xfs_is_shutdown(mp))
  635. break;
  636. schedule();
  637. }
  638. finish_wait(&zi->zi_zone_wait, &wait);
  639. return oz;
  640. }
  641. static unsigned int
  642. xfs_zone_alloc_blocks(
  643. struct xfs_open_zone *oz,
  644. xfs_filblks_t count_fsb,
  645. sector_t *sector,
  646. bool *is_seq)
  647. {
  648. struct xfs_rtgroup *rtg = oz->oz_rtg;
  649. struct xfs_mount *mp = rtg_mount(rtg);
  650. xfs_rgblock_t allocated;
  651. spin_lock(&oz->oz_alloc_lock);
  652. count_fsb = min3(count_fsb, XFS_MAX_BMBT_EXTLEN,
  653. (xfs_filblks_t)rtg_blocks(rtg) - oz->oz_allocated);
  654. if (!count_fsb) {
  655. spin_unlock(&oz->oz_alloc_lock);
  656. return 0;
  657. }
  658. allocated = oz->oz_allocated;
  659. oz->oz_allocated += count_fsb;
  660. spin_unlock(&oz->oz_alloc_lock);
  661. trace_xfs_zone_alloc_blocks(oz, allocated, count_fsb);
  662. *sector = xfs_gbno_to_daddr(rtg_group(rtg), 0);
  663. *is_seq = bdev_zone_is_seq(mp->m_rtdev_targp->bt_bdev, *sector);
  664. if (!*is_seq)
  665. *sector += XFS_FSB_TO_BB(mp, allocated);
  666. return XFS_FSB_TO_B(mp, count_fsb);
  667. }
  668. void
  669. xfs_mark_rtg_boundary(
  670. struct iomap_ioend *ioend)
  671. {
  672. struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount;
  673. sector_t sector = ioend->io_bio.bi_iter.bi_sector;
  674. if (xfs_rtb_to_rgbno(mp, xfs_daddr_to_rtb(mp, sector)) == 0)
  675. ioend->io_flags |= IOMAP_IOEND_BOUNDARY;
  676. }
  677. /*
  678. * Check if we have a cached last open zone available for the inode and
  679. * if yes return a reference to it.
  680. */
  681. static struct xfs_open_zone *
  682. xfs_get_cached_zone(
  683. struct xfs_inode *ip)
  684. {
  685. struct xfs_open_zone *oz;
  686. rcu_read_lock();
  687. oz = VFS_I(ip)->i_private;
  688. if (oz) {
  689. /*
  690. * GC only steals open zones at mount time, so no GC zones
  691. * should end up in the cache.
  692. */
  693. ASSERT(!oz->oz_is_gc);
  694. if (!atomic_inc_not_zero(&oz->oz_ref))
  695. oz = NULL;
  696. }
  697. rcu_read_unlock();
  698. return oz;
  699. }
  700. /*
  701. * Stash our zone in the inode so that is is reused for future allocations.
  702. *
  703. * The open_zone structure will be pinned until either the inode is freed or
  704. * until the cached open zone is replaced with a different one because the
  705. * current one was full when we tried to use it. This means we keep any
  706. * open zone around forever as long as any inode that used it for the last
  707. * write is cached, which slightly increases the memory use of cached inodes
  708. * that were every written to, but significantly simplifies the cached zone
  709. * lookup. Because the open_zone is clearly marked as full when all data
  710. * in the underlying RTG was written, the caching is always safe.
  711. */
  712. static void
  713. xfs_set_cached_zone(
  714. struct xfs_inode *ip,
  715. struct xfs_open_zone *oz)
  716. {
  717. struct xfs_open_zone *old_oz;
  718. atomic_inc(&oz->oz_ref);
  719. old_oz = xchg(&VFS_I(ip)->i_private, oz);
  720. if (old_oz)
  721. xfs_open_zone_put(old_oz);
  722. }
  723. static void
  724. xfs_submit_zoned_bio(
  725. struct iomap_ioend *ioend,
  726. struct xfs_open_zone *oz,
  727. bool is_seq)
  728. {
  729. ioend->io_bio.bi_iter.bi_sector = ioend->io_sector;
  730. ioend->io_private = oz;
  731. atomic_inc(&oz->oz_ref); /* for xfs_zoned_end_io */
  732. if (is_seq) {
  733. ioend->io_bio.bi_opf &= ~REQ_OP_WRITE;
  734. ioend->io_bio.bi_opf |= REQ_OP_ZONE_APPEND;
  735. } else {
  736. xfs_mark_rtg_boundary(ioend);
  737. }
  738. submit_bio(&ioend->io_bio);
  739. }
  740. void
  741. xfs_zone_alloc_and_submit(
  742. struct iomap_ioend *ioend,
  743. struct xfs_open_zone **oz)
  744. {
  745. struct xfs_inode *ip = XFS_I(ioend->io_inode);
  746. struct xfs_mount *mp = ip->i_mount;
  747. enum rw_hint write_hint = xfs_inode_write_hint(ip);
  748. bool pack_tight = xfs_zoned_pack_tight(ip);
  749. unsigned int alloc_len;
  750. struct iomap_ioend *split;
  751. bool is_seq;
  752. if (xfs_is_shutdown(mp))
  753. goto out_error;
  754. /*
  755. * If we don't have a locally cached zone in this write context, see if
  756. * the inode is still associated with a zone and use that if so.
  757. */
  758. if (!*oz)
  759. *oz = xfs_get_cached_zone(ip);
  760. if (!*oz) {
  761. select_zone:
  762. *oz = xfs_select_zone(mp, write_hint, pack_tight);
  763. if (!*oz)
  764. goto out_error;
  765. xfs_set_cached_zone(ip, *oz);
  766. }
  767. alloc_len = xfs_zone_alloc_blocks(*oz, XFS_B_TO_FSB(mp, ioend->io_size),
  768. &ioend->io_sector, &is_seq);
  769. if (!alloc_len) {
  770. xfs_open_zone_put(*oz);
  771. goto select_zone;
  772. }
  773. while ((split = iomap_split_ioend(ioend, alloc_len, is_seq))) {
  774. if (IS_ERR(split))
  775. goto out_split_error;
  776. alloc_len -= split->io_bio.bi_iter.bi_size;
  777. xfs_submit_zoned_bio(split, *oz, is_seq);
  778. if (!alloc_len) {
  779. xfs_open_zone_put(*oz);
  780. goto select_zone;
  781. }
  782. }
  783. xfs_submit_zoned_bio(ioend, *oz, is_seq);
  784. return;
  785. out_split_error:
  786. ioend->io_bio.bi_status = errno_to_blk_status(PTR_ERR(split));
  787. out_error:
  788. bio_io_error(&ioend->io_bio);
  789. }
  790. /*
  791. * Wake up all threads waiting for a zoned space allocation when the file system
  792. * is shut down.
  793. */
  794. void
  795. xfs_zoned_wake_all(
  796. struct xfs_mount *mp)
  797. {
  798. /*
  799. * Don't wake up if there is no m_zone_info. This is complicated by the
  800. * fact that unmount can't atomically clear m_zone_info and thus we need
  801. * to check SB_ACTIVE for that, but mount temporarily enables SB_ACTIVE
  802. * during log recovery so we can't entirely rely on that either.
  803. */
  804. if ((mp->m_super->s_flags & SB_ACTIVE) && mp->m_zone_info)
  805. wake_up_all(&mp->m_zone_info->zi_zone_wait);
  806. }
  807. /*
  808. * Check if @rgbno in @rgb is a potentially valid block. It might still be
  809. * unused, but that information is only found in the rmap.
  810. */
  811. bool
  812. xfs_zone_rgbno_is_valid(
  813. struct xfs_rtgroup *rtg,
  814. xfs_rgnumber_t rgbno)
  815. {
  816. lockdep_assert_held(&rtg_rmap(rtg)->i_lock);
  817. if (rtg->rtg_open_zone)
  818. return rgbno < rtg->rtg_open_zone->oz_allocated;
  819. return !xa_get_mark(&rtg_mount(rtg)->m_groups[XG_TYPE_RTG].xa,
  820. rtg_rgno(rtg), XFS_RTG_FREE);
  821. }
  822. static void
  823. xfs_free_open_zones(
  824. struct xfs_zone_info *zi)
  825. {
  826. struct xfs_open_zone *oz;
  827. spin_lock(&zi->zi_open_zones_lock);
  828. while ((oz = list_first_entry_or_null(&zi->zi_open_zones,
  829. struct xfs_open_zone, oz_entry))) {
  830. list_del(&oz->oz_entry);
  831. xfs_open_zone_put(oz);
  832. }
  833. spin_unlock(&zi->zi_open_zones_lock);
  834. /*
  835. * Wait for all open zones to be freed so that they drop the group
  836. * references:
  837. */
  838. rcu_barrier();
  839. }
  840. struct xfs_init_zones {
  841. uint32_t zone_size;
  842. uint32_t zone_capacity;
  843. uint64_t available;
  844. uint64_t reclaimable;
  845. };
  846. /*
  847. * For sequential write required zones, we restart writing at the hardware write
  848. * pointer returned by xfs_validate_blk_zone().
  849. *
  850. * For conventional zones or conventional devices we have to query the rmap to
  851. * find the highest recorded block and set the write pointer to the block after
  852. * that. In case of a power loss this misses blocks where the data I/O has
  853. * completed but not recorded in the rmap yet, and it also rewrites blocks if
  854. * the most recently written ones got deleted again before unmount, but this is
  855. * the best we can do without hardware support.
  856. */
  857. static int
  858. xfs_query_write_pointer(
  859. struct xfs_init_zones *iz,
  860. struct xfs_rtgroup *rtg,
  861. xfs_rgblock_t *write_pointer)
  862. {
  863. struct xfs_mount *mp = rtg_mount(rtg);
  864. struct block_device *bdev = mp->m_rtdev_targp->bt_bdev;
  865. sector_t start = xfs_gbno_to_daddr(&rtg->rtg_group, 0);
  866. xfs_rgblock_t highest_rgbno;
  867. struct blk_zone zone = {};
  868. int error;
  869. if (bdev_is_zoned(bdev)) {
  870. error = blkdev_get_zone_info(bdev, start, &zone);
  871. if (error)
  872. return error;
  873. if (zone.start != start) {
  874. xfs_warn(mp, "mismatched zone start: 0x%llx/0x%llx.",
  875. zone.start, start);
  876. return -EFSCORRUPTED;
  877. }
  878. if (!xfs_validate_blk_zone(mp, &zone, rtg_rgno(rtg),
  879. iz->zone_size, iz->zone_capacity,
  880. write_pointer))
  881. return -EFSCORRUPTED;
  882. /*
  883. * Use the hardware write pointer returned by
  884. * xfs_validate_blk_zone for sequential write required zones,
  885. * else fall through to the rmap-based estimation below.
  886. */
  887. if (zone.cond != BLK_ZONE_COND_NOT_WP)
  888. return 0;
  889. }
  890. xfs_rtgroup_lock(rtg, XFS_RTGLOCK_RMAP);
  891. highest_rgbno = xfs_rtrmap_highest_rgbno(rtg);
  892. xfs_rtgroup_unlock(rtg, XFS_RTGLOCK_RMAP);
  893. if (highest_rgbno == NULLRGBLOCK)
  894. *write_pointer = 0;
  895. else
  896. *write_pointer = highest_rgbno + 1;
  897. return 0;
  898. }
  899. static int
  900. xfs_init_zone(
  901. struct xfs_init_zones *iz,
  902. struct xfs_rtgroup *rtg,
  903. xfs_rgblock_t write_pointer)
  904. {
  905. struct xfs_mount *mp = rtg_mount(rtg);
  906. struct xfs_zone_info *zi = mp->m_zone_info;
  907. uint32_t used = rtg_rmap(rtg)->i_used_blocks;
  908. int error;
  909. if (write_pointer > rtg->rtg_extents) {
  910. xfs_warn(mp, "zone %u has invalid write pointer (0x%x).",
  911. rtg_rgno(rtg), write_pointer);
  912. return -EFSCORRUPTED;
  913. }
  914. if (used > rtg->rtg_extents) {
  915. xfs_warn(mp,
  916. "zone %u has used counter (0x%x) larger than zone capacity (0x%llx).",
  917. rtg_rgno(rtg), used, rtg->rtg_extents);
  918. return -EFSCORRUPTED;
  919. }
  920. if (used > write_pointer) {
  921. xfs_warn(mp,
  922. "zone %u has used counter (0x%x) larger than write pointer (0x%x).",
  923. rtg_rgno(rtg), used, write_pointer);
  924. return -EFSCORRUPTED;
  925. }
  926. if (write_pointer == 0 && used != 0) {
  927. xfs_warn(mp, "empty zone %u has non-zero used counter (0x%x).",
  928. rtg_rgno(rtg), used);
  929. return -EFSCORRUPTED;
  930. }
  931. /*
  932. * If there are no used blocks, but the zone is not in empty state yet
  933. * we lost power before the zoned reset. In that case finish the work
  934. * here.
  935. */
  936. if (write_pointer == rtg_blocks(rtg) && used == 0) {
  937. error = xfs_zone_gc_reset_sync(rtg);
  938. if (error)
  939. return error;
  940. write_pointer = 0;
  941. }
  942. if (write_pointer == 0) {
  943. /* zone is empty */
  944. atomic_inc(&zi->zi_nr_free_zones);
  945. xfs_group_set_mark(rtg_group(rtg), XFS_RTG_FREE);
  946. iz->available += rtg_blocks(rtg);
  947. } else if (write_pointer < rtg_blocks(rtg)) {
  948. /* zone is open */
  949. struct xfs_open_zone *oz;
  950. atomic_inc(&rtg_group(rtg)->xg_active_ref);
  951. oz = xfs_init_open_zone(rtg, write_pointer, WRITE_LIFE_NOT_SET,
  952. false);
  953. list_add_tail(&oz->oz_entry, &zi->zi_open_zones);
  954. zi->zi_nr_open_zones++;
  955. iz->available += (rtg_blocks(rtg) - write_pointer);
  956. iz->reclaimable += write_pointer - used;
  957. } else if (used < rtg_blocks(rtg)) {
  958. /* zone fully written, but has freed blocks */
  959. xfs_zone_account_reclaimable(rtg, rtg_blocks(rtg) - used);
  960. iz->reclaimable += (rtg_blocks(rtg) - used);
  961. }
  962. return 0;
  963. }
  964. /*
  965. * Calculate the max open zone limit based on the of number of backing zones
  966. * available.
  967. */
  968. static inline uint32_t
  969. xfs_max_open_zones(
  970. struct xfs_mount *mp)
  971. {
  972. unsigned int max_open, max_open_data_zones;
  973. /*
  974. * We need two zones for every open data zone, one in reserve as we
  975. * don't reclaim open zones. One data zone and its spare is included
  976. * in XFS_MIN_ZONES to support at least one user data writer.
  977. */
  978. max_open_data_zones = (mp->m_sb.sb_rgcount - XFS_MIN_ZONES) / 2 + 1;
  979. max_open = max_open_data_zones + XFS_OPEN_GC_ZONES;
  980. /*
  981. * Cap the max open limit to 1/4 of available space. Without this we'd
  982. * run out of easy reclaim targets too quickly and storage devices don't
  983. * handle huge numbers of concurrent write streams overly well.
  984. */
  985. max_open = min(max_open, mp->m_sb.sb_rgcount / 4);
  986. return max(XFS_MIN_OPEN_ZONES, max_open);
  987. }
  988. /*
  989. * Normally we use the open zone limit that the device reports. If there is
  990. * none let the user pick one from the command line.
  991. *
  992. * If the device doesn't report an open zone limit and there is no override,
  993. * allow to hold about a quarter of the zones open. In theory we could allow
  994. * all to be open, but at that point we run into GC deadlocks because we can't
  995. * reclaim open zones.
  996. *
  997. * When used on conventional SSDs a lower open limit is advisable as we'll
  998. * otherwise overwhelm the FTL just as much as a conventional block allocator.
  999. *
  1000. * Note: To debug the open zone management code, force max_open to 1 here.
  1001. */
  1002. static int
  1003. xfs_calc_open_zones(
  1004. struct xfs_mount *mp)
  1005. {
  1006. struct block_device *bdev = mp->m_rtdev_targp->bt_bdev;
  1007. unsigned int bdev_open_zones = bdev_max_open_zones(bdev);
  1008. if (!mp->m_max_open_zones) {
  1009. if (bdev_open_zones)
  1010. mp->m_max_open_zones = bdev_open_zones;
  1011. else
  1012. mp->m_max_open_zones = XFS_DEFAULT_MAX_OPEN_ZONES;
  1013. }
  1014. if (mp->m_max_open_zones < XFS_MIN_OPEN_ZONES) {
  1015. xfs_notice(mp, "need at least %u open zones.",
  1016. XFS_MIN_OPEN_ZONES);
  1017. return -EIO;
  1018. }
  1019. if (bdev_open_zones && bdev_open_zones < mp->m_max_open_zones) {
  1020. mp->m_max_open_zones = bdev_open_zones;
  1021. xfs_info(mp, "limiting open zones to %u due to hardware limit.\n",
  1022. bdev_open_zones);
  1023. }
  1024. if (mp->m_max_open_zones > xfs_max_open_zones(mp)) {
  1025. mp->m_max_open_zones = xfs_max_open_zones(mp);
  1026. xfs_info(mp,
  1027. "limiting open zones to %u due to total zone count (%u)",
  1028. mp->m_max_open_zones, mp->m_sb.sb_rgcount);
  1029. }
  1030. return 0;
  1031. }
  1032. static unsigned long *
  1033. xfs_alloc_bucket_bitmap(
  1034. struct xfs_mount *mp)
  1035. {
  1036. return kvmalloc_array(BITS_TO_LONGS(mp->m_sb.sb_rgcount),
  1037. sizeof(unsigned long), GFP_KERNEL | __GFP_ZERO);
  1038. }
  1039. static struct xfs_zone_info *
  1040. xfs_alloc_zone_info(
  1041. struct xfs_mount *mp)
  1042. {
  1043. struct xfs_zone_info *zi;
  1044. int i;
  1045. zi = kzalloc_obj(*zi);
  1046. if (!zi)
  1047. return NULL;
  1048. INIT_LIST_HEAD(&zi->zi_open_zones);
  1049. INIT_LIST_HEAD(&zi->zi_reclaim_reservations);
  1050. spin_lock_init(&zi->zi_reset_list_lock);
  1051. spin_lock_init(&zi->zi_open_zones_lock);
  1052. spin_lock_init(&zi->zi_reservation_lock);
  1053. init_waitqueue_head(&zi->zi_zone_wait);
  1054. spin_lock_init(&zi->zi_used_buckets_lock);
  1055. for (i = 0; i < XFS_ZONE_USED_BUCKETS; i++) {
  1056. zi->zi_used_bucket_bitmap[i] = xfs_alloc_bucket_bitmap(mp);
  1057. if (!zi->zi_used_bucket_bitmap[i])
  1058. goto out_free_bitmaps;
  1059. }
  1060. return zi;
  1061. out_free_bitmaps:
  1062. while (--i > 0)
  1063. kvfree(zi->zi_used_bucket_bitmap[i]);
  1064. kfree(zi);
  1065. return NULL;
  1066. }
  1067. static void
  1068. xfs_free_zone_info(
  1069. struct xfs_zone_info *zi)
  1070. {
  1071. int i;
  1072. xfs_free_open_zones(zi);
  1073. for (i = 0; i < XFS_ZONE_USED_BUCKETS; i++)
  1074. kvfree(zi->zi_used_bucket_bitmap[i]);
  1075. kfree(zi);
  1076. }
  1077. int
  1078. xfs_mount_zones(
  1079. struct xfs_mount *mp)
  1080. {
  1081. struct xfs_init_zones iz = {
  1082. .zone_capacity = mp->m_groups[XG_TYPE_RTG].blocks,
  1083. .zone_size = xfs_rtgroup_raw_size(mp),
  1084. };
  1085. struct xfs_rtgroup *rtg = NULL;
  1086. int error;
  1087. if (!mp->m_rtdev_targp) {
  1088. xfs_notice(mp, "RT device missing.");
  1089. return -EINVAL;
  1090. }
  1091. if (!xfs_has_rtgroups(mp) || !xfs_has_rmapbt(mp)) {
  1092. xfs_notice(mp, "invalid flag combination.");
  1093. return -EFSCORRUPTED;
  1094. }
  1095. if (mp->m_sb.sb_rextsize != 1) {
  1096. xfs_notice(mp, "zoned file systems do not support rextsize.");
  1097. return -EFSCORRUPTED;
  1098. }
  1099. if (mp->m_sb.sb_rgcount < XFS_MIN_ZONES) {
  1100. xfs_notice(mp,
  1101. "zoned file systems need to have at least %u zones.", XFS_MIN_ZONES);
  1102. return -EFSCORRUPTED;
  1103. }
  1104. error = xfs_calc_open_zones(mp);
  1105. if (error)
  1106. return error;
  1107. mp->m_zone_info = xfs_alloc_zone_info(mp);
  1108. if (!mp->m_zone_info)
  1109. return -ENOMEM;
  1110. xfs_info(mp, "%u zones of %u blocks (%u max open zones)",
  1111. mp->m_sb.sb_rgcount, iz.zone_capacity, mp->m_max_open_zones);
  1112. trace_xfs_zones_mount(mp);
  1113. /*
  1114. * The writeback code switches between inodes regularly to provide
  1115. * fairness. The default lower bound is 4MiB, but for zoned file
  1116. * systems we want to increase that both to reduce seeks, but also more
  1117. * importantly so that workloads that writes files in a multiple of the
  1118. * zone size do not get fragmented and require garbage collection when
  1119. * they shouldn't. Increase is to the zone size capped by the max
  1120. * extent len.
  1121. *
  1122. * Note that because s_min_writeback_pages is a superblock field, this
  1123. * value also get applied to non-zoned files on the data device if
  1124. * there are any. On typical zoned setup all data is on the RT device
  1125. * because using the more efficient sequential write required zones
  1126. * is the reason for using the zone allocator, and either the RT device
  1127. * and the (meta)data device are on the same block device, or the
  1128. * (meta)data device is on a fast SSD while the data on the RT device
  1129. * is on a SMR HDD. In any combination of the above cases enforcing
  1130. * the higher min_writeback_pages for non-RT inodes is either a noop
  1131. * or beneficial.
  1132. */
  1133. mp->m_super->s_min_writeback_pages =
  1134. XFS_FSB_TO_B(mp, min(iz.zone_capacity, XFS_MAX_BMBT_EXTLEN)) >>
  1135. PAGE_SHIFT;
  1136. while ((rtg = xfs_rtgroup_next(mp, rtg))) {
  1137. xfs_rgblock_t write_pointer;
  1138. error = xfs_query_write_pointer(&iz, rtg, &write_pointer);
  1139. if (!error)
  1140. error = xfs_init_zone(&iz, rtg, write_pointer);
  1141. if (error) {
  1142. xfs_rtgroup_rele(rtg);
  1143. goto out_free_zone_info;
  1144. }
  1145. }
  1146. xfs_set_freecounter(mp, XC_FREE_RTAVAILABLE, iz.available);
  1147. xfs_set_freecounter(mp, XC_FREE_RTEXTENTS,
  1148. iz.available + iz.reclaimable);
  1149. /*
  1150. * The user may configure GC to free up a percentage of unused blocks.
  1151. * By default this is 0. GC will always trigger at the minimum level
  1152. * for keeping max_open_zones available for data placement.
  1153. */
  1154. mp->m_zonegc_low_space = 0;
  1155. error = xfs_zone_gc_mount(mp);
  1156. if (error)
  1157. goto out_free_zone_info;
  1158. return 0;
  1159. out_free_zone_info:
  1160. xfs_free_zone_info(mp->m_zone_info);
  1161. return error;
  1162. }
  1163. void
  1164. xfs_unmount_zones(
  1165. struct xfs_mount *mp)
  1166. {
  1167. xfs_zone_gc_unmount(mp);
  1168. xfs_free_zone_info(mp->m_zone_info);
  1169. }