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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
35 * $FreeBSD: src/sys/kern/vfs_syscalls.c,v 1.151.2.18 2003/04/04 20:35:58 tegge Exp $
38 #include <sys/param.h>
39 #include <sys/systm.h>
42 #include <sys/sysent.h>
43 #include <sys/malloc.h>
44 #include <sys/mount.h>
45 #include <sys/mountctl.h>
46 #include <sys/sysmsg.h>
47 #include <sys/filedesc.h>
48 #include <sys/kernel.h>
49 #include <sys/fcntl.h>
51 #include <sys/linker.h>
53 #include <sys/unistd.h>
54 #include <sys/vnode.h>
58 #include <sys/namei.h>
59 #include <sys/nlookup.h>
60 #include <sys/dirent.h>
61 #include <sys/extattr.h>
62 #include <sys/spinlock.h>
63 #include <sys/kern_syscall.h>
64 #include <sys/objcache.h>
65 #include <sys/sysctl.h>
68 #include <sys/file2.h>
69 #include <sys/spinlock2.h>
72 #include <vm/vm_object.h>
73 #include <vm/vm_page.h>
75 #include <machine/limits.h>
76 #include <machine/stdarg.h>
78 static void mount_warning(struct mount *mp, const char *ctl, ...)
80 static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb);
81 static int checkvp_chdir (struct vnode *vn, struct thread *td);
82 static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch);
83 static int get_fscap(const char *);
84 static int chroot_refuse_vdir_fds (thread_t td, struct filedesc *fdp);
85 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
86 static int getutimes (struct timeval *, struct timespec *);
87 static int getutimens (const struct timespec *, struct timespec *, int *);
88 static int setfown (struct mount *, struct vnode *, uid_t, gid_t);
89 static int setfmode (struct vnode *, int);
90 static int setfflags (struct vnode *, u_long);
91 static int setutimes (struct vnode *, struct vattr *,
92 const struct timespec *, int);
94 static int usermount = 0; /* if 1, non-root can mount fs. */
95 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
96 "Allow non-root users to mount filesystems");
98 static int debug_unmount = 0; /* if 1 loop until unmount success */
99 SYSCTL_INT(_vfs, OID_AUTO, debug_unmount, CTLFLAG_RW, &debug_unmount, 0,
100 "Stall failed unmounts in loop");
102 static struct krate krate_rename = { 1 };
105 * Virtual File System System Calls
109 * Mount a file system.
111 * mount_args(char *type, char *path, int flags, caddr_t data)
116 sys_mount(struct sysmsg *sysmsg, const struct mount_args *uap)
118 struct thread *td = curthread;
121 struct mount *mp, *nullmp;
122 struct vfsconf *vfsp;
123 int error, flag = 0, flag2 = 0;
126 int flags = uap->flags;
128 struct nlookupdata nd;
129 char fstypename[MFSNAMELEN];
134 /* We do not allow user mounts inside a jail for now */
135 if (usermount && jailed(cred)) {
141 * Extract the file system type. We need to know this early, to take
142 * appropriate actions for jails and the filesystems to mount.
144 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0)
148 * Select the correct cap according to the file system type.
150 priv = get_fscap(fstypename);
152 if (usermount == 0 && (error = caps_priv_check_td(td, priv)))
156 * Do not allow NFS export by non-root users.
158 if (flags & MNT_EXPORTED) {
159 error = caps_priv_check_td(td, priv);
164 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
166 if (caps_priv_check_td(td, priv))
167 flags |= MNT_NOSUID | MNT_NODEV;
170 * Lookup the requested path and extract the nch and vnode.
172 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
174 if ((error = nlookup(&nd)) == 0) {
175 if (nd.nl_nch.ncp->nc_vp == NULL)
185 * If the target filesystem is resolved via a nullfs mount, then
186 * nd.nl_nch.mount will be pointing to the nullfs mount structure
187 * instead of the target file system. We need it in case we are
190 nullmp = nd.nl_nch.mount;
193 * Extract the locked+refd ncp and cleanup the nd structure
196 cache_zero(&nd.nl_nch);
199 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
200 (mp = cache_findmount(&nch)) != NULL) {
209 * now we have the locked ref'd nch and unreferenced vnode.
212 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
219 * Now we have an unlocked ref'd nch and a locked ref'd vp
221 if (flags & MNT_UPDATE) {
222 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
229 if (strncmp(fstypename, "null", 5) == 0) {
237 flag2 = mp->mnt_kern_flag;
239 * We only allow the filesystem to be reloaded if it
240 * is currently mounted read-only.
242 if ((flags & MNT_RELOAD) &&
243 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
246 error = EOPNOTSUPP; /* Needs translation */
250 * Only root, or the user that did the original mount is
251 * permitted to update it.
253 if (mp->mnt_stat.f_owner != cred->cr_uid &&
254 (error = caps_priv_check_td(td, priv))) {
259 if (vfs_busy(mp, LK_NOWAIT)) {
272 mp->mnt_flag |= flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
273 lwkt_gettoken(&mp->mnt_token);
280 * If the user is not root, ensure that they own the directory
281 * onto which we are attempting to mount.
283 if ((error = VOP_GETATTR(vp, &va)) ||
284 (va.va_uid != cred->cr_uid &&
285 (error = caps_priv_check_td(td, priv)))) {
290 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
295 if (vp->v_type != VDIR) {
301 if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
307 vfsp = vfsconf_find_by_name(fstypename);
311 /* Only load modules for root (very important!) */
312 error = caps_priv_check_td(td, SYSCAP_RESTRICTEDROOT);
318 error = linker_load_file(fstypename, &lf);
319 if (error || lf == NULL) {
327 /* lookup again, see if the VFS was loaded */
328 vfsp = vfsconf_find_by_name(fstypename);
331 linker_file_unload(lf);
346 * Allocate and initialize the filesystem.
348 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
349 mount_init(mp, vfsp->vfc_vfsops);
350 vfs_busy(mp, LK_NOWAIT);
352 mp->mnt_pbuf_count = nswbuf_kva / NSWBUF_SPLIT;
353 vfsp->vfc_refcount++;
354 mp->mnt_stat.f_type = vfsp->vfc_typenum;
355 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
356 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
357 mp->mnt_stat.f_owner = cred->cr_uid;
358 lwkt_gettoken(&mp->mnt_token);
362 * (per-mount token acquired at this point)
364 * Set the mount level flags.
366 if (flags & MNT_RDONLY)
367 mp->mnt_flag |= MNT_RDONLY;
368 else if (mp->mnt_flag & MNT_RDONLY)
369 mp->mnt_kern_flag |= MNTK_WANTRDWR;
370 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
371 MNT_SYNCHRONOUS | MNT_ASYNC | MNT_NOATIME |
372 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
373 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR |
375 mp->mnt_flag |= flags & (MNT_NOSUID | MNT_NOEXEC |
376 MNT_NODEV | MNT_SYNCHRONOUS | MNT_ASYNC | MNT_FORCE |
377 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
378 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR |
382 * Pre-set the mount's ALL_MPSAFE flags if specified in the vfsconf.
383 * This way the initial VFS_MOUNT() call will also be MPSAFE.
385 if (vfsp->vfc_flags & VFCF_MPSAFE)
386 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE;
389 * Mount the filesystem.
390 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
393 if (mp->mnt_flag & MNT_UPDATE) {
394 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
395 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
396 mp->mnt_flag &= ~MNT_RDONLY;
397 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
398 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
401 mp->mnt_kern_flag = flag2;
403 lwkt_reltoken(&mp->mnt_token);
409 mp->mnt_ncmounton = nch;
410 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
411 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
414 * Put the new filesystem on the mount list after root. The mount
415 * point gets its own mnt_ncmountpt (unless the VFS already set one
416 * up) which represents the root of the mount. The lookup code
417 * detects the mount point going forward and checks the root of
418 * the mount going backwards.
420 * It is not necessary to invalidate or purge the vnode underneath
421 * because elements under the mount will be given their own glue
425 if (mp->mnt_ncmountpt.ncp == NULL) {
427 * Allocate, then unlock, but leave the ref intact.
428 * This is the mnt_refs (1) that we will retain
429 * through to the unmount.
431 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
432 cache_unlock(&mp->mnt_ncmountpt);
436 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
438 cache_ismounting(mp);
439 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
441 mountlist_insert(mp, MNTINS_LAST);
443 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
444 error = vfs_allocate_syncvnode(mp);
445 lwkt_reltoken(&mp->mnt_token);
447 error = VFS_START(mp, 0);
449 KNOTE(&fs_klist, VQ_MOUNT);
451 bzero(&mp->mnt_ncmounton, sizeof(mp->mnt_ncmounton));
452 vn_syncer_thr_stop(mp);
453 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
454 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
455 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
456 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
457 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
459 crfree(mp->mnt_cred);
462 mp->mnt_vfc->vfc_refcount--;
463 lwkt_reltoken(&mp->mnt_token);
474 * Scan all active processes to see if any of them have a current
475 * or root directory onto which the new filesystem has just been
476 * mounted. If so, replace them with the new mount point.
478 * Both old_nch and new_nch are ref'd on call but not locked.
479 * new_nch must be temporarily locked so it can be associated with the
480 * vnode representing the root of the mount point.
482 struct checkdirs_info {
483 struct nchandle old_nch;
484 struct nchandle new_nch;
485 struct vnode *old_vp;
486 struct vnode *new_vp;
489 static int checkdirs_callback(struct proc *p, void *data);
492 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
494 struct checkdirs_info info;
500 * If the old mount point's vnode has a usecount of 1, it is not
501 * being held as a descriptor anywhere.
503 olddp = old_nch->ncp->nc_vp;
504 if (olddp == NULL || VREFCNT(olddp) == 1)
508 * Force the root vnode of the new mount point to be resolved
509 * so we can update any matching processes.
512 if (VFS_ROOT(mp, &newdp))
513 panic("mount: lost mount");
516 vn_lock(newdp, LK_EXCLUSIVE | LK_RETRY);
517 cache_setunresolved(new_nch);
518 cache_setvp(new_nch, newdp);
519 cache_unlock(new_nch);
522 * Special handling of the root node
524 if (rootvnode == olddp) {
526 vfs_cache_setroot(newdp, cache_hold(new_nch));
530 * Pass newdp separately so the callback does not have to access
531 * it via new_nch->ncp->nc_vp.
533 info.old_nch = *old_nch;
534 info.new_nch = *new_nch;
536 allproc_scan(checkdirs_callback, &info, 0);
541 * NOTE: callback is not MP safe because the scanned process's filedesc
542 * structure can be ripped out from under us, amoung other things.
545 checkdirs_callback(struct proc *p, void *data)
547 struct checkdirs_info *info = data;
548 struct filedesc *fdp;
549 struct nchandle ncdrop1;
550 struct nchandle ncdrop2;
551 struct vnode *vprele1;
552 struct vnode *vprele2;
554 if ((fdp = p->p_fd) != NULL) {
555 cache_zero(&ncdrop1);
556 cache_zero(&ncdrop2);
561 * MPUNSAFE - XXX fdp can be pulled out from under a
564 * A shared filedesc is ok, we don't have to copy it
565 * because we are making this change globally.
567 spin_lock(&fdp->fd_spin);
568 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
569 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
570 vprele1 = fdp->fd_cdir;
572 fdp->fd_cdir = info->new_vp;
573 ncdrop1 = fdp->fd_ncdir;
574 cache_copy(&info->new_nch, &fdp->fd_ncdir);
576 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
577 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
578 vprele2 = fdp->fd_rdir;
580 fdp->fd_rdir = info->new_vp;
581 ncdrop2 = fdp->fd_nrdir;
582 cache_copy(&info->new_nch, &fdp->fd_nrdir);
584 spin_unlock(&fdp->fd_spin);
586 cache_drop(&ncdrop1);
588 cache_drop(&ncdrop2);
598 * Unmount a file system.
600 * Note: unmount takes a path to the vnode mounted on as argument,
601 * not special file (as before).
603 * umount_args(char *path, int flags)
608 sys_unmount(struct sysmsg *sysmsg, const struct unmount_args *uap)
610 struct thread *td = curthread;
611 struct proc *p __debugvar = td->td_proc;
612 struct mount *mp = NULL;
613 struct nlookupdata nd;
614 char fstypename[MFSNAMELEN];
623 /* We do not allow user umounts inside a jail for now */
624 if (usermount && jailed(cred)) {
629 error = nlookup_init(&nd, uap->path, UIO_USERSPACE,
630 NLC_FOLLOW | NLC_IGNBADDIR);
632 error = nlookup(&nd);
636 mp = nd.nl_nch.mount;
638 /* Figure out the fsname in order to select proper privs */
639 ksnprintf(fstypename, MFSNAMELEN, "%s", mp->mnt_vfc->vfc_name);
640 priv = get_fscap(fstypename);
642 if (usermount == 0 && (error = caps_priv_check_td(td, priv))) {
648 * Only root, or the user that did the original mount is
649 * permitted to unmount this filesystem.
651 if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) &&
652 (error = caps_priv_check_td(td, priv)))
658 * Don't allow unmounting the root file system.
660 if (mp->mnt_flag & MNT_ROOTFS) {
666 * Must be the root of the filesystem
668 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
673 /* Check if this mount belongs to this prison */
674 if (jailed(cred) && mp->mnt_cred && (!mp->mnt_cred->cr_prison ||
675 mp->mnt_cred->cr_prison != cred->cr_prison)) {
676 kprintf("mountpoint %s does not belong to this jail\n",
683 * If no error try to issue the unmount. We lose our cache
684 * ref when we call nlookup_done so we must hold the mount point
685 * to prevent use-after-free races.
691 error = dounmount(mp, uap->flags, 0);
701 * Do the actual file system unmount (interlocked against the mountlist
702 * token and mp->mnt_token).
705 dounmount_interlock(struct mount *mp)
707 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
709 mp->mnt_kern_flag |= MNTK_UNMOUNT;
714 * Returns non-zero if the specified process uses the specified
718 process_uses_mount(struct proc *p, struct mount *mp)
720 struct filedesc *fdp;
728 if (fdp->fd_ncdir.mount == mp ||
729 fdp->fd_nrdir.mount == mp ||
730 fdp->fd_njdir.mount == mp)
736 spin_lock_shared(&fdp->fd_spin);
737 for (n = 0; n < fdp->fd_nfiles; ++n) {
738 fp = fdp->fd_files[n].fp;
739 if (fp && fp->f_nchandle.mount == mp) {
744 spin_unlock_shared(&fdp->fd_spin);
750 * Cleanup processes that have references to the mount point
751 * being force-unmounted.
753 struct unmount_allproc_info {
759 unmount_allproc_cb(struct proc *p, void *arg)
761 struct unmount_allproc_info *info;
767 if (p->p_textnch.mount == mp)
768 cache_drop(&p->p_textnch);
769 if (info->sig && process_uses_mount(p, mp))
770 ksignal(p, info->sig);
776 * The guts of the unmount code. The mount owns one ref and one hold
777 * count. If we successfully interlock the unmount, those refs are ours.
778 * (The ref is from mnt_ncmountpt).
780 * When halting we shortcut certain mount types such as devfs by not actually
781 * issuing the VFS_SYNC() or VFS_UNMOUNT(). They are still disconnected
782 * from the mountlist so higher-level filesytems can unmount cleanly.
784 * The mount types that allow QUICKHALT are: devfs, tmpfs, procfs.
787 dounmount(struct mount *mp, int flags, int halting)
789 struct namecache *ncp;
800 lwkt_gettoken(&mp->mnt_token);
803 * When halting, certain mount points can essentially just
804 * be unhooked and otherwise ignored.
806 if (halting && (mp->mnt_kern_flag & MNTK_QUICKHALT)) {
815 * Exclusive access for unmounting purposes.
817 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
821 * We now 'own' the last mp->mnt_refs
823 * Allow filesystems to detect that a forced unmount is in progress.
825 if (flags & MNT_FORCE)
826 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
827 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_TIMELOCK);
828 error = lockmgr(&mp->mnt_lock, lflags);
830 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
831 if (mp->mnt_kern_flag & MNTK_MWAIT) {
832 mp->mnt_kern_flag &= ~MNTK_MWAIT;
838 if (mp->mnt_flag & MNT_EXPUBLIC)
839 vfs_setpublicfs(NULL, NULL, NULL);
841 vfs_msync(mp, MNT_WAIT);
842 async_flag = mp->mnt_flag & MNT_ASYNC;
843 mp->mnt_flag &=~ MNT_ASYNC;
846 * Decomission our special mnt_syncer vnode. This also stops
847 * the vnlru code. If we are unable to unmount we recommission
850 * Then sync the filesystem.
852 if ((vp = mp->mnt_syncer) != NULL) {
853 mp->mnt_syncer = NULL;
854 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
860 * Sync normally-mounted filesystem.
862 if (quickhalt == 0) {
863 if ((mp->mnt_flag & MNT_RDONLY) == 0)
864 VFS_SYNC(mp, MNT_WAIT);
868 * nchandle records ref the mount structure. Expect a count of 1
869 * (our mount->mnt_ncmountpt).
871 * Scans can get temporary refs on a mountpoint (thought really
872 * heavy duty stuff like cache_findmount() do not).
874 for (retry = 0; (retry < 10 || debug_unmount); ++retry) {
876 * Invalidate the namecache topology under the mount.
877 * nullfs mounts alias a real mount's namecache topology
878 * and it should not be invalidated in that case.
880 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
881 cache_lock(&mp->mnt_ncmountpt);
882 cache_inval(&mp->mnt_ncmountpt,
883 CINV_DESTROY|CINV_CHILDREN);
884 cache_unlock(&mp->mnt_ncmountpt);
890 cache_unmounting(mp);
891 if (mp->mnt_refs != 1)
892 cache_clearmntcache(mp);
895 * Break out if we are good. Don't count ncp refs if the
898 ncp = (mp->mnt_kern_flag & MNTK_NCALIASED) ?
899 NULL : mp->mnt_ncmountpt.ncp;
900 if (mp->mnt_refs == 1 &&
901 (ncp == NULL || (ncp->nc_refs == 1 &&
902 TAILQ_FIRST(&ncp->nc_list) == NULL))) {
907 * If forcing the unmount, clean out any p->p_textnch
908 * nchandles that match this mount.
910 * In addition any process which has a current, root, or
911 * jail directory matching the mount, or which has an open
912 * descriptor matching the mount, will be killed. We first
913 * try SIGKILL, and if that doesn't work we issue SIGQUIT.
915 if (flags & MNT_FORCE) {
916 struct unmount_allproc_info info;
930 allproc_scan(&unmount_allproc_cb, &info, 0);
936 tsleep(&mp->mnt_refs, 0, "mntbsy", hz / 4 + 1);
937 if (debug_unmount && (retry & 15) == 15) {
939 "(%p) debug - retry %d, "
940 "%d namecache refs, %d mount refs",
942 (ncp ? ncp->nc_refs - 1 : 0),
948 "forced umount of \"%s\" - "
949 "%d namecache refs, %d mount refs",
950 (mp->mnt_ncmountpt.ncp ?
951 mp->mnt_ncmountpt.ncp->nc_name : "?"),
952 (ncp ? ncp->nc_refs - 1 : 0),
957 ncp = (mp->mnt_kern_flag & MNTK_NCALIASED) ?
958 NULL : mp->mnt_ncmountpt.ncp;
959 if (mp->mnt_refs != 1 ||
960 (ncp != NULL && (ncp->nc_refs != 1 ||
961 TAILQ_FIRST(&ncp->nc_list)))) {
963 "(%p): %d namecache refs, %d mount refs "
966 (ncp ? ncp->nc_refs - 1 : 0),
968 if (flags & MNT_FORCE) {
970 mount_warning(mp, "forcing unmount\n");
977 * So far so good, sync the filesystem once more and
978 * call the VFS unmount code if the sync succeeds.
980 if (error == 0 && quickhalt == 0) {
981 if (mp->mnt_flag & MNT_RDONLY) {
982 error = VFS_UNMOUNT(mp, flags);
984 error = VFS_SYNC(mp, MNT_WAIT);
985 if (error == 0 || /* no error */
986 error == EOPNOTSUPP || /* no sync avail */
987 (flags & MNT_FORCE)) { /* force anyway */
988 error = VFS_UNMOUNT(mp, flags);
993 "(%p) unmount: vfs refused to unmount, "
1000 * If an error occurred we can still recover, restoring the
1001 * syncer vnode and misc flags.
1004 if (mp->mnt_syncer == NULL && hadsyncer)
1005 vfs_allocate_syncvnode(mp);
1006 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1007 mp->mnt_flag |= async_flag;
1008 lockmgr(&mp->mnt_lock, LK_RELEASE);
1009 if (mp->mnt_kern_flag & MNTK_MWAIT) {
1010 mp->mnt_kern_flag &= ~MNTK_MWAIT;
1016 * Clean up any journals still associated with the mount after
1017 * filesystem activity has ceased.
1019 journal_remove_all_journals(mp,
1020 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
1022 mountlist_remove(mp);
1025 * Remove any installed vnode ops here so the individual VFSs don't
1028 * mnt_refs should go to zero when we scrap mnt_ncmountpt.
1030 * When quickhalting we have to keep these intact because the
1031 * underlying vnodes have not been destroyed, and some might be
1034 if (quickhalt == 0) {
1035 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
1036 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
1037 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
1038 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
1039 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
1042 if (mp->mnt_ncmountpt.ncp != NULL) {
1043 nch = mp->mnt_ncmountpt;
1044 cache_zero(&mp->mnt_ncmountpt);
1045 cache_clrmountpt(&nch);
1048 if (mp->mnt_ncmounton.ncp != NULL) {
1049 cache_unmounting(mp);
1050 nch = mp->mnt_ncmounton;
1051 cache_zero(&mp->mnt_ncmounton);
1052 cache_clrmountpt(&nch);
1057 crfree(mp->mnt_cred);
1058 mp->mnt_cred = NULL;
1061 mp->mnt_vfc->vfc_refcount--;
1064 * If not quickhalting the mount, we expect there to be no
1067 if (quickhalt == 0 && !TAILQ_EMPTY(&mp->mnt_nvnodelist))
1068 panic("unmount: dangling vnode");
1073 lockmgr(&mp->mnt_lock, LK_RELEASE);
1074 if (mp->mnt_kern_flag & MNTK_MWAIT) {
1075 mp->mnt_kern_flag &= ~MNTK_MWAIT;
1080 * If we reach here and freeok != 0 we must free the mount.
1081 * mnt_refs should already have dropped to 0, so if it is not
1082 * zero we must cycle the caches and wait.
1084 * When we are satisfied that the mount has disconnected we can
1085 * drop the hold on the mp that represented the mount (though the
1086 * caller might actually have another, so the caller's drop may
1087 * do the actual free).
1090 if (mp->mnt_refs > 0)
1091 cache_clearmntcache(mp);
1092 while (mp->mnt_refs > 0) {
1093 cache_unmounting(mp);
1095 tsleep(&mp->mnt_refs, 0, "umntrwait", hz / 10 + 1);
1096 cache_clearmntcache(mp);
1098 lwkt_reltoken(&mp->mnt_token);
1102 cache_clearmntcache(mp);
1105 KNOTE(&fs_klist, VQ_UNMOUNT);
1108 lwkt_reltoken(&mp->mnt_token);
1114 mount_warning(struct mount *mp, const char *ctl, ...)
1120 __va_start(va, ctl);
1121 if (cache_fullpath(NULL, &mp->mnt_ncmounton, NULL,
1122 &ptr, &buf, 0) == 0) {
1123 kprintf("unmount(%s): ", ptr);
1128 kprintf("unmount(%p", mp);
1129 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
1130 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
1139 * Shim cache_fullpath() to handle the case where a process is chrooted into
1140 * a subdirectory of a mount. In this case if the root mount matches the
1141 * process root directory's mount we have to specify the process's root
1142 * directory instead of the mount point, because the mount point might
1143 * be above the root directory.
1147 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
1149 struct nchandle *nch;
1151 if (p && p->p_fd->fd_nrdir.mount == mp)
1152 nch = &p->p_fd->fd_nrdir;
1154 nch = &mp->mnt_ncmountpt;
1155 return(cache_fullpath(p, nch, NULL, rb, fb, 0));
1159 * Sync each mounted filesystem.
1163 static int syncprt = 0;
1164 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
1167 static int sync_callback(struct mount *mp, void *data);
1170 sys_sync(struct sysmsg *sysmsg, const struct sync_args *uap)
1172 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
1178 sync_callback(struct mount *mp, void *data __unused)
1182 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
1183 lwkt_gettoken(&mp->mnt_token);
1184 asyncflag = mp->mnt_flag & MNT_ASYNC;
1185 mp->mnt_flag &= ~MNT_ASYNC;
1186 lwkt_reltoken(&mp->mnt_token);
1187 vfs_msync(mp, MNT_NOWAIT);
1188 VFS_SYNC(mp, MNT_NOWAIT);
1189 lwkt_gettoken(&mp->mnt_token);
1190 mp->mnt_flag |= asyncflag;
1191 lwkt_reltoken(&mp->mnt_token);
1196 /* XXX PRISON: could be per prison flag */
1197 static int prison_quotas;
1199 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
1203 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
1205 * Change filesystem quotas.
1210 sys_quotactl(struct sysmsg *sysmsg, const struct quotactl_args *uap)
1212 struct nlookupdata nd;
1218 if (td->td_ucred->cr_prison && !prison_quotas) {
1223 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1225 error = nlookup(&nd);
1227 mp = nd.nl_nch.mount;
1228 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
1229 uap->arg, nd.nl_cred);
1237 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
1238 * void *buf, int buflen)
1240 * This function operates on a mount point and executes the specified
1241 * operation using the specified control data, and possibly returns data.
1243 * The actual number of bytes stored in the result buffer is returned, 0
1244 * if none, otherwise an error is returned.
1249 sys_mountctl(struct sysmsg *sysmsg, const struct mountctl_args *uap)
1251 struct thread *td = curthread;
1259 * Sanity and permissions checks. We must be root.
1261 if (td->td_ucred->cr_prison != NULL)
1263 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
1264 (error = caps_priv_check_td(td, SYSCAP_RESTRICTEDROOT)) != 0)
1270 * Argument length checks
1272 if (uap->ctllen < 0 || uap->ctllen > 1024)
1274 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
1276 if (uap->path == NULL)
1280 * Allocate the necessary buffers and copyin data
1282 path = objcache_get(namei_oc, M_WAITOK);
1283 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1288 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
1289 error = copyin(uap->ctl, ctl, uap->ctllen);
1294 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
1297 * Validate the descriptor
1300 fp = holdfp(td, uap->fd, -1);
1310 * Execute the internal kernel function and clean up.
1312 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen,
1313 buf, uap->buflen, &sysmsg->sysmsg_result);
1315 dropfp(td, uap->fd, fp);
1316 if (error == 0 && sysmsg->sysmsg_result > 0)
1317 error = copyout(buf, uap->buf, sysmsg->sysmsg_result);
1320 objcache_put(namei_oc, path);
1329 * Execute a mount control operation by resolving the path to a mount point
1330 * and calling vop_mountctl().
1332 * Use the mount point from the nch instead of the vnode so nullfs mounts
1333 * can properly spike the VOP.
1336 kern_mountctl(const char *path, int op, struct file *fp,
1337 const void *ctl, int ctllen,
1338 void *buf, int buflen, int *res)
1341 struct nlookupdata nd;
1342 struct nchandle nch;
1348 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1351 error = nlookup(&nd);
1356 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1363 * Yes, all this is needed to use the nch.mount below, because
1364 * we must maintain a ref on the mount to avoid ripouts (e.g.
1365 * due to heavy mount/unmount use by synth or poudriere).
1368 cache_zero(&nd.nl_nch);
1376 * Must be the root of the filesystem
1378 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1383 if (mp == NULL || mp->mnt_kern_flag & MNTK_UNMOUNT) {
1384 kprintf("kern_mountctl: Warning, \"%s\" racing unmount\n",
1390 error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen,
1399 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1401 struct thread *td = curthread;
1402 struct proc *p = td->td_proc;
1405 char *fullpath, *freepath;
1408 if ((error = nlookup(nd)) != 0)
1410 mp = nd->nl_nch.mount;
1414 * Ignore refresh error, user should have visibility.
1415 * This can happen if a NFS mount goes bad (e.g. server
1416 * revokes perms or goes down).
1418 error = VFS_STATFS(mp, sp, nd->nl_cred);
1421 error = mount_path(p, mp, &fullpath, &freepath);
1424 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1425 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1426 kfree(freepath, M_TEMP);
1428 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1429 bcopy(sp, buf, sizeof(*buf));
1430 /* Only root should have access to the fsid's. */
1431 if (caps_priv_check_td(td, SYSCAP_RESTRICTEDROOT))
1432 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1437 * statfs_args(char *path, struct statfs *buf)
1439 * Get filesystem statistics.
1442 sys_statfs(struct sysmsg *sysmsg, const struct statfs_args *uap)
1444 struct nlookupdata nd;
1448 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1450 error = kern_statfs(&nd, &buf);
1453 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1458 kern_fstatfs(int fd, struct statfs *buf)
1460 struct thread *td = curthread;
1461 struct proc *p = td->td_proc;
1465 char *fullpath, *freepath;
1469 if ((error = holdvnode(td, fd, &fp)) != 0)
1473 * Try to use mount info from any overlays rather than the
1474 * mount info for the underlying vnode, otherwise we will
1475 * fail when operating on null-mounted paths inside a chroot.
1477 if ((mp = fp->f_nchandle.mount) == NULL)
1478 mp = ((struct vnode *)fp->f_data)->v_mount;
1483 if (fp->f_cred == NULL) {
1489 * Ignore refresh error, user should have visibility.
1490 * This can happen if a NFS mount goes bad (e.g. server
1491 * revokes perms or goes down).
1494 error = VFS_STATFS(mp, sp, fp->f_cred);
1496 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1498 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1499 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1500 kfree(freepath, M_TEMP);
1502 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1503 bcopy(sp, buf, sizeof(*buf));
1505 /* Only root should have access to the fsid's. */
1506 if (caps_priv_check_td(td, SYSCAP_RESTRICTEDROOT))
1507 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1515 * fstatfs_args(int fd, struct statfs *buf)
1517 * Get filesystem statistics.
1520 sys_fstatfs(struct sysmsg *sysmsg, const struct fstatfs_args *uap)
1525 error = kern_fstatfs(uap->fd, &buf);
1528 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1533 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1539 if ((error = nlookup(nd)) != 0)
1541 mp = nd->nl_nch.mount;
1542 sp = &mp->mnt_vstat;
1543 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1547 if (mp->mnt_flag & MNT_RDONLY)
1548 sp->f_flag |= ST_RDONLY;
1549 if (mp->mnt_flag & MNT_NOSUID)
1550 sp->f_flag |= ST_NOSUID;
1551 bcopy(sp, buf, sizeof(*buf));
1556 * statfs_args(char *path, struct statfs *buf)
1558 * Get filesystem statistics.
1561 sys_statvfs(struct sysmsg *sysmsg, const struct statvfs_args *uap)
1563 struct nlookupdata nd;
1567 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1569 error = kern_statvfs(&nd, &buf);
1572 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1577 kern_fstatvfs(int fd, struct statvfs *buf)
1579 struct thread *td = curthread;
1585 if ((error = holdvnode(td, fd, &fp)) != 0)
1587 if ((mp = fp->f_nchandle.mount) == NULL)
1588 mp = ((struct vnode *)fp->f_data)->v_mount;
1593 if (fp->f_cred == NULL) {
1597 sp = &mp->mnt_vstat;
1598 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1602 if (mp->mnt_flag & MNT_RDONLY)
1603 sp->f_flag |= ST_RDONLY;
1604 if (mp->mnt_flag & MNT_NOSUID)
1605 sp->f_flag |= ST_NOSUID;
1607 bcopy(sp, buf, sizeof(*buf));
1615 * fstatfs_args(int fd, struct statfs *buf)
1617 * Get filesystem statistics.
1620 sys_fstatvfs(struct sysmsg *sysmsg, const struct fstatvfs_args *uap)
1625 error = kern_fstatvfs(uap->fd, &buf);
1628 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1633 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1635 * Get statistics on all filesystems.
1638 struct getfsstat_info {
1639 struct statfs *sfsp;
1647 static int getfsstat_callback(struct mount *, void *);
1650 sys_getfsstat(struct sysmsg *sysmsg, const struct getfsstat_args *uap)
1652 struct thread *td = curthread;
1653 struct getfsstat_info info;
1655 bzero(&info, sizeof(info));
1657 info.maxcount = uap->bufsize / sizeof(struct statfs);
1658 info.sfsp = uap->buf;
1660 info.flags = uap->flags;
1663 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1664 if (info.sfsp && info.count > info.maxcount)
1665 sysmsg->sysmsg_result = info.maxcount;
1667 sysmsg->sysmsg_result = info.count;
1668 return (info.error);
1672 getfsstat_callback(struct mount *mp, void *data)
1674 struct getfsstat_info *info = data;
1680 if (info->td->td_proc && !chroot_visible_mnt(mp, info->td->td_proc))
1683 if (info->sfsp && info->count < info->maxcount) {
1687 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1688 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1689 * overrides MNT_WAIT.
1691 * Ignore refresh error, user should have visibility.
1692 * This can happen if a NFS mount goes bad (e.g. server
1693 * revokes perms or goes down).
1695 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1696 (info->flags & MNT_WAIT)) &&
1697 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1700 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1702 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1704 info->error = error;
1707 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1708 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1709 kfree(freepath, M_TEMP);
1711 error = copyout(sp, info->sfsp, sizeof(*sp));
1713 info->error = error;
1723 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1724 long bufsize, int flags)
1726 * Get statistics on all filesystems.
1729 struct getvfsstat_info {
1730 struct statfs *sfsp;
1731 struct statvfs *vsfsp;
1739 static int getvfsstat_callback(struct mount *, void *);
1742 sys_getvfsstat(struct sysmsg *sysmsg, const struct getvfsstat_args *uap)
1744 struct thread *td = curthread;
1745 struct getvfsstat_info info;
1747 bzero(&info, sizeof(info));
1749 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1750 info.sfsp = uap->buf;
1751 info.vsfsp = uap->vbuf;
1753 info.flags = uap->flags;
1756 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1757 if (info.vsfsp && info.count > info.maxcount)
1758 sysmsg->sysmsg_result = info.maxcount;
1760 sysmsg->sysmsg_result = info.count;
1761 return (info.error);
1765 getvfsstat_callback(struct mount *mp, void *data)
1767 struct getvfsstat_info *info = data;
1769 struct statvfs *vsp;
1774 if (info->td->td_proc && !chroot_visible_mnt(mp, info->td->td_proc))
1777 if (info->vsfsp && info->count < info->maxcount) {
1779 vsp = &mp->mnt_vstat;
1782 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1783 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1784 * overrides MNT_WAIT.
1786 * Ignore refresh error, user should have visibility.
1787 * This can happen if a NFS mount goes bad (e.g. server
1788 * revokes perms or goes down).
1790 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1791 (info->flags & MNT_WAIT)) &&
1792 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1795 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1797 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1798 (info->flags & MNT_WAIT)) &&
1799 (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) {
1803 if (mp->mnt_flag & MNT_RDONLY)
1804 vsp->f_flag |= ST_RDONLY;
1805 if (mp->mnt_flag & MNT_NOSUID)
1806 vsp->f_flag |= ST_NOSUID;
1808 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1810 info->error = error;
1813 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1814 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1815 kfree(freepath, M_TEMP);
1817 error = copyout(sp, info->sfsp, sizeof(*sp));
1819 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1821 info->error = error;
1833 * fchdir_args(int fd)
1835 * Change current working directory to a given file descriptor.
1838 sys_fchdir(struct sysmsg *sysmsg, const struct fchdir_args *uap)
1840 struct thread *td = curthread;
1841 struct proc *p = td->td_proc;
1842 struct filedesc *fdp = p->p_fd;
1843 struct vnode *vp, *ovp;
1846 struct nchandle nch, onch, tnch;
1849 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
1851 lwkt_gettoken(&p->p_token);
1852 vp = (struct vnode *)fp->f_data;
1854 vn_lock(vp, LK_SHARED | LK_RETRY);
1855 if (fp->f_nchandle.ncp == NULL)
1858 error = checkvp_chdir(vp, td);
1863 cache_copy(&fp->f_nchandle, &nch);
1866 * If the ncp has become a mount point, traverse through
1870 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1871 (mp = cache_findmount(&nch)) != NULL
1873 error = nlookup_mp(mp, &tnch);
1875 cache_unlock(&tnch); /* leave ref intact */
1877 vp = tnch.ncp->nc_vp;
1878 error = vget(vp, LK_SHARED);
1879 KKASSERT(error == 0);
1883 cache_dropmount(mp);
1886 spin_lock(&fdp->fd_spin);
1888 onch = fdp->fd_ncdir;
1890 fdp->fd_ncdir = nch;
1891 spin_unlock(&fdp->fd_spin);
1892 vn_unlock(vp); /* leave ref intact */
1901 lwkt_reltoken(&p->p_token);
1906 kern_chdir(struct nlookupdata *nd)
1908 struct thread *td = curthread;
1909 struct proc *p = td->td_proc;
1910 struct filedesc *fdp = p->p_fd;
1911 struct vnode *vp, *ovp;
1912 struct nchandle onch;
1915 nd->nl_flags |= NLC_SHAREDLOCK;
1916 if ((error = nlookup(nd)) != 0)
1918 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1920 if ((error = vget(vp, LK_SHARED)) != 0)
1923 lwkt_gettoken(&p->p_token);
1924 error = checkvp_chdir(vp, td);
1927 spin_lock(&fdp->fd_spin);
1929 onch = fdp->fd_ncdir;
1930 fdp->fd_ncdir = nd->nl_nch;
1932 spin_unlock(&fdp->fd_spin);
1933 cache_unlock(&nd->nl_nch); /* leave reference intact */
1936 cache_zero(&nd->nl_nch);
1940 lwkt_reltoken(&p->p_token);
1945 * chdir_args(char *path)
1947 * Change current working directory (``.'').
1950 sys_chdir(struct sysmsg *sysmsg, const struct chdir_args *uap)
1952 struct nlookupdata nd;
1955 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1957 error = kern_chdir(&nd);
1963 * Helper function for raised chroot(2) security function: Refuse if
1964 * any filedescriptors are open directories.
1967 chroot_refuse_vdir_fds(thread_t td, struct filedesc *fdp)
1974 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1975 if ((error = holdvnode(td, fd, &fp)) != 0)
1977 vp = (struct vnode *)fp->f_data;
1978 if (vp->v_type != VDIR) {
1989 * This sysctl determines if we will allow a process to chroot(2) if it
1990 * has a directory open:
1991 * 0: disallowed for all processes.
1992 * 1: allowed for processes that were not already chroot(2)'ed.
1993 * 2: allowed for all processes.
1996 static int chroot_allow_open_directories = 1;
1998 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1999 &chroot_allow_open_directories, 0, "");
2002 * chroot to the specified namecache entry. We obtain the vp from the
2003 * namecache data. The passed ncp must be locked and referenced and will
2004 * remain locked and referenced on return.
2007 kern_chroot(struct nchandle *nch)
2009 struct thread *td = curthread;
2010 struct proc *p = td->td_proc;
2011 struct filedesc *fdp = p->p_fd;
2016 * Only privileged user can chroot
2018 error = caps_priv_check(td->td_ucred, SYSCAP_NOVFS_CHROOT);
2023 * Disallow open directory descriptors (fchdir() breakouts).
2025 if (chroot_allow_open_directories == 0 ||
2026 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
2027 if ((error = chroot_refuse_vdir_fds(td, fdp)) != 0)
2030 if ((vp = nch->ncp->nc_vp) == NULL)
2033 if ((error = vget(vp, LK_SHARED)) != 0)
2037 * Check the validity of vp as a directory to change to and
2038 * associate it with rdir/jdir.
2040 error = checkvp_chdir(vp, td);
2041 vn_unlock(vp); /* leave reference intact */
2043 lwkt_gettoken(&p->p_token);
2044 vrele(fdp->fd_rdir);
2045 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
2046 cache_drop(&fdp->fd_nrdir);
2047 cache_copy(nch, &fdp->fd_nrdir);
2048 if (fdp->fd_jdir == NULL) {
2051 cache_copy(nch, &fdp->fd_njdir);
2053 if ((p->p_flags & P_DIDCHROOT) == 0) {
2054 p->p_flags |= P_DIDCHROOT;
2055 if (p->p_depth <= 65535 - 32)
2058 lwkt_reltoken(&p->p_token);
2066 * chroot_args(char *path)
2068 * Change notion of root (``/'') directory.
2071 sys_chroot(struct sysmsg *sysmsg, const struct chroot_args *uap)
2073 struct thread *td __debugvar = curthread;
2074 struct nlookupdata nd;
2077 KKASSERT(td->td_proc);
2078 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2080 nd.nl_flags |= NLC_EXEC;
2081 error = nlookup(&nd);
2083 error = kern_chroot(&nd.nl_nch);
2090 sys_chroot_kernel(struct sysmsg *sysmsg, const struct chroot_kernel_args *uap)
2092 struct thread *td = curthread;
2093 struct nlookupdata nd;
2094 struct nchandle *nch;
2098 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2102 error = nlookup(&nd);
2108 error = caps_priv_check(td->td_ucred, SYSCAP_NOVFS_CHROOT);
2112 if ((vp = nch->ncp->nc_vp) == NULL) {
2117 if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0)
2120 vfs_cache_setroot(vp, cache_hold(nch));
2129 * Common routine for chroot and chdir. Given a locked, referenced vnode,
2130 * determine whether it is legal to chdir to the vnode. The vnode's state
2131 * is not changed by this call.
2134 checkvp_chdir(struct vnode *vp, struct thread *td)
2138 if (vp->v_type != VDIR)
2141 error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
2146 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
2148 struct thread *td = curthread;
2149 struct proc *p = td->td_proc;
2150 struct lwp *lp = td->td_lwp;
2151 struct filedesc *fdp = p->p_fd;
2155 int type, indx, error = 0;
2158 if ((oflags & O_ACCMODE) == O_ACCMODE)
2160 flags = FFLAGS(oflags);
2161 error = falloc(lp, &nfp, NULL);
2165 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
2168 * Call vn_open() to do the lookup and assign the vnode to the
2169 * file pointer. vn_open() does not change the ref count on fp
2170 * and the vnode, on success, will be inherited by the file pointer
2173 * Request a shared lock on the vnode if possible.
2175 * When NLC_SHAREDLOCK is set we may still need an exclusive vnode
2176 * lock for O_RDWR opens on executables in order to avoid a VTEXT
2177 * detection race. The NLC_EXCLLOCK_IFEXEC handles this case.
2179 * NOTE: We need a flag to separate terminal vnode locking from
2180 * parent locking. O_CREAT needs parent locking, but O_TRUNC
2181 * and O_RDWR only need to lock the terminal vnode exclusively.
2183 nd->nl_flags |= NLC_LOCKVP;
2184 if ((flags & (O_CREAT|O_TRUNC)) == 0) {
2185 nd->nl_flags |= NLC_SHAREDLOCK;
2187 nd->nl_flags |= NLC_EXCLLOCK_IFEXEC;
2191 * Issue the vn_open, passing in the referenced fp. the vn_open()
2192 * is allowed to replace fp by fdrop()ing it and returning its own
2196 error = vn_open(nd, &nfp, flags, cmode);
2201 * Deal with any error condition
2204 fdrop(fp); /* our ref */
2205 if (error == ERESTART)
2211 * Reserve a file descriptor.
2213 if ((error = fdalloc(p, 0, &indx)) != 0) {
2219 * Handle advisory lock flags. This is only supported with vnodes.
2220 * For things like /dev/fd/N we might not actually get a vnode.
2222 if ((flags & (O_EXLOCK | O_SHLOCK)) && fp->f_type == DTYPE_VNODE) {
2225 vp = (struct vnode *)fp->f_data;
2228 lf.l_whence = SEEK_SET;
2231 if (flags & O_EXLOCK)
2232 lf.l_type = F_WRLCK;
2234 lf.l_type = F_RDLCK;
2235 if (flags & FNONBLOCK)
2240 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type);
2243 * lock request failed. Clean up the reserved
2247 fsetfd(fdp, NULL, indx);
2251 atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */
2256 * release our private reference, leaving the one associated with the
2257 * descriptor table intact.
2259 if (oflags & O_CLOEXEC)
2260 fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
2261 fsetfd(fdp, fp, indx);
2269 * open_args(char *path, int flags, int mode)
2271 * Check permissions, allocate an open file structure,
2272 * and call the device open routine if any.
2275 sys_open(struct sysmsg *sysmsg, const struct open_args *uap)
2277 struct nlookupdata nd;
2280 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2282 error = kern_open(&nd, uap->flags,
2283 uap->mode, &sysmsg->sysmsg_result);
2290 * openat_args(int fd, char *path, int flags, int mode)
2293 sys_openat(struct sysmsg *sysmsg, const struct openat_args *uap)
2295 struct nlookupdata nd;
2299 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2301 error = kern_open(&nd, uap->flags, uap->mode,
2302 &sysmsg->sysmsg_result);
2304 nlookup_done_at(&nd, fp);
2309 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
2311 struct thread *td = curthread;
2312 struct proc *p = td->td_proc;
2321 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2322 vattr.va_rmajor = rmajor;
2323 vattr.va_rminor = rminor;
2325 switch (mode & S_IFMT) {
2326 case S_IFMT: /* used by badsect to flag bad sectors */
2327 error = caps_priv_check(td->td_ucred, SYSCAP_NOVFS_MKNOD_BAD);
2328 vattr.va_type = VBAD;
2331 error = caps_priv_check_td(td, SYSCAP_NOVFS_MKNOD_DEV);
2332 vattr.va_type = VCHR;
2335 error = caps_priv_check_td(td, SYSCAP_NOVFS_MKNOD_DEV);
2336 vattr.va_type = VBLK;
2339 error = caps_priv_check(td->td_ucred, SYSCAP_NOVFS_MKNOD_WHT);
2342 case S_IFDIR: /* special directories support for HAMMER */
2343 error = caps_priv_check(td->td_ucred, SYSCAP_NOVFS_MKNOD_DIR);
2344 vattr.va_type = VDIR;
2347 return (kern_mkfifo(nd, mode));
2358 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2359 if ((error = nlookup(nd)) != 0)
2361 if (nd->nl_nch.ncp->nc_vp)
2363 if (nd->nl_dvp == NULL)
2365 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2369 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2370 nd->nl_cred, NAMEI_CREATE);
2373 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2374 &vp, nd->nl_cred, &vattr);
2382 * mknod_args(char *path, int mode, int dev)
2384 * Create a special file.
2387 sys_mknod(struct sysmsg *sysmsg, const struct mknod_args *uap)
2389 struct nlookupdata nd;
2392 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2394 error = kern_mknod(&nd, uap->mode,
2395 umajor(uap->dev), uminor(uap->dev));
2402 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2404 * Create a special file. The path is relative to the directory associated
2408 sys_mknodat(struct sysmsg *sysmsg, const struct mknodat_args *uap)
2410 struct nlookupdata nd;
2414 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2416 error = kern_mknod(&nd, uap->mode,
2417 umajor(uap->dev), uminor(uap->dev));
2419 nlookup_done_at(&nd, fp);
2424 kern_mkfifo(struct nlookupdata *nd, int mode)
2426 struct thread *td = curthread;
2427 struct proc *p = td->td_proc;
2434 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2435 if ((error = nlookup(nd)) != 0)
2437 if (nd->nl_nch.ncp->nc_vp)
2439 if (nd->nl_dvp == NULL)
2441 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2445 vattr.va_type = VFIFO;
2446 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2448 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2455 * mkfifo_args(char *path, int mode)
2457 * Create a named pipe.
2460 sys_mkfifo(struct sysmsg *sysmsg, const struct mkfifo_args *uap)
2462 struct nlookupdata nd;
2465 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2467 error = kern_mkfifo(&nd, uap->mode);
2473 * mkfifoat_args(int fd, char *path, mode_t mode)
2475 * Create a named pipe. The path is relative to the directory associated
2479 sys_mkfifoat(struct sysmsg *sysmsg, const struct mkfifoat_args *uap)
2481 struct nlookupdata nd;
2485 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2487 error = kern_mkfifo(&nd, uap->mode);
2488 nlookup_done_at(&nd, fp);
2492 static int hardlink_check_uid = 0;
2493 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2494 &hardlink_check_uid, 0,
2495 "Unprivileged processes cannot create hard links to files owned by other "
2497 static int hardlink_check_gid = 0;
2498 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2499 &hardlink_check_gid, 0,
2500 "Unprivileged processes cannot create hard links to files owned by other "
2504 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2510 * Shortcut if disabled
2512 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2516 * Privileged user can always hardlink
2518 if (caps_priv_check(cred, SYSCAP_NOVFS_LINK) == 0)
2522 * Otherwise only if the originating file is owned by the
2523 * same user or group. Note that any group is allowed if
2524 * the file is owned by the caller.
2526 error = VOP_GETATTR(vp, &va);
2530 if (hardlink_check_uid) {
2531 if (cred->cr_uid != va.va_uid)
2535 if (hardlink_check_gid) {
2536 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2544 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2546 struct thread *td = curthread;
2551 * Lookup the source and obtained a locked vnode.
2553 * You may only hardlink a file which you have write permission
2554 * on or which you own.
2556 * XXX relookup on vget failure / race ?
2559 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2560 if ((error = nlookup(nd)) != 0)
2562 vp = nd->nl_nch.ncp->nc_vp;
2563 KKASSERT(vp != NULL);
2564 if (vp->v_type == VDIR)
2565 return (EPERM); /* POSIX */
2566 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2568 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2572 * Unlock the source so we can lookup the target without deadlocking
2573 * (XXX vp is locked already, possible other deadlock?). The target
2576 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2577 nd->nl_flags &= ~NLC_NCPISLOCKED;
2578 cache_unlock(&nd->nl_nch);
2581 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2582 if ((error = nlookup(linknd)) != 0) {
2586 if (linknd->nl_nch.ncp->nc_vp) {
2590 if (linknd->nl_dvp == NULL) {
2594 VFS_MODIFYING(vp->v_mount);
2595 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
2602 * Finally run the new API VOP.
2604 error = can_hardlink(vp, td, td->td_ucred);
2606 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2607 vp, linknd->nl_cred);
2614 * link_args(char *path, char *link)
2616 * Make a hard file link.
2619 sys_link(struct sysmsg *sysmsg, const struct link_args *uap)
2621 struct nlookupdata nd, linknd;
2624 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2626 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2628 error = kern_link(&nd, &linknd);
2629 nlookup_done(&linknd);
2636 * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags)
2638 * Make a hard file link. The path1 argument is relative to the directory
2639 * associated with fd1, and similarly the path2 argument is relative to
2640 * the directory associated with fd2.
2643 sys_linkat(struct sysmsg *sysmsg, const struct linkat_args *uap)
2645 struct nlookupdata nd, linknd;
2646 struct file *fp1, *fp2;
2649 error = nlookup_init_at(&nd, &fp1, uap->fd1, uap->path1, UIO_USERSPACE,
2650 (uap->flags & AT_SYMLINK_FOLLOW) ? NLC_FOLLOW : 0);
2652 error = nlookup_init_at(&linknd, &fp2, uap->fd2,
2653 uap->path2, UIO_USERSPACE, 0);
2655 error = kern_link(&nd, &linknd);
2656 nlookup_done_at(&linknd, fp2);
2658 nlookup_done_at(&nd, fp1);
2663 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2671 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2672 if ((error = nlookup(nd)) != 0)
2674 if (nd->nl_nch.ncp->nc_vp)
2676 if (nd->nl_dvp == NULL)
2678 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2682 vattr.va_mode = mode;
2683 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2690 * symlink(char *path, char *link)
2692 * Make a symbolic link.
2695 sys_symlink(struct sysmsg *sysmsg, const struct symlink_args *uap)
2697 struct thread *td = curthread;
2698 struct nlookupdata nd;
2703 path = objcache_get(namei_oc, M_WAITOK);
2704 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2706 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2708 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2709 error = kern_symlink(&nd, path, mode);
2713 objcache_put(namei_oc, path);
2718 * symlinkat_args(char *path1, int fd, char *path2)
2720 * Make a symbolic link. The path2 argument is relative to the directory
2721 * associated with fd.
2724 sys_symlinkat(struct sysmsg *sysmsg, const struct symlinkat_args *uap)
2726 struct thread *td = curthread;
2727 struct nlookupdata nd;
2733 path1 = objcache_get(namei_oc, M_WAITOK);
2734 error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2736 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2739 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2740 error = kern_symlink(&nd, path1, mode);
2742 nlookup_done_at(&nd, fp);
2744 objcache_put(namei_oc, path1);
2749 * undelete_args(char *path)
2751 * Delete a whiteout from the filesystem.
2754 sys_undelete(struct sysmsg *sysmsg, const struct undelete_args *uap)
2756 struct nlookupdata nd;
2759 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2761 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2763 error = nlookup(&nd);
2764 if (error == 0 && nd.nl_dvp == NULL)
2767 error = ncp_writechk(&nd.nl_nch);
2769 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2777 kern_unlink(struct nlookupdata *nd)
2782 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2783 if ((error = nlookup(nd)) != 0)
2785 if (nd->nl_dvp == NULL)
2787 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2789 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2794 * unlink_args(char *path)
2796 * Delete a name from the filesystem.
2799 sys_unlink(struct sysmsg *sysmsg, const struct unlink_args *uap)
2801 struct nlookupdata nd;
2804 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2806 error = kern_unlink(&nd);
2813 * unlinkat_args(int fd, char *path, int flags)
2815 * Delete the file or directory entry pointed to by fd/path.
2818 sys_unlinkat(struct sysmsg *sysmsg, const struct unlinkat_args *uap)
2820 struct nlookupdata nd;
2824 if (uap->flags & ~AT_REMOVEDIR)
2827 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2829 if (uap->flags & AT_REMOVEDIR)
2830 error = kern_rmdir(&nd);
2832 error = kern_unlink(&nd);
2834 nlookup_done_at(&nd, fp);
2839 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2841 struct thread *td = curthread;
2844 struct vattr_lite lva;
2848 fp = holdfp(td, fd, -1);
2851 if (fp->f_type != DTYPE_VNODE) {
2855 vp = (struct vnode *)fp->f_data;
2859 spin_lock(&fp->f_spin);
2860 new_offset = fp->f_offset + offset;
2864 error = VOP_GETATTR_LITE(vp, &lva);
2865 spin_lock(&fp->f_spin);
2866 new_offset = offset + lva.va_size;
2869 new_offset = offset;
2871 spin_lock(&fp->f_spin);
2876 spin_lock(&fp->f_spin);
2881 * Validate the seek position. Negative offsets are not allowed
2882 * for regular files or directories.
2884 * Normally we would also not want to allow negative offsets for
2885 * character and block-special devices. However kvm addresses
2886 * on 64 bit architectures might appear to be negative and must
2890 if (new_offset < 0 &&
2891 (vp->v_type == VREG || vp->v_type == VDIR)) {
2894 fp->f_offset = new_offset;
2897 *res = fp->f_offset;
2898 spin_unlock(&fp->f_spin);
2906 * lseek_args(int fd, int pad, off_t offset, int whence)
2908 * Reposition read/write file offset.
2911 sys_lseek(struct sysmsg *sysmsg, const struct lseek_args *uap)
2915 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2916 &sysmsg->sysmsg_offset);
2922 * Check if current process can access given file. amode is a bitmask of *_OK
2923 * access bits. flags is a bitmask of AT_* flags.
2926 kern_access(struct nlookupdata *nd, int amode, int flags)
2931 if (flags & ~AT_EACCESS)
2933 nd->nl_flags |= NLC_SHAREDLOCK;
2934 if ((error = nlookup(nd)) != 0)
2936 if ((amode & W_OK) && (error = ncp_writechk(&nd->nl_nch)) != 0)
2939 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
2943 /* Flags == 0 means only check for existence. */
2952 if ((mode & VWRITE) == 0 ||
2953 (error = vn_writechk(vp)) == 0) {
2954 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2958 * If the file handle is stale we have to re-resolve the
2959 * entry with the ncp held exclusively. This is a hack
2962 if (error == ESTALE) {
2966 cache_unlock(&nd->nl_nch);
2967 cache_lock(&nd->nl_nch);
2968 dummy_gen = nd->nl_nch.ncp->nc_generation;
2969 cache_setunresolved(&nd->nl_nch);
2970 error = cache_resolve(&nd->nl_nch, &dummy_gen,
2984 * access_args(char *path, int flags)
2986 * Check access permissions.
2989 sys_access(struct sysmsg *sysmsg, const struct access_args *uap)
2991 struct nlookupdata nd;
2994 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2996 error = kern_access(&nd, uap->flags, 0);
3003 * eaccess_args(char *path, int flags)
3005 * Check access permissions.
3008 sys_eaccess(struct sysmsg *sysmsg, const struct eaccess_args *uap)
3010 struct nlookupdata nd;
3013 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3015 error = kern_access(&nd, uap->flags, AT_EACCESS);
3022 * faccessat_args(int fd, char *path, int amode, int flags)
3024 * Check access permissions.
3027 sys_faccessat(struct sysmsg *sysmsg, const struct faccessat_args *uap)
3029 struct nlookupdata nd;
3033 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
3036 error = kern_access(&nd, uap->amode, uap->flags);
3037 nlookup_done_at(&nd, fp);
3042 kern_stat(struct nlookupdata *nd, struct stat *st)
3047 nd->nl_flags |= NLC_SHAREDLOCK;
3048 if ((error = nlookup(nd)) != 0)
3051 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
3055 error = cache_vref(&nd->nl_nch, NULL, &vp);
3057 error = vget(vp, LK_SHARED);
3061 error = vn_stat(vp, st, nd->nl_cred);
3064 * If the file handle is stale we have to re-resolve the
3065 * entry with the ncp held exclusively. This is a hack
3068 if (error == ESTALE) {
3075 cache_unlock(&nd->nl_nch);
3076 cache_lock(&nd->nl_nch);
3077 dummy_gen = nd->nl_nch.ncp->nc_generation;
3078 cache_setunresolved(&nd->nl_nch);
3079 error = cache_resolve(&nd->nl_nch, &dummy_gen, nd->nl_cred);
3093 * stat_args(char *path, struct stat *ub)
3095 * Get file status; this version follows links.
3098 sys_stat(struct sysmsg *sysmsg, const struct stat_args *uap)
3100 struct nlookupdata nd;
3104 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3106 error = kern_stat(&nd, &st);
3108 error = copyout(&st, uap->ub, sizeof(*uap->ub));
3115 * lstat_args(char *path, struct stat *ub)
3117 * Get file status; this version does not follow links.
3120 sys_lstat(struct sysmsg *sysmsg, const struct lstat_args *uap)
3122 struct nlookupdata nd;
3126 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3128 error = kern_stat(&nd, &st);
3130 error = copyout(&st, uap->ub, sizeof(*uap->ub));
3137 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
3139 * Get status of file pointed to by fd/path.
3142 sys_fstatat(struct sysmsg *sysmsg, const struct fstatat_args *uap)
3144 struct nlookupdata nd;
3150 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3153 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3155 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3156 UIO_USERSPACE, flags);
3158 error = kern_stat(&nd, &st);
3160 error = copyout(&st, uap->sb, sizeof(*uap->sb));
3162 nlookup_done_at(&nd, fp);
3167 kern_pathconf(char *path, int name, int flags, register_t *sysmsg_regp)
3169 struct nlookupdata nd;
3174 error = nlookup_init(&nd, path, UIO_USERSPACE, flags);
3176 error = nlookup(&nd);
3178 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3181 error = VOP_PATHCONF(vp, name, sysmsg_regp);
3188 * pathconf_Args(char *path, int name)
3190 * Get configurable pathname variables.
3193 sys_pathconf(struct sysmsg *sysmsg, const struct pathconf_args *uap)
3195 return (kern_pathconf(uap->path, uap->name, NLC_FOLLOW,
3196 &sysmsg->sysmsg_reg));
3200 * lpathconf_Args(char *path, int name)
3202 * Get configurable pathname variables, but don't follow symlinks.
3205 sys_lpathconf(struct sysmsg *sysmsg, const struct lpathconf_args *uap)
3207 return (kern_pathconf(uap->path, uap->name, 0, &sysmsg->sysmsg_reg));
3212 * kern_readlink isn't properly split yet. There is a copyin burried
3213 * in VOP_READLINK().
3216 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
3218 struct thread *td = curthread;
3224 nd->nl_flags |= NLC_SHAREDLOCK;
3225 if ((error = nlookup(nd)) != 0)
3227 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
3230 if (vp->v_type != VLNK) {
3233 aiov.iov_base = buf;
3234 aiov.iov_len = count;
3235 auio.uio_iov = &aiov;
3236 auio.uio_iovcnt = 1;
3237 auio.uio_offset = 0;
3238 auio.uio_rw = UIO_READ;
3239 auio.uio_segflg = UIO_USERSPACE;
3241 auio.uio_resid = count;
3242 error = VOP_READLINK(vp, &auio, td->td_ucred);
3245 *res = count - auio.uio_resid;
3250 * readlink_args(char *path, char *buf, int count)
3252 * Return target name of a symbolic link.
3255 sys_readlink(struct sysmsg *sysmsg, const struct readlink_args *uap)
3257 struct nlookupdata nd;
3260 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3262 error = kern_readlink(&nd, uap->buf, uap->count,
3263 &sysmsg->sysmsg_result);
3270 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
3272 * Return target name of a symbolic link. The path is relative to the
3273 * directory associated with fd.
3276 sys_readlinkat(struct sysmsg *sysmsg, const struct readlinkat_args *uap)
3278 struct nlookupdata nd;
3282 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
3284 error = kern_readlink(&nd, uap->buf, uap->bufsize,
3285 &sysmsg->sysmsg_result);
3287 nlookup_done_at(&nd, fp);
3292 setfflags(struct vnode *vp, u_long flags)
3294 struct thread *td = curthread;
3299 * Prevent non-root users from setting flags on devices. When
3300 * a device is reused, users can retain ownership of the device
3301 * if they are allowed to set flags and programs assume that
3302 * chown can't fail when done as root.
3304 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
3306 caps_priv_check(td->td_ucred, SYSCAP_NOVFS_CHFLAGS_DEV)) != 0))
3312 * note: vget is required for any operation that might mod the vnode
3313 * so VINACTIVE is properly cleared.
3315 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3317 vattr.va_flags = flags;
3318 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3325 * chflags(const char *path, u_long flags)
3327 * Change flags of a file given a path name.
3330 sys_chflags(struct sysmsg *sysmsg, const struct chflags_args *uap)
3332 struct nlookupdata nd;
3337 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3339 error = nlookup(&nd);
3341 error = ncp_writechk(&nd.nl_nch);
3343 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3346 error = setfflags(vp, uap->flags);
3353 * lchflags(const char *path, u_long flags)
3355 * Change flags of a file given a path name, but don't follow symlinks.
3358 sys_lchflags(struct sysmsg *sysmsg, const struct lchflags_args *uap)
3360 struct nlookupdata nd;
3365 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3367 error = nlookup(&nd);
3369 error = ncp_writechk(&nd.nl_nch);
3371 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3374 error = setfflags(vp, uap->flags);
3381 * fchflags_args(int fd, u_flags flags)
3383 * Change flags of a file given a file descriptor.
3386 sys_fchflags(struct sysmsg *sysmsg, const struct fchflags_args *uap)
3388 struct thread *td = curthread;
3392 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
3394 if (fp->f_nchandle.ncp)
3395 error = ncp_writechk(&fp->f_nchandle);
3397 error = setfflags((struct vnode *) fp->f_data, uap->flags);
3403 * chflagsat_args(int fd, const char *path, u_long flags, int atflags)
3404 * change flags given a pathname relative to a filedescriptor
3407 sys_chflagsat(struct sysmsg *sysmsg, const struct chflagsat_args *uap)
3409 struct nlookupdata nd;
3415 if (uap->atflags & ~AT_SYMLINK_NOFOLLOW)
3418 lookupflags = (uap->atflags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3421 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, lookupflags);
3423 error = nlookup(&nd);
3425 error = ncp_writechk(&nd.nl_nch);
3427 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3428 nlookup_done_at(&nd, fp);
3430 error = setfflags(vp, uap->flags);
3438 setfmode(struct vnode *vp, int mode)
3440 struct thread *td = curthread;
3445 * note: vget is required for any operation that might mod the vnode
3446 * so VINACTIVE is properly cleared.
3448 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3450 vattr.va_mode = mode & ALLPERMS;
3451 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3452 cache_inval_wxok(vp);
3459 kern_chmod(struct nlookupdata *nd, int mode)
3464 if ((error = nlookup(nd)) != 0)
3466 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3468 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3469 error = setfmode(vp, mode);
3475 * chmod_args(char *path, int mode)
3477 * Change mode of a file given path name.
3480 sys_chmod(struct sysmsg *sysmsg, const struct chmod_args *uap)
3482 struct nlookupdata nd;
3485 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3487 error = kern_chmod(&nd, uap->mode);
3493 * lchmod_args(char *path, int mode)
3495 * Change mode of a file given path name (don't follow links.)
3498 sys_lchmod(struct sysmsg *sysmsg, const struct lchmod_args *uap)
3500 struct nlookupdata nd;
3503 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3505 error = kern_chmod(&nd, uap->mode);
3511 * fchmod_args(int fd, int mode)
3513 * Change mode of a file given a file descriptor.
3516 sys_fchmod(struct sysmsg *sysmsg, const struct fchmod_args *uap)
3518 struct thread *td = curthread;
3522 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
3524 if (fp->f_nchandle.ncp)
3525 error = ncp_writechk(&fp->f_nchandle);
3527 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3533 * fchmodat_args(char *path, int mode)
3535 * Change mode of a file pointed to by fd/path.
3538 sys_fchmodat(struct sysmsg *sysmsg, const struct fchmodat_args *uap)
3540 struct nlookupdata nd;
3545 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3547 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3549 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3550 UIO_USERSPACE, flags);
3552 error = kern_chmod(&nd, uap->mode);
3553 nlookup_done_at(&nd, fp);
3558 setfown(struct mount *mp, struct vnode *vp, uid_t uid, gid_t gid)
3560 struct thread *td = curthread;
3568 * note: vget is required for any operation that might mod the vnode
3569 * so VINACTIVE is properly cleared.
3571 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3572 if ((error = VOP_GETATTR(vp, &vattr)) != 0)
3574 o_uid = vattr.va_uid;
3575 o_gid = vattr.va_gid;
3576 size = vattr.va_size;
3581 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3590 VFS_ACCOUNT(mp, o_uid, o_gid, -size);
3591 VFS_ACCOUNT(mp, uid, gid, size);
3598 kern_chown(struct nlookupdata *nd, int uid, int gid)
3603 if ((error = nlookup(nd)) != 0)
3605 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3607 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3608 error = setfown(nd->nl_nch.mount, vp, uid, gid);
3614 * chown(char *path, int uid, int gid)
3616 * Set ownership given a path name.
3619 sys_chown(struct sysmsg *sysmsg, const struct chown_args *uap)
3621 struct nlookupdata nd;
3624 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3626 error = kern_chown(&nd, uap->uid, uap->gid);
3632 * lchown_args(char *path, int uid, int gid)
3634 * Set ownership given a path name, do not cross symlinks.
3637 sys_lchown(struct sysmsg *sysmsg, const struct lchown_args *uap)
3639 struct nlookupdata nd;
3642 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3644 error = kern_chown(&nd, uap->uid, uap->gid);
3650 * fchown_args(int fd, int uid, int gid)
3652 * Set ownership given a file descriptor.
3655 sys_fchown(struct sysmsg *sysmsg, const struct fchown_args *uap)
3657 struct thread *td = curthread;
3658 struct proc *p = td->td_proc;
3662 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
3664 if (fp->f_nchandle.ncp)
3665 error = ncp_writechk(&fp->f_nchandle);
3667 error = setfown(p->p_fd->fd_ncdir.mount,
3668 (struct vnode *)fp->f_data, uap->uid, uap->gid);
3674 * fchownat(int fd, char *path, int uid, int gid, int flags)
3676 * Set ownership of file pointed to by fd/path.
3679 sys_fchownat(struct sysmsg *sysmsg, const struct fchownat_args *uap)
3681 struct nlookupdata nd;
3686 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3688 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3690 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3691 UIO_USERSPACE, flags);
3693 error = kern_chown(&nd, uap->uid, uap->gid);
3694 nlookup_done_at(&nd, fp);
3700 getutimes(struct timeval *tvp, struct timespec *tsp)
3702 struct timeval tv[2];
3707 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3710 if ((error = itimerfix(tvp)) != 0)
3712 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3713 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3719 getutimens(const struct timespec *ts, struct timespec *newts, int *nullflag)
3721 struct timespec tsnow;
3735 if (newts[0].tv_nsec == UTIME_OMIT && newts[1].tv_nsec == UTIME_OMIT)
3737 if (newts[0].tv_nsec == UTIME_NOW && newts[1].tv_nsec == UTIME_NOW)
3740 if (newts[0].tv_nsec == UTIME_OMIT)
3741 newts[0].tv_sec = VNOVAL;
3742 else if (newts[0].tv_nsec == UTIME_NOW)
3744 else if ((error = itimespecfix(&newts[0])) != 0)
3747 if (newts[1].tv_nsec == UTIME_OMIT)
3748 newts[1].tv_sec = VNOVAL;
3749 else if (newts[1].tv_nsec == UTIME_NOW)
3751 else if ((error = itimespecfix(&newts[1])) != 0)
3758 setutimes(struct vnode *vp, struct vattr *vattr,
3759 const struct timespec *ts, int nullflag)
3761 struct thread *td = curthread;
3765 vattr->va_atime = ts[0];
3766 vattr->va_mtime = ts[1];
3768 vattr->va_vaflags |= VA_UTIMES_NULL;
3769 error = VOP_SETATTR(vp, vattr, td->td_ucred);
3775 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3777 struct timespec ts[2];
3781 if ((error = getutimes(tptr, ts)) != 0)
3784 error = kern_utimensat(nd, tptr ? ts : NULL, 0);
3789 * utimes_args(char *path, struct timeval *tptr)
3791 * Set the access and modification times of a file.
3794 sys_utimes(struct sysmsg *sysmsg, const struct utimes_args *uap)
3796 struct timeval tv[2];
3797 struct nlookupdata nd;
3801 error = copyin(uap->tptr, tv, sizeof(tv));
3805 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3807 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3813 * lutimes_args(char *path, struct timeval *tptr)
3815 * Set the access and modification times of a file.
3818 sys_lutimes(struct sysmsg *sysmsg, const struct lutimes_args *uap)
3820 struct timeval tv[2];
3821 struct nlookupdata nd;
3825 error = copyin(uap->tptr, tv, sizeof(tv));
3829 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3831 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3837 * Set utimes on a file descriptor. The creds used to open the
3838 * file are used to determine whether the operation is allowed
3842 kern_futimens(int fd, struct timespec *ts)
3844 struct thread *td = curthread;
3845 struct timespec newts[2];
3849 struct vattr_lite lva;
3853 error = getutimens(ts, newts, &nullflag);
3856 if ((error = holdvnode(td, fd, &fp)) != 0)
3858 if (fp->f_nchandle.ncp)
3859 error = ncp_writechk(&fp->f_nchandle);
3862 error = vget(vp, LK_EXCLUSIVE);
3864 error = VOP_GETATTR_FP(vp, &vattr, fp);
3866 lva.va_type = vattr.va_type;
3867 lva.va_nlink = vattr.va_nlink;
3868 lva.va_mode = vattr.va_mode;
3869 lva.va_uid = vattr.va_uid;
3870 lva.va_gid = vattr.va_gid;
3871 lva.va_size = vattr.va_size;
3872 lva.va_flags = vattr.va_flags;
3874 error = naccess_lva(&lva, NLC_OWN | NLC_WRITE,
3878 error = setutimes(vp, &vattr, newts, nullflag);
3888 * futimens_args(int fd, struct timespec *ts)
3890 * Set the access and modification times of a file.
3893 sys_futimens(struct sysmsg *sysmsg, const struct futimens_args *uap)
3895 struct timespec ts[2];
3899 error = copyin(uap->ts, ts, sizeof(ts));
3903 error = kern_futimens(uap->fd, uap->ts ? ts : NULL);
3908 kern_futimes(int fd, struct timeval *tptr)
3910 struct timespec ts[2];
3914 if ((error = getutimes(tptr, ts)) != 0)
3917 error = kern_futimens(fd, tptr ? ts : NULL);
3922 * futimes_args(int fd, struct timeval *tptr)
3924 * Set the access and modification times of a file.
3927 sys_futimes(struct sysmsg *sysmsg, const struct futimes_args *uap)
3929 struct timeval tv[2];
3933 error = copyin(uap->tptr, tv, sizeof(tv));
3937 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3942 kern_utimensat(struct nlookupdata *nd, const struct timespec *ts, int flags)
3944 struct timespec newts[2];
3950 if (flags & ~AT_SYMLINK_NOFOLLOW)
3953 error = getutimens(ts, newts, &nullflag);
3957 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3958 if ((error = nlookup(nd)) != 0)
3960 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3962 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3964 if ((error = vn_writechk(vp)) == 0) {
3965 error = vget(vp, LK_EXCLUSIVE);
3967 error = setutimes(vp, &vattr, newts, nullflag);
3976 * utimensat_args(int fd, const char *path, const struct timespec *ts, int flags);
3978 * Set file access and modification times of a file.
3981 sys_utimensat(struct sysmsg *sysmsg, const struct utimensat_args *uap)
3983 struct timespec ts[2];
3984 struct nlookupdata nd;
3990 error = copyin(uap->ts, ts, sizeof(ts));
3995 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3996 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3997 UIO_USERSPACE, flags);
3999 error = kern_utimensat(&nd, uap->ts ? ts : NULL, uap->flags);
4000 nlookup_done_at(&nd, fp);
4005 kern_truncate(struct nlookupdata *nd, off_t length)
4012 uint64_t old_size = 0;
4016 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
4017 if ((error = nlookup(nd)) != 0)
4019 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4021 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
4023 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
4028 if (vp->v_type == VDIR) {
4032 if (vfs_quota_enabled) {
4033 error = VOP_GETATTR(vp, &vattr);
4034 KASSERT(error == 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
4037 old_size = vattr.va_size;
4040 if ((error = vn_writechk(vp)) == 0) {
4042 vattr.va_size = length;
4043 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
4044 VFS_ACCOUNT(nd->nl_nch.mount, uid, gid, length - old_size);
4052 * truncate(char *path, int pad, off_t length)
4054 * Truncate a file given its path name.
4057 sys_truncate(struct sysmsg *sysmsg, const struct truncate_args *uap)
4059 struct nlookupdata nd;
4062 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4064 error = kern_truncate(&nd, uap->length);
4070 kern_ftruncate(int fd, off_t length)
4072 struct thread *td = curthread;
4079 uint64_t old_size = 0;
4084 if ((error = holdvnode(td, fd, &fp)) != 0)
4086 if (fp->f_nchandle.ncp) {
4087 error = ncp_writechk(&fp->f_nchandle);
4091 if ((fp->f_flag & FWRITE) == 0) {
4095 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
4099 vp = (struct vnode *)fp->f_data;
4100 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4101 if (vp->v_type == VDIR) {
4107 if (vfs_quota_enabled) {
4108 error = VOP_GETATTR_FP(vp, &vattr, fp);
4109 KASSERT(error == 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
4112 old_size = vattr.va_size;
4115 if ((error = vn_writechk(vp)) == 0) {
4117 vattr.va_size = length;
4118 error = VOP_SETATTR_FP(vp, &vattr, fp->f_cred, fp);
4120 VFS_ACCOUNT(mp, uid, gid, length - old_size);
4129 * ftruncate_args(int fd, int pad, off_t length)
4131 * Truncate a file given a file descriptor.
4134 sys_ftruncate(struct sysmsg *sysmsg, const struct ftruncate_args *uap)
4138 error = kern_ftruncate(uap->fd, uap->length);
4144 kern_fsync(int fd, bool fullsync)
4146 struct thread *td = curthread;
4152 if ((error = holdvnode(td, fd, &fp)) != 0)
4154 vp = (struct vnode *)fp->f_data;
4155 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4156 if ((obj = vp->v_object) != NULL) {
4157 if (vp->v_mount == NULL ||
4158 (vp->v_mount->mnt_kern_flag & MNTK_NOMSYNC) == 0) {
4159 vm_object_page_clean(obj, 0, 0, 0);
4163 VOP_FSYNC_FP(vp, MNT_WAIT, VOP_FSYNC_SYSCALL, fp) :
4164 VOP_FDATASYNC_FP(vp, MNT_WAIT, VOP_FSYNC_SYSCALL, fp);
4165 if (error == 0 && vp->v_mount)
4166 error = buf_fsync(vp);
4176 * Sync an open file.
4179 sys_fsync(struct sysmsg *sysmsg, const struct fsync_args *uap)
4181 return (kern_fsync(uap->fd, true));
4187 * Data-sync an open file.
4190 sys_fdatasync(struct sysmsg *sysmsg, const struct fdatasync_args *uap)
4192 return (kern_fsync(uap->fd, false));
4198 * NOTE: error == 0 and nl_dvp is NULL indicates a mount point, operation
4199 * disallowed. e.g. /var/cache where /var/cache is a null-mount, for
4203 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
4205 struct nchandle fnchd;
4206 struct nchandle tnchd;
4207 struct namecache *ncp;
4211 struct mount *userenlk;
4217 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
4218 if ((error = nlookup(fromnd)) != 0)
4222 * Attempt to rename a mount point (from or to)
4224 if (error == 0 && fromnd->nl_dvp == NULL)
4227 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
4229 fnchd.mount = fromnd->nl_nch.mount;
4233 * unlock the source nch so we can lookup the target nch without
4234 * deadlocking. The target may or may not exist so we do not check
4235 * for a target vp like kern_mkdir() and other creation functions do.
4237 * The source and target directories are ref'd and rechecked after
4238 * everything is relocked to determine if the source or target file
4241 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
4242 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
4243 fncp_gen = fromnd->nl_nch.ncp->nc_generation;
4245 if (fromnd->nl_nch.ncp->nc_vp &&
4246 fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
4247 userenlk = fnchd.mount;
4248 cache_unlock(&fromnd->nl_nch);
4249 lockmgr(&userenlk->mnt_renlock, LK_EXCLUSIVE);
4252 cache_unlock(&fromnd->nl_nch);
4258 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
4259 if ((error = nlookup(tond)) != 0) {
4263 tncp_gen = tond->nl_nch.ncp->nc_generation;
4266 * Attempt to rename a mount point (from or to)
4268 if (error == 0 && tond->nl_dvp == NULL) {
4274 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
4279 tnchd.mount = tond->nl_nch.mount;
4283 * If the source and target are the same there is nothing to do
4285 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
4293 * Mount points cannot be renamed or overwritten
4295 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
4305 * Lock all four namecache entries. tond is already locked.
4307 cache_lock4_tondlocked(&fnchd, &fromnd->nl_nch,
4308 &tnchd, &tond->nl_nch,
4309 fromnd->nl_cred, tond->nl_cred);
4310 fromnd->nl_flags |= NLC_NCPISLOCKED;
4313 * If the namecache generation changed for either fromnd or tond,
4316 if (((fromnd->nl_nch.ncp->nc_generation - fncp_gen) & ~1) ||
4317 ((tond->nl_nch.ncp->nc_generation - tncp_gen) & ~1))
4319 krateprintf(&krate_rename,
4320 "kern_rename: retry due to race on: "
4321 "\"%s\" -> \"%s\" (%d,%d)\n",
4322 fromnd->nl_nch.ncp->nc_name,
4323 tond->nl_nch.ncp->nc_name,
4324 fromnd->nl_nch.ncp->nc_generation - fncp_gen,
4325 tond->nl_nch.ncp->nc_generation - tncp_gen);
4331 * If either fromnd or tond are marked destroyed a ripout occured
4332 * out from under us and we must retry.
4334 if ((fromnd->nl_nch.ncp->nc_flag & (NCF_DESTROYED | NCF_UNRESOLVED)) ||
4335 fromnd->nl_nch.ncp->nc_vp == NULL ||
4336 (tond->nl_nch.ncp->nc_flag & (NCF_DESTROYED | NCF_UNRESOLVED))) {
4337 krateprintf(&krate_rename,
4338 "kern_rename: retry due to ripout on: "
4339 "\"%s\" -> \"%s\"\n",
4340 fromnd->nl_nch.ncp->nc_name,
4341 tond->nl_nch.ncp->nc_name);
4347 * Make sure the parent directories linkages are the same. We have
4348 * already checked that fromnd and tond are not mount points so this
4349 * should not loop forever on a cross-mount.
4351 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
4352 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
4358 * Both the source and target must be within the same filesystem and
4359 * in the same filesystem as their parent directories within the
4360 * namecache topology.
4362 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
4365 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
4366 mp != tond->nl_nch.mount) {
4372 * Make sure the mount point is writable
4374 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
4379 * If the target exists and either the source or target is a directory,
4380 * then both must be directories.
4382 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
4385 if (tond->nl_nch.ncp->nc_vp) {
4386 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
4388 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
4389 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
4391 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
4397 * You cannot rename a source into itself or a subdirectory of itself.
4398 * We check this by travsersing the target directory upwards looking
4399 * for a match against the source.
4401 * Only required when renaming a directory, in which case userenlk is
4404 if (__predict_false(userenlk && error == 0)) {
4405 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
4406 if (fromnd->nl_nch.ncp == ncp) {
4414 * Even though the namespaces are different, they may still represent
4415 * hardlinks to the same file. The filesystem might have a hard time
4416 * with this so we issue a NREMOVE of the source instead of a NRENAME
4417 * when we detect the situation.
4420 fdvp = fromnd->nl_dvp;
4421 tdvp = tond->nl_dvp;
4422 if (fdvp == NULL || tdvp == NULL) {
4424 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
4425 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
4428 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
4429 fdvp, tdvp, tond->nl_cred);
4437 lockmgr(&userenlk->mnt_renlock, LK_RELEASE);
4442 * rename_args(char *from, char *to)
4444 * Rename files. Source and destination must either both be directories,
4445 * or both not be directories. If target is a directory, it must be empty.
4448 sys_rename(struct sysmsg *sysmsg, const struct rename_args *uap)
4450 struct nlookupdata fromnd, tond;
4454 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
4456 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
4458 error = kern_rename(&fromnd, &tond);
4459 nlookup_done(&tond);
4461 nlookup_done(&fromnd);
4462 } while (error == EAGAIN);
4467 * renameat_args(int oldfd, char *old, int newfd, char *new)
4469 * Rename files using paths relative to the directories associated with
4470 * oldfd and newfd. Source and destination must either both be directories,
4471 * or both not be directories. If target is a directory, it must be empty.
4474 sys_renameat(struct sysmsg *sysmsg, const struct renameat_args *uap)
4476 struct nlookupdata oldnd, newnd;
4477 struct file *oldfp, *newfp;
4481 error = nlookup_init_at(&oldnd, &oldfp,
4482 uap->oldfd, uap->old,
4485 error = nlookup_init_at(&newnd, &newfp,
4486 uap->newfd, uap->new,
4489 error = kern_rename(&oldnd, &newnd);
4490 nlookup_done_at(&newnd, newfp);
4492 nlookup_done_at(&oldnd, oldfp);
4493 } while (error == EAGAIN);
4498 kern_mkdir(struct nlookupdata *nd, int mode)
4500 struct thread *td = curthread;
4501 struct proc *p = td->td_proc;
4507 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
4508 if ((error = nlookup(nd)) != 0)
4511 if (nd->nl_nch.ncp->nc_vp)
4513 if (nd->nl_dvp == NULL)
4515 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4518 vattr.va_type = VDIR;
4519 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
4522 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
4529 * mkdir_args(char *path, int mode)
4531 * Make a directory file.
4534 sys_mkdir(struct sysmsg *sysmsg, const struct mkdir_args *uap)
4536 struct nlookupdata nd;
4539 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4541 error = kern_mkdir(&nd, uap->mode);
4547 * mkdirat_args(int fd, char *path, mode_t mode)
4549 * Make a directory file. The path is relative to the directory associated
4553 sys_mkdirat(struct sysmsg *sysmsg, const struct mkdirat_args *uap)
4555 struct nlookupdata nd;
4559 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
4561 error = kern_mkdir(&nd, uap->mode);
4562 nlookup_done_at(&nd, fp);
4567 kern_rmdir(struct nlookupdata *nd)
4572 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
4573 if ((error = nlookup(nd)) != 0)
4577 * Do not allow directories representing mount points to be
4578 * deleted, even if empty. Check write perms on mount point
4579 * in case the vnode is aliased (aka nullfs).
4581 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
4583 if (nd->nl_dvp == NULL)
4585 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4587 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
4592 * rmdir_args(char *path)
4594 * Remove a directory file.
4597 sys_rmdir(struct sysmsg *sysmsg, const struct rmdir_args *uap)
4599 struct nlookupdata nd;
4602 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4604 error = kern_rmdir(&nd);
4610 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
4611 enum uio_seg direction)
4613 struct thread *td = curthread;
4621 if ((error = holdvnode(td, fd, &fp)) != 0)
4623 if ((fp->f_flag & FREAD) == 0) {
4627 vp = (struct vnode *)fp->f_data;
4628 if (vp->v_type != VDIR) {
4632 aiov.iov_base = buf;
4633 aiov.iov_len = count;
4634 auio.uio_iov = &aiov;
4635 auio.uio_iovcnt = 1;
4636 auio.uio_rw = UIO_READ;
4637 auio.uio_segflg = direction;
4639 auio.uio_resid = count;
4640 loff = auio.uio_offset = fp->f_offset;
4641 error = VOP_READDIR_FP(vp, &auio, fp->f_cred, &eofflag, NULL, NULL, fp);
4642 fp->f_offset = auio.uio_offset;
4647 * WARNING! *basep may not be wide enough to accomodate the
4648 * seek offset. XXX should we hack this to return the upper 32 bits
4649 * for offsets greater then 4G?
4652 *basep = (long)loff;
4654 *res = count - auio.uio_resid;
4661 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4663 * Read a block of directory entries in a file system independent format.
4666 sys_getdirentries(struct sysmsg *sysmsg, const struct getdirentries_args *uap)
4671 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
4672 &sysmsg->sysmsg_result, UIO_USERSPACE);
4674 if (error == 0 && uap->basep)
4675 error = copyout(&base, uap->basep, sizeof(*uap->basep));
4680 * getdents_args(int fd, char *buf, size_t count)
4683 sys_getdents(struct sysmsg *sysmsg, const struct getdents_args *uap)
4687 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
4688 &sysmsg->sysmsg_result, UIO_USERSPACE);
4694 * Set the mode mask for creation of filesystem nodes.
4696 * umask(int newmask)
4699 sys_umask(struct sysmsg *sysmsg, const struct umask_args *uap)
4701 struct thread *td = curthread;
4702 struct proc *p = td->td_proc;
4703 struct filedesc *fdp;
4706 sysmsg->sysmsg_result = fdp->fd_cmask;
4707 fdp->fd_cmask = uap->newmask & ALLPERMS;
4712 * revoke(char *path)
4714 * Void all references to file by ripping underlying filesystem
4718 sys_revoke(struct sysmsg *sysmsg, const struct revoke_args *uap)
4720 struct nlookupdata nd;
4727 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4729 error = nlookup(&nd);
4731 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4732 cred = crhold(nd.nl_cred);
4736 error = VOP_GETATTR(vp, &vattr);
4737 if (error == 0 && cred->cr_uid != vattr.va_uid)
4738 error = caps_priv_check(cred, SYSCAP_NOVFS_REVOKE);
4739 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4741 error = vrevoke(vp, cred);
4742 } else if (error == 0) {
4743 error = vrevoke(vp, cred);
4753 * getfh_args(char *fname, fhandle_t *fhp)
4755 * Get (NFS) file handle
4757 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4758 * mount. This allows nullfs mounts to be explicitly exported.
4760 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4762 * nullfs mounts of subdirectories are not safe. That is, it will
4763 * work, but you do not really have protection against access to
4764 * the related parent directories.
4767 sys_getfh(struct sysmsg *sysmsg, const struct getfh_args *uap)
4769 struct nlookupdata nd;
4776 * Must be super user
4778 if ((error = caps_priv_check_self(SYSCAP_RESTRICTEDROOT)) != 0)
4782 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4784 error = nlookup(&nd);
4786 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4787 mp = nd.nl_nch.mount;
4790 bzero(&fh, sizeof(fh));
4791 fh.fh_fsid = mp->mnt_stat.f_fsid;
4792 error = VFS_VPTOFH(vp, &fh.fh_fid);
4795 error = copyout(&fh, uap->fhp, sizeof(fh));
4801 * fhopen_args(const struct fhandle *u_fhp, int flags)
4803 * syscall for the rpc.lockd to use to translate a NFS file handle into
4804 * an open descriptor.
4806 * WARNING: Do not remove the caps_priv_check() call or this becomes
4807 * one giant security hole.
4810 sys_fhopen(struct sysmsg *sysmsg, const struct fhopen_args *uap)
4812 struct thread *td = curthread;
4813 struct filedesc *fdp = td->td_proc->p_fd;
4818 struct vattr *vap = &vat;
4820 int fmode, mode, error = 0, type;
4826 * Must be super user
4828 error = caps_priv_check_td(td, SYSCAP_RESTRICTEDROOT);
4832 fmode = FFLAGS(uap->flags);
4835 * Why not allow a non-read/write open for our lockd?
4837 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4839 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4844 * Find the mount point
4846 mp = vfs_getvfs(&fhp.fh_fsid);
4851 /* now give me my vnode, it gets returned to me locked */
4852 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4856 * from now on we have to make sure not
4857 * to forget about the vnode
4858 * any error that causes an abort must vput(vp)
4859 * just set error = err and 'goto bad;'.
4865 if (vp->v_type == VLNK) {
4869 if (vp->v_type == VSOCK) {
4874 if (fmode & (FWRITE | O_TRUNC)) {
4875 if (vp->v_type == VDIR) {
4879 error = vn_writechk(vp);
4887 error = VOP_ACCESS(vp, mode, td->td_ucred);
4891 if (fmode & O_TRUNC) {
4892 vn_unlock(vp); /* XXX */
4893 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4896 error = VOP_SETATTR(vp, vap, td->td_ucred);
4902 * VOP_OPEN needs the file pointer so it can potentially override
4905 * WARNING! no f_nchandle will be associated when fhopen()ing a
4908 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4910 error = VOP_OPEN(vp, fmode, td->td_ucred, &nfp);
4915 * setting f_ops this way prevents VOP_CLOSE from being
4916 * called or fdrop() releasing the vp from v_data. Since
4917 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4919 fp->f_ops = &badfileops;
4925 * The fp is given its own reference, we still have our ref and lock.
4927 * Assert that all regular files must be created with a VM object.
4929 if (vp->v_type == VREG && vp->v_object == NULL) {
4930 kprintf("fhopen: regular file did not "
4931 "have VM object: %p\n",
4937 * The open was successful. Handle any locking requirements.
4939 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4940 lf.l_whence = SEEK_SET;
4943 if (fmode & O_EXLOCK)
4944 lf.l_type = F_WRLCK;
4946 lf.l_type = F_RDLCK;
4947 if (fmode & FNONBLOCK)
4952 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK,
4955 * release our private reference.
4957 fsetfd(fdp, NULL, indx);
4962 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4963 atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */
4967 * Clean up. Associate the file pointer with the previously
4968 * reserved descriptor and return it.
4971 if (uap->flags & O_CLOEXEC)
4972 fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
4973 fsetfd(fdp, fp, indx);
4975 sysmsg->sysmsg_result = indx;
4981 fsetfd(fdp, NULL, indx);
4992 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4995 sys_fhstat(struct sysmsg *sysmsg, const struct fhstat_args *uap)
4997 struct thread *td = curthread;
5005 * Must be super user
5007 error = caps_priv_check_td(td, SYSCAP_RESTRICTEDROOT);
5011 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
5015 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
5018 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
5019 error = vn_stat(vp, &sb, td->td_ucred);
5024 error = copyout(&sb, uap->sb, sizeof(sb));
5032 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
5035 sys_fhstatfs(struct sysmsg *sysmsg, const struct fhstatfs_args *uap)
5037 struct thread *td = curthread;
5038 struct proc *p = td->td_proc;
5043 char *fullpath, *freepath;
5048 * Must be super user
5050 error = caps_priv_check_td(td, SYSCAP_RESTRICTEDROOT);
5054 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
5057 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
5061 if (p != NULL && !chroot_visible_mnt(mp, p)) {
5066 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
5071 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
5074 error = mount_path(p, mp, &fullpath, &freepath);
5077 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
5078 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
5079 kfree(freepath, M_TEMP);
5081 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
5082 if (caps_priv_check_td(td, SYSCAP_RESTRICTEDROOT)) {
5083 bcopy(sp, &sb, sizeof(sb));
5084 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
5087 error = copyout(sp, uap->buf, sizeof(*sp));
5096 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
5099 sys_fhstatvfs(struct sysmsg *sysmsg, const struct fhstatvfs_args *uap)
5101 struct thread *td = curthread;
5102 struct proc *p = td->td_proc;
5110 * Must be super user
5112 if ((error = caps_priv_check_td(td, SYSCAP_RESTRICTEDROOT)))
5115 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
5118 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
5122 if (p != NULL && !chroot_visible_mnt(mp, p)) {
5127 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
5130 sp = &mp->mnt_vstat;
5132 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
5136 if (mp->mnt_flag & MNT_RDONLY)
5137 sp->f_flag |= ST_RDONLY;
5138 if (mp->mnt_flag & MNT_NOSUID)
5139 sp->f_flag |= ST_NOSUID;
5140 error = copyout(sp, uap->buf, sizeof(*sp));
5149 * Syscall to push extended attribute configuration information into the
5150 * VFS. Accepts a path, which it converts to a mountpoint, as well as
5151 * a command (int cmd), and attribute name and misc data. For now, the
5152 * attribute name is left in userspace for consumption by the VFS_op.
5153 * It will probably be changed to be copied into sysspace by the
5154 * syscall in the future, once issues with various consumers of the
5155 * attribute code have raised their hands.
5157 * Currently this is used only by UFS Extended Attributes.
5160 sys_extattrctl(struct sysmsg *sysmsg, const struct extattrctl_args *uap)
5162 struct nlookupdata nd;
5164 char attrname[EXTATTR_MAXNAMELEN];
5172 if (error == 0 && uap->filename) {
5173 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
5176 error = nlookup(&nd);
5178 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
5182 if (error == 0 && uap->attrname) {
5183 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
5188 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
5190 error = nlookup(&nd);
5192 error = ncp_writechk(&nd.nl_nch);
5194 error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
5196 uap->attrname, nd.nl_cred);
5205 * Syscall to get a named extended attribute on a file or directory.
5208 sys_extattr_set_file(struct sysmsg *sysmsg,
5209 const struct extattr_set_file_args *uap)
5211 char attrname[EXTATTR_MAXNAMELEN];
5212 struct nlookupdata nd;
5218 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
5224 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
5226 error = nlookup(&nd);
5228 error = ncp_writechk(&nd.nl_nch);
5230 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
5236 bzero(&auio, sizeof(auio));
5237 aiov.iov_base = uap->data;
5238 aiov.iov_len = uap->nbytes;
5239 auio.uio_iov = &aiov;
5240 auio.uio_iovcnt = 1;
5241 auio.uio_offset = 0;
5242 auio.uio_resid = uap->nbytes;
5243 auio.uio_rw = UIO_WRITE;
5244 auio.uio_td = curthread;
5246 error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
5255 * Syscall to get a named extended attribute on a file or directory.
5258 sys_extattr_get_file(struct sysmsg *sysmsg,
5259 const struct extattr_get_file_args *uap)
5261 char attrname[EXTATTR_MAXNAMELEN];
5262 struct nlookupdata nd;
5268 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
5274 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
5276 error = nlookup(&nd);
5278 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_SHARED, &vp);
5284 bzero(&auio, sizeof(auio));
5285 aiov.iov_base = uap->data;
5286 aiov.iov_len = uap->nbytes;
5287 auio.uio_iov = &aiov;
5288 auio.uio_iovcnt = 1;
5289 auio.uio_offset = 0;
5290 auio.uio_resid = uap->nbytes;
5291 auio.uio_rw = UIO_READ;
5292 auio.uio_td = curthread;
5294 error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
5296 sysmsg->sysmsg_result = uap->nbytes - auio.uio_resid;
5304 * Syscall to delete a named extended attribute from a file or directory.
5305 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
5308 sys_extattr_delete_file(struct sysmsg *sysmsg,
5309 const struct extattr_delete_file_args *uap)
5311 char attrname[EXTATTR_MAXNAMELEN];
5312 struct nlookupdata nd;
5316 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
5320 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
5322 error = nlookup(&nd);
5324 error = ncp_writechk(&nd.nl_nch);
5326 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
5328 error = VOP_SETEXTATTR(vp, uap->attrnamespace,
5329 attrname, NULL, nd.nl_cred);
5338 * Determine if the mount is visible to the process.
5341 chroot_visible_mnt(struct mount *mp, struct proc *p)
5343 struct nchandle nch;
5346 * Traverse from the mount point upwards. If we hit the process
5347 * root then the mount point is visible to the process.
5349 nch = mp->mnt_ncmountpt;
5351 if (nch.mount == p->p_fd->fd_nrdir.mount &&
5352 nch.ncp == p->p_fd->fd_nrdir.ncp) {
5355 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
5356 nch = nch.mount->mnt_ncmounton;
5358 nch.ncp = nch.ncp->nc_parent;
5363 * If the mount point is not visible to the process, but the
5364 * process root is in a subdirectory of the mount, return
5367 if (p->p_fd->fd_nrdir.mount == mp)
5374 * Return the appropriate system capability restriction.
5377 get_fscap(const char *fsname)
5380 if (strncmp("null", fsname, 5) == 0) {
5381 return SYSCAP_NOMOUNT_NULLFS;
5382 } else if (strncmp(fsname, "devfs", 6) == 0) {
5383 return SYSCAP_NOMOUNT_DEVFS;
5384 } else if (strncmp(fsname, "procfs", 7) == 0) {
5385 return SYSCAP_NOMOUNT_PROCFS;
5386 } else if (strncmp(fsname, "tmpfs", 6) == 0) {
5387 return SYSCAP_NOMOUNT_TMPFS;
5388 } else if (strncmp(fsname, "fusefs", 7) == 0) {
5389 return SYSCAP_NOMOUNT_FUSE;
5391 return SYSCAP_RESTRICTEDROOT;
5395 sys___realpath(struct sysmsg *sysmsg, const struct __realpath_args *uap)
5397 struct nlookupdata nd;
5404 * Invalid length if less than 0. 0 is allowed
5406 if ((ssize_t)uap->len < 0)
5411 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
5415 nd.nl_flags |= NLC_SHAREDLOCK;
5416 error = nlookup(&nd);
5420 if (nd.nl_nch.ncp->nc_vp == NULL) {
5426 * Shortcut test for existence.
5428 if (uap->len == 0) {
5429 error = ENAMETOOLONG;
5434 * Obtain the path relative to the process root. The nch must not
5435 * be locked for the cache_fullpath() call.
5437 if (nd.nl_flags & NLC_NCPISLOCKED) {
5438 nd.nl_flags &= ~NLC_NCPISLOCKED;
5439 cache_unlock(&nd.nl_nch);
5441 error = cache_fullpath(curproc, &nd.nl_nch, NULL, &rbuf, &fbuf, 0);
5445 rlen = (ssize_t)strlen(rbuf);
5446 if (rlen >= uap->len) {
5447 error = ENAMETOOLONG;
5450 error = copyout(rbuf, uap->buf, rlen + 1);
5452 sysmsg->sysmsg_szresult = rlen;
5456 kfree(fbuf, M_TEMP);
5462 sys_posix_fallocate(struct sysmsg *sysmsg, const struct posix_fallocate_args *uap)
5464 return (kern_posix_fallocate(uap->fd, uap->offset, uap->len));
5468 kern_posix_fallocate(int fd, off_t offset, off_t len)
5470 struct thread *td = curthread;
5475 if (offset < 0 || len <= 0)
5477 /* Check for wrap. */
5478 if (offset > OFF_MAX - len)
5481 fp = holdfp(td, fd, -1);
5485 switch (fp->f_type) {
5497 if ((fp->f_flag & FWRITE) == 0) {
5503 if (vp->v_type != VREG) {
5508 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
5509 error = VOP_ALLOCATE(vp, offset, len);