Home | History | Annotate | Line # | Download | only in common
vfs_syscalls_43.c revision 1.2
      1 /*	$NetBSD: vfs_syscalls_43.c,v 1.2 1995/09/19 22:02:05 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  * (c) UNIX System Laboratories, Inc.
      7  * All or some portions of this file are derived from material licensed
      8  * to the University of California by American Telephone and Telegraph
      9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  * the permission of UNIX System Laboratories, Inc.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	@(#)vfs_syscalls.c	8.28 (Berkeley) 12/10/94
     41  */
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/filedesc.h>
     46 #include <sys/kernel.h>
     47 #include <sys/proc.h>
     48 #include <sys/file.h>
     49 #include <sys/vnode.h>
     50 #include <sys/namei.h>
     51 #include <sys/dirent.h>
     52 #include <sys/socket.h>
     53 #include <sys/socketvar.h>
     54 #include <sys/stat.h>
     55 #include <sys/ioctl.h>
     56 #include <sys/fcntl.h>
     57 #include <sys/malloc.h>
     58 #include <sys/syslog.h>
     59 #include <sys/unistd.h>
     60 #include <sys/resourcevar.h>
     61 
     62 #include <sys/mount.h>
     63 #include <sys/syscallargs.h>
     64 
     65 /*
     66  * Convert from an old to a new stat structure.
     67  */
     68 static int
     69 cvtstat(st, ost)
     70 	struct stat *st;
     71 	struct ostat *ost;
     72 {
     73 
     74 	ost->st_dev = st->st_dev;
     75 	ost->st_ino = st->st_ino;
     76 	ost->st_mode = st->st_mode;
     77 	ost->st_nlink = st->st_nlink;
     78 	ost->st_uid = st->st_uid;
     79 	ost->st_gid = st->st_gid;
     80 	ost->st_rdev = st->st_rdev;
     81 	if (st->st_size < (quad_t)1 << 32)
     82 		ost->st_size = st->st_size;
     83 	else
     84 		ost->st_size = -2;
     85 	ost->st_atime = st->st_atime;
     86 	ost->st_mtime = st->st_mtime;
     87 	ost->st_ctime = st->st_ctime;
     88 	ost->st_blksize = st->st_blksize;
     89 	ost->st_blocks = st->st_blocks;
     90 	ost->st_flags = st->st_flags;
     91 	ost->st_gen = st->st_gen;
     92 }
     93 
     94 /*
     95  * Get file status; this version follows links.
     96  */
     97 /* ARGSUSED */
     98 int
     99 compat_43_stat(p, v, retval)
    100 	struct proc *p;
    101 	void *v;
    102 	register_t *retval;
    103 {
    104 	register struct compat_43_stat_args /* {
    105 		syscallarg(char *) path;
    106 		syscallarg(struct ostat *) ub;
    107 	} */ *uap = v;
    108 	struct stat sb;
    109 	struct ostat osb;
    110 	int error;
    111 	struct nameidata nd;
    112 
    113 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
    114 	    SCARG(uap, path), p);
    115 	if (error = namei(&nd))
    116 		return (error);
    117 	error = vn_stat(nd.ni_vp, &sb, p);
    118 	vput(nd.ni_vp);
    119 	if (error)
    120 		return (error);
    121 	cvtstat(&sb, &osb);
    122 	error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
    123 	return (error);
    124 }
    125 
    126 
    127 /*
    128  * Get file status; this version does not follow links.
    129  */
    130 /* ARGSUSED */
    131 int
    132 compat_43_lstat(p, v, retval)
    133 	struct proc *p;
    134 	void *v;
    135 	register_t *retval;
    136 {
    137 	register struct compat_43_lstat_args /* {
    138 		syscallarg(char *) path;
    139 		syscallarg(struct ostat *) ub;
    140 	} */ *uap = v;
    141 	struct vnode *vp, *dvp;
    142 	struct stat sb, sb1;
    143 	struct ostat osb;
    144 	int error;
    145 	struct nameidata nd;
    146 
    147 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKPARENT, UIO_USERSPACE,
    148 	    SCARG(uap, path), p);
    149 	if (error = namei(&nd))
    150 		return (error);
    151 	/*
    152 	 * For symbolic links, always return the attributes of its
    153 	 * containing directory, except for mode, size, and links.
    154 	 */
    155 	vp = nd.ni_vp;
    156 	dvp = nd.ni_dvp;
    157 	if (vp->v_type != VLNK) {
    158 		if (dvp == vp)
    159 			vrele(dvp);
    160 		else
    161 			vput(dvp);
    162 		error = vn_stat(vp, &sb, p);
    163 		vput(vp);
    164 		if (error)
    165 			return (error);
    166 	} else {
    167 		error = vn_stat(dvp, &sb, p);
    168 		vput(dvp);
    169 		if (error) {
    170 			vput(vp);
    171 			return (error);
    172 		}
    173 		error = vn_stat(vp, &sb1, p);
    174 		vput(vp);
    175 		if (error)
    176 			return (error);
    177 		sb.st_mode &= ~S_IFDIR;
    178 		sb.st_mode |= S_IFLNK;
    179 		sb.st_nlink = sb1.st_nlink;
    180 		sb.st_size = sb1.st_size;
    181 		sb.st_blocks = sb1.st_blocks;
    182 	}
    183 	cvtstat(&sb, &osb);
    184 	error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
    185 	return (error);
    186 }
    187 
    188 
    189 /*
    190  * Return status information about a file descriptor.
    191  */
    192 /* ARGSUSED */
    193 compat_43_fstat(p, v, retval)
    194 	struct proc *p;
    195 	void *v;
    196 	register_t *retval;
    197 {
    198 	register struct compat_43_fstat_args /* {
    199 		syscallarg(int) fd;
    200 		syscallarg(struct ostat *) sb;
    201 	} */ *uap = v;
    202 	int fd = SCARG(uap, fd);
    203 	register struct filedesc *fdp = p->p_fd;
    204 	register struct file *fp;
    205 	struct stat ub;
    206 	struct ostat oub;
    207 	int error;
    208 
    209 	if ((u_int)fd >= fdp->fd_nfiles ||
    210 	    (fp = fdp->fd_ofiles[fd]) == NULL)
    211 		return (EBADF);
    212 	switch (fp->f_type) {
    213 
    214 	case DTYPE_VNODE:
    215 		error = vn_stat((struct vnode *)fp->f_data, &ub, p);
    216 		break;
    217 
    218 	case DTYPE_SOCKET:
    219 		error = soo_stat((struct socket *)fp->f_data, &ub);
    220 		break;
    221 
    222 	default:
    223 		panic("ofstat");
    224 		/*NOTREACHED*/
    225 	}
    226 	cvtstat(&ub, &oub);
    227 	if (error == 0)
    228 		error = copyout((caddr_t)&oub, (caddr_t)SCARG(uap, sb),
    229 		    sizeof (oub));
    230 	return (error);
    231 }
    232 
    233 
    234 /*
    235  * Truncate a file given a file descriptor.
    236  */
    237 /* ARGSUSED */
    238 int
    239 compat_43_ftruncate(p, v, retval)
    240 	struct proc *p;
    241 	void *v;
    242 	register_t *retval;
    243 {
    244 	register struct compat_43_ftruncate_args /* {
    245 		syscallarg(int) fd;
    246 		syscallarg(long) length;
    247 	} */ *uap = v;
    248 	struct ftruncate_args /* {
    249 		syscallarg(int) fd;
    250 		syscallarg(int) pad;
    251 		syscallarg(off_t) length;
    252 	} */ nuap;
    253 
    254 	SCARG(&nuap, fd) = SCARG(uap, fd);
    255 	SCARG(&nuap, length) = SCARG(uap, length);
    256 	return (ftruncate(p, &nuap, retval));
    257 }
    258 
    259 /*
    260  * Truncate a file given its path name.
    261  */
    262 /* ARGSUSED */
    263 int
    264 compat_43_truncate(p, v, retval)
    265 	struct proc *p;
    266 	void *v;
    267 	register_t *retval;
    268 {
    269 	register struct compat_43_truncate_args /* {
    270 		syscallarg(char *) path;
    271 		syscallarg(long) length;
    272 	} */ *uap = v;
    273 	struct truncate_args /* {
    274 		syscallarg(char *) path;
    275 		syscallarg(int) pad;
    276 		syscallarg(off_t) length;
    277 	} */ nuap;
    278 
    279 	SCARG(&nuap, path) = SCARG(uap, path);
    280 	SCARG(&nuap, length) = SCARG(uap, length);
    281 	return (truncate(p, &nuap, retval));
    282 }
    283 
    284 
    285 /*
    286  * Reposition read/write file offset.
    287  */
    288 int
    289 compat_43_lseek(p, v, retval)
    290 	struct proc *p;
    291 	void *v;
    292 	register_t *retval;
    293 {
    294 	register struct compat_43_lseek_args /* {
    295 		syscallarg(int) fd;
    296 		syscallarg(long) offset;
    297 		syscallarg(int) whence;
    298 	} */ *uap = v;
    299 	struct lseek_args /* {
    300 		syscallarg(int) fd;
    301 		syscallarg(int) pad;
    302 		syscallarg(off_t) offset;
    303 		syscallarg(int) whence;
    304 	} */ nuap;
    305 	off_t qret;
    306 	int error;
    307 
    308 	SCARG(&nuap, fd) = SCARG(uap, fd);
    309 	SCARG(&nuap, offset) = SCARG(uap, offset);
    310 	SCARG(&nuap, whence) = SCARG(uap, whence);
    311 	error = lseek(p, &nuap, (register_t *)&qret);
    312 	*(long *)retval = qret;
    313 	return (error);
    314 }
    315 
    316 
    317 /*
    318  * Create a file.
    319  */
    320 int
    321 compat_43_creat(p, v, retval)
    322 	struct proc *p;
    323 	void *v;
    324 	register_t *retval;
    325 {
    326 	register struct compat_43_creat_args /* {
    327 		syscallarg(char *) path;
    328 		syscallarg(int) mode;
    329 	} */ *uap = v;
    330 	struct open_args /* {
    331 		syscallarg(char *) path;
    332 		syscallarg(int) flags;
    333 		syscallarg(int) mode;
    334 	} */ nuap;
    335 
    336 	SCARG(&nuap, path) = SCARG(uap, path);
    337 	SCARG(&nuap, mode) = SCARG(uap, mode);
    338 	SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
    339 	return (open(p, &nuap, retval));
    340 }
    341 
    342 /*ARGSUSED*/
    343 int
    344 compat_43_quota(p, uap, retval)
    345 	struct proc *p;
    346 	void *uap;
    347 	register_t *retval;
    348 {
    349 
    350 	return (ENOSYS);
    351 }
    352 
    353 
    354 /*
    355  * Read a block of directory entries in a file system independent format.
    356  */
    357 int
    358 compat_43_getdirentries(p, v, retval)
    359 	struct proc *p;
    360 	void *v;
    361 	register_t *retval;
    362 {
    363 	register struct compat_43_getdirentries_args /* {
    364 		syscallarg(int) fd;
    365 		syscallarg(char *) buf;
    366 		syscallarg(u_int) count;
    367 		syscallarg(long *) basep;
    368 	} */ *uap = v;
    369 	register struct vnode *vp;
    370 	struct file *fp;
    371 	struct uio auio, kuio;
    372 	struct iovec aiov, kiov;
    373 	struct dirent *dp, *edp;
    374 	caddr_t dirbuf;
    375 	int error, eofflag, readcnt;
    376 	long loff;
    377 
    378 	if (error = getvnode(p->p_fd, SCARG(uap, fd), &fp))
    379 		return (error);
    380 	if ((fp->f_flag & FREAD) == 0)
    381 		return (EBADF);
    382 	vp = (struct vnode *)fp->f_data;
    383 unionread:
    384 	if (vp->v_type != VDIR)
    385 		return (EINVAL);
    386 	aiov.iov_base = SCARG(uap, buf);
    387 	aiov.iov_len = SCARG(uap, count);
    388 	auio.uio_iov = &aiov;
    389 	auio.uio_iovcnt = 1;
    390 	auio.uio_rw = UIO_READ;
    391 	auio.uio_segflg = UIO_USERSPACE;
    392 	auio.uio_procp = p;
    393 	auio.uio_resid = SCARG(uap, count);
    394 	VOP_LOCK(vp);
    395 	loff = auio.uio_offset = fp->f_offset;
    396 #	if (BYTE_ORDER != LITTLE_ENDIAN)
    397 		if (vp->v_mount->mnt_maxsymlinklen <= 0) {
    398 			error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
    399 			    (u_long *)0, 0);
    400 			fp->f_offset = auio.uio_offset;
    401 		} else
    402 #	endif
    403 	{
    404 		kuio = auio;
    405 		kuio.uio_iov = &kiov;
    406 		kuio.uio_segflg = UIO_SYSSPACE;
    407 		kiov.iov_len = SCARG(uap, count);
    408 		MALLOC(dirbuf, caddr_t, SCARG(uap, count), M_TEMP, M_WAITOK);
    409 		kiov.iov_base = dirbuf;
    410 		error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
    411 			    (u_long *)0, 0);
    412 		fp->f_offset = kuio.uio_offset;
    413 		if (error == 0) {
    414 			readcnt = SCARG(uap, count) - kuio.uio_resid;
    415 			edp = (struct dirent *)&dirbuf[readcnt];
    416 			for (dp = (struct dirent *)dirbuf; dp < edp; ) {
    417 #				if (BYTE_ORDER == LITTLE_ENDIAN)
    418 					/*
    419 					 * The expected low byte of
    420 					 * dp->d_namlen is our dp->d_type.
    421 					 * The high MBZ byte of dp->d_namlen
    422 					 * is our dp->d_namlen.
    423 					 */
    424 					dp->d_type = dp->d_namlen;
    425 					dp->d_namlen = 0;
    426 #				else
    427 					/*
    428 					 * The dp->d_type is the high byte
    429 					 * of the expected dp->d_namlen,
    430 					 * so must be zero'ed.
    431 					 */
    432 					dp->d_type = 0;
    433 #				endif
    434 				if (dp->d_reclen > 0) {
    435 					dp = (struct dirent *)
    436 					    ((char *)dp + dp->d_reclen);
    437 				} else {
    438 					error = EIO;
    439 					break;
    440 				}
    441 			}
    442 			if (dp >= edp)
    443 				error = uiomove(dirbuf, readcnt, &auio);
    444 		}
    445 		FREE(dirbuf, M_TEMP);
    446 	}
    447 	VOP_UNLOCK(vp);
    448 	if (error)
    449 		return (error);
    450 
    451 #ifdef UNION
    452 {
    453 	extern int (**union_vnodeop_p)();
    454 	extern struct vnode *union_dircache __P((struct vnode *));
    455 
    456 	if ((SCARG(uap, count) == auio.uio_resid) &&
    457 	    (vp->v_op == union_vnodeop_p)) {
    458 		struct vnode *lvp;
    459 
    460 		lvp = union_dircache(vp);
    461 		if (lvp != NULLVP) {
    462 			struct vattr va;
    463 
    464 			/*
    465 			 * If the directory is opaque,
    466 			 * then don't show lower entries
    467 			 */
    468 			error = VOP_GETATTR(vp, &va, fp->f_cred, p);
    469 			if (va.va_flags & OPAQUE) {
    470 				vput(lvp);
    471 				lvp = NULL;
    472 			}
    473 		}
    474 
    475 		if (lvp != NULLVP) {
    476 			error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
    477 			VOP_UNLOCK(lvp);
    478 
    479 			if (error) {
    480 				vrele(lvp);
    481 				return (error);
    482 			}
    483 			fp->f_data = (caddr_t) lvp;
    484 			fp->f_offset = 0;
    485 			error = vn_close(vp, FREAD, fp->f_cred, p);
    486 			if (error)
    487 				return (error);
    488 			vp = lvp;
    489 			goto unionread;
    490 		}
    491 	}
    492 }
    493 #endif /* UNION */
    494 
    495 	if ((SCARG(uap, count) == auio.uio_resid) &&
    496 	    (vp->v_flag & VROOT) &&
    497 	    (vp->v_mount->mnt_flag & MNT_UNION)) {
    498 		struct vnode *tvp = vp;
    499 		vp = vp->v_mount->mnt_vnodecovered;
    500 		VREF(vp);
    501 		fp->f_data = (caddr_t) vp;
    502 		fp->f_offset = 0;
    503 		vrele(tvp);
    504 		goto unionread;
    505 	}
    506 	error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
    507 	    sizeof(long));
    508 	*retval = SCARG(uap, count) - auio.uio_resid;
    509 	return (error);
    510 }
    511