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/sysproto.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>
70 #include <sys/mplock2.h>
73 #include <vm/vm_object.h>
74 #include <vm/vm_page.h>
76 #include <machine/limits.h>
77 #include <machine/stdarg.h>
79 static void mount_warning(struct mount *mp, const char *ctl, ...)
81 static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb);
82 static int checkvp_chdir (struct vnode *vn, struct thread *td);
83 static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch);
84 static int chroot_refuse_vdir_fds (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 *, int);
91 static int setutimes (struct vnode *, struct vattr *,
92 const struct timespec *, int);
93 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");
99 * Virtual File System System Calls
103 * Mount a file system.
105 * mount_args(char *type, char *path, int flags, caddr_t data)
110 sys_mount(struct mount_args *uap)
112 struct thread *td = curthread;
115 struct mount *mp, *nullmp;
116 struct vfsconf *vfsp;
117 int error, flag = 0, flag2 = 0;
120 struct nlookupdata nd;
121 char fstypename[MFSNAMELEN];
129 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
133 * Do not allow NFS export by non-root users.
135 if (uap->flags & MNT_EXPORTED) {
136 error = priv_check(td, PRIV_ROOT);
141 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
143 if (priv_check(td, PRIV_ROOT))
144 uap->flags |= MNT_NOSUID | MNT_NODEV;
147 * Lookup the requested path and extract the nch and vnode.
149 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
151 if ((error = nlookup(&nd)) == 0) {
152 if (nd.nl_nch.ncp->nc_vp == NULL)
162 * If the target filesystem is resolved via a nullfs mount, then
163 * nd.nl_nch.mount will be pointing to the nullfs mount structure
164 * instead of the target file system. We need it in case we are
167 nullmp = nd.nl_nch.mount;
170 * Extract the locked+refd ncp and cleanup the nd structure
173 cache_zero(&nd.nl_nch);
176 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
177 (mp = cache_findmount(&nch)) != NULL) {
186 * now we have the locked ref'd nch and unreferenced vnode.
189 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
196 * Extract the file system type. We need to know this early, to take
197 * appropriate actions if we are dealing with a nullfs.
199 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
206 * Now we have an unlocked ref'd nch and a locked ref'd vp
208 if (uap->flags & MNT_UPDATE) {
209 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
216 if (strncmp(fstypename, "null", 5) == 0) {
224 flag2 = mp->mnt_kern_flag;
226 * We only allow the filesystem to be reloaded if it
227 * is currently mounted read-only.
229 if ((uap->flags & MNT_RELOAD) &&
230 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
233 error = EOPNOTSUPP; /* Needs translation */
237 * Only root, or the user that did the original mount is
238 * permitted to update it.
240 if (mp->mnt_stat.f_owner != cred->cr_uid &&
241 (error = priv_check(td, PRIV_ROOT))) {
246 if (vfs_busy(mp, LK_NOWAIT)) {
260 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
261 lwkt_gettoken(&mp->mnt_token);
267 * If the user is not root, ensure that they own the directory
268 * onto which we are attempting to mount.
270 if ((error = VOP_GETATTR(vp, &va)) ||
271 (va.va_uid != cred->cr_uid &&
272 (error = priv_check(td, PRIV_ROOT)))) {
277 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
282 if (vp->v_type != VDIR) {
288 if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
294 vfsp = vfsconf_find_by_name(fstypename);
298 /* Only load modules for root (very important!) */
299 if ((error = priv_check(td, PRIV_ROOT)) != 0) {
304 error = linker_load_file(fstypename, &lf);
305 if (error || lf == NULL) {
313 /* lookup again, see if the VFS was loaded */
314 vfsp = vfsconf_find_by_name(fstypename);
317 linker_file_unload(lf);
332 * Allocate and initialize the filesystem.
334 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
336 vfs_busy(mp, LK_NOWAIT);
337 mp->mnt_op = vfsp->vfc_vfsops;
339 vfsp->vfc_refcount++;
340 mp->mnt_stat.f_type = vfsp->vfc_typenum;
341 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
342 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
343 mp->mnt_stat.f_owner = cred->cr_uid;
344 lwkt_gettoken(&mp->mnt_token);
348 * (per-mount token acquired at this point)
350 * Set the mount level flags.
352 if (uap->flags & MNT_RDONLY)
353 mp->mnt_flag |= MNT_RDONLY;
354 else if (mp->mnt_flag & MNT_RDONLY)
355 mp->mnt_kern_flag |= MNTK_WANTRDWR;
356 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
357 MNT_SYNCHRONOUS | MNT_ASYNC | MNT_NOATIME |
358 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
359 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
360 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
361 MNT_NODEV | MNT_SYNCHRONOUS | MNT_ASYNC | MNT_FORCE |
362 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
363 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
365 * Mount the filesystem.
366 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
369 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
370 if (mp->mnt_flag & MNT_UPDATE) {
371 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
372 mp->mnt_flag &= ~MNT_RDONLY;
373 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
374 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
377 mp->mnt_kern_flag = flag2;
379 lwkt_reltoken(&mp->mnt_token);
385 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
388 * Put the new filesystem on the mount list after root. The mount
389 * point gets its own mnt_ncmountpt (unless the VFS already set one
390 * up) which represents the root of the mount. The lookup code
391 * detects the mount point going forward and checks the root of
392 * the mount going backwards.
394 * It is not necessary to invalidate or purge the vnode underneath
395 * because elements under the mount will be given their own glue
399 if (mp->mnt_ncmountpt.ncp == NULL) {
401 * allocate, then unlock, but leave the ref intact
403 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
404 cache_unlock(&mp->mnt_ncmountpt);
406 mp->mnt_ncmounton = nch; /* inherits ref */
407 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
408 cache_ismounting(mp);
410 mountlist_insert(mp, MNTINS_LAST);
412 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
413 error = vfs_allocate_syncvnode(mp);
414 lwkt_reltoken(&mp->mnt_token);
416 error = VFS_START(mp, 0);
419 vn_syncer_thr_stop(mp);
420 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
421 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
422 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
423 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
424 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
425 mp->mnt_vfc->vfc_refcount--;
426 lwkt_reltoken(&mp->mnt_token);
437 * Scan all active processes to see if any of them have a current
438 * or root directory onto which the new filesystem has just been
439 * mounted. If so, replace them with the new mount point.
441 * Both old_nch and new_nch are ref'd on call but not locked.
442 * new_nch must be temporarily locked so it can be associated with the
443 * vnode representing the root of the mount point.
445 struct checkdirs_info {
446 struct nchandle old_nch;
447 struct nchandle new_nch;
448 struct vnode *old_vp;
449 struct vnode *new_vp;
452 static int checkdirs_callback(struct proc *p, void *data);
455 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
457 struct checkdirs_info info;
463 * If the old mount point's vnode has a usecount of 1, it is not
464 * being held as a descriptor anywhere.
466 olddp = old_nch->ncp->nc_vp;
467 if (olddp == NULL || VREFCNT(olddp) == 1)
471 * Force the root vnode of the new mount point to be resolved
472 * so we can update any matching processes.
475 if (VFS_ROOT(mp, &newdp))
476 panic("mount: lost mount");
479 vn_lock(newdp, LK_EXCLUSIVE | LK_RETRY);
480 cache_setunresolved(new_nch);
481 cache_setvp(new_nch, newdp);
482 cache_unlock(new_nch);
485 * Special handling of the root node
487 if (rootvnode == olddp) {
489 vfs_cache_setroot(newdp, cache_hold(new_nch));
493 * Pass newdp separately so the callback does not have to access
494 * it via new_nch->ncp->nc_vp.
496 info.old_nch = *old_nch;
497 info.new_nch = *new_nch;
499 allproc_scan(checkdirs_callback, &info);
504 * NOTE: callback is not MP safe because the scanned process's filedesc
505 * structure can be ripped out from under us, amoung other things.
508 checkdirs_callback(struct proc *p, void *data)
510 struct checkdirs_info *info = data;
511 struct filedesc *fdp;
512 struct nchandle ncdrop1;
513 struct nchandle ncdrop2;
514 struct vnode *vprele1;
515 struct vnode *vprele2;
517 if ((fdp = p->p_fd) != NULL) {
518 cache_zero(&ncdrop1);
519 cache_zero(&ncdrop2);
524 * MPUNSAFE - XXX fdp can be pulled out from under a
527 * A shared filedesc is ok, we don't have to copy it
528 * because we are making this change globally.
530 spin_lock(&fdp->fd_spin);
531 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
532 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
533 vprele1 = fdp->fd_cdir;
535 fdp->fd_cdir = info->new_vp;
536 ncdrop1 = fdp->fd_ncdir;
537 cache_copy(&info->new_nch, &fdp->fd_ncdir);
539 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
540 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
541 vprele2 = fdp->fd_rdir;
543 fdp->fd_rdir = info->new_vp;
544 ncdrop2 = fdp->fd_nrdir;
545 cache_copy(&info->new_nch, &fdp->fd_nrdir);
547 spin_unlock(&fdp->fd_spin);
549 cache_drop(&ncdrop1);
551 cache_drop(&ncdrop2);
561 * Unmount a file system.
563 * Note: unmount takes a path to the vnode mounted on as argument,
564 * not special file (as before).
566 * umount_args(char *path, int flags)
571 sys_unmount(struct unmount_args *uap)
573 struct thread *td = curthread;
574 struct proc *p __debugvar = td->td_proc;
575 struct mount *mp = NULL;
576 struct nlookupdata nd;
581 if (td->td_ucred->cr_prison != NULL) {
585 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
588 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
590 error = nlookup(&nd);
594 mp = nd.nl_nch.mount;
597 * Only root, or the user that did the original mount is
598 * permitted to unmount this filesystem.
600 if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) &&
601 (error = priv_check(td, PRIV_ROOT)))
605 * Don't allow unmounting the root file system.
607 if (mp->mnt_flag & MNT_ROOTFS) {
613 * Must be the root of the filesystem
615 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
623 error = dounmount(mp, uap->flags);
630 * Do the actual file system unmount.
633 dounmount_interlock(struct mount *mp)
635 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
637 mp->mnt_kern_flag |= MNTK_UNMOUNT;
642 unmount_allproc_cb(struct proc *p, void *arg)
646 if (p->p_textnch.ncp == NULL)
649 mp = (struct mount *)arg;
650 if (p->p_textnch.mount == mp)
651 cache_drop(&p->p_textnch);
657 dounmount(struct mount *mp, int flags)
659 struct namecache *ncp;
668 lwkt_gettoken(&mp->mnt_token);
670 * Exclusive access for unmounting purposes
672 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
676 * Allow filesystems to detect that a forced unmount is in progress.
678 if (flags & MNT_FORCE)
679 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
680 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_TIMELOCK);
681 error = lockmgr(&mp->mnt_lock, lflags);
683 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
684 if (mp->mnt_kern_flag & MNTK_MWAIT) {
685 mp->mnt_kern_flag &= ~MNTK_MWAIT;
691 if (mp->mnt_flag & MNT_EXPUBLIC)
692 vfs_setpublicfs(NULL, NULL, NULL);
694 vfs_msync(mp, MNT_WAIT);
695 async_flag = mp->mnt_flag & MNT_ASYNC;
696 mp->mnt_flag &=~ MNT_ASYNC;
699 * If this filesystem isn't aliasing other filesystems,
700 * try to invalidate any remaining namecache entries and
701 * check the count afterwords.
703 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
704 cache_lock(&mp->mnt_ncmountpt);
705 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
706 cache_unlock(&mp->mnt_ncmountpt);
708 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
709 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
710 allproc_scan(&unmount_allproc_cb, mp);
713 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
714 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
716 if ((flags & MNT_FORCE) == 0) {
718 mount_warning(mp, "Cannot unmount: "
724 mount_warning(mp, "Forced unmount: "
735 * Decomission our special mnt_syncer vnode. This also stops
736 * the vnlru code. If we are unable to unmount we recommission
739 * Then sync the filesystem.
741 if ((vp = mp->mnt_syncer) != NULL) {
742 mp->mnt_syncer = NULL;
743 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
746 if ((mp->mnt_flag & MNT_RDONLY) == 0)
747 VFS_SYNC(mp, MNT_WAIT);
750 * nchandle records ref the mount structure. Expect a count of 1
751 * (our mount->mnt_ncmountpt).
753 * Scans can get temporary refs on a mountpoint (thought really
754 * heavy duty stuff like cache_findmount() do not).
756 for (retry = 0; retry < 10 && mp->mnt_refs != 1; ++retry) {
757 cache_unmounting(mp);
758 tsleep(&mp->mnt_refs, 0, "mntbsy", hz / 10 + 1);
760 if (mp->mnt_refs != 1) {
761 if ((flags & MNT_FORCE) == 0) {
762 mount_warning(mp, "Cannot unmount: "
763 "%d mount refs still present",
767 mount_warning(mp, "Forced unmount: "
768 "%d mount refs still present",
775 * So far so good, sync the filesystem once more and
776 * call the VFS unmount code if the sync succeeds.
778 * A filesystem without explicitly initialzed .vfs_sync
779 * fails here since vfs_stdnosync() returns EOPNOTSUPP.
780 * A filesystem needs to implement vfs_stdsync() which
781 * returns 0 even if there is no backing storage.
784 if (((mp->mnt_flag & MNT_RDONLY) ||
785 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
786 (flags & MNT_FORCE)) {
787 error = VFS_UNMOUNT(mp, flags);
792 * If an error occurred we can still recover, restoring the
793 * syncer vnode and misc flags.
796 if (mp->mnt_syncer == NULL)
797 vfs_allocate_syncvnode(mp);
798 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
799 mp->mnt_flag |= async_flag;
800 lockmgr(&mp->mnt_lock, LK_RELEASE);
801 if (mp->mnt_kern_flag & MNTK_MWAIT) {
802 mp->mnt_kern_flag &= ~MNTK_MWAIT;
808 * Clean up any journals still associated with the mount after
809 * filesystem activity has ceased.
811 journal_remove_all_journals(mp,
812 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
814 mountlist_remove(mp);
817 * Remove any installed vnode ops here so the individual VFSs don't
820 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
821 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
822 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
823 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
824 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
826 if (mp->mnt_ncmountpt.ncp != NULL) {
827 nch = mp->mnt_ncmountpt;
828 cache_zero(&mp->mnt_ncmountpt);
829 cache_clrmountpt(&nch);
832 if (mp->mnt_ncmounton.ncp != NULL) {
833 cache_unmounting(mp);
834 nch = mp->mnt_ncmounton;
835 cache_zero(&mp->mnt_ncmounton);
836 cache_clrmountpt(&nch);
840 mp->mnt_vfc->vfc_refcount--;
841 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
842 panic("unmount: dangling vnode");
843 lockmgr(&mp->mnt_lock, LK_RELEASE);
844 if (mp->mnt_kern_flag & MNTK_MWAIT) {
845 mp->mnt_kern_flag &= ~MNTK_MWAIT;
850 * If we reach here and freeok != 0 we must free the mount.
851 * If refs > 1 cycle and wait, just in case someone tried
852 * to busy the mount after we decided to do the unmount.
855 while (mp->mnt_refs > 1) {
856 cache_unmounting(mp);
858 tsleep(&mp->mnt_refs, 0, "umntrwait", hz / 10 + 1);
860 lwkt_reltoken(&mp->mnt_token);
867 lwkt_reltoken(&mp->mnt_token);
873 mount_warning(struct mount *mp, const char *ctl, ...)
880 if (cache_fullpath(NULL, &mp->mnt_ncmounton, NULL,
881 &ptr, &buf, 0) == 0) {
882 kprintf("unmount(%s): ", ptr);
887 kprintf("unmount(%p", mp);
888 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
889 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
898 * Shim cache_fullpath() to handle the case where a process is chrooted into
899 * a subdirectory of a mount. In this case if the root mount matches the
900 * process root directory's mount we have to specify the process's root
901 * directory instead of the mount point, because the mount point might
902 * be above the root directory.
906 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
908 struct nchandle *nch;
910 if (p && p->p_fd->fd_nrdir.mount == mp)
911 nch = &p->p_fd->fd_nrdir;
913 nch = &mp->mnt_ncmountpt;
914 return(cache_fullpath(p, nch, NULL, rb, fb, 0));
918 * Sync each mounted filesystem.
922 static int syncprt = 0;
923 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
926 static int sync_callback(struct mount *mp, void *data);
929 sys_sync(struct sync_args *uap)
931 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
937 sync_callback(struct mount *mp, void *data __unused)
941 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
942 asyncflag = mp->mnt_flag & MNT_ASYNC;
943 mp->mnt_flag &= ~MNT_ASYNC;
944 vfs_msync(mp, MNT_NOWAIT);
945 VFS_SYNC(mp, MNT_NOWAIT);
946 mp->mnt_flag |= asyncflag;
951 /* XXX PRISON: could be per prison flag */
952 static int prison_quotas;
954 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
958 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
960 * Change filesystem quotas.
965 sys_quotactl(struct quotactl_args *uap)
967 struct nlookupdata nd;
974 if (td->td_ucred->cr_prison && !prison_quotas) {
979 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
981 error = nlookup(&nd);
983 mp = nd.nl_nch.mount;
984 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
985 uap->arg, nd.nl_cred);
994 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
995 * void *buf, int buflen)
997 * This function operates on a mount point and executes the specified
998 * operation using the specified control data, and possibly returns data.
1000 * The actual number of bytes stored in the result buffer is returned, 0
1001 * if none, otherwise an error is returned.
1006 sys_mountctl(struct mountctl_args *uap)
1008 struct thread *td = curthread;
1009 struct proc *p = td->td_proc;
1017 * Sanity and permissions checks. We must be root.
1020 if (td->td_ucred->cr_prison != NULL)
1022 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
1023 (error = priv_check(td, PRIV_ROOT)) != 0)
1027 * Argument length checks
1029 if (uap->ctllen < 0 || uap->ctllen > 1024)
1031 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
1033 if (uap->path == NULL)
1037 * Allocate the necessary buffers and copyin data
1039 path = objcache_get(namei_oc, M_WAITOK);
1040 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1045 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
1046 error = copyin(uap->ctl, ctl, uap->ctllen);
1051 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
1054 * Validate the descriptor
1057 fp = holdfp(p->p_fd, uap->fd, -1);
1067 * Execute the internal kernel function and clean up.
1070 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
1074 if (error == 0 && uap->sysmsg_result > 0)
1075 error = copyout(buf, uap->buf, uap->sysmsg_result);
1078 objcache_put(namei_oc, path);
1087 * Execute a mount control operation by resolving the path to a mount point
1088 * and calling vop_mountctl().
1090 * Use the mount point from the nch instead of the vnode so nullfs mounts
1091 * can properly spike the VOP.
1094 kern_mountctl(const char *path, int op, struct file *fp,
1095 const void *ctl, int ctllen,
1096 void *buf, int buflen, int *res)
1100 struct nlookupdata nd;
1105 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1107 error = nlookup(&nd);
1109 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1110 mp = nd.nl_nch.mount;
1117 * Must be the root of the filesystem
1119 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1123 error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen,
1130 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1132 struct thread *td = curthread;
1133 struct proc *p = td->td_proc;
1136 char *fullpath, *freepath;
1139 if ((error = nlookup(nd)) != 0)
1141 mp = nd->nl_nch.mount;
1143 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1146 error = mount_path(p, mp, &fullpath, &freepath);
1149 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1150 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1151 kfree(freepath, M_TEMP);
1153 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1154 bcopy(sp, buf, sizeof(*buf));
1155 /* Only root should have access to the fsid's. */
1156 if (priv_check(td, PRIV_ROOT))
1157 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1162 * statfs_args(char *path, struct statfs *buf)
1164 * Get filesystem statistics.
1167 sys_statfs(struct statfs_args *uap)
1169 struct nlookupdata nd;
1173 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1175 error = kern_statfs(&nd, &buf);
1178 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1183 kern_fstatfs(int fd, struct statfs *buf)
1185 struct thread *td = curthread;
1186 struct proc *p = td->td_proc;
1190 char *fullpath, *freepath;
1194 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1198 * Try to use mount info from any overlays rather than the
1199 * mount info for the underlying vnode, otherwise we will
1200 * fail when operating on null-mounted paths inside a chroot.
1202 if ((mp = fp->f_nchandle.mount) == NULL)
1203 mp = ((struct vnode *)fp->f_data)->v_mount;
1208 if (fp->f_cred == NULL) {
1213 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1216 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1218 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1219 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1220 kfree(freepath, M_TEMP);
1222 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1223 bcopy(sp, buf, sizeof(*buf));
1225 /* Only root should have access to the fsid's. */
1226 if (priv_check(td, PRIV_ROOT))
1227 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1235 * fstatfs_args(int fd, struct statfs *buf)
1237 * Get filesystem statistics.
1240 sys_fstatfs(struct fstatfs_args *uap)
1245 error = kern_fstatfs(uap->fd, &buf);
1248 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1253 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1259 if ((error = nlookup(nd)) != 0)
1261 mp = nd->nl_nch.mount;
1262 sp = &mp->mnt_vstat;
1263 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1267 if (mp->mnt_flag & MNT_RDONLY)
1268 sp->f_flag |= ST_RDONLY;
1269 if (mp->mnt_flag & MNT_NOSUID)
1270 sp->f_flag |= ST_NOSUID;
1271 bcopy(sp, buf, sizeof(*buf));
1276 * statfs_args(char *path, struct statfs *buf)
1278 * Get filesystem statistics.
1281 sys_statvfs(struct statvfs_args *uap)
1283 struct nlookupdata nd;
1287 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1289 error = kern_statvfs(&nd, &buf);
1292 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1297 kern_fstatvfs(int fd, struct statvfs *buf)
1299 struct thread *td = curthread;
1300 struct proc *p = td->td_proc;
1307 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1309 if ((mp = fp->f_nchandle.mount) == NULL)
1310 mp = ((struct vnode *)fp->f_data)->v_mount;
1315 if (fp->f_cred == NULL) {
1319 sp = &mp->mnt_vstat;
1320 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1324 if (mp->mnt_flag & MNT_RDONLY)
1325 sp->f_flag |= ST_RDONLY;
1326 if (mp->mnt_flag & MNT_NOSUID)
1327 sp->f_flag |= ST_NOSUID;
1329 bcopy(sp, buf, sizeof(*buf));
1337 * fstatfs_args(int fd, struct statfs *buf)
1339 * Get filesystem statistics.
1342 sys_fstatvfs(struct fstatvfs_args *uap)
1347 error = kern_fstatvfs(uap->fd, &buf);
1350 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1355 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1357 * Get statistics on all filesystems.
1360 struct getfsstat_info {
1361 struct statfs *sfsp;
1369 static int getfsstat_callback(struct mount *, void *);
1372 sys_getfsstat(struct getfsstat_args *uap)
1374 struct thread *td = curthread;
1375 struct getfsstat_info info;
1377 bzero(&info, sizeof(info));
1379 info.maxcount = uap->bufsize / sizeof(struct statfs);
1380 info.sfsp = uap->buf;
1382 info.flags = uap->flags;
1385 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1386 if (info.sfsp && info.count > info.maxcount)
1387 uap->sysmsg_result = info.maxcount;
1389 uap->sysmsg_result = info.count;
1390 return (info.error);
1394 getfsstat_callback(struct mount *mp, void *data)
1396 struct getfsstat_info *info = data;
1402 if (info->sfsp && info->count < info->maxcount) {
1403 if (info->td->td_proc &&
1404 !chroot_visible_mnt(mp, info->td->td_proc)) {
1410 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1411 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1412 * overrides MNT_WAIT.
1414 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1415 (info->flags & MNT_WAIT)) &&
1416 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1419 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1421 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1423 info->error = error;
1426 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1427 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1428 kfree(freepath, M_TEMP);
1430 error = copyout(sp, info->sfsp, sizeof(*sp));
1432 info->error = error;
1442 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1443 long bufsize, int flags)
1445 * Get statistics on all filesystems.
1448 struct getvfsstat_info {
1449 struct statfs *sfsp;
1450 struct statvfs *vsfsp;
1458 static int getvfsstat_callback(struct mount *, void *);
1461 sys_getvfsstat(struct getvfsstat_args *uap)
1463 struct thread *td = curthread;
1464 struct getvfsstat_info info;
1466 bzero(&info, sizeof(info));
1468 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1469 info.sfsp = uap->buf;
1470 info.vsfsp = uap->vbuf;
1472 info.flags = uap->flags;
1475 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1476 if (info.vsfsp && info.count > info.maxcount)
1477 uap->sysmsg_result = info.maxcount;
1479 uap->sysmsg_result = info.count;
1480 return (info.error);
1484 getvfsstat_callback(struct mount *mp, void *data)
1486 struct getvfsstat_info *info = data;
1488 struct statvfs *vsp;
1493 if (info->vsfsp && info->count < info->maxcount) {
1494 if (info->td->td_proc &&
1495 !chroot_visible_mnt(mp, info->td->td_proc)) {
1499 vsp = &mp->mnt_vstat;
1502 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1503 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1504 * overrides MNT_WAIT.
1506 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1507 (info->flags & MNT_WAIT)) &&
1508 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1511 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1513 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1514 (info->flags & MNT_WAIT)) &&
1515 (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) {
1519 if (mp->mnt_flag & MNT_RDONLY)
1520 vsp->f_flag |= ST_RDONLY;
1521 if (mp->mnt_flag & MNT_NOSUID)
1522 vsp->f_flag |= ST_NOSUID;
1524 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1526 info->error = error;
1529 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1530 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1531 kfree(freepath, M_TEMP);
1533 error = copyout(sp, info->sfsp, sizeof(*sp));
1535 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1537 info->error = error;
1549 * fchdir_args(int fd)
1551 * Change current working directory to a given file descriptor.
1554 sys_fchdir(struct fchdir_args *uap)
1556 struct thread *td = curthread;
1557 struct proc *p = td->td_proc;
1558 struct filedesc *fdp = p->p_fd;
1559 struct vnode *vp, *ovp;
1562 struct nchandle nch, onch, tnch;
1565 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1567 lwkt_gettoken(&p->p_token);
1568 vp = (struct vnode *)fp->f_data;
1570 vn_lock(vp, LK_SHARED | LK_RETRY);
1571 if (fp->f_nchandle.ncp == NULL)
1574 error = checkvp_chdir(vp, td);
1579 cache_copy(&fp->f_nchandle, &nch);
1582 * If the ncp has become a mount point, traverse through
1586 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1587 (mp = cache_findmount(&nch)) != NULL
1589 error = nlookup_mp(mp, &tnch);
1591 cache_unlock(&tnch); /* leave ref intact */
1593 vp = tnch.ncp->nc_vp;
1594 error = vget(vp, LK_SHARED);
1595 KKASSERT(error == 0);
1599 cache_dropmount(mp);
1603 onch = fdp->fd_ncdir;
1604 vn_unlock(vp); /* leave ref intact */
1606 fdp->fd_ncdir = nch;
1615 lwkt_reltoken(&p->p_token);
1620 kern_chdir(struct nlookupdata *nd)
1622 struct thread *td = curthread;
1623 struct proc *p = td->td_proc;
1624 struct filedesc *fdp = p->p_fd;
1625 struct vnode *vp, *ovp;
1626 struct nchandle onch;
1629 nd->nl_flags |= NLC_SHAREDLOCK;
1630 if ((error = nlookup(nd)) != 0)
1632 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1634 if ((error = vget(vp, LK_SHARED)) != 0)
1637 lwkt_gettoken(&p->p_token);
1638 error = checkvp_chdir(vp, td);
1642 onch = fdp->fd_ncdir;
1643 cache_unlock(&nd->nl_nch); /* leave reference intact */
1644 fdp->fd_ncdir = nd->nl_nch;
1648 cache_zero(&nd->nl_nch);
1652 lwkt_reltoken(&p->p_token);
1657 * chdir_args(char *path)
1659 * Change current working directory (``.'').
1662 sys_chdir(struct chdir_args *uap)
1664 struct nlookupdata nd;
1667 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1669 error = kern_chdir(&nd);
1675 * Helper function for raised chroot(2) security function: Refuse if
1676 * any filedescriptors are open directories.
1679 chroot_refuse_vdir_fds(struct filedesc *fdp)
1686 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1687 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1689 vp = (struct vnode *)fp->f_data;
1690 if (vp->v_type != VDIR) {
1701 * This sysctl determines if we will allow a process to chroot(2) if it
1702 * has a directory open:
1703 * 0: disallowed for all processes.
1704 * 1: allowed for processes that were not already chroot(2)'ed.
1705 * 2: allowed for all processes.
1708 static int chroot_allow_open_directories = 1;
1710 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1711 &chroot_allow_open_directories, 0, "");
1714 * chroot to the specified namecache entry. We obtain the vp from the
1715 * namecache data. The passed ncp must be locked and referenced and will
1716 * remain locked and referenced on return.
1719 kern_chroot(struct nchandle *nch)
1721 struct thread *td = curthread;
1722 struct proc *p = td->td_proc;
1723 struct filedesc *fdp = p->p_fd;
1728 * Only privileged user can chroot
1730 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1735 * Disallow open directory descriptors (fchdir() breakouts).
1737 if (chroot_allow_open_directories == 0 ||
1738 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1739 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1742 if ((vp = nch->ncp->nc_vp) == NULL)
1745 if ((error = vget(vp, LK_SHARED)) != 0)
1749 * Check the validity of vp as a directory to change to and
1750 * associate it with rdir/jdir.
1752 error = checkvp_chdir(vp, td);
1753 vn_unlock(vp); /* leave reference intact */
1755 vrele(fdp->fd_rdir);
1756 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1757 cache_drop(&fdp->fd_nrdir);
1758 cache_copy(nch, &fdp->fd_nrdir);
1759 if (fdp->fd_jdir == NULL) {
1762 cache_copy(nch, &fdp->fd_njdir);
1771 * chroot_args(char *path)
1773 * Change notion of root (``/'') directory.
1776 sys_chroot(struct chroot_args *uap)
1778 struct thread *td __debugvar = curthread;
1779 struct nlookupdata nd;
1782 KKASSERT(td->td_proc);
1783 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1785 nd.nl_flags |= NLC_EXEC;
1786 error = nlookup(&nd);
1788 error = kern_chroot(&nd.nl_nch);
1795 sys_chroot_kernel(struct chroot_kernel_args *uap)
1797 struct thread *td = curthread;
1798 struct nlookupdata nd;
1799 struct nchandle *nch;
1803 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1807 error = nlookup(&nd);
1813 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1817 if ((vp = nch->ncp->nc_vp) == NULL) {
1822 if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0)
1825 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap->path);
1827 vfs_cache_setroot(vp, cache_hold(nch));
1837 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1838 * determine whether it is legal to chdir to the vnode. The vnode's state
1839 * is not changed by this call.
1842 checkvp_chdir(struct vnode *vp, struct thread *td)
1846 if (vp->v_type != VDIR)
1849 error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
1854 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1856 struct thread *td = curthread;
1857 struct proc *p = td->td_proc;
1858 struct lwp *lp = td->td_lwp;
1859 struct filedesc *fdp = p->p_fd;
1864 int type, indx, error = 0;
1867 if ((oflags & O_ACCMODE) == O_ACCMODE)
1869 flags = FFLAGS(oflags);
1870 error = falloc(lp, &nfp, NULL);
1874 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1877 * XXX p_dupfd is a real mess. It allows a device to return a
1878 * file descriptor to be duplicated rather then doing the open
1884 * Call vn_open() to do the lookup and assign the vnode to the
1885 * file pointer. vn_open() does not change the ref count on fp
1886 * and the vnode, on success, will be inherited by the file pointer
1889 * Request a shared lock on the vnode if possible.
1891 nd->nl_flags |= NLC_LOCKVP;
1892 if ((flags & (O_CREAT|O_TRUNC)) == 0)
1893 nd->nl_flags |= NLC_SHAREDLOCK;
1895 error = vn_open(nd, fp, flags, cmode);
1900 * handle special fdopen() case. bleh. dupfdopen() is
1901 * responsible for dropping the old contents of ofiles[indx]
1904 * Note that fsetfd() will add a ref to fp which represents
1905 * the fd_files[] assignment. We must still drop our
1908 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1909 if (fdalloc(p, 0, &indx) == 0) {
1910 error = dupfdopen(fdp, indx, lp->lwp_dupfd, flags, error);
1913 fdrop(fp); /* our ref */
1916 fsetfd(fdp, NULL, indx);
1919 fdrop(fp); /* our ref */
1920 if (error == ERESTART)
1926 * ref the vnode for ourselves so it can't be ripped out from under
1927 * is. XXX need an ND flag to request that the vnode be returned
1930 * Reserve a file descriptor but do not assign it until the open
1933 vp = (struct vnode *)fp->f_data;
1935 if ((error = fdalloc(p, 0, &indx)) != 0) {
1942 * If no error occurs the vp will have been assigned to the file
1947 if (flags & (O_EXLOCK | O_SHLOCK)) {
1948 lf.l_whence = SEEK_SET;
1951 if (flags & O_EXLOCK)
1952 lf.l_type = F_WRLCK;
1954 lf.l_type = F_RDLCK;
1955 if (flags & FNONBLOCK)
1960 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1962 * lock request failed. Clean up the reserved
1966 fsetfd(fdp, NULL, indx);
1970 atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */
1974 * Assert that all regular file vnodes were created with a object.
1976 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1977 ("open: regular file has no backing object after vn_open"));
1983 * release our private reference, leaving the one associated with the
1984 * descriptor table intact.
1986 if (oflags & O_CLOEXEC)
1987 fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
1988 fsetfd(fdp, fp, indx);
1995 * open_args(char *path, int flags, int mode)
1997 * Check permissions, allocate an open file structure,
1998 * and call the device open routine if any.
2001 sys_open(struct open_args *uap)
2003 struct nlookupdata nd;
2006 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2008 error = kern_open(&nd, uap->flags,
2009 uap->mode, &uap->sysmsg_result);
2016 * openat_args(int fd, char *path, int flags, int mode)
2019 sys_openat(struct openat_args *uap)
2021 struct nlookupdata nd;
2025 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2027 error = kern_open(&nd, uap->flags, uap->mode,
2028 &uap->sysmsg_result);
2030 nlookup_done_at(&nd, fp);
2035 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
2037 struct thread *td = curthread;
2038 struct proc *p = td->td_proc;
2047 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2048 vattr.va_rmajor = rmajor;
2049 vattr.va_rminor = rminor;
2051 switch (mode & S_IFMT) {
2052 case S_IFMT: /* used by badsect to flag bad sectors */
2053 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
2054 vattr.va_type = VBAD;
2057 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2058 vattr.va_type = VCHR;
2061 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2062 vattr.va_type = VBLK;
2065 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2068 case S_IFDIR: /* special directories support for HAMMER */
2069 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2070 vattr.va_type = VDIR;
2081 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2082 if ((error = nlookup(nd)) != 0)
2084 if (nd->nl_nch.ncp->nc_vp)
2086 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2090 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2091 nd->nl_cred, NAMEI_CREATE);
2094 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2095 &vp, nd->nl_cred, &vattr);
2103 * mknod_args(char *path, int mode, int dev)
2105 * Create a special file.
2108 sys_mknod(struct mknod_args *uap)
2110 struct nlookupdata nd;
2113 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2115 error = kern_mknod(&nd, uap->mode,
2116 umajor(uap->dev), uminor(uap->dev));
2123 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2125 * Create a special file. The path is relative to the directory associated
2129 sys_mknodat(struct mknodat_args *uap)
2131 struct nlookupdata nd;
2135 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2137 error = kern_mknod(&nd, uap->mode,
2138 umajor(uap->dev), uminor(uap->dev));
2140 nlookup_done_at(&nd, fp);
2145 kern_mkfifo(struct nlookupdata *nd, int mode)
2147 struct thread *td = curthread;
2148 struct proc *p = td->td_proc;
2155 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2156 if ((error = nlookup(nd)) != 0)
2158 if (nd->nl_nch.ncp->nc_vp)
2160 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2164 vattr.va_type = VFIFO;
2165 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2167 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2174 * mkfifo_args(char *path, int mode)
2176 * Create a named pipe.
2179 sys_mkfifo(struct mkfifo_args *uap)
2181 struct nlookupdata nd;
2184 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2186 error = kern_mkfifo(&nd, uap->mode);
2192 * mkfifoat_args(int fd, char *path, mode_t mode)
2194 * Create a named pipe. The path is relative to the directory associated
2198 sys_mkfifoat(struct mkfifoat_args *uap)
2200 struct nlookupdata nd;
2204 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2206 error = kern_mkfifo(&nd, uap->mode);
2207 nlookup_done_at(&nd, fp);
2211 static int hardlink_check_uid = 0;
2212 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2213 &hardlink_check_uid, 0,
2214 "Unprivileged processes cannot create hard links to files owned by other "
2216 static int hardlink_check_gid = 0;
2217 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2218 &hardlink_check_gid, 0,
2219 "Unprivileged processes cannot create hard links to files owned by other "
2223 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2229 * Shortcut if disabled
2231 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2235 * Privileged user can always hardlink
2237 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2241 * Otherwise only if the originating file is owned by the
2242 * same user or group. Note that any group is allowed if
2243 * the file is owned by the caller.
2245 error = VOP_GETATTR(vp, &va);
2249 if (hardlink_check_uid) {
2250 if (cred->cr_uid != va.va_uid)
2254 if (hardlink_check_gid) {
2255 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2263 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2265 struct thread *td = curthread;
2270 * Lookup the source and obtained a locked vnode.
2272 * You may only hardlink a file which you have write permission
2273 * on or which you own.
2275 * XXX relookup on vget failure / race ?
2278 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2279 if ((error = nlookup(nd)) != 0)
2281 vp = nd->nl_nch.ncp->nc_vp;
2282 KKASSERT(vp != NULL);
2283 if (vp->v_type == VDIR)
2284 return (EPERM); /* POSIX */
2285 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2287 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2291 * Unlock the source so we can lookup the target without deadlocking
2292 * (XXX vp is locked already, possible other deadlock?). The target
2295 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2296 nd->nl_flags &= ~NLC_NCPISLOCKED;
2297 cache_unlock(&nd->nl_nch);
2300 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2301 if ((error = nlookup(linknd)) != 0) {
2305 if (linknd->nl_nch.ncp->nc_vp) {
2309 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
2316 * Finally run the new API VOP.
2318 error = can_hardlink(vp, td, td->td_ucred);
2320 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2321 vp, linknd->nl_cred);
2328 * link_args(char *path, char *link)
2330 * Make a hard file link.
2333 sys_link(struct link_args *uap)
2335 struct nlookupdata nd, linknd;
2338 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2340 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2342 error = kern_link(&nd, &linknd);
2343 nlookup_done(&linknd);
2350 * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags)
2352 * Make a hard file link. The path1 argument is relative to the directory
2353 * associated with fd1, and similarly the path2 argument is relative to
2354 * the directory associated with fd2.
2357 sys_linkat(struct linkat_args *uap)
2359 struct nlookupdata nd, linknd;
2360 struct file *fp1, *fp2;
2363 error = nlookup_init_at(&nd, &fp1, uap->fd1, uap->path1, UIO_USERSPACE,
2364 (uap->flags & AT_SYMLINK_FOLLOW) ? NLC_FOLLOW : 0);
2366 error = nlookup_init_at(&linknd, &fp2, uap->fd2,
2367 uap->path2, UIO_USERSPACE, 0);
2369 error = kern_link(&nd, &linknd);
2370 nlookup_done_at(&linknd, fp2);
2372 nlookup_done_at(&nd, fp1);
2377 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2385 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2386 if ((error = nlookup(nd)) != 0)
2388 if (nd->nl_nch.ncp->nc_vp)
2390 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2394 vattr.va_mode = mode;
2395 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2402 * symlink(char *path, char *link)
2404 * Make a symbolic link.
2407 sys_symlink(struct symlink_args *uap)
2409 struct thread *td = curthread;
2410 struct nlookupdata nd;
2415 path = objcache_get(namei_oc, M_WAITOK);
2416 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2418 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2420 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2421 error = kern_symlink(&nd, path, mode);
2425 objcache_put(namei_oc, path);
2430 * symlinkat_args(char *path1, int fd, char *path2)
2432 * Make a symbolic link. The path2 argument is relative to the directory
2433 * associated with fd.
2436 sys_symlinkat(struct symlinkat_args *uap)
2438 struct thread *td = curthread;
2439 struct nlookupdata nd;
2445 path1 = objcache_get(namei_oc, M_WAITOK);
2446 error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2448 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2451 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2452 error = kern_symlink(&nd, path1, mode);
2454 nlookup_done_at(&nd, fp);
2456 objcache_put(namei_oc, path1);
2461 * undelete_args(char *path)
2463 * Delete a whiteout from the filesystem.
2466 sys_undelete(struct undelete_args *uap)
2468 struct nlookupdata nd;
2471 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2473 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2475 error = nlookup(&nd);
2477 error = ncp_writechk(&nd.nl_nch);
2479 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2487 kern_unlink(struct nlookupdata *nd)
2492 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2493 if ((error = nlookup(nd)) != 0)
2495 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2497 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2502 * unlink_args(char *path)
2504 * Delete a name from the filesystem.
2507 sys_unlink(struct unlink_args *uap)
2509 struct nlookupdata nd;
2512 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2514 error = kern_unlink(&nd);
2521 * unlinkat_args(int fd, char *path, int flags)
2523 * Delete the file or directory entry pointed to by fd/path.
2526 sys_unlinkat(struct unlinkat_args *uap)
2528 struct nlookupdata nd;
2532 if (uap->flags & ~AT_REMOVEDIR)
2535 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2537 if (uap->flags & AT_REMOVEDIR)
2538 error = kern_rmdir(&nd);
2540 error = kern_unlink(&nd);
2542 nlookup_done_at(&nd, fp);
2547 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2549 struct thread *td = curthread;
2550 struct proc *p = td->td_proc;
2557 fp = holdfp(p->p_fd, fd, -1);
2560 if (fp->f_type != DTYPE_VNODE) {
2564 vp = (struct vnode *)fp->f_data;
2568 spin_lock(&fp->f_spin);
2569 new_offset = fp->f_offset + offset;
2573 error = VOP_GETATTR(vp, &vattr);
2574 spin_lock(&fp->f_spin);
2575 new_offset = offset + vattr.va_size;
2578 new_offset = offset;
2580 spin_lock(&fp->f_spin);
2585 spin_lock(&fp->f_spin);
2590 * Validate the seek position. Negative offsets are not allowed
2591 * for regular files or directories.
2593 * Normally we would also not want to allow negative offsets for
2594 * character and block-special devices. However kvm addresses
2595 * on 64 bit architectures might appear to be negative and must
2599 if (new_offset < 0 &&
2600 (vp->v_type == VREG || vp->v_type == VDIR)) {
2603 fp->f_offset = new_offset;
2606 *res = fp->f_offset;
2607 spin_unlock(&fp->f_spin);
2614 * lseek_args(int fd, int pad, off_t offset, int whence)
2616 * Reposition read/write file offset.
2619 sys_lseek(struct lseek_args *uap)
2623 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2624 &uap->sysmsg_offset);
2630 * Check if current process can access given file. amode is a bitmask of *_OK
2631 * access bits. flags is a bitmask of AT_* flags.
2634 kern_access(struct nlookupdata *nd, int amode, int flags)
2639 if (flags & ~AT_EACCESS)
2641 nd->nl_flags |= NLC_SHAREDLOCK;
2642 if ((error = nlookup(nd)) != 0)
2645 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
2649 /* Flags == 0 means only check for existence. */
2658 if ((mode & VWRITE) == 0 ||
2659 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2660 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2663 * If the file handle is stale we have to re-resolve the
2664 * entry with the ncp held exclusively. This is a hack
2667 if (error == ESTALE) {
2669 cache_unlock(&nd->nl_nch);
2670 cache_lock(&nd->nl_nch);
2671 cache_setunresolved(&nd->nl_nch);
2672 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2685 * access_args(char *path, int flags)
2687 * Check access permissions.
2690 sys_access(struct access_args *uap)
2692 struct nlookupdata nd;
2695 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2697 error = kern_access(&nd, uap->flags, 0);
2704 * eaccess_args(char *path, int flags)
2706 * Check access permissions.
2709 sys_eaccess(struct eaccess_args *uap)
2711 struct nlookupdata nd;
2714 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2716 error = kern_access(&nd, uap->flags, AT_EACCESS);
2723 * faccessat_args(int fd, char *path, int amode, int flags)
2725 * Check access permissions.
2728 sys_faccessat(struct faccessat_args *uap)
2730 struct nlookupdata nd;
2734 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2737 error = kern_access(&nd, uap->amode, uap->flags);
2738 nlookup_done_at(&nd, fp);
2743 kern_stat(struct nlookupdata *nd, struct stat *st)
2748 nd->nl_flags |= NLC_SHAREDLOCK;
2749 if ((error = nlookup(nd)) != 0)
2752 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2755 if ((error = vget(vp, LK_SHARED)) != 0)
2757 error = vn_stat(vp, st, nd->nl_cred);
2760 * If the file handle is stale we have to re-resolve the
2761 * entry with the ncp held exclusively. This is a hack
2764 if (error == ESTALE) {
2766 cache_unlock(&nd->nl_nch);
2767 cache_lock(&nd->nl_nch);
2768 cache_setunresolved(&nd->nl_nch);
2769 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2779 * stat_args(char *path, struct stat *ub)
2781 * Get file status; this version follows links.
2784 sys_stat(struct stat_args *uap)
2786 struct nlookupdata nd;
2790 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2792 error = kern_stat(&nd, &st);
2794 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2801 * lstat_args(char *path, struct stat *ub)
2803 * Get file status; this version does not follow links.
2806 sys_lstat(struct lstat_args *uap)
2808 struct nlookupdata nd;
2812 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2814 error = kern_stat(&nd, &st);
2816 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2823 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2825 * Get status of file pointed to by fd/path.
2828 sys_fstatat(struct fstatat_args *uap)
2830 struct nlookupdata nd;
2836 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2839 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2841 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2842 UIO_USERSPACE, flags);
2844 error = kern_stat(&nd, &st);
2846 error = copyout(&st, uap->sb, sizeof(*uap->sb));
2848 nlookup_done_at(&nd, fp);
2853 kern_pathconf(char *path, int name, int flags, register_t *sysmsg_regp)
2855 struct nlookupdata nd;
2860 error = nlookup_init(&nd, path, UIO_USERSPACE, flags);
2862 error = nlookup(&nd);
2864 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2867 error = VOP_PATHCONF(vp, name, sysmsg_regp);
2874 * pathconf_Args(char *path, int name)
2876 * Get configurable pathname variables.
2879 sys_pathconf(struct pathconf_args *uap)
2881 return (kern_pathconf(uap->path, uap->name, NLC_FOLLOW,
2886 * lpathconf_Args(char *path, int name)
2888 * Get configurable pathname variables, but don't follow symlinks.
2891 sys_lpathconf(struct lpathconf_args *uap)
2893 return (kern_pathconf(uap->path, uap->name, 0, &uap->sysmsg_reg));
2898 * kern_readlink isn't properly split yet. There is a copyin burried
2899 * in VOP_READLINK().
2902 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2904 struct thread *td = curthread;
2910 nd->nl_flags |= NLC_SHAREDLOCK;
2911 if ((error = nlookup(nd)) != 0)
2913 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
2916 if (vp->v_type != VLNK) {
2919 aiov.iov_base = buf;
2920 aiov.iov_len = count;
2921 auio.uio_iov = &aiov;
2922 auio.uio_iovcnt = 1;
2923 auio.uio_offset = 0;
2924 auio.uio_rw = UIO_READ;
2925 auio.uio_segflg = UIO_USERSPACE;
2927 auio.uio_resid = count;
2928 error = VOP_READLINK(vp, &auio, td->td_ucred);
2931 *res = count - auio.uio_resid;
2936 * readlink_args(char *path, char *buf, int count)
2938 * Return target name of a symbolic link.
2941 sys_readlink(struct readlink_args *uap)
2943 struct nlookupdata nd;
2946 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2948 error = kern_readlink(&nd, uap->buf, uap->count,
2949 &uap->sysmsg_result);
2956 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
2958 * Return target name of a symbolic link. The path is relative to the
2959 * directory associated with fd.
2962 sys_readlinkat(struct readlinkat_args *uap)
2964 struct nlookupdata nd;
2968 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2970 error = kern_readlink(&nd, uap->buf, uap->bufsize,
2971 &uap->sysmsg_result);
2973 nlookup_done_at(&nd, fp);
2978 setfflags(struct vnode *vp, int flags)
2980 struct thread *td = curthread;
2985 * Prevent non-root users from setting flags on devices. When
2986 * a device is reused, users can retain ownership of the device
2987 * if they are allowed to set flags and programs assume that
2988 * chown can't fail when done as root.
2990 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2991 ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
2995 * note: vget is required for any operation that might mod the vnode
2996 * so VINACTIVE is properly cleared.
2998 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3000 vattr.va_flags = flags;
3001 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3008 * chflags(char *path, int flags)
3010 * Change flags of a file given a path name.
3013 sys_chflags(struct chflags_args *uap)
3015 struct nlookupdata nd;
3020 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3022 error = nlookup(&nd);
3024 error = ncp_writechk(&nd.nl_nch);
3026 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3029 error = setfflags(vp, uap->flags);
3036 * lchflags(char *path, int flags)
3038 * Change flags of a file given a path name, but don't follow symlinks.
3041 sys_lchflags(struct lchflags_args *uap)
3043 struct nlookupdata nd;
3048 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3050 error = nlookup(&nd);
3052 error = ncp_writechk(&nd.nl_nch);
3054 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3057 error = setfflags(vp, uap->flags);
3064 * fchflags_args(int fd, int flags)
3066 * Change flags of a file given a file descriptor.
3069 sys_fchflags(struct fchflags_args *uap)
3071 struct thread *td = curthread;
3072 struct proc *p = td->td_proc;
3076 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3078 if (fp->f_nchandle.ncp)
3079 error = ncp_writechk(&fp->f_nchandle);
3081 error = setfflags((struct vnode *) fp->f_data, uap->flags);
3087 * chflagsat_args(int fd, const char *path, int flags, int atflags)
3088 * change flags given a pathname relative to a filedescriptor
3090 int sys_chflagsat(struct chflagsat_args *uap)
3092 struct nlookupdata nd;
3098 if (uap->atflags & ~AT_SYMLINK_NOFOLLOW)
3101 lookupflags = (uap->atflags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3104 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, lookupflags);
3106 error = nlookup(&nd);
3108 error = ncp_writechk(&nd.nl_nch);
3110 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3111 nlookup_done_at(&nd, fp);
3113 error = setfflags(vp, uap->flags);
3121 setfmode(struct vnode *vp, int mode)
3123 struct thread *td = curthread;
3128 * note: vget is required for any operation that might mod the vnode
3129 * so VINACTIVE is properly cleared.
3131 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3133 vattr.va_mode = mode & ALLPERMS;
3134 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3141 kern_chmod(struct nlookupdata *nd, int mode)
3146 if ((error = nlookup(nd)) != 0)
3148 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3150 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3151 error = setfmode(vp, mode);
3157 * chmod_args(char *path, int mode)
3159 * Change mode of a file given path name.
3162 sys_chmod(struct chmod_args *uap)
3164 struct nlookupdata nd;
3167 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3169 error = kern_chmod(&nd, uap->mode);
3175 * lchmod_args(char *path, int mode)
3177 * Change mode of a file given path name (don't follow links.)
3180 sys_lchmod(struct lchmod_args *uap)
3182 struct nlookupdata nd;
3185 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3187 error = kern_chmod(&nd, uap->mode);
3193 * fchmod_args(int fd, int mode)
3195 * Change mode of a file given a file descriptor.
3198 sys_fchmod(struct fchmod_args *uap)
3200 struct thread *td = curthread;
3201 struct proc *p = td->td_proc;
3205 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3207 if (fp->f_nchandle.ncp)
3208 error = ncp_writechk(&fp->f_nchandle);
3210 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3216 * fchmodat_args(char *path, int mode)
3218 * Change mode of a file pointed to by fd/path.
3221 sys_fchmodat(struct fchmodat_args *uap)
3223 struct nlookupdata nd;
3228 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3230 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3232 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3233 UIO_USERSPACE, flags);
3235 error = kern_chmod(&nd, uap->mode);
3236 nlookup_done_at(&nd, fp);
3241 setfown(struct mount *mp, struct vnode *vp, uid_t uid, gid_t gid)
3243 struct thread *td = curthread;
3251 * note: vget is required for any operation that might mod the vnode
3252 * so VINACTIVE is properly cleared.
3254 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3255 if ((error = VOP_GETATTR(vp, &vattr)) != 0)
3257 o_uid = vattr.va_uid;
3258 o_gid = vattr.va_gid;
3259 size = vattr.va_size;
3264 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3273 VFS_ACCOUNT(mp, o_uid, o_gid, -size);
3274 VFS_ACCOUNT(mp, uid, gid, size);
3281 kern_chown(struct nlookupdata *nd, int uid, int gid)
3286 if ((error = nlookup(nd)) != 0)
3288 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3290 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3291 error = setfown(nd->nl_nch.mount, vp, uid, gid);
3297 * chown(char *path, int uid, int gid)
3299 * Set ownership given a path name.
3302 sys_chown(struct chown_args *uap)
3304 struct nlookupdata nd;
3307 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3309 error = kern_chown(&nd, uap->uid, uap->gid);
3315 * lchown_args(char *path, int uid, int gid)
3317 * Set ownership given a path name, do not cross symlinks.
3320 sys_lchown(struct lchown_args *uap)
3322 struct nlookupdata nd;
3325 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3327 error = kern_chown(&nd, uap->uid, uap->gid);
3333 * fchown_args(int fd, int uid, int gid)
3335 * Set ownership given a file descriptor.
3338 sys_fchown(struct fchown_args *uap)
3340 struct thread *td = curthread;
3341 struct proc *p = td->td_proc;
3345 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3347 if (fp->f_nchandle.ncp)
3348 error = ncp_writechk(&fp->f_nchandle);
3350 error = setfown(p->p_fd->fd_ncdir.mount,
3351 (struct vnode *)fp->f_data, uap->uid, uap->gid);
3357 * fchownat(int fd, char *path, int uid, int gid, int flags)
3359 * Set ownership of file pointed to by fd/path.
3362 sys_fchownat(struct fchownat_args *uap)
3364 struct nlookupdata nd;
3369 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3371 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3373 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3374 UIO_USERSPACE, flags);
3376 error = kern_chown(&nd, uap->uid, uap->gid);
3377 nlookup_done_at(&nd, fp);
3383 getutimes(struct timeval *tvp, struct timespec *tsp)
3385 struct timeval tv[2];
3390 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3393 if ((error = itimerfix(tvp)) != 0)
3395 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3396 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3402 getutimens(const struct timespec *ts, struct timespec *newts, int *nullflag)
3404 struct timespec tsnow;
3418 if (newts[0].tv_nsec == UTIME_OMIT && newts[1].tv_nsec == UTIME_OMIT)
3420 if (newts[0].tv_nsec == UTIME_NOW && newts[1].tv_nsec == UTIME_NOW)
3423 if (newts[0].tv_nsec == UTIME_OMIT)
3424 newts[0].tv_sec = VNOVAL;
3425 else if (newts[0].tv_nsec == UTIME_NOW)
3427 else if ((error = itimespecfix(&newts[0])) != 0)
3430 if (newts[1].tv_nsec == UTIME_OMIT)
3431 newts[1].tv_sec = VNOVAL;
3432 else if (newts[1].tv_nsec == UTIME_NOW)
3434 else if ((error = itimespecfix(&newts[1])) != 0)
3441 setutimes(struct vnode *vp, struct vattr *vattr,
3442 const struct timespec *ts, int nullflag)
3444 struct thread *td = curthread;
3448 vattr->va_atime = ts[0];
3449 vattr->va_mtime = ts[1];
3451 vattr->va_vaflags |= VA_UTIMES_NULL;
3452 error = VOP_SETATTR(vp, vattr, td->td_ucred);
3458 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3460 struct timespec ts[2];
3464 if ((error = getutimes(tptr, ts)) != 0)
3467 error = kern_utimensat(nd, tptr ? ts : NULL, 0);
3472 * utimes_args(char *path, struct timeval *tptr)
3474 * Set the access and modification times of a file.
3477 sys_utimes(struct utimes_args *uap)
3479 struct timeval tv[2];
3480 struct nlookupdata nd;
3484 error = copyin(uap->tptr, tv, sizeof(tv));
3488 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3490 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3496 * lutimes_args(char *path, struct timeval *tptr)
3498 * Set the access and modification times of a file.
3501 sys_lutimes(struct lutimes_args *uap)
3503 struct timeval tv[2];
3504 struct nlookupdata nd;
3508 error = copyin(uap->tptr, tv, sizeof(tv));
3512 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3514 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3520 * Set utimes on a file descriptor. The creds used to open the
3521 * file are used to determine whether the operation is allowed
3525 kern_futimens(int fd, struct timespec *ts)
3527 struct thread *td = curthread;
3528 struct proc *p = td->td_proc;
3529 struct timespec newts[2];
3536 error = getutimens(ts, newts, &nullflag);
3539 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3541 if (fp->f_nchandle.ncp)
3542 error = ncp_writechk(&fp->f_nchandle);
3545 error = vget(vp, LK_EXCLUSIVE);
3547 error = VOP_GETATTR(vp, &vattr);
3549 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3553 error = setutimes(vp, &vattr, newts, nullflag);
3563 * futimens_args(int fd, struct timespec *ts)
3565 * Set the access and modification times of a file.
3568 sys_futimens(struct futimens_args *uap)
3570 struct timespec ts[2];
3574 error = copyin(uap->ts, ts, sizeof(ts));
3578 error = kern_futimens(uap->fd, uap->ts ? ts : NULL);
3583 kern_futimes(int fd, struct timeval *tptr)
3585 struct timespec ts[2];
3589 if ((error = getutimes(tptr, ts)) != 0)
3592 error = kern_futimens(fd, tptr ? ts : NULL);
3597 * futimes_args(int fd, struct timeval *tptr)
3599 * Set the access and modification times of a file.
3602 sys_futimes(struct futimes_args *uap)
3604 struct timeval tv[2];
3608 error = copyin(uap->tptr, tv, sizeof(tv));
3612 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3617 kern_utimensat(struct nlookupdata *nd, const struct timespec *ts, int flags)
3619 struct timespec newts[2];
3625 if (flags & ~AT_SYMLINK_NOFOLLOW)
3628 error = getutimens(ts, newts, &nullflag);
3632 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3633 if ((error = nlookup(nd)) != 0)
3635 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3637 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3639 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3640 error = vget(vp, LK_EXCLUSIVE);
3642 error = setutimes(vp, &vattr, newts, nullflag);
3651 * utimensat_args(int fd, const char *path, const struct timespec *ts, int flags);
3653 * Set file access and modification times of a file.
3656 sys_utimensat(struct utimensat_args *uap)
3658 struct timespec ts[2];
3659 struct nlookupdata nd;
3665 error = copyin(uap->ts, ts, sizeof(ts));
3670 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3671 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3672 UIO_USERSPACE, flags);
3674 error = kern_utimensat(&nd, uap->ts ? ts : NULL, uap->flags);
3675 nlookup_done_at(&nd, fp);
3680 kern_truncate(struct nlookupdata *nd, off_t length)
3687 uint64_t old_size = 0;
3691 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3692 if ((error = nlookup(nd)) != 0)
3694 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3696 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3698 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
3703 if (vp->v_type == VDIR) {
3707 if (vfs_quota_enabled) {
3708 error = VOP_GETATTR(vp, &vattr);
3709 KASSERT(error == 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3712 old_size = vattr.va_size;
3715 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3717 vattr.va_size = length;
3718 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3719 VFS_ACCOUNT(nd->nl_nch.mount, uid, gid, length - old_size);
3727 * truncate(char *path, int pad, off_t length)
3729 * Truncate a file given its path name.
3732 sys_truncate(struct truncate_args *uap)
3734 struct nlookupdata nd;
3737 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3739 error = kern_truncate(&nd, uap->length);
3745 kern_ftruncate(int fd, off_t length)
3747 struct thread *td = curthread;
3748 struct proc *p = td->td_proc;
3755 uint64_t old_size = 0;
3760 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3762 if (fp->f_nchandle.ncp) {
3763 error = ncp_writechk(&fp->f_nchandle);
3767 if ((fp->f_flag & FWRITE) == 0) {
3771 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3775 vp = (struct vnode *)fp->f_data;
3776 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3777 if (vp->v_type == VDIR) {
3783 if (vfs_quota_enabled) {
3784 error = VOP_GETATTR(vp, &vattr);
3785 KASSERT(error == 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
3788 old_size = vattr.va_size;
3791 if ((error = vn_writechk(vp, NULL)) == 0) {
3793 vattr.va_size = length;
3794 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3796 VFS_ACCOUNT(mp, uid, gid, length - old_size);
3805 * ftruncate_args(int fd, int pad, off_t length)
3807 * Truncate a file given a file descriptor.
3810 sys_ftruncate(struct ftruncate_args *uap)
3814 error = kern_ftruncate(uap->fd, uap->length);
3822 * Sync an open file.
3825 sys_fsync(struct fsync_args *uap)
3827 struct thread *td = curthread;
3828 struct proc *p = td->td_proc;
3834 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3836 vp = (struct vnode *)fp->f_data;
3837 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3838 if ((obj = vp->v_object) != NULL) {
3839 if (vp->v_mount == NULL ||
3840 (vp->v_mount->mnt_kern_flag & MNTK_NOMSYNC) == 0) {
3841 vm_object_page_clean(obj, 0, 0, 0);
3844 error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
3845 if (error == 0 && vp->v_mount)
3846 error = buf_fsync(vp);
3854 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3856 struct nchandle fnchd;
3857 struct nchandle tnchd;
3858 struct namecache *ncp;
3867 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3868 if ((error = nlookup(fromnd)) != 0)
3870 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3872 fnchd.mount = fromnd->nl_nch.mount;
3876 * unlock the source nch so we can lookup the target nch without
3877 * deadlocking. The target may or may not exist so we do not check
3878 * for a target vp like kern_mkdir() and other creation functions do.
3880 * The source and target directories are ref'd and rechecked after
3881 * everything is relocked to determine if the source or target file
3884 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3885 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3887 fncp_gen = fromnd->nl_nch.ncp->nc_generation;
3889 cache_unlock(&fromnd->nl_nch);
3891 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3892 if ((error = nlookup(tond)) != 0) {
3896 tncp_gen = tond->nl_nch.ncp->nc_generation;
3898 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3902 tnchd.mount = tond->nl_nch.mount;
3906 * If the source and target are the same there is nothing to do
3908 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3915 * Mount points cannot be renamed or overwritten
3917 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3926 * Relock the source ncp. cache_relock() will deal with any
3927 * deadlocks against the already-locked tond and will also
3928 * make sure both are resolved.
3930 * NOTE AFTER RELOCKING: The source or target ncp may have become
3931 * invalid while they were unlocked, nc_vp and nc_mount could
3934 cache_relock(&fromnd->nl_nch, fromnd->nl_cred,
3935 &tond->nl_nch, tond->nl_cred);
3936 fromnd->nl_flags |= NLC_NCPISLOCKED;
3939 * If the namecache generation changed for either fromnd or tond,
3942 if (fromnd->nl_nch.ncp->nc_generation != fncp_gen ||
3943 tond->nl_nch.ncp->nc_generation != tncp_gen) {
3944 kprintf("kern_rename: retry due to gen on: "
3945 "\"%s\" -> \"%s\"\n",
3946 fromnd->nl_nch.ncp->nc_name,
3947 tond->nl_nch.ncp->nc_name);
3954 * If either fromnd or tond are marked destroyed a ripout occured
3955 * out from under us and we must retry.
3957 if ((fromnd->nl_nch.ncp->nc_flag & (NCF_DESTROYED | NCF_UNRESOLVED)) ||
3958 fromnd->nl_nch.ncp->nc_vp == NULL ||
3959 (tond->nl_nch.ncp->nc_flag & NCF_DESTROYED)) {
3960 kprintf("kern_rename: retry due to ripout on: "
3961 "\"%s\" -> \"%s\"\n",
3962 fromnd->nl_nch.ncp->nc_name,
3963 tond->nl_nch.ncp->nc_name);
3970 * Make sure the parent directories linkages are the same.
3971 * XXX shouldn't be needed any more w/ generation check above.
3973 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3974 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3981 * Both the source and target must be within the same filesystem and
3982 * in the same filesystem as their parent directories within the
3983 * namecache topology.
3985 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3988 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3989 mp != tond->nl_nch.mount) {
3996 * Make sure the mount point is writable
3998 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
4005 * If the target exists and either the source or target is a directory,
4006 * then both must be directories.
4008 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
4011 if (tond->nl_nch.ncp->nc_vp) {
4012 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
4014 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
4015 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
4017 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
4023 * You cannot rename a source into itself or a subdirectory of itself.
4024 * We check this by travsersing the target directory upwards looking
4025 * for a match against the source.
4030 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
4031 if (fromnd->nl_nch.ncp == ncp) {
4042 * Even though the namespaces are different, they may still represent
4043 * hardlinks to the same file. The filesystem might have a hard time
4044 * with this so we issue a NREMOVE of the source instead of a NRENAME
4045 * when we detect the situation.
4048 fdvp = fromnd->nl_dvp;
4049 tdvp = tond->nl_dvp;
4050 if (fdvp == NULL || tdvp == NULL) {
4052 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
4053 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
4056 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
4057 fdvp, tdvp, tond->nl_cred);
4064 * rename_args(char *from, char *to)
4066 * Rename files. Source and destination must either both be directories,
4067 * or both not be directories. If target is a directory, it must be empty.
4070 sys_rename(struct rename_args *uap)
4072 struct nlookupdata fromnd, tond;
4076 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
4078 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
4080 error = kern_rename(&fromnd, &tond);
4081 nlookup_done(&tond);
4083 nlookup_done(&fromnd);
4084 } while (error == EAGAIN);
4089 * renameat_args(int oldfd, char *old, int newfd, char *new)
4091 * Rename files using paths relative to the directories associated with
4092 * oldfd and newfd. Source and destination must either both be directories,
4093 * or both not be directories. If target is a directory, it must be empty.
4096 sys_renameat(struct renameat_args *uap)
4098 struct nlookupdata oldnd, newnd;
4099 struct file *oldfp, *newfp;
4103 error = nlookup_init_at(&oldnd, &oldfp,
4104 uap->oldfd, uap->old,
4107 error = nlookup_init_at(&newnd, &newfp,
4108 uap->newfd, uap->new,
4111 error = kern_rename(&oldnd, &newnd);
4112 nlookup_done_at(&newnd, newfp);
4114 nlookup_done_at(&oldnd, oldfp);
4115 } while (error == EAGAIN);
4120 kern_mkdir(struct nlookupdata *nd, int mode)
4122 struct thread *td = curthread;
4123 struct proc *p = td->td_proc;
4129 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
4130 if ((error = nlookup(nd)) != 0)
4133 if (nd->nl_nch.ncp->nc_vp)
4135 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4138 vattr.va_type = VDIR;
4139 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
4142 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
4149 * mkdir_args(char *path, int mode)
4151 * Make a directory file.
4154 sys_mkdir(struct mkdir_args *uap)
4156 struct nlookupdata nd;
4159 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4161 error = kern_mkdir(&nd, uap->mode);
4167 * mkdirat_args(int fd, char *path, mode_t mode)
4169 * Make a directory file. The path is relative to the directory associated
4173 sys_mkdirat(struct mkdirat_args *uap)
4175 struct nlookupdata nd;
4179 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
4181 error = kern_mkdir(&nd, uap->mode);
4182 nlookup_done_at(&nd, fp);
4187 kern_rmdir(struct nlookupdata *nd)
4192 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
4193 if ((error = nlookup(nd)) != 0)
4197 * Do not allow directories representing mount points to be
4198 * deleted, even if empty. Check write perms on mount point
4199 * in case the vnode is aliased (aka nullfs).
4201 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
4203 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4205 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
4210 * rmdir_args(char *path)
4212 * Remove a directory file.
4215 sys_rmdir(struct rmdir_args *uap)
4217 struct nlookupdata nd;
4220 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4222 error = kern_rmdir(&nd);
4228 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
4229 enum uio_seg direction)
4231 struct thread *td = curthread;
4232 struct proc *p = td->td_proc;
4240 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
4242 if ((fp->f_flag & FREAD) == 0) {
4246 vp = (struct vnode *)fp->f_data;
4247 if (vp->v_type != VDIR) {
4251 aiov.iov_base = buf;
4252 aiov.iov_len = count;
4253 auio.uio_iov = &aiov;
4254 auio.uio_iovcnt = 1;
4255 auio.uio_rw = UIO_READ;
4256 auio.uio_segflg = direction;
4258 auio.uio_resid = count;
4259 loff = auio.uio_offset = fp->f_offset;
4260 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
4261 fp->f_offset = auio.uio_offset;
4266 * WARNING! *basep may not be wide enough to accomodate the
4267 * seek offset. XXX should we hack this to return the upper 32 bits
4268 * for offsets greater then 4G?
4271 *basep = (long)loff;
4273 *res = count - auio.uio_resid;
4280 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4282 * Read a block of directory entries in a file system independent format.
4285 sys_getdirentries(struct getdirentries_args *uap)
4290 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
4291 &uap->sysmsg_result, UIO_USERSPACE);
4293 if (error == 0 && uap->basep)
4294 error = copyout(&base, uap->basep, sizeof(*uap->basep));
4299 * getdents_args(int fd, char *buf, size_t count)
4302 sys_getdents(struct getdents_args *uap)
4306 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
4307 &uap->sysmsg_result, UIO_USERSPACE);
4313 * Set the mode mask for creation of filesystem nodes.
4315 * umask(int newmask)
4318 sys_umask(struct umask_args *uap)
4320 struct thread *td = curthread;
4321 struct proc *p = td->td_proc;
4322 struct filedesc *fdp;
4325 uap->sysmsg_result = fdp->fd_cmask;
4326 fdp->fd_cmask = uap->newmask & ALLPERMS;
4331 * revoke(char *path)
4333 * Void all references to file by ripping underlying filesystem
4337 sys_revoke(struct revoke_args *uap)
4339 struct nlookupdata nd;
4346 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4348 error = nlookup(&nd);
4350 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4351 cred = crhold(nd.nl_cred);
4355 error = VOP_GETATTR(vp, &vattr);
4356 if (error == 0 && cred->cr_uid != vattr.va_uid)
4357 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4358 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4360 error = vrevoke(vp, cred);
4361 } else if (error == 0) {
4362 error = vrevoke(vp, cred);
4372 * getfh_args(char *fname, fhandle_t *fhp)
4374 * Get (NFS) file handle
4376 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4377 * mount. This allows nullfs mounts to be explicitly exported.
4379 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4381 * nullfs mounts of subdirectories are not safe. That is, it will
4382 * work, but you do not really have protection against access to
4383 * the related parent directories.
4386 sys_getfh(struct getfh_args *uap)
4388 struct thread *td = curthread;
4389 struct nlookupdata nd;
4396 * Must be super user
4398 if ((error = priv_check(td, PRIV_ROOT)) != 0)
4402 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4404 error = nlookup(&nd);
4406 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4407 mp = nd.nl_nch.mount;
4410 bzero(&fh, sizeof(fh));
4411 fh.fh_fsid = mp->mnt_stat.f_fsid;
4412 error = VFS_VPTOFH(vp, &fh.fh_fid);
4415 error = copyout(&fh, uap->fhp, sizeof(fh));
4421 * fhopen_args(const struct fhandle *u_fhp, int flags)
4423 * syscall for the rpc.lockd to use to translate a NFS file handle into
4424 * an open descriptor.
4426 * warning: do not remove the priv_check() call or this becomes one giant
4430 sys_fhopen(struct fhopen_args *uap)
4432 struct thread *td = curthread;
4433 struct filedesc *fdp = td->td_proc->p_fd;
4438 struct vattr *vap = &vat;
4440 int fmode, mode, error = 0, type;
4446 * Must be super user
4448 error = priv_check(td, PRIV_ROOT);
4452 fmode = FFLAGS(uap->flags);
4455 * Why not allow a non-read/write open for our lockd?
4457 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4459 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4464 * Find the mount point
4466 mp = vfs_getvfs(&fhp.fh_fsid);
4471 /* now give me my vnode, it gets returned to me locked */
4472 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4476 * from now on we have to make sure not
4477 * to forget about the vnode
4478 * any error that causes an abort must vput(vp)
4479 * just set error = err and 'goto bad;'.
4485 if (vp->v_type == VLNK) {
4489 if (vp->v_type == VSOCK) {
4494 if (fmode & (FWRITE | O_TRUNC)) {
4495 if (vp->v_type == VDIR) {
4499 error = vn_writechk(vp, NULL);
4507 error = VOP_ACCESS(vp, mode, td->td_ucred);
4511 if (fmode & O_TRUNC) {
4512 vn_unlock(vp); /* XXX */
4513 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4516 error = VOP_SETATTR(vp, vap, td->td_ucred);
4522 * VOP_OPEN needs the file pointer so it can potentially override
4525 * WARNING! no f_nchandle will be associated when fhopen()ing a
4528 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4532 error = VOP_OPEN(vp, fmode, td->td_ucred, fp);
4535 * setting f_ops this way prevents VOP_CLOSE from being
4536 * called or fdrop() releasing the vp from v_data. Since
4537 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4539 fp->f_ops = &badfileops;
4545 * The fp is given its own reference, we still have our ref and lock.
4547 * Assert that all regular files must be created with a VM object.
4549 if (vp->v_type == VREG && vp->v_object == NULL) {
4550 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
4555 * The open was successful. Handle any locking requirements.
4557 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4558 lf.l_whence = SEEK_SET;
4561 if (fmode & O_EXLOCK)
4562 lf.l_type = F_WRLCK;
4564 lf.l_type = F_RDLCK;
4565 if (fmode & FNONBLOCK)
4570 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
4572 * release our private reference.
4574 fsetfd(fdp, NULL, indx);
4579 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4580 atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */
4584 * Clean up. Associate the file pointer with the previously
4585 * reserved descriptor and return it.
4588 if (uap->flags & O_CLOEXEC)
4589 fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
4590 fsetfd(fdp, fp, indx);
4592 uap->sysmsg_result = indx;
4596 fsetfd(fdp, NULL, indx);
4605 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4608 sys_fhstat(struct fhstat_args *uap)
4610 struct thread *td = curthread;
4618 * Must be super user
4620 error = priv_check(td, PRIV_ROOT);
4624 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4628 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4631 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4632 error = vn_stat(vp, &sb, td->td_ucred);
4637 error = copyout(&sb, uap->sb, sizeof(sb));
4642 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4645 sys_fhstatfs(struct fhstatfs_args *uap)
4647 struct thread *td = curthread;
4648 struct proc *p = td->td_proc;
4653 char *fullpath, *freepath;
4658 * Must be super user
4660 if ((error = priv_check(td, PRIV_ROOT)))
4663 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4666 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4670 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4675 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4680 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4683 error = mount_path(p, mp, &fullpath, &freepath);
4686 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4687 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4688 kfree(freepath, M_TEMP);
4690 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4691 if (priv_check(td, PRIV_ROOT)) {
4692 bcopy(sp, &sb, sizeof(sb));
4693 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4696 error = copyout(sp, uap->buf, sizeof(*sp));
4702 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4705 sys_fhstatvfs(struct fhstatvfs_args *uap)
4707 struct thread *td = curthread;
4708 struct proc *p = td->td_proc;
4716 * Must be super user
4718 if ((error = priv_check(td, PRIV_ROOT)))
4721 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4724 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4728 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4733 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4736 sp = &mp->mnt_vstat;
4738 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
4742 if (mp->mnt_flag & MNT_RDONLY)
4743 sp->f_flag |= ST_RDONLY;
4744 if (mp->mnt_flag & MNT_NOSUID)
4745 sp->f_flag |= ST_NOSUID;
4746 error = copyout(sp, uap->buf, sizeof(*sp));
4753 * Syscall to push extended attribute configuration information into the
4754 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4755 * a command (int cmd), and attribute name and misc data. For now, the
4756 * attribute name is left in userspace for consumption by the VFS_op.
4757 * It will probably be changed to be copied into sysspace by the
4758 * syscall in the future, once issues with various consumers of the
4759 * attribute code have raised their hands.
4761 * Currently this is used only by UFS Extended Attributes.
4764 sys_extattrctl(struct extattrctl_args *uap)
4766 struct nlookupdata nd;
4768 char attrname[EXTATTR_MAXNAMELEN];
4776 if (error == 0 && uap->filename) {
4777 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
4780 error = nlookup(&nd);
4782 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4786 if (error == 0 && uap->attrname) {
4787 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4792 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4794 error = nlookup(&nd);
4796 error = ncp_writechk(&nd.nl_nch);
4798 error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
4800 uap->attrname, nd.nl_cred);
4809 * Syscall to get a named extended attribute on a file or directory.
4812 sys_extattr_set_file(struct extattr_set_file_args *uap)
4814 char attrname[EXTATTR_MAXNAMELEN];
4815 struct nlookupdata nd;
4821 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4827 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4829 error = nlookup(&nd);
4831 error = ncp_writechk(&nd.nl_nch);
4833 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4839 bzero(&auio, sizeof(auio));
4840 aiov.iov_base = uap->data;
4841 aiov.iov_len = uap->nbytes;
4842 auio.uio_iov = &aiov;
4843 auio.uio_iovcnt = 1;
4844 auio.uio_offset = 0;
4845 auio.uio_resid = uap->nbytes;
4846 auio.uio_rw = UIO_WRITE;
4847 auio.uio_td = curthread;
4849 error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
4858 * Syscall to get a named extended attribute on a file or directory.
4861 sys_extattr_get_file(struct extattr_get_file_args *uap)
4863 char attrname[EXTATTR_MAXNAMELEN];
4864 struct nlookupdata nd;
4870 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4876 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4878 error = nlookup(&nd);
4880 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_SHARED, &vp);
4886 bzero(&auio, sizeof(auio));
4887 aiov.iov_base = uap->data;
4888 aiov.iov_len = uap->nbytes;
4889 auio.uio_iov = &aiov;
4890 auio.uio_iovcnt = 1;
4891 auio.uio_offset = 0;
4892 auio.uio_resid = uap->nbytes;
4893 auio.uio_rw = UIO_READ;
4894 auio.uio_td = curthread;
4896 error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
4898 uap->sysmsg_result = uap->nbytes - auio.uio_resid;
4906 * Syscall to delete a named extended attribute from a file or directory.
4907 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4910 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4912 char attrname[EXTATTR_MAXNAMELEN];
4913 struct nlookupdata nd;
4917 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4921 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4923 error = nlookup(&nd);
4925 error = ncp_writechk(&nd.nl_nch);
4927 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4929 error = VOP_SETEXTATTR(vp, uap->attrnamespace,
4930 attrname, NULL, nd.nl_cred);
4939 * Determine if the mount is visible to the process.
4942 chroot_visible_mnt(struct mount *mp, struct proc *p)
4944 struct nchandle nch;
4947 * Traverse from the mount point upwards. If we hit the process
4948 * root then the mount point is visible to the process.
4950 nch = mp->mnt_ncmountpt;
4952 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4953 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4956 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4957 nch = nch.mount->mnt_ncmounton;
4959 nch.ncp = nch.ncp->nc_parent;
4964 * If the mount point is not visible to the process, but the
4965 * process root is in a subdirectory of the mount, return
4968 if (p->p_fd->fd_nrdir.mount == mp)