xref: /original-bsd/sys/ufs/lfs/lfs_vnops.c (revision 655593d0)
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  *	@(#)lfs_vnops.c	7.60 (Berkeley) 04/16/91
8  */
9 
10 #include "param.h"
11 #include "systm.h"
12 #include "namei.h"
13 #include "resourcevar.h"
14 #include "kernel.h"
15 #include "file.h"
16 #include "stat.h"
17 #include "buf.h"
18 #include "proc.h"
19 #include "conf.h"
20 #include "mount.h"
21 #include "vnode.h"
22 #include "specdev.h"
23 #include "fifo.h"
24 #include "malloc.h"
25 
26 #include "lockf.h"
27 #include "quota.h"
28 #include "inode.h"
29 #include "fs.h"
30 
31 /*
32  * Create a regular file
33  */
34 ufs_create(ndp, vap, p)
35 	struct nameidata *ndp;
36 	struct vattr *vap;
37 	struct proc *p;
38 {
39 	struct inode *ip;
40 	int error;
41 
42 	if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip))
43 		return (error);
44 	ndp->ni_vp = ITOV(ip);
45 	return (0);
46 }
47 
48 /*
49  * Mknod vnode call
50  */
51 /* ARGSUSED */
52 ufs_mknod(ndp, vap, cred, p)
53 	struct nameidata *ndp;
54 	struct ucred *cred;
55 	struct vattr *vap;
56 	struct proc *p;
57 {
58 	register struct vnode *vp;
59 	struct inode *ip;
60 	int error;
61 
62 	if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip))
63 		return (error);
64 	ip->i_flag |= IACC|IUPD|ICHG;
65 	if (vap->va_rdev != VNOVAL) {
66 		/*
67 		 * Want to be able to use this to make badblock
68 		 * inodes, so don't truncate the dev number.
69 		 */
70 		ip->i_rdev = vap->va_rdev;
71 	}
72 	/*
73 	 * Remove inode so that it will be reloaded by iget and
74 	 * checked to see if it is an alias of an existing entry
75 	 * in the inode cache.
76 	 */
77 	vp = ITOV(ip);
78 	vput(vp);
79 	vp->v_type = VNON;
80 	vgone(vp);
81 	return (0);
82 }
83 
84 /*
85  * Open called.
86  *
87  * Nothing to do.
88  */
89 /* ARGSUSED */
90 ufs_open(vp, mode, cred, p)
91 	struct vnode *vp;
92 	int mode;
93 	struct ucred *cred;
94 	struct proc *p;
95 {
96 
97 	return (0);
98 }
99 
100 /*
101  * Close called
102  *
103  * Update the times on the inode.
104  */
105 /* ARGSUSED */
106 ufs_close(vp, fflag, cred, p)
107 	struct vnode *vp;
108 	int fflag;
109 	struct ucred *cred;
110 	struct proc *p;
111 {
112 	register struct inode *ip = VTOI(vp);
113 
114 	if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED))
115 		ITIMES(ip, &time, &time);
116 	return (0);
117 }
118 
119 /*
120  * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
121  * The mode is shifted to select the owner/group/other fields. The
122  * super user is granted all permissions.
123  */
124 ufs_access(vp, mode, cred, p)
125 	struct vnode *vp;
126 	register int mode;
127 	struct ucred *cred;
128 	struct proc *p;
129 {
130 	register struct inode *ip = VTOI(vp);
131 	register gid_t *gp;
132 	int i, error;
133 
134 #ifdef DIAGNOSTIC
135 	if (!VOP_ISLOCKED(vp)) {
136 		vprint("ufs_access: not locked", vp);
137 		panic("ufs_access: not locked");
138 	}
139 #endif
140 #ifdef QUOTA
141 	if (mode & VWRITE) {
142 		switch (vp->v_type) {
143 		case VREG: case VDIR: case VLNK:
144 			if (error = getinoquota(ip))
145 				return (error);
146 		}
147 	}
148 #endif /* QUOTA */
149 	/*
150 	 * If you're the super-user, you always get access.
151 	 */
152 	if (cred->cr_uid == 0)
153 		return (0);
154 	/*
155 	 * Access check is based on only one of owner, group, public.
156 	 * If not owner, then check group. If not a member of the
157 	 * group, then check public access.
158 	 */
159 	if (cred->cr_uid != ip->i_uid) {
160 		mode >>= 3;
161 		gp = cred->cr_groups;
162 		for (i = 0; i < cred->cr_ngroups; i++, gp++)
163 			if (ip->i_gid == *gp)
164 				goto found;
165 		mode >>= 3;
166 found:
167 		;
168 	}
169 	if ((ip->i_mode & mode) != 0)
170 		return (0);
171 	return (EACCES);
172 }
173 
174 /* ARGSUSED */
175 ufs_getattr(vp, vap, cred, p)
176 	struct vnode *vp;
177 	register struct vattr *vap;
178 	struct ucred *cred;
179 	struct proc *p;
180 {
181 	register struct inode *ip = VTOI(vp);
182 
183 	ITIMES(ip, &time, &time);
184 	/*
185 	 * Copy from inode table
186 	 */
187 	vap->va_fsid = ip->i_dev;
188 	vap->va_fileid = ip->i_number;
189 	vap->va_mode = ip->i_mode & ~IFMT;
190 	vap->va_nlink = ip->i_nlink;
191 	vap->va_uid = ip->i_uid;
192 	vap->va_gid = ip->i_gid;
193 	vap->va_rdev = (dev_t)ip->i_rdev;
194 #ifdef tahoe
195 	vap->va_size = ip->i_size;
196 	vap->va_size_rsv = 0;
197 #else
198 	vap->va_qsize = ip->i_din.di_qsize;
199 #endif
200 	vap->va_atime.tv_sec = ip->i_atime;
201 	vap->va_atime.tv_usec = 0;
202 	vap->va_mtime.tv_sec = ip->i_mtime;
203 	vap->va_mtime.tv_usec = 0;
204 	vap->va_ctime.tv_sec = ip->i_ctime;
205 	vap->va_ctime.tv_usec = 0;
206 	vap->va_flags = ip->i_flags;
207 	vap->va_gen = ip->i_gen;
208 	/* this doesn't belong here */
209 	if (vp->v_type == VBLK)
210 		vap->va_blocksize = BLKDEV_IOSIZE;
211 	else if (vp->v_type == VCHR)
212 		vap->va_blocksize = MAXBSIZE;
213 	else
214 		vap->va_blocksize = ip->i_fs->fs_bsize;
215 	vap->va_bytes = dbtob(ip->i_blocks);
216 	vap->va_bytes_rsv = 0;
217 	vap->va_type = vp->v_type;
218 	return (0);
219 }
220 
221 /*
222  * Set attribute vnode op. called from several syscalls
223  */
224 ufs_setattr(vp, vap, cred, p)
225 	register struct vnode *vp;
226 	register struct vattr *vap;
227 	register struct ucred *cred;
228 	struct proc *p;
229 {
230 	register struct inode *ip = VTOI(vp);
231 	int error = 0;
232 
233 	/*
234 	 * Check for unsetable attributes.
235 	 */
236 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
237 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
238 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
239 	    ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
240 		return (EINVAL);
241 	}
242 	/*
243 	 * Go through the fields and update iff not VNOVAL.
244 	 */
245 	if (vap->va_uid != (u_short)VNOVAL || vap->va_gid != (u_short)VNOVAL)
246 		if (error = chown1(vp, vap->va_uid, vap->va_gid, p))
247 			return (error);
248 	if (vap->va_size != VNOVAL) {
249 		if (vp->v_type == VDIR)
250 			return (EISDIR);
251 		if (error = itrunc(ip, vap->va_size, 0)) /* XXX IO_SYNC? */
252 			return (error);
253 	}
254 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
255 		if (cred->cr_uid != ip->i_uid &&
256 		    (error = suser(cred, &p->p_acflag)))
257 			return (error);
258 		if (vap->va_atime.tv_sec != VNOVAL)
259 			ip->i_flag |= IACC;
260 		if (vap->va_mtime.tv_sec != VNOVAL)
261 			ip->i_flag |= IUPD;
262 		ip->i_flag |= ICHG;
263 		if (error = iupdat(ip, &vap->va_atime, &vap->va_mtime, 1))
264 			return (error);
265 	}
266 	if (vap->va_mode != (u_short)VNOVAL)
267 		error = chmod1(vp, (int)vap->va_mode, p);
268 	if (vap->va_flags != VNOVAL) {
269 		if (cred->cr_uid != ip->i_uid &&
270 		    (error = suser(cred, &p->p_acflag)))
271 			return (error);
272 		if (cred->cr_uid == 0) {
273 			ip->i_flags = vap->va_flags;
274 		} else {
275 			ip->i_flags &= 0xffff0000;
276 			ip->i_flags |= (vap->va_flags & 0xffff);
277 		}
278 		ip->i_flag |= ICHG;
279 	}
280 	return (error);
281 }
282 
283 /*
284  * Change the mode on a file.
285  * Inode must be locked before calling.
286  */
287 chmod1(vp, mode, p)
288 	register struct vnode *vp;
289 	register int mode;
290 	struct proc *p;
291 {
292 	register struct ucred *cred = p->p_ucred;
293 	register struct inode *ip = VTOI(vp);
294 	int error;
295 
296 	if (cred->cr_uid != ip->i_uid &&
297 	    (error = suser(cred, &p->p_acflag)))
298 		return (error);
299 	if (cred->cr_uid) {
300 		if (vp->v_type != VDIR && (mode & ISVTX))
301 			return (EFTYPE);
302 		if (!groupmember(ip->i_gid, cred) && (mode & ISGID))
303 			return (EPERM);
304 	}
305 	ip->i_mode &= ~07777;
306 	ip->i_mode |= mode & 07777;
307 	ip->i_flag |= ICHG;
308 	if ((vp->v_flag & VTEXT) && (ip->i_mode & ISVTX) == 0)
309 		(void) vnode_pager_uncache(vp);
310 	return (0);
311 }
312 
313 /*
314  * Perform chown operation on inode ip;
315  * inode must be locked prior to call.
316  */
317 chown1(vp, uid, gid, p)
318 	register struct vnode *vp;
319 	uid_t uid;
320 	gid_t gid;
321 	struct proc *p;
322 {
323 	register struct inode *ip = VTOI(vp);
324 	register struct ucred *cred = p->p_ucred;
325 	uid_t ouid;
326 	gid_t ogid;
327 	int error = 0;
328 #ifdef QUOTA
329 	register int i;
330 	long change;
331 #endif
332 
333 	if (uid == (u_short)VNOVAL)
334 		uid = ip->i_uid;
335 	if (gid == (u_short)VNOVAL)
336 		gid = ip->i_gid;
337 	/*
338 	 * If we don't own the file, are trying to change the owner
339 	 * of the file, or are not a member of the target group,
340 	 * the caller must be superuser or the call fails.
341 	 */
342 	if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
343 	    !groupmember((gid_t)gid, cred)) &&
344 	    (error = suser(cred, &p->p_acflag)))
345 		return (error);
346 	ouid = ip->i_uid;
347 	ogid = ip->i_gid;
348 #ifdef QUOTA
349 	if (error = getinoquota(ip))
350 		return (error);
351 	if (ouid == uid) {
352 		dqrele(vp, ip->i_dquot[USRQUOTA]);
353 		ip->i_dquot[USRQUOTA] = NODQUOT;
354 	}
355 	if (ogid == gid) {
356 		dqrele(vp, ip->i_dquot[GRPQUOTA]);
357 		ip->i_dquot[GRPQUOTA] = NODQUOT;
358 	}
359 	change = ip->i_blocks;
360 	(void) chkdq(ip, -change, cred, CHOWN);
361 	(void) chkiq(ip, -1, cred, CHOWN);
362 	for (i = 0; i < MAXQUOTAS; i++) {
363 		dqrele(vp, ip->i_dquot[i]);
364 		ip->i_dquot[i] = NODQUOT;
365 	}
366 #endif
367 	ip->i_uid = uid;
368 	ip->i_gid = gid;
369 #ifdef QUOTA
370 	if ((error = getinoquota(ip)) == 0) {
371 		if (ouid == uid) {
372 			dqrele(vp, ip->i_dquot[USRQUOTA]);
373 			ip->i_dquot[USRQUOTA] = NODQUOT;
374 		}
375 		if (ogid == gid) {
376 			dqrele(vp, ip->i_dquot[GRPQUOTA]);
377 			ip->i_dquot[GRPQUOTA] = NODQUOT;
378 		}
379 		if ((error = chkdq(ip, change, cred, CHOWN)) == 0) {
380 			if ((error = chkiq(ip, 1, cred, CHOWN)) == 0)
381 				goto good;
382 			else
383 				(void) chkdq(ip, -change, cred, CHOWN|FORCE);
384 		}
385 		for (i = 0; i < MAXQUOTAS; i++) {
386 			dqrele(vp, ip->i_dquot[i]);
387 			ip->i_dquot[i] = NODQUOT;
388 		}
389 	}
390 	ip->i_uid = ouid;
391 	ip->i_gid = ogid;
392 	if (getinoquota(ip) == 0) {
393 		if (ouid == uid) {
394 			dqrele(vp, ip->i_dquot[USRQUOTA]);
395 			ip->i_dquot[USRQUOTA] = NODQUOT;
396 		}
397 		if (ogid == gid) {
398 			dqrele(vp, ip->i_dquot[GRPQUOTA]);
399 			ip->i_dquot[GRPQUOTA] = NODQUOT;
400 		}
401 		(void) chkdq(ip, change, cred, FORCE|CHOWN);
402 		(void) chkiq(ip, 1, cred, FORCE|CHOWN);
403 		(void) getinoquota(ip);
404 	}
405 	return (error);
406 good:
407 	if (getinoquota(ip))
408 		panic("chown: lost quota");
409 #endif /* QUOTA */
410 	if (ouid != uid || ogid != gid)
411 		ip->i_flag |= ICHG;
412 	if (ouid != uid && cred->cr_uid != 0)
413 		ip->i_mode &= ~ISUID;
414 	if (ogid != gid && cred->cr_uid != 0)
415 		ip->i_mode &= ~ISGID;
416 	return (0);
417 }
418 
419 /*
420  * Vnode op for reading.
421  */
422 /* ARGSUSED */
423 ufs_read(vp, uio, ioflag, cred)
424 	struct vnode *vp;
425 	register struct uio *uio;
426 	int ioflag;
427 	struct ucred *cred;
428 {
429 	register struct inode *ip = VTOI(vp);
430 	register struct fs *fs;
431 	struct buf *bp;
432 	daddr_t lbn, bn, rablock;
433 	int size, diff, error = 0;
434 	long n, on, type;
435 
436 #ifdef DIAGNOSTIC
437 	if (uio->uio_rw != UIO_READ)
438 		panic("ufs_read mode");
439 	type = ip->i_mode & IFMT;
440 	if (type != IFDIR && type != IFREG && type != IFLNK)
441 		panic("ufs_read type");
442 #endif
443 	if (uio->uio_resid == 0)
444 		return (0);
445 	if (uio->uio_offset < 0)
446 		return (EINVAL);
447 	ip->i_flag |= IACC;
448 	fs = ip->i_fs;
449 	do {
450 		lbn = lblkno(fs, uio->uio_offset);
451 		on = blkoff(fs, uio->uio_offset);
452 		n = MIN((unsigned)(fs->fs_bsize - on), uio->uio_resid);
453 		diff = ip->i_size - uio->uio_offset;
454 		if (diff <= 0)
455 			return (0);
456 		if (diff < n)
457 			n = diff;
458 		size = blksize(fs, ip, lbn);
459 		rablock = lbn + 1;
460 		if (vp->v_lastr + 1 == lbn &&
461 		    lblktosize(fs, rablock) < ip->i_size)
462 			error = breada(ITOV(ip), lbn, size, rablock,
463 				blksize(fs, ip, rablock), NOCRED, &bp);
464 		else
465 			error = bread(ITOV(ip), lbn, size, NOCRED, &bp);
466 		vp->v_lastr = lbn;
467 		n = MIN(n, size - bp->b_resid);
468 		if (error) {
469 			brelse(bp);
470 			return (error);
471 		}
472 		error = uiomove(bp->b_un.b_addr + on, (int)n, uio);
473 		if (n + on == fs->fs_bsize || uio->uio_offset == ip->i_size)
474 			bp->b_flags |= B_AGE;
475 		brelse(bp);
476 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
477 	return (error);
478 }
479 
480 /*
481  * Vnode op for writing.
482  */
483 ufs_write(vp, uio, ioflag, cred)
484 	register struct vnode *vp;
485 	struct uio *uio;
486 	int ioflag;
487 	struct ucred *cred;
488 {
489 	struct proc *p = uio->uio_procp;
490 	register struct inode *ip = VTOI(vp);
491 	register struct fs *fs;
492 	struct buf *bp;
493 	daddr_t lbn, bn;
494 	u_long osize;
495 	int n, on, flags;
496 	int size, resid, error = 0;
497 
498 #ifdef DIAGNOSTIC
499 	if (uio->uio_rw != UIO_WRITE)
500 		panic("ufs_write mode");
501 #endif
502 	switch (vp->v_type) {
503 	case VREG:
504 		if (ioflag & IO_APPEND)
505 			uio->uio_offset = ip->i_size;
506 		/* fall through */
507 	case VLNK:
508 		break;
509 
510 	case VDIR:
511 		if ((ioflag & IO_SYNC) == 0)
512 			panic("ufs_write nonsync dir write");
513 		break;
514 
515 	default:
516 		panic("ufs_write type");
517 	}
518 	if (uio->uio_offset < 0)
519 		return (EINVAL);
520 	if (uio->uio_resid == 0)
521 		return (0);
522 	/*
523 	 * Maybe this should be above the vnode op call, but so long as
524 	 * file servers have no limits, i don't think it matters
525 	 */
526 	if (vp->v_type == VREG &&
527 	    uio->uio_offset + uio->uio_resid >
528 	      p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
529 		psignal(p, SIGXFSZ);
530 		return (EFBIG);
531 	}
532 	resid = uio->uio_resid;
533 	osize = ip->i_size;
534 	fs = ip->i_fs;
535 	flags = 0;
536 	if (ioflag & IO_SYNC)
537 		flags = B_SYNC;
538 	do {
539 		lbn = lblkno(fs, uio->uio_offset);
540 		on = blkoff(fs, uio->uio_offset);
541 		n = MIN((unsigned)(fs->fs_bsize - on), uio->uio_resid);
542 		if (n < fs->fs_bsize)
543 			flags |= B_CLRBUF;
544 		else
545 			flags &= ~B_CLRBUF;
546 		if (error = balloc(ip, lbn, (int)(on + n), &bp, flags))
547 			break;
548 		bn = bp->b_blkno;
549 		if (uio->uio_offset + n > ip->i_size) {
550 			ip->i_size = uio->uio_offset + n;
551 			vnode_pager_setsize(vp, ip->i_size);
552 		}
553 		size = blksize(fs, ip, lbn);
554 		(void) vnode_pager_uncache(vp);
555 		n = MIN(n, size - bp->b_resid);
556 		error = uiomove(bp->b_un.b_addr + on, n, uio);
557 		if (ioflag & IO_SYNC)
558 			(void) bwrite(bp);
559 		else if (n + on == fs->fs_bsize) {
560 			bp->b_flags |= B_AGE;
561 			bawrite(bp);
562 		} else
563 			bdwrite(bp);
564 		ip->i_flag |= IUPD|ICHG;
565 		if (cred->cr_uid != 0)
566 			ip->i_mode &= ~(ISUID|ISGID);
567 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
568 	if (error && (ioflag & IO_UNIT)) {
569 		(void) itrunc(ip, osize, ioflag & IO_SYNC);
570 		uio->uio_offset -= resid - uio->uio_resid;
571 		uio->uio_resid = resid;
572 	}
573 	if (!error && (ioflag & IO_SYNC))
574 		error = iupdat(ip, &time, &time, 1);
575 	return (error);
576 }
577 
578 /* ARGSUSED */
579 ufs_ioctl(vp, com, data, fflag, cred, p)
580 	struct vnode *vp;
581 	int com;
582 	caddr_t data;
583 	int fflag;
584 	struct ucred *cred;
585 	struct proc *p;
586 {
587 
588 	return (ENOTTY);
589 }
590 
591 /* ARGSUSED */
592 ufs_select(vp, which, fflags, cred, p)
593 	struct vnode *vp;
594 	int which, fflags;
595 	struct ucred *cred;
596 	struct proc *p;
597 {
598 
599 	/*
600 	 * We should really check to see if I/O is possible.
601 	 */
602 	return (1);
603 }
604 
605 /*
606  * Mmap a file
607  *
608  * NB Currently unsupported.
609  */
610 /* ARGSUSED */
611 ufs_mmap(vp, fflags, cred, p)
612 	struct vnode *vp;
613 	int fflags;
614 	struct ucred *cred;
615 	struct proc *p;
616 {
617 
618 	return (EINVAL);
619 }
620 
621 /*
622  * Synch an open file.
623  */
624 /* ARGSUSED */
625 ufs_fsync(vp, fflags, cred, waitfor, p)
626 	struct vnode *vp;
627 	int fflags;
628 	struct ucred *cred;
629 	int waitfor;
630 	struct proc *p;
631 {
632 	struct inode *ip = VTOI(vp);
633 
634 	if (fflags & FWRITE)
635 		ip->i_flag |= ICHG;
636 	vflushbuf(vp, waitfor == MNT_WAIT ? B_SYNC : 0);
637 	return (iupdat(ip, &time, &time, waitfor == MNT_WAIT));
638 }
639 
640 /*
641  * Seek on a file
642  *
643  * Nothing to do, so just return.
644  */
645 /* ARGSUSED */
646 ufs_seek(vp, oldoff, newoff, cred)
647 	struct vnode *vp;
648 	off_t oldoff, newoff;
649 	struct ucred *cred;
650 {
651 
652 	return (0);
653 }
654 
655 /*
656  * ufs remove
657  * Hard to avoid races here, especially
658  * in unlinking directories.
659  */
660 ufs_remove(ndp, p)
661 	struct nameidata *ndp;
662 	struct proc *p;
663 {
664 	register struct inode *ip, *dp;
665 	int error;
666 
667 	ip = VTOI(ndp->ni_vp);
668 	dp = VTOI(ndp->ni_dvp);
669 	error = dirremove(ndp);
670 	if (!error) {
671 		ip->i_nlink--;
672 		ip->i_flag |= ICHG;
673 	}
674 	if (dp == ip)
675 		vrele(ITOV(ip));
676 	else
677 		iput(ip);
678 	iput(dp);
679 	return (error);
680 }
681 
682 /*
683  * link vnode call
684  */
685 ufs_link(vp, ndp, p)
686 	register struct vnode *vp;
687 	register struct nameidata *ndp;
688 	struct proc *p;
689 {
690 	register struct inode *ip = VTOI(vp);
691 	int error;
692 
693 	if ((unsigned short)ip->i_nlink >= LINK_MAX)
694 		return (EMLINK);
695 	if (ndp->ni_dvp != vp)
696 		ILOCK(ip);
697 	ip->i_nlink++;
698 	ip->i_flag |= ICHG;
699 	error = iupdat(ip, &time, &time, 1);
700 	if (!error)
701 		error = direnter(ip, ndp);
702 	if (ndp->ni_dvp != vp)
703 		IUNLOCK(ip);
704 	vput(ndp->ni_dvp);
705 	if (error) {
706 		ip->i_nlink--;
707 		ip->i_flag |= ICHG;
708 	}
709 	return (error);
710 }
711 
712 /*
713  * Rename system call.
714  * 	rename("foo", "bar");
715  * is essentially
716  *	unlink("bar");
717  *	link("foo", "bar");
718  *	unlink("foo");
719  * but ``atomically''.  Can't do full commit without saving state in the
720  * inode on disk which isn't feasible at this time.  Best we can do is
721  * always guarantee the target exists.
722  *
723  * Basic algorithm is:
724  *
725  * 1) Bump link count on source while we're linking it to the
726  *    target.  This also ensure the inode won't be deleted out
727  *    from underneath us while we work (it may be truncated by
728  *    a concurrent `trunc' or `open' for creation).
729  * 2) Link source to destination.  If destination already exists,
730  *    delete it first.
731  * 3) Unlink source reference to inode if still around. If a
732  *    directory was moved and the parent of the destination
733  *    is different from the source, patch the ".." entry in the
734  *    directory.
735  */
736 ufs_rename(fndp, tndp, p)
737 	register struct nameidata *fndp, *tndp;
738 	struct proc *p;
739 {
740 	register struct inode *ip, *xp, *dp;
741 	struct dirtemplate dirbuf;
742 	int doingdirectory = 0, oldparent = 0, newparent = 0;
743 	int error = 0;
744 
745 	dp = VTOI(fndp->ni_dvp);
746 	ip = VTOI(fndp->ni_vp);
747 	ILOCK(ip);
748 	if ((ip->i_mode&IFMT) == IFDIR) {
749 		register struct direct *d = &fndp->ni_dent;
750 
751 		/*
752 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
753 		 */
754 		if ((d->d_namlen == 1 && d->d_name[0] == '.') || dp == ip ||
755 		    fndp->ni_isdotdot || (ip->i_flag & IRENAME)) {
756 			VOP_ABORTOP(tndp);
757 			vput(tndp->ni_dvp);
758 			if (tndp->ni_vp)
759 				vput(tndp->ni_vp);
760 			VOP_ABORTOP(fndp);
761 			vrele(fndp->ni_dvp);
762 			vput(fndp->ni_vp);
763 			return (EINVAL);
764 		}
765 		ip->i_flag |= IRENAME;
766 		oldparent = dp->i_number;
767 		doingdirectory++;
768 	}
769 	vrele(fndp->ni_dvp);
770 
771 	/*
772 	 * 1) Bump link count while we're moving stuff
773 	 *    around.  If we crash somewhere before
774 	 *    completing our work, the link count
775 	 *    may be wrong, but correctable.
776 	 */
777 	ip->i_nlink++;
778 	ip->i_flag |= ICHG;
779 	error = iupdat(ip, &time, &time, 1);
780 	IUNLOCK(ip);
781 
782 	/*
783 	 * When the target exists, both the directory
784 	 * and target vnodes are returned locked.
785 	 */
786 	dp = VTOI(tndp->ni_dvp);
787 	xp = NULL;
788 	if (tndp->ni_vp)
789 		xp = VTOI(tndp->ni_vp);
790 	/*
791 	 * If ".." must be changed (ie the directory gets a new
792 	 * parent) then the source directory must not be in the
793 	 * directory heirarchy above the target, as this would
794 	 * orphan everything below the source directory. Also
795 	 * the user must have write permission in the source so
796 	 * as to be able to change "..". We must repeat the call
797 	 * to namei, as the parent directory is unlocked by the
798 	 * call to checkpath().
799 	 */
800 	if (oldparent != dp->i_number)
801 		newparent = dp->i_number;
802 	if (doingdirectory && newparent) {
803 		VOP_LOCK(fndp->ni_vp);
804 		error = ufs_access(fndp->ni_vp, VWRITE, tndp->ni_cred, p);
805 		VOP_UNLOCK(fndp->ni_vp);
806 		if (error)
807 			goto bad;
808 		tndp->ni_nameiop &= ~(MODMASK | OPMASK);
809 		tndp->ni_nameiop |= RENAME | LOCKPARENT | LOCKLEAF | NOCACHE;
810 		do {
811 			dp = VTOI(tndp->ni_dvp);
812 			if (xp != NULL)
813 				iput(xp);
814 			if (error = checkpath(ip, dp, tndp->ni_cred))
815 				goto out;
816 			if (error = namei(tndp, p))
817 				goto out;
818 			xp = NULL;
819 			if (tndp->ni_vp)
820 				xp = VTOI(tndp->ni_vp);
821 		} while (dp != VTOI(tndp->ni_dvp));
822 	}
823 	/*
824 	 * 2) If target doesn't exist, link the target
825 	 *    to the source and unlink the source.
826 	 *    Otherwise, rewrite the target directory
827 	 *    entry to reference the source inode and
828 	 *    expunge the original entry's existence.
829 	 */
830 	if (xp == NULL) {
831 		if (dp->i_dev != ip->i_dev)
832 			panic("rename: EXDEV");
833 		/*
834 		 * Account for ".." in new directory.
835 		 * When source and destination have the same
836 		 * parent we don't fool with the link count.
837 		 */
838 		if (doingdirectory && newparent) {
839 			if ((unsigned short)dp->i_nlink >= LINK_MAX) {
840 				error = EMLINK;
841 				goto bad;
842 			}
843 			dp->i_nlink++;
844 			dp->i_flag |= ICHG;
845 			if (error = iupdat(dp, &time, &time, 1))
846 				goto bad;
847 		}
848 		if (error = direnter(ip, tndp)) {
849 			if (doingdirectory && newparent) {
850 				dp->i_nlink--;
851 				dp->i_flag |= ICHG;
852 				(void) iupdat(dp, &time, &time, 1);
853 			}
854 			goto bad;
855 		}
856 		iput(dp);
857 	} else {
858 		if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
859 			panic("rename: EXDEV");
860 		/*
861 		 * Short circuit rename(foo, foo).
862 		 */
863 		if (xp->i_number == ip->i_number)
864 			panic("rename: same file");
865 		/*
866 		 * If the parent directory is "sticky", then the user must
867 		 * own the parent directory, or the destination of the rename,
868 		 * otherwise the destination may not be changed (except by
869 		 * root). This implements append-only directories.
870 		 */
871 		if ((dp->i_mode & ISVTX) && tndp->ni_cred->cr_uid != 0 &&
872 		    tndp->ni_cred->cr_uid != dp->i_uid &&
873 		    xp->i_uid != tndp->ni_cred->cr_uid) {
874 			error = EPERM;
875 			goto bad;
876 		}
877 		/*
878 		 * Target must be empty if a directory
879 		 * and have no links to it.
880 		 * Also, insure source and target are
881 		 * compatible (both directories, or both
882 		 * not directories).
883 		 */
884 		if ((xp->i_mode&IFMT) == IFDIR) {
885 			if (!dirempty(xp, dp->i_number, tndp->ni_cred) ||
886 			    xp->i_nlink > 2) {
887 				error = ENOTEMPTY;
888 				goto bad;
889 			}
890 			if (!doingdirectory) {
891 				error = ENOTDIR;
892 				goto bad;
893 			}
894 			cache_purge(ITOV(dp));
895 		} else if (doingdirectory) {
896 			error = EISDIR;
897 			goto bad;
898 		}
899 		if (error = dirrewrite(dp, ip, tndp))
900 			goto bad;
901 		/*
902 		 * If the target directory is in the same
903 		 * directory as the source directory,
904 		 * decrement the link count on the parent
905 		 * of the target directory.
906 		 */
907 		 if (doingdirectory && !newparent) {
908 			dp->i_nlink--;
909 			dp->i_flag |= ICHG;
910 		}
911 		vput(ITOV(dp));
912 		/*
913 		 * Adjust the link count of the target to
914 		 * reflect the dirrewrite above.  If this is
915 		 * a directory it is empty and there are
916 		 * no links to it, so we can squash the inode and
917 		 * any space associated with it.  We disallowed
918 		 * renaming over top of a directory with links to
919 		 * it above, as the remaining link would point to
920 		 * a directory without "." or ".." entries.
921 		 */
922 		xp->i_nlink--;
923 		if (doingdirectory) {
924 			if (--xp->i_nlink != 0)
925 				panic("rename: linked directory");
926 			error = itrunc(xp, (u_long)0, IO_SYNC);
927 		}
928 		xp->i_flag |= ICHG;
929 		iput(xp);
930 		xp = NULL;
931 	}
932 
933 	/*
934 	 * 3) Unlink the source.
935 	 */
936 	fndp->ni_nameiop &= ~(MODMASK | OPMASK);
937 	fndp->ni_nameiop |= DELETE | LOCKPARENT | LOCKLEAF;
938 	(void)namei(fndp, p);
939 	if (fndp->ni_vp != NULL) {
940 		xp = VTOI(fndp->ni_vp);
941 		dp = VTOI(fndp->ni_dvp);
942 	} else {
943 		/*
944 		 * From name has disappeared.
945 		 */
946 		if (doingdirectory)
947 			panic("rename: lost dir entry");
948 		vrele(ITOV(ip));
949 		return (0);
950 	}
951 	/*
952 	 * Ensure that the directory entry still exists and has not
953 	 * changed while the new name has been entered. If the source is
954 	 * a file then the entry may have been unlinked or renamed. In
955 	 * either case there is no further work to be done. If the source
956 	 * is a directory then it cannot have been rmdir'ed; its link
957 	 * count of three would cause a rmdir to fail with ENOTEMPTY.
958 	 * The IRENAME flag ensures that it cannot be moved by another
959 	 * rename.
960 	 */
961 	if (xp != ip) {
962 		if (doingdirectory)
963 			panic("rename: lost dir entry");
964 	} else {
965 		/*
966 		 * If the source is a directory with a
967 		 * new parent, the link count of the old
968 		 * parent directory must be decremented
969 		 * and ".." set to point to the new parent.
970 		 */
971 		if (doingdirectory && newparent) {
972 			dp->i_nlink--;
973 			dp->i_flag |= ICHG;
974 			error = vn_rdwr(UIO_READ, ITOV(xp), (caddr_t)&dirbuf,
975 				sizeof (struct dirtemplate), (off_t)0,
976 				UIO_SYSSPACE, IO_NODELOCKED,
977 				tndp->ni_cred, (int *)0, (struct proc *)0);
978 			if (error == 0) {
979 				if (dirbuf.dotdot_namlen != 2 ||
980 				    dirbuf.dotdot_name[0] != '.' ||
981 				    dirbuf.dotdot_name[1] != '.') {
982 					dirbad(xp, 12, "rename: mangled dir");
983 				} else {
984 					dirbuf.dotdot_ino = newparent;
985 					(void) vn_rdwr(UIO_WRITE, ITOV(xp),
986 					    (caddr_t)&dirbuf,
987 					    sizeof (struct dirtemplate),
988 					    (off_t)0, UIO_SYSSPACE,
989 					    IO_NODELOCKED|IO_SYNC,
990 					    tndp->ni_cred, (int *)0,
991 					    (struct proc *)0);
992 					cache_purge(ITOV(dp));
993 				}
994 			}
995 		}
996 		error = dirremove(fndp);
997 		if (!error) {
998 			xp->i_nlink--;
999 			xp->i_flag |= ICHG;
1000 		}
1001 		xp->i_flag &= ~IRENAME;
1002 	}
1003 	if (dp)
1004 		vput(ITOV(dp));
1005 	if (xp)
1006 		vput(ITOV(xp));
1007 	vrele(ITOV(ip));
1008 	return (error);
1009 
1010 bad:
1011 	if (xp)
1012 		vput(ITOV(xp));
1013 	vput(ITOV(dp));
1014 out:
1015 	ip->i_nlink--;
1016 	ip->i_flag |= ICHG;
1017 	vrele(ITOV(ip));
1018 	return (error);
1019 }
1020 
1021 /*
1022  * A virgin directory (no blushing please).
1023  */
1024 struct dirtemplate mastertemplate = {
1025 	0, 12, 1, ".",
1026 	0, DIRBLKSIZ - 12, 2, ".."
1027 };
1028 
1029 /*
1030  * Mkdir system call
1031  */
1032 ufs_mkdir(ndp, vap, p)
1033 	struct nameidata *ndp;
1034 	struct vattr *vap;
1035 	struct proc *p;
1036 {
1037 	register struct inode *ip, *dp;
1038 	struct inode *tip;
1039 	struct vnode *dvp;
1040 	struct dirtemplate dirtemplate;
1041 	int error;
1042 	int dmode;
1043 
1044 	dvp = ndp->ni_dvp;
1045 	dp = VTOI(dvp);
1046 	if ((unsigned short)dp->i_nlink >= LINK_MAX) {
1047 		iput(dp);
1048 		return (EMLINK);
1049 	}
1050 	dmode = vap->va_mode&0777;
1051 	dmode |= IFDIR;
1052 	/*
1053 	 * Must simulate part of maknode here
1054 	 * in order to acquire the inode, but
1055 	 * not have it entered in the parent
1056 	 * directory.  The entry is made later
1057 	 * after writing "." and ".." entries out.
1058 	 */
1059 	if (error = ialloc(dp, dirpref(dp->i_fs), dmode, ndp->ni_cred, &tip)) {
1060 		iput(dp);
1061 		return (error);
1062 	}
1063 	ip = tip;
1064 	ip->i_uid = ndp->ni_cred->cr_uid;
1065 	ip->i_gid = dp->i_gid;
1066 #ifdef QUOTA
1067 	if ((error = getinoquota(ip)) ||
1068 	    (error = chkiq(ip, 1, ndp->ni_cred, 0))) {
1069 		ifree(ip, ip->i_number, dmode);
1070 		iput(ip);
1071 		iput(dp);
1072 		return (error);
1073 	}
1074 #endif
1075 	ip->i_flag |= IACC|IUPD|ICHG;
1076 	ip->i_mode = dmode;
1077 	ITOV(ip)->v_type = VDIR;	/* Rest init'd in iget() */
1078 	ip->i_nlink = 2;
1079 	error = iupdat(ip, &time, &time, 1);
1080 
1081 	/*
1082 	 * Bump link count in parent directory
1083 	 * to reflect work done below.  Should
1084 	 * be done before reference is created
1085 	 * so reparation is possible if we crash.
1086 	 */
1087 	dp->i_nlink++;
1088 	dp->i_flag |= ICHG;
1089 	if (error = iupdat(dp, &time, &time, 1))
1090 		goto bad;
1091 
1092 	/*
1093 	 * Initialize directory with "."
1094 	 * and ".." from static template.
1095 	 */
1096 	dirtemplate = mastertemplate;
1097 	dirtemplate.dot_ino = ip->i_number;
1098 	dirtemplate.dotdot_ino = dp->i_number;
1099 	error = vn_rdwr(UIO_WRITE, ITOV(ip), (caddr_t)&dirtemplate,
1100 	    sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
1101 	    IO_NODELOCKED|IO_SYNC, ndp->ni_cred, (int *)0, (struct proc *)0);
1102 	if (error) {
1103 		dp->i_nlink--;
1104 		dp->i_flag |= ICHG;
1105 		goto bad;
1106 	}
1107 	if (DIRBLKSIZ > dp->i_fs->fs_fsize) {
1108 		panic("mkdir: blksize");     /* XXX - should grow w/balloc() */
1109 	} else {
1110 		ip->i_size = DIRBLKSIZ;
1111 		ip->i_flag |= ICHG;
1112 	}
1113 	/*
1114 	 * Directory all set up, now
1115 	 * install the entry for it in
1116 	 * the parent directory.
1117 	 */
1118 	if (error = direnter(ip, ndp)) {
1119 		dp->i_nlink--;
1120 		dp->i_flag |= ICHG;
1121 	}
1122 bad:
1123 	/*
1124 	 * No need to do an explicit itrunc here,
1125 	 * vrele will do this for us because we set
1126 	 * the link count to 0.
1127 	 */
1128 	if (error) {
1129 		ip->i_nlink = 0;
1130 		ip->i_flag |= ICHG;
1131 		iput(ip);
1132 	} else
1133 		ndp->ni_vp = ITOV(ip);
1134 	iput(dp);
1135 	return (error);
1136 }
1137 
1138 /*
1139  * Rmdir system call.
1140  */
1141 ufs_rmdir(ndp, p)
1142 	register struct nameidata *ndp;
1143 	struct proc *p;
1144 {
1145 	register struct inode *ip, *dp;
1146 	int error = 0;
1147 
1148 	ip = VTOI(ndp->ni_vp);
1149 	dp = VTOI(ndp->ni_dvp);
1150 	/*
1151 	 * No rmdir "." please.
1152 	 */
1153 	if (dp == ip) {
1154 		vrele(ITOV(dp));
1155 		iput(ip);
1156 		return (EINVAL);
1157 	}
1158 	/*
1159 	 * Verify the directory is empty (and valid).
1160 	 * (Rmdir ".." won't be valid since
1161 	 *  ".." will contain a reference to
1162 	 *  the current directory and thus be
1163 	 *  non-empty.)
1164 	 */
1165 	if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number, ndp->ni_cred)) {
1166 		error = ENOTEMPTY;
1167 		goto out;
1168 	}
1169 	/*
1170 	 * Delete reference to directory before purging
1171 	 * inode.  If we crash in between, the directory
1172 	 * will be reattached to lost+found,
1173 	 */
1174 	if (error = dirremove(ndp))
1175 		goto out;
1176 	dp->i_nlink--;
1177 	dp->i_flag |= ICHG;
1178 	cache_purge(ITOV(dp));
1179 	iput(dp);
1180 	ndp->ni_dvp = NULL;
1181 	/*
1182 	 * Truncate inode.  The only stuff left
1183 	 * in the directory is "." and "..".  The
1184 	 * "." reference is inconsequential since
1185 	 * we're quashing it.  The ".." reference
1186 	 * has already been adjusted above.  We've
1187 	 * removed the "." reference and the reference
1188 	 * in the parent directory, but there may be
1189 	 * other hard links so decrement by 2 and
1190 	 * worry about them later.
1191 	 */
1192 	ip->i_nlink -= 2;
1193 	error = itrunc(ip, (u_long)0, IO_SYNC);
1194 	cache_purge(ITOV(ip));
1195 out:
1196 	if (ndp->ni_dvp)
1197 		iput(dp);
1198 	iput(ip);
1199 	return (error);
1200 }
1201 
1202 /*
1203  * symlink -- make a symbolic link
1204  */
1205 ufs_symlink(ndp, vap, target, p)
1206 	struct nameidata *ndp;
1207 	struct vattr *vap;
1208 	char *target;
1209 	struct proc *p;
1210 {
1211 	struct inode *ip;
1212 	int error;
1213 
1214 	error = maknode(IFLNK | vap->va_mode, ndp, &ip);
1215 	if (error)
1216 		return (error);
1217 	error = vn_rdwr(UIO_WRITE, ITOV(ip), target, strlen(target), (off_t)0,
1218 		UIO_SYSSPACE, IO_NODELOCKED, ndp->ni_cred, (int *)0,
1219 		(struct proc *)0);
1220 	iput(ip);
1221 	return (error);
1222 }
1223 
1224 /*
1225  * Vnode op for read and write
1226  */
1227 ufs_readdir(vp, uio, cred, eofflagp)
1228 	struct vnode *vp;
1229 	register struct uio *uio;
1230 	struct ucred *cred;
1231 	int *eofflagp;
1232 {
1233 	int count, lost, error;
1234 
1235 	count = uio->uio_resid;
1236 	count &= ~(DIRBLKSIZ - 1);
1237 	lost = uio->uio_resid - count;
1238 	if (count < DIRBLKSIZ || (uio->uio_offset & (DIRBLKSIZ -1)))
1239 		return (EINVAL);
1240 	uio->uio_resid = count;
1241 	uio->uio_iov->iov_len = count;
1242 	error = ufs_read(vp, uio, 0, cred);
1243 	uio->uio_resid += lost;
1244 	if ((VTOI(vp)->i_size - uio->uio_offset) <= 0)
1245 		*eofflagp = 1;
1246 	else
1247 		*eofflagp = 0;
1248 	return (error);
1249 }
1250 
1251 /*
1252  * Return target name of a symbolic link
1253  */
1254 ufs_readlink(vp, uiop, cred)
1255 	struct vnode *vp;
1256 	struct uio *uiop;
1257 	struct ucred *cred;
1258 {
1259 
1260 	return (ufs_read(vp, uiop, 0, cred));
1261 }
1262 
1263 /*
1264  * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually
1265  * done. Nothing to do at the moment.
1266  */
1267 /* ARGSUSED */
1268 ufs_abortop(ndp)
1269 	struct nameidata *ndp;
1270 {
1271 
1272 	return (0);
1273 }
1274 
1275 /*
1276  * Lock an inode.
1277  */
1278 ufs_lock(vp)
1279 	struct vnode *vp;
1280 {
1281 	register struct inode *ip = VTOI(vp);
1282 
1283 	ILOCK(ip);
1284 	return (0);
1285 }
1286 
1287 /*
1288  * Unlock an inode.
1289  */
1290 ufs_unlock(vp)
1291 	struct vnode *vp;
1292 {
1293 	register struct inode *ip = VTOI(vp);
1294 
1295 	if (!(ip->i_flag & ILOCKED))
1296 		panic("ufs_unlock NOT LOCKED");
1297 	IUNLOCK(ip);
1298 	return (0);
1299 }
1300 
1301 /*
1302  * Check for a locked inode.
1303  */
1304 ufs_islocked(vp)
1305 	struct vnode *vp;
1306 {
1307 
1308 	if (VTOI(vp)->i_flag & ILOCKED)
1309 		return (1);
1310 	return (0);
1311 }
1312 
1313 /*
1314  * Get access to bmap
1315  */
1316 ufs_bmap(vp, bn, vpp, bnp)
1317 	struct vnode *vp;
1318 	daddr_t bn;
1319 	struct vnode **vpp;
1320 	daddr_t *bnp;
1321 {
1322 	struct inode *ip = VTOI(vp);
1323 
1324 	if (vpp != NULL)
1325 		*vpp = ip->i_devvp;
1326 	if (bnp == NULL)
1327 		return (0);
1328 	return (bmap(ip, bn, bnp));
1329 }
1330 
1331 /*
1332  * Calculate the logical to physical mapping if not done already,
1333  * then call the device strategy routine.
1334  */
1335 int checkoverlap = 0;
1336 
1337 ufs_strategy(bp)
1338 	register struct buf *bp;
1339 {
1340 	register struct inode *ip = VTOI(bp->b_vp);
1341 	struct vnode *vp;
1342 	int error;
1343 
1344 	if (bp->b_vp->v_type == VBLK || bp->b_vp->v_type == VCHR)
1345 		panic("ufs_strategy: spec");
1346 	if (bp->b_blkno == bp->b_lblkno) {
1347 		if (error = bmap(ip, bp->b_lblkno, &bp->b_blkno))
1348 			return (error);
1349 		if ((long)bp->b_blkno == -1)
1350 			clrbuf(bp);
1351 	}
1352 	if ((long)bp->b_blkno == -1) {
1353 		biodone(bp);
1354 		return (0);
1355 	}
1356 #ifdef DIAGNOSTIC
1357 	if (checkoverlap) {
1358 		register struct buf *ep;
1359 		struct buf *ebp;
1360 		daddr_t start, last;
1361 
1362 		ebp = &buf[nbuf];
1363 		start = bp->b_blkno;
1364 		last = start + btodb(bp->b_bcount) - 1;
1365 		for (ep = buf; ep < ebp; ep++) {
1366 			if (ep == bp || (ep->b_flags & B_INVAL) ||
1367 			    ep->b_vp == NULLVP)
1368 				continue;
1369 			if (VOP_BMAP(ep->b_vp, (daddr_t)0, &vp, (daddr_t)0))
1370 				continue;
1371 			if (vp != ip->i_devvp)
1372 				continue;
1373 			/* look for overlap */
1374 			if (ep->b_bcount == 0 || ep->b_blkno > last ||
1375 			    ep->b_blkno + btodb(ep->b_bcount) <= start)
1376 				continue;
1377 			vprint("Disk overlap", vp);
1378 			printf("\tstart %d, end %d overlap start %d, end %d\n",
1379 				start, last, ep->b_blkno,
1380 				ep->b_blkno + btodb(ep->b_bcount) - 1);
1381 			panic("Disk buffer overlap");
1382 		}
1383 	}
1384 #endif /* DIAGNOSTIC */
1385 	vp = ip->i_devvp;
1386 	bp->b_dev = vp->v_rdev;
1387 	(*(vp->v_op->vn_strategy))(bp);
1388 	return (0);
1389 }
1390 
1391 /*
1392  * Print out the contents of an inode.
1393  */
1394 ufs_print(vp)
1395 	struct vnode *vp;
1396 {
1397 	register struct inode *ip = VTOI(vp);
1398 
1399 	printf("tag VT_UFS, ino %d, on dev %d, %d", ip->i_number,
1400 		major(ip->i_dev), minor(ip->i_dev));
1401 #ifdef FIFO
1402 	if (vp->v_type == VFIFO)
1403 		fifo_printinfo(vp);
1404 #endif /* FIFO */
1405 	printf("%s\n", (ip->i_flag & ILOCKED) ? " (LOCKED)" : "");
1406 	if (ip->i_spare0 == 0)
1407 		return;
1408 	printf("\towner pid %d", ip->i_spare0);
1409 	if (ip->i_spare1)
1410 		printf(" waiting pid %d", ip->i_spare1);
1411 	printf("\n");
1412 }
1413 
1414 /*
1415  * Read wrapper for special devices.
1416  */
1417 ufsspec_read(vp, uio, ioflag, cred)
1418 	struct vnode *vp;
1419 	struct uio *uio;
1420 	int ioflag;
1421 	struct ucred *cred;
1422 {
1423 
1424 	/*
1425 	 * Set access flag.
1426 	 */
1427 	VTOI(vp)->i_flag |= IACC;
1428 	return (spec_read(vp, uio, ioflag, cred));
1429 }
1430 
1431 /*
1432  * Write wrapper for special devices.
1433  */
1434 ufsspec_write(vp, uio, ioflag, cred)
1435 	struct vnode *vp;
1436 	struct uio *uio;
1437 	int ioflag;
1438 	struct ucred *cred;
1439 {
1440 
1441 	/*
1442 	 * Set update and change flags.
1443 	 */
1444 	VTOI(vp)->i_flag |= IUPD|ICHG;
1445 	return (spec_write(vp, uio, ioflag, cred));
1446 }
1447 
1448 /*
1449  * Close wrapper for special devices.
1450  *
1451  * Update the times on the inode then do device close.
1452  */
1453 ufsspec_close(vp, fflag, cred, p)
1454 	struct vnode *vp;
1455 	int fflag;
1456 	struct ucred *cred;
1457 	struct proc *p;
1458 {
1459 	register struct inode *ip = VTOI(vp);
1460 
1461 	if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED))
1462 		ITIMES(ip, &time, &time);
1463 	return (spec_close(vp, fflag, cred, p));
1464 }
1465 
1466 #ifdef FIFO
1467 /*
1468  * Read wrapper for fifo's
1469  */
1470 ufsfifo_read(vp, uio, ioflag, cred)
1471 	struct vnode *vp;
1472 	struct uio *uio;
1473 	int ioflag;
1474 	struct ucred *cred;
1475 {
1476 
1477 	/*
1478 	 * Set access flag.
1479 	 */
1480 	VTOI(vp)->i_flag |= IACC;
1481 	return (fifo_read(vp, uio, ioflag, cred));
1482 }
1483 
1484 /*
1485  * Write wrapper for fifo's.
1486  */
1487 ufsfifo_write(vp, uio, ioflag, cred)
1488 	struct vnode *vp;
1489 	struct uio *uio;
1490 	int ioflag;
1491 	struct ucred *cred;
1492 {
1493 
1494 	/*
1495 	 * Set update and change flags.
1496 	 */
1497 	VTOI(vp)->i_flag |= IUPD|ICHG;
1498 	return (fifo_write(vp, uio, ioflag, cred));
1499 }
1500 
1501 /*
1502  * Close wrapper for fifo's.
1503  *
1504  * Update the times on the inode then do device close.
1505  */
1506 ufsfifo_close(vp, fflag, cred, p)
1507 	struct vnode *vp;
1508 	int fflag;
1509 	struct ucred *cred;
1510 	struct proc *p;
1511 {
1512 	register struct inode *ip = VTOI(vp);
1513 
1514 	if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED))
1515 		ITIMES(ip, &time, &time);
1516 	return (fifo_close(vp, fflag, cred, p));
1517 }
1518 #endif /* FIFO */
1519 
1520 /*
1521  * Make a new file.
1522  */
1523 maknode(mode, ndp, ipp)
1524 	int mode;
1525 	register struct nameidata *ndp;
1526 	struct inode **ipp;
1527 {
1528 	register struct inode *ip;
1529 	struct inode *tip;
1530 	register struct inode *pdir = VTOI(ndp->ni_dvp);
1531 	ino_t ipref;
1532 	int error;
1533 
1534 	*ipp = 0;
1535 	if ((mode & IFMT) == 0)
1536 		mode |= IFREG;
1537 	if ((mode & IFMT) == IFDIR)
1538 		ipref = dirpref(pdir->i_fs);
1539 	else
1540 		ipref = pdir->i_number;
1541 	if (error = ialloc(pdir, ipref, mode, ndp->ni_cred, &tip)) {
1542 		iput(pdir);
1543 		return (error);
1544 	}
1545 	ip = tip;
1546 	ip->i_uid = ndp->ni_cred->cr_uid;
1547 	ip->i_gid = pdir->i_gid;
1548 #ifdef QUOTA
1549 	if ((error = getinoquota(ip)) ||
1550 	    (error = chkiq(ip, 1, ndp->ni_cred, 0))) {
1551 		ifree(ip, ip->i_number, mode);
1552 		iput(ip);
1553 		iput(pdir);
1554 		return (error);
1555 	}
1556 #endif
1557 	ip->i_flag |= IACC|IUPD|ICHG;
1558 	ip->i_mode = mode;
1559 	ITOV(ip)->v_type = IFTOVT(mode);	/* Rest init'd in iget() */
1560 	ip->i_nlink = 1;
1561 	if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, ndp->ni_cred) &&
1562 	    suser(ndp->ni_cred, NULL))
1563 		ip->i_mode &= ~ISGID;
1564 
1565 	/*
1566 	 * Make sure inode goes to disk before directory entry.
1567 	 */
1568 	if (error = iupdat(ip, &time, &time, 1))
1569 		goto bad;
1570 	if (error = direnter(ip, ndp))
1571 		goto bad;
1572 	iput(pdir);
1573 	*ipp = ip;
1574 	return (0);
1575 
1576 bad:
1577 	/*
1578 	 * Write error occurred trying to update the inode
1579 	 * or the directory so must deallocate the inode.
1580 	 */
1581 	iput(pdir);
1582 	ip->i_nlink = 0;
1583 	ip->i_flag |= ICHG;
1584 	iput(ip);
1585 	return (error);
1586 }
1587 
1588 /*
1589  * Advisory record locking support
1590  */
1591 ufs_advlock(vp, id, op, fl, flags)
1592 	struct vnode *vp;
1593 	caddr_t id;
1594 	int op;
1595 	register struct flock *fl;
1596 	int flags;
1597 {
1598 	register struct inode *ip = VTOI(vp);
1599 	register struct lockf *lock;
1600 	off_t start, end;
1601 	int error;
1602 
1603 	/*
1604 	 * Avoid the common case of unlocking when inode has no locks.
1605 	 */
1606 	if (ip->i_lockf == (struct lockf *)0) {
1607 		if (op != F_SETLK) {
1608 			fl->l_type = F_UNLCK;
1609 			return (0);
1610 		}
1611 	}
1612 	/*
1613 	 * Convert the flock structure into a start and end.
1614 	 */
1615 	switch (fl->l_whence) {
1616 
1617 	case SEEK_SET:
1618 	case SEEK_CUR:
1619 		/*
1620 		 * Caller is responsible for adding any necessary offset
1621 		 * when SEEK_CUR is used.
1622 		 */
1623 		start = fl->l_start;
1624 		break;
1625 
1626 	case SEEK_END:
1627 		start = ip->i_size + fl->l_start;
1628 		break;
1629 
1630 	default:
1631 		return (EINVAL);
1632 	}
1633 	if (start < 0)
1634 		return (EINVAL);
1635 	if (fl->l_len == 0)
1636 		end = -1;
1637 	else
1638 		end = start + fl->l_len - 1;
1639 	/*
1640 	 * Create the lockf structure
1641 	 */
1642 	MALLOC(lock, struct lockf *, sizeof *lock, M_LOCKF, M_WAITOK);
1643 	lock->lf_start = start;
1644 	lock->lf_end = end;
1645 	lock->lf_id = id;
1646 	lock->lf_inode = ip;
1647 	lock->lf_type = fl->l_type;
1648 	lock->lf_next = (struct lockf *)0;
1649 	lock->lf_block = (struct lockf *)0;
1650 	lock->lf_flags = flags;
1651 	/*
1652 	 * Do the requested operation.
1653 	 */
1654 	switch(op) {
1655 	case F_SETLK:
1656 		return (lf_setlock(lock));
1657 
1658 	case F_UNLCK:
1659 		error = lf_clearlock(lock);
1660 		FREE(lock, M_LOCKF);
1661 		return (error);
1662 
1663 	case F_GETLK:
1664 		error = lf_getlock(lock, fl);
1665 		FREE(lock, M_LOCKF);
1666 		return (error);
1667 
1668 	default:
1669 		free(lock, M_LOCKF);
1670 		return (EINVAL);
1671 	}
1672 	/* NOTREACHED */
1673 }
1674 
1675 /*
1676  * Global vfs data structures for ufs
1677  */
1678 struct vnodeops ufs_vnodeops = {
1679 	ufs_lookup,		/* lookup */
1680 	ufs_create,		/* create */
1681 	ufs_mknod,		/* mknod */
1682 	ufs_open,		/* open */
1683 	ufs_close,		/* close */
1684 	ufs_access,		/* access */
1685 	ufs_getattr,		/* getattr */
1686 	ufs_setattr,		/* setattr */
1687 	ufs_read,		/* read */
1688 	ufs_write,		/* write */
1689 	ufs_ioctl,		/* ioctl */
1690 	ufs_select,		/* select */
1691 	ufs_mmap,		/* mmap */
1692 	ufs_fsync,		/* fsync */
1693 	ufs_seek,		/* seek */
1694 	ufs_remove,		/* remove */
1695 	ufs_link,		/* link */
1696 	ufs_rename,		/* rename */
1697 	ufs_mkdir,		/* mkdir */
1698 	ufs_rmdir,		/* rmdir */
1699 	ufs_symlink,		/* symlink */
1700 	ufs_readdir,		/* readdir */
1701 	ufs_readlink,		/* readlink */
1702 	ufs_abortop,		/* abortop */
1703 	ufs_inactive,		/* inactive */
1704 	ufs_reclaim,		/* reclaim */
1705 	ufs_lock,		/* lock */
1706 	ufs_unlock,		/* unlock */
1707 	ufs_bmap,		/* bmap */
1708 	ufs_strategy,		/* strategy */
1709 	ufs_print,		/* print */
1710 	ufs_islocked,		/* islocked */
1711 	ufs_advlock,		/* advlock */
1712 };
1713 
1714 struct vnodeops spec_inodeops = {
1715 	spec_lookup,		/* lookup */
1716 	spec_create,		/* create */
1717 	spec_mknod,		/* mknod */
1718 	spec_open,		/* open */
1719 	ufsspec_close,		/* close */
1720 	ufs_access,		/* access */
1721 	ufs_getattr,		/* getattr */
1722 	ufs_setattr,		/* setattr */
1723 	ufsspec_read,		/* read */
1724 	ufsspec_write,		/* write */
1725 	spec_ioctl,		/* ioctl */
1726 	spec_select,		/* select */
1727 	spec_mmap,		/* mmap */
1728 	spec_fsync,		/* fsync */
1729 	spec_seek,		/* seek */
1730 	spec_remove,		/* remove */
1731 	spec_link,		/* link */
1732 	spec_rename,		/* rename */
1733 	spec_mkdir,		/* mkdir */
1734 	spec_rmdir,		/* rmdir */
1735 	spec_symlink,		/* symlink */
1736 	spec_readdir,		/* readdir */
1737 	spec_readlink,		/* readlink */
1738 	spec_abortop,		/* abortop */
1739 	ufs_inactive,		/* inactive */
1740 	ufs_reclaim,		/* reclaim */
1741 	ufs_lock,		/* lock */
1742 	ufs_unlock,		/* unlock */
1743 	spec_bmap,		/* bmap */
1744 	spec_strategy,		/* strategy */
1745 	ufs_print,		/* print */
1746 	ufs_islocked,		/* islocked */
1747 	spec_advlock,		/* advlock */
1748 };
1749 
1750 #ifdef FIFO
1751 struct vnodeops fifo_inodeops = {
1752 	fifo_lookup,		/* lookup */
1753 	fifo_create,		/* create */
1754 	fifo_mknod,		/* mknod */
1755 	fifo_open,		/* open */
1756 	ufsfifo_close,		/* close */
1757 	ufs_access,		/* access */
1758 	ufs_getattr,		/* getattr */
1759 	ufs_setattr,		/* setattr */
1760 	ufsfifo_read,		/* read */
1761 	ufsfifo_write,		/* write */
1762 	fifo_ioctl,		/* ioctl */
1763 	fifo_select,		/* select */
1764 	fifo_mmap,		/* mmap */
1765 	fifo_fsync,		/* fsync */
1766 	fifo_seek,		/* seek */
1767 	fifo_remove,		/* remove */
1768 	fifo_link,		/* link */
1769 	fifo_rename,		/* rename */
1770 	fifo_mkdir,		/* mkdir */
1771 	fifo_rmdir,		/* rmdir */
1772 	fifo_symlink,		/* symlink */
1773 	fifo_readdir,		/* readdir */
1774 	fifo_readlink,		/* readlink */
1775 	fifo_abortop,		/* abortop */
1776 	ufs_inactive,		/* inactive */
1777 	ufs_reclaim,		/* reclaim */
1778 	ufs_lock,		/* lock */
1779 	ufs_unlock,		/* unlock */
1780 	fifo_bmap,		/* bmap */
1781 	fifo_strategy,		/* strategy */
1782 	ufs_print,		/* print */
1783 	ufs_islocked,		/* islocked */
1784 	fifo_advlock,		/* advlock */
1785 };
1786 #endif /* FIFO */
1787 
1788 enum vtype iftovt_tab[16] = {
1789 	VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
1790 	VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
1791 };
1792 int	vttoif_tab[9] = {
1793 	0, IFREG, IFDIR, IFBLK, IFCHR, IFLNK, IFSOCK, IFIFO, IFMT,
1794 };
1795