Home | History | Annotate | Line # | Download | only in common
vfs_syscalls_43.c revision 1.9
      1 /*	$NetBSD: vfs_syscalls_43.c,v 1.9 1998/02/19 00:35:26 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 "fs_union.h"
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/filedesc.h>
     48 #include <sys/kernel.h>
     49 #include <sys/proc.h>
     50 #include <sys/file.h>
     51 #include <sys/vnode.h>
     52 #include <sys/namei.h>
     53 #include <sys/dirent.h>
     54 #include <sys/socket.h>
     55 #include <sys/socketvar.h>
     56 #include <sys/stat.h>
     57 #include <sys/ioctl.h>
     58 #include <sys/fcntl.h>
     59 #include <sys/malloc.h>
     60 #include <sys/syslog.h>
     61 #include <sys/unistd.h>
     62 #include <sys/resourcevar.h>
     63 
     64 #include <sys/mount.h>
     65 #include <sys/syscallargs.h>
     66 
     67 static void cvtstat __P((struct stat *, struct stat43 *));
     68 
     69 /*
     70  * Convert from an old to a new stat structure.
     71  */
     72 static void
     73 cvtstat(st, ost)
     74 	struct stat *st;
     75 	struct stat43 *ost;
     76 {
     77 
     78 	ost->st_dev = st->st_dev;
     79 	ost->st_ino = st->st_ino;
     80 	ost->st_mode = st->st_mode;
     81 	ost->st_nlink = st->st_nlink;
     82 	ost->st_uid = st->st_uid;
     83 	ost->st_gid = st->st_gid;
     84 	ost->st_rdev = st->st_rdev;
     85 	if (st->st_size < (quad_t)1 << 32)
     86 		ost->st_size = st->st_size;
     87 	else
     88 		ost->st_size = -2;
     89 	ost->st_atime = st->st_atime;
     90 	ost->st_mtime = st->st_mtime;
     91 	ost->st_ctime = st->st_ctime;
     92 	ost->st_blksize = st->st_blksize;
     93 	ost->st_blocks = st->st_blocks;
     94 	ost->st_flags = st->st_flags;
     95 	ost->st_gen = st->st_gen;
     96 }
     97 
     98 /*
     99  * Get file status; this version follows links.
    100  */
    101 /* ARGSUSED */
    102 int
    103 compat_43_sys_stat(p, v, retval)
    104 	struct proc *p;
    105 	void *v;
    106 	register_t *retval;
    107 {
    108 	register struct compat_43_sys_stat_args /* {
    109 		syscallarg(char *) path;
    110 		syscallarg(struct stat43 *) ub;
    111 	} */ *uap = v;
    112 	struct stat sb;
    113 	struct stat43 osb;
    114 	int error;
    115 	struct nameidata nd;
    116 
    117 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
    118 	    SCARG(uap, path), p);
    119 	if ((error = namei(&nd)) != 0)
    120 		return (error);
    121 	error = vn_stat(nd.ni_vp, &sb, p);
    122 	vput(nd.ni_vp);
    123 	if (error)
    124 		return (error);
    125 	cvtstat(&sb, &osb);
    126 	error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
    127 	return (error);
    128 }
    129 
    130 
    131 /*
    132  * Get file status; this version does not follow links.
    133  */
    134 /* ARGSUSED */
    135 int
    136 compat_43_sys_lstat(p, v, retval)
    137 	struct proc *p;
    138 	void *v;
    139 	register_t *retval;
    140 {
    141 	register struct compat_43_sys_lstat_args /* {
    142 		syscallarg(char *) path;
    143 		syscallarg(struct stat43 *) ub;
    144 	} */ *uap = v;
    145 	struct stat sb;
    146 	struct stat43 osb;
    147 	int error;
    148 	struct nameidata nd;
    149 
    150 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
    151 	    SCARG(uap, path), p);
    152 	if ((error = namei(&nd)) != 0)
    153 		return (error);
    154 	error = vn_stat(nd.ni_vp, &sb, p);
    155 	vput(nd.ni_vp);
    156 	if (error)
    157 		return (error);
    158 	cvtstat(&sb, &osb);
    159 	error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
    160 	return (error);
    161 }
    162 
    163 
    164 /*
    165  * Return status information about a file descriptor.
    166  */
    167 /* ARGSUSED */
    168 int
    169 compat_43_sys_fstat(p, v, retval)
    170 	struct proc *p;
    171 	void *v;
    172 	register_t *retval;
    173 {
    174 	register struct compat_43_sys_fstat_args /* {
    175 		syscallarg(int) fd;
    176 		syscallarg(struct stat43 *) sb;
    177 	} */ *uap = v;
    178 	int fd = SCARG(uap, fd);
    179 	register struct filedesc *fdp = p->p_fd;
    180 	register struct file *fp;
    181 	struct stat ub;
    182 	struct stat43 oub;
    183 	int error;
    184 
    185 	if ((u_int)fd >= fdp->fd_nfiles ||
    186 	    (fp = fdp->fd_ofiles[fd]) == NULL)
    187 		return (EBADF);
    188 	switch (fp->f_type) {
    189 
    190 	case DTYPE_VNODE:
    191 		error = vn_stat((struct vnode *)fp->f_data, &ub, p);
    192 		break;
    193 
    194 	case DTYPE_SOCKET:
    195 		error = soo_stat((struct socket *)fp->f_data, &ub);
    196 		break;
    197 
    198 	default:
    199 		panic("ofstat");
    200 		/*NOTREACHED*/
    201 	}
    202 	cvtstat(&ub, &oub);
    203 	if (error == 0)
    204 		error = copyout((caddr_t)&oub, (caddr_t)SCARG(uap, sb),
    205 		    sizeof (oub));
    206 	return (error);
    207 }
    208 
    209 
    210 /*
    211  * Truncate a file given a file descriptor.
    212  */
    213 /* ARGSUSED */
    214 int
    215 compat_43_sys_ftruncate(p, v, retval)
    216 	struct proc *p;
    217 	void *v;
    218 	register_t *retval;
    219 {
    220 	register struct compat_43_sys_ftruncate_args /* {
    221 		syscallarg(int) fd;
    222 		syscallarg(long) length;
    223 	} */ *uap = v;
    224 	struct sys_ftruncate_args /* {
    225 		syscallarg(int) fd;
    226 		syscallarg(int) pad;
    227 		syscallarg(off_t) length;
    228 	} */ nuap;
    229 
    230 	SCARG(&nuap, fd) = SCARG(uap, fd);
    231 	SCARG(&nuap, length) = SCARG(uap, length);
    232 	return (sys_ftruncate(p, &nuap, retval));
    233 }
    234 
    235 /*
    236  * Truncate a file given its path name.
    237  */
    238 /* ARGSUSED */
    239 int
    240 compat_43_sys_truncate(p, v, retval)
    241 	struct proc *p;
    242 	void *v;
    243 	register_t *retval;
    244 {
    245 	register struct compat_43_sys_truncate_args /* {
    246 		syscallarg(char *) path;
    247 		syscallarg(long) length;
    248 	} */ *uap = v;
    249 	struct sys_truncate_args /* {
    250 		syscallarg(char *) path;
    251 		syscallarg(int) pad;
    252 		syscallarg(off_t) length;
    253 	} */ nuap;
    254 
    255 	SCARG(&nuap, path) = SCARG(uap, path);
    256 	SCARG(&nuap, length) = SCARG(uap, length);
    257 	return (sys_truncate(p, &nuap, retval));
    258 }
    259 
    260 
    261 /*
    262  * Reposition read/write file offset.
    263  */
    264 int
    265 compat_43_sys_lseek(p, v, retval)
    266 	struct proc *p;
    267 	void *v;
    268 	register_t *retval;
    269 {
    270 	register struct compat_43_sys_lseek_args /* {
    271 		syscallarg(int) fd;
    272 		syscallarg(long) offset;
    273 		syscallarg(int) whence;
    274 	} */ *uap = v;
    275 	struct sys_lseek_args /* {
    276 		syscallarg(int) fd;
    277 		syscallarg(int) pad;
    278 		syscallarg(off_t) offset;
    279 		syscallarg(int) whence;
    280 	} */ nuap;
    281 	off_t qret;
    282 	int error;
    283 
    284 	SCARG(&nuap, fd) = SCARG(uap, fd);
    285 	SCARG(&nuap, offset) = SCARG(uap, offset);
    286 	SCARG(&nuap, whence) = SCARG(uap, whence);
    287 	error = sys_lseek(p, &nuap, (register_t *)&qret);
    288 	*(long *)retval = qret;
    289 	return (error);
    290 }
    291 
    292 
    293 /*
    294  * Create a file.
    295  */
    296 int
    297 compat_43_sys_creat(p, v, retval)
    298 	struct proc *p;
    299 	void *v;
    300 	register_t *retval;
    301 {
    302 	register struct compat_43_sys_creat_args /* {
    303 		syscallarg(char *) path;
    304 		syscallarg(int) mode;
    305 	} */ *uap = v;
    306 	struct sys_open_args /* {
    307 		syscallarg(char *) path;
    308 		syscallarg(int) flags;
    309 		syscallarg(int) mode;
    310 	} */ nuap;
    311 
    312 	SCARG(&nuap, path) = SCARG(uap, path);
    313 	SCARG(&nuap, mode) = SCARG(uap, mode);
    314 	SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
    315 	return (sys_open(p, &nuap, retval));
    316 }
    317 
    318 /*ARGSUSED*/
    319 int
    320 compat_43_sys_quota(p, v, retval)
    321 	struct proc *p;
    322 	void *v;
    323 	register_t *retval;
    324 {
    325 
    326 	return (ENOSYS);
    327 }
    328 
    329 
    330 /*
    331  * Read a block of directory entries in a file system independent format.
    332  */
    333 int
    334 compat_43_sys_getdirentries(p, v, retval)
    335 	struct proc *p;
    336 	void *v;
    337 	register_t *retval;
    338 {
    339 	register struct compat_43_sys_getdirentries_args /* {
    340 		syscallarg(int) fd;
    341 		syscallarg(char *) buf;
    342 		syscallarg(u_int) count;
    343 		syscallarg(long *) basep;
    344 	} */ *uap = v;
    345 	register struct vnode *vp;
    346 	struct file *fp;
    347 	struct uio auio, kuio;
    348 	struct iovec aiov, kiov;
    349 	struct dirent *dp, *edp;
    350 	caddr_t dirbuf;
    351 	int error, eofflag, readcnt;
    352 	long loff;
    353 
    354 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    355 		return (error);
    356 	if ((fp->f_flag & FREAD) == 0)
    357 		return (EBADF);
    358 	vp = (struct vnode *)fp->f_data;
    359 unionread:
    360 	if (vp->v_type != VDIR)
    361 		return (EINVAL);
    362 	aiov.iov_base = SCARG(uap, buf);
    363 	aiov.iov_len = SCARG(uap, count);
    364 	auio.uio_iov = &aiov;
    365 	auio.uio_iovcnt = 1;
    366 	auio.uio_rw = UIO_READ;
    367 	auio.uio_segflg = UIO_USERSPACE;
    368 	auio.uio_procp = p;
    369 	auio.uio_resid = SCARG(uap, count);
    370 	VOP_LOCK(vp);
    371 	loff = auio.uio_offset = fp->f_offset;
    372 #	if (BYTE_ORDER != LITTLE_ENDIAN)
    373 		if (vp->v_mount->mnt_maxsymlinklen <= 0) {
    374 			error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
    375 			    (off_t *)0, 0);
    376 			fp->f_offset = auio.uio_offset;
    377 		} else
    378 #	endif
    379 	{
    380 		kuio = auio;
    381 		kuio.uio_iov = &kiov;
    382 		kuio.uio_segflg = UIO_SYSSPACE;
    383 		kiov.iov_len = SCARG(uap, count);
    384 		MALLOC(dirbuf, caddr_t, SCARG(uap, count), M_TEMP, M_WAITOK);
    385 		kiov.iov_base = dirbuf;
    386 		error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
    387 			    (off_t *)0, 0);
    388 		fp->f_offset = kuio.uio_offset;
    389 		if (error == 0) {
    390 			readcnt = SCARG(uap, count) - kuio.uio_resid;
    391 			edp = (struct dirent *)&dirbuf[readcnt];
    392 			for (dp = (struct dirent *)dirbuf; dp < edp; ) {
    393 #				if (BYTE_ORDER == LITTLE_ENDIAN)
    394 					/*
    395 					 * The expected low byte of
    396 					 * dp->d_namlen is our dp->d_type.
    397 					 * The high MBZ byte of dp->d_namlen
    398 					 * is our dp->d_namlen.
    399 					 */
    400 					dp->d_type = dp->d_namlen;
    401 					dp->d_namlen = 0;
    402 #				else
    403 					/*
    404 					 * The dp->d_type is the high byte
    405 					 * of the expected dp->d_namlen,
    406 					 * so must be zero'ed.
    407 					 */
    408 					dp->d_type = 0;
    409 #				endif
    410 				if (dp->d_reclen > 0) {
    411 					dp = (struct dirent *)
    412 					    ((char *)dp + dp->d_reclen);
    413 				} else {
    414 					error = EIO;
    415 					break;
    416 				}
    417 			}
    418 			if (dp >= edp)
    419 				error = uiomove(dirbuf, readcnt, &auio);
    420 		}
    421 		FREE(dirbuf, M_TEMP);
    422 	}
    423 	VOP_UNLOCK(vp);
    424 	if (error)
    425 		return (error);
    426 
    427 #ifdef UNION
    428 {
    429 	extern int (**union_vnodeop_p) __P((void *));
    430 	extern struct vnode *union_dircache __P((struct vnode *));
    431 
    432 	if ((SCARG(uap, count) == auio.uio_resid) &&
    433 	    (vp->v_op == union_vnodeop_p)) {
    434 		struct vnode *lvp;
    435 
    436 		lvp = union_dircache(vp);
    437 		if (lvp != NULLVP) {
    438 			struct vattr va;
    439 
    440 			/*
    441 			 * If the directory is opaque,
    442 			 * then don't show lower entries
    443 			 */
    444 			error = VOP_GETATTR(vp, &va, fp->f_cred, p);
    445 			if (va.va_flags & OPAQUE) {
    446 				vput(lvp);
    447 				lvp = NULL;
    448 			}
    449 		}
    450 
    451 		if (lvp != NULLVP) {
    452 			error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
    453 			VOP_UNLOCK(lvp);
    454 
    455 			if (error) {
    456 				vrele(lvp);
    457 				return (error);
    458 			}
    459 			fp->f_data = (caddr_t) lvp;
    460 			fp->f_offset = 0;
    461 			error = vn_close(vp, FREAD, fp->f_cred, p);
    462 			if (error)
    463 				return (error);
    464 			vp = lvp;
    465 			goto unionread;
    466 		}
    467 	}
    468 }
    469 #endif /* UNION */
    470 
    471 	if ((SCARG(uap, count) == auio.uio_resid) &&
    472 	    (vp->v_flag & VROOT) &&
    473 	    (vp->v_mount->mnt_flag & MNT_UNION)) {
    474 		struct vnode *tvp = vp;
    475 		vp = vp->v_mount->mnt_vnodecovered;
    476 		VREF(vp);
    477 		fp->f_data = (caddr_t) vp;
    478 		fp->f_offset = 0;
    479 		vrele(tvp);
    480 		goto unionread;
    481 	}
    482 	error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
    483 	    sizeof(long));
    484 	*retval = SCARG(uap, count) - auio.uio_resid;
    485 	return (error);
    486 }
    487