/*-
* Copyright (c) 2012 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Taylor R Campbell.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* tmpfs_gro_directory_empty_p: Return true if the directory vp is
* empty. dvp is its parent.
*
* vp and dvp must be locked and referenced.
*/
static bool
tmpfs_gro_directory_empty_p(struct mount *mp, kauth_cred_t cred,
struct vnode *vp, struct vnode *dvp)
{
/*
* tmpfs_gro_remove_check_possible: Check whether a remove is possible
* independent of credentials.
*/
static int
tmpfs_gro_remove_check_possible(struct mount *mp,
struct vnode *dvp, struct vnode *vp)
{
/*
* If we are moving from one directory to another, detach the
* source entry and reattach it to the target directory.
*/
if (fdvp != tdvp) {
tmpfs_dir_detach(fdnode, *fdep);
tmpfs_dir_attach(tdnode, *fdep, VP_TO_TMPFS_NODE(fvp));
}
/*
* If we are replacing an existing target entry, delete it.
*
* XXX What if the target is a directory with whiteout entries?
*/
if (tvp != NULL) {
tdnode = VP_TO_TMPFS_DIR(tdvp);
/*
* Decrement the extra link count for `.' so
* the vnode will be recycled when released.
*/
VP_TO_TMPFS_NODE(tvp)->tn_links--;
}
tmpfs_dir_detach(tdnode, *tdep);
tmpfs_free_dirent(VFS_TO_TMPFS(mp), *tdep);
*tvp_nlinkp = VP_TO_TMPFS_NODE(tvp)->tn_links;
}
/*
* Update the directory entry's name if necessary, and flag
* metadata updates. A memory allocation failure here is not
* OK because we've already committed some changes that we
* can't back out at this point, hence the early allocation
* above.
*/
if (newname != NULL) {
KASSERT(tcnp->cn_namelen <= TMPFS_MAXNAMLEN);
/*
* Update the timestamps of both parent directories and
* the renamed file itself.
*/
tmpfs_update(fdvp, TMPFS_UPDATE_MTIME | TMPFS_UPDATE_CTIME);
tmpfs_update(tdvp, TMPFS_UPDATE_MTIME | TMPFS_UPDATE_CTIME);
tmpfs_update(fvp, TMPFS_UPDATE_CTIME);
genfs_rename_cache_purge(fdvp, fvp, tdvp, tvp);
return 0;
}
/*
* tmpfs_gro_remove: Rename an object over another link to itself,
* effectively removing just the original link.
*/
static int
tmpfs_gro_remove(struct mount *mp, kauth_cred_t cred,
struct vnode *dvp, struct componentname *cnp, void *de, struct vnode *vp,
nlink_t *tvp_nlinkp)
{
tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp);
struct tmpfs_dirent **dep = de;
/*
* tmpfs_rmdired_p: Check whether the directory vp has been rmdired.
*
* vp must be locked and referenced.
*/
static bool
tmpfs_rmdired_p(struct vnode *vp)
{
/*
* We need to provisionally lock tdvp to keep rmdir from
* deleting it -- or any ancestor -- at an inopportune moment.
*/
error = tmpfs_gro_lock_directory(mp, tdvp);
if (error)
return error;
/*
* If dnode is null then vp has been rmdir'd, which is
* not supposed to happen because we have it locked.
*/
KASSERT(dnode != NULL);
/* Did we hit the root without finding fdvp? */
if (dnode == VP_TO_TMPFS_NODE(vp)) {
vput(vp);
*intermediate_node_ret = NULL;
return 0;
}
/* Did we find that fdvp is an ancestor of tdvp? */
if (dnode == VP_TO_TMPFS_NODE(fdvp)) {
KASSERT(dnode->tn_vnode == fdvp);
/* Unlock vp, but keep it referenced. */
VOP_UNLOCK(vp);
*intermediate_node_ret = vp;
return 0;
}
/* Neither -- keep ascending the family tree. */
ovp = vp;
vp = NULL;
error = vcache_get(mp, &dnode, sizeof(dnode), &vp);
vput(ovp);
if (error)
return error;
error = vn_lock(vp, LK_EXCLUSIVE);
if (error) {
vrele(vp);
return error;
}
/*
* vcache_get only guarantees that dnode will not
* be freed while we get a vnode for it. It does not
* preserve any other invariants, so we must check
* whether the parent has been removed in the meantime.
*/
if (tmpfs_rmdired_p(vp)) {
vput(vp);
return ENOENT;
}
}
}
/*
* tmpfs_gro_lock_directory: Lock the directory vp, but fail if it has
* been rmdir'd.
*/
static int
tmpfs_gro_lock_directory(struct mount *mp, struct vnode *vp)
{