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