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