Module Name:    src
Committed By:   reinoud
Date:           Wed Jul 10 15:10:56 UTC 2013

Modified Files:
        src/sys/fs/udf: files.udf udf_vnops.c
Added Files:
        src/sys/fs/udf: udf_rename.c

Log Message:
Implement udf_rename() using the new genfs_rename() framework.

Fixes PR kern/47986


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/sys/fs/udf/files.udf
cvs rdiff -u -r0 -r1.1 src/sys/fs/udf/udf_rename.c
cvs rdiff -u -r1.84 -r1.85 src/sys/fs/udf/udf_vnops.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/fs/udf/files.udf
diff -u src/sys/fs/udf/files.udf:1.4 src/sys/fs/udf/files.udf:1.5
--- src/sys/fs/udf/files.udf:1.4	Tue Mar  2 16:43:48 2010
+++ src/sys/fs/udf/files.udf	Wed Jul 10 15:10:56 2013
@@ -1,4 +1,4 @@
-#	$NetBSD: files.udf,v 1.4 2010/03/02 16:43:48 pooka Exp $
+#	$NetBSD: files.udf,v 1.5 2013/07/10 15:10:56 reinoud Exp $
 
 deffs	UDF
 
@@ -12,4 +12,5 @@ file	fs/udf/udf_strat_sequential.c	udf
 file	fs/udf/udf_strat_direct.c	udf
 file	fs/udf/udf_strat_rmw.c		udf
 file	fs/udf/udf_allocation.c		udf
+file	fs/udf/udf_rename.c		udf
 

Index: src/sys/fs/udf/udf_vnops.c
diff -u src/sys/fs/udf/udf_vnops.c:1.84 src/sys/fs/udf/udf_vnops.c:1.85
--- src/sys/fs/udf/udf_vnops.c:1.84	Mon Jul  8 08:21:12 2013
+++ src/sys/fs/udf/udf_vnops.c	Wed Jul 10 15:10:56 2013
@@ -1,4 +1,4 @@
-/* $NetBSD: udf_vnops.c,v 1.84 2013/07/08 08:21:12 reinoud Exp $ */
+/* $NetBSD: udf_vnops.c,v 1.85 2013/07/10 15:10:56 reinoud Exp $ */
 
 /*
  * Copyright (c) 2006, 2008 Reinoud Zandijk
@@ -32,7 +32,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__KERNEL_RCSID(0, "$NetBSD: udf_vnops.c,v 1.84 2013/07/08 08:21:12 reinoud Exp $");
+__KERNEL_RCSID(0, "$NetBSD: udf_vnops.c,v 1.85 2013/07/10 15:10:56 reinoud Exp $");
 #endif /* not lint */
 
 
@@ -667,7 +667,8 @@ udf_lookup(void *v)
 	ump = dir_node->ump;
 	*vpp = NULL;
 
-	DPRINTF(LOOKUP, ("udf_lookup called\n"));
+	DPRINTF(LOOKUP, ("udf_lookup called, lookup `%s`\n",
+		cnp->cn_nameptr));
 
 	/* simplify/clarification flags */
 	nameiop     = cnp->cn_nameiop;
@@ -720,6 +721,11 @@ udf_lookup(void *v)
 		/* special case 2 '..' */
 		DPRINTF(LOOKUP, ("\tlookup '..'\n"));
 
+		if (islastcn && cnp->cn_nameiop == RENAME) {
+			error = EINVAL;
+			goto out;
+		}
+
 		/* get our node */
 		name    = "..";
 		namelen = 2;
@@ -1534,11 +1540,6 @@ udf_do_link(struct vnode *dvp, struct vn
 	KASSERT(vp->v_type != VDIR);
 	KASSERT(dvp->v_mount == vp->v_mount);
 
-	/* lock node */
-	error = vn_lock(vp, LK_EXCLUSIVE);
-	if (error)
-		return error;
-
 	/* get attributes */
 	dir_node = VTOI(dvp);
 	udf_node = VTOI(vp);
@@ -1893,256 +1894,9 @@ udf_readlink(void *v)
 /* --------------------------------------------------------------------- */
 
 /*
- * Check if source directory is in the path of the target directory.  Target
- * is supplied locked, source is unlocked. The target is always vput before
- * returning. Modeled after UFS.
- *
- * If source is on the path from target to the root, return error.
+ * udf_rename() moved to udf_rename.c
  */
 
-static int
-udf_on_rootpath(struct udf_node *source, struct udf_node *target)
-{
-	struct udf_mount *ump = target->ump;
-	struct udf_node *res_node;
-	struct long_ad icb_loc, *root_icb;
-	const char *name;
-	int namelen;
-	int error, found;
-
-	name     = "..";
-	namelen  = 2;
-	error    = 0;
-	res_node = target;
-
-	root_icb   = &ump->fileset_desc->rootdir_icb;
-
-	/* if nodes are equal, it is no use looking */
-	if (udf_compare_icb(&source->loc, &target->loc) == 0) {
-		error = EEXIST;
-		goto out;
-	}
-
-	/* nothing can exist before the root */
-	if (udf_compare_icb(root_icb, &target->loc) == 0) {
-		error = 0;
-		goto out;
-	}
-
-	for (;;) {
-		DPRINTF(NODE, ("udf_on_rootpath : "
-			"source vp %p, looking at vp %p\n",
-			source->vnode, res_node->vnode));
-
-		/* sanity check */
-		if (res_node->vnode->v_type != VDIR) {
-			error = ENOTDIR;
-			goto out;
-		}
-
-		/* go down one level */
-		error = udf_lookup_name_in_dir(res_node->vnode, name, namelen,
-			&icb_loc, &found);
-		DPRINTF(NODE, ("\tlookup of '..' resulted in error %d, "
-			"found %d\n", error, found));
-
-		if (!found)
-			error = ENOENT;
-		if (error)
-			goto out;
-
-		/* did we encounter source node? */
-		if (udf_compare_icb(&icb_loc, &source->loc) == 0) {
-			error = EINVAL;
-			goto out;
-		}
-
-		/* did we encounter the root node? */
-		if (udf_compare_icb(&icb_loc, root_icb) == 0) {
-			error = 0;
-			goto out;
-		}
-
-		/* push our intermediate node, we're done with it */
-		/* DPRINTF(NODE, ("\tvput %p\n", target->vnode)); */
-		vput(res_node->vnode);
-
-		DPRINTF(NODE, ("\tgetting the .. node\n"));
-		error = udf_get_node(ump, &icb_loc, &res_node);
-
-		if (error) {	/* argh, bail out */
-			KASSERT(res_node == NULL);
-			// res_node = NULL;
-			goto out;
-		}
-	}
-out:
-	DPRINTF(NODE, ("\tresult: %svalid, error = %d\n", error?"in":"", error));
-
-	/* put our last node */
-	if (res_node)
-		vput(res_node->vnode);
-
-	return error;
-}
-
-/* note: i tried to follow the logics of the tmpfs rename code */
-int
-udf_rename(void *v)
-{
-	struct vop_rename_args /* {
-		struct vnode *a_fdvp;
-		struct vnode *a_fvp;
-		struct componentname *a_fcnp;
-		struct vnode *a_tdvp;
-		struct vnode *a_tvp;
-		struct componentname *a_tcnp;
-	} */ *ap = v;
-	struct vnode *tvp = ap->a_tvp;
-	struct vnode *tdvp = ap->a_tdvp;
-	struct vnode *fvp = ap->a_fvp;
-	struct vnode *fdvp = ap->a_fdvp;
-	struct componentname *tcnp = ap->a_tcnp;
-	struct componentname *fcnp = ap->a_fcnp;
-	struct udf_node *fnode, *fdnode, *tnode, *tdnode;
-	struct vattr fvap, tvap;
-	int error;
-
-	DPRINTF(CALL, ("udf_rename called\n"));
-
-	/* disallow cross-device renames */
-	if (fvp->v_mount != tdvp->v_mount ||
-	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
-		error = EXDEV;
-		goto out_unlocked;
-	}
-
-	fnode  = VTOI(fvp);
-	fdnode = VTOI(fdvp);
-	tnode  = (tvp == NULL) ? NULL : VTOI(tvp);
-	tdnode = VTOI(tdvp);
-
-	/* lock our source dir */
-	if (fdnode != tdnode) {
-		error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
-		if (error != 0)
-			goto out_unlocked;
-	}
-
-	/* get info about the node to be moved */
-	vn_lock(fvp, LK_SHARED | LK_RETRY);
-	error = VOP_GETATTR(fvp, &fvap, FSCRED);
-	VOP_UNLOCK(fvp);
-	KASSERT(error == 0);
-
-	/* check when to delete the old already existing entry */
-	if (tvp) {
-		/* get info about the node to be moved to */
-		error = VOP_GETATTR(tvp, &tvap, FSCRED);
-		KASSERT(error == 0);
-
-		/* if both dirs, make sure the destination is empty */
-		if (fvp->v_type == VDIR && tvp->v_type == VDIR) {
-			if (tvap.va_nlink > 2) {
-				error = ENOTEMPTY;
-				goto out;
-			}
-		}
-		/* if moving dir, make sure destination is dir too */
-		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
-			error = ENOTDIR;
-			goto out;
-		}
-		/* if we're moving a non-directory, make sure dest is no dir */
-		if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
-			error = EISDIR;
-			goto out;
-		}
-	}
-
-	/* check if moving a directory to a new parent is allowed */
-	if ((fdnode != tdnode) && (fvp->v_type == VDIR)) {
-		/* release tvp since we might encounter it and lock up */
-		if (tvp)
-			vput(tvp);
-
-		/* vref tdvp since we lose its ref in udf_on_rootpath */
-		vref(tdvp);
-
-		/* search if fnode is a component of tdnode's path to root */
-		error = udf_on_rootpath(fnode, tdnode);
-
-		DPRINTF(NODE, ("Dir rename allowed ? %s\n", error ? "NO":"YES"));
-
-		if (error) {
-			/* compensate for our vref earlier */
-			vrele(tdvp);
-			goto out;
-		}
-
-		/* relock tdvp; its still here due to the vref earlier */
-		vn_lock(tdvp, LK_EXCLUSIVE | LK_RETRY);
-
-		/*
-		 * re-lookup tvp since the parent has been unlocked, so could
-		 * have changed/removed in the meantime.
-		 */
-		error = relookup(tdvp, &tvp, tcnp, 0);
-		if (error) {
-			vput(tdvp);
-			goto out;
-		}
-		tnode  = (tvp == NULL) ? NULL : VTOI(tvp);
-	}
-
-	/* remove existing entry if present */
-	if (tvp) 
-		udf_dir_detach(tdnode->ump, tdnode, tnode, tcnp);
-
-	/* create new directory entry for the node */
-	error = udf_dir_attach(tdnode->ump, tdnode, fnode, &fvap, tcnp);
-	if (error)
-		goto out;
-
-	/* unlink old directory entry for the node, if failing, unattach new */
-	error = udf_dir_detach(tdnode->ump, fdnode, fnode, fcnp);
-	if (error)
-		udf_dir_detach(tdnode->ump, tdnode, fnode, tcnp);
-	if (error)
-		goto out;
-
-	/* update tnode's '..' if moving directory to new parent */
-	if ((fdnode != tdnode) && (fvp->v_type == VDIR)) {
-		/* update fnode's '..' entry */
-		error = udf_dir_update_rootentry(fnode->ump, fnode, tdnode);
-		if (error) {
-			/* 'try' to recover from this situation */
-			udf_dir_attach(tdnode->ump, fdnode, fnode, &fvap, fcnp);
-			udf_dir_detach(tdnode->ump, tdnode, fnode, tcnp);
-		}
-	}
-
-out:
-        if (fdnode != tdnode)
-                VOP_UNLOCK(fdvp);
-
-out_unlocked:
-	VOP_ABORTOP(tdvp, tcnp);
-	if (tdvp == tvp)
-		vrele(tdvp);
-	else
-		vput(tdvp);
-	if (tvp)
-		vput(tvp);
-	VOP_ABORTOP(fdvp, fcnp);
-
-	/* release source nodes. */
-	vrele(fdvp);
-	vrele(fvp);
-
-	return error;
-}
-
 /* --------------------------------------------------------------------- */
 
 int

Added files:

Index: src/sys/fs/udf/udf_rename.c
diff -u /dev/null src/sys/fs/udf/udf_rename.c:1.1
--- /dev/null	Wed Jul 10 15:10:56 2013
+++ src/sys/fs/udf/udf_rename.c	Wed Jul 10 15:10:56 2013
@@ -0,0 +1,671 @@
+/* $NetBSD: udf_rename.c,v 1.1 2013/07/10 15:10:56 reinoud Exp $ */
+
+/*
+ * Copyright (c) 2013 Reinoud Zandijk
+ * All rights reserved.
+ * 
+ * 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 AUTHOR ``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 AUTHOR 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.
+ * 
+ * Comments and trivial code from the reference implementation in tmpfs.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: udf_rename.c,v 1.1 2013/07/10 15:10:56 reinoud Exp $");
+
+#include <sys/param.h>
+#include <sys/errno.h>
+#include <sys/kauth.h>
+#include <sys/mount.h>
+#include <sys/namei.h>
+#include <sys/stat.h>
+#include <sys/malloc.h>
+#include <sys/dirent.h>
+#include <sys/vnode.h>
+#include <sys/vnode_if.h>
+
+#include <miscfs/genfs/genfs.h>
+
+#include <fs/udf/ecma167-udf.h>
+#include <fs/udf/udf_mount.h>
+#include <sys/dirhash.h>
+
+#include "udf.h"
+#include "udf_subr.h"
+#include "udf_bswap.h"
+
+
+/* forwards */
+static int udf_sane_rename( struct vnode *, struct componentname *,
+    struct vnode *, struct componentname *,
+    kauth_cred_t, bool);
+static bool udf_rmdired_p(struct vnode *);
+static int udf_gro_lock_directory(struct mount *, struct vnode *);
+
+static const struct genfs_rename_ops udf_genfs_rename_ops;
+
+
+#define VTOI(vnode) ((struct udf_node *) (vnode)->v_data)
+
+
+/*
+ * udf_sane_rename: The hairiest vop, with the saner API.
+ *
+ * Arguments:
+ *
+ * . fdvp (from directory vnode),
+ * . fcnp (from component name),
+ * . tdvp (to directory vnode),
+ * . tcnp (to component name),
+ * . cred (credentials structure), and
+ * . posixly_correct (flag for behaviour if target & source link same file).
+ *
+ * fdvp and tdvp may be the same, and must be referenced and unlocked.
+ */
+static int
+udf_sane_rename( struct vnode *fdvp, struct componentname *fcnp,
+    struct vnode *tdvp, struct componentname *tcnp,
+    kauth_cred_t cred, bool posixly_correct)
+{
+	struct dirent fdirent, tdirent;	/* XXX OK? XXX */
+
+	DPRINTF(CALL, ("udf_sane_rename '%s' -> '%s'\n",
+		fcnp->cn_nameptr, tcnp->cn_nameptr));
+	return genfs_sane_rename(&udf_genfs_rename_ops,
+	    fdvp, fcnp, &fdirent, tdvp, tcnp, &tdirent,
+	    cred, posixly_correct);
+}
+
+
+/*
+ * udf_rename: the hairiest vop, with the insanest API. Pass to
+ * genfs_insane_rename immediately.
+ */
+int
+udf_rename(void *v)
+{
+	struct vop_rename_args /* {
+		struct vnode *a_fdvp;
+		struct vnode *a_fvp;
+		struct componentname *a_fcnp;
+		struct vnode *a_tdvp;
+		struct vnode *a_tvp;
+		struct componentname *a_tcnp;
+	} */ *ap = v;
+	DPRINTF(CALL, ("udf_rename called\n"));
+	return genfs_insane_rename(ap, &udf_sane_rename);
+}
+
+
+/*
+ * udf_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
+udf_gro_directory_empty_p(struct mount *mp, kauth_cred_t cred,
+    struct vnode *vp, struct vnode *dvp)
+{
+	struct udf_node *udf_node = VTOI(vp);
+	int error, isempty;
+
+	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);
+
+	DPRINTF(CALL, ("udf_gro_directory_empty_p called\n"));
+	/* make sure our `leaf' node's hash is populated */
+	dirhash_get(&udf_node->dir_hash);
+	error = udf_dirhash_fill(udf_node);
+	if (error) {
+		dirhash_put(udf_node->dir_hash);
+		return error;
+	}
+
+	/* check to see if the directory is empty */
+	isempty = dirhash_dir_isempty(udf_node->dir_hash);
+	dirhash_put(udf_node->dir_hash);
+
+	return isempty;
+}
+
+
+/*
+ * udf_gro_rename_check_possible: check whether a rename is possible
+ * independent of credentials.
+ */
+static int
+udf_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));
+
+	DPRINTF(CALL, ("udf_gro_rename_check_possible called\n"));
+	if (mp->mnt_flag & MNT_RDONLY)
+		return EROFS;
+
+	/* flags not implemented since they are not defined (yet) in UDF */
+	return 0;
+}
+
+
+/*
+ * udf_gro_rename_check_permitted: check whether a rename is permitted given
+ * our credentials.
+ */
+static int
+udf_gro_rename_check_permitted(struct mount *mp, kauth_cred_t cred,
+    struct vnode *fdvp, struct vnode *fvp,
+    struct vnode *tdvp, struct vnode *tvp)
+{
+	struct udf_node *fdir_node = VTOI(fdvp);
+	struct udf_node *tdir_node = VTOI(tdvp);
+	struct udf_node *f_node = VTOI(fvp);
+	struct udf_node *t_node = (tvp? VTOI(tvp): NULL);
+	mode_t fdmode, tdmode;
+	uid_t fduid, tduid, fuid, tuid;
+	gid_t gdummy;
+
+	(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));
+
+	DPRINTF(CALL, ("udf_gro_rename_check_permitted called\n"));
+	fdmode = udf_getaccessmode(fdir_node);
+	tdmode = udf_getaccessmode(tdir_node);
+
+	udf_getownership(fdir_node, &fduid, &gdummy);
+	udf_getownership(tdir_node, &tduid, &gdummy);
+	udf_getownership(f_node,    &fuid, &gdummy);
+
+	tuid = 0;
+	if (t_node)
+		udf_getownership(t_node, &tuid, &gdummy);
+
+	return genfs_ufslike_rename_check_permitted(cred,
+	    fdvp, fdmode, fduid,
+	    fvp,  fuid,
+	    tdvp, tdmode, tduid,
+	    tvp,  tuid);
+}
+
+
+/*
+ * udf_gro_remove_check_possible: check whether a remove is possible
+ * independent of credentials.
+ *
+ * XXX could check for special attributes?
+ */
+static int
+udf_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);
+
+	DPRINTF(CALL, ("udf_gro_remove_check_possible called\n"));
+	if (mp->mnt_flag & MNT_RDONLY)
+		return EROFS;
+
+	/* flags not implemented since they are not defined (yet) in UDF */
+	return 0;
+}
+
+
+/*
+ * udf_gro_remove_check_permitted: check whether a remove is permitted given
+ * our credentials.
+ */
+static int
+udf_gro_remove_check_permitted(struct mount *mp, kauth_cred_t cred,
+    struct vnode *dvp, struct vnode *vp)
+{
+	struct udf_node *dir_node = VTOI(dvp);
+	struct udf_node *udf_node = VTOI(vp);
+	mode_t dmode;
+	uid_t duid, uid;
+	gid_t gdummy;
+
+	(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);
+
+	DPRINTF(CALL, ("udf_gro_remove_check_permitted called\n"));
+	dmode = udf_getaccessmode(dir_node);
+
+	udf_getownership(dir_node, &duid, &gdummy);
+	udf_getownership(udf_node, &uid,  &gdummy);
+
+	return genfs_ufslike_remove_check_permitted(cred,
+	    dvp, dmode, duid,
+	    vp, uid);
+}
+
+
+/*
+ * udf_gro_rename: actually perform the rename operation.
+ */
+static int
+udf_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)
+{
+	struct dirent **fdirentp = fde;
+	struct dirent **tdirentp = tde;
+	struct udf_node *fnode, *fdnode, *tnode, *tdnode;
+	struct vattr fvap;
+	int error;
+
+	(void)cred;
+	KASSERT(mp != NULL);
+	KASSERT(fdvp != NULL);
+	KASSERT(fcnp != NULL);
+	KASSERT(fvp != NULL);
+	KASSERT(tdvp != NULL);
+	KASSERT(tcnp != NULL);
+	KASSERT(fdirentp != NULL);
+	KASSERT(tdirentp != NULL);
+	KASSERT(fdirentp != tdirentp);
+	KASSERT((*fdirentp) != (*tdirentp));
+	KASSERT((*fdirentp) != 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));
+
+	DPRINTF(CALL, ("udf_gro_rename called\n"));
+	DPRINTF(NODE, ("udf_gro_rename called : %s -> %s\n",
+		fcnp->cn_nameptr, tcnp->cn_nameptr));
+
+	fnode  = VTOI(fvp);
+	fdnode = VTOI(fdvp);
+	tnode  = (tvp == NULL) ? NULL : VTOI(tvp);
+	tdnode = VTOI(tdvp);
+
+	/* get attribute information */
+	error = VOP_GETATTR(fvp, &fvap, NULL);
+	if (error)
+		return error;
+
+	/* remove existing entry if present */
+	if (tvp) 
+		udf_dir_detach(tdnode->ump, tdnode, tnode, tcnp);
+
+	/* create new directory entry for the node */
+	error = udf_dir_attach(tdnode->ump, tdnode, fnode, &fvap, tcnp);
+	if (error)
+		return error;
+
+	/* unlink old directory entry for the node, if failing, unattach new */
+	error = udf_dir_detach(tdnode->ump, fdnode, fnode, fcnp);
+	if (error) {
+		udf_dir_detach(tdnode->ump, tdnode, fnode, tcnp);
+	} else if ((fdnode != tdnode) && (fvp->v_type == VDIR)) {
+		/* update fnode's '..' entry */
+		error = udf_dir_update_rootentry(fnode->ump, fnode, tdnode);
+		if (error) {
+			/* 'try' to recover from this situation */
+			udf_dir_attach(tdnode->ump, fdnode, fnode, &fvap, fcnp);
+			udf_dir_detach(tdnode->ump, tdnode, fnode, tcnp);
+		}
+	}
+
+	if (!error) {
+		VN_KNOTE(fvp, NOTE_RENAME);
+		genfs_rename_cache_purge(fdvp, fvp, tdvp, tvp);
+	}
+
+	return error;
+}
+
+
+/*
+ * udf_gro_remove: rename an object over another link to itself, effectively
+ * removing just the original link.
+ */
+static int
+udf_gro_remove(struct mount *mp, kauth_cred_t cred,
+    struct vnode *dvp, struct componentname *cnp, void *de, struct vnode *vp)
+{
+	struct udf_node *dir_node, *udf_node;
+
+	(void)vp;
+	KASSERT(mp != NULL);
+	KASSERT(dvp != NULL);
+	KASSERT(cnp != 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);
+
+	DPRINTF(CALL, ("udf_gro_remove called\n"));
+
+	dir_node = VTOI(dvp);
+	udf_node = VTOI(vp);
+	udf_dir_detach(dir_node->ump, dir_node, udf_node, cnp);
+
+	return 0;
+}
+
+
+/*
+ * udf_gro_lookup: look up and save the lookup results.
+ */
+static int
+udf_gro_lookup(struct mount *mp, struct vnode *dvp,
+    struct componentname *cnp, void *de_ret, struct vnode **vp_ret)
+{
+	struct udf_node *dir_node, *res_node;
+	struct long_ad   icb_loc;
+	const char *name;
+	int namelen, error;
+	int found;
+
+	(void)mp;
+	KASSERT(mp != NULL);
+	KASSERT(dvp != NULL);
+	KASSERT(cnp != NULL);
+	KASSERT(vp_ret != NULL);
+	KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
+
+	dir_node = VTOI(dvp);
+
+	DPRINTF(CALL, ("udf_gro_lookup called\n"));
+
+	/* lookup filename in the directory; location icb_loc */
+	name    = cnp->cn_nameptr;
+	namelen = cnp->cn_namelen;
+	error = udf_lookup_name_in_dir(dvp, name, namelen,
+			&icb_loc, &found);
+	if (error)
+		return error;
+	if (!found)
+		return ENOENT;
+
+	DPRINTF(LOOKUP, ("udf_gro_lookup found '%s'\n", name));
+	error = udf_get_node(dir_node->ump, &icb_loc, &res_node);
+	if (error)
+		return error;
+	*vp_ret = res_node->vnode;
+	VOP_UNLOCK(res_node->vnode);
+
+	return 0;
+}
+
+
+/*
+ * udf_rmdired_p: check whether the directory vp has been rmdired.
+ *
+ * vp must be locked and referenced.
+ */
+static bool
+udf_rmdired_p(struct vnode *vp)
+{
+	struct long_ad icb_loc;
+	const char *name;
+	int namelen;
+	int error, found;
+
+	DPRINTF(CALL, ("udf_rmdired_p called\n"));
+
+	KASSERT(vp != NULL);
+	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
+	KASSERT(vp->v_type == VDIR);
+
+	/* get our node */
+	name    = "..";
+	namelen = 2;
+	error = udf_lookup_name_in_dir(vp, name, namelen,
+			&icb_loc, &found);
+	return (error || !found);
+}
+
+
+/*
+ * udf_gro_genealogy: analyze the genealogy of the source and target
+ * directories.
+ */
+static int
+udf_gro_genealogy(struct mount *mp, kauth_cred_t cred,
+    struct vnode *fdvp, struct vnode *tdvp,
+    struct vnode **intermediate_node_ret)
+{
+	struct udf_mount *ump;
+	struct udf_node *source, *target, *parent_node, *child_node;
+	struct long_ad parent_loc;
+	const char *name;
+	int namelen;
+	int error, found;
+
+	(void)cred;
+	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);
+
+	DPRINTF(CALL, ("udf_gro_genealogy called\n"));
+
+	/*
+	 * We need to provisionally lock tdvp to keep rmdir from deleting it
+	 * -- or any ancestor -- at an inopportune moment.
+	 *
+	 * XXX WHY is this not in genfs's rename? XXX
+	 */
+	error = udf_gro_lock_directory(mp, tdvp);
+	if (error)
+		return error;
+
+	source = VTOI(fdvp);
+	target = VTOI(tdvp);
+
+	name     = "..";
+	namelen  = 2;
+	error    = 0;
+
+	ump = target->ump;
+
+	/* if nodes are equal, it is no use looking */
+	if (udf_compare_icb(&source->loc, &target->loc) == 0)
+		return EEXIST;
+
+	child_node = target;
+	vref(child_node->vnode);
+
+	for (;;) {
+		DPRINTF(NODE, ("udf_gro_genealogy : "
+			"source vp %p, looking at vp %p\n",
+			source->vnode, child_node->vnode));
+
+		/* sanity check */
+		if (child_node->vnode->v_type != VDIR) {
+			vput(child_node->vnode);
+			return ENOTDIR;
+		}
+
+		/* go down one level */
+		error = udf_lookup_name_in_dir(child_node->vnode, name, namelen,
+			&parent_loc, &found);
+		DPRINTF(NODE, ("\tlookup of parent '..' resulted in error %d, "
+			"found %d\n", error, found));
+		if (!found)
+			error = ENOENT;
+		if (error) {
+			vput(child_node->vnode);
+			return error;
+		}
+
+		/* did we encounter the root node? i.e. loop back */
+		if (udf_compare_icb(&parent_loc, &child_node->loc) == 0) {
+			DPRINTF(NODE, ("ROOT found!\n"));
+			vput(child_node->vnode);
+			*intermediate_node_ret = NULL;
+			return 0;
+		}
+
+		/* did we encounter source node? */
+		if (udf_compare_icb(&parent_loc, &source->loc) == 0) {
+			DPRINTF(NODE, ("SOURCE NODE FOUND\n"));
+			*intermediate_node_ret = child_node->vnode;
+			VOP_UNLOCK(child_node->vnode);
+			return 0;
+		}
+
+		DPRINTF(NODE, ("\tgetting the parent node\n"));
+		error = udf_get_node(ump, &parent_loc, &parent_node);
+		if (error) {
+			vput(child_node->vnode);
+			return error;
+		}
+
+		if (udf_rmdired_p(parent_node->vnode)) {
+			vput(child_node->vnode);
+			vput(parent_node->vnode);
+			return ENOENT;
+		}
+
+		/*
+		 * Push our intermediate node, we're done with it, but do
+		 * remember our parent location
+		 */
+		vput(child_node->vnode);
+		child_node = parent_node;
+	}
+}
+
+
+/*
+ * udf_gro_lock_directory: lock the directory vp, but fail if it has been
+ * rmdir'd.
+ */
+static int
+udf_gro_lock_directory(struct mount *mp, struct vnode *vp)
+{
+
+	(void)mp;
+	KASSERT(mp != NULL);
+	KASSERT(vp != NULL);
+	KASSERT(vp->v_mount == mp);
+
+	DPRINTF(CALL, ("udf_gro_lock_directory called\n"));
+	DPRINTF(LOCKING, ("udf_gro_lock_directory called\n"));
+
+	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
+
+	if (udf_rmdired_p(vp)) {
+		VOP_UNLOCK(vp);
+		return ENOENT;
+	}
+
+	return 0;
+}
+
+
+static const struct genfs_rename_ops udf_genfs_rename_ops = {
+	.gro_directory_empty_p		= udf_gro_directory_empty_p,
+	.gro_rename_check_possible	= udf_gro_rename_check_possible,
+	.gro_rename_check_permitted	= udf_gro_rename_check_permitted,
+	.gro_remove_check_possible	= udf_gro_remove_check_possible,
+	.gro_remove_check_permitted	= udf_gro_remove_check_permitted,
+	.gro_rename			= udf_gro_rename,
+	.gro_remove			= udf_gro_remove,
+	.gro_lookup			= udf_gro_lookup,
+	.gro_genealogy			= udf_gro_genealogy,
+	.gro_lock_directory		= udf_gro_lock_directory,
+};

Reply via email to