xref: /linux/rust/kernel/debugfs.rs (revision 5e40b591cb46c0379d5406fa5548c9b2a3801353)
17f201ca1SMatthew Maurer // SPDX-License-Identifier: GPL-2.0
27f201ca1SMatthew Maurer // Copyright (C) 2025 Google LLC.
37f201ca1SMatthew Maurer 
47f201ca1SMatthew Maurer //! DebugFS Abstraction
57f201ca1SMatthew Maurer //!
67f201ca1SMatthew Maurer //! C header: [`include/linux/debugfs.h`](srctree/include/linux/debugfs.h)
77f201ca1SMatthew Maurer 
87f201ca1SMatthew Maurer // When DebugFS is disabled, many parameters are dead. Linting for this isn't helpful.
97f201ca1SMatthew Maurer #![cfg_attr(not(CONFIG_DEBUG_FS), allow(unused_variables))]
107f201ca1SMatthew Maurer 
117f201ca1SMatthew Maurer use crate::prelude::*;
127f201ca1SMatthew Maurer use crate::str::CStr;
137f201ca1SMatthew Maurer #[cfg(CONFIG_DEBUG_FS)]
147f201ca1SMatthew Maurer use crate::sync::Arc;
15*5e40b591SMatthew Maurer use core::marker::PhantomPinned;
16*5e40b591SMatthew Maurer use core::ops::Deref;
177f201ca1SMatthew Maurer 
18*5e40b591SMatthew Maurer mod traits;
19*5e40b591SMatthew Maurer pub use traits::Writer;
20*5e40b591SMatthew Maurer 
21*5e40b591SMatthew Maurer mod file_ops;
22*5e40b591SMatthew Maurer use file_ops::{FileOps, ReadFile};
237f201ca1SMatthew Maurer #[cfg(CONFIG_DEBUG_FS)]
247f201ca1SMatthew Maurer mod entry;
257f201ca1SMatthew Maurer #[cfg(CONFIG_DEBUG_FS)]
267f201ca1SMatthew Maurer use entry::Entry;
277f201ca1SMatthew Maurer 
287f201ca1SMatthew Maurer /// Owning handle to a DebugFS directory.
297f201ca1SMatthew Maurer ///
307f201ca1SMatthew Maurer /// The directory in the filesystem represented by [`Dir`] will be removed when handle has been
317f201ca1SMatthew Maurer /// dropped *and* all children have been removed.
327f201ca1SMatthew Maurer // If we have a parent, we hold a reference to it in the `Entry`. This prevents the `dentry`
337f201ca1SMatthew Maurer // we point to from being cleaned up if our parent `Dir`/`Entry` is dropped before us.
347f201ca1SMatthew Maurer //
357f201ca1SMatthew Maurer // The `None` option indicates that the `Arc` could not be allocated, so our children would not be
367f201ca1SMatthew Maurer // able to refer to us. In this case, we need to silently fail. All future child directories/files
377f201ca1SMatthew Maurer // will silently fail as well.
387f201ca1SMatthew Maurer #[derive(Clone)]
397f201ca1SMatthew Maurer pub struct Dir(#[cfg(CONFIG_DEBUG_FS)] Option<Arc<Entry>>);
407f201ca1SMatthew Maurer 
417f201ca1SMatthew Maurer impl Dir {
427f201ca1SMatthew Maurer     /// Create a new directory in DebugFS. If `parent` is [`None`], it will be created at the root.
437f201ca1SMatthew Maurer     fn create(name: &CStr, parent: Option<&Dir>) -> Self {
447f201ca1SMatthew Maurer         #[cfg(CONFIG_DEBUG_FS)]
457f201ca1SMatthew Maurer         {
467f201ca1SMatthew Maurer             let parent_entry = match parent {
477f201ca1SMatthew Maurer                 // If the parent couldn't be allocated, just early-return
487f201ca1SMatthew Maurer                 Some(Dir(None)) => return Self(None),
497f201ca1SMatthew Maurer                 Some(Dir(Some(entry))) => Some(entry.clone()),
507f201ca1SMatthew Maurer                 None => None,
517f201ca1SMatthew Maurer             };
527f201ca1SMatthew Maurer             Self(
537f201ca1SMatthew Maurer                 // If Arc creation fails, the `Entry` will be dropped, so the directory will be
547f201ca1SMatthew Maurer                 // cleaned up.
557f201ca1SMatthew Maurer                 Arc::new(Entry::dynamic_dir(name, parent_entry), GFP_KERNEL).ok(),
567f201ca1SMatthew Maurer             )
577f201ca1SMatthew Maurer         }
587f201ca1SMatthew Maurer         #[cfg(not(CONFIG_DEBUG_FS))]
597f201ca1SMatthew Maurer         Self()
607f201ca1SMatthew Maurer     }
617f201ca1SMatthew Maurer 
62*5e40b591SMatthew Maurer     /// Creates a DebugFS file which will own the data produced by the initializer provided in
63*5e40b591SMatthew Maurer     /// `data`.
64*5e40b591SMatthew Maurer     fn create_file<'a, T, E: 'a>(
65*5e40b591SMatthew Maurer         &'a self,
66*5e40b591SMatthew Maurer         name: &'a CStr,
67*5e40b591SMatthew Maurer         data: impl PinInit<T, E> + 'a,
68*5e40b591SMatthew Maurer         file_ops: &'static FileOps<T>,
69*5e40b591SMatthew Maurer     ) -> impl PinInit<File<T>, E> + 'a
70*5e40b591SMatthew Maurer     where
71*5e40b591SMatthew Maurer         T: Sync + 'static,
72*5e40b591SMatthew Maurer     {
73*5e40b591SMatthew Maurer         let scope = Scope::<T>::new(data, move |data| {
74*5e40b591SMatthew Maurer             #[cfg(CONFIG_DEBUG_FS)]
75*5e40b591SMatthew Maurer             if let Some(parent) = &self.0 {
76*5e40b591SMatthew Maurer                 // SAFETY: Because data derives from a scope, and our entry will be dropped before
77*5e40b591SMatthew Maurer                 // the data is dropped, it is guaranteed to outlive the entry we return.
78*5e40b591SMatthew Maurer                 unsafe { Entry::dynamic_file(name, parent.clone(), data, file_ops) }
79*5e40b591SMatthew Maurer             } else {
80*5e40b591SMatthew Maurer                 Entry::empty()
81*5e40b591SMatthew Maurer             }
82*5e40b591SMatthew Maurer         });
83*5e40b591SMatthew Maurer         try_pin_init! {
84*5e40b591SMatthew Maurer             File {
85*5e40b591SMatthew Maurer                 scope <- scope
86*5e40b591SMatthew Maurer             } ? E
87*5e40b591SMatthew Maurer         }
88*5e40b591SMatthew Maurer     }
89*5e40b591SMatthew Maurer 
907f201ca1SMatthew Maurer     /// Create a new directory in DebugFS at the root.
917f201ca1SMatthew Maurer     ///
927f201ca1SMatthew Maurer     /// # Examples
937f201ca1SMatthew Maurer     ///
947f201ca1SMatthew Maurer     /// ```
957f201ca1SMatthew Maurer     /// # use kernel::c_str;
967f201ca1SMatthew Maurer     /// # use kernel::debugfs::Dir;
977f201ca1SMatthew Maurer     /// let debugfs = Dir::new(c_str!("parent"));
987f201ca1SMatthew Maurer     /// ```
997f201ca1SMatthew Maurer     pub fn new(name: &CStr) -> Self {
1007f201ca1SMatthew Maurer         Dir::create(name, None)
1017f201ca1SMatthew Maurer     }
1027f201ca1SMatthew Maurer 
1037f201ca1SMatthew Maurer     /// Creates a subdirectory within this directory.
1047f201ca1SMatthew Maurer     ///
1057f201ca1SMatthew Maurer     /// # Examples
1067f201ca1SMatthew Maurer     ///
1077f201ca1SMatthew Maurer     /// ```
1087f201ca1SMatthew Maurer     /// # use kernel::c_str;
1097f201ca1SMatthew Maurer     /// # use kernel::debugfs::Dir;
1107f201ca1SMatthew Maurer     /// let parent = Dir::new(c_str!("parent"));
1117f201ca1SMatthew Maurer     /// let child = parent.subdir(c_str!("child"));
1127f201ca1SMatthew Maurer     /// ```
1137f201ca1SMatthew Maurer     pub fn subdir(&self, name: &CStr) -> Self {
1147f201ca1SMatthew Maurer         Dir::create(name, Some(self))
1157f201ca1SMatthew Maurer     }
116*5e40b591SMatthew Maurer 
117*5e40b591SMatthew Maurer     /// Creates a read-only file in this directory.
118*5e40b591SMatthew Maurer     ///
119*5e40b591SMatthew Maurer     /// The file's contents are produced by invoking [`Writer::write`] on the value initialized by
120*5e40b591SMatthew Maurer     /// `data`.
121*5e40b591SMatthew Maurer     ///
122*5e40b591SMatthew Maurer     /// # Examples
123*5e40b591SMatthew Maurer     ///
124*5e40b591SMatthew Maurer     /// ```
125*5e40b591SMatthew Maurer     /// # use kernel::c_str;
126*5e40b591SMatthew Maurer     /// # use kernel::debugfs::Dir;
127*5e40b591SMatthew Maurer     /// # use kernel::prelude::*;
128*5e40b591SMatthew Maurer     /// # let dir = Dir::new(c_str!("my_debugfs_dir"));
129*5e40b591SMatthew Maurer     /// let file = KBox::pin_init(dir.read_only_file(c_str!("foo"), 200), GFP_KERNEL)?;
130*5e40b591SMatthew Maurer     /// // "my_debugfs_dir/foo" now contains the number 200.
131*5e40b591SMatthew Maurer     /// // The file is removed when `file` is dropped.
132*5e40b591SMatthew Maurer     /// # Ok::<(), Error>(())
133*5e40b591SMatthew Maurer     /// ```
134*5e40b591SMatthew Maurer     pub fn read_only_file<'a, T, E: 'a>(
135*5e40b591SMatthew Maurer         &'a self,
136*5e40b591SMatthew Maurer         name: &'a CStr,
137*5e40b591SMatthew Maurer         data: impl PinInit<T, E> + 'a,
138*5e40b591SMatthew Maurer     ) -> impl PinInit<File<T>, E> + 'a
139*5e40b591SMatthew Maurer     where
140*5e40b591SMatthew Maurer         T: Writer + Send + Sync + 'static,
141*5e40b591SMatthew Maurer     {
142*5e40b591SMatthew Maurer         let file_ops = &<T as ReadFile<_>>::FILE_OPS;
143*5e40b591SMatthew Maurer         self.create_file(name, data, file_ops)
144*5e40b591SMatthew Maurer     }
145*5e40b591SMatthew Maurer }
146*5e40b591SMatthew Maurer 
147*5e40b591SMatthew Maurer #[pin_data]
148*5e40b591SMatthew Maurer /// Handle to a DebugFS scope, which ensures that attached `data` will outlive the provided
149*5e40b591SMatthew Maurer /// [`Entry`] without moving.
150*5e40b591SMatthew Maurer /// Currently, this is used to back [`File`] so that its `read` and/or `write` implementations
151*5e40b591SMatthew Maurer /// can assume that their backing data is still alive.
152*5e40b591SMatthew Maurer struct Scope<T> {
153*5e40b591SMatthew Maurer     // This order is load-bearing for drops - `_entry` must be dropped before `data`.
154*5e40b591SMatthew Maurer     #[cfg(CONFIG_DEBUG_FS)]
155*5e40b591SMatthew Maurer     _entry: Entry,
156*5e40b591SMatthew Maurer     #[pin]
157*5e40b591SMatthew Maurer     data: T,
158*5e40b591SMatthew Maurer     // Even if `T` is `Unpin`, we still can't allow it to be moved.
159*5e40b591SMatthew Maurer     #[pin]
160*5e40b591SMatthew Maurer     _pin: PhantomPinned,
161*5e40b591SMatthew Maurer }
162*5e40b591SMatthew Maurer 
163*5e40b591SMatthew Maurer #[pin_data]
164*5e40b591SMatthew Maurer /// Handle to a DebugFS file, owning its backing data.
165*5e40b591SMatthew Maurer ///
166*5e40b591SMatthew Maurer /// When dropped, the DebugFS file will be removed and the attached data will be dropped.
167*5e40b591SMatthew Maurer pub struct File<T> {
168*5e40b591SMatthew Maurer     #[pin]
169*5e40b591SMatthew Maurer     scope: Scope<T>,
170*5e40b591SMatthew Maurer }
171*5e40b591SMatthew Maurer 
172*5e40b591SMatthew Maurer #[cfg(not(CONFIG_DEBUG_FS))]
173*5e40b591SMatthew Maurer impl<'b, T: 'b> Scope<T> {
174*5e40b591SMatthew Maurer     fn new<E: 'b, F>(data: impl PinInit<T, E> + 'b, init: F) -> impl PinInit<Self, E> + 'b
175*5e40b591SMatthew Maurer     where
176*5e40b591SMatthew Maurer         F: for<'a> FnOnce(&'a T) + 'b,
177*5e40b591SMatthew Maurer     {
178*5e40b591SMatthew Maurer         try_pin_init! {
179*5e40b591SMatthew Maurer             Self {
180*5e40b591SMatthew Maurer                 data <- data,
181*5e40b591SMatthew Maurer                 _pin: PhantomPinned
182*5e40b591SMatthew Maurer             } ? E
183*5e40b591SMatthew Maurer         }
184*5e40b591SMatthew Maurer         .pin_chain(|scope| {
185*5e40b591SMatthew Maurer             init(&scope.data);
186*5e40b591SMatthew Maurer             Ok(())
187*5e40b591SMatthew Maurer         })
188*5e40b591SMatthew Maurer     }
189*5e40b591SMatthew Maurer }
190*5e40b591SMatthew Maurer 
191*5e40b591SMatthew Maurer #[cfg(CONFIG_DEBUG_FS)]
192*5e40b591SMatthew Maurer impl<'b, T: 'b> Scope<T> {
193*5e40b591SMatthew Maurer     fn entry_mut(self: Pin<&mut Self>) -> &mut Entry {
194*5e40b591SMatthew Maurer         // SAFETY: _entry is not structurally pinned.
195*5e40b591SMatthew Maurer         unsafe { &mut Pin::into_inner_unchecked(self)._entry }
196*5e40b591SMatthew Maurer     }
197*5e40b591SMatthew Maurer 
198*5e40b591SMatthew Maurer     fn new<E: 'b, F>(data: impl PinInit<T, E> + 'b, init: F) -> impl PinInit<Self, E> + 'b
199*5e40b591SMatthew Maurer     where
200*5e40b591SMatthew Maurer         F: for<'a> FnOnce(&'a T) -> Entry + 'b,
201*5e40b591SMatthew Maurer     {
202*5e40b591SMatthew Maurer         try_pin_init! {
203*5e40b591SMatthew Maurer             Self {
204*5e40b591SMatthew Maurer                 _entry: Entry::empty(),
205*5e40b591SMatthew Maurer                 data <- data,
206*5e40b591SMatthew Maurer                 _pin: PhantomPinned
207*5e40b591SMatthew Maurer             } ? E
208*5e40b591SMatthew Maurer         }
209*5e40b591SMatthew Maurer         .pin_chain(|scope| {
210*5e40b591SMatthew Maurer             *scope.entry_mut() = init(&scope.data);
211*5e40b591SMatthew Maurer             Ok(())
212*5e40b591SMatthew Maurer         })
213*5e40b591SMatthew Maurer     }
214*5e40b591SMatthew Maurer }
215*5e40b591SMatthew Maurer 
216*5e40b591SMatthew Maurer impl<T> Deref for Scope<T> {
217*5e40b591SMatthew Maurer     type Target = T;
218*5e40b591SMatthew Maurer     fn deref(&self) -> &T {
219*5e40b591SMatthew Maurer         &self.data
220*5e40b591SMatthew Maurer     }
221*5e40b591SMatthew Maurer }
222*5e40b591SMatthew Maurer 
223*5e40b591SMatthew Maurer impl<T> Deref for File<T> {
224*5e40b591SMatthew Maurer     type Target = T;
225*5e40b591SMatthew Maurer     fn deref(&self) -> &T {
226*5e40b591SMatthew Maurer         &self.scope
227*5e40b591SMatthew Maurer     }
2287f201ca1SMatthew Maurer }
229