Message ID | 20191010170239.GC13098@magnolia (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | loop: fix no-unmap write-zeroes request behavior | expand |
On Thu, Oct 10, 2019 at 10:02:39AM -0700, Darrick J. Wong wrote: > From: Darrick J. Wong <darrick.wong@oracle.com> > > Currently, if the loop device receives a WRITE_ZEROES request, it asks > the underlying filesystem to punch out the range. This behavior is > correct if unmapping is allowed. However, a NOUNMAP request means that > the caller forbids us from freeing the storage backing the range, so > punching out the range is incorrect behavior. It doesn't really forbid, as most protocols don't have a way for forbid deallocation. It requests not to. Otherwise this looks fine, although I would have implemented it slightly differently: > case REQ_OP_FLUSH: > return lo_req_flush(lo, rq); > case REQ_OP_DISCARD: > - case REQ_OP_WRITE_ZEROES: > return lo_discard(lo, rq, pos); > + case REQ_OP_WRITE_ZEROES: > + return lo_zeroout(lo, rq, pos); This could just become: case REQ_OP_WRITE_ZEROES: if (rq->cmd_flags & REQ_NOUNMAP)) return lo_zeroout(lo, rq, pos); /*FALLTHRU*/ case REQ_OP_DISCARD: return lo_discard(lo, rq, pos);
diff --git a/drivers/block/loop.c b/drivers/block/loop.c index f6f77eaa7217..0dc981e94bf0 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -441,6 +441,35 @@ static int lo_discard(struct loop_device *lo, struct request *rq, loff_t pos) return ret; } +static int lo_zeroout(struct loop_device *lo, struct request *rq, loff_t pos) +{ + struct file *file = lo->lo_backing_file; + int mode = FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE; + int ret; + + /* If we're allowed to unmap the blocks, ask the fs to punch them. */ + if (!(rq->cmd_flags & REQ_NOUNMAP)) { + ret = lo_discard(lo, rq, pos); + if (!ret) + return 0; + } + + /* + * Otherwise, ask the fs to zero out the blocks, which will result in + * space being allocated to the file. + */ + if (!file->f_op->fallocate) { + ret = -EOPNOTSUPP; + goto out; + } + + ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq)); + if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP)) + ret = -EIO; + out: + return ret; +} + static int lo_req_flush(struct loop_device *lo, struct request *rq) { struct file *file = lo->lo_backing_file; @@ -597,8 +626,9 @@ static int do_req_filebacked(struct loop_device *lo, struct request *rq) case REQ_OP_FLUSH: return lo_req_flush(lo, rq); case REQ_OP_DISCARD: - case REQ_OP_WRITE_ZEROES: return lo_discard(lo, rq, pos); + case REQ_OP_WRITE_ZEROES: + return lo_zeroout(lo, rq, pos); case REQ_OP_WRITE: if (lo->transfer) return lo_write_transfer(lo, rq, pos);