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