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