#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: lfs_rename.c,v 1.26 2025/11/01 04:10:47 perseant Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/namei.h>
#include <sys/resourcevar.h>
#include <sys/kernel.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/buf.h>
#include <sys/proc.h>
#include <sys/mount.h>
#include <sys/vnode.h>
#include <sys/vnode_if.h>
#include <sys/pool.h>
#include <sys/signalvar.h>
#include <sys/kauth.h>
#include <sys/syslog.h>
#include <miscfs/fifofs/fifo.h>
#include <miscfs/genfs/genfs.h>
#include <miscfs/specfs/specdev.h>
#include <ufs/lfs/ulfs_inode.h>
#include <ufs/lfs/ulfsmount.h>
#include <ufs/lfs/ulfs_bswap.h>
#include <ufs/lfs/ulfs_extern.h>
#include <ufs/lfs/lfs.h>
#include <ufs/lfs/lfs_accessors.h>
#include <ufs/lfs/lfs_extern.h>
static bool
ulfs_gro_directory_empty_p(struct mount *mp, kauth_cred_t cred,
struct vnode *vp, struct vnode *dvp)
{
(void)mp;
KASSERT(mp != NULL);
KASSERT(vp != NULL);
KASSERT(dvp != NULL);
KASSERT(vp != dvp);
KASSERT(vp->v_mount == mp);
KASSERT(dvp->v_mount == mp);
KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
return ulfs_dirempty(VTOI(vp), VTOI(dvp)->i_number, cred);
}
static int
ulfs_gro_rename_check_possible(struct mount *mp,
struct vnode *fdvp, struct vnode *fvp,
struct vnode *tdvp, struct vnode *tvp)
{
(void)mp;
KASSERT(mp != NULL);
KASSERT(fdvp != NULL);
KASSERT(fvp != NULL);
KASSERT(tdvp != NULL);
KASSERT(fdvp != fvp);
KASSERT(fdvp != tvp);
KASSERT(tdvp != fvp);
KASSERT(tdvp != tvp);
KASSERT(fvp != tvp);
KASSERT(fdvp->v_type == VDIR);
KASSERT(tdvp->v_type == VDIR);
KASSERT(fdvp->v_mount == mp);
KASSERT(fvp->v_mount == mp);
KASSERT(tdvp->v_mount == mp);
KASSERT((tvp == NULL) || (tvp->v_mount == mp));
KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
return genfs_ufslike_rename_check_possible(
VTOI(fdvp)->i_flags, VTOI(fvp)->i_flags,
VTOI(tdvp)->i_flags, (tvp? VTOI(tvp)->i_flags : 0),
(tvp != NULL),
IMMUTABLE, APPEND);
}
static int
ulfs_gro_rename_check_permitted(struct mount *mp, kauth_cred_t cred,
struct vnode *fdvp, struct vnode *fvp,
struct vnode *tdvp, struct vnode *tvp)
{
(void)mp;
KASSERT(mp != NULL);
KASSERT(fdvp != NULL);
KASSERT(fvp != NULL);
KASSERT(tdvp != NULL);
KASSERT(fdvp != fvp);
KASSERT(fdvp != tvp);
KASSERT(tdvp != fvp);
KASSERT(tdvp != tvp);
KASSERT(fvp != tvp);
KASSERT(fdvp->v_type == VDIR);
KASSERT(tdvp->v_type == VDIR);
KASSERT(fdvp->v_mount == mp);
KASSERT(fvp->v_mount == mp);
KASSERT(tdvp->v_mount == mp);
KASSERT((tvp == NULL) || (tvp->v_mount == mp));
KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
return genfs_ufslike_rename_check_permitted(cred,
fdvp, VTOI(fdvp)->i_mode, VTOI(fdvp)->i_uid,
fvp, VTOI(fvp)->i_uid,
tdvp, VTOI(tdvp)->i_mode, VTOI(tdvp)->i_uid,
tvp, (tvp? VTOI(tvp)->i_uid : 0));
}
static int
ulfs_gro_remove_check_possible(struct mount *mp,
struct vnode *dvp, struct vnode *vp)
{
(void)mp;
KASSERT(mp != NULL);
KASSERT(dvp != NULL);
KASSERT(vp != NULL);
KASSERT(dvp != vp);
KASSERT(dvp->v_type == VDIR);
KASSERT(vp->v_type != VDIR);
KASSERT(dvp->v_mount == mp);
KASSERT(vp->v_mount == mp);
KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
return genfs_ufslike_remove_check_possible(
VTOI(dvp)->i_flags, VTOI(vp)->i_flags,
IMMUTABLE, APPEND);
}
static int
ulfs_gro_remove_check_permitted(struct mount *mp, kauth_cred_t cred,
struct vnode *dvp, struct vnode *vp)
{
(void)mp;
KASSERT(mp != NULL);
KASSERT(dvp != NULL);
KASSERT(vp != NULL);
KASSERT(dvp != vp);
KASSERT(dvp->v_type == VDIR);
KASSERT(vp->v_type != VDIR);
KASSERT(dvp->v_mount == mp);
KASSERT(vp->v_mount == mp);
KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
return genfs_ufslike_remove_check_permitted(cred,
dvp, VTOI(dvp)->i_mode, VTOI(dvp)->i_uid, vp, VTOI(vp)->i_uid);
}
static bool
ulfs_rename_ulr_overlap_p(const struct ulfs_lookup_results *fulr,
const struct ulfs_lookup_results *tulr)
{
doff_t from_prev_start, from_prev_end, to_start, to_end;
KASSERT(fulr != NULL);
KASSERT(tulr != NULL);
KASSERT(fulr != tulr);
from_prev_end = fulr->ulr_offset;
KASSERT(fulr->ulr_count <= from_prev_end);
from_prev_start = (from_prev_end - fulr->ulr_count);
to_start = tulr->ulr_offset;
KASSERT(tulr->ulr_count < (LFS_MAXDIRSIZE - to_start));
to_end = (to_start + tulr->ulr_count);
return
(((to_start <= from_prev_start) && (from_prev_start < to_end)) ||
((to_start <= from_prev_end) && (from_prev_end < to_end)));
}
static int
ulfs_direct_namlen(const LFS_DIRHEADER *ep, const struct vnode *vp)
{
struct lfs *fs;
KASSERT(ep != NULL);
KASSERT(vp != NULL);
KASSERT(VTOI(vp) != NULL);
KASSERT(VTOI(vp)->i_ump != NULL);
KASSERT(VTOI(vp)->i_lfs != NULL);
fs = VTOI(vp)->i_lfs;
return lfs_dir_getnamlen(fs, ep);
}
static int
ulfs_rename_recalculate_fulr(struct vnode *dvp,
struct ulfs_lookup_results *fulr, const struct ulfs_lookup_results *tulr,
const struct componentname *fcnp)
{
struct mount *mp;
struct lfs *fs;
struct ulfsmount *ump;
int dirblksiz;
doff_t search_start, search_end;
doff_t offset;
struct buf *bp;
char *dirbuf;
LFS_DIRHEADER *ep;
uint32_t reclen;
uint32_t prev_reclen;
int error;
KASSERT(dvp != NULL);
KASSERT(dvp->v_mount != NULL);
KASSERT(VTOI(dvp) != NULL);
KASSERT(fulr != NULL);
KASSERT(tulr != NULL);
KASSERT(fulr != tulr);
KASSERT(ulfs_rename_ulr_overlap_p(fulr, tulr));
mp = dvp->v_mount;
ump = VFSTOULFS(mp);
fs = ump->um_lfs;
KASSERT(ump != NULL);
KASSERT(ump == VTOI(dvp)->i_ump);
KASSERT(fs == VTOI(dvp)->i_lfs);
dirblksiz = fs->um_dirblksiz;
KASSERT(0 < dirblksiz);
KASSERT((dirblksiz & (dirblksiz - 1)) == 0);
KASSERT(dirblksiz <= mp->mnt_stat.f_iosize);
search_start = tulr->ulr_offset;
KASSERT(fulr->ulr_reclen < (LFS_MAXDIRSIZE - fulr->ulr_offset));
search_end = (fulr->ulr_offset + fulr->ulr_reclen);
KASSERT(search_start <= search_end);
KASSERT((search_end - (search_start &~ (dirblksiz - 1))) <= dirblksiz);
dirbuf = NULL;
bp = NULL;
error = ulfs_blkatoff(dvp, (off_t)search_start, &dirbuf, &bp, false);
if (error)
return error;
KASSERT(dirbuf != NULL);
KASSERT(bp != NULL);
KASSERT((search_end - search_start) <=
(bp->b_bcount - (search_start & (mp->mnt_stat.f_iosize - 1))));
prev_reclen = fulr->ulr_count;
offset = search_start;
for (;;) {
KASSERT(search_start <= offset);
KASSERT(offset < search_end);
ep = (LFS_DIRHEADER *)(dirbuf + (offset - search_start));
reclen = lfs_dir_getreclen(fs, ep);
if (lfs_dir_getino(fs, ep) == 0)
goto next;
if (lfs_dir_getino(fs, ep) == ULFS_WINO)
goto next;
if (fcnp->cn_namelen != ulfs_direct_namlen(ep, dvp))
goto next;
if (memcmp(lfs_dir_nameptr(fs, ep), fcnp->cn_nameptr, fcnp->cn_namelen))
goto next;
break;
next:
if (! ((reclen < search_end) &&
(offset < (search_end - reclen)))) {
brelse(bp, 0);
return EIO;
}
KASSERT(reclen < search_end);
KASSERT(offset < (search_end - reclen));
KASSERT((offset &~ (dirblksiz - 1)) ==
((offset + reclen) &~ (dirblksiz - 1)));
prev_reclen = reclen;
offset += reclen;
}
fulr->ulr_offset = offset;
fulr->ulr_reclen = reclen;
fulr->ulr_count = ((offset & (dirblksiz - 1))? prev_reclen : 0);
brelse(bp, 0);
return 0;
}
static int
ulfs_gro_remove(struct mount *mp, kauth_cred_t cred,
struct vnode *dvp, struct componentname *cnp, void *de, struct vnode *vp,
nlink_t *tvp_nlinkp)
{
struct ulfs_lookup_results *ulr = de;
int error;
KASSERT(mp != NULL);
KASSERT(dvp != NULL);
KASSERT(cnp != NULL);
KASSERT(ulr != NULL);
KASSERT(vp != NULL);
KASSERT(dvp != vp);
KASSERT(dvp->v_mount == mp);
KASSERT(vp->v_mount == mp);
KASSERT(dvp->v_type == VDIR);
KASSERT(vp->v_type != VDIR);
KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
KASSERT(cnp->cn_nameiop == DELETE);
error = ulfs_dirremove(dvp, ulr, VTOI(vp), cnp->cn_flags, 0);
*tvp_nlinkp = VTOI(vp)->i_nlink;
return error;
}
static int
ulfs_gro_lookup(struct mount *mp, struct vnode *dvp,
struct componentname *cnp, void *de_ret, struct vnode **vp_ret)
{
struct ulfs_lookup_results *ulr_ret = de_ret;
struct vnode *vp = NULL;
int error;
(void)mp;
KASSERT(mp != NULL);
KASSERT(dvp != NULL);
KASSERT(cnp != NULL);
KASSERT(ulr_ret != NULL);
KASSERT(vp_ret != NULL);
KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
cnp->cn_flags &=~ MODMASK;
cnp->cn_flags |= (LOCKPARENT | LOCKLEAF);
error = relookup(dvp, &vp, cnp, 0 );
if ((error == 0) && (vp == NULL)) {
error = ENOENT;
goto out;
} else if (error) {
return error;
}
KASSERT(vp != NULL);
VOP_UNLOCK(vp);
out: *ulr_ret = VTOI(dvp)->i_crap;
*vp_ret = vp;
return error;
}
static bool
ulfs_rmdired_p(struct vnode *vp)
{
KASSERT(vp != NULL);
KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
KASSERT(vp->v_type == VDIR);
return (VTOI(vp)->i_size == 0);
}
static int
ulfs_read_dotdot(struct vnode *vp, kauth_cred_t cred, ino_t *ino_ret)
{
struct lfs *fs;
union lfs_dirtemplate dirbuf;
LFS_DIRHEADER *dotdot;
const char *name;
int error;
KASSERT(vp != NULL);
KASSERT(ino_ret != NULL);
KASSERT(vp->v_type == VDIR);
KASSERT(VTOI(vp) != NULL);
KASSERT(VTOI(vp)->i_lfs != NULL);
fs = VTOI(vp)->i_lfs;
error = ulfs_bufio(UIO_READ, vp, &dirbuf, sizeof dirbuf, (off_t)0,
IO_NODELOCKED, cred, NULL, NULL);
if (error)
return error;
dotdot = lfs_dirtemplate_dotdot(fs, &dirbuf);
name = lfs_dirtemplate_dotdotname(fs, &dirbuf);
if (lfs_dir_getnamlen(fs, dotdot) != 2 ||
name[0] != '.' ||
name[1] != '.')
return ENOTDIR;
*ino_ret = lfs_dir_getino(fs, dotdot);
return 0;
}
static int
ulfs_gro_lock_directory(struct mount *mp, struct vnode *vp)
{
(void)mp;
KASSERT(mp != NULL);
KASSERT(vp != NULL);
KASSERT(vp->v_mount == mp);
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
if (ulfs_rmdired_p(vp)) {
VOP_UNLOCK(vp);
return ENOENT;
}
return 0;
}
static int
ulfs_gro_genealogy(struct mount *mp, kauth_cred_t cred,
struct vnode *fdvp, struct vnode *tdvp,
struct vnode **intermediate_node_ret)
{
struct vnode *vp, *dvp;
ino_t dotdot_ino = -1;
int error;
KASSERT(mp != NULL);
KASSERT(fdvp != NULL);
KASSERT(tdvp != NULL);
KASSERT(fdvp != tdvp);
KASSERT(intermediate_node_ret != NULL);
KASSERT(fdvp->v_mount == mp);
KASSERT(tdvp->v_mount == mp);
KASSERT(fdvp->v_type == VDIR);
KASSERT(tdvp->v_type == VDIR);
error = ulfs_gro_lock_directory(mp, tdvp);
if (error)
return error;
vp = tdvp;
vref(vp);
for (;;) {
KASSERT(vp != NULL);
KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
KASSERT(vp->v_mount == mp);
KASSERT(vp->v_type == VDIR);
KASSERT(!ulfs_rmdired_p(vp));
if (VTOI(vp)->i_number == ULFS_ROOTINO) {
vput(vp);
*intermediate_node_ret = NULL;
return 0;
}
error = ulfs_read_dotdot(vp, cred, &dotdot_ino);
if (error) {
vput(vp);
return error;
}
if (VTOI(fdvp)->i_number == dotdot_ino) {
VOP_UNLOCK(vp);
*intermediate_node_ret = vp;
return 0;
}
error = vcache_get(mp, &dotdot_ino, sizeof(dotdot_ino), &dvp);
vput(vp);
if (error)
return error;
error = vn_lock(dvp, LK_EXCLUSIVE);
if (error) {
vrele(dvp);
return error;
}
KASSERT(dvp != NULL);
KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
vp = dvp;
if (vp->v_type != VDIR) {
vput(vp);
return ENOTDIR;
}
if (ulfs_rmdired_p(vp)) {
vput(vp);
return ENOENT;
}
}
}
static int
ulfs_gro_rename(struct mount *mp, kauth_cred_t cred,
struct vnode *fdvp, struct componentname *fcnp,
void *fde, struct vnode *fvp,
struct vnode *tdvp, struct componentname *tcnp,
void *tde, struct vnode *tvp, nlink_t *tvp_nlinkp)
{
struct lfs *fs;
struct ulfs_lookup_results *fulr = fde;
struct ulfs_lookup_results *tulr = tde;
bool directory_p, reparent_p;
int error;
KASSERT(mp != NULL);
KASSERT(fdvp != NULL);
KASSERT(fcnp != NULL);
KASSERT(fulr != NULL);
KASSERT(fvp != NULL);
KASSERT(tdvp != NULL);
KASSERT(tcnp != NULL);
KASSERT(tulr != NULL);
KASSERT(fulr != tulr);
KASSERT(fdvp != fvp);
KASSERT(fdvp != tvp);
KASSERT(tdvp != fvp);
KASSERT(tdvp != tvp);
KASSERT(fvp != tvp);
KASSERT(fdvp->v_mount == mp);
KASSERT(fvp->v_mount == mp);
KASSERT(tdvp->v_mount == mp);
KASSERT((tvp == NULL) || (tvp->v_mount == mp));
KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
fs = VTOI(fdvp)->i_lfs;
KASSERT(fs == VTOI(tdvp)->i_lfs);
if ((nlink_t)VTOI(fvp)->i_nlink >= LINK_MAX)
return EMLINK;
directory_p = (fvp->v_type == VDIR);
KASSERT(directory_p == ((VTOI(fvp)->i_mode & LFS_IFMT) == LFS_IFDIR));
KASSERT((tvp == NULL) || (directory_p == (tvp->v_type == VDIR)));
KASSERT((tvp == NULL) || (directory_p ==
((VTOI(tvp)->i_mode & LFS_IFMT) == LFS_IFDIR)));
reparent_p = (fdvp != tdvp);
KASSERT(reparent_p == (VTOI(fdvp)->i_number != VTOI(tdvp)->i_number));
error = 0;
KASSERT((nlink_t)VTOI(fvp)->i_nlink < LINK_MAX);
VTOI(fvp)->i_nlink++;
DIP_ASSIGN(VTOI(fvp), nlink, VTOI(fvp)->i_nlink);
VTOI(fvp)->i_state |= IN_CHANGE;
error = lfs_update(fvp, NULL, NULL, UPDATE_DIROP);
if (error)
goto whymustithurtsomuch;
if (tvp == NULL) {
if (directory_p && reparent_p) {
if ((nlink_t)VTOI(tdvp)->i_nlink >= LINK_MAX) {
error = EMLINK;
goto whymustithurtsomuch;
}
KASSERT((nlink_t)VTOI(tdvp)->i_nlink < LINK_MAX);
VTOI(tdvp)->i_nlink++;
DIP_ASSIGN(VTOI(tdvp), nlink, VTOI(tdvp)->i_nlink);
VTOI(tdvp)->i_state |= IN_CHANGE;
error = lfs_update(tdvp, NULL, NULL, UPDATE_DIROP);
if (error) {
KASSERT(0 < VTOI(tdvp)->i_nlink);
VTOI(tdvp)->i_nlink--;
DIP_ASSIGN(VTOI(tdvp), nlink,
VTOI(tdvp)->i_nlink);
VTOI(tdvp)->i_state |= IN_CHANGE;
goto whymustithurtsomuch;
}
}
error = ulfs_direnter(tdvp, tulr,
NULL, tcnp, VTOI(fvp)->i_number, LFS_IFTODT(VTOI(fvp)->i_mode),
NULL);
if (error) {
if (directory_p && reparent_p) {
KASSERT(0 < VTOI(tdvp)->i_nlink);
VTOI(tdvp)->i_nlink--;
DIP_ASSIGN(VTOI(tdvp), nlink,
VTOI(tdvp)->i_nlink);
VTOI(tdvp)->i_state |= IN_CHANGE;
(void)lfs_update(tdvp, NULL, NULL,
UPDATE_WAIT | UPDATE_DIROP);
}
goto whymustithurtsomuch;
}
} else {
if (directory_p)
cache_purge(tdvp);
error = ulfs_dirrewrite(VTOI(tdvp), tulr->ulr_offset,
VTOI(tvp), VTOI(fvp)->i_number, LFS_IFTODT(VTOI(fvp)->i_mode),
((directory_p && reparent_p) ? reparent_p : directory_p),
IN_CHANGE | IN_UPDATE);
if (error)
goto whymustithurtsomuch;
if (directory_p && !reparent_p) {
KASSERT(fdvp == tdvp);
KASSERT(0 < VTOI(tdvp)->i_nlink);
VTOI(tdvp)->i_nlink--;
DIP_ASSIGN(VTOI(tdvp), nlink, VTOI(tdvp)->i_nlink);
VTOI(tdvp)->i_state |= IN_CHANGE;
}
if (directory_p) {
if (VTOI(tvp)->i_nlink != 1)
ulfs_dirbad(VTOI(tvp), (doff_t)0,
"hard-linked directory");
VTOI(tvp)->i_nlink = 0;
DIP_ASSIGN(VTOI(tvp), nlink, 0);
error = lfs_truncate(tvp, (off_t)0, IO_SYNC, cred);
if (error)
goto whymustithurtsomuch;
}
}
if (directory_p && reparent_p) {
off_t position;
position = LFS_DIRECTSIZ(fs, 1);
error = ulfs_dirrewrite(VTOI(fvp), position,
VTOI(fdvp), VTOI(tdvp)->i_number, LFS_DT_DIR, 0, IN_CHANGE);
#if 0
if (error)
goto whymustithurtsomuch;
#endif
cache_purge(fdvp);
}
if (!reparent_p && (tvp == NULL) &&
ulfs_rename_ulr_overlap_p(fulr, tulr)) {
error = ulfs_rename_recalculate_fulr(fdvp, fulr, tulr, fcnp);
#if 0
if (error)
goto whymustithurtsomuch;
#endif
}
error = ulfs_dirremove(fdvp, fulr, VTOI(fvp), fcnp->cn_flags, 0);
if (error)
#if 0
goto whymustithurtsomuch;
#endif
goto arghmybrainhurts;
if (tvp != NULL) {
*tvp_nlinkp = VTOI(tvp)->i_nlink;
}
#if 0
genfs_rename_cache_purge(fdvp, fvp, tdvp, tvp);
#endif
goto arghmybrainhurts;
whymustithurtsomuch:
KASSERT(0 < VTOI(fvp)->i_nlink);
VTOI(fvp)->i_nlink--;
DIP_ASSIGN(VTOI(fvp), nlink, VTOI(fvp)->i_nlink);
VTOI(fvp)->i_state |= IN_CHANGE;
arghmybrainhurts:
return error;
}
static int
lfs_gro_rename(struct mount *mp, kauth_cred_t cred,
struct vnode *fdvp, struct componentname *fcnp,
void *fde, struct vnode *fvp,
struct vnode *tdvp, struct componentname *tcnp,
void *tde, struct vnode *tvp, nlink_t *tvp_nlinkp)
{
int error;
KASSERT(mp != NULL);
KASSERT(fdvp != NULL);
KASSERT(fcnp != NULL);
KASSERT(fde != NULL);
KASSERT(fvp != NULL);
KASSERT(tdvp != NULL);
KASSERT(tcnp != NULL);
KASSERT(tde != NULL);
KASSERT(fdvp != fvp);
KASSERT(fdvp != tvp);
KASSERT(tdvp != fvp);
KASSERT(tdvp != tvp);
KASSERT(fvp != tvp);
KASSERT(fdvp->v_mount == mp);
KASSERT(fvp->v_mount == mp);
KASSERT(tdvp->v_mount == mp);
KASSERT((tvp == NULL) || (tvp->v_mount == mp));
KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
error = lfs_set_dirop(tdvp, tvp);
if (error != 0)
return error;
MARK_VNODE(fdvp);
MARK_VNODE(fvp);
error = ulfs_gro_rename(mp, cred,
fdvp, fcnp, fde, fvp,
tdvp, tcnp, tde, tvp,
tvp_nlinkp);
if (tvp && VTOI(tvp)->i_nlink == 0)
lfs_orphan(VTOI(tvp)->i_lfs, tvp);
UNMARK_VNODE(fdvp);
UNMARK_VNODE(fvp);
UNMARK_VNODE(tdvp);
if (tvp) {
UNMARK_VNODE(tvp);
}
lfs_unset_dirop(VFSTOULFS(mp)->um_lfs, tdvp, "rename");
vrele(tdvp);
if (tvp) {
vrele(tvp);
}
return error;
}
static const struct genfs_rename_ops lfs_genfs_rename_ops = {
.gro_directory_empty_p = ulfs_gro_directory_empty_p,
.gro_rename_check_possible = ulfs_gro_rename_check_possible,
.gro_rename_check_permitted = ulfs_gro_rename_check_permitted,
.gro_remove_check_possible = ulfs_gro_remove_check_possible,
.gro_remove_check_permitted = ulfs_gro_remove_check_permitted,
.gro_rename = lfs_gro_rename,
.gro_remove = ulfs_gro_remove,
.gro_lookup = ulfs_gro_lookup,
.gro_genealogy = ulfs_gro_genealogy,
.gro_lock_directory = ulfs_gro_lock_directory,
};
static int
lfs_sane_rename(
struct vnode *fdvp, struct componentname *fcnp,
struct vnode *tdvp, struct componentname *tcnp,
kauth_cred_t cred, bool posixly_correct)
{
struct ulfs_lookup_results fulr, tulr;
KASSERT(fcnp != NULL);
KASSERT(tcnp != NULL);
KASSERT(fcnp != tcnp);
KASSERT(fcnp->cn_nameptr != NULL);
KASSERT(tcnp->cn_nameptr != NULL);
if ((fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT)
return EINVAL;
if ((fcnp->cn_namelen == 1) && (fcnp->cn_nameptr[0] == '.'))
return EINVAL;
if ((tcnp->cn_namelen == 1) && (tcnp->cn_nameptr[0] == '.'))
return EINVAL;
return genfs_sane_rename(&lfs_genfs_rename_ops,
fdvp, fcnp, &fulr, tdvp, tcnp, &tulr,
cred, posixly_correct);
}
int
lfs_rename(void *v)
{
return genfs_insane_rename(v, &lfs_sane_rename);
}