From patchwork Wed May 13 13:38:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martijn Coenen X-Patchwork-Id: 11546155 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id DF17B90 for ; Wed, 13 May 2020 13:39:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 12491223E0 for ; Wed, 13 May 2020 13:39:37 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=android.com header.i=@android.com header.b="bMtNP9ht" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388028AbgEMNjc (ORCPT ); Wed, 13 May 2020 09:39:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56856 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388325AbgEMNi7 (ORCPT ); Wed, 13 May 2020 09:38:59 -0400 Received: from mail-wm1-x343.google.com (mail-wm1-x343.google.com [IPv6:2a00:1450:4864:20::343]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 66BBDC061A0E for ; Wed, 13 May 2020 06:38:59 -0700 (PDT) Received: by mail-wm1-x343.google.com with SMTP id m12so21680217wmc.0 for ; Wed, 13 May 2020 06:38:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=xyRGlbI4Ky3Dk3+EyeqNaJIIfLuLQ5HFlEzVPTO9nr4=; b=bMtNP9htiHa4g6CO0FhlyLgjdmVg2uoH/JS6A+XeeQjNSsWHJ8U8m0wdZGnw1Y+0aL kW8OYvE+eNBeWbbm/5Gab5wkT1yO1e6mIJUToNMcIPXRt7iTR0/vCIetz/nAJn27FLLz jhPPpIRzrjCO7NAduITv9ZIwigQhrw9Qrd7XSq6ZHbMgGPCG8DIfXLK4Mz26e6UyQIsC kFE/QS0MJpOZwOAHP/JmflUOLkHqqpPF2Ucefo9KzZkVgBFzOPS1UsPF3YxZe44yFlfI eGemUuSFNmHex8uVKB8LTn1f6/I4kJhidJrkgjKQ+bDuFwMnHX66Mv8lS/yNh7KxVHHy 5Qiw== 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=xyRGlbI4Ky3Dk3+EyeqNaJIIfLuLQ5HFlEzVPTO9nr4=; b=g2LETOytIrcKylW+6J9D8NgmlCDUXN3OXsfSjtePcA7fXlp0CDpeyKnrTAh8ottgD/ h9L6Kab6upPAkm/JZC3dy/KfyFFitrHDJ/0mW0+OOKwQIy7msK1q55GYEZuYYgCqS/GO 8TunBVtNan4lS0KPVygf8/GiarQJ1lCZzRWY3nhclNDjAJqjsog/ae2vE8SdzAVfQN6y B9FURi9qS2PaxPs85rbkGhlzIx3pVbjovmU5MgOTiJ2/ncL08r+xZjtARkUMz76iPU1P YDkF9ck+j4pqNZBBr1baitOkvRn2Dqk8QlgEGUN6zphMi/Y0ZpXTtEwrn+gave08gm8E KLPg== X-Gm-Message-State: AGi0PuYB4zFP0ht83srE6THaDqvtt3CBpmhE2OExWh+e//hCQQqdGi1r Nj7Rvbo4bl0S7KC7dJhpYx9TqA== X-Google-Smtp-Source: APiQypIjxnkrTUJVBpGMV8YOgo+iI7d5niRSc7PWL+lVfFV/KjhGlb+A6PRiT5dOGTNyca62+5s28w== X-Received: by 2002:a1c:3581:: with SMTP id c123mr32473872wma.150.1589377130947; Wed, 13 May 2020 06:38:50 -0700 (PDT) Received: from maco2.ams.corp.google.com (a83-162-234-235.adsl.xs4all.nl. [83.162.234.235]) by smtp.gmail.com with ESMTPSA id m6sm26202653wrq.5.2020.05.13.06.38.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 May 2020 06:38:50 -0700 (PDT) From: Martijn Coenen To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com Cc: narayan@google.com, zezeozue@google.com, maco@google.com, kernel-team@android.com, bvanassche@acm.org, Chaitanya.Kulkarni@wdc.com, jaegeuk@kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Martijn Coenen , Bob Liu Subject: [PATCH v5 01/11] loop: Call loop_config_discard() only after new config is applied Date: Wed, 13 May 2020 15:38:35 +0200 Message-Id: <20200513133845.244903-2-maco@android.com> X-Mailer: git-send-email 2.26.2.645.ge9eca65c58-goog In-Reply-To: <20200513133845.244903-1-maco@android.com> References: <20200513133845.244903-1-maco@android.com> MIME-Version: 1.0 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org loop_set_status() calls loop_config_discard() to configure discard for the loop device; however, the discard configuration depends on whether the loop device uses encryption, and when we call it the encryption configuration has not been updated yet. Move the call down so we apply the correct discard configuration based on the new configuration. Reviewed-by: Christoph Hellwig Reviewed-by: Bob Liu Reviewed-by: Bart Van Assche Signed-off-by: Martijn Coenen --- drivers/block/loop.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index da693e6a834e..f1754262fc94 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -1334,8 +1334,6 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) } } - loop_config_discard(lo); - memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE); lo->lo_file_name[LO_NAME_SIZE-1] = 0; @@ -1359,6 +1357,8 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) lo->lo_key_owner = uid; } + loop_config_discard(lo); + /* update dio if lo_offset or transfer is changed */ __loop_update_dio(lo, lo->use_dio); From patchwork Wed May 13 13:38:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martijn Coenen X-Patchwork-Id: 11546163 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2040E59D for ; Wed, 13 May 2020 13:39:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 431D7223E0 for ; Wed, 13 May 2020 13:39:54 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=android.com header.i=@android.com header.b="Kg9qJJ1o" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388192AbgEMNiz (ORCPT ); Wed, 13 May 2020 09:38:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56794 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388183AbgEMNiy (ORCPT ); Wed, 13 May 2020 09:38:54 -0400 Received: from mail-wm1-x342.google.com (mail-wm1-x342.google.com [IPv6:2a00:1450:4864:20::342]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5C684C061A0C for ; Wed, 13 May 2020 06:38:53 -0700 (PDT) Received: by mail-wm1-x342.google.com with SMTP id d207so8962471wmd.0 for ; Wed, 13 May 2020 06:38:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=E0D6BkdLn12sF+QPBQ9DDFZok3hDXQNtAmE1mTHFkes=; b=Kg9qJJ1o9t8EF/BBzAGeSYZEi/IVzeB/gtMvBV3ykUZcYKhBQJTacySLMWhotnuumL kaa1fEmfVhNVHZToCs0GVKJcFAJduQJBeWBU/PAINkcgzkZT8S/JJGcY/ts+BR9gkdt0 J+Ioo5EnFFigVarmb50yOe1/CFOfStep9MaGkmB30NcEplHVv9abRmrLG9xyYkwp+phD 4HkQID2tLdB3roHkDx4GCPiax4+Jbqk4rotVnmP9kNVcnMqVQrH/3E3meAnsVpLEC0zV HRnuVBnp6u9L+4CEgqTr6FyzQ/LG0oocgDekKw+2cFwClUBjLoLk5sbBsUQ59MFkV+lc HIjA== 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=E0D6BkdLn12sF+QPBQ9DDFZok3hDXQNtAmE1mTHFkes=; b=BjyMIlqTVPCaJ9Qqerw1iN5WOp0Aon5+9aSVZ9AZQW2oTrMckFKgPdGrhrjiQ+mwzS 9q1+p1fie/7hTw98C73d0qu0a2bybw4PDk+/pujnV4cPCC7O3rA4DLBcAr5nHu80jVKw nnUpCXkmQQtQsqlUhAmwLezPen/ixWIHDFzaXOMVDbtmF/N36l775OQPUyuIDWS1mJak 8Sw0mwmE6/ultIEohOITeVsELKgVeY6poNCNRp0B6yhlG2MXLcHYkf5zKTGtBkoQR+5x w3Ay8B6OjXSkjsIRckaqU24AE+wxFiXiEXG9l+QwPUrs6YOMQLBnsrmF1Jh5v6UFPA3Q OIfQ== X-Gm-Message-State: AGi0PuaCo0jZDtJjds3ZOpv2nGRjZuRKJKTsV2P5jEP9T04q7W+YOC5b lKBDKN/13oO6pmKHhi6XuVOkhw== X-Google-Smtp-Source: APiQypL2vNZ1Uo3ORB7H30R0+fCegEqQ56XS8YTx7EIhM/40krnBpL7pemSe51y129kmdwDXRg33MQ== X-Received: by 2002:a7b:c95a:: with SMTP id i26mr29616989wml.117.1589377132142; Wed, 13 May 2020 06:38:52 -0700 (PDT) Received: from maco2.ams.corp.google.com (a83-162-234-235.adsl.xs4all.nl. [83.162.234.235]) by smtp.gmail.com with ESMTPSA id m6sm26202653wrq.5.2020.05.13.06.38.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 May 2020 06:38:51 -0700 (PDT) From: Martijn Coenen To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com Cc: narayan@google.com, zezeozue@google.com, maco@google.com, kernel-team@android.com, bvanassche@acm.org, Chaitanya.Kulkarni@wdc.com, jaegeuk@kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Martijn Coenen Subject: [PATCH v5 02/11] loop: Remove sector_t truncation checks Date: Wed, 13 May 2020 15:38:36 +0200 Message-Id: <20200513133845.244903-3-maco@android.com> X-Mailer: git-send-email 2.26.2.645.ge9eca65c58-goog In-Reply-To: <20200513133845.244903-1-maco@android.com> References: <20200513133845.244903-1-maco@android.com> MIME-Version: 1.0 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org sector_t is now always u64, so we don't need to check for truncation. Signed-off-by: Martijn Coenen Reviewed-by: Christoph Hellwig --- drivers/block/loop.c | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index f1754262fc94..00de7fec0ed5 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -228,24 +228,20 @@ static void __loop_update_dio(struct loop_device *lo, bool dio) blk_mq_unfreeze_queue(lo->lo_queue); } -static int +static void figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit) { loff_t size = get_size(offset, sizelimit, lo->lo_backing_file); - sector_t x = (sector_t)size; struct block_device *bdev = lo->lo_device; - if (unlikely((loff_t)x != size)) - return -EFBIG; if (lo->lo_offset != offset) lo->lo_offset = offset; if (lo->lo_sizelimit != sizelimit) lo->lo_sizelimit = sizelimit; - set_capacity(lo->lo_disk, x); + set_capacity(lo->lo_disk, size); bd_set_size(bdev, (loff_t)get_capacity(bdev->bd_disk) << 9); /* let user-space know about the new size */ kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); - return 0; } static inline int @@ -1003,10 +999,8 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode, !file->f_op->write_iter) lo_flags |= LO_FLAGS_READ_ONLY; - error = -EFBIG; size = get_loop_size(lo, file); - if ((loff_t)(sector_t)size != size) - goto out_unlock; + error = loop_prepare_queue(lo); if (error) goto out_unlock; @@ -1328,10 +1322,7 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) lo->lo_device->bd_inode->i_mapping->nrpages); goto out_unfreeze; } - if (figure_loop_size(lo, info->lo_offset, info->lo_sizelimit)) { - err = -EFBIG; - goto out_unfreeze; - } + figure_loop_size(lo, info->lo_offset, info->lo_sizelimit); } memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); @@ -1534,7 +1525,9 @@ static int loop_set_capacity(struct loop_device *lo) if (unlikely(lo->lo_state != Lo_bound)) return -ENXIO; - return figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit); + figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit); + + return 0; } static int loop_set_dio(struct loop_device *lo, unsigned long arg) From patchwork Wed May 13 13:38:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martijn Coenen X-Patchwork-Id: 11546381 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id DFC3118B3 for ; Wed, 13 May 2020 13:45:10 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3C93A223C7 for ; Wed, 13 May 2020 13:38:56 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=android.com header.i=@android.com header.b="M/Au1mtf" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388198AbgEMNi4 (ORCPT ); Wed, 13 May 2020 09:38:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56802 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388191AbgEMNiz (ORCPT ); Wed, 13 May 2020 09:38:55 -0400 Received: from mail-wr1-x441.google.com (mail-wr1-x441.google.com [IPv6:2a00:1450:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6BD64C061A0C for ; Wed, 13 May 2020 06:38:54 -0700 (PDT) Received: by mail-wr1-x441.google.com with SMTP id l18so20926282wrn.6 for ; Wed, 13 May 2020 06:38:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=N4tnjsx74zfjEiGN4AVN3WivqX+vN/K0hbsTMSGuGKc=; b=M/Au1mtf637fE49LjUWpOy5c/Dc7SVnK/fec49o+IJ+rd3KZ+rs4TLSQAdAlMXz74O tBqjrddV6Xtt+2vaDEwGkJt9tykl6DnXUMJvyzZLgSI54HVq/IZsVoLMdTe1wi+txQCp 8sC2zwp6AI2tiCJwmskQJV2JG9KDROPzy7zxhQviMZNsRrQpKbBpPynqK7f7yhU8cLbY HtlVUKiA8w7bjBdCj+fmCT/niGR3TczchqRuriN/sWjizop6Wo/Rc4uNhKVSDuO5iVcd z2mQ4iBzlH/BQzIsrDky7PdFvmYeem0aZSEHvgpd4WRUO2UxeIfJ4wMQ+UCA6Oln7Kmc 7VjA== 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=N4tnjsx74zfjEiGN4AVN3WivqX+vN/K0hbsTMSGuGKc=; b=NGkP9o+1ZJKHJqT4YTtsIc6wWX+KtdjWswHPC7sPaCwBPo9yiLjBJ786L8pesdHEXf 6XACBYnAaUaaJg7dwNwpBBIQCLCkumrsJdBHjbUyQXZFjiF1emhcCMQR7Bsoi+ZZS82F H9FJQW+ZpVattD1JPk+h8HWA/KmpAQwgd3SBknxCM/39xMYtHOpL6bn96t96lWIcXUkf pAuz6TRLhON0i8PCfbxp2B9ibshqYv07ah/obS94AwHyD1kiU/wqEaN07aHQ9fjonGPW AqKKHqRzzolj0A0pFoVw4Tjx7vvyJminPCPuDCdxDGL4+s+9p/plFgxn20ng7U7xjMga QYbg== X-Gm-Message-State: AGi0PuYGIa2e/jUIbAz+89V4ZddtCsjFwukrYUOqoVk21Yp32ZREUv1C Stv+wKlcUJj17fpQF1qtjz+tuwOBBKOKbQ== X-Google-Smtp-Source: APiQypLF0JO0A7KUhinkJ32QI/HYHE16/WvvsU+c1NE58s5yvy9WXda4oQ/YQBWrumxU5Cuc+/sssg== X-Received: by 2002:adf:9422:: with SMTP id 31mr23964444wrq.10.1589377133240; Wed, 13 May 2020 06:38:53 -0700 (PDT) Received: from maco2.ams.corp.google.com (a83-162-234-235.adsl.xs4all.nl. [83.162.234.235]) by smtp.gmail.com with ESMTPSA id m6sm26202653wrq.5.2020.05.13.06.38.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 May 2020 06:38:52 -0700 (PDT) From: Martijn Coenen To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com Cc: narayan@google.com, zezeozue@google.com, maco@google.com, kernel-team@android.com, bvanassche@acm.org, Chaitanya.Kulkarni@wdc.com, jaegeuk@kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Martijn Coenen Subject: [PATCH v5 03/11] loop: Factor out setting loop device size Date: Wed, 13 May 2020 15:38:37 +0200 Message-Id: <20200513133845.244903-4-maco@android.com> X-Mailer: git-send-email 2.26.2.645.ge9eca65c58-goog In-Reply-To: <20200513133845.244903-1-maco@android.com> References: <20200513133845.244903-1-maco@android.com> MIME-Version: 1.0 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org This code is used repeatedly. Reviewed-by: Christoph Hellwig Signed-off-by: Martijn Coenen --- drivers/block/loop.c | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 00de7fec0ed5..e69ff3c19eff 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -228,20 +228,35 @@ static void __loop_update_dio(struct loop_device *lo, bool dio) blk_mq_unfreeze_queue(lo->lo_queue); } +/** + * loop_set_size() - sets device size and notifies userspace + * @lo: struct loop_device to set the size for + * @size: new size of the loop device + * + * Callers must validate that the size passed into this function fits into + * a sector_t, eg using loop_validate_size() + */ +static void loop_set_size(struct loop_device *lo, loff_t size) +{ + struct block_device *bdev = lo->lo_device; + + set_capacity(lo->lo_disk, size); + bd_set_size(bdev, size << SECTOR_SHIFT); + /* let user-space know about the new size */ + kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); +} + static void figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit) { loff_t size = get_size(offset, sizelimit, lo->lo_backing_file); - struct block_device *bdev = lo->lo_device; if (lo->lo_offset != offset) lo->lo_offset = offset; if (lo->lo_sizelimit != sizelimit) lo->lo_sizelimit = sizelimit; - set_capacity(lo->lo_disk, size); - bd_set_size(bdev, (loff_t)get_capacity(bdev->bd_disk) << 9); - /* let user-space know about the new size */ - kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); + + loop_set_size(lo, size); } static inline int @@ -1034,11 +1049,8 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode, loop_update_rotational(lo); loop_update_dio(lo); - set_capacity(lo->lo_disk, size); - bd_set_size(bdev, size << 9); loop_sysfs_init(lo); - /* let user-space know about the new size */ - kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); + loop_set_size(lo, size); set_blocksize(bdev, S_ISBLK(inode->i_mode) ? block_size(inode->i_bdev) : PAGE_SIZE); From patchwork Wed May 13 13:38:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martijn Coenen X-Patchwork-Id: 11546153 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2AB201391 for ; Wed, 13 May 2020 13:39:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4EA222242E for ; Wed, 13 May 2020 13:39:36 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=android.com header.i=@android.com header.b="TL34dow3" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388325AbgEMNjd (ORCPT ); Wed, 13 May 2020 09:39:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56842 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388314AbgEMNi7 (ORCPT ); Wed, 13 May 2020 09:38:59 -0400 Received: from mail-wr1-x442.google.com (mail-wr1-x442.google.com [IPv6:2a00:1450:4864:20::442]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C1E75C061A0C for ; Wed, 13 May 2020 06:38:58 -0700 (PDT) Received: by mail-wr1-x442.google.com with SMTP id e1so7006711wrt.5 for ; Wed, 13 May 2020 06:38:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=e1sTXqRGguAy+llg7dGLJYj6/VWdX40SKxOhmoNPZP0=; b=TL34dow3AFECXbJ22HTyK9KKsX1QdBLwrxktF/YGJpH66oiAUsx7m2rSbf4mCCs9hF EKiFq6v7JUV8pbTRNRzyQnks+IUVQHjODPEWyzMhtxz9+TuVguf5lZmFyjKiwEHZOiSm tfII4L7jNTinj5cOB6qyKXFZwCAUv86HZgmazcxq5qiBwQTOy8O4AA7A5rZ53w/H3fuT QxFD4l2FAMJHWSUU5Z3C/ujQjAux3oO8X/oM02mUh7nUSTEjdm8xq9bCMZ8g1iXs9cjd 2mQPyA6X7tC7DWS7nDgyEh6Rw/gwO8+aHyjeKa/mWri0IRS2H5XbWhghK874x1RGkCcI 1Cyw== 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=e1sTXqRGguAy+llg7dGLJYj6/VWdX40SKxOhmoNPZP0=; b=DeNjkis7UO1CwISyquT1A/psverxiAUzxh0qLbmzpRKLbMi8T/eTRByyr/DMG+Z+Af Lve5eGh/9cE2fkOcVSyrh5Xz9iVgTHjiJU12UYRLS7wX0ENc1fU0m6Fv5T0wXXFTsTy/ Jg+Wa89IMdttB4GAsZ4a5uBhMwUcQ/WfEIrBJVx/PaT4xe/DzebmU28c9Y8CyLtSrCrW Qxzba0exyFqVnPw6k4ooQqFkieOXXAgv12pEaK85lQta80hS2lroD1KXX8dZSCfuh61T O+EIndGiAasZ3dbBYGBmuyA3ht4tOs+etNz81kbivSFcnak5NsPrSJrEzqG1so31QDLe I/Dw== X-Gm-Message-State: AGi0Pub+RVTefyAi+K0s86cXAZynVLVW1Re1Xrlomt6O/TPBPyyvHWO1 Kh5MZdT+JVeNvi5xEhj4Pau3Zg== X-Google-Smtp-Source: APiQypJ07uWSubrJZfMnOBzOk7xCePBiIOR3e99yUMkYg7UnoXJyFS7B/1RkG5bqQAmhF6CRbEeeKA== X-Received: by 2002:adf:e951:: with SMTP id m17mr25514589wrn.352.1589377134532; Wed, 13 May 2020 06:38:54 -0700 (PDT) Received: from maco2.ams.corp.google.com (a83-162-234-235.adsl.xs4all.nl. [83.162.234.235]) by smtp.gmail.com with ESMTPSA id m6sm26202653wrq.5.2020.05.13.06.38.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 May 2020 06:38:53 -0700 (PDT) From: Martijn Coenen To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com Cc: narayan@google.com, zezeozue@google.com, maco@google.com, kernel-team@android.com, bvanassche@acm.org, Chaitanya.Kulkarni@wdc.com, jaegeuk@kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Martijn Coenen Subject: [PATCH v5 04/11] loop: Switch to set_capacity_revalidate_and_notify() Date: Wed, 13 May 2020 15:38:38 +0200 Message-Id: <20200513133845.244903-5-maco@android.com> X-Mailer: git-send-email 2.26.2.645.ge9eca65c58-goog In-Reply-To: <20200513133845.244903-1-maco@android.com> References: <20200513133845.244903-1-maco@android.com> MIME-Version: 1.0 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org This was recently added to block/genhd.c, and takes care of both updating the capacity and notifying userspace of the new size. Reviewed-by: Christoph Hellwig Signed-off-by: Martijn Coenen --- drivers/block/loop.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index e69ff3c19eff..d9a756f8abd5 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -240,10 +240,9 @@ static void loop_set_size(struct loop_device *lo, loff_t size) { struct block_device *bdev = lo->lo_device; - set_capacity(lo->lo_disk, size); bd_set_size(bdev, size << SECTOR_SHIFT); - /* let user-space know about the new size */ - kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); + + set_capacity_revalidate_and_notify(lo->lo_disk, size, false); } static void From patchwork Wed May 13 13:38:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martijn Coenen X-Patchwork-Id: 11546159 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8EA251391 for ; Wed, 13 May 2020 13:39:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B339C2240C for ; Wed, 13 May 2020 13:39:47 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=android.com header.i=@android.com header.b="dszfHefJ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387990AbgEMNi6 (ORCPT ); Wed, 13 May 2020 09:38:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56818 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388265AbgEMNi5 (ORCPT ); Wed, 13 May 2020 09:38:57 -0400 Received: from mail-wm1-x343.google.com (mail-wm1-x343.google.com [IPv6:2a00:1450:4864:20::343]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D8931C061A0E for ; Wed, 13 May 2020 06:38:56 -0700 (PDT) Received: by mail-wm1-x343.google.com with SMTP id g12so28531040wmh.3 for ; Wed, 13 May 2020 06:38:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=eYHviJVahcwDhxjEt/fmtYPaGxNJX7zxM8/7Ab1/oKM=; b=dszfHefJ/6wPpOkY6yHlMPpo9u0aofe27dyNCu9H6WbQusflK/2g2i5DPVgxFkOE99 3+xMxns/IcbHbuMCigFsUYpwDw3rX3sIp8ehscd/3v5p9G+BjI7C4VBeTK3gYMJk/KXW 3MZ0KCwEl0sTVTbSC8NB+XaxGsXkuXOlBrdOHdPpIAjGSj1zY7wVL4cX/hEVxnB32xRc 8c2r8d9oaMeZhUNRjTK9OSv2OHUim6+JSg+lVAnh5raV5l5cz3AKeJgWDhlb5gVq5gfR Ergx+pbIVnchLIcwK340dlB/WGU1+K0U/KkNSKgRnzjYCPNfvu0mbnBgqI4KXL4R9I2r 95IA== 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=eYHviJVahcwDhxjEt/fmtYPaGxNJX7zxM8/7Ab1/oKM=; b=C/rq9NNZSD061EdsGvuh9pTkG9zY3FKE6pKbwQ5VHtA74OEE+Nh4Grlv0GCOvI2fDn H7mYGc5Sb2XH9l/cdqfZM3JwYhZnI015KPgNx94Zz0g29NXo+COJ0VM8UrMEfKtSaNgw E2capwA18pOuJUu1hc8NfxPLl+FxN6/vLRaWT+R1VL4vjtA0JXA4H0HG2W5zm6rdDk3O L4GqZVp4R6susKAfdxvA+28aXV4FQOi/N6s5Z0verBeVOAxD4cBEJ1Dn3qrtOt/AFLMZ i/6bs740yZZBBZ2sw5q6zkD0ut8X9eYupy1OADTGgHubYCv6SHht6RloEvdfhPhTdOvk gVgA== X-Gm-Message-State: AOAM531XJTFCaxcZHv/kDDO8iw6WnYQogkPD36i9y5O6ESTv2oTNTGvh jQlY7/RndB3pqamqXGiOwdjhCA== X-Google-Smtp-Source: ABdhPJwro6lOehGpNJwWlMkVkHMVfZV3Yl8YDIn16VxTnE16IUWiZ8ilQmJMXdZrKUOXcKp3g5E6+w== X-Received: by 2002:a1c:3187:: with SMTP id x129mr1800080wmx.27.1589377135614; Wed, 13 May 2020 06:38:55 -0700 (PDT) Received: from maco2.ams.corp.google.com (a83-162-234-235.adsl.xs4all.nl. [83.162.234.235]) by smtp.gmail.com with ESMTPSA id m6sm26202653wrq.5.2020.05.13.06.38.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 May 2020 06:38:54 -0700 (PDT) From: Martijn Coenen To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com Cc: narayan@google.com, zezeozue@google.com, maco@google.com, kernel-team@android.com, bvanassche@acm.org, Chaitanya.Kulkarni@wdc.com, jaegeuk@kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Martijn Coenen Subject: [PATCH v5 05/11] loop: Refactor loop_set_status() size calculation Date: Wed, 13 May 2020 15:38:39 +0200 Message-Id: <20200513133845.244903-6-maco@android.com> X-Mailer: git-send-email 2.26.2.645.ge9eca65c58-goog In-Reply-To: <20200513133845.244903-1-maco@android.com> References: <20200513133845.244903-1-maco@android.com> MIME-Version: 1.0 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org figure_loop_size() calculates the loop size based on the passed in parameters, but at the same time it updates the offset and sizelimit parameters in the loop device configuration. That is a somewhat unexpected side effect of a function with this name, and it is only only needed by one of the two callers of this function - loop_set_status(). Move the lo_offset and lo_sizelimit assignment back into loop_set_status(), and use the newly factored out functions to validate and apply the newly calculated size. This allows us to get rid of figure_loop_size() in a follow-up commit. Reviewed-by: Christoph Hellwig Signed-off-by: Martijn Coenen --- drivers/block/loop.c | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index d9a756f8abd5..c134b3439483 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -250,11 +250,6 @@ figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit) { loff_t size = get_size(offset, sizelimit, lo->lo_backing_file); - if (lo->lo_offset != offset) - lo->lo_offset = offset; - if (lo->lo_sizelimit != sizelimit) - lo->lo_sizelimit = sizelimit; - loop_set_size(lo, size); } @@ -1272,6 +1267,7 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) kuid_t uid = current_uid(); struct block_device *bdev; bool partscan = false; + bool size_changed = false; err = mutex_lock_killable(&loop_ctl_mutex); if (err) @@ -1293,6 +1289,7 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) if (lo->lo_offset != info->lo_offset || lo->lo_sizelimit != info->lo_sizelimit) { + size_changed = true; sync_blockdev(lo->lo_device); kill_bdev(lo->lo_device); } @@ -1300,6 +1297,15 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) /* I/O need to be drained during transfer transition */ blk_mq_freeze_queue(lo->lo_queue); + if (size_changed && lo->lo_device->bd_inode->i_mapping->nrpages) { + /* If any pages were dirtied after kill_bdev(), try again */ + err = -EAGAIN; + pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n", + __func__, lo->lo_number, lo->lo_file_name, + lo->lo_device->bd_inode->i_mapping->nrpages); + goto out_unfreeze; + } + err = loop_release_xfer(lo); if (err) goto out_unfreeze; @@ -1323,19 +1329,8 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) if (err) goto out_unfreeze; - if (lo->lo_offset != info->lo_offset || - lo->lo_sizelimit != info->lo_sizelimit) { - /* kill_bdev should have truncated all the pages */ - if (lo->lo_device->bd_inode->i_mapping->nrpages) { - err = -EAGAIN; - pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n", - __func__, lo->lo_number, lo->lo_file_name, - lo->lo_device->bd_inode->i_mapping->nrpages); - goto out_unfreeze; - } - figure_loop_size(lo, info->lo_offset, info->lo_sizelimit); - } - + lo->lo_offset = info->lo_offset; + lo->lo_sizelimit = info->lo_sizelimit; memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE); lo->lo_file_name[LO_NAME_SIZE-1] = 0; @@ -1359,6 +1354,12 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) lo->lo_key_owner = uid; } + if (size_changed) { + loff_t new_size = get_size(lo->lo_offset, lo->lo_sizelimit, + lo->lo_backing_file); + loop_set_size(lo, new_size); + } + loop_config_discard(lo); /* update dio if lo_offset or transfer is changed */ From patchwork Wed May 13 13:38:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martijn Coenen X-Patchwork-Id: 11546161 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3337F90 for ; Wed, 13 May 2020 13:39:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5526D2240B for ; Wed, 13 May 2020 13:39:48 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=android.com header.i=@android.com header.b="eGCVZxVH" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387983AbgEMNjs (ORCPT ); Wed, 13 May 2020 09:39:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56836 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388071AbgEMNi6 (ORCPT ); Wed, 13 May 2020 09:38:58 -0400 Received: from mail-wr1-x443.google.com (mail-wr1-x443.google.com [IPv6:2a00:1450:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D2F4C061A0F for ; Wed, 13 May 2020 06:38:58 -0700 (PDT) Received: by mail-wr1-x443.google.com with SMTP id l18so20926589wrn.6 for ; Wed, 13 May 2020 06:38:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=DgaYzMr4at/dBaiZhwCRdQ4/MPv6wjkUvq/fU8vDFyU=; b=eGCVZxVH41xtk/Hjf+/yYBJ2rL1w6wlC3Q0Ug4CbUk3ChWSrV8fzgQX9ykCvUT+WD9 k4TVZ93AvvFeUwiNHWIkVYVGAymD2Gk39up77TJ2y37bP9aBqIS3aqzRrhVmlB8WF7M0 WDt5iLjYWw76K76yNr9+BBY784ZgOP7+9caXVVhTMgrdZ8fkU8Kc4SaUf2qcJEufNd4V rQECv2nuZtoI3PyLMqtZeRkyHn/P0sAzh3uUx1p2MjSKTFvAxy76bwFE0dIYoFBg5PmV 1RajsSLC34y31X7v/094xPxLGacJnioom8pXaoWEHCGPCdXDpj2tEwylNfKY8m3YkA20 uq7w== 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=DgaYzMr4at/dBaiZhwCRdQ4/MPv6wjkUvq/fU8vDFyU=; b=C/cMOIVqNe5ywMFpUtYjkMDr0LfWoghoFLXt5Ji1tlAXs8niM31aE9WaUw9gxfUotT OsrZ1pDonmfbglD4DfGkaP5Dh4QwJHesbe3u/oq6azCTepmTXBwqj5vbdncc1dK8RzIy 622YCx+QRR9WcgOIy8J1P7jifjtN7OhxD4X/LBKDEYuj5L4o2QUMOzToYrcZNYb87j+D uN0MBIVnlctKiVkHEuPZGCPtqv7Ex77d8OK1UboY3mDQt858CtCW0yIG+pU8dJzoED4G dyP9eUnmPLGmOFEucJgooOgtuLKi0xzvngMghnOUEGiBp5FpKc5Z4FcCM7s0VICR+Flx WmcA== X-Gm-Message-State: AGi0PuZ5ibLFFdCxjgjCskxFPNlE+QWaTgEh2PQnnZqvyeu/ZsYF3vZD EXnV/xQb4J8GpUo586bZVaAdAg== X-Google-Smtp-Source: APiQypLgfx0bZ/W+soTyjb30iFZoKYeCoiNB6MFptekC6Ez/iO7aZR3TZFMjzWE1yIp6pLSqi1qjsQ== X-Received: by 2002:a05:6000:1241:: with SMTP id j1mr30927974wrx.42.1589377136723; Wed, 13 May 2020 06:38:56 -0700 (PDT) Received: from maco2.ams.corp.google.com (a83-162-234-235.adsl.xs4all.nl. [83.162.234.235]) by smtp.gmail.com with ESMTPSA id m6sm26202653wrq.5.2020.05.13.06.38.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 May 2020 06:38:55 -0700 (PDT) From: Martijn Coenen To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com Cc: narayan@google.com, zezeozue@google.com, maco@google.com, kernel-team@android.com, bvanassche@acm.org, Chaitanya.Kulkarni@wdc.com, jaegeuk@kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Martijn Coenen Subject: [PATCH v5 06/11] loop: Remove figure_loop_size() Date: Wed, 13 May 2020 15:38:40 +0200 Message-Id: <20200513133845.244903-7-maco@android.com> X-Mailer: git-send-email 2.26.2.645.ge9eca65c58-goog In-Reply-To: <20200513133845.244903-1-maco@android.com> References: <20200513133845.244903-1-maco@android.com> MIME-Version: 1.0 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org This function was now only used by loop_set_capacity(). Just open code the remaining code in the caller instead. Reviewed-by: Christoph Hellwig Signed-off-by: Martijn Coenen --- drivers/block/loop.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index c134b3439483..e281a9f03d96 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -245,14 +245,6 @@ static void loop_set_size(struct loop_device *lo, loff_t size) set_capacity_revalidate_and_notify(lo->lo_disk, size, false); } -static void -figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit) -{ - loff_t size = get_size(offset, sizelimit, lo->lo_backing_file); - - loop_set_size(lo, size); -} - static inline int lo_do_transfer(struct loop_device *lo, int cmd, struct page *rpage, unsigned roffs, @@ -1534,10 +1526,13 @@ loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) { static int loop_set_capacity(struct loop_device *lo) { + loff_t size; + if (unlikely(lo->lo_state != Lo_bound)) return -ENXIO; - figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit); + size = get_loop_size(lo, lo->lo_backing_file); + loop_set_size(lo, size); return 0; } From patchwork Wed May 13 13:38:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martijn Coenen X-Patchwork-Id: 11546151 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B6B5859D for ; Wed, 13 May 2020 13:39:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D78432240A for ; Wed, 13 May 2020 13:39:35 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=android.com header.i=@android.com header.b="XLDLOeJ1" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388555AbgEMNjd (ORCPT ); Wed, 13 May 2020 09:39:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56848 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388324AbgEMNi7 (ORCPT ); Wed, 13 May 2020 09:38:59 -0400 Received: from mail-wm1-x344.google.com (mail-wm1-x344.google.com [IPv6:2a00:1450:4864:20::344]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0BAEAC05BD0A for ; Wed, 13 May 2020 06:38:59 -0700 (PDT) Received: by mail-wm1-x344.google.com with SMTP id n5so13639137wmd.0 for ; Wed, 13 May 2020 06:38:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=i9VKkgYcutgK58oduvWyhmP92sHEi0TmGwJSY9JiTVM=; b=XLDLOeJ1jWNVKadKhYblmQ1wc4bEzYSUdgqh1AqZiiYrEA/XsSyYwmM2WkUOMKMUah cOEzxOAWJmeSZ0QteHp+uD1rnPmWKGxm/jQH8nw7TW6iuSgi59OX35Vy1D68K/qnSPlE snxwDVOe8FIW4qJuExZJ5y9fipQcCUZK7HAv0dLaT4oUdvnRG0B1VgpM5PndmB7SraQW 6VebFD8iPbRJSp1UzrPOd8SOBIqA/iOxWmuV+Tn714cU8wud9K4UD30bqg7l6gyu4pnK 9WPjUOdyoTAmxBRDrbYAp4XqDhpowcjRdNiwWCfXn0AUThalcp9WZTuZDU5MFmfz+q6/ ebcw== 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=i9VKkgYcutgK58oduvWyhmP92sHEi0TmGwJSY9JiTVM=; b=o9maHJlrAa1HfL3WnvFV+pnWGZE+zYYaHjPNffB2MMX2uNPg2uI1vnLcFxi2JeGuDI xyB0wZX8NVSglOTboPUNLOK0bD6hK2Ka/rILrzzc5TrjzWikZAr/p1mzMNmWP89C4b50 W28rGjtGTuuHitgod93UOZ0NC39xtG54Rv/elrDqZ5C763aDAVYISdEeIO1Koptj1tom nAO0auzElVK5aEXjFD89dxF9Dm+cciRxhl1kwkXRsf0cXB9ltf6yEnTwaZUcqeWTWDwF GDlMZifuCgZfFDf0PvukOoPPjdJaXWyHure2MZb+gkVtyDbquhzQahXBX4YiloHWwpSN +nTA== X-Gm-Message-State: AGi0PubAxvO3fgKanaYt4ggiEc5egucrdPeTuOB965ZPBBl70KcVPMjf vsISUcloVZQTe8MsU6VVLPvqyw== X-Google-Smtp-Source: APiQypLc80O1KIJtrEk8zdvq1XtASt6qABrDzk+XGaNM/J21GEj9gZ53eLHBoJLozC6/kn4sxpfU0A== X-Received: by 2002:a7b:c959:: with SMTP id i25mr23723098wml.84.1589377137700; Wed, 13 May 2020 06:38:57 -0700 (PDT) Received: from maco2.ams.corp.google.com (a83-162-234-235.adsl.xs4all.nl. [83.162.234.235]) by smtp.gmail.com with ESMTPSA id m6sm26202653wrq.5.2020.05.13.06.38.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 May 2020 06:38:57 -0700 (PDT) From: Martijn Coenen To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com Cc: narayan@google.com, zezeozue@google.com, maco@google.com, kernel-team@android.com, bvanassche@acm.org, Chaitanya.Kulkarni@wdc.com, jaegeuk@kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Martijn Coenen Subject: [PATCH v5 07/11] loop: Factor out configuring loop from status Date: Wed, 13 May 2020 15:38:41 +0200 Message-Id: <20200513133845.244903-8-maco@android.com> X-Mailer: git-send-email 2.26.2.645.ge9eca65c58-goog In-Reply-To: <20200513133845.244903-1-maco@android.com> References: <20200513133845.244903-1-maco@android.com> MIME-Version: 1.0 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org Factor out this code into a separate function, so it can be reused by other code more easily. Reviewed-by: Christoph Hellwig Signed-off-by: Martijn Coenen --- drivers/block/loop.c | 117 +++++++++++++++++++++++++------------------ 1 file changed, 67 insertions(+), 50 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index e281a9f03d96..6a4c0ba225ca 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -1251,75 +1251,43 @@ static int loop_clr_fd(struct loop_device *lo) return __loop_clr_fd(lo, false); } +/** + * loop_set_status_from_info - configure device from loop_info + * @lo: struct loop_device to configure + * @info: struct loop_info64 to configure the device with + * + * Configures the loop device parameters according to the passed + * in loop_info64 configuration. + */ static int -loop_set_status(struct loop_device *lo, const struct loop_info64 *info) +loop_set_status_from_info(struct loop_device *lo, + const struct loop_info64 *info) { int err; struct loop_func_table *xfer; kuid_t uid = current_uid(); - struct block_device *bdev; - bool partscan = false; - bool size_changed = false; - - err = mutex_lock_killable(&loop_ctl_mutex); - if (err) - return err; - if (lo->lo_encrypt_key_size && - !uid_eq(lo->lo_key_owner, uid) && - !capable(CAP_SYS_ADMIN)) { - err = -EPERM; - goto out_unlock; - } - if (lo->lo_state != Lo_bound) { - err = -ENXIO; - goto out_unlock; - } - if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) { - err = -EINVAL; - goto out_unlock; - } - - if (lo->lo_offset != info->lo_offset || - lo->lo_sizelimit != info->lo_sizelimit) { - size_changed = true; - sync_blockdev(lo->lo_device); - kill_bdev(lo->lo_device); - } - /* I/O need to be drained during transfer transition */ - blk_mq_freeze_queue(lo->lo_queue); - - if (size_changed && lo->lo_device->bd_inode->i_mapping->nrpages) { - /* If any pages were dirtied after kill_bdev(), try again */ - err = -EAGAIN; - pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n", - __func__, lo->lo_number, lo->lo_file_name, - lo->lo_device->bd_inode->i_mapping->nrpages); - goto out_unfreeze; - } + if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) + return -EINVAL; err = loop_release_xfer(lo); if (err) - goto out_unfreeze; + return err; if (info->lo_encrypt_type) { unsigned int type = info->lo_encrypt_type; - if (type >= MAX_LO_CRYPT) { - err = -EINVAL; - goto out_unfreeze; - } + if (type >= MAX_LO_CRYPT) + return -EINVAL; xfer = xfer_funcs[type]; - if (xfer == NULL) { - err = -EINVAL; - goto out_unfreeze; - } + if (xfer == NULL) + return -EINVAL; } else xfer = NULL; err = loop_init_xfer(lo, xfer, info); if (err) - goto out_unfreeze; + return err; lo->lo_offset = info->lo_offset; lo->lo_sizelimit = info->lo_sizelimit; @@ -1346,6 +1314,55 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) lo->lo_key_owner = uid; } + return 0; +} + +static int +loop_set_status(struct loop_device *lo, const struct loop_info64 *info) +{ + int err; + struct block_device *bdev; + kuid_t uid = current_uid(); + bool partscan = false; + bool size_changed = false; + + err = mutex_lock_killable(&loop_ctl_mutex); + if (err) + return err; + if (lo->lo_encrypt_key_size && + !uid_eq(lo->lo_key_owner, uid) && + !capable(CAP_SYS_ADMIN)) { + err = -EPERM; + goto out_unlock; + } + if (lo->lo_state != Lo_bound) { + err = -ENXIO; + goto out_unlock; + } + + if (lo->lo_offset != info->lo_offset || + lo->lo_sizelimit != info->lo_sizelimit) { + size_changed = true; + sync_blockdev(lo->lo_device); + kill_bdev(lo->lo_device); + } + + /* I/O need to be drained during transfer transition */ + blk_mq_freeze_queue(lo->lo_queue); + + if (size_changed && lo->lo_device->bd_inode->i_mapping->nrpages) { + /* If any pages were dirtied after kill_bdev(), try again */ + err = -EAGAIN; + pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n", + __func__, lo->lo_number, lo->lo_file_name, + lo->lo_device->bd_inode->i_mapping->nrpages); + goto out_unfreeze; + } + + err = loop_set_status_from_info(lo, info); + if (err) + goto out_unfreeze; + if (size_changed) { loff_t new_size = get_size(lo->lo_offset, lo->lo_sizelimit, lo->lo_backing_file); From patchwork Wed May 13 13:38:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martijn Coenen X-Patchwork-Id: 11546149 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7EDFD90 for ; Wed, 13 May 2020 13:39:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A435C223E0 for ; Wed, 13 May 2020 13:39:30 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=android.com header.i=@android.com header.b="MRimYn1B" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387949AbgEMNja (ORCPT ); Wed, 13 May 2020 09:39:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56848 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388350AbgEMNjA (ORCPT ); Wed, 13 May 2020 09:39:00 -0400 Received: from mail-wm1-x341.google.com (mail-wm1-x341.google.com [IPv6:2a00:1450:4864:20::341]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6CE95C05BD0A for ; Wed, 13 May 2020 06:39:00 -0700 (PDT) Received: by mail-wm1-x341.google.com with SMTP id h4so26601370wmb.4 for ; Wed, 13 May 2020 06:39:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=LBTQ6eQQNZClAXm4Mu8rwjnwwY7IgTM1Zu9ooGgm8tc=; b=MRimYn1BzHn9Y/kf1+72ehYmhU8z3RGbI4382aewpxl+wuzBqpQpfoH5bKGMm5RCQk yCYWz3dQxrUMWRnWQcrfaz7JF6t4X3Ym/Xdn9l9ADB0AGXL0px1CEXhNMoR67V7Is6fg P4/ACLUwNjeD/TcCN1Vf9ImkwV2MtmyraGAA6vhlGpxIwJ+AJuu/0xisboiPC3zUn9Ae r4oUmrFknq/WKMah8f14fjaDvd0bZCnyhIcf0FhCNhj681X0njTEdaNiV2E6auXIoZcD adcJyjfpoJ4lI39KcFUkoR0XBnvr7Ff4mLmrfh+HaVpEFVxcsb5XRCasB9WGx/gw1jFv qZmQ== 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=LBTQ6eQQNZClAXm4Mu8rwjnwwY7IgTM1Zu9ooGgm8tc=; b=NbNCjQxnydEc79CLOEWGOf9lgk51WWHhyPJntuaWf6BYFyVSkkJjHrw9JJ/pAp+GH6 yLWjwWcsKDp4irYSWy1sjYiDEusEkZ5ddX2DiNbOAZSU1InWSDoCfUzRQUV8tDGbXq3j 1WEMM4bJZg0gqCIQ/doQ0/E2pWu7sWoiCYWIk+te5CZz7HoMw75uw/bZ8F5qJMeBee7S zKf4QSds6lEB8zw98HSTlPXhM1tZgKTqF2APvSR9SyJQW5hC+9+zIpbXJBR0SCsfvajx /CrzJ0aYA44c3t0QOC6xHR4S3LQJurERM9rByTYrtzkd0zPIBR/1AquOab0HxEeYYaRC il2Q== X-Gm-Message-State: AGi0PuaDlET/kVRk2e4waXlWsCPtYDN5paD0SUlQSoq7g058q3Iu95tn Ezscr7RG35b9B3d+4h2ERSSPPw== X-Google-Smtp-Source: APiQypKDZXiGlqbbIXjnTDfD4v2C9vlzh6r01ctvDN2CH70edPD/tPjXN6HV6xGGJv4pYFtfPIss4g== X-Received: by 2002:a7b:cb53:: with SMTP id v19mr41936758wmj.166.1589377139165; Wed, 13 May 2020 06:38:59 -0700 (PDT) Received: from maco2.ams.corp.google.com (a83-162-234-235.adsl.xs4all.nl. [83.162.234.235]) by smtp.gmail.com with ESMTPSA id m6sm26202653wrq.5.2020.05.13.06.38.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 May 2020 06:38:58 -0700 (PDT) From: Martijn Coenen To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com Cc: narayan@google.com, zezeozue@google.com, maco@google.com, kernel-team@android.com, bvanassche@acm.org, Chaitanya.Kulkarni@wdc.com, jaegeuk@kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Martijn Coenen Subject: [PATCH v5 08/11] loop: Move loop_set_status_from_info() and friends up Date: Wed, 13 May 2020 15:38:42 +0200 Message-Id: <20200513133845.244903-9-maco@android.com> X-Mailer: git-send-email 2.26.2.645.ge9eca65c58-goog In-Reply-To: <20200513133845.244903-1-maco@android.com> References: <20200513133845.244903-1-maco@android.com> MIME-Version: 1.0 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org So we can use it without forward declaration. This is a separate commit to make it easier to verify that this is just a move, without functional modifications. Reviewed-by: Christoph Hellwig Signed-off-by: Martijn Coenen --- drivers/block/loop.c | 206 +++++++++++++++++++++---------------------- 1 file changed, 103 insertions(+), 103 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 6a4c0ba225ca..4dc11d954169 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -949,6 +949,109 @@ static void loop_update_rotational(struct loop_device *lo) blk_queue_flag_clear(QUEUE_FLAG_NONROT, q); } +static int +loop_release_xfer(struct loop_device *lo) +{ + int err = 0; + struct loop_func_table *xfer = lo->lo_encryption; + + if (xfer) { + if (xfer->release) + err = xfer->release(lo); + lo->transfer = NULL; + lo->lo_encryption = NULL; + module_put(xfer->owner); + } + return err; +} + +static int +loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer, + const struct loop_info64 *i) +{ + int err = 0; + + if (xfer) { + struct module *owner = xfer->owner; + + if (!try_module_get(owner)) + return -EINVAL; + if (xfer->init) + err = xfer->init(lo, i); + if (err) + module_put(owner); + else + lo->lo_encryption = xfer; + } + return err; +} + +/** + * loop_set_status_from_info - configure device from loop_info + * @lo: struct loop_device to configure + * @info: struct loop_info64 to configure the device with + * + * Configures the loop device parameters according to the passed + * in loop_info64 configuration. + */ +static int +loop_set_status_from_info(struct loop_device *lo, + const struct loop_info64 *info) +{ + int err; + struct loop_func_table *xfer; + kuid_t uid = current_uid(); + + if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) + return -EINVAL; + + err = loop_release_xfer(lo); + if (err) + return err; + + if (info->lo_encrypt_type) { + unsigned int type = info->lo_encrypt_type; + + if (type >= MAX_LO_CRYPT) + return -EINVAL; + xfer = xfer_funcs[type]; + if (xfer == NULL) + return -EINVAL; + } else + xfer = NULL; + + err = loop_init_xfer(lo, xfer, info); + if (err) + return err; + + lo->lo_offset = info->lo_offset; + lo->lo_sizelimit = info->lo_sizelimit; + memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); + memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE); + lo->lo_file_name[LO_NAME_SIZE-1] = 0; + lo->lo_crypt_name[LO_NAME_SIZE-1] = 0; + + if (!xfer) + xfer = &none_funcs; + lo->transfer = xfer->transfer; + lo->ioctl = xfer->ioctl; + + if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) != + (info->lo_flags & LO_FLAGS_AUTOCLEAR)) + lo->lo_flags ^= LO_FLAGS_AUTOCLEAR; + + lo->lo_encrypt_key_size = info->lo_encrypt_key_size; + lo->lo_init[0] = info->lo_init[0]; + lo->lo_init[1] = info->lo_init[1]; + if (info->lo_encrypt_key_size) { + memcpy(lo->lo_encrypt_key, info->lo_encrypt_key, + info->lo_encrypt_key_size); + lo->lo_key_owner = uid; + } + + return 0; +} + static int loop_set_fd(struct loop_device *lo, fmode_t mode, struct block_device *bdev, unsigned int arg) { @@ -1070,43 +1173,6 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode, return error; } -static int -loop_release_xfer(struct loop_device *lo) -{ - int err = 0; - struct loop_func_table *xfer = lo->lo_encryption; - - if (xfer) { - if (xfer->release) - err = xfer->release(lo); - lo->transfer = NULL; - lo->lo_encryption = NULL; - module_put(xfer->owner); - } - return err; -} - -static int -loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer, - const struct loop_info64 *i) -{ - int err = 0; - - if (xfer) { - struct module *owner = xfer->owner; - - if (!try_module_get(owner)) - return -EINVAL; - if (xfer->init) - err = xfer->init(lo, i); - if (err) - module_put(owner); - else - lo->lo_encryption = xfer; - } - return err; -} - static int __loop_clr_fd(struct loop_device *lo, bool release) { struct file *filp = NULL; @@ -1251,72 +1317,6 @@ static int loop_clr_fd(struct loop_device *lo) return __loop_clr_fd(lo, false); } -/** - * loop_set_status_from_info - configure device from loop_info - * @lo: struct loop_device to configure - * @info: struct loop_info64 to configure the device with - * - * Configures the loop device parameters according to the passed - * in loop_info64 configuration. - */ -static int -loop_set_status_from_info(struct loop_device *lo, - const struct loop_info64 *info) -{ - int err; - struct loop_func_table *xfer; - kuid_t uid = current_uid(); - - if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) - return -EINVAL; - - err = loop_release_xfer(lo); - if (err) - return err; - - if (info->lo_encrypt_type) { - unsigned int type = info->lo_encrypt_type; - - if (type >= MAX_LO_CRYPT) - return -EINVAL; - xfer = xfer_funcs[type]; - if (xfer == NULL) - return -EINVAL; - } else - xfer = NULL; - - err = loop_init_xfer(lo, xfer, info); - if (err) - return err; - - lo->lo_offset = info->lo_offset; - lo->lo_sizelimit = info->lo_sizelimit; - memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); - memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE); - lo->lo_file_name[LO_NAME_SIZE-1] = 0; - lo->lo_crypt_name[LO_NAME_SIZE-1] = 0; - - if (!xfer) - xfer = &none_funcs; - lo->transfer = xfer->transfer; - lo->ioctl = xfer->ioctl; - - if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) != - (info->lo_flags & LO_FLAGS_AUTOCLEAR)) - lo->lo_flags ^= LO_FLAGS_AUTOCLEAR; - - lo->lo_encrypt_key_size = info->lo_encrypt_key_size; - lo->lo_init[0] = info->lo_init[0]; - lo->lo_init[1] = info->lo_init[1]; - if (info->lo_encrypt_key_size) { - memcpy(lo->lo_encrypt_key, info->lo_encrypt_key, - info->lo_encrypt_key_size); - lo->lo_key_owner = uid; - } - - return 0; -} - static int loop_set_status(struct loop_device *lo, const struct loop_info64 *info) { From patchwork Wed May 13 13:38:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martijn Coenen X-Patchwork-Id: 11546147 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6305259D for ; Wed, 13 May 2020 13:39:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8768D223E0 for ; Wed, 13 May 2020 13:39:30 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=android.com header.i=@android.com header.b="vq2w2pJB" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387843AbgEMNj3 (ORCPT ); Wed, 13 May 2020 09:39:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56850 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388359AbgEMNjB (ORCPT ); Wed, 13 May 2020 09:39:01 -0400 Received: from mail-wm1-x341.google.com (mail-wm1-x341.google.com [IPv6:2a00:1450:4864:20::341]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 87C5BC061A0C for ; Wed, 13 May 2020 06:39:01 -0700 (PDT) Received: by mail-wm1-x341.google.com with SMTP id g12so28531399wmh.3 for ; Wed, 13 May 2020 06:39:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=73y2EkDX2Dlo8B8Rg2AO0hZwVG6UoIFuwEH7MmnfvIk=; b=vq2w2pJBraCI2zP4u8/588HjpEAoDfIH0FZKaHxdyRonCuEfO683f7KTY2yLjJGUlM gOXQBdQ0eWCv6MF492fCviR57ZNLYFhTwqgZYPQDNuNoS9WEZIPQ9b2UvGARh2BqvvhJ aRc43kp0XLknxr4aWOdzc6E1LOflni8eDsTaG3f597SMVZMcVHcBBq37hIjU4Qya+Q+h sZrTWs5UHsg6zKBXf32OF5dz+3wDFyGkSnP4RzYHSYY9RtPxxHvc7Kuvh+3eYMlazV24 T/IbHPUaqX7uKFWcbEOrbYkzOsqk4jeBYrTBGdpnO9TAkuSjwFadeByWhgnZJcqcOvxk UKAQ== 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=73y2EkDX2Dlo8B8Rg2AO0hZwVG6UoIFuwEH7MmnfvIk=; b=V5KJFMzaNmT+tr0B2aMOMe9IRtkSIsIG5Ycp9H/AyiOesnHkVOsLRBStntGKKUBOoi gKriqCW4wX9PW8Wj/Wq8lGWOr5yVyUlJog7CxDyCbA4u62o/7qTHsOXWfmvUFs2acfw3 MfxkBTm3p0xoWpS0TG8h6iGAeszXI+L0M6x+pMJri+MwNFu/m5g5X4ypwHQFobit35jL OaTd81jDUfBPMvagNxebvifAV+sjhQdyP9shz0bzyuzcWWl/OIHownjn+1LHxKK+6ozE BVSY+TtBKc4R7q2xDxLaYo/8Yy8HDp5wTnRsQpZZuRvD+x5M2KdE0s4JnEoNssX+MS6m 7ogg== X-Gm-Message-State: AGi0PuYJZRF/h0Oi6Q+mGe4E53DmaZYKvAy/7W+OGaC4qJKyp7Ahrv+J zTMbWUHI6Av3fmYqAVZRw//koQ== X-Google-Smtp-Source: APiQypJKNnonUlim7ZOi25FTphWD9VrUisBBoAjjUOwOam2+35TMRzU9Zm7owMB+f9WKPUwv0LlXnA== X-Received: by 2002:a1c:3581:: with SMTP id c123mr32474479wma.150.1589377140295; Wed, 13 May 2020 06:39:00 -0700 (PDT) Received: from maco2.ams.corp.google.com (a83-162-234-235.adsl.xs4all.nl. [83.162.234.235]) by smtp.gmail.com with ESMTPSA id m6sm26202653wrq.5.2020.05.13.06.38.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 May 2020 06:38:59 -0700 (PDT) From: Martijn Coenen To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com Cc: narayan@google.com, zezeozue@google.com, maco@google.com, kernel-team@android.com, bvanassche@acm.org, Chaitanya.Kulkarni@wdc.com, jaegeuk@kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Martijn Coenen Subject: [PATCH v5 09/11] loop: Rework lo_ioctl() __user argument casting Date: Wed, 13 May 2020 15:38:43 +0200 Message-Id: <20200513133845.244903-10-maco@android.com> X-Mailer: git-send-email 2.26.2.645.ge9eca65c58-goog In-Reply-To: <20200513133845.244903-1-maco@android.com> References: <20200513133845.244903-1-maco@android.com> MIME-Version: 1.0 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org In preparation for a new ioctl that needs to copy_from_user(); makes the code easier to read as well. Reviewed-by: Christoph Hellwig Signed-off-by: Martijn Coenen --- drivers/block/loop.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 4dc11d954169..31f10da4945e 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -1634,6 +1634,7 @@ static int lo_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, unsigned long arg) { struct loop_device *lo = bdev->bd_disk->private_data; + void __user *argp = (void __user *) arg; int err; switch (cmd) { @@ -1646,21 +1647,19 @@ static int lo_ioctl(struct block_device *bdev, fmode_t mode, case LOOP_SET_STATUS: err = -EPERM; if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { - err = loop_set_status_old(lo, - (struct loop_info __user *)arg); + err = loop_set_status_old(lo, argp); } break; case LOOP_GET_STATUS: - return loop_get_status_old(lo, (struct loop_info __user *) arg); + return loop_get_status_old(lo, argp); case LOOP_SET_STATUS64: err = -EPERM; if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { - err = loop_set_status64(lo, - (struct loop_info64 __user *) arg); + err = loop_set_status64(lo, argp); } break; case LOOP_GET_STATUS64: - return loop_get_status64(lo, (struct loop_info64 __user *) arg); + return loop_get_status64(lo, argp); case LOOP_SET_CAPACITY: case LOOP_SET_DIRECT_IO: case LOOP_SET_BLOCK_SIZE: From patchwork Wed May 13 13:38:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martijn Coenen X-Patchwork-Id: 11546141 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A6A9B59D for ; Wed, 13 May 2020 13:39:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CB78E223E0 for ; Wed, 13 May 2020 13:39:20 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=android.com header.i=@android.com header.b="k9uAgJrv" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733282AbgEMNjU (ORCPT ); Wed, 13 May 2020 09:39:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56870 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388463AbgEMNjE (ORCPT ); Wed, 13 May 2020 09:39:04 -0400 Received: from mail-wm1-x341.google.com (mail-wm1-x341.google.com [IPv6:2a00:1450:4864:20::341]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A4FD2C061A0C for ; Wed, 13 May 2020 06:39:03 -0700 (PDT) Received: by mail-wm1-x341.google.com with SMTP id n5so13639432wmd.0 for ; Wed, 13 May 2020 06:39:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=POCEZLhOMyAYL79dmttqIcaXe8olR2uGKpnhJHwT9Bc=; b=k9uAgJrvSLqYqjlr/7FemhreMX5L8RfzI3k3jZMrTHzti+aqwlEj5LuLiXs0/j7C5t 7o9Euo+zXbkXXdrhue3P/LypqUb9wN40SwJf+GO5fE/DX9iW13FGkDYG9gINTFKvquCv KjSkprs4rDMRRZeXbdm3Bz1hBTUrkij/dnfTGI6x2C/Ys3XrIKMlFkUJfOzi8cpWLsgN WesotT8PIdpI1Ad4Sw7uOZ5klj29YLKM+/Aysuc5oJ3kAH909pGWgeBWf/YH/zw4GqKC /crJaiM8nnYOLhEEV6ojaQME9NCUGr32QSTCxBGMOl5CHu47Wx9X41fuSfYlgPG4Am/Z aaEA== 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=POCEZLhOMyAYL79dmttqIcaXe8olR2uGKpnhJHwT9Bc=; b=kIjTU/tYiD7Le0hgVaLbQnh3syNCYI0w852WPQne+7sZ32lAl7H3spNIwKuy6/L2Ov 09qukVNqEyvm0pky/x4HRVGKl7DlOFMmb0xiTCcsnxduM6+wF3o6giR7lJgGY8DTA66y sJnCVtYmz2x8e7KAc3qPEjWoYwLfMfGb48bdVbFq4k0dQ0AdokR+yLboMkgKWVgCOm0S sxCRnt+cxOSReOGrXhC6MlDZ82MOe0wPyEjIRidKkrdUiit4bdUh/4/IQlIQ1HQmvhWK luDGAUG1C4DAUpe7g1FS6P+lCGj4lCBRfGKtv/dodEat0tZ5T4bzV3Xr9km3XN2J64Z5 Mwmg== X-Gm-Message-State: AGi0PuZScnuBUtjTlAyazSTK1hfw08z9ajtCwfR4yRJBYtCHhFOlrskH 6yFVmXsv1UfoemnxOO9EhZ+otg== X-Google-Smtp-Source: APiQypIQ8EMG0eQ2MileWb5d3BNe03CVQDjqnEtCDHpdaRXZv8NJbwfGqtVbjy9y6eDqGqENL63mNw== X-Received: by 2002:a7b:c95a:: with SMTP id i26mr29617586wml.117.1589377141372; Wed, 13 May 2020 06:39:01 -0700 (PDT) Received: from maco2.ams.corp.google.com (a83-162-234-235.adsl.xs4all.nl. [83.162.234.235]) by smtp.gmail.com with ESMTPSA id m6sm26202653wrq.5.2020.05.13.06.39.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 May 2020 06:39:00 -0700 (PDT) From: Martijn Coenen To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com Cc: narayan@google.com, zezeozue@google.com, maco@google.com, kernel-team@android.com, bvanassche@acm.org, Chaitanya.Kulkarni@wdc.com, jaegeuk@kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Martijn Coenen Subject: [PATCH v5 10/11] loop: Clean up LOOP_SET_STATUS lo_flags handling Date: Wed, 13 May 2020 15:38:44 +0200 Message-Id: <20200513133845.244903-11-maco@android.com> X-Mailer: git-send-email 2.26.2.645.ge9eca65c58-goog In-Reply-To: <20200513133845.244903-1-maco@android.com> References: <20200513133845.244903-1-maco@android.com> MIME-Version: 1.0 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org LOOP_SET_STATUS(64) will actually allow some lo_flags to be modified; in particular, LO_FLAGS_AUTOCLEAR can be set and cleared, whereas LO_FLAGS_PARTSCAN can be set to request a partition scan. Make this explicit by updating the UAPI to include the flags that can be set/cleared using this ioctl. The implementation can then blindly take over the passed in flags, and use the previous flags for those flags that can't be set / cleared using LOOP_SET_STATUS. Reviewed-by: Christoph Hellwig Signed-off-by: Martijn Coenen --- drivers/block/loop.c | 19 +++++++++++++------ include/uapi/linux/loop.h | 10 ++++++++-- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 31f10da4945e..13518ba191f5 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -1036,9 +1036,7 @@ loop_set_status_from_info(struct loop_device *lo, lo->transfer = xfer->transfer; lo->ioctl = xfer->ioctl; - if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) != - (info->lo_flags & LO_FLAGS_AUTOCLEAR)) - lo->lo_flags ^= LO_FLAGS_AUTOCLEAR; + lo->lo_flags = info->lo_flags; lo->lo_encrypt_key_size = info->lo_encrypt_key_size; lo->lo_init[0] = info->lo_init[0]; @@ -1323,6 +1321,7 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) int err; struct block_device *bdev; kuid_t uid = current_uid(); + int prev_lo_flags; bool partscan = false; bool size_changed = false; @@ -1359,10 +1358,19 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) goto out_unfreeze; } + prev_lo_flags = lo->lo_flags; + err = loop_set_status_from_info(lo, info); if (err) goto out_unfreeze; + /* Mask out flags that can't be set using LOOP_SET_STATUS. */ + lo->lo_flags &= ~LOOP_SET_STATUS_SETTABLE_FLAGS; + /* For those flags, use the previous values instead */ + lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_SETTABLE_FLAGS; + /* For flags that can't be cleared, use previous values too */ + lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_CLEARABLE_FLAGS; + if (size_changed) { loff_t new_size = get_size(lo->lo_offset, lo->lo_sizelimit, lo->lo_backing_file); @@ -1377,9 +1385,8 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) out_unfreeze: blk_mq_unfreeze_queue(lo->lo_queue); - if (!err && (info->lo_flags & LO_FLAGS_PARTSCAN) && - !(lo->lo_flags & LO_FLAGS_PARTSCAN)) { - lo->lo_flags |= LO_FLAGS_PARTSCAN; + if (!err && (lo->lo_flags & LO_FLAGS_PARTSCAN) && + !(prev_lo_flags & LO_FLAGS_PARTSCAN)) { lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN; bdev = lo->lo_device; partscan = true; diff --git a/include/uapi/linux/loop.h b/include/uapi/linux/loop.h index 080a8df134ef..6b32fee80ce0 100644 --- a/include/uapi/linux/loop.h +++ b/include/uapi/linux/loop.h @@ -25,6 +25,12 @@ enum { LO_FLAGS_DIRECT_IO = 16, }; +/* LO_FLAGS that can be set using LOOP_SET_STATUS(64) */ +#define LOOP_SET_STATUS_SETTABLE_FLAGS (LO_FLAGS_AUTOCLEAR | LO_FLAGS_PARTSCAN) + +/* LO_FLAGS that can be cleared using LOOP_SET_STATUS(64) */ +#define LOOP_SET_STATUS_CLEARABLE_FLAGS (LO_FLAGS_AUTOCLEAR) + #include /* for __kernel_old_dev_t */ #include /* for __u64 */ @@ -37,7 +43,7 @@ struct loop_info { int lo_offset; int lo_encrypt_type; int lo_encrypt_key_size; /* ioctl w/o */ - int lo_flags; /* ioctl r/o */ + int lo_flags; char lo_name[LO_NAME_SIZE]; unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */ unsigned long lo_init[2]; @@ -53,7 +59,7 @@ struct loop_info64 { __u32 lo_number; /* ioctl r/o */ __u32 lo_encrypt_type; __u32 lo_encrypt_key_size; /* ioctl w/o */ - __u32 lo_flags; /* ioctl r/o */ + __u32 lo_flags; __u8 lo_file_name[LO_NAME_SIZE]; __u8 lo_crypt_name[LO_NAME_SIZE]; __u8 lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */ From patchwork Wed May 13 13:38:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martijn Coenen X-Patchwork-Id: 11546143 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3B58714BF for ; Wed, 13 May 2020 13:39:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5690E2242B for ; Wed, 13 May 2020 13:39:21 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=android.com header.i=@android.com header.b="kQeB/K4j" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388679AbgEMNjV (ORCPT ); Wed, 13 May 2020 09:39:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56880 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388424AbgEMNjE (ORCPT ); Wed, 13 May 2020 09:39:04 -0400 Received: from mail-wm1-x342.google.com (mail-wm1-x342.google.com [IPv6:2a00:1450:4864:20::342]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E552DC061A0F for ; Wed, 13 May 2020 06:39:03 -0700 (PDT) Received: by mail-wm1-x342.google.com with SMTP id g14so11897783wme.1 for ; Wed, 13 May 2020 06:39:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=m42DexSkjthnmR2nv1z7z9ZHwTht4oRAMG/N2nS3T54=; b=kQeB/K4jcQYEjcw+9FHGg/IXlltIydy0Hgjac/SFoG0F3KfEHQIROVvwdIVkfHnms8 tL5jiu5VaMkyBlgwk3tXtComo3k7d8qXxLXX5BMQ08QSSHZ+DuSnomSt0Ozorf3cIFzn eSuatGamb3DGTcLgW/WFHAz7n7PKi7DtQ9cLGRdUA3uwp3U60j4VKEyIUz0zr4tthZHU j1U1mb+hG5yaG1H08evvxTCUKsEnVL7n9yefpdlBWXTieOZAHK9TSJK/CFIsbYfgTzzU Vxu1dqeYsyAGp2R+X4tXs475YkmUz7qaZ9NrDCnEMyv9nZDkzJ/2yvBAlayqCgb99+mp 7ZoA== 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=m42DexSkjthnmR2nv1z7z9ZHwTht4oRAMG/N2nS3T54=; b=Fg2lZEyWSynFkVcsv9vGpwTo1u2iTTtNdeKglMHzaJRo1ixwzaGQAmPLUtcqQAUsJ3 tKqpTzhhOJcAYVEVOMcXUcI5dPPh+HBss+CIXNhmn6wPULNpYr8Yv/I+G/pSJLwRBtwB 6mAy5fHSe1X55PvIvLcuAdg95IIsEegELoWUVVmjHjJ2V1iLXV0muTE4f/U9FzEkXvYz lR/YS+Mml8tSr2pPFPvVtvCO7WKcflm8mbWdqH5w1plDDdv280XjhopFsMjt5suIvdYX 8b0s6EGA0AU11E1Eo/xioVOz33i0BAyc8bwy6TJVy2nrgJ5UoBZg18YvAh6rNdnx4WGh 8xcg== X-Gm-Message-State: AGi0PuYeZ2FZ3FDTl3i1AUN/5nlwk2+558RvBpL4NCDa7P/Rh532aLCz lgo8vrEO1bG3GQu+Tr28HQX9Kg== X-Google-Smtp-Source: APiQypJaSku80ijd+GlLJCgxQu5fjO4XKe64vj7INT3TepylibJN/t/CMrDBhsGo1S5zX9b44LUTuw== X-Received: by 2002:a1c:384:: with SMTP id 126mr43642294wmd.58.1589377142570; Wed, 13 May 2020 06:39:02 -0700 (PDT) Received: from maco2.ams.corp.google.com (a83-162-234-235.adsl.xs4all.nl. [83.162.234.235]) by smtp.gmail.com with ESMTPSA id m6sm26202653wrq.5.2020.05.13.06.39.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 May 2020 06:39:01 -0700 (PDT) From: Martijn Coenen To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com Cc: narayan@google.com, zezeozue@google.com, maco@google.com, kernel-team@android.com, bvanassche@acm.org, Chaitanya.Kulkarni@wdc.com, jaegeuk@kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Martijn Coenen , Linux API Subject: [PATCH v5 11/11] loop: Add LOOP_CONFIGURE ioctl Date: Wed, 13 May 2020 15:38:45 +0200 Message-Id: <20200513133845.244903-12-maco@android.com> X-Mailer: git-send-email 2.26.2.645.ge9eca65c58-goog In-Reply-To: <20200513133845.244903-1-maco@android.com> References: <20200513133845.244903-1-maco@android.com> MIME-Version: 1.0 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org This allows userspace to completely setup a loop device with a single ioctl, removing the in-between state where the device can be partially configured - eg the loop device has a backing file associated with it, but is reading from the wrong offset. Besides removing the intermediate state, another big benefit of this ioctl is that LOOP_SET_STATUS can be slow; the main reason for this slowness is that LOOP_SET_STATUS(64) calls blk_mq_freeze_queue() to freeze the associated queue; this requires waiting for RCU synchronization, which I've measured can take about 15-20ms on this device on average. In addition to doing what LOOP_SET_STATUS can do, LOOP_CONFIGURE can also be used to: - Set the correct block size immediately by setting loop_config.block_size (avoids LOOP_SET_BLOCK_SIZE) - Explicitly request direct I/O mode by setting LO_FLAGS_DIRECT_IO in loop_config.info.lo_flags (avoids LOOP_SET_DIRECT_IO) - Explicitly request read-only mode by setting LO_FLAGS_READ_ONLY in loop_config.info.lo_flags Here's setting up ~70 regular loop devices with an offset on an x86 Android device, using LOOP_SET_FD and LOOP_SET_STATUS: vsoc_x86:/system/apex # time for i in `seq 30 100`; do losetup -r -o 4096 /dev/block/loop$i com.android.adbd.apex; done 0m03.40s real 0m00.02s user 0m00.03s system Here's configuring ~70 devices in the same way, but using a modified losetup that uses the new LOOP_CONFIGURE ioctl: vsoc_x86:/system/apex # time for i in `seq 30 100`; do losetup -r -o 4096 /dev/block/loop$i com.android.adbd.apex; done 0m01.94s real 0m00.01s user 0m00.01s system Reviewed-by: Christoph Hellwig Signed-off-by: Martijn Coenen --- drivers/block/loop.c | 104 ++++++++++++++++++++++++++++---------- include/uapi/linux/loop.h | 21 ++++++++ 2 files changed, 97 insertions(+), 28 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 13518ba191f5..a565c5aafa52 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -228,6 +228,19 @@ static void __loop_update_dio(struct loop_device *lo, bool dio) blk_mq_unfreeze_queue(lo->lo_queue); } +/** + * loop_validate_block_size() - validates the passed in block size + * @bsize: size to validate + */ +static int +loop_validate_block_size(unsigned short bsize) +{ + if (bsize < 512 || bsize > PAGE_SIZE || !is_power_of_2(bsize)) + return -EINVAL; + + return 0; +} + /** * loop_set_size() - sets device size and notifies userspace * @lo: struct loop_device to set the size for @@ -1050,23 +1063,24 @@ loop_set_status_from_info(struct loop_device *lo, return 0; } -static int loop_set_fd(struct loop_device *lo, fmode_t mode, - struct block_device *bdev, unsigned int arg) +static int loop_configure(struct loop_device *lo, fmode_t mode, + struct block_device *bdev, + const struct loop_config *config) { struct file *file; struct inode *inode; struct address_space *mapping; struct block_device *claimed_bdev = NULL; - int lo_flags = 0; int error; loff_t size; bool partscan; + unsigned short bsize; /* This is safe, since we have a reference from open(). */ __module_get(THIS_MODULE); error = -EBADF; - file = fget(arg); + file = fget(config->fd); if (!file) goto out; @@ -1075,7 +1089,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode, * here to avoid changing device under exclusive owner. */ if (!(mode & FMODE_EXCL)) { - claimed_bdev = bd_start_claiming(bdev, loop_set_fd); + claimed_bdev = bd_start_claiming(bdev, loop_configure); if (IS_ERR(claimed_bdev)) { error = PTR_ERR(claimed_bdev); goto out_putf; @@ -1097,11 +1111,26 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode, mapping = file->f_mapping; inode = mapping->host; + size = get_loop_size(lo, file); + + if ((config->info.lo_flags & ~LOOP_CONFIGURE_SETTABLE_FLAGS) != 0) { + error = -EINVAL; + goto out_unlock; + } + + if (config->block_size) { + error = loop_validate_block_size(config->block_size); + if (error) + goto out_unlock; + } + + error = loop_set_status_from_info(lo, &config->info); + if (error) + goto out_unlock; + if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) || !file->f_op->write_iter) - lo_flags |= LO_FLAGS_READ_ONLY; - - size = get_loop_size(lo, file); + lo->lo_flags |= LO_FLAGS_READ_ONLY; error = loop_prepare_queue(lo); if (error) @@ -1109,30 +1138,28 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode, error = 0; - set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0); + set_device_ro(bdev, (lo->lo_flags & LO_FLAGS_READ_ONLY) != 0); - lo->use_dio = false; + lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO; lo->lo_device = bdev; - lo->lo_flags = lo_flags; lo->lo_backing_file = file; - lo->transfer = NULL; - lo->ioctl = NULL; - lo->lo_sizelimit = 0; lo->old_gfp_mask = mapping_gfp_mask(mapping); mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); - if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync) + if (!(lo->lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync) blk_queue_write_cache(lo->lo_queue, true, false); - if (io_is_direct(lo->lo_backing_file) && inode->i_sb->s_bdev) { + if (config->block_size) + bsize = config->block_size; + else if (io_is_direct(lo->lo_backing_file) && inode->i_sb->s_bdev) /* In case of direct I/O, match underlying block size */ - unsigned short bsize = bdev_logical_block_size( - inode->i_sb->s_bdev); + bsize = bdev_logical_block_size(inode->i_sb->s_bdev); + else + bsize = 512; - blk_queue_logical_block_size(lo->lo_queue, bsize); - blk_queue_physical_block_size(lo->lo_queue, bsize); - blk_queue_io_min(lo->lo_queue, bsize); - } + blk_queue_logical_block_size(lo->lo_queue, bsize); + blk_queue_physical_block_size(lo->lo_queue, bsize); + blk_queue_io_min(lo->lo_queue, bsize); loop_update_rotational(lo); loop_update_dio(lo); @@ -1155,14 +1182,14 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode, if (partscan) loop_reread_partitions(lo, bdev); if (claimed_bdev) - bd_abort_claiming(bdev, claimed_bdev, loop_set_fd); + bd_abort_claiming(bdev, claimed_bdev, loop_configure); return 0; out_unlock: mutex_unlock(&loop_ctl_mutex); out_bdev: if (claimed_bdev) - bd_abort_claiming(bdev, claimed_bdev, loop_set_fd); + bd_abort_claiming(bdev, claimed_bdev, loop_configure); out_putf: fput(file); out: @@ -1582,8 +1609,9 @@ static int loop_set_block_size(struct loop_device *lo, unsigned long arg) if (lo->lo_state != Lo_bound) return -ENXIO; - if (arg < 512 || arg > PAGE_SIZE || !is_power_of_2(arg)) - return -EINVAL; + err = loop_validate_block_size(arg); + if (err) + return err; if (lo->lo_queue->limits.logical_block_size == arg) return 0; @@ -1645,8 +1673,27 @@ static int lo_ioctl(struct block_device *bdev, fmode_t mode, int err; switch (cmd) { - case LOOP_SET_FD: - return loop_set_fd(lo, mode, bdev, arg); + case LOOP_SET_FD: { + /* + * Legacy case - pass in a zeroed out struct loop_config with + * only the file descriptor set , which corresponds with the + * default parameters we'd have used otherwise. + */ + struct loop_config config; + + memset(&config, 0, sizeof(config)); + config.fd = arg; + + return loop_configure(lo, mode, bdev, &config); + } + case LOOP_CONFIGURE: { + struct loop_config config; + + if (copy_from_user(&config, argp, sizeof(config))) + return -EFAULT; + + return loop_configure(lo, mode, bdev, &config); + } case LOOP_CHANGE_FD: return loop_change_fd(lo, bdev, arg); case LOOP_CLR_FD: @@ -1818,6 +1865,7 @@ static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode, case LOOP_CLR_FD: case LOOP_GET_STATUS64: case LOOP_SET_STATUS64: + case LOOP_CONFIGURE: arg = (unsigned long) compat_ptr(arg); /* fall through */ case LOOP_SET_FD: diff --git a/include/uapi/linux/loop.h b/include/uapi/linux/loop.h index 6b32fee80ce0..24a1c45bd1ae 100644 --- a/include/uapi/linux/loop.h +++ b/include/uapi/linux/loop.h @@ -31,6 +31,10 @@ enum { /* LO_FLAGS that can be cleared using LOOP_SET_STATUS(64) */ #define LOOP_SET_STATUS_CLEARABLE_FLAGS (LO_FLAGS_AUTOCLEAR) +/* LO_FLAGS that can be set using LOOP_CONFIGURE */ +#define LOOP_CONFIGURE_SETTABLE_FLAGS (LO_FLAGS_READ_ONLY | LO_FLAGS_AUTOCLEAR \ + | LO_FLAGS_PARTSCAN | LO_FLAGS_DIRECT_IO) + #include /* for __kernel_old_dev_t */ #include /* for __u64 */ @@ -66,6 +70,22 @@ struct loop_info64 { __u64 lo_init[2]; }; +/** + * struct loop_config - Complete configuration for a loop device. + * @fd: fd of the file to be used as a backing file for the loop device. + * @block_size: block size to use; ignored if 0. + * @info: struct loop_info64 to configure the loop device with. + * + * This structure is used with the LOOP_CONFIGURE ioctl, and can be used to + * atomically setup and configure all loop device parameters at once. + */ +struct loop_config { + __u32 fd; + __u32 block_size; + struct loop_info64 info; + __u64 __reserved[8]; +}; + /* * Loop filter types */ @@ -96,6 +116,7 @@ struct loop_info64 { #define LOOP_SET_CAPACITY 0x4C07 #define LOOP_SET_DIRECT_IO 0x4C08 #define LOOP_SET_BLOCK_SIZE 0x4C09 +#define LOOP_CONFIGURE 0x4C0A /* /dev/loop-control interface */ #define LOOP_CTL_ADD 0x4C80