From patchwork Fri Jun 7 14:55:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 13690303 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 3EAB61990D9 for ; Fri, 7 Jun 2024 14:55:53 +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=1717772154; cv=none; b=GgyDcAebp8QaIbWCe8pdCyfHzQLv95W4osrZPOcYkUGKC3QWohQ6E8TwJdyBL+K0/SuVbyYkDYm63iQ2sOnaOaerEVT4vJc/XpgFhnjua6g6ZQULZWgoDV35r/mVbrNS7iDqdLq1aZNh1GnCmZxOGuH685bAG/fcK4bMu4Z+fzE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717772154; c=relaxed/simple; bh=XT752m2ZpER1NkGIF7sWtnOyCyqQcjmj71FGb9XAVz0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=J1LwrEWc9UyuAqsWv27HNEg2PsM/1DtOUeuv45f+9mbUwSwuT0bz1m3ItN35XbG1uEjqzT+lo/ZuvsXWKO4K7s0n8EkWiT8N+/pmySEoikLIgykJbAzTpLr84+nUYLPICjzMc97SMLokM5HJZafDZeXuy6X9d/s4PPiIQzoSzTQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=eyvXdza5; 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="eyvXdza5" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BDD5CC3277B; Fri, 7 Jun 2024 14:55:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717772153; bh=XT752m2ZpER1NkGIF7sWtnOyCyqQcjmj71FGb9XAVz0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=eyvXdza5zRy4l/W0ptzW7uIUSI6XCqRNpoQTtARWLICHhr/B+koVvigKkWzxmL+ax 88YJOrvpYFujFxlkS6v+KWLBtEupO5b9yqeta7YTyt7K0LkwbxEPLO9rWBC7EwjvOR qqazSQUoJ+lkU0Yt/cDDptWD15bAuskoeeq7CpHUNaCTC1S0DQFhMXrre64JL41dtN 3FLsf/8uSHLD7T0eZJ2gk9hVTGB92fVQD6JCiXsrlxwtf1GaMxF6BT9uZHesoScFnG IBMaOU3VBxbhMVPR68KahPcK/qS+rTdB/hJ5krBbxVmMXB7SU7dCDP73Al4jyKq3aJ XhsT/ELFSChsg== From: Christian Brauner Date: Fri, 07 Jun 2024 16:55:34 +0200 Subject: [PATCH 1/4] fs: use semaphore gard in listmount() Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240607-vfs-listmount-reverse-v1-1-7877a2bfa5e5@kernel.org> References: <20240607-vfs-listmount-reverse-v1-0-7877a2bfa5e5@kernel.org> In-Reply-To: <20240607-vfs-listmount-reverse-v1-0-7877a2bfa5e5@kernel.org> To: linux-fsdevel@vger.kernel.org Cc: Miklos Szeredi , Karel Zak , Christian Brauner X-Mailer: b4 0.14-dev-2ee9f X-Developer-Signature: v=1; a=openpgp-sha256; l=923; i=brauner@kernel.org; h=from:subject:message-id; bh=XT752m2ZpER1NkGIF7sWtnOyCyqQcjmj71FGb9XAVz0=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMaQly5cxHNqz3CJ31v/vdTM53xbm1Xn63/MznrtSRejNe 6fnHc+NO0pZGMS4GGTFFFkc2k3C5ZbzVGw2ytSAmcPKBDaEi1MAJqK1nuGfUquOvdmx1fcXX67o ehrrFBStdlRWuebAbk0D3YS8uYt+MPxTWyTwWu/GrF8RH7M5Y2vNU/gv8srUHtbKLowUFeEsF2U AAA== X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Instead of open-coding the locking use a guard. Signed-off-by: Christian Brauner --- fs/namespace.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fs/namespace.c b/fs/namespace.c index 5a51315c6678..72c6e884728b 100644 --- a/fs/namespace.c +++ b/fs/namespace.c @@ -5106,7 +5106,8 @@ SYSCALL_DEFINE4(listmount, const struct mnt_id_req __user *, req, u64 __user *, mnt_parent_id = kreq.mnt_id; last_mnt_id = kreq.param; - down_read(&namespace_sem); + guard(rwsem_read)(&namespace_sem); + get_fs_root(current->fs, &root); if (mnt_parent_id == LSMT_ROOT) { orig = root; @@ -5125,7 +5126,6 @@ SYSCALL_DEFINE4(listmount, const struct mnt_id_req __user *, req, u64 __user *, ret = do_listmount(first, &orig, mnt_parent_id, mnt_ids, nr_mnt_ids, &root); err: path_put(&root); - up_read(&namespace_sem); return ret; } From patchwork Fri Jun 7 14:55: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: 13690304 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 682831990D9 for ; Fri, 7 Jun 2024 14:55:55 +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=1717772155; cv=none; b=KabeQhyayzpIGuHwbasZDf1nVqDifJGvHd2Gtdt4fFvZmERUMgk99acjo7Gmxo97A1xkTvzhOa1Ca6JmlWpgP6V2kC1mAN07LTrS2fULfhXweUdJ8vG0LaBRmko8IcEff484IXukWHHEgleDmYE4NZWxwvwM/vSYJBcBimOQpkY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717772155; c=relaxed/simple; bh=lXEWnQoKFX8hD3f8kGU3u1jNUSzOr1a9E68qjTBr3Yg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=HLc1xaa5rJHuaxijqx3YYLD4JBSkleDPyX9JRxHReWMU1X4mb/YuUZjilJwnwsx/BpFuuZCsFRwCi1fbmv58uceZyQHLNusggUU1S5gk4yVPgmU2r1c90U0+bTXqv/omu+MvvZfuhVH+5Uu8aLeHnURDpRwZ2kkxSHQrK9yUiEs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=W6ObOs3v; 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="W6ObOs3v" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4553FC2BBFC; Fri, 7 Jun 2024 14:55:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717772155; bh=lXEWnQoKFX8hD3f8kGU3u1jNUSzOr1a9E68qjTBr3Yg=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=W6ObOs3vQyT5bHQlaSYmsojbf+1QUzvOkJB0n03ijh9oMpDBsnOYJ4AwHdOifq61n qWK1T/icgzxBk8M44lbxzV8lTpiY9D4XzTv1ncVwvFNgySLru87nGqOnClycR0BdZ0 Uht7nm3TmyYRykLhcJ8Q/ZmwYTAbny3Xn+jtxsy9XFPl5l3XxZDtmzs2/cuLAaKl2F Vr9tYORlrrU3/v4VhEDjFK5ndf0NizpU1F9GsE7AiRXt1oShZ3PBdV2LvEUiBB76Ax hHtZYmHKnDjXqdMWDwVt1uy9fWpOTEO32lNV8a49XSCWoCr3SxT1IeESD65tmfs7ih 2m5w99jO9efPg== From: Christian Brauner Date: Fri, 07 Jun 2024 16:55:35 +0200 Subject: [PATCH 2/4] path: add cleanup helper Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240607-vfs-listmount-reverse-v1-2-7877a2bfa5e5@kernel.org> References: <20240607-vfs-listmount-reverse-v1-0-7877a2bfa5e5@kernel.org> In-Reply-To: <20240607-vfs-listmount-reverse-v1-0-7877a2bfa5e5@kernel.org> To: linux-fsdevel@vger.kernel.org Cc: Miklos Szeredi , Karel Zak , Christian Brauner X-Mailer: b4 0.14-dev-2ee9f X-Developer-Signature: v=1; a=openpgp-sha256; l=983; i=brauner@kernel.org; h=from:subject:message-id; bh=lXEWnQoKFX8hD3f8kGU3u1jNUSzOr1a9E68qjTBr3Yg=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMaQly5c5165p2KT/VvuchmzIBK4Va1M6HJdMc7Z3Mo3j6 DsjZvKno5SFQYyLQVZMkcWh3SRcbjlPxWajTA2YOaxMIEMYuDgFYCLnJzL8ZFRLUbN2MJrvvWnl 2TO3dG/2rXPki9ETZ9StzdQ49u6uA8P/yOBtam8ZrzAunnLqZ+Tm5cfSuRaa9sQ9E9t/a/Mq91N 7OAE= X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Add a simple cleanup helper so we can cleanup struct path easily. No need for any extra machinery. Avoid DEFINE_FREE() as it causes a local copy of struct path to be used. Just rely on path_put() directly called from a cleanup helper. Signed-off-by: Christian Brauner --- include/linux/path.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/include/linux/path.h b/include/linux/path.h index 475225a03d0d..ca073e70decd 100644 --- a/include/linux/path.h +++ b/include/linux/path.h @@ -24,4 +24,13 @@ static inline void path_put_init(struct path *path) *path = (struct path) { }; } +/* + * Cleanup macro for use with __free(path_put). Avoids dereference and + * copying @path unlike DEFINE_FREE(). path_put() will handle the empty + * path correctly just ensure @path is initialized: + * + * struct path path __free(path_put) = {}; + */ +#define __free_path_put path_put + #endif /* _LINUX_PATH_H */ From patchwork Fri Jun 7 14:55: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: 13690305 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 2E72954660 for ; Fri, 7 Jun 2024 14:55:56 +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=1717772157; cv=none; b=OCcLUukHPlWtfLcr+UE65smigGJCSW78u6Gn+pSUDaXCR//KqSOKBNkDMVj74/eOF8R2lupxqSkT9vwAashMgyBEDpn9XICNkegH0zLP72Zk7/JWqj+HFE9bsKnWeGj9luCk2aTFYc24xl4kH5CHLv0dV+g2h73pExdMOzr4Wcc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717772157; c=relaxed/simple; bh=SQAhfliB9A4fi4EiiOt7HGCAdbfU54EMw+aMNpy4EF4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=VawGgGRnLzBPLxOrHuoxY6r9QfiRfh0fhB/pe1T0dNGTnOIEv9IBz5YWX0M/eI5JMeKe15m05ax9JnYsvWTz1SICkeGJ8wr++KXI2xZ1dhV/6n0FW+MrtaYpfRzylFe8Mk533Oe8ASjWEP72lvMl7IBQObQMIbOKWHrgmvTX/HA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EYbYX05r; 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="EYbYX05r" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CB11CC2BBFC; Fri, 7 Jun 2024 14:55:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717772156; bh=SQAhfliB9A4fi4EiiOt7HGCAdbfU54EMw+aMNpy4EF4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=EYbYX05rLGzBk0f+9Qz6fvMqJJfP18RuyTl7yMbpW1cWxuruRxYEpNT7sgEt7aDFb CdMcjoH7mlgAGEfezzxhoBPrW0Kqg77jYwNPHxJb8FCGUQ5g5B5oO4w1Zyy5NaOimb /sDvUqHtPuX2L7K6dbrSraFp5Hw9sOwKy0WkSbXMXn6QAGZ8yy2/QuKdZk8CTsMnfw VCbkyYuGCD0D3NqL4tmGvyqWhl8Q7+diflsBFHlWFFr33tuSdOGFdvZfAs4Tprq1fP 01GDuDZWlTCV+jIOqABv+W0yldcVRxjoTgTWUIxL+J54JYiCHKeMb7tjmoAof3Si6G zvuvH4PCgJPXA== From: Christian Brauner Date: Fri, 07 Jun 2024 16:55:36 +0200 Subject: [PATCH 3/4] fs: simplify error handling Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240607-vfs-listmount-reverse-v1-3-7877a2bfa5e5@kernel.org> References: <20240607-vfs-listmount-reverse-v1-0-7877a2bfa5e5@kernel.org> In-Reply-To: <20240607-vfs-listmount-reverse-v1-0-7877a2bfa5e5@kernel.org> To: linux-fsdevel@vger.kernel.org Cc: Miklos Szeredi , Karel Zak , Christian Brauner X-Mailer: b4 0.14-dev-2ee9f X-Developer-Signature: v=1; a=openpgp-sha256; l=1609; i=brauner@kernel.org; h=from:subject:message-id; bh=SQAhfliB9A4fi4EiiOt7HGCAdbfU54EMw+aMNpy4EF4=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMaQly5fVTbM1n7bCTWcHt8c8niVMD413ZTDOLVAXV59Te 3lnv86HjlIWBjEuBlkxRRaHdpNwueU8FZuNMjVg5rAygQxh4OIUgIncP8Pwzzy7aslGg5uc70+L HgznneK79xX/Z87PNvYl66+aamz8rMzw38d1z2SX23fWLNZorFgQJrV6funj9TyWD12Wv94tM9t KjgcA X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Rely on cleanup helper and simplify error handling Signed-off-by: Christian Brauner --- fs/namespace.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/fs/namespace.c b/fs/namespace.c index 72c6e884728b..507f310dbf33 100644 --- a/fs/namespace.c +++ b/fs/namespace.c @@ -5083,10 +5083,11 @@ static ssize_t do_listmount(struct mount *first, struct path *orig, SYSCALL_DEFINE4(listmount, const struct mnt_id_req __user *, req, u64 __user *, mnt_ids, size_t, nr_mnt_ids, unsigned int, flags) { + struct path root __free(path_put) = {}; struct mnt_namespace *ns = current->nsproxy->mnt_ns; struct mnt_id_req kreq; struct mount *first; - struct path root, orig; + struct path orig; u64 mnt_parent_id, last_mnt_id; const size_t maxcount = (size_t)-1 >> 3; ssize_t ret; @@ -5112,10 +5113,9 @@ SYSCALL_DEFINE4(listmount, const struct mnt_id_req __user *, req, u64 __user *, if (mnt_parent_id == LSMT_ROOT) { orig = root; } else { - ret = -ENOENT; orig.mnt = lookup_mnt_in_ns(mnt_parent_id, ns); if (!orig.mnt) - goto err; + return -ENOENT; orig.dentry = orig.mnt->mnt_root; } if (!last_mnt_id) @@ -5123,10 +5123,7 @@ SYSCALL_DEFINE4(listmount, const struct mnt_id_req __user *, req, u64 __user *, else first = mnt_find_id_at(ns, last_mnt_id + 1); - ret = do_listmount(first, &orig, mnt_parent_id, mnt_ids, nr_mnt_ids, &root); -err: - path_put(&root); - return ret; + return do_listmount(first, &orig, mnt_parent_id, mnt_ids, nr_mnt_ids, &root); } From patchwork Fri Jun 7 14:55: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: 13690306 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 BEE9154660 for ; Fri, 7 Jun 2024 14:55:58 +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=1717772158; cv=none; b=FMmodOXYQUiSZ6XujHrO30qiOChI9lOIyzl9+YbRp6xvGXh90UjdahJXSajxCQ1nAp3q9PJeLwS99f2UxyMIAm282vDAICLeninjP7GiSsXqu3k86LOL8hOSWFP2CSwHQuBdax43L4WsSZxMAUT9JNQYiqzhtQ3fhVNGfWiUVAU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717772158; c=relaxed/simple; bh=g7bulEmSdhtPayCYyl9eLgPHqmNEoIh5/t7WlOUgO5Y=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=TZ11oxv2MLvHdIaHRkdynDG+E0aupXlgevQ4O7oo+MW7pXBTiVhXPYSWWAij6avkRDsmKKRFA+V4vvo2kRVNAEA9csxkhKFG6M0PM2btYDvwAtfJEtby5s05ONGeQSVZC59zkL7rEA0fo1ZZBSID8IzKC2BeIaPsn1D6cTrBYOk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BuRsynMP; 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="BuRsynMP" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 48FDEC32786; Fri, 7 Jun 2024 14:55:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717772158; bh=g7bulEmSdhtPayCYyl9eLgPHqmNEoIh5/t7WlOUgO5Y=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=BuRsynMPS2FI4TQ2JGuJVA3FdH3TwmTBpVKipBVkNHTGKTFoU4brFpNAPP39IcZxR gZU11DsKYyBO4k3XrGeXmnVYTiiQ6CVZy3dfeHFFgzV5mlWWIUERBbqbN6AQDNcs9r MGgjQxfPTdiik5tf3mJlBSEHnMq5ZRUwE9eV2sezuZxnHrE6oVSx3fAxfJ/ChsJv6w ul094eT2fkC/6k7Taeb9MXVVYqzdctYiF8VXGWjUeSW0wvygzX1eC3+nIfSnR5K746 m1kPFBUUqHXLWtk00W/N0ynw2rLRIN2tt1AprWMcifRO3HE06yrrlPXBKmX0NfgVjz NgbAA3nZ10Kqw== From: Christian Brauner Date: Fri, 07 Jun 2024 16:55:37 +0200 Subject: [PATCH 4/4] listmount: allow listing in reverse order Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240607-vfs-listmount-reverse-v1-4-7877a2bfa5e5@kernel.org> References: <20240607-vfs-listmount-reverse-v1-0-7877a2bfa5e5@kernel.org> In-Reply-To: <20240607-vfs-listmount-reverse-v1-0-7877a2bfa5e5@kernel.org> To: linux-fsdevel@vger.kernel.org Cc: Miklos Szeredi , Karel Zak , Christian Brauner X-Mailer: b4 0.14-dev-2ee9f X-Developer-Signature: v=1; a=openpgp-sha256; l=5183; i=brauner@kernel.org; h=from:subject:message-id; bh=g7bulEmSdhtPayCYyl9eLgPHqmNEoIh5/t7WlOUgO5Y=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMaQly5dZMk2rfJJaN3eC0Z+ndrWbP1uabI0u2dLl2Oewt W/vjcb/HaUsDGJcDLJiiiwO7Sbhcst5KjYbZWrAzGFlAhnCwMUpABNZXcbIsEF4afq+aTl3JO7L /9Hd6BTXu4bR7W1jb5zitg4JiyVutgz/HW8bxSj3KZyfGR4cZKL0vbnAY/Z7JQdtwV3B2jILoyS 4AQ== X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 util-linux is about to implement listmount() and statmount() support. Karel requested the ability to scan the mount table in backwards order because that's what libmount currently does in order to get the latest mount first. We currently don't support this in listmount(). Add a new LISTMOUNT_RESERVE flag to allow listing mounts in reverse order. For example, listing all child mounts of /sys without LISTMOUNT_REVERSE gives: /sys/kernel/security @ mnt_id: 4294968369 /sys/fs/cgroup @ mnt_id: 4294968370 /sys/firmware/efi/efivars @ mnt_id: 4294968371 /sys/fs/bpf @ mnt_id: 4294968372 /sys/kernel/tracing @ mnt_id: 4294968373 /sys/kernel/debug @ mnt_id: 4294968374 /sys/fs/fuse/connections @ mnt_id: 4294968375 /sys/kernel/config @ mnt_id: 4294968376 whereas with LISTMOUNT_RESERVE it gives: /sys/kernel/config @ mnt_id: 4294968376 /sys/fs/fuse/connections @ mnt_id: 4294968375 /sys/kernel/debug @ mnt_id: 4294968374 /sys/kernel/tracing @ mnt_id: 4294968373 /sys/fs/bpf @ mnt_id: 4294968372 /sys/firmware/efi/efivars @ mnt_id: 4294968371 /sys/fs/cgroup @ mnt_id: 4294968370 /sys/kernel/security @ mnt_id: 4294968369 Signed-off-by: Christian Brauner --- fs/namespace.c | 62 ++++++++++++++++++++++++++++++++++++++-------- include/uapi/linux/mount.h | 1 + 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/fs/namespace.c b/fs/namespace.c index 507f310dbf33..911c149c7979 100644 --- a/fs/namespace.c +++ b/fs/namespace.c @@ -1448,6 +1448,30 @@ static struct mount *mnt_find_id_at(struct mnt_namespace *ns, u64 mnt_id) return ret; } +/* + * Returns the mount which either has the specified mnt_id, or has the next + * greater id before the specified one. + */ +static struct mount *mnt_find_id_at_reverse(struct mnt_namespace *ns, u64 mnt_id) +{ + struct rb_node *node = ns->mounts.rb_node; + struct mount *ret = NULL; + + while (node) { + struct mount *m = node_to_mount(node); + + if (mnt_id >= m->mnt_id_unique) { + ret = node_to_mount(node); + if (mnt_id == m->mnt_id_unique) + break; + node = node->rb_right; + } else { + node = node->rb_left; + } + } + return ret; +} + #ifdef CONFIG_PROC_FS /* iterator; we want it to have access to namespace_sem, thus here... */ @@ -5042,14 +5066,22 @@ SYSCALL_DEFINE4(statmount, const struct mnt_id_req __user *, req, return ret; } -static struct mount *listmnt_next(struct mount *curr) +static struct mount *listmnt_next(struct mount *curr, bool reverse) { - return node_to_mount(rb_next(&curr->mnt_node)); + struct rb_node *node; + + if (reverse) + node = rb_prev(&curr->mnt_node); + else + node = rb_next(&curr->mnt_node); + + return node_to_mount(node); } static ssize_t do_listmount(struct mount *first, struct path *orig, u64 mnt_parent_id, u64 __user *mnt_ids, - size_t nr_mnt_ids, const struct path *root) + size_t nr_mnt_ids, const struct path *root, + bool reverse) { struct mount *r; ssize_t ret; @@ -5066,7 +5098,7 @@ static ssize_t do_listmount(struct mount *first, struct path *orig, if (ret) return ret; - for (ret = 0, r = first; r && nr_mnt_ids; r = listmnt_next(r)) { + for (ret = 0, r = first; r && nr_mnt_ids; r = listmnt_next(r, reverse)) { if (r->mnt_id_unique == mnt_parent_id) continue; if (!is_path_reachable(r, r->mnt.mnt_root, orig)) @@ -5090,9 +5122,10 @@ SYSCALL_DEFINE4(listmount, const struct mnt_id_req __user *, req, u64 __user *, struct path orig; u64 mnt_parent_id, last_mnt_id; const size_t maxcount = (size_t)-1 >> 3; + bool reverse_order; ssize_t ret; - if (flags) + if (flags & ~LISTMOUNT_REVERSE) return -EINVAL; if (unlikely(nr_mnt_ids > maxcount)) @@ -5118,12 +5151,21 @@ SYSCALL_DEFINE4(listmount, const struct mnt_id_req __user *, req, u64 __user *, return -ENOENT; orig.dentry = orig.mnt->mnt_root; } - if (!last_mnt_id) - first = node_to_mount(rb_first(&ns->mounts)); - else - first = mnt_find_id_at(ns, last_mnt_id + 1); + reverse_order = flags & LISTMOUNT_REVERSE; + if (!last_mnt_id) { + if (reverse_order) + first = node_to_mount(rb_last(&ns->mounts)); + else + first = node_to_mount(rb_first(&ns->mounts)); + } else { + if (reverse_order) + first = mnt_find_id_at_reverse(ns, last_mnt_id - 1); + else + first = mnt_find_id_at(ns, last_mnt_id + 1); + } - return do_listmount(first, &orig, mnt_parent_id, mnt_ids, nr_mnt_ids, &root); + return do_listmount(first, &orig, mnt_parent_id, mnt_ids, nr_mnt_ids, + &root, reverse_order); } diff --git a/include/uapi/linux/mount.h b/include/uapi/linux/mount.h index ad5478dbad00..88d78de1519f 100644 --- a/include/uapi/linux/mount.h +++ b/include/uapi/linux/mount.h @@ -207,5 +207,6 @@ struct mnt_id_req { * Special @mnt_id values that can be passed to listmount */ #define LSMT_ROOT 0xffffffffffffffff /* root mount */ +#define LISTMOUNT_REVERSE (1 << 0) /* List later mounts first */ #endif /* _UAPI_LINUX_MOUNT_H */