dm-zoned-target.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  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_dev *dev;
  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 int nr_ddevs;
  36. unsigned int flags;
  37. /* Zoned block device information */
  38. struct dmz_dev *dev;
  39. /* For metadata handling */
  40. struct dmz_metadata *metadata;
  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 =
  63. 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 (bioctx->dev && bio->bi_status != BLK_STS_OK)
  67. bioctx->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 =
  100. dm_per_bio_data(bio, sizeof(struct dmz_bioctx));
  101. struct dmz_dev *dev = zone->dev;
  102. struct bio *clone;
  103. if (dev->flags & DMZ_BDEV_DYING)
  104. return -EIO;
  105. clone = bio_alloc_clone(dev->bdev, bio, GFP_NOIO, &dmz->bio_set);
  106. if (!clone)
  107. return -ENOMEM;
  108. bioctx->dev = dev;
  109. clone->bi_iter.bi_sector =
  110. dmz_start_sect(dmz->metadata, zone) + dmz_blk2sect(chunk_block);
  111. clone->bi_iter.bi_size = dmz_blk2sect(nr_blocks) << SECTOR_SHIFT;
  112. clone->bi_end_io = dmz_clone_endio;
  113. clone->bi_private = bioctx;
  114. bio_advance(bio, clone->bi_iter.bi_size);
  115. refcount_inc(&bioctx->ref);
  116. submit_bio_noacct(clone);
  117. if (bio_op(bio) == REQ_OP_WRITE && dmz_is_seq(zone))
  118. zone->wp_block += nr_blocks;
  119. return 0;
  120. }
  121. /*
  122. * Zero out pages of discarded blocks accessed by a read BIO.
  123. */
  124. static void dmz_handle_read_zero(struct dmz_target *dmz, struct bio *bio,
  125. sector_t chunk_block, unsigned int nr_blocks)
  126. {
  127. unsigned int size = nr_blocks << DMZ_BLOCK_SHIFT;
  128. /* Clear nr_blocks */
  129. swap(bio->bi_iter.bi_size, size);
  130. zero_fill_bio(bio);
  131. swap(bio->bi_iter.bi_size, size);
  132. bio_advance(bio, size);
  133. }
  134. /*
  135. * Process a read BIO.
  136. */
  137. static int dmz_handle_read(struct dmz_target *dmz, struct dm_zone *zone,
  138. struct bio *bio)
  139. {
  140. struct dmz_metadata *zmd = dmz->metadata;
  141. sector_t chunk_block = dmz_chunk_block(zmd, dmz_bio_block(bio));
  142. unsigned int nr_blocks = dmz_bio_blocks(bio);
  143. sector_t end_block = chunk_block + nr_blocks;
  144. struct dm_zone *rzone, *bzone;
  145. int ret;
  146. /* Read into unmapped chunks need only zeroing the BIO buffer */
  147. if (!zone) {
  148. zero_fill_bio(bio);
  149. return 0;
  150. }
  151. DMDEBUG("(%s): READ chunk %llu -> %s zone %u, block %llu, %u blocks",
  152. dmz_metadata_label(zmd),
  153. (unsigned long long)dmz_bio_chunk(zmd, bio),
  154. (dmz_is_rnd(zone) ? "RND" :
  155. (dmz_is_cache(zone) ? "CACHE" : "SEQ")),
  156. zone->id,
  157. (unsigned long long)chunk_block, nr_blocks);
  158. /* Check block validity to determine the read location */
  159. bzone = zone->bzone;
  160. while (chunk_block < end_block) {
  161. nr_blocks = 0;
  162. if (dmz_is_rnd(zone) || dmz_is_cache(zone) ||
  163. chunk_block < zone->wp_block) {
  164. /* Test block validity in the data zone */
  165. ret = dmz_block_valid(zmd, zone, chunk_block);
  166. if (ret < 0)
  167. return ret;
  168. if (ret > 0) {
  169. /* Read data zone blocks */
  170. nr_blocks = ret;
  171. rzone = zone;
  172. }
  173. }
  174. /*
  175. * No valid blocks found in the data zone.
  176. * Check the buffer zone, if there is one.
  177. */
  178. if (!nr_blocks && bzone) {
  179. ret = dmz_block_valid(zmd, bzone, chunk_block);
  180. if (ret < 0)
  181. return ret;
  182. if (ret > 0) {
  183. /* Read buffer zone blocks */
  184. nr_blocks = ret;
  185. rzone = bzone;
  186. }
  187. }
  188. if (nr_blocks) {
  189. /* Valid blocks found: read them */
  190. nr_blocks = min_t(unsigned int, nr_blocks,
  191. end_block - chunk_block);
  192. ret = dmz_submit_bio(dmz, rzone, bio,
  193. chunk_block, nr_blocks);
  194. if (ret)
  195. return ret;
  196. chunk_block += nr_blocks;
  197. } else {
  198. /* No valid block: zeroout the current BIO block */
  199. dmz_handle_read_zero(dmz, bio, chunk_block, 1);
  200. chunk_block++;
  201. }
  202. }
  203. return 0;
  204. }
  205. /*
  206. * Write blocks directly in a data zone, at the write pointer.
  207. * If a buffer zone is assigned, invalidate the blocks written
  208. * in place.
  209. */
  210. static int dmz_handle_direct_write(struct dmz_target *dmz,
  211. struct dm_zone *zone, struct bio *bio,
  212. sector_t chunk_block,
  213. unsigned int nr_blocks)
  214. {
  215. struct dmz_metadata *zmd = dmz->metadata;
  216. struct dm_zone *bzone = zone->bzone;
  217. int ret;
  218. if (dmz_is_readonly(zone))
  219. return -EROFS;
  220. /* Submit write */
  221. ret = dmz_submit_bio(dmz, zone, bio, chunk_block, nr_blocks);
  222. if (ret)
  223. return ret;
  224. /*
  225. * Validate the blocks in the data zone and invalidate
  226. * in the buffer zone, if there is one.
  227. */
  228. ret = dmz_validate_blocks(zmd, zone, chunk_block, nr_blocks);
  229. if (ret == 0 && bzone)
  230. ret = dmz_invalidate_blocks(zmd, bzone, chunk_block, nr_blocks);
  231. return ret;
  232. }
  233. /*
  234. * Write blocks in the buffer zone of @zone.
  235. * If no buffer zone is assigned yet, get one.
  236. * Called with @zone write locked.
  237. */
  238. static int dmz_handle_buffered_write(struct dmz_target *dmz,
  239. struct dm_zone *zone, struct bio *bio,
  240. sector_t chunk_block,
  241. unsigned int nr_blocks)
  242. {
  243. struct dmz_metadata *zmd = dmz->metadata;
  244. struct dm_zone *bzone;
  245. int ret;
  246. /* Get the buffer zone. One will be allocated if needed */
  247. bzone = dmz_get_chunk_buffer(zmd, zone);
  248. if (IS_ERR(bzone))
  249. return PTR_ERR(bzone);
  250. if (dmz_is_readonly(bzone))
  251. return -EROFS;
  252. /* Submit write */
  253. ret = dmz_submit_bio(dmz, bzone, bio, chunk_block, nr_blocks);
  254. if (ret)
  255. return ret;
  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. struct dmz_metadata *zmd = dmz->metadata;
  272. sector_t chunk_block = dmz_chunk_block(zmd, dmz_bio_block(bio));
  273. unsigned int nr_blocks = dmz_bio_blocks(bio);
  274. if (!zone)
  275. return -ENOSPC;
  276. DMDEBUG("(%s): WRITE chunk %llu -> %s zone %u, block %llu, %u blocks",
  277. dmz_metadata_label(zmd),
  278. (unsigned long long)dmz_bio_chunk(zmd, bio),
  279. (dmz_is_rnd(zone) ? "RND" :
  280. (dmz_is_cache(zone) ? "CACHE" : "SEQ")),
  281. zone->id,
  282. (unsigned long long)chunk_block, nr_blocks);
  283. if (dmz_is_rnd(zone) || dmz_is_cache(zone) ||
  284. chunk_block == zone->wp_block) {
  285. /*
  286. * zone is a random zone or it is a sequential zone
  287. * and the BIO is aligned to the zone write pointer:
  288. * direct write the zone.
  289. */
  290. return dmz_handle_direct_write(dmz, zone, bio,
  291. chunk_block, nr_blocks);
  292. }
  293. /*
  294. * This is an unaligned write in a sequential zone:
  295. * use buffered write.
  296. */
  297. return dmz_handle_buffered_write(dmz, zone, bio, chunk_block, nr_blocks);
  298. }
  299. /*
  300. * Process a discard BIO.
  301. */
  302. static int dmz_handle_discard(struct dmz_target *dmz, struct dm_zone *zone,
  303. struct bio *bio)
  304. {
  305. struct dmz_metadata *zmd = dmz->metadata;
  306. sector_t block = dmz_bio_block(bio);
  307. unsigned int nr_blocks = dmz_bio_blocks(bio);
  308. sector_t chunk_block = dmz_chunk_block(zmd, block);
  309. int ret = 0;
  310. /* For unmapped chunks, there is nothing to do */
  311. if (!zone)
  312. return 0;
  313. if (dmz_is_readonly(zone))
  314. return -EROFS;
  315. DMDEBUG("(%s): DISCARD chunk %llu -> zone %u, block %llu, %u blocks",
  316. dmz_metadata_label(dmz->metadata),
  317. (unsigned long long)dmz_bio_chunk(zmd, bio),
  318. zone->id,
  319. (unsigned long long)chunk_block, nr_blocks);
  320. /*
  321. * Invalidate blocks in the data zone and its
  322. * buffer zone if one is mapped.
  323. */
  324. if (dmz_is_rnd(zone) || dmz_is_cache(zone) ||
  325. chunk_block < zone->wp_block)
  326. ret = dmz_invalidate_blocks(zmd, zone, chunk_block, nr_blocks);
  327. if (ret == 0 && zone->bzone)
  328. ret = dmz_invalidate_blocks(zmd, zone->bzone,
  329. chunk_block, nr_blocks);
  330. return ret;
  331. }
  332. /*
  333. * Process a BIO.
  334. */
  335. static void dmz_handle_bio(struct dmz_target *dmz, struct dm_chunk_work *cw,
  336. struct bio *bio)
  337. {
  338. struct dmz_bioctx *bioctx =
  339. dm_per_bio_data(bio, sizeof(struct dmz_bioctx));
  340. struct dmz_metadata *zmd = dmz->metadata;
  341. struct dm_zone *zone;
  342. int ret;
  343. dmz_lock_metadata(zmd);
  344. /*
  345. * Get the data zone mapping the chunk. There may be no
  346. * mapping for read and discard. If a mapping is obtained,
  347. + the zone returned will be set to active state.
  348. */
  349. zone = dmz_get_chunk_mapping(zmd, dmz_bio_chunk(zmd, bio),
  350. bio_op(bio));
  351. if (IS_ERR(zone)) {
  352. ret = PTR_ERR(zone);
  353. goto out;
  354. }
  355. /* Process the BIO */
  356. if (zone) {
  357. dmz_activate_zone(zone);
  358. bioctx->zone = zone;
  359. dmz_reclaim_bio_acc(zone->dev->reclaim);
  360. }
  361. switch (bio_op(bio)) {
  362. case REQ_OP_READ:
  363. ret = dmz_handle_read(dmz, zone, bio);
  364. break;
  365. case REQ_OP_WRITE:
  366. ret = dmz_handle_write(dmz, zone, bio);
  367. break;
  368. case REQ_OP_DISCARD:
  369. case REQ_OP_WRITE_ZEROES:
  370. ret = dmz_handle_discard(dmz, zone, bio);
  371. break;
  372. default:
  373. DMERR("(%s): Unsupported BIO operation 0x%x",
  374. dmz_metadata_label(dmz->metadata), bio_op(bio));
  375. ret = -EIO;
  376. }
  377. /*
  378. * Release the chunk mapping. This will check that the mapping
  379. * is still valid, that is, that the zone used still has valid blocks.
  380. */
  381. if (zone)
  382. dmz_put_chunk_mapping(zmd, zone);
  383. out:
  384. dmz_bio_endio(bio, errno_to_blk_status(ret));
  385. dmz_unlock_metadata(zmd);
  386. }
  387. /*
  388. * Increment a chunk reference counter.
  389. */
  390. static inline void dmz_get_chunk_work(struct dm_chunk_work *cw)
  391. {
  392. refcount_inc(&cw->refcount);
  393. }
  394. /*
  395. * Decrement a chunk work reference count and
  396. * free it if it becomes 0.
  397. */
  398. static void dmz_put_chunk_work(struct dm_chunk_work *cw)
  399. {
  400. if (refcount_dec_and_test(&cw->refcount)) {
  401. WARN_ON(!bio_list_empty(&cw->bio_list));
  402. radix_tree_delete(&cw->target->chunk_rxtree, cw->chunk);
  403. kfree(cw);
  404. }
  405. }
  406. /*
  407. * Chunk BIO work function.
  408. */
  409. static void dmz_chunk_work(struct work_struct *work)
  410. {
  411. struct dm_chunk_work *cw = container_of(work, struct dm_chunk_work, work);
  412. struct dmz_target *dmz = cw->target;
  413. struct bio *bio;
  414. mutex_lock(&dmz->chunk_lock);
  415. /* Process the chunk BIOs */
  416. while ((bio = bio_list_pop(&cw->bio_list))) {
  417. mutex_unlock(&dmz->chunk_lock);
  418. dmz_handle_bio(dmz, cw, bio);
  419. mutex_lock(&dmz->chunk_lock);
  420. dmz_put_chunk_work(cw);
  421. }
  422. /* Queueing the work incremented the work refcount */
  423. dmz_put_chunk_work(cw);
  424. mutex_unlock(&dmz->chunk_lock);
  425. }
  426. /*
  427. * Flush work.
  428. */
  429. static void dmz_flush_work(struct work_struct *work)
  430. {
  431. struct dmz_target *dmz = container_of(work, struct dmz_target, flush_work.work);
  432. struct bio *bio;
  433. int ret;
  434. /* Flush dirty metadata blocks */
  435. ret = dmz_flush_metadata(dmz->metadata);
  436. if (ret)
  437. DMDEBUG("(%s): Metadata flush failed, rc=%d",
  438. dmz_metadata_label(dmz->metadata), ret);
  439. /* Process queued flush requests */
  440. while (1) {
  441. spin_lock(&dmz->flush_lock);
  442. bio = bio_list_pop(&dmz->flush_list);
  443. spin_unlock(&dmz->flush_lock);
  444. if (!bio)
  445. break;
  446. dmz_bio_endio(bio, errno_to_blk_status(ret));
  447. }
  448. queue_delayed_work(dmz->flush_wq, &dmz->flush_work, DMZ_FLUSH_PERIOD);
  449. }
  450. /*
  451. * Get a chunk work and start it to process a new BIO.
  452. * If the BIO chunk has no work yet, create one.
  453. */
  454. static int dmz_queue_chunk_work(struct dmz_target *dmz, struct bio *bio)
  455. {
  456. unsigned int chunk = dmz_bio_chunk(dmz->metadata, bio);
  457. struct dm_chunk_work *cw;
  458. int ret = 0;
  459. mutex_lock(&dmz->chunk_lock);
  460. /* Get the BIO chunk work. If one is not active yet, create one */
  461. cw = radix_tree_lookup(&dmz->chunk_rxtree, chunk);
  462. if (cw) {
  463. dmz_get_chunk_work(cw);
  464. } else {
  465. /* Create a new chunk work */
  466. cw = kmalloc(sizeof(struct dm_chunk_work), GFP_NOIO);
  467. if (unlikely(!cw)) {
  468. ret = -ENOMEM;
  469. goto out;
  470. }
  471. INIT_WORK(&cw->work, dmz_chunk_work);
  472. refcount_set(&cw->refcount, 1);
  473. cw->target = dmz;
  474. cw->chunk = chunk;
  475. bio_list_init(&cw->bio_list);
  476. ret = radix_tree_insert(&dmz->chunk_rxtree, chunk, cw);
  477. if (unlikely(ret)) {
  478. kfree(cw);
  479. goto out;
  480. }
  481. }
  482. bio_list_add(&cw->bio_list, bio);
  483. if (queue_work(dmz->chunk_wq, &cw->work))
  484. dmz_get_chunk_work(cw);
  485. out:
  486. mutex_unlock(&dmz->chunk_lock);
  487. return ret;
  488. }
  489. /*
  490. * Check if the backing device is being removed. If it's on the way out,
  491. * start failing I/O. Reclaim and metadata components also call this
  492. * function to cleanly abort operation in the event of such failure.
  493. */
  494. bool dmz_bdev_is_dying(struct dmz_dev *dmz_dev)
  495. {
  496. if (dmz_dev->flags & DMZ_BDEV_DYING)
  497. return true;
  498. if (dmz_dev->flags & DMZ_CHECK_BDEV)
  499. return !dmz_check_bdev(dmz_dev);
  500. if (blk_queue_dying(bdev_get_queue(dmz_dev->bdev))) {
  501. dmz_dev_warn(dmz_dev, "Backing device queue dying");
  502. dmz_dev->flags |= DMZ_BDEV_DYING;
  503. }
  504. return dmz_dev->flags & DMZ_BDEV_DYING;
  505. }
  506. /*
  507. * Check the backing device availability. This detects such events as
  508. * backing device going offline due to errors, media removals, etc.
  509. * This check is less efficient than dmz_bdev_is_dying() and should
  510. * only be performed as a part of error handling.
  511. */
  512. bool dmz_check_bdev(struct dmz_dev *dmz_dev)
  513. {
  514. struct gendisk *disk;
  515. dmz_dev->flags &= ~DMZ_CHECK_BDEV;
  516. if (dmz_bdev_is_dying(dmz_dev))
  517. return false;
  518. disk = dmz_dev->bdev->bd_disk;
  519. if (disk->fops->check_events &&
  520. disk->fops->check_events(disk, 0) & DISK_EVENT_MEDIA_CHANGE) {
  521. dmz_dev_warn(dmz_dev, "Backing device offline");
  522. dmz_dev->flags |= DMZ_BDEV_DYING;
  523. }
  524. return !(dmz_dev->flags & DMZ_BDEV_DYING);
  525. }
  526. /*
  527. * Process a new BIO.
  528. */
  529. static int dmz_map(struct dm_target *ti, struct bio *bio)
  530. {
  531. struct dmz_target *dmz = ti->private;
  532. struct dmz_metadata *zmd = dmz->metadata;
  533. struct dmz_bioctx *bioctx = dm_per_bio_data(bio, sizeof(struct dmz_bioctx));
  534. sector_t sector = bio->bi_iter.bi_sector;
  535. unsigned int nr_sectors = bio_sectors(bio);
  536. sector_t chunk_sector;
  537. int ret;
  538. if (dmz_dev_is_dying(zmd))
  539. return DM_MAPIO_KILL;
  540. DMDEBUG("(%s): BIO op %d sector %llu + %u => chunk %llu, block %llu, %u blocks",
  541. dmz_metadata_label(zmd),
  542. bio_op(bio), (unsigned long long)sector, nr_sectors,
  543. (unsigned long long)dmz_bio_chunk(zmd, bio),
  544. (unsigned long long)dmz_chunk_block(zmd, dmz_bio_block(bio)),
  545. (unsigned int)dmz_bio_blocks(bio));
  546. if (!nr_sectors && bio_op(bio) != REQ_OP_WRITE)
  547. return DM_MAPIO_REMAPPED;
  548. /* The BIO should be block aligned */
  549. if ((nr_sectors & DMZ_BLOCK_SECTORS_MASK) || (sector & DMZ_BLOCK_SECTORS_MASK))
  550. return DM_MAPIO_KILL;
  551. /* Initialize the BIO context */
  552. bioctx->dev = NULL;
  553. bioctx->zone = NULL;
  554. bioctx->bio = bio;
  555. refcount_set(&bioctx->ref, 1);
  556. /* Set the BIO pending in the flush list */
  557. if (!nr_sectors && bio_op(bio) == REQ_OP_WRITE) {
  558. spin_lock(&dmz->flush_lock);
  559. bio_list_add(&dmz->flush_list, bio);
  560. spin_unlock(&dmz->flush_lock);
  561. mod_delayed_work(dmz->flush_wq, &dmz->flush_work, 0);
  562. return DM_MAPIO_SUBMITTED;
  563. }
  564. /* Split zone BIOs to fit entirely into a zone */
  565. chunk_sector = sector & (dmz_zone_nr_sectors(zmd) - 1);
  566. if (chunk_sector + nr_sectors > dmz_zone_nr_sectors(zmd))
  567. dm_accept_partial_bio(bio, dmz_zone_nr_sectors(zmd) - chunk_sector);
  568. /* Now ready to handle this BIO */
  569. ret = dmz_queue_chunk_work(dmz, bio);
  570. if (ret) {
  571. DMDEBUG("(%s): BIO op %d, can't process chunk %llu, err %i",
  572. dmz_metadata_label(zmd),
  573. bio_op(bio), (u64)dmz_bio_chunk(zmd, bio),
  574. ret);
  575. return DM_MAPIO_REQUEUE;
  576. }
  577. return DM_MAPIO_SUBMITTED;
  578. }
  579. /*
  580. * Get zoned device information.
  581. */
  582. static int dmz_get_zoned_device(struct dm_target *ti, char *path,
  583. int idx, int nr_devs)
  584. {
  585. struct dmz_target *dmz = ti->private;
  586. struct dm_dev *ddev;
  587. struct dmz_dev *dev;
  588. int ret;
  589. struct block_device *bdev;
  590. /* Get the target device */
  591. ret = dm_get_device(ti, path, dm_table_get_mode(ti->table), &ddev);
  592. if (ret) {
  593. ti->error = "Get target device failed";
  594. return ret;
  595. }
  596. bdev = ddev->bdev;
  597. if (bdev_zoned_model(bdev) == BLK_ZONED_NONE) {
  598. if (nr_devs == 1) {
  599. ti->error = "Invalid regular device";
  600. goto err;
  601. }
  602. if (idx != 0) {
  603. ti->error = "First device must be a regular device";
  604. goto err;
  605. }
  606. if (dmz->ddev[0]) {
  607. ti->error = "Too many regular devices";
  608. goto err;
  609. }
  610. dev = &dmz->dev[idx];
  611. dev->flags = DMZ_BDEV_REGULAR;
  612. } else {
  613. if (dmz->ddev[idx]) {
  614. ti->error = "Too many zoned devices";
  615. goto err;
  616. }
  617. if (nr_devs > 1 && idx == 0) {
  618. ti->error = "First device must be a regular device";
  619. goto err;
  620. }
  621. dev = &dmz->dev[idx];
  622. }
  623. dev->bdev = bdev;
  624. dev->dev_idx = idx;
  625. dev->capacity = bdev_nr_sectors(bdev);
  626. if (ti->begin) {
  627. ti->error = "Partial mapping is not supported";
  628. goto err;
  629. }
  630. dmz->ddev[idx] = ddev;
  631. return 0;
  632. err:
  633. dm_put_device(ti, ddev);
  634. return -EINVAL;
  635. }
  636. /*
  637. * Cleanup zoned device information.
  638. */
  639. static void dmz_put_zoned_devices(struct dm_target *ti)
  640. {
  641. struct dmz_target *dmz = ti->private;
  642. int i;
  643. for (i = 0; i < dmz->nr_ddevs; i++)
  644. if (dmz->ddev[i])
  645. dm_put_device(ti, dmz->ddev[i]);
  646. kfree(dmz->ddev);
  647. }
  648. static int dmz_fixup_devices(struct dm_target *ti)
  649. {
  650. struct dmz_target *dmz = ti->private;
  651. struct dmz_dev *reg_dev = NULL;
  652. sector_t zone_nr_sectors = 0;
  653. int i;
  654. /*
  655. * When we have more than on devices, the first one must be a
  656. * regular block device and the others zoned block devices.
  657. */
  658. if (dmz->nr_ddevs > 1) {
  659. reg_dev = &dmz->dev[0];
  660. if (!(reg_dev->flags & DMZ_BDEV_REGULAR)) {
  661. ti->error = "Primary disk is not a regular device";
  662. return -EINVAL;
  663. }
  664. for (i = 1; i < dmz->nr_ddevs; i++) {
  665. struct dmz_dev *zoned_dev = &dmz->dev[i];
  666. struct block_device *bdev = zoned_dev->bdev;
  667. if (zoned_dev->flags & DMZ_BDEV_REGULAR) {
  668. ti->error = "Secondary disk is not a zoned device";
  669. return -EINVAL;
  670. }
  671. if (zone_nr_sectors &&
  672. zone_nr_sectors != bdev_zone_sectors(bdev)) {
  673. ti->error = "Zone nr sectors mismatch";
  674. return -EINVAL;
  675. }
  676. zone_nr_sectors = bdev_zone_sectors(bdev);
  677. zoned_dev->zone_nr_sectors = zone_nr_sectors;
  678. zoned_dev->nr_zones = bdev_nr_zones(bdev);
  679. }
  680. } else {
  681. struct dmz_dev *zoned_dev = &dmz->dev[0];
  682. struct block_device *bdev = zoned_dev->bdev;
  683. if (zoned_dev->flags & DMZ_BDEV_REGULAR) {
  684. ti->error = "Disk is not a zoned device";
  685. return -EINVAL;
  686. }
  687. zoned_dev->zone_nr_sectors = bdev_zone_sectors(bdev);
  688. zoned_dev->nr_zones = bdev_nr_zones(bdev);
  689. }
  690. if (reg_dev) {
  691. sector_t zone_offset;
  692. reg_dev->zone_nr_sectors = zone_nr_sectors;
  693. reg_dev->nr_zones =
  694. DIV_ROUND_UP_SECTOR_T(reg_dev->capacity,
  695. reg_dev->zone_nr_sectors);
  696. reg_dev->zone_offset = 0;
  697. zone_offset = reg_dev->nr_zones;
  698. for (i = 1; i < dmz->nr_ddevs; i++) {
  699. dmz->dev[i].zone_offset = zone_offset;
  700. zone_offset += dmz->dev[i].nr_zones;
  701. }
  702. }
  703. return 0;
  704. }
  705. /*
  706. * Setup target.
  707. */
  708. static int dmz_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  709. {
  710. struct dmz_target *dmz;
  711. int ret, i;
  712. /* Check arguments */
  713. if (argc < 1) {
  714. ti->error = "Invalid argument count";
  715. return -EINVAL;
  716. }
  717. /* Allocate and initialize the target descriptor */
  718. dmz = kzalloc(sizeof(struct dmz_target), GFP_KERNEL);
  719. if (!dmz) {
  720. ti->error = "Unable to allocate the zoned target descriptor";
  721. return -ENOMEM;
  722. }
  723. dmz->dev = kcalloc(argc, sizeof(struct dmz_dev), GFP_KERNEL);
  724. if (!dmz->dev) {
  725. ti->error = "Unable to allocate the zoned device descriptors";
  726. kfree(dmz);
  727. return -ENOMEM;
  728. }
  729. dmz->ddev = kcalloc(argc, sizeof(struct dm_dev *), GFP_KERNEL);
  730. if (!dmz->ddev) {
  731. ti->error = "Unable to allocate the dm device descriptors";
  732. ret = -ENOMEM;
  733. goto err;
  734. }
  735. dmz->nr_ddevs = argc;
  736. ti->private = dmz;
  737. /* Get the target zoned block device */
  738. for (i = 0; i < argc; i++) {
  739. ret = dmz_get_zoned_device(ti, argv[i], i, argc);
  740. if (ret)
  741. goto err_dev;
  742. }
  743. ret = dmz_fixup_devices(ti);
  744. if (ret)
  745. goto err_dev;
  746. /* Initialize metadata */
  747. ret = dmz_ctr_metadata(dmz->dev, argc, &dmz->metadata,
  748. dm_table_device_name(ti->table));
  749. if (ret) {
  750. ti->error = "Metadata initialization failed";
  751. goto err_dev;
  752. }
  753. /* Set target (no write same support) */
  754. ti->max_io_len = dmz_zone_nr_sectors(dmz->metadata);
  755. ti->num_flush_bios = 1;
  756. ti->num_discard_bios = 1;
  757. ti->num_write_zeroes_bios = 1;
  758. ti->per_io_data_size = sizeof(struct dmz_bioctx);
  759. ti->flush_supported = true;
  760. ti->discards_supported = true;
  761. /* The exposed capacity is the number of chunks that can be mapped */
  762. ti->len = (sector_t)dmz_nr_chunks(dmz->metadata) <<
  763. dmz_zone_nr_sectors_shift(dmz->metadata);
  764. /* Zone BIO */
  765. ret = bioset_init(&dmz->bio_set, DMZ_MIN_BIOS, 0, 0);
  766. if (ret) {
  767. ti->error = "Create BIO set failed";
  768. goto err_meta;
  769. }
  770. /* Chunk BIO work */
  771. mutex_init(&dmz->chunk_lock);
  772. INIT_RADIX_TREE(&dmz->chunk_rxtree, GFP_NOIO);
  773. dmz->chunk_wq = alloc_workqueue("dmz_cwq_%s",
  774. WQ_MEM_RECLAIM | WQ_UNBOUND, 0,
  775. dmz_metadata_label(dmz->metadata));
  776. if (!dmz->chunk_wq) {
  777. ti->error = "Create chunk workqueue failed";
  778. ret = -ENOMEM;
  779. goto err_bio;
  780. }
  781. /* Flush work */
  782. spin_lock_init(&dmz->flush_lock);
  783. bio_list_init(&dmz->flush_list);
  784. INIT_DELAYED_WORK(&dmz->flush_work, dmz_flush_work);
  785. dmz->flush_wq = alloc_ordered_workqueue("dmz_fwq_%s", WQ_MEM_RECLAIM,
  786. dmz_metadata_label(dmz->metadata));
  787. if (!dmz->flush_wq) {
  788. ti->error = "Create flush workqueue failed";
  789. ret = -ENOMEM;
  790. goto err_cwq;
  791. }
  792. mod_delayed_work(dmz->flush_wq, &dmz->flush_work, DMZ_FLUSH_PERIOD);
  793. /* Initialize reclaim */
  794. for (i = 0; i < dmz->nr_ddevs; i++) {
  795. ret = dmz_ctr_reclaim(dmz->metadata, &dmz->dev[i].reclaim, i);
  796. if (ret) {
  797. ti->error = "Zone reclaim initialization failed";
  798. goto err_fwq;
  799. }
  800. }
  801. DMINFO("(%s): Target device: %llu 512-byte logical sectors (%llu blocks)",
  802. dmz_metadata_label(dmz->metadata),
  803. (unsigned long long)ti->len,
  804. (unsigned long long)dmz_sect2blk(ti->len));
  805. return 0;
  806. err_fwq:
  807. destroy_workqueue(dmz->flush_wq);
  808. err_cwq:
  809. destroy_workqueue(dmz->chunk_wq);
  810. err_bio:
  811. mutex_destroy(&dmz->chunk_lock);
  812. bioset_exit(&dmz->bio_set);
  813. err_meta:
  814. dmz_dtr_metadata(dmz->metadata);
  815. err_dev:
  816. dmz_put_zoned_devices(ti);
  817. err:
  818. kfree(dmz->dev);
  819. kfree(dmz);
  820. return ret;
  821. }
  822. /*
  823. * Cleanup target.
  824. */
  825. static void dmz_dtr(struct dm_target *ti)
  826. {
  827. struct dmz_target *dmz = ti->private;
  828. int i;
  829. destroy_workqueue(dmz->chunk_wq);
  830. for (i = 0; i < dmz->nr_ddevs; i++)
  831. dmz_dtr_reclaim(dmz->dev[i].reclaim);
  832. cancel_delayed_work_sync(&dmz->flush_work);
  833. destroy_workqueue(dmz->flush_wq);
  834. (void) dmz_flush_metadata(dmz->metadata);
  835. dmz_dtr_metadata(dmz->metadata);
  836. bioset_exit(&dmz->bio_set);
  837. dmz_put_zoned_devices(ti);
  838. mutex_destroy(&dmz->chunk_lock);
  839. kfree(dmz->dev);
  840. kfree(dmz);
  841. }
  842. /*
  843. * Setup target request queue limits.
  844. */
  845. static void dmz_io_hints(struct dm_target *ti, struct queue_limits *limits)
  846. {
  847. struct dmz_target *dmz = ti->private;
  848. unsigned int chunk_sectors = dmz_zone_nr_sectors(dmz->metadata);
  849. limits->logical_block_size = DMZ_BLOCK_SIZE;
  850. limits->physical_block_size = DMZ_BLOCK_SIZE;
  851. blk_limits_io_min(limits, DMZ_BLOCK_SIZE);
  852. blk_limits_io_opt(limits, DMZ_BLOCK_SIZE);
  853. limits->discard_alignment = 0;
  854. limits->discard_granularity = DMZ_BLOCK_SIZE;
  855. limits->max_discard_sectors = chunk_sectors;
  856. limits->max_hw_discard_sectors = chunk_sectors;
  857. limits->max_write_zeroes_sectors = chunk_sectors;
  858. /* FS hint to try to align to the device zone size */
  859. limits->chunk_sectors = chunk_sectors;
  860. limits->max_sectors = chunk_sectors;
  861. /* We are exposing a drive-managed zoned block device */
  862. limits->zoned = BLK_ZONED_NONE;
  863. }
  864. /*
  865. * Pass on ioctl to the backend device.
  866. */
  867. static int dmz_prepare_ioctl(struct dm_target *ti, struct block_device **bdev)
  868. {
  869. struct dmz_target *dmz = ti->private;
  870. struct dmz_dev *dev = &dmz->dev[0];
  871. if (!dmz_check_bdev(dev))
  872. return -EIO;
  873. *bdev = dev->bdev;
  874. return 0;
  875. }
  876. /*
  877. * Stop works on suspend.
  878. */
  879. static void dmz_suspend(struct dm_target *ti)
  880. {
  881. struct dmz_target *dmz = ti->private;
  882. int i;
  883. flush_workqueue(dmz->chunk_wq);
  884. for (i = 0; i < dmz->nr_ddevs; i++)
  885. dmz_suspend_reclaim(dmz->dev[i].reclaim);
  886. cancel_delayed_work_sync(&dmz->flush_work);
  887. }
  888. /*
  889. * Restart works on resume or if suspend failed.
  890. */
  891. static void dmz_resume(struct dm_target *ti)
  892. {
  893. struct dmz_target *dmz = ti->private;
  894. int i;
  895. queue_delayed_work(dmz->flush_wq, &dmz->flush_work, DMZ_FLUSH_PERIOD);
  896. for (i = 0; i < dmz->nr_ddevs; i++)
  897. dmz_resume_reclaim(dmz->dev[i].reclaim);
  898. }
  899. static int dmz_iterate_devices(struct dm_target *ti,
  900. iterate_devices_callout_fn fn, void *data)
  901. {
  902. struct dmz_target *dmz = ti->private;
  903. unsigned int zone_nr_sectors = dmz_zone_nr_sectors(dmz->metadata);
  904. sector_t capacity;
  905. int i, r;
  906. for (i = 0; i < dmz->nr_ddevs; i++) {
  907. capacity = dmz->dev[i].capacity & ~(zone_nr_sectors - 1);
  908. r = fn(ti, dmz->ddev[i], 0, capacity, data);
  909. if (r)
  910. break;
  911. }
  912. return r;
  913. }
  914. static void dmz_status(struct dm_target *ti, status_type_t type,
  915. unsigned int status_flags, char *result,
  916. unsigned int maxlen)
  917. {
  918. struct dmz_target *dmz = ti->private;
  919. ssize_t sz = 0;
  920. char buf[BDEVNAME_SIZE];
  921. struct dmz_dev *dev;
  922. int i;
  923. switch (type) {
  924. case STATUSTYPE_INFO:
  925. DMEMIT("%u zones %u/%u cache",
  926. dmz_nr_zones(dmz->metadata),
  927. dmz_nr_unmap_cache_zones(dmz->metadata),
  928. dmz_nr_cache_zones(dmz->metadata));
  929. for (i = 0; i < dmz->nr_ddevs; i++) {
  930. /*
  931. * For a multi-device setup the first device
  932. * contains only cache zones.
  933. */
  934. if ((i == 0) &&
  935. (dmz_nr_cache_zones(dmz->metadata) > 0))
  936. continue;
  937. DMEMIT(" %u/%u random %u/%u sequential",
  938. dmz_nr_unmap_rnd_zones(dmz->metadata, i),
  939. dmz_nr_rnd_zones(dmz->metadata, i),
  940. dmz_nr_unmap_seq_zones(dmz->metadata, i),
  941. dmz_nr_seq_zones(dmz->metadata, i));
  942. }
  943. break;
  944. case STATUSTYPE_TABLE:
  945. dev = &dmz->dev[0];
  946. format_dev_t(buf, dev->bdev->bd_dev);
  947. DMEMIT("%s", buf);
  948. for (i = 1; i < dmz->nr_ddevs; i++) {
  949. dev = &dmz->dev[i];
  950. format_dev_t(buf, dev->bdev->bd_dev);
  951. DMEMIT(" %s", buf);
  952. }
  953. break;
  954. case STATUSTYPE_IMA:
  955. *result = '\0';
  956. break;
  957. }
  958. return;
  959. }
  960. static int dmz_message(struct dm_target *ti, unsigned int argc, char **argv,
  961. char *result, unsigned int maxlen)
  962. {
  963. struct dmz_target *dmz = ti->private;
  964. int r = -EINVAL;
  965. if (!strcasecmp(argv[0], "reclaim")) {
  966. int i;
  967. for (i = 0; i < dmz->nr_ddevs; i++)
  968. dmz_schedule_reclaim(dmz->dev[i].reclaim);
  969. r = 0;
  970. } else
  971. DMERR("unrecognized message %s", argv[0]);
  972. return r;
  973. }
  974. static struct target_type dmz_type = {
  975. .name = "zoned",
  976. .version = {2, 0, 0},
  977. .features = DM_TARGET_SINGLETON | DM_TARGET_MIXED_ZONED_MODEL,
  978. .module = THIS_MODULE,
  979. .ctr = dmz_ctr,
  980. .dtr = dmz_dtr,
  981. .map = dmz_map,
  982. .io_hints = dmz_io_hints,
  983. .prepare_ioctl = dmz_prepare_ioctl,
  984. .postsuspend = dmz_suspend,
  985. .resume = dmz_resume,
  986. .iterate_devices = dmz_iterate_devices,
  987. .status = dmz_status,
  988. .message = dmz_message,
  989. };
  990. static int __init dmz_init(void)
  991. {
  992. return dm_register_target(&dmz_type);
  993. }
  994. static void __exit dmz_exit(void)
  995. {
  996. dm_unregister_target(&dmz_type);
  997. }
  998. module_init(dmz_init);
  999. module_exit(dmz_exit);
  1000. MODULE_DESCRIPTION(DM_NAME " target for zoned block devices");
  1001. MODULE_AUTHOR("Damien Le Moal <[email protected]>");
  1002. MODULE_LICENSE("GPL");