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