f2fs-tools: introduce F2FS_FEATURE_LOST_FOUND feature
v3 cover f2fs-tools: introduce F2FS_FEATURE_LOST_FOUND feature
v3->v2:
* mkfs: remove extra newline, fix return value and incorrect data types
* fsck: get file type according to i_mode instead of F2FS_FT_REG_FILE
v2->v1:
* discard obsolete dnodes after all inodes are created.
* add new error message if file type (i_mode) is totally wrong.
* change "-O lost+found" to "-O lost_found".
* only set filename and bitmap in the second slot of lost+found dentry.
* handle inode_crtime when creating lost+found inode.
* fix endiain issue.
* fix memory leak in fsck_failed_reconnect_file{_dnode|_idnode|_didnode}.
* translate i_mode to FILE_TYPE instead of F2FS_FT_REG_FILE when reconnecting files.
* get_node_info() reads nat block again if nat entry is invalid.
---8<---
Commit 390fe587b1 ("fsck.f2fs: support restore lost files into ./lost_found/") restores unreachable files, which have no parent directory or their parent directories are removed by fsck, to ./lost_found directory.
However, it has several limitations:
1. ./lost_found dir does not locate inside f2fs, thus it may needs another partition to save ./lost_found.
2. encrypted files are not supported to be restored.
3. some xattrs may be lost, it depends on whether dump_node() enables dumping such xattrs.
4. not support restoring files on Android devices (the same reason as
#1).
In order to address the above limitations, the LOST_FOUND feature is introduced. This is an ext4-style lost+found. It tries to restore unreachable files into /lost+found directory inside f2fs.
If LOST_FOUND feature is switched on when mkfs, /lost+found directory is created by mkfs. It can also be created by mkdir manually. It should be located right under root directory.
If fsck detects unreachable nids, it scans all these nids, and skips non- inode or directory inode nodes. For the left nids, they are all file inodes. Fsck then checks these inodes and update corresponding counters and bitmaps.
From now on, the filesystem is in a "consistent" state, fsck can allocate blocks safely and start reconnecting files to lost+found. The reconnection adds new dentry in lost+found and update metadata (i_name, i_pino) of file inode. If reconnection fails, fsck will clear the counters and bitmaps. Files reconnected to lost+found will be renamed after its ino number.
However, because of lost+found directory, f2fs should avoid to encrypting root directory. So f2fs should also check if LOST_FOUND feature is enabled or not.
I create testcases which inject faults to generate unreachable nids. To enable LOST_FOUND, we should give "-O lost_found" to mkfs.
# mkfs.f2fs -O lost_found -O encrypt test.img # tree /data/ /data/ ├── dir │ ├── foo │ └── subdir │ └── bar ├── encrypt-dir │ ├── encrypt-foo │ └── encrypt-subdir │ └── encrypt-bar └── lost+found
5 directories, 4 files
Testcases:
1) corrupt nat_entry->blk_addr of dir [PASS]
2) corrupt nat_entry->blk_addr of encrypt-dir [PASS]
3) remove lost+found, then corrupt nat_entry->blk_addr of dir [PASS]
Any comments and tests are appreciated.
Thanks,
Sheng
Sheng Yong (2):
mkfs.f2fs: create lost+found directory fsck.f2fs: reconnect unreachable files to lost+found
fsck/dir.c | 19 ++- fsck/fsck.c | 389 +++++++++++++++++++++++++++++++++++++++++++++++- fsck/fsck.h | 3 + fsck/mount.c | 5 + include/f2fs_fs.h | 6 + mkfs/f2fs_format.c | 223 +++++++++++++++++++++++++-- mkfs/f2fs_format_main.c | 2 + 7 files changed, 632 insertions(+), 15 deletions(-)
Signature · 10 lines
2.14.1 ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot _______________________________________________ Linux-f2fs-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
RFC PATCH v3 1/2 mkfs.f2fs: create lost+found directory
This patch introduces a new feature F2FS_FEATURE_LOST_FOUND. It can be switched on by indicating `-O lost_found'. If LOST_FOUND feature is enabled, an empty directory lost+found is created by mkfs.
This is a preparation for fsck. During fsck, the directory is used to save unreachable files, which have no parent directory or their parent directory is removed by fsck. Encrypted files are also allowed to be saved here.
--- fsck/mount.c | 3 + include/f2fs_fs.h | 6 ++ mkfs/f2fs_format.c | 223 +++++++++++++++++++++++++++++++++++++++++++++--- mkfs/f2fs_format_main.c | 2 + 4 files changed, 222 insertions(+), 12 deletions(-)
fsck/mount.c +3 -0
@@ -460,6 +460,9 @@ void print_sb_state(struct f2fs_super_block *sb)if (f &cpu_to_le32 (F2FS_FEATURE_INODE_CRTIME )) {MSG (0 ,"%s" ," inode_crtime" ); } +if (f &cpu_to_le32 (F2FS_FEATURE_LOST_FOUND )) { +MSG (0 ,"%s" ," lost_found" ); + }MSG (0 ,"\n" );MSG (0 ,"Info: superblock encrypt level = %d, salt = " , sb->encryption_level);
include/f2fs_fs.h +6 -0
@@ -291,6 +291,8 @@ static inline uint64_t bswap_64(uint64_t val)#define VERSION_LEN 256 +#define LPF "lost+found" +enum f2fs_config_func {MKFS ,FSCK ,
@@ -369,6 +371,9 @@ struct f2fs_configuration {u_int32_t next_free_nid;u_int32_t quota_inum;u_int32_t quota_dnum; +u_int32_t lpf_inum; +u_int32_t lpf_dnum; +u_int32_t lpf_ino;/* defragmentation parameters */ int defrag_shrink;
@@ -557,6 +562,7 @@ enum {#define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR 0x0040#define F2FS_FEATURE_QUOTA_INO 0x0080#define F2FS_FEATURE_INODE_CRTIME 0x0100 +#define F2FS_FEATURE_LOST_FOUND 0x0200#define MAX_VOLUME_NAME 512
mkfs/f2fs_format.c +211 -12
321 lines not on this page · raw message
mkfs/f2fs_format_main.c +2 -0
@@ -95,6 +95,8 @@ static void parse_feature(const char *features) c.feature |=cpu_to_le32 (F2FS_FEATURE_QUOTA_INO ); }else if (!strcmp (features,"inode_crtime" )) { c.feature |=cpu_to_le32 (F2FS_FEATURE_INODE_CRTIME ); + }else if (!strcmp (features,"lost_found" )) { + c.feature |=cpu_to_le32 (F2FS_FEATURE_LOST_FOUND ); }else {MSG (0 ,"Error: Wrong features\n" );mkfs_usage ();
Signature · 6 lines
2.14.1 ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
RFC PATCH v3 2/2 fsck.f2fs: reconnect unreachable files to lost+found
In reply to Sheng YongThis patch introduces lost_found feature to fsck. If a file is found unreachable by fsck. Fsck tries to reconnect the file to lost+found directory:
1. Scan all unreachable file inodes, ignore non-inodes ones and directories.
2. Check them and fix incorrupted data to make sure filesystem metadata (mainly counters and main/nat bitmap) are all consistent.
3. Reconnect these files to lost+found. If lost+found does not exist, create it first. During reconnecting, expand lost+found's dentry block automatically. Reconnected files are renamed after its ino number.
4. If reconnect fails drop the node and restore filesystem metadata.
--- fsck/dir.c | 19 ++- fsck/fsck.c | 389 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- fsck/fsck.h | 3 + fsck/mount.c | 2 + 4 files changed, 410 insertions(+), 3 deletions(-)
fsck/dir.c +18 -1
@@ -176,6 +176,23 @@ static int f2fs_find_entry(struct f2fs_sb_info *sbi,return 0 ; } +/* return ino if file exists, otherwise return 0 */ +nid_t f2fs_lookup (struct f2fs_sb_info *sbi,struct f2fs_node *dir, +u8 *name,int len) +{ +int err; +struct dentry de = { + .name = name, + .len = len, + }; + + err =f2fs_find_entry (sbi, dir, &de); +if (err ==1 ) +return de.ino; +else +return 0 ; +} + static void f2fs_update_dentry(nid_t ino, int file_type, struct f2fs_dentry_ptr *d, const unsigned char *name, int len, f2fs_hash_t name_hash,
@@ -199,7 +216,7 @@ static void f2fs_update_dentry(nid_t ino, int file_type,/* * f2fs_add_link - Add a new file(dir) to parent dir. */ -static int f2fs_add_link (struct f2fs_sb_info *sbi,struct f2fs_node *parent, +int f2fs_add_link (struct f2fs_sb_info *sbi,struct f2fs_node *parent,const unsigned char *name,int name_len,nid_t ino,int file_type,block_t p_blkaddr,int inc_link) {
fsck/fsck.c +387 -2
420 lines not on this page · raw message
fsck/fsck.h +3 -0
@@ -243,6 +243,9 @@ int f2fs_mkdir(struct f2fs_sb_info *, struct dentry *);int f2fs_symlink (struct f2fs_sb_info *,struct dentry *);int inode_set_selinux (struct f2fs_sb_info *,u32 ,const char *);int f2fs_find_path (struct f2fs_sb_info *,char *,nid_t *); +nid_t f2fs_lookup (struct f2fs_sb_info *,struct f2fs_node *,u8 *,int ); +int f2fs_add_link (struct f2fs_sb_info *,struct f2fs_node *, +const unsigned char *,int ,nid_t ,int ,block_t ,int );/* xattr.c */ void *read_all_xattrs (struct f2fs_sb_info *,struct f2fs_node *);
fsck/mount.c +2 -0
@@ -1587,6 +1587,8 @@ void update_nat_blkaddr(struct f2fs_sb_info *sbi, nid_t ino,if (ino) nat_block->entries[entry_off].ino =cpu_to_le32 (ino); nat_block->entries[entry_off].block_addr =cpu_to_le32 (newaddr); +if (c.func ==FSCK ) +F2FS_FSCK (sbi)->entries[nid] = nat_block->entries[entry_off]; ret =dev_write_block (nat_block, block_addr);ASSERT (ret >=0 );
Signature · 6 lines
2.14.1 ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
Re: [RFC PATCH v3 2/2] fsck.f2fs: reconnect unreachable files to lost+found
Quoted 13 lines · Sheng Yong
This patch introduces
lost_foundfeature to fsck. If a file is found unreachable by fsck. Fsck tries to reconnect the file to lost+found directory:
1. Scan all unreachable file inodes, ignore non-inodes ones and directories.
2. Check them and fix incorrupted data to make sure filesystem metadata (mainly counters and main/nat bitmap) are all consistent.
3. Reconnect these files to lost+found. If lost+found does not exist, create it first. During reconnecting, expand lost+found's dentry block automatically. Reconnected files are renamed after its ino number.
4. If reconnect fails drop the node and restore filesystem metadata.
Quoted 1 line
Signed-off-by Sheng Yong <[email protected]>
Thanks,
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot