xref: /original-bsd/sys/ufs/ffs/ufs_vnops.c (revision cf2124ff)
1 /*
2  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)ufs_vnops.c	7.112 (Berkeley) 12/09/92
8  */
9 
10 #include <sys/param.h>
11 #include <sys/systm.h>
12 #include <sys/namei.h>
13 #include <sys/resourcevar.h>
14 #include <sys/kernel.h>
15 #include <sys/file.h>
16 #include <sys/stat.h>
17 #include <sys/buf.h>
18 #include <sys/proc.h>
19 #include <sys/conf.h>
20 #include <sys/mount.h>
21 #include <sys/vnode.h>
22 #include <sys/malloc.h>
23 #include <sys/dirent.h>
24 
25 #include <vm/vm.h>
26 
27 #include <miscfs/specfs/specdev.h>
28 
29 #include <ufs/ufs/lockf.h>
30 #include <ufs/ufs/quota.h>
31 #include <ufs/ufs/inode.h>
32 #include <ufs/ufs/dir.h>
33 #include <ufs/ufs/ufsmount.h>
34 #include <ufs/ufs/ufs_extern.h>
35 
36 static int ufs_chmod __P((struct vnode *, int, struct ucred *, struct proc *));
37 static int ufs_chown
38 	__P((struct vnode *, uid_t, gid_t, struct ucred *, struct proc *));
39 
40 union _qcvt {
41 	quad_t qcvt;
42 	long val[2];
43 };
44 #define SETHIGH(q, h) { \
45 	union _qcvt tmp; \
46 	tmp.qcvt = (q); \
47 	tmp.val[_QUAD_HIGHWORD] = (h); \
48 	(q) = tmp.qcvt; \
49 }
50 #define SETLOW(q, l) { \
51 	union _qcvt tmp; \
52 	tmp.qcvt = (q); \
53 	tmp.val[_QUAD_LOWWORD] = (l); \
54 	(q) = tmp.qcvt; \
55 }
56 
57 /*
58  * Create a regular file
59  */
60 int
61 ufs_create(ap)
62 	struct vop_create_args /* {
63 		struct vnode *a_dvp;
64 		struct vnode **a_vpp;
65 		struct componentname *a_cnp;
66 		struct vattr *a_vap;
67 	} */ *ap;
68 {
69 	int error;
70 
71 	if (error =
72 	    ufs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
73 	    ap->a_dvp, ap->a_vpp, ap->a_cnp))
74 		return (error);
75 	return (0);
76 }
77 
78 /*
79  * Mknod vnode call
80  */
81 /* ARGSUSED */
82 int
83 ufs_mknod(ap)
84 	struct vop_mknod_args /* {
85 		struct vnode *a_dvp;
86 		struct vnode **a_vpp;
87 		struct componentname *a_cnp;
88 		struct vattr *a_vap;
89 	} */ *ap;
90 {
91 	register struct vattr *vap = ap->a_vap;
92 	register struct vnode **vpp = ap->a_vpp;
93 	register struct inode *ip;
94 	int error;
95 
96 	if (error =
97 	    ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
98 	    ap->a_dvp, vpp, ap->a_cnp))
99 		return (error);
100 	ip = VTOI(*vpp);
101 	ip->i_flag |= IACC|IUPD|ICHG;
102 	if (vap->va_rdev != VNOVAL) {
103 		/*
104 		 * Want to be able to use this to make badblock
105 		 * inodes, so don't truncate the dev number.
106 		 */
107 		ip->i_rdev = vap->va_rdev;
108 	}
109 	/*
110 	 * Remove inode so that it will be reloaded by iget and
111 	 * checked to see if it is an alias of an existing entry
112 	 * in the inode cache.
113 	 */
114 	vput(*vpp);
115 	(*vpp)->v_type = VNON;
116 	vgone(*vpp);
117 	*vpp = 0;
118 	return (0);
119 }
120 
121 /*
122  * Open called.
123  *
124  * Nothing to do.
125  */
126 /* ARGSUSED */
127 int
128 ufs_open(ap)
129 	struct vop_open_args /* {
130 		struct vnode *a_vp;
131 		int  a_mode;
132 		struct ucred *a_cred;
133 		struct proc *a_p;
134 	} */ *ap;
135 {
136 
137 	return (0);
138 }
139 
140 /*
141  * Close called
142  *
143  * Update the times on the inode.
144  */
145 /* ARGSUSED */
146 int
147 ufs_close(ap)
148 	struct vop_close_args /* {
149 		struct vnode *a_vp;
150 		int  a_fflag;
151 		struct ucred *a_cred;
152 		struct proc *a_p;
153 	} */ *ap;
154 {
155 	register struct vnode *vp = ap->a_vp;
156 	register struct inode *ip = VTOI(vp);
157 
158 	if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED))
159 		ITIMES(ip, &time, &time);
160 	return (0);
161 }
162 
163 /*
164  * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
165  * The mode is shifted to select the owner/group/other fields. The
166  * super user is granted all permissions.
167  */
168 int
169 ufs_access(ap)
170 	struct vop_access_args /* {
171 		struct vnode *a_vp;
172 		int  a_mode;
173 		struct ucred *a_cred;
174 		struct proc *a_p;
175 	} */ *ap;
176 {
177 	register struct vnode *vp = ap->a_vp;
178 	register struct inode *ip = VTOI(vp);
179 	register struct ucred *cred = ap->a_cred;
180 	mode_t mode = ap->a_mode;
181 	register gid_t *gp;
182 	int i, error;
183 
184 #ifdef DIAGNOSTIC
185 	if (!VOP_ISLOCKED(vp)) {
186 		vprint("ufs_access: not locked", vp);
187 		panic("ufs_access: not locked");
188 	}
189 #endif
190 #ifdef QUOTA
191 	if (mode & VWRITE) {
192 		switch (vp->v_type) {
193 		case VREG: case VDIR: case VLNK:
194 			if (error = getinoquota(ip))
195 				return (error);
196 		}
197 	}
198 #endif /* QUOTA */
199 	/*
200 	 * If you're the super-user, you always get access.
201 	 */
202 	if (cred->cr_uid == 0)
203 		return (0);
204 	/*
205 	 * Access check is based on only one of owner, group, public.
206 	 * If not owner, then check group. If not a member of the
207 	 * group, then check public access.
208 	 */
209 	if (cred->cr_uid != ip->i_uid) {
210 		mode >>= 3;
211 		gp = cred->cr_groups;
212 		for (i = 0; i < cred->cr_ngroups; i++, gp++)
213 			if (ip->i_gid == *gp)
214 				goto found;
215 		mode >>= 3;
216 found:
217 		;
218 	}
219 	return ((ip->i_mode & mode) == mode ? 0 : EACCES);
220 }
221 
222 /* ARGSUSED */
223 int
224 ufs_getattr(ap)
225 	struct vop_getattr_args /* {
226 		struct vnode *a_vp;
227 		struct vattr *a_vap;
228 		struct ucred *a_cred;
229 		struct proc *a_p;
230 	} */ *ap;
231 {
232 	register struct vnode *vp = ap->a_vp;
233 	register struct inode *ip = VTOI(vp);
234 	register struct vattr *vap = ap->a_vap;
235 
236 	ITIMES(ip, &time, &time);
237 	/*
238 	 * Copy from inode table
239 	 */
240 	vap->va_fsid = ip->i_dev;
241 	vap->va_fileid = ip->i_number;
242 	vap->va_mode = ip->i_mode & ~IFMT;
243 	vap->va_nlink = ip->i_nlink;
244 	vap->va_uid = ip->i_uid;
245 	vap->va_gid = ip->i_gid;
246 	vap->va_rdev = (dev_t)ip->i_rdev;
247 	vap->va_size = ip->i_din.di_size;
248 	vap->va_atime = ip->i_atime;
249 	vap->va_mtime = ip->i_mtime;
250 	vap->va_ctime = ip->i_ctime;
251 	vap->va_flags = ip->i_flags;
252 	vap->va_gen = ip->i_gen;
253 	/* this doesn't belong here */
254 	if (vp->v_type == VBLK)
255 		vap->va_blocksize = BLKDEV_IOSIZE;
256 	else if (vp->v_type == VCHR)
257 		vap->va_blocksize = MAXBSIZE;
258 	else
259 		vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
260 	vap->va_bytes = dbtob(ip->i_blocks);
261 	vap->va_type = vp->v_type;
262 	vap->va_filerev = ip->i_modrev;
263 	return (0);
264 }
265 
266 /*
267  * Set attribute vnode op. called from several syscalls
268  */
269 int
270 ufs_setattr(ap)
271 	struct vop_setattr_args /* {
272 		struct vnode *a_vp;
273 		struct vattr *a_vap;
274 		struct ucred *a_cred;
275 		struct proc *a_p;
276 	} */ *ap;
277 {
278 	register struct vattr *vap = ap->a_vap;
279 	register struct vnode *vp = ap->a_vp;
280 	register struct inode *ip = VTOI(vp);
281 	register struct ucred *cred = ap->a_cred;
282 	register struct proc *p = ap->a_p;
283 	struct timeval atimeval, mtimeval;
284 	int error;
285 
286 	/*
287 	 * Check for unsettable attributes.
288 	 */
289 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
290 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
291 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
292 	    ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
293 		return (EINVAL);
294 	}
295 	/*
296 	 * Go through the fields and update iff not VNOVAL.
297 	 */
298 	if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL)
299 		if (error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred, p))
300 			return (error);
301 	if (vap->va_size != VNOVAL) {
302 		if (vp->v_type == VDIR)
303 			return (EISDIR);
304 		if (error = VOP_TRUNCATE(vp, vap->va_size, 0, cred, p))
305 			return (error);
306 	}
307 	ip = VTOI(vp);
308 	if (vap->va_atime.ts_sec != VNOVAL || vap->va_mtime.ts_sec != VNOVAL) {
309 		if (cred->cr_uid != ip->i_uid &&
310 		    (error = suser(cred, &p->p_acflag)))
311 			return (error);
312 		if (vap->va_atime.ts_sec != VNOVAL)
313 			ip->i_flag |= IACC;
314 		if (vap->va_mtime.ts_sec != VNOVAL)
315 			ip->i_flag |= IUPD | ICHG;
316 		atimeval.tv_sec = vap->va_atime.ts_sec;
317 		atimeval.tv_usec = vap->va_atime.ts_nsec / 1000;
318 		mtimeval.tv_sec = vap->va_mtime.ts_sec;
319 		mtimeval.tv_usec = vap->va_mtime.ts_nsec / 1000;
320 		if (error = VOP_UPDATE(vp, &atimeval, &mtimeval, 1))
321 			return (error);
322 	}
323 	error = 0;
324 	if (vap->va_mode != (mode_t)VNOVAL)
325 		error = ufs_chmod(vp, (int)vap->va_mode, cred, p);
326 	if (vap->va_flags != VNOVAL) {
327 		if (cred->cr_uid != ip->i_uid &&
328 		    (error = suser(cred, &p->p_acflag)))
329 			return (error);
330 		if (cred->cr_uid == 0) {
331 			ip->i_flags = vap->va_flags;
332 		} else {
333 			ip->i_flags &= 0xffff0000;
334 			ip->i_flags |= (vap->va_flags & 0xffff);
335 		}
336 		ip->i_flag |= ICHG;
337 	}
338 	return (error);
339 }
340 
341 /*
342  * Change the mode on a file.
343  * Inode must be locked before calling.
344  */
345 static int
346 ufs_chmod(vp, mode, cred, p)
347 	register struct vnode *vp;
348 	register int mode;
349 	register struct ucred *cred;
350 	struct proc *p;
351 {
352 	register struct inode *ip = VTOI(vp);
353 	int error;
354 
355 	if (cred->cr_uid != ip->i_uid &&
356 	    (error = suser(cred, &p->p_acflag)))
357 		return (error);
358 	if (cred->cr_uid) {
359 		if (vp->v_type != VDIR && (mode & ISVTX))
360 			return (EFTYPE);
361 		if (!groupmember(ip->i_gid, cred) && (mode & ISGID))
362 			return (EPERM);
363 	}
364 	ip->i_mode &= ~07777;
365 	ip->i_mode |= mode & 07777;
366 	ip->i_flag |= ICHG;
367 	if ((vp->v_flag & VTEXT) && (ip->i_mode & ISVTX) == 0)
368 		(void) vnode_pager_uncache(vp);
369 	return (0);
370 }
371 
372 /*
373  * Perform chown operation on inode ip;
374  * inode must be locked prior to call.
375  */
376 static int
377 ufs_chown(vp, uid, gid, cred, p)
378 	register struct vnode *vp;
379 	uid_t uid;
380 	gid_t gid;
381 	struct ucred *cred;
382 	struct proc *p;
383 {
384 	register struct inode *ip = VTOI(vp);
385 	uid_t ouid;
386 	gid_t ogid;
387 	int error = 0;
388 #ifdef QUOTA
389 	register int i;
390 	long change;
391 #endif
392 
393 	if (uid == (uid_t)VNOVAL)
394 		uid = ip->i_uid;
395 	if (gid == (gid_t)VNOVAL)
396 		gid = ip->i_gid;
397 	/*
398 	 * If we don't own the file, are trying to change the owner
399 	 * of the file, or are not a member of the target group,
400 	 * the caller must be superuser or the call fails.
401 	 */
402 	if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
403 	    !groupmember((gid_t)gid, cred)) &&
404 	    (error = suser(cred, &p->p_acflag)))
405 		return (error);
406 	ouid = ip->i_uid;
407 	ogid = ip->i_gid;
408 #ifdef QUOTA
409 	if (error = getinoquota(ip))
410 		return (error);
411 	if (ouid == uid) {
412 		dqrele(vp, ip->i_dquot[USRQUOTA]);
413 		ip->i_dquot[USRQUOTA] = NODQUOT;
414 	}
415 	if (ogid == gid) {
416 		dqrele(vp, ip->i_dquot[GRPQUOTA]);
417 		ip->i_dquot[GRPQUOTA] = NODQUOT;
418 	}
419 	change = ip->i_blocks;
420 	(void) chkdq(ip, -change, cred, CHOWN);
421 	(void) chkiq(ip, -1, cred, CHOWN);
422 	for (i = 0; i < MAXQUOTAS; i++) {
423 		dqrele(vp, ip->i_dquot[i]);
424 		ip->i_dquot[i] = NODQUOT;
425 	}
426 #endif
427 	ip->i_uid = uid;
428 	ip->i_gid = gid;
429 #ifdef QUOTA
430 	if ((error = getinoquota(ip)) == 0) {
431 		if (ouid == uid) {
432 			dqrele(vp, ip->i_dquot[USRQUOTA]);
433 			ip->i_dquot[USRQUOTA] = NODQUOT;
434 		}
435 		if (ogid == gid) {
436 			dqrele(vp, ip->i_dquot[GRPQUOTA]);
437 			ip->i_dquot[GRPQUOTA] = NODQUOT;
438 		}
439 		if ((error = chkdq(ip, change, cred, CHOWN)) == 0) {
440 			if ((error = chkiq(ip, 1, cred, CHOWN)) == 0)
441 				goto good;
442 			else
443 				(void) chkdq(ip, -change, cred, CHOWN|FORCE);
444 		}
445 		for (i = 0; i < MAXQUOTAS; i++) {
446 			dqrele(vp, ip->i_dquot[i]);
447 			ip->i_dquot[i] = NODQUOT;
448 		}
449 	}
450 	ip->i_uid = ouid;
451 	ip->i_gid = ogid;
452 	if (getinoquota(ip) == 0) {
453 		if (ouid == uid) {
454 			dqrele(vp, ip->i_dquot[USRQUOTA]);
455 			ip->i_dquot[USRQUOTA] = NODQUOT;
456 		}
457 		if (ogid == gid) {
458 			dqrele(vp, ip->i_dquot[GRPQUOTA]);
459 			ip->i_dquot[GRPQUOTA] = NODQUOT;
460 		}
461 		(void) chkdq(ip, change, cred, FORCE|CHOWN);
462 		(void) chkiq(ip, 1, cred, FORCE|CHOWN);
463 		(void) getinoquota(ip);
464 	}
465 	return (error);
466 good:
467 	if (getinoquota(ip))
468 		panic("chown: lost quota");
469 #endif /* QUOTA */
470 	if (ouid != uid || ogid != gid)
471 		ip->i_flag |= ICHG;
472 	if (ouid != uid && cred->cr_uid != 0)
473 		ip->i_mode &= ~ISUID;
474 	if (ogid != gid && cred->cr_uid != 0)
475 		ip->i_mode &= ~ISGID;
476 	return (0);
477 }
478 
479 /* ARGSUSED */
480 int
481 ufs_ioctl(ap)
482 	struct vop_ioctl_args /* {
483 		struct vnode *a_vp;
484 		int  a_command;
485 		caddr_t  a_data;
486 		int  a_fflag;
487 		struct ucred *a_cred;
488 		struct proc *a_p;
489 	} */ *ap;
490 {
491 
492 	return (ENOTTY);
493 }
494 
495 /* ARGSUSED */
496 int
497 ufs_select(ap)
498 	struct vop_select_args /* {
499 		struct vnode *a_vp;
500 		int  a_which;
501 		int  a_fflags;
502 		struct ucred *a_cred;
503 		struct proc *a_p;
504 	} */ *ap;
505 {
506 
507 	/*
508 	 * We should really check to see if I/O is possible.
509 	 */
510 	return (1);
511 }
512 
513 /*
514  * Mmap a file
515  *
516  * NB Currently unsupported.
517  */
518 /* ARGSUSED */
519 int
520 ufs_mmap(ap)
521 	struct vop_mmap_args /* {
522 		struct vnode *a_vp;
523 		int  a_fflags;
524 		struct ucred *a_cred;
525 		struct proc *a_p;
526 	} */ *ap;
527 {
528 
529 	return (EINVAL);
530 }
531 
532 /*
533  * Seek on a file
534  *
535  * Nothing to do, so just return.
536  */
537 /* ARGSUSED */
538 int
539 ufs_seek(ap)
540 	struct vop_seek_args /* {
541 		struct vnode *a_vp;
542 		off_t  a_oldoff;
543 		off_t  a_newoff;
544 		struct ucred *a_cred;
545 	} */ *ap;
546 {
547 
548 	return (0);
549 }
550 
551 /*
552  * ufs remove
553  * Hard to avoid races here, especially
554  * in unlinking directories.
555  */
556 int
557 ufs_remove(ap)
558 	struct vop_remove_args /* {
559 		struct vnode *a_dvp;
560 		struct vnode *a_vp;
561 		struct componentname *a_cnp;
562 	} */ *ap;
563 {
564 	register struct inode *ip;
565 	register struct vnode *vp = ap->a_vp;
566 	register struct vnode *dvp = ap->a_dvp;
567 	int error;
568 
569 	ip = VTOI(vp);
570 	error = ufs_dirremove(dvp, ap->a_cnp);
571 	if (!error) {
572 		ip = VTOI(vp);
573 		ip->i_nlink--;
574 		ip->i_flag |= ICHG;
575 	}
576 	if (dvp == vp)
577 		vrele(vp);
578 	else
579 		vput(vp);
580 	vput(dvp);
581 	return (error);
582 }
583 
584 /*
585  * link vnode call
586  */
587 int
588 ufs_link(ap)
589 	struct vop_link_args /* {
590 		struct vnode *a_vp;
591 		struct vnode *a_tdvp;
592 		struct componentname *a_cnp;
593 	} */ *ap;
594 {
595 	register struct vnode *vp = ap->a_vp;
596 	register struct vnode *tdvp = ap->a_tdvp;
597 	register struct componentname *cnp = ap->a_cnp;
598 	register struct inode *ip;
599 	struct timeval tv;
600 	int error;
601 
602 #ifdef DIAGNOSTIC
603 	if ((cnp->cn_flags & HASBUF) == 0)
604 		panic("ufs_link: no name");
605 #endif
606 	if (vp->v_mount != tdvp->v_mount) {
607 		VOP_ABORTOP(vp, cnp);
608 		error = EXDEV;
609 		goto out2;
610 	}
611 	if (vp != tdvp && (error = VOP_LOCK(tdvp))) {
612 		VOP_ABORTOP(vp, cnp);
613 		goto out2;
614 	}
615 	ip = VTOI(tdvp);
616 	if ((nlink_t)ip->i_nlink >= LINK_MAX) {
617 		VOP_ABORTOP(vp, cnp);
618 		error = EMLINK;
619 		goto out1;
620 	}
621 	ip->i_nlink++;
622 	ip->i_flag |= ICHG;
623 	tv = time;
624 	error = VOP_UPDATE(tdvp, &tv, &tv, 1);
625 	if (!error)
626 		error = ufs_direnter(ip, vp, cnp);
627 	if (error) {
628 		ip->i_nlink--;
629 		ip->i_flag |= ICHG;
630 	}
631 	FREE(cnp->cn_pnbuf, M_NAMEI);
632 out1:
633 	if (vp != tdvp)
634 		VOP_UNLOCK(tdvp);
635 out2:
636 	vput(vp);
637 	return (error);
638 }
639 
640 
641 
642 /*
643  * relookup - lookup a path name component
644  *    Used by lookup to re-aquire things.
645  */
646 int
647 relookup(dvp, vpp, cnp)
648 	struct vnode *dvp, **vpp;
649 	struct componentname *cnp;
650 {
651 	register struct vnode *dp = 0;	/* the directory we are searching */
652 	struct vnode *tdp;		/* saved dp */
653 	struct mount *mp;		/* mount table entry */
654 	int docache;			/* == 0 do not cache last component */
655 	int wantparent;			/* 1 => wantparent or lockparent flag */
656 	int rdonly;			/* lookup read-only flag bit */
657 	char *cp;			/* DEBUG: check name ptr/len */
658 	int newhash;			/* DEBUG: check name hash */
659 	int error = 0;
660 
661 	/*
662 	 * Setup: break out flag bits into variables.
663 	 */
664 	wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
665 	docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
666 	if (cnp->cn_nameiop == DELETE ||
667 	    (wantparent && cnp->cn_nameiop != CREATE))
668 		docache = 0;
669 	rdonly = cnp->cn_flags & RDONLY;
670 	cnp->cn_flags &= ~ISSYMLINK;
671 	dp = dvp;
672 	VOP_LOCK(dp);
673 
674 /* dirloop: */
675 	/*
676 	 * Search a new directory.
677 	 *
678 	 * The cn_hash value is for use by vfs_cache.
679 	 * The last component of the filename is left accessible via
680 	 * cnp->cn_nameptr for callers that need the name. Callers needing
681 	 * the name set the SAVENAME flag. When done, they assume
682 	 * responsibility for freeing the pathname buffer.
683 	 */
684 #ifdef NAMEI_DIAGNOSTIC
685 	for (newhash = 0, cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
686 		newhash += (unsigned char)*cp;
687 	if (newhash != cnp->cn_hash)
688 		panic("relookup: bad hash");
689 	if (cnp->cn_namelen != cp - cnp->cn_nameptr)
690 		panic ("relookup: bad len");
691 	if (*cp != 0)
692 		panic("relookup: not last component");
693 	printf("{%s}: ", cnp->cn_nameptr);
694 #endif
695 
696 	/*
697 	 * Check for degenerate name (e.g. / or "")
698 	 * which is a way of talking about a directory,
699 	 * e.g. like "/." or ".".
700 	 */
701 	if (cnp->cn_nameptr[0] == '\0') {
702 		if (cnp->cn_nameiop != LOOKUP || wantparent) {
703 			error = EISDIR;
704 			goto bad;
705 		}
706 		if (dp->v_type != VDIR) {
707 			error = ENOTDIR;
708 			goto bad;
709 		}
710 		if (!(cnp->cn_flags & LOCKLEAF))
711 			VOP_UNLOCK(dp);
712 		*vpp = dp;
713 		if (cnp->cn_flags & SAVESTART)
714 			panic("lookup: SAVESTART");
715 		return (0);
716 	}
717 
718 	if (cnp->cn_flags & ISDOTDOT)
719 		panic ("relookup: lookup on dot-dot");
720 
721 	/*
722 	 * We now have a segment name to search for, and a directory to search.
723 	 */
724 	if (error = VOP_LOOKUP(dp, vpp, cnp)) {
725 #ifdef DIAGNOSTIC
726 		if (*vpp != NULL)
727 			panic("leaf should be empty");
728 #endif
729 		if (error != EJUSTRETURN)
730 			goto bad;
731 		/*
732 		 * If creating and at end of pathname, then can consider
733 		 * allowing file to be created.
734 		 */
735 		if (rdonly || (dvp->v_mount->mnt_flag & MNT_RDONLY)) {
736 			error = EROFS;
737 			goto bad;
738 		}
739 		/* ASSERT(dvp == ndp->ni_startdir) */
740 		if (cnp->cn_flags & SAVESTART)
741 			VREF(dvp);
742 		/*
743 		 * We return with ni_vp NULL to indicate that the entry
744 		 * doesn't currently exist, leaving a pointer to the
745 		 * (possibly locked) directory inode in ndp->ni_dvp.
746 		 */
747 		return (0);
748 	}
749 	dp = *vpp;
750 
751 #ifdef DIAGNOSTIC
752 	/*
753 	 * Check for symbolic link
754 	 */
755 	if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW))
756 		panic ("relookup: symlink found.\n");
757 #endif
758 
759 nextname:
760 	/*
761 	 * Check for read-only file systems.
762 	 */
763 	if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME) {
764 		/*
765 		 * Disallow directory write attempts on read-only
766 		 * file systems.
767 		 */
768 		if (rdonly || (dp->v_mount->mnt_flag & MNT_RDONLY) ||
769 		    (wantparent &&
770 		     (dvp->v_mount->mnt_flag & MNT_RDONLY))) {
771 			error = EROFS;
772 			goto bad2;
773 		}
774 	}
775 	/* ASSERT(dvp == ndp->ni_startdir) */
776 	if (cnp->cn_flags & SAVESTART)
777 		VREF(dvp);
778 
779 	if (!wantparent)
780 		vrele(dvp);
781 	if ((cnp->cn_flags & LOCKLEAF) == 0)
782 		VOP_UNLOCK(dp);
783 	return (0);
784 
785 bad2:
786 	if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))
787 		VOP_UNLOCK(dvp);
788 	vrele(dvp);
789 bad:
790 	vput(dp);
791 	*vpp = NULL;
792 	return (error);
793 }
794 
795 
796 /*
797  * Rename system call.
798  * 	rename("foo", "bar");
799  * is essentially
800  *	unlink("bar");
801  *	link("foo", "bar");
802  *	unlink("foo");
803  * but ``atomically''.  Can't do full commit without saving state in the
804  * inode on disk which isn't feasible at this time.  Best we can do is
805  * always guarantee the target exists.
806  *
807  * Basic algorithm is:
808  *
809  * 1) Bump link count on source while we're linking it to the
810  *    target.  This also ensure the inode won't be deleted out
811  *    from underneath us while we work (it may be truncated by
812  *    a concurrent `trunc' or `open' for creation).
813  * 2) Link source to destination.  If destination already exists,
814  *    delete it first.
815  * 3) Unlink source reference to inode if still around. If a
816  *    directory was moved and the parent of the destination
817  *    is different from the source, patch the ".." entry in the
818  *    directory.
819  */
820 int
821 ufs_rename(ap)
822 	struct vop_rename_args  /* {
823 		struct vnode *a_fdvp;
824 		struct vnode *a_fvp;
825 		struct componentname *a_fcnp;
826 		struct vnode *a_tdvp;
827 		struct vnode *a_tvp;
828 		struct componentname *a_tcnp;
829 	} */ *ap;
830 {
831 	struct vnode *tvp = ap->a_tvp;
832 	register struct vnode *tdvp = ap->a_tdvp;
833 	struct vnode *fvp = ap->a_fvp;
834 	register struct vnode *fdvp = ap->a_fdvp;
835 	register struct componentname *tcnp = ap->a_tcnp;
836 	register struct componentname *fcnp = ap->a_fcnp;
837 	register struct inode *ip, *xp, *dp;
838 	struct dirtemplate dirbuf;
839 	struct timeval tv;
840 	int doingdirectory = 0, oldparent = 0, newparent = 0;
841 	int error = 0;
842 	int fdvpneedsrele = 1, tdvpneedsrele = 1;
843 	u_char namlen;
844 
845 #ifdef DIAGNOSTIC
846 	if ((tcnp->cn_flags & HASBUF) == 0 ||
847 	    (fcnp->cn_flags & HASBUF) == 0)
848 		panic("ufs_rename: no name");
849 #endif
850 	/*
851 	 * Check for cross-device rename.
852 	 */
853 	if ((fvp->v_mount != tdvp->v_mount) ||
854 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
855 		error = EXDEV;
856 abortit:
857 		VOP_ABORTOP(tdvp, tcnp); /* XXX, why not in NFS? */
858 		if (tdvp == tvp)
859 			vrele(tdvp);
860 		else
861 			vput(tdvp);
862 		if (tvp)
863 			vput(tvp);
864 		VOP_ABORTOP(fdvp, fcnp); /* XXX, why not in NFS? */
865 		vrele(fdvp);
866 		vrele(fvp);
867 		return (error);
868 	}
869 
870 	/*
871 	 * Check if just deleting a link name.
872 	 */
873 	if (fvp == tvp) {
874 		if (fvp->v_type == VDIR) {
875 			error = EINVAL;
876 			goto abortit;
877 		}
878 		VOP_ABORTOP(fdvp, fcnp);
879 		vrele(fdvp);
880 		vrele(fvp);
881 		vput(tdvp);
882 		vput(tvp);
883 		tcnp->cn_flags &= ~MODMASK;
884 		tcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
885 		if ((tcnp->cn_flags & SAVESTART) == 0)
886 			panic("ufs_rename: lost from startdir");
887 		tcnp->cn_nameiop = DELETE;
888 		(void) relookup(tdvp, &tvp, tcnp);
889 		return (VOP_REMOVE(tdvp, tvp, tcnp));
890 	}
891 	if (error = VOP_LOCK(fvp))
892 		goto abortit;
893 	dp = VTOI(fdvp);
894 	ip = VTOI(fvp);
895 	if ((ip->i_mode & IFMT) == IFDIR) {
896 		/*
897 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
898 		 */
899 		if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
900 		    dp == ip || (fcnp->cn_flags&ISDOTDOT) ||
901 		    (ip->i_flag & IRENAME)) {
902 			VOP_UNLOCK(fvp);
903 			error = EINVAL;
904 			goto abortit;
905 		}
906 		ip->i_flag |= IRENAME;
907 		oldparent = dp->i_number;
908 		doingdirectory++;
909 	}
910 	vrele(fdvp);
911 
912 	/*
913 	 * When the target exists, both the directory
914 	 * and target vnodes are returned locked.
915 	 */
916 	dp = VTOI(tdvp);
917 	xp = NULL;
918 	if (tvp)
919 		xp = VTOI(tvp);
920 
921 	/*
922 	 * 1) Bump link count while we're moving stuff
923 	 *    around.  If we crash somewhere before
924 	 *    completing our work, the link count
925 	 *    may be wrong, but correctable.
926 	 */
927 	ip->i_nlink++;
928 	ip->i_flag |= ICHG;
929 	tv = time;
930 	if (error = VOP_UPDATE(fvp, &tv, &tv, 1)) {
931 		VOP_UNLOCK(fvp);
932 		goto bad;
933 	}
934 
935 	/*
936 	 * If ".." must be changed (ie the directory gets a new
937 	 * parent) then the source directory must not be in the
938 	 * directory heirarchy above the target, as this would
939 	 * orphan everything below the source directory. Also
940 	 * the user must have write permission in the source so
941 	 * as to be able to change "..". We must repeat the call
942 	 * to namei, as the parent directory is unlocked by the
943 	 * call to checkpath().
944 	 */
945 	error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc);
946 	VOP_UNLOCK(fvp);
947 	if (oldparent != dp->i_number)
948 		newparent = dp->i_number;
949 	if (doingdirectory && newparent) {
950 		if (error)	/* write access check above */
951 			goto bad;
952 		if (xp != NULL)
953 			vput(tvp);
954 		if (error = ufs_checkpath(ip, dp, tcnp->cn_cred))
955 			goto out;
956 		if ((tcnp->cn_flags & SAVESTART) == 0)
957 			panic("ufs_rename: lost to startdir");
958 		if (error = relookup(tdvp, &tvp, tcnp))
959 			goto out;
960 		dp = VTOI(tdvp);
961 		xp = NULL;
962 		if (tvp)
963 			xp = VTOI(tvp);
964 	}
965 	/*
966 	 * 2) If target doesn't exist, link the target
967 	 *    to the source and unlink the source.
968 	 *    Otherwise, rewrite the target directory
969 	 *    entry to reference the source inode and
970 	 *    expunge the original entry's existence.
971 	 */
972 	if (xp == NULL) {
973 		if (dp->i_dev != ip->i_dev)
974 			panic("rename: EXDEV");
975 		/*
976 		 * Account for ".." in new directory.
977 		 * When source and destination have the same
978 		 * parent we don't fool with the link count.
979 		 */
980 		if (doingdirectory && newparent) {
981 			if ((nlink_t)dp->i_nlink >= LINK_MAX) {
982 				error = EMLINK;
983 				goto bad;
984 			}
985 			dp->i_nlink++;
986 			dp->i_flag |= ICHG;
987 			if (error = VOP_UPDATE(tdvp, &tv, &tv, 1))
988 				goto bad;
989 		}
990 		if (error = ufs_direnter(ip, tdvp, tcnp)) {
991 			if (doingdirectory && newparent) {
992 				dp->i_nlink--;
993 				dp->i_flag |= ICHG;
994 				(void)VOP_UPDATE(tdvp, &tv, &tv, 1);
995 			}
996 			goto bad;
997 		}
998 		vput(tdvp);
999 	} else {
1000 		if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
1001 			panic("rename: EXDEV");
1002 		/*
1003 		 * Short circuit rename(foo, foo).
1004 		 */
1005 		if (xp->i_number == ip->i_number)
1006 			panic("rename: same file");
1007 		/*
1008 		 * If the parent directory is "sticky", then the user must
1009 		 * own the parent directory, or the destination of the rename,
1010 		 * otherwise the destination may not be changed (except by
1011 		 * root). This implements append-only directories.
1012 		 */
1013 		if ((dp->i_mode & ISVTX) && tcnp->cn_cred->cr_uid != 0 &&
1014 		    tcnp->cn_cred->cr_uid != dp->i_uid &&
1015 		    xp->i_uid != tcnp->cn_cred->cr_uid) {
1016 			error = EPERM;
1017 			goto bad;
1018 		}
1019 		/*
1020 		 * Target must be empty if a directory and have no links
1021 		 * to it. Also, ensure source and target are compatible
1022 		 * (both directories, or both not directories).
1023 		 */
1024 		if ((xp->i_mode&IFMT) == IFDIR) {
1025 			if (!ufs_dirempty(xp, dp->i_number, tcnp->cn_cred) ||
1026 			    xp->i_nlink > 2) {
1027 				error = ENOTEMPTY;
1028 				goto bad;
1029 			}
1030 			if (!doingdirectory) {
1031 				error = ENOTDIR;
1032 				goto bad;
1033 			}
1034 			cache_purge(tdvp);
1035 		} else if (doingdirectory) {
1036 			error = EISDIR;
1037 			goto bad;
1038 		}
1039 		if (error = ufs_dirrewrite(dp, ip, tcnp))
1040 			goto bad;
1041 		/*
1042 		 * If the target directory is in the same
1043 		 * directory as the source directory,
1044 		 * decrement the link count on the parent
1045 		 * of the target directory.
1046 		 */
1047 		 if (doingdirectory && !newparent) {
1048 			dp->i_nlink--;
1049 			dp->i_flag |= ICHG;
1050 		}
1051 		vput(tdvp);
1052 		/*
1053 		 * Adjust the link count of the target to
1054 		 * reflect the dirrewrite above.  If this is
1055 		 * a directory it is empty and there are
1056 		 * no links to it, so we can squash the inode and
1057 		 * any space associated with it.  We disallowed
1058 		 * renaming over top of a directory with links to
1059 		 * it above, as the remaining link would point to
1060 		 * a directory without "." or ".." entries.
1061 		 */
1062 		xp->i_nlink--;
1063 		if (doingdirectory) {
1064 			if (--xp->i_nlink != 0)
1065 				panic("rename: linked directory");
1066 			error = VOP_TRUNCATE(tvp, (off_t)0, IO_SYNC,
1067 			    tcnp->cn_cred, tcnp->cn_proc);
1068 		}
1069 		xp->i_flag |= ICHG;
1070 		vput(tvp);
1071 		xp = NULL;
1072 	}
1073 
1074 	/*
1075 	 * 3) Unlink the source.
1076 	 */
1077 	fcnp->cn_flags &= ~MODMASK;
1078 	fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1079 	if ((fcnp->cn_flags & SAVESTART) == 0)
1080 		panic("ufs_rename: lost from startdir");
1081 	(void) relookup(fdvp, &fvp, fcnp);
1082 	if (fvp != NULL) {
1083 		xp = VTOI(fvp);
1084 		dp = VTOI(fdvp);
1085 	} else {
1086 		/*
1087 		 * From name has disappeared.
1088 		 */
1089 		if (doingdirectory)
1090 			panic("rename: lost dir entry");
1091 		vrele(ap->a_fvp);
1092 		return (0);
1093 	}
1094 	/*
1095 	 * Ensure that the directory entry still exists and has not
1096 	 * changed while the new name has been entered. If the source is
1097 	 * a file then the entry may have been unlinked or renamed. In
1098 	 * either case there is no further work to be done. If the source
1099 	 * is a directory then it cannot have been rmdir'ed; its link
1100 	 * count of three would cause a rmdir to fail with ENOTEMPTY.
1101 	 * The IRENAME flag ensures that it cannot be moved by another
1102 	 * rename.
1103 	 */
1104 	if (xp != ip) {
1105 		if (doingdirectory)
1106 			panic("rename: lost dir entry");
1107 	} else {
1108 		/*
1109 		 * If the source is a directory with a
1110 		 * new parent, the link count of the old
1111 		 * parent directory must be decremented
1112 		 * and ".." set to point to the new parent.
1113 		 */
1114 		if (doingdirectory && newparent) {
1115 			dp->i_nlink--;
1116 			dp->i_flag |= ICHG;
1117 			error = vn_rdwr(UIO_READ, fvp, (caddr_t)&dirbuf,
1118 				sizeof (struct dirtemplate), (off_t)0,
1119 				UIO_SYSSPACE, IO_NODELOCKED,
1120 				tcnp->cn_cred, (int *)0, (struct proc *)0);
1121 			if (error == 0) {
1122 #				if (BYTE_ORDER == LITTLE_ENDIAN)
1123 					if (fvp->v_mount->mnt_maxsymlinklen <= 0)
1124 						namlen = dirbuf.dotdot_type;
1125 					else
1126 						namlen = dirbuf.dotdot_namlen;
1127 #				else
1128 					namlen = dirbuf.dotdot_namlen;
1129 #				endif
1130 				if (namlen != 2 ||
1131 				    dirbuf.dotdot_name[0] != '.' ||
1132 				    dirbuf.dotdot_name[1] != '.') {
1133 					ufs_dirbad(xp, (doff_t)12,
1134 					    "rename: mangled dir");
1135 				} else {
1136 					dirbuf.dotdot_ino = newparent;
1137 					(void) vn_rdwr(UIO_WRITE, fvp,
1138 					    (caddr_t)&dirbuf,
1139 					    sizeof (struct dirtemplate),
1140 					    (off_t)0, UIO_SYSSPACE,
1141 					    IO_NODELOCKED|IO_SYNC,
1142 					    tcnp->cn_cred, (int *)0,
1143 					    (struct proc *)0);
1144 					cache_purge(fdvp);
1145 				}
1146 			}
1147 		}
1148 		error = ufs_dirremove(fdvp, fcnp);
1149 		if (!error) {
1150 			xp->i_nlink--;
1151 			xp->i_flag |= ICHG;
1152 		}
1153 		xp->i_flag &= ~IRENAME;
1154 	}
1155 	if (dp)
1156 		vput(fdvp);
1157 	if (xp)
1158 		vput(fvp);
1159 	vrele(ap->a_fvp);
1160 	return (error);
1161 
1162 bad:
1163 	if (xp)
1164 		vput(ITOV(xp));
1165 	vput(ITOV(dp));
1166 out:
1167 	if (VOP_LOCK(fvp) == 0) {
1168 		ip->i_nlink--;
1169 		ip->i_flag |= ICHG;
1170 		vput(fvp);
1171 	} else
1172 		vrele(fvp);
1173 	return (error);
1174 }
1175 
1176 /*
1177  * A virgin directory (no blushing please).
1178  */
1179 static struct dirtemplate mastertemplate = {
1180 	0, 12, DT_DIR, 1, ".",
1181 	0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
1182 };
1183 static struct odirtemplate omastertemplate = {
1184 	0, 12, 1, ".",
1185 	0, DIRBLKSIZ - 12, 2, ".."
1186 };
1187 
1188 /*
1189  * Mkdir system call
1190  */
1191 int
1192 ufs_mkdir(ap)
1193 	struct vop_mkdir_args /* {
1194 		struct vnode *a_dvp;
1195 		struct vnode **a_vpp;
1196 		struct componentname *a_cnp;
1197 		struct vattr *a_vap;
1198 	} */ *ap;
1199 {
1200 	register struct vnode *dvp = ap->a_dvp;
1201 	register struct vattr *vap = ap->a_vap;
1202 	register struct componentname *cnp = ap->a_cnp;
1203 	register struct inode *ip, *dp;
1204 	struct vnode *tvp;
1205 	struct dirtemplate dirtemplate, *dtp;
1206 	struct timeval tv;
1207 	int error, dmode;
1208 
1209 #ifdef DIAGNOSTIC
1210 	if ((cnp->cn_flags & HASBUF) == 0)
1211 		panic("ufs_mkdir: no name");
1212 #endif
1213 	dp = VTOI(dvp);
1214 	if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1215 		free(cnp->cn_pnbuf, M_NAMEI);
1216 		vput(dvp);
1217 		return (EMLINK);
1218 	}
1219 	dmode = vap->va_mode&0777;
1220 	dmode |= IFDIR;
1221 	/*
1222 	 * Must simulate part of maknode here to acquire the inode, but
1223 	 * not have it entered in the parent directory. The entry is made
1224 	 * later after writing "." and ".." entries.
1225 	 */
1226 	if (error = VOP_VALLOC(dvp, dmode, cnp->cn_cred, &tvp)) {
1227 		free(cnp->cn_pnbuf, M_NAMEI);
1228 		vput(dvp);
1229 		return (error);
1230 	}
1231 	ip = VTOI(tvp);
1232 	ip->i_uid = cnp->cn_cred->cr_uid;
1233 	ip->i_gid = dp->i_gid;
1234 #ifdef QUOTA
1235 	if ((error = getinoquota(ip)) ||
1236 	    (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
1237 		free(cnp->cn_pnbuf, M_NAMEI);
1238 		VOP_VFREE(tvp, ip->i_number, dmode);
1239 		vput(tvp);
1240 		vput(dvp);
1241 		return (error);
1242 	}
1243 #endif
1244 	ip->i_flag |= IACC|IUPD|ICHG;
1245 	ip->i_mode = dmode;
1246 	tvp->v_type = VDIR;	/* Rest init'd in iget() */
1247 	ip->i_nlink = 2;
1248 	tv = time;
1249 	error = VOP_UPDATE(tvp, &tv, &tv, 1);
1250 
1251 	/*
1252 	 * Bump link count in parent directory
1253 	 * to reflect work done below.  Should
1254 	 * be done before reference is created
1255 	 * so reparation is possible if we crash.
1256 	 */
1257 	dp->i_nlink++;
1258 	dp->i_flag |= ICHG;
1259 	if (error = VOP_UPDATE(dvp, &tv, &tv, 1))
1260 		goto bad;
1261 
1262 	/* Initialize directory with "." and ".." from static template. */
1263 	if (dvp->v_mount->mnt_maxsymlinklen > 0)
1264 		dtp = &mastertemplate;
1265 	else
1266 		dtp = (struct dirtemplate *)&omastertemplate;
1267 	dirtemplate = *dtp;
1268 	dirtemplate.dot_ino = ip->i_number;
1269 	dirtemplate.dotdot_ino = dp->i_number;
1270 	error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)&dirtemplate,
1271 	    sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
1272 	    IO_NODELOCKED|IO_SYNC, cnp->cn_cred, (int *)0, (struct proc *)0);
1273 	if (error) {
1274 		dp->i_nlink--;
1275 		dp->i_flag |= ICHG;
1276 		goto bad;
1277 	}
1278 	if (DIRBLKSIZ > VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
1279 		panic("ufs_mkdir: blksize"); /* XXX should grow with balloc() */
1280 	else {
1281 		ip->i_size = DIRBLKSIZ;
1282 		ip->i_flag |= ICHG;
1283 	}
1284 
1285 	/* Directory set up, now install it's entry in the parent directory. */
1286 	if (error = ufs_direnter(ip, dvp, cnp)) {
1287 		dp->i_nlink--;
1288 		dp->i_flag |= ICHG;
1289 	}
1290 bad:
1291 	/*
1292 	 * No need to do an explicit VOP_TRUNCATE here, vrele will do this
1293 	 * for us because we set the link count to 0.
1294 	 */
1295 	if (error) {
1296 		ip->i_nlink = 0;
1297 		ip->i_flag |= ICHG;
1298 		vput(tvp);
1299 	} else
1300 		*ap->a_vpp = tvp;
1301 	FREE(cnp->cn_pnbuf, M_NAMEI);
1302 	vput(dvp);
1303 	return (error);
1304 }
1305 
1306 /*
1307  * Rmdir system call.
1308  */
1309 int
1310 ufs_rmdir(ap)
1311 	struct vop_rmdir_args /* {
1312 		struct vnode *a_dvp;
1313 		struct vnode *a_vp;
1314 		struct componentname *a_cnp;
1315 	} */ *ap;
1316 {
1317 	register struct vnode *vp = ap->a_vp;
1318 	register struct vnode *dvp = ap->a_dvp;
1319 	register struct componentname *cnp = ap->a_cnp;
1320 	register struct inode *ip, *dp;
1321 	int error;
1322 
1323 	ip = VTOI(vp);
1324 	dp = VTOI(dvp);
1325 	/*
1326 	 * No rmdir "." please.
1327 	 */
1328 	if (dp == ip) {
1329 		vrele(dvp);
1330 		vput(vp);
1331 		return (EINVAL);
1332 	}
1333 	/*
1334 	 * Verify the directory is empty (and valid).
1335 	 * (Rmdir ".." won't be valid since
1336 	 *  ".." will contain a reference to
1337 	 *  the current directory and thus be
1338 	 *  non-empty.)
1339 	 */
1340 	error = 0;
1341 	if (ip->i_nlink != 2 ||
1342 	    !ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1343 		error = ENOTEMPTY;
1344 		goto out;
1345 	}
1346 	/*
1347 	 * Delete reference to directory before purging
1348 	 * inode.  If we crash in between, the directory
1349 	 * will be reattached to lost+found,
1350 	 */
1351 	if (error = ufs_dirremove(dvp, cnp))
1352 		goto out;
1353 	dp->i_nlink--;
1354 	dp->i_flag |= ICHG;
1355 	cache_purge(dvp);
1356 	vput(dvp);
1357 	dvp = NULL;
1358 	/*
1359 	 * Truncate inode.  The only stuff left
1360 	 * in the directory is "." and "..".  The
1361 	 * "." reference is inconsequential since
1362 	 * we're quashing it.  The ".." reference
1363 	 * has already been adjusted above.  We've
1364 	 * removed the "." reference and the reference
1365 	 * in the parent directory, but there may be
1366 	 * other hard links so decrement by 2 and
1367 	 * worry about them later.
1368 	 */
1369 	ip->i_nlink -= 2;
1370 	error = VOP_TRUNCATE(vp, (off_t)0, IO_SYNC, cnp->cn_cred,
1371 	    cnp->cn_proc);
1372 	cache_purge(ITOV(ip));
1373 out:
1374 	if (dvp)
1375 		vput(dvp);
1376 	vput(vp);
1377 	return (error);
1378 }
1379 
1380 /*
1381  * symlink -- make a symbolic link
1382  */
1383 int
1384 ufs_symlink(ap)
1385 	struct vop_symlink_args /* {
1386 		struct vnode *a_dvp;
1387 		struct vnode **a_vpp;
1388 		struct componentname *a_cnp;
1389 		struct vattr *a_vap;
1390 		char *a_target;
1391 	} */ *ap;
1392 {
1393 	register struct vnode *vp, **vpp = ap->a_vpp;
1394 	register struct inode *ip;
1395 	int len, error;
1396 
1397 	if (error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1398 	    vpp, ap->a_cnp))
1399 		return (error);
1400 	vp = *vpp;
1401 	len = strlen(ap->a_target);
1402 	if (len < vp->v_mount->mnt_maxsymlinklen) {
1403 		ip = VTOI(vp);
1404 		bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1405 		ip->i_size = len;
1406 		ip->i_flag |= IUPD|ICHG;
1407 	} else
1408 		error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1409 		    UIO_SYSSPACE, IO_NODELOCKED, ap->a_cnp->cn_cred, (int *)0,
1410 		    (struct proc *)0);
1411 	vput(vp);
1412 	return (error);
1413 }
1414 
1415 /*
1416  * Vnode op for reading directories.
1417  *
1418  * The routine below assumes that the on-disk format of a directory
1419  * is the same as that defined by <sys/dirent.h>. If the on-disk
1420  * format changes, then it will be necessary to do a conversion
1421  * from the on-disk format that read returns to the format defined
1422  * by <sys/dirent.h>.
1423  */
1424 int
1425 ufs_readdir(ap)
1426 	struct vop_readdir_args /* {
1427 		struct vnode *a_vp;
1428 		struct uio *a_uio;
1429 		struct ucred *a_cred;
1430 	} */ *ap;
1431 {
1432 	register struct uio *uio = ap->a_uio;
1433 	int count, lost, error;
1434 
1435 	count = uio->uio_resid;
1436 	count &= ~(DIRBLKSIZ - 1);
1437 	lost = uio->uio_resid - count;
1438 	if (count < DIRBLKSIZ || (uio->uio_offset & (DIRBLKSIZ -1)))
1439 		return (EINVAL);
1440 	uio->uio_resid = count;
1441 	uio->uio_iov->iov_len = count;
1442 #	if (BYTE_ORDER == LITTLE_ENDIAN)
1443 		if (ap->a_vp->v_mount->mnt_maxsymlinklen > 0) {
1444 			error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1445 		} else {
1446 			struct dirent *dp, *edp;
1447 			struct uio auio;
1448 			struct iovec aiov;
1449 			caddr_t dirbuf;
1450 			int readcnt;
1451 			u_char tmp;
1452 
1453 			auio = *uio;
1454 			auio.uio_iov = &aiov;
1455 			auio.uio_iovcnt = 1;
1456 			auio.uio_segflg = UIO_SYSSPACE;
1457 			aiov.iov_len = count;
1458 			MALLOC(dirbuf, caddr_t, count, M_TEMP, M_WAITOK);
1459 			aiov.iov_base = dirbuf;
1460 			error = VOP_READ(ap->a_vp, &auio, 0, ap->a_cred);
1461 			if (error == 0) {
1462 				readcnt = count - auio.uio_resid;
1463 				edp = (struct dirent *)&dirbuf[readcnt];
1464 				for (dp = (struct dirent *)dirbuf; dp < edp; ) {
1465 					tmp = dp->d_namlen;
1466 					dp->d_namlen = dp->d_type;
1467 					dp->d_type = tmp;
1468 					if (dp->d_reclen > 0) {
1469 						dp = (struct dirent *)
1470 						    ((char *)dp + dp->d_reclen);
1471 					} else {
1472 						error = EIO;
1473 						break;
1474 					}
1475 				}
1476 				if (dp >= edp)
1477 					error = uiomove(dirbuf, readcnt, uio);
1478 			}
1479 			FREE(dirbuf, M_TEMP);
1480 		}
1481 #	else
1482 		error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1483 #	endif
1484 	uio->uio_resid += lost;
1485 	return (error);
1486 }
1487 
1488 /*
1489  * Return target name of a symbolic link
1490  */
1491 int
1492 ufs_readlink(ap)
1493 	struct vop_readlink_args /* {
1494 		struct vnode *a_vp;
1495 		struct uio *a_uio;
1496 		struct ucred *a_cred;
1497 	} */ *ap;
1498 {
1499 	register struct vnode *vp = ap->a_vp;
1500 	register struct inode *ip = VTOI(vp);
1501 	int isize;
1502 
1503 	isize = ip->i_size;
1504 	if (isize < vp->v_mount->mnt_maxsymlinklen) {
1505 		uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1506 		return (0);
1507 	}
1508 	return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1509 }
1510 
1511 /*
1512  * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually
1513  * done. If a buffer has been saved in anticipation of a CREATE, delete it.
1514  */
1515 /* ARGSUSED */
1516 int
1517 ufs_abortop(ap)
1518 	struct vop_abortop_args /* {
1519 		struct vnode *a_dvp;
1520 		struct componentname *a_cnp;
1521 	} */ *ap;
1522 {
1523 	if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
1524 		FREE(ap->a_cnp->cn_pnbuf, M_NAMEI);
1525 	return (0);
1526 }
1527 
1528 /*
1529  * Lock an inode. If its already locked, set the WANT bit and sleep.
1530  */
1531 int
1532 ufs_lock(ap)
1533 	struct vop_lock_args /* {
1534 		struct vnode *a_vp;
1535 	} */ *ap;
1536 {
1537 	register struct vnode *vp = ap->a_vp;
1538 	register struct inode *ip;
1539 	struct proc *p = curproc;	/* XXX */
1540 
1541 start:
1542 	while (vp->v_flag & VXLOCK) {
1543 		vp->v_flag |= VXWANT;
1544 		sleep((caddr_t)vp, PINOD);
1545 	}
1546 	if (vp->v_tag == VT_NON)
1547 		return (ENOENT);
1548 	ip = VTOI(vp);
1549 	if (ip->i_flag & ILOCKED) {
1550 		ip->i_flag |= IWANT;
1551 #ifdef DIAGNOSTIC
1552 		if (p) {
1553 			if (p->p_pid == ip->i_lockholder)
1554 				panic("locking against myself");
1555 			ip->i_lockwaiter = p->p_pid;
1556 		} else
1557 			ip->i_lockwaiter = -1;
1558 #endif
1559 		(void) sleep((caddr_t)ip, PINOD);
1560 		goto start;
1561 	}
1562 #ifdef DIAGNOSTIC
1563 	ip->i_lockwaiter = 0;
1564 	if (ip->i_lockholder != 0)
1565 		panic("lockholder (%d) != 0", ip->i_lockholder);
1566 	if (p && p->p_pid == 0)
1567 		printf("locking by process 0\n");
1568 	if (p)
1569 		ip->i_lockholder = p->p_pid;
1570 	else
1571 		ip->i_lockholder = -1;
1572 #endif
1573 	ip->i_flag |= ILOCKED;
1574 	return (0);
1575 }
1576 
1577 /*
1578  * Unlock an inode.  If WANT bit is on, wakeup.
1579  */
1580 int lockcount = 90;
1581 int
1582 ufs_unlock(ap)
1583 	struct vop_unlock_args /* {
1584 		struct vnode *a_vp;
1585 	} */ *ap;
1586 {
1587 	register struct inode *ip = VTOI(ap->a_vp);
1588 	struct proc *p = curproc;	/* XXX */
1589 
1590 #ifdef DIAGNOSTIC
1591 	if ((ip->i_flag & ILOCKED) == 0) {
1592 		vprint("ufs_unlock: unlocked inode", ap->a_vp);
1593 		panic("ufs_unlock NOT LOCKED");
1594 	}
1595 	if (p && p->p_pid != ip->i_lockholder && p->p_pid > -1 &&
1596 	    ip->i_lockholder > -1 && lockcount++ < 100)
1597 		panic("unlocker (%d) != lock holder (%d)",
1598 		    p->p_pid, ip->i_lockholder);
1599 	ip->i_lockholder = 0;
1600 #endif
1601 	ip->i_flag &= ~ILOCKED;
1602 	if (ip->i_flag & IWANT) {
1603 		ip->i_flag &= ~IWANT;
1604 		wakeup((caddr_t)ip);
1605 	}
1606 	return (0);
1607 }
1608 
1609 /*
1610  * Check for a locked inode.
1611  */
1612 int
1613 ufs_islocked(ap)
1614 	struct vop_islocked_args /* {
1615 		struct vnode *a_vp;
1616 	} */ *ap;
1617 {
1618 
1619 	if (VTOI(ap->a_vp)->i_flag & ILOCKED)
1620 		return (1);
1621 	return (0);
1622 }
1623 
1624 /*
1625  * Calculate the logical to physical mapping if not done already,
1626  * then call the device strategy routine.
1627  */
1628 int
1629 ufs_strategy(ap)
1630 	struct vop_strategy_args /* {
1631 		struct buf *a_bp;
1632 	} */ *ap;
1633 {
1634 	register struct buf *bp = ap->a_bp;
1635 	register struct vnode *vp = bp->b_vp;
1636 	register struct inode *ip;
1637 	int error;
1638 
1639 	ip = VTOI(vp);
1640 	if (vp->v_type == VBLK || vp->v_type == VCHR)
1641 		panic("ufs_strategy: spec");
1642 	if (bp->b_blkno == bp->b_lblkno) {
1643 		if (error =
1644 		    VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL)) {
1645 			bp->b_error = error;
1646 			bp->b_flags |= B_ERROR;
1647 			biodone(bp);
1648 			return (error);
1649 		}
1650 		if ((long)bp->b_blkno == -1)
1651 			clrbuf(bp);
1652 	}
1653 	if ((long)bp->b_blkno == -1) {
1654 		biodone(bp);
1655 		return (0);
1656 	}
1657 	vp = ip->i_devvp;
1658 	bp->b_dev = vp->v_rdev;
1659 	VOCALL (vp->v_op, VOFFSET(vop_strategy), ap);
1660 	return (0);
1661 }
1662 
1663 /*
1664  * Print out the contents of an inode.
1665  */
1666 int
1667 ufs_print(ap)
1668 	struct vop_print_args /* {
1669 		struct vnode *a_vp;
1670 	} */ *ap;
1671 {
1672 	register struct vnode *vp = ap->a_vp;
1673 	register struct inode *ip = VTOI(vp);
1674 
1675 	printf("tag VT_UFS, ino %d, on dev %d, %d", ip->i_number,
1676 		major(ip->i_dev), minor(ip->i_dev));
1677 #ifdef FIFO
1678 	if (vp->v_type == VFIFO)
1679 		fifo_printinfo(vp);
1680 #endif /* FIFO */
1681 	printf("%s\n", (ip->i_flag & ILOCKED) ? " (LOCKED)" : "");
1682 	if (ip->i_lockholder == 0)
1683 		return (0);
1684 	printf("\towner pid %d", ip->i_lockholder);
1685 	if (ip->i_lockwaiter)
1686 		printf(" waiting pid %d", ip->i_lockwaiter);
1687 	printf("\n");
1688 	return (0);
1689 }
1690 
1691 /*
1692  * Read wrapper for special devices.
1693  */
1694 int
1695 ufsspec_read(ap)
1696 	struct vop_read_args /* {
1697 		struct vnode *a_vp;
1698 		struct uio *a_uio;
1699 		int  a_ioflag;
1700 		struct ucred *a_cred;
1701 	} */ *ap;
1702 {
1703 
1704 	/*
1705 	 * Set access flag.
1706 	 */
1707 	VTOI(ap->a_vp)->i_flag |= IACC;
1708 	return (VOCALL (spec_vnodeop_p, VOFFSET(vop_read), ap));
1709 }
1710 
1711 /*
1712  * Write wrapper for special devices.
1713  */
1714 int
1715 ufsspec_write(ap)
1716 	struct vop_write_args /* {
1717 		struct vnode *a_vp;
1718 		struct uio *a_uio;
1719 		int  a_ioflag;
1720 		struct ucred *a_cred;
1721 	} */ *ap;
1722 {
1723 
1724 	/*
1725 	 * Set update and change flags.
1726 	 */
1727 	VTOI(ap->a_vp)->i_flag |= IUPD|ICHG;
1728 	return (VOCALL (spec_vnodeop_p, VOFFSET(vop_write), ap));
1729 }
1730 
1731 /*
1732  * Close wrapper for special devices.
1733  *
1734  * Update the times on the inode then do device close.
1735  */
1736 int
1737 ufsspec_close(ap)
1738 	struct vop_close_args /* {
1739 		struct vnode *a_vp;
1740 		int  a_fflag;
1741 		struct ucred *a_cred;
1742 		struct proc *a_p;
1743 	} */ *ap;
1744 {
1745 	register struct inode *ip = VTOI(ap->a_vp);
1746 
1747 	if (ap->a_vp->v_usecount > 1 && !(ip->i_flag & ILOCKED))
1748 		ITIMES(ip, &time, &time);
1749 	return (VOCALL (spec_vnodeop_p, VOFFSET(vop_close), ap));
1750 }
1751 
1752 #ifdef FIFO
1753 /*
1754  * Read wrapper for fifo's
1755  */
1756 int
1757 ufsfifo_read(ap)
1758 	struct vop_read_args /* {
1759 		struct vnode *a_vp;
1760 		struct uio *a_uio;
1761 		int  a_ioflag;
1762 		struct ucred *a_cred;
1763 	} */ *ap;
1764 {
1765 	extern int (**fifo_vnodeop_p)();
1766 
1767 	/*
1768 	 * Set access flag.
1769 	 */
1770 	VTOI(ap->a_vp)->i_flag |= IACC;
1771 	return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_read), ap));
1772 }
1773 
1774 /*
1775  * Write wrapper for fifo's.
1776  */
1777 int
1778 ufsfifo_write(ap)
1779 	struct vop_write_args /* {
1780 		struct vnode *a_vp;
1781 		struct uio *a_uio;
1782 		int  a_ioflag;
1783 		struct ucred *a_cred;
1784 	} */ *ap;
1785 {
1786 	extern int (**fifo_vnodeop_p)();
1787 
1788 	/*
1789 	 * Set update and change flags.
1790 	 */
1791 	VTOI(ap->a_vp)->i_flag |= IUPD|ICHG;
1792 	return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_write), ap));
1793 }
1794 
1795 /*
1796  * Close wrapper for fifo's.
1797  *
1798  * Update the times on the inode then do device close.
1799  */
1800 ufsfifo_close(ap)
1801 	struct vop_close_args /* {
1802 		struct vnode *a_vp;
1803 		int  a_fflag;
1804 		struct ucred *a_cred;
1805 		struct proc *a_p;
1806 	} */ *ap;
1807 {
1808 	extern int (**fifo_vnodeop_p)();
1809 	register struct inode *ip = VTOI(ap->a_vp);
1810 
1811 	if (ap->a_vp->v_usecount > 1 && !(ip->i_flag & ILOCKED))
1812 		ITIMES(ip, &time, &time);
1813 	return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_close), ap));
1814 }
1815 #endif /* FIFO */
1816 
1817 /*
1818  * Advisory record locking support
1819  */
1820 int
1821 ufs_advlock(ap)
1822 	struct vop_advlock_args /* {
1823 		struct vnode *a_vp;
1824 		caddr_t  a_id;
1825 		int  a_op;
1826 		struct flock *a_fl;
1827 		int  a_flags;
1828 	} */ *ap;
1829 {
1830 	register struct inode *ip = VTOI(ap->a_vp);
1831 	register struct flock *fl = ap->a_fl;
1832 	register struct lockf *lock;
1833 	off_t start, end;
1834 	int error;
1835 
1836 	/*
1837 	 * Avoid the common case of unlocking when inode has no locks.
1838 	 */
1839 	if (ip->i_lockf == (struct lockf *)0) {
1840 		if (ap->a_op != F_SETLK) {
1841 			fl->l_type = F_UNLCK;
1842 			return (0);
1843 		}
1844 	}
1845 	/*
1846 	 * Convert the flock structure into a start and end.
1847 	 */
1848 	switch (fl->l_whence) {
1849 
1850 	case SEEK_SET:
1851 	case SEEK_CUR:
1852 		/*
1853 		 * Caller is responsible for adding any necessary offset
1854 		 * when SEEK_CUR is used.
1855 		 */
1856 		start = fl->l_start;
1857 		break;
1858 
1859 	case SEEK_END:
1860 		start = ip->i_size + fl->l_start;
1861 		break;
1862 
1863 	default:
1864 		return (EINVAL);
1865 	}
1866 	if (start < 0)
1867 		return (EINVAL);
1868 	if (fl->l_len == 0)
1869 		end = -1;
1870 	else
1871 		end = start + fl->l_len - 1;
1872 	/*
1873 	 * Create the lockf structure
1874 	 */
1875 	MALLOC(lock, struct lockf *, sizeof *lock, M_LOCKF, M_WAITOK);
1876 	lock->lf_start = start;
1877 	lock->lf_end = end;
1878 	lock->lf_id = ap->a_id;
1879 	lock->lf_inode = ip;
1880 	lock->lf_type = fl->l_type;
1881 	lock->lf_next = (struct lockf *)0;
1882 	lock->lf_block = (struct lockf *)0;
1883 	lock->lf_flags = ap->a_flags;
1884 	/*
1885 	 * Do the requested operation.
1886 	 */
1887 	switch(ap->a_op) {
1888 	case F_SETLK:
1889 		return (lf_setlock(lock));
1890 
1891 	case F_UNLCK:
1892 		error = lf_clearlock(lock);
1893 		FREE(lock, M_LOCKF);
1894 		return (error);
1895 
1896 	case F_GETLK:
1897 		error = lf_getlock(lock, fl);
1898 		FREE(lock, M_LOCKF);
1899 		return (error);
1900 
1901 	default:
1902 		free(lock, M_LOCKF);
1903 		return (EINVAL);
1904 	}
1905 	/* NOTREACHED */
1906 }
1907 
1908 /*
1909  * Initialize the vnode associated with a new inode, handle aliased
1910  * vnodes.
1911  */
1912 int
1913 ufs_vinit(mntp, specops, fifoops, vpp)
1914 	struct mount *mntp;
1915 	int (**specops)();
1916 	int (**fifoops)();
1917 	struct vnode **vpp;
1918 {
1919 	struct inode *ip;
1920 	struct vnode *vp, *nvp;
1921 
1922 	vp = *vpp;
1923 	ip = VTOI(vp);
1924 	switch(vp->v_type = IFTOVT(ip->i_mode)) {
1925 	case VCHR:
1926 	case VBLK:
1927 		vp->v_op = specops;
1928 		if (nvp = checkalias(vp, ip->i_rdev, mntp)) {
1929 			/*
1930 			 * Discard unneeded vnode, but save its inode.
1931 			 */
1932 			ufs_ihashrem(ip);
1933 			VOP_UNLOCK(vp);
1934 			nvp->v_data = vp->v_data;
1935 			vp->v_data = NULL;
1936 			vp->v_op = spec_vnodeop_p;
1937 			vrele(vp);
1938 			vgone(vp);
1939 			/*
1940 			 * Reinitialize aliased inode.
1941 			 */
1942 			vp = nvp;
1943 			ip->i_vnode = vp;
1944 			ufs_ihashins(ip);
1945 		}
1946 		break;
1947 	case VFIFO:
1948 #ifdef FIFO
1949 		vp->v_op = fifoops;
1950 		break;
1951 #else
1952 		return (EOPNOTSUPP);
1953 #endif
1954 	}
1955 	if (ip->i_number == ROOTINO)
1956                 vp->v_flag |= VROOT;
1957 	/*
1958 	 * Initialize modrev times
1959 	 */
1960 	SETHIGH(ip->i_modrev, mono_time.tv_sec);
1961 	SETLOW(ip->i_modrev, mono_time.tv_usec * 4294);
1962 	*vpp = vp;
1963 	return (0);
1964 }
1965 
1966 /*
1967  * Allocate a new inode.
1968  */
1969 int
1970 ufs_makeinode(mode, dvp, vpp, cnp)
1971 	int mode;
1972 	struct vnode *dvp;
1973 	struct vnode **vpp;
1974 	struct componentname *cnp;
1975 {
1976 	register struct inode *ip, *pdir;
1977 	struct timeval tv;
1978 	struct vnode *tvp;
1979 	int error;
1980 
1981 	pdir = VTOI(dvp);
1982 #ifdef DIAGNOSTIC
1983 	if ((cnp->cn_flags & HASBUF) == 0)
1984 		panic("ufs_makeinode: no name");
1985 #endif
1986 	*vpp = NULL;
1987 	if ((mode & IFMT) == 0)
1988 		mode |= IFREG;
1989 
1990 	if (error = VOP_VALLOC(dvp, mode, cnp->cn_cred, &tvp)) {
1991 		free(cnp->cn_pnbuf, M_NAMEI);
1992 		vput(dvp);
1993 		return (error);
1994 	}
1995 	ip = VTOI(tvp);
1996 	ip->i_uid = cnp->cn_cred->cr_uid;
1997 	ip->i_gid = pdir->i_gid;
1998 #ifdef QUOTA
1999 	if ((error = getinoquota(ip)) ||
2000 	    (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
2001 		free(cnp->cn_pnbuf, M_NAMEI);
2002 		VOP_VFREE(tvp, ip->i_number, mode);
2003 		vput(tvp);
2004 		vput(dvp);
2005 		return (error);
2006 	}
2007 #endif
2008 	ip->i_flag |= IACC|IUPD|ICHG;
2009 	ip->i_mode = mode;
2010 	tvp->v_type = IFTOVT(mode);	/* Rest init'd in iget() */
2011 	ip->i_nlink = 1;
2012 	if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
2013 	    suser(cnp->cn_cred, NULL))
2014 		ip->i_mode &= ~ISGID;
2015 
2016 	/*
2017 	 * Make sure inode goes to disk before directory entry.
2018 	 */
2019 	tv = time;
2020 	if (error = VOP_UPDATE(tvp, &tv, &tv, 1))
2021 		goto bad;
2022 	if (error = ufs_direnter(ip, dvp, cnp))
2023 		goto bad;
2024 	if ((cnp->cn_flags & SAVESTART) == 0)
2025 		FREE(cnp->cn_pnbuf, M_NAMEI);
2026 	vput(dvp);
2027 	*vpp = tvp;
2028 	return (0);
2029 
2030 bad:
2031 	/*
2032 	 * Write error occurred trying to update the inode
2033 	 * or the directory so must deallocate the inode.
2034 	 */
2035 	free(cnp->cn_pnbuf, M_NAMEI);
2036 	vput(dvp);
2037 	ip->i_nlink = 0;
2038 	ip->i_flag |= ICHG;
2039 	vput(tvp);
2040 	return (error);
2041 }
2042