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