From patchwork Fri Oct 11 15:43:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 13832689 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0384A194AE8; Fri, 11 Oct 2024 15:43:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728661424; cv=none; b=TuCZ4Tzp2RANmhkChUtoZp74ZYSP33/3saIioBuVfNSAemP7koWMMduPhWgG8bfy1TdUkM6yPit1IKWpsKB4m9kDoFi5AUL63JHqCSlYbV1qEZNTU16hkUk+EZFXqBJs9LfWdTD+vP/0Nq536BqXUb014N7Hsh/dX6skCgabj6I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728661424; c=relaxed/simple; bh=FmDNKZ9WHiF4im2ScJHOv4xmHbseTiUosNqW13aFw6A=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=u9wd90KMa2ZzISBiAbenriLXWSDWvajaDoDWzMpCU83Zuw9EW8BvQyu2yOhj3rWbdam13OEQY260tkoCd/+k0u5vC/nJFVcxCGCX5cKGJYpHvYOHkoSTwVZ9MoLv2g7Foxyv6JYQGUn3s88xHrmv2exunXAo31FmUMYpLOIPxKw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=oHWwi/CG; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="oHWwi/CG" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 66A34C4CEC7; Fri, 11 Oct 2024 15:43:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1728661423; bh=FmDNKZ9WHiF4im2ScJHOv4xmHbseTiUosNqW13aFw6A=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=oHWwi/CGg7XWIWRzWHZxfepa2Km6smQbSwGag2KG/LnE+8YCUns18iUwq1wnX44Pq 3mUKyZumAY+fOVtEm8S0skEJuqcnK9o9t5ta/Ox9jvOwCAJR2QDV/5BwQhXzF4jF+E NH4UAuYoswtsb62fXtHENwJa2uAUTPXfdiA4Sffh91x8vsH6/aYuNn1TemkA7cTn9V rni5BWRcOkTlq3mwAWvq6jLMi9HiQQePHLQz101DGcjrQDMzr/177ooMsh5QRNhZy5 3u2kXwqucpgyvfkEnYcGk1EvhEd3Gv+Pqh0Xobz1kNjuPanTXxScz1iV/34dxPwmfd QrJS2KP4eHJkQ== From: Christian Brauner Date: Fri, 11 Oct 2024 17:43:35 +0200 Subject: [PATCH RFC 1/3] ovl: specify layers via file descriptors Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241011-work-overlayfs-v1-1-e34243841279@kernel.org> References: <20241011-work-overlayfs-v1-0-e34243841279@kernel.org> In-Reply-To: <20241011-work-overlayfs-v1-0-e34243841279@kernel.org> To: Miklos Szeredi , Amir Goldstein Cc: Josef Bacik , linux-fsdevel@vger.kernel.org, linux-unionfs@vger.kernel.org, Christian Brauner X-Mailer: b4 0.15-dev-2a633 X-Developer-Signature: v=1; a=openpgp-sha256; l=7107; i=brauner@kernel.org; h=from:subject:message-id; bh=FmDNKZ9WHiF4im2ScJHOv4xmHbseTiUosNqW13aFw6A=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMaRzuq8pnZE5n1Nk6gth6VsWkzZ1WZcb5Vj1/itTmyN80 1inkHdNRykLgxgXg6yYIotDu0m43HKeis1GmRowc1iZQIYwcHEKwERK+xgZvlT3tu2ILT+XdSpR YWGkzPYV03iW38otsQ4Unm/amRxlwMjQIrRv74qI6Fzmh9t8gw323a9XFCvedHATo0a7/JO7/4I YAA== X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Currently overlayfs only allows specifying layers through path names. This is inconvenient for users such as systemd that want to assemble an overlayfs mount purely based on file descriptors. This introduces the new mount options: lowerdir_fd+ datadir_fd+ upperdir_fd workdir_fd which can be used as follows: fsconfig(fd_overlay, FSCONFIG_SET_FD, "upperdir_fd+", NULL, fd_upper); fsconfig(fd_overlay, FSCONFIG_SET_FD, "workdir_fd+", NULL, fd_work); fsconfig(fd_overlay, FSCONFIG_SET_FD, "lowerdir_fd+", NULL, fd_lower1); fsconfig(fd_overlay, FSCONFIG_SET_FD, "lowerdir_fd+", NULL, fd_lower2); Since the mount api doesn't allow overloading of mount option parameters (except for strings and flags). Making this work for arbitrary parameters would be quite ugly or file descriptors would have to be special cased. Neither is very appealing. I do prefer the *_fd mount options because they aren't ambiguous. Signed-off-by: Christian Brauner --- fs/overlayfs/params.c | 132 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 109 insertions(+), 23 deletions(-) diff --git a/fs/overlayfs/params.c b/fs/overlayfs/params.c index e42546c6c5dfbea930414856d791e3e4424a999e..2da7f231401ef034bb62d72a5d34f4a7e9179f8b 100644 --- a/fs/overlayfs/params.c +++ b/fs/overlayfs/params.c @@ -59,6 +59,10 @@ enum ovl_opt { Opt_metacopy, Opt_verity, Opt_volatile, + Opt_lowerdir_add_fd, + Opt_datadir_add_fd, + Opt_upperdir_fd, + Opt_workdir_fd, }; static const struct constant_table ovl_parameter_bool[] = { @@ -155,6 +159,10 @@ const struct fs_parameter_spec ovl_parameter_spec[] = { fsparam_enum("metacopy", Opt_metacopy, ovl_parameter_bool), fsparam_enum("verity", Opt_verity, ovl_parameter_verity), fsparam_flag("volatile", Opt_volatile), + fsparam_fd("lowerdir_fd+", Opt_lowerdir_add_fd), + fsparam_fd("datadir_fd+", Opt_datadir_add_fd), + fsparam_fd("upperdir_fd", Opt_upperdir_fd), + fsparam_fd("workdir_fd", Opt_workdir_fd), {} }; @@ -343,19 +351,27 @@ static void ovl_add_layer(struct fs_context *fc, enum ovl_opt layer, switch (layer) { case Opt_workdir: + fallthrough; + case Opt_workdir_fd: swap(config->workdir, *pname); swap(ctx->work, *path); break; case Opt_upperdir: + fallthrough; + case Opt_upperdir_fd: swap(config->upperdir, *pname); swap(ctx->upper, *path); break; case Opt_datadir_add: + fallthrough; + case Opt_datadir_add_fd: ctx->nr_data++; fallthrough; case Opt_lowerdir: fallthrough; case Opt_lowerdir_add: + fallthrough; + case Opt_lowerdir_add_fd: WARN_ON(ctx->nr >= ctx->capacity); l = &ctx->lower[ctx->nr++]; memset(l, 0, sizeof(*l)); @@ -367,43 +383,96 @@ static void ovl_add_layer(struct fs_context *fc, enum ovl_opt layer, } } -static int ovl_parse_layer(struct fs_context *fc, const char *layer_name, enum ovl_opt layer) +static inline bool is_upper_layer(enum ovl_opt layer) { - char *name = kstrdup(layer_name, GFP_KERNEL); - bool upper = (layer == Opt_upperdir || layer == Opt_workdir); - struct path path; - int err; + return layer == Opt_upperdir || layer == Opt_upperdir_fd || + layer == Opt_workdir || layer == Opt_workdir_fd; +} + +static inline bool is_layer_fd(enum ovl_opt layer) +{ + return layer == Opt_upperdir_fd || layer == Opt_workdir_fd || + layer == Opt_lowerdir_add_fd || layer == Opt_datadir_add_fd; +} + +/* Handle non-file descriptor-based layer options that require path lookup. */ +static inline int ovl_kern_path(const char *layer_name, struct path *layer_path, + enum ovl_opt layer) +{ + switch (layer) { + case Opt_upperdir: + fallthrough; + case Opt_workdir: + fallthrough; + case Opt_lowerdir: + return ovl_mount_dir(layer_name, layer_path); + case Opt_lowerdir_add: + fallthrough; + case Opt_datadir_add: + return ovl_mount_dir_noesc(layer_name, layer_path); + default: + WARN_ON_ONCE(true); + return -EINVAL; + } + + return 0; +} + +static int ovl_do_parse_layer(struct fs_context *fc, const char *layer_name, + struct path *layer_path, enum ovl_opt layer) +{ + char *name __free(kfree) = kstrdup(layer_name, GFP_KERNEL); + bool upper; + int err = 0; if (!name) return -ENOMEM; - if (upper || layer == Opt_lowerdir) - err = ovl_mount_dir(name, &path); - else - err = ovl_mount_dir_noesc(name, &path); - if (err) - goto out_free; - - err = ovl_mount_dir_check(fc, &path, layer, name, upper); + upper = is_upper_layer(layer); + err = ovl_mount_dir_check(fc, layer_path, layer, name, upper); if (err) - goto out_put; + return err; if (!upper) { err = ovl_ctx_realloc_lower(fc); if (err) - goto out_put; + return err; } /* Store the user provided path string in ctx to show in mountinfo */ - ovl_add_layer(fc, layer, &path, &name); - -out_put: - path_put(&path); -out_free: - kfree(name); + ovl_add_layer(fc, layer, layer_path, &name); return err; } +static int ovl_parse_layer(struct fs_context *fc, struct fs_parameter *param, + enum ovl_opt layer) +{ + struct path path __free(path_put) = {}; + char *buf __free(kfree) = NULL; + char *layer_name; + int err = 0; + + if (is_layer_fd(layer)) { + buf = kmalloc(PATH_MAX, GFP_KERNEL_ACCOUNT); + if (!buf) + return -ENOMEM; + + path = param->file->f_path; + path_get(&path); + + layer_name = d_path(&path, buf, PATH_MAX); + if (IS_ERR(layer_name)) + return PTR_ERR(layer_name); + } else { + layer_name = param->string; + err = ovl_kern_path(layer_name, &path, layer); + } + if (err) + return err; + + return ovl_do_parse_layer(fc, layer_name, &path, layer); +} + static void ovl_reset_lowerdirs(struct ovl_fs_context *ctx) { struct ovl_fs_context_layer *l = ctx->lower; @@ -474,7 +543,13 @@ static int ovl_parse_param_lowerdir(const char *name, struct fs_context *fc) iter = dup; for (nr = 0; nr < nr_lower; nr++) { - err = ovl_parse_layer(fc, iter, Opt_lowerdir); + struct path path __free(path_put) = {}; + + err = ovl_kern_path(iter, &path, Opt_lowerdir); + if (err) + goto out_err; + + err = ovl_do_parse_layer(fc, iter, &path, Opt_lowerdir); if (err) goto out_err; @@ -552,10 +627,21 @@ static int ovl_parse_param(struct fs_context *fc, struct fs_parameter *param) err = ovl_parse_param_lowerdir(param->string, fc); break; case Opt_lowerdir_add: + fallthrough; case Opt_datadir_add: + fallthrough; case Opt_upperdir: + fallthrough; case Opt_workdir: - err = ovl_parse_layer(fc, param->string, opt); + fallthrough; + case Opt_lowerdir_add_fd: + fallthrough; + case Opt_datadir_add_fd: + fallthrough; + case Opt_upperdir_fd: + fallthrough; + case Opt_workdir_fd: + err = ovl_parse_layer(fc, param, opt); break; case Opt_default_permissions: config->default_permissions = true; From patchwork Fri Oct 11 15:43:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 13832690 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D165B188CB1; Fri, 11 Oct 2024 15:43:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728661425; cv=none; b=OyYLmxLKTYeIHMExaGe5wR0li9ljvZ65RNjXDftlrLLKuIB1WQ2GJucrNn5GVC0wvDZchGmhL60b4grQ8dYtMyVSmBdwoeskD42kjyLzRDCgbiFDM/DbfxeEQDWTBEP7YHSBNVA/hiaXAlcovR9Q2aBRSY2+Hp7hyZx7M9ufUQ8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728661425; c=relaxed/simple; bh=KRWLagaS4YguCRS2vcI21OA10tbn8Q0Kj2MV+AwTbjI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=DCXxAWoNoIDMVrIoY5AKK8BL/WmcS2UStggcJvLHQ+LrMbIE9GbBtQ754FwqpPmrl4bg8ZulqucsarFGlEcYFzMxvLcGJJ8wZkya7tP9NHaZcEDvopnDGRIzDY3ME4MP1Ed57EWQPQKI9JYqMSSvIR4741ws/B+QpTG00vc6v7E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ly1NKGef; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ly1NKGef" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1A1C7C4CECE; Fri, 11 Oct 2024 15:43:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1728661425; bh=KRWLagaS4YguCRS2vcI21OA10tbn8Q0Kj2MV+AwTbjI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=ly1NKGef4G0L8o03BqQ7sOla1D8XAYJ9k1H2z6dYUCEft3u5bt5GAoeyul8ue/0tE SoO3KDaNXkxXQ0HBHBZDse+J6BEnqe7zuOl1SNywtEQqaYWxi/EHBInk1NHbdkMxhQ I9cjZHpaJJvSHc0LIs67TN6X1+jS3a0rTKn3D0eBoVVJJcq/JO8PzQJAyVabelnUof g4LTleKbbMU461uRfW0W9lVZjOph5yoLMCjr9JbMQabNfed9YXJMHMWbeE1thCoEFF 9xsABc2Kf6y7TAPMejvE/jsSu0j5f3wuksyUYLJFZl+/tAkWEM92/OzOdIKPgzYrOE UwESOuSw32Ydw== From: Christian Brauner Date: Fri, 11 Oct 2024 17:43:36 +0200 Subject: [PATCH RFC 2/3] selftests: use shared header Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241011-work-overlayfs-v1-2-e34243841279@kernel.org> References: <20241011-work-overlayfs-v1-0-e34243841279@kernel.org> In-Reply-To: <20241011-work-overlayfs-v1-0-e34243841279@kernel.org> To: Miklos Szeredi , Amir Goldstein Cc: Josef Bacik , linux-fsdevel@vger.kernel.org, linux-unionfs@vger.kernel.org, Christian Brauner X-Mailer: b4 0.15-dev-2a633 X-Developer-Signature: v=1; a=openpgp-sha256; l=3280; i=brauner@kernel.org; h=from:subject:message-id; bh=KRWLagaS4YguCRS2vcI21OA10tbn8Q0Kj2MV+AwTbjI=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMaRzuq/pL/7J68/2d9Hctx+1Iw9xlG2JWFNf49G/o2WSd W25j35iRykLgxgXg6yYIotDu0m43HKeis1GmRowc1iZQIYwcHEKwEQubGD4H/G0207soYv95Nl8 TN33FZ7Zf+MMFNQTdHok5vNj2evKKYwMB+7dmqS4aJNDVtrz5e3Nczne9E+6ulCs68VU9/maQSu 62QE= X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 So that we don't have to redefine the same system calls over and over. Signed-off-by: Christian Brauner --- .../selftests/filesystems/overlayfs/dev_in_maps.c | 27 +------------- .../selftests/filesystems/overlayfs/wrappers.h | 43 ++++++++++++++++++++++ 2 files changed, 44 insertions(+), 26 deletions(-) diff --git a/tools/testing/selftests/filesystems/overlayfs/dev_in_maps.c b/tools/testing/selftests/filesystems/overlayfs/dev_in_maps.c index 2862aae58b79acbe175ab6b36b42798bb99a2225..3b796264223f81fc753d0adaeccc04077023520b 100644 --- a/tools/testing/selftests/filesystems/overlayfs/dev_in_maps.c +++ b/tools/testing/selftests/filesystems/overlayfs/dev_in_maps.c @@ -17,32 +17,7 @@ #include "../../kselftest.h" #include "log.h" - -static int sys_fsopen(const char *fsname, unsigned int flags) -{ - return syscall(__NR_fsopen, fsname, flags); -} - -static int sys_fsconfig(int fd, unsigned int cmd, const char *key, const char *value, int aux) -{ - return syscall(__NR_fsconfig, fd, cmd, key, value, aux); -} - -static int sys_fsmount(int fd, unsigned int flags, unsigned int attr_flags) -{ - return syscall(__NR_fsmount, fd, flags, attr_flags); -} -static int sys_mount(const char *src, const char *tgt, const char *fst, - unsigned long flags, const void *data) -{ - return syscall(__NR_mount, src, tgt, fst, flags, data); -} -static int sys_move_mount(int from_dfd, const char *from_pathname, - int to_dfd, const char *to_pathname, - unsigned int flags) -{ - return syscall(__NR_move_mount, from_dfd, from_pathname, to_dfd, to_pathname, flags); -} +#include "wrappers.h" static long get_file_dev_and_inode(void *addr, struct statx *stx) { diff --git a/tools/testing/selftests/filesystems/overlayfs/wrappers.h b/tools/testing/selftests/filesystems/overlayfs/wrappers.h new file mode 100644 index 0000000000000000000000000000000000000000..4f99e10f7f018fd9a7be5263f68d34807da4c53c --- /dev/null +++ b/tools/testing/selftests/filesystems/overlayfs/wrappers.h @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: GPL-2.0 +// +#ifndef __SELFTEST_OVERLAYFS_WRAPPERS_H__ +#define __SELFTEST_OVERLAYFS_WRAPPERS_H__ + +#define _GNU_SOURCE + +#include +#include +#include + +static inline int sys_fsopen(const char *fsname, unsigned int flags) +{ + return syscall(__NR_fsopen, fsname, flags); +} + +static inline int sys_fsconfig(int fd, unsigned int cmd, const char *key, + const char *value, int aux) +{ + return syscall(__NR_fsconfig, fd, cmd, key, value, aux); +} + +static inline int sys_fsmount(int fd, unsigned int flags, + unsigned int attr_flags) +{ + return syscall(__NR_fsmount, fd, flags, attr_flags); +} + +static inline int sys_mount(const char *src, const char *tgt, const char *fst, + unsigned long flags, const void *data) +{ + return syscall(__NR_mount, src, tgt, fst, flags, data); +} + +static inline int sys_move_mount(int from_dfd, const char *from_pathname, + int to_dfd, const char *to_pathname, + unsigned int flags) +{ + return syscall(__NR_move_mount, from_dfd, from_pathname, to_dfd, + to_pathname, flags); +} + +#endif From patchwork Fri Oct 11 15:43:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 13832691 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 79214188CB1; Fri, 11 Oct 2024 15:43:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728661427; cv=none; b=q7LEPFVqfTR7bEbTZouymETJhb3pyPmBusMgejOF/5THoRi8vGoAoD1Gd/vp0SDGBadB+Ybm6L9qVy8eRarTi0/igyqWuqNh7tn45iYIgZLw7L+nEUzxxk/T/YDealioRtB49aV6uf3XzPm4qt/+e6W5CC11MsdzM5gH6gZTgK8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728661427; c=relaxed/simple; bh=bCbit3F4y36FfZnMyApzouUE8QDKB2Nl49dJAtQVd7I=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ctnV3k9RdpFgJkLmPZ0dlnjDCAHvUcvqFvRE5Myl0y5fcjQ+WaBh8cc0DI/11sKhJWMsysdBDKZeBC1vjOnSHYCK4hM+Atklg64dX6M2tp0LKnY7TVwsjQ2SjMsobkzSFJnUp45RaT27YSm8yCE0WdDILn1tezMFw1NYs+rGuSo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=uokheuNX; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="uokheuNX" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C7AB6C4CECF; Fri, 11 Oct 2024 15:43:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1728661427; bh=bCbit3F4y36FfZnMyApzouUE8QDKB2Nl49dJAtQVd7I=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=uokheuNXxSOCalqh5xhh+QrAxqCu87HSbNEerh1iiZxlRcTMhlfssxqePJbPuxcRE XBYdWKqeJ+XR1Fqi9MXJTqkjJ5MXgnfL9eNqlomrPCGJzzwmbR5Im13zxEUKHOrPFc AGWmyEgRe91LDWWu0YfVYQ0xZ6HSINfPLQe9J+QTUDSu8iqvtrFitRYOGJ+EJmkPHl +3RuH/CFZTzvC/B+JV+85SgFPHqDKDF6o3pf+1c3Ef/rb2yWwZaNbl8LuClqJo27ZD 8do6aemilpUGMhnCutWY+becxNswknudNMxT+jr5ZexPY3kePcRC1samOM9a6RQ4St Vy/KIK4RaB/Kw== From: Christian Brauner Date: Fri, 11 Oct 2024 17:43:37 +0200 Subject: [PATCH RFC 3/3] selftests: add overlayfs fd mounting selftests Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241011-work-overlayfs-v1-3-e34243841279@kernel.org> References: <20241011-work-overlayfs-v1-0-e34243841279@kernel.org> In-Reply-To: <20241011-work-overlayfs-v1-0-e34243841279@kernel.org> To: Miklos Szeredi , Amir Goldstein Cc: Josef Bacik , linux-fsdevel@vger.kernel.org, linux-unionfs@vger.kernel.org, Christian Brauner X-Mailer: b4 0.15-dev-2a633 X-Developer-Signature: v=1; a=openpgp-sha256; l=6371; i=brauner@kernel.org; h=from:subject:message-id; bh=bCbit3F4y36FfZnMyApzouUE8QDKB2Nl49dJAtQVd7I=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMaRzuq/5LsCS9Pq1z5mfXx7tD6nW4Hq8NT6nz/ryy+jaV WvqHr516ChlYRDjYpAVU2RxaDcJl1vOU7HZKFMDZg4rE8gQBi5OAZgI/0OG3yxfiyM6Jhh/udp+ wnBByMKHWoYxAtXWZ6t2WdXnM/+Jv87wv2ja1iWz2rXvnTVYcXvTL+f996zSFzBaXGx8oHv8tK9 PAisA X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Signed-off-by: Christian Brauner --- .../selftests/filesystems/overlayfs/.gitignore | 1 + .../selftests/filesystems/overlayfs/Makefile | 2 +- .../filesystems/overlayfs/set_layers_via_fds.c | 122 +++++++++++++++++++++ .../selftests/filesystems/overlayfs/wrappers.h | 4 + 4 files changed, 128 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/filesystems/overlayfs/.gitignore b/tools/testing/selftests/filesystems/overlayfs/.gitignore index 52ae618fdd980ee22424d35d79f077077b132401..e23a18c8b37f2cdbb121496b1df1faffd729ad79 100644 --- a/tools/testing/selftests/filesystems/overlayfs/.gitignore +++ b/tools/testing/selftests/filesystems/overlayfs/.gitignore @@ -1,2 +1,3 @@ # SPDX-License-Identifier: GPL-2.0-only dev_in_maps +set_layers_via_fds diff --git a/tools/testing/selftests/filesystems/overlayfs/Makefile b/tools/testing/selftests/filesystems/overlayfs/Makefile index 56b2b48a765b1d6706faee14616597ed0315f267..e8d1adb021af44588dd7af1049de66833bb584ce 100644 --- a/tools/testing/selftests/filesystems/overlayfs/Makefile +++ b/tools/testing/selftests/filesystems/overlayfs/Makefile @@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 -TEST_GEN_PROGS := dev_in_maps +TEST_GEN_PROGS := dev_in_maps set_layers_via_fds CFLAGS := -Wall -Werror diff --git a/tools/testing/selftests/filesystems/overlayfs/set_layers_via_fds.c b/tools/testing/selftests/filesystems/overlayfs/set_layers_via_fds.c new file mode 100644 index 0000000000000000000000000000000000000000..1796da8c2350f5063172a7cd591e5324f87a4c38 --- /dev/null +++ b/tools/testing/selftests/filesystems/overlayfs/set_layers_via_fds.c @@ -0,0 +1,122 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE +#define __SANE_USERSPACE_TYPES__ // Use ll64 + +#include +#include +#include +#include +#include +#include +#include + +#include "../../kselftest_harness.h" +#include "log.h" +#include "wrappers.h" + +FIXTURE(set_layers_via_fds) { +}; + +FIXTURE_SETUP(set_layers_via_fds) +{ + ASSERT_EQ(mkdir("/set_layers_via_fds", 0755), 0); +} + +FIXTURE_TEARDOWN(set_layers_via_fds) +{ + umount2("/set_layers_via_fds", 0); + ASSERT_EQ(rmdir("/set_layers_via_fds"), 0); +} + +TEST_F(set_layers_via_fds, set_layers_via_fds) +{ + int fd_context, fd_tmpfs, fd_overlay; + int layer_fds[5] = { -EBADF, -EBADF, -EBADF, -EBADF, -EBADF }; + bool layers_found[5] = { false, false, false, false, false }; + size_t len = 0; + char *line = NULL; + FILE *f_mountinfo; + + ASSERT_EQ(unshare(CLONE_NEWNS), 0); + ASSERT_EQ(sys_mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL), 0); + + fd_context = sys_fsopen("tmpfs", 0); + ASSERT_GE(fd_context, 0); + + ASSERT_EQ(sys_fsconfig(fd_context, FSCONFIG_CMD_CREATE, NULL, NULL, 0), 0); + fd_tmpfs = sys_fsmount(fd_context, 0, 0); + ASSERT_GE(fd_tmpfs, 0); + ASSERT_EQ(close(fd_context), 0); + + ASSERT_EQ(mkdirat(fd_tmpfs, "w", 0755), 0); + ASSERT_EQ(mkdirat(fd_tmpfs, "u", 0755), 0); + ASSERT_EQ(mkdirat(fd_tmpfs, "l1", 0755), 0); + ASSERT_EQ(mkdirat(fd_tmpfs, "l2", 0755), 0); + ASSERT_EQ(mkdirat(fd_tmpfs, "l3", 0755), 0); + + layer_fds[0] = openat(fd_tmpfs, "w", O_DIRECTORY); + ASSERT_GE(layer_fds[0], 0); + + layer_fds[1] = openat(fd_tmpfs, "u", O_DIRECTORY); + ASSERT_GE(layer_fds[1], 0); + + layer_fds[2] = openat(fd_tmpfs, "l1", O_DIRECTORY); + ASSERT_GE(layer_fds[2], 0); + + layer_fds[3] = openat(fd_tmpfs, "l2", O_DIRECTORY); + ASSERT_GE(layer_fds[3], 0); + + layer_fds[4] = openat(fd_tmpfs, "l3", O_DIRECTORY); + ASSERT_GE(layer_fds[4], 0); + + ASSERT_EQ(sys_move_mount(fd_tmpfs, "", -EBADF, "/tmp", MOVE_MOUNT_F_EMPTY_PATH), 0); + ASSERT_EQ(close(fd_tmpfs), 0); + + fd_context = sys_fsopen("overlay", 0); + ASSERT_GE(fd_context, 0); + + ASSERT_NE(sys_fsconfig(fd_context, FSCONFIG_SET_FD, "lowerdir_fd", NULL, layer_fds[2]), 0); + + ASSERT_EQ(sys_fsconfig(fd_context, FSCONFIG_SET_FD, "workdir_fd", NULL, layer_fds[0]), 0); + ASSERT_EQ(sys_fsconfig(fd_context, FSCONFIG_SET_FD, "upperdir_fd", NULL, layer_fds[1]), 0); + ASSERT_EQ(sys_fsconfig(fd_context, FSCONFIG_SET_FD, "lowerdir_fd+", NULL, layer_fds[2]), 0); + ASSERT_EQ(sys_fsconfig(fd_context, FSCONFIG_SET_FD, "lowerdir_fd+", NULL, layer_fds[3]), 0); + ASSERT_EQ(sys_fsconfig(fd_context, FSCONFIG_SET_FD, "lowerdir_fd+", NULL, layer_fds[4]), 0); + + ASSERT_EQ(sys_fsconfig(fd_context, FSCONFIG_CMD_CREATE, NULL, NULL, 0), 0); + + fd_overlay = sys_fsmount(fd_context, 0, 0); + ASSERT_GE(fd_overlay, 0); + + ASSERT_EQ(sys_move_mount(fd_overlay, "", -EBADF, "/set_layers_via_fds", MOVE_MOUNT_F_EMPTY_PATH), 0); + + f_mountinfo = fopen("/proc/self/mountinfo", "r"); + ASSERT_NE(f_mountinfo, NULL); + + while (getline(&line, &len, f_mountinfo) != -1) { + char *haystack = line; + + if (strstr(haystack, "workdir=/tmp/w")) + layers_found[0] = true; + if (strstr(haystack, "upperdir=/tmp/u")) + layers_found[1] = true; + if (strstr(haystack, "lowerdir+=/tmp/l1")) + layers_found[2] = true; + if (strstr(haystack, "lowerdir+=/tmp/l2")) + layers_found[3] = true; + if (strstr(haystack, "lowerdir+=/tmp/l3")) + layers_found[4] = true; + } + free(line); + + for (int i = 0; i < 5; i++) { + ASSERT_EQ(layers_found[i], true); + ASSERT_EQ(close(layer_fds[i]), 0); + } + + ASSERT_EQ(close(fd_context), 0); + ASSERT_EQ(close(fd_overlay), 0); + ASSERT_EQ(fclose(f_mountinfo), 0); +} + +TEST_HARNESS_MAIN diff --git a/tools/testing/selftests/filesystems/overlayfs/wrappers.h b/tools/testing/selftests/filesystems/overlayfs/wrappers.h index 4f99e10f7f018fd9a7be5263f68d34807da4c53c..071b95fd2ac0ad7b02d90e8e89df73fd27be69c3 100644 --- a/tools/testing/selftests/filesystems/overlayfs/wrappers.h +++ b/tools/testing/selftests/filesystems/overlayfs/wrappers.h @@ -32,6 +32,10 @@ static inline int sys_mount(const char *src, const char *tgt, const char *fst, return syscall(__NR_mount, src, tgt, fst, flags, data); } +#ifndef MOVE_MOUNT_F_EMPTY_PATH +#define MOVE_MOUNT_F_EMPTY_PATH 0x00000004 /* Empty from path permitted */ +#endif + static inline int sys_move_mount(int from_dfd, const char *from_pathname, int to_dfd, const char *to_pathname, unsigned int flags)