Home | History | Annotate | Line # | Download | only in common
vfs_syscalls_12.c revision 1.22
      1 /*	$NetBSD: vfs_syscalls_12.c,v 1.22 2007/04/30 14:05:47 dsl Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  * (c) UNIX System Laboratories, Inc.
      7  * All or some portions of this file are derived from material licensed
      8  * to the University of California by American Telephone and Telegraph
      9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  * the permission of UNIX System Laboratories, Inc.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	From: @(#)vfs_syscalls.c	8.28 (Berkeley) 12/10/94
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls_12.c,v 1.22 2007/04/30 14:05:47 dsl Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/namei.h>
     45 #include <sys/filedesc.h>
     46 #include <sys/kernel.h>
     47 #include <sys/file.h>
     48 #include <sys/stat.h>
     49 #include <sys/socketvar.h>
     50 #include <sys/vnode.h>
     51 #include <sys/mount.h>
     52 #include <sys/proc.h>
     53 #include <sys/uio.h>
     54 #include <sys/dirent.h>
     55 #include <sys/vfs_syscalls.h>
     56 
     57 #include <sys/syscallargs.h>
     58 
     59 #include <compat/sys/stat.h>
     60 #include <compat/common/compat_file.h>
     61 
     62 /*
     63  * Convert from a new to an old stat structure.
     64  */
     65 void
     66 compat_12_stat_conv(const struct stat *st, struct stat12 *ost)
     67 {
     68 
     69 	ost->st_dev = st->st_dev;
     70 	ost->st_ino = st->st_ino;
     71 	ost->st_mode = st->st_mode & 0xffff;
     72 	if (st->st_nlink >= (1 << 15))
     73 		ost->st_nlink = (1 << 15) - 1;
     74 	else
     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_size = st->st_size;
     83 	ost->st_blocks = st->st_blocks;
     84 	ost->st_blksize = st->st_blksize;
     85 	ost->st_flags = st->st_flags;
     86 	ost->st_gen = st->st_gen;
     87 }
     88 
     89 /*
     90  * Read a block of directory entries in a file system independent format.
     91  */
     92 int
     93 compat_12_sys_getdirentries(struct lwp *l, void *v, register_t *retval)
     94 {
     95 	struct compat_12_sys_getdirentries_args /* {
     96 		syscallarg(int) fd;
     97 		syscallarg(char *) buf;
     98 		syscallarg(u_int) count;
     99 		syscallarg(long *) basep;
    100 	} */ *uap = v;
    101 	struct proc *p = l->l_proc;
    102 	struct file *fp;
    103 	int error, done;
    104 	long loff;
    105 
    106 	/* getvnode() will use the descriptor for us */
    107 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    108 		return error;
    109 	if ((fp->f_flag & FREAD) == 0) {
    110 		error = EBADF;
    111 		goto out;
    112 	}
    113 
    114 	loff = fp->f_offset;
    115 
    116 	error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
    117 			SCARG(uap, count), &done, l, 0, 0);
    118 
    119 	error = copyout(&loff, SCARG(uap, basep), sizeof(long));
    120 	*retval = done;
    121  out:
    122 	FILE_UNUSE(fp, l);
    123 	return error;
    124 }
    125 
    126 /*
    127  * Get file status; this version follows links.
    128  */
    129 /* ARGSUSED */
    130 int
    131 compat_12_sys_stat(struct lwp *l, void *v, register_t *retval)
    132 {
    133 	struct compat_12_sys_stat_args /* {
    134 		syscallarg(const char *) path;
    135 		syscallarg(struct stat12 *) ub;
    136 	} */ *uap = v;
    137 	struct stat sb;
    138 	struct stat12 osb;
    139 	int error;
    140 
    141 	error = do_sys_stat(l, SCARG(uap, path), FOLLOW, &sb);
    142 	if (error)
    143 		return (error);
    144 	compat_12_stat_conv(&sb, &osb);
    145 	error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
    146 	return (error);
    147 }
    148 
    149 
    150 /*
    151  * Get file status; this version does not follow links.
    152  */
    153 /* ARGSUSED */
    154 int
    155 compat_12_sys_lstat(struct lwp *l, void *v, register_t *retval)
    156 {
    157 	struct compat_12_sys_lstat_args /* {
    158 		syscallarg(const char *) path;
    159 		syscallarg(struct stat12 *) ub;
    160 	} */ *uap = v;
    161 	struct stat sb;
    162 	struct stat12 osb;
    163 	int error;
    164 
    165 	error = do_sys_stat(l, SCARG(uap, path), NOFOLLOW, &sb);
    166 	if (error)
    167 		return (error);
    168 	compat_12_stat_conv(&sb, &osb);
    169 	error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
    170 	return (error);
    171 }
    172 
    173 /*
    174  * Return status information about a file descriptor.
    175  */
    176 /* ARGSUSED */
    177 int
    178 compat_12_sys_fstat(struct lwp *l, void *v, register_t *retval)
    179 {
    180 	struct compat_12_sys_fstat_args /* {
    181 		syscallarg(int) fd;
    182 		syscallarg(struct stat12 *) sb;
    183 	} */ *uap = v;
    184 	struct proc *p = l->l_proc;
    185 	int fd = SCARG(uap, fd);
    186 	struct filedesc *fdp = p->p_fd;
    187 	struct file *fp;
    188 	struct stat ub;
    189 	struct stat12 oub;
    190 	int error;
    191 
    192 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    193 		return (EBADF);
    194 
    195 	FILE_USE(fp);
    196 	error = (*fp->f_ops->fo_stat)(fp, &ub, l);
    197 	FILE_UNUSE(fp, l);
    198 
    199 	if (error == 0) {
    200 		compat_12_stat_conv(&ub, &oub);
    201 		error = copyout(&oub, SCARG(uap, sb), sizeof (oub));
    202 	}
    203 	return (error);
    204 }
    205