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 . and .. subdirs and add two when setting the initial link count for directories. This way, the number of links is always correctly accounted for. With this fix applied, we can mount an image with such empty directories, access them, create subdirectories and remove them. This also prevents corrupting such filesystems as when the inodes would be put, since no links were accounted for, all of its clusters would be marked as free. Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@xxxxxxxxxx> Cc: Gwendal Grignou <gwendal@xxxxxxxxxxxx> Link: https://lore.kernel.org/all/20220204062232.3410036-1-gwendal@xxxxxxxxxxxx/ Cc: dlunev@xxxxxxxxxxxx --- fs/fat/dir.c | 4 +++- fs/fat/inode.c | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/fs/fat/dir.c b/fs/fat/dir.c index acbec5bdd521..4e4a359a1ea3 100644 --- a/fs/fat/dir.c +++ b/fs/fat/dir.c @@ -949,7 +949,9 @@ 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_DOT , MSDOS_NAME) && + strncmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) count++; } brelse(bh); diff --git a/fs/fat/inode.c b/fs/fat/inode.c index d9e6fbb6f246..234c244d1252 100644 --- a/fs/fat/inode.c +++ b/fs/fat/inode.c @@ -534,7 +534,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)+2); error = fat_validate_dir(inode); if (error < 0) -- 2.34.1