1 1.45 riastrad /* $NetBSD: vfs_syscalls_30.c,v 1.45 2022/03/12 20:46:03 riastradh Exp $ */ 2 1.1 christos 3 1.1 christos /*- 4 1.26 ad * Copyright (c) 2005, 2008 The NetBSD Foundation, Inc. 5 1.1 christos * All rights reserved. 6 1.1 christos * 7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation 8 1.1 christos * by Christos Zoulas. 9 1.1 christos * 10 1.1 christos * Redistribution and use in source and binary forms, with or without 11 1.1 christos * modification, are permitted provided that the following conditions 12 1.1 christos * are met: 13 1.1 christos * 1. Redistributions of source code must retain the above copyright 14 1.1 christos * notice, this list of conditions and the following disclaimer. 15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 christos * notice, this list of conditions and the following disclaimer in the 17 1.1 christos * documentation and/or other materials provided with the distribution. 18 1.1 christos * 19 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 christos * POSSIBILITY OF SUCH DAMAGE. 30 1.1 christos */ 31 1.1 christos #include <sys/cdefs.h> 32 1.45 riastrad __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls_30.c,v 1.45 2022/03/12 20:46:03 riastradh Exp $"); 33 1.38 pgoyette 34 1.38 pgoyette #if defined(_KERNEL_OPT) 35 1.38 pgoyette #include "opt_compat_netbsd.h" 36 1.38 pgoyette #endif 37 1.1 christos 38 1.1 christos #include <sys/param.h> 39 1.1 christos #include <sys/systm.h> 40 1.1 christos #include <sys/namei.h> 41 1.1 christos #include <sys/filedesc.h> 42 1.1 christos #include <sys/kernel.h> 43 1.1 christos #include <sys/file.h> 44 1.1 christos #include <sys/stat.h> 45 1.1 christos #include <sys/socketvar.h> 46 1.1 christos #include <sys/vnode.h> 47 1.1 christos #include <sys/mount.h> 48 1.1 christos #include <sys/proc.h> 49 1.1 christos #include <sys/uio.h> 50 1.1 christos #include <sys/dirent.h> 51 1.1 christos #include <sys/malloc.h> 52 1.9 elad #include <sys/kauth.h> 53 1.21 dsl #include <sys/vfs_syscalls.h> 54 1.38 pgoyette #include <sys/syscall.h> 55 1.38 pgoyette #include <sys/syscallvar.h> 56 1.1 christos #include <sys/syscallargs.h> 57 1.1 christos 58 1.38 pgoyette #include <compat/common/compat_mod.h> 59 1.1 christos #include <compat/common/compat_util.h> 60 1.39 christos 61 1.5 christos #include <compat/sys/stat.h> 62 1.5 christos #include <compat/sys/dirent.h> 63 1.10 martin #include <compat/sys/mount.h> 64 1.39 christos #include <compat/sys/statvfs.h> 65 1.1 christos 66 1.38 pgoyette static const struct syscall_package vfs_syscalls_30_syscalls[] = { 67 1.38 pgoyette { SYS_compat_30___fhstat30, 0, (sy_call_t *)compat_30_sys___fhstat30 }, 68 1.38 pgoyette { SYS_compat_30___fstat13, 0, (sy_call_t *)compat_30_sys___fstat13 }, 69 1.38 pgoyette { SYS_compat_30___lstat13, 0, (sy_call_t *)compat_30_sys___lstat13 }, 70 1.38 pgoyette { SYS_compat_30___stat13, 0, (sy_call_t *)compat_30_sys___stat13 }, 71 1.38 pgoyette { SYS_compat_30_fhopen, 0, (sy_call_t *)compat_30_sys_fhopen }, 72 1.38 pgoyette { SYS_compat_30_fhstat, 0, (sy_call_t *)compat_30_sys_fhstat }, 73 1.38 pgoyette { SYS_compat_30_fhstatvfs1, 0, (sy_call_t *)compat_30_sys_fhstatvfs1 }, 74 1.38 pgoyette { SYS_compat_30_getdents, 0, (sy_call_t *)compat_30_sys_getdents }, 75 1.38 pgoyette { SYS_compat_30_getfh, 0, (sy_call_t *)compat_30_sys_getfh }, 76 1.38 pgoyette { 0,0, NULL } 77 1.38 pgoyette }; 78 1.38 pgoyette 79 1.1 christos /* 80 1.1 christos * Convert from a new to an old stat structure. 81 1.1 christos */ 82 1.1 christos static void 83 1.1 christos cvtstat(struct stat13 *ost, const struct stat *st) 84 1.1 christos { 85 1.1 christos 86 1.42 christos /* Handle any padding. */ 87 1.42 christos memset(ost, 0, sizeof(*ost)); 88 1.1 christos ost->st_dev = st->st_dev; 89 1.1 christos ost->st_ino = (uint32_t)st->st_ino; 90 1.1 christos ost->st_mode = st->st_mode; 91 1.1 christos ost->st_nlink = st->st_nlink; 92 1.1 christos ost->st_uid = st->st_uid; 93 1.1 christos ost->st_gid = st->st_gid; 94 1.1 christos ost->st_rdev = st->st_rdev; 95 1.29 christos timespec_to_timespec50(&st->st_atimespec, &ost->st_atimespec); 96 1.29 christos timespec_to_timespec50(&st->st_mtimespec, &ost->st_mtimespec); 97 1.29 christos timespec_to_timespec50(&st->st_ctimespec, &ost->st_ctimespec); 98 1.29 christos timespec_to_timespec50(&st->st_birthtimespec, &ost->st_birthtimespec); 99 1.1 christos ost->st_size = st->st_size; 100 1.1 christos ost->st_blocks = st->st_blocks; 101 1.1 christos ost->st_blksize = st->st_blksize; 102 1.1 christos ost->st_flags = st->st_flags; 103 1.1 christos ost->st_gen = st->st_gen; 104 1.1 christos } 105 1.1 christos 106 1.1 christos /* 107 1.1 christos * Get file status; this version follows links. 108 1.1 christos */ 109 1.1 christos /* ARGSUSED */ 110 1.1 christos int 111 1.38 pgoyette compat_30_sys___stat13(struct lwp *l, 112 1.38 pgoyette const struct compat_30_sys___stat13_args *uap, register_t *retval) 113 1.1 christos { 114 1.25 dsl /* { 115 1.1 christos syscallarg(const char *) path; 116 1.1 christos syscallarg(struct stat13 *) ub; 117 1.25 dsl } */ 118 1.1 christos struct stat sb; 119 1.1 christos struct stat13 osb; 120 1.1 christos int error; 121 1.1 christos 122 1.26 ad error = do_sys_stat(SCARG(uap, path), FOLLOW, &sb); 123 1.1 christos if (error) 124 1.1 christos return error; 125 1.1 christos cvtstat(&osb, &sb); 126 1.42 christos return copyout(&osb, SCARG(uap, ub), sizeof(osb)); 127 1.1 christos } 128 1.1 christos 129 1.1 christos 130 1.1 christos /* 131 1.1 christos * Get file status; this version does not follow links. 132 1.1 christos */ 133 1.1 christos /* ARGSUSED */ 134 1.1 christos int 135 1.38 pgoyette compat_30_sys___lstat13(struct lwp *l, 136 1.38 pgoyette const struct compat_30_sys___lstat13_args *uap, register_t *retval) 137 1.1 christos { 138 1.25 dsl /* { 139 1.1 christos syscallarg(const char *) path; 140 1.1 christos syscallarg(struct stat13 *) ub; 141 1.25 dsl } */ 142 1.1 christos struct stat sb; 143 1.1 christos struct stat13 osb; 144 1.1 christos int error; 145 1.1 christos 146 1.26 ad error = do_sys_stat(SCARG(uap, path), NOFOLLOW, &sb); 147 1.1 christos if (error) 148 1.1 christos return error; 149 1.1 christos cvtstat(&osb, &sb); 150 1.42 christos return copyout(&osb, SCARG(uap, ub), sizeof(osb)); 151 1.1 christos } 152 1.1 christos 153 1.8 christos /* ARGSUSED */ 154 1.8 christos int 155 1.38 pgoyette compat_30_sys_fhstat(struct lwp *l, 156 1.38 pgoyette const struct compat_30_sys_fhstat_args *uap, register_t *retval) 157 1.8 christos { 158 1.25 dsl /* { 159 1.10 martin syscallarg(const struct compat_30_fhandle *) fhp; 160 1.8 christos syscallarg(struct stat13 *) sb; 161 1.25 dsl } */ 162 1.8 christos struct stat sb; 163 1.8 christos struct stat13 osb; 164 1.8 christos int error; 165 1.8 christos 166 1.42 christos error = do_fhstat(l, SCARG(uap, fhp), sizeof(*SCARG(uap, fhp)), &sb); 167 1.8 christos if (error) 168 1.42 christos return error; 169 1.8 christos cvtstat(&osb, &sb); 170 1.42 christos return copyout(&osb, SCARG(uap, sb), sizeof(osb)); 171 1.8 christos } 172 1.8 christos 173 1.1 christos /* 174 1.1 christos * Return status information about a file descriptor. 175 1.1 christos */ 176 1.1 christos /* ARGSUSED */ 177 1.1 christos int 178 1.38 pgoyette compat_30_sys___fstat13(struct lwp *l, 179 1.38 pgoyette const struct compat_30_sys___fstat13_args *uap, register_t *retval) 180 1.1 christos { 181 1.25 dsl /* { 182 1.1 christos syscallarg(int) fd; 183 1.1 christos syscallarg(struct stat13 *) sb; 184 1.25 dsl } */ 185 1.1 christos struct stat sb; 186 1.1 christos struct stat13 osb; 187 1.1 christos int error; 188 1.1 christos 189 1.30 njoly error = do_sys_fstat(SCARG(uap, fd), &sb); 190 1.1 christos if (error) 191 1.1 christos return error; 192 1.1 christos cvtstat(&osb, &sb); 193 1.42 christos return copyout(&osb, SCARG(uap, sb), sizeof(osb)); 194 1.1 christos } 195 1.1 christos 196 1.1 christos /* 197 1.1 christos * Read a block of directory entries in a file system independent format. 198 1.1 christos */ 199 1.1 christos int 200 1.38 pgoyette compat_30_sys_getdents(struct lwp *l, 201 1.38 pgoyette const struct compat_30_sys_getdents_args *uap, register_t *retval) 202 1.1 christos { 203 1.25 dsl /* { 204 1.1 christos syscallarg(int) fd; 205 1.1 christos syscallarg(char *) buf; 206 1.1 christos syscallarg(size_t) count; 207 1.25 dsl } */ 208 1.1 christos struct dirent *bdp; 209 1.1 christos struct vnode *vp; 210 1.20 christos char *inp, *tbuf; /* BSD-format */ 211 1.1 christos int len, reclen; /* BSD-format */ 212 1.20 christos char *outp; /* NetBSD-3.0-format */ 213 1.1 christos int resid; 214 1.1 christos struct file *fp; 215 1.1 christos struct uio auio; 216 1.1 christos struct iovec aiov; 217 1.1 christos struct dirent12 idb; 218 1.1 christos off_t off; /* true file offset */ 219 1.1 christos int buflen, error, eofflag; 220 1.1 christos off_t *cookiebuf = NULL, *cookie; 221 1.1 christos int ncookies; 222 1.44 riastrad bool any = false; 223 1.1 christos 224 1.28 ad /* fd_getvnode() will use the descriptor for us */ 225 1.28 ad if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0) 226 1.1 christos return error; 227 1.1 christos 228 1.1 christos if ((fp->f_flag & FREAD) == 0) { 229 1.1 christos error = EBADF; 230 1.1 christos goto out1; 231 1.1 christos } 232 1.1 christos 233 1.34 matt vp = fp->f_vnode; 234 1.1 christos if (vp->v_type != VDIR) { 235 1.1 christos error = EINVAL; 236 1.1 christos goto out1; 237 1.1 christos } 238 1.1 christos 239 1.37 riastrad buflen = uimin(MAXBSIZE, SCARG(uap, count)); 240 1.1 christos tbuf = malloc(buflen, M_TEMP, M_WAITOK); 241 1.1 christos vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 242 1.1 christos off = fp->f_offset; 243 1.1 christos again: 244 1.1 christos aiov.iov_base = tbuf; 245 1.1 christos aiov.iov_len = buflen; 246 1.1 christos auio.uio_iov = &aiov; 247 1.1 christos auio.uio_iovcnt = 1; 248 1.1 christos auio.uio_rw = UIO_READ; 249 1.1 christos auio.uio_resid = buflen; 250 1.1 christos auio.uio_offset = off; 251 1.7 yamt UIO_SETUP_SYSSPACE(&auio); 252 1.1 christos /* 253 1.1 christos * First we read into the malloc'ed buffer, then 254 1.1 christos * we massage it into user space, one record at a time. 255 1.1 christos */ 256 1.1 christos error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf, 257 1.1 christos &ncookies); 258 1.1 christos if (error) 259 1.1 christos goto out; 260 1.1 christos 261 1.1 christos inp = tbuf; 262 1.1 christos outp = SCARG(uap, buf); 263 1.1 christos resid = SCARG(uap, count); 264 1.1 christos if ((len = buflen - auio.uio_resid) == 0) 265 1.1 christos goto eof; 266 1.1 christos 267 1.1 christos for (cookie = cookiebuf; len > 0; len -= reclen) { 268 1.1 christos bdp = (struct dirent *)inp; 269 1.1 christos reclen = bdp->d_reclen; 270 1.2 christos if (reclen & _DIRENT_ALIGN(bdp)) 271 1.42 christos panic("%s: bad reclen %d", __func__, reclen); 272 1.1 christos if (cookie) 273 1.1 christos off = *cookie++; /* each entry points to the next */ 274 1.1 christos else 275 1.1 christos off += reclen; 276 1.1 christos if ((off >> 32) != 0) { 277 1.4 yamt compat_offseterr(vp, "netbsd30_getdents"); 278 1.1 christos error = EINVAL; 279 1.1 christos goto out; 280 1.1 christos } 281 1.43 riastrad memset(&idb, 0, sizeof(idb)); 282 1.1 christos if (bdp->d_namlen >= sizeof(idb.d_name)) 283 1.1 christos idb.d_namlen = sizeof(idb.d_name) - 1; 284 1.1 christos else 285 1.1 christos idb.d_namlen = bdp->d_namlen; 286 1.1 christos idb.d_reclen = _DIRENT_SIZE(&idb); 287 1.1 christos if (reclen > len || resid < idb.d_reclen) { 288 1.1 christos /* entry too big for buffer, so just stop */ 289 1.44 riastrad any = true; 290 1.1 christos break; 291 1.1 christos } 292 1.1 christos /* 293 1.4 yamt * Massage in place to make a NetBSD-3.0-shaped dirent 294 1.1 christos * (otherwise we have to worry about touching user memory 295 1.1 christos * outside of the copyout() call). 296 1.1 christos */ 297 1.1 christos idb.d_fileno = (u_int32_t)bdp->d_fileno; 298 1.1 christos idb.d_type = bdp->d_type; 299 1.1 christos (void)memcpy(idb.d_name, bdp->d_name, idb.d_namlen); 300 1.3 yamt memset(idb.d_name + idb.d_namlen, 0, 301 1.3 yamt idb.d_reclen - _DIRENT_NAMEOFF(&idb) - idb.d_namlen); 302 1.1 christos if ((error = copyout(&idb, outp, idb.d_reclen)) != 0) 303 1.1 christos goto out; 304 1.1 christos /* advance past this real entry */ 305 1.1 christos inp += reclen; 306 1.4 yamt /* advance output past NetBSD-3.0-shaped entry */ 307 1.1 christos outp += idb.d_reclen; 308 1.1 christos resid -= idb.d_reclen; 309 1.44 riastrad any = true; 310 1.1 christos } 311 1.1 christos 312 1.1 christos /* if we squished out the whole block, try again */ 313 1.45 riastrad if (!any) { 314 1.31 he if (cookiebuf) 315 1.31 he free(cookiebuf, M_TEMP); 316 1.31 he cookiebuf = NULL; 317 1.1 christos goto again; 318 1.31 he } 319 1.1 christos fp->f_offset = off; /* update the vnode offset */ 320 1.1 christos 321 1.1 christos eof: 322 1.1 christos *retval = SCARG(uap, count) - resid; 323 1.1 christos out: 324 1.32 hannken VOP_UNLOCK(vp); 325 1.1 christos if (cookiebuf) 326 1.1 christos free(cookiebuf, M_TEMP); 327 1.1 christos free(tbuf, M_TEMP); 328 1.1 christos out1: 329 1.26 ad fd_putfile(SCARG(uap, fd)); 330 1.1 christos return error; 331 1.1 christos } 332 1.10 martin 333 1.10 martin /* 334 1.10 martin * Get file handle system call 335 1.10 martin */ 336 1.10 martin int 337 1.38 pgoyette compat_30_sys_getfh(struct lwp *l, const struct compat_30_sys_getfh_args *uap, 338 1.38 pgoyette register_t *retval) 339 1.10 martin { 340 1.25 dsl /* { 341 1.10 martin syscallarg(char *) fname; 342 1.10 martin syscallarg(struct compat_30_fhandle *) fhp; 343 1.25 dsl } */ 344 1.10 martin struct vnode *vp; 345 1.11 yamt struct compat_30_fhandle fh; 346 1.10 martin int error; 347 1.33 dholland struct pathbuf *pb; 348 1.10 martin struct nameidata nd; 349 1.10 martin size_t sz; 350 1.10 martin 351 1.10 martin /* 352 1.10 martin * Must be super user 353 1.10 martin */ 354 1.17 elad error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE, 355 1.17 elad 0, NULL, NULL, NULL); 356 1.10 martin if (error) 357 1.10 martin return (error); 358 1.33 dholland 359 1.33 dholland error = pathbuf_copyin(SCARG(uap, fname), &pb); 360 1.33 dholland if (error) { 361 1.33 dholland return error; 362 1.33 dholland } 363 1.33 dholland NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb); 364 1.10 martin error = namei(&nd); 365 1.36 christos pathbuf_destroy(pb); 366 1.36 christos if (error) 367 1.36 christos return error; 368 1.10 martin vp = nd.ni_vp; 369 1.35 maxv 370 1.10 martin sz = sizeof(struct compat_30_fhandle); 371 1.11 yamt error = vfs_composefh(vp, (void *)&fh, &sz); 372 1.10 martin vput(vp); 373 1.43 riastrad CTASSERT(FHANDLE_SIZE_COMPAT == sizeof(struct compat_30_fhandle)); 374 1.15 yamt if (sz != FHANDLE_SIZE_COMPAT) { 375 1.15 yamt error = EINVAL; 376 1.15 yamt } 377 1.10 martin if (error) 378 1.42 christos return error; 379 1.42 christos return copyout(&fh, SCARG(uap, fhp), sizeof(fh)); 380 1.10 martin } 381 1.13 martin 382 1.13 martin /* 383 1.13 martin * Open a file given a file handle. 384 1.13 martin * 385 1.13 martin * Check permissions, allocate an open file structure, 386 1.13 martin * and call the device open routine if any. 387 1.13 martin */ 388 1.13 martin int 389 1.38 pgoyette compat_30_sys_fhopen(struct lwp *l, 390 1.38 pgoyette const struct compat_30_sys_fhopen_args *uap, register_t *retval) 391 1.13 martin { 392 1.25 dsl /* { 393 1.13 martin syscallarg(const fhandle_t *) fhp; 394 1.13 martin syscallarg(int) flags; 395 1.25 dsl } */ 396 1.13 martin 397 1.14 yamt return dofhopen(l, SCARG(uap, fhp), FHANDLE_SIZE_COMPAT, 398 1.14 yamt SCARG(uap, flags), retval); 399 1.13 martin } 400 1.13 martin 401 1.13 martin /* ARGSUSED */ 402 1.13 martin int 403 1.38 pgoyette compat_30_sys___fhstat30(struct lwp *l, 404 1.38 pgoyette const struct compat_30_sys___fhstat30_args *uap_30, register_t *retval) 405 1.13 martin { 406 1.25 dsl /* { 407 1.13 martin syscallarg(const fhandle_t *) fhp; 408 1.29 christos syscallarg(struct stat30 *) sb; 409 1.25 dsl } */ 410 1.29 christos struct stat sb; 411 1.29 christos struct stat13 osb; 412 1.29 christos int error; 413 1.23 dsl 414 1.29 christos error = do_fhstat(l, SCARG(uap_30, fhp), FHANDLE_SIZE_COMPAT, &sb); 415 1.29 christos if (error) 416 1.29 christos return error; 417 1.29 christos cvtstat(&osb, &sb); 418 1.42 christos return copyout(&osb, SCARG(uap_30, sb), sizeof(osb)); 419 1.13 martin } 420 1.13 martin 421 1.13 martin /* ARGSUSED */ 422 1.13 martin int 423 1.38 pgoyette compat_30_sys_fhstatvfs1(struct lwp *l, 424 1.39 christos const struct compat_30_sys_fhstatvfs1_args *uap, register_t *retval) 425 1.13 martin { 426 1.25 dsl /* { 427 1.13 martin syscallarg(const fhandle_t *) fhp; 428 1.39 christos syscallarg(struct statvfs90 *) buf; 429 1.13 martin syscallarg(int) flags; 430 1.25 dsl } */ 431 1.39 christos struct statvfs *sb = STATVFSBUF_GET(); 432 1.39 christos int error = do_fhstatvfs(l, SCARG(uap, fhp), FHANDLE_SIZE_COMPAT, 433 1.39 christos sb, SCARG(uap, flags)); 434 1.39 christos 435 1.39 christos if (!error) { 436 1.39 christos error = statvfs_to_statvfs90_copy(sb, SCARG(uap, buf), 437 1.39 christos sizeof(struct statvfs90)); 438 1.39 christos } 439 1.23 dsl 440 1.39 christos STATVFSBUF_PUT(sb); 441 1.13 martin 442 1.39 christos return error; 443 1.13 martin } 444 1.38 pgoyette 445 1.38 pgoyette int 446 1.38 pgoyette vfs_syscalls_30_init(void) 447 1.38 pgoyette { 448 1.38 pgoyette 449 1.38 pgoyette return syscall_establish(NULL, vfs_syscalls_30_syscalls); 450 1.38 pgoyette } 451 1.38 pgoyette 452 1.38 pgoyette int 453 1.38 pgoyette vfs_syscalls_30_fini(void) 454 1.38 pgoyette { 455 1.38 pgoyette 456 1.38 pgoyette return syscall_disestablish(NULL, vfs_syscalls_30_syscalls); 457 1.38 pgoyette } 458