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