1 /* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 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 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94 39 * $FreeBSD: src/sys/kern/vfs_syscalls.c,v 1.151.2.18 2003/04/04 20:35:58 tegge Exp $ 40 */ 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/buf.h> 45 #include <sys/conf.h> 46 #include <sys/sysent.h> 47 #include <sys/malloc.h> 48 #include <sys/mount.h> 49 #include <sys/mountctl.h> 50 #include <sys/sysproto.h> 51 #include <sys/filedesc.h> 52 #include <sys/kernel.h> 53 #include <sys/fcntl.h> 54 #include <sys/file.h> 55 #include <sys/linker.h> 56 #include <sys/stat.h> 57 #include <sys/unistd.h> 58 #include <sys/vnode.h> 59 #include <sys/proc.h> 60 #include <sys/priv.h> 61 #include <sys/jail.h> 62 #include <sys/namei.h> 63 #include <sys/nlookup.h> 64 #include <sys/dirent.h> 65 #include <sys/extattr.h> 66 #include <sys/spinlock.h> 67 #include <sys/kern_syscall.h> 68 #include <sys/objcache.h> 69 #include <sys/sysctl.h> 70 71 #include <sys/buf2.h> 72 #include <sys/file2.h> 73 #include <sys/spinlock2.h> 74 #include <sys/mplock2.h> 75 76 #include <vm/vm.h> 77 #include <vm/vm_object.h> 78 #include <vm/vm_page.h> 79 80 #include <machine/limits.h> 81 #include <machine/stdarg.h> 82 83 #include <vfs/union/union.h> 84 85 static void mount_warning(struct mount *mp, const char *ctl, ...) 86 __printflike(2, 3); 87 static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb); 88 static int checkvp_chdir (struct vnode *vn, struct thread *td); 89 static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch); 90 static int chroot_refuse_vdir_fds (struct filedesc *fdp); 91 static int chroot_visible_mnt(struct mount *mp, struct proc *p); 92 static int getutimes (const struct timeval *, struct timespec *); 93 static int setfown (struct mount *, struct vnode *, uid_t, gid_t); 94 static int setfmode (struct vnode *, int); 95 static int setfflags (struct vnode *, int); 96 static int setutimes (struct vnode *, struct vattr *, 97 const struct timespec *, int); 98 static int usermount = 0; /* if 1, non-root can mount fs. */ 99 100 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *); 101 102 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, 103 "Allow non-root users to mount filesystems"); 104 105 /* 106 * Virtual File System System Calls 107 */ 108 109 /* 110 * Mount a file system. 111 * 112 * mount_args(char *type, char *path, int flags, caddr_t data) 113 * 114 * MPALMOSTSAFE 115 */ 116 int 117 sys_mount(struct mount_args *uap) 118 { 119 struct thread *td = curthread; 120 struct vnode *vp; 121 struct nchandle nch; 122 struct mount *mp, *nullmp; 123 struct vfsconf *vfsp; 124 int error, flag = 0, flag2 = 0; 125 int hasmount; 126 struct vattr va; 127 struct nlookupdata nd; 128 char fstypename[MFSNAMELEN]; 129 struct ucred *cred; 130 131 get_mplock(); 132 cred = td->td_ucred; 133 if (jailed(cred)) { 134 error = EPERM; 135 goto done; 136 } 137 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT))) 138 goto done; 139 140 /* 141 * Do not allow NFS export by non-root users. 142 */ 143 if (uap->flags & MNT_EXPORTED) { 144 error = priv_check(td, PRIV_ROOT); 145 if (error) 146 goto done; 147 } 148 /* 149 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users 150 */ 151 if (priv_check(td, PRIV_ROOT)) 152 uap->flags |= MNT_NOSUID | MNT_NODEV; 153 154 /* 155 * Lookup the requested path and extract the nch and vnode. 156 */ 157 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 158 if (error == 0) { 159 if ((error = nlookup(&nd)) == 0) { 160 if (nd.nl_nch.ncp->nc_vp == NULL) 161 error = ENOENT; 162 } 163 } 164 if (error) { 165 nlookup_done(&nd); 166 goto done; 167 } 168 169 /* 170 * If the target filesystem is resolved via a nullfs mount, then 171 * nd.nl_nch.mount will be pointing to the nullfs mount structure 172 * instead of the target file system. We need it in case we are 173 * doing an update. 174 */ 175 nullmp = nd.nl_nch.mount; 176 177 /* 178 * Extract the locked+refd ncp and cleanup the nd structure 179 */ 180 nch = nd.nl_nch; 181 cache_zero(&nd.nl_nch); 182 nlookup_done(&nd); 183 184 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) && cache_findmount(&nch)) 185 hasmount = 1; 186 else 187 hasmount = 0; 188 189 190 /* 191 * now we have the locked ref'd nch and unreferenced vnode. 192 */ 193 vp = nch.ncp->nc_vp; 194 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) { 195 cache_put(&nch); 196 goto done; 197 } 198 cache_unlock(&nch); 199 200 /* 201 * Extract the file system type. We need to know this early, to take 202 * appropriate actions if we are dealing with a nullfs. 203 */ 204 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) { 205 cache_drop(&nch); 206 vput(vp); 207 goto done; 208 } 209 210 /* 211 * Now we have an unlocked ref'd nch and a locked ref'd vp 212 */ 213 if (uap->flags & MNT_UPDATE) { 214 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) { 215 cache_drop(&nch); 216 vput(vp); 217 error = EINVAL; 218 goto done; 219 } 220 221 if (strncmp(fstypename, "null", 5) == 0) { 222 KKASSERT(nullmp); 223 mp = nullmp; 224 } else { 225 mp = vp->v_mount; 226 } 227 228 flag = mp->mnt_flag; 229 flag2 = mp->mnt_kern_flag; 230 /* 231 * We only allow the filesystem to be reloaded if it 232 * is currently mounted read-only. 233 */ 234 if ((uap->flags & MNT_RELOAD) && 235 ((mp->mnt_flag & MNT_RDONLY) == 0)) { 236 cache_drop(&nch); 237 vput(vp); 238 error = EOPNOTSUPP; /* Needs translation */ 239 goto done; 240 } 241 /* 242 * Only root, or the user that did the original mount is 243 * permitted to update it. 244 */ 245 if (mp->mnt_stat.f_owner != cred->cr_uid && 246 (error = priv_check(td, PRIV_ROOT))) { 247 cache_drop(&nch); 248 vput(vp); 249 goto done; 250 } 251 if (vfs_busy(mp, LK_NOWAIT)) { 252 cache_drop(&nch); 253 vput(vp); 254 error = EBUSY; 255 goto done; 256 } 257 if ((vp->v_flag & VMOUNT) != 0 || hasmount) { 258 cache_drop(&nch); 259 vfs_unbusy(mp); 260 vput(vp); 261 error = EBUSY; 262 goto done; 263 } 264 vsetflags(vp, VMOUNT); 265 mp->mnt_flag |= 266 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE); 267 vn_unlock(vp); 268 goto update; 269 } 270 /* 271 * If the user is not root, ensure that they own the directory 272 * onto which we are attempting to mount. 273 */ 274 if ((error = VOP_GETATTR(vp, &va)) || 275 (va.va_uid != cred->cr_uid && (error = priv_check(td, PRIV_ROOT)))) { 276 cache_drop(&nch); 277 vput(vp); 278 goto done; 279 } 280 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) { 281 cache_drop(&nch); 282 vput(vp); 283 goto done; 284 } 285 if (vp->v_type != VDIR) { 286 cache_drop(&nch); 287 vput(vp); 288 error = ENOTDIR; 289 goto done; 290 } 291 if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) { 292 cache_drop(&nch); 293 vput(vp); 294 error = EPERM; 295 goto done; 296 } 297 vfsp = vfsconf_find_by_name(fstypename); 298 if (vfsp == NULL) { 299 linker_file_t lf; 300 301 /* Only load modules for root (very important!) */ 302 if ((error = priv_check(td, PRIV_ROOT)) != 0) { 303 cache_drop(&nch); 304 vput(vp); 305 goto done; 306 } 307 error = linker_load_file(fstypename, &lf); 308 if (error || lf == NULL) { 309 cache_drop(&nch); 310 vput(vp); 311 if (lf == NULL) 312 error = ENODEV; 313 goto done; 314 } 315 lf->userrefs++; 316 /* lookup again, see if the VFS was loaded */ 317 vfsp = vfsconf_find_by_name(fstypename); 318 if (vfsp == NULL) { 319 lf->userrefs--; 320 linker_file_unload(lf); 321 cache_drop(&nch); 322 vput(vp); 323 error = ENODEV; 324 goto done; 325 } 326 } 327 if ((vp->v_flag & VMOUNT) != 0 || hasmount) { 328 cache_drop(&nch); 329 vput(vp); 330 error = EBUSY; 331 goto done; 332 } 333 vsetflags(vp, VMOUNT); 334 335 /* 336 * Allocate and initialize the filesystem. 337 */ 338 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK); 339 mount_init(mp); 340 vfs_busy(mp, LK_NOWAIT); 341 mp->mnt_op = vfsp->vfc_vfsops; 342 mp->mnt_vfc = vfsp; 343 vfsp->vfc_refcount++; 344 mp->mnt_stat.f_type = vfsp->vfc_typenum; 345 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK; 346 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN); 347 mp->mnt_stat.f_owner = cred->cr_uid; 348 vn_unlock(vp); 349 update: 350 /* 351 * Set the mount level flags. 352 */ 353 if (uap->flags & MNT_RDONLY) 354 mp->mnt_flag |= MNT_RDONLY; 355 else if (mp->mnt_flag & MNT_RDONLY) 356 mp->mnt_kern_flag |= MNTK_WANTRDWR; 357 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV | 358 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME | 359 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM | 360 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR); 361 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC | 362 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE | 363 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM | 364 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR); 365 /* 366 * Mount the filesystem. 367 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they 368 * get. 369 */ 370 error = VFS_MOUNT(mp, uap->path, uap->data, cred); 371 if (mp->mnt_flag & MNT_UPDATE) { 372 if (mp->mnt_kern_flag & MNTK_WANTRDWR) 373 mp->mnt_flag &= ~MNT_RDONLY; 374 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE); 375 mp->mnt_kern_flag &=~ MNTK_WANTRDWR; 376 if (error) { 377 mp->mnt_flag = flag; 378 mp->mnt_kern_flag = flag2; 379 } 380 vfs_unbusy(mp); 381 vclrflags(vp, VMOUNT); 382 vrele(vp); 383 cache_drop(&nch); 384 goto done; 385 } 386 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 387 /* 388 * Put the new filesystem on the mount list after root. The mount 389 * point gets its own mnt_ncmountpt (unless the VFS already set one 390 * up) which represents the root of the mount. The lookup code 391 * detects the mount point going forward and checks the root of 392 * the mount going backwards. 393 * 394 * It is not necessary to invalidate or purge the vnode underneath 395 * because elements under the mount will be given their own glue 396 * namecache record. 397 */ 398 if (!error) { 399 if (mp->mnt_ncmountpt.ncp == NULL) { 400 /* 401 * allocate, then unlock, but leave the ref intact 402 */ 403 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL); 404 cache_unlock(&mp->mnt_ncmountpt); 405 } 406 mp->mnt_ncmounton = nch; /* inherits ref */ 407 nch.ncp->nc_flag |= NCF_ISMOUNTPT; 408 409 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */ 410 vclrflags(vp, VMOUNT); 411 mountlist_insert(mp, MNTINS_LAST); 412 vn_unlock(vp); 413 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt); 414 error = vfs_allocate_syncvnode(mp); 415 vfs_unbusy(mp); 416 error = VFS_START(mp, 0); 417 vrele(vp); 418 } else { 419 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops); 420 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops); 421 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops); 422 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops); 423 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops); 424 vclrflags(vp, VMOUNT); 425 mp->mnt_vfc->vfc_refcount--; 426 vfs_unbusy(mp); 427 kfree(mp, M_MOUNT); 428 cache_drop(&nch); 429 vput(vp); 430 } 431 done: 432 rel_mplock(); 433 return (error); 434 } 435 436 /* 437 * Scan all active processes to see if any of them have a current 438 * or root directory onto which the new filesystem has just been 439 * mounted. If so, replace them with the new mount point. 440 * 441 * The passed ncp is ref'd and locked (from the mount code) and 442 * must be associated with the vnode representing the root of the 443 * mount point. 444 */ 445 struct checkdirs_info { 446 struct nchandle old_nch; 447 struct nchandle new_nch; 448 struct vnode *old_vp; 449 struct vnode *new_vp; 450 }; 451 452 static int checkdirs_callback(struct proc *p, void *data); 453 454 static void 455 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch) 456 { 457 struct checkdirs_info info; 458 struct vnode *olddp; 459 struct vnode *newdp; 460 struct mount *mp; 461 462 /* 463 * If the old mount point's vnode has a usecount of 1, it is not 464 * being held as a descriptor anywhere. 465 */ 466 olddp = old_nch->ncp->nc_vp; 467 if (olddp == NULL || olddp->v_sysref.refcnt == 1) 468 return; 469 470 /* 471 * Force the root vnode of the new mount point to be resolved 472 * so we can update any matching processes. 473 */ 474 mp = new_nch->mount; 475 if (VFS_ROOT(mp, &newdp)) 476 panic("mount: lost mount"); 477 cache_setunresolved(new_nch); 478 cache_setvp(new_nch, newdp); 479 480 /* 481 * Special handling of the root node 482 */ 483 if (rootvnode == olddp) { 484 vref(newdp); 485 vfs_cache_setroot(newdp, cache_hold(new_nch)); 486 } 487 488 /* 489 * Pass newdp separately so the callback does not have to access 490 * it via new_nch->ncp->nc_vp. 491 */ 492 info.old_nch = *old_nch; 493 info.new_nch = *new_nch; 494 info.new_vp = newdp; 495 allproc_scan(checkdirs_callback, &info); 496 vput(newdp); 497 } 498 499 /* 500 * NOTE: callback is not MP safe because the scanned process's filedesc 501 * structure can be ripped out from under us, amoung other things. 502 */ 503 static int 504 checkdirs_callback(struct proc *p, void *data) 505 { 506 struct checkdirs_info *info = data; 507 struct filedesc *fdp; 508 struct nchandle ncdrop1; 509 struct nchandle ncdrop2; 510 struct vnode *vprele1; 511 struct vnode *vprele2; 512 513 if ((fdp = p->p_fd) != NULL) { 514 cache_zero(&ncdrop1); 515 cache_zero(&ncdrop2); 516 vprele1 = NULL; 517 vprele2 = NULL; 518 519 /* 520 * MPUNSAFE - XXX fdp can be pulled out from under a 521 * foreign process. 522 * 523 * A shared filedesc is ok, we don't have to copy it 524 * because we are making this change globally. 525 */ 526 spin_lock(&fdp->fd_spin); 527 if (fdp->fd_ncdir.mount == info->old_nch.mount && 528 fdp->fd_ncdir.ncp == info->old_nch.ncp) { 529 vprele1 = fdp->fd_cdir; 530 vref(info->new_vp); 531 fdp->fd_cdir = info->new_vp; 532 ncdrop1 = fdp->fd_ncdir; 533 cache_copy(&info->new_nch, &fdp->fd_ncdir); 534 } 535 if (fdp->fd_nrdir.mount == info->old_nch.mount && 536 fdp->fd_nrdir.ncp == info->old_nch.ncp) { 537 vprele2 = fdp->fd_rdir; 538 vref(info->new_vp); 539 fdp->fd_rdir = info->new_vp; 540 ncdrop2 = fdp->fd_nrdir; 541 cache_copy(&info->new_nch, &fdp->fd_nrdir); 542 } 543 spin_unlock(&fdp->fd_spin); 544 if (ncdrop1.ncp) 545 cache_drop(&ncdrop1); 546 if (ncdrop2.ncp) 547 cache_drop(&ncdrop2); 548 if (vprele1) 549 vrele(vprele1); 550 if (vprele2) 551 vrele(vprele2); 552 } 553 return(0); 554 } 555 556 /* 557 * Unmount a file system. 558 * 559 * Note: unmount takes a path to the vnode mounted on as argument, 560 * not special file (as before). 561 * 562 * umount_args(char *path, int flags) 563 * 564 * MPALMOSTSAFE 565 */ 566 int 567 sys_unmount(struct unmount_args *uap) 568 { 569 struct thread *td = curthread; 570 struct proc *p __debugvar = td->td_proc; 571 struct mount *mp = NULL; 572 struct nlookupdata nd; 573 int error; 574 575 KKASSERT(p); 576 get_mplock(); 577 if (td->td_ucred->cr_prison != NULL) { 578 error = EPERM; 579 goto done; 580 } 581 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT))) 582 goto done; 583 584 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 585 if (error == 0) 586 error = nlookup(&nd); 587 if (error) 588 goto out; 589 590 mp = nd.nl_nch.mount; 591 592 /* 593 * Only root, or the user that did the original mount is 594 * permitted to unmount this filesystem. 595 */ 596 if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) && 597 (error = priv_check(td, PRIV_ROOT))) 598 goto out; 599 600 /* 601 * Don't allow unmounting the root file system. 602 */ 603 if (mp->mnt_flag & MNT_ROOTFS) { 604 error = EINVAL; 605 goto out; 606 } 607 608 /* 609 * Must be the root of the filesystem 610 */ 611 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) { 612 error = EINVAL; 613 goto out; 614 } 615 616 out: 617 nlookup_done(&nd); 618 if (error == 0) 619 error = dounmount(mp, uap->flags); 620 done: 621 rel_mplock(); 622 return (error); 623 } 624 625 /* 626 * Do the actual file system unmount. 627 */ 628 static int 629 dounmount_interlock(struct mount *mp) 630 { 631 if (mp->mnt_kern_flag & MNTK_UNMOUNT) 632 return (EBUSY); 633 mp->mnt_kern_flag |= MNTK_UNMOUNT; 634 return(0); 635 } 636 637 static int 638 unmount_allproc_cb(struct proc *p, void *arg) 639 { 640 struct mount *mp; 641 642 if (p->p_textnch.ncp == NULL) 643 return 0; 644 645 mp = (struct mount *)arg; 646 if (p->p_textnch.mount == mp) 647 cache_drop(&p->p_textnch); 648 649 return 0; 650 } 651 652 int 653 dounmount(struct mount *mp, int flags) 654 { 655 struct namecache *ncp; 656 struct nchandle nch; 657 struct vnode *vp; 658 int error; 659 int async_flag; 660 int lflags; 661 int freeok = 1; 662 663 /* 664 * Exclusive access for unmounting purposes 665 */ 666 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0) 667 return (error); 668 669 /* 670 * Allow filesystems to detect that a forced unmount is in progress. 671 */ 672 if (flags & MNT_FORCE) 673 mp->mnt_kern_flag |= MNTK_UNMOUNTF; 674 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_NOWAIT); 675 error = lockmgr(&mp->mnt_lock, lflags); 676 if (error) { 677 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF); 678 if (mp->mnt_kern_flag & MNTK_MWAIT) 679 wakeup(mp); 680 return (error); 681 } 682 683 if (mp->mnt_flag & MNT_EXPUBLIC) 684 vfs_setpublicfs(NULL, NULL, NULL); 685 686 vfs_msync(mp, MNT_WAIT); 687 async_flag = mp->mnt_flag & MNT_ASYNC; 688 mp->mnt_flag &=~ MNT_ASYNC; 689 690 /* 691 * If this filesystem isn't aliasing other filesystems, 692 * try to invalidate any remaining namecache entries and 693 * check the count afterwords. 694 */ 695 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) { 696 cache_lock(&mp->mnt_ncmountpt); 697 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN); 698 cache_unlock(&mp->mnt_ncmountpt); 699 700 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL && 701 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) { 702 allproc_scan(&unmount_allproc_cb, mp); 703 } 704 705 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL && 706 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) { 707 708 if ((flags & MNT_FORCE) == 0) { 709 error = EBUSY; 710 mount_warning(mp, "Cannot unmount: " 711 "%d namecache " 712 "references still " 713 "present", 714 ncp->nc_refs - 1); 715 } else { 716 mount_warning(mp, "Forced unmount: " 717 "%d namecache " 718 "references still " 719 "present", 720 ncp->nc_refs - 1); 721 freeok = 0; 722 } 723 } 724 } 725 726 /* 727 * nchandle records ref the mount structure. Expect a count of 1 728 * (our mount->mnt_ncmountpt). 729 */ 730 if (mp->mnt_refs != 1) { 731 if ((flags & MNT_FORCE) == 0) { 732 mount_warning(mp, "Cannot unmount: " 733 "%d process references still " 734 "present", mp->mnt_refs); 735 error = EBUSY; 736 } else { 737 mount_warning(mp, "Forced unmount: " 738 "%d process references still " 739 "present", mp->mnt_refs); 740 freeok = 0; 741 } 742 } 743 744 /* 745 * Decomission our special mnt_syncer vnode. This also stops 746 * the vnlru code. If we are unable to unmount we recommission 747 * the vnode. 748 */ 749 if (error == 0) { 750 if ((vp = mp->mnt_syncer) != NULL) { 751 mp->mnt_syncer = NULL; 752 vrele(vp); 753 } 754 if (((mp->mnt_flag & MNT_RDONLY) || 755 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) || 756 (flags & MNT_FORCE)) { 757 error = VFS_UNMOUNT(mp, flags); 758 } 759 } 760 if (error) { 761 if (mp->mnt_syncer == NULL) 762 vfs_allocate_syncvnode(mp); 763 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF); 764 mp->mnt_flag |= async_flag; 765 lockmgr(&mp->mnt_lock, LK_RELEASE); 766 if (mp->mnt_kern_flag & MNTK_MWAIT) 767 wakeup(mp); 768 return (error); 769 } 770 /* 771 * Clean up any journals still associated with the mount after 772 * filesystem activity has ceased. 773 */ 774 journal_remove_all_journals(mp, 775 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0)); 776 777 mountlist_remove(mp); 778 779 /* 780 * Remove any installed vnode ops here so the individual VFSs don't 781 * have to. 782 */ 783 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops); 784 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops); 785 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops); 786 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops); 787 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops); 788 789 if (mp->mnt_ncmountpt.ncp != NULL) { 790 nch = mp->mnt_ncmountpt; 791 cache_zero(&mp->mnt_ncmountpt); 792 cache_clrmountpt(&nch); 793 cache_drop(&nch); 794 } 795 if (mp->mnt_ncmounton.ncp != NULL) { 796 nch = mp->mnt_ncmounton; 797 cache_zero(&mp->mnt_ncmounton); 798 cache_clrmountpt(&nch); 799 cache_drop(&nch); 800 } 801 802 mp->mnt_vfc->vfc_refcount--; 803 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist)) 804 panic("unmount: dangling vnode"); 805 lockmgr(&mp->mnt_lock, LK_RELEASE); 806 if (mp->mnt_kern_flag & MNTK_MWAIT) 807 wakeup(mp); 808 if (freeok) 809 kfree(mp, M_MOUNT); 810 return (0); 811 } 812 813 static 814 void 815 mount_warning(struct mount *mp, const char *ctl, ...) 816 { 817 char *ptr; 818 char *buf; 819 __va_list va; 820 821 __va_start(va, ctl); 822 if (cache_fullpath(NULL, &mp->mnt_ncmounton, &ptr, &buf, 0) == 0) { 823 kprintf("unmount(%s): ", ptr); 824 kvprintf(ctl, va); 825 kprintf("\n"); 826 kfree(buf, M_TEMP); 827 } else { 828 kprintf("unmount(%p", mp); 829 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name) 830 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name); 831 kprintf("): "); 832 kvprintf(ctl, va); 833 kprintf("\n"); 834 } 835 __va_end(va); 836 } 837 838 /* 839 * Shim cache_fullpath() to handle the case where a process is chrooted into 840 * a subdirectory of a mount. In this case if the root mount matches the 841 * process root directory's mount we have to specify the process's root 842 * directory instead of the mount point, because the mount point might 843 * be above the root directory. 844 */ 845 static 846 int 847 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb) 848 { 849 struct nchandle *nch; 850 851 if (p && p->p_fd->fd_nrdir.mount == mp) 852 nch = &p->p_fd->fd_nrdir; 853 else 854 nch = &mp->mnt_ncmountpt; 855 return(cache_fullpath(p, nch, rb, fb, 0)); 856 } 857 858 /* 859 * Sync each mounted filesystem. 860 */ 861 862 #ifdef DEBUG 863 static int syncprt = 0; 864 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, ""); 865 #endif /* DEBUG */ 866 867 static int sync_callback(struct mount *mp, void *data); 868 869 int 870 sys_sync(struct sync_args *uap) 871 { 872 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD); 873 #ifdef DEBUG 874 /* 875 * print out buffer pool stat information on each sync() call. 876 */ 877 if (syncprt) 878 vfs_bufstats(); 879 #endif /* DEBUG */ 880 return (0); 881 } 882 883 static 884 int 885 sync_callback(struct mount *mp, void *data __unused) 886 { 887 int asyncflag; 888 889 if ((mp->mnt_flag & MNT_RDONLY) == 0) { 890 asyncflag = mp->mnt_flag & MNT_ASYNC; 891 mp->mnt_flag &= ~MNT_ASYNC; 892 vfs_msync(mp, MNT_NOWAIT); 893 VFS_SYNC(mp, MNT_NOWAIT | MNT_LAZY); 894 mp->mnt_flag |= asyncflag; 895 } 896 return(0); 897 } 898 899 /* XXX PRISON: could be per prison flag */ 900 static int prison_quotas; 901 #if 0 902 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, ""); 903 #endif 904 905 /* 906 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg) 907 * 908 * Change filesystem quotas. 909 * 910 * MPALMOSTSAFE 911 */ 912 int 913 sys_quotactl(struct quotactl_args *uap) 914 { 915 struct nlookupdata nd; 916 struct thread *td; 917 struct mount *mp; 918 int error; 919 920 get_mplock(); 921 td = curthread; 922 if (td->td_ucred->cr_prison && !prison_quotas) { 923 error = EPERM; 924 goto done; 925 } 926 927 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 928 if (error == 0) 929 error = nlookup(&nd); 930 if (error == 0) { 931 mp = nd.nl_nch.mount; 932 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, 933 uap->arg, nd.nl_cred); 934 } 935 nlookup_done(&nd); 936 done: 937 rel_mplock(); 938 return (error); 939 } 940 941 /* 942 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen, 943 * void *buf, int buflen) 944 * 945 * This function operates on a mount point and executes the specified 946 * operation using the specified control data, and possibly returns data. 947 * 948 * The actual number of bytes stored in the result buffer is returned, 0 949 * if none, otherwise an error is returned. 950 * 951 * MPALMOSTSAFE 952 */ 953 int 954 sys_mountctl(struct mountctl_args *uap) 955 { 956 struct thread *td = curthread; 957 struct proc *p = td->td_proc; 958 struct file *fp; 959 void *ctl = NULL; 960 void *buf = NULL; 961 char *path = NULL; 962 int error; 963 964 /* 965 * Sanity and permissions checks. We must be root. 966 */ 967 KKASSERT(p); 968 if (td->td_ucred->cr_prison != NULL) 969 return (EPERM); 970 if ((uap->op != MOUNTCTL_MOUNTFLAGS) && 971 (error = priv_check(td, PRIV_ROOT)) != 0) 972 return (error); 973 974 /* 975 * Argument length checks 976 */ 977 if (uap->ctllen < 0 || uap->ctllen > 1024) 978 return (EINVAL); 979 if (uap->buflen < 0 || uap->buflen > 16 * 1024) 980 return (EINVAL); 981 if (uap->path == NULL) 982 return (EINVAL); 983 984 /* 985 * Allocate the necessary buffers and copyin data 986 */ 987 path = objcache_get(namei_oc, M_WAITOK); 988 error = copyinstr(uap->path, path, MAXPATHLEN, NULL); 989 if (error) 990 goto done; 991 992 if (uap->ctllen) { 993 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO); 994 error = copyin(uap->ctl, ctl, uap->ctllen); 995 if (error) 996 goto done; 997 } 998 if (uap->buflen) 999 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO); 1000 1001 /* 1002 * Validate the descriptor 1003 */ 1004 if (uap->fd >= 0) { 1005 fp = holdfp(p->p_fd, uap->fd, -1); 1006 if (fp == NULL) { 1007 error = EBADF; 1008 goto done; 1009 } 1010 } else { 1011 fp = NULL; 1012 } 1013 1014 /* 1015 * Execute the internal kernel function and clean up. 1016 */ 1017 get_mplock(); 1018 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result); 1019 rel_mplock(); 1020 if (fp) 1021 fdrop(fp); 1022 if (error == 0 && uap->sysmsg_result > 0) 1023 error = copyout(buf, uap->buf, uap->sysmsg_result); 1024 done: 1025 if (path) 1026 objcache_put(namei_oc, path); 1027 if (ctl) 1028 kfree(ctl, M_TEMP); 1029 if (buf) 1030 kfree(buf, M_TEMP); 1031 return (error); 1032 } 1033 1034 /* 1035 * Execute a mount control operation by resolving the path to a mount point 1036 * and calling vop_mountctl(). 1037 * 1038 * Use the mount point from the nch instead of the vnode so nullfs mounts 1039 * can properly spike the VOP. 1040 */ 1041 int 1042 kern_mountctl(const char *path, int op, struct file *fp, 1043 const void *ctl, int ctllen, 1044 void *buf, int buflen, int *res) 1045 { 1046 struct vnode *vp; 1047 struct mount *mp; 1048 struct nlookupdata nd; 1049 int error; 1050 1051 *res = 0; 1052 vp = NULL; 1053 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW); 1054 if (error == 0) 1055 error = nlookup(&nd); 1056 if (error == 0) 1057 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp); 1058 mp = nd.nl_nch.mount; 1059 nlookup_done(&nd); 1060 if (error) 1061 return (error); 1062 vn_unlock(vp); 1063 1064 /* 1065 * Must be the root of the filesystem 1066 */ 1067 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) { 1068 vrele(vp); 1069 return (EINVAL); 1070 } 1071 error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen, 1072 buf, buflen, res); 1073 vrele(vp); 1074 return (error); 1075 } 1076 1077 int 1078 kern_statfs(struct nlookupdata *nd, struct statfs *buf) 1079 { 1080 struct thread *td = curthread; 1081 struct proc *p = td->td_proc; 1082 struct mount *mp; 1083 struct statfs *sp; 1084 char *fullpath, *freepath; 1085 int error; 1086 1087 if ((error = nlookup(nd)) != 0) 1088 return (error); 1089 mp = nd->nl_nch.mount; 1090 sp = &mp->mnt_stat; 1091 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0) 1092 return (error); 1093 1094 error = mount_path(p, mp, &fullpath, &freepath); 1095 if (error) 1096 return(error); 1097 bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 1098 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname)); 1099 kfree(freepath, M_TEMP); 1100 1101 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; 1102 bcopy(sp, buf, sizeof(*buf)); 1103 /* Only root should have access to the fsid's. */ 1104 if (priv_check(td, PRIV_ROOT)) 1105 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0; 1106 return (0); 1107 } 1108 1109 /* 1110 * statfs_args(char *path, struct statfs *buf) 1111 * 1112 * Get filesystem statistics. 1113 */ 1114 int 1115 sys_statfs(struct statfs_args *uap) 1116 { 1117 struct nlookupdata nd; 1118 struct statfs buf; 1119 int error; 1120 1121 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 1122 if (error == 0) 1123 error = kern_statfs(&nd, &buf); 1124 nlookup_done(&nd); 1125 if (error == 0) 1126 error = copyout(&buf, uap->buf, sizeof(*uap->buf)); 1127 return (error); 1128 } 1129 1130 int 1131 kern_fstatfs(int fd, struct statfs *buf) 1132 { 1133 struct thread *td = curthread; 1134 struct proc *p = td->td_proc; 1135 struct file *fp; 1136 struct mount *mp; 1137 struct statfs *sp; 1138 char *fullpath, *freepath; 1139 int error; 1140 1141 KKASSERT(p); 1142 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0) 1143 return (error); 1144 1145 /* 1146 * Try to use mount info from any overlays rather than the 1147 * mount info for the underlying vnode, otherwise we will 1148 * fail when operating on null-mounted paths inside a chroot. 1149 */ 1150 if ((mp = fp->f_nchandle.mount) == NULL) 1151 mp = ((struct vnode *)fp->f_data)->v_mount; 1152 if (mp == NULL) { 1153 error = EBADF; 1154 goto done; 1155 } 1156 if (fp->f_cred == NULL) { 1157 error = EINVAL; 1158 goto done; 1159 } 1160 sp = &mp->mnt_stat; 1161 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0) 1162 goto done; 1163 1164 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0) 1165 goto done; 1166 bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 1167 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname)); 1168 kfree(freepath, M_TEMP); 1169 1170 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; 1171 bcopy(sp, buf, sizeof(*buf)); 1172 1173 /* Only root should have access to the fsid's. */ 1174 if (priv_check(td, PRIV_ROOT)) 1175 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0; 1176 error = 0; 1177 done: 1178 fdrop(fp); 1179 return (error); 1180 } 1181 1182 /* 1183 * fstatfs_args(int fd, struct statfs *buf) 1184 * 1185 * Get filesystem statistics. 1186 */ 1187 int 1188 sys_fstatfs(struct fstatfs_args *uap) 1189 { 1190 struct statfs buf; 1191 int error; 1192 1193 error = kern_fstatfs(uap->fd, &buf); 1194 1195 if (error == 0) 1196 error = copyout(&buf, uap->buf, sizeof(*uap->buf)); 1197 return (error); 1198 } 1199 1200 int 1201 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf) 1202 { 1203 struct mount *mp; 1204 struct statvfs *sp; 1205 int error; 1206 1207 if ((error = nlookup(nd)) != 0) 1208 return (error); 1209 mp = nd->nl_nch.mount; 1210 sp = &mp->mnt_vstat; 1211 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0) 1212 return (error); 1213 1214 sp->f_flag = 0; 1215 if (mp->mnt_flag & MNT_RDONLY) 1216 sp->f_flag |= ST_RDONLY; 1217 if (mp->mnt_flag & MNT_NOSUID) 1218 sp->f_flag |= ST_NOSUID; 1219 bcopy(sp, buf, sizeof(*buf)); 1220 return (0); 1221 } 1222 1223 /* 1224 * statfs_args(char *path, struct statfs *buf) 1225 * 1226 * Get filesystem statistics. 1227 */ 1228 int 1229 sys_statvfs(struct statvfs_args *uap) 1230 { 1231 struct nlookupdata nd; 1232 struct statvfs buf; 1233 int error; 1234 1235 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 1236 if (error == 0) 1237 error = kern_statvfs(&nd, &buf); 1238 nlookup_done(&nd); 1239 if (error == 0) 1240 error = copyout(&buf, uap->buf, sizeof(*uap->buf)); 1241 return (error); 1242 } 1243 1244 int 1245 kern_fstatvfs(int fd, struct statvfs *buf) 1246 { 1247 struct thread *td = curthread; 1248 struct proc *p = td->td_proc; 1249 struct file *fp; 1250 struct mount *mp; 1251 struct statvfs *sp; 1252 int error; 1253 1254 KKASSERT(p); 1255 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0) 1256 return (error); 1257 if ((mp = fp->f_nchandle.mount) == NULL) 1258 mp = ((struct vnode *)fp->f_data)->v_mount; 1259 if (mp == NULL) { 1260 error = EBADF; 1261 goto done; 1262 } 1263 if (fp->f_cred == NULL) { 1264 error = EINVAL; 1265 goto done; 1266 } 1267 sp = &mp->mnt_vstat; 1268 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0) 1269 goto done; 1270 1271 sp->f_flag = 0; 1272 if (mp->mnt_flag & MNT_RDONLY) 1273 sp->f_flag |= ST_RDONLY; 1274 if (mp->mnt_flag & MNT_NOSUID) 1275 sp->f_flag |= ST_NOSUID; 1276 1277 bcopy(sp, buf, sizeof(*buf)); 1278 error = 0; 1279 done: 1280 fdrop(fp); 1281 return (error); 1282 } 1283 1284 /* 1285 * fstatfs_args(int fd, struct statfs *buf) 1286 * 1287 * Get filesystem statistics. 1288 */ 1289 int 1290 sys_fstatvfs(struct fstatvfs_args *uap) 1291 { 1292 struct statvfs buf; 1293 int error; 1294 1295 error = kern_fstatvfs(uap->fd, &buf); 1296 1297 if (error == 0) 1298 error = copyout(&buf, uap->buf, sizeof(*uap->buf)); 1299 return (error); 1300 } 1301 1302 /* 1303 * getfsstat_args(struct statfs *buf, long bufsize, int flags) 1304 * 1305 * Get statistics on all filesystems. 1306 */ 1307 1308 struct getfsstat_info { 1309 struct statfs *sfsp; 1310 long count; 1311 long maxcount; 1312 int error; 1313 int flags; 1314 struct thread *td; 1315 }; 1316 1317 static int getfsstat_callback(struct mount *, void *); 1318 1319 int 1320 sys_getfsstat(struct getfsstat_args *uap) 1321 { 1322 struct thread *td = curthread; 1323 struct getfsstat_info info; 1324 1325 bzero(&info, sizeof(info)); 1326 1327 info.maxcount = uap->bufsize / sizeof(struct statfs); 1328 info.sfsp = uap->buf; 1329 info.count = 0; 1330 info.flags = uap->flags; 1331 info.td = td; 1332 1333 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD); 1334 if (info.sfsp && info.count > info.maxcount) 1335 uap->sysmsg_result = info.maxcount; 1336 else 1337 uap->sysmsg_result = info.count; 1338 return (info.error); 1339 } 1340 1341 static int 1342 getfsstat_callback(struct mount *mp, void *data) 1343 { 1344 struct getfsstat_info *info = data; 1345 struct statfs *sp; 1346 char *freepath; 1347 char *fullpath; 1348 int error; 1349 1350 if (info->sfsp && info->count < info->maxcount) { 1351 if (info->td->td_proc && 1352 !chroot_visible_mnt(mp, info->td->td_proc)) { 1353 return(0); 1354 } 1355 sp = &mp->mnt_stat; 1356 1357 /* 1358 * If MNT_NOWAIT or MNT_LAZY is specified, do not 1359 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY 1360 * overrides MNT_WAIT. 1361 */ 1362 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 || 1363 (info->flags & MNT_WAIT)) && 1364 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) { 1365 return(0); 1366 } 1367 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; 1368 1369 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath); 1370 if (error) { 1371 info->error = error; 1372 return(-1); 1373 } 1374 bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 1375 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname)); 1376 kfree(freepath, M_TEMP); 1377 1378 error = copyout(sp, info->sfsp, sizeof(*sp)); 1379 if (error) { 1380 info->error = error; 1381 return (-1); 1382 } 1383 ++info->sfsp; 1384 } 1385 info->count++; 1386 return(0); 1387 } 1388 1389 /* 1390 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf, 1391 long bufsize, int flags) 1392 * 1393 * Get statistics on all filesystems. 1394 */ 1395 1396 struct getvfsstat_info { 1397 struct statfs *sfsp; 1398 struct statvfs *vsfsp; 1399 long count; 1400 long maxcount; 1401 int error; 1402 int flags; 1403 struct thread *td; 1404 }; 1405 1406 static int getvfsstat_callback(struct mount *, void *); 1407 1408 int 1409 sys_getvfsstat(struct getvfsstat_args *uap) 1410 { 1411 struct thread *td = curthread; 1412 struct getvfsstat_info info; 1413 1414 bzero(&info, sizeof(info)); 1415 1416 info.maxcount = uap->vbufsize / sizeof(struct statvfs); 1417 info.sfsp = uap->buf; 1418 info.vsfsp = uap->vbuf; 1419 info.count = 0; 1420 info.flags = uap->flags; 1421 info.td = td; 1422 1423 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD); 1424 if (info.vsfsp && info.count > info.maxcount) 1425 uap->sysmsg_result = info.maxcount; 1426 else 1427 uap->sysmsg_result = info.count; 1428 return (info.error); 1429 } 1430 1431 static int 1432 getvfsstat_callback(struct mount *mp, void *data) 1433 { 1434 struct getvfsstat_info *info = data; 1435 struct statfs *sp; 1436 struct statvfs *vsp; 1437 char *freepath; 1438 char *fullpath; 1439 int error; 1440 1441 if (info->vsfsp && info->count < info->maxcount) { 1442 if (info->td->td_proc && 1443 !chroot_visible_mnt(mp, info->td->td_proc)) { 1444 return(0); 1445 } 1446 sp = &mp->mnt_stat; 1447 vsp = &mp->mnt_vstat; 1448 1449 /* 1450 * If MNT_NOWAIT or MNT_LAZY is specified, do not 1451 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY 1452 * overrides MNT_WAIT. 1453 */ 1454 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 || 1455 (info->flags & MNT_WAIT)) && 1456 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) { 1457 return(0); 1458 } 1459 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; 1460 1461 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 || 1462 (info->flags & MNT_WAIT)) && 1463 (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) { 1464 return(0); 1465 } 1466 vsp->f_flag = 0; 1467 if (mp->mnt_flag & MNT_RDONLY) 1468 vsp->f_flag |= ST_RDONLY; 1469 if (mp->mnt_flag & MNT_NOSUID) 1470 vsp->f_flag |= ST_NOSUID; 1471 1472 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath); 1473 if (error) { 1474 info->error = error; 1475 return(-1); 1476 } 1477 bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 1478 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname)); 1479 kfree(freepath, M_TEMP); 1480 1481 error = copyout(sp, info->sfsp, sizeof(*sp)); 1482 if (error == 0) 1483 error = copyout(vsp, info->vsfsp, sizeof(*vsp)); 1484 if (error) { 1485 info->error = error; 1486 return (-1); 1487 } 1488 ++info->sfsp; 1489 ++info->vsfsp; 1490 } 1491 info->count++; 1492 return(0); 1493 } 1494 1495 1496 /* 1497 * fchdir_args(int fd) 1498 * 1499 * Change current working directory to a given file descriptor. 1500 */ 1501 int 1502 sys_fchdir(struct fchdir_args *uap) 1503 { 1504 struct thread *td = curthread; 1505 struct proc *p = td->td_proc; 1506 struct filedesc *fdp = p->p_fd; 1507 struct vnode *vp, *ovp; 1508 struct mount *mp; 1509 struct file *fp; 1510 struct nchandle nch, onch, tnch; 1511 int error; 1512 1513 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0) 1514 return (error); 1515 lwkt_gettoken(&p->p_token); 1516 vp = (struct vnode *)fp->f_data; 1517 vref(vp); 1518 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1519 if (fp->f_nchandle.ncp == NULL) 1520 error = ENOTDIR; 1521 else 1522 error = checkvp_chdir(vp, td); 1523 if (error) { 1524 vput(vp); 1525 goto done; 1526 } 1527 cache_copy(&fp->f_nchandle, &nch); 1528 1529 /* 1530 * If the ncp has become a mount point, traverse through 1531 * the mount point. 1532 */ 1533 1534 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) && 1535 (mp = cache_findmount(&nch)) != NULL 1536 ) { 1537 error = nlookup_mp(mp, &tnch); 1538 if (error == 0) { 1539 cache_unlock(&tnch); /* leave ref intact */ 1540 vput(vp); 1541 vp = tnch.ncp->nc_vp; 1542 error = vget(vp, LK_SHARED); 1543 KKASSERT(error == 0); 1544 cache_drop(&nch); 1545 nch = tnch; 1546 } 1547 } 1548 if (error == 0) { 1549 ovp = fdp->fd_cdir; 1550 onch = fdp->fd_ncdir; 1551 vn_unlock(vp); /* leave ref intact */ 1552 fdp->fd_cdir = vp; 1553 fdp->fd_ncdir = nch; 1554 cache_drop(&onch); 1555 vrele(ovp); 1556 } else { 1557 cache_drop(&nch); 1558 vput(vp); 1559 } 1560 fdrop(fp); 1561 done: 1562 lwkt_reltoken(&p->p_token); 1563 return (error); 1564 } 1565 1566 int 1567 kern_chdir(struct nlookupdata *nd) 1568 { 1569 struct thread *td = curthread; 1570 struct proc *p = td->td_proc; 1571 struct filedesc *fdp = p->p_fd; 1572 struct vnode *vp, *ovp; 1573 struct nchandle onch; 1574 int error; 1575 1576 if ((error = nlookup(nd)) != 0) 1577 return (error); 1578 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL) 1579 return (ENOENT); 1580 if ((error = vget(vp, LK_SHARED)) != 0) 1581 return (error); 1582 1583 lwkt_gettoken(&p->p_token); 1584 error = checkvp_chdir(vp, td); 1585 vn_unlock(vp); 1586 if (error == 0) { 1587 ovp = fdp->fd_cdir; 1588 onch = fdp->fd_ncdir; 1589 cache_unlock(&nd->nl_nch); /* leave reference intact */ 1590 fdp->fd_ncdir = nd->nl_nch; 1591 fdp->fd_cdir = vp; 1592 cache_drop(&onch); 1593 vrele(ovp); 1594 cache_zero(&nd->nl_nch); 1595 } else { 1596 vrele(vp); 1597 } 1598 lwkt_reltoken(&p->p_token); 1599 return (error); 1600 } 1601 1602 /* 1603 * chdir_args(char *path) 1604 * 1605 * Change current working directory (``.''). 1606 */ 1607 int 1608 sys_chdir(struct chdir_args *uap) 1609 { 1610 struct nlookupdata nd; 1611 int error; 1612 1613 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 1614 if (error == 0) 1615 error = kern_chdir(&nd); 1616 nlookup_done(&nd); 1617 return (error); 1618 } 1619 1620 /* 1621 * Helper function for raised chroot(2) security function: Refuse if 1622 * any filedescriptors are open directories. 1623 */ 1624 static int 1625 chroot_refuse_vdir_fds(struct filedesc *fdp) 1626 { 1627 struct vnode *vp; 1628 struct file *fp; 1629 int error; 1630 int fd; 1631 1632 for (fd = 0; fd < fdp->fd_nfiles ; fd++) { 1633 if ((error = holdvnode(fdp, fd, &fp)) != 0) 1634 continue; 1635 vp = (struct vnode *)fp->f_data; 1636 if (vp->v_type != VDIR) { 1637 fdrop(fp); 1638 continue; 1639 } 1640 fdrop(fp); 1641 return(EPERM); 1642 } 1643 return (0); 1644 } 1645 1646 /* 1647 * This sysctl determines if we will allow a process to chroot(2) if it 1648 * has a directory open: 1649 * 0: disallowed for all processes. 1650 * 1: allowed for processes that were not already chroot(2)'ed. 1651 * 2: allowed for all processes. 1652 */ 1653 1654 static int chroot_allow_open_directories = 1; 1655 1656 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW, 1657 &chroot_allow_open_directories, 0, ""); 1658 1659 /* 1660 * chroot to the specified namecache entry. We obtain the vp from the 1661 * namecache data. The passed ncp must be locked and referenced and will 1662 * remain locked and referenced on return. 1663 */ 1664 int 1665 kern_chroot(struct nchandle *nch) 1666 { 1667 struct thread *td = curthread; 1668 struct proc *p = td->td_proc; 1669 struct filedesc *fdp = p->p_fd; 1670 struct vnode *vp; 1671 int error; 1672 1673 /* 1674 * Only privileged user can chroot 1675 */ 1676 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0); 1677 if (error) 1678 return (error); 1679 1680 /* 1681 * Disallow open directory descriptors (fchdir() breakouts). 1682 */ 1683 if (chroot_allow_open_directories == 0 || 1684 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) { 1685 if ((error = chroot_refuse_vdir_fds(fdp)) != 0) 1686 return (error); 1687 } 1688 if ((vp = nch->ncp->nc_vp) == NULL) 1689 return (ENOENT); 1690 1691 if ((error = vget(vp, LK_SHARED)) != 0) 1692 return (error); 1693 1694 /* 1695 * Check the validity of vp as a directory to change to and 1696 * associate it with rdir/jdir. 1697 */ 1698 error = checkvp_chdir(vp, td); 1699 vn_unlock(vp); /* leave reference intact */ 1700 if (error == 0) { 1701 vrele(fdp->fd_rdir); 1702 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */ 1703 cache_drop(&fdp->fd_nrdir); 1704 cache_copy(nch, &fdp->fd_nrdir); 1705 if (fdp->fd_jdir == NULL) { 1706 fdp->fd_jdir = vp; 1707 vref(fdp->fd_jdir); 1708 cache_copy(nch, &fdp->fd_njdir); 1709 } 1710 } else { 1711 vrele(vp); 1712 } 1713 return (error); 1714 } 1715 1716 /* 1717 * chroot_args(char *path) 1718 * 1719 * Change notion of root (``/'') directory. 1720 */ 1721 int 1722 sys_chroot(struct chroot_args *uap) 1723 { 1724 struct thread *td __debugvar = curthread; 1725 struct nlookupdata nd; 1726 int error; 1727 1728 KKASSERT(td->td_proc); 1729 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 1730 if (error == 0) { 1731 nd.nl_flags |= NLC_EXEC; 1732 error = nlookup(&nd); 1733 if (error == 0) 1734 error = kern_chroot(&nd.nl_nch); 1735 } 1736 nlookup_done(&nd); 1737 return(error); 1738 } 1739 1740 int 1741 sys_chroot_kernel(struct chroot_kernel_args *uap) 1742 { 1743 struct thread *td = curthread; 1744 struct nlookupdata nd; 1745 struct nchandle *nch; 1746 struct vnode *vp; 1747 int error; 1748 1749 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 1750 if (error) 1751 goto error_nond; 1752 1753 error = nlookup(&nd); 1754 if (error) 1755 goto error_out; 1756 1757 nch = &nd.nl_nch; 1758 1759 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0); 1760 if (error) 1761 goto error_out; 1762 1763 if ((vp = nch->ncp->nc_vp) == NULL) { 1764 error = ENOENT; 1765 goto error_out; 1766 } 1767 1768 if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0) 1769 goto error_out; 1770 1771 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap->path); 1772 get_mplock(); 1773 vfs_cache_setroot(vp, cache_hold(nch)); 1774 rel_mplock(); 1775 1776 error_out: 1777 nlookup_done(&nd); 1778 error_nond: 1779 return(error); 1780 } 1781 1782 /* 1783 * Common routine for chroot and chdir. Given a locked, referenced vnode, 1784 * determine whether it is legal to chdir to the vnode. The vnode's state 1785 * is not changed by this call. 1786 */ 1787 int 1788 checkvp_chdir(struct vnode *vp, struct thread *td) 1789 { 1790 int error; 1791 1792 if (vp->v_type != VDIR) 1793 error = ENOTDIR; 1794 else 1795 error = VOP_EACCESS(vp, VEXEC, td->td_ucred); 1796 return (error); 1797 } 1798 1799 int 1800 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res) 1801 { 1802 struct thread *td = curthread; 1803 struct proc *p = td->td_proc; 1804 struct lwp *lp = td->td_lwp; 1805 struct filedesc *fdp = p->p_fd; 1806 int cmode, flags; 1807 struct file *nfp; 1808 struct file *fp; 1809 struct vnode *vp; 1810 int type, indx, error; 1811 struct flock lf; 1812 1813 if ((oflags & O_ACCMODE) == O_ACCMODE) 1814 return (EINVAL); 1815 flags = FFLAGS(oflags); 1816 error = falloc(lp, &nfp, NULL); 1817 if (error) 1818 return (error); 1819 fp = nfp; 1820 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT; 1821 1822 /* 1823 * XXX p_dupfd is a real mess. It allows a device to return a 1824 * file descriptor to be duplicated rather then doing the open 1825 * itself. 1826 */ 1827 lp->lwp_dupfd = -1; 1828 1829 /* 1830 * Call vn_open() to do the lookup and assign the vnode to the 1831 * file pointer. vn_open() does not change the ref count on fp 1832 * and the vnode, on success, will be inherited by the file pointer 1833 * and unlocked. 1834 */ 1835 nd->nl_flags |= NLC_LOCKVP; 1836 error = vn_open(nd, fp, flags, cmode); 1837 nlookup_done(nd); 1838 if (error) { 1839 /* 1840 * handle special fdopen() case. bleh. dupfdopen() is 1841 * responsible for dropping the old contents of ofiles[indx] 1842 * if it succeeds. 1843 * 1844 * Note that fsetfd() will add a ref to fp which represents 1845 * the fd_files[] assignment. We must still drop our 1846 * reference. 1847 */ 1848 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) { 1849 if (fdalloc(p, 0, &indx) == 0) { 1850 error = dupfdopen(fdp, indx, lp->lwp_dupfd, flags, error); 1851 if (error == 0) { 1852 *res = indx; 1853 fdrop(fp); /* our ref */ 1854 return (0); 1855 } 1856 fsetfd(fdp, NULL, indx); 1857 } 1858 } 1859 fdrop(fp); /* our ref */ 1860 if (error == ERESTART) 1861 error = EINTR; 1862 return (error); 1863 } 1864 1865 /* 1866 * ref the vnode for ourselves so it can't be ripped out from under 1867 * is. XXX need an ND flag to request that the vnode be returned 1868 * anyway. 1869 * 1870 * Reserve a file descriptor but do not assign it until the open 1871 * succeeds. 1872 */ 1873 vp = (struct vnode *)fp->f_data; 1874 vref(vp); 1875 if ((error = fdalloc(p, 0, &indx)) != 0) { 1876 fdrop(fp); 1877 vrele(vp); 1878 return (error); 1879 } 1880 1881 /* 1882 * If no error occurs the vp will have been assigned to the file 1883 * pointer. 1884 */ 1885 lp->lwp_dupfd = 0; 1886 1887 if (flags & (O_EXLOCK | O_SHLOCK)) { 1888 lf.l_whence = SEEK_SET; 1889 lf.l_start = 0; 1890 lf.l_len = 0; 1891 if (flags & O_EXLOCK) 1892 lf.l_type = F_WRLCK; 1893 else 1894 lf.l_type = F_RDLCK; 1895 if (flags & FNONBLOCK) 1896 type = 0; 1897 else 1898 type = F_WAIT; 1899 1900 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) { 1901 /* 1902 * lock request failed. Clean up the reserved 1903 * descriptor. 1904 */ 1905 vrele(vp); 1906 fsetfd(fdp, NULL, indx); 1907 fdrop(fp); 1908 return (error); 1909 } 1910 fp->f_flag |= FHASLOCK; 1911 } 1912 #if 0 1913 /* 1914 * Assert that all regular file vnodes were created with a object. 1915 */ 1916 KASSERT(vp->v_type != VREG || vp->v_object != NULL, 1917 ("open: regular file has no backing object after vn_open")); 1918 #endif 1919 1920 vrele(vp); 1921 1922 /* 1923 * release our private reference, leaving the one associated with the 1924 * descriptor table intact. 1925 */ 1926 fsetfd(fdp, fp, indx); 1927 fdrop(fp); 1928 *res = indx; 1929 return (0); 1930 } 1931 1932 /* 1933 * open_args(char *path, int flags, int mode) 1934 * 1935 * Check permissions, allocate an open file structure, 1936 * and call the device open routine if any. 1937 */ 1938 int 1939 sys_open(struct open_args *uap) 1940 { 1941 struct nlookupdata nd; 1942 int error; 1943 1944 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); 1945 if (error == 0) { 1946 error = kern_open(&nd, uap->flags, 1947 uap->mode, &uap->sysmsg_result); 1948 } 1949 nlookup_done(&nd); 1950 return (error); 1951 } 1952 1953 /* 1954 * openat_args(int fd, char *path, int flags, int mode) 1955 */ 1956 int 1957 sys_openat(struct openat_args *uap) 1958 { 1959 struct nlookupdata nd; 1960 int error; 1961 struct file *fp; 1962 1963 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0); 1964 if (error == 0) { 1965 error = kern_open(&nd, uap->flags, uap->mode, 1966 &uap->sysmsg_result); 1967 } 1968 nlookup_done_at(&nd, fp); 1969 return (error); 1970 } 1971 1972 int 1973 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor) 1974 { 1975 struct thread *td = curthread; 1976 struct proc *p = td->td_proc; 1977 struct vnode *vp; 1978 struct vattr vattr; 1979 int error; 1980 int whiteout = 0; 1981 1982 KKASSERT(p); 1983 1984 VATTR_NULL(&vattr); 1985 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask; 1986 vattr.va_rmajor = rmajor; 1987 vattr.va_rminor = rminor; 1988 1989 switch (mode & S_IFMT) { 1990 case S_IFMT: /* used by badsect to flag bad sectors */ 1991 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0); 1992 vattr.va_type = VBAD; 1993 break; 1994 case S_IFCHR: 1995 error = priv_check(td, PRIV_VFS_MKNOD_DEV); 1996 vattr.va_type = VCHR; 1997 break; 1998 case S_IFBLK: 1999 error = priv_check(td, PRIV_VFS_MKNOD_DEV); 2000 vattr.va_type = VBLK; 2001 break; 2002 case S_IFWHT: 2003 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0); 2004 whiteout = 1; 2005 break; 2006 case S_IFDIR: /* special directories support for HAMMER */ 2007 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0); 2008 vattr.va_type = VDIR; 2009 break; 2010 default: 2011 error = EINVAL; 2012 break; 2013 } 2014 2015 if (error) 2016 return (error); 2017 2018 bwillinode(1); 2019 nd->nl_flags |= NLC_CREATE | NLC_REFDVP; 2020 if ((error = nlookup(nd)) != 0) 2021 return (error); 2022 if (nd->nl_nch.ncp->nc_vp) 2023 return (EEXIST); 2024 if ((error = ncp_writechk(&nd->nl_nch)) != 0) 2025 return (error); 2026 2027 if (whiteout) { 2028 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp, 2029 nd->nl_cred, NAMEI_CREATE); 2030 } else { 2031 vp = NULL; 2032 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, 2033 &vp, nd->nl_cred, &vattr); 2034 if (error == 0) 2035 vput(vp); 2036 } 2037 return (error); 2038 } 2039 2040 /* 2041 * mknod_args(char *path, int mode, int dev) 2042 * 2043 * Create a special file. 2044 */ 2045 int 2046 sys_mknod(struct mknod_args *uap) 2047 { 2048 struct nlookupdata nd; 2049 int error; 2050 2051 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); 2052 if (error == 0) { 2053 error = kern_mknod(&nd, uap->mode, 2054 umajor(uap->dev), uminor(uap->dev)); 2055 } 2056 nlookup_done(&nd); 2057 return (error); 2058 } 2059 2060 /* 2061 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev) 2062 * 2063 * Create a special file. The path is relative to the directory associated 2064 * with fd. 2065 */ 2066 int 2067 sys_mknodat(struct mknodat_args *uap) 2068 { 2069 struct nlookupdata nd; 2070 struct file *fp; 2071 int error; 2072 2073 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0); 2074 if (error == 0) { 2075 error = kern_mknod(&nd, uap->mode, 2076 umajor(uap->dev), uminor(uap->dev)); 2077 } 2078 nlookup_done_at(&nd, fp); 2079 return (error); 2080 } 2081 2082 int 2083 kern_mkfifo(struct nlookupdata *nd, int mode) 2084 { 2085 struct thread *td = curthread; 2086 struct proc *p = td->td_proc; 2087 struct vattr vattr; 2088 struct vnode *vp; 2089 int error; 2090 2091 bwillinode(1); 2092 2093 nd->nl_flags |= NLC_CREATE | NLC_REFDVP; 2094 if ((error = nlookup(nd)) != 0) 2095 return (error); 2096 if (nd->nl_nch.ncp->nc_vp) 2097 return (EEXIST); 2098 if ((error = ncp_writechk(&nd->nl_nch)) != 0) 2099 return (error); 2100 2101 VATTR_NULL(&vattr); 2102 vattr.va_type = VFIFO; 2103 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask; 2104 vp = NULL; 2105 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr); 2106 if (error == 0) 2107 vput(vp); 2108 return (error); 2109 } 2110 2111 /* 2112 * mkfifo_args(char *path, int mode) 2113 * 2114 * Create a named pipe. 2115 */ 2116 int 2117 sys_mkfifo(struct mkfifo_args *uap) 2118 { 2119 struct nlookupdata nd; 2120 int error; 2121 2122 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); 2123 if (error == 0) 2124 error = kern_mkfifo(&nd, uap->mode); 2125 nlookup_done(&nd); 2126 return (error); 2127 } 2128 2129 /* 2130 * mkfifoat_args(int fd, char *path, mode_t mode) 2131 * 2132 * Create a named pipe. The path is relative to the directory associated 2133 * with fd. 2134 */ 2135 int 2136 sys_mkfifoat(struct mkfifoat_args *uap) 2137 { 2138 struct nlookupdata nd; 2139 struct file *fp; 2140 int error; 2141 2142 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0); 2143 if (error == 0) 2144 error = kern_mkfifo(&nd, uap->mode); 2145 nlookup_done_at(&nd, fp); 2146 return (error); 2147 } 2148 2149 static int hardlink_check_uid = 0; 2150 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW, 2151 &hardlink_check_uid, 0, 2152 "Unprivileged processes cannot create hard links to files owned by other " 2153 "users"); 2154 static int hardlink_check_gid = 0; 2155 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW, 2156 &hardlink_check_gid, 0, 2157 "Unprivileged processes cannot create hard links to files owned by other " 2158 "groups"); 2159 2160 static int 2161 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred) 2162 { 2163 struct vattr va; 2164 int error; 2165 2166 /* 2167 * Shortcut if disabled 2168 */ 2169 if (hardlink_check_uid == 0 && hardlink_check_gid == 0) 2170 return (0); 2171 2172 /* 2173 * Privileged user can always hardlink 2174 */ 2175 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0) 2176 return (0); 2177 2178 /* 2179 * Otherwise only if the originating file is owned by the 2180 * same user or group. Note that any group is allowed if 2181 * the file is owned by the caller. 2182 */ 2183 error = VOP_GETATTR(vp, &va); 2184 if (error != 0) 2185 return (error); 2186 2187 if (hardlink_check_uid) { 2188 if (cred->cr_uid != va.va_uid) 2189 return (EPERM); 2190 } 2191 2192 if (hardlink_check_gid) { 2193 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred)) 2194 return (EPERM); 2195 } 2196 2197 return (0); 2198 } 2199 2200 int 2201 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd) 2202 { 2203 struct thread *td = curthread; 2204 struct vnode *vp; 2205 int error; 2206 2207 /* 2208 * Lookup the source and obtained a locked vnode. 2209 * 2210 * You may only hardlink a file which you have write permission 2211 * on or which you own. 2212 * 2213 * XXX relookup on vget failure / race ? 2214 */ 2215 bwillinode(1); 2216 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK; 2217 if ((error = nlookup(nd)) != 0) 2218 return (error); 2219 vp = nd->nl_nch.ncp->nc_vp; 2220 KKASSERT(vp != NULL); 2221 if (vp->v_type == VDIR) 2222 return (EPERM); /* POSIX */ 2223 if ((error = ncp_writechk(&nd->nl_nch)) != 0) 2224 return (error); 2225 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) 2226 return (error); 2227 2228 /* 2229 * Unlock the source so we can lookup the target without deadlocking 2230 * (XXX vp is locked already, possible other deadlock?). The target 2231 * must not exist. 2232 */ 2233 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED); 2234 nd->nl_flags &= ~NLC_NCPISLOCKED; 2235 cache_unlock(&nd->nl_nch); 2236 vn_unlock(vp); 2237 2238 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP; 2239 if ((error = nlookup(linknd)) != 0) { 2240 vrele(vp); 2241 return (error); 2242 } 2243 if (linknd->nl_nch.ncp->nc_vp) { 2244 vrele(vp); 2245 return (EEXIST); 2246 } 2247 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) { 2248 vrele(vp); 2249 return (error); 2250 } 2251 2252 /* 2253 * Finally run the new API VOP. 2254 */ 2255 error = can_hardlink(vp, td, td->td_ucred); 2256 if (error == 0) { 2257 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp, 2258 vp, linknd->nl_cred); 2259 } 2260 vput(vp); 2261 return (error); 2262 } 2263 2264 /* 2265 * link_args(char *path, char *link) 2266 * 2267 * Make a hard file link. 2268 */ 2269 int 2270 sys_link(struct link_args *uap) 2271 { 2272 struct nlookupdata nd, linknd; 2273 int error; 2274 2275 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 2276 if (error == 0) { 2277 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0); 2278 if (error == 0) 2279 error = kern_link(&nd, &linknd); 2280 nlookup_done(&linknd); 2281 } 2282 nlookup_done(&nd); 2283 return (error); 2284 } 2285 2286 int 2287 kern_symlink(struct nlookupdata *nd, char *path, int mode) 2288 { 2289 struct vattr vattr; 2290 struct vnode *vp; 2291 struct vnode *dvp; 2292 int error; 2293 2294 bwillinode(1); 2295 nd->nl_flags |= NLC_CREATE | NLC_REFDVP; 2296 if ((error = nlookup(nd)) != 0) 2297 return (error); 2298 if (nd->nl_nch.ncp->nc_vp) 2299 return (EEXIST); 2300 if ((error = ncp_writechk(&nd->nl_nch)) != 0) 2301 return (error); 2302 dvp = nd->nl_dvp; 2303 VATTR_NULL(&vattr); 2304 vattr.va_mode = mode; 2305 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path); 2306 if (error == 0) 2307 vput(vp); 2308 return (error); 2309 } 2310 2311 /* 2312 * symlink(char *path, char *link) 2313 * 2314 * Make a symbolic link. 2315 */ 2316 int 2317 sys_symlink(struct symlink_args *uap) 2318 { 2319 struct thread *td = curthread; 2320 struct nlookupdata nd; 2321 char *path; 2322 int error; 2323 int mode; 2324 2325 path = objcache_get(namei_oc, M_WAITOK); 2326 error = copyinstr(uap->path, path, MAXPATHLEN, NULL); 2327 if (error == 0) { 2328 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0); 2329 if (error == 0) { 2330 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask; 2331 error = kern_symlink(&nd, path, mode); 2332 } 2333 nlookup_done(&nd); 2334 } 2335 objcache_put(namei_oc, path); 2336 return (error); 2337 } 2338 2339 /* 2340 * symlinkat_args(char *path1, int fd, char *path2) 2341 * 2342 * Make a symbolic link. The path2 argument is relative to the directory 2343 * associated with fd. 2344 */ 2345 int 2346 sys_symlinkat(struct symlinkat_args *uap) 2347 { 2348 struct thread *td = curthread; 2349 struct nlookupdata nd; 2350 struct file *fp; 2351 char *path1; 2352 int error; 2353 int mode; 2354 2355 path1 = objcache_get(namei_oc, M_WAITOK); 2356 error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL); 2357 if (error == 0) { 2358 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2, 2359 UIO_USERSPACE, 0); 2360 if (error == 0) { 2361 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask; 2362 error = kern_symlink(&nd, path1, mode); 2363 } 2364 nlookup_done_at(&nd, fp); 2365 } 2366 objcache_put(namei_oc, path1); 2367 return (error); 2368 } 2369 2370 /* 2371 * undelete_args(char *path) 2372 * 2373 * Delete a whiteout from the filesystem. 2374 */ 2375 int 2376 sys_undelete(struct undelete_args *uap) 2377 { 2378 struct nlookupdata nd; 2379 int error; 2380 2381 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); 2382 bwillinode(1); 2383 nd.nl_flags |= NLC_DELETE | NLC_REFDVP; 2384 if (error == 0) 2385 error = nlookup(&nd); 2386 if (error == 0) 2387 error = ncp_writechk(&nd.nl_nch); 2388 if (error == 0) { 2389 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred, 2390 NAMEI_DELETE); 2391 } 2392 nlookup_done(&nd); 2393 return (error); 2394 } 2395 2396 int 2397 kern_unlink(struct nlookupdata *nd) 2398 { 2399 int error; 2400 2401 bwillinode(1); 2402 nd->nl_flags |= NLC_DELETE | NLC_REFDVP; 2403 if ((error = nlookup(nd)) != 0) 2404 return (error); 2405 if ((error = ncp_writechk(&nd->nl_nch)) != 0) 2406 return (error); 2407 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred); 2408 return (error); 2409 } 2410 2411 /* 2412 * unlink_args(char *path) 2413 * 2414 * Delete a name from the filesystem. 2415 */ 2416 int 2417 sys_unlink(struct unlink_args *uap) 2418 { 2419 struct nlookupdata nd; 2420 int error; 2421 2422 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); 2423 if (error == 0) 2424 error = kern_unlink(&nd); 2425 nlookup_done(&nd); 2426 return (error); 2427 } 2428 2429 2430 /* 2431 * unlinkat_args(int fd, char *path, int flags) 2432 * 2433 * Delete the file or directory entry pointed to by fd/path. 2434 */ 2435 int 2436 sys_unlinkat(struct unlinkat_args *uap) 2437 { 2438 struct nlookupdata nd; 2439 struct file *fp; 2440 int error; 2441 2442 if (uap->flags & ~AT_REMOVEDIR) 2443 return (EINVAL); 2444 2445 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0); 2446 if (error == 0) { 2447 if (uap->flags & AT_REMOVEDIR) 2448 error = kern_rmdir(&nd); 2449 else 2450 error = kern_unlink(&nd); 2451 } 2452 nlookup_done_at(&nd, fp); 2453 return (error); 2454 } 2455 2456 int 2457 kern_lseek(int fd, off_t offset, int whence, off_t *res) 2458 { 2459 struct thread *td = curthread; 2460 struct proc *p = td->td_proc; 2461 struct file *fp; 2462 struct vnode *vp; 2463 struct vattr vattr; 2464 off_t new_offset; 2465 int error; 2466 2467 fp = holdfp(p->p_fd, fd, -1); 2468 if (fp == NULL) 2469 return (EBADF); 2470 if (fp->f_type != DTYPE_VNODE) { 2471 error = ESPIPE; 2472 goto done; 2473 } 2474 vp = (struct vnode *)fp->f_data; 2475 2476 switch (whence) { 2477 case L_INCR: 2478 spin_lock(&fp->f_spin); 2479 new_offset = fp->f_offset + offset; 2480 error = 0; 2481 break; 2482 case L_XTND: 2483 error = VOP_GETATTR(vp, &vattr); 2484 spin_lock(&fp->f_spin); 2485 new_offset = offset + vattr.va_size; 2486 break; 2487 case L_SET: 2488 new_offset = offset; 2489 error = 0; 2490 spin_lock(&fp->f_spin); 2491 break; 2492 default: 2493 new_offset = 0; 2494 error = EINVAL; 2495 spin_lock(&fp->f_spin); 2496 break; 2497 } 2498 2499 /* 2500 * Validate the seek position. Negative offsets are not allowed 2501 * for regular files or directories. 2502 * 2503 * Normally we would also not want to allow negative offsets for 2504 * character and block-special devices. However kvm addresses 2505 * on 64 bit architectures might appear to be negative and must 2506 * be allowed. 2507 */ 2508 if (error == 0) { 2509 if (new_offset < 0 && 2510 (vp->v_type == VREG || vp->v_type == VDIR)) { 2511 error = EINVAL; 2512 } else { 2513 fp->f_offset = new_offset; 2514 } 2515 } 2516 *res = fp->f_offset; 2517 spin_unlock(&fp->f_spin); 2518 done: 2519 fdrop(fp); 2520 return (error); 2521 } 2522 2523 /* 2524 * lseek_args(int fd, int pad, off_t offset, int whence) 2525 * 2526 * Reposition read/write file offset. 2527 */ 2528 int 2529 sys_lseek(struct lseek_args *uap) 2530 { 2531 int error; 2532 2533 error = kern_lseek(uap->fd, uap->offset, uap->whence, 2534 &uap->sysmsg_offset); 2535 2536 return (error); 2537 } 2538 2539 /* 2540 * Check if current process can access given file. amode is a bitmask of *_OK 2541 * access bits. flags is a bitmask of AT_* flags. 2542 */ 2543 int 2544 kern_access(struct nlookupdata *nd, int amode, int flags) 2545 { 2546 struct vnode *vp; 2547 int error, mode; 2548 2549 if (flags & ~AT_EACCESS) 2550 return (EINVAL); 2551 if ((error = nlookup(nd)) != 0) 2552 return (error); 2553 retry: 2554 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp); 2555 if (error) 2556 return (error); 2557 2558 /* Flags == 0 means only check for existence. */ 2559 if (amode) { 2560 mode = 0; 2561 if (amode & R_OK) 2562 mode |= VREAD; 2563 if (amode & W_OK) 2564 mode |= VWRITE; 2565 if (amode & X_OK) 2566 mode |= VEXEC; 2567 if ((mode & VWRITE) == 0 || 2568 (error = vn_writechk(vp, &nd->nl_nch)) == 0) 2569 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred); 2570 2571 /* 2572 * If the file handle is stale we have to re-resolve the 2573 * entry. This is a hack at the moment. 2574 */ 2575 if (error == ESTALE) { 2576 vput(vp); 2577 cache_setunresolved(&nd->nl_nch); 2578 error = cache_resolve(&nd->nl_nch, nd->nl_cred); 2579 if (error == 0) { 2580 vp = NULL; 2581 goto retry; 2582 } 2583 return(error); 2584 } 2585 } 2586 vput(vp); 2587 return (error); 2588 } 2589 2590 /* 2591 * access_args(char *path, int flags) 2592 * 2593 * Check access permissions. 2594 */ 2595 int 2596 sys_access(struct access_args *uap) 2597 { 2598 struct nlookupdata nd; 2599 int error; 2600 2601 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 2602 if (error == 0) 2603 error = kern_access(&nd, uap->flags, 0); 2604 nlookup_done(&nd); 2605 return (error); 2606 } 2607 2608 2609 /* 2610 * faccessat_args(int fd, char *path, int amode, int flags) 2611 * 2612 * Check access permissions. 2613 */ 2614 int 2615 sys_faccessat(struct faccessat_args *uap) 2616 { 2617 struct nlookupdata nd; 2618 struct file *fp; 2619 int error; 2620 2621 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 2622 NLC_FOLLOW); 2623 if (error == 0) 2624 error = kern_access(&nd, uap->amode, uap->flags); 2625 nlookup_done_at(&nd, fp); 2626 return (error); 2627 } 2628 2629 2630 int 2631 kern_stat(struct nlookupdata *nd, struct stat *st) 2632 { 2633 int error; 2634 struct vnode *vp; 2635 2636 if ((error = nlookup(nd)) != 0) 2637 return (error); 2638 again: 2639 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL) 2640 return (ENOENT); 2641 2642 if ((error = vget(vp, LK_SHARED)) != 0) 2643 return (error); 2644 error = vn_stat(vp, st, nd->nl_cred); 2645 2646 /* 2647 * If the file handle is stale we have to re-resolve the entry. This 2648 * is a hack at the moment. 2649 */ 2650 if (error == ESTALE) { 2651 vput(vp); 2652 cache_setunresolved(&nd->nl_nch); 2653 error = cache_resolve(&nd->nl_nch, nd->nl_cred); 2654 if (error == 0) 2655 goto again; 2656 } else { 2657 vput(vp); 2658 } 2659 return (error); 2660 } 2661 2662 /* 2663 * stat_args(char *path, struct stat *ub) 2664 * 2665 * Get file status; this version follows links. 2666 */ 2667 int 2668 sys_stat(struct stat_args *uap) 2669 { 2670 struct nlookupdata nd; 2671 struct stat st; 2672 int error; 2673 2674 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 2675 if (error == 0) { 2676 error = kern_stat(&nd, &st); 2677 if (error == 0) 2678 error = copyout(&st, uap->ub, sizeof(*uap->ub)); 2679 } 2680 nlookup_done(&nd); 2681 return (error); 2682 } 2683 2684 /* 2685 * lstat_args(char *path, struct stat *ub) 2686 * 2687 * Get file status; this version does not follow links. 2688 */ 2689 int 2690 sys_lstat(struct lstat_args *uap) 2691 { 2692 struct nlookupdata nd; 2693 struct stat st; 2694 int error; 2695 2696 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); 2697 if (error == 0) { 2698 error = kern_stat(&nd, &st); 2699 if (error == 0) 2700 error = copyout(&st, uap->ub, sizeof(*uap->ub)); 2701 } 2702 nlookup_done(&nd); 2703 return (error); 2704 } 2705 2706 /* 2707 * fstatat_args(int fd, char *path, struct stat *sb, int flags) 2708 * 2709 * Get status of file pointed to by fd/path. 2710 */ 2711 int 2712 sys_fstatat(struct fstatat_args *uap) 2713 { 2714 struct nlookupdata nd; 2715 struct stat st; 2716 int error; 2717 int flags; 2718 struct file *fp; 2719 2720 if (uap->flags & ~AT_SYMLINK_NOFOLLOW) 2721 return (EINVAL); 2722 2723 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW; 2724 2725 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, 2726 UIO_USERSPACE, flags); 2727 if (error == 0) { 2728 error = kern_stat(&nd, &st); 2729 if (error == 0) 2730 error = copyout(&st, uap->sb, sizeof(*uap->sb)); 2731 } 2732 nlookup_done_at(&nd, fp); 2733 return (error); 2734 } 2735 2736 /* 2737 * pathconf_Args(char *path, int name) 2738 * 2739 * Get configurable pathname variables. 2740 */ 2741 int 2742 sys_pathconf(struct pathconf_args *uap) 2743 { 2744 struct nlookupdata nd; 2745 struct vnode *vp; 2746 int error; 2747 2748 vp = NULL; 2749 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 2750 if (error == 0) 2751 error = nlookup(&nd); 2752 if (error == 0) 2753 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp); 2754 nlookup_done(&nd); 2755 if (error == 0) { 2756 error = VOP_PATHCONF(vp, uap->name, &uap->sysmsg_reg); 2757 vput(vp); 2758 } 2759 return (error); 2760 } 2761 2762 /* 2763 * XXX: daver 2764 * kern_readlink isn't properly split yet. There is a copyin burried 2765 * in VOP_READLINK(). 2766 */ 2767 int 2768 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res) 2769 { 2770 struct thread *td = curthread; 2771 struct vnode *vp; 2772 struct iovec aiov; 2773 struct uio auio; 2774 int error; 2775 2776 if ((error = nlookup(nd)) != 0) 2777 return (error); 2778 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp); 2779 if (error) 2780 return (error); 2781 if (vp->v_type != VLNK) { 2782 error = EINVAL; 2783 } else { 2784 aiov.iov_base = buf; 2785 aiov.iov_len = count; 2786 auio.uio_iov = &aiov; 2787 auio.uio_iovcnt = 1; 2788 auio.uio_offset = 0; 2789 auio.uio_rw = UIO_READ; 2790 auio.uio_segflg = UIO_USERSPACE; 2791 auio.uio_td = td; 2792 auio.uio_resid = count; 2793 error = VOP_READLINK(vp, &auio, td->td_ucred); 2794 } 2795 vput(vp); 2796 *res = count - auio.uio_resid; 2797 return (error); 2798 } 2799 2800 /* 2801 * readlink_args(char *path, char *buf, int count) 2802 * 2803 * Return target name of a symbolic link. 2804 */ 2805 int 2806 sys_readlink(struct readlink_args *uap) 2807 { 2808 struct nlookupdata nd; 2809 int error; 2810 2811 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); 2812 if (error == 0) { 2813 error = kern_readlink(&nd, uap->buf, uap->count, 2814 &uap->sysmsg_result); 2815 } 2816 nlookup_done(&nd); 2817 return (error); 2818 } 2819 2820 /* 2821 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize) 2822 * 2823 * Return target name of a symbolic link. The path is relative to the 2824 * directory associated with fd. 2825 */ 2826 int 2827 sys_readlinkat(struct readlinkat_args *uap) 2828 { 2829 struct nlookupdata nd; 2830 struct file *fp; 2831 int error; 2832 2833 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0); 2834 if (error == 0) { 2835 error = kern_readlink(&nd, uap->buf, uap->bufsize, 2836 &uap->sysmsg_result); 2837 } 2838 nlookup_done_at(&nd, fp); 2839 return (error); 2840 } 2841 2842 static int 2843 setfflags(struct vnode *vp, int flags) 2844 { 2845 struct thread *td = curthread; 2846 int error; 2847 struct vattr vattr; 2848 2849 /* 2850 * Prevent non-root users from setting flags on devices. When 2851 * a device is reused, users can retain ownership of the device 2852 * if they are allowed to set flags and programs assume that 2853 * chown can't fail when done as root. 2854 */ 2855 if ((vp->v_type == VCHR || vp->v_type == VBLK) && 2856 ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0)) 2857 return (error); 2858 2859 /* 2860 * note: vget is required for any operation that might mod the vnode 2861 * so VINACTIVE is properly cleared. 2862 */ 2863 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) { 2864 VATTR_NULL(&vattr); 2865 vattr.va_flags = flags; 2866 error = VOP_SETATTR(vp, &vattr, td->td_ucred); 2867 vput(vp); 2868 } 2869 return (error); 2870 } 2871 2872 /* 2873 * chflags(char *path, int flags) 2874 * 2875 * Change flags of a file given a path name. 2876 */ 2877 int 2878 sys_chflags(struct chflags_args *uap) 2879 { 2880 struct nlookupdata nd; 2881 struct vnode *vp; 2882 int error; 2883 2884 vp = NULL; 2885 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 2886 if (error == 0) 2887 error = nlookup(&nd); 2888 if (error == 0) 2889 error = ncp_writechk(&nd.nl_nch); 2890 if (error == 0) 2891 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp); 2892 nlookup_done(&nd); 2893 if (error == 0) { 2894 error = setfflags(vp, uap->flags); 2895 vrele(vp); 2896 } 2897 return (error); 2898 } 2899 2900 /* 2901 * lchflags(char *path, int flags) 2902 * 2903 * Change flags of a file given a path name, but don't follow symlinks. 2904 */ 2905 int 2906 sys_lchflags(struct lchflags_args *uap) 2907 { 2908 struct nlookupdata nd; 2909 struct vnode *vp; 2910 int error; 2911 2912 vp = NULL; 2913 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); 2914 if (error == 0) 2915 error = nlookup(&nd); 2916 if (error == 0) 2917 error = ncp_writechk(&nd.nl_nch); 2918 if (error == 0) 2919 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp); 2920 nlookup_done(&nd); 2921 if (error == 0) { 2922 error = setfflags(vp, uap->flags); 2923 vrele(vp); 2924 } 2925 return (error); 2926 } 2927 2928 /* 2929 * fchflags_args(int fd, int flags) 2930 * 2931 * Change flags of a file given a file descriptor. 2932 */ 2933 int 2934 sys_fchflags(struct fchflags_args *uap) 2935 { 2936 struct thread *td = curthread; 2937 struct proc *p = td->td_proc; 2938 struct file *fp; 2939 int error; 2940 2941 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0) 2942 return (error); 2943 if (fp->f_nchandle.ncp) 2944 error = ncp_writechk(&fp->f_nchandle); 2945 if (error == 0) 2946 error = setfflags((struct vnode *) fp->f_data, uap->flags); 2947 fdrop(fp); 2948 return (error); 2949 } 2950 2951 static int 2952 setfmode(struct vnode *vp, int mode) 2953 { 2954 struct thread *td = curthread; 2955 int error; 2956 struct vattr vattr; 2957 2958 /* 2959 * note: vget is required for any operation that might mod the vnode 2960 * so VINACTIVE is properly cleared. 2961 */ 2962 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) { 2963 VATTR_NULL(&vattr); 2964 vattr.va_mode = mode & ALLPERMS; 2965 error = VOP_SETATTR(vp, &vattr, td->td_ucred); 2966 vput(vp); 2967 } 2968 return error; 2969 } 2970 2971 int 2972 kern_chmod(struct nlookupdata *nd, int mode) 2973 { 2974 struct vnode *vp; 2975 int error; 2976 2977 if ((error = nlookup(nd)) != 0) 2978 return (error); 2979 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0) 2980 return (error); 2981 if ((error = ncp_writechk(&nd->nl_nch)) == 0) 2982 error = setfmode(vp, mode); 2983 vrele(vp); 2984 return (error); 2985 } 2986 2987 /* 2988 * chmod_args(char *path, int mode) 2989 * 2990 * Change mode of a file given path name. 2991 */ 2992 int 2993 sys_chmod(struct chmod_args *uap) 2994 { 2995 struct nlookupdata nd; 2996 int error; 2997 2998 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 2999 if (error == 0) 3000 error = kern_chmod(&nd, uap->mode); 3001 nlookup_done(&nd); 3002 return (error); 3003 } 3004 3005 /* 3006 * lchmod_args(char *path, int mode) 3007 * 3008 * Change mode of a file given path name (don't follow links.) 3009 */ 3010 int 3011 sys_lchmod(struct lchmod_args *uap) 3012 { 3013 struct nlookupdata nd; 3014 int error; 3015 3016 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); 3017 if (error == 0) 3018 error = kern_chmod(&nd, uap->mode); 3019 nlookup_done(&nd); 3020 return (error); 3021 } 3022 3023 /* 3024 * fchmod_args(int fd, int mode) 3025 * 3026 * Change mode of a file given a file descriptor. 3027 */ 3028 int 3029 sys_fchmod(struct fchmod_args *uap) 3030 { 3031 struct thread *td = curthread; 3032 struct proc *p = td->td_proc; 3033 struct file *fp; 3034 int error; 3035 3036 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0) 3037 return (error); 3038 if (fp->f_nchandle.ncp) 3039 error = ncp_writechk(&fp->f_nchandle); 3040 if (error == 0) 3041 error = setfmode((struct vnode *)fp->f_data, uap->mode); 3042 fdrop(fp); 3043 return (error); 3044 } 3045 3046 /* 3047 * fchmodat_args(char *path, int mode) 3048 * 3049 * Change mode of a file pointed to by fd/path. 3050 */ 3051 int 3052 sys_fchmodat(struct fchmodat_args *uap) 3053 { 3054 struct nlookupdata nd; 3055 struct file *fp; 3056 int error; 3057 int flags; 3058 3059 if (uap->flags & ~AT_SYMLINK_NOFOLLOW) 3060 return (EINVAL); 3061 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW; 3062 3063 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, 3064 UIO_USERSPACE, flags); 3065 if (error == 0) 3066 error = kern_chmod(&nd, uap->mode); 3067 nlookup_done_at(&nd, fp); 3068 return (error); 3069 } 3070 3071 static int 3072 setfown(struct mount *mp, struct vnode *vp, uid_t uid, gid_t gid) 3073 { 3074 struct thread *td = curthread; 3075 int error; 3076 struct vattr vattr; 3077 uid_t o_uid; 3078 gid_t o_gid; 3079 uint64_t size; 3080 3081 /* 3082 * note: vget is required for any operation that might mod the vnode 3083 * so VINACTIVE is properly cleared. 3084 */ 3085 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) { 3086 if ((error = VOP_GETATTR(vp, &vattr)) != 0) 3087 return error; 3088 o_uid = vattr.va_uid; 3089 o_gid = vattr.va_gid; 3090 size = vattr.va_size; 3091 3092 VATTR_NULL(&vattr); 3093 vattr.va_uid = uid; 3094 vattr.va_gid = gid; 3095 error = VOP_SETATTR(vp, &vattr, td->td_ucred); 3096 vput(vp); 3097 } 3098 3099 if (error == 0) { 3100 if (uid == -1) 3101 uid = o_uid; 3102 if (gid == -1) 3103 gid = o_gid; 3104 VFS_ACCOUNT(mp, o_uid, o_gid, -size); 3105 VFS_ACCOUNT(mp, uid, gid, size); 3106 } 3107 3108 return error; 3109 } 3110 3111 int 3112 kern_chown(struct nlookupdata *nd, int uid, int gid) 3113 { 3114 struct vnode *vp; 3115 int error; 3116 3117 if ((error = nlookup(nd)) != 0) 3118 return (error); 3119 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0) 3120 return (error); 3121 if ((error = ncp_writechk(&nd->nl_nch)) == 0) 3122 error = setfown(nd->nl_nch.mount, vp, uid, gid); 3123 vrele(vp); 3124 return (error); 3125 } 3126 3127 /* 3128 * chown(char *path, int uid, int gid) 3129 * 3130 * Set ownership given a path name. 3131 */ 3132 int 3133 sys_chown(struct chown_args *uap) 3134 { 3135 struct nlookupdata nd; 3136 int error; 3137 3138 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 3139 if (error == 0) 3140 error = kern_chown(&nd, uap->uid, uap->gid); 3141 nlookup_done(&nd); 3142 return (error); 3143 } 3144 3145 /* 3146 * lchown_args(char *path, int uid, int gid) 3147 * 3148 * Set ownership given a path name, do not cross symlinks. 3149 */ 3150 int 3151 sys_lchown(struct lchown_args *uap) 3152 { 3153 struct nlookupdata nd; 3154 int error; 3155 3156 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); 3157 if (error == 0) 3158 error = kern_chown(&nd, uap->uid, uap->gid); 3159 nlookup_done(&nd); 3160 return (error); 3161 } 3162 3163 /* 3164 * fchown_args(int fd, int uid, int gid) 3165 * 3166 * Set ownership given a file descriptor. 3167 */ 3168 int 3169 sys_fchown(struct fchown_args *uap) 3170 { 3171 struct thread *td = curthread; 3172 struct proc *p = td->td_proc; 3173 struct file *fp; 3174 int error; 3175 3176 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0) 3177 return (error); 3178 if (fp->f_nchandle.ncp) 3179 error = ncp_writechk(&fp->f_nchandle); 3180 if (error == 0) 3181 error = setfown(p->p_fd->fd_ncdir.mount, 3182 (struct vnode *)fp->f_data, uap->uid, uap->gid); 3183 fdrop(fp); 3184 return (error); 3185 } 3186 3187 /* 3188 * fchownat(int fd, char *path, int uid, int gid, int flags) 3189 * 3190 * Set ownership of file pointed to by fd/path. 3191 */ 3192 int 3193 sys_fchownat(struct fchownat_args *uap) 3194 { 3195 struct nlookupdata nd; 3196 struct file *fp; 3197 int error; 3198 int flags; 3199 3200 if (uap->flags & ~AT_SYMLINK_NOFOLLOW) 3201 return (EINVAL); 3202 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW; 3203 3204 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, 3205 UIO_USERSPACE, flags); 3206 if (error == 0) 3207 error = kern_chown(&nd, uap->uid, uap->gid); 3208 nlookup_done_at(&nd, fp); 3209 return (error); 3210 } 3211 3212 3213 static int 3214 getutimes(const struct timeval *tvp, struct timespec *tsp) 3215 { 3216 struct timeval tv[2]; 3217 3218 if (tvp == NULL) { 3219 microtime(&tv[0]); 3220 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]); 3221 tsp[1] = tsp[0]; 3222 } else { 3223 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]); 3224 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]); 3225 } 3226 return 0; 3227 } 3228 3229 static int 3230 setutimes(struct vnode *vp, struct vattr *vattr, 3231 const struct timespec *ts, int nullflag) 3232 { 3233 struct thread *td = curthread; 3234 int error; 3235 3236 VATTR_NULL(vattr); 3237 vattr->va_atime = ts[0]; 3238 vattr->va_mtime = ts[1]; 3239 if (nullflag) 3240 vattr->va_vaflags |= VA_UTIMES_NULL; 3241 error = VOP_SETATTR(vp, vattr, td->td_ucred); 3242 3243 return error; 3244 } 3245 3246 int 3247 kern_utimes(struct nlookupdata *nd, struct timeval *tptr) 3248 { 3249 struct timespec ts[2]; 3250 struct vnode *vp; 3251 struct vattr vattr; 3252 int error; 3253 3254 if ((error = getutimes(tptr, ts)) != 0) 3255 return (error); 3256 3257 /* 3258 * NOTE: utimes() succeeds for the owner even if the file 3259 * is not user-writable. 3260 */ 3261 nd->nl_flags |= NLC_OWN | NLC_WRITE; 3262 3263 if ((error = nlookup(nd)) != 0) 3264 return (error); 3265 if ((error = ncp_writechk(&nd->nl_nch)) != 0) 3266 return (error); 3267 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0) 3268 return (error); 3269 3270 /* 3271 * note: vget is required for any operation that might mod the vnode 3272 * so VINACTIVE is properly cleared. 3273 */ 3274 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) { 3275 error = vget(vp, LK_EXCLUSIVE); 3276 if (error == 0) { 3277 error = setutimes(vp, &vattr, ts, (tptr == NULL)); 3278 vput(vp); 3279 } 3280 } 3281 vrele(vp); 3282 return (error); 3283 } 3284 3285 /* 3286 * utimes_args(char *path, struct timeval *tptr) 3287 * 3288 * Set the access and modification times of a file. 3289 */ 3290 int 3291 sys_utimes(struct utimes_args *uap) 3292 { 3293 struct timeval tv[2]; 3294 struct nlookupdata nd; 3295 int error; 3296 3297 if (uap->tptr) { 3298 error = copyin(uap->tptr, tv, sizeof(tv)); 3299 if (error) 3300 return (error); 3301 } 3302 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 3303 if (error == 0) 3304 error = kern_utimes(&nd, uap->tptr ? tv : NULL); 3305 nlookup_done(&nd); 3306 return (error); 3307 } 3308 3309 /* 3310 * lutimes_args(char *path, struct timeval *tptr) 3311 * 3312 * Set the access and modification times of a file. 3313 */ 3314 int 3315 sys_lutimes(struct lutimes_args *uap) 3316 { 3317 struct timeval tv[2]; 3318 struct nlookupdata nd; 3319 int error; 3320 3321 if (uap->tptr) { 3322 error = copyin(uap->tptr, tv, sizeof(tv)); 3323 if (error) 3324 return (error); 3325 } 3326 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); 3327 if (error == 0) 3328 error = kern_utimes(&nd, uap->tptr ? tv : NULL); 3329 nlookup_done(&nd); 3330 return (error); 3331 } 3332 3333 /* 3334 * Set utimes on a file descriptor. The creds used to open the 3335 * file are used to determine whether the operation is allowed 3336 * or not. 3337 */ 3338 int 3339 kern_futimes(int fd, struct timeval *tptr) 3340 { 3341 struct thread *td = curthread; 3342 struct proc *p = td->td_proc; 3343 struct timespec ts[2]; 3344 struct file *fp; 3345 struct vnode *vp; 3346 struct vattr vattr; 3347 int error; 3348 3349 error = getutimes(tptr, ts); 3350 if (error) 3351 return (error); 3352 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0) 3353 return (error); 3354 if (fp->f_nchandle.ncp) 3355 error = ncp_writechk(&fp->f_nchandle); 3356 if (error == 0) { 3357 vp = fp->f_data; 3358 error = vget(vp, LK_EXCLUSIVE); 3359 if (error == 0) { 3360 error = VOP_GETATTR(vp, &vattr); 3361 if (error == 0) { 3362 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE, 3363 fp->f_cred); 3364 } 3365 if (error == 0) { 3366 error = setutimes(vp, &vattr, ts, 3367 (tptr == NULL)); 3368 } 3369 vput(vp); 3370 } 3371 } 3372 fdrop(fp); 3373 return (error); 3374 } 3375 3376 /* 3377 * futimes_args(int fd, struct timeval *tptr) 3378 * 3379 * Set the access and modification times of a file. 3380 */ 3381 int 3382 sys_futimes(struct futimes_args *uap) 3383 { 3384 struct timeval tv[2]; 3385 int error; 3386 3387 if (uap->tptr) { 3388 error = copyin(uap->tptr, tv, sizeof(tv)); 3389 if (error) 3390 return (error); 3391 } 3392 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL); 3393 3394 return (error); 3395 } 3396 3397 int 3398 kern_truncate(struct nlookupdata *nd, off_t length) 3399 { 3400 struct vnode *vp; 3401 struct vattr vattr; 3402 int error; 3403 uid_t uid; 3404 gid_t gid; 3405 uint64_t old_size; 3406 3407 if (length < 0) 3408 return(EINVAL); 3409 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE; 3410 if ((error = nlookup(nd)) != 0) 3411 return (error); 3412 if ((error = ncp_writechk(&nd->nl_nch)) != 0) 3413 return (error); 3414 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0) 3415 return (error); 3416 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) { 3417 vrele(vp); 3418 return (error); 3419 } 3420 if (vp->v_type == VDIR) { 3421 error = EISDIR; 3422 goto done; 3423 } 3424 error = VOP_GETATTR(vp, &vattr); 3425 KASSERT(error == 0, ("kern_truncate(): VOP_GETATTR didn't return 0")); 3426 uid = vattr.va_uid; 3427 gid = vattr.va_gid; 3428 old_size = vattr.va_size; 3429 3430 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) { 3431 VATTR_NULL(&vattr); 3432 vattr.va_size = length; 3433 error = VOP_SETATTR(vp, &vattr, nd->nl_cred); 3434 VFS_ACCOUNT(nd->nl_nch.mount, uid, gid, length - old_size); 3435 } 3436 done: 3437 vput(vp); 3438 return (error); 3439 } 3440 3441 /* 3442 * truncate(char *path, int pad, off_t length) 3443 * 3444 * Truncate a file given its path name. 3445 */ 3446 int 3447 sys_truncate(struct truncate_args *uap) 3448 { 3449 struct nlookupdata nd; 3450 int error; 3451 3452 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 3453 if (error == 0) 3454 error = kern_truncate(&nd, uap->length); 3455 nlookup_done(&nd); 3456 return error; 3457 } 3458 3459 int 3460 kern_ftruncate(int fd, off_t length) 3461 { 3462 struct thread *td = curthread; 3463 struct proc *p = td->td_proc; 3464 struct vattr vattr; 3465 struct vnode *vp; 3466 struct file *fp; 3467 int error; 3468 uid_t uid; 3469 gid_t gid; 3470 uint64_t old_size; 3471 3472 if (length < 0) 3473 return(EINVAL); 3474 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0) 3475 return (error); 3476 if (fp->f_nchandle.ncp) { 3477 error = ncp_writechk(&fp->f_nchandle); 3478 if (error) 3479 goto done; 3480 } 3481 if ((fp->f_flag & FWRITE) == 0) { 3482 error = EINVAL; 3483 goto done; 3484 } 3485 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */ 3486 error = EINVAL; 3487 goto done; 3488 } 3489 vp = (struct vnode *)fp->f_data; 3490 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3491 if (vp->v_type == VDIR) { 3492 error = EISDIR; 3493 goto done; 3494 } 3495 3496 error = VOP_GETATTR(vp, &vattr); 3497 KASSERT(error == 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0")); 3498 uid = vattr.va_uid; 3499 gid = vattr.va_gid; 3500 old_size = vattr.va_size; 3501 3502 if ((error = vn_writechk(vp, NULL)) == 0) { 3503 VATTR_NULL(&vattr); 3504 vattr.va_size = length; 3505 error = VOP_SETATTR(vp, &vattr, fp->f_cred); 3506 VFS_ACCOUNT(p->p_fd->fd_ncdir.mount, uid, gid, length - old_size); 3507 } 3508 vn_unlock(vp); 3509 done: 3510 fdrop(fp); 3511 return (error); 3512 } 3513 3514 /* 3515 * ftruncate_args(int fd, int pad, off_t length) 3516 * 3517 * Truncate a file given a file descriptor. 3518 */ 3519 int 3520 sys_ftruncate(struct ftruncate_args *uap) 3521 { 3522 int error; 3523 3524 error = kern_ftruncate(uap->fd, uap->length); 3525 3526 return (error); 3527 } 3528 3529 /* 3530 * fsync(int fd) 3531 * 3532 * Sync an open file. 3533 */ 3534 int 3535 sys_fsync(struct fsync_args *uap) 3536 { 3537 struct thread *td = curthread; 3538 struct proc *p = td->td_proc; 3539 struct vnode *vp; 3540 struct file *fp; 3541 vm_object_t obj; 3542 int error; 3543 3544 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0) 3545 return (error); 3546 vp = (struct vnode *)fp->f_data; 3547 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3548 if ((obj = vp->v_object) != NULL) 3549 vm_object_page_clean(obj, 0, 0, 0); 3550 error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL); 3551 if (error == 0 && vp->v_mount) 3552 error = buf_fsync(vp); 3553 vn_unlock(vp); 3554 fdrop(fp); 3555 3556 return (error); 3557 } 3558 3559 int 3560 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond) 3561 { 3562 struct nchandle fnchd; 3563 struct nchandle tnchd; 3564 struct namecache *ncp; 3565 struct vnode *fdvp; 3566 struct vnode *tdvp; 3567 struct mount *mp; 3568 int error; 3569 3570 bwillinode(1); 3571 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC; 3572 if ((error = nlookup(fromnd)) != 0) 3573 return (error); 3574 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL) 3575 return (ENOENT); 3576 fnchd.mount = fromnd->nl_nch.mount; 3577 cache_hold(&fnchd); 3578 3579 /* 3580 * unlock the source nch so we can lookup the target nch without 3581 * deadlocking. The target may or may not exist so we do not check 3582 * for a target vp like kern_mkdir() and other creation functions do. 3583 * 3584 * The source and target directories are ref'd and rechecked after 3585 * everything is relocked to determine if the source or target file 3586 * has been renamed. 3587 */ 3588 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED); 3589 fromnd->nl_flags &= ~NLC_NCPISLOCKED; 3590 cache_unlock(&fromnd->nl_nch); 3591 3592 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP; 3593 if ((error = nlookup(tond)) != 0) { 3594 cache_drop(&fnchd); 3595 return (error); 3596 } 3597 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) { 3598 cache_drop(&fnchd); 3599 return (ENOENT); 3600 } 3601 tnchd.mount = tond->nl_nch.mount; 3602 cache_hold(&tnchd); 3603 3604 /* 3605 * If the source and target are the same there is nothing to do 3606 */ 3607 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) { 3608 cache_drop(&fnchd); 3609 cache_drop(&tnchd); 3610 return (0); 3611 } 3612 3613 /* 3614 * Mount points cannot be renamed or overwritten 3615 */ 3616 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) & 3617 NCF_ISMOUNTPT 3618 ) { 3619 cache_drop(&fnchd); 3620 cache_drop(&tnchd); 3621 return (EINVAL); 3622 } 3623 3624 /* 3625 * Relock the source ncp. cache_relock() will deal with any 3626 * deadlocks against the already-locked tond and will also 3627 * make sure both are resolved. 3628 * 3629 * NOTE AFTER RELOCKING: The source or target ncp may have become 3630 * invalid while they were unlocked, nc_vp and nc_mount could 3631 * be NULL. 3632 */ 3633 cache_relock(&fromnd->nl_nch, fromnd->nl_cred, 3634 &tond->nl_nch, tond->nl_cred); 3635 fromnd->nl_flags |= NLC_NCPISLOCKED; 3636 3637 /* 3638 * make sure the parent directories linkages are the same 3639 */ 3640 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent || 3641 tnchd.ncp != tond->nl_nch.ncp->nc_parent) { 3642 cache_drop(&fnchd); 3643 cache_drop(&tnchd); 3644 return (ENOENT); 3645 } 3646 3647 /* 3648 * Both the source and target must be within the same filesystem and 3649 * in the same filesystem as their parent directories within the 3650 * namecache topology. 3651 * 3652 * NOTE: fromnd's nc_mount or nc_vp could be NULL. 3653 */ 3654 mp = fnchd.mount; 3655 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount || 3656 mp != tond->nl_nch.mount) { 3657 cache_drop(&fnchd); 3658 cache_drop(&tnchd); 3659 return (EXDEV); 3660 } 3661 3662 /* 3663 * Make sure the mount point is writable 3664 */ 3665 if ((error = ncp_writechk(&tond->nl_nch)) != 0) { 3666 cache_drop(&fnchd); 3667 cache_drop(&tnchd); 3668 return (error); 3669 } 3670 3671 /* 3672 * If the target exists and either the source or target is a directory, 3673 * then both must be directories. 3674 * 3675 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h 3676 * have become NULL. 3677 */ 3678 if (tond->nl_nch.ncp->nc_vp) { 3679 if (fromnd->nl_nch.ncp->nc_vp == NULL) { 3680 error = ENOENT; 3681 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) { 3682 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR) 3683 error = ENOTDIR; 3684 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) { 3685 error = EISDIR; 3686 } 3687 } 3688 3689 /* 3690 * You cannot rename a source into itself or a subdirectory of itself. 3691 * We check this by travsersing the target directory upwards looking 3692 * for a match against the source. 3693 * 3694 * XXX MPSAFE 3695 */ 3696 if (error == 0) { 3697 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) { 3698 if (fromnd->nl_nch.ncp == ncp) { 3699 error = EINVAL; 3700 break; 3701 } 3702 } 3703 } 3704 3705 cache_drop(&fnchd); 3706 cache_drop(&tnchd); 3707 3708 /* 3709 * Even though the namespaces are different, they may still represent 3710 * hardlinks to the same file. The filesystem might have a hard time 3711 * with this so we issue a NREMOVE of the source instead of a NRENAME 3712 * when we detect the situation. 3713 */ 3714 if (error == 0) { 3715 fdvp = fromnd->nl_dvp; 3716 tdvp = tond->nl_dvp; 3717 if (fdvp == NULL || tdvp == NULL) { 3718 error = EPERM; 3719 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) { 3720 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp, 3721 fromnd->nl_cred); 3722 } else { 3723 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch, 3724 fdvp, tdvp, tond->nl_cred); 3725 } 3726 } 3727 return (error); 3728 } 3729 3730 /* 3731 * rename_args(char *from, char *to) 3732 * 3733 * Rename files. Source and destination must either both be directories, 3734 * or both not be directories. If target is a directory, it must be empty. 3735 */ 3736 int 3737 sys_rename(struct rename_args *uap) 3738 { 3739 struct nlookupdata fromnd, tond; 3740 int error; 3741 3742 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0); 3743 if (error == 0) { 3744 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0); 3745 if (error == 0) 3746 error = kern_rename(&fromnd, &tond); 3747 nlookup_done(&tond); 3748 } 3749 nlookup_done(&fromnd); 3750 return (error); 3751 } 3752 3753 /* 3754 * renameat_args(int oldfd, char *old, int newfd, char *new) 3755 * 3756 * Rename files using paths relative to the directories associated with 3757 * oldfd and newfd. Source and destination must either both be directories, 3758 * or both not be directories. If target is a directory, it must be empty. 3759 */ 3760 int 3761 sys_renameat(struct renameat_args *uap) 3762 { 3763 struct nlookupdata oldnd, newnd; 3764 struct file *oldfp, *newfp; 3765 int error; 3766 3767 error = nlookup_init_at(&oldnd, &oldfp, uap->oldfd, uap->old, 3768 UIO_USERSPACE, 0); 3769 if (error == 0) { 3770 error = nlookup_init_at(&newnd, &newfp, uap->newfd, uap->new, 3771 UIO_USERSPACE, 0); 3772 if (error == 0) 3773 error = kern_rename(&oldnd, &newnd); 3774 nlookup_done_at(&newnd, newfp); 3775 } 3776 nlookup_done_at(&oldnd, oldfp); 3777 return (error); 3778 } 3779 3780 int 3781 kern_mkdir(struct nlookupdata *nd, int mode) 3782 { 3783 struct thread *td = curthread; 3784 struct proc *p = td->td_proc; 3785 struct vnode *vp; 3786 struct vattr vattr; 3787 int error; 3788 3789 bwillinode(1); 3790 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP; 3791 if ((error = nlookup(nd)) != 0) 3792 return (error); 3793 3794 if (nd->nl_nch.ncp->nc_vp) 3795 return (EEXIST); 3796 if ((error = ncp_writechk(&nd->nl_nch)) != 0) 3797 return (error); 3798 VATTR_NULL(&vattr); 3799 vattr.va_type = VDIR; 3800 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask; 3801 3802 vp = NULL; 3803 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr); 3804 if (error == 0) 3805 vput(vp); 3806 return (error); 3807 } 3808 3809 /* 3810 * mkdir_args(char *path, int mode) 3811 * 3812 * Make a directory file. 3813 */ 3814 int 3815 sys_mkdir(struct mkdir_args *uap) 3816 { 3817 struct nlookupdata nd; 3818 int error; 3819 3820 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); 3821 if (error == 0) 3822 error = kern_mkdir(&nd, uap->mode); 3823 nlookup_done(&nd); 3824 return (error); 3825 } 3826 3827 /* 3828 * mkdirat_args(int fd, char *path, mode_t mode) 3829 * 3830 * Make a directory file. The path is relative to the directory associated 3831 * with fd. 3832 */ 3833 int 3834 sys_mkdirat(struct mkdirat_args *uap) 3835 { 3836 struct nlookupdata nd; 3837 struct file *fp; 3838 int error; 3839 3840 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0); 3841 if (error == 0) 3842 error = kern_mkdir(&nd, uap->mode); 3843 nlookup_done_at(&nd, fp); 3844 return (error); 3845 } 3846 3847 int 3848 kern_rmdir(struct nlookupdata *nd) 3849 { 3850 int error; 3851 3852 bwillinode(1); 3853 nd->nl_flags |= NLC_DELETE | NLC_REFDVP; 3854 if ((error = nlookup(nd)) != 0) 3855 return (error); 3856 3857 /* 3858 * Do not allow directories representing mount points to be 3859 * deleted, even if empty. Check write perms on mount point 3860 * in case the vnode is aliased (aka nullfs). 3861 */ 3862 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT)) 3863 return (EINVAL); 3864 if ((error = ncp_writechk(&nd->nl_nch)) != 0) 3865 return (error); 3866 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred); 3867 return (error); 3868 } 3869 3870 /* 3871 * rmdir_args(char *path) 3872 * 3873 * Remove a directory file. 3874 */ 3875 int 3876 sys_rmdir(struct rmdir_args *uap) 3877 { 3878 struct nlookupdata nd; 3879 int error; 3880 3881 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); 3882 if (error == 0) 3883 error = kern_rmdir(&nd); 3884 nlookup_done(&nd); 3885 return (error); 3886 } 3887 3888 int 3889 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res, 3890 enum uio_seg direction) 3891 { 3892 struct thread *td = curthread; 3893 struct proc *p = td->td_proc; 3894 struct vnode *vp; 3895 struct file *fp; 3896 struct uio auio; 3897 struct iovec aiov; 3898 off_t loff; 3899 int error, eofflag; 3900 3901 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0) 3902 return (error); 3903 if ((fp->f_flag & FREAD) == 0) { 3904 error = EBADF; 3905 goto done; 3906 } 3907 vp = (struct vnode *)fp->f_data; 3908 unionread: 3909 if (vp->v_type != VDIR) { 3910 error = EINVAL; 3911 goto done; 3912 } 3913 aiov.iov_base = buf; 3914 aiov.iov_len = count; 3915 auio.uio_iov = &aiov; 3916 auio.uio_iovcnt = 1; 3917 auio.uio_rw = UIO_READ; 3918 auio.uio_segflg = direction; 3919 auio.uio_td = td; 3920 auio.uio_resid = count; 3921 loff = auio.uio_offset = fp->f_offset; 3922 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL); 3923 fp->f_offset = auio.uio_offset; 3924 if (error) 3925 goto done; 3926 if (count == auio.uio_resid) { 3927 if (union_dircheckp) { 3928 error = union_dircheckp(td, &vp, fp); 3929 if (error == -1) 3930 goto unionread; 3931 if (error) 3932 goto done; 3933 } 3934 #if 0 3935 if ((vp->v_flag & VROOT) && 3936 (vp->v_mount->mnt_flag & MNT_UNION)) { 3937 struct vnode *tvp = vp; 3938 vp = vp->v_mount->mnt_vnodecovered; 3939 vref(vp); 3940 fp->f_data = vp; 3941 fp->f_offset = 0; 3942 vrele(tvp); 3943 goto unionread; 3944 } 3945 #endif 3946 } 3947 3948 /* 3949 * WARNING! *basep may not be wide enough to accomodate the 3950 * seek offset. XXX should we hack this to return the upper 32 bits 3951 * for offsets greater then 4G? 3952 */ 3953 if (basep) { 3954 *basep = (long)loff; 3955 } 3956 *res = count - auio.uio_resid; 3957 done: 3958 fdrop(fp); 3959 return (error); 3960 } 3961 3962 /* 3963 * getdirentries_args(int fd, char *buf, u_int conut, long *basep) 3964 * 3965 * Read a block of directory entries in a file system independent format. 3966 */ 3967 int 3968 sys_getdirentries(struct getdirentries_args *uap) 3969 { 3970 long base; 3971 int error; 3972 3973 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base, 3974 &uap->sysmsg_result, UIO_USERSPACE); 3975 3976 if (error == 0 && uap->basep) 3977 error = copyout(&base, uap->basep, sizeof(*uap->basep)); 3978 return (error); 3979 } 3980 3981 /* 3982 * getdents_args(int fd, char *buf, size_t count) 3983 */ 3984 int 3985 sys_getdents(struct getdents_args *uap) 3986 { 3987 int error; 3988 3989 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL, 3990 &uap->sysmsg_result, UIO_USERSPACE); 3991 3992 return (error); 3993 } 3994 3995 /* 3996 * Set the mode mask for creation of filesystem nodes. 3997 * 3998 * umask(int newmask) 3999 */ 4000 int 4001 sys_umask(struct umask_args *uap) 4002 { 4003 struct thread *td = curthread; 4004 struct proc *p = td->td_proc; 4005 struct filedesc *fdp; 4006 4007 fdp = p->p_fd; 4008 uap->sysmsg_result = fdp->fd_cmask; 4009 fdp->fd_cmask = uap->newmask & ALLPERMS; 4010 return (0); 4011 } 4012 4013 /* 4014 * revoke(char *path) 4015 * 4016 * Void all references to file by ripping underlying filesystem 4017 * away from vnode. 4018 */ 4019 int 4020 sys_revoke(struct revoke_args *uap) 4021 { 4022 struct nlookupdata nd; 4023 struct vattr vattr; 4024 struct vnode *vp; 4025 struct ucred *cred; 4026 int error; 4027 4028 vp = NULL; 4029 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 4030 if (error == 0) 4031 error = nlookup(&nd); 4032 if (error == 0) 4033 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp); 4034 cred = crhold(nd.nl_cred); 4035 nlookup_done(&nd); 4036 if (error == 0) { 4037 if (error == 0) 4038 error = VOP_GETATTR(vp, &vattr); 4039 if (error == 0 && cred->cr_uid != vattr.va_uid) 4040 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0); 4041 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) { 4042 if (vcount(vp) > 0) 4043 error = vrevoke(vp, cred); 4044 } else if (error == 0) { 4045 error = vrevoke(vp, cred); 4046 } 4047 vrele(vp); 4048 } 4049 if (cred) 4050 crfree(cred); 4051 return (error); 4052 } 4053 4054 /* 4055 * getfh_args(char *fname, fhandle_t *fhp) 4056 * 4057 * Get (NFS) file handle 4058 * 4059 * NOTE: We use the fsid of the covering mount, even if it is a nullfs 4060 * mount. This allows nullfs mounts to be explicitly exported. 4061 * 4062 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe. 4063 * 4064 * nullfs mounts of subdirectories are not safe. That is, it will 4065 * work, but you do not really have protection against access to 4066 * the related parent directories. 4067 */ 4068 int 4069 sys_getfh(struct getfh_args *uap) 4070 { 4071 struct thread *td = curthread; 4072 struct nlookupdata nd; 4073 fhandle_t fh; 4074 struct vnode *vp; 4075 struct mount *mp; 4076 int error; 4077 4078 /* 4079 * Must be super user 4080 */ 4081 if ((error = priv_check(td, PRIV_ROOT)) != 0) 4082 return (error); 4083 4084 vp = NULL; 4085 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW); 4086 if (error == 0) 4087 error = nlookup(&nd); 4088 if (error == 0) 4089 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp); 4090 mp = nd.nl_nch.mount; 4091 nlookup_done(&nd); 4092 if (error == 0) { 4093 bzero(&fh, sizeof(fh)); 4094 fh.fh_fsid = mp->mnt_stat.f_fsid; 4095 error = VFS_VPTOFH(vp, &fh.fh_fid); 4096 vput(vp); 4097 if (error == 0) 4098 error = copyout(&fh, uap->fhp, sizeof(fh)); 4099 } 4100 return (error); 4101 } 4102 4103 /* 4104 * fhopen_args(const struct fhandle *u_fhp, int flags) 4105 * 4106 * syscall for the rpc.lockd to use to translate a NFS file handle into 4107 * an open descriptor. 4108 * 4109 * warning: do not remove the priv_check() call or this becomes one giant 4110 * security hole. 4111 */ 4112 int 4113 sys_fhopen(struct fhopen_args *uap) 4114 { 4115 struct thread *td = curthread; 4116 struct filedesc *fdp = td->td_proc->p_fd; 4117 struct mount *mp; 4118 struct vnode *vp; 4119 struct fhandle fhp; 4120 struct vattr vat; 4121 struct vattr *vap = &vat; 4122 struct flock lf; 4123 int fmode, mode, error, type; 4124 struct file *nfp; 4125 struct file *fp; 4126 int indx; 4127 4128 /* 4129 * Must be super user 4130 */ 4131 error = priv_check(td, PRIV_ROOT); 4132 if (error) 4133 return (error); 4134 4135 fmode = FFLAGS(uap->flags); 4136 4137 /* 4138 * Why not allow a non-read/write open for our lockd? 4139 */ 4140 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT)) 4141 return (EINVAL); 4142 error = copyin(uap->u_fhp, &fhp, sizeof(fhp)); 4143 if (error) 4144 return(error); 4145 4146 /* 4147 * Find the mount point 4148 */ 4149 mp = vfs_getvfs(&fhp.fh_fsid); 4150 if (mp == NULL) { 4151 error = ESTALE; 4152 goto done; 4153 } 4154 /* now give me my vnode, it gets returned to me locked */ 4155 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp); 4156 if (error) 4157 goto done; 4158 /* 4159 * from now on we have to make sure not 4160 * to forget about the vnode 4161 * any error that causes an abort must vput(vp) 4162 * just set error = err and 'goto bad;'. 4163 */ 4164 4165 /* 4166 * from vn_open 4167 */ 4168 if (vp->v_type == VLNK) { 4169 error = EMLINK; 4170 goto bad; 4171 } 4172 if (vp->v_type == VSOCK) { 4173 error = EOPNOTSUPP; 4174 goto bad; 4175 } 4176 mode = 0; 4177 if (fmode & (FWRITE | O_TRUNC)) { 4178 if (vp->v_type == VDIR) { 4179 error = EISDIR; 4180 goto bad; 4181 } 4182 error = vn_writechk(vp, NULL); 4183 if (error) 4184 goto bad; 4185 mode |= VWRITE; 4186 } 4187 if (fmode & FREAD) 4188 mode |= VREAD; 4189 if (mode) { 4190 error = VOP_ACCESS(vp, mode, td->td_ucred); 4191 if (error) 4192 goto bad; 4193 } 4194 if (fmode & O_TRUNC) { 4195 vn_unlock(vp); /* XXX */ 4196 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */ 4197 VATTR_NULL(vap); 4198 vap->va_size = 0; 4199 error = VOP_SETATTR(vp, vap, td->td_ucred); 4200 if (error) 4201 goto bad; 4202 } 4203 4204 /* 4205 * VOP_OPEN needs the file pointer so it can potentially override 4206 * it. 4207 * 4208 * WARNING! no f_nchandle will be associated when fhopen()ing a 4209 * directory. XXX 4210 */ 4211 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0) 4212 goto bad; 4213 fp = nfp; 4214 4215 error = VOP_OPEN(vp, fmode, td->td_ucred, fp); 4216 if (error) { 4217 /* 4218 * setting f_ops this way prevents VOP_CLOSE from being 4219 * called or fdrop() releasing the vp from v_data. Since 4220 * the VOP_OPEN failed we don't want to VOP_CLOSE. 4221 */ 4222 fp->f_ops = &badfileops; 4223 fp->f_data = NULL; 4224 goto bad_drop; 4225 } 4226 4227 /* 4228 * The fp is given its own reference, we still have our ref and lock. 4229 * 4230 * Assert that all regular files must be created with a VM object. 4231 */ 4232 if (vp->v_type == VREG && vp->v_object == NULL) { 4233 kprintf("fhopen: regular file did not have VM object: %p\n", vp); 4234 goto bad_drop; 4235 } 4236 4237 /* 4238 * The open was successful. Handle any locking requirements. 4239 */ 4240 if (fmode & (O_EXLOCK | O_SHLOCK)) { 4241 lf.l_whence = SEEK_SET; 4242 lf.l_start = 0; 4243 lf.l_len = 0; 4244 if (fmode & O_EXLOCK) 4245 lf.l_type = F_WRLCK; 4246 else 4247 lf.l_type = F_RDLCK; 4248 if (fmode & FNONBLOCK) 4249 type = 0; 4250 else 4251 type = F_WAIT; 4252 vn_unlock(vp); 4253 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) { 4254 /* 4255 * release our private reference. 4256 */ 4257 fsetfd(fdp, NULL, indx); 4258 fdrop(fp); 4259 vrele(vp); 4260 goto done; 4261 } 4262 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 4263 fp->f_flag |= FHASLOCK; 4264 } 4265 4266 /* 4267 * Clean up. Associate the file pointer with the previously 4268 * reserved descriptor and return it. 4269 */ 4270 vput(vp); 4271 fsetfd(fdp, fp, indx); 4272 fdrop(fp); 4273 uap->sysmsg_result = indx; 4274 return (0); 4275 4276 bad_drop: 4277 fsetfd(fdp, NULL, indx); 4278 fdrop(fp); 4279 bad: 4280 vput(vp); 4281 done: 4282 return (error); 4283 } 4284 4285 /* 4286 * fhstat_args(struct fhandle *u_fhp, struct stat *sb) 4287 */ 4288 int 4289 sys_fhstat(struct fhstat_args *uap) 4290 { 4291 struct thread *td = curthread; 4292 struct stat sb; 4293 fhandle_t fh; 4294 struct mount *mp; 4295 struct vnode *vp; 4296 int error; 4297 4298 /* 4299 * Must be super user 4300 */ 4301 error = priv_check(td, PRIV_ROOT); 4302 if (error) 4303 return (error); 4304 4305 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t)); 4306 if (error) 4307 return (error); 4308 4309 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) 4310 error = ESTALE; 4311 if (error == 0) { 4312 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) { 4313 error = vn_stat(vp, &sb, td->td_ucred); 4314 vput(vp); 4315 } 4316 } 4317 if (error == 0) 4318 error = copyout(&sb, uap->sb, sizeof(sb)); 4319 return (error); 4320 } 4321 4322 /* 4323 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf) 4324 */ 4325 int 4326 sys_fhstatfs(struct fhstatfs_args *uap) 4327 { 4328 struct thread *td = curthread; 4329 struct proc *p = td->td_proc; 4330 struct statfs *sp; 4331 struct mount *mp; 4332 struct vnode *vp; 4333 struct statfs sb; 4334 char *fullpath, *freepath; 4335 fhandle_t fh; 4336 int error; 4337 4338 /* 4339 * Must be super user 4340 */ 4341 if ((error = priv_check(td, PRIV_ROOT))) 4342 return (error); 4343 4344 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0) 4345 return (error); 4346 4347 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) { 4348 error = ESTALE; 4349 goto done; 4350 } 4351 if (p != NULL && !chroot_visible_mnt(mp, p)) { 4352 error = ESTALE; 4353 goto done; 4354 } 4355 4356 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0) 4357 goto done; 4358 mp = vp->v_mount; 4359 sp = &mp->mnt_stat; 4360 vput(vp); 4361 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0) 4362 goto done; 4363 4364 error = mount_path(p, mp, &fullpath, &freepath); 4365 if (error) 4366 goto done; 4367 bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 4368 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname)); 4369 kfree(freepath, M_TEMP); 4370 4371 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; 4372 if (priv_check(td, PRIV_ROOT)) { 4373 bcopy(sp, &sb, sizeof(sb)); 4374 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0; 4375 sp = &sb; 4376 } 4377 error = copyout(sp, uap->buf, sizeof(*sp)); 4378 done: 4379 return (error); 4380 } 4381 4382 /* 4383 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf) 4384 */ 4385 int 4386 sys_fhstatvfs(struct fhstatvfs_args *uap) 4387 { 4388 struct thread *td = curthread; 4389 struct proc *p = td->td_proc; 4390 struct statvfs *sp; 4391 struct mount *mp; 4392 struct vnode *vp; 4393 fhandle_t fh; 4394 int error; 4395 4396 /* 4397 * Must be super user 4398 */ 4399 if ((error = priv_check(td, PRIV_ROOT))) 4400 return (error); 4401 4402 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0) 4403 return (error); 4404 4405 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) { 4406 error = ESTALE; 4407 goto done; 4408 } 4409 if (p != NULL && !chroot_visible_mnt(mp, p)) { 4410 error = ESTALE; 4411 goto done; 4412 } 4413 4414 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp))) 4415 goto done; 4416 mp = vp->v_mount; 4417 sp = &mp->mnt_vstat; 4418 vput(vp); 4419 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0) 4420 goto done; 4421 4422 sp->f_flag = 0; 4423 if (mp->mnt_flag & MNT_RDONLY) 4424 sp->f_flag |= ST_RDONLY; 4425 if (mp->mnt_flag & MNT_NOSUID) 4426 sp->f_flag |= ST_NOSUID; 4427 error = copyout(sp, uap->buf, sizeof(*sp)); 4428 done: 4429 return (error); 4430 } 4431 4432 4433 /* 4434 * Syscall to push extended attribute configuration information into the 4435 * VFS. Accepts a path, which it converts to a mountpoint, as well as 4436 * a command (int cmd), and attribute name and misc data. For now, the 4437 * attribute name is left in userspace for consumption by the VFS_op. 4438 * It will probably be changed to be copied into sysspace by the 4439 * syscall in the future, once issues with various consumers of the 4440 * attribute code have raised their hands. 4441 * 4442 * Currently this is used only by UFS Extended Attributes. 4443 */ 4444 int 4445 sys_extattrctl(struct extattrctl_args *uap) 4446 { 4447 struct nlookupdata nd; 4448 struct vnode *vp; 4449 char attrname[EXTATTR_MAXNAMELEN]; 4450 int error; 4451 size_t size; 4452 4453 attrname[0] = 0; 4454 vp = NULL; 4455 error = 0; 4456 4457 if (error == 0 && uap->filename) { 4458 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE, 4459 NLC_FOLLOW); 4460 if (error == 0) 4461 error = nlookup(&nd); 4462 if (error == 0) 4463 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp); 4464 nlookup_done(&nd); 4465 } 4466 4467 if (error == 0 && uap->attrname) { 4468 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, 4469 &size); 4470 } 4471 4472 if (error == 0) { 4473 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 4474 if (error == 0) 4475 error = nlookup(&nd); 4476 if (error == 0) 4477 error = ncp_writechk(&nd.nl_nch); 4478 if (error == 0) { 4479 error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp, 4480 uap->attrnamespace, 4481 uap->attrname, nd.nl_cred); 4482 } 4483 nlookup_done(&nd); 4484 } 4485 4486 return (error); 4487 } 4488 4489 /* 4490 * Syscall to get a named extended attribute on a file or directory. 4491 */ 4492 int 4493 sys_extattr_set_file(struct extattr_set_file_args *uap) 4494 { 4495 char attrname[EXTATTR_MAXNAMELEN]; 4496 struct nlookupdata nd; 4497 struct vnode *vp; 4498 struct uio auio; 4499 struct iovec aiov; 4500 int error; 4501 4502 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN); 4503 if (error) 4504 return (error); 4505 4506 vp = NULL; 4507 4508 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 4509 if (error == 0) 4510 error = nlookup(&nd); 4511 if (error == 0) 4512 error = ncp_writechk(&nd.nl_nch); 4513 if (error == 0) 4514 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp); 4515 if (error) { 4516 nlookup_done(&nd); 4517 return (error); 4518 } 4519 4520 bzero(&auio, sizeof(auio)); 4521 aiov.iov_base = uap->data; 4522 aiov.iov_len = uap->nbytes; 4523 auio.uio_iov = &aiov; 4524 auio.uio_iovcnt = 1; 4525 auio.uio_offset = 0; 4526 auio.uio_resid = uap->nbytes; 4527 auio.uio_rw = UIO_WRITE; 4528 auio.uio_td = curthread; 4529 4530 error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname, 4531 &auio, nd.nl_cred); 4532 4533 vput(vp); 4534 nlookup_done(&nd); 4535 return (error); 4536 } 4537 4538 /* 4539 * Syscall to get a named extended attribute on a file or directory. 4540 */ 4541 int 4542 sys_extattr_get_file(struct extattr_get_file_args *uap) 4543 { 4544 char attrname[EXTATTR_MAXNAMELEN]; 4545 struct nlookupdata nd; 4546 struct uio auio; 4547 struct iovec aiov; 4548 struct vnode *vp; 4549 int error; 4550 4551 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN); 4552 if (error) 4553 return (error); 4554 4555 vp = NULL; 4556 4557 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 4558 if (error == 0) 4559 error = nlookup(&nd); 4560 if (error == 0) 4561 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp); 4562 if (error) { 4563 nlookup_done(&nd); 4564 return (error); 4565 } 4566 4567 bzero(&auio, sizeof(auio)); 4568 aiov.iov_base = uap->data; 4569 aiov.iov_len = uap->nbytes; 4570 auio.uio_iov = &aiov; 4571 auio.uio_iovcnt = 1; 4572 auio.uio_offset = 0; 4573 auio.uio_resid = uap->nbytes; 4574 auio.uio_rw = UIO_READ; 4575 auio.uio_td = curthread; 4576 4577 error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname, 4578 &auio, nd.nl_cred); 4579 uap->sysmsg_result = uap->nbytes - auio.uio_resid; 4580 4581 vput(vp); 4582 nlookup_done(&nd); 4583 return(error); 4584 } 4585 4586 /* 4587 * Syscall to delete a named extended attribute from a file or directory. 4588 * Accepts attribute name. The real work happens in VOP_SETEXTATTR(). 4589 */ 4590 int 4591 sys_extattr_delete_file(struct extattr_delete_file_args *uap) 4592 { 4593 char attrname[EXTATTR_MAXNAMELEN]; 4594 struct nlookupdata nd; 4595 struct vnode *vp; 4596 int error; 4597 4598 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN); 4599 if (error) 4600 return(error); 4601 4602 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 4603 if (error == 0) 4604 error = nlookup(&nd); 4605 if (error == 0) 4606 error = ncp_writechk(&nd.nl_nch); 4607 if (error == 0) { 4608 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp); 4609 if (error == 0) { 4610 error = VOP_SETEXTATTR(vp, uap->attrnamespace, 4611 attrname, NULL, nd.nl_cred); 4612 vput(vp); 4613 } 4614 } 4615 nlookup_done(&nd); 4616 return(error); 4617 } 4618 4619 /* 4620 * Determine if the mount is visible to the process. 4621 */ 4622 static int 4623 chroot_visible_mnt(struct mount *mp, struct proc *p) 4624 { 4625 struct nchandle nch; 4626 4627 /* 4628 * Traverse from the mount point upwards. If we hit the process 4629 * root then the mount point is visible to the process. 4630 */ 4631 nch = mp->mnt_ncmountpt; 4632 while (nch.ncp) { 4633 if (nch.mount == p->p_fd->fd_nrdir.mount && 4634 nch.ncp == p->p_fd->fd_nrdir.ncp) { 4635 return(1); 4636 } 4637 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) { 4638 nch = nch.mount->mnt_ncmounton; 4639 } else { 4640 nch.ncp = nch.ncp->nc_parent; 4641 } 4642 } 4643 4644 /* 4645 * If the mount point is not visible to the process, but the 4646 * process root is in a subdirectory of the mount, return 4647 * TRUE anyway. 4648 */ 4649 if (p->p_fd->fd_nrdir.mount == mp) 4650 return(1); 4651 4652 return(0); 4653 } 4654 4655