Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp821468lqh; Thu, 28 Mar 2024 19:00:08 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWfieD/xy7VLXFDnfiCKRC/+wKI+sCHVczzlG6eMkgAknlBf1RKwu5Ip0YomH+VewoWHPlomYLDxRGZ3wqDW4o2x2ITJMXvcdZVKAZ2Zg== X-Google-Smtp-Source: AGHT+IEUAuI5xjh/wqEEee9gITV6fzGZudRHg17BhzBH9iRvUw1/I5PmWwAL5zRu/P7ZaLvM0zPE X-Received: by 2002:a17:906:1387:b0:a4e:221f:2ddf with SMTP id f7-20020a170906138700b00a4e221f2ddfmr517380ejc.49.1711677608078; Thu, 28 Mar 2024 19:00:08 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711677608; cv=pass; d=google.com; s=arc-20160816; b=PMLOjZ4ElO7gdUmN/lN+r3XXO++3rkw/TYUxQkM5ZlpOA3lA/ku/R5Zg4ZZKFm8f09 vaUjsh/lYgWc8Hz1QQ6jFtDYu4HJWdKiXOxKn17UZ8vH6T/V0ipPGL5dZEQmd4goZjTp TLToK5jS5e12HblAII5vCUwXySOmvpwJPPk5358fYmFu/O0MDT0dzzk/+XWoBUvE2B7H yWuzYJI1Xrf+knFysRABC2cVzxOcTt+T8QZnHKurJm6b5nvP9pW5wIwa1mDe1n5bD5xM S0viTDou5zuTRLzMYp7s6HJAj+cSTSERkrJxHtSDPchkUDiUCfzGrjDdAqvv888XfTYZ JFCw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=jC8NburpJ0gtA38yoeQoqc6ho+qO84rwoWBCmC/NNxg=; fh=IYDWY8VORpg6tikGZo/J0nhQqYPMHqvTBHdWVEe2TRI=; b=Us5NSw1GlC1H/ks8ZdggEKmGgMyzud2KqmpReEY0Tfi2Z5YmR4Co4gG+NFU8/B+wnN DHGLJM8A5xo1vo6XhIRA05YzCEP7ZpiywtROmRlQdXENaxXdndiVMQpUiZmptUBOqWyO tg7Hc0RqLPQb0bZlVpYi/LhfD10duu6x9ZBnB2RdKYm99l6v9Hvv5Jx0vBn7kDgDnnH1 znN7ZppRVZ3Rem3ZKpsQo81KLRimfNpXlUSwuOR1lgoBa1UNJkISxBltaZA4nRmUeTGV ZQ9AlB+r+0RnzeirX7AfVsotsZTj66b6oqT8jY17PA9saTPjXKKODCFvLHi1gG8ftQli C1Ew==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=umdU8kKN; arc=pass (i=1 spf=pass spfdomain=flex--drosen.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-123994-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-123994-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id mp29-20020a1709071b1d00b00a46daccf6fesi1297654ejc.849.2024.03.28.19.00.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 19:00:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-123994-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=umdU8kKN; arc=pass (i=1 spf=pass spfdomain=flex--drosen.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-123994-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-123994-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 99C801F23A81 for ; Fri, 29 Mar 2024 02:00:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0D62447F72; Fri, 29 Mar 2024 01:54:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="umdU8kKN" Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E1D0244370 for ; Fri, 29 Mar 2024 01:54:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711677272; cv=none; b=psGBu14qs4Rxhj25t3PSZkrW9MzHFjW6xRVW/Ev9et7bSA2yJQbGDJD4n86ASaT+i4dptp9ZPo34rZvfh0Vk16yawiqEOBjQ4oLK8sfWNiHwiuAI/y8VJC836Y6Lm8ISDFafbEKcDS2+uUMKj+tmRmnItXFWvDDadOE9t2dLwP8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711677272; c=relaxed/simple; bh=6SSatiKOQPv2797ukkf5H5el6HcxXqvKZ4hc3ZglMjM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=KFoRSQuVLYnejxia4d40iphBOJthyLcNtj/461CIKa/qgEXXrbtaMD+OmuKX8ydYAF/9EhEn/eN90IP3pDBdG+nHdYifEmVruMvkRayhMYNjclz8+Jq54yrf1a/kEP0j7WzIA7GDaV3FD9k3EVb9QudF29r/VZXtMTWPNGMZi+g= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--drosen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=umdU8kKN; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--drosen.bounces.google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc6b26783b4so2186937276.0 for ; Thu, 28 Mar 2024 18:54:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711677270; x=1712282070; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=jC8NburpJ0gtA38yoeQoqc6ho+qO84rwoWBCmC/NNxg=; b=umdU8kKNdDEYQlPari+AHAKgh9RMcDZANNe5jlbScNPHEInszqxpAouo5u4l6Pdj8K uNq3kY9TBRMHdnyGujp5rYamBIaHlpTHf0PEYp8IcodZ3N4YBSVzJ8cQ6r9jUXG22+QJ ydNu08vkn8phta/LqumOpmexjHG8MavdOTRPjSyhNqm+Ez6xmnhkvIij01VUsjiCywnJ d5XEyD5QV9kQXY3w+UpavVJ9ESF6c9Pgk1H5SENpnuNMfe4Uqs6x3F724Od6x2uQH2Zp lYFtIiSa4SIirEon1EQqCm5VcpATEGyl5G1/i4cNYtL1i6UTp0p4Z3i/9RliiIecusJf WuwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711677270; x=1712282070; 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=jC8NburpJ0gtA38yoeQoqc6ho+qO84rwoWBCmC/NNxg=; b=koDCZQmIOx4OZy2RyLmK2VhT2NIVrFIx5qi9E4lK4+f4Lus+KHQM2XELmVqR86fu8d GqYjrvjD39qmQwQpsCzW5F33B+kqUJRUbMop4JncR+rvaf3UwsKPvWnCd32IlIuSwPp4 A9pnudAy80dFYH1+skAr7cUyW4NFPmTnQTt90bpbUzZV16rt0B//j7qYNhxHNoL/Uwdx TdLHGfBXU59BcheTeeZeoOBXn8CV8pUW5vboOFHfaVOWdpOjGi6HHRY28+G58r2jIK5H Gjf+M380ArwPaGpayFNr12OK2tpar4y9bH/uKzWqFwXNxPMTn0f0NMQxI1szbkUUBF5+ +cLg== X-Forwarded-Encrypted: i=1; AJvYcCW9o6XQ4StytCam7IlGI1vfanZztJ9gTcaeIyf3P2R1spHYszj3qFiKihKb115OxI39hL9gp6O+iHXR0mWKUgFandXx0hwJiNYu8UmE X-Gm-Message-State: AOJu0YwTiK17YsDs42ZuuucXusMfbK+44vWYi9836Vni9jrvNkHr/LGs 1pnzcbP041PJlHIDg44xvq7GUZNjMDlwNw5rY7sr4npj+U/uUtoCFG0UHtxXf26/nOqXvYzb1TW VqQ== X-Received: from drosen.mtv.corp.google.com ([2620:15c:211:201:fcce:d6ab:804c:b94b]) (user=drosen job=sendgmr) by 2002:a05:6902:160f:b0:dc6:ebd4:cca2 with SMTP id bw15-20020a056902160f00b00dc6ebd4cca2mr72791ybb.11.1711677270016; Thu, 28 Mar 2024 18:54:30 -0700 (PDT) Date: Thu, 28 Mar 2024 18:53:29 -0700 In-Reply-To: <20240329015351.624249-1-drosen@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240329015351.624249-1-drosen@google.com> X-Mailer: git-send-email 2.44.0.478.gd926399ef9-goog Message-ID: <20240329015351.624249-15-drosen@google.com> Subject: [RFC PATCH v4 14/36] 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 , Eduard Zingerman , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , Jonathan Corbet , Joanne Koong , Mykola Lysenko , Christian Brauner , kernel-team@android.com, Daniel Rosenberg , Paul Lawrence 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 | 202 ++++++++++++++++++++++++++++++++++++++ fs/fuse/fuse_i.h | 6 ++ fs/fuse/readdir.c | 5 + include/uapi/linux/fuse.h | 6 ++ 4 files changed, 219 insertions(+) diff --git a/fs/fuse/backing.c b/fs/fuse/backing.c index c813237b6599..0182236c2735 100644 --- a/fs/fuse/backing.c +++ b/fs/fuse/backing.c @@ -1657,6 +1657,208 @@ int fuse_bpf_unlink(int *out, struct inode *dir, struct dentry *entry) dir, entry); } +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 fuse_read_args *args, + struct file *file, struct dir_context *ctx, + bool *force_again, bool *allow_force, bool is_continued) +{ + struct fuse_file *ff = file->private_data; + + *fa = (struct bpf_fuse_args) { + .info = (struct bpf_fuse_meta_info) { + .nodeid = ff->nodeid, + .opcode = FUSE_READDIR, + }, + .in_numargs = 1, + .in_args[0] = (struct bpf_fuse_arg) { + .size = sizeof(args->in), + .value = &args->in, + }, + }; + + args->in = (struct fuse_read_in) { + .fh = ff->fh, + .offset = ctx->pos, + .size = PAGE_SIZE, + }; + + *force_again = false; + *allow_force = true; + return 0; +} + +static int fuse_readdir_initialize_out(struct bpf_fuse_args *fa, struct fuse_read_args *args, + struct file *file, struct dir_context *ctx, + bool *force_again, bool *allow_force, bool is_continued) +{ + u8 *page = (u8 *)__get_free_page(GFP_KERNEL); + + if (!page) + return -ENOMEM; + + fa->flags = FUSE_BPF_OUT_ARGVAR; + fa->out_numargs = 2; + fa->out_args[0] = (struct bpf_fuse_arg) { + .size = sizeof(args->out), + .value = &args->out, + }; + fa->out_args[1] = (struct bpf_fuse_arg) { + .is_buffer = true, + .buffer = &args->buffer, + }; + args->out = (struct fuse_read_out) { + .again = 0, + .offset = 0, + }; + args->buffer = (struct fuse_buffer) { + .data = page, + .size = PAGE_SIZE, + .alloc_size = PAGE_SIZE, + .max_size = PAGE_SIZE, + .flags = 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 = container_of(ctx, struct fusebpf_ctx, ctx); + struct fuse_dirent *fd = (struct fuse_dirent *)(ec->addr + ec->offset); + + if (ec->offset + sizeof(struct fuse_dirent) + namelen > PAGE_SIZE) + return false; + + *fd = (struct fuse_dirent) { + .ino = ino, + .off = offset, + .namelen = namelen, + .type = d_type, + }; + + memcpy(fd->name, name, namelen); + ec->offset += FUSE_DIRENT_SIZE(fd); + + return true; +} + +static int parse_dirfile(char *buf, size_t nbytes, struct dir_context *ctx, + loff_t next_offset) +{ + char *buf_start = buf; + + while (nbytes >= FUSE_NAME_OFFSET) { + struct fuse_dirent *dirent = (struct fuse_dirent *) buf; + size_t reclen = FUSE_DIRENT_SIZE(dirent); + + if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX) + return -EIO; + if (reclen > nbytes) + break; + if (memchr(dirent->name, '/', dirent->namelen) != NULL) + return -EIO; + + ctx->pos = dirent->off; + if (!dir_emit(ctx, dirent->name, dirent->namelen, dirent->ino, + dirent->type)) { + // If we can't make any progress, user buffer is too small + if (buf == buf_start) + return -EINVAL; + else + return 0; + } + + buf += reclen; + nbytes -= reclen; + } + ctx->pos = next_offset; + + 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 = file->private_data; + struct file *backing_dir = ff->backing_file; + struct fuse_read_out *fro = fa->out_args[0].value; + struct fusebpf_ctx ec; + + ec = (struct fusebpf_ctx) { + .ctx.actor = filldir, + .ctx.pos = ctx->pos, + .addr = fa->out_args[1].buffer->data, + }; + + if (!ec.addr) + return -ENOMEM; + + if (!is_continued) + backing_dir->f_pos = file->f_pos; + + *out = iterate_dir(backing_dir, &ec.ctx); + if (ec.offset == 0) + *allow_force = false; + fa->out_args[1].buffer->size = ec.offset; + + fro->offset = ec.ctx.pos; + fro->again = 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 = fa->out_args[0].value; + struct fuse_file *ff = file->private_data; + struct file *backing_dir = ff->backing_file; + + *out = parse_dirfile(fa->out_args[1].buffer->data, fa->out_args[1].buffer->size, ctx, fro->offset); + *force_again = !!fro->again; + if (*force_again && !*allow_force) + *out = -EINVAL; + + backing_dir->f_pos = ctx->pos; + + free_page((unsigned long)fa->out_args[1].buffer->data); + return *out; +} + +int fuse_bpf_readdir(int *out, struct inode *inode, struct file *file, struct dir_context *ctx) +{ + int ret; + bool allow_force; + bool force_again = false; + bool is_continued = false; + +again: + ret = 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 >= 0) { + is_continued = 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 bd187dbf20b2..ab52003de194 100644 --- a/fs/fuse/fuse_i.h +++ b/fs/fuse/fuse_i.h @@ -1452,6 +1452,7 @@ int fuse_bpf_file_read_iter(ssize_t *out, struct inode *inode, struct kiocb *ioc int fuse_bpf_file_write_iter(ssize_t *out, struct inode *inode, struct kiocb *iocb, struct iov_iter *from); int fuse_bpf_file_fallocate(int *out, struct inode *inode, struct file *file, 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, struct dir_context *ctx); int fuse_bpf_access(int *out, struct inode *inode, int mask); #else @@ -1522,6 +1523,11 @@ static inline int fuse_bpf_lookup(struct dentry **out, struct inode *dir, struct return 0; } +static inline int fuse_bpf_readdir(int *out, struct inode *inode, struct file *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 c66a54d6c7d3..53a1fd756772 100644 --- a/fs/fuse/readdir.c +++ b/fs/fuse/readdir.c @@ -20,6 +20,8 @@ static bool fuse_use_readdirplus(struct inode *dir, struct dir_context *ctx) if (!fc->do_readdirplus) return false; + if (fi->nodeid == 0) + return false; if (!fc->readdirplus_auto) return true; if (test_and_clear_bit(FUSE_I_ADVISE_RDPLUS, &fi->state)) @@ -592,6 +594,9 @@ int fuse_readdir(struct file *file, struct dir_context *ctx) if (fuse_is_bad(inode)) return -EIO; + if (fuse_bpf_readdir(&err, inode, file, ctx)) + return err; + mutex_lock(&ff->readdir.lock); err = UNCACHED; diff --git a/include/uapi/linux/fuse.h b/include/uapi/linux/fuse.h index 8efaa9eecc5f..3417717c1a55 100644 --- a/include/uapi/linux/fuse.h +++ b/include/uapi/linux/fuse.h @@ -811,6 +811,12 @@ struct fuse_read_in { uint32_t padding; }; +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; -- 2.44.0.478.gd926399ef9-goog