Home | History | Annotate | Line # | Download | only in common
vfs_syscalls_12.c revision 1.37
      1  1.37  pgoyette /*	$NetBSD: vfs_syscalls_12.c,v 1.37 2019/01/27 02:08:39 pgoyette 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.37  pgoyette __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls_12.c,v 1.37 2019/01/27 02:08:39 pgoyette Exp $");
     41  1.37  pgoyette 
     42  1.37  pgoyette #if defined(_KERNEL_OPT)
     43  1.37  pgoyette #include "opt_compat_netbsd.h"
     44  1.37  pgoyette #endif
     45   1.1      fvdl 
     46   1.1      fvdl #include <sys/param.h>
     47   1.1      fvdl #include <sys/systm.h>
     48   1.1      fvdl #include <sys/namei.h>
     49   1.1      fvdl #include <sys/filedesc.h>
     50   1.1      fvdl #include <sys/kernel.h>
     51   1.1      fvdl #include <sys/file.h>
     52   1.1      fvdl #include <sys/stat.h>
     53   1.2  christos #include <sys/socketvar.h>
     54   1.1      fvdl #include <sys/vnode.h>
     55   1.1      fvdl #include <sys/proc.h>
     56   1.1      fvdl #include <sys/uio.h>
     57   1.1      fvdl #include <sys/dirent.h>
     58  1.21       dsl #include <sys/vfs_syscalls.h>
     59   1.1      fvdl 
     60  1.37  pgoyette #include <sys/syscall.h>
     61  1.37  pgoyette #include <sys/syscallvar.h>
     62   1.1      fvdl #include <sys/syscallargs.h>
     63   1.1      fvdl 
     64  1.16  christos #include <compat/sys/stat.h>
     65  1.30  christos #include <compat/sys/dirent.h>
     66   1.2  christos 
     67  1.37  pgoyette #include <compat/common/compat_mod.h>
     68  1.37  pgoyette 
     69  1.37  pgoyette static const struct syscall_package vfs_syscalls_12_syscalls[] = {
     70  1.37  pgoyette 	{ SYS_compat_12_fstat12, 0, (sy_call_t *)compat_12_sys_fstat },
     71  1.37  pgoyette 	{ SYS_compat_12_getdirentries, 0,
     72  1.37  pgoyette 	    (sy_call_t *)compat_12_sys_getdirentries },
     73  1.37  pgoyette 	{ SYS_compat_12_lstat12, 0, (sy_call_t *)compat_12_sys_lstat },
     74  1.37  pgoyette 	{ SYS_compat_12_stat12, 0, (sy_call_t *)compat_12_sys_stat },
     75  1.37  pgoyette 	{ 0, 0, NULL }
     76  1.37  pgoyette };
     77  1.37  pgoyette 
     78   1.2  christos /*
     79   1.4  wrstuden  * Convert from a new to an old stat structure.
     80   1.2  christos  */
     81  1.22       dsl void
     82  1.22       dsl compat_12_stat_conv(const struct stat *st, struct stat12 *ost)
     83   1.2  christos {
     84   1.2  christos 
     85   1.2  christos 	ost->st_dev = st->st_dev;
     86   1.2  christos 	ost->st_ino = st->st_ino;
     87   1.4  wrstuden 	ost->st_mode = st->st_mode & 0xffff;
     88   1.2  christos 	if (st->st_nlink >= (1 << 15))
     89   1.2  christos 		ost->st_nlink = (1 << 15) - 1;
     90   1.2  christos 	else
     91   1.2  christos 		ost->st_nlink = st->st_nlink;
     92   1.2  christos 	ost->st_uid = st->st_uid;
     93   1.2  christos 	ost->st_gid = st->st_gid;
     94   1.2  christos 	ost->st_rdev = st->st_rdev;
     95  1.27  christos 	timespec_to_timespec50(&st->st_atimespec, &ost->st_atimespec);
     96  1.27  christos 	timespec_to_timespec50(&st->st_mtimespec, &ost->st_mtimespec);
     97  1.27  christos 	timespec_to_timespec50(&st->st_ctimespec, &ost->st_ctimespec);
     98   1.2  christos 	ost->st_size = st->st_size;
     99   1.3   mycroft 	ost->st_blocks = st->st_blocks;
    100   1.2  christos 	ost->st_blksize = st->st_blksize;
    101   1.2  christos 	ost->st_flags = st->st_flags;
    102   1.2  christos 	ost->st_gen = st->st_gen;
    103   1.2  christos }
    104   1.3   mycroft 
    105   1.1      fvdl /*
    106   1.1      fvdl  * Read a block of directory entries in a file system independent format.
    107   1.1      fvdl  */
    108   1.1      fvdl int
    109  1.37  pgoyette compat_12_sys_getdirentries(struct lwp *l,
    110  1.37  pgoyette     const struct compat_12_sys_getdirentries_args *uap, register_t *retval)
    111   1.1      fvdl {
    112  1.24       dsl 	/* {
    113   1.1      fvdl 		syscallarg(int) fd;
    114   1.1      fvdl 		syscallarg(char *) buf;
    115   1.1      fvdl 		syscallarg(u_int) count;
    116   1.1      fvdl 		syscallarg(long *) basep;
    117  1.24       dsl 	} */
    118  1.30  christos 	struct dirent *bdp;
    119  1.30  christos 	struct vnode *vp;
    120  1.30  christos 	char *inp, *tbuf;		/* Current-format */
    121  1.30  christos 	int len, reclen;		/* Current-format */
    122  1.30  christos 	char *outp;			/* Dirent12-format */
    123  1.30  christos 	int resid, old_reclen = 0;	/* Dirent12-format */
    124   1.1      fvdl 	struct file *fp;
    125  1.30  christos 	struct uio auio;
    126  1.30  christos 	struct iovec aiov;
    127  1.30  christos 	struct dirent12 idb;
    128  1.30  christos 	off_t off;		/* true file offset */
    129  1.30  christos 	int buflen, error, eofflag, nbytes;
    130  1.30  christos 	struct vattr va;
    131  1.30  christos 	off_t *cookiebuf = NULL, *cookie;
    132  1.30  christos 	int ncookies;
    133   1.1      fvdl 	long loff;
    134  1.30  christos 
    135  1.26        ad 	/* fd_getvnode() will use the descriptor for us */
    136  1.26        ad 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
    137  1.30  christos 		return (error);
    138  1.30  christos 
    139   1.5   thorpej 	if ((fp->f_flag & FREAD) == 0) {
    140   1.5   thorpej 		error = EBADF;
    141  1.30  christos 		goto out1;
    142  1.30  christos 	}
    143  1.30  christos 
    144  1.31      matt 	vp = (struct vnode *)fp->f_vnode;
    145  1.30  christos 	if (vp->v_type != VDIR) {
    146  1.30  christos 		error = ENOTDIR;
    147  1.30  christos 		goto out1;
    148   1.5   thorpej 	}
    149   1.1      fvdl 
    150  1.30  christos 	vn_lock(vp, LK_SHARED | LK_RETRY);
    151  1.30  christos 	error = VOP_GETATTR(vp, &va, l->l_cred);
    152  1.30  christos 	VOP_UNLOCK(vp);
    153  1.30  christos 	if (error)
    154  1.30  christos 		goto out1;
    155  1.30  christos 
    156   1.1      fvdl 	loff = fp->f_offset;
    157  1.30  christos 	nbytes = SCARG(uap, count);
    158  1.36  riastrad 	buflen = uimin(MAXBSIZE, nbytes);
    159  1.30  christos 	if (buflen < va.va_blocksize)
    160  1.30  christos 		buflen = va.va_blocksize;
    161  1.30  christos 	tbuf = malloc(buflen, M_TEMP, M_WAITOK);
    162  1.30  christos 
    163  1.30  christos 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    164  1.30  christos 	off = fp->f_offset;
    165  1.30  christos again:
    166  1.30  christos 	aiov.iov_base = tbuf;
    167  1.30  christos 	aiov.iov_len = buflen;
    168  1.30  christos 	auio.uio_iov = &aiov;
    169  1.30  christos 	auio.uio_iovcnt = 1;
    170  1.30  christos 	auio.uio_rw = UIO_READ;
    171  1.30  christos 	auio.uio_resid = buflen;
    172  1.30  christos 	auio.uio_offset = off;
    173  1.30  christos 	UIO_SETUP_SYSSPACE(&auio);
    174  1.30  christos 	/*
    175  1.30  christos          * First we read into the malloc'ed buffer, then
    176  1.30  christos          * we massage it into user space, one record at a time.
    177  1.30  christos          */
    178  1.30  christos 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
    179  1.30  christos 	    &ncookies);
    180  1.30  christos 	if (error)
    181  1.30  christos 		goto out;
    182  1.30  christos 
    183  1.30  christos 	inp = tbuf;
    184  1.30  christos 	outp = SCARG(uap, buf);
    185  1.30  christos 	resid = nbytes;
    186  1.30  christos 	if ((len = buflen - auio.uio_resid) == 0)
    187  1.30  christos 		goto eof;
    188  1.30  christos 
    189  1.30  christos 	for (cookie = cookiebuf; len > 0; len -= reclen) {
    190  1.30  christos 		bdp = (struct dirent *)inp;
    191  1.30  christos 		reclen = bdp->d_reclen;
    192  1.34  riastrad 		if (reclen & 3) {
    193  1.34  riastrad 			error = EIO;
    194  1.34  riastrad 			goto out;
    195  1.34  riastrad 		}
    196  1.30  christos 		if (bdp->d_fileno == 0) {
    197  1.30  christos 			inp += reclen;	/* it is a hole; squish it out */
    198  1.30  christos 			if (cookie)
    199  1.30  christos 				off = *cookie++;
    200  1.30  christos 			else
    201  1.30  christos 				off += reclen;
    202  1.30  christos 			continue;
    203  1.30  christos 		}
    204  1.35  christos 		if (bdp->d_namlen >= sizeof(idb.d_name))
    205  1.35  christos 			idb.d_namlen = sizeof(idb.d_name) - 1;
    206  1.35  christos 		else
    207  1.35  christos 			idb.d_namlen = bdp->d_namlen;
    208  1.30  christos 		old_reclen = _DIRENT_RECLEN(&idb, bdp->d_namlen);
    209  1.30  christos 		if (reclen > len || resid < old_reclen) {
    210  1.30  christos 			/* entry too big for buffer, so just stop */
    211  1.30  christos 			outp++;
    212  1.30  christos 			break;
    213  1.30  christos 		}
    214  1.30  christos 		/*
    215  1.30  christos 		 * Massage in place to make a Dirent12-shaped dirent (otherwise
    216  1.30  christos 		 * we have to worry about touching user memory outside of
    217  1.30  christos 		 * the copyout() call).
    218  1.30  christos 		 */
    219  1.30  christos 		idb.d_fileno = (uint32_t)bdp->d_fileno;
    220  1.30  christos 		idb.d_reclen = (uint16_t)old_reclen;
    221  1.30  christos 		idb.d_type = (uint8_t)bdp->d_type;
    222  1.35  christos 		(void)memcpy(idb.d_name, bdp->d_name, idb.d_namlen);
    223  1.35  christos 		memset(idb.d_name + idb.d_namlen, 0,
    224  1.35  christos 		    idb.d_reclen - _DIRENT_NAMEOFF(&idb) - idb.d_namlen);
    225  1.30  christos 		if ((error = copyout(&idb, outp, old_reclen)))
    226  1.30  christos 			goto out;
    227  1.30  christos 		/* advance past this real entry */
    228  1.30  christos 		inp += reclen;
    229  1.30  christos 		if (cookie)
    230  1.30  christos 			off = *cookie++; /* each entry points to itself */
    231  1.30  christos 		else
    232  1.30  christos 			off += reclen;
    233  1.30  christos 		/* advance output past Dirent12-shaped entry */
    234  1.30  christos 		outp += old_reclen;
    235  1.30  christos 		resid -= old_reclen;
    236  1.30  christos 	}
    237   1.1      fvdl 
    238  1.30  christos 	/* if we squished out the whole block, try again */
    239  1.30  christos 	if (outp == SCARG(uap, buf)) {
    240  1.30  christos 		if (cookiebuf)
    241  1.30  christos 			free(cookiebuf, M_TEMP);
    242  1.30  christos 		cookiebuf = NULL;
    243  1.30  christos 		goto again;
    244  1.30  christos 	}
    245  1.30  christos 	fp->f_offset = off;	/* update the vnode offset */
    246   1.1      fvdl 
    247  1.30  christos eof:
    248  1.30  christos 	*retval = nbytes - resid;
    249  1.30  christos out:
    250  1.30  christos 	VOP_UNLOCK(vp);
    251  1.30  christos 	if (cookiebuf)
    252  1.30  christos 		free(cookiebuf, M_TEMP);
    253  1.30  christos 	free(tbuf, M_TEMP);
    254  1.30  christos out1:
    255  1.25        ad 	fd_putfile(SCARG(uap, fd));
    256  1.30  christos 	if (error)
    257  1.30  christos 		return error;
    258  1.30  christos 	return copyout(&loff, SCARG(uap, basep), sizeof(long));
    259   1.2  christos }
    260   1.2  christos 
    261   1.2  christos /*
    262   1.2  christos  * Get file status; this version follows links.
    263   1.2  christos  */
    264   1.2  christos /* ARGSUSED */
    265   1.2  christos int
    266  1.37  pgoyette compat_12_sys_stat(struct lwp *l, const struct compat_12_sys_stat_args *uap,
    267  1.37  pgoyette     register_t *retval)
    268   1.2  christos {
    269  1.24       dsl 	/* {
    270   1.3   mycroft 		syscallarg(const char *) path;
    271   1.2  christos 		syscallarg(struct stat12 *) ub;
    272  1.24       dsl 	} */
    273   1.2  christos 	struct stat sb;
    274   1.2  christos 	struct stat12 osb;
    275   1.2  christos 	int error;
    276   1.2  christos 
    277  1.25        ad 	error = do_sys_stat(SCARG(uap, path), FOLLOW, &sb);
    278   1.2  christos 	if (error)
    279   1.2  christos 		return (error);
    280  1.22       dsl 	compat_12_stat_conv(&sb, &osb);
    281   1.3   mycroft 	error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
    282   1.2  christos 	return (error);
    283   1.2  christos }
    284   1.2  christos 
    285   1.2  christos 
    286   1.2  christos /*
    287   1.2  christos  * Get file status; this version does not follow links.
    288   1.2  christos  */
    289   1.2  christos /* ARGSUSED */
    290   1.2  christos int
    291  1.37  pgoyette compat_12_sys_lstat(struct lwp *l, const struct compat_12_sys_lstat_args *uap,
    292  1.37  pgoyette     register_t *retval)
    293   1.2  christos {
    294  1.24       dsl 	/* {
    295   1.3   mycroft 		syscallarg(const char *) path;
    296   1.2  christos 		syscallarg(struct stat12 *) ub;
    297  1.24       dsl 	} */
    298   1.2  christos 	struct stat sb;
    299   1.2  christos 	struct stat12 osb;
    300   1.2  christos 	int error;
    301   1.2  christos 
    302  1.25        ad 	error = do_sys_stat(SCARG(uap, path), NOFOLLOW, &sb);
    303   1.2  christos 	if (error)
    304   1.2  christos 		return (error);
    305  1.22       dsl 	compat_12_stat_conv(&sb, &osb);
    306   1.2  christos 	error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
    307   1.2  christos 	return (error);
    308   1.2  christos }
    309   1.2  christos 
    310   1.2  christos /*
    311   1.2  christos  * Return status information about a file descriptor.
    312   1.2  christos  */
    313   1.2  christos /* ARGSUSED */
    314   1.2  christos int
    315  1.37  pgoyette compat_12_sys_fstat(struct lwp *l, const struct compat_12_sys_fstat_args *uap,
    316  1.37  pgoyette     register_t *retval)
    317   1.2  christos {
    318  1.24       dsl 	/* {
    319   1.2  christos 		syscallarg(int) fd;
    320   1.2  christos 		syscallarg(struct stat12 *) sb;
    321  1.24       dsl 	} */
    322   1.2  christos 	struct stat ub;
    323   1.2  christos 	struct stat12 oub;
    324   1.2  christos 	int error;
    325   1.2  christos 
    326  1.28     njoly 	error = do_sys_fstat(SCARG(uap, fd), &ub);
    327   1.3   mycroft 	if (error == 0) {
    328  1.22       dsl 		compat_12_stat_conv(&ub, &oub);
    329   1.3   mycroft 		error = copyout(&oub, SCARG(uap, sb), sizeof (oub));
    330   1.3   mycroft 	}
    331   1.2  christos 	return (error);
    332   1.1      fvdl }
    333  1.37  pgoyette 
    334  1.37  pgoyette int
    335  1.37  pgoyette vfs_syscalls_12_init(void)
    336  1.37  pgoyette {
    337  1.37  pgoyette 
    338  1.37  pgoyette 	return syscall_establish(NULL, vfs_syscalls_12_syscalls);
    339  1.37  pgoyette }
    340  1.37  pgoyette 
    341  1.37  pgoyette int
    342  1.37  pgoyette vfs_syscalls_12_fini(void)
    343  1.37  pgoyette {
    344  1.37  pgoyette 
    345  1.37  pgoyette 	return syscall_disestablish(NULL, vfs_syscalls_12_syscalls);
    346  1.37  pgoyette }
    347