linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] fat: ignore .. subdir and always add a link to dirs
@ 2024-02-22 20:30 Thadeu Lima de Souza Cascardo
  2024-02-23  1:52 ` OGAWA Hirofumi
  0 siblings, 1 reply; 23+ messages in thread
From: Thadeu Lima de Souza Cascardo @ 2024-02-22 20:30 UTC (permalink / raw)
  To: OGAWA Hirofumi
  Cc: linux-kernel, linux-fsdevel, Gwendal Grignou, dlunev,
	Thadeu Lima de Souza Cascardo

The tools used for creating images for the Lego Mindstrom EV3 are not
adding '.' and '..' entry in the 'Projects' directory.

Without this fix, the kernel can not fill the inode structure for
'Projects' directory.

See https://github.com/microsoft/pxt-ev3/issues/980
And https://github.com/microsoft/uf2-linux/issues/6

When counting the number of subdirs, ignore .. subdir and add one when
setting the initial link count for directories. This way, when .. is
present, it is still accounted for, and when neither . or .. are present, a
single link is still done, as it should, since this link would be the one
from the parent directory.

With this fix applied, we can mount an image with such empty directories,
access them, create subdirectories and remove them.

Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@igalia.com>
Cc: Gwendal Grignou <gwendal@chromium.org>
Link: https://lore.kernel.org/all/20220204062232.3410036-1-gwendal@chromium.org/
Cc: dlunev@chromium.org
---
 fs/fat/dir.c   |  3 ++-
 fs/fat/inode.c | 12 +++++++++---
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/fs/fat/dir.c b/fs/fat/dir.c
index 00235b8a1823..fcdb652efc53 100644
--- a/fs/fat/dir.c
+++ b/fs/fat/dir.c
@@ -937,7 +937,8 @@ int fat_subdirs(struct inode *dir)
 	bh = NULL;
 	cpos = 0;
 	while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
-		if (de->attr & ATTR_DIR)
+		if (de->attr & ATTR_DIR &&
+		    strncmp(de->name, MSDOS_DOTDOT, MSDOS_NAME))
 			count++;
 	}
 	brelse(bh);
diff --git a/fs/fat/inode.c b/fs/fat/inode.c
index 1fac3dabf130..9a3bd38a4494 100644
--- a/fs/fat/inode.c
+++ b/fs/fat/inode.c
@@ -494,8 +494,14 @@ static int fat_validate_dir(struct inode *dir)
 {
 	struct super_block *sb = dir->i_sb;
 
-	if (dir->i_nlink < 2) {
-		/* Directory should have "."/".." entries at least. */
+	if (dir->i_nlink < 1) {
+		/*
+		 * Though it is expected that directories have at least
+		 * "."/".." entries, there are filesystems in the field that
+		 * don't have either. Even in those cases, at least one link
+		 * is necessary, as otherwise, when trying to increment it,
+		 * VFS would BUG.
+		 */
 		fat_fs_error(sb, "corrupted directory (invalid entries)");
 		return -EIO;
 	}
@@ -534,7 +540,7 @@ int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)
 			return error;
 		MSDOS_I(inode)->mmu_private = inode->i_size;
 
-		set_nlink(inode, fat_subdirs(inode));
+		set_nlink(inode, fat_subdirs(inode) + 1);
 
 		error = fat_validate_dir(inode);
 		if (error < 0)
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2024-03-13 12:43 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-02-22 20:30 [PATCH] fat: ignore .. subdir and always add a link to dirs Thadeu Lima de Souza Cascardo
2024-02-23  1:52 ` OGAWA Hirofumi
2024-02-23  2:02   ` Thadeu Lima de Souza Cascardo
2024-02-23  8:27     ` OGAWA Hirofumi
2024-02-23  8:32       ` OGAWA Hirofumi
2024-02-23  9:58         ` Thadeu Lima de Souza Cascardo
2024-02-23 12:29           ` OGAWA Hirofumi
2024-02-23 13:16             ` Thadeu Lima de Souza Cascardo
2024-02-23 13:33             ` Thadeu Lima de Souza Cascardo
2024-02-28  1:42             ` Thadeu Lima de Souza Cascardo
2024-02-28  3:38               ` OGAWA Hirofumi
2024-02-28  9:10                 ` Thadeu Lima de Souza Cascardo
2024-03-04 23:37                   ` Thadeu Lima de Souza Cascardo
2024-03-05  4:14                     ` OGAWA Hirofumi
2024-03-10  5:52                   ` OGAWA Hirofumi
2024-03-10 10:14                     ` Thadeu Lima de Souza Cascardo
2024-03-10 14:59                       ` OGAWA Hirofumi
2024-03-13  7:58                         ` Thadeu Lima de Souza Cascardo
2024-03-13  8:05                           ` OGAWA Hirofumi
2024-03-13  8:41                             ` Thadeu Lima de Souza Cascardo
2024-03-13 11:06                               ` OGAWA Hirofumi
2024-03-13 11:16                                 ` Thadeu Lima de Souza Cascardo
2024-03-13 12:43                                   ` OGAWA Hirofumi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).