| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 |
- /* /share: a small writable in-memory filesystem.
- *
- * Files live in whole pages from the frame allocator (identity mapped, so a
- * borrowed pointer works), and the directory namespace is the full relative
- * path of every entry, like the pack ramfs. That keeps mkdir/list/rmdir
- * simple without an inode tree. A directory exists as an entry of its own,
- * so empty directories survive. Everything is volatile.
- */
- #include "vfs.h"
- #include "share.h"
- #include "kmalloc.h"
- #include "mm.h"
- struct share_ent {
- char path[SHARE_PATH_MAX];
- unsigned int base; /* first page, 0 when the file is empty */
- unsigned int size;
- unsigned int is_dir;
- };
- static struct share_ent *ents;
- static unsigned int ent_count;
- static unsigned int used_bytes;
- static int s_eq(const char *a, const char *b)
- {
- unsigned int i = 0;
- while (a[i] && b[i] && a[i] == b[i])
- i++;
- return a[i] == '\0' && b[i] == '\0';
- }
- static unsigned int s_len(const char *s)
- {
- unsigned int n = 0;
- while (s[n])
- n++;
- return n;
- }
- static void s_copy(char *d, const char *s, unsigned int cap)
- {
- unsigned int i = 0;
- while (i + 1 < cap && s[i]) {
- d[i] = s[i];
- i++;
- }
- d[i] = '\0';
- }
- static void mem_copy(void *d, const void *s, unsigned int n)
- {
- unsigned char *dd = d;
- const unsigned char *ss = s;
- while (n--)
- *dd++ = *ss++;
- }
- /* A relative path: components separated by '/', no leading/trailing slash,
- * no "." or "..", no control characters, total length < SHARE_PATH_MAX. */
- static int path_ok(const char *path)
- {
- unsigned int i = 0, comp = 0;
- if (!path || !path[0])
- return 0;
- while (path[i]) {
- char c = path[i];
- if (c == '/') {
- if (comp == 0)
- return 0;
- if (comp == 1 && path[i - 1] == '.')
- return 0;
- if (comp == 2 && path[i - 1] == '.' &&
- path[i - 2] == '.')
- return 0;
- comp = 0;
- } else {
- if ((unsigned char)c < 0x20)
- return 0;
- if (++comp > SHARE_PATH_MAX - 2)
- return 0;
- }
- if (++i >= SHARE_PATH_MAX)
- return 0;
- }
- if (comp == 0)
- return 0;
- if (comp == 1 && path[i - 1] == '.')
- return 0;
- if (comp == 2 && path[i - 1] == '.' && path[i - 2] == '.')
- return 0;
- return 1;
- }
- static void parent_of(const char *path, char *parent)
- {
- int i = (int)s_len(path) - 1;
- while (i >= 0 && path[i] != '/')
- i--;
- if (i < 0) {
- parent[0] = '\0';
- return;
- }
- s_copy(parent, path, (unsigned int)i + 1);
- parent[i] = '\0';
- }
- static int lookup(const char *path)
- {
- unsigned int i;
- for (i = 0; i < ent_count; i++)
- if (s_eq(ents[i].path, path))
- return (int)i;
- return -1;
- }
- static void free_data(unsigned int base, unsigned int size)
- {
- unsigned int pages = (size + 0xFFFu) >> 12;
- unsigned int i;
- if (!base)
- return;
- for (i = 0; i < pages; i++)
- mm_free_page(base + i * 0x1000);
- }
- static void remove_ent(unsigned int idx)
- {
- free_data(ents[idx].base, ents[idx].size);
- used_bytes -= ents[idx].size;
- ent_count--;
- if (idx != ent_count)
- ents[idx] = ents[ent_count];
- }
- /* Full path of the child of `dir` containing `p`, into `name`. Returns 0
- * when the entry is not directly inside `dir`. */
- static int child_name(const char *p, const char *dir, char *name,
- unsigned int cap)
- {
- unsigned int dl = s_len(dir), n = 0, i;
- const char *rest;
- if (dl) {
- for (i = 0; i < dl; i++)
- if (p[i] != dir[i])
- return 0;
- if (p[dl] != '/')
- return 0;
- rest = p + dl + 1;
- } else {
- rest = p;
- }
- if (!rest[0])
- return 0;
- while (rest[n] && rest[n] != '/')
- n++;
- if (n >= cap)
- return 0;
- mem_copy(name, rest, n);
- name[n] = '\0';
- return 1;
- }
- /* ---- fs_ops ---- */
- static int share_op_read_file(struct vfs_mount *m, const char *path,
- void *buf, unsigned int max)
- {
- int idx;
- (void)m;
- idx = lookup(path);
- if (idx < 0)
- return VFS_ENOENT;
- if (ents[idx].is_dir)
- return VFS_EINVAL;
- if (ents[idx].size > max)
- return (int)max;
- mem_copy(buf, (const void *)ents[idx].base, ents[idx].size);
- return (int)ents[idx].size;
- }
- static int share_op_load_file(struct vfs_mount *m, const char *path,
- struct vfs_file *out)
- {
- int idx;
- (void)m;
- idx = lookup(path);
- if (idx < 0)
- return VFS_ENOENT;
- if (ents[idx].is_dir)
- return VFS_EINVAL;
- out->data = (const void *)ents[idx].base;
- out->size = ents[idx].size;
- return 0;
- }
- static void share_op_release_file(struct vfs_mount *m, struct vfs_file *f)
- {
- (void)m;
- (void)f;
- }
- static int share_op_stat(struct vfs_mount *m, const char *path,
- unsigned int *size, int *is_dir)
- {
- int idx;
- (void)m;
- if (path[0] == '\0') {
- if (size)
- *size = 0;
- if (is_dir)
- *is_dir = 1;
- return 0;
- }
- idx = lookup(path);
- if (idx < 0)
- return VFS_ENOENT;
- if (size)
- *size = ents[idx].is_dir ? 0 : ents[idx].size;
- if (is_dir)
- *is_dir = (int)ents[idx].is_dir;
- return 0;
- }
- static int share_op_write_file(struct vfs_mount *m, const char *path,
- const void *buf, unsigned int len)
- {
- char parent[SHARE_PATH_MAX];
- unsigned int base = 0, old_size = 0;
- int idx, pidx;
- (void)m;
- if (!path_ok(path))
- return VFS_EINVAL;
- idx = lookup(path);
- if (idx >= 0 && ents[idx].is_dir)
- return VFS_EINVAL;
- parent_of(path, parent);
- if (parent[0]) {
- pidx = lookup(parent);
- if (pidx < 0 || !ents[pidx].is_dir)
- return VFS_EINVAL;
- }
- if (idx >= 0)
- old_size = ents[idx].size;
- if (used_bytes - old_size + len > SHARE_MAX_BYTES)
- return VFS_ENOSPC;
- if (len) {
- unsigned int pages = (len + 0xFFFu) >> 12;
- base = mm_alloc_pages(pages);
- if (!base)
- return VFS_ENOSPC;
- mem_copy((void *)base, buf, len);
- }
- if (idx < 0) {
- if (ent_count >= SHARE_MAX_ENTRIES) {
- free_data(base, len);
- return VFS_ENOSPC;
- }
- idx = (int)ent_count++;
- s_copy(ents[idx].path, path, sizeof(ents[idx].path));
- ents[idx].base = 0;
- ents[idx].size = 0;
- ents[idx].is_dir = 0;
- }
- free_data(ents[idx].base, ents[idx].size);
- used_bytes -= ents[idx].size;
- ents[idx].base = base;
- ents[idx].size = len;
- ents[idx].is_dir = 0;
- used_bytes += len;
- return 0;
- }
- static int share_op_unlink(struct vfs_mount *m, const char *path)
- {
- int idx = lookup(path);
- (void)m;
- if (idx < 0)
- return VFS_ENOENT;
- if (ents[idx].is_dir)
- return VFS_EINVAL;
- remove_ent((unsigned int)idx);
- return 0;
- }
- static int share_op_mkdir(struct vfs_mount *m, const char *path)
- {
- char parent[SHARE_PATH_MAX];
- unsigned int i;
- int pidx;
- (void)m;
- if (!path_ok(path) || lookup(path) >= 0)
- return VFS_EINVAL;
- parent_of(path, parent);
- if (parent[0]) {
- pidx = lookup(parent);
- if (pidx < 0 || !ents[pidx].is_dir)
- return VFS_EINVAL;
- }
- /* Do not create a directory whose path is already a file prefix. */
- for (i = 0; i < ent_count; i++) {
- unsigned int pl = s_len(path);
- if (s_len(ents[i].path) > pl &&
- ents[i].path[pl] == '/') {
- unsigned int j;
- for (j = 0; j < pl && ents[i].path[j] == path[j]; j++)
- ;
- if (j == pl)
- return VFS_EINVAL;
- }
- }
- if (ent_count >= SHARE_MAX_ENTRIES)
- return VFS_ENOSPC;
- i = ent_count++;
- s_copy(ents[i].path, path, sizeof(ents[i].path));
- ents[i].base = 0;
- ents[i].size = 0;
- ents[i].is_dir = 1;
- return 0;
- }
- static int share_op_rmdir(struct vfs_mount *m, const char *path)
- {
- unsigned int i, pl;
- int idx = lookup(path);
- (void)m;
- if (idx < 0)
- return VFS_ENOENT;
- if (!ents[idx].is_dir)
- return VFS_EINVAL;
- pl = s_len(path);
- for (i = 0; i < ent_count; i++) {
- unsigned int j;
- if (s_len(ents[i].path) <= pl || ents[i].path[pl] != '/')
- continue;
- for (j = 0; j < pl && ents[i].path[j] == path[j]; j++)
- ;
- if (j == pl)
- return VFS_EINVAL; /* not empty */
- }
- remove_ent((unsigned int)idx);
- return 0;
- }
- static int share_op_list_dir(struct vfs_mount *m, const char *path,
- unsigned int index, struct vfs_dirent *out)
- {
- char name[SHARE_PATH_MAX], seen_name[SHARE_PATH_MAX];
- unsigned int i, seen = 0;
- int idx;
- (void)m;
- if (path[0]) {
- idx = lookup(path);
- if (idx < 0)
- return VFS_ENOENT;
- if (!ents[idx].is_dir)
- return VFS_EINVAL;
- }
- for (i = 0; i < ent_count; i++) {
- unsigned int j;
- int dup = 0, exact;
- char full[SHARE_PATH_MAX];
- if (!child_name(ents[i].path, path, name, sizeof(name)))
- continue;
- for (j = 0; j < i; j++) {
- if (child_name(ents[j].path, path, seen_name,
- sizeof(seen_name)) &&
- s_eq(seen_name, name)) {
- dup = 1;
- break;
- }
- }
- if (dup)
- continue;
- if (seen++ != index)
- continue;
- /* The child is a directory when its own entry says so. */
- if (path[0]) {
- unsigned int k = 0;
- for (; path[k] && k + 1 < sizeof(full); k++)
- full[k] = path[k];
- if (k + 1 < sizeof(full))
- full[k++] = '/';
- for (j = 0; name[j] && k + 1 < sizeof(full); j++)
- full[k++] = name[j];
- full[k] = '\0';
- } else {
- s_copy(full, name, sizeof(full));
- }
- exact = lookup(full);
- s_copy(out->name, name, sizeof(out->name));
- out->size = (exact >= 0 && !ents[exact].is_dir) ?
- ents[exact].size : 0;
- out->is_dir = (exact >= 0 && ents[exact].is_dir) ? 1 : 0;
- out->raw_83 = 0;
- return 1;
- }
- return 0;
- }
- const struct fs_ops share_ops = {
- .raw_83 = 0,
- .read_file = share_op_read_file,
- .load_file = share_op_load_file,
- .release_file = share_op_release_file,
- .stat = share_op_stat,
- .write_file = share_op_write_file,
- .unlink = share_op_unlink,
- .mkdir = share_op_mkdir,
- .rmdir = share_op_rmdir,
- .list_dir = share_op_list_dir,
- };
- int share_init(void)
- {
- if (ents)
- return 0;
- ents = kmalloc(sizeof(*ents) * SHARE_MAX_ENTRIES);
- if (!ents)
- return -1;
- ent_count = 0;
- used_bytes = 0;
- return 0;
- }
|