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