zoned.c 71 KB

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