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 (const struct timeval *, struct timespec *);
89 static int setfown (struct mount *, struct vnode *, uid_t, gid_t);
90 static int setfmode (struct vnode *, int);
91 static int setfflags (struct vnode *, int);
92 static int setutimes (struct vnode *, struct vattr *,
93 const struct timespec *, int);
94 static int usermount = 0; /* if 1, non-root can mount fs. */
96 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
98 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
99 "Allow non-root users to mount filesystems");
102 * Virtual File System System Calls
106 * Mount a file system.
108 * mount_args(char *type, char *path, int flags, caddr_t data)
113 sys_mount(struct mount_args *uap)
115 struct thread *td = curthread;
118 struct mount *mp, *nullmp;
119 struct vfsconf *vfsp;
120 int error, flag = 0, flag2 = 0;
123 struct nlookupdata nd;
124 char fstypename[MFSNAMELEN];
132 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
136 * Do not allow NFS export by non-root users.
138 if (uap->flags & MNT_EXPORTED) {
139 error = priv_check(td, PRIV_ROOT);
144 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
146 if (priv_check(td, PRIV_ROOT))
147 uap->flags |= MNT_NOSUID | MNT_NODEV;
150 * Lookup the requested path and extract the nch and vnode.
152 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
154 if ((error = nlookup(&nd)) == 0) {
155 if (nd.nl_nch.ncp->nc_vp == NULL)
165 * If the target filesystem is resolved via a nullfs mount, then
166 * nd.nl_nch.mount will be pointing to the nullfs mount structure
167 * instead of the target file system. We need it in case we are
170 nullmp = nd.nl_nch.mount;
173 * Extract the locked+refd ncp and cleanup the nd structure
176 cache_zero(&nd.nl_nch);
179 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
180 (mp = cache_findmount(&nch)) != NULL) {
189 * now we have the locked ref'd nch and unreferenced vnode.
192 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
199 * Extract the file system type. We need to know this early, to take
200 * appropriate actions if we are dealing with a nullfs.
202 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
209 * Now we have an unlocked ref'd nch and a locked ref'd vp
211 if (uap->flags & MNT_UPDATE) {
212 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
219 if (strncmp(fstypename, "null", 5) == 0) {
227 flag2 = mp->mnt_kern_flag;
229 * We only allow the filesystem to be reloaded if it
230 * is currently mounted read-only.
232 if ((uap->flags & MNT_RELOAD) &&
233 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
236 error = EOPNOTSUPP; /* Needs translation */
240 * Only root, or the user that did the original mount is
241 * permitted to update it.
243 if (mp->mnt_stat.f_owner != cred->cr_uid &&
244 (error = priv_check(td, PRIV_ROOT))) {
249 if (vfs_busy(mp, LK_NOWAIT)) {
263 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
264 lwkt_gettoken(&mp->mnt_token);
270 * If the user is not root, ensure that they own the directory
271 * onto which we are attempting to mount.
273 if ((error = VOP_GETATTR(vp, &va)) ||
274 (va.va_uid != cred->cr_uid &&
275 (error = priv_check(td, PRIV_ROOT)))) {
280 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
285 if (vp->v_type != VDIR) {
291 if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
297 vfsp = vfsconf_find_by_name(fstypename);
301 /* Only load modules for root (very important!) */
302 if ((error = priv_check(td, PRIV_ROOT)) != 0) {
307 error = linker_load_file(fstypename, &lf);
308 if (error || lf == NULL) {
316 /* lookup again, see if the VFS was loaded */
317 vfsp = vfsconf_find_by_name(fstypename);
320 linker_file_unload(lf);
335 * Allocate and initialize the filesystem.
337 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
339 vfs_busy(mp, LK_NOWAIT);
340 mp->mnt_op = vfsp->vfc_vfsops;
342 vfsp->vfc_refcount++;
343 mp->mnt_stat.f_type = vfsp->vfc_typenum;
344 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
345 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
346 mp->mnt_stat.f_owner = cred->cr_uid;
347 lwkt_gettoken(&mp->mnt_token);
351 * (per-mount token acquired at this point)
353 * Set the mount level flags.
355 if (uap->flags & MNT_RDONLY)
356 mp->mnt_flag |= MNT_RDONLY;
357 else if (mp->mnt_flag & MNT_RDONLY)
358 mp->mnt_kern_flag |= MNTK_WANTRDWR;
359 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
360 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
361 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
362 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
363 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
364 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
365 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
366 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
368 * Mount the filesystem.
369 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
372 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
373 if (mp->mnt_flag & MNT_UPDATE) {
374 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
375 mp->mnt_flag &= ~MNT_RDONLY;
376 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
377 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
380 mp->mnt_kern_flag = flag2;
382 lwkt_reltoken(&mp->mnt_token);
388 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
391 * Put the new filesystem on the mount list after root. The mount
392 * point gets its own mnt_ncmountpt (unless the VFS already set one
393 * up) which represents the root of the mount. The lookup code
394 * detects the mount point going forward and checks the root of
395 * the mount going backwards.
397 * It is not necessary to invalidate or purge the vnode underneath
398 * because elements under the mount will be given their own glue
402 if (mp->mnt_ncmountpt.ncp == NULL) {
404 * allocate, then unlock, but leave the ref intact
406 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
407 cache_unlock(&mp->mnt_ncmountpt);
409 mp->mnt_ncmounton = nch; /* inherits ref */
410 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
411 cache_ismounting(mp);
413 mountlist_insert(mp, MNTINS_LAST);
415 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
416 error = vfs_allocate_syncvnode(mp);
417 lwkt_reltoken(&mp->mnt_token);
419 error = VFS_START(mp, 0);
422 vn_syncer_thr_stop(mp);
423 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
424 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
425 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
426 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
427 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
428 mp->mnt_vfc->vfc_refcount--;
429 lwkt_reltoken(&mp->mnt_token);
440 * Scan all active processes to see if any of them have a current
441 * or root directory onto which the new filesystem has just been
442 * mounted. If so, replace them with the new mount point.
444 * Both old_nch and new_nch are ref'd on call but not locked.
445 * new_nch must be temporarily locked so it can be associated with the
446 * vnode representing the root of the mount point.
448 struct checkdirs_info {
449 struct nchandle old_nch;
450 struct nchandle new_nch;
451 struct vnode *old_vp;
452 struct vnode *new_vp;
455 static int checkdirs_callback(struct proc *p, void *data);
458 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
460 struct checkdirs_info info;
466 * If the old mount point's vnode has a usecount of 1, it is not
467 * being held as a descriptor anywhere.
469 olddp = old_nch->ncp->nc_vp;
470 if (olddp == NULL || VREFCNT(olddp) == 1)
474 * Force the root vnode of the new mount point to be resolved
475 * so we can update any matching processes.
478 if (VFS_ROOT(mp, &newdp))
479 panic("mount: lost mount");
482 vn_lock(newdp, LK_EXCLUSIVE | LK_RETRY);
483 cache_setunresolved(new_nch);
484 cache_setvp(new_nch, newdp);
485 cache_unlock(new_nch);
488 * Special handling of the root node
490 if (rootvnode == olddp) {
492 vfs_cache_setroot(newdp, cache_hold(new_nch));
496 * Pass newdp separately so the callback does not have to access
497 * it via new_nch->ncp->nc_vp.
499 info.old_nch = *old_nch;
500 info.new_nch = *new_nch;
502 allproc_scan(checkdirs_callback, &info);
507 * NOTE: callback is not MP safe because the scanned process's filedesc
508 * structure can be ripped out from under us, amoung other things.
511 checkdirs_callback(struct proc *p, void *data)
513 struct checkdirs_info *info = data;
514 struct filedesc *fdp;
515 struct nchandle ncdrop1;
516 struct nchandle ncdrop2;
517 struct vnode *vprele1;
518 struct vnode *vprele2;
520 if ((fdp = p->p_fd) != NULL) {
521 cache_zero(&ncdrop1);
522 cache_zero(&ncdrop2);
527 * MPUNSAFE - XXX fdp can be pulled out from under a
530 * A shared filedesc is ok, we don't have to copy it
531 * because we are making this change globally.
533 spin_lock(&fdp->fd_spin);
534 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
535 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
536 vprele1 = fdp->fd_cdir;
538 fdp->fd_cdir = info->new_vp;
539 ncdrop1 = fdp->fd_ncdir;
540 cache_copy(&info->new_nch, &fdp->fd_ncdir);
542 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
543 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
544 vprele2 = fdp->fd_rdir;
546 fdp->fd_rdir = info->new_vp;
547 ncdrop2 = fdp->fd_nrdir;
548 cache_copy(&info->new_nch, &fdp->fd_nrdir);
550 spin_unlock(&fdp->fd_spin);
552 cache_drop(&ncdrop1);
554 cache_drop(&ncdrop2);
564 * Unmount a file system.
566 * Note: unmount takes a path to the vnode mounted on as argument,
567 * not special file (as before).
569 * umount_args(char *path, int flags)
574 sys_unmount(struct unmount_args *uap)
576 struct thread *td = curthread;
577 struct proc *p __debugvar = td->td_proc;
578 struct mount *mp = NULL;
579 struct nlookupdata nd;
584 if (td->td_ucred->cr_prison != NULL) {
588 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
591 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
593 error = nlookup(&nd);
597 mp = nd.nl_nch.mount;
600 * Only root, or the user that did the original mount is
601 * permitted to unmount this filesystem.
603 if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) &&
604 (error = priv_check(td, PRIV_ROOT)))
608 * Don't allow unmounting the root file system.
610 if (mp->mnt_flag & MNT_ROOTFS) {
616 * Must be the root of the filesystem
618 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
626 error = dounmount(mp, uap->flags);
633 * Do the actual file system unmount.
636 dounmount_interlock(struct mount *mp)
638 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
640 mp->mnt_kern_flag |= MNTK_UNMOUNT;
645 unmount_allproc_cb(struct proc *p, void *arg)
649 if (p->p_textnch.ncp == NULL)
652 mp = (struct mount *)arg;
653 if (p->p_textnch.mount == mp)
654 cache_drop(&p->p_textnch);
660 dounmount(struct mount *mp, int flags)
662 struct namecache *ncp;
671 lwkt_gettoken(&mp->mnt_token);
673 * Exclusive access for unmounting purposes
675 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
679 * Allow filesystems to detect that a forced unmount is in progress.
681 if (flags & MNT_FORCE)
682 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
683 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_TIMELOCK);
684 error = lockmgr(&mp->mnt_lock, lflags);
686 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
687 if (mp->mnt_kern_flag & MNTK_MWAIT) {
688 mp->mnt_kern_flag &= ~MNTK_MWAIT;
694 if (mp->mnt_flag & MNT_EXPUBLIC)
695 vfs_setpublicfs(NULL, NULL, NULL);
697 vfs_msync(mp, MNT_WAIT);
698 async_flag = mp->mnt_flag & MNT_ASYNC;
699 mp->mnt_flag &=~ MNT_ASYNC;
702 * If this filesystem isn't aliasing other filesystems,
703 * try to invalidate any remaining namecache entries and
704 * check the count afterwords.
706 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
707 cache_lock(&mp->mnt_ncmountpt);
708 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
709 cache_unlock(&mp->mnt_ncmountpt);
711 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
712 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
713 allproc_scan(&unmount_allproc_cb, mp);
716 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
717 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
719 if ((flags & MNT_FORCE) == 0) {
721 mount_warning(mp, "Cannot unmount: "
727 mount_warning(mp, "Forced unmount: "
738 * Decomission our special mnt_syncer vnode. This also stops
739 * the vnlru code. If we are unable to unmount we recommission
742 * Then sync the filesystem.
744 if ((vp = mp->mnt_syncer) != NULL) {
745 mp->mnt_syncer = NULL;
746 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
749 if ((mp->mnt_flag & MNT_RDONLY) == 0)
750 VFS_SYNC(mp, MNT_WAIT);
753 * nchandle records ref the mount structure. Expect a count of 1
754 * (our mount->mnt_ncmountpt).
756 * Scans can get temporary refs on a mountpoint (thought really
757 * heavy duty stuff like cache_findmount() do not).
759 for (retry = 0; retry < 10 && mp->mnt_refs != 1; ++retry) {
760 cache_unmounting(mp);
761 tsleep(&mp->mnt_refs, 0, "mntbsy", hz / 10 + 1);
763 if (mp->mnt_refs != 1) {
764 if ((flags & MNT_FORCE) == 0) {
765 mount_warning(mp, "Cannot unmount: "
766 "%d mount refs still present",
770 mount_warning(mp, "Forced unmount: "
771 "%d mount refs still present",
778 * So far so good, sync the filesystem once more and
779 * call the VFS unmount code if the sync succeeds.
782 if (((mp->mnt_flag & MNT_RDONLY) ||
783 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
784 (flags & MNT_FORCE)) {
785 error = VFS_UNMOUNT(mp, flags);
790 * If an error occurred we can still recover, restoring the
791 * syncer vnode and misc flags.
794 if (mp->mnt_syncer == NULL)
795 vfs_allocate_syncvnode(mp);
796 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
797 mp->mnt_flag |= async_flag;
798 lockmgr(&mp->mnt_lock, LK_RELEASE);
799 if (mp->mnt_kern_flag & MNTK_MWAIT) {
800 mp->mnt_kern_flag &= ~MNTK_MWAIT;
806 * Clean up any journals still associated with the mount after
807 * filesystem activity has ceased.
809 journal_remove_all_journals(mp,
810 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
812 mountlist_remove(mp);
815 * Remove any installed vnode ops here so the individual VFSs don't
818 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
819 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
820 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
821 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
822 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
824 if (mp->mnt_ncmountpt.ncp != NULL) {
825 nch = mp->mnt_ncmountpt;
826 cache_zero(&mp->mnt_ncmountpt);
827 cache_clrmountpt(&nch);
830 if (mp->mnt_ncmounton.ncp != NULL) {
831 cache_unmounting(mp);
832 nch = mp->mnt_ncmounton;
833 cache_zero(&mp->mnt_ncmounton);
834 cache_clrmountpt(&nch);
838 mp->mnt_vfc->vfc_refcount--;
839 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
840 panic("unmount: dangling vnode");
841 lockmgr(&mp->mnt_lock, LK_RELEASE);
842 if (mp->mnt_kern_flag & MNTK_MWAIT) {
843 mp->mnt_kern_flag &= ~MNTK_MWAIT;
848 * If we reach here and freeok != 0 we must free the mount.
849 * If refs > 1 cycle and wait, just in case someone tried
850 * to busy the mount after we decided to do the unmount.
853 while (mp->mnt_refs > 1) {
854 cache_unmounting(mp);
856 tsleep(&mp->mnt_refs, 0, "umntrwait", hz / 10 + 1);
858 lwkt_reltoken(&mp->mnt_token);
865 lwkt_reltoken(&mp->mnt_token);
871 mount_warning(struct mount *mp, const char *ctl, ...)
878 if (cache_fullpath(NULL, &mp->mnt_ncmounton, NULL,
879 &ptr, &buf, 0) == 0) {
880 kprintf("unmount(%s): ", ptr);
885 kprintf("unmount(%p", mp);
886 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
887 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
896 * Shim cache_fullpath() to handle the case where a process is chrooted into
897 * a subdirectory of a mount. In this case if the root mount matches the
898 * process root directory's mount we have to specify the process's root
899 * directory instead of the mount point, because the mount point might
900 * be above the root directory.
904 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
906 struct nchandle *nch;
908 if (p && p->p_fd->fd_nrdir.mount == mp)
909 nch = &p->p_fd->fd_nrdir;
911 nch = &mp->mnt_ncmountpt;
912 return(cache_fullpath(p, nch, NULL, rb, fb, 0));
916 * Sync each mounted filesystem.
920 static int syncprt = 0;
921 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
924 static int sync_callback(struct mount *mp, void *data);
927 sys_sync(struct sync_args *uap)
929 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
935 sync_callback(struct mount *mp, void *data __unused)
939 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
940 asyncflag = mp->mnt_flag & MNT_ASYNC;
941 mp->mnt_flag &= ~MNT_ASYNC;
942 vfs_msync(mp, MNT_NOWAIT);
943 VFS_SYNC(mp, MNT_NOWAIT);
944 mp->mnt_flag |= asyncflag;
949 /* XXX PRISON: could be per prison flag */
950 static int prison_quotas;
952 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
956 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
958 * Change filesystem quotas.
963 sys_quotactl(struct quotactl_args *uap)
965 struct nlookupdata nd;
972 if (td->td_ucred->cr_prison && !prison_quotas) {
977 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
979 error = nlookup(&nd);
981 mp = nd.nl_nch.mount;
982 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
983 uap->arg, nd.nl_cred);
992 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
993 * void *buf, int buflen)
995 * This function operates on a mount point and executes the specified
996 * operation using the specified control data, and possibly returns data.
998 * The actual number of bytes stored in the result buffer is returned, 0
999 * if none, otherwise an error is returned.
1004 sys_mountctl(struct mountctl_args *uap)
1006 struct thread *td = curthread;
1007 struct proc *p = td->td_proc;
1015 * Sanity and permissions checks. We must be root.
1018 if (td->td_ucred->cr_prison != NULL)
1020 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
1021 (error = priv_check(td, PRIV_ROOT)) != 0)
1025 * Argument length checks
1027 if (uap->ctllen < 0 || uap->ctllen > 1024)
1029 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
1031 if (uap->path == NULL)
1035 * Allocate the necessary buffers and copyin data
1037 path = objcache_get(namei_oc, M_WAITOK);
1038 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1043 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
1044 error = copyin(uap->ctl, ctl, uap->ctllen);
1049 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
1052 * Validate the descriptor
1055 fp = holdfp(p->p_fd, uap->fd, -1);
1065 * Execute the internal kernel function and clean up.
1068 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
1072 if (error == 0 && uap->sysmsg_result > 0)
1073 error = copyout(buf, uap->buf, uap->sysmsg_result);
1076 objcache_put(namei_oc, path);
1085 * Execute a mount control operation by resolving the path to a mount point
1086 * and calling vop_mountctl().
1088 * Use the mount point from the nch instead of the vnode so nullfs mounts
1089 * can properly spike the VOP.
1092 kern_mountctl(const char *path, int op, struct file *fp,
1093 const void *ctl, int ctllen,
1094 void *buf, int buflen, int *res)
1098 struct nlookupdata nd;
1103 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1105 error = nlookup(&nd);
1107 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1108 mp = nd.nl_nch.mount;
1115 * Must be the root of the filesystem
1117 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1121 error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen,
1128 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1130 struct thread *td = curthread;
1131 struct proc *p = td->td_proc;
1134 char *fullpath, *freepath;
1137 if ((error = nlookup(nd)) != 0)
1139 mp = nd->nl_nch.mount;
1141 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1144 error = mount_path(p, mp, &fullpath, &freepath);
1147 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1148 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1149 kfree(freepath, M_TEMP);
1151 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1152 bcopy(sp, buf, sizeof(*buf));
1153 /* Only root should have access to the fsid's. */
1154 if (priv_check(td, PRIV_ROOT))
1155 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1160 * statfs_args(char *path, struct statfs *buf)
1162 * Get filesystem statistics.
1165 sys_statfs(struct statfs_args *uap)
1167 struct nlookupdata nd;
1171 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1173 error = kern_statfs(&nd, &buf);
1176 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1181 kern_fstatfs(int fd, struct statfs *buf)
1183 struct thread *td = curthread;
1184 struct proc *p = td->td_proc;
1188 char *fullpath, *freepath;
1192 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1196 * Try to use mount info from any overlays rather than the
1197 * mount info for the underlying vnode, otherwise we will
1198 * fail when operating on null-mounted paths inside a chroot.
1200 if ((mp = fp->f_nchandle.mount) == NULL)
1201 mp = ((struct vnode *)fp->f_data)->v_mount;
1206 if (fp->f_cred == NULL) {
1211 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1214 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1216 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1217 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1218 kfree(freepath, M_TEMP);
1220 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1221 bcopy(sp, buf, sizeof(*buf));
1223 /* Only root should have access to the fsid's. */
1224 if (priv_check(td, PRIV_ROOT))
1225 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1233 * fstatfs_args(int fd, struct statfs *buf)
1235 * Get filesystem statistics.
1238 sys_fstatfs(struct fstatfs_args *uap)
1243 error = kern_fstatfs(uap->fd, &buf);
1246 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1251 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1257 if ((error = nlookup(nd)) != 0)
1259 mp = nd->nl_nch.mount;
1260 sp = &mp->mnt_vstat;
1261 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1265 if (mp->mnt_flag & MNT_RDONLY)
1266 sp->f_flag |= ST_RDONLY;
1267 if (mp->mnt_flag & MNT_NOSUID)
1268 sp->f_flag |= ST_NOSUID;
1269 bcopy(sp, buf, sizeof(*buf));
1274 * statfs_args(char *path, struct statfs *buf)
1276 * Get filesystem statistics.
1279 sys_statvfs(struct statvfs_args *uap)
1281 struct nlookupdata nd;
1285 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1287 error = kern_statvfs(&nd, &buf);
1290 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1295 kern_fstatvfs(int fd, struct statvfs *buf)
1297 struct thread *td = curthread;
1298 struct proc *p = td->td_proc;
1305 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1307 if ((mp = fp->f_nchandle.mount) == NULL)
1308 mp = ((struct vnode *)fp->f_data)->v_mount;
1313 if (fp->f_cred == NULL) {
1317 sp = &mp->mnt_vstat;
1318 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1322 if (mp->mnt_flag & MNT_RDONLY)
1323 sp->f_flag |= ST_RDONLY;
1324 if (mp->mnt_flag & MNT_NOSUID)
1325 sp->f_flag |= ST_NOSUID;
1327 bcopy(sp, buf, sizeof(*buf));
1335 * fstatfs_args(int fd, struct statfs *buf)
1337 * Get filesystem statistics.
1340 sys_fstatvfs(struct fstatvfs_args *uap)
1345 error = kern_fstatvfs(uap->fd, &buf);
1348 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1353 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1355 * Get statistics on all filesystems.
1358 struct getfsstat_info {
1359 struct statfs *sfsp;
1367 static int getfsstat_callback(struct mount *, void *);
1370 sys_getfsstat(struct getfsstat_args *uap)
1372 struct thread *td = curthread;
1373 struct getfsstat_info info;
1375 bzero(&info, sizeof(info));
1377 info.maxcount = uap->bufsize / sizeof(struct statfs);
1378 info.sfsp = uap->buf;
1380 info.flags = uap->flags;
1383 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1384 if (info.sfsp && info.count > info.maxcount)
1385 uap->sysmsg_result = info.maxcount;
1387 uap->sysmsg_result = info.count;
1388 return (info.error);
1392 getfsstat_callback(struct mount *mp, void *data)
1394 struct getfsstat_info *info = data;
1400 if (info->sfsp && info->count < info->maxcount) {
1401 if (info->td->td_proc &&
1402 !chroot_visible_mnt(mp, info->td->td_proc)) {
1408 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1409 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1410 * overrides MNT_WAIT.
1412 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1413 (info->flags & MNT_WAIT)) &&
1414 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1417 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1419 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1421 info->error = error;
1424 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1425 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1426 kfree(freepath, M_TEMP);
1428 error = copyout(sp, info->sfsp, sizeof(*sp));
1430 info->error = error;
1440 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1441 long bufsize, int flags)
1443 * Get statistics on all filesystems.
1446 struct getvfsstat_info {
1447 struct statfs *sfsp;
1448 struct statvfs *vsfsp;
1456 static int getvfsstat_callback(struct mount *, void *);
1459 sys_getvfsstat(struct getvfsstat_args *uap)
1461 struct thread *td = curthread;
1462 struct getvfsstat_info info;
1464 bzero(&info, sizeof(info));
1466 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1467 info.sfsp = uap->buf;
1468 info.vsfsp = uap->vbuf;
1470 info.flags = uap->flags;
1473 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1474 if (info.vsfsp && info.count > info.maxcount)
1475 uap->sysmsg_result = info.maxcount;
1477 uap->sysmsg_result = info.count;
1478 return (info.error);
1482 getvfsstat_callback(struct mount *mp, void *data)
1484 struct getvfsstat_info *info = data;
1486 struct statvfs *vsp;
1491 if (info->vsfsp && info->count < info->maxcount) {
1492 if (info->td->td_proc &&
1493 !chroot_visible_mnt(mp, info->td->td_proc)) {
1497 vsp = &mp->mnt_vstat;
1500 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1501 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1502 * overrides MNT_WAIT.
1504 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1505 (info->flags & MNT_WAIT)) &&
1506 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1509 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1511 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1512 (info->flags & MNT_WAIT)) &&
1513 (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) {
1517 if (mp->mnt_flag & MNT_RDONLY)
1518 vsp->f_flag |= ST_RDONLY;
1519 if (mp->mnt_flag & MNT_NOSUID)
1520 vsp->f_flag |= ST_NOSUID;
1522 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1524 info->error = error;
1527 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1528 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1529 kfree(freepath, M_TEMP);
1531 error = copyout(sp, info->sfsp, sizeof(*sp));
1533 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1535 info->error = error;
1547 * fchdir_args(int fd)
1549 * Change current working directory to a given file descriptor.
1552 sys_fchdir(struct fchdir_args *uap)
1554 struct thread *td = curthread;
1555 struct proc *p = td->td_proc;
1556 struct filedesc *fdp = p->p_fd;
1557 struct vnode *vp, *ovp;
1560 struct nchandle nch, onch, tnch;
1563 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1565 lwkt_gettoken(&p->p_token);
1566 vp = (struct vnode *)fp->f_data;
1568 vn_lock(vp, LK_SHARED | LK_RETRY);
1569 if (fp->f_nchandle.ncp == NULL)
1572 error = checkvp_chdir(vp, td);
1577 cache_copy(&fp->f_nchandle, &nch);
1580 * If the ncp has become a mount point, traverse through
1584 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1585 (mp = cache_findmount(&nch)) != NULL
1587 error = nlookup_mp(mp, &tnch);
1589 cache_unlock(&tnch); /* leave ref intact */
1591 vp = tnch.ncp->nc_vp;
1592 error = vget(vp, LK_SHARED);
1593 KKASSERT(error == 0);
1597 cache_dropmount(mp);
1601 onch = fdp->fd_ncdir;
1602 vn_unlock(vp); /* leave ref intact */
1604 fdp->fd_ncdir = nch;
1613 lwkt_reltoken(&p->p_token);
1618 kern_chdir(struct nlookupdata *nd)
1620 struct thread *td = curthread;
1621 struct proc *p = td->td_proc;
1622 struct filedesc *fdp = p->p_fd;
1623 struct vnode *vp, *ovp;
1624 struct nchandle onch;
1627 nd->nl_flags |= NLC_SHAREDLOCK;
1628 if ((error = nlookup(nd)) != 0)
1630 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1632 if ((error = vget(vp, LK_SHARED)) != 0)
1635 lwkt_gettoken(&p->p_token);
1636 error = checkvp_chdir(vp, td);
1640 onch = fdp->fd_ncdir;
1641 cache_unlock(&nd->nl_nch); /* leave reference intact */
1642 fdp->fd_ncdir = nd->nl_nch;
1646 cache_zero(&nd->nl_nch);
1650 lwkt_reltoken(&p->p_token);
1655 * chdir_args(char *path)
1657 * Change current working directory (``.'').
1660 sys_chdir(struct chdir_args *uap)
1662 struct nlookupdata nd;
1665 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1667 error = kern_chdir(&nd);
1673 * Helper function for raised chroot(2) security function: Refuse if
1674 * any filedescriptors are open directories.
1677 chroot_refuse_vdir_fds(struct filedesc *fdp)
1684 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1685 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1687 vp = (struct vnode *)fp->f_data;
1688 if (vp->v_type != VDIR) {
1699 * This sysctl determines if we will allow a process to chroot(2) if it
1700 * has a directory open:
1701 * 0: disallowed for all processes.
1702 * 1: allowed for processes that were not already chroot(2)'ed.
1703 * 2: allowed for all processes.
1706 static int chroot_allow_open_directories = 1;
1708 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1709 &chroot_allow_open_directories, 0, "");
1712 * chroot to the specified namecache entry. We obtain the vp from the
1713 * namecache data. The passed ncp must be locked and referenced and will
1714 * remain locked and referenced on return.
1717 kern_chroot(struct nchandle *nch)
1719 struct thread *td = curthread;
1720 struct proc *p = td->td_proc;
1721 struct filedesc *fdp = p->p_fd;
1726 * Only privileged user can chroot
1728 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1733 * Disallow open directory descriptors (fchdir() breakouts).
1735 if (chroot_allow_open_directories == 0 ||
1736 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1737 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1740 if ((vp = nch->ncp->nc_vp) == NULL)
1743 if ((error = vget(vp, LK_SHARED)) != 0)
1747 * Check the validity of vp as a directory to change to and
1748 * associate it with rdir/jdir.
1750 error = checkvp_chdir(vp, td);
1751 vn_unlock(vp); /* leave reference intact */
1753 vrele(fdp->fd_rdir);
1754 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1755 cache_drop(&fdp->fd_nrdir);
1756 cache_copy(nch, &fdp->fd_nrdir);
1757 if (fdp->fd_jdir == NULL) {
1760 cache_copy(nch, &fdp->fd_njdir);
1769 * chroot_args(char *path)
1771 * Change notion of root (``/'') directory.
1774 sys_chroot(struct chroot_args *uap)
1776 struct thread *td __debugvar = curthread;
1777 struct nlookupdata nd;
1780 KKASSERT(td->td_proc);
1781 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1783 nd.nl_flags |= NLC_EXEC;
1784 error = nlookup(&nd);
1786 error = kern_chroot(&nd.nl_nch);
1793 sys_chroot_kernel(struct chroot_kernel_args *uap)
1795 struct thread *td = curthread;
1796 struct nlookupdata nd;
1797 struct nchandle *nch;
1801 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1805 error = nlookup(&nd);
1811 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1815 if ((vp = nch->ncp->nc_vp) == NULL) {
1820 if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0)
1823 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap->path);
1825 vfs_cache_setroot(vp, cache_hold(nch));
1835 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1836 * determine whether it is legal to chdir to the vnode. The vnode's state
1837 * is not changed by this call.
1840 checkvp_chdir(struct vnode *vp, struct thread *td)
1844 if (vp->v_type != VDIR)
1847 error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
1852 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1854 struct thread *td = curthread;
1855 struct proc *p = td->td_proc;
1856 struct lwp *lp = td->td_lwp;
1857 struct filedesc *fdp = p->p_fd;
1862 int type, indx, error = 0;
1865 if ((oflags & O_ACCMODE) == O_ACCMODE)
1867 flags = FFLAGS(oflags);
1868 error = falloc(lp, &nfp, NULL);
1872 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1875 * XXX p_dupfd is a real mess. It allows a device to return a
1876 * file descriptor to be duplicated rather then doing the open
1882 * Call vn_open() to do the lookup and assign the vnode to the
1883 * file pointer. vn_open() does not change the ref count on fp
1884 * and the vnode, on success, will be inherited by the file pointer
1887 * Request a shared lock on the vnode if possible.
1889 nd->nl_flags |= NLC_LOCKVP;
1890 if ((flags & (O_CREAT|O_TRUNC)) == 0)
1891 nd->nl_flags |= NLC_SHAREDLOCK;
1893 error = vn_open(nd, fp, flags, cmode);
1898 * handle special fdopen() case. bleh. dupfdopen() is
1899 * responsible for dropping the old contents of ofiles[indx]
1902 * Note that fsetfd() will add a ref to fp which represents
1903 * the fd_files[] assignment. We must still drop our
1906 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1907 if (fdalloc(p, 0, &indx) == 0) {
1908 error = dupfdopen(fdp, indx, lp->lwp_dupfd, flags, error);
1911 fdrop(fp); /* our ref */
1914 fsetfd(fdp, NULL, indx);
1917 fdrop(fp); /* our ref */
1918 if (error == ERESTART)
1924 * ref the vnode for ourselves so it can't be ripped out from under
1925 * is. XXX need an ND flag to request that the vnode be returned
1928 * Reserve a file descriptor but do not assign it until the open
1931 vp = (struct vnode *)fp->f_data;
1933 if ((error = fdalloc(p, 0, &indx)) != 0) {
1940 * If no error occurs the vp will have been assigned to the file
1945 if (flags & (O_EXLOCK | O_SHLOCK)) {
1946 lf.l_whence = SEEK_SET;
1949 if (flags & O_EXLOCK)
1950 lf.l_type = F_WRLCK;
1952 lf.l_type = F_RDLCK;
1953 if (flags & FNONBLOCK)
1958 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1960 * lock request failed. Clean up the reserved
1964 fsetfd(fdp, NULL, indx);
1968 fp->f_flag |= FHASLOCK;
1972 * Assert that all regular file vnodes were created with a object.
1974 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1975 ("open: regular file has no backing object after vn_open"));
1981 * release our private reference, leaving the one associated with the
1982 * descriptor table intact.
1984 fsetfd(fdp, fp, indx);
1987 if (oflags & O_CLOEXEC)
1988 error = fsetfdflags(fdp, *res, UF_EXCLOSE);
1993 * open_args(char *path, int flags, int mode)
1995 * Check permissions, allocate an open file structure,
1996 * and call the device open routine if any.
1999 sys_open(struct open_args *uap)
2001 struct nlookupdata nd;
2004 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2006 error = kern_open(&nd, uap->flags,
2007 uap->mode, &uap->sysmsg_result);
2014 * openat_args(int fd, char *path, int flags, int mode)
2017 sys_openat(struct openat_args *uap)
2019 struct nlookupdata nd;
2023 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2025 error = kern_open(&nd, uap->flags, uap->mode,
2026 &uap->sysmsg_result);
2028 nlookup_done_at(&nd, fp);
2033 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
2035 struct thread *td = curthread;
2036 struct proc *p = td->td_proc;
2045 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2046 vattr.va_rmajor = rmajor;
2047 vattr.va_rminor = rminor;
2049 switch (mode & S_IFMT) {
2050 case S_IFMT: /* used by badsect to flag bad sectors */
2051 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
2052 vattr.va_type = VBAD;
2055 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2056 vattr.va_type = VCHR;
2059 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2060 vattr.va_type = VBLK;
2063 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2066 case S_IFDIR: /* special directories support for HAMMER */
2067 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2068 vattr.va_type = VDIR;
2079 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2080 if ((error = nlookup(nd)) != 0)
2082 if (nd->nl_nch.ncp->nc_vp)
2084 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2088 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2089 nd->nl_cred, NAMEI_CREATE);
2092 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2093 &vp, nd->nl_cred, &vattr);
2101 * mknod_args(char *path, int mode, int dev)
2103 * Create a special file.
2106 sys_mknod(struct mknod_args *uap)
2108 struct nlookupdata nd;
2111 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2113 error = kern_mknod(&nd, uap->mode,
2114 umajor(uap->dev), uminor(uap->dev));
2121 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2123 * Create a special file. The path is relative to the directory associated
2127 sys_mknodat(struct mknodat_args *uap)
2129 struct nlookupdata nd;
2133 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2135 error = kern_mknod(&nd, uap->mode,
2136 umajor(uap->dev), uminor(uap->dev));
2138 nlookup_done_at(&nd, fp);
2143 kern_mkfifo(struct nlookupdata *nd, int mode)
2145 struct thread *td = curthread;
2146 struct proc *p = td->td_proc;
2153 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2154 if ((error = nlookup(nd)) != 0)
2156 if (nd->nl_nch.ncp->nc_vp)
2158 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2162 vattr.va_type = VFIFO;
2163 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2165 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2172 * mkfifo_args(char *path, int mode)
2174 * Create a named pipe.
2177 sys_mkfifo(struct mkfifo_args *uap)
2179 struct nlookupdata nd;
2182 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2184 error = kern_mkfifo(&nd, uap->mode);
2190 * mkfifoat_args(int fd, char *path, mode_t mode)
2192 * Create a named pipe. The path is relative to the directory associated
2196 sys_mkfifoat(struct mkfifoat_args *uap)
2198 struct nlookupdata nd;
2202 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2204 error = kern_mkfifo(&nd, uap->mode);
2205 nlookup_done_at(&nd, fp);
2209 static int hardlink_check_uid = 0;
2210 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2211 &hardlink_check_uid, 0,
2212 "Unprivileged processes cannot create hard links to files owned by other "
2214 static int hardlink_check_gid = 0;
2215 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2216 &hardlink_check_gid, 0,
2217 "Unprivileged processes cannot create hard links to files owned by other "
2221 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2227 * Shortcut if disabled
2229 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2233 * Privileged user can always hardlink
2235 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2239 * Otherwise only if the originating file is owned by the
2240 * same user or group. Note that any group is allowed if
2241 * the file is owned by the caller.
2243 error = VOP_GETATTR(vp, &va);
2247 if (hardlink_check_uid) {
2248 if (cred->cr_uid != va.va_uid)
2252 if (hardlink_check_gid) {
2253 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2261 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2263 struct thread *td = curthread;
2268 * Lookup the source and obtained a locked vnode.
2270 * You may only hardlink a file which you have write permission
2271 * on or which you own.
2273 * XXX relookup on vget failure / race ?
2276 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2277 if ((error = nlookup(nd)) != 0)
2279 vp = nd->nl_nch.ncp->nc_vp;
2280 KKASSERT(vp != NULL);
2281 if (vp->v_type == VDIR)
2282 return (EPERM); /* POSIX */
2283 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2285 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2289 * Unlock the source so we can lookup the target without deadlocking
2290 * (XXX vp is locked already, possible other deadlock?). The target
2293 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2294 nd->nl_flags &= ~NLC_NCPISLOCKED;
2295 cache_unlock(&nd->nl_nch);
2298 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2299 if ((error = nlookup(linknd)) != 0) {
2303 if (linknd->nl_nch.ncp->nc_vp) {
2307 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
2314 * Finally run the new API VOP.
2316 error = can_hardlink(vp, td, td->td_ucred);
2318 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2319 vp, linknd->nl_cred);
2326 * link_args(char *path, char *link)
2328 * Make a hard file link.
2331 sys_link(struct link_args *uap)
2333 struct nlookupdata nd, linknd;
2336 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2338 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2340 error = kern_link(&nd, &linknd);
2341 nlookup_done(&linknd);
2348 * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags)
2350 * Make a hard file link. The path1 argument is relative to the directory
2351 * associated with fd1, and similarly the path2 argument is relative to
2352 * the directory associated with fd2.
2355 sys_linkat(struct linkat_args *uap)
2357 struct nlookupdata nd, linknd;
2358 struct file *fp1, *fp2;
2361 error = nlookup_init_at(&nd, &fp1, uap->fd1, uap->path1, UIO_USERSPACE,
2362 (uap->flags & AT_SYMLINK_FOLLOW) ? NLC_FOLLOW : 0);
2364 error = nlookup_init_at(&linknd, &fp2, uap->fd2,
2365 uap->path2, UIO_USERSPACE, 0);
2367 error = kern_link(&nd, &linknd);
2368 nlookup_done_at(&linknd, fp2);
2370 nlookup_done_at(&nd, fp1);
2375 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2383 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2384 if ((error = nlookup(nd)) != 0)
2386 if (nd->nl_nch.ncp->nc_vp)
2388 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2392 vattr.va_mode = mode;
2393 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2400 * symlink(char *path, char *link)
2402 * Make a symbolic link.
2405 sys_symlink(struct symlink_args *uap)
2407 struct thread *td = curthread;
2408 struct nlookupdata nd;
2413 path = objcache_get(namei_oc, M_WAITOK);
2414 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2416 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2418 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2419 error = kern_symlink(&nd, path, mode);
2423 objcache_put(namei_oc, path);
2428 * symlinkat_args(char *path1, int fd, char *path2)
2430 * Make a symbolic link. The path2 argument is relative to the directory
2431 * associated with fd.
2434 sys_symlinkat(struct symlinkat_args *uap)
2436 struct thread *td = curthread;
2437 struct nlookupdata nd;
2443 path1 = objcache_get(namei_oc, M_WAITOK);
2444 error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2446 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2449 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2450 error = kern_symlink(&nd, path1, mode);
2452 nlookup_done_at(&nd, fp);
2454 objcache_put(namei_oc, path1);
2459 * undelete_args(char *path)
2461 * Delete a whiteout from the filesystem.
2464 sys_undelete(struct undelete_args *uap)
2466 struct nlookupdata nd;
2469 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2471 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2473 error = nlookup(&nd);
2475 error = ncp_writechk(&nd.nl_nch);
2477 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2485 kern_unlink(struct nlookupdata *nd)
2490 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2491 if ((error = nlookup(nd)) != 0)
2493 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2495 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2500 * unlink_args(char *path)
2502 * Delete a name from the filesystem.
2505 sys_unlink(struct unlink_args *uap)
2507 struct nlookupdata nd;
2510 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2512 error = kern_unlink(&nd);
2519 * unlinkat_args(int fd, char *path, int flags)
2521 * Delete the file or directory entry pointed to by fd/path.
2524 sys_unlinkat(struct unlinkat_args *uap)
2526 struct nlookupdata nd;
2530 if (uap->flags & ~AT_REMOVEDIR)
2533 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2535 if (uap->flags & AT_REMOVEDIR)
2536 error = kern_rmdir(&nd);
2538 error = kern_unlink(&nd);
2540 nlookup_done_at(&nd, fp);
2545 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2547 struct thread *td = curthread;
2548 struct proc *p = td->td_proc;
2555 fp = holdfp(p->p_fd, fd, -1);
2558 if (fp->f_type != DTYPE_VNODE) {
2562 vp = (struct vnode *)fp->f_data;
2566 spin_lock(&fp->f_spin);
2567 new_offset = fp->f_offset + offset;
2571 error = VOP_GETATTR(vp, &vattr);
2572 spin_lock(&fp->f_spin);
2573 new_offset = offset + vattr.va_size;
2576 new_offset = offset;
2578 spin_lock(&fp->f_spin);
2583 spin_lock(&fp->f_spin);
2588 * Validate the seek position. Negative offsets are not allowed
2589 * for regular files or directories.
2591 * Normally we would also not want to allow negative offsets for
2592 * character and block-special devices. However kvm addresses
2593 * on 64 bit architectures might appear to be negative and must
2597 if (new_offset < 0 &&
2598 (vp->v_type == VREG || vp->v_type == VDIR)) {
2601 fp->f_offset = new_offset;
2604 *res = fp->f_offset;
2605 spin_unlock(&fp->f_spin);
2612 * lseek_args(int fd, int pad, off_t offset, int whence)
2614 * Reposition read/write file offset.
2617 sys_lseek(struct lseek_args *uap)
2621 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2622 &uap->sysmsg_offset);
2628 * Check if current process can access given file. amode is a bitmask of *_OK
2629 * access bits. flags is a bitmask of AT_* flags.
2632 kern_access(struct nlookupdata *nd, int amode, int flags)
2637 if (flags & ~AT_EACCESS)
2639 nd->nl_flags |= NLC_SHAREDLOCK;
2640 if ((error = nlookup(nd)) != 0)
2643 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
2647 /* Flags == 0 means only check for existence. */
2656 if ((mode & VWRITE) == 0 ||
2657 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2658 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2661 * If the file handle is stale we have to re-resolve the
2662 * entry with the ncp held exclusively. This is a hack
2665 if (error == ESTALE) {
2667 cache_unlock(&nd->nl_nch);
2668 cache_lock(&nd->nl_nch);
2669 cache_setunresolved(&nd->nl_nch);
2670 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2683 * access_args(char *path, int flags)
2685 * Check access permissions.
2688 sys_access(struct access_args *uap)
2690 struct nlookupdata nd;
2693 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2695 error = kern_access(&nd, uap->flags, 0);
2702 * eaccess_args(char *path, int flags)
2704 * Check access permissions.
2707 sys_eaccess(struct eaccess_args *uap)
2709 struct nlookupdata nd;
2712 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2714 error = kern_access(&nd, uap->flags, AT_EACCESS);
2721 * faccessat_args(int fd, char *path, int amode, int flags)
2723 * Check access permissions.
2726 sys_faccessat(struct faccessat_args *uap)
2728 struct nlookupdata nd;
2732 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2735 error = kern_access(&nd, uap->amode, uap->flags);
2736 nlookup_done_at(&nd, fp);
2741 kern_stat(struct nlookupdata *nd, struct stat *st)
2746 nd->nl_flags |= NLC_SHAREDLOCK;
2747 if ((error = nlookup(nd)) != 0)
2750 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2753 if ((error = vget(vp, LK_SHARED)) != 0)
2755 error = vn_stat(vp, st, nd->nl_cred);
2758 * If the file handle is stale we have to re-resolve the
2759 * entry with the ncp held exclusively. This is a hack
2762 if (error == ESTALE) {
2764 cache_unlock(&nd->nl_nch);
2765 cache_lock(&nd->nl_nch);
2766 cache_setunresolved(&nd->nl_nch);
2767 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2777 * stat_args(char *path, struct stat *ub)
2779 * Get file status; this version follows links.
2782 sys_stat(struct stat_args *uap)
2784 struct nlookupdata nd;
2788 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2790 error = kern_stat(&nd, &st);
2792 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2799 * lstat_args(char *path, struct stat *ub)
2801 * Get file status; this version does not follow links.
2804 sys_lstat(struct lstat_args *uap)
2806 struct nlookupdata nd;
2810 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2812 error = kern_stat(&nd, &st);
2814 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2821 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2823 * Get status of file pointed to by fd/path.
2826 sys_fstatat(struct fstatat_args *uap)
2828 struct nlookupdata nd;
2834 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2837 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2839 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2840 UIO_USERSPACE, flags);
2842 error = kern_stat(&nd, &st);
2844 error = copyout(&st, uap->sb, sizeof(*uap->sb));
2846 nlookup_done_at(&nd, fp);
2851 kern_pathconf(char *path, int name, int flags, register_t *sysmsg_regp)
2853 struct nlookupdata nd;
2858 error = nlookup_init(&nd, path, UIO_USERSPACE, flags);
2860 error = nlookup(&nd);
2862 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2865 error = VOP_PATHCONF(vp, name, sysmsg_regp);
2872 * pathconf_Args(char *path, int name)
2874 * Get configurable pathname variables.
2877 sys_pathconf(struct pathconf_args *uap)
2879 return (kern_pathconf(uap->path, uap->name, NLC_FOLLOW,
2884 * lpathconf_Args(char *path, int name)
2886 * Get configurable pathname variables, but don't follow symlinks.
2889 sys_lpathconf(struct lpathconf_args *uap)
2891 return (kern_pathconf(uap->path, uap->name, 0, &uap->sysmsg_reg));
2896 * kern_readlink isn't properly split yet. There is a copyin burried
2897 * in VOP_READLINK().
2900 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2902 struct thread *td = curthread;
2908 nd->nl_flags |= NLC_SHAREDLOCK;
2909 if ((error = nlookup(nd)) != 0)
2911 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
2914 if (vp->v_type != VLNK) {
2917 aiov.iov_base = buf;
2918 aiov.iov_len = count;
2919 auio.uio_iov = &aiov;
2920 auio.uio_iovcnt = 1;
2921 auio.uio_offset = 0;
2922 auio.uio_rw = UIO_READ;
2923 auio.uio_segflg = UIO_USERSPACE;
2925 auio.uio_resid = count;
2926 error = VOP_READLINK(vp, &auio, td->td_ucred);
2929 *res = count - auio.uio_resid;
2934 * readlink_args(char *path, char *buf, int count)
2936 * Return target name of a symbolic link.
2939 sys_readlink(struct readlink_args *uap)
2941 struct nlookupdata nd;
2944 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2946 error = kern_readlink(&nd, uap->buf, uap->count,
2947 &uap->sysmsg_result);
2954 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
2956 * Return target name of a symbolic link. The path is relative to the
2957 * directory associated with fd.
2960 sys_readlinkat(struct readlinkat_args *uap)
2962 struct nlookupdata nd;
2966 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2968 error = kern_readlink(&nd, uap->buf, uap->bufsize,
2969 &uap->sysmsg_result);
2971 nlookup_done_at(&nd, fp);
2976 setfflags(struct vnode *vp, int flags)
2978 struct thread *td = curthread;
2983 * Prevent non-root users from setting flags on devices. When
2984 * a device is reused, users can retain ownership of the device
2985 * if they are allowed to set flags and programs assume that
2986 * chown can't fail when done as root.
2988 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2989 ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
2993 * note: vget is required for any operation that might mod the vnode
2994 * so VINACTIVE is properly cleared.
2996 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2998 vattr.va_flags = flags;
2999 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3006 * chflags(char *path, int flags)
3008 * Change flags of a file given a path name.
3011 sys_chflags(struct chflags_args *uap)
3013 struct nlookupdata nd;
3018 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3020 error = nlookup(&nd);
3022 error = ncp_writechk(&nd.nl_nch);
3024 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3027 error = setfflags(vp, uap->flags);
3034 * lchflags(char *path, int flags)
3036 * Change flags of a file given a path name, but don't follow symlinks.
3039 sys_lchflags(struct lchflags_args *uap)
3041 struct nlookupdata nd;
3046 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3048 error = nlookup(&nd);
3050 error = ncp_writechk(&nd.nl_nch);
3052 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3055 error = setfflags(vp, uap->flags);
3062 * fchflags_args(int fd, int flags)
3064 * Change flags of a file given a file descriptor.
3067 sys_fchflags(struct fchflags_args *uap)
3069 struct thread *td = curthread;
3070 struct proc *p = td->td_proc;
3074 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3076 if (fp->f_nchandle.ncp)
3077 error = ncp_writechk(&fp->f_nchandle);
3079 error = setfflags((struct vnode *) fp->f_data, uap->flags);
3085 setfmode(struct vnode *vp, int mode)
3087 struct thread *td = curthread;
3092 * note: vget is required for any operation that might mod the vnode
3093 * so VINACTIVE is properly cleared.
3095 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3097 vattr.va_mode = mode & ALLPERMS;
3098 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3105 kern_chmod(struct nlookupdata *nd, int mode)
3110 if ((error = nlookup(nd)) != 0)
3112 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3114 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3115 error = setfmode(vp, mode);
3121 * chmod_args(char *path, int mode)
3123 * Change mode of a file given path name.
3126 sys_chmod(struct chmod_args *uap)
3128 struct nlookupdata nd;
3131 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3133 error = kern_chmod(&nd, uap->mode);
3139 * lchmod_args(char *path, int mode)
3141 * Change mode of a file given path name (don't follow links.)
3144 sys_lchmod(struct lchmod_args *uap)
3146 struct nlookupdata nd;
3149 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3151 error = kern_chmod(&nd, uap->mode);
3157 * fchmod_args(int fd, int mode)
3159 * Change mode of a file given a file descriptor.
3162 sys_fchmod(struct fchmod_args *uap)
3164 struct thread *td = curthread;
3165 struct proc *p = td->td_proc;
3169 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3171 if (fp->f_nchandle.ncp)
3172 error = ncp_writechk(&fp->f_nchandle);
3174 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3180 * fchmodat_args(char *path, int mode)
3182 * Change mode of a file pointed to by fd/path.
3185 sys_fchmodat(struct fchmodat_args *uap)
3187 struct nlookupdata nd;
3192 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3194 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3196 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3197 UIO_USERSPACE, flags);
3199 error = kern_chmod(&nd, uap->mode);
3200 nlookup_done_at(&nd, fp);
3205 setfown(struct mount *mp, struct vnode *vp, uid_t uid, gid_t gid)
3207 struct thread *td = curthread;
3215 * note: vget is required for any operation that might mod the vnode
3216 * so VINACTIVE is properly cleared.
3218 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3219 if ((error = VOP_GETATTR(vp, &vattr)) != 0)
3221 o_uid = vattr.va_uid;
3222 o_gid = vattr.va_gid;
3223 size = vattr.va_size;
3228 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3237 VFS_ACCOUNT(mp, o_uid, o_gid, -size);
3238 VFS_ACCOUNT(mp, uid, gid, size);
3245 kern_chown(struct nlookupdata *nd, int uid, int gid)
3250 if ((error = nlookup(nd)) != 0)
3252 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3254 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3255 error = setfown(nd->nl_nch.mount, vp, uid, gid);
3261 * chown(char *path, int uid, int gid)
3263 * Set ownership given a path name.
3266 sys_chown(struct chown_args *uap)
3268 struct nlookupdata nd;
3271 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3273 error = kern_chown(&nd, uap->uid, uap->gid);
3279 * lchown_args(char *path, int uid, int gid)
3281 * Set ownership given a path name, do not cross symlinks.
3284 sys_lchown(struct lchown_args *uap)
3286 struct nlookupdata nd;
3289 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3291 error = kern_chown(&nd, uap->uid, uap->gid);
3297 * fchown_args(int fd, int uid, int gid)
3299 * Set ownership given a file descriptor.
3302 sys_fchown(struct fchown_args *uap)
3304 struct thread *td = curthread;
3305 struct proc *p = td->td_proc;
3309 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3311 if (fp->f_nchandle.ncp)
3312 error = ncp_writechk(&fp->f_nchandle);
3314 error = setfown(p->p_fd->fd_ncdir.mount,
3315 (struct vnode *)fp->f_data, uap->uid, uap->gid);
3321 * fchownat(int fd, char *path, int uid, int gid, int flags)
3323 * Set ownership of file pointed to by fd/path.
3326 sys_fchownat(struct fchownat_args *uap)
3328 struct nlookupdata nd;
3333 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3335 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3337 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3338 UIO_USERSPACE, flags);
3340 error = kern_chown(&nd, uap->uid, uap->gid);
3341 nlookup_done_at(&nd, fp);
3347 getutimes(const struct timeval *tvp, struct timespec *tsp)
3349 struct timeval tv[2];
3353 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3356 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3357 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3363 setutimes(struct vnode *vp, struct vattr *vattr,
3364 const struct timespec *ts, int nullflag)
3366 struct thread *td = curthread;
3370 vattr->va_atime = ts[0];
3371 vattr->va_mtime = ts[1];
3373 vattr->va_vaflags |= VA_UTIMES_NULL;
3374 error = VOP_SETATTR(vp, vattr, td->td_ucred);
3380 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3382 struct timespec ts[2];
3387 if ((error = getutimes(tptr, ts)) != 0)
3391 * NOTE: utimes() succeeds for the owner even if the file
3392 * is not user-writable.
3394 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3396 if ((error = nlookup(nd)) != 0)
3398 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3400 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3404 * note: vget is required for any operation that might mod the vnode
3405 * so VINACTIVE is properly cleared.
3407 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3408 error = vget(vp, LK_EXCLUSIVE);
3410 error = setutimes(vp, &vattr, ts, (tptr == NULL));
3419 * utimes_args(char *path, struct timeval *tptr)
3421 * Set the access and modification times of a file.
3424 sys_utimes(struct utimes_args *uap)
3426 struct timeval tv[2];
3427 struct nlookupdata nd;
3431 error = copyin(uap->tptr, tv, sizeof(tv));
3435 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3437 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3443 * lutimes_args(char *path, struct timeval *tptr)
3445 * Set the access and modification times of a file.
3448 sys_lutimes(struct lutimes_args *uap)
3450 struct timeval tv[2];
3451 struct nlookupdata nd;
3455 error = copyin(uap->tptr, tv, sizeof(tv));
3459 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3461 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3467 * Set utimes on a file descriptor. The creds used to open the
3468 * file are used to determine whether the operation is allowed
3472 kern_futimes(int fd, struct timeval *tptr)
3474 struct thread *td = curthread;
3475 struct proc *p = td->td_proc;
3476 struct timespec ts[2];
3482 error = getutimes(tptr, ts);
3485 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3487 if (fp->f_nchandle.ncp)
3488 error = ncp_writechk(&fp->f_nchandle);
3491 error = vget(vp, LK_EXCLUSIVE);
3493 error = VOP_GETATTR(vp, &vattr);
3495 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3499 error = setutimes(vp, &vattr, ts,
3510 * futimes_args(int fd, struct timeval *tptr)
3512 * Set the access and modification times of a file.
3515 sys_futimes(struct futimes_args *uap)
3517 struct timeval tv[2];
3521 error = copyin(uap->tptr, tv, sizeof(tv));
3525 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3531 kern_truncate(struct nlookupdata *nd, off_t length)
3538 uint64_t old_size = 0;
3542 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3543 if ((error = nlookup(nd)) != 0)
3545 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3547 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3549 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
3554 if (vp->v_type == VDIR) {
3558 if (vfs_quota_enabled) {
3559 error = VOP_GETATTR(vp, &vattr);
3560 KASSERT(error == 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3563 old_size = vattr.va_size;
3566 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3568 vattr.va_size = length;
3569 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3570 VFS_ACCOUNT(nd->nl_nch.mount, uid, gid, length - old_size);
3578 * truncate(char *path, int pad, off_t length)
3580 * Truncate a file given its path name.
3583 sys_truncate(struct truncate_args *uap)
3585 struct nlookupdata nd;
3588 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3590 error = kern_truncate(&nd, uap->length);
3596 kern_ftruncate(int fd, off_t length)
3598 struct thread *td = curthread;
3599 struct proc *p = td->td_proc;
3606 uint64_t old_size = 0;
3611 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3613 if (fp->f_nchandle.ncp) {
3614 error = ncp_writechk(&fp->f_nchandle);
3618 if ((fp->f_flag & FWRITE) == 0) {
3622 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3626 vp = (struct vnode *)fp->f_data;
3627 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3628 if (vp->v_type == VDIR) {
3634 if (vfs_quota_enabled) {
3635 error = VOP_GETATTR(vp, &vattr);
3636 KASSERT(error == 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
3639 old_size = vattr.va_size;
3642 if ((error = vn_writechk(vp, NULL)) == 0) {
3644 vattr.va_size = length;
3645 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3647 VFS_ACCOUNT(mp, uid, gid, length - old_size);
3656 * ftruncate_args(int fd, int pad, off_t length)
3658 * Truncate a file given a file descriptor.
3661 sys_ftruncate(struct ftruncate_args *uap)
3665 error = kern_ftruncate(uap->fd, uap->length);
3673 * Sync an open file.
3676 sys_fsync(struct fsync_args *uap)
3678 struct thread *td = curthread;
3679 struct proc *p = td->td_proc;
3685 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3687 vp = (struct vnode *)fp->f_data;
3688 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3689 if ((obj = vp->v_object) != NULL) {
3690 if (vp->v_mount == NULL ||
3691 (vp->v_mount->mnt_kern_flag & MNTK_NOMSYNC) == 0) {
3692 vm_object_page_clean(obj, 0, 0, 0);
3695 error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
3696 if (error == 0 && vp->v_mount)
3697 error = buf_fsync(vp);
3705 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3707 struct nchandle fnchd;
3708 struct nchandle tnchd;
3709 struct namecache *ncp;
3716 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3717 if ((error = nlookup(fromnd)) != 0)
3719 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3721 fnchd.mount = fromnd->nl_nch.mount;
3725 * unlock the source nch so we can lookup the target nch without
3726 * deadlocking. The target may or may not exist so we do not check
3727 * for a target vp like kern_mkdir() and other creation functions do.
3729 * The source and target directories are ref'd and rechecked after
3730 * everything is relocked to determine if the source or target file
3733 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3734 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3735 cache_unlock(&fromnd->nl_nch);
3737 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3738 if ((error = nlookup(tond)) != 0) {
3742 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3746 tnchd.mount = tond->nl_nch.mount;
3750 * If the source and target are the same there is nothing to do
3752 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3759 * Mount points cannot be renamed or overwritten
3761 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3770 * Relock the source ncp. cache_relock() will deal with any
3771 * deadlocks against the already-locked tond and will also
3772 * make sure both are resolved.
3774 * NOTE AFTER RELOCKING: The source or target ncp may have become
3775 * invalid while they were unlocked, nc_vp and nc_mount could
3778 cache_relock(&fromnd->nl_nch, fromnd->nl_cred,
3779 &tond->nl_nch, tond->nl_cred);
3780 fromnd->nl_flags |= NLC_NCPISLOCKED;
3783 * If either fromnd or tond are marked destroyed a ripout occured
3784 * out from under us and we must retry.
3786 if ((fromnd->nl_nch.ncp->nc_flag & (NCF_DESTROYED | NCF_UNRESOLVED)) ||
3787 fromnd->nl_nch.ncp->nc_vp == NULL ||
3788 (tond->nl_nch.ncp->nc_flag & NCF_DESTROYED)) {
3789 kprintf("kern_rename: retry due to ripout on: "
3790 "\"%s\" -> \"%s\"\n",
3791 fromnd->nl_nch.ncp->nc_name,
3792 tond->nl_nch.ncp->nc_name);
3799 * make sure the parent directories linkages are the same
3801 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3802 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3809 * Both the source and target must be within the same filesystem and
3810 * in the same filesystem as their parent directories within the
3811 * namecache topology.
3813 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3816 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3817 mp != tond->nl_nch.mount) {
3824 * Make sure the mount point is writable
3826 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3833 * If the target exists and either the source or target is a directory,
3834 * then both must be directories.
3836 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3839 if (tond->nl_nch.ncp->nc_vp) {
3840 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3842 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3843 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3845 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3851 * You cannot rename a source into itself or a subdirectory of itself.
3852 * We check this by travsersing the target directory upwards looking
3853 * for a match against the source.
3858 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3859 if (fromnd->nl_nch.ncp == ncp) {
3870 * Even though the namespaces are different, they may still represent
3871 * hardlinks to the same file. The filesystem might have a hard time
3872 * with this so we issue a NREMOVE of the source instead of a NRENAME
3873 * when we detect the situation.
3876 fdvp = fromnd->nl_dvp;
3877 tdvp = tond->nl_dvp;
3878 if (fdvp == NULL || tdvp == NULL) {
3880 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3881 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3884 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3885 fdvp, tdvp, tond->nl_cred);
3892 * rename_args(char *from, char *to)
3894 * Rename files. Source and destination must either both be directories,
3895 * or both not be directories. If target is a directory, it must be empty.
3898 sys_rename(struct rename_args *uap)
3900 struct nlookupdata fromnd, tond;
3904 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3906 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3908 error = kern_rename(&fromnd, &tond);
3909 nlookup_done(&tond);
3911 nlookup_done(&fromnd);
3912 } while (error == EAGAIN);
3917 * renameat_args(int oldfd, char *old, int newfd, char *new)
3919 * Rename files using paths relative to the directories associated with
3920 * oldfd and newfd. Source and destination must either both be directories,
3921 * or both not be directories. If target is a directory, it must be empty.
3924 sys_renameat(struct renameat_args *uap)
3926 struct nlookupdata oldnd, newnd;
3927 struct file *oldfp, *newfp;
3931 error = nlookup_init_at(&oldnd, &oldfp,
3932 uap->oldfd, uap->old,
3935 error = nlookup_init_at(&newnd, &newfp,
3936 uap->newfd, uap->new,
3939 error = kern_rename(&oldnd, &newnd);
3940 nlookup_done_at(&newnd, newfp);
3942 nlookup_done_at(&oldnd, oldfp);
3943 } while (error == EAGAIN);
3948 kern_mkdir(struct nlookupdata *nd, int mode)
3950 struct thread *td = curthread;
3951 struct proc *p = td->td_proc;
3957 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3958 if ((error = nlookup(nd)) != 0)
3961 if (nd->nl_nch.ncp->nc_vp)
3963 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3966 vattr.va_type = VDIR;
3967 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3970 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
3977 * mkdir_args(char *path, int mode)
3979 * Make a directory file.
3982 sys_mkdir(struct mkdir_args *uap)
3984 struct nlookupdata nd;
3987 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3989 error = kern_mkdir(&nd, uap->mode);
3995 * mkdirat_args(int fd, char *path, mode_t mode)
3997 * Make a directory file. The path is relative to the directory associated
4001 sys_mkdirat(struct mkdirat_args *uap)
4003 struct nlookupdata nd;
4007 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
4009 error = kern_mkdir(&nd, uap->mode);
4010 nlookup_done_at(&nd, fp);
4015 kern_rmdir(struct nlookupdata *nd)
4020 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
4021 if ((error = nlookup(nd)) != 0)
4025 * Do not allow directories representing mount points to be
4026 * deleted, even if empty. Check write perms on mount point
4027 * in case the vnode is aliased (aka nullfs).
4029 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
4031 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4033 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
4038 * rmdir_args(char *path)
4040 * Remove a directory file.
4043 sys_rmdir(struct rmdir_args *uap)
4045 struct nlookupdata nd;
4048 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4050 error = kern_rmdir(&nd);
4056 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
4057 enum uio_seg direction)
4059 struct thread *td = curthread;
4060 struct proc *p = td->td_proc;
4068 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
4070 if ((fp->f_flag & FREAD) == 0) {
4074 vp = (struct vnode *)fp->f_data;
4076 if (vp->v_type != VDIR) {
4080 aiov.iov_base = buf;
4081 aiov.iov_len = count;
4082 auio.uio_iov = &aiov;
4083 auio.uio_iovcnt = 1;
4084 auio.uio_rw = UIO_READ;
4085 auio.uio_segflg = direction;
4087 auio.uio_resid = count;
4088 loff = auio.uio_offset = fp->f_offset;
4089 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
4090 fp->f_offset = auio.uio_offset;
4093 if (count == auio.uio_resid) {
4094 if (union_dircheckp) {
4095 error = union_dircheckp(td, &vp, fp);
4102 if ((vp->v_flag & VROOT) &&
4103 (vp->v_mount->mnt_flag & MNT_UNION)) {
4104 struct vnode *tvp = vp;
4105 vp = vp->v_mount->mnt_vnodecovered;
4116 * WARNING! *basep may not be wide enough to accomodate the
4117 * seek offset. XXX should we hack this to return the upper 32 bits
4118 * for offsets greater then 4G?
4121 *basep = (long)loff;
4123 *res = count - auio.uio_resid;
4130 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4132 * Read a block of directory entries in a file system independent format.
4135 sys_getdirentries(struct getdirentries_args *uap)
4140 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
4141 &uap->sysmsg_result, UIO_USERSPACE);
4143 if (error == 0 && uap->basep)
4144 error = copyout(&base, uap->basep, sizeof(*uap->basep));
4149 * getdents_args(int fd, char *buf, size_t count)
4152 sys_getdents(struct getdents_args *uap)
4156 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
4157 &uap->sysmsg_result, UIO_USERSPACE);
4163 * Set the mode mask for creation of filesystem nodes.
4165 * umask(int newmask)
4168 sys_umask(struct umask_args *uap)
4170 struct thread *td = curthread;
4171 struct proc *p = td->td_proc;
4172 struct filedesc *fdp;
4175 uap->sysmsg_result = fdp->fd_cmask;
4176 fdp->fd_cmask = uap->newmask & ALLPERMS;
4181 * revoke(char *path)
4183 * Void all references to file by ripping underlying filesystem
4187 sys_revoke(struct revoke_args *uap)
4189 struct nlookupdata nd;
4196 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4198 error = nlookup(&nd);
4200 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4201 cred = crhold(nd.nl_cred);
4205 error = VOP_GETATTR(vp, &vattr);
4206 if (error == 0 && cred->cr_uid != vattr.va_uid)
4207 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4208 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4210 error = vrevoke(vp, cred);
4211 } else if (error == 0) {
4212 error = vrevoke(vp, cred);
4222 * getfh_args(char *fname, fhandle_t *fhp)
4224 * Get (NFS) file handle
4226 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4227 * mount. This allows nullfs mounts to be explicitly exported.
4229 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4231 * nullfs mounts of subdirectories are not safe. That is, it will
4232 * work, but you do not really have protection against access to
4233 * the related parent directories.
4236 sys_getfh(struct getfh_args *uap)
4238 struct thread *td = curthread;
4239 struct nlookupdata nd;
4246 * Must be super user
4248 if ((error = priv_check(td, PRIV_ROOT)) != 0)
4252 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4254 error = nlookup(&nd);
4256 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4257 mp = nd.nl_nch.mount;
4260 bzero(&fh, sizeof(fh));
4261 fh.fh_fsid = mp->mnt_stat.f_fsid;
4262 error = VFS_VPTOFH(vp, &fh.fh_fid);
4265 error = copyout(&fh, uap->fhp, sizeof(fh));
4271 * fhopen_args(const struct fhandle *u_fhp, int flags)
4273 * syscall for the rpc.lockd to use to translate a NFS file handle into
4274 * an open descriptor.
4276 * warning: do not remove the priv_check() call or this becomes one giant
4280 sys_fhopen(struct fhopen_args *uap)
4282 struct thread *td = curthread;
4283 struct filedesc *fdp = td->td_proc->p_fd;
4288 struct vattr *vap = &vat;
4290 int fmode, mode, error = 0, type;
4296 * Must be super user
4298 error = priv_check(td, PRIV_ROOT);
4302 fmode = FFLAGS(uap->flags);
4305 * Why not allow a non-read/write open for our lockd?
4307 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4309 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4314 * Find the mount point
4316 mp = vfs_getvfs(&fhp.fh_fsid);
4321 /* now give me my vnode, it gets returned to me locked */
4322 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4326 * from now on we have to make sure not
4327 * to forget about the vnode
4328 * any error that causes an abort must vput(vp)
4329 * just set error = err and 'goto bad;'.
4335 if (vp->v_type == VLNK) {
4339 if (vp->v_type == VSOCK) {
4344 if (fmode & (FWRITE | O_TRUNC)) {
4345 if (vp->v_type == VDIR) {
4349 error = vn_writechk(vp, NULL);
4357 error = VOP_ACCESS(vp, mode, td->td_ucred);
4361 if (fmode & O_TRUNC) {
4362 vn_unlock(vp); /* XXX */
4363 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4366 error = VOP_SETATTR(vp, vap, td->td_ucred);
4372 * VOP_OPEN needs the file pointer so it can potentially override
4375 * WARNING! no f_nchandle will be associated when fhopen()ing a
4378 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4382 error = VOP_OPEN(vp, fmode, td->td_ucred, fp);
4385 * setting f_ops this way prevents VOP_CLOSE from being
4386 * called or fdrop() releasing the vp from v_data. Since
4387 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4389 fp->f_ops = &badfileops;
4395 * The fp is given its own reference, we still have our ref and lock.
4397 * Assert that all regular files must be created with a VM object.
4399 if (vp->v_type == VREG && vp->v_object == NULL) {
4400 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
4405 * The open was successful. Handle any locking requirements.
4407 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4408 lf.l_whence = SEEK_SET;
4411 if (fmode & O_EXLOCK)
4412 lf.l_type = F_WRLCK;
4414 lf.l_type = F_RDLCK;
4415 if (fmode & FNONBLOCK)
4420 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
4422 * release our private reference.
4424 fsetfd(fdp, NULL, indx);
4429 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4430 fp->f_flag |= FHASLOCK;
4434 * Clean up. Associate the file pointer with the previously
4435 * reserved descriptor and return it.
4438 fsetfd(fdp, fp, indx);
4440 uap->sysmsg_result = indx;
4441 if (uap->flags & O_CLOEXEC)
4442 error = fsetfdflags(fdp, indx, UF_EXCLOSE);
4446 fsetfd(fdp, NULL, indx);
4455 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4458 sys_fhstat(struct fhstat_args *uap)
4460 struct thread *td = curthread;
4468 * Must be super user
4470 error = priv_check(td, PRIV_ROOT);
4474 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4478 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4481 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4482 error = vn_stat(vp, &sb, td->td_ucred);
4487 error = copyout(&sb, uap->sb, sizeof(sb));
4492 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4495 sys_fhstatfs(struct fhstatfs_args *uap)
4497 struct thread *td = curthread;
4498 struct proc *p = td->td_proc;
4503 char *fullpath, *freepath;
4508 * Must be super user
4510 if ((error = priv_check(td, PRIV_ROOT)))
4513 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4516 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4520 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4525 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4530 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4533 error = mount_path(p, mp, &fullpath, &freepath);
4536 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4537 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4538 kfree(freepath, M_TEMP);
4540 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4541 if (priv_check(td, PRIV_ROOT)) {
4542 bcopy(sp, &sb, sizeof(sb));
4543 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4546 error = copyout(sp, uap->buf, sizeof(*sp));
4552 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4555 sys_fhstatvfs(struct fhstatvfs_args *uap)
4557 struct thread *td = curthread;
4558 struct proc *p = td->td_proc;
4566 * Must be super user
4568 if ((error = priv_check(td, PRIV_ROOT)))
4571 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4574 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4578 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4583 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4586 sp = &mp->mnt_vstat;
4588 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
4592 if (mp->mnt_flag & MNT_RDONLY)
4593 sp->f_flag |= ST_RDONLY;
4594 if (mp->mnt_flag & MNT_NOSUID)
4595 sp->f_flag |= ST_NOSUID;
4596 error = copyout(sp, uap->buf, sizeof(*sp));
4603 * Syscall to push extended attribute configuration information into the
4604 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4605 * a command (int cmd), and attribute name and misc data. For now, the
4606 * attribute name is left in userspace for consumption by the VFS_op.
4607 * It will probably be changed to be copied into sysspace by the
4608 * syscall in the future, once issues with various consumers of the
4609 * attribute code have raised their hands.
4611 * Currently this is used only by UFS Extended Attributes.
4614 sys_extattrctl(struct extattrctl_args *uap)
4616 struct nlookupdata nd;
4618 char attrname[EXTATTR_MAXNAMELEN];
4626 if (error == 0 && uap->filename) {
4627 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
4630 error = nlookup(&nd);
4632 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4636 if (error == 0 && uap->attrname) {
4637 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4642 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4644 error = nlookup(&nd);
4646 error = ncp_writechk(&nd.nl_nch);
4648 error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
4650 uap->attrname, nd.nl_cred);
4659 * Syscall to get a named extended attribute on a file or directory.
4662 sys_extattr_set_file(struct extattr_set_file_args *uap)
4664 char attrname[EXTATTR_MAXNAMELEN];
4665 struct nlookupdata nd;
4671 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4677 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4679 error = nlookup(&nd);
4681 error = ncp_writechk(&nd.nl_nch);
4683 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4689 bzero(&auio, sizeof(auio));
4690 aiov.iov_base = uap->data;
4691 aiov.iov_len = uap->nbytes;
4692 auio.uio_iov = &aiov;
4693 auio.uio_iovcnt = 1;
4694 auio.uio_offset = 0;
4695 auio.uio_resid = uap->nbytes;
4696 auio.uio_rw = UIO_WRITE;
4697 auio.uio_td = curthread;
4699 error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
4708 * Syscall to get a named extended attribute on a file or directory.
4711 sys_extattr_get_file(struct extattr_get_file_args *uap)
4713 char attrname[EXTATTR_MAXNAMELEN];
4714 struct nlookupdata nd;
4720 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4726 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4728 error = nlookup(&nd);
4730 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_SHARED, &vp);
4736 bzero(&auio, sizeof(auio));
4737 aiov.iov_base = uap->data;
4738 aiov.iov_len = uap->nbytes;
4739 auio.uio_iov = &aiov;
4740 auio.uio_iovcnt = 1;
4741 auio.uio_offset = 0;
4742 auio.uio_resid = uap->nbytes;
4743 auio.uio_rw = UIO_READ;
4744 auio.uio_td = curthread;
4746 error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
4748 uap->sysmsg_result = uap->nbytes - auio.uio_resid;
4756 * Syscall to delete a named extended attribute from a file or directory.
4757 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4760 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4762 char attrname[EXTATTR_MAXNAMELEN];
4763 struct nlookupdata nd;
4767 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4771 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4773 error = nlookup(&nd);
4775 error = ncp_writechk(&nd.nl_nch);
4777 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4779 error = VOP_SETEXTATTR(vp, uap->attrnamespace,
4780 attrname, NULL, nd.nl_cred);
4789 * Determine if the mount is visible to the process.
4792 chroot_visible_mnt(struct mount *mp, struct proc *p)
4794 struct nchandle nch;
4797 * Traverse from the mount point upwards. If we hit the process
4798 * root then the mount point is visible to the process.
4800 nch = mp->mnt_ncmountpt;
4802 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4803 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4806 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4807 nch = nch.mount->mnt_ncmounton;
4809 nch.ncp = nch.ncp->nc_parent;
4814 * If the mount point is not visible to the process, but the
4815 * process root is in a subdirectory of the mount, return
4818 if (p->p_fd->fd_nrdir.mount == mp)