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