From patchwork Mon May 2 05:46:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dharmendra Singh X-Patchwork-Id: 12833840 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 12C22C433F5 for ; Mon, 2 May 2022 05:46:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1383366AbiEBFuR (ORCPT ); Mon, 2 May 2022 01:50:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58332 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377965AbiEBFuQ (ORCPT ); Mon, 2 May 2022 01:50:16 -0400 Received: from mail-pg1-x535.google.com (mail-pg1-x535.google.com [IPv6:2607:f8b0:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 72AB33EA9E; Sun, 1 May 2022 22:46:48 -0700 (PDT) Received: by mail-pg1-x535.google.com with SMTP id x12so10946829pgj.7; Sun, 01 May 2022 22:46:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :organization; bh=gOhjSMrjCwKbxjE2hP/4KxUnfkTZuZCh55gMElj3XMU=; b=hXVt2uaKx83KYv4DnHdOY2xnOK+1ASmvWdNb0jINLT1yhrSSVYEyG+8FVs468XcgkE 4rOr9DuQQXpU1PYi1X9WVjzrm3OGfFVsD1L2XYWfKS3XoCmNvGXoIIP50cluBvbuH4lV 8lIsf6vMUlPlL8LiyIBp9WpaoapL3YOCuSVZOWXXMahLHx97GLT8QSAifqajz58G4005 IMv+QA2ktYtdAfKsrEz23NxfZaf69Ts+JXeObO73eVLlJGaNnAETFm+QsmKc4CIKbYoJ iePkNatil7yETgRfLYLzONe+3BKiD9oIVoRXrxsaSmyqE5EPMQgB4vHugFeUrr4d0mPD dBFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:organization; bh=gOhjSMrjCwKbxjE2hP/4KxUnfkTZuZCh55gMElj3XMU=; b=ywqV804m26IHLt9CpDEHLIky9ijvBTY1wMCpBT7O6gori9stK8y9K5KbmD0hfB+g0g weYIWNOfwhM43qiJUhCenRhXZxtUyZGN2W6eE8AwTtFROME8ItgC3tSj+svvQZyZVJcE uCfjNqTwrt5ZHCNO3CThqocmrXUwVEd7RdIg0dqGtZXALNpB0u5b2hfxW4/KnsUfbA8X SBG+7gqoOecWjtrZAr8ErveLE+vWJdpPU1grq8VXAMAvr/u/6MYukaHWeIoLsDR/mWvP 0mRh2WGxCNpmHUL5wUbN3P85RoikomSBy+gFJtepYGMWHbYzDHHeBDp1KHFxjLFxO3dE ZLWQ== X-Gm-Message-State: AOAM533U+BSAXfkL0ikfa1PEty3OIyUnThQM8afZ+ZJd9kP/eFIE6qEK 8yCGIzMeEvV9mWjoe0q5GEZAtbj2Ah9qrA== X-Google-Smtp-Source: ABdhPJxZWYQLQn4lkgn1A4u6tESSLG6jHbD2F5EudzX+9HzON9WoceLp76RaLy3GrDawjV7FEUohrg== X-Received: by 2002:a63:6fc4:0:b0:393:9567:16dc with SMTP id k187-20020a636fc4000000b00393956716dcmr8380956pgc.593.1651470407862; Sun, 01 May 2022 22:46:47 -0700 (PDT) Received: from localhost.localdomain ([123.201.245.164]) by smtp.googlemail.com with ESMTPSA id q20-20020a62e114000000b0050dc76281c8sm3787490pfh.162.2022.05.01.22.46.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 May 2022 22:46:47 -0700 (PDT) From: Dharmendra Singh To: miklos@szeredi.hu Cc: Dharmendra Singh , linux-fsdevel@vger.kernel.org, fuse-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org, bschubert@ddn.com, Dharmendra Singh Subject: [PATCH v3 1/3] FUSE: Implement atomic lookup + create Date: Mon, 2 May 2022 11:16:26 +0530 Message-Id: <20220502054628.25826-2-dharamhans87@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220502054628.25826-1-dharamhans87@gmail.com> References: <20220502054628.25826-1-dharamhans87@gmail.com> Organization: DDN STORAGE Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org From: Dharmendra Singh When we go for creating a file (O_CREAT), we trigger a lookup to FUSE USER SPACE. It is very much likely that file does not exist yet as O_CREAT is passed to open(). This lookup can be avoided and can be performed as part of create call into libfuse. This lookup + create in single call to libfuse and finally to USER SPACE has been named as atomic create. It is expected that USER SPACE create the file, open it and fills in the attributes which are then used to make inode stand/revalidate in the kernel cache. Also if file was newly created(does not exist yet by this time) in USER SPACE then it should be indicated in `struct fuse_file_info` by setting a bit which is again used by libfuse to send some flags back to fuse kernel to indicate that that file was newly created. These flags are used by kernel to indicate changes in parent directory. Fuse kernel automatically detects if atomic create is implemented by libfuse/USER SPACE or not. And depending upon the outcome of this check all further creates are decided to be atomic or non-atomic creates. If libfuse/USER SPACE has not implemented the atomic create operation then by default behaviour remains same i.e we do not optimize lookup calls which are triggered before create calls into libfuse. Signed-off-by: Dharmendra Singh --- fs/fuse/dir.c | 82 +++++++++++++++++++++++++++++++++++---- fs/fuse/fuse_i.h | 3 ++ include/uapi/linux/fuse.h | 3 ++ 3 files changed, 81 insertions(+), 7 deletions(-) diff --git a/fs/fuse/dir.c b/fs/fuse/dir.c index 656e921f3506..cad3322a007f 100644 --- a/fs/fuse/dir.c +++ b/fs/fuse/dir.c @@ -523,7 +523,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, uint32_t opcode) { int err; struct inode *inode; @@ -535,8 +535,10 @@ static int fuse_create_open(struct inode *dir, struct dentry *entry, struct fuse_entry_out outentry; struct fuse_inode *fi; struct fuse_file *ff; + struct dentry *res = NULL; void *security_ctx = NULL; u32 security_ctxlen; + bool atomic_create = (opcode == FUSE_ATOMIC_CREATE ? true : false); /* Userspace expects S_IFREG in create mode */ BUG_ON((mode & S_IFMT) != S_IFREG); @@ -566,7 +568,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); @@ -613,9 +615,44 @@ static int fuse_create_open(struct inode *dir, struct dentry *entry, goto out_err; } kfree(forget); - d_instantiate(entry, inode); + /* + * In atomic create, we skipped lookup and it is very much likely that + * dentry has DCACHE_PAR_LOOKUP flag set on it so call d_splice_alias(). + * Note: Only REG file is allowed under create/atomic create. + */ + /* There is special case when at very first call where we check if + * atomic create is implemented by USER SPACE/libfuse or not, we + * skipped lookup. Now, in case where atomic create is not implemented + * underlying, we fall back to FUSE_CREATE. here we are required to handle + * DCACHE_PAR_LOOKUP flag. + */ + if (!atomic_create && !d_in_lookup(entry) && fm->fc->no_atomic_create) + d_instantiate(entry, inode); + else { + res = d_splice_alias(inode, entry); + if (res) { + /* Close the file in user space, but do not unlink it, + * if it was created - with network file systems other + * clients might have already accessed it. + */ + if (IS_ERR(res)) { + fi = get_fuse_inode(inode); + fuse_sync_release(fi, ff, flags); + fuse_queue_forget(fm->fc, forget, outentry.nodeid, 1); + err = PTR_ERR(res); + goto out_err; + } + /* res is expected to be NULL since its REG file */ + WARN_ON(res); + } + } fuse_change_entry_timeout(entry, &outentry); - fuse_dir_changed(dir); + /* + * In case of atomic create, we want to indicate directory change + * only if USER SPACE actually created the file. + */ + if (!atomic_create || (outopen.open_flags & FOPEN_FILE_CREATED)) + fuse_dir_changed(dir); err = finish_open(file, entry, generic_file_open); if (err) { fi = get_fuse_inode(inode); @@ -634,6 +671,29 @@ static int fuse_create_open(struct inode *dir, struct dentry *entry, return err; } +static int fuse_atomic_create(struct inode *dir, struct dentry *entry, + struct file *file, unsigned int flags, + umode_t mode) +{ + int err; + struct fuse_conn *fc = get_fuse_conn(dir); + + if (fc->no_atomic_create) + return -ENOSYS; + + err = fuse_create_open(dir, entry, file, flags, mode, + FUSE_ATOMIC_CREATE); + /* If atomic create is not implemented then indicate in fc so that next + * request falls back to normal create instead of going into libufse and + * returning with -ENOSYS. + */ + if (err == -ENOSYS) { + if (!fc->no_atomic_create) + fc->no_atomic_create = 1; + } + return err; +} + static int fuse_mknod(struct user_namespace *, struct inode *, struct dentry *, umode_t, dev_t); static int fuse_atomic_open(struct inode *dir, struct dentry *entry, @@ -643,11 +703,12 @@ static int fuse_atomic_open(struct inode *dir, struct dentry *entry, int err; struct fuse_conn *fc = get_fuse_conn(dir); struct dentry *res = NULL; + bool create = flags & O_CREAT ? true : false; if (fuse_is_bad(dir)) return -EIO; - if (d_in_lookup(entry)) { + if ((!create || fc->no_atomic_create) && d_in_lookup(entry)) { res = fuse_lookup(dir, entry, 0); if (IS_ERR(res)) return PTR_ERR(res); @@ -656,7 +717,7 @@ static int fuse_atomic_open(struct inode *dir, struct dentry *entry, entry = res; } - if (!(flags & O_CREAT) || d_really_is_positive(entry)) + if (!create || d_really_is_positive(entry)) goto no_open; /* Only creates */ @@ -665,7 +726,13 @@ 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_atomic_create(dir, entry, file, flags, mode); + /* Libfuse/user space has not implemented atomic create, therefore + * fall back to normal create. + */ + if (err == -ENOSYS) + err = fuse_create_open(dir, entry, file, flags, mode, + FUSE_CREATE); if (err == -ENOSYS) { fc->no_create = 1; goto mknod; @@ -683,6 +750,7 @@ static int fuse_atomic_open(struct inode *dir, struct dentry *entry, } /* + * Code shared between mknod, mkdir, symlink and link */ static int create_new_entry(struct fuse_mount *fm, struct fuse_args *args, diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h index e8e59fbdefeb..d577a591ab16 100644 --- a/fs/fuse/fuse_i.h +++ b/fs/fuse/fuse_i.h @@ -669,6 +669,9 @@ struct fuse_conn { /** Is open/release not implemented by fs? */ unsigned no_open:1; + /** Is atomic create not implemented by fs? */ + unsigned no_atomic_create:1; + /** Is opendir/releasedir not implemented by fs? */ unsigned no_opendir:1; diff --git a/include/uapi/linux/fuse.h b/include/uapi/linux/fuse.h index d6ccee961891..e4b56004b148 100644 --- a/include/uapi/linux/fuse.h +++ b/include/uapi/linux/fuse.h @@ -301,6 +301,7 @@ struct fuse_file_lock { * FOPEN_CACHE_DIR: allow caching this directory * FOPEN_STREAM: the file is stream-like (no file position at all) * FOPEN_NOFLUSH: don't flush data cache on close (unless FUSE_WRITEBACK_CACHE) + * FOPEN_FILE_CREATED: the file was actually created */ #define FOPEN_DIRECT_IO (1 << 0) #define FOPEN_KEEP_CACHE (1 << 1) @@ -308,6 +309,7 @@ struct fuse_file_lock { #define FOPEN_CACHE_DIR (1 << 3) #define FOPEN_STREAM (1 << 4) #define FOPEN_NOFLUSH (1 << 5) +#define FOPEN_FILE_CREATED (1 << 6) /** * INIT request/reply flags @@ -537,6 +539,7 @@ enum fuse_opcode { FUSE_SETUPMAPPING = 48, FUSE_REMOVEMAPPING = 49, FUSE_SYNCFS = 50, + FUSE_ATOMIC_CREATE = 51, /* CUSE specific operations */ CUSE_INIT = 4096, From patchwork Mon May 2 05:46:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dharmendra Singh X-Patchwork-Id: 12833841 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 29D14C433EF for ; Mon, 2 May 2022 05:47:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1383385AbiEBFu3 (ORCPT ); Mon, 2 May 2022 01:50:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59164 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383379AbiEBFu0 (ORCPT ); Mon, 2 May 2022 01:50:26 -0400 Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7DB823EB8D; Sun, 1 May 2022 22:46:53 -0700 (PDT) Received: by mail-pf1-x431.google.com with SMTP id x52so9963880pfu.11; Sun, 01 May 2022 22:46:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :organization; bh=l1dSrjQAuEBdUacWP6Ms303wdXzk6rFGelJiALQ8vx8=; b=D55JiRZ9umtuz62VMtueyD3d6fkFdNkXRf7KXsmvv1dy2tnWVS/yhMXrGH5A9RvN08 7QIkJYGlRAv57/6fA60U0nhkcbjznYuiVsCJkgqA8YgogCUEFsVsA46Qlgee4+zOit6E VdEztuIkyQPjxr7BhunphcXtPmoDnhDKzWLDRchOeb2QWsSQXNciPAZFnSffxZ7FFCGB 7IjAIZhLSOLg1dQphI0EIEjBzTTgA7nTvLQu3it4n3HCP8I4MYpluiZnqWSeT0c/j1wM mdkMSBMJPK8Gw1SteGJMc5y/tZpndfApIYt79Os3v5+PnAYruIFAB+9GfTtLgbk5QGbr oOPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:organization; bh=l1dSrjQAuEBdUacWP6Ms303wdXzk6rFGelJiALQ8vx8=; b=y+VIkljO0qdY7mnIxnNHappxEr2+HK6o8HMdqe2QgT2z4o42TsoB5885egStVxwVAm LNCt1c0orGFxVL/NoOUw4wyHUU/RraNPEeA5tNcECXvRZqaaYok/dsSPH8TKuuC1eb+A X3M5/7uVifjYhWU0YHMoW53d0/3eHyzTEbFIklRrUnpnTFENU5CM5b/e+fSg1kSdXbO9 bzC3PMrmzKIyoavSiRyZq6AwBNW67oxmm0l3yczzEtYoR9UtXNZt4YqtXp5D6uh6GwIe K1Vta9RuNjqCT3wNGdTUf565jNm7dTMtQA2qFv9fHHC5sMRr5G5ZCA7eKUn/1eXilc7U 7sSw== X-Gm-Message-State: AOAM531c2ArdbYM8auX72z6om0wzN+Ps9WOGXqp9XVJkhrUOY9X9gcw9 Ubn9tbNR1EBDDlmdl3KDOTqp//9w47TYQg== X-Google-Smtp-Source: ABdhPJxyz0MqkCml2Qa5P8ZdvRlbRjiUeCvvZywo/Uxu2gizAHsEx4fx+S61GwjXte4ASYYnxSObHw== X-Received: by 2002:a63:6b42:0:b0:3c1:9a7b:cb5a with SMTP id g63-20020a636b42000000b003c19a7bcb5amr8701265pgc.563.1651470412821; Sun, 01 May 2022 22:46:52 -0700 (PDT) Received: from localhost.localdomain ([123.201.245.164]) by smtp.googlemail.com with ESMTPSA id q20-20020a62e114000000b0050dc76281c8sm3787490pfh.162.2022.05.01.22.46.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 May 2022 22:46:52 -0700 (PDT) From: Dharmendra Singh To: miklos@szeredi.hu Cc: Dharmendra Singh , linux-fsdevel@vger.kernel.org, fuse-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org, bschubert@ddn.com, Dharmendra Singh Subject: [PATCH v3 2/3] Implement atomic lookup + open Date: Mon, 2 May 2022 11:16:27 +0530 Message-Id: <20220502054628.25826-3-dharamhans87@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220502054628.25826-1-dharamhans87@gmail.com> References: <20220502054628.25826-1-dharamhans87@gmail.com> Organization: DDN STORAGE Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org From: Dharmendra Singh We can optimize aggressive lookups which are triggered when there is normal open for file/dir (dentry is new/negative). Here in this case since we are anyway going to open the file/dir with USER SPACE, avoid this separate lookup call into libfuse and combine it with open call into libfuse. This lookup + open in single call to libfuse has been named as atomic open. It is expected that USER SPACE opens the file and fills in the attributes, which are then used to make inode stand/revalidate in the kernel cache. Signed-off-by: Dharmendra Singh --- fs/fuse/dir.c | 78 ++++++++++++++++++++++++++++++--------- fs/fuse/fuse_i.h | 3 ++ fs/fuse/inode.c | 4 +- include/uapi/linux/fuse.h | 2 + 4 files changed, 69 insertions(+), 18 deletions(-) diff --git a/fs/fuse/dir.c b/fs/fuse/dir.c index cad3322a007f..6879d3a86796 100644 --- a/fs/fuse/dir.c +++ b/fs/fuse/dir.c @@ -516,18 +516,18 @@ static int get_security_context(struct dentry *entry, umode_t mode, } /* - * Atomic create+open operation - * - * If the filesystem doesn't support this, then fall back to separate - * 'mknod' + 'open' requests. + * This is common function for initiating atomic operations into libfuse. + * Currently being used by Atomic create(atomic lookup + create) and + * Atomic open(atomic lookup + open). */ -static int fuse_create_open(struct inode *dir, struct dentry *entry, - struct file *file, unsigned int flags, - umode_t mode, uint32_t opcode) +static int fuse_atomic_do_common(struct inode *dir, struct dentry *entry, + struct dentry **alias, struct file *file, + unsigned int flags, umode_t mode, uint32_t opcode) { int err; struct inode *inode; struct fuse_mount *fm = get_fuse_mount(dir); + struct fuse_conn *fc = get_fuse_conn(dir); FUSE_ARGS(args); struct fuse_forget_link *forget; struct fuse_create_in inarg; @@ -539,9 +539,13 @@ static int fuse_create_open(struct inode *dir, struct dentry *entry, void *security_ctx = NULL; u32 security_ctxlen; bool atomic_create = (opcode == FUSE_ATOMIC_CREATE ? true : false); + bool create_op = (opcode == FUSE_CREATE || + opcode == FUSE_ATOMIC_CREATE) ? true : false; + if (alias) + *alias = NULL; /* Userspace expects S_IFREG in create mode */ - BUG_ON((mode & S_IFMT) != S_IFREG); + BUG_ON(create_op && (mode & S_IFMT) != S_IFREG); forget = fuse_alloc_forget(); err = -ENOMEM; @@ -553,7 +557,7 @@ static int fuse_create_open(struct inode *dir, struct dentry *entry, if (!ff) goto out_put_forget_req; - if (!fm->fc->dont_mask) + if (!fc->dont_mask) mode &= ~current_umask(); flags &= ~O_NOCTTY; @@ -642,8 +646,9 @@ static int fuse_create_open(struct inode *dir, struct dentry *entry, err = PTR_ERR(res); goto out_err; } - /* res is expected to be NULL since its REG file */ - WARN_ON(res); + entry = res; + if (alias) + *alias = res; } } fuse_change_entry_timeout(entry, &outentry); @@ -681,8 +686,8 @@ static int fuse_atomic_create(struct inode *dir, struct dentry *entry, if (fc->no_atomic_create) return -ENOSYS; - err = fuse_create_open(dir, entry, file, flags, mode, - FUSE_ATOMIC_CREATE); + err = fuse_atomic_do_common(dir, entry, NULL, file, flags, mode, + FUSE_ATOMIC_CREATE); /* If atomic create is not implemented then indicate in fc so that next * request falls back to normal create instead of going into libufse and * returning with -ENOSYS. @@ -694,6 +699,29 @@ static int fuse_atomic_create(struct inode *dir, struct dentry *entry, return err; } +static int fuse_do_atomic_open(struct inode *dir, struct dentry *entry, + struct dentry **alias, struct file *file, + unsigned int flags, umode_t mode) +{ + int err; + struct fuse_conn *fc = get_fuse_conn(dir); + + if (!fc->do_atomic_open) + return -ENOSYS; + + err = fuse_atomic_do_common(dir, entry, alias, file, flags, mode, + FUSE_ATOMIC_OPEN); + /* Atomic open imply atomic trunc as well i.e truncate should be performed + * as part of atomic open call itself. + */ + if (!fc->atomic_o_trunc) { + if (fc->do_atomic_open) + fc->atomic_o_trunc = 1; + } + + return err; +} + static int fuse_mknod(struct user_namespace *, struct inode *, struct dentry *, umode_t, dev_t); static int fuse_atomic_open(struct inode *dir, struct dentry *entry, @@ -702,12 +730,23 @@ static int fuse_atomic_open(struct inode *dir, struct dentry *entry, { int err; struct fuse_conn *fc = get_fuse_conn(dir); - struct dentry *res = NULL; + struct dentry *res = NULL, *alias = NULL; bool create = flags & O_CREAT ? true : false; if (fuse_is_bad(dir)) return -EIO; + if (!create) { + err = fuse_do_atomic_open(dir, entry, &alias, + file, flags, mode); + res = alias; + if (!err || err != -ENOSYS) + goto out_dput; + } + /* + * ENOSYS fall back for open- user space does not have full + * atomic open. + */ if ((!create || fc->no_atomic_create) && d_in_lookup(entry)) { res = fuse_lookup(dir, entry, 0); if (IS_ERR(res)) @@ -730,9 +769,14 @@ static int fuse_atomic_open(struct inode *dir, struct dentry *entry, /* Libfuse/user space has not implemented atomic create, therefore * fall back to normal create. */ - if (err == -ENOSYS) - err = fuse_create_open(dir, entry, file, flags, mode, - FUSE_CREATE); + /* Atomic create+open operation + * If the filesystem doesn't support this, then fall back to separate + * 'mknod' + 'open' requests. + */ + if (err == -ENOSYS) { + err = fuse_atomic_do_common(dir, entry, NULL, file, flags, + mode, FUSE_CREATE); + } if (err == -ENOSYS) { fc->no_create = 1; goto mknod; diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h index d577a591ab16..24793b82303f 100644 --- a/fs/fuse/fuse_i.h +++ b/fs/fuse/fuse_i.h @@ -669,6 +669,9 @@ struct fuse_conn { /** Is open/release not implemented by fs? */ unsigned no_open:1; + /** Is atomic open implemented by fs ? */ + unsigned do_atomic_open : 1; + /** Is atomic create not implemented by fs? */ unsigned no_atomic_create:1; diff --git a/fs/fuse/inode.c b/fs/fuse/inode.c index ee846ce371d8..5f667de69115 100644 --- a/fs/fuse/inode.c +++ b/fs/fuse/inode.c @@ -1190,6 +1190,8 @@ static void process_init_reply(struct fuse_mount *fm, struct fuse_args *args, fc->setxattr_ext = 1; if (flags & FUSE_SECURITY_CTX) fc->init_security = 1; + if (flags & FUSE_DO_ATOMIC_OPEN) + fc->do_atomic_open = 1; } else { ra_pages = fc->max_read / PAGE_SIZE; fc->no_lock = 1; @@ -1235,7 +1237,7 @@ void fuse_send_init(struct fuse_mount *fm) FUSE_ABORT_ERROR | FUSE_MAX_PAGES | FUSE_CACHE_SYMLINKS | FUSE_NO_OPENDIR_SUPPORT | FUSE_EXPLICIT_INVAL_DATA | FUSE_HANDLE_KILLPRIV_V2 | FUSE_SETXATTR_EXT | FUSE_INIT_EXT | - FUSE_SECURITY_CTX; + FUSE_SECURITY_CTX | FUSE_DO_ATOMIC_OPEN; #ifdef CONFIG_FUSE_DAX if (fm->fc->dax) flags |= FUSE_MAP_ALIGNMENT; diff --git a/include/uapi/linux/fuse.h b/include/uapi/linux/fuse.h index e4b56004b148..ab91e391241a 100644 --- a/include/uapi/linux/fuse.h +++ b/include/uapi/linux/fuse.h @@ -391,6 +391,7 @@ struct fuse_file_lock { /* bits 32..63 get shifted down 32 bits into the flags2 field */ #define FUSE_SECURITY_CTX (1ULL << 32) #define FUSE_HAS_INODE_DAX (1ULL << 33) +#define FUSE_DO_ATOMIC_OPEN (1ULL << 34) /** * CUSE INIT request/reply flags @@ -540,6 +541,7 @@ enum fuse_opcode { FUSE_REMOVEMAPPING = 49, FUSE_SYNCFS = 50, FUSE_ATOMIC_CREATE = 51, + FUSE_ATOMIC_OPEN = 52, /* CUSE specific operations */ CUSE_INIT = 4096, From patchwork Mon May 2 05:46:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dharmendra Singh X-Patchwork-Id: 12833842 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 113FCC433F5 for ; Mon, 2 May 2022 05:47:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1383378AbiEBFua (ORCPT ); Mon, 2 May 2022 01:50:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59162 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383373AbiEBFu2 (ORCPT ); Mon, 2 May 2022 01:50:28 -0400 Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 60A1F3EBA4; Sun, 1 May 2022 22:46:58 -0700 (PDT) Received: by mail-pj1-x1033.google.com with SMTP id cu23-20020a17090afa9700b001d98d8e53b7so11939029pjb.0; Sun, 01 May 2022 22:46:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :organization; bh=2fzpAqDYNWjV/li59hFbkiMS5pJrhKDBdp6dYsAI/ps=; b=oa864DM0+/NesLrdgaSf0f5WzMws+4IwLKuZecBnJGsEKtO/oirL3Pt6awR/h4SfEk tqSeIp0txtk4ZOUchxG1mDf4Fk+qchnrmnfvuZjLMkK8LdMj6Ucn0TdjAFudSdJmGZZd HOjGDCQaU54XKDVqOyyLSJwahNOcTqKhItabx/dsWkT/OmxtVvMV37IzrdL+5XdJbI/d OISS7pfqIq4s0Ln7jP0BmFAmXFLpI0z2yAlQEqD+d25Oe7drCyTODwQOqlPpytRnXpD7 4BQST7aSzAgiFzWTAaXbMr/OQ1xZnsAvcql6X1vR86q6AqG8YzUFdonW72mgsUF8Vrxr 2XWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:organization; bh=2fzpAqDYNWjV/li59hFbkiMS5pJrhKDBdp6dYsAI/ps=; b=FKvU1Q0fOpBkNntjbpugMGbPYK6jkGdS8t7v2xbCz3xbNIbDwduJuJC6NTbU8vq0fD 5Y7L7e+C1y+1YUE+OGWppjhA7hk+VbtenWASjLaJAhsggDnszFZAGj6DbwBwu/jMjShz S8NzAyjICeiUZa/uDJYDo5Z54xOHtZ9njNJVLqdv162YFHs/HAzCjtHhH0MUnppS1qSf YxHG77LDBFfTPrFDuwd4iom8UnFxWAhOj2hmKvL0VHPDb/1rwyQC1pPyu5MbQpZUIBhc r9Kv3rQIP1bM8IjpGGEDQndwF4HghR8gz2ABGqYkdivEesf+GNkyWWFAim+a2vQqCppS VFAQ== X-Gm-Message-State: AOAM532UlJ+JlXMfCwtr4h21zNz16Jhcw/u3PUtmyX3jvgfxiKbgkwCg MGO5Nb/s/kRp4UgUmvYFzk0= X-Google-Smtp-Source: ABdhPJwvGvwpqV2yqcpFUbyIvm/pieVYBrEZO3woLg1R0qSrWu5EBPlXGA2I6jMQi0j1i3ceVO2TaQ== X-Received: by 2002:a17:902:cf0b:b0:151:9d28:f46f with SMTP id i11-20020a170902cf0b00b001519d28f46fmr10131376plg.53.1651470417339; Sun, 01 May 2022 22:46:57 -0700 (PDT) Received: from localhost.localdomain ([123.201.245.164]) by smtp.googlemail.com with ESMTPSA id q20-20020a62e114000000b0050dc76281c8sm3787490pfh.162.2022.05.01.22.46.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 May 2022 22:46:57 -0700 (PDT) From: Dharmendra Singh To: miklos@szeredi.hu Cc: Dharmendra Singh , linux-fsdevel@vger.kernel.org, fuse-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org, bschubert@ddn.com, Dharmendra Singh Subject: [PATCH v3 3/3] Avoid lookup in d_revalidate() Date: Mon, 2 May 2022 11:16:28 +0530 Message-Id: <20220502054628.25826-4-dharamhans87@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220502054628.25826-1-dharamhans87@gmail.com> References: <20220502054628.25826-1-dharamhans87@gmail.com> Organization: DDN STORAGE Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org From: Dharmendra Singh With atomic open + lookup implemented, it is possible to avoid lookups in FUSE d_revalidate() for objects other than directories. If FUSE is mounted with default permissions then this optimization is not possible as we need to fetch fresh inode attributes for permission check. This lookup skipped in d_revalidate() can be performed as part of open call into libfuse which is made from fuse_file_open(). And when we return from USER SPACE with file opened and fresh attributes, we can revalidate the inode. Signed-off-by: Dharmendra Singh Reported-by: kernel test robot Reported-by: kernel test robot --- fs/fuse/dir.c | 90 +++++++++++++++++++++++++++++++++++++++++------- fs/fuse/file.c | 30 ++++++++++++++-- fs/fuse/fuse_i.h | 10 +++++- fs/fuse/ioctl.c | 2 +- 4 files changed, 115 insertions(+), 17 deletions(-) diff --git a/fs/fuse/dir.c b/fs/fuse/dir.c index 6879d3a86796..0b3e26dfdf29 100644 --- a/fs/fuse/dir.c +++ b/fs/fuse/dir.c @@ -196,6 +196,7 @@ static void fuse_lookup_init(struct fuse_conn *fc, struct fuse_args *args, * the lookup once more. If the lookup results in the same inode, * then refresh the attributes, timeouts and mark the dentry valid. */ + static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags) { struct inode *inode; @@ -224,6 +225,17 @@ static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags) fm = get_fuse_mount(inode); + /* If atomic open is supported by FUSE then use this opportunity + * (only for non-dir) to avoid this lookup and combine + * lookup + open into single call. + */ + if (!fm->fc->default_permissions && fm->fc->do_atomic_open && + !(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) && + (flags & LOOKUP_OPEN) && !S_ISDIR(inode->i_mode)) { + ret = 1; + goto out; + } + forget = fuse_alloc_forget(); ret = -ENOMEM; if (!forget) @@ -515,14 +527,51 @@ static int get_security_context(struct dentry *entry, umode_t mode, return err; } +/* + * Revalidate the inode after we got fresh attributes from user space. + */ +static int fuse_atomic_open_revalidate_inode(struct inode *reval_inode, + struct dentry *entry, + struct fuse_inode *fi, + struct fuse_forget_link *forget, + struct fuse_entry_out *outentry, + u64 attr_version) +{ + struct fuse_conn *fc = get_fuse_conn(reval_inode); + /* Mode should be other than directory */ + BUG_ON(S_ISDIR(reval_inode->i_mode)); + + if (outentry->nodeid != get_node_id(reval_inode)) { + fuse_queue_forget(fc, forget, outentry->nodeid, 1); + return -ESTALE; + } + if (fuse_stale_inode(reval_inode, outentry->generation, + &outentry->attr)) { + fuse_make_bad(reval_inode); + return -ESTALE; + } + fi = get_fuse_inode(reval_inode); + spin_lock(&fi->lock); + fi->nlookup++; + spin_unlock(&fi->lock); + + forget_all_cached_acls(reval_inode); + fuse_change_attributes(reval_inode, &outentry->attr, + entry_attr_timeout(outentry), attr_version); + fuse_change_entry_timeout(entry, outentry); + return 0; +} + + /* * This is common function for initiating atomic operations into libfuse. * Currently being used by Atomic create(atomic lookup + create) and * Atomic open(atomic lookup + open). */ -static int fuse_atomic_do_common(struct inode *dir, struct dentry *entry, - struct dentry **alias, struct file *file, - unsigned int flags, umode_t mode, uint32_t opcode) +int fuse_do_atomic_common(struct inode *dir, struct dentry *entry, + struct dentry **alias, struct file *file, + struct inode *reval_inode, unsigned int flags, + umode_t mode, uint32_t opcode) { int err; struct inode *inode; @@ -541,6 +590,8 @@ static int fuse_atomic_do_common(struct inode *dir, struct dentry *entry, bool atomic_create = (opcode == FUSE_ATOMIC_CREATE ? true : false); bool create_op = (opcode == FUSE_CREATE || opcode == FUSE_ATOMIC_CREATE) ? true : false; + u64 attr_version = fuse_get_attr_version(fm->fc); + if (alias) *alias = NULL; @@ -609,6 +660,20 @@ static int fuse_atomic_do_common(struct inode *dir, struct dentry *entry, ff->fh = outopen.fh; ff->nodeid = outentry.nodeid; ff->open_flags = outopen.open_flags; + + /* Inode revalidation was bypassed previously for type other than + * directories, revalidate now as we got fresh attributes. + */ + if (reval_inode) { + err = fuse_atomic_open_revalidate_inode(reval_inode, entry, fi, + forget, &outentry, + attr_version); + if (err) + goto out_free_ff; + inode = reval_inode; + kfree(forget); + goto out_finish_open; + } inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation, &outentry.attr, entry_attr_timeout(&outentry), 0); if (!inode) { @@ -659,6 +724,7 @@ static int fuse_atomic_do_common(struct inode *dir, struct dentry *entry, if (!atomic_create || (outopen.open_flags & FOPEN_FILE_CREATED)) fuse_dir_changed(dir); err = finish_open(file, entry, generic_file_open); +out_finish_open: if (err) { fi = get_fuse_inode(inode); fuse_sync_release(fi, ff, flags); @@ -686,7 +752,7 @@ static int fuse_atomic_create(struct inode *dir, struct dentry *entry, if (fc->no_atomic_create) return -ENOSYS; - err = fuse_atomic_do_common(dir, entry, NULL, file, flags, mode, + err = fuse_do_atomic_common(dir, entry, NULL, file, NULL, flags, mode, FUSE_ATOMIC_CREATE); /* If atomic create is not implemented then indicate in fc so that next * request falls back to normal create instead of going into libufse and @@ -699,9 +765,10 @@ static int fuse_atomic_create(struct inode *dir, struct dentry *entry, return err; } -static int fuse_do_atomic_open(struct inode *dir, struct dentry *entry, - struct dentry **alias, struct file *file, - unsigned int flags, umode_t mode) +int fuse_do_atomic_open(struct inode *dir, struct dentry *entry, + struct dentry **alias, struct file *file, + struct inode *reval_inode, unsigned int flags, + umode_t mode) { int err; struct fuse_conn *fc = get_fuse_conn(dir); @@ -709,8 +776,8 @@ static int fuse_do_atomic_open(struct inode *dir, struct dentry *entry, if (!fc->do_atomic_open) return -ENOSYS; - err = fuse_atomic_do_common(dir, entry, alias, file, flags, mode, - FUSE_ATOMIC_OPEN); + err = fuse_do_atomic_common(dir, entry, alias, file, reval_inode, flags, + mode, FUSE_ATOMIC_OPEN); /* Atomic open imply atomic trunc as well i.e truncate should be performed * as part of atomic open call itself. */ @@ -718,7 +785,6 @@ static int fuse_do_atomic_open(struct inode *dir, struct dentry *entry, if (fc->do_atomic_open) fc->atomic_o_trunc = 1; } - return err; } @@ -738,7 +804,7 @@ static int fuse_atomic_open(struct inode *dir, struct dentry *entry, if (!create) { err = fuse_do_atomic_open(dir, entry, &alias, - file, flags, mode); + file, NULL, flags, mode); res = alias; if (!err || err != -ENOSYS) goto out_dput; @@ -774,7 +840,7 @@ static int fuse_atomic_open(struct inode *dir, struct dentry *entry, * 'mknod' + 'open' requests. */ if (err == -ENOSYS) { - err = fuse_atomic_do_common(dir, entry, NULL, file, flags, + err = fuse_do_atomic_common(dir, entry, NULL, file, NULL, flags, mode, FUSE_CREATE); } if (err == -ENOSYS) { diff --git a/fs/fuse/file.c b/fs/fuse/file.c index 829094451774..2b0548163249 100644 --- a/fs/fuse/file.c +++ b/fs/fuse/file.c @@ -125,11 +125,15 @@ static void fuse_file_put(struct fuse_file *ff, bool sync, bool isdir) } struct fuse_file *fuse_file_open(struct fuse_mount *fm, u64 nodeid, - unsigned int open_flags, bool isdir) + struct file *file, unsigned int open_flags, + bool isdir) { struct fuse_conn *fc = fm->fc; struct fuse_file *ff; + struct dentry *dentry = NULL; + struct dentry *parent = NULL; int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN; + int ret; ff = fuse_file_alloc(fm); if (!ff) @@ -138,6 +142,11 @@ struct fuse_file *fuse_file_open(struct fuse_mount *fm, u64 nodeid, ff->fh = 0; /* Default for no-open */ ff->open_flags = FOPEN_KEEP_CACHE | (isdir ? FOPEN_CACHE_DIR : 0); + + /* For directories we already had lookup */ + if (!isdir && fc->do_atomic_open && file != NULL) + goto revalidate_atomic_open; + if (isdir ? !fc->no_opendir : !fc->no_open) { struct fuse_open_out outarg; int err; @@ -164,12 +173,27 @@ struct fuse_file *fuse_file_open(struct fuse_mount *fm, u64 nodeid, ff->nodeid = nodeid; return ff; + +revalidate_atomic_open: + dentry = file->f_path.dentry; + /* Get ref on parent */ + parent = dget_parent(dentry); + open_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY); + ret = fuse_do_atomic_open(d_inode_rcu(parent), dentry, NULL, file, + d_inode_rcu(dentry), open_flags, 0); + dput(parent); + if (ret) + goto err_out; + ff = file->private_data; + return ff; +err_out: + return ERR_PTR(ret); } int fuse_do_open(struct fuse_mount *fm, u64 nodeid, struct file *file, bool isdir) { - struct fuse_file *ff = fuse_file_open(fm, nodeid, file->f_flags, isdir); + struct fuse_file *ff = fuse_file_open(fm, nodeid, file, file->f_flags, isdir); if (!IS_ERR(ff)) file->private_data = ff; @@ -252,7 +276,7 @@ int fuse_open_common(struct inode *inode, struct file *file, bool isdir) } err = fuse_do_open(fm, get_node_id(inode), file, isdir); - if (!err) + if (!err && (!fc->do_atomic_open || isdir)) fuse_finish_open(inode, file); out: diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h index 24793b82303f..5c83e4249b7e 100644 --- a/fs/fuse/fuse_i.h +++ b/fs/fuse/fuse_i.h @@ -1014,6 +1014,13 @@ void fuse_finish_open(struct inode *inode, struct file *file); void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff, unsigned int flags); +/** + * Send atomic create + open or lookup + open + */ +int fuse_do_atomic_open(struct inode *dir, struct dentry *entry, + struct dentry **alias, struct file *file, + struct inode *reval_inode, unsigned int flags, + umode_t mode); /** * Send RELEASE or RELEASEDIR request */ @@ -1317,7 +1324,8 @@ int fuse_fileattr_set(struct user_namespace *mnt_userns, /* file.c */ struct fuse_file *fuse_file_open(struct fuse_mount *fm, u64 nodeid, - unsigned int open_flags, bool isdir); + struct file *file, unsigned int open_flags, + bool isdir); void fuse_file_release(struct inode *inode, struct fuse_file *ff, unsigned int open_flags, fl_owner_t id, bool isdir); diff --git a/fs/fuse/ioctl.c b/fs/fuse/ioctl.c index fbc09dab1f85..63106a54ba1a 100644 --- a/fs/fuse/ioctl.c +++ b/fs/fuse/ioctl.c @@ -408,7 +408,7 @@ static struct fuse_file *fuse_priv_ioctl_prepare(struct inode *inode) if (!S_ISREG(inode->i_mode) && !isdir) return ERR_PTR(-ENOTTY); - return fuse_file_open(fm, get_node_id(inode), O_RDONLY, isdir); + return fuse_file_open(fm, get_node_id(inode), NULL, O_RDONLY, isdir); } static void fuse_priv_ioctl_cleanup(struct inode *inode, struct fuse_file *ff)