xref: /original-bsd/sys/ufs/ufs/ufs_vnops.c (revision 7e21a27b)
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.23 (Berkeley) 04/03/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((u_quad_t)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 		int *ncookies;
1554 		u_long **a_cookies;
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 		struct dirent *dp, *dpstart;
1614 		off_t offstart;
1615 		u_long *cookies;
1616 		int ncookies;
1617 
1618 		/*
1619 		 * Only the NFS server uses cookies, and it loads the
1620 		 * directory block into system space, so we can just look at
1621 		 * it directly.
1622 		 */
1623 		if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1624 			panic("ufs_readdir: lost in space");
1625 		dpstart = (struct dirent *)
1626 		     (uio->uio_iov->iov_base - (uio->uio_offset - off));
1627 		offstart = off;
1628 		for (dp = dpstart, ncookies = 0; off < uio->uio_offset; ) {
1629 			if (dp->d_reclen == 0)
1630 				break;
1631 			off += dp->d_reclen;
1632 			ncookies++;
1633 			dp = (struct dirent *)((caddr_t)dp + dp->d_reclen);
1634 		}
1635 		lost += uio->uio_offset - off;
1636 		uio->uio_offset = off;
1637 		MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1638 		    M_WAITOK);
1639 		*ap->a_ncookies = ncookies;
1640 		*ap->a_cookies = cookies;
1641 		for (off = offstart, dp = dpstart; off < uio->uio_offset; ) {
1642 			*(cookies++) = off;
1643 			off += dp->d_reclen;
1644 			dp = (struct dirent *)((caddr_t)dp + dp->d_reclen);
1645 		}
1646 	}
1647 	uio->uio_resid += lost;
1648 	*ap->a_eofflag = VTOI(ap->a_vp)->i_size <= uio->uio_offset;
1649 	return (error);
1650 }
1651 
1652 /*
1653  * Return target name of a symbolic link
1654  */
1655 int
1656 ufs_readlink(ap)
1657 	struct vop_readlink_args /* {
1658 		struct vnode *a_vp;
1659 		struct uio *a_uio;
1660 		struct ucred *a_cred;
1661 	} */ *ap;
1662 {
1663 	register struct vnode *vp = ap->a_vp;
1664 	register struct inode *ip = VTOI(vp);
1665 	int isize;
1666 
1667 	isize = ip->i_size;
1668 	if (isize < vp->v_mount->mnt_maxsymlinklen) {
1669 		uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1670 		return (0);
1671 	}
1672 	return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1673 }
1674 
1675 /*
1676  * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually
1677  * done. If a buffer has been saved in anticipation of a CREATE, delete it.
1678  */
1679 /* ARGSUSED */
1680 int
1681 ufs_abortop(ap)
1682 	struct vop_abortop_args /* {
1683 		struct vnode *a_dvp;
1684 		struct componentname *a_cnp;
1685 	} */ *ap;
1686 {
1687 	if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
1688 		FREE(ap->a_cnp->cn_pnbuf, M_NAMEI);
1689 	return (0);
1690 }
1691 
1692 /*
1693  * Lock an inode. If its already locked, set the WANT bit and sleep.
1694  */
1695 int
1696 ufs_lock(ap)
1697 	struct vop_lock_args /* {
1698 		struct vnode *a_vp;
1699 	} */ *ap;
1700 {
1701 	register struct vnode *vp = ap->a_vp;
1702 	register struct inode *ip;
1703 	struct proc *p = curproc;	/* XXX */
1704 
1705 start:
1706 	while (vp->v_flag & VXLOCK) {
1707 		vp->v_flag |= VXWANT;
1708 		sleep((caddr_t)vp, PINOD);
1709 	}
1710 	if (vp->v_tag == VT_NON)
1711 		return (ENOENT);
1712 	ip = VTOI(vp);
1713 	if (ip->i_flag & IN_LOCKED) {
1714 		ip->i_flag |= IN_WANTED;
1715 #ifdef DIAGNOSTIC
1716 		if (p) {
1717 			if (p->p_pid == ip->i_lockholder)
1718 				panic("locking against myself");
1719 			ip->i_lockwaiter = p->p_pid;
1720 		} else
1721 			ip->i_lockwaiter = -1;
1722 #endif
1723 		(void) sleep((caddr_t)ip, PINOD);
1724 		goto start;
1725 	}
1726 #ifdef DIAGNOSTIC
1727 	ip->i_lockwaiter = 0;
1728 	if (ip->i_lockholder != 0)
1729 		panic("lockholder (%d) != 0", ip->i_lockholder);
1730 	if (p && p->p_pid == 0)
1731 		printf("locking by process 0\n");
1732 	if (p)
1733 		ip->i_lockholder = p->p_pid;
1734 	else
1735 		ip->i_lockholder = -1;
1736 #endif
1737 	ip->i_flag |= IN_LOCKED;
1738 	return (0);
1739 }
1740 
1741 /*
1742  * Unlock an inode.  If WANT bit is on, wakeup.
1743  */
1744 int lockcount = 90;
1745 int
1746 ufs_unlock(ap)
1747 	struct vop_unlock_args /* {
1748 		struct vnode *a_vp;
1749 	} */ *ap;
1750 {
1751 	register struct inode *ip = VTOI(ap->a_vp);
1752 	struct proc *p = curproc;	/* XXX */
1753 
1754 #ifdef DIAGNOSTIC
1755 	if ((ip->i_flag & IN_LOCKED) == 0) {
1756 		vprint("ufs_unlock: unlocked inode", ap->a_vp);
1757 		panic("ufs_unlock NOT LOCKED");
1758 	}
1759 	if (p && p->p_pid != ip->i_lockholder && p->p_pid > -1 &&
1760 	    ip->i_lockholder > -1 && lockcount++ < 100)
1761 		panic("unlocker (%d) != lock holder (%d)",
1762 		    p->p_pid, ip->i_lockholder);
1763 	ip->i_lockholder = 0;
1764 #endif
1765 	ip->i_flag &= ~IN_LOCKED;
1766 	if (ip->i_flag & IN_WANTED) {
1767 		ip->i_flag &= ~IN_WANTED;
1768 		wakeup((caddr_t)ip);
1769 	}
1770 	return (0);
1771 }
1772 
1773 /*
1774  * Check for a locked inode.
1775  */
1776 int
1777 ufs_islocked(ap)
1778 	struct vop_islocked_args /* {
1779 		struct vnode *a_vp;
1780 	} */ *ap;
1781 {
1782 
1783 	if (VTOI(ap->a_vp)->i_flag & IN_LOCKED)
1784 		return (1);
1785 	return (0);
1786 }
1787 
1788 /*
1789  * Calculate the logical to physical mapping if not done already,
1790  * then call the device strategy routine.
1791  */
1792 int
1793 ufs_strategy(ap)
1794 	struct vop_strategy_args /* {
1795 		struct buf *a_bp;
1796 	} */ *ap;
1797 {
1798 	register struct buf *bp = ap->a_bp;
1799 	register struct vnode *vp = bp->b_vp;
1800 	register struct inode *ip;
1801 	int error;
1802 
1803 	ip = VTOI(vp);
1804 	if (vp->v_type == VBLK || vp->v_type == VCHR)
1805 		panic("ufs_strategy: spec");
1806 	if (bp->b_blkno == bp->b_lblkno) {
1807 		if (error =
1808 		    VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL)) {
1809 			bp->b_error = error;
1810 			bp->b_flags |= B_ERROR;
1811 			biodone(bp);
1812 			return (error);
1813 		}
1814 		if ((long)bp->b_blkno == -1)
1815 			clrbuf(bp);
1816 	}
1817 	if ((long)bp->b_blkno == -1) {
1818 		biodone(bp);
1819 		return (0);
1820 	}
1821 	vp = ip->i_devvp;
1822 	bp->b_dev = vp->v_rdev;
1823 	VOCALL (vp->v_op, VOFFSET(vop_strategy), ap);
1824 	return (0);
1825 }
1826 
1827 /*
1828  * Print out the contents of an inode.
1829  */
1830 int
1831 ufs_print(ap)
1832 	struct vop_print_args /* {
1833 		struct vnode *a_vp;
1834 	} */ *ap;
1835 {
1836 	register struct vnode *vp = ap->a_vp;
1837 	register struct inode *ip = VTOI(vp);
1838 
1839 	printf("tag VT_UFS, ino %d, on dev %d, %d", ip->i_number,
1840 		major(ip->i_dev), minor(ip->i_dev));
1841 #ifdef FIFO
1842 	if (vp->v_type == VFIFO)
1843 		fifo_printinfo(vp);
1844 #endif /* FIFO */
1845 	printf("%s\n", (ip->i_flag & IN_LOCKED) ? " (LOCKED)" : "");
1846 	if (ip->i_lockholder == 0)
1847 		return (0);
1848 	printf("\towner pid %d", ip->i_lockholder);
1849 	if (ip->i_lockwaiter)
1850 		printf(" waiting pid %d", ip->i_lockwaiter);
1851 	printf("\n");
1852 	return (0);
1853 }
1854 
1855 /*
1856  * Read wrapper for special devices.
1857  */
1858 int
1859 ufsspec_read(ap)
1860 	struct vop_read_args /* {
1861 		struct vnode *a_vp;
1862 		struct uio *a_uio;
1863 		int  a_ioflag;
1864 		struct ucred *a_cred;
1865 	} */ *ap;
1866 {
1867 
1868 	/*
1869 	 * Set access flag.
1870 	 */
1871 	VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
1872 	return (VOCALL (spec_vnodeop_p, VOFFSET(vop_read), ap));
1873 }
1874 
1875 /*
1876  * Write wrapper for special devices.
1877  */
1878 int
1879 ufsspec_write(ap)
1880 	struct vop_write_args /* {
1881 		struct vnode *a_vp;
1882 		struct uio *a_uio;
1883 		int  a_ioflag;
1884 		struct ucred *a_cred;
1885 	} */ *ap;
1886 {
1887 
1888 	/*
1889 	 * Set update and change flags.
1890 	 */
1891 	VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1892 	return (VOCALL (spec_vnodeop_p, VOFFSET(vop_write), ap));
1893 }
1894 
1895 /*
1896  * Close wrapper for special devices.
1897  *
1898  * Update the times on the inode then do device close.
1899  */
1900 int
1901 ufsspec_close(ap)
1902 	struct vop_close_args /* {
1903 		struct vnode *a_vp;
1904 		int  a_fflag;
1905 		struct ucred *a_cred;
1906 		struct proc *a_p;
1907 	} */ *ap;
1908 {
1909 	register struct inode *ip = VTOI(ap->a_vp);
1910 
1911 	if (ap->a_vp->v_usecount > 1 && !(ip->i_flag & IN_LOCKED))
1912 		ITIMES(ip, &time, &time);
1913 	return (VOCALL (spec_vnodeop_p, VOFFSET(vop_close), ap));
1914 }
1915 
1916 #ifdef FIFO
1917 /*
1918  * Read wrapper for fifo's
1919  */
1920 int
1921 ufsfifo_read(ap)
1922 	struct vop_read_args /* {
1923 		struct vnode *a_vp;
1924 		struct uio *a_uio;
1925 		int  a_ioflag;
1926 		struct ucred *a_cred;
1927 	} */ *ap;
1928 {
1929 	extern int (**fifo_vnodeop_p)();
1930 
1931 	/*
1932 	 * Set access flag.
1933 	 */
1934 	VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
1935 	return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_read), ap));
1936 }
1937 
1938 /*
1939  * Write wrapper for fifo's.
1940  */
1941 int
1942 ufsfifo_write(ap)
1943 	struct vop_write_args /* {
1944 		struct vnode *a_vp;
1945 		struct uio *a_uio;
1946 		int  a_ioflag;
1947 		struct ucred *a_cred;
1948 	} */ *ap;
1949 {
1950 	extern int (**fifo_vnodeop_p)();
1951 
1952 	/*
1953 	 * Set update and change flags.
1954 	 */
1955 	VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1956 	return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_write), ap));
1957 }
1958 
1959 /*
1960  * Close wrapper for fifo's.
1961  *
1962  * Update the times on the inode then do device close.
1963  */
1964 ufsfifo_close(ap)
1965 	struct vop_close_args /* {
1966 		struct vnode *a_vp;
1967 		int  a_fflag;
1968 		struct ucred *a_cred;
1969 		struct proc *a_p;
1970 	} */ *ap;
1971 {
1972 	extern int (**fifo_vnodeop_p)();
1973 	register struct inode *ip = VTOI(ap->a_vp);
1974 
1975 	if (ap->a_vp->v_usecount > 1 && !(ip->i_flag & IN_LOCKED))
1976 		ITIMES(ip, &time, &time);
1977 	return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_close), ap));
1978 }
1979 #endif /* FIFO */
1980 
1981 /*
1982  * Return POSIX pathconf information applicable to ufs filesystems.
1983  */
1984 ufs_pathconf(ap)
1985 	struct vop_pathconf_args /* {
1986 		struct vnode *a_vp;
1987 		int a_name;
1988 		int *a_retval;
1989 	} */ *ap;
1990 {
1991 
1992 	switch (ap->a_name) {
1993 	case _PC_LINK_MAX:
1994 		*ap->a_retval = LINK_MAX;
1995 		return (0);
1996 	case _PC_NAME_MAX:
1997 		*ap->a_retval = NAME_MAX;
1998 		return (0);
1999 	case _PC_PATH_MAX:
2000 		*ap->a_retval = PATH_MAX;
2001 		return (0);
2002 	case _PC_PIPE_BUF:
2003 		*ap->a_retval = PIPE_BUF;
2004 		return (0);
2005 	case _PC_CHOWN_RESTRICTED:
2006 		*ap->a_retval = 1;
2007 		return (0);
2008 	case _PC_NO_TRUNC:
2009 		*ap->a_retval = 1;
2010 		return (0);
2011 	default:
2012 		return (EINVAL);
2013 	}
2014 	/* NOTREACHED */
2015 }
2016 
2017 /*
2018  * Advisory record locking support
2019  */
2020 int
2021 ufs_advlock(ap)
2022 	struct vop_advlock_args /* {
2023 		struct vnode *a_vp;
2024 		caddr_t  a_id;
2025 		int  a_op;
2026 		struct flock *a_fl;
2027 		int  a_flags;
2028 	} */ *ap;
2029 {
2030 	register struct inode *ip = VTOI(ap->a_vp);
2031 	register struct flock *fl = ap->a_fl;
2032 	register struct lockf *lock;
2033 	off_t start, end;
2034 	int error;
2035 
2036 	/*
2037 	 * Avoid the common case of unlocking when inode has no locks.
2038 	 */
2039 	if (ip->i_lockf == (struct lockf *)0) {
2040 		if (ap->a_op != F_SETLK) {
2041 			fl->l_type = F_UNLCK;
2042 			return (0);
2043 		}
2044 	}
2045 	/*
2046 	 * Convert the flock structure into a start and end.
2047 	 */
2048 	switch (fl->l_whence) {
2049 
2050 	case SEEK_SET:
2051 	case SEEK_CUR:
2052 		/*
2053 		 * Caller is responsible for adding any necessary offset
2054 		 * when SEEK_CUR is used.
2055 		 */
2056 		start = fl->l_start;
2057 		break;
2058 
2059 	case SEEK_END:
2060 		start = ip->i_size + fl->l_start;
2061 		break;
2062 
2063 	default:
2064 		return (EINVAL);
2065 	}
2066 	if (start < 0)
2067 		return (EINVAL);
2068 	if (fl->l_len == 0)
2069 		end = -1;
2070 	else
2071 		end = start + fl->l_len - 1;
2072 	/*
2073 	 * Create the lockf structure
2074 	 */
2075 	MALLOC(lock, struct lockf *, sizeof *lock, M_LOCKF, M_WAITOK);
2076 	lock->lf_start = start;
2077 	lock->lf_end = end;
2078 	lock->lf_id = ap->a_id;
2079 	lock->lf_inode = ip;
2080 	lock->lf_type = fl->l_type;
2081 	lock->lf_next = (struct lockf *)0;
2082 	TAILQ_INIT(&lock->lf_blkhd);
2083 	lock->lf_flags = ap->a_flags;
2084 	/*
2085 	 * Do the requested operation.
2086 	 */
2087 	switch(ap->a_op) {
2088 	case F_SETLK:
2089 		return (lf_setlock(lock));
2090 
2091 	case F_UNLCK:
2092 		error = lf_clearlock(lock);
2093 		FREE(lock, M_LOCKF);
2094 		return (error);
2095 
2096 	case F_GETLK:
2097 		error = lf_getlock(lock, fl);
2098 		FREE(lock, M_LOCKF);
2099 		return (error);
2100 
2101 	default:
2102 		free(lock, M_LOCKF);
2103 		return (EINVAL);
2104 	}
2105 	/* NOTREACHED */
2106 }
2107 
2108 /*
2109  * Initialize the vnode associated with a new inode, handle aliased
2110  * vnodes.
2111  */
2112 int
2113 ufs_vinit(mntp, specops, fifoops, vpp)
2114 	struct mount *mntp;
2115 	int (**specops)();
2116 	int (**fifoops)();
2117 	struct vnode **vpp;
2118 {
2119 	struct inode *ip;
2120 	struct vnode *vp, *nvp;
2121 
2122 	vp = *vpp;
2123 	ip = VTOI(vp);
2124 	switch(vp->v_type = IFTOVT(ip->i_mode)) {
2125 	case VCHR:
2126 	case VBLK:
2127 		vp->v_op = specops;
2128 		if (nvp = checkalias(vp, ip->i_rdev, mntp)) {
2129 			/*
2130 			 * Discard unneeded vnode, but save its inode.
2131 			 */
2132 			ufs_ihashrem(ip);
2133 			VOP_UNLOCK(vp);
2134 			nvp->v_data = vp->v_data;
2135 			vp->v_data = NULL;
2136 			vp->v_op = spec_vnodeop_p;
2137 			vrele(vp);
2138 			vgone(vp);
2139 			/*
2140 			 * Reinitialize aliased inode.
2141 			 */
2142 			vp = nvp;
2143 			ip->i_vnode = vp;
2144 			ufs_ihashins(ip);
2145 		}
2146 		break;
2147 	case VFIFO:
2148 #ifdef FIFO
2149 		vp->v_op = fifoops;
2150 		break;
2151 #else
2152 		return (EOPNOTSUPP);
2153 #endif
2154 	}
2155 	if (ip->i_number == ROOTINO)
2156                 vp->v_flag |= VROOT;
2157 	/*
2158 	 * Initialize modrev times
2159 	 */
2160 	SETHIGH(ip->i_modrev, mono_time.tv_sec);
2161 	SETLOW(ip->i_modrev, mono_time.tv_usec * 4294);
2162 	*vpp = vp;
2163 	return (0);
2164 }
2165 
2166 /*
2167  * Allocate a new inode.
2168  */
2169 int
2170 ufs_makeinode(mode, dvp, vpp, cnp)
2171 	int mode;
2172 	struct vnode *dvp;
2173 	struct vnode **vpp;
2174 	struct componentname *cnp;
2175 {
2176 	register struct inode *ip, *pdir;
2177 	struct timeval tv;
2178 	struct vnode *tvp;
2179 	int error;
2180 
2181 	pdir = VTOI(dvp);
2182 #ifdef DIAGNOSTIC
2183 	if ((cnp->cn_flags & HASBUF) == 0)
2184 		panic("ufs_makeinode: no name");
2185 #endif
2186 	*vpp = NULL;
2187 	if ((mode & IFMT) == 0)
2188 		mode |= IFREG;
2189 
2190 	if (error = VOP_VALLOC(dvp, mode, cnp->cn_cred, &tvp)) {
2191 		free(cnp->cn_pnbuf, M_NAMEI);
2192 		vput(dvp);
2193 		return (error);
2194 	}
2195 	ip = VTOI(tvp);
2196 	ip->i_gid = pdir->i_gid;
2197 	if ((mode & IFMT) == IFLNK)
2198 		ip->i_uid = pdir->i_uid;
2199 	else
2200 		ip->i_uid = cnp->cn_cred->cr_uid;
2201 #ifdef QUOTA
2202 	if ((error = getinoquota(ip)) ||
2203 	    (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
2204 		free(cnp->cn_pnbuf, M_NAMEI);
2205 		VOP_VFREE(tvp, ip->i_number, mode);
2206 		vput(tvp);
2207 		vput(dvp);
2208 		return (error);
2209 	}
2210 #endif
2211 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
2212 	ip->i_mode = mode;
2213 	tvp->v_type = IFTOVT(mode);	/* Rest init'd in getnewvnode(). */
2214 	ip->i_nlink = 1;
2215 	if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
2216 	    suser(cnp->cn_cred, NULL))
2217 		ip->i_mode &= ~ISGID;
2218 
2219 	if (cnp->cn_flags & ISWHITEOUT)
2220 		ip->i_flags |= UF_OPAQUE;
2221 
2222 	/*
2223 	 * Make sure inode goes to disk before directory entry.
2224 	 */
2225 	tv = time;
2226 	if (error = VOP_UPDATE(tvp, &tv, &tv, 1))
2227 		goto bad;
2228 	if (error = ufs_direnter(ip, dvp, cnp))
2229 		goto bad;
2230 	if ((cnp->cn_flags & SAVESTART) == 0)
2231 		FREE(cnp->cn_pnbuf, M_NAMEI);
2232 	vput(dvp);
2233 	*vpp = tvp;
2234 	return (0);
2235 
2236 bad:
2237 	/*
2238 	 * Write error occurred trying to update the inode
2239 	 * or the directory so must deallocate the inode.
2240 	 */
2241 	free(cnp->cn_pnbuf, M_NAMEI);
2242 	vput(dvp);
2243 	ip->i_nlink = 0;
2244 	ip->i_flag |= IN_CHANGE;
2245 	vput(tvp);
2246 	return (error);
2247 }
2248