starry-kernel 0.7.7

A Linux-compatible OS kernel built on ArceOS unikernel
Documentation
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use alloc::{borrow::Cow, string::String, sync::Arc, vec::Vec};
use core::{any::Any, cmp::Ordering, task::Context};

use ax_sync::Mutex;
use axfs_ng_vfs::{
    FileNodeOps, FilesystemOps, FsIoEvents, FsPollable, Metadata, MetadataUpdate, NodeFlags,
    NodeOps, NodePermission, NodeType, VfsError, VfsResult,
};
use axpoll::{IoEvents, Pollable};
use inherit_methods_macro::inherit_methods;

use super::fs::{SimpleFs, SimpleFsNode};

fn fs_events_to_io(events: FsIoEvents) -> IoEvents {
    IoEvents::from_bits_truncate(events.bits())
}

fn io_events_to_fs(events: IoEvents) -> FsIoEvents {
    FsIoEvents::from_bits_truncate(events.bits())
}

/// Operations for a simple file.
pub trait SimpleFileOps: Send + Sync + 'static {
    /// Reads all content in the file.
    fn read_all(&self) -> VfsResult<Cow<'_, [u8]>>;
    /// Replaces the file's content with `data`.
    fn write_all(&self, _data: &[u8]) -> VfsResult<()> {
        Err(VfsError::BadFileDescriptor)
    }
}

/// Type representing operation applied to a simple file.
pub enum SimpleFileOperation<'a> {
    /// Reading the file's content
    Read,
    /// Replacing the file's content
    Write(&'a [u8]),
}

/// A wrapper that implements [`SimpleFileOps`] for `Fn(SimpleFileOperation) ->
/// VfsResult<Option<impl Into<Vec<u8>>>>`.
pub struct RwFile<F>(F);

impl<F, R> RwFile<F>
where
    F: Fn(SimpleFileOperation) -> VfsResult<Option<R>> + Send + Sync,
    R: Into<Vec<u8>>,
{
    /// Creates a new `RwFile`.
    pub fn new(imp: F) -> Self {
        Self(imp)
    }
}

impl<F, R> SimpleFileOps for RwFile<F>
where
    F: Fn(SimpleFileOperation) -> VfsResult<Option<R>> + Send + Sync + 'static,
    R: Into<Vec<u8>>,
{
    fn read_all(&self) -> VfsResult<Cow<'_, [u8]>> {
        (self.0)(SimpleFileOperation::Read).map(|it| Cow::Owned(it.unwrap().into()))
    }

    fn write_all(&self, data: &[u8]) -> VfsResult<()> {
        (self.0)(SimpleFileOperation::Write(data)).map(|_| ())
    }
}

pub trait SimpleFileContent {
    /// Converts the content into bytes.
    fn into_content(self) -> Cow<'static, [u8]>;
}

impl SimpleFileContent for Vec<u8> {
    fn into_content(self) -> Cow<'static, [u8]> {
        Cow::Owned(self)
    }
}

impl SimpleFileContent for String {
    fn into_content(self) -> Cow<'static, [u8]> {
        Cow::Owned(self.into_bytes())
    }
}

impl SimpleFileContent for &'static str {
    fn into_content(self) -> Cow<'static, [u8]> {
        Cow::Borrowed(self.as_bytes())
    }
}

impl SimpleFileContent for &'static [u8] {
    fn into_content(self) -> Cow<'static, [u8]> {
        Cow::Borrowed(self)
    }
}

impl<F, R> SimpleFileOps for F
where
    F: Fn() -> VfsResult<R> + Send + Sync + 'static,
    R: SimpleFileContent,
{
    fn read_all(&self) -> VfsResult<Cow<'_, [u8]>> {
        Ok((self)()?.into_content())
    }
}

/// A simple file.
pub struct SimpleFile {
    node: SimpleFsNode,
    ops: Arc<dyn SimpleFileOps>,
}

impl SimpleFile {
    /// Creates a simple file from given file operations.
    pub fn new(fs: Arc<SimpleFs>, ty: NodeType, ops: impl SimpleFileOps) -> Arc<Self> {
        let node = SimpleFsNode::new(fs, ty, NodePermission::default());
        Arc::new(Self {
            node,
            ops: Arc::new(ops),
        })
    }

    /// Creates a simple file from given file operations.
    pub fn new_regular(fs: Arc<SimpleFs>, ops: impl SimpleFileOps) -> Arc<Self> {
        Self::new(fs, NodeType::RegularFile, ops)
    }

    /// Overwrite the node's stored ownership, permission bits and timestamps.
    /// Pseudo-filesystems that back a real kernel object (e.g. `/dev/mqueue`,
    /// whose files carry the owning queue's `i_mode`/`i_uid`/`i_gid` and inode
    /// times) use this to report those instead of the defaults. The node's
    /// `size` still comes from the live content length.
    pub fn set_attrs(
        &self,
        mode: NodePermission,
        uid: u32,
        gid: u32,
        atime: core::time::Duration,
        mtime: core::time::Duration,
        ctime: core::time::Duration,
    ) {
        let mut metadata = self.node.metadata.lock();
        metadata.mode = mode;
        metadata.uid = uid;
        metadata.gid = gid;
        metadata.atime = atime;
        metadata.mtime = mtime;
        metadata.ctime = ctime;
    }

    /// Report a fixed `st_size` from `stat` instead of the live content length.
    /// For pseudo files that mirror a kernel object whose inode size is a fixed
    /// documented width (e.g. `/dev/mqueue/<name>` = `FILENT_SIZE` 80), so
    /// `stat` matches Linux regardless of the current status-line length.
    ///
    /// Stored on the node's metadata because `stat` reads the size through
    /// [`SimpleFsNode::metadata`], which now honors a non-zero stored size
    /// instead of always recomputing from the live content length.
    pub fn set_fixed_size(&self, size: u64) {
        self.node.metadata.lock().size = size;
    }
}

#[inherit_methods(from = "self.node")]
impl NodeOps for SimpleFile {
    fn inode(&self) -> u64;

    fn metadata(&self) -> VfsResult<Metadata>;

    fn update_metadata(&self, update: MetadataUpdate) -> VfsResult<()>;

    fn filesystem(&self) -> &dyn FilesystemOps;

    fn sync(&self, data_only: bool) -> VfsResult<()>;

    fn into_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
        self
    }

    fn len(&self) -> VfsResult<u64> {
        Ok(self.ops.read_all()?.len() as u64)
    }

    fn flags(&self) -> NodeFlags {
        NodeFlags::NON_CACHEABLE
    }
}

impl FileNodeOps for SimpleFile {
    fn read_at(&self, buf: &mut [u8], offset: u64) -> VfsResult<usize> {
        let data = self.ops.read_all()?;
        if offset >= data.len() as u64 {
            return Ok(0);
        }
        let data = &data[offset as usize..];
        let read = data.len().min(buf.len());
        buf[..read].copy_from_slice(&data[..read]);
        Ok(read)
    }

    fn write_at(&self, buf: &[u8], offset: u64) -> VfsResult<usize> {
        let data = self.ops.read_all()?;
        if offset == 0 && buf.len() >= data.len() {
            self.ops.write_all(buf)?;
            return Ok(buf.len());
        }
        let mut data = data.to_vec();
        let end_pos = offset + buf.len() as u64;
        if end_pos > data.len() as u64 {
            data.resize(end_pos as usize, 0);
        }
        data[offset as usize..end_pos as usize].copy_from_slice(buf);
        self.ops.write_all(&data)?;
        Ok(buf.len())
    }

    fn append(&self, buf: &[u8]) -> VfsResult<(usize, u64)> {
        let mut data = self.ops.read_all()?.to_vec();
        data.extend_from_slice(buf);
        self.ops.write_all(&data)?;
        Ok((buf.len(), data.len() as u64))
    }

    fn set_len(&self, len: u64) -> VfsResult<()> {
        let data = self.ops.read_all()?;
        match len.cmp(&(data.len() as u64)) {
            Ordering::Less => self.ops.write_all(&data[..len as usize]),
            Ordering::Greater => {
                let mut data = data.to_vec();
                data.resize(len as usize, 0);
                self.ops.write_all(&data)
            }
            _ => Ok(()),
        }
    }

    fn set_symlink(&self, target: &str) -> VfsResult<()> {
        self.ops.write_all(target.as_bytes())
    }
}

impl FsPollable for SimpleFile {
    fn poll(&self) -> FsIoEvents {
        FsIoEvents::IN | FsIoEvents::OUT
    }

    fn register(&self, _context: &mut Context<'_>, _events: FsIoEvents) {}
}

impl Pollable for SimpleFile {
    fn poll(&self) -> IoEvents {
        fs_events_to_io(FsPollable::poll(self))
    }

    fn register(&self, context: &mut Context<'_>, events: IoEvents) {
        FsPollable::register(self, context, io_events_to_fs(events));
    }
}

/// A special file that directly implements file operations without caching content in the kernel.
/// It is used for files in procfs and debugfs that need to reflect real-time data.
pub struct SpecialFsFile<T: DirectRwFsFileOps> {
    node: SimpleFsNode,
    ops: Arc<T>,
}

pub trait DirectRwFsFileOps: Send + Sync + 'static {
    /// Reads a number of bytes starting from a given offset.
    fn read_at(&self, buf: &mut [u8], offset: u64) -> VfsResult<usize>;
    /// Writes a number of bytes starting from a given offset.
    fn write_at(&self, _buf: &[u8], _offset: u64) -> VfsResult<usize> {
        Err(VfsError::InvalidInput)
    }
}

impl<T: DirectRwFsFileOps> SpecialFsFile<T> {
    /// Creates a file from given file object and specified permissions.
    pub fn new_with_perm(
        fs: Arc<SimpleFs>,
        ty: NodeType,
        obj: T,
        perm: NodePermission,
    ) -> Arc<Self> {
        let node = SimpleFsNode::new(fs, ty, perm);
        Arc::new(Self {
            node,
            ops: Arc::new(obj),
        })
    }

    /// Creates a regular file from given file operations object and specified permissions.
    pub fn new_regular_with_perm(fs: Arc<SimpleFs>, obj: T, perm: NodePermission) -> Arc<Self> {
        Self::new_with_perm(fs, NodeType::RegularFile, obj, perm)
    }
}

#[inherit_methods(from = "self.node")]
impl<T: DirectRwFsFileOps> NodeOps for SpecialFsFile<T> {
    fn inode(&self) -> u64;

    fn metadata(&self) -> VfsResult<Metadata>;

    fn update_metadata(&self, update: MetadataUpdate) -> VfsResult<()>;

    fn filesystem(&self) -> &dyn FilesystemOps;

    fn sync(&self, data_only: bool) -> VfsResult<()>;

    fn into_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
        self
    }

    fn len(&self) -> VfsResult<u64> {
        Ok(0)
    }

    fn flags(&self) -> NodeFlags {
        NodeFlags::NON_CACHEABLE
    }
}

impl<T: DirectRwFsFileOps> FsPollable for SpecialFsFile<T> {
    fn poll(&self) -> FsIoEvents {
        // TODO: support poll for special files when needed
        FsIoEvents::IN | FsIoEvents::OUT
    }

    fn register(&self, _context: &mut Context<'_>, _events: FsIoEvents) {
        // SpecialFsFile reports itself as always-ready via `poll()` (IN|OUT),
        // so registration is a no-op. Matches `SimpleFile::register` above —
        // turning this into `unimplemented!()` was a regression that panicked
        // the kernel on any `epoll_ctl` against debugfs/procfs special files
        // (tracepoint trace_pipe, saved_cmdlines, dyn_debug controls, …).
    }
}

impl<T: DirectRwFsFileOps> Pollable for SpecialFsFile<T> {
    fn poll(&self) -> IoEvents {
        fs_events_to_io(FsPollable::poll(self))
    }

    fn register(&self, context: &mut Context<'_>, events: IoEvents) {
        FsPollable::register(self, context, io_events_to_fs(events));
    }
}

impl<T: DirectRwFsFileOps> FileNodeOps for SpecialFsFile<T> {
    fn read_at(&self, buf: &mut [u8], offset: u64) -> VfsResult<usize> {
        self.ops.read_at(buf, offset)
    }

    fn write_at(&self, buf: &[u8], offset: u64) -> VfsResult<usize> {
        self.ops.write_at(buf, offset)
    }

    fn append(&self, buf: &[u8]) -> VfsResult<(usize, u64)> {
        let w = self.ops.write_at(buf, 0)?;
        Ok((w, 0))
    }

    fn set_len(&self, len: u64) -> VfsResult<()> {
        if len == 0 {
            // Shell redirection usually opens these files with O_TRUNC.
            return Ok(());
        }
        Err(VfsError::InvalidInput)
    }

    fn set_symlink(&self, _target: &str) -> VfsResult<()> {
        Err(VfsError::InvalidInput)
    }
}

// TODO: create a linux like seq file that supports iterating content in chunks instead of reading all content at once, to avoid large memory usage for large files.
/// A Sequential file, which only supports reading all content. It is used for procfs and sysfs.
pub struct SeqObject {
    ops: Arc<dyn SimpleFileOps>,
    content_cache: Mutex<Option<Vec<u8>>>,
}

impl DirectRwFsFileOps for SeqObject {
    fn read_at(&self, buf: &mut [u8], offset: u64) -> VfsResult<usize> {
        let mut cache = self.content_cache.lock();
        if cache.is_none() || offset == 0 {
            let content = self.ops.read_all()?;
            *cache = Some(content.into_owned());
        }

        let data = cache.as_ref().unwrap();
        if offset >= data.len() as u64 {
            return Ok(0);
        }
        let data = &data[offset as usize..];
        let read = data.len().min(buf.len());
        buf[..read].copy_from_slice(&data[..read]);
        Ok(read)
    }
}

impl SeqObject {
    /// Creates a new `SeqObject` instance with given file operations.
    /// Now, we just reuse `SimpleFileOps` for simplicity, but we will likely
    /// need a separate trait for `SeqObject` in the future when we want to support
    /// more features like iterating content.
    pub fn new(ops: impl SimpleFileOps) -> Self {
        Self {
            content_cache: Mutex::new(None),
            ops: Arc::new(ops),
        }
    }
}