xref: /csrg-svn/sys/ufs/lfs/lfs_vnops.c (revision 46679)
123405Smckusick /*
237737Smckusick  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
337737Smckusick  * All rights reserved.
423405Smckusick  *
544539Sbostic  * %sccs.include.redist.c%
637737Smckusick  *
7*46679Smckusick  *	@(#)lfs_vnops.c	7.55 (Berkeley) 02/25/91
823405Smckusick  */
937Sbill 
1017101Sbloom #include "param.h"
1117101Sbloom #include "systm.h"
1217101Sbloom #include "user.h"
1317101Sbloom #include "kernel.h"
1417101Sbloom #include "file.h"
1517101Sbloom #include "stat.h"
1617101Sbloom #include "buf.h"
1717101Sbloom #include "proc.h"
1817101Sbloom #include "socket.h"
1917101Sbloom #include "socketvar.h"
2037737Smckusick #include "conf.h"
2117101Sbloom #include "mount.h"
2237737Smckusick #include "vnode.h"
2340653Smckusick #include "specdev.h"
2446207Smckusick #include "fcntl.h"
2546207Smckusick #include "malloc.h"
2646207Smckusick #include "../ufs/lockf.h"
2741312Smckusick #include "../ufs/quota.h"
2837737Smckusick #include "../ufs/inode.h"
2937737Smckusick #include "../ufs/fs.h"
3037Sbill 
319167Ssam /*
3237737Smckusick  * Global vfs data structures for ufs
339167Ssam  */
346254Sroot 
3537737Smckusick int	ufs_lookup(),
3637737Smckusick 	ufs_create(),
3737737Smckusick 	ufs_mknod(),
3837737Smckusick 	ufs_open(),
3937737Smckusick 	ufs_close(),
4037737Smckusick 	ufs_access(),
4137737Smckusick 	ufs_getattr(),
4237737Smckusick 	ufs_setattr(),
4337737Smckusick 	ufs_read(),
4437737Smckusick 	ufs_write(),
4537737Smckusick 	ufs_ioctl(),
4637737Smckusick 	ufs_select(),
4737737Smckusick 	ufs_mmap(),
4837737Smckusick 	ufs_fsync(),
4937737Smckusick 	ufs_seek(),
5037737Smckusick 	ufs_remove(),
5137737Smckusick 	ufs_link(),
5237737Smckusick 	ufs_rename(),
5337737Smckusick 	ufs_mkdir(),
5437737Smckusick 	ufs_rmdir(),
5537737Smckusick 	ufs_symlink(),
5637737Smckusick 	ufs_readdir(),
5737737Smckusick 	ufs_readlink(),
5837737Smckusick 	ufs_abortop(),
5937737Smckusick 	ufs_inactive(),
6039391Smckusick 	ufs_reclaim(),
6137737Smckusick 	ufs_lock(),
6237737Smckusick 	ufs_unlock(),
6337737Smckusick 	ufs_bmap(),
6439674Smckusick 	ufs_strategy(),
6539909Smckusick 	ufs_print(),
6646207Smckusick 	ufs_islocked(),
6746207Smckusick 	ufs_advlock();
686254Sroot 
6937737Smckusick struct vnodeops ufs_vnodeops = {
7039674Smckusick 	ufs_lookup,		/* lookup */
7139674Smckusick 	ufs_create,		/* create */
7239674Smckusick 	ufs_mknod,		/* mknod */
7339674Smckusick 	ufs_open,		/* open */
7439674Smckusick 	ufs_close,		/* close */
7539674Smckusick 	ufs_access,		/* access */
7639674Smckusick 	ufs_getattr,		/* getattr */
7739674Smckusick 	ufs_setattr,		/* setattr */
7839674Smckusick 	ufs_read,		/* read */
7939674Smckusick 	ufs_write,		/* write */
8039674Smckusick 	ufs_ioctl,		/* ioctl */
8139674Smckusick 	ufs_select,		/* select */
8239674Smckusick 	ufs_mmap,		/* mmap */
8339674Smckusick 	ufs_fsync,		/* fsync */
8439674Smckusick 	ufs_seek,		/* seek */
8539674Smckusick 	ufs_remove,		/* remove */
8639674Smckusick 	ufs_link,		/* link */
8739674Smckusick 	ufs_rename,		/* rename */
8839674Smckusick 	ufs_mkdir,		/* mkdir */
8939674Smckusick 	ufs_rmdir,		/* rmdir */
9039674Smckusick 	ufs_symlink,		/* symlink */
9139674Smckusick 	ufs_readdir,		/* readdir */
9239674Smckusick 	ufs_readlink,		/* readlink */
9339674Smckusick 	ufs_abortop,		/* abortop */
9439674Smckusick 	ufs_inactive,		/* inactive */
9539674Smckusick 	ufs_reclaim,		/* reclaim */
9639674Smckusick 	ufs_lock,		/* lock */
9739674Smckusick 	ufs_unlock,		/* unlock */
9839674Smckusick 	ufs_bmap,		/* bmap */
9939674Smckusick 	ufs_strategy,		/* strategy */
10039674Smckusick 	ufs_print,		/* print */
10139909Smckusick 	ufs_islocked,		/* islocked */
10246207Smckusick 	ufs_advlock,		/* advlock */
10337737Smckusick };
1046254Sroot 
10539435Smckusick int	spec_lookup(),
10639435Smckusick 	spec_open(),
10739628Smckusick 	ufsspec_read(),
10839628Smckusick 	ufsspec_write(),
10939435Smckusick 	spec_strategy(),
11039674Smckusick 	spec_bmap(),
11139435Smckusick 	spec_ioctl(),
11239435Smckusick 	spec_select(),
11339628Smckusick 	ufsspec_close(),
11446207Smckusick 	spec_advlock(),
11539435Smckusick 	spec_badop(),
116*46679Smckusick 	nullop();
11739435Smckusick 
11839435Smckusick struct vnodeops spec_inodeops = {
11939597Smckusick 	spec_lookup,		/* lookup */
12039597Smckusick 	spec_badop,		/* create */
12139597Smckusick 	spec_badop,		/* mknod */
12239597Smckusick 	spec_open,		/* open */
12339628Smckusick 	ufsspec_close,		/* close */
12439597Smckusick 	ufs_access,		/* access */
12539597Smckusick 	ufs_getattr,		/* getattr */
12639597Smckusick 	ufs_setattr,		/* setattr */
12739628Smckusick 	ufsspec_read,		/* read */
12839628Smckusick 	ufsspec_write,		/* write */
12939597Smckusick 	spec_ioctl,		/* ioctl */
13039597Smckusick 	spec_select,		/* select */
13139597Smckusick 	spec_badop,		/* mmap */
132*46679Smckusick 	nullop,			/* fsync */
13339597Smckusick 	spec_badop,		/* seek */
13439597Smckusick 	spec_badop,		/* remove */
13539597Smckusick 	spec_badop,		/* link */
13639597Smckusick 	spec_badop,		/* rename */
13739597Smckusick 	spec_badop,		/* mkdir */
13839597Smckusick 	spec_badop,		/* rmdir */
13939597Smckusick 	spec_badop,		/* symlink */
14039597Smckusick 	spec_badop,		/* readdir */
14139597Smckusick 	spec_badop,		/* readlink */
14239597Smckusick 	spec_badop,		/* abortop */
14339597Smckusick 	ufs_inactive,		/* inactive */
14439597Smckusick 	ufs_reclaim,		/* reclaim */
14539597Smckusick 	ufs_lock,		/* lock */
14639597Smckusick 	ufs_unlock,		/* unlock */
14739674Smckusick 	spec_bmap,		/* bmap */
14839597Smckusick 	spec_strategy,		/* strategy */
14939674Smckusick 	ufs_print,		/* print */
15039909Smckusick 	ufs_islocked,		/* islocked */
15146207Smckusick 	spec_advlock,		/* advlock */
15239435Smckusick };
15339435Smckusick 
15440290Smckusick #ifdef FIFO
15540290Smckusick int	fifo_lookup(),
15640290Smckusick 	fifo_open(),
15740290Smckusick 	ufsfifo_read(),
15840290Smckusick 	ufsfifo_write(),
15940290Smckusick 	fifo_bmap(),
16040290Smckusick 	fifo_ioctl(),
16140290Smckusick 	fifo_select(),
16240290Smckusick 	ufsfifo_close(),
16340290Smckusick 	fifo_print(),
16446207Smckusick 	fifo_advlock(),
165*46679Smckusick 	fifo_badop();
16640290Smckusick 
16740290Smckusick struct vnodeops fifo_inodeops = {
16840290Smckusick 	fifo_lookup,		/* lookup */
16940290Smckusick 	fifo_badop,		/* create */
17040290Smckusick 	fifo_badop,		/* mknod */
17140290Smckusick 	fifo_open,		/* open */
17240290Smckusick 	ufsfifo_close,		/* close */
17340290Smckusick 	ufs_access,		/* access */
17440290Smckusick 	ufs_getattr,		/* getattr */
17540290Smckusick 	ufs_setattr,		/* setattr */
17640290Smckusick 	ufsfifo_read,		/* read */
17740290Smckusick 	ufsfifo_write,		/* write */
17840290Smckusick 	fifo_ioctl,		/* ioctl */
17940290Smckusick 	fifo_select,		/* select */
18040290Smckusick 	fifo_badop,		/* mmap */
181*46679Smckusick 	nullop,			/* fsync */
18240290Smckusick 	fifo_badop,		/* seek */
18340290Smckusick 	fifo_badop,		/* remove */
18440290Smckusick 	fifo_badop,		/* link */
18540290Smckusick 	fifo_badop,		/* rename */
18640290Smckusick 	fifo_badop,		/* mkdir */
18740290Smckusick 	fifo_badop,		/* rmdir */
18840290Smckusick 	fifo_badop,		/* symlink */
18940290Smckusick 	fifo_badop,		/* readdir */
19040290Smckusick 	fifo_badop,		/* readlink */
19140290Smckusick 	fifo_badop,		/* abortop */
19240290Smckusick 	ufs_inactive,		/* inactive */
19340290Smckusick 	ufs_reclaim,		/* reclaim */
19440290Smckusick 	ufs_lock,		/* lock */
19540290Smckusick 	ufs_unlock,		/* unlock */
19640290Smckusick 	fifo_bmap,		/* bmap */
19740290Smckusick 	fifo_badop,		/* strategy */
19840290Smckusick 	ufs_print,		/* print */
19940290Smckusick 	ufs_islocked,		/* islocked */
20046207Smckusick 	fifo_advlock,		/* advlock */
20137737Smckusick };
20240290Smckusick #endif /* FIFO */
20340290Smckusick 
20440290Smckusick enum vtype iftovt_tab[16] = {
20540290Smckusick 	VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
20640290Smckusick 	VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
20737737Smckusick };
20840290Smckusick int	vttoif_tab[9] = {
20940290Smckusick 	0, IFREG, IFDIR, IFBLK, IFCHR, IFLNK, IFSOCK, IFIFO, IFMT,
21040290Smckusick };
2116254Sroot 
2129167Ssam /*
21337737Smckusick  * Create a regular file
2149167Ssam  */
21537737Smckusick ufs_create(ndp, vap)
21637737Smckusick 	struct nameidata *ndp;
21737737Smckusick 	struct vattr *vap;
2186254Sroot {
21937737Smckusick 	struct inode *ip;
22037737Smckusick 	int error;
2216254Sroot 
22237737Smckusick 	if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip))
22337737Smckusick 		return (error);
22437737Smckusick 	ndp->ni_vp = ITOV(ip);
22537737Smckusick 	return (0);
2266254Sroot }
2276254Sroot 
22837Sbill /*
22937737Smckusick  * Mknod vnode call
2306254Sroot  */
23137737Smckusick /* ARGSUSED */
23237737Smckusick ufs_mknod(ndp, vap, cred)
23337737Smckusick 	struct nameidata *ndp;
23437737Smckusick 	struct ucred *cred;
23537737Smckusick 	struct vattr *vap;
2366254Sroot {
23739435Smckusick 	register struct vnode *vp;
23837737Smckusick 	struct inode *ip;
23937737Smckusick 	int error;
2406254Sroot 
24137737Smckusick 	if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip))
24237737Smckusick 		return (error);
24340290Smckusick 	ip->i_flag |= IACC|IUPD|ICHG;
24440290Smckusick 	if (vap->va_rdev != VNOVAL) {
24537737Smckusick 		/*
24637737Smckusick 		 * Want to be able to use this to make badblock
24737737Smckusick 		 * inodes, so don't truncate the dev number.
24837737Smckusick 		 */
24939608Smckusick 		ip->i_rdev = vap->va_rdev;
25012756Ssam 	}
25137737Smckusick 	/*
25237737Smckusick 	 * Remove inode so that it will be reloaded by iget and
25337737Smckusick 	 * checked to see if it is an alias of an existing entry
25437737Smckusick 	 * in the inode cache.
25537737Smckusick 	 */
25640290Smckusick 	vp = ITOV(ip);
25740290Smckusick 	vput(vp);
25839435Smckusick 	vp->v_type = VNON;
25939435Smckusick 	vgone(vp);
26037737Smckusick 	return (0);
2616254Sroot }
2626254Sroot 
2636254Sroot /*
26437737Smckusick  * Open called.
26537737Smckusick  *
26637737Smckusick  * Nothing to do.
2676254Sroot  */
26837737Smckusick /* ARGSUSED */
26937737Smckusick ufs_open(vp, mode, cred)
27037737Smckusick 	struct vnode *vp;
27137737Smckusick 	int mode;
27237737Smckusick 	struct ucred *cred;
2736254Sroot {
2746254Sroot 
27537737Smckusick 	return (0);
2766254Sroot }
2776254Sroot 
2786254Sroot /*
27937737Smckusick  * Close called
28037737Smckusick  *
28137737Smckusick  * Update the times on the inode.
2826254Sroot  */
28337737Smckusick /* ARGSUSED */
28437737Smckusick ufs_close(vp, fflag, cred)
28537737Smckusick 	struct vnode *vp;
28637737Smckusick 	int fflag;
28737737Smckusick 	struct ucred *cred;
2886254Sroot {
28937737Smckusick 	register struct inode *ip = VTOI(vp);
2906254Sroot 
29139815Smckusick 	if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED))
29237737Smckusick 		ITIMES(ip, &time, &time);
29337737Smckusick 	return (0);
2946254Sroot }
2956254Sroot 
29641312Smckusick /*
29741312Smckusick  * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
29841312Smckusick  * The mode is shifted to select the owner/group/other fields. The
29941312Smckusick  * super user is granted all permissions.
30041312Smckusick  */
30137737Smckusick ufs_access(vp, mode, cred)
30237737Smckusick 	struct vnode *vp;
30341312Smckusick 	register int mode;
30437737Smckusick 	struct ucred *cred;
3056254Sroot {
30641312Smckusick 	register struct inode *ip = VTOI(vp);
30741312Smckusick 	register gid_t *gp;
30841312Smckusick 	int i, error;
3096254Sroot 
31041312Smckusick #ifdef DIAGNOSTIC
31141312Smckusick 	if (!VOP_ISLOCKED(vp)) {
31241312Smckusick 		vprint("ufs_access: not locked", vp);
31341312Smckusick 		panic("ufs_access: not locked");
31441312Smckusick 	}
31541312Smckusick #endif
31641312Smckusick #ifdef QUOTA
31741312Smckusick 	if (mode & VWRITE) {
31841312Smckusick 		switch (vp->v_type) {
31941312Smckusick 		case VREG: case VDIR: case VLNK:
32041312Smckusick 			if (error = getinoquota(ip))
32141312Smckusick 				return (error);
32241312Smckusick 		}
32341312Smckusick 	}
32441312Smckusick #endif /* QUOTA */
32541312Smckusick 	/*
32641312Smckusick 	 * If you're the super-user, you always get access.
32741312Smckusick 	 */
32841312Smckusick 	if (cred->cr_uid == 0)
32941312Smckusick 		return (0);
33041312Smckusick 	/*
33141312Smckusick 	 * Access check is based on only one of owner, group, public.
33241312Smckusick 	 * If not owner, then check group. If not a member of the
33341312Smckusick 	 * group, then check public access.
33441312Smckusick 	 */
33541312Smckusick 	if (cred->cr_uid != ip->i_uid) {
33641312Smckusick 		mode >>= 3;
33741312Smckusick 		gp = cred->cr_groups;
33841312Smckusick 		for (i = 0; i < cred->cr_ngroups; i++, gp++)
33941312Smckusick 			if (ip->i_gid == *gp)
34041312Smckusick 				goto found;
34141312Smckusick 		mode >>= 3;
34241312Smckusick found:
34341312Smckusick 		;
34441312Smckusick 	}
34541312Smckusick 	if ((ip->i_mode & mode) != 0)
34641312Smckusick 		return (0);
34741312Smckusick 	return (EACCES);
3486254Sroot }
3496254Sroot 
35037737Smckusick /* ARGSUSED */
35137737Smckusick ufs_getattr(vp, vap, cred)
35237737Smckusick 	struct vnode *vp;
35337737Smckusick 	register struct vattr *vap;
35437737Smckusick 	struct ucred *cred;
3556254Sroot {
35637737Smckusick 	register struct inode *ip = VTOI(vp);
3576254Sroot 
35837737Smckusick 	ITIMES(ip, &time, &time);
3596254Sroot 	/*
36037737Smckusick 	 * Copy from inode table
3616254Sroot 	 */
36237737Smckusick 	vap->va_fsid = ip->i_dev;
36337737Smckusick 	vap->va_fileid = ip->i_number;
36437737Smckusick 	vap->va_mode = ip->i_mode & ~IFMT;
36537737Smckusick 	vap->va_nlink = ip->i_nlink;
36637737Smckusick 	vap->va_uid = ip->i_uid;
36737737Smckusick 	vap->va_gid = ip->i_gid;
36837737Smckusick 	vap->va_rdev = (dev_t)ip->i_rdev;
36941312Smckusick #ifdef tahoe
37041312Smckusick 	vap->va_size = ip->i_size;
37141312Smckusick 	vap->va_size_rsv = 0;
37241312Smckusick #else
37340641Smckusick 	vap->va_qsize = ip->i_din.di_qsize;
37441312Smckusick #endif
37537737Smckusick 	vap->va_atime.tv_sec = ip->i_atime;
37638578Smckusick 	vap->va_atime.tv_usec = 0;
37737737Smckusick 	vap->va_mtime.tv_sec = ip->i_mtime;
37838578Smckusick 	vap->va_mtime.tv_usec = 0;
37937737Smckusick 	vap->va_ctime.tv_sec = ip->i_ctime;
38038578Smckusick 	vap->va_ctime.tv_usec = 0;
38138254Smckusick 	vap->va_flags = ip->i_flags;
38238254Smckusick 	vap->va_gen = ip->i_gen;
38337737Smckusick 	/* this doesn't belong here */
38437737Smckusick 	if (vp->v_type == VBLK)
38537737Smckusick 		vap->va_blocksize = BLKDEV_IOSIZE;
38637737Smckusick 	else if (vp->v_type == VCHR)
38737737Smckusick 		vap->va_blocksize = MAXBSIZE;
3887142Smckusick 	else
38937737Smckusick 		vap->va_blocksize = ip->i_fs->fs_bsize;
39038657Smckusick 	vap->va_bytes = dbtob(ip->i_blocks);
39140641Smckusick 	vap->va_bytes_rsv = 0;
39237737Smckusick 	vap->va_type = vp->v_type;
39337737Smckusick 	return (0);
3946254Sroot }
3956254Sroot 
3966254Sroot /*
39737737Smckusick  * Set attribute vnode op. called from several syscalls
3986254Sroot  */
39937737Smckusick ufs_setattr(vp, vap, cred)
40037737Smckusick 	register struct vnode *vp;
40137737Smckusick 	register struct vattr *vap;
40237737Smckusick 	register struct ucred *cred;
4036254Sroot {
40437737Smckusick 	register struct inode *ip = VTOI(vp);
40537737Smckusick 	int error = 0;
4066254Sroot 
40737737Smckusick 	/*
40837737Smckusick 	 * Check for unsetable attributes.
40937737Smckusick 	 */
41037737Smckusick 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
41137737Smckusick 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
41237737Smckusick 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
41338254Smckusick 	    ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
41437737Smckusick 		return (EINVAL);
41516540Ssam 	}
41637737Smckusick 	/*
41737737Smckusick 	 * Go through the fields and update iff not VNOVAL.
41837737Smckusick 	 */
41937737Smckusick 	if (vap->va_uid != (u_short)VNOVAL || vap->va_gid != (u_short)VNOVAL)
42037737Smckusick 		if (error = chown1(vp, vap->va_uid, vap->va_gid, cred))
42137737Smckusick 			return (error);
42237737Smckusick 	if (vap->va_size != VNOVAL) {
42337737Smckusick 		if (vp->v_type == VDIR)
42437737Smckusick 			return (EISDIR);
42539674Smckusick 		if (error = itrunc(ip, vap->va_size, 0)) /* XXX IO_SYNC? */
42637737Smckusick 			return (error);
42713878Ssam 	}
42837737Smckusick 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
42937773Smckusick 		if (cred->cr_uid != ip->i_uid &&
43037773Smckusick 		    (error = suser(cred, &u.u_acflag)))
43137773Smckusick 			return (error);
43237737Smckusick 		if (vap->va_atime.tv_sec != VNOVAL)
43337737Smckusick 			ip->i_flag |= IACC;
43437737Smckusick 		if (vap->va_mtime.tv_sec != VNOVAL)
43537737Smckusick 			ip->i_flag |= IUPD;
43637737Smckusick 		ip->i_flag |= ICHG;
43737737Smckusick 		if (error = iupdat(ip, &vap->va_atime, &vap->va_mtime, 1))
43837737Smckusick 			return (error);
4396254Sroot 	}
44037737Smckusick 	if (vap->va_mode != (u_short)VNOVAL)
44137737Smckusick 		error = chmod1(vp, (int)vap->va_mode, cred);
44238254Smckusick 	if (vap->va_flags != VNOVAL) {
44338254Smckusick 		if (cred->cr_uid != ip->i_uid &&
44438254Smckusick 		    (error = suser(cred, &u.u_acflag)))
44538254Smckusick 			return (error);
44638254Smckusick 		if (cred->cr_uid == 0) {
44738254Smckusick 			ip->i_flags = vap->va_flags;
44838254Smckusick 		} else {
44938254Smckusick 			ip->i_flags &= 0xffff0000;
45038254Smckusick 			ip->i_flags |= (vap->va_flags & 0xffff);
45138254Smckusick 		}
45238254Smckusick 		ip->i_flag |= ICHG;
45338254Smckusick 	}
45437737Smckusick 	return (error);
4556254Sroot }
4566254Sroot 
4576254Sroot /*
4589167Ssam  * Change the mode on a file.
4599167Ssam  * Inode must be locked before calling.
4609167Ssam  */
46137737Smckusick chmod1(vp, mode, cred)
46237737Smckusick 	register struct vnode *vp;
4637701Ssam 	register int mode;
46437737Smckusick 	struct ucred *cred;
4657701Ssam {
46637737Smckusick 	register struct inode *ip = VTOI(vp);
46737773Smckusick 	int error;
4687868Sroot 
46937773Smckusick 	if (cred->cr_uid != ip->i_uid &&
47037773Smckusick 	    (error = suser(cred, &u.u_acflag)))
47137773Smckusick 		return (error);
47237737Smckusick 	if (cred->cr_uid) {
47346206Smckusick 		if (vp->v_type != VDIR && (mode & ISVTX))
47445783Sbostic 			return (EFTYPE);
47546206Smckusick 		if (!groupmember(ip->i_gid, cred) && (mode & ISGID))
47645783Sbostic 			return (EPERM);
4777439Sroot 	}
47845783Sbostic 	ip->i_mode &= ~07777;
47937737Smckusick 	ip->i_mode |= mode & 07777;
4806254Sroot 	ip->i_flag |= ICHG;
48137737Smckusick 	if ((vp->v_flag & VTEXT) && (ip->i_mode & ISVTX) == 0)
48245722Smckusick 		(void) vnode_pager_uncache(vp);
48321015Smckusick 	return (0);
4845992Swnj }
4855992Swnj 
4869167Ssam /*
4877701Ssam  * Perform chown operation on inode ip;
4887701Ssam  * inode must be locked prior to call.
4897701Ssam  */
49037737Smckusick chown1(vp, uid, gid, cred)
49137737Smckusick 	register struct vnode *vp;
49237737Smckusick 	uid_t uid;
49337737Smckusick 	gid_t gid;
49437737Smckusick 	struct ucred *cred;
4957701Ssam {
49637737Smckusick 	register struct inode *ip = VTOI(vp);
49741312Smckusick 	uid_t ouid;
49841312Smckusick 	gid_t ogid;
49941312Smckusick 	int error = 0;
5007701Ssam #ifdef QUOTA
50141312Smckusick 	register int i;
50241312Smckusick 	long change;
50311811Ssam #endif
5047701Ssam 
50537737Smckusick 	if (uid == (u_short)VNOVAL)
50611811Ssam 		uid = ip->i_uid;
50737737Smckusick 	if (gid == (u_short)VNOVAL)
50811811Ssam 		gid = ip->i_gid;
50936614Sbostic 	/*
51036614Sbostic 	 * If we don't own the file, are trying to change the owner
51136614Sbostic 	 * of the file, or are not a member of the target group,
51236614Sbostic 	 * the caller must be superuser or the call fails.
51336614Sbostic 	 */
51437737Smckusick 	if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
51537737Smckusick 	    !groupmember((gid_t)gid, cred)) &&
51637737Smckusick 	    (error = suser(cred, &u.u_acflag)))
51737737Smckusick 		return (error);
51841312Smckusick 	ouid = ip->i_uid;
51941312Smckusick 	ogid = ip->i_gid;
52011811Ssam #ifdef QUOTA
52141312Smckusick 	if (error = getinoquota(ip))
52241312Smckusick 		return (error);
52341312Smckusick 	if (ouid == uid) {
52441312Smckusick 		dqrele(vp, ip->i_dquot[USRQUOTA]);
52541312Smckusick 		ip->i_dquot[USRQUOTA] = NODQUOT;
52641312Smckusick 	}
52741312Smckusick 	if (ogid == gid) {
52841312Smckusick 		dqrele(vp, ip->i_dquot[GRPQUOTA]);
52941312Smckusick 		ip->i_dquot[GRPQUOTA] = NODQUOT;
53041312Smckusick 	}
53141312Smckusick 	change = ip->i_blocks;
53241312Smckusick 	(void) chkdq(ip, -change, cred, CHOWN);
53341312Smckusick 	(void) chkiq(ip, -1, cred, CHOWN);
53441312Smckusick 	for (i = 0; i < MAXQUOTAS; i++) {
53541312Smckusick 		dqrele(vp, ip->i_dquot[i]);
53641312Smckusick 		ip->i_dquot[i] = NODQUOT;
53741312Smckusick 	}
5387482Skre #endif
53911811Ssam 	ip->i_uid = uid;
54011811Ssam 	ip->i_gid = gid;
5417701Ssam #ifdef QUOTA
54241312Smckusick 	if ((error = getinoquota(ip)) == 0) {
54341312Smckusick 		if (ouid == uid) {
54441312Smckusick 			dqrele(vp, ip->i_dquot[USRQUOTA]);
54541312Smckusick 			ip->i_dquot[USRQUOTA] = NODQUOT;
54641312Smckusick 		}
54741312Smckusick 		if (ogid == gid) {
54841312Smckusick 			dqrele(vp, ip->i_dquot[GRPQUOTA]);
54941312Smckusick 			ip->i_dquot[GRPQUOTA] = NODQUOT;
55041312Smckusick 		}
55141312Smckusick 		if ((error = chkdq(ip, change, cred, CHOWN)) == 0) {
55241312Smckusick 			if ((error = chkiq(ip, 1, cred, CHOWN)) == 0)
55341927Smckusick 				goto good;
55441312Smckusick 			else
55541312Smckusick 				(void) chkdq(ip, -change, cred, CHOWN|FORCE);
55641312Smckusick 		}
55741312Smckusick 		for (i = 0; i < MAXQUOTAS; i++) {
55841312Smckusick 			dqrele(vp, ip->i_dquot[i]);
55941312Smckusick 			ip->i_dquot[i] = NODQUOT;
56041312Smckusick 		}
56141312Smckusick 	}
56241312Smckusick 	ip->i_uid = ouid;
56341312Smckusick 	ip->i_gid = ogid;
56441312Smckusick 	if (getinoquota(ip) == 0) {
56541312Smckusick 		if (ouid == uid) {
56641312Smckusick 			dqrele(vp, ip->i_dquot[USRQUOTA]);
56741312Smckusick 			ip->i_dquot[USRQUOTA] = NODQUOT;
56841312Smckusick 		}
56941312Smckusick 		if (ogid == gid) {
57041312Smckusick 			dqrele(vp, ip->i_dquot[GRPQUOTA]);
57141312Smckusick 			ip->i_dquot[GRPQUOTA] = NODQUOT;
57241312Smckusick 		}
57341927Smckusick 		(void) chkdq(ip, change, cred, FORCE|CHOWN);
57441927Smckusick 		(void) chkiq(ip, 1, cred, FORCE|CHOWN);
57542440Smckusick 		(void) getinoquota(ip);
57641312Smckusick 	}
57742440Smckusick 	return (error);
57841927Smckusick good:
57942440Smckusick 	if (getinoquota(ip))
58042440Smckusick 		panic("chown: lost quota");
58142440Smckusick #endif /* QUOTA */
58241312Smckusick 	if (ouid != uid || ogid != gid)
58341312Smckusick 		ip->i_flag |= ICHG;
58441312Smckusick 	if (ouid != uid && cred->cr_uid != 0)
58541312Smckusick 		ip->i_mode &= ~ISUID;
58641312Smckusick 	if (ogid != gid && cred->cr_uid != 0)
58741312Smckusick 		ip->i_mode &= ~ISGID;
58812646Ssam 	return (0);
58937Sbill }
59037Sbill 
59139608Smckusick /*
59239608Smckusick  * Vnode op for reading.
59339608Smckusick  */
59437737Smckusick /* ARGSUSED */
59539608Smckusick ufs_read(vp, uio, ioflag, cred)
59639608Smckusick 	struct vnode *vp;
59739608Smckusick 	register struct uio *uio;
59839608Smckusick 	int ioflag;
59939608Smckusick 	struct ucred *cred;
60039608Smckusick {
60139608Smckusick 	register struct inode *ip = VTOI(vp);
60239608Smckusick 	register struct fs *fs;
60339608Smckusick 	struct buf *bp;
60439608Smckusick 	daddr_t lbn, bn, rablock;
60539896Smckusick 	int size, diff, error = 0;
60639608Smckusick 	long n, on, type;
60739608Smckusick 
60839608Smckusick 	if (uio->uio_rw != UIO_READ)
60939608Smckusick 		panic("ufs_read mode");
61039608Smckusick 	type = ip->i_mode & IFMT;
61139608Smckusick 	if (type != IFDIR && type != IFREG && type != IFLNK)
61239608Smckusick 		panic("ufs_read type");
61339608Smckusick 	if (uio->uio_resid == 0)
61439608Smckusick 		return (0);
61539608Smckusick 	if (uio->uio_offset < 0)
61639608Smckusick 		return (EINVAL);
61739608Smckusick 	ip->i_flag |= IACC;
61839608Smckusick 	fs = ip->i_fs;
61939608Smckusick 	do {
62039608Smckusick 		lbn = lblkno(fs, uio->uio_offset);
62139608Smckusick 		on = blkoff(fs, uio->uio_offset);
62239608Smckusick 		n = MIN((unsigned)(fs->fs_bsize - on), uio->uio_resid);
62339608Smckusick 		diff = ip->i_size - uio->uio_offset;
62439608Smckusick 		if (diff <= 0)
62539608Smckusick 			return (0);
62639608Smckusick 		if (diff < n)
62739608Smckusick 			n = diff;
62839608Smckusick 		size = blksize(fs, ip, lbn);
62939674Smckusick 		rablock = lbn + 1;
63039896Smckusick 		if (vp->v_lastr + 1 == lbn &&
63139896Smckusick 		    lblktosize(fs, rablock) < ip->i_size)
63239896Smckusick 			error = breada(ITOV(ip), lbn, size, rablock,
63339896Smckusick 				blksize(fs, ip, rablock), NOCRED, &bp);
63439608Smckusick 		else
63539674Smckusick 			error = bread(ITOV(ip), lbn, size, NOCRED, &bp);
63639815Smckusick 		vp->v_lastr = lbn;
63739608Smckusick 		n = MIN(n, size - bp->b_resid);
63839608Smckusick 		if (error) {
63939608Smckusick 			brelse(bp);
64039608Smckusick 			return (error);
64139608Smckusick 		}
64239608Smckusick 		error = uiomove(bp->b_un.b_addr + on, (int)n, uio);
64339608Smckusick 		if (n + on == fs->fs_bsize || uio->uio_offset == ip->i_size)
64439608Smckusick 			bp->b_flags |= B_AGE;
64539608Smckusick 		brelse(bp);
64639608Smckusick 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
64739608Smckusick 	return (error);
64839608Smckusick }
64939608Smckusick 
65039608Smckusick /*
65139608Smckusick  * Vnode op for writing.
65239608Smckusick  */
65339608Smckusick ufs_write(vp, uio, ioflag, cred)
65439608Smckusick 	register struct vnode *vp;
65539608Smckusick 	struct uio *uio;
65639608Smckusick 	int ioflag;
65739608Smckusick 	struct ucred *cred;
65839608Smckusick {
65939608Smckusick 	register struct inode *ip = VTOI(vp);
66039608Smckusick 	register struct fs *fs;
66139608Smckusick 	struct buf *bp;
66239608Smckusick 	daddr_t lbn, bn;
66339608Smckusick 	u_long osize;
66445722Smckusick 	int n, on, flags;
66545722Smckusick 	int size, resid, error = 0;
66639608Smckusick 
66739608Smckusick 	if (uio->uio_rw != UIO_WRITE)
66839608Smckusick 		panic("ufs_write mode");
66939608Smckusick 	switch (vp->v_type) {
67039608Smckusick 	case VREG:
67139608Smckusick 		if (ioflag & IO_APPEND)
67239608Smckusick 			uio->uio_offset = ip->i_size;
67339608Smckusick 		/* fall through */
67439608Smckusick 	case VLNK:
67539608Smckusick 		break;
67639608Smckusick 
67739608Smckusick 	case VDIR:
67839608Smckusick 		if ((ioflag & IO_SYNC) == 0)
67939608Smckusick 			panic("ufs_write nonsync dir write");
68039608Smckusick 		break;
68139608Smckusick 
68239608Smckusick 	default:
68339608Smckusick 		panic("ufs_write type");
68439608Smckusick 	}
68539608Smckusick 	if (uio->uio_offset < 0)
68639608Smckusick 		return (EINVAL);
68739608Smckusick 	if (uio->uio_resid == 0)
68839608Smckusick 		return (0);
68939608Smckusick 	/*
69039608Smckusick 	 * Maybe this should be above the vnode op call, but so long as
69139608Smckusick 	 * file servers have no limits, i don't think it matters
69239608Smckusick 	 */
69339608Smckusick 	if (vp->v_type == VREG &&
69439608Smckusick 	    uio->uio_offset + uio->uio_resid >
69539608Smckusick 	      u.u_rlimit[RLIMIT_FSIZE].rlim_cur) {
69639608Smckusick 		psignal(u.u_procp, SIGXFSZ);
69739608Smckusick 		return (EFBIG);
69839608Smckusick 	}
69939608Smckusick 	resid = uio->uio_resid;
70039608Smckusick 	osize = ip->i_size;
70139608Smckusick 	fs = ip->i_fs;
70239674Smckusick 	flags = 0;
70339674Smckusick 	if (ioflag & IO_SYNC)
70439674Smckusick 		flags = B_SYNC;
70539608Smckusick 	do {
70639608Smckusick 		lbn = lblkno(fs, uio->uio_offset);
70739608Smckusick 		on = blkoff(fs, uio->uio_offset);
70839608Smckusick 		n = MIN((unsigned)(fs->fs_bsize - on), uio->uio_resid);
70939608Smckusick 		if (n < fs->fs_bsize)
71039674Smckusick 			flags |= B_CLRBUF;
71139608Smckusick 		else
71239674Smckusick 			flags &= ~B_CLRBUF;
71339674Smckusick 		if (error = balloc(ip, lbn, (int)(on + n), &bp, flags))
71439608Smckusick 			break;
71539674Smckusick 		bn = bp->b_blkno;
71645722Smckusick 		if (uio->uio_offset + n > ip->i_size) {
71739608Smckusick 			ip->i_size = uio->uio_offset + n;
71845722Smckusick 			vnode_pager_setsize(vp, ip->i_size);
71945722Smckusick 		}
72039608Smckusick 		size = blksize(fs, ip, lbn);
72145722Smckusick 		(void) vnode_pager_uncache(vp);
72239608Smckusick 		n = MIN(n, size - bp->b_resid);
72339608Smckusick 		error = uiomove(bp->b_un.b_addr + on, n, uio);
72439608Smckusick 		if (ioflag & IO_SYNC)
72539608Smckusick 			(void) bwrite(bp);
72639608Smckusick 		else if (n + on == fs->fs_bsize) {
72739608Smckusick 			bp->b_flags |= B_AGE;
72839608Smckusick 			bawrite(bp);
72939608Smckusick 		} else
73039608Smckusick 			bdwrite(bp);
73139608Smckusick 		ip->i_flag |= IUPD|ICHG;
73239608Smckusick 		if (cred->cr_uid != 0)
73339608Smckusick 			ip->i_mode &= ~(ISUID|ISGID);
73439608Smckusick 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
73539608Smckusick 	if (error && (ioflag & IO_UNIT)) {
73639674Smckusick 		(void) itrunc(ip, osize, ioflag & IO_SYNC);
73739608Smckusick 		uio->uio_offset -= resid - uio->uio_resid;
73839608Smckusick 		uio->uio_resid = resid;
73939608Smckusick 	}
74042493Smckusick 	if (!error && (ioflag & IO_SYNC))
74142493Smckusick 		error = iupdat(ip, &time, &time, 1);
74239608Smckusick 	return (error);
74339608Smckusick }
74439608Smckusick 
74539608Smckusick /* ARGSUSED */
74637737Smckusick ufs_ioctl(vp, com, data, fflag, cred)
74737737Smckusick 	struct vnode *vp;
74837737Smckusick 	int com;
74937737Smckusick 	caddr_t data;
75037737Smckusick 	int fflag;
75137737Smckusick 	struct ucred *cred;
75211811Ssam {
75311811Ssam 
75437737Smckusick 	return (ENOTTY);
75511811Ssam }
75611811Ssam 
75737737Smckusick /* ARGSUSED */
75840290Smckusick ufs_select(vp, which, fflags, cred)
75937737Smckusick 	struct vnode *vp;
76040290Smckusick 	int which, fflags;
76137737Smckusick 	struct ucred *cred;
76237737Smckusick {
76337737Smckusick 
76437737Smckusick 	return (1);		/* XXX */
76537737Smckusick }
76637737Smckusick 
7679167Ssam /*
76837737Smckusick  * Mmap a file
76937737Smckusick  *
77037737Smckusick  * NB Currently unsupported.
7719167Ssam  */
77237737Smckusick /* ARGSUSED */
77337737Smckusick ufs_mmap(vp, fflags, cred)
77437737Smckusick 	struct vnode *vp;
77537737Smckusick 	int fflags;
77637737Smckusick 	struct ucred *cred;
77737Sbill {
77837Sbill 
77937737Smckusick 	return (EINVAL);
78037Sbill }
7817535Sroot 
7829167Ssam /*
78337737Smckusick  * Synch an open file.
7849167Ssam  */
78537737Smckusick /* ARGSUSED */
78639597Smckusick ufs_fsync(vp, fflags, cred, waitfor)
78737737Smckusick 	struct vnode *vp;
78837737Smckusick 	int fflags;
78937737Smckusick 	struct ucred *cred;
79039597Smckusick 	int waitfor;
7917701Ssam {
79239597Smckusick 	struct inode *ip = VTOI(vp);
7937701Ssam 
79437737Smckusick 	if (fflags&FWRITE)
79537737Smckusick 		ip->i_flag |= ICHG;
79639674Smckusick 	vflushbuf(vp, waitfor == MNT_WAIT ? B_SYNC : 0);
79739674Smckusick 	return (iupdat(ip, &time, &time, waitfor == MNT_WAIT));
7987701Ssam }
7997701Ssam 
8009167Ssam /*
80137737Smckusick  * Seek on a file
80237737Smckusick  *
80337737Smckusick  * Nothing to do, so just return.
8049167Ssam  */
80537737Smckusick /* ARGSUSED */
80637737Smckusick ufs_seek(vp, oldoff, newoff, cred)
80737737Smckusick 	struct vnode *vp;
80837737Smckusick 	off_t oldoff, newoff;
80937737Smckusick 	struct ucred *cred;
8107701Ssam {
8117701Ssam 
81237737Smckusick 	return (0);
81337737Smckusick }
81437737Smckusick 
81537737Smckusick /*
81637737Smckusick  * ufs remove
81737737Smckusick  * Hard to avoid races here, especially
81837737Smckusick  * in unlinking directories.
81937737Smckusick  */
82037737Smckusick ufs_remove(ndp)
82137737Smckusick 	struct nameidata *ndp;
82237737Smckusick {
82337737Smckusick 	register struct inode *ip, *dp;
82437737Smckusick 	int error;
82537737Smckusick 
82637737Smckusick 	ip = VTOI(ndp->ni_vp);
82737737Smckusick 	dp = VTOI(ndp->ni_dvp);
82837737Smckusick 	error = dirremove(ndp);
82937737Smckusick 	if (!error) {
83037737Smckusick 		ip->i_nlink--;
83137737Smckusick 		ip->i_flag |= ICHG;
8327701Ssam 	}
83337737Smckusick 	if (dp == ip)
83437737Smckusick 		vrele(ITOV(ip));
83537737Smckusick 	else
83637737Smckusick 		iput(ip);
83737737Smckusick 	iput(dp);
83837737Smckusick 	return (error);
8397701Ssam }
8407701Ssam 
8419167Ssam /*
84237737Smckusick  * link vnode call
8439167Ssam  */
84437737Smckusick ufs_link(vp, ndp)
84537737Smckusick 	register struct vnode *vp;
84637737Smckusick 	register struct nameidata *ndp;
8479167Ssam {
84837737Smckusick 	register struct inode *ip = VTOI(vp);
84937737Smckusick 	int error;
8509167Ssam 
85146251Smckusick 	if ((unsigned short)ip->i_nlink >= LINK_MAX)
85246251Smckusick 		return (EMLINK);
85337737Smckusick 	if (ndp->ni_dvp != vp)
85437737Smckusick 		ILOCK(ip);
85537737Smckusick 	ip->i_nlink++;
85637737Smckusick 	ip->i_flag |= ICHG;
85737737Smckusick 	error = iupdat(ip, &time, &time, 1);
85837737Smckusick 	if (!error)
85937737Smckusick 		error = direnter(ip, ndp);
86037737Smckusick 	if (ndp->ni_dvp != vp)
86137737Smckusick 		IUNLOCK(ip);
86237737Smckusick 	if (error) {
86337737Smckusick 		ip->i_nlink--;
86430598Smckusick 		ip->i_flag |= ICHG;
86537737Smckusick 	}
86637737Smckusick 	return (error);
8679167Ssam }
8689167Ssam 
8699167Ssam /*
8709167Ssam  * Rename system call.
8719167Ssam  * 	rename("foo", "bar");
8729167Ssam  * is essentially
8739167Ssam  *	unlink("bar");
8749167Ssam  *	link("foo", "bar");
8759167Ssam  *	unlink("foo");
8769167Ssam  * but ``atomically''.  Can't do full commit without saving state in the
8779167Ssam  * inode on disk which isn't feasible at this time.  Best we can do is
8789167Ssam  * always guarantee the target exists.
8799167Ssam  *
8809167Ssam  * Basic algorithm is:
8819167Ssam  *
8829167Ssam  * 1) Bump link count on source while we're linking it to the
88337737Smckusick  *    target.  This also ensure the inode won't be deleted out
88416776Smckusick  *    from underneath us while we work (it may be truncated by
88516776Smckusick  *    a concurrent `trunc' or `open' for creation).
8869167Ssam  * 2) Link source to destination.  If destination already exists,
8879167Ssam  *    delete it first.
88816776Smckusick  * 3) Unlink source reference to inode if still around. If a
88916776Smckusick  *    directory was moved and the parent of the destination
8909167Ssam  *    is different from the source, patch the ".." entry in the
8919167Ssam  *    directory.
8929167Ssam  */
89337737Smckusick ufs_rename(fndp, tndp)
89437737Smckusick 	register struct nameidata *fndp, *tndp;
8957701Ssam {
8969167Ssam 	register struct inode *ip, *xp, *dp;
89716776Smckusick 	struct dirtemplate dirbuf;
89816776Smckusick 	int doingdirectory = 0, oldparent = 0, newparent = 0;
89910051Ssam 	int error = 0;
9007701Ssam 
90137737Smckusick 	dp = VTOI(fndp->ni_dvp);
90237737Smckusick 	ip = VTOI(fndp->ni_vp);
90337737Smckusick 	ILOCK(ip);
9049167Ssam 	if ((ip->i_mode&IFMT) == IFDIR) {
90537737Smckusick 		register struct direct *d = &fndp->ni_dent;
9069167Ssam 
9079167Ssam 		/*
90811641Ssam 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
9099167Ssam 		 */
91037737Smckusick 		if ((d->d_namlen == 1 && d->d_name[0] == '.') || dp == ip ||
91137737Smckusick 		    fndp->ni_isdotdot || (ip->i_flag & IRENAME)) {
91242466Smckusick 			VOP_ABORTOP(tndp);
91342466Smckusick 			vput(tndp->ni_dvp);
91442466Smckusick 			if (tndp->ni_vp)
91542466Smckusick 				vput(tndp->ni_vp);
91642466Smckusick 			VOP_ABORTOP(fndp);
91742466Smckusick 			vrele(fndp->ni_dvp);
91842466Smckusick 			vput(fndp->ni_vp);
91937737Smckusick 			return (EINVAL);
9209167Ssam 		}
92116776Smckusick 		ip->i_flag |= IRENAME;
9229167Ssam 		oldparent = dp->i_number;
9239167Ssam 		doingdirectory++;
9249167Ssam 	}
92537737Smckusick 	vrele(fndp->ni_dvp);
9269167Ssam 
9279167Ssam 	/*
9289167Ssam 	 * 1) Bump link count while we're moving stuff
9299167Ssam 	 *    around.  If we crash somewhere before
9309167Ssam 	 *    completing our work, the link count
9319167Ssam 	 *    may be wrong, but correctable.
9329167Ssam 	 */
9339167Ssam 	ip->i_nlink++;
9349167Ssam 	ip->i_flag |= ICHG;
93537737Smckusick 	error = iupdat(ip, &time, &time, 1);
93616664Smckusick 	IUNLOCK(ip);
9379167Ssam 
9389167Ssam 	/*
9399167Ssam 	 * When the target exists, both the directory
94037737Smckusick 	 * and target vnodes are returned locked.
9419167Ssam 	 */
94237737Smckusick 	dp = VTOI(tndp->ni_dvp);
94337737Smckusick 	xp = NULL;
94437737Smckusick 	if (tndp->ni_vp)
94537737Smckusick 		xp = VTOI(tndp->ni_vp);
9469167Ssam 	/*
94711641Ssam 	 * If ".." must be changed (ie the directory gets a new
94812816Smckusick 	 * parent) then the source directory must not be in the
94912816Smckusick 	 * directory heirarchy above the target, as this would
95012816Smckusick 	 * orphan everything below the source directory. Also
95112816Smckusick 	 * the user must have write permission in the source so
95212816Smckusick 	 * as to be able to change "..". We must repeat the call
95312816Smckusick 	 * to namei, as the parent directory is unlocked by the
95412816Smckusick 	 * call to checkpath().
95511641Ssam 	 */
95616776Smckusick 	if (oldparent != dp->i_number)
95716776Smckusick 		newparent = dp->i_number;
95816776Smckusick 	if (doingdirectory && newparent) {
95941466Smckusick 		VOP_LOCK(fndp->ni_vp);
96041466Smckusick 		error = ufs_access(fndp->ni_vp, VWRITE, tndp->ni_cred);
96141466Smckusick 		VOP_UNLOCK(fndp->ni_vp);
96241466Smckusick 		if (error)
96312816Smckusick 			goto bad;
96446511Smckusick 		tndp->ni_nameiop &= ~(MODMASK | OPMASK);
96546511Smckusick 		tndp->ni_nameiop |= RENAME | LOCKPARENT | LOCKLEAF | NOCACHE;
96612816Smckusick 		do {
96737737Smckusick 			dp = VTOI(tndp->ni_dvp);
96812816Smckusick 			if (xp != NULL)
96938069Smckusick 				iput(xp);
97037737Smckusick 			if (error = checkpath(ip, dp, tndp->ni_cred))
97112816Smckusick 				goto out;
97237737Smckusick 			if (error = namei(tndp))
97312816Smckusick 				goto out;
97437737Smckusick 			xp = NULL;
97537737Smckusick 			if (tndp->ni_vp)
97637737Smckusick 				xp = VTOI(tndp->ni_vp);
97737737Smckusick 		} while (dp != VTOI(tndp->ni_dvp));
97812816Smckusick 	}
97911641Ssam 	/*
9809167Ssam 	 * 2) If target doesn't exist, link the target
9819167Ssam 	 *    to the source and unlink the source.
9829167Ssam 	 *    Otherwise, rewrite the target directory
9839167Ssam 	 *    entry to reference the source inode and
9849167Ssam 	 *    expunge the original entry's existence.
9859167Ssam 	 */
9869167Ssam 	if (xp == NULL) {
98737737Smckusick 		if (dp->i_dev != ip->i_dev)
98837737Smckusick 			panic("rename: EXDEV");
9899167Ssam 		/*
99016776Smckusick 		 * Account for ".." in new directory.
99116776Smckusick 		 * When source and destination have the same
99216776Smckusick 		 * parent we don't fool with the link count.
9939167Ssam 		 */
99416776Smckusick 		if (doingdirectory && newparent) {
99546251Smckusick 			if ((unsigned short)dp->i_nlink >= LINK_MAX) {
99646251Smckusick 				error = EMLINK;
99746251Smckusick 				goto bad;
99846251Smckusick 			}
9999167Ssam 			dp->i_nlink++;
10009167Ssam 			dp->i_flag |= ICHG;
100146251Smckusick 			if (error = iupdat(dp, &time, &time, 1))
100246251Smckusick 				goto bad;
10039167Ssam 		}
100437737Smckusick 		if (error = direnter(ip, tndp))
10059167Ssam 			goto out;
10069167Ssam 	} else {
100737737Smckusick 		if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
100837737Smckusick 			panic("rename: EXDEV");
10099167Ssam 		/*
101010590Ssam 		 * Short circuit rename(foo, foo).
101110590Ssam 		 */
101210590Ssam 		if (xp->i_number == ip->i_number)
101337737Smckusick 			panic("rename: same file");
101410590Ssam 		/*
101524433Sbloom 		 * If the parent directory is "sticky", then the user must
101624433Sbloom 		 * own the parent directory, or the destination of the rename,
101724433Sbloom 		 * otherwise the destination may not be changed (except by
101824433Sbloom 		 * root). This implements append-only directories.
101924433Sbloom 		 */
102037737Smckusick 		if ((dp->i_mode & ISVTX) && tndp->ni_cred->cr_uid != 0 &&
102137737Smckusick 		    tndp->ni_cred->cr_uid != dp->i_uid &&
102237737Smckusick 		    xp->i_uid != tndp->ni_cred->cr_uid) {
102324433Sbloom 			error = EPERM;
102424433Sbloom 			goto bad;
102524433Sbloom 		}
102624433Sbloom 		/*
102710051Ssam 		 * Target must be empty if a directory
102810051Ssam 		 * and have no links to it.
10299167Ssam 		 * Also, insure source and target are
10309167Ssam 		 * compatible (both directories, or both
10319167Ssam 		 * not directories).
10329167Ssam 		 */
10339167Ssam 		if ((xp->i_mode&IFMT) == IFDIR) {
103437737Smckusick 			if (!dirempty(xp, dp->i_number, tndp->ni_cred) ||
103537737Smckusick 			    xp->i_nlink > 2) {
103610051Ssam 				error = ENOTEMPTY;
10379167Ssam 				goto bad;
10389167Ssam 			}
10399167Ssam 			if (!doingdirectory) {
104010051Ssam 				error = ENOTDIR;
10419167Ssam 				goto bad;
10429167Ssam 			}
104337737Smckusick 			cache_purge(ITOV(dp));
10449167Ssam 		} else if (doingdirectory) {
104510051Ssam 			error = EISDIR;
10469167Ssam 			goto bad;
10479167Ssam 		}
104837737Smckusick 		if (error = dirrewrite(dp, ip, tndp))
104937737Smckusick 			goto bad;
105045354Smckusick 		/*
105145354Smckusick 		 * If the target directory is in the same
105245354Smckusick 		 * directory as the source directory,
105345354Smckusick 		 * decrement the link count on the parent
105445354Smckusick 		 * of the target directory.
105545354Smckusick 		 */
105645354Smckusick 		 if (doingdirectory && !newparent) {
105745354Smckusick 			dp->i_nlink--;
105845354Smckusick 			dp->i_flag |= ICHG;
105945354Smckusick 		}
106037737Smckusick 		vput(ITOV(dp));
10619167Ssam 		/*
106210051Ssam 		 * Adjust the link count of the target to
106310051Ssam 		 * reflect the dirrewrite above.  If this is
106410051Ssam 		 * a directory it is empty and there are
106510051Ssam 		 * no links to it, so we can squash the inode and
106610051Ssam 		 * any space associated with it.  We disallowed
106710051Ssam 		 * renaming over top of a directory with links to
106816776Smckusick 		 * it above, as the remaining link would point to
106916776Smckusick 		 * a directory without "." or ".." entries.
10709167Ssam 		 */
107110051Ssam 		xp->i_nlink--;
10729167Ssam 		if (doingdirectory) {
107310051Ssam 			if (--xp->i_nlink != 0)
107410051Ssam 				panic("rename: linked directory");
107539674Smckusick 			error = itrunc(xp, (u_long)0, IO_SYNC);
107610051Ssam 		}
10779167Ssam 		xp->i_flag |= ICHG;
107838398Smckusick 		iput(xp);
107910246Ssam 		xp = NULL;
10809167Ssam 	}
10819167Ssam 
10829167Ssam 	/*
10839167Ssam 	 * 3) Unlink the source.
10849167Ssam 	 */
108546511Smckusick 	fndp->ni_nameiop &= ~(MODMASK | OPMASK);
108646511Smckusick 	fndp->ni_nameiop |= DELETE | LOCKPARENT | LOCKLEAF;
108737737Smckusick 	(void)namei(fndp);
108837737Smckusick 	if (fndp->ni_vp != NULL) {
108937737Smckusick 		xp = VTOI(fndp->ni_vp);
109037737Smckusick 		dp = VTOI(fndp->ni_dvp);
109137737Smckusick 	} else {
109246250Smckusick 		/*
109346250Smckusick 		 * From name has disappeared.
109446250Smckusick 		 */
109546250Smckusick 		if (doingdirectory)
109646250Smckusick 			panic("rename: lost dir entry");
109746250Smckusick 		vrele(ITOV(ip));
109846250Smckusick 		return (0);
109937737Smckusick 	}
11009167Ssam 	/*
110137737Smckusick 	 * Ensure that the directory entry still exists and has not
110216776Smckusick 	 * changed while the new name has been entered. If the source is
110316776Smckusick 	 * a file then the entry may have been unlinked or renamed. In
110416776Smckusick 	 * either case there is no further work to be done. If the source
110516776Smckusick 	 * is a directory then it cannot have been rmdir'ed; its link
110616776Smckusick 	 * count of three would cause a rmdir to fail with ENOTEMPTY.
110737737Smckusick 	 * The IRENAME flag ensures that it cannot be moved by another
110816776Smckusick 	 * rename.
11099167Ssam 	 */
111017758Smckusick 	if (xp != ip) {
111116776Smckusick 		if (doingdirectory)
111217758Smckusick 			panic("rename: lost dir entry");
111316776Smckusick 	} else {
11149167Ssam 		/*
111516776Smckusick 		 * If the source is a directory with a
111616776Smckusick 		 * new parent, the link count of the old
111716776Smckusick 		 * parent directory must be decremented
111816776Smckusick 		 * and ".." set to point to the new parent.
11199167Ssam 		 */
112016776Smckusick 		if (doingdirectory && newparent) {
11219167Ssam 			dp->i_nlink--;
11229167Ssam 			dp->i_flag |= ICHG;
112339597Smckusick 			error = vn_rdwr(UIO_READ, ITOV(xp), (caddr_t)&dirbuf,
112437737Smckusick 				sizeof (struct dirtemplate), (off_t)0,
112539597Smckusick 				UIO_SYSSPACE, IO_NODELOCKED,
112639597Smckusick 				tndp->ni_cred, (int *)0);
112716776Smckusick 			if (error == 0) {
112816776Smckusick 				if (dirbuf.dotdot_namlen != 2 ||
112916776Smckusick 				    dirbuf.dotdot_name[0] != '.' ||
113016776Smckusick 				    dirbuf.dotdot_name[1] != '.') {
113139610Smckusick 					dirbad(xp, 12, "rename: mangled dir");
113216776Smckusick 				} else {
113316776Smckusick 					dirbuf.dotdot_ino = newparent;
113439597Smckusick 					(void) vn_rdwr(UIO_WRITE, ITOV(xp),
113516776Smckusick 					    (caddr_t)&dirbuf,
113616776Smckusick 					    sizeof (struct dirtemplate),
113737740Smckusick 					    (off_t)0, UIO_SYSSPACE,
113839597Smckusick 					    IO_NODELOCKED|IO_SYNC,
113937737Smckusick 					    tndp->ni_cred, (int *)0);
114037737Smckusick 					cache_purge(ITOV(dp));
114116776Smckusick 				}
114216776Smckusick 			}
11439167Ssam 		}
114437737Smckusick 		error = dirremove(fndp);
114537737Smckusick 		if (!error) {
114616776Smckusick 			xp->i_nlink--;
114716776Smckusick 			xp->i_flag |= ICHG;
11489167Ssam 		}
114916776Smckusick 		xp->i_flag &= ~IRENAME;
11509167Ssam 	}
11519167Ssam 	if (dp)
115237737Smckusick 		vput(ITOV(dp));
115316776Smckusick 	if (xp)
115437737Smckusick 		vput(ITOV(xp));
115537737Smckusick 	vrele(ITOV(ip));
115637737Smckusick 	return (error);
11579167Ssam 
11589167Ssam bad:
11599167Ssam 	if (xp)
116037737Smckusick 		vput(ITOV(xp));
116137737Smckusick 	vput(ITOV(dp));
11629167Ssam out:
11639167Ssam 	ip->i_nlink--;
11649167Ssam 	ip->i_flag |= ICHG;
116537737Smckusick 	vrele(ITOV(ip));
116637737Smckusick 	return (error);
11677701Ssam }
11687701Ssam 
11697535Sroot /*
117012756Ssam  * A virgin directory (no blushing please).
117112756Ssam  */
117212756Ssam struct dirtemplate mastertemplate = {
117312756Ssam 	0, 12, 1, ".",
117412756Ssam 	0, DIRBLKSIZ - 12, 2, ".."
117512756Ssam };
117612756Ssam 
117712756Ssam /*
117812756Ssam  * Mkdir system call
117912756Ssam  */
118037737Smckusick ufs_mkdir(ndp, vap)
118137737Smckusick 	struct nameidata *ndp;
118237737Smckusick 	struct vattr *vap;
118312756Ssam {
118412756Ssam 	register struct inode *ip, *dp;
118537737Smckusick 	struct inode *tip;
118637737Smckusick 	struct vnode *dvp;
118712756Ssam 	struct dirtemplate dirtemplate;
118837737Smckusick 	int error;
118937737Smckusick 	int dmode;
119012756Ssam 
119137737Smckusick 	dvp = ndp->ni_dvp;
119237737Smckusick 	dp = VTOI(dvp);
119346251Smckusick 	if ((unsigned short)dp->i_nlink >= LINK_MAX) {
119446251Smckusick 		iput(dp);
119546251Smckusick 		return (EMLINK);
119646251Smckusick 	}
119737737Smckusick 	dmode = vap->va_mode&0777;
119837737Smckusick 	dmode |= IFDIR;
119912756Ssam 	/*
120012756Ssam 	 * Must simulate part of maknode here
120112756Ssam 	 * in order to acquire the inode, but
120212756Ssam 	 * not have it entered in the parent
120312756Ssam 	 * directory.  The entry is made later
120412756Ssam 	 * after writing "." and ".." entries out.
120512756Ssam 	 */
120641312Smckusick 	if (error = ialloc(dp, dirpref(dp->i_fs), dmode, ndp->ni_cred, &tip)) {
120712756Ssam 		iput(dp);
120837737Smckusick 		return (error);
120912756Ssam 	}
121037737Smckusick 	ip = tip;
121141312Smckusick 	ip->i_uid = ndp->ni_cred->cr_uid;
121241312Smckusick 	ip->i_gid = dp->i_gid;
121312756Ssam #ifdef QUOTA
121441312Smckusick 	if ((error = getinoquota(ip)) ||
121541312Smckusick 	    (error = chkiq(ip, 1, ndp->ni_cred, 0))) {
121641312Smckusick 		ifree(ip, ip->i_number, dmode);
121741312Smckusick 		iput(ip);
121841312Smckusick 		iput(dp);
121941312Smckusick 		return (error);
122041312Smckusick 	}
122112756Ssam #endif
122212756Ssam 	ip->i_flag |= IACC|IUPD|ICHG;
122337737Smckusick 	ip->i_mode = dmode;
122437737Smckusick 	ITOV(ip)->v_type = VDIR;	/* Rest init'd in iget() */
122512756Ssam 	ip->i_nlink = 2;
122637737Smckusick 	error = iupdat(ip, &time, &time, 1);
122712756Ssam 
122812756Ssam 	/*
122912756Ssam 	 * Bump link count in parent directory
123012756Ssam 	 * to reflect work done below.  Should
123112756Ssam 	 * be done before reference is created
123212756Ssam 	 * so reparation is possible if we crash.
123312756Ssam 	 */
123412756Ssam 	dp->i_nlink++;
123512756Ssam 	dp->i_flag |= ICHG;
123637737Smckusick 	error = iupdat(dp, &time, &time, 1);
123712756Ssam 
123812756Ssam 	/*
123912756Ssam 	 * Initialize directory with "."
124012756Ssam 	 * and ".." from static template.
124112756Ssam 	 */
124212756Ssam 	dirtemplate = mastertemplate;
124312756Ssam 	dirtemplate.dot_ino = ip->i_number;
124412756Ssam 	dirtemplate.dotdot_ino = dp->i_number;
124539597Smckusick 	error = vn_rdwr(UIO_WRITE, ITOV(ip), (caddr_t)&dirtemplate,
124637737Smckusick 		sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
124739597Smckusick 		IO_NODELOCKED|IO_SYNC, ndp->ni_cred, (int *)0);
124837737Smckusick 	if (error) {
124912756Ssam 		dp->i_nlink--;
125012756Ssam 		dp->i_flag |= ICHG;
125112756Ssam 		goto bad;
125212756Ssam 	}
125343288Smckusick 	if (DIRBLKSIZ > dp->i_fs->fs_fsize) {
125437737Smckusick 		panic("mkdir: blksize");     /* XXX - should grow w/balloc() */
125543288Smckusick 	} else {
125618103Smckusick 		ip->i_size = DIRBLKSIZ;
125743288Smckusick 		ip->i_flag |= ICHG;
125843288Smckusick 	}
125912756Ssam 	/*
126012756Ssam 	 * Directory all set up, now
126112756Ssam 	 * install the entry for it in
126212756Ssam 	 * the parent directory.
126312756Ssam 	 */
126437737Smckusick 	error = direnter(ip, ndp);
126512756Ssam 	dp = NULL;
126637737Smckusick 	if (error) {
126746511Smckusick 		ndp->ni_nameiop &= ~(MODMASK | OPMASK);
126846511Smckusick 		ndp->ni_nameiop |= LOOKUP | NOCACHE;
126937737Smckusick 		error = namei(ndp);
127037737Smckusick 		if (!error) {
127137737Smckusick 			dp = VTOI(ndp->ni_vp);
127212756Ssam 			dp->i_nlink--;
127312756Ssam 			dp->i_flag |= ICHG;
127412756Ssam 		}
127512756Ssam 	}
127612756Ssam bad:
127712756Ssam 	/*
127812756Ssam 	 * No need to do an explicit itrunc here,
127937737Smckusick 	 * vrele will do this for us because we set
128012756Ssam 	 * the link count to 0.
128112756Ssam 	 */
128237737Smckusick 	if (error) {
128312756Ssam 		ip->i_nlink = 0;
128412756Ssam 		ip->i_flag |= ICHG;
128538144Smckusick 		iput(ip);
128638144Smckusick 	} else
128738144Smckusick 		ndp->ni_vp = ITOV(ip);
128812756Ssam 	if (dp)
128912756Ssam 		iput(dp);
129037737Smckusick 	return (error);
129112756Ssam }
129212756Ssam 
129312756Ssam /*
129412756Ssam  * Rmdir system call.
129512756Ssam  */
129637737Smckusick ufs_rmdir(ndp)
129737737Smckusick 	register struct nameidata *ndp;
129812756Ssam {
129912756Ssam 	register struct inode *ip, *dp;
130037737Smckusick 	int error = 0;
130112756Ssam 
130237737Smckusick 	ip = VTOI(ndp->ni_vp);
130337737Smckusick 	dp = VTOI(ndp->ni_dvp);
130412756Ssam 	/*
130512756Ssam 	 * No rmdir "." please.
130612756Ssam 	 */
130712756Ssam 	if (dp == ip) {
130837737Smckusick 		vrele(ITOV(dp));
130912756Ssam 		iput(ip);
131037737Smckusick 		return (EINVAL);
131112756Ssam 	}
131212756Ssam 	/*
131312756Ssam 	 * Verify the directory is empty (and valid).
131412756Ssam 	 * (Rmdir ".." won't be valid since
131512756Ssam 	 *  ".." will contain a reference to
131612756Ssam 	 *  the current directory and thus be
131712756Ssam 	 *  non-empty.)
131812756Ssam 	 */
131937737Smckusick 	if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number, ndp->ni_cred)) {
132037737Smckusick 		error = ENOTEMPTY;
132112756Ssam 		goto out;
132212756Ssam 	}
132312756Ssam 	/*
132412756Ssam 	 * Delete reference to directory before purging
132512756Ssam 	 * inode.  If we crash in between, the directory
132612756Ssam 	 * will be reattached to lost+found,
132712756Ssam 	 */
132837737Smckusick 	if (error = dirremove(ndp))
132912756Ssam 		goto out;
133012756Ssam 	dp->i_nlink--;
133112756Ssam 	dp->i_flag |= ICHG;
133237737Smckusick 	cache_purge(ITOV(dp));
133312756Ssam 	iput(dp);
133437737Smckusick 	ndp->ni_dvp = NULL;
133512756Ssam 	/*
133612756Ssam 	 * Truncate inode.  The only stuff left
133712756Ssam 	 * in the directory is "." and "..".  The
133812756Ssam 	 * "." reference is inconsequential since
133912756Ssam 	 * we're quashing it.  The ".." reference
134012756Ssam 	 * has already been adjusted above.  We've
134112756Ssam 	 * removed the "." reference and the reference
134212756Ssam 	 * in the parent directory, but there may be
134312756Ssam 	 * other hard links so decrement by 2 and
134412756Ssam 	 * worry about them later.
134512756Ssam 	 */
134612756Ssam 	ip->i_nlink -= 2;
134739674Smckusick 	error = itrunc(ip, (u_long)0, IO_SYNC);
134837737Smckusick 	cache_purge(ITOV(ip));
134912756Ssam out:
135037737Smckusick 	if (ndp->ni_dvp)
135112756Ssam 		iput(dp);
135212756Ssam 	iput(ip);
135337737Smckusick 	return (error);
135412756Ssam }
135512756Ssam 
135637737Smckusick /*
135737737Smckusick  * symlink -- make a symbolic link
135837737Smckusick  */
135937737Smckusick ufs_symlink(ndp, vap, target)
136037737Smckusick 	struct nameidata *ndp;
136137737Smckusick 	struct vattr *vap;
136237737Smckusick 	char *target;
136312756Ssam {
136437737Smckusick 	struct inode *ip;
136537737Smckusick 	int error;
136612756Ssam 
136737737Smckusick 	error = maknode(IFLNK | vap->va_mode, ndp, &ip);
136837737Smckusick 	if (error)
136937737Smckusick 		return (error);
137039597Smckusick 	error = vn_rdwr(UIO_WRITE, ITOV(ip), target, strlen(target), (off_t)0,
137139597Smckusick 		UIO_SYSSPACE, IO_NODELOCKED, ndp->ni_cred, (int *)0);
137237737Smckusick 	iput(ip);
137337737Smckusick 	return (error);
137437737Smckusick }
137537737Smckusick 
137637737Smckusick /*
137737737Smckusick  * Vnode op for read and write
137837737Smckusick  */
137940345Smckusick ufs_readdir(vp, uio, cred, eofflagp)
138037737Smckusick 	struct vnode *vp;
138137737Smckusick 	register struct uio *uio;
138237737Smckusick 	struct ucred *cred;
138340345Smckusick 	int *eofflagp;
138437737Smckusick {
138539597Smckusick 	int count, lost, error;
138637737Smckusick 
138737737Smckusick 	count = uio->uio_resid;
138837737Smckusick 	count &= ~(DIRBLKSIZ - 1);
138939597Smckusick 	lost = uio->uio_resid - count;
139039597Smckusick 	if (count < DIRBLKSIZ || (uio->uio_offset & (DIRBLKSIZ -1)))
139137737Smckusick 		return (EINVAL);
139237737Smckusick 	uio->uio_resid = count;
139337737Smckusick 	uio->uio_iov->iov_len = count;
139439597Smckusick 	error = ufs_read(vp, uio, 0, cred);
139539597Smckusick 	uio->uio_resid += lost;
139640345Smckusick 	if ((VTOI(vp)->i_size - uio->uio_offset) <= 0)
139740345Smckusick 		*eofflagp = 1;
139840345Smckusick 	else
139940345Smckusick 		*eofflagp = 0;
140037737Smckusick 	return (error);
140137737Smckusick }
140237737Smckusick 
140337737Smckusick /*
140437737Smckusick  * Return target name of a symbolic link
140537737Smckusick  */
140637737Smckusick ufs_readlink(vp, uiop, cred)
140737737Smckusick 	struct vnode *vp;
140837737Smckusick 	struct uio *uiop;
140937737Smckusick 	struct ucred *cred;
141037737Smckusick {
141137737Smckusick 
141239597Smckusick 	return (ufs_read(vp, uiop, 0, cred));
141337737Smckusick }
141437737Smckusick 
141537737Smckusick /*
141637737Smckusick  * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually
141742466Smckusick  * done. Nothing to do at the moment.
141837737Smckusick  */
141942466Smckusick /* ARGSUSED */
142037737Smckusick ufs_abortop(ndp)
142142466Smckusick 	struct nameidata *ndp;
142237737Smckusick {
142337737Smckusick 
142442466Smckusick 	return (0);
142512756Ssam }
142612756Ssam 
142739909Smckusick /*
142839909Smckusick  * Lock an inode.
142939909Smckusick  */
143037737Smckusick ufs_lock(vp)
143137737Smckusick 	struct vnode *vp;
143237737Smckusick {
143337737Smckusick 	register struct inode *ip = VTOI(vp);
143437737Smckusick 
143537737Smckusick 	ILOCK(ip);
143637737Smckusick 	return (0);
143737737Smckusick }
143837737Smckusick 
143939909Smckusick /*
144039909Smckusick  * Unlock an inode.
144139909Smckusick  */
144237737Smckusick ufs_unlock(vp)
144337737Smckusick 	struct vnode *vp;
144437737Smckusick {
144537737Smckusick 	register struct inode *ip = VTOI(vp);
144637737Smckusick 
144737737Smckusick 	if (!(ip->i_flag & ILOCKED))
144837737Smckusick 		panic("ufs_unlock NOT LOCKED");
144937737Smckusick 	IUNLOCK(ip);
145037737Smckusick 	return (0);
145137737Smckusick }
145237737Smckusick 
145312756Ssam /*
145439909Smckusick  * Check for a locked inode.
145539909Smckusick  */
145639909Smckusick ufs_islocked(vp)
145739909Smckusick 	struct vnode *vp;
145839909Smckusick {
145939909Smckusick 
146039909Smckusick 	if (VTOI(vp)->i_flag & ILOCKED)
146139909Smckusick 		return (1);
146239909Smckusick 	return (0);
146339909Smckusick }
146439909Smckusick 
146539909Smckusick /*
146637737Smckusick  * Get access to bmap
146712756Ssam  */
146837737Smckusick ufs_bmap(vp, bn, vpp, bnp)
146937737Smckusick 	struct vnode *vp;
147037737Smckusick 	daddr_t bn;
147137737Smckusick 	struct vnode **vpp;
147237737Smckusick 	daddr_t *bnp;
147312756Ssam {
147437737Smckusick 	struct inode *ip = VTOI(vp);
147512756Ssam 
147637737Smckusick 	if (vpp != NULL)
147737737Smckusick 		*vpp = ip->i_devvp;
147837737Smckusick 	if (bnp == NULL)
147937737Smckusick 		return (0);
148041538Smckusick 	return (bmap(ip, bn, bnp));
148112756Ssam }
148237737Smckusick 
148337737Smckusick /*
148441538Smckusick  * Calculate the logical to physical mapping if not done already,
148541538Smckusick  * then call the device strategy routine.
148637737Smckusick  */
148741538Smckusick int checkoverlap = 0;
148839674Smckusick 
148937737Smckusick ufs_strategy(bp)
149037737Smckusick 	register struct buf *bp;
149137737Smckusick {
149239674Smckusick 	register struct inode *ip = VTOI(bp->b_vp);
149339674Smckusick 	struct vnode *vp;
149439674Smckusick 	int error;
149539674Smckusick 
149639674Smckusick 	if (bp->b_vp->v_type == VBLK || bp->b_vp->v_type == VCHR)
149739674Smckusick 		panic("ufs_strategy: spec");
149839674Smckusick 	if (bp->b_blkno == bp->b_lblkno) {
149939674Smckusick 		if (error = bmap(ip, bp->b_lblkno, &bp->b_blkno))
150039674Smckusick 			return (error);
150139896Smckusick 		if ((long)bp->b_blkno == -1)
150239674Smckusick 			clrbuf(bp);
150339674Smckusick 	}
150439896Smckusick 	if ((long)bp->b_blkno == -1) {
150539896Smckusick 		biodone(bp);
150639674Smckusick 		return (0);
150739896Smckusick 	}
150841538Smckusick #ifdef DIAGNOSTIC
150939674Smckusick 	if (checkoverlap) {
151041538Smckusick 		register struct buf *ep;
151141538Smckusick 		struct buf *ebp;
151241538Smckusick 		daddr_t start, last;
151341538Smckusick 
151439674Smckusick 		ebp = &buf[nbuf];
151539674Smckusick 		start = bp->b_blkno;
151639674Smckusick 		last = start + btodb(bp->b_bcount) - 1;
151739674Smckusick 		for (ep = buf; ep < ebp; ep++) {
151839674Smckusick 			if (ep == bp || (ep->b_flags & B_INVAL) ||
151941396Smckusick 			    ep->b_vp == NULLVP)
152039674Smckusick 				continue;
152139674Smckusick 			if (VOP_BMAP(ep->b_vp, (daddr_t)0, &vp, (daddr_t)0))
152239674Smckusick 				continue;
152339674Smckusick 			if (vp != ip->i_devvp)
152439674Smckusick 				continue;
152539674Smckusick 			/* look for overlap */
152639674Smckusick 			if (ep->b_bcount == 0 || ep->b_blkno > last ||
152739674Smckusick 			    ep->b_blkno + btodb(ep->b_bcount) <= start)
152839674Smckusick 				continue;
152939896Smckusick 			vprint("Disk overlap", vp);
153039896Smckusick 			printf("\tstart %d, end %d overlap start %d, end %d\n",
153139896Smckusick 				start, last, ep->b_blkno,
153239896Smckusick 				ep->b_blkno + btodb(ep->b_bcount) - 1);
153341538Smckusick 			panic("Disk buffer overlap");
153439674Smckusick 		}
153539674Smckusick 	}
153641538Smckusick #endif /* DIAGNOSTIC */
153739674Smckusick 	vp = ip->i_devvp;
153839674Smckusick 	bp->b_dev = vp->v_rdev;
153939674Smckusick 	(*(vp->v_op->vn_strategy))(bp);
154037737Smckusick 	return (0);
154137737Smckusick }
154237737Smckusick 
154337737Smckusick /*
154439674Smckusick  * Print out the contents of an inode.
154539674Smckusick  */
154639674Smckusick ufs_print(vp)
154739674Smckusick 	struct vnode *vp;
154839674Smckusick {
154939674Smckusick 	register struct inode *ip = VTOI(vp);
155039674Smckusick 
155140293Smckusick 	printf("tag VT_UFS, ino %d, on dev %d, %d", ip->i_number,
155240293Smckusick 		major(ip->i_dev), minor(ip->i_dev));
155340293Smckusick #ifdef FIFO
155440293Smckusick 	if (vp->v_type == VFIFO)
155540293Smckusick 		fifo_printinfo(vp);
155640293Smckusick #endif /* FIFO */
155740293Smckusick 	printf("%s\n", (ip->i_flag & ILOCKED) ? " (LOCKED)" : "");
155839900Smckusick 	if (ip->i_spare0 == 0)
155939900Smckusick 		return;
156039900Smckusick 	printf("\towner pid %d", ip->i_spare0);
156139900Smckusick 	if (ip->i_spare1)
156239900Smckusick 		printf(" waiting pid %d", ip->i_spare1);
156339900Smckusick 	printf("\n");
156439674Smckusick }
156539674Smckusick 
156639674Smckusick /*
156739628Smckusick  * Read wrapper for special devices.
156839628Smckusick  */
156939628Smckusick ufsspec_read(vp, uio, ioflag, cred)
157039628Smckusick 	struct vnode *vp;
157139628Smckusick 	struct uio *uio;
157239628Smckusick 	int ioflag;
157339628Smckusick 	struct ucred *cred;
157439628Smckusick {
157539628Smckusick 
157639628Smckusick 	/*
157739628Smckusick 	 * Set access flag.
157839628Smckusick 	 */
157939628Smckusick 	VTOI(vp)->i_flag |= IACC;
158039628Smckusick 	return (spec_read(vp, uio, ioflag, cred));
158139628Smckusick }
158239628Smckusick 
158339628Smckusick /*
158439628Smckusick  * Write wrapper for special devices.
158539628Smckusick  */
158639628Smckusick ufsspec_write(vp, uio, ioflag, cred)
158739628Smckusick 	struct vnode *vp;
158839628Smckusick 	struct uio *uio;
158939628Smckusick 	int ioflag;
159039628Smckusick 	struct ucred *cred;
159139628Smckusick {
159239628Smckusick 
159339628Smckusick 	/*
159439628Smckusick 	 * Set update and change flags.
159539628Smckusick 	 */
159639628Smckusick 	VTOI(vp)->i_flag |= IUPD|ICHG;
159739628Smckusick 	return (spec_write(vp, uio, ioflag, cred));
159839628Smckusick }
159939628Smckusick 
160039628Smckusick /*
160139628Smckusick  * Close wrapper for special devices.
160239628Smckusick  *
160339628Smckusick  * Update the times on the inode then do device close.
160439628Smckusick  */
160539628Smckusick ufsspec_close(vp, fflag, cred)
160639628Smckusick 	struct vnode *vp;
160739628Smckusick 	int fflag;
160839628Smckusick 	struct ucred *cred;
160939628Smckusick {
161039628Smckusick 	register struct inode *ip = VTOI(vp);
161139628Smckusick 
161239815Smckusick 	if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED))
161339628Smckusick 		ITIMES(ip, &time, &time);
161439628Smckusick 	return (spec_close(vp, fflag, cred));
161539628Smckusick }
161639628Smckusick 
161740290Smckusick #ifdef FIFO
161839628Smckusick /*
161940290Smckusick  * Read wrapper for fifo's
162040290Smckusick  */
162140290Smckusick ufsfifo_read(vp, uio, ioflag, cred)
162240290Smckusick 	struct vnode *vp;
162340290Smckusick 	struct uio *uio;
162440290Smckusick 	int ioflag;
162540290Smckusick 	struct ucred *cred;
162640290Smckusick {
162740290Smckusick 
162840290Smckusick 	/*
162940290Smckusick 	 * Set access flag.
163040290Smckusick 	 */
163140290Smckusick 	VTOI(vp)->i_flag |= IACC;
163240290Smckusick 	return (fifo_read(vp, uio, ioflag, cred));
163340290Smckusick }
163440290Smckusick 
163540290Smckusick /*
163640290Smckusick  * Write wrapper for fifo's.
163740290Smckusick  */
163840290Smckusick ufsfifo_write(vp, uio, ioflag, cred)
163940290Smckusick 	struct vnode *vp;
164040290Smckusick 	struct uio *uio;
164140290Smckusick 	int ioflag;
164240290Smckusick 	struct ucred *cred;
164340290Smckusick {
164440290Smckusick 
164540290Smckusick 	/*
164640290Smckusick 	 * Set update and change flags.
164740290Smckusick 	 */
164840290Smckusick 	VTOI(vp)->i_flag |= IUPD|ICHG;
164940290Smckusick 	return (fifo_write(vp, uio, ioflag, cred));
165040290Smckusick }
165140290Smckusick 
165240290Smckusick /*
165340290Smckusick  * Close wrapper for fifo's.
165440290Smckusick  *
165540290Smckusick  * Update the times on the inode then do device close.
165640290Smckusick  */
165740290Smckusick ufsfifo_close(vp, fflag, cred)
165840290Smckusick 	struct vnode *vp;
165940290Smckusick 	int fflag;
166040290Smckusick 	struct ucred *cred;
166140290Smckusick {
166240290Smckusick 	register struct inode *ip = VTOI(vp);
166340290Smckusick 
166440290Smckusick 	if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED))
166540290Smckusick 		ITIMES(ip, &time, &time);
166640290Smckusick 	return (fifo_close(vp, fflag, cred));
166740290Smckusick }
166840290Smckusick #endif /* FIFO */
166940290Smckusick 
167040290Smckusick /*
167137737Smckusick  * Make a new file.
167237737Smckusick  */
167337737Smckusick maknode(mode, ndp, ipp)
167437737Smckusick 	int mode;
167537737Smckusick 	register struct nameidata *ndp;
167637737Smckusick 	struct inode **ipp;
167737737Smckusick {
167837737Smckusick 	register struct inode *ip;
167937737Smckusick 	struct inode *tip;
168037737Smckusick 	register struct inode *pdir = VTOI(ndp->ni_dvp);
168137737Smckusick 	ino_t ipref;
168237737Smckusick 	int error;
168337737Smckusick 
168437737Smckusick 	*ipp = 0;
168541312Smckusick 	if ((mode & IFMT) == 0)
168641312Smckusick 		mode |= IFREG;
168737737Smckusick 	if ((mode & IFMT) == IFDIR)
168837737Smckusick 		ipref = dirpref(pdir->i_fs);
168937737Smckusick 	else
169037737Smckusick 		ipref = pdir->i_number;
169141312Smckusick 	if (error = ialloc(pdir, ipref, mode, ndp->ni_cred, &tip)) {
169237737Smckusick 		iput(pdir);
169337737Smckusick 		return (error);
169437737Smckusick 	}
169537737Smckusick 	ip = tip;
169641312Smckusick 	ip->i_uid = ndp->ni_cred->cr_uid;
169741312Smckusick 	ip->i_gid = pdir->i_gid;
169837737Smckusick #ifdef QUOTA
169941312Smckusick 	if ((error = getinoquota(ip)) ||
170041312Smckusick 	    (error = chkiq(ip, 1, ndp->ni_cred, 0))) {
170141312Smckusick 		ifree(ip, ip->i_number, mode);
170241312Smckusick 		iput(ip);
170341312Smckusick 		iput(pdir);
170441312Smckusick 		return (error);
170541312Smckusick 	}
170637737Smckusick #endif
170737737Smckusick 	ip->i_flag |= IACC|IUPD|ICHG;
170837737Smckusick 	ip->i_mode = mode;
170937737Smckusick 	ITOV(ip)->v_type = IFTOVT(mode);	/* Rest init'd in iget() */
171037737Smckusick 	ip->i_nlink = 1;
171137737Smckusick 	if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, ndp->ni_cred) &&
171237737Smckusick 	    suser(ndp->ni_cred, NULL))
171337737Smckusick 		ip->i_mode &= ~ISGID;
171437737Smckusick 
171537737Smckusick 	/*
171637737Smckusick 	 * Make sure inode goes to disk before directory entry.
171737737Smckusick 	 */
171841312Smckusick 	if (error = iupdat(ip, &time, &time, 1))
171941312Smckusick 		goto bad;
172041312Smckusick 	if (error = direnter(ip, ndp)) {
172141312Smckusick 		pdir = NULL;
172241312Smckusick 		goto bad;
172337737Smckusick 	}
172437737Smckusick 	*ipp = ip;
172537737Smckusick 	return (0);
172641312Smckusick 
172741312Smckusick bad:
172841312Smckusick 	/*
172941312Smckusick 	 * Write error occurred trying to update the inode
173041312Smckusick 	 * or the directory so must deallocate the inode.
173141312Smckusick 	 */
173241312Smckusick 	if (pdir)
173341312Smckusick 		iput(pdir);
173441312Smckusick 	ip->i_nlink = 0;
173541312Smckusick 	ip->i_flag |= ICHG;
173641312Smckusick 	iput(ip);
173741312Smckusick 	return (error);
173837737Smckusick }
173946207Smckusick 
174046207Smckusick /*
174146207Smckusick  * Advisory record locking support
174246207Smckusick  */
174346207Smckusick ufs_advlock(vp, id, op, fl, flags)
174446207Smckusick 	struct vnode *vp;
174546207Smckusick 	caddr_t id;
174646207Smckusick 	int op;
174746207Smckusick 	register struct flock *fl;
174846207Smckusick 	int flags;
174946207Smckusick {
175046207Smckusick 	register struct inode *ip = VTOI(vp);
175146207Smckusick 	register struct lockf *lock;
175246207Smckusick 	off_t start, end;
175346207Smckusick 	int error;
175446207Smckusick 
175546207Smckusick 	/*
175646207Smckusick 	 * Avoid the common case of unlocking when inode has no locks.
175746207Smckusick 	 */
175846207Smckusick 	if (ip->i_lockf == (struct lockf *)0) {
175946207Smckusick 		if (op != F_SETLK) {
176046207Smckusick 			fl->l_type = F_UNLCK;
176146207Smckusick 			return (0);
176246207Smckusick 		}
176346207Smckusick 	}
176446207Smckusick 	/*
176546207Smckusick 	 * Convert the flock structure into a start and end.
176646207Smckusick 	 */
176746207Smckusick 	switch (fl->l_whence) {
176846207Smckusick 
176946207Smckusick 	case SEEK_SET:
177046207Smckusick 	case SEEK_CUR:
177146207Smckusick 		/*
177246207Smckusick 		 * Caller is responsible for adding any necessary offset
177346207Smckusick 		 * when SEEK_CUR is used.
177446207Smckusick 		 */
177546207Smckusick 		start = fl->l_start;
177646207Smckusick 		break;
177746207Smckusick 
177846207Smckusick 	case SEEK_END:
177946207Smckusick 		start = ip->i_size + fl->l_start;
178046207Smckusick 		break;
178146207Smckusick 
178246207Smckusick 	default:
178346207Smckusick 		return (EINVAL);
178446207Smckusick 	}
178546207Smckusick 	if (start < 0)
178646207Smckusick 		return (EINVAL);
178746207Smckusick 	if (fl->l_len == 0)
178846207Smckusick 		end = -1;
178946207Smckusick 	else
179046507Smckusick 		end = start + fl->l_len - 1;
179146207Smckusick 	/*
179246207Smckusick 	 * Create the lockf structure
179346207Smckusick 	 */
179446207Smckusick 	MALLOC(lock, struct lockf *, sizeof *lock, M_LOCKF, M_WAITOK);
179546207Smckusick 	lock->lf_start = start;
179646207Smckusick 	lock->lf_end = end;
179746207Smckusick 	lock->lf_id = id;
179846207Smckusick 	lock->lf_inode = ip;
179946207Smckusick 	lock->lf_type = fl->l_type;
180046207Smckusick 	lock->lf_next = (struct lockf *)0;
180146207Smckusick 	lock->lf_block = (struct lockf *)0;
180246207Smckusick 	lock->lf_flags = flags;
180346207Smckusick 	/*
180446207Smckusick 	 * Do the requested operation.
180546207Smckusick 	 */
180646207Smckusick 	switch(op) {
180746207Smckusick 	case F_SETLK:
1808*46679Smckusick 		return (lf_setlock(lock));
180946207Smckusick 
181046207Smckusick 	case F_UNLCK:
1811*46679Smckusick 		error = lf_clearlock(lock);
1812*46679Smckusick 		FREE(lock, M_LOCKF);
1813*46679Smckusick 		return (error);
181446207Smckusick 
181546207Smckusick 	case F_GETLK:
1816*46679Smckusick 		error = lf_getlock(lock, fl);
1817*46679Smckusick 		FREE(lock, M_LOCKF);
1818*46679Smckusick 		return (error);
181946207Smckusick 
182046207Smckusick 	default:
182146207Smckusick 		free(lock, M_LOCKF);
182246207Smckusick 		return (EINVAL);
182346207Smckusick 	}
182446207Smckusick 	/* NOTREACHED */
182546207Smckusick }
1826