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