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