1 /* $NetBSD: vfs_syscalls_43.c,v 1.40 2007/10/10 20:42:21 ad 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 * @(#)vfs_syscalls.c 8.28 (Berkeley) 12/10/94 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls_43.c,v 1.40 2007/10/10 20:42:21 ad Exp $"); 41 42 #if defined(_KERNEL_OPT) 43 #include "fs_union.h" 44 #endif 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/filedesc.h> 49 #include <sys/kernel.h> 50 #include <sys/proc.h> 51 #include <sys/file.h> 52 #include <sys/vnode.h> 53 #include <sys/namei.h> 54 #include <sys/dirent.h> 55 #include <sys/socket.h> 56 #include <sys/socketvar.h> 57 #include <sys/stat.h> 58 #include <sys/malloc.h> 59 #include <sys/ioctl.h> 60 #include <sys/fcntl.h> 61 #include <sys/syslog.h> 62 #include <sys/unistd.h> 63 #include <sys/resourcevar.h> 64 #include <sys/sysctl.h> 65 66 #include <sys/mount.h> 67 #include <sys/syscallargs.h> 68 #include <sys/vfs_syscalls.h> 69 70 #include <compat/sys/stat.h> 71 #include <compat/sys/mount.h> 72 73 static void cvtstat __P((struct stat *, struct stat43 *)); 74 75 /* 76 * Convert from an old to a new stat structure. 77 */ 78 static void 79 cvtstat(st, ost) 80 struct stat *st; 81 struct stat43 *ost; 82 { 83 84 ost->st_dev = st->st_dev; 85 ost->st_ino = st->st_ino; 86 ost->st_mode = st->st_mode & 0xffff; 87 ost->st_nlink = st->st_nlink; 88 ost->st_uid = st->st_uid; 89 ost->st_gid = st->st_gid; 90 ost->st_rdev = st->st_rdev; 91 if (st->st_size < (quad_t)1 << 32) 92 ost->st_size = st->st_size; 93 else 94 ost->st_size = -2; 95 ost->st_atime = st->st_atime; 96 ost->st_mtime = st->st_mtime; 97 ost->st_ctime = st->st_ctime; 98 ost->st_blksize = st->st_blksize; 99 ost->st_blocks = st->st_blocks; 100 ost->st_flags = st->st_flags; 101 ost->st_gen = st->st_gen; 102 } 103 104 /* 105 * Get file status; this version follows links. 106 */ 107 /* ARGSUSED */ 108 int 109 compat_43_sys_stat(struct lwp *l, void *v, register_t *retval) 110 { 111 struct compat_43_sys_stat_args /* { 112 syscallarg(char *) path; 113 syscallarg(struct stat43 *) ub; 114 } */ *uap = v; 115 struct stat sb; 116 struct stat43 osb; 117 int error; 118 119 error = do_sys_stat(l, SCARG(uap, path), FOLLOW, &sb); 120 if (error) 121 return (error); 122 cvtstat(&sb, &osb); 123 error = copyout((void *)&osb, (void *)SCARG(uap, ub), sizeof (osb)); 124 return (error); 125 } 126 127 /* 128 * Get file status; this version does not follow links. 129 */ 130 /* ARGSUSED */ 131 int 132 compat_43_sys_lstat(struct lwp *l, void *v, register_t *retval) 133 { 134 struct compat_43_sys_lstat_args /* { 135 syscallarg(char *) path; 136 syscallarg(struct ostat *) ub; 137 } */ *uap = v; 138 struct vnode *vp, *dvp; 139 struct stat sb, sb1; 140 struct stat43 osb; 141 int error; 142 struct nameidata nd; 143 int ndflags; 144 145 ndflags = NOFOLLOW | LOCKLEAF | LOCKPARENT | TRYEMULROOT; 146 again: 147 NDINIT(&nd, LOOKUP, ndflags, UIO_USERSPACE, SCARG(uap, path), l); 148 if ((error = namei(&nd))) { 149 if (error == EISDIR && (ndflags & LOCKPARENT) != 0) { 150 /* 151 * Should only happen on '/'. Retry without LOCKPARENT; 152 * this is safe since the vnode won't be a VLNK. 153 */ 154 ndflags &= ~LOCKPARENT; 155 goto again; 156 } 157 return (error); 158 } 159 /* 160 * For symbolic links, always return the attributes of its 161 * containing directory, except for mode, size, and links. 162 */ 163 vp = nd.ni_vp; 164 dvp = nd.ni_dvp; 165 if (vp->v_type != VLNK) { 166 if ((ndflags & LOCKPARENT) != 0) { 167 if (dvp == vp) 168 vrele(dvp); 169 else 170 vput(dvp); 171 } 172 error = vn_stat(vp, &sb, l); 173 vput(vp); 174 if (error) 175 return (error); 176 } else { 177 error = vn_stat(dvp, &sb, l); 178 vput(dvp); 179 if (error) { 180 vput(vp); 181 return (error); 182 } 183 error = vn_stat(vp, &sb1, l); 184 vput(vp); 185 if (error) 186 return (error); 187 sb.st_mode &= ~S_IFDIR; 188 sb.st_mode |= S_IFLNK; 189 sb.st_nlink = sb1.st_nlink; 190 sb.st_size = sb1.st_size; 191 sb.st_blocks = sb1.st_blocks; 192 } 193 cvtstat(&sb, &osb); 194 error = copyout((void *)&osb, (void *)SCARG(uap, ub), sizeof (osb)); 195 return (error); 196 } 197 198 /* 199 * Return status information about a file descriptor. 200 */ 201 /* ARGSUSED */ 202 int 203 compat_43_sys_fstat(struct lwp *l, void *v, register_t *retval) 204 { 205 struct compat_43_sys_fstat_args /* { 206 syscallarg(int) fd; 207 syscallarg(struct stat43 *) sb; 208 } */ *uap = v; 209 struct proc *p = l->l_proc; 210 int fd = SCARG(uap, fd); 211 struct filedesc *fdp = p->p_fd; 212 struct file *fp; 213 struct stat ub; 214 struct stat43 oub; 215 int error; 216 217 if ((fp = fd_getfile(fdp, fd)) == NULL) 218 return (EBADF); 219 220 FILE_USE(fp); 221 error = (*fp->f_ops->fo_stat)(fp, &ub, l); 222 FILE_UNUSE(fp, l); 223 224 if (error == 0) { 225 cvtstat(&ub, &oub); 226 error = copyout((void *)&oub, (void *)SCARG(uap, sb), 227 sizeof (oub)); 228 } 229 230 231 return (error); 232 } 233 234 235 /* 236 * Truncate a file given a file descriptor. 237 */ 238 /* ARGSUSED */ 239 int 240 compat_43_sys_ftruncate(struct lwp *l, void *v, register_t *retval) 241 { 242 struct compat_43_sys_ftruncate_args /* { 243 syscallarg(int) fd; 244 syscallarg(long) length; 245 } */ *uap = v; 246 struct sys_ftruncate_args /* { 247 syscallarg(int) fd; 248 syscallarg(int) pad; 249 syscallarg(off_t) length; 250 } */ nuap; 251 252 SCARG(&nuap, fd) = SCARG(uap, fd); 253 SCARG(&nuap, length) = SCARG(uap, length); 254 return (sys_ftruncate(l, &nuap, retval)); 255 } 256 257 /* 258 * Truncate a file given its path name. 259 */ 260 /* ARGSUSED */ 261 int 262 compat_43_sys_truncate(struct lwp *l, void *v, register_t *retval) 263 { 264 struct compat_43_sys_truncate_args /* { 265 syscallarg(char *) path; 266 syscallarg(long) length; 267 } */ *uap = v; 268 struct sys_truncate_args /* { 269 syscallarg(char *) path; 270 syscallarg(int) pad; 271 syscallarg(off_t) length; 272 } */ nuap; 273 274 SCARG(&nuap, path) = SCARG(uap, path); 275 SCARG(&nuap, length) = SCARG(uap, length); 276 return (sys_truncate(l, &nuap, retval)); 277 } 278 279 280 /* 281 * Reposition read/write file offset. 282 */ 283 int 284 compat_43_sys_lseek(struct lwp *l, void *v, register_t *retval) 285 { 286 struct compat_43_sys_lseek_args /* { 287 syscallarg(int) fd; 288 syscallarg(long) offset; 289 syscallarg(int) whence; 290 } */ *uap = v; 291 struct sys_lseek_args /* { 292 syscallarg(int) fd; 293 syscallarg(int) pad; 294 syscallarg(off_t) offset; 295 syscallarg(int) whence; 296 } */ nuap; 297 off_t qret; 298 int error; 299 300 SCARG(&nuap, fd) = SCARG(uap, fd); 301 SCARG(&nuap, offset) = SCARG(uap, offset); 302 SCARG(&nuap, whence) = SCARG(uap, whence); 303 error = sys_lseek(l, &nuap, (void *)&qret); 304 *(long *)retval = qret; 305 return (error); 306 } 307 308 309 /* 310 * Create a file. 311 */ 312 int 313 compat_43_sys_creat(struct lwp *l, void *v, register_t *retval) 314 { 315 struct compat_43_sys_creat_args /* { 316 syscallarg(char *) path; 317 syscallarg(int) mode; 318 } */ *uap = v; 319 struct sys_open_args /* { 320 syscallarg(char *) path; 321 syscallarg(int) flags; 322 syscallarg(int) mode; 323 } */ nuap; 324 325 SCARG(&nuap, path) = SCARG(uap, path); 326 SCARG(&nuap, mode) = SCARG(uap, mode); 327 SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC; 328 return (sys_open(l, &nuap, retval)); 329 } 330 331 /*ARGSUSED*/ 332 int 333 compat_43_sys_quota(struct lwp *l, void *v, 334 register_t *retval) 335 { 336 337 return (ENOSYS); 338 } 339 340 341 /* 342 * Read a block of directory entries in a file system independent format. 343 */ 344 int 345 compat_43_sys_getdirentries(struct lwp *l, void *v, register_t *retval) 346 { 347 struct compat_43_sys_getdirentries_args /* { 348 syscallarg(int) fd; 349 syscallarg(char *) buf; 350 syscallarg(u_int) count; 351 syscallarg(long *) basep; 352 } */ *uap = v; 353 struct proc *p = l->l_proc; 354 struct vnode *vp; 355 struct file *fp; 356 struct uio auio, kuio; 357 struct iovec aiov, kiov; 358 struct dirent *dp, *edp; 359 char *dirbuf; 360 size_t count = min(MAXBSIZE, (size_t)SCARG(uap, count)); 361 362 int error, eofflag, readcnt; 363 long loff; 364 365 /* getvnode() will use the descriptor for us */ 366 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) 367 return (error); 368 if ((fp->f_flag & FREAD) == 0) { 369 error = EBADF; 370 goto out; 371 } 372 vp = (struct vnode *)fp->f_data; 373 unionread: 374 if (vp->v_type != VDIR) { 375 error = EINVAL; 376 goto out; 377 } 378 aiov.iov_base = SCARG(uap, buf); 379 aiov.iov_len = count; 380 auio.uio_iov = &aiov; 381 auio.uio_iovcnt = 1; 382 auio.uio_rw = UIO_READ; 383 auio.uio_resid = count; 384 KASSERT(l == curlwp); 385 auio.uio_vmspace = l->l_proc->p_vmspace; 386 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 387 loff = auio.uio_offset = fp->f_offset; 388 # if (BYTE_ORDER != LITTLE_ENDIAN) 389 if ((vp->v_mount->mnt_iflag & IMNT_DTYPE) == 0) { 390 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, 391 (off_t **)0, (int *)0); 392 fp->f_offset = auio.uio_offset; 393 } else 394 # endif 395 { 396 kuio = auio; 397 kuio.uio_iov = &kiov; 398 kiov.iov_len = count; 399 dirbuf = malloc(count, M_TEMP, M_WAITOK); 400 kiov.iov_base = dirbuf; 401 UIO_SETUP_SYSSPACE(&kuio); 402 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag, 403 (off_t **)0, (int *)0); 404 fp->f_offset = kuio.uio_offset; 405 if (error == 0) { 406 readcnt = count - kuio.uio_resid; 407 edp = (struct dirent *)&dirbuf[readcnt]; 408 for (dp = (struct dirent *)dirbuf; dp < edp; ) { 409 # if (BYTE_ORDER == LITTLE_ENDIAN) 410 /* 411 * The expected low byte of 412 * dp->d_namlen is our dp->d_type. 413 * The high MBZ byte of dp->d_namlen 414 * is our dp->d_namlen. 415 */ 416 dp->d_type = dp->d_namlen; 417 dp->d_namlen = 0; 418 # else 419 /* 420 * The dp->d_type is the high byte 421 * of the expected dp->d_namlen, 422 * so must be zero'ed. 423 */ 424 dp->d_type = 0; 425 # endif 426 if (dp->d_reclen > 0) { 427 dp = (struct dirent *) 428 ((char *)dp + dp->d_reclen); 429 } else { 430 error = EIO; 431 break; 432 } 433 } 434 if (dp >= edp) 435 error = uiomove(dirbuf, readcnt, &auio); 436 } 437 free(dirbuf, M_TEMP); 438 } 439 VOP_UNLOCK(vp, 0); 440 if (error) 441 goto out; 442 443 #ifdef UNION 444 { 445 extern int (**union_vnodeop_p) __P((void *)); 446 extern struct vnode *union_dircache __P((struct vnode *)); 447 448 if ((count == auio.uio_resid) && 449 (vp->v_op == union_vnodeop_p)) { 450 struct vnode *lvp; 451 452 lvp = union_dircache(vp); 453 if (lvp != NULLVP) { 454 struct vattr va; 455 456 /* 457 * If the directory is opaque, 458 * then don't show lower entries 459 */ 460 error = VOP_GETATTR(vp, &va, fp->f_cred, l); 461 if (va.va_flags & OPAQUE) { 462 vput(lvp); 463 lvp = NULL; 464 } 465 } 466 467 if (lvp != NULLVP) { 468 error = VOP_OPEN(lvp, FREAD, fp->f_cred, l); 469 VOP_UNLOCK(lvp, 0); 470 471 if (error) { 472 vrele(lvp); 473 goto out; 474 } 475 fp->f_data = (void *) lvp; 476 fp->f_offset = 0; 477 error = vn_close(vp, FREAD, fp->f_cred, l); 478 if (error) 479 goto out; 480 vp = lvp; 481 goto unionread; 482 } 483 } 484 } 485 #endif /* UNION */ 486 487 if ((count == auio.uio_resid) && 488 (vp->v_vflag & VV_ROOT) && 489 (vp->v_mount->mnt_flag & MNT_UNION)) { 490 struct vnode *tvp = vp; 491 vp = vp->v_mount->mnt_vnodecovered; 492 VREF(vp); 493 fp->f_data = (void *) vp; 494 fp->f_offset = 0; 495 vrele(tvp); 496 goto unionread; 497 } 498 error = copyout((void *)&loff, (void *)SCARG(uap, basep), 499 sizeof(long)); 500 *retval = count - auio.uio_resid; 501 out: 502 FILE_UNUSE(fp, l); 503 return (error); 504 } 505 506 /* 507 * sysctl helper routine for vfs.generic.conf lookups. 508 */ 509 #if defined(COMPAT_09) || defined(COMPAT_43) || defined(COMPAT_44) 510 static int 511 sysctl_vfs_generic_conf(SYSCTLFN_ARGS) 512 { 513 struct vfsconf vfc; 514 extern const char * const mountcompatnames[]; 515 extern int nmountcompatnames; 516 struct sysctlnode node; 517 struct vfsops *vfsp; 518 u_int vfsnum; 519 520 if (namelen != 1) 521 return (ENOTDIR); 522 vfsnum = name[0]; 523 if (vfsnum >= nmountcompatnames || 524 mountcompatnames[vfsnum] == NULL) 525 return (EOPNOTSUPP); 526 vfsp = vfs_getopsbyname(mountcompatnames[vfsnum]); 527 if (vfsp == NULL) 528 return (EOPNOTSUPP); 529 530 vfc.vfc_vfsops = vfsp; 531 strncpy(vfc.vfc_name, vfsp->vfs_name, sizeof(vfc.vfc_name)); 532 vfc.vfc_typenum = vfsnum; 533 vfc.vfc_refcount = vfsp->vfs_refcount; 534 vfc.vfc_flags = 0; 535 vfc.vfc_mountroot = vfsp->vfs_mountroot; 536 vfc.vfc_next = NULL; 537 vfs_delref(vfsp); 538 539 node = *rnode; 540 node.sysctl_data = &vfc; 541 return (sysctl_lookup(SYSCTLFN_CALL(&node))); 542 } 543 544 /* 545 * Top level filesystem related information gathering. 546 */ 547 SYSCTL_SETUP(compat_sysctl_vfs_setup, "compat sysctl vfs subtree setup") 548 { 549 extern int nmountcompatnames; 550 551 sysctl_createv(clog, 0, NULL, NULL, 552 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE, 553 CTLTYPE_INT, "maxtypenum", 554 SYSCTL_DESCR("Highest valid filesystem type number"), 555 NULL, nmountcompatnames, NULL, 0, 556 CTL_VFS, VFS_GENERIC, VFS_MAXTYPENUM, CTL_EOL); 557 sysctl_createv(clog, 0, NULL, NULL, 558 CTLFLAG_PERMANENT, 559 CTLTYPE_STRUCT, "conf", 560 SYSCTL_DESCR("Filesystem configuration information"), 561 sysctl_vfs_generic_conf, 0, NULL, 562 sizeof(struct vfsconf), 563 CTL_VFS, VFS_GENERIC, VFS_CONF, CTL_EOL); 564 } 565 #endif 566