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
@@ -6,11 +6,11 @@
6
6
  //! bound; the walk asks `revalidate` before trusting it, so what changed behind
7
7
  //! the runtime's back is found again.
8
8
 
9
+ use crate::sched::TaskId;
9
10
  use std::any::Any;
10
11
  use std::num::NonZeroUsize;
11
12
  use std::sync::atomic::{AtomicUsize, Ordering};
12
13
  use std::sync::{Arc, Condvar, Mutex, MutexGuard, OnceLock, PoisonError, Weak};
13
- use std::thread::ThreadId;
14
14
 
15
15
  use lru::LruCache;
16
16
 
@@ -42,11 +42,6 @@ pub trait DentryOperations: Send + Sync {
42
42
  /// The most names a Filesystem keeps that nothing holds.
43
43
  const MAX_CACHED_DENTRIES: NonZeroUsize = NonZeroUsize::new(1024).expect("the bound is not zero");
44
44
 
45
- /// Every move of a dentry, as Linux's `rename_lock`, so a walk up its
46
- /// parents answers where the dentry stood at one instant, and a walk learns of a
47
- /// move landing meanwhile.
48
- pub(super) static RENAME_LOCK: SeqLock = SeqLock::new();
49
-
50
45
  /// One name of a Filesystem, and where it stands now. Only the VFS makes
51
46
  /// one, so a Filesystem reads the name a call is about from it.
52
47
  pub struct Dentry {
@@ -106,9 +101,9 @@ enum LookupState {
106
101
  Unasked,
107
102
  /// In lookup for the walk that made the dentry, which has yet to ask, as
108
103
  /// `d_alloc_parallel` marks the dentry it makes.
109
- Allocated(ThreadId),
104
+ Allocated(TaskId),
110
105
  /// The walk asking the Filesystem what stands here, while it asks.
111
- Asking(ThreadId),
106
+ Asking(TaskId),
112
107
  /// A lookup answered for the name, or a call made what stands there.
113
108
  Answered,
114
109
  }
@@ -207,7 +202,7 @@ impl Dentry {
207
202
  /// Each name from the Filesystem's root down to this directory, as Linux's
208
203
  /// `__dentry_path` walks up until `IS_ROOT`.
209
204
  fn names(&self) -> Vec<String> {
210
- RENAME_LOCK.read(|| {
205
+ self.rename_lock().read(|| {
211
206
  let mut names = Vec::new();
212
207
  let mut at = self.get();
213
208
  while !at.is_root() {
@@ -378,16 +373,12 @@ impl Dentry {
378
373
  &self,
379
374
  ask: impl FnOnce() -> Result<T, Errno>,
380
375
  ) -> Result<Option<T>, Errno> {
381
- let ends = match self.begin_lookup()? {
376
+ let _ends = match self.begin_lookup()? {
382
377
  InLookup::Answered => return Ok(None),
383
- InLookup::Begun => true,
384
- InLookup::Within => false,
378
+ InLookup::Begun => Some(LookupDone(self)),
379
+ InLookup::Within => None,
385
380
  };
386
- let asked = ask();
387
- if ends {
388
- self.lookup_done();
389
- }
390
- asked.map(Some)
381
+ ask().map(Some)
391
382
  }
392
383
 
393
384
  /// Record what a lookup found at this name of `sb`, as Linux's
@@ -447,7 +438,7 @@ impl Dentry {
447
438
  fn unalias(self: &Arc<Self>, sb: &SuperBlock, alias: &Arc<Dentry>) -> Result<(), Errno> {
448
439
  let (parent, name) = self.parent_and_name();
449
440
  let mut table = self.hashtables.dentry_hashtable();
450
- RENAME_LOCK.write(|| {
441
+ self.rename_lock().write(|| {
451
442
  if d_ancestor(alias, self) || alias.is_root() {
452
443
  return Err(Errno::FilesystemLoop);
453
444
  }
@@ -499,6 +490,7 @@ impl Dentry {
499
490
  fn wait_lookup(&self) -> Result<bool, Errno> {
500
491
  let current = sched::current();
501
492
  loop {
493
+ sched::handle_signal();
502
494
  let state = self.state();
503
495
  match state.asked {
504
496
  LookupState::Allocated(walk) | LookupState::Asking(walk) if walk == current => {
@@ -593,12 +585,19 @@ impl Dentry {
593
585
  /// Whether this dentry is `ancestor` or stands beneath it, as Linux's
594
586
  /// `is_subdir` answers.
595
587
  pub(crate) fn is_subdir(self: &Arc<Self>, ancestor: &Arc<Dentry>) -> bool {
596
- Arc::ptr_eq(self, ancestor) || RENAME_LOCK.read(|| d_ancestor(ancestor, self))
588
+ Arc::ptr_eq(self, ancestor) || self.rename_lock().read(|| d_ancestor(ancestor, self))
589
+ }
590
+
591
+ /// What every move of a dentry of this Filesystem writes, so a walk up
592
+ /// its parents answers where the dentry stood at one instant, and a walk
593
+ /// learns of a move landing meanwhile.
594
+ pub(super) fn rename_lock(&self) -> &SeqLock {
595
+ &self.hashtables.rename_lock
597
596
  }
598
597
  }
599
598
 
600
599
  /// Whether `ancestor` stands above `dentry`, as Linux's `d_ancestor` answers.
601
- /// The caller holds `RENAME_LOCK`, so no move lands during the walk up.
600
+ /// The caller holds the rename lock, so no move lands during the walk up.
602
601
  fn d_ancestor(ancestor: &Arc<Dentry>, dentry: &Arc<Dentry>) -> bool {
603
602
  let mut at = dentry.clone();
604
603
  while !at.is_root() {
@@ -610,6 +609,18 @@ fn d_ancestor(ancestor: &Arc<Dentry>, dentry: &Arc<Dentry>) -> bool {
610
609
  false
611
610
  }
612
611
 
612
+ /// Ends the lookup its walk began on a dentry once it goes, as Linux's
613
+ /// `d_lookup_done` follows every return from `->lookup`. A panic unwinding
614
+ /// past it ends the lookup too, an exit the kernel has no counterpart for, so
615
+ /// the walks waiting on the name go on.
616
+ struct LookupDone<'a>(&'a Dentry);
617
+
618
+ impl Drop for LookupDone<'_> {
619
+ fn drop(&mut self) {
620
+ self.0.lookup_done();
621
+ }
622
+ }
623
+
613
624
  /// A dentry holds the one above it, and moves can stack them as deep as a
614
625
  /// Filesystem holds directories, so the last reference lets go of each in a
615
626
  /// loop, as Linux's `dput` walks up in one.
@@ -638,8 +649,13 @@ fn key(parent: &Arc<Dentry>, name: &str) -> Key {
638
649
  }
639
650
 
640
651
  /// The dentries of one Filesystem a walk finds by the directory above and the
641
- /// name there.
652
+ /// name there, and the lock every move among them writes.
642
653
  struct Hashtables {
654
+ /// Written by every move of a dentry, as Linux's `rename_lock`. It is the
655
+ /// instance's rather than the process's, since a move never crosses
656
+ /// Filesystems: a walk learns only of the moves that could carry its
657
+ /// directories, and a Sandbox learns nothing of another's moves.
658
+ rename_lock: SeqLock,
643
659
  /// Those a lookup answered, as Linux's `dentry_hashtable`.
644
660
  dentry_hashtable: Mutex<Hashtable<Key, Dentry>>,
645
661
  /// Those a walk is still looking up, as Linux's `in_lookup_hashtable`, so
@@ -706,6 +722,7 @@ pub(crate) struct Dentries {
706
722
  impl Dentries {
707
723
  pub(crate) fn new(ops: Option<Arc<dyn DentryOperations>>) -> Self {
708
724
  let hashtables = Arc::new(Hashtables {
725
+ rename_lock: SeqLock::new(),
709
726
  dentry_hashtable: Mutex::default(),
710
727
  in_lookup_hashtable: Mutex::default(),
711
728
  inode_hashtable: Mutex::default(),
@@ -825,14 +842,14 @@ impl Dentries {
825
842
  }
826
843
 
827
844
  /// Let go of `dentry`, which no longer names what stood at its name when
828
- /// `RENAME_LOCK` read `asked`, and of the names kept beneath it, as Linux's
845
+ /// its rename lock read `asked`, and of the names kept beneath it, as Linux's
829
846
  /// `d_invalidate` drops it and shrinks what is below a positive one. One a
830
847
  /// move carried elsewhere since stays where it went: every move takes this
831
848
  /// table before it writes, so none lands between the check and the drop.
832
849
  pub(crate) fn invalidate(&self, dentry: &Arc<Dentry>, asked: usize) {
833
850
  {
834
851
  let mut table = self.hashtables.dentry_hashtable();
835
- if RENAME_LOCK.read_seqretry(asked) || !dentry.hashed() {
852
+ if self.hashtables.rename_lock.read_seqretry(asked) || !dentry.hashed() {
836
853
  return;
837
854
  }
838
855
  drop_hashed(&mut table, dentry);
@@ -884,7 +901,7 @@ impl Dentries {
884
901
  let mut table = self.hashtables.dentry_hashtable();
885
902
  let (parent, name) = target.parent_and_name();
886
903
  let (from, from_name) = dentry.parent_and_name();
887
- RENAME_LOCK.write(|| {
904
+ self.hashtables.rename_lock.write(|| {
888
905
  let mut state = dentry.state();
889
906
  if state.hashed {
890
907
  table.remove(&key(&from, &from_name));
@@ -902,7 +919,7 @@ impl Dentries {
902
919
  pub(crate) fn exchange(&self, first: &Arc<Dentry>, second: &Arc<Dentry>) {
903
920
  let mut table = self.hashtables.dentry_hashtable();
904
921
  let (first_at, second_at) = (first.parent_and_name(), second.parent_and_name());
905
- RENAME_LOCK.write(|| {
922
+ self.hashtables.rename_lock.write(|| {
906
923
  let (mut one, mut other) = (first.state(), second.state());
907
924
  for (state, (parent, name)) in [(&one, &first_at), (&other, &second_at)] {
908
925
  if state.hashed {
@@ -1060,6 +1077,10 @@ mod tests {
1060
1077
  struct ByPathDir(KernPath);
1061
1078
 
1062
1079
  impl FileOperations for ByPathDir {
1080
+ fn has_iterate_shared(&self) -> bool {
1081
+ true
1082
+ }
1083
+
1063
1084
  fn iterate_shared(
1064
1085
  &mut self,
1065
1086
  dentry: &Dentry,
@@ -1128,16 +1149,44 @@ mod tests {
1128
1149
  let lock = lock.clone();
1129
1150
  thread::spawn(move || lock.read_seqbegin())
1130
1151
  };
1131
- thread::sleep(std::time::Duration::from_millis(50));
1152
+ crate::sched::testing::wait_until("the walk waiting for the move or answering", || {
1153
+ lock.waiting() == 1 || reader.is_finished()
1154
+ });
1132
1155
  let waited = !reader.is_finished();
1133
1156
  release.send(()).ok();
1134
- crate::sched::testing::join("the write", writer);
1135
- let begun = crate::sched::testing::join("the read", reader);
1157
+ crate::sched::testing::join("the move", writer);
1158
+ let begun = crate::sched::testing::join("the walk", reader);
1136
1159
  assert_eq!(
1137
1160
  (waited, begun, lock.read_seqretry(begun)),
1138
1161
  (true, 2, false),
1139
- "SeqLock::read_seqbegin during a write must wait for it to end and answer where the \
1140
- writes stand after it, which no later read finds changed"
1162
+ "a walk begun while a move is under way must wait for the move to land and begin \
1163
+ where the moves stand after it, which no later walk finds changed"
1164
+ );
1165
+ }
1166
+
1167
+ // @behavior RT-407
1168
+ #[test]
1169
+ fn a_walk_begun_after_a_move_panicked_begins_at_once() {
1170
+ use crate::locking::seqlock::SeqLock;
1171
+ use std::thread;
1172
+
1173
+ let lock = Arc::new(SeqLock::new());
1174
+ let panicked = {
1175
+ let lock = lock.clone();
1176
+ thread::spawn(move || lock.write(|| panic!("the write panics")))
1177
+ .join()
1178
+ .is_err()
1179
+ };
1180
+ let reader = {
1181
+ let lock = lock.clone();
1182
+ thread::spawn(move || lock.read_seqbegin())
1183
+ };
1184
+ let begun = crate::sched::testing::join("the read", reader);
1185
+ assert_eq!(
1186
+ (panicked, begun),
1187
+ (true, 2),
1188
+ "a walk beginning after a move that panicked must go on at once, the move \
1189
+ counted as ended"
1141
1190
  );
1142
1191
  }
1143
1192
 
@@ -1266,17 +1315,7 @@ mod tests {
1266
1315
  "leaf",
1267
1316
  ));
1268
1317
  let beside = kept(negative(&dentries, &root, "other"));
1269
- // The rename lock is the process's, so another test's rename can
1270
- // move it between the read and the invalidation, which then keeps
1271
- // the name as a raced revalidation does; ask again until one ran
1272
- // undisturbed.
1273
- loop {
1274
- let asked = super::RENAME_LOCK.read_seqbegin();
1275
- dentries.invalidate(&dir, asked);
1276
- if !super::RENAME_LOCK.read_seqretry(asked) {
1277
- break;
1278
- }
1279
- }
1318
+ dentries.invalidate(&dir, dir.rename_lock().read_seqbegin());
1280
1319
  assert_eq!(
1281
1320
  (beneath.upgrade().is_some(), beside.upgrade().is_some()),
1282
1321
  (false, true),
@@ -130,6 +130,14 @@ pub trait FileOperations: Any + Send + Sync {
130
130
  Err(Errno::NotADirectory)
131
131
  }
132
132
 
133
+ /// Whether this file has `iterate_shared`, as Linux's `iterate_dir` asks
134
+ /// whether `f_op->iterate_shared` is set before it locks the directory and
135
+ /// Rust-for-Linux's `#[vtable]` answers `HAS_` for each operation. A file
136
+ /// implementing `iterate_shared` answers true here too.
137
+ fn has_iterate_shared(&self) -> bool {
138
+ false
139
+ }
140
+
133
141
  /// Reserve `len` bytes from `offset`, as `f_op->fallocate` does. A file
134
142
  /// without it refuses as Linux's `vfs_fallocate` refuses one without it.
135
143
  fn fallocate(&mut self, _offset: u64, _len: u64) -> Result<(), Errno> {
@@ -726,11 +734,15 @@ impl File {
726
734
  }
727
735
 
728
736
  /// Hand `ctx` the entries of the directory from where it stands until the
729
- /// actor stops, leaving it past each taken, as `iterate_dir` moves `f_pos`. It
737
+ /// actor stops, leaving it past each taken, as `iterate_dir` moves `f_pos`. A
738
+ /// file without `iterate_shared` answers not a directory before it locks. It
730
739
  /// holds the directory's lock shared, so a removal through the VFS lands
731
740
  /// before or after, and a removed directory answers not found, as it asks
732
741
  /// `IS_DEADDIR`.
733
742
  pub fn iterate_dir(&mut self, ctx: &mut DirContext<'_>) -> Result<(), Errno> {
743
+ if !self.ops.has_iterate_shared() {
744
+ return Err(Errno::NotADirectory);
745
+ }
734
746
  let dentry = self.path.dentry().clone();
735
747
  let _held = dentry.inode_lock_shared()?;
736
748
  if dentry.is_deaddir() {
@@ -854,6 +866,41 @@ mod tests {
854
866
  }
855
867
  }
856
868
 
869
+ /// A file its filesystem opens with no operations of its own.
870
+ struct ListsNothing;
871
+
872
+ impl FileOperations for ListsNothing {}
873
+
874
+ // @behavior RT-408
875
+ #[test]
876
+ fn a_listing_of_a_file_that_lists_nothing_waits_on_no_lock() {
877
+ use std::sync::mpsc;
878
+ let mut file = open_one(|| Box::new(ListsNothing));
879
+ let dentry = file.path.dentry().clone();
880
+ let (held, holding) = mpsc::channel();
881
+ let (release, releasing) = mpsc::channel::<()>();
882
+ let owner = std::thread::spawn(move || {
883
+ let _held = dentry.inode_lock().expect("the lock is free");
884
+ held.send(()).ok();
885
+ releasing.recv_timeout(crate::sched::testing::MAX_WAIT).ok();
886
+ });
887
+ holding
888
+ .recv_timeout(crate::sched::testing::MAX_WAIT)
889
+ .expect("the other thread must hold the lock");
890
+ let waits = crate::sched::testing::count_waits();
891
+ let listed = crate::sched::testing::within_max_wait(|| {
892
+ file.iterate_dir(&mut DirContext::new(0, &mut |_| true))
893
+ });
894
+ release.send(()).ok();
895
+ crate::sched::testing::join("the owner", owner);
896
+ assert_eq!(
897
+ (listed, waits.load(Ordering::SeqCst)),
898
+ (Err(Errno::NotADirectory), 0),
899
+ "a listing of a file its filesystem gives no way to list must answer ENOTDIR \
900
+ without waiting on the lock another thread holds on it"
901
+ );
902
+ }
903
+
857
904
  // The stream's own read moves the position handed to it, so only handing
858
905
  // none and keeping none leaves each read at 0, as file_ppos answers NULL
859
906
  // for FMODE_STREAM.
@@ -1153,6 +1200,10 @@ mod tests {
1153
1200
  generic_file_llseek(dentry, pos, offset, whence)
1154
1201
  }
1155
1202
 
1203
+ fn has_iterate_shared(&self) -> bool {
1204
+ true
1205
+ }
1206
+
1156
1207
  fn iterate_shared(
1157
1208
  &mut self,
1158
1209
  dentry: &Dentry,
@@ -1331,16 +1382,6 @@ mod tests {
1331
1382
  );
1332
1383
  }
1333
1384
 
1334
- /// A listing of `file` from the start, its actor stopping after `room`
1335
- /// entries.
1336
- fn begin_listing(file: &mut File, room: usize) -> Result<(), Errno> {
1337
- let mut seen = 0;
1338
- file.iterate_dir(&mut DirContext::new(0, &mut |_| {
1339
- seen += 1;
1340
- seen < room
1341
- }))
1342
- }
1343
-
1344
1385
  /// A security module refusing to open any directory.
1345
1386
  struct RefusesDirectories;
1346
1387
 
@@ -1379,48 +1420,6 @@ mod tests {
1379
1420
  );
1380
1421
  }
1381
1422
 
1382
- // Every descriptor holds a listing of its own, so only counting them together
1383
- // keeps a guest from multiplying a directory by its open files.
1384
- // @behavior RT-237
1385
- #[test]
1386
- fn listings_held_between_calls_count_against_their_limit() {
1387
- let mount = Mount::new(
1388
- "/data",
1389
- Filesystem::new(ReadsWhole).expect("the instance must stand"),
1390
- Access::Read,
1391
- )
1392
- .expect("/data is a guest path");
1393
- // `.`, `..`, and two one-letter names.
1394
- let one_listing = 4 * size_of::<DirEntry>() + 1 + 2 + 2;
1395
- let cred = crate::security::prepare_creds(&[], Some(one_listing));
1396
- // A Wasm Driver opens the root before the Policies apply and lists through
1397
- // the Cred they were added to, so both must count against one limit.
1398
- let restricted =
1399
- crate::security::restrict_self(&cred, [&crate::grant::Grant::Mount(mount.clone())])
1400
- .expect("every Mount stands");
1401
- let open = |cred: &Arc<Cred>| {
1402
- File::root(mount.mnt(), cred).unwrap_or_else(|_| panic!("the root must open"))
1403
- };
1404
- let (mut first, mut second, mut whole) = (open(&cred), open(&restricted), open(&cred));
1405
- let held = begin_listing(&mut first, 1);
1406
- let refused = begin_listing(&mut second, 1);
1407
- let read_whole = begin_listing(&mut whole, usize::MAX);
1408
- drop(first);
1409
- let after_close = begin_listing(&mut second, 1);
1410
- assert_eq!(
1411
- (held, refused, read_whole, after_close),
1412
- (
1413
- Ok(()),
1414
- Err(Errno::OutOfMemory),
1415
- Err(Errno::OutOfMemory),
1416
- Ok(())
1417
- ),
1418
- "File::iterate_dir of a second listing past its caller's memory limit, stopped part \
1419
- way or read whole, must answer ENOMEM, as ovl_cache_get allocates before dir_emit, \
1420
- and closing the first must give its bytes back"
1421
- );
1422
- }
1423
-
1424
1423
  /// Each entry of the root of `filesystem` with what a listing names it
1425
1424
  /// and what a stat of it names it.
1426
1425
  fn identities<F: crate::vfs::FileSystemType>(
@@ -33,28 +33,35 @@ pub(crate) fn filp_open(
33
33
  do_file_open(cred, start, path, &build_open_flags(how)?)
34
34
  }
35
35
 
36
- /// A directory of a Filesystem reached through the VFS.
36
+ /// A directory of a Filesystem reached through the VFS, held open as a
37
+ /// descriptor holds it once `open_dir` opened it.
37
38
  #[derive(Clone)]
38
- pub(crate) struct KernPath(Path);
39
+ pub(crate) struct KernPath {
40
+ path: Path,
41
+ _held: Option<Arc<Box<dyn FileOperations>>>,
42
+ }
39
43
 
40
44
  /// The root of `filesystem`, mounted read-write.
41
45
  pub(crate) fn kern_path(filesystem: Arc<dyn FileSystemType>) -> KernPath {
42
46
  let filesystem = SuperBlock::new(filesystem).expect("the instance must stand");
43
47
  let mount = Mount::new("/data", filesystem, Access::ReadWrite).expect("/data is a guest path");
44
- KernPath(Path::root(mount.mnt()))
48
+ KernPath {
49
+ path: Path::root(mount.mnt()),
50
+ _held: None,
51
+ }
45
52
  }
46
53
 
47
54
  impl KernPath {
48
55
  /// `path` resolved beneath this directory, a link at its end left as
49
56
  /// it stands.
50
57
  fn resolve(&self, path: &str) -> Result<Nameidata, Errno> {
51
- filename_lookup(&Arc::default(), &self.0, path, false)
58
+ filename_lookup(&Arc::default(), &self.path, path, false)
52
59
  }
53
60
 
54
61
  /// The directory holding the last name of `path`, and that name's dentry,
55
62
  /// whether or not anything stands there.
56
63
  fn parent(&self, path: &str) -> Result<(Nameidata, Arc<Dentry>), Errno> {
57
- let nd = filename_parentat(&Arc::default(), &self.0, path)?;
64
+ let nd = filename_parentat(&Arc::default(), &self.path, path)?;
58
65
  let child = lookup_one_qstr_excl(nd.mnt.sb(), nd.dentry(), &nd.last)?;
59
66
  Ok((nd, child))
60
67
  }
@@ -127,8 +134,15 @@ impl KernPath {
127
134
  match dentry.file_type()? {
128
135
  FileType::Symlink => Err(Errno::FilesystemLoop),
129
136
  FileType::Dir => {
130
- dentry.getattr()?;
131
- Ok(Self(self.0.with_dentry(dentry)))
137
+ let directory = OpenHow {
138
+ directory: true,
139
+ ..OpenHow::default()
140
+ };
141
+ let held = dentry.open(&directory)?;
142
+ Ok(Self {
143
+ path: self.path.with_dentry(dentry),
144
+ _held: Some(Arc::new(held)),
145
+ })
132
146
  }
133
147
  _ => Err(Errno::NotADirectory),
134
148
  }
@@ -136,7 +150,7 @@ impl KernPath {
136
150
 
137
151
  /// What the directory reports of itself, as `vfs_getattr`.
138
152
  pub(crate) fn vfs_getattr(&self) -> Result<Kstat, Errno> {
139
- self.0.dentry().getattr()
153
+ self.path.dentry().getattr()
140
154
  }
141
155
 
142
156
  /// What `path` names, a link reported as itself, as `vfs_lstat`; the
@@ -149,11 +163,10 @@ impl KernPath {
149
163
  }
150
164
 
151
165
  pub(crate) fn lookup(&self, path: &str) -> Result<String, Errno> {
152
- let nd = filename_parentat(&Arc::default(), &self.0, path)?;
166
+ let nd = filename_parentat(&Arc::default(), &self.path, path)?;
153
167
  Ok(lookup_one_qstr_excl(nd.mnt.sb(), nd.dentry(), &nd.last)?.name())
154
168
  }
155
169
 
156
- #[cfg(unix)]
157
170
  pub(crate) fn vfs_readlink(&self, path: &str) -> Result<String, Errno> {
158
171
  self.resolve(path)?.path()?.get_link()
159
172
  }
@@ -163,7 +176,7 @@ impl KernPath {
163
176
  read: true,
164
177
  ..OpenHow::default()
165
178
  };
166
- File::dentry_open(self.0.clone(), &read, &Arc::default())?.iterate_shared()
179
+ File::dentry_open(self.path.clone(), &read, &Arc::default())?.iterate_shared()
167
180
  }
168
181
 
169
182
  pub(crate) fn vfs_mkdir(&self, path: &str) -> Result<(), Errno> {