1 /* $NetBSD: vfs_syscalls.c,v 1.218 2005/04/06 13:49:31 yamt Exp $ */ 2 3 /* 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * (c) UNIX System Laboratories, Inc. 7 * All or some portions of this file are derived from material licensed 8 * to the University of California by American Telephone and Telegraph 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 * the permission of UNIX System Laboratories, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)vfs_syscalls.c 8.42 (Berkeley) 7/31/95 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls.c,v 1.218 2005/04/06 13:49:31 yamt Exp $"); 41 42 #include "opt_compat_netbsd.h" 43 #include "opt_compat_43.h" 44 #include "opt_ktrace.h" 45 #include "fss.h" 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/namei.h> 50 #include <sys/filedesc.h> 51 #include <sys/kernel.h> 52 #include <sys/file.h> 53 #include <sys/stat.h> 54 #include <sys/vnode.h> 55 #include <sys/mount.h> 56 #include <sys/proc.h> 57 #include <sys/uio.h> 58 #include <sys/malloc.h> 59 #include <sys/dirent.h> 60 #include <sys/extattr.h> 61 #include <sys/sysctl.h> 62 #include <sys/sa.h> 63 #include <sys/syscallargs.h> 64 #ifdef KTRACE 65 #include <sys/ktrace.h> 66 #endif 67 68 #include <miscfs/genfs/genfs.h> 69 #include <miscfs/syncfs/syncfs.h> 70 71 #if NFSS > 0 72 #include <dev/fssvar.h> 73 #endif 74 75 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount struct"); 76 77 static int change_dir(struct nameidata *, struct proc *); 78 static int change_flags(struct vnode *, u_long, struct proc *); 79 static int change_mode(struct vnode *, int, struct proc *p); 80 static int change_owner(struct vnode *, uid_t, gid_t, struct proc *, int); 81 static int change_utimes(struct vnode *vp, const struct timeval *, 82 struct proc *p); 83 static int rename_files(const char *, const char *, struct proc *, int); 84 85 void checkdirs(struct vnode *); 86 87 int dovfsusermount = 0; 88 89 /* 90 * Virtual File System System Calls 91 */ 92 93 /* 94 * Mount a file system. 95 */ 96 97 #if defined(COMPAT_09) || defined(COMPAT_43) 98 /* 99 * This table is used to maintain compatibility with 4.3BSD 100 * and NetBSD 0.9 mount syscalls. Note, the order is important! 101 * 102 * Do not modify this table. It should only contain filesystems 103 * supported by NetBSD 0.9 and 4.3BSD. 104 */ 105 const char * const mountcompatnames[] = { 106 NULL, /* 0 = MOUNT_NONE */ 107 MOUNT_FFS, /* 1 = MOUNT_UFS */ 108 MOUNT_NFS, /* 2 */ 109 MOUNT_MFS, /* 3 */ 110 MOUNT_MSDOS, /* 4 */ 111 MOUNT_CD9660, /* 5 = MOUNT_ISOFS */ 112 MOUNT_FDESC, /* 6 */ 113 MOUNT_KERNFS, /* 7 */ 114 NULL, /* 8 = MOUNT_DEVFS */ 115 MOUNT_AFS, /* 9 */ 116 }; 117 const int nmountcompatnames = sizeof(mountcompatnames) / 118 sizeof(mountcompatnames[0]); 119 #endif /* COMPAT_09 || COMPAT_43 */ 120 121 /* ARGSUSED */ 122 int 123 sys_mount(l, v, retval) 124 struct lwp *l; 125 void *v; 126 register_t *retval; 127 { 128 struct sys_mount_args /* { 129 syscallarg(const char *) type; 130 syscallarg(const char *) path; 131 syscallarg(int) flags; 132 syscallarg(void *) data; 133 } */ *uap = v; 134 struct proc *p = l->l_proc; 135 struct vnode *vp; 136 struct mount *mp; 137 int error, flag = 0; 138 char fstypename[MFSNAMELEN]; 139 struct vattr va; 140 struct nameidata nd; 141 struct vfsops *vfs; 142 143 /* 144 * if MNT_GETARGS is specified, it should be only flag. 145 */ 146 147 if ((SCARG(uap, flags) & MNT_GETARGS) != 0 && 148 (SCARG(uap, flags) & ~MNT_GETARGS) != 0) { 149 return EINVAL; 150 } 151 152 if (dovfsusermount == 0 && (SCARG(uap, flags) & MNT_GETARGS) == 0 && 153 (error = suser(p->p_ucred, &p->p_acflag))) 154 return (error); 155 /* 156 * Get vnode to be covered 157 */ 158 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, 159 SCARG(uap, path), p); 160 if ((error = namei(&nd)) != 0) 161 return (error); 162 vp = nd.ni_vp; 163 /* 164 * A lookup in VFS_MOUNT might result in an attempt to 165 * lock this vnode again, so make the lock recursive. 166 */ 167 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_SETRECURSE); 168 if (SCARG(uap, flags) & (MNT_UPDATE | MNT_GETARGS)) { 169 if ((vp->v_flag & VROOT) == 0) { 170 vput(vp); 171 return (EINVAL); 172 } 173 mp = vp->v_mount; 174 flag = mp->mnt_flag; 175 vfs = mp->mnt_op; 176 /* 177 * We only allow the filesystem to be reloaded if it 178 * is currently mounted read-only. 179 */ 180 if ((SCARG(uap, flags) & MNT_RELOAD) && 181 ((mp->mnt_flag & MNT_RDONLY) == 0)) { 182 vput(vp); 183 return (EOPNOTSUPP); /* Needs translation */ 184 } 185 /* 186 * In "highly secure" mode, don't let the caller do anything 187 * but downgrade a filesystem from read-write to read-only. 188 * (see also below; MNT_UPDATE or MNT_GETARGS is required.) 189 */ 190 if (securelevel >= 2 && 191 SCARG(uap, flags) != MNT_GETARGS && 192 SCARG(uap, flags) != 193 (mp->mnt_flag | MNT_RDONLY | 194 MNT_RELOAD | MNT_FORCE | MNT_UPDATE)) { 195 vput(vp); 196 return (EPERM); 197 } 198 mp->mnt_flag |= SCARG(uap, flags) & 199 (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_GETARGS); 200 /* 201 * Only root, or the user that did the original mount is 202 * permitted to update it. 203 */ 204 if ((mp->mnt_flag & MNT_GETARGS) == 0 && 205 mp->mnt_stat.f_owner != p->p_ucred->cr_uid && 206 (error = suser(p->p_ucred, &p->p_acflag)) != 0) { 207 vput(vp); 208 return (error); 209 } 210 /* 211 * Do not allow NFS export by non-root users. For non-root 212 * users, silently enforce MNT_NOSUID and MNT_NODEV, and 213 * MNT_NOEXEC if mount point is already MNT_NOEXEC. 214 */ 215 if (p->p_ucred->cr_uid != 0) { 216 if (SCARG(uap, flags) & MNT_EXPORTED) { 217 vput(vp); 218 return (EPERM); 219 } 220 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV; 221 if (flag & MNT_NOEXEC) 222 SCARG(uap, flags) |= MNT_NOEXEC; 223 } 224 if (vfs_busy(mp, LK_NOWAIT, 0)) { 225 vput(vp); 226 return (EPERM); 227 } 228 goto update; 229 } else { 230 if (securelevel >= 2) { 231 vput(vp); 232 return (EPERM); 233 } 234 } 235 /* 236 * If the user is not root, ensure that they own the directory 237 * onto which we are attempting to mount. 238 */ 239 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) != 0 || 240 (va.va_uid != p->p_ucred->cr_uid && 241 (error = suser(p->p_ucred, &p->p_acflag)) != 0)) { 242 vput(vp); 243 return (error); 244 } 245 /* 246 * Do not allow NFS export by non-root users. For non-root users, 247 * silently enforce MNT_NOSUID and MNT_NODEV, and MNT_NOEXEC if the 248 * mount point is already MNT_NOEXEC. 249 */ 250 if (p->p_ucred->cr_uid != 0) { 251 if (SCARG(uap, flags) & MNT_EXPORTED) { 252 vput(vp); 253 return (EPERM); 254 } 255 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV; 256 if (vp->v_mount->mnt_flag & MNT_NOEXEC) 257 SCARG(uap, flags) |= MNT_NOEXEC; 258 } 259 if ((error = vinvalbuf(vp, V_SAVE, p->p_ucred, p, 0, 0)) != 0) { 260 vput(vp); 261 return (error); 262 } 263 if (vp->v_type != VDIR) { 264 vput(vp); 265 return (ENOTDIR); 266 } 267 error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL); 268 if (error) { 269 #if defined(COMPAT_09) || defined(COMPAT_43) 270 /* 271 * Historically filesystem types were identified by number. 272 * If we get an integer for the filesystem type instead of a 273 * string, we check to see if it matches one of the historic 274 * filesystem types. 275 */ 276 u_long fsindex = (u_long)SCARG(uap, type); 277 if (fsindex >= nmountcompatnames || 278 mountcompatnames[fsindex] == NULL) { 279 vput(vp); 280 return (ENODEV); 281 } 282 strncpy(fstypename, mountcompatnames[fsindex], MFSNAMELEN); 283 #else 284 vput(vp); 285 return (error); 286 #endif 287 } 288 #ifdef COMPAT_10 289 /* Accept `ufs' as an alias for `ffs'. */ 290 if (!strncmp(fstypename, "ufs", MFSNAMELEN)) 291 strncpy(fstypename, "ffs", MFSNAMELEN); 292 #endif 293 if ((vfs = vfs_getopsbyname(fstypename)) == NULL) { 294 vput(vp); 295 return (ENODEV); 296 } 297 if (vp->v_mountedhere != NULL) { 298 vput(vp); 299 return (EBUSY); 300 } 301 302 /* 303 * Allocate and initialize the file system. 304 */ 305 mp = (struct mount *)malloc((u_long)sizeof(struct mount), 306 M_MOUNT, M_WAITOK); 307 memset((char *)mp, 0, (u_long)sizeof(struct mount)); 308 lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0); 309 simple_lock_init(&mp->mnt_slock); 310 (void)vfs_busy(mp, LK_NOWAIT, 0); 311 mp->mnt_op = vfs; 312 vfs->vfs_refcount++; 313 mp->mnt_vnodecovered = vp; 314 mp->mnt_stat.f_owner = p->p_ucred->cr_uid; 315 mp->mnt_unmounter = NULL; 316 mp->mnt_leaf = mp; 317 318 /* 319 * The underlying file system may refuse the mount for 320 * various reasons. Allow the user to force it to happen. 321 */ 322 mp->mnt_flag |= SCARG(uap, flags) & MNT_FORCE; 323 update: 324 if ((SCARG(uap, flags) & MNT_GETARGS) == 0) { 325 /* 326 * Set the mount level flags. 327 */ 328 if (SCARG(uap, flags) & MNT_RDONLY) 329 mp->mnt_flag |= MNT_RDONLY; 330 else if (mp->mnt_flag & MNT_RDONLY) 331 mp->mnt_iflag |= IMNT_WANTRDWR; 332 mp->mnt_flag &= 333 ~(MNT_NOSUID | MNT_NOEXEC | MNT_NODEV | 334 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP | 335 MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM | MNT_SOFTDEP); 336 mp->mnt_flag |= SCARG(uap, flags) & 337 (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV | 338 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP | 339 MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM | MNT_SOFTDEP | 340 MNT_IGNORE); 341 } 342 /* 343 * Mount the filesystem. 344 */ 345 error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, p); 346 if (mp->mnt_flag & (MNT_UPDATE | MNT_GETARGS)) { 347 if (mp->mnt_iflag & IMNT_WANTRDWR) 348 mp->mnt_flag &= ~MNT_RDONLY; 349 if (error) 350 mp->mnt_flag = flag; 351 mp->mnt_flag &=~ 352 (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_GETARGS); 353 mp->mnt_iflag &=~ IMNT_WANTRDWR; 354 if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0) { 355 if (mp->mnt_syncer == NULL) 356 error = vfs_allocate_syncvnode(mp); 357 } else { 358 if (mp->mnt_syncer != NULL) 359 vfs_deallocate_syncvnode(mp); 360 } 361 vfs_unbusy(mp); 362 VOP_UNLOCK(vp, 0); 363 vrele(vp); 364 return (error); 365 } 366 /* 367 * Put the new filesystem on the mount list after root. 368 */ 369 cache_purge(vp); 370 if (!error) { 371 mp->mnt_flag &=~ 372 (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_GETARGS); 373 mp->mnt_iflag &=~ IMNT_WANTRDWR; 374 vp->v_mountedhere = mp; 375 simple_lock(&mountlist_slock); 376 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list); 377 simple_unlock(&mountlist_slock); 378 checkdirs(vp); 379 VOP_UNLOCK(vp, 0); 380 if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0) 381 error = vfs_allocate_syncvnode(mp); 382 vfs_unbusy(mp); 383 (void) VFS_STATVFS(mp, &mp->mnt_stat, p); 384 if ((error = VFS_START(mp, 0, p))) 385 vrele(vp); 386 } else { 387 vp->v_mountedhere = (struct mount *)0; 388 vfs->vfs_refcount--; 389 vfs_unbusy(mp); 390 free(mp, M_MOUNT); 391 vput(vp); 392 } 393 return (error); 394 } 395 396 /* 397 * Scan all active processes to see if any of them have a current 398 * or root directory onto which the new filesystem has just been 399 * mounted. If so, replace them with the new mount point. 400 */ 401 void 402 checkdirs(olddp) 403 struct vnode *olddp; 404 { 405 struct cwdinfo *cwdi; 406 struct vnode *newdp; 407 struct proc *p; 408 409 if (olddp->v_usecount == 1) 410 return; 411 if (VFS_ROOT(olddp->v_mountedhere, &newdp)) 412 panic("mount: lost mount"); 413 proclist_lock_read(); 414 PROCLIST_FOREACH(p, &allproc) { 415 cwdi = p->p_cwdi; 416 if (!cwdi) 417 continue; 418 if (cwdi->cwdi_cdir == olddp) { 419 vrele(cwdi->cwdi_cdir); 420 VREF(newdp); 421 cwdi->cwdi_cdir = newdp; 422 } 423 if (cwdi->cwdi_rdir == olddp) { 424 vrele(cwdi->cwdi_rdir); 425 VREF(newdp); 426 cwdi->cwdi_rdir = newdp; 427 } 428 } 429 proclist_unlock_read(); 430 if (rootvnode == olddp) { 431 vrele(rootvnode); 432 VREF(newdp); 433 rootvnode = newdp; 434 } 435 vput(newdp); 436 } 437 438 /* 439 * Unmount a file system. 440 * 441 * Note: unmount takes a path to the vnode mounted on as argument, 442 * not special file (as before). 443 */ 444 /* ARGSUSED */ 445 int 446 sys_unmount(l, v, retval) 447 struct lwp *l; 448 void *v; 449 register_t *retval; 450 { 451 struct sys_unmount_args /* { 452 syscallarg(const char *) path; 453 syscallarg(int) flags; 454 } */ *uap = v; 455 struct proc *p = l->l_proc; 456 struct vnode *vp; 457 struct mount *mp; 458 int error; 459 struct nameidata nd; 460 461 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, 462 SCARG(uap, path), p); 463 if ((error = namei(&nd)) != 0) 464 return (error); 465 vp = nd.ni_vp; 466 mp = vp->v_mount; 467 468 /* 469 * Only root, or the user that did the original mount is 470 * permitted to unmount this filesystem. 471 */ 472 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) && 473 (error = suser(p->p_ucred, &p->p_acflag)) != 0) { 474 vput(vp); 475 return (error); 476 } 477 478 /* 479 * Don't allow unmounting the root file system. 480 */ 481 if (mp->mnt_flag & MNT_ROOTFS) { 482 vput(vp); 483 return (EINVAL); 484 } 485 486 /* 487 * Must be the root of the filesystem 488 */ 489 if ((vp->v_flag & VROOT) == 0) { 490 vput(vp); 491 return (EINVAL); 492 } 493 vput(vp); 494 495 /* 496 * XXX Freeze syncer. Must do this before locking the 497 * mount point. See dounmount() for details. 498 */ 499 lockmgr(&syncer_lock, LK_EXCLUSIVE, NULL); 500 501 if (vfs_busy(mp, 0, 0)) { 502 lockmgr(&syncer_lock, LK_RELEASE, NULL); 503 return (EBUSY); 504 } 505 506 return (dounmount(mp, SCARG(uap, flags), p)); 507 } 508 509 /* 510 * Do the actual file system unmount. File system is assumed to have been 511 * marked busy by the caller. 512 */ 513 int 514 dounmount(mp, flags, p) 515 struct mount *mp; 516 int flags; 517 struct proc *p; 518 { 519 struct vnode *coveredvp; 520 int error; 521 int async; 522 int used_syncer; 523 524 simple_lock(&mountlist_slock); 525 vfs_unbusy(mp); 526 used_syncer = (mp->mnt_syncer != NULL); 527 528 /* 529 * XXX Syncer must be frozen when we get here. This should really 530 * be done on a per-mountpoint basis, but especially the softdep 531 * code possibly called from the syncer doens't exactly work on a 532 * per-mountpoint basis, so the softdep code would become a maze 533 * of vfs_busy() calls. 534 * 535 * The caller of dounmount() must acquire syncer_lock because 536 * the syncer itself acquires locks in syncer_lock -> vfs_busy 537 * order, and we must preserve that order to avoid deadlock. 538 * 539 * So, if the file system did not use the syncer, now is 540 * the time to release the syncer_lock. 541 */ 542 if (used_syncer == 0) 543 lockmgr(&syncer_lock, LK_RELEASE, NULL); 544 545 mp->mnt_iflag |= IMNT_UNMOUNT; 546 mp->mnt_unmounter = p; 547 lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK, &mountlist_slock); 548 vn_start_write(NULL, &mp, V_WAIT); 549 550 if (mp->mnt_flag & MNT_EXPUBLIC) 551 vfs_setpublicfs(NULL, NULL, NULL); 552 async = mp->mnt_flag & MNT_ASYNC; 553 mp->mnt_flag &= ~MNT_ASYNC; 554 cache_purgevfs(mp); /* remove cache entries for this file sys */ 555 if (mp->mnt_syncer != NULL) 556 vfs_deallocate_syncvnode(mp); 557 error = 0; 558 if ((mp->mnt_flag & MNT_RDONLY) == 0) { 559 #if NFSS > 0 560 error = fss_umount_hook(mp, (flags & MNT_FORCE)); 561 #endif 562 if (error == 0) 563 error = VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p); 564 } 565 if (error == 0 || (flags & MNT_FORCE)) 566 error = VFS_UNMOUNT(mp, flags, p); 567 vn_finished_write(mp, 0); 568 simple_lock(&mountlist_slock); 569 if (error) { 570 if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0) 571 (void) vfs_allocate_syncvnode(mp); 572 mp->mnt_iflag &= ~IMNT_UNMOUNT; 573 mp->mnt_unmounter = NULL; 574 mp->mnt_flag |= async; 575 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE, 576 &mountlist_slock); 577 if (used_syncer) 578 lockmgr(&syncer_lock, LK_RELEASE, NULL); 579 simple_lock(&mp->mnt_slock); 580 while (mp->mnt_wcnt > 0) { 581 wakeup(mp); 582 ltsleep(&mp->mnt_wcnt, PVFS, "mntwcnt1", 583 0, &mp->mnt_slock); 584 } 585 simple_unlock(&mp->mnt_slock); 586 return (error); 587 } 588 CIRCLEQ_REMOVE(&mountlist, mp, mnt_list); 589 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) { 590 coveredvp->v_mountedhere = NULL; 591 vrele(coveredvp); 592 } 593 mp->mnt_op->vfs_refcount--; 594 if (LIST_FIRST(&mp->mnt_vnodelist) != NULL) 595 panic("unmount: dangling vnode"); 596 mp->mnt_iflag |= IMNT_GONE; 597 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &mountlist_slock); 598 if (used_syncer) 599 lockmgr(&syncer_lock, LK_RELEASE, NULL); 600 simple_lock(&mp->mnt_slock); 601 while (mp->mnt_wcnt > 0) { 602 wakeup(mp); 603 ltsleep(&mp->mnt_wcnt, PVFS, "mntwcnt2", 0, &mp->mnt_slock); 604 } 605 simple_unlock(&mp->mnt_slock); 606 free(mp, M_MOUNT); 607 return (0); 608 } 609 610 /* 611 * Sync each mounted filesystem. 612 */ 613 #ifdef DEBUG 614 int syncprt = 0; 615 struct ctldebug debug0 = { "syncprt", &syncprt }; 616 #endif 617 618 /* ARGSUSED */ 619 int 620 sys_sync(l, v, retval) 621 struct lwp *l; 622 void *v; 623 register_t *retval; 624 { 625 struct mount *mp, *nmp; 626 int asyncflag; 627 struct proc *p = l == NULL ? &proc0 : l->l_proc; 628 629 simple_lock(&mountlist_slock); 630 for (mp = mountlist.cqh_last; mp != (void *)&mountlist; mp = nmp) { 631 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) { 632 nmp = mp->mnt_list.cqe_prev; 633 continue; 634 } 635 if ((mp->mnt_flag & MNT_RDONLY) == 0 && 636 vn_start_write(NULL, &mp, V_NOWAIT) == 0) { 637 asyncflag = mp->mnt_flag & MNT_ASYNC; 638 mp->mnt_flag &= ~MNT_ASYNC; 639 VFS_SYNC(mp, MNT_NOWAIT, p->p_ucred, p); 640 if (asyncflag) 641 mp->mnt_flag |= MNT_ASYNC; 642 vn_finished_write(mp, 0); 643 } 644 simple_lock(&mountlist_slock); 645 nmp = mp->mnt_list.cqe_prev; 646 vfs_unbusy(mp); 647 648 } 649 simple_unlock(&mountlist_slock); 650 #ifdef DEBUG 651 if (syncprt) 652 vfs_bufstats(); 653 #endif /* DEBUG */ 654 return (0); 655 } 656 657 /* 658 * Change filesystem quotas. 659 */ 660 /* ARGSUSED */ 661 int 662 sys_quotactl(l, v, retval) 663 struct lwp *l; 664 void *v; 665 register_t *retval; 666 { 667 struct sys_quotactl_args /* { 668 syscallarg(const char *) path; 669 syscallarg(int) cmd; 670 syscallarg(int) uid; 671 syscallarg(caddr_t) arg; 672 } */ *uap = v; 673 struct proc *p = l->l_proc; 674 struct mount *mp; 675 int error; 676 struct nameidata nd; 677 678 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p); 679 if ((error = namei(&nd)) != 0) 680 return (error); 681 error = vn_start_write(nd.ni_vp, &mp, V_WAIT | V_PCATCH); 682 vrele(nd.ni_vp); 683 if (error) 684 return (error); 685 error = VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid), 686 SCARG(uap, arg), p); 687 vn_finished_write(mp, 0); 688 return (error); 689 } 690 691 int 692 dostatvfs(struct mount *mp, struct statvfs *sp, struct proc *p, int flags, 693 int root) 694 { 695 struct cwdinfo *cwdi = p->p_cwdi; 696 int error = 0; 697 698 /* 699 * If MNT_NOWAIT or MNT_LAZY is specified, do not 700 * refresh the fsstat cache. MNT_WAIT or MNT_LAZY 701 * overrides MNT_NOWAIT. 702 */ 703 if (flags == MNT_NOWAIT || flags == MNT_LAZY || 704 (flags != MNT_WAIT && flags != 0)) { 705 memcpy(sp, &mp->mnt_stat, sizeof(*sp)); 706 goto done; 707 } 708 709 /* Get the filesystem stats now */ 710 memset(sp, 0, sizeof(*sp)); 711 if ((error = VFS_STATVFS(mp, sp, p)) != 0) { 712 return error; 713 } 714 715 if (cwdi->cwdi_rdir == NULL) 716 (void)memcpy(&mp->mnt_stat, sp, sizeof(mp->mnt_stat)); 717 done: 718 if (cwdi->cwdi_rdir != NULL) { 719 size_t len; 720 char *bp; 721 char *path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 722 if (!path) 723 return ENOMEM; 724 725 bp = path + MAXPATHLEN; 726 *--bp = '\0'; 727 error = getcwd_common(cwdi->cwdi_rdir, rootvnode, &bp, path, 728 MAXPATHLEN / 2, 0, p); 729 if (error) { 730 free(path, M_TEMP); 731 return error; 732 } 733 len = strlen(bp); 734 /* 735 * for mount points that are below our root, we can see 736 * them, so we fix up the pathname and return them. The 737 * rest we cannot see, so we don't allow viewing the 738 * data. 739 */ 740 if (strncmp(bp, sp->f_mntonname, len) == 0) { 741 strlcpy(sp->f_mntonname, &sp->f_mntonname[len], 742 sizeof(sp->f_mntonname)); 743 if (sp->f_mntonname[0] == '\0') 744 (void)strlcpy(sp->f_mntonname, "/", 745 sizeof(sp->f_mntonname)); 746 } else { 747 if (root) 748 (void)strlcpy(sp->f_mntonname, "/", 749 sizeof(sp->f_mntonname)); 750 else 751 error = EPERM; 752 } 753 free(path, M_TEMP); 754 } 755 sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK; 756 return error; 757 } 758 759 /* 760 * Get filesystem statistics. 761 */ 762 /* ARGSUSED */ 763 int 764 sys_statvfs1(l, v, retval) 765 struct lwp *l; 766 void *v; 767 register_t *retval; 768 { 769 struct sys_statvfs1_args /* { 770 syscallarg(const char *) path; 771 syscallarg(struct statvfs *) buf; 772 syscallarg(int) flags; 773 } */ *uap = v; 774 struct proc *p = l->l_proc; 775 struct mount *mp; 776 struct statvfs sbuf; 777 int error; 778 struct nameidata nd; 779 780 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p); 781 if ((error = namei(&nd)) != 0) 782 return error; 783 mp = nd.ni_vp->v_mount; 784 vrele(nd.ni_vp); 785 if ((error = dostatvfs(mp, &sbuf, p, SCARG(uap, flags), 1)) != 0) 786 return error; 787 return copyout(&sbuf, SCARG(uap, buf), sizeof(sbuf)); 788 } 789 790 /* 791 * Get filesystem statistics. 792 */ 793 /* ARGSUSED */ 794 int 795 sys_fstatvfs1(l, v, retval) 796 struct lwp *l; 797 void *v; 798 register_t *retval; 799 { 800 struct sys_fstatvfs1_args /* { 801 syscallarg(int) fd; 802 syscallarg(struct statvfs *) buf; 803 syscallarg(int) flags; 804 } */ *uap = v; 805 struct proc *p = l->l_proc; 806 struct file *fp; 807 struct mount *mp; 808 struct statvfs sbuf; 809 int error; 810 811 /* getvnode() will use the descriptor for us */ 812 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) 813 return (error); 814 mp = ((struct vnode *)fp->f_data)->v_mount; 815 if ((error = dostatvfs(mp, &sbuf, p, SCARG(uap, flags), 1)) != 0) 816 goto out; 817 error = copyout(&sbuf, SCARG(uap, buf), sizeof(sbuf)); 818 out: 819 FILE_UNUSE(fp, p); 820 return error; 821 } 822 823 824 /* 825 * Get statistics on all filesystems. 826 */ 827 int 828 sys_getvfsstat(l, v, retval) 829 struct lwp *l; 830 void *v; 831 register_t *retval; 832 { 833 struct sys_getvfsstat_args /* { 834 syscallarg(struct statvfs *) buf; 835 syscallarg(size_t) bufsize; 836 syscallarg(int) flags; 837 } */ *uap = v; 838 int root = 0; 839 struct proc *p = l->l_proc; 840 struct mount *mp, *nmp; 841 struct statvfs sbuf; 842 struct statvfs *sfsp; 843 size_t count, maxcount; 844 int error = 0; 845 846 maxcount = SCARG(uap, bufsize) / sizeof(struct statvfs); 847 sfsp = SCARG(uap, buf); 848 simple_lock(&mountlist_slock); 849 count = 0; 850 for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist; 851 mp = nmp) { 852 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) { 853 nmp = CIRCLEQ_NEXT(mp, mnt_list); 854 continue; 855 } 856 if (sfsp && count < maxcount) { 857 error = dostatvfs(mp, &sbuf, p, SCARG(uap, flags), 0); 858 if (error) { 859 simple_lock(&mountlist_slock); 860 nmp = CIRCLEQ_NEXT(mp, mnt_list); 861 vfs_unbusy(mp); 862 continue; 863 } 864 error = copyout(&sbuf, sfsp, sizeof(*sfsp)); 865 if (error) { 866 vfs_unbusy(mp); 867 return (error); 868 } 869 sfsp++; 870 root |= strcmp(sbuf.f_mntonname, "/") == 0; 871 } 872 count++; 873 simple_lock(&mountlist_slock); 874 nmp = CIRCLEQ_NEXT(mp, mnt_list); 875 vfs_unbusy(mp); 876 } 877 simple_unlock(&mountlist_slock); 878 if (root == 0 && p->p_cwdi->cwdi_rdir) { 879 /* 880 * fake a root entry 881 */ 882 if ((error = dostatvfs(p->p_cwdi->cwdi_rdir->v_mount, &sbuf, p, 883 SCARG(uap, flags), 1)) != 0) 884 return error; 885 if (sfsp) 886 error = copyout(&sbuf, sfsp, sizeof(*sfsp)); 887 count++; 888 } 889 if (sfsp && count > maxcount) 890 *retval = maxcount; 891 else 892 *retval = count; 893 return error; 894 } 895 896 /* 897 * Change current working directory to a given file descriptor. 898 */ 899 /* ARGSUSED */ 900 int 901 sys_fchdir(l, v, retval) 902 struct lwp *l; 903 void *v; 904 register_t *retval; 905 { 906 struct sys_fchdir_args /* { 907 syscallarg(int) fd; 908 } */ *uap = v; 909 struct proc *p = l->l_proc; 910 struct filedesc *fdp = p->p_fd; 911 struct cwdinfo *cwdi = p->p_cwdi; 912 struct vnode *vp, *tdp; 913 struct mount *mp; 914 struct file *fp; 915 int error; 916 917 /* getvnode() will use the descriptor for us */ 918 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0) 919 return (error); 920 vp = (struct vnode *)fp->f_data; 921 922 VREF(vp); 923 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 924 if (vp->v_type != VDIR) 925 error = ENOTDIR; 926 else 927 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p); 928 while (!error && (mp = vp->v_mountedhere) != NULL) { 929 if (vfs_busy(mp, 0, 0)) 930 continue; 931 error = VFS_ROOT(mp, &tdp); 932 vfs_unbusy(mp); 933 if (error) 934 break; 935 vput(vp); 936 vp = tdp; 937 } 938 if (error) { 939 vput(vp); 940 goto out; 941 } 942 VOP_UNLOCK(vp, 0); 943 944 /* 945 * Disallow changing to a directory not under the process's 946 * current root directory (if there is one). 947 */ 948 if (cwdi->cwdi_rdir && !vn_isunder(vp, NULL, p)) { 949 vrele(vp); 950 error = EPERM; /* operation not permitted */ 951 goto out; 952 } 953 954 vrele(cwdi->cwdi_cdir); 955 cwdi->cwdi_cdir = vp; 956 out: 957 FILE_UNUSE(fp, p); 958 return (error); 959 } 960 961 /* 962 * Change this process's notion of the root directory to a given file descriptor. 963 */ 964 965 int 966 sys_fchroot(l, v, retval) 967 struct lwp *l; 968 void *v; 969 register_t *retval; 970 { 971 struct sys_fchroot_args *uap = v; 972 struct proc *p = l->l_proc; 973 struct filedesc *fdp = p->p_fd; 974 struct cwdinfo *cwdi = p->p_cwdi; 975 struct vnode *vp; 976 struct file *fp; 977 int error; 978 979 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 980 return error; 981 /* getvnode() will use the descriptor for us */ 982 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0) 983 return error; 984 vp = (struct vnode *) fp->f_data; 985 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 986 if (vp->v_type != VDIR) 987 error = ENOTDIR; 988 else 989 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p); 990 VOP_UNLOCK(vp, 0); 991 if (error) 992 goto out; 993 VREF(vp); 994 995 /* 996 * Prevent escaping from chroot by putting the root under 997 * the working directory. Silently chdir to / if we aren't 998 * already there. 999 */ 1000 if (!vn_isunder(cwdi->cwdi_cdir, vp, p)) { 1001 /* 1002 * XXX would be more failsafe to change directory to a 1003 * deadfs node here instead 1004 */ 1005 vrele(cwdi->cwdi_cdir); 1006 VREF(vp); 1007 cwdi->cwdi_cdir = vp; 1008 } 1009 1010 if (cwdi->cwdi_rdir != NULL) 1011 vrele(cwdi->cwdi_rdir); 1012 cwdi->cwdi_rdir = vp; 1013 out: 1014 FILE_UNUSE(fp, p); 1015 return (error); 1016 } 1017 1018 1019 1020 /* 1021 * Change current working directory (``.''). 1022 */ 1023 /* ARGSUSED */ 1024 int 1025 sys_chdir(l, v, retval) 1026 struct lwp *l; 1027 void *v; 1028 register_t *retval; 1029 { 1030 struct sys_chdir_args /* { 1031 syscallarg(const char *) path; 1032 } */ *uap = v; 1033 struct proc *p = l->l_proc; 1034 struct cwdinfo *cwdi = p->p_cwdi; 1035 int error; 1036 struct nameidata nd; 1037 1038 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, 1039 SCARG(uap, path), p); 1040 if ((error = change_dir(&nd, p)) != 0) 1041 return (error); 1042 vrele(cwdi->cwdi_cdir); 1043 cwdi->cwdi_cdir = nd.ni_vp; 1044 return (0); 1045 } 1046 1047 /* 1048 * Change notion of root (``/'') directory. 1049 */ 1050 /* ARGSUSED */ 1051 int 1052 sys_chroot(l, v, retval) 1053 struct lwp *l; 1054 void *v; 1055 register_t *retval; 1056 { 1057 struct sys_chroot_args /* { 1058 syscallarg(const char *) path; 1059 } */ *uap = v; 1060 struct proc *p = l->l_proc; 1061 struct cwdinfo *cwdi = p->p_cwdi; 1062 struct vnode *vp; 1063 int error; 1064 struct nameidata nd; 1065 1066 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 1067 return (error); 1068 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, 1069 SCARG(uap, path), p); 1070 if ((error = change_dir(&nd, p)) != 0) 1071 return (error); 1072 if (cwdi->cwdi_rdir != NULL) 1073 vrele(cwdi->cwdi_rdir); 1074 vp = nd.ni_vp; 1075 cwdi->cwdi_rdir = vp; 1076 1077 /* 1078 * Prevent escaping from chroot by putting the root under 1079 * the working directory. Silently chdir to / if we aren't 1080 * already there. 1081 */ 1082 if (!vn_isunder(cwdi->cwdi_cdir, vp, p)) { 1083 /* 1084 * XXX would be more failsafe to change directory to a 1085 * deadfs node here instead 1086 */ 1087 vrele(cwdi->cwdi_cdir); 1088 VREF(vp); 1089 cwdi->cwdi_cdir = vp; 1090 } 1091 1092 return (0); 1093 } 1094 1095 /* 1096 * Common routine for chroot and chdir. 1097 */ 1098 static int 1099 change_dir(ndp, p) 1100 struct nameidata *ndp; 1101 struct proc *p; 1102 { 1103 struct vnode *vp; 1104 int error; 1105 1106 if ((error = namei(ndp)) != 0) 1107 return (error); 1108 vp = ndp->ni_vp; 1109 if (vp->v_type != VDIR) 1110 error = ENOTDIR; 1111 else 1112 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p); 1113 1114 if (error) 1115 vput(vp); 1116 else 1117 VOP_UNLOCK(vp, 0); 1118 return (error); 1119 } 1120 1121 /* 1122 * Check permissions, allocate an open file structure, 1123 * and call the device open routine if any. 1124 */ 1125 int 1126 sys_open(l, v, retval) 1127 struct lwp *l; 1128 void *v; 1129 register_t *retval; 1130 { 1131 struct sys_open_args /* { 1132 syscallarg(const char *) path; 1133 syscallarg(int) flags; 1134 syscallarg(int) mode; 1135 } */ *uap = v; 1136 struct proc *p = l->l_proc; 1137 struct cwdinfo *cwdi = p->p_cwdi; 1138 struct filedesc *fdp = p->p_fd; 1139 struct file *fp; 1140 struct vnode *vp; 1141 int flags, cmode; 1142 int type, indx, error; 1143 struct flock lf; 1144 struct nameidata nd; 1145 1146 flags = FFLAGS(SCARG(uap, flags)); 1147 if ((flags & (FREAD | FWRITE)) == 0) 1148 return (EINVAL); 1149 /* falloc() will use the file descriptor for us */ 1150 if ((error = falloc(p, &fp, &indx)) != 0) 1151 return (error); 1152 cmode = ((SCARG(uap, mode) &~ cwdi->cwdi_cmask) & ALLPERMS) &~ S_ISTXT; 1153 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p); 1154 l->l_dupfd = -indx - 1; /* XXX check for fdopen */ 1155 if ((error = vn_open(&nd, flags, cmode)) != 0) { 1156 FILE_UNUSE(fp, p); 1157 fdp->fd_ofiles[indx] = NULL; 1158 ffree(fp); 1159 if ((error == EDUPFD || error == EMOVEFD) && 1160 l->l_dupfd >= 0 && /* XXX from fdopen */ 1161 (error = 1162 dupfdopen(p, indx, l->l_dupfd, flags, error)) == 0) { 1163 *retval = indx; 1164 return (0); 1165 } 1166 if (error == ERESTART) 1167 error = EINTR; 1168 fdremove(fdp, indx); 1169 return (error); 1170 } 1171 l->l_dupfd = 0; 1172 vp = nd.ni_vp; 1173 fp->f_flag = flags & FMASK; 1174 fp->f_type = DTYPE_VNODE; 1175 fp->f_ops = &vnops; 1176 fp->f_data = vp; 1177 if (flags & (O_EXLOCK | O_SHLOCK)) { 1178 lf.l_whence = SEEK_SET; 1179 lf.l_start = 0; 1180 lf.l_len = 0; 1181 if (flags & O_EXLOCK) 1182 lf.l_type = F_WRLCK; 1183 else 1184 lf.l_type = F_RDLCK; 1185 type = F_FLOCK; 1186 if ((flags & FNONBLOCK) == 0) 1187 type |= F_WAIT; 1188 VOP_UNLOCK(vp, 0); 1189 error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, type); 1190 if (error) { 1191 (void) vn_close(vp, fp->f_flag, fp->f_cred, p); 1192 FILE_UNUSE(fp, p); 1193 ffree(fp); 1194 fdremove(fdp, indx); 1195 return (error); 1196 } 1197 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1198 fp->f_flag |= FHASLOCK; 1199 } 1200 VOP_UNLOCK(vp, 0); 1201 *retval = indx; 1202 FILE_SET_MATURE(fp); 1203 FILE_UNUSE(fp, p); 1204 return (0); 1205 } 1206 1207 /* 1208 * Get file handle system call 1209 */ 1210 int 1211 sys_getfh(l, v, retval) 1212 struct lwp *l; 1213 void *v; 1214 register_t *retval; 1215 { 1216 struct sys_getfh_args /* { 1217 syscallarg(char *) fname; 1218 syscallarg(fhandle_t *) fhp; 1219 } */ *uap = v; 1220 struct proc *p = l->l_proc; 1221 struct vnode *vp; 1222 fhandle_t fh; 1223 int error; 1224 struct nameidata nd; 1225 1226 /* 1227 * Must be super user 1228 */ 1229 error = suser(p->p_ucred, &p->p_acflag); 1230 if (error) 1231 return (error); 1232 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, 1233 SCARG(uap, fname), p); 1234 error = namei(&nd); 1235 if (error) 1236 return (error); 1237 vp = nd.ni_vp; 1238 memset(&fh, 0, sizeof(fh)); 1239 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsidx; 1240 error = VFS_VPTOFH(vp, &fh.fh_fid); 1241 vput(vp); 1242 if (error) 1243 return (error); 1244 error = copyout(&fh, (caddr_t)SCARG(uap, fhp), sizeof (fh)); 1245 return (error); 1246 } 1247 1248 /* 1249 * Open a file given a file handle. 1250 * 1251 * Check permissions, allocate an open file structure, 1252 * and call the device open routine if any. 1253 */ 1254 int 1255 sys_fhopen(l, v, retval) 1256 struct lwp *l; 1257 void *v; 1258 register_t *retval; 1259 { 1260 struct sys_fhopen_args /* { 1261 syscallarg(const fhandle_t *) fhp; 1262 syscallarg(int) flags; 1263 } */ *uap = v; 1264 struct proc *p = l->l_proc; 1265 struct filedesc *fdp = p->p_fd; 1266 struct file *fp; 1267 struct vnode *vp = NULL; 1268 struct mount *mp; 1269 struct ucred *cred = p->p_ucred; 1270 int flags; 1271 struct file *nfp; 1272 int type, indx, error=0; 1273 struct flock lf; 1274 struct vattr va; 1275 fhandle_t fh; 1276 1277 /* 1278 * Must be super user 1279 */ 1280 if ((error = suser(p->p_ucred, &p->p_acflag))) 1281 return (error); 1282 1283 flags = FFLAGS(SCARG(uap, flags)); 1284 if ((flags & (FREAD | FWRITE)) == 0) 1285 return (EINVAL); 1286 if ((flags & O_CREAT)) 1287 return (EINVAL); 1288 /* falloc() will use the file descriptor for us */ 1289 if ((error = falloc(p, &nfp, &indx)) != 0) 1290 return (error); 1291 fp = nfp; 1292 if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0) 1293 goto bad; 1294 1295 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) { 1296 error = ESTALE; 1297 goto bad; 1298 } 1299 1300 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)) != 0) { 1301 vp = NULL; /* most likely unnecessary sanity for bad: */ 1302 goto bad; 1303 } 1304 1305 /* Now do an effective vn_open */ 1306 1307 if (vp->v_type == VSOCK) { 1308 error = EOPNOTSUPP; 1309 goto bad; 1310 } 1311 if (flags & FREAD) { 1312 if ((error = VOP_ACCESS(vp, VREAD, cred, p)) != 0) 1313 goto bad; 1314 } 1315 if (flags & (FWRITE | O_TRUNC)) { 1316 if (vp->v_type == VDIR) { 1317 error = EISDIR; 1318 goto bad; 1319 } 1320 if ((error = vn_writechk(vp)) != 0 || 1321 (error = VOP_ACCESS(vp, VWRITE, cred, p)) != 0) 1322 goto bad; 1323 } 1324 if (flags & O_TRUNC) { 1325 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) 1326 goto bad; 1327 VOP_UNLOCK(vp, 0); /* XXX */ 1328 VOP_LEASE(vp, p, cred, LEASE_WRITE); 1329 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */ 1330 VATTR_NULL(&va); 1331 va.va_size = 0; 1332 error = VOP_SETATTR(vp, &va, cred, p); 1333 vn_finished_write(mp, 0); 1334 if (error) 1335 goto bad; 1336 } 1337 if ((error = VOP_OPEN(vp, flags, cred, p)) != 0) 1338 goto bad; 1339 if (vp->v_type == VREG && 1340 uvn_attach(vp, flags & FWRITE ? VM_PROT_WRITE : 0) == NULL) { 1341 error = EIO; 1342 goto bad; 1343 } 1344 if (flags & FWRITE) 1345 vp->v_writecount++; 1346 1347 /* done with modified vn_open, now finish what sys_open does. */ 1348 1349 fp->f_flag = flags & FMASK; 1350 fp->f_type = DTYPE_VNODE; 1351 fp->f_ops = &vnops; 1352 fp->f_data = vp; 1353 if (flags & (O_EXLOCK | O_SHLOCK)) { 1354 lf.l_whence = SEEK_SET; 1355 lf.l_start = 0; 1356 lf.l_len = 0; 1357 if (flags & O_EXLOCK) 1358 lf.l_type = F_WRLCK; 1359 else 1360 lf.l_type = F_RDLCK; 1361 type = F_FLOCK; 1362 if ((flags & FNONBLOCK) == 0) 1363 type |= F_WAIT; 1364 VOP_UNLOCK(vp, 0); 1365 error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, type); 1366 if (error) { 1367 (void) vn_close(vp, fp->f_flag, fp->f_cred, p); 1368 FILE_UNUSE(fp, p); 1369 ffree(fp); 1370 fdremove(fdp, indx); 1371 return (error); 1372 } 1373 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1374 fp->f_flag |= FHASLOCK; 1375 } 1376 VOP_UNLOCK(vp, 0); 1377 *retval = indx; 1378 FILE_SET_MATURE(fp); 1379 FILE_UNUSE(fp, p); 1380 return (0); 1381 1382 bad: 1383 FILE_UNUSE(fp, p); 1384 ffree(fp); 1385 fdremove(fdp, indx); 1386 if (vp != NULL) 1387 vput(vp); 1388 return (error); 1389 } 1390 1391 /* ARGSUSED */ 1392 int 1393 sys_fhstat(l, v, retval) 1394 struct lwp *l; 1395 void *v; 1396 register_t *retval; 1397 { 1398 struct sys_fhstat_args /* { 1399 syscallarg(const fhandle_t *) fhp; 1400 syscallarg(struct stat *) sb; 1401 } */ *uap = v; 1402 struct proc *p = l->l_proc; 1403 struct stat sb; 1404 int error; 1405 fhandle_t fh; 1406 struct mount *mp; 1407 struct vnode *vp; 1408 1409 /* 1410 * Must be super user 1411 */ 1412 if ((error = suser(p->p_ucred, &p->p_acflag))) 1413 return (error); 1414 1415 if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0) 1416 return (error); 1417 1418 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) 1419 return (ESTALE); 1420 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp))) 1421 return (error); 1422 error = vn_stat(vp, &sb, p); 1423 vput(vp); 1424 if (error) 1425 return (error); 1426 error = copyout(&sb, SCARG(uap, sb), sizeof(sb)); 1427 return (error); 1428 } 1429 1430 /* ARGSUSED */ 1431 int 1432 sys_fhstatvfs1(l, v, retval) 1433 struct lwp *l; 1434 void *v; 1435 register_t *retval; 1436 { 1437 struct sys_fhstatvfs1_args /* 1438 syscallarg(const fhandle_t *) fhp; 1439 syscallarg(struct statvfs *) buf; 1440 syscallarg(int) flags; 1441 } */ *uap = v; 1442 struct proc *p = l->l_proc; 1443 struct statvfs sbuf; 1444 fhandle_t fh; 1445 struct mount *mp; 1446 struct vnode *vp; 1447 int error; 1448 1449 /* 1450 * Must be super user 1451 */ 1452 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 1453 return error; 1454 1455 if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0) 1456 return error; 1457 1458 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) 1459 return ESTALE; 1460 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp))) 1461 return error; 1462 1463 mp = vp->v_mount; 1464 if ((error = dostatvfs(mp, &sbuf, p, SCARG(uap, flags), 1)) != 0) { 1465 vput(vp); 1466 return error; 1467 } 1468 vput(vp); 1469 return copyout(&sbuf, SCARG(uap, buf), sizeof(sbuf)); 1470 } 1471 1472 /* 1473 * Create a special file. 1474 */ 1475 /* ARGSUSED */ 1476 int 1477 sys_mknod(l, v, retval) 1478 struct lwp *l; 1479 void *v; 1480 register_t *retval; 1481 { 1482 struct sys_mknod_args /* { 1483 syscallarg(const char *) path; 1484 syscallarg(int) mode; 1485 syscallarg(int) dev; 1486 } */ *uap = v; 1487 struct proc *p = l->l_proc; 1488 struct vnode *vp; 1489 struct mount *mp; 1490 struct vattr vattr; 1491 int error; 1492 int whiteout = 0; 1493 struct nameidata nd; 1494 1495 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 1496 return (error); 1497 restart: 1498 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p); 1499 if ((error = namei(&nd)) != 0) 1500 return (error); 1501 vp = nd.ni_vp; 1502 if (vp != NULL) 1503 error = EEXIST; 1504 else { 1505 VATTR_NULL(&vattr); 1506 vattr.va_mode = 1507 (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask; 1508 vattr.va_rdev = SCARG(uap, dev); 1509 whiteout = 0; 1510 1511 switch (SCARG(uap, mode) & S_IFMT) { 1512 case S_IFMT: /* used by badsect to flag bad sectors */ 1513 vattr.va_type = VBAD; 1514 break; 1515 case S_IFCHR: 1516 vattr.va_type = VCHR; 1517 break; 1518 case S_IFBLK: 1519 vattr.va_type = VBLK; 1520 break; 1521 case S_IFWHT: 1522 whiteout = 1; 1523 break; 1524 default: 1525 error = EINVAL; 1526 break; 1527 } 1528 } 1529 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 1530 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1531 if (nd.ni_dvp == vp) 1532 vrele(nd.ni_dvp); 1533 else 1534 vput(nd.ni_dvp); 1535 if (vp) 1536 vrele(vp); 1537 if ((error = vn_start_write(NULL, &mp, 1538 V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0) 1539 return (error); 1540 goto restart; 1541 } 1542 if (!error) { 1543 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE); 1544 if (whiteout) { 1545 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE); 1546 if (error) 1547 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1548 vput(nd.ni_dvp); 1549 } else { 1550 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, 1551 &nd.ni_cnd, &vattr); 1552 if (error == 0) 1553 vput(nd.ni_vp); 1554 } 1555 } else { 1556 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1557 if (nd.ni_dvp == vp) 1558 vrele(nd.ni_dvp); 1559 else 1560 vput(nd.ni_dvp); 1561 if (vp) 1562 vrele(vp); 1563 } 1564 vn_finished_write(mp, 0); 1565 return (error); 1566 } 1567 1568 /* 1569 * Create a named pipe. 1570 */ 1571 /* ARGSUSED */ 1572 int 1573 sys_mkfifo(l, v, retval) 1574 struct lwp *l; 1575 void *v; 1576 register_t *retval; 1577 { 1578 struct sys_mkfifo_args /* { 1579 syscallarg(const char *) path; 1580 syscallarg(int) mode; 1581 } */ *uap = v; 1582 struct proc *p = l->l_proc; 1583 struct mount *mp; 1584 struct vattr vattr; 1585 int error; 1586 struct nameidata nd; 1587 1588 restart: 1589 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p); 1590 if ((error = namei(&nd)) != 0) 1591 return (error); 1592 if (nd.ni_vp != NULL) { 1593 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1594 if (nd.ni_dvp == nd.ni_vp) 1595 vrele(nd.ni_dvp); 1596 else 1597 vput(nd.ni_dvp); 1598 vrele(nd.ni_vp); 1599 return (EEXIST); 1600 } 1601 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 1602 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1603 if (nd.ni_dvp == nd.ni_vp) 1604 vrele(nd.ni_dvp); 1605 else 1606 vput(nd.ni_dvp); 1607 if (nd.ni_vp) 1608 vrele(nd.ni_vp); 1609 if ((error = vn_start_write(NULL, &mp, 1610 V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0) 1611 return (error); 1612 goto restart; 1613 } 1614 VATTR_NULL(&vattr); 1615 vattr.va_type = VFIFO; 1616 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask; 1617 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE); 1618 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 1619 if (error == 0) 1620 vput(nd.ni_vp); 1621 vn_finished_write(mp, 0); 1622 return (error); 1623 } 1624 1625 /* 1626 * Make a hard file link. 1627 */ 1628 /* ARGSUSED */ 1629 int 1630 sys_link(l, v, retval) 1631 struct lwp *l; 1632 void *v; 1633 register_t *retval; 1634 { 1635 struct sys_link_args /* { 1636 syscallarg(const char *) path; 1637 syscallarg(const char *) link; 1638 } */ *uap = v; 1639 struct proc *p = l->l_proc; 1640 struct vnode *vp; 1641 struct mount *mp; 1642 struct nameidata nd; 1643 int error; 1644 1645 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p); 1646 if ((error = namei(&nd)) != 0) 1647 return (error); 1648 vp = nd.ni_vp; 1649 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) { 1650 vrele(vp); 1651 return (error); 1652 } 1653 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p); 1654 if ((error = namei(&nd)) != 0) 1655 goto out; 1656 if (nd.ni_vp) { 1657 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1658 if (nd.ni_dvp == nd.ni_vp) 1659 vrele(nd.ni_dvp); 1660 else 1661 vput(nd.ni_dvp); 1662 vrele(nd.ni_vp); 1663 error = EEXIST; 1664 goto out; 1665 } 1666 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE); 1667 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE); 1668 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd); 1669 out: 1670 vrele(vp); 1671 vn_finished_write(mp, 0); 1672 return (error); 1673 } 1674 1675 /* 1676 * Make a symbolic link. 1677 */ 1678 /* ARGSUSED */ 1679 int 1680 sys_symlink(l, v, retval) 1681 struct lwp *l; 1682 void *v; 1683 register_t *retval; 1684 { 1685 struct sys_symlink_args /* { 1686 syscallarg(const char *) path; 1687 syscallarg(const char *) link; 1688 } */ *uap = v; 1689 struct proc *p = l->l_proc; 1690 struct mount *mp; 1691 struct vattr vattr; 1692 char *path; 1693 int error; 1694 struct nameidata nd; 1695 1696 path = PNBUF_GET(); 1697 error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL); 1698 if (error) 1699 goto out; 1700 restart: 1701 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p); 1702 if ((error = namei(&nd)) != 0) 1703 goto out; 1704 if (nd.ni_vp) { 1705 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1706 if (nd.ni_dvp == nd.ni_vp) 1707 vrele(nd.ni_dvp); 1708 else 1709 vput(nd.ni_dvp); 1710 vrele(nd.ni_vp); 1711 error = EEXIST; 1712 goto out; 1713 } 1714 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 1715 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1716 if (nd.ni_dvp == nd.ni_vp) 1717 vrele(nd.ni_dvp); 1718 else 1719 vput(nd.ni_dvp); 1720 if ((error = vn_start_write(NULL, &mp, 1721 V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0) 1722 return (error); 1723 goto restart; 1724 } 1725 VATTR_NULL(&vattr); 1726 vattr.va_mode = ACCESSPERMS &~ p->p_cwdi->cwdi_cmask; 1727 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE); 1728 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path); 1729 if (error == 0) 1730 vput(nd.ni_vp); 1731 vn_finished_write(mp, 0); 1732 out: 1733 PNBUF_PUT(path); 1734 return (error); 1735 } 1736 1737 /* 1738 * Delete a whiteout from the filesystem. 1739 */ 1740 /* ARGSUSED */ 1741 int 1742 sys_undelete(l, v, retval) 1743 struct lwp *l; 1744 void *v; 1745 register_t *retval; 1746 { 1747 struct sys_undelete_args /* { 1748 syscallarg(const char *) path; 1749 } */ *uap = v; 1750 struct proc *p = l->l_proc; 1751 int error; 1752 struct mount *mp; 1753 struct nameidata nd; 1754 1755 restart: 1756 NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE, 1757 SCARG(uap, path), p); 1758 error = namei(&nd); 1759 if (error) 1760 return (error); 1761 1762 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) { 1763 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1764 if (nd.ni_dvp == nd.ni_vp) 1765 vrele(nd.ni_dvp); 1766 else 1767 vput(nd.ni_dvp); 1768 if (nd.ni_vp) 1769 vrele(nd.ni_vp); 1770 return (EEXIST); 1771 } 1772 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 1773 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1774 if (nd.ni_dvp == nd.ni_vp) 1775 vrele(nd.ni_dvp); 1776 else 1777 vput(nd.ni_dvp); 1778 if ((error = vn_start_write(NULL, &mp, 1779 V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0) 1780 return (error); 1781 goto restart; 1782 } 1783 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE); 1784 if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0) 1785 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1786 vput(nd.ni_dvp); 1787 vn_finished_write(mp, 0); 1788 return (error); 1789 } 1790 1791 /* 1792 * Delete a name from the filesystem. 1793 */ 1794 /* ARGSUSED */ 1795 int 1796 sys_unlink(l, v, retval) 1797 struct lwp *l; 1798 void *v; 1799 register_t *retval; 1800 { 1801 struct sys_unlink_args /* { 1802 syscallarg(const char *) path; 1803 } */ *uap = v; 1804 struct proc *p = l->l_proc; 1805 struct mount *mp; 1806 struct vnode *vp; 1807 int error; 1808 struct nameidata nd; 1809 1810 restart: 1811 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE, 1812 SCARG(uap, path), p); 1813 if ((error = namei(&nd)) != 0) 1814 return (error); 1815 vp = nd.ni_vp; 1816 1817 /* 1818 * The root of a mounted filesystem cannot be deleted. 1819 */ 1820 if (vp->v_flag & VROOT) { 1821 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1822 if (nd.ni_dvp == vp) 1823 vrele(nd.ni_dvp); 1824 else 1825 vput(nd.ni_dvp); 1826 vput(vp); 1827 error = EBUSY; 1828 goto out; 1829 } 1830 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 1831 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1832 if (nd.ni_dvp == vp) 1833 vrele(nd.ni_dvp); 1834 else 1835 vput(nd.ni_dvp); 1836 vput(vp); 1837 if ((error = vn_start_write(NULL, &mp, 1838 V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0) 1839 return (error); 1840 goto restart; 1841 } 1842 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE); 1843 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE); 1844 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd); 1845 vn_finished_write(mp, 0); 1846 out: 1847 return (error); 1848 } 1849 1850 /* 1851 * Reposition read/write file offset. 1852 */ 1853 int 1854 sys_lseek(l, v, retval) 1855 struct lwp *l; 1856 void *v; 1857 register_t *retval; 1858 { 1859 struct sys_lseek_args /* { 1860 syscallarg(int) fd; 1861 syscallarg(int) pad; 1862 syscallarg(off_t) offset; 1863 syscallarg(int) whence; 1864 } */ *uap = v; 1865 struct proc *p = l->l_proc; 1866 struct ucred *cred = p->p_ucred; 1867 struct filedesc *fdp = p->p_fd; 1868 struct file *fp; 1869 struct vnode *vp; 1870 struct vattr vattr; 1871 off_t newoff; 1872 int error; 1873 1874 if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL) 1875 return (EBADF); 1876 1877 FILE_USE(fp); 1878 1879 vp = (struct vnode *)fp->f_data; 1880 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) { 1881 error = ESPIPE; 1882 goto out; 1883 } 1884 1885 switch (SCARG(uap, whence)) { 1886 case SEEK_CUR: 1887 newoff = fp->f_offset + SCARG(uap, offset); 1888 break; 1889 case SEEK_END: 1890 error = VOP_GETATTR(vp, &vattr, cred, p); 1891 if (error) 1892 goto out; 1893 newoff = SCARG(uap, offset) + vattr.va_size; 1894 break; 1895 case SEEK_SET: 1896 newoff = SCARG(uap, offset); 1897 break; 1898 default: 1899 error = EINVAL; 1900 goto out; 1901 } 1902 if ((error = VOP_SEEK(vp, fp->f_offset, newoff, cred)) != 0) 1903 goto out; 1904 1905 *(off_t *)retval = fp->f_offset = newoff; 1906 out: 1907 FILE_UNUSE(fp, p); 1908 return (error); 1909 } 1910 1911 /* 1912 * Positional read system call. 1913 */ 1914 int 1915 sys_pread(l, v, retval) 1916 struct lwp *l; 1917 void *v; 1918 register_t *retval; 1919 { 1920 struct sys_pread_args /* { 1921 syscallarg(int) fd; 1922 syscallarg(void *) buf; 1923 syscallarg(size_t) nbyte; 1924 syscallarg(off_t) offset; 1925 } */ *uap = v; 1926 struct proc *p = l->l_proc; 1927 struct filedesc *fdp = p->p_fd; 1928 struct file *fp; 1929 struct vnode *vp; 1930 off_t offset; 1931 int error, fd = SCARG(uap, fd); 1932 1933 if ((fp = fd_getfile(fdp, fd)) == NULL) 1934 return (EBADF); 1935 1936 if ((fp->f_flag & FREAD) == 0) { 1937 simple_unlock(&fp->f_slock); 1938 return (EBADF); 1939 } 1940 1941 FILE_USE(fp); 1942 1943 vp = (struct vnode *)fp->f_data; 1944 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) { 1945 error = ESPIPE; 1946 goto out; 1947 } 1948 1949 offset = SCARG(uap, offset); 1950 1951 /* 1952 * XXX This works because no file systems actually 1953 * XXX take any action on the seek operation. 1954 */ 1955 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0) 1956 goto out; 1957 1958 /* dofileread() will unuse the descriptor for us */ 1959 return (dofileread(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte), 1960 &offset, 0, retval)); 1961 1962 out: 1963 FILE_UNUSE(fp, p); 1964 return (error); 1965 } 1966 1967 /* 1968 * Positional scatter read system call. 1969 */ 1970 int 1971 sys_preadv(l, v, retval) 1972 struct lwp *l; 1973 void *v; 1974 register_t *retval; 1975 { 1976 struct sys_preadv_args /* { 1977 syscallarg(int) fd; 1978 syscallarg(const struct iovec *) iovp; 1979 syscallarg(int) iovcnt; 1980 syscallarg(off_t) offset; 1981 } */ *uap = v; 1982 struct proc *p = l->l_proc; 1983 struct filedesc *fdp = p->p_fd; 1984 struct file *fp; 1985 struct vnode *vp; 1986 off_t offset; 1987 int error, fd = SCARG(uap, fd); 1988 1989 if ((fp = fd_getfile(fdp, fd)) == NULL) 1990 return (EBADF); 1991 1992 if ((fp->f_flag & FREAD) == 0) { 1993 simple_unlock(&fp->f_slock); 1994 return (EBADF); 1995 } 1996 1997 FILE_USE(fp); 1998 1999 vp = (struct vnode *)fp->f_data; 2000 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) { 2001 error = ESPIPE; 2002 goto out; 2003 } 2004 2005 offset = SCARG(uap, offset); 2006 2007 /* 2008 * XXX This works because no file systems actually 2009 * XXX take any action on the seek operation. 2010 */ 2011 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0) 2012 goto out; 2013 2014 /* dofilereadv() will unuse the descriptor for us */ 2015 return (dofilereadv(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt), 2016 &offset, 0, retval)); 2017 2018 out: 2019 FILE_UNUSE(fp, p); 2020 return (error); 2021 } 2022 2023 /* 2024 * Positional write system call. 2025 */ 2026 int 2027 sys_pwrite(l, v, retval) 2028 struct lwp *l; 2029 void *v; 2030 register_t *retval; 2031 { 2032 struct sys_pwrite_args /* { 2033 syscallarg(int) fd; 2034 syscallarg(const void *) buf; 2035 syscallarg(size_t) nbyte; 2036 syscallarg(off_t) offset; 2037 } */ *uap = v; 2038 struct proc *p = l->l_proc; 2039 struct filedesc *fdp = p->p_fd; 2040 struct file *fp; 2041 struct vnode *vp; 2042 off_t offset; 2043 int error, fd = SCARG(uap, fd); 2044 2045 if ((fp = fd_getfile(fdp, fd)) == NULL) 2046 return (EBADF); 2047 2048 if ((fp->f_flag & FWRITE) == 0) { 2049 simple_unlock(&fp->f_slock); 2050 return (EBADF); 2051 } 2052 2053 FILE_USE(fp); 2054 2055 vp = (struct vnode *)fp->f_data; 2056 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) { 2057 error = ESPIPE; 2058 goto out; 2059 } 2060 2061 offset = SCARG(uap, offset); 2062 2063 /* 2064 * XXX This works because no file systems actually 2065 * XXX take any action on the seek operation. 2066 */ 2067 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0) 2068 goto out; 2069 2070 /* dofilewrite() will unuse the descriptor for us */ 2071 return (dofilewrite(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte), 2072 &offset, 0, retval)); 2073 2074 out: 2075 FILE_UNUSE(fp, p); 2076 return (error); 2077 } 2078 2079 /* 2080 * Positional gather write system call. 2081 */ 2082 int 2083 sys_pwritev(l, v, retval) 2084 struct lwp *l; 2085 void *v; 2086 register_t *retval; 2087 { 2088 struct sys_pwritev_args /* { 2089 syscallarg(int) fd; 2090 syscallarg(const struct iovec *) iovp; 2091 syscallarg(int) iovcnt; 2092 syscallarg(off_t) offset; 2093 } */ *uap = v; 2094 struct proc *p = l->l_proc; 2095 struct filedesc *fdp = p->p_fd; 2096 struct file *fp; 2097 struct vnode *vp; 2098 off_t offset; 2099 int error, fd = SCARG(uap, fd); 2100 2101 if ((fp = fd_getfile(fdp, fd)) == NULL) 2102 return (EBADF); 2103 2104 if ((fp->f_flag & FWRITE) == 0) { 2105 simple_unlock(&fp->f_slock); 2106 return (EBADF); 2107 } 2108 2109 FILE_USE(fp); 2110 2111 vp = (struct vnode *)fp->f_data; 2112 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) { 2113 error = ESPIPE; 2114 goto out; 2115 } 2116 2117 offset = SCARG(uap, offset); 2118 2119 /* 2120 * XXX This works because no file systems actually 2121 * XXX take any action on the seek operation. 2122 */ 2123 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0) 2124 goto out; 2125 2126 /* dofilewritev() will unuse the descriptor for us */ 2127 return (dofilewritev(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt), 2128 &offset, 0, retval)); 2129 2130 out: 2131 FILE_UNUSE(fp, p); 2132 return (error); 2133 } 2134 2135 /* 2136 * Check access permissions. 2137 */ 2138 int 2139 sys_access(l, v, retval) 2140 struct lwp *l; 2141 void *v; 2142 register_t *retval; 2143 { 2144 struct sys_access_args /* { 2145 syscallarg(const char *) path; 2146 syscallarg(int) flags; 2147 } */ *uap = v; 2148 struct proc *p = l->l_proc; 2149 struct ucred *cred; 2150 struct vnode *vp; 2151 int error, flags; 2152 struct nameidata nd; 2153 2154 cred = crdup(p->p_ucred); 2155 cred->cr_uid = p->p_cred->p_ruid; 2156 cred->cr_gid = p->p_cred->p_rgid; 2157 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, 2158 SCARG(uap, path), p); 2159 /* Override default credentials */ 2160 nd.ni_cnd.cn_cred = cred; 2161 if ((error = namei(&nd)) != 0) 2162 goto out; 2163 vp = nd.ni_vp; 2164 2165 /* Flags == 0 means only check for existence. */ 2166 if (SCARG(uap, flags)) { 2167 flags = 0; 2168 if (SCARG(uap, flags) & R_OK) 2169 flags |= VREAD; 2170 if (SCARG(uap, flags) & W_OK) 2171 flags |= VWRITE; 2172 if (SCARG(uap, flags) & X_OK) 2173 flags |= VEXEC; 2174 2175 error = VOP_ACCESS(vp, flags, cred, p); 2176 if (!error && (flags & VWRITE)) 2177 error = vn_writechk(vp); 2178 } 2179 vput(vp); 2180 out: 2181 crfree(cred); 2182 return (error); 2183 } 2184 2185 /* 2186 * Get file status; this version follows links. 2187 */ 2188 /* ARGSUSED */ 2189 int 2190 sys___stat13(l, v, retval) 2191 struct lwp *l; 2192 void *v; 2193 register_t *retval; 2194 { 2195 struct sys___stat13_args /* { 2196 syscallarg(const char *) path; 2197 syscallarg(struct stat *) ub; 2198 } */ *uap = v; 2199 struct proc *p = l->l_proc; 2200 struct stat sb; 2201 int error; 2202 struct nameidata nd; 2203 2204 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, 2205 SCARG(uap, path), p); 2206 if ((error = namei(&nd)) != 0) 2207 return (error); 2208 error = vn_stat(nd.ni_vp, &sb, p); 2209 vput(nd.ni_vp); 2210 if (error) 2211 return (error); 2212 error = copyout(&sb, SCARG(uap, ub), sizeof(sb)); 2213 return (error); 2214 } 2215 2216 /* 2217 * Get file status; this version does not follow links. 2218 */ 2219 /* ARGSUSED */ 2220 int 2221 sys___lstat13(l, v, retval) 2222 struct lwp *l; 2223 void *v; 2224 register_t *retval; 2225 { 2226 struct sys___lstat13_args /* { 2227 syscallarg(const char *) path; 2228 syscallarg(struct stat *) ub; 2229 } */ *uap = v; 2230 struct proc *p = l->l_proc; 2231 struct stat sb; 2232 int error; 2233 struct nameidata nd; 2234 2235 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE, 2236 SCARG(uap, path), p); 2237 if ((error = namei(&nd)) != 0) 2238 return (error); 2239 error = vn_stat(nd.ni_vp, &sb, p); 2240 vput(nd.ni_vp); 2241 if (error) 2242 return (error); 2243 error = copyout(&sb, SCARG(uap, ub), sizeof(sb)); 2244 return (error); 2245 } 2246 2247 /* 2248 * Get configurable pathname variables. 2249 */ 2250 /* ARGSUSED */ 2251 int 2252 sys_pathconf(l, v, retval) 2253 struct lwp *l; 2254 void *v; 2255 register_t *retval; 2256 { 2257 struct sys_pathconf_args /* { 2258 syscallarg(const char *) path; 2259 syscallarg(int) name; 2260 } */ *uap = v; 2261 struct proc *p = l->l_proc; 2262 int error; 2263 struct nameidata nd; 2264 2265 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, 2266 SCARG(uap, path), p); 2267 if ((error = namei(&nd)) != 0) 2268 return (error); 2269 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval); 2270 vput(nd.ni_vp); 2271 return (error); 2272 } 2273 2274 /* 2275 * Return target name of a symbolic link. 2276 */ 2277 /* ARGSUSED */ 2278 int 2279 sys_readlink(l, v, retval) 2280 struct lwp *l; 2281 void *v; 2282 register_t *retval; 2283 { 2284 struct sys_readlink_args /* { 2285 syscallarg(const char *) path; 2286 syscallarg(char *) buf; 2287 syscallarg(size_t) count; 2288 } */ *uap = v; 2289 struct proc *p = l->l_proc; 2290 struct vnode *vp; 2291 struct iovec aiov; 2292 struct uio auio; 2293 int error; 2294 struct nameidata nd; 2295 2296 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE, 2297 SCARG(uap, path), p); 2298 if ((error = namei(&nd)) != 0) 2299 return (error); 2300 vp = nd.ni_vp; 2301 if (vp->v_type != VLNK) 2302 error = EINVAL; 2303 else if (!(vp->v_mount->mnt_flag & MNT_SYMPERM) || 2304 (error = VOP_ACCESS(vp, VREAD, p->p_ucred, p)) == 0) { 2305 aiov.iov_base = SCARG(uap, buf); 2306 aiov.iov_len = SCARG(uap, count); 2307 auio.uio_iov = &aiov; 2308 auio.uio_iovcnt = 1; 2309 auio.uio_offset = 0; 2310 auio.uio_rw = UIO_READ; 2311 auio.uio_segflg = UIO_USERSPACE; 2312 auio.uio_procp = p; 2313 auio.uio_resid = SCARG(uap, count); 2314 error = VOP_READLINK(vp, &auio, p->p_ucred); 2315 } 2316 vput(vp); 2317 *retval = SCARG(uap, count) - auio.uio_resid; 2318 return (error); 2319 } 2320 2321 /* 2322 * Change flags of a file given a path name. 2323 */ 2324 /* ARGSUSED */ 2325 int 2326 sys_chflags(l, v, retval) 2327 struct lwp *l; 2328 void *v; 2329 register_t *retval; 2330 { 2331 struct sys_chflags_args /* { 2332 syscallarg(const char *) path; 2333 syscallarg(u_long) flags; 2334 } */ *uap = v; 2335 struct proc *p = l->l_proc; 2336 struct vnode *vp; 2337 int error; 2338 struct nameidata nd; 2339 2340 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p); 2341 if ((error = namei(&nd)) != 0) 2342 return (error); 2343 vp = nd.ni_vp; 2344 error = change_flags(vp, SCARG(uap, flags), p); 2345 vput(vp); 2346 return (error); 2347 } 2348 2349 /* 2350 * Change flags of a file given a file descriptor. 2351 */ 2352 /* ARGSUSED */ 2353 int 2354 sys_fchflags(l, v, retval) 2355 struct lwp *l; 2356 void *v; 2357 register_t *retval; 2358 { 2359 struct sys_fchflags_args /* { 2360 syscallarg(int) fd; 2361 syscallarg(u_long) flags; 2362 } */ *uap = v; 2363 struct proc *p = l->l_proc; 2364 struct vnode *vp; 2365 struct file *fp; 2366 int error; 2367 2368 /* getvnode() will use the descriptor for us */ 2369 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) 2370 return (error); 2371 vp = (struct vnode *)fp->f_data; 2372 error = change_flags(vp, SCARG(uap, flags), p); 2373 VOP_UNLOCK(vp, 0); 2374 FILE_UNUSE(fp, p); 2375 return (error); 2376 } 2377 2378 /* 2379 * Change flags of a file given a path name; this version does 2380 * not follow links. 2381 */ 2382 int 2383 sys_lchflags(l, v, retval) 2384 struct lwp *l; 2385 void *v; 2386 register_t *retval; 2387 { 2388 struct sys_lchflags_args /* { 2389 syscallarg(const char *) path; 2390 syscallarg(u_long) flags; 2391 } */ *uap = v; 2392 struct proc *p = l->l_proc; 2393 struct vnode *vp; 2394 int error; 2395 struct nameidata nd; 2396 2397 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p); 2398 if ((error = namei(&nd)) != 0) 2399 return (error); 2400 vp = nd.ni_vp; 2401 error = change_flags(vp, SCARG(uap, flags), p); 2402 vput(vp); 2403 return (error); 2404 } 2405 2406 /* 2407 * Common routine to change flags of a file. 2408 */ 2409 int 2410 change_flags(vp, flags, p) 2411 struct vnode *vp; 2412 u_long flags; 2413 struct proc *p; 2414 { 2415 struct mount *mp; 2416 struct vattr vattr; 2417 int error; 2418 2419 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) 2420 return (error); 2421 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE); 2422 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2423 /* 2424 * Non-superusers cannot change the flags on devices, even if they 2425 * own them. 2426 */ 2427 if (suser(p->p_ucred, &p->p_acflag) != 0) { 2428 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0) 2429 goto out; 2430 if (vattr.va_type == VCHR || vattr.va_type == VBLK) { 2431 error = EINVAL; 2432 goto out; 2433 } 2434 } 2435 VATTR_NULL(&vattr); 2436 vattr.va_flags = flags; 2437 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p); 2438 out: 2439 vn_finished_write(mp, 0); 2440 return (error); 2441 } 2442 2443 /* 2444 * Change mode of a file given path name; this version follows links. 2445 */ 2446 /* ARGSUSED */ 2447 int 2448 sys_chmod(l, v, retval) 2449 struct lwp *l; 2450 void *v; 2451 register_t *retval; 2452 { 2453 struct sys_chmod_args /* { 2454 syscallarg(const char *) path; 2455 syscallarg(int) mode; 2456 } */ *uap = v; 2457 struct proc *p = l->l_proc; 2458 int error; 2459 struct nameidata nd; 2460 2461 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p); 2462 if ((error = namei(&nd)) != 0) 2463 return (error); 2464 2465 error = change_mode(nd.ni_vp, SCARG(uap, mode), p); 2466 2467 vrele(nd.ni_vp); 2468 return (error); 2469 } 2470 2471 /* 2472 * Change mode of a file given a file descriptor. 2473 */ 2474 /* ARGSUSED */ 2475 int 2476 sys_fchmod(l, v, retval) 2477 struct lwp *l; 2478 void *v; 2479 register_t *retval; 2480 { 2481 struct sys_fchmod_args /* { 2482 syscallarg(int) fd; 2483 syscallarg(int) mode; 2484 } */ *uap = v; 2485 struct proc *p = l->l_proc; 2486 struct file *fp; 2487 int error; 2488 2489 /* getvnode() will use the descriptor for us */ 2490 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) 2491 return (error); 2492 2493 error = change_mode((struct vnode *)fp->f_data, SCARG(uap, mode), p); 2494 FILE_UNUSE(fp, p); 2495 return (error); 2496 } 2497 2498 /* 2499 * Change mode of a file given path name; this version does not follow links. 2500 */ 2501 /* ARGSUSED */ 2502 int 2503 sys_lchmod(l, v, retval) 2504 struct lwp *l; 2505 void *v; 2506 register_t *retval; 2507 { 2508 struct sys_lchmod_args /* { 2509 syscallarg(const char *) path; 2510 syscallarg(int) mode; 2511 } */ *uap = v; 2512 struct proc *p = l->l_proc; 2513 int error; 2514 struct nameidata nd; 2515 2516 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p); 2517 if ((error = namei(&nd)) != 0) 2518 return (error); 2519 2520 error = change_mode(nd.ni_vp, SCARG(uap, mode), p); 2521 2522 vrele(nd.ni_vp); 2523 return (error); 2524 } 2525 2526 /* 2527 * Common routine to set mode given a vnode. 2528 */ 2529 static int 2530 change_mode(vp, mode, p) 2531 struct vnode *vp; 2532 int mode; 2533 struct proc *p; 2534 { 2535 struct mount *mp; 2536 struct vattr vattr; 2537 int error; 2538 2539 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) 2540 return (error); 2541 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE); 2542 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2543 VATTR_NULL(&vattr); 2544 vattr.va_mode = mode & ALLPERMS; 2545 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p); 2546 VOP_UNLOCK(vp, 0); 2547 vn_finished_write(mp, 0); 2548 return (error); 2549 } 2550 2551 /* 2552 * Set ownership given a path name; this version follows links. 2553 */ 2554 /* ARGSUSED */ 2555 int 2556 sys_chown(l, v, retval) 2557 struct lwp *l; 2558 void *v; 2559 register_t *retval; 2560 { 2561 struct sys_chown_args /* { 2562 syscallarg(const char *) path; 2563 syscallarg(uid_t) uid; 2564 syscallarg(gid_t) gid; 2565 } */ *uap = v; 2566 struct proc *p = l->l_proc; 2567 int error; 2568 struct nameidata nd; 2569 2570 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p); 2571 if ((error = namei(&nd)) != 0) 2572 return (error); 2573 2574 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0); 2575 2576 vrele(nd.ni_vp); 2577 return (error); 2578 } 2579 2580 /* 2581 * Set ownership given a path name; this version follows links. 2582 * Provides POSIX semantics. 2583 */ 2584 /* ARGSUSED */ 2585 int 2586 sys___posix_chown(l, v, retval) 2587 struct lwp *l; 2588 void *v; 2589 register_t *retval; 2590 { 2591 struct sys_chown_args /* { 2592 syscallarg(const char *) path; 2593 syscallarg(uid_t) uid; 2594 syscallarg(gid_t) gid; 2595 } */ *uap = v; 2596 struct proc *p = l->l_proc; 2597 int error; 2598 struct nameidata nd; 2599 2600 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p); 2601 if ((error = namei(&nd)) != 0) 2602 return (error); 2603 2604 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1); 2605 2606 vrele(nd.ni_vp); 2607 return (error); 2608 } 2609 2610 /* 2611 * Set ownership given a file descriptor. 2612 */ 2613 /* ARGSUSED */ 2614 int 2615 sys_fchown(l, v, retval) 2616 struct lwp *l; 2617 void *v; 2618 register_t *retval; 2619 { 2620 struct sys_fchown_args /* { 2621 syscallarg(int) fd; 2622 syscallarg(uid_t) uid; 2623 syscallarg(gid_t) gid; 2624 } */ *uap = v; 2625 struct proc *p = l->l_proc; 2626 int error; 2627 struct file *fp; 2628 2629 /* getvnode() will use the descriptor for us */ 2630 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) 2631 return (error); 2632 2633 error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid), 2634 SCARG(uap, gid), p, 0); 2635 FILE_UNUSE(fp, p); 2636 return (error); 2637 } 2638 2639 /* 2640 * Set ownership given a file descriptor, providing POSIX/XPG semantics. 2641 */ 2642 /* ARGSUSED */ 2643 int 2644 sys___posix_fchown(l, v, retval) 2645 struct lwp *l; 2646 void *v; 2647 register_t *retval; 2648 { 2649 struct sys_fchown_args /* { 2650 syscallarg(int) fd; 2651 syscallarg(uid_t) uid; 2652 syscallarg(gid_t) gid; 2653 } */ *uap = v; 2654 struct proc *p = l->l_proc; 2655 int error; 2656 struct file *fp; 2657 2658 /* getvnode() will use the descriptor for us */ 2659 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) 2660 return (error); 2661 2662 error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid), 2663 SCARG(uap, gid), p, 1); 2664 FILE_UNUSE(fp, p); 2665 return (error); 2666 } 2667 2668 /* 2669 * Set ownership given a path name; this version does not follow links. 2670 */ 2671 /* ARGSUSED */ 2672 int 2673 sys_lchown(l, v, retval) 2674 struct lwp *l; 2675 void *v; 2676 register_t *retval; 2677 { 2678 struct sys_lchown_args /* { 2679 syscallarg(const char *) path; 2680 syscallarg(uid_t) uid; 2681 syscallarg(gid_t) gid; 2682 } */ *uap = v; 2683 struct proc *p = l->l_proc; 2684 int error; 2685 struct nameidata nd; 2686 2687 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p); 2688 if ((error = namei(&nd)) != 0) 2689 return (error); 2690 2691 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0); 2692 2693 vrele(nd.ni_vp); 2694 return (error); 2695 } 2696 2697 /* 2698 * Set ownership given a path name; this version does not follow links. 2699 * Provides POSIX/XPG semantics. 2700 */ 2701 /* ARGSUSED */ 2702 int 2703 sys___posix_lchown(l, v, retval) 2704 struct lwp *l; 2705 void *v; 2706 register_t *retval; 2707 { 2708 struct sys_lchown_args /* { 2709 syscallarg(const char *) path; 2710 syscallarg(uid_t) uid; 2711 syscallarg(gid_t) gid; 2712 } */ *uap = v; 2713 struct proc *p = l->l_proc; 2714 int error; 2715 struct nameidata nd; 2716 2717 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p); 2718 if ((error = namei(&nd)) != 0) 2719 return (error); 2720 2721 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1); 2722 2723 vrele(nd.ni_vp); 2724 return (error); 2725 } 2726 2727 /* 2728 * Common routine to set ownership given a vnode. 2729 */ 2730 static int 2731 change_owner(vp, uid, gid, p, posix_semantics) 2732 struct vnode *vp; 2733 uid_t uid; 2734 gid_t gid; 2735 struct proc *p; 2736 int posix_semantics; 2737 { 2738 struct mount *mp; 2739 struct vattr vattr; 2740 mode_t newmode; 2741 int error; 2742 2743 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) 2744 return (error); 2745 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE); 2746 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2747 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0) 2748 goto out; 2749 2750 #define CHANGED(x) ((int)(x) != -1) 2751 newmode = vattr.va_mode; 2752 if (posix_semantics) { 2753 /* 2754 * POSIX/XPG semantics: if the caller is not the super-user, 2755 * clear set-user-id and set-group-id bits. Both POSIX and 2756 * the XPG consider the behaviour for calls by the super-user 2757 * implementation-defined; we leave the set-user-id and set- 2758 * group-id settings intact in that case. 2759 */ 2760 if (suser(p->p_ucred, NULL) != 0) 2761 newmode &= ~(S_ISUID | S_ISGID); 2762 } else { 2763 /* 2764 * NetBSD semantics: when changing owner and/or group, 2765 * clear the respective bit(s). 2766 */ 2767 if (CHANGED(uid)) 2768 newmode &= ~S_ISUID; 2769 if (CHANGED(gid)) 2770 newmode &= ~S_ISGID; 2771 } 2772 /* Update va_mode iff altered. */ 2773 if (vattr.va_mode == newmode) 2774 newmode = VNOVAL; 2775 2776 VATTR_NULL(&vattr); 2777 vattr.va_uid = CHANGED(uid) ? uid : (uid_t)VNOVAL; 2778 vattr.va_gid = CHANGED(gid) ? gid : (gid_t)VNOVAL; 2779 vattr.va_mode = newmode; 2780 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p); 2781 #undef CHANGED 2782 2783 out: 2784 VOP_UNLOCK(vp, 0); 2785 vn_finished_write(mp, 0); 2786 return (error); 2787 } 2788 2789 /* 2790 * Set the access and modification times given a path name; this 2791 * version follows links. 2792 */ 2793 /* ARGSUSED */ 2794 int 2795 sys_utimes(l, v, retval) 2796 struct lwp *l; 2797 void *v; 2798 register_t *retval; 2799 { 2800 struct sys_utimes_args /* { 2801 syscallarg(const char *) path; 2802 syscallarg(const struct timeval *) tptr; 2803 } */ *uap = v; 2804 struct proc *p = l->l_proc; 2805 int error; 2806 struct nameidata nd; 2807 2808 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p); 2809 if ((error = namei(&nd)) != 0) 2810 return (error); 2811 2812 error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p); 2813 2814 vrele(nd.ni_vp); 2815 return (error); 2816 } 2817 2818 /* 2819 * Set the access and modification times given a file descriptor. 2820 */ 2821 /* ARGSUSED */ 2822 int 2823 sys_futimes(l, v, retval) 2824 struct lwp *l; 2825 void *v; 2826 register_t *retval; 2827 { 2828 struct sys_futimes_args /* { 2829 syscallarg(int) fd; 2830 syscallarg(const struct timeval *) tptr; 2831 } */ *uap = v; 2832 struct proc *p = l->l_proc; 2833 int error; 2834 struct file *fp; 2835 2836 /* getvnode() will use the descriptor for us */ 2837 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) 2838 return (error); 2839 2840 error = change_utimes((struct vnode *)fp->f_data, SCARG(uap, tptr), p); 2841 FILE_UNUSE(fp, p); 2842 return (error); 2843 } 2844 2845 /* 2846 * Set the access and modification times given a path name; this 2847 * version does not follow links. 2848 */ 2849 /* ARGSUSED */ 2850 int 2851 sys_lutimes(l, v, retval) 2852 struct lwp *l; 2853 void *v; 2854 register_t *retval; 2855 { 2856 struct sys_lutimes_args /* { 2857 syscallarg(const char *) path; 2858 syscallarg(const struct timeval *) tptr; 2859 } */ *uap = v; 2860 struct proc *p = l->l_proc; 2861 int error; 2862 struct nameidata nd; 2863 2864 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p); 2865 if ((error = namei(&nd)) != 0) 2866 return (error); 2867 2868 error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p); 2869 2870 vrele(nd.ni_vp); 2871 return (error); 2872 } 2873 2874 /* 2875 * Common routine to set access and modification times given a vnode. 2876 */ 2877 static int 2878 change_utimes(vp, tptr, p) 2879 struct vnode *vp; 2880 const struct timeval *tptr; 2881 struct proc *p; 2882 { 2883 struct timeval tv[2]; 2884 struct mount *mp; 2885 struct vattr vattr; 2886 int error; 2887 2888 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) 2889 return (error); 2890 VATTR_NULL(&vattr); 2891 if (tptr == NULL) { 2892 microtime(&tv[0]); 2893 tv[1] = tv[0]; 2894 vattr.va_vaflags |= VA_UTIMES_NULL; 2895 } else { 2896 error = copyin(tptr, tv, sizeof(tv)); 2897 if (error) 2898 goto out; 2899 } 2900 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE); 2901 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2902 vattr.va_atime.tv_sec = tv[0].tv_sec; 2903 vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000; 2904 vattr.va_mtime.tv_sec = tv[1].tv_sec; 2905 vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000; 2906 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p); 2907 VOP_UNLOCK(vp, 0); 2908 out: 2909 vn_finished_write(mp, 0); 2910 return (error); 2911 } 2912 2913 /* 2914 * Truncate a file given its path name. 2915 */ 2916 /* ARGSUSED */ 2917 int 2918 sys_truncate(l, v, retval) 2919 struct lwp *l; 2920 void *v; 2921 register_t *retval; 2922 { 2923 struct sys_truncate_args /* { 2924 syscallarg(const char *) path; 2925 syscallarg(int) pad; 2926 syscallarg(off_t) length; 2927 } */ *uap = v; 2928 struct proc *p = l->l_proc; 2929 struct vnode *vp; 2930 struct mount *mp; 2931 struct vattr vattr; 2932 int error; 2933 struct nameidata nd; 2934 2935 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p); 2936 if ((error = namei(&nd)) != 0) 2937 return (error); 2938 vp = nd.ni_vp; 2939 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) { 2940 vrele(vp); 2941 return (error); 2942 } 2943 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE); 2944 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2945 if (vp->v_type == VDIR) 2946 error = EISDIR; 2947 else if ((error = vn_writechk(vp)) == 0 && 2948 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) { 2949 VATTR_NULL(&vattr); 2950 vattr.va_size = SCARG(uap, length); 2951 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p); 2952 } 2953 vput(vp); 2954 vn_finished_write(mp, 0); 2955 return (error); 2956 } 2957 2958 /* 2959 * Truncate a file given a file descriptor. 2960 */ 2961 /* ARGSUSED */ 2962 int 2963 sys_ftruncate(l, v, retval) 2964 struct lwp *l; 2965 void *v; 2966 register_t *retval; 2967 { 2968 struct sys_ftruncate_args /* { 2969 syscallarg(int) fd; 2970 syscallarg(int) pad; 2971 syscallarg(off_t) length; 2972 } */ *uap = v; 2973 struct proc *p = l->l_proc; 2974 struct mount *mp; 2975 struct vattr vattr; 2976 struct vnode *vp; 2977 struct file *fp; 2978 int error; 2979 2980 /* getvnode() will use the descriptor for us */ 2981 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) 2982 return (error); 2983 if ((fp->f_flag & FWRITE) == 0) { 2984 error = EINVAL; 2985 goto out; 2986 } 2987 vp = (struct vnode *)fp->f_data; 2988 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) { 2989 FILE_UNUSE(fp, p); 2990 return (error); 2991 } 2992 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE); 2993 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2994 if (vp->v_type == VDIR) 2995 error = EISDIR; 2996 else if ((error = vn_writechk(vp)) == 0) { 2997 VATTR_NULL(&vattr); 2998 vattr.va_size = SCARG(uap, length); 2999 error = VOP_SETATTR(vp, &vattr, fp->f_cred, p); 3000 } 3001 VOP_UNLOCK(vp, 0); 3002 vn_finished_write(mp, 0); 3003 out: 3004 FILE_UNUSE(fp, p); 3005 return (error); 3006 } 3007 3008 /* 3009 * Sync an open file. 3010 */ 3011 /* ARGSUSED */ 3012 int 3013 sys_fsync(l, v, retval) 3014 struct lwp *l; 3015 void *v; 3016 register_t *retval; 3017 { 3018 struct sys_fsync_args /* { 3019 syscallarg(int) fd; 3020 } */ *uap = v; 3021 struct proc *p = l->l_proc; 3022 struct vnode *vp; 3023 struct mount *mp; 3024 struct file *fp; 3025 int error; 3026 3027 /* getvnode() will use the descriptor for us */ 3028 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) 3029 return (error); 3030 vp = (struct vnode *)fp->f_data; 3031 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) { 3032 FILE_UNUSE(fp, p); 3033 return (error); 3034 } 3035 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3036 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, 0, 0, p); 3037 if (error == 0 && bioops.io_fsync != NULL && 3038 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP)) 3039 (*bioops.io_fsync)(vp, 0); 3040 VOP_UNLOCK(vp, 0); 3041 vn_finished_write(mp, 0); 3042 FILE_UNUSE(fp, p); 3043 return (error); 3044 } 3045 3046 /* 3047 * Sync a range of file data. API modeled after that found in AIX. 3048 * 3049 * FDATASYNC indicates that we need only save enough metadata to be able 3050 * to re-read the written data. Note we duplicate AIX's requirement that 3051 * the file be open for writing. 3052 */ 3053 /* ARGSUSED */ 3054 int 3055 sys_fsync_range(l, v, retval) 3056 struct lwp *l; 3057 void *v; 3058 register_t *retval; 3059 { 3060 struct sys_fsync_range_args /* { 3061 syscallarg(int) fd; 3062 syscallarg(int) flags; 3063 syscallarg(off_t) start; 3064 syscallarg(int) length; 3065 } */ *uap = v; 3066 struct proc *p = l->l_proc; 3067 struct vnode *vp; 3068 struct file *fp; 3069 int flags, nflags; 3070 off_t s, e, len; 3071 int error; 3072 3073 /* getvnode() will use the descriptor for us */ 3074 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) 3075 return (error); 3076 3077 if ((fp->f_flag & FWRITE) == 0) { 3078 FILE_UNUSE(fp, p); 3079 return (EBADF); 3080 } 3081 3082 flags = SCARG(uap, flags); 3083 if (((flags & (FDATASYNC | FFILESYNC)) == 0) || 3084 ((~flags & (FDATASYNC | FFILESYNC)) == 0)) { 3085 return (EINVAL); 3086 } 3087 /* Now set up the flags for value(s) to pass to VOP_FSYNC() */ 3088 if (flags & FDATASYNC) 3089 nflags = FSYNC_DATAONLY | FSYNC_WAIT; 3090 else 3091 nflags = FSYNC_WAIT; 3092 if (flags & FDISKSYNC) 3093 nflags |= FSYNC_CACHE; 3094 3095 len = SCARG(uap, length); 3096 /* If length == 0, we do the whole file, and s = l = 0 will do that */ 3097 if (len) { 3098 s = SCARG(uap, start); 3099 e = s + len; 3100 if (e < s) { 3101 FILE_UNUSE(fp, p); 3102 return (EINVAL); 3103 } 3104 } else { 3105 e = 0; 3106 s = 0; 3107 } 3108 3109 vp = (struct vnode *)fp->f_data; 3110 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3111 error = VOP_FSYNC(vp, fp->f_cred, nflags, s, e, p); 3112 3113 if (error == 0 && bioops.io_fsync != NULL && 3114 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP)) 3115 (*bioops.io_fsync)(vp, nflags); 3116 3117 VOP_UNLOCK(vp, 0); 3118 FILE_UNUSE(fp, p); 3119 return (error); 3120 } 3121 3122 /* 3123 * Sync the data of an open file. 3124 */ 3125 /* ARGSUSED */ 3126 int 3127 sys_fdatasync(l, v, retval) 3128 struct lwp *l; 3129 void *v; 3130 register_t *retval; 3131 { 3132 struct sys_fdatasync_args /* { 3133 syscallarg(int) fd; 3134 } */ *uap = v; 3135 struct proc *p = l->l_proc; 3136 struct vnode *vp; 3137 struct file *fp; 3138 int error; 3139 3140 /* getvnode() will use the descriptor for us */ 3141 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) 3142 return (error); 3143 if ((fp->f_flag & FWRITE) == 0) { 3144 FILE_UNUSE(fp, p); 3145 return (EBADF); 3146 } 3147 vp = (struct vnode *)fp->f_data; 3148 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3149 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, 0, 0, p); 3150 VOP_UNLOCK(vp, 0); 3151 FILE_UNUSE(fp, p); 3152 return (error); 3153 } 3154 3155 /* 3156 * Rename files, (standard) BSD semantics frontend. 3157 */ 3158 /* ARGSUSED */ 3159 int 3160 sys_rename(l, v, retval) 3161 struct lwp *l; 3162 void *v; 3163 register_t *retval; 3164 { 3165 struct sys_rename_args /* { 3166 syscallarg(const char *) from; 3167 syscallarg(const char *) to; 3168 } */ *uap = v; 3169 struct proc *p = l->l_proc; 3170 3171 return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 0)); 3172 } 3173 3174 /* 3175 * Rename files, POSIX semantics frontend. 3176 */ 3177 /* ARGSUSED */ 3178 int 3179 sys___posix_rename(l, v, retval) 3180 struct lwp *l; 3181 void *v; 3182 register_t *retval; 3183 { 3184 struct sys___posix_rename_args /* { 3185 syscallarg(const char *) from; 3186 syscallarg(const char *) to; 3187 } */ *uap = v; 3188 struct proc *p = l->l_proc; 3189 3190 return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 1)); 3191 } 3192 3193 /* 3194 * Rename files. Source and destination must either both be directories, 3195 * or both not be directories. If target is a directory, it must be empty. 3196 * If `from' and `to' refer to the same object, the value of the `retain' 3197 * argument is used to determine whether `from' will be 3198 * 3199 * (retain == 0) deleted unless `from' and `to' refer to the same 3200 * object in the file system's name space (BSD). 3201 * (retain == 1) always retained (POSIX). 3202 */ 3203 static int 3204 rename_files(from, to, p, retain) 3205 const char *from, *to; 3206 struct proc *p; 3207 int retain; 3208 { 3209 struct mount *mp = NULL; 3210 struct vnode *tvp, *fvp, *tdvp; 3211 struct nameidata fromnd, tond; 3212 int error; 3213 3214 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE, 3215 from, p); 3216 if ((error = namei(&fromnd)) != 0) 3217 return (error); 3218 fvp = fromnd.ni_vp; 3219 error = vn_start_write(fvp, &mp, V_WAIT | V_PCATCH); 3220 if (error != 0) { 3221 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd); 3222 vrele(fromnd.ni_dvp); 3223 vrele(fvp); 3224 if (fromnd.ni_startdir) 3225 vrele(fromnd.ni_startdir); 3226 PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf); 3227 return (error); 3228 } 3229 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | 3230 (fvp->v_type == VDIR ? CREATEDIR : 0), UIO_USERSPACE, to, p); 3231 if ((error = namei(&tond)) != 0) { 3232 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd); 3233 vrele(fromnd.ni_dvp); 3234 vrele(fvp); 3235 goto out1; 3236 } 3237 tdvp = tond.ni_dvp; 3238 tvp = tond.ni_vp; 3239 3240 if (tvp != NULL) { 3241 if (fvp->v_type == VDIR && tvp->v_type != VDIR) { 3242 error = ENOTDIR; 3243 goto out; 3244 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) { 3245 error = EISDIR; 3246 goto out; 3247 } 3248 } 3249 3250 if (fvp == tdvp) 3251 error = EINVAL; 3252 3253 /* 3254 * Source and destination refer to the same object. 3255 */ 3256 if (fvp == tvp) { 3257 if (retain) 3258 error = -1; 3259 else if (fromnd.ni_dvp == tdvp && 3260 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen && 3261 !memcmp(fromnd.ni_cnd.cn_nameptr, 3262 tond.ni_cnd.cn_nameptr, 3263 fromnd.ni_cnd.cn_namelen)) 3264 error = -1; 3265 } 3266 3267 out: 3268 if (!error) { 3269 VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE); 3270 if (fromnd.ni_dvp != tdvp) 3271 VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE); 3272 if (tvp) { 3273 VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE); 3274 } 3275 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd, 3276 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd); 3277 } else { 3278 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd); 3279 if (tdvp == tvp) 3280 vrele(tdvp); 3281 else 3282 vput(tdvp); 3283 if (tvp) 3284 vput(tvp); 3285 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd); 3286 vrele(fromnd.ni_dvp); 3287 vrele(fvp); 3288 } 3289 vrele(tond.ni_startdir); 3290 PNBUF_PUT(tond.ni_cnd.cn_pnbuf); 3291 out1: 3292 vn_finished_write(mp, 0); 3293 if (fromnd.ni_startdir) 3294 vrele(fromnd.ni_startdir); 3295 PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf); 3296 return (error == -1 ? 0 : error); 3297 } 3298 3299 /* 3300 * Make a directory file. 3301 */ 3302 /* ARGSUSED */ 3303 int 3304 sys_mkdir(l, v, retval) 3305 struct lwp *l; 3306 void *v; 3307 register_t *retval; 3308 { 3309 struct sys_mkdir_args /* { 3310 syscallarg(const char *) path; 3311 syscallarg(int) mode; 3312 } */ *uap = v; 3313 struct proc *p = l->l_proc; 3314 struct mount *mp; 3315 struct vnode *vp; 3316 struct vattr vattr; 3317 int error; 3318 struct nameidata nd; 3319 3320 restart: 3321 NDINIT(&nd, CREATE, LOCKPARENT | CREATEDIR, UIO_USERSPACE, 3322 SCARG(uap, path), p); 3323 if ((error = namei(&nd)) != 0) 3324 return (error); 3325 vp = nd.ni_vp; 3326 if (vp != NULL) { 3327 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 3328 if (nd.ni_dvp == vp) 3329 vrele(nd.ni_dvp); 3330 else 3331 vput(nd.ni_dvp); 3332 vrele(vp); 3333 return (EEXIST); 3334 } 3335 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 3336 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 3337 if (nd.ni_dvp == vp) 3338 vrele(nd.ni_dvp); 3339 else 3340 vput(nd.ni_dvp); 3341 if ((error = vn_start_write(NULL, &mp, 3342 V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0) 3343 return (error); 3344 goto restart; 3345 } 3346 VATTR_NULL(&vattr); 3347 vattr.va_type = VDIR; 3348 vattr.va_mode = 3349 (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_cwdi->cwdi_cmask; 3350 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE); 3351 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 3352 if (!error) 3353 vput(nd.ni_vp); 3354 vn_finished_write(mp, 0); 3355 return (error); 3356 } 3357 3358 /* 3359 * Remove a directory file. 3360 */ 3361 /* ARGSUSED */ 3362 int 3363 sys_rmdir(l, v, retval) 3364 struct lwp *l; 3365 void *v; 3366 register_t *retval; 3367 { 3368 struct sys_rmdir_args /* { 3369 syscallarg(const char *) path; 3370 } */ *uap = v; 3371 struct proc *p = l->l_proc; 3372 struct mount *mp; 3373 struct vnode *vp; 3374 int error; 3375 struct nameidata nd; 3376 3377 restart: 3378 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE, 3379 SCARG(uap, path), p); 3380 if ((error = namei(&nd)) != 0) 3381 return (error); 3382 vp = nd.ni_vp; 3383 if (vp->v_type != VDIR) { 3384 error = ENOTDIR; 3385 goto out; 3386 } 3387 /* 3388 * No rmdir "." please. 3389 */ 3390 if (nd.ni_dvp == vp) { 3391 error = EINVAL; 3392 goto out; 3393 } 3394 /* 3395 * The root of a mounted filesystem cannot be deleted. 3396 */ 3397 if (vp->v_flag & VROOT) { 3398 error = EBUSY; 3399 goto out; 3400 } 3401 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 3402 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 3403 if (nd.ni_dvp == vp) 3404 vrele(nd.ni_dvp); 3405 else 3406 vput(nd.ni_dvp); 3407 vput(vp); 3408 if ((error = vn_start_write(NULL, &mp, 3409 V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0) 3410 return (error); 3411 goto restart; 3412 } 3413 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE); 3414 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE); 3415 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd); 3416 vn_finished_write(mp, 0); 3417 return (error); 3418 3419 out: 3420 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 3421 if (nd.ni_dvp == vp) 3422 vrele(nd.ni_dvp); 3423 else 3424 vput(nd.ni_dvp); 3425 vput(vp); 3426 return (error); 3427 } 3428 3429 /* 3430 * Read a block of directory entries in a file system independent format. 3431 */ 3432 int 3433 sys_getdents(l, v, retval) 3434 struct lwp *l; 3435 void *v; 3436 register_t *retval; 3437 { 3438 struct sys_getdents_args /* { 3439 syscallarg(int) fd; 3440 syscallarg(char *) buf; 3441 syscallarg(size_t) count; 3442 } */ *uap = v; 3443 struct proc *p = l->l_proc; 3444 struct file *fp; 3445 int error, done; 3446 3447 /* getvnode() will use the descriptor for us */ 3448 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) 3449 return (error); 3450 if ((fp->f_flag & FREAD) == 0) { 3451 error = EBADF; 3452 goto out; 3453 } 3454 error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE, 3455 SCARG(uap, count), &done, p, 0, 0); 3456 #ifdef KTRACE 3457 if (!error && KTRPOINT(p, KTR_GENIO)) { 3458 struct iovec iov; 3459 iov.iov_base = SCARG(uap, buf); 3460 iov.iov_len = done; 3461 ktrgenio(p, SCARG(uap, fd), UIO_READ, &iov, done, 0); 3462 } 3463 #endif 3464 *retval = done; 3465 out: 3466 FILE_UNUSE(fp, p); 3467 return (error); 3468 } 3469 3470 /* 3471 * Set the mode mask for creation of filesystem nodes. 3472 */ 3473 int 3474 sys_umask(l, v, retval) 3475 struct lwp *l; 3476 void *v; 3477 register_t *retval; 3478 { 3479 struct sys_umask_args /* { 3480 syscallarg(mode_t) newmask; 3481 } */ *uap = v; 3482 struct proc *p = l->l_proc; 3483 struct cwdinfo *cwdi; 3484 3485 cwdi = p->p_cwdi; 3486 *retval = cwdi->cwdi_cmask; 3487 cwdi->cwdi_cmask = SCARG(uap, newmask) & ALLPERMS; 3488 return (0); 3489 } 3490 3491 /* 3492 * Void all references to file by ripping underlying filesystem 3493 * away from vnode. 3494 */ 3495 /* ARGSUSED */ 3496 int 3497 sys_revoke(l, v, retval) 3498 struct lwp *l; 3499 void *v; 3500 register_t *retval; 3501 { 3502 struct sys_revoke_args /* { 3503 syscallarg(const char *) path; 3504 } */ *uap = v; 3505 struct proc *p = l->l_proc; 3506 struct mount *mp; 3507 struct vnode *vp; 3508 struct vattr vattr; 3509 int error; 3510 struct nameidata nd; 3511 3512 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p); 3513 if ((error = namei(&nd)) != 0) 3514 return (error); 3515 vp = nd.ni_vp; 3516 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0) 3517 goto out; 3518 if (p->p_ucred->cr_uid != vattr.va_uid && 3519 (error = suser(p->p_ucred, &p->p_acflag)) != 0) 3520 goto out; 3521 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) 3522 goto out; 3523 if (vp->v_usecount > 1 || (vp->v_flag & (VALIASED | VLAYER))) 3524 VOP_REVOKE(vp, REVOKEALL); 3525 vn_finished_write(mp, 0); 3526 out: 3527 vrele(vp); 3528 return (error); 3529 } 3530 3531 /* 3532 * Convert a user file descriptor to a kernel file entry. 3533 */ 3534 int 3535 getvnode(fdp, fd, fpp) 3536 struct filedesc *fdp; 3537 int fd; 3538 struct file **fpp; 3539 { 3540 struct vnode *vp; 3541 struct file *fp; 3542 3543 if ((fp = fd_getfile(fdp, fd)) == NULL) 3544 return (EBADF); 3545 3546 FILE_USE(fp); 3547 3548 if (fp->f_type != DTYPE_VNODE) { 3549 FILE_UNUSE(fp, NULL); 3550 return (EINVAL); 3551 } 3552 3553 vp = (struct vnode *)fp->f_data; 3554 if (vp->v_type == VBAD) { 3555 FILE_UNUSE(fp, NULL); 3556 return (EBADF); 3557 } 3558 3559 *fpp = fp; 3560 return (0); 3561 } 3562 3563 /* 3564 * Push extended attribute configuration information into the VFS. 3565 * 3566 * NOTE: Not all file systems that support extended attributes will 3567 * require the use of this system call. 3568 */ 3569 int 3570 sys_extattrctl(struct lwp *l, void *v, register_t *retval) 3571 { 3572 struct sys_extattrctl_args /* { 3573 syscallarg(const char *) path; 3574 syscallarg(int) cmd; 3575 syscallarg(const char *) filename; 3576 syscallarg(int) attrnamespace; 3577 syscallarg(const char *) attrname; 3578 } */ *uap = v; 3579 struct proc *p = l->l_proc; 3580 struct vnode *vp; 3581 struct nameidata nd; 3582 struct mount *mp; 3583 char attrname[EXTATTR_MAXNAMELEN]; 3584 int error; 3585 3586 if (SCARG(uap, attrname) != NULL) { 3587 error = copyinstr(SCARG(uap, attrname), attrname, 3588 sizeof(attrname), NULL); 3589 if (error) 3590 return (error); 3591 } 3592 3593 vp = NULL; 3594 if (SCARG(uap, filename) != NULL) { 3595 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, 3596 SCARG(uap, filename), p); 3597 error = namei(&nd); 3598 if (error) 3599 return (error); 3600 vp = nd.ni_vp; 3601 } 3602 3603 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p); 3604 error = namei(&nd); 3605 if (error) { 3606 if (vp != NULL) 3607 vput(vp); 3608 return (error); 3609 } 3610 3611 error = vn_start_write(nd.ni_vp, &mp, V_WAIT | V_PCATCH); 3612 if (error) { 3613 if (vp != NULL) 3614 vput(vp); 3615 return (error); 3616 } 3617 3618 error = VFS_EXTATTRCTL(mp, SCARG(uap, cmd), vp, 3619 SCARG(uap, attrnamespace), 3620 SCARG(uap, attrname) != NULL ? attrname : NULL, p); 3621 3622 vn_finished_write(mp, 0); 3623 3624 if (vp != NULL) 3625 vrele(vp); 3626 3627 return (error); 3628 } 3629 3630 /* 3631 * Set a named extended attribute on a file or directory. 3632 */ 3633 static int 3634 extattr_set_vp(struct vnode *vp, int attrnamespace, const char *attrname, 3635 const void *data, size_t nbytes, struct proc *p, register_t *retval) 3636 { 3637 struct mount *mp; 3638 struct uio auio; 3639 struct iovec aiov; 3640 ssize_t cnt; 3641 int error; 3642 3643 error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH); 3644 if (error) 3645 return (error); 3646 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE); 3647 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3648 3649 aiov.iov_base = (caddr_t) data; /* XXX kills const */ 3650 aiov.iov_len = nbytes; 3651 auio.uio_iov = &aiov; 3652 auio.uio_iovcnt = 1; 3653 auio.uio_offset = 0; 3654 if (nbytes > INT_MAX) { 3655 error = EINVAL; 3656 goto done; 3657 } 3658 auio.uio_resid = nbytes; 3659 auio.uio_rw = UIO_WRITE; 3660 auio.uio_segflg = UIO_USERSPACE; 3661 auio.uio_procp = p; 3662 cnt = nbytes; 3663 3664 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, 3665 p->p_ucred, p); 3666 cnt -= auio.uio_resid; 3667 retval[0] = cnt; 3668 3669 done: 3670 VOP_UNLOCK(vp, 0); 3671 vn_finished_write(mp, 0); 3672 return (error); 3673 } 3674 3675 int 3676 sys_extattr_set_fd(struct lwp *l, void *v, register_t *retval) 3677 { 3678 struct sys_extattr_set_fd_args /* { 3679 syscallarg(int) fd; 3680 syscallarg(int) attrnamespace; 3681 syscallarg(const char *) attrname; 3682 syscallarg(const void *) data; 3683 syscallarg(size_t) nbytes; 3684 } */ *uap = v; 3685 struct proc *p = l->l_proc; 3686 struct file *fp; 3687 struct vnode *vp; 3688 char attrname[EXTATTR_MAXNAMELEN]; 3689 int error; 3690 3691 error = copyinstr(SCARG(uap, attrname), attrname, sizeof(attrname), 3692 NULL); 3693 if (error) 3694 return (error); 3695 3696 error = getvnode(p->p_fd, SCARG(uap, fd), &fp); 3697 if (error) 3698 return (error); 3699 vp = (struct vnode *) fp->f_data; 3700 3701 error = extattr_set_vp(vp, SCARG(uap, attrnamespace), attrname, 3702 SCARG(uap, data), SCARG(uap, nbytes), p, retval); 3703 3704 FILE_UNUSE(fp, p); 3705 return (error); 3706 } 3707 3708 int 3709 sys_extattr_set_file(struct lwp *l, void *v, register_t *retval) 3710 { 3711 struct sys_extattr_set_file_args /* { 3712 syscallarg(const char *) path; 3713 syscallarg(int) attrnamespace; 3714 syscallarg(const char *) attrname; 3715 syscallarg(const void *) data; 3716 syscallarg(size_t) nbytes; 3717 } */ *uap = v; 3718 struct proc *p = l->l_proc; 3719 struct nameidata nd; 3720 char attrname[EXTATTR_MAXNAMELEN]; 3721 int error; 3722 3723 error = copyinstr(SCARG(uap, attrname), attrname, sizeof(attrname), 3724 NULL); 3725 if (error) 3726 return (error); 3727 3728 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p); 3729 error = namei(&nd); 3730 if (error) 3731 return (error); 3732 3733 error = extattr_set_vp(nd.ni_vp, SCARG(uap, attrnamespace), attrname, 3734 SCARG(uap, data), SCARG(uap, nbytes), p, retval); 3735 3736 vrele(nd.ni_vp); 3737 return (error); 3738 } 3739 3740 int 3741 sys_extattr_set_link(struct lwp *l, void *v, register_t *retval) 3742 { 3743 struct sys_extattr_set_link_args /* { 3744 syscallarg(const char *) path; 3745 syscallarg(int) attrnamespace; 3746 syscallarg(const char *) attrname; 3747 syscallarg(const void *) data; 3748 syscallarg(size_t) nbytes; 3749 } */ *uap = v; 3750 struct proc *p = l->l_proc; 3751 struct nameidata nd; 3752 char attrname[EXTATTR_MAXNAMELEN]; 3753 int error; 3754 3755 error = copyinstr(SCARG(uap, attrname), attrname, sizeof(attrname), 3756 NULL); 3757 if (error) 3758 return (error); 3759 3760 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p); 3761 error = namei(&nd); 3762 if (error) 3763 return (error); 3764 3765 error = extattr_set_vp(nd.ni_vp, SCARG(uap, attrnamespace), attrname, 3766 SCARG(uap, data), SCARG(uap, nbytes), p, retval); 3767 3768 vrele(nd.ni_vp); 3769 return (error); 3770 } 3771 3772 /* 3773 * Get a named extended attribute on a file or directory. 3774 */ 3775 static int 3776 extattr_get_vp(struct vnode *vp, int attrnamespace, const char *attrname, 3777 void *data, size_t nbytes, struct proc *p, register_t *retval) 3778 { 3779 struct uio auio, *auiop; 3780 struct iovec aiov; 3781 ssize_t cnt; 3782 size_t size, *sizep; 3783 int error; 3784 3785 VOP_LEASE(vp, p, p->p_ucred, LEASE_READ); 3786 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3787 3788 /* 3789 * Slightly unusual semantics: if the user provides a NULL data 3790 * pointer, they don't want to receive the data, just the maximum 3791 * read length. 3792 */ 3793 auiop = NULL; 3794 sizep = NULL; 3795 cnt = 0; 3796 if (data != NULL) { 3797 aiov.iov_base = data; 3798 aiov.iov_len = nbytes; 3799 auio.uio_iov = &aiov; 3800 auio.uio_offset = 0; 3801 if (nbytes > INT_MAX) { 3802 error = EINVAL; 3803 goto done; 3804 } 3805 auio.uio_resid = nbytes; 3806 auio.uio_rw = UIO_READ; 3807 auio.uio_segflg = UIO_USERSPACE; 3808 auio.uio_procp = p; 3809 auiop = &auio; 3810 cnt = nbytes; 3811 } else 3812 sizep = &size; 3813 3814 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, auiop, sizep, 3815 p->p_ucred, p); 3816 3817 if (auiop != NULL) { 3818 cnt -= auio.uio_resid; 3819 retval[0] = cnt; 3820 } else 3821 retval[0] = size; 3822 3823 done: 3824 VOP_UNLOCK(vp, 0); 3825 return (error); 3826 } 3827 3828 int 3829 sys_extattr_get_fd(struct lwp *l, void *v, register_t *retval) 3830 { 3831 struct sys_extattr_get_fd_args /* { 3832 syscallarg(int) fd; 3833 syscallarg(int) attrnamespace; 3834 syscallarg(const char *) attrname; 3835 syscallarg(void *) data; 3836 syscallarg(size_t) nbytes; 3837 } */ *uap = v; 3838 struct proc *p = l->l_proc; 3839 struct file *fp; 3840 struct vnode *vp; 3841 char attrname[EXTATTR_MAXNAMELEN]; 3842 int error; 3843 3844 error = copyinstr(SCARG(uap, attrname), attrname, sizeof(attrname), 3845 NULL); 3846 if (error) 3847 return (error); 3848 3849 error = getvnode(p->p_fd, SCARG(uap, fd), &fp); 3850 if (error) 3851 return (error); 3852 vp = (struct vnode *) fp->f_data; 3853 3854 error = extattr_get_vp(vp, SCARG(uap, attrnamespace), attrname, 3855 SCARG(uap, data), SCARG(uap, nbytes), p, retval); 3856 3857 FILE_UNUSE(fp, p); 3858 return (error); 3859 } 3860 3861 int 3862 sys_extattr_get_file(struct lwp *l, void *v, register_t *retval) 3863 { 3864 struct sys_extattr_get_file_args /* { 3865 syscallarg(const char *) path; 3866 syscallarg(int) attrnamespace; 3867 syscallarg(const char *) attrname; 3868 syscallarg(void *) data; 3869 syscallarg(size_t) nbytes; 3870 } */ *uap = v; 3871 struct proc *p = l->l_proc; 3872 struct nameidata nd; 3873 char attrname[EXTATTR_MAXNAMELEN]; 3874 int error; 3875 3876 error = copyinstr(SCARG(uap, attrname), attrname, sizeof(attrname), 3877 NULL); 3878 if (error) 3879 return (error); 3880 3881 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p); 3882 error = namei(&nd); 3883 if (error) 3884 return (error); 3885 3886 error = extattr_get_vp(nd.ni_vp, SCARG(uap, attrnamespace), attrname, 3887 SCARG(uap, data), SCARG(uap, nbytes), p, retval); 3888 3889 vrele(nd.ni_vp); 3890 return (error); 3891 } 3892 3893 int 3894 sys_extattr_get_link(struct lwp *l, void *v, register_t *retval) 3895 { 3896 struct sys_extattr_get_link_args /* { 3897 syscallarg(const char *) path; 3898 syscallarg(int) attrnamespace; 3899 syscallarg(const char *) attrname; 3900 syscallarg(void *) data; 3901 syscallarg(size_t) nbytes; 3902 } */ *uap = v; 3903 struct proc *p = l->l_proc; 3904 struct nameidata nd; 3905 char attrname[EXTATTR_MAXNAMELEN]; 3906 int error; 3907 3908 error = copyinstr(SCARG(uap, attrname), attrname, sizeof(attrname), 3909 NULL); 3910 if (error) 3911 return (error); 3912 3913 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p); 3914 error = namei(&nd); 3915 if (error) 3916 return (error); 3917 3918 error = extattr_get_vp(nd.ni_vp, SCARG(uap, attrnamespace), attrname, 3919 SCARG(uap, data), SCARG(uap, nbytes), p, retval); 3920 3921 vrele(nd.ni_vp); 3922 return (error); 3923 } 3924 3925 /* 3926 * Delete a named extended attribute on a file or directory. 3927 */ 3928 static int 3929 extattr_delete_vp(struct vnode *vp, int attrnamespace, const char *attrname, 3930 struct proc *p) 3931 { 3932 struct mount *mp; 3933 int error; 3934 3935 error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH); 3936 if (error) 3937 return (error); 3938 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE); 3939 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3940 3941 error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, p->p_ucred, p); 3942 if (error == EOPNOTSUPP) 3943 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL, 3944 p->p_ucred, p); 3945 3946 VOP_UNLOCK(vp, 0); 3947 vn_finished_write(mp, 0); 3948 return (error); 3949 } 3950 3951 int 3952 sys_extattr_delete_fd(struct lwp *l, void *v, register_t *retval) 3953 { 3954 struct sys_extattr_delete_fd_args /* { 3955 syscallarg(int) fd; 3956 syscallarg(int) attrnamespace; 3957 syscallarg(const char *) attrname; 3958 } */ *uap = v; 3959 struct proc *p = l->l_proc; 3960 struct file *fp; 3961 struct vnode *vp; 3962 char attrname[EXTATTR_MAXNAMELEN]; 3963 int error; 3964 3965 error = copyinstr(SCARG(uap, attrname), attrname, sizeof(attrname), 3966 NULL); 3967 if (error) 3968 return (error); 3969 3970 error = getvnode(p->p_fd, SCARG(uap, fd), &fp); 3971 if (error) 3972 return (error); 3973 vp = (struct vnode *) fp->f_data; 3974 3975 error = extattr_delete_vp(vp, SCARG(uap, attrnamespace), attrname, p); 3976 3977 FILE_UNUSE(fp, p); 3978 return (error); 3979 } 3980 3981 int 3982 sys_extattr_delete_file(struct lwp *l, void *v, register_t *retval) 3983 { 3984 struct sys_extattr_delete_file_args /* { 3985 syscallarg(const char *) path; 3986 syscallarg(int) attrnamespace; 3987 syscallarg(const char *) attrname; 3988 } */ *uap = v; 3989 struct proc *p = l->l_proc; 3990 struct nameidata nd; 3991 char attrname[EXTATTR_MAXNAMELEN]; 3992 int error; 3993 3994 error = copyinstr(SCARG(uap, attrname), attrname, sizeof(attrname), 3995 NULL); 3996 if (error) 3997 return (error); 3998 3999 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p); 4000 error = namei(&nd); 4001 if (error) 4002 return (error); 4003 4004 error = extattr_delete_vp(nd.ni_vp, SCARG(uap, attrnamespace), attrname, 4005 p); 4006 4007 vrele(nd.ni_vp); 4008 return (error); 4009 } 4010 4011 int 4012 sys_extattr_delete_link(struct lwp *l, void *v, register_t *retval) 4013 { 4014 struct sys_extattr_delete_link_args /* { 4015 syscallarg(const char *) path; 4016 syscallarg(int) attrnamespace; 4017 syscallarg(const char *) attrname; 4018 } */ *uap = v; 4019 struct proc *p = l->l_proc; 4020 struct nameidata nd; 4021 char attrname[EXTATTR_MAXNAMELEN]; 4022 int error; 4023 4024 error = copyinstr(SCARG(uap, attrname), attrname, sizeof(attrname), 4025 NULL); 4026 if (error) 4027 return (error); 4028 4029 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p); 4030 error = namei(&nd); 4031 if (error) 4032 return (error); 4033 4034 error = extattr_delete_vp(nd.ni_vp, SCARG(uap, attrnamespace), attrname, 4035 p); 4036 4037 vrele(nd.ni_vp); 4038 return (error); 4039 } 4040 4041 /* 4042 * Retrieve a list of extended attributes on a file or directory. 4043 */ 4044 static int 4045 extattr_list_vp(struct vnode *vp, int attrnamespace, void *data, size_t nbytes, 4046 struct proc *p, register_t *retval) 4047 { 4048 struct uio auio, *auiop; 4049 size_t size, *sizep; 4050 struct iovec aiov; 4051 ssize_t cnt; 4052 int error; 4053 4054 VOP_LEASE(vp, p, p->p_ucred, LEASE_READ); 4055 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 4056 4057 auiop = NULL; 4058 sizep = NULL; 4059 cnt = 0; 4060 if (data != NULL) { 4061 aiov.iov_base = data; 4062 aiov.iov_len = nbytes; 4063 auio.uio_iov = &aiov; 4064 auio.uio_offset = 0; 4065 if (nbytes > INT_MAX) { 4066 error = EINVAL; 4067 goto done; 4068 } 4069 auio.uio_resid = nbytes; 4070 auio.uio_rw = UIO_READ; 4071 auio.uio_segflg = UIO_USERSPACE; 4072 auio.uio_procp = p; 4073 auiop = &auio; 4074 cnt = nbytes; 4075 } else 4076 sizep = &size; 4077 4078 error = VOP_LISTEXTATTR(vp, attrnamespace, auiop, sizep, 4079 p->p_ucred, p); 4080 4081 if (auiop != NULL) { 4082 cnt -= auio.uio_resid; 4083 retval[0] = cnt; 4084 } else 4085 retval[0] = size; 4086 4087 done: 4088 VOP_UNLOCK(vp, 0); 4089 return (error); 4090 } 4091 4092 int 4093 sys_extattr_list_fd(struct lwp *l, void *v, register_t *retval) 4094 { 4095 struct sys_extattr_list_fd_args /* { 4096 syscallarg(int) fd; 4097 syscallarg(int) attrnamespace; 4098 syscallarg(void *) data; 4099 syscallarg(size_t) nbytes; 4100 } */ *uap = v; 4101 struct proc *p = l->l_proc; 4102 struct file *fp; 4103 struct vnode *vp; 4104 int error; 4105 4106 error = getvnode(p->p_fd, SCARG(uap, fd), &fp); 4107 if (error) 4108 return (error); 4109 vp = (struct vnode *) fp->f_data; 4110 4111 error = extattr_list_vp(vp, SCARG(uap, attrnamespace), 4112 SCARG(uap, data), SCARG(uap, nbytes), p, retval); 4113 4114 FILE_UNUSE(fp, p); 4115 return (error); 4116 } 4117 4118 int 4119 sys_extattr_list_file(struct lwp *l, void *v, register_t *retval) 4120 { 4121 struct sys_extattr_list_file_args /* { 4122 syscallarg(const char *) path; 4123 syscallarg(int) attrnamespace; 4124 syscallarg(void *) data; 4125 syscallarg(size_t) nbytes; 4126 } */ *uap = v; 4127 struct proc *p = l->l_proc; 4128 struct nameidata nd; 4129 int error; 4130 4131 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p); 4132 error = namei(&nd); 4133 if (error) 4134 return (error); 4135 4136 error = extattr_list_vp(nd.ni_vp, SCARG(uap, attrnamespace), 4137 SCARG(uap, data), SCARG(uap, nbytes), p, retval); 4138 4139 vrele(nd.ni_vp); 4140 return (error); 4141 } 4142 4143 int 4144 sys_extattr_list_link(struct lwp *l, void *v, register_t *retval) 4145 { 4146 struct sys_extattr_list_link_args /* { 4147 syscallarg(const char *) path; 4148 syscallarg(int) attrnamespace; 4149 syscallarg(void *) data; 4150 syscallarg(size_t) nbytes; 4151 } */ *uap = v; 4152 struct proc *p = l->l_proc; 4153 struct nameidata nd; 4154 int error; 4155 4156 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p); 4157 error = namei(&nd); 4158 if (error) 4159 return (error); 4160 4161 error = extattr_list_vp(nd.ni_vp, SCARG(uap, attrnamespace), 4162 SCARG(uap, data), SCARG(uap, nbytes), p, retval); 4163 4164 vrele(nd.ni_vp); 4165 return (error); 4166 } 4167