blk-zoned.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Zoned block device handling
  4. *
  5. * Copyright (c) 2015, Hannes Reinecke
  6. * Copyright (c) 2015, SUSE Linux GmbH
  7. *
  8. * Copyright (c) 2016, Damien Le Moal
  9. * Copyright (c) 2016, Western Digital
  10. * Copyright (c) 2024, Western Digital Corporation or its affiliates.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/blkdev.h>
  15. #include <linux/blk-mq.h>
  16. #include <linux/mm.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/sched/mm.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/refcount.h>
  21. #include <linux/mempool.h>
  22. #include "blk.h"
  23. #include "blk-mq-sched.h"
  24. #include "blk-mq-debugfs.h"
  25. #define ZONE_COND_NAME(name) [BLK_ZONE_COND_##name] = #name
  26. static const char *const zone_cond_name[] = {
  27. ZONE_COND_NAME(NOT_WP),
  28. ZONE_COND_NAME(EMPTY),
  29. ZONE_COND_NAME(IMP_OPEN),
  30. ZONE_COND_NAME(EXP_OPEN),
  31. ZONE_COND_NAME(CLOSED),
  32. ZONE_COND_NAME(READONLY),
  33. ZONE_COND_NAME(FULL),
  34. ZONE_COND_NAME(OFFLINE),
  35. };
  36. #undef ZONE_COND_NAME
  37. /*
  38. * Per-zone write plug.
  39. * @node: hlist_node structure for managing the plug using a hash table.
  40. * @ref: Zone write plug reference counter. A zone write plug reference is
  41. * always at least 1 when the plug is hashed in the disk plug hash table.
  42. * The reference is incremented whenever a new BIO needing plugging is
  43. * submitted and when a function needs to manipulate a plug. The
  44. * reference count is decremented whenever a plugged BIO completes and
  45. * when a function that referenced the plug returns. The initial
  46. * reference is dropped whenever the zone of the zone write plug is reset,
  47. * finished and when the zone becomes full (last write BIO to the zone
  48. * completes).
  49. * @lock: Spinlock to atomically manipulate the plug.
  50. * @flags: Flags indicating the plug state.
  51. * @zone_no: The number of the zone the plug is managing.
  52. * @wp_offset: The zone write pointer location relative to the start of the zone
  53. * as a number of 512B sectors.
  54. * @bio_list: The list of BIOs that are currently plugged.
  55. * @bio_work: Work struct to handle issuing of plugged BIOs
  56. * @rcu_head: RCU head to free zone write plugs with an RCU grace period.
  57. * @disk: The gendisk the plug belongs to.
  58. */
  59. struct blk_zone_wplug {
  60. struct hlist_node node;
  61. refcount_t ref;
  62. spinlock_t lock;
  63. unsigned int flags;
  64. unsigned int zone_no;
  65. unsigned int wp_offset;
  66. struct bio_list bio_list;
  67. struct work_struct bio_work;
  68. struct rcu_head rcu_head;
  69. struct gendisk *disk;
  70. };
  71. /*
  72. * Zone write plug flags bits:
  73. * - BLK_ZONE_WPLUG_PLUGGED: Indicates that the zone write plug is plugged,
  74. * that is, that write BIOs are being throttled due to a write BIO already
  75. * being executed or the zone write plug bio list is not empty.
  76. * - BLK_ZONE_WPLUG_NEED_WP_UPDATE: Indicates that we lost track of a zone
  77. * write pointer offset and need to update it.
  78. * - BLK_ZONE_WPLUG_UNHASHED: Indicates that the zone write plug was removed
  79. * from the disk hash table and that the initial reference to the zone
  80. * write plug set when the plug was first added to the hash table has been
  81. * dropped. This flag is set when a zone is reset, finished or become full,
  82. * to prevent new references to the zone write plug to be taken for
  83. * newly incoming BIOs. A zone write plug flagged with this flag will be
  84. * freed once all remaining references from BIOs or functions are dropped.
  85. */
  86. #define BLK_ZONE_WPLUG_PLUGGED (1U << 0)
  87. #define BLK_ZONE_WPLUG_NEED_WP_UPDATE (1U << 1)
  88. #define BLK_ZONE_WPLUG_UNHASHED (1U << 2)
  89. /**
  90. * blk_zone_cond_str - Return string XXX in BLK_ZONE_COND_XXX.
  91. * @zone_cond: BLK_ZONE_COND_XXX.
  92. *
  93. * Description: Centralize block layer function to convert BLK_ZONE_COND_XXX
  94. * into string format. Useful in the debugging and tracing zone conditions. For
  95. * invalid BLK_ZONE_COND_XXX it returns string "UNKNOWN".
  96. */
  97. const char *blk_zone_cond_str(enum blk_zone_cond zone_cond)
  98. {
  99. static const char *zone_cond_str = "UNKNOWN";
  100. if (zone_cond < ARRAY_SIZE(zone_cond_name) && zone_cond_name[zone_cond])
  101. zone_cond_str = zone_cond_name[zone_cond];
  102. return zone_cond_str;
  103. }
  104. EXPORT_SYMBOL_GPL(blk_zone_cond_str);
  105. struct disk_report_zones_cb_args {
  106. struct gendisk *disk;
  107. report_zones_cb user_cb;
  108. void *user_data;
  109. };
  110. static void disk_zone_wplug_sync_wp_offset(struct gendisk *disk,
  111. struct blk_zone *zone);
  112. static int disk_report_zones_cb(struct blk_zone *zone, unsigned int idx,
  113. void *data)
  114. {
  115. struct disk_report_zones_cb_args *args = data;
  116. struct gendisk *disk = args->disk;
  117. if (disk->zone_wplugs_hash)
  118. disk_zone_wplug_sync_wp_offset(disk, zone);
  119. if (!args->user_cb)
  120. return 0;
  121. return args->user_cb(zone, idx, args->user_data);
  122. }
  123. /**
  124. * blkdev_report_zones - Get zones information
  125. * @bdev: Target block device
  126. * @sector: Sector from which to report zones
  127. * @nr_zones: Maximum number of zones to report
  128. * @cb: Callback function called for each reported zone
  129. * @data: Private data for the callback
  130. *
  131. * Description:
  132. * Get zone information starting from the zone containing @sector for at most
  133. * @nr_zones, and call @cb for each zone reported by the device.
  134. * To report all zones in a device starting from @sector, the BLK_ALL_ZONES
  135. * constant can be passed to @nr_zones.
  136. * Returns the number of zones reported by the device, or a negative errno
  137. * value in case of failure.
  138. *
  139. * Note: The caller must use memalloc_noXX_save/restore() calls to control
  140. * memory allocations done within this function.
  141. */
  142. int blkdev_report_zones(struct block_device *bdev, sector_t sector,
  143. unsigned int nr_zones, report_zones_cb cb, void *data)
  144. {
  145. struct gendisk *disk = bdev->bd_disk;
  146. sector_t capacity = get_capacity(disk);
  147. struct disk_report_zones_cb_args args = {
  148. .disk = disk,
  149. .user_cb = cb,
  150. .user_data = data,
  151. };
  152. if (!bdev_is_zoned(bdev) || WARN_ON_ONCE(!disk->fops->report_zones))
  153. return -EOPNOTSUPP;
  154. if (!nr_zones || sector >= capacity)
  155. return 0;
  156. return disk->fops->report_zones(disk, sector, nr_zones,
  157. disk_report_zones_cb, &args);
  158. }
  159. EXPORT_SYMBOL_GPL(blkdev_report_zones);
  160. static int blkdev_zone_reset_all(struct block_device *bdev)
  161. {
  162. struct bio bio;
  163. bio_init(&bio, bdev, NULL, 0, REQ_OP_ZONE_RESET_ALL | REQ_SYNC);
  164. return submit_bio_wait(&bio);
  165. }
  166. /**
  167. * blkdev_zone_mgmt - Execute a zone management operation on a range of zones
  168. * @bdev: Target block device
  169. * @op: Operation to be performed on the zones
  170. * @sector: Start sector of the first zone to operate on
  171. * @nr_sectors: Number of sectors, should be at least the length of one zone and
  172. * must be zone size aligned.
  173. *
  174. * Description:
  175. * Perform the specified operation on the range of zones specified by
  176. * @sector..@sector+@nr_sectors. Specifying the entire disk sector range
  177. * is valid, but the specified range should not contain conventional zones.
  178. * The operation to execute on each zone can be a zone reset, open, close
  179. * or finish request.
  180. */
  181. int blkdev_zone_mgmt(struct block_device *bdev, enum req_op op,
  182. sector_t sector, sector_t nr_sectors)
  183. {
  184. sector_t zone_sectors = bdev_zone_sectors(bdev);
  185. sector_t capacity = bdev_nr_sectors(bdev);
  186. sector_t end_sector = sector + nr_sectors;
  187. struct bio *bio = NULL;
  188. int ret = 0;
  189. if (!bdev_is_zoned(bdev))
  190. return -EOPNOTSUPP;
  191. if (bdev_read_only(bdev))
  192. return -EPERM;
  193. if (!op_is_zone_mgmt(op))
  194. return -EOPNOTSUPP;
  195. if (end_sector <= sector || end_sector > capacity)
  196. /* Out of range */
  197. return -EINVAL;
  198. /* Check alignment (handle eventual smaller last zone) */
  199. if (!bdev_is_zone_start(bdev, sector))
  200. return -EINVAL;
  201. if (!bdev_is_zone_start(bdev, nr_sectors) && end_sector != capacity)
  202. return -EINVAL;
  203. /*
  204. * In the case of a zone reset operation over all zones, use
  205. * REQ_OP_ZONE_RESET_ALL.
  206. */
  207. if (op == REQ_OP_ZONE_RESET && sector == 0 && nr_sectors == capacity)
  208. return blkdev_zone_reset_all(bdev);
  209. while (sector < end_sector) {
  210. bio = blk_next_bio(bio, bdev, 0, op | REQ_SYNC, GFP_KERNEL);
  211. bio->bi_iter.bi_sector = sector;
  212. sector += zone_sectors;
  213. /* This may take a while, so be nice to others */
  214. cond_resched();
  215. }
  216. ret = submit_bio_wait(bio);
  217. bio_put(bio);
  218. return ret;
  219. }
  220. EXPORT_SYMBOL_GPL(blkdev_zone_mgmt);
  221. struct zone_report_args {
  222. struct blk_zone __user *zones;
  223. };
  224. static int blkdev_copy_zone_to_user(struct blk_zone *zone, unsigned int idx,
  225. void *data)
  226. {
  227. struct zone_report_args *args = data;
  228. if (copy_to_user(&args->zones[idx], zone, sizeof(struct blk_zone)))
  229. return -EFAULT;
  230. return 0;
  231. }
  232. /*
  233. * BLKREPORTZONE ioctl processing.
  234. * Called from blkdev_ioctl.
  235. */
  236. int blkdev_report_zones_ioctl(struct block_device *bdev, unsigned int cmd,
  237. unsigned long arg)
  238. {
  239. void __user *argp = (void __user *)arg;
  240. struct zone_report_args args;
  241. struct blk_zone_report rep;
  242. int ret;
  243. if (!argp)
  244. return -EINVAL;
  245. if (!bdev_is_zoned(bdev))
  246. return -ENOTTY;
  247. if (copy_from_user(&rep, argp, sizeof(struct blk_zone_report)))
  248. return -EFAULT;
  249. if (!rep.nr_zones)
  250. return -EINVAL;
  251. args.zones = argp + sizeof(struct blk_zone_report);
  252. ret = blkdev_report_zones(bdev, rep.sector, rep.nr_zones,
  253. blkdev_copy_zone_to_user, &args);
  254. if (ret < 0)
  255. return ret;
  256. rep.nr_zones = ret;
  257. rep.flags = BLK_ZONE_REP_CAPACITY;
  258. if (copy_to_user(argp, &rep, sizeof(struct blk_zone_report)))
  259. return -EFAULT;
  260. return 0;
  261. }
  262. static int blkdev_truncate_zone_range(struct block_device *bdev,
  263. blk_mode_t mode, const struct blk_zone_range *zrange)
  264. {
  265. loff_t start, end;
  266. if (zrange->sector + zrange->nr_sectors <= zrange->sector ||
  267. zrange->sector + zrange->nr_sectors > get_capacity(bdev->bd_disk))
  268. /* Out of range */
  269. return -EINVAL;
  270. start = zrange->sector << SECTOR_SHIFT;
  271. end = ((zrange->sector + zrange->nr_sectors) << SECTOR_SHIFT) - 1;
  272. return truncate_bdev_range(bdev, mode, start, end);
  273. }
  274. /*
  275. * BLKRESETZONE, BLKOPENZONE, BLKCLOSEZONE and BLKFINISHZONE ioctl processing.
  276. * Called from blkdev_ioctl.
  277. */
  278. int blkdev_zone_mgmt_ioctl(struct block_device *bdev, blk_mode_t mode,
  279. unsigned int cmd, unsigned long arg)
  280. {
  281. void __user *argp = (void __user *)arg;
  282. struct blk_zone_range zrange;
  283. enum req_op op;
  284. int ret;
  285. if (!argp)
  286. return -EINVAL;
  287. if (!bdev_is_zoned(bdev))
  288. return -ENOTTY;
  289. if (!(mode & BLK_OPEN_WRITE))
  290. return -EBADF;
  291. if (copy_from_user(&zrange, argp, sizeof(struct blk_zone_range)))
  292. return -EFAULT;
  293. switch (cmd) {
  294. case BLKRESETZONE:
  295. op = REQ_OP_ZONE_RESET;
  296. /* Invalidate the page cache, including dirty pages. */
  297. inode_lock(bdev->bd_mapping->host);
  298. filemap_invalidate_lock(bdev->bd_mapping);
  299. ret = blkdev_truncate_zone_range(bdev, mode, &zrange);
  300. if (ret)
  301. goto fail;
  302. break;
  303. case BLKOPENZONE:
  304. op = REQ_OP_ZONE_OPEN;
  305. break;
  306. case BLKCLOSEZONE:
  307. op = REQ_OP_ZONE_CLOSE;
  308. break;
  309. case BLKFINISHZONE:
  310. op = REQ_OP_ZONE_FINISH;
  311. break;
  312. default:
  313. return -ENOTTY;
  314. }
  315. ret = blkdev_zone_mgmt(bdev, op, zrange.sector, zrange.nr_sectors);
  316. fail:
  317. if (cmd == BLKRESETZONE) {
  318. filemap_invalidate_unlock(bdev->bd_mapping);
  319. inode_unlock(bdev->bd_mapping->host);
  320. }
  321. return ret;
  322. }
  323. static inline bool disk_zone_is_conv(struct gendisk *disk, sector_t sector)
  324. {
  325. unsigned long *bitmap;
  326. bool is_conv;
  327. rcu_read_lock();
  328. bitmap = rcu_dereference(disk->conv_zones_bitmap);
  329. is_conv = bitmap && test_bit(disk_zone_no(disk, sector), bitmap);
  330. rcu_read_unlock();
  331. return is_conv;
  332. }
  333. static bool disk_zone_is_last(struct gendisk *disk, struct blk_zone *zone)
  334. {
  335. return zone->start + zone->len >= get_capacity(disk);
  336. }
  337. static bool disk_zone_is_full(struct gendisk *disk,
  338. unsigned int zno, unsigned int offset_in_zone)
  339. {
  340. if (zno < disk->nr_zones - 1)
  341. return offset_in_zone >= disk->zone_capacity;
  342. return offset_in_zone >= disk->last_zone_capacity;
  343. }
  344. static bool disk_zone_wplug_is_full(struct gendisk *disk,
  345. struct blk_zone_wplug *zwplug)
  346. {
  347. return disk_zone_is_full(disk, zwplug->zone_no, zwplug->wp_offset);
  348. }
  349. static bool disk_insert_zone_wplug(struct gendisk *disk,
  350. struct blk_zone_wplug *zwplug)
  351. {
  352. struct blk_zone_wplug *zwplg;
  353. unsigned long flags;
  354. unsigned int idx =
  355. hash_32(zwplug->zone_no, disk->zone_wplugs_hash_bits);
  356. /*
  357. * Add the new zone write plug to the hash table, but carefully as we
  358. * are racing with other submission context, so we may already have a
  359. * zone write plug for the same zone.
  360. */
  361. spin_lock_irqsave(&disk->zone_wplugs_lock, flags);
  362. hlist_for_each_entry_rcu(zwplg, &disk->zone_wplugs_hash[idx], node) {
  363. if (zwplg->zone_no == zwplug->zone_no) {
  364. spin_unlock_irqrestore(&disk->zone_wplugs_lock, flags);
  365. return false;
  366. }
  367. }
  368. hlist_add_head_rcu(&zwplug->node, &disk->zone_wplugs_hash[idx]);
  369. atomic_inc(&disk->nr_zone_wplugs);
  370. spin_unlock_irqrestore(&disk->zone_wplugs_lock, flags);
  371. return true;
  372. }
  373. static struct blk_zone_wplug *disk_get_hashed_zone_wplug(struct gendisk *disk,
  374. sector_t sector)
  375. {
  376. unsigned int zno = disk_zone_no(disk, sector);
  377. unsigned int idx = hash_32(zno, disk->zone_wplugs_hash_bits);
  378. struct blk_zone_wplug *zwplug;
  379. rcu_read_lock();
  380. hlist_for_each_entry_rcu(zwplug, &disk->zone_wplugs_hash[idx], node) {
  381. if (zwplug->zone_no == zno &&
  382. refcount_inc_not_zero(&zwplug->ref)) {
  383. rcu_read_unlock();
  384. return zwplug;
  385. }
  386. }
  387. rcu_read_unlock();
  388. return NULL;
  389. }
  390. static inline struct blk_zone_wplug *disk_get_zone_wplug(struct gendisk *disk,
  391. sector_t sector)
  392. {
  393. if (!atomic_read(&disk->nr_zone_wplugs))
  394. return NULL;
  395. return disk_get_hashed_zone_wplug(disk, sector);
  396. }
  397. static void disk_free_zone_wplug_rcu(struct rcu_head *rcu_head)
  398. {
  399. struct blk_zone_wplug *zwplug =
  400. container_of(rcu_head, struct blk_zone_wplug, rcu_head);
  401. mempool_free(zwplug, zwplug->disk->zone_wplugs_pool);
  402. }
  403. static inline void disk_put_zone_wplug(struct blk_zone_wplug *zwplug)
  404. {
  405. if (refcount_dec_and_test(&zwplug->ref)) {
  406. WARN_ON_ONCE(!bio_list_empty(&zwplug->bio_list));
  407. WARN_ON_ONCE(zwplug->flags & BLK_ZONE_WPLUG_PLUGGED);
  408. WARN_ON_ONCE(!(zwplug->flags & BLK_ZONE_WPLUG_UNHASHED));
  409. call_rcu(&zwplug->rcu_head, disk_free_zone_wplug_rcu);
  410. }
  411. }
  412. static inline bool disk_should_remove_zone_wplug(struct gendisk *disk,
  413. struct blk_zone_wplug *zwplug)
  414. {
  415. /* If the zone write plug was already removed, we are done. */
  416. if (zwplug->flags & BLK_ZONE_WPLUG_UNHASHED)
  417. return false;
  418. /* If the zone write plug is still plugged, it cannot be removed. */
  419. if (zwplug->flags & BLK_ZONE_WPLUG_PLUGGED)
  420. return false;
  421. /*
  422. * Completions of BIOs with blk_zone_write_plug_bio_endio() may
  423. * happen after handling a request completion with
  424. * blk_zone_write_plug_finish_request() (e.g. with split BIOs
  425. * that are chained). In such case, disk_zone_wplug_unplug_bio()
  426. * should not attempt to remove the zone write plug until all BIO
  427. * completions are seen. Check by looking at the zone write plug
  428. * reference count, which is 2 when the plug is unused (one reference
  429. * taken when the plug was allocated and another reference taken by the
  430. * caller context).
  431. */
  432. if (refcount_read(&zwplug->ref) > 2)
  433. return false;
  434. /* We can remove zone write plugs for zones that are empty or full. */
  435. return !zwplug->wp_offset || disk_zone_wplug_is_full(disk, zwplug);
  436. }
  437. static void disk_remove_zone_wplug(struct gendisk *disk,
  438. struct blk_zone_wplug *zwplug)
  439. {
  440. unsigned long flags;
  441. /* If the zone write plug was already removed, we have nothing to do. */
  442. if (zwplug->flags & BLK_ZONE_WPLUG_UNHASHED)
  443. return;
  444. /*
  445. * Mark the zone write plug as unhashed and drop the extra reference we
  446. * took when the plug was inserted in the hash table.
  447. */
  448. zwplug->flags |= BLK_ZONE_WPLUG_UNHASHED;
  449. spin_lock_irqsave(&disk->zone_wplugs_lock, flags);
  450. hlist_del_init_rcu(&zwplug->node);
  451. atomic_dec(&disk->nr_zone_wplugs);
  452. spin_unlock_irqrestore(&disk->zone_wplugs_lock, flags);
  453. disk_put_zone_wplug(zwplug);
  454. }
  455. static void blk_zone_wplug_bio_work(struct work_struct *work);
  456. /*
  457. * Get a reference on the write plug for the zone containing @sector.
  458. * If the plug does not exist, it is allocated and hashed.
  459. * Return a pointer to the zone write plug with the plug spinlock held.
  460. */
  461. static struct blk_zone_wplug *disk_get_and_lock_zone_wplug(struct gendisk *disk,
  462. sector_t sector, gfp_t gfp_mask,
  463. unsigned long *flags)
  464. {
  465. unsigned int zno = disk_zone_no(disk, sector);
  466. struct blk_zone_wplug *zwplug;
  467. again:
  468. zwplug = disk_get_zone_wplug(disk, sector);
  469. if (zwplug) {
  470. /*
  471. * Check that a BIO completion or a zone reset or finish
  472. * operation has not already removed the zone write plug from
  473. * the hash table and dropped its reference count. In such case,
  474. * we need to get a new plug so start over from the beginning.
  475. */
  476. spin_lock_irqsave(&zwplug->lock, *flags);
  477. if (zwplug->flags & BLK_ZONE_WPLUG_UNHASHED) {
  478. spin_unlock_irqrestore(&zwplug->lock, *flags);
  479. disk_put_zone_wplug(zwplug);
  480. goto again;
  481. }
  482. return zwplug;
  483. }
  484. /*
  485. * Allocate and initialize a zone write plug with an extra reference
  486. * so that it is not freed when the zone write plug becomes idle without
  487. * the zone being full.
  488. */
  489. zwplug = mempool_alloc(disk->zone_wplugs_pool, gfp_mask);
  490. if (!zwplug)
  491. return NULL;
  492. INIT_HLIST_NODE(&zwplug->node);
  493. refcount_set(&zwplug->ref, 2);
  494. spin_lock_init(&zwplug->lock);
  495. zwplug->flags = 0;
  496. zwplug->zone_no = zno;
  497. zwplug->wp_offset = bdev_offset_from_zone_start(disk->part0, sector);
  498. bio_list_init(&zwplug->bio_list);
  499. INIT_WORK(&zwplug->bio_work, blk_zone_wplug_bio_work);
  500. zwplug->disk = disk;
  501. spin_lock_irqsave(&zwplug->lock, *flags);
  502. /*
  503. * Insert the new zone write plug in the hash table. This can fail only
  504. * if another context already inserted a plug. Retry from the beginning
  505. * in such case.
  506. */
  507. if (!disk_insert_zone_wplug(disk, zwplug)) {
  508. spin_unlock_irqrestore(&zwplug->lock, *flags);
  509. mempool_free(zwplug, disk->zone_wplugs_pool);
  510. goto again;
  511. }
  512. return zwplug;
  513. }
  514. static inline void blk_zone_wplug_bio_io_error(struct blk_zone_wplug *zwplug,
  515. struct bio *bio)
  516. {
  517. struct request_queue *q = zwplug->disk->queue;
  518. bio_clear_flag(bio, BIO_ZONE_WRITE_PLUGGING);
  519. bio_io_error(bio);
  520. disk_put_zone_wplug(zwplug);
  521. blk_queue_exit(q);
  522. }
  523. /*
  524. * Abort (fail) all plugged BIOs of a zone write plug.
  525. */
  526. static void disk_zone_wplug_abort(struct blk_zone_wplug *zwplug)
  527. {
  528. struct bio *bio;
  529. if (bio_list_empty(&zwplug->bio_list))
  530. return;
  531. pr_warn_ratelimited("%s: zone %u: Aborting plugged BIOs\n",
  532. zwplug->disk->disk_name, zwplug->zone_no);
  533. while ((bio = bio_list_pop(&zwplug->bio_list)))
  534. blk_zone_wplug_bio_io_error(zwplug, bio);
  535. }
  536. /*
  537. * Set a zone write plug write pointer offset to the specified value.
  538. * This aborts all plugged BIOs, which is fine as this function is called for
  539. * a zone reset operation, a zone finish operation or if the zone needs a wp
  540. * update from a report zone after a write error.
  541. */
  542. static void disk_zone_wplug_set_wp_offset(struct gendisk *disk,
  543. struct blk_zone_wplug *zwplug,
  544. unsigned int wp_offset)
  545. {
  546. lockdep_assert_held(&zwplug->lock);
  547. /* Update the zone write pointer and abort all plugged BIOs. */
  548. zwplug->flags &= ~BLK_ZONE_WPLUG_NEED_WP_UPDATE;
  549. zwplug->wp_offset = wp_offset;
  550. disk_zone_wplug_abort(zwplug);
  551. /*
  552. * The zone write plug now has no BIO plugged: remove it from the
  553. * hash table so that it cannot be seen. The plug will be freed
  554. * when the last reference is dropped.
  555. */
  556. if (disk_should_remove_zone_wplug(disk, zwplug))
  557. disk_remove_zone_wplug(disk, zwplug);
  558. }
  559. static unsigned int blk_zone_wp_offset(struct blk_zone *zone)
  560. {
  561. switch (zone->cond) {
  562. case BLK_ZONE_COND_IMP_OPEN:
  563. case BLK_ZONE_COND_EXP_OPEN:
  564. case BLK_ZONE_COND_CLOSED:
  565. return zone->wp - zone->start;
  566. case BLK_ZONE_COND_FULL:
  567. return zone->len;
  568. case BLK_ZONE_COND_EMPTY:
  569. return 0;
  570. case BLK_ZONE_COND_NOT_WP:
  571. case BLK_ZONE_COND_OFFLINE:
  572. case BLK_ZONE_COND_READONLY:
  573. default:
  574. /*
  575. * Conventional, offline and read-only zones do not have a valid
  576. * write pointer.
  577. */
  578. return UINT_MAX;
  579. }
  580. }
  581. static void disk_zone_wplug_sync_wp_offset(struct gendisk *disk,
  582. struct blk_zone *zone)
  583. {
  584. struct blk_zone_wplug *zwplug;
  585. unsigned long flags;
  586. zwplug = disk_get_zone_wplug(disk, zone->start);
  587. if (!zwplug)
  588. return;
  589. spin_lock_irqsave(&zwplug->lock, flags);
  590. if (zwplug->flags & BLK_ZONE_WPLUG_NEED_WP_UPDATE)
  591. disk_zone_wplug_set_wp_offset(disk, zwplug,
  592. blk_zone_wp_offset(zone));
  593. spin_unlock_irqrestore(&zwplug->lock, flags);
  594. disk_put_zone_wplug(zwplug);
  595. }
  596. static int disk_zone_sync_wp_offset(struct gendisk *disk, sector_t sector)
  597. {
  598. struct disk_report_zones_cb_args args = {
  599. .disk = disk,
  600. };
  601. return disk->fops->report_zones(disk, sector, 1,
  602. disk_report_zones_cb, &args);
  603. }
  604. static bool blk_zone_wplug_handle_reset_or_finish(struct bio *bio,
  605. unsigned int wp_offset)
  606. {
  607. struct gendisk *disk = bio->bi_bdev->bd_disk;
  608. sector_t sector = bio->bi_iter.bi_sector;
  609. struct blk_zone_wplug *zwplug;
  610. unsigned long flags;
  611. /* Conventional zones cannot be reset nor finished. */
  612. if (disk_zone_is_conv(disk, sector)) {
  613. bio_io_error(bio);
  614. return true;
  615. }
  616. /*
  617. * No-wait reset or finish BIOs do not make much sense as the callers
  618. * issue these as blocking operations in most cases. To avoid issues
  619. * the BIO execution potentially failing with BLK_STS_AGAIN, warn about
  620. * REQ_NOWAIT being set and ignore that flag.
  621. */
  622. if (WARN_ON_ONCE(bio->bi_opf & REQ_NOWAIT))
  623. bio->bi_opf &= ~REQ_NOWAIT;
  624. /*
  625. * If we have a zone write plug, set its write pointer offset to 0
  626. * (reset case) or to the zone size (finish case). This will abort all
  627. * BIOs plugged for the target zone. It is fine as resetting or
  628. * finishing zones while writes are still in-flight will result in the
  629. * writes failing anyway.
  630. */
  631. zwplug = disk_get_zone_wplug(disk, sector);
  632. if (zwplug) {
  633. spin_lock_irqsave(&zwplug->lock, flags);
  634. disk_zone_wplug_set_wp_offset(disk, zwplug, wp_offset);
  635. spin_unlock_irqrestore(&zwplug->lock, flags);
  636. disk_put_zone_wplug(zwplug);
  637. }
  638. return false;
  639. }
  640. static bool blk_zone_wplug_handle_reset_all(struct bio *bio)
  641. {
  642. struct gendisk *disk = bio->bi_bdev->bd_disk;
  643. struct blk_zone_wplug *zwplug;
  644. unsigned long flags;
  645. sector_t sector;
  646. /*
  647. * Set the write pointer offset of all zone write plugs to 0. This will
  648. * abort all plugged BIOs. It is fine as resetting zones while writes
  649. * are still in-flight will result in the writes failing anyway.
  650. */
  651. for (sector = 0; sector < get_capacity(disk);
  652. sector += disk->queue->limits.chunk_sectors) {
  653. zwplug = disk_get_zone_wplug(disk, sector);
  654. if (zwplug) {
  655. spin_lock_irqsave(&zwplug->lock, flags);
  656. disk_zone_wplug_set_wp_offset(disk, zwplug, 0);
  657. spin_unlock_irqrestore(&zwplug->lock, flags);
  658. disk_put_zone_wplug(zwplug);
  659. }
  660. }
  661. return false;
  662. }
  663. static void disk_zone_wplug_schedule_bio_work(struct gendisk *disk,
  664. struct blk_zone_wplug *zwplug)
  665. {
  666. /*
  667. * Take a reference on the zone write plug and schedule the submission
  668. * of the next plugged BIO. blk_zone_wplug_bio_work() will release the
  669. * reference we take here.
  670. */
  671. WARN_ON_ONCE(!(zwplug->flags & BLK_ZONE_WPLUG_PLUGGED));
  672. refcount_inc(&zwplug->ref);
  673. queue_work(disk->zone_wplugs_wq, &zwplug->bio_work);
  674. }
  675. static inline void disk_zone_wplug_add_bio(struct gendisk *disk,
  676. struct blk_zone_wplug *zwplug,
  677. struct bio *bio, unsigned int nr_segs)
  678. {
  679. bool schedule_bio_work = false;
  680. /*
  681. * Grab an extra reference on the BIO request queue usage counter.
  682. * This reference will be reused to submit a request for the BIO for
  683. * blk-mq devices and dropped when the BIO is failed and after
  684. * it is issued in the case of BIO-based devices.
  685. */
  686. percpu_ref_get(&bio->bi_bdev->bd_disk->queue->q_usage_counter);
  687. /*
  688. * The BIO is being plugged and thus will have to wait for the on-going
  689. * write and for all other writes already plugged. So polling makes
  690. * no sense.
  691. */
  692. bio_clear_polled(bio);
  693. /*
  694. * REQ_NOWAIT BIOs are always handled using the zone write plug BIO
  695. * work, which can block. So clear the REQ_NOWAIT flag and schedule the
  696. * work if this is the first BIO we are plugging.
  697. */
  698. if (bio->bi_opf & REQ_NOWAIT) {
  699. schedule_bio_work = !(zwplug->flags & BLK_ZONE_WPLUG_PLUGGED);
  700. bio->bi_opf &= ~REQ_NOWAIT;
  701. }
  702. /*
  703. * Reuse the poll cookie field to store the number of segments when
  704. * split to the hardware limits.
  705. */
  706. bio->__bi_nr_segments = nr_segs;
  707. /*
  708. * We always receive BIOs after they are split and ready to be issued.
  709. * The block layer passes the parts of a split BIO in order, and the
  710. * user must also issue write sequentially. So simply add the new BIO
  711. * at the tail of the list to preserve the sequential write order.
  712. */
  713. bio_list_add(&zwplug->bio_list, bio);
  714. zwplug->flags |= BLK_ZONE_WPLUG_PLUGGED;
  715. if (schedule_bio_work)
  716. disk_zone_wplug_schedule_bio_work(disk, zwplug);
  717. }
  718. /*
  719. * Called from bio_attempt_back_merge() when a BIO was merged with a request.
  720. */
  721. void blk_zone_write_plug_bio_merged(struct bio *bio)
  722. {
  723. struct blk_zone_wplug *zwplug;
  724. unsigned long flags;
  725. /*
  726. * If the BIO was already plugged, then we were called through
  727. * blk_zone_write_plug_init_request() -> blk_attempt_bio_merge().
  728. * For this case, we already hold a reference on the zone write plug for
  729. * the BIO and blk_zone_write_plug_init_request() will handle the
  730. * zone write pointer offset update.
  731. */
  732. if (bio_flagged(bio, BIO_ZONE_WRITE_PLUGGING))
  733. return;
  734. bio_set_flag(bio, BIO_ZONE_WRITE_PLUGGING);
  735. /*
  736. * Get a reference on the zone write plug of the target zone and advance
  737. * the zone write pointer offset. Given that this is a merge, we already
  738. * have at least one request and one BIO referencing the zone write
  739. * plug. So this should not fail.
  740. */
  741. zwplug = disk_get_zone_wplug(bio->bi_bdev->bd_disk,
  742. bio->bi_iter.bi_sector);
  743. if (WARN_ON_ONCE(!zwplug))
  744. return;
  745. spin_lock_irqsave(&zwplug->lock, flags);
  746. zwplug->wp_offset += bio_sectors(bio);
  747. spin_unlock_irqrestore(&zwplug->lock, flags);
  748. }
  749. /*
  750. * Attempt to merge plugged BIOs with a newly prepared request for a BIO that
  751. * already went through zone write plugging (either a new BIO or one that was
  752. * unplugged).
  753. */
  754. void blk_zone_write_plug_init_request(struct request *req)
  755. {
  756. sector_t req_back_sector = blk_rq_pos(req) + blk_rq_sectors(req);
  757. struct request_queue *q = req->q;
  758. struct gendisk *disk = q->disk;
  759. struct blk_zone_wplug *zwplug =
  760. disk_get_zone_wplug(disk, blk_rq_pos(req));
  761. unsigned long flags;
  762. struct bio *bio;
  763. if (WARN_ON_ONCE(!zwplug))
  764. return;
  765. /*
  766. * Indicate that completion of this request needs to be handled with
  767. * blk_zone_write_plug_finish_request(), which will drop the reference
  768. * on the zone write plug we took above on entry to this function.
  769. */
  770. req->rq_flags |= RQF_ZONE_WRITE_PLUGGING;
  771. if (blk_queue_nomerges(q))
  772. return;
  773. /*
  774. * Walk through the list of plugged BIOs to check if they can be merged
  775. * into the back of the request.
  776. */
  777. spin_lock_irqsave(&zwplug->lock, flags);
  778. while (!disk_zone_wplug_is_full(disk, zwplug)) {
  779. bio = bio_list_peek(&zwplug->bio_list);
  780. if (!bio)
  781. break;
  782. if (bio->bi_iter.bi_sector != req_back_sector ||
  783. !blk_rq_merge_ok(req, bio))
  784. break;
  785. WARN_ON_ONCE(bio_op(bio) != REQ_OP_WRITE_ZEROES &&
  786. !bio->__bi_nr_segments);
  787. bio_list_pop(&zwplug->bio_list);
  788. if (bio_attempt_back_merge(req, bio, bio->__bi_nr_segments) !=
  789. BIO_MERGE_OK) {
  790. bio_list_add_head(&zwplug->bio_list, bio);
  791. break;
  792. }
  793. /*
  794. * Drop the extra reference on the queue usage we got when
  795. * plugging the BIO and advance the write pointer offset.
  796. */
  797. blk_queue_exit(q);
  798. zwplug->wp_offset += bio_sectors(bio);
  799. req_back_sector += bio_sectors(bio);
  800. }
  801. spin_unlock_irqrestore(&zwplug->lock, flags);
  802. }
  803. /*
  804. * Check and prepare a BIO for submission by incrementing the write pointer
  805. * offset of its zone write plug and changing zone append operations into
  806. * regular write when zone append emulation is needed.
  807. */
  808. static bool blk_zone_wplug_prepare_bio(struct blk_zone_wplug *zwplug,
  809. struct bio *bio)
  810. {
  811. struct gendisk *disk = bio->bi_bdev->bd_disk;
  812. /*
  813. * If we lost track of the zone write pointer due to a write error,
  814. * the user must either execute a report zones, reset the zone or finish
  815. * the to recover a reliable write pointer position. Fail BIOs if the
  816. * user did not do that as we cannot handle emulated zone append
  817. * otherwise.
  818. */
  819. if (zwplug->flags & BLK_ZONE_WPLUG_NEED_WP_UPDATE)
  820. return false;
  821. /*
  822. * Check that the user is not attempting to write to a full zone.
  823. * We know such BIO will fail, and that would potentially overflow our
  824. * write pointer offset beyond the end of the zone.
  825. */
  826. if (disk_zone_wplug_is_full(disk, zwplug))
  827. return false;
  828. if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
  829. /*
  830. * Use a regular write starting at the current write pointer.
  831. * Similarly to native zone append operations, do not allow
  832. * merging.
  833. */
  834. bio->bi_opf &= ~REQ_OP_MASK;
  835. bio->bi_opf |= REQ_OP_WRITE | REQ_NOMERGE;
  836. bio->bi_iter.bi_sector += zwplug->wp_offset;
  837. /*
  838. * Remember that this BIO is in fact a zone append operation
  839. * so that we can restore its operation code on completion.
  840. */
  841. bio_set_flag(bio, BIO_EMULATES_ZONE_APPEND);
  842. } else {
  843. /*
  844. * Check for non-sequential writes early as we know that BIOs
  845. * with a start sector not unaligned to the zone write pointer
  846. * will fail.
  847. */
  848. if (bio_offset_from_zone_start(bio) != zwplug->wp_offset)
  849. return false;
  850. }
  851. /* Advance the zone write pointer offset. */
  852. zwplug->wp_offset += bio_sectors(bio);
  853. return true;
  854. }
  855. static bool blk_zone_wplug_handle_write(struct bio *bio, unsigned int nr_segs)
  856. {
  857. struct gendisk *disk = bio->bi_bdev->bd_disk;
  858. sector_t sector = bio->bi_iter.bi_sector;
  859. struct blk_zone_wplug *zwplug;
  860. gfp_t gfp_mask = GFP_NOIO;
  861. unsigned long flags;
  862. /*
  863. * BIOs must be fully contained within a zone so that we use the correct
  864. * zone write plug for the entire BIO. For blk-mq devices, the block
  865. * layer should already have done any splitting required to ensure this
  866. * and this BIO should thus not be straddling zone boundaries. For
  867. * BIO-based devices, it is the responsibility of the driver to split
  868. * the bio before submitting it.
  869. */
  870. if (WARN_ON_ONCE(bio_straddles_zones(bio))) {
  871. bio_io_error(bio);
  872. return true;
  873. }
  874. /* Conventional zones do not need write plugging. */
  875. if (disk_zone_is_conv(disk, sector)) {
  876. /* Zone append to conventional zones is not allowed. */
  877. if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
  878. bio_io_error(bio);
  879. return true;
  880. }
  881. return false;
  882. }
  883. if (bio->bi_opf & REQ_NOWAIT)
  884. gfp_mask = GFP_NOWAIT;
  885. zwplug = disk_get_and_lock_zone_wplug(disk, sector, gfp_mask, &flags);
  886. if (!zwplug) {
  887. if (bio->bi_opf & REQ_NOWAIT)
  888. bio_wouldblock_error(bio);
  889. else
  890. bio_io_error(bio);
  891. return true;
  892. }
  893. /* Indicate that this BIO is being handled using zone write plugging. */
  894. bio_set_flag(bio, BIO_ZONE_WRITE_PLUGGING);
  895. /*
  896. * If the zone is already plugged, add the BIO to the plug BIO list.
  897. * Do the same for REQ_NOWAIT BIOs to ensure that we will not see a
  898. * BLK_STS_AGAIN failure if we let the BIO execute.
  899. * Otherwise, plug and let the BIO execute.
  900. */
  901. if ((zwplug->flags & BLK_ZONE_WPLUG_PLUGGED) ||
  902. (bio->bi_opf & REQ_NOWAIT))
  903. goto plug;
  904. if (!blk_zone_wplug_prepare_bio(zwplug, bio)) {
  905. spin_unlock_irqrestore(&zwplug->lock, flags);
  906. bio_io_error(bio);
  907. return true;
  908. }
  909. zwplug->flags |= BLK_ZONE_WPLUG_PLUGGED;
  910. spin_unlock_irqrestore(&zwplug->lock, flags);
  911. return false;
  912. plug:
  913. disk_zone_wplug_add_bio(disk, zwplug, bio, nr_segs);
  914. spin_unlock_irqrestore(&zwplug->lock, flags);
  915. return true;
  916. }
  917. static void blk_zone_wplug_handle_native_zone_append(struct bio *bio)
  918. {
  919. struct gendisk *disk = bio->bi_bdev->bd_disk;
  920. struct blk_zone_wplug *zwplug;
  921. unsigned long flags;
  922. /*
  923. * We have native support for zone append operations, so we are not
  924. * going to handle @bio through plugging. However, we may already have a
  925. * zone write plug for the target zone if that zone was previously
  926. * partially written using regular writes. In such case, we risk leaving
  927. * the plug in the disk hash table if the zone is fully written using
  928. * zone append operations. Avoid this by removing the zone write plug.
  929. */
  930. zwplug = disk_get_zone_wplug(disk, bio->bi_iter.bi_sector);
  931. if (likely(!zwplug))
  932. return;
  933. spin_lock_irqsave(&zwplug->lock, flags);
  934. /*
  935. * We are about to remove the zone write plug. But if the user
  936. * (mistakenly) has issued regular writes together with native zone
  937. * append, we must aborts the writes as otherwise the plugged BIOs would
  938. * not be executed by the plug BIO work as disk_get_zone_wplug() will
  939. * return NULL after the plug is removed. Aborting the plugged write
  940. * BIOs is consistent with the fact that these writes will most likely
  941. * fail anyway as there is no ordering guarantees between zone append
  942. * operations and regular write operations.
  943. */
  944. if (!bio_list_empty(&zwplug->bio_list)) {
  945. pr_warn_ratelimited("%s: zone %u: Invalid mix of zone append and regular writes\n",
  946. disk->disk_name, zwplug->zone_no);
  947. disk_zone_wplug_abort(zwplug);
  948. }
  949. disk_remove_zone_wplug(disk, zwplug);
  950. spin_unlock_irqrestore(&zwplug->lock, flags);
  951. disk_put_zone_wplug(zwplug);
  952. }
  953. /**
  954. * blk_zone_plug_bio - Handle a zone write BIO with zone write plugging
  955. * @bio: The BIO being submitted
  956. * @nr_segs: The number of physical segments of @bio
  957. *
  958. * Handle write, write zeroes and zone append operations requiring emulation
  959. * using zone write plugging.
  960. *
  961. * Return true whenever @bio execution needs to be delayed through the zone
  962. * write plug. Otherwise, return false to let the submission path process
  963. * @bio normally.
  964. */
  965. bool blk_zone_plug_bio(struct bio *bio, unsigned int nr_segs)
  966. {
  967. struct block_device *bdev = bio->bi_bdev;
  968. if (WARN_ON_ONCE(!bdev->bd_disk->zone_wplugs_hash))
  969. return false;
  970. /*
  971. * Regular writes and write zeroes need to be handled through the target
  972. * zone write plug. This includes writes with REQ_FUA | REQ_PREFLUSH
  973. * which may need to go through the flush machinery depending on the
  974. * target device capabilities. Plugging such writes is fine as the flush
  975. * machinery operates at the request level, below the plug, and
  976. * completion of the flush sequence will go through the regular BIO
  977. * completion, which will handle zone write plugging.
  978. * Zone append operations for devices that requested emulation must
  979. * also be plugged so that these BIOs can be changed into regular
  980. * write BIOs.
  981. * Zone reset, reset all and finish commands need special treatment
  982. * to correctly track the write pointer offset of zones. These commands
  983. * are not plugged as we do not need serialization with write
  984. * operations. It is the responsibility of the user to not issue reset
  985. * and finish commands when write operations are in flight.
  986. */
  987. switch (bio_op(bio)) {
  988. case REQ_OP_ZONE_APPEND:
  989. if (!bdev_emulates_zone_append(bdev)) {
  990. blk_zone_wplug_handle_native_zone_append(bio);
  991. return false;
  992. }
  993. fallthrough;
  994. case REQ_OP_WRITE:
  995. case REQ_OP_WRITE_ZEROES:
  996. return blk_zone_wplug_handle_write(bio, nr_segs);
  997. case REQ_OP_ZONE_RESET:
  998. return blk_zone_wplug_handle_reset_or_finish(bio, 0);
  999. case REQ_OP_ZONE_FINISH:
  1000. return blk_zone_wplug_handle_reset_or_finish(bio,
  1001. bdev_zone_sectors(bdev));
  1002. case REQ_OP_ZONE_RESET_ALL:
  1003. return blk_zone_wplug_handle_reset_all(bio);
  1004. default:
  1005. return false;
  1006. }
  1007. return false;
  1008. }
  1009. EXPORT_SYMBOL_GPL(blk_zone_plug_bio);
  1010. static void disk_zone_wplug_unplug_bio(struct gendisk *disk,
  1011. struct blk_zone_wplug *zwplug)
  1012. {
  1013. unsigned long flags;
  1014. spin_lock_irqsave(&zwplug->lock, flags);
  1015. /* Schedule submission of the next plugged BIO if we have one. */
  1016. if (!bio_list_empty(&zwplug->bio_list)) {
  1017. disk_zone_wplug_schedule_bio_work(disk, zwplug);
  1018. spin_unlock_irqrestore(&zwplug->lock, flags);
  1019. return;
  1020. }
  1021. zwplug->flags &= ~BLK_ZONE_WPLUG_PLUGGED;
  1022. /*
  1023. * If the zone is full (it was fully written or finished, or empty
  1024. * (it was reset), remove its zone write plug from the hash table.
  1025. */
  1026. if (disk_should_remove_zone_wplug(disk, zwplug))
  1027. disk_remove_zone_wplug(disk, zwplug);
  1028. spin_unlock_irqrestore(&zwplug->lock, flags);
  1029. }
  1030. void blk_zone_write_plug_bio_endio(struct bio *bio)
  1031. {
  1032. struct gendisk *disk = bio->bi_bdev->bd_disk;
  1033. struct blk_zone_wplug *zwplug =
  1034. disk_get_zone_wplug(disk, bio->bi_iter.bi_sector);
  1035. unsigned long flags;
  1036. if (WARN_ON_ONCE(!zwplug))
  1037. return;
  1038. /* Make sure we do not see this BIO again by clearing the plug flag. */
  1039. bio_clear_flag(bio, BIO_ZONE_WRITE_PLUGGING);
  1040. /*
  1041. * If this is a regular write emulating a zone append operation,
  1042. * restore the original operation code.
  1043. */
  1044. if (bio_flagged(bio, BIO_EMULATES_ZONE_APPEND)) {
  1045. bio->bi_opf &= ~REQ_OP_MASK;
  1046. bio->bi_opf |= REQ_OP_ZONE_APPEND;
  1047. bio_clear_flag(bio, BIO_EMULATES_ZONE_APPEND);
  1048. }
  1049. /*
  1050. * If the BIO failed, abort all plugged BIOs and mark the plug as
  1051. * needing a write pointer update.
  1052. */
  1053. if (bio->bi_status != BLK_STS_OK) {
  1054. spin_lock_irqsave(&zwplug->lock, flags);
  1055. disk_zone_wplug_abort(zwplug);
  1056. zwplug->flags |= BLK_ZONE_WPLUG_NEED_WP_UPDATE;
  1057. spin_unlock_irqrestore(&zwplug->lock, flags);
  1058. }
  1059. /* Drop the reference we took when the BIO was issued. */
  1060. disk_put_zone_wplug(zwplug);
  1061. /*
  1062. * For BIO-based devices, blk_zone_write_plug_finish_request()
  1063. * is not called. So we need to schedule execution of the next
  1064. * plugged BIO here.
  1065. */
  1066. if (bdev_test_flag(bio->bi_bdev, BD_HAS_SUBMIT_BIO))
  1067. disk_zone_wplug_unplug_bio(disk, zwplug);
  1068. /* Drop the reference we took when entering this function. */
  1069. disk_put_zone_wplug(zwplug);
  1070. }
  1071. void blk_zone_write_plug_finish_request(struct request *req)
  1072. {
  1073. struct gendisk *disk = req->q->disk;
  1074. struct blk_zone_wplug *zwplug;
  1075. zwplug = disk_get_zone_wplug(disk, req->__sector);
  1076. if (WARN_ON_ONCE(!zwplug))
  1077. return;
  1078. req->rq_flags &= ~RQF_ZONE_WRITE_PLUGGING;
  1079. /*
  1080. * Drop the reference we took when the request was initialized in
  1081. * blk_zone_write_plug_init_request().
  1082. */
  1083. disk_put_zone_wplug(zwplug);
  1084. disk_zone_wplug_unplug_bio(disk, zwplug);
  1085. /* Drop the reference we took when entering this function. */
  1086. disk_put_zone_wplug(zwplug);
  1087. }
  1088. static void blk_zone_wplug_bio_work(struct work_struct *work)
  1089. {
  1090. struct blk_zone_wplug *zwplug =
  1091. container_of(work, struct blk_zone_wplug, bio_work);
  1092. struct block_device *bdev;
  1093. unsigned long flags;
  1094. struct bio *bio;
  1095. bool prepared;
  1096. /*
  1097. * Submit the next plugged BIO. If we do not have any, clear
  1098. * the plugged flag.
  1099. */
  1100. again:
  1101. spin_lock_irqsave(&zwplug->lock, flags);
  1102. bio = bio_list_pop(&zwplug->bio_list);
  1103. if (!bio) {
  1104. zwplug->flags &= ~BLK_ZONE_WPLUG_PLUGGED;
  1105. spin_unlock_irqrestore(&zwplug->lock, flags);
  1106. goto put_zwplug;
  1107. }
  1108. prepared = blk_zone_wplug_prepare_bio(zwplug, bio);
  1109. spin_unlock_irqrestore(&zwplug->lock, flags);
  1110. if (!prepared) {
  1111. blk_zone_wplug_bio_io_error(zwplug, bio);
  1112. goto again;
  1113. }
  1114. bdev = bio->bi_bdev;
  1115. /*
  1116. * blk-mq devices will reuse the extra reference on the request queue
  1117. * usage counter we took when the BIO was plugged, but the submission
  1118. * path for BIO-based devices will not do that. So drop this extra
  1119. * reference here.
  1120. */
  1121. if (bdev_test_flag(bdev, BD_HAS_SUBMIT_BIO)) {
  1122. bdev->bd_disk->fops->submit_bio(bio);
  1123. blk_queue_exit(bdev->bd_disk->queue);
  1124. } else {
  1125. blk_mq_submit_bio(bio);
  1126. }
  1127. put_zwplug:
  1128. /* Drop the reference we took in disk_zone_wplug_schedule_bio_work(). */
  1129. disk_put_zone_wplug(zwplug);
  1130. }
  1131. static inline unsigned int disk_zone_wplugs_hash_size(struct gendisk *disk)
  1132. {
  1133. return 1U << disk->zone_wplugs_hash_bits;
  1134. }
  1135. void disk_init_zone_resources(struct gendisk *disk)
  1136. {
  1137. spin_lock_init(&disk->zone_wplugs_lock);
  1138. }
  1139. /*
  1140. * For the size of a disk zone write plug hash table, use the size of the
  1141. * zone write plug mempool, which is the maximum of the disk open zones and
  1142. * active zones limits. But do not exceed 4KB (512 hlist head entries), that is,
  1143. * 9 bits. For a disk that has no limits, mempool size defaults to 128.
  1144. */
  1145. #define BLK_ZONE_WPLUG_MAX_HASH_BITS 9
  1146. #define BLK_ZONE_WPLUG_DEFAULT_POOL_SIZE 128
  1147. static int disk_alloc_zone_resources(struct gendisk *disk,
  1148. unsigned int pool_size)
  1149. {
  1150. unsigned int i;
  1151. atomic_set(&disk->nr_zone_wplugs, 0);
  1152. disk->zone_wplugs_hash_bits =
  1153. min(ilog2(pool_size) + 1, BLK_ZONE_WPLUG_MAX_HASH_BITS);
  1154. disk->zone_wplugs_hash =
  1155. kcalloc(disk_zone_wplugs_hash_size(disk),
  1156. sizeof(struct hlist_head), GFP_KERNEL);
  1157. if (!disk->zone_wplugs_hash)
  1158. return -ENOMEM;
  1159. for (i = 0; i < disk_zone_wplugs_hash_size(disk); i++)
  1160. INIT_HLIST_HEAD(&disk->zone_wplugs_hash[i]);
  1161. disk->zone_wplugs_pool = mempool_create_kmalloc_pool(pool_size,
  1162. sizeof(struct blk_zone_wplug));
  1163. if (!disk->zone_wplugs_pool)
  1164. goto free_hash;
  1165. disk->zone_wplugs_wq =
  1166. alloc_workqueue("%s_zwplugs", WQ_MEM_RECLAIM | WQ_HIGHPRI,
  1167. pool_size, disk->disk_name);
  1168. if (!disk->zone_wplugs_wq)
  1169. goto destroy_pool;
  1170. return 0;
  1171. destroy_pool:
  1172. mempool_destroy(disk->zone_wplugs_pool);
  1173. disk->zone_wplugs_pool = NULL;
  1174. free_hash:
  1175. kfree(disk->zone_wplugs_hash);
  1176. disk->zone_wplugs_hash = NULL;
  1177. disk->zone_wplugs_hash_bits = 0;
  1178. return -ENOMEM;
  1179. }
  1180. static void disk_destroy_zone_wplugs_hash_table(struct gendisk *disk)
  1181. {
  1182. struct blk_zone_wplug *zwplug;
  1183. unsigned int i;
  1184. if (!disk->zone_wplugs_hash)
  1185. return;
  1186. /* Free all the zone write plugs we have. */
  1187. for (i = 0; i < disk_zone_wplugs_hash_size(disk); i++) {
  1188. while (!hlist_empty(&disk->zone_wplugs_hash[i])) {
  1189. zwplug = hlist_entry(disk->zone_wplugs_hash[i].first,
  1190. struct blk_zone_wplug, node);
  1191. refcount_inc(&zwplug->ref);
  1192. disk_remove_zone_wplug(disk, zwplug);
  1193. disk_put_zone_wplug(zwplug);
  1194. }
  1195. }
  1196. WARN_ON_ONCE(atomic_read(&disk->nr_zone_wplugs));
  1197. kfree(disk->zone_wplugs_hash);
  1198. disk->zone_wplugs_hash = NULL;
  1199. disk->zone_wplugs_hash_bits = 0;
  1200. }
  1201. static unsigned int disk_set_conv_zones_bitmap(struct gendisk *disk,
  1202. unsigned long *bitmap)
  1203. {
  1204. unsigned int nr_conv_zones = 0;
  1205. unsigned long flags;
  1206. spin_lock_irqsave(&disk->zone_wplugs_lock, flags);
  1207. if (bitmap)
  1208. nr_conv_zones = bitmap_weight(bitmap, disk->nr_zones);
  1209. bitmap = rcu_replace_pointer(disk->conv_zones_bitmap, bitmap,
  1210. lockdep_is_held(&disk->zone_wplugs_lock));
  1211. spin_unlock_irqrestore(&disk->zone_wplugs_lock, flags);
  1212. kfree_rcu_mightsleep(bitmap);
  1213. return nr_conv_zones;
  1214. }
  1215. void disk_free_zone_resources(struct gendisk *disk)
  1216. {
  1217. if (!disk->zone_wplugs_pool)
  1218. return;
  1219. if (disk->zone_wplugs_wq) {
  1220. destroy_workqueue(disk->zone_wplugs_wq);
  1221. disk->zone_wplugs_wq = NULL;
  1222. }
  1223. disk_destroy_zone_wplugs_hash_table(disk);
  1224. /*
  1225. * Wait for the zone write plugs to be RCU-freed before
  1226. * destorying the mempool.
  1227. */
  1228. rcu_barrier();
  1229. mempool_destroy(disk->zone_wplugs_pool);
  1230. disk->zone_wplugs_pool = NULL;
  1231. disk_set_conv_zones_bitmap(disk, NULL);
  1232. disk->zone_capacity = 0;
  1233. disk->last_zone_capacity = 0;
  1234. disk->nr_zones = 0;
  1235. }
  1236. static inline bool disk_need_zone_resources(struct gendisk *disk)
  1237. {
  1238. /*
  1239. * All mq zoned devices need zone resources so that the block layer
  1240. * can automatically handle write BIO plugging. BIO-based device drivers
  1241. * (e.g. DM devices) are normally responsible for handling zone write
  1242. * ordering and do not need zone resources, unless the driver requires
  1243. * zone append emulation.
  1244. */
  1245. return queue_is_mq(disk->queue) ||
  1246. queue_emulates_zone_append(disk->queue);
  1247. }
  1248. static int disk_revalidate_zone_resources(struct gendisk *disk,
  1249. unsigned int nr_zones)
  1250. {
  1251. struct queue_limits *lim = &disk->queue->limits;
  1252. unsigned int pool_size;
  1253. if (!disk_need_zone_resources(disk))
  1254. return 0;
  1255. /*
  1256. * If the device has no limit on the maximum number of open and active
  1257. * zones, use BLK_ZONE_WPLUG_DEFAULT_POOL_SIZE.
  1258. */
  1259. pool_size = max(lim->max_open_zones, lim->max_active_zones);
  1260. if (!pool_size)
  1261. pool_size = min(BLK_ZONE_WPLUG_DEFAULT_POOL_SIZE, nr_zones);
  1262. if (!disk->zone_wplugs_hash)
  1263. return disk_alloc_zone_resources(disk, pool_size);
  1264. return 0;
  1265. }
  1266. struct blk_revalidate_zone_args {
  1267. struct gendisk *disk;
  1268. unsigned long *conv_zones_bitmap;
  1269. unsigned int nr_zones;
  1270. unsigned int zone_capacity;
  1271. unsigned int last_zone_capacity;
  1272. sector_t sector;
  1273. };
  1274. /*
  1275. * Update the disk zone resources information and device queue limits.
  1276. * The disk queue is frozen when this is executed.
  1277. */
  1278. static int disk_update_zone_resources(struct gendisk *disk,
  1279. struct blk_revalidate_zone_args *args)
  1280. {
  1281. struct request_queue *q = disk->queue;
  1282. unsigned int nr_seq_zones, nr_conv_zones;
  1283. unsigned int pool_size;
  1284. struct queue_limits lim;
  1285. disk->nr_zones = args->nr_zones;
  1286. disk->zone_capacity = args->zone_capacity;
  1287. disk->last_zone_capacity = args->last_zone_capacity;
  1288. nr_conv_zones =
  1289. disk_set_conv_zones_bitmap(disk, args->conv_zones_bitmap);
  1290. if (nr_conv_zones >= disk->nr_zones) {
  1291. pr_warn("%s: Invalid number of conventional zones %u / %u\n",
  1292. disk->disk_name, nr_conv_zones, disk->nr_zones);
  1293. return -ENODEV;
  1294. }
  1295. lim = queue_limits_start_update(q);
  1296. /*
  1297. * Some devices can advertize zone resource limits that are larger than
  1298. * the number of sequential zones of the zoned block device, e.g. a
  1299. * small ZNS namespace. For such case, assume that the zoned device has
  1300. * no zone resource limits.
  1301. */
  1302. nr_seq_zones = disk->nr_zones - nr_conv_zones;
  1303. if (lim.max_open_zones >= nr_seq_zones)
  1304. lim.max_open_zones = 0;
  1305. if (lim.max_active_zones >= nr_seq_zones)
  1306. lim.max_active_zones = 0;
  1307. if (!disk->zone_wplugs_pool)
  1308. goto commit;
  1309. /*
  1310. * If the device has no limit on the maximum number of open and active
  1311. * zones, set its max open zone limit to the mempool size to indicate
  1312. * to the user that there is a potential performance impact due to
  1313. * dynamic zone write plug allocation when simultaneously writing to
  1314. * more zones than the size of the mempool.
  1315. */
  1316. pool_size = max(lim.max_open_zones, lim.max_active_zones);
  1317. if (!pool_size)
  1318. pool_size = min(BLK_ZONE_WPLUG_DEFAULT_POOL_SIZE, nr_seq_zones);
  1319. mempool_resize(disk->zone_wplugs_pool, pool_size);
  1320. if (!lim.max_open_zones && !lim.max_active_zones) {
  1321. if (pool_size < nr_seq_zones)
  1322. lim.max_open_zones = pool_size;
  1323. else
  1324. lim.max_open_zones = 0;
  1325. }
  1326. commit:
  1327. return queue_limits_commit_update_frozen(q, &lim);
  1328. }
  1329. static int blk_revalidate_conv_zone(struct blk_zone *zone, unsigned int idx,
  1330. struct blk_revalidate_zone_args *args)
  1331. {
  1332. struct gendisk *disk = args->disk;
  1333. if (zone->capacity != zone->len) {
  1334. pr_warn("%s: Invalid conventional zone capacity\n",
  1335. disk->disk_name);
  1336. return -ENODEV;
  1337. }
  1338. if (disk_zone_is_last(disk, zone))
  1339. args->last_zone_capacity = zone->capacity;
  1340. if (!disk_need_zone_resources(disk))
  1341. return 0;
  1342. if (!args->conv_zones_bitmap) {
  1343. args->conv_zones_bitmap =
  1344. bitmap_zalloc(args->nr_zones, GFP_NOIO);
  1345. if (!args->conv_zones_bitmap)
  1346. return -ENOMEM;
  1347. }
  1348. set_bit(idx, args->conv_zones_bitmap);
  1349. return 0;
  1350. }
  1351. static int blk_revalidate_seq_zone(struct blk_zone *zone, unsigned int idx,
  1352. struct blk_revalidate_zone_args *args)
  1353. {
  1354. struct gendisk *disk = args->disk;
  1355. struct blk_zone_wplug *zwplug;
  1356. unsigned int wp_offset;
  1357. unsigned long flags;
  1358. /*
  1359. * Remember the capacity of the first sequential zone and check
  1360. * if it is constant for all zones, ignoring the last zone as it can be
  1361. * smaller.
  1362. */
  1363. if (!args->zone_capacity)
  1364. args->zone_capacity = zone->capacity;
  1365. if (disk_zone_is_last(disk, zone)) {
  1366. args->last_zone_capacity = zone->capacity;
  1367. } else if (zone->capacity != args->zone_capacity) {
  1368. pr_warn("%s: Invalid variable zone capacity\n",
  1369. disk->disk_name);
  1370. return -ENODEV;
  1371. }
  1372. /*
  1373. * If the device needs zone append emulation, we need to track the
  1374. * write pointer of all zones that are not empty nor full. So make sure
  1375. * we have a zone write plug for such zone if the device has a zone
  1376. * write plug hash table.
  1377. */
  1378. if (!queue_emulates_zone_append(disk->queue) || !disk->zone_wplugs_hash)
  1379. return 0;
  1380. disk_zone_wplug_sync_wp_offset(disk, zone);
  1381. wp_offset = blk_zone_wp_offset(zone);
  1382. if (!wp_offset || wp_offset >= zone->capacity)
  1383. return 0;
  1384. zwplug = disk_get_and_lock_zone_wplug(disk, zone->wp, GFP_NOIO, &flags);
  1385. if (!zwplug)
  1386. return -ENOMEM;
  1387. spin_unlock_irqrestore(&zwplug->lock, flags);
  1388. disk_put_zone_wplug(zwplug);
  1389. return 0;
  1390. }
  1391. /*
  1392. * Helper function to check the validity of zones of a zoned block device.
  1393. */
  1394. static int blk_revalidate_zone_cb(struct blk_zone *zone, unsigned int idx,
  1395. void *data)
  1396. {
  1397. struct blk_revalidate_zone_args *args = data;
  1398. struct gendisk *disk = args->disk;
  1399. sector_t zone_sectors = disk->queue->limits.chunk_sectors;
  1400. int ret;
  1401. /* Check for bad zones and holes in the zone report */
  1402. if (zone->start != args->sector) {
  1403. pr_warn("%s: Zone gap at sectors %llu..%llu\n",
  1404. disk->disk_name, args->sector, zone->start);
  1405. return -ENODEV;
  1406. }
  1407. if (zone->start >= get_capacity(disk) || !zone->len) {
  1408. pr_warn("%s: Invalid zone start %llu, length %llu\n",
  1409. disk->disk_name, zone->start, zone->len);
  1410. return -ENODEV;
  1411. }
  1412. /*
  1413. * All zones must have the same size, with the exception on an eventual
  1414. * smaller last zone.
  1415. */
  1416. if (!disk_zone_is_last(disk, zone)) {
  1417. if (zone->len != zone_sectors) {
  1418. pr_warn("%s: Invalid zoned device with non constant zone size\n",
  1419. disk->disk_name);
  1420. return -ENODEV;
  1421. }
  1422. } else if (zone->len > zone_sectors) {
  1423. pr_warn("%s: Invalid zoned device with larger last zone size\n",
  1424. disk->disk_name);
  1425. return -ENODEV;
  1426. }
  1427. if (!zone->capacity || zone->capacity > zone->len) {
  1428. pr_warn("%s: Invalid zone capacity\n",
  1429. disk->disk_name);
  1430. return -ENODEV;
  1431. }
  1432. /* Check zone type */
  1433. switch (zone->type) {
  1434. case BLK_ZONE_TYPE_CONVENTIONAL:
  1435. ret = blk_revalidate_conv_zone(zone, idx, args);
  1436. break;
  1437. case BLK_ZONE_TYPE_SEQWRITE_REQ:
  1438. ret = blk_revalidate_seq_zone(zone, idx, args);
  1439. break;
  1440. case BLK_ZONE_TYPE_SEQWRITE_PREF:
  1441. default:
  1442. pr_warn("%s: Invalid zone type 0x%x at sectors %llu\n",
  1443. disk->disk_name, (int)zone->type, zone->start);
  1444. ret = -ENODEV;
  1445. }
  1446. if (!ret)
  1447. args->sector += zone->len;
  1448. return ret;
  1449. }
  1450. /**
  1451. * blk_revalidate_disk_zones - (re)allocate and initialize zone write plugs
  1452. * @disk: Target disk
  1453. *
  1454. * Helper function for low-level device drivers to check, (re) allocate and
  1455. * initialize resources used for managing zoned disks. This function should
  1456. * normally be called by blk-mq based drivers when a zoned gendisk is probed
  1457. * and when the zone configuration of the gendisk changes (e.g. after a format).
  1458. * Before calling this function, the device driver must already have set the
  1459. * device zone size (chunk_sector limit) and the max zone append limit.
  1460. * BIO based drivers can also use this function as long as the device queue
  1461. * can be safely frozen.
  1462. */
  1463. int blk_revalidate_disk_zones(struct gendisk *disk)
  1464. {
  1465. struct request_queue *q = disk->queue;
  1466. sector_t zone_sectors = q->limits.chunk_sectors;
  1467. sector_t capacity = get_capacity(disk);
  1468. struct blk_revalidate_zone_args args = { };
  1469. unsigned int noio_flag;
  1470. int ret = -ENOMEM;
  1471. if (WARN_ON_ONCE(!blk_queue_is_zoned(q)))
  1472. return -EIO;
  1473. if (!capacity)
  1474. return -ENODEV;
  1475. /*
  1476. * Checks that the device driver indicated a valid zone size and that
  1477. * the max zone append limit is set.
  1478. */
  1479. if (!zone_sectors || !is_power_of_2(zone_sectors)) {
  1480. pr_warn("%s: Invalid non power of two zone size (%llu)\n",
  1481. disk->disk_name, zone_sectors);
  1482. return -ENODEV;
  1483. }
  1484. if (!queue_max_zone_append_sectors(q)) {
  1485. pr_warn("%s: Invalid 0 maximum zone append limit\n",
  1486. disk->disk_name);
  1487. return -ENODEV;
  1488. }
  1489. /*
  1490. * Ensure that all memory allocations in this context are done as if
  1491. * GFP_NOIO was specified.
  1492. */
  1493. args.disk = disk;
  1494. args.nr_zones = (capacity + zone_sectors - 1) >> ilog2(zone_sectors);
  1495. noio_flag = memalloc_noio_save();
  1496. ret = disk_revalidate_zone_resources(disk, args.nr_zones);
  1497. if (ret) {
  1498. memalloc_noio_restore(noio_flag);
  1499. return ret;
  1500. }
  1501. ret = disk->fops->report_zones(disk, 0, UINT_MAX,
  1502. blk_revalidate_zone_cb, &args);
  1503. if (!ret) {
  1504. pr_warn("%s: No zones reported\n", disk->disk_name);
  1505. ret = -ENODEV;
  1506. }
  1507. memalloc_noio_restore(noio_flag);
  1508. /*
  1509. * If zones where reported, make sure that the entire disk capacity
  1510. * has been checked.
  1511. */
  1512. if (ret > 0 && args.sector != capacity) {
  1513. pr_warn("%s: Missing zones from sector %llu\n",
  1514. disk->disk_name, args.sector);
  1515. ret = -ENODEV;
  1516. }
  1517. /*
  1518. * Set the new disk zone parameters only once the queue is frozen and
  1519. * all I/Os are completed.
  1520. */
  1521. if (ret > 0)
  1522. ret = disk_update_zone_resources(disk, &args);
  1523. else
  1524. pr_warn("%s: failed to revalidate zones\n", disk->disk_name);
  1525. if (ret) {
  1526. blk_mq_freeze_queue(q);
  1527. disk_free_zone_resources(disk);
  1528. blk_mq_unfreeze_queue(q);
  1529. }
  1530. return ret;
  1531. }
  1532. EXPORT_SYMBOL_GPL(blk_revalidate_disk_zones);
  1533. /**
  1534. * blk_zone_issue_zeroout - zero-fill a block range in a zone
  1535. * @bdev: blockdev to write
  1536. * @sector: start sector
  1537. * @nr_sects: number of sectors to write
  1538. * @gfp_mask: memory allocation flags (for bio_alloc)
  1539. *
  1540. * Description:
  1541. * Zero-fill a block range in a zone (@sector must be equal to the zone write
  1542. * pointer), handling potential errors due to the (initially unknown) lack of
  1543. * hardware offload (See blkdev_issue_zeroout()).
  1544. */
  1545. int blk_zone_issue_zeroout(struct block_device *bdev, sector_t sector,
  1546. sector_t nr_sects, gfp_t gfp_mask)
  1547. {
  1548. int ret;
  1549. if (WARN_ON_ONCE(!bdev_is_zoned(bdev)))
  1550. return -EIO;
  1551. ret = blkdev_issue_zeroout(bdev, sector, nr_sects, gfp_mask,
  1552. BLKDEV_ZERO_NOFALLBACK);
  1553. if (ret != -EOPNOTSUPP)
  1554. return ret;
  1555. /*
  1556. * The failed call to blkdev_issue_zeroout() advanced the zone write
  1557. * pointer. Undo this using a report zone to update the zone write
  1558. * pointer to the correct current value.
  1559. */
  1560. ret = disk_zone_sync_wp_offset(bdev->bd_disk, sector);
  1561. if (ret != 1)
  1562. return ret < 0 ? ret : -EIO;
  1563. /*
  1564. * Retry without BLKDEV_ZERO_NOFALLBACK to force the fallback to a
  1565. * regular write with zero-pages.
  1566. */
  1567. return blkdev_issue_zeroout(bdev, sector, nr_sects, gfp_mask, 0);
  1568. }
  1569. EXPORT_SYMBOL_GPL(blk_zone_issue_zeroout);
  1570. #ifdef CONFIG_BLK_DEBUG_FS
  1571. int queue_zone_wplugs_show(void *data, struct seq_file *m)
  1572. {
  1573. struct request_queue *q = data;
  1574. struct gendisk *disk = q->disk;
  1575. struct blk_zone_wplug *zwplug;
  1576. unsigned int zwp_wp_offset, zwp_flags;
  1577. unsigned int zwp_zone_no, zwp_ref;
  1578. unsigned int zwp_bio_list_size, i;
  1579. unsigned long flags;
  1580. if (!disk->zone_wplugs_hash)
  1581. return 0;
  1582. rcu_read_lock();
  1583. for (i = 0; i < disk_zone_wplugs_hash_size(disk); i++) {
  1584. hlist_for_each_entry_rcu(zwplug,
  1585. &disk->zone_wplugs_hash[i], node) {
  1586. spin_lock_irqsave(&zwplug->lock, flags);
  1587. zwp_zone_no = zwplug->zone_no;
  1588. zwp_flags = zwplug->flags;
  1589. zwp_ref = refcount_read(&zwplug->ref);
  1590. zwp_wp_offset = zwplug->wp_offset;
  1591. zwp_bio_list_size = bio_list_size(&zwplug->bio_list);
  1592. spin_unlock_irqrestore(&zwplug->lock, flags);
  1593. seq_printf(m, "%u 0x%x %u %u %u\n",
  1594. zwp_zone_no, zwp_flags, zwp_ref,
  1595. zwp_wp_offset, zwp_bio_list_size);
  1596. }
  1597. }
  1598. rcu_read_unlock();
  1599. return 0;
  1600. }
  1601. #endif