Home | History | Annotate | Line # | Download | only in common
vfs_syscalls_30.c revision 1.10
      1 /*	$NetBSD: vfs_syscalls_30.c,v 1.10 2006/07/13 12:00:25 martin Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2005 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Christos Zoulas.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls_30.c,v 1.10 2006/07/13 12:00:25 martin Exp $");
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/namei.h>
     44 #include <sys/filedesc.h>
     45 #include <sys/kernel.h>
     46 #include <sys/file.h>
     47 #include <sys/stat.h>
     48 #include <sys/socketvar.h>
     49 #include <sys/vnode.h>
     50 #include <sys/mount.h>
     51 #include <sys/proc.h>
     52 #include <sys/uio.h>
     53 #include <sys/dirent.h>
     54 #include <sys/malloc.h>
     55 #include <sys/kauth.h>
     56 
     57 #include <sys/sa.h>
     58 #include <sys/syscallargs.h>
     59 
     60 #include <compat/common/compat_util.h>
     61 #include <compat/sys/stat.h>
     62 #include <compat/sys/dirent.h>
     63 #include <compat/sys/mount.h>
     64 
     65 static void cvtstat(struct stat13 *, const struct stat *);
     66 
     67 /*
     68  * Convert from a new to an old stat structure.
     69  */
     70 static void
     71 cvtstat(struct stat13 *ost, const struct stat *st)
     72 {
     73 
     74 	ost->st_dev = st->st_dev;
     75 	ost->st_ino = (uint32_t)st->st_ino;
     76 	ost->st_mode = st->st_mode;
     77 	ost->st_nlink = st->st_nlink;
     78 	ost->st_uid = st->st_uid;
     79 	ost->st_gid = st->st_gid;
     80 	ost->st_rdev = st->st_rdev;
     81 	ost->st_atimespec = st->st_atimespec;
     82 	ost->st_mtimespec = st->st_mtimespec;
     83 	ost->st_ctimespec = st->st_ctimespec;
     84 	ost->st_birthtimespec = st->st_birthtimespec;
     85 	ost->st_size = st->st_size;
     86 	ost->st_blocks = st->st_blocks;
     87 	ost->st_blksize = st->st_blksize;
     88 	ost->st_flags = st->st_flags;
     89 	ost->st_gen = st->st_gen;
     90 }
     91 
     92 /*
     93  * Get file status; this version follows links.
     94  */
     95 /* ARGSUSED */
     96 int
     97 compat_30_sys___stat13(struct lwp *l, void *v, register_t *retval)
     98 {
     99 	struct compat_30_sys___stat13_args /* {
    100 		syscallarg(const char *) path;
    101 		syscallarg(struct stat13 *) ub;
    102 	} */ *uap = v;
    103 	struct stat sb;
    104 	struct stat13 osb;
    105 	int error;
    106 	struct nameidata nd;
    107 
    108 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
    109 	    SCARG(uap, path), l);
    110 	if ((error = namei(&nd)) != 0)
    111 		return error;
    112 	error = vn_stat(nd.ni_vp, &sb, l);
    113 	vput(nd.ni_vp);
    114 	if (error)
    115 		return error;
    116 	cvtstat(&osb, &sb);
    117 	error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
    118 	return error;
    119 }
    120 
    121 
    122 /*
    123  * Get file status; this version does not follow links.
    124  */
    125 /* ARGSUSED */
    126 int
    127 compat_30_sys___lstat13(struct lwp *l, void *v, register_t *retval)
    128 {
    129 	struct compat_30_sys___lstat13_args /* {
    130 		syscallarg(const char *) path;
    131 		syscallarg(struct stat13 *) ub;
    132 	} */ *uap = v;
    133 	struct stat sb;
    134 	struct stat13 osb;
    135 	int error;
    136 	struct nameidata nd;
    137 
    138 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
    139 	    SCARG(uap, path), l);
    140 	if ((error = namei(&nd)) != 0)
    141 		return error;
    142 	error = vn_stat(nd.ni_vp, &sb, l);
    143 	vput(nd.ni_vp);
    144 	if (error)
    145 		return error;
    146 	cvtstat(&osb, &sb);
    147 	error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
    148 	return error;
    149 }
    150 
    151 /* ARGSUSED */
    152 int
    153 compat_30_sys_fhstat(struct lwp *l, void *v, register_t *retval)
    154 {
    155 	struct compat_30_sys_fhstat_args /* {
    156 		syscallarg(const struct compat_30_fhandle *) fhp;
    157 		syscallarg(struct stat13 *) sb;
    158 	} */ *uap = v;
    159 	struct proc *p = l->l_proc;
    160 	struct stat sb;
    161 	struct stat13 osb;
    162 	int error;
    163 	struct compat_30_fhandle fh;
    164 	struct mount *mp;
    165 	struct vnode *vp;
    166 
    167 	/*
    168 	 * Must be super user
    169 	 */
    170 	if ((error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER,
    171 	    &p->p_acflag)))
    172 		return (error);
    173 
    174 	if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fh))) != 0)
    175 		return (error);
    176 
    177 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
    178 		return (ESTALE);
    179 	if (mp->mnt_op->vfs_fhtovp == NULL)
    180 		return EOPNOTSUPP;
    181 	if ((error = VFS_FHTOVP(mp, (struct fid*)&fh.fh_fid, &vp)))
    182 		return (error);
    183 	error = vn_stat(vp, &sb, l);
    184 	vput(vp);
    185 	if (error)
    186 		return (error);
    187 	cvtstat(&osb, &sb);
    188 	error = copyout(&osb, SCARG(uap, sb), sizeof(sb));
    189 	return (error);
    190 }
    191 
    192 /*
    193  * Return status information about a file descriptor.
    194  */
    195 /* ARGSUSED */
    196 int
    197 compat_30_sys___fstat13(struct lwp *l, void *v, register_t *retval)
    198 {
    199 	struct compat_30_sys___fstat13_args /* {
    200 		syscallarg(int) fd;
    201 		syscallarg(struct stat13 *) sb;
    202 	} */ *uap = v;
    203 	struct proc *p = l->l_proc;
    204 	int fd = SCARG(uap, fd);
    205 	struct filedesc *fdp = p->p_fd;
    206 	struct file *fp;
    207 	struct stat sb;
    208 	struct stat13 osb;
    209 	int error;
    210 
    211 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    212 		return EBADF;
    213 
    214 	FILE_USE(fp);
    215 	error = (*fp->f_ops->fo_stat)(fp, &sb, l);
    216 	FILE_UNUSE(fp, l);
    217 
    218 	if (error)
    219 		return error;
    220 	cvtstat(&osb, &sb);
    221 	error = copyout(&osb, SCARG(uap, sb), sizeof (osb));
    222 	return error;
    223 }
    224 
    225 /*
    226  * Read a block of directory entries in a file system independent format.
    227  */
    228 int
    229 compat_30_sys_getdents(struct lwp *l, void *v, register_t *retval)
    230 {
    231 	struct compat_30_sys_getdents_args /* {
    232 		syscallarg(int) fd;
    233 		syscallarg(char *) buf;
    234 		syscallarg(size_t) count;
    235 	} */ *uap = v;
    236 	struct proc *p = l->l_proc;
    237 	struct dirent *bdp;
    238 	struct vnode *vp;
    239 	caddr_t inp, tbuf;	/* BSD-format */
    240 	int len, reclen;	/* BSD-format */
    241 	caddr_t outp;		/* NetBSD-3.0-format */
    242 	int resid;
    243 	struct file *fp;
    244 	struct uio auio;
    245 	struct iovec aiov;
    246 	struct dirent12 idb;
    247 	off_t off;		/* true file offset */
    248 	int buflen, error, eofflag;
    249 	off_t *cookiebuf = NULL, *cookie;
    250 	int ncookies;
    251 
    252 	/* getvnode() will use the descriptor for us */
    253 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    254 		return error;
    255 
    256 	if ((fp->f_flag & FREAD) == 0) {
    257 		error = EBADF;
    258 		goto out1;
    259 	}
    260 
    261 	vp = (struct vnode *)fp->f_data;
    262 	if (vp->v_type != VDIR) {
    263 		error = EINVAL;
    264 		goto out1;
    265 	}
    266 
    267 	buflen = min(MAXBSIZE, SCARG(uap, count));
    268 	tbuf = malloc(buflen, M_TEMP, M_WAITOK);
    269 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    270 	off = fp->f_offset;
    271 again:
    272 	aiov.iov_base = tbuf;
    273 	aiov.iov_len = buflen;
    274 	auio.uio_iov = &aiov;
    275 	auio.uio_iovcnt = 1;
    276 	auio.uio_rw = UIO_READ;
    277 	auio.uio_resid = buflen;
    278 	auio.uio_offset = off;
    279 	UIO_SETUP_SYSSPACE(&auio);
    280 	/*
    281          * First we read into the malloc'ed buffer, then
    282          * we massage it into user space, one record at a time.
    283          */
    284 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
    285 	    &ncookies);
    286 	if (error)
    287 		goto out;
    288 
    289 	inp = tbuf;
    290 	outp = SCARG(uap, buf);
    291 	resid = SCARG(uap, count);
    292 	if ((len = buflen - auio.uio_resid) == 0)
    293 		goto eof;
    294 
    295 	for (cookie = cookiebuf; len > 0; len -= reclen) {
    296 		bdp = (struct dirent *)inp;
    297 		reclen = bdp->d_reclen;
    298 		if (reclen & _DIRENT_ALIGN(bdp))
    299 			panic("netbsd30_getdents: bad reclen %d", reclen);
    300 		if (cookie)
    301 			off = *cookie++; /* each entry points to the next */
    302 		else
    303 			off += reclen;
    304 		if ((off >> 32) != 0) {
    305 			compat_offseterr(vp, "netbsd30_getdents");
    306 			error = EINVAL;
    307 			goto out;
    308 		}
    309 		if (bdp->d_namlen >= sizeof(idb.d_name))
    310 			idb.d_namlen = sizeof(idb.d_name) - 1;
    311 		else
    312 			idb.d_namlen = bdp->d_namlen;
    313 		idb.d_reclen = _DIRENT_SIZE(&idb);
    314 		if (reclen > len || resid < idb.d_reclen) {
    315 			/* entry too big for buffer, so just stop */
    316 			outp++;
    317 			break;
    318 		}
    319 		/*
    320 		 * Massage in place to make a NetBSD-3.0-shaped dirent
    321 		 * (otherwise we have to worry about touching user memory
    322 		 * outside of the copyout() call).
    323 		 */
    324 		idb.d_fileno = (u_int32_t)bdp->d_fileno;
    325 		idb.d_type = bdp->d_type;
    326 		(void)memcpy(idb.d_name, bdp->d_name, idb.d_namlen);
    327 		memset(idb.d_name + idb.d_namlen, 0,
    328 		    idb.d_reclen - _DIRENT_NAMEOFF(&idb) - idb.d_namlen);
    329 		if ((error = copyout(&idb, outp, idb.d_reclen)) != 0)
    330 			goto out;
    331 		/* advance past this real entry */
    332 		inp += reclen;
    333 		/* advance output past NetBSD-3.0-shaped entry */
    334 		outp += idb.d_reclen;
    335 		resid -= idb.d_reclen;
    336 	}
    337 
    338 	/* if we squished out the whole block, try again */
    339 	if (outp == SCARG(uap, buf))
    340 		goto again;
    341 	fp->f_offset = off;	/* update the vnode offset */
    342 
    343 eof:
    344 	*retval = SCARG(uap, count) - resid;
    345 out:
    346 	VOP_UNLOCK(vp, 0);
    347 	if (cookiebuf)
    348 		free(cookiebuf, M_TEMP);
    349 	free(tbuf, M_TEMP);
    350 out1:
    351 	FILE_UNUSE(fp, l);
    352 	return error;
    353 }
    354 
    355 /*
    356  * Get file handle system call
    357  */
    358 int
    359 compat_30_sys_getfh(struct lwp *l, void *v, register_t *retval)
    360 {
    361 	struct compat_30_sys_getfh_args /* {
    362 		syscallarg(char *) fname;
    363 		syscallarg(struct compat_30_fhandle *) fhp;
    364 	} */ *uap = v;
    365 	struct proc *p = l->l_proc;
    366 	struct vnode *vp;
    367 	fhandle_t fh;
    368 	int error;
    369 	struct nameidata nd;
    370 	size_t sz;
    371 
    372 	/*
    373 	 * Must be super user
    374 	 */
    375 	error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER,
    376 				  &p->p_acflag);
    377 	if (error)
    378 		return (error);
    379 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
    380 	    SCARG(uap, fname), l);
    381 	error = namei(&nd);
    382 	if (error)
    383 		return (error);
    384 	vp = nd.ni_vp;
    385 	sz = sizeof(struct compat_30_fhandle);
    386 	error = vfs_composefh(vp, &fh, &sz);
    387 	vput(vp);
    388 	if (error)
    389 		return (error);
    390 	error = copyout(&fh, (caddr_t)SCARG(uap, fhp),
    391 	    sizeof(struct compat_30_fhandle));
    392 	return (error);
    393 }
    394