Received: by 2002:a05:6358:53a8:b0:117:f937:c515 with SMTP id z40csp4755949rwe; Mon, 17 Apr 2023 18:43:32 -0700 (PDT) X-Google-Smtp-Source: AKy350YXatQqBkanh16qoVZDut5Xug2OzE5jziC86BmhRPD89yvKCNd83b3luZ370kRPsBRbvscY X-Received: by 2002:a17:90b:1093:b0:23b:d506:226e with SMTP id gj19-20020a17090b109300b0023bd506226emr401489pjb.42.1681782212116; Mon, 17 Apr 2023 18:43:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681782212; cv=none; d=google.com; s=arc-20160816; b=jKPDiojgKHC2JZiHByIghEFt5U3faQkWOzZJY54sqm27Beg9bzxKr2JNj7bGElM/O2 l6xKdWaNpKA55e9HGXxUku9dD1RW1IJVps3Sd8ZXrhobdl03PuFRdFNvqa+0OiOMQ+yf 4Blf5kFXLkgN02YruH1bVDgD5D9jtGR0SpSLVfnQmaSxDXQjKbaA5giAY5qx6ANh6kyI hWM+N3fn5hLnsUidV/WfJme02nlTVSJqmnZsTR+MwGLWpFLqTOduw/fIWppQIhk7EiYS bNOhuxjG7GxUL27VNXhkeeC0gqNsSICGqwHNX1gwipqhbicy1iXkKcin8qPOcFbmHbTk bIYQ== 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=NYL4IdJjh78Mx0ERjHXq1HM7WDhJyXfUI9ZLvdFWk98=; b=ZqJdVhSGnnOm4jFFErmjfx7J0FAcBXVQtRXsKm3H9LFo+WTOH3orCC4vkvwjebkT/o XoSWGVNIioCbsVwEld4nhoKGMvj/7/wnXruha6OiUDSVi83D+rZXV8mfAAtFMpC5Mxqs moUQPuKEQu1wpZnlJwkp6joYMvC0cOrfAUvXRZloi0znDfx03XpOw6KuMn9i31e2tVZB /nF1Vo58IPpfU+PyfgBu49hbCK6uYqr/sFLgJ+OAR+top5i1hbWY95AAhp4P3a58TPjJ MzNRaN3NuoEvntd/lwYVuSpMzcf0WkGEAKdtLGAzytfY/zqtITppAj1668yc/IPF7aop 7CSQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=gDPc8KFB; 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 78-20020a630151000000b0050be441fea9si13084502pgb.806.2023.04.17.18.43.18; Mon, 17 Apr 2023 18:43:32 -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=gDPc8KFB; 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 S230256AbjDRBmz (ORCPT + 99 others); Mon, 17 Apr 2023 21:42:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53844 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230373AbjDRBmQ (ORCPT ); Mon, 17 Apr 2023 21:42:16 -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 84D217AAE for ; Mon, 17 Apr 2023 18:41:23 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 132-20020a250c8a000000b00b8f4e12dd57so13411469ybm.1 for ; Mon, 17 Apr 2023 18:41:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1681782080; x=1684374080; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=NYL4IdJjh78Mx0ERjHXq1HM7WDhJyXfUI9ZLvdFWk98=; b=gDPc8KFBWBSDxYuCP48vLLO3Vue1Z2AubLCuuloL4zyW7pB2dhVnlUg0914m1YDuUd 6Ne1KGXCk4u/o9jyWFV2XXPimdtwupUUcvgkfbZVD+J5Tucff+wx4eEkpGMKftKAyHVg ggIWdp57XhPiL081zazQF4KJrhUtgo1n62jsm/nCPnSpTOEEa0QdfXLnA7/I0/JPpbtE VeS6W6NpNQNbK8tDqcm+zMNAS2xTiCwbBK1HqQrGP1vXMzw3/f3jYAJNVlOzDCvIkqDR jKpbPZPCr3xy77kbdxnHzURMl+sPneGV3POnYfCtJgR+D28OHQNxJkj6v0N/ovT13U9O wz4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681782080; x=1684374080; 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=NYL4IdJjh78Mx0ERjHXq1HM7WDhJyXfUI9ZLvdFWk98=; b=M/ZyxA11jmAWvNaj5V6cQ8NKKap4uhKW9vNkrVVanxv+71cRwDGIJ+ozEkwk/3uZB0 wKzayWHv5brrD6ut4wEHQg7pwoyah/8Z2idjzgB2REymP8kB60ZlI2fKefYgn9830ekq pOTYGwzbUD2R+2QXJ5xxjVIKiH8VbOtX4BrAmVrJPtOiV6xGDfpzHzJ1+Dm6LxeRaJ5x cHmgvJ3P8sDCcbyt7EQX+KpwMWLqNWZSYGeFG7vjLDLi9m5oiYIuatE1de/3REwfvCpQ YB/e6/31fibpCi+KB20o+b3Vi6v+zU547giBX1OpRpnFomI8ekWQATYA0kmT1rLFWneC QYUQ== X-Gm-Message-State: AAQBX9cfjqcSimjALmfB3L/8vW8RRKqy4KbwKu3kT/8bnhRYRh3A++qw lP1Ovx0YY2yPVNJMnqzF8KSJ+iLDY8s= X-Received: from drosen.mtv.corp.google.com ([2620:15c:211:201:e67a:98b0:942d:86aa]) (user=drosen job=sendgmr) by 2002:a0d:ec02:0:b0:54c:2723:560d with SMTP id q2-20020a0dec02000000b0054c2723560dmr10854311ywn.3.1681782080382; Mon, 17 Apr 2023 18:41:20 -0700 (PDT) Date: Mon, 17 Apr 2023 18:40:13 -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-14-drosen@google.com> Subject: [RFC PATCH v3 13/37] fuse-bpf: Add lseek 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=unavailable 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_LSEEK Signed-off-by: Daniel Rosenberg Signed-off-by: Paul Lawrence --- fs/fuse/backing.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++ fs/fuse/file.c | 3 ++ fs/fuse/fuse_i.h | 6 ++++ 3 files changed, 98 insertions(+) diff --git a/fs/fuse/backing.c b/fs/fuse/backing.c index 930aa370e376..c4916dde48c8 100644 --- a/fs/fuse/backing.c +++ b/fs/fuse/backing.c @@ -207,6 +207,95 @@ static void fuse_stat_to_attr(struct fuse_conn *fc, struct inode *inode, attr->blksize = 1 << blkbits; } +struct fuse_lseek_args { + struct fuse_lseek_in in; + struct fuse_lseek_out out; +}; + +static int fuse_lseek_initialize_in(struct bpf_fuse_args *fa, struct fuse_lseek_args *args, + struct file *file, loff_t offset, int whence) +{ + struct fuse_file *fuse_file = file->private_data; + + args->in = (struct fuse_lseek_in) { + .fh = fuse_file->fh, + .offset = offset, + .whence = whence, + }; + + *fa = (struct bpf_fuse_args) { + .info = (struct bpf_fuse_meta_info) { + .nodeid = get_node_id(file->f_inode), + .opcode = FUSE_LSEEK, + }, + .in_numargs = 1, + .in_args[0].size = sizeof(args->in), + .in_args[0].value = &args->in, + }; + + return 0; +} + +static int fuse_lseek_initialize_out(struct bpf_fuse_args *fa, struct fuse_lseek_args *args, + struct file *file, loff_t offset, int whence) +{ + fa->out_numargs = 1; + fa->out_args[0].size = sizeof(args->out); + fa->out_args[0].value = &args->out; + + return 0; +} + +static int fuse_lseek_backing(struct bpf_fuse_args *fa, loff_t *out, + struct file *file, loff_t offset, int whence) +{ + const struct fuse_lseek_in *fli = fa->in_args[0].value; + struct fuse_lseek_out *flo = fa->out_args[0].value; + struct fuse_file *fuse_file = file->private_data; + struct file *backing_file = fuse_file->backing_file; + + /* TODO: Handle changing of the file handle */ + if (offset == 0) { + if (whence == SEEK_CUR) { + flo->offset = file->f_pos; + *out = flo->offset; + return 0; + } + + if (whence == SEEK_SET) { + flo->offset = vfs_setpos(file, 0, 0); + *out = flo->offset; + return 0; + } + } + + inode_lock(file->f_inode); + backing_file->f_pos = file->f_pos; + *out = vfs_llseek(backing_file, fli->offset, fli->whence); + flo->offset = *out; + inode_unlock(file->f_inode); + return 0; +} + +static int fuse_lseek_finalize(struct bpf_fuse_args *fa, loff_t *out, + struct file *file, loff_t offset, int whence) +{ + struct fuse_lseek_out *flo = fa->out_args[0].value; + + if (!fa->info.error_in) + file->f_pos = flo->offset; + *out = flo->offset; + return 0; +} + +int fuse_bpf_lseek(loff_t *out, struct inode *inode, struct file *file, loff_t offset, int whence) +{ + return bpf_fuse_backing(inode, struct fuse_lseek_args, out, + fuse_lseek_initialize_in, fuse_lseek_initialize_out, + fuse_lseek_backing, fuse_lseek_finalize, + file, offset, whence); +} + ssize_t fuse_backing_mmap(struct file *file, struct vm_area_struct *vma) { int ret; diff --git a/fs/fuse/file.c b/fs/fuse/file.c index 865167a80d35..9758bd1665a6 100644 --- a/fs/fuse/file.c +++ b/fs/fuse/file.c @@ -2779,6 +2779,9 @@ static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence) loff_t retval; struct inode *inode = file_inode(file); + if (fuse_bpf_lseek(&retval, inode, file, offset, whence)) + return retval; + switch (whence) { case SEEK_SET: case SEEK_CUR: diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h index 5eb357f482dc..624d0cebd287 100644 --- a/fs/fuse/fuse_i.h +++ b/fs/fuse/fuse_i.h @@ -1405,11 +1405,17 @@ int parse_fuse_bpf_entry(struct fuse_bpf_entry *fbe, int num_entries); #ifdef CONFIG_FUSE_BPF +int fuse_bpf_lseek(loff_t *out, struct inode *inode, struct file *file, loff_t offset, int whence); int fuse_bpf_lookup(struct dentry **out, struct inode *dir, struct dentry *entry, unsigned int flags); int fuse_bpf_access(int *out, struct inode *inode, int mask); #else +static inline int fuse_bpf_lseek(loff_t *out, struct inode *inode, struct file *file, loff_t offset, int whence) +{ + return 0; +} + static inline int fuse_bpf_lookup(struct dentry **out, struct inode *dir, struct dentry *entry, unsigned int flags) { return 0; -- 2.40.0.634.g4ca3ef3211-goog