From patchwork Thu Sep 22 08:44:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miklos Szeredi X-Patchwork-Id: 12984737 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D46CDC6FA82 for ; Thu, 22 Sep 2022 08:45:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230304AbiIVIpF (ORCPT ); Thu, 22 Sep 2022 04:45:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44484 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230374AbiIVIo5 (ORCPT ); Thu, 22 Sep 2022 04:44:57 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D7D3561B0F for ; Thu, 22 Sep 2022 01:44:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1663836288; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=YUr4vpC7819u5uQSeXZvt7C05pVq5tFuU8XWNKqRjX4=; b=M6VR3Qa2GbM7KTh4yk/6o9LylZvi9qvsstuQRmxOuXRcvp9bAIhFw5K1gLgJyJoqO9kpJa o8ih/a281saS4UIDl56fhxCCPO1RpWU+EZQyKjX7Ye/T9w+RQGRr/FdUmCZE2bzIJ7WFvM jlAgnLVbqfYAjXHdZFkKtUumvKS0szg= Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-27-Dms1aIyoNaWZOzP_t9bsdA-1; Thu, 22 Sep 2022 04:44:47 -0400 X-MC-Unique: Dms1aIyoNaWZOzP_t9bsdA-1 Received: by mail-ej1-f70.google.com with SMTP id nc18-20020a1709071c1200b00780b046fb4eso4207056ejc.22 for ; Thu, 22 Sep 2022 01:44:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=YUr4vpC7819u5uQSeXZvt7C05pVq5tFuU8XWNKqRjX4=; b=aRfObKDAiopq3CwyWQprd+dFLN8xz6UESoh4lcNStR4uYQGDdBgEJMFUSCR/R3y3cj 9leNHahYe2ze9JclgRx01YVqH6V38RVlQxTMSTiqfP198UWx+iFUb86lfhgTOfBHMNI0 gLsbA59yakqQl3TCf0s/OMtxrs1oouGqghTwRSvMtczxtrwynIHb5R0DgMExdXBFl+hs P+UkSyNYdh6ZvfuJOcAqlW6BqzBdiJgLvdD6hsZiwPcrFuHn1W7uq8+zM2ir0AUBXDCw Gwx44hW27+zNpYHiSA/IROZ+++WzvXNLNGqBGI9uTY/UK8cjDpiO0dWH/JcUDbOjpnVg Zo0A== X-Gm-Message-State: ACrzQf0XcKqP5Tdx+khWdkNjbg0QrLUayNpFo08PdAbuB+bKv8r+KDU3 7XrDCNQlNumssvcFmJfZ8F01fFIold/PY9sU+b66pmeqqxPX0BbNaupg3Xvr4sJSqxiYy3c5pcu IhdMdEUPExSXFqQMW+NzA7FJNPVhh33bMzv5GX3GgE3gvHN008rfASuyUoyajnu/7dBJ6euH1ex NLJQ== X-Received: by 2002:a17:907:6d03:b0:780:6fdd:974e with SMTP id sa3-20020a1709076d0300b007806fdd974emr1805595ejc.288.1663836285980; Thu, 22 Sep 2022 01:44:45 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4OoZYLWxyUdTn44c8s/grB0DBwcG1pYYYDnsq3V+vEu6Q2QCgS9MEYEXmep0RmwGFyvOEnxQ== X-Received: by 2002:a17:907:6d03:b0:780:6fdd:974e with SMTP id sa3-20020a1709076d0300b007806fdd974emr1805572ejc.288.1663836285655; Thu, 22 Sep 2022 01:44:45 -0700 (PDT) Received: from miu.piliscsaba.redhat.com (193-226-214-223.pool.digikabel.hu. [193.226.214.223]) by smtp.gmail.com with ESMTPSA id h15-20020a170906718f00b00730b3bdd8d7sm2297942ejk.179.2022.09.22.01.44.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Sep 2022 01:44:45 -0700 (PDT) From: Miklos Szeredi To: linux-fsdevel@vger.kernel.org Cc: Al Viro , Christian Brauner , Amir Goldstein , David Howells , Yu-li Lin , Chirantan Ekbote Subject: [PATCH v4 01/10] vfs: add vfs_tmpfile_open() helper Date: Thu, 22 Sep 2022 10:44:33 +0200 Message-Id: <20220922084442.2401223-2-mszeredi@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220922084442.2401223-1-mszeredi@redhat.com> References: <20220922084442.2401223-1-mszeredi@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org This helper unifies tmpfile creation with opening. Existing vfs_tmpfile() callers outside of fs/namei.c will be converted to using this helper. There are two such callers: cachefile and overlayfs. The cachefiles code currently uses the open_with_fake_path() helper to open the tmpfile, presumably to disable accounting of the open file. Overlayfs uses tmpfile for copy_up, which means these struct file instances will be short lived, hence it doesn't really matter if they are accounted or not. Disable accounting in this helper too, which should be okay for both callers. Add MAY_OPEN permission checking for consistency. Like for create(2) read/write permissions are not checked. Signed-off-by: Miklos Szeredi Reviewed-by: Christian Brauner (Microsoft) --- fs/namei.c | 41 +++++++++++++++++++++++++++++++++++++++++ include/linux/fs.h | 4 ++++ 2 files changed, 45 insertions(+) diff --git a/fs/namei.c b/fs/namei.c index 53b4bc094db2..81c388a813d3 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -3624,6 +3624,47 @@ struct dentry *vfs_tmpfile(struct user_namespace *mnt_userns, } EXPORT_SYMBOL(vfs_tmpfile); +/** + * vfs_tmpfile_open - open a tmpfile for kernel internal use + * @mnt_userns: user namespace of the mount the inode was found from + * @parentpath: path of the base directory + * @mode: mode of the new tmpfile + * @open_flag: flags + * @cred: credentials for open + * + * Create and open a temporary file. The file is not accounted in nr_files, + * hence this is only for kernel internal use, and must not be installed into + * file tables or such. + */ +struct file *vfs_tmpfile_open(struct user_namespace *mnt_userns, + const struct path *parentpath, + umode_t mode, int open_flag, const struct cred *cred) +{ + struct file *file; + int error; + struct path path = { .mnt = parentpath->mnt }; + + path.dentry = vfs_tmpfile(mnt_userns, parentpath->dentry, mode, open_flag); + if (IS_ERR(path.dentry)) + return ERR_CAST(path.dentry); + + error = may_open(mnt_userns, &path, 0, open_flag); + file = ERR_PTR(error); + if (error) + goto out_dput; + + /* + * This relies on the "noaccount" property of fake open, otherwise + * equivalent to dentry_open(). + */ + file = open_with_fake_path(&path, open_flag, d_inode(path.dentry), cred); +out_dput: + dput(path.dentry); + + return file; +} +EXPORT_SYMBOL(vfs_tmpfile_open); + static int do_tmpfile(struct nameidata *nd, unsigned flags, const struct open_flags *op, struct file *file) diff --git a/include/linux/fs.h b/include/linux/fs.h index 9eced4cc286e..15fafda95dd3 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2007,6 +2007,10 @@ static inline int vfs_whiteout(struct user_namespace *mnt_userns, struct dentry *vfs_tmpfile(struct user_namespace *mnt_userns, struct dentry *dentry, umode_t mode, int open_flag); +struct file *vfs_tmpfile_open(struct user_namespace *mnt_userns, + const struct path *parentpath, + umode_t mode, int open_flag, const struct cred *cred); + int vfs_mkobj(struct dentry *, umode_t, int (*f)(struct dentry *, umode_t, void *), void *); From patchwork Thu Sep 22 08:44:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miklos Szeredi X-Patchwork-Id: 12984736 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D257EC6FA8B for ; Thu, 22 Sep 2022 08:45:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229995AbiIVIpE (ORCPT ); Thu, 22 Sep 2022 04:45:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44488 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230378AbiIVIo5 (ORCPT ); Thu, 22 Sep 2022 04:44:57 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3E028A0267 for ; Thu, 22 Sep 2022 01:44:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1663836290; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=x8lUJ3SpIV4mlVs/JuPNx0a+WWSeeA9bZCgVmcay4M8=; b=YQv7Jf3x792gVe8FyvXFdh8UkWCQ88XyPiAWNK0Isu9uMDyxpsmfJWTer27g+Z/aA3KpJZ vcTwZK7ivFplZcCWYaEwQuKsZys22PO4C6trGCgE+ddqSj1XtF1qL+wE9d4Hx7kiposupQ Bs1wsqe8Ktskp12idYYRAmqfFbZhGeA= Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-594-m5vb3ALKNPmz1ZVJtRoG0Q-1; Thu, 22 Sep 2022 04:44:48 -0400 X-MC-Unique: m5vb3ALKNPmz1ZVJtRoG0Q-1 Received: by mail-ed1-f70.google.com with SMTP id e15-20020a056402190f00b0044f41e776a0so6166205edz.0 for ; Thu, 22 Sep 2022 01:44:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=x8lUJ3SpIV4mlVs/JuPNx0a+WWSeeA9bZCgVmcay4M8=; b=iniMokm3bvxnnkCsuB3+cXxfOXz/n1ClWFCpG2CohnPTds6No9VMEZXWomog5RD1NC XKzjIr3cb/TqO0zMwLFeG9QqhXrGTUXnFBGq53LT95LHj5REKtOltybvSMfC2O4pARj1 qbiAFiT3/7BzZz9S8nHSJWp9oxBP2fN8flHJfhOnZP5QJG8Ao0m64G/GcAp9jnwIVXUw tEXKpR0Hk0RVx/nmV1TPtt1dk61YwCBrpb/HjWapFJTQCNXoArDQAz4vq2PZNjztKKh/ 8FclWnX1RYAYQV2hib9MtOIeRPbk8hU3fPP/E04lWaMvMeGkvBzUA7F75YB9ezVZM5Bf hiDg== X-Gm-Message-State: ACrzQf2eXEgoUP0w3DuIAw2DqN7vjq+ES22RsLsEZZJ2bpKXZjl8rc18 kWw/pVmqTJ2/5oIHuWXxVk9sl2MJr/MrlCWNG6X4W3oXZwxUn5CoD8DVrtpiXCT2rFs2GQJTXfT A1Hq02nURfaY09Hs3rYufXDj4sqr7m2yYTCWGDUUh1c0/rMUmQWBOtkJjxwJAoqZsPtgacd4GGa RCFA== X-Received: by 2002:aa7:c382:0:b0:454:9591:79fe with SMTP id k2-20020aa7c382000000b00454959179femr2148300edq.253.1663836286904; Thu, 22 Sep 2022 01:44:46 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6eFjvyOJxrjK/S38FYT1FYsFoor5/NPGXCzHHhjnyD6Ze1SbQt0l1+KG0NK5RrnzdaqH43Jw== X-Received: by 2002:aa7:c382:0:b0:454:9591:79fe with SMTP id k2-20020aa7c382000000b00454959179femr2148282edq.253.1663836286703; Thu, 22 Sep 2022 01:44:46 -0700 (PDT) Received: from miu.piliscsaba.redhat.com (193-226-214-223.pool.digikabel.hu. [193.226.214.223]) by smtp.gmail.com with ESMTPSA id h15-20020a170906718f00b00730b3bdd8d7sm2297942ejk.179.2022.09.22.01.44.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Sep 2022 01:44:46 -0700 (PDT) From: Miklos Szeredi To: linux-fsdevel@vger.kernel.org Cc: Al Viro , Al Viro , Christian Brauner , Amir Goldstein , David Howells , Yu-li Lin , Chirantan Ekbote Subject: [PATCH v4 02/10] hugetlbfs: cleanup mknod and tmpfile Date: Thu, 22 Sep 2022 10:44:34 +0200 Message-Id: <20220922084442.2401223-3-mszeredi@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220922084442.2401223-1-mszeredi@redhat.com> References: <20220922084442.2401223-1-mszeredi@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org From: Al Viro Duplicate the few lines that are shared between hugetlbfs_mknod() and hugetlbfs_tmpfile(). This is a prerequisite for sanely changing the signature of ->tmpfile(). Signed-off-by: Al Viro Reviewed-by: Christian Brauner (Microsoft) Signed-off-by: Miklos Szeredi --- fs/hugetlbfs/inode.c | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index f7a5b5124d8a..0b458beb318c 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -885,33 +885,18 @@ static struct inode *hugetlbfs_get_inode(struct super_block *sb, /* * File creation. Allocate an inode, and we're done.. */ -static int do_hugetlbfs_mknod(struct inode *dir, - struct dentry *dentry, - umode_t mode, - dev_t dev, - bool tmpfile) +static int hugetlbfs_mknod(struct user_namespace *mnt_userns, struct inode *dir, + struct dentry *dentry, umode_t mode, dev_t dev) { struct inode *inode; - int error = -ENOSPC; inode = hugetlbfs_get_inode(dir->i_sb, dir, mode, dev); - if (inode) { - dir->i_ctime = dir->i_mtime = current_time(dir); - if (tmpfile) { - d_tmpfile(dentry, inode); - } else { - d_instantiate(dentry, inode); - dget(dentry);/* Extra count - pin the dentry in core */ - } - error = 0; - } - return error; -} - -static int hugetlbfs_mknod(struct user_namespace *mnt_userns, struct inode *dir, - struct dentry *dentry, umode_t mode, dev_t dev) -{ - return do_hugetlbfs_mknod(dir, dentry, mode, dev, false); + if (!inode) + return -ENOSPC; + dir->i_ctime = dir->i_mtime = current_time(dir); + d_instantiate(dentry, inode); + dget(dentry);/* Extra count - pin the dentry in core */ + return 0; } static int hugetlbfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, @@ -935,7 +920,14 @@ static int hugetlbfs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, struct dentry *dentry, umode_t mode) { - return do_hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0, true); + struct inode *inode; + + inode = hugetlbfs_get_inode(dir->i_sb, dir, mode | S_IFREG, 0); + if (!inode) + return -ENOSPC; + dir->i_ctime = dir->i_mtime = current_time(dir); + d_tmpfile(dentry, inode); + return 0; } static int hugetlbfs_symlink(struct user_namespace *mnt_userns, From patchwork Thu Sep 22 08:44:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miklos Szeredi X-Patchwork-Id: 12984738 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2A1D0C6FA90 for ; Thu, 22 Sep 2022 08:45:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229603AbiIVIpH (ORCPT ); Thu, 22 Sep 2022 04:45:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44542 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230380AbiIVIo6 (ORCPT ); Thu, 22 Sep 2022 04:44:58 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3DAA29FAAC for ; Thu, 22 Sep 2022 01:44:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1663836290; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PzsiJLh4qivuvaD01T8k0cktWemsb5i0yo5HxUE+jT4=; b=RTttvXj8skHrcydFLj4E71LjUkZ3Wh/140H0csHfFZZ2WcZWXcMZUlS0YIL8SjSrZkur32 09UjvO57q+8wxrYFdK0JRCzfsn+Txa0QRuxe9q5iEgMjx5lOpDjvPbWjx6t0c/7UeItG7v lmZDq70J33tZFJCRlQFuo2sHxiBpU9k= Received: from mail-ej1-f71.google.com (mail-ej1-f71.google.com [209.85.218.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-634-uceJsMaHO3GV3WF_K-bfEA-1; Thu, 22 Sep 2022 04:44:49 -0400 X-MC-Unique: uceJsMaHO3GV3WF_K-bfEA-1 Received: by mail-ej1-f71.google.com with SMTP id oz30-20020a1709077d9e00b0077239b6a915so4177716ejc.11 for ; Thu, 22 Sep 2022 01:44:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=PzsiJLh4qivuvaD01T8k0cktWemsb5i0yo5HxUE+jT4=; b=CY9Oek2hAUUv/wRROtBXYvhEUI79uVxi5lxut3aSgfn4L7GzJrqpOgMGRoQfnZDdOz HxC1IIXfIj0I1mCsw/AqPoMgfbEbKHyPyKLarR0As4RAJPREP7d9pL9j7lAg32U01Gvo XYqus66nbri9BhpPAXwCqTzYu5oE6mQleZJOnr3HNjxLhxcM8Ickl+vR9n+dTg4VJoYp YHDp/xfRO067BWhZCPiZ1ZMHyjLUO3LF+UDoRnNuN0kjdCY88owSQeZagBEQjkJKgold 7D9NP24s2uts86RWmck3uqraTpbhEnC6jC3Gx3kgrNcy+KuPaAvTL3NDW67jTgpae6Jc 5bGw== X-Gm-Message-State: ACrzQf1SpVP8WCCiTMeGKeP8XqewB0BjxypKJcPlmBjpS4pkUccOpS3v kciUc4XxVyu3RbvEpSEWxu/FxqfpjOUFoWLDSkt44vghp1JG5nV3qzo27h8piNRxOpSIwMgGS/n Guk+ifhErkBdM9vpV5Hj6jdDGu2WjP+HHJ+gT1iXO+aloxJIfCSue5aBVFmP+hFro6oZJ9xDyqA panA== X-Received: by 2002:aa7:c415:0:b0:44d:f432:3e84 with SMTP id j21-20020aa7c415000000b0044df4323e84mr2174426edq.56.1663836288184; Thu, 22 Sep 2022 01:44:48 -0700 (PDT) X-Google-Smtp-Source: AMsMyM41ddCte/pwWp/k9Dbeb6MyI1l216b+mSVDUBBBrzugCjAykrYvR+z6fgakR2o0uqGdtsc8uA== X-Received: by 2002:aa7:c415:0:b0:44d:f432:3e84 with SMTP id j21-20020aa7c415000000b0044df4323e84mr2174404edq.56.1663836287987; Thu, 22 Sep 2022 01:44:47 -0700 (PDT) Received: from miu.piliscsaba.redhat.com (193-226-214-223.pool.digikabel.hu. [193.226.214.223]) by smtp.gmail.com with ESMTPSA id h15-20020a170906718f00b00730b3bdd8d7sm2297942ejk.179.2022.09.22.01.44.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Sep 2022 01:44:47 -0700 (PDT) From: Miklos Szeredi To: linux-fsdevel@vger.kernel.org Cc: Al Viro , Christian Brauner , Amir Goldstein , David Howells , Yu-li Lin , Chirantan Ekbote Subject: [PATCH v4 03/10] cachefiles: tmpfile error handling cleanup Date: Thu, 22 Sep 2022 10:44:35 +0200 Message-Id: <20220922084442.2401223-4-mszeredi@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220922084442.2401223-1-mszeredi@redhat.com> References: <20220922084442.2401223-1-mszeredi@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Separate the error labels from the success path and use 'ret' to store the error value before jumping to the error label. Signed-off-by: Miklos Szeredi --- fs/cachefiles/namei.c | 55 ++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/fs/cachefiles/namei.c b/fs/cachefiles/namei.c index facf2ebe464b..d3a5884fe5c9 100644 --- a/fs/cachefiles/namei.c +++ b/fs/cachefiles/namei.c @@ -460,31 +460,27 @@ struct file *cachefiles_create_tmpfile(struct cachefiles_object *object) path.mnt = cache->mnt; ret = cachefiles_inject_write_error(); - if (ret == 0) + if (ret == 0) { path.dentry = vfs_tmpfile(&init_user_ns, fan, S_IFREG, O_RDWR); - else - path.dentry = ERR_PTR(ret); - if (IS_ERR(path.dentry)) { - trace_cachefiles_vfs_error(object, d_inode(fan), PTR_ERR(path.dentry), + ret = PTR_ERR_OR_ZERO(path.dentry); + } + if (ret) { + trace_cachefiles_vfs_error(object, d_inode(fan), ret, cachefiles_trace_tmpfile_error); - if (PTR_ERR(path.dentry) == -EIO) + if (ret == -EIO) cachefiles_io_error_obj(object, "Failed to create tmpfile"); - file = ERR_CAST(path.dentry); - goto out; + goto err; } trace_cachefiles_tmpfile(object, d_backing_inode(path.dentry)); - if (!cachefiles_mark_inode_in_use(object, path.dentry)) { - file = ERR_PTR(-EBUSY); - goto out_dput; - } + ret = -EBUSY; + if (!cachefiles_mark_inode_in_use(object, path.dentry)) + goto err_dput; ret = cachefiles_ondemand_init_object(object); - if (ret < 0) { - file = ERR_PTR(ret); - goto out_unuse; - } + if (ret < 0) + goto err_unuse; ni_size = object->cookie->object_size; ni_size = round_up(ni_size, CACHEFILES_DIO_BLOCK_SIZE); @@ -499,36 +495,37 @@ struct file *cachefiles_create_tmpfile(struct cachefiles_object *object) trace_cachefiles_vfs_error( object, d_backing_inode(path.dentry), ret, cachefiles_trace_trunc_error); - file = ERR_PTR(ret); - goto out_unuse; + goto err_unuse; } } file = open_with_fake_path(&path, O_RDWR | O_LARGEFILE | O_DIRECT, d_backing_inode(path.dentry), cache->cache_cred); + ret = PTR_ERR(file); if (IS_ERR(file)) { trace_cachefiles_vfs_error(object, d_backing_inode(path.dentry), - PTR_ERR(file), - cachefiles_trace_open_error); - goto out_unuse; + ret, cachefiles_trace_open_error); + goto err_unuse; } + ret = -EINVAL; if (unlikely(!file->f_op->read_iter) || unlikely(!file->f_op->write_iter)) { fput(file); pr_notice("Cache does not support read_iter and write_iter\n"); - file = ERR_PTR(-EINVAL); - goto out_unuse; + goto err_unuse; } - - goto out_dput; - -out_unuse: - cachefiles_do_unmark_inode_in_use(object, path.dentry); -out_dput: dput(path.dentry); out: cachefiles_end_secure(cache, saved_cred); return file; + +err_unuse: + cachefiles_do_unmark_inode_in_use(object, path.dentry); +err_dput: + dput(path.dentry); +err: + file = ERR_PTR(ret); + goto out; } /* From patchwork Thu Sep 22 08:44:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miklos Szeredi X-Patchwork-Id: 12984740 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D4184C6FA8B for ; Thu, 22 Sep 2022 08:45:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230380AbiIVIpW (ORCPT ); Thu, 22 Sep 2022 04:45:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44610 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230394AbiIVIo7 (ORCPT ); Thu, 22 Sep 2022 04:44:59 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3DDB9A0247 for ; Thu, 22 Sep 2022 01:44:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1663836291; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pNJWIfJSeZg6QdnpOp0Q5fhm9Bpczx3RBHL8ibawlzI=; b=anPFyOzksUSnROa9/p3Zc+vM0tnHCbNfnpczDka12wQDKAjZTCGuEm0kpG+M86lgo3xKHo aXCRPyx0S1oeHmfPN1u9wJ1C78gxAHhObBEj9c1LJlSPrRrxEx9lstLHsydk7vXA/HLLjr ShOQGTWjxGT6qnxZQIQISYhLN8SKtwA= Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-241-UjjHf6czO5-1jxOsXg2bBg-1; Thu, 22 Sep 2022 04:44:50 -0400 X-MC-Unique: UjjHf6czO5-1jxOsXg2bBg-1 Received: by mail-ej1-f70.google.com with SMTP id hr12-20020a1709073f8c00b0077e8371f847so4234169ejc.20 for ; Thu, 22 Sep 2022 01:44:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=pNJWIfJSeZg6QdnpOp0Q5fhm9Bpczx3RBHL8ibawlzI=; b=4Y1VFMNk/KxcgD6kdaV/Ebmth0QHQ/UwR2ja6RIfORGoFpo/kopdLM/l8cCRCfurBl eiJjff844SHbX7TK+OGAY3XN5mianvXuRVv5MVq3exfQArx7z1SRs7kzeH0ZeGO9ATIE +KA2KQ75bzze2PqSKQgdxiyeQAizAIJzCG1PLXHPKy/Po4Hz/DyGHBh9C/2wYoXWKhqT T4jVYzriMXWCmSojqaAUiPdyr8S6PsDmZDLAyGwrPnnlQ3GDRXr22/TN3r5zYtWDX4BH E8w7PVwbT7gspZ3iuZRHZIvGeAU8hCV6MZ8tL61rja5pg7kfEB6F/ni+A01nur/nCXPS qj9A== X-Gm-Message-State: ACrzQf3WySDMCG7OkCF36xh2fN/Mbi4fjoVeXAjICEAHNNPptBs8JJ1O a0XxIJAOuEddDo9BV5RewL5sB+x3JhH/dY9ei8J1q/QvK41Ma2kBXyyoPld4LhVQaxWzd6osGBO Ie55YNbve74ORNiZsGztO8a+NSjiuSK2na9nCNtu+zDfM1SOCfUM3g474cNU2wSVePVRLUCm0Ne F9tw== X-Received: by 2002:a17:907:960e:b0:782:68e3:620f with SMTP id gb14-20020a170907960e00b0078268e3620fmr487523ejc.663.1663836289113; Thu, 22 Sep 2022 01:44:49 -0700 (PDT) X-Google-Smtp-Source: AMsMyM41kuSs0ywXc/KeQPJA/EndPAQ7NsfRy75GYDPZW8027PY9PEd7FtuzpSW8th8Yt1odwDMhnA== X-Received: by 2002:a17:907:960e:b0:782:68e3:620f with SMTP id gb14-20020a170907960e00b0078268e3620fmr487499ejc.663.1663836288829; Thu, 22 Sep 2022 01:44:48 -0700 (PDT) Received: from miu.piliscsaba.redhat.com (193-226-214-223.pool.digikabel.hu. [193.226.214.223]) by smtp.gmail.com with ESMTPSA id h15-20020a170906718f00b00730b3bdd8d7sm2297942ejk.179.2022.09.22.01.44.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Sep 2022 01:44:48 -0700 (PDT) From: Miklos Szeredi To: linux-fsdevel@vger.kernel.org Cc: Al Viro , Christian Brauner , Amir Goldstein , David Howells , Yu-li Lin , Chirantan Ekbote Subject: [PATCH v4 04/10] cachefiles: only pass inode to *mark_inode_inuse() helpers Date: Thu, 22 Sep 2022 10:44:36 +0200 Message-Id: <20220922084442.2401223-5-mszeredi@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220922084442.2401223-1-mszeredi@redhat.com> References: <20220922084442.2401223-1-mszeredi@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org The only reason to pass dentry was because of a pr_notice() text. Move that to the two callers where it makes sense and add a WARN_ON() to the third. Signed-off-by: Miklos Szeredi Reviewed-by: Christian Brauner (Microsoft) --- fs/cachefiles/namei.c | 47 ++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/fs/cachefiles/namei.c b/fs/cachefiles/namei.c index d3a5884fe5c9..4d04f5fb49f3 100644 --- a/fs/cachefiles/namei.c +++ b/fs/cachefiles/namei.c @@ -15,9 +15,8 @@ * file or directory. The caller must hold the inode lock. */ static bool __cachefiles_mark_inode_in_use(struct cachefiles_object *object, - struct dentry *dentry) + struct inode *inode) { - struct inode *inode = d_backing_inode(dentry); bool can_use = false; if (!(inode->i_flags & S_KERNEL_FILE)) { @@ -26,21 +25,18 @@ static bool __cachefiles_mark_inode_in_use(struct cachefiles_object *object, can_use = true; } else { trace_cachefiles_mark_failed(object, inode); - pr_notice("cachefiles: Inode already in use: %pd (B=%lx)\n", - dentry, inode->i_ino); } return can_use; } static bool cachefiles_mark_inode_in_use(struct cachefiles_object *object, - struct dentry *dentry) + struct inode *inode) { - struct inode *inode = d_backing_inode(dentry); bool can_use; inode_lock(inode); - can_use = __cachefiles_mark_inode_in_use(object, dentry); + can_use = __cachefiles_mark_inode_in_use(object, inode); inode_unlock(inode); return can_use; } @@ -49,21 +45,17 @@ static bool cachefiles_mark_inode_in_use(struct cachefiles_object *object, * Unmark a backing inode. The caller must hold the inode lock. */ static void __cachefiles_unmark_inode_in_use(struct cachefiles_object *object, - struct dentry *dentry) + struct inode *inode) { - struct inode *inode = d_backing_inode(dentry); - inode->i_flags &= ~S_KERNEL_FILE; trace_cachefiles_mark_inactive(object, inode); } static void cachefiles_do_unmark_inode_in_use(struct cachefiles_object *object, - struct dentry *dentry) + struct inode *inode) { - struct inode *inode = d_backing_inode(dentry); - inode_lock(inode); - __cachefiles_unmark_inode_in_use(object, dentry); + __cachefiles_unmark_inode_in_use(object, inode); inode_unlock(inode); } @@ -78,7 +70,7 @@ void cachefiles_unmark_inode_in_use(struct cachefiles_object *object, struct inode *inode = file_inode(file); if (inode) { - cachefiles_do_unmark_inode_in_use(object, file->f_path.dentry); + cachefiles_do_unmark_inode_in_use(object, file_inode(file)); if (!test_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags)) { atomic_long_add(inode->i_blocks, &cache->b_released); @@ -164,8 +156,11 @@ struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache, inode_lock(d_inode(subdir)); inode_unlock(d_inode(dir)); - if (!__cachefiles_mark_inode_in_use(NULL, subdir)) + if (!__cachefiles_mark_inode_in_use(NULL, d_inode(subdir))) { + pr_notice("cachefiles: Inode already in use: %pd (B=%lx)\n", + subdir, d_inode(subdir)->i_ino); goto mark_error; + } inode_unlock(d_inode(subdir)); @@ -225,7 +220,7 @@ void cachefiles_put_directory(struct dentry *dir) { if (dir) { inode_lock(dir->d_inode); - __cachefiles_unmark_inode_in_use(NULL, dir); + __cachefiles_unmark_inode_in_use(NULL, d_inode(dir)); inode_unlock(dir->d_inode); dput(dir); } @@ -410,7 +405,7 @@ int cachefiles_bury_object(struct cachefiles_cache *cache, "Rename failed with error %d", ret); } - __cachefiles_unmark_inode_in_use(object, rep); + __cachefiles_unmark_inode_in_use(object, d_inode(rep)); unlock_rename(cache->graveyard, dir); dput(grave); _leave(" = 0"); @@ -474,9 +469,9 @@ struct file *cachefiles_create_tmpfile(struct cachefiles_object *object) trace_cachefiles_tmpfile(object, d_backing_inode(path.dentry)); - ret = -EBUSY; - if (!cachefiles_mark_inode_in_use(object, path.dentry)) - goto err_dput; + /* This is a newly created file with no other possible user */ + if (!cachefiles_mark_inode_in_use(object, d_inode(path.dentry))) + WARN_ON(1); ret = cachefiles_ondemand_init_object(object); if (ret < 0) @@ -520,8 +515,7 @@ struct file *cachefiles_create_tmpfile(struct cachefiles_object *object) return file; err_unuse: - cachefiles_do_unmark_inode_in_use(object, path.dentry); -err_dput: + cachefiles_do_unmark_inode_in_use(object, d_inode(path.dentry)); dput(path.dentry); err: file = ERR_PTR(ret); @@ -566,8 +560,11 @@ static bool cachefiles_open_file(struct cachefiles_object *object, _enter("%pd", dentry); - if (!cachefiles_mark_inode_in_use(object, dentry)) + if (!cachefiles_mark_inode_in_use(object, d_inode(dentry))) { + pr_notice("cachefiles: Inode already in use: %pd (B=%lx)\n", + dentry, d_inode(dentry)->i_ino); return false; + } /* We need to open a file interface onto a data file now as we can't do * it on demand because writeback called from do_exit() sees @@ -621,7 +618,7 @@ static bool cachefiles_open_file(struct cachefiles_object *object, error_fput: fput(file); error: - cachefiles_do_unmark_inode_in_use(object, dentry); + cachefiles_do_unmark_inode_in_use(object, d_inode(dentry)); dput(dentry); return false; } From patchwork Thu Sep 22 08:44:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miklos Szeredi X-Patchwork-Id: 12984739 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8E024C6FA82 for ; Thu, 22 Sep 2022 08:45:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230192AbiIVIpV (ORCPT ); Thu, 22 Sep 2022 04:45:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44302 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230340AbiIVIo7 (ORCPT ); Thu, 22 Sep 2022 04:44:59 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3DCC7A00E1 for ; Thu, 22 Sep 2022 01:44:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1663836293; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=K1pR2BYypep7fpfPKTSIp9hGnNDl6nJRFYw8u5/Oigo=; b=VMYNtugNiA3EY0uOitwKxAh6ZbNk1UyHj9ePyML+dGEx/bkpKmH79e5oLGHTsgZOU+e/f7 DrnJU956cywpkTugToVd/0kNUY4Pn3YlOsA1uCT8xu8CX9gcLKYgH9jrdx9UG7Bp/hPB2s JiIRxhX+5CcSsE16uO0aflMvVYONtSM= Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-127-hB8jw7QkPWu8PjWth57cpg-1; Thu, 22 Sep 2022 04:44:51 -0400 X-MC-Unique: hB8jw7QkPWu8PjWth57cpg-1 Received: by mail-ej1-f70.google.com with SMTP id sa15-20020a1709076d0f00b00781d793f51fso1340081ejc.18 for ; Thu, 22 Sep 2022 01:44:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=K1pR2BYypep7fpfPKTSIp9hGnNDl6nJRFYw8u5/Oigo=; b=VljOH3NKAAobpotmlNS1ILDk3ItIQyiJfQG7m5wAbNjcnXKn2G+6TQdTSbvQZk6/tB e3Yzwkh9MURsOiwv+QgY3sKX9B09NWQEVe3cjwhqiLEWryH7uD0uxU0ZaPKF77+c/p+0 ghNWHsqpaZ/m1VWOX5e6vFkMGysm+A/dYae/VqOKS5fDomylPNDSwzP7hKb6xuwHZ/Fb 9yuF0W1kTvK9lF9LGWaQEl1ugKO1CEjjgWGSIOZ/XNNQHAgdJqF5Z8gzI4b4Fh96lIzN CXGPMD7EMGjlMBbccuyGkWceKuGdxPiH8EAX6F1jDt6oHROhrGEfZRqqnuQIOWcpUo63 Vazw== X-Gm-Message-State: ACrzQf3DRC/fXSEFcXk2B7FXuc79ftaXjJBggHxFEm3iRnhbHqvnl46x rgAnogced7HVYySa/0O+cjZCbcu+mVDpkCVZtnhidjgtB/R7Jw5M44RgDMA0B+sAQzrxjGTu5Mu rKeIY5j8bDjrJNcjf4Ug26IsdA1cNstnerqD5Z7aXk7QbOTmQlPlXf9WGxZTryl7+7IFQe8fQI1 PYtg== X-Received: by 2002:a17:907:97cd:b0:780:6829:cb9d with SMTP id js13-20020a17090797cd00b007806829cb9dmr1923918ejc.344.1663836290152; Thu, 22 Sep 2022 01:44:50 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7JJW4IRkVzDZsjbqAsH4ChxfLNwqDIITtK4puuJWwHSJhBuG9Auerz/4iSSDAD6oR6P0BTTg== X-Received: by 2002:a17:907:97cd:b0:780:6829:cb9d with SMTP id js13-20020a17090797cd00b007806829cb9dmr1923892ejc.344.1663836289856; Thu, 22 Sep 2022 01:44:49 -0700 (PDT) Received: from miu.piliscsaba.redhat.com (193-226-214-223.pool.digikabel.hu. [193.226.214.223]) by smtp.gmail.com with ESMTPSA id h15-20020a170906718f00b00730b3bdd8d7sm2297942ejk.179.2022.09.22.01.44.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Sep 2022 01:44:49 -0700 (PDT) From: Miklos Szeredi To: linux-fsdevel@vger.kernel.org Cc: Al Viro , Christian Brauner , Amir Goldstein , David Howells , Yu-li Lin , Chirantan Ekbote Subject: [PATCH v4 05/10] cachefiles: use vfs_tmpfile_open() helper Date: Thu, 22 Sep 2022 10:44:37 +0200 Message-Id: <20220922084442.2401223-6-mszeredi@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220922084442.2401223-1-mszeredi@redhat.com> References: <20220922084442.2401223-1-mszeredi@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Use the vfs_tmpfile_open() helper instead of doing tmpfile creation and opening separately. The only minor difference is that previously no permission checking was done, while vfs_tmpfile_open() will call may_open() with zero access mask (i.e. no access is checked). Even if this would make a difference with callers caps (don't see how it could, even in the LSM codepaths) cachfiles raises caps before performing the tmpfile creation, so this extra permission check will not result in any regression. Signed-off-by: Miklos Szeredi Reviewed-by: Christian Brauner (Microsoft) --- fs/cachefiles/namei.c | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/fs/cachefiles/namei.c b/fs/cachefiles/namei.c index 4d04f5fb49f3..622d7c644bd4 100644 --- a/fs/cachefiles/namei.c +++ b/fs/cachefiles/namei.c @@ -446,18 +446,19 @@ struct file *cachefiles_create_tmpfile(struct cachefiles_object *object) const struct cred *saved_cred; struct dentry *fan = volume->fanout[(u8)object->cookie->key_hash]; struct file *file; - struct path path; + const struct path parentpath = { .mnt = cache->mnt, .dentry = fan }; uint64_t ni_size; long ret; cachefiles_begin_secure(cache, &saved_cred); - path.mnt = cache->mnt; ret = cachefiles_inject_write_error(); if (ret == 0) { - path.dentry = vfs_tmpfile(&init_user_ns, fan, S_IFREG, O_RDWR); - ret = PTR_ERR_OR_ZERO(path.dentry); + file = vfs_tmpfile_open(&init_user_ns, &parentpath, S_IFREG, + O_RDWR | O_LARGEFILE | O_DIRECT, + cache->cache_cred); + ret = PTR_ERR_OR_ZERO(file); } if (ret) { trace_cachefiles_vfs_error(object, d_inode(fan), ret, @@ -467,10 +468,10 @@ struct file *cachefiles_create_tmpfile(struct cachefiles_object *object) goto err; } - trace_cachefiles_tmpfile(object, d_backing_inode(path.dentry)); + trace_cachefiles_tmpfile(object, file_inode(file)); /* This is a newly created file with no other possible user */ - if (!cachefiles_mark_inode_in_use(object, d_inode(path.dentry))) + if (!cachefiles_mark_inode_in_use(object, file_inode(file))) WARN_ON(1); ret = cachefiles_ondemand_init_object(object); @@ -481,27 +482,19 @@ struct file *cachefiles_create_tmpfile(struct cachefiles_object *object) ni_size = round_up(ni_size, CACHEFILES_DIO_BLOCK_SIZE); if (ni_size > 0) { - trace_cachefiles_trunc(object, d_backing_inode(path.dentry), 0, ni_size, + trace_cachefiles_trunc(object, file_inode(file), 0, ni_size, cachefiles_trunc_expand_tmpfile); ret = cachefiles_inject_write_error(); if (ret == 0) - ret = vfs_truncate(&path, ni_size); + ret = vfs_truncate(&file->f_path, ni_size); if (ret < 0) { trace_cachefiles_vfs_error( - object, d_backing_inode(path.dentry), ret, + object, file_inode(file), ret, cachefiles_trace_trunc_error); goto err_unuse; } } - file = open_with_fake_path(&path, O_RDWR | O_LARGEFILE | O_DIRECT, - d_backing_inode(path.dentry), cache->cache_cred); - ret = PTR_ERR(file); - if (IS_ERR(file)) { - trace_cachefiles_vfs_error(object, d_backing_inode(path.dentry), - ret, cachefiles_trace_open_error); - goto err_unuse; - } ret = -EINVAL; if (unlikely(!file->f_op->read_iter) || unlikely(!file->f_op->write_iter)) { @@ -509,14 +502,13 @@ struct file *cachefiles_create_tmpfile(struct cachefiles_object *object) pr_notice("Cache does not support read_iter and write_iter\n"); goto err_unuse; } - dput(path.dentry); out: cachefiles_end_secure(cache, saved_cred); return file; err_unuse: - cachefiles_do_unmark_inode_in_use(object, d_inode(path.dentry)); - dput(path.dentry); + cachefiles_do_unmark_inode_in_use(object, file_inode(file)); + fput(file); err: file = ERR_PTR(ret); goto out; From patchwork Thu Sep 22 08:44:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miklos Szeredi X-Patchwork-Id: 12984741 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8B003C6FA82 for ; Thu, 22 Sep 2022 08:45:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230352AbiIVIpX (ORCPT ); Thu, 22 Sep 2022 04:45:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44624 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230404AbiIVIo7 (ORCPT ); Thu, 22 Sep 2022 04:44:59 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3E9755B79A for ; Thu, 22 Sep 2022 01:44:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1663836294; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=6rcFQG0iVLGv2/ysstKj5Y5FuHiIoW0CS65N+NYHLtI=; b=bnYiTdC97vo1LDUgKV1v2BqnhQrkgaBQmk5S3gje+Y6opaKBh7qXHh9CkHhGiHhTwvmGxn xdIH5/u/TsIRzZFom2rcSFfoB/0Jq2zczvEJ2pJih6vGnUlS0JDLb6lqRRGOEiEvz/WsBm EZcozI3C8skBocHI1ygbTd9nT8gfKDY= Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-616-uhOZF1UmN3iMOM5aYGKH-A-1; Thu, 22 Sep 2022 04:44:52 -0400 X-MC-Unique: uhOZF1UmN3iMOM5aYGKH-A-1 Received: by mail-ed1-f69.google.com with SMTP id y9-20020a056402270900b00451dfbbc9b2so6147187edd.12 for ; Thu, 22 Sep 2022 01:44:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=6rcFQG0iVLGv2/ysstKj5Y5FuHiIoW0CS65N+NYHLtI=; b=OF2Tl37dW/b3MuAlFyh8pC615hxLfXxRnAO+PZB7YKMnwqXitcM93Z6E/ywNf6yBsr oMua5GL/rwAIMWWfId6JF2O0o8ffWGBFZoejLQXUqJhFtSRMoQXXlxhJVH25fpXST46m 9bhlZfSgZ1qafVGa6nVspGpEIvAK1AXVThaM9/KK/diOM2iQO6tDlS6xFx/1NxzZ+Qmh 2/69904GoCjsfoLNjrUDYsihO7DB7IHgJ6aR3t997LsWi3IzyhoytwO65888uksZH9pt +3+enwIVmF9bwtwi6yNwAAfPH/Z8YpAsR3zh0YSstLeoUWlCmBAznMNoMzrNgWXwAMu8 JlfQ== X-Gm-Message-State: ACrzQf3k1hwHeZCoRTNp9O8/siTBPRptMOxZ3JSOlZ/JjwuMP+mSnt3k 7JqgUQ+1dO9rglfyDY48PVo0ITyL0z2Hk8eFhmUeFeuL3rL1dXLzoMoWSehfXK8vx0oiyNdSb92 4qOy3EAGY/RYzRk6MyDm1gHgBsMDzudany8H6G09G9ewteyo2l6GLx5sp2pnZumghHfQ4u7W2eD cn4Q== X-Received: by 2002:aa7:de91:0:b0:451:ecf8:e3b6 with SMTP id j17-20020aa7de91000000b00451ecf8e3b6mr2078391edv.402.1663836291291; Thu, 22 Sep 2022 01:44:51 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4QhYPOlANG7yL0Meuo62QZQ/eQCwLMy0EhcU1nqAz3LIqGJimKl+4U23oUTnsHFrXhD7c9FA== X-Received: by 2002:aa7:de91:0:b0:451:ecf8:e3b6 with SMTP id j17-20020aa7de91000000b00451ecf8e3b6mr2078375edv.402.1663836291017; Thu, 22 Sep 2022 01:44:51 -0700 (PDT) Received: from miu.piliscsaba.redhat.com (193-226-214-223.pool.digikabel.hu. [193.226.214.223]) by smtp.gmail.com with ESMTPSA id h15-20020a170906718f00b00730b3bdd8d7sm2297942ejk.179.2022.09.22.01.44.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Sep 2022 01:44:50 -0700 (PDT) From: Miklos Szeredi To: linux-fsdevel@vger.kernel.org Cc: Al Viro , Christian Brauner , Amir Goldstein , David Howells , Yu-li Lin , Chirantan Ekbote Subject: [PATCH v4 06/10] ovl: use vfs_tmpfile_open() helper Date: Thu, 22 Sep 2022 10:44:38 +0200 Message-Id: <20220922084442.2401223-7-mszeredi@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220922084442.2401223-1-mszeredi@redhat.com> References: <20220922084442.2401223-1-mszeredi@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org If tmpfile is used for copy up, then use this helper to create the tmpfile and open it at the same time. This will later allow filesystems such as fuse to do this operation atomically. Reviewed-by: Christian Brauner (Microsoft) Signed-off-by: Miklos Szeredi --- fs/overlayfs/copy_up.c | 108 +++++++++++++++++++++------------------ fs/overlayfs/overlayfs.h | 14 ++--- fs/overlayfs/super.c | 10 ++-- fs/overlayfs/util.c | 2 +- 4 files changed, 72 insertions(+), 62 deletions(-) diff --git a/fs/overlayfs/copy_up.c b/fs/overlayfs/copy_up.c index fdde6c56cc3d..62a63e9ca57d 100644 --- a/fs/overlayfs/copy_up.c +++ b/fs/overlayfs/copy_up.c @@ -193,11 +193,11 @@ static int ovl_copy_fileattr(struct inode *inode, struct path *old, return ovl_real_fileattr_set(new, &newfa); } -static int ovl_copy_up_data(struct ovl_fs *ofs, struct path *old, - struct path *new, loff_t len) +static int ovl_copy_up_file(struct ovl_fs *ofs, struct dentry *dentry, + struct file *new_file, loff_t len) { + struct path datapath; struct file *old_file; - struct file *new_file; loff_t old_pos = 0; loff_t new_pos = 0; loff_t cloned; @@ -206,23 +206,18 @@ static int ovl_copy_up_data(struct ovl_fs *ofs, struct path *old, bool skip_hole = false; int error = 0; - if (len == 0) - return 0; + ovl_path_lowerdata(dentry, &datapath); + if (WARN_ON(datapath.dentry == NULL)) + return -EIO; - old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY); + old_file = ovl_path_open(&datapath, O_LARGEFILE | O_RDONLY); if (IS_ERR(old_file)) return PTR_ERR(old_file); - new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY); - if (IS_ERR(new_file)) { - error = PTR_ERR(new_file); - goto out_fput; - } - /* Try to use clone_file_range to clone up within the same fs */ cloned = do_clone_file_range(old_file, 0, new_file, 0, len, 0); if (cloned == len) - goto out; + goto out_fput; /* Couldn't clone, so now we try to copy the data */ /* Check if lower fs supports seek operation */ @@ -282,10 +277,8 @@ static int ovl_copy_up_data(struct ovl_fs *ofs, struct path *old, len -= bytes; } -out: if (!error && ovl_should_sync(ofs)) error = vfs_fsync(new_file, 0); - fput(new_file); out_fput: fput(old_file); return error; @@ -556,30 +549,31 @@ static int ovl_link_up(struct ovl_copy_up_ctx *c) return err; } -static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp) +static int ovl_copy_up_data(struct ovl_copy_up_ctx *c, const struct path *temp) { struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); - struct inode *inode = d_inode(c->dentry); - struct path upperpath, datapath; + struct file *new_file; int err; - ovl_path_upper(c->dentry, &upperpath); - if (WARN_ON(upperpath.dentry != NULL)) - return -EIO; + if (!S_ISREG(c->stat.mode) || c->metacopy || !c->stat.size) + return 0; - upperpath.dentry = temp; + new_file = ovl_path_open(temp, O_LARGEFILE | O_WRONLY); + if (IS_ERR(new_file)) + return PTR_ERR(new_file); - /* - * Copy up data first and then xattrs. Writing data after - * xattrs will remove security.capability xattr automatically. - */ - if (S_ISREG(c->stat.mode) && !c->metacopy) { - ovl_path_lowerdata(c->dentry, &datapath); - err = ovl_copy_up_data(ofs, &datapath, &upperpath, - c->stat.size); - if (err) - return err; - } + err = ovl_copy_up_file(ofs, c->dentry, new_file, c->stat.size); + fput(new_file); + + return err; +} + +static int ovl_copy_up_metadata(struct ovl_copy_up_ctx *c, struct dentry *temp) +{ + struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); + struct inode *inode = d_inode(c->dentry); + struct path upperpath = { .mnt = ovl_upper_mnt(ofs), .dentry = temp }; + int err; err = ovl_copy_xattr(c->dentry->d_sb, &c->lowerpath, temp); if (err) @@ -662,6 +656,7 @@ static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c) struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); struct inode *inode; struct inode *udir = d_inode(c->destdir), *wdir = d_inode(c->workdir); + struct path path = { .mnt = ovl_upper_mnt(ofs) }; struct dentry *temp, *upper; struct ovl_cu_creds cc; int err; @@ -688,7 +683,16 @@ static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c) if (IS_ERR(temp)) goto unlock; - err = ovl_copy_up_inode(c, temp); + /* + * Copy up data first and then xattrs. Writing data after + * xattrs will remove security.capability xattr automatically. + */ + path.dentry = temp; + err = ovl_copy_up_data(c, &path); + if (err) + goto cleanup; + + err = ovl_copy_up_metadata(c, temp); if (err) goto cleanup; @@ -732,6 +736,7 @@ static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c) struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); struct inode *udir = d_inode(c->destdir); struct dentry *temp, *upper; + struct file *tmpfile; struct ovl_cu_creds cc; int err; @@ -739,15 +744,22 @@ static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c) if (err) return err; - temp = ovl_do_tmpfile(ofs, c->workdir, c->stat.mode); + tmpfile = ovl_do_tmpfile(ofs, c->workdir, c->stat.mode); ovl_revert_cu_creds(&cc); - if (IS_ERR(temp)) - return PTR_ERR(temp); + if (IS_ERR(tmpfile)) + return PTR_ERR(tmpfile); - err = ovl_copy_up_inode(c, temp); + temp = tmpfile->f_path.dentry; + if (!c->metacopy && c->stat.size) { + err = ovl_copy_up_file(ofs, c->dentry, tmpfile, c->stat.size); + if (err) + return err; + } + + err = ovl_copy_up_metadata(c, temp); if (err) - goto out_dput; + goto out_fput; inode_lock_nested(udir, I_MUTEX_PARENT); @@ -761,16 +773,14 @@ static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c) inode_unlock(udir); if (err) - goto out_dput; + goto out_fput; if (!c->metacopy) ovl_set_upperdata(d_inode(c->dentry)); - ovl_inode_update(d_inode(c->dentry), temp); + ovl_inode_update(d_inode(c->dentry), dget(temp)); - return 0; - -out_dput: - dput(temp); +out_fput: + fput(tmpfile); return err; } @@ -899,7 +909,7 @@ static ssize_t ovl_getxattr_value(struct path *path, char *name, char **value) static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c) { struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); - struct path upperpath, datapath; + struct path upperpath; int err; char *capability = NULL; ssize_t cap_size; @@ -908,10 +918,6 @@ static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c) if (WARN_ON(upperpath.dentry == NULL)) return -EIO; - ovl_path_lowerdata(c->dentry, &datapath); - if (WARN_ON(datapath.dentry == NULL)) - return -EIO; - if (c->stat.size) { err = cap_size = ovl_getxattr_value(&upperpath, XATTR_NAME_CAPS, &capability); @@ -919,7 +925,7 @@ static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c) goto out; } - err = ovl_copy_up_data(ofs, &datapath, &upperpath, c->stat.size); + err = ovl_copy_up_data(c, &upperpath); if (err) goto out_free; diff --git a/fs/overlayfs/overlayfs.h b/fs/overlayfs/overlayfs.h index 87759165d32b..0f9dbd0e2ff5 100644 --- a/fs/overlayfs/overlayfs.h +++ b/fs/overlayfs/overlayfs.h @@ -310,14 +310,16 @@ static inline int ovl_do_whiteout(struct ovl_fs *ofs, return err; } -static inline struct dentry *ovl_do_tmpfile(struct ovl_fs *ofs, - struct dentry *dentry, umode_t mode) +static inline struct file *ovl_do_tmpfile(struct ovl_fs *ofs, + struct dentry *dentry, umode_t mode) { - struct dentry *ret = vfs_tmpfile(ovl_upper_mnt_userns(ofs), dentry, mode, 0); - int err = PTR_ERR_OR_ZERO(ret); + struct path path = { .mnt = ovl_upper_mnt(ofs), .dentry = dentry }; + struct file *file = vfs_tmpfile_open(ovl_upper_mnt_userns(ofs), &path, mode, + O_LARGEFILE | O_WRONLY, current_cred()); + int err = PTR_ERR_OR_ZERO(file); pr_debug("tmpfile(%pd2, 0%o) = %i\n", dentry, mode, err); - return ret; + return file; } static inline struct dentry *ovl_lookup_upper(struct ovl_fs *ofs, @@ -401,7 +403,7 @@ void ovl_inode_update(struct inode *inode, struct dentry *upperdentry); void ovl_dir_modified(struct dentry *dentry, bool impurity); u64 ovl_dentry_version_get(struct dentry *dentry); bool ovl_is_whiteout(struct dentry *dentry); -struct file *ovl_path_open(struct path *path, int flags); +struct file *ovl_path_open(const struct path *path, int flags); int ovl_copy_up_start(struct dentry *dentry, int flags); void ovl_copy_up_end(struct dentry *dentry); bool ovl_already_copied_up(struct dentry *dentry, int flags); diff --git a/fs/overlayfs/super.c b/fs/overlayfs/super.c index ec746d447f1b..7837223689c1 100644 --- a/fs/overlayfs/super.c +++ b/fs/overlayfs/super.c @@ -15,6 +15,7 @@ #include #include #include +#include #include "overlayfs.h" MODULE_AUTHOR("Miklos Szeredi "); @@ -1356,7 +1357,8 @@ static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs, struct path *workpath) { struct vfsmount *mnt = ovl_upper_mnt(ofs); - struct dentry *temp, *workdir; + struct dentry *workdir; + struct file *tmpfile; bool rename_whiteout; bool d_type; int fh_type; @@ -1392,10 +1394,10 @@ static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs, pr_warn("upper fs needs to support d_type.\n"); /* Check if upper/work fs supports O_TMPFILE */ - temp = ovl_do_tmpfile(ofs, ofs->workdir, S_IFREG | 0); - ofs->tmpfile = !IS_ERR(temp); + tmpfile = ovl_do_tmpfile(ofs, ofs->workdir, S_IFREG | 0); + ofs->tmpfile = !IS_ERR(tmpfile); if (ofs->tmpfile) - dput(temp); + fput(tmpfile); else pr_warn("upper fs does not support tmpfile.\n"); diff --git a/fs/overlayfs/util.c b/fs/overlayfs/util.c index 87f811c089e4..968926c0c7ab 100644 --- a/fs/overlayfs/util.c +++ b/fs/overlayfs/util.c @@ -490,7 +490,7 @@ bool ovl_is_whiteout(struct dentry *dentry) return inode && IS_WHITEOUT(inode); } -struct file *ovl_path_open(struct path *path, int flags) +struct file *ovl_path_open(const struct path *path, int flags) { struct inode *inode = d_inode(path->dentry); struct user_namespace *real_mnt_userns = mnt_user_ns(path->mnt); From patchwork Thu Sep 22 08:44:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miklos Szeredi X-Patchwork-Id: 12984742 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BE8DAC6FA82 for ; Thu, 22 Sep 2022 08:45:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230350AbiIVIpZ (ORCPT ); Thu, 22 Sep 2022 04:45:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43872 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230346AbiIVIo7 (ORCPT ); Thu, 22 Sep 2022 04:44:59 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A982760EE for ; Thu, 22 Sep 2022 01:44:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1663836295; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=RrTKazNRm0paFcNnYRxkeaPVY77eHZ75mhDDJvcKZCg=; b=St5JW7L8yC0sCbNHCj1p1Yg5ZfdqvUCIfr0fGQpGuVwYh7t8IScK2VJ6iGzH7NOEUq1LWt /Knk5UCE+Kvi+O1cx50ytlcXVJP/IrwsulT9z8MjicoZDQBm1AwjsEamKGQnw+/BG5vFWQ 7RdGFJyDbe434z2K0vQ+2s16fXaTy7s= Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-177-eeNS3tmxPH-Lxk0wG_xGeA-1; Thu, 22 Sep 2022 04:44:54 -0400 X-MC-Unique: eeNS3tmxPH-Lxk0wG_xGeA-1 Received: by mail-ed1-f70.google.com with SMTP id w20-20020a05640234d400b00450f24c8ca6so6144871edc.13 for ; Thu, 22 Sep 2022 01:44:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=RrTKazNRm0paFcNnYRxkeaPVY77eHZ75mhDDJvcKZCg=; b=DqbIa1YQXbKMrGqFHXp5kxmDx+aMnW1evwo5aEH9+xeMq8JbUhwPiQDLTS+h/jj+ra YRuUhGMM2RIrL80oalaZbvRG7peMnYcTQ2Kg3HgGFELS+I30+DMOEE7hkVNlR+zs4tpz nWjTRAaMliAtKdoDS7vC+U7Vr4Ff5870Gmsk3Wv5lpX9/ReoyGde6cznAxUyBje+Iu9w Jj+po4ybMHYnx8/N+9xJH+M270OrB4Fw/RbJn7F+mTacO8o+8RWyyu4y2yrtjOmFqgdB gdrEWiU890UuBeEPsaSwI04LJ/ii8tbN5qCDLIon8Tg4hSRRxTvHZYsIM7c7Nm8nMybq ebAw== X-Gm-Message-State: ACrzQf09gnrqNrkQfISQcI1z3MIX7kJ9X13xuXdr4TEwJkGtmkq4GR/w Un2SHiuyvrMha0t60QlB16Lv4aeOGuOKNsBCkTrzSYVrEez2E3xVP5e881w3RecTNfSCOvq5I2d St+SX260aYy2Mmf74LJj+BB0SjSR7zifX/TrEmi/RUrrfg2fGdZVwkWgJnxkoqycrIwEWsmtIhB ndBQ== X-Received: by 2002:a17:906:4fd0:b0:781:bcca:78e6 with SMTP id i16-20020a1709064fd000b00781bcca78e6mr1839871ejw.508.1663836292377; Thu, 22 Sep 2022 01:44:52 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4CB1DudmdST2b/PsoDFSnCNK55cPHhtwUFpO1lDweC9SpS4gsR3p6jChXTNytVDBr/Z1ZOsQ== X-Received: by 2002:a17:906:4fd0:b0:781:bcca:78e6 with SMTP id i16-20020a1709064fd000b00781bcca78e6mr1839850ejw.508.1663836292065; Thu, 22 Sep 2022 01:44:52 -0700 (PDT) Received: from miu.piliscsaba.redhat.com (193-226-214-223.pool.digikabel.hu. [193.226.214.223]) by smtp.gmail.com with ESMTPSA id h15-20020a170906718f00b00730b3bdd8d7sm2297942ejk.179.2022.09.22.01.44.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Sep 2022 01:44:51 -0700 (PDT) From: Miklos Szeredi To: linux-fsdevel@vger.kernel.org Cc: Al Viro , Christian Brauner , Amir Goldstein , David Howells , Yu-li Lin , Chirantan Ekbote Subject: [PATCH v4 07/10] vfs: make vfs_tmpfile() static Date: Thu, 22 Sep 2022 10:44:39 +0200 Message-Id: <20220922084442.2401223-8-mszeredi@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220922084442.2401223-1-mszeredi@redhat.com> References: <20220922084442.2401223-1-mszeredi@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org No callers outside of fs/namei.c anymore. Reviewed-by: Christian Brauner (Microsoft) Signed-off-by: Miklos Szeredi --- fs/namei.c | 3 +-- include/linux/fs.h | 3 --- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/fs/namei.c b/fs/namei.c index 81c388a813d3..03ad4e55fb26 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -3583,7 +3583,7 @@ static int do_open(struct nameidata *nd, * On non-idmapped mounts or if permission checking is to be performed on the * raw inode simply passs init_user_ns. */ -struct dentry *vfs_tmpfile(struct user_namespace *mnt_userns, +static struct dentry *vfs_tmpfile(struct user_namespace *mnt_userns, struct dentry *dentry, umode_t mode, int open_flag) { struct dentry *child = NULL; @@ -3622,7 +3622,6 @@ struct dentry *vfs_tmpfile(struct user_namespace *mnt_userns, dput(child); return ERR_PTR(error); } -EXPORT_SYMBOL(vfs_tmpfile); /** * vfs_tmpfile_open - open a tmpfile for kernel internal use diff --git a/include/linux/fs.h b/include/linux/fs.h index 15fafda95dd3..02646542f6bb 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2004,9 +2004,6 @@ static inline int vfs_whiteout(struct user_namespace *mnt_userns, WHITEOUT_DEV); } -struct dentry *vfs_tmpfile(struct user_namespace *mnt_userns, - struct dentry *dentry, umode_t mode, int open_flag); - struct file *vfs_tmpfile_open(struct user_namespace *mnt_userns, const struct path *parentpath, umode_t mode, int open_flag, const struct cred *cred); From patchwork Thu Sep 22 08:44:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miklos Szeredi X-Patchwork-Id: 12984743 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9465CC6FA8B for ; Thu, 22 Sep 2022 08:45:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230417AbiIVIp2 (ORCPT ); Thu, 22 Sep 2022 04:45:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44262 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230409AbiIVIpA (ORCPT ); Thu, 22 Sep 2022 04:45:00 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ABCC1AB4E1 for ; Thu, 22 Sep 2022 01:44:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1663836296; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=q2MdUQ+cskiOFqdTJdIqCbmLrGfJwLVlFJJJyVKZST0=; b=hZpOg9vEhhkbF+Ha0X6v40b2VMAvHNhCNPtQkK7jUqzT+JhqnqmZCsY4/y8Sjb3OtwI4mr E7neGS2ehBfjCHwCfOEQ6GuJtxlSSnzxU3A2wfH8jR20U+NyUEBjOFGoI8oqRp3VgjvHOG XkFfDMMxe0JmRFmGroTJRfb5+2VBO1k= Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-260-e52gERjiP4muMjHPc2yPAQ-1; Thu, 22 Sep 2022 04:44:55 -0400 X-MC-Unique: e52gERjiP4muMjHPc2yPAQ-1 Received: by mail-ed1-f72.google.com with SMTP id y1-20020a056402358100b00451b144e23eso6139633edc.18 for ; Thu, 22 Sep 2022 01:44:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=q2MdUQ+cskiOFqdTJdIqCbmLrGfJwLVlFJJJyVKZST0=; b=MNBzO8E/yf9Vb1pM4KDiZrwbMRWyvBaUb6YCFr+kGh2U+/jsvggxO35VrOVQ0irq2f obOzjd8Atv3+obMnadU+9Vi0lSuEICoNe7SBeo+BgBOBfBng4LsVeDQTDaHJT3bl3niv SrGRDYSJzo97/p0CndzjCX+RSlszyOXxoPN1QCtS/IFfUwm1oimoYTPERpfcrGzSG415 IO6fBSWC+w5I0GezDR0gGAvJDwQ7Bibvj9dMmBSYTeacFpYKYf33NS+qJD/qTRoJPlgs 7R/2L5apkjV0MUMoRjqnfBnHPWHhejTWugwhlugh2j1Vk3CyEmWLo9A6QE93P7lgD0GQ FdKA== X-Gm-Message-State: ACrzQf154peDksInun0la7E7RqJNhdeAv7D/8/vmwxEY5XqxBAHlqB+C TCH5VSNuE12HJUsSfVYyBZ8MXZatr5fMfhRs1SKCfzAtL1zoHKc/KpSxsvc18VarddlWrLQcnWc cATb/WOIWEGY2QYXxYP/J8h+xGF8xx38UEjfK+Nu+Y1TbQj5ZOPLLOfSNZKvftuPHWxx2x60f7W nqbA== X-Received: by 2002:a17:907:a40c:b0:781:53dd:27fd with SMTP id sg12-20020a170907a40c00b0078153dd27fdmr1846871ejc.261.1663836293810; Thu, 22 Sep 2022 01:44:53 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5EOsg89DoiZJRR0cGBkgX2BhdVx/pNpXNSIrV8Tyan5OQ91mNaU83SqMVwLrjojl/fTzkeWw== X-Received: by 2002:a17:907:a40c:b0:781:53dd:27fd with SMTP id sg12-20020a170907a40c00b0078153dd27fdmr1846854ejc.261.1663836293564; Thu, 22 Sep 2022 01:44:53 -0700 (PDT) Received: from miu.piliscsaba.redhat.com (193-226-214-223.pool.digikabel.hu. [193.226.214.223]) by smtp.gmail.com with ESMTPSA id h15-20020a170906718f00b00730b3bdd8d7sm2297942ejk.179.2022.09.22.01.44.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Sep 2022 01:44:52 -0700 (PDT) From: Miklos Szeredi To: linux-fsdevel@vger.kernel.org Cc: Al Viro , Christian Brauner , Amir Goldstein , David Howells , Yu-li Lin , Chirantan Ekbote Subject: [PATCH v4 08/10] vfs: move open right after ->tmpfile() Date: Thu, 22 Sep 2022 10:44:40 +0200 Message-Id: <20220922084442.2401223-9-mszeredi@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220922084442.2401223-1-mszeredi@redhat.com> References: <20220922084442.2401223-1-mszeredi@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Create a helper finish_open_simple() that opens the file with the original dentry. Handle the error case here as well to simplify callers. Call this helper right after ->tmpfile() is called. Next patch will change the tmpfile API and move this call into tmpfile instances. Signed-off-by: Miklos Szeredi --- fs/namei.c | 83 ++++++++++++++++++---------------------------- include/linux/fs.h | 9 +++++ 2 files changed, 42 insertions(+), 50 deletions(-) diff --git a/fs/namei.c b/fs/namei.c index 03ad4e55fb26..fea56fe9f306 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -3583,44 +3583,44 @@ static int do_open(struct nameidata *nd, * On non-idmapped mounts or if permission checking is to be performed on the * raw inode simply passs init_user_ns. */ -static struct dentry *vfs_tmpfile(struct user_namespace *mnt_userns, - struct dentry *dentry, umode_t mode, int open_flag) +static int vfs_tmpfile(struct user_namespace *mnt_userns, + const struct path *parentpath, + struct file *file, umode_t mode) { - struct dentry *child = NULL; - struct inode *dir = dentry->d_inode; + struct dentry *child; + struct inode *dir = d_inode(parentpath->dentry); struct inode *inode; int error; /* we want directory to be writable */ error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC); if (error) - goto out_err; - error = -EOPNOTSUPP; + return error; if (!dir->i_op->tmpfile) - goto out_err; - error = -ENOMEM; - child = d_alloc(dentry, &slash_name); + return -EOPNOTSUPP; + child = d_alloc(parentpath->dentry, &slash_name); if (unlikely(!child)) - goto out_err; + return -ENOMEM; + file->f_path.mnt = parentpath->mnt; + file->f_path.dentry = child; mode = vfs_prepare_mode(mnt_userns, dir, mode, mode, mode); error = dir->i_op->tmpfile(mnt_userns, dir, child, mode); + error = finish_open_simple(file, error); + dput(child); if (error) - goto out_err; - error = -ENOENT; - inode = child->d_inode; - if (unlikely(!inode)) - goto out_err; - if (!(open_flag & O_EXCL)) { + return error; + /* Don't check for other permissions, the inode was just created */ + error = may_open(mnt_userns, &file->f_path, 0, file->f_flags); + if (error) + return error; + inode = file_inode(file); + if (!(file->f_flags & O_EXCL)) { spin_lock(&inode->i_lock); inode->i_state |= I_LINKABLE; spin_unlock(&inode->i_lock); } ima_post_create_tmpfile(mnt_userns, inode); - return child; - -out_err: - dput(child); - return ERR_PTR(error); + return 0; } /** @@ -3641,25 +3641,15 @@ struct file *vfs_tmpfile_open(struct user_namespace *mnt_userns, { struct file *file; int error; - struct path path = { .mnt = parentpath->mnt }; - - path.dentry = vfs_tmpfile(mnt_userns, parentpath->dentry, mode, open_flag); - if (IS_ERR(path.dentry)) - return ERR_CAST(path.dentry); - - error = may_open(mnt_userns, &path, 0, open_flag); - file = ERR_PTR(error); - if (error) - goto out_dput; - - /* - * This relies on the "noaccount" property of fake open, otherwise - * equivalent to dentry_open(). - */ - file = open_with_fake_path(&path, open_flag, d_inode(path.dentry), cred); -out_dput: - dput(path.dentry); + file = alloc_empty_file_noaccount(open_flag, cred); + if (!IS_ERR(file)) { + error = vfs_tmpfile(mnt_userns, parentpath, file, mode); + if (error) { + fput(file); + file = ERR_PTR(error); + } + } return file; } EXPORT_SYMBOL(vfs_tmpfile_open); @@ -3669,26 +3659,19 @@ static int do_tmpfile(struct nameidata *nd, unsigned flags, struct file *file) { struct user_namespace *mnt_userns; - struct dentry *child; struct path path; int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path); + if (unlikely(error)) return error; error = mnt_want_write(path.mnt); if (unlikely(error)) goto out; mnt_userns = mnt_user_ns(path.mnt); - child = vfs_tmpfile(mnt_userns, path.dentry, op->mode, op->open_flag); - error = PTR_ERR(child); - if (IS_ERR(child)) + error = vfs_tmpfile(mnt_userns, &path, file, op->mode); + if (error) goto out2; - dput(path.dentry); - path.dentry = child; - audit_inode(nd->name, child, 0); - /* Don't check for other permissions, the inode was just created */ - error = may_open(mnt_userns, &path, 0, op->open_flag); - if (!error) - error = vfs_open(&path, file); + audit_inode(nd->name, file->f_path.dentry, 0); out2: mnt_drop_write(path.mnt); out: diff --git a/include/linux/fs.h b/include/linux/fs.h index 02646542f6bb..a3c50869e79b 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2780,6 +2780,15 @@ extern int finish_open(struct file *file, struct dentry *dentry, int (*open)(struct inode *, struct file *)); extern int finish_no_open(struct file *file, struct dentry *dentry); +/* Helper for the simple case when original dentry is used */ +static inline int finish_open_simple(struct file *file, int error) +{ + if (error) + return error; + + return finish_open(file, file->f_path.dentry, NULL); +} + /* fs/dcache.c */ extern void __init vfs_caches_init_early(void); extern void __init vfs_caches_init(void); From patchwork Thu Sep 22 08:44:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miklos Szeredi X-Patchwork-Id: 12984745 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5BB5BC6FA82 for ; Thu, 22 Sep 2022 08:45:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230368AbiIVIpk (ORCPT ); Thu, 22 Sep 2022 04:45:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44678 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230365AbiIVIpA (ORCPT ); Thu, 22 Sep 2022 04:45:00 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ED01882868 for ; Thu, 22 Sep 2022 01:44:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1663836298; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CTLscTj53fcBRlA0UnhI+TBVRL+Som3XHnKIkQg9opM=; b=JN0JyxR5BqOrxAFjh8cVUIg1aSO9QrKnrRxgfBOt2RJMx40Y7yneGUFp+/IRmvfDrn6hM4 0ht9kT/oEPF+lRbn1AYeC06ARhKpBl1rbqILx7Y/7DEI8DSbfWpqeJqz2wUDtvlSQSDb2i BiLDbwx/8uzr1zNTI8ELjRG1sJqr75s= Received: from mail-ej1-f71.google.com (mail-ej1-f71.google.com [209.85.218.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-347-wp4yV7vKPl23BtPsuBucDQ-1; Thu, 22 Sep 2022 04:44:57 -0400 X-MC-Unique: wp4yV7vKPl23BtPsuBucDQ-1 Received: by mail-ej1-f71.google.com with SMTP id qb30-20020a1709077e9e00b0077d1271283eso4136441ejc.2 for ; Thu, 22 Sep 2022 01:44:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=CTLscTj53fcBRlA0UnhI+TBVRL+Som3XHnKIkQg9opM=; b=b6Gq0BIINlGidSolmnId1mct1V5wod0Bz+VDRFZeT8GbsPpZBKhG5WdUNmfX8yBKK+ qB0xsBJbn7wtD7oWNz0XvILhBhnw1KDTXMx/G+sW3I4UTk0zNIJ89bIBesoaF6ig03ZT lzDl1HZHN1TH/a4sefjlW2qtg/xqsEOtUCK+BHTogzchnKFs4wbq0vxYEg+Y+r9jcaLe 4skLtpt1pTss+hw2zFWHJB3c8jnMxsmPWDzwhrsI87pUNai2GhTr8+Hf4YfHS1kfocBb dP+pAcBNjmFxvWHKpi+kztpXMCXEZPy2vjR2kEJybnHspVg3MQIXyrdyRNwCbJBiIz7R q1uA== X-Gm-Message-State: ACrzQf3mRfHre6WXksITu5OBJpa7U+o31L5ycWT9ia6rIJbGdzjm81um 7CxnXsZDQ3d0140Ej3uEVunGM1NjjnXvhFRmi3ZIKs70RmEKnD7Q+ooodZAblnlsq00sNqm0f/W XofRj/1T4kR2ybPMjvGWqSptSlJxhUTD23dMesHLLt0col7qsbPQL4GWD77HPVE4WYoyPB6yUAl XQKg== X-Received: by 2002:a05:6402:1849:b0:453:ba03:9e41 with SMTP id v9-20020a056402184900b00453ba039e41mr2157988edy.202.1663836295257; Thu, 22 Sep 2022 01:44:55 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6oItFeP3+PcQC+2ZlCOUJC5ygCtzYnvpZsY9r9C/ppKMNLJOr5TXqZO82lon1IebrzWjAOmw== X-Received: by 2002:a05:6402:1849:b0:453:ba03:9e41 with SMTP id v9-20020a056402184900b00453ba039e41mr2157945edy.202.1663836294678; Thu, 22 Sep 2022 01:44:54 -0700 (PDT) Received: from miu.piliscsaba.redhat.com (193-226-214-223.pool.digikabel.hu. [193.226.214.223]) by smtp.gmail.com with ESMTPSA id h15-20020a170906718f00b00730b3bdd8d7sm2297942ejk.179.2022.09.22.01.44.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Sep 2022 01:44:54 -0700 (PDT) From: Miklos Szeredi To: linux-fsdevel@vger.kernel.org Cc: Al Viro , Christian Brauner , Amir Goldstein , David Howells , Yu-li Lin , Chirantan Ekbote Subject: [PATCH v4 09/10] vfs: open inside ->tmpfile() Date: Thu, 22 Sep 2022 10:44:41 +0200 Message-Id: <20220922084442.2401223-10-mszeredi@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220922084442.2401223-1-mszeredi@redhat.com> References: <20220922084442.2401223-1-mszeredi@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org This is in preparation for adding tmpfile support to fuse, which requires that the tmpfile creation and opening are done as a single operation. Replace the 'struct dentry *' argument of i_op->tmpfile with 'struct file *'. Call finish_open_simple() as the last thing in ->tmpfile() instances (may be omitted in the error case). Change d_tmpfile() argument to 'struct file *' as well to make callers more readable. Signed-off-by: Miklos Szeredi Reviewed-by: Christian Brauner (Microsoft) --- Documentation/filesystems/locking.rst | 3 ++- Documentation/filesystems/porting.rst | 10 ++++++++++ Documentation/filesystems/vfs.rst | 6 ++++-- fs/bad_inode.c | 2 +- fs/btrfs/inode.c | 8 ++++---- fs/dcache.c | 4 +++- fs/ext2/namei.c | 6 +++--- fs/ext4/namei.c | 6 +++--- fs/f2fs/namei.c | 13 ++++++++----- fs/hugetlbfs/inode.c | 6 +++--- fs/minix/namei.c | 6 +++--- fs/namei.c | 3 +-- fs/ramfs/inode.c | 6 +++--- fs/ubifs/dir.c | 7 ++++--- fs/udf/namei.c | 6 +++--- fs/xfs/xfs_iops.c | 16 +++++++++------- include/linux/dcache.h | 3 ++- include/linux/fs.h | 2 +- mm/shmem.c | 6 +++--- 19 files changed, 70 insertions(+), 49 deletions(-) diff --git a/Documentation/filesystems/locking.rst b/Documentation/filesystems/locking.rst index 4bb2627026ec..8f737e76935c 100644 --- a/Documentation/filesystems/locking.rst +++ b/Documentation/filesystems/locking.rst @@ -79,7 +79,8 @@ prototypes:: int (*atomic_open)(struct inode *, struct dentry *, struct file *, unsigned open_flag, umode_t create_mode); - int (*tmpfile) (struct inode *, struct dentry *, umode_t); + int (*tmpfile) (struct user_namespace *, struct inode *, + struct file *, umode_t); int (*fileattr_set)(struct user_namespace *mnt_userns, struct dentry *dentry, struct fileattr *fa); int (*fileattr_get)(struct dentry *dentry, struct fileattr *fa); diff --git a/Documentation/filesystems/porting.rst b/Documentation/filesystems/porting.rst index aee9aaf9f3df..af138241bb4b 100644 --- a/Documentation/filesystems/porting.rst +++ b/Documentation/filesystems/porting.rst @@ -922,3 +922,13 @@ is provided - file_open_root_mnt(). In-tree users adjusted. no_llseek is gone; don't set .llseek to that - just leave it NULL instead. Checks for "does that file have llseek(2), or should it fail with ESPIPE" should be done by looking at FMODE_LSEEK in file->f_mode. + +--- + +**mandatory** + +Calling conventions for ->tmpfile() have changed. It now takes a struct +file pointer instead of struct dentry pointer. d_tmpfile() is similarly +changed to simplify callers. The passed file is in a non-open state and on +success must be opened before returning (e.g. by calling +finish_open_simple()). diff --git a/Documentation/filesystems/vfs.rst b/Documentation/filesystems/vfs.rst index 6cd6953e175b..71b0b8114b18 100644 --- a/Documentation/filesystems/vfs.rst +++ b/Documentation/filesystems/vfs.rst @@ -439,7 +439,7 @@ As of kernel 2.6.22, the following members are defined: void (*update_time)(struct inode *, struct timespec *, int); int (*atomic_open)(struct inode *, struct dentry *, struct file *, unsigned open_flag, umode_t create_mode); - int (*tmpfile) (struct user_namespace *, struct inode *, struct dentry *, umode_t); + int (*tmpfile) (struct user_namespace *, struct inode *, struct file *, umode_t); int (*set_acl)(struct user_namespace *, struct inode *, struct posix_acl *, int); int (*fileattr_set)(struct user_namespace *mnt_userns, struct dentry *dentry, struct fileattr *fa); @@ -589,7 +589,9 @@ otherwise noted. ``tmpfile`` called in the end of O_TMPFILE open(). Optional, equivalent to atomically creating, opening and unlinking a file in given - directory. + directory. On success needs to return with the file already + open; this can be done by calling finish_open_simple() right at + the end. ``fileattr_get`` called on ioctl(FS_IOC_GETFLAGS) and ioctl(FS_IOC_FSGETXATTR) to diff --git a/fs/bad_inode.c b/fs/bad_inode.c index 12b8fdcc445b..9d1cde8066cf 100644 --- a/fs/bad_inode.c +++ b/fs/bad_inode.c @@ -147,7 +147,7 @@ static int bad_inode_atomic_open(struct inode *inode, struct dentry *dentry, } static int bad_inode_tmpfile(struct user_namespace *mnt_userns, - struct inode *inode, struct dentry *dentry, + struct inode *inode, struct file *file, umode_t mode) { return -EIO; diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 1372210869b1..416373721085 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -10168,7 +10168,7 @@ static int btrfs_permission(struct user_namespace *mnt_userns, } static int btrfs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, - struct dentry *dentry, umode_t mode) + struct file *file, umode_t mode) { struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); struct btrfs_trans_handle *trans; @@ -10176,7 +10176,7 @@ static int btrfs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, struct inode *inode; struct btrfs_new_inode_args new_inode_args = { .dir = dir, - .dentry = dentry, + .dentry = file->f_path.dentry, .orphan = true, }; unsigned int trans_num_items; @@ -10213,7 +10213,7 @@ static int btrfs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, set_nlink(inode, 1); if (!ret) { - d_tmpfile(dentry, inode); + d_tmpfile(file, inode); unlock_new_inode(inode); mark_inode_dirty(inode); } @@ -10225,7 +10225,7 @@ static int btrfs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, out_inode: if (ret) iput(inode); - return ret; + return finish_open_simple(file, ret); } void btrfs_set_range_writeback(struct btrfs_inode *inode, u64 start, u64 end) diff --git a/fs/dcache.c b/fs/dcache.c index bb0c4d0038db..89dc61389102 100644 --- a/fs/dcache.c +++ b/fs/dcache.c @@ -3258,8 +3258,10 @@ void d_genocide(struct dentry *parent) EXPORT_SYMBOL(d_genocide); -void d_tmpfile(struct dentry *dentry, struct inode *inode) +void d_tmpfile(struct file *file, struct inode *inode) { + struct dentry *dentry = file->f_path.dentry; + inode_dec_link_count(inode); BUG_ON(dentry->d_name.name != dentry->d_iname || !hlist_unhashed(&dentry->d_u.d_alias) || diff --git a/fs/ext2/namei.c b/fs/ext2/namei.c index 5fd9a22d2b70..9125eab85146 100644 --- a/fs/ext2/namei.c +++ b/fs/ext2/namei.c @@ -120,7 +120,7 @@ static int ext2_create (struct user_namespace * mnt_userns, } static int ext2_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, - struct dentry *dentry, umode_t mode) + struct file *file, umode_t mode) { struct inode *inode = ext2_new_inode(dir, mode, NULL); if (IS_ERR(inode)) @@ -128,9 +128,9 @@ static int ext2_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, ext2_set_file_ops(inode); mark_inode_dirty(inode); - d_tmpfile(dentry, inode); + d_tmpfile(file, inode); unlock_new_inode(inode); - return 0; + return finish_open_simple(file, 0); } static int ext2_mknod (struct user_namespace * mnt_userns, struct inode * dir, diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c index 3a31b662f661..9c3fde633a6e 100644 --- a/fs/ext4/namei.c +++ b/fs/ext4/namei.c @@ -2849,7 +2849,7 @@ static int ext4_mknod(struct user_namespace *mnt_userns, struct inode *dir, } static int ext4_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, - struct dentry *dentry, umode_t mode) + struct file *file, umode_t mode) { handle_t *handle; struct inode *inode; @@ -2871,7 +2871,7 @@ static int ext4_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, inode->i_op = &ext4_file_inode_operations; inode->i_fop = &ext4_file_operations; ext4_set_aops(inode); - d_tmpfile(dentry, inode); + d_tmpfile(file, inode); err = ext4_orphan_add(handle, inode); if (err) goto err_unlock_inode; @@ -2882,7 +2882,7 @@ static int ext4_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, ext4_journal_stop(handle); if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries)) goto retry; - return err; + return finish_open_simple(file, err); err_unlock_inode: ext4_journal_stop(handle); unlock_new_inode(inode); diff --git a/fs/f2fs/namei.c b/fs/f2fs/namei.c index bf00d5057abb..d5065a5af1f8 100644 --- a/fs/f2fs/namei.c +++ b/fs/f2fs/namei.c @@ -845,7 +845,7 @@ static int f2fs_mknod(struct user_namespace *mnt_userns, struct inode *dir, } static int __f2fs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, - struct dentry *dentry, umode_t mode, bool is_whiteout, + struct file *file, umode_t mode, bool is_whiteout, struct inode **new_inode) { struct f2fs_sb_info *sbi = F2FS_I_SB(dir); @@ -892,8 +892,8 @@ static int __f2fs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, inode->i_state |= I_LINKABLE; spin_unlock(&inode->i_lock); } else { - if (dentry) - d_tmpfile(dentry, inode); + if (file) + d_tmpfile(file, inode); else f2fs_i_links_write(inode, false); } @@ -915,16 +915,19 @@ static int __f2fs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, } static int f2fs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, - struct dentry *dentry, umode_t mode) + struct file *file, umode_t mode) { struct f2fs_sb_info *sbi = F2FS_I_SB(dir); + int err; if (unlikely(f2fs_cp_error(sbi))) return -EIO; if (!f2fs_is_checkpoint_ready(sbi)) return -ENOSPC; - return __f2fs_tmpfile(mnt_userns, dir, dentry, mode, false, NULL); + err = __f2fs_tmpfile(mnt_userns, dir, file, mode, false, NULL); + + return finish_open_simple(file, err); } static int f2fs_create_whiteout(struct user_namespace *mnt_userns, diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index 0b458beb318c..026daa8fc221 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -917,7 +917,7 @@ static int hugetlbfs_create(struct user_namespace *mnt_userns, } static int hugetlbfs_tmpfile(struct user_namespace *mnt_userns, - struct inode *dir, struct dentry *dentry, + struct inode *dir, struct file *file, umode_t mode) { struct inode *inode; @@ -926,8 +926,8 @@ static int hugetlbfs_tmpfile(struct user_namespace *mnt_userns, if (!inode) return -ENOSPC; dir->i_ctime = dir->i_mtime = current_time(dir); - d_tmpfile(dentry, inode); - return 0; + d_tmpfile(file, inode); + return finish_open_simple(file, 0); } static int hugetlbfs_symlink(struct user_namespace *mnt_userns, diff --git a/fs/minix/namei.c b/fs/minix/namei.c index 937fa5fae2b8..8afdc408ca4f 100644 --- a/fs/minix/namei.c +++ b/fs/minix/namei.c @@ -53,16 +53,16 @@ static int minix_mknod(struct user_namespace *mnt_userns, struct inode *dir, } static int minix_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, - struct dentry *dentry, umode_t mode) + struct file *file, umode_t mode) { int error; struct inode *inode = minix_new_inode(dir, mode, &error); if (inode) { minix_set_inode(inode, 0); mark_inode_dirty(inode); - d_tmpfile(dentry, inode); + d_tmpfile(file, inode); } - return error; + return finish_open_simple(file, error); } static int minix_create(struct user_namespace *mnt_userns, struct inode *dir, diff --git a/fs/namei.c b/fs/namei.c index fea56fe9f306..c4ca2c3e4c4b 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -3604,8 +3604,7 @@ static int vfs_tmpfile(struct user_namespace *mnt_userns, file->f_path.mnt = parentpath->mnt; file->f_path.dentry = child; mode = vfs_prepare_mode(mnt_userns, dir, mode, mode, mode); - error = dir->i_op->tmpfile(mnt_userns, dir, child, mode); - error = finish_open_simple(file, error); + error = dir->i_op->tmpfile(mnt_userns, dir, file, mode); dput(child); if (error) return error; diff --git a/fs/ramfs/inode.c b/fs/ramfs/inode.c index bc66d0173e33..b3257e852820 100644 --- a/fs/ramfs/inode.c +++ b/fs/ramfs/inode.c @@ -146,15 +146,15 @@ static int ramfs_symlink(struct user_namespace *mnt_userns, struct inode *dir, } static int ramfs_tmpfile(struct user_namespace *mnt_userns, - struct inode *dir, struct dentry *dentry, umode_t mode) + struct inode *dir, struct file *file, umode_t mode) { struct inode *inode; inode = ramfs_get_inode(dir->i_sb, dir, mode, 0); if (!inode) return -ENOSPC; - d_tmpfile(dentry, inode); - return 0; + d_tmpfile(file, inode); + return finish_open_simple(file, 0); } static const struct inode_operations ramfs_dir_inode_operations = { diff --git a/fs/ubifs/dir.c b/fs/ubifs/dir.c index 86151889548e..f59acd6a3615 100644 --- a/fs/ubifs/dir.c +++ b/fs/ubifs/dir.c @@ -424,8 +424,9 @@ static void unlock_2_inodes(struct inode *inode1, struct inode *inode2) } static int ubifs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, - struct dentry *dentry, umode_t mode) + struct file *file, umode_t mode) { + struct dentry *dentry = file->f_path.dentry; struct inode *inode; struct ubifs_info *c = dir->i_sb->s_fs_info; struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1, @@ -475,7 +476,7 @@ static int ubifs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, mutex_lock(&ui->ui_mutex); insert_inode_hash(inode); - d_tmpfile(dentry, inode); + d_tmpfile(file, inode); ubifs_assert(c, ui->dirty); instantiated = 1; @@ -489,7 +490,7 @@ static int ubifs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, ubifs_release_budget(c, &req); - return 0; + return finish_open_simple(file, 0); out_cancel: unlock_2_inodes(dir, inode); diff --git a/fs/udf/namei.c b/fs/udf/namei.c index b3d5f97f16cd..fb4c30e05245 100644 --- a/fs/udf/namei.c +++ b/fs/udf/namei.c @@ -626,7 +626,7 @@ static int udf_create(struct user_namespace *mnt_userns, struct inode *dir, } static int udf_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, - struct dentry *dentry, umode_t mode) + struct file *file, umode_t mode) { struct inode *inode = udf_new_inode(dir, mode); @@ -640,9 +640,9 @@ static int udf_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, inode->i_op = &udf_file_inode_operations; inode->i_fop = &udf_file_operations; mark_inode_dirty(inode); - d_tmpfile(dentry, inode); + d_tmpfile(file, inode); unlock_new_inode(inode); - return 0; + return finish_open_simple(file, 0); } static int udf_mknod(struct user_namespace *mnt_userns, struct inode *dir, diff --git a/fs/xfs/xfs_iops.c b/fs/xfs/xfs_iops.c index 45518b8c613c..764409c466fd 100644 --- a/fs/xfs/xfs_iops.c +++ b/fs/xfs/xfs_iops.c @@ -167,7 +167,7 @@ xfs_generic_create( struct dentry *dentry, umode_t mode, dev_t rdev, - bool tmpfile) /* unnamed file */ + struct file *tmpfile) /* unnamed file */ { struct inode *inode; struct xfs_inode *ip = NULL; @@ -234,7 +234,7 @@ xfs_generic_create( * d_tmpfile can immediately set it back to zero. */ set_nlink(inode, 1); - d_tmpfile(dentry, inode); + d_tmpfile(tmpfile, inode); } else d_instantiate(dentry, inode); @@ -261,7 +261,7 @@ xfs_vn_mknod( umode_t mode, dev_t rdev) { - return xfs_generic_create(mnt_userns, dir, dentry, mode, rdev, false); + return xfs_generic_create(mnt_userns, dir, dentry, mode, rdev, NULL); } STATIC int @@ -272,7 +272,7 @@ xfs_vn_create( umode_t mode, bool flags) { - return xfs_generic_create(mnt_userns, dir, dentry, mode, 0, false); + return xfs_generic_create(mnt_userns, dir, dentry, mode, 0, NULL); } STATIC int @@ -283,7 +283,7 @@ xfs_vn_mkdir( umode_t mode) { return xfs_generic_create(mnt_userns, dir, dentry, mode | S_IFDIR, 0, - false); + NULL); } STATIC struct dentry * @@ -1080,10 +1080,12 @@ STATIC int xfs_vn_tmpfile( struct user_namespace *mnt_userns, struct inode *dir, - struct dentry *dentry, + struct file *file, umode_t mode) { - return xfs_generic_create(mnt_userns, dir, dentry, mode, 0, true); + int err = xfs_generic_create(mnt_userns, dir, file->f_path.dentry, mode, 0, file); + + return finish_open_simple(file, err); } static const struct inode_operations xfs_inode_operations = { diff --git a/include/linux/dcache.h b/include/linux/dcache.h index 92c78ed02b54..bde9f8ff8869 100644 --- a/include/linux/dcache.h +++ b/include/linux/dcache.h @@ -16,6 +16,7 @@ #include struct path; +struct file; struct vfsmount; /* @@ -250,7 +251,7 @@ extern struct dentry * d_make_root(struct inode *); /* - the ramfs-type tree */ extern void d_genocide(struct dentry *); -extern void d_tmpfile(struct dentry *, struct inode *); +extern void d_tmpfile(struct file *, struct inode *); extern struct dentry *d_find_alias(struct inode *); extern void d_prune_aliases(struct inode *); diff --git a/include/linux/fs.h b/include/linux/fs.h index a3c50869e79b..8218d9964ff8 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2168,7 +2168,7 @@ struct inode_operations { struct file *, unsigned open_flag, umode_t create_mode); int (*tmpfile) (struct user_namespace *, struct inode *, - struct dentry *, umode_t); + struct file *, umode_t); int (*set_acl)(struct user_namespace *, struct inode *, struct posix_acl *, int); int (*fileattr_set)(struct user_namespace *mnt_userns, diff --git a/mm/shmem.c b/mm/shmem.c index 42e5888bf84d..f63c51bc373e 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2912,7 +2912,7 @@ shmem_mknod(struct user_namespace *mnt_userns, struct inode *dir, static int shmem_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, - struct dentry *dentry, umode_t mode) + struct file *file, umode_t mode) { struct inode *inode; int error = -ENOSPC; @@ -2927,9 +2927,9 @@ shmem_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, error = simple_acl_create(dir, inode); if (error) goto out_iput; - d_tmpfile(dentry, inode); + d_tmpfile(file, inode); } - return error; + return finish_open_simple(file, error); out_iput: iput(inode); return error; From patchwork Thu Sep 22 08:44:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miklos Szeredi X-Patchwork-Id: 12984744 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1E320C6FA8B for ; Thu, 22 Sep 2022 08:45:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230414AbiIVIpd (ORCPT ); Thu, 22 Sep 2022 04:45:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44264 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229609AbiIVIpA (ORCPT ); Thu, 22 Sep 2022 04:45:00 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1B21FABD47 for ; Thu, 22 Sep 2022 01:44:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1663836298; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sCqc/9Xx1Mg6CDOhiBaD4VyKFWMPu9fiwnLMKTncAc0=; b=ibZIvd5xhh6ANlLwn//Kpt5jE2kJLVTC1L9IqJks9eJAyDiOmxnFHj4WP6mNKmjWIPYVTb jnj0k7FKhg+tyW75MKrIB2+eiZCaJxvXpElCw+57dzkqHb77OCZnh/2k4xVA0CVOoeFzoy o7PinL0mA6BlYvtjyTa36S7wHd5liCk= Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-626-JCr7Qe78O8SdGXVhS1Yk4g-1; Thu, 22 Sep 2022 04:44:57 -0400 X-MC-Unique: JCr7Qe78O8SdGXVhS1Yk4g-1 Received: by mail-ed1-f72.google.com with SMTP id h13-20020a056402280d00b004528c8400afso6078117ede.6 for ; Thu, 22 Sep 2022 01:44:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=sCqc/9Xx1Mg6CDOhiBaD4VyKFWMPu9fiwnLMKTncAc0=; b=65jAa2i7qI2MRPcaUZO+lF2XM2FFqchiX7wnuEDH3wJFeez0Tiq8he5f3qY0UyRBG6 uiM0XRqYBZdxBpUhMDNi8cPcadeGiEAOzwLypQ2rZ2nH5lRT8yuIfdQ/3oEyDNCcAe0L +ze2BONV01CfGllPsinOsnWOhPhrcbh9FNWqG5YiRw0lQenLCA7Qgn9tGQKQo9sg1qfx WEjvVF9ik9L2lIpLDgHEiV6WX8QGcgLWco4fDqmjL/vI6wKE9SSTnU07Hrr9toeejSeH WQo4hI+W/lhSskNbNedL2qwvcvGLxZFWs1WSEnogpq9AYtLMv4kUIpKr5DRrMKwA8UUn 2fAA== X-Gm-Message-State: ACrzQf1iWHeVXWfrNHNAZEA06t40QvC2ocurdKQm6AisKH8Qk1yEDvqD UDkplsxwfLpXBkcJX5/A4M2lzgwPGZ+/vm9DA3qyLm6y6q8xVUEDJ/GT4bZ9NXTtb/uvVVFmgoP T5D8FcJ0IPF9oucJ2B5XVvbB2DDT1no8/3dk1/HuWYp5XXadrr1Zs32echU/iRfKW3zdLVINE7c DZCA== X-Received: by 2002:a17:906:974d:b0:780:2c07:7617 with SMTP id o13-20020a170906974d00b007802c077617mr1805128ejy.707.1663836295840; Thu, 22 Sep 2022 01:44:55 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7Kfio+BwfvFTeZq/Wq4V2OTvFkLsQN+9/TKY21Iueoy5yJCvYFjHbjrm04GCk4k6ZwBGXG9g== X-Received: by 2002:a17:906:974d:b0:780:2c07:7617 with SMTP id o13-20020a170906974d00b007802c077617mr1805110ejy.707.1663836295624; Thu, 22 Sep 2022 01:44:55 -0700 (PDT) Received: from miu.piliscsaba.redhat.com (193-226-214-223.pool.digikabel.hu. [193.226.214.223]) by smtp.gmail.com with ESMTPSA id h15-20020a170906718f00b00730b3bdd8d7sm2297942ejk.179.2022.09.22.01.44.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Sep 2022 01:44:55 -0700 (PDT) From: Miklos Szeredi To: linux-fsdevel@vger.kernel.org Cc: Al Viro , Christian Brauner , Amir Goldstein , David Howells , Yu-li Lin , Chirantan Ekbote Subject: [PATCH v4 10/10] fuse: implement ->tmpfile() Date: Thu, 22 Sep 2022 10:44:42 +0200 Message-Id: <20220922084442.2401223-11-mszeredi@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220922084442.2401223-1-mszeredi@redhat.com> References: <20220922084442.2401223-1-mszeredi@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org This is basically equivalent to the FUSE_CREATE operation which creates and opens a regular file. Add a new FUSE_TMPFILE operation, otherwise just reuse the protocol and the code for FUSE_CREATE. Signed-off-by: Miklos Szeredi Acked-by: Christian Brauner (Microsoft) --- fs/fuse/dir.c | 24 +++++++++++++++++++++--- fs/fuse/fuse_i.h | 3 +++ include/uapi/linux/fuse.h | 6 +++++- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/fs/fuse/dir.c b/fs/fuse/dir.c index b585b04e815e..bb97a384dc5d 100644 --- a/fs/fuse/dir.c +++ b/fs/fuse/dir.c @@ -529,7 +529,7 @@ static int get_security_context(struct dentry *entry, umode_t mode, */ static int fuse_create_open(struct inode *dir, struct dentry *entry, struct file *file, unsigned int flags, - umode_t mode) + umode_t mode, u32 opcode) { int err; struct inode *inode; @@ -573,7 +573,7 @@ static int fuse_create_open(struct inode *dir, struct dentry *entry, inarg.open_flags |= FUSE_OPEN_KILL_SUIDGID; } - args.opcode = FUSE_CREATE; + args.opcode = opcode; args.nodeid = get_node_id(dir); args.in_numargs = 2; args.in_args[0].size = sizeof(inarg); @@ -676,7 +676,7 @@ static int fuse_atomic_open(struct inode *dir, struct dentry *entry, if (fc->no_create) goto mknod; - err = fuse_create_open(dir, entry, file, flags, mode); + err = fuse_create_open(dir, entry, file, flags, mode, FUSE_CREATE); if (err == -ENOSYS) { fc->no_create = 1; goto mknod; @@ -802,6 +802,23 @@ static int fuse_create(struct user_namespace *mnt_userns, struct inode *dir, return fuse_mknod(&init_user_ns, dir, entry, mode, 0); } +static int fuse_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, + struct file *file, umode_t mode) +{ + struct fuse_conn *fc = get_fuse_conn(dir); + int err; + + if (fc->no_tmpfile) + return -EOPNOTSUPP; + + err = fuse_create_open(dir, file->f_path.dentry, file, file->f_flags, mode, FUSE_TMPFILE); + if (err == -ENOSYS) { + fc->no_tmpfile = 1; + err = -EOPNOTSUPP; + } + return err; +} + static int fuse_mkdir(struct user_namespace *mnt_userns, struct inode *dir, struct dentry *entry, umode_t mode) { @@ -1913,6 +1930,7 @@ static const struct inode_operations fuse_dir_inode_operations = { .setattr = fuse_setattr, .create = fuse_create, .atomic_open = fuse_atomic_open, + .tmpfile = fuse_tmpfile, .mknod = fuse_mknod, .permission = fuse_permission, .getattr = fuse_getattr, diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h index 488b460e046f..98a9cf531873 100644 --- a/fs/fuse/fuse_i.h +++ b/fs/fuse/fuse_i.h @@ -784,6 +784,9 @@ struct fuse_conn { /* Does the filesystem support per inode DAX? */ unsigned int inode_dax:1; + /* Is tmpfile not implemented by fs? */ + unsigned int no_tmpfile:1; + /** The number of requests waiting for completion */ atomic_t num_waiting; diff --git a/include/uapi/linux/fuse.h b/include/uapi/linux/fuse.h index d6ccee961891..76ee8f9e024a 100644 --- a/include/uapi/linux/fuse.h +++ b/include/uapi/linux/fuse.h @@ -194,6 +194,9 @@ * - add FUSE_SECURITY_CTX init flag * - add security context to create, mkdir, symlink, and mknod requests * - add FUSE_HAS_INODE_DAX, FUSE_ATTR_DAX + * + * 7.37 + * - add FUSE_TMPFILE */ #ifndef _LINUX_FUSE_H @@ -229,7 +232,7 @@ #define FUSE_KERNEL_VERSION 7 /** Minor version number of this interface */ -#define FUSE_KERNEL_MINOR_VERSION 36 +#define FUSE_KERNEL_MINOR_VERSION 37 /** The node ID of the root inode */ #define FUSE_ROOT_ID 1 @@ -537,6 +540,7 @@ enum fuse_opcode { FUSE_SETUPMAPPING = 48, FUSE_REMOVEMAPPING = 49, FUSE_SYNCFS = 50, + FUSE_TMPFILE = 51, /* CUSE specific operations */ CUSE_INIT = 4096,