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