aboutsummaryrefslogtreecommitdiff
path: root/lib/fs/memfs/memfs.c
blob: aa9bb8ed151890dd7d3d02dabb8877bfbdffabf8 (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
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
/*
 * Copyright (c) 2015 Travis Geiselbrecht
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <string.h>
#include <stdlib.h>
#include <debug.h>
#include <err.h>
#include <trace.h>
#include <list.h>
#include <lk/init.h>
#include <lib/fs.h>
#include <kernel/mutex.h>

#define LOCAL_TRACE 0

typedef struct {
    struct list_node files;
    struct list_node dcookies;

    mutex_t lock;
} memfs_t;

typedef struct {
    struct list_node node;
    memfs_t *fs;

    // name
    char *name;

    // main data area
    uint8_t *ptr;
    size_t len;
} memfs_file_t;

struct dircookie {
    struct list_node node;
    memfs_t *fs;

    // next entry that will be returned
    memfs_file_t *next_file;
};

static memfs_file_t *find_file(memfs_t *mem, const char *name)
{
    memfs_file_t *file;
    list_for_every_entry(&mem->files, file, memfs_file_t, node) {
        if (!strcmp(name, file->name))
            return file;
    }

    return NULL;
}

const char *trim_name(const char *_name)
{
    // chew up leading slashes
    const char *name = &_name[0];
    while (*name == '/')
        name++;

    return name;
}

static status_t memfs_mount(struct bdev *dev, fscookie **cookie)
{
    LTRACEF("dev %p, cookie %p\n", dev, cookie);

    memfs_t *mem = malloc(sizeof(*mem));
    if (!mem)
        return ERR_NO_MEMORY;

    list_initialize(&mem->files);
    list_initialize(&mem->dcookies);
    mutex_init(&mem->lock);

    *cookie = (fscookie *)mem;

    return NO_ERROR;
}

static status_t memfs_unmount(fscookie *cookie)
{
    LTRACEF("cookie %p\n", cookie);

    memfs_t *mem = (memfs_t *)cookie;

    mutex_acquire(&mem->lock);

    // free all the files
    memfs_file_t *file;
    while ((file = list_remove_head_type(&mem->files, memfs_file_t, node))) {
        free(file->ptr);
        free(file->name);
        free(file);
    }

    mutex_release(&mem->lock);

    free(mem);

    return NO_ERROR;
}

static status_t memfs_create(fscookie *cookie, const char *name, filecookie **fcookie, uint64_t len)
{
    status_t err;

    LTRACEF("cookie %p name '%s' filecookie %p len %llu\n", cookie, name, fcookie, len);

    memfs_t *mem = (memfs_t *)cookie;

    if (len >= ULONG_MAX)
        return ERR_NO_MEMORY;

    // make sure we strip out any leading /
    name = trim_name(name);

    // we can't handle directories right now, so fail if the file has a / in its name
    if (strchr(name, '/'))
        return ERR_NOT_SUPPORTED;

    mutex_acquire(&mem->lock);

    // see if the file already exists
    if (find_file(mem, name)) {
        err = ERR_ALREADY_EXISTS;
        goto out;
    }

    // allocate a new file
    memfs_file_t *file = malloc(sizeof(*file));
    if (!file) {
        err = ERR_NO_MEMORY;
        goto out;
    }

    // allocate the space for it
    file->ptr = calloc(1, len);
    if (!file->ptr) {
        free(file);
        err = ERR_NO_MEMORY;
        goto out;
    }
    file->len = len;

    // fill in some metadata and stuff it in the file list
    file->name = strdup(name);
    file->fs = mem;

    list_add_tail(&mem->files, &file->node);

    *fcookie = (filecookie *)file;

    err = NO_ERROR;

out:
    mutex_release(&mem->lock);

    return err;
}

static status_t memfs_open(fscookie *cookie, const char *name, filecookie **fcookie)
{
    LTRACEF("cookie %p name '%s' filecookie %p\n", cookie, name, fcookie);

    memfs_t *mem = (memfs_t *)cookie;

    // make sure we strip out any leading /
    name = trim_name(name);

    mutex_acquire(&mem->lock);
    memfs_file_t *file = find_file(mem, name);
    mutex_release(&mem->lock);

    if (!file)
        return ERR_NOT_FOUND;

    *fcookie = (filecookie *)file;

    return NO_ERROR;
}

static status_t memfs_close(filecookie *fcookie)
{
    memfs_file_t *file = (memfs_file_t *)fcookie;

    LTRACEF("cookie %p name '%s'\n", fcookie, file->name);

    return NO_ERROR;
}

static ssize_t memfs_read(filecookie *fcookie, void *buf, off_t off, size_t len)
{
    LTRACEF("filecookie %p buf %p offset %lld len %zu\n", fcookie, buf, off, len);

    memfs_file_t *file = (memfs_file_t *)fcookie;

    if (off < 0)
        return ERR_INVALID_ARGS;

    mutex_acquire(&file->fs->lock);

    if (off >= file->len) {
        len = 0;
    } else if (off + len > file->len) {
        len = file->len - off;
    }

    // copy that floppy
    memcpy(buf, file->ptr + off, len);

    mutex_release(&file->fs->lock);

    return len;
}

static ssize_t memfs_write(filecookie *fcookie, const void *buf, off_t off, size_t len)
{
    LTRACEF("filecookie %p buf %p offset %lld len %zu\n", fcookie, buf, off, len);

    memfs_file_t *file = (memfs_file_t *)fcookie;

    if (off < 0)
        return ERR_INVALID_ARGS;

    mutex_acquire(&file->fs->lock);

    // see if this write will extend the file
    if (off + len > file->len) {
        void *ptr = realloc(file->ptr, off + len);
        if (!ptr) {
            mutex_release(&file->fs->lock);
            return ERR_NO_MEMORY;
        }

        file->ptr = ptr;
        file->len = off + len;
    }

    memcpy(file->ptr + off, buf, len);

    mutex_release(&file->fs->lock);

    return len;
}

static status_t memfs_stat(filecookie *fcookie, struct file_stat *stat)
{
    LTRACEF("filecookie %p stat %p\n", fcookie, stat);

    memfs_file_t *file = (memfs_file_t *)fcookie;

    mutex_acquire(&file->fs->lock);

    if (stat) {
        stat->is_dir = false;
        stat->size = file->len;
    }

    mutex_release(&file->fs->lock);

    return NO_ERROR;
}

static status_t memfs_opendir(fscookie *cookie, const char *name, dircookie **dcookie)
{
    LTRACEF("cookie %p name '%s' dircookie %p\n", cookie, name, dcookie);

    memfs_t *mem = (memfs_t *)cookie;

    // make sure we strip out any leading /
    name = trim_name(name);

    // at the moment, we only support opening "" (with / stripped)
    if (strcmp("", name))
        return ERR_NOT_FOUND;

    // allocate a dir cookie, point it at the first file, and stuff it in the dircookie jar
    dircookie *dir = malloc(sizeof(*dir));
    if (!dir)
        return ERR_NO_MEMORY;

    dir->fs = mem;

    mutex_acquire(&mem->lock);
    dir->next_file = list_peek_head_type(&mem->files, memfs_file_t, node);
    list_add_head(&mem->dcookies, &dir->node);
    mutex_release(&mem->lock);

    *dcookie = dir;

    return NO_ERROR;
}

static status_t memfs_readdir(dircookie *dcookie, struct dirent *ent)
{
    status_t err;

    LTRACEF("dircookie %p ent %p\n", dcookie, ent);

    if (!ent)
        return ERR_INVALID_ARGS;

    mutex_acquire(&dcookie->fs->lock);

    // return the next file in the list and bump the cursor
    if (dcookie->next_file) {
        strlcpy(ent->name, dcookie->next_file->name, sizeof(ent->name));
        dcookie->next_file = list_next_type(&dcookie->fs->files, &dcookie->next_file->node, memfs_file_t, node);
        err = NO_ERROR;
    } else {
        err = ERR_NOT_FOUND;
    }

    mutex_release(&dcookie->fs->lock);

    return err;
}

static status_t memfs_closedir(dircookie *dcookie)
{
    LTRACEF("dircookie %p\n", dcookie);

    // free the dircookie
    mutex_acquire(&dcookie->fs->lock);
    list_delete(&dcookie->node);
    mutex_release(&dcookie->fs->lock);

    free(dcookie);

    return NO_ERROR;
}

static const struct fs_api memfs_api = {
    .mount = memfs_mount,
    .unmount = memfs_unmount,

    .create = memfs_create,
    .open = memfs_open,
    .close = memfs_close,

    .read = memfs_read,
    .write = memfs_write,

    .stat = memfs_stat,

#if 0
    status_t (*mkdir)(fscookie *, const char *);
#endif
    .opendir = memfs_opendir,
    .readdir = memfs_readdir,
    .closedir = memfs_closedir,

};

STATIC_FS_IMPL(memfs, &memfs_api);