Merge "Use AtomicU64 for handle_ref_count"

This commit is contained in:
Treehugger Robot 2022-03-09 18:28:59 +00:00 committed by Gerrit Code Review
commit 4031fb3346
1 changed files with 14 additions and 11 deletions

View File

@ -104,7 +104,7 @@ struct InodeState {
/// stay alive until the reference count reaches zero. /// stay alive until the reference count reaches zero.
/// ///
/// Note: This is not to be confused with hardlinks, which AuthFS doesn't currently implement. /// Note: This is not to be confused with hardlinks, which AuthFS doesn't currently implement.
handle_ref_count: u64, handle_ref_count: AtomicU64,
/// Whether the inode is already unlinked, i.e. should be removed, once `handle_ref_count` is /// Whether the inode is already unlinked, i.e. should be removed, once `handle_ref_count` is
/// down to zero. /// down to zero.
@ -113,11 +113,11 @@ struct InodeState {
impl InodeState { impl InodeState {
fn new(entry: AuthFsEntry) -> Self { fn new(entry: AuthFsEntry) -> Self {
InodeState { entry, handle_ref_count: 0, unlinked: false } InodeState { entry, handle_ref_count: AtomicU64::new(0), unlinked: false }
} }
fn new_with_ref_count(entry: AuthFsEntry, handle_ref_count: u64) -> Self { fn new_with_ref_count(entry: AuthFsEntry, handle_ref_count: u64) -> Self {
InodeState { entry, handle_ref_count, unlinked: false } InodeState { entry, handle_ref_count: AtomicU64::new(handle_ref_count), unlinked: false }
} }
} }
@ -511,7 +511,7 @@ impl FileSystem for AuthFs {
} }
fn lookup(&self, _ctx: Context, parent: Inode, name: &CStr) -> io::Result<Entry> { fn lookup(&self, _ctx: Context, parent: Inode, name: &CStr) -> io::Result<Entry> {
let mut inode_table = self.inode_table.write().unwrap(); let inode_table = self.inode_table.read().unwrap();
// Look up the entry's inode number in parent directory. // Look up the entry's inode number in parent directory.
let inode = let inode =
@ -528,8 +528,8 @@ impl FileSystem for AuthFs {
})?; })?;
// Create the entry's stat if found. // Create the entry's stat if found.
let st = handle_inode_mut_locked( let st = handle_inode_locked(
&mut inode_table, &inode_table,
&inode, &inode,
|InodeState { entry, handle_ref_count, .. }| { |InodeState { entry, handle_ref_count, .. }| {
let st = match entry { let st = match entry {
@ -551,7 +551,9 @@ impl FileSystem for AuthFs {
AccessMode::Variable(attr.mode()), AccessMode::Variable(attr.mode()),
), ),
}?; }?;
*handle_ref_count += 1; if handle_ref_count.fetch_add(1, Ordering::Relaxed) == u64::MAX {
panic!("Handle reference count overflow");
}
Ok(st) Ok(st)
}, },
)?; )?;
@ -571,15 +573,16 @@ impl FileSystem for AuthFs {
&mut inode_table, &mut inode_table,
&inode, &inode,
|InodeState { handle_ref_count, unlinked, .. }| { |InodeState { handle_ref_count, unlinked, .. }| {
if count > *handle_ref_count { let current = handle_ref_count.get_mut();
if count > *current {
error!( error!(
"Trying to decrease refcount of inode {} by {} (> current {})", "Trying to decrease refcount of inode {} by {} (> current {})",
inode, count, *handle_ref_count inode, count, *current
); );
panic!(); // log to logcat with error! panic!(); // log to logcat with error!
} }
*handle_ref_count = handle_ref_count.saturating_sub(count); *current -= count;
Ok(*unlinked && *handle_ref_count == 0) Ok(*unlinked && *current == 0)
}, },
); );