#include <lunaix/fs/twifs.h>
-#define PATH_DELIM '/'
-
-#define unlock_inode(inode) mutex_unlock(&inode->lock)
-#define lock_inode(inode) \
- ({ \
- mutex_lock(&inode->lock); \
- lru_use_one(inode_lru, &inode->lru); \
- })
-
-#define unlock_dnode(dnode) mutex_unlock(&dnode->lock)
-#define lock_dnode(dnode) \
- ({ \
- mutex_lock(&dnode->lock); \
- lru_use_one(dnode_lru, &dnode->lru); \
- })
-
static struct cake_pile* dnode_pile;
static struct cake_pile* inode_pile;
static struct cake_pile* file_pile;
struct v_dnode* vfs_sysroot;
static struct hbucket* dnode_cache;
-static struct lru_zone *dnode_lru, *inode_lru;
+struct lru_zone *dnode_lru, *inode_lru;
struct hstr vfs_ddot = HSTR("..", 2);
struct hstr vfs_dot = HSTR(".", 1);
void
vfs_dcache_add(struct v_dnode* parent, struct v_dnode* dnode)
{
+ assert(parent);
+
atomic_fetch_add(&dnode->ref_count, 1);
dnode->parent = parent;
llist_append(&parent->children, &dnode->siblings);
void
vfs_dcache_remove(struct v_dnode* dnode)
{
+ assert(dnode);
assert(dnode->ref_count == 1);
llist_delete(&dnode->siblings);
void
vfs_dcache_rehash(struct v_dnode* new_parent, struct v_dnode* dnode)
{
+ assert(new_parent);
+
hstr_rehash(&dnode->name, HSTR_FULL_HASH);
vfs_dcache_remove(dnode);
vfs_dcache_add(new_parent, dnode);
}
-int
-__vfs_walk(struct v_dnode* start,
- const char* path,
- struct v_dnode** dentry,
- struct hstr* component,
- int walk_options)
-{
- int errno = 0;
- int i = 0, j = 0;
-
- if (path[0] == PATH_DELIM || !start) {
- if ((walk_options & VFS_WALK_FSRELATIVE) && start) {
- start = start->super_block->root;
- } else {
- start = vfs_sysroot;
- if (!vfs_sysroot->mnt) {
- panick("vfs: no root");
- }
- }
- i++;
- }
-
- struct v_dnode* dnode;
- struct v_dnode* current_level = start;
-
- char name_content[VFS_NAME_MAXLEN];
- struct hstr name = HSTR(name_content, 0);
-
- char current = path[i++], lookahead;
- while (current && current_level) {
- lookahead = path[i++];
- if (current != PATH_DELIM) {
- if (j >= VFS_NAME_MAXLEN - 1) {
- return ENAMETOOLONG;
- }
- if (!VFS_VALID_CHAR(current)) {
- return EINVAL;
- }
- name_content[j++] = current;
- if (lookahead) {
- goto cont;
- }
- }
-
- // handling cases like /^.*(\/+).*$/
- if (lookahead == PATH_DELIM) {
- goto cont;
- }
-
- lock_dnode(current_level);
-
- name_content[j] = 0;
- name.len = j;
- hstr_rehash(&name, HSTR_FULL_HASH);
-
- if (!lookahead && (walk_options & VFS_WALK_PARENT)) {
- if (component) {
- component->hash = name.hash;
- component->len = j;
- strcpy(component->value, name_content);
- }
- unlock_dnode(current_level);
- break;
- }
-
- dnode = vfs_dcache_lookup(current_level, &name);
-
- if (!dnode) {
- dnode = vfs_d_alloc(current_level, &name);
-
- if (!dnode) {
- errno = ENOMEM;
- goto error;
- }
-
- struct v_inode* current_inode = current_level->inode;
-
- lock_inode(current_inode);
-
- errno = current_inode->ops->dir_lookup(current_inode, dnode);
-
- if (errno == ENOENT && (walk_options & VFS_WALK_MKPARENT)) {
- if (!current_inode->ops->mkdir) {
- errno = ENOTSUP;
- } else {
- errno = current_inode->ops->mkdir(current_inode, dnode);
- }
- }
-
- vfs_dcache_add(current_level, dnode);
- unlock_inode(current_inode);
-
- if (errno) {
- unlock_dnode(current_level);
- goto cleanup;
- }
- }
-
- unlock_dnode(current_level);
-
- j = 0;
- current_level = dnode;
- cont:
- current = lookahead;
- };
-
- *dentry = current_level;
- return 0;
-
-cleanup:
- vfs_d_free(dnode);
-error:
- *dentry = NULL;
- return errno;
-}
-
-#define VFS_MAX_SYMLINK 16
-
-int
-vfs_walk(struct v_dnode* start,
- const char* path,
- struct v_dnode** dentry,
- struct hstr* component,
- int options)
-{
- struct v_dnode* interim;
- const char* pathname = path;
- int errno = __vfs_walk(start, path, &interim, component, options);
- int counter = 0;
-
- // FIXME This is NOT a correct way to resolve symlink!
- while (!errno && interim->inode && (options & VFS_WALK_NOFOLLOW)) {
- if (counter >= VFS_MAX_SYMLINK) {
- errno = ELOOP;
- continue;
- }
- if ((interim->inode->itype & VFS_IFSYMLINK) &&
- interim->inode->ops->read_symlink) {
-
- lock_inode(interim->inode);
- errno =
- interim->inode->ops->read_symlink(interim->inode, &pathname);
- unlock_inode(interim->inode);
-
- if (errno) {
- break;
- }
- } else {
- break;
- }
- errno = __vfs_walk(start, pathname, &interim, component, options);
- counter++;
- }
-
- *dentry = errno ? 0 : interim;
-
- return errno;
-}
-
int
vfs_open(struct v_dnode* dnode, struct v_file** file)
{
memset(inode, 0, sizeof(*inode));
mutex_init(&inode->lock);
+ llist_init_head(&inode->xattrs);
sb->ops.init_inode(sb, inode);
#define FLOCATE_CREATE_EMPTY 1
int
-__vfs_getfd(int fd, struct v_fd** fd_s)
+vfs_getfd(int fd, struct v_fd** fd_s)
{
if (TEST_FD(fd) && (*fd_s = __current->fdtable->fds[fd])) {
return 0;
{
struct v_fd* fd_s;
int errno = 0;
- if ((errno = __vfs_getfd(fd, &fd_s))) {
+ if ((errno = vfs_getfd(fd, &fd_s))) {
goto done_err;
}
struct v_fd* fd_s;
int errno;
- if ((errno = __vfs_getfd(fd, &fd_s))) {
+ if ((errno = vfs_getfd(fd, &fd_s))) {
goto done;
}
{
int errno = 0;
struct v_fd* fd_s;
- if ((errno = __vfs_getfd(fd, &fd_s))) {
+ if ((errno = vfs_getfd(fd, &fd_s))) {
goto done;
}
{
int errno = 0;
struct v_fd* fd_s;
- if ((errno = __vfs_getfd(fd, &fd_s))) {
+ if ((errno = vfs_getfd(fd, &fd_s))) {
goto done;
}
{
int errno = 0;
struct v_fd* fd_s;
- if ((errno = __vfs_getfd(fd, &fd_s))) {
+ if ((errno = vfs_getfd(fd, &fd_s))) {
goto done;
}
len += cpy_size;
if (len < size) {
- buf[len++] = PATH_DELIM;
+ buf[len++] = VFS_PATH_DELIM;
}
return len;
{
int errno;
struct v_fd* fd_s;
- if ((errno = __vfs_getfd(fd, &fd_s))) {
+ if ((errno = vfs_getfd(fd, &fd_s))) {
goto done;
}
{
int errno;
struct v_fd* fd_s;
- if ((errno = __vfs_getfd(dirfd, &fd_s))) {
+ if ((errno = vfs_getfd(dirfd, &fd_s))) {
goto done;
}
{
int errno;
struct v_fd* fd_s;
- if ((errno = __vfs_getfd(fd, &fd_s))) {
+ if ((errno = vfs_getfd(fd, &fd_s))) {
goto done;
}
{
int errno;
struct v_fd* fd_s;
- if (!(errno = __vfs_getfd(fildes, &fd_s))) {
+ if (!(errno = vfs_getfd(fildes, &fd_s))) {
errno = vfs_fsync(fd_s->file);
}
int errno;
struct v_fd *oldfd_s, *newfd_s;
- if ((errno = __vfs_getfd(oldfd, &oldfd_s))) {
+ if ((errno = vfs_getfd(oldfd, &oldfd_s))) {
goto done;
}
{
int errno, newfd;
struct v_fd *oldfd_s, *newfd_s;
- if ((errno = __vfs_getfd(oldfd, &oldfd_s))) {
+ if ((errno = vfs_getfd(oldfd, &oldfd_s))) {
goto done;
}
struct v_fd* fd_s;
int errno = 0;
- if ((errno = __vfs_getfd(fd, &fd_s))) {
+ if ((errno = vfs_getfd(fd, &fd_s))) {
goto done;
}
size_t len = 0;
if (!__current->cwd) {
- *buf = PATH_DELIM;
+ *buf = VFS_PATH_DELIM;
len = 1;
} else {
len = vfs_get_path(__current->cwd, buf, size, 0);