Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp6568099ybx; Mon, 11 Nov 2019 11:05:34 -0800 (PST) X-Google-Smtp-Source: APXvYqzNXyXTcU/Nu/aikc1bvmfhJ0VGwHxPYvCHvfnuCUuyanjpoPkdAs1pSCp3lK2d1AGG4uSq X-Received: by 2002:a50:fb14:: with SMTP id d20mr27133399edq.190.1573499134823; Mon, 11 Nov 2019 11:05:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573499134; cv=none; d=google.com; s=arc-20160816; b=ZlxQZmC8diG73v4Toa89u/Y0Q0UMTo3IYVmcqZ3W10b5P/VD6fLleI6h3g2OTAJuTj qsx6ncyiBB5wQmVsVL1plmLWMYQKTwyht061ml6Wgs9O8QeSNKU/4ZHZTep0aLmkw4/D AGyH9rmClmeOFzG2uM20i0p+XGbLfuMJsJwGU/bcRivx3rqa0QFveubkBVO7Zbwdd2kc F1NS72uoOdehknP+qKp+x/yuoadaW0j6MAdv/orDwvsavU8g08aIISV4L8wSJ35BY/w6 bnsWbc7yRWIM67a+Yp+yesSqiXCQJcz4E96ET8/jPzSbwqtqcfF0ldSNn+KlH9AKJhXy UoCQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=OWH4ZBjmP1PLAL/11yQZ4HO6sSk47LP135Lp1gszF60=; b=kwd97BzFoowekplAOLwIznD8BuhWXHJYvTuhWszPKIy1GyV+9i++2Mxmm9nJdzxx74 MTkBFKz8bo+1OADz/0KBRTDd/hjqrDIZNLZFmgFLT2W++VoSatOTeqHH+5eWv6/bpODV FLxCd2viAzuBHDHL4KVlRh/o6tRpxGFUX+83R7BAt6Phcb0E9G9QJ04IzfrWyO0dio5k weurRuLCnMigLDiKbsMiVAqidJ7AOUMHqeZf0LRw93ySDPbuvvUiw/yEJZubHgocptNy d1j6wV9Xl2gU0UNpmvdlGAfu/WO18cIYm0K+KBabBGRCzSjnj5AWiCs20o6jlZcaqa0R V96w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Ltf7Pulm; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id si11si9736977ejb.71.2019.11.11.11.04.57; Mon, 11 Nov 2019 11:05:34 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Ltf7Pulm; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729320AbfKKTBk (ORCPT + 99 others); Mon, 11 Nov 2019 14:01:40 -0500 Received: from mail-pf1-f196.google.com ([209.85.210.196]:33696 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729923AbfKKSuu (ORCPT ); Mon, 11 Nov 2019 13:50:50 -0500 Received: by mail-pf1-f196.google.com with SMTP id c184so11302177pfb.0 for ; Mon, 11 Nov 2019 10:50:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=OWH4ZBjmP1PLAL/11yQZ4HO6sSk47LP135Lp1gszF60=; b=Ltf7PulmhFtdy+4C35b3m8AnAibdIVdG9rTbvR7cbDuEbGdhOKRneKymSPq0DAbF2P maHgL/z8DJu2qQV0vvSm3XtBmBJrKI+eMcta7SlWYg/aqvN+Y9/y3NwvkGJeEBSmABe8 rQiwgf1hFz93afRB9aodrG/W+hMaRkxPDy9is= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=OWH4ZBjmP1PLAL/11yQZ4HO6sSk47LP135Lp1gszF60=; b=fVpA6r4aRWRiejtDRuzoagoodSq9tId1HC0gdORFXxV6cEfOX453Gb8J6Ye7kpGC/e neU3SZKoCPmpud6EyfCND9rcWJr3kf4Vr0SiIKeivvPvUNDpgzYBHINYCauW37v+o8KP rXF0p0W4uAazs9OaQ55gk7aeoN4OSKnaBnfIs7Wj7jgf1wP3NPTme2mkoL89S2pXwMqO Y3XxFWUVDA/HqInsd5/+GujPZ6X04fiyXO61rd3YTbgOF9PJ+/luPsrjphvmwHxADbFq LgToDda1TI06/lEdCnanWQoNcTYKA8MfJDsErE06XD7iv44UanXu/Zej8Dhmbfmlblj/ RKlA== X-Gm-Message-State: APjAAAVYjTyEho6iA9pUwTmsF1M9SO7mvOlnr6CpeUYAM1+LU5+49/ik HQDiHgr4M9bcqFeRNlXUEnQDnA== X-Received: by 2002:a62:1c8:: with SMTP id 191mr31885384pfb.152.1573498250075; Mon, 11 Nov 2019 10:50:50 -0800 (PST) Received: from evgreen2.mtv.corp.google.com ([2620:15c:202:201:ffda:7716:9afc:1301]) by smtp.gmail.com with ESMTPSA id v63sm15220971pfb.181.2019.11.11.10.50.49 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 11 Nov 2019 10:50:49 -0800 (PST) From: Evan Green To: Jens Axboe , Martin K Petersen Cc: Gwendal Grignou , Ming Lei , "Darrick J . Wong" , Alexis Savery , Douglas Anderson , Bart Van Assche , Evan Green , Chaitanya Kulkarni , linux-block@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v6 2/2] loop: Better discard support for block devices Date: Mon, 11 Nov 2019 10:50:30 -0800 Message-Id: <20191111185030.215451-3-evgreen@chromium.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191111185030.215451-1-evgreen@chromium.org> References: <20191111185030.215451-1-evgreen@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If the backing device for a loop device is a block device, then mirror the "write zeroes" capabilities of the underlying block device into the loop device. Copy this capability into both max_write_zeroes_sectors and max_discard_sectors of the loop device. The reason for this is that REQ_OP_DISCARD on a loop device translates into blkdev_issue_zeroout(), rather than blkdev_issue_discard(). This presents a consistent interface for loop devices (that discarded data is zeroed), regardless of the backing device type of the loop device. There should be no behavior change for loop devices backed by regular files. While in there, differentiate between REQ_OP_DISCARD and REQ_OP_WRITE_ZEROES, which are different for block devices, but which the loop device had just been lumping together, since they're largely the same for files. This change fixes blktest block/003, and removes an extraneous error print in block/013 when testing on a loop device backed by a block device that does not support discard. Signed-off-by: Evan Green Reviewed-by: Gwendal Grignou Reviewed-by: Chaitanya Kulkarni --- Changes in v6: None Changes in v5: - Don't mirror discard if lo_encrypt_key_size is non-zero (Gwendal) Changes in v4: - Mirror blkdev's write_zeroes into loopdev's discard_sectors. Changes in v3: - Updated commit description Changes in v2: None drivers/block/loop.c | 57 ++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index d749156a3d88..236f6deb0772 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -417,19 +417,14 @@ static int lo_read_transfer(struct loop_device *lo, struct request *rq, return ret; } -static int lo_discard(struct loop_device *lo, struct request *rq, loff_t pos) +static int lo_discard(struct loop_device *lo, struct request *rq, + int mode, loff_t pos) { - /* - * We use punch hole to reclaim the free space used by the - * image a.k.a. discard. However we do not support discard if - * encryption is enabled, because it may give an attacker - * useful information. - */ struct file *file = lo->lo_backing_file; - int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE; + struct request_queue *q = lo->lo_queue; int ret; - if ((!file->f_op->fallocate) || lo->lo_encrypt_key_size) { + if (!blk_queue_discard(q)) { ret = -EOPNOTSUPP; goto out; } @@ -599,8 +594,13 @@ 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: + return lo_discard(lo, rq, + FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, pos); + case REQ_OP_WRITE_ZEROES: - return lo_discard(lo, rq, pos); + return lo_discard(lo, rq, + FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE, pos); + case REQ_OP_WRITE: if (lo->transfer) return lo_write_transfer(lo, rq, pos); @@ -854,6 +854,21 @@ static void loop_config_discard(struct loop_device *lo) struct file *file = lo->lo_backing_file; struct inode *inode = file->f_mapping->host; struct request_queue *q = lo->lo_queue; + struct request_queue *backingq; + + /* + * If the backing device is a block device, mirror its zeroing + * capability. REQ_OP_DISCARD translates to a zero-out even when backed + * by block devices to keep consistent behavior with file-backed loop + * devices. + */ + if (S_ISBLK(inode->i_mode) && !lo->lo_encrypt_key_size) { + backingq = bdev_get_queue(inode->i_bdev); + blk_queue_max_discard_sectors(q, + backingq->limits.max_write_zeroes_sectors); + + blk_queue_max_write_zeroes_sectors(q, + backingq->limits.max_write_zeroes_sectors); /* * We use punch hole to reclaim the free space used by the @@ -861,22 +876,24 @@ static void loop_config_discard(struct loop_device *lo) * encryption is enabled, because it may give an attacker * useful information. */ - if ((!file->f_op->fallocate) || - lo->lo_encrypt_key_size) { + } else if ((!file->f_op->fallocate) || lo->lo_encrypt_key_size) { q->limits.discard_granularity = 0; q->limits.discard_alignment = 0; blk_queue_max_discard_sectors(q, 0); blk_queue_max_write_zeroes_sectors(q, 0); - blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q); - return; - } - q->limits.discard_granularity = inode->i_sb->s_blocksize; - q->limits.discard_alignment = 0; + } else { + q->limits.discard_granularity = inode->i_sb->s_blocksize; + q->limits.discard_alignment = 0; + + blk_queue_max_discard_sectors(q, UINT_MAX >> 9); + blk_queue_max_write_zeroes_sectors(q, UINT_MAX >> 9); + } - blk_queue_max_discard_sectors(q, UINT_MAX >> 9); - blk_queue_max_write_zeroes_sectors(q, UINT_MAX >> 9); - blk_queue_flag_set(QUEUE_FLAG_DISCARD, q); + if (q->limits.max_write_zeroes_sectors) + blk_queue_flag_set(QUEUE_FLAG_DISCARD, q); + else + blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q); } static void loop_unprepare_queue(struct loop_device *lo) -- 2.21.0