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 #include <vfs/union/union.h>
81 static void mount_warning(struct mount *mp, const char *ctl, ...)
83 static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb);
84 static int checkvp_chdir (struct vnode *vn, struct thread *td);
85 static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch);
86 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
87 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
88 static int getutimes (struct timeval *, struct timespec *);
89 static int getutimens (const struct timespec *, struct timespec *, int *);
90 static int setfown (struct mount *, struct vnode *, uid_t, gid_t);
91 static int setfmode (struct vnode *, int);
92 static int setfflags (struct vnode *, int);
93 static int setutimes (struct vnode *, struct vattr *,
94 const struct timespec *, int);
95 static int usermount = 0; /* if 1, non-root can mount fs. */
97 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
99 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
100 "Allow non-root users to mount filesystems");
103 * Virtual File System System Calls
107 * Mount a file system.
109 * mount_args(char *type, char *path, int flags, caddr_t data)
114 sys_mount(struct mount_args *uap)
116 struct thread *td = curthread;
119 struct mount *mp, *nullmp;
120 struct vfsconf *vfsp;
121 int error, flag = 0, flag2 = 0;
124 struct nlookupdata nd;
125 char fstypename[MFSNAMELEN];
133 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
137 * Do not allow NFS export by non-root users.
139 if (uap->flags & MNT_EXPORTED) {
140 error = priv_check(td, PRIV_ROOT);
145 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
147 if (priv_check(td, PRIV_ROOT))
148 uap->flags |= MNT_NOSUID | MNT_NODEV;
151 * Lookup the requested path and extract the nch and vnode.
153 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
155 if ((error = nlookup(&nd)) == 0) {
156 if (nd.nl_nch.ncp->nc_vp == NULL)
166 * If the target filesystem is resolved via a nullfs mount, then
167 * nd.nl_nch.mount will be pointing to the nullfs mount structure
168 * instead of the target file system. We need it in case we are
171 nullmp = nd.nl_nch.mount;
174 * Extract the locked+refd ncp and cleanup the nd structure
177 cache_zero(&nd.nl_nch);
180 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
181 (mp = cache_findmount(&nch)) != NULL) {
190 * now we have the locked ref'd nch and unreferenced vnode.
193 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
200 * Extract the file system type. We need to know this early, to take
201 * appropriate actions if we are dealing with a nullfs.
203 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
210 * Now we have an unlocked ref'd nch and a locked ref'd vp
212 if (uap->flags & MNT_UPDATE) {
213 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
220 if (strncmp(fstypename, "null", 5) == 0) {
228 flag2 = mp->mnt_kern_flag;
230 * We only allow the filesystem to be reloaded if it
231 * is currently mounted read-only.
233 if ((uap->flags & MNT_RELOAD) &&
234 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
237 error = EOPNOTSUPP; /* Needs translation */
241 * Only root, or the user that did the original mount is
242 * permitted to update it.
244 if (mp->mnt_stat.f_owner != cred->cr_uid &&
245 (error = priv_check(td, PRIV_ROOT))) {
250 if (vfs_busy(mp, LK_NOWAIT)) {
264 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
265 lwkt_gettoken(&mp->mnt_token);
271 * If the user is not root, ensure that they own the directory
272 * onto which we are attempting to mount.
274 if ((error = VOP_GETATTR(vp, &va)) ||
275 (va.va_uid != cred->cr_uid &&
276 (error = priv_check(td, PRIV_ROOT)))) {
281 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
286 if (vp->v_type != VDIR) {
292 if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
298 vfsp = vfsconf_find_by_name(fstypename);
302 /* Only load modules for root (very important!) */
303 if ((error = priv_check(td, PRIV_ROOT)) != 0) {
308 error = linker_load_file(fstypename, &lf);
309 if (error || lf == NULL) {
317 /* lookup again, see if the VFS was loaded */
318 vfsp = vfsconf_find_by_name(fstypename);
321 linker_file_unload(lf);
336 * Allocate and initialize the filesystem.
338 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
340 vfs_busy(mp, LK_NOWAIT);
341 mp->mnt_op = vfsp->vfc_vfsops;
343 vfsp->vfc_refcount++;
344 mp->mnt_stat.f_type = vfsp->vfc_typenum;
345 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
346 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
347 mp->mnt_stat.f_owner = cred->cr_uid;
348 lwkt_gettoken(&mp->mnt_token);
352 * (per-mount token acquired at this point)
354 * Set the mount level flags.
356 if (uap->flags & MNT_RDONLY)
357 mp->mnt_flag |= MNT_RDONLY;
358 else if (mp->mnt_flag & MNT_RDONLY)
359 mp->mnt_kern_flag |= MNTK_WANTRDWR;
360 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
361 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
362 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
363 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
364 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
365 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
366 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
367 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
369 * Mount the filesystem.
370 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
373 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
374 if (mp->mnt_flag & MNT_UPDATE) {
375 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
376 mp->mnt_flag &= ~MNT_RDONLY;
377 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
378 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
381 mp->mnt_kern_flag = flag2;
383 lwkt_reltoken(&mp->mnt_token);
389 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
392 * Put the new filesystem on the mount list after root. The mount
393 * point gets its own mnt_ncmountpt (unless the VFS already set one
394 * up) which represents the root of the mount. The lookup code
395 * detects the mount point going forward and checks the root of
396 * the mount going backwards.
398 * It is not necessary to invalidate or purge the vnode underneath
399 * because elements under the mount will be given their own glue
403 if (mp->mnt_ncmountpt.ncp == NULL) {
405 * allocate, then unlock, but leave the ref intact
407 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
408 cache_unlock(&mp->mnt_ncmountpt);
410 mp->mnt_ncmounton = nch; /* inherits ref */
411 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
412 cache_ismounting(mp);
414 mountlist_insert(mp, MNTINS_LAST);
416 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
417 error = vfs_allocate_syncvnode(mp);
418 lwkt_reltoken(&mp->mnt_token);
420 error = VFS_START(mp, 0);
423 vn_syncer_thr_stop(mp);
424 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
425 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
426 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
427 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
428 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
429 mp->mnt_vfc->vfc_refcount--;
430 lwkt_reltoken(&mp->mnt_token);
441 * Scan all active processes to see if any of them have a current
442 * or root directory onto which the new filesystem has just been
443 * mounted. If so, replace them with the new mount point.
445 * Both old_nch and new_nch are ref'd on call but not locked.
446 * new_nch must be temporarily locked so it can be associated with the
447 * vnode representing the root of the mount point.
449 struct checkdirs_info {
450 struct nchandle old_nch;
451 struct nchandle new_nch;
452 struct vnode *old_vp;
453 struct vnode *new_vp;
456 static int checkdirs_callback(struct proc *p, void *data);
459 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
461 struct checkdirs_info info;
467 * If the old mount point's vnode has a usecount of 1, it is not
468 * being held as a descriptor anywhere.
470 olddp = old_nch->ncp->nc_vp;
471 if (olddp == NULL || VREFCNT(olddp) == 1)
475 * Force the root vnode of the new mount point to be resolved
476 * so we can update any matching processes.
479 if (VFS_ROOT(mp, &newdp))
480 panic("mount: lost mount");
483 vn_lock(newdp, LK_EXCLUSIVE | LK_RETRY);
484 cache_setunresolved(new_nch);
485 cache_setvp(new_nch, newdp);
486 cache_unlock(new_nch);
489 * Special handling of the root node
491 if (rootvnode == olddp) {
493 vfs_cache_setroot(newdp, cache_hold(new_nch));
497 * Pass newdp separately so the callback does not have to access
498 * it via new_nch->ncp->nc_vp.
500 info.old_nch = *old_nch;
501 info.new_nch = *new_nch;
503 allproc_scan(checkdirs_callback, &info);
508 * NOTE: callback is not MP safe because the scanned process's filedesc
509 * structure can be ripped out from under us, amoung other things.
512 checkdirs_callback(struct proc *p, void *data)
514 struct checkdirs_info *info = data;
515 struct filedesc *fdp;
516 struct nchandle ncdrop1;
517 struct nchandle ncdrop2;
518 struct vnode *vprele1;
519 struct vnode *vprele2;
521 if ((fdp = p->p_fd) != NULL) {
522 cache_zero(&ncdrop1);
523 cache_zero(&ncdrop2);
528 * MPUNSAFE - XXX fdp can be pulled out from under a
531 * A shared filedesc is ok, we don't have to copy it
532 * because we are making this change globally.
534 spin_lock(&fdp->fd_spin);
535 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
536 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
537 vprele1 = fdp->fd_cdir;
539 fdp->fd_cdir = info->new_vp;
540 ncdrop1 = fdp->fd_ncdir;
541 cache_copy(&info->new_nch, &fdp->fd_ncdir);
543 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
544 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
545 vprele2 = fdp->fd_rdir;
547 fdp->fd_rdir = info->new_vp;
548 ncdrop2 = fdp->fd_nrdir;
549 cache_copy(&info->new_nch, &fdp->fd_nrdir);
551 spin_unlock(&fdp->fd_spin);
553 cache_drop(&ncdrop1);
555 cache_drop(&ncdrop2);
565 * Unmount a file system.
567 * Note: unmount takes a path to the vnode mounted on as argument,
568 * not special file (as before).
570 * umount_args(char *path, int flags)
575 sys_unmount(struct unmount_args *uap)
577 struct thread *td = curthread;
578 struct proc *p __debugvar = td->td_proc;
579 struct mount *mp = NULL;
580 struct nlookupdata nd;
585 if (td->td_ucred->cr_prison != NULL) {
589 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
592 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
594 error = nlookup(&nd);
598 mp = nd.nl_nch.mount;
601 * Only root, or the user that did the original mount is
602 * permitted to unmount this filesystem.
604 if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) &&
605 (error = priv_check(td, PRIV_ROOT)))
609 * Don't allow unmounting the root file system.
611 if (mp->mnt_flag & MNT_ROOTFS) {
617 * Must be the root of the filesystem
619 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
627 error = dounmount(mp, uap->flags);
634 * Do the actual file system unmount.
637 dounmount_interlock(struct mount *mp)
639 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
641 mp->mnt_kern_flag |= MNTK_UNMOUNT;
646 unmount_allproc_cb(struct proc *p, void *arg)
650 if (p->p_textnch.ncp == NULL)
653 mp = (struct mount *)arg;
654 if (p->p_textnch.mount == mp)
655 cache_drop(&p->p_textnch);
661 dounmount(struct mount *mp, int flags)
663 struct namecache *ncp;
672 lwkt_gettoken(&mp->mnt_token);
674 * Exclusive access for unmounting purposes
676 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
680 * Allow filesystems to detect that a forced unmount is in progress.
682 if (flags & MNT_FORCE)
683 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
684 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_TIMELOCK);
685 error = lockmgr(&mp->mnt_lock, lflags);
687 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
688 if (mp->mnt_kern_flag & MNTK_MWAIT) {
689 mp->mnt_kern_flag &= ~MNTK_MWAIT;
695 if (mp->mnt_flag & MNT_EXPUBLIC)
696 vfs_setpublicfs(NULL, NULL, NULL);
698 vfs_msync(mp, MNT_WAIT);
699 async_flag = mp->mnt_flag & MNT_ASYNC;
700 mp->mnt_flag &=~ MNT_ASYNC;
703 * If this filesystem isn't aliasing other filesystems,
704 * try to invalidate any remaining namecache entries and
705 * check the count afterwords.
707 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
708 cache_lock(&mp->mnt_ncmountpt);
709 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
710 cache_unlock(&mp->mnt_ncmountpt);
712 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
713 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
714 allproc_scan(&unmount_allproc_cb, mp);
717 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
718 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
720 if ((flags & MNT_FORCE) == 0) {
722 mount_warning(mp, "Cannot unmount: "
728 mount_warning(mp, "Forced unmount: "
739 * Decomission our special mnt_syncer vnode. This also stops
740 * the vnlru code. If we are unable to unmount we recommission
743 * Then sync the filesystem.
745 if ((vp = mp->mnt_syncer) != NULL) {
746 mp->mnt_syncer = NULL;
747 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
750 if ((mp->mnt_flag & MNT_RDONLY) == 0)
751 VFS_SYNC(mp, MNT_WAIT);
754 * nchandle records ref the mount structure. Expect a count of 1
755 * (our mount->mnt_ncmountpt).
757 * Scans can get temporary refs on a mountpoint (thought really
758 * heavy duty stuff like cache_findmount() do not).
760 for (retry = 0; retry < 10 && mp->mnt_refs != 1; ++retry) {
761 cache_unmounting(mp);
762 tsleep(&mp->mnt_refs, 0, "mntbsy", hz / 10 + 1);
764 if (mp->mnt_refs != 1) {
765 if ((flags & MNT_FORCE) == 0) {
766 mount_warning(mp, "Cannot unmount: "
767 "%d mount refs still present",
771 mount_warning(mp, "Forced unmount: "
772 "%d mount refs still present",
779 * So far so good, sync the filesystem once more and
780 * call the VFS unmount code if the sync succeeds.
783 if (((mp->mnt_flag & MNT_RDONLY) ||
784 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
785 (flags & MNT_FORCE)) {
786 error = VFS_UNMOUNT(mp, flags);
791 * If an error occurred we can still recover, restoring the
792 * syncer vnode and misc flags.
795 if (mp->mnt_syncer == NULL)
796 vfs_allocate_syncvnode(mp);
797 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
798 mp->mnt_flag |= async_flag;
799 lockmgr(&mp->mnt_lock, LK_RELEASE);
800 if (mp->mnt_kern_flag & MNTK_MWAIT) {
801 mp->mnt_kern_flag &= ~MNTK_MWAIT;
807 * Clean up any journals still associated with the mount after
808 * filesystem activity has ceased.
810 journal_remove_all_journals(mp,
811 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
813 mountlist_remove(mp);
816 * Remove any installed vnode ops here so the individual VFSs don't
819 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
820 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
821 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
822 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
823 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
825 if (mp->mnt_ncmountpt.ncp != NULL) {
826 nch = mp->mnt_ncmountpt;
827 cache_zero(&mp->mnt_ncmountpt);
828 cache_clrmountpt(&nch);
831 if (mp->mnt_ncmounton.ncp != NULL) {
832 cache_unmounting(mp);
833 nch = mp->mnt_ncmounton;
834 cache_zero(&mp->mnt_ncmounton);
835 cache_clrmountpt(&nch);
839 mp->mnt_vfc->vfc_refcount--;
840 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
841 panic("unmount: dangling vnode");
842 lockmgr(&mp->mnt_lock, LK_RELEASE);
843 if (mp->mnt_kern_flag & MNTK_MWAIT) {
844 mp->mnt_kern_flag &= ~MNTK_MWAIT;
849 * If we reach here and freeok != 0 we must free the mount.
850 * If refs > 1 cycle and wait, just in case someone tried
851 * to busy the mount after we decided to do the unmount.
854 while (mp->mnt_refs > 1) {
855 cache_unmounting(mp);
857 tsleep(&mp->mnt_refs, 0, "umntrwait", hz / 10 + 1);
859 lwkt_reltoken(&mp->mnt_token);
866 lwkt_reltoken(&mp->mnt_token);
872 mount_warning(struct mount *mp, const char *ctl, ...)
879 if (cache_fullpath(NULL, &mp->mnt_ncmounton, NULL,
880 &ptr, &buf, 0) == 0) {
881 kprintf("unmount(%s): ", ptr);
886 kprintf("unmount(%p", mp);
887 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
888 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
897 * Shim cache_fullpath() to handle the case where a process is chrooted into
898 * a subdirectory of a mount. In this case if the root mount matches the
899 * process root directory's mount we have to specify the process's root
900 * directory instead of the mount point, because the mount point might
901 * be above the root directory.
905 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
907 struct nchandle *nch;
909 if (p && p->p_fd->fd_nrdir.mount == mp)
910 nch = &p->p_fd->fd_nrdir;
912 nch = &mp->mnt_ncmountpt;
913 return(cache_fullpath(p, nch, NULL, rb, fb, 0));
917 * Sync each mounted filesystem.
921 static int syncprt = 0;
922 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
925 static int sync_callback(struct mount *mp, void *data);
928 sys_sync(struct sync_args *uap)
930 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
936 sync_callback(struct mount *mp, void *data __unused)
940 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
941 asyncflag = mp->mnt_flag & MNT_ASYNC;
942 mp->mnt_flag &= ~MNT_ASYNC;
943 vfs_msync(mp, MNT_NOWAIT);
944 VFS_SYNC(mp, MNT_NOWAIT);
945 mp->mnt_flag |= asyncflag;
950 /* XXX PRISON: could be per prison flag */
951 static int prison_quotas;
953 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
957 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
959 * Change filesystem quotas.
964 sys_quotactl(struct quotactl_args *uap)
966 struct nlookupdata nd;
973 if (td->td_ucred->cr_prison && !prison_quotas) {
978 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
980 error = nlookup(&nd);
982 mp = nd.nl_nch.mount;
983 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
984 uap->arg, nd.nl_cred);
993 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
994 * void *buf, int buflen)
996 * This function operates on a mount point and executes the specified
997 * operation using the specified control data, and possibly returns data.
999 * The actual number of bytes stored in the result buffer is returned, 0
1000 * if none, otherwise an error is returned.
1005 sys_mountctl(struct mountctl_args *uap)
1007 struct thread *td = curthread;
1008 struct proc *p = td->td_proc;
1016 * Sanity and permissions checks. We must be root.
1019 if (td->td_ucred->cr_prison != NULL)
1021 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
1022 (error = priv_check(td, PRIV_ROOT)) != 0)
1026 * Argument length checks
1028 if (uap->ctllen < 0 || uap->ctllen > 1024)
1030 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
1032 if (uap->path == NULL)
1036 * Allocate the necessary buffers and copyin data
1038 path = objcache_get(namei_oc, M_WAITOK);
1039 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1044 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
1045 error = copyin(uap->ctl, ctl, uap->ctllen);
1050 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
1053 * Validate the descriptor
1056 fp = holdfp(p->p_fd, uap->fd, -1);
1066 * Execute the internal kernel function and clean up.
1069 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
1073 if (error == 0 && uap->sysmsg_result > 0)
1074 error = copyout(buf, uap->buf, uap->sysmsg_result);
1077 objcache_put(namei_oc, path);
1086 * Execute a mount control operation by resolving the path to a mount point
1087 * and calling vop_mountctl().
1089 * Use the mount point from the nch instead of the vnode so nullfs mounts
1090 * can properly spike the VOP.
1093 kern_mountctl(const char *path, int op, struct file *fp,
1094 const void *ctl, int ctllen,
1095 void *buf, int buflen, int *res)
1099 struct nlookupdata nd;
1104 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1106 error = nlookup(&nd);
1108 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1109 mp = nd.nl_nch.mount;
1116 * Must be the root of the filesystem
1118 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1122 error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen,
1129 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1131 struct thread *td = curthread;
1132 struct proc *p = td->td_proc;
1135 char *fullpath, *freepath;
1138 if ((error = nlookup(nd)) != 0)
1140 mp = nd->nl_nch.mount;
1142 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1145 error = mount_path(p, mp, &fullpath, &freepath);
1148 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1149 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1150 kfree(freepath, M_TEMP);
1152 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1153 bcopy(sp, buf, sizeof(*buf));
1154 /* Only root should have access to the fsid's. */
1155 if (priv_check(td, PRIV_ROOT))
1156 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1161 * statfs_args(char *path, struct statfs *buf)
1163 * Get filesystem statistics.
1166 sys_statfs(struct statfs_args *uap)
1168 struct nlookupdata nd;
1172 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1174 error = kern_statfs(&nd, &buf);
1177 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1182 kern_fstatfs(int fd, struct statfs *buf)
1184 struct thread *td = curthread;
1185 struct proc *p = td->td_proc;
1189 char *fullpath, *freepath;
1193 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1197 * Try to use mount info from any overlays rather than the
1198 * mount info for the underlying vnode, otherwise we will
1199 * fail when operating on null-mounted paths inside a chroot.
1201 if ((mp = fp->f_nchandle.mount) == NULL)
1202 mp = ((struct vnode *)fp->f_data)->v_mount;
1207 if (fp->f_cred == NULL) {
1212 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1215 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1217 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1218 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1219 kfree(freepath, M_TEMP);
1221 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1222 bcopy(sp, buf, sizeof(*buf));
1224 /* Only root should have access to the fsid's. */
1225 if (priv_check(td, PRIV_ROOT))
1226 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1234 * fstatfs_args(int fd, struct statfs *buf)
1236 * Get filesystem statistics.
1239 sys_fstatfs(struct fstatfs_args *uap)
1244 error = kern_fstatfs(uap->fd, &buf);
1247 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1252 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1258 if ((error = nlookup(nd)) != 0)
1260 mp = nd->nl_nch.mount;
1261 sp = &mp->mnt_vstat;
1262 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1266 if (mp->mnt_flag & MNT_RDONLY)
1267 sp->f_flag |= ST_RDONLY;
1268 if (mp->mnt_flag & MNT_NOSUID)
1269 sp->f_flag |= ST_NOSUID;
1270 bcopy(sp, buf, sizeof(*buf));
1275 * statfs_args(char *path, struct statfs *buf)
1277 * Get filesystem statistics.
1280 sys_statvfs(struct statvfs_args *uap)
1282 struct nlookupdata nd;
1286 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1288 error = kern_statvfs(&nd, &buf);
1291 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1296 kern_fstatvfs(int fd, struct statvfs *buf)
1298 struct thread *td = curthread;
1299 struct proc *p = td->td_proc;
1306 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1308 if ((mp = fp->f_nchandle.mount) == NULL)
1309 mp = ((struct vnode *)fp->f_data)->v_mount;
1314 if (fp->f_cred == NULL) {
1318 sp = &mp->mnt_vstat;
1319 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1323 if (mp->mnt_flag & MNT_RDONLY)
1324 sp->f_flag |= ST_RDONLY;
1325 if (mp->mnt_flag & MNT_NOSUID)
1326 sp->f_flag |= ST_NOSUID;
1328 bcopy(sp, buf, sizeof(*buf));
1336 * fstatfs_args(int fd, struct statfs *buf)
1338 * Get filesystem statistics.
1341 sys_fstatvfs(struct fstatvfs_args *uap)
1346 error = kern_fstatvfs(uap->fd, &buf);
1349 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1354 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1356 * Get statistics on all filesystems.
1359 struct getfsstat_info {
1360 struct statfs *sfsp;
1368 static int getfsstat_callback(struct mount *, void *);
1371 sys_getfsstat(struct getfsstat_args *uap)
1373 struct thread *td = curthread;
1374 struct getfsstat_info info;
1376 bzero(&info, sizeof(info));
1378 info.maxcount = uap->bufsize / sizeof(struct statfs);
1379 info.sfsp = uap->buf;
1381 info.flags = uap->flags;
1384 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1385 if (info.sfsp && info.count > info.maxcount)
1386 uap->sysmsg_result = info.maxcount;
1388 uap->sysmsg_result = info.count;
1389 return (info.error);
1393 getfsstat_callback(struct mount *mp, void *data)
1395 struct getfsstat_info *info = data;
1401 if (info->sfsp && info->count < info->maxcount) {
1402 if (info->td->td_proc &&
1403 !chroot_visible_mnt(mp, info->td->td_proc)) {
1409 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1410 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1411 * overrides MNT_WAIT.
1413 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1414 (info->flags & MNT_WAIT)) &&
1415 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1418 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1420 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1422 info->error = error;
1425 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1426 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1427 kfree(freepath, M_TEMP);
1429 error = copyout(sp, info->sfsp, sizeof(*sp));
1431 info->error = error;
1441 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1442 long bufsize, int flags)
1444 * Get statistics on all filesystems.
1447 struct getvfsstat_info {
1448 struct statfs *sfsp;
1449 struct statvfs *vsfsp;
1457 static int getvfsstat_callback(struct mount *, void *);
1460 sys_getvfsstat(struct getvfsstat_args *uap)
1462 struct thread *td = curthread;
1463 struct getvfsstat_info info;
1465 bzero(&info, sizeof(info));
1467 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1468 info.sfsp = uap->buf;
1469 info.vsfsp = uap->vbuf;
1471 info.flags = uap->flags;
1474 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1475 if (info.vsfsp && info.count > info.maxcount)
1476 uap->sysmsg_result = info.maxcount;
1478 uap->sysmsg_result = info.count;
1479 return (info.error);
1483 getvfsstat_callback(struct mount *mp, void *data)
1485 struct getvfsstat_info *info = data;
1487 struct statvfs *vsp;
1492 if (info->vsfsp && info->count < info->maxcount) {
1493 if (info->td->td_proc &&
1494 !chroot_visible_mnt(mp, info->td->td_proc)) {
1498 vsp = &mp->mnt_vstat;
1501 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1502 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1503 * overrides MNT_WAIT.
1505 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1506 (info->flags & MNT_WAIT)) &&
1507 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1510 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1512 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1513 (info->flags & MNT_WAIT)) &&
1514 (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) {
1518 if (mp->mnt_flag & MNT_RDONLY)
1519 vsp->f_flag |= ST_RDONLY;
1520 if (mp->mnt_flag & MNT_NOSUID)
1521 vsp->f_flag |= ST_NOSUID;
1523 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1525 info->error = error;
1528 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1529 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1530 kfree(freepath, M_TEMP);
1532 error = copyout(sp, info->sfsp, sizeof(*sp));
1534 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1536 info->error = error;
1548 * fchdir_args(int fd)
1550 * Change current working directory to a given file descriptor.
1553 sys_fchdir(struct fchdir_args *uap)
1555 struct thread *td = curthread;
1556 struct proc *p = td->td_proc;
1557 struct filedesc *fdp = p->p_fd;
1558 struct vnode *vp, *ovp;
1561 struct nchandle nch, onch, tnch;
1564 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1566 lwkt_gettoken(&p->p_token);
1567 vp = (struct vnode *)fp->f_data;
1569 vn_lock(vp, LK_SHARED | LK_RETRY);
1570 if (fp->f_nchandle.ncp == NULL)
1573 error = checkvp_chdir(vp, td);
1578 cache_copy(&fp->f_nchandle, &nch);
1581 * If the ncp has become a mount point, traverse through
1585 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1586 (mp = cache_findmount(&nch)) != NULL
1588 error = nlookup_mp(mp, &tnch);
1590 cache_unlock(&tnch); /* leave ref intact */
1592 vp = tnch.ncp->nc_vp;
1593 error = vget(vp, LK_SHARED);
1594 KKASSERT(error == 0);
1598 cache_dropmount(mp);
1602 onch = fdp->fd_ncdir;
1603 vn_unlock(vp); /* leave ref intact */
1605 fdp->fd_ncdir = nch;
1614 lwkt_reltoken(&p->p_token);
1619 kern_chdir(struct nlookupdata *nd)
1621 struct thread *td = curthread;
1622 struct proc *p = td->td_proc;
1623 struct filedesc *fdp = p->p_fd;
1624 struct vnode *vp, *ovp;
1625 struct nchandle onch;
1628 nd->nl_flags |= NLC_SHAREDLOCK;
1629 if ((error = nlookup(nd)) != 0)
1631 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1633 if ((error = vget(vp, LK_SHARED)) != 0)
1636 lwkt_gettoken(&p->p_token);
1637 error = checkvp_chdir(vp, td);
1641 onch = fdp->fd_ncdir;
1642 cache_unlock(&nd->nl_nch); /* leave reference intact */
1643 fdp->fd_ncdir = nd->nl_nch;
1647 cache_zero(&nd->nl_nch);
1651 lwkt_reltoken(&p->p_token);
1656 * chdir_args(char *path)
1658 * Change current working directory (``.'').
1661 sys_chdir(struct chdir_args *uap)
1663 struct nlookupdata nd;
1666 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1668 error = kern_chdir(&nd);
1674 * Helper function for raised chroot(2) security function: Refuse if
1675 * any filedescriptors are open directories.
1678 chroot_refuse_vdir_fds(struct filedesc *fdp)
1685 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1686 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1688 vp = (struct vnode *)fp->f_data;
1689 if (vp->v_type != VDIR) {
1700 * This sysctl determines if we will allow a process to chroot(2) if it
1701 * has a directory open:
1702 * 0: disallowed for all processes.
1703 * 1: allowed for processes that were not already chroot(2)'ed.
1704 * 2: allowed for all processes.
1707 static int chroot_allow_open_directories = 1;
1709 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1710 &chroot_allow_open_directories, 0, "");
1713 * chroot to the specified namecache entry. We obtain the vp from the
1714 * namecache data. The passed ncp must be locked and referenced and will
1715 * remain locked and referenced on return.
1718 kern_chroot(struct nchandle *nch)
1720 struct thread *td = curthread;
1721 struct proc *p = td->td_proc;
1722 struct filedesc *fdp = p->p_fd;
1727 * Only privileged user can chroot
1729 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1734 * Disallow open directory descriptors (fchdir() breakouts).
1736 if (chroot_allow_open_directories == 0 ||
1737 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1738 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1741 if ((vp = nch->ncp->nc_vp) == NULL)
1744 if ((error = vget(vp, LK_SHARED)) != 0)
1748 * Check the validity of vp as a directory to change to and
1749 * associate it with rdir/jdir.
1751 error = checkvp_chdir(vp, td);
1752 vn_unlock(vp); /* leave reference intact */
1754 vrele(fdp->fd_rdir);
1755 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1756 cache_drop(&fdp->fd_nrdir);
1757 cache_copy(nch, &fdp->fd_nrdir);
1758 if (fdp->fd_jdir == NULL) {
1761 cache_copy(nch, &fdp->fd_njdir);
1770 * chroot_args(char *path)
1772 * Change notion of root (``/'') directory.
1775 sys_chroot(struct chroot_args *uap)
1777 struct thread *td __debugvar = curthread;
1778 struct nlookupdata nd;
1781 KKASSERT(td->td_proc);
1782 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1784 nd.nl_flags |= NLC_EXEC;
1785 error = nlookup(&nd);
1787 error = kern_chroot(&nd.nl_nch);
1794 sys_chroot_kernel(struct chroot_kernel_args *uap)
1796 struct thread *td = curthread;
1797 struct nlookupdata nd;
1798 struct nchandle *nch;
1802 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1806 error = nlookup(&nd);
1812 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1816 if ((vp = nch->ncp->nc_vp) == NULL) {
1821 if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0)
1824 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap->path);
1826 vfs_cache_setroot(vp, cache_hold(nch));
1836 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1837 * determine whether it is legal to chdir to the vnode. The vnode's state
1838 * is not changed by this call.
1841 checkvp_chdir(struct vnode *vp, struct thread *td)
1845 if (vp->v_type != VDIR)
1848 error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
1853 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1855 struct thread *td = curthread;
1856 struct proc *p = td->td_proc;
1857 struct lwp *lp = td->td_lwp;
1858 struct filedesc *fdp = p->p_fd;
1863 int type, indx, error = 0;
1866 if ((oflags & O_ACCMODE) == O_ACCMODE)
1868 flags = FFLAGS(oflags);
1869 error = falloc(lp, &nfp, NULL);
1873 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1876 * XXX p_dupfd is a real mess. It allows a device to return a
1877 * file descriptor to be duplicated rather then doing the open
1883 * Call vn_open() to do the lookup and assign the vnode to the
1884 * file pointer. vn_open() does not change the ref count on fp
1885 * and the vnode, on success, will be inherited by the file pointer
1888 * Request a shared lock on the vnode if possible.
1890 nd->nl_flags |= NLC_LOCKVP;
1891 if ((flags & (O_CREAT|O_TRUNC)) == 0)
1892 nd->nl_flags |= NLC_SHAREDLOCK;
1894 error = vn_open(nd, fp, flags, cmode);
1899 * handle special fdopen() case. bleh. dupfdopen() is
1900 * responsible for dropping the old contents of ofiles[indx]
1903 * Note that fsetfd() will add a ref to fp which represents
1904 * the fd_files[] assignment. We must still drop our
1907 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1908 if (fdalloc(p, 0, &indx) == 0) {
1909 error = dupfdopen(fdp, indx, lp->lwp_dupfd, flags, error);
1912 fdrop(fp); /* our ref */
1915 fsetfd(fdp, NULL, indx);
1918 fdrop(fp); /* our ref */
1919 if (error == ERESTART)
1925 * ref the vnode for ourselves so it can't be ripped out from under
1926 * is. XXX need an ND flag to request that the vnode be returned
1929 * Reserve a file descriptor but do not assign it until the open
1932 vp = (struct vnode *)fp->f_data;
1934 if ((error = fdalloc(p, 0, &indx)) != 0) {
1941 * If no error occurs the vp will have been assigned to the file
1946 if (flags & (O_EXLOCK | O_SHLOCK)) {
1947 lf.l_whence = SEEK_SET;
1950 if (flags & O_EXLOCK)
1951 lf.l_type = F_WRLCK;
1953 lf.l_type = F_RDLCK;
1954 if (flags & FNONBLOCK)
1959 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1961 * lock request failed. Clean up the reserved
1965 fsetfd(fdp, NULL, indx);
1969 atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */
1973 * Assert that all regular file vnodes were created with a object.
1975 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1976 ("open: regular file has no backing object after vn_open"));
1982 * release our private reference, leaving the one associated with the
1983 * descriptor table intact.
1985 if (oflags & O_CLOEXEC)
1986 fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
1987 fsetfd(fdp, fp, indx);
1994 * open_args(char *path, int flags, int mode)
1996 * Check permissions, allocate an open file structure,
1997 * and call the device open routine if any.
2000 sys_open(struct open_args *uap)
2002 struct nlookupdata nd;
2005 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2007 error = kern_open(&nd, uap->flags,
2008 uap->mode, &uap->sysmsg_result);
2015 * openat_args(int fd, char *path, int flags, int mode)
2018 sys_openat(struct openat_args *uap)
2020 struct nlookupdata nd;
2024 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2026 error = kern_open(&nd, uap->flags, uap->mode,
2027 &uap->sysmsg_result);
2029 nlookup_done_at(&nd, fp);
2034 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
2036 struct thread *td = curthread;
2037 struct proc *p = td->td_proc;
2046 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2047 vattr.va_rmajor = rmajor;
2048 vattr.va_rminor = rminor;
2050 switch (mode & S_IFMT) {
2051 case S_IFMT: /* used by badsect to flag bad sectors */
2052 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
2053 vattr.va_type = VBAD;
2056 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2057 vattr.va_type = VCHR;
2060 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2061 vattr.va_type = VBLK;
2064 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2067 case S_IFDIR: /* special directories support for HAMMER */
2068 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2069 vattr.va_type = VDIR;
2080 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2081 if ((error = nlookup(nd)) != 0)
2083 if (nd->nl_nch.ncp->nc_vp)
2085 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2089 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2090 nd->nl_cred, NAMEI_CREATE);
2093 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2094 &vp, nd->nl_cred, &vattr);
2102 * mknod_args(char *path, int mode, int dev)
2104 * Create a special file.
2107 sys_mknod(struct mknod_args *uap)
2109 struct nlookupdata nd;
2112 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2114 error = kern_mknod(&nd, uap->mode,
2115 umajor(uap->dev), uminor(uap->dev));
2122 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2124 * Create a special file. The path is relative to the directory associated
2128 sys_mknodat(struct mknodat_args *uap)
2130 struct nlookupdata nd;
2134 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2136 error = kern_mknod(&nd, uap->mode,
2137 umajor(uap->dev), uminor(uap->dev));
2139 nlookup_done_at(&nd, fp);
2144 kern_mkfifo(struct nlookupdata *nd, int mode)
2146 struct thread *td = curthread;
2147 struct proc *p = td->td_proc;
2154 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2155 if ((error = nlookup(nd)) != 0)
2157 if (nd->nl_nch.ncp->nc_vp)
2159 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2163 vattr.va_type = VFIFO;
2164 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2166 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2173 * mkfifo_args(char *path, int mode)
2175 * Create a named pipe.
2178 sys_mkfifo(struct mkfifo_args *uap)
2180 struct nlookupdata nd;
2183 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2185 error = kern_mkfifo(&nd, uap->mode);
2191 * mkfifoat_args(int fd, char *path, mode_t mode)
2193 * Create a named pipe. The path is relative to the directory associated
2197 sys_mkfifoat(struct mkfifoat_args *uap)
2199 struct nlookupdata nd;
2203 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2205 error = kern_mkfifo(&nd, uap->mode);
2206 nlookup_done_at(&nd, fp);
2210 static int hardlink_check_uid = 0;
2211 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2212 &hardlink_check_uid, 0,
2213 "Unprivileged processes cannot create hard links to files owned by other "
2215 static int hardlink_check_gid = 0;
2216 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2217 &hardlink_check_gid, 0,
2218 "Unprivileged processes cannot create hard links to files owned by other "
2222 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2228 * Shortcut if disabled
2230 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2234 * Privileged user can always hardlink
2236 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2240 * Otherwise only if the originating file is owned by the
2241 * same user or group. Note that any group is allowed if
2242 * the file is owned by the caller.
2244 error = VOP_GETATTR(vp, &va);
2248 if (hardlink_check_uid) {
2249 if (cred->cr_uid != va.va_uid)
2253 if (hardlink_check_gid) {
2254 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2262 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2264 struct thread *td = curthread;
2269 * Lookup the source and obtained a locked vnode.
2271 * You may only hardlink a file which you have write permission
2272 * on or which you own.
2274 * XXX relookup on vget failure / race ?
2277 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2278 if ((error = nlookup(nd)) != 0)
2280 vp = nd->nl_nch.ncp->nc_vp;
2281 KKASSERT(vp != NULL);
2282 if (vp->v_type == VDIR)
2283 return (EPERM); /* POSIX */
2284 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2286 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2290 * Unlock the source so we can lookup the target without deadlocking
2291 * (XXX vp is locked already, possible other deadlock?). The target
2294 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2295 nd->nl_flags &= ~NLC_NCPISLOCKED;
2296 cache_unlock(&nd->nl_nch);
2299 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2300 if ((error = nlookup(linknd)) != 0) {
2304 if (linknd->nl_nch.ncp->nc_vp) {
2308 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
2315 * Finally run the new API VOP.
2317 error = can_hardlink(vp, td, td->td_ucred);
2319 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2320 vp, linknd->nl_cred);
2327 * link_args(char *path, char *link)
2329 * Make a hard file link.
2332 sys_link(struct link_args *uap)
2334 struct nlookupdata nd, linknd;
2337 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2339 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2341 error = kern_link(&nd, &linknd);
2342 nlookup_done(&linknd);
2349 * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags)
2351 * Make a hard file link. The path1 argument is relative to the directory
2352 * associated with fd1, and similarly the path2 argument is relative to
2353 * the directory associated with fd2.
2356 sys_linkat(struct linkat_args *uap)
2358 struct nlookupdata nd, linknd;
2359 struct file *fp1, *fp2;
2362 error = nlookup_init_at(&nd, &fp1, uap->fd1, uap->path1, UIO_USERSPACE,
2363 (uap->flags & AT_SYMLINK_FOLLOW) ? NLC_FOLLOW : 0);
2365 error = nlookup_init_at(&linknd, &fp2, uap->fd2,
2366 uap->path2, UIO_USERSPACE, 0);
2368 error = kern_link(&nd, &linknd);
2369 nlookup_done_at(&linknd, fp2);
2371 nlookup_done_at(&nd, fp1);
2376 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2384 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2385 if ((error = nlookup(nd)) != 0)
2387 if (nd->nl_nch.ncp->nc_vp)
2389 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2393 vattr.va_mode = mode;
2394 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2401 * symlink(char *path, char *link)
2403 * Make a symbolic link.
2406 sys_symlink(struct symlink_args *uap)
2408 struct thread *td = curthread;
2409 struct nlookupdata nd;
2414 path = objcache_get(namei_oc, M_WAITOK);
2415 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2417 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2419 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2420 error = kern_symlink(&nd, path, mode);
2424 objcache_put(namei_oc, path);
2429 * symlinkat_args(char *path1, int fd, char *path2)
2431 * Make a symbolic link. The path2 argument is relative to the directory
2432 * associated with fd.
2435 sys_symlinkat(struct symlinkat_args *uap)
2437 struct thread *td = curthread;
2438 struct nlookupdata nd;
2444 path1 = objcache_get(namei_oc, M_WAITOK);
2445 error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2447 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2450 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2451 error = kern_symlink(&nd, path1, mode);
2453 nlookup_done_at(&nd, fp);
2455 objcache_put(namei_oc, path1);
2460 * undelete_args(char *path)
2462 * Delete a whiteout from the filesystem.
2465 sys_undelete(struct undelete_args *uap)
2467 struct nlookupdata nd;
2470 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2472 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2474 error = nlookup(&nd);
2476 error = ncp_writechk(&nd.nl_nch);
2478 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2486 kern_unlink(struct nlookupdata *nd)
2491 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2492 if ((error = nlookup(nd)) != 0)
2494 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2496 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2501 * unlink_args(char *path)
2503 * Delete a name from the filesystem.
2506 sys_unlink(struct unlink_args *uap)
2508 struct nlookupdata nd;
2511 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2513 error = kern_unlink(&nd);
2520 * unlinkat_args(int fd, char *path, int flags)
2522 * Delete the file or directory entry pointed to by fd/path.
2525 sys_unlinkat(struct unlinkat_args *uap)
2527 struct nlookupdata nd;
2531 if (uap->flags & ~AT_REMOVEDIR)
2534 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2536 if (uap->flags & AT_REMOVEDIR)
2537 error = kern_rmdir(&nd);
2539 error = kern_unlink(&nd);
2541 nlookup_done_at(&nd, fp);
2546 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2548 struct thread *td = curthread;
2549 struct proc *p = td->td_proc;
2556 fp = holdfp(p->p_fd, fd, -1);
2559 if (fp->f_type != DTYPE_VNODE) {
2563 vp = (struct vnode *)fp->f_data;
2567 spin_lock(&fp->f_spin);
2568 new_offset = fp->f_offset + offset;
2572 error = VOP_GETATTR(vp, &vattr);
2573 spin_lock(&fp->f_spin);
2574 new_offset = offset + vattr.va_size;
2577 new_offset = offset;
2579 spin_lock(&fp->f_spin);
2584 spin_lock(&fp->f_spin);
2589 * Validate the seek position. Negative offsets are not allowed
2590 * for regular files or directories.
2592 * Normally we would also not want to allow negative offsets for
2593 * character and block-special devices. However kvm addresses
2594 * on 64 bit architectures might appear to be negative and must
2598 if (new_offset < 0 &&
2599 (vp->v_type == VREG || vp->v_type == VDIR)) {
2602 fp->f_offset = new_offset;
2605 *res = fp->f_offset;
2606 spin_unlock(&fp->f_spin);
2613 * lseek_args(int fd, int pad, off_t offset, int whence)
2615 * Reposition read/write file offset.
2618 sys_lseek(struct lseek_args *uap)
2622 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2623 &uap->sysmsg_offset);
2629 * Check if current process can access given file. amode is a bitmask of *_OK
2630 * access bits. flags is a bitmask of AT_* flags.
2633 kern_access(struct nlookupdata *nd, int amode, int flags)
2638 if (flags & ~AT_EACCESS)
2640 nd->nl_flags |= NLC_SHAREDLOCK;
2641 if ((error = nlookup(nd)) != 0)
2644 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
2648 /* Flags == 0 means only check for existence. */
2657 if ((mode & VWRITE) == 0 ||
2658 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2659 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2662 * If the file handle is stale we have to re-resolve the
2663 * entry with the ncp held exclusively. This is a hack
2666 if (error == ESTALE) {
2668 cache_unlock(&nd->nl_nch);
2669 cache_lock(&nd->nl_nch);
2670 cache_setunresolved(&nd->nl_nch);
2671 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2684 * access_args(char *path, int flags)
2686 * Check access permissions.
2689 sys_access(struct access_args *uap)
2691 struct nlookupdata nd;
2694 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2696 error = kern_access(&nd, uap->flags, 0);
2703 * eaccess_args(char *path, int flags)
2705 * Check access permissions.
2708 sys_eaccess(struct eaccess_args *uap)
2710 struct nlookupdata nd;
2713 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2715 error = kern_access(&nd, uap->flags, AT_EACCESS);
2722 * faccessat_args(int fd, char *path, int amode, int flags)
2724 * Check access permissions.
2727 sys_faccessat(struct faccessat_args *uap)
2729 struct nlookupdata nd;
2733 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2736 error = kern_access(&nd, uap->amode, uap->flags);
2737 nlookup_done_at(&nd, fp);
2742 kern_stat(struct nlookupdata *nd, struct stat *st)
2747 nd->nl_flags |= NLC_SHAREDLOCK;
2748 if ((error = nlookup(nd)) != 0)
2751 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2754 if ((error = vget(vp, LK_SHARED)) != 0)
2756 error = vn_stat(vp, st, nd->nl_cred);
2759 * If the file handle is stale we have to re-resolve the
2760 * entry with the ncp held exclusively. This is a hack
2763 if (error == ESTALE) {
2765 cache_unlock(&nd->nl_nch);
2766 cache_lock(&nd->nl_nch);
2767 cache_setunresolved(&nd->nl_nch);
2768 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2778 * stat_args(char *path, struct stat *ub)
2780 * Get file status; this version follows links.
2783 sys_stat(struct stat_args *uap)
2785 struct nlookupdata nd;
2789 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2791 error = kern_stat(&nd, &st);
2793 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2800 * lstat_args(char *path, struct stat *ub)
2802 * Get file status; this version does not follow links.
2805 sys_lstat(struct lstat_args *uap)
2807 struct nlookupdata nd;
2811 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2813 error = kern_stat(&nd, &st);
2815 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2822 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2824 * Get status of file pointed to by fd/path.
2827 sys_fstatat(struct fstatat_args *uap)
2829 struct nlookupdata nd;
2835 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2838 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2840 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2841 UIO_USERSPACE, flags);
2843 error = kern_stat(&nd, &st);
2845 error = copyout(&st, uap->sb, sizeof(*uap->sb));
2847 nlookup_done_at(&nd, fp);
2852 kern_pathconf(char *path, int name, int flags, register_t *sysmsg_regp)
2854 struct nlookupdata nd;
2859 error = nlookup_init(&nd, path, UIO_USERSPACE, flags);
2861 error = nlookup(&nd);
2863 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2866 error = VOP_PATHCONF(vp, name, sysmsg_regp);
2873 * pathconf_Args(char *path, int name)
2875 * Get configurable pathname variables.
2878 sys_pathconf(struct pathconf_args *uap)
2880 return (kern_pathconf(uap->path, uap->name, NLC_FOLLOW,
2885 * lpathconf_Args(char *path, int name)
2887 * Get configurable pathname variables, but don't follow symlinks.
2890 sys_lpathconf(struct lpathconf_args *uap)
2892 return (kern_pathconf(uap->path, uap->name, 0, &uap->sysmsg_reg));
2897 * kern_readlink isn't properly split yet. There is a copyin burried
2898 * in VOP_READLINK().
2901 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2903 struct thread *td = curthread;
2909 nd->nl_flags |= NLC_SHAREDLOCK;
2910 if ((error = nlookup(nd)) != 0)
2912 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
2915 if (vp->v_type != VLNK) {
2918 aiov.iov_base = buf;
2919 aiov.iov_len = count;
2920 auio.uio_iov = &aiov;
2921 auio.uio_iovcnt = 1;
2922 auio.uio_offset = 0;
2923 auio.uio_rw = UIO_READ;
2924 auio.uio_segflg = UIO_USERSPACE;
2926 auio.uio_resid = count;
2927 error = VOP_READLINK(vp, &auio, td->td_ucred);
2930 *res = count - auio.uio_resid;
2935 * readlink_args(char *path, char *buf, int count)
2937 * Return target name of a symbolic link.
2940 sys_readlink(struct readlink_args *uap)
2942 struct nlookupdata nd;
2945 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2947 error = kern_readlink(&nd, uap->buf, uap->count,
2948 &uap->sysmsg_result);
2955 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
2957 * Return target name of a symbolic link. The path is relative to the
2958 * directory associated with fd.
2961 sys_readlinkat(struct readlinkat_args *uap)
2963 struct nlookupdata nd;
2967 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2969 error = kern_readlink(&nd, uap->buf, uap->bufsize,
2970 &uap->sysmsg_result);
2972 nlookup_done_at(&nd, fp);
2977 setfflags(struct vnode *vp, int flags)
2979 struct thread *td = curthread;
2984 * Prevent non-root users from setting flags on devices. When
2985 * a device is reused, users can retain ownership of the device
2986 * if they are allowed to set flags and programs assume that
2987 * chown can't fail when done as root.
2989 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2990 ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
2994 * note: vget is required for any operation that might mod the vnode
2995 * so VINACTIVE is properly cleared.
2997 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2999 vattr.va_flags = flags;
3000 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3007 * chflags(char *path, int flags)
3009 * Change flags of a file given a path name.
3012 sys_chflags(struct chflags_args *uap)
3014 struct nlookupdata nd;
3019 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3021 error = nlookup(&nd);
3023 error = ncp_writechk(&nd.nl_nch);
3025 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3028 error = setfflags(vp, uap->flags);
3035 * lchflags(char *path, int flags)
3037 * Change flags of a file given a path name, but don't follow symlinks.
3040 sys_lchflags(struct lchflags_args *uap)
3042 struct nlookupdata nd;
3047 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3049 error = nlookup(&nd);
3051 error = ncp_writechk(&nd.nl_nch);
3053 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3056 error = setfflags(vp, uap->flags);
3063 * fchflags_args(int fd, int flags)
3065 * Change flags of a file given a file descriptor.
3068 sys_fchflags(struct fchflags_args *uap)
3070 struct thread *td = curthread;
3071 struct proc *p = td->td_proc;
3075 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3077 if (fp->f_nchandle.ncp)
3078 error = ncp_writechk(&fp->f_nchandle);
3080 error = setfflags((struct vnode *) fp->f_data, uap->flags);
3086 * chflagsat_args(int fd, const char *path, int flags, int atflags)
3087 * change flags given a pathname relative to a filedescriptor
3089 int sys_chflagsat(struct chflagsat_args *uap)
3091 struct nlookupdata nd;
3097 if (uap->atflags & ~AT_SYMLINK_NOFOLLOW)
3100 lookupflags = (uap->atflags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3103 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, lookupflags);
3105 error = nlookup(&nd);
3107 error = ncp_writechk(&nd.nl_nch);
3109 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3110 nlookup_done_at(&nd, fp);
3112 error = setfflags(vp, uap->flags);
3120 setfmode(struct vnode *vp, int mode)
3122 struct thread *td = curthread;
3127 * note: vget is required for any operation that might mod the vnode
3128 * so VINACTIVE is properly cleared.
3130 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3132 vattr.va_mode = mode & ALLPERMS;
3133 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3140 kern_chmod(struct nlookupdata *nd, int mode)
3145 if ((error = nlookup(nd)) != 0)
3147 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3149 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3150 error = setfmode(vp, mode);
3156 * chmod_args(char *path, int mode)
3158 * Change mode of a file given path name.
3161 sys_chmod(struct chmod_args *uap)
3163 struct nlookupdata nd;
3166 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3168 error = kern_chmod(&nd, uap->mode);
3174 * lchmod_args(char *path, int mode)
3176 * Change mode of a file given path name (don't follow links.)
3179 sys_lchmod(struct lchmod_args *uap)
3181 struct nlookupdata nd;
3184 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3186 error = kern_chmod(&nd, uap->mode);
3192 * fchmod_args(int fd, int mode)
3194 * Change mode of a file given a file descriptor.
3197 sys_fchmod(struct fchmod_args *uap)
3199 struct thread *td = curthread;
3200 struct proc *p = td->td_proc;
3204 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3206 if (fp->f_nchandle.ncp)
3207 error = ncp_writechk(&fp->f_nchandle);
3209 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3215 * fchmodat_args(char *path, int mode)
3217 * Change mode of a file pointed to by fd/path.
3220 sys_fchmodat(struct fchmodat_args *uap)
3222 struct nlookupdata nd;
3227 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3229 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3231 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3232 UIO_USERSPACE, flags);
3234 error = kern_chmod(&nd, uap->mode);
3235 nlookup_done_at(&nd, fp);
3240 setfown(struct mount *mp, struct vnode *vp, uid_t uid, gid_t gid)
3242 struct thread *td = curthread;
3250 * note: vget is required for any operation that might mod the vnode
3251 * so VINACTIVE is properly cleared.
3253 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3254 if ((error = VOP_GETATTR(vp, &vattr)) != 0)
3256 o_uid = vattr.va_uid;
3257 o_gid = vattr.va_gid;
3258 size = vattr.va_size;
3263 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3272 VFS_ACCOUNT(mp, o_uid, o_gid, -size);
3273 VFS_ACCOUNT(mp, uid, gid, size);
3280 kern_chown(struct nlookupdata *nd, int uid, int gid)
3285 if ((error = nlookup(nd)) != 0)
3287 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3289 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3290 error = setfown(nd->nl_nch.mount, vp, uid, gid);
3296 * chown(char *path, int uid, int gid)
3298 * Set ownership given a path name.
3301 sys_chown(struct chown_args *uap)
3303 struct nlookupdata nd;
3306 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3308 error = kern_chown(&nd, uap->uid, uap->gid);
3314 * lchown_args(char *path, int uid, int gid)
3316 * Set ownership given a path name, do not cross symlinks.
3319 sys_lchown(struct lchown_args *uap)
3321 struct nlookupdata nd;
3324 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3326 error = kern_chown(&nd, uap->uid, uap->gid);
3332 * fchown_args(int fd, int uid, int gid)
3334 * Set ownership given a file descriptor.
3337 sys_fchown(struct fchown_args *uap)
3339 struct thread *td = curthread;
3340 struct proc *p = td->td_proc;
3344 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3346 if (fp->f_nchandle.ncp)
3347 error = ncp_writechk(&fp->f_nchandle);
3349 error = setfown(p->p_fd->fd_ncdir.mount,
3350 (struct vnode *)fp->f_data, uap->uid, uap->gid);
3356 * fchownat(int fd, char *path, int uid, int gid, int flags)
3358 * Set ownership of file pointed to by fd/path.
3361 sys_fchownat(struct fchownat_args *uap)
3363 struct nlookupdata nd;
3368 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3370 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3372 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3373 UIO_USERSPACE, flags);
3375 error = kern_chown(&nd, uap->uid, uap->gid);
3376 nlookup_done_at(&nd, fp);
3382 getutimes(struct timeval *tvp, struct timespec *tsp)
3384 struct timeval tv[2];
3389 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3392 if ((error = itimerfix(tvp)) != 0)
3394 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3395 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3401 getutimens(const struct timespec *ts, struct timespec *newts, int *nullflag)
3403 struct timespec tsnow;
3417 if (newts[0].tv_nsec == UTIME_OMIT && newts[1].tv_nsec == UTIME_OMIT)
3419 if (newts[0].tv_nsec == UTIME_NOW && newts[1].tv_nsec == UTIME_NOW)
3422 if (newts[0].tv_nsec == UTIME_OMIT)
3423 newts[0].tv_sec = VNOVAL;
3424 else if (newts[0].tv_nsec == UTIME_NOW)
3426 else if ((error = itimespecfix(&newts[0])) != 0)
3429 if (newts[1].tv_nsec == UTIME_OMIT)
3430 newts[1].tv_sec = VNOVAL;
3431 else if (newts[1].tv_nsec == UTIME_NOW)
3433 else if ((error = itimespecfix(&newts[1])) != 0)
3440 setutimes(struct vnode *vp, struct vattr *vattr,
3441 const struct timespec *ts, int nullflag)
3443 struct thread *td = curthread;
3447 vattr->va_atime = ts[0];
3448 vattr->va_mtime = ts[1];
3450 vattr->va_vaflags |= VA_UTIMES_NULL;
3451 error = VOP_SETATTR(vp, vattr, td->td_ucred);
3457 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3459 struct timespec ts[2];
3463 if ((error = getutimes(tptr, ts)) != 0)
3466 error = kern_utimensat(nd, tptr ? ts : NULL, 0);
3471 * utimes_args(char *path, struct timeval *tptr)
3473 * Set the access and modification times of a file.
3476 sys_utimes(struct utimes_args *uap)
3478 struct timeval tv[2];
3479 struct nlookupdata nd;
3483 error = copyin(uap->tptr, tv, sizeof(tv));
3487 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3489 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3495 * lutimes_args(char *path, struct timeval *tptr)
3497 * Set the access and modification times of a file.
3500 sys_lutimes(struct lutimes_args *uap)
3502 struct timeval tv[2];
3503 struct nlookupdata nd;
3507 error = copyin(uap->tptr, tv, sizeof(tv));
3511 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3513 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3519 * Set utimes on a file descriptor. The creds used to open the
3520 * file are used to determine whether the operation is allowed
3524 kern_futimens(int fd, struct timespec *ts)
3526 struct thread *td = curthread;
3527 struct proc *p = td->td_proc;
3528 struct timespec newts[2];
3535 error = getutimens(ts, newts, &nullflag);
3538 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3540 if (fp->f_nchandle.ncp)
3541 error = ncp_writechk(&fp->f_nchandle);
3544 error = vget(vp, LK_EXCLUSIVE);
3546 error = VOP_GETATTR(vp, &vattr);
3548 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3552 error = setutimes(vp, &vattr, newts, nullflag);
3562 * futimens_args(int fd, struct timespec *ts)
3564 * Set the access and modification times of a file.
3567 sys_futimens(struct futimens_args *uap)
3569 struct timespec ts[2];
3573 error = copyin(uap->ts, ts, sizeof(ts));
3577 error = kern_futimens(uap->fd, uap->ts ? ts : NULL);
3582 kern_futimes(int fd, struct timeval *tptr)
3584 struct timespec ts[2];
3588 if ((error = getutimes(tptr, ts)) != 0)
3591 error = kern_futimens(fd, tptr ? ts : NULL);
3596 * futimes_args(int fd, struct timeval *tptr)
3598 * Set the access and modification times of a file.
3601 sys_futimes(struct futimes_args *uap)
3603 struct timeval tv[2];
3607 error = copyin(uap->tptr, tv, sizeof(tv));
3611 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3616 kern_utimensat(struct nlookupdata *nd, const struct timespec *ts, int flags)
3618 struct timespec newts[2];
3624 if (flags & ~AT_SYMLINK_NOFOLLOW)
3627 error = getutimens(ts, newts, &nullflag);
3631 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3632 if ((error = nlookup(nd)) != 0)
3634 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3636 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3638 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3639 error = vget(vp, LK_EXCLUSIVE);
3641 error = setutimes(vp, &vattr, newts, nullflag);
3650 * utimensat_args(int fd, const char *path, const struct timespec *ts, int flags);
3652 * Set file access and modification times of a file.
3655 sys_utimensat(struct utimensat_args *uap)
3657 struct timespec ts[2];
3658 struct nlookupdata nd;
3664 error = copyin(uap->ts, ts, sizeof(ts));
3669 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3670 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3671 UIO_USERSPACE, flags);
3673 error = kern_utimensat(&nd, uap->ts ? ts : NULL, uap->flags);
3674 nlookup_done_at(&nd, fp);
3679 kern_truncate(struct nlookupdata *nd, off_t length)
3686 uint64_t old_size = 0;
3690 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3691 if ((error = nlookup(nd)) != 0)
3693 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3695 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3697 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
3702 if (vp->v_type == VDIR) {
3706 if (vfs_quota_enabled) {
3707 error = VOP_GETATTR(vp, &vattr);
3708 KASSERT(error == 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3711 old_size = vattr.va_size;
3714 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3716 vattr.va_size = length;
3717 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3718 VFS_ACCOUNT(nd->nl_nch.mount, uid, gid, length - old_size);
3726 * truncate(char *path, int pad, off_t length)
3728 * Truncate a file given its path name.
3731 sys_truncate(struct truncate_args *uap)
3733 struct nlookupdata nd;
3736 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3738 error = kern_truncate(&nd, uap->length);
3744 kern_ftruncate(int fd, off_t length)
3746 struct thread *td = curthread;
3747 struct proc *p = td->td_proc;
3754 uint64_t old_size = 0;
3759 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3761 if (fp->f_nchandle.ncp) {
3762 error = ncp_writechk(&fp->f_nchandle);
3766 if ((fp->f_flag & FWRITE) == 0) {
3770 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3774 vp = (struct vnode *)fp->f_data;
3775 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3776 if (vp->v_type == VDIR) {
3782 if (vfs_quota_enabled) {
3783 error = VOP_GETATTR(vp, &vattr);
3784 KASSERT(error == 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
3787 old_size = vattr.va_size;
3790 if ((error = vn_writechk(vp, NULL)) == 0) {
3792 vattr.va_size = length;
3793 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3795 VFS_ACCOUNT(mp, uid, gid, length - old_size);
3804 * ftruncate_args(int fd, int pad, off_t length)
3806 * Truncate a file given a file descriptor.
3809 sys_ftruncate(struct ftruncate_args *uap)
3813 error = kern_ftruncate(uap->fd, uap->length);
3821 * Sync an open file.
3824 sys_fsync(struct fsync_args *uap)
3826 struct thread *td = curthread;
3827 struct proc *p = td->td_proc;
3833 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3835 vp = (struct vnode *)fp->f_data;
3836 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3837 if ((obj = vp->v_object) != NULL) {
3838 if (vp->v_mount == NULL ||
3839 (vp->v_mount->mnt_kern_flag & MNTK_NOMSYNC) == 0) {
3840 vm_object_page_clean(obj, 0, 0, 0);
3843 error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
3844 if (error == 0 && vp->v_mount)
3845 error = buf_fsync(vp);
3853 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3855 struct nchandle fnchd;
3856 struct nchandle tnchd;
3857 struct namecache *ncp;
3866 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3867 if ((error = nlookup(fromnd)) != 0)
3869 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3871 fnchd.mount = fromnd->nl_nch.mount;
3875 * unlock the source nch so we can lookup the target nch without
3876 * deadlocking. The target may or may not exist so we do not check
3877 * for a target vp like kern_mkdir() and other creation functions do.
3879 * The source and target directories are ref'd and rechecked after
3880 * everything is relocked to determine if the source or target file
3883 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3884 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3886 fncp_gen = fromnd->nl_nch.ncp->nc_generation;
3888 cache_unlock(&fromnd->nl_nch);
3890 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3891 if ((error = nlookup(tond)) != 0) {
3895 tncp_gen = tond->nl_nch.ncp->nc_generation;
3897 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3901 tnchd.mount = tond->nl_nch.mount;
3905 * If the source and target are the same there is nothing to do
3907 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3914 * Mount points cannot be renamed or overwritten
3916 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3925 * Relock the source ncp. cache_relock() will deal with any
3926 * deadlocks against the already-locked tond and will also
3927 * make sure both are resolved.
3929 * NOTE AFTER RELOCKING: The source or target ncp may have become
3930 * invalid while they were unlocked, nc_vp and nc_mount could
3933 cache_relock(&fromnd->nl_nch, fromnd->nl_cred,
3934 &tond->nl_nch, tond->nl_cred);
3935 fromnd->nl_flags |= NLC_NCPISLOCKED;
3938 * If the namecache generation changed for either fromnd or tond,
3941 if (fromnd->nl_nch.ncp->nc_generation != fncp_gen ||
3942 tond->nl_nch.ncp->nc_generation != tncp_gen) {
3943 kprintf("kern_rename: retry due to gen on: "
3944 "\"%s\" -> \"%s\"\n",
3945 fromnd->nl_nch.ncp->nc_name,
3946 tond->nl_nch.ncp->nc_name);
3953 * If either fromnd or tond are marked destroyed a ripout occured
3954 * out from under us and we must retry.
3956 if ((fromnd->nl_nch.ncp->nc_flag & (NCF_DESTROYED | NCF_UNRESOLVED)) ||
3957 fromnd->nl_nch.ncp->nc_vp == NULL ||
3958 (tond->nl_nch.ncp->nc_flag & NCF_DESTROYED)) {
3959 kprintf("kern_rename: retry due to ripout on: "
3960 "\"%s\" -> \"%s\"\n",
3961 fromnd->nl_nch.ncp->nc_name,
3962 tond->nl_nch.ncp->nc_name);
3969 * Make sure the parent directories linkages are the same.
3970 * XXX shouldn't be needed any more w/ generation check above.
3972 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3973 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3980 * Both the source and target must be within the same filesystem and
3981 * in the same filesystem as their parent directories within the
3982 * namecache topology.
3984 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3987 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3988 mp != tond->nl_nch.mount) {
3995 * Make sure the mount point is writable
3997 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
4004 * If the target exists and either the source or target is a directory,
4005 * then both must be directories.
4007 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
4010 if (tond->nl_nch.ncp->nc_vp) {
4011 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
4013 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
4014 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
4016 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
4022 * You cannot rename a source into itself or a subdirectory of itself.
4023 * We check this by travsersing the target directory upwards looking
4024 * for a match against the source.
4029 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
4030 if (fromnd->nl_nch.ncp == ncp) {
4041 * Even though the namespaces are different, they may still represent
4042 * hardlinks to the same file. The filesystem might have a hard time
4043 * with this so we issue a NREMOVE of the source instead of a NRENAME
4044 * when we detect the situation.
4047 fdvp = fromnd->nl_dvp;
4048 tdvp = tond->nl_dvp;
4049 if (fdvp == NULL || tdvp == NULL) {
4051 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
4052 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
4055 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
4056 fdvp, tdvp, tond->nl_cred);
4063 * rename_args(char *from, char *to)
4065 * Rename files. Source and destination must either both be directories,
4066 * or both not be directories. If target is a directory, it must be empty.
4069 sys_rename(struct rename_args *uap)
4071 struct nlookupdata fromnd, tond;
4075 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
4077 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
4079 error = kern_rename(&fromnd, &tond);
4080 nlookup_done(&tond);
4082 nlookup_done(&fromnd);
4083 } while (error == EAGAIN);
4088 * renameat_args(int oldfd, char *old, int newfd, char *new)
4090 * Rename files using paths relative to the directories associated with
4091 * oldfd and newfd. Source and destination must either both be directories,
4092 * or both not be directories. If target is a directory, it must be empty.
4095 sys_renameat(struct renameat_args *uap)
4097 struct nlookupdata oldnd, newnd;
4098 struct file *oldfp, *newfp;
4102 error = nlookup_init_at(&oldnd, &oldfp,
4103 uap->oldfd, uap->old,
4106 error = nlookup_init_at(&newnd, &newfp,
4107 uap->newfd, uap->new,
4110 error = kern_rename(&oldnd, &newnd);
4111 nlookup_done_at(&newnd, newfp);
4113 nlookup_done_at(&oldnd, oldfp);
4114 } while (error == EAGAIN);
4119 kern_mkdir(struct nlookupdata *nd, int mode)
4121 struct thread *td = curthread;
4122 struct proc *p = td->td_proc;
4128 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
4129 if ((error = nlookup(nd)) != 0)
4132 if (nd->nl_nch.ncp->nc_vp)
4134 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4137 vattr.va_type = VDIR;
4138 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
4141 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
4148 * mkdir_args(char *path, int mode)
4150 * Make a directory file.
4153 sys_mkdir(struct mkdir_args *uap)
4155 struct nlookupdata nd;
4158 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4160 error = kern_mkdir(&nd, uap->mode);
4166 * mkdirat_args(int fd, char *path, mode_t mode)
4168 * Make a directory file. The path is relative to the directory associated
4172 sys_mkdirat(struct mkdirat_args *uap)
4174 struct nlookupdata nd;
4178 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
4180 error = kern_mkdir(&nd, uap->mode);
4181 nlookup_done_at(&nd, fp);
4186 kern_rmdir(struct nlookupdata *nd)
4191 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
4192 if ((error = nlookup(nd)) != 0)
4196 * Do not allow directories representing mount points to be
4197 * deleted, even if empty. Check write perms on mount point
4198 * in case the vnode is aliased (aka nullfs).
4200 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
4202 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4204 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
4209 * rmdir_args(char *path)
4211 * Remove a directory file.
4214 sys_rmdir(struct rmdir_args *uap)
4216 struct nlookupdata nd;
4219 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4221 error = kern_rmdir(&nd);
4227 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
4228 enum uio_seg direction)
4230 struct thread *td = curthread;
4231 struct proc *p = td->td_proc;
4239 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
4241 if ((fp->f_flag & FREAD) == 0) {
4245 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;
4264 if (count == auio.uio_resid) {
4265 if (union_dircheckp) {
4266 error = union_dircheckp(td, &vp, fp);
4273 if ((vp->v_flag & VROOT) &&
4274 (vp->v_mount->mnt_flag & MNT_UNION)) {
4275 struct vnode *tvp = vp;
4276 vp = vp->v_mount->mnt_vnodecovered;
4287 * WARNING! *basep may not be wide enough to accomodate the
4288 * seek offset. XXX should we hack this to return the upper 32 bits
4289 * for offsets greater then 4G?
4292 *basep = (long)loff;
4294 *res = count - auio.uio_resid;
4301 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4303 * Read a block of directory entries in a file system independent format.
4306 sys_getdirentries(struct getdirentries_args *uap)
4311 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
4312 &uap->sysmsg_result, UIO_USERSPACE);
4314 if (error == 0 && uap->basep)
4315 error = copyout(&base, uap->basep, sizeof(*uap->basep));
4320 * getdents_args(int fd, char *buf, size_t count)
4323 sys_getdents(struct getdents_args *uap)
4327 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
4328 &uap->sysmsg_result, UIO_USERSPACE);
4334 * Set the mode mask for creation of filesystem nodes.
4336 * umask(int newmask)
4339 sys_umask(struct umask_args *uap)
4341 struct thread *td = curthread;
4342 struct proc *p = td->td_proc;
4343 struct filedesc *fdp;
4346 uap->sysmsg_result = fdp->fd_cmask;
4347 fdp->fd_cmask = uap->newmask & ALLPERMS;
4352 * revoke(char *path)
4354 * Void all references to file by ripping underlying filesystem
4358 sys_revoke(struct revoke_args *uap)
4360 struct nlookupdata nd;
4367 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4369 error = nlookup(&nd);
4371 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4372 cred = crhold(nd.nl_cred);
4376 error = VOP_GETATTR(vp, &vattr);
4377 if (error == 0 && cred->cr_uid != vattr.va_uid)
4378 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4379 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4381 error = vrevoke(vp, cred);
4382 } else if (error == 0) {
4383 error = vrevoke(vp, cred);
4393 * getfh_args(char *fname, fhandle_t *fhp)
4395 * Get (NFS) file handle
4397 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4398 * mount. This allows nullfs mounts to be explicitly exported.
4400 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4402 * nullfs mounts of subdirectories are not safe. That is, it will
4403 * work, but you do not really have protection against access to
4404 * the related parent directories.
4407 sys_getfh(struct getfh_args *uap)
4409 struct thread *td = curthread;
4410 struct nlookupdata nd;
4417 * Must be super user
4419 if ((error = priv_check(td, PRIV_ROOT)) != 0)
4423 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4425 error = nlookup(&nd);
4427 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4428 mp = nd.nl_nch.mount;
4431 bzero(&fh, sizeof(fh));
4432 fh.fh_fsid = mp->mnt_stat.f_fsid;
4433 error = VFS_VPTOFH(vp, &fh.fh_fid);
4436 error = copyout(&fh, uap->fhp, sizeof(fh));
4442 * fhopen_args(const struct fhandle *u_fhp, int flags)
4444 * syscall for the rpc.lockd to use to translate a NFS file handle into
4445 * an open descriptor.
4447 * warning: do not remove the priv_check() call or this becomes one giant
4451 sys_fhopen(struct fhopen_args *uap)
4453 struct thread *td = curthread;
4454 struct filedesc *fdp = td->td_proc->p_fd;
4459 struct vattr *vap = &vat;
4461 int fmode, mode, error = 0, type;
4467 * Must be super user
4469 error = priv_check(td, PRIV_ROOT);
4473 fmode = FFLAGS(uap->flags);
4476 * Why not allow a non-read/write open for our lockd?
4478 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4480 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4485 * Find the mount point
4487 mp = vfs_getvfs(&fhp.fh_fsid);
4492 /* now give me my vnode, it gets returned to me locked */
4493 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4497 * from now on we have to make sure not
4498 * to forget about the vnode
4499 * any error that causes an abort must vput(vp)
4500 * just set error = err and 'goto bad;'.
4506 if (vp->v_type == VLNK) {
4510 if (vp->v_type == VSOCK) {
4515 if (fmode & (FWRITE | O_TRUNC)) {
4516 if (vp->v_type == VDIR) {
4520 error = vn_writechk(vp, NULL);
4528 error = VOP_ACCESS(vp, mode, td->td_ucred);
4532 if (fmode & O_TRUNC) {
4533 vn_unlock(vp); /* XXX */
4534 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4537 error = VOP_SETATTR(vp, vap, td->td_ucred);
4543 * VOP_OPEN needs the file pointer so it can potentially override
4546 * WARNING! no f_nchandle will be associated when fhopen()ing a
4549 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4553 error = VOP_OPEN(vp, fmode, td->td_ucred, fp);
4556 * setting f_ops this way prevents VOP_CLOSE from being
4557 * called or fdrop() releasing the vp from v_data. Since
4558 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4560 fp->f_ops = &badfileops;
4566 * The fp is given its own reference, we still have our ref and lock.
4568 * Assert that all regular files must be created with a VM object.
4570 if (vp->v_type == VREG && vp->v_object == NULL) {
4571 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
4576 * The open was successful. Handle any locking requirements.
4578 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4579 lf.l_whence = SEEK_SET;
4582 if (fmode & O_EXLOCK)
4583 lf.l_type = F_WRLCK;
4585 lf.l_type = F_RDLCK;
4586 if (fmode & FNONBLOCK)
4591 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
4593 * release our private reference.
4595 fsetfd(fdp, NULL, indx);
4600 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4601 atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */
4605 * Clean up. Associate the file pointer with the previously
4606 * reserved descriptor and return it.
4609 if (uap->flags & O_CLOEXEC)
4610 fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
4611 fsetfd(fdp, fp, indx);
4613 uap->sysmsg_result = indx;
4617 fsetfd(fdp, NULL, indx);
4626 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4629 sys_fhstat(struct fhstat_args *uap)
4631 struct thread *td = curthread;
4639 * Must be super user
4641 error = priv_check(td, PRIV_ROOT);
4645 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4649 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4652 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4653 error = vn_stat(vp, &sb, td->td_ucred);
4658 error = copyout(&sb, uap->sb, sizeof(sb));
4663 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4666 sys_fhstatfs(struct fhstatfs_args *uap)
4668 struct thread *td = curthread;
4669 struct proc *p = td->td_proc;
4674 char *fullpath, *freepath;
4679 * Must be super user
4681 if ((error = priv_check(td, PRIV_ROOT)))
4684 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4687 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4691 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4696 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4701 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4704 error = mount_path(p, mp, &fullpath, &freepath);
4707 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4708 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4709 kfree(freepath, M_TEMP);
4711 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4712 if (priv_check(td, PRIV_ROOT)) {
4713 bcopy(sp, &sb, sizeof(sb));
4714 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4717 error = copyout(sp, uap->buf, sizeof(*sp));
4723 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4726 sys_fhstatvfs(struct fhstatvfs_args *uap)
4728 struct thread *td = curthread;
4729 struct proc *p = td->td_proc;
4737 * Must be super user
4739 if ((error = priv_check(td, PRIV_ROOT)))
4742 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4745 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4749 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4754 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4757 sp = &mp->mnt_vstat;
4759 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
4763 if (mp->mnt_flag & MNT_RDONLY)
4764 sp->f_flag |= ST_RDONLY;
4765 if (mp->mnt_flag & MNT_NOSUID)
4766 sp->f_flag |= ST_NOSUID;
4767 error = copyout(sp, uap->buf, sizeof(*sp));
4774 * Syscall to push extended attribute configuration information into the
4775 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4776 * a command (int cmd), and attribute name and misc data. For now, the
4777 * attribute name is left in userspace for consumption by the VFS_op.
4778 * It will probably be changed to be copied into sysspace by the
4779 * syscall in the future, once issues with various consumers of the
4780 * attribute code have raised their hands.
4782 * Currently this is used only by UFS Extended Attributes.
4785 sys_extattrctl(struct extattrctl_args *uap)
4787 struct nlookupdata nd;
4789 char attrname[EXTATTR_MAXNAMELEN];
4797 if (error == 0 && uap->filename) {
4798 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
4801 error = nlookup(&nd);
4803 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4807 if (error == 0 && uap->attrname) {
4808 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4813 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4815 error = nlookup(&nd);
4817 error = ncp_writechk(&nd.nl_nch);
4819 error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
4821 uap->attrname, nd.nl_cred);
4830 * Syscall to get a named extended attribute on a file or directory.
4833 sys_extattr_set_file(struct extattr_set_file_args *uap)
4835 char attrname[EXTATTR_MAXNAMELEN];
4836 struct nlookupdata nd;
4842 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4848 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4850 error = nlookup(&nd);
4852 error = ncp_writechk(&nd.nl_nch);
4854 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4860 bzero(&auio, sizeof(auio));
4861 aiov.iov_base = uap->data;
4862 aiov.iov_len = uap->nbytes;
4863 auio.uio_iov = &aiov;
4864 auio.uio_iovcnt = 1;
4865 auio.uio_offset = 0;
4866 auio.uio_resid = uap->nbytes;
4867 auio.uio_rw = UIO_WRITE;
4868 auio.uio_td = curthread;
4870 error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
4879 * Syscall to get a named extended attribute on a file or directory.
4882 sys_extattr_get_file(struct extattr_get_file_args *uap)
4884 char attrname[EXTATTR_MAXNAMELEN];
4885 struct nlookupdata nd;
4891 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4897 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4899 error = nlookup(&nd);
4901 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_SHARED, &vp);
4907 bzero(&auio, sizeof(auio));
4908 aiov.iov_base = uap->data;
4909 aiov.iov_len = uap->nbytes;
4910 auio.uio_iov = &aiov;
4911 auio.uio_iovcnt = 1;
4912 auio.uio_offset = 0;
4913 auio.uio_resid = uap->nbytes;
4914 auio.uio_rw = UIO_READ;
4915 auio.uio_td = curthread;
4917 error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
4919 uap->sysmsg_result = uap->nbytes - auio.uio_resid;
4927 * Syscall to delete a named extended attribute from a file or directory.
4928 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4931 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4933 char attrname[EXTATTR_MAXNAMELEN];
4934 struct nlookupdata nd;
4938 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4942 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4944 error = nlookup(&nd);
4946 error = ncp_writechk(&nd.nl_nch);
4948 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4950 error = VOP_SETEXTATTR(vp, uap->attrnamespace,
4951 attrname, NULL, nd.nl_cred);
4960 * Determine if the mount is visible to the process.
4963 chroot_visible_mnt(struct mount *mp, struct proc *p)
4965 struct nchandle nch;
4968 * Traverse from the mount point upwards. If we hit the process
4969 * root then the mount point is visible to the process.
4971 nch = mp->mnt_ncmountpt;
4973 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4974 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4977 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4978 nch = nch.mount->mnt_ncmounton;
4980 nch.ncp = nch.ncp->nc_parent;
4985 * If the mount point is not visible to the process, but the
4986 * process root is in a subdirectory of the mount, return
4989 if (p->p_fd->fd_nrdir.mount == mp)