| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 |
|
| 112 | use crate::alloc::flags;
|
| 113 | use crate::container_of;
|
| 114 | use crate::page::PAGE_SIZE;
|
| 115 | use crate::prelude::*;
|
| 116 | use crate::str::CString;
|
| 117 | use crate::sync::Arc;
|
| 118 | use crate::sync::ArcBorrow;
|
| 119 | use crate::types::Opaque;
|
| 120 | use core::cell::UnsafeCell;
|
| 121 | use core::marker::PhantomData;
|
| 122 |
|
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | #[pin_data(PinnedDrop)]
|
| 128 | pub struct Subsystem<Data> {
|
| 129 | #[pin]
|
| 130 | subsystem: Opaque<bindings::configfs_subsystem>,
|
| 131 | #[pin]
|
| 132 | data: Data,
|
| 133 | }
|
| 134 |
|
| 135 | |
| 136 | unsafe impl<Data> Sync for Subsystem<Data> {}
|
| 137 |
|
| 138 | |
| 139 | |
| 140 | unsafe impl<Data: Send> Send for Subsystem<Data> {}
|
| 141 |
|
| 142 | impl<Data> Subsystem<Data> {
|
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | pub fn new(
|
| 149 | name: &'static CStr,
|
| 150 | item_type: &'static ItemType<Subsystem<Data>, Data>,
|
| 151 | data: impl PinInit<Data, Error>,
|
| 152 | ) -> impl PinInit<Self, Error> {
|
| 153 | try_pin_init!(Self {
|
| 154 | subsystem <- pin_init::init_zeroed().chain(
|
| 155 | |place: &mut Opaque<bindings::configfs_subsystem>| {
|
| 156 | |
| 157 | unsafe {
|
| 158 | bindings::config_group_init_type_name(
|
| 159 | &mut (*place.get()).su_group,
|
| 160 | name.as_char_ptr(),
|
| 161 | item_type.as_ptr(),
|
| 162 | )
|
| 163 | };
|
| 164 |
|
| 165 | |
| 166 | unsafe {
|
| 167 | bindings::__mutex_init(
|
| 168 | &mut (*place.get()).su_mutex,
|
| 169 | kernel::optional_name!().as_char_ptr(),
|
| 170 | kernel::static_lock_class!().as_ptr(),
|
| 171 | )
|
| 172 | }
|
| 173 | Ok(())
|
| 174 | }
|
| 175 | ),
|
| 176 | data <- data,
|
| 177 | })
|
| 178 | .pin_chain(|this| {
|
| 179 | crate::error::to_result(
|
| 180 | |
| 181 | unsafe { bindings::configfs_register_subsystem(this.subsystem.get()) },
|
| 182 | )
|
| 183 | })
|
| 184 | }
|
| 185 | }
|
| 186 |
|
| 187 | #[pinned_drop]
|
| 188 | impl<Data> PinnedDrop for Subsystem<Data> {
|
| 189 | fn drop(self: Pin<&mut Self>) {
|
| 190 | |
| 191 | unsafe { bindings::configfs_unregister_subsystem(self.subsystem.get()) };
|
| 192 | |
| 193 | unsafe { bindings::mutex_destroy(&raw mut (*self.subsystem.get()).su_mutex) };
|
| 194 | }
|
| 195 | }
|
| 196 |
|
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | pub unsafe trait HasGroup<Data> {
|
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | unsafe fn group(this: *const Self) -> *const bindings::config_group;
|
| 213 |
|
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | unsafe fn container_of(group: *const bindings::config_group) -> *const Self;
|
| 221 | }
|
| 222 |
|
| 223 | |
| 224 | |
| 225 | unsafe impl<Data> HasGroup<Data> for Subsystem<Data> {
|
| 226 | unsafe fn group(this: *const Self) -> *const bindings::config_group {
|
| 227 | |
| 228 | unsafe { &raw const (*(*this).subsystem.get()).su_group }
|
| 229 | }
|
| 230 |
|
| 231 | unsafe fn container_of(group: *const bindings::config_group) -> *const Self {
|
| 232 | |
| 233 | let c_subsys_ptr = unsafe { container_of!(group, bindings::configfs_subsystem, su_group) };
|
| 234 | let opaque_ptr = c_subsys_ptr.cast::<Opaque<bindings::configfs_subsystem>>();
|
| 235 | |
| 236 | |
| 237 | unsafe { container_of!(opaque_ptr, Subsystem<Data>, subsystem) }
|
| 238 | }
|
| 239 | }
|
| 240 |
|
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | #[pin_data]
|
| 246 | pub struct Group<Data> {
|
| 247 | #[pin]
|
| 248 | group: Opaque<bindings::config_group>,
|
| 249 | #[pin]
|
| 250 | data: Data,
|
| 251 | }
|
| 252 |
|
| 253 | |
| 254 | unsafe impl<Data> Sync for Group<Data> {}
|
| 255 |
|
| 256 | |
| 257 | |
| 258 | unsafe impl<Data: Send> Send for Group<Data> {}
|
| 259 |
|
| 260 | impl<Data> Group<Data> {
|
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | pub fn new(
|
| 266 | name: CString,
|
| 267 | item_type: &'static ItemType<Group<Data>, Data>,
|
| 268 | data: impl PinInit<Data, Error>,
|
| 269 | ) -> impl PinInit<Self, Error> {
|
| 270 | try_pin_init!(Self {
|
| 271 | group <- pin_init::init_zeroed().chain(|v: &mut Opaque<bindings::config_group>| {
|
| 272 | let place = v.get();
|
| 273 | let name = name.to_bytes_with_nul().as_ptr();
|
| 274 | |
| 275 | unsafe {
|
| 276 | bindings::config_group_init_type_name(place, name.cast(), item_type.as_ptr())
|
| 277 | };
|
| 278 | Ok(())
|
| 279 | }),
|
| 280 | data <- data,
|
| 281 | })
|
| 282 | }
|
| 283 | }
|
| 284 |
|
| 285 | |
| 286 | |
| 287 | unsafe impl<Data> HasGroup<Data> for Group<Data> {
|
| 288 | unsafe fn group(this: *const Self) -> *const bindings::config_group {
|
| 289 | Opaque::cast_into(
|
| 290 | |
| 291 | |
| 292 | unsafe { &raw const (*this).group },
|
| 293 | )
|
| 294 | }
|
| 295 |
|
| 296 | unsafe fn container_of(group: *const bindings::config_group) -> *const Self {
|
| 297 | let opaque_ptr = group.cast::<Opaque<bindings::config_group>>();
|
| 298 | |
| 299 | |
| 300 | unsafe { container_of!(opaque_ptr, Self, group) }
|
| 301 | }
|
| 302 | }
|
| 303 |
|
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | unsafe fn get_group_data<'a, Parent>(this: *mut bindings::config_group) -> &'a Parent {
|
| 314 | |
| 315 | let subsys = unsafe { (*this).cg_subsys };
|
| 316 |
|
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | let is_root = !subsys.is_null()
|
| 324 | && core::ptr::eq(
|
| 325 | this.cast_const(),
|
| 326 | |
| 327 | |
| 328 | |
| 329 | unsafe { &raw const (*subsys).su_group },
|
| 330 | );
|
| 331 |
|
| 332 | if is_root {
|
| 333 | |
| 334 | |
| 335 | |
| 336 | unsafe { &(*Subsystem::container_of(this)).data }
|
| 337 | } else {
|
| 338 | |
| 339 | |
| 340 | unsafe { &(*Group::<Parent>::container_of(this)).data }
|
| 341 | }
|
| 342 | }
|
| 343 |
|
| 344 | struct GroupOperationsVTable<Parent, Child>(PhantomData<(Parent, Child)>);
|
| 345 |
|
| 346 | impl<Parent, Child> GroupOperationsVTable<Parent, Child>
|
| 347 | where
|
| 348 | Parent: GroupOperations<Child = Child>,
|
| 349 | |
| 350 | |
| 351 | Arc<Group<Child>>: Send,
|
| 352 | {
|
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | unsafe extern "C" fn make_group(
|
| 367 | this: *mut bindings::config_group,
|
| 368 | name: *const kernel::ffi::c_char,
|
| 369 | ) -> *mut bindings::config_group {
|
| 370 | |
| 371 | |
| 372 | |
| 373 | let parent_data = unsafe { get_group_data(this) };
|
| 374 |
|
| 375 | let group_init = match Parent::make_group(
|
| 376 | parent_data,
|
| 377 | |
| 378 | |
| 379 | unsafe { CStr::from_char_ptr(name) },
|
| 380 | ) {
|
| 381 | Ok(init) => init,
|
| 382 | Err(e) => return e.to_ptr(),
|
| 383 | };
|
| 384 |
|
| 385 | let child_group = <Arc<Group<Child>> as InPlaceInit<Group<Child>>>::try_pin_init(
|
| 386 | group_init,
|
| 387 | flags::GFP_KERNEL,
|
| 388 | );
|
| 389 |
|
| 390 | match child_group {
|
| 391 | Ok(child_group) => {
|
| 392 | let child_group_ptr = child_group.into_raw();
|
| 393 | |
| 394 | |
| 395 | unsafe { Group::<Child>::group(child_group_ptr) }.cast_mut()
|
| 396 | }
|
| 397 | Err(e) => e.to_ptr(),
|
| 398 | }
|
| 399 | }
|
| 400 |
|
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | unsafe extern "C" fn drop_item(
|
| 414 | this: *mut bindings::config_group,
|
| 415 | item: *mut bindings::config_item,
|
| 416 | ) {
|
| 417 | |
| 418 | |
| 419 | |
| 420 | let parent_data = unsafe { get_group_data(this) };
|
| 421 |
|
| 422 | |
| 423 | |
| 424 | let c_child_group_ptr = unsafe { container_of!(item, bindings::config_group, cg_item) };
|
| 425 | |
| 426 | |
| 427 | let r_child_group_ptr = unsafe { Group::<Child>::container_of(c_child_group_ptr) };
|
| 428 |
|
| 429 | if Parent::HAS_DROP_ITEM {
|
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | let arc: Arc<Group<Child>> = unsafe { Arc::from_raw(r_child_group_ptr.cast_mut()) };
|
| 435 |
|
| 436 | Parent::drop_item(parent_data, arc.as_arc_borrow());
|
| 437 | arc.into_raw();
|
| 438 | }
|
| 439 |
|
| 440 | |
| 441 | |
| 442 | unsafe { bindings::config_item_put(item) };
|
| 443 | }
|
| 444 |
|
| 445 | const VTABLE: bindings::configfs_group_operations = bindings::configfs_group_operations {
|
| 446 | make_item: None,
|
| 447 | make_group: Some(Self::make_group),
|
| 448 | disconnect_notify: None,
|
| 449 | drop_item: Some(Self::drop_item),
|
| 450 | is_visible: None,
|
| 451 | is_bin_visible: None,
|
| 452 | };
|
| 453 |
|
| 454 | const fn vtable_ptr() -> *const bindings::configfs_group_operations {
|
| 455 | &Self::VTABLE
|
| 456 | }
|
| 457 | }
|
| 458 |
|
| 459 | struct ItemOperationsVTable<Container, Data>(PhantomData<(Container, Data)>);
|
| 460 |
|
| 461 | impl<Data> ItemOperationsVTable<Group<Data>, Data>
|
| 462 | where
|
| 463 | Data: 'static,
|
| 464 | |
| 465 | Arc<Group<Data>>: Send,
|
| 466 | {
|
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | unsafe extern "C" fn release(this: *mut bindings::config_item) {
|
| 475 | |
| 476 | |
| 477 | let c_group_ptr = unsafe { kernel::container_of!(this, bindings::config_group, cg_item) };
|
| 478 | |
| 479 | |
| 480 | let r_group_ptr = unsafe { Group::<Data>::container_of(c_group_ptr) };
|
| 481 |
|
| 482 | |
| 483 | |
| 484 | |
| 485 | let pin_self: Arc<Group<Data>> = unsafe { Arc::from_raw(r_group_ptr.cast_mut()) };
|
| 486 | drop(pin_self);
|
| 487 | }
|
| 488 |
|
| 489 | const VTABLE: bindings::configfs_item_operations = bindings::configfs_item_operations {
|
| 490 | release: Some(Self::release),
|
| 491 | allow_link: None,
|
| 492 | drop_link: None,
|
| 493 | };
|
| 494 |
|
| 495 | const fn vtable_ptr() -> *const bindings::configfs_item_operations {
|
| 496 | &Self::VTABLE
|
| 497 | }
|
| 498 | }
|
| 499 |
|
| 500 | impl<Data> ItemOperationsVTable<Subsystem<Data>, Data> {
|
| 501 | const VTABLE: bindings::configfs_item_operations = bindings::configfs_item_operations {
|
| 502 | release: None,
|
| 503 | allow_link: None,
|
| 504 | drop_link: None,
|
| 505 | };
|
| 506 |
|
| 507 | const fn vtable_ptr() -> *const bindings::configfs_item_operations {
|
| 508 | &Self::VTABLE
|
| 509 | }
|
| 510 | }
|
| 511 |
|
| 512 | |
| 513 | |
| 514 | |
| 515 | #[vtable]
|
| 516 | pub trait GroupOperations: Sync {
|
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | type Child: 'static + Send;
|
| 522 |
|
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 531 | fn make_group(&self, name: &CStr) -> Result<impl PinInit<Group<Self::Child>, Error>>;
|
| 532 |
|
| 533 | |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | fn drop_item(&self, _child: ArcBorrow<'_, Group<Self::Child>>) {
|
| 544 | kernel::build_error!(kernel::error::VTABLE_DEFAULT_ERROR)
|
| 545 | }
|
| 546 | }
|
| 547 |
|
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | #[repr(transparent)]
|
| 553 | pub struct Attribute<const ID: u64, O, Data> {
|
| 554 | attribute: Opaque<bindings::configfs_attribute>,
|
| 555 | _p: PhantomData<(O, Data)>,
|
| 556 | }
|
| 557 |
|
| 558 | |
| 559 | unsafe impl<const ID: u64, O, Data> Sync for Attribute<ID, O, Data> {}
|
| 560 |
|
| 561 | |
| 562 | unsafe impl<const ID: u64, O, Data> Send for Attribute<ID, O, Data> {}
|
| 563 |
|
| 564 | impl<const ID: u64, O, Data> Attribute<ID, O, Data>
|
| 565 | where
|
| 566 | O: AttributeOperations<ID, Data = Data>,
|
| 567 | {
|
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | unsafe extern "C" fn show(
|
| 580 | item: *mut bindings::config_item,
|
| 581 | page: *mut kernel::ffi::c_char,
|
| 582 | ) -> isize {
|
| 583 | let c_group: *mut bindings::config_group =
|
| 584 | |
| 585 | |
| 586 | unsafe { container_of!(item, bindings::config_group, cg_item) };
|
| 587 |
|
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | let data: &Data = unsafe { get_group_data(c_group) };
|
| 593 |
|
| 594 | |
| 595 | let ret = O::show(data, unsafe { &mut *(page.cast::<[u8; PAGE_SIZE]>()) });
|
| 596 |
|
| 597 | match ret {
|
| 598 | Ok(size) => size as isize,
|
| 599 | Err(err) => err.to_errno() as isize,
|
| 600 | }
|
| 601 | }
|
| 602 |
|
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | |
| 614 | unsafe extern "C" fn store(
|
| 615 | item: *mut bindings::config_item,
|
| 616 | page: *const kernel::ffi::c_char,
|
| 617 | size: usize,
|
| 618 | ) -> isize {
|
| 619 | let c_group: *mut bindings::config_group =
|
| 620 | |
| 621 | |
| 622 | unsafe { container_of!(item, bindings::config_group, cg_item) };
|
| 623 |
|
| 624 | |
| 625 | |
| 626 | |
| 627 | |
| 628 | let data: &Data = unsafe { get_group_data(c_group) };
|
| 629 |
|
| 630 | let ret = O::store(
|
| 631 | data,
|
| 632 | |
| 633 | |
| 634 | unsafe { core::slice::from_raw_parts(page.cast(), size) },
|
| 635 | );
|
| 636 |
|
| 637 | match ret {
|
| 638 | Ok(()) => size as isize,
|
| 639 | Err(err) => err.to_errno() as isize,
|
| 640 | }
|
| 641 | }
|
| 642 |
|
| 643 | |
| 644 | |
| 645 | |
| 646 | pub const fn new(name: &'static CStr) -> Self {
|
| 647 | Self {
|
| 648 | attribute: Opaque::new(bindings::configfs_attribute {
|
| 649 | ca_name: crate::str::as_char_ptr_in_const_context(name),
|
| 650 | ca_owner: core::ptr::null_mut(),
|
| 651 | ca_mode: 0o660,
|
| 652 | show: Some(Self::show),
|
| 653 | store: if O::HAS_STORE {
|
| 654 | Some(Self::store)
|
| 655 | } else {
|
| 656 | None
|
| 657 | },
|
| 658 | }),
|
| 659 | _p: PhantomData,
|
| 660 | }
|
| 661 | }
|
| 662 | }
|
| 663 |
|
| 664 | |
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | |
| 671 | |
| 672 | |
| 673 | |
| 674 | |
| 675 | |
| 676 | #[vtable]
|
| 677 | pub trait AttributeOperations<const ID: u64 = 0> {
|
| 678 | |
| 679 | |
| 680 | type Data: Sync;
|
| 681 |
|
| 682 | |
| 683 | |
| 684 | |
| 685 | |
| 686 | |
| 687 | |
| 688 | fn show(data: &Self::Data, page: &mut [u8; PAGE_SIZE]) -> Result<usize>;
|
| 689 |
|
| 690 | |
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | fn store(_data: &Self::Data, _page: &[u8]) -> Result {
|
| 697 | kernel::build_error!(kernel::error::VTABLE_DEFAULT_ERROR)
|
| 698 | }
|
| 699 | }
|
| 700 |
|
| 701 | |
| 702 | |
| 703 | |
| 704 | |
| 705 | |
| 706 | |
| 707 | |
| 708 | |
| 709 | |
| 710 | |
| 711 | |
| 712 | |
| 713 | #[repr(transparent)]
|
| 714 | pub struct AttributeList<const N: usize, Data>(
|
| 715 | |
| 716 | |
| 717 | UnsafeCell<[*mut kernel::ffi::c_void; N]>,
|
| 718 | PhantomData<Data>,
|
| 719 | );
|
| 720 |
|
| 721 | |
| 722 | unsafe impl<const N: usize, Data> Send for AttributeList<N, Data> {}
|
| 723 |
|
| 724 | |
| 725 | unsafe impl<const N: usize, Data> Sync for AttributeList<N, Data> {}
|
| 726 |
|
| 727 | impl<const N: usize, Data> AttributeList<N, Data> {
|
| 728 | |
| 729 | |
| 730 | |
| 731 | |
| 732 | #[doc(hidden)]
|
| 733 | pub const unsafe fn new() -> Self {
|
| 734 | Self(UnsafeCell::new([core::ptr::null_mut(); N]), PhantomData)
|
| 735 | }
|
| 736 |
|
| 737 | |
| 738 | |
| 739 | |
| 740 | |
| 741 | #[doc(hidden)]
|
| 742 | pub const unsafe fn add<const I: usize, const ID: u64, O>(
|
| 743 | &'static self,
|
| 744 | attribute: &'static Attribute<ID, O, Data>,
|
| 745 | ) where
|
| 746 | O: AttributeOperations<ID, Data = Data>,
|
| 747 | {
|
| 748 | |
| 749 | const { assert!(I < N - 1, "Invalid attribute index") };
|
| 750 |
|
| 751 | |
| 752 | |
| 753 | unsafe { (&mut *self.0.get())[I] = core::ptr::from_ref(attribute).cast_mut().cast() };
|
| 754 | }
|
| 755 | }
|
| 756 |
|
| 757 | |
| 758 | |
| 759 | |
| 760 | |
| 761 | |
| 762 | |
| 763 | #[pin_data]
|
| 764 | pub struct ItemType<Container, Data> {
|
| 765 | #[pin]
|
| 766 | item_type: Opaque<bindings::config_item_type>,
|
| 767 | _p: PhantomData<(Container, Data)>,
|
| 768 | }
|
| 769 |
|
| 770 | |
| 771 | unsafe impl<Container, Data> Sync for ItemType<Container, Data> {}
|
| 772 |
|
| 773 | |
| 774 | unsafe impl<Container, Data> Send for ItemType<Container, Data> {}
|
| 775 |
|
| 776 | macro_rules! impl_item_type {
|
| 777 | ($tpe:ty) => {
|
| 778 | impl<Data> ItemType<$tpe, Data> {
|
| 779 | #[doc(hidden)]
|
| 780 | pub const fn new_with_child_ctor<const N: usize, Child>(
|
| 781 | owner: &'static ThisModule,
|
| 782 | attributes: &'static AttributeList<N, Data>,
|
| 783 | ) -> Self
|
| 784 | where
|
| 785 | Data: GroupOperations<Child = Child>,
|
| 786 | Arc<Group<Child>>: Send,
|
| 787 | Arc<Group<Data>>: Send,
|
| 788 | {
|
| 789 | Self {
|
| 790 | item_type: Opaque::new(bindings::config_item_type {
|
| 791 | ct_owner: owner.as_ptr(),
|
| 792 | ct_group_ops: GroupOperationsVTable::<Data, Child>::vtable_ptr(),
|
| 793 | ct_item_ops: ItemOperationsVTable::<$tpe, Data>::vtable_ptr(),
|
| 794 | __bindgen_anon_1: bindings::config_item_type__bindgen_ty_1 {
|
| 795 | ct_attrs_const: core::ptr::from_ref(attributes).cast(),
|
| 796 | },
|
| 797 | ct_bin_attrs: core::ptr::null(),
|
| 798 | }),
|
| 799 | _p: PhantomData,
|
| 800 | }
|
| 801 | }
|
| 802 |
|
| 803 | #[doc(hidden)]
|
| 804 | pub const fn new<const N: usize>(
|
| 805 | owner: &'static ThisModule,
|
| 806 | attributes: &'static AttributeList<N, Data>,
|
| 807 | ) -> Self
|
| 808 | where
|
| 809 | Arc<Group<Data>>: Send,
|
| 810 | {
|
| 811 | Self {
|
| 812 | item_type: Opaque::new(bindings::config_item_type {
|
| 813 | ct_owner: owner.as_ptr(),
|
| 814 | ct_group_ops: core::ptr::null(),
|
| 815 | ct_item_ops: ItemOperationsVTable::<$tpe, Data>::vtable_ptr(),
|
| 816 | __bindgen_anon_1: bindings::config_item_type__bindgen_ty_1 {
|
| 817 | ct_attrs_const: core::ptr::from_ref(attributes).cast(),
|
| 818 | },
|
| 819 | ct_bin_attrs: core::ptr::null(),
|
| 820 | }),
|
| 821 | _p: PhantomData,
|
| 822 | }
|
| 823 | }
|
| 824 | }
|
| 825 | };
|
| 826 | }
|
| 827 |
|
| 828 | impl_item_type!(Subsystem<Data>);
|
| 829 | impl_item_type!(Group<Data>);
|
| 830 |
|
| 831 | impl<Container, Data> ItemType<Container, Data> {
|
| 832 | fn as_ptr(&self) -> *const bindings::config_item_type {
|
| 833 | self.item_type.get()
|
| 834 | }
|
| 835 | }
|
| 836 |
|
| 837 | |
| 838 | |
| 839 | |
| 840 | |
| 841 | |
| 842 | |
| 843 | |
| 844 | |
| 845 | |
| 846 | |
| 847 | |
| 848 | |
| 849 | |
| 850 | |
| 851 | |
| 852 | |
| 853 | |
| 854 | |
| 855 | |
| 856 | |
| 857 | |
| 858 | |
| 859 | |
| 860 | |
| 861 | |
| 862 | |
| 863 | |
| 864 | |
| 865 | |
| 866 | |
| 867 | |
| 868 | |
| 869 | |
| 870 | |
| 871 | |
| 872 | |
| 873 | |
| 874 | |
| 875 | |
| 876 | |
| 877 | |
| 878 | |
| 879 | |
| 880 | |
| 881 | |
| 882 | |
| 883 | |
| 884 | |
| 885 | |
| 886 | |
| 887 | |
| 888 | |
| 889 | |
| 890 | |
| 891 | |
| 892 | |
| 893 | |
| 894 | |
| 895 | |
| 896 | |
| 897 | |
| 898 | |
| 899 | |
| 900 | |
| 901 | |
| 902 | |
| 903 | |
| 904 | |
| 905 | |
| 906 | |
| 907 | |
| 908 | |
| 909 | |
| 910 | |
| 911 | |
| 912 | |
| 913 | |
| 914 | |
| 915 | |
| 916 | |
| 917 | |
| 918 | |
| 919 | |
| 920 | |
| 921 | |
| 922 | |
| 923 | |
| 924 | |
| 925 | |
| 926 | |
| 927 | #[allow(clippy::crate_in_macro_def)]
|
| 928 | #[macro_export]
|
| 929 | macro_rules! configfs_attrs {
|
| 930 | (
|
| 931 | container: $container:ty,
|
| 932 | data: $data:ty,
|
| 933 | attributes: [
|
| 934 | $($name:ident: $attr:literal),* $(,)?
|
| 935 | ] $(,)?
|
| 936 | ) => {
|
| 937 | $crate::configfs_attrs!(
|
| 938 | count:
|
| 939 | @container($container),
|
| 940 | @data($data),
|
| 941 | @child(),
|
| 942 | @no_child(x),
|
| 943 | @attrs($($name $attr)*),
|
| 944 | @eat($($name $attr,)*),
|
| 945 | @assign(),
|
| 946 | @cnt(0usize),
|
| 947 | )
|
| 948 | };
|
| 949 | (
|
| 950 | container: $container:ty,
|
| 951 | data: $data:ty,
|
| 952 | child: $child:ty,
|
| 953 | attributes: [
|
| 954 | $($name:ident: $attr:literal),* $(,)?
|
| 955 | ] $(,)?
|
| 956 | ) => {
|
| 957 | $crate::configfs_attrs!(
|
| 958 | count:
|
| 959 | @container($container),
|
| 960 | @data($data),
|
| 961 | @child($child),
|
| 962 | @no_child(),
|
| 963 | @attrs($($name $attr)*),
|
| 964 | @eat($($name $attr,)*),
|
| 965 | @assign(),
|
| 966 | @cnt(0usize),
|
| 967 | )
|
| 968 | };
|
| 969 | (count:
|
| 970 | @container($container:ty),
|
| 971 | @data($data:ty),
|
| 972 | @child($($child:ty)?),
|
| 973 | @no_child($($no_child:ident)?),
|
| 974 | @attrs($($aname:ident $aattr:literal)*),
|
| 975 | @eat($name:ident $attr:literal, $($rname:ident $rattr:literal,)*),
|
| 976 | @assign($($assign:block)*),
|
| 977 | @cnt($cnt:expr),
|
| 978 | ) => {
|
| 979 | $crate::configfs_attrs!(
|
| 980 | count:
|
| 981 | @container($container),
|
| 982 | @data($data),
|
| 983 | @child($($child)?),
|
| 984 | @no_child($($no_child)?),
|
| 985 | @attrs($($aname $aattr)*),
|
| 986 | @eat($($rname $rattr,)*),
|
| 987 | @assign($($assign)* {
|
| 988 | const N: usize = $cnt;
|
| 989 | |
| 990 |
|
| 991 | |
| 992 | |
| 993 | |
| 994 | |
| 995 | |
| 996 | unsafe {
|
| 997 | $crate::macros::paste!(
|
| 998 | [< $data:upper _ATTRS >]
|
| 999 | .add::<N, $attr, _>(&[< $data:upper _ $name:upper _ATTR >])
|
| 1000 | )
|
| 1001 | };
|
| 1002 | }),
|
| 1003 | @cnt(1usize + $cnt),
|
| 1004 | )
|
| 1005 | };
|
| 1006 | (count:
|
| 1007 | @container($container:ty),
|
| 1008 | @data($data:ty),
|
| 1009 | @child($($child:ty)?),
|
| 1010 | @no_child($($no_child:ident)?),
|
| 1011 | @attrs($($aname:ident $aattr:literal)*),
|
| 1012 | @eat(),
|
| 1013 | @assign($($assign:block)*),
|
| 1014 | @cnt($cnt:expr),
|
| 1015 | ) =>
|
| 1016 | {
|
| 1017 | $crate::configfs_attrs!(
|
| 1018 | final:
|
| 1019 | @container($container),
|
| 1020 | @data($data),
|
| 1021 | @child($($child)?),
|
| 1022 | @no_child($($no_child)?),
|
| 1023 | @attrs($($aname $aattr)*),
|
| 1024 | @assign($($assign)*),
|
| 1025 | @cnt($cnt),
|
| 1026 | )
|
| 1027 | };
|
| 1028 | (final:
|
| 1029 | @container($container:ty),
|
| 1030 | @data($data:ty),
|
| 1031 | @child($($child:ty)?),
|
| 1032 | @no_child($($no_child:ident)?),
|
| 1033 | @attrs($($name:ident $attr:literal)*),
|
| 1034 | @assign($($assign:block)*),
|
| 1035 | @cnt($cnt:expr),
|
| 1036 | ) =>
|
| 1037 | {
|
| 1038 | $crate::macros::paste!{
|
| 1039 | {
|
| 1040 | $(
|
| 1041 | |
| 1042 | static [< $data:upper _ $name:upper _ATTR >]:
|
| 1043 | $crate::configfs::Attribute<$attr, $data, $data> =
|
| 1044 | unsafe {
|
| 1045 | $crate::configfs::Attribute::new(
|
| 1046 | $crate::c_str!(::core::stringify!($name)),
|
| 1047 | )
|
| 1048 | };
|
| 1049 | )*
|
| 1050 |
|
| 1051 |
|
| 1052 | |
| 1053 | const N: usize = $cnt + 1usize;
|
| 1054 |
|
| 1055 | |
| 1056 | static [< $data:upper _ATTRS >]:
|
| 1057 | $crate::configfs::AttributeList<N, $data> =
|
| 1058 | unsafe { $crate::configfs::AttributeList::new() };
|
| 1059 |
|
| 1060 | $($assign)*
|
| 1061 |
|
| 1062 | $(
|
| 1063 | const [<$no_child:upper>]: bool = true;
|
| 1064 |
|
| 1065 | static [< $data:upper _TPE >] : $crate::configfs::ItemType<$container, $data> =
|
| 1066 | $crate::configfs::ItemType::<$container, $data>::new::<N>(
|
| 1067 | $crate::module::this_module::<crate::LocalModule>(),
|
| 1068 | &[<$ data:upper _ATTRS >]
|
| 1069 | );
|
| 1070 | )?
|
| 1071 |
|
| 1072 | $(
|
| 1073 | static [< $data:upper _TPE >]:
|
| 1074 | $crate::configfs::ItemType<$container, $data> =
|
| 1075 | $crate::configfs::ItemType::<$container, $data>::
|
| 1076 | new_with_child_ctor::<N, $child>(
|
| 1077 | $crate::module::this_module::<crate::LocalModule>(),
|
| 1078 | &[<$ data:upper _ATTRS >]
|
| 1079 | );
|
| 1080 | )?
|
| 1081 |
|
| 1082 | & [< $data:upper _TPE >]
|
| 1083 | }
|
| 1084 | }
|
| 1085 | };
|
| 1086 |
|
| 1087 | }
|
| 1088 |
|
| 1089 | pub use crate::configfs_attrs;
|