xref: /original-bsd/sys/ufs/ufs/ufs_vnops.c (revision e031425c)
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.17 (Berkeley) 01/07/95
13  */
14 
15 #include <sys/param.h>
16 #include <sys/systm.h>
17 #include <sys/namei.h>
18 #include <sys/resourcevar.h>
19 #include <sys/kernel.h>
20 #include <sys/file.h>
21 #include <sys/stat.h>
22 #include <sys/buf.h>
23 #include <sys/proc.h>
24 #include <sys/conf.h>
25 #include <sys/mount.h>
26 #include <sys/vnode.h>
27 #include <sys/malloc.h>
28 #include <sys/dirent.h>
29 
30 #include <vm/vm.h>
31 
32 #include <miscfs/specfs/specdev.h>
33 
34 #include <ufs/ufs/lockf.h>
35 #include <ufs/ufs/quota.h>
36 #include <ufs/ufs/inode.h>
37 #include <ufs/ufs/dir.h>
38 #include <ufs/ufs/ufsmount.h>
39 #include <ufs/ufs/ufs_extern.h>
40 
41 static int ufs_chmod __P((struct vnode *, int, struct ucred *, struct proc *));
42 static int ufs_chown
43 	__P((struct vnode *, uid_t, gid_t, struct ucred *, struct proc *));
44 
45 union _qcvt {
46 	int64_t qcvt;
47 	int32_t val[2];
48 };
49 #define SETHIGH(q, h) { \
50 	union _qcvt tmp; \
51 	tmp.qcvt = (q); \
52 	tmp.val[_QUAD_HIGHWORD] = (h); \
53 	(q) = tmp.qcvt; \
54 }
55 #define SETLOW(q, l) { \
56 	union _qcvt tmp; \
57 	tmp.qcvt = (q); \
58 	tmp.val[_QUAD_LOWWORD] = (l); \
59 	(q) = tmp.qcvt; \
60 }
61 
62 /*
63  * Create a regular file
64  */
65 int
66 ufs_create(ap)
67 	struct vop_create_args /* {
68 		struct vnode *a_dvp;
69 		struct vnode **a_vpp;
70 		struct componentname *a_cnp;
71 		struct vattr *a_vap;
72 	} */ *ap;
73 {
74 	int error;
75 
76 	if (error =
77 	    ufs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
78 	    ap->a_dvp, ap->a_vpp, ap->a_cnp))
79 		return (error);
80 	return (0);
81 }
82 
83 /*
84  * Mknod vnode call
85  */
86 /* ARGSUSED */
87 int
88 ufs_mknod(ap)
89 	struct vop_mknod_args /* {
90 		struct vnode *a_dvp;
91 		struct vnode **a_vpp;
92 		struct componentname *a_cnp;
93 		struct vattr *a_vap;
94 	} */ *ap;
95 {
96 	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 	    (gid != ip->i_gid && !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 
983 		/* Release destination completely. */
984 		VOP_ABORTOP(tdvp, tcnp);
985 		vput(tdvp);
986 		vput(tvp);
987 
988 		/* Delete source. */
989 		vrele(fdvp);
990 		vrele(fvp);
991 		fcnp->cn_flags &= ~MODMASK;
992 		fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
993 		if ((fcnp->cn_flags & SAVESTART) == 0)
994 			panic("ufs_rename: lost from startdir");
995 		fcnp->cn_nameiop = DELETE;
996 		(void) relookup(fdvp, &fvp, fcnp);
997 		return (VOP_REMOVE(fdvp, fvp, fcnp));
998 	}
999 	if (error = VOP_LOCK(fvp))
1000 		goto abortit;
1001 	dp = VTOI(fdvp);
1002 	ip = VTOI(fvp);
1003 	if ((ip->i_flags & (IMMUTABLE | APPEND)) || (dp->i_flags & APPEND)) {
1004 		VOP_UNLOCK(fvp);
1005 		error = EPERM;
1006 		goto abortit;
1007 	}
1008 	if ((ip->i_mode & IFMT) == IFDIR) {
1009 		/*
1010 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
1011 		 */
1012 		if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
1013 		    dp == ip || (fcnp->cn_flags&ISDOTDOT) ||
1014 		    (ip->i_flag & IN_RENAME)) {
1015 			VOP_UNLOCK(fvp);
1016 			error = EINVAL;
1017 			goto abortit;
1018 		}
1019 		ip->i_flag |= IN_RENAME;
1020 		oldparent = dp->i_number;
1021 		doingdirectory++;
1022 	}
1023 	vrele(fdvp);
1024 
1025 	/*
1026 	 * When the target exists, both the directory
1027 	 * and target vnodes are returned locked.
1028 	 */
1029 	dp = VTOI(tdvp);
1030 	xp = NULL;
1031 	if (tvp)
1032 		xp = VTOI(tvp);
1033 
1034 	/*
1035 	 * 1) Bump link count while we're moving stuff
1036 	 *    around.  If we crash somewhere before
1037 	 *    completing our work, the link count
1038 	 *    may be wrong, but correctable.
1039 	 */
1040 	ip->i_nlink++;
1041 	ip->i_flag |= IN_CHANGE;
1042 	tv = time;
1043 	if (error = VOP_UPDATE(fvp, &tv, &tv, 1)) {
1044 		VOP_UNLOCK(fvp);
1045 		goto bad;
1046 	}
1047 
1048 	/*
1049 	 * If ".." must be changed (ie the directory gets a new
1050 	 * parent) then the source directory must not be in the
1051 	 * directory heirarchy above the target, as this would
1052 	 * orphan everything below the source directory. Also
1053 	 * the user must have write permission in the source so
1054 	 * as to be able to change "..". We must repeat the call
1055 	 * to namei, as the parent directory is unlocked by the
1056 	 * call to checkpath().
1057 	 */
1058 	error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc);
1059 	VOP_UNLOCK(fvp);
1060 	if (oldparent != dp->i_number)
1061 		newparent = dp->i_number;
1062 	if (doingdirectory && newparent) {
1063 		if (error)	/* write access check above */
1064 			goto bad;
1065 		if (xp != NULL)
1066 			vput(tvp);
1067 		if (error = ufs_checkpath(ip, dp, tcnp->cn_cred))
1068 			goto out;
1069 		if ((tcnp->cn_flags & SAVESTART) == 0)
1070 			panic("ufs_rename: lost to startdir");
1071 		if (error = relookup(tdvp, &tvp, tcnp))
1072 			goto out;
1073 		dp = VTOI(tdvp);
1074 		xp = NULL;
1075 		if (tvp)
1076 			xp = VTOI(tvp);
1077 	}
1078 	/*
1079 	 * 2) If target doesn't exist, link the target
1080 	 *    to the source and unlink the source.
1081 	 *    Otherwise, rewrite the target directory
1082 	 *    entry to reference the source inode and
1083 	 *    expunge the original entry's existence.
1084 	 */
1085 	if (xp == NULL) {
1086 		if (dp->i_dev != ip->i_dev)
1087 			panic("rename: EXDEV");
1088 		/*
1089 		 * Account for ".." in new directory.
1090 		 * When source and destination have the same
1091 		 * parent we don't fool with the link count.
1092 		 */
1093 		if (doingdirectory && newparent) {
1094 			if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1095 				error = EMLINK;
1096 				goto bad;
1097 			}
1098 			dp->i_nlink++;
1099 			dp->i_flag |= IN_CHANGE;
1100 			if (error = VOP_UPDATE(tdvp, &tv, &tv, 1))
1101 				goto bad;
1102 		}
1103 		if (error = ufs_direnter(ip, tdvp, tcnp)) {
1104 			if (doingdirectory && newparent) {
1105 				dp->i_nlink--;
1106 				dp->i_flag |= IN_CHANGE;
1107 				(void)VOP_UPDATE(tdvp, &tv, &tv, 1);
1108 			}
1109 			goto bad;
1110 		}
1111 		vput(tdvp);
1112 	} else {
1113 		if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
1114 			panic("rename: EXDEV");
1115 		/*
1116 		 * Short circuit rename(foo, foo).
1117 		 */
1118 		if (xp->i_number == ip->i_number)
1119 			panic("rename: same file");
1120 		/*
1121 		 * If the parent directory is "sticky", then the user must
1122 		 * own the parent directory, or the destination of the rename,
1123 		 * otherwise the destination may not be changed (except by
1124 		 * root). This implements append-only directories.
1125 		 */
1126 		if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
1127 		    tcnp->cn_cred->cr_uid != dp->i_uid &&
1128 		    xp->i_uid != tcnp->cn_cred->cr_uid) {
1129 			error = EPERM;
1130 			goto bad;
1131 		}
1132 		/*
1133 		 * Target must be empty if a directory and have no links
1134 		 * to it. Also, ensure source and target are compatible
1135 		 * (both directories, or both not directories).
1136 		 */
1137 		if ((xp->i_mode&IFMT) == IFDIR) {
1138 			if (!ufs_dirempty(xp, dp->i_number, tcnp->cn_cred) ||
1139 			    xp->i_nlink > 2) {
1140 				error = ENOTEMPTY;
1141 				goto bad;
1142 			}
1143 			if (!doingdirectory) {
1144 				error = ENOTDIR;
1145 				goto bad;
1146 			}
1147 			cache_purge(tdvp);
1148 		} else if (doingdirectory) {
1149 			error = EISDIR;
1150 			goto bad;
1151 		}
1152 		if (error = ufs_dirrewrite(dp, ip, tcnp))
1153 			goto bad;
1154 		/*
1155 		 * If the target directory is in the same
1156 		 * directory as the source directory,
1157 		 * decrement the link count on the parent
1158 		 * of the target directory.
1159 		 */
1160 		 if (doingdirectory && !newparent) {
1161 			dp->i_nlink--;
1162 			dp->i_flag |= IN_CHANGE;
1163 		}
1164 		vput(tdvp);
1165 		/*
1166 		 * Adjust the link count of the target to
1167 		 * reflect the dirrewrite above.  If this is
1168 		 * a directory it is empty and there are
1169 		 * no links to it, so we can squash the inode and
1170 		 * any space associated with it.  We disallowed
1171 		 * renaming over top of a directory with links to
1172 		 * it above, as the remaining link would point to
1173 		 * a directory without "." or ".." entries.
1174 		 */
1175 		xp->i_nlink--;
1176 		if (doingdirectory) {
1177 			if (--xp->i_nlink != 0)
1178 				panic("rename: linked directory");
1179 			error = VOP_TRUNCATE(tvp, (off_t)0, IO_SYNC,
1180 			    tcnp->cn_cred, tcnp->cn_proc);
1181 		}
1182 		xp->i_flag |= IN_CHANGE;
1183 		vput(tvp);
1184 		xp = NULL;
1185 	}
1186 
1187 	/*
1188 	 * 3) Unlink the source.
1189 	 */
1190 	fcnp->cn_flags &= ~MODMASK;
1191 	fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1192 	if ((fcnp->cn_flags & SAVESTART) == 0)
1193 		panic("ufs_rename: lost from startdir");
1194 	(void) relookup(fdvp, &fvp, fcnp);
1195 	if (fvp != NULL) {
1196 		xp = VTOI(fvp);
1197 		dp = VTOI(fdvp);
1198 	} else {
1199 		/*
1200 		 * From name has disappeared.
1201 		 */
1202 		if (doingdirectory)
1203 			panic("rename: lost dir entry");
1204 		vrele(ap->a_fvp);
1205 		return (0);
1206 	}
1207 	/*
1208 	 * Ensure that the directory entry still exists and has not
1209 	 * changed while the new name has been entered. If the source is
1210 	 * a file then the entry may have been unlinked or renamed. In
1211 	 * either case there is no further work to be done. If the source
1212 	 * is a directory then it cannot have been rmdir'ed; its link
1213 	 * count of three would cause a rmdir to fail with ENOTEMPTY.
1214 	 * The IRENAME flag ensures that it cannot be moved by another
1215 	 * rename.
1216 	 */
1217 	if (xp != ip) {
1218 		if (doingdirectory)
1219 			panic("rename: lost dir entry");
1220 	} else {
1221 		/*
1222 		 * If the source is a directory with a
1223 		 * new parent, the link count of the old
1224 		 * parent directory must be decremented
1225 		 * and ".." set to point to the new parent.
1226 		 */
1227 		if (doingdirectory && newparent) {
1228 			dp->i_nlink--;
1229 			dp->i_flag |= IN_CHANGE;
1230 			error = vn_rdwr(UIO_READ, fvp, (caddr_t)&dirbuf,
1231 				sizeof (struct dirtemplate), (off_t)0,
1232 				UIO_SYSSPACE, IO_NODELOCKED,
1233 				tcnp->cn_cred, (int *)0, (struct proc *)0);
1234 			if (error == 0) {
1235 #				if (BYTE_ORDER == LITTLE_ENDIAN)
1236 					if (fvp->v_mount->mnt_maxsymlinklen <= 0)
1237 						namlen = dirbuf.dotdot_type;
1238 					else
1239 						namlen = dirbuf.dotdot_namlen;
1240 #				else
1241 					namlen = dirbuf.dotdot_namlen;
1242 #				endif
1243 				if (namlen != 2 ||
1244 				    dirbuf.dotdot_name[0] != '.' ||
1245 				    dirbuf.dotdot_name[1] != '.') {
1246 					ufs_dirbad(xp, (doff_t)12,
1247 					    "rename: mangled dir");
1248 				} else {
1249 					dirbuf.dotdot_ino = newparent;
1250 					(void) vn_rdwr(UIO_WRITE, fvp,
1251 					    (caddr_t)&dirbuf,
1252 					    sizeof (struct dirtemplate),
1253 					    (off_t)0, UIO_SYSSPACE,
1254 					    IO_NODELOCKED|IO_SYNC,
1255 					    tcnp->cn_cred, (int *)0,
1256 					    (struct proc *)0);
1257 					cache_purge(fdvp);
1258 				}
1259 			}
1260 		}
1261 		error = ufs_dirremove(fdvp, fcnp);
1262 		if (!error) {
1263 			xp->i_nlink--;
1264 			xp->i_flag |= IN_CHANGE;
1265 		}
1266 		xp->i_flag &= ~IN_RENAME;
1267 	}
1268 	if (dp)
1269 		vput(fdvp);
1270 	if (xp)
1271 		vput(fvp);
1272 	vrele(ap->a_fvp);
1273 	return (error);
1274 
1275 bad:
1276 	if (xp)
1277 		vput(ITOV(xp));
1278 	vput(ITOV(dp));
1279 out:
1280 	if (doingdirectory)
1281 		ip->i_flag &= ~IN_RENAME;
1282 	if (VOP_LOCK(fvp) == 0) {
1283 		ip->i_nlink--;
1284 		ip->i_flag |= IN_CHANGE;
1285 		vput(fvp);
1286 	} else
1287 		vrele(fvp);
1288 	return (error);
1289 }
1290 
1291 /*
1292  * A virgin directory (no blushing please).
1293  */
1294 static struct dirtemplate mastertemplate = {
1295 	0, 12, DT_DIR, 1, ".",
1296 	0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
1297 };
1298 static struct odirtemplate omastertemplate = {
1299 	0, 12, 1, ".",
1300 	0, DIRBLKSIZ - 12, 2, ".."
1301 };
1302 
1303 /*
1304  * Mkdir system call
1305  */
1306 int
1307 ufs_mkdir(ap)
1308 	struct vop_mkdir_args /* {
1309 		struct vnode *a_dvp;
1310 		struct vnode **a_vpp;
1311 		struct componentname *a_cnp;
1312 		struct vattr *a_vap;
1313 	} */ *ap;
1314 {
1315 	register struct vnode *dvp = ap->a_dvp;
1316 	register struct vattr *vap = ap->a_vap;
1317 	register struct componentname *cnp = ap->a_cnp;
1318 	register struct inode *ip, *dp;
1319 	struct vnode *tvp;
1320 	struct dirtemplate dirtemplate, *dtp;
1321 	struct timeval tv;
1322 	int error, dmode;
1323 
1324 #ifdef DIAGNOSTIC
1325 	if ((cnp->cn_flags & HASBUF) == 0)
1326 		panic("ufs_mkdir: no name");
1327 #endif
1328 	dp = VTOI(dvp);
1329 	if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1330 		error = EMLINK;
1331 		goto out;
1332 	}
1333 	dmode = vap->va_mode & 0777;
1334 	dmode |= IFDIR;
1335 	/*
1336 	 * Must simulate part of ufs_makeinode here to acquire the inode,
1337 	 * but not have it entered in the parent directory. The entry is
1338 	 * made later after writing "." and ".." entries.
1339 	 */
1340 	if (error = VOP_VALLOC(dvp, dmode, cnp->cn_cred, &tvp))
1341 		goto out;
1342 	ip = VTOI(tvp);
1343 	ip->i_uid = cnp->cn_cred->cr_uid;
1344 	ip->i_gid = dp->i_gid;
1345 #ifdef QUOTA
1346 	if ((error = getinoquota(ip)) ||
1347 	    (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
1348 		free(cnp->cn_pnbuf, M_NAMEI);
1349 		VOP_VFREE(tvp, ip->i_number, dmode);
1350 		vput(tvp);
1351 		vput(dvp);
1352 		return (error);
1353 	}
1354 #endif
1355 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1356 	ip->i_mode = dmode;
1357 	tvp->v_type = VDIR;	/* Rest init'd in getnewvnode(). */
1358 	ip->i_nlink = 2;
1359 	if (cnp->cn_flags & ISWHITEOUT)
1360 		ip->i_flags |= UF_OPAQUE;
1361 	tv = time;
1362 	error = VOP_UPDATE(tvp, &tv, &tv, 1);
1363 
1364 	/*
1365 	 * Bump link count in parent directory
1366 	 * to reflect work done below.  Should
1367 	 * be done before reference is created
1368 	 * so reparation is possible if we crash.
1369 	 */
1370 	dp->i_nlink++;
1371 	dp->i_flag |= IN_CHANGE;
1372 	if (error = VOP_UPDATE(dvp, &tv, &tv, 1))
1373 		goto bad;
1374 
1375 	/* Initialize directory with "." and ".." from static template. */
1376 	if (dvp->v_mount->mnt_maxsymlinklen > 0)
1377 		dtp = &mastertemplate;
1378 	else
1379 		dtp = (struct dirtemplate *)&omastertemplate;
1380 	dirtemplate = *dtp;
1381 	dirtemplate.dot_ino = ip->i_number;
1382 	dirtemplate.dotdot_ino = dp->i_number;
1383 	error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)&dirtemplate,
1384 	    sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
1385 	    IO_NODELOCKED|IO_SYNC, cnp->cn_cred, (int *)0, (struct proc *)0);
1386 	if (error) {
1387 		dp->i_nlink--;
1388 		dp->i_flag |= IN_CHANGE;
1389 		goto bad;
1390 	}
1391 	if (DIRBLKSIZ > VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
1392 		panic("ufs_mkdir: blksize"); /* XXX should grow with balloc() */
1393 	else {
1394 		ip->i_size = DIRBLKSIZ;
1395 		ip->i_flag |= IN_CHANGE;
1396 	}
1397 
1398 	/* Directory set up, now install it's entry in the parent directory. */
1399 	if (error = ufs_direnter(ip, dvp, cnp)) {
1400 		dp->i_nlink--;
1401 		dp->i_flag |= IN_CHANGE;
1402 	}
1403 bad:
1404 	/*
1405 	 * No need to do an explicit VOP_TRUNCATE here, vrele will do this
1406 	 * for us because we set the link count to 0.
1407 	 */
1408 	if (error) {
1409 		ip->i_nlink = 0;
1410 		ip->i_flag |= IN_CHANGE;
1411 		vput(tvp);
1412 	} else
1413 		*ap->a_vpp = tvp;
1414 out:
1415 	FREE(cnp->cn_pnbuf, M_NAMEI);
1416 	vput(dvp);
1417 	return (error);
1418 }
1419 
1420 /*
1421  * Rmdir system call.
1422  */
1423 int
1424 ufs_rmdir(ap)
1425 	struct vop_rmdir_args /* {
1426 		struct vnode *a_dvp;
1427 		struct vnode *a_vp;
1428 		struct componentname *a_cnp;
1429 	} */ *ap;
1430 {
1431 	register struct vnode *vp = ap->a_vp;
1432 	register struct vnode *dvp = ap->a_dvp;
1433 	register struct componentname *cnp = ap->a_cnp;
1434 	register struct inode *ip, *dp;
1435 	int error;
1436 
1437 	ip = VTOI(vp);
1438 	dp = VTOI(dvp);
1439 	/*
1440 	 * No rmdir "." please.
1441 	 */
1442 	if (dp == ip) {
1443 		vrele(dvp);
1444 		vput(vp);
1445 		return (EINVAL);
1446 	}
1447 	/*
1448 	 * Verify the directory is empty (and valid).
1449 	 * (Rmdir ".." won't be valid since
1450 	 *  ".." will contain a reference to
1451 	 *  the current directory and thus be
1452 	 *  non-empty.)
1453 	 */
1454 	error = 0;
1455 	if (ip->i_nlink != 2 ||
1456 	    !ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1457 		error = ENOTEMPTY;
1458 		goto out;
1459 	}
1460 	if ((dp->i_flags & APPEND) || (ip->i_flags & (IMMUTABLE | APPEND))) {
1461 		error = EPERM;
1462 		goto out;
1463 	}
1464 	/*
1465 	 * Delete reference to directory before purging
1466 	 * inode.  If we crash in between, the directory
1467 	 * will be reattached to lost+found,
1468 	 */
1469 	if (error = ufs_dirremove(dvp, cnp))
1470 		goto out;
1471 	dp->i_nlink--;
1472 	dp->i_flag |= IN_CHANGE;
1473 	cache_purge(dvp);
1474 	vput(dvp);
1475 	dvp = NULL;
1476 	/*
1477 	 * Truncate inode.  The only stuff left
1478 	 * in the directory is "." and "..".  The
1479 	 * "." reference is inconsequential since
1480 	 * we're quashing it.  The ".." reference
1481 	 * has already been adjusted above.  We've
1482 	 * removed the "." reference and the reference
1483 	 * in the parent directory, but there may be
1484 	 * other hard links so decrement by 2 and
1485 	 * worry about them later.
1486 	 */
1487 	ip->i_nlink -= 2;
1488 	error = VOP_TRUNCATE(vp, (off_t)0, IO_SYNC, cnp->cn_cred,
1489 	    cnp->cn_proc);
1490 	cache_purge(ITOV(ip));
1491 out:
1492 	if (dvp)
1493 		vput(dvp);
1494 	vput(vp);
1495 	return (error);
1496 }
1497 
1498 /*
1499  * symlink -- make a symbolic link
1500  */
1501 int
1502 ufs_symlink(ap)
1503 	struct vop_symlink_args /* {
1504 		struct vnode *a_dvp;
1505 		struct vnode **a_vpp;
1506 		struct componentname *a_cnp;
1507 		struct vattr *a_vap;
1508 		char *a_target;
1509 	} */ *ap;
1510 {
1511 	register struct vnode *vp, **vpp = ap->a_vpp;
1512 	register struct inode *ip;
1513 	int len, error;
1514 
1515 	if (error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1516 	    vpp, ap->a_cnp))
1517 		return (error);
1518 	vp = *vpp;
1519 	len = strlen(ap->a_target);
1520 	if (len < vp->v_mount->mnt_maxsymlinklen) {
1521 		ip = VTOI(vp);
1522 		bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1523 		ip->i_size = len;
1524 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
1525 	} else
1526 		error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1527 		    UIO_SYSSPACE, IO_NODELOCKED, ap->a_cnp->cn_cred, (int *)0,
1528 		    (struct proc *)0);
1529 	vput(vp);
1530 	return (error);
1531 }
1532 
1533 /*
1534  * Vnode op for reading directories.
1535  *
1536  * The routine below assumes that the on-disk format of a directory
1537  * is the same as that defined by <sys/dirent.h>. If the on-disk
1538  * format changes, then it will be necessary to do a conversion
1539  * from the on-disk format that read returns to the format defined
1540  * by <sys/dirent.h>.
1541  */
1542 int
1543 ufs_readdir(ap)
1544 	struct vop_readdir_args /* {
1545 		struct vnode *a_vp;
1546 		struct uio *a_uio;
1547 		struct ucred *a_cred;
1548 		int *a_eofflag;
1549 		u_long *a_cookies;
1550 		int ncookies;
1551 	} */ *ap;
1552 {
1553 	register struct uio *uio = ap->a_uio;
1554 	int error;
1555 	size_t count, lost;
1556 	off_t off = uio->uio_offset;
1557 
1558 	count = uio->uio_resid;
1559 	/* Make sure we don't return partial entries. */
1560 	count -= (uio->uio_offset + count) & (DIRBLKSIZ -1);
1561 	if (count <= 0)
1562 		return (EINVAL);
1563 	lost = uio->uio_resid - count;
1564 	uio->uio_resid = count;
1565 	uio->uio_iov->iov_len = count;
1566 #	if (BYTE_ORDER == LITTLE_ENDIAN)
1567 		if (ap->a_vp->v_mount->mnt_maxsymlinklen > 0) {
1568 			error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1569 		} else {
1570 			struct dirent *dp, *edp;
1571 			struct uio auio;
1572 			struct iovec aiov;
1573 			caddr_t dirbuf;
1574 			int readcnt;
1575 			u_char tmp;
1576 
1577 			auio = *uio;
1578 			auio.uio_iov = &aiov;
1579 			auio.uio_iovcnt = 1;
1580 			auio.uio_segflg = UIO_SYSSPACE;
1581 			aiov.iov_len = count;
1582 			MALLOC(dirbuf, caddr_t, count, M_TEMP, M_WAITOK);
1583 			aiov.iov_base = dirbuf;
1584 			error = VOP_READ(ap->a_vp, &auio, 0, ap->a_cred);
1585 			if (error == 0) {
1586 				readcnt = count - auio.uio_resid;
1587 				edp = (struct dirent *)&dirbuf[readcnt];
1588 				for (dp = (struct dirent *)dirbuf; dp < edp; ) {
1589 					tmp = dp->d_namlen;
1590 					dp->d_namlen = dp->d_type;
1591 					dp->d_type = tmp;
1592 					if (dp->d_reclen > 0) {
1593 						dp = (struct dirent *)
1594 						    ((char *)dp + dp->d_reclen);
1595 					} else {
1596 						error = EIO;
1597 						break;
1598 					}
1599 				}
1600 				if (dp >= edp)
1601 					error = uiomove(dirbuf, readcnt, uio);
1602 			}
1603 			FREE(dirbuf, M_TEMP);
1604 		}
1605 #	else
1606 		error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1607 #	endif
1608 	if (!error && ap->a_ncookies) {
1609 		register struct dirent *dp;
1610 		register u_long *cookies = ap->a_cookies;
1611 		register int ncookies = ap->a_ncookies;
1612 
1613 		/*
1614 		 * Only the NFS server uses cookies, and it loads the
1615 		 * directory block into system space, so we can just look at
1616 		 * it directly.
1617 		 */
1618 		if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1619 			panic("ufs_readdir: lost in space");
1620 		dp = (struct dirent *)
1621 		     (uio->uio_iov->iov_base - (uio->uio_offset - off));
1622 		while (ncookies-- && off < uio->uio_offset) {
1623 			if (dp->d_reclen == 0)
1624 				break;
1625 			off += dp->d_reclen;
1626 			*(cookies++) = off;
1627 			dp = (struct dirent *)((caddr_t)dp + dp->d_reclen);
1628 		}
1629 		lost += uio->uio_offset - off;
1630 		uio->uio_offset = off;
1631 	}
1632 	uio->uio_resid += lost;
1633 	*ap->a_eofflag = VTOI(ap->a_vp)->i_size <= uio->uio_offset;
1634 	return (error);
1635 }
1636 
1637 /*
1638  * Return target name of a symbolic link
1639  */
1640 int
1641 ufs_readlink(ap)
1642 	struct vop_readlink_args /* {
1643 		struct vnode *a_vp;
1644 		struct uio *a_uio;
1645 		struct ucred *a_cred;
1646 	} */ *ap;
1647 {
1648 	register struct vnode *vp = ap->a_vp;
1649 	register struct inode *ip = VTOI(vp);
1650 	int isize;
1651 
1652 	isize = ip->i_size;
1653 	if (isize < vp->v_mount->mnt_maxsymlinklen) {
1654 		uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1655 		return (0);
1656 	}
1657 	return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1658 }
1659 
1660 /*
1661  * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually
1662  * done. If a buffer has been saved in anticipation of a CREATE, delete it.
1663  */
1664 /* ARGSUSED */
1665 int
1666 ufs_abortop(ap)
1667 	struct vop_abortop_args /* {
1668 		struct vnode *a_dvp;
1669 		struct componentname *a_cnp;
1670 	} */ *ap;
1671 {
1672 	if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
1673 		FREE(ap->a_cnp->cn_pnbuf, M_NAMEI);
1674 	return (0);
1675 }
1676 
1677 /*
1678  * Lock an inode. If its already locked, set the WANT bit and sleep.
1679  */
1680 int
1681 ufs_lock(ap)
1682 	struct vop_lock_args /* {
1683 		struct vnode *a_vp;
1684 	} */ *ap;
1685 {
1686 	register struct vnode *vp = ap->a_vp;
1687 	register struct inode *ip;
1688 	struct proc *p = curproc;	/* XXX */
1689 
1690 start:
1691 	while (vp->v_flag & VXLOCK) {
1692 		vp->v_flag |= VXWANT;
1693 		sleep((caddr_t)vp, PINOD);
1694 	}
1695 	if (vp->v_tag == VT_NON)
1696 		return (ENOENT);
1697 	ip = VTOI(vp);
1698 	if (ip->i_flag & IN_LOCKED) {
1699 		ip->i_flag |= IN_WANTED;
1700 #ifdef DIAGNOSTIC
1701 		if (p) {
1702 			if (p->p_pid == ip->i_lockholder)
1703 				panic("locking against myself");
1704 			ip->i_lockwaiter = p->p_pid;
1705 		} else
1706 			ip->i_lockwaiter = -1;
1707 #endif
1708 		(void) sleep((caddr_t)ip, PINOD);
1709 		goto start;
1710 	}
1711 #ifdef DIAGNOSTIC
1712 	ip->i_lockwaiter = 0;
1713 	if (ip->i_lockholder != 0)
1714 		panic("lockholder (%d) != 0", ip->i_lockholder);
1715 	if (p && p->p_pid == 0)
1716 		printf("locking by process 0\n");
1717 	if (p)
1718 		ip->i_lockholder = p->p_pid;
1719 	else
1720 		ip->i_lockholder = -1;
1721 #endif
1722 	ip->i_flag |= IN_LOCKED;
1723 	return (0);
1724 }
1725 
1726 /*
1727  * Unlock an inode.  If WANT bit is on, wakeup.
1728  */
1729 int lockcount = 90;
1730 int
1731 ufs_unlock(ap)
1732 	struct vop_unlock_args /* {
1733 		struct vnode *a_vp;
1734 	} */ *ap;
1735 {
1736 	register struct inode *ip = VTOI(ap->a_vp);
1737 	struct proc *p = curproc;	/* XXX */
1738 
1739 #ifdef DIAGNOSTIC
1740 	if ((ip->i_flag & IN_LOCKED) == 0) {
1741 		vprint("ufs_unlock: unlocked inode", ap->a_vp);
1742 		panic("ufs_unlock NOT LOCKED");
1743 	}
1744 	if (p && p->p_pid != ip->i_lockholder && p->p_pid > -1 &&
1745 	    ip->i_lockholder > -1 && lockcount++ < 100)
1746 		panic("unlocker (%d) != lock holder (%d)",
1747 		    p->p_pid, ip->i_lockholder);
1748 	ip->i_lockholder = 0;
1749 #endif
1750 	ip->i_flag &= ~IN_LOCKED;
1751 	if (ip->i_flag & IN_WANTED) {
1752 		ip->i_flag &= ~IN_WANTED;
1753 		wakeup((caddr_t)ip);
1754 	}
1755 	return (0);
1756 }
1757 
1758 /*
1759  * Check for a locked inode.
1760  */
1761 int
1762 ufs_islocked(ap)
1763 	struct vop_islocked_args /* {
1764 		struct vnode *a_vp;
1765 	} */ *ap;
1766 {
1767 
1768 	if (VTOI(ap->a_vp)->i_flag & IN_LOCKED)
1769 		return (1);
1770 	return (0);
1771 }
1772 
1773 /*
1774  * Calculate the logical to physical mapping if not done already,
1775  * then call the device strategy routine.
1776  */
1777 int
1778 ufs_strategy(ap)
1779 	struct vop_strategy_args /* {
1780 		struct buf *a_bp;
1781 	} */ *ap;
1782 {
1783 	register struct buf *bp = ap->a_bp;
1784 	register struct vnode *vp = bp->b_vp;
1785 	register struct inode *ip;
1786 	int error;
1787 
1788 	ip = VTOI(vp);
1789 	if (vp->v_type == VBLK || vp->v_type == VCHR)
1790 		panic("ufs_strategy: spec");
1791 	if (bp->b_blkno == bp->b_lblkno) {
1792 		if (error =
1793 		    VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL)) {
1794 			bp->b_error = error;
1795 			bp->b_flags |= B_ERROR;
1796 			biodone(bp);
1797 			return (error);
1798 		}
1799 		if ((long)bp->b_blkno == -1)
1800 			clrbuf(bp);
1801 	}
1802 	if ((long)bp->b_blkno == -1) {
1803 		biodone(bp);
1804 		return (0);
1805 	}
1806 	vp = ip->i_devvp;
1807 	bp->b_dev = vp->v_rdev;
1808 	VOCALL (vp->v_op, VOFFSET(vop_strategy), ap);
1809 	return (0);
1810 }
1811 
1812 /*
1813  * Print out the contents of an inode.
1814  */
1815 int
1816 ufs_print(ap)
1817 	struct vop_print_args /* {
1818 		struct vnode *a_vp;
1819 	} */ *ap;
1820 {
1821 	register struct vnode *vp = ap->a_vp;
1822 	register struct inode *ip = VTOI(vp);
1823 
1824 	printf("tag VT_UFS, ino %d, on dev %d, %d", ip->i_number,
1825 		major(ip->i_dev), minor(ip->i_dev));
1826 #ifdef FIFO
1827 	if (vp->v_type == VFIFO)
1828 		fifo_printinfo(vp);
1829 #endif /* FIFO */
1830 	printf("%s\n", (ip->i_flag & IN_LOCKED) ? " (LOCKED)" : "");
1831 	if (ip->i_lockholder == 0)
1832 		return (0);
1833 	printf("\towner pid %d", ip->i_lockholder);
1834 	if (ip->i_lockwaiter)
1835 		printf(" waiting pid %d", ip->i_lockwaiter);
1836 	printf("\n");
1837 	return (0);
1838 }
1839 
1840 /*
1841  * Read wrapper for special devices.
1842  */
1843 int
1844 ufsspec_read(ap)
1845 	struct vop_read_args /* {
1846 		struct vnode *a_vp;
1847 		struct uio *a_uio;
1848 		int  a_ioflag;
1849 		struct ucred *a_cred;
1850 	} */ *ap;
1851 {
1852 
1853 	/*
1854 	 * Set access flag.
1855 	 */
1856 	VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
1857 	return (VOCALL (spec_vnodeop_p, VOFFSET(vop_read), ap));
1858 }
1859 
1860 /*
1861  * Write wrapper for special devices.
1862  */
1863 int
1864 ufsspec_write(ap)
1865 	struct vop_write_args /* {
1866 		struct vnode *a_vp;
1867 		struct uio *a_uio;
1868 		int  a_ioflag;
1869 		struct ucred *a_cred;
1870 	} */ *ap;
1871 {
1872 
1873 	/*
1874 	 * Set update and change flags.
1875 	 */
1876 	VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1877 	return (VOCALL (spec_vnodeop_p, VOFFSET(vop_write), ap));
1878 }
1879 
1880 /*
1881  * Close wrapper for special devices.
1882  *
1883  * Update the times on the inode then do device close.
1884  */
1885 int
1886 ufsspec_close(ap)
1887 	struct vop_close_args /* {
1888 		struct vnode *a_vp;
1889 		int  a_fflag;
1890 		struct ucred *a_cred;
1891 		struct proc *a_p;
1892 	} */ *ap;
1893 {
1894 	register struct inode *ip = VTOI(ap->a_vp);
1895 
1896 	if (ap->a_vp->v_usecount > 1 && !(ip->i_flag & IN_LOCKED))
1897 		ITIMES(ip, &time, &time);
1898 	return (VOCALL (spec_vnodeop_p, VOFFSET(vop_close), ap));
1899 }
1900 
1901 #ifdef FIFO
1902 /*
1903  * Read wrapper for fifo's
1904  */
1905 int
1906 ufsfifo_read(ap)
1907 	struct vop_read_args /* {
1908 		struct vnode *a_vp;
1909 		struct uio *a_uio;
1910 		int  a_ioflag;
1911 		struct ucred *a_cred;
1912 	} */ *ap;
1913 {
1914 	extern int (**fifo_vnodeop_p)();
1915 
1916 	/*
1917 	 * Set access flag.
1918 	 */
1919 	VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
1920 	return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_read), ap));
1921 }
1922 
1923 /*
1924  * Write wrapper for fifo's.
1925  */
1926 int
1927 ufsfifo_write(ap)
1928 	struct vop_write_args /* {
1929 		struct vnode *a_vp;
1930 		struct uio *a_uio;
1931 		int  a_ioflag;
1932 		struct ucred *a_cred;
1933 	} */ *ap;
1934 {
1935 	extern int (**fifo_vnodeop_p)();
1936 
1937 	/*
1938 	 * Set update and change flags.
1939 	 */
1940 	VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1941 	return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_write), ap));
1942 }
1943 
1944 /*
1945  * Close wrapper for fifo's.
1946  *
1947  * Update the times on the inode then do device close.
1948  */
1949 ufsfifo_close(ap)
1950 	struct vop_close_args /* {
1951 		struct vnode *a_vp;
1952 		int  a_fflag;
1953 		struct ucred *a_cred;
1954 		struct proc *a_p;
1955 	} */ *ap;
1956 {
1957 	extern int (**fifo_vnodeop_p)();
1958 	register struct inode *ip = VTOI(ap->a_vp);
1959 
1960 	if (ap->a_vp->v_usecount > 1 && !(ip->i_flag & IN_LOCKED))
1961 		ITIMES(ip, &time, &time);
1962 	return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_close), ap));
1963 }
1964 #endif /* FIFO */
1965 
1966 /*
1967  * Return POSIX pathconf information applicable to ufs filesystems.
1968  */
1969 ufs_pathconf(ap)
1970 	struct vop_pathconf_args /* {
1971 		struct vnode *a_vp;
1972 		int a_name;
1973 		int *a_retval;
1974 	} */ *ap;
1975 {
1976 
1977 	switch (ap->a_name) {
1978 	case _PC_LINK_MAX:
1979 		*ap->a_retval = LINK_MAX;
1980 		return (0);
1981 	case _PC_NAME_MAX:
1982 		*ap->a_retval = NAME_MAX;
1983 		return (0);
1984 	case _PC_PATH_MAX:
1985 		*ap->a_retval = PATH_MAX;
1986 		return (0);
1987 	case _PC_PIPE_BUF:
1988 		*ap->a_retval = PIPE_BUF;
1989 		return (0);
1990 	case _PC_CHOWN_RESTRICTED:
1991 		*ap->a_retval = 1;
1992 		return (0);
1993 	case _PC_NO_TRUNC:
1994 		*ap->a_retval = 1;
1995 		return (0);
1996 	default:
1997 		return (EINVAL);
1998 	}
1999 	/* NOTREACHED */
2000 }
2001 
2002 /*
2003  * Advisory record locking support
2004  */
2005 int
2006 ufs_advlock(ap)
2007 	struct vop_advlock_args /* {
2008 		struct vnode *a_vp;
2009 		caddr_t  a_id;
2010 		int  a_op;
2011 		struct flock *a_fl;
2012 		int  a_flags;
2013 	} */ *ap;
2014 {
2015 	register struct inode *ip = VTOI(ap->a_vp);
2016 	register struct flock *fl = ap->a_fl;
2017 	register struct lockf *lock;
2018 	off_t start, end;
2019 	int error;
2020 
2021 	/*
2022 	 * Avoid the common case of unlocking when inode has no locks.
2023 	 */
2024 	if (ip->i_lockf == (struct lockf *)0) {
2025 		if (ap->a_op != F_SETLK) {
2026 			fl->l_type = F_UNLCK;
2027 			return (0);
2028 		}
2029 	}
2030 	/*
2031 	 * Convert the flock structure into a start and end.
2032 	 */
2033 	switch (fl->l_whence) {
2034 
2035 	case SEEK_SET:
2036 	case SEEK_CUR:
2037 		/*
2038 		 * Caller is responsible for adding any necessary offset
2039 		 * when SEEK_CUR is used.
2040 		 */
2041 		start = fl->l_start;
2042 		break;
2043 
2044 	case SEEK_END:
2045 		start = ip->i_size + fl->l_start;
2046 		break;
2047 
2048 	default:
2049 		return (EINVAL);
2050 	}
2051 	if (start < 0)
2052 		return (EINVAL);
2053 	if (fl->l_len == 0)
2054 		end = -1;
2055 	else
2056 		end = start + fl->l_len - 1;
2057 	/*
2058 	 * Create the lockf structure
2059 	 */
2060 	MALLOC(lock, struct lockf *, sizeof *lock, M_LOCKF, M_WAITOK);
2061 	lock->lf_start = start;
2062 	lock->lf_end = end;
2063 	lock->lf_id = ap->a_id;
2064 	lock->lf_inode = ip;
2065 	lock->lf_type = fl->l_type;
2066 	lock->lf_next = (struct lockf *)0;
2067 	TAILQ_INIT(&lock->lf_blkhd);
2068 	lock->lf_flags = ap->a_flags;
2069 	/*
2070 	 * Do the requested operation.
2071 	 */
2072 	switch(ap->a_op) {
2073 	case F_SETLK:
2074 		return (lf_setlock(lock));
2075 
2076 	case F_UNLCK:
2077 		error = lf_clearlock(lock);
2078 		FREE(lock, M_LOCKF);
2079 		return (error);
2080 
2081 	case F_GETLK:
2082 		error = lf_getlock(lock, fl);
2083 		FREE(lock, M_LOCKF);
2084 		return (error);
2085 
2086 	default:
2087 		free(lock, M_LOCKF);
2088 		return (EINVAL);
2089 	}
2090 	/* NOTREACHED */
2091 }
2092 
2093 /*
2094  * Initialize the vnode associated with a new inode, handle aliased
2095  * vnodes.
2096  */
2097 int
2098 ufs_vinit(mntp, specops, fifoops, vpp)
2099 	struct mount *mntp;
2100 	int (**specops)();
2101 	int (**fifoops)();
2102 	struct vnode **vpp;
2103 {
2104 	struct inode *ip;
2105 	struct vnode *vp, *nvp;
2106 
2107 	vp = *vpp;
2108 	ip = VTOI(vp);
2109 	switch(vp->v_type = IFTOVT(ip->i_mode)) {
2110 	case VCHR:
2111 	case VBLK:
2112 		vp->v_op = specops;
2113 		if (nvp = checkalias(vp, ip->i_rdev, mntp)) {
2114 			/*
2115 			 * Discard unneeded vnode, but save its inode.
2116 			 */
2117 			ufs_ihashrem(ip);
2118 			VOP_UNLOCK(vp);
2119 			nvp->v_data = vp->v_data;
2120 			vp->v_data = NULL;
2121 			vp->v_op = spec_vnodeop_p;
2122 			vrele(vp);
2123 			vgone(vp);
2124 			/*
2125 			 * Reinitialize aliased inode.
2126 			 */
2127 			vp = nvp;
2128 			ip->i_vnode = vp;
2129 			ufs_ihashins(ip);
2130 		}
2131 		break;
2132 	case VFIFO:
2133 #ifdef FIFO
2134 		vp->v_op = fifoops;
2135 		break;
2136 #else
2137 		return (EOPNOTSUPP);
2138 #endif
2139 	}
2140 	if (ip->i_number == ROOTINO)
2141                 vp->v_flag |= VROOT;
2142 	/*
2143 	 * Initialize modrev times
2144 	 */
2145 	SETHIGH(ip->i_modrev, mono_time.tv_sec);
2146 	SETLOW(ip->i_modrev, mono_time.tv_usec * 4294);
2147 	*vpp = vp;
2148 	return (0);
2149 }
2150 
2151 /*
2152  * Allocate a new inode.
2153  */
2154 int
2155 ufs_makeinode(mode, dvp, vpp, cnp)
2156 	int mode;
2157 	struct vnode *dvp;
2158 	struct vnode **vpp;
2159 	struct componentname *cnp;
2160 {
2161 	register struct inode *ip, *pdir;
2162 	struct timeval tv;
2163 	struct vnode *tvp;
2164 	int error;
2165 
2166 	pdir = VTOI(dvp);
2167 #ifdef DIAGNOSTIC
2168 	if ((cnp->cn_flags & HASBUF) == 0)
2169 		panic("ufs_makeinode: no name");
2170 #endif
2171 	*vpp = NULL;
2172 	if ((mode & IFMT) == 0)
2173 		mode |= IFREG;
2174 
2175 	if (error = VOP_VALLOC(dvp, mode, cnp->cn_cred, &tvp)) {
2176 		free(cnp->cn_pnbuf, M_NAMEI);
2177 		vput(dvp);
2178 		return (error);
2179 	}
2180 	ip = VTOI(tvp);
2181 	ip->i_gid = pdir->i_gid;
2182 	if ((mode & IFMT) == IFLNK)
2183 		ip->i_uid = pdir->i_uid;
2184 	else
2185 		ip->i_uid = cnp->cn_cred->cr_uid;
2186 #ifdef QUOTA
2187 	if ((error = getinoquota(ip)) ||
2188 	    (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
2189 		free(cnp->cn_pnbuf, M_NAMEI);
2190 		VOP_VFREE(tvp, ip->i_number, mode);
2191 		vput(tvp);
2192 		vput(dvp);
2193 		return (error);
2194 	}
2195 #endif
2196 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
2197 	ip->i_mode = mode;
2198 	tvp->v_type = IFTOVT(mode);	/* Rest init'd in getnewvnode(). */
2199 	ip->i_nlink = 1;
2200 	if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
2201 	    suser(cnp->cn_cred, NULL))
2202 		ip->i_mode &= ~ISGID;
2203 
2204 	if (cnp->cn_flags & ISWHITEOUT)
2205 		ip->i_flags |= UF_OPAQUE;
2206 
2207 	/*
2208 	 * Make sure inode goes to disk before directory entry.
2209 	 */
2210 	tv = time;
2211 	if (error = VOP_UPDATE(tvp, &tv, &tv, 1))
2212 		goto bad;
2213 	if (error = ufs_direnter(ip, dvp, cnp))
2214 		goto bad;
2215 	if ((cnp->cn_flags & SAVESTART) == 0)
2216 		FREE(cnp->cn_pnbuf, M_NAMEI);
2217 	vput(dvp);
2218 	*vpp = tvp;
2219 	return (0);
2220 
2221 bad:
2222 	/*
2223 	 * Write error occurred trying to update the inode
2224 	 * or the directory so must deallocate the inode.
2225 	 */
2226 	free(cnp->cn_pnbuf, M_NAMEI);
2227 	vput(dvp);
2228 	ip->i_nlink = 0;
2229 	ip->i_flag |= IN_CHANGE;
2230 	vput(tvp);
2231 	return (error);
2232 }
2233