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