Lines Matching +full:cache +full:- +full:op +full:- +full:block +full:- +full:size

1 // SPDX-License-Identifier: GPL-2.0-only
7 * Device-mapper target to emulate smaller logical block
8 * size on backing devices exposing (natively) larger ones.
16 #include <linux/dm-bufio.h>
22 /* Emulated block size context. */
24 struct dm_dev *dev; /* Underlying device to emulate block size on. */
25 struct dm_bufio_client *bufio; /* Use dm-bufio for read and read-modify-write processing. */
31 unsigned int e_bs; /* Emulated block size in sectors exposed to upper layer. */
32 …unsigned int u_bs; /* Underlying block size in sectors retrieved from/set on lower layer device. …
33 unsigned char block_shift; /* bitshift sectors -> blocks used in dm-bufio API. */
34 bool u_bs_set:1; /* Flag to indicate underlying block size is set on table line. */
39 return sector >> ec->block_shift; in __sector_to_block()
44 return sector & (bs - 1); in __block_mod()
50 sector_t end_sector = __block_mod(bio->bi_iter.bi_sector, ec->u_bs) + bio_sectors(bio); in __nr_blocks()
52 return __sector_to_block(ec, end_sector) + (__block_mod(end_sector, ec->u_bs) ? 1 : 0); in __nr_blocks()
65 static int __ebs_rw_bvec(struct ebs_c *ec, enum req_op op, struct bio_vec *bv, in __ebs_rw_bvec() argument
71 unsigned int bv_len = bv->bv_len; in __ebs_rw_bvec()
72 unsigned int buf_off = to_bytes(__block_mod(iter->bi_sector, ec->u_bs)); in __ebs_rw_bvec()
73 sector_t block = __sector_to_block(ec, iter->bi_sector); in __ebs_rw_bvec() local
76 if (unlikely(!bv->bv_page || !bv_len)) in __ebs_rw_bvec()
77 return -EIO; in __ebs_rw_bvec()
81 /* Handle overlapping page <-> blocks */ in __ebs_rw_bvec()
83 cur_len = min(dm_bufio_get_block_size(ec->bufio) - buf_off, bv_len); in __ebs_rw_bvec()
85 /* Avoid reading for writes in case bio vector's page overwrites block completely. */ in __ebs_rw_bvec()
86 if (op == REQ_OP_READ || buf_off || bv_len < dm_bufio_get_block_size(ec->bufio)) in __ebs_rw_bvec()
87 ba = dm_bufio_read(ec->bufio, block, &b); in __ebs_rw_bvec()
89 ba = dm_bufio_new(ec->bufio, block, &b); in __ebs_rw_bvec()
100 if (op == REQ_OP_READ) { in __ebs_rw_bvec()
102 flush_dcache_page(bv->bv_page); in __ebs_rw_bvec()
104 flush_dcache_page(bv->bv_page); in __ebs_rw_bvec()
113 bv_len -= cur_len; in __ebs_rw_bvec()
115 block++; in __ebs_rw_bvec()
122 static int __ebs_rw_bio(struct ebs_c *ec, enum req_op op, struct bio *bio) in __ebs_rw_bio() argument
129 rr = __ebs_rw_bvec(ec, op, &bv, &iter); in __ebs_rw_bio()
145 sector_t block, blocks, sector = bio->bi_iter.bi_sector; in __ebs_discard_bio() local
147 block = __sector_to_block(ec, sector); in __ebs_discard_bio()
151 * Partial first underlying block (__nr_blocks() may have in __ebs_discard_bio()
152 * resulted in one block). in __ebs_discard_bio()
154 if (__block_mod(sector, ec->u_bs)) { in __ebs_discard_bio()
155 block++; in __ebs_discard_bio()
156 blocks--; in __ebs_discard_bio()
159 /* Partial last underlying block if any. */ in __ebs_discard_bio()
160 if (blocks && __block_mod(bio_end_sector(bio), ec->u_bs)) in __ebs_discard_bio()
161 blocks--; in __ebs_discard_bio()
163 return blocks ? dm_bufio_issue_discard(ec->bufio, block, blocks) : 0; in __ebs_discard_bio()
166 /* Release blocks them from the bufio cache. */
169 sector_t blocks, sector = bio->bi_iter.bi_sector; in __ebs_forget_bio()
173 dm_bufio_forget_buffers(ec->bufio, __sector_to_block(ec, sector), blocks); in __ebs_forget_bio()
188 spin_lock_irq(&ec->lock); in __ebs_process_bios()
189 bios = ec->bios_in; in __ebs_process_bios()
190 bio_list_init(&ec->bios_in); in __ebs_process_bios()
191 spin_unlock_irq(&ec->lock); in __ebs_process_bios()
193 /* Prefetch all read and any mis-aligned write buffers */ in __ebs_process_bios()
195 block1 = __sector_to_block(ec, bio->bi_iter.bi_sector); in __ebs_process_bios()
197 dm_bufio_prefetch(ec->bufio, block1, __nr_blocks(ec, bio)); in __ebs_process_bios()
198 else if (bio_op(bio) == REQ_OP_WRITE && !(bio->bi_opf & REQ_PREFLUSH)) { in __ebs_process_bios()
200 if (__block_mod(bio->bi_iter.bi_sector, ec->u_bs)) in __ebs_process_bios()
201 dm_bufio_prefetch(ec->bufio, block1, 1); in __ebs_process_bios()
202 if (__block_mod(bio_end_sector(bio), ec->u_bs) && block2 != block1) in __ebs_process_bios()
203 dm_bufio_prefetch(ec->bufio, block2, 1); in __ebs_process_bios()
208 r = -EIO; in __ebs_process_bios()
220 bio->bi_status = errno_to_blk_status(r); in __ebs_process_bios()
227 r = write ? dm_bufio_write_dirty_buffers(ec->bufio) : 0; in __ebs_process_bios()
239 * Construct an emulated block size mapping: <dev_path> <offset> <ebs> [<ubs>]
243 * <ebs>: emulated block size in units of 512 bytes exposed to the upper layer
244 * [<ubs>]: underlying block size in units of 512 bytes imposed on the lower layer;
245 * optional, if not supplied, retrieve logical block size from underlying device
256 ti->error = "Invalid argument count"; in ebs_ctr()
257 return -EINVAL; in ebs_ctr()
260 ec = ti->private = kzalloc(sizeof(*ec), GFP_KERNEL); in ebs_ctr()
262 ti->error = "Cannot allocate ebs context"; in ebs_ctr()
263 return -ENOMEM; in ebs_ctr()
266 r = -EINVAL; in ebs_ctr()
269 (sector_t)tmp >= ti->len) { in ebs_ctr()
270 ti->error = "Invalid device offset sector"; in ebs_ctr()
273 ec->start = tmp; in ebs_ctr()
278 ti->error = "Invalid emulated block size"; in ebs_ctr()
281 ec->e_bs = tmp1; in ebs_ctr()
285 ti->error = "Invalid underlying block size"; in ebs_ctr()
288 ec->u_bs = tmp1; in ebs_ctr()
289 ec->u_bs_set = true; in ebs_ctr()
291 ec->u_bs_set = false; in ebs_ctr()
293 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &ec->dev); in ebs_ctr()
295 ti->error = "Device lookup failed"; in ebs_ctr()
296 ec->dev = NULL; in ebs_ctr()
300 r = -EINVAL; in ebs_ctr()
301 if (!ec->u_bs_set) { in ebs_ctr()
302 ec->u_bs = to_sector(bdev_logical_block_size(ec->dev->bdev)); in ebs_ctr()
303 if (!__ebs_check_bs(ec->u_bs)) { in ebs_ctr()
304 ti->error = "Invalid retrieved underlying block size"; in ebs_ctr()
309 if (!ec->u_bs_set && ec->e_bs == ec->u_bs) in ebs_ctr()
310 DMINFO("Emulation superfluous: emulated equal to underlying block size"); in ebs_ctr()
312 if (__block_mod(ec->start, ec->u_bs)) { in ebs_ctr()
313 ti->error = "Device offset must be multiple of underlying block size"; in ebs_ctr()
317 ec->bufio = dm_bufio_client_create(ec->dev->bdev, to_bytes(ec->u_bs), 1, in ebs_ctr()
319 if (IS_ERR(ec->bufio)) { in ebs_ctr()
320 ti->error = "Cannot create dm bufio client"; in ebs_ctr()
321 r = PTR_ERR(ec->bufio); in ebs_ctr()
322 ec->bufio = NULL; in ebs_ctr()
326 ec->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM); in ebs_ctr()
327 if (!ec->wq) { in ebs_ctr()
328 ti->error = "Cannot create dm-" DM_MSG_PREFIX " workqueue"; in ebs_ctr()
329 r = -ENOMEM; in ebs_ctr()
333 ec->block_shift = __ffs(ec->u_bs); in ebs_ctr()
334 INIT_WORK(&ec->ws, &__ebs_process_bios); in ebs_ctr()
335 bio_list_init(&ec->bios_in); in ebs_ctr()
336 spin_lock_init(&ec->lock); in ebs_ctr()
338 ti->num_flush_bios = 1; in ebs_ctr()
339 ti->num_discard_bios = 1; in ebs_ctr()
340 ti->num_secure_erase_bios = 0; in ebs_ctr()
341 ti->num_write_zeroes_bios = 0; in ebs_ctr()
350 struct ebs_c *ec = ti->private; in ebs_dtr()
352 if (ec->wq) in ebs_dtr()
353 destroy_workqueue(ec->wq); in ebs_dtr()
354 if (ec->bufio) in ebs_dtr()
355 dm_bufio_client_destroy(ec->bufio); in ebs_dtr()
356 if (ec->dev) in ebs_dtr()
357 dm_put_device(ti, ec->dev); in ebs_dtr()
363 struct ebs_c *ec = ti->private; in ebs_map()
365 bio_set_dev(bio, ec->dev->bdev); in ebs_map()
366 bio->bi_iter.bi_sector = ec->start + dm_target_offset(ti, bio->bi_iter.bi_sector); in ebs_map()
372 * -or- in ebs_map()
373 * emulation with ebs == ubs aiming for tests of dm-bufio overhead. in ebs_map()
375 if (likely(__block_mod(bio->bi_iter.bi_sector, ec->u_bs) || in ebs_map()
376 __block_mod(bio_end_sector(bio), ec->u_bs) || in ebs_map()
377 ec->e_bs == ec->u_bs)) { in ebs_map()
378 spin_lock_irq(&ec->lock); in ebs_map()
379 bio_list_add(&ec->bios_in, bio); in ebs_map()
380 spin_unlock_irq(&ec->lock); in ebs_map()
382 queue_work(ec->wq, &ec->ws); in ebs_map()
396 struct ebs_c *ec = ti->private; in ebs_status()
403 snprintf(result, maxlen, ec->u_bs_set ? "%s %llu %u %u" : "%s %llu %u", in ebs_status()
404 ec->dev->name, (unsigned long long) ec->start, ec->e_bs, ec->u_bs); in ebs_status()
414 struct ebs_c *ec = ti->private; in ebs_prepare_ioctl()
415 struct dm_dev *dev = ec->dev; in ebs_prepare_ioctl()
420 *bdev = dev->bdev; in ebs_prepare_ioctl()
421 return !!(ec->start || ti->len != bdev_nr_sectors(dev->bdev)); in ebs_prepare_ioctl()
426 struct ebs_c *ec = ti->private; in ebs_io_hints()
428 limits->logical_block_size = to_bytes(ec->e_bs); in ebs_io_hints()
429 limits->physical_block_size = to_bytes(ec->u_bs); in ebs_io_hints()
430 limits->alignment_offset = limits->physical_block_size; in ebs_io_hints()
431 limits->io_min = limits->logical_block_size; in ebs_io_hints()
437 struct ebs_c *ec = ti->private; in ebs_iterate_devices()
439 return fn(ti, ec->dev, ec->start, ti->len, data); in ebs_iterate_devices()
457 MODULE_AUTHOR("Heinz Mauelshagen <dm-devel@lists.linux.dev>");
458 MODULE_DESCRIPTION(DM_NAME " emulated block size target");