dm-zoned-target.c 23 KB

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