kobako 0.30.0 → 0.31.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 (266) hide show
  1. checksums.yaml +4 -4
  2. data/.release-please-manifest.json +1 -1
  3. data/CHANGELOG.md +335 -0
  4. data/Cargo.lock +469 -11
  5. data/README.md +43 -3
  6. data/ROADMAP.md +1 -1
  7. data/crates/kobako-runtime/CHANGELOG.md +219 -0
  8. data/crates/kobako-runtime/Cargo.toml +51 -3
  9. data/crates/kobako-runtime/README.md +7 -4
  10. data/crates/kobako-runtime/src/cred.rs +124 -0
  11. data/crates/kobako-runtime/src/dispatch.rs +4 -5
  12. data/crates/kobako-runtime/src/endpoint/buffer.rs +153 -44
  13. data/crates/kobako-runtime/src/endpoint/dispatch.rs +80 -41
  14. data/crates/kobako-runtime/src/endpoint/invoke.rs +23 -36
  15. data/crates/kobako-runtime/src/endpoint.rs +137 -36
  16. data/crates/kobako-runtime/src/errno.rs +391 -0
  17. data/crates/kobako-runtime/src/error.rs +31 -10
  18. data/crates/kobako-runtime/src/filesystem/anon_inodes.rs +52 -0
  19. data/crates/kobako-runtime/src/filesystem/bind/lookup.rs +52 -14
  20. data/crates/kobako-runtime/src/filesystem/bind/walk.rs +19 -20
  21. data/crates/kobako-runtime/src/filesystem/bind.rs +437 -245
  22. data/crates/kobako-runtime/src/filesystem/overlay/file.rs +6 -6
  23. data/crates/kobako-runtime/src/filesystem/overlay/path.rs +43 -65
  24. data/crates/kobako-runtime/src/filesystem/overlay/stack/copy_up.rs +9 -16
  25. data/crates/kobako-runtime/src/filesystem/overlay/stack/dir.rs +47 -72
  26. data/crates/kobako-runtime/src/filesystem/overlay/stack/namei.rs +12 -12
  27. data/crates/kobako-runtime/src/filesystem/overlay/stack/readdir.rs +4 -7
  28. data/crates/kobako-runtime/src/filesystem/overlay/stack.rs +121 -37
  29. data/crates/kobako-runtime/src/filesystem/overlay.rs +302 -343
  30. data/crates/kobako-runtime/src/filesystem/pipe.rs +12 -9
  31. data/crates/kobako-runtime/src/filesystem/rdir.rs +15 -32
  32. data/crates/kobako-runtime/src/filesystem/tmpfs/seed.rs +32 -0
  33. data/crates/kobako-runtime/src/filesystem/tmpfs/tmpfile.rs +3 -3
  34. data/crates/kobako-runtime/src/filesystem/tmpfs/whiteout.rs +57 -35
  35. data/crates/kobako-runtime/src/filesystem/tmpfs/xattr.rs +2 -2
  36. data/crates/kobako-runtime/src/filesystem/tmpfs.rs +172 -160
  37. data/crates/kobako-runtime/src/filesystem/wrap.rs +36 -52
  38. data/crates/kobako-runtime/src/filesystem.rs +6 -3
  39. data/crates/kobako-runtime/src/fork.rs +96 -0
  40. data/crates/kobako-runtime/src/grant.rs +3 -0
  41. data/crates/kobako-runtime/src/hashtable.rs +4 -2
  42. data/crates/kobako-runtime/src/lib.rs +18 -7
  43. data/crates/kobako-runtime/src/limits.rs +195 -0
  44. data/crates/kobako-runtime/src/locking.rs +10 -0
  45. data/crates/kobako-runtime/src/memcg.rs +383 -0
  46. data/crates/kobako-runtime/src/mount.rs +67 -9
  47. data/crates/kobako-runtime/src/net/backends.rs +43 -0
  48. data/crates/kobako-runtime/src/net/connections.rs +179 -0
  49. data/crates/kobako-runtime/src/net/credential.rs +125 -0
  50. data/crates/kobako-runtime/src/net/egress.rs +160 -0
  51. data/crates/kobako-runtime/src/net/family.rs +181 -0
  52. data/crates/kobako-runtime/src/net/http.rs +2733 -0
  53. data/crates/kobako-runtime/src/net/lookup.rs +285 -0
  54. data/crates/kobako-runtime/src/net/mock.rs +251 -0
  55. data/crates/kobako-runtime/src/net/namespace.rs +1366 -0
  56. data/crates/kobako-runtime/src/net/reactor.rs +531 -0
  57. data/crates/kobako-runtime/src/net/resolv/darwin.rs +275 -0
  58. data/crates/kobako-runtime/src/net/resolv/stub/tests.rs +469 -0
  59. data/crates/kobako-runtime/src/net/resolv/stub.rs +536 -0
  60. data/crates/kobako-runtime/src/net/resolv/windows.rs +248 -0
  61. data/crates/kobako-runtime/src/net/resolv.rs +227 -0
  62. data/crates/kobako-runtime/src/net/socket.rs +870 -0
  63. data/crates/kobako-runtime/src/net/tcp.rs +1588 -0
  64. data/crates/kobako-runtime/src/net/tls.rs +58 -0
  65. data/crates/kobako-runtime/src/net/transport.rs +279 -0
  66. data/crates/kobako-runtime/src/net/udp.rs +1003 -0
  67. data/crates/kobako-runtime/src/net.rs +42 -0
  68. data/crates/kobako-runtime/src/network/builder.rs +177 -0
  69. data/crates/kobako-runtime/src/network/ruleset.rs +777 -0
  70. data/crates/kobako-runtime/src/network.rs +551 -0
  71. data/crates/kobako-runtime/src/policy.rs +16 -0
  72. data/crates/kobako-runtime/src/profile.rs +29 -144
  73. data/crates/kobako-runtime/src/runtime.rs +3 -5
  74. data/crates/kobako-runtime/src/sched.rs +130 -29
  75. data/crates/kobako-runtime/src/security/entry_policy.rs +22 -0
  76. data/crates/kobako-runtime/src/{vfs/cred.rs → security/hooks.rs} +52 -144
  77. data/crates/kobako-runtime/src/security/mount_policy.rs +158 -0
  78. data/crates/kobako-runtime/src/security.rs +101 -196
  79. data/crates/kobako-runtime/src/snapshot.rs +121 -2
  80. data/crates/kobako-runtime/src/vfs/attr.rs +5 -5
  81. data/crates/kobako-runtime/src/vfs/dentry.rs +143 -89
  82. data/crates/kobako-runtime/src/vfs/file.rs +199 -203
  83. data/crates/kobako-runtime/src/vfs/inode.rs +41 -45
  84. data/crates/kobako-runtime/src/vfs/kern_path.rs +38 -42
  85. data/crates/kobako-runtime/src/vfs/mount.rs +10 -9
  86. data/crates/kobako-runtime/src/vfs/namei.rs +829 -700
  87. data/crates/kobako-runtime/src/vfs/namespace.rs +162 -0
  88. data/crates/kobako-runtime/src/vfs/open_files.rs +69 -0
  89. data/crates/kobako-runtime/src/vfs/openat2.rs +3 -3
  90. data/crates/kobako-runtime/src/vfs/path.rs +6 -6
  91. data/crates/kobako-runtime/src/vfs/readdir.rs +6 -6
  92. data/crates/kobako-runtime/src/vfs/stat.rs +159 -23
  93. data/crates/kobako-runtime/src/vfs/super_block.rs +18 -14
  94. data/crates/kobako-runtime/src/vfs/utimes.rs +37 -27
  95. data/crates/kobako-runtime/src/vfs.rs +9 -6
  96. data/crates/kobako-runtime/src/wait.rs +356 -0
  97. data/crates/kobako-transport/CHANGELOG.md +31 -0
  98. data/crates/kobako-transport/Cargo.toml +1 -1
  99. data/crates/kobako-transport/README.md +3 -4
  100. data/crates/kobako-transport/src/abi.rs +78 -72
  101. data/crates/kobako-transport/src/envelope/bytes.rs +17 -6
  102. data/crates/kobako-transport/src/envelope/call.rs +32 -4
  103. data/crates/kobako-transport/src/envelope/error_record.rs +21 -0
  104. data/crates/kobako-transport/src/envelope/name.rs +99 -0
  105. data/crates/kobako-transport/src/envelope/outcome.rs +9 -9
  106. data/crates/kobako-transport/src/envelope/preamble.rs +24 -21
  107. data/crates/kobako-transport/src/envelope/reply.rs +27 -27
  108. data/crates/kobako-transport/src/envelope/unsupported.rs +10 -10
  109. data/crates/kobako-transport/src/envelope.rs +13 -8
  110. data/crates/kobako-wasmtime/CHANGELOG.md +161 -0
  111. data/crates/kobako-wasmtime/Cargo.toml +26 -7
  112. data/crates/kobako-wasmtime/README.md +3 -2
  113. data/crates/kobako-wasmtime/build/preview2.rs +1339 -0
  114. data/crates/kobako-wasmtime/build.rs +16 -0
  115. data/crates/kobako-wasmtime/src/abi.rs +17 -37
  116. data/crates/kobako-wasmtime/src/ambient.rs +33 -18
  117. data/crates/kobako-wasmtime/src/cache.rs +23 -15
  118. data/crates/kobako-wasmtime/src/config.rs +52 -68
  119. data/crates/kobako-wasmtime/src/driver.rs +48 -63
  120. data/crates/kobako-wasmtime/src/exports.rs +143 -17
  121. data/crates/kobako-wasmtime/src/guest.rs +12 -6
  122. data/crates/kobako-wasmtime/src/guest_binary.rs +14 -0
  123. data/crates/kobako-wasmtime/src/imports.rs +42 -18
  124. data/crates/kobako-wasmtime/src/instance_pre.rs +4 -4
  125. data/crates/kobako-wasmtime/src/invocation.rs +74 -12
  126. data/crates/kobako-wasmtime/src/lib.rs +5 -3
  127. data/crates/kobako-wasmtime/src/limiter.rs +70 -5
  128. data/crates/kobako-wasmtime/src/preview1/descriptors.rs +309 -0
  129. data/crates/kobako-wasmtime/src/preview1/differential/mount.rs +5 -15
  130. data/crates/kobako-wasmtime/src/preview1/differential.rs +27 -31
  131. data/crates/kobako-wasmtime/src/preview1/errno.rs +55 -66
  132. data/crates/kobako-wasmtime/src/preview1/fd.rs +327 -296
  133. data/crates/kobako-wasmtime/src/preview1/memory.rs +21 -12
  134. data/crates/kobako-wasmtime/src/preview1/mount_view/wrap.rs +34 -53
  135. data/crates/kobako-wasmtime/src/preview1/mount_view.rs +231 -217
  136. data/crates/kobako-wasmtime/src/preview1/path.rs +56 -121
  137. data/crates/kobako-wasmtime/src/preview1/poll.rs +244 -120
  138. data/crates/kobako-wasmtime/src/preview1/sock.rs +10 -8
  139. data/crates/kobako-wasmtime/src/preview1/testsuite.rs +5 -15
  140. data/crates/kobako-wasmtime/src/preview1.rs +46 -42
  141. data/crates/kobako-wasmtime/src/preview2/abi.rs +227 -0
  142. data/crates/kobako-wasmtime/src/preview2/error.rs +118 -0
  143. data/crates/kobako-wasmtime/src/preview2/filesystem.rs +122 -0
  144. data/crates/kobako-wasmtime/src/preview2/host.rs +3959 -0
  145. data/crates/kobako-wasmtime/src/preview2/http.rs +1503 -0
  146. data/crates/kobako-wasmtime/src/preview2/io.rs +315 -0
  147. data/crates/kobako-wasmtime/src/preview2/sockets.rs +379 -0
  148. data/crates/kobako-wasmtime/src/preview2/table.rs +305 -0
  149. data/crates/kobako-wasmtime/src/preview2.rs +57 -0
  150. data/crates/kobako-wasmtime/src/trap.rs +24 -9
  151. data/crates/kobako-wasmtime/src/wasi.rs +210 -81
  152. data/crates/kobako-wasmtime/wit/deps/cli.wit +233 -0
  153. data/crates/kobako-wasmtime/wit/deps/clocks.wit +162 -0
  154. data/crates/kobako-wasmtime/wit/deps/filesystem.wit +589 -0
  155. data/crates/kobako-wasmtime/wit/deps/http.wit +733 -0
  156. data/crates/kobako-wasmtime/wit/deps/io.wit +299 -0
  157. data/crates/kobako-wasmtime/wit/deps/random.wit +92 -0
  158. data/crates/kobako-wasmtime/wit/deps/sockets.wit +949 -0
  159. data/data/kobako.wasm +0 -0
  160. data/ext/kobako/Cargo.toml +1 -1
  161. data/ext/kobako/src/{runtime/errors.rs → errors.rs} +34 -26
  162. data/ext/kobako/src/{runtime/escape.rs → escape.rs} +42 -11
  163. data/ext/kobako/src/{runtime/filesystem → filesystem}/grants.rs +30 -26
  164. data/ext/kobako/src/{runtime/filesystem → filesystem}/ruby.rs +149 -168
  165. data/ext/kobako/src/{runtime/filesystem.rs → filesystem.rs} +24 -15
  166. data/ext/kobako/src/gvl.rs +306 -0
  167. data/ext/kobako/src/lib.rs +64 -3
  168. data/ext/kobako/src/network/mock.rs +187 -0
  169. data/ext/kobako/src/network.rs +591 -0
  170. data/ext/kobako/src/runtime/bridge.rs +30 -29
  171. data/ext/kobako/src/runtime/snapshot.rs +4 -4
  172. data/ext/kobako/src/runtime.rs +221 -143
  173. data/ext/kobako/src/wire.rs +42 -0
  174. data/lib/kobako/catalog/extensions.rb +14 -2
  175. data/lib/kobako/catalog/flags.rb +3 -5
  176. data/lib/kobako/catalog/grants.rb +5 -4
  177. data/lib/kobako/catalog/handles.rb +5 -27
  178. data/lib/kobako/catalog/mount.rb +11 -3
  179. data/lib/kobako/catalog/network.rb +35 -0
  180. data/lib/kobako/catalog/replacements.rb +28 -0
  181. data/lib/kobako/catalog/services.rb +5 -19
  182. data/lib/kobako/catalog/snippets.rb +3 -18
  183. data/lib/kobako/catalog.rb +3 -0
  184. data/lib/kobako/codec/{utils.rb → boundary.rb} +4 -2
  185. data/lib/kobako/codec/decoder.rb +4 -4
  186. data/lib/kobako/codec/ext_types.rb +3 -3
  187. data/lib/kobako/codec/handle_walk.rb +3 -43
  188. data/lib/kobako/codec/value_budget.rb +132 -40
  189. data/lib/kobako/codec.rb +1 -1
  190. data/lib/kobako/context.rb +24 -11
  191. data/lib/kobako/errors.rb +15 -12
  192. data/lib/kobako/extension.rb +3 -2
  193. data/lib/kobako/filesystem.rb +9 -2
  194. data/lib/kobako/handle.rb +2 -2
  195. data/lib/kobako/names.rb +28 -0
  196. data/lib/kobako/network.rb +210 -0
  197. data/lib/kobako/outcome.rb +7 -7
  198. data/lib/kobako/pool.rb +45 -23
  199. data/lib/kobako/runtime.rb +3 -2
  200. data/lib/kobako/sandbox.rb +92 -35
  201. data/lib/kobako/sandbox_options.rb +41 -106
  202. data/lib/kobako/transport/argument_wrap.rb +64 -0
  203. data/lib/kobako/transport/block_failure.rb +1 -1
  204. data/lib/kobako/transport/dispatcher.rb +6 -6
  205. data/lib/kobako/transport/exposure.rb +26 -15
  206. data/lib/kobako/transport/reflection.rb +21 -4
  207. data/lib/kobako/transport/run.rb +8 -23
  208. data/lib/kobako/transport/yielder.rb +2 -9
  209. data/lib/kobako/transport.rb +1 -0
  210. data/lib/kobako/version.rb +1 -1
  211. data/lib/kobako.rb +6 -2
  212. data/release-please-config.json +15 -0
  213. data/sig/core/thread.rbs +6 -0
  214. data/sig/kobako/catalog/extensions.rbs +2 -0
  215. data/sig/kobako/catalog/grants.rbs +7 -4
  216. data/sig/kobako/catalog/handles.rbs +0 -2
  217. data/sig/kobako/catalog/mount.rbs +3 -1
  218. data/sig/kobako/catalog/network.rbs +20 -0
  219. data/sig/kobako/catalog/replacements.rbs +14 -0
  220. data/sig/kobako/catalog/services.rbs +0 -4
  221. data/sig/kobako/catalog/snippets.rbs +0 -2
  222. data/sig/kobako/codec/{utils.rbs → boundary.rbs} +2 -2
  223. data/sig/kobako/codec/handle_walk.rbs +0 -6
  224. data/sig/kobako/codec/value_budget.rbs +47 -4
  225. data/sig/kobako/context.rbs +3 -1
  226. data/sig/kobako/errors.rbs +4 -4
  227. data/sig/kobako/filesystem.rbs +21 -3
  228. data/sig/kobako/network.rbs +115 -0
  229. data/sig/kobako/outcome.rbs +1 -1
  230. data/sig/kobako/pool.rbs +7 -3
  231. data/sig/kobako/runtime.rbs +32 -3
  232. data/sig/kobako/sandbox.rbs +19 -2
  233. data/sig/kobako/sandbox_options.rbs +29 -38
  234. data/sig/kobako/transport/argument_wrap.rbs +15 -0
  235. data/sig/kobako/transport/error.rbs +1 -1
  236. data/sig/kobako/transport/exposure.rbs +2 -4
  237. data/sig/kobako/transport/reflection.rbs +10 -0
  238. data/sig/kobako/transport/run.rbs +0 -2
  239. data/sig/kobako/transport/yielder.rbs +0 -3
  240. data/sig/kobako/transport.rbs +1 -1
  241. data/sig/kobako.rbs +13 -0
  242. metadata +84 -35
  243. data/crates/kobako-runtime/src/endpoint/capture.rs +0 -115
  244. data/crates/kobako-runtime/src/endpoint/memory_cap.rs +0 -238
  245. data/crates/kobako-runtime/src/filesystem/path.rs +0 -21
  246. data/crates/kobako-runtime/src/vfs/errno.rs +0 -153
  247. data/crates/kobako-runtime/tests/linux_baseline/calls.rs +0 -171
  248. data/crates/kobako-runtime/tests/linux_baseline/concurrent.rs +0 -276
  249. data/crates/kobako-runtime/tests/linux_baseline/kobako.rs +0 -323
  250. data/crates/kobako-runtime/tests/linux_baseline/linux-baseline-expected.txt +0 -16
  251. data/crates/kobako-runtime/tests/linux_baseline/linux.rs +0 -449
  252. data/crates/kobako-runtime/tests/linux_baseline/main.rs +0 -611
  253. data/crates/kobako-runtime/tests/linux_baseline/permissions.rs +0 -236
  254. data/crates/kobako-wasmtime/src/descriptors.rs +0 -200
  255. data/crates/kobako-wasmtime/tests/integration/compile_posture.rs +0 -43
  256. data/crates/kobako-wasmtime/tests/integration/guest_file.rs +0 -1085
  257. data/crates/kobako-wasmtime/tests/integration/main.rs +0 -29
  258. data/crates/kobako-wasmtime/tests/integration/preamble_flags.rs +0 -70
  259. data/crates/kobako-wasmtime/tests/integration/snippet_compile.rs +0 -95
  260. data/crates/kobako-wasmtime/tests/integration/support/mod.rs +0 -30
  261. data/crates/kobako-wasmtime/tests/integration/unanswered_call.rs +0 -86
  262. data/crates/kobako-wasmtime/tests/integration/unsupported_call.rs +0 -51
  263. data/ext/kobako/src/runtime/gvl.rs +0 -147
  264. data/lib/kobako/constant_name.rb +0 -8
  265. data/sig/kobako/constant_name.rbs +0 -3
  266. /data/ext/kobako/src/{runtime/pinned_value.rs → pinned_value.rs} +0 -0
@@ -5,15 +5,18 @@
5
5
 
6
6
  use std::sync::{Arc, LazyLock, Mutex, PoisonError};
7
7
 
8
+ use crate::cred::Cred;
8
9
  use crate::vfs::{
9
- alloc_file_pseudo, Cred, Dentry, FMode, File, FileOperations, FileSystemType, FileType,
10
- FilesystemError, Inode, InodeOperations, Kiocb, Kstat, OpenHow, PollMask, SuperBlock, VfsMount,
10
+ alloc_file_pseudo, Dentry, FMode, File, FileOperations, FileSystemType, FileType, Inode,
11
+ InodeOperations, Kiocb, Kstat, OpenHow, PollMask, SuperBlock, VfsMount,
11
12
  };
13
+ use crate::wait::PollTable;
14
+ use crate::Errno;
12
15
 
13
16
  /// The one Filesystem every pipe stands on, as Linux's `pipe_mnt`. No path
14
17
  /// reaches it: a pipe is only ever open.
15
18
  static PIPE_MNT: LazyLock<Arc<VfsMount>> = LazyLock::new(|| {
16
- let sb = SuperBlock::new(Arc::new(PipeFs)).expect("pipefs fills in nothing that can fail");
19
+ let sb = SuperBlock::new(PipeFs).expect("pipefs fills in nothing that can fail");
17
20
  VfsMount::kern_mount(sb)
18
21
  });
19
22
 
@@ -21,14 +24,14 @@ static PIPE_MNT: LazyLock<Arc<VfsMount>> = LazyLock::new(|| {
21
24
  struct PipeFs;
22
25
 
23
26
  impl FileSystemType for PipeFs {
24
- fn fill_super(self: Arc<Self>, sb: &mut SuperBlock) -> Result<(), FilesystemError> {
27
+ fn fill_super(self: Arc<Self>, sb: &mut SuperBlock) -> Result<(), Errno> {
25
28
  sb.d_make_root(Inode::new(&Kstat::new(FileType::Dir, 0), self));
26
29
  Ok(())
27
30
  }
28
31
  }
29
32
 
30
33
  impl InodeOperations for PipeFs {
31
- fn getattr(&self, _dentry: &Dentry) -> Result<Kstat, FilesystemError> {
34
+ fn getattr(&self, _dentry: &Dentry) -> Result<Kstat, Errno> {
32
35
  Ok(Kstat::new(FileType::Fifo, 0))
33
36
  }
34
37
  }
@@ -82,7 +85,7 @@ impl FileOperations for AnonPipe {
82
85
  /// A pipe has no offsets, so a read takes what comes next. A drained pipe
83
86
  /// reads nothing, as a closed writer's does, and so does a read into no
84
87
  /// room, as `pipe_read` answers one.
85
- fn read_iter(&mut self, _iocb: &mut Kiocb, buf: &mut [u8]) -> Result<usize, FilesystemError> {
88
+ fn read_iter(&mut self, _iocb: &mut Kiocb, buf: &mut [u8]) -> Result<usize, Errno> {
86
89
  let mut state = self.0.state();
87
90
  let start = state.read;
88
91
  let rest = &state.bytes[start..];
@@ -95,19 +98,19 @@ impl FileOperations for AnonPipe {
95
98
  /// A pipe has no offsets, so a write lands after the last. A write past
96
99
  /// the capacity keeps what fit and answers a failure, which is how a
97
100
  /// capture one past its limit learns it was truncated.
98
- fn write_iter(&mut self, _iocb: &mut Kiocb, buf: &[u8]) -> Result<usize, FilesystemError> {
101
+ fn write_iter(&mut self, _iocb: &mut Kiocb, buf: &[u8]) -> Result<usize, Errno> {
99
102
  let mut state = self.0.state();
100
103
  let take = buf.len().min(self.0.capacity - state.bytes.len());
101
104
  state.bytes.extend_from_slice(&buf[..take]);
102
105
  if take < buf.len() {
103
- return Err(FilesystemError::Other("the pipe is full".to_string()));
106
+ return Err(Errno::Other("the pipe is full".to_string()));
104
107
  }
105
108
  Ok(take)
106
109
  }
107
110
 
108
111
  /// Each end is ready for what it was opened for, as `pipe_poll` reads
109
112
  /// `f_mode`; nothing here waits for the other end.
110
- fn poll(&self, mode: FMode) -> PollMask {
113
+ fn poll(&self, mode: FMode, _table: Option<&mut PollTable>) -> PollMask {
111
114
  match mode.contains(FMode::READ) {
112
115
  true => PollMask::IN,
113
116
  false => PollMask::OUT,
@@ -2,9 +2,10 @@
2
2
  //! goes on, as 9p's `fid->rdir` holds what its server sent, so a caller reading in
3
3
  //! small steps costs one listing, not one per step.
4
4
 
5
- use std::sync::Arc;
6
-
7
- use crate::vfs::{Cred, Dentry, DirContext, DirEntry, FileType, FilesystemError};
5
+ use crate::cred::Cred;
6
+ use crate::memcg::MemoryCharge;
7
+ use crate::vfs::{Dentry, DirContext, DirEntry, FileType};
8
+ use crate::Errno;
8
9
 
9
10
  /// The entries a listing began with, `.` and `..` first, on the open directory
10
11
  /// until it reaches its end, counted against who holds them.
@@ -14,32 +15,14 @@ pub struct Rdir(Option<Listing>);
14
15
  struct Listing {
15
16
  entries: Vec<DirEntry>,
16
17
  /// Gives the entries' bytes back once the listing goes.
17
- _charge: Charge,
18
- }
19
-
20
- /// Bytes a listing counts for as long as it holds them, as shmem pairs
21
- /// `shmem_acct_size` with `shmem_unacct_size`.
22
- struct Charge {
23
- cred: Arc<Cred>,
24
- bytes: usize,
18
+ _charge: MemoryCharge,
25
19
  }
26
20
 
27
- impl Charge {
28
- fn account(cred: &Arc<Cred>, entries: &[DirEntry]) -> Result<Self, FilesystemError> {
29
- let names: usize = entries.iter().map(|entry| entry.name.capacity()).sum();
30
- let bytes = size_of_val(entries) + names;
31
- cred.security_vm_enough_memory(bytes)?;
32
- Ok(Self {
33
- cred: cred.clone(),
34
- bytes,
35
- })
36
- }
37
- }
38
-
39
- impl Drop for Charge {
40
- fn drop(&mut self) {
41
- self.cred.vm_unacct_memory(self.bytes);
42
- }
21
+ /// Charge the bytes `entries` hold to `cred` for as long as the listing holds
22
+ /// them.
23
+ fn charge(cred: &Cred, entries: &[DirEntry]) -> Result<MemoryCharge, Errno> {
24
+ let names: usize = entries.iter().map(|entry| entry.name.capacity()).sum();
25
+ MemoryCharge::new(cred.memory_budget(), size_of_val(entries) + names)
43
26
  }
44
27
 
45
28
  impl Rdir {
@@ -52,12 +35,12 @@ impl Rdir {
52
35
  /// listing ends, as `ovl_cache_get` and `v9fs_alloc_rdir_buf` allocate before
53
36
  /// the first `dir_emit`: past what a security module allows, the listing
54
37
  /// answers out of memory before it hands any entry.
55
- pub fn iterate(
38
+ pub fn iterate_shared(
56
39
  &mut self,
57
40
  dentry: &Dentry,
58
41
  ctx: &mut DirContext<'_>,
59
- read: impl FnOnce() -> Result<Vec<DirEntry>, FilesystemError>,
60
- ) -> Result<(), FilesystemError> {
42
+ read: impl FnOnce() -> Result<Vec<DirEntry>, Errno>,
43
+ ) -> Result<(), Errno> {
61
44
  let listing = match self.0.take().filter(|_| ctx.pos() != 0) {
62
45
  Some(held) => held,
63
46
  None => {
@@ -65,7 +48,7 @@ impl Rdir {
65
48
  let mut entries = dots(dentry)?;
66
49
  entries.extend(read);
67
50
  entries.shrink_to_fit();
68
- let _charge = Charge::account(ctx.cred(), &entries)?;
51
+ let _charge = charge(ctx.cred(), &entries)?;
69
52
  Listing { entries, _charge }
70
53
  }
71
54
  };
@@ -78,7 +61,7 @@ impl Rdir {
78
61
 
79
62
  /// `.` and `..` as a stat of `dentry` and of what stands above it reports them,
80
63
  /// `..` naming `dentry` itself at the root.
81
- fn dots(dentry: &Dentry) -> Result<Vec<DirEntry>, FilesystemError> {
64
+ fn dots(dentry: &Dentry) -> Result<Vec<DirEntry>, Errno> {
82
65
  let dot = dentry.getattr()?;
83
66
  let dotdot = dentry.parent().getattr()?;
84
67
  Ok(vec![
@@ -0,0 +1,32 @@
1
+ //! The paths a Host App seeds a Tmpfs with, components joined by `/` and the
2
+ //! empty path being the root.
3
+
4
+ use super::SeedError;
5
+
6
+ /// The last name of `path`, within its parent.
7
+ pub(super) fn name(path: &str) -> &str {
8
+ path.rsplit_once('/').map_or(path, |(_, name)| name)
9
+ }
10
+
11
+ /// Every directory above `path`, outermost first, the root left out.
12
+ pub(super) fn ancestors(path: &str) -> impl Iterator<Item = &str> {
13
+ path.match_indices('/').map(move |(end, _)| &path[..end])
14
+ }
15
+
16
+ /// `path` in the form a Tmpfs holds it: no leading `/`, and no empty or
17
+ /// `.` component. The root, and a `..` that would leave the Tmpfs, are
18
+ /// refused.
19
+ pub(super) fn normalize(path: &str) -> Result<String, SeedError> {
20
+ let path = path
21
+ .split('/')
22
+ .filter(|component| !component.is_empty() && *component != ".")
23
+ .collect::<Vec<_>>()
24
+ .join("/");
25
+ if path.is_empty() {
26
+ return Err(SeedError::Root);
27
+ }
28
+ if path.split('/').any(|name| name == "..") {
29
+ return Err(SeedError::DotDot(path));
30
+ }
31
+ Ok(path)
32
+ }
@@ -2,13 +2,13 @@
2
2
  //! `shmem_tmpfile` and `linkat` let overlayfs copy a file up unseen, and
3
3
  //! the times set on what it copies.
4
4
 
5
- use super::super::{FilesystemError, Iattr};
5
+ use super::super::{Errno, Iattr};
6
6
  use super::{lock, Content, Node, Tmpfs, TmpfsFile};
7
7
 
8
8
  impl Tmpfs {
9
9
  /// An empty file with no name yet, as `O_TMPFILE` opens one. Dropped
10
10
  /// unnamed, it gives its room back.
11
- pub(crate) fn tmpfile(&self) -> Result<TmpfsFile, FilesystemError> {
11
+ pub(crate) fn tmpfile(&self) -> Result<TmpfsFile, Errno> {
12
12
  Ok(TmpfsFile {
13
13
  data: Content::create(&self.tree)?,
14
14
  })
@@ -16,7 +16,7 @@ impl Tmpfs {
16
16
 
17
17
  /// Name `file` `name` in this directory, as `simple_link` names a file
18
18
  /// `O_TMPFILE` opened once `vfs_link` found the name free.
19
- pub(crate) fn link(&self, file: &TmpfsFile, name: &str) -> Result<(), FilesystemError> {
19
+ pub(crate) fn link(&self, file: &TmpfsFile, name: &str) -> Result<(), Errno> {
20
20
  self.dir.state().insert(name, Node::File(file.data.clone()));
21
21
  Ok(())
22
22
  }
@@ -3,16 +3,39 @@
3
3
  //! `shmem_rename2` takes `RENAME_NOREPLACE`, `RENAME_EXCHANGE`, and
4
4
  //! `RENAME_WHITEOUT`.
5
5
 
6
- use std::sync::Arc;
6
+ use std::sync::atomic::Ordering;
7
+ use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
7
8
 
8
- use super::super::FilesystemError;
9
- use super::{Node, Space, Tmpfs};
10
- use crate::vfs::RenameFlag;
9
+ use super::super::Errno;
10
+ use super::{Node, Space, Times, Tmpfs};
11
+ use crate::vfs::{FileType, Kstat, RenameFlag};
11
12
 
12
13
  /// A whiteout, counting one inode for as long as it stands, as the
13
- /// character device tmpfs makes for one does.
14
+ /// character device `shmem_mknod` makes for one does: an inode of its own,
15
+ /// with its own times.
14
16
  pub(super) struct Whiteout {
15
17
  space: Arc<Space>,
18
+ dev: u64,
19
+ ino: u64,
20
+ times: Mutex<Times>,
21
+ }
22
+
23
+ impl Whiteout {
24
+ pub(super) fn getattr(&self) -> Kstat {
25
+ self.times().report(
26
+ Kstat::new(FileType::Chrdev, 0)
27
+ .with_identity(self.dev, self.ino)
28
+ .with_nlink(1),
29
+ )
30
+ }
31
+
32
+ pub(super) fn ino(&self) -> u64 {
33
+ self.ino
34
+ }
35
+
36
+ pub(super) fn times(&self) -> MutexGuard<'_, Times> {
37
+ self.times.lock().unwrap_or_else(PoisonError::into_inner)
38
+ }
16
39
  }
17
40
 
18
41
  impl Drop for Whiteout {
@@ -22,64 +45,63 @@ impl Drop for Whiteout {
22
45
  }
23
46
 
24
47
  impl Tmpfs {
25
- fn new_whiteout(&self) -> Result<Node, FilesystemError> {
48
+ fn new_whiteout(&self) -> Result<Node, Errno> {
26
49
  self.tree.space.inodes.take(1)?;
27
- Ok(Node::Whiteout {
28
- _inode: Arc::new(Whiteout {
29
- space: self.tree.space.clone(),
30
- }),
31
- })
50
+ Ok(Node::Whiteout(Arc::new(Whiteout {
51
+ space: self.tree.space.clone(),
52
+ dev: self.tree.dev,
53
+ ino: self.tree.next_ino.fetch_add(1, Ordering::Relaxed),
54
+ times: Mutex::new(Times::now()),
55
+ })))
32
56
  }
33
57
 
34
58
  /// Whether a whiteout stands at `name`, as overlayfs's
35
59
  /// `ovl_is_whiteout` reads a character device 0:0.
36
60
  pub(crate) fn is_whiteout(&self, name: &str) -> bool {
37
- matches!(self.dir.get(name), Ok(Node::Whiteout { .. }))
61
+ matches!(self.dir.get(name), Ok(Node::Whiteout(_)))
38
62
  }
39
63
 
40
64
  /// Make a whiteout at `name`, as `shmem_mknod` makes the character
41
65
  /// device 0:0 `vfs_whiteout` asks for.
42
- pub(crate) fn whiteout(&self, name: &str) -> Result<(), FilesystemError> {
66
+ pub(crate) fn whiteout(&self, name: &str) -> Result<(), Errno> {
43
67
  let whiteout = self.new_whiteout()?;
44
68
  self.dir.state().insert(name, whiteout);
45
69
  Ok(())
46
70
  }
47
71
 
48
- /// Move `from` to `to` in `target` as `shmem_rename2` does with
49
- /// `flag`, the entry taking all it holds, so no name beneath it
50
- /// changes. Without one, what stands at `to` goes as rename(2)
72
+ /// Move `old_name` to `new_name` in `target` as `shmem_rename2` does with
73
+ /// `flags`, the entry taking all it holds, so no name beneath it
74
+ /// changes. Without one, what stands at `new_name` goes as rename(2)
51
75
  /// replaces it, once the VFS found the move fits what stands on each
52
76
  /// side.
53
77
  pub(super) fn rename_at(
54
78
  &self,
55
- from: &str,
79
+ old_name: &str,
56
80
  target: &Tmpfs,
57
- to: &str,
58
- flag: Option<RenameFlag>,
59
- ) -> Result<(), FilesystemError> {
60
- let (from_dir, to_dir) = (&self.dir, &target.dir);
61
- let moving = from_dir.get(from)?;
62
- let replaced = to_dir.get(to).ok();
63
- if flag == Some(RenameFlag::Exchange) {
64
- let replaced = replaced.ok_or(FilesystemError::NotFound)?;
65
- from_dir.state().exchange(from, replaced);
66
- to_dir.state().exchange(to, moving);
81
+ new_name: &str,
82
+ flags: Option<RenameFlag>,
83
+ ) -> Result<(), Errno> {
84
+ let (old_dir, new_dir) = (&self.dir, &target.dir);
85
+ let moving = old_dir.get(old_name)?;
86
+ let replaced = new_dir.get(new_name).ok();
87
+ if flags == Some(RenameFlag::Exchange) {
88
+ let replaced = replaced.ok_or(Errno::NotFound)?;
89
+ old_dir.state().exchange(old_name, replaced);
90
+ new_dir.state().exchange(new_name, moving);
67
91
  return Ok(());
68
92
  }
69
- if let Some(Node::Directory(dir)) = &replaced {
70
- if !dir.state().entries.is_empty() {
71
- return Err(FilesystemError::DirectoryNotEmpty);
72
- }
93
+ if let Some(replaced) = &replaced {
94
+ replaced.ensure_empty()?;
73
95
  }
74
- let left = match flag {
96
+ let left = match flags {
75
97
  Some(RenameFlag::Whiteout) => Some(self.new_whiteout()?),
76
98
  _ => None,
77
99
  };
78
100
  match left {
79
- Some(left) => from_dir.state().exchange(from, left),
80
- None => from_dir.state().take(from),
101
+ Some(left) => old_dir.state().exchange(old_name, left),
102
+ None => old_dir.state().take(old_name),
81
103
  }
82
- to_dir.state().insert(to, moving);
104
+ new_dir.state().insert(new_name, moving);
83
105
  Ok(())
84
106
  }
85
107
  }
@@ -1,7 +1,7 @@
1
1
  //! A directory's extended attributes, as tmpfs keeps the `trusted.*`
2
2
  //! ones overlayfs marks a directory opaque with.
3
3
 
4
- use super::super::FilesystemError;
4
+ use super::super::Errno;
5
5
  use super::Tmpfs;
6
6
 
7
7
  impl Tmpfs {
@@ -12,7 +12,7 @@ impl Tmpfs {
12
12
  }
13
13
 
14
14
  /// Set the attribute `name` on this directory, as `setxattr` does.
15
- pub(crate) fn setxattr(&self, name: &str, value: &[u8]) -> Result<(), FilesystemError> {
15
+ pub(crate) fn setxattr(&self, name: &str, value: &[u8]) -> Result<(), Errno> {
16
16
  self.dir
17
17
  .state()
18
18
  .xattrs