*/
#include <klibc/string.h>
-#include <lunaix/dirent.h>
#include <lunaix/foptions.h>
#include <lunaix/fs.h>
#include <lunaix/mm/cake.h>
#include <lunaix/process.h>
#include <lunaix/spike.h>
#include <lunaix/syscall.h>
+#include <lunaix/syscall_utils.h>
#include <lunaix/fs/twifs.h>
+#include <sys/dirent_defs.h>
+
static struct cake_pile* dnode_pile;
static struct cake_pile* inode_pile;
static struct cake_pile* file_pile;
atomic_fetch_sub(&file->dnode->ref_count, 1);
file->inode->open_count--;
- // Prevent dead lock.
- // This happened when process is terminated while blocking on read.
- // In that case, the process is still holding the inode lock and it will
- // never get released.
/*
+ * Prevent dead lock.
+ * This happened when process is terminated while blocking on read.
+ * In that case, the process is still holding the inode lock and it
+ will never get released.
* The unlocking should also include ownership check.
*
* To see why, consider two process both open the same file both with
pcache_release(inode->pg_cache);
vfree(inode->pg_cache);
}
- inode->ops->sync(inode);
+ // we don't need to sync inode.
+ // If an inode can be free, then it must be properly closed.
+ // Hence it must be synced already!
+ if (inode->destruct) {
+ inode->destruct(inode);
+ }
hlist_delete(&inode->hash_list);
cake_release(inode_pile, inode);
}
/* ---- System call definition and support ---- */
#define FLOCATE_CREATE_EMPTY 1
+#define FLOCATE_CREATE_ONLY 2
int
vfs_getfd(int fd, struct v_fd** fd_s)
}
errno = vfs_walk(*fdir, name.value, file, NULL, 0);
- if (errno != ENOENT || !(options & FLOCATE_CREATE_EMPTY)) {
+
+ if (errno != ENOENT && (options & FLOCATE_CREATE_ONLY)) {
+ return EEXIST;
+ }
+
+ if (errno != ENOENT ||
+ !(options & (FLOCATE_CREATE_EMPTY | FLOCATE_CREATE_ONLY))) {
return errno;
}
{
int errno, fd;
struct v_dnode *dentry, *file;
- struct v_file* ofile = 0;
+ struct v_file* ofile = NULL;
errno = __vfs_try_locate_file(
path, &dentry, &file, (options & FO_CREATE) ? FLOCATE_CREATE_EMPTY : 0);
- if (errno || (errno = vfs_open(file, &ofile))) {
- return errno;
- }
+ if (!errno && !(errno = vfs_alloc_fdslot(&fd))) {
- struct v_inode* o_inode = ofile->inode;
+ if (errno || (errno = vfs_open(file, &ofile))) {
+ return errno;
+ }
- if (!errno && !(errno = vfs_alloc_fdslot(&fd))) {
struct v_fd* fd_s = cake_grab(fd_pile);
memset(fd_s, 0, sizeof(*fd_s));
const int len,
const int dtype)
{
- struct dirent* dent = (struct dirent*)dctx->cb_data;
+ struct lx_dirent* dent = (struct lx_dirent*)dctx->cb_data;
strncpy(dent->d_name, name, DIRENT_NAME_MAX_LEN);
dent->d_nlen = len;
dent->d_type = dtype;
}
-__DEFINE_LXSYSCALL2(int, readdir, int, fd, struct dirent*, dent)
+__DEFINE_LXSYSCALL2(int, sys_readdir, int, fd, struct lx_dirent*, dent)
{
struct v_fd* fd_s;
int errno;
link_target)
{
int errno;
- struct v_dnode* dnode;
- if ((errno = vfs_walk_proc(pathname, &dnode, NULL, 0))) {
+ struct v_dnode *dnode, *file;
+ if ((errno = __vfs_try_locate_file(
+ pathname, &dnode, &file, FLOCATE_CREATE_ONLY))) {
goto done;
}
- if (errno = vfs_check_writable(dnode)) {
+ if (errno = vfs_check_writable(file)) {
goto done;
}
- if (!dnode->inode->ops->set_symlink) {
+ if (!file->inode->ops->set_symlink) {
errno = ENOTSUP;
goto done;
}
- lock_inode(dnode->inode);
+ lock_inode(file->inode);
- errno = dnode->inode->ops->set_symlink(dnode->inode, link_target);
+ errno = file->inode->ops->set_symlink(file->inode, link_target);
- unlock_inode(dnode->inode);
+ unlock_inode(file->inode);
done:
return DO_STATUS(errno);
}
+void
+vfs_ref_file(struct v_file* file)
+{
+ atomic_fetch_add(&file->ref_count, 1);
+}
+
void
vfs_ref_dnode(struct v_dnode* dnode)
{
}
}
- buf[len + 1] = '\0';
+ buf[len] = '\0';
ret_ptr = buf;