+ /*
+ * 3) Unlink the source.
+ */
+ fndp->ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF;
+ (void)namei(fndp);
+ if (fndp->ni_vp != NULL) {
+ xp = VTOI(fndp->ni_vp);
+ dp = VTOI(fndp->ni_dvp);
+ } else {
+ if (fndp->ni_dvp != NULL)
+ vput(fndp->ni_dvp);
+ xp = NULL;
+ dp = NULL;
+ }
+ /*
+ * Ensure that the directory entry still exists and has not
+ * changed while the new name has been entered. If the source is
+ * a file then the entry may have been unlinked or renamed. In
+ * either case there is no further work to be done. If the source
+ * is a directory then it cannot have been rmdir'ed; its link
+ * count of three would cause a rmdir to fail with ENOTEMPTY.
+ * The IRENAME flag ensures that it cannot be moved by another
+ * rename.
+ */
+ if (xp != ip) {
+ if (doingdirectory)
+ panic("rename: lost dir entry");
+ } else {
+ /*
+ * If the source is a directory with a
+ * new parent, the link count of the old
+ * parent directory must be decremented
+ * and ".." set to point to the new parent.
+ */
+ if (doingdirectory && newparent) {
+ dp->i_nlink--;
+ dp->i_flag |= ICHG;
+ error = rdwri(UIO_READ, xp, (caddr_t)&dirbuf,
+ sizeof (struct dirtemplate), (off_t)0,
+ UIO_SYSSPACE, tndp->ni_cred, (int *)0);
+ if (error == 0) {
+ if (dirbuf.dotdot_namlen != 2 ||
+ dirbuf.dotdot_name[0] != '.' ||
+ dirbuf.dotdot_name[1] != '.') {
+ printf("rename: mangled dir\n");
+ } else {
+ dirbuf.dotdot_ino = newparent;
+ (void) rdwri(UIO_WRITE, xp,
+ (caddr_t)&dirbuf,
+ sizeof (struct dirtemplate),
+ (off_t)0, UIO_SYSSPACE,
+ tndp->ni_cred, (int *)0);
+ cache_purge(ITOV(dp));
+ }
+ }
+ }
+ error = dirremove(fndp);
+ if (!error) {
+ xp->i_nlink--;
+ xp->i_flag |= ICHG;
+ }
+ xp->i_flag &= ~IRENAME;
+ }
+ if (dp)
+ vput(ITOV(dp));
+ if (xp)
+ vput(ITOV(xp));
+ vrele(ITOV(ip));
+ return (error);
+
+bad:
+ if (xp)
+ vput(ITOV(xp));
+ vput(ITOV(dp));