+ return ret_ptr;
+}
+
+int
+vfs_do_rename(struct v_dnode* current, struct v_dnode* target)
+{
+ if (current->inode->id == target->inode->id) {
+ // hard link
+ return 0;
+ }
+
+ if (current->ref_count > 1 || target->ref_count > 1) {
+ return EBUSY;
+ }
+
+ if (current->super_block != target->super_block) {
+ return EXDEV;
+ }
+
+ int errno = 0;
+
+ struct v_dnode* oldparent = current->parent;
+ struct v_dnode* newparent = target->parent;
+
+ lock_dnode(current);
+ lock_dnode(target);
+ if (oldparent)
+ lock_dnode(oldparent);
+ if (newparent)
+ lock_dnode(newparent);
+
+ if (!llist_empty(&target->children)) {
+ errno = ENOTEMPTY;
+ unlock_dnode(target);
+ goto cleanup;
+ }
+
+ if ((errno = current->inode->ops.rename(current->inode, current, target))) {
+ unlock_dnode(target);
+ goto cleanup;
+ }
+
+ // re-position current
+ hstrcpy(¤t->name, &target->name);
+ vfs_dcache_rehash(newparent, current);
+
+ // detach target
+ vfs_dcache_remove(target);
+
+ unlock_dnode(target);
+
+cleanup:
+ unlock_dnode(current);
+ if (oldparent)
+ unlock_dnode(oldparent);
+ if (newparent)
+ unlock_dnode(newparent);
+
+ return errno;
+}
+
+__DEFINE_LXSYSCALL2(int, rename, const char*, oldpath, const char*, newpath)
+{
+ struct v_dnode *cur, *target_parent, *target;
+ struct hstr name = HSTR(valloc(VFS_NAME_MAXLEN), 0);
+ int errno = 0;
+
+ if ((errno = vfs_walk(__current->cwd, oldpath, &cur, NULL, 0))) {
+ goto done;
+ }
+
+ if ((errno = vfs_walk(
+ __current->cwd, newpath, &target_parent, &name, VFS_WALK_PARENT))) {
+ goto done;
+ }
+
+ errno = vfs_walk(target_parent, name.value, &target, NULL, 0);
+ if (errno == ENOENT) {
+ target = vfs_d_alloc();
+ } else if (errno) {
+ goto done;
+ }
+
+ if (!target) {
+ errno = ENOMEM;
+ goto done;
+ }
+
+ hstrcpy(&target->name, &name);
+
+ if (!(errno = vfs_do_rename(cur, target))) {
+ vfs_d_free(target);
+ }
+
+done:
+ vfree(name.value);
+ return DO_STATUS(errno);
+}
+
+__DEFINE_LXSYSCALL3(int,
+ mount,
+ const char*,
+ source,
+ const char*,
+ target,
+ const char*,
+ fstype)
+{
+ struct v_dnode *dev, *mnt;
+ int errno = 0;
+
+ if ((errno = vfs_walk(__current->cwd, source, &dev, NULL, 0))) {
+ goto done;
+ }
+
+ if ((errno = vfs_walk(__current->cwd, target, &mnt, NULL, 0))) {
+ goto done;
+ }
+
+ if (!(dev->inode->itype & VFS_IFVOLDEV)) {
+ errno = ENOTDEV;
+ goto done;
+ }
+
+ if (mnt->ref_count > 1) {
+ errno = EBUSY;
+ goto done;
+ }
+
+ // FIXME should not touch the underlying fs!
+ struct device* device =
+ (struct device*)((struct twifs_node*)dev->inode->data)->data;
+
+ errno = vfs_mount_at(fstype, device, mnt);
+
+done:
+ return DO_STATUS(errno);
+}
+
+__DEFINE_LXSYSCALL1(int, unmount, const char*, target)
+{
+ return vfs_unmount(target);