Skip to content

Commit 6fbeb00

Browse files
Mikulas Patockasnitm
authored andcommitted
dm bufio: implement discard
Add functions dm_bufio_issue_discard and dm_bufio_discard_buffers. dm_bufio_issue_discard sends discard request to the underlying device. dm_bufio_discard_buffers frees buffers in the range and then calls dm_bufio_issue_discard. Also, factor out block_to_sector for reuse in dm_bufio_issue_discard. Signed-off-by: Mikulas Patocka <mpatocka@redhat.com> Signed-off-by: Mike Snitzer <snitzer@redhat.com>
1 parent d3c7b35 commit 6fbeb00

2 files changed

Lines changed: 76 additions & 5 deletions

File tree

‎drivers/md/dm-bufio.c‎

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,19 @@ static void use_bio(struct dm_buffer *b, int rw, sector_t sector,
631631
submit_bio(bio);
632632
}
633633

634+
static inline sector_t block_to_sector(struct dm_bufio_client *c, sector_t block)
635+
{
636+
sector_t sector;
637+
638+
if (likely(c->sectors_per_block_bits >= 0))
639+
sector = block << c->sectors_per_block_bits;
640+
else
641+
sector = block * (c->block_size >> SECTOR_SHIFT);
642+
sector += c->start;
643+
644+
return sector;
645+
}
646+
634647
static void submit_io(struct dm_buffer *b, int rw, void (*end_io)(struct dm_buffer *, blk_status_t))
635648
{
636649
unsigned n_sectors;
@@ -639,11 +652,7 @@ static void submit_io(struct dm_buffer *b, int rw, void (*end_io)(struct dm_buff
639652

640653
b->end_io = end_io;
641654

642-
if (likely(b->c->sectors_per_block_bits >= 0))
643-
sector = b->block << b->c->sectors_per_block_bits;
644-
else
645-
sector = b->block * (b->c->block_size >> SECTOR_SHIFT);
646-
sector += b->c->start;
655+
sector = block_to_sector(b->c, b->block);
647656

648657
if (rw != REQ_OP_WRITE) {
649658
n_sectors = b->c->block_size >> SECTOR_SHIFT;
@@ -1325,6 +1334,56 @@ int dm_bufio_issue_flush(struct dm_bufio_client *c)
13251334
}
13261335
EXPORT_SYMBOL_GPL(dm_bufio_issue_flush);
13271336

1337+
/*
1338+
* Use dm-io to send a discard request to flush the device.
1339+
*/
1340+
int dm_bufio_issue_discard(struct dm_bufio_client *c, sector_t block, sector_t count)
1341+
{
1342+
struct dm_io_request io_req = {
1343+
.bi_op = REQ_OP_DISCARD,
1344+
.bi_op_flags = REQ_SYNC,
1345+
.mem.type = DM_IO_KMEM,
1346+
.mem.ptr.addr = NULL,
1347+
.client = c->dm_io,
1348+
};
1349+
struct dm_io_region io_reg = {
1350+
.bdev = c->bdev,
1351+
.sector = block_to_sector(c, block),
1352+
.count = block_to_sector(c, count),
1353+
};
1354+
1355+
BUG_ON(dm_bufio_in_request());
1356+
1357+
return dm_io(&io_req, 1, &io_reg, NULL);
1358+
}
1359+
EXPORT_SYMBOL_GPL(dm_bufio_issue_discard);
1360+
1361+
/*
1362+
* Free the specified range of buffers. If a buffer is held by other process, it
1363+
* is not freed. If a buffer is dirty, it is discarded without writeback.
1364+
* Finally, send the discard request to the device.
1365+
*/
1366+
int dm_bufio_discard_buffers(struct dm_bufio_client *c, sector_t block, sector_t count)
1367+
{
1368+
sector_t i;
1369+
1370+
for (i = block; i < block + count; i++) {
1371+
struct dm_buffer *b;
1372+
dm_bufio_lock(c);
1373+
b = __find(c, i);
1374+
if (b && likely(!b->hold_count)) {
1375+
wait_on_bit_io(&b->state, B_READING, TASK_UNINTERRUPTIBLE);
1376+
wait_on_bit_io(&b->state, B_WRITING, TASK_UNINTERRUPTIBLE);
1377+
__unlink_buffer(b);
1378+
__free_buffer_wake(b);
1379+
}
1380+
dm_bufio_unlock(c);
1381+
}
1382+
1383+
return dm_bufio_issue_discard(c, block, count);
1384+
}
1385+
EXPORT_SYMBOL_GPL(dm_bufio_discard_buffers);
1386+
13281387
/*
13291388
* We first delete any other buffer that may be at that new location.
13301389
*

‎include/linux/dm-bufio.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,18 @@ int dm_bufio_write_dirty_buffers(struct dm_bufio_client *c);
118118
*/
119119
int dm_bufio_issue_flush(struct dm_bufio_client *c);
120120

121+
/*
122+
* Send a discard request to the underlying device.
123+
*/
124+
int dm_bufio_issue_discard(struct dm_bufio_client *c, sector_t block, sector_t count);
125+
126+
/*
127+
* Free the specified range of buffers. If a buffer is held by other process, it
128+
* is not freed. If a buffer is dirty, it is discarded without writeback.
129+
* Finally, send the discard request to the device.
130+
*/
131+
int dm_bufio_discard_buffers(struct dm_bufio_client *c, sector_t block, sector_t count);
132+
121133
/*
122134
* Like dm_bufio_release but also move the buffer to the new
123135
* block. dm_bufio_write_dirty_buffers is needed to commit the new block.

0 commit comments

Comments
 (0)