Home | History | Annotate | Line # | Download | only in common
vfs_syscalls_12.c revision 1.35
      1  1.35  christos /*	$NetBSD: vfs_syscalls_12.c,v 1.35 2017/12/03 15:23:30 christos Exp $	*/
      2   1.1      fvdl 
      3   1.1      fvdl /*
      4   1.1      fvdl  * Copyright (c) 1989, 1993
      5   1.1      fvdl  *	The Regents of the University of California.  All rights reserved.
      6   1.1      fvdl  * (c) UNIX System Laboratories, Inc.
      7   1.1      fvdl  * All or some portions of this file are derived from material licensed
      8   1.1      fvdl  * to the University of California by American Telephone and Telegraph
      9   1.1      fvdl  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10   1.1      fvdl  * the permission of UNIX System Laboratories, Inc.
     11   1.1      fvdl  *
     12   1.1      fvdl  * Redistribution and use in source and binary forms, with or without
     13   1.1      fvdl  * modification, are permitted provided that the following conditions
     14   1.1      fvdl  * are met:
     15   1.1      fvdl  * 1. Redistributions of source code must retain the above copyright
     16   1.1      fvdl  *    notice, this list of conditions and the following disclaimer.
     17   1.1      fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     18   1.1      fvdl  *    notice, this list of conditions and the following disclaimer in the
     19   1.1      fvdl  *    documentation and/or other materials provided with the distribution.
     20  1.14       agc  * 3. Neither the name of the University nor the names of its contributors
     21   1.1      fvdl  *    may be used to endorse or promote products derived from this software
     22   1.1      fvdl  *    without specific prior written permission.
     23   1.1      fvdl  *
     24   1.1      fvdl  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25   1.1      fvdl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26   1.1      fvdl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27   1.1      fvdl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28   1.1      fvdl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29   1.1      fvdl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30   1.1      fvdl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31   1.1      fvdl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32   1.1      fvdl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33   1.1      fvdl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34   1.1      fvdl  * SUCH DAMAGE.
     35   1.1      fvdl  *
     36   1.1      fvdl  *	From: @(#)vfs_syscalls.c	8.28 (Berkeley) 12/10/94
     37   1.1      fvdl  */
     38  1.10     lukem 
     39  1.10     lukem #include <sys/cdefs.h>
     40  1.35  christos __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls_12.c,v 1.35 2017/12/03 15:23:30 christos Exp $");
     41   1.1      fvdl 
     42   1.1      fvdl #include <sys/param.h>
     43   1.1      fvdl #include <sys/systm.h>
     44   1.1      fvdl #include <sys/namei.h>
     45   1.1      fvdl #include <sys/filedesc.h>
     46   1.1      fvdl #include <sys/kernel.h>
     47   1.1      fvdl #include <sys/file.h>
     48   1.1      fvdl #include <sys/stat.h>
     49   1.2  christos #include <sys/socketvar.h>
     50   1.1      fvdl #include <sys/vnode.h>
     51   1.1      fvdl #include <sys/proc.h>
     52   1.1      fvdl #include <sys/uio.h>
     53   1.1      fvdl #include <sys/dirent.h>
     54  1.21       dsl #include <sys/vfs_syscalls.h>
     55   1.1      fvdl 
     56   1.1      fvdl #include <sys/syscallargs.h>
     57   1.1      fvdl 
     58  1.16  christos #include <compat/sys/stat.h>
     59  1.30  christos #include <compat/sys/dirent.h>
     60   1.2  christos 
     61   1.2  christos /*
     62   1.4  wrstuden  * Convert from a new to an old stat structure.
     63   1.2  christos  */
     64  1.22       dsl void
     65  1.22       dsl compat_12_stat_conv(const struct stat *st, struct stat12 *ost)
     66   1.2  christos {
     67   1.2  christos 
     68   1.2  christos 	ost->st_dev = st->st_dev;
     69   1.2  christos 	ost->st_ino = st->st_ino;
     70   1.4  wrstuden 	ost->st_mode = st->st_mode & 0xffff;
     71   1.2  christos 	if (st->st_nlink >= (1 << 15))
     72   1.2  christos 		ost->st_nlink = (1 << 15) - 1;
     73   1.2  christos 	else
     74   1.2  christos 		ost->st_nlink = st->st_nlink;
     75   1.2  christos 	ost->st_uid = st->st_uid;
     76   1.2  christos 	ost->st_gid = st->st_gid;
     77   1.2  christos 	ost->st_rdev = st->st_rdev;
     78  1.27  christos 	timespec_to_timespec50(&st->st_atimespec, &ost->st_atimespec);
     79  1.27  christos 	timespec_to_timespec50(&st->st_mtimespec, &ost->st_mtimespec);
     80  1.27  christos 	timespec_to_timespec50(&st->st_ctimespec, &ost->st_ctimespec);
     81   1.2  christos 	ost->st_size = st->st_size;
     82   1.3   mycroft 	ost->st_blocks = st->st_blocks;
     83   1.2  christos 	ost->st_blksize = st->st_blksize;
     84   1.2  christos 	ost->st_flags = st->st_flags;
     85   1.2  christos 	ost->st_gen = st->st_gen;
     86   1.2  christos }
     87   1.3   mycroft 
     88   1.1      fvdl /*
     89   1.1      fvdl  * Read a block of directory entries in a file system independent format.
     90   1.1      fvdl  */
     91   1.1      fvdl int
     92  1.24       dsl compat_12_sys_getdirentries(struct lwp *l, const struct compat_12_sys_getdirentries_args *uap, register_t *retval)
     93   1.1      fvdl {
     94  1.24       dsl 	/* {
     95   1.1      fvdl 		syscallarg(int) fd;
     96   1.1      fvdl 		syscallarg(char *) buf;
     97   1.1      fvdl 		syscallarg(u_int) count;
     98   1.1      fvdl 		syscallarg(long *) basep;
     99  1.24       dsl 	} */
    100  1.30  christos 	struct dirent *bdp;
    101  1.30  christos 	struct vnode *vp;
    102  1.30  christos 	char *inp, *tbuf;		/* Current-format */
    103  1.30  christos 	int len, reclen;		/* Current-format */
    104  1.30  christos 	char *outp;			/* Dirent12-format */
    105  1.30  christos 	int resid, old_reclen = 0;	/* Dirent12-format */
    106   1.1      fvdl 	struct file *fp;
    107  1.30  christos 	struct uio auio;
    108  1.30  christos 	struct iovec aiov;
    109  1.30  christos 	struct dirent12 idb;
    110  1.30  christos 	off_t off;		/* true file offset */
    111  1.30  christos 	int buflen, error, eofflag, nbytes;
    112  1.30  christos 	struct vattr va;
    113  1.30  christos 	off_t *cookiebuf = NULL, *cookie;
    114  1.30  christos 	int ncookies;
    115   1.1      fvdl 	long loff;
    116  1.30  christos 
    117  1.26        ad 	/* fd_getvnode() will use the descriptor for us */
    118  1.26        ad 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
    119  1.30  christos 		return (error);
    120  1.30  christos 
    121   1.5   thorpej 	if ((fp->f_flag & FREAD) == 0) {
    122   1.5   thorpej 		error = EBADF;
    123  1.30  christos 		goto out1;
    124  1.30  christos 	}
    125  1.30  christos 
    126  1.31      matt 	vp = (struct vnode *)fp->f_vnode;
    127  1.30  christos 	if (vp->v_type != VDIR) {
    128  1.30  christos 		error = ENOTDIR;
    129  1.30  christos 		goto out1;
    130   1.5   thorpej 	}
    131   1.1      fvdl 
    132  1.30  christos 	vn_lock(vp, LK_SHARED | LK_RETRY);
    133  1.30  christos 	error = VOP_GETATTR(vp, &va, l->l_cred);
    134  1.30  christos 	VOP_UNLOCK(vp);
    135  1.30  christos 	if (error)
    136  1.30  christos 		goto out1;
    137  1.30  christos 
    138   1.1      fvdl 	loff = fp->f_offset;
    139  1.30  christos 	nbytes = SCARG(uap, count);
    140  1.30  christos 	buflen = min(MAXBSIZE, nbytes);
    141  1.30  christos 	if (buflen < va.va_blocksize)
    142  1.30  christos 		buflen = va.va_blocksize;
    143  1.30  christos 	tbuf = malloc(buflen, M_TEMP, M_WAITOK);
    144  1.30  christos 
    145  1.30  christos 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    146  1.30  christos 	off = fp->f_offset;
    147  1.30  christos again:
    148  1.30  christos 	aiov.iov_base = tbuf;
    149  1.30  christos 	aiov.iov_len = buflen;
    150  1.30  christos 	auio.uio_iov = &aiov;
    151  1.30  christos 	auio.uio_iovcnt = 1;
    152  1.30  christos 	auio.uio_rw = UIO_READ;
    153  1.30  christos 	auio.uio_resid = buflen;
    154  1.30  christos 	auio.uio_offset = off;
    155  1.30  christos 	UIO_SETUP_SYSSPACE(&auio);
    156  1.30  christos 	/*
    157  1.30  christos          * First we read into the malloc'ed buffer, then
    158  1.30  christos          * we massage it into user space, one record at a time.
    159  1.30  christos          */
    160  1.30  christos 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
    161  1.30  christos 	    &ncookies);
    162  1.30  christos 	if (error)
    163  1.30  christos 		goto out;
    164  1.30  christos 
    165  1.30  christos 	inp = tbuf;
    166  1.30  christos 	outp = SCARG(uap, buf);
    167  1.30  christos 	resid = nbytes;
    168  1.30  christos 	if ((len = buflen - auio.uio_resid) == 0)
    169  1.30  christos 		goto eof;
    170  1.30  christos 
    171  1.30  christos 	for (cookie = cookiebuf; len > 0; len -= reclen) {
    172  1.30  christos 		bdp = (struct dirent *)inp;
    173  1.30  christos 		reclen = bdp->d_reclen;
    174  1.34  riastrad 		if (reclen & 3) {
    175  1.34  riastrad 			error = EIO;
    176  1.34  riastrad 			goto out;
    177  1.34  riastrad 		}
    178  1.30  christos 		if (bdp->d_fileno == 0) {
    179  1.30  christos 			inp += reclen;	/* it is a hole; squish it out */
    180  1.30  christos 			if (cookie)
    181  1.30  christos 				off = *cookie++;
    182  1.30  christos 			else
    183  1.30  christos 				off += reclen;
    184  1.30  christos 			continue;
    185  1.30  christos 		}
    186  1.35  christos 		if (bdp->d_namlen >= sizeof(idb.d_name))
    187  1.35  christos 			idb.d_namlen = sizeof(idb.d_name) - 1;
    188  1.35  christos 		else
    189  1.35  christos 			idb.d_namlen = bdp->d_namlen;
    190  1.30  christos 		old_reclen = _DIRENT_RECLEN(&idb, bdp->d_namlen);
    191  1.30  christos 		if (reclen > len || resid < old_reclen) {
    192  1.30  christos 			/* entry too big for buffer, so just stop */
    193  1.30  christos 			outp++;
    194  1.30  christos 			break;
    195  1.30  christos 		}
    196  1.30  christos 		/*
    197  1.30  christos 		 * Massage in place to make a Dirent12-shaped dirent (otherwise
    198  1.30  christos 		 * we have to worry about touching user memory outside of
    199  1.30  christos 		 * the copyout() call).
    200  1.30  christos 		 */
    201  1.30  christos 		idb.d_fileno = (uint32_t)bdp->d_fileno;
    202  1.30  christos 		idb.d_reclen = (uint16_t)old_reclen;
    203  1.30  christos 		idb.d_type = (uint8_t)bdp->d_type;
    204  1.35  christos 		(void)memcpy(idb.d_name, bdp->d_name, idb.d_namlen);
    205  1.35  christos 		memset(idb.d_name + idb.d_namlen, 0,
    206  1.35  christos 		    idb.d_reclen - _DIRENT_NAMEOFF(&idb) - idb.d_namlen);
    207  1.30  christos 		if ((error = copyout(&idb, outp, old_reclen)))
    208  1.30  christos 			goto out;
    209  1.30  christos 		/* advance past this real entry */
    210  1.30  christos 		inp += reclen;
    211  1.30  christos 		if (cookie)
    212  1.30  christos 			off = *cookie++; /* each entry points to itself */
    213  1.30  christos 		else
    214  1.30  christos 			off += reclen;
    215  1.30  christos 		/* advance output past Dirent12-shaped entry */
    216  1.30  christos 		outp += old_reclen;
    217  1.30  christos 		resid -= old_reclen;
    218  1.30  christos 	}
    219   1.1      fvdl 
    220  1.30  christos 	/* if we squished out the whole block, try again */
    221  1.30  christos 	if (outp == SCARG(uap, buf)) {
    222  1.30  christos 		if (cookiebuf)
    223  1.30  christos 			free(cookiebuf, M_TEMP);
    224  1.30  christos 		cookiebuf = NULL;
    225  1.30  christos 		goto again;
    226  1.30  christos 	}
    227  1.30  christos 	fp->f_offset = off;	/* update the vnode offset */
    228   1.1      fvdl 
    229  1.30  christos eof:
    230  1.30  christos 	*retval = nbytes - resid;
    231  1.30  christos out:
    232  1.30  christos 	VOP_UNLOCK(vp);
    233  1.30  christos 	if (cookiebuf)
    234  1.30  christos 		free(cookiebuf, M_TEMP);
    235  1.30  christos 	free(tbuf, M_TEMP);
    236  1.30  christos out1:
    237  1.25        ad 	fd_putfile(SCARG(uap, fd));
    238  1.30  christos 	if (error)
    239  1.30  christos 		return error;
    240  1.30  christos 	return copyout(&loff, SCARG(uap, basep), sizeof(long));
    241   1.2  christos }
    242   1.2  christos 
    243   1.2  christos /*
    244   1.2  christos  * Get file status; this version follows links.
    245   1.2  christos  */
    246   1.2  christos /* ARGSUSED */
    247   1.2  christos int
    248  1.24       dsl compat_12_sys_stat(struct lwp *l, const struct compat_12_sys_stat_args *uap, register_t *retval)
    249   1.2  christos {
    250  1.24       dsl 	/* {
    251   1.3   mycroft 		syscallarg(const char *) path;
    252   1.2  christos 		syscallarg(struct stat12 *) ub;
    253  1.24       dsl 	} */
    254   1.2  christos 	struct stat sb;
    255   1.2  christos 	struct stat12 osb;
    256   1.2  christos 	int error;
    257   1.2  christos 
    258  1.25        ad 	error = do_sys_stat(SCARG(uap, path), FOLLOW, &sb);
    259   1.2  christos 	if (error)
    260   1.2  christos 		return (error);
    261  1.22       dsl 	compat_12_stat_conv(&sb, &osb);
    262   1.3   mycroft 	error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
    263   1.2  christos 	return (error);
    264   1.2  christos }
    265   1.2  christos 
    266   1.2  christos 
    267   1.2  christos /*
    268   1.2  christos  * Get file status; this version does not follow links.
    269   1.2  christos  */
    270   1.2  christos /* ARGSUSED */
    271   1.2  christos int
    272  1.24       dsl compat_12_sys_lstat(struct lwp *l, const struct compat_12_sys_lstat_args *uap, register_t *retval)
    273   1.2  christos {
    274  1.24       dsl 	/* {
    275   1.3   mycroft 		syscallarg(const char *) path;
    276   1.2  christos 		syscallarg(struct stat12 *) ub;
    277  1.24       dsl 	} */
    278   1.2  christos 	struct stat sb;
    279   1.2  christos 	struct stat12 osb;
    280   1.2  christos 	int error;
    281   1.2  christos 
    282  1.25        ad 	error = do_sys_stat(SCARG(uap, path), NOFOLLOW, &sb);
    283   1.2  christos 	if (error)
    284   1.2  christos 		return (error);
    285  1.22       dsl 	compat_12_stat_conv(&sb, &osb);
    286   1.2  christos 	error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
    287   1.2  christos 	return (error);
    288   1.2  christos }
    289   1.2  christos 
    290   1.2  christos /*
    291   1.2  christos  * Return status information about a file descriptor.
    292   1.2  christos  */
    293   1.2  christos /* ARGSUSED */
    294   1.2  christos int
    295  1.24       dsl compat_12_sys_fstat(struct lwp *l, const struct compat_12_sys_fstat_args *uap, register_t *retval)
    296   1.2  christos {
    297  1.24       dsl 	/* {
    298   1.2  christos 		syscallarg(int) fd;
    299   1.2  christos 		syscallarg(struct stat12 *) sb;
    300  1.24       dsl 	} */
    301   1.2  christos 	struct stat ub;
    302   1.2  christos 	struct stat12 oub;
    303   1.2  christos 	int error;
    304   1.2  christos 
    305  1.28     njoly 	error = do_sys_fstat(SCARG(uap, fd), &ub);
    306   1.3   mycroft 	if (error == 0) {
    307  1.22       dsl 		compat_12_stat_conv(&ub, &oub);
    308   1.3   mycroft 		error = copyout(&oub, SCARG(uap, sb), sizeof (oub));
    309   1.3   mycroft 	}
    310   1.2  christos 	return (error);
    311   1.1      fvdl }
    312