[U-Boot] [PATCH v1 0/3] Add support for symlink creation in EXT4

This series adds support for the creation of symbolic links on ext4 file-systems. The motivation behind this work is to have the ability to "do" the job of update-alternatives in u-boot. Firmware on TI's platform are usually managed with update-alternatives and are thus targeted by a symbolic link. In some situations we need the ability to select an alternate firmware before the linux kernel is started so that when a early driver needing the firmware comes up, it can be fed the firmware of our choice.
Tested on a am57xx_evm, using a EXT4 partition on external SDcard. The filesystem can be checked later with: fsck.ext4 -f <dev>
usage example: => ln mmc 0:2 zImage /boot/the_linux_kernel
Jean-Jacques Hiblot (3): fs: ext4: constify the buffer passed to write functions fs: ext4: Add support for the creation of symbolic links fs: Add a new command to create symbolic links
cmd/fs.c | 14 ++++++++++++ fs/ext4/ext4_common.c | 6 ++--- fs/ext4/ext4_common.h | 2 +- fs/ext4/ext4_write.c | 51 ++++++++++++++++++++++++++++++++----------- fs/fs.c | 44 +++++++++++++++++++++++++++++++++++++ include/ext4fs.h | 5 +++-- include/fs.h | 2 ++ 7 files changed, 105 insertions(+), 19 deletions(-)

There is no need to modify the buffer passed to ext4fs_write_file(). The memset() has no utility there, and comes probably from the equivalent ext4fs_read_file() function.
Signed-off-by: Jean-Jacques Hiblot jjhiblot@ti.com ---
fs/ext4/ext4_common.c | 2 +- fs/ext4/ext4_common.h | 2 +- fs/ext4/ext4_write.c | 11 +++++------ include/ext4fs.h | 2 +- 4 files changed, 8 insertions(+), 9 deletions(-)
diff --git a/fs/ext4/ext4_common.c b/fs/ext4/ext4_common.c index 67e2471bd3..b881482c39 100644 --- a/fs/ext4/ext4_common.c +++ b/fs/ext4/ext4_common.c @@ -190,7 +190,7 @@ uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n) return res; }
-void put_ext4(uint64_t off, void *buf, uint32_t size) +void put_ext4(u64 off, const void *buf, u32 size) { uint64_t startblock; uint64_t remainder; diff --git a/fs/ext4/ext4_common.h b/fs/ext4/ext4_common.h index 1ee81ab7ce..50155eccfc 100644 --- a/fs/ext4/ext4_common.h +++ b/fs/ext4/ext4_common.h @@ -72,7 +72,7 @@ int ext4fs_iget(int inode_no, struct ext2_inode *inode); void ext4fs_allocate_blocks(struct ext2_inode *file_inode, unsigned int total_remaining_blocks, unsigned int *total_no_of_block); -void put_ext4(uint64_t off, void *buf, uint32_t size); +void put_ext4(u64 off, const void *buf, u32 size); struct ext2_block_group *ext4fs_get_group_descriptor (const struct ext_filesystem *fs, uint32_t bg_idx); uint64_t ext4fs_bg_get_block_id(const struct ext2_block_group *bg, diff --git a/fs/ext4/ext4_write.c b/fs/ext4/ext4_write.c index a7f543f7df..b5b9184f56 100644 --- a/fs/ext4/ext4_write.c +++ b/fs/ext4/ext4_write.c @@ -752,7 +752,7 @@ void ext4fs_deinit(void) * contigous sectors as ext4fs_read_file */ static int ext4fs_write_file(struct ext2_inode *file_inode, - int pos, unsigned int len, char *buf) + int pos, unsigned int len, const char *buf) { int i; int blockcnt; @@ -764,7 +764,7 @@ static int ext4fs_write_file(struct ext2_inode *file_inode, int delayed_start = 0; int delayed_extent = 0; int delayed_next = 0; - char *delayed_buf = NULL; + const char *delayed_buf = NULL;
/* Adjust len so it we can't read past the end of the file. */ if (len > filesize) @@ -816,7 +816,6 @@ static int ext4fs_write_file(struct ext2_inode *file_inode, (uint32_t) delayed_extent); previous_block_number = -1; } - memset(buf, 0, fs->blksz - skipfirst); } buf += fs->blksz - skipfirst; } @@ -830,8 +829,8 @@ static int ext4fs_write_file(struct ext2_inode *file_inode, return len; }
-int ext4fs_write(const char *fname, unsigned char *buffer, - unsigned long sizebytes) +int ext4fs_write(const char *fname, const char *buffer, + unsigned long sizebytes) { int ret = 0; struct ext2_inode *file_inode = NULL; @@ -943,7 +942,7 @@ int ext4fs_write(const char *fname, unsigned char *buffer, if (ext4fs_put_metadata(temp_ptr, itable_blkno)) goto fail; /* copy the file content into data blocks */ - if (ext4fs_write_file(file_inode, 0, sizebytes, (char *)buffer) == -1) { + if (ext4fs_write_file(file_inode, 0, sizebytes, buffer) == -1) { printf("Error in copying content\n"); /* FIXME: Deallocate data blocks */ goto fail; diff --git a/include/ext4fs.h b/include/ext4fs.h index bb55639107..9adffde547 100644 --- a/include/ext4fs.h +++ b/include/ext4fs.h @@ -127,7 +127,7 @@ extern int gindex; int ext4fs_init(void); void ext4fs_deinit(void); int ext4fs_filename_unlink(char *filename); -int ext4fs_write(const char *fname, unsigned char *buffer, +int ext4fs_write(const char *fname, const char *buffer, unsigned long sizebytes); int ext4_write_file(const char *filename, void *buf, loff_t offset, loff_t len, loff_t *actwrite);

Re-use the functions used to write/create a file, to support creation of a symbolic link. The difference with a regular file are small: - The inode mode is flagged with S_IFLNK instead of S_IFREG - The ext2_dirent's filetype is FILETYPE_SYMLINK instead of FILETYPE_REG - Instead of storing the content of a file in allocated blocks, the path to the target is stored. And if the target's path is short enough, no block is allocated and the target's path is stored in ext2_inode.b.symlink
As with regulars files, if a file/symlink with the same name exits, it is unlinked first and then re-created.
Signed-off-by: Jean-Jacques Hiblot jjhiblot@ti.com ---
fs/ext4/ext4_common.c | 4 ++-- fs/ext4/ext4_write.c | 42 ++++++++++++++++++++++++++++++++++-------- include/ext4fs.h | 3 ++- 3 files changed, 38 insertions(+), 11 deletions(-)
diff --git a/fs/ext4/ext4_common.c b/fs/ext4/ext4_common.c index b881482c39..15db191cea 100644 --- a/fs/ext4/ext4_common.c +++ b/fs/ext4/ext4_common.c @@ -607,7 +607,7 @@ restart_read: dir->direntlen = cpu_to_le16(fs->blksz - totalbytes);
dir->namelen = strlen(filename); - dir->filetype = FILETYPE_REG; /* regular file */ + dir->filetype = file_type; temp_dir = (char *)dir; temp_dir = temp_dir + sizeof(struct ext2_dirent); memcpy(temp_dir, filename, strlen(filename)); @@ -944,7 +944,7 @@ int ext4fs_filename_unlink(char *filename) /* read the block no allocated to a file */ for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) { blknr = read_allocated_block(g_parent_inode, blk_idx); - if (blknr <= 0) + if (blknr < 0) break; inodeno = unlink_filename(filename, blknr); if (inodeno != -1) diff --git a/fs/ext4/ext4_write.c b/fs/ext4/ext4_write.c index b5b9184f56..3b2b617d51 100644 --- a/fs/ext4/ext4_write.c +++ b/fs/ext4/ext4_write.c @@ -830,7 +830,7 @@ static int ext4fs_write_file(struct ext2_inode *file_inode, }
int ext4fs_write(const char *fname, const char *buffer, - unsigned long sizebytes) + unsigned long sizebytes, int type) { int ret = 0; struct ext2_inode *file_inode = NULL; @@ -853,8 +853,12 @@ int ext4fs_write(const char *fname, const char *buffer, struct ext2_block_group *bgd = NULL; struct ext_filesystem *fs = get_fs(); ALLOC_CACHE_ALIGN_BUFFER(char, filename, 256); + bool store_link_in_inode = false; memset(filename, 0x00, 256);
+ if (type != FILETYPE_REG && type != FILETYPE_SYMLINK) + return -1; + g_parent_inode = zalloc(fs->inodesz); if (!g_parent_inode) goto fail; @@ -886,8 +890,16 @@ int ext4fs_write(const char *fname, const char *buffer, if (ret) goto fail; } - /* calucalate how many blocks required */ - bytes_reqd_for_file = sizebytes; + + /* calculate how many blocks required */ + if (type == FILETYPE_SYMLINK && + sizebytes <= sizeof(file_inode->b.symlink)) { + store_link_in_inode = true; + bytes_reqd_for_file = 0; + } else { + bytes_reqd_for_file = sizebytes; + } + blks_reqd_for_file = lldiv(bytes_reqd_for_file, fs->blksz); if (do_div(bytes_reqd_for_file, fs->blksz) != 0) { blks_reqd_for_file++; @@ -900,7 +912,7 @@ int ext4fs_write(const char *fname, const char *buffer, goto fail; }
- inodeno = ext4fs_update_parent_dentry(filename, FILETYPE_REG); + inodeno = ext4fs_update_parent_dentry(filename, type); if (inodeno == -1) goto fail; /* prepare file inode */ @@ -908,14 +920,23 @@ int ext4fs_write(const char *fname, const char *buffer, if (!inode_buffer) goto fail; file_inode = (struct ext2_inode *)inode_buffer; - file_inode->mode = cpu_to_le16(S_IFREG | S_IRWXU | - S_IRGRP | S_IROTH | S_IXGRP | S_IXOTH); + file_inode->size = cpu_to_le32(sizebytes); + if (type == FILETYPE_SYMLINK) { + file_inode->mode = cpu_to_le16(S_IFLNK | S_IRWXU | S_IRWXG | + S_IRWXO); + if (store_link_in_inode) { + strncpy(file_inode->b.symlink, buffer, sizebytes); + sizebytes = 0; + } + } else { + file_inode->mode = cpu_to_le16(S_IFREG | S_IRWXU | S_IRGRP | + S_IROTH | S_IXGRP | S_IXOTH); + } /* ToDo: Update correct time */ file_inode->mtime = cpu_to_le32(timestamp); file_inode->atime = cpu_to_le32(timestamp); file_inode->ctime = cpu_to_le32(timestamp); file_inode->nlinks = cpu_to_le16(1); - file_inode->size = cpu_to_le32(sizebytes);
/* Allocate data blocks */ ext4fs_allocate_blocks(file_inode, blocks_remaining, @@ -1007,7 +1028,7 @@ int ext4_write_file(const char *filename, void *buf, loff_t offset, return -1; }
- ret = ext4fs_write(filename, buf, len); + ret = ext4fs_write(filename, buf, len, FILETYPE_REG); if (ret) { printf("** Error ext4fs_write() **\n"); goto fail; @@ -1022,3 +1043,8 @@ fail:
return -1; } + +int ext4fs_create_link(const char *target, const char *fname) +{ + return ext4fs_write(fname, target, strlen(target), FILETYPE_SYMLINK); +} diff --git a/include/ext4fs.h b/include/ext4fs.h index 9adffde547..8938169b7b 100644 --- a/include/ext4fs.h +++ b/include/ext4fs.h @@ -128,9 +128,10 @@ int ext4fs_init(void); void ext4fs_deinit(void); int ext4fs_filename_unlink(char *filename); int ext4fs_write(const char *fname, const char *buffer, - unsigned long sizebytes); + unsigned long sizebytes, int type); int ext4_write_file(const char *filename, void *buf, loff_t offset, loff_t len, loff_t *actwrite); +int ext4fs_create_link(const char *target, const char *fname); #endif
struct ext_filesystem *get_fs(void);

The command line is: ln <interface> <dev[:part]> target linkname
Currently symbolic links are supported only in ext4 and only if the option CMD_EXT4_WRITE is enabled.
Signed-off-by: Jean-Jacques Hiblot jjhiblot@ti.com
---
cmd/fs.c | 14 ++++++++++++++ fs/fs.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ include/fs.h | 2 ++ 3 files changed, 60 insertions(+)
diff --git a/cmd/fs.c b/cmd/fs.c index 8064a1c84d..ba0e08cd12 100644 --- a/cmd/fs.c +++ b/cmd/fs.c @@ -74,6 +74,20 @@ U_BOOT_CMD( " device type 'interface' instance 'dev'." )
+static int do_ln_wrapper(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[]) +{ + return do_ln(cmdtp, flag, argc, argv, FS_TYPE_ANY); +} + +U_BOOT_CMD( + ln, 5, 1, do_ln_wrapper, + "Create a symbolic link", + "<interface> <dev[:part]> target linkname\n" + " - create a symbolic link to 'target' with the name 'linkname' on\n" + " device type 'interface' instance 'dev'." +) + static int do_fstype_wrapper(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { diff --git a/fs/fs.c b/fs/fs.c index 7fd22101ef..953a38dadc 100644 --- a/fs/fs.c +++ b/fs/fs.c @@ -90,6 +90,11 @@ static inline int fs_write_unsupported(const char *filename, void *buf, return -1; }
+static inline int fs_ln_unsupported(const char *filename, const char *target) +{ + return -1; +} + static inline void fs_close_unsupported(void) { } @@ -154,6 +159,7 @@ struct fstype_info { void (*closedir)(struct fs_dir_stream *dirs); int (*unlink)(const char *filename); int (*mkdir)(const char *dirname); + int (*ln)(const char *filename, const char *target); };
static struct fstype_info fstypes[] = { @@ -181,6 +187,7 @@ static struct fstype_info fstypes[] = { .opendir = fat_opendir, .readdir = fat_readdir, .closedir = fat_closedir, + .ln = fs_ln_unsupported, }, #endif #ifdef CONFIG_FS_EXT4 @@ -196,8 +203,10 @@ static struct fstype_info fstypes[] = { .read = ext4_read_file, #ifdef CONFIG_CMD_EXT4_WRITE .write = ext4_write_file, + .ln = ext4fs_create_link, #else .write = fs_write_unsupported, + .ln = fs_ln_unsupported, #endif .uuid = ext4fs_uuid, .opendir = fs_opendir_unsupported, @@ -221,6 +230,7 @@ static struct fstype_info fstypes[] = { .opendir = fs_opendir_unsupported, .unlink = fs_unlink_unsupported, .mkdir = fs_mkdir_unsupported, + .ln = fs_ln_unsupported, }, #endif #ifdef CONFIG_CMD_UBIFS @@ -239,6 +249,7 @@ static struct fstype_info fstypes[] = { .opendir = fs_opendir_unsupported, .unlink = fs_unlink_unsupported, .mkdir = fs_mkdir_unsupported, + .ln = fs_ln_unsupported, }, #endif #ifdef CONFIG_FS_BTRFS @@ -257,6 +268,7 @@ static struct fstype_info fstypes[] = { .opendir = fs_opendir_unsupported, .unlink = fs_unlink_unsupported, .mkdir = fs_mkdir_unsupported, + .ln = fs_ln_unsupported, }, #endif { @@ -274,6 +286,7 @@ static struct fstype_info fstypes[] = { .opendir = fs_opendir_unsupported, .unlink = fs_unlink_unsupported, .mkdir = fs_mkdir_unsupported, + .ln = fs_ln_unsupported, }, };
@@ -602,6 +615,22 @@ int fs_mkdir(const char *dirname) return ret; }
+int fs_ln(const char *fname, const char *target) +{ + struct fstype_info *info = fs_get_info(fs_type); + int ret; + + ret = info->ln(fname, target); + + if (ret < 0) { + printf("** Unable to create link %s -> %s **\n", fname, target); + ret = -1; + } + fs_close(); + + return ret; +} + int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], int fstype) { @@ -838,3 +867,18 @@ int do_mkdir(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
return 0; } + +int do_ln(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], + int fstype) +{ + if (argc != 5) + return CMD_RET_USAGE; + + if (fs_set_blk_dev(argv[1], argv[2], fstype)) + return 1; + + if (fs_ln(argv[3], argv[4])) + return 1; + + return 0; +} diff --git a/include/fs.h b/include/fs.h index aa3604db8d..6854597700 100644 --- a/include/fs.h +++ b/include/fs.h @@ -191,6 +191,8 @@ int do_rm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], int fstype); int do_mkdir(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], int fstype); +int do_ln(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], + int fstype);
/* * Determine the UUID of the specified filesystem and print it. Optionally it is

On Tue, 29 Jan 2019 14:40:08 +0100 Jean-Jacques Hiblot jjhiblot@ti.com wrote:
This series adds support for the creation of symbolic links on ext4 file-systems. The motivation behind this work is to have the ability to "do" the job of update-alternatives in u-boot. Firmware on TI's platform are usually managed with update-alternatives and are thus targeted by a symbolic link. In some situations we need the ability to select an alternate firmware before the linux kernel is started so that when a early driver needing the firmware comes up, it can be fed the firmware of our choice.
Tested on a am57xx_evm, using a EXT4 partition on external SDcard. The filesystem can be checked later with: fsck.ext4 -f <dev>
usage example: => ln mmc 0:2 zImage /boot/the_linux_kernel
Could you also add a test for symlink to sandbox? This is a high level code (FS ext4), so it can be nice tested there.
Jean-Jacques Hiblot (3): fs: ext4: constify the buffer passed to write functions fs: ext4: Add support for the creation of symbolic links fs: Add a new command to create symbolic links
cmd/fs.c | 14 ++++++++++++ fs/ext4/ext4_common.c | 6 ++--- fs/ext4/ext4_common.h | 2 +- fs/ext4/ext4_write.c | 51 ++++++++++++++++++++++++++++++++----------- fs/fs.c | 44 +++++++++++++++++++++++++++++++++++++ include/ext4fs.h | 5 +++-- include/fs.h | 2 ++ 7 files changed, 105 insertions(+), 19 deletions(-)
Best regards,
Lukasz Majewski
--
DENX Software Engineering GmbH, Managing Director: Wolfgang Denk HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email: lukma@denx.de

On Wed, Jan 30, 2019 at 12:24:29AM +0100, Lukasz Majewski wrote:
On Tue, 29 Jan 2019 14:40:08 +0100 Jean-Jacques Hiblot jjhiblot@ti.com wrote:
This series adds support for the creation of symbolic links on ext4 file-systems. The motivation behind this work is to have the ability to "do" the job of update-alternatives in u-boot. Firmware on TI's platform are usually managed with update-alternatives and are thus targeted by a symbolic link. In some situations we need the ability to select an alternate firmware before the linux kernel is started so that when a early driver needing the firmware comes up, it can be fed the firmware of our choice.
Tested on a am57xx_evm, using a EXT4 partition on external SDcard. The filesystem can be checked later with: fsck.ext4 -f <dev>
usage example: => ln mmc 0:2 zImage /boot/the_linux_kernel
Could you also add a test for symlink to sandbox? This is a high level code (FS ext4), so it can be nice tested there.
Jean-Jacques Hiblot (3): fs: ext4: constify the buffer passed to write functions fs: ext4: Add support for the creation of symbolic links fs: Add a new command to create symbolic links
cmd/fs.c | 14 ++++++++++++ fs/ext4/ext4_common.c | 6 ++--- fs/ext4/ext4_common.h | 2 +- fs/ext4/ext4_write.c | 51 ++++++++++++++++++++++++++++++++----------- fs/fs.c | 44 +++++++++++++++++++++++++++++++++++++ include/ext4fs.h | 5 +++-- include/fs.h | 2 ++ 7 files changed, 105 insertions(+), 19 deletions(-)
Yes, now that we have tests under pytest, we need to drop something for this under test/py/tests/test_fs/ and my first thought is in a new test_symlink.py file so that someday when we get more than just FAT/EXTn tests the next FS that supports symlinks gets the tests for free if/when it supports symlinks.
participants (3)
-
Jean-Jacques Hiblot
-
Lukasz Majewski
-
Tom Rini