dm-zoned-target.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  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_is_zoned(bdev)) {
  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. limits->io_min = DMZ_BLOCK_SIZE;
  852. limits->io_opt = DMZ_BLOCK_SIZE;
  853. limits->discard_alignment = 0;
  854. limits->discard_granularity = DMZ_BLOCK_SIZE;
  855. limits->max_hw_discard_sectors = chunk_sectors;
  856. limits->max_write_zeroes_sectors = chunk_sectors;
  857. /* FS hint to try to align to the device zone size */
  858. limits->chunk_sectors = chunk_sectors;
  859. limits->max_sectors = chunk_sectors;
  860. /* We are exposing a drive-managed zoned block device */
  861. limits->features &= ~BLK_FEAT_ZONED;
  862. }
  863. /*
  864. * Pass on ioctl to the backend device.
  865. */
  866. static int dmz_prepare_ioctl(struct dm_target *ti, struct block_device **bdev)
  867. {
  868. struct dmz_target *dmz = ti->private;
  869. struct dmz_dev *dev = &dmz->dev[0];
  870. if (!dmz_check_bdev(dev))
  871. return -EIO;
  872. *bdev = dev->bdev;
  873. return 0;
  874. }
  875. /*
  876. * Stop works on suspend.
  877. */
  878. static void dmz_suspend(struct dm_target *ti)
  879. {
  880. struct dmz_target *dmz = ti->private;
  881. int i;
  882. flush_workqueue(dmz->chunk_wq);
  883. for (i = 0; i < dmz->nr_ddevs; i++)
  884. dmz_suspend_reclaim(dmz->dev[i].reclaim);
  885. cancel_delayed_work_sync(&dmz->flush_work);
  886. }
  887. /*
  888. * Restart works on resume or if suspend failed.
  889. */
  890. static void dmz_resume(struct dm_target *ti)
  891. {
  892. struct dmz_target *dmz = ti->private;
  893. int i;
  894. queue_delayed_work(dmz->flush_wq, &dmz->flush_work, DMZ_FLUSH_PERIOD);
  895. for (i = 0; i < dmz->nr_ddevs; i++)
  896. dmz_resume_reclaim(dmz->dev[i].reclaim);
  897. }
  898. static int dmz_iterate_devices(struct dm_target *ti,
  899. iterate_devices_callout_fn fn, void *data)
  900. {
  901. struct dmz_target *dmz = ti->private;
  902. unsigned int zone_nr_sectors = dmz_zone_nr_sectors(dmz->metadata);
  903. sector_t capacity;
  904. int i, r = 0;
  905. for (i = 0; i < dmz->nr_ddevs; i++) {
  906. capacity = dmz->dev[i].capacity & ~(zone_nr_sectors - 1);
  907. r = fn(ti, dmz->ddev[i], 0, capacity, data);
  908. if (r)
  909. break;
  910. }
  911. return r;
  912. }
  913. static void dmz_status(struct dm_target *ti, status_type_t type,
  914. unsigned int status_flags, char *result,
  915. unsigned int maxlen)
  916. {
  917. struct dmz_target *dmz = ti->private;
  918. ssize_t sz = 0;
  919. char buf[BDEVNAME_SIZE];
  920. struct dmz_dev *dev;
  921. int i;
  922. switch (type) {
  923. case STATUSTYPE_INFO:
  924. DMEMIT("%u zones %u/%u cache",
  925. dmz_nr_zones(dmz->metadata),
  926. dmz_nr_unmap_cache_zones(dmz->metadata),
  927. dmz_nr_cache_zones(dmz->metadata));
  928. for (i = 0; i < dmz->nr_ddevs; i++) {
  929. /*
  930. * For a multi-device setup the first device
  931. * contains only cache zones.
  932. */
  933. if ((i == 0) &&
  934. (dmz_nr_cache_zones(dmz->metadata) > 0))
  935. continue;
  936. DMEMIT(" %u/%u random %u/%u sequential",
  937. dmz_nr_unmap_rnd_zones(dmz->metadata, i),
  938. dmz_nr_rnd_zones(dmz->metadata, i),
  939. dmz_nr_unmap_seq_zones(dmz->metadata, i),
  940. dmz_nr_seq_zones(dmz->metadata, i));
  941. }
  942. break;
  943. case STATUSTYPE_TABLE:
  944. dev = &dmz->dev[0];
  945. format_dev_t(buf, dev->bdev->bd_dev);
  946. DMEMIT("%s", buf);
  947. for (i = 1; i < dmz->nr_ddevs; i++) {
  948. dev = &dmz->dev[i];
  949. format_dev_t(buf, dev->bdev->bd_dev);
  950. DMEMIT(" %s", buf);
  951. }
  952. break;
  953. case STATUSTYPE_IMA:
  954. *result = '\0';
  955. break;
  956. }
  957. }
  958. static int dmz_message(struct dm_target *ti, unsigned int argc, char **argv,
  959. char *result, unsigned int maxlen)
  960. {
  961. struct dmz_target *dmz = ti->private;
  962. int r = -EINVAL;
  963. if (!strcasecmp(argv[0], "reclaim")) {
  964. int i;
  965. for (i = 0; i < dmz->nr_ddevs; i++)
  966. dmz_schedule_reclaim(dmz->dev[i].reclaim);
  967. r = 0;
  968. } else
  969. DMERR("unrecognized message %s", argv[0]);
  970. return r;
  971. }
  972. static struct target_type zoned_target = {
  973. .name = "zoned",
  974. .version = {2, 0, 0},
  975. .features = DM_TARGET_SINGLETON | DM_TARGET_MIXED_ZONED_MODEL,
  976. .module = THIS_MODULE,
  977. .ctr = dmz_ctr,
  978. .dtr = dmz_dtr,
  979. .map = dmz_map,
  980. .io_hints = dmz_io_hints,
  981. .prepare_ioctl = dmz_prepare_ioctl,
  982. .postsuspend = dmz_suspend,
  983. .resume = dmz_resume,
  984. .iterate_devices = dmz_iterate_devices,
  985. .status = dmz_status,
  986. .message = dmz_message,
  987. };
  988. module_dm(zoned);
  989. MODULE_DESCRIPTION(DM_NAME " target for zoned block devices");
  990. MODULE_AUTHOR("Damien Le Moal <damien.lemoal@wdc.com>");
  991. MODULE_LICENSE("GPL");