zoned.c 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/bitops.h>
  3. #include <linux/slab.h>
  4. #include <linux/blkdev.h>
  5. #include <linux/sched/mm.h>
  6. #include <linux/atomic.h>
  7. #include <linux/vmalloc.h>
  8. #include "ctree.h"
  9. #include "volumes.h"
  10. #include "zoned.h"
  11. #include "disk-io.h"
  12. #include "block-group.h"
  13. #include "dev-replace.h"
  14. #include "space-info.h"
  15. #include "fs.h"
  16. #include "accessors.h"
  17. #include "bio.h"
  18. #include "transaction.h"
  19. #include "sysfs.h"
  20. /* Maximum number of zones to report per blkdev_report_zones() call */
  21. #define BTRFS_REPORT_NR_ZONES 4096
  22. /* Invalid allocation pointer value for missing devices */
  23. #define WP_MISSING_DEV ((u64)-1)
  24. /* Pseudo write pointer value for conventional zone */
  25. #define WP_CONVENTIONAL ((u64)-2)
  26. /*
  27. * Location of the first zone of superblock logging zone pairs.
  28. *
  29. * - primary superblock: 0B (zone 0)
  30. * - first copy: 512G (zone starting at that offset)
  31. * - second copy: 4T (zone starting at that offset)
  32. */
  33. #define BTRFS_SB_LOG_PRIMARY_OFFSET (0ULL)
  34. #define BTRFS_SB_LOG_FIRST_OFFSET (512ULL * SZ_1G)
  35. #define BTRFS_SB_LOG_SECOND_OFFSET (4096ULL * SZ_1G)
  36. #define BTRFS_SB_LOG_FIRST_SHIFT ilog2(BTRFS_SB_LOG_FIRST_OFFSET)
  37. #define BTRFS_SB_LOG_SECOND_SHIFT ilog2(BTRFS_SB_LOG_SECOND_OFFSET)
  38. /* Number of superblock log zones */
  39. #define BTRFS_NR_SB_LOG_ZONES 2
  40. /* Default number of max active zones when the device has no limits. */
  41. #define BTRFS_DEFAULT_MAX_ACTIVE_ZONES 128
  42. /*
  43. * Minimum of active zones we need:
  44. *
  45. * - BTRFS_SUPER_MIRROR_MAX zones for superblock mirrors
  46. * - 3 zones to ensure at least one zone per SYSTEM, META and DATA block group
  47. * - 1 zone for tree-log dedicated block group
  48. * - 1 zone for relocation
  49. */
  50. #define BTRFS_MIN_ACTIVE_ZONES (BTRFS_SUPER_MIRROR_MAX + 5)
  51. /*
  52. * Minimum / maximum supported zone size. Currently, SMR disks have a zone
  53. * size of 256MiB, and we are expecting ZNS drives to be in the 1-4GiB range.
  54. * We do not expect the zone size to become larger than 8GiB or smaller than
  55. * 4MiB in the near future.
  56. */
  57. #define BTRFS_MAX_ZONE_SIZE SZ_8G
  58. #define BTRFS_MIN_ZONE_SIZE SZ_4M
  59. #define SUPER_INFO_SECTORS ((u64)BTRFS_SUPER_INFO_SIZE >> SECTOR_SHIFT)
  60. static void wait_eb_writebacks(struct btrfs_block_group *block_group);
  61. static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_written);
  62. static inline bool sb_zone_is_full(const struct blk_zone *zone)
  63. {
  64. return (zone->cond == BLK_ZONE_COND_FULL) ||
  65. (zone->wp + SUPER_INFO_SECTORS > zone->start + zone->capacity);
  66. }
  67. static int copy_zone_info_cb(struct blk_zone *zone, unsigned int idx, void *data)
  68. {
  69. struct blk_zone *zones = data;
  70. memcpy(&zones[idx], zone, sizeof(*zone));
  71. return 0;
  72. }
  73. static int sb_write_pointer(struct block_device *bdev, struct blk_zone *zones,
  74. u64 *wp_ret)
  75. {
  76. bool empty[BTRFS_NR_SB_LOG_ZONES];
  77. bool full[BTRFS_NR_SB_LOG_ZONES];
  78. sector_t sector;
  79. for (int i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
  80. ASSERT(zones[i].type != BLK_ZONE_TYPE_CONVENTIONAL,
  81. "zones[%d].type=%d", i, zones[i].type);
  82. empty[i] = (zones[i].cond == BLK_ZONE_COND_EMPTY);
  83. full[i] = sb_zone_is_full(&zones[i]);
  84. }
  85. /*
  86. * Possible states of log buffer zones
  87. *
  88. * Empty[0] In use[0] Full[0]
  89. * Empty[1] * 0 1
  90. * In use[1] x x 1
  91. * Full[1] 0 0 C
  92. *
  93. * Log position:
  94. * *: Special case, no superblock is written
  95. * 0: Use write pointer of zones[0]
  96. * 1: Use write pointer of zones[1]
  97. * C: Compare super blocks from zones[0] and zones[1], use the latest
  98. * one determined by generation
  99. * x: Invalid state
  100. */
  101. if (empty[0] && empty[1]) {
  102. /* Special case to distinguish no superblock to read */
  103. *wp_ret = zones[0].start << SECTOR_SHIFT;
  104. return -ENOENT;
  105. } else if (full[0] && full[1]) {
  106. /* Compare two super blocks */
  107. struct address_space *mapping = bdev->bd_mapping;
  108. struct page *page[BTRFS_NR_SB_LOG_ZONES];
  109. struct btrfs_super_block *super[BTRFS_NR_SB_LOG_ZONES];
  110. for (int i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
  111. u64 zone_end = (zones[i].start + zones[i].capacity) << SECTOR_SHIFT;
  112. u64 bytenr = ALIGN_DOWN(zone_end, BTRFS_SUPER_INFO_SIZE) -
  113. BTRFS_SUPER_INFO_SIZE;
  114. page[i] = read_cache_page_gfp(mapping,
  115. bytenr >> PAGE_SHIFT, GFP_NOFS);
  116. if (IS_ERR(page[i])) {
  117. if (i == 1)
  118. btrfs_release_disk_super(super[0]);
  119. return PTR_ERR(page[i]);
  120. }
  121. super[i] = page_address(page[i]);
  122. }
  123. if (btrfs_super_generation(super[0]) >
  124. btrfs_super_generation(super[1]))
  125. sector = zones[1].start;
  126. else
  127. sector = zones[0].start;
  128. for (int i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++)
  129. btrfs_release_disk_super(super[i]);
  130. } else if (!full[0] && (empty[1] || full[1])) {
  131. sector = zones[0].wp;
  132. } else if (full[0]) {
  133. sector = zones[1].wp;
  134. } else {
  135. return -EUCLEAN;
  136. }
  137. *wp_ret = sector << SECTOR_SHIFT;
  138. return 0;
  139. }
  140. /*
  141. * Get the first zone number of the superblock mirror
  142. */
  143. static inline u32 sb_zone_number(int shift, int mirror)
  144. {
  145. u64 zone = U64_MAX;
  146. ASSERT(mirror < BTRFS_SUPER_MIRROR_MAX, "mirror=%d", mirror);
  147. switch (mirror) {
  148. case 0: zone = 0; break;
  149. case 1: zone = 1ULL << (BTRFS_SB_LOG_FIRST_SHIFT - shift); break;
  150. case 2: zone = 1ULL << (BTRFS_SB_LOG_SECOND_SHIFT - shift); break;
  151. }
  152. ASSERT(zone <= U32_MAX, "zone=%llu", zone);
  153. return (u32)zone;
  154. }
  155. static inline sector_t zone_start_sector(u32 zone_number,
  156. struct block_device *bdev)
  157. {
  158. return (sector_t)zone_number << ilog2(bdev_zone_sectors(bdev));
  159. }
  160. static inline u64 zone_start_physical(u32 zone_number,
  161. struct btrfs_zoned_device_info *zone_info)
  162. {
  163. return (u64)zone_number << zone_info->zone_size_shift;
  164. }
  165. /*
  166. * Emulate blkdev_report_zones() for a non-zoned device. It slices up the block
  167. * device into static sized chunks and fake a conventional zone on each of
  168. * them.
  169. */
  170. static int emulate_report_zones(struct btrfs_device *device, u64 pos,
  171. struct blk_zone *zones, unsigned int nr_zones)
  172. {
  173. const sector_t zone_sectors = device->fs_info->zone_size >> SECTOR_SHIFT;
  174. sector_t bdev_size = bdev_nr_sectors(device->bdev);
  175. unsigned int i;
  176. pos >>= SECTOR_SHIFT;
  177. for (i = 0; i < nr_zones; i++) {
  178. zones[i].start = i * zone_sectors + pos;
  179. zones[i].len = zone_sectors;
  180. zones[i].capacity = zone_sectors;
  181. zones[i].wp = zones[i].start + zone_sectors;
  182. zones[i].type = BLK_ZONE_TYPE_CONVENTIONAL;
  183. zones[i].cond = BLK_ZONE_COND_NOT_WP;
  184. if (zones[i].wp >= bdev_size) {
  185. i++;
  186. break;
  187. }
  188. }
  189. return i;
  190. }
  191. static int btrfs_get_dev_zones(struct btrfs_device *device, u64 pos,
  192. struct blk_zone *zones, unsigned int *nr_zones)
  193. {
  194. struct btrfs_zoned_device_info *zinfo = device->zone_info;
  195. int ret;
  196. if (!*nr_zones)
  197. return 0;
  198. if (!bdev_is_zoned(device->bdev)) {
  199. ret = emulate_report_zones(device, pos, zones, *nr_zones);
  200. *nr_zones = ret;
  201. return 0;
  202. }
  203. /* Check cache */
  204. if (zinfo->zone_cache) {
  205. unsigned int i;
  206. u32 zno;
  207. ASSERT(IS_ALIGNED(pos, zinfo->zone_size),
  208. "pos=%llu zinfo->zone_size=%llu", pos, zinfo->zone_size);
  209. zno = pos >> zinfo->zone_size_shift;
  210. /*
  211. * We cannot report zones beyond the zone end. So, it is OK to
  212. * cap *nr_zones to at the end.
  213. */
  214. *nr_zones = min_t(u32, *nr_zones, zinfo->nr_zones - zno);
  215. for (i = 0; i < *nr_zones; i++) {
  216. struct blk_zone *zone_info;
  217. zone_info = &zinfo->zone_cache[zno + i];
  218. if (!zone_info->len)
  219. break;
  220. }
  221. if (i == *nr_zones) {
  222. /* Cache hit on all the zones */
  223. memcpy(zones, zinfo->zone_cache + zno,
  224. sizeof(*zinfo->zone_cache) * *nr_zones);
  225. return 0;
  226. }
  227. }
  228. ret = blkdev_report_zones_cached(device->bdev, pos >> SECTOR_SHIFT,
  229. *nr_zones, copy_zone_info_cb, zones);
  230. if (ret < 0) {
  231. btrfs_err(device->fs_info,
  232. "zoned: failed to read zone %llu on %s (devid %llu)",
  233. pos, rcu_dereference(device->name),
  234. device->devid);
  235. return ret;
  236. }
  237. *nr_zones = ret;
  238. if (unlikely(!ret))
  239. return -EIO;
  240. /* Populate cache */
  241. if (zinfo->zone_cache) {
  242. u32 zno = pos >> zinfo->zone_size_shift;
  243. memcpy(zinfo->zone_cache + zno, zones,
  244. sizeof(*zinfo->zone_cache) * *nr_zones);
  245. }
  246. return 0;
  247. }
  248. /* The emulated zone size is determined from the size of device extent */
  249. static int calculate_emulated_zone_size(struct btrfs_fs_info *fs_info)
  250. {
  251. BTRFS_PATH_AUTO_FREE(path);
  252. struct btrfs_root *root = fs_info->dev_root;
  253. struct btrfs_key key;
  254. struct extent_buffer *leaf;
  255. struct btrfs_dev_extent *dext;
  256. int ret = 0;
  257. key.objectid = 1;
  258. key.type = BTRFS_DEV_EXTENT_KEY;
  259. key.offset = 0;
  260. path = btrfs_alloc_path();
  261. if (!path)
  262. return -ENOMEM;
  263. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  264. if (ret < 0)
  265. return ret;
  266. if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
  267. ret = btrfs_next_leaf(root, path);
  268. if (ret < 0)
  269. return ret;
  270. /* No dev extents at all? Not good */
  271. if (unlikely(ret > 0))
  272. return -EUCLEAN;
  273. }
  274. leaf = path->nodes[0];
  275. dext = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent);
  276. fs_info->zone_size = btrfs_dev_extent_length(leaf, dext);
  277. return 0;
  278. }
  279. int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info)
  280. {
  281. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  282. struct btrfs_device *device;
  283. int ret = 0;
  284. /* fs_info->zone_size might not set yet. Use the incomapt flag here. */
  285. if (!btrfs_fs_incompat(fs_info, ZONED))
  286. return 0;
  287. /*
  288. * No need to take the device_list mutex here, we're still in the mount
  289. * path and devices cannot be added to or removed from the list yet.
  290. */
  291. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  292. /* We can skip reading of zone info for missing devices */
  293. if (!device->bdev)
  294. continue;
  295. ret = btrfs_get_dev_zone_info(device, true);
  296. if (ret)
  297. break;
  298. }
  299. return ret;
  300. }
  301. int btrfs_get_dev_zone_info(struct btrfs_device *device, bool populate_cache)
  302. {
  303. struct btrfs_fs_info *fs_info = device->fs_info;
  304. struct btrfs_zoned_device_info *zone_info = NULL;
  305. struct block_device *bdev = device->bdev;
  306. unsigned int max_active_zones;
  307. unsigned int nactive;
  308. sector_t nr_sectors;
  309. sector_t sector = 0;
  310. struct blk_zone *zones = NULL;
  311. unsigned int i, nreported = 0, nr_zones;
  312. sector_t zone_sectors;
  313. char *model, *emulated;
  314. int ret;
  315. /*
  316. * Cannot use btrfs_is_zoned here, since fs_info::zone_size might not
  317. * yet be set.
  318. */
  319. if (!btrfs_fs_incompat(fs_info, ZONED))
  320. return 0;
  321. if (device->zone_info)
  322. return 0;
  323. zone_info = kzalloc_obj(*zone_info);
  324. if (!zone_info)
  325. return -ENOMEM;
  326. device->zone_info = zone_info;
  327. if (!bdev_is_zoned(bdev)) {
  328. if (!fs_info->zone_size) {
  329. ret = calculate_emulated_zone_size(fs_info);
  330. if (ret)
  331. goto out;
  332. }
  333. ASSERT(fs_info->zone_size);
  334. zone_sectors = fs_info->zone_size >> SECTOR_SHIFT;
  335. } else {
  336. zone_sectors = bdev_zone_sectors(bdev);
  337. }
  338. ASSERT(is_power_of_two_u64(zone_sectors));
  339. zone_info->zone_size = zone_sectors << SECTOR_SHIFT;
  340. /* We reject devices with a zone size larger than 8GB */
  341. if (zone_info->zone_size > BTRFS_MAX_ZONE_SIZE) {
  342. btrfs_err(fs_info,
  343. "zoned: %s: zone size %llu larger than supported maximum %llu",
  344. rcu_dereference(device->name),
  345. zone_info->zone_size, BTRFS_MAX_ZONE_SIZE);
  346. ret = -EINVAL;
  347. goto out;
  348. } else if (zone_info->zone_size < BTRFS_MIN_ZONE_SIZE) {
  349. btrfs_err(fs_info,
  350. "zoned: %s: zone size %llu smaller than supported minimum %u",
  351. rcu_dereference(device->name),
  352. zone_info->zone_size, BTRFS_MIN_ZONE_SIZE);
  353. ret = -EINVAL;
  354. goto out;
  355. }
  356. nr_sectors = bdev_nr_sectors(bdev);
  357. zone_info->zone_size_shift = ilog2(zone_info->zone_size);
  358. zone_info->nr_zones = nr_sectors >> ilog2(zone_sectors);
  359. if (!IS_ALIGNED(nr_sectors, zone_sectors))
  360. zone_info->nr_zones++;
  361. max_active_zones = min_not_zero(bdev_max_active_zones(bdev),
  362. bdev_max_open_zones(bdev));
  363. if (!max_active_zones && zone_info->nr_zones > BTRFS_DEFAULT_MAX_ACTIVE_ZONES)
  364. max_active_zones = BTRFS_DEFAULT_MAX_ACTIVE_ZONES;
  365. if (max_active_zones && max_active_zones < BTRFS_MIN_ACTIVE_ZONES) {
  366. btrfs_err(fs_info,
  367. "zoned: %s: max active zones %u is too small, need at least %u active zones",
  368. rcu_dereference(device->name), max_active_zones,
  369. BTRFS_MIN_ACTIVE_ZONES);
  370. ret = -EINVAL;
  371. goto out;
  372. }
  373. zone_info->max_active_zones = max_active_zones;
  374. zone_info->seq_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
  375. if (!zone_info->seq_zones) {
  376. ret = -ENOMEM;
  377. goto out;
  378. }
  379. zone_info->empty_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
  380. if (!zone_info->empty_zones) {
  381. ret = -ENOMEM;
  382. goto out;
  383. }
  384. zone_info->active_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
  385. if (!zone_info->active_zones) {
  386. ret = -ENOMEM;
  387. goto out;
  388. }
  389. zones = kvzalloc_objs(struct blk_zone, BTRFS_REPORT_NR_ZONES);
  390. if (!zones) {
  391. ret = -ENOMEM;
  392. goto out;
  393. }
  394. /*
  395. * Enable zone cache only for a zoned device. On a non-zoned device, we
  396. * fill the zone info with emulated CONVENTIONAL zones, so no need to
  397. * use the cache.
  398. */
  399. if (populate_cache && bdev_is_zoned(device->bdev)) {
  400. zone_info->zone_cache = vcalloc(zone_info->nr_zones,
  401. sizeof(struct blk_zone));
  402. if (!zone_info->zone_cache) {
  403. btrfs_err(device->fs_info,
  404. "zoned: failed to allocate zone cache for %s",
  405. rcu_dereference(device->name));
  406. ret = -ENOMEM;
  407. goto out;
  408. }
  409. }
  410. /* Get zones type */
  411. nactive = 0;
  412. while (sector < nr_sectors) {
  413. nr_zones = BTRFS_REPORT_NR_ZONES;
  414. ret = btrfs_get_dev_zones(device, sector << SECTOR_SHIFT, zones,
  415. &nr_zones);
  416. if (ret)
  417. goto out;
  418. for (i = 0; i < nr_zones; i++) {
  419. if (zones[i].type == BLK_ZONE_TYPE_SEQWRITE_REQ)
  420. __set_bit(nreported, zone_info->seq_zones);
  421. switch (zones[i].cond) {
  422. case BLK_ZONE_COND_EMPTY:
  423. __set_bit(nreported, zone_info->empty_zones);
  424. break;
  425. case BLK_ZONE_COND_IMP_OPEN:
  426. case BLK_ZONE_COND_EXP_OPEN:
  427. case BLK_ZONE_COND_CLOSED:
  428. case BLK_ZONE_COND_ACTIVE:
  429. __set_bit(nreported, zone_info->active_zones);
  430. nactive++;
  431. break;
  432. }
  433. nreported++;
  434. }
  435. sector = zones[nr_zones - 1].start + zones[nr_zones - 1].len;
  436. }
  437. if (unlikely(nreported != zone_info->nr_zones)) {
  438. btrfs_err(device->fs_info,
  439. "inconsistent number of zones on %s (%u/%u)",
  440. rcu_dereference(device->name), nreported,
  441. zone_info->nr_zones);
  442. ret = -EIO;
  443. goto out;
  444. }
  445. if (max_active_zones) {
  446. if (unlikely(nactive > max_active_zones)) {
  447. if (bdev_max_active_zones(bdev) == 0) {
  448. max_active_zones = 0;
  449. zone_info->max_active_zones = 0;
  450. goto validate;
  451. }
  452. btrfs_err(device->fs_info,
  453. "zoned: %u active zones on %s exceeds max_active_zones %u",
  454. nactive, rcu_dereference(device->name),
  455. max_active_zones);
  456. ret = -EIO;
  457. goto out;
  458. }
  459. atomic_set(&zone_info->active_zones_left,
  460. max_active_zones - nactive);
  461. set_bit(BTRFS_FS_ACTIVE_ZONE_TRACKING, &fs_info->flags);
  462. }
  463. validate:
  464. /* Validate superblock log */
  465. nr_zones = BTRFS_NR_SB_LOG_ZONES;
  466. for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
  467. u32 sb_zone;
  468. u64 sb_wp;
  469. int sb_pos = BTRFS_NR_SB_LOG_ZONES * i;
  470. sb_zone = sb_zone_number(zone_info->zone_size_shift, i);
  471. if (sb_zone + 1 >= zone_info->nr_zones)
  472. continue;
  473. ret = btrfs_get_dev_zones(device,
  474. zone_start_physical(sb_zone, zone_info),
  475. &zone_info->sb_zones[sb_pos],
  476. &nr_zones);
  477. if (ret)
  478. goto out;
  479. if (unlikely(nr_zones != BTRFS_NR_SB_LOG_ZONES)) {
  480. btrfs_err(device->fs_info,
  481. "zoned: failed to read super block log zone info at devid %llu zone %u",
  482. device->devid, sb_zone);
  483. ret = -EUCLEAN;
  484. goto out;
  485. }
  486. /*
  487. * If zones[0] is conventional, always use the beginning of the
  488. * zone to record superblock. No need to validate in that case.
  489. */
  490. if (zone_info->sb_zones[BTRFS_NR_SB_LOG_ZONES * i].type ==
  491. BLK_ZONE_TYPE_CONVENTIONAL)
  492. continue;
  493. ret = sb_write_pointer(device->bdev,
  494. &zone_info->sb_zones[sb_pos], &sb_wp);
  495. if (unlikely(ret != -ENOENT && ret)) {
  496. btrfs_err(device->fs_info,
  497. "zoned: super block log zone corrupted devid %llu zone %u",
  498. device->devid, sb_zone);
  499. ret = -EUCLEAN;
  500. goto out;
  501. }
  502. }
  503. kvfree(zones);
  504. if (bdev_is_zoned(bdev)) {
  505. model = "host-managed zoned";
  506. emulated = "";
  507. } else {
  508. model = "regular";
  509. emulated = "emulated ";
  510. }
  511. btrfs_info(fs_info,
  512. "%s block device %s, %u %szones of %llu bytes",
  513. model, rcu_dereference(device->name), zone_info->nr_zones,
  514. emulated, zone_info->zone_size);
  515. return 0;
  516. out:
  517. kvfree(zones);
  518. btrfs_destroy_dev_zone_info(device);
  519. return ret;
  520. }
  521. void btrfs_destroy_dev_zone_info(struct btrfs_device *device)
  522. {
  523. struct btrfs_zoned_device_info *zone_info = device->zone_info;
  524. if (!zone_info)
  525. return;
  526. bitmap_free(zone_info->active_zones);
  527. bitmap_free(zone_info->seq_zones);
  528. bitmap_free(zone_info->empty_zones);
  529. vfree(zone_info->zone_cache);
  530. kfree(zone_info);
  531. device->zone_info = NULL;
  532. }
  533. struct btrfs_zoned_device_info *btrfs_clone_dev_zone_info(struct btrfs_device *orig_dev)
  534. {
  535. struct btrfs_zoned_device_info *zone_info;
  536. zone_info = kmemdup(orig_dev->zone_info, sizeof(*zone_info), GFP_KERNEL);
  537. if (!zone_info)
  538. return NULL;
  539. zone_info->seq_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
  540. if (!zone_info->seq_zones)
  541. goto out;
  542. bitmap_copy(zone_info->seq_zones, orig_dev->zone_info->seq_zones,
  543. zone_info->nr_zones);
  544. zone_info->empty_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
  545. if (!zone_info->empty_zones)
  546. goto out;
  547. bitmap_copy(zone_info->empty_zones, orig_dev->zone_info->empty_zones,
  548. zone_info->nr_zones);
  549. zone_info->active_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
  550. if (!zone_info->active_zones)
  551. goto out;
  552. bitmap_copy(zone_info->active_zones, orig_dev->zone_info->active_zones,
  553. zone_info->nr_zones);
  554. zone_info->zone_cache = NULL;
  555. return zone_info;
  556. out:
  557. bitmap_free(zone_info->seq_zones);
  558. bitmap_free(zone_info->empty_zones);
  559. bitmap_free(zone_info->active_zones);
  560. kfree(zone_info);
  561. return NULL;
  562. }
  563. static int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos, struct blk_zone *zone)
  564. {
  565. unsigned int nr_zones = 1;
  566. int ret;
  567. ret = btrfs_get_dev_zones(device, pos, zone, &nr_zones);
  568. if (ret != 0 || !nr_zones)
  569. return ret ? ret : -EIO;
  570. return 0;
  571. }
  572. static int btrfs_check_for_zoned_device(struct btrfs_fs_info *fs_info)
  573. {
  574. struct btrfs_device *device;
  575. list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
  576. if (device->bdev && bdev_is_zoned(device->bdev)) {
  577. btrfs_err(fs_info,
  578. "zoned: mode not enabled but zoned device found: %pg",
  579. device->bdev);
  580. return -EINVAL;
  581. }
  582. }
  583. return 0;
  584. }
  585. int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info)
  586. {
  587. struct queue_limits *lim = &fs_info->limits;
  588. struct btrfs_device *device;
  589. u64 zone_size = 0;
  590. int ret;
  591. /*
  592. * Host-Managed devices can't be used without the ZONED flag. With the
  593. * ZONED all devices can be used, using zone emulation if required.
  594. */
  595. if (!btrfs_fs_incompat(fs_info, ZONED))
  596. return btrfs_check_for_zoned_device(fs_info);
  597. blk_set_stacking_limits(lim);
  598. list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
  599. struct btrfs_zoned_device_info *zone_info = device->zone_info;
  600. if (!device->bdev)
  601. continue;
  602. if (!zone_size) {
  603. zone_size = zone_info->zone_size;
  604. } else if (zone_info->zone_size != zone_size) {
  605. btrfs_err(fs_info,
  606. "zoned: unequal block device zone sizes: have %llu found %llu",
  607. zone_info->zone_size, zone_size);
  608. return -EINVAL;
  609. }
  610. /*
  611. * With the zoned emulation, we can have non-zoned device on the
  612. * zoned mode. In this case, we don't have a valid max zone
  613. * append size.
  614. */
  615. if (bdev_is_zoned(device->bdev))
  616. blk_stack_limits(lim, bdev_limits(device->bdev), 0);
  617. }
  618. ret = blk_validate_limits(lim);
  619. if (ret) {
  620. btrfs_err(fs_info, "zoned: failed to validate queue limits");
  621. return ret;
  622. }
  623. /*
  624. * stripe_size is always aligned to BTRFS_STRIPE_LEN in
  625. * btrfs_create_chunk(). Since we want stripe_len == zone_size,
  626. * check the alignment here.
  627. */
  628. if (!IS_ALIGNED(zone_size, BTRFS_STRIPE_LEN)) {
  629. btrfs_err(fs_info,
  630. "zoned: zone size %llu not aligned to stripe %u",
  631. zone_size, BTRFS_STRIPE_LEN);
  632. return -EINVAL;
  633. }
  634. if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
  635. btrfs_err(fs_info, "zoned: mixed block groups not supported");
  636. return -EINVAL;
  637. }
  638. fs_info->zone_size = zone_size;
  639. /*
  640. * Also limit max_zone_append_size by max_segments * PAGE_SIZE.
  641. * Technically, we can have multiple pages per segment. But, since
  642. * we add the pages one by one to a bio, and cannot increase the
  643. * metadata reservation even if it increases the number of extents, it
  644. * is safe to stick with the limit.
  645. */
  646. fs_info->max_zone_append_size = ALIGN_DOWN(
  647. min3((u64)lim->max_zone_append_sectors << SECTOR_SHIFT,
  648. (u64)lim->max_sectors << SECTOR_SHIFT,
  649. (u64)lim->max_segments << PAGE_SHIFT),
  650. fs_info->sectorsize);
  651. fs_info->fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_ZONED;
  652. fs_info->max_extent_size = min_not_zero(fs_info->max_extent_size,
  653. fs_info->max_zone_append_size);
  654. /*
  655. * Check mount options here, because we might change fs_info->zoned
  656. * from fs_info->zone_size.
  657. */
  658. ret = btrfs_check_mountopts_zoned(fs_info, &fs_info->mount_opt);
  659. if (ret)
  660. return ret;
  661. btrfs_info(fs_info, "zoned mode enabled with zone size %llu", zone_size);
  662. return 0;
  663. }
  664. int btrfs_check_mountopts_zoned(const struct btrfs_fs_info *info,
  665. unsigned long long *mount_opt)
  666. {
  667. if (!btrfs_is_zoned(info))
  668. return 0;
  669. /*
  670. * Space cache writing is not COWed. Disable that to avoid write errors
  671. * in sequential zones.
  672. */
  673. if (btrfs_raw_test_opt(*mount_opt, SPACE_CACHE)) {
  674. btrfs_err(info, "zoned: space cache v1 is not supported");
  675. return -EINVAL;
  676. }
  677. if (btrfs_raw_test_opt(*mount_opt, NODATACOW)) {
  678. btrfs_err(info, "zoned: NODATACOW not supported");
  679. return -EINVAL;
  680. }
  681. if (btrfs_raw_test_opt(*mount_opt, DISCARD_ASYNC)) {
  682. btrfs_info(info,
  683. "zoned: async discard ignored and disabled for zoned mode");
  684. btrfs_clear_opt(*mount_opt, DISCARD_ASYNC);
  685. }
  686. return 0;
  687. }
  688. static int sb_log_location(struct block_device *bdev, struct blk_zone *zones,
  689. int rw, u64 *bytenr_ret)
  690. {
  691. u64 wp;
  692. int ret;
  693. if (zones[0].type == BLK_ZONE_TYPE_CONVENTIONAL) {
  694. *bytenr_ret = zones[0].start << SECTOR_SHIFT;
  695. return 0;
  696. }
  697. ret = sb_write_pointer(bdev, zones, &wp);
  698. if (ret != -ENOENT && ret < 0)
  699. return ret;
  700. if (rw == WRITE) {
  701. struct blk_zone *reset = NULL;
  702. if (wp == zones[0].start << SECTOR_SHIFT)
  703. reset = &zones[0];
  704. else if (wp == zones[1].start << SECTOR_SHIFT)
  705. reset = &zones[1];
  706. if (reset && reset->cond != BLK_ZONE_COND_EMPTY) {
  707. unsigned int nofs_flags;
  708. ASSERT(sb_zone_is_full(reset));
  709. nofs_flags = memalloc_nofs_save();
  710. ret = blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
  711. reset->start, reset->len);
  712. memalloc_nofs_restore(nofs_flags);
  713. if (ret)
  714. return ret;
  715. reset->cond = BLK_ZONE_COND_EMPTY;
  716. reset->wp = reset->start;
  717. }
  718. } else if (ret != -ENOENT) {
  719. /*
  720. * For READ, we want the previous one. Move write pointer to
  721. * the end of a zone, if it is at the head of a zone.
  722. */
  723. u64 zone_end = 0;
  724. if (wp == zones[0].start << SECTOR_SHIFT)
  725. zone_end = zones[1].start + zones[1].capacity;
  726. else if (wp == zones[1].start << SECTOR_SHIFT)
  727. zone_end = zones[0].start + zones[0].capacity;
  728. if (zone_end)
  729. wp = ALIGN_DOWN(zone_end << SECTOR_SHIFT,
  730. BTRFS_SUPER_INFO_SIZE);
  731. wp -= BTRFS_SUPER_INFO_SIZE;
  732. }
  733. *bytenr_ret = wp;
  734. return 0;
  735. }
  736. int btrfs_sb_log_location_bdev(struct block_device *bdev, int mirror, int rw,
  737. u64 *bytenr_ret)
  738. {
  739. struct blk_zone zones[BTRFS_NR_SB_LOG_ZONES];
  740. sector_t zone_sectors;
  741. u32 sb_zone;
  742. int ret;
  743. u8 zone_sectors_shift;
  744. sector_t nr_sectors;
  745. u32 nr_zones;
  746. if (!bdev_is_zoned(bdev)) {
  747. *bytenr_ret = btrfs_sb_offset(mirror);
  748. return 0;
  749. }
  750. ASSERT(rw == READ || rw == WRITE);
  751. zone_sectors = bdev_zone_sectors(bdev);
  752. if (!is_power_of_2(zone_sectors))
  753. return -EINVAL;
  754. zone_sectors_shift = ilog2(zone_sectors);
  755. nr_sectors = bdev_nr_sectors(bdev);
  756. nr_zones = nr_sectors >> zone_sectors_shift;
  757. sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
  758. if (sb_zone + 1 >= nr_zones)
  759. return -ENOENT;
  760. ret = blkdev_report_zones_cached(bdev, zone_start_sector(sb_zone, bdev),
  761. BTRFS_NR_SB_LOG_ZONES,
  762. copy_zone_info_cb, zones);
  763. if (ret < 0)
  764. return ret;
  765. if (unlikely(ret != BTRFS_NR_SB_LOG_ZONES))
  766. return -EIO;
  767. return sb_log_location(bdev, zones, rw, bytenr_ret);
  768. }
  769. int btrfs_sb_log_location(struct btrfs_device *device, int mirror, int rw,
  770. u64 *bytenr_ret)
  771. {
  772. struct btrfs_zoned_device_info *zinfo = device->zone_info;
  773. u32 zone_num;
  774. /*
  775. * For a zoned filesystem on a non-zoned block device, use the same
  776. * super block locations as regular filesystem. Doing so, the super
  777. * block can always be retrieved and the zoned flag of the volume
  778. * detected from the super block information.
  779. */
  780. if (!bdev_is_zoned(device->bdev)) {
  781. *bytenr_ret = btrfs_sb_offset(mirror);
  782. return 0;
  783. }
  784. zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
  785. if (zone_num + 1 >= zinfo->nr_zones)
  786. return -ENOENT;
  787. return sb_log_location(device->bdev,
  788. &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror],
  789. rw, bytenr_ret);
  790. }
  791. static inline bool is_sb_log_zone(struct btrfs_zoned_device_info *zinfo,
  792. int mirror)
  793. {
  794. u32 zone_num;
  795. if (!zinfo)
  796. return false;
  797. zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
  798. if (zone_num + 1 >= zinfo->nr_zones)
  799. return false;
  800. if (!test_bit(zone_num, zinfo->seq_zones))
  801. return false;
  802. return true;
  803. }
  804. int btrfs_advance_sb_log(struct btrfs_device *device, int mirror)
  805. {
  806. struct btrfs_zoned_device_info *zinfo = device->zone_info;
  807. struct blk_zone *zone;
  808. int i;
  809. if (!is_sb_log_zone(zinfo, mirror))
  810. return 0;
  811. zone = &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror];
  812. for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
  813. /* Advance the next zone */
  814. if (zone->cond == BLK_ZONE_COND_FULL) {
  815. zone++;
  816. continue;
  817. }
  818. if (zone->cond == BLK_ZONE_COND_EMPTY)
  819. zone->cond = BLK_ZONE_COND_IMP_OPEN;
  820. zone->wp += SUPER_INFO_SECTORS;
  821. if (sb_zone_is_full(zone)) {
  822. /*
  823. * No room left to write new superblock. Since
  824. * superblock is written with REQ_SYNC, it is safe to
  825. * finish the zone now.
  826. *
  827. * If the write pointer is exactly at the capacity,
  828. * explicit ZONE_FINISH is not necessary.
  829. */
  830. if (zone->wp != zone->start + zone->capacity) {
  831. unsigned int nofs_flags;
  832. int ret;
  833. nofs_flags = memalloc_nofs_save();
  834. ret = blkdev_zone_mgmt(device->bdev,
  835. REQ_OP_ZONE_FINISH, zone->start,
  836. zone->len);
  837. memalloc_nofs_restore(nofs_flags);
  838. if (ret)
  839. return ret;
  840. }
  841. zone->wp = zone->start + zone->len;
  842. zone->cond = BLK_ZONE_COND_FULL;
  843. }
  844. return 0;
  845. }
  846. /* All the zones are FULL. Should not reach here. */
  847. DEBUG_WARN("unexpected state, all zones full");
  848. return -EIO;
  849. }
  850. int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror)
  851. {
  852. unsigned int nofs_flags;
  853. sector_t zone_sectors;
  854. sector_t nr_sectors;
  855. u8 zone_sectors_shift;
  856. u32 sb_zone;
  857. u32 nr_zones;
  858. int ret;
  859. zone_sectors = bdev_zone_sectors(bdev);
  860. zone_sectors_shift = ilog2(zone_sectors);
  861. nr_sectors = bdev_nr_sectors(bdev);
  862. nr_zones = nr_sectors >> zone_sectors_shift;
  863. sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
  864. if (sb_zone + 1 >= nr_zones)
  865. return -ENOENT;
  866. nofs_flags = memalloc_nofs_save();
  867. ret = blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
  868. zone_start_sector(sb_zone, bdev),
  869. zone_sectors * BTRFS_NR_SB_LOG_ZONES);
  870. memalloc_nofs_restore(nofs_flags);
  871. return ret;
  872. }
  873. /*
  874. * Find allocatable zones within a given region.
  875. *
  876. * @device: the device to allocate a region on
  877. * @hole_start: the position of the hole to allocate the region
  878. * @num_bytes: size of wanted region
  879. * @hole_end: the end of the hole
  880. * @return: position of allocatable zones
  881. *
  882. * Allocatable region should not contain any superblock locations.
  883. */
  884. u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start,
  885. u64 hole_end, u64 num_bytes)
  886. {
  887. struct btrfs_zoned_device_info *zinfo = device->zone_info;
  888. const u8 shift = zinfo->zone_size_shift;
  889. u64 nzones = num_bytes >> shift;
  890. u64 pos = hole_start;
  891. u64 begin, end;
  892. bool have_sb;
  893. int i;
  894. ASSERT(IS_ALIGNED(hole_start, zinfo->zone_size),
  895. "hole_start=%llu zinfo->zone_size=%llu", hole_start, zinfo->zone_size);
  896. ASSERT(IS_ALIGNED(num_bytes, zinfo->zone_size),
  897. "num_bytes=%llu zinfo->zone_size=%llu", num_bytes, zinfo->zone_size);
  898. while (pos < hole_end) {
  899. begin = pos >> shift;
  900. end = begin + nzones;
  901. if (end > zinfo->nr_zones)
  902. return hole_end;
  903. /* Check if zones in the region are all empty */
  904. if (btrfs_dev_is_sequential(device, pos) &&
  905. !bitmap_test_range_all_set(zinfo->empty_zones, begin, nzones)) {
  906. pos += zinfo->zone_size;
  907. continue;
  908. }
  909. have_sb = false;
  910. for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
  911. u32 sb_zone;
  912. u64 sb_pos;
  913. sb_zone = sb_zone_number(shift, i);
  914. if (!(end <= sb_zone ||
  915. sb_zone + BTRFS_NR_SB_LOG_ZONES <= begin)) {
  916. have_sb = true;
  917. pos = zone_start_physical(
  918. sb_zone + BTRFS_NR_SB_LOG_ZONES, zinfo);
  919. break;
  920. }
  921. /* We also need to exclude regular superblock positions */
  922. sb_pos = btrfs_sb_offset(i);
  923. if (!(pos + num_bytes <= sb_pos ||
  924. sb_pos + BTRFS_SUPER_INFO_SIZE <= pos)) {
  925. have_sb = true;
  926. pos = ALIGN(sb_pos + BTRFS_SUPER_INFO_SIZE,
  927. zinfo->zone_size);
  928. break;
  929. }
  930. }
  931. if (!have_sb)
  932. break;
  933. }
  934. return pos;
  935. }
  936. static bool btrfs_dev_set_active_zone(struct btrfs_device *device, u64 pos)
  937. {
  938. struct btrfs_zoned_device_info *zone_info = device->zone_info;
  939. unsigned int zno = (pos >> zone_info->zone_size_shift);
  940. /* We can use any number of zones */
  941. if (zone_info->max_active_zones == 0)
  942. return true;
  943. if (!test_bit(zno, zone_info->active_zones)) {
  944. /* Active zone left? */
  945. if (atomic_dec_if_positive(&zone_info->active_zones_left) < 0)
  946. return false;
  947. if (test_and_set_bit(zno, zone_info->active_zones)) {
  948. /* Someone already set the bit */
  949. atomic_inc(&zone_info->active_zones_left);
  950. }
  951. }
  952. return true;
  953. }
  954. static void btrfs_dev_clear_active_zone(struct btrfs_device *device, u64 pos)
  955. {
  956. struct btrfs_zoned_device_info *zone_info = device->zone_info;
  957. unsigned int zno = (pos >> zone_info->zone_size_shift);
  958. /* We can use any number of zones */
  959. if (zone_info->max_active_zones == 0)
  960. return;
  961. if (test_and_clear_bit(zno, zone_info->active_zones))
  962. atomic_inc(&zone_info->active_zones_left);
  963. }
  964. int btrfs_reset_device_zone(struct btrfs_device *device, u64 physical,
  965. u64 length, u64 *bytes)
  966. {
  967. unsigned int nofs_flags;
  968. int ret;
  969. *bytes = 0;
  970. nofs_flags = memalloc_nofs_save();
  971. ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_RESET,
  972. physical >> SECTOR_SHIFT, length >> SECTOR_SHIFT);
  973. memalloc_nofs_restore(nofs_flags);
  974. if (ret)
  975. return ret;
  976. *bytes = length;
  977. while (length) {
  978. btrfs_dev_set_zone_empty(device, physical);
  979. btrfs_dev_clear_active_zone(device, physical);
  980. physical += device->zone_info->zone_size;
  981. length -= device->zone_info->zone_size;
  982. }
  983. return 0;
  984. }
  985. int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size)
  986. {
  987. struct btrfs_zoned_device_info *zinfo = device->zone_info;
  988. const u8 shift = zinfo->zone_size_shift;
  989. unsigned long begin = start >> shift;
  990. unsigned long nbits = size >> shift;
  991. u64 pos;
  992. int ret;
  993. ASSERT(IS_ALIGNED(start, zinfo->zone_size),
  994. "start=%llu, zinfo->zone_size=%llu", start, zinfo->zone_size);
  995. ASSERT(IS_ALIGNED(size, zinfo->zone_size),
  996. "size=%llu, zinfo->zone_size=%llu", size, zinfo->zone_size);
  997. if (begin + nbits > zinfo->nr_zones)
  998. return -ERANGE;
  999. /* All the zones are conventional */
  1000. if (bitmap_test_range_all_zero(zinfo->seq_zones, begin, nbits))
  1001. return 0;
  1002. /* All the zones are sequential and empty */
  1003. if (bitmap_test_range_all_set(zinfo->seq_zones, begin, nbits) &&
  1004. bitmap_test_range_all_set(zinfo->empty_zones, begin, nbits))
  1005. return 0;
  1006. for (pos = start; pos < start + size; pos += zinfo->zone_size) {
  1007. u64 reset_bytes;
  1008. if (!btrfs_dev_is_sequential(device, pos) ||
  1009. btrfs_dev_is_empty_zone(device, pos))
  1010. continue;
  1011. /* Free regions should be empty */
  1012. btrfs_warn(
  1013. device->fs_info,
  1014. "zoned: resetting device %s (devid %llu) zone %llu for allocation",
  1015. rcu_dereference(device->name), device->devid, pos >> shift);
  1016. WARN_ON_ONCE(1);
  1017. ret = btrfs_reset_device_zone(device, pos, zinfo->zone_size,
  1018. &reset_bytes);
  1019. if (ret)
  1020. return ret;
  1021. }
  1022. return 0;
  1023. }
  1024. /*
  1025. * Calculate an allocation pointer from the extent allocation information
  1026. * for a block group consist of conventional zones. It is pointed to the
  1027. * end of the highest addressed extent in the block group as an allocation
  1028. * offset.
  1029. */
  1030. static int calculate_alloc_pointer(struct btrfs_block_group *cache,
  1031. u64 *offset_ret, bool new)
  1032. {
  1033. struct btrfs_fs_info *fs_info = cache->fs_info;
  1034. struct btrfs_root *root;
  1035. BTRFS_PATH_AUTO_FREE(path);
  1036. struct btrfs_key key;
  1037. struct btrfs_key found_key;
  1038. const u64 bg_end = btrfs_block_group_end(cache);
  1039. int ret;
  1040. u64 length;
  1041. /*
  1042. * Avoid tree lookups for a new block group, there's no use for it.
  1043. * It must always be 0.
  1044. *
  1045. * Also, we have a lock chain of extent buffer lock -> chunk mutex.
  1046. * For new a block group, this function is called from
  1047. * btrfs_make_block_group() which is already taking the chunk mutex.
  1048. * Thus, we cannot call calculate_alloc_pointer() which takes extent
  1049. * buffer locks to avoid deadlock.
  1050. */
  1051. if (new) {
  1052. *offset_ret = 0;
  1053. return 0;
  1054. }
  1055. path = btrfs_alloc_path();
  1056. if (!path)
  1057. return -ENOMEM;
  1058. key.objectid = bg_end;
  1059. key.type = 0;
  1060. key.offset = 0;
  1061. root = btrfs_extent_root(fs_info, key.objectid);
  1062. if (unlikely(!root)) {
  1063. btrfs_err(fs_info,
  1064. "missing extent root for extent at bytenr %llu",
  1065. key.objectid);
  1066. return -EUCLEAN;
  1067. }
  1068. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1069. /* We should not find the exact match */
  1070. if (unlikely(!ret))
  1071. ret = -EUCLEAN;
  1072. if (ret < 0)
  1073. return ret;
  1074. ret = btrfs_previous_extent_item(root, path, cache->start);
  1075. if (ret) {
  1076. if (ret == 1) {
  1077. ret = 0;
  1078. *offset_ret = 0;
  1079. }
  1080. return ret;
  1081. }
  1082. btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
  1083. if (found_key.type == BTRFS_EXTENT_ITEM_KEY)
  1084. length = found_key.offset;
  1085. else
  1086. length = fs_info->nodesize;
  1087. if (unlikely(!(found_key.objectid >= cache->start &&
  1088. found_key.objectid + length <= bg_end))) {
  1089. return -EUCLEAN;
  1090. }
  1091. *offset_ret = found_key.objectid + length - cache->start;
  1092. return 0;
  1093. }
  1094. struct zone_info {
  1095. u64 physical;
  1096. u64 capacity;
  1097. u64 alloc_offset;
  1098. };
  1099. static int btrfs_load_zone_info(struct btrfs_fs_info *fs_info, int zone_idx,
  1100. struct zone_info *info, unsigned long *active,
  1101. struct btrfs_chunk_map *map, bool new)
  1102. {
  1103. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  1104. struct btrfs_device *device;
  1105. int dev_replace_is_ongoing = 0;
  1106. unsigned int nofs_flag;
  1107. struct blk_zone zone;
  1108. int ret;
  1109. info->physical = map->stripes[zone_idx].physical;
  1110. down_read(&dev_replace->rwsem);
  1111. device = map->stripes[zone_idx].dev;
  1112. if (!device->bdev) {
  1113. up_read(&dev_replace->rwsem);
  1114. info->alloc_offset = WP_MISSING_DEV;
  1115. return 0;
  1116. }
  1117. /* Consider a zone as active if we can allow any number of active zones. */
  1118. if (!device->zone_info->max_active_zones)
  1119. __set_bit(zone_idx, active);
  1120. if (!btrfs_dev_is_sequential(device, info->physical)) {
  1121. up_read(&dev_replace->rwsem);
  1122. info->alloc_offset = WP_CONVENTIONAL;
  1123. info->capacity = device->zone_info->zone_size;
  1124. return 0;
  1125. }
  1126. ASSERT(!new || btrfs_dev_is_empty_zone(device, info->physical));
  1127. /* This zone will be used for allocation, so mark this zone non-empty. */
  1128. btrfs_dev_clear_zone_empty(device, info->physical);
  1129. dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
  1130. if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
  1131. btrfs_dev_clear_zone_empty(dev_replace->tgtdev, info->physical);
  1132. /*
  1133. * The group is mapped to a sequential zone. Get the zone write pointer
  1134. * to determine the allocation offset within the zone.
  1135. */
  1136. WARN_ON(!IS_ALIGNED(info->physical, fs_info->zone_size));
  1137. if (new) {
  1138. sector_t capacity;
  1139. capacity = bdev_zone_capacity(device->bdev, info->physical >> SECTOR_SHIFT);
  1140. up_read(&dev_replace->rwsem);
  1141. info->alloc_offset = 0;
  1142. info->capacity = capacity << SECTOR_SHIFT;
  1143. return 0;
  1144. }
  1145. nofs_flag = memalloc_nofs_save();
  1146. ret = btrfs_get_dev_zone(device, info->physical, &zone);
  1147. memalloc_nofs_restore(nofs_flag);
  1148. if (ret) {
  1149. up_read(&dev_replace->rwsem);
  1150. if (ret != -EIO && ret != -EOPNOTSUPP)
  1151. return ret;
  1152. info->alloc_offset = WP_MISSING_DEV;
  1153. return 0;
  1154. }
  1155. if (unlikely(zone.type == BLK_ZONE_TYPE_CONVENTIONAL)) {
  1156. btrfs_err(fs_info,
  1157. "zoned: unexpected conventional zone %llu on device %s (devid %llu)",
  1158. zone.start << SECTOR_SHIFT, rcu_dereference(device->name),
  1159. device->devid);
  1160. up_read(&dev_replace->rwsem);
  1161. return -EIO;
  1162. }
  1163. info->capacity = (zone.capacity << SECTOR_SHIFT);
  1164. switch (zone.cond) {
  1165. case BLK_ZONE_COND_OFFLINE:
  1166. case BLK_ZONE_COND_READONLY:
  1167. btrfs_err(fs_info,
  1168. "zoned: offline/readonly zone %llu on device %s (devid %llu)",
  1169. (info->physical >> device->zone_info->zone_size_shift),
  1170. rcu_dereference(device->name), device->devid);
  1171. info->alloc_offset = WP_MISSING_DEV;
  1172. break;
  1173. case BLK_ZONE_COND_EMPTY:
  1174. info->alloc_offset = 0;
  1175. break;
  1176. case BLK_ZONE_COND_FULL:
  1177. info->alloc_offset = info->capacity;
  1178. break;
  1179. default:
  1180. /* Partially used zone. */
  1181. info->alloc_offset = ((zone.wp - zone.start) << SECTOR_SHIFT);
  1182. __set_bit(zone_idx, active);
  1183. break;
  1184. }
  1185. up_read(&dev_replace->rwsem);
  1186. return 0;
  1187. }
  1188. static int btrfs_load_block_group_single(struct btrfs_block_group *bg,
  1189. struct zone_info *info,
  1190. unsigned long *active)
  1191. {
  1192. if (unlikely(info->alloc_offset == WP_MISSING_DEV)) {
  1193. btrfs_err(bg->fs_info,
  1194. "zoned: cannot recover write pointer for zone %llu",
  1195. info->physical);
  1196. return -EIO;
  1197. }
  1198. bg->alloc_offset = info->alloc_offset;
  1199. bg->zone_capacity = info->capacity;
  1200. if (test_bit(0, active))
  1201. set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags);
  1202. return 0;
  1203. }
  1204. static int btrfs_load_block_group_dup(struct btrfs_block_group *bg,
  1205. struct btrfs_chunk_map *map,
  1206. struct zone_info *zone_info,
  1207. unsigned long *active,
  1208. u64 last_alloc)
  1209. {
  1210. struct btrfs_fs_info *fs_info = bg->fs_info;
  1211. if ((map->type & BTRFS_BLOCK_GROUP_DATA) && !fs_info->stripe_root) {
  1212. btrfs_err(fs_info, "zoned: data DUP profile needs raid-stripe-tree");
  1213. return -EINVAL;
  1214. }
  1215. bg->zone_capacity = min_not_zero(zone_info[0].capacity, zone_info[1].capacity);
  1216. if (unlikely(zone_info[0].alloc_offset == WP_MISSING_DEV)) {
  1217. btrfs_err(fs_info,
  1218. "zoned: cannot recover write pointer for zone %llu",
  1219. zone_info[0].physical);
  1220. return -EIO;
  1221. }
  1222. if (unlikely(zone_info[1].alloc_offset == WP_MISSING_DEV)) {
  1223. btrfs_err(fs_info,
  1224. "zoned: cannot recover write pointer for zone %llu",
  1225. zone_info[1].physical);
  1226. return -EIO;
  1227. }
  1228. /*
  1229. * When the last extent is removed, last_alloc can be smaller than the other write
  1230. * pointer. In that case, last_alloc should be moved to the corresponding write
  1231. * pointer position.
  1232. */
  1233. for (int i = 0; i < map->num_stripes; i++) {
  1234. if (zone_info[i].alloc_offset == WP_CONVENTIONAL)
  1235. continue;
  1236. if (last_alloc <= zone_info[i].alloc_offset) {
  1237. last_alloc = zone_info[i].alloc_offset;
  1238. break;
  1239. }
  1240. }
  1241. if (zone_info[0].alloc_offset == WP_CONVENTIONAL)
  1242. zone_info[0].alloc_offset = last_alloc;
  1243. if (zone_info[1].alloc_offset == WP_CONVENTIONAL)
  1244. zone_info[1].alloc_offset = last_alloc;
  1245. if (unlikely(zone_info[0].alloc_offset != zone_info[1].alloc_offset)) {
  1246. btrfs_err(fs_info,
  1247. "zoned: write pointer offset mismatch of zones in DUP profile");
  1248. return -EIO;
  1249. }
  1250. if (test_bit(0, active) != test_bit(1, active)) {
  1251. if (unlikely(!btrfs_zone_activate(bg)))
  1252. return -EIO;
  1253. } else if (test_bit(0, active)) {
  1254. set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags);
  1255. }
  1256. bg->alloc_offset = zone_info[0].alloc_offset;
  1257. return 0;
  1258. }
  1259. static int btrfs_load_block_group_raid1(struct btrfs_block_group *bg,
  1260. struct btrfs_chunk_map *map,
  1261. struct zone_info *zone_info,
  1262. unsigned long *active,
  1263. u64 last_alloc)
  1264. {
  1265. struct btrfs_fs_info *fs_info = bg->fs_info;
  1266. int i;
  1267. if ((map->type & BTRFS_BLOCK_GROUP_DATA) && !fs_info->stripe_root) {
  1268. btrfs_err(fs_info, "zoned: data %s needs raid-stripe-tree",
  1269. btrfs_bg_type_to_raid_name(map->type));
  1270. return -EINVAL;
  1271. }
  1272. /* In case a device is missing we have a cap of 0, so don't use it. */
  1273. bg->zone_capacity = min_not_zero(zone_info[0].capacity, zone_info[1].capacity);
  1274. /*
  1275. * When the last extent is removed, last_alloc can be smaller than the other write
  1276. * pointer. In that case, last_alloc should be moved to the corresponding write
  1277. * pointer position.
  1278. */
  1279. for (i = 0; i < map->num_stripes; i++) {
  1280. if (zone_info[i].alloc_offset == WP_MISSING_DEV ||
  1281. zone_info[i].alloc_offset == WP_CONVENTIONAL)
  1282. continue;
  1283. if (last_alloc <= zone_info[i].alloc_offset) {
  1284. last_alloc = zone_info[i].alloc_offset;
  1285. break;
  1286. }
  1287. }
  1288. for (i = 0; i < map->num_stripes; i++) {
  1289. if (zone_info[i].alloc_offset == WP_MISSING_DEV)
  1290. continue;
  1291. if (zone_info[i].alloc_offset == WP_CONVENTIONAL)
  1292. zone_info[i].alloc_offset = last_alloc;
  1293. if (unlikely((zone_info[0].alloc_offset != zone_info[i].alloc_offset) &&
  1294. !btrfs_test_opt(fs_info, DEGRADED))) {
  1295. btrfs_err(fs_info,
  1296. "zoned: write pointer offset mismatch of zones in %s profile",
  1297. btrfs_bg_type_to_raid_name(map->type));
  1298. return -EIO;
  1299. }
  1300. if (test_bit(0, active) != test_bit(i, active)) {
  1301. if (unlikely(!btrfs_test_opt(fs_info, DEGRADED) &&
  1302. !btrfs_zone_activate(bg))) {
  1303. return -EIO;
  1304. }
  1305. } else {
  1306. if (test_bit(0, active))
  1307. set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags);
  1308. }
  1309. }
  1310. if (zone_info[0].alloc_offset != WP_MISSING_DEV)
  1311. bg->alloc_offset = zone_info[0].alloc_offset;
  1312. else
  1313. bg->alloc_offset = zone_info[i - 1].alloc_offset;
  1314. return 0;
  1315. }
  1316. static int btrfs_load_block_group_raid0(struct btrfs_block_group *bg,
  1317. struct btrfs_chunk_map *map,
  1318. struct zone_info *zone_info,
  1319. unsigned long *active,
  1320. u64 last_alloc)
  1321. {
  1322. struct btrfs_fs_info *fs_info = bg->fs_info;
  1323. u64 stripe_nr = 0, stripe_offset = 0;
  1324. u64 prev_offset = 0;
  1325. u32 stripe_index = 0;
  1326. bool has_partial = false, has_conventional = false;
  1327. if ((map->type & BTRFS_BLOCK_GROUP_DATA) && !fs_info->stripe_root) {
  1328. btrfs_err(fs_info, "zoned: data %s needs raid-stripe-tree",
  1329. btrfs_bg_type_to_raid_name(map->type));
  1330. return -EINVAL;
  1331. }
  1332. /*
  1333. * When the last extent is removed, last_alloc can be smaller than the other write
  1334. * pointer. In that case, last_alloc should be moved to the corresponding write
  1335. * pointer position.
  1336. */
  1337. for (int i = 0; i < map->num_stripes; i++) {
  1338. u64 alloc;
  1339. if (zone_info[i].alloc_offset == WP_MISSING_DEV ||
  1340. zone_info[i].alloc_offset == WP_CONVENTIONAL)
  1341. continue;
  1342. stripe_nr = zone_info[i].alloc_offset >> BTRFS_STRIPE_LEN_SHIFT;
  1343. stripe_offset = zone_info[i].alloc_offset & BTRFS_STRIPE_LEN_MASK;
  1344. if (stripe_offset == 0 && stripe_nr > 0) {
  1345. stripe_nr--;
  1346. stripe_offset = BTRFS_STRIPE_LEN;
  1347. }
  1348. alloc = ((stripe_nr * map->num_stripes + i) << BTRFS_STRIPE_LEN_SHIFT) +
  1349. stripe_offset;
  1350. last_alloc = max(last_alloc, alloc);
  1351. /* Partially written stripe found. It should be last. */
  1352. if (zone_info[i].alloc_offset & BTRFS_STRIPE_LEN_MASK)
  1353. break;
  1354. }
  1355. stripe_nr = 0;
  1356. stripe_offset = 0;
  1357. if (last_alloc) {
  1358. u32 factor = map->num_stripes;
  1359. stripe_nr = last_alloc >> BTRFS_STRIPE_LEN_SHIFT;
  1360. stripe_offset = last_alloc & BTRFS_STRIPE_LEN_MASK;
  1361. stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
  1362. }
  1363. for (int i = 0; i < map->num_stripes; i++) {
  1364. if (zone_info[i].alloc_offset == WP_MISSING_DEV)
  1365. continue;
  1366. if (zone_info[i].alloc_offset == WP_CONVENTIONAL) {
  1367. has_conventional = true;
  1368. zone_info[i].alloc_offset = btrfs_stripe_nr_to_offset(stripe_nr);
  1369. if (stripe_index > i)
  1370. zone_info[i].alloc_offset += BTRFS_STRIPE_LEN;
  1371. else if (stripe_index == i)
  1372. zone_info[i].alloc_offset += stripe_offset;
  1373. }
  1374. /* Verification */
  1375. if (i != 0) {
  1376. if (unlikely(prev_offset < zone_info[i].alloc_offset)) {
  1377. btrfs_err(fs_info,
  1378. "zoned: stripe position disorder found in block group %llu",
  1379. bg->start);
  1380. return -EIO;
  1381. }
  1382. if (unlikely(has_partial &&
  1383. (zone_info[i].alloc_offset & BTRFS_STRIPE_LEN_MASK))) {
  1384. btrfs_err(fs_info,
  1385. "zoned: multiple partial written stripe found in block group %llu",
  1386. bg->start);
  1387. return -EIO;
  1388. }
  1389. }
  1390. prev_offset = zone_info[i].alloc_offset;
  1391. if ((zone_info[i].alloc_offset & BTRFS_STRIPE_LEN_MASK) != 0)
  1392. has_partial = true;
  1393. if (test_bit(0, active) != test_bit(i, active)) {
  1394. if (unlikely(!btrfs_zone_activate(bg)))
  1395. return -EIO;
  1396. } else {
  1397. if (test_bit(0, active))
  1398. set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags);
  1399. }
  1400. bg->zone_capacity += zone_info[i].capacity;
  1401. bg->alloc_offset += zone_info[i].alloc_offset;
  1402. }
  1403. /* Check if all devices stay in the same stripe row. */
  1404. if (unlikely(zone_info[0].alloc_offset -
  1405. zone_info[map->num_stripes - 1].alloc_offset > BTRFS_STRIPE_LEN)) {
  1406. btrfs_err(fs_info, "zoned: stripe gap too large in block group %llu", bg->start);
  1407. return -EIO;
  1408. }
  1409. if (unlikely(has_conventional && bg->alloc_offset < last_alloc)) {
  1410. btrfs_err(fs_info, "zoned: allocated extent stays beyond write pointers %llu %llu",
  1411. bg->alloc_offset, last_alloc);
  1412. return -EIO;
  1413. }
  1414. return 0;
  1415. }
  1416. static int btrfs_load_block_group_raid10(struct btrfs_block_group *bg,
  1417. struct btrfs_chunk_map *map,
  1418. struct zone_info *zone_info,
  1419. unsigned long *active,
  1420. u64 last_alloc)
  1421. {
  1422. struct btrfs_fs_info *fs_info = bg->fs_info;
  1423. u64 AUTO_KFREE(raid0_allocs);
  1424. u64 stripe_nr = 0, stripe_offset = 0;
  1425. u32 stripe_index = 0;
  1426. bool has_partial = false, has_conventional = false;
  1427. u64 prev_offset = 0;
  1428. if ((map->type & BTRFS_BLOCK_GROUP_DATA) && !fs_info->stripe_root) {
  1429. btrfs_err(fs_info, "zoned: data %s needs raid-stripe-tree",
  1430. btrfs_bg_type_to_raid_name(map->type));
  1431. return -EINVAL;
  1432. }
  1433. raid0_allocs = kcalloc(map->num_stripes / map->sub_stripes, sizeof(*raid0_allocs),
  1434. GFP_NOFS);
  1435. if (!raid0_allocs)
  1436. return -ENOMEM;
  1437. /*
  1438. * When the last extent is removed, last_alloc can be smaller than the other write
  1439. * pointer. In that case, last_alloc should be moved to the corresponding write
  1440. * pointer position.
  1441. */
  1442. for (int i = 0; i < map->num_stripes; i += map->sub_stripes) {
  1443. u64 alloc = zone_info[i].alloc_offset;
  1444. for (int j = 1; j < map->sub_stripes; j++) {
  1445. int idx = i + j;
  1446. if (zone_info[idx].alloc_offset == WP_MISSING_DEV ||
  1447. zone_info[idx].alloc_offset == WP_CONVENTIONAL)
  1448. continue;
  1449. if (alloc == WP_MISSING_DEV || alloc == WP_CONVENTIONAL) {
  1450. alloc = zone_info[idx].alloc_offset;
  1451. } else if (unlikely(zone_info[idx].alloc_offset != alloc)) {
  1452. btrfs_err(fs_info,
  1453. "zoned: write pointer mismatch found in block group %llu",
  1454. bg->start);
  1455. return -EIO;
  1456. }
  1457. }
  1458. raid0_allocs[i / map->sub_stripes] = alloc;
  1459. if (alloc == WP_CONVENTIONAL)
  1460. continue;
  1461. if (unlikely(alloc == WP_MISSING_DEV)) {
  1462. btrfs_err(fs_info,
  1463. "zoned: cannot recover write pointer of block group %llu due to missing device",
  1464. bg->start);
  1465. return -EIO;
  1466. }
  1467. stripe_nr = alloc >> BTRFS_STRIPE_LEN_SHIFT;
  1468. stripe_offset = alloc & BTRFS_STRIPE_LEN_MASK;
  1469. if (stripe_offset == 0 && stripe_nr > 0) {
  1470. stripe_nr--;
  1471. stripe_offset = BTRFS_STRIPE_LEN;
  1472. }
  1473. alloc = ((stripe_nr * (map->num_stripes / map->sub_stripes) +
  1474. (i / map->sub_stripes)) <<
  1475. BTRFS_STRIPE_LEN_SHIFT) + stripe_offset;
  1476. last_alloc = max(last_alloc, alloc);
  1477. }
  1478. stripe_nr = 0;
  1479. stripe_offset = 0;
  1480. if (last_alloc) {
  1481. u32 factor = map->num_stripes / map->sub_stripes;
  1482. stripe_nr = last_alloc >> BTRFS_STRIPE_LEN_SHIFT;
  1483. stripe_offset = last_alloc & BTRFS_STRIPE_LEN_MASK;
  1484. stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
  1485. }
  1486. for (int i = 0; i < map->num_stripes; i++) {
  1487. int idx = i / map->sub_stripes;
  1488. if (raid0_allocs[idx] == WP_CONVENTIONAL) {
  1489. has_conventional = true;
  1490. raid0_allocs[idx] = btrfs_stripe_nr_to_offset(stripe_nr);
  1491. if (stripe_index > idx)
  1492. raid0_allocs[idx] += BTRFS_STRIPE_LEN;
  1493. else if (stripe_index == idx)
  1494. raid0_allocs[idx] += stripe_offset;
  1495. }
  1496. if ((i % map->sub_stripes) == 0) {
  1497. /* Verification */
  1498. if (i != 0) {
  1499. if (unlikely(prev_offset < raid0_allocs[idx])) {
  1500. btrfs_err(fs_info,
  1501. "zoned: stripe position disorder found in block group %llu",
  1502. bg->start);
  1503. return -EIO;
  1504. }
  1505. if (unlikely(has_partial &&
  1506. (raid0_allocs[idx] & BTRFS_STRIPE_LEN_MASK))) {
  1507. btrfs_err(fs_info,
  1508. "zoned: multiple partial written stripe found in block group %llu",
  1509. bg->start);
  1510. return -EIO;
  1511. }
  1512. }
  1513. prev_offset = raid0_allocs[idx];
  1514. if ((raid0_allocs[idx] & BTRFS_STRIPE_LEN_MASK) != 0)
  1515. has_partial = true;
  1516. }
  1517. if (zone_info[i].alloc_offset == WP_MISSING_DEV ||
  1518. zone_info[i].alloc_offset == WP_CONVENTIONAL)
  1519. zone_info[i].alloc_offset = raid0_allocs[idx];
  1520. if (test_bit(0, active) != test_bit(i, active)) {
  1521. if (unlikely(!btrfs_zone_activate(bg)))
  1522. return -EIO;
  1523. } else if (test_bit(0, active)) {
  1524. set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags);
  1525. }
  1526. if ((i % map->sub_stripes) == 0) {
  1527. bg->zone_capacity += zone_info[i].capacity;
  1528. bg->alloc_offset += zone_info[i].alloc_offset;
  1529. }
  1530. }
  1531. /* Check if all devices stay in the same stripe row. */
  1532. if (unlikely(zone_info[0].alloc_offset -
  1533. zone_info[map->num_stripes - 1].alloc_offset > BTRFS_STRIPE_LEN)) {
  1534. btrfs_err(fs_info, "zoned: stripe gap too large in block group %llu",
  1535. bg->start);
  1536. return -EIO;
  1537. }
  1538. if (unlikely(has_conventional && bg->alloc_offset < last_alloc)) {
  1539. btrfs_err(fs_info, "zoned: allocated extent stays beyond write pointers %llu %llu",
  1540. bg->alloc_offset, last_alloc);
  1541. return -EIO;
  1542. }
  1543. return 0;
  1544. }
  1545. EXPORT_FOR_TESTS
  1546. int btrfs_load_block_group_by_raid_type(struct btrfs_block_group *bg,
  1547. struct btrfs_chunk_map *map,
  1548. struct zone_info *zone_info,
  1549. unsigned long *active, u64 last_alloc)
  1550. {
  1551. struct btrfs_fs_info *fs_info = bg->fs_info;
  1552. u64 profile;
  1553. int ret;
  1554. profile = map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK;
  1555. switch (profile) {
  1556. case 0: /* single */
  1557. ret = btrfs_load_block_group_single(bg, &zone_info[0], active);
  1558. break;
  1559. case BTRFS_BLOCK_GROUP_DUP:
  1560. ret = btrfs_load_block_group_dup(bg, map, zone_info, active, last_alloc);
  1561. break;
  1562. case BTRFS_BLOCK_GROUP_RAID1:
  1563. case BTRFS_BLOCK_GROUP_RAID1C3:
  1564. case BTRFS_BLOCK_GROUP_RAID1C4:
  1565. ret = btrfs_load_block_group_raid1(bg, map, zone_info, active, last_alloc);
  1566. break;
  1567. case BTRFS_BLOCK_GROUP_RAID0:
  1568. ret = btrfs_load_block_group_raid0(bg, map, zone_info, active, last_alloc);
  1569. break;
  1570. case BTRFS_BLOCK_GROUP_RAID10:
  1571. ret = btrfs_load_block_group_raid10(bg, map, zone_info, active, last_alloc);
  1572. break;
  1573. case BTRFS_BLOCK_GROUP_RAID5:
  1574. case BTRFS_BLOCK_GROUP_RAID6:
  1575. default:
  1576. btrfs_err(fs_info, "zoned: profile %s not yet supported",
  1577. btrfs_bg_type_to_raid_name(map->type));
  1578. return -EINVAL;
  1579. }
  1580. if (ret == -EIO && profile != 0 && profile != BTRFS_BLOCK_GROUP_RAID0 &&
  1581. profile != BTRFS_BLOCK_GROUP_RAID10) {
  1582. /*
  1583. * Detected broken write pointer. Make this block group
  1584. * unallocatable by setting the allocation pointer at the end of
  1585. * allocatable region. Relocating this block group will fix the
  1586. * mismatch.
  1587. *
  1588. * Currently, we cannot handle RAID0 or RAID10 case like this
  1589. * because we don't have a proper zone_capacity value. But,
  1590. * reading from this block group won't work anyway by a missing
  1591. * stripe.
  1592. */
  1593. bg->alloc_offset = bg->zone_capacity;
  1594. }
  1595. return ret;
  1596. }
  1597. int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
  1598. {
  1599. struct btrfs_fs_info *fs_info = cache->fs_info;
  1600. struct btrfs_chunk_map *map;
  1601. u64 logical = cache->start;
  1602. u64 length = cache->length;
  1603. struct zone_info AUTO_KFREE(zone_info);
  1604. int ret;
  1605. int i;
  1606. unsigned long *active = NULL;
  1607. u64 last_alloc = 0;
  1608. u32 num_sequential = 0, num_conventional = 0;
  1609. if (!btrfs_is_zoned(fs_info))
  1610. return 0;
  1611. /* Sanity check */
  1612. if (unlikely(!IS_ALIGNED(length, fs_info->zone_size))) {
  1613. btrfs_err(fs_info,
  1614. "zoned: block group %llu len %llu unaligned to zone size %llu",
  1615. logical, length, fs_info->zone_size);
  1616. return -EIO;
  1617. }
  1618. map = btrfs_find_chunk_map(fs_info, logical, length);
  1619. if (!map)
  1620. return -EINVAL;
  1621. cache->physical_map = map;
  1622. zone_info = kcalloc(map->num_stripes, sizeof(*zone_info), GFP_NOFS);
  1623. if (!zone_info) {
  1624. ret = -ENOMEM;
  1625. goto out;
  1626. }
  1627. active = bitmap_zalloc(map->num_stripes, GFP_NOFS);
  1628. if (!active) {
  1629. ret = -ENOMEM;
  1630. goto out;
  1631. }
  1632. for (i = 0; i < map->num_stripes; i++) {
  1633. ret = btrfs_load_zone_info(fs_info, i, &zone_info[i], active, map, new);
  1634. if (ret)
  1635. goto out;
  1636. if (zone_info[i].alloc_offset == WP_CONVENTIONAL)
  1637. num_conventional++;
  1638. else
  1639. num_sequential++;
  1640. }
  1641. if (num_sequential > 0)
  1642. set_bit(BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE, &cache->runtime_flags);
  1643. if (num_conventional > 0) {
  1644. ret = calculate_alloc_pointer(cache, &last_alloc, new);
  1645. if (ret) {
  1646. btrfs_err(fs_info,
  1647. "zoned: failed to determine allocation offset of bg %llu",
  1648. cache->start);
  1649. goto out;
  1650. } else if (map->num_stripes == num_conventional) {
  1651. cache->alloc_offset = last_alloc;
  1652. cache->zone_capacity = cache->length;
  1653. set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &cache->runtime_flags);
  1654. goto out;
  1655. }
  1656. }
  1657. ret = btrfs_load_block_group_by_raid_type(cache, map, zone_info, active, last_alloc);
  1658. out:
  1659. /* Reject non SINGLE data profiles without RST */
  1660. if ((map->type & BTRFS_BLOCK_GROUP_DATA) &&
  1661. (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) &&
  1662. !fs_info->stripe_root) {
  1663. btrfs_err(fs_info, "zoned: data %s needs raid-stripe-tree",
  1664. btrfs_bg_type_to_raid_name(map->type));
  1665. ret = -EINVAL;
  1666. }
  1667. if (unlikely(cache->alloc_offset > cache->zone_capacity)) {
  1668. btrfs_err(fs_info,
  1669. "zoned: invalid write pointer %llu (larger than zone capacity %llu) in block group %llu",
  1670. cache->alloc_offset, cache->zone_capacity,
  1671. cache->start);
  1672. ret = -EIO;
  1673. }
  1674. /* An extent is allocated after the write pointer */
  1675. if (!ret && num_conventional && last_alloc > cache->alloc_offset) {
  1676. btrfs_err(fs_info,
  1677. "zoned: got wrong write pointer in BG %llu: %llu > %llu",
  1678. logical, last_alloc, cache->alloc_offset);
  1679. ret = -EIO;
  1680. }
  1681. if (!ret) {
  1682. cache->meta_write_pointer = cache->alloc_offset + cache->start;
  1683. if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &cache->runtime_flags)) {
  1684. btrfs_get_block_group(cache);
  1685. spin_lock(&fs_info->zone_active_bgs_lock);
  1686. list_add_tail(&cache->active_bg_list,
  1687. &fs_info->zone_active_bgs);
  1688. spin_unlock(&fs_info->zone_active_bgs_lock);
  1689. }
  1690. } else {
  1691. btrfs_free_chunk_map(cache->physical_map);
  1692. cache->physical_map = NULL;
  1693. }
  1694. bitmap_free(active);
  1695. return ret;
  1696. }
  1697. void btrfs_calc_zone_unusable(struct btrfs_block_group *cache)
  1698. {
  1699. u64 unusable, free;
  1700. if (!btrfs_is_zoned(cache->fs_info))
  1701. return;
  1702. WARN_ON(cache->bytes_super != 0);
  1703. unusable = (cache->alloc_offset - cache->used) +
  1704. (cache->length - cache->zone_capacity);
  1705. free = cache->zone_capacity - cache->alloc_offset;
  1706. /* We only need ->free_space in ALLOC_SEQ block groups */
  1707. cache->cached = BTRFS_CACHE_FINISHED;
  1708. cache->free_space_ctl->free_space = free;
  1709. cache->zone_unusable = unusable;
  1710. }
  1711. bool btrfs_use_zone_append(struct btrfs_bio *bbio)
  1712. {
  1713. u64 start = (bbio->bio.bi_iter.bi_sector << SECTOR_SHIFT);
  1714. struct btrfs_inode *inode = bbio->inode;
  1715. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  1716. struct btrfs_block_group *cache;
  1717. bool ret = false;
  1718. if (!btrfs_is_zoned(fs_info))
  1719. return false;
  1720. if (!is_data_inode(inode))
  1721. return false;
  1722. if (btrfs_op(&bbio->bio) != BTRFS_MAP_WRITE)
  1723. return false;
  1724. /*
  1725. * Using REQ_OP_ZONE_APPEND for relocation can break assumptions on the
  1726. * extent layout the relocation code has.
  1727. * Furthermore we have set aside own block-group from which only the
  1728. * relocation "process" can allocate and make sure only one process at a
  1729. * time can add pages to an extent that gets relocated, so it's safe to
  1730. * use regular REQ_OP_WRITE for this special case.
  1731. */
  1732. if (btrfs_is_data_reloc_root(inode->root))
  1733. return false;
  1734. cache = btrfs_lookup_block_group(fs_info, start);
  1735. ASSERT(cache);
  1736. if (!cache)
  1737. return false;
  1738. ret = !!test_bit(BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE, &cache->runtime_flags);
  1739. btrfs_put_block_group(cache);
  1740. return ret;
  1741. }
  1742. void btrfs_record_physical_zoned(struct btrfs_bio *bbio)
  1743. {
  1744. const u64 physical = bbio->bio.bi_iter.bi_sector << SECTOR_SHIFT;
  1745. struct btrfs_ordered_sum *sum = bbio->sums;
  1746. if (physical < bbio->orig_physical)
  1747. sum->logical -= bbio->orig_physical - physical;
  1748. else
  1749. sum->logical += physical - bbio->orig_physical;
  1750. }
  1751. static void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent *ordered,
  1752. u64 logical)
  1753. {
  1754. struct extent_map_tree *em_tree = &ordered->inode->extent_tree;
  1755. struct extent_map *em;
  1756. ordered->disk_bytenr = logical;
  1757. write_lock(&em_tree->lock);
  1758. em = btrfs_search_extent_mapping(em_tree, ordered->file_offset,
  1759. ordered->num_bytes);
  1760. /* The em should be a new COW extent, thus it should not have an offset. */
  1761. ASSERT(em->offset == 0, "em->offset=%llu", em->offset);
  1762. em->disk_bytenr = logical;
  1763. btrfs_free_extent_map(em);
  1764. write_unlock(&em_tree->lock);
  1765. }
  1766. static bool btrfs_zoned_split_ordered(struct btrfs_ordered_extent *ordered,
  1767. u64 logical, u64 len)
  1768. {
  1769. struct btrfs_ordered_extent *new;
  1770. if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags) &&
  1771. btrfs_split_extent_map(ordered->inode, ordered->file_offset,
  1772. ordered->num_bytes, len, logical))
  1773. return false;
  1774. new = btrfs_split_ordered_extent(ordered, len);
  1775. if (IS_ERR(new))
  1776. return false;
  1777. new->disk_bytenr = logical;
  1778. btrfs_finish_one_ordered(new);
  1779. return true;
  1780. }
  1781. void btrfs_finish_ordered_zoned(struct btrfs_ordered_extent *ordered)
  1782. {
  1783. struct btrfs_inode *inode = ordered->inode;
  1784. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  1785. struct btrfs_ordered_sum *sum;
  1786. u64 logical, len;
  1787. /*
  1788. * Write to pre-allocated region is for the data relocation, and so
  1789. * it should use WRITE operation. No split/rewrite are necessary.
  1790. */
  1791. if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags))
  1792. return;
  1793. ASSERT(!list_empty(&ordered->list));
  1794. /* The ordered->list can be empty in the above pre-alloc case. */
  1795. sum = list_first_entry(&ordered->list, struct btrfs_ordered_sum, list);
  1796. logical = sum->logical;
  1797. len = sum->len;
  1798. while (len < ordered->disk_num_bytes) {
  1799. sum = list_next_entry(sum, list);
  1800. if (sum->logical == logical + len) {
  1801. len += sum->len;
  1802. continue;
  1803. }
  1804. if (!btrfs_zoned_split_ordered(ordered, logical, len)) {
  1805. set_bit(BTRFS_ORDERED_IOERR, &ordered->flags);
  1806. btrfs_err(fs_info, "failed to split ordered extent");
  1807. goto out;
  1808. }
  1809. logical = sum->logical;
  1810. len = sum->len;
  1811. }
  1812. if (ordered->disk_bytenr != logical)
  1813. btrfs_rewrite_logical_zoned(ordered, logical);
  1814. out:
  1815. /*
  1816. * If we end up here for nodatasum I/O, the btrfs_ordered_sum structures
  1817. * were allocated by btrfs_alloc_dummy_sum only to record the logical
  1818. * addresses and don't contain actual checksums. We thus must free them
  1819. * here so that we don't attempt to log the csums later.
  1820. */
  1821. if ((inode->flags & BTRFS_INODE_NODATASUM) ||
  1822. test_bit(BTRFS_FS_STATE_NO_DATA_CSUMS, &fs_info->fs_state)) {
  1823. while ((sum = list_first_entry_or_null(&ordered->list,
  1824. typeof(*sum), list))) {
  1825. list_del(&sum->list);
  1826. kfree(sum);
  1827. }
  1828. }
  1829. }
  1830. static bool check_bg_is_active(struct btrfs_eb_write_context *ctx,
  1831. struct btrfs_block_group **active_bg)
  1832. {
  1833. const struct writeback_control *wbc = ctx->wbc;
  1834. struct btrfs_block_group *block_group = ctx->zoned_bg;
  1835. struct btrfs_fs_info *fs_info = block_group->fs_info;
  1836. if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags))
  1837. return true;
  1838. if (fs_info->treelog_bg == block_group->start) {
  1839. if (!btrfs_zone_activate(block_group)) {
  1840. int ret_fin = btrfs_zone_finish_one_bg(fs_info);
  1841. if (ret_fin != 1 || !btrfs_zone_activate(block_group))
  1842. return false;
  1843. }
  1844. } else if (*active_bg != block_group) {
  1845. struct btrfs_block_group *tgt = *active_bg;
  1846. /* zoned_meta_io_lock protects fs_info->active_{meta,system}_bg. */
  1847. lockdep_assert_held(&fs_info->zoned_meta_io_lock);
  1848. if (tgt) {
  1849. /*
  1850. * If there is an unsent IO left in the allocated area,
  1851. * we cannot wait for them as it may cause a deadlock.
  1852. */
  1853. if (tgt->meta_write_pointer < tgt->start + tgt->alloc_offset) {
  1854. if (wbc->sync_mode == WB_SYNC_NONE ||
  1855. (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync))
  1856. return false;
  1857. }
  1858. /* Pivot active metadata/system block group. */
  1859. btrfs_zoned_meta_io_unlock(fs_info);
  1860. wait_eb_writebacks(tgt);
  1861. do_zone_finish(tgt, true);
  1862. btrfs_zoned_meta_io_lock(fs_info);
  1863. if (*active_bg == tgt) {
  1864. btrfs_put_block_group(tgt);
  1865. *active_bg = NULL;
  1866. }
  1867. }
  1868. if (!btrfs_zone_activate(block_group))
  1869. return false;
  1870. if (*active_bg != block_group) {
  1871. ASSERT(*active_bg == NULL);
  1872. *active_bg = block_group;
  1873. btrfs_get_block_group(block_group);
  1874. }
  1875. }
  1876. return true;
  1877. }
  1878. /*
  1879. * Check if @ctx->eb is aligned to the write pointer.
  1880. *
  1881. * Return:
  1882. * 0: @ctx->eb is at the write pointer. You can write it.
  1883. * -EAGAIN: There is a hole. The caller should handle the case.
  1884. * -EBUSY: There is a hole, but the caller can just bail out.
  1885. */
  1886. int btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
  1887. struct btrfs_eb_write_context *ctx)
  1888. {
  1889. const struct writeback_control *wbc = ctx->wbc;
  1890. const struct extent_buffer *eb = ctx->eb;
  1891. struct btrfs_block_group *block_group = ctx->zoned_bg;
  1892. if (!btrfs_is_zoned(fs_info))
  1893. return 0;
  1894. if (block_group) {
  1895. if (block_group->start > eb->start ||
  1896. btrfs_block_group_end(block_group) <= eb->start) {
  1897. btrfs_put_block_group(block_group);
  1898. block_group = NULL;
  1899. ctx->zoned_bg = NULL;
  1900. }
  1901. }
  1902. if (!block_group) {
  1903. block_group = btrfs_lookup_block_group(fs_info, eb->start);
  1904. if (!block_group)
  1905. return 0;
  1906. ctx->zoned_bg = block_group;
  1907. }
  1908. if (block_group->meta_write_pointer == eb->start) {
  1909. struct btrfs_block_group **tgt;
  1910. if (!test_bit(BTRFS_FS_ACTIVE_ZONE_TRACKING, &fs_info->flags))
  1911. return 0;
  1912. if (block_group->flags & BTRFS_BLOCK_GROUP_SYSTEM)
  1913. tgt = &fs_info->active_system_bg;
  1914. else
  1915. tgt = &fs_info->active_meta_bg;
  1916. if (check_bg_is_active(ctx, tgt))
  1917. return 0;
  1918. }
  1919. /*
  1920. * Since we may release fs_info->zoned_meta_io_lock, someone can already
  1921. * start writing this eb. In that case, we can just bail out.
  1922. */
  1923. if (block_group->meta_write_pointer > eb->start)
  1924. return -EBUSY;
  1925. /* If for_sync, this hole will be filled with transaction commit. */
  1926. if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync)
  1927. return -EAGAIN;
  1928. return -EBUSY;
  1929. }
  1930. int btrfs_zoned_issue_zeroout(struct btrfs_device *device, u64 physical, u64 length)
  1931. {
  1932. if (!btrfs_dev_is_sequential(device, physical))
  1933. return -EOPNOTSUPP;
  1934. return blkdev_issue_zeroout(device->bdev, physical >> SECTOR_SHIFT,
  1935. length >> SECTOR_SHIFT, GFP_NOFS, 0);
  1936. }
  1937. static int read_zone_info(struct btrfs_fs_info *fs_info, u64 logical,
  1938. struct blk_zone *zone)
  1939. {
  1940. struct btrfs_io_context *bioc = NULL;
  1941. u64 mapped_length = PAGE_SIZE;
  1942. unsigned int nofs_flag;
  1943. int nmirrors;
  1944. int i, ret;
  1945. ret = btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
  1946. &mapped_length, &bioc, NULL, NULL);
  1947. if (unlikely(ret || !bioc || mapped_length < PAGE_SIZE)) {
  1948. ret = -EIO;
  1949. goto out_put_bioc;
  1950. }
  1951. if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
  1952. ret = -EINVAL;
  1953. goto out_put_bioc;
  1954. }
  1955. nofs_flag = memalloc_nofs_save();
  1956. nmirrors = (int)bioc->num_stripes;
  1957. for (i = 0; i < nmirrors; i++) {
  1958. u64 physical = bioc->stripes[i].physical;
  1959. struct btrfs_device *dev = bioc->stripes[i].dev;
  1960. /* Missing device */
  1961. if (!dev->bdev)
  1962. continue;
  1963. ret = btrfs_get_dev_zone(dev, physical, zone);
  1964. /* Failing device */
  1965. if (ret == -EIO || ret == -EOPNOTSUPP)
  1966. continue;
  1967. break;
  1968. }
  1969. memalloc_nofs_restore(nofs_flag);
  1970. out_put_bioc:
  1971. btrfs_put_bioc(bioc);
  1972. return ret;
  1973. }
  1974. /*
  1975. * Synchronize write pointer in a zone at @physical_start on @tgt_dev, by
  1976. * filling zeros between @physical_pos to a write pointer of dev-replace
  1977. * source device.
  1978. */
  1979. int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev, u64 logical,
  1980. u64 physical_start, u64 physical_pos)
  1981. {
  1982. struct btrfs_fs_info *fs_info = tgt_dev->fs_info;
  1983. struct blk_zone zone;
  1984. u64 length;
  1985. u64 wp;
  1986. int ret;
  1987. if (!btrfs_dev_is_sequential(tgt_dev, physical_pos))
  1988. return 0;
  1989. ret = read_zone_info(fs_info, logical, &zone);
  1990. if (ret)
  1991. return ret;
  1992. wp = physical_start + ((zone.wp - zone.start) << SECTOR_SHIFT);
  1993. if (physical_pos == wp)
  1994. return 0;
  1995. if (unlikely(physical_pos > wp))
  1996. return -EUCLEAN;
  1997. length = wp - physical_pos;
  1998. return btrfs_zoned_issue_zeroout(tgt_dev, physical_pos, length);
  1999. }
  2000. /*
  2001. * Activate block group and underlying device zones
  2002. *
  2003. * @block_group: the block group to activate
  2004. *
  2005. * Return: true on success, false otherwise
  2006. */
  2007. bool btrfs_zone_activate(struct btrfs_block_group *block_group)
  2008. {
  2009. struct btrfs_fs_info *fs_info = block_group->fs_info;
  2010. struct btrfs_chunk_map *map;
  2011. struct btrfs_device *device;
  2012. u64 physical;
  2013. const bool is_data = (block_group->flags & BTRFS_BLOCK_GROUP_DATA);
  2014. bool ret;
  2015. int i;
  2016. if (!btrfs_is_zoned(block_group->fs_info))
  2017. return true;
  2018. map = block_group->physical_map;
  2019. spin_lock(&fs_info->zone_active_bgs_lock);
  2020. spin_lock(&block_group->lock);
  2021. if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags)) {
  2022. ret = true;
  2023. goto out_unlock;
  2024. }
  2025. if (block_group->flags & BTRFS_BLOCK_GROUP_DATA) {
  2026. /* The caller should check if the block group is full. */
  2027. if (WARN_ON_ONCE(btrfs_zoned_bg_is_full(block_group))) {
  2028. ret = false;
  2029. goto out_unlock;
  2030. }
  2031. } else {
  2032. /* Since it is already written, it should have been active. */
  2033. WARN_ON_ONCE(block_group->meta_write_pointer != block_group->start);
  2034. }
  2035. for (i = 0; i < map->num_stripes; i++) {
  2036. struct btrfs_zoned_device_info *zinfo;
  2037. int reserved = 0;
  2038. device = map->stripes[i].dev;
  2039. physical = map->stripes[i].physical;
  2040. zinfo = device->zone_info;
  2041. if (!device->bdev)
  2042. continue;
  2043. if (zinfo->max_active_zones == 0)
  2044. continue;
  2045. if (is_data)
  2046. reserved = zinfo->reserved_active_zones;
  2047. /*
  2048. * For the data block group, leave active zones for one
  2049. * metadata block group and one system block group.
  2050. */
  2051. if (atomic_read(&zinfo->active_zones_left) <= reserved) {
  2052. ret = false;
  2053. goto out_unlock;
  2054. }
  2055. if (!btrfs_dev_set_active_zone(device, physical)) {
  2056. /* Cannot activate the zone */
  2057. ret = false;
  2058. goto out_unlock;
  2059. }
  2060. if (!is_data)
  2061. zinfo->reserved_active_zones--;
  2062. }
  2063. /* Successfully activated all the zones */
  2064. set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
  2065. spin_unlock(&block_group->lock);
  2066. /* For the active block group list */
  2067. btrfs_get_block_group(block_group);
  2068. list_add_tail(&block_group->active_bg_list, &fs_info->zone_active_bgs);
  2069. spin_unlock(&fs_info->zone_active_bgs_lock);
  2070. return true;
  2071. out_unlock:
  2072. spin_unlock(&block_group->lock);
  2073. spin_unlock(&fs_info->zone_active_bgs_lock);
  2074. return ret;
  2075. }
  2076. static void wait_eb_writebacks(struct btrfs_block_group *block_group)
  2077. {
  2078. struct btrfs_fs_info *fs_info = block_group->fs_info;
  2079. const u64 end = btrfs_block_group_end(block_group);
  2080. struct extent_buffer *eb;
  2081. unsigned long index, start = (block_group->start >> fs_info->nodesize_bits);
  2082. rcu_read_lock();
  2083. xa_for_each_start(&fs_info->buffer_tree, index, eb, start) {
  2084. if (eb->start < block_group->start)
  2085. continue;
  2086. if (eb->start >= end)
  2087. break;
  2088. rcu_read_unlock();
  2089. wait_on_extent_buffer_writeback(eb);
  2090. rcu_read_lock();
  2091. }
  2092. rcu_read_unlock();
  2093. }
  2094. static int call_zone_finish(struct btrfs_block_group *block_group,
  2095. struct btrfs_io_stripe *stripe)
  2096. {
  2097. struct btrfs_device *device = stripe->dev;
  2098. const u64 physical = stripe->physical;
  2099. struct btrfs_zoned_device_info *zinfo = device->zone_info;
  2100. int ret;
  2101. if (!device->bdev)
  2102. return 0;
  2103. if (zinfo->max_active_zones == 0)
  2104. return 0;
  2105. if (btrfs_dev_is_sequential(device, physical)) {
  2106. unsigned int nofs_flags;
  2107. nofs_flags = memalloc_nofs_save();
  2108. ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_FINISH,
  2109. physical >> SECTOR_SHIFT,
  2110. zinfo->zone_size >> SECTOR_SHIFT);
  2111. memalloc_nofs_restore(nofs_flags);
  2112. if (ret)
  2113. return ret;
  2114. }
  2115. if (!(block_group->flags & BTRFS_BLOCK_GROUP_DATA))
  2116. zinfo->reserved_active_zones++;
  2117. btrfs_dev_clear_active_zone(device, physical);
  2118. return 0;
  2119. }
  2120. static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_written)
  2121. {
  2122. struct btrfs_fs_info *fs_info = block_group->fs_info;
  2123. struct btrfs_chunk_map *map;
  2124. const bool is_metadata = (block_group->flags &
  2125. (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM));
  2126. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  2127. int ret = 0;
  2128. int i;
  2129. spin_lock(&block_group->lock);
  2130. if (!test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags)) {
  2131. spin_unlock(&block_group->lock);
  2132. return 0;
  2133. }
  2134. /* Check if we have unwritten allocated space */
  2135. if (is_metadata &&
  2136. block_group->start + block_group->alloc_offset > block_group->meta_write_pointer) {
  2137. spin_unlock(&block_group->lock);
  2138. return -EAGAIN;
  2139. }
  2140. /*
  2141. * If we are sure that the block group is full (= no more room left for
  2142. * new allocation) and the IO for the last usable block is completed, we
  2143. * don't need to wait for the other IOs. This holds because we ensure
  2144. * the sequential IO submissions using the ZONE_APPEND command for data
  2145. * and block_group->meta_write_pointer for metadata.
  2146. */
  2147. if (!fully_written) {
  2148. if (test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &block_group->runtime_flags)) {
  2149. spin_unlock(&block_group->lock);
  2150. return -EAGAIN;
  2151. }
  2152. spin_unlock(&block_group->lock);
  2153. ret = btrfs_inc_block_group_ro(block_group, false);
  2154. if (ret)
  2155. return ret;
  2156. /* Ensure all writes in this block group finish */
  2157. btrfs_wait_block_group_reservations(block_group);
  2158. /* No need to wait for NOCOW writers. Zoned mode does not allow that */
  2159. btrfs_wait_ordered_roots(fs_info, U64_MAX, block_group);
  2160. /* Wait for extent buffers to be written. */
  2161. if (is_metadata)
  2162. wait_eb_writebacks(block_group);
  2163. spin_lock(&block_group->lock);
  2164. /*
  2165. * Bail out if someone already deactivated the block group, or
  2166. * allocated space is left in the block group.
  2167. */
  2168. if (!test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
  2169. &block_group->runtime_flags)) {
  2170. spin_unlock(&block_group->lock);
  2171. btrfs_dec_block_group_ro(block_group);
  2172. return 0;
  2173. }
  2174. if (block_group->reserved ||
  2175. test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC,
  2176. &block_group->runtime_flags)) {
  2177. spin_unlock(&block_group->lock);
  2178. btrfs_dec_block_group_ro(block_group);
  2179. return -EAGAIN;
  2180. }
  2181. }
  2182. clear_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
  2183. block_group->alloc_offset = block_group->zone_capacity;
  2184. if (block_group->flags & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM))
  2185. block_group->meta_write_pointer = block_group->start +
  2186. block_group->zone_capacity;
  2187. block_group->free_space_ctl->free_space = 0;
  2188. btrfs_clear_treelog_bg(block_group);
  2189. btrfs_clear_data_reloc_bg(block_group);
  2190. spin_unlock(&block_group->lock);
  2191. down_read(&dev_replace->rwsem);
  2192. map = block_group->physical_map;
  2193. for (i = 0; i < map->num_stripes; i++) {
  2194. ret = call_zone_finish(block_group, &map->stripes[i]);
  2195. if (ret) {
  2196. up_read(&dev_replace->rwsem);
  2197. return ret;
  2198. }
  2199. }
  2200. up_read(&dev_replace->rwsem);
  2201. if (!fully_written)
  2202. btrfs_dec_block_group_ro(block_group);
  2203. spin_lock(&fs_info->zone_active_bgs_lock);
  2204. ASSERT(!list_empty(&block_group->active_bg_list));
  2205. list_del_init(&block_group->active_bg_list);
  2206. spin_unlock(&fs_info->zone_active_bgs_lock);
  2207. /* For active_bg_list */
  2208. btrfs_put_block_group(block_group);
  2209. clear_and_wake_up_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
  2210. return 0;
  2211. }
  2212. int btrfs_zone_finish(struct btrfs_block_group *block_group)
  2213. {
  2214. if (!btrfs_is_zoned(block_group->fs_info))
  2215. return 0;
  2216. return do_zone_finish(block_group, false);
  2217. }
  2218. bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices, u64 flags)
  2219. {
  2220. struct btrfs_fs_info *fs_info = fs_devices->fs_info;
  2221. struct btrfs_device *device;
  2222. bool ret = false;
  2223. if (!btrfs_is_zoned(fs_info))
  2224. return true;
  2225. if (test_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags))
  2226. return false;
  2227. /* Check if there is a device with active zones left */
  2228. mutex_lock(&fs_info->chunk_mutex);
  2229. spin_lock(&fs_info->zone_active_bgs_lock);
  2230. list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
  2231. struct btrfs_zoned_device_info *zinfo = device->zone_info;
  2232. int reserved = 0;
  2233. if (!device->bdev)
  2234. continue;
  2235. if (!zinfo->max_active_zones) {
  2236. ret = true;
  2237. break;
  2238. }
  2239. if (flags & BTRFS_BLOCK_GROUP_DATA)
  2240. reserved = zinfo->reserved_active_zones;
  2241. switch (flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
  2242. case 0: /* single */
  2243. ret = (atomic_read(&zinfo->active_zones_left) >= (1 + reserved));
  2244. break;
  2245. case BTRFS_BLOCK_GROUP_DUP:
  2246. ret = (atomic_read(&zinfo->active_zones_left) >= (2 + reserved));
  2247. break;
  2248. }
  2249. if (ret)
  2250. break;
  2251. }
  2252. spin_unlock(&fs_info->zone_active_bgs_lock);
  2253. mutex_unlock(&fs_info->chunk_mutex);
  2254. if (!ret)
  2255. set_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
  2256. return ret;
  2257. }
  2258. int btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, u64 logical, u64 length)
  2259. {
  2260. struct btrfs_block_group *block_group;
  2261. u64 min_alloc_bytes;
  2262. if (!btrfs_is_zoned(fs_info))
  2263. return 0;
  2264. block_group = btrfs_lookup_block_group(fs_info, logical);
  2265. if (WARN_ON_ONCE(!block_group))
  2266. return -ENOENT;
  2267. /* No MIXED_BG on zoned btrfs. */
  2268. if (block_group->flags & BTRFS_BLOCK_GROUP_DATA)
  2269. min_alloc_bytes = fs_info->sectorsize;
  2270. else
  2271. min_alloc_bytes = fs_info->nodesize;
  2272. /* Bail out if we can allocate more data from this block group. */
  2273. if (logical + length + min_alloc_bytes <=
  2274. block_group->start + block_group->zone_capacity)
  2275. goto out;
  2276. do_zone_finish(block_group, true);
  2277. out:
  2278. btrfs_put_block_group(block_group);
  2279. return 0;
  2280. }
  2281. static void btrfs_zone_finish_endio_workfn(struct work_struct *work)
  2282. {
  2283. int ret;
  2284. struct btrfs_block_group *bg =
  2285. container_of(work, struct btrfs_block_group, zone_finish_work);
  2286. wait_on_extent_buffer_writeback(bg->last_eb);
  2287. free_extent_buffer(bg->last_eb);
  2288. ret = do_zone_finish(bg, true);
  2289. if (ret)
  2290. btrfs_handle_fs_error(bg->fs_info, ret,
  2291. "Failed to finish block-group's zone");
  2292. btrfs_put_block_group(bg);
  2293. }
  2294. void btrfs_schedule_zone_finish_bg(struct btrfs_block_group *bg,
  2295. struct extent_buffer *eb)
  2296. {
  2297. if (!test_bit(BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE, &bg->runtime_flags) ||
  2298. eb->start + eb->len * 2 <= bg->start + bg->zone_capacity)
  2299. return;
  2300. if (WARN_ON(bg->zone_finish_work.func == btrfs_zone_finish_endio_workfn)) {
  2301. btrfs_err(bg->fs_info, "double scheduling of bg %llu zone finishing",
  2302. bg->start);
  2303. return;
  2304. }
  2305. /* For the work */
  2306. btrfs_get_block_group(bg);
  2307. refcount_inc(&eb->refs);
  2308. bg->last_eb = eb;
  2309. INIT_WORK(&bg->zone_finish_work, btrfs_zone_finish_endio_workfn);
  2310. queue_work(system_dfl_wq, &bg->zone_finish_work);
  2311. }
  2312. void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg)
  2313. {
  2314. struct btrfs_fs_info *fs_info = bg->fs_info;
  2315. spin_lock(&fs_info->relocation_bg_lock);
  2316. if (fs_info->data_reloc_bg == bg->start)
  2317. fs_info->data_reloc_bg = 0;
  2318. spin_unlock(&fs_info->relocation_bg_lock);
  2319. }
  2320. void btrfs_zoned_reserve_data_reloc_bg(struct btrfs_fs_info *fs_info)
  2321. {
  2322. struct btrfs_space_info *data_sinfo = fs_info->data_sinfo;
  2323. struct btrfs_space_info *space_info = data_sinfo;
  2324. struct btrfs_trans_handle *trans;
  2325. struct btrfs_block_group *bg;
  2326. struct list_head *bg_list;
  2327. u64 alloc_flags;
  2328. bool first = true;
  2329. bool did_chunk_alloc = false;
  2330. int index;
  2331. int ret;
  2332. if (!btrfs_is_zoned(fs_info))
  2333. return;
  2334. if (fs_info->data_reloc_bg)
  2335. return;
  2336. if (sb_rdonly(fs_info->sb))
  2337. return;
  2338. alloc_flags = btrfs_get_alloc_profile(fs_info, space_info->flags);
  2339. index = btrfs_bg_flags_to_raid_index(alloc_flags);
  2340. /* Scan the data space_info to find empty block groups. Take the second one. */
  2341. again:
  2342. bg_list = &space_info->block_groups[index];
  2343. list_for_each_entry(bg, bg_list, list) {
  2344. if (bg->alloc_offset != 0)
  2345. continue;
  2346. if (first) {
  2347. first = false;
  2348. continue;
  2349. }
  2350. if (space_info == data_sinfo) {
  2351. /* Migrate the block group to the data relocation space_info. */
  2352. struct btrfs_space_info *reloc_sinfo = data_sinfo->sub_group[0];
  2353. int factor;
  2354. ASSERT(reloc_sinfo->subgroup_id == BTRFS_SUB_GROUP_DATA_RELOC,
  2355. "reloc_sinfo->subgroup_id=%d", reloc_sinfo->subgroup_id);
  2356. factor = btrfs_bg_type_to_factor(bg->flags);
  2357. down_write(&space_info->groups_sem);
  2358. list_del_init(&bg->list);
  2359. /* We can assume this as we choose the second empty one. */
  2360. ASSERT(!list_empty(&space_info->block_groups[index]));
  2361. up_write(&space_info->groups_sem);
  2362. spin_lock(&space_info->lock);
  2363. space_info->total_bytes -= bg->length;
  2364. space_info->disk_total -= bg->length * factor;
  2365. space_info->disk_total -= bg->zone_unusable;
  2366. /* There is no allocation ever happened. */
  2367. ASSERT(bg->used == 0, "bg->used=%llu", bg->used);
  2368. /* No super block in a block group on the zoned setup. */
  2369. ASSERT(bg->bytes_super == 0, "bg->bytes_super=%llu", bg->bytes_super);
  2370. spin_unlock(&space_info->lock);
  2371. bg->space_info = reloc_sinfo;
  2372. if (reloc_sinfo->block_group_kobjs[index] == NULL)
  2373. btrfs_sysfs_add_block_group_type(bg);
  2374. btrfs_add_bg_to_space_info(fs_info, bg);
  2375. }
  2376. fs_info->data_reloc_bg = bg->start;
  2377. set_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &bg->runtime_flags);
  2378. btrfs_zone_activate(bg);
  2379. return;
  2380. }
  2381. if (did_chunk_alloc)
  2382. return;
  2383. trans = btrfs_join_transaction(fs_info->tree_root);
  2384. if (IS_ERR(trans))
  2385. return;
  2386. /* Allocate new BG in the data relocation space_info. */
  2387. space_info = data_sinfo->sub_group[0];
  2388. ASSERT(space_info->subgroup_id == BTRFS_SUB_GROUP_DATA_RELOC,
  2389. "space_info->subgroup_id=%d", space_info->subgroup_id);
  2390. ret = btrfs_chunk_alloc(trans, space_info, alloc_flags, CHUNK_ALLOC_FORCE);
  2391. btrfs_end_transaction(trans);
  2392. if (ret == 1) {
  2393. /*
  2394. * We allocated a new block group in the data relocation space_info. We
  2395. * can take that one.
  2396. */
  2397. first = false;
  2398. did_chunk_alloc = true;
  2399. goto again;
  2400. }
  2401. }
  2402. void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info)
  2403. {
  2404. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  2405. struct btrfs_device *device;
  2406. if (!btrfs_is_zoned(fs_info))
  2407. return;
  2408. mutex_lock(&fs_devices->device_list_mutex);
  2409. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  2410. if (device->zone_info) {
  2411. vfree(device->zone_info->zone_cache);
  2412. device->zone_info->zone_cache = NULL;
  2413. }
  2414. }
  2415. mutex_unlock(&fs_devices->device_list_mutex);
  2416. }
  2417. bool btrfs_zoned_should_reclaim(const struct btrfs_fs_info *fs_info)
  2418. {
  2419. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  2420. struct btrfs_device *device;
  2421. u64 total = btrfs_super_total_bytes(fs_info->super_copy);
  2422. u64 used = 0;
  2423. u64 factor;
  2424. ASSERT(btrfs_is_zoned(fs_info));
  2425. if (fs_info->bg_reclaim_threshold == 0)
  2426. return false;
  2427. mutex_lock(&fs_devices->device_list_mutex);
  2428. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  2429. if (!device->bdev)
  2430. continue;
  2431. used += device->bytes_used;
  2432. }
  2433. mutex_unlock(&fs_devices->device_list_mutex);
  2434. factor = div64_u64(used * 100, total);
  2435. return factor >= fs_info->bg_reclaim_threshold;
  2436. }
  2437. void btrfs_zoned_release_data_reloc_bg(struct btrfs_fs_info *fs_info, u64 logical,
  2438. u64 length)
  2439. {
  2440. struct btrfs_block_group *block_group;
  2441. if (!btrfs_is_zoned(fs_info))
  2442. return;
  2443. block_group = btrfs_lookup_block_group(fs_info, logical);
  2444. /* It should be called on a previous data relocation block group. */
  2445. ASSERT(block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA));
  2446. spin_lock(&block_group->lock);
  2447. if (!test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &block_group->runtime_flags))
  2448. goto out;
  2449. /* All relocation extents are written. */
  2450. if (block_group->start + block_group->alloc_offset == logical + length) {
  2451. /*
  2452. * Now, release this block group for further allocations and
  2453. * zone finish.
  2454. */
  2455. clear_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC,
  2456. &block_group->runtime_flags);
  2457. }
  2458. out:
  2459. spin_unlock(&block_group->lock);
  2460. btrfs_put_block_group(block_group);
  2461. }
  2462. int btrfs_zone_finish_one_bg(struct btrfs_fs_info *fs_info)
  2463. {
  2464. struct btrfs_block_group *block_group;
  2465. struct btrfs_block_group *min_bg = NULL;
  2466. u64 min_avail = U64_MAX;
  2467. int ret;
  2468. spin_lock(&fs_info->zone_active_bgs_lock);
  2469. list_for_each_entry(block_group, &fs_info->zone_active_bgs,
  2470. active_bg_list) {
  2471. u64 avail;
  2472. spin_lock(&block_group->lock);
  2473. if (block_group->reserved || block_group->alloc_offset == 0 ||
  2474. !(block_group->flags & BTRFS_BLOCK_GROUP_DATA) ||
  2475. test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &block_group->runtime_flags)) {
  2476. spin_unlock(&block_group->lock);
  2477. continue;
  2478. }
  2479. avail = block_group->zone_capacity - block_group->alloc_offset;
  2480. if (min_avail > avail) {
  2481. if (min_bg)
  2482. btrfs_put_block_group(min_bg);
  2483. min_bg = block_group;
  2484. min_avail = avail;
  2485. btrfs_get_block_group(min_bg);
  2486. }
  2487. spin_unlock(&block_group->lock);
  2488. }
  2489. spin_unlock(&fs_info->zone_active_bgs_lock);
  2490. if (!min_bg)
  2491. return 0;
  2492. ret = btrfs_zone_finish(min_bg);
  2493. btrfs_put_block_group(min_bg);
  2494. return ret < 0 ? ret : 1;
  2495. }
  2496. int btrfs_zoned_activate_one_bg(struct btrfs_space_info *space_info, bool do_finish)
  2497. {
  2498. struct btrfs_fs_info *fs_info = space_info->fs_info;
  2499. struct btrfs_block_group *bg;
  2500. int index;
  2501. if (!btrfs_is_zoned(fs_info) || (space_info->flags & BTRFS_BLOCK_GROUP_DATA))
  2502. return 0;
  2503. for (;;) {
  2504. int ret;
  2505. bool need_finish = false;
  2506. down_read(&space_info->groups_sem);
  2507. for (index = 0; index < BTRFS_NR_RAID_TYPES; index++) {
  2508. list_for_each_entry(bg, &space_info->block_groups[index],
  2509. list) {
  2510. if (!spin_trylock(&bg->lock))
  2511. continue;
  2512. if (btrfs_zoned_bg_is_full(bg) ||
  2513. test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
  2514. &bg->runtime_flags)) {
  2515. spin_unlock(&bg->lock);
  2516. continue;
  2517. }
  2518. spin_unlock(&bg->lock);
  2519. if (btrfs_zone_activate(bg)) {
  2520. up_read(&space_info->groups_sem);
  2521. return 1;
  2522. }
  2523. need_finish = true;
  2524. }
  2525. }
  2526. up_read(&space_info->groups_sem);
  2527. if (!do_finish || !need_finish)
  2528. break;
  2529. ret = btrfs_zone_finish_one_bg(fs_info);
  2530. if (ret == 0)
  2531. break;
  2532. if (ret < 0)
  2533. return ret;
  2534. }
  2535. return 0;
  2536. }
  2537. /*
  2538. * Reserve zones for one metadata block group, one tree-log block group, and one
  2539. * system block group.
  2540. */
  2541. void btrfs_check_active_zone_reservation(struct btrfs_fs_info *fs_info)
  2542. {
  2543. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  2544. struct btrfs_block_group *block_group;
  2545. struct btrfs_device *device;
  2546. /* Reserve zones for normal SINGLE metadata and tree-log block group. */
  2547. unsigned int metadata_reserve = 2;
  2548. /* Reserve a zone for SINGLE system block group. */
  2549. unsigned int system_reserve = 1;
  2550. if (!test_bit(BTRFS_FS_ACTIVE_ZONE_TRACKING, &fs_info->flags))
  2551. return;
  2552. /*
  2553. * This function is called from the mount context. So, there is no
  2554. * parallel process touching the bits. No need for read_seqretry().
  2555. */
  2556. if (fs_info->avail_metadata_alloc_bits & BTRFS_BLOCK_GROUP_DUP)
  2557. metadata_reserve = 4;
  2558. if (fs_info->avail_system_alloc_bits & BTRFS_BLOCK_GROUP_DUP)
  2559. system_reserve = 2;
  2560. /* Apply the reservation on all the devices. */
  2561. mutex_lock(&fs_devices->device_list_mutex);
  2562. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  2563. if (!device->bdev)
  2564. continue;
  2565. device->zone_info->reserved_active_zones =
  2566. metadata_reserve + system_reserve;
  2567. }
  2568. mutex_unlock(&fs_devices->device_list_mutex);
  2569. /* Release reservation for currently active block groups. */
  2570. spin_lock(&fs_info->zone_active_bgs_lock);
  2571. list_for_each_entry(block_group, &fs_info->zone_active_bgs, active_bg_list) {
  2572. struct btrfs_chunk_map *map = block_group->physical_map;
  2573. if (!(block_group->flags &
  2574. (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM)))
  2575. continue;
  2576. for (int i = 0; i < map->num_stripes; i++)
  2577. map->stripes[i].dev->zone_info->reserved_active_zones--;
  2578. }
  2579. spin_unlock(&fs_info->zone_active_bgs_lock);
  2580. }
  2581. /*
  2582. * Reset the zones of unused block groups from @space_info->bytes_zone_unusable.
  2583. *
  2584. * @space_info: the space to work on
  2585. * @num_bytes: targeting reclaim bytes
  2586. *
  2587. * This one resets the zones of a block group, so we can reuse the region
  2588. * without removing the block group. On the other hand, btrfs_delete_unused_bgs()
  2589. * just removes a block group and frees up the underlying zones. So, we still
  2590. * need to allocate a new block group to reuse the zones.
  2591. *
  2592. * Resetting is faster than deleting/recreating a block group. It is similar
  2593. * to freeing the logical space on the regular mode. However, we cannot change
  2594. * the block group's profile with this operation.
  2595. */
  2596. int btrfs_reset_unused_block_groups(struct btrfs_space_info *space_info, u64 num_bytes)
  2597. {
  2598. struct btrfs_fs_info *fs_info = space_info->fs_info;
  2599. const sector_t zone_size_sectors = fs_info->zone_size >> SECTOR_SHIFT;
  2600. if (!btrfs_is_zoned(fs_info))
  2601. return 0;
  2602. while (num_bytes > 0) {
  2603. struct btrfs_chunk_map *map;
  2604. struct btrfs_block_group *bg = NULL;
  2605. bool found = false;
  2606. u64 reclaimed = 0;
  2607. /*
  2608. * Here, we choose a fully zone_unusable block group. It's
  2609. * technically possible to reset a partly zone_unusable block
  2610. * group, which still has some free space left. However,
  2611. * handling that needs to cope with the allocation side, which
  2612. * makes the logic more complex. So, let's handle the easy case
  2613. * for now.
  2614. */
  2615. spin_lock(&fs_info->unused_bgs_lock);
  2616. list_for_each_entry(bg, &fs_info->unused_bgs, bg_list) {
  2617. if ((bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK) != space_info->flags)
  2618. continue;
  2619. /*
  2620. * Use trylock to avoid locking order violation. In
  2621. * btrfs_reclaim_bgs_work(), the lock order is
  2622. * &bg->lock -> &fs_info->unused_bgs_lock. We skip a
  2623. * block group if we cannot take its lock.
  2624. */
  2625. if (!spin_trylock(&bg->lock))
  2626. continue;
  2627. if (btrfs_is_block_group_used(bg) || bg->zone_unusable < bg->length) {
  2628. spin_unlock(&bg->lock);
  2629. continue;
  2630. }
  2631. spin_unlock(&bg->lock);
  2632. found = true;
  2633. break;
  2634. }
  2635. if (!found) {
  2636. spin_unlock(&fs_info->unused_bgs_lock);
  2637. return 0;
  2638. }
  2639. list_del_init(&bg->bg_list);
  2640. btrfs_put_block_group(bg);
  2641. spin_unlock(&fs_info->unused_bgs_lock);
  2642. /*
  2643. * Since the block group is fully zone_unusable and we cannot
  2644. * allocate from this block group anymore, we don't need to set
  2645. * this block group read-only.
  2646. */
  2647. down_read(&fs_info->dev_replace.rwsem);
  2648. map = bg->physical_map;
  2649. for (int i = 0; i < map->num_stripes; i++) {
  2650. struct btrfs_io_stripe *stripe = &map->stripes[i];
  2651. unsigned int nofs_flags;
  2652. int ret;
  2653. nofs_flags = memalloc_nofs_save();
  2654. ret = blkdev_zone_mgmt(stripe->dev->bdev, REQ_OP_ZONE_RESET,
  2655. stripe->physical >> SECTOR_SHIFT,
  2656. zone_size_sectors);
  2657. memalloc_nofs_restore(nofs_flags);
  2658. if (ret) {
  2659. up_read(&fs_info->dev_replace.rwsem);
  2660. return ret;
  2661. }
  2662. }
  2663. up_read(&fs_info->dev_replace.rwsem);
  2664. spin_lock(&space_info->lock);
  2665. spin_lock(&bg->lock);
  2666. ASSERT(!btrfs_is_block_group_used(bg));
  2667. if (bg->ro) {
  2668. spin_unlock(&bg->lock);
  2669. spin_unlock(&space_info->lock);
  2670. continue;
  2671. }
  2672. reclaimed = bg->alloc_offset;
  2673. bg->zone_unusable = bg->length - bg->zone_capacity;
  2674. bg->alloc_offset = 0;
  2675. /*
  2676. * This holds because we currently reset fully used then freed
  2677. * block group.
  2678. */
  2679. ASSERT(reclaimed == bg->zone_capacity,
  2680. "reclaimed=%llu bg->zone_capacity=%llu", reclaimed, bg->zone_capacity);
  2681. bg->free_space_ctl->free_space += reclaimed;
  2682. space_info->bytes_zone_unusable -= reclaimed;
  2683. spin_unlock(&bg->lock);
  2684. btrfs_return_free_space(space_info, reclaimed);
  2685. spin_unlock(&space_info->lock);
  2686. if (num_bytes <= reclaimed)
  2687. break;
  2688. num_bytes -= reclaimed;
  2689. }
  2690. return 0;
  2691. }
  2692. void btrfs_show_zoned_stats(struct btrfs_fs_info *fs_info, struct seq_file *seq)
  2693. {
  2694. struct btrfs_block_group *bg;
  2695. u64 data_reloc_bg;
  2696. u64 treelog_bg;
  2697. seq_puts(seq, "\n zoned statistics:\n");
  2698. spin_lock(&fs_info->zone_active_bgs_lock);
  2699. seq_printf(seq, "\tactive block-groups: %zu\n",
  2700. list_count_nodes(&fs_info->zone_active_bgs));
  2701. spin_unlock(&fs_info->zone_active_bgs_lock);
  2702. spin_lock(&fs_info->unused_bgs_lock);
  2703. seq_printf(seq, "\t reclaimable: %zu\n",
  2704. list_count_nodes(&fs_info->reclaim_bgs));
  2705. seq_printf(seq, "\t unused: %zu\n", list_count_nodes(&fs_info->unused_bgs));
  2706. spin_unlock(&fs_info->unused_bgs_lock);
  2707. seq_printf(seq,"\t need reclaim: %s\n",
  2708. str_true_false(btrfs_zoned_should_reclaim(fs_info)));
  2709. data_reloc_bg = data_race(fs_info->data_reloc_bg);
  2710. if (data_reloc_bg)
  2711. seq_printf(seq, "\tdata relocation block-group: %llu\n",
  2712. data_reloc_bg);
  2713. treelog_bg = data_race(fs_info->treelog_bg);
  2714. if (treelog_bg)
  2715. seq_printf(seq, "\ttree-log block-group: %llu\n", treelog_bg);
  2716. spin_lock(&fs_info->zone_active_bgs_lock);
  2717. seq_puts(seq, "\tactive zones:\n");
  2718. list_for_each_entry(bg, &fs_info->zone_active_bgs, active_bg_list) {
  2719. u64 start;
  2720. u64 alloc_offset;
  2721. u64 used;
  2722. u64 reserved;
  2723. u64 zone_unusable;
  2724. const char *typestr = btrfs_space_info_type_str(bg->space_info);
  2725. spin_lock(&bg->lock);
  2726. start = bg->start;
  2727. alloc_offset = bg->alloc_offset;
  2728. used = bg->used;
  2729. reserved = bg->reserved;
  2730. zone_unusable = bg->zone_unusable;
  2731. spin_unlock(&bg->lock);
  2732. seq_printf(seq,
  2733. "\t start: %llu, wp: %llu used: %llu, reserved: %llu, unusable: %llu (%s)\n",
  2734. start, alloc_offset, used, reserved, zone_unusable, typestr);
  2735. }
  2736. spin_unlock(&fs_info->zone_active_bgs_lock);
  2737. }