vfs_close(struct v_file* file)
{
int errno = 0;
- if (!(errno = file->ops->close(file))) {
+ if (file->ref_count > 1) {
+ atomic_fetch_sub(&file->ref_count, 1);
+ } else if (!(errno = file->ops->close(file))) {
atomic_fetch_sub(&file->dnode->ref_count, 1);
file->inode->open_count--;
+
+ // Remove 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.
+ // FIXME is this a good solution?
+ /*
+ * Consider two process both open the same file both with fd=x.
+ * Process A: busy on reading x
+ * Process B: do nothing with x
+ * Assume that, after a very short time, process B get terminated while
+ * process A is still busy in it's reading business. By this design, the
+ * inode lock of this file x is get released by B rather than A. And
+ * this will cause a probable race condition on A if other process is
+ * writing to this file later after B exit.
+ *
+ * A possible solution is to add a owner identification in the lock
+ * context, so only the lock holder can do the release.
+ */
+ if (mutex_on_hold(&file->inode->lock)) {
+ unlock_inode(file->inode);
+ }
mnt_chillax(file->dnode->mnt);
pcache_commit_all(file->inode);
char name_str[VFS_NAME_MAXLEN];
struct hstr name = HSTR(name_str, 0);
int errno;
+
+ name_str[0] = 0;
if ((errno = vfs_walk_proc(path, fdir, &name, VFS_WALK_PARENT))) {
return errno;
}
goto done_err;
}
- if (fd_s->file->ref_count > 1) {
- fd_s->file->ref_count--;
- } else if ((errno = vfs_close(fd_s->file))) {
+ if ((errno = vfs_close(fd_s->file))) {
goto done_err;
}
__vfs_readdir_callback };
errno = 1;
if (dent->d_offset == 0) {
- __vfs_readdir_callback(&dctx, vfs_dot.value, vfs_dot.len, 0);
+ __vfs_readdir_callback(&dctx, vfs_dot.value, vfs_dot.len, DT_DIR);
} else if (dent->d_offset == 1) {
- __vfs_readdir_callback(&dctx, vfs_ddot.value, vfs_ddot.len, 0);
+ __vfs_readdir_callback(&dctx, vfs_ddot.value, vfs_ddot.len, DT_DIR);
} else {
dctx.index -= 2;
if ((errno = fd_s->file->ops->readdir(fd_s->file, &dctx)) != 1) {
file->inode->atime = clock_unixtime();
- __SYSCALL_INTERRUPTIBLE({
- if ((file->inode->itype & VFS_IFSEQDEV) || (fd_s->flags & FO_DIRECT)) {
- errno = file->ops->read(file->inode, buf, count, file->f_pos);
- } else {
- errno = pcache_read(file->inode, buf, count, file->f_pos);
- }
- })
+ if ((file->inode->itype & VFS_IFSEQDEV) || (fd_s->flags & FO_DIRECT)) {
+ errno = file->ops->read(file->inode, buf, count, file->f_pos);
+ } else {
+ errno = pcache_read(file->inode, buf, count, file->f_pos);
+ }
if (errno > 0) {
file->f_pos += errno;
file->inode->mtime = clock_unixtime();
- __SYSCALL_INTERRUPTIBLE({
- if ((file->inode->itype & VFS_IFSEQDEV) || (fd_s->flags & FO_DIRECT)) {
- errno = file->ops->write(file->inode, buf, count, file->f_pos);
- } else {
- errno = pcache_write(file->inode, buf, count, file->f_pos);
- }
- })
+ if ((file->inode->itype & VFS_IFSEQDEV) || (fd_s->flags & FO_DIRECT)) {
+ errno = file->ops->write(file->inode, buf, count, file->f_pos);
+ } else {
+ errno = pcache_write(file->inode, buf, count, file->f_pos);
+ }
if (errno > 0) {
file->f_pos += errno;
return 0;
}
+int
+vfs_get_dtype(int itype)
+{
+ switch (itype) {
+ case VFS_IFDIR:
+ return DT_DIR;
+ case VFS_IFSYMLINK:
+ return DT_SYMLINK;
+ default:
+ return DT_PIPE;
+ }
+}
+
__DEFINE_LXSYSCALL3(int, realpathat, int, fd, char*, buf, size_t, size)
{
int errno;
return DO_STATUS(errno);
}
+void
+vfs_ref_dnode(struct v_dnode* dnode)
+{
+ atomic_fetch_add(&dnode->ref_count, 1);
+ mnt_mkbusy(dnode->mnt);
+}
+
+void
+vfs_unref_dnode(struct v_dnode* dnode)
+{
+ atomic_fetch_sub(&dnode->ref_count, 1);
+ mnt_chillax(dnode->mnt);
+}
+
int
-__vfs_do_chdir(struct v_dnode* dnode)
+vfs_do_chdir(struct proc_info* proc, struct v_dnode* dnode)
{
int errno = 0;
goto done;
}
- if (__current->cwd) {
- atomic_fetch_sub(&__current->cwd->ref_count, 1);
- mnt_chillax(__current->cwd->mnt);
+ if (proc->cwd) {
+ vfs_unref_dnode(proc->cwd);
}
- atomic_fetch_add(&dnode->ref_count, 1);
- mnt_mkbusy(dnode->mnt);
- __current->cwd = dnode;
+ vfs_ref_dnode(dnode);
+ proc->cwd = dnode;
unlock_dnode(dnode);
goto done;
}
- errno = __vfs_do_chdir(dnode);
+ errno = vfs_do_chdir(__current, dnode);
done:
return DO_STATUS(errno);
goto done;
}
- errno = __vfs_do_chdir(fd_s->file->dnode);
+ errno = vfs_do_chdir(__current, fd_s->file->dnode);
done:
return DO_STATUS(errno);