1 /* $NetBSD: vfs_syscalls.c,v 1.404 2010/03/03 00:47:31 yamt Exp $ */ 2 3 /*- 4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Andrew Doran. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 1989, 1993 34 * The Regents of the University of California. All rights reserved. 35 * (c) UNIX System Laboratories, Inc. 36 * All or some portions of this file are derived from material licensed 37 * to the University of California by American Telephone and Telegraph 38 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 39 * the permission of UNIX System Laboratories, Inc. 40 * 41 * Redistribution and use in source and binary forms, with or without 42 * modification, are permitted provided that the following conditions 43 * are met: 44 * 1. Redistributions of source code must retain the above copyright 45 * notice, this list of conditions and the following disclaimer. 46 * 2. Redistributions in binary form must reproduce the above copyright 47 * notice, this list of conditions and the following disclaimer in the 48 * documentation and/or other materials provided with the distribution. 49 * 3. Neither the name of the University nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63 * SUCH DAMAGE. 64 * 65 * @(#)vfs_syscalls.c 8.42 (Berkeley) 7/31/95 66 */ 67 68 #include <sys/cdefs.h> 69 __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls.c,v 1.404 2010/03/03 00:47:31 yamt Exp $"); 70 71 #ifdef _KERNEL_OPT 72 #include "opt_fileassoc.h" 73 #include "veriexec.h" 74 #endif 75 76 #include <sys/param.h> 77 #include <sys/systm.h> 78 #include <sys/namei.h> 79 #include <sys/filedesc.h> 80 #include <sys/kernel.h> 81 #include <sys/file.h> 82 #include <sys/stat.h> 83 #include <sys/vnode.h> 84 #include <sys/mount.h> 85 #include <sys/proc.h> 86 #include <sys/uio.h> 87 #include <sys/kmem.h> 88 #include <sys/dirent.h> 89 #include <sys/sysctl.h> 90 #include <sys/syscallargs.h> 91 #include <sys/vfs_syscalls.h> 92 #include <sys/ktrace.h> 93 #ifdef FILEASSOC 94 #include <sys/fileassoc.h> 95 #endif /* FILEASSOC */ 96 #include <sys/verified_exec.h> 97 #include <sys/kauth.h> 98 #include <sys/atomic.h> 99 #include <sys/module.h> 100 #include <sys/buf.h> 101 102 #include <miscfs/genfs/genfs.h> 103 #include <miscfs/syncfs/syncfs.h> 104 #include <miscfs/specfs/specdev.h> 105 106 #include <nfs/rpcv2.h> 107 #include <nfs/nfsproto.h> 108 #include <nfs/nfs.h> 109 #include <nfs/nfs_var.h> 110 111 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount struct"); 112 113 static int change_flags(struct vnode *, u_long, struct lwp *); 114 static int change_mode(struct vnode *, int, struct lwp *l); 115 static int change_owner(struct vnode *, uid_t, gid_t, struct lwp *, int); 116 117 void checkdirs(struct vnode *); 118 119 /* 120 * Virtual File System System Calls 121 */ 122 123 /* 124 * Mount a file system. 125 */ 126 127 /* 128 * This table is used to maintain compatibility with 4.3BSD 129 * and NetBSD 0.9 mount syscalls - and possibly other systems. 130 * Note, the order is important! 131 * 132 * Do not modify this table. It should only contain filesystems 133 * supported by NetBSD 0.9 and 4.3BSD. 134 */ 135 const char * const mountcompatnames[] = { 136 NULL, /* 0 = MOUNT_NONE */ 137 MOUNT_FFS, /* 1 = MOUNT_UFS */ 138 MOUNT_NFS, /* 2 */ 139 MOUNT_MFS, /* 3 */ 140 MOUNT_MSDOS, /* 4 */ 141 MOUNT_CD9660, /* 5 = MOUNT_ISOFS */ 142 MOUNT_FDESC, /* 6 */ 143 MOUNT_KERNFS, /* 7 */ 144 NULL, /* 8 = MOUNT_DEVFS */ 145 MOUNT_AFS, /* 9 */ 146 }; 147 const int nmountcompatnames = sizeof(mountcompatnames) / 148 sizeof(mountcompatnames[0]); 149 150 static int 151 mount_update(struct lwp *l, struct vnode *vp, const char *path, int flags, 152 void *data, size_t *data_len) 153 { 154 struct mount *mp; 155 int error = 0, saved_flags; 156 157 mp = vp->v_mount; 158 saved_flags = mp->mnt_flag; 159 160 /* We can operate only on VV_ROOT nodes. */ 161 if ((vp->v_vflag & VV_ROOT) == 0) { 162 error = EINVAL; 163 goto out; 164 } 165 166 /* 167 * We only allow the filesystem to be reloaded if it 168 * is currently mounted read-only. Additionally, we 169 * prevent read-write to read-only downgrades. 170 */ 171 if ((flags & (MNT_RELOAD | MNT_RDONLY)) != 0 && 172 (mp->mnt_flag & MNT_RDONLY) == 0) { 173 error = EOPNOTSUPP; /* Needs translation */ 174 goto out; 175 } 176 177 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT, 178 KAUTH_REQ_SYSTEM_MOUNT_UPDATE, mp, KAUTH_ARG(flags), data); 179 if (error) 180 goto out; 181 182 if (vfs_busy(mp, NULL)) { 183 error = EPERM; 184 goto out; 185 } 186 187 mutex_enter(&mp->mnt_updating); 188 189 mp->mnt_flag &= ~MNT_OP_FLAGS; 190 mp->mnt_flag |= flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE); 191 192 /* 193 * Set the mount level flags. 194 */ 195 if (flags & MNT_RDONLY) 196 mp->mnt_flag |= MNT_RDONLY; 197 else if (mp->mnt_flag & MNT_RDONLY) 198 mp->mnt_iflag |= IMNT_WANTRDWR; 199 mp->mnt_flag &= 200 ~(MNT_NOSUID | MNT_NOEXEC | MNT_NODEV | 201 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP | 202 MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM | MNT_SOFTDEP | 203 MNT_LOG); 204 mp->mnt_flag |= flags & 205 (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV | 206 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP | 207 MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM | MNT_SOFTDEP | 208 MNT_LOG | MNT_IGNORE); 209 210 error = VFS_MOUNT(mp, path, data, data_len); 211 212 if (error && data != NULL) { 213 int error2; 214 215 /* 216 * Update failed; let's try and see if it was an 217 * export request. For compat with 3.0 and earlier. 218 */ 219 error2 = vfs_hooks_reexport(mp, path, data); 220 221 /* 222 * Only update error code if the export request was 223 * understood but some problem occurred while 224 * processing it. 225 */ 226 if (error2 != EJUSTRETURN) 227 error = error2; 228 } 229 230 if (mp->mnt_iflag & IMNT_WANTRDWR) 231 mp->mnt_flag &= ~MNT_RDONLY; 232 if (error) 233 mp->mnt_flag = saved_flags; 234 mp->mnt_flag &= ~MNT_OP_FLAGS; 235 mp->mnt_iflag &= ~IMNT_WANTRDWR; 236 if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0) { 237 if (mp->mnt_syncer == NULL) 238 error = vfs_allocate_syncvnode(mp); 239 } else { 240 if (mp->mnt_syncer != NULL) 241 vfs_deallocate_syncvnode(mp); 242 } 243 mutex_exit(&mp->mnt_updating); 244 vfs_unbusy(mp, false, NULL); 245 246 out: 247 return (error); 248 } 249 250 static int 251 mount_get_vfsops(const char *fstype, struct vfsops **vfsops) 252 { 253 char fstypename[sizeof(((struct statvfs *)NULL)->f_fstypename)]; 254 int error; 255 256 /* Copy file-system type from userspace. */ 257 error = copyinstr(fstype, fstypename, sizeof(fstypename), NULL); 258 if (error) { 259 /* 260 * Historically, filesystem types were identified by numbers. 261 * If we get an integer for the filesystem type instead of a 262 * string, we check to see if it matches one of the historic 263 * filesystem types. 264 */ 265 u_long fsindex = (u_long)fstype; 266 if (fsindex >= nmountcompatnames || 267 mountcompatnames[fsindex] == NULL) 268 return ENODEV; 269 strlcpy(fstypename, mountcompatnames[fsindex], 270 sizeof(fstypename)); 271 } 272 273 /* Accept `ufs' as an alias for `ffs', for compatibility. */ 274 if (strcmp(fstypename, "ufs") == 0) 275 fstypename[0] = 'f'; 276 277 if ((*vfsops = vfs_getopsbyname(fstypename)) != NULL) 278 return 0; 279 280 /* If we can autoload a vfs module, try again */ 281 mutex_enter(&module_lock); 282 (void)module_autoload(fstypename, MODULE_CLASS_VFS); 283 mutex_exit(&module_lock); 284 285 if ((*vfsops = vfs_getopsbyname(fstypename)) != NULL) 286 return 0; 287 288 return ENODEV; 289 } 290 291 static int 292 mount_domount(struct lwp *l, struct vnode **vpp, struct vfsops *vfsops, 293 const char *path, int flags, void *data, size_t *data_len, u_int recurse) 294 { 295 struct mount *mp; 296 struct vnode *vp = *vpp; 297 struct vattr va; 298 int error; 299 300 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT, 301 KAUTH_REQ_SYSTEM_MOUNT_NEW, vp, KAUTH_ARG(flags), data); 302 if (error) { 303 vfs_delref(vfsops); 304 return error; 305 } 306 307 /* Can't make a non-dir a mount-point (from here anyway). */ 308 if (vp->v_type != VDIR) { 309 vfs_delref(vfsops); 310 return ENOTDIR; 311 } 312 313 /* 314 * If the user is not root, ensure that they own the directory 315 * onto which we are attempting to mount. 316 */ 317 if ((error = VOP_GETATTR(vp, &va, l->l_cred)) != 0 || 318 (va.va_uid != kauth_cred_geteuid(l->l_cred) && 319 (error = kauth_authorize_generic(l->l_cred, 320 KAUTH_GENERIC_ISSUSER, NULL)) != 0)) { 321 vfs_delref(vfsops); 322 return error; 323 } 324 325 if (flags & MNT_EXPORTED) { 326 vfs_delref(vfsops); 327 return EINVAL; 328 } 329 330 if ((error = vinvalbuf(vp, V_SAVE, l->l_cred, l, 0, 0)) != 0) { 331 vfs_delref(vfsops); 332 return error; 333 } 334 335 /* 336 * Check if a file-system is not already mounted on this vnode. 337 */ 338 if (vp->v_mountedhere != NULL) { 339 vfs_delref(vfsops); 340 return EBUSY; 341 } 342 343 if ((mp = vfs_mountalloc(vfsops, vp)) == NULL) { 344 vfs_delref(vfsops); 345 return ENOMEM; 346 } 347 348 mp->mnt_stat.f_owner = kauth_cred_geteuid(l->l_cred); 349 350 /* 351 * The underlying file system may refuse the mount for 352 * various reasons. Allow the user to force it to happen. 353 * 354 * Set the mount level flags. 355 */ 356 mp->mnt_flag = flags & 357 (MNT_FORCE | MNT_NOSUID | MNT_NOEXEC | MNT_NODEV | 358 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP | 359 MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM | MNT_SOFTDEP | 360 MNT_LOG | MNT_IGNORE | MNT_RDONLY); 361 362 mutex_enter(&mp->mnt_updating); 363 error = VFS_MOUNT(mp, path, data, data_len); 364 mp->mnt_flag &= ~MNT_OP_FLAGS; 365 366 /* 367 * Put the new filesystem on the mount list after root. 368 */ 369 cache_purge(vp); 370 if (error != 0) { 371 vp->v_mountedhere = NULL; 372 mutex_exit(&mp->mnt_updating); 373 vfs_unbusy(mp, false, NULL); 374 vfs_destroy(mp); 375 return error; 376 } 377 378 mp->mnt_iflag &= ~IMNT_WANTRDWR; 379 mutex_enter(&mountlist_lock); 380 vp->v_mountedhere = mp; 381 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list); 382 mutex_exit(&mountlist_lock); 383 vn_restorerecurse(vp, recurse); 384 VOP_UNLOCK(vp, 0); 385 checkdirs(vp); 386 if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0) 387 error = vfs_allocate_syncvnode(mp); 388 /* Hold an additional reference to the mount across VFS_START(). */ 389 mutex_exit(&mp->mnt_updating); 390 vfs_unbusy(mp, true, NULL); 391 (void) VFS_STATVFS(mp, &mp->mnt_stat); 392 error = VFS_START(mp, 0); 393 if (error) 394 vrele(vp); 395 /* Drop reference held for VFS_START(). */ 396 vfs_destroy(mp); 397 *vpp = NULL; 398 return error; 399 } 400 401 static int 402 mount_getargs(struct lwp *l, struct vnode *vp, const char *path, int flags, 403 void *data, size_t *data_len) 404 { 405 struct mount *mp; 406 int error; 407 408 /* If MNT_GETARGS is specified, it should be the only flag. */ 409 if (flags & ~MNT_GETARGS) 410 return EINVAL; 411 412 mp = vp->v_mount; 413 414 /* XXX: probably some notion of "can see" here if we want isolation. */ 415 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT, 416 KAUTH_REQ_SYSTEM_MOUNT_GET, mp, data, NULL); 417 if (error) 418 return error; 419 420 if ((vp->v_vflag & VV_ROOT) == 0) 421 return EINVAL; 422 423 if (vfs_busy(mp, NULL)) 424 return EPERM; 425 426 mutex_enter(&mp->mnt_updating); 427 mp->mnt_flag &= ~MNT_OP_FLAGS; 428 mp->mnt_flag |= MNT_GETARGS; 429 error = VFS_MOUNT(mp, path, data, data_len); 430 mp->mnt_flag &= ~MNT_OP_FLAGS; 431 mutex_exit(&mp->mnt_updating); 432 433 vfs_unbusy(mp, false, NULL); 434 return (error); 435 } 436 437 int 438 sys___mount50(struct lwp *l, const struct sys___mount50_args *uap, register_t *retval) 439 { 440 /* { 441 syscallarg(const char *) type; 442 syscallarg(const char *) path; 443 syscallarg(int) flags; 444 syscallarg(void *) data; 445 syscallarg(size_t) data_len; 446 } */ 447 448 return do_sys_mount(l, NULL, SCARG(uap, type), SCARG(uap, path), 449 SCARG(uap, flags), SCARG(uap, data), UIO_USERSPACE, 450 SCARG(uap, data_len), retval); 451 } 452 453 int 454 do_sys_mount(struct lwp *l, struct vfsops *vfsops, const char *type, 455 const char *path, int flags, void *data, enum uio_seg data_seg, 456 size_t data_len, register_t *retval) 457 { 458 struct vnode *vp; 459 void *data_buf = data; 460 u_int recurse; 461 bool vfsopsrele = false; 462 int error; 463 464 /* XXX: The calling convention of this routine is totally bizarre */ 465 if (vfsops) 466 vfsopsrele = true; 467 468 /* 469 * Get vnode to be covered 470 */ 471 error = namei_simple_user(path, NSM_FOLLOW_TRYEMULROOT, &vp); 472 if (error != 0) { 473 /* XXXgcc */ 474 vp = NULL; 475 recurse = 0; 476 goto done; 477 } 478 479 /* 480 * A lookup in VFS_MOUNT might result in an attempt to 481 * lock this vnode again, so make the lock recursive. 482 */ 483 if (vfsops == NULL) { 484 if (flags & (MNT_GETARGS | MNT_UPDATE)) { 485 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 486 recurse = vn_setrecurse(vp); 487 vfsops = vp->v_mount->mnt_op; 488 } else { 489 /* 'type' is userspace */ 490 error = mount_get_vfsops(type, &vfsops); 491 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 492 recurse = vn_setrecurse(vp); 493 if (error != 0) 494 goto done; 495 vfsopsrele = true; 496 } 497 } else { 498 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 499 recurse = vn_setrecurse(vp); 500 } 501 502 if (data != NULL && data_seg == UIO_USERSPACE) { 503 if (data_len == 0) { 504 /* No length supplied, use default for filesystem */ 505 data_len = vfsops->vfs_min_mount_data; 506 if (data_len > VFS_MAX_MOUNT_DATA) { 507 error = EINVAL; 508 goto done; 509 } 510 /* 511 * Hopefully a longer buffer won't make copyin() fail. 512 * For compatibility with 3.0 and earlier. 513 */ 514 if (flags & MNT_UPDATE 515 && data_len < sizeof (struct mnt_export_args30)) 516 data_len = sizeof (struct mnt_export_args30); 517 } 518 data_buf = kmem_alloc(data_len, KM_SLEEP); 519 520 /* NFS needs the buffer even for mnt_getargs .... */ 521 error = copyin(data, data_buf, data_len); 522 if (error != 0) 523 goto done; 524 } 525 526 if (flags & MNT_GETARGS) { 527 if (data_len == 0) { 528 error = EINVAL; 529 goto done; 530 } 531 error = mount_getargs(l, vp, path, flags, data_buf, &data_len); 532 if (error != 0) 533 goto done; 534 if (data_seg == UIO_USERSPACE) 535 error = copyout(data_buf, data, data_len); 536 *retval = data_len; 537 } else if (flags & MNT_UPDATE) { 538 error = mount_update(l, vp, path, flags, data_buf, &data_len); 539 } else { 540 /* Locking is handled internally in mount_domount(). */ 541 KASSERT(vfsopsrele == true); 542 error = mount_domount(l, &vp, vfsops, path, flags, data_buf, 543 &data_len, recurse); 544 vfsopsrele = false; 545 } 546 547 done: 548 if (vfsopsrele) 549 vfs_delref(vfsops); 550 if (vp != NULL) { 551 vn_restorerecurse(vp, recurse); 552 vput(vp); 553 } 554 if (data_buf != data) 555 kmem_free(data_buf, data_len); 556 return (error); 557 } 558 559 /* 560 * Scan all active processes to see if any of them have a current 561 * or root directory onto which the new filesystem has just been 562 * mounted. If so, replace them with the new mount point. 563 */ 564 void 565 checkdirs(struct vnode *olddp) 566 { 567 struct cwdinfo *cwdi; 568 struct vnode *newdp, *rele1, *rele2; 569 struct proc *p; 570 bool retry; 571 572 if (olddp->v_usecount == 1) 573 return; 574 if (VFS_ROOT(olddp->v_mountedhere, &newdp)) 575 panic("mount: lost mount"); 576 577 do { 578 retry = false; 579 mutex_enter(proc_lock); 580 PROCLIST_FOREACH(p, &allproc) { 581 if ((cwdi = p->p_cwdi) == NULL) 582 continue; 583 /* 584 * Can't change to the old directory any more, 585 * so even if we see a stale value it's not a 586 * problem. 587 */ 588 if (cwdi->cwdi_cdir != olddp && 589 cwdi->cwdi_rdir != olddp) 590 continue; 591 retry = true; 592 rele1 = NULL; 593 rele2 = NULL; 594 atomic_inc_uint(&cwdi->cwdi_refcnt); 595 mutex_exit(proc_lock); 596 rw_enter(&cwdi->cwdi_lock, RW_WRITER); 597 if (cwdi->cwdi_cdir == olddp) { 598 rele1 = cwdi->cwdi_cdir; 599 vref(newdp); 600 cwdi->cwdi_cdir = newdp; 601 } 602 if (cwdi->cwdi_rdir == olddp) { 603 rele2 = cwdi->cwdi_rdir; 604 vref(newdp); 605 cwdi->cwdi_rdir = newdp; 606 } 607 rw_exit(&cwdi->cwdi_lock); 608 cwdfree(cwdi); 609 if (rele1 != NULL) 610 vrele(rele1); 611 if (rele2 != NULL) 612 vrele(rele2); 613 mutex_enter(proc_lock); 614 break; 615 } 616 mutex_exit(proc_lock); 617 } while (retry); 618 619 if (rootvnode == olddp) { 620 vrele(rootvnode); 621 vref(newdp); 622 rootvnode = newdp; 623 } 624 vput(newdp); 625 } 626 627 /* 628 * Unmount a file system. 629 * 630 * Note: unmount takes a path to the vnode mounted on as argument, 631 * not special file (as before). 632 */ 633 /* ARGSUSED */ 634 int 635 sys_unmount(struct lwp *l, const struct sys_unmount_args *uap, register_t *retval) 636 { 637 /* { 638 syscallarg(const char *) path; 639 syscallarg(int) flags; 640 } */ 641 struct vnode *vp; 642 struct mount *mp; 643 int error; 644 struct nameidata nd; 645 646 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE, 647 SCARG(uap, path)); 648 if ((error = namei(&nd)) != 0) 649 return (error); 650 vp = nd.ni_vp; 651 mp = vp->v_mount; 652 atomic_inc_uint(&mp->mnt_refcnt); 653 VOP_UNLOCK(vp, 0); 654 655 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT, 656 KAUTH_REQ_SYSTEM_MOUNT_UNMOUNT, mp, NULL, NULL); 657 if (error) { 658 vrele(vp); 659 vfs_destroy(mp); 660 return (error); 661 } 662 663 /* 664 * Don't allow unmounting the root file system. 665 */ 666 if (mp->mnt_flag & MNT_ROOTFS) { 667 vrele(vp); 668 vfs_destroy(mp); 669 return (EINVAL); 670 } 671 672 /* 673 * Must be the root of the filesystem 674 */ 675 if ((vp->v_vflag & VV_ROOT) == 0) { 676 vrele(vp); 677 vfs_destroy(mp); 678 return (EINVAL); 679 } 680 681 vrele(vp); 682 error = dounmount(mp, SCARG(uap, flags), l); 683 vfs_destroy(mp); 684 return error; 685 } 686 687 /* 688 * Do the actual file system unmount. File system is assumed to have 689 * been locked by the caller. 690 * 691 * => Caller hold reference to the mount, explicitly for dounmount(). 692 */ 693 int 694 dounmount(struct mount *mp, int flags, struct lwp *l) 695 { 696 struct vnode *coveredvp; 697 int error; 698 int async; 699 int used_syncer; 700 701 #if NVERIEXEC > 0 702 error = veriexec_unmountchk(mp); 703 if (error) 704 return (error); 705 #endif /* NVERIEXEC > 0 */ 706 707 /* 708 * XXX Freeze syncer. Must do this before locking the 709 * mount point. See dounmount() for details. 710 */ 711 mutex_enter(&syncer_mutex); 712 rw_enter(&mp->mnt_unmounting, RW_WRITER); 713 if ((mp->mnt_iflag & IMNT_GONE) != 0) { 714 rw_exit(&mp->mnt_unmounting); 715 mutex_exit(&syncer_mutex); 716 return ENOENT; 717 } 718 719 used_syncer = (mp->mnt_syncer != NULL); 720 721 /* 722 * XXX Syncer must be frozen when we get here. This should really 723 * be done on a per-mountpoint basis, but the syncer doesn't work 724 * like that. 725 * 726 * The caller of dounmount() must acquire syncer_mutex because 727 * the syncer itself acquires locks in syncer_mutex -> vfs_busy 728 * order, and we must preserve that order to avoid deadlock. 729 * 730 * So, if the file system did not use the syncer, now is 731 * the time to release the syncer_mutex. 732 */ 733 if (used_syncer == 0) 734 mutex_exit(&syncer_mutex); 735 736 mp->mnt_iflag |= IMNT_UNMOUNT; 737 async = mp->mnt_flag & MNT_ASYNC; 738 mp->mnt_flag &= ~MNT_ASYNC; 739 cache_purgevfs(mp); /* remove cache entries for this file sys */ 740 if (mp->mnt_syncer != NULL) 741 vfs_deallocate_syncvnode(mp); 742 error = 0; 743 if ((mp->mnt_flag & MNT_RDONLY) == 0) { 744 error = VFS_SYNC(mp, MNT_WAIT, l->l_cred); 745 } 746 vfs_scrubvnlist(mp); 747 if (error == 0 || (flags & MNT_FORCE)) 748 error = VFS_UNMOUNT(mp, flags); 749 if (error) { 750 if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0) 751 (void) vfs_allocate_syncvnode(mp); 752 mp->mnt_iflag &= ~IMNT_UNMOUNT; 753 mp->mnt_flag |= async; 754 rw_exit(&mp->mnt_unmounting); 755 if (used_syncer) 756 mutex_exit(&syncer_mutex); 757 return (error); 758 } 759 vfs_scrubvnlist(mp); 760 mutex_enter(&mountlist_lock); 761 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) 762 coveredvp->v_mountedhere = NULL; 763 CIRCLEQ_REMOVE(&mountlist, mp, mnt_list); 764 mp->mnt_iflag |= IMNT_GONE; 765 mutex_exit(&mountlist_lock); 766 if (TAILQ_FIRST(&mp->mnt_vnodelist) != NULL) 767 panic("unmount: dangling vnode"); 768 if (used_syncer) 769 mutex_exit(&syncer_mutex); 770 vfs_hooks_unmount(mp); 771 rw_exit(&mp->mnt_unmounting); 772 vfs_destroy(mp); /* reference from mount() */ 773 if (coveredvp != NULLVP) 774 vrele(coveredvp); 775 return (0); 776 } 777 778 /* 779 * Sync each mounted filesystem. 780 */ 781 #ifdef DEBUG 782 int syncprt = 0; 783 struct ctldebug debug0 = { "syncprt", &syncprt }; 784 #endif 785 786 /* ARGSUSED */ 787 int 788 sys_sync(struct lwp *l, const void *v, register_t *retval) 789 { 790 struct mount *mp, *nmp; 791 int asyncflag; 792 793 if (l == NULL) 794 l = &lwp0; 795 796 mutex_enter(&mountlist_lock); 797 for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist; 798 mp = nmp) { 799 if (vfs_busy(mp, &nmp)) { 800 continue; 801 } 802 mutex_enter(&mp->mnt_updating); 803 if ((mp->mnt_flag & MNT_RDONLY) == 0) { 804 asyncflag = mp->mnt_flag & MNT_ASYNC; 805 mp->mnt_flag &= ~MNT_ASYNC; 806 VFS_SYNC(mp, MNT_NOWAIT, l->l_cred); 807 if (asyncflag) 808 mp->mnt_flag |= MNT_ASYNC; 809 } 810 mutex_exit(&mp->mnt_updating); 811 vfs_unbusy(mp, false, &nmp); 812 } 813 mutex_exit(&mountlist_lock); 814 #ifdef DEBUG 815 if (syncprt) 816 vfs_bufstats(); 817 #endif /* DEBUG */ 818 return (0); 819 } 820 821 /* 822 * Change filesystem quotas. 823 */ 824 /* ARGSUSED */ 825 int 826 sys_quotactl(struct lwp *l, const struct sys_quotactl_args *uap, register_t *retval) 827 { 828 /* { 829 syscallarg(const char *) path; 830 syscallarg(int) cmd; 831 syscallarg(int) uid; 832 syscallarg(void *) arg; 833 } */ 834 struct mount *mp; 835 int error; 836 struct vnode *vp; 837 838 error = namei_simple_user(SCARG(uap, path), 839 NSM_FOLLOW_TRYEMULROOT, &vp); 840 if (error != 0) 841 return (error); 842 mp = vp->v_mount; 843 error = VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid), 844 SCARG(uap, arg)); 845 vrele(vp); 846 return (error); 847 } 848 849 int 850 dostatvfs(struct mount *mp, struct statvfs *sp, struct lwp *l, int flags, 851 int root) 852 { 853 struct cwdinfo *cwdi = l->l_proc->p_cwdi; 854 int error = 0; 855 856 /* 857 * If MNT_NOWAIT or MNT_LAZY is specified, do not 858 * refresh the fsstat cache. MNT_WAIT or MNT_LAZY 859 * overrides MNT_NOWAIT. 860 */ 861 if (flags == MNT_NOWAIT || flags == MNT_LAZY || 862 (flags != MNT_WAIT && flags != 0)) { 863 memcpy(sp, &mp->mnt_stat, sizeof(*sp)); 864 goto done; 865 } 866 867 /* Get the filesystem stats now */ 868 memset(sp, 0, sizeof(*sp)); 869 if ((error = VFS_STATVFS(mp, sp)) != 0) { 870 return error; 871 } 872 873 if (cwdi->cwdi_rdir == NULL) 874 (void)memcpy(&mp->mnt_stat, sp, sizeof(mp->mnt_stat)); 875 done: 876 if (cwdi->cwdi_rdir != NULL) { 877 size_t len; 878 char *bp; 879 char c; 880 char *path = PNBUF_GET(); 881 882 bp = path + MAXPATHLEN; 883 *--bp = '\0'; 884 rw_enter(&cwdi->cwdi_lock, RW_READER); 885 error = getcwd_common(cwdi->cwdi_rdir, rootvnode, &bp, path, 886 MAXPATHLEN / 2, 0, l); 887 rw_exit(&cwdi->cwdi_lock); 888 if (error) { 889 PNBUF_PUT(path); 890 return error; 891 } 892 len = strlen(bp); 893 if (len != 1) { 894 /* 895 * for mount points that are below our root, we can see 896 * them, so we fix up the pathname and return them. The 897 * rest we cannot see, so we don't allow viewing the 898 * data. 899 */ 900 if (strncmp(bp, sp->f_mntonname, len) == 0 && 901 ((c = sp->f_mntonname[len]) == '/' || c == '\0')) { 902 (void)strlcpy(sp->f_mntonname, 903 c == '\0' ? "/" : &sp->f_mntonname[len], 904 sizeof(sp->f_mntonname)); 905 } else { 906 if (root) 907 (void)strlcpy(sp->f_mntonname, "/", 908 sizeof(sp->f_mntonname)); 909 else 910 error = EPERM; 911 } 912 } 913 PNBUF_PUT(path); 914 } 915 sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK; 916 return error; 917 } 918 919 /* 920 * Get filesystem statistics by path. 921 */ 922 int 923 do_sys_pstatvfs(struct lwp *l, const char *path, int flags, struct statvfs *sb) 924 { 925 struct mount *mp; 926 int error; 927 struct vnode *vp; 928 929 error = namei_simple_user(path, NSM_FOLLOW_TRYEMULROOT, &vp); 930 if (error != 0) 931 return error; 932 mp = vp->v_mount; 933 error = dostatvfs(mp, sb, l, flags, 1); 934 vrele(vp); 935 return error; 936 } 937 938 /* ARGSUSED */ 939 int 940 sys_statvfs1(struct lwp *l, const struct sys_statvfs1_args *uap, register_t *retval) 941 { 942 /* { 943 syscallarg(const char *) path; 944 syscallarg(struct statvfs *) buf; 945 syscallarg(int) flags; 946 } */ 947 struct statvfs *sb; 948 int error; 949 950 sb = STATVFSBUF_GET(); 951 error = do_sys_pstatvfs(l, SCARG(uap, path), SCARG(uap, flags), sb); 952 if (error == 0) 953 error = copyout(sb, SCARG(uap, buf), sizeof(*sb)); 954 STATVFSBUF_PUT(sb); 955 return error; 956 } 957 958 /* 959 * Get filesystem statistics by fd. 960 */ 961 int 962 do_sys_fstatvfs(struct lwp *l, int fd, int flags, struct statvfs *sb) 963 { 964 file_t *fp; 965 struct mount *mp; 966 int error; 967 968 /* fd_getvnode() will use the descriptor for us */ 969 if ((error = fd_getvnode(fd, &fp)) != 0) 970 return (error); 971 mp = ((struct vnode *)fp->f_data)->v_mount; 972 error = dostatvfs(mp, sb, curlwp, flags, 1); 973 fd_putfile(fd); 974 return error; 975 } 976 977 /* ARGSUSED */ 978 int 979 sys_fstatvfs1(struct lwp *l, const struct sys_fstatvfs1_args *uap, register_t *retval) 980 { 981 /* { 982 syscallarg(int) fd; 983 syscallarg(struct statvfs *) buf; 984 syscallarg(int) flags; 985 } */ 986 struct statvfs *sb; 987 int error; 988 989 sb = STATVFSBUF_GET(); 990 error = do_sys_fstatvfs(l, SCARG(uap, fd), SCARG(uap, flags), sb); 991 if (error == 0) 992 error = copyout(sb, SCARG(uap, buf), sizeof(*sb)); 993 STATVFSBUF_PUT(sb); 994 return error; 995 } 996 997 998 /* 999 * Get statistics on all filesystems. 1000 */ 1001 int 1002 do_sys_getvfsstat(struct lwp *l, void *sfsp, size_t bufsize, int flags, 1003 int (*copyfn)(const void *, void *, size_t), size_t entry_sz, 1004 register_t *retval) 1005 { 1006 int root = 0; 1007 struct proc *p = l->l_proc; 1008 struct mount *mp, *nmp; 1009 struct statvfs *sb; 1010 size_t count, maxcount; 1011 int error = 0; 1012 1013 sb = STATVFSBUF_GET(); 1014 maxcount = bufsize / entry_sz; 1015 mutex_enter(&mountlist_lock); 1016 count = 0; 1017 for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist; 1018 mp = nmp) { 1019 if (vfs_busy(mp, &nmp)) { 1020 continue; 1021 } 1022 if (sfsp && count < maxcount) { 1023 error = dostatvfs(mp, sb, l, flags, 0); 1024 if (error) { 1025 vfs_unbusy(mp, false, &nmp); 1026 error = 0; 1027 continue; 1028 } 1029 error = copyfn(sb, sfsp, entry_sz); 1030 if (error) { 1031 vfs_unbusy(mp, false, NULL); 1032 goto out; 1033 } 1034 sfsp = (char *)sfsp + entry_sz; 1035 root |= strcmp(sb->f_mntonname, "/") == 0; 1036 } 1037 count++; 1038 vfs_unbusy(mp, false, &nmp); 1039 } 1040 mutex_exit(&mountlist_lock); 1041 1042 if (root == 0 && p->p_cwdi->cwdi_rdir) { 1043 /* 1044 * fake a root entry 1045 */ 1046 error = dostatvfs(p->p_cwdi->cwdi_rdir->v_mount, 1047 sb, l, flags, 1); 1048 if (error != 0) 1049 goto out; 1050 if (sfsp) { 1051 error = copyfn(sb, sfsp, entry_sz); 1052 if (error != 0) 1053 goto out; 1054 } 1055 count++; 1056 } 1057 if (sfsp && count > maxcount) 1058 *retval = maxcount; 1059 else 1060 *retval = count; 1061 out: 1062 STATVFSBUF_PUT(sb); 1063 return error; 1064 } 1065 1066 int 1067 sys_getvfsstat(struct lwp *l, const struct sys_getvfsstat_args *uap, register_t *retval) 1068 { 1069 /* { 1070 syscallarg(struct statvfs *) buf; 1071 syscallarg(size_t) bufsize; 1072 syscallarg(int) flags; 1073 } */ 1074 1075 return do_sys_getvfsstat(l, SCARG(uap, buf), SCARG(uap, bufsize), 1076 SCARG(uap, flags), copyout, sizeof (struct statvfs), retval); 1077 } 1078 1079 /* 1080 * Change current working directory to a given file descriptor. 1081 */ 1082 /* ARGSUSED */ 1083 int 1084 sys_fchdir(struct lwp *l, const struct sys_fchdir_args *uap, register_t *retval) 1085 { 1086 /* { 1087 syscallarg(int) fd; 1088 } */ 1089 struct proc *p = l->l_proc; 1090 struct cwdinfo *cwdi; 1091 struct vnode *vp, *tdp; 1092 struct mount *mp; 1093 file_t *fp; 1094 int error, fd; 1095 1096 /* fd_getvnode() will use the descriptor for us */ 1097 fd = SCARG(uap, fd); 1098 if ((error = fd_getvnode(fd, &fp)) != 0) 1099 return (error); 1100 vp = fp->f_data; 1101 1102 vref(vp); 1103 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1104 if (vp->v_type != VDIR) 1105 error = ENOTDIR; 1106 else 1107 error = VOP_ACCESS(vp, VEXEC, l->l_cred); 1108 if (error) { 1109 vput(vp); 1110 goto out; 1111 } 1112 while ((mp = vp->v_mountedhere) != NULL) { 1113 error = vfs_busy(mp, NULL); 1114 vput(vp); 1115 if (error != 0) 1116 goto out; 1117 error = VFS_ROOT(mp, &tdp); 1118 vfs_unbusy(mp, false, NULL); 1119 if (error) 1120 goto out; 1121 vp = tdp; 1122 } 1123 VOP_UNLOCK(vp, 0); 1124 1125 /* 1126 * Disallow changing to a directory not under the process's 1127 * current root directory (if there is one). 1128 */ 1129 cwdi = p->p_cwdi; 1130 rw_enter(&cwdi->cwdi_lock, RW_WRITER); 1131 if (cwdi->cwdi_rdir && !vn_isunder(vp, NULL, l)) { 1132 vrele(vp); 1133 error = EPERM; /* operation not permitted */ 1134 } else { 1135 vrele(cwdi->cwdi_cdir); 1136 cwdi->cwdi_cdir = vp; 1137 } 1138 rw_exit(&cwdi->cwdi_lock); 1139 1140 out: 1141 fd_putfile(fd); 1142 return (error); 1143 } 1144 1145 /* 1146 * Change this process's notion of the root directory to a given file 1147 * descriptor. 1148 */ 1149 int 1150 sys_fchroot(struct lwp *l, const struct sys_fchroot_args *uap, register_t *retval) 1151 { 1152 struct proc *p = l->l_proc; 1153 struct vnode *vp; 1154 file_t *fp; 1155 int error, fd = SCARG(uap, fd); 1156 1157 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_CHROOT, 1158 KAUTH_REQ_SYSTEM_CHROOT_FCHROOT, NULL, NULL, NULL)) != 0) 1159 return error; 1160 /* fd_getvnode() will use the descriptor for us */ 1161 if ((error = fd_getvnode(fd, &fp)) != 0) 1162 return error; 1163 vp = fp->f_data; 1164 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1165 if (vp->v_type != VDIR) 1166 error = ENOTDIR; 1167 else 1168 error = VOP_ACCESS(vp, VEXEC, l->l_cred); 1169 VOP_UNLOCK(vp, 0); 1170 if (error) 1171 goto out; 1172 vref(vp); 1173 1174 change_root(p->p_cwdi, vp, l); 1175 1176 out: 1177 fd_putfile(fd); 1178 return (error); 1179 } 1180 1181 /* 1182 * Change current working directory (``.''). 1183 */ 1184 /* ARGSUSED */ 1185 int 1186 sys_chdir(struct lwp *l, const struct sys_chdir_args *uap, register_t *retval) 1187 { 1188 /* { 1189 syscallarg(const char *) path; 1190 } */ 1191 struct proc *p = l->l_proc; 1192 struct cwdinfo *cwdi; 1193 int error; 1194 struct vnode *vp; 1195 1196 if ((error = chdir_lookup(SCARG(uap, path), UIO_USERSPACE, 1197 &vp, l)) != 0) 1198 return (error); 1199 cwdi = p->p_cwdi; 1200 rw_enter(&cwdi->cwdi_lock, RW_WRITER); 1201 vrele(cwdi->cwdi_cdir); 1202 cwdi->cwdi_cdir = vp; 1203 rw_exit(&cwdi->cwdi_lock); 1204 return (0); 1205 } 1206 1207 /* 1208 * Change notion of root (``/'') directory. 1209 */ 1210 /* ARGSUSED */ 1211 int 1212 sys_chroot(struct lwp *l, const struct sys_chroot_args *uap, register_t *retval) 1213 { 1214 /* { 1215 syscallarg(const char *) path; 1216 } */ 1217 struct proc *p = l->l_proc; 1218 int error; 1219 struct vnode *vp; 1220 1221 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_CHROOT, 1222 KAUTH_REQ_SYSTEM_CHROOT_CHROOT, NULL, NULL, NULL)) != 0) 1223 return (error); 1224 if ((error = chdir_lookup(SCARG(uap, path), UIO_USERSPACE, 1225 &vp, l)) != 0) 1226 return (error); 1227 1228 change_root(p->p_cwdi, vp, l); 1229 1230 return (0); 1231 } 1232 1233 /* 1234 * Common routine for chroot and fchroot. 1235 * NB: callers need to properly authorize the change root operation. 1236 */ 1237 void 1238 change_root(struct cwdinfo *cwdi, struct vnode *vp, struct lwp *l) 1239 { 1240 1241 rw_enter(&cwdi->cwdi_lock, RW_WRITER); 1242 if (cwdi->cwdi_rdir != NULL) 1243 vrele(cwdi->cwdi_rdir); 1244 cwdi->cwdi_rdir = vp; 1245 1246 /* 1247 * Prevent escaping from chroot by putting the root under 1248 * the working directory. Silently chdir to / if we aren't 1249 * already there. 1250 */ 1251 if (!vn_isunder(cwdi->cwdi_cdir, vp, l)) { 1252 /* 1253 * XXX would be more failsafe to change directory to a 1254 * deadfs node here instead 1255 */ 1256 vrele(cwdi->cwdi_cdir); 1257 vref(vp); 1258 cwdi->cwdi_cdir = vp; 1259 } 1260 rw_exit(&cwdi->cwdi_lock); 1261 } 1262 1263 /* 1264 * Common routine for chroot and chdir. 1265 */ 1266 int 1267 chdir_lookup(const char *path, int where, struct vnode **vpp, struct lwp *l) 1268 { 1269 struct nameidata nd; 1270 int error; 1271 1272 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, where, 1273 path); 1274 if ((error = namei(&nd)) != 0) 1275 return (error); 1276 *vpp = nd.ni_vp; 1277 if ((*vpp)->v_type != VDIR) 1278 error = ENOTDIR; 1279 else 1280 error = VOP_ACCESS(*vpp, VEXEC, l->l_cred); 1281 1282 if (error) 1283 vput(*vpp); 1284 else 1285 VOP_UNLOCK(*vpp, 0); 1286 return (error); 1287 } 1288 1289 /* 1290 * Check permissions, allocate an open file structure, 1291 * and call the device open routine if any. 1292 */ 1293 int 1294 sys_open(struct lwp *l, const struct sys_open_args *uap, register_t *retval) 1295 { 1296 /* { 1297 syscallarg(const char *) path; 1298 syscallarg(int) flags; 1299 syscallarg(int) mode; 1300 } */ 1301 struct proc *p = l->l_proc; 1302 struct cwdinfo *cwdi = p->p_cwdi; 1303 file_t *fp; 1304 struct vnode *vp; 1305 int flags, cmode; 1306 int type, indx, error; 1307 struct flock lf; 1308 struct nameidata nd; 1309 1310 flags = FFLAGS(SCARG(uap, flags)); 1311 if ((flags & (FREAD | FWRITE)) == 0) 1312 return (EINVAL); 1313 if ((error = fd_allocfile(&fp, &indx)) != 0) 1314 return (error); 1315 /* We're going to read cwdi->cwdi_cmask unlocked here. */ 1316 cmode = ((SCARG(uap, mode) &~ cwdi->cwdi_cmask) & ALLPERMS) &~ S_ISTXT; 1317 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE, 1318 SCARG(uap, path)); 1319 l->l_dupfd = -indx - 1; /* XXX check for fdopen */ 1320 if ((error = vn_open(&nd, flags, cmode)) != 0) { 1321 fd_abort(p, fp, indx); 1322 if ((error == EDUPFD || error == EMOVEFD) && 1323 l->l_dupfd >= 0 && /* XXX from fdopen */ 1324 (error = 1325 fd_dupopen(l->l_dupfd, &indx, flags, error)) == 0) { 1326 *retval = indx; 1327 return (0); 1328 } 1329 if (error == ERESTART) 1330 error = EINTR; 1331 return (error); 1332 } 1333 1334 l->l_dupfd = 0; 1335 vp = nd.ni_vp; 1336 fp->f_flag = flags & FMASK; 1337 fp->f_type = DTYPE_VNODE; 1338 fp->f_ops = &vnops; 1339 fp->f_data = vp; 1340 if (flags & (O_EXLOCK | O_SHLOCK)) { 1341 lf.l_whence = SEEK_SET; 1342 lf.l_start = 0; 1343 lf.l_len = 0; 1344 if (flags & O_EXLOCK) 1345 lf.l_type = F_WRLCK; 1346 else 1347 lf.l_type = F_RDLCK; 1348 type = F_FLOCK; 1349 if ((flags & FNONBLOCK) == 0) 1350 type |= F_WAIT; 1351 VOP_UNLOCK(vp, 0); 1352 error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, type); 1353 if (error) { 1354 (void) vn_close(vp, fp->f_flag, fp->f_cred); 1355 fd_abort(p, fp, indx); 1356 return (error); 1357 } 1358 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1359 atomic_or_uint(&fp->f_flag, FHASLOCK); 1360 } 1361 VOP_UNLOCK(vp, 0); 1362 *retval = indx; 1363 fd_affix(p, fp, indx); 1364 return (0); 1365 } 1366 1367 static void 1368 vfs__fhfree(fhandle_t *fhp) 1369 { 1370 size_t fhsize; 1371 1372 if (fhp == NULL) { 1373 return; 1374 } 1375 fhsize = FHANDLE_SIZE(fhp); 1376 kmem_free(fhp, fhsize); 1377 } 1378 1379 /* 1380 * vfs_composefh: compose a filehandle. 1381 */ 1382 1383 int 1384 vfs_composefh(struct vnode *vp, fhandle_t *fhp, size_t *fh_size) 1385 { 1386 struct mount *mp; 1387 struct fid *fidp; 1388 int error; 1389 size_t needfhsize; 1390 size_t fidsize; 1391 1392 mp = vp->v_mount; 1393 fidp = NULL; 1394 if (*fh_size < FHANDLE_SIZE_MIN) { 1395 fidsize = 0; 1396 } else { 1397 fidsize = *fh_size - offsetof(fhandle_t, fh_fid); 1398 if (fhp != NULL) { 1399 memset(fhp, 0, *fh_size); 1400 fhp->fh_fsid = mp->mnt_stat.f_fsidx; 1401 fidp = &fhp->fh_fid; 1402 } 1403 } 1404 error = VFS_VPTOFH(vp, fidp, &fidsize); 1405 needfhsize = FHANDLE_SIZE_FROM_FILEID_SIZE(fidsize); 1406 if (error == 0 && *fh_size < needfhsize) { 1407 error = E2BIG; 1408 } 1409 *fh_size = needfhsize; 1410 return error; 1411 } 1412 1413 int 1414 vfs_composefh_alloc(struct vnode *vp, fhandle_t **fhpp) 1415 { 1416 struct mount *mp; 1417 fhandle_t *fhp; 1418 size_t fhsize; 1419 size_t fidsize; 1420 int error; 1421 1422 *fhpp = NULL; 1423 mp = vp->v_mount; 1424 fidsize = 0; 1425 error = VFS_VPTOFH(vp, NULL, &fidsize); 1426 KASSERT(error != 0); 1427 if (error != E2BIG) { 1428 goto out; 1429 } 1430 fhsize = FHANDLE_SIZE_FROM_FILEID_SIZE(fidsize); 1431 fhp = kmem_zalloc(fhsize, KM_SLEEP); 1432 if (fhp == NULL) { 1433 error = ENOMEM; 1434 goto out; 1435 } 1436 fhp->fh_fsid = mp->mnt_stat.f_fsidx; 1437 error = VFS_VPTOFH(vp, &fhp->fh_fid, &fidsize); 1438 if (error == 0) { 1439 KASSERT((FHANDLE_SIZE(fhp) == fhsize && 1440 FHANDLE_FILEID(fhp)->fid_len == fidsize)); 1441 *fhpp = fhp; 1442 } else { 1443 kmem_free(fhp, fhsize); 1444 } 1445 out: 1446 return error; 1447 } 1448 1449 void 1450 vfs_composefh_free(fhandle_t *fhp) 1451 { 1452 1453 vfs__fhfree(fhp); 1454 } 1455 1456 /* 1457 * vfs_fhtovp: lookup a vnode by a filehandle. 1458 */ 1459 1460 int 1461 vfs_fhtovp(fhandle_t *fhp, struct vnode **vpp) 1462 { 1463 struct mount *mp; 1464 int error; 1465 1466 *vpp = NULL; 1467 mp = vfs_getvfs(FHANDLE_FSID(fhp)); 1468 if (mp == NULL) { 1469 error = ESTALE; 1470 goto out; 1471 } 1472 if (mp->mnt_op->vfs_fhtovp == NULL) { 1473 error = EOPNOTSUPP; 1474 goto out; 1475 } 1476 error = VFS_FHTOVP(mp, FHANDLE_FILEID(fhp), vpp); 1477 out: 1478 return error; 1479 } 1480 1481 /* 1482 * vfs_copyinfh_alloc: allocate and copyin a filehandle, given 1483 * the needed size. 1484 */ 1485 1486 int 1487 vfs_copyinfh_alloc(const void *ufhp, size_t fhsize, fhandle_t **fhpp) 1488 { 1489 fhandle_t *fhp; 1490 int error; 1491 1492 *fhpp = NULL; 1493 if (fhsize > FHANDLE_SIZE_MAX) { 1494 return EINVAL; 1495 } 1496 if (fhsize < FHANDLE_SIZE_MIN) { 1497 return EINVAL; 1498 } 1499 again: 1500 fhp = kmem_alloc(fhsize, KM_SLEEP); 1501 if (fhp == NULL) { 1502 return ENOMEM; 1503 } 1504 error = copyin(ufhp, fhp, fhsize); 1505 if (error == 0) { 1506 /* XXX this check shouldn't be here */ 1507 if (FHANDLE_SIZE(fhp) == fhsize) { 1508 *fhpp = fhp; 1509 return 0; 1510 } else if (fhsize == NFSX_V2FH && FHANDLE_SIZE(fhp) < fhsize) { 1511 /* 1512 * a kludge for nfsv2 padded handles. 1513 */ 1514 size_t sz; 1515 1516 sz = FHANDLE_SIZE(fhp); 1517 kmem_free(fhp, fhsize); 1518 fhsize = sz; 1519 goto again; 1520 } else { 1521 /* 1522 * userland told us wrong size. 1523 */ 1524 error = EINVAL; 1525 } 1526 } 1527 kmem_free(fhp, fhsize); 1528 return error; 1529 } 1530 1531 void 1532 vfs_copyinfh_free(fhandle_t *fhp) 1533 { 1534 1535 vfs__fhfree(fhp); 1536 } 1537 1538 /* 1539 * Get file handle system call 1540 */ 1541 int 1542 sys___getfh30(struct lwp *l, const struct sys___getfh30_args *uap, register_t *retval) 1543 { 1544 /* { 1545 syscallarg(char *) fname; 1546 syscallarg(fhandle_t *) fhp; 1547 syscallarg(size_t *) fh_size; 1548 } */ 1549 struct vnode *vp; 1550 fhandle_t *fh; 1551 int error; 1552 struct nameidata nd; 1553 size_t sz; 1554 size_t usz; 1555 1556 /* 1557 * Must be super user 1558 */ 1559 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE, 1560 0, NULL, NULL, NULL); 1561 if (error) 1562 return (error); 1563 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE, 1564 SCARG(uap, fname)); 1565 error = namei(&nd); 1566 if (error) 1567 return (error); 1568 vp = nd.ni_vp; 1569 error = vfs_composefh_alloc(vp, &fh); 1570 vput(vp); 1571 if (error != 0) { 1572 goto out; 1573 } 1574 error = copyin(SCARG(uap, fh_size), &usz, sizeof(size_t)); 1575 if (error != 0) { 1576 goto out; 1577 } 1578 sz = FHANDLE_SIZE(fh); 1579 error = copyout(&sz, SCARG(uap, fh_size), sizeof(size_t)); 1580 if (error != 0) { 1581 goto out; 1582 } 1583 if (usz >= sz) { 1584 error = copyout(fh, SCARG(uap, fhp), sz); 1585 } else { 1586 error = E2BIG; 1587 } 1588 out: 1589 vfs_composefh_free(fh); 1590 return (error); 1591 } 1592 1593 /* 1594 * Open a file given a file handle. 1595 * 1596 * Check permissions, allocate an open file structure, 1597 * and call the device open routine if any. 1598 */ 1599 1600 int 1601 dofhopen(struct lwp *l, const void *ufhp, size_t fhsize, int oflags, 1602 register_t *retval) 1603 { 1604 file_t *fp; 1605 struct vnode *vp = NULL; 1606 kauth_cred_t cred = l->l_cred; 1607 file_t *nfp; 1608 int type, indx, error=0; 1609 struct flock lf; 1610 struct vattr va; 1611 fhandle_t *fh; 1612 int flags; 1613 proc_t *p; 1614 1615 p = curproc; 1616 1617 /* 1618 * Must be super user 1619 */ 1620 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE, 1621 0, NULL, NULL, NULL))) 1622 return (error); 1623 1624 flags = FFLAGS(oflags); 1625 if ((flags & (FREAD | FWRITE)) == 0) 1626 return (EINVAL); 1627 if ((flags & O_CREAT)) 1628 return (EINVAL); 1629 if ((error = fd_allocfile(&nfp, &indx)) != 0) 1630 return (error); 1631 fp = nfp; 1632 error = vfs_copyinfh_alloc(ufhp, fhsize, &fh); 1633 if (error != 0) { 1634 goto bad; 1635 } 1636 error = vfs_fhtovp(fh, &vp); 1637 if (error != 0) { 1638 goto bad; 1639 } 1640 1641 /* Now do an effective vn_open */ 1642 1643 if (vp->v_type == VSOCK) { 1644 error = EOPNOTSUPP; 1645 goto bad; 1646 } 1647 error = vn_openchk(vp, cred, flags); 1648 if (error != 0) 1649 goto bad; 1650 if (flags & O_TRUNC) { 1651 VOP_UNLOCK(vp, 0); /* XXX */ 1652 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */ 1653 vattr_null(&va); 1654 va.va_size = 0; 1655 error = VOP_SETATTR(vp, &va, cred); 1656 if (error) 1657 goto bad; 1658 } 1659 if ((error = VOP_OPEN(vp, flags, cred)) != 0) 1660 goto bad; 1661 if (flags & FWRITE) { 1662 mutex_enter(&vp->v_interlock); 1663 vp->v_writecount++; 1664 mutex_exit(&vp->v_interlock); 1665 } 1666 1667 /* done with modified vn_open, now finish what sys_open does. */ 1668 1669 fp->f_flag = flags & FMASK; 1670 fp->f_type = DTYPE_VNODE; 1671 fp->f_ops = &vnops; 1672 fp->f_data = vp; 1673 if (flags & (O_EXLOCK | O_SHLOCK)) { 1674 lf.l_whence = SEEK_SET; 1675 lf.l_start = 0; 1676 lf.l_len = 0; 1677 if (flags & O_EXLOCK) 1678 lf.l_type = F_WRLCK; 1679 else 1680 lf.l_type = F_RDLCK; 1681 type = F_FLOCK; 1682 if ((flags & FNONBLOCK) == 0) 1683 type |= F_WAIT; 1684 VOP_UNLOCK(vp, 0); 1685 error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, type); 1686 if (error) { 1687 (void) vn_close(vp, fp->f_flag, fp->f_cred); 1688 fd_abort(p, fp, indx); 1689 return (error); 1690 } 1691 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1692 atomic_or_uint(&fp->f_flag, FHASLOCK); 1693 } 1694 VOP_UNLOCK(vp, 0); 1695 *retval = indx; 1696 fd_affix(p, fp, indx); 1697 vfs_copyinfh_free(fh); 1698 return (0); 1699 1700 bad: 1701 fd_abort(p, fp, indx); 1702 if (vp != NULL) 1703 vput(vp); 1704 vfs_copyinfh_free(fh); 1705 return (error); 1706 } 1707 1708 int 1709 sys___fhopen40(struct lwp *l, const struct sys___fhopen40_args *uap, register_t *retval) 1710 { 1711 /* { 1712 syscallarg(const void *) fhp; 1713 syscallarg(size_t) fh_size; 1714 syscallarg(int) flags; 1715 } */ 1716 1717 return dofhopen(l, SCARG(uap, fhp), SCARG(uap, fh_size), 1718 SCARG(uap, flags), retval); 1719 } 1720 1721 int 1722 do_fhstat(struct lwp *l, const void *ufhp, size_t fhsize, struct stat *sb) 1723 { 1724 int error; 1725 fhandle_t *fh; 1726 struct vnode *vp; 1727 1728 /* 1729 * Must be super user 1730 */ 1731 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE, 1732 0, NULL, NULL, NULL))) 1733 return (error); 1734 1735 error = vfs_copyinfh_alloc(ufhp, fhsize, &fh); 1736 if (error != 0) 1737 return error; 1738 1739 error = vfs_fhtovp(fh, &vp); 1740 vfs_copyinfh_free(fh); 1741 if (error != 0) 1742 return error; 1743 1744 error = vn_stat(vp, sb); 1745 vput(vp); 1746 return error; 1747 } 1748 1749 1750 /* ARGSUSED */ 1751 int 1752 sys___fhstat50(struct lwp *l, const struct sys___fhstat50_args *uap, register_t *retval) 1753 { 1754 /* { 1755 syscallarg(const void *) fhp; 1756 syscallarg(size_t) fh_size; 1757 syscallarg(struct stat *) sb; 1758 } */ 1759 struct stat sb; 1760 int error; 1761 1762 error = do_fhstat(l, SCARG(uap, fhp), SCARG(uap, fh_size), &sb); 1763 if (error) 1764 return error; 1765 return copyout(&sb, SCARG(uap, sb), sizeof(sb)); 1766 } 1767 1768 int 1769 do_fhstatvfs(struct lwp *l, const void *ufhp, size_t fhsize, struct statvfs *sb, 1770 int flags) 1771 { 1772 fhandle_t *fh; 1773 struct mount *mp; 1774 struct vnode *vp; 1775 int error; 1776 1777 /* 1778 * Must be super user 1779 */ 1780 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE, 1781 0, NULL, NULL, NULL))) 1782 return error; 1783 1784 error = vfs_copyinfh_alloc(ufhp, fhsize, &fh); 1785 if (error != 0) 1786 return error; 1787 1788 error = vfs_fhtovp(fh, &vp); 1789 vfs_copyinfh_free(fh); 1790 if (error != 0) 1791 return error; 1792 1793 mp = vp->v_mount; 1794 error = dostatvfs(mp, sb, l, flags, 1); 1795 vput(vp); 1796 return error; 1797 } 1798 1799 /* ARGSUSED */ 1800 int 1801 sys___fhstatvfs140(struct lwp *l, const struct sys___fhstatvfs140_args *uap, register_t *retval) 1802 { 1803 /* { 1804 syscallarg(const void *) fhp; 1805 syscallarg(size_t) fh_size; 1806 syscallarg(struct statvfs *) buf; 1807 syscallarg(int) flags; 1808 } */ 1809 struct statvfs *sb = STATVFSBUF_GET(); 1810 int error; 1811 1812 error = do_fhstatvfs(l, SCARG(uap, fhp), SCARG(uap, fh_size), sb, 1813 SCARG(uap, flags)); 1814 if (error == 0) 1815 error = copyout(sb, SCARG(uap, buf), sizeof(*sb)); 1816 STATVFSBUF_PUT(sb); 1817 return error; 1818 } 1819 1820 /* 1821 * Create a special file. 1822 */ 1823 /* ARGSUSED */ 1824 int 1825 sys___mknod50(struct lwp *l, const struct sys___mknod50_args *uap, 1826 register_t *retval) 1827 { 1828 /* { 1829 syscallarg(const char *) path; 1830 syscallarg(mode_t) mode; 1831 syscallarg(dev_t) dev; 1832 } */ 1833 return do_sys_mknod(l, SCARG(uap, path), SCARG(uap, mode), 1834 SCARG(uap, dev), retval, UIO_USERSPACE); 1835 } 1836 1837 int 1838 do_sys_mknod(struct lwp *l, const char *pathname, mode_t mode, dev_t dev, 1839 register_t *retval, enum uio_seg seg) 1840 { 1841 struct proc *p = l->l_proc; 1842 struct vnode *vp; 1843 struct vattr vattr; 1844 int error, optype; 1845 struct nameidata nd; 1846 char *path; 1847 const char *cpath; 1848 1849 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MKNOD, 1850 0, NULL, NULL, NULL)) != 0) 1851 return (error); 1852 1853 optype = VOP_MKNOD_DESCOFFSET; 1854 1855 VERIEXEC_PATH_GET(pathname, seg, cpath, path); 1856 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, seg, cpath); 1857 1858 if ((error = namei(&nd)) != 0) 1859 goto out; 1860 vp = nd.ni_vp; 1861 if (vp != NULL) 1862 error = EEXIST; 1863 else { 1864 vattr_null(&vattr); 1865 /* We will read cwdi->cwdi_cmask unlocked. */ 1866 vattr.va_mode = (mode & ALLPERMS) &~ p->p_cwdi->cwdi_cmask; 1867 vattr.va_rdev = dev; 1868 1869 switch (mode & S_IFMT) { 1870 case S_IFMT: /* used by badsect to flag bad sectors */ 1871 vattr.va_type = VBAD; 1872 break; 1873 case S_IFCHR: 1874 vattr.va_type = VCHR; 1875 break; 1876 case S_IFBLK: 1877 vattr.va_type = VBLK; 1878 break; 1879 case S_IFWHT: 1880 optype = VOP_WHITEOUT_DESCOFFSET; 1881 break; 1882 case S_IFREG: 1883 #if NVERIEXEC > 0 1884 error = veriexec_openchk(l, nd.ni_vp, nd.ni_dirp, 1885 O_CREAT); 1886 #endif /* NVERIEXEC > 0 */ 1887 vattr.va_type = VREG; 1888 vattr.va_rdev = VNOVAL; 1889 optype = VOP_CREATE_DESCOFFSET; 1890 break; 1891 default: 1892 error = EINVAL; 1893 break; 1894 } 1895 } 1896 if (!error) { 1897 switch (optype) { 1898 case VOP_WHITEOUT_DESCOFFSET: 1899 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE); 1900 if (error) 1901 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1902 vput(nd.ni_dvp); 1903 break; 1904 1905 case VOP_MKNOD_DESCOFFSET: 1906 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, 1907 &nd.ni_cnd, &vattr); 1908 if (error == 0) 1909 vput(nd.ni_vp); 1910 break; 1911 1912 case VOP_CREATE_DESCOFFSET: 1913 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, 1914 &nd.ni_cnd, &vattr); 1915 if (error == 0) 1916 vput(nd.ni_vp); 1917 break; 1918 } 1919 } else { 1920 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1921 if (nd.ni_dvp == vp) 1922 vrele(nd.ni_dvp); 1923 else 1924 vput(nd.ni_dvp); 1925 if (vp) 1926 vrele(vp); 1927 } 1928 out: 1929 VERIEXEC_PATH_PUT(path); 1930 return (error); 1931 } 1932 1933 /* 1934 * Create a named pipe. 1935 */ 1936 /* ARGSUSED */ 1937 int 1938 sys_mkfifo(struct lwp *l, const struct sys_mkfifo_args *uap, register_t *retval) 1939 { 1940 /* { 1941 syscallarg(const char *) path; 1942 syscallarg(int) mode; 1943 } */ 1944 struct proc *p = l->l_proc; 1945 struct vattr vattr; 1946 int error; 1947 struct nameidata nd; 1948 1949 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, UIO_USERSPACE, 1950 SCARG(uap, path)); 1951 if ((error = namei(&nd)) != 0) 1952 return (error); 1953 if (nd.ni_vp != NULL) { 1954 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1955 if (nd.ni_dvp == nd.ni_vp) 1956 vrele(nd.ni_dvp); 1957 else 1958 vput(nd.ni_dvp); 1959 vrele(nd.ni_vp); 1960 return (EEXIST); 1961 } 1962 vattr_null(&vattr); 1963 vattr.va_type = VFIFO; 1964 /* We will read cwdi->cwdi_cmask unlocked. */ 1965 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask; 1966 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 1967 if (error == 0) 1968 vput(nd.ni_vp); 1969 return (error); 1970 } 1971 1972 /* 1973 * Make a hard file link. 1974 */ 1975 /* ARGSUSED */ 1976 int 1977 sys_link(struct lwp *l, const struct sys_link_args *uap, register_t *retval) 1978 { 1979 /* { 1980 syscallarg(const char *) path; 1981 syscallarg(const char *) link; 1982 } */ 1983 struct vnode *vp; 1984 struct nameidata nd; 1985 int error; 1986 1987 error = namei_simple_user(SCARG(uap, path), 1988 NSM_FOLLOW_TRYEMULROOT, &vp); 1989 if (error != 0) 1990 return (error); 1991 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, UIO_USERSPACE, 1992 SCARG(uap, link)); 1993 if ((error = namei(&nd)) != 0) 1994 goto out; 1995 if (nd.ni_vp) { 1996 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1997 if (nd.ni_dvp == nd.ni_vp) 1998 vrele(nd.ni_dvp); 1999 else 2000 vput(nd.ni_dvp); 2001 vrele(nd.ni_vp); 2002 error = EEXIST; 2003 goto out; 2004 } 2005 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd); 2006 out: 2007 vrele(vp); 2008 return (error); 2009 } 2010 2011 /* 2012 * Make a symbolic link. 2013 */ 2014 /* ARGSUSED */ 2015 int 2016 sys_symlink(struct lwp *l, const struct sys_symlink_args *uap, register_t *retval) 2017 { 2018 /* { 2019 syscallarg(const char *) path; 2020 syscallarg(const char *) link; 2021 } */ 2022 struct proc *p = l->l_proc; 2023 struct vattr vattr; 2024 char *path; 2025 int error; 2026 struct nameidata nd; 2027 2028 path = PNBUF_GET(); 2029 error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL); 2030 if (error) 2031 goto out; 2032 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, UIO_USERSPACE, 2033 SCARG(uap, link)); 2034 if ((error = namei(&nd)) != 0) 2035 goto out; 2036 if (nd.ni_vp) { 2037 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 2038 if (nd.ni_dvp == nd.ni_vp) 2039 vrele(nd.ni_dvp); 2040 else 2041 vput(nd.ni_dvp); 2042 vrele(nd.ni_vp); 2043 error = EEXIST; 2044 goto out; 2045 } 2046 vattr_null(&vattr); 2047 vattr.va_type = VLNK; 2048 /* We will read cwdi->cwdi_cmask unlocked. */ 2049 vattr.va_mode = ACCESSPERMS &~ p->p_cwdi->cwdi_cmask; 2050 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path); 2051 if (error == 0) 2052 vput(nd.ni_vp); 2053 out: 2054 PNBUF_PUT(path); 2055 return (error); 2056 } 2057 2058 /* 2059 * Delete a whiteout from the filesystem. 2060 */ 2061 /* ARGSUSED */ 2062 int 2063 sys_undelete(struct lwp *l, const struct sys_undelete_args *uap, register_t *retval) 2064 { 2065 /* { 2066 syscallarg(const char *) path; 2067 } */ 2068 int error; 2069 struct nameidata nd; 2070 2071 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | TRYEMULROOT, 2072 UIO_USERSPACE, SCARG(uap, path)); 2073 error = namei(&nd); 2074 if (error) 2075 return (error); 2076 2077 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) { 2078 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 2079 if (nd.ni_dvp == nd.ni_vp) 2080 vrele(nd.ni_dvp); 2081 else 2082 vput(nd.ni_dvp); 2083 if (nd.ni_vp) 2084 vrele(nd.ni_vp); 2085 return (EEXIST); 2086 } 2087 if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0) 2088 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 2089 vput(nd.ni_dvp); 2090 return (error); 2091 } 2092 2093 /* 2094 * Delete a name from the filesystem. 2095 */ 2096 /* ARGSUSED */ 2097 int 2098 sys_unlink(struct lwp *l, const struct sys_unlink_args *uap, register_t *retval) 2099 { 2100 /* { 2101 syscallarg(const char *) path; 2102 } */ 2103 2104 return do_sys_unlink(SCARG(uap, path), UIO_USERSPACE); 2105 } 2106 2107 int 2108 do_sys_unlink(const char *arg, enum uio_seg seg) 2109 { 2110 struct vnode *vp; 2111 int error; 2112 struct nameidata nd; 2113 char *path; 2114 const char *cpath; 2115 2116 VERIEXEC_PATH_GET(arg, seg, cpath, path); 2117 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | TRYEMULROOT, seg, cpath); 2118 2119 if ((error = namei(&nd)) != 0) 2120 goto out; 2121 vp = nd.ni_vp; 2122 2123 /* 2124 * The root of a mounted filesystem cannot be deleted. 2125 */ 2126 if (vp->v_vflag & VV_ROOT) { 2127 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 2128 if (nd.ni_dvp == vp) 2129 vrele(nd.ni_dvp); 2130 else 2131 vput(nd.ni_dvp); 2132 vput(vp); 2133 error = EBUSY; 2134 goto out; 2135 } 2136 2137 #if NVERIEXEC > 0 2138 /* Handle remove requests for veriexec entries. */ 2139 if ((error = veriexec_removechk(curlwp, nd.ni_vp, nd.ni_dirp)) != 0) { 2140 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 2141 if (nd.ni_dvp == vp) 2142 vrele(nd.ni_dvp); 2143 else 2144 vput(nd.ni_dvp); 2145 vput(vp); 2146 goto out; 2147 } 2148 #endif /* NVERIEXEC > 0 */ 2149 2150 #ifdef FILEASSOC 2151 (void)fileassoc_file_delete(vp); 2152 #endif /* FILEASSOC */ 2153 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd); 2154 out: 2155 VERIEXEC_PATH_PUT(path); 2156 return (error); 2157 } 2158 2159 /* 2160 * Reposition read/write file offset. 2161 */ 2162 int 2163 sys_lseek(struct lwp *l, const struct sys_lseek_args *uap, register_t *retval) 2164 { 2165 /* { 2166 syscallarg(int) fd; 2167 syscallarg(int) pad; 2168 syscallarg(off_t) offset; 2169 syscallarg(int) whence; 2170 } */ 2171 kauth_cred_t cred = l->l_cred; 2172 file_t *fp; 2173 struct vnode *vp; 2174 struct vattr vattr; 2175 off_t newoff; 2176 int error, fd; 2177 2178 fd = SCARG(uap, fd); 2179 2180 if ((fp = fd_getfile(fd)) == NULL) 2181 return (EBADF); 2182 2183 vp = fp->f_data; 2184 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) { 2185 error = ESPIPE; 2186 goto out; 2187 } 2188 2189 switch (SCARG(uap, whence)) { 2190 case SEEK_CUR: 2191 newoff = fp->f_offset + SCARG(uap, offset); 2192 break; 2193 case SEEK_END: 2194 error = VOP_GETATTR(vp, &vattr, cred); 2195 if (error) { 2196 goto out; 2197 } 2198 newoff = SCARG(uap, offset) + vattr.va_size; 2199 break; 2200 case SEEK_SET: 2201 newoff = SCARG(uap, offset); 2202 break; 2203 default: 2204 error = EINVAL; 2205 goto out; 2206 } 2207 if ((error = VOP_SEEK(vp, fp->f_offset, newoff, cred)) == 0) { 2208 *(off_t *)retval = fp->f_offset = newoff; 2209 } 2210 out: 2211 fd_putfile(fd); 2212 return (error); 2213 } 2214 2215 /* 2216 * Positional read system call. 2217 */ 2218 int 2219 sys_pread(struct lwp *l, const struct sys_pread_args *uap, register_t *retval) 2220 { 2221 /* { 2222 syscallarg(int) fd; 2223 syscallarg(void *) buf; 2224 syscallarg(size_t) nbyte; 2225 syscallarg(off_t) offset; 2226 } */ 2227 file_t *fp; 2228 struct vnode *vp; 2229 off_t offset; 2230 int error, fd = SCARG(uap, fd); 2231 2232 if ((fp = fd_getfile(fd)) == NULL) 2233 return (EBADF); 2234 2235 if ((fp->f_flag & FREAD) == 0) { 2236 fd_putfile(fd); 2237 return (EBADF); 2238 } 2239 2240 vp = fp->f_data; 2241 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) { 2242 error = ESPIPE; 2243 goto out; 2244 } 2245 2246 offset = SCARG(uap, offset); 2247 2248 /* 2249 * XXX This works because no file systems actually 2250 * XXX take any action on the seek operation. 2251 */ 2252 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0) 2253 goto out; 2254 2255 /* dofileread() will unuse the descriptor for us */ 2256 return (dofileread(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte), 2257 &offset, 0, retval)); 2258 2259 out: 2260 fd_putfile(fd); 2261 return (error); 2262 } 2263 2264 /* 2265 * Positional scatter read system call. 2266 */ 2267 int 2268 sys_preadv(struct lwp *l, const struct sys_preadv_args *uap, register_t *retval) 2269 { 2270 /* { 2271 syscallarg(int) fd; 2272 syscallarg(const struct iovec *) iovp; 2273 syscallarg(int) iovcnt; 2274 syscallarg(off_t) offset; 2275 } */ 2276 off_t offset = SCARG(uap, offset); 2277 2278 return do_filereadv(SCARG(uap, fd), SCARG(uap, iovp), 2279 SCARG(uap, iovcnt), &offset, 0, retval); 2280 } 2281 2282 /* 2283 * Positional write system call. 2284 */ 2285 int 2286 sys_pwrite(struct lwp *l, const struct sys_pwrite_args *uap, register_t *retval) 2287 { 2288 /* { 2289 syscallarg(int) fd; 2290 syscallarg(const void *) buf; 2291 syscallarg(size_t) nbyte; 2292 syscallarg(off_t) offset; 2293 } */ 2294 file_t *fp; 2295 struct vnode *vp; 2296 off_t offset; 2297 int error, fd = SCARG(uap, fd); 2298 2299 if ((fp = fd_getfile(fd)) == NULL) 2300 return (EBADF); 2301 2302 if ((fp->f_flag & FWRITE) == 0) { 2303 fd_putfile(fd); 2304 return (EBADF); 2305 } 2306 2307 vp = fp->f_data; 2308 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) { 2309 error = ESPIPE; 2310 goto out; 2311 } 2312 2313 offset = SCARG(uap, offset); 2314 2315 /* 2316 * XXX This works because no file systems actually 2317 * XXX take any action on the seek operation. 2318 */ 2319 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0) 2320 goto out; 2321 2322 /* dofilewrite() will unuse the descriptor for us */ 2323 return (dofilewrite(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte), 2324 &offset, 0, retval)); 2325 2326 out: 2327 fd_putfile(fd); 2328 return (error); 2329 } 2330 2331 /* 2332 * Positional gather write system call. 2333 */ 2334 int 2335 sys_pwritev(struct lwp *l, const struct sys_pwritev_args *uap, register_t *retval) 2336 { 2337 /* { 2338 syscallarg(int) fd; 2339 syscallarg(const struct iovec *) iovp; 2340 syscallarg(int) iovcnt; 2341 syscallarg(off_t) offset; 2342 } */ 2343 off_t offset = SCARG(uap, offset); 2344 2345 return do_filewritev(SCARG(uap, fd), SCARG(uap, iovp), 2346 SCARG(uap, iovcnt), &offset, 0, retval); 2347 } 2348 2349 /* 2350 * Check access permissions. 2351 */ 2352 int 2353 sys_access(struct lwp *l, const struct sys_access_args *uap, register_t *retval) 2354 { 2355 /* { 2356 syscallarg(const char *) path; 2357 syscallarg(int) flags; 2358 } */ 2359 kauth_cred_t cred; 2360 struct vnode *vp; 2361 int error, flags; 2362 struct nameidata nd; 2363 2364 cred = kauth_cred_dup(l->l_cred); 2365 kauth_cred_seteuid(cred, kauth_cred_getuid(l->l_cred)); 2366 kauth_cred_setegid(cred, kauth_cred_getgid(l->l_cred)); 2367 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE, 2368 SCARG(uap, path)); 2369 /* Override default credentials */ 2370 nd.ni_cnd.cn_cred = cred; 2371 if ((error = namei(&nd)) != 0) 2372 goto out; 2373 vp = nd.ni_vp; 2374 2375 /* Flags == 0 means only check for existence. */ 2376 if (SCARG(uap, flags)) { 2377 flags = 0; 2378 if (SCARG(uap, flags) & R_OK) 2379 flags |= VREAD; 2380 if (SCARG(uap, flags) & W_OK) 2381 flags |= VWRITE; 2382 if (SCARG(uap, flags) & X_OK) 2383 flags |= VEXEC; 2384 2385 error = VOP_ACCESS(vp, flags, cred); 2386 if (!error && (flags & VWRITE)) 2387 error = vn_writechk(vp); 2388 } 2389 vput(vp); 2390 out: 2391 kauth_cred_free(cred); 2392 return (error); 2393 } 2394 2395 /* 2396 * Common code for all sys_stat functions, including compat versions. 2397 */ 2398 int 2399 do_sys_stat(const char *path, unsigned int nd_flags, struct stat *sb) 2400 { 2401 int error; 2402 struct nameidata nd; 2403 2404 NDINIT(&nd, LOOKUP, nd_flags | LOCKLEAF | TRYEMULROOT, 2405 UIO_USERSPACE, path); 2406 error = namei(&nd); 2407 if (error != 0) 2408 return error; 2409 error = vn_stat(nd.ni_vp, sb); 2410 vput(nd.ni_vp); 2411 return error; 2412 } 2413 2414 /* 2415 * Get file status; this version follows links. 2416 */ 2417 /* ARGSUSED */ 2418 int 2419 sys___stat50(struct lwp *l, const struct sys___stat50_args *uap, register_t *retval) 2420 { 2421 /* { 2422 syscallarg(const char *) path; 2423 syscallarg(struct stat *) ub; 2424 } */ 2425 struct stat sb; 2426 int error; 2427 2428 error = do_sys_stat(SCARG(uap, path), FOLLOW, &sb); 2429 if (error) 2430 return error; 2431 return copyout(&sb, SCARG(uap, ub), sizeof(sb)); 2432 } 2433 2434 /* 2435 * Get file status; this version does not follow links. 2436 */ 2437 /* ARGSUSED */ 2438 int 2439 sys___lstat50(struct lwp *l, const struct sys___lstat50_args *uap, register_t *retval) 2440 { 2441 /* { 2442 syscallarg(const char *) path; 2443 syscallarg(struct stat *) ub; 2444 } */ 2445 struct stat sb; 2446 int error; 2447 2448 error = do_sys_stat(SCARG(uap, path), NOFOLLOW, &sb); 2449 if (error) 2450 return error; 2451 return copyout(&sb, SCARG(uap, ub), sizeof(sb)); 2452 } 2453 2454 /* 2455 * Get configurable pathname variables. 2456 */ 2457 /* ARGSUSED */ 2458 int 2459 sys_pathconf(struct lwp *l, const struct sys_pathconf_args *uap, register_t *retval) 2460 { 2461 /* { 2462 syscallarg(const char *) path; 2463 syscallarg(int) name; 2464 } */ 2465 int error; 2466 struct nameidata nd; 2467 2468 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE, 2469 SCARG(uap, path)); 2470 if ((error = namei(&nd)) != 0) 2471 return (error); 2472 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval); 2473 vput(nd.ni_vp); 2474 return (error); 2475 } 2476 2477 /* 2478 * Return target name of a symbolic link. 2479 */ 2480 /* ARGSUSED */ 2481 int 2482 sys_readlink(struct lwp *l, const struct sys_readlink_args *uap, register_t *retval) 2483 { 2484 /* { 2485 syscallarg(const char *) path; 2486 syscallarg(char *) buf; 2487 syscallarg(size_t) count; 2488 } */ 2489 struct vnode *vp; 2490 struct iovec aiov; 2491 struct uio auio; 2492 int error; 2493 struct nameidata nd; 2494 2495 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE, 2496 SCARG(uap, path)); 2497 if ((error = namei(&nd)) != 0) 2498 return (error); 2499 vp = nd.ni_vp; 2500 if (vp->v_type != VLNK) 2501 error = EINVAL; 2502 else if (!(vp->v_mount->mnt_flag & MNT_SYMPERM) || 2503 (error = VOP_ACCESS(vp, VREAD, l->l_cred)) == 0) { 2504 aiov.iov_base = SCARG(uap, buf); 2505 aiov.iov_len = SCARG(uap, count); 2506 auio.uio_iov = &aiov; 2507 auio.uio_iovcnt = 1; 2508 auio.uio_offset = 0; 2509 auio.uio_rw = UIO_READ; 2510 KASSERT(l == curlwp); 2511 auio.uio_vmspace = l->l_proc->p_vmspace; 2512 auio.uio_resid = SCARG(uap, count); 2513 error = VOP_READLINK(vp, &auio, l->l_cred); 2514 } 2515 vput(vp); 2516 *retval = SCARG(uap, count) - auio.uio_resid; 2517 return (error); 2518 } 2519 2520 /* 2521 * Change flags of a file given a path name. 2522 */ 2523 /* ARGSUSED */ 2524 int 2525 sys_chflags(struct lwp *l, const struct sys_chflags_args *uap, register_t *retval) 2526 { 2527 /* { 2528 syscallarg(const char *) path; 2529 syscallarg(u_long) flags; 2530 } */ 2531 struct vnode *vp; 2532 int error; 2533 2534 error = namei_simple_user(SCARG(uap, path), 2535 NSM_FOLLOW_TRYEMULROOT, &vp); 2536 if (error != 0) 2537 return (error); 2538 error = change_flags(vp, SCARG(uap, flags), l); 2539 vput(vp); 2540 return (error); 2541 } 2542 2543 /* 2544 * Change flags of a file given a file descriptor. 2545 */ 2546 /* ARGSUSED */ 2547 int 2548 sys_fchflags(struct lwp *l, const struct sys_fchflags_args *uap, register_t *retval) 2549 { 2550 /* { 2551 syscallarg(int) fd; 2552 syscallarg(u_long) flags; 2553 } */ 2554 struct vnode *vp; 2555 file_t *fp; 2556 int error; 2557 2558 /* fd_getvnode() will use the descriptor for us */ 2559 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0) 2560 return (error); 2561 vp = fp->f_data; 2562 error = change_flags(vp, SCARG(uap, flags), l); 2563 VOP_UNLOCK(vp, 0); 2564 fd_putfile(SCARG(uap, fd)); 2565 return (error); 2566 } 2567 2568 /* 2569 * Change flags of a file given a path name; this version does 2570 * not follow links. 2571 */ 2572 int 2573 sys_lchflags(struct lwp *l, const struct sys_lchflags_args *uap, register_t *retval) 2574 { 2575 /* { 2576 syscallarg(const char *) path; 2577 syscallarg(u_long) flags; 2578 } */ 2579 struct vnode *vp; 2580 int error; 2581 2582 error = namei_simple_user(SCARG(uap, path), 2583 NSM_NOFOLLOW_TRYEMULROOT, &vp); 2584 if (error != 0) 2585 return (error); 2586 error = change_flags(vp, SCARG(uap, flags), l); 2587 vput(vp); 2588 return (error); 2589 } 2590 2591 /* 2592 * Common routine to change flags of a file. 2593 */ 2594 int 2595 change_flags(struct vnode *vp, u_long flags, struct lwp *l) 2596 { 2597 struct vattr vattr; 2598 int error; 2599 2600 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2601 /* 2602 * Non-superusers cannot change the flags on devices, even if they 2603 * own them. 2604 */ 2605 if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL)) { 2606 if ((error = VOP_GETATTR(vp, &vattr, l->l_cred)) != 0) 2607 goto out; 2608 if (vattr.va_type == VCHR || vattr.va_type == VBLK) { 2609 error = EINVAL; 2610 goto out; 2611 } 2612 } 2613 vattr_null(&vattr); 2614 vattr.va_flags = flags; 2615 error = VOP_SETATTR(vp, &vattr, l->l_cred); 2616 out: 2617 return (error); 2618 } 2619 2620 /* 2621 * Change mode of a file given path name; this version follows links. 2622 */ 2623 /* ARGSUSED */ 2624 int 2625 sys_chmod(struct lwp *l, const struct sys_chmod_args *uap, register_t *retval) 2626 { 2627 /* { 2628 syscallarg(const char *) path; 2629 syscallarg(int) mode; 2630 } */ 2631 int error; 2632 struct vnode *vp; 2633 2634 error = namei_simple_user(SCARG(uap, path), 2635 NSM_FOLLOW_TRYEMULROOT, &vp); 2636 if (error != 0) 2637 return (error); 2638 2639 error = change_mode(vp, SCARG(uap, mode), l); 2640 2641 vrele(vp); 2642 return (error); 2643 } 2644 2645 /* 2646 * Change mode of a file given a file descriptor. 2647 */ 2648 /* ARGSUSED */ 2649 int 2650 sys_fchmod(struct lwp *l, const struct sys_fchmod_args *uap, register_t *retval) 2651 { 2652 /* { 2653 syscallarg(int) fd; 2654 syscallarg(int) mode; 2655 } */ 2656 file_t *fp; 2657 int error; 2658 2659 /* fd_getvnode() will use the descriptor for us */ 2660 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0) 2661 return (error); 2662 error = change_mode(fp->f_data, SCARG(uap, mode), l); 2663 fd_putfile(SCARG(uap, fd)); 2664 return (error); 2665 } 2666 2667 /* 2668 * Change mode of a file given path name; this version does not follow links. 2669 */ 2670 /* ARGSUSED */ 2671 int 2672 sys_lchmod(struct lwp *l, const struct sys_lchmod_args *uap, register_t *retval) 2673 { 2674 /* { 2675 syscallarg(const char *) path; 2676 syscallarg(int) mode; 2677 } */ 2678 int error; 2679 struct vnode *vp; 2680 2681 error = namei_simple_user(SCARG(uap, path), 2682 NSM_NOFOLLOW_TRYEMULROOT, &vp); 2683 if (error != 0) 2684 return (error); 2685 2686 error = change_mode(vp, SCARG(uap, mode), l); 2687 2688 vrele(vp); 2689 return (error); 2690 } 2691 2692 /* 2693 * Common routine to set mode given a vnode. 2694 */ 2695 static int 2696 change_mode(struct vnode *vp, int mode, struct lwp *l) 2697 { 2698 struct vattr vattr; 2699 int error; 2700 2701 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2702 vattr_null(&vattr); 2703 vattr.va_mode = mode & ALLPERMS; 2704 error = VOP_SETATTR(vp, &vattr, l->l_cred); 2705 VOP_UNLOCK(vp, 0); 2706 return (error); 2707 } 2708 2709 /* 2710 * Set ownership given a path name; this version follows links. 2711 */ 2712 /* ARGSUSED */ 2713 int 2714 sys_chown(struct lwp *l, const struct sys_chown_args *uap, register_t *retval) 2715 { 2716 /* { 2717 syscallarg(const char *) path; 2718 syscallarg(uid_t) uid; 2719 syscallarg(gid_t) gid; 2720 } */ 2721 int error; 2722 struct vnode *vp; 2723 2724 error = namei_simple_user(SCARG(uap, path), 2725 NSM_FOLLOW_TRYEMULROOT, &vp); 2726 if (error != 0) 2727 return (error); 2728 2729 error = change_owner(vp, SCARG(uap, uid), SCARG(uap, gid), l, 0); 2730 2731 vrele(vp); 2732 return (error); 2733 } 2734 2735 /* 2736 * Set ownership given a path name; this version follows links. 2737 * Provides POSIX semantics. 2738 */ 2739 /* ARGSUSED */ 2740 int 2741 sys___posix_chown(struct lwp *l, const struct sys___posix_chown_args *uap, register_t *retval) 2742 { 2743 /* { 2744 syscallarg(const char *) path; 2745 syscallarg(uid_t) uid; 2746 syscallarg(gid_t) gid; 2747 } */ 2748 int error; 2749 struct vnode *vp; 2750 2751 error = namei_simple_user(SCARG(uap, path), 2752 NSM_FOLLOW_TRYEMULROOT, &vp); 2753 if (error != 0) 2754 return (error); 2755 2756 error = change_owner(vp, SCARG(uap, uid), SCARG(uap, gid), l, 1); 2757 2758 vrele(vp); 2759 return (error); 2760 } 2761 2762 /* 2763 * Set ownership given a file descriptor. 2764 */ 2765 /* ARGSUSED */ 2766 int 2767 sys_fchown(struct lwp *l, const struct sys_fchown_args *uap, register_t *retval) 2768 { 2769 /* { 2770 syscallarg(int) fd; 2771 syscallarg(uid_t) uid; 2772 syscallarg(gid_t) gid; 2773 } */ 2774 int error; 2775 file_t *fp; 2776 2777 /* fd_getvnode() will use the descriptor for us */ 2778 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0) 2779 return (error); 2780 error = change_owner(fp->f_data, SCARG(uap, uid), SCARG(uap, gid), 2781 l, 0); 2782 fd_putfile(SCARG(uap, fd)); 2783 return (error); 2784 } 2785 2786 /* 2787 * Set ownership given a file descriptor, providing POSIX/XPG semantics. 2788 */ 2789 /* ARGSUSED */ 2790 int 2791 sys___posix_fchown(struct lwp *l, const struct sys___posix_fchown_args *uap, register_t *retval) 2792 { 2793 /* { 2794 syscallarg(int) fd; 2795 syscallarg(uid_t) uid; 2796 syscallarg(gid_t) gid; 2797 } */ 2798 int error; 2799 file_t *fp; 2800 2801 /* fd_getvnode() will use the descriptor for us */ 2802 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0) 2803 return (error); 2804 error = change_owner(fp->f_data, SCARG(uap, uid), SCARG(uap, gid), 2805 l, 1); 2806 fd_putfile(SCARG(uap, fd)); 2807 return (error); 2808 } 2809 2810 /* 2811 * Set ownership given a path name; this version does not follow links. 2812 */ 2813 /* ARGSUSED */ 2814 int 2815 sys_lchown(struct lwp *l, const struct sys_lchown_args *uap, register_t *retval) 2816 { 2817 /* { 2818 syscallarg(const char *) path; 2819 syscallarg(uid_t) uid; 2820 syscallarg(gid_t) gid; 2821 } */ 2822 int error; 2823 struct vnode *vp; 2824 2825 error = namei_simple_user(SCARG(uap, path), 2826 NSM_NOFOLLOW_TRYEMULROOT, &vp); 2827 if (error != 0) 2828 return (error); 2829 2830 error = change_owner(vp, SCARG(uap, uid), SCARG(uap, gid), l, 0); 2831 2832 vrele(vp); 2833 return (error); 2834 } 2835 2836 /* 2837 * Set ownership given a path name; this version does not follow links. 2838 * Provides POSIX/XPG semantics. 2839 */ 2840 /* ARGSUSED */ 2841 int 2842 sys___posix_lchown(struct lwp *l, const struct sys___posix_lchown_args *uap, register_t *retval) 2843 { 2844 /* { 2845 syscallarg(const char *) path; 2846 syscallarg(uid_t) uid; 2847 syscallarg(gid_t) gid; 2848 } */ 2849 int error; 2850 struct vnode *vp; 2851 2852 error = namei_simple_user(SCARG(uap, path), 2853 NSM_NOFOLLOW_TRYEMULROOT, &vp); 2854 if (error != 0) 2855 return (error); 2856 2857 error = change_owner(vp, SCARG(uap, uid), SCARG(uap, gid), l, 1); 2858 2859 vrele(vp); 2860 return (error); 2861 } 2862 2863 /* 2864 * Common routine to set ownership given a vnode. 2865 */ 2866 static int 2867 change_owner(struct vnode *vp, uid_t uid, gid_t gid, struct lwp *l, 2868 int posix_semantics) 2869 { 2870 struct vattr vattr; 2871 mode_t newmode; 2872 int error; 2873 2874 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2875 if ((error = VOP_GETATTR(vp, &vattr, l->l_cred)) != 0) 2876 goto out; 2877 2878 #define CHANGED(x) ((int)(x) != -1) 2879 newmode = vattr.va_mode; 2880 if (posix_semantics) { 2881 /* 2882 * POSIX/XPG semantics: if the caller is not the super-user, 2883 * clear set-user-id and set-group-id bits. Both POSIX and 2884 * the XPG consider the behaviour for calls by the super-user 2885 * implementation-defined; we leave the set-user-id and set- 2886 * group-id settings intact in that case. 2887 */ 2888 if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, 2889 NULL) != 0) 2890 newmode &= ~(S_ISUID | S_ISGID); 2891 } else { 2892 /* 2893 * NetBSD semantics: when changing owner and/or group, 2894 * clear the respective bit(s). 2895 */ 2896 if (CHANGED(uid)) 2897 newmode &= ~S_ISUID; 2898 if (CHANGED(gid)) 2899 newmode &= ~S_ISGID; 2900 } 2901 /* Update va_mode iff altered. */ 2902 if (vattr.va_mode == newmode) 2903 newmode = VNOVAL; 2904 2905 vattr_null(&vattr); 2906 vattr.va_uid = CHANGED(uid) ? uid : (uid_t)VNOVAL; 2907 vattr.va_gid = CHANGED(gid) ? gid : (gid_t)VNOVAL; 2908 vattr.va_mode = newmode; 2909 error = VOP_SETATTR(vp, &vattr, l->l_cred); 2910 #undef CHANGED 2911 2912 out: 2913 VOP_UNLOCK(vp, 0); 2914 return (error); 2915 } 2916 2917 /* 2918 * Set the access and modification times given a path name; this 2919 * version follows links. 2920 */ 2921 /* ARGSUSED */ 2922 int 2923 sys___utimes50(struct lwp *l, const struct sys___utimes50_args *uap, 2924 register_t *retval) 2925 { 2926 /* { 2927 syscallarg(const char *) path; 2928 syscallarg(const struct timeval *) tptr; 2929 } */ 2930 2931 return do_sys_utimes(l, NULL, SCARG(uap, path), FOLLOW, 2932 SCARG(uap, tptr), UIO_USERSPACE); 2933 } 2934 2935 /* 2936 * Set the access and modification times given a file descriptor. 2937 */ 2938 /* ARGSUSED */ 2939 int 2940 sys___futimes50(struct lwp *l, const struct sys___futimes50_args *uap, 2941 register_t *retval) 2942 { 2943 /* { 2944 syscallarg(int) fd; 2945 syscallarg(const struct timeval *) tptr; 2946 } */ 2947 int error; 2948 file_t *fp; 2949 2950 /* fd_getvnode() will use the descriptor for us */ 2951 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0) 2952 return (error); 2953 error = do_sys_utimes(l, fp->f_data, NULL, 0, SCARG(uap, tptr), 2954 UIO_USERSPACE); 2955 fd_putfile(SCARG(uap, fd)); 2956 return (error); 2957 } 2958 2959 /* 2960 * Set the access and modification times given a path name; this 2961 * version does not follow links. 2962 */ 2963 int 2964 sys___lutimes50(struct lwp *l, const struct sys___lutimes50_args *uap, 2965 register_t *retval) 2966 { 2967 /* { 2968 syscallarg(const char *) path; 2969 syscallarg(const struct timeval *) tptr; 2970 } */ 2971 2972 return do_sys_utimes(l, NULL, SCARG(uap, path), NOFOLLOW, 2973 SCARG(uap, tptr), UIO_USERSPACE); 2974 } 2975 2976 /* 2977 * Common routine to set access and modification times given a vnode. 2978 */ 2979 int 2980 do_sys_utimes(struct lwp *l, struct vnode *vp, const char *path, int flag, 2981 const struct timeval *tptr, enum uio_seg seg) 2982 { 2983 struct vattr vattr; 2984 int error, dorele = 0; 2985 namei_simple_flags_t sflags; 2986 2987 bool vanull, setbirthtime; 2988 struct timespec ts[2]; 2989 2990 /* 2991 * I have checked all callers and they pass either FOLLOW, 2992 * NOFOLLOW, or 0 (when they don't pass a path), and NOFOLLOW 2993 * is 0. More to the point, they don't pass anything else. 2994 * Let's keep it that way at least until the namei interfaces 2995 * are fully sanitized. 2996 */ 2997 KASSERT(flag == NOFOLLOW || flag == FOLLOW); 2998 sflags = (flag == FOLLOW) ? 2999 NSM_FOLLOW_TRYEMULROOT : NSM_NOFOLLOW_TRYEMULROOT; 3000 3001 if (tptr == NULL) { 3002 vanull = true; 3003 nanotime(&ts[0]); 3004 ts[1] = ts[0]; 3005 } else { 3006 struct timeval tv[2]; 3007 3008 vanull = false; 3009 if (seg != UIO_SYSSPACE) { 3010 error = copyin(tptr, tv, sizeof (tv)); 3011 if (error != 0) 3012 return error; 3013 tptr = tv; 3014 } 3015 TIMEVAL_TO_TIMESPEC(&tptr[0], &ts[0]); 3016 TIMEVAL_TO_TIMESPEC(&tptr[1], &ts[1]); 3017 } 3018 3019 if (vp == NULL) { 3020 /* note: SEG describes TPTR, not PATH; PATH is always user */ 3021 error = namei_simple_user(path, sflags, &vp); 3022 if (error != 0) 3023 return error; 3024 dorele = 1; 3025 } 3026 3027 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3028 setbirthtime = (VOP_GETATTR(vp, &vattr, l->l_cred) == 0 && 3029 timespeccmp(&ts[1], &vattr.va_birthtime, <)); 3030 vattr_null(&vattr); 3031 vattr.va_atime = ts[0]; 3032 vattr.va_mtime = ts[1]; 3033 if (setbirthtime) 3034 vattr.va_birthtime = ts[1]; 3035 if (vanull) 3036 vattr.va_vaflags |= VA_UTIMES_NULL; 3037 error = VOP_SETATTR(vp, &vattr, l->l_cred); 3038 VOP_UNLOCK(vp, 0); 3039 3040 if (dorele != 0) 3041 vrele(vp); 3042 3043 return error; 3044 } 3045 3046 /* 3047 * Truncate a file given its path name. 3048 */ 3049 /* ARGSUSED */ 3050 int 3051 sys_truncate(struct lwp *l, const struct sys_truncate_args *uap, register_t *retval) 3052 { 3053 /* { 3054 syscallarg(const char *) path; 3055 syscallarg(int) pad; 3056 syscallarg(off_t) length; 3057 } */ 3058 struct vnode *vp; 3059 struct vattr vattr; 3060 int error; 3061 3062 error = namei_simple_user(SCARG(uap, path), 3063 NSM_FOLLOW_TRYEMULROOT, &vp); 3064 if (error != 0) 3065 return (error); 3066 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3067 if (vp->v_type == VDIR) 3068 error = EISDIR; 3069 else if ((error = vn_writechk(vp)) == 0 && 3070 (error = VOP_ACCESS(vp, VWRITE, l->l_cred)) == 0) { 3071 vattr_null(&vattr); 3072 vattr.va_size = SCARG(uap, length); 3073 error = VOP_SETATTR(vp, &vattr, l->l_cred); 3074 } 3075 vput(vp); 3076 return (error); 3077 } 3078 3079 /* 3080 * Truncate a file given a file descriptor. 3081 */ 3082 /* ARGSUSED */ 3083 int 3084 sys_ftruncate(struct lwp *l, const struct sys_ftruncate_args *uap, register_t *retval) 3085 { 3086 /* { 3087 syscallarg(int) fd; 3088 syscallarg(int) pad; 3089 syscallarg(off_t) length; 3090 } */ 3091 struct vattr vattr; 3092 struct vnode *vp; 3093 file_t *fp; 3094 int error; 3095 3096 /* fd_getvnode() will use the descriptor for us */ 3097 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0) 3098 return (error); 3099 if ((fp->f_flag & FWRITE) == 0) { 3100 error = EINVAL; 3101 goto out; 3102 } 3103 vp = fp->f_data; 3104 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3105 if (vp->v_type == VDIR) 3106 error = EISDIR; 3107 else if ((error = vn_writechk(vp)) == 0) { 3108 vattr_null(&vattr); 3109 vattr.va_size = SCARG(uap, length); 3110 error = VOP_SETATTR(vp, &vattr, fp->f_cred); 3111 } 3112 VOP_UNLOCK(vp, 0); 3113 out: 3114 fd_putfile(SCARG(uap, fd)); 3115 return (error); 3116 } 3117 3118 /* 3119 * Sync an open file. 3120 */ 3121 /* ARGSUSED */ 3122 int 3123 sys_fsync(struct lwp *l, const struct sys_fsync_args *uap, register_t *retval) 3124 { 3125 /* { 3126 syscallarg(int) fd; 3127 } */ 3128 struct vnode *vp; 3129 file_t *fp; 3130 int error; 3131 3132 /* fd_getvnode() will use the descriptor for us */ 3133 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0) 3134 return (error); 3135 vp = fp->f_data; 3136 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3137 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, 0, 0); 3138 VOP_UNLOCK(vp, 0); 3139 fd_putfile(SCARG(uap, fd)); 3140 return (error); 3141 } 3142 3143 /* 3144 * Sync a range of file data. API modeled after that found in AIX. 3145 * 3146 * FDATASYNC indicates that we need only save enough metadata to be able 3147 * to re-read the written data. Note we duplicate AIX's requirement that 3148 * the file be open for writing. 3149 */ 3150 /* ARGSUSED */ 3151 int 3152 sys_fsync_range(struct lwp *l, const struct sys_fsync_range_args *uap, register_t *retval) 3153 { 3154 /* { 3155 syscallarg(int) fd; 3156 syscallarg(int) flags; 3157 syscallarg(off_t) start; 3158 syscallarg(off_t) length; 3159 } */ 3160 struct vnode *vp; 3161 file_t *fp; 3162 int flags, nflags; 3163 off_t s, e, len; 3164 int error; 3165 3166 /* fd_getvnode() will use the descriptor for us */ 3167 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0) 3168 return (error); 3169 3170 if ((fp->f_flag & FWRITE) == 0) { 3171 error = EBADF; 3172 goto out; 3173 } 3174 3175 flags = SCARG(uap, flags); 3176 if (((flags & (FDATASYNC | FFILESYNC)) == 0) || 3177 ((~flags & (FDATASYNC | FFILESYNC)) == 0)) { 3178 error = EINVAL; 3179 goto out; 3180 } 3181 /* Now set up the flags for value(s) to pass to VOP_FSYNC() */ 3182 if (flags & FDATASYNC) 3183 nflags = FSYNC_DATAONLY | FSYNC_WAIT; 3184 else 3185 nflags = FSYNC_WAIT; 3186 if (flags & FDISKSYNC) 3187 nflags |= FSYNC_CACHE; 3188 3189 len = SCARG(uap, length); 3190 /* If length == 0, we do the whole file, and s = l = 0 will do that */ 3191 if (len) { 3192 s = SCARG(uap, start); 3193 e = s + len; 3194 if (e < s) { 3195 error = EINVAL; 3196 goto out; 3197 } 3198 } else { 3199 e = 0; 3200 s = 0; 3201 } 3202 3203 vp = fp->f_data; 3204 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3205 error = VOP_FSYNC(vp, fp->f_cred, nflags, s, e); 3206 VOP_UNLOCK(vp, 0); 3207 out: 3208 fd_putfile(SCARG(uap, fd)); 3209 return (error); 3210 } 3211 3212 /* 3213 * Sync the data of an open file. 3214 */ 3215 /* ARGSUSED */ 3216 int 3217 sys_fdatasync(struct lwp *l, const struct sys_fdatasync_args *uap, register_t *retval) 3218 { 3219 /* { 3220 syscallarg(int) fd; 3221 } */ 3222 struct vnode *vp; 3223 file_t *fp; 3224 int error; 3225 3226 /* fd_getvnode() will use the descriptor for us */ 3227 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0) 3228 return (error); 3229 if ((fp->f_flag & FWRITE) == 0) { 3230 fd_putfile(SCARG(uap, fd)); 3231 return (EBADF); 3232 } 3233 vp = fp->f_data; 3234 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3235 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, 0, 0); 3236 VOP_UNLOCK(vp, 0); 3237 fd_putfile(SCARG(uap, fd)); 3238 return (error); 3239 } 3240 3241 /* 3242 * Rename files, (standard) BSD semantics frontend. 3243 */ 3244 /* ARGSUSED */ 3245 int 3246 sys_rename(struct lwp *l, const struct sys_rename_args *uap, register_t *retval) 3247 { 3248 /* { 3249 syscallarg(const char *) from; 3250 syscallarg(const char *) to; 3251 } */ 3252 3253 return (do_sys_rename(SCARG(uap, from), SCARG(uap, to), UIO_USERSPACE, 0)); 3254 } 3255 3256 /* 3257 * Rename files, POSIX semantics frontend. 3258 */ 3259 /* ARGSUSED */ 3260 int 3261 sys___posix_rename(struct lwp *l, const struct sys___posix_rename_args *uap, register_t *retval) 3262 { 3263 /* { 3264 syscallarg(const char *) from; 3265 syscallarg(const char *) to; 3266 } */ 3267 3268 return (do_sys_rename(SCARG(uap, from), SCARG(uap, to), UIO_USERSPACE, 1)); 3269 } 3270 3271 /* 3272 * Rename files. Source and destination must either both be directories, 3273 * or both not be directories. If target is a directory, it must be empty. 3274 * If `from' and `to' refer to the same object, the value of the `retain' 3275 * argument is used to determine whether `from' will be 3276 * 3277 * (retain == 0) deleted unless `from' and `to' refer to the same 3278 * object in the file system's name space (BSD). 3279 * (retain == 1) always retained (POSIX). 3280 */ 3281 int 3282 do_sys_rename(const char *from, const char *to, enum uio_seg seg, int retain) 3283 { 3284 struct vnode *tvp, *fvp, *tdvp; 3285 struct nameidata fromnd, tond; 3286 struct mount *fs; 3287 struct lwp *l = curlwp; 3288 struct proc *p; 3289 uint32_t saveflag; 3290 int error; 3291 3292 NDINIT(&fromnd, DELETE, LOCKPARENT | SAVESTART | TRYEMULROOT | INRENAME, 3293 seg, from); 3294 if ((error = namei(&fromnd)) != 0) 3295 return (error); 3296 if (fromnd.ni_dvp != fromnd.ni_vp) 3297 VOP_UNLOCK(fromnd.ni_dvp, 0); 3298 fvp = fromnd.ni_vp; 3299 3300 fs = fvp->v_mount; 3301 error = VFS_RENAMELOCK_ENTER(fs); 3302 if (error) { 3303 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd); 3304 vrele(fromnd.ni_dvp); 3305 vrele(fvp); 3306 goto out1; 3307 } 3308 3309 /* 3310 * close, partially, yet another race - ideally we should only 3311 * go as far as getting fromnd.ni_dvp before getting the per-fs 3312 * lock, and then continue to get fromnd.ni_vp, but we can't do 3313 * that with namei as it stands. 3314 * 3315 * This still won't prevent rmdir from nuking fromnd.ni_vp 3316 * under us. The real fix is to get the locks in the right 3317 * order and do the lookups in the right places, but that's a 3318 * major rototill. 3319 * 3320 * Preserve the SAVESTART in cn_flags, because who knows what 3321 * might happen if we don't. 3322 * 3323 * Note: this logic (as well as this whole function) is cloned 3324 * in nfs_serv.c. Proceed accordingly. 3325 */ 3326 vrele(fvp); 3327 if ((fromnd.ni_cnd.cn_namelen == 1 && 3328 fromnd.ni_cnd.cn_nameptr[0] == '.') || 3329 (fromnd.ni_cnd.cn_namelen == 2 && 3330 fromnd.ni_cnd.cn_nameptr[0] == '.' && 3331 fromnd.ni_cnd.cn_nameptr[1] == '.')) { 3332 error = EINVAL; 3333 VFS_RENAMELOCK_EXIT(fs); 3334 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd); 3335 vrele(fromnd.ni_dvp); 3336 goto out1; 3337 } 3338 saveflag = fromnd.ni_cnd.cn_flags & SAVESTART; 3339 fromnd.ni_cnd.cn_flags &= ~SAVESTART; 3340 vn_lock(fromnd.ni_dvp, LK_EXCLUSIVE | LK_RETRY); 3341 error = relookup(fromnd.ni_dvp, &fromnd.ni_vp, &fromnd.ni_cnd); 3342 fromnd.ni_cnd.cn_flags |= saveflag; 3343 if (error) { 3344 VOP_UNLOCK(fromnd.ni_dvp, 0); 3345 VFS_RENAMELOCK_EXIT(fs); 3346 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd); 3347 vrele(fromnd.ni_dvp); 3348 goto out1; 3349 } 3350 VOP_UNLOCK(fromnd.ni_vp, 0); 3351 if (fromnd.ni_dvp != fromnd.ni_vp) 3352 VOP_UNLOCK(fromnd.ni_dvp, 0); 3353 fvp = fromnd.ni_vp; 3354 3355 NDINIT(&tond, RENAME, 3356 LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | TRYEMULROOT 3357 | INRENAME | (fvp->v_type == VDIR ? CREATEDIR : 0), 3358 seg, to); 3359 if ((error = namei(&tond)) != 0) { 3360 VFS_RENAMELOCK_EXIT(fs); 3361 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd); 3362 vrele(fromnd.ni_dvp); 3363 vrele(fvp); 3364 goto out1; 3365 } 3366 tdvp = tond.ni_dvp; 3367 tvp = tond.ni_vp; 3368 3369 if (tvp != NULL) { 3370 if (fvp->v_type == VDIR && tvp->v_type != VDIR) { 3371 error = ENOTDIR; 3372 goto out; 3373 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) { 3374 error = EISDIR; 3375 goto out; 3376 } 3377 } 3378 3379 if (fvp == tdvp) 3380 error = EINVAL; 3381 3382 /* 3383 * Source and destination refer to the same object. 3384 */ 3385 if (fvp == tvp) { 3386 if (retain) 3387 error = -1; 3388 else if (fromnd.ni_dvp == tdvp && 3389 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen && 3390 !memcmp(fromnd.ni_cnd.cn_nameptr, 3391 tond.ni_cnd.cn_nameptr, 3392 fromnd.ni_cnd.cn_namelen)) 3393 error = -1; 3394 } 3395 3396 #if NVERIEXEC > 0 3397 if (!error) { 3398 char *f1, *f2; 3399 size_t f1_len; 3400 size_t f2_len; 3401 3402 f1_len = fromnd.ni_cnd.cn_namelen + 1; 3403 f1 = kmem_alloc(f1_len, KM_SLEEP); 3404 strlcpy(f1, fromnd.ni_cnd.cn_nameptr, f1_len); 3405 3406 f2_len = tond.ni_cnd.cn_namelen + 1; 3407 f2 = kmem_alloc(f2_len, KM_SLEEP); 3408 strlcpy(f2, tond.ni_cnd.cn_nameptr, f2_len); 3409 3410 error = veriexec_renamechk(l, fvp, f1, tvp, f2); 3411 3412 kmem_free(f1, f1_len); 3413 kmem_free(f2, f2_len); 3414 } 3415 #endif /* NVERIEXEC > 0 */ 3416 3417 out: 3418 p = l->l_proc; 3419 if (!error) { 3420 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd, 3421 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd); 3422 VFS_RENAMELOCK_EXIT(fs); 3423 } else { 3424 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd); 3425 if (tdvp == tvp) 3426 vrele(tdvp); 3427 else 3428 vput(tdvp); 3429 if (tvp) 3430 vput(tvp); 3431 VFS_RENAMELOCK_EXIT(fs); 3432 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd); 3433 vrele(fromnd.ni_dvp); 3434 vrele(fvp); 3435 } 3436 vrele(tond.ni_startdir); 3437 PNBUF_PUT(tond.ni_cnd.cn_pnbuf); 3438 out1: 3439 if (fromnd.ni_startdir) 3440 vrele(fromnd.ni_startdir); 3441 PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf); 3442 return (error == -1 ? 0 : error); 3443 } 3444 3445 /* 3446 * Make a directory file. 3447 */ 3448 /* ARGSUSED */ 3449 int 3450 sys_mkdir(struct lwp *l, const struct sys_mkdir_args *uap, register_t *retval) 3451 { 3452 /* { 3453 syscallarg(const char *) path; 3454 syscallarg(int) mode; 3455 } */ 3456 3457 return do_sys_mkdir(SCARG(uap, path), SCARG(uap, mode), UIO_USERSPACE); 3458 } 3459 3460 int 3461 do_sys_mkdir(const char *path, mode_t mode, enum uio_seg seg) 3462 { 3463 struct proc *p = curlwp->l_proc; 3464 struct vnode *vp; 3465 struct vattr vattr; 3466 int error; 3467 struct nameidata nd; 3468 3469 NDINIT(&nd, CREATE, LOCKPARENT | CREATEDIR | TRYEMULROOT, 3470 seg, path); 3471 if ((error = namei(&nd)) != 0) 3472 return (error); 3473 vp = nd.ni_vp; 3474 if (vp != NULL) { 3475 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 3476 if (nd.ni_dvp == vp) 3477 vrele(nd.ni_dvp); 3478 else 3479 vput(nd.ni_dvp); 3480 vrele(vp); 3481 return (EEXIST); 3482 } 3483 vattr_null(&vattr); 3484 vattr.va_type = VDIR; 3485 /* We will read cwdi->cwdi_cmask unlocked. */ 3486 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_cwdi->cwdi_cmask; 3487 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 3488 if (!error) 3489 vput(nd.ni_vp); 3490 return (error); 3491 } 3492 3493 /* 3494 * Remove a directory file. 3495 */ 3496 /* ARGSUSED */ 3497 int 3498 sys_rmdir(struct lwp *l, const struct sys_rmdir_args *uap, register_t *retval) 3499 { 3500 /* { 3501 syscallarg(const char *) path; 3502 } */ 3503 struct vnode *vp; 3504 int error; 3505 struct nameidata nd; 3506 3507 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE, 3508 SCARG(uap, path)); 3509 if ((error = namei(&nd)) != 0) 3510 return (error); 3511 vp = nd.ni_vp; 3512 if (vp->v_type != VDIR) { 3513 error = ENOTDIR; 3514 goto out; 3515 } 3516 /* 3517 * No rmdir "." please. 3518 */ 3519 if (nd.ni_dvp == vp) { 3520 error = EINVAL; 3521 goto out; 3522 } 3523 /* 3524 * The root of a mounted filesystem cannot be deleted. 3525 */ 3526 if ((vp->v_vflag & VV_ROOT) != 0 || vp->v_mountedhere != NULL) { 3527 error = EBUSY; 3528 goto out; 3529 } 3530 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd); 3531 return (error); 3532 3533 out: 3534 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 3535 if (nd.ni_dvp == vp) 3536 vrele(nd.ni_dvp); 3537 else 3538 vput(nd.ni_dvp); 3539 vput(vp); 3540 return (error); 3541 } 3542 3543 /* 3544 * Read a block of directory entries in a file system independent format. 3545 */ 3546 int 3547 sys___getdents30(struct lwp *l, const struct sys___getdents30_args *uap, register_t *retval) 3548 { 3549 /* { 3550 syscallarg(int) fd; 3551 syscallarg(char *) buf; 3552 syscallarg(size_t) count; 3553 } */ 3554 file_t *fp; 3555 int error, done; 3556 3557 /* fd_getvnode() will use the descriptor for us */ 3558 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0) 3559 return (error); 3560 if ((fp->f_flag & FREAD) == 0) { 3561 error = EBADF; 3562 goto out; 3563 } 3564 error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE, 3565 SCARG(uap, count), &done, l, 0, 0); 3566 ktrgenio(SCARG(uap, fd), UIO_READ, SCARG(uap, buf), done, error); 3567 *retval = done; 3568 out: 3569 fd_putfile(SCARG(uap, fd)); 3570 return (error); 3571 } 3572 3573 /* 3574 * Set the mode mask for creation of filesystem nodes. 3575 */ 3576 int 3577 sys_umask(struct lwp *l, const struct sys_umask_args *uap, register_t *retval) 3578 { 3579 /* { 3580 syscallarg(mode_t) newmask; 3581 } */ 3582 struct proc *p = l->l_proc; 3583 struct cwdinfo *cwdi; 3584 3585 /* 3586 * cwdi->cwdi_cmask will be read unlocked elsewhere. What's 3587 * important is that we serialize changes to the mask. The 3588 * rw_exit() will issue a write memory barrier on our behalf, 3589 * and force the changes out to other CPUs (as it must use an 3590 * atomic operation, draining the local CPU's store buffers). 3591 */ 3592 cwdi = p->p_cwdi; 3593 rw_enter(&cwdi->cwdi_lock, RW_WRITER); 3594 *retval = cwdi->cwdi_cmask; 3595 cwdi->cwdi_cmask = SCARG(uap, newmask) & ALLPERMS; 3596 rw_exit(&cwdi->cwdi_lock); 3597 3598 return (0); 3599 } 3600 3601 int 3602 dorevoke(struct vnode *vp, kauth_cred_t cred) 3603 { 3604 struct vattr vattr; 3605 int error; 3606 3607 if ((error = VOP_GETATTR(vp, &vattr, cred)) != 0) 3608 return error; 3609 if (kauth_cred_geteuid(cred) == vattr.va_uid || 3610 (error = kauth_authorize_generic(cred, 3611 KAUTH_GENERIC_ISSUSER, NULL)) == 0) 3612 VOP_REVOKE(vp, REVOKEALL); 3613 return (error); 3614 } 3615 3616 /* 3617 * Void all references to file by ripping underlying filesystem 3618 * away from vnode. 3619 */ 3620 /* ARGSUSED */ 3621 int 3622 sys_revoke(struct lwp *l, const struct sys_revoke_args *uap, register_t *retval) 3623 { 3624 /* { 3625 syscallarg(const char *) path; 3626 } */ 3627 struct vnode *vp; 3628 int error; 3629 3630 error = namei_simple_user(SCARG(uap, path), 3631 NSM_FOLLOW_TRYEMULROOT, &vp); 3632 if (error != 0) 3633 return (error); 3634 error = dorevoke(vp, l->l_cred); 3635 vrele(vp); 3636 return (error); 3637 } 3638