Bug 218309 - BUG REPORT: ubifs: inconsistent filesystem if powercut in selinux or encryption scenario
Summary: BUG REPORT: ubifs: inconsistent filesystem if powercut in selinux or encrypti...
Status: NEW
Alias: None
Product: File System
Classification: Unclassified
Component: Other (show other bugs)
Hardware: All Linux
: P3 normal
Assignee: fs_other
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-12-25 12:57 UTC by Zhihao Cheng
Modified: 2024-04-07 01:43 UTC (History)
0 users

See Also:
Kernel Version:
Subsystem:
Regression: No
Bisected commit-id:


Attachments
diff (347 bytes, patch)
2023-12-25 13:07 UTC, Zhihao Cheng
Details | Diff
test.sh (1.10 KB, application/x-shellscript)
2023-12-25 13:07 UTC, Zhihao Cheng
Details
partially fix patch (8.01 KB, application/mbox)
2024-03-28 13:46 UTC, Zhihao Cheng
Details
powercut test case (2.39 KB, application/x-shellscript)
2024-04-07 01:42 UTC, Zhihao Cheng
Details

Description Zhihao Cheng 2023-12-25 12:57:51 UTC
UBIFS will mount failed if chk_fs enabled after powercut in selinux or encryption scenario:
[  281.381740] UBIFS error (ubi0:0 pid 7995): dbg_check_filesystem [ubifs]: inode 66 nlink is 1, but calculated nlink is 0
[  281.383330] UBIFS (ubi0:0): dump of the inode 66 sitting in LEB 12:4360
[  281.383334] 	magic          0x6101831
[  281.383784] 	crc            0xf4acb4b7
[  281.384300] 	node_type      0 (inode node)
[  281.384839] 	group_type     2 (last of node group)
[  281.385353] 	sqnum          18
[  281.385720] 	len            160
[  281.386148] 	key            (66, inode)
[  281.386611] 	creat_sqnum    14
[  281.387030] 	size           0
[  281.387399] 	nlink          1
[  281.387816] 	atime          1703508734.0
[  281.388303] 	mtime          1703508734.0
[  281.388813] 	ctime          1703508734.0
[  281.389302] 	uid            0
[  281.389668] 	gid            0
[  281.390076] 	mode           33188
[  281.390483] 	flags          0x41
[  281.390917] 	xattr_cnt      1
[  281.391295] 	xattr_size     272
[  281.391682] 	xattr_names    1
[  281.392088] 	compr_type     0x3
[  281.392476] 	data len       0
[  281.392878] UBIFS error (ubi0:0 pid 7995): dbg_check_filesystem [ubifs]: file-system check failed with error -22
[  281.394165] CPU: 1 PID: 7995 Comm: mount Not tainted 6.7.0-rc7-00010-g029b7d9ce6ab-dirty #358
[  281.395199] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.16.1-2.fc37 04/01/2014
[  281.396254] Call Trace:
[  281.396516]  <TASK>
[  281.396746]  dump_stack_lvl+0x86/0xc0
[  281.397177]  dump_stack+0x18/0x30
[  281.397524]  dbg_check_filesystem+0xa0/0x4e0 [ubifs]
[  281.398076]  ubifs_mount+0x1d9f/0x2f80 [ubifs]
[  281.398558]  legacy_get_tree+0x30/0x70
[  281.398978]  vfs_get_tree+0x2b/0x150
[  281.399361]  path_mount+0x392/0x1060
[  281.399742]  do_mount+0x91/0xc0
[  281.400096]  __x64_sys_mount+0xc2/0x160
[  281.400501]  do_syscall_64+0x6f/0x140
[  281.400897]  entry_SYSCALL_64_after_hwframe+0x63/0x6b
Comment 1 Zhihao Cheng 2023-12-25 13:05:52 UTC
There are two paths creating xattrs before creating a new file:
ubifs_create
  PATH 1: ubifs_new_inode -> fscrypt_set_context -> ubifs_crypt_set_context -> ubifs_xattr_set -> create_xattr -> ubifs_jnl_update // inode and xattrs are written on disk
  PATH 2: ubifs_init_security -> security_inode_init_security -> init_xattrs -> ubifs_xattr_set -> create_xattr -> ubifs_jnl_update // inode and xattrs are written on disk
  power cut !!!
  ubifs_jnl_update //  write inode and dentry on disk

Inode and xattrs are found on disk after mounting, there is no dentry corresponds to the inode, dbg_check_filesystem will report inconsistent filesystem!

This problem is hard to be fixed, because UBIFS does't support an atomic operation for ubifs_create if selinux or encryption is enabled. The atomic op only works in ubifs_jnl_update for now.
Comment 2 Zhihao Cheng 2023-12-25 13:07:38 UTC
Created attachment 305653 [details]
diff
Comment 3 Zhihao Cheng 2023-12-25 13:07:58 UTC
Created attachment 305654 [details]
test.sh

Reproducer:
CONFIG_FS_ENCRYPTION=y

1. Apply diff and compile kernel
2. ./test.sh && dmesg

[  281.381740] UBIFS error (ubi0:0 pid 7995): dbg_check_filesystem [ubifs]: inode 66 nlink is 1, but calculated nlink is 0
[  281.383330] UBIFS (ubi0:0): dump of the inode 66 sitting in LEB 12:4360
[  281.383334] 	magic          0x6101831
[  281.383784] 	crc            0xf4acb4b7
[  281.384300] 	node_type      0 (inode node)
[  281.384839] 	group_type     2 (last of node group)
[  281.385353] 	sqnum          18
[  281.385720] 	len            160
[  281.386148] 	key            (66, inode)
[  281.386611] 	creat_sqnum    14
[  281.387030] 	size           0
[  281.387399] 	nlink          1
[  281.387816] 	atime          1703508734.0
[  281.388303] 	mtime          1703508734.0
[  281.388813] 	ctime          1703508734.0
[  281.389302] 	uid            0
[  281.389668] 	gid            0
[  281.390076] 	mode           33188
[  281.390483] 	flags          0x41
[  281.390917] 	xattr_cnt      1
[  281.391295] 	xattr_size     272
[  281.391682] 	xattr_names    1
[  281.392088] 	compr_type     0x3
[  281.392476] 	data len       0
[  281.392878] UBIFS error (ubi0:0 pid 7995): dbg_check_filesystem [ubifs]: file-system check failed with error -22
Comment 4 Zhihao Cheng 2024-03-28 13:46:24 UTC
Created attachment 306055 [details]
partially fix patch

cannot fix following case:
    P1                             P2
ubifs_create
  ubifs_init_security
   ubifs_xattr_set
    ubifs_jnl_update
                                  sync 
     >> powercut! <<
  ubifs_jnl_update
Comment 5 Zhihao Cheng 2024-04-07 01:42:58 UTC
Created attachment 306099 [details]
powercut test case
Comment 6 Zhihao Cheng 2024-04-07 01:43:16 UTC
(In reply to Zhihao Cheng from comment #5)
> Created attachment 306099 [details]
> powercut test case

Based on the framework [1]

[1] https://patchwork.ozlabs.org/project/linux-mtd/patch/20231228013639.2827205-2-chengzhihao1@huawei.com/

Note You need to log in before you can comment on or make changes to this bug.