xref: /csrg-svn/sys/ufs/lfs/lfs_vnops.c (revision 37737)
123405Smckusick /*
2*37737Smckusick  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3*37737Smckusick  * All rights reserved.
423405Smckusick  *
5*37737Smckusick  * Redistribution and use in source and binary forms are permitted
6*37737Smckusick  * provided that the above copyright notice and this paragraph are
7*37737Smckusick  * duplicated in all such forms and that any documentation,
8*37737Smckusick  * advertising materials, and other materials related to such
9*37737Smckusick  * distribution and use acknowledge that the software was developed
10*37737Smckusick  * by the University of California, Berkeley.  The name of the
11*37737Smckusick  * University may not be used to endorse or promote products derived
12*37737Smckusick  * from this software without specific prior written permission.
13*37737Smckusick  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*37737Smckusick  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*37737Smckusick  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16*37737Smckusick  *
17*37737Smckusick  *	@(#)lfs_vnops.c	7.7 (Berkeley) 05/09/89
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"
31*37737Smckusick #include "conf.h"
3217101Sbloom #include "mount.h"
33*37737Smckusick #include "vnode.h"
34*37737Smckusick #include "../ufs/inode.h"
35*37737Smckusick #include "../ufs/fs.h"
36*37737Smckusick #include "../ufs/quota.h"
3737Sbill 
389167Ssam /*
39*37737Smckusick  * Global vfs data structures for ufs
409167Ssam  */
416254Sroot 
42*37737Smckusick int	ufs_lookup(),
43*37737Smckusick 	ufs_create(),
44*37737Smckusick 	ufs_mknod(),
45*37737Smckusick 	ufs_open(),
46*37737Smckusick 	ufs_close(),
47*37737Smckusick 	ufs_access(),
48*37737Smckusick 	ufs_getattr(),
49*37737Smckusick 	ufs_setattr(),
50*37737Smckusick 	ufs_read(),
51*37737Smckusick 	ufs_write(),
52*37737Smckusick 	ufs_ioctl(),
53*37737Smckusick 	ufs_select(),
54*37737Smckusick 	ufs_mmap(),
55*37737Smckusick 	ufs_fsync(),
56*37737Smckusick 	ufs_seek(),
57*37737Smckusick 	ufs_remove(),
58*37737Smckusick 	ufs_link(),
59*37737Smckusick 	ufs_rename(),
60*37737Smckusick 	ufs_mkdir(),
61*37737Smckusick 	ufs_rmdir(),
62*37737Smckusick 	ufs_symlink(),
63*37737Smckusick 	ufs_readdir(),
64*37737Smckusick 	ufs_readlink(),
65*37737Smckusick 	ufs_abortop(),
66*37737Smckusick 	ufs_inactive(),
67*37737Smckusick 	ufs_lock(),
68*37737Smckusick 	ufs_unlock(),
69*37737Smckusick 	ufs_bmap(),
70*37737Smckusick 	ufs_strategy();
716254Sroot 
72*37737Smckusick struct vnodeops ufs_vnodeops = {
73*37737Smckusick 	ufs_lookup,
74*37737Smckusick 	ufs_create,
75*37737Smckusick 	ufs_mknod,
76*37737Smckusick 	ufs_open,
77*37737Smckusick 	ufs_close,
78*37737Smckusick 	ufs_access,
79*37737Smckusick 	ufs_getattr,
80*37737Smckusick 	ufs_setattr,
81*37737Smckusick 	ufs_read,
82*37737Smckusick 	ufs_write,
83*37737Smckusick 	ufs_ioctl,
84*37737Smckusick 	ufs_select,
85*37737Smckusick 	ufs_mmap,
86*37737Smckusick 	ufs_fsync,
87*37737Smckusick 	ufs_seek,
88*37737Smckusick 	ufs_remove,
89*37737Smckusick 	ufs_link,
90*37737Smckusick 	ufs_rename,
91*37737Smckusick 	ufs_mkdir,
92*37737Smckusick 	ufs_rmdir,
93*37737Smckusick 	ufs_symlink,
94*37737Smckusick 	ufs_readdir,
95*37737Smckusick 	ufs_readlink,
96*37737Smckusick 	ufs_abortop,
97*37737Smckusick 	ufs_inactive,
98*37737Smckusick 	ufs_lock,
99*37737Smckusick 	ufs_unlock,
100*37737Smckusick 	ufs_bmap,
101*37737Smckusick 	ufs_strategy,
102*37737Smckusick };
1036254Sroot 
104*37737Smckusick enum vtype iftovt_tab[8] = {
105*37737Smckusick 	VNON, VCHR, VDIR, VBLK, VREG, VLNK, VSOCK, VBAD,
106*37737Smckusick };
107*37737Smckusick int	vttoif_tab[8] = {
108*37737Smckusick 	0, IFREG, IFDIR, IFBLK, IFCHR, IFLNK, IFSOCK, IFMT,
109*37737Smckusick };
1106254Sroot 
1119167Ssam /*
112*37737Smckusick  * Create a regular file
1139167Ssam  */
114*37737Smckusick ufs_create(ndp, vap)
115*37737Smckusick 	struct nameidata *ndp;
116*37737Smckusick 	struct vattr *vap;
1176254Sroot {
118*37737Smckusick 	struct inode *ip;
119*37737Smckusick 	int error;
1206254Sroot 
121*37737Smckusick 	if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip))
122*37737Smckusick 		return (error);
123*37737Smckusick 	ndp->ni_vp = ITOV(ip);
124*37737Smckusick 	return (0);
1256254Sroot }
1266254Sroot 
12737Sbill /*
128*37737Smckusick  * Mknod vnode call
1296254Sroot  */
130*37737Smckusick /* ARGSUSED */
131*37737Smckusick ufs_mknod(ndp, vap, cred)
132*37737Smckusick 	struct nameidata *ndp;
133*37737Smckusick 	struct ucred *cred;
134*37737Smckusick 	struct vattr *vap;
1356254Sroot {
136*37737Smckusick 	struct inode *ip;
137*37737Smckusick 	int error;
1386254Sroot 
139*37737Smckusick 	if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip))
140*37737Smckusick 		return (error);
141*37737Smckusick 	if (vap->va_rdev) {
142*37737Smckusick 		/*
143*37737Smckusick 		 * Want to be able to use this to make badblock
144*37737Smckusick 		 * inodes, so don't truncate the dev number.
145*37737Smckusick 		 */
146*37737Smckusick 		ITOV(ip)->v_rdev = ip->i_rdev = vap->va_rdev;
147*37737Smckusick 		ip->i_flag |= IACC|IUPD|ICHG;
14812756Ssam 	}
1497701Ssam 	iput(ip);
150*37737Smckusick 	/*
151*37737Smckusick 	 * Remove inode so that it will be reloaded by iget and
152*37737Smckusick 	 * checked to see if it is an alias of an existing entry
153*37737Smckusick 	 * in the inode cache.
154*37737Smckusick 	 */
155*37737Smckusick 	remque(ip);
156*37737Smckusick 	ip->i_forw = ip;
157*37737Smckusick 	ip->i_back = ip;
158*37737Smckusick 	return (0);
1596254Sroot }
1606254Sroot 
1616254Sroot /*
162*37737Smckusick  * Open called.
163*37737Smckusick  *
164*37737Smckusick  * Nothing to do.
1656254Sroot  */
166*37737Smckusick /* ARGSUSED */
167*37737Smckusick ufs_open(vp, mode, cred)
168*37737Smckusick 	struct vnode *vp;
169*37737Smckusick 	int mode;
170*37737Smckusick 	struct ucred *cred;
1716254Sroot {
1726254Sroot 
173*37737Smckusick 	return (0);
1746254Sroot }
1756254Sroot 
1766254Sroot /*
177*37737Smckusick  * Close called
178*37737Smckusick  *
179*37737Smckusick  * Update the times on the inode.
1806254Sroot  */
181*37737Smckusick /* ARGSUSED */
182*37737Smckusick ufs_close(vp, fflag, cred)
183*37737Smckusick 	struct vnode *vp;
184*37737Smckusick 	int fflag;
185*37737Smckusick 	struct ucred *cred;
1866254Sroot {
187*37737Smckusick 	register struct inode *ip = VTOI(vp);
1886254Sroot 
189*37737Smckusick 	if (vp->v_count > 1 && !(ip->i_flag & ILOCKED))
190*37737Smckusick 		ITIMES(ip, &time, &time);
191*37737Smckusick 	return (0);
1926254Sroot }
1936254Sroot 
194*37737Smckusick ufs_access(vp, mode, cred)
195*37737Smckusick 	struct vnode *vp;
196*37737Smckusick 	int mode;
197*37737Smckusick 	struct ucred *cred;
1986254Sroot {
1996254Sroot 
200*37737Smckusick 	return (iaccess(VTOI(vp), mode, cred));
2016254Sroot }
2026254Sroot 
203*37737Smckusick /* ARGSUSED */
204*37737Smckusick ufs_getattr(vp, vap, cred)
205*37737Smckusick 	struct vnode *vp;
206*37737Smckusick 	register struct vattr *vap;
207*37737Smckusick 	struct ucred *cred;
2086254Sroot {
209*37737Smckusick 	register struct inode *ip = VTOI(vp);
2106254Sroot 
211*37737Smckusick 	ITIMES(ip, &time, &time);
2126254Sroot 	/*
213*37737Smckusick 	 * Copy from inode table
2146254Sroot 	 */
215*37737Smckusick 	vap->va_fsid = ip->i_dev;
216*37737Smckusick 	vap->va_fileid = ip->i_number;
217*37737Smckusick 	vap->va_mode = ip->i_mode & ~IFMT;
218*37737Smckusick 	vap->va_nlink = ip->i_nlink;
219*37737Smckusick 	vap->va_uid = ip->i_uid;
220*37737Smckusick 	vap->va_gid = ip->i_gid;
221*37737Smckusick 	vap->va_rdev = (dev_t)ip->i_rdev;
222*37737Smckusick 	vap->va_size = ip->i_ic.ic_size.val[0];
223*37737Smckusick 	vap->va_size1 = ip->i_ic.ic_size.val[1];
224*37737Smckusick 	vap->va_atime.tv_sec = ip->i_atime;
225*37737Smckusick 	vap->va_mtime.tv_sec = ip->i_mtime;
226*37737Smckusick 	vap->va_ctime.tv_sec = ip->i_ctime;
227*37737Smckusick 	/* this doesn't belong here */
228*37737Smckusick 	if (vp->v_type == VBLK)
229*37737Smckusick 		vap->va_blocksize = BLKDEV_IOSIZE;
230*37737Smckusick 	else if (vp->v_type == VCHR)
231*37737Smckusick 		vap->va_blocksize = MAXBSIZE;
2327142Smckusick 	else
233*37737Smckusick 		vap->va_blocksize = ip->i_fs->fs_bsize;
234*37737Smckusick 	/*
235*37737Smckusick 	 * XXX THIS IS NOT CORRECT!!, but be sure to change vn_stat()
236*37737Smckusick 	 * if you change it.
237*37737Smckusick 	 */
238*37737Smckusick 	vap->va_bytes = ip->i_blocks;
239*37737Smckusick 	vap->va_bytes1 = -1;
240*37737Smckusick 	vap->va_type = vp->v_type;
241*37737Smckusick 	return (0);
2426254Sroot }
2436254Sroot 
2446254Sroot /*
245*37737Smckusick  * Set attribute vnode op. called from several syscalls
2466254Sroot  */
247*37737Smckusick ufs_setattr(vp, vap, cred)
248*37737Smckusick 	register struct vnode *vp;
249*37737Smckusick 	register struct vattr *vap;
250*37737Smckusick 	register struct ucred *cred;
2516254Sroot {
252*37737Smckusick 	register struct inode *ip = VTOI(vp);
253*37737Smckusick 	int error = 0;
2546254Sroot 
255*37737Smckusick 	/*
256*37737Smckusick 	 * Check for unsetable attributes.
257*37737Smckusick 	 */
258*37737Smckusick 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
259*37737Smckusick 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
260*37737Smckusick 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
261*37737Smckusick 	    ((int)vap->va_bytes != VNOVAL)) {
262*37737Smckusick 		return (EINVAL);
26316540Ssam 	}
264*37737Smckusick 	/*
265*37737Smckusick 	 * Go through the fields and update iff not VNOVAL.
266*37737Smckusick 	 */
267*37737Smckusick 	if (vap->va_uid != (u_short)VNOVAL || vap->va_gid != (u_short)VNOVAL)
268*37737Smckusick 		if (error = chown1(vp, vap->va_uid, vap->va_gid, cred))
269*37737Smckusick 			return (error);
270*37737Smckusick 	if (vap->va_size != VNOVAL) {
271*37737Smckusick 		if (vp->v_type == VDIR)
272*37737Smckusick 			return (EISDIR);
273*37737Smckusick 		if (error = iaccess(ip, IWRITE, cred))
274*37737Smckusick 			return (error);
275*37737Smckusick 		if (error = itrunc(ip, vap->va_size))
276*37737Smckusick 			return (error);
27713878Ssam 	}
278*37737Smckusick 	/*
279*37737Smckusick 	 * Check whether the following attributes can be changed.
280*37737Smckusick 	 */
281*37737Smckusick 	if (cred->cr_uid != ip->i_uid &&
282*37737Smckusick 	    (error = suser(cred, &u.u_acflag)))
283*37737Smckusick 		return (error);
284*37737Smckusick 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
285*37737Smckusick 		if (vap->va_atime.tv_sec != VNOVAL)
286*37737Smckusick 			ip->i_flag |= IACC;
287*37737Smckusick 		if (vap->va_mtime.tv_sec != VNOVAL)
288*37737Smckusick 			ip->i_flag |= IUPD;
289*37737Smckusick 		ip->i_flag |= ICHG;
290*37737Smckusick 		if (error = iupdat(ip, &vap->va_atime, &vap->va_mtime, 1))
291*37737Smckusick 			return (error);
2926254Sroot 	}
293*37737Smckusick 	if (vap->va_mode != (u_short)VNOVAL)
294*37737Smckusick 		error = chmod1(vp, (int)vap->va_mode, cred);
295*37737Smckusick 	return (error);
2966254Sroot }
2976254Sroot 
2986254Sroot /*
2999167Ssam  * Change the mode on a file.
3009167Ssam  * Inode must be locked before calling.
3019167Ssam  */
302*37737Smckusick chmod1(vp, mode, cred)
303*37737Smckusick 	register struct vnode *vp;
3047701Ssam 	register int mode;
305*37737Smckusick 	struct ucred *cred;
3067701Ssam {
307*37737Smckusick 	register struct inode *ip = VTOI(vp);
3087868Sroot 
3096254Sroot 	ip->i_mode &= ~07777;
310*37737Smckusick 	if (cred->cr_uid) {
311*37737Smckusick 		if (vp->v_type != VDIR)
31221015Smckusick 			mode &= ~ISVTX;
313*37737Smckusick 		if (!groupmember(ip->i_gid, cred))
31411811Ssam 			mode &= ~ISGID;
3157439Sroot 	}
316*37737Smckusick 	ip->i_mode |= mode & 07777;
3176254Sroot 	ip->i_flag |= ICHG;
318*37737Smckusick 	if ((vp->v_flag & VTEXT) && (ip->i_mode & ISVTX) == 0)
319*37737Smckusick 		xrele(vp);
32021015Smckusick 	return (0);
3215992Swnj }
3225992Swnj 
3239167Ssam /*
3247701Ssam  * Perform chown operation on inode ip;
3257701Ssam  * inode must be locked prior to call.
3267701Ssam  */
327*37737Smckusick chown1(vp, uid, gid, cred)
328*37737Smckusick 	register struct vnode *vp;
329*37737Smckusick 	uid_t uid;
330*37737Smckusick 	gid_t gid;
331*37737Smckusick 	struct ucred *cred;
3327701Ssam {
333*37737Smckusick 	register struct inode *ip = VTOI(vp);
3347701Ssam #ifdef QUOTA
3357701Ssam 	register long change;
33611811Ssam #endif
337*37737Smckusick 	int error;
3387701Ssam 
339*37737Smckusick 	if (uid == (u_short)VNOVAL)
34011811Ssam 		uid = ip->i_uid;
341*37737Smckusick 	if (gid == (u_short)VNOVAL)
34211811Ssam 		gid = ip->i_gid;
34336614Sbostic 	/*
34436614Sbostic 	 * If we don't own the file, are trying to change the owner
34536614Sbostic 	 * of the file, or are not a member of the target group,
34636614Sbostic 	 * the caller must be superuser or the call fails.
34736614Sbostic 	 */
348*37737Smckusick 	if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
349*37737Smckusick 	    !groupmember((gid_t)gid, cred)) &&
350*37737Smckusick 	    (error = suser(cred, &u.u_acflag)))
351*37737Smckusick 		return (error);
35211811Ssam #ifdef QUOTA
35314385Ssam 	if (ip->i_uid == uid)		/* this just speeds things a little */
3547482Skre 		change = 0;
35512646Ssam 	else
35612646Ssam 		change = ip->i_blocks;
35712646Ssam 	(void) chkdq(ip, -change, 1);
35812646Ssam 	(void) chkiq(ip->i_dev, ip, ip->i_uid, 1);
3597482Skre 	dqrele(ip->i_dquot);
3607482Skre #endif
36111811Ssam 	ip->i_uid = uid;
36211811Ssam 	ip->i_gid = gid;
3636254Sroot 	ip->i_flag |= ICHG;
364*37737Smckusick 	if (cred->cr_ruid != 0)
3656254Sroot 		ip->i_mode &= ~(ISUID|ISGID);
3667701Ssam #ifdef QUOTA
3677482Skre 	ip->i_dquot = inoquota(ip);
36812646Ssam 	(void) chkdq(ip, change, 1);
36926361Skarels 	(void) chkiq(ip->i_dev, (struct inode *)NULL, (uid_t)uid, 1);
37012646Ssam 	return (u.u_error);		/* should == 0 ALWAYS !! */
37112646Ssam #else
37212646Ssam 	return (0);
3737482Skre #endif
37437Sbill }
37537Sbill 
376*37737Smckusick /* ARGSUSED */
377*37737Smckusick ufs_ioctl(vp, com, data, fflag, cred)
378*37737Smckusick 	struct vnode *vp;
379*37737Smckusick 	int com;
380*37737Smckusick 	caddr_t data;
381*37737Smckusick 	int fflag;
382*37737Smckusick 	struct ucred *cred;
38311811Ssam {
38411811Ssam 
385*37737Smckusick 	printf("ufs_ioctl called with type %d\n", vp->v_type);
386*37737Smckusick 	return (ENOTTY);
38711811Ssam }
38811811Ssam 
389*37737Smckusick /* ARGSUSED */
390*37737Smckusick ufs_select(vp, which, cred)
391*37737Smckusick 	struct vnode *vp;
392*37737Smckusick 	int which;
393*37737Smckusick 	struct ucred *cred;
394*37737Smckusick {
395*37737Smckusick 
396*37737Smckusick 	printf("ufs_select called with type %d\n", vp->v_type);
397*37737Smckusick 	return (1);		/* XXX */
398*37737Smckusick }
399*37737Smckusick 
4009167Ssam /*
401*37737Smckusick  * Mmap a file
402*37737Smckusick  *
403*37737Smckusick  * NB Currently unsupported.
4049167Ssam  */
405*37737Smckusick /* ARGSUSED */
406*37737Smckusick ufs_mmap(vp, fflags, cred)
407*37737Smckusick 	struct vnode *vp;
408*37737Smckusick 	int fflags;
409*37737Smckusick 	struct ucred *cred;
41037Sbill {
41137Sbill 
412*37737Smckusick 	return (EINVAL);
41337Sbill }
4147535Sroot 
4159167Ssam /*
416*37737Smckusick  * Synch an open file.
4179167Ssam  */
418*37737Smckusick /* ARGSUSED */
419*37737Smckusick ufs_fsync(vp, fflags, cred)
420*37737Smckusick 	struct vnode *vp;
421*37737Smckusick 	int fflags;
422*37737Smckusick 	struct ucred *cred;
4237701Ssam {
424*37737Smckusick 	register struct inode *ip = VTOI(vp);
425*37737Smckusick 	int error;
4267701Ssam 
427*37737Smckusick 	ILOCK(ip);
428*37737Smckusick 	if (fflags&FWRITE)
429*37737Smckusick 		ip->i_flag |= ICHG;
430*37737Smckusick 	error = syncip(ip);
431*37737Smckusick 	IUNLOCK(ip);
432*37737Smckusick 	return (error);
4337701Ssam }
4347701Ssam 
4359167Ssam /*
436*37737Smckusick  * Seek on a file
437*37737Smckusick  *
438*37737Smckusick  * Nothing to do, so just return.
4399167Ssam  */
440*37737Smckusick /* ARGSUSED */
441*37737Smckusick ufs_seek(vp, oldoff, newoff, cred)
442*37737Smckusick 	struct vnode *vp;
443*37737Smckusick 	off_t oldoff, newoff;
444*37737Smckusick 	struct ucred *cred;
4457701Ssam {
4467701Ssam 
447*37737Smckusick 	return (0);
448*37737Smckusick }
449*37737Smckusick 
450*37737Smckusick /*
451*37737Smckusick  * ufs remove
452*37737Smckusick  * Hard to avoid races here, especially
453*37737Smckusick  * in unlinking directories.
454*37737Smckusick  */
455*37737Smckusick ufs_remove(ndp)
456*37737Smckusick 	struct nameidata *ndp;
457*37737Smckusick {
458*37737Smckusick 	register struct inode *ip, *dp;
459*37737Smckusick 	int error;
460*37737Smckusick 
461*37737Smckusick 	ip = VTOI(ndp->ni_vp);
462*37737Smckusick 	dp = VTOI(ndp->ni_dvp);
463*37737Smckusick 	error = dirremove(ndp);
464*37737Smckusick 	if (!error) {
465*37737Smckusick 		ip->i_nlink--;
466*37737Smckusick 		ip->i_flag |= ICHG;
4677701Ssam 	}
468*37737Smckusick 	if (dp == ip)
469*37737Smckusick 		vrele(ITOV(ip));
470*37737Smckusick 	else
471*37737Smckusick 		iput(ip);
472*37737Smckusick 	iput(dp);
473*37737Smckusick 	return (error);
4747701Ssam }
4757701Ssam 
4769167Ssam /*
477*37737Smckusick  * link vnode call
4789167Ssam  */
479*37737Smckusick ufs_link(vp, ndp)
480*37737Smckusick 	register struct vnode *vp;
481*37737Smckusick 	register struct nameidata *ndp;
4829167Ssam {
483*37737Smckusick 	register struct inode *ip = VTOI(vp);
484*37737Smckusick 	int error;
4859167Ssam 
486*37737Smckusick 	if (ndp->ni_dvp != vp)
487*37737Smckusick 		ILOCK(ip);
488*37737Smckusick 	if (ip->i_nlink == LINK_MAX - 1) {
489*37737Smckusick 		error = EMLINK;
490*37737Smckusick 		goto out;
491*37737Smckusick 	}
492*37737Smckusick 	ip->i_nlink++;
493*37737Smckusick 	ip->i_flag |= ICHG;
494*37737Smckusick 	error = iupdat(ip, &time, &time, 1);
495*37737Smckusick 	if (!error)
496*37737Smckusick 		error = direnter(ip, ndp);
497*37737Smckusick out:
498*37737Smckusick 	if (ndp->ni_dvp != vp)
499*37737Smckusick 		IUNLOCK(ip);
500*37737Smckusick 	if (error) {
501*37737Smckusick 		ip->i_nlink--;
50230598Smckusick 		ip->i_flag |= ICHG;
503*37737Smckusick 	}
504*37737Smckusick 	return (error);
5059167Ssam }
5069167Ssam 
5079167Ssam /*
5089167Ssam  * Rename system call.
5099167Ssam  * 	rename("foo", "bar");
5109167Ssam  * is essentially
5119167Ssam  *	unlink("bar");
5129167Ssam  *	link("foo", "bar");
5139167Ssam  *	unlink("foo");
5149167Ssam  * but ``atomically''.  Can't do full commit without saving state in the
5159167Ssam  * inode on disk which isn't feasible at this time.  Best we can do is
5169167Ssam  * always guarantee the target exists.
5179167Ssam  *
5189167Ssam  * Basic algorithm is:
5199167Ssam  *
5209167Ssam  * 1) Bump link count on source while we're linking it to the
521*37737Smckusick  *    target.  This also ensure the inode won't be deleted out
52216776Smckusick  *    from underneath us while we work (it may be truncated by
52316776Smckusick  *    a concurrent `trunc' or `open' for creation).
5249167Ssam  * 2) Link source to destination.  If destination already exists,
5259167Ssam  *    delete it first.
52616776Smckusick  * 3) Unlink source reference to inode if still around. If a
52716776Smckusick  *    directory was moved and the parent of the destination
5289167Ssam  *    is different from the source, patch the ".." entry in the
5299167Ssam  *    directory.
5309167Ssam  */
531*37737Smckusick ufs_rename(fndp, tndp)
532*37737Smckusick 	register struct nameidata *fndp, *tndp;
5337701Ssam {
5349167Ssam 	register struct inode *ip, *xp, *dp;
53516776Smckusick 	struct dirtemplate dirbuf;
53616776Smckusick 	int doingdirectory = 0, oldparent = 0, newparent = 0;
53710051Ssam 	int error = 0;
5387701Ssam 
539*37737Smckusick 	dp = VTOI(fndp->ni_dvp);
540*37737Smckusick 	ip = VTOI(fndp->ni_vp);
541*37737Smckusick 	ILOCK(ip);
5429167Ssam 	if ((ip->i_mode&IFMT) == IFDIR) {
543*37737Smckusick 		register struct direct *d = &fndp->ni_dent;
5449167Ssam 
5459167Ssam 		/*
54611641Ssam 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
5479167Ssam 		 */
548*37737Smckusick 		if ((d->d_namlen == 1 && d->d_name[0] == '.') || dp == ip ||
549*37737Smckusick 		    fndp->ni_isdotdot || (ip->i_flag & IRENAME)) {
550*37737Smckusick 			IUNLOCK(ip);
551*37737Smckusick 			ufs_abortop(fndp);
552*37737Smckusick 			ufs_abortop(tndp);
553*37737Smckusick 			return (EINVAL);
5549167Ssam 		}
55516776Smckusick 		ip->i_flag |= IRENAME;
5569167Ssam 		oldparent = dp->i_number;
5579167Ssam 		doingdirectory++;
5589167Ssam 	}
559*37737Smckusick 	vrele(fndp->ni_dvp);
5609167Ssam 
5619167Ssam 	/*
5629167Ssam 	 * 1) Bump link count while we're moving stuff
5639167Ssam 	 *    around.  If we crash somewhere before
5649167Ssam 	 *    completing our work, the link count
5659167Ssam 	 *    may be wrong, but correctable.
5669167Ssam 	 */
5679167Ssam 	ip->i_nlink++;
5689167Ssam 	ip->i_flag |= ICHG;
569*37737Smckusick 	error = iupdat(ip, &time, &time, 1);
57016664Smckusick 	IUNLOCK(ip);
5719167Ssam 
5729167Ssam 	/*
5739167Ssam 	 * When the target exists, both the directory
574*37737Smckusick 	 * and target vnodes are returned locked.
5759167Ssam 	 */
576*37737Smckusick 	dp = VTOI(tndp->ni_dvp);
577*37737Smckusick 	xp = NULL;
578*37737Smckusick 	if (tndp->ni_vp)
579*37737Smckusick 		xp = VTOI(tndp->ni_vp);
5809167Ssam 	/*
58111641Ssam 	 * If ".." must be changed (ie the directory gets a new
58212816Smckusick 	 * parent) then the source directory must not be in the
58312816Smckusick 	 * directory heirarchy above the target, as this would
58412816Smckusick 	 * orphan everything below the source directory. Also
58512816Smckusick 	 * the user must have write permission in the source so
58612816Smckusick 	 * as to be able to change "..". We must repeat the call
58712816Smckusick 	 * to namei, as the parent directory is unlocked by the
58812816Smckusick 	 * call to checkpath().
58911641Ssam 	 */
59016776Smckusick 	if (oldparent != dp->i_number)
59116776Smckusick 		newparent = dp->i_number;
59216776Smckusick 	if (doingdirectory && newparent) {
593*37737Smckusick 		if (error = iaccess(ip, IWRITE, tndp->ni_cred))
59412816Smckusick 			goto bad;
595*37737Smckusick 		tndp->ni_nameiop = RENAME | LOCKPARENT | LOCKLEAF | NOCACHE;
59612816Smckusick 		do {
597*37737Smckusick 			dp = VTOI(tndp->ni_dvp);
59812816Smckusick 			if (xp != NULL)
599*37737Smckusick 				vput(ITOV(xp));
600*37737Smckusick 			if (error = checkpath(ip, dp, tndp->ni_cred))
60112816Smckusick 				goto out;
602*37737Smckusick 			if (error = namei(tndp))
60312816Smckusick 				goto out;
604*37737Smckusick 			xp = NULL;
605*37737Smckusick 			if (tndp->ni_vp)
606*37737Smckusick 				xp = VTOI(tndp->ni_vp);
607*37737Smckusick 		} while (dp != VTOI(tndp->ni_dvp));
60812816Smckusick 	}
60911641Ssam 	/*
6109167Ssam 	 * 2) If target doesn't exist, link the target
6119167Ssam 	 *    to the source and unlink the source.
6129167Ssam 	 *    Otherwise, rewrite the target directory
6139167Ssam 	 *    entry to reference the source inode and
6149167Ssam 	 *    expunge the original entry's existence.
6159167Ssam 	 */
6169167Ssam 	if (xp == NULL) {
617*37737Smckusick 		if (dp->i_dev != ip->i_dev)
618*37737Smckusick 			panic("rename: EXDEV");
6199167Ssam 		/*
62016776Smckusick 		 * Account for ".." in new directory.
62116776Smckusick 		 * When source and destination have the same
62216776Smckusick 		 * parent we don't fool with the link count.
6239167Ssam 		 */
62416776Smckusick 		if (doingdirectory && newparent) {
6259167Ssam 			dp->i_nlink++;
6269167Ssam 			dp->i_flag |= ICHG;
627*37737Smckusick 			error = iupdat(dp, &time, &time, 1);
6289167Ssam 		}
629*37737Smckusick 		if (error = direnter(ip, tndp))
6309167Ssam 			goto out;
6319167Ssam 	} else {
632*37737Smckusick 		if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
633*37737Smckusick 			panic("rename: EXDEV");
6349167Ssam 		/*
63510590Ssam 		 * Short circuit rename(foo, foo).
63610590Ssam 		 */
63710590Ssam 		if (xp->i_number == ip->i_number)
638*37737Smckusick 			panic("rename: same file");
63910590Ssam 		/*
64024433Sbloom 		 * If the parent directory is "sticky", then the user must
64124433Sbloom 		 * own the parent directory, or the destination of the rename,
64224433Sbloom 		 * otherwise the destination may not be changed (except by
64324433Sbloom 		 * root). This implements append-only directories.
64424433Sbloom 		 */
645*37737Smckusick 		if ((dp->i_mode & ISVTX) && tndp->ni_cred->cr_uid != 0 &&
646*37737Smckusick 		    tndp->ni_cred->cr_uid != dp->i_uid &&
647*37737Smckusick 		    xp->i_uid != tndp->ni_cred->cr_uid) {
64824433Sbloom 			error = EPERM;
64924433Sbloom 			goto bad;
65024433Sbloom 		}
65124433Sbloom 		/*
65210051Ssam 		 * Target must be empty if a directory
65310051Ssam 		 * and have no links to it.
6549167Ssam 		 * Also, insure source and target are
6559167Ssam 		 * compatible (both directories, or both
6569167Ssam 		 * not directories).
6579167Ssam 		 */
6589167Ssam 		if ((xp->i_mode&IFMT) == IFDIR) {
659*37737Smckusick 			if (!dirempty(xp, dp->i_number, tndp->ni_cred) ||
660*37737Smckusick 			    xp->i_nlink > 2) {
66110051Ssam 				error = ENOTEMPTY;
6629167Ssam 				goto bad;
6639167Ssam 			}
6649167Ssam 			if (!doingdirectory) {
66510051Ssam 				error = ENOTDIR;
6669167Ssam 				goto bad;
6679167Ssam 			}
668*37737Smckusick 			cache_purge(ITOV(dp));
6699167Ssam 		} else if (doingdirectory) {
67010051Ssam 			error = EISDIR;
6719167Ssam 			goto bad;
6729167Ssam 		}
673*37737Smckusick 		if (error = dirrewrite(dp, ip, tndp))
674*37737Smckusick 			goto bad;
675*37737Smckusick 		vput(ITOV(dp));
6769167Ssam 		/*
67710051Ssam 		 * Adjust the link count of the target to
67810051Ssam 		 * reflect the dirrewrite above.  If this is
67910051Ssam 		 * a directory it is empty and there are
68010051Ssam 		 * no links to it, so we can squash the inode and
68110051Ssam 		 * any space associated with it.  We disallowed
68210051Ssam 		 * renaming over top of a directory with links to
68316776Smckusick 		 * it above, as the remaining link would point to
68416776Smckusick 		 * a directory without "." or ".." entries.
6859167Ssam 		 */
68610051Ssam 		xp->i_nlink--;
6879167Ssam 		if (doingdirectory) {
68810051Ssam 			if (--xp->i_nlink != 0)
68910051Ssam 				panic("rename: linked directory");
690*37737Smckusick 			error = itrunc(xp, (u_long)0);
69110051Ssam 		}
6929167Ssam 		xp->i_flag |= ICHG;
693*37737Smckusick 		vput(ITOV(xp));
69410246Ssam 		xp = NULL;
6959167Ssam 	}
6969167Ssam 
6979167Ssam 	/*
6989167Ssam 	 * 3) Unlink the source.
6999167Ssam 	 */
700*37737Smckusick 	fndp->ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF;
701*37737Smckusick 	(void)namei(fndp);
702*37737Smckusick 	if (fndp->ni_vp != NULL) {
703*37737Smckusick 		xp = VTOI(fndp->ni_vp);
704*37737Smckusick 		dp = VTOI(fndp->ni_dvp);
705*37737Smckusick 	} else {
706*37737Smckusick 		xp = NULL;
70717758Smckusick 		dp = NULL;
708*37737Smckusick 	}
7099167Ssam 	/*
710*37737Smckusick 	 * Ensure that the directory entry still exists and has not
71116776Smckusick 	 * changed while the new name has been entered. If the source is
71216776Smckusick 	 * a file then the entry may have been unlinked or renamed. In
71316776Smckusick 	 * either case there is no further work to be done. If the source
71416776Smckusick 	 * is a directory then it cannot have been rmdir'ed; its link
71516776Smckusick 	 * count of three would cause a rmdir to fail with ENOTEMPTY.
716*37737Smckusick 	 * The IRENAME flag ensures that it cannot be moved by another
71716776Smckusick 	 * rename.
7189167Ssam 	 */
71917758Smckusick 	if (xp != ip) {
72016776Smckusick 		if (doingdirectory)
72117758Smckusick 			panic("rename: lost dir entry");
72216776Smckusick 	} else {
7239167Ssam 		/*
72416776Smckusick 		 * If the source is a directory with a
72516776Smckusick 		 * new parent, the link count of the old
72616776Smckusick 		 * parent directory must be decremented
72716776Smckusick 		 * and ".." set to point to the new parent.
7289167Ssam 		 */
72916776Smckusick 		if (doingdirectory && newparent) {
7309167Ssam 			dp->i_nlink--;
7319167Ssam 			dp->i_flag |= ICHG;
73216776Smckusick 			error = rdwri(UIO_READ, xp, (caddr_t)&dirbuf,
733*37737Smckusick 				sizeof (struct dirtemplate), (off_t)0,
734*37737Smckusick 				UIO_USERSPACE, tndp->ni_cred, (int *)0);
73516776Smckusick 			if (error == 0) {
73616776Smckusick 				if (dirbuf.dotdot_namlen != 2 ||
73716776Smckusick 				    dirbuf.dotdot_name[0] != '.' ||
73816776Smckusick 				    dirbuf.dotdot_name[1] != '.') {
73916776Smckusick 					printf("rename: mangled dir\n");
74016776Smckusick 				} else {
74116776Smckusick 					dirbuf.dotdot_ino = newparent;
74216776Smckusick 					(void) rdwri(UIO_WRITE, xp,
74316776Smckusick 					    (caddr_t)&dirbuf,
74416776Smckusick 					    sizeof (struct dirtemplate),
745*37737Smckusick 					    (off_t)0, UIO_USERSPACE,
746*37737Smckusick 					    tndp->ni_cred, (int *)0);
747*37737Smckusick 					cache_purge(ITOV(dp));
74816776Smckusick 				}
74916776Smckusick 			}
7509167Ssam 		}
751*37737Smckusick 		error = dirremove(fndp);
752*37737Smckusick 		if (!error) {
75316776Smckusick 			xp->i_nlink--;
75416776Smckusick 			xp->i_flag |= ICHG;
7559167Ssam 		}
75616776Smckusick 		xp->i_flag &= ~IRENAME;
7579167Ssam 	}
7589167Ssam 	if (dp)
759*37737Smckusick 		vput(ITOV(dp));
76016776Smckusick 	if (xp)
761*37737Smckusick 		vput(ITOV(xp));
762*37737Smckusick 	vrele(ITOV(ip));
763*37737Smckusick 	return (error);
7649167Ssam 
7659167Ssam bad:
7669167Ssam 	if (xp)
767*37737Smckusick 		vput(ITOV(xp));
768*37737Smckusick 	vput(ITOV(dp));
7699167Ssam out:
7709167Ssam 	ip->i_nlink--;
7719167Ssam 	ip->i_flag |= ICHG;
772*37737Smckusick 	vrele(ITOV(ip));
773*37737Smckusick 	return (error);
7747701Ssam }
7757701Ssam 
7767535Sroot /*
77712756Ssam  * A virgin directory (no blushing please).
77812756Ssam  */
77912756Ssam struct dirtemplate mastertemplate = {
78012756Ssam 	0, 12, 1, ".",
78112756Ssam 	0, DIRBLKSIZ - 12, 2, ".."
78212756Ssam };
78312756Ssam 
78412756Ssam /*
78512756Ssam  * Mkdir system call
78612756Ssam  */
787*37737Smckusick ufs_mkdir(ndp, vap)
788*37737Smckusick 	struct nameidata *ndp;
789*37737Smckusick 	struct vattr *vap;
79012756Ssam {
79112756Ssam 	register struct inode *ip, *dp;
792*37737Smckusick 	struct inode *tip;
793*37737Smckusick 	struct vnode *dvp;
79412756Ssam 	struct dirtemplate dirtemplate;
795*37737Smckusick 	int error;
796*37737Smckusick 	int dmode;
79712756Ssam 
798*37737Smckusick 	dvp = ndp->ni_dvp;
799*37737Smckusick 	dp = VTOI(dvp);
800*37737Smckusick 	dmode = vap->va_mode&0777;
801*37737Smckusick 	dmode |= IFDIR;
80212756Ssam 	/*
80312756Ssam 	 * Must simulate part of maknode here
80412756Ssam 	 * in order to acquire the inode, but
80512756Ssam 	 * not have it entered in the parent
80612756Ssam 	 * directory.  The entry is made later
80712756Ssam 	 * after writing "." and ".." entries out.
80812756Ssam 	 */
809*37737Smckusick 	error = ialloc(dp, dirpref(dp->i_fs), dmode, &tip);
810*37737Smckusick 	if (error) {
81112756Ssam 		iput(dp);
812*37737Smckusick 		return (error);
81312756Ssam 	}
814*37737Smckusick 	ip = tip;
81512756Ssam #ifdef QUOTA
81612756Ssam 	if (ip->i_dquot != NODQUOT)
81712756Ssam 		panic("mkdir: dquot");
81812756Ssam #endif
81912756Ssam 	ip->i_flag |= IACC|IUPD|ICHG;
820*37737Smckusick 	ip->i_mode = dmode;
821*37737Smckusick 	ITOV(ip)->v_type = VDIR;	/* Rest init'd in iget() */
82212756Ssam 	ip->i_nlink = 2;
823*37737Smckusick 	ip->i_uid = ndp->ni_cred->cr_uid;
82412756Ssam 	ip->i_gid = dp->i_gid;
82512756Ssam #ifdef QUOTA
82612756Ssam 	ip->i_dquot = inoquota(ip);
82712756Ssam #endif
828*37737Smckusick 	error = iupdat(ip, &time, &time, 1);
82912756Ssam 
83012756Ssam 	/*
83112756Ssam 	 * Bump link count in parent directory
83212756Ssam 	 * to reflect work done below.  Should
83312756Ssam 	 * be done before reference is created
83412756Ssam 	 * so reparation is possible if we crash.
83512756Ssam 	 */
83612756Ssam 	dp->i_nlink++;
83712756Ssam 	dp->i_flag |= ICHG;
838*37737Smckusick 	error = iupdat(dp, &time, &time, 1);
83912756Ssam 
84012756Ssam 	/*
84112756Ssam 	 * Initialize directory with "."
84212756Ssam 	 * and ".." from static template.
84312756Ssam 	 */
84412756Ssam 	dirtemplate = mastertemplate;
84512756Ssam 	dirtemplate.dot_ino = ip->i_number;
84612756Ssam 	dirtemplate.dotdot_ino = dp->i_number;
847*37737Smckusick 	error = rdwri(UIO_WRITE, ip, (caddr_t)&dirtemplate,
848*37737Smckusick 		sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
849*37737Smckusick 		ndp->ni_cred, (int *)0);
850*37737Smckusick 	if (error) {
85112756Ssam 		dp->i_nlink--;
85212756Ssam 		dp->i_flag |= ICHG;
85312756Ssam 		goto bad;
85412756Ssam 	}
855*37737Smckusick 	if (DIRBLKSIZ > dp->i_fs->fs_fsize)
856*37737Smckusick 		panic("mkdir: blksize");     /* XXX - should grow w/balloc() */
85718103Smckusick 	else
85818103Smckusick 		ip->i_size = DIRBLKSIZ;
85912756Ssam 	/*
86012756Ssam 	 * Directory all set up, now
86112756Ssam 	 * install the entry for it in
86212756Ssam 	 * the parent directory.
86312756Ssam 	 */
864*37737Smckusick 	error = direnter(ip, ndp);
86512756Ssam 	dp = NULL;
866*37737Smckusick 	if (error) {
86716694Smckusick 		ndp->ni_nameiop = LOOKUP | NOCACHE;
868*37737Smckusick 		error = namei(ndp);
869*37737Smckusick 		if (!error) {
870*37737Smckusick 			dp = VTOI(ndp->ni_vp);
87112756Ssam 			dp->i_nlink--;
87212756Ssam 			dp->i_flag |= ICHG;
87312756Ssam 		}
87412756Ssam 	}
87512756Ssam bad:
87612756Ssam 	/*
87712756Ssam 	 * No need to do an explicit itrunc here,
878*37737Smckusick 	 * vrele will do this for us because we set
87912756Ssam 	 * the link count to 0.
88012756Ssam 	 */
881*37737Smckusick 	if (error) {
88212756Ssam 		ip->i_nlink = 0;
88312756Ssam 		ip->i_flag |= ICHG;
88412756Ssam 	}
885*37737Smckusick 	iput(ip);
88612756Ssam 	if (dp)
88712756Ssam 		iput(dp);
888*37737Smckusick 	return (error);
88912756Ssam }
89012756Ssam 
89112756Ssam /*
89212756Ssam  * Rmdir system call.
89312756Ssam  */
894*37737Smckusick ufs_rmdir(ndp)
895*37737Smckusick 	register struct nameidata *ndp;
89612756Ssam {
89712756Ssam 	register struct inode *ip, *dp;
898*37737Smckusick 	int error = 0;
89912756Ssam 
900*37737Smckusick 	ip = VTOI(ndp->ni_vp);
901*37737Smckusick 	dp = VTOI(ndp->ni_dvp);
90212756Ssam 	/*
90312756Ssam 	 * No rmdir "." please.
90412756Ssam 	 */
90512756Ssam 	if (dp == ip) {
906*37737Smckusick 		vrele(ITOV(dp));
90712756Ssam 		iput(ip);
908*37737Smckusick 		return (EINVAL);
90912756Ssam 	}
91012756Ssam 	/*
91112756Ssam 	 * Verify the directory is empty (and valid).
91212756Ssam 	 * (Rmdir ".." won't be valid since
91312756Ssam 	 *  ".." will contain a reference to
91412756Ssam 	 *  the current directory and thus be
91512756Ssam 	 *  non-empty.)
91612756Ssam 	 */
917*37737Smckusick 	if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number, ndp->ni_cred)) {
918*37737Smckusick 		error = ENOTEMPTY;
91912756Ssam 		goto out;
92012756Ssam 	}
92112756Ssam 	/*
92212756Ssam 	 * Delete reference to directory before purging
92312756Ssam 	 * inode.  If we crash in between, the directory
92412756Ssam 	 * will be reattached to lost+found,
92512756Ssam 	 */
926*37737Smckusick 	if (error = dirremove(ndp))
92712756Ssam 		goto out;
92812756Ssam 	dp->i_nlink--;
92912756Ssam 	dp->i_flag |= ICHG;
930*37737Smckusick 	cache_purge(ITOV(dp));
93112756Ssam 	iput(dp);
932*37737Smckusick 	ndp->ni_dvp = NULL;
93312756Ssam 	/*
93412756Ssam 	 * Truncate inode.  The only stuff left
93512756Ssam 	 * in the directory is "." and "..".  The
93612756Ssam 	 * "." reference is inconsequential since
93712756Ssam 	 * we're quashing it.  The ".." reference
93812756Ssam 	 * has already been adjusted above.  We've
93912756Ssam 	 * removed the "." reference and the reference
94012756Ssam 	 * in the parent directory, but there may be
94112756Ssam 	 * other hard links so decrement by 2 and
94212756Ssam 	 * worry about them later.
94312756Ssam 	 */
94412756Ssam 	ip->i_nlink -= 2;
945*37737Smckusick 	error = itrunc(ip, (u_long)0);
946*37737Smckusick 	cache_purge(ITOV(ip));
94712756Ssam out:
948*37737Smckusick 	if (ndp->ni_dvp)
94912756Ssam 		iput(dp);
95012756Ssam 	iput(ip);
951*37737Smckusick 	return (error);
95212756Ssam }
95312756Ssam 
954*37737Smckusick /*
955*37737Smckusick  * symlink -- make a symbolic link
956*37737Smckusick  */
957*37737Smckusick ufs_symlink(ndp, vap, target)
958*37737Smckusick 	struct nameidata *ndp;
959*37737Smckusick 	struct vattr *vap;
960*37737Smckusick 	char *target;
96112756Ssam {
962*37737Smckusick 	struct inode *ip;
963*37737Smckusick 	int error;
96412756Ssam 
965*37737Smckusick 	error = maknode(IFLNK | vap->va_mode, ndp, &ip);
966*37737Smckusick 	if (error)
967*37737Smckusick 		return (error);
968*37737Smckusick 	error = rdwri(UIO_WRITE, ip, target, strlen(target), (off_t)0,
969*37737Smckusick 		UIO_SYSSPACE, ndp->ni_cred, (int *)0);
970*37737Smckusick 	iput(ip);
971*37737Smckusick 	return (error);
972*37737Smckusick }
973*37737Smckusick 
974*37737Smckusick /*
975*37737Smckusick  * Vnode op for read and write
976*37737Smckusick  */
977*37737Smckusick ufs_readdir(vp, uio, offp, cred)
978*37737Smckusick 	struct vnode *vp;
979*37737Smckusick 	register struct uio *uio;
980*37737Smckusick 	off_t *offp;
981*37737Smckusick 	struct ucred *cred;
982*37737Smckusick {
983*37737Smckusick 	register struct inode *ip = VTOI(vp);
984*37737Smckusick 	int count, error;
985*37737Smckusick 
986*37737Smckusick 	ILOCK(ip);
987*37737Smckusick 	uio->uio_offset = *offp;
988*37737Smckusick 	count = uio->uio_resid;
989*37737Smckusick 	count &= ~(DIRBLKSIZ - 1);
990*37737Smckusick 	if (vp->v_type != VDIR || uio->uio_iovcnt != 1 ||
991*37737Smckusick 	    (count < DIRBLKSIZ) || (uio->uio_offset & (DIRBLKSIZ -1))) {
992*37737Smckusick 		IUNLOCK(ip);
993*37737Smckusick 		return (EINVAL);
99416540Ssam 	}
995*37737Smckusick 	uio->uio_resid = count;
996*37737Smckusick 	uio->uio_iov->iov_len = count;
997*37737Smckusick 	error = readip(ip, uio, cred);
998*37737Smckusick 	*offp += count - uio->uio_resid;
999*37737Smckusick 	IUNLOCK(ip);
1000*37737Smckusick 	return (error);
1001*37737Smckusick }
1002*37737Smckusick 
1003*37737Smckusick /*
1004*37737Smckusick  * Return target name of a symbolic link
1005*37737Smckusick  */
1006*37737Smckusick ufs_readlink(vp, uiop, cred)
1007*37737Smckusick 	struct vnode *vp;
1008*37737Smckusick 	struct uio *uiop;
1009*37737Smckusick 	struct ucred *cred;
1010*37737Smckusick {
1011*37737Smckusick 
1012*37737Smckusick 	return (readip(VTOI(vp), uiop, cred));
1013*37737Smckusick }
1014*37737Smckusick 
1015*37737Smckusick /*
1016*37737Smckusick  * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually
1017*37737Smckusick  * done. Iff ni_vp/ni_dvp not null and locked, unlock.
1018*37737Smckusick  */
1019*37737Smckusick ufs_abortop(ndp)
1020*37737Smckusick 	register struct nameidata *ndp;
1021*37737Smckusick {
1022*37737Smckusick 	register struct inode *ip;
1023*37737Smckusick 
1024*37737Smckusick 	if (ndp->ni_vp) {
1025*37737Smckusick 		ip = VTOI(ndp->ni_vp);
1026*37737Smckusick 		if (ip->i_flag & ILOCKED)
1027*37737Smckusick 			IUNLOCK(ip);
1028*37737Smckusick 		vrele(ndp->ni_vp);
102912756Ssam 	}
1030*37737Smckusick 	if (ndp->ni_dvp) {
1031*37737Smckusick 		ip = VTOI(ndp->ni_dvp);
1032*37737Smckusick 		if (ip->i_flag & ILOCKED)
1033*37737Smckusick 			IUNLOCK(ip);
1034*37737Smckusick 		vrele(ndp->ni_dvp);
1035*37737Smckusick 	}
1036*37737Smckusick 	return;
103712756Ssam }
103812756Ssam 
1039*37737Smckusick ufs_lock(vp)
1040*37737Smckusick 	struct vnode *vp;
1041*37737Smckusick {
1042*37737Smckusick 	register struct inode *ip = VTOI(vp);
1043*37737Smckusick 
1044*37737Smckusick 	ILOCK(ip);
1045*37737Smckusick 	return (0);
1046*37737Smckusick }
1047*37737Smckusick 
1048*37737Smckusick ufs_unlock(vp)
1049*37737Smckusick 	struct vnode *vp;
1050*37737Smckusick {
1051*37737Smckusick 	register struct inode *ip = VTOI(vp);
1052*37737Smckusick 
1053*37737Smckusick 	if (!(ip->i_flag & ILOCKED))
1054*37737Smckusick 		panic("ufs_unlock NOT LOCKED");
1055*37737Smckusick 	IUNLOCK(ip);
1056*37737Smckusick 	return (0);
1057*37737Smckusick }
1058*37737Smckusick 
105912756Ssam /*
1060*37737Smckusick  * Get access to bmap
106112756Ssam  */
1062*37737Smckusick ufs_bmap(vp, bn, vpp, bnp)
1063*37737Smckusick 	struct vnode *vp;
1064*37737Smckusick 	daddr_t bn;
1065*37737Smckusick 	struct vnode **vpp;
1066*37737Smckusick 	daddr_t *bnp;
106712756Ssam {
1068*37737Smckusick 	struct inode *ip = VTOI(vp);
106912756Ssam 
1070*37737Smckusick 	if (vpp != NULL)
1071*37737Smckusick 		*vpp = ip->i_devvp;
1072*37737Smckusick 	if (bnp == NULL)
1073*37737Smckusick 		return (0);
1074*37737Smckusick 	return (bmap(ip, bn, bnp, (daddr_t *)0, (int *)0));
107512756Ssam }
1076*37737Smckusick 
1077*37737Smckusick /*
1078*37737Smckusick  * Just call the device strategy routine
1079*37737Smckusick  */
1080*37737Smckusick ufs_strategy(bp)
1081*37737Smckusick 	register struct buf *bp;
1082*37737Smckusick {
1083*37737Smckusick 	(*bdevsw[major(bp->b_dev)].d_strategy)(bp);
1084*37737Smckusick 	return (0);
1085*37737Smckusick }
1086*37737Smckusick 
1087*37737Smckusick /*
1088*37737Smckusick  * Make a new file.
1089*37737Smckusick  */
1090*37737Smckusick maknode(mode, ndp, ipp)
1091*37737Smckusick 	int mode;
1092*37737Smckusick 	register struct nameidata *ndp;
1093*37737Smckusick 	struct inode **ipp;
1094*37737Smckusick {
1095*37737Smckusick 	register struct inode *ip;
1096*37737Smckusick 	struct inode *tip;
1097*37737Smckusick 	register struct inode *pdir = VTOI(ndp->ni_dvp);
1098*37737Smckusick 	ino_t ipref;
1099*37737Smckusick 	int error;
1100*37737Smckusick 
1101*37737Smckusick 	*ipp = 0;
1102*37737Smckusick 	if ((mode & IFMT) == IFDIR)
1103*37737Smckusick 		ipref = dirpref(pdir->i_fs);
1104*37737Smckusick 	else
1105*37737Smckusick 		ipref = pdir->i_number;
1106*37737Smckusick 	error = ialloc(pdir, ipref, mode, &tip);
1107*37737Smckusick 	if (error) {
1108*37737Smckusick 		iput(pdir);
1109*37737Smckusick 		return (error);
1110*37737Smckusick 	}
1111*37737Smckusick 	ip = tip;
1112*37737Smckusick #ifdef QUOTA
1113*37737Smckusick 	if (ip->i_dquot != NODQUOT)
1114*37737Smckusick 		panic("maknode: dquot");
1115*37737Smckusick #endif
1116*37737Smckusick 	ip->i_flag |= IACC|IUPD|ICHG;
1117*37737Smckusick 	if ((mode & IFMT) == 0)
1118*37737Smckusick 		mode |= IFREG;
1119*37737Smckusick 	ip->i_mode = mode;
1120*37737Smckusick 	ITOV(ip)->v_type = IFTOVT(mode);	/* Rest init'd in iget() */
1121*37737Smckusick 	ip->i_nlink = 1;
1122*37737Smckusick 	ip->i_uid = ndp->ni_cred->cr_uid;
1123*37737Smckusick 	ip->i_gid = pdir->i_gid;
1124*37737Smckusick 	if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, ndp->ni_cred) &&
1125*37737Smckusick 	    suser(ndp->ni_cred, NULL))
1126*37737Smckusick 		ip->i_mode &= ~ISGID;
1127*37737Smckusick #ifdef QUOTA
1128*37737Smckusick 	ip->i_dquot = inoquota(ip);
1129*37737Smckusick #endif
1130*37737Smckusick 
1131*37737Smckusick 	/*
1132*37737Smckusick 	 * Make sure inode goes to disk before directory entry.
1133*37737Smckusick 	 */
1134*37737Smckusick 	if ((error = iupdat(ip, &time, &time, 1)) ||
1135*37737Smckusick 	    (error = direnter(ip, ndp))) {
1136*37737Smckusick 		/*
1137*37737Smckusick 		 * Write error occurred trying to update the inode
1138*37737Smckusick 		 * or the directory so must deallocate the inode.
1139*37737Smckusick 		 */
1140*37737Smckusick 		ip->i_nlink = 0;
1141*37737Smckusick 		ip->i_flag |= ICHG;
1142*37737Smckusick 		iput(ip);
1143*37737Smckusick 		return (error);
1144*37737Smckusick 	}
1145*37737Smckusick 	*ipp = ip;
1146*37737Smckusick 	return (0);
1147*37737Smckusick }
1148