summaryrefslogtreecommitdiff
path: root/na/fs/ramfs/file.cc
blob: 6d9a0707e71d2186f721b9cf305ffa1106a433df (plain)
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
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* Copyright (C) 2026 Richard Qin */

/*
 * ramfs/file.cc — RamFS file operations (read/write via in-memory pages)
 *
 * Phase 1: simple flat buffer per-inode via i_private.
 * Will migrate to page_cache integration later.
 */

#include <lume/fs.h>
#include <lume/file.h>
#include <lume/dcache.h>
#include <lume/slab.h>
#include <lume/errno.h>
#include <lume/fs_types.h>

/*
 * Simplest possible in-memory file storage:
 * i_private points to a dynamically grown buffer.
 */
struct ramfs_file_data {
    char*   buf;
    uint64  capacity;
};

static ramfs_file_data* get_or_create_data(inode* ip)
{
    if (!ip->i_private) {
        auto* data = new ramfs_file_data{};
        data->buf = nullptr;
        data->capacity = 0;
        ip->i_private = data;
    }
    return static_cast<ramfs_file_data*>(ip->i_private);
}

static int ensure_capacity(ramfs_file_data* data, uint64 needed)
{
    if (needed <= data->capacity) return 0;

    /* Round up to page size */
    uint64 new_cap = (needed + 4095) & ~4095ULL;
    char* new_buf = new char[new_cap];
    if (!new_buf) return -ENOMEM;

    /* Copy old data */
    if (data->buf && data->capacity > 0) {
        for (uint64 i = 0; i < data->capacity; i++)
            new_buf[i] = data->buf[i];
    }
    /* Zero new region */
    for (uint64 i = data->capacity; i < new_cap; i++)
        new_buf[i] = 0;

    delete[] data->buf;
    data->buf = new_buf;
    data->capacity = new_cap;
    return 0;
}

/* ================================================================== */
/*  ramfs_read                                                        */
/* ================================================================== */

static int64 ramfs_read(file* fp, char* buf, uint64 count, int64* pos)
{
    inode* ip = file_inode(fp);
    int64 offset = *pos;

    if (offset < 0) return -EINVAL;
    if (offset >= ip->i_size) return 0;

    int64 avail = ip->i_size - offset;
    if (static_cast<int64>(count) > avail)
        count = static_cast<uint64>(avail);

    auto* data = static_cast<ramfs_file_data*>(ip->i_private);
    if (!data || !data->buf) return 0;

    /* Copy to user buffer.
     * TODO: use copy_to_user when called from syscall context */
    for (uint64 i = 0; i < count; i++)
        buf[i] = data->buf[offset + i];

    *pos += count;
    return static_cast<int64>(count);
}

/* ================================================================== */
/*  ramfs_write                                                       */
/* ================================================================== */

static int64 ramfs_write(file* fp, const char* buf, uint64 count, int64* pos)
{
    inode* ip = file_inode(fp);
    int64 offset = *pos;

    if (fp->f_flags & O_APPEND)
        offset = ip->i_size;

    if (offset < 0) return -EINVAL;

    auto* data = get_or_create_data(ip);
    uint64 end = static_cast<uint64>(offset) + count;

    int err = ensure_capacity(data, end);
    if (err) return err;

    /* Copy from user buffer */
    for (uint64 i = 0; i < count; i++)
        data->buf[offset + i] = buf[i];

    if (static_cast<int64>(end) > ip->i_size)
        ip->i_size = end;

    *pos = end;
    mark_inode_dirty(ip);
    return static_cast<int64>(count);
}

/* ================================================================== */
/*  ramfs_dir_iterate — getdents64                                    */
/* ================================================================== */

static int ramfs_dir_iterate(file* fp, dir_context* ctx)
{
    dentry* dir_de = file_dentry(fp);
    inode* dir_ip = file_inode(fp);
    int64 pos = ctx->pos;
    int count = 0;

    /* pos 0: emit "." */
    if (pos == 0) {
        if (!ctx->actor(ctx, ".", 1, pos, dir_ip->i_ino, DT_DIR))
            return 0;
        ctx->pos = 1;
        pos = 1;
        count++;
    }

    /* pos 1: emit ".." */
    if (pos == 1) {
        uint64 parent_ino = dir_de->d_parent->d_inode
            ? dir_de->d_parent->d_inode->i_ino : dir_ip->i_ino;
        if (!ctx->actor(ctx, "..", 2, pos, parent_ino, DT_DIR))
            return 0;
        ctx->pos = 2;
        pos = 2;
        count++;
    }

    /* pos >= 2: iterate children */
    int64 child_idx = 2;
    list_node* node;
    list_for_each(node, &dir_de->d_children) {
        dentry* child = list_entry(node, dentry, d_child);

        if (!child->d_inode)
            continue;  /* skip negative dentries */

        if (child_idx >= pos) {
            uint8 d_type = DT_UNKNOWN;
            if (S_ISDIR(child->d_inode->i_mode)) d_type = DT_DIR;
            else if (S_ISREG(child->d_inode->i_mode)) d_type = DT_REG;
            else if (S_ISCHR(child->d_inode->i_mode)) d_type = DT_CHR;
            else if (S_ISBLK(child->d_inode->i_mode)) d_type = DT_BLK;
            else if (S_ISLNK(child->d_inode->i_mode)) d_type = DT_LNK;
            else if (S_ISFIFO(child->d_inode->i_mode)) d_type = DT_FIFO;

            if (!ctx->actor(ctx, child->d_name.name,
                            static_cast<int>(child->d_name.len),
                            child_idx, child->d_inode->i_ino, d_type))
                return 0;

            ctx->pos = child_idx + 1;
            count++;
        }
        child_idx++;
    }

    return count;
}

extern file_operations ramfs_file_operations;
extern file_operations ramfs_dir_operations;

file_operations ramfs_file_operations;
file_operations ramfs_dir_operations;

void ramfs_file_ops_init()
{
    ramfs_file_operations.llseek         = nullptr;
    ramfs_file_operations.read           = ramfs_read;
    ramfs_file_operations.write          = ramfs_write;
    ramfs_file_operations.iterate_shared = nullptr;
    ramfs_file_operations.poll           = nullptr;
    ramfs_file_operations.unlocked_ioctl = nullptr;
    ramfs_file_operations.mmap           = nullptr;
    ramfs_file_operations.open           = nullptr;
    ramfs_file_operations.flush          = nullptr;
    ramfs_file_operations.release        = nullptr;
    ramfs_file_operations.fsync          = nullptr;

    ramfs_dir_operations.llseek          = nullptr;
    ramfs_dir_operations.read            = nullptr;
    ramfs_dir_operations.write           = nullptr;
    ramfs_dir_operations.iterate_shared  = ramfs_dir_iterate;
    ramfs_dir_operations.poll            = nullptr;
    ramfs_dir_operations.unlocked_ioctl  = nullptr;
    ramfs_dir_operations.mmap            = nullptr;
    ramfs_dir_operations.open            = nullptr;
    ramfs_dir_operations.flush           = nullptr;
    ramfs_dir_operations.release         = nullptr;
    ramfs_dir_operations.fsync           = nullptr;
}