From patchwork Wed Apr 29 14:03:32 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martijn Coenen X-Patchwork-Id: 11517515 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 1126592A for ; Wed, 29 Apr 2020 14:03:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EE06B20B80 for ; Wed, 29 Apr 2020 14:03:49 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=android.com header.i=@android.com header.b="FAMz5fbi" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727986AbgD2ODt (ORCPT ); Wed, 29 Apr 2020 10:03:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57238 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1726885AbgD2ODs (ORCPT ); Wed, 29 Apr 2020 10:03:48 -0400 Received: from mail-wr1-x444.google.com (mail-wr1-x444.google.com [IPv6:2a00:1450:4864:20::444]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B47D8C03C1AD for ; Wed, 29 Apr 2020 07:03:47 -0700 (PDT) Received: by mail-wr1-x444.google.com with SMTP id j1so2716309wrt.1 for ; Wed, 29 Apr 2020 07:03:47 -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=vJRGIxviTVuQOHD/t055S82ZgyhsDZt1HgODzUPBt0I=; b=FAMz5fbij9gEL5YFRhS6bp6ikk4ZxQkd0SJVt068YCgnSpGZ954yW+C6oVAAN7r09K N3Gij48s/YN3CUvtxoWe5ty7etv4bNix5ZZ4IKp7tJoJw7Ygf6g72NSWw8HRpK4qgZN6 I+Va9t/Ht2G3JcYkRXcPTvQ2jMbtfrKfQ4hDr+TDtvXg+D9OzIDsTUGlskzgC8Nyyvbf fRYsoi2FbDtrEJgb3uAT93DqT7rhARm9+3jl11BSiIQkV0CQ6aB6VYj96/g8BNqdoRkg GWmwC4M4+H7ORQyaTi0JH/mQv6sjF4pCaHiSQfgMOYezARbU56ETqPm4dKjQQGkkGQg7 Hi0Q== 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=vJRGIxviTVuQOHD/t055S82ZgyhsDZt1HgODzUPBt0I=; b=VwZzQcXSieIIq7by0p63jbOpao3Wmk1f1Lggf7MuYIvJoZDgO0WgvGAswHTf6D/j6k CaYOUukN65rVBj2XcvmlxDvj/C3MCviL0GtkR3uJu0iD9qsl/Rw6vuaWWQS5M25awoZd iqSC8QxuHRjV6khnXJo3qDgr67+3CpGsyMefX7xCd+CAbb8OaQGk5FKqt1YfrS2MQw+C TehAJkGKHZX3jDBmwIE798zSbbBHEkFBGM1rHbvp9J0xXW0jimAvWfd8awOkog5ydnEN LchQZZKv56croFTrK58H+pA7sed4CCNc+06BiwZRBjnV1HlRFf3333apD+TU02w/PKZ0 RSQQ== X-Gm-Message-State: AGi0Pubd8awzQ3TLNiyezlAg59/RB4+W9Jq7A+4Mqg9W0HWoE2KEzl4P B/RzVPQ8Myu1sFqnLkqKn5Z/sw== X-Google-Smtp-Source: APiQypJ76WMHhpA7mLprFkyfvgMFHW0slffzZUtpl3y8w3UqHCST3nm+eX4TJGahw4FXC31y2kjKqw== X-Received: by 2002:a5d:458a:: with SMTP id p10mr25792895wrq.273.1588169026426; Wed, 29 Apr 2020 07:03:46 -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 d133sm8887008wmc.27.2020.04.29.07.03.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Apr 2020 07:03:45 -0700 (PDT) From: Martijn Coenen To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com Cc: narayan@google.com, zezeozue@google.com, kernel-team@android.com, maco@google.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 v4 01/10] loop: Factor out loop size validation Date: Wed, 29 Apr 2020 16:03:32 +0200 Message-Id: <20200429140341.13294-2-maco@android.com> X-Mailer: git-send-email 2.26.2.303.gf8c07b1a785-goog In-Reply-To: <20200429140341.13294-1-maco@android.com> References: <20200429140341.13294-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 Ensuring we don't truncate loff_t when casting to sector_t is done in multiple places; factor it out. Signed-off-by: Martijn Coenen --- drivers/block/loop.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index f1754262fc94..396b8bd4d75c 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -228,15 +228,30 @@ static void __loop_update_dio(struct loop_device *lo, bool dio) blk_mq_unfreeze_queue(lo->lo_queue); } +/** + * loop_validate_size() - validates that the passed in size fits in a sector_t + * @size: size to validate + */ +static int +loop_validate_size(loff_t size) +{ + if ((loff_t)(sector_t)size != size) + return -EFBIG; + + return 0; +} + static int figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit) { + int err; 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; + err = loop_validate_size(size); + if (err) + return err; + if (lo->lo_offset != offset) lo->lo_offset = offset; if (lo->lo_sizelimit != sizelimit) @@ -1003,9 +1018,9 @@ 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) + error = loop_validate_size(size); + if (error) goto out_unlock; error = loop_prepare_queue(lo); if (error) From patchwork Wed Apr 29 14:03:33 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martijn Coenen X-Patchwork-Id: 11517533 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 291A614B4 for ; Wed, 29 Apr 2020 14:04:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 10B86208FE for ; Wed, 29 Apr 2020 14:04:36 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=android.com header.i=@android.com header.b="kJ8IquZ7" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728231AbgD2OEf (ORCPT ); Wed, 29 Apr 2020 10:04:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57244 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1727989AbgD2ODu (ORCPT ); Wed, 29 Apr 2020 10:03:50 -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 F41F9C03C1AD for ; Wed, 29 Apr 2020 07:03:49 -0700 (PDT) Received: by mail-wm1-x343.google.com with SMTP id h4so2167414wmb.4 for ; Wed, 29 Apr 2020 07:03:49 -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=PSfbPTORILGMmy9wi1uMwPHU+QF0h0YWCES/qqpWmno=; b=kJ8IquZ7YxxuHbIawgD2euMh4fvtUg6vQsnmwKnHVh9yxtm2qDtfNcrReo3r3/LrvC x2Wpp7ROj65v20V04lnpdZh55JeEbrDSWf16GI5oKQuF+MDM0SbnNbNkUzPjEKchLXp1 WlXxgr5/3+Wb7Rmacdj/hnzKyadnnCWfx0Ei+KYtpWJo1tlqXfHL2pnULz63u4nthsU3 QQncpO1+l43dgX/77Stvn8wBl7EWY14G+mCvvRcJTDIMcp9PDEe0PWcZkkjvIszsb9fv uEKMX+CMkePL/9aT/ZL30rLsfaoGN2M4mG8qZ7y4uVFoDOsOoPhPa80iDpF5Dgn4QYwN 4cpg== 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=PSfbPTORILGMmy9wi1uMwPHU+QF0h0YWCES/qqpWmno=; b=chr0VTflTtUK+3QIS9U+l6SloFUSRJl0X6fdjdbUl3Lv4mNCP89xDL4hjrXTidYP4R GFx2464BeHDNbRG7N+Sdc6lc5m3ah6S+dDhB1Bhtz6EeonuYLDCUXpbQM1zfq6ZVDWyh LTnz2CnxA+t7PhV8oo8xEG4ITMFN0NWBY3fLyEdi9t8tiOyEnwBSQcFMQzdvPRDsVAmC 2fxcFtb1Mto7sYZJcBTgmDMjl6niZt9ePoebKrV33ju3OQWz1ur7IEC8XW5P7vz0tQrx LM0ccquzmqvJ1kuImnsIb+0tJ0QbfHDygJ7/PDOcgXVcW2x04Zcqnqu8SqQzOQDuemVm POew== X-Gm-Message-State: AGi0PuYKb7ayOB01B0pJgwjHfux/yn0OlWEDjrMF8gju0NM4jUR2dGIP ZSKD+A70yVQFPM7uUSNsfAlZoQ== X-Google-Smtp-Source: APiQypJsEbPf4KHwwqefKDtX7LOwJ1N3QidiKiM8JwypyuHYoxNvQM6kC0a3HDoA+yBY6hppzTtm2Q== X-Received: by 2002:a05:600c:1008:: with SMTP id c8mr3359726wmc.14.1588169027770; Wed, 29 Apr 2020 07:03:47 -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 d133sm8887008wmc.27.2020.04.29.07.03.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Apr 2020 07:03:46 -0700 (PDT) From: Martijn Coenen To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com Cc: narayan@google.com, zezeozue@google.com, kernel-team@android.com, maco@google.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 v4 02/10] loop: Factor out setting loop device size Date: Wed, 29 Apr 2020 16:03:33 +0200 Message-Id: <20200429140341.13294-3-maco@android.com> X-Mailer: git-send-email 2.26.2.303.gf8c07b1a785-goog In-Reply-To: <20200429140341.13294-1-maco@android.com> References: <20200429140341.13294-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. Signed-off-by: Martijn Coenen Reviewed-by: Christoph Hellwig --- drivers/block/loop.c | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 396b8bd4d75c..6643e48ad71c 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -241,12 +241,29 @@ loop_validate_size(loff_t size) return 0; } +/** + * 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 int figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit) { int err; loff_t size = get_size(offset, sizelimit, lo->lo_backing_file); - struct block_device *bdev = lo->lo_device; err = loop_validate_size(size); if (err) @@ -256,10 +273,9 @@ figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit) lo->lo_offset = offset; if (lo->lo_sizelimit != sizelimit) lo->lo_sizelimit = sizelimit; - set_capacity(lo->lo_disk, x); - 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); + return 0; } @@ -1055,11 +1071,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 Apr 29 14:03:34 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martijn Coenen X-Patchwork-Id: 11517531 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 E85F092A for ; Wed, 29 Apr 2020 14:04:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CF35F208FE for ; Wed, 29 Apr 2020 14:04:28 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=android.com header.i=@android.com header.b="sxIzaHkm" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727119AbgD2OE2 (ORCPT ); Wed, 29 Apr 2020 10:04:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57254 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1728047AbgD2ODv (ORCPT ); Wed, 29 Apr 2020 10:03:51 -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 42584C035494 for ; Wed, 29 Apr 2020 07:03:50 -0700 (PDT) Received: by mail-wr1-x442.google.com with SMTP id x17so2683850wrt.5 for ; Wed, 29 Apr 2020 07:03:50 -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=GVQ6PJVYJ1ywRYR59x9OnKcPE1BH9cVkm73W4+2Zo6k=; b=sxIzaHkm4DJewXcHHF7Lc2rxcr6U2EwKWQsb+VKVC6R4KesLhY2vD/na6H4tlmOK9/ HxcXJwT29QU1jNUghkYucNmUY2JiggMgdeLUhyQSMqNk0lXMWefTquFcCzio0ljO05QS VbyRilWCdf3See6S1xwAgB0MuNwVQANodbrBSL/Vioj89L/zmYBBXAtjYsJfaW+TAMM6 sL9WVolxXpeRl9RlBoMR40192Y1xqH8jxANH73uCWshGc1+QJfMO+3OzWTYQldI1ZhU/ dbKHj3/iG9wHVlrZpJQeaGTZcQjx2bpBE4w4Xe70DOYT7pV6Kwpy3e3AJS7pCqtC50xm +Nmw== 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=GVQ6PJVYJ1ywRYR59x9OnKcPE1BH9cVkm73W4+2Zo6k=; b=sbB53s7jO05TNiPgYsrhpp523E/KoPjcbyfzbDaAzjmjlY7dW54/dHsL5hT+Wk3p92 cHiiWM/1wQwAFERUHCyy/Jc4UMhZG5LPWO4kljSrKVoClf9Ymk3Z4U9Vabi90c4fOpkf OO/mqutfPuzP14daW6KtwjT/0O52CyMfFuzbjdDtLA/U9ym8v9bJwZy8NbcYncxweo7u MuI1h6lST5gnvl4cO0a98ZjvbmBUyavuSbuSvGsyeJF3a90Wr02ZAvklkkaLQsy81AtP pXtWrM5xiCqzfNObd//hfyI4YpYNas66jyDisSASdVosTF7+noaOLRL6bSuUZCZtNwII Ezhw== X-Gm-Message-State: AGi0PubRfz80FBsI6hTU4ctuEU8io7DVjLLESirdOGajsiUpQDzACrwn +RyyanLJ9HXE0ptLEYHZIG8oxA== X-Google-Smtp-Source: APiQypLMl/5GOABd24RbSC64VM+meR4NKVn0FFgfHYJQTI/1/27Ydb0VqQiSLlmjgVmI1tyZTO4GKQ== X-Received: by 2002:a5d:498b:: with SMTP id r11mr39222128wrq.368.1588169028956; Wed, 29 Apr 2020 07:03:48 -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 d133sm8887008wmc.27.2020.04.29.07.03.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Apr 2020 07:03:48 -0700 (PDT) From: Martijn Coenen To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com Cc: narayan@google.com, zezeozue@google.com, kernel-team@android.com, maco@google.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 v4 03/10] loop: Switch to set_capacity_revalidate_and_notify() Date: Wed, 29 Apr 2020 16:03:34 +0200 Message-Id: <20200429140341.13294-4-maco@android.com> X-Mailer: git-send-email 2.26.2.303.gf8c07b1a785-goog In-Reply-To: <20200429140341.13294-1-maco@android.com> References: <20200429140341.13294-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. Signed-off-by: Martijn Coenen Reviewed-by: Christoph Hellwig --- 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 6643e48ad71c..8f3194c2b8aa 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -253,10 +253,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 int From patchwork Wed Apr 29 14:03: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: 11517529 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 D524B92A for ; Wed, 29 Apr 2020 14:04:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BCA2A2082E for ; Wed, 29 Apr 2020 14:04:26 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=android.com header.i=@android.com header.b="ITfmWJKK" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728007AbgD2ODw (ORCPT ); Wed, 29 Apr 2020 10:03:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57256 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1728080AbgD2ODw (ORCPT ); Wed, 29 Apr 2020 10:03:52 -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 ACE60C03C1AD for ; Wed, 29 Apr 2020 07:03:51 -0700 (PDT) Received: by mail-wr1-x442.google.com with SMTP id x17so2683965wrt.5 for ; Wed, 29 Apr 2020 07:03:51 -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=PoJQjkbDBTvS6CFNLYXBcb580Ir6qHsMYFIGsSVt/dk=; b=ITfmWJKKpQF4vwJQgg3vHBkXHE0SUBFXOnZzX6K2D+4GdAQYvyJzC5trVgkBo+K47+ TIcQX1Z+K/C5s1TNPsk/8WPZBxGpRyuS2Ol79xxwdA/FObD3HukuPGqH+1DFB/DKMKF4 Qyw8YmKJpXwYH/TcFDtiPJzVccGPqBKDesdZ1vZIAKPNiL4FJ/Atzo50cdCUIOoRprO7 mKcoWEvW7NWEjdmKfZJkoevt82+TbbWRIMvuW0Xux+Tf3LV3L5jTUmJ8AfHIqCC1zDC/ RfULXfIBWninskSu0MfXNW2zhxdSpn1oigOJF420HWBBoWfnOr/9hWeV2b7Sjc1geqBW Fb5g== 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=PoJQjkbDBTvS6CFNLYXBcb580Ir6qHsMYFIGsSVt/dk=; b=ohFtu0Bw2oftE1mb0WWWGxoSoxFQBE5FFj8nGUE4p0WDcn1duXvjzaON26p03oo76r 8s+z9VjISxTNdCyBrDhHzCGM9BPv6K5Z9hc17mcMWAxhHeIBb75RZCyQO/+qvLTg+Faw 3jh9cDrjpuw5xMCBy29x5viQKzS95icosdIXIQIOgtsrLq4k4vhlDwScVS58l+bSLIxg N6mdD2CkkNsgFgVDQwD+apDevS6bNlOwsy5/tQm3+O7dqFMDTYkBZ6Ml+sbCuexs0YPV uRNwrE6kv3gI3hEKily5pI4Rt6XzM18qrKKA/vygYXIX1KnVEoL98+gqYsPBMFxR8GSe L8SA== X-Gm-Message-State: AGi0PubhBjXwc98/0N/vYe2sl8UAWrc68O8XRchuXgC12FmdJwaN0GEv ItNQOWfKt4BvtFbBUvrNQ2bJQg== X-Google-Smtp-Source: APiQypLdGhK29pFXm0dZ/bi2Kip6HNq6NAOpBoOKsa7X/fOmZscDthU9nK3s9w7zqMH2en3DTmnWNA== X-Received: by 2002:adf:e84f:: with SMTP id d15mr41411225wrn.296.1588169030446; Wed, 29 Apr 2020 07:03: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 d133sm8887008wmc.27.2020.04.29.07.03.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Apr 2020 07:03:49 -0700 (PDT) From: Martijn Coenen To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com Cc: narayan@google.com, zezeozue@google.com, kernel-team@android.com, maco@google.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 v4 04/10] loop: Refactor loop_set_status() size calculation Date: Wed, 29 Apr 2020 16:03:35 +0200 Message-Id: <20200429140341.13294-5-maco@android.com> X-Mailer: git-send-email 2.26.2.303.gf8c07b1a785-goog In-Reply-To: <20200429140341.13294-1-maco@android.com> References: <20200429140341.13294-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. Signed-off-by: Martijn Coenen Reviewed-by: Christoph Hellwig --- drivers/block/loop.c | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 8f3194c2b8aa..9f5913879921 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -268,11 +268,6 @@ figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit) if (err) return err; - if (lo->lo_offset != offset) - lo->lo_offset = offset; - if (lo->lo_sizelimit != sizelimit) - lo->lo_sizelimit = sizelimit; - loop_set_size(lo, size); return 0; @@ -1294,6 +1289,8 @@ 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; + loff_t validated_size; err = mutex_lock_killable(&loop_ctl_mutex); if (err) @@ -1315,6 +1312,13 @@ 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) { + loff_t size = get_size(info->lo_offset, info->lo_sizelimit, + lo->lo_backing_file); + err = loop_validate_size(size); + if (err) + goto out_unlock; + size_changed = true; + validated_size = size; sync_blockdev(lo->lo_device); kill_bdev(lo->lo_device); } @@ -1322,6 +1326,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; @@ -1345,22 +1358,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; - } - if (figure_loop_size(lo, info->lo_offset, info->lo_sizelimit)) { - err = -EFBIG; - goto out_unfreeze; - } - } - + 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; @@ -1384,6 +1383,9 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) lo->lo_key_owner = uid; } + if (size_changed) + loop_set_size(lo, validated_size); + loop_config_discard(lo); /* update dio if lo_offset or transfer is changed */ From patchwork Wed Apr 29 14:03: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: 11517525 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 1CCF592A for ; Wed, 29 Apr 2020 14:04:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 05503208FE for ; Wed, 29 Apr 2020 14:04:20 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=android.com header.i=@android.com header.b="SlWtdLeY" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728105AbgD2OD4 (ORCPT ); Wed, 29 Apr 2020 10:03:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57264 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1728097AbgD2ODx (ORCPT ); Wed, 29 Apr 2020 10:03:53 -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 1E1D5C03C1AD for ; Wed, 29 Apr 2020 07:03:53 -0700 (PDT) Received: by mail-wr1-x442.google.com with SMTP id d15so2698057wrx.3 for ; Wed, 29 Apr 2020 07:03: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=TIeVXquln7tlIswAu85f97R17dJ4bzbZ9XV/+MshL8g=; b=SlWtdLeYzYPtXagrnKtjgKGpFduQsopIMSr3rFtGr5NKZQnx6pvIqJ1bzghFJinboF d9mc05RTxXBRZrTDUFfr5cJmb6DM7qhDviLAXWGUdR30Un0HTiJIw398GNmtcrNIHWzC 5E7qrwv2trXInl3YWdhWJvPYAtWUgC2vQ+p61aOxPJBSZhe53Q5hgZS7+PdKVEYj/pE1 6oQrISeDHDRVIO0QdQmk7HJUjbcxYEXNMhq+mNuaMcXzdh3F0tLFt50fK8IAjJCCdxl/ K0t4ZK/YBANLr9llB9W1a5GEW+tshtgWOqyh0F5XjXz8jrMuMtOZv5gegVmVeMlIG8Fb SP9w== 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=TIeVXquln7tlIswAu85f97R17dJ4bzbZ9XV/+MshL8g=; b=tzswaxYktWXPdBQBZz1Zsgz/r3+Sx7UDGQKgfgDQftfOn918lzXBAZv/SNhitO+7x+ oZ0O8u/ZSuPjoEYf25qzge7JuCL9dADeuc1nZE1IRX0qt2WQ7zz8AvkVrWZPtlDt1any uA5+sk4TcehiO8tEufbIYMaTShnqJLXcxVMfjB1hSbgfUeMrtZlaJ1v2JVdpfZrvk+p3 ZOZRSuHiWHAnuV/MUtqQ6LqPx7m9B5dD1RVEf03RyYETdXHgVCcUHdM+gOBzGU0t15sf Di9K//gwkSb8Anarf80s64GD7pzZEr6Wq66Au/FOEc7ocNXCkgSWcAk74/ozhchJhfDX RP6w== X-Gm-Message-State: AGi0Pubic036xugw5Dhi9TMSu6Z33j4A048fOH8wHFYy1L52g7wMp23T vIpMYlmrSRuizbGKjxKDBzV4rA== X-Google-Smtp-Source: APiQypJzPpeiQf7KPXZBFO4DRN+xbvSitwNF/2b2JhPozw2wP25rWwFUGnyFMDbuGAGQ6oHAJ5rUow== X-Received: by 2002:a5d:51c6:: with SMTP id n6mr39219978wrv.314.1588169031853; Wed, 29 Apr 2020 07:03:51 -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 d133sm8887008wmc.27.2020.04.29.07.03.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Apr 2020 07:03:51 -0700 (PDT) From: Martijn Coenen To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com Cc: narayan@google.com, zezeozue@google.com, kernel-team@android.com, maco@google.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 v4 05/10] loop: Remove figure_loop_size() Date: Wed, 29 Apr 2020 16:03:36 +0200 Message-Id: <20200429140341.13294-6-maco@android.com> X-Mailer: git-send-email 2.26.2.303.gf8c07b1a785-goog In-Reply-To: <20200429140341.13294-1-maco@android.com> References: <20200429140341.13294-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. Signed-off-by: Martijn Coenen Reviewed-by: Christoph Hellwig --- drivers/block/loop.c | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 9f5913879921..32755e874326 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -258,21 +258,6 @@ static void loop_set_size(struct loop_device *lo, loff_t size) set_capacity_revalidate_and_notify(lo->lo_disk, size, false); } -static int -figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit) -{ - int err; - loff_t size = get_size(offset, sizelimit, lo->lo_backing_file); - - err = loop_validate_size(size); - if (err) - return err; - - loop_set_size(lo, size); - - return 0; -} - static inline int lo_do_transfer(struct loop_device *lo, int cmd, struct page *rpage, unsigned roffs, @@ -1560,10 +1545,21 @@ loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) { static int loop_set_capacity(struct loop_device *lo) { + int err; + loff_t size; + if (unlikely(lo->lo_state != Lo_bound)) return -ENXIO; - return figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit); + size = get_loop_size(lo, lo->lo_backing_file); + + err = loop_validate_size(size); + if (err) + return err; + + loop_set_size(lo, size); + + return 0; } static int loop_set_dio(struct loop_device *lo, unsigned long arg) From patchwork Wed Apr 29 14:03: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: 11517527 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 A115014B4 for ; Wed, 29 Apr 2020 14:04:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 891D22082E for ; Wed, 29 Apr 2020 14:04:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=android.com header.i=@android.com header.b="R0DwcNb3" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728122AbgD2OEU (ORCPT ); Wed, 29 Apr 2020 10:04:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57274 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1728102AbgD2ODz (ORCPT ); Wed, 29 Apr 2020 10:03:55 -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 736BAC03C1AD for ; Wed, 29 Apr 2020 07:03:54 -0700 (PDT) Received: by mail-wr1-x443.google.com with SMTP id b11so2679935wrs.6 for ; Wed, 29 Apr 2020 07:03: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=2HC08dUHVOBIjJahrhCZVwxh1xu6QFmdAnccVqLD3gg=; b=R0DwcNb379f900LA16sf6jG4SWkLq/2xQL+qN7tElj7ZFiQNFqvQf0zH1wjKlWfEjR sRQbQT2T1J16XwicUS3/Z2un3jV3lu/4sFb35zv4LIPc/RFL63s9vvf5Kl4DS41XabHz l5V4uFJxg9yKfp1ipBl9DMTamCHhgdPFt3SnlOkib5kBFLykkBfCMYN1ePxiiGb+r4D1 ubd+FySlJQB1Rj5VG7R8O8TCFuFLceidrIAcJpEIiWESB+7aTfEU2yJdbiYsuaM7iY+4 PFYaFZrdJLJTifaXQpccywEeK7lQM6QZ4NP4vee7yZtC4neSUcE4rTeL2SVekohewYfn qzTg== 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=2HC08dUHVOBIjJahrhCZVwxh1xu6QFmdAnccVqLD3gg=; b=sDWXYlnJWGz7rzZ+xx43nFljgKmMKCoNdvJIj91Y9qsHQHIO7Yn2nuDA4sAzmq/taz szaXHo3WxfoKPlEPoKEHZJxKcB5Pnptiv6vCs72kqvAb0Wz36GmhqfPKFSi1s+A+zgFJ dvt31s8dcr1pXPaqtSv4sQjjuBMZ5Xj8B/xze8kjZ7VDab/gUkimfc0ZnOEpjZmBaCTF z0jWASZuiUzIuH8cvoNDbDcIDWmTAPFE2SuQ3tCI1AMw0ZpdoHK1NhQ4ootP7tlmVQ/1 lIKlQ9WcUaTxivosa878Ezim+waR5EezfrnoebB3pNAF5owBBYqg0WF5+FaeeNx0ibBk 1aNA== X-Gm-Message-State: AGi0PuY/13/tfEfkqDOFMKag5EcVzvwCcWWo8Y6okZKbg3F7w4RdiRSo kQSxByLljfnc0Cnx3JGUgbtwtg== X-Google-Smtp-Source: APiQypJZibuLny3Lo7URMsqeXYQiMnJb3tO5aJv9bpGM4hSzfrTV2Mi1xp8ZsRwizK8SkX2hOvEhYw== X-Received: by 2002:a5d:634f:: with SMTP id b15mr37706649wrw.46.1588169033159; Wed, 29 Apr 2020 07:03: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 d133sm8887008wmc.27.2020.04.29.07.03.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Apr 2020 07:03:52 -0700 (PDT) From: Martijn Coenen To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com Cc: narayan@google.com, zezeozue@google.com, kernel-team@android.com, maco@google.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 v4 06/10] loop: Factor out configuring loop from status Date: Wed, 29 Apr 2020 16:03:37 +0200 Message-Id: <20200429140341.13294-7-maco@android.com> X-Mailer: git-send-email 2.26.2.303.gf8c07b1a785-goog In-Reply-To: <20200429140341.13294-1-maco@android.com> References: <20200429140341.13294-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. Signed-off-by: Martijn Coenen Reviewed-by: Christoph Hellwig --- 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 32755e874326..4a36a3f47503 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -1266,13 +1266,78 @@ 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(); + + 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) +{ + int err; struct block_device *bdev; + kuid_t uid = current_uid(); bool partscan = false; bool size_changed = false; loff_t validated_size; @@ -1290,10 +1355,6 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) 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) { @@ -1320,54 +1381,10 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) goto out_unfreeze; } - err = loop_release_xfer(lo); + err = loop_set_status_from_info(lo, info); if (err) goto out_unfreeze; - if (info->lo_encrypt_type) { - unsigned int type = info->lo_encrypt_type; - - if (type >= MAX_LO_CRYPT) { - err = -EINVAL; - goto out_unfreeze; - } - xfer = xfer_funcs[type]; - if (xfer == NULL) { - err = -EINVAL; - goto out_unfreeze; - } - } else - xfer = NULL; - - err = loop_init_xfer(lo, xfer, info); - if (err) - goto out_unfreeze; - - 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; - } - if (size_changed) loop_set_size(lo, validated_size); From patchwork Wed Apr 29 14:03: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: 11517523 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 C349E14B4 for ; Wed, 29 Apr 2020 14:04:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A5BDC2186A for ; Wed, 29 Apr 2020 14:04:17 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=android.com header.i=@android.com header.b="LE9BhdJ0" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727888AbgD2OEO (ORCPT ); Wed, 29 Apr 2020 10:04:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1728120AbgD2OD5 (ORCPT ); Wed, 29 Apr 2020 10:03:57 -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 E3AD2C035494 for ; Wed, 29 Apr 2020 07:03:55 -0700 (PDT) Received: by mail-wm1-x344.google.com with SMTP id y24so2159124wma.4 for ; Wed, 29 Apr 2020 07:03:55 -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=hmU3PBXf6dhWHFQlW8YZGbPLjN065hLB6uprJoU2IF0=; b=LE9BhdJ0/TmpLlVMtxtt6RToML+PfaNi4fMHhei9MClgSqIW9ok01yn4G2P9d1gvE0 kI0+x/hdrPCUp3rf5B6dgOhPlyFuGzDtHA0ibLtShdxyvTsmbrsiZgvUKfgW4P4yrEfC 793mnY4YMGpcvwbtpTeIYY1XNja31PeA7X3GQzt9dSuSNInJ29YqYN/gzNUl+DPzXtrK xqtZxXTYFtbiA8QhcuUss1wB6O/rIhiey5euIx6P5Zfmnywn9kCJ3I0u5mwOcGWNEvym BYwNZTlp5o8S8IYbaGEkpMbxu/mgp14r4rR8JpSpOP4pnhWtAcILpxM9T3gvWBfyaVuB t3VA== 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=hmU3PBXf6dhWHFQlW8YZGbPLjN065hLB6uprJoU2IF0=; b=lBP+1lul+UpAoX7hKwpn2NLF5odC3wCMdT1B7g6m28g1uOdSB3aO9sDLYCnqmfjiWT c1iol8cMlQkbAfrqvABibPoaPhoxJ+YYNhhJ22fzenKAtyxxTE4arvjFTmB/NLLtk0qc TUiamtkqejS7d3rSj+KFsGp/g2HuonDsxuYPE9ji3GX0cYLMCAK8CoKnEszeRzHWP2Qi B8OyOJ8TIWVPgrHwaybKUkY2ZHcCqr+DMxjSQZHPKIcC9iGNQ2vrjj1jBoyLVxea2sh3 EeXhmrOWFdOwO2Yi9rBHMSx239OMtP3+Z0Mo4YZhvUHCFx2qO5h6DFWbuwGZazRCP4t3 EmWg== X-Gm-Message-State: AGi0PuaFA+bckkYgPW7xtJnEYhdmB7uiN2ycjrJVJ5s6os8aC+9CKM8w 1ajyGCk5yGL/pdjU7oLVz89nzw== X-Google-Smtp-Source: APiQypJKTvAtLG7slROxyNfQknjkesDM9yKf/JC72SKZhVuC+bDDogyptbmnCLXSBW+dKWmD/6S7mQ== X-Received: by 2002:a1c:48c:: with SMTP id 134mr3236527wme.47.1588169034570; Wed, 29 Apr 2020 07:03: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 d133sm8887008wmc.27.2020.04.29.07.03.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Apr 2020 07:03:53 -0700 (PDT) From: Martijn Coenen To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com Cc: narayan@google.com, zezeozue@google.com, kernel-team@android.com, maco@google.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 v4 07/10] loop: Move loop_set_status_from_info() and friends up Date: Wed, 29 Apr 2020 16:03:38 +0200 Message-Id: <20200429140341.13294-8-maco@android.com> X-Mailer: git-send-email 2.26.2.303.gf8c07b1a785-goog In-Reply-To: <20200429140341.13294-1-maco@android.com> References: <20200429140341.13294-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. Signed-off-by: Martijn Coenen Reviewed-by: Christoph Hellwig --- 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 4a36a3f47503..60ba1ed95d77 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -962,6 +962,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) { @@ -1085,43 +1188,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; @@ -1266,72 +1332,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 Apr 29 14:03: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: 11517519 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 E8F7814DD for ; Wed, 29 Apr 2020 14:04:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D0432217D8 for ; Wed, 29 Apr 2020 14:04:01 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=android.com header.i=@android.com header.b="a2nlCP4k" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728142AbgD2OEB (ORCPT ); Wed, 29 Apr 2020 10:04:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57288 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1728130AbgD2OD6 (ORCPT ); Wed, 29 Apr 2020 10:03:58 -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 14AD6C03C1AE for ; Wed, 29 Apr 2020 07:03:57 -0700 (PDT) Received: by mail-wr1-x442.google.com with SMTP id i10so2664169wrv.10 for ; Wed, 29 Apr 2020 07:03:57 -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=76X/UFv1Q/0+UD4XjTZigIYFIhcVyVy/pGs0yDiv+KI=; b=a2nlCP4kujk1JdspGfkSvRMjfAyKHgNcbh7eDZ2YsgnmMhWW8SMQaYeHm3YKbrRGKt OyuLq94Sxd0aok+1tgG6swhbvvcYqLrMRtEDd277rIdyChH+nzHZwMxN8bapKTMEHjbo 1nvS6vLTt6AaVowqe3YLrqci7+9VD1OSyhmjpSytgr9L/w1M6GKPS9+vEHIOauzRYz5Z 03e3erq/GXuzs3GJr0XzyEgkx4bxLIieEcR/EAXLEo5vxRyEaW7XcZd5Hhfyvd5Rpnq4 +UvAdvoFaE257ABnFIM63T3Ld6C+pePjL3nQqcZORFqKGUQZMgFcAtmNfyT4l8nss7wh ZRQQ== 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=76X/UFv1Q/0+UD4XjTZigIYFIhcVyVy/pGs0yDiv+KI=; b=Sd3OhBB8EDMoROrkEn1ewe7/IlwA4p9HHdz1wBX6ZWrVrURLtTBNC8zs7Z+w53XP+l Kwrq4Tu/Fra+vF+GTk8n3XzeIOSIo8HU8AkSOImOV/7qJt5FfR6sIj/qV+lfQKen1LIS wVlSVBdh19h1lyp5C+B2uNvZ+GIly1HymAzjbVPff4gzohhbYdG0z2dA6V5vZIXNhzrl 465nq1bUkuMKdQwm1jsqU3fPx+W0hzPOtWwRSe5XDpm5+PToeRVdCmlmilRlUW9bsOoN x9hnLxE8IY+v3CxtTEWQ25S0rKMVSjoPbXoU5KfnIzLVCok7yhktGfs+OBxzjvkWhA19 FWXQ== X-Gm-Message-State: AGi0PuZWSIKxFCVHEDnnyxuTcYEtzludduBf8qRtGVmsfx/KlUEPYRxM KDFGzSLWjCy5EbMYFK9YeKOEBg== X-Google-Smtp-Source: APiQypKa3V4BSnAtNrL9VpaMlBURZo2D72JJgh7YSC6FI2/Dx8zbM4CM5D/LdfbzcQb8QMrT0MHzIg== X-Received: by 2002:a05:6000:110a:: with SMTP id z10mr39247853wrw.389.1588169035863; Wed, 29 Apr 2020 07:03: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 d133sm8887008wmc.27.2020.04.29.07.03.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Apr 2020 07:03:54 -0700 (PDT) From: Martijn Coenen To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com Cc: narayan@google.com, zezeozue@google.com, kernel-team@android.com, maco@google.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 v4 08/10] loop: Rework lo_ioctl() __user argument casting Date: Wed, 29 Apr 2020 16:03:39 +0200 Message-Id: <20200429140341.13294-9-maco@android.com> X-Mailer: git-send-email 2.26.2.303.gf8c07b1a785-goog In-Reply-To: <20200429140341.13294-1-maco@android.com> References: <20200429140341.13294-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. Signed-off-by: Martijn Coenen Reviewed-by: Christoph Hellwig --- 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 60ba1ed95d77..f172a64333aa 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -1659,6 +1659,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) { @@ -1671,21 +1672,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 Apr 29 14:03: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: 11517517 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 80E3292A for ; Wed, 29 Apr 2020 14:04:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6352F2082E for ; Wed, 29 Apr 2020 14:04:01 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=android.com header.i=@android.com header.b="CUy816SA" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728138AbgD2OD7 (ORCPT ); Wed, 29 Apr 2020 10:03:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1728137AbgD2OD7 (ORCPT ); Wed, 29 Apr 2020 10:03:59 -0400 Received: from mail-wr1-x444.google.com (mail-wr1-x444.google.com [IPv6:2a00:1450:4864:20::444]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 761B7C035493 for ; Wed, 29 Apr 2020 07:03:58 -0700 (PDT) Received: by mail-wr1-x444.google.com with SMTP id t14so2657211wrw.12 for ; Wed, 29 Apr 2020 07:03: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=WSTM3Qi/4U9iaA1lz8BvS/1iZyuC1rKHgcM75ldZEu4=; b=CUy816SARBoL3Uki3aIIhK4RmGONTtkFUicVkdJGwEY79gT7qkVdXYcuhkWVB7hMOP dWvfwllgHNLuEmmFhBvTQztCjHJPyzkVpBQwTAPZBJB7/akeF4Sldi2OQRO6ZX37f1Vd P7HTOFl1gKJZN8is5CoOkKHL7BhFkubDw+V0nNNa8LPi1bebKXNAPAdeqrUd8aL5awjR 9zG+o0v+2l0mOvQ8wsY0VJQhPbaEywfjlJU0sdx5fA7nW+sD2aFKBxdTC8Q8ItMmnWb0 OtIurjg7erO3sU910m+PEr/BLGEVYhIAF3tI/ONoxIXSHwyXMztfzoc5naQyKcxaXP7L +1sA== 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=WSTM3Qi/4U9iaA1lz8BvS/1iZyuC1rKHgcM75ldZEu4=; b=PslGpZeN4puMBXWBA3xjhVzg8+K/GYNW7y79M2sYpYgvWw8goqG54l8hoV8rviiF7K /ab2IudoKfzsGhLW0AWbkXdAVytTmOtMo7fVVNrwc/hEAMmRuOf9e26ZqeO/IrTLFE/N g/hNIMxluI47fFnvhnusGxivtFF/ye62svKda/EbmiDV0hHKy7jpFWo/tVyV9L+VziaD 2mDT1+CqIBP4KZsu3reuvF3yHHZxwz/9mf97hgmUc5Q6ZAETgw/xDUhOaxJlnxuys6v2 hu5D77j9+TsYOQkneH2YdCcF5806B8vc3JbXSMEbfSJu6e2rbVaOWQ7OhlBztqwVr7DO KUVA== X-Gm-Message-State: AGi0PuaOGixbc4xzL9S2z6heQaTyXJs3gmUDZzfvViC8yKAxl6+9alwA n2DIEdHSAv7jKPeyQx6UKnzZKg== X-Google-Smtp-Source: APiQypLh806kYCaic9QEG1Qzl0KzfPxn1oM3FQfGFXaXHMJCgdvQnOl9wOD92kYUKJ4GjvyTRpnrhQ== X-Received: by 2002:adf:fa04:: with SMTP id m4mr38662555wrr.30.1588169037163; Wed, 29 Apr 2020 07:03: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 d133sm8887008wmc.27.2020.04.29.07.03.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Apr 2020 07:03:56 -0700 (PDT) From: Martijn Coenen To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com Cc: narayan@google.com, zezeozue@google.com, kernel-team@android.com, maco@google.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 v4 09/10] loop: Clean up LOOP_SET_STATUS lo_flags handling. Date: Wed, 29 Apr 2020 16:03:40 +0200 Message-Id: <20200429140341.13294-10-maco@android.com> X-Mailer: git-send-email 2.26.2.303.gf8c07b1a785-goog In-Reply-To: <20200429140341.13294-1-maco@android.com> References: <20200429140341.13294-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. Signed-off-by: Martijn Coenen Reviewed-by: Christoph Hellwig --- 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 f172a64333aa..cfbdd99fdb1a 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -1049,9 +1049,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]; @@ -1338,6 +1336,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; loff_t validated_size; @@ -1381,10 +1380,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) loop_set_size(lo, validated_size); @@ -1396,9 +1404,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 Apr 29 14:03: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: 11517521 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 6676192A for ; Wed, 29 Apr 2020 14:04:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 49847208FE for ; Wed, 29 Apr 2020 14:04:02 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=android.com header.i=@android.com header.b="QSLmat8x" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728152AbgD2OEB (ORCPT ); Wed, 29 Apr 2020 10:04:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57306 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1728143AbgD2OEB (ORCPT ); Wed, 29 Apr 2020 10:04:01 -0400 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B015C03C1AE for ; Wed, 29 Apr 2020 07:04:00 -0700 (PDT) Received: by mail-wm1-x331.google.com with SMTP id x4so2174672wmj.1 for ; Wed, 29 Apr 2020 07:04: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=hWPukYztpiknGtv35UrZBY3p0N3khiINow/Iw/DxGds=; b=QSLmat8xz9IY4lcRQBMniufYgQyi9VSrus/oIT182m9pagFmgxSw2+/P7bVSI28gnR kFwNwxFSVmAALrvDZwPCT1idRgBSBQqzJeMPAmZZedC7RN2c7SdEjTDLc9pMo1s8Gia1 9h4vd2ep+tTblJl5AiG1IGa0igv1pW5dkLbhk8lmOOKJfNN3yJXU3E7Wt2noG8A3BAC0 SIXXnTpGi+B0O8PLJAIJm9ES2uvgop9x1rHdZBnVrdhLuLcscXywE7NbAE/2VlhK5KrN UOveaLA6H+EXw0KdPAliIL6K0jGAMvUxFAvKWfpRDxUQIhLMIOgTjNJgJqHDgrP+PXR2 aIhg== 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=hWPukYztpiknGtv35UrZBY3p0N3khiINow/Iw/DxGds=; b=MmLyOQp3ko41TqbDBmi0k+32nX/CWJ4IIVpYejKDuKIxU84pryGxqsGD/suoH+gyxJ /IurDzyK+zHDg442FcA+z6qrmxlI0+6iuCjo5Umjj9PKcz31XBT4kXRU3xUppZ+Ckit3 spodhWw4tJ0LerImm3Rzu3TkfV2NkMg20m54DhOVJUFmmRjEt0nNMIAfE3q3G3FyKMJ8 Pi5mn2E/lnmkMgd9hFAQxCEFSusZ4622s4bQVUR4xb7pyUx7KfX4BS4DE0+eu77pl/sf ClGrVtJx7v4Tx6NrWLZt/yd25fGaoQwIqK9lA21Z/AaVcpz9g/hcq65Xw7dyLynWUmJ9 9mFg== X-Gm-Message-State: AGi0PubUW35KoxThCWizKtmlhJzNkEQYjBxNEDuSDKX+sTMQhu9hNg5w NyOGkwMiZmr6L4S8SoThQeu4Qw== X-Google-Smtp-Source: APiQypLsRxPhmjO7oIdCRK5IjKfWf5HEIJPJKOGiiPGtXq0lez6DRpOjodiX5RJrKowIY8eqDT0fag== X-Received: by 2002:a1c:8084:: with SMTP id b126mr3347201wmd.135.1588169038479; Wed, 29 Apr 2020 07:03:58 -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 d133sm8887008wmc.27.2020.04.29.07.03.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Apr 2020 07:03:57 -0700 (PDT) From: Martijn Coenen To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com Cc: narayan@google.com, zezeozue@google.com, kernel-team@android.com, maco@google.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 v4 10/10] loop: Add LOOP_CONFIGURE ioctl Date: Wed, 29 Apr 2020 16:03:41 +0200 Message-Id: <20200429140341.13294-11-maco@android.com> X-Mailer: git-send-email 2.26.2.303.gf8c07b1a785-goog In-Reply-To: <20200429140341.13294-1-maco@android.com> References: <20200429140341.13294-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 Signed-off-by: Martijn Coenen Reviewed-by: Christoph Hellwig --- drivers/block/loop.c | 107 +++++++++++++++++++++++++++----------- include/uapi/linux/loop.h | 21 ++++++++ 2 files changed, 99 insertions(+), 29 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index cfbdd99fdb1a..a353ce55fd18 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -241,6 +241,19 @@ loop_validate_size(loff_t size) return 0; } +/** + * 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 @@ -1063,23 +1076,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; @@ -1088,7 +1102,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; @@ -1110,44 +1124,58 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode, mapping = file->f_mapping; inode = mapping->host; - 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); error = loop_validate_size(size); if (error) goto out_unlock; + + 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->lo_flags |= LO_FLAGS_READ_ONLY; + error = loop_prepare_queue(lo); if (error) goto out_unlock; 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); @@ -1170,14 +1198,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: @@ -1607,8 +1635,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; @@ -1670,8 +1699,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: @@ -1843,6 +1891,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