Skip to content

Commit 2762b82

Browse files
jiangliueryugey
authored andcommitted
vfs: correctly set attr.st_ino for loopup()
Correctly set attr.st_ino for loopup(), otherwise stat() syscall may get wrong data. Signed-off-by: Jiang Liu <gerry@linux.alibaba.com>
1 parent 0c7c137 commit 2762b82

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

src/api/vfs/mod.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,11 +533,13 @@ impl Vfs {
533533
// cross mountpoint, return mount root entry
534534
entry = mnt.root_entry;
535535
entry.inode = self.convert_inode(mnt.fs_idx, mnt.ino)?;
536+
entry.attr.st_ino = entry.inode;
536537
trace!(
537-
"vfs lookup cross mountpoint, return new mount fs_idx {} inode {} fuse inode {}",
538+
"vfs lookup cross mountpoint, return new mount fs_idx {} inode 0x{:x} fuse inode 0x{:x}, attr inode 0x{:x}",
538539
mnt.fs_idx,
539540
mnt.ino,
540-
entry.inode
541+
entry.inode,
542+
entry.attr.st_ino,
541543
);
542544
}
543545
None => entry.inode = self.convert_inode(idata.fs_idx(), entry.inode)?,
@@ -560,6 +562,18 @@ mod tests {
560562
fn lookup(&self, _: &Context, _: Self::Inode, _: &CStr) -> Result<Entry> {
561563
Ok(Entry::default())
562564
}
565+
fn getattr(
566+
&self,
567+
_ctx: &Context,
568+
_inode: Self::Inode,
569+
_handle: Option<Self::Handle>,
570+
) -> Result<(stat64, Duration)> {
571+
let mut attr = Attr {
572+
..Default::default()
573+
};
574+
attr.ino = 1;
575+
Ok((attr.into(), Duration::from_secs(1)))
576+
}
563577
}
564578

565579
pub(crate) struct FakeFileSystemTwo {}
@@ -1013,6 +1027,11 @@ mod tests {
10131027
)
10141028
.unwrap();
10151029
assert_eq!(entry3.inode, 0);
1030+
1031+
let (stat, _) = vfs
1032+
.getattr(&ctx, VfsInode(0x100_0000_0000_0001), None)
1033+
.unwrap();
1034+
assert_eq!(stat.st_ino, 0x100_0000_0000_0001);
10161035
}
10171036

10181037
#[test]
@@ -1022,6 +1041,18 @@ mod tests {
10221041
let fs2 = FakeFileSystemTwo {};
10231042
assert!(vfs.mount(Box::new(fs1), "/foo").is_ok());
10241043
assert!(vfs.mount(Box::new(fs2), "/bar").is_ok());
1044+
1045+
// Lookup inode on pseudo file system.
1046+
let ctx = Context::new();
1047+
let entry1 = vfs
1048+
.lookup(
1049+
&ctx,
1050+
ROOT_ID.into(),
1051+
CString::new("bar").unwrap().as_c_str(),
1052+
)
1053+
.unwrap();
1054+
assert_eq!(entry1.inode, 0x200_0000_0000_0001);
1055+
assert_eq!(entry1.attr.st_ino, 0x200_0000_0000_0001);
10251056
}
10261057

10271058
#[test]

0 commit comments

Comments
 (0)