Skip to content

Commit bf86bcd

Browse files
Christoph Hellwigaxboe
Christoph Hellwig
authored andcommitted
blk-lib: check for kill signal in ioctl BLKZEROOUT
Zeroout can access a significant capacity and take longer than the user expected. A user may change their mind about wanting to run that command and attempt to kill the process and do something else with their device. But since the task is uninterruptable, they have to wait for it to finish, which could be many hours. Add a new BLKDEV_ZERO_KILLABLE flag for blkdev_issue_zeroout that checks for a fatal signal at each iteration so the user doesn't have to wait for their regretted operation to complete naturally. Heavily based on an earlier patch from Keith Busch. Signed-off-by: Christoph Hellwig <[email protected]> Reviewed-by: Martin K. Petersen <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 39722a2 commit bf86bcd

File tree

3 files changed

+45
-24
lines changed

3 files changed

+45
-24
lines changed

block/blk-lib.c

+43-23
Original file line numberDiff line numberDiff line change
@@ -115,24 +115,27 @@ static void __blkdev_issue_write_zeroes(struct block_device *bdev,
115115
sector_t sector, sector_t nr_sects, gfp_t gfp_mask,
116116
struct bio **biop, unsigned flags)
117117
{
118-
struct bio *bio = *biop;
119-
120118
while (nr_sects) {
121119
unsigned int len = min_t(sector_t, nr_sects,
122120
bio_write_zeroes_limit(bdev));
121+
struct bio *bio;
122+
123+
if ((flags & BLKDEV_ZERO_KILLABLE) &&
124+
fatal_signal_pending(current))
125+
break;
123126

124-
bio = blk_next_bio(bio, bdev, 0, REQ_OP_WRITE_ZEROES, gfp_mask);
127+
bio = bio_alloc(bdev, 0, REQ_OP_WRITE_ZEROES, gfp_mask);
125128
bio->bi_iter.bi_sector = sector;
126129
if (flags & BLKDEV_ZERO_NOUNMAP)
127130
bio->bi_opf |= REQ_NOUNMAP;
128131

129132
bio->bi_iter.bi_size = len << SECTOR_SHIFT;
133+
*biop = bio_chain_and_submit(*biop, bio);
134+
130135
nr_sects -= len;
131136
sector += len;
132137
cond_resched();
133138
}
134-
135-
*biop = bio;
136139
}
137140

138141
static int blkdev_issue_write_zeroes(struct block_device *bdev, sector_t sector,
@@ -145,6 +148,12 @@ static int blkdev_issue_write_zeroes(struct block_device *bdev, sector_t sector,
145148
blk_start_plug(&plug);
146149
__blkdev_issue_write_zeroes(bdev, sector, nr_sects, gfp, &bio, flags);
147150
if (bio) {
151+
if ((flags & BLKDEV_ZERO_KILLABLE) &&
152+
fatal_signal_pending(current)) {
153+
bio_await_chain(bio);
154+
blk_finish_plug(&plug);
155+
return -EINTR;
156+
}
148157
ret = submit_bio_wait(bio);
149158
bio_put(bio);
150159
}
@@ -176,29 +185,34 @@ static unsigned int __blkdev_sectors_to_bio_pages(sector_t nr_sects)
176185

177186
static void __blkdev_issue_zero_pages(struct block_device *bdev,
178187
sector_t sector, sector_t nr_sects, gfp_t gfp_mask,
179-
struct bio **biop)
188+
struct bio **biop, unsigned int flags)
180189
{
181-
struct bio *bio = *biop;
182-
int bi_size = 0;
183-
unsigned int sz;
190+
while (nr_sects) {
191+
unsigned int nr_vecs = __blkdev_sectors_to_bio_pages(nr_sects);
192+
struct bio *bio;
184193

185-
while (nr_sects != 0) {
186-
bio = blk_next_bio(bio, bdev, __blkdev_sectors_to_bio_pages(nr_sects),
187-
REQ_OP_WRITE, gfp_mask);
194+
bio = bio_alloc(bdev, nr_vecs, REQ_OP_WRITE, gfp_mask);
188195
bio->bi_iter.bi_sector = sector;
189196

190-
while (nr_sects != 0) {
191-
sz = min((sector_t) PAGE_SIZE, nr_sects << 9);
192-
bi_size = bio_add_page(bio, ZERO_PAGE(0), sz, 0);
193-
nr_sects -= bi_size >> 9;
194-
sector += bi_size >> 9;
195-
if (bi_size < sz)
197+
if ((flags & BLKDEV_ZERO_KILLABLE) &&
198+
fatal_signal_pending(current))
199+
break;
200+
201+
do {
202+
unsigned int len, added;
203+
204+
len = min_t(sector_t,
205+
PAGE_SIZE, nr_sects << SECTOR_SHIFT);
206+
added = bio_add_page(bio, ZERO_PAGE(0), len, 0);
207+
if (added < len)
196208
break;
197-
}
209+
nr_sects -= added >> SECTOR_SHIFT;
210+
sector += added >> SECTOR_SHIFT;
211+
} while (nr_sects);
212+
213+
*biop = bio_chain_and_submit(*biop, bio);
198214
cond_resched();
199215
}
200-
201-
*biop = bio;
202216
}
203217

204218
static int blkdev_issue_zero_pages(struct block_device *bdev, sector_t sector,
@@ -212,8 +226,14 @@ static int blkdev_issue_zero_pages(struct block_device *bdev, sector_t sector,
212226
return -EOPNOTSUPP;
213227

214228
blk_start_plug(&plug);
215-
__blkdev_issue_zero_pages(bdev, sector, nr_sects, gfp, &bio);
229+
__blkdev_issue_zero_pages(bdev, sector, nr_sects, gfp, &bio, flags);
216230
if (bio) {
231+
if ((flags & BLKDEV_ZERO_KILLABLE) &&
232+
fatal_signal_pending(current)) {
233+
bio_await_chain(bio);
234+
blk_finish_plug(&plug);
235+
return -EINTR;
236+
}
217237
ret = submit_bio_wait(bio);
218238
bio_put(bio);
219239
}
@@ -255,7 +275,7 @@ int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
255275
if (flags & BLKDEV_ZERO_NOFALLBACK)
256276
return -EOPNOTSUPP;
257277
__blkdev_issue_zero_pages(bdev, sector, nr_sects, gfp_mask,
258-
biop);
278+
biop, flags);
259279
}
260280
return 0;
261281
}

block/ioctl.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ static int blk_ioctl_zeroout(struct block_device *bdev, blk_mode_t mode,
224224
goto fail;
225225

226226
err = blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL,
227-
BLKDEV_ZERO_NOUNMAP);
227+
BLKDEV_ZERO_NOUNMAP | BLKDEV_ZERO_KILLABLE);
228228

229229
fail:
230230
filemap_invalidate_unlock(bdev->bd_mapping);

include/linux/blkdev.h

+1
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,7 @@ int blkdev_issue_secure_erase(struct block_device *bdev, sector_t sector,
10951095

10961096
#define BLKDEV_ZERO_NOUNMAP (1 << 0) /* do not free blocks */
10971097
#define BLKDEV_ZERO_NOFALLBACK (1 << 1) /* don't write explicit zeroes */
1098+
#define BLKDEV_ZERO_KILLABLE (1 << 2) /* interruptible by fatal signals */
10981099

10991100
extern int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
11001101
sector_t nr_sects, gfp_t gfp_mask, struct bio **biop,

0 commit comments

Comments
 (0)