Home | History | Annotate | Line # | Download | only in common
vfs_syscalls_30.c revision 1.5
      1 /*	$NetBSD: vfs_syscalls_30.c,v 1.5 2005/09/13 01:42:32 christos 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.5 2005/09/13 01:42:32 christos 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 
     56 #include <sys/sa.h>
     57 #include <sys/syscallargs.h>
     58 
     59 #include <compat/common/compat_util.h>
     60 #include <compat/sys/stat.h>
     61 #include <compat/sys/dirent.h>
     62 
     63 static void cvtstat(struct stat13 *, const struct stat *);
     64 
     65 /*
     66  * Convert from a new to an old stat structure.
     67  */
     68 static void
     69 cvtstat(struct stat13 *ost, const struct stat *st)
     70 {
     71 
     72 	ost->st_dev = st->st_dev;
     73 	ost->st_ino = (uint32_t)st->st_ino;
     74 	ost->st_mode = st->st_mode;
     75 	ost->st_nlink = st->st_nlink;
     76 	ost->st_uid = st->st_uid;
     77 	ost->st_gid = st->st_gid;
     78 	ost->st_rdev = st->st_rdev;
     79 	ost->st_atimespec = st->st_atimespec;
     80 	ost->st_mtimespec = st->st_mtimespec;
     81 	ost->st_ctimespec = st->st_ctimespec;
     82 	ost->st_birthtimespec = st->st_birthtimespec;
     83 	ost->st_size = st->st_size;
     84 	ost->st_blocks = st->st_blocks;
     85 	ost->st_blksize = st->st_blksize;
     86 	ost->st_flags = st->st_flags;
     87 	ost->st_gen = st->st_gen;
     88 }
     89 
     90 /*
     91  * Get file status; this version follows links.
     92  */
     93 /* ARGSUSED */
     94 int
     95 compat_30_sys___stat13(struct lwp *l, void *v, register_t *retval)
     96 {
     97 	struct compat_30_sys___stat13_args /* {
     98 		syscallarg(const char *) path;
     99 		syscallarg(struct stat13 *) ub;
    100 	} */ *uap = v;
    101 	struct proc *p = l->l_proc;
    102 	struct stat sb;
    103 	struct stat13 osb;
    104 	int error;
    105 	struct nameidata nd;
    106 
    107 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
    108 	    SCARG(uap, path), p);
    109 	if ((error = namei(&nd)) != 0)
    110 		return error;
    111 	error = vn_stat(nd.ni_vp, &sb, p);
    112 	vput(nd.ni_vp);
    113 	if (error)
    114 		return error;
    115 	cvtstat(&osb, &sb);
    116 	error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
    117 	return error;
    118 }
    119 
    120 
    121 /*
    122  * Get file status; this version does not follow links.
    123  */
    124 /* ARGSUSED */
    125 int
    126 compat_30_sys___lstat13(struct lwp *l, void *v, register_t *retval)
    127 {
    128 	struct compat_30_sys___lstat13_args /* {
    129 		syscallarg(const char *) path;
    130 		syscallarg(struct stat13 *) ub;
    131 	} */ *uap = v;
    132 	struct proc *p = l->l_proc;
    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), p);
    140 	if ((error = namei(&nd)) != 0)
    141 		return error;
    142 	error = vn_stat(nd.ni_vp, &sb, p);
    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 /*
    152  * Return status information about a file descriptor.
    153  */
    154 /* ARGSUSED */
    155 int
    156 compat_30_sys___fstat13(struct lwp *l, void *v, register_t *retval)
    157 {
    158 	struct compat_30_sys___fstat13_args /* {
    159 		syscallarg(int) fd;
    160 		syscallarg(struct stat13 *) sb;
    161 	} */ *uap = v;
    162 	struct proc *p = l->l_proc;
    163 	int fd = SCARG(uap, fd);
    164 	struct filedesc *fdp = p->p_fd;
    165 	struct file *fp;
    166 	struct stat sb;
    167 	struct stat13 osb;
    168 	int error;
    169 
    170 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    171 		return EBADF;
    172 
    173 	FILE_USE(fp);
    174 	error = (*fp->f_ops->fo_stat)(fp, &sb, p);
    175 	FILE_UNUSE(fp, p);
    176 
    177 	if (error)
    178 		return error;
    179 	cvtstat(&osb, &sb);
    180 	error = copyout(&osb, SCARG(uap, sb), sizeof (osb));
    181 	return error;
    182 }
    183 
    184 /*
    185  * Read a block of directory entries in a file system independent format.
    186  */
    187 int
    188 compat_30_sys_getdents(struct lwp *l, void *v, register_t *retval)
    189 {
    190 	struct compat_30_sys_getdents_args /* {
    191 		syscallarg(int) fd;
    192 		syscallarg(char *) buf;
    193 		syscallarg(size_t) count;
    194 	} */ *uap = v;
    195 	struct proc *p = l->l_proc;
    196 	struct dirent *bdp;
    197 	struct vnode *vp;
    198 	caddr_t inp, tbuf;	/* BSD-format */
    199 	int len, reclen;	/* BSD-format */
    200 	caddr_t outp;		/* NetBSD-3.0-format */
    201 	int resid;
    202 	struct file *fp;
    203 	struct uio auio;
    204 	struct iovec aiov;
    205 	struct dirent12 idb;
    206 	off_t off;		/* true file offset */
    207 	int buflen, error, eofflag;
    208 	off_t *cookiebuf = NULL, *cookie;
    209 	int ncookies;
    210 
    211 	/* getvnode() will use the descriptor for us */
    212 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    213 		return error;
    214 
    215 	if ((fp->f_flag & FREAD) == 0) {
    216 		error = EBADF;
    217 		goto out1;
    218 	}
    219 
    220 	vp = (struct vnode *)fp->f_data;
    221 	if (vp->v_type != VDIR) {
    222 		error = EINVAL;
    223 		goto out1;
    224 	}
    225 
    226 	buflen = min(MAXBSIZE, SCARG(uap, count));
    227 	tbuf = malloc(buflen, M_TEMP, M_WAITOK);
    228 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    229 	off = fp->f_offset;
    230 again:
    231 	aiov.iov_base = tbuf;
    232 	aiov.iov_len = buflen;
    233 	auio.uio_iov = &aiov;
    234 	auio.uio_iovcnt = 1;
    235 	auio.uio_rw = UIO_READ;
    236 	auio.uio_segflg = UIO_SYSSPACE;
    237 	auio.uio_procp = NULL;
    238 	auio.uio_resid = buflen;
    239 	auio.uio_offset = off;
    240 	/*
    241          * First we read into the malloc'ed buffer, then
    242          * we massage it into user space, one record at a time.
    243          */
    244 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
    245 	    &ncookies);
    246 	if (error)
    247 		goto out;
    248 
    249 	inp = tbuf;
    250 	outp = SCARG(uap, buf);
    251 	resid = SCARG(uap, count);
    252 	if ((len = buflen - auio.uio_resid) == 0)
    253 		goto eof;
    254 
    255 	for (cookie = cookiebuf; len > 0; len -= reclen) {
    256 		bdp = (struct dirent *)inp;
    257 		reclen = bdp->d_reclen;
    258 		if (reclen & _DIRENT_ALIGN(bdp))
    259 			panic("netbsd30_getdents: bad reclen %d", reclen);
    260 		if (cookie)
    261 			off = *cookie++; /* each entry points to the next */
    262 		else
    263 			off += reclen;
    264 		if ((off >> 32) != 0) {
    265 			compat_offseterr(vp, "netbsd30_getdents");
    266 			error = EINVAL;
    267 			goto out;
    268 		}
    269 		if (bdp->d_namlen >= sizeof(idb.d_name))
    270 			idb.d_namlen = sizeof(idb.d_name) - 1;
    271 		else
    272 			idb.d_namlen = bdp->d_namlen;
    273 		idb.d_reclen = _DIRENT_SIZE(&idb);
    274 		if (reclen > len || resid < idb.d_reclen) {
    275 			/* entry too big for buffer, so just stop */
    276 			outp++;
    277 			break;
    278 		}
    279 		/*
    280 		 * Massage in place to make a NetBSD-3.0-shaped dirent
    281 		 * (otherwise we have to worry about touching user memory
    282 		 * outside of the copyout() call).
    283 		 */
    284 		idb.d_fileno = (u_int32_t)bdp->d_fileno;
    285 		idb.d_type = bdp->d_type;
    286 		(void)memcpy(idb.d_name, bdp->d_name, idb.d_namlen);
    287 		memset(idb.d_name + idb.d_namlen, 0,
    288 		    idb.d_reclen - _DIRENT_NAMEOFF(&idb) - idb.d_namlen);
    289 		if ((error = copyout(&idb, outp, idb.d_reclen)) != 0)
    290 			goto out;
    291 		/* advance past this real entry */
    292 		inp += reclen;
    293 		/* advance output past NetBSD-3.0-shaped entry */
    294 		outp += idb.d_reclen;
    295 		resid -= idb.d_reclen;
    296 	}
    297 
    298 	/* if we squished out the whole block, try again */
    299 	if (outp == SCARG(uap, buf))
    300 		goto again;
    301 	fp->f_offset = off;	/* update the vnode offset */
    302 
    303 eof:
    304 	*retval = SCARG(uap, count) - resid;
    305 out:
    306 	VOP_UNLOCK(vp, 0);
    307 	if (cookiebuf)
    308 		free(cookiebuf, M_TEMP);
    309 	free(tbuf, M_TEMP);
    310 out1:
    311 	FILE_UNUSE(fp, p);
    312 	return error;
    313 }
    314