From nobody Wed Dec 17 21:15:12 2025 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 0DB54C77B76 for ; Tue, 18 Apr 2023 01:43:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231217AbjDRBnd (ORCPT ); Mon, 17 Apr 2023 21:43:33 -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-Google-Smtp-Source: AKy350Zd0zSOj3A4RVvFzehl/6ZADh+4u0SSYtyXG0ZS+awJjZupIGXSlNBiv6NTnINRlJ+t8jkY64w9PeM= 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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, str= uct dentry *entry) dir, entry); } =20 +static int fuse_rename_backing_common(struct inode *olddir, + struct dentry *oldent, + struct inode *newdir, + struct dentry *newent, unsigned int flags) +{ + int err =3D 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 =3D 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 =3D -EXDEV; + goto put_old_path; + } + if (new_backing_path.mnt !=3D old_backing_path.mnt) { + err =3D -EXDEV; + goto put_new_path; + } + old_backing_dentry =3D old_backing_path.dentry; + new_backing_dentry =3D new_backing_path.dentry; + old_backing_dir_dentry =3D dget_parent(old_backing_dentry); + new_backing_dir_dentry =3D dget_parent(new_backing_dentry); + target_inode =3D d_inode(newent); + + trap =3D lock_rename(old_backing_dir_dentry, new_backing_dir_dentry); + if (trap =3D=3D old_backing_dentry) { + err =3D -EINVAL; + goto put_parents; + } + if (trap =3D=3D new_backing_dentry) { + err =3D -ENOTEMPTY; + goto put_parents; + } + + rd =3D (struct renamedata) { + .old_mnt_idmap =3D &nop_mnt_idmap, + .old_dir =3D d_inode(old_backing_dir_dentry), + .old_dentry =3D old_backing_dentry, + .new_mnt_idmap =3D &nop_mnt_idmap, + .new_dir =3D d_inode(new_backing_dir_dentry), + .new_dentry =3D new_backing_dentry, + .flags =3D flags, + }; + err =3D 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 fus= e_rename2_args *args, + struct inode *olddir, struct dentry *oldent, + struct inode *newdir, struct dentry *newent, + unsigned int flags) +{ + *args =3D (struct fuse_rename2_args) { + .in =3D (struct fuse_rename2_in) { + .newdir =3D get_node_id(newdir), + .flags =3D flags, + }, + .old_name =3D (struct fuse_buffer) { + .data =3D (void *) oldent->d_name.name, + .size =3D oldent->d_name.len + 1, + .flags =3D BPF_FUSE_IMMUTABLE, + }, + .new_name =3D (struct fuse_buffer) { + .data =3D (void *) newent->d_name.name, + .size =3D newent->d_name.len + 1, + .flags =3D BPF_FUSE_IMMUTABLE, + }, + + }; + *fa =3D (struct bpf_fuse_args) { + .info =3D (struct bpf_fuse_meta_info) { + .nodeid =3D get_node_id(olddir), + .opcode =3D FUSE_RENAME2, + }, + .in_numargs =3D 3, + .in_args[0] =3D (struct bpf_fuse_arg) { + .size =3D sizeof(args->in), + .value =3D &args->in, + }, + .in_args[1] =3D (struct bpf_fuse_arg) { + .is_buffer =3D true, + .buffer =3D &args->old_name, + }, + .in_args[2] =3D (struct bpf_fuse_arg) { + .is_buffer =3D true, + .buffer =3D &args->new_name, + }, + }; + + return 0; +} + +static int fuse_rename2_initialize_out(struct bpf_fuse_args *fa, struct fu= se_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 =3D fa->in_args[0].value; + + /* TODO: deal with changing dirs/ents */ + *out =3D 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 =3D (struct fuse_rename_args) { + .in =3D (struct fuse_rename_in) { + .newdir =3D get_node_id(newdir), + }, + .old_name =3D (struct fuse_buffer) { + .data =3D (void *) oldent->d_name.name, + .size =3D oldent->d_name.len + 1, + .flags =3D BPF_FUSE_IMMUTABLE, + }, + .new_name =3D (struct fuse_buffer) { + .data =3D (void *) newent->d_name.name, + .size =3D newent->d_name.len + 1, + .flags =3D BPF_FUSE_IMMUTABLE, + }, + + }; + *fa =3D (struct bpf_fuse_args) { + .info =3D (struct bpf_fuse_meta_info) { + .nodeid =3D get_node_id(olddir), + .opcode =3D FUSE_RENAME, + }, + .in_numargs =3D 3, + .in_args[0] =3D (struct bpf_fuse_arg) { + .size =3D sizeof(args->in), + .value =3D &args->in, + }, + .in_args[1] =3D (struct bpf_fuse_arg) { + .is_buffer =3D true, + .buffer =3D &args->old_name, + }, + .in_args[2] =3D (struct bpf_fuse_arg) { + .is_buffer =3D true, + .buffer =3D &args->new_name, + }, + }; + + return 0; +} + +static int fuse_rename_initialize_out(struct bpf_fuse_args *fa, struct fus= e_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 =3D 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, str= uct inode *olddir, return -EINVAL; =20 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; =20 @@ -1195,6 +1199,9 @@ static int fuse_rename2(struct mnt_idmap *idmap, stru= ct inode *olddir, err =3D -EINVAL; } } else { + if (fuse_bpf_rename(&err, olddir, oldent, newdir, newent)) + return err; + err =3D 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, umod= e_t mode, dev_t rdev); int fuse_bpf_mkdir(int *out, struct inode *dir, struct dentry *entry, umod= e_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 in= ode *dir, struct dentry *ent return 0; } =20 +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 d= entry *oldent, + struct inode *newdir, struct dentry *newent) +{ + return 0; +} + static inline int fuse_bpf_unlink(int *out, struct inode *dir, struct dent= ry *entry) { return 0; --=20 2.40.0.634.g4ca3ef3211-goog