1 /* $NetBSD: vfs_syscalls_12.c,v 1.29 2011/01/19 10:21:16 tsutsui 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.29 2011/01/19 10:21:16 tsutsui 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/proc.h> 52 #include <sys/uio.h> 53 #include <sys/dirent.h> 54 #include <sys/vfs_syscalls.h> 55 56 #include <sys/syscallargs.h> 57 58 #include <compat/sys/stat.h> 59 60 /* 61 * Convert from a new to an old stat structure. 62 */ 63 void 64 compat_12_stat_conv(const struct stat *st, struct stat12 *ost) 65 { 66 67 ost->st_dev = st->st_dev; 68 ost->st_ino = st->st_ino; 69 ost->st_mode = st->st_mode & 0xffff; 70 if (st->st_nlink >= (1 << 15)) 71 ost->st_nlink = (1 << 15) - 1; 72 else 73 ost->st_nlink = st->st_nlink; 74 ost->st_uid = st->st_uid; 75 ost->st_gid = st->st_gid; 76 ost->st_rdev = st->st_rdev; 77 timespec_to_timespec50(&st->st_atimespec, &ost->st_atimespec); 78 timespec_to_timespec50(&st->st_mtimespec, &ost->st_mtimespec); 79 timespec_to_timespec50(&st->st_ctimespec, &ost->st_ctimespec); 80 ost->st_size = st->st_size; 81 ost->st_blocks = st->st_blocks; 82 ost->st_blksize = st->st_blksize; 83 ost->st_flags = st->st_flags; 84 ost->st_gen = st->st_gen; 85 } 86 87 /* 88 * Read a block of directory entries in a file system independent format. 89 */ 90 int 91 compat_12_sys_getdirentries(struct lwp *l, const struct compat_12_sys_getdirentries_args *uap, register_t *retval) 92 { 93 /* { 94 syscallarg(int) fd; 95 syscallarg(char *) buf; 96 syscallarg(u_int) count; 97 syscallarg(long *) basep; 98 } */ 99 struct file *fp; 100 int error, done; 101 long loff; 102 103 /* fd_getvnode() will use the descriptor for us */ 104 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0) 105 return error; 106 if ((fp->f_flag & FREAD) == 0) { 107 error = EBADF; 108 goto out; 109 } 110 111 loff = fp->f_offset; 112 113 error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE, 114 SCARG(uap, count), &done, l, 0, 0); 115 116 error = copyout(&loff, SCARG(uap, basep), sizeof(long)); 117 *retval = done; 118 out: 119 fd_putfile(SCARG(uap, fd)); 120 return error; 121 } 122 123 /* 124 * Get file status; this version follows links. 125 */ 126 /* ARGSUSED */ 127 int 128 compat_12_sys_stat(struct lwp *l, const struct compat_12_sys_stat_args *uap, register_t *retval) 129 { 130 /* { 131 syscallarg(const char *) path; 132 syscallarg(struct stat12 *) ub; 133 } */ 134 struct stat sb; 135 struct stat12 osb; 136 int error; 137 138 error = do_sys_stat(SCARG(uap, path), FOLLOW, &sb); 139 if (error) 140 return (error); 141 compat_12_stat_conv(&sb, &osb); 142 error = copyout(&osb, SCARG(uap, ub), sizeof (osb)); 143 return (error); 144 } 145 146 147 /* 148 * Get file status; this version does not follow links. 149 */ 150 /* ARGSUSED */ 151 int 152 compat_12_sys_lstat(struct lwp *l, const struct compat_12_sys_lstat_args *uap, register_t *retval) 153 { 154 /* { 155 syscallarg(const char *) path; 156 syscallarg(struct stat12 *) ub; 157 } */ 158 struct stat sb; 159 struct stat12 osb; 160 int error; 161 162 error = do_sys_stat(SCARG(uap, path), NOFOLLOW, &sb); 163 if (error) 164 return (error); 165 compat_12_stat_conv(&sb, &osb); 166 error = copyout(&osb, SCARG(uap, ub), sizeof (osb)); 167 return (error); 168 } 169 170 /* 171 * Return status information about a file descriptor. 172 */ 173 /* ARGSUSED */ 174 int 175 compat_12_sys_fstat(struct lwp *l, const struct compat_12_sys_fstat_args *uap, register_t *retval) 176 { 177 /* { 178 syscallarg(int) fd; 179 syscallarg(struct stat12 *) sb; 180 } */ 181 struct stat ub; 182 struct stat12 oub; 183 int error; 184 185 error = do_sys_fstat(SCARG(uap, fd), &ub); 186 if (error == 0) { 187 compat_12_stat_conv(&ub, &oub); 188 error = copyout(&oub, SCARG(uap, sb), sizeof (oub)); 189 } 190 return (error); 191 } 192