kobako 0.31.0 → 0.32.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (133) hide show
  1. checksums.yaml +4 -4
  2. data/.githooks/linux-tests.sh +29 -0
  3. data/.release-please-manifest.json +1 -1
  4. data/CHANGELOG.md +125 -0
  5. data/Cargo.lock +457 -494
  6. data/README.md +2 -3
  7. data/ROADMAP.md +0 -1
  8. data/crates/kobako-runtime/CHANGELOG.md +90 -0
  9. data/crates/kobako-runtime/Cargo.toml +14 -15
  10. data/crates/kobako-runtime/README.md +1 -1
  11. data/crates/kobako-runtime/src/cred.rs +32 -20
  12. data/crates/kobako-runtime/src/filesystem/bind/fid.rs +300 -22
  13. data/crates/kobako-runtime/src/filesystem/bind/lookup.rs +56 -22
  14. data/crates/kobako-runtime/src/filesystem/bind/unicode.rs +109 -0
  15. data/crates/kobako-runtime/src/filesystem/bind/walk.rs +33 -14
  16. data/crates/kobako-runtime/src/filesystem/bind.rs +393 -220
  17. data/crates/kobako-runtime/src/filesystem/overlay/stack/copy_up.rs +2 -0
  18. data/crates/kobako-runtime/src/filesystem/overlay/stack.rs +9 -10
  19. data/crates/kobako-runtime/src/filesystem/overlay.rs +277 -46
  20. data/crates/kobako-runtime/src/filesystem/rdir.rs +8 -27
  21. data/crates/kobako-runtime/src/filesystem/testing.rs +31 -0
  22. data/crates/kobako-runtime/src/filesystem/tmpfs.rs +47 -13
  23. data/crates/kobako-runtime/src/filesystem.rs +2 -0
  24. data/crates/kobako-runtime/src/fork.rs +239 -16
  25. data/crates/kobako-runtime/src/limits.rs +17 -41
  26. data/crates/kobako-runtime/src/locking/seqlock.rs +29 -3
  27. data/crates/kobako-runtime/src/locking.rs +508 -113
  28. data/crates/kobako-runtime/src/memcg.rs +83 -160
  29. data/crates/kobako-runtime/src/mount.rs +0 -30
  30. data/crates/kobako-runtime/src/net/connections.rs +53 -13
  31. data/crates/kobako-runtime/src/net/egress.rs +44 -24
  32. data/crates/kobako-runtime/src/net/http.rs +888 -456
  33. data/crates/kobako-runtime/src/net/lookup.rs +150 -50
  34. data/crates/kobako-runtime/src/net/namespace.rs +97 -95
  35. data/crates/kobako-runtime/src/net/reactor.rs +298 -203
  36. data/crates/kobako-runtime/src/net/resolv/darwin.rs +8 -3
  37. data/crates/kobako-runtime/src/net/resolv/stub/tests.rs +50 -0
  38. data/crates/kobako-runtime/src/net/resolv/stub.rs +29 -23
  39. data/crates/kobako-runtime/src/net/resolv/windows.rs +14 -44
  40. data/crates/kobako-runtime/src/net/resolv.rs +39 -15
  41. data/crates/kobako-runtime/src/net/skbuff.rs +109 -0
  42. data/crates/kobako-runtime/src/net/socket.rs +51 -52
  43. data/crates/kobako-runtime/src/net/task.rs +223 -0
  44. data/crates/kobako-runtime/src/net/tcp.rs +410 -158
  45. data/crates/kobako-runtime/src/net/tls.rs +65 -3
  46. data/crates/kobako-runtime/src/net/transport.rs +72 -47
  47. data/crates/kobako-runtime/src/net/udp.rs +231 -129
  48. data/crates/kobako-runtime/src/net.rs +3 -0
  49. data/crates/kobako-runtime/src/sched.rs +244 -58
  50. data/crates/kobako-runtime/src/security.rs +27 -16
  51. data/crates/kobako-runtime/src/vfs/dentry.rs +80 -41
  52. data/crates/kobako-runtime/src/vfs/file.rs +52 -53
  53. data/crates/kobako-runtime/src/vfs/kern_path.rs +24 -11
  54. data/crates/kobako-runtime/src/vfs/namei.rs +399 -184
  55. data/crates/kobako-runtime/src/vfs/namespace.rs +123 -40
  56. data/crates/kobako-runtime/src/vfs/stat.rs +105 -16
  57. data/crates/kobako-runtime/src/vfs.rs +1 -0
  58. data/crates/kobako-runtime/src/wait.rs +75 -50
  59. data/crates/kobako-transport/CHANGELOG.md +7 -0
  60. data/crates/kobako-transport/Cargo.toml +1 -1
  61. data/crates/kobako-transport/README.md +1 -1
  62. data/crates/kobako-wasmtime/CHANGELOG.md +47 -0
  63. data/crates/kobako-wasmtime/Cargo.toml +27 -6
  64. data/crates/kobako-wasmtime/README.md +3 -3
  65. data/crates/kobako-wasmtime/src/abi.rs +2 -1
  66. data/crates/kobako-wasmtime/src/ambient.rs +11 -2
  67. data/crates/kobako-wasmtime/src/cache/unix.rs +95 -0
  68. data/crates/kobako-wasmtime/src/cache/windows.rs +359 -0
  69. data/crates/kobako-wasmtime/src/cache.rs +88 -103
  70. data/crates/kobako-wasmtime/src/driver.rs +40 -27
  71. data/crates/kobako-wasmtime/src/invocation.rs +21 -7
  72. data/crates/kobako-wasmtime/src/lib.rs +2 -0
  73. data/crates/kobako-wasmtime/src/limiter.rs +78 -25
  74. data/crates/kobako-wasmtime/src/preview1/descriptors.rs +13 -17
  75. data/crates/kobako-wasmtime/src/preview1/differential/mount.rs +4 -5
  76. data/crates/kobako-wasmtime/src/preview1/differential.rs +1 -1
  77. data/crates/kobako-wasmtime/src/preview1/fd.rs +23 -19
  78. data/crates/kobako-wasmtime/src/preview1/mount_view.rs +130 -197
  79. data/crates/kobako-wasmtime/src/preview1/path.rs +3 -3
  80. data/crates/kobako-wasmtime/src/preview1/poll.rs +28 -22
  81. data/crates/kobako-wasmtime/src/preview1/random.rs +125 -0
  82. data/crates/kobako-wasmtime/src/preview1/testing.rs +31 -0
  83. data/crates/kobako-wasmtime/src/preview1.rs +85 -9
  84. data/crates/kobako-wasmtime/src/preview2/filesystem.rs +2 -2
  85. data/crates/kobako-wasmtime/src/preview2/host.rs +609 -249
  86. data/crates/kobako-wasmtime/src/preview2/http.rs +299 -215
  87. data/crates/kobako-wasmtime/src/preview2/io.rs +17 -4
  88. data/crates/kobako-wasmtime/src/preview2/table.rs +24 -72
  89. data/crates/kobako-wasmtime/src/trap.rs +68 -23
  90. data/crates/kobako-wasmtime/src/waits.rs +59 -0
  91. data/crates/kobako-wasmtime/src/wasi.rs +91 -10
  92. data/crates/kobako-wasmtime/witx/wasi_snapshot_preview1.witx +13 -0
  93. data/data/kobako.wasm +0 -0
  94. data/ext/kobako/Cargo.toml +6 -2
  95. data/ext/kobako/src/escape.rs +332 -26
  96. data/ext/kobako/src/filesystem/grants.rs +5 -7
  97. data/ext/kobako/src/filesystem/ruby.rs +10 -3
  98. data/ext/kobako/src/filesystem.rs +47 -8
  99. data/ext/kobako/src/fork.rs +72 -0
  100. data/ext/kobako/src/gvl.rs +499 -76
  101. data/ext/kobako/src/handoff.rs +490 -0
  102. data/ext/kobako/src/lib.rs +10 -0
  103. data/ext/kobako/src/network.rs +5 -3
  104. data/ext/kobako/src/pinned_value.rs +28 -15
  105. data/ext/kobako/src/runtime/bridge.rs +32 -25
  106. data/ext/kobako/src/runtime.rs +74 -43
  107. data/lib/kobako/catalog/handles.rb +9 -5
  108. data/lib/kobako/codec/decoder.rb +1 -1
  109. data/lib/kobako/codec/state.rb +2 -18
  110. data/lib/kobako/codec/value_budget.rb +4 -4
  111. data/lib/kobako/codec.rb +5 -6
  112. data/lib/kobako/errors.rb +4 -7
  113. data/lib/kobako/filesystem.rb +1 -1
  114. data/lib/kobako/fork.rb +42 -0
  115. data/lib/kobako/handle.rb +1 -2
  116. data/lib/kobako/sandbox.rb +17 -36
  117. data/lib/kobako/sandbox_options.rb +7 -9
  118. data/lib/kobako/usage.rb +4 -2
  119. data/lib/kobako/version.rb +1 -1
  120. data/lib/kobako.rb +2 -2
  121. data/sig/kobako/catalog/handles.rbs +2 -0
  122. data/sig/kobako/codec/state.rbs +0 -4
  123. data/sig/kobako/codec/value_budget.rbs +1 -1
  124. data/sig/kobako/codec.rbs +2 -2
  125. data/sig/kobako/errors.rbs +0 -3
  126. data/sig/kobako/fork.rbs +17 -0
  127. data/sig/kobako/runtime.rbs +2 -2
  128. data/sig/kobako/sandbox.rbs +3 -12
  129. data/sig/kobako/sandbox_options.rbs +0 -2
  130. metadata +15 -4
  131. data/lib/kobako/pool.rb +0 -162
  132. data/sig/core/thread.rbs +0 -6
  133. data/sig/kobako/pool.rbs +0 -46
@@ -9,6 +9,7 @@ use super::super::super::{Errno, FileType, OpenHow};
9
9
  use super::super::inode::OverlayFileInode;
10
10
  use super::super::path::{timestamps, OverlayPath};
11
11
  use super::{copy_whole, OverlayFs, ViewDir, UPPER};
12
+ use crate::sched;
12
13
  use crate::vfs::{Dentry, RenameFlag};
13
14
 
14
15
  impl OverlayFs {
@@ -28,6 +29,7 @@ impl OverlayFs {
28
29
  at = next;
29
30
  };
30
31
  while let Some(dir) = unmade.pop() {
32
+ sched::stop_if_fatal()?;
31
33
  made = self.copy_up_dir(&dir, &made)?;
32
34
  }
33
35
  Ok(made)
@@ -26,7 +26,7 @@ use super::path::OverlayPath;
26
26
 
27
27
  use super::super::{Errno, FileOperations, FileType, Filesystem, Kiocb};
28
28
  use crate::cred::Cred;
29
- use crate::sched::fatal_signal_pending;
29
+ use crate::sched::stop_if_fatal;
30
30
  use crate::vfs::{Dentry, Identity};
31
31
 
32
32
  /// The directories of the upper Tmpfs holding the view's changes and the
@@ -124,8 +124,9 @@ fn ovl_lower_positive(level: &Level, name: &str) -> bool {
124
124
  }
125
125
 
126
126
  /// Copy the `len` bytes the lower file held when asked, a chunk at a time, as
127
- /// `ovl_copy_up_file` does: a run past its deadline or interrupted stops
128
- /// between chunks with EINTR, and a file that ended sooner stops the copy.
127
+ /// `ovl_copy_up_file` does: a signal is handled between chunks, a run past its
128
+ /// deadline or interrupted stops there with EINTR, and a file that ended
129
+ /// sooner stops the copy.
129
130
  fn copy_whole(
130
131
  source: &mut dyn FileOperations,
131
132
  target: &mut dyn FileOperations,
@@ -135,9 +136,7 @@ fn copy_whole(
135
136
  let (mut from, mut to) = (Kiocb::new(0), Kiocb::new(0));
136
137
  let mut left = len;
137
138
  while left > 0 {
138
- if fatal_signal_pending() {
139
- return Err(Errno::Interrupted);
140
- }
139
+ stop_if_fatal()?;
141
140
  let chunk = buf.len().min(usize::try_from(left).unwrap_or(usize::MAX));
142
141
  let read = source.read_iter(&mut from, &mut buf[..chunk])?;
143
142
  if read == 0 {
@@ -352,7 +351,7 @@ mod tests {
352
351
  let (held, taken) = std::sync::mpsc::channel();
353
352
  let (release, released) = std::sync::mpsc::channel::<()>();
354
353
  std::thread::scope(|scope| {
355
- let holder = scope.spawn(move || {
354
+ let owner = scope.spawn(move || {
356
355
  let _lock = work
357
356
  .dentry()
358
357
  .inode_lock()
@@ -360,12 +359,12 @@ mod tests {
360
359
  held.send(()).expect("the test must wait for the lock");
361
360
  released.recv().ok();
362
361
  });
363
- taken.recv().expect("the holder must take the lock");
362
+ taken.recv().expect("the owner must take the lock");
364
363
  crate::sched::run_task(Some(Instant::now()), || fs.cleanup_temp(work, &temp));
365
364
  release
366
365
  .send(())
367
- .expect("the holder must wait for its release");
368
- crate::sched::testing::join_scoped("the lock holder", holder);
366
+ .expect("the owner must wait for its release");
367
+ crate::sched::testing::join_scoped("the lock owner", owner);
369
368
  });
370
369
  let left = names_in(work);
371
370
  fs.tempname()
@@ -387,6 +387,10 @@ impl FileOperations for OverlayDir {
387
387
  generic_read_dir()
388
388
  }
389
389
 
390
+ fn has_iterate_shared(&self) -> bool {
391
+ true
392
+ }
393
+
390
394
  fn iterate_shared(&mut self, dentry: &Dentry, ctx: &mut DirContext<'_>) -> Result<(), Errno> {
391
395
  let (fs, cache) = (&self.fs, &mut self.cache);
392
396
  cache.iterate_shared(dentry, ctx, || fs.dir_read_merged(&fs.dir(dentry)?.level()))
@@ -580,6 +584,19 @@ mod tests {
580
584
  Arc::new([(path, contents)].into_iter().collect())
581
585
  }
582
586
 
587
+ /// Set every time of what stands at `path` to a moment long past, so a
588
+ /// time a later change sets differs from it whatever the clock's
589
+ /// resolution.
590
+ fn set_long_ago(fs: &KernPath, path: &str) {
591
+ let long_ago = std::time::SystemTime::UNIX_EPOCH + Duration::from_secs(1);
592
+ let at = Some(crate::vfs::SetTime::At(long_ago));
593
+ let attr = Iattr {
594
+ ctime: Some(long_ago),
595
+ ..Iattr::for_times(at, at)
596
+ };
597
+ fs.setattr(path, &attr).expect("the times must be set");
598
+ }
599
+
583
600
  // A helper beneath the VFS skips the super block's read-only answer, so only
584
601
  // the view's own upper standing empty keeps the change off its lower Layers.
585
602
  // @behavior FS-197
@@ -658,10 +675,8 @@ mod tests {
658
675
  /// A host directory holding `target`, `dir/inner`, and the links `link`
659
676
  /// to `target`, `dirlink` to `dir`, and `dangling` to nothing, removed
660
677
  /// when it drops.
661
- #[cfg(unix)]
662
678
  struct LinkTree(std::path::PathBuf);
663
679
 
664
- #[cfg(unix)]
665
680
  impl LinkTree {
666
681
  fn new(name: &str) -> Self {
667
682
  let path =
@@ -675,7 +690,7 @@ mod tests {
675
690
  ("dir", "dirlink"),
676
691
  ("missing", "dangling"),
677
692
  ] {
678
- std::os::unix::fs::symlink(target, path.join(link)).expect("the link must be made");
693
+ crate::filesystem::testing::symlink(target, path.join(link));
679
694
  }
680
695
  Self(path)
681
696
  }
@@ -699,7 +714,6 @@ mod tests {
699
714
  // so only merging directory over directory keeps the lower link's
700
715
  // target out of the view.
701
716
  // @behavior FS-113
702
- #[cfg(unix)]
703
717
  #[test]
704
718
  fn a_lower_layers_link_beneath_an_upper_directory_is_not_merged() {
705
719
  let tree = LinkTree::new("not-merged");
@@ -718,7 +732,6 @@ mod tests {
718
732
  );
719
733
  }
720
734
 
721
- #[cfg(unix)]
722
735
  impl Drop for LinkTree {
723
736
  fn drop(&mut self) {
724
737
  let _ = std::fs::remove_dir_all(&self.0);
@@ -728,7 +741,6 @@ mod tests {
728
741
  // The link stands in the Bind Layer beneath a Tmpfs one, so only
729
742
  // the Overlay asking that Layer without following refuses it.
730
743
  // @behavior FS-100
731
- #[cfg(unix)]
732
744
  #[test]
733
745
  fn an_overlay_opens_no_link_as_a_directory_where_asked_not_to_follow() {
734
746
  let host = LinkTree::new("nofollow");
@@ -745,7 +757,6 @@ mod tests {
745
757
  }
746
758
 
747
759
  // @behavior FS-049
748
- #[cfg(unix)]
749
760
  #[test]
750
761
  fn an_overlay_reports_a_link_its_layer_holds_as_a_link() {
751
762
  let host = LinkTree::new("lstat");
@@ -762,7 +773,6 @@ mod tests {
762
773
  // A link to a directory follows to one, and a dangling link follows to
763
774
  // nothing, so only judging each link itself lets both go.
764
775
  // @behavior FS-050
765
- #[cfg(unix)]
766
776
  #[test]
767
777
  fn an_overlay_removes_a_link_its_read_only_layer_holds_as_itself() {
768
778
  let host = LinkTree::new("unlink");
@@ -947,7 +957,10 @@ mod tests {
947
957
  let lower: Tmpfs = [(format!("{deep}/file"), "x")].into_iter().collect();
948
958
  let overlay = Overlay::new(
949
959
  vec![Filesystem::new(lower).expect("the instance must stand")],
950
- Some(Filesystem::new(Tmpfs::new()).expect("the instance must stand")),
960
+ Some(
961
+ Filesystem::new(Tmpfs::new().with_nr_inodes(None))
962
+ .expect("the instance must stand"),
963
+ ),
951
964
  )
952
965
  .unwrap_or_else(|_| panic!("the Overlay must make"));
953
966
  let made = std::thread::Builder::new()
@@ -971,7 +984,6 @@ mod tests {
971
984
 
972
985
  // The upper is a Tmpfs, which holds no link, so a link cannot land there.
973
986
  // @behavior FS-051
974
- #[cfg(unix)]
975
987
  #[test]
976
988
  fn an_overlay_refuses_to_move_a_link_its_read_only_layer_holds() {
977
989
  let host = LinkTree::new("through");
@@ -990,7 +1002,6 @@ mod tests {
990
1002
  // The lower Tmpfs holds a readable `dangling`, so only the upper
991
1003
  // link standing at that path can keep the read from reaching it.
992
1004
  // @behavior FS-053
993
- #[cfg(unix)]
994
1005
  #[test]
995
1006
  fn a_link_a_layer_holds_hides_what_the_layers_below_hold_there() {
996
1007
  let host = LinkTree::new("shadow");
@@ -1017,7 +1028,6 @@ mod tests {
1017
1028
  // The lower Tmpfs holds `dirlink/x` beside where the upper link points,
1018
1029
  // so only the link hiding everything beneath it keeps `x` out.
1019
1030
  // @behavior FS-057
1020
- #[cfg(unix)]
1021
1031
  #[test]
1022
1032
  fn a_link_a_layer_holds_hides_what_the_layers_below_hold_beneath_it() {
1023
1033
  let host = LinkTree::new("shadow-beneath");
@@ -1127,7 +1137,6 @@ mod tests {
1127
1137
  // The link points at nothing, so only the link itself can be what an
1128
1138
  // exclusive create or a new directory finds there.
1129
1139
  // @behavior FS-052
1130
- #[cfg(unix)]
1131
1140
  #[test]
1132
1141
  fn an_overlay_finds_a_dangling_link_where_a_new_entry_would_go() {
1133
1142
  let host = LinkTree::new("dangling");
@@ -1565,7 +1574,6 @@ mod tests {
1565
1574
  // Bind reports the same identity for both names, so without the view keeping
1566
1575
  // them apart the VFS would answer the move as having nothing to do.
1567
1576
  // @behavior FS-170
1568
- #[cfg(unix)]
1569
1577
  #[test]
1570
1578
  fn a_lower_layers_hard_link_moves_onto_the_other_as_two_entries() {
1571
1579
  let host = LinkTree::new("hard-link");
@@ -1733,8 +1741,8 @@ mod tests {
1733
1741
  }
1734
1742
 
1735
1743
  /// Another call through the VFS at `overlay`, started once on a thread
1736
- /// of its own and given a while to finish, recording whether it did
1737
- /// before the call starting it went on.
1744
+ /// of its own and held for until it waits or finishes, recording whether
1745
+ /// it finished before the call starting it went on.
1738
1746
  #[derive(Default)]
1739
1747
  struct Interleave {
1740
1748
  overlay: Mutex<Option<crate::vfs::Path>>,
@@ -1749,7 +1757,11 @@ mod tests {
1749
1757
  };
1750
1758
  let finished = Arc::new(AtomicUsize::new(0));
1751
1759
  let marker = finished.clone();
1760
+ let (counted, counts) = std::sync::mpsc::channel();
1752
1761
  let handle = thread::spawn(move || {
1762
+ counted
1763
+ .send(crate::sched::testing::count_waits())
1764
+ .expect("the call starting it takes the count");
1753
1765
  let how = crate::vfs::OpenHow {
1754
1766
  write: true,
1755
1767
  create: true,
@@ -1759,10 +1771,10 @@ mod tests {
1759
1771
  crate::vfs::kern_path::filp_open(&Arc::default(), &overlay, "dir/new", &how);
1760
1772
  marker.store(1, Ordering::SeqCst);
1761
1773
  });
1762
- let deadline = Instant::now() + Duration::from_millis(200);
1763
- while finished.load(Ordering::SeqCst) == 0 && Instant::now() < deadline {
1764
- thread::sleep(Duration::from_millis(5));
1765
- }
1774
+ let waits = counts.recv().expect("the other call counts its waits");
1775
+ crate::sched::testing::wait_until("the other call waiting or finishing", || {
1776
+ waits.load(Ordering::SeqCst) > 0 || finished.load(Ordering::SeqCst) == 1
1777
+ });
1766
1778
  *self.finished_inside.lock().unwrap() = Some(finished.load(Ordering::SeqCst) == 1);
1767
1779
  *self.other.lock().unwrap() = Some(handle);
1768
1780
  }
@@ -2073,10 +2085,10 @@ mod tests {
2073
2085
  #[test]
2074
2086
  fn a_copy_up_keeps_the_lower_files_times() {
2075
2087
  let lower = tmpfs_with("dir/file", "lower");
2088
+ set_long_ago(&on(&lower), "dir/file");
2076
2089
  let before = on(&lower)
2077
2090
  .vfs_lstat("dir/file")
2078
2091
  .expect("the lower file must stand");
2079
- std::thread::sleep(Duration::from_millis(10));
2080
2092
  let overlay = at(over(lower, Arc::new(Tmpfs::new())));
2081
2093
  overlay
2082
2094
  .open(
@@ -2103,11 +2115,11 @@ mod tests {
2103
2115
  let lower: Arc<Tmpfs> = Arc::new([("dir/a", "a"), ("dir/b", "b")].into_iter().collect());
2104
2116
  let overlay = at(over(lower, Arc::new(Tmpfs::new())));
2105
2117
  overwrite(&overlay, "dir/a", b"changed");
2118
+ set_long_ago(&overlay, "dir");
2106
2119
  let before = overlay
2107
2120
  .vfs_lstat("dir")
2108
2121
  .expect("the directory must stand")
2109
2122
  .mtime;
2110
- std::thread::sleep(Duration::from_millis(10));
2111
2123
  overlay
2112
2124
  .open(
2113
2125
  "dir/b",
@@ -2240,6 +2252,184 @@ mod tests {
2240
2252
  );
2241
2253
  }
2242
2254
 
2255
+ /// A host directory holding `dir/a`, `dir/b`, and `dir/c`, removed when it
2256
+ /// drops.
2257
+ struct HostDir(std::path::PathBuf);
2258
+
2259
+ impl HostDir {
2260
+ fn new(name: &str) -> Self {
2261
+ let path =
2262
+ std::env::temp_dir().join(format!("kobako-overlay-{name}-{}", std::process::id()));
2263
+ let _ = std::fs::remove_dir_all(&path);
2264
+ std::fs::create_dir_all(path.join("dir")).expect("the host directory must be made");
2265
+ for file in ["a", "b", "c"] {
2266
+ std::fs::write(path.join("dir").join(file), "x").expect("the file must be made");
2267
+ }
2268
+ Self(path)
2269
+ }
2270
+ }
2271
+
2272
+ impl Drop for HostDir {
2273
+ fn drop(&mut self) {
2274
+ let _ = std::fs::remove_dir_all(&self.0);
2275
+ }
2276
+ }
2277
+
2278
+ /// `dir` of a `Tmpfs`, of a `Bind` of `host`, and of an `Overlay`, each holding
2279
+ /// `a`, `b`, and `c`, opened to list.
2280
+ fn listed_kinds(host: &HostDir) -> Vec<(&'static str, KernPath)> {
2281
+ let files = || -> Tmpfs {
2282
+ ["dir/a", "dir/b", "dir/c"]
2283
+ .map(|path| (path, "x"))
2284
+ .into_iter()
2285
+ .collect()
2286
+ };
2287
+ let bind = super::super::Bind::new(&host.0).expect("the host directory must open");
2288
+ [
2289
+ ("Tmpfs", at(files())),
2290
+ ("Bind", at(bind)),
2291
+ (
2292
+ "Overlay",
2293
+ at(over(Arc::new(files()), Arc::new(Tmpfs::new()))),
2294
+ ),
2295
+ ]
2296
+ .map(|(kind, fs)| (kind, fs.open_dir("dir").expect("dir must open")))
2297
+ .into()
2298
+ }
2299
+
2300
+ // @behavior FS-232
2301
+ #[test]
2302
+ fn a_listing_past_its_runs_deadline_answers_eintr() {
2303
+ let host = HostDir::new("listing-deadline");
2304
+ let answers: Vec<_> = listed_kinds(&host)
2305
+ .iter()
2306
+ .map(|(kind, dir)| {
2307
+ let listed = crate::sched::run_task(Some(Instant::now()), || dir.iterate_dir());
2308
+ (*kind, listed.err())
2309
+ })
2310
+ .collect();
2311
+ assert_eq!(
2312
+ answers,
2313
+ ["Tmpfs", "Bind", "Overlay"].map(|kind| (kind, Some(Errno::Interrupted))),
2314
+ "iterate_shared of each built-in Filesystem in a run past its deadline must answer \
2315
+ EINTR before its next entry"
2316
+ );
2317
+ }
2318
+
2319
+ // @behavior FS-233
2320
+ #[test]
2321
+ fn a_listing_hands_a_pending_signal_over_once() {
2322
+ let host = HostDir::new("listing-signal");
2323
+ let answers: Vec<_> = listed_kinds(&host)
2324
+ .iter()
2325
+ .map(|(kind, dir)| {
2326
+ let handled = crate::sched::testing::count_signals();
2327
+ let names = crate::sched::run_task(None, || {
2328
+ crate::sched::Task::current().signal();
2329
+ dir.iterate_dir()
2330
+ })
2331
+ .map(|entries| {
2332
+ let mut names: Vec<_> = entries.into_iter().map(|entry| entry.name).collect();
2333
+ names.sort();
2334
+ names
2335
+ });
2336
+ (*kind, handled.load(Ordering::SeqCst), names)
2337
+ })
2338
+ .collect();
2339
+ let all = || Ok(vec!["a".to_string(), "b".to_string(), "c".to_string()]);
2340
+ assert_eq!(
2341
+ answers,
2342
+ vec![
2343
+ ("Tmpfs", 1, all()),
2344
+ ("Bind", 1, all()),
2345
+ ("Overlay", 1, all())
2346
+ ],
2347
+ "iterate_shared of each built-in Filesystem with a signal pending must hand it to the \
2348
+ Blocking once and list every entry"
2349
+ );
2350
+ }
2351
+
2352
+ // @behavior FS-234
2353
+ #[test]
2354
+ fn a_copy_up_of_ancestors_past_its_runs_deadline_answers_eintr() {
2355
+ let upper = Arc::new(Tmpfs::new());
2356
+ let overlay = at(over(tmpfs_with("a/b/f", "lower"), upper.clone()));
2357
+ let made =
2358
+ crate::sched::run_task(Some(Instant::now()), || write(&overlay, "a/b/new", b"x"));
2359
+ assert_eq!(
2360
+ (made, on(&upper).vfs_lstat("upper/a").err()),
2361
+ (Err(Errno::Interrupted), Some(Errno::NotFound)),
2362
+ "a create through an Overlay whose run's deadline passed must answer EINTR before it \
2363
+ copies up the first ancestor, as ovl_copy_up_flags climbs them one at a time"
2364
+ );
2365
+ }
2366
+
2367
+ /// A layer whose files signal the run reading them as they hand over their
2368
+ /// first chunk.
2369
+ struct SignalsOnRead(Arc<AtomicUsize>);
2370
+
2371
+ impl Hook for SignalsOnRead {
2372
+ fn open(
2373
+ &self,
2374
+ inner: &dyn InodeOperations,
2375
+ dentry: &Dentry,
2376
+ how: &OpenHow,
2377
+ ) -> Result<Box<dyn FileOperations>, Errno> {
2378
+ Ok(Box::new(SignalsFirstRead(
2379
+ inner.open(dentry, how)?,
2380
+ self.0.clone(),
2381
+ )))
2382
+ }
2383
+ }
2384
+
2385
+ /// A file signalling its reader on the first read, counting the reads.
2386
+ struct SignalsFirstRead(Box<dyn FileOperations>, Arc<AtomicUsize>);
2387
+
2388
+ impl FileOperations for SignalsFirstRead {
2389
+ fn read_iter(&mut self, iocb: &mut Kiocb, buf: &mut [u8]) -> Result<usize, Errno> {
2390
+ if self.1.fetch_add(1, Ordering::SeqCst) == 0 {
2391
+ crate::sched::Task::current().signal();
2392
+ }
2393
+ self.0.read_iter(iocb, buf)
2394
+ }
2395
+ }
2396
+
2397
+ // The signal arrives as the copy reads, since every walk to the file hands
2398
+ // one over first.
2399
+ // @behavior FS-235
2400
+ #[test]
2401
+ fn a_copy_up_hands_a_pending_signal_over_between_chunks() {
2402
+ let upper = Arc::new(Tmpfs::new());
2403
+ let contents = "x".repeat(3 * 64 * 1024);
2404
+ let reads = Arc::new(AtomicUsize::new(0));
2405
+ let lower = Wrap::new(
2406
+ tmpfs_with("big", &contents),
2407
+ Arc::new(SignalsOnRead(reads.clone())),
2408
+ );
2409
+ let overlay = at(over(Arc::new(lower), upper.clone()));
2410
+ let read_when_handled = Arc::new(Mutex::new(Vec::new()));
2411
+ crate::sched::testing::on_signal({
2412
+ let (reads, seen) = (reads.clone(), read_when_handled.clone());
2413
+ move || {
2414
+ seen.lock()
2415
+ .expect("never poisoned")
2416
+ .push(reads.load(Ordering::SeqCst))
2417
+ }
2418
+ });
2419
+ let written = crate::sched::run_task(None, || write_over(&overlay, "big", b"y"));
2420
+ crate::sched::testing::on_signal(|| {});
2421
+ assert_eq!(
2422
+ (
2423
+ written,
2424
+ read_when_handled.lock().expect("never poisoned").clone(),
2425
+ on(&upper).vfs_lstat("upper/big").map(|stat| stat.size)
2426
+ ),
2427
+ (Ok(()), vec![1], Ok(contents.len() as u64)),
2428
+ "a copy up whose run is signalled as it reads must hand the signal to the Blocking \
2429
+ before its next chunk and copy the whole file"
2430
+ );
2431
+ }
2432
+
2243
2433
  /// A Tmpfs reporting `file` two bytes long, though it holds more.
2244
2434
  struct ReportsTwoBytes;
2245
2435
 
@@ -2323,6 +2513,10 @@ mod tests {
2323
2513
  struct FoldedDir(KernPath);
2324
2514
 
2325
2515
  impl FileOperations for FoldedDir {
2516
+ fn has_iterate_shared(&self) -> bool {
2517
+ true
2518
+ }
2519
+
2326
2520
  fn iterate_shared(
2327
2521
  &mut self,
2328
2522
  dentry: &Dentry,
@@ -2633,14 +2827,27 @@ mod tests {
2633
2827
  );
2634
2828
  }
2635
2829
 
2636
- /// Slows the first ask about the directory `e` once armed, so a second copy
2637
- /// up of it finds the first under way.
2638
- struct SlowsDirectory(AtomicBool);
2830
+ /// Holds the first ask about the directory `e` once armed, as a copy up
2831
+ /// holding the view's lock of it asks, until another thread waits in
2832
+ /// line for that lock or its open ended, so a second copy up of it finds
2833
+ /// the first under way.
2834
+ #[derive(Default)]
2835
+ struct HoldsDirectory {
2836
+ lock: Mutex<Option<Arc<crate::locking::RwSemaphore>>>,
2837
+ ended: AtomicUsize,
2838
+ }
2639
2839
 
2640
- impl Hook for SlowsDirectory {
2840
+ impl Hook for HoldsDirectory {
2641
2841
  fn getattr(&self, inner: &dyn InodeOperations, dentry: &Dentry) -> Result<Kstat, Errno> {
2642
- if dentry.name() == "e" && self.0.swap(false, Ordering::SeqCst) {
2643
- thread::sleep(Duration::from_millis(100));
2842
+ let armed = match dentry.name() == "e" {
2843
+ true => self.lock.lock().unwrap().take(),
2844
+ false => None,
2845
+ };
2846
+ if let Some(lock) = armed {
2847
+ crate::sched::testing::wait_until(
2848
+ "the other open waiting for the directory this one copies up, or ending",
2849
+ || lock.queued() > 0 || self.ended.load(Ordering::SeqCst) > 0,
2850
+ );
2644
2851
  }
2645
2852
  inner.getattr(dentry)
2646
2853
  }
@@ -2655,7 +2862,7 @@ mod tests {
2655
2862
  use crate::mount::{Access, Mount};
2656
2863
  use crate::vfs::{self, OpenHow, Path};
2657
2864
  let lower: Tmpfs = [("d/e/f", "1"), ("d/e/g", "2")].into_iter().collect();
2658
- let hook = Arc::new(SlowsDirectory(AtomicBool::new(false)));
2865
+ let hook = Arc::new(HoldsDirectory::default());
2659
2866
  let overlay = Overlay::new(
2660
2867
  vec![Filesystem::new(Wrap::new(Arc::new(lower), hook.clone()))
2661
2868
  .expect("the instance must stand")],
@@ -2677,12 +2884,28 @@ mod tests {
2677
2884
  for name in ["d/e/f", "d/e/g"] {
2678
2885
  vfs::vfs_fstatat(&cred, &root, name, false).expect("the file stands");
2679
2886
  }
2680
- hook.0.store(true, Ordering::SeqCst);
2887
+ let read_dir = OpenHow {
2888
+ read: true,
2889
+ directory: true,
2890
+ ..OpenHow::default()
2891
+ };
2892
+ let Ok(held) = vfs::kern_path::filp_open(&cred, &root, "d/e", &read_dir) else {
2893
+ panic!("the directory must open");
2894
+ };
2895
+ let inode = held
2896
+ .path()
2897
+ .dentry()
2898
+ .inode()
2899
+ .expect("the directory has an inode");
2900
+ let view = OverlayDirInode::of(&inode).expect("the view holds the directory");
2901
+ *hook.lock.lock().unwrap() = Some(view.lock.clone());
2902
+ let open = |name| {
2903
+ let opened = vfs::kern_path::filp_open(&cred, &root, name, &write).map(drop);
2904
+ hook.ended.fetch_add(1, Ordering::SeqCst);
2905
+ opened
2906
+ };
2681
2907
  let opened = thread::scope(|scope| {
2682
- let (first, second) = (
2683
- scope.spawn(|| vfs::kern_path::filp_open(&cred, &root, "d/e/f", &write).map(drop)),
2684
- scope.spawn(|| vfs::kern_path::filp_open(&cred, &root, "d/e/g", &write).map(drop)),
2685
- );
2908
+ let (first, second) = (scope.spawn(|| open("d/e/f")), scope.spawn(|| open("d/e/g")));
2686
2909
  (
2687
2910
  crate::sched::testing::join_scoped("the first open", first),
2688
2911
  crate::sched::testing::join_scoped("the second open", second),
@@ -3249,13 +3472,14 @@ mod tests {
3249
3472
  );
3250
3473
  }
3251
3474
 
3252
- /// A Tmpfs whose first open sleeps a while, flagging that it began.
3253
- #[derive(Default)]
3254
- struct SleepsOnOpen {
3475
+ /// A `Tmpfs` whose first open holds, flagging that it began, until `waits`
3476
+ /// counts a wait.
3477
+ struct HoldsFirstOpen {
3255
3478
  opening: AtomicBool,
3479
+ waits: Arc<AtomicUsize>,
3256
3480
  }
3257
3481
 
3258
- impl Hook for SleepsOnOpen {
3482
+ impl Hook for HoldsFirstOpen {
3259
3483
  fn open(
3260
3484
  &self,
3261
3485
  inner: &dyn InodeOperations,
@@ -3263,26 +3487,32 @@ mod tests {
3263
3487
  how: &OpenHow,
3264
3488
  ) -> Result<Box<dyn FileOperations>, Errno> {
3265
3489
  if !self.opening.swap(true, Ordering::SeqCst) {
3266
- thread::sleep(Duration::from_millis(100));
3490
+ crate::sched::testing::wait_until(
3491
+ "the second copy up waiting for the first one's lock",
3492
+ || self.waits.load(Ordering::SeqCst) > 0,
3493
+ );
3267
3494
  }
3268
3495
  inner.open(dentry, how)
3269
3496
  }
3270
3497
  }
3271
3498
 
3272
- // The first copy up holds the file's lock while its lower open sleeps,
3499
+ // The first copy up holds the file's lock while its lower open holds,
3273
3500
  // so the second, of the same file, finds the lock held and waits.
3274
3501
  // @behavior FS-039
3275
3502
  #[test]
3276
3503
  fn a_contended_copy_up_waits_through_the_registered_blocking() {
3277
3504
  let counts = crate::sched::testing::count_waits();
3278
- let sleeps = Arc::new(SleepsOnOpen::default());
3279
- let lower = Wrap::new(tmpfs_with("note", "lower"), sleeps.clone());
3505
+ let holds = Arc::new(HoldsFirstOpen {
3506
+ opening: AtomicBool::new(false),
3507
+ waits: counts.clone(),
3508
+ });
3509
+ let lower = Wrap::new(tmpfs_with("note", "lower"), holds.clone());
3280
3510
  let overlay = over(Arc::new(lower), Arc::new(Tmpfs::new()));
3281
3511
  let first = at(overlay);
3282
3512
  let second = first.clone();
3283
3513
  let copying = thread::spawn(move || write_over(&first, "note", b"first"));
3284
3514
  crate::sched::testing::wait_until("the first copy up opening its lower file", || {
3285
- sleeps.opening.load(Ordering::SeqCst)
3515
+ holds.opening.load(Ordering::SeqCst)
3286
3516
  });
3287
3517
  let waited =
3288
3518
  crate::sched::testing::within_max_wait(|| write_over(&second, "note", b"later"));
@@ -3320,15 +3550,16 @@ mod tests {
3320
3550
  );
3321
3551
  }
3322
3552
 
3323
- // The upper Layer's directory changes after the lower one is made, so
3324
- // the two report different times and only the upper's matches.
3553
+ // The upper layer's directory changes now, and the lower one's times are
3554
+ // long past, so the two report different times and only the upper's
3555
+ // matches.
3325
3556
  // @behavior FS-117
3326
3557
  #[test]
3327
3558
  fn an_overlay_reports_a_merged_directory_as_overlayfs_does() {
3328
3559
  let lower: Arc<Tmpfs> = Arc::new([("dir/a.txt", "a")].into_iter().collect());
3560
+ set_long_ago(&on(&lower), "dir");
3329
3561
  let upper: Arc<Tmpfs> = Arc::new(Tmpfs::new());
3330
3562
  let overlay = at(over(lower.clone(), upper.clone()));
3331
- std::thread::sleep(std::time::Duration::from_millis(10));
3332
3563
  write(&overlay, "dir/b.txt", b"b").expect("the upper must take a write");
3333
3564
  let merged = overlay.vfs_lstat("dir").expect("the directory must stand");
3334
3565
  let modified = |layer: &Arc<Tmpfs>, path: &str| {