2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
35 * $FreeBSD: src/sys/kern/vfs_syscalls.c,v 1.151.2.18 2003/04/04 20:35:58 tegge Exp $
38 #include <sys/param.h>
39 #include <sys/systm.h>
42 #include <sys/sysent.h>
43 #include <sys/malloc.h>
44 #include <sys/mount.h>
45 #include <sys/mountctl.h>
46 #include <sys/sysproto.h>
47 #include <sys/filedesc.h>
48 #include <sys/kernel.h>
49 #include <sys/fcntl.h>
51 #include <sys/linker.h>
53 #include <sys/unistd.h>
54 #include <sys/vnode.h>
58 #include <sys/namei.h>
59 #include <sys/nlookup.h>
60 #include <sys/dirent.h>
61 #include <sys/extattr.h>
62 #include <sys/spinlock.h>
63 #include <sys/kern_syscall.h>
64 #include <sys/objcache.h>
65 #include <sys/sysctl.h>
68 #include <sys/file2.h>
69 #include <sys/spinlock2.h>
70 #include <sys/mplock2.h>
73 #include <vm/vm_object.h>
74 #include <vm/vm_page.h>
76 #include <machine/limits.h>
77 #include <machine/stdarg.h>
79 static void mount_warning(struct mount *mp, const char *ctl, ...)
81 static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb);
82 static int checkvp_chdir (struct vnode *vn, struct thread *td);
83 static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch);
84 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
85 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
86 static int getutimes (struct timeval *, struct timespec *);
87 static int getutimens (const struct timespec *, struct timespec *, int *);
88 static int setfown (struct mount *, struct vnode *, uid_t, gid_t);
89 static int setfmode (struct vnode *, int);
90 static int setfflags (struct vnode *, int);
91 static int setutimes (struct vnode *, struct vattr *,
92 const struct timespec *, int);
93 static int usermount = 0; /* if 1, non-root can mount fs. */
95 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
96 "Allow non-root users to mount filesystems");
99 * Virtual File System System Calls
103 * Mount a file system.
105 * mount_args(char *type, char *path, int flags, caddr_t data)
110 sys_mount(struct mount_args *uap)
112 struct thread *td = curthread;
115 struct mount *mp, *nullmp;
116 struct vfsconf *vfsp;
117 int error, flag = 0, flag2 = 0;
120 struct nlookupdata nd;
121 char fstypename[MFSNAMELEN];
129 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
133 * Do not allow NFS export by non-root users.
135 if (uap->flags & MNT_EXPORTED) {
136 error = priv_check(td, PRIV_ROOT);
141 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
143 if (priv_check(td, PRIV_ROOT))
144 uap->flags |= MNT_NOSUID | MNT_NODEV;
147 * Lookup the requested path and extract the nch and vnode.
149 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
151 if ((error = nlookup(&nd)) == 0) {
152 if (nd.nl_nch.ncp->nc_vp == NULL)
162 * If the target filesystem is resolved via a nullfs mount, then
163 * nd.nl_nch.mount will be pointing to the nullfs mount structure
164 * instead of the target file system. We need it in case we are
167 nullmp = nd.nl_nch.mount;
170 * Extract the locked+refd ncp and cleanup the nd structure
173 cache_zero(&nd.nl_nch);
176 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
177 (mp = cache_findmount(&nch)) != NULL) {
186 * now we have the locked ref'd nch and unreferenced vnode.
189 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
196 * Extract the file system type. We need to know this early, to take
197 * appropriate actions if we are dealing with a nullfs.
199 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
206 * Now we have an unlocked ref'd nch and a locked ref'd vp
208 if (uap->flags & MNT_UPDATE) {
209 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
216 if (strncmp(fstypename, "null", 5) == 0) {
224 flag2 = mp->mnt_kern_flag;
226 * We only allow the filesystem to be reloaded if it
227 * is currently mounted read-only.
229 if ((uap->flags & MNT_RELOAD) &&
230 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
233 error = EOPNOTSUPP; /* Needs translation */
237 * Only root, or the user that did the original mount is
238 * permitted to update it.
240 if (mp->mnt_stat.f_owner != cred->cr_uid &&
241 (error = priv_check(td, PRIV_ROOT))) {
246 if (vfs_busy(mp, LK_NOWAIT)) {
260 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
261 lwkt_gettoken(&mp->mnt_token);
267 * If the user is not root, ensure that they own the directory
268 * onto which we are attempting to mount.
270 if ((error = VOP_GETATTR(vp, &va)) ||
271 (va.va_uid != cred->cr_uid &&
272 (error = priv_check(td, PRIV_ROOT)))) {
277 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
282 if (vp->v_type != VDIR) {
288 if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
294 vfsp = vfsconf_find_by_name(fstypename);
298 /* Only load modules for root (very important!) */
299 if ((error = priv_check(td, PRIV_ROOT)) != 0) {
304 error = linker_load_file(fstypename, &lf);
305 if (error || lf == NULL) {
313 /* lookup again, see if the VFS was loaded */
314 vfsp = vfsconf_find_by_name(fstypename);
317 linker_file_unload(lf);
332 * Allocate and initialize the filesystem.
334 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
336 vfs_busy(mp, LK_NOWAIT);
337 mp->mnt_op = vfsp->vfc_vfsops;
339 mp->mnt_pbuf_count = nswbuf_kva / NSWBUF_SPLIT;
340 vfsp->vfc_refcount++;
341 mp->mnt_stat.f_type = vfsp->vfc_typenum;
342 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
343 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
344 mp->mnt_stat.f_owner = cred->cr_uid;
345 lwkt_gettoken(&mp->mnt_token);
349 * (per-mount token acquired at this point)
351 * Set the mount level flags.
353 if (uap->flags & MNT_RDONLY)
354 mp->mnt_flag |= MNT_RDONLY;
355 else if (mp->mnt_flag & MNT_RDONLY)
356 mp->mnt_kern_flag |= MNTK_WANTRDWR;
357 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
358 MNT_SYNCHRONOUS | MNT_ASYNC | MNT_NOATIME |
359 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
360 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR |
362 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
363 MNT_NODEV | MNT_SYNCHRONOUS | MNT_ASYNC | MNT_FORCE |
364 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
365 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);
410 mp->mnt_ncmounton = nch; /* inherits ref */
412 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
414 cache_ismounting(mp);
415 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
417 mountlist_insert(mp, MNTINS_LAST);
419 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
420 error = vfs_allocate_syncvnode(mp);
421 lwkt_reltoken(&mp->mnt_token);
423 error = VFS_START(mp, 0);
425 KNOTE(&fs_klist, VQ_MOUNT);
427 vn_syncer_thr_stop(mp);
428 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
429 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
430 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
431 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
432 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
433 mp->mnt_vfc->vfc_refcount--;
434 lwkt_reltoken(&mp->mnt_token);
445 * Scan all active processes to see if any of them have a current
446 * or root directory onto which the new filesystem has just been
447 * mounted. If so, replace them with the new mount point.
449 * Both old_nch and new_nch are ref'd on call but not locked.
450 * new_nch must be temporarily locked so it can be associated with the
451 * vnode representing the root of the mount point.
453 struct checkdirs_info {
454 struct nchandle old_nch;
455 struct nchandle new_nch;
456 struct vnode *old_vp;
457 struct vnode *new_vp;
460 static int checkdirs_callback(struct proc *p, void *data);
463 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
465 struct checkdirs_info info;
471 * If the old mount point's vnode has a usecount of 1, it is not
472 * being held as a descriptor anywhere.
474 olddp = old_nch->ncp->nc_vp;
475 if (olddp == NULL || VREFCNT(olddp) == 1)
479 * Force the root vnode of the new mount point to be resolved
480 * so we can update any matching processes.
483 if (VFS_ROOT(mp, &newdp))
484 panic("mount: lost mount");
487 vn_lock(newdp, LK_EXCLUSIVE | LK_RETRY);
488 cache_setunresolved(new_nch);
489 cache_setvp(new_nch, newdp);
490 cache_unlock(new_nch);
493 * Special handling of the root node
495 if (rootvnode == olddp) {
497 vfs_cache_setroot(newdp, cache_hold(new_nch));
501 * Pass newdp separately so the callback does not have to access
502 * it via new_nch->ncp->nc_vp.
504 info.old_nch = *old_nch;
505 info.new_nch = *new_nch;
507 allproc_scan(checkdirs_callback, &info);
512 * NOTE: callback is not MP safe because the scanned process's filedesc
513 * structure can be ripped out from under us, amoung other things.
516 checkdirs_callback(struct proc *p, void *data)
518 struct checkdirs_info *info = data;
519 struct filedesc *fdp;
520 struct nchandle ncdrop1;
521 struct nchandle ncdrop2;
522 struct vnode *vprele1;
523 struct vnode *vprele2;
525 if ((fdp = p->p_fd) != NULL) {
526 cache_zero(&ncdrop1);
527 cache_zero(&ncdrop2);
532 * MPUNSAFE - XXX fdp can be pulled out from under a
535 * A shared filedesc is ok, we don't have to copy it
536 * because we are making this change globally.
538 spin_lock(&fdp->fd_spin);
539 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
540 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
541 vprele1 = fdp->fd_cdir;
543 fdp->fd_cdir = info->new_vp;
544 ncdrop1 = fdp->fd_ncdir;
545 cache_copy(&info->new_nch, &fdp->fd_ncdir);
547 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
548 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
549 vprele2 = fdp->fd_rdir;
551 fdp->fd_rdir = info->new_vp;
552 ncdrop2 = fdp->fd_nrdir;
553 cache_copy(&info->new_nch, &fdp->fd_nrdir);
555 spin_unlock(&fdp->fd_spin);
557 cache_drop(&ncdrop1);
559 cache_drop(&ncdrop2);
569 * Unmount a file system.
571 * Note: unmount takes a path to the vnode mounted on as argument,
572 * not special file (as before).
574 * umount_args(char *path, int flags)
579 sys_unmount(struct unmount_args *uap)
581 struct thread *td = curthread;
582 struct proc *p __debugvar = td->td_proc;
583 struct mount *mp = NULL;
584 struct nlookupdata nd;
589 if (td->td_ucred->cr_prison != NULL) {
593 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
596 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
598 error = nlookup(&nd);
602 mp = nd.nl_nch.mount;
605 * Only root, or the user that did the original mount is
606 * permitted to unmount this filesystem.
608 if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) &&
609 (error = priv_check(td, PRIV_ROOT)))
613 * Don't allow unmounting the root file system.
615 if (mp->mnt_flag & MNT_ROOTFS) {
621 * Must be the root of the filesystem
623 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
631 error = dounmount(mp, uap->flags);
638 * Do the actual file system unmount.
641 dounmount_interlock(struct mount *mp)
643 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
645 mp->mnt_kern_flag |= MNTK_UNMOUNT;
650 unmount_allproc_cb(struct proc *p, void *arg)
654 if (p->p_textnch.ncp == NULL)
657 mp = (struct mount *)arg;
658 if (p->p_textnch.mount == mp)
659 cache_drop(&p->p_textnch);
665 dounmount(struct mount *mp, int flags)
667 struct namecache *ncp;
676 lwkt_gettoken(&mp->mnt_token);
678 * Exclusive access for unmounting purposes
680 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
684 * Allow filesystems to detect that a forced unmount is in progress.
686 if (flags & MNT_FORCE)
687 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
688 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_TIMELOCK);
689 error = lockmgr(&mp->mnt_lock, lflags);
691 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
692 if (mp->mnt_kern_flag & MNTK_MWAIT) {
693 mp->mnt_kern_flag &= ~MNTK_MWAIT;
699 if (mp->mnt_flag & MNT_EXPUBLIC)
700 vfs_setpublicfs(NULL, NULL, NULL);
702 vfs_msync(mp, MNT_WAIT);
703 async_flag = mp->mnt_flag & MNT_ASYNC;
704 mp->mnt_flag &=~ MNT_ASYNC;
707 * If this filesystem isn't aliasing other filesystems,
708 * try to invalidate any remaining namecache entries and
709 * check the count afterwords.
711 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
712 cache_lock(&mp->mnt_ncmountpt);
713 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
714 cache_unlock(&mp->mnt_ncmountpt);
716 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
717 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
718 allproc_scan(&unmount_allproc_cb, mp);
721 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
722 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
724 if ((flags & MNT_FORCE) == 0) {
726 mount_warning(mp, "Cannot unmount: "
732 mount_warning(mp, "Forced unmount: "
743 * Decomission our special mnt_syncer vnode. This also stops
744 * the vnlru code. If we are unable to unmount we recommission
747 * Then sync the filesystem.
749 if ((vp = mp->mnt_syncer) != NULL) {
750 mp->mnt_syncer = NULL;
751 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
754 if ((mp->mnt_flag & MNT_RDONLY) == 0)
755 VFS_SYNC(mp, MNT_WAIT);
758 * nchandle records ref the mount structure. Expect a count of 1
759 * (our mount->mnt_ncmountpt).
761 * Scans can get temporary refs on a mountpoint (thought really
762 * heavy duty stuff like cache_findmount() do not).
764 for (retry = 0; retry < 10 && mp->mnt_refs != 1; ++retry) {
765 cache_unmounting(mp);
766 tsleep(&mp->mnt_refs, 0, "mntbsy", hz / 10 + 1);
768 if (mp->mnt_refs != 1) {
769 if ((flags & MNT_FORCE) == 0) {
770 mount_warning(mp, "Cannot unmount: "
771 "%d mount refs still present",
775 mount_warning(mp, "Forced unmount: "
776 "%d mount refs still present",
783 * So far so good, sync the filesystem once more and
784 * call the VFS unmount code if the sync succeeds.
787 if (mp->mnt_flag & MNT_RDONLY) {
788 error = VFS_UNMOUNT(mp, flags);
790 error = VFS_SYNC(mp, MNT_WAIT);
792 (error == EOPNOTSUPP) || /* No sync */
793 (flags & MNT_FORCE)) {
794 error = VFS_UNMOUNT(mp, flags);
800 * If an error occurred we can still recover, restoring the
801 * syncer vnode and misc flags.
804 if (mp->mnt_syncer == NULL)
805 vfs_allocate_syncvnode(mp);
806 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
807 mp->mnt_flag |= async_flag;
808 lockmgr(&mp->mnt_lock, LK_RELEASE);
809 if (mp->mnt_kern_flag & MNTK_MWAIT) {
810 mp->mnt_kern_flag &= ~MNTK_MWAIT;
816 * Clean up any journals still associated with the mount after
817 * filesystem activity has ceased.
819 journal_remove_all_journals(mp,
820 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
822 mountlist_remove(mp);
825 * Remove any installed vnode ops here so the individual VFSs don't
828 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
829 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
830 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
831 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
832 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
834 if (mp->mnt_ncmountpt.ncp != NULL) {
835 nch = mp->mnt_ncmountpt;
836 cache_zero(&mp->mnt_ncmountpt);
837 cache_clrmountpt(&nch);
840 if (mp->mnt_ncmounton.ncp != NULL) {
841 cache_unmounting(mp);
842 nch = mp->mnt_ncmounton;
843 cache_zero(&mp->mnt_ncmounton);
844 cache_clrmountpt(&nch);
848 mp->mnt_vfc->vfc_refcount--;
849 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
850 panic("unmount: dangling vnode");
851 lockmgr(&mp->mnt_lock, LK_RELEASE);
852 if (mp->mnt_kern_flag & MNTK_MWAIT) {
853 mp->mnt_kern_flag &= ~MNTK_MWAIT;
858 * If we reach here and freeok != 0 we must free the mount.
859 * If refs > 1 cycle and wait, just in case someone tried
860 * to busy the mount after we decided to do the unmount.
863 while (mp->mnt_refs > 1) {
864 cache_unmounting(mp);
866 tsleep(&mp->mnt_refs, 0, "umntrwait", hz / 10 + 1);
868 lwkt_reltoken(&mp->mnt_token);
873 KNOTE(&fs_klist, VQ_UNMOUNT);
876 lwkt_reltoken(&mp->mnt_token);
882 mount_warning(struct mount *mp, const char *ctl, ...)
889 if (cache_fullpath(NULL, &mp->mnt_ncmounton, NULL,
890 &ptr, &buf, 0) == 0) {
891 kprintf("unmount(%s): ", ptr);
896 kprintf("unmount(%p", mp);
897 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
898 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
907 * Shim cache_fullpath() to handle the case where a process is chrooted into
908 * a subdirectory of a mount. In this case if the root mount matches the
909 * process root directory's mount we have to specify the process's root
910 * directory instead of the mount point, because the mount point might
911 * be above the root directory.
915 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
917 struct nchandle *nch;
919 if (p && p->p_fd->fd_nrdir.mount == mp)
920 nch = &p->p_fd->fd_nrdir;
922 nch = &mp->mnt_ncmountpt;
923 return(cache_fullpath(p, nch, NULL, rb, fb, 0));
927 * Sync each mounted filesystem.
931 static int syncprt = 0;
932 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
935 static int sync_callback(struct mount *mp, void *data);
938 sys_sync(struct sync_args *uap)
940 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
946 sync_callback(struct mount *mp, void *data __unused)
950 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
951 asyncflag = mp->mnt_flag & MNT_ASYNC;
952 mp->mnt_flag &= ~MNT_ASYNC;
953 vfs_msync(mp, MNT_NOWAIT);
954 VFS_SYNC(mp, MNT_NOWAIT);
955 mp->mnt_flag |= asyncflag;
960 /* XXX PRISON: could be per prison flag */
961 static int prison_quotas;
963 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
967 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
969 * Change filesystem quotas.
974 sys_quotactl(struct quotactl_args *uap)
976 struct nlookupdata nd;
983 if (td->td_ucred->cr_prison && !prison_quotas) {
988 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
990 error = nlookup(&nd);
992 mp = nd.nl_nch.mount;
993 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
994 uap->arg, nd.nl_cred);
1003 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
1004 * void *buf, int buflen)
1006 * This function operates on a mount point and executes the specified
1007 * operation using the specified control data, and possibly returns data.
1009 * The actual number of bytes stored in the result buffer is returned, 0
1010 * if none, otherwise an error is returned.
1015 sys_mountctl(struct mountctl_args *uap)
1017 struct thread *td = curthread;
1018 struct proc *p = td->td_proc;
1026 * Sanity and permissions checks. We must be root.
1029 if (td->td_ucred->cr_prison != NULL)
1031 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
1032 (error = priv_check(td, PRIV_ROOT)) != 0)
1036 * Argument length checks
1038 if (uap->ctllen < 0 || uap->ctllen > 1024)
1040 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
1042 if (uap->path == NULL)
1046 * Allocate the necessary buffers and copyin data
1048 path = objcache_get(namei_oc, M_WAITOK);
1049 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1054 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
1055 error = copyin(uap->ctl, ctl, uap->ctllen);
1060 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
1063 * Validate the descriptor
1066 fp = holdfp(p->p_fd, uap->fd, -1);
1076 * Execute the internal kernel function and clean up.
1079 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
1083 if (error == 0 && uap->sysmsg_result > 0)
1084 error = copyout(buf, uap->buf, uap->sysmsg_result);
1087 objcache_put(namei_oc, path);
1096 * Execute a mount control operation by resolving the path to a mount point
1097 * and calling vop_mountctl().
1099 * Use the mount point from the nch instead of the vnode so nullfs mounts
1100 * can properly spike the VOP.
1103 kern_mountctl(const char *path, int op, struct file *fp,
1104 const void *ctl, int ctllen,
1105 void *buf, int buflen, int *res)
1108 struct nlookupdata nd;
1109 struct nchandle nch;
1115 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1118 error = nlookup(&nd);
1123 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1130 * Yes, all this is needed to use the nch.mount below, because
1131 * we must maintain a ref on the mount to avoid ripouts (e.g.
1132 * due to heavy mount/unmount use by synth or poudriere).
1135 cache_zero(&nd.nl_nch);
1143 * Must be the root of the filesystem
1145 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1150 if (mp == NULL || mp->mnt_kern_flag & MNTK_UNMOUNT) {
1151 kprintf("kern_mountctl: Warning, \"%s\" racing unmount\n",
1157 error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen,
1166 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1168 struct thread *td = curthread;
1169 struct proc *p = td->td_proc;
1172 char *fullpath, *freepath;
1175 if ((error = nlookup(nd)) != 0)
1177 mp = nd->nl_nch.mount;
1179 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1182 error = mount_path(p, mp, &fullpath, &freepath);
1185 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1186 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1187 kfree(freepath, M_TEMP);
1189 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1190 bcopy(sp, buf, sizeof(*buf));
1191 /* Only root should have access to the fsid's. */
1192 if (priv_check(td, PRIV_ROOT))
1193 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1198 * statfs_args(char *path, struct statfs *buf)
1200 * Get filesystem statistics.
1203 sys_statfs(struct statfs_args *uap)
1205 struct nlookupdata nd;
1209 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1211 error = kern_statfs(&nd, &buf);
1214 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1219 kern_fstatfs(int fd, struct statfs *buf)
1221 struct thread *td = curthread;
1222 struct proc *p = td->td_proc;
1226 char *fullpath, *freepath;
1230 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1234 * Try to use mount info from any overlays rather than the
1235 * mount info for the underlying vnode, otherwise we will
1236 * fail when operating on null-mounted paths inside a chroot.
1238 if ((mp = fp->f_nchandle.mount) == NULL)
1239 mp = ((struct vnode *)fp->f_data)->v_mount;
1244 if (fp->f_cred == NULL) {
1249 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1252 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1254 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1255 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1256 kfree(freepath, M_TEMP);
1258 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1259 bcopy(sp, buf, sizeof(*buf));
1261 /* Only root should have access to the fsid's. */
1262 if (priv_check(td, PRIV_ROOT))
1263 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1271 * fstatfs_args(int fd, struct statfs *buf)
1273 * Get filesystem statistics.
1276 sys_fstatfs(struct fstatfs_args *uap)
1281 error = kern_fstatfs(uap->fd, &buf);
1284 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1289 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1295 if ((error = nlookup(nd)) != 0)
1297 mp = nd->nl_nch.mount;
1298 sp = &mp->mnt_vstat;
1299 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1303 if (mp->mnt_flag & MNT_RDONLY)
1304 sp->f_flag |= ST_RDONLY;
1305 if (mp->mnt_flag & MNT_NOSUID)
1306 sp->f_flag |= ST_NOSUID;
1307 bcopy(sp, buf, sizeof(*buf));
1312 * statfs_args(char *path, struct statfs *buf)
1314 * Get filesystem statistics.
1317 sys_statvfs(struct statvfs_args *uap)
1319 struct nlookupdata nd;
1323 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1325 error = kern_statvfs(&nd, &buf);
1328 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1333 kern_fstatvfs(int fd, struct statvfs *buf)
1335 struct thread *td = curthread;
1336 struct proc *p = td->td_proc;
1343 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1345 if ((mp = fp->f_nchandle.mount) == NULL)
1346 mp = ((struct vnode *)fp->f_data)->v_mount;
1351 if (fp->f_cred == NULL) {
1355 sp = &mp->mnt_vstat;
1356 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1360 if (mp->mnt_flag & MNT_RDONLY)
1361 sp->f_flag |= ST_RDONLY;
1362 if (mp->mnt_flag & MNT_NOSUID)
1363 sp->f_flag |= ST_NOSUID;
1365 bcopy(sp, buf, sizeof(*buf));
1373 * fstatfs_args(int fd, struct statfs *buf)
1375 * Get filesystem statistics.
1378 sys_fstatvfs(struct fstatvfs_args *uap)
1383 error = kern_fstatvfs(uap->fd, &buf);
1386 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1391 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1393 * Get statistics on all filesystems.
1396 struct getfsstat_info {
1397 struct statfs *sfsp;
1405 static int getfsstat_callback(struct mount *, void *);
1408 sys_getfsstat(struct getfsstat_args *uap)
1410 struct thread *td = curthread;
1411 struct getfsstat_info info;
1413 bzero(&info, sizeof(info));
1415 info.maxcount = uap->bufsize / sizeof(struct statfs);
1416 info.sfsp = uap->buf;
1418 info.flags = uap->flags;
1421 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1422 if (info.sfsp && info.count > info.maxcount)
1423 uap->sysmsg_result = info.maxcount;
1425 uap->sysmsg_result = info.count;
1426 return (info.error);
1430 getfsstat_callback(struct mount *mp, void *data)
1432 struct getfsstat_info *info = data;
1438 if (info->sfsp && info->count < info->maxcount) {
1439 if (info->td->td_proc &&
1440 !chroot_visible_mnt(mp, info->td->td_proc)) {
1446 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1447 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1448 * overrides MNT_WAIT.
1450 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1451 (info->flags & MNT_WAIT)) &&
1452 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1455 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1457 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1459 info->error = error;
1462 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1463 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1464 kfree(freepath, M_TEMP);
1466 error = copyout(sp, info->sfsp, sizeof(*sp));
1468 info->error = error;
1478 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1479 long bufsize, int flags)
1481 * Get statistics on all filesystems.
1484 struct getvfsstat_info {
1485 struct statfs *sfsp;
1486 struct statvfs *vsfsp;
1494 static int getvfsstat_callback(struct mount *, void *);
1497 sys_getvfsstat(struct getvfsstat_args *uap)
1499 struct thread *td = curthread;
1500 struct getvfsstat_info info;
1502 bzero(&info, sizeof(info));
1504 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1505 info.sfsp = uap->buf;
1506 info.vsfsp = uap->vbuf;
1508 info.flags = uap->flags;
1511 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1512 if (info.vsfsp && info.count > info.maxcount)
1513 uap->sysmsg_result = info.maxcount;
1515 uap->sysmsg_result = info.count;
1516 return (info.error);
1520 getvfsstat_callback(struct mount *mp, void *data)
1522 struct getvfsstat_info *info = data;
1524 struct statvfs *vsp;
1529 if (info->vsfsp && info->count < info->maxcount) {
1530 if (info->td->td_proc &&
1531 !chroot_visible_mnt(mp, info->td->td_proc)) {
1535 vsp = &mp->mnt_vstat;
1538 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1539 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1540 * overrides MNT_WAIT.
1542 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1543 (info->flags & MNT_WAIT)) &&
1544 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1547 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1549 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1550 (info->flags & MNT_WAIT)) &&
1551 (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) {
1555 if (mp->mnt_flag & MNT_RDONLY)
1556 vsp->f_flag |= ST_RDONLY;
1557 if (mp->mnt_flag & MNT_NOSUID)
1558 vsp->f_flag |= ST_NOSUID;
1560 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1562 info->error = error;
1565 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1566 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1567 kfree(freepath, M_TEMP);
1569 error = copyout(sp, info->sfsp, sizeof(*sp));
1571 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1573 info->error = error;
1585 * fchdir_args(int fd)
1587 * Change current working directory to a given file descriptor.
1590 sys_fchdir(struct fchdir_args *uap)
1592 struct thread *td = curthread;
1593 struct proc *p = td->td_proc;
1594 struct filedesc *fdp = p->p_fd;
1595 struct vnode *vp, *ovp;
1598 struct nchandle nch, onch, tnch;
1601 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1603 lwkt_gettoken(&p->p_token);
1604 vp = (struct vnode *)fp->f_data;
1606 vn_lock(vp, LK_SHARED | LK_RETRY);
1607 if (fp->f_nchandle.ncp == NULL)
1610 error = checkvp_chdir(vp, td);
1615 cache_copy(&fp->f_nchandle, &nch);
1618 * If the ncp has become a mount point, traverse through
1622 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1623 (mp = cache_findmount(&nch)) != NULL
1625 error = nlookup_mp(mp, &tnch);
1627 cache_unlock(&tnch); /* leave ref intact */
1629 vp = tnch.ncp->nc_vp;
1630 error = vget(vp, LK_SHARED);
1631 KKASSERT(error == 0);
1635 cache_dropmount(mp);
1639 onch = fdp->fd_ncdir;
1640 vn_unlock(vp); /* leave ref intact */
1642 fdp->fd_ncdir = nch;
1651 lwkt_reltoken(&p->p_token);
1656 kern_chdir(struct nlookupdata *nd)
1658 struct thread *td = curthread;
1659 struct proc *p = td->td_proc;
1660 struct filedesc *fdp = p->p_fd;
1661 struct vnode *vp, *ovp;
1662 struct nchandle onch;
1665 nd->nl_flags |= NLC_SHAREDLOCK;
1666 if ((error = nlookup(nd)) != 0)
1668 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1670 if ((error = vget(vp, LK_SHARED)) != 0)
1673 lwkt_gettoken(&p->p_token);
1674 error = checkvp_chdir(vp, td);
1678 onch = fdp->fd_ncdir;
1679 cache_unlock(&nd->nl_nch); /* leave reference intact */
1680 fdp->fd_ncdir = nd->nl_nch;
1684 cache_zero(&nd->nl_nch);
1688 lwkt_reltoken(&p->p_token);
1693 * chdir_args(char *path)
1695 * Change current working directory (``.'').
1698 sys_chdir(struct chdir_args *uap)
1700 struct nlookupdata nd;
1703 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1705 error = kern_chdir(&nd);
1711 * Helper function for raised chroot(2) security function: Refuse if
1712 * any filedescriptors are open directories.
1715 chroot_refuse_vdir_fds(struct filedesc *fdp)
1722 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1723 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1725 vp = (struct vnode *)fp->f_data;
1726 if (vp->v_type != VDIR) {
1737 * This sysctl determines if we will allow a process to chroot(2) if it
1738 * has a directory open:
1739 * 0: disallowed for all processes.
1740 * 1: allowed for processes that were not already chroot(2)'ed.
1741 * 2: allowed for all processes.
1744 static int chroot_allow_open_directories = 1;
1746 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1747 &chroot_allow_open_directories, 0, "");
1750 * chroot to the specified namecache entry. We obtain the vp from the
1751 * namecache data. The passed ncp must be locked and referenced and will
1752 * remain locked and referenced on return.
1755 kern_chroot(struct nchandle *nch)
1757 struct thread *td = curthread;
1758 struct proc *p = td->td_proc;
1759 struct filedesc *fdp = p->p_fd;
1764 * Only privileged user can chroot
1766 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1771 * Disallow open directory descriptors (fchdir() breakouts).
1773 if (chroot_allow_open_directories == 0 ||
1774 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1775 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1778 if ((vp = nch->ncp->nc_vp) == NULL)
1781 if ((error = vget(vp, LK_SHARED)) != 0)
1785 * Check the validity of vp as a directory to change to and
1786 * associate it with rdir/jdir.
1788 error = checkvp_chdir(vp, td);
1789 vn_unlock(vp); /* leave reference intact */
1791 vrele(fdp->fd_rdir);
1792 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1793 cache_drop(&fdp->fd_nrdir);
1794 cache_copy(nch, &fdp->fd_nrdir);
1795 if (fdp->fd_jdir == NULL) {
1798 cache_copy(nch, &fdp->fd_njdir);
1807 * chroot_args(char *path)
1809 * Change notion of root (``/'') directory.
1812 sys_chroot(struct chroot_args *uap)
1814 struct thread *td __debugvar = curthread;
1815 struct nlookupdata nd;
1818 KKASSERT(td->td_proc);
1819 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1821 nd.nl_flags |= NLC_EXEC;
1822 error = nlookup(&nd);
1824 error = kern_chroot(&nd.nl_nch);
1831 sys_chroot_kernel(struct chroot_kernel_args *uap)
1833 struct thread *td = curthread;
1834 struct nlookupdata nd;
1835 struct nchandle *nch;
1839 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1843 error = nlookup(&nd);
1849 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1853 if ((vp = nch->ncp->nc_vp) == NULL) {
1858 if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0)
1861 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap->path);
1863 vfs_cache_setroot(vp, cache_hold(nch));
1873 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1874 * determine whether it is legal to chdir to the vnode. The vnode's state
1875 * is not changed by this call.
1878 checkvp_chdir(struct vnode *vp, struct thread *td)
1882 if (vp->v_type != VDIR)
1885 error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
1890 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1892 struct thread *td = curthread;
1893 struct proc *p = td->td_proc;
1894 struct lwp *lp = td->td_lwp;
1895 struct filedesc *fdp = p->p_fd;
1900 int type, indx, error = 0;
1903 if ((oflags & O_ACCMODE) == O_ACCMODE)
1905 flags = FFLAGS(oflags);
1906 error = falloc(lp, &nfp, NULL);
1910 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1913 * XXX p_dupfd is a real mess. It allows a device to return a
1914 * file descriptor to be duplicated rather then doing the open
1920 * Call vn_open() to do the lookup and assign the vnode to the
1921 * file pointer. vn_open() does not change the ref count on fp
1922 * and the vnode, on success, will be inherited by the file pointer
1925 * Request a shared lock on the vnode if possible.
1927 nd->nl_flags |= NLC_LOCKVP;
1928 if ((flags & (O_CREAT|O_TRUNC)) == 0)
1929 nd->nl_flags |= NLC_SHAREDLOCK;
1931 error = vn_open(nd, fp, flags, cmode);
1936 * handle special fdopen() case. bleh. dupfdopen() is
1937 * responsible for dropping the old contents of ofiles[indx]
1940 * Note that fsetfd() will add a ref to fp which represents
1941 * the fd_files[] assignment. We must still drop our
1944 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1945 if (fdalloc(p, 0, &indx) == 0) {
1946 error = dupfdopen(fdp, indx, lp->lwp_dupfd, flags, error);
1949 fdrop(fp); /* our ref */
1952 fsetfd(fdp, NULL, indx);
1955 fdrop(fp); /* our ref */
1956 if (error == ERESTART)
1962 * ref the vnode for ourselves so it can't be ripped out from under
1963 * is. XXX need an ND flag to request that the vnode be returned
1966 * Reserve a file descriptor but do not assign it until the open
1969 vp = (struct vnode *)fp->f_data;
1971 if ((error = fdalloc(p, 0, &indx)) != 0) {
1978 * If no error occurs the vp will have been assigned to the file
1983 if (flags & (O_EXLOCK | O_SHLOCK)) {
1984 lf.l_whence = SEEK_SET;
1987 if (flags & O_EXLOCK)
1988 lf.l_type = F_WRLCK;
1990 lf.l_type = F_RDLCK;
1991 if (flags & FNONBLOCK)
1996 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1998 * lock request failed. Clean up the reserved
2002 fsetfd(fdp, NULL, indx);
2006 atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */
2010 * Assert that all regular file vnodes were created with a object.
2012 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
2013 ("open: regular file has no backing object after vn_open"));
2019 * release our private reference, leaving the one associated with the
2020 * descriptor table intact.
2022 if (oflags & O_CLOEXEC)
2023 fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
2024 fsetfd(fdp, fp, indx);
2031 * open_args(char *path, int flags, int mode)
2033 * Check permissions, allocate an open file structure,
2034 * and call the device open routine if any.
2037 sys_open(struct open_args *uap)
2039 struct nlookupdata nd;
2042 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2044 error = kern_open(&nd, uap->flags,
2045 uap->mode, &uap->sysmsg_result);
2052 * openat_args(int fd, char *path, int flags, int mode)
2055 sys_openat(struct openat_args *uap)
2057 struct nlookupdata nd;
2061 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2063 error = kern_open(&nd, uap->flags, uap->mode,
2064 &uap->sysmsg_result);
2066 nlookup_done_at(&nd, fp);
2071 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
2073 struct thread *td = curthread;
2074 struct proc *p = td->td_proc;
2083 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2084 vattr.va_rmajor = rmajor;
2085 vattr.va_rminor = rminor;
2087 switch (mode & S_IFMT) {
2088 case S_IFMT: /* used by badsect to flag bad sectors */
2089 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
2090 vattr.va_type = VBAD;
2093 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2094 vattr.va_type = VCHR;
2097 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2098 vattr.va_type = VBLK;
2101 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2104 case S_IFDIR: /* special directories support for HAMMER */
2105 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2106 vattr.va_type = VDIR;
2117 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2118 if ((error = nlookup(nd)) != 0)
2120 if (nd->nl_nch.ncp->nc_vp)
2122 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2126 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2127 nd->nl_cred, NAMEI_CREATE);
2130 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2131 &vp, nd->nl_cred, &vattr);
2139 * mknod_args(char *path, int mode, int dev)
2141 * Create a special file.
2144 sys_mknod(struct mknod_args *uap)
2146 struct nlookupdata nd;
2149 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2151 error = kern_mknod(&nd, uap->mode,
2152 umajor(uap->dev), uminor(uap->dev));
2159 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2161 * Create a special file. The path is relative to the directory associated
2165 sys_mknodat(struct mknodat_args *uap)
2167 struct nlookupdata nd;
2171 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2173 error = kern_mknod(&nd, uap->mode,
2174 umajor(uap->dev), uminor(uap->dev));
2176 nlookup_done_at(&nd, fp);
2181 kern_mkfifo(struct nlookupdata *nd, int mode)
2183 struct thread *td = curthread;
2184 struct proc *p = td->td_proc;
2191 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2192 if ((error = nlookup(nd)) != 0)
2194 if (nd->nl_nch.ncp->nc_vp)
2196 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2200 vattr.va_type = VFIFO;
2201 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2203 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2210 * mkfifo_args(char *path, int mode)
2212 * Create a named pipe.
2215 sys_mkfifo(struct mkfifo_args *uap)
2217 struct nlookupdata nd;
2220 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2222 error = kern_mkfifo(&nd, uap->mode);
2228 * mkfifoat_args(int fd, char *path, mode_t mode)
2230 * Create a named pipe. The path is relative to the directory associated
2234 sys_mkfifoat(struct mkfifoat_args *uap)
2236 struct nlookupdata nd;
2240 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2242 error = kern_mkfifo(&nd, uap->mode);
2243 nlookup_done_at(&nd, fp);
2247 static int hardlink_check_uid = 0;
2248 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2249 &hardlink_check_uid, 0,
2250 "Unprivileged processes cannot create hard links to files owned by other "
2252 static int hardlink_check_gid = 0;
2253 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2254 &hardlink_check_gid, 0,
2255 "Unprivileged processes cannot create hard links to files owned by other "
2259 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2265 * Shortcut if disabled
2267 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2271 * Privileged user can always hardlink
2273 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2277 * Otherwise only if the originating file is owned by the
2278 * same user or group. Note that any group is allowed if
2279 * the file is owned by the caller.
2281 error = VOP_GETATTR(vp, &va);
2285 if (hardlink_check_uid) {
2286 if (cred->cr_uid != va.va_uid)
2290 if (hardlink_check_gid) {
2291 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2299 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2301 struct thread *td = curthread;
2306 * Lookup the source and obtained a locked vnode.
2308 * You may only hardlink a file which you have write permission
2309 * on or which you own.
2311 * XXX relookup on vget failure / race ?
2314 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2315 if ((error = nlookup(nd)) != 0)
2317 vp = nd->nl_nch.ncp->nc_vp;
2318 KKASSERT(vp != NULL);
2319 if (vp->v_type == VDIR)
2320 return (EPERM); /* POSIX */
2321 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2323 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2327 * Unlock the source so we can lookup the target without deadlocking
2328 * (XXX vp is locked already, possible other deadlock?). The target
2331 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2332 nd->nl_flags &= ~NLC_NCPISLOCKED;
2333 cache_unlock(&nd->nl_nch);
2336 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2337 if ((error = nlookup(linknd)) != 0) {
2341 if (linknd->nl_nch.ncp->nc_vp) {
2345 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
2352 * Finally run the new API VOP.
2354 error = can_hardlink(vp, td, td->td_ucred);
2356 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2357 vp, linknd->nl_cred);
2364 * link_args(char *path, char *link)
2366 * Make a hard file link.
2369 sys_link(struct link_args *uap)
2371 struct nlookupdata nd, linknd;
2374 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2376 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2378 error = kern_link(&nd, &linknd);
2379 nlookup_done(&linknd);
2386 * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags)
2388 * Make a hard file link. The path1 argument is relative to the directory
2389 * associated with fd1, and similarly the path2 argument is relative to
2390 * the directory associated with fd2.
2393 sys_linkat(struct linkat_args *uap)
2395 struct nlookupdata nd, linknd;
2396 struct file *fp1, *fp2;
2399 error = nlookup_init_at(&nd, &fp1, uap->fd1, uap->path1, UIO_USERSPACE,
2400 (uap->flags & AT_SYMLINK_FOLLOW) ? NLC_FOLLOW : 0);
2402 error = nlookup_init_at(&linknd, &fp2, uap->fd2,
2403 uap->path2, UIO_USERSPACE, 0);
2405 error = kern_link(&nd, &linknd);
2406 nlookup_done_at(&linknd, fp2);
2408 nlookup_done_at(&nd, fp1);
2413 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2421 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2422 if ((error = nlookup(nd)) != 0)
2424 if (nd->nl_nch.ncp->nc_vp)
2426 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2430 vattr.va_mode = mode;
2431 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2438 * symlink(char *path, char *link)
2440 * Make a symbolic link.
2443 sys_symlink(struct symlink_args *uap)
2445 struct thread *td = curthread;
2446 struct nlookupdata nd;
2451 path = objcache_get(namei_oc, M_WAITOK);
2452 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2454 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2456 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2457 error = kern_symlink(&nd, path, mode);
2461 objcache_put(namei_oc, path);
2466 * symlinkat_args(char *path1, int fd, char *path2)
2468 * Make a symbolic link. The path2 argument is relative to the directory
2469 * associated with fd.
2472 sys_symlinkat(struct symlinkat_args *uap)
2474 struct thread *td = curthread;
2475 struct nlookupdata nd;
2481 path1 = objcache_get(namei_oc, M_WAITOK);
2482 error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2484 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2487 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2488 error = kern_symlink(&nd, path1, mode);
2490 nlookup_done_at(&nd, fp);
2492 objcache_put(namei_oc, path1);
2497 * undelete_args(char *path)
2499 * Delete a whiteout from the filesystem.
2502 sys_undelete(struct undelete_args *uap)
2504 struct nlookupdata nd;
2507 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2509 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2511 error = nlookup(&nd);
2513 error = ncp_writechk(&nd.nl_nch);
2515 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2523 kern_unlink(struct nlookupdata *nd)
2528 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2529 if ((error = nlookup(nd)) != 0)
2531 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2533 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2538 * unlink_args(char *path)
2540 * Delete a name from the filesystem.
2543 sys_unlink(struct unlink_args *uap)
2545 struct nlookupdata nd;
2548 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2550 error = kern_unlink(&nd);
2557 * unlinkat_args(int fd, char *path, int flags)
2559 * Delete the file or directory entry pointed to by fd/path.
2562 sys_unlinkat(struct unlinkat_args *uap)
2564 struct nlookupdata nd;
2568 if (uap->flags & ~AT_REMOVEDIR)
2571 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2573 if (uap->flags & AT_REMOVEDIR)
2574 error = kern_rmdir(&nd);
2576 error = kern_unlink(&nd);
2578 nlookup_done_at(&nd, fp);
2583 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2585 struct thread *td = curthread;
2586 struct proc *p = td->td_proc;
2593 fp = holdfp(p->p_fd, fd, -1);
2596 if (fp->f_type != DTYPE_VNODE) {
2600 vp = (struct vnode *)fp->f_data;
2604 spin_lock(&fp->f_spin);
2605 new_offset = fp->f_offset + offset;
2609 error = VOP_GETATTR(vp, &vattr);
2610 spin_lock(&fp->f_spin);
2611 new_offset = offset + vattr.va_size;
2614 new_offset = offset;
2616 spin_lock(&fp->f_spin);
2621 spin_lock(&fp->f_spin);
2626 * Validate the seek position. Negative offsets are not allowed
2627 * for regular files or directories.
2629 * Normally we would also not want to allow negative offsets for
2630 * character and block-special devices. However kvm addresses
2631 * on 64 bit architectures might appear to be negative and must
2635 if (new_offset < 0 &&
2636 (vp->v_type == VREG || vp->v_type == VDIR)) {
2639 fp->f_offset = new_offset;
2642 *res = fp->f_offset;
2643 spin_unlock(&fp->f_spin);
2650 * lseek_args(int fd, int pad, off_t offset, int whence)
2652 * Reposition read/write file offset.
2655 sys_lseek(struct lseek_args *uap)
2659 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2660 &uap->sysmsg_offset);
2666 * Check if current process can access given file. amode is a bitmask of *_OK
2667 * access bits. flags is a bitmask of AT_* flags.
2670 kern_access(struct nlookupdata *nd, int amode, int flags)
2675 if (flags & ~AT_EACCESS)
2677 nd->nl_flags |= NLC_SHAREDLOCK;
2678 if ((error = nlookup(nd)) != 0)
2681 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
2685 /* Flags == 0 means only check for existence. */
2694 if ((mode & VWRITE) == 0 ||
2695 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2696 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2699 * If the file handle is stale we have to re-resolve the
2700 * entry with the ncp held exclusively. This is a hack
2703 if (error == ESTALE) {
2705 cache_unlock(&nd->nl_nch);
2706 cache_lock(&nd->nl_nch);
2707 cache_setunresolved(&nd->nl_nch);
2708 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2721 * access_args(char *path, int flags)
2723 * Check access permissions.
2726 sys_access(struct access_args *uap)
2728 struct nlookupdata nd;
2731 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2733 error = kern_access(&nd, uap->flags, 0);
2740 * eaccess_args(char *path, int flags)
2742 * Check access permissions.
2745 sys_eaccess(struct eaccess_args *uap)
2747 struct nlookupdata nd;
2750 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2752 error = kern_access(&nd, uap->flags, AT_EACCESS);
2759 * faccessat_args(int fd, char *path, int amode, int flags)
2761 * Check access permissions.
2764 sys_faccessat(struct faccessat_args *uap)
2766 struct nlookupdata nd;
2770 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2773 error = kern_access(&nd, uap->amode, uap->flags);
2774 nlookup_done_at(&nd, fp);
2779 kern_stat(struct nlookupdata *nd, struct stat *st)
2784 nd->nl_flags |= NLC_SHAREDLOCK;
2785 if ((error = nlookup(nd)) != 0)
2788 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2791 if ((error = vget(vp, LK_SHARED)) != 0)
2793 error = vn_stat(vp, st, nd->nl_cred);
2796 * If the file handle is stale we have to re-resolve the
2797 * entry with the ncp held exclusively. This is a hack
2800 if (error == ESTALE) {
2802 cache_unlock(&nd->nl_nch);
2803 cache_lock(&nd->nl_nch);
2804 cache_setunresolved(&nd->nl_nch);
2805 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2815 * stat_args(char *path, struct stat *ub)
2817 * Get file status; this version follows links.
2820 sys_stat(struct stat_args *uap)
2822 struct nlookupdata nd;
2826 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2828 error = kern_stat(&nd, &st);
2830 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2837 * lstat_args(char *path, struct stat *ub)
2839 * Get file status; this version does not follow links.
2842 sys_lstat(struct lstat_args *uap)
2844 struct nlookupdata nd;
2848 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2850 error = kern_stat(&nd, &st);
2852 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2859 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2861 * Get status of file pointed to by fd/path.
2864 sys_fstatat(struct fstatat_args *uap)
2866 struct nlookupdata nd;
2872 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2875 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2877 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2878 UIO_USERSPACE, flags);
2880 error = kern_stat(&nd, &st);
2882 error = copyout(&st, uap->sb, sizeof(*uap->sb));
2884 nlookup_done_at(&nd, fp);
2889 kern_pathconf(char *path, int name, int flags, register_t *sysmsg_regp)
2891 struct nlookupdata nd;
2896 error = nlookup_init(&nd, path, UIO_USERSPACE, flags);
2898 error = nlookup(&nd);
2900 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2903 error = VOP_PATHCONF(vp, name, sysmsg_regp);
2910 * pathconf_Args(char *path, int name)
2912 * Get configurable pathname variables.
2915 sys_pathconf(struct pathconf_args *uap)
2917 return (kern_pathconf(uap->path, uap->name, NLC_FOLLOW,
2922 * lpathconf_Args(char *path, int name)
2924 * Get configurable pathname variables, but don't follow symlinks.
2927 sys_lpathconf(struct lpathconf_args *uap)
2929 return (kern_pathconf(uap->path, uap->name, 0, &uap->sysmsg_reg));
2934 * kern_readlink isn't properly split yet. There is a copyin burried
2935 * in VOP_READLINK().
2938 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2940 struct thread *td = curthread;
2946 nd->nl_flags |= NLC_SHAREDLOCK;
2947 if ((error = nlookup(nd)) != 0)
2949 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
2952 if (vp->v_type != VLNK) {
2955 aiov.iov_base = buf;
2956 aiov.iov_len = count;
2957 auio.uio_iov = &aiov;
2958 auio.uio_iovcnt = 1;
2959 auio.uio_offset = 0;
2960 auio.uio_rw = UIO_READ;
2961 auio.uio_segflg = UIO_USERSPACE;
2963 auio.uio_resid = count;
2964 error = VOP_READLINK(vp, &auio, td->td_ucred);
2967 *res = count - auio.uio_resid;
2972 * readlink_args(char *path, char *buf, int count)
2974 * Return target name of a symbolic link.
2977 sys_readlink(struct readlink_args *uap)
2979 struct nlookupdata nd;
2982 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2984 error = kern_readlink(&nd, uap->buf, uap->count,
2985 &uap->sysmsg_result);
2992 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
2994 * Return target name of a symbolic link. The path is relative to the
2995 * directory associated with fd.
2998 sys_readlinkat(struct readlinkat_args *uap)
3000 struct nlookupdata nd;
3004 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
3006 error = kern_readlink(&nd, uap->buf, uap->bufsize,
3007 &uap->sysmsg_result);
3009 nlookup_done_at(&nd, fp);
3014 setfflags(struct vnode *vp, int flags)
3016 struct thread *td = curthread;
3021 * Prevent non-root users from setting flags on devices. When
3022 * a device is reused, users can retain ownership of the device
3023 * if they are allowed to set flags and programs assume that
3024 * chown can't fail when done as root.
3026 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
3027 ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
3031 * note: vget is required for any operation that might mod the vnode
3032 * so VINACTIVE is properly cleared.
3034 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3036 vattr.va_flags = flags;
3037 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3044 * chflags(char *path, int flags)
3046 * Change flags of a file given a path name.
3049 sys_chflags(struct chflags_args *uap)
3051 struct nlookupdata nd;
3056 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3058 error = nlookup(&nd);
3060 error = ncp_writechk(&nd.nl_nch);
3062 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3065 error = setfflags(vp, uap->flags);
3072 * lchflags(char *path, int flags)
3074 * Change flags of a file given a path name, but don't follow symlinks.
3077 sys_lchflags(struct lchflags_args *uap)
3079 struct nlookupdata nd;
3084 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3086 error = nlookup(&nd);
3088 error = ncp_writechk(&nd.nl_nch);
3090 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3093 error = setfflags(vp, uap->flags);
3100 * fchflags_args(int fd, int flags)
3102 * Change flags of a file given a file descriptor.
3105 sys_fchflags(struct fchflags_args *uap)
3107 struct thread *td = curthread;
3108 struct proc *p = td->td_proc;
3112 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3114 if (fp->f_nchandle.ncp)
3115 error = ncp_writechk(&fp->f_nchandle);
3117 error = setfflags((struct vnode *) fp->f_data, uap->flags);
3123 * chflagsat_args(int fd, const char *path, int flags, int atflags)
3124 * change flags given a pathname relative to a filedescriptor
3126 int sys_chflagsat(struct chflagsat_args *uap)
3128 struct nlookupdata nd;
3134 if (uap->atflags & ~AT_SYMLINK_NOFOLLOW)
3137 lookupflags = (uap->atflags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3140 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, lookupflags);
3142 error = nlookup(&nd);
3144 error = ncp_writechk(&nd.nl_nch);
3146 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3147 nlookup_done_at(&nd, fp);
3149 error = setfflags(vp, uap->flags);
3157 setfmode(struct vnode *vp, int mode)
3159 struct thread *td = curthread;
3164 * note: vget is required for any operation that might mod the vnode
3165 * so VINACTIVE is properly cleared.
3167 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3169 vattr.va_mode = mode & ALLPERMS;
3170 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3177 kern_chmod(struct nlookupdata *nd, int mode)
3182 if ((error = nlookup(nd)) != 0)
3184 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3186 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3187 error = setfmode(vp, mode);
3193 * chmod_args(char *path, int mode)
3195 * Change mode of a file given path name.
3198 sys_chmod(struct chmod_args *uap)
3200 struct nlookupdata nd;
3203 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3205 error = kern_chmod(&nd, uap->mode);
3211 * lchmod_args(char *path, int mode)
3213 * Change mode of a file given path name (don't follow links.)
3216 sys_lchmod(struct lchmod_args *uap)
3218 struct nlookupdata nd;
3221 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3223 error = kern_chmod(&nd, uap->mode);
3229 * fchmod_args(int fd, int mode)
3231 * Change mode of a file given a file descriptor.
3234 sys_fchmod(struct fchmod_args *uap)
3236 struct thread *td = curthread;
3237 struct proc *p = td->td_proc;
3241 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3243 if (fp->f_nchandle.ncp)
3244 error = ncp_writechk(&fp->f_nchandle);
3246 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3252 * fchmodat_args(char *path, int mode)
3254 * Change mode of a file pointed to by fd/path.
3257 sys_fchmodat(struct fchmodat_args *uap)
3259 struct nlookupdata nd;
3264 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3266 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3268 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3269 UIO_USERSPACE, flags);
3271 error = kern_chmod(&nd, uap->mode);
3272 nlookup_done_at(&nd, fp);
3277 setfown(struct mount *mp, struct vnode *vp, uid_t uid, gid_t gid)
3279 struct thread *td = curthread;
3287 * note: vget is required for any operation that might mod the vnode
3288 * so VINACTIVE is properly cleared.
3290 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3291 if ((error = VOP_GETATTR(vp, &vattr)) != 0)
3293 o_uid = vattr.va_uid;
3294 o_gid = vattr.va_gid;
3295 size = vattr.va_size;
3300 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3309 VFS_ACCOUNT(mp, o_uid, o_gid, -size);
3310 VFS_ACCOUNT(mp, uid, gid, size);
3317 kern_chown(struct nlookupdata *nd, int uid, int gid)
3322 if ((error = nlookup(nd)) != 0)
3324 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3326 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3327 error = setfown(nd->nl_nch.mount, vp, uid, gid);
3333 * chown(char *path, int uid, int gid)
3335 * Set ownership given a path name.
3338 sys_chown(struct chown_args *uap)
3340 struct nlookupdata nd;
3343 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3345 error = kern_chown(&nd, uap->uid, uap->gid);
3351 * lchown_args(char *path, int uid, int gid)
3353 * Set ownership given a path name, do not cross symlinks.
3356 sys_lchown(struct lchown_args *uap)
3358 struct nlookupdata nd;
3361 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3363 error = kern_chown(&nd, uap->uid, uap->gid);
3369 * fchown_args(int fd, int uid, int gid)
3371 * Set ownership given a file descriptor.
3374 sys_fchown(struct fchown_args *uap)
3376 struct thread *td = curthread;
3377 struct proc *p = td->td_proc;
3381 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3383 if (fp->f_nchandle.ncp)
3384 error = ncp_writechk(&fp->f_nchandle);
3386 error = setfown(p->p_fd->fd_ncdir.mount,
3387 (struct vnode *)fp->f_data, uap->uid, uap->gid);
3393 * fchownat(int fd, char *path, int uid, int gid, int flags)
3395 * Set ownership of file pointed to by fd/path.
3398 sys_fchownat(struct fchownat_args *uap)
3400 struct nlookupdata nd;
3405 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3407 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3409 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3410 UIO_USERSPACE, flags);
3412 error = kern_chown(&nd, uap->uid, uap->gid);
3413 nlookup_done_at(&nd, fp);
3419 getutimes(struct timeval *tvp, struct timespec *tsp)
3421 struct timeval tv[2];
3426 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3429 if ((error = itimerfix(tvp)) != 0)
3431 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3432 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3438 getutimens(const struct timespec *ts, struct timespec *newts, int *nullflag)
3440 struct timespec tsnow;
3454 if (newts[0].tv_nsec == UTIME_OMIT && newts[1].tv_nsec == UTIME_OMIT)
3456 if (newts[0].tv_nsec == UTIME_NOW && newts[1].tv_nsec == UTIME_NOW)
3459 if (newts[0].tv_nsec == UTIME_OMIT)
3460 newts[0].tv_sec = VNOVAL;
3461 else if (newts[0].tv_nsec == UTIME_NOW)
3463 else if ((error = itimespecfix(&newts[0])) != 0)
3466 if (newts[1].tv_nsec == UTIME_OMIT)
3467 newts[1].tv_sec = VNOVAL;
3468 else if (newts[1].tv_nsec == UTIME_NOW)
3470 else if ((error = itimespecfix(&newts[1])) != 0)
3477 setutimes(struct vnode *vp, struct vattr *vattr,
3478 const struct timespec *ts, int nullflag)
3480 struct thread *td = curthread;
3484 vattr->va_atime = ts[0];
3485 vattr->va_mtime = ts[1];
3487 vattr->va_vaflags |= VA_UTIMES_NULL;
3488 error = VOP_SETATTR(vp, vattr, td->td_ucred);
3494 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3496 struct timespec ts[2];
3500 if ((error = getutimes(tptr, ts)) != 0)
3503 error = kern_utimensat(nd, tptr ? ts : NULL, 0);
3508 * utimes_args(char *path, struct timeval *tptr)
3510 * Set the access and modification times of a file.
3513 sys_utimes(struct utimes_args *uap)
3515 struct timeval tv[2];
3516 struct nlookupdata nd;
3520 error = copyin(uap->tptr, tv, sizeof(tv));
3524 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3526 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3532 * lutimes_args(char *path, struct timeval *tptr)
3534 * Set the access and modification times of a file.
3537 sys_lutimes(struct lutimes_args *uap)
3539 struct timeval tv[2];
3540 struct nlookupdata nd;
3544 error = copyin(uap->tptr, tv, sizeof(tv));
3548 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3550 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3556 * Set utimes on a file descriptor. The creds used to open the
3557 * file are used to determine whether the operation is allowed
3561 kern_futimens(int fd, struct timespec *ts)
3563 struct thread *td = curthread;
3564 struct proc *p = td->td_proc;
3565 struct timespec newts[2];
3572 error = getutimens(ts, newts, &nullflag);
3575 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3577 if (fp->f_nchandle.ncp)
3578 error = ncp_writechk(&fp->f_nchandle);
3581 error = vget(vp, LK_EXCLUSIVE);
3583 error = VOP_GETATTR(vp, &vattr);
3585 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3589 error = setutimes(vp, &vattr, newts, nullflag);
3599 * futimens_args(int fd, struct timespec *ts)
3601 * Set the access and modification times of a file.
3604 sys_futimens(struct futimens_args *uap)
3606 struct timespec ts[2];
3610 error = copyin(uap->ts, ts, sizeof(ts));
3614 error = kern_futimens(uap->fd, uap->ts ? ts : NULL);
3619 kern_futimes(int fd, struct timeval *tptr)
3621 struct timespec ts[2];
3625 if ((error = getutimes(tptr, ts)) != 0)
3628 error = kern_futimens(fd, tptr ? ts : NULL);
3633 * futimes_args(int fd, struct timeval *tptr)
3635 * Set the access and modification times of a file.
3638 sys_futimes(struct futimes_args *uap)
3640 struct timeval tv[2];
3644 error = copyin(uap->tptr, tv, sizeof(tv));
3648 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3653 kern_utimensat(struct nlookupdata *nd, const struct timespec *ts, int flags)
3655 struct timespec newts[2];
3661 if (flags & ~AT_SYMLINK_NOFOLLOW)
3664 error = getutimens(ts, newts, &nullflag);
3668 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3669 if ((error = nlookup(nd)) != 0)
3671 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3673 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3675 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3676 error = vget(vp, LK_EXCLUSIVE);
3678 error = setutimes(vp, &vattr, newts, nullflag);
3687 * utimensat_args(int fd, const char *path, const struct timespec *ts, int flags);
3689 * Set file access and modification times of a file.
3692 sys_utimensat(struct utimensat_args *uap)
3694 struct timespec ts[2];
3695 struct nlookupdata nd;
3701 error = copyin(uap->ts, ts, sizeof(ts));
3706 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3707 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3708 UIO_USERSPACE, flags);
3710 error = kern_utimensat(&nd, uap->ts ? ts : NULL, uap->flags);
3711 nlookup_done_at(&nd, fp);
3716 kern_truncate(struct nlookupdata *nd, off_t length)
3723 uint64_t old_size = 0;
3727 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3728 if ((error = nlookup(nd)) != 0)
3730 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3732 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3734 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
3739 if (vp->v_type == VDIR) {
3743 if (vfs_quota_enabled) {
3744 error = VOP_GETATTR(vp, &vattr);
3745 KASSERT(error == 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3748 old_size = vattr.va_size;
3751 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3753 vattr.va_size = length;
3754 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3755 VFS_ACCOUNT(nd->nl_nch.mount, uid, gid, length - old_size);
3763 * truncate(char *path, int pad, off_t length)
3765 * Truncate a file given its path name.
3768 sys_truncate(struct truncate_args *uap)
3770 struct nlookupdata nd;
3773 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3775 error = kern_truncate(&nd, uap->length);
3781 kern_ftruncate(int fd, off_t length)
3783 struct thread *td = curthread;
3784 struct proc *p = td->td_proc;
3791 uint64_t old_size = 0;
3796 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3798 if (fp->f_nchandle.ncp) {
3799 error = ncp_writechk(&fp->f_nchandle);
3803 if ((fp->f_flag & FWRITE) == 0) {
3807 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3811 vp = (struct vnode *)fp->f_data;
3812 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3813 if (vp->v_type == VDIR) {
3819 if (vfs_quota_enabled) {
3820 error = VOP_GETATTR(vp, &vattr);
3821 KASSERT(error == 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
3824 old_size = vattr.va_size;
3827 if ((error = vn_writechk(vp, NULL)) == 0) {
3829 vattr.va_size = length;
3830 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3832 VFS_ACCOUNT(mp, uid, gid, length - old_size);
3841 * ftruncate_args(int fd, int pad, off_t length)
3843 * Truncate a file given a file descriptor.
3846 sys_ftruncate(struct ftruncate_args *uap)
3850 error = kern_ftruncate(uap->fd, uap->length);
3858 * Sync an open file.
3861 sys_fsync(struct fsync_args *uap)
3863 struct thread *td = curthread;
3864 struct proc *p = td->td_proc;
3870 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3872 vp = (struct vnode *)fp->f_data;
3873 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3874 if ((obj = vp->v_object) != NULL) {
3875 if (vp->v_mount == NULL ||
3876 (vp->v_mount->mnt_kern_flag & MNTK_NOMSYNC) == 0) {
3877 vm_object_page_clean(obj, 0, 0, 0);
3880 error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
3881 if (error == 0 && vp->v_mount)
3882 error = buf_fsync(vp);
3890 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3892 struct nchandle fnchd;
3893 struct nchandle tnchd;
3894 struct namecache *ncp;
3903 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3904 if ((error = nlookup(fromnd)) != 0)
3906 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3908 fnchd.mount = fromnd->nl_nch.mount;
3912 * unlock the source nch so we can lookup the target nch without
3913 * deadlocking. The target may or may not exist so we do not check
3914 * for a target vp like kern_mkdir() and other creation functions do.
3916 * The source and target directories are ref'd and rechecked after
3917 * everything is relocked to determine if the source or target file
3920 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3921 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3923 fncp_gen = fromnd->nl_nch.ncp->nc_generation;
3925 cache_unlock(&fromnd->nl_nch);
3927 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3928 if ((error = nlookup(tond)) != 0) {
3932 tncp_gen = tond->nl_nch.ncp->nc_generation;
3934 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3938 tnchd.mount = tond->nl_nch.mount;
3942 * If the source and target are the same there is nothing to do
3944 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3951 * Mount points cannot be renamed or overwritten
3953 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3962 * Relock the source ncp. cache_relock() will deal with any
3963 * deadlocks against the already-locked tond and will also
3964 * make sure both are resolved.
3966 * NOTE AFTER RELOCKING: The source or target ncp may have become
3967 * invalid while they were unlocked, nc_vp and nc_mount could
3970 cache_relock(&fromnd->nl_nch, fromnd->nl_cred,
3971 &tond->nl_nch, tond->nl_cred);
3972 fromnd->nl_flags |= NLC_NCPISLOCKED;
3975 * If the namecache generation changed for either fromnd or tond,
3978 if (fromnd->nl_nch.ncp->nc_generation != fncp_gen ||
3979 tond->nl_nch.ncp->nc_generation != tncp_gen) {
3980 kprintf("kern_rename: retry due to gen on: "
3981 "\"%s\" -> \"%s\"\n",
3982 fromnd->nl_nch.ncp->nc_name,
3983 tond->nl_nch.ncp->nc_name);
3990 * If either fromnd or tond are marked destroyed a ripout occured
3991 * out from under us and we must retry.
3993 if ((fromnd->nl_nch.ncp->nc_flag & (NCF_DESTROYED | NCF_UNRESOLVED)) ||
3994 fromnd->nl_nch.ncp->nc_vp == NULL ||
3995 (tond->nl_nch.ncp->nc_flag & NCF_DESTROYED)) {
3996 kprintf("kern_rename: retry due to ripout on: "
3997 "\"%s\" -> \"%s\"\n",
3998 fromnd->nl_nch.ncp->nc_name,
3999 tond->nl_nch.ncp->nc_name);
4006 * Make sure the parent directories linkages are the same.
4007 * XXX shouldn't be needed any more w/ generation check above.
4009 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
4010 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
4017 * Both the source and target must be within the same filesystem and
4018 * in the same filesystem as their parent directories within the
4019 * namecache topology.
4021 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
4024 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
4025 mp != tond->nl_nch.mount) {
4032 * Make sure the mount point is writable
4034 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
4041 * If the target exists and either the source or target is a directory,
4042 * then both must be directories.
4044 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
4047 if (tond->nl_nch.ncp->nc_vp) {
4048 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
4050 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
4051 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
4053 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
4059 * You cannot rename a source into itself or a subdirectory of itself.
4060 * We check this by travsersing the target directory upwards looking
4061 * for a match against the source.
4066 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
4067 if (fromnd->nl_nch.ncp == ncp) {
4078 * Even though the namespaces are different, they may still represent
4079 * hardlinks to the same file. The filesystem might have a hard time
4080 * with this so we issue a NREMOVE of the source instead of a NRENAME
4081 * when we detect the situation.
4084 fdvp = fromnd->nl_dvp;
4085 tdvp = tond->nl_dvp;
4086 if (fdvp == NULL || tdvp == NULL) {
4088 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
4089 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
4092 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
4093 fdvp, tdvp, tond->nl_cred);
4100 * rename_args(char *from, char *to)
4102 * Rename files. Source and destination must either both be directories,
4103 * or both not be directories. If target is a directory, it must be empty.
4106 sys_rename(struct rename_args *uap)
4108 struct nlookupdata fromnd, tond;
4112 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
4114 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
4116 error = kern_rename(&fromnd, &tond);
4117 nlookup_done(&tond);
4119 nlookup_done(&fromnd);
4120 } while (error == EAGAIN);
4125 * renameat_args(int oldfd, char *old, int newfd, char *new)
4127 * Rename files using paths relative to the directories associated with
4128 * oldfd and newfd. Source and destination must either both be directories,
4129 * or both not be directories. If target is a directory, it must be empty.
4132 sys_renameat(struct renameat_args *uap)
4134 struct nlookupdata oldnd, newnd;
4135 struct file *oldfp, *newfp;
4139 error = nlookup_init_at(&oldnd, &oldfp,
4140 uap->oldfd, uap->old,
4143 error = nlookup_init_at(&newnd, &newfp,
4144 uap->newfd, uap->new,
4147 error = kern_rename(&oldnd, &newnd);
4148 nlookup_done_at(&newnd, newfp);
4150 nlookup_done_at(&oldnd, oldfp);
4151 } while (error == EAGAIN);
4156 kern_mkdir(struct nlookupdata *nd, int mode)
4158 struct thread *td = curthread;
4159 struct proc *p = td->td_proc;
4165 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
4166 if ((error = nlookup(nd)) != 0)
4169 if (nd->nl_nch.ncp->nc_vp)
4171 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4174 vattr.va_type = VDIR;
4175 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
4178 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
4185 * mkdir_args(char *path, int mode)
4187 * Make a directory file.
4190 sys_mkdir(struct mkdir_args *uap)
4192 struct nlookupdata nd;
4195 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4197 error = kern_mkdir(&nd, uap->mode);
4203 * mkdirat_args(int fd, char *path, mode_t mode)
4205 * Make a directory file. The path is relative to the directory associated
4209 sys_mkdirat(struct mkdirat_args *uap)
4211 struct nlookupdata nd;
4215 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
4217 error = kern_mkdir(&nd, uap->mode);
4218 nlookup_done_at(&nd, fp);
4223 kern_rmdir(struct nlookupdata *nd)
4228 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
4229 if ((error = nlookup(nd)) != 0)
4233 * Do not allow directories representing mount points to be
4234 * deleted, even if empty. Check write perms on mount point
4235 * in case the vnode is aliased (aka nullfs).
4237 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
4239 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4241 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
4246 * rmdir_args(char *path)
4248 * Remove a directory file.
4251 sys_rmdir(struct rmdir_args *uap)
4253 struct nlookupdata nd;
4256 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4258 error = kern_rmdir(&nd);
4264 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
4265 enum uio_seg direction)
4267 struct thread *td = curthread;
4268 struct proc *p = td->td_proc;
4276 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
4278 if ((fp->f_flag & FREAD) == 0) {
4282 vp = (struct vnode *)fp->f_data;
4283 if (vp->v_type != VDIR) {
4287 aiov.iov_base = buf;
4288 aiov.iov_len = count;
4289 auio.uio_iov = &aiov;
4290 auio.uio_iovcnt = 1;
4291 auio.uio_rw = UIO_READ;
4292 auio.uio_segflg = direction;
4294 auio.uio_resid = count;
4295 loff = auio.uio_offset = fp->f_offset;
4296 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
4297 fp->f_offset = auio.uio_offset;
4302 * WARNING! *basep may not be wide enough to accomodate the
4303 * seek offset. XXX should we hack this to return the upper 32 bits
4304 * for offsets greater then 4G?
4307 *basep = (long)loff;
4309 *res = count - auio.uio_resid;
4316 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4318 * Read a block of directory entries in a file system independent format.
4321 sys_getdirentries(struct getdirentries_args *uap)
4326 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
4327 &uap->sysmsg_result, UIO_USERSPACE);
4329 if (error == 0 && uap->basep)
4330 error = copyout(&base, uap->basep, sizeof(*uap->basep));
4335 * getdents_args(int fd, char *buf, size_t count)
4338 sys_getdents(struct getdents_args *uap)
4342 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
4343 &uap->sysmsg_result, UIO_USERSPACE);
4349 * Set the mode mask for creation of filesystem nodes.
4351 * umask(int newmask)
4354 sys_umask(struct umask_args *uap)
4356 struct thread *td = curthread;
4357 struct proc *p = td->td_proc;
4358 struct filedesc *fdp;
4361 uap->sysmsg_result = fdp->fd_cmask;
4362 fdp->fd_cmask = uap->newmask & ALLPERMS;
4367 * revoke(char *path)
4369 * Void all references to file by ripping underlying filesystem
4373 sys_revoke(struct revoke_args *uap)
4375 struct nlookupdata nd;
4382 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4384 error = nlookup(&nd);
4386 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4387 cred = crhold(nd.nl_cred);
4391 error = VOP_GETATTR(vp, &vattr);
4392 if (error == 0 && cred->cr_uid != vattr.va_uid)
4393 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4394 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4396 error = vrevoke(vp, cred);
4397 } else if (error == 0) {
4398 error = vrevoke(vp, cred);
4408 * getfh_args(char *fname, fhandle_t *fhp)
4410 * Get (NFS) file handle
4412 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4413 * mount. This allows nullfs mounts to be explicitly exported.
4415 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4417 * nullfs mounts of subdirectories are not safe. That is, it will
4418 * work, but you do not really have protection against access to
4419 * the related parent directories.
4422 sys_getfh(struct getfh_args *uap)
4424 struct thread *td = curthread;
4425 struct nlookupdata nd;
4432 * Must be super user
4434 if ((error = priv_check(td, PRIV_ROOT)) != 0)
4438 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4440 error = nlookup(&nd);
4442 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4443 mp = nd.nl_nch.mount;
4446 bzero(&fh, sizeof(fh));
4447 fh.fh_fsid = mp->mnt_stat.f_fsid;
4448 error = VFS_VPTOFH(vp, &fh.fh_fid);
4451 error = copyout(&fh, uap->fhp, sizeof(fh));
4457 * fhopen_args(const struct fhandle *u_fhp, int flags)
4459 * syscall for the rpc.lockd to use to translate a NFS file handle into
4460 * an open descriptor.
4462 * warning: do not remove the priv_check() call or this becomes one giant
4466 sys_fhopen(struct fhopen_args *uap)
4468 struct thread *td = curthread;
4469 struct filedesc *fdp = td->td_proc->p_fd;
4474 struct vattr *vap = &vat;
4476 int fmode, mode, error = 0, type;
4482 * Must be super user
4484 error = priv_check(td, PRIV_ROOT);
4488 fmode = FFLAGS(uap->flags);
4491 * Why not allow a non-read/write open for our lockd?
4493 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4495 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4500 * Find the mount point
4502 mp = vfs_getvfs(&fhp.fh_fsid);
4507 /* now give me my vnode, it gets returned to me locked */
4508 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4512 * from now on we have to make sure not
4513 * to forget about the vnode
4514 * any error that causes an abort must vput(vp)
4515 * just set error = err and 'goto bad;'.
4521 if (vp->v_type == VLNK) {
4525 if (vp->v_type == VSOCK) {
4530 if (fmode & (FWRITE | O_TRUNC)) {
4531 if (vp->v_type == VDIR) {
4535 error = vn_writechk(vp, NULL);
4543 error = VOP_ACCESS(vp, mode, td->td_ucred);
4547 if (fmode & O_TRUNC) {
4548 vn_unlock(vp); /* XXX */
4549 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4552 error = VOP_SETATTR(vp, vap, td->td_ucred);
4558 * VOP_OPEN needs the file pointer so it can potentially override
4561 * WARNING! no f_nchandle will be associated when fhopen()ing a
4564 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4568 error = VOP_OPEN(vp, fmode, td->td_ucred, fp);
4571 * setting f_ops this way prevents VOP_CLOSE from being
4572 * called or fdrop() releasing the vp from v_data. Since
4573 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4575 fp->f_ops = &badfileops;
4581 * The fp is given its own reference, we still have our ref and lock.
4583 * Assert that all regular files must be created with a VM object.
4585 if (vp->v_type == VREG && vp->v_object == NULL) {
4586 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
4591 * The open was successful. Handle any locking requirements.
4593 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4594 lf.l_whence = SEEK_SET;
4597 if (fmode & O_EXLOCK)
4598 lf.l_type = F_WRLCK;
4600 lf.l_type = F_RDLCK;
4601 if (fmode & FNONBLOCK)
4606 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
4608 * release our private reference.
4610 fsetfd(fdp, NULL, indx);
4615 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4616 atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */
4620 * Clean up. Associate the file pointer with the previously
4621 * reserved descriptor and return it.
4624 if (uap->flags & O_CLOEXEC)
4625 fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
4626 fsetfd(fdp, fp, indx);
4628 uap->sysmsg_result = indx;
4632 fsetfd(fdp, NULL, indx);
4641 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4644 sys_fhstat(struct fhstat_args *uap)
4646 struct thread *td = curthread;
4654 * Must be super user
4656 error = priv_check(td, PRIV_ROOT);
4660 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4664 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4667 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4668 error = vn_stat(vp, &sb, td->td_ucred);
4673 error = copyout(&sb, uap->sb, sizeof(sb));
4678 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4681 sys_fhstatfs(struct fhstatfs_args *uap)
4683 struct thread *td = curthread;
4684 struct proc *p = td->td_proc;
4689 char *fullpath, *freepath;
4694 * Must be super user
4696 if ((error = priv_check(td, PRIV_ROOT)))
4699 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4702 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4706 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4711 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4716 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4719 error = mount_path(p, mp, &fullpath, &freepath);
4722 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4723 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4724 kfree(freepath, M_TEMP);
4726 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4727 if (priv_check(td, PRIV_ROOT)) {
4728 bcopy(sp, &sb, sizeof(sb));
4729 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4732 error = copyout(sp, uap->buf, sizeof(*sp));
4738 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4741 sys_fhstatvfs(struct fhstatvfs_args *uap)
4743 struct thread *td = curthread;
4744 struct proc *p = td->td_proc;
4752 * Must be super user
4754 if ((error = priv_check(td, PRIV_ROOT)))
4757 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4760 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4764 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4769 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4772 sp = &mp->mnt_vstat;
4774 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
4778 if (mp->mnt_flag & MNT_RDONLY)
4779 sp->f_flag |= ST_RDONLY;
4780 if (mp->mnt_flag & MNT_NOSUID)
4781 sp->f_flag |= ST_NOSUID;
4782 error = copyout(sp, uap->buf, sizeof(*sp));
4789 * Syscall to push extended attribute configuration information into the
4790 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4791 * a command (int cmd), and attribute name and misc data. For now, the
4792 * attribute name is left in userspace for consumption by the VFS_op.
4793 * It will probably be changed to be copied into sysspace by the
4794 * syscall in the future, once issues with various consumers of the
4795 * attribute code have raised their hands.
4797 * Currently this is used only by UFS Extended Attributes.
4800 sys_extattrctl(struct extattrctl_args *uap)
4802 struct nlookupdata nd;
4804 char attrname[EXTATTR_MAXNAMELEN];
4812 if (error == 0 && uap->filename) {
4813 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
4816 error = nlookup(&nd);
4818 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4822 if (error == 0 && uap->attrname) {
4823 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4828 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4830 error = nlookup(&nd);
4832 error = ncp_writechk(&nd.nl_nch);
4834 error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
4836 uap->attrname, nd.nl_cred);
4845 * Syscall to get a named extended attribute on a file or directory.
4848 sys_extattr_set_file(struct extattr_set_file_args *uap)
4850 char attrname[EXTATTR_MAXNAMELEN];
4851 struct nlookupdata nd;
4857 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4863 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4865 error = nlookup(&nd);
4867 error = ncp_writechk(&nd.nl_nch);
4869 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4875 bzero(&auio, sizeof(auio));
4876 aiov.iov_base = uap->data;
4877 aiov.iov_len = uap->nbytes;
4878 auio.uio_iov = &aiov;
4879 auio.uio_iovcnt = 1;
4880 auio.uio_offset = 0;
4881 auio.uio_resid = uap->nbytes;
4882 auio.uio_rw = UIO_WRITE;
4883 auio.uio_td = curthread;
4885 error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
4894 * Syscall to get a named extended attribute on a file or directory.
4897 sys_extattr_get_file(struct extattr_get_file_args *uap)
4899 char attrname[EXTATTR_MAXNAMELEN];
4900 struct nlookupdata nd;
4906 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4912 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4914 error = nlookup(&nd);
4916 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_SHARED, &vp);
4922 bzero(&auio, sizeof(auio));
4923 aiov.iov_base = uap->data;
4924 aiov.iov_len = uap->nbytes;
4925 auio.uio_iov = &aiov;
4926 auio.uio_iovcnt = 1;
4927 auio.uio_offset = 0;
4928 auio.uio_resid = uap->nbytes;
4929 auio.uio_rw = UIO_READ;
4930 auio.uio_td = curthread;
4932 error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
4934 uap->sysmsg_result = uap->nbytes - auio.uio_resid;
4942 * Syscall to delete a named extended attribute from a file or directory.
4943 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4946 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4948 char attrname[EXTATTR_MAXNAMELEN];
4949 struct nlookupdata nd;
4953 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4957 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4959 error = nlookup(&nd);
4961 error = ncp_writechk(&nd.nl_nch);
4963 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4965 error = VOP_SETEXTATTR(vp, uap->attrnamespace,
4966 attrname, NULL, nd.nl_cred);
4975 * Determine if the mount is visible to the process.
4978 chroot_visible_mnt(struct mount *mp, struct proc *p)
4980 struct nchandle nch;
4983 * Traverse from the mount point upwards. If we hit the process
4984 * root then the mount point is visible to the process.
4986 nch = mp->mnt_ncmountpt;
4988 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4989 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4992 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4993 nch = nch.mount->mnt_ncmounton;
4995 nch.ncp = nch.ncp->nc_parent;
5000 * If the mount point is not visible to the process, but the
5001 * process root is in a subdirectory of the mount, return
5004 if (p->p_fd->fd_nrdir.mount == mp)