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