xref: /csrg-svn/sys/ufs/lfs/lfs_vnops.c (revision 40293)
123405Smckusick /*
237737Smckusick  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
337737Smckusick  * All rights reserved.
423405Smckusick  *
537737Smckusick  * Redistribution and use in source and binary forms are permitted
637737Smckusick  * provided that the above copyright notice and this paragraph are
737737Smckusick  * duplicated in all such forms and that any documentation,
837737Smckusick  * advertising materials, and other materials related to such
937737Smckusick  * distribution and use acknowledge that the software was developed
1037737Smckusick  * by the University of California, Berkeley.  The name of the
1137737Smckusick  * University may not be used to endorse or promote products derived
1237737Smckusick  * from this software without specific prior written permission.
1337737Smckusick  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1437737Smckusick  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1537737Smckusick  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1637737Smckusick  *
17*40293Smckusick  *	@(#)lfs_vnops.c	7.32 (Berkeley) 03/05/90
1823405Smckusick  */
1937Sbill 
2017101Sbloom #include "param.h"
2117101Sbloom #include "systm.h"
2217101Sbloom #include "user.h"
2317101Sbloom #include "kernel.h"
2417101Sbloom #include "file.h"
2517101Sbloom #include "stat.h"
2617101Sbloom #include "buf.h"
2717101Sbloom #include "proc.h"
2817101Sbloom #include "uio.h"
2917101Sbloom #include "socket.h"
3017101Sbloom #include "socketvar.h"
3137737Smckusick #include "conf.h"
3217101Sbloom #include "mount.h"
3337737Smckusick #include "vnode.h"
3437737Smckusick #include "../ufs/inode.h"
3537737Smckusick #include "../ufs/fs.h"
3637737Smckusick #include "../ufs/quota.h"
3737Sbill 
389167Ssam /*
3937737Smckusick  * Global vfs data structures for ufs
409167Ssam  */
416254Sroot 
4237737Smckusick int	ufs_lookup(),
4337737Smckusick 	ufs_create(),
4437737Smckusick 	ufs_mknod(),
4537737Smckusick 	ufs_open(),
4637737Smckusick 	ufs_close(),
4737737Smckusick 	ufs_access(),
4837737Smckusick 	ufs_getattr(),
4937737Smckusick 	ufs_setattr(),
5037737Smckusick 	ufs_read(),
5137737Smckusick 	ufs_write(),
5237737Smckusick 	ufs_ioctl(),
5337737Smckusick 	ufs_select(),
5437737Smckusick 	ufs_mmap(),
5537737Smckusick 	ufs_fsync(),
5637737Smckusick 	ufs_seek(),
5737737Smckusick 	ufs_remove(),
5837737Smckusick 	ufs_link(),
5937737Smckusick 	ufs_rename(),
6037737Smckusick 	ufs_mkdir(),
6137737Smckusick 	ufs_rmdir(),
6237737Smckusick 	ufs_symlink(),
6337737Smckusick 	ufs_readdir(),
6437737Smckusick 	ufs_readlink(),
6537737Smckusick 	ufs_abortop(),
6637737Smckusick 	ufs_inactive(),
6739391Smckusick 	ufs_reclaim(),
6837737Smckusick 	ufs_lock(),
6937737Smckusick 	ufs_unlock(),
7037737Smckusick 	ufs_bmap(),
7139674Smckusick 	ufs_strategy(),
7239909Smckusick 	ufs_print(),
7339909Smckusick 	ufs_islocked();
746254Sroot 
7537737Smckusick struct vnodeops ufs_vnodeops = {
7639674Smckusick 	ufs_lookup,		/* lookup */
7739674Smckusick 	ufs_create,		/* create */
7839674Smckusick 	ufs_mknod,		/* mknod */
7939674Smckusick 	ufs_open,		/* open */
8039674Smckusick 	ufs_close,		/* close */
8139674Smckusick 	ufs_access,		/* access */
8239674Smckusick 	ufs_getattr,		/* getattr */
8339674Smckusick 	ufs_setattr,		/* setattr */
8439674Smckusick 	ufs_read,		/* read */
8539674Smckusick 	ufs_write,		/* write */
8639674Smckusick 	ufs_ioctl,		/* ioctl */
8739674Smckusick 	ufs_select,		/* select */
8839674Smckusick 	ufs_mmap,		/* mmap */
8939674Smckusick 	ufs_fsync,		/* fsync */
9039674Smckusick 	ufs_seek,		/* seek */
9139674Smckusick 	ufs_remove,		/* remove */
9239674Smckusick 	ufs_link,		/* link */
9339674Smckusick 	ufs_rename,		/* rename */
9439674Smckusick 	ufs_mkdir,		/* mkdir */
9539674Smckusick 	ufs_rmdir,		/* rmdir */
9639674Smckusick 	ufs_symlink,		/* symlink */
9739674Smckusick 	ufs_readdir,		/* readdir */
9839674Smckusick 	ufs_readlink,		/* readlink */
9939674Smckusick 	ufs_abortop,		/* abortop */
10039674Smckusick 	ufs_inactive,		/* inactive */
10139674Smckusick 	ufs_reclaim,		/* reclaim */
10239674Smckusick 	ufs_lock,		/* lock */
10339674Smckusick 	ufs_unlock,		/* unlock */
10439674Smckusick 	ufs_bmap,		/* bmap */
10539674Smckusick 	ufs_strategy,		/* strategy */
10639674Smckusick 	ufs_print,		/* print */
10739909Smckusick 	ufs_islocked,		/* islocked */
10837737Smckusick };
1096254Sroot 
11039435Smckusick int	spec_lookup(),
11139435Smckusick 	spec_open(),
11239628Smckusick 	ufsspec_read(),
11339628Smckusick 	ufsspec_write(),
11439435Smckusick 	spec_strategy(),
11539674Smckusick 	spec_bmap(),
11639435Smckusick 	spec_ioctl(),
11739435Smckusick 	spec_select(),
11839628Smckusick 	ufsspec_close(),
11939435Smckusick 	spec_badop(),
12039435Smckusick 	spec_nullop();
12139435Smckusick 
12239435Smckusick struct vnodeops spec_inodeops = {
12339597Smckusick 	spec_lookup,		/* lookup */
12439597Smckusick 	spec_badop,		/* create */
12539597Smckusick 	spec_badop,		/* mknod */
12639597Smckusick 	spec_open,		/* open */
12739628Smckusick 	ufsspec_close,		/* close */
12839597Smckusick 	ufs_access,		/* access */
12939597Smckusick 	ufs_getattr,		/* getattr */
13039597Smckusick 	ufs_setattr,		/* setattr */
13139628Smckusick 	ufsspec_read,		/* read */
13239628Smckusick 	ufsspec_write,		/* write */
13339597Smckusick 	spec_ioctl,		/* ioctl */
13439597Smckusick 	spec_select,		/* select */
13539597Smckusick 	spec_badop,		/* mmap */
13639597Smckusick 	spec_nullop,		/* fsync */
13739597Smckusick 	spec_badop,		/* seek */
13839597Smckusick 	spec_badop,		/* remove */
13939597Smckusick 	spec_badop,		/* link */
14039597Smckusick 	spec_badop,		/* rename */
14139597Smckusick 	spec_badop,		/* mkdir */
14239597Smckusick 	spec_badop,		/* rmdir */
14339597Smckusick 	spec_badop,		/* symlink */
14439597Smckusick 	spec_badop,		/* readdir */
14539597Smckusick 	spec_badop,		/* readlink */
14639597Smckusick 	spec_badop,		/* abortop */
14739597Smckusick 	ufs_inactive,		/* inactive */
14839597Smckusick 	ufs_reclaim,		/* reclaim */
14939597Smckusick 	ufs_lock,		/* lock */
15039597Smckusick 	ufs_unlock,		/* unlock */
15139674Smckusick 	spec_bmap,		/* bmap */
15239597Smckusick 	spec_strategy,		/* strategy */
15339674Smckusick 	ufs_print,		/* print */
15439909Smckusick 	ufs_islocked,		/* islocked */
15539435Smckusick };
15639435Smckusick 
15740290Smckusick #ifdef FIFO
15840290Smckusick int	fifo_lookup(),
15940290Smckusick 	fifo_open(),
16040290Smckusick 	ufsfifo_read(),
16140290Smckusick 	ufsfifo_write(),
16240290Smckusick 	fifo_bmap(),
16340290Smckusick 	fifo_ioctl(),
16440290Smckusick 	fifo_select(),
16540290Smckusick 	ufsfifo_close(),
16640290Smckusick 	fifo_print(),
16740290Smckusick 	fifo_badop(),
16840290Smckusick 	fifo_nullop();
16940290Smckusick 
17040290Smckusick struct vnodeops fifo_inodeops = {
17140290Smckusick 	fifo_lookup,		/* lookup */
17240290Smckusick 	fifo_badop,		/* create */
17340290Smckusick 	fifo_badop,		/* mknod */
17440290Smckusick 	fifo_open,		/* open */
17540290Smckusick 	ufsfifo_close,		/* close */
17640290Smckusick 	ufs_access,		/* access */
17740290Smckusick 	ufs_getattr,		/* getattr */
17840290Smckusick 	ufs_setattr,		/* setattr */
17940290Smckusick 	ufsfifo_read,		/* read */
18040290Smckusick 	ufsfifo_write,		/* write */
18140290Smckusick 	fifo_ioctl,		/* ioctl */
18240290Smckusick 	fifo_select,		/* select */
18340290Smckusick 	fifo_badop,		/* mmap */
18440290Smckusick 	fifo_nullop,		/* fsync */
18540290Smckusick 	fifo_badop,		/* seek */
18640290Smckusick 	fifo_badop,		/* remove */
18740290Smckusick 	fifo_badop,		/* link */
18840290Smckusick 	fifo_badop,		/* rename */
18940290Smckusick 	fifo_badop,		/* mkdir */
19040290Smckusick 	fifo_badop,		/* rmdir */
19140290Smckusick 	fifo_badop,		/* symlink */
19240290Smckusick 	fifo_badop,		/* readdir */
19340290Smckusick 	fifo_badop,		/* readlink */
19440290Smckusick 	fifo_badop,		/* abortop */
19540290Smckusick 	ufs_inactive,		/* inactive */
19640290Smckusick 	ufs_reclaim,		/* reclaim */
19740290Smckusick 	ufs_lock,		/* lock */
19840290Smckusick 	ufs_unlock,		/* unlock */
19940290Smckusick 	fifo_bmap,		/* bmap */
20040290Smckusick 	fifo_badop,		/* strategy */
20140290Smckusick 	ufs_print,		/* print */
20240290Smckusick 	ufs_islocked,		/* islocked */
20337737Smckusick };
20440290Smckusick #endif /* FIFO */
20540290Smckusick 
20640290Smckusick enum vtype iftovt_tab[16] = {
20740290Smckusick 	VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
20840290Smckusick 	VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
20937737Smckusick };
21040290Smckusick int	vttoif_tab[9] = {
21140290Smckusick 	0, IFREG, IFDIR, IFBLK, IFCHR, IFLNK, IFSOCK, IFIFO, IFMT,
21240290Smckusick };
2136254Sroot 
2149167Ssam /*
21537737Smckusick  * Create a regular file
2169167Ssam  */
21737737Smckusick ufs_create(ndp, vap)
21837737Smckusick 	struct nameidata *ndp;
21937737Smckusick 	struct vattr *vap;
2206254Sroot {
22137737Smckusick 	struct inode *ip;
22237737Smckusick 	int error;
2236254Sroot 
22437737Smckusick 	if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip))
22537737Smckusick 		return (error);
22637737Smckusick 	ndp->ni_vp = ITOV(ip);
22737737Smckusick 	return (0);
2286254Sroot }
2296254Sroot 
23037Sbill /*
23137737Smckusick  * Mknod vnode call
2326254Sroot  */
23337737Smckusick /* ARGSUSED */
23437737Smckusick ufs_mknod(ndp, vap, cred)
23537737Smckusick 	struct nameidata *ndp;
23637737Smckusick 	struct ucred *cred;
23737737Smckusick 	struct vattr *vap;
2386254Sroot {
23939435Smckusick 	register struct vnode *vp;
24037737Smckusick 	struct inode *ip;
24137737Smckusick 	int error;
2426254Sroot 
24337737Smckusick 	if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip))
24437737Smckusick 		return (error);
24540290Smckusick 	ip->i_flag |= IACC|IUPD|ICHG;
24640290Smckusick 	if (vap->va_rdev != VNOVAL) {
24737737Smckusick 		/*
24837737Smckusick 		 * Want to be able to use this to make badblock
24937737Smckusick 		 * inodes, so don't truncate the dev number.
25037737Smckusick 		 */
25139608Smckusick 		ip->i_rdev = vap->va_rdev;
25212756Ssam 	}
25337737Smckusick 	/*
25437737Smckusick 	 * Remove inode so that it will be reloaded by iget and
25537737Smckusick 	 * checked to see if it is an alias of an existing entry
25637737Smckusick 	 * in the inode cache.
25737737Smckusick 	 */
25840290Smckusick 	vp = ITOV(ip);
25940290Smckusick 	vput(vp);
26039435Smckusick 	vp->v_type = VNON;
26139435Smckusick 	vgone(vp);
26237737Smckusick 	return (0);
2636254Sroot }
2646254Sroot 
2656254Sroot /*
26637737Smckusick  * Open called.
26737737Smckusick  *
26837737Smckusick  * Nothing to do.
2696254Sroot  */
27037737Smckusick /* ARGSUSED */
27137737Smckusick ufs_open(vp, mode, cred)
27237737Smckusick 	struct vnode *vp;
27337737Smckusick 	int mode;
27437737Smckusick 	struct ucred *cred;
2756254Sroot {
2766254Sroot 
27737737Smckusick 	return (0);
2786254Sroot }
2796254Sroot 
2806254Sroot /*
28137737Smckusick  * Close called
28237737Smckusick  *
28337737Smckusick  * Update the times on the inode.
2846254Sroot  */
28537737Smckusick /* ARGSUSED */
28637737Smckusick ufs_close(vp, fflag, cred)
28737737Smckusick 	struct vnode *vp;
28837737Smckusick 	int fflag;
28937737Smckusick 	struct ucred *cred;
2906254Sroot {
29137737Smckusick 	register struct inode *ip = VTOI(vp);
2926254Sroot 
29339815Smckusick 	if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED))
29437737Smckusick 		ITIMES(ip, &time, &time);
29537737Smckusick 	return (0);
2966254Sroot }
2976254Sroot 
29837737Smckusick ufs_access(vp, mode, cred)
29937737Smckusick 	struct vnode *vp;
30037737Smckusick 	int mode;
30137737Smckusick 	struct ucred *cred;
3026254Sroot {
3036254Sroot 
30437737Smckusick 	return (iaccess(VTOI(vp), mode, cred));
3056254Sroot }
3066254Sroot 
30737737Smckusick /* ARGSUSED */
30837737Smckusick ufs_getattr(vp, vap, cred)
30937737Smckusick 	struct vnode *vp;
31037737Smckusick 	register struct vattr *vap;
31137737Smckusick 	struct ucred *cred;
3126254Sroot {
31337737Smckusick 	register struct inode *ip = VTOI(vp);
3146254Sroot 
31537737Smckusick 	ITIMES(ip, &time, &time);
3166254Sroot 	/*
31737737Smckusick 	 * Copy from inode table
3186254Sroot 	 */
31937737Smckusick 	vap->va_fsid = ip->i_dev;
32037737Smckusick 	vap->va_fileid = ip->i_number;
32137737Smckusick 	vap->va_mode = ip->i_mode & ~IFMT;
32237737Smckusick 	vap->va_nlink = ip->i_nlink;
32337737Smckusick 	vap->va_uid = ip->i_uid;
32437737Smckusick 	vap->va_gid = ip->i_gid;
32537737Smckusick 	vap->va_rdev = (dev_t)ip->i_rdev;
32639391Smckusick 	vap->va_size = ip->i_din.di_qsize.val[0];
32739391Smckusick 	vap->va_size1 = ip->i_din.di_qsize.val[1];
32837737Smckusick 	vap->va_atime.tv_sec = ip->i_atime;
32938578Smckusick 	vap->va_atime.tv_usec = 0;
33037737Smckusick 	vap->va_mtime.tv_sec = ip->i_mtime;
33138578Smckusick 	vap->va_mtime.tv_usec = 0;
33237737Smckusick 	vap->va_ctime.tv_sec = ip->i_ctime;
33338578Smckusick 	vap->va_ctime.tv_usec = 0;
33438254Smckusick 	vap->va_flags = ip->i_flags;
33538254Smckusick 	vap->va_gen = ip->i_gen;
33637737Smckusick 	/* this doesn't belong here */
33737737Smckusick 	if (vp->v_type == VBLK)
33837737Smckusick 		vap->va_blocksize = BLKDEV_IOSIZE;
33937737Smckusick 	else if (vp->v_type == VCHR)
34037737Smckusick 		vap->va_blocksize = MAXBSIZE;
3417142Smckusick 	else
34237737Smckusick 		vap->va_blocksize = ip->i_fs->fs_bsize;
34338657Smckusick 	vap->va_bytes = dbtob(ip->i_blocks);
34437737Smckusick 	vap->va_bytes1 = -1;
34537737Smckusick 	vap->va_type = vp->v_type;
34637737Smckusick 	return (0);
3476254Sroot }
3486254Sroot 
3496254Sroot /*
35037737Smckusick  * Set attribute vnode op. called from several syscalls
3516254Sroot  */
35237737Smckusick ufs_setattr(vp, vap, cred)
35337737Smckusick 	register struct vnode *vp;
35437737Smckusick 	register struct vattr *vap;
35537737Smckusick 	register struct ucred *cred;
3566254Sroot {
35737737Smckusick 	register struct inode *ip = VTOI(vp);
35837737Smckusick 	int error = 0;
3596254Sroot 
36037737Smckusick 	/*
36137737Smckusick 	 * Check for unsetable attributes.
36237737Smckusick 	 */
36337737Smckusick 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
36437737Smckusick 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
36537737Smckusick 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
36638254Smckusick 	    ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
36737737Smckusick 		return (EINVAL);
36816540Ssam 	}
36937737Smckusick 	/*
37037737Smckusick 	 * Go through the fields and update iff not VNOVAL.
37137737Smckusick 	 */
37237737Smckusick 	if (vap->va_uid != (u_short)VNOVAL || vap->va_gid != (u_short)VNOVAL)
37337737Smckusick 		if (error = chown1(vp, vap->va_uid, vap->va_gid, cred))
37437737Smckusick 			return (error);
37537737Smckusick 	if (vap->va_size != VNOVAL) {
37637737Smckusick 		if (vp->v_type == VDIR)
37737737Smckusick 			return (EISDIR);
37839674Smckusick 		if (error = itrunc(ip, vap->va_size, 0)) /* XXX IO_SYNC? */
37937737Smckusick 			return (error);
38013878Ssam 	}
38137737Smckusick 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
38237773Smckusick 		if (cred->cr_uid != ip->i_uid &&
38337773Smckusick 		    (error = suser(cred, &u.u_acflag)))
38437773Smckusick 			return (error);
38537737Smckusick 		if (vap->va_atime.tv_sec != VNOVAL)
38637737Smckusick 			ip->i_flag |= IACC;
38737737Smckusick 		if (vap->va_mtime.tv_sec != VNOVAL)
38837737Smckusick 			ip->i_flag |= IUPD;
38937737Smckusick 		ip->i_flag |= ICHG;
39037737Smckusick 		if (error = iupdat(ip, &vap->va_atime, &vap->va_mtime, 1))
39137737Smckusick 			return (error);
3926254Sroot 	}
39337737Smckusick 	if (vap->va_mode != (u_short)VNOVAL)
39437737Smckusick 		error = chmod1(vp, (int)vap->va_mode, cred);
39538254Smckusick 	if (vap->va_flags != VNOVAL) {
39638254Smckusick 		if (cred->cr_uid != ip->i_uid &&
39738254Smckusick 		    (error = suser(cred, &u.u_acflag)))
39838254Smckusick 			return (error);
39938254Smckusick 		if (cred->cr_uid == 0) {
40038254Smckusick 			ip->i_flags = vap->va_flags;
40138254Smckusick 		} else {
40238254Smckusick 			ip->i_flags &= 0xffff0000;
40338254Smckusick 			ip->i_flags |= (vap->va_flags & 0xffff);
40438254Smckusick 		}
40538254Smckusick 		ip->i_flag |= ICHG;
40638254Smckusick 	}
40737737Smckusick 	return (error);
4086254Sroot }
4096254Sroot 
4106254Sroot /*
4119167Ssam  * Change the mode on a file.
4129167Ssam  * Inode must be locked before calling.
4139167Ssam  */
41437737Smckusick chmod1(vp, mode, cred)
41537737Smckusick 	register struct vnode *vp;
4167701Ssam 	register int mode;
41737737Smckusick 	struct ucred *cred;
4187701Ssam {
41937737Smckusick 	register struct inode *ip = VTOI(vp);
42037773Smckusick 	int error;
4217868Sroot 
42237773Smckusick 	if (cred->cr_uid != ip->i_uid &&
42337773Smckusick 	    (error = suser(cred, &u.u_acflag)))
42437773Smckusick 		return (error);
4256254Sroot 	ip->i_mode &= ~07777;
42637737Smckusick 	if (cred->cr_uid) {
42737737Smckusick 		if (vp->v_type != VDIR)
42821015Smckusick 			mode &= ~ISVTX;
42937737Smckusick 		if (!groupmember(ip->i_gid, cred))
43011811Ssam 			mode &= ~ISGID;
4317439Sroot 	}
43237737Smckusick 	ip->i_mode |= mode & 07777;
4336254Sroot 	ip->i_flag |= ICHG;
43437737Smckusick 	if ((vp->v_flag & VTEXT) && (ip->i_mode & ISVTX) == 0)
43537737Smckusick 		xrele(vp);
43621015Smckusick 	return (0);
4375992Swnj }
4385992Swnj 
4399167Ssam /*
4407701Ssam  * Perform chown operation on inode ip;
4417701Ssam  * inode must be locked prior to call.
4427701Ssam  */
44337737Smckusick chown1(vp, uid, gid, cred)
44437737Smckusick 	register struct vnode *vp;
44537737Smckusick 	uid_t uid;
44637737Smckusick 	gid_t gid;
44737737Smckusick 	struct ucred *cred;
4487701Ssam {
44937737Smckusick 	register struct inode *ip = VTOI(vp);
4507701Ssam #ifdef QUOTA
4517701Ssam 	register long change;
45211811Ssam #endif
45337737Smckusick 	int error;
4547701Ssam 
45537737Smckusick 	if (uid == (u_short)VNOVAL)
45611811Ssam 		uid = ip->i_uid;
45737737Smckusick 	if (gid == (u_short)VNOVAL)
45811811Ssam 		gid = ip->i_gid;
45936614Sbostic 	/*
46036614Sbostic 	 * If we don't own the file, are trying to change the owner
46136614Sbostic 	 * of the file, or are not a member of the target group,
46236614Sbostic 	 * the caller must be superuser or the call fails.
46336614Sbostic 	 */
46437737Smckusick 	if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
46537737Smckusick 	    !groupmember((gid_t)gid, cred)) &&
46637737Smckusick 	    (error = suser(cred, &u.u_acflag)))
46737737Smckusick 		return (error);
46811811Ssam #ifdef QUOTA
46914385Ssam 	if (ip->i_uid == uid)		/* this just speeds things a little */
4707482Skre 		change = 0;
47112646Ssam 	else
47212646Ssam 		change = ip->i_blocks;
47312646Ssam 	(void) chkdq(ip, -change, 1);
47412646Ssam 	(void) chkiq(ip->i_dev, ip, ip->i_uid, 1);
4757482Skre 	dqrele(ip->i_dquot);
4767482Skre #endif
47739306Smckusick 	if (ip->i_uid != uid && cred->cr_uid != 0)
47839306Smckusick 		ip->i_mode &= ~ISUID;
47939306Smckusick 	if (ip->i_gid != gid && cred->cr_uid != 0)
48039306Smckusick 		ip->i_mode &= ~ISGID;
48111811Ssam 	ip->i_uid = uid;
48211811Ssam 	ip->i_gid = gid;
4836254Sroot 	ip->i_flag |= ICHG;
4847701Ssam #ifdef QUOTA
4857482Skre 	ip->i_dquot = inoquota(ip);
48612646Ssam 	(void) chkdq(ip, change, 1);
48726361Skarels 	(void) chkiq(ip->i_dev, (struct inode *)NULL, (uid_t)uid, 1);
48812646Ssam 	return (u.u_error);		/* should == 0 ALWAYS !! */
48912646Ssam #else
49012646Ssam 	return (0);
4917482Skre #endif
49237Sbill }
49337Sbill 
49439608Smckusick /*
49539608Smckusick  * Vnode op for reading.
49639608Smckusick  */
49737737Smckusick /* ARGSUSED */
49839608Smckusick ufs_read(vp, uio, ioflag, cred)
49939608Smckusick 	struct vnode *vp;
50039608Smckusick 	register struct uio *uio;
50139608Smckusick 	int ioflag;
50239608Smckusick 	struct ucred *cred;
50339608Smckusick {
50439608Smckusick 	register struct inode *ip = VTOI(vp);
50539608Smckusick 	register struct fs *fs;
50639608Smckusick 	struct buf *bp;
50739608Smckusick 	daddr_t lbn, bn, rablock;
50839896Smckusick 	int size, diff, error = 0;
50939608Smckusick 	long n, on, type;
51039608Smckusick 
51139608Smckusick 	if (uio->uio_rw != UIO_READ)
51239608Smckusick 		panic("ufs_read mode");
51339608Smckusick 	type = ip->i_mode & IFMT;
51439608Smckusick 	if (type != IFDIR && type != IFREG && type != IFLNK)
51539608Smckusick 		panic("ufs_read type");
51639608Smckusick 	if (uio->uio_resid == 0)
51739608Smckusick 		return (0);
51839608Smckusick 	if (uio->uio_offset < 0)
51939608Smckusick 		return (EINVAL);
52039608Smckusick 	ip->i_flag |= IACC;
52139608Smckusick 	fs = ip->i_fs;
52239608Smckusick 	do {
52339608Smckusick 		lbn = lblkno(fs, uio->uio_offset);
52439608Smckusick 		on = blkoff(fs, uio->uio_offset);
52539608Smckusick 		n = MIN((unsigned)(fs->fs_bsize - on), uio->uio_resid);
52639608Smckusick 		diff = ip->i_size - uio->uio_offset;
52739608Smckusick 		if (diff <= 0)
52839608Smckusick 			return (0);
52939608Smckusick 		if (diff < n)
53039608Smckusick 			n = diff;
53139608Smckusick 		size = blksize(fs, ip, lbn);
53239674Smckusick 		rablock = lbn + 1;
53339896Smckusick 		if (vp->v_lastr + 1 == lbn &&
53439896Smckusick 		    lblktosize(fs, rablock) < ip->i_size)
53539896Smckusick 			error = breada(ITOV(ip), lbn, size, rablock,
53639896Smckusick 				blksize(fs, ip, rablock), NOCRED, &bp);
53739608Smckusick 		else
53839674Smckusick 			error = bread(ITOV(ip), lbn, size, NOCRED, &bp);
53939815Smckusick 		vp->v_lastr = lbn;
54039608Smckusick 		n = MIN(n, size - bp->b_resid);
54139608Smckusick 		if (error) {
54239608Smckusick 			brelse(bp);
54339608Smckusick 			return (error);
54439608Smckusick 		}
54539608Smckusick 		error = uiomove(bp->b_un.b_addr + on, (int)n, uio);
54639608Smckusick 		if (n + on == fs->fs_bsize || uio->uio_offset == ip->i_size)
54739608Smckusick 			bp->b_flags |= B_AGE;
54839608Smckusick 		brelse(bp);
54939608Smckusick 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
55039608Smckusick 	return (error);
55139608Smckusick }
55239608Smckusick 
55339608Smckusick /*
55439608Smckusick  * Vnode op for writing.
55539608Smckusick  */
55639608Smckusick ufs_write(vp, uio, ioflag, cred)
55739608Smckusick 	register struct vnode *vp;
55839608Smckusick 	struct uio *uio;
55939608Smckusick 	int ioflag;
56039608Smckusick 	struct ucred *cred;
56139608Smckusick {
56239608Smckusick 	register struct inode *ip = VTOI(vp);
56339608Smckusick 	register struct fs *fs;
56439608Smckusick 	struct buf *bp;
56539608Smckusick 	daddr_t lbn, bn;
56639608Smckusick 	u_long osize;
56739608Smckusick 	int i, n, on, flags;
56839608Smckusick 	int count, size, resid, error = 0;
56939608Smckusick 
57039608Smckusick 	if (uio->uio_rw != UIO_WRITE)
57139608Smckusick 		panic("ufs_write mode");
57239608Smckusick 	switch (vp->v_type) {
57339608Smckusick 	case VREG:
57439608Smckusick 		if (ioflag & IO_APPEND)
57539608Smckusick 			uio->uio_offset = ip->i_size;
57639608Smckusick 		/* fall through */
57739608Smckusick 	case VLNK:
57839608Smckusick 		break;
57939608Smckusick 
58039608Smckusick 	case VDIR:
58139608Smckusick 		if ((ioflag & IO_SYNC) == 0)
58239608Smckusick 			panic("ufs_write nonsync dir write");
58339608Smckusick 		break;
58439608Smckusick 
58539608Smckusick 	default:
58639608Smckusick 		panic("ufs_write type");
58739608Smckusick 	}
58839608Smckusick 	if (uio->uio_offset < 0)
58939608Smckusick 		return (EINVAL);
59039608Smckusick 	if (uio->uio_resid == 0)
59139608Smckusick 		return (0);
59239608Smckusick 	/*
59339608Smckusick 	 * Maybe this should be above the vnode op call, but so long as
59439608Smckusick 	 * file servers have no limits, i don't think it matters
59539608Smckusick 	 */
59639608Smckusick 	if (vp->v_type == VREG &&
59739608Smckusick 	    uio->uio_offset + uio->uio_resid >
59839608Smckusick 	      u.u_rlimit[RLIMIT_FSIZE].rlim_cur) {
59939608Smckusick 		psignal(u.u_procp, SIGXFSZ);
60039608Smckusick 		return (EFBIG);
60139608Smckusick 	}
60239608Smckusick 	resid = uio->uio_resid;
60339608Smckusick 	osize = ip->i_size;
60439608Smckusick 	fs = ip->i_fs;
60539674Smckusick 	flags = 0;
60639674Smckusick 	if (ioflag & IO_SYNC)
60739674Smckusick 		flags = B_SYNC;
60839608Smckusick 	do {
60939608Smckusick 		lbn = lblkno(fs, uio->uio_offset);
61039608Smckusick 		on = blkoff(fs, uio->uio_offset);
61139608Smckusick 		n = MIN((unsigned)(fs->fs_bsize - on), uio->uio_resid);
61239608Smckusick 		if (n < fs->fs_bsize)
61339674Smckusick 			flags |= B_CLRBUF;
61439608Smckusick 		else
61539674Smckusick 			flags &= ~B_CLRBUF;
61639674Smckusick 		if (error = balloc(ip, lbn, (int)(on + n), &bp, flags))
61739608Smckusick 			break;
61839674Smckusick 		bn = bp->b_blkno;
61939608Smckusick 		if (uio->uio_offset + n > ip->i_size)
62039608Smckusick 			ip->i_size = uio->uio_offset + n;
62139608Smckusick 		size = blksize(fs, ip, lbn);
62239608Smckusick 		count = howmany(size, CLBYTES);
62339608Smckusick 		for (i = 0; i < count; i++)
62439608Smckusick 			munhash(ip->i_devvp, bn + i * CLBYTES / DEV_BSIZE);
62539608Smckusick 		n = MIN(n, size - bp->b_resid);
62639608Smckusick 		error = uiomove(bp->b_un.b_addr + on, n, uio);
62739608Smckusick 		if (ioflag & IO_SYNC)
62839608Smckusick 			(void) bwrite(bp);
62939608Smckusick 		else if (n + on == fs->fs_bsize) {
63039608Smckusick 			bp->b_flags |= B_AGE;
63139608Smckusick 			bawrite(bp);
63239608Smckusick 		} else
63339608Smckusick 			bdwrite(bp);
63439608Smckusick 		ip->i_flag |= IUPD|ICHG;
63539608Smckusick 		if (cred->cr_uid != 0)
63639608Smckusick 			ip->i_mode &= ~(ISUID|ISGID);
63739608Smckusick 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
63839608Smckusick 	if (error && (ioflag & IO_UNIT)) {
63939674Smckusick 		(void) itrunc(ip, osize, ioflag & IO_SYNC);
64039608Smckusick 		uio->uio_offset -= resid - uio->uio_resid;
64139608Smckusick 		uio->uio_resid = resid;
64239608Smckusick 	}
64339608Smckusick 	return (error);
64439608Smckusick }
64539608Smckusick 
64639608Smckusick /* ARGSUSED */
64737737Smckusick ufs_ioctl(vp, com, data, fflag, cred)
64837737Smckusick 	struct vnode *vp;
64937737Smckusick 	int com;
65037737Smckusick 	caddr_t data;
65137737Smckusick 	int fflag;
65237737Smckusick 	struct ucred *cred;
65311811Ssam {
65411811Ssam 
65537737Smckusick 	return (ENOTTY);
65611811Ssam }
65711811Ssam 
65837737Smckusick /* ARGSUSED */
65940290Smckusick ufs_select(vp, which, fflags, cred)
66037737Smckusick 	struct vnode *vp;
66140290Smckusick 	int which, fflags;
66237737Smckusick 	struct ucred *cred;
66337737Smckusick {
66437737Smckusick 
66537737Smckusick 	return (1);		/* XXX */
66637737Smckusick }
66737737Smckusick 
6689167Ssam /*
66937737Smckusick  * Mmap a file
67037737Smckusick  *
67137737Smckusick  * NB Currently unsupported.
6729167Ssam  */
67337737Smckusick /* ARGSUSED */
67437737Smckusick ufs_mmap(vp, fflags, cred)
67537737Smckusick 	struct vnode *vp;
67637737Smckusick 	int fflags;
67737737Smckusick 	struct ucred *cred;
67837Sbill {
67937Sbill 
68037737Smckusick 	return (EINVAL);
68137Sbill }
6827535Sroot 
6839167Ssam /*
68437737Smckusick  * Synch an open file.
6859167Ssam  */
68637737Smckusick /* ARGSUSED */
68739597Smckusick ufs_fsync(vp, fflags, cred, waitfor)
68837737Smckusick 	struct vnode *vp;
68937737Smckusick 	int fflags;
69037737Smckusick 	struct ucred *cred;
69139597Smckusick 	int waitfor;
6927701Ssam {
69339597Smckusick 	struct inode *ip = VTOI(vp);
6947701Ssam 
69537737Smckusick 	if (fflags&FWRITE)
69637737Smckusick 		ip->i_flag |= ICHG;
69739674Smckusick 	vflushbuf(vp, waitfor == MNT_WAIT ? B_SYNC : 0);
69839674Smckusick 	return (iupdat(ip, &time, &time, waitfor == MNT_WAIT));
6997701Ssam }
7007701Ssam 
7019167Ssam /*
70237737Smckusick  * Seek on a file
70337737Smckusick  *
70437737Smckusick  * Nothing to do, so just return.
7059167Ssam  */
70637737Smckusick /* ARGSUSED */
70737737Smckusick ufs_seek(vp, oldoff, newoff, cred)
70837737Smckusick 	struct vnode *vp;
70937737Smckusick 	off_t oldoff, newoff;
71037737Smckusick 	struct ucred *cred;
7117701Ssam {
7127701Ssam 
71337737Smckusick 	return (0);
71437737Smckusick }
71537737Smckusick 
71637737Smckusick /*
71737737Smckusick  * ufs remove
71837737Smckusick  * Hard to avoid races here, especially
71937737Smckusick  * in unlinking directories.
72037737Smckusick  */
72137737Smckusick ufs_remove(ndp)
72237737Smckusick 	struct nameidata *ndp;
72337737Smckusick {
72437737Smckusick 	register struct inode *ip, *dp;
72537737Smckusick 	int error;
72637737Smckusick 
72737737Smckusick 	ip = VTOI(ndp->ni_vp);
72837737Smckusick 	dp = VTOI(ndp->ni_dvp);
72937737Smckusick 	error = dirremove(ndp);
73037737Smckusick 	if (!error) {
73137737Smckusick 		ip->i_nlink--;
73237737Smckusick 		ip->i_flag |= ICHG;
7337701Ssam 	}
73437737Smckusick 	if (dp == ip)
73537737Smckusick 		vrele(ITOV(ip));
73637737Smckusick 	else
73737737Smckusick 		iput(ip);
73837737Smckusick 	iput(dp);
73937737Smckusick 	return (error);
7407701Ssam }
7417701Ssam 
7429167Ssam /*
74337737Smckusick  * link vnode call
7449167Ssam  */
74537737Smckusick ufs_link(vp, ndp)
74637737Smckusick 	register struct vnode *vp;
74737737Smckusick 	register struct nameidata *ndp;
7489167Ssam {
74937737Smckusick 	register struct inode *ip = VTOI(vp);
75037737Smckusick 	int error;
7519167Ssam 
75237737Smckusick 	if (ndp->ni_dvp != vp)
75337737Smckusick 		ILOCK(ip);
75437737Smckusick 	if (ip->i_nlink == LINK_MAX - 1) {
75537737Smckusick 		error = EMLINK;
75637737Smckusick 		goto out;
75737737Smckusick 	}
75837737Smckusick 	ip->i_nlink++;
75937737Smckusick 	ip->i_flag |= ICHG;
76037737Smckusick 	error = iupdat(ip, &time, &time, 1);
76137737Smckusick 	if (!error)
76237737Smckusick 		error = direnter(ip, ndp);
76337737Smckusick out:
76437737Smckusick 	if (ndp->ni_dvp != vp)
76537737Smckusick 		IUNLOCK(ip);
76637737Smckusick 	if (error) {
76737737Smckusick 		ip->i_nlink--;
76830598Smckusick 		ip->i_flag |= ICHG;
76937737Smckusick 	}
77037737Smckusick 	return (error);
7719167Ssam }
7729167Ssam 
7739167Ssam /*
7749167Ssam  * Rename system call.
7759167Ssam  * 	rename("foo", "bar");
7769167Ssam  * is essentially
7779167Ssam  *	unlink("bar");
7789167Ssam  *	link("foo", "bar");
7799167Ssam  *	unlink("foo");
7809167Ssam  * but ``atomically''.  Can't do full commit without saving state in the
7819167Ssam  * inode on disk which isn't feasible at this time.  Best we can do is
7829167Ssam  * always guarantee the target exists.
7839167Ssam  *
7849167Ssam  * Basic algorithm is:
7859167Ssam  *
7869167Ssam  * 1) Bump link count on source while we're linking it to the
78737737Smckusick  *    target.  This also ensure the inode won't be deleted out
78816776Smckusick  *    from underneath us while we work (it may be truncated by
78916776Smckusick  *    a concurrent `trunc' or `open' for creation).
7909167Ssam  * 2) Link source to destination.  If destination already exists,
7919167Ssam  *    delete it first.
79216776Smckusick  * 3) Unlink source reference to inode if still around. If a
79316776Smckusick  *    directory was moved and the parent of the destination
7949167Ssam  *    is different from the source, patch the ".." entry in the
7959167Ssam  *    directory.
7969167Ssam  */
79737737Smckusick ufs_rename(fndp, tndp)
79837737Smckusick 	register struct nameidata *fndp, *tndp;
7997701Ssam {
8009167Ssam 	register struct inode *ip, *xp, *dp;
80116776Smckusick 	struct dirtemplate dirbuf;
80216776Smckusick 	int doingdirectory = 0, oldparent = 0, newparent = 0;
80310051Ssam 	int error = 0;
8047701Ssam 
80537737Smckusick 	dp = VTOI(fndp->ni_dvp);
80637737Smckusick 	ip = VTOI(fndp->ni_vp);
80737737Smckusick 	ILOCK(ip);
8089167Ssam 	if ((ip->i_mode&IFMT) == IFDIR) {
80937737Smckusick 		register struct direct *d = &fndp->ni_dent;
8109167Ssam 
8119167Ssam 		/*
81211641Ssam 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
8139167Ssam 		 */
81437737Smckusick 		if ((d->d_namlen == 1 && d->d_name[0] == '.') || dp == ip ||
81537737Smckusick 		    fndp->ni_isdotdot || (ip->i_flag & IRENAME)) {
81637737Smckusick 			IUNLOCK(ip);
81737737Smckusick 			ufs_abortop(fndp);
81837737Smckusick 			ufs_abortop(tndp);
81937737Smckusick 			return (EINVAL);
8209167Ssam 		}
82116776Smckusick 		ip->i_flag |= IRENAME;
8229167Ssam 		oldparent = dp->i_number;
8239167Ssam 		doingdirectory++;
8249167Ssam 	}
82537737Smckusick 	vrele(fndp->ni_dvp);
8269167Ssam 
8279167Ssam 	/*
8289167Ssam 	 * 1) Bump link count while we're moving stuff
8299167Ssam 	 *    around.  If we crash somewhere before
8309167Ssam 	 *    completing our work, the link count
8319167Ssam 	 *    may be wrong, but correctable.
8329167Ssam 	 */
8339167Ssam 	ip->i_nlink++;
8349167Ssam 	ip->i_flag |= ICHG;
83537737Smckusick 	error = iupdat(ip, &time, &time, 1);
83616664Smckusick 	IUNLOCK(ip);
8379167Ssam 
8389167Ssam 	/*
8399167Ssam 	 * When the target exists, both the directory
84037737Smckusick 	 * and target vnodes are returned locked.
8419167Ssam 	 */
84237737Smckusick 	dp = VTOI(tndp->ni_dvp);
84337737Smckusick 	xp = NULL;
84437737Smckusick 	if (tndp->ni_vp)
84537737Smckusick 		xp = VTOI(tndp->ni_vp);
8469167Ssam 	/*
84711641Ssam 	 * If ".." must be changed (ie the directory gets a new
84812816Smckusick 	 * parent) then the source directory must not be in the
84912816Smckusick 	 * directory heirarchy above the target, as this would
85012816Smckusick 	 * orphan everything below the source directory. Also
85112816Smckusick 	 * the user must have write permission in the source so
85212816Smckusick 	 * as to be able to change "..". We must repeat the call
85312816Smckusick 	 * to namei, as the parent directory is unlocked by the
85412816Smckusick 	 * call to checkpath().
85511641Ssam 	 */
85616776Smckusick 	if (oldparent != dp->i_number)
85716776Smckusick 		newparent = dp->i_number;
85816776Smckusick 	if (doingdirectory && newparent) {
85937737Smckusick 		if (error = iaccess(ip, IWRITE, tndp->ni_cred))
86012816Smckusick 			goto bad;
86137737Smckusick 		tndp->ni_nameiop = RENAME | LOCKPARENT | LOCKLEAF | NOCACHE;
86212816Smckusick 		do {
86337737Smckusick 			dp = VTOI(tndp->ni_dvp);
86412816Smckusick 			if (xp != NULL)
86538069Smckusick 				iput(xp);
86637737Smckusick 			if (error = checkpath(ip, dp, tndp->ni_cred))
86712816Smckusick 				goto out;
86837737Smckusick 			if (error = namei(tndp))
86912816Smckusick 				goto out;
87037737Smckusick 			xp = NULL;
87137737Smckusick 			if (tndp->ni_vp)
87237737Smckusick 				xp = VTOI(tndp->ni_vp);
87337737Smckusick 		} while (dp != VTOI(tndp->ni_dvp));
87412816Smckusick 	}
87511641Ssam 	/*
8769167Ssam 	 * 2) If target doesn't exist, link the target
8779167Ssam 	 *    to the source and unlink the source.
8789167Ssam 	 *    Otherwise, rewrite the target directory
8799167Ssam 	 *    entry to reference the source inode and
8809167Ssam 	 *    expunge the original entry's existence.
8819167Ssam 	 */
8829167Ssam 	if (xp == NULL) {
88337737Smckusick 		if (dp->i_dev != ip->i_dev)
88437737Smckusick 			panic("rename: EXDEV");
8859167Ssam 		/*
88616776Smckusick 		 * Account for ".." in new directory.
88716776Smckusick 		 * When source and destination have the same
88816776Smckusick 		 * parent we don't fool with the link count.
8899167Ssam 		 */
89016776Smckusick 		if (doingdirectory && newparent) {
8919167Ssam 			dp->i_nlink++;
8929167Ssam 			dp->i_flag |= ICHG;
89337737Smckusick 			error = iupdat(dp, &time, &time, 1);
8949167Ssam 		}
89537737Smckusick 		if (error = direnter(ip, tndp))
8969167Ssam 			goto out;
8979167Ssam 	} else {
89837737Smckusick 		if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
89937737Smckusick 			panic("rename: EXDEV");
9009167Ssam 		/*
90110590Ssam 		 * Short circuit rename(foo, foo).
90210590Ssam 		 */
90310590Ssam 		if (xp->i_number == ip->i_number)
90437737Smckusick 			panic("rename: same file");
90510590Ssam 		/*
90624433Sbloom 		 * If the parent directory is "sticky", then the user must
90724433Sbloom 		 * own the parent directory, or the destination of the rename,
90824433Sbloom 		 * otherwise the destination may not be changed (except by
90924433Sbloom 		 * root). This implements append-only directories.
91024433Sbloom 		 */
91137737Smckusick 		if ((dp->i_mode & ISVTX) && tndp->ni_cred->cr_uid != 0 &&
91237737Smckusick 		    tndp->ni_cred->cr_uid != dp->i_uid &&
91337737Smckusick 		    xp->i_uid != tndp->ni_cred->cr_uid) {
91424433Sbloom 			error = EPERM;
91524433Sbloom 			goto bad;
91624433Sbloom 		}
91724433Sbloom 		/*
91810051Ssam 		 * Target must be empty if a directory
91910051Ssam 		 * and have no links to it.
9209167Ssam 		 * Also, insure source and target are
9219167Ssam 		 * compatible (both directories, or both
9229167Ssam 		 * not directories).
9239167Ssam 		 */
9249167Ssam 		if ((xp->i_mode&IFMT) == IFDIR) {
92537737Smckusick 			if (!dirempty(xp, dp->i_number, tndp->ni_cred) ||
92637737Smckusick 			    xp->i_nlink > 2) {
92710051Ssam 				error = ENOTEMPTY;
9289167Ssam 				goto bad;
9299167Ssam 			}
9309167Ssam 			if (!doingdirectory) {
93110051Ssam 				error = ENOTDIR;
9329167Ssam 				goto bad;
9339167Ssam 			}
93437737Smckusick 			cache_purge(ITOV(dp));
9359167Ssam 		} else if (doingdirectory) {
93610051Ssam 			error = EISDIR;
9379167Ssam 			goto bad;
9389167Ssam 		}
93937737Smckusick 		if (error = dirrewrite(dp, ip, tndp))
94037737Smckusick 			goto bad;
94137737Smckusick 		vput(ITOV(dp));
9429167Ssam 		/*
94310051Ssam 		 * Adjust the link count of the target to
94410051Ssam 		 * reflect the dirrewrite above.  If this is
94510051Ssam 		 * a directory it is empty and there are
94610051Ssam 		 * no links to it, so we can squash the inode and
94710051Ssam 		 * any space associated with it.  We disallowed
94810051Ssam 		 * renaming over top of a directory with links to
94916776Smckusick 		 * it above, as the remaining link would point to
95016776Smckusick 		 * a directory without "." or ".." entries.
9519167Ssam 		 */
95210051Ssam 		xp->i_nlink--;
9539167Ssam 		if (doingdirectory) {
95410051Ssam 			if (--xp->i_nlink != 0)
95510051Ssam 				panic("rename: linked directory");
95639674Smckusick 			error = itrunc(xp, (u_long)0, IO_SYNC);
95710051Ssam 		}
9589167Ssam 		xp->i_flag |= ICHG;
95938398Smckusick 		iput(xp);
96010246Ssam 		xp = NULL;
9619167Ssam 	}
9629167Ssam 
9639167Ssam 	/*
9649167Ssam 	 * 3) Unlink the source.
9659167Ssam 	 */
96637737Smckusick 	fndp->ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF;
96737737Smckusick 	(void)namei(fndp);
96837737Smckusick 	if (fndp->ni_vp != NULL) {
96937737Smckusick 		xp = VTOI(fndp->ni_vp);
97037737Smckusick 		dp = VTOI(fndp->ni_dvp);
97137737Smckusick 	} else {
97238069Smckusick 		if (fndp->ni_dvp != NULL)
97338069Smckusick 			vput(fndp->ni_dvp);
97437737Smckusick 		xp = NULL;
97517758Smckusick 		dp = NULL;
97637737Smckusick 	}
9779167Ssam 	/*
97837737Smckusick 	 * Ensure that the directory entry still exists and has not
97916776Smckusick 	 * changed while the new name has been entered. If the source is
98016776Smckusick 	 * a file then the entry may have been unlinked or renamed. In
98116776Smckusick 	 * either case there is no further work to be done. If the source
98216776Smckusick 	 * is a directory then it cannot have been rmdir'ed; its link
98316776Smckusick 	 * count of three would cause a rmdir to fail with ENOTEMPTY.
98437737Smckusick 	 * The IRENAME flag ensures that it cannot be moved by another
98516776Smckusick 	 * rename.
9869167Ssam 	 */
98717758Smckusick 	if (xp != ip) {
98816776Smckusick 		if (doingdirectory)
98917758Smckusick 			panic("rename: lost dir entry");
99016776Smckusick 	} else {
9919167Ssam 		/*
99216776Smckusick 		 * If the source is a directory with a
99316776Smckusick 		 * new parent, the link count of the old
99416776Smckusick 		 * parent directory must be decremented
99516776Smckusick 		 * and ".." set to point to the new parent.
9969167Ssam 		 */
99716776Smckusick 		if (doingdirectory && newparent) {
9989167Ssam 			dp->i_nlink--;
9999167Ssam 			dp->i_flag |= ICHG;
100039597Smckusick 			error = vn_rdwr(UIO_READ, ITOV(xp), (caddr_t)&dirbuf,
100137737Smckusick 				sizeof (struct dirtemplate), (off_t)0,
100239597Smckusick 				UIO_SYSSPACE, IO_NODELOCKED,
100339597Smckusick 				tndp->ni_cred, (int *)0);
100416776Smckusick 			if (error == 0) {
100516776Smckusick 				if (dirbuf.dotdot_namlen != 2 ||
100616776Smckusick 				    dirbuf.dotdot_name[0] != '.' ||
100716776Smckusick 				    dirbuf.dotdot_name[1] != '.') {
100839610Smckusick 					dirbad(xp, 12, "rename: mangled dir");
100916776Smckusick 				} else {
101016776Smckusick 					dirbuf.dotdot_ino = newparent;
101139597Smckusick 					(void) vn_rdwr(UIO_WRITE, ITOV(xp),
101216776Smckusick 					    (caddr_t)&dirbuf,
101316776Smckusick 					    sizeof (struct dirtemplate),
101437740Smckusick 					    (off_t)0, UIO_SYSSPACE,
101539597Smckusick 					    IO_NODELOCKED|IO_SYNC,
101637737Smckusick 					    tndp->ni_cred, (int *)0);
101737737Smckusick 					cache_purge(ITOV(dp));
101816776Smckusick 				}
101916776Smckusick 			}
10209167Ssam 		}
102137737Smckusick 		error = dirremove(fndp);
102237737Smckusick 		if (!error) {
102316776Smckusick 			xp->i_nlink--;
102416776Smckusick 			xp->i_flag |= ICHG;
10259167Ssam 		}
102616776Smckusick 		xp->i_flag &= ~IRENAME;
10279167Ssam 	}
10289167Ssam 	if (dp)
102937737Smckusick 		vput(ITOV(dp));
103016776Smckusick 	if (xp)
103137737Smckusick 		vput(ITOV(xp));
103237737Smckusick 	vrele(ITOV(ip));
103337737Smckusick 	return (error);
10349167Ssam 
10359167Ssam bad:
10369167Ssam 	if (xp)
103737737Smckusick 		vput(ITOV(xp));
103837737Smckusick 	vput(ITOV(dp));
10399167Ssam out:
10409167Ssam 	ip->i_nlink--;
10419167Ssam 	ip->i_flag |= ICHG;
104237737Smckusick 	vrele(ITOV(ip));
104337737Smckusick 	return (error);
10447701Ssam }
10457701Ssam 
10467535Sroot /*
104712756Ssam  * A virgin directory (no blushing please).
104812756Ssam  */
104912756Ssam struct dirtemplate mastertemplate = {
105012756Ssam 	0, 12, 1, ".",
105112756Ssam 	0, DIRBLKSIZ - 12, 2, ".."
105212756Ssam };
105312756Ssam 
105412756Ssam /*
105512756Ssam  * Mkdir system call
105612756Ssam  */
105737737Smckusick ufs_mkdir(ndp, vap)
105837737Smckusick 	struct nameidata *ndp;
105937737Smckusick 	struct vattr *vap;
106012756Ssam {
106112756Ssam 	register struct inode *ip, *dp;
106237737Smckusick 	struct inode *tip;
106337737Smckusick 	struct vnode *dvp;
106412756Ssam 	struct dirtemplate dirtemplate;
106537737Smckusick 	int error;
106637737Smckusick 	int dmode;
106712756Ssam 
106837737Smckusick 	dvp = ndp->ni_dvp;
106937737Smckusick 	dp = VTOI(dvp);
107037737Smckusick 	dmode = vap->va_mode&0777;
107137737Smckusick 	dmode |= IFDIR;
107212756Ssam 	/*
107312756Ssam 	 * Must simulate part of maknode here
107412756Ssam 	 * in order to acquire the inode, but
107512756Ssam 	 * not have it entered in the parent
107612756Ssam 	 * directory.  The entry is made later
107712756Ssam 	 * after writing "." and ".." entries out.
107812756Ssam 	 */
107937737Smckusick 	error = ialloc(dp, dirpref(dp->i_fs), dmode, &tip);
108037737Smckusick 	if (error) {
108112756Ssam 		iput(dp);
108237737Smckusick 		return (error);
108312756Ssam 	}
108437737Smckusick 	ip = tip;
108512756Ssam #ifdef QUOTA
108612756Ssam 	if (ip->i_dquot != NODQUOT)
108712756Ssam 		panic("mkdir: dquot");
108812756Ssam #endif
108912756Ssam 	ip->i_flag |= IACC|IUPD|ICHG;
109037737Smckusick 	ip->i_mode = dmode;
109137737Smckusick 	ITOV(ip)->v_type = VDIR;	/* Rest init'd in iget() */
109212756Ssam 	ip->i_nlink = 2;
109337737Smckusick 	ip->i_uid = ndp->ni_cred->cr_uid;
109412756Ssam 	ip->i_gid = dp->i_gid;
109512756Ssam #ifdef QUOTA
109612756Ssam 	ip->i_dquot = inoquota(ip);
109712756Ssam #endif
109837737Smckusick 	error = iupdat(ip, &time, &time, 1);
109912756Ssam 
110012756Ssam 	/*
110112756Ssam 	 * Bump link count in parent directory
110212756Ssam 	 * to reflect work done below.  Should
110312756Ssam 	 * be done before reference is created
110412756Ssam 	 * so reparation is possible if we crash.
110512756Ssam 	 */
110612756Ssam 	dp->i_nlink++;
110712756Ssam 	dp->i_flag |= ICHG;
110837737Smckusick 	error = iupdat(dp, &time, &time, 1);
110912756Ssam 
111012756Ssam 	/*
111112756Ssam 	 * Initialize directory with "."
111212756Ssam 	 * and ".." from static template.
111312756Ssam 	 */
111412756Ssam 	dirtemplate = mastertemplate;
111512756Ssam 	dirtemplate.dot_ino = ip->i_number;
111612756Ssam 	dirtemplate.dotdot_ino = dp->i_number;
111739597Smckusick 	error = vn_rdwr(UIO_WRITE, ITOV(ip), (caddr_t)&dirtemplate,
111837737Smckusick 		sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
111939597Smckusick 		IO_NODELOCKED|IO_SYNC, ndp->ni_cred, (int *)0);
112037737Smckusick 	if (error) {
112112756Ssam 		dp->i_nlink--;
112212756Ssam 		dp->i_flag |= ICHG;
112312756Ssam 		goto bad;
112412756Ssam 	}
112537737Smckusick 	if (DIRBLKSIZ > dp->i_fs->fs_fsize)
112637737Smckusick 		panic("mkdir: blksize");     /* XXX - should grow w/balloc() */
112718103Smckusick 	else
112818103Smckusick 		ip->i_size = DIRBLKSIZ;
112912756Ssam 	/*
113012756Ssam 	 * Directory all set up, now
113112756Ssam 	 * install the entry for it in
113212756Ssam 	 * the parent directory.
113312756Ssam 	 */
113437737Smckusick 	error = direnter(ip, ndp);
113512756Ssam 	dp = NULL;
113637737Smckusick 	if (error) {
113716694Smckusick 		ndp->ni_nameiop = LOOKUP | NOCACHE;
113837737Smckusick 		error = namei(ndp);
113937737Smckusick 		if (!error) {
114037737Smckusick 			dp = VTOI(ndp->ni_vp);
114112756Ssam 			dp->i_nlink--;
114212756Ssam 			dp->i_flag |= ICHG;
114312756Ssam 		}
114412756Ssam 	}
114512756Ssam bad:
114612756Ssam 	/*
114712756Ssam 	 * No need to do an explicit itrunc here,
114837737Smckusick 	 * vrele will do this for us because we set
114912756Ssam 	 * the link count to 0.
115012756Ssam 	 */
115137737Smckusick 	if (error) {
115212756Ssam 		ip->i_nlink = 0;
115312756Ssam 		ip->i_flag |= ICHG;
115438144Smckusick 		iput(ip);
115538144Smckusick 	} else
115638144Smckusick 		ndp->ni_vp = ITOV(ip);
115712756Ssam 	if (dp)
115812756Ssam 		iput(dp);
115937737Smckusick 	return (error);
116012756Ssam }
116112756Ssam 
116212756Ssam /*
116312756Ssam  * Rmdir system call.
116412756Ssam  */
116537737Smckusick ufs_rmdir(ndp)
116637737Smckusick 	register struct nameidata *ndp;
116712756Ssam {
116812756Ssam 	register struct inode *ip, *dp;
116937737Smckusick 	int error = 0;
117012756Ssam 
117137737Smckusick 	ip = VTOI(ndp->ni_vp);
117237737Smckusick 	dp = VTOI(ndp->ni_dvp);
117312756Ssam 	/*
117412756Ssam 	 * No rmdir "." please.
117512756Ssam 	 */
117612756Ssam 	if (dp == ip) {
117737737Smckusick 		vrele(ITOV(dp));
117812756Ssam 		iput(ip);
117937737Smckusick 		return (EINVAL);
118012756Ssam 	}
118112756Ssam 	/*
118212756Ssam 	 * Verify the directory is empty (and valid).
118312756Ssam 	 * (Rmdir ".." won't be valid since
118412756Ssam 	 *  ".." will contain a reference to
118512756Ssam 	 *  the current directory and thus be
118612756Ssam 	 *  non-empty.)
118712756Ssam 	 */
118837737Smckusick 	if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number, ndp->ni_cred)) {
118937737Smckusick 		error = ENOTEMPTY;
119012756Ssam 		goto out;
119112756Ssam 	}
119212756Ssam 	/*
119312756Ssam 	 * Delete reference to directory before purging
119412756Ssam 	 * inode.  If we crash in between, the directory
119512756Ssam 	 * will be reattached to lost+found,
119612756Ssam 	 */
119737737Smckusick 	if (error = dirremove(ndp))
119812756Ssam 		goto out;
119912756Ssam 	dp->i_nlink--;
120012756Ssam 	dp->i_flag |= ICHG;
120137737Smckusick 	cache_purge(ITOV(dp));
120212756Ssam 	iput(dp);
120337737Smckusick 	ndp->ni_dvp = NULL;
120412756Ssam 	/*
120512756Ssam 	 * Truncate inode.  The only stuff left
120612756Ssam 	 * in the directory is "." and "..".  The
120712756Ssam 	 * "." reference is inconsequential since
120812756Ssam 	 * we're quashing it.  The ".." reference
120912756Ssam 	 * has already been adjusted above.  We've
121012756Ssam 	 * removed the "." reference and the reference
121112756Ssam 	 * in the parent directory, but there may be
121212756Ssam 	 * other hard links so decrement by 2 and
121312756Ssam 	 * worry about them later.
121412756Ssam 	 */
121512756Ssam 	ip->i_nlink -= 2;
121639674Smckusick 	error = itrunc(ip, (u_long)0, IO_SYNC);
121737737Smckusick 	cache_purge(ITOV(ip));
121812756Ssam out:
121937737Smckusick 	if (ndp->ni_dvp)
122012756Ssam 		iput(dp);
122112756Ssam 	iput(ip);
122237737Smckusick 	return (error);
122312756Ssam }
122412756Ssam 
122537737Smckusick /*
122637737Smckusick  * symlink -- make a symbolic link
122737737Smckusick  */
122837737Smckusick ufs_symlink(ndp, vap, target)
122937737Smckusick 	struct nameidata *ndp;
123037737Smckusick 	struct vattr *vap;
123137737Smckusick 	char *target;
123212756Ssam {
123337737Smckusick 	struct inode *ip;
123437737Smckusick 	int error;
123512756Ssam 
123637737Smckusick 	error = maknode(IFLNK | vap->va_mode, ndp, &ip);
123737737Smckusick 	if (error)
123837737Smckusick 		return (error);
123939597Smckusick 	error = vn_rdwr(UIO_WRITE, ITOV(ip), target, strlen(target), (off_t)0,
124039597Smckusick 		UIO_SYSSPACE, IO_NODELOCKED, ndp->ni_cred, (int *)0);
124137737Smckusick 	iput(ip);
124237737Smckusick 	return (error);
124337737Smckusick }
124437737Smckusick 
124537737Smckusick /*
124637737Smckusick  * Vnode op for read and write
124737737Smckusick  */
124839597Smckusick ufs_readdir(vp, uio, cred)
124937737Smckusick 	struct vnode *vp;
125037737Smckusick 	register struct uio *uio;
125137737Smckusick 	struct ucred *cred;
125237737Smckusick {
125339597Smckusick 	int count, lost, error;
125437737Smckusick 
125537737Smckusick 	count = uio->uio_resid;
125637737Smckusick 	count &= ~(DIRBLKSIZ - 1);
125739597Smckusick 	lost = uio->uio_resid - count;
125839597Smckusick 	if (count < DIRBLKSIZ || (uio->uio_offset & (DIRBLKSIZ -1)))
125937737Smckusick 		return (EINVAL);
126037737Smckusick 	uio->uio_resid = count;
126137737Smckusick 	uio->uio_iov->iov_len = count;
126239597Smckusick 	error = ufs_read(vp, uio, 0, cred);
126339597Smckusick 	uio->uio_resid += lost;
126437737Smckusick 	return (error);
126537737Smckusick }
126637737Smckusick 
126737737Smckusick /*
126837737Smckusick  * Return target name of a symbolic link
126937737Smckusick  */
127037737Smckusick ufs_readlink(vp, uiop, cred)
127137737Smckusick 	struct vnode *vp;
127237737Smckusick 	struct uio *uiop;
127337737Smckusick 	struct ucred *cred;
127437737Smckusick {
127537737Smckusick 
127639597Smckusick 	return (ufs_read(vp, uiop, 0, cred));
127737737Smckusick }
127837737Smckusick 
127937737Smckusick /*
128037737Smckusick  * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually
128137737Smckusick  * done. Iff ni_vp/ni_dvp not null and locked, unlock.
128237737Smckusick  */
128337737Smckusick ufs_abortop(ndp)
128437737Smckusick 	register struct nameidata *ndp;
128537737Smckusick {
128637737Smckusick 
128740107Smckusick 	if (ndp->ni_dvp) {
128840107Smckusick 		if (VOP_ISLOCKED(ndp->ni_dvp))
128940107Smckusick 			VOP_UNLOCK(ndp->ni_dvp);
129040107Smckusick 		vrele(ndp->ni_dvp);
129140107Smckusick 	}
129237737Smckusick 	if (ndp->ni_vp) {
129340107Smckusick 		if (VOP_ISLOCKED(ndp->ni_vp))
129440107Smckusick 			VOP_UNLOCK(ndp->ni_vp);
129537737Smckusick 		vrele(ndp->ni_vp);
129612756Ssam 	}
129737737Smckusick 	return;
129812756Ssam }
129912756Ssam 
130039909Smckusick /*
130139909Smckusick  * Lock an inode.
130239909Smckusick  */
130337737Smckusick ufs_lock(vp)
130437737Smckusick 	struct vnode *vp;
130537737Smckusick {
130637737Smckusick 	register struct inode *ip = VTOI(vp);
130737737Smckusick 
130837737Smckusick 	ILOCK(ip);
130937737Smckusick 	return (0);
131037737Smckusick }
131137737Smckusick 
131239909Smckusick /*
131339909Smckusick  * Unlock an inode.
131439909Smckusick  */
131537737Smckusick ufs_unlock(vp)
131637737Smckusick 	struct vnode *vp;
131737737Smckusick {
131837737Smckusick 	register struct inode *ip = VTOI(vp);
131937737Smckusick 
132037737Smckusick 	if (!(ip->i_flag & ILOCKED))
132137737Smckusick 		panic("ufs_unlock NOT LOCKED");
132237737Smckusick 	IUNLOCK(ip);
132337737Smckusick 	return (0);
132437737Smckusick }
132537737Smckusick 
132612756Ssam /*
132739909Smckusick  * Check for a locked inode.
132839909Smckusick  */
132939909Smckusick ufs_islocked(vp)
133039909Smckusick 	struct vnode *vp;
133139909Smckusick {
133239909Smckusick 
133339909Smckusick 	if (VTOI(vp)->i_flag & ILOCKED)
133439909Smckusick 		return (1);
133539909Smckusick 	return (0);
133639909Smckusick }
133739909Smckusick 
133839909Smckusick /*
133937737Smckusick  * Get access to bmap
134012756Ssam  */
134137737Smckusick ufs_bmap(vp, bn, vpp, bnp)
134237737Smckusick 	struct vnode *vp;
134337737Smckusick 	daddr_t bn;
134437737Smckusick 	struct vnode **vpp;
134537737Smckusick 	daddr_t *bnp;
134612756Ssam {
134737737Smckusick 	struct inode *ip = VTOI(vp);
134812756Ssam 
134937737Smckusick 	if (vpp != NULL)
135037737Smckusick 		*vpp = ip->i_devvp;
135137737Smckusick 	if (bnp == NULL)
135237737Smckusick 		return (0);
135337737Smckusick 	return (bmap(ip, bn, bnp, (daddr_t *)0, (int *)0));
135412756Ssam }
135537737Smckusick 
135637737Smckusick /*
135737737Smckusick  * Just call the device strategy routine
135837737Smckusick  */
135939674Smckusick int checkoverlap = 1;
136039674Smckusick 
136137737Smckusick ufs_strategy(bp)
136237737Smckusick 	register struct buf *bp;
136337737Smckusick {
136439674Smckusick 	register struct inode *ip = VTOI(bp->b_vp);
136539674Smckusick 	register struct buf *ep;
136639674Smckusick 	struct vnode *vp;
136739674Smckusick 	struct buf *ebp;
136839674Smckusick 	daddr_t start, last;
136939674Smckusick 	int error;
137039674Smckusick 
137139674Smckusick 	if (bp->b_vp->v_type == VBLK || bp->b_vp->v_type == VCHR)
137239674Smckusick 		panic("ufs_strategy: spec");
137339674Smckusick 	if (bp->b_blkno == bp->b_lblkno) {
137439674Smckusick 		if (error = bmap(ip, bp->b_lblkno, &bp->b_blkno))
137539674Smckusick 			return (error);
137639896Smckusick 		if ((long)bp->b_blkno == -1)
137739674Smckusick 			clrbuf(bp);
137839674Smckusick 	}
137939896Smckusick 	if ((long)bp->b_blkno == -1) {
138039896Smckusick 		biodone(bp);
138139674Smckusick 		return (0);
138239896Smckusick 	}
138339674Smckusick 	if (checkoverlap) {
138439674Smckusick 		ebp = &buf[nbuf];
138539674Smckusick 		start = bp->b_blkno;
138639674Smckusick 		last = start + btodb(bp->b_bcount) - 1;
138739674Smckusick 		for (ep = buf; ep < ebp; ep++) {
138839674Smckusick 			if (ep == bp || (ep->b_flags & B_INVAL) ||
138939674Smckusick 			    ep->b_vp == (struct vnode *)0)
139039674Smckusick 				continue;
139139674Smckusick 			if (VOP_BMAP(ep->b_vp, (daddr_t)0, &vp, (daddr_t)0))
139239674Smckusick 				continue;
139339674Smckusick 			if (vp != ip->i_devvp)
139439674Smckusick 				continue;
139539674Smckusick 			/* look for overlap */
139639674Smckusick 			if (ep->b_bcount == 0 || ep->b_blkno > last ||
139739674Smckusick 			    ep->b_blkno + btodb(ep->b_bcount) <= start)
139839674Smckusick 				continue;
139939896Smckusick 			vprint("Disk overlap", vp);
140039896Smckusick 			printf("\tstart %d, end %d overlap start %d, end %d\n",
140139896Smckusick 				start, last, ep->b_blkno,
140239896Smckusick 				ep->b_blkno + btodb(ep->b_bcount) - 1);
140339674Smckusick 		}
140439674Smckusick 	}
140539674Smckusick 	vp = ip->i_devvp;
140639674Smckusick 	bp->b_dev = vp->v_rdev;
140739674Smckusick 	(*(vp->v_op->vn_strategy))(bp);
140837737Smckusick 	return (0);
140937737Smckusick }
141037737Smckusick 
141137737Smckusick /*
141239674Smckusick  * Print out the contents of an inode.
141339674Smckusick  */
141439674Smckusick ufs_print(vp)
141539674Smckusick 	struct vnode *vp;
141639674Smckusick {
141739674Smckusick 	register struct inode *ip = VTOI(vp);
141839674Smckusick 
1419*40293Smckusick 	printf("tag VT_UFS, ino %d, on dev %d, %d", ip->i_number,
1420*40293Smckusick 		major(ip->i_dev), minor(ip->i_dev));
1421*40293Smckusick #ifdef FIFO
1422*40293Smckusick 	if (vp->v_type == VFIFO)
1423*40293Smckusick 		fifo_printinfo(vp);
1424*40293Smckusick #endif /* FIFO */
1425*40293Smckusick 	printf("%s\n", (ip->i_flag & ILOCKED) ? " (LOCKED)" : "");
142639900Smckusick 	if (ip->i_spare0 == 0)
142739900Smckusick 		return;
142839900Smckusick 	printf("\towner pid %d", ip->i_spare0);
142939900Smckusick 	if (ip->i_spare1)
143039900Smckusick 		printf(" waiting pid %d", ip->i_spare1);
143139900Smckusick 	printf("\n");
143239674Smckusick }
143339674Smckusick 
143439674Smckusick /*
143539628Smckusick  * Read wrapper for special devices.
143639628Smckusick  */
143739628Smckusick ufsspec_read(vp, uio, ioflag, cred)
143839628Smckusick 	struct vnode *vp;
143939628Smckusick 	struct uio *uio;
144039628Smckusick 	int ioflag;
144139628Smckusick 	struct ucred *cred;
144239628Smckusick {
144339628Smckusick 
144439628Smckusick 	/*
144539628Smckusick 	 * Set access flag.
144639628Smckusick 	 */
144739628Smckusick 	VTOI(vp)->i_flag |= IACC;
144839628Smckusick 	return (spec_read(vp, uio, ioflag, cred));
144939628Smckusick }
145039628Smckusick 
145139628Smckusick /*
145239628Smckusick  * Write wrapper for special devices.
145339628Smckusick  */
145439628Smckusick ufsspec_write(vp, uio, ioflag, cred)
145539628Smckusick 	struct vnode *vp;
145639628Smckusick 	struct uio *uio;
145739628Smckusick 	int ioflag;
145839628Smckusick 	struct ucred *cred;
145939628Smckusick {
146039628Smckusick 
146139628Smckusick 	/*
146239628Smckusick 	 * Set update and change flags.
146339628Smckusick 	 */
146439628Smckusick 	VTOI(vp)->i_flag |= IUPD|ICHG;
146539628Smckusick 	return (spec_write(vp, uio, ioflag, cred));
146639628Smckusick }
146739628Smckusick 
146839628Smckusick /*
146939628Smckusick  * Close wrapper for special devices.
147039628Smckusick  *
147139628Smckusick  * Update the times on the inode then do device close.
147239628Smckusick  */
147339628Smckusick ufsspec_close(vp, fflag, cred)
147439628Smckusick 	struct vnode *vp;
147539628Smckusick 	int fflag;
147639628Smckusick 	struct ucred *cred;
147739628Smckusick {
147839628Smckusick 	register struct inode *ip = VTOI(vp);
147939628Smckusick 
148039815Smckusick 	if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED))
148139628Smckusick 		ITIMES(ip, &time, &time);
148239628Smckusick 	return (spec_close(vp, fflag, cred));
148339628Smckusick }
148439628Smckusick 
148540290Smckusick #ifdef FIFO
148639628Smckusick /*
148740290Smckusick  * Read wrapper for fifo's
148840290Smckusick  */
148940290Smckusick ufsfifo_read(vp, uio, ioflag, cred)
149040290Smckusick 	struct vnode *vp;
149140290Smckusick 	struct uio *uio;
149240290Smckusick 	int ioflag;
149340290Smckusick 	struct ucred *cred;
149440290Smckusick {
149540290Smckusick 
149640290Smckusick 	/*
149740290Smckusick 	 * Set access flag.
149840290Smckusick 	 */
149940290Smckusick 	VTOI(vp)->i_flag |= IACC;
150040290Smckusick 	return (fifo_read(vp, uio, ioflag, cred));
150140290Smckusick }
150240290Smckusick 
150340290Smckusick /*
150440290Smckusick  * Write wrapper for fifo's.
150540290Smckusick  */
150640290Smckusick ufsfifo_write(vp, uio, ioflag, cred)
150740290Smckusick 	struct vnode *vp;
150840290Smckusick 	struct uio *uio;
150940290Smckusick 	int ioflag;
151040290Smckusick 	struct ucred *cred;
151140290Smckusick {
151240290Smckusick 
151340290Smckusick 	/*
151440290Smckusick 	 * Set update and change flags.
151540290Smckusick 	 */
151640290Smckusick 	VTOI(vp)->i_flag |= IUPD|ICHG;
151740290Smckusick 	return (fifo_write(vp, uio, ioflag, cred));
151840290Smckusick }
151940290Smckusick 
152040290Smckusick /*
152140290Smckusick  * Close wrapper for fifo's.
152240290Smckusick  *
152340290Smckusick  * Update the times on the inode then do device close.
152440290Smckusick  */
152540290Smckusick ufsfifo_close(vp, fflag, cred)
152640290Smckusick 	struct vnode *vp;
152740290Smckusick 	int fflag;
152840290Smckusick 	struct ucred *cred;
152940290Smckusick {
153040290Smckusick 	register struct inode *ip = VTOI(vp);
153140290Smckusick 
153240290Smckusick 	if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED))
153340290Smckusick 		ITIMES(ip, &time, &time);
153440290Smckusick 	return (fifo_close(vp, fflag, cred));
153540290Smckusick }
153640290Smckusick #endif /* FIFO */
153740290Smckusick 
153840290Smckusick /*
153937737Smckusick  * Make a new file.
154037737Smckusick  */
154137737Smckusick maknode(mode, ndp, ipp)
154237737Smckusick 	int mode;
154337737Smckusick 	register struct nameidata *ndp;
154437737Smckusick 	struct inode **ipp;
154537737Smckusick {
154637737Smckusick 	register struct inode *ip;
154737737Smckusick 	struct inode *tip;
154837737Smckusick 	register struct inode *pdir = VTOI(ndp->ni_dvp);
154937737Smckusick 	ino_t ipref;
155037737Smckusick 	int error;
155137737Smckusick 
155237737Smckusick 	*ipp = 0;
155337737Smckusick 	if ((mode & IFMT) == IFDIR)
155437737Smckusick 		ipref = dirpref(pdir->i_fs);
155537737Smckusick 	else
155637737Smckusick 		ipref = pdir->i_number;
155737737Smckusick 	error = ialloc(pdir, ipref, mode, &tip);
155837737Smckusick 	if (error) {
155937737Smckusick 		iput(pdir);
156037737Smckusick 		return (error);
156137737Smckusick 	}
156237737Smckusick 	ip = tip;
156337737Smckusick #ifdef QUOTA
156437737Smckusick 	if (ip->i_dquot != NODQUOT)
156537737Smckusick 		panic("maknode: dquot");
156637737Smckusick #endif
156737737Smckusick 	ip->i_flag |= IACC|IUPD|ICHG;
156837737Smckusick 	if ((mode & IFMT) == 0)
156937737Smckusick 		mode |= IFREG;
157037737Smckusick 	ip->i_mode = mode;
157137737Smckusick 	ITOV(ip)->v_type = IFTOVT(mode);	/* Rest init'd in iget() */
157237737Smckusick 	ip->i_nlink = 1;
157337737Smckusick 	ip->i_uid = ndp->ni_cred->cr_uid;
157437737Smckusick 	ip->i_gid = pdir->i_gid;
157537737Smckusick 	if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, ndp->ni_cred) &&
157637737Smckusick 	    suser(ndp->ni_cred, NULL))
157737737Smckusick 		ip->i_mode &= ~ISGID;
157837737Smckusick #ifdef QUOTA
157937737Smckusick 	ip->i_dquot = inoquota(ip);
158037737Smckusick #endif
158137737Smckusick 
158237737Smckusick 	/*
158337737Smckusick 	 * Make sure inode goes to disk before directory entry.
158437737Smckusick 	 */
158537737Smckusick 	if ((error = iupdat(ip, &time, &time, 1)) ||
158637737Smckusick 	    (error = direnter(ip, ndp))) {
158737737Smckusick 		/*
158837737Smckusick 		 * Write error occurred trying to update the inode
158937737Smckusick 		 * or the directory so must deallocate the inode.
159037737Smckusick 		 */
159137737Smckusick 		ip->i_nlink = 0;
159237737Smckusick 		ip->i_flag |= ICHG;
159337737Smckusick 		iput(ip);
159437737Smckusick 		return (error);
159537737Smckusick 	}
159637737Smckusick 	*ipp = ip;
159737737Smckusick 	return (0);
159837737Smckusick }
1599