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