Home | History | Annotate | Line # | Download | only in common
vfs_syscalls_43.c revision 1.48
      1 /*	$NetBSD: vfs_syscalls_43.c,v 1.48 2008/11/19 18:36:02 ad 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.48 2008/11/19 18:36:02 ad 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/malloc.h>
     59 #include <sys/ioctl.h>
     60 #include <sys/fcntl.h>
     61 #include <sys/syslog.h>
     62 #include <sys/unistd.h>
     63 #include <sys/resourcevar.h>
     64 #include <sys/sysctl.h>
     65 
     66 #include <sys/mount.h>
     67 #include <sys/syscallargs.h>
     68 #include <sys/vfs_syscalls.h>
     69 
     70 #include <compat/sys/stat.h>
     71 #include <compat/sys/mount.h>
     72 
     73 #include <compat/common/compat_util.h>
     74 
     75 static void cvtstat(struct stat *, struct stat43 *);
     76 
     77 /*
     78  * Convert from an old to a new stat structure.
     79  */
     80 static void
     81 cvtstat(struct stat *st, 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, const struct compat_43_sys_stat_args *uap, register_t *retval)
    110 {
    111 	/* {
    112 		syscallarg(char *) path;
    113 		syscallarg(struct stat43 *) ub;
    114 	} */
    115 	struct stat sb;
    116 	struct stat43 osb;
    117 	int error;
    118 
    119 	error = do_sys_stat(SCARG(uap, path), FOLLOW, &sb);
    120 	if (error)
    121 		return (error);
    122 	cvtstat(&sb, &osb);
    123 	error = copyout((void *)&osb, (void *)SCARG(uap, ub), sizeof (osb));
    124 	return (error);
    125 }
    126 
    127 /*
    128  * Get file status; this version does not follow links.
    129  */
    130 /* ARGSUSED */
    131 int
    132 compat_43_sys_lstat(struct lwp *l, const struct compat_43_sys_lstat_args *uap, register_t *retval)
    133 {
    134 	/* {
    135 		syscallarg(char *) path;
    136 		syscallarg(struct ostat *) ub;
    137 	} */
    138 	struct vnode *vp, *dvp;
    139 	struct stat sb, sb1;
    140 	struct stat43 osb;
    141 	int error;
    142 	struct nameidata nd;
    143 	int ndflags;
    144 
    145 	ndflags = NOFOLLOW | LOCKLEAF | LOCKPARENT | TRYEMULROOT;
    146 again:
    147 	NDINIT(&nd, LOOKUP, ndflags, UIO_USERSPACE, SCARG(uap, path));
    148 	if ((error = namei(&nd))) {
    149 		if (error == EISDIR && (ndflags & LOCKPARENT) != 0) {
    150 			/*
    151 			 * Should only happen on '/'. Retry without LOCKPARENT;
    152 			 * this is safe since the vnode won't be a VLNK.
    153 			 */
    154 			ndflags &= ~LOCKPARENT;
    155 			goto again;
    156 		}
    157 		return (error);
    158 	}
    159 	/*
    160 	 * For symbolic links, always return the attributes of its
    161 	 * containing directory, except for mode, size, and links.
    162 	 */
    163 	vp = nd.ni_vp;
    164 	dvp = nd.ni_dvp;
    165 	if (vp->v_type != VLNK) {
    166 		if ((ndflags & LOCKPARENT) != 0) {
    167 			if (dvp == vp)
    168 				vrele(dvp);
    169 			else
    170 				vput(dvp);
    171 		}
    172 		error = vn_stat(vp, &sb);
    173 		vput(vp);
    174 		if (error)
    175 			return (error);
    176 	} else {
    177 		error = vn_stat(dvp, &sb);
    178 		vput(dvp);
    179 		if (error) {
    180 			vput(vp);
    181 			return (error);
    182 		}
    183 		error = vn_stat(vp, &sb1);
    184 		vput(vp);
    185 		if (error)
    186 			return (error);
    187 		sb.st_mode &= ~S_IFDIR;
    188 		sb.st_mode |= S_IFLNK;
    189 		sb.st_nlink = sb1.st_nlink;
    190 		sb.st_size = sb1.st_size;
    191 		sb.st_blocks = sb1.st_blocks;
    192 	}
    193 	cvtstat(&sb, &osb);
    194 	error = copyout((void *)&osb, (void *)SCARG(uap, ub), sizeof (osb));
    195 	return (error);
    196 }
    197 
    198 /*
    199  * Return status information about a file descriptor.
    200  */
    201 /* ARGSUSED */
    202 int
    203 compat_43_sys_fstat(struct lwp *l, const struct compat_43_sys_fstat_args *uap, register_t *retval)
    204 {
    205 	/* {
    206 		syscallarg(int) fd;
    207 		syscallarg(struct stat43 *) sb;
    208 	} */
    209 	int fd = SCARG(uap, fd);
    210 	struct file *fp;
    211 	struct stat ub;
    212 	struct stat43 oub;
    213 	int error;
    214 
    215 	if ((fp = fd_getfile(fd)) == NULL)
    216 		return (EBADF);
    217 	error = (*fp->f_ops->fo_stat)(fp, &ub);
    218 	fd_putfile(fd);
    219 	if (error == 0) {
    220 		cvtstat(&ub, &oub);
    221 		error = copyout((void *)&oub, (void *)SCARG(uap, sb),
    222 		    sizeof (oub));
    223 	}
    224 
    225 
    226 	return (error);
    227 }
    228 
    229 
    230 /*
    231  * Truncate a file given a file descriptor.
    232  */
    233 /* ARGSUSED */
    234 int
    235 compat_43_sys_ftruncate(struct lwp *l, const struct compat_43_sys_ftruncate_args *uap, register_t *retval)
    236 {
    237 	/* {
    238 		syscallarg(int) fd;
    239 		syscallarg(long) length;
    240 	} */
    241 	struct sys_ftruncate_args /* {
    242 		syscallarg(int) fd;
    243 		syscallarg(int) pad;
    244 		syscallarg(off_t) length;
    245 	} */ nuap;
    246 
    247 	SCARG(&nuap, fd) = SCARG(uap, fd);
    248 	SCARG(&nuap, length) = SCARG(uap, length);
    249 	return (sys_ftruncate(l, &nuap, retval));
    250 }
    251 
    252 /*
    253  * Truncate a file given its path name.
    254  */
    255 /* ARGSUSED */
    256 int
    257 compat_43_sys_truncate(struct lwp *l, const struct compat_43_sys_truncate_args *uap, register_t *retval)
    258 {
    259 	/* {
    260 		syscallarg(char *) path;
    261 		syscallarg(long) length;
    262 	} */
    263 	struct sys_truncate_args /* {
    264 		syscallarg(char *) path;
    265 		syscallarg(int) pad;
    266 		syscallarg(off_t) length;
    267 	} */ nuap;
    268 
    269 	SCARG(&nuap, path) = SCARG(uap, path);
    270 	SCARG(&nuap, length) = SCARG(uap, length);
    271 	return (sys_truncate(l, &nuap, retval));
    272 }
    273 
    274 
    275 /*
    276  * Reposition read/write file offset.
    277  */
    278 int
    279 compat_43_sys_lseek(struct lwp *l, const struct compat_43_sys_lseek_args *uap, register_t *retval)
    280 {
    281 	/* {
    282 		syscallarg(int) fd;
    283 		syscallarg(long) offset;
    284 		syscallarg(int) whence;
    285 	} */
    286 	struct sys_lseek_args /* {
    287 		syscallarg(int) fd;
    288 		syscallarg(int) pad;
    289 		syscallarg(off_t) offset;
    290 		syscallarg(int) whence;
    291 	} */ nuap;
    292 	off_t qret;
    293 	int error;
    294 
    295 	SCARG(&nuap, fd) = SCARG(uap, fd);
    296 	SCARG(&nuap, offset) = SCARG(uap, offset);
    297 	SCARG(&nuap, whence) = SCARG(uap, whence);
    298 	error = sys_lseek(l, &nuap, (void *)&qret);
    299 	*(long *)retval = qret;
    300 	return (error);
    301 }
    302 
    303 
    304 /*
    305  * Create a file.
    306  */
    307 int
    308 compat_43_sys_creat(struct lwp *l, const struct compat_43_sys_creat_args *uap, register_t *retval)
    309 {
    310 	/* {
    311 		syscallarg(char *) path;
    312 		syscallarg(int) mode;
    313 	} */
    314 	struct sys_open_args /* {
    315 		syscallarg(char *) path;
    316 		syscallarg(int) flags;
    317 		syscallarg(int) mode;
    318 	} */ nuap;
    319 
    320 	SCARG(&nuap, path) = SCARG(uap, path);
    321 	SCARG(&nuap, mode) = SCARG(uap, mode);
    322 	SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
    323 	return (sys_open(l, &nuap, retval));
    324 }
    325 
    326 /*ARGSUSED*/
    327 int
    328 compat_43_sys_quota(struct lwp *l, const void *v, register_t *retval)
    329 {
    330 
    331 	return (ENOSYS);
    332 }
    333 
    334 
    335 /*
    336  * Read a block of directory entries in a file system independent format.
    337  */
    338 int
    339 compat_43_sys_getdirentries(struct lwp *l, const struct compat_43_sys_getdirentries_args *uap, register_t *retval)
    340 {
    341 	/* {
    342 		syscallarg(int) fd;
    343 		syscallarg(char *) buf;
    344 		syscallarg(u_int) count;
    345 		syscallarg(long *) basep;
    346 	} */
    347 	struct vnode *vp;
    348 	struct file *fp;
    349 	struct uio auio, kuio;
    350 	struct iovec aiov, kiov;
    351 	struct dirent *dp, *edp;
    352 	char *dirbuf;
    353 	size_t count = min(MAXBSIZE, (size_t)SCARG(uap, count));
    354 
    355 	int error, eofflag, readcnt;
    356 	long loff;
    357 
    358 	/* fd_getvnode() will use the descriptor for us */
    359 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
    360 		return (error);
    361 	if ((fp->f_flag & FREAD) == 0) {
    362 		error = EBADF;
    363 		goto out;
    364 	}
    365 	vp = (struct vnode *)fp->f_data;
    366 unionread:
    367 	if (vp->v_type != VDIR) {
    368 		error = EINVAL;
    369 		goto out;
    370 	}
    371 	aiov.iov_base = SCARG(uap, buf);
    372 	aiov.iov_len = count;
    373 	auio.uio_iov = &aiov;
    374 	auio.uio_iovcnt = 1;
    375 	auio.uio_rw = UIO_READ;
    376 	auio.uio_resid = count;
    377 	KASSERT(l == curlwp);
    378 	auio.uio_vmspace = curproc->p_vmspace;
    379 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    380 	loff = auio.uio_offset = fp->f_offset;
    381 #	if (BYTE_ORDER != LITTLE_ENDIAN)
    382 		if ((vp->v_mount->mnt_iflag & IMNT_DTYPE) == 0) {
    383 			error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
    384 			    (off_t **)0, (int *)0);
    385 			fp->f_offset = auio.uio_offset;
    386 		} else
    387 #	endif
    388 	{
    389 		kuio = auio;
    390 		kuio.uio_iov = &kiov;
    391 		kiov.iov_len = count;
    392 		dirbuf = malloc(count, M_TEMP, M_WAITOK);
    393 		kiov.iov_base = dirbuf;
    394 		UIO_SETUP_SYSSPACE(&kuio);
    395 		error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
    396 			    (off_t **)0, (int *)0);
    397 		fp->f_offset = kuio.uio_offset;
    398 		if (error == 0) {
    399 			readcnt = count - kuio.uio_resid;
    400 			edp = (struct dirent *)&dirbuf[readcnt];
    401 			for (dp = (struct dirent *)dirbuf; dp < edp; ) {
    402 #				if (BYTE_ORDER == LITTLE_ENDIAN)
    403 					/*
    404 					 * The expected low byte of
    405 					 * dp->d_namlen is our dp->d_type.
    406 					 * The high MBZ byte of dp->d_namlen
    407 					 * is our dp->d_namlen.
    408 					 */
    409 					dp->d_type = dp->d_namlen;
    410 					dp->d_namlen = 0;
    411 #				else
    412 					/*
    413 					 * The dp->d_type is the high byte
    414 					 * of the expected dp->d_namlen,
    415 					 * so must be zero'ed.
    416 					 */
    417 					dp->d_type = 0;
    418 #				endif
    419 				if (dp->d_reclen > 0) {
    420 					dp = (struct dirent *)
    421 					    ((char *)dp + dp->d_reclen);
    422 				} else {
    423 					error = EIO;
    424 					break;
    425 				}
    426 			}
    427 			if (dp >= edp)
    428 				error = uiomove(dirbuf, readcnt, &auio);
    429 		}
    430 		free(dirbuf, M_TEMP);
    431 	}
    432 	VOP_UNLOCK(vp, 0);
    433 	if (error)
    434 		goto out;
    435 
    436 	if ((count == auio.uio_resid) &&
    437 	    (vp->v_vflag & VV_ROOT) &&
    438 	    (vp->v_mount->mnt_flag & MNT_UNION)) {
    439 		struct vnode *tvp = vp;
    440 		vp = vp->v_mount->mnt_vnodecovered;
    441 		VREF(vp);
    442 		fp->f_data = (void *) vp;
    443 		fp->f_offset = 0;
    444 		vrele(tvp);
    445 		goto unionread;
    446 	}
    447 	error = copyout((void *)&loff, (void *)SCARG(uap, basep),
    448 	    sizeof(long));
    449 	*retval = count - auio.uio_resid;
    450  out:
    451 	fd_putfile(SCARG(uap, fd));
    452 	return (error);
    453 }
    454 
    455 /*
    456  * sysctl helper routine for vfs.generic.conf lookups.
    457  */
    458 #if defined(COMPAT_09) || defined(COMPAT_43) || defined(COMPAT_44)
    459 static struct sysctllog *compat_clog;
    460 
    461 static int
    462 sysctl_vfs_generic_conf(SYSCTLFN_ARGS)
    463 {
    464         struct vfsconf vfc;
    465         extern const char * const mountcompatnames[];
    466         extern int nmountcompatnames;
    467 	struct sysctlnode node;
    468 	struct vfsops *vfsp;
    469 	u_int vfsnum;
    470 
    471 	if (namelen != 1)
    472 		return (ENOTDIR);
    473 	vfsnum = name[0];
    474 	if (vfsnum >= nmountcompatnames ||
    475 	    mountcompatnames[vfsnum] == NULL)
    476 		return (EOPNOTSUPP);
    477 	vfsp = vfs_getopsbyname(mountcompatnames[vfsnum]);
    478 	if (vfsp == NULL)
    479 		return (EOPNOTSUPP);
    480 
    481 	vfc.vfc_vfsops = vfsp;
    482 	strncpy(vfc.vfc_name, vfsp->vfs_name, sizeof(vfc.vfc_name));
    483 	vfc.vfc_typenum = vfsnum;
    484 	vfc.vfc_refcount = vfsp->vfs_refcount;
    485 	vfc.vfc_flags = 0;
    486 	vfc.vfc_mountroot = vfsp->vfs_mountroot;
    487 	vfc.vfc_next = NULL;
    488 	vfs_delref(vfsp);
    489 
    490 	node = *rnode;
    491 	node.sysctl_data = &vfc;
    492 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
    493 }
    494 
    495 /*
    496  * Top level filesystem related information gathering.
    497  */
    498 void
    499 compat_sysctl_init(void)
    500 {
    501 	extern int nmountcompatnames;
    502 
    503 	sysctl_createv(&compat_clog, 0, NULL, NULL,
    504 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    505 		       CTLTYPE_INT, "maxtypenum",
    506 		       SYSCTL_DESCR("Highest valid filesystem type number"),
    507 		       NULL, nmountcompatnames, NULL, 0,
    508 		       CTL_VFS, VFS_GENERIC, VFS_MAXTYPENUM, CTL_EOL);
    509 	sysctl_createv(&compat_clog, 0, NULL, NULL,
    510 		       CTLFLAG_PERMANENT,
    511 		       CTLTYPE_STRUCT, "conf",
    512 		       SYSCTL_DESCR("Filesystem configuration information"),
    513 		       sysctl_vfs_generic_conf, 0, NULL,
    514 		       sizeof(struct vfsconf),
    515 		       CTL_VFS, VFS_GENERIC, VFS_CONF, CTL_EOL);
    516 }
    517 
    518 void
    519 compat_sysctl_fini(void)
    520 {
    521 
    522 	sysctl_teardown(&compat_clog);
    523 }
    524 #endif
    525