[fxfs] add lookup_extended_attribute

Split reading extended attributes into two steps - a general lookup
method that can read inline attributes from the tree, and otherwise
returns the attribute ID if the value is stored in an attribute.

This lookup method is now used for most of the extended attribute
helpers which did this lookup step. For local handle owners, the helpers
read the attribute as well like they used to.

The selinux helper and list_extended_attributes (on the handle and on
Directory) only read the tree, so they move to the generic side too. One
small difference: get_inline_selinux_context now returns Inconsistent
for an xattr record that points outside the xattr attribute range, as
the other xattr methods already did.

Bug: 570053552
Test: fx test fxfs_lib_test
Test: fx test fxfs-platform-test
Test: fx test fxfs-remote-volume-tests
Change-Id: I6224bf39778b6b81eb7bb641c61b33096a6a6964
Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/1971956
Lint: android-build-ayeaye@system.gserviceaccount.com <android-build-ayeaye@system.gserviceaccount.com>
Presubmit-Verified: fuchsia-internal-scoped@luci-project-accounts.iam.gserviceaccount.com <fuchsia-internal-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: James Sullivan <jfsulliv@google.com>
Commit-Queue: Stephen Demos <sdemos@google.com>
Risk: Fuchsia CL Risk (Advisory) <groupagent-fuchsia-risk@google.com>
Fuchsia-Auto-Submit: Stephen Demos <sdemos@google.com>
diff --git a/src/storage/fxfs/src/object_store/directory.rs b/src/storage/fxfs/src/object_store/directory.rs
index 76ffc5b..0ce28bb 100644
--- a/src/storage/fxfs/src/object_store/directory.rs
+++ b/src/storage/fxfs/src/object_store/directory.rs
@@ -486,6 +486,11 @@
             )
         }
     }
+
+    pub async fn list_extended_attributes(&self) -> Result<Vec<Vec<u8>>, Error> {
+        ensure!(!self.is_deleted(), FxfsError::Deleted);
+        self.handle.list_extended_attributes().await
+    }
 }
 
 #[fxfs_trace::trace]
@@ -1410,11 +1415,6 @@
         Ok(())
     }
 
-    pub async fn list_extended_attributes(&self) -> Result<Vec<Vec<u8>>, Error> {
-        ensure!(!self.is_deleted(), FxfsError::Deleted);
-        self.handle.list_extended_attributes().await
-    }
-
     pub async fn get_extended_attribute(&self, name: Vec<u8>) -> Result<Vec<u8>, Error> {
         ensure!(!self.is_deleted(), FxfsError::Deleted);
         self.handle.get_extended_attribute(name).await
diff --git a/src/storage/fxfs/src/object_store/store_object_handle.rs b/src/storage/fxfs/src/object_store/store_object_handle.rs
index 1dca3ad..c1e62655 100644
--- a/src/storage/fxfs/src/object_store/store_object_handle.rs
+++ b/src/storage/fxfs/src/object_store/store_object_handle.rs
@@ -421,6 +421,75 @@
     pub fn is_encrypted(&self) -> bool {
         !matches!(self.encryption, Encryption::None)
     }
+
+    pub async fn list_extended_attributes(&self) -> Result<Vec<Vec<u8>>, Error> {
+        let layer_set = self.store().tree().layer_set();
+        let mut merger = layer_set.merger();
+        // Seek to the first extended attribute key for this object.
+        let mut iter = merger
+            .query(Query::FullRange(&ObjectKey::extended_attribute(self.object_id(), Vec::new())))
+            .await?;
+        let mut out = Vec::new();
+        while let Some(item) = iter.get() {
+            // Skip deleted extended attributes.
+            if item.value != &ObjectValue::None {
+                match item.key {
+                    ObjectKey { object_id, data: ObjectKeyData::ExtendedAttribute { name } }
+                        if *object_id == self.object_id() =>
+                    {
+                        out.push(name.clone());
+                    }
+                    // Once we hit a record belonging to another object, or one that is not an
+                    // extended attribute key, we have reached the end of this object's extended
+                    // attributes. Subsequent objects' records will start with lower variants
+                    // (e.g. ObjectKeyData::Object) which trigger this break.
+                    _ => break,
+                }
+            }
+            iter.advance().await?;
+        }
+        Ok(out)
+    }
+
+    /// Looks up the extended attribute `name`, returning `None` if it doesn't exist. Values that
+    /// are too large to be stored inline are stored in an attribute (in the Fxfs sense), which the
+    /// caller has to read separately (e.g. with `read_attr`).
+    pub async fn lookup_extended_attribute(
+        &self,
+        name: Vec<u8>,
+    ) -> Result<Option<ExtendedAttributeValue>, Error> {
+        let key = ObjectKey::extended_attribute(self.object_id(), name);
+        match self.store().tree().find_value(&key).await? {
+            None => Ok(None),
+            Some(ObjectValue::ExtendedAttribute(value)) => {
+                if let ExtendedAttributeValue::AttributeId(id) = &value {
+                    ensure!(id.is_xattr(), FxfsError::Inconsistent);
+                }
+                Ok(Some(value))
+            }
+            Some(_) => bail!(
+                anyhow!(FxfsError::Inconsistent)
+                    .context("lookup_extended_attribute: Expected ExtendedAttribute value")
+            ),
+        }
+    }
+
+    /// Looks up the values for the extended attribute `fio::SELINUX_CONTEXT_NAME`, returning it
+    /// if it is found inline. If it is not inline, it will request use of the
+    /// `get_extended_attributes` method. If the entry doesn't exist at all, returns None.
+    pub async fn get_inline_selinux_context(&self) -> Result<Option<fio::SelinuxContext>, Error> {
+        // This optimization is only useful as long as the attribute is smaller than inline sizes.
+        // Avoid reading the data out of the attributes.
+        const_assert!(fio::MAX_SELINUX_CONTEXT_ATTRIBUTE_LEN as usize <= MAX_INLINE_XATTR_SIZE);
+        Ok(self.lookup_extended_attribute(fio::SELINUX_CONTEXT_NAME.into()).await?.map(|value| {
+            match value {
+                ExtendedAttributeValue::Inline(value) => fio::SelinuxContext::Data(value),
+                ExtendedAttributeValue::AttributeId(_) => {
+                    fio::SelinuxContext::UseExtendedAttributes(fio::EmptyStruct {})
+                }
+            }
+        }))
+    }
 }
 
 impl<S: LocalHandleOwner> StoreObjectHandle<S> {
@@ -2086,81 +2155,12 @@
         }
     }
 
-    pub async fn list_extended_attributes(&self) -> Result<Vec<Vec<u8>>, Error> {
-        let layer_set = self.store().tree().layer_set();
-        let mut merger = layer_set.merger();
-        // Seek to the first extended attribute key for this object.
-        let mut iter = merger
-            .query(Query::FullRange(&ObjectKey::extended_attribute(self.object_id(), Vec::new())))
-            .await?;
-        let mut out = Vec::new();
-        while let Some(item) = iter.get() {
-            // Skip deleted extended attributes.
-            if item.value != &ObjectValue::None {
-                match item.key {
-                    ObjectKey { object_id, data: ObjectKeyData::ExtendedAttribute { name } }
-                        if *object_id == self.object_id() =>
-                    {
-                        out.push(name.clone());
-                    }
-                    // Once we hit a record belonging to another object, or one that is not an
-                    // extended attribute key, we have reached the end of this object's extended
-                    // attributes. Subsequent objects' records will start with lower variants
-                    // (e.g. ObjectKeyData::Object) which trigger this break.
-                    _ => break,
-                }
-            }
-            iter.advance().await?;
-        }
-        Ok(out)
-    }
-
-    /// Looks up the values for the extended attribute `fio::SELINUX_CONTEXT_NAME`, returning it
-    /// if it is found inline. If it is not inline, it will request use of the
-    /// `get_extended_attributes` method. If the entry doesn't exist at all, returns None.
-    pub async fn get_inline_selinux_context(&self) -> Result<Option<fio::SelinuxContext>, Error> {
-        // This optimization is only useful as long as the attribute is smaller than inline sizes.
-        // Avoid reading the data out of the attributes.
-        const_assert!(fio::MAX_SELINUX_CONTEXT_ATTRIBUTE_LEN as usize <= MAX_INLINE_XATTR_SIZE);
-        self.store()
-            .tree()
-            .find_map(
-                &ObjectKey::extended_attribute(self.object_id(), fio::SELINUX_CONTEXT_NAME.into()),
-                |item| match item.value {
-                    ObjectValue::ExtendedAttribute(ExtendedAttributeValue::Inline(value)) => {
-                        Ok(fio::SelinuxContext::Data(value.clone()))
-                    }
-                    ObjectValue::ExtendedAttribute(ExtendedAttributeValue::AttributeId(_)) => {
-                        Ok(fio::SelinuxContext::UseExtendedAttributes(fio::EmptyStruct {}))
-                    }
-                    _ => Err(anyhow!(FxfsError::Inconsistent).context(
-                        "get_inline_extended_attribute: Expected ExtendedAttribute value",
-                    )),
-                },
-            )
-            .await?
-            .transpose()
-    }
-
     pub async fn get_extended_attribute(&self, name: Vec<u8>) -> Result<Vec<u8>, Error> {
-        let value = self
-            .store()
-            .tree()
-            .find_value(&ObjectKey::extended_attribute(self.object_id(), name))
-            .await?
-            .ok_or(FxfsError::NotFound)?;
-        match value {
-            ObjectValue::ExtendedAttribute(ExtendedAttributeValue::Inline(value)) => Ok(value),
-            ObjectValue::ExtendedAttribute(ExtendedAttributeValue::AttributeId(id)) => {
-                ensure!(id.is_xattr(), FxfsError::Inconsistent);
+        match self.lookup_extended_attribute(name).await?.ok_or(FxfsError::NotFound)? {
+            ExtendedAttributeValue::Inline(value) => Ok(value),
+            ExtendedAttributeValue::AttributeId(id) => {
                 Ok(self.read_attr(id).await?.ok_or(FxfsError::Inconsistent)?.into_vec())
             }
-            _ => {
-                bail!(
-                    anyhow!(FxfsError::Inconsistent)
-                        .context("get_extended_attribute: Expected ExtendedAttribute value")
-                )
-            }
         }
     }
 
@@ -2190,34 +2190,13 @@
         ensure!(name.len() <= MAX_XATTR_NAME_SIZE, FxfsError::TooBig);
         ensure!(value.len() <= MAX_XATTR_VALUE_SIZE, FxfsError::TooBig);
         let tree = self.store().tree();
-        let object_key = ObjectKey::extended_attribute(self.object_id(), name);
+        let object_key = ObjectKey::extended_attribute(self.object_id(), name.clone());
 
-        let existing_attribute_id = {
-            let find_result = tree
-                .find_map(&object_key, |item| match item.value {
-                    ObjectValue::ExtendedAttribute(ExtendedAttributeValue::Inline(..)) => Ok(None),
-                    ObjectValue::ExtendedAttribute(ExtendedAttributeValue::AttributeId(id)) => {
-                        ensure!(id.is_xattr(), FxfsError::Inconsistent);
-                        Ok(Some(*id))
-                    }
-                    _ => Err(anyhow!(FxfsError::Inconsistent)
-                        .context("expected extended attribute value")),
-                })
-                .await?;
-            let (found, existing_attribute_id) = match find_result {
-                Some(id) => (true, id?),
-                None => (false, None),
-            };
-            match mode {
-                SetExtendedAttributeMode::Create if found => {
-                    bail!(FxfsError::AlreadyExists)
-                }
-                SetExtendedAttributeMode::Replace if !found => {
-                    bail!(FxfsError::NotFound)
-                }
-                _ => (),
-            }
-            existing_attribute_id
+        let existing_attribute_id = match (mode, self.lookup_extended_attribute(name).await?) {
+            (SetExtendedAttributeMode::Create, Some(_)) => bail!(FxfsError::AlreadyExists),
+            (SetExtendedAttributeMode::Replace, None) => bail!(FxfsError::NotFound),
+            (_, Some(ExtendedAttributeValue::AttributeId(id))) => Some(id),
+            (_, Some(ExtendedAttributeValue::Inline(_)) | None) => None,
         };
 
         if let Some(attribute_id) = existing_attribute_id {
@@ -2299,8 +2278,7 @@
 
     pub async fn remove_extended_attribute(&self, name: Vec<u8>) -> Result<(), Error> {
         let store = self.store();
-        let tree = store.tree();
-        let object_key = ObjectKey::extended_attribute(self.object_id(), name);
+        let object_key = ObjectKey::extended_attribute(self.object_id(), name.clone());
 
         // NB: The API says we have to return an error if the attribute doesn't exist, so we have
         // to look it up first to make sure we have a record of it before we delete it. Make sure
@@ -2309,18 +2287,11 @@
         let keys = lock_keys![LockKey::object(store.store_object_id(), self.object_id())];
         let mut transaction = store.new_transaction(keys, Options::default()).await?;
 
-        let attribute_to_delete = tree
-            .find_map(&object_key, |item| match item.value {
-                ObjectValue::ExtendedAttribute(ExtendedAttributeValue::AttributeId(id)) => {
-                    ensure!(id.is_xattr(), FxfsError::Inconsistent);
-                    Ok(Some(*id))
-                }
-                ObjectValue::ExtendedAttribute(ExtendedAttributeValue::Inline(..)) => Ok(None),
-                _ => Err(anyhow!(FxfsError::Inconsistent)
-                    .context("remove_extended_attribute: Expected ExtendedAttribute value")),
-            })
-            .await?
-            .ok_or(FxfsError::NotFound)??;
+        let attribute_to_delete =
+            match self.lookup_extended_attribute(name).await?.ok_or(FxfsError::NotFound)? {
+                ExtendedAttributeValue::AttributeId(id) => Some(id),
+                ExtendedAttributeValue::Inline(_) => None,
+            };
 
         transaction.add(
             store.store_object_id(),
@@ -2490,10 +2461,12 @@
     use crate::object_store::object_record::ObjectKeyData;
     use crate::object_store::transaction::{Mutation, ObjectStoreMutation, Options, lock_keys};
     use crate::object_store::{
-        AttributeId, AttributeKey, DataObjectHandle, Directory, HandleOptions, LockKey, ObjectKey,
-        ObjectStore, ObjectValue, SetExtendedAttributeMode, StoreObjectHandle,
+        AttributeId, AttributeKey, DataObjectHandle, Directory, ExtendedAttributeValue,
+        HandleOptions, LockKey, ObjectKey, ObjectStore, ObjectValue, SetExtendedAttributeMode,
+        StoreObjectHandle,
     };
     use bit_vec::BitVec;
+    use fidl_fuchsia_io as fio;
     use fuchsia_async as fasync;
     use futures::{TryStreamExt, join};
     use std::sync::Arc;
@@ -2790,9 +2763,21 @@
                 ObjectValue::extended_attribute(AttributeId::DATA),
             ),
         );
+        transaction.add(
+            store.store_object_id(),
+            Mutation::replace_or_insert_object(
+                ObjectKey::extended_attribute(object.object_id(), fio::SELINUX_CONTEXT_NAME.into()),
+                ObjectValue::extended_attribute(AttributeId::DATA),
+            ),
+        );
         transaction.commit().await.unwrap();
 
         is_error(
+            object.lookup_extended_attribute(b"bad_attr".to_vec()).await.unwrap_err(),
+            FxfsError::Inconsistent,
+        );
+        is_error(object.get_inline_selinux_context().await.unwrap_err(), FxfsError::Inconsistent);
+        is_error(
             object.get_extended_attribute(b"bad_attr".to_vec()).await.unwrap_err(),
             FxfsError::Inconsistent,
         );
@@ -2875,6 +2860,40 @@
     }
 
     #[fuchsia::test]
+    async fn lookup_extended_attribute() {
+        let (fs, object) = test_filesystem_and_empty_object().await;
+
+        let inline_attr = TestAttr::new(b"user.inline", b"foo");
+        let large_attr = TestAttr::new(b"user.large", vec![3u8; 300]);
+        for attr in [&inline_attr, &large_attr] {
+            object
+                .set_extended_attribute(attr.name(), attr.value(), SetExtendedAttributeMode::Set)
+                .await
+                .unwrap();
+        }
+
+        assert_eq!(object.lookup_extended_attribute(b"user.missing".to_vec()).await.unwrap(), None);
+        assert_eq!(
+            object.lookup_extended_attribute(inline_attr.name()).await.unwrap(),
+            Some(ExtendedAttributeValue::Inline(inline_attr.value()))
+        );
+        let Some(ExtendedAttributeValue::AttributeId(id)) =
+            object.lookup_extended_attribute(large_attr.name()).await.unwrap()
+        else {
+            panic!("expected the large value to be stored in an attribute");
+        };
+        assert_eq!(
+            object.read_attr(id).await.unwrap().expect("read_attr returned none").into_vec(),
+            large_attr.value()
+        );
+
+        object.remove_extended_attribute(inline_attr.name()).await.unwrap();
+        assert_eq!(object.lookup_extended_attribute(inline_attr.name()).await.unwrap(), None);
+
+        fs.close().await.expect("close failed");
+    }
+
+    #[fuchsia::test]
     async fn multiple_extended_attributes() {
         let (fs, object) = test_filesystem_and_empty_object().await;