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