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