From patchwork Fri Apr 3 14:04:54 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 2114 Return-Path: X-Original-To: u-boot-concept@u-boot.org Delivered-To: u-boot-concept@u-boot.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1775225208; bh=laXwvaAsOe/upSvm8kAM2FnUNx05LwnNI6AWeh47NjY=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=b1NaUCg28LPQfsTQ7QOtWb9N83iZbCIWw58KeIHuxWV2Cwv24Gpl9pESDXhDFQEGB JUWx2FIEDvTxnCQ4apkuGPKXEo6dk5oADetR8kB+rnEskRhoczzK66SN3IgC4iVW/+ OpzdpaGJwZhoq2xifteIBOhOoKcxJSYb7KOwGMY3o+1Rs7p+H4EYB1Tzc5tuZ7cEUH yhj/BZ44kZxqqBXgKLoHUjQFLW0WkrH+2neL4r8PEMetf0MZYlsa6v+S+PkzSHkqLZ cz+7Gl44Oz2Gh9IF/l9Ye7isMaiPPc4oXFSwMmPyiL9G8Hlq1VmwJFTWxsOq2xlmVj ua4VV/MZAPhww== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 910DE67EE6 for ; Fri, 3 Apr 2026 08:06:48 -0600 (MDT) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10024) with ESMTP id yYW2PSqfaOIw for ; Fri, 3 Apr 2026 08:06:48 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1775225206; bh=laXwvaAsOe/upSvm8kAM2FnUNx05LwnNI6AWeh47NjY=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=VpexVgl1FZBnxUFuZFLjDJgW+Pj78t1GElXwPXsAdv/6oQQfivczHJrGVUbQxDdMY TR+ugT/IrQWqUGmf9uEcbcu47ETK332k+osZgW/Lst0Ua1ti0Pfmue5dgr88aIkVPY ckNK/DRXDJ0aQvUyf+b4uF0JN19f+aXOPb7UlSuMPoDVXaMqIjfEjr38QmgHb2eLrt CPWCF8nYjZit9oVSkfUzWEwTkvx0MNNSu7yJXrj0AIcHZhjgfeyDfih9MCaQalpkQW HHfhPifZ+YGfwgPoUTLcJwXWanosXE6RL8vjKY7m9kEog86tC6q0ph5+F3DqrMv9Zl CBVpM85Ix3U5g== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 5E19E6A364 for ; Fri, 3 Apr 2026 08:06:46 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1775225201; bh=LJ1Bk1z+aWcAvpoYL4QuD7LwpeR/0l2nWsiJnDTJCQg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Enad5e6l24w1i7pCLl10QXT1z7bCzhMoiWwyxWVaC9rqvC5DMnmInTex+HC4fd+l0 lmON/hGSdZdxKKzYn+dYcMqFSOm3FJ4bz7ySNJFGGqdeRe3BAKI5c56YcPCAPJZuL0 h/JkHcNWEnlZjWBa0c54Dkw2q7Sj0Xpzn8jekDAqBcxm3J275gIRRgpIDSWqgCjygm F3+gOsQtQmses2ZihYoDRwJq8jeCwWwCuDYhnpQjYm0iL6N3pjOhuKyK45WOvbGPhN TovhOXmeCAIBU1qc4nNvedezfsME0ozVWAK8LK/B8qYiyb4t+6XI6Bru8azxp4FV6V 3/xbRNjf/TBJg== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id AAFAD67EE6; Fri, 3 Apr 2026 08:06:41 -0600 (MDT) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10026) with ESMTP id Kp7FxqVAHIAn; Fri, 3 Apr 2026 08:06:41 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1775225201; bh=eBr/QNvapV4Qap9Lfhg0k5UBBLuT2hkx9GFjIto4C7I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FcFzJ8LFj78elzBUfUJ1JzMWwxmBQM1hUAvPybAOlp3Bh0PDdTT8HFpwXjfQbLyrz 8AMeJ5Ykh5t3msRSUR2lNKMyyXxaqc/Qt9Gt0MhFZLNdASdVeMctTDdeuAXRPDRE2/ kb18WeI/U6dzIPEA33qZI3oonp7u9NdXsm9N9Anv8NdX7RH2cNe/M/n8hJv8/CkGGM kbWa1nGagLSx7EQiJqw3L2Q6020tBmNlTw8cAekHUwPgrWNwmBsgZI5hOiSrKpbq1t nOmS/nZLCRbcQQkqmf118Fwi4DarSlrysJUkgDLYi/xgnzvgjpFp+tTDFUEKlB5Pp8 KUg1z5F7zjoRw== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 2027C6A34F; Fri, 3 Apr 2026 08:06:41 -0600 (MDT) From: Simon Glass To: U-Boot Concept Date: Fri, 3 Apr 2026 08:04:54 -0600 Message-ID: <20260403140523.1998228-33-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260403140523.1998228-1-sjg@u-boot.org> References: <20260403140523.1998228-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: BLD5NTARKFLCXGMNBBP3LBSNNVHUKGTM X-Message-ID-Hash: BLD5NTARKFLCXGMNBBP3LBSNNVHUKGTM X-MailFrom: sjg@u-boot.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Simon Glass X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 32/34] fs: Add ext4 VFS driver List-Id: Discussion and patches related to U-Boot Concept Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Simon Glass Add a UCLASS_FS driver for the old ext4 filesystem implementation, allowing it to be mounted through the VFS layer. The driver wraps the existing ext4fs functions for mount, unmount, directory listing, file read/write, unlink and symlink creation. Write operations are guarded by CONFIG_EXT4_WRITE. This complements the ext4l VFS driver, enabling VFS on boards that use the original ext4 implementation. Signed-off-by: Simon Glass --- fs/ext4/Makefile | 1 + fs/ext4/fs.c | 264 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 265 insertions(+) create mode 100644 fs/ext4/fs.c diff --git a/fs/ext4/Makefile b/fs/ext4/Makefile index 6ae44a2d0a3..f79baccc6a7 100644 --- a/fs/ext4/Makefile +++ b/fs/ext4/Makefile @@ -9,3 +9,4 @@ obj-y := ext4fs.o ext4_common.o dev.o obj-$(CONFIG_EXT4_WRITE) += ext4_write.o ext4_journal.o +obj-$(CONFIG_$(PHASE_)VFS) += fs.o diff --git a/fs/ext4/fs.c b/fs/ext4/fs.c new file mode 100644 index 00000000000..09cbddee50f --- /dev/null +++ b/fs/ext4/fs.c @@ -0,0 +1,264 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * ext4 filesystem driver for the VFS layer + * + * Wraps the existing ext4 implementation to provide UCLASS_FS and + * UCLASS_DIR devices, following the same pattern as the FAT VFS driver. + * + * Copyright 2026 Simon Glass + */ + +#define LOG_CATEGORY UCLASS_FS + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/** + * struct ext4_dir_priv - Private info for ext4 directory devices + * + * @strm: Directory stream from ext4fs_opendir(), or NULL + */ +struct ext4_dir_priv { + struct fs_dir_stream *strm; +}; + +static int ext4_vfs_mount(struct udevice *dev) +{ + struct fs_priv *uc_priv = dev_get_uclass_priv(dev); + struct fs_plat *plat = dev_get_uclass_plat(dev); + int ret; + + if (uc_priv->mounted) + return log_msg_ret("emm", -EISCONN); + + if (!plat->desc) + return log_msg_ret("emd", -ENODEV); + + ret = ext4fs_probe(plat->desc, &plat->part); + if (ret) + return log_msg_ret("emp", ret); + + uc_priv->mounted = true; + + return 0; +} + +static int ext4_vfs_unmount(struct udevice *dev) +{ + struct fs_priv *uc_priv = dev_get_uclass_priv(dev); + + if (!uc_priv->mounted) + return log_msg_ret("euu", -ENOTCONN); + + ext4fs_close(); + uc_priv->mounted = false; + + return 0; +} + +static int ext4_vfs_lookup_dir(struct udevice *dev, const char *path, + struct udevice **dirp) +{ + struct udevice *dir; + int ret; + + ret = dir_add_probe(dev, DM_DRIVER_GET(ext4_vfs_dir), path, &dir); + if (ret) + return log_msg_ret("eld", ret); + + *dirp = dir; + + return 0; +} + +#if IS_ENABLED(CONFIG_EXT4_WRITE) +static int ext4_vfs_unlink(struct udevice *dev, const char *path) +{ + return ext4fs_filename_unlink((char *)path); +} + +static int ext4_vfs_ln(struct udevice *dev, const char *path, + const char *target) +{ + return ext4fs_create_link(target, path); +} +#endif + +static const struct fs_ops ext4_vfs_ops = { + .mount = ext4_vfs_mount, + .unmount = ext4_vfs_unmount, + .lookup_dir = ext4_vfs_lookup_dir, +#if IS_ENABLED(CONFIG_EXT4_WRITE) + .unlink = ext4_vfs_unlink, + .ln = ext4_vfs_ln, +#endif +}; + +U_BOOT_DRIVER(ext4_old_fs) = { + .name = "ext4_old_fs", + .id = UCLASS_FS, + .ops = &ext4_vfs_ops, +}; + +/* ext4 directory driver */ + +static int ext4_dir_open(struct udevice *dev, struct fs_dir_stream *strm) +{ + struct ext4_dir_priv *priv = dev_get_priv(dev); + struct dir_uc_priv *uc_priv = dev_get_uclass_priv(dev); + struct fs_dir_stream *ext4_strm; + const char *path; + int ret; + + path = *uc_priv->path ? uc_priv->path : "/"; + ret = ext4fs_opendir(path, &ext4_strm); + if (ret) + return log_msg_ret("edo", ret); + + priv->strm = ext4_strm; + + return 0; +} + +static int ext4_dir_read(struct udevice *dev, struct fs_dir_stream *strm, + struct fs_dirent *dent) +{ + struct ext4_dir_priv *priv = dev_get_priv(dev); + struct fs_dirent *ext4_dent; + int ret; + + ret = ext4fs_readdir(priv->strm, &ext4_dent); + if (ret) + return ret; + + *dent = *ext4_dent; + + return 0; +} + +static int ext4_dir_close(struct udevice *dev, struct fs_dir_stream *strm) +{ + struct ext4_dir_priv *priv = dev_get_priv(dev); + + ext4fs_closedir(priv->strm); + priv->strm = NULL; + + return 0; +} + +/* ext4 file driver */ + +/** + * struct ext4_file_priv - Private info for ext4 file devices + * + * @path: Full path within the ext4 filesystem + */ +struct ext4_file_priv { + char path[FILE_MAX_PATH_LEN]; +}; + +static ssize_t ext4_read_iter(struct udevice *dev, struct iov_iter *iter, + loff_t pos) +{ + struct ext4_file_priv *priv = dev_get_priv(dev); + loff_t actual; + int ret; + + ret = ext4_read_file(priv->path, iter_iov_ptr(iter), pos, + iter_iov_avail(iter), &actual); + if (ret) + return log_msg_ret("efr", ret); + iter_advance(iter, actual); + + return actual; +} + +#if IS_ENABLED(CONFIG_EXT4_WRITE) +static ssize_t ext4_write_iter(struct udevice *dev, struct iov_iter *iter, + loff_t pos) +{ + struct ext4_file_priv *priv = dev_get_priv(dev); + loff_t actual; + int ret; + + ret = ext4_write_file(priv->path, (void *)iter_iov_ptr(iter), pos, + iter_iov_avail(iter), &actual); + if (ret) + return log_msg_ret("efw", ret); + iter_advance(iter, actual); + + return actual; +} +#endif + +static struct file_ops ext4_file_ops = { + .read_iter = ext4_read_iter, +#if IS_ENABLED(CONFIG_EXT4_WRITE) + .write_iter = ext4_write_iter, +#endif +}; + +U_BOOT_DRIVER(ext4_vfs_file) = { + .name = "ext4_vfs_file", + .id = UCLASS_FILE, + .ops = &ext4_file_ops, + .priv_auto = sizeof(struct ext4_file_priv), +}; + +static int ext4_dir_open_file(struct udevice *dir, const char *leaf, + enum dir_open_flags_t oflags, + struct udevice **filp) +{ + struct dir_uc_priv *uc_priv = dev_get_uclass_priv(dir); + struct ext4_file_priv *priv; + struct udevice *dev; + char path[FILE_MAX_PATH_LEN]; + loff_t size = 0; + int ret; + + if (*uc_priv->path) + snprintf(path, sizeof(path), "%s/%s", uc_priv->path, leaf); + else + snprintf(path, sizeof(path), "/%s", leaf); + + if (oflags == DIR_O_RDONLY) { + if (!ext4fs_exists(path)) + return log_msg_ret("eoe", -ENOENT); + ret = ext4fs_size(path, &size); + if (ret) + return log_msg_ret("eos", ret); + } + + ret = file_add_probe(dir, DM_DRIVER_REF(ext4_vfs_file), leaf, + size, oflags, &dev); + if (ret) + return log_msg_ret("eop", ret); + + priv = dev_get_priv(dev); + strlcpy(priv->path, path, sizeof(priv->path)); + *filp = dev; + + return 0; +} + +static struct dir_ops ext4_dir_ops = { + .open = ext4_dir_open, + .read = ext4_dir_read, + .close = ext4_dir_close, + .open_file = ext4_dir_open_file, +}; + +U_BOOT_DRIVER(ext4_vfs_dir) = { + .name = "ext4_vfs_dir", + .id = UCLASS_DIR, + .ops = &ext4_dir_ops, + .priv_auto = sizeof(struct ext4_dir_priv), +};