]> git.apps.os.sepia.ceph.com Git - ceph-client.git/commitdiff
rust: device: implement Device::parent()
authorDanilo Krummrich <dakr@kernel.org>
Mon, 14 Apr 2025 13:18:05 +0000 (15:18 +0200)
committerDanilo Krummrich <dakr@kernel.org>
Sat, 19 Apr 2025 08:41:28 +0000 (10:41 +0200)
Device::parent() returns a reference to the device' parent device, if
any.

Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Link: https://lore.kernel.org/r/20250414131934.28418-3-dakr@kernel.org
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
rust/kernel/device.rs

index 0353c5552769c1f4cfce778cb7cd3e315681fe2d..0d237cebd65eb447a9bbcbe5214a75a50a49850e 100644 (file)
@@ -67,6 +67,25 @@ impl<Ctx: DeviceContext> Device<Ctx> {
         self.0.get()
     }
 
+    /// Returns a reference to the parent device, if any.
+    #[expect(unused)]
+    pub(crate) fn parent(&self) -> Option<&Self> {
+        // SAFETY:
+        // - By the type invariant `self.as_raw()` is always valid.
+        // - The parent device is only ever set at device creation.
+        let parent = unsafe { (*self.as_raw()).parent };
+
+        if parent.is_null() {
+            None
+        } else {
+            // SAFETY:
+            // - Since `parent` is not NULL, it must be a valid pointer to a `struct device`.
+            // - `parent` is valid for the lifetime of `self`, since a `struct device` holds a
+            //   reference count of its parent.
+            Some(unsafe { Self::as_ref(parent) })
+        }
+    }
+
     /// Convert a raw C `struct device` pointer to a `&'a Device`.
     ///
     /// # Safety