1 /* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94 39 * $FreeBSD: src/sys/kern/vfs_syscalls.c,v 1.151.2.18 2003/04/04 20:35:58 tegge Exp $ 40 * $DragonFly: src/sys/kern/vfs_syscalls.c,v 1.49 2004/12/17 00:18:07 dillon Exp $ 41 */ 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/buf.h> 46 #include <sys/conf.h> 47 #include <sys/sysent.h> 48 #include <sys/malloc.h> 49 #include <sys/mount.h> 50 #include <sys/sysproto.h> 51 #include <sys/filedesc.h> 52 #include <sys/kernel.h> 53 #include <sys/fcntl.h> 54 #include <sys/file.h> 55 #include <sys/linker.h> 56 #include <sys/stat.h> 57 #include <sys/unistd.h> 58 #include <sys/vnode.h> 59 #include <sys/proc.h> 60 #include <sys/namei.h> 61 #include <sys/nlookup.h> 62 #include <sys/dirent.h> 63 #include <sys/extattr.h> 64 #include <sys/kern_syscall.h> 65 66 #include <machine/limits.h> 67 #include <vfs/union/union.h> 68 #include <sys/sysctl.h> 69 #include <vm/vm.h> 70 #include <vm/vm_object.h> 71 #include <vm/vm_zone.h> 72 #include <vm/vm_page.h> 73 74 #include <sys/file2.h> 75 76 static int checkvp_chdir (struct vnode *vn, struct thread *td); 77 static void checkdirs (struct vnode *olddp, struct namecache *ncp); 78 static int chroot_refuse_vdir_fds (struct filedesc *fdp); 79 static int getutimes (const struct timeval *, struct timespec *); 80 static int setfown (struct vnode *, uid_t, gid_t); 81 static int setfmode (struct vnode *, int); 82 static int setfflags (struct vnode *, int); 83 static int setutimes (struct vnode *, const struct timespec *, int); 84 static int usermount = 0; /* if 1, non-root can mount fs. */ 85 86 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *); 87 88 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, ""); 89 90 /* 91 * Virtual File System System Calls 92 */ 93 94 /* 95 * Mount a file system. 96 */ 97 /* 98 * mount_args(char *type, char *path, int flags, caddr_t data) 99 */ 100 /* ARGSUSED */ 101 int 102 mount(struct mount_args *uap) 103 { 104 struct thread *td = curthread; 105 struct proc *p = td->td_proc; 106 struct vnode *vp; 107 struct namecache *ncp; 108 struct mount *mp; 109 struct vfsconf *vfsp; 110 int error, flag = 0, flag2 = 0; 111 struct vattr va; 112 struct nlookupdata nd; 113 char fstypename[MFSNAMELEN]; 114 lwkt_tokref ilock; 115 struct nlcomponent nlc; 116 117 KKASSERT(p); 118 if (p->p_ucred->cr_prison != NULL) 119 return (EPERM); 120 if (usermount == 0 && (error = suser(td))) 121 return (error); 122 /* 123 * Do not allow NFS export by non-root users. 124 */ 125 if (SCARG(uap, flags) & MNT_EXPORTED) { 126 error = suser(td); 127 if (error) 128 return (error); 129 } 130 /* 131 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users 132 */ 133 if (suser(td)) 134 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV; 135 136 /* 137 * Lookup the requested path and extract the ncp and vnode. 138 */ 139 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 140 if (error == 0) { 141 if ((error = nlookup(&nd)) == 0) { 142 if (nd.nl_ncp->nc_vp == NULL) 143 error = ENOENT; 144 } 145 } 146 if (error) { 147 nlookup_done(&nd); 148 return (error); 149 } 150 151 /* 152 * Extract the locked+refd ncp and cleanup the nd structure 153 */ 154 ncp = nd.nl_ncp; 155 nd.nl_ncp = NULL; 156 nlookup_done(&nd); 157 158 /* 159 * now we have the locked ref'd ncp and unreferenced vnode. 160 */ 161 vp = ncp->nc_vp; 162 if ((error = vget(vp, LK_EXCLUSIVE, td)) != 0) { 163 cache_put(ncp); 164 return (error); 165 } 166 cache_unlock(ncp); 167 168 /* 169 * Now we have an unlocked ref'd ncp and a locked ref'd vp 170 */ 171 if (SCARG(uap, flags) & MNT_UPDATE) { 172 if ((vp->v_flag & VROOT) == 0) { 173 cache_drop(ncp); 174 vput(vp); 175 return (EINVAL); 176 } 177 mp = vp->v_mount; 178 flag = mp->mnt_flag; 179 flag2 = mp->mnt_kern_flag; 180 /* 181 * We only allow the filesystem to be reloaded if it 182 * is currently mounted read-only. 183 */ 184 if ((SCARG(uap, flags) & MNT_RELOAD) && 185 ((mp->mnt_flag & MNT_RDONLY) == 0)) { 186 cache_drop(ncp); 187 vput(vp); 188 return (EOPNOTSUPP); /* Needs translation */ 189 } 190 /* 191 * Only root, or the user that did the original mount is 192 * permitted to update it. 193 */ 194 if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid && 195 (error = suser(td))) { 196 cache_drop(ncp); 197 vput(vp); 198 return (error); 199 } 200 if (vfs_busy(mp, LK_NOWAIT, NULL, td)) { 201 cache_drop(ncp); 202 vput(vp); 203 return (EBUSY); 204 } 205 if ((vp->v_flag & VMOUNT) != 0 || 206 vp->v_mountedhere != NULL) { 207 cache_drop(ncp); 208 vfs_unbusy(mp, td); 209 vput(vp); 210 return (EBUSY); 211 } 212 vp->v_flag |= VMOUNT; 213 mp->mnt_flag |= 214 SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE); 215 VOP_UNLOCK(vp, 0, td); 216 goto update; 217 } 218 /* 219 * If the user is not root, ensure that they own the directory 220 * onto which we are attempting to mount. 221 */ 222 if ((error = VOP_GETATTR(vp, &va, td)) || 223 (va.va_uid != p->p_ucred->cr_uid && 224 (error = suser(td)))) { 225 cache_drop(ncp); 226 vput(vp); 227 return (error); 228 } 229 if ((error = vinvalbuf(vp, V_SAVE, td, 0, 0)) != 0) { 230 cache_drop(ncp); 231 vput(vp); 232 return (error); 233 } 234 if (vp->v_type != VDIR) { 235 cache_drop(ncp); 236 vput(vp); 237 return (ENOTDIR); 238 } 239 if ((error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL)) != 0) { 240 cache_drop(ncp); 241 vput(vp); 242 return (error); 243 } 244 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) { 245 if (!strcmp(vfsp->vfc_name, fstypename)) 246 break; 247 } 248 if (vfsp == NULL) { 249 linker_file_t lf; 250 251 /* Only load modules for root (very important!) */ 252 if ((error = suser(td)) != 0) { 253 cache_drop(ncp); 254 vput(vp); 255 return error; 256 } 257 error = linker_load_file(fstypename, &lf); 258 if (error || lf == NULL) { 259 cache_drop(ncp); 260 vput(vp); 261 if (lf == NULL) 262 error = ENODEV; 263 return error; 264 } 265 lf->userrefs++; 266 /* lookup again, see if the VFS was loaded */ 267 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) { 268 if (!strcmp(vfsp->vfc_name, fstypename)) 269 break; 270 } 271 if (vfsp == NULL) { 272 lf->userrefs--; 273 linker_file_unload(lf); 274 cache_drop(ncp); 275 vput(vp); 276 return (ENODEV); 277 } 278 } 279 if ((vp->v_flag & VMOUNT) != 0 || 280 vp->v_mountedhere != NULL) { 281 cache_drop(ncp); 282 vput(vp); 283 return (EBUSY); 284 } 285 vp->v_flag |= VMOUNT; 286 287 /* 288 * Allocate and initialize the filesystem. 289 */ 290 mp = malloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK); 291 TAILQ_INIT(&mp->mnt_nvnodelist); 292 TAILQ_INIT(&mp->mnt_reservedvnlist); 293 mp->mnt_nvnodelistsize = 0; 294 lockinit(&mp->mnt_lock, 0, "vfslock", 0, LK_NOPAUSE); 295 vfs_busy(mp, LK_NOWAIT, NULL, td); 296 mp->mnt_op = vfsp->vfc_vfsops; 297 mp->mnt_vfc = vfsp; 298 vfsp->vfc_refcount++; 299 mp->mnt_stat.f_type = vfsp->vfc_typenum; 300 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK; 301 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN); 302 mp->mnt_vnodecovered = vp; 303 mp->mnt_stat.f_owner = p->p_ucred->cr_uid; 304 mp->mnt_iosize_max = DFLTPHYS; 305 VOP_UNLOCK(vp, 0, td); 306 update: 307 /* 308 * Set the mount level flags. 309 */ 310 if (SCARG(uap, flags) & MNT_RDONLY) 311 mp->mnt_flag |= MNT_RDONLY; 312 else if (mp->mnt_flag & MNT_RDONLY) 313 mp->mnt_kern_flag |= MNTK_WANTRDWR; 314 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV | 315 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME | 316 MNT_NOSYMFOLLOW | MNT_IGNORE | 317 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR); 318 mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC | 319 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE | 320 MNT_NOSYMFOLLOW | MNT_IGNORE | 321 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR); 322 /* 323 * Mount the filesystem. 324 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they 325 * get. 326 */ 327 error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), td); 328 if (mp->mnt_flag & MNT_UPDATE) { 329 if (mp->mnt_kern_flag & MNTK_WANTRDWR) 330 mp->mnt_flag &= ~MNT_RDONLY; 331 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE); 332 mp->mnt_kern_flag &=~ MNTK_WANTRDWR; 333 if (error) { 334 mp->mnt_flag = flag; 335 mp->mnt_kern_flag = flag2; 336 } 337 vfs_unbusy(mp, td); 338 vp->v_flag &= ~VMOUNT; 339 vrele(vp); 340 cache_drop(ncp); 341 return (error); 342 } 343 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 344 /* 345 * Put the new filesystem on the mount list after root. The mount 346 * point gets its own mnt_ncp which is a special ncp linking the 347 * vnode-under to the root of the new mount. The lookup code 348 * detects the mount point going forward and detects the special 349 * mnt_ncp via NCP_MOUNTPT going backwards. 350 * 351 * It is not necessary to invalidate or purge the vnode underneath 352 * because elements under the mount will be given their own glue 353 * namecache record. 354 */ 355 if (!error) { 356 nlc.nlc_nameptr = ""; 357 nlc.nlc_namelen = 0; 358 mp->mnt_ncp = cache_nlookup(ncp, &nlc); 359 mp->mnt_ncp->nc_flag |= NCF_MOUNTPT; 360 mp->mnt_ncp->nc_mount = mp; 361 cache_drop(ncp); 362 /* XXX get the root of the fs and cache_setvp(mnt_ncp...) */ 363 vp->v_flag &= ~VMOUNT; 364 vp->v_mountedhere = mp; 365 lwkt_gettoken(&ilock, &mountlist_token); 366 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list); 367 lwkt_reltoken(&ilock); 368 checkdirs(vp, mp->mnt_ncp); 369 cache_unlock(mp->mnt_ncp); /* leave ref intact */ 370 VOP_UNLOCK(vp, 0, td); 371 error = vfs_allocate_syncvnode(mp); 372 vfs_unbusy(mp, td); 373 if ((error = VFS_START(mp, 0, td)) != 0) 374 vrele(vp); 375 } else { 376 vfs_rm_vnodeops(&mp->mnt_vn_coherency_ops); 377 vfs_rm_vnodeops(&mp->mnt_vn_journal_ops); 378 vfs_rm_vnodeops(&mp->mnt_vn_norm_ops); 379 vfs_rm_vnodeops(&mp->mnt_vn_spec_ops); 380 vfs_rm_vnodeops(&mp->mnt_vn_fifo_ops); 381 vp->v_flag &= ~VMOUNT; 382 mp->mnt_vfc->vfc_refcount--; 383 vfs_unbusy(mp, td); 384 free(mp, M_MOUNT); 385 cache_drop(ncp); 386 vput(vp); 387 } 388 return (error); 389 } 390 391 /* 392 * Scan all active processes to see if any of them have a current 393 * or root directory onto which the new filesystem has just been 394 * mounted. If so, replace them with the new mount point. 395 * 396 * The passed ncp is ref'd and locked (from the mount code) and 397 * must be associated with the vnode representing the root of the 398 * mount point. 399 */ 400 static void 401 checkdirs(struct vnode *olddp, struct namecache *ncp) 402 { 403 struct filedesc *fdp; 404 struct vnode *newdp; 405 struct mount *mp; 406 struct proc *p; 407 408 if (olddp->v_usecount == 1) 409 return; 410 mp = olddp->v_mountedhere; 411 if (VFS_ROOT(mp, &newdp)) 412 panic("mount: lost mount"); 413 cache_setvp(ncp, newdp); 414 415 if (rootvnode == olddp) { 416 vref(newdp); 417 vfs_cache_setroot(newdp, cache_hold(ncp)); 418 } 419 420 FOREACH_PROC_IN_SYSTEM(p) { 421 fdp = p->p_fd; 422 if (fdp->fd_cdir == olddp) { 423 vrele(fdp->fd_cdir); 424 vref(newdp); 425 fdp->fd_cdir = newdp; 426 cache_drop(fdp->fd_ncdir); 427 fdp->fd_ncdir = cache_hold(ncp); 428 } 429 if (fdp->fd_rdir == olddp) { 430 vrele(fdp->fd_rdir); 431 vref(newdp); 432 fdp->fd_rdir = newdp; 433 cache_drop(fdp->fd_nrdir); 434 fdp->fd_nrdir = cache_hold(ncp); 435 } 436 } 437 vput(newdp); 438 } 439 440 /* 441 * Unmount a file system. 442 * 443 * Note: unmount takes a path to the vnode mounted on as argument, 444 * not special file (as before). 445 */ 446 /* 447 * umount_args(char *path, int flags) 448 */ 449 /* ARGSUSED */ 450 int 451 unmount(struct unmount_args *uap) 452 { 453 struct thread *td = curthread; 454 struct proc *p = td->td_proc; 455 struct vnode *vp; 456 struct mount *mp; 457 int error; 458 struct nlookupdata nd; 459 460 KKASSERT(p); 461 if (p->p_ucred->cr_prison != NULL) 462 return (EPERM); 463 if (usermount == 0 && (error = suser(td))) 464 return (error); 465 466 vp = NULL; 467 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW); 468 if (error == 0) 469 error = nlookup(&nd); 470 if (error == 0) 471 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp); 472 nlookup_done(&nd); 473 if (error) 474 return (error); 475 476 mp = vp->v_mount; 477 478 /* 479 * Only root, or the user that did the original mount is 480 * permitted to unmount this filesystem. 481 */ 482 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) && 483 (error = suser(td))) { 484 vput(vp); 485 return (error); 486 } 487 488 /* 489 * Don't allow unmounting the root file system. 490 */ 491 if (mp->mnt_flag & MNT_ROOTFS) { 492 vput(vp); 493 return (EINVAL); 494 } 495 496 /* 497 * Must be the root of the filesystem 498 */ 499 if ((vp->v_flag & VROOT) == 0) { 500 vput(vp); 501 return (EINVAL); 502 } 503 vput(vp); 504 return (dounmount(mp, SCARG(uap, flags), td)); 505 } 506 507 /* 508 * Do the actual file system unmount. 509 */ 510 int 511 dounmount(struct mount *mp, int flags, struct thread *td) 512 { 513 struct vnode *coveredvp; 514 int error; 515 int async_flag; 516 lwkt_tokref ilock; 517 518 lwkt_gettoken(&ilock, &mountlist_token); 519 if (mp->mnt_kern_flag & MNTK_UNMOUNT) { 520 lwkt_reltoken(&ilock); 521 return (EBUSY); 522 } 523 mp->mnt_kern_flag |= MNTK_UNMOUNT; 524 /* Allow filesystems to detect that a forced unmount is in progress. */ 525 if (flags & MNT_FORCE) 526 mp->mnt_kern_flag |= MNTK_UNMOUNTF; 527 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK | 528 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), &ilock, td); 529 if (error) { 530 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF); 531 if (mp->mnt_kern_flag & MNTK_MWAIT) 532 wakeup(mp); 533 return (error); 534 } 535 536 if (mp->mnt_flag & MNT_EXPUBLIC) 537 vfs_setpublicfs(NULL, NULL, NULL); 538 539 vfs_msync(mp, MNT_WAIT); 540 async_flag = mp->mnt_flag & MNT_ASYNC; 541 mp->mnt_flag &=~ MNT_ASYNC; 542 cache_purgevfs(mp); /* remove cache entries for this file sys */ 543 if (mp->mnt_syncer != NULL) 544 vrele(mp->mnt_syncer); 545 if (((mp->mnt_flag & MNT_RDONLY) || 546 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) || 547 (flags & MNT_FORCE)) 548 error = VFS_UNMOUNT(mp, flags, td); 549 lwkt_gettokref(&ilock); 550 if (error) { 551 if (mp->mnt_syncer == NULL) 552 vfs_allocate_syncvnode(mp); 553 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF); 554 mp->mnt_flag |= async_flag; 555 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE, 556 &ilock, td); 557 if (mp->mnt_kern_flag & MNTK_MWAIT) 558 wakeup(mp); 559 return (error); 560 } 561 TAILQ_REMOVE(&mountlist, mp, mnt_list); 562 563 /* 564 * Remove any installed vnode ops here so the individual VFSs don't 565 * have to. 566 */ 567 vfs_rm_vnodeops(&mp->mnt_vn_coherency_ops); 568 vfs_rm_vnodeops(&mp->mnt_vn_journal_ops); 569 vfs_rm_vnodeops(&mp->mnt_vn_norm_ops); 570 vfs_rm_vnodeops(&mp->mnt_vn_spec_ops); 571 vfs_rm_vnodeops(&mp->mnt_vn_fifo_ops); 572 573 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) { 574 coveredvp->v_mountedhere = NULL; 575 vrele(coveredvp); 576 cache_drop(mp->mnt_ncp); 577 mp->mnt_ncp = NULL; 578 } 579 mp->mnt_vfc->vfc_refcount--; 580 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist)) 581 panic("unmount: dangling vnode"); 582 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &ilock, td); 583 if (mp->mnt_kern_flag & MNTK_MWAIT) 584 wakeup(mp); 585 free(mp, M_MOUNT); 586 return (0); 587 } 588 589 /* 590 * Sync each mounted filesystem. 591 */ 592 593 #ifdef DEBUG 594 static int syncprt = 0; 595 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, ""); 596 #endif /* DEBUG */ 597 598 /* ARGSUSED */ 599 int 600 sync(struct sync_args *uap) 601 { 602 struct thread *td = curthread; 603 struct mount *mp, *nmp; 604 lwkt_tokref ilock; 605 int asyncflag; 606 607 lwkt_gettoken(&ilock, &mountlist_token); 608 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 609 if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) { 610 nmp = TAILQ_NEXT(mp, mnt_list); 611 continue; 612 } 613 if ((mp->mnt_flag & MNT_RDONLY) == 0) { 614 asyncflag = mp->mnt_flag & MNT_ASYNC; 615 mp->mnt_flag &= ~MNT_ASYNC; 616 vfs_msync(mp, MNT_NOWAIT); 617 VFS_SYNC(mp, MNT_NOWAIT, td); 618 mp->mnt_flag |= asyncflag; 619 } 620 lwkt_gettokref(&ilock); 621 nmp = TAILQ_NEXT(mp, mnt_list); 622 vfs_unbusy(mp, td); 623 } 624 lwkt_reltoken(&ilock); 625 /* 626 * print out buffer pool stat information on each sync() call. 627 */ 628 #ifdef DEBUG 629 if (syncprt) 630 vfs_bufstats(); 631 #endif /* DEBUG */ 632 return (0); 633 } 634 635 /* XXX PRISON: could be per prison flag */ 636 static int prison_quotas; 637 #if 0 638 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, ""); 639 #endif 640 641 /* 642 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg) 643 * 644 * Change filesystem quotas. 645 */ 646 /* ARGSUSED */ 647 int 648 quotactl(struct quotactl_args *uap) 649 { 650 struct nlookupdata nd; 651 struct thread *td; 652 struct proc *p; 653 struct mount *mp; 654 int error; 655 656 td = curthread; 657 p = td->td_proc; 658 if (p->p_ucred->cr_prison && !prison_quotas) 659 return (EPERM); 660 661 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW); 662 if (error == 0) 663 error = nlookup(&nd); 664 if (error == 0) { 665 mp = nd.nl_ncp->nc_mount; 666 error = VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid), 667 SCARG(uap, arg), nd.nl_td); 668 } 669 nlookup_done(&nd); 670 return (error); 671 } 672 673 int 674 kern_statfs(struct nlookupdata *nd, struct statfs *buf) 675 { 676 struct thread *td = curthread; 677 struct mount *mp; 678 struct statfs *sp; 679 int error; 680 681 if ((error = nlookup(nd)) != 0) 682 return (error); 683 mp = nd->nl_ncp->nc_mount; 684 sp = &mp->mnt_stat; 685 if ((error = VFS_STATFS(mp, sp, td)) != 0) 686 return (error); 687 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; 688 bcopy(sp, buf, sizeof(*buf)); 689 /* Only root should have access to the fsid's. */ 690 if (suser(td)) 691 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0; 692 return (0); 693 } 694 695 /* 696 * statfs_args(char *path, struct statfs *buf) 697 * 698 * Get filesystem statistics. 699 */ 700 int 701 statfs(struct statfs_args *uap) 702 { 703 struct nlookupdata nd; 704 struct statfs buf; 705 int error; 706 707 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 708 if (error == 0) 709 error = kern_statfs(&nd, &buf); 710 nlookup_done(&nd); 711 if (error == 0) 712 error = copyout(&buf, uap->buf, sizeof(*uap->buf)); 713 return (error); 714 } 715 716 int 717 kern_fstatfs(int fd, struct statfs *buf) 718 { 719 struct thread *td = curthread; 720 struct proc *p = td->td_proc; 721 struct file *fp; 722 struct mount *mp; 723 struct statfs *sp; 724 int error; 725 726 KKASSERT(p); 727 error = getvnode(p->p_fd, fd, &fp); 728 if (error) 729 return (error); 730 mp = ((struct vnode *)fp->f_data)->v_mount; 731 if (mp == NULL) 732 return (EBADF); 733 sp = &mp->mnt_stat; 734 error = VFS_STATFS(mp, sp, td); 735 if (error) 736 return (error); 737 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; 738 bcopy(sp, buf, sizeof(*buf)); 739 /* Only root should have access to the fsid's. */ 740 if (suser(td)) 741 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0; 742 return (0); 743 } 744 745 /* 746 * fstatfs_args(int fd, struct statfs *buf) 747 * 748 * Get filesystem statistics. 749 */ 750 int 751 fstatfs(struct fstatfs_args *uap) 752 { 753 struct statfs buf; 754 int error; 755 756 error = kern_fstatfs(uap->fd, &buf); 757 758 if (error == 0) 759 error = copyout(&buf, uap->buf, sizeof(*uap->buf)); 760 return (error); 761 } 762 763 /* 764 * getfsstat_args(struct statfs *buf, long bufsize, int flags) 765 * 766 * Get statistics on all filesystems. 767 */ 768 /* ARGSUSED */ 769 int 770 getfsstat(struct getfsstat_args *uap) 771 { 772 struct thread *td = curthread; 773 struct mount *mp, *nmp; 774 struct statfs *sp; 775 caddr_t sfsp; 776 lwkt_tokref ilock; 777 long count, maxcount, error; 778 779 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs); 780 sfsp = (caddr_t)SCARG(uap, buf); 781 count = 0; 782 lwkt_gettoken(&ilock, &mountlist_token); 783 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 784 if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) { 785 nmp = TAILQ_NEXT(mp, mnt_list); 786 continue; 787 } 788 if (sfsp && count < maxcount) { 789 sp = &mp->mnt_stat; 790 /* 791 * If MNT_NOWAIT or MNT_LAZY is specified, do not 792 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY 793 * overrides MNT_WAIT. 794 */ 795 if (((SCARG(uap, flags) & (MNT_LAZY|MNT_NOWAIT)) == 0 || 796 (SCARG(uap, flags) & MNT_WAIT)) && 797 (error = VFS_STATFS(mp, sp, td))) { 798 lwkt_gettokref(&ilock); 799 nmp = TAILQ_NEXT(mp, mnt_list); 800 vfs_unbusy(mp, td); 801 continue; 802 } 803 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; 804 error = copyout(sp, sfsp, sizeof(*sp)); 805 if (error) { 806 vfs_unbusy(mp, td); 807 return (error); 808 } 809 sfsp += sizeof(*sp); 810 } 811 count++; 812 lwkt_gettokref(&ilock); 813 nmp = TAILQ_NEXT(mp, mnt_list); 814 vfs_unbusy(mp, td); 815 } 816 lwkt_reltoken(&ilock); 817 if (sfsp && count > maxcount) 818 uap->sysmsg_result = maxcount; 819 else 820 uap->sysmsg_result = count; 821 return (0); 822 } 823 824 /* 825 * fchdir_args(int fd) 826 * 827 * Change current working directory to a given file descriptor. 828 */ 829 /* ARGSUSED */ 830 int 831 fchdir(struct fchdir_args *uap) 832 { 833 struct thread *td = curthread; 834 struct proc *p = td->td_proc; 835 struct filedesc *fdp = p->p_fd; 836 struct vnode *vp, *ovp; 837 struct mount *mp; 838 struct file *fp; 839 struct namecache *ncp, *oncp; 840 struct namecache *nct; 841 int error; 842 843 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0) 844 return (error); 845 vp = (struct vnode *)fp->f_data; 846 vref(vp); 847 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 848 if (vp->v_type != VDIR || fp->f_ncp == NULL) 849 error = ENOTDIR; 850 else 851 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td); 852 if (error) { 853 vput(vp); 854 return (error); 855 } 856 ncp = cache_hold(fp->f_ncp); 857 while (!error && (mp = vp->v_mountedhere) != NULL) { 858 error = nlookup_mp(mp, &nct); 859 if (error == 0) { 860 cache_unlock(nct); /* leave ref intact */ 861 vput(vp); 862 vp = nct->nc_vp; 863 error = vget(vp, LK_SHARED, td); 864 KKASSERT(error == 0); 865 cache_drop(ncp); 866 ncp = nct; 867 } 868 } 869 if (error == 0) { 870 ovp = fdp->fd_cdir; 871 oncp = fdp->fd_ncdir; 872 VOP_UNLOCK(vp, 0, td); /* leave ref intact */ 873 fdp->fd_cdir = vp; 874 fdp->fd_ncdir = ncp; 875 cache_drop(oncp); 876 vrele(ovp); 877 } else { 878 cache_drop(ncp); 879 vput(vp); 880 } 881 return (error); 882 } 883 884 int 885 kern_chdir(struct nlookupdata *nd) 886 { 887 struct thread *td = curthread; 888 struct proc *p = td->td_proc; 889 struct filedesc *fdp = p->p_fd; 890 struct vnode *vp, *ovp; 891 struct namecache *oncp; 892 int error; 893 894 if ((error = nlookup(nd)) != 0) 895 return (error); 896 if ((vp = nd->nl_ncp->nc_vp) == NULL) 897 return (ENOENT); 898 if ((error = vget(vp, LK_SHARED, td)) != 0) 899 return (error); 900 901 error = checkvp_chdir(vp, td); 902 VOP_UNLOCK(vp, 0, td); 903 if (error == 0) { 904 ovp = fdp->fd_cdir; 905 oncp = fdp->fd_ncdir; 906 cache_unlock(nd->nl_ncp); /* leave reference intact */ 907 fdp->fd_ncdir = nd->nl_ncp; 908 fdp->fd_cdir = vp; 909 cache_drop(oncp); 910 vrele(ovp); 911 nd->nl_ncp = NULL; 912 } else { 913 vrele(vp); 914 } 915 return (error); 916 } 917 918 /* 919 * chdir_args(char *path) 920 * 921 * Change current working directory (``.''). 922 */ 923 int 924 chdir(struct chdir_args *uap) 925 { 926 struct nlookupdata nd; 927 int error; 928 929 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 930 if (error == 0) 931 error = kern_chdir(&nd); 932 nlookup_done(&nd); 933 return (error); 934 } 935 936 /* 937 * Helper function for raised chroot(2) security function: Refuse if 938 * any filedescriptors are open directories. 939 */ 940 static int 941 chroot_refuse_vdir_fds(fdp) 942 struct filedesc *fdp; 943 { 944 struct vnode *vp; 945 struct file *fp; 946 int error; 947 int fd; 948 949 for (fd = 0; fd < fdp->fd_nfiles ; fd++) { 950 error = getvnode(fdp, fd, &fp); 951 if (error) 952 continue; 953 vp = (struct vnode *)fp->f_data; 954 if (vp->v_type != VDIR) 955 continue; 956 return(EPERM); 957 } 958 return (0); 959 } 960 961 /* 962 * This sysctl determines if we will allow a process to chroot(2) if it 963 * has a directory open: 964 * 0: disallowed for all processes. 965 * 1: allowed for processes that were not already chroot(2)'ed. 966 * 2: allowed for all processes. 967 */ 968 969 static int chroot_allow_open_directories = 1; 970 971 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW, 972 &chroot_allow_open_directories, 0, ""); 973 974 /* 975 * chroot to the specified namecache entry. We obtain the vp from the 976 * namecache data. The passed ncp must be locked and referenced and will 977 * remain locked and referenced on return. 978 */ 979 static int 980 kern_chroot(struct nlookupdata *nd) 981 { 982 struct thread *td = curthread; 983 struct proc *p = td->td_proc; 984 struct filedesc *fdp = p->p_fd; 985 struct namecache *ncp; 986 struct vnode *vp; 987 int error; 988 989 /* 990 * Only root can chroot 991 */ 992 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0) 993 return (error); 994 995 if ((error = nlookup(nd)) != 0) 996 return (error); 997 ncp = nd->nl_ncp; 998 999 /* 1000 * Disallow open directory descriptors (fchdir() breakouts). 1001 */ 1002 if (chroot_allow_open_directories == 0 || 1003 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) { 1004 if ((error = chroot_refuse_vdir_fds(fdp)) != 0) 1005 return (error); 1006 } 1007 if ((vp = ncp->nc_vp) == NULL) 1008 return (ENOENT); 1009 1010 if ((error = vget(vp, LK_SHARED, td)) != 0) 1011 return (error); 1012 1013 /* 1014 * Check the validity of vp as a directory to change to and 1015 * associate it with rdir/jdir. 1016 */ 1017 error = checkvp_chdir(vp, td); 1018 VOP_UNLOCK(vp, 0, td); /* leave reference intact */ 1019 if (error == 0) { 1020 vrele(fdp->fd_rdir); 1021 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */ 1022 cache_drop(fdp->fd_nrdir); 1023 fdp->fd_nrdir = cache_hold(ncp); 1024 if (fdp->fd_jdir == NULL) { 1025 fdp->fd_jdir = vp; 1026 vref(fdp->fd_jdir); 1027 fdp->fd_njdir = cache_hold(ncp); 1028 } 1029 } else { 1030 vrele(vp); 1031 } 1032 return (error); 1033 } 1034 1035 /* 1036 * chroot_args(char *path) 1037 * 1038 * Change notion of root (``/'') directory. 1039 */ 1040 /* ARGSUSED */ 1041 int 1042 chroot(struct chroot_args *uap) 1043 { 1044 struct thread *td = curthread; 1045 struct nlookupdata nd; 1046 int error; 1047 1048 KKASSERT(td->td_proc); 1049 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 1050 if (error == 0) 1051 error = kern_chroot(&nd); 1052 nlookup_done(&nd); 1053 return (error); 1054 } 1055 1056 /* 1057 * Common routine for chroot and chdir. Given a locked, referenced vnode, 1058 * determine whether it is legal to chdir to the vnode. The vnode's state 1059 * is not changed by this call. 1060 */ 1061 int 1062 checkvp_chdir(struct vnode *vp, struct thread *td) 1063 { 1064 int error; 1065 1066 if (vp->v_type != VDIR) 1067 error = ENOTDIR; 1068 else 1069 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred, td); 1070 return (error); 1071 } 1072 1073 int 1074 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res) 1075 { 1076 struct thread *td = curthread; 1077 struct proc *p = td->td_proc; 1078 struct filedesc *fdp = p->p_fd; 1079 int cmode, flags; 1080 struct file *nfp; 1081 struct file *fp; 1082 struct vnode *vp; 1083 int type, indx, error; 1084 struct flock lf; 1085 1086 if ((oflags & O_ACCMODE) == O_ACCMODE) 1087 return (EINVAL); 1088 flags = FFLAGS(oflags); 1089 error = falloc(p, &nfp, NULL); 1090 if (error) 1091 return (error); 1092 fp = nfp; 1093 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT; 1094 1095 /* 1096 * XXX p_dupfd is a real mess. It allows a device to return a 1097 * file descriptor to be duplicated rather then doing the open 1098 * itself. 1099 */ 1100 p->p_dupfd = -1; 1101 1102 /* 1103 * Call vn_open() to do the lookup and assign the vnode to the 1104 * file pointer. vn_open() does not change the ref count on fp 1105 * and the vnode, on success, will be inherited by the file pointer 1106 * and unlocked. 1107 */ 1108 nd->nl_flags |= NLC_LOCKVP; 1109 error = vn_open(nd, fp, flags, cmode); 1110 nlookup_done(nd); 1111 if (error) { 1112 /* 1113 * handle special fdopen() case. bleh. dupfdopen() is 1114 * responsible for dropping the old contents of ofiles[indx] 1115 * if it succeeds. 1116 * 1117 * Note that if fsetfd() succeeds it will add a ref to fp 1118 * which represents the fd_ofiles[] assignment. We must still 1119 * drop our reference. 1120 */ 1121 if ((error == ENODEV || error == ENXIO) && p->p_dupfd >= 0) { 1122 if (fsetfd(p, fp, &indx) == 0) { 1123 error = dupfdopen(fdp, indx, p->p_dupfd, flags, error); 1124 if (error == 0) { 1125 *res = indx; 1126 fdrop(fp, td); /* our ref */ 1127 return (0); 1128 } 1129 if (fdp->fd_ofiles[indx] == fp) { 1130 fdp->fd_ofiles[indx] = NULL; 1131 fdrop(fp, td); /* fd_ofiles[] ref */ 1132 } 1133 } 1134 } 1135 fdrop(fp, td); /* our ref */ 1136 if (error == ERESTART) 1137 error = EINTR; 1138 return (error); 1139 } 1140 1141 /* 1142 * ref the vnode for ourselves so it can't be ripped out from under 1143 * is. XXX need an ND flag to request that the vnode be returned 1144 * anyway. 1145 */ 1146 vp = (struct vnode *)fp->f_data; 1147 vref(vp); 1148 if ((error = fsetfd(p, fp, &indx)) != 0) { 1149 fdrop(fp, td); 1150 vrele(vp); 1151 return (error); 1152 } 1153 1154 /* 1155 * If no error occurs the vp will have been assigned to the file 1156 * pointer. 1157 */ 1158 p->p_dupfd = 0; 1159 1160 /* 1161 * There should be 2 references on the file, one from the descriptor 1162 * table, and one for us. 1163 * 1164 * Handle the case where someone closed the file (via its file 1165 * descriptor) while we were blocked. The end result should look 1166 * like opening the file succeeded but it was immediately closed. 1167 */ 1168 if (fp->f_count == 1) { 1169 KASSERT(fdp->fd_ofiles[indx] != fp, 1170 ("Open file descriptor lost all refs")); 1171 vrele(vp); 1172 fo_close(fp, td); 1173 fdrop(fp, td); 1174 *res = indx; 1175 return 0; 1176 } 1177 1178 if (flags & (O_EXLOCK | O_SHLOCK)) { 1179 lf.l_whence = SEEK_SET; 1180 lf.l_start = 0; 1181 lf.l_len = 0; 1182 if (flags & O_EXLOCK) 1183 lf.l_type = F_WRLCK; 1184 else 1185 lf.l_type = F_RDLCK; 1186 type = F_FLOCK; 1187 if ((flags & FNONBLOCK) == 0) 1188 type |= F_WAIT; 1189 1190 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) { 1191 /* 1192 * lock request failed. Normally close the descriptor 1193 * but handle the case where someone might have dup()d 1194 * it when we weren't looking. One reference is 1195 * owned by the descriptor array, the other by us. 1196 */ 1197 vrele(vp); 1198 if (fdp->fd_ofiles[indx] == fp) { 1199 fdp->fd_ofiles[indx] = NULL; 1200 fdrop(fp, td); 1201 } 1202 fdrop(fp, td); 1203 return (error); 1204 } 1205 fp->f_flag |= FHASLOCK; 1206 } 1207 /* assert that vn_open created a backing object if one is needed */ 1208 KASSERT(!vn_canvmio(vp) || VOP_GETVOBJECT(vp, NULL) == 0, 1209 ("open: vmio vnode has no backing object after vn_open")); 1210 1211 vrele(vp); 1212 1213 /* 1214 * release our private reference, leaving the one associated with the 1215 * descriptor table intact. 1216 */ 1217 fdrop(fp, td); 1218 *res = indx; 1219 return (0); 1220 } 1221 1222 /* 1223 * open_args(char *path, int flags, int mode) 1224 * 1225 * Check permissions, allocate an open file structure, 1226 * and call the device open routine if any. 1227 */ 1228 int 1229 open(struct open_args *uap) 1230 { 1231 struct nlookupdata nd; 1232 int error; 1233 1234 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 1235 if (error == 0) { 1236 error = kern_open(&nd, uap->flags, 1237 uap->mode, &uap->sysmsg_result); 1238 } 1239 nlookup_done(&nd); 1240 return (error); 1241 } 1242 1243 int 1244 kern_mknod(struct nlookupdata *nd, int mode, int dev) 1245 { 1246 struct namecache *ncp; 1247 struct thread *td = curthread; 1248 struct proc *p = td->td_proc; 1249 struct vnode *vp; 1250 struct vattr vattr; 1251 int error; 1252 int whiteout = 0; 1253 1254 KKASSERT(p); 1255 1256 switch (mode & S_IFMT) { 1257 case S_IFCHR: 1258 case S_IFBLK: 1259 error = suser(td); 1260 break; 1261 default: 1262 error = suser_cred(p->p_ucred, PRISON_ROOT); 1263 break; 1264 } 1265 if (error) 1266 return (error); 1267 1268 bwillwrite(); 1269 nd->nl_flags |= NLC_CREATE; 1270 if ((error = nlookup(nd)) != 0) 1271 return (error); 1272 ncp = nd->nl_ncp; 1273 if (ncp->nc_vp) 1274 return (EEXIST); 1275 1276 VATTR_NULL(&vattr); 1277 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask; 1278 vattr.va_rdev = dev; 1279 whiteout = 0; 1280 1281 switch (mode & S_IFMT) { 1282 case S_IFMT: /* used by badsect to flag bad sectors */ 1283 vattr.va_type = VBAD; 1284 break; 1285 case S_IFCHR: 1286 vattr.va_type = VCHR; 1287 break; 1288 case S_IFBLK: 1289 vattr.va_type = VBLK; 1290 break; 1291 case S_IFWHT: 1292 whiteout = 1; 1293 break; 1294 default: 1295 error = EINVAL; 1296 break; 1297 } 1298 if (error == 0) { 1299 if (whiteout) { 1300 error = VOP_NWHITEOUT(ncp, nd->nl_cred, NAMEI_CREATE); 1301 } else { 1302 vp = NULL; 1303 error = VOP_NMKNOD(ncp, &vp, nd->nl_cred, &vattr); 1304 if (error == 0) 1305 vput(vp); 1306 } 1307 } 1308 return (error); 1309 } 1310 1311 /* 1312 * mknod_args(char *path, int mode, int dev) 1313 * 1314 * Create a special file. 1315 */ 1316 int 1317 mknod(struct mknod_args *uap) 1318 { 1319 struct nlookupdata nd; 1320 int error; 1321 1322 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); 1323 if (error == 0) 1324 error = kern_mknod(&nd, uap->mode, uap->dev); 1325 nlookup_done(&nd); 1326 return (error); 1327 } 1328 1329 int 1330 kern_mkfifo(struct nlookupdata *nd, int mode) 1331 { 1332 struct namecache *ncp; 1333 struct thread *td = curthread; 1334 struct proc *p = td->td_proc; 1335 struct vattr vattr; 1336 struct vnode *vp; 1337 int error; 1338 1339 bwillwrite(); 1340 1341 nd->nl_flags |= NLC_CREATE; 1342 if ((error = nlookup(nd)) != 0) 1343 return (error); 1344 ncp = nd->nl_ncp; 1345 if (ncp->nc_vp) 1346 return (EEXIST); 1347 1348 VATTR_NULL(&vattr); 1349 vattr.va_type = VFIFO; 1350 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask; 1351 vp = NULL; 1352 error = VOP_NMKNOD(ncp, &vp, nd->nl_cred, &vattr); 1353 if (error == 0) 1354 vput(vp); 1355 return (error); 1356 } 1357 1358 /* 1359 * mkfifo_args(char *path, int mode) 1360 * 1361 * Create a named pipe. 1362 */ 1363 int 1364 mkfifo(struct mkfifo_args *uap) 1365 { 1366 struct nlookupdata nd; 1367 int error; 1368 1369 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); 1370 if (error == 0) 1371 error = kern_mkfifo(&nd, uap->mode); 1372 nlookup_done(&nd); 1373 return (error); 1374 } 1375 1376 int 1377 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd) 1378 { 1379 struct thread *td = curthread; 1380 struct vnode *vp; 1381 int error; 1382 1383 /* 1384 * Lookup the source and obtained a locked vnode. 1385 * 1386 * XXX relookup on vget failure / race ? 1387 */ 1388 bwillwrite(); 1389 if ((error = nlookup(nd)) != 0) 1390 return (error); 1391 vp = nd->nl_ncp->nc_vp; 1392 KKASSERT(vp != NULL); 1393 if (vp->v_type == VDIR) 1394 return (EPERM); /* POSIX */ 1395 if ((error = vget(vp, LK_EXCLUSIVE, td)) != 0) 1396 return (error); 1397 1398 /* 1399 * Unlock the source so we can lookup the target without deadlocking 1400 * (XXX vp is locked already, possible other deadlock?). The target 1401 * must not exist. 1402 */ 1403 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED); 1404 nd->nl_flags &= ~NLC_NCPISLOCKED; 1405 cache_unlock(nd->nl_ncp); 1406 1407 linknd->nl_flags |= NLC_CREATE; 1408 if ((error = nlookup(linknd)) != 0) { 1409 vput(vp); 1410 return (error); 1411 } 1412 if (linknd->nl_ncp->nc_vp) { 1413 vput(vp); 1414 return (EEXIST); 1415 } 1416 1417 /* 1418 * Finally run the new API VOP. 1419 */ 1420 error = VOP_NLINK(linknd->nl_ncp, vp, linknd->nl_cred); 1421 vput(vp); 1422 return (error); 1423 } 1424 1425 /* 1426 * link_args(char *path, char *link) 1427 * 1428 * Make a hard file link. 1429 */ 1430 int 1431 link(struct link_args *uap) 1432 { 1433 struct nlookupdata nd, linknd; 1434 int error; 1435 1436 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 1437 if (error == 0) { 1438 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0); 1439 if (error == 0) 1440 error = kern_link(&nd, &linknd); 1441 nlookup_done(&linknd); 1442 } 1443 nlookup_done(&nd); 1444 return (error); 1445 } 1446 1447 int 1448 kern_symlink(struct nlookupdata *nd, char *path, int mode) 1449 { 1450 struct namecache *ncp; 1451 struct vattr vattr; 1452 struct vnode *vp; 1453 int error; 1454 1455 bwillwrite(); 1456 nd->nl_flags |= NLC_CREATE; 1457 if ((error = nlookup(nd)) != 0) 1458 return (error); 1459 ncp = nd->nl_ncp; 1460 if (ncp->nc_vp) 1461 return (EEXIST); 1462 1463 VATTR_NULL(&vattr); 1464 vattr.va_mode = mode; 1465 error = VOP_NSYMLINK(ncp, &vp, nd->nl_cred, &vattr, path); 1466 if (error == 0) 1467 vput(vp); 1468 return (error); 1469 } 1470 1471 /* 1472 * symlink(char *path, char *link) 1473 * 1474 * Make a symbolic link. 1475 */ 1476 int 1477 symlink(struct symlink_args *uap) 1478 { 1479 struct thread *td = curthread; 1480 struct nlookupdata nd; 1481 char *path; 1482 int error; 1483 int mode; 1484 1485 path = zalloc(namei_zone); 1486 error = copyinstr(uap->path, path, MAXPATHLEN, NULL); 1487 if (error == 0) { 1488 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0); 1489 if (error == 0) { 1490 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask; 1491 error = kern_symlink(&nd, path, mode); 1492 } 1493 nlookup_done(&nd); 1494 } 1495 zfree(namei_zone, path); 1496 return (error); 1497 } 1498 1499 /* 1500 * undelete_args(char *path) 1501 * 1502 * Delete a whiteout from the filesystem. 1503 */ 1504 /* ARGSUSED */ 1505 int 1506 undelete(struct undelete_args *uap) 1507 { 1508 struct nlookupdata nd; 1509 int error; 1510 1511 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, 0); 1512 bwillwrite(); 1513 nd.nl_flags |= NLC_DELETE; 1514 if (error == 0) 1515 error = nlookup(&nd); 1516 if (error == 0) 1517 error = VOP_NWHITEOUT(nd.nl_ncp, nd.nl_cred, NAMEI_DELETE); 1518 nlookup_done(&nd); 1519 return (error); 1520 } 1521 1522 int 1523 kern_unlink(struct nlookupdata *nd) 1524 { 1525 struct namecache *ncp; 1526 int error; 1527 1528 bwillwrite(); 1529 nd->nl_flags |= NLC_DELETE; 1530 if ((error = nlookup(nd)) != 0) 1531 return (error); 1532 ncp = nd->nl_ncp; 1533 error = VOP_NREMOVE(ncp, nd->nl_cred); 1534 return (error); 1535 } 1536 1537 /* 1538 * unlink_args(char *path) 1539 * 1540 * Delete a name from the filesystem. 1541 */ 1542 int 1543 unlink(struct unlink_args *uap) 1544 { 1545 struct nlookupdata nd; 1546 int error; 1547 1548 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); 1549 if (error == 0) 1550 error = kern_unlink(&nd); 1551 nlookup_done(&nd); 1552 return (error); 1553 } 1554 1555 int 1556 kern_lseek(int fd, off_t offset, int whence, off_t *res) 1557 { 1558 struct thread *td = curthread; 1559 struct proc *p = td->td_proc; 1560 struct filedesc *fdp = p->p_fd; 1561 struct file *fp; 1562 struct vattr vattr; 1563 int error; 1564 1565 if (fd >= fdp->fd_nfiles || 1566 (fp = fdp->fd_ofiles[fd]) == NULL) 1567 return (EBADF); 1568 if (fp->f_type != DTYPE_VNODE) 1569 return (ESPIPE); 1570 switch (whence) { 1571 case L_INCR: 1572 fp->f_offset += offset; 1573 break; 1574 case L_XTND: 1575 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, td); 1576 if (error) 1577 return (error); 1578 fp->f_offset = offset + vattr.va_size; 1579 break; 1580 case L_SET: 1581 fp->f_offset = offset; 1582 break; 1583 default: 1584 return (EINVAL); 1585 } 1586 *res = fp->f_offset; 1587 return (0); 1588 } 1589 1590 /* 1591 * lseek_args(int fd, int pad, off_t offset, int whence) 1592 * 1593 * Reposition read/write file offset. 1594 */ 1595 int 1596 lseek(struct lseek_args *uap) 1597 { 1598 int error; 1599 1600 error = kern_lseek(uap->fd, uap->offset, uap->whence, 1601 &uap->sysmsg_offset); 1602 1603 return (error); 1604 } 1605 1606 int 1607 kern_access(struct nlookupdata *nd, int aflags) 1608 { 1609 struct thread *td = curthread; 1610 struct vnode *vp; 1611 int error, flags; 1612 1613 if ((error = nlookup(nd)) != 0) 1614 return (error); 1615 retry: 1616 error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp); 1617 if (error) 1618 return (error); 1619 1620 /* Flags == 0 means only check for existence. */ 1621 if (aflags) { 1622 flags = 0; 1623 if (aflags & R_OK) 1624 flags |= VREAD; 1625 if (aflags & W_OK) 1626 flags |= VWRITE; 1627 if (aflags & X_OK) 1628 flags |= VEXEC; 1629 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0) 1630 error = VOP_ACCESS(vp, flags, nd->nl_cred, td); 1631 1632 /* 1633 * If the file handle is stale we have to re-resolve the 1634 * entry. This is a hack at the moment. 1635 */ 1636 if (error == ESTALE) { 1637 cache_setunresolved(nd->nl_ncp); 1638 error = cache_resolve(nd->nl_ncp, nd->nl_cred); 1639 if (error == 0) { 1640 vput(vp); 1641 vp = NULL; 1642 goto retry; 1643 } 1644 } 1645 } 1646 vput(vp); 1647 return (error); 1648 } 1649 1650 /* 1651 * access_args(char *path, int flags) 1652 * 1653 * Check access permissions. 1654 */ 1655 int 1656 access(struct access_args *uap) 1657 { 1658 struct nlookupdata nd; 1659 int error; 1660 1661 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 1662 if (error == 0) 1663 error = kern_access(&nd, uap->flags); 1664 nlookup_done(&nd); 1665 return (error); 1666 } 1667 1668 int 1669 kern_stat(struct nlookupdata *nd, struct stat *st) 1670 { 1671 int error; 1672 struct vnode *vp; 1673 thread_t td; 1674 1675 if ((error = nlookup(nd)) != 0) 1676 return (error); 1677 again: 1678 if ((vp = nd->nl_ncp->nc_vp) == NULL) 1679 return (ENOENT); 1680 1681 td = curthread; 1682 if ((error = vget(vp, LK_SHARED, td)) != 0) 1683 return (error); 1684 error = vn_stat(vp, st, td); 1685 1686 /* 1687 * If the file handle is stale we have to re-resolve the entry. This 1688 * is a hack at the moment. 1689 */ 1690 if (error == ESTALE) { 1691 cache_setunresolved(nd->nl_ncp); 1692 error = cache_resolve(nd->nl_ncp, nd->nl_cred); 1693 if (error == 0) { 1694 vput(vp); 1695 goto again; 1696 } 1697 } 1698 vput(vp); 1699 return (error); 1700 } 1701 1702 /* 1703 * stat_args(char *path, struct stat *ub) 1704 * 1705 * Get file status; this version follows links. 1706 */ 1707 int 1708 stat(struct stat_args *uap) 1709 { 1710 struct nlookupdata nd; 1711 struct stat st; 1712 int error; 1713 1714 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 1715 if (error == 0) { 1716 error = kern_stat(&nd, &st); 1717 if (error == 0) 1718 error = copyout(&st, uap->ub, sizeof(*uap->ub)); 1719 } 1720 nlookup_done(&nd); 1721 return (error); 1722 } 1723 1724 /* 1725 * lstat_args(char *path, struct stat *ub) 1726 * 1727 * Get file status; this version does not follow links. 1728 */ 1729 int 1730 lstat(struct lstat_args *uap) 1731 { 1732 struct nlookupdata nd; 1733 struct stat st; 1734 int error; 1735 1736 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); 1737 if (error == 0) { 1738 error = kern_stat(&nd, &st); 1739 if (error == 0) 1740 error = copyout(&st, uap->ub, sizeof(*uap->ub)); 1741 } 1742 nlookup_done(&nd); 1743 return (error); 1744 } 1745 1746 void 1747 cvtnstat(sb, nsb) 1748 struct stat *sb; 1749 struct nstat *nsb; 1750 { 1751 nsb->st_dev = sb->st_dev; 1752 nsb->st_ino = sb->st_ino; 1753 nsb->st_mode = sb->st_mode; 1754 nsb->st_nlink = sb->st_nlink; 1755 nsb->st_uid = sb->st_uid; 1756 nsb->st_gid = sb->st_gid; 1757 nsb->st_rdev = sb->st_rdev; 1758 nsb->st_atimespec = sb->st_atimespec; 1759 nsb->st_mtimespec = sb->st_mtimespec; 1760 nsb->st_ctimespec = sb->st_ctimespec; 1761 nsb->st_size = sb->st_size; 1762 nsb->st_blocks = sb->st_blocks; 1763 nsb->st_blksize = sb->st_blksize; 1764 nsb->st_flags = sb->st_flags; 1765 nsb->st_gen = sb->st_gen; 1766 nsb->st_qspare[0] = sb->st_qspare[0]; 1767 nsb->st_qspare[1] = sb->st_qspare[1]; 1768 } 1769 1770 /* 1771 * nstat_args(char *path, struct nstat *ub) 1772 */ 1773 /* ARGSUSED */ 1774 int 1775 nstat(struct nstat_args *uap) 1776 { 1777 struct thread *td = curthread; 1778 struct vnode *vp; 1779 struct stat sb; 1780 struct nstat nsb; 1781 struct nlookupdata nd; 1782 int error; 1783 1784 vp = NULL; 1785 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW); 1786 if (error == 0) 1787 error = nlookup(&nd); 1788 if (error == 0) 1789 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp); 1790 nlookup_done(&nd); 1791 if (error == 0) { 1792 error = vn_stat(vp, &sb, td); 1793 vput(vp); 1794 if (error == 0) { 1795 cvtnstat(&sb, &nsb); 1796 error = copyout(&nsb, SCARG(uap, ub), sizeof(nsb)); 1797 } 1798 } 1799 return (error); 1800 } 1801 1802 /* 1803 * lstat_args(char *path, struct stat *ub) 1804 * 1805 * Get file status; this version does not follow links. 1806 */ 1807 /* ARGSUSED */ 1808 int 1809 nlstat(struct nlstat_args *uap) 1810 { 1811 struct thread *td = curthread; 1812 struct vnode *vp; 1813 struct stat sb; 1814 struct nstat nsb; 1815 struct nlookupdata nd; 1816 int error; 1817 1818 vp = NULL; 1819 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, 0); 1820 if (error == 0) 1821 error = nlookup(&nd); 1822 if (error == 0) 1823 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp); 1824 nlookup_done(&nd); 1825 if (error == 0) { 1826 error = vn_stat(vp, &sb, td); 1827 vput(vp); 1828 if (error == 0) { 1829 cvtnstat(&sb, &nsb); 1830 error = copyout(&nsb, SCARG(uap, ub), sizeof(nsb)); 1831 } 1832 } 1833 return (error); 1834 } 1835 1836 /* 1837 * pathconf_Args(char *path, int name) 1838 * 1839 * Get configurable pathname variables. 1840 */ 1841 /* ARGSUSED */ 1842 int 1843 pathconf(struct pathconf_args *uap) 1844 { 1845 struct nlookupdata nd; 1846 struct vnode *vp; 1847 int error; 1848 1849 vp = NULL; 1850 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW); 1851 if (error == 0) 1852 error = nlookup(&nd); 1853 if (error == 0) 1854 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp); 1855 nlookup_done(&nd); 1856 if (error == 0) { 1857 error = VOP_PATHCONF(vp, SCARG(uap, name), uap->sysmsg_fds); 1858 vput(vp); 1859 } 1860 return (error); 1861 } 1862 1863 /* 1864 * XXX: daver 1865 * kern_readlink isn't properly split yet. There is a copyin burried 1866 * in VOP_READLINK(). 1867 */ 1868 int 1869 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res) 1870 { 1871 struct thread *td = curthread; 1872 struct proc *p = td->td_proc; 1873 struct vnode *vp; 1874 struct iovec aiov; 1875 struct uio auio; 1876 int error; 1877 1878 if ((error = nlookup(nd)) != 0) 1879 return (error); 1880 error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp); 1881 if (error) 1882 return (error); 1883 if (vp->v_type != VLNK) { 1884 error = EINVAL; 1885 } else { 1886 aiov.iov_base = buf; 1887 aiov.iov_len = count; 1888 auio.uio_iov = &aiov; 1889 auio.uio_iovcnt = 1; 1890 auio.uio_offset = 0; 1891 auio.uio_rw = UIO_READ; 1892 auio.uio_segflg = UIO_USERSPACE; 1893 auio.uio_td = td; 1894 auio.uio_resid = count; 1895 error = VOP_READLINK(vp, &auio, p->p_ucred); 1896 } 1897 vput(vp); 1898 *res = count - auio.uio_resid; 1899 return (error); 1900 } 1901 1902 /* 1903 * readlink_args(char *path, char *buf, int count) 1904 * 1905 * Return target name of a symbolic link. 1906 */ 1907 int 1908 readlink(struct readlink_args *uap) 1909 { 1910 struct nlookupdata nd; 1911 int error; 1912 1913 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); 1914 if (error == 0) { 1915 error = kern_readlink(&nd, uap->buf, uap->count, 1916 &uap->sysmsg_result); 1917 } 1918 nlookup_done(&nd); 1919 return (error); 1920 } 1921 1922 static int 1923 setfflags(struct vnode *vp, int flags) 1924 { 1925 struct thread *td = curthread; 1926 struct proc *p = td->td_proc; 1927 int error; 1928 struct vattr vattr; 1929 1930 /* 1931 * Prevent non-root users from setting flags on devices. When 1932 * a device is reused, users can retain ownership of the device 1933 * if they are allowed to set flags and programs assume that 1934 * chown can't fail when done as root. 1935 */ 1936 if ((vp->v_type == VCHR || vp->v_type == VBLK) && 1937 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)) 1938 return (error); 1939 1940 /* 1941 * note: vget is required for any operation that might mod the vnode 1942 * so VINACTIVE is properly cleared. 1943 */ 1944 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE); 1945 if ((error = vget(vp, LK_EXCLUSIVE, td)) == 0) { 1946 VATTR_NULL(&vattr); 1947 vattr.va_flags = flags; 1948 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td); 1949 vput(vp); 1950 } 1951 return (error); 1952 } 1953 1954 /* 1955 * chflags(char *path, int flags) 1956 * 1957 * Change flags of a file given a path name. 1958 */ 1959 /* ARGSUSED */ 1960 int 1961 chflags(struct chflags_args *uap) 1962 { 1963 struct nlookupdata nd; 1964 struct vnode *vp; 1965 int error; 1966 1967 vp = NULL; 1968 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW); 1969 /* XXX Add NLC flag indicating modifying operation? */ 1970 if (error == 0) 1971 error = nlookup(&nd); 1972 if (error == 0) 1973 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp); 1974 nlookup_done(&nd); 1975 if (error == 0) { 1976 error = setfflags(vp, SCARG(uap, flags)); 1977 vrele(vp); 1978 } 1979 return (error); 1980 } 1981 1982 /* 1983 * fchflags_args(int fd, int flags) 1984 * 1985 * Change flags of a file given a file descriptor. 1986 */ 1987 /* ARGSUSED */ 1988 int 1989 fchflags(struct fchflags_args *uap) 1990 { 1991 struct thread *td = curthread; 1992 struct proc *p = td->td_proc; 1993 struct file *fp; 1994 int error; 1995 1996 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) 1997 return (error); 1998 return setfflags((struct vnode *) fp->f_data, SCARG(uap, flags)); 1999 } 2000 2001 static int 2002 setfmode(struct vnode *vp, int mode) 2003 { 2004 struct thread *td = curthread; 2005 struct proc *p = td->td_proc; 2006 int error; 2007 struct vattr vattr; 2008 2009 /* 2010 * note: vget is required for any operation that might mod the vnode 2011 * so VINACTIVE is properly cleared. 2012 */ 2013 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE); 2014 if ((error = vget(vp, LK_EXCLUSIVE, td)) == 0) { 2015 VATTR_NULL(&vattr); 2016 vattr.va_mode = mode & ALLPERMS; 2017 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td); 2018 vput(vp); 2019 } 2020 return error; 2021 } 2022 2023 int 2024 kern_chmod(struct nlookupdata *nd, int mode) 2025 { 2026 struct vnode *vp; 2027 int error; 2028 2029 /* XXX Add NLC flag indicating modifying operation? */ 2030 if ((error = nlookup(nd)) != 0) 2031 return (error); 2032 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0) 2033 return (error); 2034 error = setfmode(vp, mode); 2035 vrele(vp); 2036 return (error); 2037 } 2038 2039 /* 2040 * chmod_args(char *path, int mode) 2041 * 2042 * Change mode of a file given path name. 2043 */ 2044 /* ARGSUSED */ 2045 int 2046 chmod(struct chmod_args *uap) 2047 { 2048 struct nlookupdata nd; 2049 int error; 2050 2051 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 2052 if (error == 0) 2053 error = kern_chmod(&nd, uap->mode); 2054 nlookup_done(&nd); 2055 return (error); 2056 } 2057 2058 /* 2059 * lchmod_args(char *path, int mode) 2060 * 2061 * Change mode of a file given path name (don't follow links.) 2062 */ 2063 /* ARGSUSED */ 2064 int 2065 lchmod(struct lchmod_args *uap) 2066 { 2067 struct nlookupdata nd; 2068 int error; 2069 2070 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); 2071 if (error == 0) 2072 error = kern_chmod(&nd, uap->mode); 2073 nlookup_done(&nd); 2074 return (error); 2075 } 2076 2077 /* 2078 * fchmod_args(int fd, int mode) 2079 * 2080 * Change mode of a file given a file descriptor. 2081 */ 2082 /* ARGSUSED */ 2083 int 2084 fchmod(struct fchmod_args *uap) 2085 { 2086 struct thread *td = curthread; 2087 struct proc *p = td->td_proc; 2088 struct file *fp; 2089 int error; 2090 2091 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) 2092 return (error); 2093 return setfmode((struct vnode *)fp->f_data, SCARG(uap, mode)); 2094 } 2095 2096 static int 2097 setfown(struct vnode *vp, uid_t uid, gid_t gid) 2098 { 2099 struct thread *td = curthread; 2100 struct proc *p = td->td_proc; 2101 int error; 2102 struct vattr vattr; 2103 2104 /* 2105 * note: vget is required for any operation that might mod the vnode 2106 * so VINACTIVE is properly cleared. 2107 */ 2108 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE); 2109 if ((error = vget(vp, LK_EXCLUSIVE, td)) == 0) { 2110 VATTR_NULL(&vattr); 2111 vattr.va_uid = uid; 2112 vattr.va_gid = gid; 2113 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td); 2114 vput(vp); 2115 } 2116 return error; 2117 } 2118 2119 int 2120 kern_chown(struct nlookupdata *nd, int uid, int gid) 2121 { 2122 struct vnode *vp; 2123 int error; 2124 2125 /* XXX Add NLC flag indicating modifying operation? */ 2126 if ((error = nlookup(nd)) != 0) 2127 return (error); 2128 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0) 2129 return (error); 2130 error = setfown(vp, uid, gid); 2131 vrele(vp); 2132 return (error); 2133 } 2134 2135 /* 2136 * chown(char *path, int uid, int gid) 2137 * 2138 * Set ownership given a path name. 2139 */ 2140 int 2141 chown(struct chown_args *uap) 2142 { 2143 struct nlookupdata nd; 2144 int error; 2145 2146 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 2147 if (error == 0) 2148 error = kern_chown(&nd, uap->uid, uap->gid); 2149 nlookup_done(&nd); 2150 return (error); 2151 } 2152 2153 /* 2154 * lchown_args(char *path, int uid, int gid) 2155 * 2156 * Set ownership given a path name, do not cross symlinks. 2157 */ 2158 int 2159 lchown(struct lchown_args *uap) 2160 { 2161 struct nlookupdata nd; 2162 int error; 2163 2164 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); 2165 if (error == 0) 2166 error = kern_chown(&nd, uap->uid, uap->gid); 2167 nlookup_done(&nd); 2168 return (error); 2169 } 2170 2171 /* 2172 * fchown_args(int fd, int uid, int gid) 2173 * 2174 * Set ownership given a file descriptor. 2175 */ 2176 /* ARGSUSED */ 2177 int 2178 fchown(struct fchown_args *uap) 2179 { 2180 struct thread *td = curthread; 2181 struct proc *p = td->td_proc; 2182 struct file *fp; 2183 int error; 2184 2185 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) 2186 return (error); 2187 return setfown((struct vnode *)fp->f_data, 2188 SCARG(uap, uid), SCARG(uap, gid)); 2189 } 2190 2191 static int 2192 getutimes(const struct timeval *tvp, struct timespec *tsp) 2193 { 2194 struct timeval tv[2]; 2195 2196 if (tvp == NULL) { 2197 microtime(&tv[0]); 2198 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]); 2199 tsp[1] = tsp[0]; 2200 } else { 2201 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]); 2202 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]); 2203 } 2204 return 0; 2205 } 2206 2207 static int 2208 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag) 2209 { 2210 struct thread *td = curthread; 2211 struct proc *p = td->td_proc; 2212 int error; 2213 struct vattr vattr; 2214 2215 /* 2216 * note: vget is required for any operation that might mod the vnode 2217 * so VINACTIVE is properly cleared. 2218 */ 2219 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE); 2220 if ((error = vget(vp, LK_EXCLUSIVE, td)) == 0) { 2221 VATTR_NULL(&vattr); 2222 vattr.va_atime = ts[0]; 2223 vattr.va_mtime = ts[1]; 2224 if (nullflag) 2225 vattr.va_vaflags |= VA_UTIMES_NULL; 2226 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td); 2227 vput(vp); 2228 } 2229 return error; 2230 } 2231 2232 int 2233 kern_utimes(struct nlookupdata *nd, struct timeval *tptr) 2234 { 2235 struct timespec ts[2]; 2236 struct vnode *vp; 2237 int error; 2238 2239 if ((error = getutimes(tptr, ts)) != 0) 2240 return (error); 2241 /* XXX Add NLC flag indicating modifying operation? */ 2242 if ((error = nlookup(nd)) != 0) 2243 return (error); 2244 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0) 2245 return (error); 2246 error = setutimes(vp, ts, tptr == NULL); 2247 vrele(vp); 2248 return (error); 2249 } 2250 2251 /* 2252 * utimes_args(char *path, struct timeval *tptr) 2253 * 2254 * Set the access and modification times of a file. 2255 */ 2256 int 2257 utimes(struct utimes_args *uap) 2258 { 2259 struct timeval tv[2]; 2260 struct nlookupdata nd; 2261 int error; 2262 2263 if (uap->tptr) { 2264 error = copyin(uap->tptr, tv, sizeof(tv)); 2265 if (error) 2266 return (error); 2267 } 2268 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 2269 if (error == 0) 2270 error = kern_utimes(&nd, uap->tptr ? tv : NULL); 2271 nlookup_done(&nd); 2272 return (error); 2273 } 2274 2275 /* 2276 * lutimes_args(char *path, struct timeval *tptr) 2277 * 2278 * Set the access and modification times of a file. 2279 */ 2280 int 2281 lutimes(struct lutimes_args *uap) 2282 { 2283 struct timeval tv[2]; 2284 struct nlookupdata nd; 2285 int error; 2286 2287 if (uap->tptr) { 2288 error = copyin(uap->tptr, tv, sizeof(tv)); 2289 if (error) 2290 return (error); 2291 } 2292 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); 2293 if (error == 0) 2294 error = kern_utimes(&nd, uap->tptr ? tv : NULL); 2295 nlookup_done(&nd); 2296 return (error); 2297 } 2298 2299 int 2300 kern_futimes(int fd, struct timeval *tptr) 2301 { 2302 struct thread *td = curthread; 2303 struct proc *p = td->td_proc; 2304 struct timespec ts[2]; 2305 struct file *fp; 2306 int error; 2307 2308 error = getutimes(tptr, ts); 2309 if (error) 2310 return (error); 2311 error = getvnode(p->p_fd, fd, &fp); 2312 if (error) 2313 return (error); 2314 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL); 2315 return (error); 2316 } 2317 2318 /* 2319 * futimes_args(int fd, struct timeval *tptr) 2320 * 2321 * Set the access and modification times of a file. 2322 */ 2323 int 2324 futimes(struct futimes_args *uap) 2325 { 2326 struct timeval tv[2]; 2327 int error; 2328 2329 if (uap->tptr) { 2330 error = copyin(uap->tptr, tv, sizeof(tv)); 2331 if (error) 2332 return (error); 2333 } 2334 2335 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL); 2336 2337 return (error); 2338 } 2339 2340 int 2341 kern_truncate(struct nlookupdata *nd, off_t length) 2342 { 2343 struct vnode *vp; 2344 struct vattr vattr; 2345 int error; 2346 2347 if (length < 0) 2348 return(EINVAL); 2349 /* XXX Add NLC flag indicating modifying operation? */ 2350 if ((error = nlookup(nd)) != 0) 2351 return (error); 2352 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0) 2353 return (error); 2354 VOP_LEASE(vp, nd->nl_td, nd->nl_cred, LEASE_WRITE); 2355 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, nd->nl_td)) != 0) { 2356 vrele(vp); 2357 return (error); 2358 } 2359 if (vp->v_type == VDIR) { 2360 error = EISDIR; 2361 } else if ((error = vn_writechk(vp)) == 0 && 2362 (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred, nd->nl_td)) == 0) { 2363 VATTR_NULL(&vattr); 2364 vattr.va_size = length; 2365 error = VOP_SETATTR(vp, &vattr, nd->nl_cred, nd->nl_td); 2366 } 2367 vput(vp); 2368 return (error); 2369 } 2370 2371 /* 2372 * truncate(char *path, int pad, off_t length) 2373 * 2374 * Truncate a file given its path name. 2375 */ 2376 int 2377 truncate(struct truncate_args *uap) 2378 { 2379 struct nlookupdata nd; 2380 int error; 2381 2382 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 2383 if (error == 0) 2384 error = kern_truncate(&nd, uap->length); 2385 nlookup_done(&nd); 2386 return error; 2387 } 2388 2389 int 2390 kern_ftruncate(int fd, off_t length) 2391 { 2392 struct thread *td = curthread; 2393 struct proc *p = td->td_proc; 2394 struct vattr vattr; 2395 struct vnode *vp; 2396 struct file *fp; 2397 int error; 2398 2399 if (length < 0) 2400 return(EINVAL); 2401 if ((error = getvnode(p->p_fd, fd, &fp)) != 0) 2402 return (error); 2403 if ((fp->f_flag & FWRITE) == 0) 2404 return (EINVAL); 2405 vp = (struct vnode *)fp->f_data; 2406 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE); 2407 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 2408 if (vp->v_type == VDIR) 2409 error = EISDIR; 2410 else if ((error = vn_writechk(vp)) == 0) { 2411 VATTR_NULL(&vattr); 2412 vattr.va_size = length; 2413 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td); 2414 } 2415 VOP_UNLOCK(vp, 0, td); 2416 return (error); 2417 } 2418 2419 /* 2420 * ftruncate_args(int fd, int pad, off_t length) 2421 * 2422 * Truncate a file given a file descriptor. 2423 */ 2424 int 2425 ftruncate(struct ftruncate_args *uap) 2426 { 2427 int error; 2428 2429 error = kern_ftruncate(uap->fd, uap->length); 2430 2431 return (error); 2432 } 2433 2434 /* 2435 * fsync(int fd) 2436 * 2437 * Sync an open file. 2438 */ 2439 /* ARGSUSED */ 2440 int 2441 fsync(struct fsync_args *uap) 2442 { 2443 struct thread *td = curthread; 2444 struct proc *p = td->td_proc; 2445 struct vnode *vp; 2446 struct file *fp; 2447 vm_object_t obj; 2448 int error; 2449 2450 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) 2451 return (error); 2452 vp = (struct vnode *)fp->f_data; 2453 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 2454 if (VOP_GETVOBJECT(vp, &obj) == 0) 2455 vm_object_page_clean(obj, 0, 0, 0); 2456 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) == 0 && 2457 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) && 2458 bioops.io_fsync) 2459 error = (*bioops.io_fsync)(vp); 2460 VOP_UNLOCK(vp, 0, td); 2461 return (error); 2462 } 2463 2464 int 2465 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond) 2466 { 2467 struct namecache *fncpd; 2468 struct namecache *tncpd; 2469 struct namecache *ncp; 2470 struct mount *mp; 2471 int error; 2472 2473 bwillwrite(); 2474 if ((error = nlookup(fromnd)) != 0) 2475 return (error); 2476 if ((fncpd = fromnd->nl_ncp->nc_parent) == NULL) 2477 return (ENOENT); 2478 cache_hold(fncpd); 2479 2480 /* 2481 * unlock the source ncp so we can lookup the target ncp without 2482 * deadlocking. The target may or may not exist so we do not check 2483 * for a target vp like kern_mkdir() and other creation functions do. 2484 * 2485 * The source and target directories are ref'd and rechecked after 2486 * everything is relocked to determine if the source or target file 2487 * has been renamed. 2488 */ 2489 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED); 2490 fromnd->nl_flags &= ~NLC_NCPISLOCKED; 2491 cache_unlock(fromnd->nl_ncp); 2492 2493 tond->nl_flags |= NLC_CREATE; 2494 if ((error = nlookup(tond)) != 0) { 2495 cache_drop(fncpd); 2496 return (error); 2497 } 2498 if ((tncpd = tond->nl_ncp->nc_parent) == NULL) { 2499 cache_drop(fncpd); 2500 return (ENOENT); 2501 } 2502 cache_hold(tncpd); 2503 2504 /* 2505 * If the source and target are the same there is nothing to do 2506 */ 2507 if (fromnd->nl_ncp == tond->nl_ncp) { 2508 cache_drop(fncpd); 2509 cache_drop(tncpd); 2510 return (0); 2511 } 2512 2513 /* 2514 * relock the source ncp 2515 */ 2516 if (cache_lock_nonblock(fromnd->nl_ncp) == 0) { 2517 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred); 2518 } else if (fromnd->nl_ncp > tond->nl_ncp) { 2519 cache_lock(fromnd->nl_ncp); 2520 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred); 2521 } else { 2522 cache_unlock(tond->nl_ncp); 2523 cache_lock(fromnd->nl_ncp); 2524 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred); 2525 cache_lock(tond->nl_ncp); 2526 cache_resolve(tond->nl_ncp, tond->nl_cred); 2527 } 2528 fromnd->nl_flags |= NLC_NCPISLOCKED; 2529 2530 /* 2531 * make sure the parent directories linkages are the same 2532 */ 2533 if (fncpd != fromnd->nl_ncp->nc_parent || 2534 tncpd != tond->nl_ncp->nc_parent) { 2535 cache_drop(fncpd); 2536 cache_drop(tncpd); 2537 return (ENOENT); 2538 } 2539 2540 /* 2541 * Both the source and target must be within the same filesystem and 2542 * in the same filesystem as their parent directories within the 2543 * namecache topology. 2544 */ 2545 mp = fncpd->nc_mount; 2546 if (mp != tncpd->nc_mount || mp != fromnd->nl_ncp->nc_mount || 2547 mp != tond->nl_ncp->nc_mount) { 2548 cache_drop(fncpd); 2549 cache_drop(tncpd); 2550 return (EXDEV); 2551 } 2552 2553 /* 2554 * If the target exists and either the source or target is a directory, 2555 * then both must be directories. 2556 */ 2557 if (tond->nl_ncp->nc_vp) { 2558 if (fromnd->nl_ncp->nc_vp->v_type == VDIR) { 2559 if (tond->nl_ncp->nc_vp->v_type != VDIR) 2560 error = ENOTDIR; 2561 } else if (tond->nl_ncp->nc_vp->v_type == VDIR) { 2562 error = EISDIR; 2563 } 2564 } 2565 2566 /* 2567 * You cannot rename a source into itself or a subdirectory of itself. 2568 * We check this by travsersing the target directory upwards looking 2569 * for a match against the source. 2570 */ 2571 if (error == 0) { 2572 for (ncp = tncpd; ncp; ncp = ncp->nc_parent) { 2573 if (fromnd->nl_ncp == ncp) { 2574 error = EINVAL; 2575 break; 2576 } 2577 } 2578 } 2579 2580 cache_drop(fncpd); 2581 cache_drop(tncpd); 2582 if (error) 2583 return (error); 2584 error = VOP_NRENAME(fromnd->nl_ncp, tond->nl_ncp, tond->nl_cred); 2585 return (error); 2586 } 2587 2588 /* 2589 * rename_args(char *from, char *to) 2590 * 2591 * Rename files. Source and destination must either both be directories, 2592 * or both not be directories. If target is a directory, it must be empty. 2593 */ 2594 int 2595 rename(struct rename_args *uap) 2596 { 2597 struct nlookupdata fromnd, tond; 2598 int error; 2599 2600 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0); 2601 if (error == 0) { 2602 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0); 2603 if (error == 0) 2604 error = kern_rename(&fromnd, &tond); 2605 nlookup_done(&tond); 2606 } 2607 nlookup_done(&fromnd); 2608 return (error); 2609 } 2610 2611 int 2612 kern_mkdir(struct nlookupdata *nd, int mode) 2613 { 2614 struct thread *td = curthread; 2615 struct proc *p = td->td_proc; 2616 struct namecache *ncp; 2617 struct vnode *vp; 2618 struct vattr vattr; 2619 int error; 2620 2621 bwillwrite(); 2622 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE; 2623 if ((error = nlookup(nd)) != 0) 2624 return (error); 2625 2626 ncp = nd->nl_ncp; 2627 if (ncp->nc_vp) 2628 return (EEXIST); 2629 2630 VATTR_NULL(&vattr); 2631 vattr.va_type = VDIR; 2632 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask; 2633 2634 vp = NULL; 2635 error = VOP_NMKDIR(ncp, &vp, p->p_ucred, &vattr); 2636 if (error == 0) 2637 vput(vp); 2638 return (error); 2639 } 2640 2641 /* 2642 * mkdir_args(char *path, int mode) 2643 * 2644 * Make a directory file. 2645 */ 2646 /* ARGSUSED */ 2647 int 2648 mkdir(struct mkdir_args *uap) 2649 { 2650 struct nlookupdata nd; 2651 int error; 2652 2653 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); 2654 if (error == 0) 2655 error = kern_mkdir(&nd, uap->mode); 2656 nlookup_done(&nd); 2657 return (error); 2658 } 2659 2660 int 2661 kern_rmdir(struct nlookupdata *nd) 2662 { 2663 struct namecache *ncp; 2664 int error; 2665 2666 bwillwrite(); 2667 nd->nl_flags |= NLC_DELETE; 2668 if ((error = nlookup(nd)) != 0) 2669 return (error); 2670 2671 ncp = nd->nl_ncp; 2672 error = VOP_NRMDIR(ncp, nd->nl_cred); 2673 return (error); 2674 } 2675 2676 /* 2677 * rmdir_args(char *path) 2678 * 2679 * Remove a directory file. 2680 */ 2681 /* ARGSUSED */ 2682 int 2683 rmdir(struct rmdir_args *uap) 2684 { 2685 struct nlookupdata nd; 2686 int error; 2687 2688 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); 2689 if (error == 0) 2690 error = kern_rmdir(&nd); 2691 nlookup_done(&nd); 2692 return (error); 2693 } 2694 2695 int 2696 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res) 2697 { 2698 struct thread *td = curthread; 2699 struct proc *p = td->td_proc; 2700 struct vnode *vp; 2701 struct file *fp; 2702 struct uio auio; 2703 struct iovec aiov; 2704 long loff; 2705 int error, eofflag; 2706 2707 if ((error = getvnode(p->p_fd, fd, &fp)) != 0) 2708 return (error); 2709 if ((fp->f_flag & FREAD) == 0) 2710 return (EBADF); 2711 vp = (struct vnode *)fp->f_data; 2712 unionread: 2713 if (vp->v_type != VDIR) 2714 return (EINVAL); 2715 aiov.iov_base = buf; 2716 aiov.iov_len = count; 2717 auio.uio_iov = &aiov; 2718 auio.uio_iovcnt = 1; 2719 auio.uio_rw = UIO_READ; 2720 auio.uio_segflg = UIO_USERSPACE; 2721 auio.uio_td = td; 2722 auio.uio_resid = count; 2723 /* vn_lock(vp, LK_SHARED | LK_RETRY, td); */ 2724 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 2725 loff = auio.uio_offset = fp->f_offset; 2726 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL); 2727 fp->f_offset = auio.uio_offset; 2728 VOP_UNLOCK(vp, 0, td); 2729 if (error) 2730 return (error); 2731 if (count == auio.uio_resid) { 2732 if (union_dircheckp) { 2733 error = union_dircheckp(td, &vp, fp); 2734 if (error == -1) 2735 goto unionread; 2736 if (error) 2737 return (error); 2738 } 2739 if ((vp->v_flag & VROOT) && 2740 (vp->v_mount->mnt_flag & MNT_UNION)) { 2741 struct vnode *tvp = vp; 2742 vp = vp->v_mount->mnt_vnodecovered; 2743 vref(vp); 2744 fp->f_data = (caddr_t)vp; 2745 fp->f_offset = 0; 2746 vrele(tvp); 2747 goto unionread; 2748 } 2749 } 2750 if (basep) { 2751 *basep = loff; 2752 } 2753 *res = count - auio.uio_resid; 2754 return (error); 2755 } 2756 2757 /* 2758 * getdirentries_args(int fd, char *buf, u_int conut, long *basep) 2759 * 2760 * Read a block of directory entries in a file system independent format. 2761 */ 2762 int 2763 getdirentries(struct getdirentries_args *uap) 2764 { 2765 long base; 2766 int error; 2767 2768 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base, 2769 &uap->sysmsg_result); 2770 2771 if (error == 0) 2772 error = copyout(&base, uap->basep, sizeof(*uap->basep)); 2773 return (error); 2774 } 2775 2776 /* 2777 * getdents_args(int fd, char *buf, size_t count) 2778 */ 2779 int 2780 getdents(struct getdents_args *uap) 2781 { 2782 int error; 2783 2784 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL, 2785 &uap->sysmsg_result); 2786 2787 return (error); 2788 } 2789 2790 /* 2791 * umask(int newmask) 2792 * 2793 * Set the mode mask for creation of filesystem nodes. 2794 * 2795 * MP SAFE 2796 */ 2797 int 2798 umask(struct umask_args *uap) 2799 { 2800 struct thread *td = curthread; 2801 struct proc *p = td->td_proc; 2802 struct filedesc *fdp; 2803 2804 fdp = p->p_fd; 2805 uap->sysmsg_result = fdp->fd_cmask; 2806 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS; 2807 return (0); 2808 } 2809 2810 /* 2811 * revoke(char *path) 2812 * 2813 * Void all references to file by ripping underlying filesystem 2814 * away from vnode. 2815 */ 2816 /* ARGSUSED */ 2817 int 2818 revoke(struct revoke_args *uap) 2819 { 2820 struct thread *td = curthread; 2821 struct nlookupdata nd; 2822 struct vattr vattr; 2823 struct vnode *vp; 2824 struct ucred *cred; 2825 int error; 2826 2827 vp = NULL; 2828 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW); 2829 if (error == 0) 2830 error = nlookup(&nd); 2831 if (error == 0) 2832 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp); 2833 cred = crhold(nd.nl_cred); 2834 nlookup_done(&nd); 2835 if (error == 0) { 2836 if (vp->v_type != VCHR && vp->v_type != VBLK) 2837 error = EINVAL; 2838 if (error == 0) 2839 error = VOP_GETATTR(vp, &vattr, td); 2840 if (error == 0 && cred->cr_uid != vattr.va_uid) 2841 error = suser_cred(cred, PRISON_ROOT); 2842 if (error == 0 && count_udev(vp->v_udev) > 0) { 2843 if ((error = vx_lock(vp)) == 0) { 2844 VOP_REVOKE(vp, REVOKEALL); 2845 vx_unlock(vp); 2846 } 2847 } 2848 vrele(vp); 2849 } 2850 crfree(cred); 2851 return (error); 2852 } 2853 2854 /* 2855 * Convert a user file descriptor to a kernel file entry. 2856 */ 2857 int 2858 getvnode(struct filedesc *fdp, int fd, struct file **fpp) 2859 { 2860 struct file *fp; 2861 2862 if ((u_int)fd >= fdp->fd_nfiles || 2863 (fp = fdp->fd_ofiles[fd]) == NULL) 2864 return (EBADF); 2865 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO) 2866 return (EINVAL); 2867 *fpp = fp; 2868 return (0); 2869 } 2870 /* 2871 * getfh_args(char *fname, fhandle_t *fhp) 2872 * 2873 * Get (NFS) file handle 2874 */ 2875 int 2876 getfh(struct getfh_args *uap) 2877 { 2878 struct thread *td = curthread; 2879 struct nlookupdata nd; 2880 fhandle_t fh; 2881 struct vnode *vp; 2882 int error; 2883 2884 /* 2885 * Must be super user 2886 */ 2887 if ((error = suser(td)) != 0) 2888 return (error); 2889 2890 vp = NULL; 2891 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW); 2892 if (error == 0) 2893 error = nlookup(&nd); 2894 if (error == 0) 2895 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp); 2896 nlookup_done(&nd); 2897 if (error == 0) { 2898 bzero(&fh, sizeof(fh)); 2899 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid; 2900 error = VFS_VPTOFH(vp, &fh.fh_fid); 2901 vput(vp); 2902 if (error == 0) 2903 error = copyout(&fh, uap->fhp, sizeof(fh)); 2904 } 2905 return (error); 2906 } 2907 2908 /* 2909 * fhopen_args(const struct fhandle *u_fhp, int flags) 2910 * 2911 * syscall for the rpc.lockd to use to translate a NFS file handle into 2912 * an open descriptor. 2913 * 2914 * warning: do not remove the suser() call or this becomes one giant 2915 * security hole. 2916 */ 2917 int 2918 fhopen(struct fhopen_args *uap) 2919 { 2920 struct thread *td = curthread; 2921 struct proc *p = td->td_proc; 2922 struct mount *mp; 2923 struct vnode *vp; 2924 struct fhandle fhp; 2925 struct vattr vat; 2926 struct vattr *vap = &vat; 2927 struct flock lf; 2928 struct filedesc *fdp = p->p_fd; 2929 int fmode, mode, error, type; 2930 struct file *nfp; 2931 struct file *fp; 2932 int indx; 2933 2934 /* 2935 * Must be super user 2936 */ 2937 error = suser(td); 2938 if (error) 2939 return (error); 2940 2941 fmode = FFLAGS(SCARG(uap, flags)); 2942 /* why not allow a non-read/write open for our lockd? */ 2943 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT)) 2944 return (EINVAL); 2945 error = copyin(SCARG(uap,u_fhp), &fhp, sizeof(fhp)); 2946 if (error) 2947 return(error); 2948 /* find the mount point */ 2949 mp = vfs_getvfs(&fhp.fh_fsid); 2950 if (mp == NULL) 2951 return (ESTALE); 2952 /* now give me my vnode, it gets returned to me locked */ 2953 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp); 2954 if (error) 2955 return (error); 2956 /* 2957 * from now on we have to make sure not 2958 * to forget about the vnode 2959 * any error that causes an abort must vput(vp) 2960 * just set error = err and 'goto bad;'. 2961 */ 2962 2963 /* 2964 * from vn_open 2965 */ 2966 if (vp->v_type == VLNK) { 2967 error = EMLINK; 2968 goto bad; 2969 } 2970 if (vp->v_type == VSOCK) { 2971 error = EOPNOTSUPP; 2972 goto bad; 2973 } 2974 mode = 0; 2975 if (fmode & (FWRITE | O_TRUNC)) { 2976 if (vp->v_type == VDIR) { 2977 error = EISDIR; 2978 goto bad; 2979 } 2980 error = vn_writechk(vp); 2981 if (error) 2982 goto bad; 2983 mode |= VWRITE; 2984 } 2985 if (fmode & FREAD) 2986 mode |= VREAD; 2987 if (mode) { 2988 error = VOP_ACCESS(vp, mode, p->p_ucred, td); 2989 if (error) 2990 goto bad; 2991 } 2992 if (fmode & O_TRUNC) { 2993 VOP_UNLOCK(vp, 0, td); /* XXX */ 2994 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE); 2995 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */ 2996 VATTR_NULL(vap); 2997 vap->va_size = 0; 2998 error = VOP_SETATTR(vp, vap, p->p_ucred, td); 2999 if (error) 3000 goto bad; 3001 } 3002 3003 /* 3004 * VOP_OPEN needs the file pointer so it can potentially override 3005 * it. 3006 * 3007 * WARNING! no f_ncp will be associated when fhopen()ing a directory. 3008 * XXX 3009 */ 3010 if ((error = falloc(p, &nfp, NULL)) != 0) 3011 goto bad; 3012 fp = nfp; 3013 3014 fp->f_data = (caddr_t)vp; 3015 fp->f_flag = fmode & FMASK; 3016 fp->f_ops = &vnode_fileops; 3017 fp->f_type = DTYPE_VNODE; 3018 3019 error = VOP_OPEN(vp, fmode, p->p_ucred, fp, td); 3020 if (error) { 3021 /* 3022 * setting f_ops this way prevents VOP_CLOSE from being 3023 * called or fdrop() releasing the vp from v_data. Since 3024 * the VOP_OPEN failed we don't want to VOP_CLOSE. 3025 */ 3026 fp->f_ops = &badfileops; 3027 fp->f_data = NULL; 3028 fdrop(fp, td); 3029 goto bad; 3030 } 3031 if (fmode & FWRITE) 3032 vp->v_writecount++; 3033 3034 /* 3035 * The fp now owns a reference on the vnode. We still have our own 3036 * ref+lock. 3037 */ 3038 vref(vp); 3039 3040 /* 3041 * Make sure that a VM object is created for VMIO support. If this 3042 * fails just fdrop() normally to clean up. 3043 */ 3044 if (vn_canvmio(vp) == TRUE) { 3045 if ((error = vfs_object_create(vp, td)) != 0) { 3046 fdrop(fp, td); 3047 goto bad; 3048 } 3049 } 3050 3051 /* 3052 * The open was successful, associate it with a file descriptor. 3053 */ 3054 if ((error = fsetfd(p, fp, &indx)) != 0) { 3055 if (fmode & FWRITE) 3056 vp->v_writecount--; 3057 fdrop(fp, td); 3058 goto bad; 3059 } 3060 3061 if (fmode & (O_EXLOCK | O_SHLOCK)) { 3062 lf.l_whence = SEEK_SET; 3063 lf.l_start = 0; 3064 lf.l_len = 0; 3065 if (fmode & O_EXLOCK) 3066 lf.l_type = F_WRLCK; 3067 else 3068 lf.l_type = F_RDLCK; 3069 type = F_FLOCK; 3070 if ((fmode & FNONBLOCK) == 0) 3071 type |= F_WAIT; 3072 VOP_UNLOCK(vp, 0, td); 3073 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) { 3074 /* 3075 * lock request failed. Normally close the descriptor 3076 * but handle the case where someone might have dup()d 3077 * or close()d it when we weren't looking. 3078 */ 3079 if (fdp->fd_ofiles[indx] == fp) { 3080 fdp->fd_ofiles[indx] = NULL; 3081 fdrop(fp, td); 3082 } 3083 3084 /* 3085 * release our private reference. 3086 */ 3087 fdrop(fp, td); 3088 vrele(vp); 3089 return (error); 3090 } 3091 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 3092 fp->f_flag |= FHASLOCK; 3093 } 3094 if ((vp->v_type == VREG) && (VOP_GETVOBJECT(vp, NULL) != 0)) 3095 vfs_object_create(vp, td); 3096 3097 vput(vp); 3098 fdrop(fp, td); 3099 uap->sysmsg_result = indx; 3100 return (0); 3101 3102 bad: 3103 vput(vp); 3104 return (error); 3105 } 3106 3107 /* 3108 * fhstat_args(struct fhandle *u_fhp, struct stat *sb) 3109 */ 3110 int 3111 fhstat(struct fhstat_args *uap) 3112 { 3113 struct thread *td = curthread; 3114 struct stat sb; 3115 fhandle_t fh; 3116 struct mount *mp; 3117 struct vnode *vp; 3118 int error; 3119 3120 /* 3121 * Must be super user 3122 */ 3123 error = suser(td); 3124 if (error) 3125 return (error); 3126 3127 error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t)); 3128 if (error) 3129 return (error); 3130 3131 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) 3132 return (ESTALE); 3133 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp))) 3134 return (error); 3135 error = vn_stat(vp, &sb, td); 3136 vput(vp); 3137 if (error) 3138 return (error); 3139 error = copyout(&sb, SCARG(uap, sb), sizeof(sb)); 3140 return (error); 3141 } 3142 3143 /* 3144 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf) 3145 */ 3146 int 3147 fhstatfs(struct fhstatfs_args *uap) 3148 { 3149 struct thread *td = curthread; 3150 struct statfs *sp; 3151 struct mount *mp; 3152 struct vnode *vp; 3153 struct statfs sb; 3154 fhandle_t fh; 3155 int error; 3156 3157 /* 3158 * Must be super user 3159 */ 3160 if ((error = suser(td))) 3161 return (error); 3162 3163 if ((error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t))) != 0) 3164 return (error); 3165 3166 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) 3167 return (ESTALE); 3168 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp))) 3169 return (error); 3170 mp = vp->v_mount; 3171 sp = &mp->mnt_stat; 3172 vput(vp); 3173 if ((error = VFS_STATFS(mp, sp, td)) != 0) 3174 return (error); 3175 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; 3176 if (suser(td)) { 3177 bcopy(sp, &sb, sizeof(sb)); 3178 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0; 3179 sp = &sb; 3180 } 3181 return (copyout(sp, SCARG(uap, buf), sizeof(*sp))); 3182 } 3183 3184 /* 3185 * Syscall to push extended attribute configuration information into the 3186 * VFS. Accepts a path, which it converts to a mountpoint, as well as 3187 * a command (int cmd), and attribute name and misc data. For now, the 3188 * attribute name is left in userspace for consumption by the VFS_op. 3189 * It will probably be changed to be copied into sysspace by the 3190 * syscall in the future, once issues with various consumers of the 3191 * attribute code have raised their hands. 3192 * 3193 * Currently this is used only by UFS Extended Attributes. 3194 */ 3195 int 3196 extattrctl(struct extattrctl_args *uap) 3197 { 3198 struct nlookupdata nd; 3199 struct mount *mp; 3200 struct vnode *vp; 3201 int error; 3202 3203 vp = NULL; 3204 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 3205 if (error == 0) 3206 error = nlookup(&nd); 3207 if (error == 0) { 3208 mp = nd.nl_ncp->nc_mount; 3209 error = VFS_EXTATTRCTL(mp, SCARG(uap, cmd), 3210 SCARG(uap, attrname), SCARG(uap, arg), 3211 nd.nl_td); 3212 } 3213 nlookup_done(&nd); 3214 return (error); 3215 } 3216 3217 /* 3218 * Syscall to set a named extended attribute on a file or directory. 3219 * Accepts attribute name, and a uio structure pointing to the data to set. 3220 * The uio is consumed in the style of writev(). The real work happens 3221 * in VOP_SETEXTATTR(). 3222 */ 3223 int 3224 extattr_set_file(struct extattr_set_file_args *uap) 3225 { 3226 char attrname[EXTATTR_MAXNAMELEN]; 3227 struct iovec aiov[UIO_SMALLIOV]; 3228 struct iovec *needfree; 3229 struct nlookupdata nd; 3230 struct iovec *iov; 3231 struct vnode *vp; 3232 struct uio auio; 3233 u_int iovlen; 3234 u_int cnt; 3235 int error; 3236 int i; 3237 3238 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN); 3239 if (error) 3240 return (error); 3241 3242 vp = NULL; 3243 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 3244 if (error == 0) 3245 error = nlookup(&nd); 3246 if (error == 0) 3247 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp); 3248 if (error) { 3249 nlookup_done(&nd); 3250 return (error); 3251 } 3252 3253 needfree = NULL; 3254 iovlen = uap->iovcnt * sizeof(struct iovec); 3255 if (uap->iovcnt > UIO_SMALLIOV) { 3256 if (uap->iovcnt > UIO_MAXIOV) { 3257 error = EINVAL; 3258 goto done; 3259 } 3260 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK); 3261 needfree = iov; 3262 } else { 3263 iov = aiov; 3264 } 3265 auio.uio_iov = iov; 3266 auio.uio_iovcnt = uap->iovcnt; 3267 auio.uio_rw = UIO_WRITE; 3268 auio.uio_segflg = UIO_USERSPACE; 3269 auio.uio_td = nd.nl_td; 3270 auio.uio_offset = 0; 3271 if ((error = copyin(uap->iovp, iov, iovlen))) 3272 goto done; 3273 auio.uio_resid = 0; 3274 for (i = 0; i < uap->iovcnt; i++) { 3275 if (iov->iov_len > INT_MAX - auio.uio_resid) { 3276 error = EINVAL; 3277 goto done; 3278 } 3279 auio.uio_resid += iov->iov_len; 3280 iov++; 3281 } 3282 cnt = auio.uio_resid; 3283 error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred, nd.nl_td); 3284 cnt -= auio.uio_resid; 3285 uap->sysmsg_result = cnt; 3286 done: 3287 vput(vp); 3288 nlookup_done(&nd); 3289 if (needfree) 3290 FREE(needfree, M_IOV); 3291 return (error); 3292 } 3293 3294 /* 3295 * Syscall to get a named extended attribute on a file or directory. 3296 * Accepts attribute name, and a uio structure pointing to a buffer for the 3297 * data. The uio is consumed in the style of readv(). The real work 3298 * happens in VOP_GETEXTATTR(); 3299 */ 3300 int 3301 extattr_get_file(struct extattr_get_file_args *uap) 3302 { 3303 char attrname[EXTATTR_MAXNAMELEN]; 3304 struct iovec aiov[UIO_SMALLIOV]; 3305 struct iovec *needfree; 3306 struct nlookupdata nd; 3307 struct iovec *iov; 3308 struct vnode *vp; 3309 struct uio auio; 3310 u_int iovlen; 3311 u_int cnt; 3312 int error; 3313 int i; 3314 3315 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN); 3316 if (error) 3317 return (error); 3318 3319 vp = NULL; 3320 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 3321 if (error == 0) 3322 error = nlookup(&nd); 3323 if (error == 0) 3324 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp); 3325 if (error) { 3326 nlookup_done(&nd); 3327 return (error); 3328 } 3329 3330 iovlen = uap->iovcnt * sizeof (struct iovec); 3331 needfree = NULL; 3332 if (uap->iovcnt > UIO_SMALLIOV) { 3333 if (uap->iovcnt > UIO_MAXIOV) { 3334 error = EINVAL; 3335 goto done; 3336 } 3337 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK); 3338 needfree = iov; 3339 } else { 3340 iov = aiov; 3341 } 3342 auio.uio_iov = iov; 3343 auio.uio_iovcnt = uap->iovcnt; 3344 auio.uio_rw = UIO_READ; 3345 auio.uio_segflg = UIO_USERSPACE; 3346 auio.uio_td = nd.nl_td; 3347 auio.uio_offset = 0; 3348 if ((error = copyin(uap->iovp, iov, iovlen))) 3349 goto done; 3350 auio.uio_resid = 0; 3351 for (i = 0; i < uap->iovcnt; i++) { 3352 if (iov->iov_len > INT_MAX - auio.uio_resid) { 3353 error = EINVAL; 3354 goto done; 3355 } 3356 auio.uio_resid += iov->iov_len; 3357 iov++; 3358 } 3359 cnt = auio.uio_resid; 3360 error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred, nd.nl_td); 3361 cnt -= auio.uio_resid; 3362 uap->sysmsg_result = cnt; 3363 done: 3364 vput(vp); 3365 nlookup_done(&nd); 3366 if (needfree) 3367 FREE(needfree, M_IOV); 3368 return(error); 3369 } 3370 3371 /* 3372 * Syscall to delete a named extended attribute from a file or directory. 3373 * Accepts attribute name. The real work happens in VOP_SETEXTATTR(). 3374 */ 3375 int 3376 extattr_delete_file(struct extattr_delete_file_args *uap) 3377 { 3378 char attrname[EXTATTR_MAXNAMELEN]; 3379 struct nlookupdata nd; 3380 struct vnode *vp; 3381 int error; 3382 3383 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN); 3384 if (error) 3385 return(error); 3386 3387 vp = NULL; 3388 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 3389 if (error == 0) 3390 error = nlookup(&nd); 3391 if (error == 0) 3392 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp); 3393 if (error) { 3394 nlookup_done(&nd); 3395 return (error); 3396 } 3397 3398 error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred, nd.nl_td); 3399 vput(vp); 3400 nlookup_done(&nd); 3401 return(error); 3402 } 3403 3404 /* 3405 * print out statistics from the current status of the buffer pool 3406 * this can be toggeled by the system control option debug.syncprt 3407 */ 3408 #ifdef DEBUG 3409 void 3410 vfs_bufstats(void) 3411 { 3412 int s, i, j, count; 3413 struct buf *bp; 3414 struct bqueues *dp; 3415 int counts[(MAXBSIZE / PAGE_SIZE) + 1]; 3416 static char *bname[3] = { "LOCKED", "LRU", "AGE" }; 3417 3418 for (dp = bufqueues, i = 0; dp < &bufqueues[3]; dp++, i++) { 3419 count = 0; 3420 for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++) 3421 counts[j] = 0; 3422 s = splbio(); 3423 TAILQ_FOREACH(bp, dp, b_freelist) { 3424 counts[bp->b_bufsize/PAGE_SIZE]++; 3425 count++; 3426 } 3427 splx(s); 3428 printf("%s: total-%d", bname[i], count); 3429 for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++) 3430 if (counts[j] != 0) 3431 printf(", %d-%d", j * PAGE_SIZE, counts[j]); 3432 printf("\n"); 3433 } 3434 } 3435 #endif 3436