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