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