| 1 | |
| 2 | |
| 3 |
|
| 4 | |
| 5 | |
| 6 | |
| 7 |
|
| 8 | |
| 9 | #![cfg_attr(not(CONFIG_DEBUG_FS), allow(unused_variables))]
|
| 10 |
|
| 11 | use crate::prelude::*;
|
| 12 | use crate::str::CStr;
|
| 13 | #[cfg(CONFIG_DEBUG_FS)]
|
| 14 | use crate::sync::Arc;
|
| 15 | use crate::uaccess::UserSliceReader;
|
| 16 | use core::fmt;
|
| 17 | use core::marker::PhantomData;
|
| 18 | use core::marker::PhantomPinned;
|
| 19 | #[cfg(CONFIG_DEBUG_FS)]
|
| 20 | use core::mem::ManuallyDrop;
|
| 21 | use core::ops::Deref;
|
| 22 |
|
| 23 | mod traits;
|
| 24 | pub use traits::{Reader, Writer};
|
| 25 |
|
| 26 | mod callback_adapters;
|
| 27 | use callback_adapters::{FormatAdapter, NoWriter, WritableAdapter};
|
| 28 | mod file_ops;
|
| 29 | use file_ops::{FileOps, ReadFile, ReadWriteFile, WriteFile};
|
| 30 | #[cfg(CONFIG_DEBUG_FS)]
|
| 31 | mod entry;
|
| 32 | #[cfg(CONFIG_DEBUG_FS)]
|
| 33 | use entry::Entry;
|
| 34 |
|
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | #[derive(Clone)]
|
| 46 | pub struct Dir(#[cfg(CONFIG_DEBUG_FS)] Option<Arc<Entry<'static>>>);
|
| 47 |
|
| 48 | impl Dir {
|
| 49 | |
| 50 | fn create(name: &CStr, parent: Option<&Dir>) -> Self {
|
| 51 | #[cfg(CONFIG_DEBUG_FS)]
|
| 52 | {
|
| 53 | let parent_entry = match parent {
|
| 54 | |
| 55 | Some(Dir(None)) => return Self(None),
|
| 56 | Some(Dir(Some(entry))) => Some(entry.clone()),
|
| 57 | None => None,
|
| 58 | };
|
| 59 | Self(
|
| 60 | |
| 61 | |
| 62 | Arc::new(Entry::dynamic_dir(name, parent_entry), GFP_KERNEL).ok(),
|
| 63 | )
|
| 64 | }
|
| 65 | #[cfg(not(CONFIG_DEBUG_FS))]
|
| 66 | Self()
|
| 67 | }
|
| 68 |
|
| 69 | |
| 70 | |
| 71 | fn create_file<'a, T, E: 'a>(
|
| 72 | &'a self,
|
| 73 | name: &'a CStr,
|
| 74 | data: impl PinInit<T, E> + 'a,
|
| 75 | file_ops: &'static FileOps<T>,
|
| 76 | ) -> impl PinInit<File<T>, E> + 'a
|
| 77 | where
|
| 78 | T: Sync + 'static,
|
| 79 | {
|
| 80 | let scope = Scope::<T>::new(data, move |data| {
|
| 81 | #[cfg(CONFIG_DEBUG_FS)]
|
| 82 | if let Some(parent) = &self.0 {
|
| 83 | |
| 84 | |
| 85 | unsafe { Entry::dynamic_file(name, parent.clone(), data, file_ops) }
|
| 86 | } else {
|
| 87 | Entry::empty()
|
| 88 | }
|
| 89 | });
|
| 90 | try_pin_init! {
|
| 91 | File {
|
| 92 | scope <- scope
|
| 93 | } ? E
|
| 94 | }
|
| 95 | }
|
| 96 |
|
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | pub fn new(name: &CStr) -> Self {
|
| 107 | Dir::create(name, None)
|
| 108 | }
|
| 109 |
|
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | pub fn subdir(&self, name: &CStr) -> Self {
|
| 121 | Dir::create(name, Some(self))
|
| 122 | }
|
| 123 |
|
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | pub fn read_only_file<'a, T, E: 'a>(
|
| 142 | &'a self,
|
| 143 | name: &'a CStr,
|
| 144 | data: impl PinInit<T, E> + 'a,
|
| 145 | ) -> impl PinInit<File<T>, E> + 'a
|
| 146 | where
|
| 147 | T: Writer + Send + Sync + 'static,
|
| 148 | {
|
| 149 | let file_ops = &<T as ReadFile<_>>::FILE_OPS;
|
| 150 | self.create_file(name, data, file_ops)
|
| 151 | }
|
| 152 |
|
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | pub fn read_callback_file<'a, T, E: 'a, F>(
|
| 180 | &'a self,
|
| 181 | name: &'a CStr,
|
| 182 | data: impl PinInit<T, E> + 'a,
|
| 183 | _f: &'static F,
|
| 184 | ) -> impl PinInit<File<T>, E> + 'a
|
| 185 | where
|
| 186 | T: Send + Sync + 'static,
|
| 187 | F: Fn(&T, &mut fmt::Formatter<'_>) -> fmt::Result + Send + Sync,
|
| 188 | {
|
| 189 | let file_ops = <FormatAdapter<T, F>>::FILE_OPS.adapt();
|
| 190 | self.create_file(name, data, file_ops)
|
| 191 | }
|
| 192 |
|
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | pub fn read_write_file<'a, T, E: 'a>(
|
| 198 | &'a self,
|
| 199 | name: &'a CStr,
|
| 200 | data: impl PinInit<T, E> + 'a,
|
| 201 | ) -> impl PinInit<File<T>, E> + 'a
|
| 202 | where
|
| 203 | T: Writer + Reader + Send + Sync + 'static,
|
| 204 | {
|
| 205 | let file_ops = &<T as ReadWriteFile<_>>::FILE_OPS;
|
| 206 | self.create_file(name, data, file_ops)
|
| 207 | }
|
| 208 |
|
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | pub fn read_write_callback_file<'a, T, E: 'a, F, W>(
|
| 216 | &'a self,
|
| 217 | name: &'a CStr,
|
| 218 | data: impl PinInit<T, E> + 'a,
|
| 219 | _f: &'static F,
|
| 220 | _w: &'static W,
|
| 221 | ) -> impl PinInit<File<T>, E> + 'a
|
| 222 | where
|
| 223 | T: Send + Sync + 'static,
|
| 224 | F: Fn(&T, &mut fmt::Formatter<'_>) -> fmt::Result + Send + Sync,
|
| 225 | W: Fn(&T, &mut UserSliceReader) -> Result + Send + Sync,
|
| 226 | {
|
| 227 | let file_ops =
|
| 228 | <WritableAdapter<FormatAdapter<T, F>, W> as file_ops::ReadWriteFile<_>>::FILE_OPS
|
| 229 | .adapt()
|
| 230 | .adapt();
|
| 231 | self.create_file(name, data, file_ops)
|
| 232 | }
|
| 233 |
|
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | pub fn write_only_file<'a, T, E: 'a>(
|
| 241 | &'a self,
|
| 242 | name: &'a CStr,
|
| 243 | data: impl PinInit<T, E> + 'a,
|
| 244 | ) -> impl PinInit<File<T>, E> + 'a
|
| 245 | where
|
| 246 | T: Reader + Send + Sync + 'static,
|
| 247 | {
|
| 248 | self.create_file(name, data, &T::FILE_OPS)
|
| 249 | }
|
| 250 |
|
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | pub fn write_callback_file<'a, T, E: 'a, W>(
|
| 256 | &'a self,
|
| 257 | name: &'a CStr,
|
| 258 | data: impl PinInit<T, E> + 'a,
|
| 259 | _w: &'static W,
|
| 260 | ) -> impl PinInit<File<T>, E> + 'a
|
| 261 | where
|
| 262 | T: Send + Sync + 'static,
|
| 263 | W: Fn(&T, &mut UserSliceReader) -> Result + Send + Sync,
|
| 264 | {
|
| 265 | let file_ops = <WritableAdapter<NoWriter<T>, W> as WriteFile<_>>::FILE_OPS
|
| 266 | .adapt()
|
| 267 | .adapt();
|
| 268 | self.create_file(name, data, file_ops)
|
| 269 | }
|
| 270 |
|
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | fn scoped_dir<'data>(&self, name: &CStr) -> ScopedDir<'data, 'static> {
|
| 277 | #[cfg(CONFIG_DEBUG_FS)]
|
| 278 | {
|
| 279 | let parent_entry = match &self.0 {
|
| 280 | None => return ScopedDir::empty(),
|
| 281 | Some(entry) => entry.clone(),
|
| 282 | };
|
| 283 | ScopedDir {
|
| 284 | entry: ManuallyDrop::new(Entry::dynamic_dir(name, Some(parent_entry))),
|
| 285 | _phantom: PhantomData,
|
| 286 | }
|
| 287 | }
|
| 288 | #[cfg(not(CONFIG_DEBUG_FS))]
|
| 289 | ScopedDir::empty()
|
| 290 | }
|
| 291 |
|
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | pub fn scope<'a, T: 'a, E: 'a, F>(
|
| 301 | &'a self,
|
| 302 | data: impl PinInit<T, E> + 'a,
|
| 303 | name: &'a CStr,
|
| 304 | init: F,
|
| 305 | ) -> impl PinInit<Scope<T>, E> + 'a
|
| 306 | where
|
| 307 | F: for<'data, 'dir> FnOnce(&'data T, &'dir ScopedDir<'data, 'dir>) + 'a,
|
| 308 | {
|
| 309 | Scope::new(data, |data| {
|
| 310 | let scoped = self.scoped_dir(name);
|
| 311 | init(data, &scoped);
|
| 312 | scoped.into_entry()
|
| 313 | })
|
| 314 | }
|
| 315 | }
|
| 316 |
|
| 317 | #[pin_data]
|
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | pub struct Scope<T> {
|
| 328 | |
| 329 | #[cfg(CONFIG_DEBUG_FS)]
|
| 330 | _entry: Entry<'static>,
|
| 331 | #[pin]
|
| 332 | data: T,
|
| 333 | |
| 334 | #[pin]
|
| 335 | _pin: PhantomPinned,
|
| 336 | }
|
| 337 |
|
| 338 | #[pin_data]
|
| 339 | |
| 340 | |
| 341 | |
| 342 | pub struct File<T> {
|
| 343 | #[pin]
|
| 344 | scope: Scope<T>,
|
| 345 | }
|
| 346 |
|
| 347 | #[cfg(not(CONFIG_DEBUG_FS))]
|
| 348 | impl<'b, T: 'b> Scope<T> {
|
| 349 | fn new<E: 'b, F>(data: impl PinInit<T, E> + 'b, init: F) -> impl PinInit<Self, E> + 'b
|
| 350 | where
|
| 351 | F: for<'a> FnOnce(&'a T) + 'b,
|
| 352 | {
|
| 353 | try_pin_init! {
|
| 354 | Self {
|
| 355 | data <- data,
|
| 356 | _pin: PhantomPinned
|
| 357 | } ? E
|
| 358 | }
|
| 359 | .pin_chain(|scope| {
|
| 360 | init(&scope.data);
|
| 361 | Ok(())
|
| 362 | })
|
| 363 | }
|
| 364 | }
|
| 365 |
|
| 366 | #[cfg(CONFIG_DEBUG_FS)]
|
| 367 | impl<'b, T: 'b> Scope<T> {
|
| 368 | fn entry_mut(self: Pin<&mut Self>) -> &mut Entry<'static> {
|
| 369 | |
| 370 | unsafe { &mut Pin::into_inner_unchecked(self)._entry }
|
| 371 | }
|
| 372 |
|
| 373 | fn new<E: 'b, F>(data: impl PinInit<T, E> + 'b, init: F) -> impl PinInit<Self, E> + 'b
|
| 374 | where
|
| 375 | F: for<'a> FnOnce(&'a T) -> Entry<'static> + 'b,
|
| 376 | {
|
| 377 | try_pin_init! {
|
| 378 | Self {
|
| 379 | _entry: Entry::empty(),
|
| 380 | data <- data,
|
| 381 | _pin: PhantomPinned
|
| 382 | } ? E
|
| 383 | }
|
| 384 | .pin_chain(|scope| {
|
| 385 | *scope.entry_mut() = init(&scope.data);
|
| 386 | Ok(())
|
| 387 | })
|
| 388 | }
|
| 389 | }
|
| 390 |
|
| 391 | impl<'a, T: 'a> Scope<T> {
|
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | pub fn dir<E: 'a, F>(
|
| 401 | data: impl PinInit<T, E> + 'a,
|
| 402 | name: &'a CStr,
|
| 403 | init: F,
|
| 404 | ) -> impl PinInit<Self, E> + 'a
|
| 405 | where
|
| 406 | F: for<'data, 'dir> FnOnce(&'data T, &'dir ScopedDir<'data, 'dir>) + 'a,
|
| 407 | {
|
| 408 | Scope::new(data, |data| {
|
| 409 | let scoped = ScopedDir::new(name);
|
| 410 | init(data, &scoped);
|
| 411 | scoped.into_entry()
|
| 412 | })
|
| 413 | }
|
| 414 | }
|
| 415 |
|
| 416 | impl<T> Deref for Scope<T> {
|
| 417 | type Target = T;
|
| 418 | fn deref(&self) -> &T {
|
| 419 | &self.data
|
| 420 | }
|
| 421 | }
|
| 422 |
|
| 423 | impl<T> Deref for File<T> {
|
| 424 | type Target = T;
|
| 425 | fn deref(&self) -> &T {
|
| 426 | &self.scope
|
| 427 | }
|
| 428 | }
|
| 429 |
|
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | pub struct ScopedDir<'data, 'dir> {
|
| 436 | #[cfg(CONFIG_DEBUG_FS)]
|
| 437 | entry: ManuallyDrop<Entry<'dir>>,
|
| 438 | _phantom: PhantomData<fn(&'data ()) -> &'dir ()>,
|
| 439 | }
|
| 440 |
|
| 441 | impl<'data, 'dir> ScopedDir<'data, 'dir> {
|
| 442 | |
| 443 | |
| 444 | |
| 445 | pub fn dir<'dir2>(&'dir2 self, name: &CStr) -> ScopedDir<'data, 'dir2> {
|
| 446 | #[cfg(not(CONFIG_DEBUG_FS))]
|
| 447 | let _ = name;
|
| 448 | ScopedDir {
|
| 449 | #[cfg(CONFIG_DEBUG_FS)]
|
| 450 | entry: ManuallyDrop::new(Entry::dir(name, Some(&*self.entry))),
|
| 451 | _phantom: PhantomData,
|
| 452 | }
|
| 453 | }
|
| 454 |
|
| 455 | fn create_file<T: Sync>(&self, name: &CStr, data: &'data T, vtable: &'static FileOps<T>) {
|
| 456 | #[cfg(CONFIG_DEBUG_FS)]
|
| 457 | core::mem::forget(Entry::file(name, &self.entry, data, vtable));
|
| 458 | }
|
| 459 |
|
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | pub fn read_only_file<T: Writer + Send + Sync + 'static>(&self, name: &CStr, data: &'data T) {
|
| 468 | self.create_file(name, data, &T::FILE_OPS)
|
| 469 | }
|
| 470 |
|
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | pub fn read_callback_file<T, F>(&self, name: &CStr, data: &'data T, _f: &'static F)
|
| 483 | where
|
| 484 | T: Send + Sync + 'static,
|
| 485 | F: Fn(&T, &mut fmt::Formatter<'_>) -> fmt::Result + Send + Sync,
|
| 486 | {
|
| 487 | let vtable = <FormatAdapter<T, F> as ReadFile<_>>::FILE_OPS.adapt();
|
| 488 | self.create_file(name, data, vtable)
|
| 489 | }
|
| 490 |
|
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | pub fn read_write_file<T: Writer + Reader + Send + Sync + 'static>(
|
| 500 | &self,
|
| 501 | name: &CStr,
|
| 502 | data: &'data T,
|
| 503 | ) {
|
| 504 | let vtable = &<T as ReadWriteFile<_>>::FILE_OPS;
|
| 505 | self.create_file(name, data, vtable)
|
| 506 | }
|
| 507 |
|
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | pub fn read_write_callback_file<T, F, W>(
|
| 519 | &self,
|
| 520 | name: &CStr,
|
| 521 | data: &'data T,
|
| 522 | _f: &'static F,
|
| 523 | _w: &'static W,
|
| 524 | ) where
|
| 525 | T: Send + Sync + 'static,
|
| 526 | F: Fn(&T, &mut fmt::Formatter<'_>) -> fmt::Result + Send + Sync,
|
| 527 | W: Fn(&T, &mut UserSliceReader) -> Result + Send + Sync,
|
| 528 | {
|
| 529 | let vtable = <WritableAdapter<FormatAdapter<T, F>, W> as ReadWriteFile<_>>::FILE_OPS
|
| 530 | .adapt()
|
| 531 | .adapt();
|
| 532 | self.create_file(name, data, vtable)
|
| 533 | }
|
| 534 |
|
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | pub fn write_only_file<T: Reader + Send + Sync + 'static>(&self, name: &CStr, data: &'data T) {
|
| 543 | let vtable = &<T as WriteFile<_>>::FILE_OPS;
|
| 544 | self.create_file(name, data, vtable)
|
| 545 | }
|
| 546 |
|
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
| 557 | pub fn write_only_callback_file<T, W>(&self, name: &CStr, data: &'data T, _w: &'static W)
|
| 558 | where
|
| 559 | T: Send + Sync + 'static,
|
| 560 | W: Fn(&T, &mut UserSliceReader) -> Result + Send + Sync,
|
| 561 | {
|
| 562 | let vtable = &<WritableAdapter<NoWriter<T>, W> as WriteFile<_>>::FILE_OPS
|
| 563 | .adapt()
|
| 564 | .adapt();
|
| 565 | self.create_file(name, data, vtable)
|
| 566 | }
|
| 567 |
|
| 568 | fn empty() -> Self {
|
| 569 | ScopedDir {
|
| 570 | #[cfg(CONFIG_DEBUG_FS)]
|
| 571 | entry: ManuallyDrop::new(Entry::empty()),
|
| 572 | _phantom: PhantomData,
|
| 573 | }
|
| 574 | }
|
| 575 | #[cfg(CONFIG_DEBUG_FS)]
|
| 576 | fn into_entry(self) -> Entry<'dir> {
|
| 577 | ManuallyDrop::into_inner(self.entry)
|
| 578 | }
|
| 579 | #[cfg(not(CONFIG_DEBUG_FS))]
|
| 580 | fn into_entry(self) {}
|
| 581 | }
|
| 582 |
|
| 583 | impl<'data> ScopedDir<'data, 'static> {
|
| 584 | |
| 585 | |
| 586 | |
| 587 | fn new(name: &CStr) -> ScopedDir<'data, 'static> {
|
| 588 | ScopedDir {
|
| 589 | #[cfg(CONFIG_DEBUG_FS)]
|
| 590 | entry: ManuallyDrop::new(Entry::dir(name, None)),
|
| 591 | _phantom: PhantomData,
|
| 592 | }
|
| 593 | }
|
| 594 | }
|