Received: by 2002:a05:6358:53a8:b0:117:f937:c515 with SMTP id z40csp4756256rwe; Mon, 17 Apr 2023 18:43:56 -0700 (PDT) X-Google-Smtp-Source: AKy350YT5hjSCiquR5stDjqh4SwYOqQNmbWmm2DC3wVUuy0W8uKunW3Thp9nDXcjSROUSXldsLPt X-Received: by 2002:a17:902:c94f:b0:1a6:dc4a:b8fa with SMTP id i15-20020a170902c94f00b001a6dc4ab8famr514580pla.54.1681782236039; Mon, 17 Apr 2023 18:43:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681782236; cv=none; d=google.com; s=arc-20160816; b=Db7QGfJDPdJpfNwoFv2mBDy6Ea6kOdTBK0DglCwvKBLydm9jCQ6rVok5FnVm2lzher z4EW7Cv8DWfqRagKalMVknrPmP0BQSapSuiXs83cukyt+cLYkf3hIIPBSFpIT7cEuXWj gJytn0P+Bi3IexWQ7N3aoK998Qyr36/jBgmkaiosUqkREfVSdNTYC20ecKBGnafiDdr5 IN7qVXWgweZ1eWa3/mzDZzssOF+57qO1/tcYHocqjZ7lHDzYUWGDXklcoK7yeydh6HiE OIq66xutDlRfTOg3CLntiTVkvM++CvoPCB96u/pPALsk7zuc+RARJZNs4gi0hDxVkzai BUYQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=mM3VEDiu9mRhnxPoyHTlcLFFgOhw6pqe5rwY+welbQE=; b=eB/j2cDw09Ie2zLUFJbnDXSuejiZBvO0WlvpHvsByZZjB5ZHkvjKvlj7MuuyvVeYEf Na/ywmYpTiYibwp9ItzzzR4dp/U10JK0V5uarG8nIRbij3GcqJWykbkDtXsqbxBC2f+T JK2FfdELz1wHDr9EKQsu9Jveiw65dS4DM4saSM6Kod3Wp+LQ1BdDq2qijiM9jiO5B1fL I149QMIS3Nnb7FYG89a0c7W3ja8vhDLgEl/hR9CJ68BXSwokccOkTpQeyreHMdrbz09a aqs8k+NDiwKIMzS6nZw+i7JbnLSGeqhj3v89f9W4hBuURjx1Mc79srvfJsG4ombAnxM8 zf2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=73fduone; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bv2-20020a632e02000000b004f2022f3cc2si13713384pgb.813.2023.04.17.18.43.42; Mon, 17 Apr 2023 18:43:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=73fduone; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231202AbjDRBnb (ORCPT + 99 others); Mon, 17 Apr 2023 21:43:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53888 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230448AbjDRBmb (ORCPT ); Mon, 17 Apr 2023 21:42:31 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 269427DBB for ; Mon, 17 Apr 2023 18:41:37 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-54faf2e22afso139560967b3.7 for ; Mon, 17 Apr 2023 18:41:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1681782097; x=1684374097; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=mM3VEDiu9mRhnxPoyHTlcLFFgOhw6pqe5rwY+welbQE=; b=73fduone9MSuyhI/Vj/EBtrUNK1HlKqIXi2R/1WFSMzTZeGfp5VAaZvM2T8Z1A2hT+ TW4GWEQGhkVXdJu5a2/KltZOzXD1AQjbMH7CIR0D5QETP4evRehG6EDqGgw/XSPXtGTD vVM4cpWKlgSrNskNHkKA3nkSoYyOvtjcA/osyf9wix2pqxY+xmbkY9RApyC6c7CrreuR ziFJAw8vrjwNraBMSSyT/8rY6SHmX6pnezYrpgPEU6LWtlkL/qSjF7IjcX7GLUKf/v2+ cH0jszXzfhYMnGCvrW87cOI//DWQdwJdFT43fsUzo5cCWupIGklT1pKXaLA4TRnEWYuZ o5oQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681782097; x=1684374097; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=mM3VEDiu9mRhnxPoyHTlcLFFgOhw6pqe5rwY+welbQE=; b=D7yHPqLz2oKwxd8HS+RO7lwUSo0bJCe+9XTEHx9nR0rqUSx6kbqzGek5psI04B1Wb1 mdhdMwViGOlKIPigaDg3H6n2WB1L58QhILknUttpJviVVtkSegLqkqt3CTjbkf7/L412 mbw6GOMWZnJcJ2zugt4thdohFcD+dDvfqedIzu6ivgr5gtaicNYrS+K7MJDL3isxpPw6 NHOyor0bS4ytSXo0fPemuNq1lD+DSAYVCb0vpZX/jNY0y3WnxM288+IXN/0kET2oI38M 616Ht00KLO5COcrN37SIglrAB0DggUOD2JivxTzEZS0mCOHGEXsBBBt1uOCGLuCmnoMI KhWA== X-Gm-Message-State: AAQBX9dnYw7R8wes6qou95yzA8rQzqn7lyU6MH63EPpwUw6tGF9lF42B uR0jq3lR/2YWCwP8yc91+djtQXkCiPs= X-Received: from drosen.mtv.corp.google.com ([2620:15c:211:201:e67a:98b0:942d:86aa]) (user=drosen job=sendgmr) by 2002:a25:d24d:0:b0:b90:1777:7194 with SMTP id j74-20020a25d24d000000b00b9017777194mr8137204ybg.6.1681782096895; Mon, 17 Apr 2023 18:41:36 -0700 (PDT) Date: Mon, 17 Apr 2023 18:40:20 -0700 In-Reply-To: <20230418014037.2412394-1-drosen@google.com> Mime-Version: 1.0 References: <20230418014037.2412394-1-drosen@google.com> X-Mailer: git-send-email 2.40.0.634.g4ca3ef3211-goog Message-ID: <20230418014037.2412394-21-drosen@google.com> Subject: [RFC PATCH v3 20/37] fuse-bpf: Add Rename support From: Daniel Rosenberg To: Miklos Szeredi , bpf@vger.kernel.org, Alexei Starovoitov Cc: Amir Goldstein , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-unionfs@vger.kernel.org, Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , Jonathan Corbet , Joanne Koong , Mykola Lysenko , kernel-team@android.com, Daniel Rosenberg , Paul Lawrence Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This adds backing support for FUSE_RENAME and FUSE_RENAME2 Signed-off-by: Daniel Rosenberg Signed-off-by: Paul Lawrence --- fs/fuse/backing.c | 250 ++++++++++++++++++++++++++++++++++++++++++++++ fs/fuse/dir.c | 7 ++ fs/fuse/fuse_i.h | 18 ++++ 3 files changed, 275 insertions(+) diff --git a/fs/fuse/backing.c b/fs/fuse/backing.c index 30492f7b2a05..d3a706b55905 100644 --- a/fs/fuse/backing.c +++ b/fs/fuse/backing.c @@ -1747,6 +1747,256 @@ int fuse_bpf_rmdir(int *out, struct inode *dir, struct dentry *entry) dir, entry); } +static int fuse_rename_backing_common(struct inode *olddir, + struct dentry *oldent, + struct inode *newdir, + struct dentry *newent, unsigned int flags) +{ + int err = 0; + struct path old_backing_path; + struct path new_backing_path; + struct dentry *old_backing_dir_dentry; + struct dentry *old_backing_dentry; + struct dentry *new_backing_dir_dentry; + struct dentry *new_backing_dentry; + struct dentry *trap = NULL; + struct inode *target_inode; + struct renamedata rd; + + //TODO Actually deal with changing anything that isn't a flag + get_fuse_backing_path(oldent, &old_backing_path); + if (!old_backing_path.dentry) + return -EBADF; + get_fuse_backing_path(newent, &new_backing_path); + if (!new_backing_path.dentry) { + /* + * TODO A file being moved from a backing path to another + * backing path which is not yet instrumented with FUSE-BPF. + * This may be slow and should be substituted with something + * more clever. + */ + err = -EXDEV; + goto put_old_path; + } + if (new_backing_path.mnt != old_backing_path.mnt) { + err = -EXDEV; + goto put_new_path; + } + old_backing_dentry = old_backing_path.dentry; + new_backing_dentry = new_backing_path.dentry; + old_backing_dir_dentry = dget_parent(old_backing_dentry); + new_backing_dir_dentry = dget_parent(new_backing_dentry); + target_inode = d_inode(newent); + + trap = lock_rename(old_backing_dir_dentry, new_backing_dir_dentry); + if (trap == old_backing_dentry) { + err = -EINVAL; + goto put_parents; + } + if (trap == new_backing_dentry) { + err = -ENOTEMPTY; + goto put_parents; + } + + rd = (struct renamedata) { + .old_mnt_idmap = &nop_mnt_idmap, + .old_dir = d_inode(old_backing_dir_dentry), + .old_dentry = old_backing_dentry, + .new_mnt_idmap = &nop_mnt_idmap, + .new_dir = d_inode(new_backing_dir_dentry), + .new_dentry = new_backing_dentry, + .flags = flags, + }; + err = vfs_rename(&rd); + if (err) + goto unlock; + if (target_inode) + fsstack_copy_attr_all(target_inode, + get_fuse_inode(target_inode)->backing_inode); + fsstack_copy_attr_all(d_inode(oldent), d_inode(old_backing_dentry)); +unlock: + unlock_rename(old_backing_dir_dentry, new_backing_dir_dentry); +put_parents: + dput(new_backing_dir_dentry); + dput(old_backing_dir_dentry); +put_new_path: + path_put(&new_backing_path); +put_old_path: + path_put(&old_backing_path); + return err; +} + +struct fuse_rename2_args { + struct fuse_rename2_in in; + struct fuse_buffer old_name; + struct fuse_buffer new_name; +}; + +static int fuse_rename2_initialize_in(struct bpf_fuse_args *fa, struct fuse_rename2_args *args, + struct inode *olddir, struct dentry *oldent, + struct inode *newdir, struct dentry *newent, + unsigned int flags) +{ + *args = (struct fuse_rename2_args) { + .in = (struct fuse_rename2_in) { + .newdir = get_node_id(newdir), + .flags = flags, + }, + .old_name = (struct fuse_buffer) { + .data = (void *) oldent->d_name.name, + .size = oldent->d_name.len + 1, + .flags = BPF_FUSE_IMMUTABLE, + }, + .new_name = (struct fuse_buffer) { + .data = (void *) newent->d_name.name, + .size = newent->d_name.len + 1, + .flags = BPF_FUSE_IMMUTABLE, + }, + + }; + *fa = (struct bpf_fuse_args) { + .info = (struct bpf_fuse_meta_info) { + .nodeid = get_node_id(olddir), + .opcode = FUSE_RENAME2, + }, + .in_numargs = 3, + .in_args[0] = (struct bpf_fuse_arg) { + .size = sizeof(args->in), + .value = &args->in, + }, + .in_args[1] = (struct bpf_fuse_arg) { + .is_buffer = true, + .buffer = &args->old_name, + }, + .in_args[2] = (struct bpf_fuse_arg) { + .is_buffer = true, + .buffer = &args->new_name, + }, + }; + + return 0; +} + +static int fuse_rename2_initialize_out(struct bpf_fuse_args *fa, struct fuse_rename2_args *args, + struct inode *olddir, struct dentry *oldent, + struct inode *newdir, struct dentry *newent, + unsigned int flags) +{ + return 0; +} + +static int fuse_rename2_backing(struct bpf_fuse_args *fa, int *out, + struct inode *olddir, struct dentry *oldent, + struct inode *newdir, struct dentry *newent, + unsigned int flags) +{ + const struct fuse_rename2_args *fri = fa->in_args[0].value; + + /* TODO: deal with changing dirs/ents */ + *out = fuse_rename_backing_common(olddir, oldent, newdir, newent, + fri->in.flags); + return *out; +} + +static int fuse_rename2_finalize(struct bpf_fuse_args *fa, int *out, + struct inode *olddir, struct dentry *oldent, + struct inode *newdir, struct dentry *newent, + unsigned int flags) +{ + return 0; +} + +int fuse_bpf_rename2(int *out, struct inode *olddir, struct dentry *oldent, + struct inode *newdir, struct dentry *newent, + unsigned int flags) +{ + return bpf_fuse_backing(olddir, struct fuse_rename2_args, out, + fuse_rename2_initialize_in, fuse_rename2_initialize_out, + fuse_rename2_backing, fuse_rename2_finalize, + olddir, oldent, newdir, newent, flags); +} + +struct fuse_rename_args { + struct fuse_rename_in in; + struct fuse_buffer old_name; + struct fuse_buffer new_name; +}; + +static int fuse_rename_initialize_in(struct bpf_fuse_args *fa, struct fuse_rename_args *args, + struct inode *olddir, struct dentry *oldent, + struct inode *newdir, struct dentry *newent) +{ + *args = (struct fuse_rename_args) { + .in = (struct fuse_rename_in) { + .newdir = get_node_id(newdir), + }, + .old_name = (struct fuse_buffer) { + .data = (void *) oldent->d_name.name, + .size = oldent->d_name.len + 1, + .flags = BPF_FUSE_IMMUTABLE, + }, + .new_name = (struct fuse_buffer) { + .data = (void *) newent->d_name.name, + .size = newent->d_name.len + 1, + .flags = BPF_FUSE_IMMUTABLE, + }, + + }; + *fa = (struct bpf_fuse_args) { + .info = (struct bpf_fuse_meta_info) { + .nodeid = get_node_id(olddir), + .opcode = FUSE_RENAME, + }, + .in_numargs = 3, + .in_args[0] = (struct bpf_fuse_arg) { + .size = sizeof(args->in), + .value = &args->in, + }, + .in_args[1] = (struct bpf_fuse_arg) { + .is_buffer = true, + .buffer = &args->old_name, + }, + .in_args[2] = (struct bpf_fuse_arg) { + .is_buffer = true, + .buffer = &args->new_name, + }, + }; + + return 0; +} + +static int fuse_rename_initialize_out(struct bpf_fuse_args *fa, struct fuse_rename_args *args, + struct inode *olddir, struct dentry *oldent, + struct inode *newdir, struct dentry *newent) +{ + return 0; +} + +static int fuse_rename_backing(struct bpf_fuse_args *fa, int *out, + struct inode *olddir, struct dentry *oldent, + struct inode *newdir, struct dentry *newent) +{ + /* TODO: deal with changing dirs/ents */ + *out = fuse_rename_backing_common(olddir, oldent, newdir, newent, 0); + return *out; +} + +static int fuse_rename_finalize(struct bpf_fuse_args *fa, int *out, + struct inode *olddir, struct dentry *oldent, + struct inode *newdir, struct dentry *newent) +{ + return 0; +} + +int fuse_bpf_rename(int *out, struct inode *olddir, struct dentry *oldent, + struct inode *newdir, struct dentry *newent) +{ + return bpf_fuse_backing(olddir, struct fuse_rename_args, out, + fuse_rename_initialize_in, fuse_rename_initialize_out, + fuse_rename_backing, fuse_rename_finalize, + olddir, oldent, newdir, newent); +} + static int fuse_unlink_initialize_in(struct bpf_fuse_args *fa, struct fuse_buffer *name, struct inode *dir, struct dentry *entry) { diff --git a/fs/fuse/dir.c b/fs/fuse/dir.c index 5ce65f696980..086e3ecada19 100644 --- a/fs/fuse/dir.c +++ b/fs/fuse/dir.c @@ -1184,6 +1184,10 @@ static int fuse_rename2(struct mnt_idmap *idmap, struct inode *olddir, return -EINVAL; if (flags) { + if (fuse_bpf_rename2(&err, olddir, oldent, newdir, newent, flags)) + return err; + + /* TODO: how should this go with bpfs involved? */ if (fc->no_rename2 || fc->minor < 23) return -EINVAL; @@ -1195,6 +1199,9 @@ static int fuse_rename2(struct mnt_idmap *idmap, struct inode *olddir, err = -EINVAL; } } else { + if (fuse_bpf_rename(&err, olddir, oldent, newdir, newent)) + return err; + err = fuse_rename_common(olddir, oldent, newdir, newent, 0, FUSE_RENAME, sizeof(struct fuse_rename_in)); diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h index e60207bf66de..5c8bd2f76fb9 100644 --- a/fs/fuse/fuse_i.h +++ b/fs/fuse/fuse_i.h @@ -1411,6 +1411,11 @@ int fuse_bpf_create_open(int *out, struct inode *dir, struct dentry *entry, int fuse_bpf_mknod(int *out, struct inode *dir, struct dentry *entry, umode_t mode, dev_t rdev); int fuse_bpf_mkdir(int *out, struct inode *dir, struct dentry *entry, umode_t mode); int fuse_bpf_rmdir(int *out, struct inode *dir, struct dentry *entry); +int fuse_bpf_rename2(int *out, struct inode *olddir, struct dentry *oldent, + struct inode *newdir, struct dentry *newent, + unsigned int flags); +int fuse_bpf_rename(int *out, struct inode *olddir, struct dentry *oldent, + struct inode *newdir, struct dentry *newent); int fuse_bpf_unlink(int *out, struct inode *dir, struct dentry *entry); int fuse_bpf_release(int *out, struct inode *inode, struct file *file); int fuse_bpf_releasedir(int *out, struct inode *inode, struct file *file); @@ -1453,6 +1458,19 @@ static inline int fuse_bpf_rmdir(int *out, struct inode *dir, struct dentry *ent return 0; } +static inline int fuse_bpf_rename2(int *out, struct inode *olddir, struct dentry *oldent, + struct inode *newdir, struct dentry *newent, + unsigned int flags) +{ + return 0; +} + +static inline int fuse_bpf_rename(int *out, struct inode *olddir, struct dentry *oldent, + struct inode *newdir, struct dentry *newent) +{ + return 0; +} + static inline int fuse_bpf_unlink(int *out, struct inode *dir, struct dentry *entry) { return 0; -- 2.40.0.634.g4ca3ef3211-goog