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_fspriv(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 nullfs mounts.
144 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0)
148 * Select the correct priv according to the file system type.
150 priv = get_fspriv(fstypename);
152 if (usermount == 0 && (error = priv_check(td, priv)))
156 * Do not allow NFS export by non-root users.
158 if (flags & MNT_EXPORTED) {
159 error = priv_check(td, priv);
164 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
166 if (priv_check(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 = priv_check(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 = priv_check(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 if ((error = priv_check(td, PRIV_ROOT)) != 0) {
317 error = linker_load_file(fstypename, &lf);
318 if (error || lf == NULL) {
326 /* lookup again, see if the VFS was loaded */
327 vfsp = vfsconf_find_by_name(fstypename);
330 linker_file_unload(lf);
345 * Allocate and initialize the filesystem.
347 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
348 mount_init(mp, vfsp->vfc_vfsops);
349 vfs_busy(mp, LK_NOWAIT);
351 mp->mnt_pbuf_count = nswbuf_kva / NSWBUF_SPLIT;
352 vfsp->vfc_refcount++;
353 mp->mnt_stat.f_type = vfsp->vfc_typenum;
354 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
355 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
356 mp->mnt_stat.f_owner = cred->cr_uid;
357 lwkt_gettoken(&mp->mnt_token);
361 * (per-mount token acquired at this point)
363 * Set the mount level flags.
365 if (flags & MNT_RDONLY)
366 mp->mnt_flag |= MNT_RDONLY;
367 else if (mp->mnt_flag & MNT_RDONLY)
368 mp->mnt_kern_flag |= MNTK_WANTRDWR;
369 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
370 MNT_SYNCHRONOUS | MNT_ASYNC | MNT_NOATIME |
371 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
372 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR |
374 mp->mnt_flag |= flags & (MNT_NOSUID | MNT_NOEXEC |
375 MNT_NODEV | MNT_SYNCHRONOUS | MNT_ASYNC | MNT_FORCE |
376 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
377 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR |
381 * Pre-set the mount's ALL_MPSAFE flags if specified in the vfsconf.
382 * This way the initial VFS_MOUNT() call will also be MPSAFE.
384 if (vfsp->vfc_flags & VFCF_MPSAFE)
385 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE;
388 * Mount the filesystem.
389 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
392 if (mp->mnt_flag & MNT_UPDATE) {
393 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
394 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
395 mp->mnt_flag &= ~MNT_RDONLY;
396 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
397 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
400 mp->mnt_kern_flag = flag2;
402 lwkt_reltoken(&mp->mnt_token);
408 mp->mnt_ncmounton = nch;
409 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
410 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
413 * Put the new filesystem on the mount list after root. The mount
414 * point gets its own mnt_ncmountpt (unless the VFS already set one
415 * up) which represents the root of the mount. The lookup code
416 * detects the mount point going forward and checks the root of
417 * the mount going backwards.
419 * It is not necessary to invalidate or purge the vnode underneath
420 * because elements under the mount will be given their own glue
424 if (mp->mnt_ncmountpt.ncp == NULL) {
426 * Allocate, then unlock, but leave the ref intact.
427 * This is the mnt_refs (1) that we will retain
428 * through to the unmount.
430 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
431 cache_unlock(&mp->mnt_ncmountpt);
435 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
437 cache_ismounting(mp);
438 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
440 mountlist_insert(mp, MNTINS_LAST);
442 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
443 error = vfs_allocate_syncvnode(mp);
444 lwkt_reltoken(&mp->mnt_token);
446 error = VFS_START(mp, 0);
448 KNOTE(&fs_klist, VQ_MOUNT);
450 bzero(&mp->mnt_ncmounton, sizeof(mp->mnt_ncmounton));
451 vn_syncer_thr_stop(mp);
452 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
453 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
454 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
455 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
456 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
458 crfree(mp->mnt_cred);
461 mp->mnt_vfc->vfc_refcount--;
462 lwkt_reltoken(&mp->mnt_token);
473 * Scan all active processes to see if any of them have a current
474 * or root directory onto which the new filesystem has just been
475 * mounted. If so, replace them with the new mount point.
477 * Both old_nch and new_nch are ref'd on call but not locked.
478 * new_nch must be temporarily locked so it can be associated with the
479 * vnode representing the root of the mount point.
481 struct checkdirs_info {
482 struct nchandle old_nch;
483 struct nchandle new_nch;
484 struct vnode *old_vp;
485 struct vnode *new_vp;
488 static int checkdirs_callback(struct proc *p, void *data);
491 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
493 struct checkdirs_info info;
499 * If the old mount point's vnode has a usecount of 1, it is not
500 * being held as a descriptor anywhere.
502 olddp = old_nch->ncp->nc_vp;
503 if (olddp == NULL || VREFCNT(olddp) == 1)
507 * Force the root vnode of the new mount point to be resolved
508 * so we can update any matching processes.
511 if (VFS_ROOT(mp, &newdp))
512 panic("mount: lost mount");
515 vn_lock(newdp, LK_EXCLUSIVE | LK_RETRY);
516 cache_setunresolved(new_nch);
517 cache_setvp(new_nch, newdp);
518 cache_unlock(new_nch);
521 * Special handling of the root node
523 if (rootvnode == olddp) {
525 vfs_cache_setroot(newdp, cache_hold(new_nch));
529 * Pass newdp separately so the callback does not have to access
530 * it via new_nch->ncp->nc_vp.
532 info.old_nch = *old_nch;
533 info.new_nch = *new_nch;
535 allproc_scan(checkdirs_callback, &info, 0);
540 * NOTE: callback is not MP safe because the scanned process's filedesc
541 * structure can be ripped out from under us, amoung other things.
544 checkdirs_callback(struct proc *p, void *data)
546 struct checkdirs_info *info = data;
547 struct filedesc *fdp;
548 struct nchandle ncdrop1;
549 struct nchandle ncdrop2;
550 struct vnode *vprele1;
551 struct vnode *vprele2;
553 if ((fdp = p->p_fd) != NULL) {
554 cache_zero(&ncdrop1);
555 cache_zero(&ncdrop2);
560 * MPUNSAFE - XXX fdp can be pulled out from under a
563 * A shared filedesc is ok, we don't have to copy it
564 * because we are making this change globally.
566 spin_lock(&fdp->fd_spin);
567 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
568 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
569 vprele1 = fdp->fd_cdir;
571 fdp->fd_cdir = info->new_vp;
572 ncdrop1 = fdp->fd_ncdir;
573 cache_copy(&info->new_nch, &fdp->fd_ncdir);
575 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
576 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
577 vprele2 = fdp->fd_rdir;
579 fdp->fd_rdir = info->new_vp;
580 ncdrop2 = fdp->fd_nrdir;
581 cache_copy(&info->new_nch, &fdp->fd_nrdir);
583 spin_unlock(&fdp->fd_spin);
585 cache_drop(&ncdrop1);
587 cache_drop(&ncdrop2);
597 * Unmount a file system.
599 * Note: unmount takes a path to the vnode mounted on as argument,
600 * not special file (as before).
602 * umount_args(char *path, int flags)
607 sys_unmount(struct sysmsg *sysmsg, const struct unmount_args *uap)
609 struct thread *td = curthread;
610 struct proc *p __debugvar = td->td_proc;
611 struct mount *mp = NULL;
612 struct nlookupdata nd;
613 char fstypename[MFSNAMELEN];
622 /* We do not allow user umounts inside a jail for now */
623 if (usermount && jailed(cred)) {
628 error = nlookup_init(&nd, uap->path, UIO_USERSPACE,
629 NLC_FOLLOW | NLC_IGNBADDIR);
631 error = nlookup(&nd);
635 mp = nd.nl_nch.mount;
637 /* Figure out the fsname in order to select proper privs */
638 ksnprintf(fstypename, MFSNAMELEN, "%s", mp->mnt_vfc->vfc_name);
639 priv = get_fspriv(fstypename);
641 if (usermount == 0 && (error = priv_check(td, priv))) {
647 * Only root, or the user that did the original mount is
648 * permitted to unmount this filesystem.
650 if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) &&
651 (error = priv_check(td, priv)))
655 * Don't allow unmounting the root file system.
657 if (mp->mnt_flag & MNT_ROOTFS) {
663 * Must be the root of the filesystem
665 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
670 /* Check if this mount belongs to this prison */
671 if (jailed(cred) && mp->mnt_cred && (!mp->mnt_cred->cr_prison ||
672 mp->mnt_cred->cr_prison != cred->cr_prison)) {
673 kprintf("mountpoint %s does not belong to this jail\n",
680 * If no error try to issue the unmount. We lose our cache
681 * ref when we call nlookup_done so we must hold the mount point
682 * to prevent use-after-free races.
688 error = dounmount(mp, uap->flags, 0);
698 * Do the actual file system unmount (interlocked against the mountlist
699 * token and mp->mnt_token).
702 dounmount_interlock(struct mount *mp)
704 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
706 mp->mnt_kern_flag |= MNTK_UNMOUNT;
711 unmount_allproc_cb(struct proc *p, void *arg)
715 if (p->p_textnch.ncp == NULL)
718 mp = (struct mount *)arg;
719 if (p->p_textnch.mount == mp)
720 cache_drop(&p->p_textnch);
726 * The guts of the unmount code. The mount owns one ref and one hold
727 * count. If we successfully interlock the unmount, those refs are ours.
728 * (The ref is from mnt_ncmountpt).
730 * When halting we shortcut certain mount types such as devfs by not actually
731 * issuing the VFS_SYNC() or VFS_UNMOUNT(). They are still disconnected
732 * from the mountlist so higher-level filesytems can unmount cleanly.
734 * The mount types that allow QUICKHALT are: devfs, tmpfs, procfs.
737 dounmount(struct mount *mp, int flags, int halting)
739 struct namecache *ncp;
750 lwkt_gettoken(&mp->mnt_token);
753 * When halting, certain mount points can essentially just
754 * be unhooked and otherwise ignored.
756 if (halting && (mp->mnt_kern_flag & MNTK_QUICKHALT)) {
765 * Exclusive access for unmounting purposes.
767 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
771 * We now 'own' the last mp->mnt_refs
773 * Allow filesystems to detect that a forced unmount is in progress.
775 if (flags & MNT_FORCE)
776 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
777 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_TIMELOCK);
778 error = lockmgr(&mp->mnt_lock, lflags);
780 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
781 if (mp->mnt_kern_flag & MNTK_MWAIT) {
782 mp->mnt_kern_flag &= ~MNTK_MWAIT;
788 if (mp->mnt_flag & MNT_EXPUBLIC)
789 vfs_setpublicfs(NULL, NULL, NULL);
791 vfs_msync(mp, MNT_WAIT);
792 async_flag = mp->mnt_flag & MNT_ASYNC;
793 mp->mnt_flag &=~ MNT_ASYNC;
796 * Decomission our special mnt_syncer vnode. This also stops
797 * the vnlru code. If we are unable to unmount we recommission
800 * Then sync the filesystem.
802 if ((vp = mp->mnt_syncer) != NULL) {
803 mp->mnt_syncer = NULL;
804 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
810 * Sync normally-mounted filesystem.
812 if (quickhalt == 0) {
813 if ((mp->mnt_flag & MNT_RDONLY) == 0)
814 VFS_SYNC(mp, MNT_WAIT);
818 * nchandle records ref the mount structure. Expect a count of 1
819 * (our mount->mnt_ncmountpt).
821 * Scans can get temporary refs on a mountpoint (thought really
822 * heavy duty stuff like cache_findmount() do not).
824 for (retry = 0; (retry < 10 || debug_unmount); ++retry) {
826 * Invalidate the namecache topology under the mount.
827 * nullfs mounts alias a real mount's namecache topology
828 * and it should not be invalidated in that case.
830 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
831 cache_lock(&mp->mnt_ncmountpt);
832 cache_inval(&mp->mnt_ncmountpt,
833 CINV_DESTROY|CINV_CHILDREN);
834 cache_unlock(&mp->mnt_ncmountpt);
840 cache_unmounting(mp);
841 if (mp->mnt_refs != 1)
842 cache_clearmntcache(mp);
845 * Break out if we are good. Don't count ncp refs if the
848 ncp = (mp->mnt_kern_flag & MNTK_NCALIASED) ?
849 NULL : mp->mnt_ncmountpt.ncp;
850 if (mp->mnt_refs == 1 &&
851 (ncp == NULL || (ncp->nc_refs == 1 &&
852 TAILQ_FIRST(&ncp->nc_list) == NULL))) {
857 * If forcing the unmount, clean out any p->p_textnch
858 * nchandles that match this mount.
860 if (flags & MNT_FORCE)
861 allproc_scan(&unmount_allproc_cb, mp, 0);
866 tsleep(&mp->mnt_refs, 0, "mntbsy", hz / 10 + 1);
867 if ((retry & 15) == 15) {
869 "(%p) debug - retry %d, "
870 "%d namecache refs, %d mount refs",
872 (ncp ? ncp->nc_refs - 1 : 0),
878 ncp = (mp->mnt_kern_flag & MNTK_NCALIASED) ?
879 NULL : mp->mnt_ncmountpt.ncp;
880 if (mp->mnt_refs != 1 ||
881 (ncp != NULL && (ncp->nc_refs != 1 ||
882 TAILQ_FIRST(&ncp->nc_list)))) {
884 "(%p): %d namecache refs, %d mount refs "
887 (ncp ? ncp->nc_refs - 1 : 0),
889 if (flags & MNT_FORCE) {
891 mount_warning(mp, "forcing unmount\n");
898 * So far so good, sync the filesystem once more and
899 * call the VFS unmount code if the sync succeeds.
901 if (error == 0 && quickhalt == 0) {
902 if (mp->mnt_flag & MNT_RDONLY) {
903 error = VFS_UNMOUNT(mp, flags);
905 error = VFS_SYNC(mp, MNT_WAIT);
906 if (error == 0 || /* no error */
907 error == EOPNOTSUPP || /* no sync avail */
908 (flags & MNT_FORCE)) { /* force anyway */
909 error = VFS_UNMOUNT(mp, flags);
914 "(%p) unmount: vfs refused to unmount, "
921 * If an error occurred we can still recover, restoring the
922 * syncer vnode and misc flags.
925 if (mp->mnt_syncer == NULL && hadsyncer)
926 vfs_allocate_syncvnode(mp);
927 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
928 mp->mnt_flag |= async_flag;
929 lockmgr(&mp->mnt_lock, LK_RELEASE);
930 if (mp->mnt_kern_flag & MNTK_MWAIT) {
931 mp->mnt_kern_flag &= ~MNTK_MWAIT;
937 * Clean up any journals still associated with the mount after
938 * filesystem activity has ceased.
940 journal_remove_all_journals(mp,
941 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
943 mountlist_remove(mp);
946 * Remove any installed vnode ops here so the individual VFSs don't
949 * mnt_refs should go to zero when we scrap mnt_ncmountpt.
951 * When quickhalting we have to keep these intact because the
952 * underlying vnodes have not been destroyed, and some might be
955 if (quickhalt == 0) {
956 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
957 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
958 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
959 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
960 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
963 if (mp->mnt_ncmountpt.ncp != NULL) {
964 nch = mp->mnt_ncmountpt;
965 cache_zero(&mp->mnt_ncmountpt);
966 cache_clrmountpt(&nch);
969 if (mp->mnt_ncmounton.ncp != NULL) {
970 cache_unmounting(mp);
971 nch = mp->mnt_ncmounton;
972 cache_zero(&mp->mnt_ncmounton);
973 cache_clrmountpt(&nch);
978 crfree(mp->mnt_cred);
982 mp->mnt_vfc->vfc_refcount--;
985 * If not quickhalting the mount, we expect there to be no
988 if (quickhalt == 0 && !TAILQ_EMPTY(&mp->mnt_nvnodelist))
989 panic("unmount: dangling vnode");
994 lockmgr(&mp->mnt_lock, LK_RELEASE);
995 if (mp->mnt_kern_flag & MNTK_MWAIT) {
996 mp->mnt_kern_flag &= ~MNTK_MWAIT;
1001 * If we reach here and freeok != 0 we must free the mount.
1002 * mnt_refs should already have dropped to 0, so if it is not
1003 * zero we must cycle the caches and wait.
1005 * When we are satisfied that the mount has disconnected we can
1006 * drop the hold on the mp that represented the mount (though the
1007 * caller might actually have another, so the caller's drop may
1008 * do the actual free).
1011 if (mp->mnt_refs > 0)
1012 cache_clearmntcache(mp);
1013 while (mp->mnt_refs > 0) {
1014 cache_unmounting(mp);
1016 tsleep(&mp->mnt_refs, 0, "umntrwait", hz / 10 + 1);
1017 cache_clearmntcache(mp);
1019 lwkt_reltoken(&mp->mnt_token);
1023 cache_clearmntcache(mp);
1026 KNOTE(&fs_klist, VQ_UNMOUNT);
1029 lwkt_reltoken(&mp->mnt_token);
1035 mount_warning(struct mount *mp, const char *ctl, ...)
1041 __va_start(va, ctl);
1042 if (cache_fullpath(NULL, &mp->mnt_ncmounton, NULL,
1043 &ptr, &buf, 0) == 0) {
1044 kprintf("unmount(%s): ", ptr);
1049 kprintf("unmount(%p", mp);
1050 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
1051 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
1060 * Shim cache_fullpath() to handle the case where a process is chrooted into
1061 * a subdirectory of a mount. In this case if the root mount matches the
1062 * process root directory's mount we have to specify the process's root
1063 * directory instead of the mount point, because the mount point might
1064 * be above the root directory.
1068 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
1070 struct nchandle *nch;
1072 if (p && p->p_fd->fd_nrdir.mount == mp)
1073 nch = &p->p_fd->fd_nrdir;
1075 nch = &mp->mnt_ncmountpt;
1076 return(cache_fullpath(p, nch, NULL, rb, fb, 0));
1080 * Sync each mounted filesystem.
1084 static int syncprt = 0;
1085 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
1088 static int sync_callback(struct mount *mp, void *data);
1091 sys_sync(struct sysmsg *sysmsg, const struct sync_args *uap)
1093 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
1099 sync_callback(struct mount *mp, void *data __unused)
1103 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
1104 lwkt_gettoken(&mp->mnt_token);
1105 asyncflag = mp->mnt_flag & MNT_ASYNC;
1106 mp->mnt_flag &= ~MNT_ASYNC;
1107 lwkt_reltoken(&mp->mnt_token);
1108 vfs_msync(mp, MNT_NOWAIT);
1109 VFS_SYNC(mp, MNT_NOWAIT);
1110 lwkt_gettoken(&mp->mnt_token);
1111 mp->mnt_flag |= asyncflag;
1112 lwkt_reltoken(&mp->mnt_token);
1117 /* XXX PRISON: could be per prison flag */
1118 static int prison_quotas;
1120 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
1124 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
1126 * Change filesystem quotas.
1131 sys_quotactl(struct sysmsg *sysmsg, const struct quotactl_args *uap)
1133 struct nlookupdata nd;
1139 if (td->td_ucred->cr_prison && !prison_quotas) {
1144 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1146 error = nlookup(&nd);
1148 mp = nd.nl_nch.mount;
1149 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
1150 uap->arg, nd.nl_cred);
1158 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
1159 * void *buf, int buflen)
1161 * This function operates on a mount point and executes the specified
1162 * operation using the specified control data, and possibly returns data.
1164 * The actual number of bytes stored in the result buffer is returned, 0
1165 * if none, otherwise an error is returned.
1170 sys_mountctl(struct sysmsg *sysmsg, const struct mountctl_args *uap)
1172 struct thread *td = curthread;
1180 * Sanity and permissions checks. We must be root.
1182 if (td->td_ucred->cr_prison != NULL)
1184 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
1185 (error = priv_check(td, PRIV_ROOT)) != 0)
1189 * Argument length checks
1191 if (uap->ctllen < 0 || uap->ctllen > 1024)
1193 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
1195 if (uap->path == NULL)
1199 * Allocate the necessary buffers and copyin data
1201 path = objcache_get(namei_oc, M_WAITOK);
1202 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1207 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
1208 error = copyin(uap->ctl, ctl, uap->ctllen);
1213 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
1216 * Validate the descriptor
1219 fp = holdfp(td, uap->fd, -1);
1229 * Execute the internal kernel function and clean up.
1231 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen,
1232 buf, uap->buflen, &sysmsg->sysmsg_result);
1234 dropfp(td, uap->fd, fp);
1235 if (error == 0 && sysmsg->sysmsg_result > 0)
1236 error = copyout(buf, uap->buf, sysmsg->sysmsg_result);
1239 objcache_put(namei_oc, path);
1248 * Execute a mount control operation by resolving the path to a mount point
1249 * and calling vop_mountctl().
1251 * Use the mount point from the nch instead of the vnode so nullfs mounts
1252 * can properly spike the VOP.
1255 kern_mountctl(const char *path, int op, struct file *fp,
1256 const void *ctl, int ctllen,
1257 void *buf, int buflen, int *res)
1260 struct nlookupdata nd;
1261 struct nchandle nch;
1267 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1270 error = nlookup(&nd);
1275 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1282 * Yes, all this is needed to use the nch.mount below, because
1283 * we must maintain a ref on the mount to avoid ripouts (e.g.
1284 * due to heavy mount/unmount use by synth or poudriere).
1287 cache_zero(&nd.nl_nch);
1295 * Must be the root of the filesystem
1297 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1302 if (mp == NULL || mp->mnt_kern_flag & MNTK_UNMOUNT) {
1303 kprintf("kern_mountctl: Warning, \"%s\" racing unmount\n",
1309 error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen,
1318 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1320 struct thread *td = curthread;
1321 struct proc *p = td->td_proc;
1324 char *fullpath, *freepath;
1327 if ((error = nlookup(nd)) != 0)
1329 mp = nd->nl_nch.mount;
1333 * Ignore refresh error, user should have visibility.
1334 * This can happen if a NFS mount goes bad (e.g. server
1335 * revokes perms or goes down).
1337 error = VFS_STATFS(mp, sp, nd->nl_cred);
1340 error = mount_path(p, mp, &fullpath, &freepath);
1343 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1344 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1345 kfree(freepath, M_TEMP);
1347 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1348 bcopy(sp, buf, sizeof(*buf));
1349 /* Only root should have access to the fsid's. */
1350 if (priv_check(td, PRIV_ROOT))
1351 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1356 * statfs_args(char *path, struct statfs *buf)
1358 * Get filesystem statistics.
1361 sys_statfs(struct sysmsg *sysmsg, const struct statfs_args *uap)
1363 struct nlookupdata nd;
1367 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1369 error = kern_statfs(&nd, &buf);
1372 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1377 kern_fstatfs(int fd, struct statfs *buf)
1379 struct thread *td = curthread;
1380 struct proc *p = td->td_proc;
1384 char *fullpath, *freepath;
1388 if ((error = holdvnode(td, fd, &fp)) != 0)
1392 * Try to use mount info from any overlays rather than the
1393 * mount info for the underlying vnode, otherwise we will
1394 * fail when operating on null-mounted paths inside a chroot.
1396 if ((mp = fp->f_nchandle.mount) == NULL)
1397 mp = ((struct vnode *)fp->f_data)->v_mount;
1402 if (fp->f_cred == NULL) {
1408 * Ignore refresh error, user should have visibility.
1409 * This can happen if a NFS mount goes bad (e.g. server
1410 * revokes perms or goes down).
1413 error = VFS_STATFS(mp, sp, fp->f_cred);
1415 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1417 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1418 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1419 kfree(freepath, M_TEMP);
1421 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1422 bcopy(sp, buf, sizeof(*buf));
1424 /* Only root should have access to the fsid's. */
1425 if (priv_check(td, PRIV_ROOT))
1426 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1434 * fstatfs_args(int fd, struct statfs *buf)
1436 * Get filesystem statistics.
1439 sys_fstatfs(struct sysmsg *sysmsg, const struct fstatfs_args *uap)
1444 error = kern_fstatfs(uap->fd, &buf);
1447 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1452 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1458 if ((error = nlookup(nd)) != 0)
1460 mp = nd->nl_nch.mount;
1461 sp = &mp->mnt_vstat;
1462 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1466 if (mp->mnt_flag & MNT_RDONLY)
1467 sp->f_flag |= ST_RDONLY;
1468 if (mp->mnt_flag & MNT_NOSUID)
1469 sp->f_flag |= ST_NOSUID;
1470 bcopy(sp, buf, sizeof(*buf));
1475 * statfs_args(char *path, struct statfs *buf)
1477 * Get filesystem statistics.
1480 sys_statvfs(struct sysmsg *sysmsg, const struct statvfs_args *uap)
1482 struct nlookupdata nd;
1486 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1488 error = kern_statvfs(&nd, &buf);
1491 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1496 kern_fstatvfs(int fd, struct statvfs *buf)
1498 struct thread *td = curthread;
1504 if ((error = holdvnode(td, fd, &fp)) != 0)
1506 if ((mp = fp->f_nchandle.mount) == NULL)
1507 mp = ((struct vnode *)fp->f_data)->v_mount;
1512 if (fp->f_cred == NULL) {
1516 sp = &mp->mnt_vstat;
1517 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1521 if (mp->mnt_flag & MNT_RDONLY)
1522 sp->f_flag |= ST_RDONLY;
1523 if (mp->mnt_flag & MNT_NOSUID)
1524 sp->f_flag |= ST_NOSUID;
1526 bcopy(sp, buf, sizeof(*buf));
1534 * fstatfs_args(int fd, struct statfs *buf)
1536 * Get filesystem statistics.
1539 sys_fstatvfs(struct sysmsg *sysmsg, const struct fstatvfs_args *uap)
1544 error = kern_fstatvfs(uap->fd, &buf);
1547 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1552 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1554 * Get statistics on all filesystems.
1557 struct getfsstat_info {
1558 struct statfs *sfsp;
1566 static int getfsstat_callback(struct mount *, void *);
1569 sys_getfsstat(struct sysmsg *sysmsg, const struct getfsstat_args *uap)
1571 struct thread *td = curthread;
1572 struct getfsstat_info info;
1574 bzero(&info, sizeof(info));
1576 info.maxcount = uap->bufsize / sizeof(struct statfs);
1577 info.sfsp = uap->buf;
1579 info.flags = uap->flags;
1582 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1583 if (info.sfsp && info.count > info.maxcount)
1584 sysmsg->sysmsg_result = info.maxcount;
1586 sysmsg->sysmsg_result = info.count;
1587 return (info.error);
1591 getfsstat_callback(struct mount *mp, void *data)
1593 struct getfsstat_info *info = data;
1599 if (info->td->td_proc && !chroot_visible_mnt(mp, info->td->td_proc))
1602 if (info->sfsp && info->count < info->maxcount) {
1606 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1607 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1608 * overrides MNT_WAIT.
1610 * Ignore refresh error, user should have visibility.
1611 * This can happen if a NFS mount goes bad (e.g. server
1612 * revokes perms or goes down).
1614 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1615 (info->flags & MNT_WAIT)) &&
1616 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1619 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1621 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1623 info->error = error;
1626 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1627 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1628 kfree(freepath, M_TEMP);
1630 error = copyout(sp, info->sfsp, sizeof(*sp));
1632 info->error = error;
1642 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1643 long bufsize, int flags)
1645 * Get statistics on all filesystems.
1648 struct getvfsstat_info {
1649 struct statfs *sfsp;
1650 struct statvfs *vsfsp;
1658 static int getvfsstat_callback(struct mount *, void *);
1661 sys_getvfsstat(struct sysmsg *sysmsg, const struct getvfsstat_args *uap)
1663 struct thread *td = curthread;
1664 struct getvfsstat_info info;
1666 bzero(&info, sizeof(info));
1668 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1669 info.sfsp = uap->buf;
1670 info.vsfsp = uap->vbuf;
1672 info.flags = uap->flags;
1675 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1676 if (info.vsfsp && info.count > info.maxcount)
1677 sysmsg->sysmsg_result = info.maxcount;
1679 sysmsg->sysmsg_result = info.count;
1680 return (info.error);
1684 getvfsstat_callback(struct mount *mp, void *data)
1686 struct getvfsstat_info *info = data;
1688 struct statvfs *vsp;
1693 if (info->td->td_proc && !chroot_visible_mnt(mp, info->td->td_proc))
1696 if (info->vsfsp && info->count < info->maxcount) {
1698 vsp = &mp->mnt_vstat;
1701 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1702 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1703 * overrides MNT_WAIT.
1705 * Ignore refresh error, user should have visibility.
1706 * This can happen if a NFS mount goes bad (e.g. server
1707 * revokes perms or goes down).
1709 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1710 (info->flags & MNT_WAIT)) &&
1711 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1714 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1716 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1717 (info->flags & MNT_WAIT)) &&
1718 (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) {
1722 if (mp->mnt_flag & MNT_RDONLY)
1723 vsp->f_flag |= ST_RDONLY;
1724 if (mp->mnt_flag & MNT_NOSUID)
1725 vsp->f_flag |= ST_NOSUID;
1727 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1729 info->error = error;
1732 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1733 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1734 kfree(freepath, M_TEMP);
1736 error = copyout(sp, info->sfsp, sizeof(*sp));
1738 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1740 info->error = error;
1752 * fchdir_args(int fd)
1754 * Change current working directory to a given file descriptor.
1757 sys_fchdir(struct sysmsg *sysmsg, const struct fchdir_args *uap)
1759 struct thread *td = curthread;
1760 struct proc *p = td->td_proc;
1761 struct filedesc *fdp = p->p_fd;
1762 struct vnode *vp, *ovp;
1765 struct nchandle nch, onch, tnch;
1768 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
1770 lwkt_gettoken(&p->p_token);
1771 vp = (struct vnode *)fp->f_data;
1773 vn_lock(vp, LK_SHARED | LK_RETRY);
1774 if (fp->f_nchandle.ncp == NULL)
1777 error = checkvp_chdir(vp, td);
1782 cache_copy(&fp->f_nchandle, &nch);
1785 * If the ncp has become a mount point, traverse through
1789 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1790 (mp = cache_findmount(&nch)) != NULL
1792 error = nlookup_mp(mp, &tnch);
1794 cache_unlock(&tnch); /* leave ref intact */
1796 vp = tnch.ncp->nc_vp;
1797 error = vget(vp, LK_SHARED);
1798 KKASSERT(error == 0);
1802 cache_dropmount(mp);
1805 spin_lock(&fdp->fd_spin);
1807 onch = fdp->fd_ncdir;
1809 fdp->fd_ncdir = nch;
1810 spin_unlock(&fdp->fd_spin);
1811 vn_unlock(vp); /* leave ref intact */
1820 lwkt_reltoken(&p->p_token);
1825 kern_chdir(struct nlookupdata *nd)
1827 struct thread *td = curthread;
1828 struct proc *p = td->td_proc;
1829 struct filedesc *fdp = p->p_fd;
1830 struct vnode *vp, *ovp;
1831 struct nchandle onch;
1834 nd->nl_flags |= NLC_SHAREDLOCK;
1835 if ((error = nlookup(nd)) != 0)
1837 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1839 if ((error = vget(vp, LK_SHARED)) != 0)
1842 lwkt_gettoken(&p->p_token);
1843 error = checkvp_chdir(vp, td);
1846 spin_lock(&fdp->fd_spin);
1848 onch = fdp->fd_ncdir;
1849 fdp->fd_ncdir = nd->nl_nch;
1851 spin_unlock(&fdp->fd_spin);
1852 cache_unlock(&nd->nl_nch); /* leave reference intact */
1855 cache_zero(&nd->nl_nch);
1859 lwkt_reltoken(&p->p_token);
1864 * chdir_args(char *path)
1866 * Change current working directory (``.'').
1869 sys_chdir(struct sysmsg *sysmsg, const struct chdir_args *uap)
1871 struct nlookupdata nd;
1874 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1876 error = kern_chdir(&nd);
1882 * Helper function for raised chroot(2) security function: Refuse if
1883 * any filedescriptors are open directories.
1886 chroot_refuse_vdir_fds(thread_t td, struct filedesc *fdp)
1893 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1894 if ((error = holdvnode(td, fd, &fp)) != 0)
1896 vp = (struct vnode *)fp->f_data;
1897 if (vp->v_type != VDIR) {
1908 * This sysctl determines if we will allow a process to chroot(2) if it
1909 * has a directory open:
1910 * 0: disallowed for all processes.
1911 * 1: allowed for processes that were not already chroot(2)'ed.
1912 * 2: allowed for all processes.
1915 static int chroot_allow_open_directories = 1;
1917 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1918 &chroot_allow_open_directories, 0, "");
1921 * chroot to the specified namecache entry. We obtain the vp from the
1922 * namecache data. The passed ncp must be locked and referenced and will
1923 * remain locked and referenced on return.
1926 kern_chroot(struct nchandle *nch)
1928 struct thread *td = curthread;
1929 struct proc *p = td->td_proc;
1930 struct filedesc *fdp = p->p_fd;
1935 * Only privileged user can chroot
1937 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1942 * Disallow open directory descriptors (fchdir() breakouts).
1944 if (chroot_allow_open_directories == 0 ||
1945 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1946 if ((error = chroot_refuse_vdir_fds(td, fdp)) != 0)
1949 if ((vp = nch->ncp->nc_vp) == NULL)
1952 if ((error = vget(vp, LK_SHARED)) != 0)
1956 * Check the validity of vp as a directory to change to and
1957 * associate it with rdir/jdir.
1959 error = checkvp_chdir(vp, td);
1960 vn_unlock(vp); /* leave reference intact */
1962 lwkt_gettoken(&p->p_token);
1963 vrele(fdp->fd_rdir);
1964 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1965 cache_drop(&fdp->fd_nrdir);
1966 cache_copy(nch, &fdp->fd_nrdir);
1967 if (fdp->fd_jdir == NULL) {
1970 cache_copy(nch, &fdp->fd_njdir);
1972 if ((p->p_flags & P_DIDCHROOT) == 0) {
1973 p->p_flags |= P_DIDCHROOT;
1974 if (p->p_depth <= 65535 - 32)
1977 lwkt_reltoken(&p->p_token);
1985 * chroot_args(char *path)
1987 * Change notion of root (``/'') directory.
1990 sys_chroot(struct sysmsg *sysmsg, const struct chroot_args *uap)
1992 struct thread *td __debugvar = curthread;
1993 struct nlookupdata nd;
1996 KKASSERT(td->td_proc);
1997 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1999 nd.nl_flags |= NLC_EXEC;
2000 error = nlookup(&nd);
2002 error = kern_chroot(&nd.nl_nch);
2009 sys_chroot_kernel(struct sysmsg *sysmsg, const struct chroot_kernel_args *uap)
2011 struct thread *td = curthread;
2012 struct nlookupdata nd;
2013 struct nchandle *nch;
2017 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2021 error = nlookup(&nd);
2027 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
2031 if ((vp = nch->ncp->nc_vp) == NULL) {
2036 if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0)
2039 vfs_cache_setroot(vp, cache_hold(nch));
2048 * Common routine for chroot and chdir. Given a locked, referenced vnode,
2049 * determine whether it is legal to chdir to the vnode. The vnode's state
2050 * is not changed by this call.
2053 checkvp_chdir(struct vnode *vp, struct thread *td)
2057 if (vp->v_type != VDIR)
2060 error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
2065 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
2067 struct thread *td = curthread;
2068 struct proc *p = td->td_proc;
2069 struct lwp *lp = td->td_lwp;
2070 struct filedesc *fdp = p->p_fd;
2074 int type, indx, error = 0;
2077 if ((oflags & O_ACCMODE) == O_ACCMODE)
2079 flags = FFLAGS(oflags);
2080 error = falloc(lp, &nfp, NULL);
2084 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
2087 * Call vn_open() to do the lookup and assign the vnode to the
2088 * file pointer. vn_open() does not change the ref count on fp
2089 * and the vnode, on success, will be inherited by the file pointer
2092 * Request a shared lock on the vnode if possible.
2094 * When NLC_SHAREDLOCK is set we may still need an exclusive vnode
2095 * lock for O_RDWR opens on executables in order to avoid a VTEXT
2096 * detection race. The NLC_EXCLLOCK_IFEXEC handles this case.
2098 * NOTE: We need a flag to separate terminal vnode locking from
2099 * parent locking. O_CREAT needs parent locking, but O_TRUNC
2100 * and O_RDWR only need to lock the terminal vnode exclusively.
2102 nd->nl_flags |= NLC_LOCKVP;
2103 if ((flags & (O_CREAT|O_TRUNC)) == 0) {
2104 nd->nl_flags |= NLC_SHAREDLOCK;
2106 nd->nl_flags |= NLC_EXCLLOCK_IFEXEC;
2110 * Issue the vn_open, passing in the referenced fp. the vn_open()
2111 * is allowed to replace fp by fdrop()ing it and returning its own
2115 error = vn_open(nd, &nfp, flags, cmode);
2120 * Deal with any error condition
2123 fdrop(fp); /* our ref */
2124 if (error == ERESTART)
2130 * Reserve a file descriptor.
2132 if ((error = fdalloc(p, 0, &indx)) != 0) {
2138 * Handle advisory lock flags. This is only supported with vnodes.
2139 * For things like /dev/fd/N we might not actually get a vnode.
2141 if ((flags & (O_EXLOCK | O_SHLOCK)) && fp->f_type == DTYPE_VNODE) {
2144 vp = (struct vnode *)fp->f_data;
2147 lf.l_whence = SEEK_SET;
2150 if (flags & O_EXLOCK)
2151 lf.l_type = F_WRLCK;
2153 lf.l_type = F_RDLCK;
2154 if (flags & FNONBLOCK)
2159 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type);
2162 * lock request failed. Clean up the reserved
2166 fsetfd(fdp, NULL, indx);
2170 atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */
2175 * release our private reference, leaving the one associated with the
2176 * descriptor table intact.
2178 if (oflags & O_CLOEXEC)
2179 fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
2180 fsetfd(fdp, fp, indx);
2188 * open_args(char *path, int flags, int mode)
2190 * Check permissions, allocate an open file structure,
2191 * and call the device open routine if any.
2194 sys_open(struct sysmsg *sysmsg, const struct open_args *uap)
2196 struct nlookupdata nd;
2199 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2201 error = kern_open(&nd, uap->flags,
2202 uap->mode, &sysmsg->sysmsg_result);
2209 * openat_args(int fd, char *path, int flags, int mode)
2212 sys_openat(struct sysmsg *sysmsg, const struct openat_args *uap)
2214 struct nlookupdata nd;
2218 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2220 error = kern_open(&nd, uap->flags, uap->mode,
2221 &sysmsg->sysmsg_result);
2223 nlookup_done_at(&nd, fp);
2228 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
2230 struct thread *td = curthread;
2231 struct proc *p = td->td_proc;
2240 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2241 vattr.va_rmajor = rmajor;
2242 vattr.va_rminor = rminor;
2244 switch (mode & S_IFMT) {
2245 case S_IFMT: /* used by badsect to flag bad sectors */
2246 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
2247 vattr.va_type = VBAD;
2250 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2251 vattr.va_type = VCHR;
2254 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2255 vattr.va_type = VBLK;
2258 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2261 case S_IFDIR: /* special directories support for HAMMER */
2262 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2263 vattr.va_type = VDIR;
2266 return (kern_mkfifo(nd, mode));
2277 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2278 if ((error = nlookup(nd)) != 0)
2280 if (nd->nl_nch.ncp->nc_vp)
2282 if (nd->nl_dvp == NULL)
2284 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2288 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2289 nd->nl_cred, NAMEI_CREATE);
2292 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2293 &vp, nd->nl_cred, &vattr);
2301 * mknod_args(char *path, int mode, int dev)
2303 * Create a special file.
2306 sys_mknod(struct sysmsg *sysmsg, const struct mknod_args *uap)
2308 struct nlookupdata nd;
2311 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2313 error = kern_mknod(&nd, uap->mode,
2314 umajor(uap->dev), uminor(uap->dev));
2321 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2323 * Create a special file. The path is relative to the directory associated
2327 sys_mknodat(struct sysmsg *sysmsg, const struct mknodat_args *uap)
2329 struct nlookupdata nd;
2333 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2335 error = kern_mknod(&nd, uap->mode,
2336 umajor(uap->dev), uminor(uap->dev));
2338 nlookup_done_at(&nd, fp);
2343 kern_mkfifo(struct nlookupdata *nd, int mode)
2345 struct thread *td = curthread;
2346 struct proc *p = td->td_proc;
2353 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2354 if ((error = nlookup(nd)) != 0)
2356 if (nd->nl_nch.ncp->nc_vp)
2358 if (nd->nl_dvp == NULL)
2360 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2364 vattr.va_type = VFIFO;
2365 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2367 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2374 * mkfifo_args(char *path, int mode)
2376 * Create a named pipe.
2379 sys_mkfifo(struct sysmsg *sysmsg, const struct mkfifo_args *uap)
2381 struct nlookupdata nd;
2384 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2386 error = kern_mkfifo(&nd, uap->mode);
2392 * mkfifoat_args(int fd, char *path, mode_t mode)
2394 * Create a named pipe. The path is relative to the directory associated
2398 sys_mkfifoat(struct sysmsg *sysmsg, const struct mkfifoat_args *uap)
2400 struct nlookupdata nd;
2404 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2406 error = kern_mkfifo(&nd, uap->mode);
2407 nlookup_done_at(&nd, fp);
2411 static int hardlink_check_uid = 0;
2412 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2413 &hardlink_check_uid, 0,
2414 "Unprivileged processes cannot create hard links to files owned by other "
2416 static int hardlink_check_gid = 0;
2417 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2418 &hardlink_check_gid, 0,
2419 "Unprivileged processes cannot create hard links to files owned by other "
2423 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2429 * Shortcut if disabled
2431 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2435 * Privileged user can always hardlink
2437 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2441 * Otherwise only if the originating file is owned by the
2442 * same user or group. Note that any group is allowed if
2443 * the file is owned by the caller.
2445 error = VOP_GETATTR(vp, &va);
2449 if (hardlink_check_uid) {
2450 if (cred->cr_uid != va.va_uid)
2454 if (hardlink_check_gid) {
2455 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2463 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2465 struct thread *td = curthread;
2470 * Lookup the source and obtained a locked vnode.
2472 * You may only hardlink a file which you have write permission
2473 * on or which you own.
2475 * XXX relookup on vget failure / race ?
2478 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2479 if ((error = nlookup(nd)) != 0)
2481 vp = nd->nl_nch.ncp->nc_vp;
2482 KKASSERT(vp != NULL);
2483 if (vp->v_type == VDIR)
2484 return (EPERM); /* POSIX */
2485 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2487 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2491 * Unlock the source so we can lookup the target without deadlocking
2492 * (XXX vp is locked already, possible other deadlock?). The target
2495 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2496 nd->nl_flags &= ~NLC_NCPISLOCKED;
2497 cache_unlock(&nd->nl_nch);
2500 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2501 if ((error = nlookup(linknd)) != 0) {
2505 if (linknd->nl_nch.ncp->nc_vp) {
2509 if (linknd->nl_dvp == NULL) {
2513 VFS_MODIFYING(vp->v_mount);
2514 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
2521 * Finally run the new API VOP.
2523 error = can_hardlink(vp, td, td->td_ucred);
2525 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2526 vp, linknd->nl_cred);
2533 * link_args(char *path, char *link)
2535 * Make a hard file link.
2538 sys_link(struct sysmsg *sysmsg, const struct link_args *uap)
2540 struct nlookupdata nd, linknd;
2543 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2545 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2547 error = kern_link(&nd, &linknd);
2548 nlookup_done(&linknd);
2555 * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags)
2557 * Make a hard file link. The path1 argument is relative to the directory
2558 * associated with fd1, and similarly the path2 argument is relative to
2559 * the directory associated with fd2.
2562 sys_linkat(struct sysmsg *sysmsg, const struct linkat_args *uap)
2564 struct nlookupdata nd, linknd;
2565 struct file *fp1, *fp2;
2568 error = nlookup_init_at(&nd, &fp1, uap->fd1, uap->path1, UIO_USERSPACE,
2569 (uap->flags & AT_SYMLINK_FOLLOW) ? NLC_FOLLOW : 0);
2571 error = nlookup_init_at(&linknd, &fp2, uap->fd2,
2572 uap->path2, UIO_USERSPACE, 0);
2574 error = kern_link(&nd, &linknd);
2575 nlookup_done_at(&linknd, fp2);
2577 nlookup_done_at(&nd, fp1);
2582 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2590 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2591 if ((error = nlookup(nd)) != 0)
2593 if (nd->nl_nch.ncp->nc_vp)
2595 if (nd->nl_dvp == NULL)
2597 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2601 vattr.va_mode = mode;
2602 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2609 * symlink(char *path, char *link)
2611 * Make a symbolic link.
2614 sys_symlink(struct sysmsg *sysmsg, const struct symlink_args *uap)
2616 struct thread *td = curthread;
2617 struct nlookupdata nd;
2622 path = objcache_get(namei_oc, M_WAITOK);
2623 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2625 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2627 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2628 error = kern_symlink(&nd, path, mode);
2632 objcache_put(namei_oc, path);
2637 * symlinkat_args(char *path1, int fd, char *path2)
2639 * Make a symbolic link. The path2 argument is relative to the directory
2640 * associated with fd.
2643 sys_symlinkat(struct sysmsg *sysmsg, const struct symlinkat_args *uap)
2645 struct thread *td = curthread;
2646 struct nlookupdata nd;
2652 path1 = objcache_get(namei_oc, M_WAITOK);
2653 error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2655 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2658 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2659 error = kern_symlink(&nd, path1, mode);
2661 nlookup_done_at(&nd, fp);
2663 objcache_put(namei_oc, path1);
2668 * undelete_args(char *path)
2670 * Delete a whiteout from the filesystem.
2673 sys_undelete(struct sysmsg *sysmsg, const struct undelete_args *uap)
2675 struct nlookupdata nd;
2678 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2680 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2682 error = nlookup(&nd);
2683 if (error == 0 && nd.nl_dvp == NULL)
2686 error = ncp_writechk(&nd.nl_nch);
2688 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2696 kern_unlink(struct nlookupdata *nd)
2701 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2702 if ((error = nlookup(nd)) != 0)
2704 if (nd->nl_dvp == NULL)
2706 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2708 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2713 * unlink_args(char *path)
2715 * Delete a name from the filesystem.
2718 sys_unlink(struct sysmsg *sysmsg, const struct unlink_args *uap)
2720 struct nlookupdata nd;
2723 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2725 error = kern_unlink(&nd);
2732 * unlinkat_args(int fd, char *path, int flags)
2734 * Delete the file or directory entry pointed to by fd/path.
2737 sys_unlinkat(struct sysmsg *sysmsg, const struct unlinkat_args *uap)
2739 struct nlookupdata nd;
2743 if (uap->flags & ~AT_REMOVEDIR)
2746 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2748 if (uap->flags & AT_REMOVEDIR)
2749 error = kern_rmdir(&nd);
2751 error = kern_unlink(&nd);
2753 nlookup_done_at(&nd, fp);
2758 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2760 struct thread *td = curthread;
2763 struct vattr_lite lva;
2767 fp = holdfp(td, fd, -1);
2770 if (fp->f_type != DTYPE_VNODE) {
2774 vp = (struct vnode *)fp->f_data;
2778 spin_lock(&fp->f_spin);
2779 new_offset = fp->f_offset + offset;
2783 error = VOP_GETATTR_LITE(vp, &lva);
2784 spin_lock(&fp->f_spin);
2785 new_offset = offset + lva.va_size;
2788 new_offset = offset;
2790 spin_lock(&fp->f_spin);
2795 spin_lock(&fp->f_spin);
2800 * Validate the seek position. Negative offsets are not allowed
2801 * for regular files or directories.
2803 * Normally we would also not want to allow negative offsets for
2804 * character and block-special devices. However kvm addresses
2805 * on 64 bit architectures might appear to be negative and must
2809 if (new_offset < 0 &&
2810 (vp->v_type == VREG || vp->v_type == VDIR)) {
2813 fp->f_offset = new_offset;
2816 *res = fp->f_offset;
2817 spin_unlock(&fp->f_spin);
2825 * lseek_args(int fd, int pad, off_t offset, int whence)
2827 * Reposition read/write file offset.
2830 sys_lseek(struct sysmsg *sysmsg, const struct lseek_args *uap)
2834 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2835 &sysmsg->sysmsg_offset);
2841 * Check if current process can access given file. amode is a bitmask of *_OK
2842 * access bits. flags is a bitmask of AT_* flags.
2845 kern_access(struct nlookupdata *nd, int amode, int flags)
2850 if (flags & ~AT_EACCESS)
2852 nd->nl_flags |= NLC_SHAREDLOCK;
2853 if ((error = nlookup(nd)) != 0)
2855 if ((amode & W_OK) && (error = ncp_writechk(&nd->nl_nch)) != 0)
2858 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
2862 /* Flags == 0 means only check for existence. */
2871 if ((mode & VWRITE) == 0 ||
2872 (error = vn_writechk(vp)) == 0) {
2873 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2877 * If the file handle is stale we have to re-resolve the
2878 * entry with the ncp held exclusively. This is a hack
2881 if (error == ESTALE) {
2885 cache_unlock(&nd->nl_nch);
2886 cache_lock(&nd->nl_nch);
2887 dummy_gen = nd->nl_nch.ncp->nc_generation;
2888 cache_setunresolved(&nd->nl_nch);
2889 error = cache_resolve(&nd->nl_nch, &dummy_gen,
2903 * access_args(char *path, int flags)
2905 * Check access permissions.
2908 sys_access(struct sysmsg *sysmsg, const struct access_args *uap)
2910 struct nlookupdata nd;
2913 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2915 error = kern_access(&nd, uap->flags, 0);
2922 * eaccess_args(char *path, int flags)
2924 * Check access permissions.
2927 sys_eaccess(struct sysmsg *sysmsg, const struct eaccess_args *uap)
2929 struct nlookupdata nd;
2932 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2934 error = kern_access(&nd, uap->flags, AT_EACCESS);
2941 * faccessat_args(int fd, char *path, int amode, int flags)
2943 * Check access permissions.
2946 sys_faccessat(struct sysmsg *sysmsg, const struct faccessat_args *uap)
2948 struct nlookupdata nd;
2952 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2955 error = kern_access(&nd, uap->amode, uap->flags);
2956 nlookup_done_at(&nd, fp);
2961 kern_stat(struct nlookupdata *nd, struct stat *st)
2966 nd->nl_flags |= NLC_SHAREDLOCK;
2967 if ((error = nlookup(nd)) != 0)
2970 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2974 error = cache_vref(&nd->nl_nch, NULL, &vp);
2976 error = vget(vp, LK_SHARED);
2980 error = vn_stat(vp, st, nd->nl_cred);
2983 * If the file handle is stale we have to re-resolve the
2984 * entry with the ncp held exclusively. This is a hack
2987 if (error == ESTALE) {
2994 cache_unlock(&nd->nl_nch);
2995 cache_lock(&nd->nl_nch);
2996 dummy_gen = nd->nl_nch.ncp->nc_generation;
2997 cache_setunresolved(&nd->nl_nch);
2998 error = cache_resolve(&nd->nl_nch, &dummy_gen, nd->nl_cred);
3012 * stat_args(char *path, struct stat *ub)
3014 * Get file status; this version follows links.
3017 sys_stat(struct sysmsg *sysmsg, const struct stat_args *uap)
3019 struct nlookupdata nd;
3023 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3025 error = kern_stat(&nd, &st);
3027 error = copyout(&st, uap->ub, sizeof(*uap->ub));
3034 * lstat_args(char *path, struct stat *ub)
3036 * Get file status; this version does not follow links.
3039 sys_lstat(struct sysmsg *sysmsg, const struct lstat_args *uap)
3041 struct nlookupdata nd;
3045 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3047 error = kern_stat(&nd, &st);
3049 error = copyout(&st, uap->ub, sizeof(*uap->ub));
3056 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
3058 * Get status of file pointed to by fd/path.
3061 sys_fstatat(struct sysmsg *sysmsg, const struct fstatat_args *uap)
3063 struct nlookupdata nd;
3069 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3072 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3074 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3075 UIO_USERSPACE, flags);
3077 error = kern_stat(&nd, &st);
3079 error = copyout(&st, uap->sb, sizeof(*uap->sb));
3081 nlookup_done_at(&nd, fp);
3086 kern_pathconf(char *path, int name, int flags, register_t *sysmsg_regp)
3088 struct nlookupdata nd;
3093 error = nlookup_init(&nd, path, UIO_USERSPACE, flags);
3095 error = nlookup(&nd);
3097 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3100 error = VOP_PATHCONF(vp, name, sysmsg_regp);
3107 * pathconf_Args(char *path, int name)
3109 * Get configurable pathname variables.
3112 sys_pathconf(struct sysmsg *sysmsg, const struct pathconf_args *uap)
3114 return (kern_pathconf(uap->path, uap->name, NLC_FOLLOW,
3115 &sysmsg->sysmsg_reg));
3119 * lpathconf_Args(char *path, int name)
3121 * Get configurable pathname variables, but don't follow symlinks.
3124 sys_lpathconf(struct sysmsg *sysmsg, const struct lpathconf_args *uap)
3126 return (kern_pathconf(uap->path, uap->name, 0, &sysmsg->sysmsg_reg));
3131 * kern_readlink isn't properly split yet. There is a copyin burried
3132 * in VOP_READLINK().
3135 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
3137 struct thread *td = curthread;
3143 nd->nl_flags |= NLC_SHAREDLOCK;
3144 if ((error = nlookup(nd)) != 0)
3146 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
3149 if (vp->v_type != VLNK) {
3152 aiov.iov_base = buf;
3153 aiov.iov_len = count;
3154 auio.uio_iov = &aiov;
3155 auio.uio_iovcnt = 1;
3156 auio.uio_offset = 0;
3157 auio.uio_rw = UIO_READ;
3158 auio.uio_segflg = UIO_USERSPACE;
3160 auio.uio_resid = count;
3161 error = VOP_READLINK(vp, &auio, td->td_ucred);
3164 *res = count - auio.uio_resid;
3169 * readlink_args(char *path, char *buf, int count)
3171 * Return target name of a symbolic link.
3174 sys_readlink(struct sysmsg *sysmsg, const struct readlink_args *uap)
3176 struct nlookupdata nd;
3179 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3181 error = kern_readlink(&nd, uap->buf, uap->count,
3182 &sysmsg->sysmsg_result);
3189 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
3191 * Return target name of a symbolic link. The path is relative to the
3192 * directory associated with fd.
3195 sys_readlinkat(struct sysmsg *sysmsg, const struct readlinkat_args *uap)
3197 struct nlookupdata nd;
3201 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
3203 error = kern_readlink(&nd, uap->buf, uap->bufsize,
3204 &sysmsg->sysmsg_result);
3206 nlookup_done_at(&nd, fp);
3211 setfflags(struct vnode *vp, u_long flags)
3213 struct thread *td = curthread;
3218 * Prevent non-root users from setting flags on devices. When
3219 * a device is reused, users can retain ownership of the device
3220 * if they are allowed to set flags and programs assume that
3221 * chown can't fail when done as root.
3223 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
3224 ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
3228 * note: vget is required for any operation that might mod the vnode
3229 * so VINACTIVE is properly cleared.
3231 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3233 vattr.va_flags = flags;
3234 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3241 * chflags(const char *path, u_long flags)
3243 * Change flags of a file given a path name.
3246 sys_chflags(struct sysmsg *sysmsg, const struct chflags_args *uap)
3248 struct nlookupdata nd;
3253 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3255 error = nlookup(&nd);
3257 error = ncp_writechk(&nd.nl_nch);
3259 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3262 error = setfflags(vp, uap->flags);
3269 * lchflags(const char *path, u_long flags)
3271 * Change flags of a file given a path name, but don't follow symlinks.
3274 sys_lchflags(struct sysmsg *sysmsg, const struct lchflags_args *uap)
3276 struct nlookupdata nd;
3281 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3283 error = nlookup(&nd);
3285 error = ncp_writechk(&nd.nl_nch);
3287 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3290 error = setfflags(vp, uap->flags);
3297 * fchflags_args(int fd, u_flags flags)
3299 * Change flags of a file given a file descriptor.
3302 sys_fchflags(struct sysmsg *sysmsg, const struct fchflags_args *uap)
3304 struct thread *td = curthread;
3308 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
3310 if (fp->f_nchandle.ncp)
3311 error = ncp_writechk(&fp->f_nchandle);
3313 error = setfflags((struct vnode *) fp->f_data, uap->flags);
3319 * chflagsat_args(int fd, const char *path, u_long flags, int atflags)
3320 * change flags given a pathname relative to a filedescriptor
3323 sys_chflagsat(struct sysmsg *sysmsg, const struct chflagsat_args *uap)
3325 struct nlookupdata nd;
3331 if (uap->atflags & ~AT_SYMLINK_NOFOLLOW)
3334 lookupflags = (uap->atflags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3337 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, lookupflags);
3339 error = nlookup(&nd);
3341 error = ncp_writechk(&nd.nl_nch);
3343 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3344 nlookup_done_at(&nd, fp);
3346 error = setfflags(vp, uap->flags);
3354 setfmode(struct vnode *vp, int mode)
3356 struct thread *td = curthread;
3361 * note: vget is required for any operation that might mod the vnode
3362 * so VINACTIVE is properly cleared.
3364 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3366 vattr.va_mode = mode & ALLPERMS;
3367 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3368 cache_inval_wxok(vp);
3375 kern_chmod(struct nlookupdata *nd, int mode)
3380 if ((error = nlookup(nd)) != 0)
3382 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3384 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3385 error = setfmode(vp, mode);
3391 * chmod_args(char *path, int mode)
3393 * Change mode of a file given path name.
3396 sys_chmod(struct sysmsg *sysmsg, const struct chmod_args *uap)
3398 struct nlookupdata nd;
3401 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3403 error = kern_chmod(&nd, uap->mode);
3409 * lchmod_args(char *path, int mode)
3411 * Change mode of a file given path name (don't follow links.)
3414 sys_lchmod(struct sysmsg *sysmsg, const struct lchmod_args *uap)
3416 struct nlookupdata nd;
3419 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3421 error = kern_chmod(&nd, uap->mode);
3427 * fchmod_args(int fd, int mode)
3429 * Change mode of a file given a file descriptor.
3432 sys_fchmod(struct sysmsg *sysmsg, const struct fchmod_args *uap)
3434 struct thread *td = curthread;
3438 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
3440 if (fp->f_nchandle.ncp)
3441 error = ncp_writechk(&fp->f_nchandle);
3443 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3449 * fchmodat_args(char *path, int mode)
3451 * Change mode of a file pointed to by fd/path.
3454 sys_fchmodat(struct sysmsg *sysmsg, const struct fchmodat_args *uap)
3456 struct nlookupdata nd;
3461 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3463 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3465 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3466 UIO_USERSPACE, flags);
3468 error = kern_chmod(&nd, uap->mode);
3469 nlookup_done_at(&nd, fp);
3474 setfown(struct mount *mp, struct vnode *vp, uid_t uid, gid_t gid)
3476 struct thread *td = curthread;
3484 * note: vget is required for any operation that might mod the vnode
3485 * so VINACTIVE is properly cleared.
3487 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3488 if ((error = VOP_GETATTR(vp, &vattr)) != 0)
3490 o_uid = vattr.va_uid;
3491 o_gid = vattr.va_gid;
3492 size = vattr.va_size;
3497 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3506 VFS_ACCOUNT(mp, o_uid, o_gid, -size);
3507 VFS_ACCOUNT(mp, uid, gid, size);
3514 kern_chown(struct nlookupdata *nd, int uid, int gid)
3519 if ((error = nlookup(nd)) != 0)
3521 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3523 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3524 error = setfown(nd->nl_nch.mount, vp, uid, gid);
3530 * chown(char *path, int uid, int gid)
3532 * Set ownership given a path name.
3535 sys_chown(struct sysmsg *sysmsg, const struct chown_args *uap)
3537 struct nlookupdata nd;
3540 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3542 error = kern_chown(&nd, uap->uid, uap->gid);
3548 * lchown_args(char *path, int uid, int gid)
3550 * Set ownership given a path name, do not cross symlinks.
3553 sys_lchown(struct sysmsg *sysmsg, const struct lchown_args *uap)
3555 struct nlookupdata nd;
3558 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3560 error = kern_chown(&nd, uap->uid, uap->gid);
3566 * fchown_args(int fd, int uid, int gid)
3568 * Set ownership given a file descriptor.
3571 sys_fchown(struct sysmsg *sysmsg, const struct fchown_args *uap)
3573 struct thread *td = curthread;
3574 struct proc *p = td->td_proc;
3578 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
3580 if (fp->f_nchandle.ncp)
3581 error = ncp_writechk(&fp->f_nchandle);
3583 error = setfown(p->p_fd->fd_ncdir.mount,
3584 (struct vnode *)fp->f_data, uap->uid, uap->gid);
3590 * fchownat(int fd, char *path, int uid, int gid, int flags)
3592 * Set ownership of file pointed to by fd/path.
3595 sys_fchownat(struct sysmsg *sysmsg, const struct fchownat_args *uap)
3597 struct nlookupdata nd;
3602 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3604 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3606 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3607 UIO_USERSPACE, flags);
3609 error = kern_chown(&nd, uap->uid, uap->gid);
3610 nlookup_done_at(&nd, fp);
3616 getutimes(struct timeval *tvp, struct timespec *tsp)
3618 struct timeval tv[2];
3623 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3626 if ((error = itimerfix(tvp)) != 0)
3628 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3629 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3635 getutimens(const struct timespec *ts, struct timespec *newts, int *nullflag)
3637 struct timespec tsnow;
3651 if (newts[0].tv_nsec == UTIME_OMIT && newts[1].tv_nsec == UTIME_OMIT)
3653 if (newts[0].tv_nsec == UTIME_NOW && newts[1].tv_nsec == UTIME_NOW)
3656 if (newts[0].tv_nsec == UTIME_OMIT)
3657 newts[0].tv_sec = VNOVAL;
3658 else if (newts[0].tv_nsec == UTIME_NOW)
3660 else if ((error = itimespecfix(&newts[0])) != 0)
3663 if (newts[1].tv_nsec == UTIME_OMIT)
3664 newts[1].tv_sec = VNOVAL;
3665 else if (newts[1].tv_nsec == UTIME_NOW)
3667 else if ((error = itimespecfix(&newts[1])) != 0)
3674 setutimes(struct vnode *vp, struct vattr *vattr,
3675 const struct timespec *ts, int nullflag)
3677 struct thread *td = curthread;
3681 vattr->va_atime = ts[0];
3682 vattr->va_mtime = ts[1];
3684 vattr->va_vaflags |= VA_UTIMES_NULL;
3685 error = VOP_SETATTR(vp, vattr, td->td_ucred);
3691 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3693 struct timespec ts[2];
3697 if ((error = getutimes(tptr, ts)) != 0)
3700 error = kern_utimensat(nd, tptr ? ts : NULL, 0);
3705 * utimes_args(char *path, struct timeval *tptr)
3707 * Set the access and modification times of a file.
3710 sys_utimes(struct sysmsg *sysmsg, const struct utimes_args *uap)
3712 struct timeval tv[2];
3713 struct nlookupdata nd;
3717 error = copyin(uap->tptr, tv, sizeof(tv));
3721 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3723 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3729 * lutimes_args(char *path, struct timeval *tptr)
3731 * Set the access and modification times of a file.
3734 sys_lutimes(struct sysmsg *sysmsg, const struct lutimes_args *uap)
3736 struct timeval tv[2];
3737 struct nlookupdata nd;
3741 error = copyin(uap->tptr, tv, sizeof(tv));
3745 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3747 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3753 * Set utimes on a file descriptor. The creds used to open the
3754 * file are used to determine whether the operation is allowed
3758 kern_futimens(int fd, struct timespec *ts)
3760 struct thread *td = curthread;
3761 struct timespec newts[2];
3765 struct vattr_lite lva;
3769 error = getutimens(ts, newts, &nullflag);
3772 if ((error = holdvnode(td, fd, &fp)) != 0)
3774 if (fp->f_nchandle.ncp)
3775 error = ncp_writechk(&fp->f_nchandle);
3778 error = vget(vp, LK_EXCLUSIVE);
3780 error = VOP_GETATTR_FP(vp, &vattr, fp);
3782 lva.va_type = vattr.va_type;
3783 lva.va_nlink = vattr.va_nlink;
3784 lva.va_mode = vattr.va_mode;
3785 lva.va_uid = vattr.va_uid;
3786 lva.va_gid = vattr.va_gid;
3787 lva.va_size = vattr.va_size;
3788 lva.va_flags = vattr.va_flags;
3790 error = naccess_lva(&lva, NLC_OWN | NLC_WRITE,
3794 error = setutimes(vp, &vattr, newts, nullflag);
3804 * futimens_args(int fd, struct timespec *ts)
3806 * Set the access and modification times of a file.
3809 sys_futimens(struct sysmsg *sysmsg, const struct futimens_args *uap)
3811 struct timespec ts[2];
3815 error = copyin(uap->ts, ts, sizeof(ts));
3819 error = kern_futimens(uap->fd, uap->ts ? ts : NULL);
3824 kern_futimes(int fd, struct timeval *tptr)
3826 struct timespec ts[2];
3830 if ((error = getutimes(tptr, ts)) != 0)
3833 error = kern_futimens(fd, tptr ? ts : NULL);
3838 * futimes_args(int fd, struct timeval *tptr)
3840 * Set the access and modification times of a file.
3843 sys_futimes(struct sysmsg *sysmsg, const struct futimes_args *uap)
3845 struct timeval tv[2];
3849 error = copyin(uap->tptr, tv, sizeof(tv));
3853 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3858 kern_utimensat(struct nlookupdata *nd, const struct timespec *ts, int flags)
3860 struct timespec newts[2];
3866 if (flags & ~AT_SYMLINK_NOFOLLOW)
3869 error = getutimens(ts, newts, &nullflag);
3873 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3874 if ((error = nlookup(nd)) != 0)
3876 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3878 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3880 if ((error = vn_writechk(vp)) == 0) {
3881 error = vget(vp, LK_EXCLUSIVE);
3883 error = setutimes(vp, &vattr, newts, nullflag);
3892 * utimensat_args(int fd, const char *path, const struct timespec *ts, int flags);
3894 * Set file access and modification times of a file.
3897 sys_utimensat(struct sysmsg *sysmsg, const struct utimensat_args *uap)
3899 struct timespec ts[2];
3900 struct nlookupdata nd;
3906 error = copyin(uap->ts, ts, sizeof(ts));
3911 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3912 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3913 UIO_USERSPACE, flags);
3915 error = kern_utimensat(&nd, uap->ts ? ts : NULL, uap->flags);
3916 nlookup_done_at(&nd, fp);
3921 kern_truncate(struct nlookupdata *nd, off_t length)
3928 uint64_t old_size = 0;
3932 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3933 if ((error = nlookup(nd)) != 0)
3935 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3937 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3939 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
3944 if (vp->v_type == VDIR) {
3948 if (vfs_quota_enabled) {
3949 error = VOP_GETATTR(vp, &vattr);
3950 KASSERT(error == 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3953 old_size = vattr.va_size;
3956 if ((error = vn_writechk(vp)) == 0) {
3958 vattr.va_size = length;
3959 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3960 VFS_ACCOUNT(nd->nl_nch.mount, uid, gid, length - old_size);
3968 * truncate(char *path, int pad, off_t length)
3970 * Truncate a file given its path name.
3973 sys_truncate(struct sysmsg *sysmsg, const struct truncate_args *uap)
3975 struct nlookupdata nd;
3978 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3980 error = kern_truncate(&nd, uap->length);
3986 kern_ftruncate(int fd, off_t length)
3988 struct thread *td = curthread;
3995 uint64_t old_size = 0;
4000 if ((error = holdvnode(td, fd, &fp)) != 0)
4002 if (fp->f_nchandle.ncp) {
4003 error = ncp_writechk(&fp->f_nchandle);
4007 if ((fp->f_flag & FWRITE) == 0) {
4011 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
4015 vp = (struct vnode *)fp->f_data;
4016 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4017 if (vp->v_type == VDIR) {
4023 if (vfs_quota_enabled) {
4024 error = VOP_GETATTR_FP(vp, &vattr, fp);
4025 KASSERT(error == 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
4028 old_size = vattr.va_size;
4031 if ((error = vn_writechk(vp)) == 0) {
4033 vattr.va_size = length;
4034 error = VOP_SETATTR_FP(vp, &vattr, fp->f_cred, fp);
4036 VFS_ACCOUNT(mp, uid, gid, length - old_size);
4045 * ftruncate_args(int fd, int pad, off_t length)
4047 * Truncate a file given a file descriptor.
4050 sys_ftruncate(struct sysmsg *sysmsg, const struct ftruncate_args *uap)
4054 error = kern_ftruncate(uap->fd, uap->length);
4060 kern_fsync(int fd, bool fullsync)
4062 struct thread *td = curthread;
4068 if ((error = holdvnode(td, fd, &fp)) != 0)
4070 vp = (struct vnode *)fp->f_data;
4071 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4072 if ((obj = vp->v_object) != NULL) {
4073 if (vp->v_mount == NULL ||
4074 (vp->v_mount->mnt_kern_flag & MNTK_NOMSYNC) == 0) {
4075 vm_object_page_clean(obj, 0, 0, 0);
4079 VOP_FSYNC_FP(vp, MNT_WAIT, VOP_FSYNC_SYSCALL, fp) :
4080 VOP_FDATASYNC_FP(vp, MNT_WAIT, VOP_FSYNC_SYSCALL, fp);
4081 if (error == 0 && vp->v_mount)
4082 error = buf_fsync(vp);
4092 * Sync an open file.
4095 sys_fsync(struct sysmsg *sysmsg, const struct fsync_args *uap)
4097 return (kern_fsync(uap->fd, true));
4103 * Data-sync an open file.
4106 sys_fdatasync(struct sysmsg *sysmsg, const struct fdatasync_args *uap)
4108 return (kern_fsync(uap->fd, false));
4114 * NOTE: error == 0 and nl_dvp is NULL indicates a mount point, operation
4115 * disallowed. e.g. /var/cache where /var/cache is a null-mount, for
4119 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
4121 struct nchandle fnchd;
4122 struct nchandle tnchd;
4123 struct namecache *ncp;
4127 struct mount *userenlk;
4133 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
4134 if ((error = nlookup(fromnd)) != 0)
4138 * Attempt to rename a mount point (from or to)
4140 if (error == 0 && fromnd->nl_dvp == NULL)
4143 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
4145 fnchd.mount = fromnd->nl_nch.mount;
4149 * unlock the source nch so we can lookup the target nch without
4150 * deadlocking. The target may or may not exist so we do not check
4151 * for a target vp like kern_mkdir() and other creation functions do.
4153 * The source and target directories are ref'd and rechecked after
4154 * everything is relocked to determine if the source or target file
4157 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
4158 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
4159 fncp_gen = fromnd->nl_nch.ncp->nc_generation;
4161 if (fromnd->nl_nch.ncp->nc_vp &&
4162 fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
4163 userenlk = fnchd.mount;
4164 cache_unlock(&fromnd->nl_nch);
4165 lockmgr(&userenlk->mnt_renlock, LK_EXCLUSIVE);
4168 cache_unlock(&fromnd->nl_nch);
4174 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
4175 if ((error = nlookup(tond)) != 0) {
4179 tncp_gen = tond->nl_nch.ncp->nc_generation;
4182 * Attempt to rename a mount point (from or to)
4184 if (error == 0 && tond->nl_dvp == NULL) {
4190 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
4195 tnchd.mount = tond->nl_nch.mount;
4199 * If the source and target are the same there is nothing to do
4201 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
4209 * Mount points cannot be renamed or overwritten
4211 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
4221 * Lock all four namecache entries. tond is already locked.
4223 cache_lock4_tondlocked(&fnchd, &fromnd->nl_nch,
4224 &tnchd, &tond->nl_nch,
4225 fromnd->nl_cred, tond->nl_cred);
4226 fromnd->nl_flags |= NLC_NCPISLOCKED;
4229 * If the namecache generation changed for either fromnd or tond,
4232 if (((fromnd->nl_nch.ncp->nc_generation - fncp_gen) & ~1) ||
4233 ((tond->nl_nch.ncp->nc_generation - tncp_gen) & ~1))
4235 krateprintf(&krate_rename,
4236 "kern_rename: retry due to race on: "
4237 "\"%s\" -> \"%s\" (%d,%d)\n",
4238 fromnd->nl_nch.ncp->nc_name,
4239 tond->nl_nch.ncp->nc_name,
4240 fromnd->nl_nch.ncp->nc_generation - fncp_gen,
4241 tond->nl_nch.ncp->nc_generation - tncp_gen);
4247 * If either fromnd or tond are marked destroyed a ripout occured
4248 * out from under us and we must retry.
4250 if ((fromnd->nl_nch.ncp->nc_flag & (NCF_DESTROYED | NCF_UNRESOLVED)) ||
4251 fromnd->nl_nch.ncp->nc_vp == NULL ||
4252 (tond->nl_nch.ncp->nc_flag & (NCF_DESTROYED | NCF_UNRESOLVED))) {
4253 krateprintf(&krate_rename,
4254 "kern_rename: retry due to ripout on: "
4255 "\"%s\" -> \"%s\"\n",
4256 fromnd->nl_nch.ncp->nc_name,
4257 tond->nl_nch.ncp->nc_name);
4263 * Make sure the parent directories linkages are the same. We have
4264 * already checked that fromnd and tond are not mount points so this
4265 * should not loop forever on a cross-mount.
4267 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
4268 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
4274 * Both the source and target must be within the same filesystem and
4275 * in the same filesystem as their parent directories within the
4276 * namecache topology.
4278 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
4281 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
4282 mp != tond->nl_nch.mount) {
4288 * Make sure the mount point is writable
4290 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
4295 * If the target exists and either the source or target is a directory,
4296 * then both must be directories.
4298 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
4301 if (tond->nl_nch.ncp->nc_vp) {
4302 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
4304 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
4305 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
4307 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
4313 * You cannot rename a source into itself or a subdirectory of itself.
4314 * We check this by travsersing the target directory upwards looking
4315 * for a match against the source.
4317 * Only required when renaming a directory, in which case userenlk is
4320 if (__predict_false(userenlk && error == 0)) {
4321 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
4322 if (fromnd->nl_nch.ncp == ncp) {
4330 * Even though the namespaces are different, they may still represent
4331 * hardlinks to the same file. The filesystem might have a hard time
4332 * with this so we issue a NREMOVE of the source instead of a NRENAME
4333 * when we detect the situation.
4336 fdvp = fromnd->nl_dvp;
4337 tdvp = tond->nl_dvp;
4338 if (fdvp == NULL || tdvp == NULL) {
4340 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
4341 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
4344 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
4345 fdvp, tdvp, tond->nl_cred);
4353 lockmgr(&userenlk->mnt_renlock, LK_RELEASE);
4358 * rename_args(char *from, char *to)
4360 * Rename files. Source and destination must either both be directories,
4361 * or both not be directories. If target is a directory, it must be empty.
4364 sys_rename(struct sysmsg *sysmsg, const struct rename_args *uap)
4366 struct nlookupdata fromnd, tond;
4370 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
4372 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
4374 error = kern_rename(&fromnd, &tond);
4375 nlookup_done(&tond);
4377 nlookup_done(&fromnd);
4378 } while (error == EAGAIN);
4383 * renameat_args(int oldfd, char *old, int newfd, char *new)
4385 * Rename files using paths relative to the directories associated with
4386 * oldfd and newfd. Source and destination must either both be directories,
4387 * or both not be directories. If target is a directory, it must be empty.
4390 sys_renameat(struct sysmsg *sysmsg, const struct renameat_args *uap)
4392 struct nlookupdata oldnd, newnd;
4393 struct file *oldfp, *newfp;
4397 error = nlookup_init_at(&oldnd, &oldfp,
4398 uap->oldfd, uap->old,
4401 error = nlookup_init_at(&newnd, &newfp,
4402 uap->newfd, uap->new,
4405 error = kern_rename(&oldnd, &newnd);
4406 nlookup_done_at(&newnd, newfp);
4408 nlookup_done_at(&oldnd, oldfp);
4409 } while (error == EAGAIN);
4414 kern_mkdir(struct nlookupdata *nd, int mode)
4416 struct thread *td = curthread;
4417 struct proc *p = td->td_proc;
4423 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
4424 if ((error = nlookup(nd)) != 0)
4427 if (nd->nl_nch.ncp->nc_vp)
4429 if (nd->nl_dvp == NULL)
4431 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4434 vattr.va_type = VDIR;
4435 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
4438 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
4445 * mkdir_args(char *path, int mode)
4447 * Make a directory file.
4450 sys_mkdir(struct sysmsg *sysmsg, const struct mkdir_args *uap)
4452 struct nlookupdata nd;
4455 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4457 error = kern_mkdir(&nd, uap->mode);
4463 * mkdirat_args(int fd, char *path, mode_t mode)
4465 * Make a directory file. The path is relative to the directory associated
4469 sys_mkdirat(struct sysmsg *sysmsg, const struct mkdirat_args *uap)
4471 struct nlookupdata nd;
4475 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
4477 error = kern_mkdir(&nd, uap->mode);
4478 nlookup_done_at(&nd, fp);
4483 kern_rmdir(struct nlookupdata *nd)
4488 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
4489 if ((error = nlookup(nd)) != 0)
4493 * Do not allow directories representing mount points to be
4494 * deleted, even if empty. Check write perms on mount point
4495 * in case the vnode is aliased (aka nullfs).
4497 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
4499 if (nd->nl_dvp == NULL)
4501 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4503 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
4508 * rmdir_args(char *path)
4510 * Remove a directory file.
4513 sys_rmdir(struct sysmsg *sysmsg, const struct rmdir_args *uap)
4515 struct nlookupdata nd;
4518 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4520 error = kern_rmdir(&nd);
4526 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
4527 enum uio_seg direction)
4529 struct thread *td = curthread;
4537 if ((error = holdvnode(td, fd, &fp)) != 0)
4539 if ((fp->f_flag & FREAD) == 0) {
4543 vp = (struct vnode *)fp->f_data;
4544 if (vp->v_type != VDIR) {
4548 aiov.iov_base = buf;
4549 aiov.iov_len = count;
4550 auio.uio_iov = &aiov;
4551 auio.uio_iovcnt = 1;
4552 auio.uio_rw = UIO_READ;
4553 auio.uio_segflg = direction;
4555 auio.uio_resid = count;
4556 loff = auio.uio_offset = fp->f_offset;
4557 error = VOP_READDIR_FP(vp, &auio, fp->f_cred, &eofflag, NULL, NULL, fp);
4558 fp->f_offset = auio.uio_offset;
4563 * WARNING! *basep may not be wide enough to accomodate the
4564 * seek offset. XXX should we hack this to return the upper 32 bits
4565 * for offsets greater then 4G?
4568 *basep = (long)loff;
4570 *res = count - auio.uio_resid;
4577 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4579 * Read a block of directory entries in a file system independent format.
4582 sys_getdirentries(struct sysmsg *sysmsg, const struct getdirentries_args *uap)
4587 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
4588 &sysmsg->sysmsg_result, UIO_USERSPACE);
4590 if (error == 0 && uap->basep)
4591 error = copyout(&base, uap->basep, sizeof(*uap->basep));
4596 * getdents_args(int fd, char *buf, size_t count)
4599 sys_getdents(struct sysmsg *sysmsg, const struct getdents_args *uap)
4603 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
4604 &sysmsg->sysmsg_result, UIO_USERSPACE);
4610 * Set the mode mask for creation of filesystem nodes.
4612 * umask(int newmask)
4615 sys_umask(struct sysmsg *sysmsg, const struct umask_args *uap)
4617 struct thread *td = curthread;
4618 struct proc *p = td->td_proc;
4619 struct filedesc *fdp;
4622 sysmsg->sysmsg_result = fdp->fd_cmask;
4623 fdp->fd_cmask = uap->newmask & ALLPERMS;
4628 * revoke(char *path)
4630 * Void all references to file by ripping underlying filesystem
4634 sys_revoke(struct sysmsg *sysmsg, const struct revoke_args *uap)
4636 struct nlookupdata nd;
4643 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4645 error = nlookup(&nd);
4647 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4648 cred = crhold(nd.nl_cred);
4652 error = VOP_GETATTR(vp, &vattr);
4653 if (error == 0 && cred->cr_uid != vattr.va_uid)
4654 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4655 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4657 error = vrevoke(vp, cred);
4658 } else if (error == 0) {
4659 error = vrevoke(vp, cred);
4669 * getfh_args(char *fname, fhandle_t *fhp)
4671 * Get (NFS) file handle
4673 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4674 * mount. This allows nullfs mounts to be explicitly exported.
4676 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4678 * nullfs mounts of subdirectories are not safe. That is, it will
4679 * work, but you do not really have protection against access to
4680 * the related parent directories.
4683 sys_getfh(struct sysmsg *sysmsg, const struct getfh_args *uap)
4685 struct thread *td = curthread;
4686 struct nlookupdata nd;
4693 * Must be super user
4695 if ((error = priv_check(td, PRIV_ROOT)) != 0)
4699 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4701 error = nlookup(&nd);
4703 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4704 mp = nd.nl_nch.mount;
4707 bzero(&fh, sizeof(fh));
4708 fh.fh_fsid = mp->mnt_stat.f_fsid;
4709 error = VFS_VPTOFH(vp, &fh.fh_fid);
4712 error = copyout(&fh, uap->fhp, sizeof(fh));
4718 * fhopen_args(const struct fhandle *u_fhp, int flags)
4720 * syscall for the rpc.lockd to use to translate a NFS file handle into
4721 * an open descriptor.
4723 * warning: do not remove the priv_check() call or this becomes one giant
4727 sys_fhopen(struct sysmsg *sysmsg, const struct fhopen_args *uap)
4729 struct thread *td = curthread;
4730 struct filedesc *fdp = td->td_proc->p_fd;
4735 struct vattr *vap = &vat;
4737 int fmode, mode, error = 0, type;
4743 * Must be super user
4745 error = priv_check(td, PRIV_ROOT);
4749 fmode = FFLAGS(uap->flags);
4752 * Why not allow a non-read/write open for our lockd?
4754 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4756 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4761 * Find the mount point
4763 mp = vfs_getvfs(&fhp.fh_fsid);
4768 /* now give me my vnode, it gets returned to me locked */
4769 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4773 * from now on we have to make sure not
4774 * to forget about the vnode
4775 * any error that causes an abort must vput(vp)
4776 * just set error = err and 'goto bad;'.
4782 if (vp->v_type == VLNK) {
4786 if (vp->v_type == VSOCK) {
4791 if (fmode & (FWRITE | O_TRUNC)) {
4792 if (vp->v_type == VDIR) {
4796 error = vn_writechk(vp);
4804 error = VOP_ACCESS(vp, mode, td->td_ucred);
4808 if (fmode & O_TRUNC) {
4809 vn_unlock(vp); /* XXX */
4810 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4813 error = VOP_SETATTR(vp, vap, td->td_ucred);
4819 * VOP_OPEN needs the file pointer so it can potentially override
4822 * WARNING! no f_nchandle will be associated when fhopen()ing a
4825 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4827 error = VOP_OPEN(vp, fmode, td->td_ucred, &nfp);
4832 * setting f_ops this way prevents VOP_CLOSE from being
4833 * called or fdrop() releasing the vp from v_data. Since
4834 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4836 fp->f_ops = &badfileops;
4842 * The fp is given its own reference, we still have our ref and lock.
4844 * Assert that all regular files must be created with a VM object.
4846 if (vp->v_type == VREG && vp->v_object == NULL) {
4847 kprintf("fhopen: regular file did not "
4848 "have VM object: %p\n",
4854 * The open was successful. Handle any locking requirements.
4856 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4857 lf.l_whence = SEEK_SET;
4860 if (fmode & O_EXLOCK)
4861 lf.l_type = F_WRLCK;
4863 lf.l_type = F_RDLCK;
4864 if (fmode & FNONBLOCK)
4869 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK,
4872 * release our private reference.
4874 fsetfd(fdp, NULL, indx);
4879 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4880 atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */
4884 * Clean up. Associate the file pointer with the previously
4885 * reserved descriptor and return it.
4888 if (uap->flags & O_CLOEXEC)
4889 fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
4890 fsetfd(fdp, fp, indx);
4892 sysmsg->sysmsg_result = indx;
4898 fsetfd(fdp, NULL, indx);
4909 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4912 sys_fhstat(struct sysmsg *sysmsg, const struct fhstat_args *uap)
4914 struct thread *td = curthread;
4922 * Must be super user
4924 error = priv_check(td, PRIV_ROOT);
4928 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4932 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4935 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4936 error = vn_stat(vp, &sb, td->td_ucred);
4941 error = copyout(&sb, uap->sb, sizeof(sb));
4949 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4952 sys_fhstatfs(struct sysmsg *sysmsg, const struct fhstatfs_args *uap)
4954 struct thread *td = curthread;
4955 struct proc *p = td->td_proc;
4960 char *fullpath, *freepath;
4965 * Must be super user
4967 if ((error = priv_check(td, PRIV_ROOT)))
4970 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4973 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4977 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4982 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4987 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4990 error = mount_path(p, mp, &fullpath, &freepath);
4993 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4994 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4995 kfree(freepath, M_TEMP);
4997 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4998 if (priv_check(td, PRIV_ROOT)) {
4999 bcopy(sp, &sb, sizeof(sb));
5000 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
5003 error = copyout(sp, uap->buf, sizeof(*sp));
5012 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
5015 sys_fhstatvfs(struct sysmsg *sysmsg, const struct fhstatvfs_args *uap)
5017 struct thread *td = curthread;
5018 struct proc *p = td->td_proc;
5026 * Must be super user
5028 if ((error = priv_check(td, PRIV_ROOT)))
5031 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
5034 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
5038 if (p != NULL && !chroot_visible_mnt(mp, p)) {
5043 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
5046 sp = &mp->mnt_vstat;
5048 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
5052 if (mp->mnt_flag & MNT_RDONLY)
5053 sp->f_flag |= ST_RDONLY;
5054 if (mp->mnt_flag & MNT_NOSUID)
5055 sp->f_flag |= ST_NOSUID;
5056 error = copyout(sp, uap->buf, sizeof(*sp));
5065 * Syscall to push extended attribute configuration information into the
5066 * VFS. Accepts a path, which it converts to a mountpoint, as well as
5067 * a command (int cmd), and attribute name and misc data. For now, the
5068 * attribute name is left in userspace for consumption by the VFS_op.
5069 * It will probably be changed to be copied into sysspace by the
5070 * syscall in the future, once issues with various consumers of the
5071 * attribute code have raised their hands.
5073 * Currently this is used only by UFS Extended Attributes.
5076 sys_extattrctl(struct sysmsg *sysmsg, const struct extattrctl_args *uap)
5078 struct nlookupdata nd;
5080 char attrname[EXTATTR_MAXNAMELEN];
5088 if (error == 0 && uap->filename) {
5089 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
5092 error = nlookup(&nd);
5094 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
5098 if (error == 0 && uap->attrname) {
5099 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
5104 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
5106 error = nlookup(&nd);
5108 error = ncp_writechk(&nd.nl_nch);
5110 error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
5112 uap->attrname, nd.nl_cred);
5121 * Syscall to get a named extended attribute on a file or directory.
5124 sys_extattr_set_file(struct sysmsg *sysmsg,
5125 const struct extattr_set_file_args *uap)
5127 char attrname[EXTATTR_MAXNAMELEN];
5128 struct nlookupdata nd;
5134 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
5140 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
5142 error = nlookup(&nd);
5144 error = ncp_writechk(&nd.nl_nch);
5146 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
5152 bzero(&auio, sizeof(auio));
5153 aiov.iov_base = uap->data;
5154 aiov.iov_len = uap->nbytes;
5155 auio.uio_iov = &aiov;
5156 auio.uio_iovcnt = 1;
5157 auio.uio_offset = 0;
5158 auio.uio_resid = uap->nbytes;
5159 auio.uio_rw = UIO_WRITE;
5160 auio.uio_td = curthread;
5162 error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
5171 * Syscall to get a named extended attribute on a file or directory.
5174 sys_extattr_get_file(struct sysmsg *sysmsg,
5175 const struct extattr_get_file_args *uap)
5177 char attrname[EXTATTR_MAXNAMELEN];
5178 struct nlookupdata nd;
5184 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
5190 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
5192 error = nlookup(&nd);
5194 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_SHARED, &vp);
5200 bzero(&auio, sizeof(auio));
5201 aiov.iov_base = uap->data;
5202 aiov.iov_len = uap->nbytes;
5203 auio.uio_iov = &aiov;
5204 auio.uio_iovcnt = 1;
5205 auio.uio_offset = 0;
5206 auio.uio_resid = uap->nbytes;
5207 auio.uio_rw = UIO_READ;
5208 auio.uio_td = curthread;
5210 error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
5212 sysmsg->sysmsg_result = uap->nbytes - auio.uio_resid;
5220 * Syscall to delete a named extended attribute from a file or directory.
5221 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
5224 sys_extattr_delete_file(struct sysmsg *sysmsg,
5225 const struct extattr_delete_file_args *uap)
5227 char attrname[EXTATTR_MAXNAMELEN];
5228 struct nlookupdata nd;
5232 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
5236 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
5238 error = nlookup(&nd);
5240 error = ncp_writechk(&nd.nl_nch);
5242 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
5244 error = VOP_SETEXTATTR(vp, uap->attrnamespace,
5245 attrname, NULL, nd.nl_cred);
5254 * Determine if the mount is visible to the process.
5257 chroot_visible_mnt(struct mount *mp, struct proc *p)
5259 struct nchandle nch;
5262 * Traverse from the mount point upwards. If we hit the process
5263 * root then the mount point is visible to the process.
5265 nch = mp->mnt_ncmountpt;
5267 if (nch.mount == p->p_fd->fd_nrdir.mount &&
5268 nch.ncp == p->p_fd->fd_nrdir.ncp) {
5271 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
5272 nch = nch.mount->mnt_ncmounton;
5274 nch.ncp = nch.ncp->nc_parent;
5279 * If the mount point is not visible to the process, but the
5280 * process root is in a subdirectory of the mount, return
5283 if (p->p_fd->fd_nrdir.mount == mp)
5289 /* Sets priv to PRIV_ROOT in case no matching fs */
5291 get_fspriv(const char *fsname)
5294 if (strncmp("null", fsname, 5) == 0) {
5295 return PRIV_VFS_MOUNT_NULLFS;
5296 } else if (strncmp(fsname, "tmpfs", 6) == 0) {
5297 return PRIV_VFS_MOUNT_TMPFS;
5304 sys___realpath(struct sysmsg *sysmsg, const struct __realpath_args *uap)
5306 struct nlookupdata nd;
5313 * Invalid length if less than 0. 0 is allowed
5315 if ((ssize_t)uap->len < 0)
5320 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
5324 nd.nl_flags |= NLC_SHAREDLOCK;
5325 error = nlookup(&nd);
5329 if (nd.nl_nch.ncp->nc_vp == NULL) {
5335 * Shortcut test for existence.
5337 if (uap->len == 0) {
5338 error = ENAMETOOLONG;
5343 * Obtain the path relative to the process root. The nch must not
5344 * be locked for the cache_fullpath() call.
5346 if (nd.nl_flags & NLC_NCPISLOCKED) {
5347 nd.nl_flags &= ~NLC_NCPISLOCKED;
5348 cache_unlock(&nd.nl_nch);
5350 error = cache_fullpath(curproc, &nd.nl_nch, NULL, &rbuf, &fbuf, 0);
5354 rlen = (ssize_t)strlen(rbuf);
5355 if (rlen >= uap->len) {
5356 error = ENAMETOOLONG;
5359 error = copyout(rbuf, uap->buf, rlen + 1);
5361 sysmsg->sysmsg_szresult = rlen;
5365 kfree(fbuf, M_TEMP);
5371 sys_posix_fallocate(struct sysmsg *sysmsg, const struct posix_fallocate_args *uap)
5373 return (kern_posix_fallocate(uap->fd, uap->offset, uap->len));
5377 kern_posix_fallocate(int fd, off_t offset, off_t len)
5379 struct thread *td = curthread;
5384 if (offset < 0 || len <= 0)
5386 /* Check for wrap. */
5387 if (offset > OFF_MAX - len)
5390 fp = holdfp(td, fd, -1);
5394 switch (fp->f_type) {
5406 if ((fp->f_flag & FWRITE) == 0) {
5412 if (vp->v_type != VREG) {
5417 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
5418 error = VOP_ALLOCATE(vp, offset, len);