Skip to contentSkip to conversation

Add support for symlink creation in EXT4

PATCH Superseded Jean-Jacques Hiblot 3 participants 6 messages Jan 29, 2019

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover 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(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/3 fs: ext4: constify the buffer passed to write functions

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.

---

 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(-)
fs/ext4/ext4_common.c +1 -1
index 67e2471bd3..b881482c39 100644
@@ -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;
fs/ext4/ext4_common.h +1 -1
index 1ee81ab7ce..50155eccfc 100644
@@ -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,
fs/ext4/ext4_write.c +5 -6
index a7f543f7df..b5b9184f56 100644
@@ -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;
include/ext4fs.h +1 -1
index bb55639107..9adffde547 100644
@@ -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);
Signature · 1 line
2.17.1
#2 lore raw

PATCH 2/3 fs: ext4: Add support for the creation of symbolic links

In reply to Jean-Jacques Hiblot

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.

---

 fs/ext4/ext4_common.c |  4 ++--
 fs/ext4/ext4_write.c  | 42 ++++++++++++++++++++++++++++++++++--------
 include/ext4fs.h      |  3 ++-
 3 files changed, 38 insertions(+), 11 deletions(-)
fs/ext4/ext4_common.c +2 -2
index b881482c39..15db191cea 100644
@@ -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)
fs/ext4/ext4_write.c +34 -8

88 lines not on this page · raw message

include/ext4fs.h +2 -1
index 9adffde547..8938169b7b 100644
@@ -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);
Signature · 1 line
2.17.1
#3 lore raw

PATCH 3/3 fs: Add a new command to create symbolic links

In reply to Jean-Jacques Hiblot

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.

---

 cmd/fs.c     | 14 ++++++++++++++
 fs/fs.c      | 44 ++++++++++++++++++++++++++++++++++++++++++++
 include/fs.h |  2 ++
 3 files changed, 60 insertions(+)
cmd/fs.c +14 -0
index 8064a1c84d..ba0e08cd12 100644
@@ -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[])
 {
fs/fs.c +44 -0

103 lines not on this page · raw message

include/fs.h +2 -0
index aa3604db8d..6854597700 100644
@@ -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
Signature · 1 line
2.17.1
#4 lore raw

cover Add support for symlink creation in EXT4

In reply to Jean-Jacques Hiblot
Quoted 10 lines · On Tue, 29 Jan 2019 14:40:08 +0100 Jean-Jacques Hiblot <[email protected]> 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.

Quoted 5 lines

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.

Quoted 7 lines

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

Quoted 9 lines
 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 at denx.de -------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20190130/d93d02a3/attachment.sig>

#5 lore raw

cover Add support for symlink creation in EXT4

Quoted 13 lines · Lukasz Majewski
Quoted 10 lines · On Tue, 29 Jan 2019 14:40:08 +0100 Jean-Jacques Hiblot <[email protected]> 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.

Quoted 24 lines
Quoted 5 lines

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.

Quoted 15 lines

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.

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20190129/0dba0276/attachment.sig>