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