From nobody Mon Sep 15 08:07:08 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 14CDDC54EBD for ; Fri, 13 Jan 2023 15:46:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229988AbjAMPqr (ORCPT ); Fri, 13 Jan 2023 10:46:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35630 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229894AbjAMPpi (ORCPT ); Fri, 13 Jan 2023 10:45:38 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 17FA7D66 for ; Fri, 13 Jan 2023 07:35:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1673624119; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=z/9uybYBXhUW4wkIRlveDlKdZJQqTPtcnsCUTg2vO2o=; b=bQuE4e0Pvf4XQimFJWbxlsB9JSMKJfLE7uWOQ8gA5bRCJ+mxIm+eN/y7uN/JbLIjZXlxzT CWctCQEwr9EIAiSgKf7NzeH9ZgLwrl5hhbPxSRymBiQxyi5G3I52ES/sBWyiQIFqxP4SQI dPAxB25q5L54IFQLfyVpb/G4wcYMrqs= Received: from mail-lj1-f199.google.com (mail-lj1-f199.google.com [209.85.208.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-606-Hmz27GeVNDSEm2YXfLoonQ-1; Fri, 13 Jan 2023 10:35:16 -0500 X-MC-Unique: Hmz27GeVNDSEm2YXfLoonQ-1 Received: by mail-lj1-f199.google.com with SMTP id s12-20020a2eb8cc000000b0027f6f40eeb3so5634875ljp.2 for ; Fri, 13 Jan 2023 07:35:16 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=z/9uybYBXhUW4wkIRlveDlKdZJQqTPtcnsCUTg2vO2o=; b=QASTeN1Idax6la/i6MMNpx3T66eNx2KWcmukg7AbpTf0kYjnqAcuGjVkGINybg4Rt9 qtJ7WeLRpcYTR0aANFD6bDMdcFPnvujSGVutPyLYUyPUwj5iN49+81Pnk8MdryiRReY7 R6xRB1YW7RsjrhRVkUaeStm9o8P2l4wFTnbCVovNjyQ7tXJrane17pG99IWpEX5/2R8X G3J8y3fQjb0DxJwXDKYN7d1bNz6uyUqwFcu0YwqVJWHgS8AXUi5VFrSeAWbs4IP1szH1 XZ9PF+EDNknrDEwuhZl3Big3IdaqFUEiXhjvVTfbtTvKLtgC/SdstCJ7VwfMTp3RDSkd 2zKw== X-Gm-Message-State: AFqh2koFUnV4+gzhvbswlVi+XXG9sXeDd5eXNMwzSWnBY+ChxUBcsCU8 MAHUHDnDp1+OfqcrSt26NABqlLlSb46zbnNv3BiVzC5F+TjgjtEC4hv+EbLwtT4lDGRpf+JaJg3 YTlr0MkV1Buljnr4HqE76xqVC X-Received: by 2002:a05:651c:200f:b0:27f:bc6c:c4f0 with SMTP id s15-20020a05651c200f00b0027fbc6cc4f0mr15465554ljo.27.1673624114672; Fri, 13 Jan 2023 07:35:14 -0800 (PST) X-Google-Smtp-Source: AMrXdXtYodRfUAStZmVCxOmY2+uknl4/INwWxiaczsDBucjw7JhTayRFAlBAHSfshWHL/RiiUhmiHw== X-Received: by 2002:a05:651c:200f:b0:27f:bc6c:c4f0 with SMTP id s15-20020a05651c200f00b0027fbc6cc4f0mr15465545ljo.27.1673624114436; Fri, 13 Jan 2023 07:35:14 -0800 (PST) Received: from localhost.localdomain (c-e6a5e255.022-110-73746f36.bbcust.telenor.se. [85.226.165.230]) by smtp.googlemail.com with ESMTPSA id p20-20020a2e9a94000000b00289bb528b8dsm725473lji.49.2023.01.13.07.35.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 07:35:13 -0800 (PST) From: Alexander Larsson To: linux-fsdevel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, gscrivan@redhat.com, Alexander Larsson Subject: [PATCH v2 4/6] composefs: Add filesystem implementation Date: Fri, 13 Jan 2023 16:33:57 +0100 Message-Id: X-Mailer: git-send-email 2.39.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This is the basic inode and filesystem implementation. Signed-off-by: Alexander Larsson Co-developed-by: Giuseppe Scrivano Signed-off-by: Giuseppe Scrivano --- fs/composefs/cfs.c | 903 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 903 insertions(+) create mode 100644 fs/composefs/cfs.c diff --git a/fs/composefs/cfs.c b/fs/composefs/cfs.c new file mode 100644 index 000000000000..b3c0adb69983 --- /dev/null +++ b/fs/composefs/cfs.c @@ -0,0 +1,903 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * composefs + * + * Copyright (C) 2000 Linus Torvalds. + * 2000 Transmeta Corp. + * Copyright (C) 2021 Giuseppe Scrivano + * Copyright (C) 2022 Alexander Larsson + * + * This file is released under the GPL. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "cfs-internals.h" + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Giuseppe Scrivano "); + +#define CFS_MAX_STACK 500 + +#define FILEID_CFS 0x91 + +struct cfs_info { + struct cfs_context_s cfs_ctx; + + char *base_path; + + size_t n_bases; + struct vfsmount **bases; + + u32 verity_check; /* 0 =3D=3D none, 1 =3D=3D if specified in image, 2 =3D= =3D require in image */ + bool has_digest; + u8 digest[SHA256_DIGEST_SIZE]; /* fs-verity digest */ +}; + +struct cfs_inode { + /* must be first for clear in cfs_alloc_inode to work */ + struct inode vfs_inode; + + struct cfs_inode_data_s inode_data; +}; + +static inline struct cfs_inode *CFS_I(struct inode *inode) +{ + return container_of(inode, struct cfs_inode, vfs_inode); +} + +static struct file empty_file; + +static const struct file_operations cfs_file_operations; + +static const struct super_operations cfs_ops; +static const struct file_operations cfs_dir_operations; +static const struct inode_operations cfs_dir_inode_operations; +static const struct inode_operations cfs_file_inode_operations; +static const struct inode_operations cfs_link_inode_operations; + +static const struct xattr_handler *cfs_xattr_handlers[]; +static const struct export_operations cfs_export_operations; + +static const struct address_space_operations cfs_aops =3D { + .direct_IO =3D noop_direct_IO, +}; + +static ssize_t cfs_listxattr(struct dentry *dentry, char *names, size_t si= ze); + +/* copied from overlayfs. */ +static unsigned int cfs_split_basedirs(char *str) +{ + unsigned int ctr =3D 1; + char *s, *d; + + for (s =3D d =3D str;; s++, d++) { + if (*s =3D=3D '\\') { + s++; + } else if (*s =3D=3D ':') { + *d =3D '\0'; + ctr++; + continue; + } + *d =3D *s; + if (!*s) + break; + } + return ctr; +} + +static struct inode *cfs_make_inode(struct cfs_context_s *ctx, + struct super_block *sb, ino_t ino_num, + struct cfs_inode_s *ino, const struct inode *dir) +{ + struct cfs_inode_data_s inode_data =3D { 0 }; + struct cfs_xattr_header_s *xattrs =3D NULL; + struct inode *inode =3D NULL; + struct cfs_inode *cino; + int ret, res; + + res =3D cfs_init_inode_data(ctx, ino, ino_num, &inode_data); + if (res < 0) + return ERR_PTR(res); + + inode =3D new_inode(sb); + if (inode) { + inode_init_owner(&init_user_ns, inode, dir, ino->st_mode); + inode->i_mapping->a_ops =3D &cfs_aops; + + cino =3D CFS_I(inode); + cino->inode_data =3D inode_data; + + inode->i_ino =3D ino_num; + set_nlink(inode, ino->st_nlink); + inode->i_rdev =3D ino->st_rdev; + inode->i_uid =3D make_kuid(current_user_ns(), ino->st_uid); + inode->i_gid =3D make_kgid(current_user_ns(), ino->st_gid); + inode->i_mode =3D ino->st_mode; + inode->i_atime =3D ino->st_mtim; + inode->i_mtime =3D ino->st_mtim; + inode->i_ctime =3D ino->st_ctim; + + switch (ino->st_mode & S_IFMT) { + case S_IFREG: + inode->i_op =3D &cfs_file_inode_operations; + inode->i_fop =3D &cfs_file_operations; + inode->i_size =3D ino->st_size; + break; + case S_IFLNK: + inode->i_link =3D cino->inode_data.path_payload; + inode->i_op =3D &cfs_link_inode_operations; + inode->i_fop =3D &cfs_file_operations; + break; + case S_IFDIR: + inode->i_op =3D &cfs_dir_inode_operations; + inode->i_fop =3D &cfs_dir_operations; + inode->i_size =3D 4096; + break; + case S_IFCHR: + case S_IFBLK: + if (current_user_ns() !=3D &init_user_ns) { + ret =3D -EPERM; + goto fail; + } + fallthrough; + default: + inode->i_op =3D &cfs_file_inode_operations; + init_special_inode(inode, ino->st_mode, ino->st_rdev); + break; + } + } + return inode; + +fail: + if (inode) + iput(inode); + kfree(xattrs); + cfs_inode_data_put(&inode_data); + return ERR_PTR(ret); +} + +static struct inode *cfs_get_root_inode(struct super_block *sb) +{ + struct cfs_info *fsi =3D sb->s_fs_info; + struct cfs_inode_s ino_buf; + struct cfs_inode_s *ino; + u64 index; + + ino =3D cfs_get_root_ino(&fsi->cfs_ctx, &ino_buf, &index); + if (IS_ERR(ino)) + return ERR_CAST(ino); + + return cfs_make_inode(&fsi->cfs_ctx, sb, index, ino, NULL); +} + +static bool cfs_iterate_cb(void *private, const char *name, int name_len, + u64 ino, unsigned int dtype) +{ + struct dir_context *ctx =3D private; + + if (!dir_emit(ctx, name, name_len, ino, dtype)) + return 0; + + ctx->pos++; + return 1; +} + +static int cfs_iterate(struct file *file, struct dir_context *ctx) +{ + struct inode *inode =3D file->f_inode; + struct cfs_info *fsi =3D inode->i_sb->s_fs_info; + struct cfs_inode *cino =3D CFS_I(inode); + + if (!dir_emit_dots(file, ctx)) + return 0; + + return cfs_dir_iterate(&fsi->cfs_ctx, inode->i_ino, &cino->inode_data, + ctx->pos - 2, cfs_iterate_cb, ctx); +} + +static struct dentry *cfs_lookup(struct inode *dir, struct dentry *dentry, + unsigned int flags) +{ + struct cfs_info *fsi =3D dir->i_sb->s_fs_info; + struct cfs_inode *cino =3D CFS_I(dir); + struct cfs_inode_s ino_buf; + struct cfs_inode_s *ino_s; + struct inode *inode; + u64 index; + int ret; + + if (dentry->d_name.len > NAME_MAX) + return ERR_PTR(-ENAMETOOLONG); + + ret =3D cfs_dir_lookup(&fsi->cfs_ctx, dir->i_ino, &cino->inode_data, + dentry->d_name.name, dentry->d_name.len, &index); + if (ret < 0) + return ERR_PTR(ret); + if (ret =3D=3D 0) + goto return_negative; + + ino_s =3D cfs_get_ino_index(&fsi->cfs_ctx, index, &ino_buf); + if (IS_ERR(ino_s)) + return ERR_CAST(ino_s); + + inode =3D cfs_make_inode(&fsi->cfs_ctx, dir->i_sb, index, ino_s, dir); + if (IS_ERR(inode)) + return ERR_CAST(inode); + + return d_splice_alias(inode, dentry); + +return_negative: + d_add(dentry, NULL); + return NULL; +} + +static const struct file_operations cfs_dir_operations =3D { + .llseek =3D generic_file_llseek, + .read =3D generic_read_dir, + .iterate_shared =3D cfs_iterate, +}; + +static const struct inode_operations cfs_dir_inode_operations =3D { + .lookup =3D cfs_lookup, + .listxattr =3D cfs_listxattr, +}; + +static const struct inode_operations cfs_link_inode_operations =3D { + .get_link =3D simple_get_link, + .listxattr =3D cfs_listxattr, +}; + +static int digest_from_string(const char *digest_str, u8 *digest) +{ + int res; + + res =3D hex2bin(digest, digest_str, SHA256_DIGEST_SIZE); + if (res < 0) + return res; + + if (digest_str[2 * SHA256_DIGEST_SIZE] !=3D 0) + return -EINVAL; /* Too long string */ + + return 0; +} + +/* + * Display the mount options in /proc/mounts. + */ +static int cfs_show_options(struct seq_file *m, struct dentry *root) +{ + struct cfs_info *fsi =3D root->d_sb->s_fs_info; + + if (fsi->base_path) + seq_show_option(m, "basedir", fsi->base_path); + if (fsi->has_digest) + seq_printf(m, ",digest=3D%*phN", SHA256_DIGEST_SIZE, fsi->digest); + if (fsi->verity_check !=3D 0) + seq_printf(m, ",verity_check=3D%u", fsi->verity_check); + + return 0; +} + +static struct kmem_cache *cfs_inode_cachep; + +static struct inode *cfs_alloc_inode(struct super_block *sb) +{ + struct cfs_inode *cino =3D alloc_inode_sb(sb, cfs_inode_cachep, GFP_KERNE= L); + + if (!cino) + return NULL; + + memset((u8 *)cino + sizeof(struct inode), 0, + sizeof(struct cfs_inode) - sizeof(struct inode)); + + return &cino->vfs_inode; +} + +static void cfs_destroy_inode(struct inode *inode) +{ + struct cfs_inode *cino =3D CFS_I(inode); + + cfs_inode_data_put(&cino->inode_data); +} + +static void cfs_free_inode(struct inode *inode) +{ + struct cfs_inode *cino =3D CFS_I(inode); + + kmem_cache_free(cfs_inode_cachep, cino); +} + +static void cfs_put_super(struct super_block *sb) +{ + struct cfs_info *fsi =3D sb->s_fs_info; + + cfs_ctx_put(&fsi->cfs_ctx); + if (fsi->bases) { + kern_unmount_array(fsi->bases, fsi->n_bases); + kfree(fsi->bases); + } + kfree(fsi->base_path); + + kfree(fsi); +} + +static int cfs_statfs(struct dentry *dentry, struct kstatfs *buf) +{ + struct cfs_info *fsi =3D dentry->d_sb->s_fs_info; + int err =3D 0; + + /* We return the free space, etc from the first base dir. */ + if (fsi->n_bases > 0) { + struct path root =3D { .mnt =3D fsi->bases[0], + .dentry =3D fsi->bases[0]->mnt_root }; + err =3D vfs_statfs(&root, buf); + } + + if (!err) { + buf->f_namelen =3D NAME_MAX; + buf->f_type =3D dentry->d_sb->s_magic; + } + + return err; +} + +static const struct super_operations cfs_ops =3D { + .statfs =3D cfs_statfs, + .drop_inode =3D generic_delete_inode, + .show_options =3D cfs_show_options, + .put_super =3D cfs_put_super, + .destroy_inode =3D cfs_destroy_inode, + .alloc_inode =3D cfs_alloc_inode, + .free_inode =3D cfs_free_inode, +}; + +enum cfs_param { + Opt_base_path, + Opt_digest, + Opt_verity_check, +}; + +const struct fs_parameter_spec cfs_parameters[] =3D { + fsparam_string("basedir", Opt_base_path), + fsparam_string("digest", Opt_digest), + fsparam_u32("verity_check", Opt_verity_check), + {} +}; + +static int cfs_parse_param(struct fs_context *fc, struct fs_parameter *par= am) +{ + struct cfs_info *fsi =3D fc->s_fs_info; + struct fs_parse_result result; + int opt, r; + + opt =3D fs_parse(fc, cfs_parameters, param, &result); + if (opt =3D=3D -ENOPARAM) + return vfs_parse_fs_param_source(fc, param); + if (opt < 0) + return opt; + + switch (opt) { + case Opt_base_path: + kfree(fsi->base_path); + /* Take ownership. */ + fsi->base_path =3D param->string; + param->string =3D NULL; + break; + case Opt_digest: + r =3D digest_from_string(param->string, fsi->digest); + if (r < 0) + return r; + fsi->has_digest =3D true; + fsi->verity_check =3D 2; /* Default to full verity check */ + break; + case Opt_verity_check: + if (result.uint_32 > 2) + return invalfc(fc, "Invalid verity_check mode"); + fsi->verity_check =3D result.uint_32; + break; + } + + return 0; +} + +static struct vfsmount *resolve_basedir(const char *name) +{ + struct path path =3D {}; + struct vfsmount *mnt; + int err =3D -EINVAL; + + if (!*name) { + pr_err("empty basedir\n"); + goto out; + } + err =3D kern_path(name, LOOKUP_FOLLOW, &path); + if (err) { + pr_err("failed to resolve '%s': %i\n", name, err); + goto out; + } + + mnt =3D clone_private_mount(&path); + err =3D PTR_ERR(mnt); + if (IS_ERR(mnt)) { + pr_err("failed to clone basedir\n"); + goto out_put; + } + + path_put(&path); + + /* Don't inherit atime flags */ + mnt->mnt_flags &=3D ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME); + + return mnt; + +out_put: + path_put(&path); +out: + return ERR_PTR(err); +} + +static int cfs_fill_super(struct super_block *sb, struct fs_context *fc) +{ + struct cfs_info *fsi =3D sb->s_fs_info; + struct vfsmount **bases =3D NULL; + size_t numbasedirs =3D 0; + struct inode *inode; + struct vfsmount *mnt; + int ret; + + if (sb->s_root) + return -EINVAL; + + /* Set up the inode allocator early */ + sb->s_op =3D &cfs_ops; + sb->s_flags |=3D SB_RDONLY; + sb->s_magic =3D CFS_MAGIC; + sb->s_xattr =3D cfs_xattr_handlers; + sb->s_export_op =3D &cfs_export_operations; + + if (fsi->base_path) { + char *lower, *splitlower =3D NULL; + + ret =3D -ENOMEM; + splitlower =3D kstrdup(fsi->base_path, GFP_KERNEL); + if (!splitlower) + goto fail; + + ret =3D -EINVAL; + numbasedirs =3D cfs_split_basedirs(splitlower); + if (numbasedirs > CFS_MAX_STACK) { + pr_err("too many lower directories, limit is %d\n", + CFS_MAX_STACK); + kfree(splitlower); + goto fail; + } + + ret =3D -ENOMEM; + bases =3D kcalloc(numbasedirs, sizeof(struct vfsmount *), GFP_KERNEL); + if (!bases) { + kfree(splitlower); + goto fail; + } + + lower =3D splitlower; + for (size_t i =3D 0; i < numbasedirs; i++) { + mnt =3D resolve_basedir(lower); + if (IS_ERR(mnt)) { + ret =3D PTR_ERR(mnt); + kfree(splitlower); + goto fail; + } + bases[i] =3D mnt; + + lower =3D strchr(lower, '\0') + 1; + } + kfree(splitlower); + } + + /* Must be inited before calling cfs_get_inode. */ + ret =3D cfs_init_ctx(fc->source, fsi->has_digest ? fsi->digest : NULL, + &fsi->cfs_ctx); + if (ret < 0) + goto fail; + + inode =3D cfs_get_root_inode(sb); + if (IS_ERR(inode)) { + ret =3D PTR_ERR(inode); + goto fail; + } + sb->s_root =3D d_make_root(inode); + + ret =3D -ENOMEM; + if (!sb->s_root) + goto fail; + + sb->s_maxbytes =3D MAX_LFS_FILESIZE; + sb->s_blocksize =3D PAGE_SIZE; + sb->s_blocksize_bits =3D PAGE_SHIFT; + + sb->s_time_gran =3D 1; + + fsi->bases =3D bases; + fsi->n_bases =3D numbasedirs; + return 0; +fail: + if (bases) { + for (size_t i =3D 0; i < numbasedirs; i++) { + if (bases[i]) + kern_unmount(bases[i]); + } + kfree(bases); + } + cfs_ctx_put(&fsi->cfs_ctx); + return ret; +} + +static int cfs_get_tree(struct fs_context *fc) +{ + return get_tree_nodev(fc, cfs_fill_super); +} + +static const struct fs_context_operations cfs_context_ops =3D { + .parse_param =3D cfs_parse_param, + .get_tree =3D cfs_get_tree, +}; + +static struct file *open_base_file(struct cfs_info *fsi, struct inode *ino= de, + struct file *file) +{ + struct cfs_inode *cino =3D CFS_I(inode); + struct file *real_file; + char *real_path =3D cino->inode_data.path_payload; + + for (size_t i =3D 0; i < fsi->n_bases; i++) { + real_file =3D file_open_root_mnt(fsi->bases[i], real_path, + file->f_flags, 0); + if (!IS_ERR(real_file) || PTR_ERR(real_file) !=3D -ENOENT) + return real_file; + } + + return ERR_PTR(-ENOENT); +} + +static int cfs_open_file(struct inode *inode, struct file *file) +{ + struct cfs_info *fsi =3D inode->i_sb->s_fs_info; + struct cfs_inode *cino =3D CFS_I(inode); + char *real_path =3D cino->inode_data.path_payload; + struct file *faked_file; + struct file *real_file; + + if (WARN_ON(!file)) + return -EIO; + + if (file->f_flags & (O_WRONLY | O_RDWR | O_CREAT | O_EXCL | O_TRUNC)) + return -EROFS; + + if (!real_path) { + file->private_data =3D &empty_file; + return 0; + } + + if (fsi->verity_check >=3D 2 && !cino->inode_data.has_digest) { + pr_warn("WARNING: composefs image file '%pd' specified no fs-verity dige= st\n", + file->f_path.dentry); + return -EIO; + } + + real_file =3D open_base_file(fsi, inode, file); + + if (IS_ERR(real_file)) + return PTR_ERR(real_file); + + /* If metadata records a digest for the file, ensure it is there + * and correct before using the contents. + */ + if (cino->inode_data.has_digest && fsi->verity_check >=3D 1) { + u8 verity_digest[FS_VERITY_MAX_DIGEST_SIZE]; + enum hash_algo verity_algo; + int res; + + res =3D fsverity_get_digest(d_inode(real_file->f_path.dentry), + verity_digest, &verity_algo); + if (res < 0) { + pr_warn("WARNING: composefs backing file '%pd' has no fs-verity digest\= n", + real_file->f_path.dentry); + fput(real_file); + return -EIO; + } + if (verity_algo !=3D HASH_ALGO_SHA256 || + memcmp(cino->inode_data.digest, verity_digest, + SHA256_DIGEST_SIZE) !=3D 0) { + pr_warn("WARNING: composefs backing file '%pd' has the wrong fs-verity = digest\n", + real_file->f_path.dentry); + fput(real_file); + return -EIO; + } + } + + faked_file =3D open_with_fake_path(&file->f_path, file->f_flags, + real_file->f_inode, current_cred()); + fput(real_file); + + if (IS_ERR(faked_file)) + return PTR_ERR(faked_file); + + file->private_data =3D faked_file; + return 0; +} + +#ifdef CONFIG_MMU +static unsigned long cfs_mmu_get_unmapped_area(struct file *file, unsigned= long addr, + unsigned long len, unsigned long pgoff, + unsigned long flags) +{ + struct file *realfile =3D file->private_data; + + if (realfile =3D=3D &empty_file) + return 0; + + return current->mm->get_unmapped_area(file, addr, len, pgoff, flags); +} +#endif + +static int cfs_release_file(struct inode *inode, struct file *file) +{ + struct file *realfile =3D file->private_data; + + if (WARN_ON(!realfile)) + return -EIO; + + if (realfile =3D=3D &empty_file) + return 0; + + fput(file->private_data); + + return 0; +} + +static int cfs_mmap(struct file *file, struct vm_area_struct *vma) +{ + struct file *realfile =3D file->private_data; + int ret; + + if (realfile =3D=3D &empty_file) + return 0; + + if (!realfile->f_op->mmap) + return -ENODEV; + + if (WARN_ON(file !=3D vma->vm_file)) + return -EIO; + + vma_set_file(vma, realfile); + + ret =3D call_mmap(vma->vm_file, vma); + + return ret; +} + +static ssize_t cfs_read_iter(struct kiocb *iocb, struct iov_iter *iter) +{ + struct file *file =3D iocb->ki_filp; + struct file *realfile =3D file->private_data; + int ret; + + if (realfile =3D=3D &empty_file) + return 0; + + if (!realfile->f_op->read_iter) + return -ENODEV; + + iocb->ki_filp =3D realfile; + ret =3D call_read_iter(realfile, iocb, iter); + iocb->ki_filp =3D file; + + return ret; +} + +static int cfs_fadvise(struct file *file, loff_t offset, loff_t len, int a= dvice) +{ + struct file *realfile =3D file->private_data; + + if (realfile =3D=3D &empty_file) + return 0; + + return vfs_fadvise(realfile, offset, len, advice); +} + +static int cfs_encode_fh(struct inode *inode, u32 *fh, int *max_len, + struct inode *parent) +{ + u32 generation; + int len =3D 3; + u64 nodeid; + + if (*max_len < len) { + *max_len =3D len; + return FILEID_INVALID; + } + + nodeid =3D inode->i_ino; + generation =3D inode->i_generation; + + fh[0] =3D (u32)(nodeid >> 32); + fh[1] =3D (u32)(nodeid & 0xffffffff); + fh[2] =3D generation; + + *max_len =3D len; + + return FILEID_CFS; +} + +static struct dentry *cfs_fh_to_dentry(struct super_block *sb, struct fid = *fid, + int fh_len, int fh_type) +{ + struct cfs_info *fsi =3D sb->s_fs_info; + struct inode *ino; + u64 inode_index; + u32 generation; + + if (fh_type !=3D FILEID_CFS || fh_len < 3) + return NULL; + + inode_index =3D (u64)(fid->raw[0]) << 32; + inode_index |=3D fid->raw[1]; + generation =3D fid->raw[2]; + + ino =3D ilookup(sb, inode_index); + if (!ino) { + struct cfs_inode_s inode_buf; + struct cfs_inode_s *inode; + + inode =3D cfs_get_ino_index(&fsi->cfs_ctx, inode_index, &inode_buf); + if (IS_ERR(inode)) + return ERR_CAST(inode); + + ino =3D cfs_make_inode(&fsi->cfs_ctx, sb, inode_index, inode, NULL); + if (IS_ERR(ino)) + return ERR_CAST(ino); + } + if (ino->i_generation !=3D generation) { + iput(ino); + return ERR_PTR(-ESTALE); + } + return d_obtain_alias(ino); +} + +static struct dentry *cfs_fh_to_parent(struct super_block *sb, struct fid = *fid, + int fh_len, int fh_type) +{ + return ERR_PTR(-EACCES); +} + +static int cfs_get_name(struct dentry *parent, char *name, struct dentry *= child) +{ + WARN_ON_ONCE(1); + return -EIO; +} + +static struct dentry *cfs_get_parent(struct dentry *dentry) +{ + WARN_ON_ONCE(1); + return ERR_PTR(-EIO); +} + +static const struct export_operations cfs_export_operations =3D { + .fh_to_dentry =3D cfs_fh_to_dentry, + .fh_to_parent =3D cfs_fh_to_parent, + .encode_fh =3D cfs_encode_fh, + .get_parent =3D cfs_get_parent, + .get_name =3D cfs_get_name, +}; + +static int cfs_getxattr(const struct xattr_handler *handler, + struct dentry *unused2, struct inode *inode, + const char *name, void *value, size_t size) +{ + struct cfs_info *fsi =3D inode->i_sb->s_fs_info; + struct cfs_inode *cino =3D CFS_I(inode); + + return cfs_get_xattr(&fsi->cfs_ctx, &cino->inode_data, name, value, size); +} + +static ssize_t cfs_listxattr(struct dentry *dentry, char *names, size_t si= ze) +{ + struct inode *inode =3D d_inode(dentry); + struct cfs_info *fsi =3D inode->i_sb->s_fs_info; + struct cfs_inode *cino =3D CFS_I(inode); + + return cfs_list_xattrs(&fsi->cfs_ctx, &cino->inode_data, names, size); +} + +static const struct file_operations cfs_file_operations =3D { + .read_iter =3D cfs_read_iter, + .mmap =3D cfs_mmap, + .fadvise =3D cfs_fadvise, + .fsync =3D noop_fsync, + .splice_read =3D generic_file_splice_read, + .llseek =3D generic_file_llseek, +#ifdef CONFIG_MMU + .get_unmapped_area =3D cfs_mmu_get_unmapped_area, +#endif + .release =3D cfs_release_file, + .open =3D cfs_open_file, +}; + +static const struct xattr_handler cfs_xattr_handler =3D { + .prefix =3D "", /* catch all */ + .get =3D cfs_getxattr, +}; + +static const struct xattr_handler *cfs_xattr_handlers[] =3D { + &cfs_xattr_handler, + NULL, +}; + +static const struct inode_operations cfs_file_inode_operations =3D { + .setattr =3D simple_setattr, + .getattr =3D simple_getattr, + + .listxattr =3D cfs_listxattr, +}; + +static int cfs_init_fs_context(struct fs_context *fc) +{ + struct cfs_info *fsi; + + fsi =3D kzalloc(sizeof(*fsi), GFP_KERNEL); + if (!fsi) + return -ENOMEM; + + fc->s_fs_info =3D fsi; + fc->ops =3D &cfs_context_ops; + return 0; +} + +static struct file_system_type cfs_type =3D { + .owner =3D THIS_MODULE, + .name =3D "composefs", + .init_fs_context =3D cfs_init_fs_context, + .parameters =3D cfs_parameters, + .kill_sb =3D kill_anon_super, +}; + +static void cfs_inode_init_once(void *foo) +{ + struct cfs_inode *cino =3D foo; + + inode_init_once(&cino->vfs_inode); +} + +static int __init init_cfs(void) +{ + cfs_inode_cachep =3D kmem_cache_create( + "cfs_inode", sizeof(struct cfs_inode), 0, + (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT), + cfs_inode_init_once); + if (!cfs_inode_cachep) + return -ENOMEM; + + return register_filesystem(&cfs_type); +} + +static void __exit exit_cfs(void) +{ + unregister_filesystem(&cfs_type); + + /* Ensure all RCU free inodes are safe to be destroyed. */ + rcu_barrier(); + + kmem_cache_destroy(cfs_inode_cachep); +} + +module_init(init_cfs); +module_exit(exit_cfs); --=20 2.39.0