toaruos/kernel/vfs/tmpfs.c

592 lines
14 KiB
C
Raw Normal View History

2021-05-31 04:47:02 +03:00
/**
* @file kernel/vfs/tmpfs.c
* @brief In-memory read-write filesystem.
*
* Generally provides the filesystem for "migrated" live CDs,
* as well as /tmp and /var.
*
* @copyright
2018-03-16 15:56:19 +03:00
* This file is part of ToaruOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
2021-05-31 04:47:02 +03:00
* Copyright (C) 2014-2021 K. Lange
2018-03-16 15:56:19 +03:00
*/
2021-05-31 04:47:02 +03:00
#include <stdint.h>
#include <errno.h>
#include <kernel/types.h>
#include <kernel/string.h>
#include <kernel/printf.h>
#include <kernel/vfs.h>
2018-03-19 05:38:11 +03:00
#include <kernel/process.h>
2018-10-31 04:51:08 +03:00
#include <kernel/tokenize.h>
2021-05-31 04:47:02 +03:00
#include <kernel/tmpfs.h>
#include <kernel/spinlock.h>
#include <kernel/mmu.h>
#include <kernel/time.h>
#include <kernel/procfs.h>
2018-03-16 15:56:19 +03:00
/* 4KB */
#define BLOCKSIZE 0x1000
#define TMPFS_TYPE_FILE 1
#define TMPFS_TYPE_DIR 2
#define TMPFS_TYPE_LINK 3
static struct tmpfs_dir * tmpfs_root = NULL;
2021-09-17 07:32:50 +03:00
static volatile intptr_t tmpfs_total_blocks = 0;
2018-03-16 15:56:19 +03:00
static fs_node_t * tmpfs_from_dir(struct tmpfs_dir * d);
static struct tmpfs_file * tmpfs_file_new(char * name) {
struct tmpfs_file * t = malloc(sizeof(struct tmpfs_file));
spin_init(t->lock);
2018-03-16 15:56:19 +03:00
t->name = strdup(name);
t->type = TMPFS_TYPE_FILE;
t->length = 0;
t->pointers = 2;
t->block_count = 0;
t->mask = 0;
t->uid = 0;
t->gid = 0;
t->atime = now();
t->mtime = t->atime;
t->ctime = t->atime;
t->blocks = malloc(t->pointers * sizeof(char *));
for (size_t i = 0; i < t->pointers; ++i) {
2021-05-31 04:47:02 +03:00
t->blocks[i] = 0;
2018-03-16 15:56:19 +03:00
}
return t;
}
static int symlink_tmpfs(fs_node_t * parent, char * target, char * name) {
2018-03-16 15:56:19 +03:00
struct tmpfs_dir * d = (struct tmpfs_dir *)parent->device;
spin_lock(d->lock);
2018-03-16 15:56:19 +03:00
foreach(f, d->files) {
struct tmpfs_file * t = (struct tmpfs_file *)f->value;
if (!strcmp(name, t->name)) {
spin_unlock(d->lock);
return -EEXIST; /* Already exists */
2018-03-16 15:56:19 +03:00
}
}
spin_unlock(d->lock);
2018-03-16 15:56:19 +03:00
struct tmpfs_file * t = tmpfs_file_new(name);
t->type = TMPFS_TYPE_LINK;
t->target = strdup(target);
t->length = strlen(target);
2018-03-16 15:56:19 +03:00
2018-07-18 09:43:17 +03:00
t->mask = 0777;
2021-05-31 04:47:02 +03:00
t->uid = this_core->current_process->user;
t->gid = this_core->current_process->user;
2018-07-18 09:43:17 +03:00
spin_lock(d->lock);
2018-03-16 15:56:19 +03:00
list_insert(d->files, t);
spin_unlock(d->lock);
return 0;
2018-03-16 15:56:19 +03:00
}
static ssize_t readlink_tmpfs(fs_node_t * node, char * buf, size_t size) {
2018-03-16 15:56:19 +03:00
struct tmpfs_file * t = (struct tmpfs_file *)(node->device);
spin_lock(t->lock);
2018-03-16 15:56:19 +03:00
if (t->type != TMPFS_TYPE_LINK) {
spin_unlock(t->lock);
2021-05-31 04:47:02 +03:00
printf("tmpfs: not a symlink?\n");
2018-03-16 15:56:19 +03:00
return -1;
}
if (size < strlen(t->target) + 1) {
memcpy(buf, t->target, size-1);
buf[size-1] = '\0';
spin_unlock(t->lock);
return size-2;
2018-03-16 15:56:19 +03:00
} else {
size_t len = strlen(t->target);
memcpy(buf, t->target, len + 1);
spin_unlock(t->lock);
return len;
2018-03-16 15:56:19 +03:00
}
}
static struct tmpfs_dir * tmpfs_dir_new(char * name, struct tmpfs_dir * parent) {
struct tmpfs_dir * d = malloc(sizeof(struct tmpfs_dir));
spin_init(d->lock);
2018-03-16 15:56:19 +03:00
d->name = strdup(name);
d->type = TMPFS_TYPE_DIR;
d->mask = 0;
d->uid = 0;
d->gid = 0;
d->atime = now();
d->mtime = d->atime;
d->ctime = d->atime;
2021-05-31 04:47:02 +03:00
d->files = list_create("tmpfs directory entries",d);
2018-03-16 15:56:19 +03:00
return d;
}
static void tmpfs_file_free(struct tmpfs_file * t) {
spin_lock(t->lock);
2018-03-16 15:56:19 +03:00
if (t->type == TMPFS_TYPE_LINK) {
/* free target string */
2018-03-16 15:56:19 +03:00
free(t->target);
}
for (size_t i = 0; i < t->block_count; ++i) {
mmu_frame_release((uintptr_t)t->blocks[i] * 0x1000);
tmpfs_total_blocks--;
2018-03-16 15:56:19 +03:00
}
spin_unlock(t->lock);
2018-03-16 15:56:19 +03:00
}
static void tmpfs_file_blocks_embiggen(struct tmpfs_file * t) {
t->pointers *= 2;
t->blocks = realloc(t->blocks, sizeof(char *) * t->pointers);
}
static char * tmpfs_file_getset_block(struct tmpfs_file * t, size_t blockid, int create) {
if (create) {
while (blockid >= t->pointers) {
tmpfs_file_blocks_embiggen(t);
}
while (blockid >= t->block_count) {
2021-05-31 04:47:02 +03:00
uintptr_t index = mmu_allocate_a_frame();
tmpfs_total_blocks++;
2021-05-31 04:47:02 +03:00
t->blocks[t->block_count] = index;
2018-03-16 15:56:19 +03:00
t->block_count += 1;
}
} else {
if (blockid >= t->block_count) {
2021-05-31 04:47:02 +03:00
printf("tmpfs: not enough blocks?\n");
2018-03-16 15:56:19 +03:00
return NULL;
}
}
return (char *)mmu_map_from_physical(t->blocks[blockid] << 12);
2018-03-16 15:56:19 +03:00
}
static ssize_t read_tmpfs(fs_node_t *node, off_t offset, size_t size, uint8_t *buffer) {
2018-03-16 15:56:19 +03:00
struct tmpfs_file * t = (struct tmpfs_file *)(node->device);
spin_lock(t->lock);
2018-03-16 15:56:19 +03:00
t->atime = now();
2021-05-31 04:47:02 +03:00
uint64_t end;
if ((size_t)offset + size > t->length) {
2018-03-16 15:56:19 +03:00
end = t->length;
} else {
end = offset + size;
}
2021-05-31 04:47:02 +03:00
uint64_t start_block = offset / BLOCKSIZE;
uint64_t end_block = end / BLOCKSIZE;
uint64_t end_size = end - end_block * BLOCKSIZE;
uint64_t size_to_read = end - offset;
if (start_block == end_block && (size_t)offset == end) {
spin_unlock(t->lock);
return 0;
}
2018-03-16 15:56:19 +03:00
if (start_block == end_block) {
void *buf = tmpfs_file_getset_block(t, start_block, 0);
2021-05-31 04:47:02 +03:00
memcpy(buffer, (uint8_t *)(((uintptr_t)buf) + ((uintptr_t)offset % BLOCKSIZE)), size_to_read);
spin_unlock(t->lock);
2018-03-16 15:56:19 +03:00
return size_to_read;
} else {
2021-05-31 04:47:02 +03:00
uint64_t block_offset;
uint64_t blocks_read = 0;
2018-03-16 15:56:19 +03:00
for (block_offset = start_block; block_offset < end_block; block_offset++, blocks_read++) {
if (block_offset == start_block) {
void *buf = tmpfs_file_getset_block(t, block_offset, 0);
2021-05-31 04:47:02 +03:00
memcpy(buffer, (uint8_t *)(((uint64_t)buf) + ((uintptr_t)offset % BLOCKSIZE)), BLOCKSIZE - (offset % BLOCKSIZE));
2018-03-16 15:56:19 +03:00
} else {
void *buf = tmpfs_file_getset_block(t, block_offset, 0);
memcpy(buffer + BLOCKSIZE * blocks_read - (offset % BLOCKSIZE), buf, BLOCKSIZE);
}
}
if (end_size) {
void *buf = tmpfs_file_getset_block(t, end_block, 0);
memcpy(buffer + BLOCKSIZE * blocks_read - (offset % BLOCKSIZE), buf, end_size);
}
}
spin_unlock(t->lock);
2018-03-16 15:56:19 +03:00
return size_to_read;
}
static ssize_t write_tmpfs(fs_node_t *node, off_t offset, size_t size, uint8_t *buffer) {
2018-03-16 15:56:19 +03:00
struct tmpfs_file * t = (struct tmpfs_file *)(node->device);
spin_lock(t->lock);
2018-03-16 15:56:19 +03:00
t->atime = now();
t->mtime = t->atime;
2021-05-31 04:47:02 +03:00
uint64_t end;
if ((size_t)offset + size > t->length) {
2018-03-16 15:56:19 +03:00
t->length = offset + size;
}
end = offset + size;
2021-05-31 04:47:02 +03:00
uint64_t start_block = offset / BLOCKSIZE;
uint64_t end_block = end / BLOCKSIZE;
uint64_t end_size = end - end_block * BLOCKSIZE;
uint64_t size_to_read = end - offset;
2018-03-16 15:56:19 +03:00
if (start_block == end_block) {
void *buf = tmpfs_file_getset_block(t, start_block, 1);
2021-05-31 04:47:02 +03:00
memcpy((uint8_t *)(((uint64_t)buf) + ((uintptr_t)offset % BLOCKSIZE)), buffer, size_to_read);
spin_unlock(t->lock);
2018-03-16 15:56:19 +03:00
return size_to_read;
} else {
2021-05-31 04:47:02 +03:00
uint64_t block_offset;
uint64_t blocks_read = 0;
2018-03-16 15:56:19 +03:00
for (block_offset = start_block; block_offset < end_block; block_offset++, blocks_read++) {
if (block_offset == start_block) {
void *buf = tmpfs_file_getset_block(t, block_offset, 1);
2021-05-31 04:47:02 +03:00
memcpy((uint8_t *)(((uint64_t)buf) + ((uintptr_t)offset % BLOCKSIZE)), buffer, BLOCKSIZE - (offset % BLOCKSIZE));
2018-03-16 15:56:19 +03:00
} else {
void *buf = tmpfs_file_getset_block(t, block_offset, 1);
memcpy(buf, buffer + BLOCKSIZE * blocks_read - (offset % BLOCKSIZE), BLOCKSIZE);
}
}
if (end_size) {
void *buf = tmpfs_file_getset_block(t, end_block, 1);
memcpy(buf, buffer + BLOCKSIZE * blocks_read - (offset % BLOCKSIZE), end_size);
}
}
spin_unlock(t->lock);
2018-03-16 15:56:19 +03:00
return size_to_read;
}
static int chmod_tmpfs(fs_node_t * node, int mode) {
struct tmpfs_file * t = (struct tmpfs_file *)(node->device);
/* XXX permissions */
t->mask = mode;
return 0;
}
static int chown_tmpfs(fs_node_t * node, int uid, int gid) {
struct tmpfs_file * t = (struct tmpfs_file *)(node->device);
spin_lock(t->lock);
if (uid != -1) t->uid = uid;
if (gid != -1) t->gid = gid;
spin_unlock(t->lock);
2018-03-16 15:56:19 +03:00
return 0;
}
static int truncate_tmpfs(fs_node_t * node) {
2018-03-16 15:56:19 +03:00
struct tmpfs_file * t = (struct tmpfs_file *)(node->device);
spin_lock(t->lock);
for (size_t i = 0; i < t->block_count; ++i) {
mmu_frame_release((uintptr_t)t->blocks[i] * 0x1000);
2021-09-17 07:32:50 +03:00
tmpfs_total_blocks--;
t->blocks[i] = 0;
}
t->block_count = 0;
t->length = 0;
t->mtime = node->atime;
spin_unlock(t->lock);
return 0;
}
2018-03-16 15:56:19 +03:00
static void open_tmpfs(fs_node_t * node, unsigned int flags) {
struct tmpfs_file * t = (struct tmpfs_file *)(node->device);
2018-03-16 15:56:19 +03:00
t->atime = now();
2018-03-16 15:56:19 +03:00
}
static fs_node_t * tmpfs_from_file(struct tmpfs_file * t) {
fs_node_t * fnode = malloc(sizeof(fs_node_t));
spin_lock(t->lock);
2018-03-16 15:56:19 +03:00
memset(fnode, 0x00, sizeof(fs_node_t));
fnode->inode = 0;
strcpy(fnode->name, t->name);
fnode->device = t;
fnode->mask = t->mask;
fnode->uid = t->uid;
fnode->gid = t->gid;
fnode->atime = t->atime;
fnode->ctime = t->ctime;
fnode->mtime = t->mtime;
fnode->flags = FS_FILE;
fnode->read = read_tmpfs;
fnode->write = write_tmpfs;
fnode->open = open_tmpfs;
fnode->close = NULL;
fnode->readdir = NULL;
fnode->finddir = NULL;
fnode->chmod = chmod_tmpfs;
fnode->chown = chown_tmpfs;
fnode->length = t->length;
fnode->truncate = truncate_tmpfs;
2018-03-16 15:56:19 +03:00
fnode->nlink = 1;
spin_unlock(t->lock);
2018-03-16 15:56:19 +03:00
return fnode;
}
static fs_node_t * tmpfs_from_link(struct tmpfs_file * t) {
fs_node_t * fnode = tmpfs_from_file(t);
fnode->flags |= FS_SYMLINK;
fnode->readlink = readlink_tmpfs;
fnode->read = NULL;
fnode->write = NULL;
fnode->create = NULL;
fnode->mkdir = NULL;
fnode->readdir = NULL;
fnode->finddir = NULL;
return fnode;
}
2021-05-31 04:47:02 +03:00
static struct dirent * readdir_tmpfs(fs_node_t *node, uint64_t index) {
2018-03-16 15:56:19 +03:00
struct tmpfs_dir * d = (struct tmpfs_dir *)node->device;
2021-05-31 04:47:02 +03:00
uint64_t i = 0;
2018-03-16 15:56:19 +03:00
if (index == 0) {
struct dirent * out = malloc(sizeof(struct dirent));
memset(out, 0x00, sizeof(struct dirent));
2021-06-05 12:01:48 +03:00
out->d_ino = 0;
strcpy(out->d_name, ".");
2018-03-16 15:56:19 +03:00
return out;
}
if (index == 1) {
struct dirent * out = malloc(sizeof(struct dirent));
memset(out, 0x00, sizeof(struct dirent));
2021-06-05 12:01:48 +03:00
out->d_ino = 0;
strcpy(out->d_name, "..");
2018-03-16 15:56:19 +03:00
return out;
}
index -= 2;
if (index >= d->files->length) return NULL;
foreach(f, d->files) {
if (i == index) {
struct tmpfs_file * t = (struct tmpfs_file *)f->value;
struct dirent * out = malloc(sizeof(struct dirent));
memset(out, 0x00, sizeof(struct dirent));
2021-06-05 12:01:48 +03:00
out->d_ino = (uint64_t)t;
strcpy(out->d_name, t->name);
2018-03-16 15:56:19 +03:00
return out;
} else {
++i;
}
}
return NULL;
}
static fs_node_t * finddir_tmpfs(fs_node_t * node, char * name) {
if (!name) return NULL;
struct tmpfs_dir * d = (struct tmpfs_dir *)node->device;
spin_lock(d->lock);
2018-03-16 15:56:19 +03:00
foreach(f, d->files) {
struct tmpfs_file * t = (struct tmpfs_file *)f->value;
if (!strcmp(name, t->name)) {
fs_node_t * out = NULL;
2018-03-16 15:56:19 +03:00
switch (t->type) {
case TMPFS_TYPE_FILE:
out = tmpfs_from_file(t);
break;
2018-03-16 15:56:19 +03:00
case TMPFS_TYPE_LINK:
out = tmpfs_from_link(t);
break;
2018-03-16 15:56:19 +03:00
case TMPFS_TYPE_DIR:
out = tmpfs_from_dir((struct tmpfs_dir *)t);
break;
2018-03-16 15:56:19 +03:00
}
spin_unlock(d->lock);
return out;
2018-03-16 15:56:19 +03:00
}
}
spin_unlock(d->lock);
2018-03-16 15:56:19 +03:00
return NULL;
}
static int try_free_dir(struct tmpfs_dir * d) {
spin_lock(d->lock);
if (d->files && d->files->length != 0) {
spin_unlock(d->lock);
return 1;
}
free(d->files);
spin_unlock(d->lock);
return 0;
}
static int unlink_tmpfs(fs_node_t * node, char * name) {
2018-03-16 15:56:19 +03:00
struct tmpfs_dir * d = (struct tmpfs_dir *)node->device;
int i = -1, j = 0;
spin_lock(d->lock);
2018-03-16 15:56:19 +03:00
foreach(f, d->files) {
struct tmpfs_file * t = (struct tmpfs_file *)f->value;
if (!strcmp(name, t->name)) {
if (t->type == TMPFS_TYPE_DIR) {
if (try_free_dir((void*)t)) {
spin_unlock(d->lock);
return -ENOTEMPTY;
}
} else {
tmpfs_file_free(t);
}
2018-03-16 15:56:19 +03:00
free(t);
i = j;
break;
}
j++;
}
if (i >= 0) {
list_remove(d->files, i);
} else {
spin_unlock(d->lock);
return -ENOENT;
2018-03-16 15:56:19 +03:00
}
spin_unlock(d->lock);
return 0;
2018-03-16 15:56:19 +03:00
}
static int create_tmpfs(fs_node_t *parent, char *name, mode_t permission) {
if (!name) return -EINVAL;
2018-03-16 15:56:19 +03:00
struct tmpfs_dir * d = (struct tmpfs_dir *)parent->device;
spin_lock(d->lock);
2018-03-16 15:56:19 +03:00
foreach(f, d->files) {
struct tmpfs_file * t = (struct tmpfs_file *)f->value;
if (!strcmp(name, t->name)) {
spin_unlock(d->lock);
return -EEXIST; /* Already exists */
2018-03-16 15:56:19 +03:00
}
}
spin_unlock(d->lock);
2018-03-16 15:56:19 +03:00
struct tmpfs_file * t = tmpfs_file_new(name);
t->mask = permission;
2021-05-31 04:47:02 +03:00
t->uid = this_core->current_process->user;
2021-08-31 13:47:04 +03:00
t->gid = this_core->current_process->user_group;
2018-03-16 15:56:19 +03:00
spin_lock(d->lock);
2018-03-16 15:56:19 +03:00
list_insert(d->files, t);
spin_unlock(d->lock);
return 0;
2018-03-16 15:56:19 +03:00
}
static int mkdir_tmpfs(fs_node_t * parent, char * name, mode_t permission) {
if (!name) return -EINVAL;
if (!strlen(name)) return -EINVAL;
2018-03-16 15:56:19 +03:00
struct tmpfs_dir * d = (struct tmpfs_dir *)parent->device;
spin_lock(d->lock);
2018-03-16 15:56:19 +03:00
foreach(f, d->files) {
struct tmpfs_file * t = (struct tmpfs_file *)f->value;
if (!strcmp(name, t->name)) {
spin_unlock(d->lock);
return -EEXIST; /* Already exists */
2018-03-16 15:56:19 +03:00
}
}
spin_unlock(d->lock);
2018-03-16 15:56:19 +03:00
struct tmpfs_dir * out = tmpfs_dir_new(name, d);
out->mask = permission;
2021-05-31 04:47:02 +03:00
out->uid = this_core->current_process->user;
out->gid = this_core->current_process->user;
2018-03-16 15:56:19 +03:00
spin_lock(d->lock);
2018-03-16 15:56:19 +03:00
list_insert(d->files, out);
spin_unlock(d->lock);
return 0;
2018-03-16 15:56:19 +03:00
}
static fs_node_t * tmpfs_from_dir(struct tmpfs_dir * d) {
fs_node_t * fnode = malloc(sizeof(fs_node_t));
spin_lock(d->lock);
2018-03-16 15:56:19 +03:00
memset(fnode, 0x00, sizeof(fs_node_t));
fnode->inode = 0;
strcpy(fnode->name, "tmp");
fnode->mask = d->mask;
fnode->uid = d->uid;
fnode->gid = d->gid;
fnode->device = d;
fnode->atime = d->atime;
fnode->mtime = d->mtime;
fnode->ctime = d->ctime;
fnode->flags = FS_DIRECTORY;
fnode->read = NULL;
fnode->write = NULL;
fnode->open = NULL;
fnode->close = NULL;
fnode->readdir = readdir_tmpfs;
fnode->finddir = finddir_tmpfs;
fnode->create = create_tmpfs;
fnode->unlink = unlink_tmpfs;
fnode->mkdir = mkdir_tmpfs;
fnode->nlink = 1; /* should be "number of children that are directories + 1" */
fnode->symlink = symlink_tmpfs;
fnode->chown = chown_tmpfs;
fnode->chmod = chmod_tmpfs;
spin_unlock(d->lock);
2018-03-16 15:56:19 +03:00
return fnode;
}
fs_node_t * tmpfs_create(char * name) {
tmpfs_root = tmpfs_dir_new(name, NULL);
tmpfs_root->mask = 0777;
tmpfs_root->uid = 0;
tmpfs_root->gid = 0;
return tmpfs_from_dir(tmpfs_root);
}
2021-05-31 04:47:02 +03:00
fs_node_t * tmpfs_mount(const char * device, const char * mount_path) {
2018-10-31 04:51:08 +03:00
char * arg = strdup(device);
char * argv[10];
int argc = tokenize(arg, ",", argv);
fs_node_t * fs = tmpfs_create(argv[0]);
if (argc > 1) {
if (strlen(argv[1]) < 3) {
2021-05-31 04:47:02 +03:00
printf("tmpfs: ignoring bad permission option for tmpfs\n");
2018-10-31 04:51:08 +03:00
} else {
int mode = ((argv[1][0] - '0') << 6) |
((argv[1][1] - '0') << 3) |
((argv[1][2] - '0') << 0);
fs->mask = mode;
}
}
2021-05-31 04:47:02 +03:00
//free(arg);
2018-03-16 15:56:19 +03:00
return fs;
}
static void tmpfs_func(fs_node_t * node) {
procfs_printf(node,
"UsedBlocks:\t%zd\n",
tmpfs_total_blocks);
}
static struct procfs_entry tmpfs_entry = {
0,
"tmpfs",
tmpfs_func,
};
2021-05-31 04:47:02 +03:00
void tmpfs_register_init(void) {
2018-03-16 15:56:19 +03:00
vfs_register("tmpfs", tmpfs_mount);
procfs_install(&tmpfs_entry);
2018-03-16 15:56:19 +03:00
}