Skip to content

Commit b7c3357

Browse files
Nick PigginJens Axboe
authored andcommitted
brd: support discard
Support discard requests in brd by zeroing or deleting the underlying backing pages. This is simply to help with testing and documentation nature of brd code. Signed-off-by: Nick Piggin <npiggin@suse.de> Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
1 parent 0e3c9a2 commit b7c3357

1 file changed

Lines changed: 52 additions & 1 deletion

File tree

‎drivers/block/brd.c‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,28 @@ static struct page *brd_insert_page(struct brd_device *brd, sector_t sector)
133133
return page;
134134
}
135135

136+
static void brd_free_page(struct brd_device *brd, sector_t sector)
137+
{
138+
struct page *page;
139+
pgoff_t idx;
140+
141+
spin_lock(&brd->brd_lock);
142+
idx = sector >> PAGE_SECTORS_SHIFT;
143+
page = radix_tree_delete(&brd->brd_pages, idx);
144+
spin_unlock(&brd->brd_lock);
145+
if (page)
146+
__free_page(page);
147+
}
148+
149+
static void brd_zero_page(struct brd_device *brd, sector_t sector)
150+
{
151+
struct page *page;
152+
153+
page = brd_lookup_page(brd, sector);
154+
if (page)
155+
clear_highpage(page);
156+
}
157+
136158
/*
137159
* Free all backing store pages and radix tree. This must only be called when
138160
* there are no other users of the device.
@@ -189,6 +211,24 @@ static int copy_to_brd_setup(struct brd_device *brd, sector_t sector, size_t n)
189211
return 0;
190212
}
191213

214+
static void discard_from_brd(struct brd_device *brd,
215+
sector_t sector, size_t n)
216+
{
217+
while (n >= PAGE_SIZE) {
218+
/*
219+
* Don't want to actually discard pages here because
220+
* re-allocating the pages can result in writeback
221+
* deadlocks under heavy load.
222+
*/
223+
if (0)
224+
brd_free_page(brd, sector);
225+
else
226+
brd_zero_page(brd, sector);
227+
sector += PAGE_SIZE >> SECTOR_SHIFT;
228+
n -= PAGE_SIZE;
229+
}
230+
}
231+
192232
/*
193233
* Copy n bytes from src to the brd starting at sector. Does not sleep.
194234
*/
@@ -300,6 +340,12 @@ static int brd_make_request(struct request_queue *q, struct bio *bio)
300340
get_capacity(bdev->bd_disk))
301341
goto out;
302342

343+
if (unlikely(bio_rw_flagged(bio, BIO_RW_DISCARD))) {
344+
err = 0;
345+
discard_from_brd(brd, sector, bio->bi_size);
346+
goto out;
347+
}
348+
303349
rw = bio_rw(bio);
304350
if (rw == READA)
305351
rw = READ;
@@ -320,7 +366,7 @@ static int brd_make_request(struct request_queue *q, struct bio *bio)
320366
}
321367

322368
#ifdef CONFIG_BLK_DEV_XIP
323-
static int brd_direct_access (struct block_device *bdev, sector_t sector,
369+
static int brd_direct_access(struct block_device *bdev, sector_t sector,
324370
void **kaddr, unsigned long *pfn)
325371
{
326372
struct brd_device *brd = bdev->bd_disk->private_data;
@@ -437,6 +483,11 @@ static struct brd_device *brd_alloc(int i)
437483
blk_queue_max_hw_sectors(brd->brd_queue, 1024);
438484
blk_queue_bounce_limit(brd->brd_queue, BLK_BOUNCE_ANY);
439485

486+
brd->brd_queue->limits.discard_granularity = PAGE_SIZE;
487+
brd->brd_queue->limits.max_discard_sectors = UINT_MAX;
488+
brd->brd_queue->limits.discard_zeroes_data = 1;
489+
queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, brd->brd_queue);
490+
440491
disk = brd->brd_disk = alloc_disk(1 << part_shift);
441492
if (!disk)
442493
goto out_free_queue;

0 commit comments

Comments
 (0)