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 60A51C77B75 for ; Tue, 18 Apr 2023 01:43:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231233AbjDRBnm (ORCPT ); Mon, 17 Apr 2023 21:43:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53720 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230232AbjDRBmm (ORCPT ); Mon, 17 Apr 2023 21:42:42 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A41295B97 for ; Mon, 17 Apr 2023 18:41:42 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 188-20020a250ac5000000b00b9265c9a5e9so2183530ybk.11 for ; Mon, 17 Apr 2023 18:41:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1681782092; x=1684374092; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=KLfNH8wOaoEhBmLooOhZO4y32d1iMom08eY0cFO8TR0=; b=WjKoP43zShmBluCRjRG7SpJRr6ZhcFjGdtCDccumEdmZPIzgFTJvIKad4FYUIjjQQy IplrWk3NdRdH2gDDLJQ9Cx/UvfS1qSwkngDqd21ylbmG4ukfCRinSmNNAs5uppGzVJLG lpzuh+saSdzIC/lLpNoMzuaoBLoXD19zIu7i6cCa28+pGHuAQbBcT10P4y35gFo66XQa Tnn9wbyumgtkRMoHf71AAl1+0l3ZdUh4BWT0RNrOjfa3KXsL2/sdfWUPFVrKfqF59nvb JuM8Zm1GDKLnP4FRMd0bLhe8vn0smcB/2wdsoJsfUhL4Z5/rqi8pn3H217Z4nqyfNjCk TSiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681782092; x=1684374092; 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=KLfNH8wOaoEhBmLooOhZO4y32d1iMom08eY0cFO8TR0=; b=RRTfa6Vcd3Y76yBLkVunQ58yjbMg6OcN2xkEgoqnvhQ2COGJUZH2I3HXDI4ymLn+4H LCWeIv8TC7OESbRcZDt8I60srhHgvk7qehAFynKrxGGoMOYtJ2tBPGeX1BGWjY0MM4G/ zcC+SAD8yAuSQy2EL7nkquXJucrCoAjeTbF+jPJarE9vk0FpL2AcQudzoUY40mlg7p2A 6TXyKcrBq2EcswZungqWpllJx61QGaVr+WwSd0/gjzyay7rIYfF07C4ZujQ1rf1lXEic nQUJDdo575x0mxd7pOzg1XLUUzwrf9qRxvmBU0Sw6XQqjtOkTTSMRWAzhtWcymfgaXNa LIgw== X-Gm-Message-State: AAQBX9fqdwbK723BqWBzdPNXe5ySo/zdas+PFpVZMrK8BjkXux0RiNpV aRzqjCzaYA9bIxLKs91fkT3vZ2ukWPk= X-Google-Smtp-Source: AKy350ZFWi1/rz2iUYQDDITIMq4pvfy+q1dsB6RG635Ep9uEFEuOKaLHWxdFW4DJWTE+80pH5vnhyBiQBOM= X-Received: from drosen.mtv.corp.google.com ([2620:15c:211:201:e67a:98b0:942d:86aa]) (user=drosen job=sendgmr) by 2002:a05:690c:d91:b0:54f:e88d:79ba with SMTP id da17-20020a05690c0d9100b0054fe88d79bamr9714213ywb.5.1681782091925; Mon, 17 Apr 2023 18:41:31 -0700 (PDT) Date: Mon, 17 Apr 2023 18:40:18 -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-19-drosen@google.com> Subject: [RFC PATCH v3 18/37] fuse-bpf: support readdir 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_READDIR Signed-off-by: Daniel Rosenberg Signed-off-by: Paul Lawrence --- fs/fuse/backing.c | 194 ++++++++++++++++++++++++++++++++++++++ fs/fuse/fuse_i.h | 6 ++ fs/fuse/readdir.c | 5 + include/uapi/linux/fuse.h | 6 ++ 4 files changed, 211 insertions(+) diff --git a/fs/fuse/backing.c b/fs/fuse/backing.c index c7709a880e9c..2908c231a695 100644 --- a/fs/fuse/backing.c +++ b/fs/fuse/backing.c @@ -1669,6 +1669,200 @@ int fuse_bpf_unlink(int *out, struct inode *dir, st= ruct dentry *entry) dir, entry); } =20 +struct fuse_read_args { + struct fuse_read_in in; + struct fuse_read_out out; + struct fuse_buffer buffer; +}; + +static int fuse_readdir_initialize_in(struct bpf_fuse_args *fa, struct fus= e_read_args *args, + struct file *file, struct dir_context *ctx, + bool *force_again, bool *allow_force, bool is_continued) +{ + struct fuse_file *ff =3D file->private_data; + + *fa =3D (struct bpf_fuse_args) { + .info =3D (struct bpf_fuse_meta_info) { + .nodeid =3D ff->nodeid, + .opcode =3D FUSE_READDIR, + }, + .in_numargs =3D 1, + .in_args[0] =3D (struct bpf_fuse_arg) { + .size =3D sizeof(args->in), + .value =3D &args->in, + }, + }; + + args->in =3D (struct fuse_read_in) { + .fh =3D ff->fh, + .offset =3D ctx->pos, + .size =3D PAGE_SIZE, + }; + + *force_again =3D false; + *allow_force =3D true; + return 0; +} + +static int fuse_readdir_initialize_out(struct bpf_fuse_args *fa, struct fu= se_read_args *args, + struct file *file, struct dir_context *ctx, + bool *force_again, bool *allow_force, bool is_continued) +{ + u8 *page =3D (u8 *)__get_free_page(GFP_KERNEL); + + if (!page) + return -ENOMEM; + + fa->flags =3D FUSE_BPF_OUT_ARGVAR; + fa->out_numargs =3D 2; + fa->out_args[0] =3D (struct bpf_fuse_arg) { + .size =3D sizeof(args->out), + .value =3D &args->out, + }; + fa->out_args[1] =3D (struct bpf_fuse_arg) { + .is_buffer =3D true, + .buffer =3D &args->buffer, + }; + args->out =3D (struct fuse_read_out) { + .again =3D 0, + .offset =3D 0, + }; + args->buffer =3D (struct fuse_buffer) { + .data =3D page, + .size =3D PAGE_SIZE, + .alloc_size =3D PAGE_SIZE, + .max_size =3D PAGE_SIZE, + .flags =3D BPF_FUSE_VARIABLE_SIZE, + }; + + return 0; +} + +struct fusebpf_ctx { + struct dir_context ctx; + u8 *addr; + size_t offset; +}; + +static bool filldir(struct dir_context *ctx, const char *name, int namelen, + loff_t offset, u64 ino, unsigned int d_type) +{ + struct fusebpf_ctx *ec =3D container_of(ctx, struct fusebpf_ctx, ctx); + struct fuse_dirent *fd =3D (struct fuse_dirent *)(ec->addr + ec->offset); + + if (ec->offset + sizeof(struct fuse_dirent) + namelen > PAGE_SIZE) + return false; + + *fd =3D (struct fuse_dirent) { + .ino =3D ino, + .off =3D offset, + .namelen =3D namelen, + .type =3D d_type, + }; + + memcpy(fd->name, name, namelen); + ec->offset +=3D FUSE_DIRENT_SIZE(fd); + + return true; +} + +static int parse_dirfile(char *buf, size_t nbytes, struct dir_context *ctx) +{ + while (nbytes >=3D FUSE_NAME_OFFSET) { + struct fuse_dirent *dirent =3D (struct fuse_dirent *) buf; + size_t reclen =3D FUSE_DIRENT_SIZE(dirent); + + if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX) + return -EIO; + if (reclen > nbytes) + break; + if (memchr(dirent->name, '/', dirent->namelen) !=3D NULL) + return -EIO; + + ctx->pos =3D dirent->off; + if (!dir_emit(ctx, dirent->name, dirent->namelen, dirent->ino, + dirent->type)) + break; + + buf +=3D reclen; + nbytes -=3D reclen; + } + + return 0; +} + +static int fuse_readdir_backing(struct bpf_fuse_args *fa, int *out, + struct file *file, struct dir_context *ctx, + bool *force_again, bool *allow_force, bool is_continued) +{ + struct fuse_file *ff =3D file->private_data; + struct file *backing_dir =3D ff->backing_file; + struct fuse_read_out *fro =3D fa->out_args[0].value; + struct fusebpf_ctx ec; + + ec =3D (struct fusebpf_ctx) { + .ctx.actor =3D filldir, + .ctx.pos =3D ctx->pos, + .addr =3D fa->out_args[1].buffer->data, + }; + + if (!ec.addr) + return -ENOMEM; + + if (!is_continued) + backing_dir->f_pos =3D file->f_pos; + + *out =3D iterate_dir(backing_dir, &ec.ctx); + if (ec.offset =3D=3D 0) + *allow_force =3D false; + fa->out_args[1].buffer->size =3D ec.offset; + + fro->offset =3D ec.ctx.pos; + fro->again =3D false; + + return *out; +} + +static int fuse_readdir_finalize(struct bpf_fuse_args *fa, int *out, + struct file *file, struct dir_context *ctx, + bool *force_again, bool *allow_force, bool is_continued) +{ + struct fuse_read_out *fro =3D fa->out_args[0].value; + struct fuse_file *ff =3D file->private_data; + struct file *backing_dir =3D ff->backing_file; + + *out =3D parse_dirfile(fa->out_args[1].buffer->data, fa->out_args[1].buff= er->size, ctx); + *force_again =3D !!fro->again; + if (*force_again && !*allow_force) + *out =3D -EINVAL; + + ctx->pos =3D fro->offset; + backing_dir->f_pos =3D fro->offset; + + free_page((unsigned long)fa->out_args[1].buffer->data); + return *out; +} + +int fuse_bpf_readdir(int *out, struct inode *inode, struct file *file, str= uct dir_context *ctx) +{ + int ret; + bool allow_force; + bool force_again =3D false; + bool is_continued =3D false; + +again: + ret =3D bpf_fuse_backing(inode, struct fuse_read_args, out, + fuse_readdir_initialize_in, fuse_readdir_initialize_out, + fuse_readdir_backing, fuse_readdir_finalize, + file, ctx, &force_again, &allow_force, is_continued); + if (force_again && *out >=3D 0) { + is_continued =3D true; + goto again; + } + + return ret; +} + static int fuse_access_initialize_in(struct bpf_fuse_args *fa, struct fuse= _access_in *in, struct inode *inode, int mask) { diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h index 4bc070b81ac2..fb3a77b79b0f 100644 --- a/fs/fuse/fuse_i.h +++ b/fs/fuse/fuse_i.h @@ -1419,6 +1419,7 @@ int fuse_bpf_file_read_iter(ssize_t *out, struct inod= e *inode, struct kiocb *ioc int fuse_bpf_file_write_iter(ssize_t *out, struct inode *inode, struct kio= cb *iocb, struct iov_iter *from); int fuse_bpf_file_fallocate(int *out, struct inode *inode, struct file *fi= le, int mode, loff_t offset, loff_t length); int fuse_bpf_lookup(struct dentry **out, struct inode *dir, struct dentry = *entry, unsigned int flags); +int fuse_bpf_readdir(int *out, struct inode *inode, struct file *file, str= uct dir_context *ctx); int fuse_bpf_access(int *out, struct inode *inode, int mask); =20 #else @@ -1489,6 +1490,11 @@ static inline int fuse_bpf_lookup(struct dentry **ou= t, struct inode *dir, struct return 0; } =20 +static inline int fuse_bpf_readdir(int *out, struct inode *inode, struct f= ile *file, struct dir_context *ctx) +{ + return 0; +} + static inline int fuse_bpf_access(int *out, struct inode *inode, int mask) { return 0; diff --git a/fs/fuse/readdir.c b/fs/fuse/readdir.c index dc603479b30e..cc6548f314f2 100644 --- a/fs/fuse/readdir.c +++ b/fs/fuse/readdir.c @@ -20,6 +20,8 @@ static bool fuse_use_readdirplus(struct inode *dir, struc= t dir_context *ctx) =20 if (!fc->do_readdirplus) return false; + if (fi->nodeid =3D=3D 0) + return false; if (!fc->readdirplus_auto) return true; if (test_and_clear_bit(FUSE_I_ADVISE_RDPLUS, &fi->state)) @@ -582,6 +584,9 @@ int fuse_readdir(struct file *file, struct dir_context = *ctx) if (fuse_is_bad(inode)) return -EIO; =20 + if (fuse_bpf_readdir(&err, inode, file, ctx)) + return err; + mutex_lock(&ff->readdir.lock); =20 err =3D UNCACHED; diff --git a/include/uapi/linux/fuse.h b/include/uapi/linux/fuse.h index dbfc8d501bcb..e779064f5fad 100644 --- a/include/uapi/linux/fuse.h +++ b/include/uapi/linux/fuse.h @@ -748,6 +748,12 @@ struct fuse_read_in { uint32_t padding; }; =20 +struct fuse_read_out { + uint64_t offset; + uint32_t again; + uint32_t padding; +}; + // This is likely not what we want struct fuse_read_iter_out { uint64_t ret; --=20 2.40.0.634.g4ca3ef3211-goog