dm-zoned-target.c 24 KB

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