Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp2618047ybz; Mon, 27 Apr 2020 00:47:27 -0700 (PDT) X-Google-Smtp-Source: APiQypLrUvYvMrllEMV9c3Vyx9AbwI5L286cgNgdUgljILjUkkrObdurSB92ivMoh4d7Ch1HslGI X-Received: by 2002:a50:cf4d:: with SMTP id d13mr17944433edk.175.1587973647757; Mon, 27 Apr 2020 00:47:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587973647; cv=none; d=google.com; s=arc-20160816; b=DCBf7Cy6ajm8JvC3cdQN/YGjfOCwFA1BrIA46w+Upg4rHOnmT5f5yN2ZdT/3R7AdFj i80e6mEqnxWIcfiURKGsRP2KYSOXFDNqcBITfhhaKs7cSFkGekeK9NaCCRKtG2FEXP/W vX9xInugHuySQqiofVLqrhsW7IdnhZMiuC1BCp6Ajp9SAKlOIhiimCtHVki1HsUyU8G7 wGOtGnXVPRNgibJ64hlJvbkdOdT8FjOgyfX5P8rylveSa2HgHuJXGpxfB4xCtzGKoqWn S9Ovf3s4MKKMzUqjslCW5gHQbSUjCWAXo3gUUjAhcXeE2JLLAm0JBgJHmmno7jy4a6OA tzKQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=BPVCBcZ+vxq/vE4PZ944u0smAG80GpkLUfGejPD/RBc=; b=U9rBCtG+chJpMfOYGTHlIgouduejE6YQMBmotpYyz7hZawlf8rWrDlW+r1GfEFnbM0 6daNThYxlp6jayTXoLHfSGpFfmcA2RCLw96xdzUTmqL3y4iYhhvMazCxBORj2Mfb6qeP 0ontF1R2EiyoH/bW+yoU37AwVDMoCw0Z+6nFmHOHqJJtVuupDhuJHvG+5nX+ydUkZVRX BPSmG7sLnVWx30yGzZut9LMVKTSYAT+B6bwvp4usE8O4Cl7Nest8hb+9vUeUNh3m9JDv cbR1VDAZbZV6/1uzd6xLayqDTsAesUpVj/P20dtl0gw5ixegKBsVaoq0cVzNShCBGQzS TJ/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@android.com header.s=20161025 header.b=gVLXq5UT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=android.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y63si8123547eda.29.2020.04.27.00.47.04; Mon, 27 Apr 2020 00:47:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@android.com header.s=20161025 header.b=gVLXq5UT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=android.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726834AbgD0HnH (ORCPT + 99 others); Mon, 27 Apr 2020 03:43:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55260 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726738AbgD0Hmm (ORCPT ); Mon, 27 Apr 2020 03:42:42 -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 D21EDC061A0F for ; Mon, 27 Apr 2020 00:42:41 -0700 (PDT) Received: by mail-wm1-x343.google.com with SMTP id e26so18398839wmk.5 for ; Mon, 27 Apr 2020 00:42:41 -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=BPVCBcZ+vxq/vE4PZ944u0smAG80GpkLUfGejPD/RBc=; b=gVLXq5UTLy0Ky0X9RXtF7PmsN+x21+1ldkywUzJDwSyOyLmpHneDPQ2I+VDgFJ/mn7 XuC4a8Kaiz+KkMu4MSvu+lqv+7aY3X1rc96lLpuBhsKGWTWEa6voZHHh8/5dA0q65fVd dX4V2Yt1Z+y9vkCTwk4Q7IJ1JD6U/4ggynoMWpUsIdYeWwzLRp+N1MwjaJEQuRxxTrx5 AKz5M/dllwEAHos70YtDbiHgGSaS3ll5T6UzwFIS7FvC1Icfo4sENqlzM80dgdXYkpPm OFpfaCmFtir8qeTArrTCXBCySxDTy2jyHHXn25HSYy8CoHx48mnCNNTq3duMpcfsjV1H iVZw== 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=BPVCBcZ+vxq/vE4PZ944u0smAG80GpkLUfGejPD/RBc=; b=TYqj/aXUXWmnCORFZIVFnC4hSJ8TV+2/W85ARw7pMDmlITE6rhsqf3AjdAr3gFEal8 CsYvIqSpfH+0WiBuE9OqeGzIUsa+rrv5snOuj6/Cmdv9MjldcmveZs2KXlFb/DrYBjKY mYhX9XuyCH+grdllrHWUWWco/saAFXjlSl/slDNftgTOwtcXlIMN9NsruGLqbvADSPqG 0afve9aw3eXHAfDAT95LccZyQtNp7eXfaSeTzqlxp1HGrOUyCRe1ur94+WQRHO4MvN1d tZaxdAOnDGV+ykdq3twEH1YhiY3XgoRwlJhF7VpvjYiq6LZ10nU3MWsk5syyTfP9kmM6 DG/Q== X-Gm-Message-State: AGi0PuZ7gFxQ6CeCkEZOnivoHml+JtNXZdMmY7ETk2CcfEUzCsowWklK juSBLfO4jgPWWPARJJmgLhoiQg== X-Received: by 2002:a1c:49:: with SMTP id 70mr23488300wma.184.1587973360626; Mon, 27 Apr 2020 00:42:40 -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 x132sm15091658wmg.33.2020.04.27.00.42.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Apr 2020 00:42:39 -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, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, maco@google.com, bvanassche@acm.org, Chaitanya.Kulkarni@wdc.com, jaegeuk@kernel.org, Martijn Coenen Subject: [PATCH v3 6/9] loop: Factor out configuring loop from status Date: Mon, 27 Apr 2020 09:42:19 +0200 Message-Id: <20200427074222.65369-7-maco@android.com> X-Mailer: git-send-email 2.26.2.303.gf8c07b1a785-goog In-Reply-To: <20200427074222.65369-1-maco@android.com> References: <20200427074222.65369-1-maco@android.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Factor out this code into a separate function, so it can be reused by other code more easily. 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 d9a1a7e8b192..b55569fce975 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -1267,13 +1267,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; @@ -1291,10 +1356,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) { @@ -1321,54 +1382,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); -- 2.26.2.303.gf8c07b1a785-goog