dm-zoned-target.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2017 Western Digital Corporation or its affiliates.
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include "dm-zoned.h"
  8. #include <linux/module.h>
  9. #define DM_MSG_PREFIX "zoned"
  10. #define DMZ_MIN_BIOS 8192
  11. /*
  12. * Zone BIO context.
  13. */
  14. struct dmz_bioctx {
  15. struct dmz_target *target;
  16. struct dm_zone *zone;
  17. struct bio *bio;
  18. refcount_t ref;
  19. };
  20. /*
  21. * Chunk work descriptor.
  22. */
  23. struct dm_chunk_work {
  24. struct work_struct work;
  25. refcount_t refcount;
  26. struct dmz_target *target;
  27. unsigned int chunk;
  28. struct bio_list bio_list;
  29. };
  30. /*
  31. * Target descriptor.
  32. */
  33. struct dmz_target {
  34. struct dm_dev *ddev;
  35. unsigned long flags;
  36. /* Zoned block device information */
  37. struct dmz_dev *dev;
  38. /* For metadata handling */
  39. struct dmz_metadata *metadata;
  40. /* For reclaim */
  41. struct dmz_reclaim *reclaim;
  42. /* For chunk work */
  43. struct radix_tree_root chunk_rxtree;
  44. struct workqueue_struct *chunk_wq;
  45. struct mutex chunk_lock;
  46. /* For cloned BIOs to zones */
  47. struct bio_set bio_set;
  48. /* For flush */
  49. spinlock_t flush_lock;
  50. struct bio_list flush_list;
  51. struct delayed_work flush_work;
  52. struct workqueue_struct *flush_wq;
  53. };
  54. /*
  55. * Flush intervals (seconds).
  56. */
  57. #define DMZ_FLUSH_PERIOD (10 * HZ)
  58. /*
  59. * Target BIO completion.
  60. */
  61. static inline void dmz_bio_endio(struct bio *bio, blk_status_t status)
  62. {
  63. struct dmz_bioctx *bioctx = dm_per_bio_data(bio, sizeof(struct dmz_bioctx));
  64. if (status != BLK_STS_OK && bio->bi_status == BLK_STS_OK)
  65. bio->bi_status = status;
  66. if (bio->bi_status != BLK_STS_OK)
  67. bioctx->target->dev->flags |= DMZ_CHECK_BDEV;
  68. if (refcount_dec_and_test(&bioctx->ref)) {
  69. struct dm_zone *zone = bioctx->zone;
  70. if (zone) {
  71. if (bio->bi_status != BLK_STS_OK &&
  72. bio_op(bio) == REQ_OP_WRITE &&
  73. dmz_is_seq(zone))
  74. set_bit(DMZ_SEQ_WRITE_ERR, &zone->flags);
  75. dmz_deactivate_zone(zone);
  76. }
  77. bio_endio(bio);
  78. }
  79. }
  80. /*
  81. * Completion callback for an internally cloned target BIO. This terminates the
  82. * target BIO when there are no more references to its context.
  83. */
  84. static void dmz_clone_endio(struct bio *clone)
  85. {
  86. struct dmz_bioctx *bioctx = clone->bi_private;
  87. blk_status_t status = clone->bi_status;
  88. bio_put(clone);
  89. dmz_bio_endio(bioctx->bio, status);
  90. }
  91. /*
  92. * Issue a clone of a target BIO. The clone may only partially process the
  93. * original target BIO.
  94. */
  95. static int dmz_submit_bio(struct dmz_target *dmz, struct dm_zone *zone,
  96. struct bio *bio, sector_t chunk_block,
  97. unsigned int nr_blocks)
  98. {
  99. struct dmz_bioctx *bioctx = dm_per_bio_data(bio, sizeof(struct dmz_bioctx));
  100. struct bio *clone;
  101. clone = bio_clone_fast(bio, GFP_NOIO, &dmz->bio_set);
  102. if (!clone)
  103. return -ENOMEM;
  104. bio_set_dev(clone, dmz->dev->bdev);
  105. clone->bi_iter.bi_sector =
  106. dmz_start_sect(dmz->metadata, zone) + dmz_blk2sect(chunk_block);
  107. clone->bi_iter.bi_size = dmz_blk2sect(nr_blocks) << SECTOR_SHIFT;
  108. clone->bi_end_io = dmz_clone_endio;
  109. clone->bi_private = bioctx;
  110. bio_advance(bio, clone->bi_iter.bi_size);
  111. refcount_inc(&bioctx->ref);
  112. generic_make_request(clone);
  113. if (bio_op(bio) == REQ_OP_WRITE && dmz_is_seq(zone))
  114. zone->wp_block += nr_blocks;
  115. return 0;
  116. }
  117. /*
  118. * Zero out pages of discarded blocks accessed by a read BIO.
  119. */
  120. static void dmz_handle_read_zero(struct dmz_target *dmz, struct bio *bio,
  121. sector_t chunk_block, unsigned int nr_blocks)
  122. {
  123. unsigned int size = nr_blocks << DMZ_BLOCK_SHIFT;
  124. /* Clear nr_blocks */
  125. swap(bio->bi_iter.bi_size, size);
  126. zero_fill_bio(bio);
  127. swap(bio->bi_iter.bi_size, size);
  128. bio_advance(bio, size);
  129. }
  130. /*
  131. * Process a read BIO.
  132. */
  133. static int dmz_handle_read(struct dmz_target *dmz, struct dm_zone *zone,
  134. struct bio *bio)
  135. {
  136. sector_t chunk_block = dmz_chunk_block(dmz->dev, dmz_bio_block(bio));
  137. unsigned int nr_blocks = dmz_bio_blocks(bio);
  138. sector_t end_block = chunk_block + nr_blocks;
  139. struct dm_zone *rzone, *bzone;
  140. int ret;
  141. /* Read into unmapped chunks need only zeroing the BIO buffer */
  142. if (!zone) {
  143. zero_fill_bio(bio);
  144. return 0;
  145. }
  146. dmz_dev_debug(dmz->dev, "READ chunk %llu -> %s zone %u, block %llu, %u blocks",
  147. (unsigned long long)dmz_bio_chunk(dmz->dev, bio),
  148. (dmz_is_rnd(zone) ? "RND" : "SEQ"),
  149. dmz_id(dmz->metadata, zone),
  150. (unsigned long long)chunk_block, nr_blocks);
  151. /* Check block validity to determine the read location */
  152. bzone = zone->bzone;
  153. while (chunk_block < end_block) {
  154. nr_blocks = 0;
  155. if (dmz_is_rnd(zone) || chunk_block < zone->wp_block) {
  156. /* Test block validity in the data zone */
  157. ret = dmz_block_valid(dmz->metadata, zone, chunk_block);
  158. if (ret < 0)
  159. return ret;
  160. if (ret > 0) {
  161. /* Read data zone blocks */
  162. nr_blocks = ret;
  163. rzone = zone;
  164. }
  165. }
  166. /*
  167. * No valid blocks found in the data zone.
  168. * Check the buffer zone, if there is one.
  169. */
  170. if (!nr_blocks && bzone) {
  171. ret = dmz_block_valid(dmz->metadata, bzone, chunk_block);
  172. if (ret < 0)
  173. return ret;
  174. if (ret > 0) {
  175. /* Read buffer zone blocks */
  176. nr_blocks = ret;
  177. rzone = bzone;
  178. }
  179. }
  180. if (nr_blocks) {
  181. /* Valid blocks found: read them */
  182. nr_blocks = min_t(unsigned int, nr_blocks, end_block - chunk_block);
  183. ret = dmz_submit_bio(dmz, rzone, bio, chunk_block, nr_blocks);
  184. if (ret)
  185. return ret;
  186. chunk_block += nr_blocks;
  187. } else {
  188. /* No valid block: zeroout the current BIO block */
  189. dmz_handle_read_zero(dmz, bio, chunk_block, 1);
  190. chunk_block++;
  191. }
  192. }
  193. return 0;
  194. }
  195. /*
  196. * Write blocks directly in a data zone, at the write pointer.
  197. * If a buffer zone is assigned, invalidate the blocks written
  198. * in place.
  199. */
  200. static int dmz_handle_direct_write(struct dmz_target *dmz,
  201. struct dm_zone *zone, struct bio *bio,
  202. sector_t chunk_block,
  203. unsigned int nr_blocks)
  204. {
  205. struct dmz_metadata *zmd = dmz->metadata;
  206. struct dm_zone *bzone = zone->bzone;
  207. int ret;
  208. if (dmz_is_readonly(zone))
  209. return -EROFS;
  210. /* Submit write */
  211. ret = dmz_submit_bio(dmz, zone, bio, chunk_block, nr_blocks);
  212. if (ret)
  213. return ret;
  214. /*
  215. * Validate the blocks in the data zone and invalidate
  216. * in the buffer zone, if there is one.
  217. */
  218. ret = dmz_validate_blocks(zmd, zone, chunk_block, nr_blocks);
  219. if (ret == 0 && bzone)
  220. ret = dmz_invalidate_blocks(zmd, bzone, chunk_block, nr_blocks);
  221. return ret;
  222. }
  223. /*
  224. * Write blocks in the buffer zone of @zone.
  225. * If no buffer zone is assigned yet, get one.
  226. * Called with @zone write locked.
  227. */
  228. static int dmz_handle_buffered_write(struct dmz_target *dmz,
  229. struct dm_zone *zone, struct bio *bio,
  230. sector_t chunk_block,
  231. unsigned int nr_blocks)
  232. {
  233. struct dmz_metadata *zmd = dmz->metadata;
  234. struct dm_zone *bzone;
  235. int ret;
  236. /* Get the buffer zone. One will be allocated if needed */
  237. bzone = dmz_get_chunk_buffer(zmd, zone);
  238. if (IS_ERR(bzone))
  239. return PTR_ERR(bzone);
  240. if (dmz_is_readonly(bzone))
  241. return -EROFS;
  242. /* Submit write */
  243. ret = dmz_submit_bio(dmz, bzone, bio, chunk_block, nr_blocks);
  244. if (ret)
  245. return ret;
  246. /*
  247. * Validate the blocks in the buffer zone
  248. * and invalidate in the data zone.
  249. */
  250. ret = dmz_validate_blocks(zmd, bzone, chunk_block, nr_blocks);
  251. if (ret == 0 && chunk_block < zone->wp_block)
  252. ret = dmz_invalidate_blocks(zmd, zone, chunk_block, nr_blocks);
  253. return ret;
  254. }
  255. /*
  256. * Process a write BIO.
  257. */
  258. static int dmz_handle_write(struct dmz_target *dmz, struct dm_zone *zone,
  259. struct bio *bio)
  260. {
  261. sector_t chunk_block = dmz_chunk_block(dmz->dev, dmz_bio_block(bio));
  262. unsigned int nr_blocks = dmz_bio_blocks(bio);
  263. if (!zone)
  264. return -ENOSPC;
  265. dmz_dev_debug(dmz->dev, "WRITE chunk %llu -> %s zone %u, block %llu, %u blocks",
  266. (unsigned long long)dmz_bio_chunk(dmz->dev, bio),
  267. (dmz_is_rnd(zone) ? "RND" : "SEQ"),
  268. dmz_id(dmz->metadata, zone),
  269. (unsigned long long)chunk_block, nr_blocks);
  270. if (dmz_is_rnd(zone) || chunk_block == zone->wp_block) {
  271. /*
  272. * zone is a random zone or it is a sequential zone
  273. * and the BIO is aligned to the zone write pointer:
  274. * direct write the zone.
  275. */
  276. return dmz_handle_direct_write(dmz, zone, bio, chunk_block, nr_blocks);
  277. }
  278. /*
  279. * This is an unaligned write in a sequential zone:
  280. * use buffered write.
  281. */
  282. return dmz_handle_buffered_write(dmz, zone, bio, chunk_block, nr_blocks);
  283. }
  284. /*
  285. * Process a discard BIO.
  286. */
  287. static int dmz_handle_discard(struct dmz_target *dmz, struct dm_zone *zone,
  288. struct bio *bio)
  289. {
  290. struct dmz_metadata *zmd = dmz->metadata;
  291. sector_t block = dmz_bio_block(bio);
  292. unsigned int nr_blocks = dmz_bio_blocks(bio);
  293. sector_t chunk_block = dmz_chunk_block(dmz->dev, block);
  294. int ret = 0;
  295. /* For unmapped chunks, there is nothing to do */
  296. if (!zone)
  297. return 0;
  298. if (dmz_is_readonly(zone))
  299. return -EROFS;
  300. dmz_dev_debug(dmz->dev, "DISCARD chunk %llu -> zone %u, block %llu, %u blocks",
  301. (unsigned long long)dmz_bio_chunk(dmz->dev, bio),
  302. dmz_id(zmd, zone),
  303. (unsigned long long)chunk_block, nr_blocks);
  304. /*
  305. * Invalidate blocks in the data zone and its
  306. * buffer zone if one is mapped.
  307. */
  308. if (dmz_is_rnd(zone) || chunk_block < zone->wp_block)
  309. ret = dmz_invalidate_blocks(zmd, zone, chunk_block, nr_blocks);
  310. if (ret == 0 && zone->bzone)
  311. ret = dmz_invalidate_blocks(zmd, zone->bzone,
  312. chunk_block, nr_blocks);
  313. return ret;
  314. }
  315. /*
  316. * Process a BIO.
  317. */
  318. static void dmz_handle_bio(struct dmz_target *dmz, struct dm_chunk_work *cw,
  319. struct bio *bio)
  320. {
  321. struct dmz_bioctx *bioctx = dm_per_bio_data(bio, sizeof(struct dmz_bioctx));
  322. struct dmz_metadata *zmd = dmz->metadata;
  323. struct dm_zone *zone;
  324. int ret;
  325. /*
  326. * Write may trigger a zone allocation. So make sure the
  327. * allocation can succeed.
  328. */
  329. if (bio_op(bio) == REQ_OP_WRITE)
  330. dmz_schedule_reclaim(dmz->reclaim);
  331. dmz_lock_metadata(zmd);
  332. if (dmz->dev->flags & DMZ_BDEV_DYING) {
  333. ret = -EIO;
  334. goto out;
  335. }
  336. /*
  337. * Get the data zone mapping the chunk. There may be no
  338. * mapping for read and discard. If a mapping is obtained,
  339. + the zone returned will be set to active state.
  340. */
  341. zone = dmz_get_chunk_mapping(zmd, dmz_bio_chunk(dmz->dev, bio),
  342. bio_op(bio));
  343. if (IS_ERR(zone)) {
  344. ret = PTR_ERR(zone);
  345. goto out;
  346. }
  347. /* Process the BIO */
  348. if (zone) {
  349. dmz_activate_zone(zone);
  350. bioctx->zone = zone;
  351. }
  352. switch (bio_op(bio)) {
  353. case REQ_OP_READ:
  354. ret = dmz_handle_read(dmz, zone, bio);
  355. break;
  356. case REQ_OP_WRITE:
  357. ret = dmz_handle_write(dmz, zone, bio);
  358. break;
  359. case REQ_OP_DISCARD:
  360. case REQ_OP_WRITE_ZEROES:
  361. ret = dmz_handle_discard(dmz, zone, bio);
  362. break;
  363. default:
  364. dmz_dev_err(dmz->dev, "Unsupported BIO operation 0x%x",
  365. bio_op(bio));
  366. ret = -EIO;
  367. }
  368. /*
  369. * Release the chunk mapping. This will check that the mapping
  370. * is still valid, that is, that the zone used still has valid blocks.
  371. */
  372. if (zone)
  373. dmz_put_chunk_mapping(zmd, zone);
  374. out:
  375. dmz_bio_endio(bio, errno_to_blk_status(ret));
  376. dmz_unlock_metadata(zmd);
  377. }
  378. /*
  379. * Increment a chunk reference counter.
  380. */
  381. static inline void dmz_get_chunk_work(struct dm_chunk_work *cw)
  382. {
  383. refcount_inc(&cw->refcount);
  384. }
  385. /*
  386. * Decrement a chunk work reference count and
  387. * free it if it becomes 0.
  388. */
  389. static void dmz_put_chunk_work(struct dm_chunk_work *cw)
  390. {
  391. if (refcount_dec_and_test(&cw->refcount)) {
  392. WARN_ON(!bio_list_empty(&cw->bio_list));
  393. radix_tree_delete(&cw->target->chunk_rxtree, cw->chunk);
  394. kfree(cw);
  395. }
  396. }
  397. /*
  398. * Chunk BIO work function.
  399. */
  400. static void dmz_chunk_work(struct work_struct *work)
  401. {
  402. struct dm_chunk_work *cw = container_of(work, struct dm_chunk_work, work);
  403. struct dmz_target *dmz = cw->target;
  404. struct bio *bio;
  405. mutex_lock(&dmz->chunk_lock);
  406. /* Process the chunk BIOs */
  407. while ((bio = bio_list_pop(&cw->bio_list))) {
  408. mutex_unlock(&dmz->chunk_lock);
  409. dmz_handle_bio(dmz, cw, bio);
  410. mutex_lock(&dmz->chunk_lock);
  411. dmz_put_chunk_work(cw);
  412. }
  413. /* Queueing the work incremented the work refcount */
  414. dmz_put_chunk_work(cw);
  415. mutex_unlock(&dmz->chunk_lock);
  416. }
  417. /*
  418. * Flush work.
  419. */
  420. static void dmz_flush_work(struct work_struct *work)
  421. {
  422. struct dmz_target *dmz = container_of(work, struct dmz_target, flush_work.work);
  423. struct bio *bio;
  424. int ret;
  425. /* Flush dirty metadata blocks */
  426. ret = dmz_flush_metadata(dmz->metadata);
  427. if (ret)
  428. dmz_dev_debug(dmz->dev, "Metadata flush failed, rc=%d\n", ret);
  429. /* Process queued flush requests */
  430. while (1) {
  431. spin_lock(&dmz->flush_lock);
  432. bio = bio_list_pop(&dmz->flush_list);
  433. spin_unlock(&dmz->flush_lock);
  434. if (!bio)
  435. break;
  436. dmz_bio_endio(bio, errno_to_blk_status(ret));
  437. }
  438. queue_delayed_work(dmz->flush_wq, &dmz->flush_work, DMZ_FLUSH_PERIOD);
  439. }
  440. /*
  441. * Get a chunk work and start it to process a new BIO.
  442. * If the BIO chunk has no work yet, create one.
  443. */
  444. static int dmz_queue_chunk_work(struct dmz_target *dmz, struct bio *bio)
  445. {
  446. unsigned int chunk = dmz_bio_chunk(dmz->dev, bio);
  447. struct dm_chunk_work *cw;
  448. int ret = 0;
  449. mutex_lock(&dmz->chunk_lock);
  450. /* Get the BIO chunk work. If one is not active yet, create one */
  451. cw = radix_tree_lookup(&dmz->chunk_rxtree, chunk);
  452. if (cw) {
  453. dmz_get_chunk_work(cw);
  454. } else {
  455. /* Create a new chunk work */
  456. cw = kmalloc(sizeof(struct dm_chunk_work), GFP_NOIO);
  457. if (unlikely(!cw)) {
  458. ret = -ENOMEM;
  459. goto out;
  460. }
  461. INIT_WORK(&cw->work, dmz_chunk_work);
  462. refcount_set(&cw->refcount, 1);
  463. cw->target = dmz;
  464. cw->chunk = chunk;
  465. bio_list_init(&cw->bio_list);
  466. ret = radix_tree_insert(&dmz->chunk_rxtree, chunk, cw);
  467. if (unlikely(ret)) {
  468. kfree(cw);
  469. goto out;
  470. }
  471. }
  472. bio_list_add(&cw->bio_list, bio);
  473. dmz_reclaim_bio_acc(dmz->reclaim);
  474. if (queue_work(dmz->chunk_wq, &cw->work))
  475. dmz_get_chunk_work(cw);
  476. out:
  477. mutex_unlock(&dmz->chunk_lock);
  478. return ret;
  479. }
  480. /*
  481. * Check if the backing device is being removed. If it's on the way out,
  482. * start failing I/O. Reclaim and metadata components also call this
  483. * function to cleanly abort operation in the event of such failure.
  484. */
  485. bool dmz_bdev_is_dying(struct dmz_dev *dmz_dev)
  486. {
  487. if (dmz_dev->flags & DMZ_BDEV_DYING)
  488. return true;
  489. if (dmz_dev->flags & DMZ_CHECK_BDEV)
  490. return !dmz_check_bdev(dmz_dev);
  491. if (blk_queue_dying(bdev_get_queue(dmz_dev->bdev))) {
  492. dmz_dev_warn(dmz_dev, "Backing device queue dying");
  493. dmz_dev->flags |= DMZ_BDEV_DYING;
  494. }
  495. return dmz_dev->flags & DMZ_BDEV_DYING;
  496. }
  497. /*
  498. * Check the backing device availability. This detects such events as
  499. * backing device going offline due to errors, media removals, etc.
  500. * This check is less efficient than dmz_bdev_is_dying() and should
  501. * only be performed as a part of error handling.
  502. */
  503. bool dmz_check_bdev(struct dmz_dev *dmz_dev)
  504. {
  505. struct gendisk *disk;
  506. dmz_dev->flags &= ~DMZ_CHECK_BDEV;
  507. if (dmz_bdev_is_dying(dmz_dev))
  508. return false;
  509. disk = dmz_dev->bdev->bd_disk;
  510. if (disk->fops->check_events &&
  511. disk->fops->check_events(disk, 0) & DISK_EVENT_MEDIA_CHANGE) {
  512. dmz_dev_warn(dmz_dev, "Backing device offline");
  513. dmz_dev->flags |= DMZ_BDEV_DYING;
  514. }
  515. return !(dmz_dev->flags & DMZ_BDEV_DYING);
  516. }
  517. /*
  518. * Process a new BIO.
  519. */
  520. static int dmz_map(struct dm_target *ti, struct bio *bio)
  521. {
  522. struct dmz_target *dmz = ti->private;
  523. struct dmz_dev *dev = dmz->dev;
  524. struct dmz_bioctx *bioctx = dm_per_bio_data(bio, sizeof(struct dmz_bioctx));
  525. sector_t sector = bio->bi_iter.bi_sector;
  526. unsigned int nr_sectors = bio_sectors(bio);
  527. sector_t chunk_sector;
  528. int ret;
  529. if (dmz_bdev_is_dying(dmz->dev))
  530. return DM_MAPIO_KILL;
  531. dmz_dev_debug(dev, "BIO op %d sector %llu + %u => chunk %llu, block %llu, %u blocks",
  532. bio_op(bio), (unsigned long long)sector, nr_sectors,
  533. (unsigned long long)dmz_bio_chunk(dmz->dev, bio),
  534. (unsigned long long)dmz_chunk_block(dmz->dev, dmz_bio_block(bio)),
  535. (unsigned int)dmz_bio_blocks(bio));
  536. bio_set_dev(bio, dev->bdev);
  537. if (!nr_sectors && bio_op(bio) != REQ_OP_WRITE)
  538. return DM_MAPIO_REMAPPED;
  539. /* The BIO should be block aligned */
  540. if ((nr_sectors & DMZ_BLOCK_SECTORS_MASK) || (sector & DMZ_BLOCK_SECTORS_MASK))
  541. return DM_MAPIO_KILL;
  542. /* Initialize the BIO context */
  543. bioctx->target = dmz;
  544. bioctx->zone = NULL;
  545. bioctx->bio = bio;
  546. refcount_set(&bioctx->ref, 1);
  547. /* Set the BIO pending in the flush list */
  548. if (!nr_sectors && bio_op(bio) == REQ_OP_WRITE) {
  549. spin_lock(&dmz->flush_lock);
  550. bio_list_add(&dmz->flush_list, bio);
  551. spin_unlock(&dmz->flush_lock);
  552. mod_delayed_work(dmz->flush_wq, &dmz->flush_work, 0);
  553. return DM_MAPIO_SUBMITTED;
  554. }
  555. /* Split zone BIOs to fit entirely into a zone */
  556. chunk_sector = sector & (dev->zone_nr_sectors - 1);
  557. if (chunk_sector + nr_sectors > dev->zone_nr_sectors)
  558. dm_accept_partial_bio(bio, dev->zone_nr_sectors - chunk_sector);
  559. /* Now ready to handle this BIO */
  560. ret = dmz_queue_chunk_work(dmz, bio);
  561. if (ret) {
  562. dmz_dev_debug(dmz->dev,
  563. "BIO op %d, can't process chunk %llu, err %i\n",
  564. bio_op(bio), (u64)dmz_bio_chunk(dmz->dev, bio),
  565. ret);
  566. return DM_MAPIO_REQUEUE;
  567. }
  568. return DM_MAPIO_SUBMITTED;
  569. }
  570. /*
  571. * Get zoned device information.
  572. */
  573. static int dmz_get_zoned_device(struct dm_target *ti, char *path)
  574. {
  575. struct dmz_target *dmz = ti->private;
  576. struct request_queue *q;
  577. struct dmz_dev *dev;
  578. sector_t aligned_capacity;
  579. int ret;
  580. /* Get the target device */
  581. ret = dm_get_device(ti, path, dm_table_get_mode(ti->table), &dmz->ddev);
  582. if (ret) {
  583. ti->error = "Get target device failed";
  584. dmz->ddev = NULL;
  585. return ret;
  586. }
  587. dev = kzalloc(sizeof(struct dmz_dev), GFP_KERNEL);
  588. if (!dev) {
  589. ret = -ENOMEM;
  590. goto err;
  591. }
  592. dev->bdev = dmz->ddev->bdev;
  593. (void)bdevname(dev->bdev, dev->name);
  594. if (bdev_zoned_model(dev->bdev) == BLK_ZONED_NONE) {
  595. ti->error = "Not a zoned block device";
  596. ret = -EINVAL;
  597. goto err;
  598. }
  599. q = bdev_get_queue(dev->bdev);
  600. dev->capacity = i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT;
  601. aligned_capacity = dev->capacity &
  602. ~((sector_t)blk_queue_zone_sectors(q) - 1);
  603. if (ti->begin ||
  604. ((ti->len != dev->capacity) && (ti->len != aligned_capacity))) {
  605. ti->error = "Partial mapping not supported";
  606. ret = -EINVAL;
  607. goto err;
  608. }
  609. dev->zone_nr_sectors = blk_queue_zone_sectors(q);
  610. dev->zone_nr_sectors_shift = ilog2(dev->zone_nr_sectors);
  611. dev->zone_nr_blocks = dmz_sect2blk(dev->zone_nr_sectors);
  612. dev->zone_nr_blocks_shift = ilog2(dev->zone_nr_blocks);
  613. dev->nr_zones = blkdev_nr_zones(dev->bdev);
  614. dmz->dev = dev;
  615. return 0;
  616. err:
  617. dm_put_device(ti, dmz->ddev);
  618. kfree(dev);
  619. return ret;
  620. }
  621. /*
  622. * Cleanup zoned device information.
  623. */
  624. static void dmz_put_zoned_device(struct dm_target *ti)
  625. {
  626. struct dmz_target *dmz = ti->private;
  627. dm_put_device(ti, dmz->ddev);
  628. kfree(dmz->dev);
  629. dmz->dev = NULL;
  630. }
  631. /*
  632. * Setup target.
  633. */
  634. static int dmz_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  635. {
  636. struct dmz_target *dmz;
  637. struct dmz_dev *dev;
  638. int ret;
  639. /* Check arguments */
  640. if (argc != 1) {
  641. ti->error = "Invalid argument count";
  642. return -EINVAL;
  643. }
  644. /* Allocate and initialize the target descriptor */
  645. dmz = kzalloc(sizeof(struct dmz_target), GFP_KERNEL);
  646. if (!dmz) {
  647. ti->error = "Unable to allocate the zoned target descriptor";
  648. return -ENOMEM;
  649. }
  650. ti->private = dmz;
  651. /* Get the target zoned block device */
  652. ret = dmz_get_zoned_device(ti, argv[0]);
  653. if (ret) {
  654. dmz->ddev = NULL;
  655. goto err;
  656. }
  657. /* Initialize metadata */
  658. dev = dmz->dev;
  659. ret = dmz_ctr_metadata(dev, &dmz->metadata);
  660. if (ret) {
  661. ti->error = "Metadata initialization failed";
  662. goto err_dev;
  663. }
  664. /* Set target (no write same support) */
  665. ti->max_io_len = dev->zone_nr_sectors;
  666. ti->num_flush_bios = 1;
  667. ti->num_discard_bios = 1;
  668. ti->num_write_zeroes_bios = 1;
  669. ti->per_io_data_size = sizeof(struct dmz_bioctx);
  670. ti->flush_supported = true;
  671. ti->discards_supported = true;
  672. /* The exposed capacity is the number of chunks that can be mapped */
  673. ti->len = (sector_t)dmz_nr_chunks(dmz->metadata) << dev->zone_nr_sectors_shift;
  674. /* Zone BIO */
  675. ret = bioset_init(&dmz->bio_set, DMZ_MIN_BIOS, 0, 0);
  676. if (ret) {
  677. ti->error = "Create BIO set failed";
  678. goto err_meta;
  679. }
  680. /* Chunk BIO work */
  681. mutex_init(&dmz->chunk_lock);
  682. INIT_RADIX_TREE(&dmz->chunk_rxtree, GFP_NOIO);
  683. dmz->chunk_wq = alloc_workqueue("dmz_cwq_%s", WQ_MEM_RECLAIM | WQ_UNBOUND,
  684. 0, dev->name);
  685. if (!dmz->chunk_wq) {
  686. ti->error = "Create chunk workqueue failed";
  687. ret = -ENOMEM;
  688. goto err_bio;
  689. }
  690. /* Flush work */
  691. spin_lock_init(&dmz->flush_lock);
  692. bio_list_init(&dmz->flush_list);
  693. INIT_DELAYED_WORK(&dmz->flush_work, dmz_flush_work);
  694. dmz->flush_wq = alloc_ordered_workqueue("dmz_fwq_%s", WQ_MEM_RECLAIM,
  695. dev->name);
  696. if (!dmz->flush_wq) {
  697. ti->error = "Create flush workqueue failed";
  698. ret = -ENOMEM;
  699. goto err_cwq;
  700. }
  701. mod_delayed_work(dmz->flush_wq, &dmz->flush_work, DMZ_FLUSH_PERIOD);
  702. /* Initialize reclaim */
  703. ret = dmz_ctr_reclaim(dev, dmz->metadata, &dmz->reclaim);
  704. if (ret) {
  705. ti->error = "Zone reclaim initialization failed";
  706. goto err_fwq;
  707. }
  708. dmz_dev_info(dev, "Target device: %llu 512-byte logical sectors (%llu blocks)",
  709. (unsigned long long)ti->len,
  710. (unsigned long long)dmz_sect2blk(ti->len));
  711. return 0;
  712. err_fwq:
  713. destroy_workqueue(dmz->flush_wq);
  714. err_cwq:
  715. destroy_workqueue(dmz->chunk_wq);
  716. err_bio:
  717. mutex_destroy(&dmz->chunk_lock);
  718. bioset_exit(&dmz->bio_set);
  719. err_meta:
  720. dmz_dtr_metadata(dmz->metadata);
  721. err_dev:
  722. dmz_put_zoned_device(ti);
  723. err:
  724. kfree(dmz);
  725. return ret;
  726. }
  727. /*
  728. * Cleanup target.
  729. */
  730. static void dmz_dtr(struct dm_target *ti)
  731. {
  732. struct dmz_target *dmz = ti->private;
  733. flush_workqueue(dmz->chunk_wq);
  734. destroy_workqueue(dmz->chunk_wq);
  735. dmz_dtr_reclaim(dmz->reclaim);
  736. cancel_delayed_work_sync(&dmz->flush_work);
  737. destroy_workqueue(dmz->flush_wq);
  738. (void) dmz_flush_metadata(dmz->metadata);
  739. dmz_dtr_metadata(dmz->metadata);
  740. bioset_exit(&dmz->bio_set);
  741. dmz_put_zoned_device(ti);
  742. mutex_destroy(&dmz->chunk_lock);
  743. kfree(dmz);
  744. }
  745. /*
  746. * Setup target request queue limits.
  747. */
  748. static void dmz_io_hints(struct dm_target *ti, struct queue_limits *limits)
  749. {
  750. struct dmz_target *dmz = ti->private;
  751. unsigned int chunk_sectors = dmz->dev->zone_nr_sectors;
  752. limits->logical_block_size = DMZ_BLOCK_SIZE;
  753. limits->physical_block_size = DMZ_BLOCK_SIZE;
  754. blk_limits_io_min(limits, DMZ_BLOCK_SIZE);
  755. blk_limits_io_opt(limits, DMZ_BLOCK_SIZE);
  756. limits->discard_alignment = DMZ_BLOCK_SIZE;
  757. limits->discard_granularity = DMZ_BLOCK_SIZE;
  758. limits->max_discard_sectors = chunk_sectors;
  759. limits->max_hw_discard_sectors = chunk_sectors;
  760. limits->max_write_zeroes_sectors = chunk_sectors;
  761. /* FS hint to try to align to the device zone size */
  762. limits->chunk_sectors = chunk_sectors;
  763. limits->max_sectors = chunk_sectors;
  764. /* We are exposing a drive-managed zoned block device */
  765. limits->zoned = BLK_ZONED_NONE;
  766. }
  767. /*
  768. * Pass on ioctl to the backend device.
  769. */
  770. static int dmz_prepare_ioctl(struct dm_target *ti, struct block_device **bdev)
  771. {
  772. struct dmz_target *dmz = ti->private;
  773. if (!dmz_check_bdev(dmz->dev))
  774. return -EIO;
  775. *bdev = dmz->dev->bdev;
  776. return 0;
  777. }
  778. /*
  779. * Stop works on suspend.
  780. */
  781. static void dmz_suspend(struct dm_target *ti)
  782. {
  783. struct dmz_target *dmz = ti->private;
  784. flush_workqueue(dmz->chunk_wq);
  785. dmz_suspend_reclaim(dmz->reclaim);
  786. cancel_delayed_work_sync(&dmz->flush_work);
  787. }
  788. /*
  789. * Restart works on resume or if suspend failed.
  790. */
  791. static void dmz_resume(struct dm_target *ti)
  792. {
  793. struct dmz_target *dmz = ti->private;
  794. queue_delayed_work(dmz->flush_wq, &dmz->flush_work, DMZ_FLUSH_PERIOD);
  795. dmz_resume_reclaim(dmz->reclaim);
  796. }
  797. static int dmz_iterate_devices(struct dm_target *ti,
  798. iterate_devices_callout_fn fn, void *data)
  799. {
  800. struct dmz_target *dmz = ti->private;
  801. struct dmz_dev *dev = dmz->dev;
  802. sector_t capacity = dev->capacity & ~(dev->zone_nr_sectors - 1);
  803. return fn(ti, dmz->ddev, 0, capacity, data);
  804. }
  805. static struct target_type dmz_type = {
  806. .name = "zoned",
  807. .version = {1, 0, 0},
  808. .features = DM_TARGET_SINGLETON | DM_TARGET_ZONED_HM,
  809. .module = THIS_MODULE,
  810. .ctr = dmz_ctr,
  811. .dtr = dmz_dtr,
  812. .map = dmz_map,
  813. .io_hints = dmz_io_hints,
  814. .prepare_ioctl = dmz_prepare_ioctl,
  815. .postsuspend = dmz_suspend,
  816. .resume = dmz_resume,
  817. .iterate_devices = dmz_iterate_devices,
  818. };
  819. static int __init dmz_init(void)
  820. {
  821. return dm_register_target(&dmz_type);
  822. }
  823. static void __exit dmz_exit(void)
  824. {
  825. dm_unregister_target(&dmz_type);
  826. }
  827. module_init(dmz_init);
  828. module_exit(dmz_exit);
  829. MODULE_DESCRIPTION(DM_NAME " target for zoned block devices");
  830. MODULE_AUTHOR("Damien Le Moal <damien.lemoal@wdc.com>");
  831. MODULE_LICENSE("GPL");