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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
39 * $FreeBSD: src/sys/kern/vfs_syscalls.c,v 1.151.2.18 2003/04/04 20:35:58 tegge Exp $
40 * $DragonFly: src/sys/kern/vfs_syscalls.c,v 1.135 2008/11/11 00:55:49 pavalos Exp $
43 #include <sys/param.h>
44 #include <sys/systm.h>
47 #include <sys/sysent.h>
48 #include <sys/malloc.h>
49 #include <sys/mount.h>
50 #include <sys/mountctl.h>
51 #include <sys/sysproto.h>
52 #include <sys/filedesc.h>
53 #include <sys/kernel.h>
54 #include <sys/fcntl.h>
56 #include <sys/linker.h>
58 #include <sys/unistd.h>
59 #include <sys/vnode.h>
61 #include <sys/namei.h>
62 #include <sys/nlookup.h>
63 #include <sys/dirent.h>
64 #include <sys/extattr.h>
65 #include <sys/spinlock.h>
66 #include <sys/kern_syscall.h>
67 #include <sys/objcache.h>
68 #include <sys/sysctl.h>
71 #include <sys/file2.h>
72 #include <sys/spinlock2.h>
75 #include <vm/vm_object.h>
76 #include <vm/vm_page.h>
78 #include <machine/limits.h>
79 #include <machine/stdarg.h>
81 #include <vfs/union/union.h>
83 static void mount_warning(struct mount *mp, const char *ctl, ...);
84 static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb);
85 static int checkvp_chdir (struct vnode *vn, struct thread *td);
86 static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch);
87 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
88 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
89 static int getutimes (const struct timeval *, struct timespec *);
90 static int setfown (struct vnode *, uid_t, gid_t);
91 static int setfmode (struct vnode *, int);
92 static int setfflags (struct vnode *, int);
93 static int setutimes (struct vnode *, 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, "");
101 * Virtual File System System Calls
105 * Mount a file system.
108 * mount_args(char *type, char *path, int flags, caddr_t data)
112 sys_mount(struct mount_args *uap)
114 struct thread *td = curthread;
115 struct proc *p = td->td_proc;
119 struct vfsconf *vfsp;
120 int error, flag = 0, flag2 = 0;
123 struct nlookupdata nd;
124 char fstypename[MFSNAMELEN];
125 struct ucred *cred = p->p_ucred;
128 if (cred->cr_prison != NULL)
130 if (usermount == 0 && (error = suser(td)))
133 * Do not allow NFS export by non-root users.
135 if (uap->flags & MNT_EXPORTED) {
141 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
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 * Extract the locked+refd ncp and cleanup the nd structure
165 cache_zero(&nd.nl_nch);
168 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) && cache_findmount(&nch))
175 * now we have the locked ref'd nch and unreferenced vnode.
178 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
185 * Now we have an unlocked ref'd nch and a locked ref'd vp
187 if (uap->flags & MNT_UPDATE) {
188 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
195 flag2 = mp->mnt_kern_flag;
197 * We only allow the filesystem to be reloaded if it
198 * is currently mounted read-only.
200 if ((uap->flags & MNT_RELOAD) &&
201 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
204 return (EOPNOTSUPP); /* Needs translation */
207 * Only root, or the user that did the original mount is
208 * permitted to update it.
210 if (mp->mnt_stat.f_owner != cred->cr_uid &&
211 (error = suser(td))) {
216 if (vfs_busy(mp, LK_NOWAIT)) {
221 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
227 vp->v_flag |= VMOUNT;
229 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
234 * If the user is not root, ensure that they own the directory
235 * onto which we are attempting to mount.
237 if ((error = VOP_GETATTR(vp, &va)) ||
238 (va.va_uid != cred->cr_uid && (error = suser(td)))) {
243 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
248 if (vp->v_type != VDIR) {
253 if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
258 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
263 vfsp = vfsconf_find_by_name(fstypename);
267 /* Only load modules for root (very important!) */
268 if ((error = suser(td)) != 0) {
273 error = linker_load_file(fstypename, &lf);
274 if (error || lf == NULL) {
282 /* lookup again, see if the VFS was loaded */
283 vfsp = vfsconf_find_by_name(fstypename);
286 linker_file_unload(lf);
292 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
297 vp->v_flag |= VMOUNT;
300 * Allocate and initialize the filesystem.
302 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
303 TAILQ_INIT(&mp->mnt_nvnodelist);
304 TAILQ_INIT(&mp->mnt_reservedvnlist);
305 TAILQ_INIT(&mp->mnt_jlist);
306 mp->mnt_nvnodelistsize = 0;
307 lockinit(&mp->mnt_lock, "vfslock", 0, 0);
308 vfs_busy(mp, LK_NOWAIT);
309 mp->mnt_op = vfsp->vfc_vfsops;
311 vfsp->vfc_refcount++;
312 mp->mnt_stat.f_type = vfsp->vfc_typenum;
313 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
314 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
315 mp->mnt_stat.f_owner = cred->cr_uid;
316 mp->mnt_iosize_max = DFLTPHYS;
320 * Set the mount level flags.
322 if (uap->flags & MNT_RDONLY)
323 mp->mnt_flag |= MNT_RDONLY;
324 else if (mp->mnt_flag & MNT_RDONLY)
325 mp->mnt_kern_flag |= MNTK_WANTRDWR;
326 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
327 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
328 MNT_NOSYMFOLLOW | MNT_IGNORE |
329 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
330 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
331 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
332 MNT_NOSYMFOLLOW | MNT_IGNORE |
333 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
335 * Mount the filesystem.
336 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
339 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
340 if (mp->mnt_flag & MNT_UPDATE) {
341 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
342 mp->mnt_flag &= ~MNT_RDONLY;
343 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
344 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
347 mp->mnt_kern_flag = flag2;
350 vp->v_flag &= ~VMOUNT;
355 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
357 * Put the new filesystem on the mount list after root. The mount
358 * point gets its own mnt_ncmountpt (unless the VFS already set one
359 * up) which represents the root of the mount. The lookup code
360 * detects the mount point going forward and checks the root of
361 * the mount going backwards.
363 * It is not necessary to invalidate or purge the vnode underneath
364 * because elements under the mount will be given their own glue
368 if (mp->mnt_ncmountpt.ncp == NULL) {
370 * allocate, then unlock, but leave the ref intact
372 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
373 cache_unlock(&mp->mnt_ncmountpt);
375 mp->mnt_ncmounton = nch; /* inherits ref */
376 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
378 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */
379 vp->v_flag &= ~VMOUNT;
380 mountlist_insert(mp, MNTINS_LAST);
381 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
383 error = vfs_allocate_syncvnode(mp);
385 error = VFS_START(mp, 0);
388 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
389 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
390 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
391 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
392 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
393 vp->v_flag &= ~VMOUNT;
394 mp->mnt_vfc->vfc_refcount--;
404 * Scan all active processes to see if any of them have a current
405 * or root directory onto which the new filesystem has just been
406 * mounted. If so, replace them with the new mount point.
408 * The passed ncp is ref'd and locked (from the mount code) and
409 * must be associated with the vnode representing the root of the
412 struct checkdirs_info {
413 struct nchandle old_nch;
414 struct nchandle new_nch;
415 struct vnode *old_vp;
416 struct vnode *new_vp;
419 static int checkdirs_callback(struct proc *p, void *data);
422 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
424 struct checkdirs_info info;
430 * If the old mount point's vnode has a usecount of 1, it is not
431 * being held as a descriptor anywhere.
433 olddp = old_nch->ncp->nc_vp;
434 if (olddp == NULL || olddp->v_sysref.refcnt == 1)
438 * Force the root vnode of the new mount point to be resolved
439 * so we can update any matching processes.
442 if (VFS_ROOT(mp, &newdp))
443 panic("mount: lost mount");
444 cache_setunresolved(new_nch);
445 cache_setvp(new_nch, newdp);
448 * Special handling of the root node
450 if (rootvnode == olddp) {
452 vfs_cache_setroot(newdp, cache_hold(new_nch));
456 * Pass newdp separately so the callback does not have to access
457 * it via new_nch->ncp->nc_vp.
459 info.old_nch = *old_nch;
460 info.new_nch = *new_nch;
462 allproc_scan(checkdirs_callback, &info);
467 * NOTE: callback is not MP safe because the scanned process's filedesc
468 * structure can be ripped out from under us, amoung other things.
471 checkdirs_callback(struct proc *p, void *data)
473 struct checkdirs_info *info = data;
474 struct filedesc *fdp;
475 struct nchandle ncdrop1;
476 struct nchandle ncdrop2;
477 struct vnode *vprele1;
478 struct vnode *vprele2;
480 if ((fdp = p->p_fd) != NULL) {
481 cache_zero(&ncdrop1);
482 cache_zero(&ncdrop2);
487 * MPUNSAFE - XXX fdp can be pulled out from under a
490 * A shared filedesc is ok, we don't have to copy it
491 * because we are making this change globally.
493 spin_lock_wr(&fdp->fd_spin);
494 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
495 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
496 vprele1 = fdp->fd_cdir;
498 fdp->fd_cdir = info->new_vp;
499 ncdrop1 = fdp->fd_ncdir;
500 cache_copy(&info->new_nch, &fdp->fd_ncdir);
502 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
503 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
504 vprele2 = fdp->fd_rdir;
506 fdp->fd_rdir = info->new_vp;
507 ncdrop2 = fdp->fd_nrdir;
508 cache_copy(&info->new_nch, &fdp->fd_nrdir);
510 spin_unlock_wr(&fdp->fd_spin);
512 cache_drop(&ncdrop1);
514 cache_drop(&ncdrop2);
524 * Unmount a file system.
526 * Note: unmount takes a path to the vnode mounted on as argument,
527 * not special file (as before).
530 * umount_args(char *path, int flags)
534 sys_unmount(struct unmount_args *uap)
536 struct thread *td = curthread;
537 struct proc *p = td->td_proc;
538 struct mount *mp = NULL;
540 struct nlookupdata nd;
543 if (p->p_ucred->cr_prison != NULL)
545 if (usermount == 0 && (error = suser(td)))
548 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
550 error = nlookup(&nd);
554 mp = nd.nl_nch.mount;
557 * Only root, or the user that did the original mount is
558 * permitted to unmount this filesystem.
560 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
565 * Don't allow unmounting the root file system.
567 if (mp->mnt_flag & MNT_ROOTFS) {
573 * Must be the root of the filesystem
575 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
584 return (dounmount(mp, uap->flags));
588 * Do the actual file system unmount.
591 dounmount_interlock(struct mount *mp)
593 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
595 mp->mnt_kern_flag |= MNTK_UNMOUNT;
600 dounmount(struct mount *mp, int flags)
602 struct namecache *ncp;
611 * Exclusive access for unmounting purposes
613 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
617 * Allow filesystems to detect that a forced unmount is in progress.
619 if (flags & MNT_FORCE)
620 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
621 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_NOWAIT);
622 error = lockmgr(&mp->mnt_lock, lflags);
624 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
625 if (mp->mnt_kern_flag & MNTK_MWAIT)
630 if (mp->mnt_flag & MNT_EXPUBLIC)
631 vfs_setpublicfs(NULL, NULL, NULL);
633 vfs_msync(mp, MNT_WAIT);
634 async_flag = mp->mnt_flag & MNT_ASYNC;
635 mp->mnt_flag &=~ MNT_ASYNC;
638 * If this filesystem isn't aliasing other filesystems,
639 * try to invalidate any remaining namecache entries and
640 * check the count afterwords.
642 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
643 cache_lock(&mp->mnt_ncmountpt);
644 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
645 cache_unlock(&mp->mnt_ncmountpt);
647 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
648 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
650 if ((flags & MNT_FORCE) == 0) {
652 mount_warning(mp, "Cannot unmount: "
658 mount_warning(mp, "Forced unmount: "
669 * nchandle records ref the mount structure. Expect a count of 1
670 * (our mount->mnt_ncmountpt).
672 if (mp->mnt_refs != 1) {
673 if ((flags & MNT_FORCE) == 0) {
674 mount_warning(mp, "Cannot unmount: "
675 "%d process references still "
676 "present", mp->mnt_refs);
679 mount_warning(mp, "Forced unmount: "
680 "%d process references still "
681 "present", mp->mnt_refs);
687 * Decomission our special mnt_syncer vnode. This also stops
688 * the vnlru code. If we are unable to unmount we recommission
692 if ((vp = mp->mnt_syncer) != NULL) {
693 mp->mnt_syncer = NULL;
696 if (((mp->mnt_flag & MNT_RDONLY) ||
697 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
698 (flags & MNT_FORCE)) {
699 error = VFS_UNMOUNT(mp, flags);
703 if (mp->mnt_syncer == NULL)
704 vfs_allocate_syncvnode(mp);
705 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
706 mp->mnt_flag |= async_flag;
707 lockmgr(&mp->mnt_lock, LK_RELEASE);
708 if (mp->mnt_kern_flag & MNTK_MWAIT)
713 * Clean up any journals still associated with the mount after
714 * filesystem activity has ceased.
716 journal_remove_all_journals(mp,
717 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
719 mountlist_remove(mp);
722 * Remove any installed vnode ops here so the individual VFSs don't
725 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
726 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
727 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
728 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
729 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
731 if (mp->mnt_ncmountpt.ncp != NULL) {
732 nch = mp->mnt_ncmountpt;
733 cache_zero(&mp->mnt_ncmountpt);
734 cache_clrmountpt(&nch);
737 if (mp->mnt_ncmounton.ncp != NULL) {
738 nch = mp->mnt_ncmounton;
739 cache_zero(&mp->mnt_ncmounton);
740 cache_clrmountpt(&nch);
744 mp->mnt_vfc->vfc_refcount--;
745 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
746 panic("unmount: dangling vnode");
747 lockmgr(&mp->mnt_lock, LK_RELEASE);
748 if (mp->mnt_kern_flag & MNTK_MWAIT)
757 mount_warning(struct mount *mp, const char *ctl, ...)
764 if (cache_fullpath(NULL, &mp->mnt_ncmounton, &ptr, &buf) == 0) {
765 kprintf("unmount(%s): ", ptr);
770 kprintf("unmount(%p", mp);
771 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
772 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
781 * Shim cache_fullpath() to handle the case where a process is chrooted into
782 * a subdirectory of a mount. In this case if the root mount matches the
783 * process root directory's mount we have to specify the process's root
784 * directory instead of the mount point, because the mount point might
785 * be above the root directory.
789 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
791 struct nchandle *nch;
793 if (p && p->p_fd->fd_nrdir.mount == mp)
794 nch = &p->p_fd->fd_nrdir;
796 nch = &mp->mnt_ncmountpt;
797 return(cache_fullpath(p, nch, rb, fb));
801 * Sync each mounted filesystem.
805 static int syncprt = 0;
806 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
809 static int sync_callback(struct mount *mp, void *data);
813 sys_sync(struct sync_args *uap)
815 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
818 * print out buffer pool stat information on each sync() call.
828 sync_callback(struct mount *mp, void *data __unused)
832 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
833 asyncflag = mp->mnt_flag & MNT_ASYNC;
834 mp->mnt_flag &= ~MNT_ASYNC;
835 vfs_msync(mp, MNT_NOWAIT);
836 VFS_SYNC(mp, MNT_NOWAIT);
837 mp->mnt_flag |= asyncflag;
842 /* XXX PRISON: could be per prison flag */
843 static int prison_quotas;
845 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
849 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
851 * Change filesystem quotas.
855 sys_quotactl(struct quotactl_args *uap)
857 struct nlookupdata nd;
865 if (p->p_ucred->cr_prison && !prison_quotas)
868 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
870 error = nlookup(&nd);
872 mp = nd.nl_nch.mount;
873 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
874 uap->arg, nd.nl_cred);
881 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
882 * void *buf, int buflen)
884 * This function operates on a mount point and executes the specified
885 * operation using the specified control data, and possibly returns data.
887 * The actual number of bytes stored in the result buffer is returned, 0
888 * if none, otherwise an error is returned.
892 sys_mountctl(struct mountctl_args *uap)
894 struct thread *td = curthread;
895 struct proc *p = td->td_proc;
903 * Sanity and permissions checks. We must be root.
906 if (p->p_ucred->cr_prison != NULL)
908 if ((error = suser(td)) != 0)
912 * Argument length checks
914 if (uap->ctllen < 0 || uap->ctllen > 1024)
916 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
918 if (uap->path == NULL)
922 * Allocate the necessary buffers and copyin data
924 path = objcache_get(namei_oc, M_WAITOK);
925 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
930 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
931 error = copyin(uap->ctl, ctl, uap->ctllen);
936 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
939 * Validate the descriptor
942 fp = holdfp(p->p_fd, uap->fd, -1);
952 * Execute the internal kernel function and clean up.
954 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
957 if (error == 0 && uap->sysmsg_result > 0)
958 error = copyout(buf, uap->buf, uap->sysmsg_result);
961 objcache_put(namei_oc, path);
970 * Execute a mount control operation by resolving the path to a mount point
971 * and calling vop_mountctl().
973 * Use the mount point from the nch instead of the vnode so nullfs mounts
974 * can properly spike the VOP.
977 kern_mountctl(const char *path, int op, struct file *fp,
978 const void *ctl, int ctllen,
979 void *buf, int buflen, int *res)
983 struct nlookupdata nd;
988 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
990 error = nlookup(&nd);
992 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
993 mp = nd.nl_nch.mount;
999 * Must be the root of the filesystem
1001 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1005 error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen,
1012 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1014 struct thread *td = curthread;
1015 struct proc *p = td->td_proc;
1018 char *fullpath, *freepath;
1021 if ((error = nlookup(nd)) != 0)
1023 mp = nd->nl_nch.mount;
1025 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1028 error = mount_path(p, mp, &fullpath, &freepath);
1031 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1032 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1033 kfree(freepath, M_TEMP);
1035 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1036 bcopy(sp, buf, sizeof(*buf));
1037 /* Only root should have access to the fsid's. */
1039 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1044 * statfs_args(char *path, struct statfs *buf)
1046 * Get filesystem statistics.
1049 sys_statfs(struct statfs_args *uap)
1051 struct nlookupdata nd;
1055 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1057 error = kern_statfs(&nd, &buf);
1060 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1065 kern_fstatfs(int fd, struct statfs *buf)
1067 struct thread *td = curthread;
1068 struct proc *p = td->td_proc;
1072 char *fullpath, *freepath;
1076 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1078 mp = ((struct vnode *)fp->f_data)->v_mount;
1083 if (fp->f_cred == NULL) {
1088 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1091 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1093 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1094 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1095 kfree(freepath, M_TEMP);
1097 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1098 bcopy(sp, buf, sizeof(*buf));
1100 /* Only root should have access to the fsid's. */
1102 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1110 * fstatfs_args(int fd, struct statfs *buf)
1112 * Get filesystem statistics.
1115 sys_fstatfs(struct fstatfs_args *uap)
1120 error = kern_fstatfs(uap->fd, &buf);
1123 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1128 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1134 if ((error = nlookup(nd)) != 0)
1136 mp = nd->nl_nch.mount;
1137 sp = &mp->mnt_vstat;
1138 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1142 if (mp->mnt_flag & MNT_RDONLY)
1143 sp->f_flag |= ST_RDONLY;
1144 if (mp->mnt_flag & MNT_NOSUID)
1145 sp->f_flag |= ST_NOSUID;
1146 bcopy(sp, buf, sizeof(*buf));
1151 * statfs_args(char *path, struct statfs *buf)
1153 * Get filesystem statistics.
1156 sys_statvfs(struct statvfs_args *uap)
1158 struct nlookupdata nd;
1162 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1164 error = kern_statvfs(&nd, &buf);
1167 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1172 kern_fstatvfs(int fd, struct statvfs *buf)
1174 struct thread *td = curthread;
1175 struct proc *p = td->td_proc;
1182 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1184 mp = ((struct vnode *)fp->f_data)->v_mount;
1189 if (fp->f_cred == NULL) {
1193 sp = &mp->mnt_vstat;
1194 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1198 if (mp->mnt_flag & MNT_RDONLY)
1199 sp->f_flag |= ST_RDONLY;
1200 if (mp->mnt_flag & MNT_NOSUID)
1201 sp->f_flag |= ST_NOSUID;
1203 bcopy(sp, buf, sizeof(*buf));
1211 * fstatfs_args(int fd, struct statfs *buf)
1213 * Get filesystem statistics.
1216 sys_fstatvfs(struct fstatvfs_args *uap)
1221 error = kern_fstatvfs(uap->fd, &buf);
1224 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1229 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1231 * Get statistics on all filesystems.
1234 struct getfsstat_info {
1235 struct statfs *sfsp;
1243 static int getfsstat_callback(struct mount *, void *);
1247 sys_getfsstat(struct getfsstat_args *uap)
1249 struct thread *td = curthread;
1250 struct proc *p = td->td_proc;
1251 struct getfsstat_info info;
1253 bzero(&info, sizeof(info));
1255 info.maxcount = uap->bufsize / sizeof(struct statfs);
1256 info.sfsp = uap->buf;
1258 info.flags = uap->flags;
1261 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1262 if (info.sfsp && info.count > info.maxcount)
1263 uap->sysmsg_result = info.maxcount;
1265 uap->sysmsg_result = info.count;
1266 return (info.error);
1270 getfsstat_callback(struct mount *mp, void *data)
1272 struct getfsstat_info *info = data;
1278 if (info->sfsp && info->count < info->maxcount) {
1279 if (info->p && !chroot_visible_mnt(mp, info->p))
1284 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1285 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1286 * overrides MNT_WAIT.
1288 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1289 (info->flags & MNT_WAIT)) &&
1290 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1293 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1295 error = mount_path(info->p, mp, &fullpath, &freepath);
1297 info->error = error;
1300 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1301 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1302 kfree(freepath, M_TEMP);
1304 error = copyout(sp, info->sfsp, sizeof(*sp));
1306 info->error = error;
1316 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1317 long bufsize, int flags)
1319 * Get statistics on all filesystems.
1322 struct getvfsstat_info {
1323 struct statfs *sfsp;
1324 struct statvfs *vsfsp;
1332 static int getvfsstat_callback(struct mount *, void *);
1336 sys_getvfsstat(struct getvfsstat_args *uap)
1338 struct thread *td = curthread;
1339 struct proc *p = td->td_proc;
1340 struct getvfsstat_info info;
1342 bzero(&info, sizeof(info));
1344 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1345 info.sfsp = uap->buf;
1346 info.vsfsp = uap->vbuf;
1348 info.flags = uap->flags;
1351 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1352 if (info.vsfsp && info.count > info.maxcount)
1353 uap->sysmsg_result = info.maxcount;
1355 uap->sysmsg_result = info.count;
1356 return (info.error);
1360 getvfsstat_callback(struct mount *mp, void *data)
1362 struct getvfsstat_info *info = data;
1364 struct statvfs *vsp;
1369 if (info->vsfsp && info->count < info->maxcount) {
1370 if (info->p && !chroot_visible_mnt(mp, info->p))
1373 vsp = &mp->mnt_vstat;
1376 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1377 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1378 * overrides MNT_WAIT.
1380 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1381 (info->flags & MNT_WAIT)) &&
1382 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1385 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1387 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1388 (info->flags & MNT_WAIT)) &&
1389 (error = VFS_STATVFS(mp, vsp, info->p->p_ucred))) {
1393 if (mp->mnt_flag & MNT_RDONLY)
1394 vsp->f_flag |= ST_RDONLY;
1395 if (mp->mnt_flag & MNT_NOSUID)
1396 vsp->f_flag |= ST_NOSUID;
1398 error = mount_path(info->p, mp, &fullpath, &freepath);
1400 info->error = error;
1403 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1404 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1405 kfree(freepath, M_TEMP);
1407 error = copyout(sp, info->sfsp, sizeof(*sp));
1409 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1411 info->error = error;
1423 * fchdir_args(int fd)
1425 * Change current working directory to a given file descriptor.
1429 sys_fchdir(struct fchdir_args *uap)
1431 struct thread *td = curthread;
1432 struct proc *p = td->td_proc;
1433 struct filedesc *fdp = p->p_fd;
1434 struct vnode *vp, *ovp;
1437 struct nchandle nch, onch, tnch;
1440 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1442 vp = (struct vnode *)fp->f_data;
1444 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1445 if (vp->v_type != VDIR || fp->f_nchandle.ncp == NULL)
1448 error = VOP_ACCESS(vp, VEXEC, p->p_ucred);
1454 cache_copy(&fp->f_nchandle, &nch);
1457 * If the ncp has become a mount point, traverse through
1461 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1462 (mp = cache_findmount(&nch)) != NULL
1464 error = nlookup_mp(mp, &tnch);
1466 cache_unlock(&tnch); /* leave ref intact */
1468 vp = tnch.ncp->nc_vp;
1469 error = vget(vp, LK_SHARED);
1470 KKASSERT(error == 0);
1477 onch = fdp->fd_ncdir;
1478 vn_unlock(vp); /* leave ref intact */
1480 fdp->fd_ncdir = nch;
1492 kern_chdir(struct nlookupdata *nd)
1494 struct thread *td = curthread;
1495 struct proc *p = td->td_proc;
1496 struct filedesc *fdp = p->p_fd;
1497 struct vnode *vp, *ovp;
1498 struct nchandle onch;
1501 if ((error = nlookup(nd)) != 0)
1503 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1505 if ((error = vget(vp, LK_SHARED)) != 0)
1508 error = checkvp_chdir(vp, td);
1512 onch = fdp->fd_ncdir;
1513 cache_unlock(&nd->nl_nch); /* leave reference intact */
1514 fdp->fd_ncdir = nd->nl_nch;
1518 cache_zero(&nd->nl_nch);
1526 * chdir_args(char *path)
1528 * Change current working directory (``.'').
1531 sys_chdir(struct chdir_args *uap)
1533 struct nlookupdata nd;
1536 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1538 error = kern_chdir(&nd);
1544 * Helper function for raised chroot(2) security function: Refuse if
1545 * any filedescriptors are open directories.
1548 chroot_refuse_vdir_fds(struct filedesc *fdp)
1555 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1556 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1558 vp = (struct vnode *)fp->f_data;
1559 if (vp->v_type != VDIR) {
1570 * This sysctl determines if we will allow a process to chroot(2) if it
1571 * has a directory open:
1572 * 0: disallowed for all processes.
1573 * 1: allowed for processes that were not already chroot(2)'ed.
1574 * 2: allowed for all processes.
1577 static int chroot_allow_open_directories = 1;
1579 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1580 &chroot_allow_open_directories, 0, "");
1583 * chroot to the specified namecache entry. We obtain the vp from the
1584 * namecache data. The passed ncp must be locked and referenced and will
1585 * remain locked and referenced on return.
1588 kern_chroot(struct nchandle *nch)
1590 struct thread *td = curthread;
1591 struct proc *p = td->td_proc;
1592 struct filedesc *fdp = p->p_fd;
1597 * Only root can chroot
1599 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
1603 * Disallow open directory descriptors (fchdir() breakouts).
1605 if (chroot_allow_open_directories == 0 ||
1606 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1607 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1610 if ((vp = nch->ncp->nc_vp) == NULL)
1613 if ((error = vget(vp, LK_SHARED)) != 0)
1617 * Check the validity of vp as a directory to change to and
1618 * associate it with rdir/jdir.
1620 error = checkvp_chdir(vp, td);
1621 vn_unlock(vp); /* leave reference intact */
1623 vrele(fdp->fd_rdir);
1624 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1625 cache_drop(&fdp->fd_nrdir);
1626 cache_copy(nch, &fdp->fd_nrdir);
1627 if (fdp->fd_jdir == NULL) {
1630 cache_copy(nch, &fdp->fd_njdir);
1639 * chroot_args(char *path)
1641 * Change notion of root (``/'') directory.
1645 sys_chroot(struct chroot_args *uap)
1647 struct thread *td = curthread;
1648 struct nlookupdata nd;
1651 KKASSERT(td->td_proc);
1652 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1657 error = nlookup(&nd);
1659 error = kern_chroot(&nd.nl_nch);
1665 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1666 * determine whether it is legal to chdir to the vnode. The vnode's state
1667 * is not changed by this call.
1670 checkvp_chdir(struct vnode *vp, struct thread *td)
1674 if (vp->v_type != VDIR)
1677 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred);
1682 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1684 struct thread *td = curthread;
1685 struct proc *p = td->td_proc;
1686 struct lwp *lp = td->td_lwp;
1687 struct filedesc *fdp = p->p_fd;
1692 int type, indx, error;
1695 if ((oflags & O_ACCMODE) == O_ACCMODE)
1697 flags = FFLAGS(oflags);
1698 error = falloc(p, &nfp, NULL);
1702 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1705 * XXX p_dupfd is a real mess. It allows a device to return a
1706 * file descriptor to be duplicated rather then doing the open
1712 * Call vn_open() to do the lookup and assign the vnode to the
1713 * file pointer. vn_open() does not change the ref count on fp
1714 * and the vnode, on success, will be inherited by the file pointer
1717 nd->nl_flags |= NLC_LOCKVP;
1718 error = vn_open(nd, fp, flags, cmode);
1722 * handle special fdopen() case. bleh. dupfdopen() is
1723 * responsible for dropping the old contents of ofiles[indx]
1726 * Note that fsetfd() will add a ref to fp which represents
1727 * the fd_files[] assignment. We must still drop our
1730 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1731 if (fdalloc(p, 0, &indx) == 0) {
1732 error = dupfdopen(p, indx, lp->lwp_dupfd, flags, error);
1735 fdrop(fp); /* our ref */
1738 fsetfd(p, NULL, indx);
1741 fdrop(fp); /* our ref */
1742 if (error == ERESTART)
1748 * ref the vnode for ourselves so it can't be ripped out from under
1749 * is. XXX need an ND flag to request that the vnode be returned
1752 * Reserve a file descriptor but do not assign it until the open
1755 vp = (struct vnode *)fp->f_data;
1757 if ((error = fdalloc(p, 0, &indx)) != 0) {
1764 * If no error occurs the vp will have been assigned to the file
1769 if (flags & (O_EXLOCK | O_SHLOCK)) {
1770 lf.l_whence = SEEK_SET;
1773 if (flags & O_EXLOCK)
1774 lf.l_type = F_WRLCK;
1776 lf.l_type = F_RDLCK;
1777 if (flags & FNONBLOCK)
1782 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1784 * lock request failed. Clean up the reserved
1788 fsetfd(p, NULL, indx);
1792 fp->f_flag |= FHASLOCK;
1796 * Assert that all regular file vnodes were created with a object.
1798 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1799 ("open: regular file has no backing object after vn_open"));
1805 * release our private reference, leaving the one associated with the
1806 * descriptor table intact.
1808 fsetfd(p, fp, indx);
1815 * open_args(char *path, int flags, int mode)
1817 * Check permissions, allocate an open file structure,
1818 * and call the device open routine if any.
1821 sys_open(struct open_args *uap)
1823 struct nlookupdata nd;
1826 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1828 error = kern_open(&nd, uap->flags,
1829 uap->mode, &uap->sysmsg_result);
1836 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
1838 struct thread *td = curthread;
1839 struct proc *p = td->td_proc;
1847 switch (mode & S_IFMT) {
1853 error = suser_cred(p->p_ucred, PRISON_ROOT);
1860 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
1861 if ((error = nlookup(nd)) != 0)
1863 if (nd->nl_nch.ncp->nc_vp)
1865 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1869 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1870 vattr.va_rmajor = rmajor;
1871 vattr.va_rminor = rminor;
1874 switch (mode & S_IFMT) {
1875 case S_IFMT: /* used by badsect to flag bad sectors */
1876 vattr.va_type = VBAD;
1879 vattr.va_type = VCHR;
1882 vattr.va_type = VBLK;
1888 /* special directories support for HAMMER */
1889 vattr.va_type = VDIR;
1897 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
1898 nd->nl_cred, NAMEI_CREATE);
1901 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
1902 &vp, nd->nl_cred, &vattr);
1911 * mknod_args(char *path, int mode, int dev)
1913 * Create a special file.
1916 sys_mknod(struct mknod_args *uap)
1918 struct nlookupdata nd;
1921 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1923 error = kern_mknod(&nd, uap->mode,
1924 umajor(uap->dev), uminor(uap->dev));
1931 kern_mkfifo(struct nlookupdata *nd, int mode)
1933 struct thread *td = curthread;
1934 struct proc *p = td->td_proc;
1941 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
1942 if ((error = nlookup(nd)) != 0)
1944 if (nd->nl_nch.ncp->nc_vp)
1946 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1950 vattr.va_type = VFIFO;
1951 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1953 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
1960 * mkfifo_args(char *path, int mode)
1962 * Create a named pipe.
1965 sys_mkfifo(struct mkfifo_args *uap)
1967 struct nlookupdata nd;
1970 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1972 error = kern_mkfifo(&nd, uap->mode);
1977 static int hardlink_check_uid = 0;
1978 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1979 &hardlink_check_uid, 0,
1980 "Unprivileged processes cannot create hard links to files owned by other "
1982 static int hardlink_check_gid = 0;
1983 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1984 &hardlink_check_gid, 0,
1985 "Unprivileged processes cannot create hard links to files owned by other "
1989 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1995 * Shortcut if disabled
1997 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2001 * root cred can always hardlink
2003 if (suser_cred(cred, PRISON_ROOT) == 0)
2007 * Otherwise only if the originating file is owned by the
2008 * same user or group. Note that any group is allowed if
2009 * the file is owned by the caller.
2011 error = VOP_GETATTR(vp, &va);
2015 if (hardlink_check_uid) {
2016 if (cred->cr_uid != va.va_uid)
2020 if (hardlink_check_gid) {
2021 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2029 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2031 struct thread *td = curthread;
2036 * Lookup the source and obtained a locked vnode.
2038 * XXX relookup on vget failure / race ?
2041 if ((error = nlookup(nd)) != 0)
2043 vp = nd->nl_nch.ncp->nc_vp;
2044 KKASSERT(vp != NULL);
2045 if (vp->v_type == VDIR)
2046 return (EPERM); /* POSIX */
2047 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2049 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2053 * Unlock the source so we can lookup the target without deadlocking
2054 * (XXX vp is locked already, possible other deadlock?). The target
2057 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2058 nd->nl_flags &= ~NLC_NCPISLOCKED;
2059 cache_unlock(&nd->nl_nch);
2061 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2062 if ((error = nlookup(linknd)) != 0) {
2066 if (linknd->nl_nch.ncp->nc_vp) {
2072 * Finally run the new API VOP.
2074 error = can_hardlink(vp, td, td->td_proc->p_ucred);
2076 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2077 vp, linknd->nl_cred);
2084 * link_args(char *path, char *link)
2086 * Make a hard file link.
2089 sys_link(struct link_args *uap)
2091 struct nlookupdata nd, linknd;
2094 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2096 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2098 error = kern_link(&nd, &linknd);
2099 nlookup_done(&linknd);
2106 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2114 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2115 if ((error = nlookup(nd)) != 0)
2117 if (nd->nl_nch.ncp->nc_vp)
2119 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2123 vattr.va_mode = mode;
2124 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2131 * symlink(char *path, char *link)
2133 * Make a symbolic link.
2136 sys_symlink(struct symlink_args *uap)
2138 struct thread *td = curthread;
2139 struct nlookupdata nd;
2144 path = objcache_get(namei_oc, M_WAITOK);
2145 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2147 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2149 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2150 error = kern_symlink(&nd, path, mode);
2154 objcache_put(namei_oc, path);
2159 * undelete_args(char *path)
2161 * Delete a whiteout from the filesystem.
2165 sys_undelete(struct undelete_args *uap)
2167 struct nlookupdata nd;
2170 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2172 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2174 error = nlookup(&nd);
2176 error = ncp_writechk(&nd.nl_nch);
2178 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2186 kern_unlink(struct nlookupdata *nd)
2191 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2192 if ((error = nlookup(nd)) != 0)
2194 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2196 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2201 * unlink_args(char *path)
2203 * Delete a name from the filesystem.
2206 sys_unlink(struct unlink_args *uap)
2208 struct nlookupdata nd;
2211 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2213 error = kern_unlink(&nd);
2219 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2221 struct thread *td = curthread;
2222 struct proc *p = td->td_proc;
2229 fp = holdfp(p->p_fd, fd, -1);
2232 if (fp->f_type != DTYPE_VNODE) {
2236 vp = (struct vnode *)fp->f_data;
2240 new_offset = fp->f_offset + offset;
2244 error = VOP_GETATTR(vp, &vattr);
2245 new_offset = offset + vattr.va_size;
2248 new_offset = offset;
2258 * Validate the seek position. Negative offsets are not allowed
2259 * for regular files, block specials, or directories.
2262 if (new_offset < 0 &&
2263 (vp->v_type == VREG || vp->v_type == VDIR ||
2264 vp->v_type == VCHR || vp->v_type == VBLK)) {
2267 fp->f_offset = new_offset;
2270 *res = fp->f_offset;
2277 * lseek_args(int fd, int pad, off_t offset, int whence)
2279 * Reposition read/write file offset.
2282 sys_lseek(struct lseek_args *uap)
2286 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2287 &uap->sysmsg_offset);
2293 kern_access(struct nlookupdata *nd, int aflags)
2298 if ((error = nlookup(nd)) != 0)
2301 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2305 /* Flags == 0 means only check for existence. */
2314 if ((flags & VWRITE) == 0 ||
2315 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2316 error = VOP_ACCESS(vp, flags, nd->nl_cred);
2319 * If the file handle is stale we have to re-resolve the
2320 * entry. This is a hack at the moment.
2322 if (error == ESTALE) {
2324 cache_setunresolved(&nd->nl_nch);
2325 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2338 * access_args(char *path, int flags)
2340 * Check access permissions.
2343 sys_access(struct access_args *uap)
2345 struct nlookupdata nd;
2348 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2350 error = kern_access(&nd, uap->flags);
2356 kern_stat(struct nlookupdata *nd, struct stat *st)
2362 if ((error = nlookup(nd)) != 0)
2365 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2369 if ((error = vget(vp, LK_SHARED)) != 0)
2371 error = vn_stat(vp, st, nd->nl_cred);
2374 * If the file handle is stale we have to re-resolve the entry. This
2375 * is a hack at the moment.
2377 if (error == ESTALE) {
2379 cache_setunresolved(&nd->nl_nch);
2380 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2390 * stat_args(char *path, struct stat *ub)
2392 * Get file status; this version follows links.
2395 sys_stat(struct stat_args *uap)
2397 struct nlookupdata nd;
2401 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2403 error = kern_stat(&nd, &st);
2405 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2412 * lstat_args(char *path, struct stat *ub)
2414 * Get file status; this version does not follow links.
2417 sys_lstat(struct lstat_args *uap)
2419 struct nlookupdata nd;
2423 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2425 error = kern_stat(&nd, &st);
2427 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2434 * pathconf_Args(char *path, int name)
2436 * Get configurable pathname variables.
2440 sys_pathconf(struct pathconf_args *uap)
2442 struct nlookupdata nd;
2447 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2449 error = nlookup(&nd);
2451 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2454 error = VOP_PATHCONF(vp, uap->name, uap->sysmsg_fds);
2462 * kern_readlink isn't properly split yet. There is a copyin burried
2463 * in VOP_READLINK().
2466 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2468 struct thread *td = curthread;
2469 struct proc *p = td->td_proc;
2475 if ((error = nlookup(nd)) != 0)
2477 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2480 if (vp->v_type != VLNK) {
2483 aiov.iov_base = buf;
2484 aiov.iov_len = count;
2485 auio.uio_iov = &aiov;
2486 auio.uio_iovcnt = 1;
2487 auio.uio_offset = 0;
2488 auio.uio_rw = UIO_READ;
2489 auio.uio_segflg = UIO_USERSPACE;
2491 auio.uio_resid = count;
2492 error = VOP_READLINK(vp, &auio, p->p_ucred);
2495 *res = count - auio.uio_resid;
2500 * readlink_args(char *path, char *buf, int count)
2502 * Return target name of a symbolic link.
2505 sys_readlink(struct readlink_args *uap)
2507 struct nlookupdata nd;
2510 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2512 error = kern_readlink(&nd, uap->buf, uap->count,
2513 &uap->sysmsg_result);
2520 setfflags(struct vnode *vp, int flags)
2522 struct thread *td = curthread;
2523 struct proc *p = td->td_proc;
2528 * Prevent non-root users from setting flags on devices. When
2529 * a device is reused, users can retain ownership of the device
2530 * if they are allowed to set flags and programs assume that
2531 * chown can't fail when done as root.
2533 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2534 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
2538 * note: vget is required for any operation that might mod the vnode
2539 * so VINACTIVE is properly cleared.
2541 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2543 vattr.va_flags = flags;
2544 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2551 * chflags(char *path, int flags)
2553 * Change flags of a file given a path name.
2557 sys_chflags(struct chflags_args *uap)
2559 struct nlookupdata nd;
2564 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2565 /* XXX Add NLC flag indicating modifying operation? */
2567 error = nlookup(&nd);
2569 error = ncp_writechk(&nd.nl_nch);
2571 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2574 error = setfflags(vp, uap->flags);
2581 * lchflags(char *path, int flags)
2583 * Change flags of a file given a path name, but don't follow symlinks.
2587 sys_lchflags(struct lchflags_args *uap)
2589 struct nlookupdata nd;
2594 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2595 /* XXX Add NLC flag indicating modifying operation? */
2597 error = nlookup(&nd);
2599 error = ncp_writechk(&nd.nl_nch);
2601 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2604 error = setfflags(vp, uap->flags);
2611 * fchflags_args(int fd, int flags)
2613 * Change flags of a file given a file descriptor.
2617 sys_fchflags(struct fchflags_args *uap)
2619 struct thread *td = curthread;
2620 struct proc *p = td->td_proc;
2624 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2626 if (fp->f_nchandle.ncp)
2627 error = ncp_writechk(&fp->f_nchandle);
2629 error = setfflags((struct vnode *) fp->f_data, uap->flags);
2635 setfmode(struct vnode *vp, int mode)
2637 struct thread *td = curthread;
2638 struct proc *p = td->td_proc;
2643 * note: vget is required for any operation that might mod the vnode
2644 * so VINACTIVE is properly cleared.
2646 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2648 vattr.va_mode = mode & ALLPERMS;
2649 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2656 kern_chmod(struct nlookupdata *nd, int mode)
2661 /* XXX Add NLC flag indicating modifying operation? */
2662 if ((error = nlookup(nd)) != 0)
2664 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2666 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2667 error = setfmode(vp, mode);
2673 * chmod_args(char *path, int mode)
2675 * Change mode of a file given path name.
2679 sys_chmod(struct chmod_args *uap)
2681 struct nlookupdata nd;
2684 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2686 error = kern_chmod(&nd, uap->mode);
2692 * lchmod_args(char *path, int mode)
2694 * Change mode of a file given path name (don't follow links.)
2698 sys_lchmod(struct lchmod_args *uap)
2700 struct nlookupdata nd;
2703 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2705 error = kern_chmod(&nd, uap->mode);
2711 * fchmod_args(int fd, int mode)
2713 * Change mode of a file given a file descriptor.
2717 sys_fchmod(struct fchmod_args *uap)
2719 struct thread *td = curthread;
2720 struct proc *p = td->td_proc;
2724 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2726 if (fp->f_nchandle.ncp)
2727 error = ncp_writechk(&fp->f_nchandle);
2729 error = setfmode((struct vnode *)fp->f_data, uap->mode);
2735 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2737 struct thread *td = curthread;
2738 struct proc *p = td->td_proc;
2743 * note: vget is required for any operation that might mod the vnode
2744 * so VINACTIVE is properly cleared.
2746 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2750 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2757 kern_chown(struct nlookupdata *nd, int uid, int gid)
2762 /* XXX Add NLC flag indicating modifying operation? */
2763 if ((error = nlookup(nd)) != 0)
2765 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2767 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2768 error = setfown(vp, uid, gid);
2774 * chown(char *path, int uid, int gid)
2776 * Set ownership given a path name.
2779 sys_chown(struct chown_args *uap)
2781 struct nlookupdata nd;
2784 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2786 error = kern_chown(&nd, uap->uid, uap->gid);
2792 * lchown_args(char *path, int uid, int gid)
2794 * Set ownership given a path name, do not cross symlinks.
2797 sys_lchown(struct lchown_args *uap)
2799 struct nlookupdata nd;
2802 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2804 error = kern_chown(&nd, uap->uid, uap->gid);
2810 * fchown_args(int fd, int uid, int gid)
2812 * Set ownership given a file descriptor.
2816 sys_fchown(struct fchown_args *uap)
2818 struct thread *td = curthread;
2819 struct proc *p = td->td_proc;
2823 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2825 if (fp->f_nchandle.ncp)
2826 error = ncp_writechk(&fp->f_nchandle);
2828 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
2834 getutimes(const struct timeval *tvp, struct timespec *tsp)
2836 struct timeval tv[2];
2840 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2843 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2844 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2850 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2852 struct thread *td = curthread;
2853 struct proc *p = td->td_proc;
2858 * note: vget is required for any operation that might mod the vnode
2859 * so VINACTIVE is properly cleared.
2861 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2863 vattr.va_atime = ts[0];
2864 vattr.va_mtime = ts[1];
2866 vattr.va_vaflags |= VA_UTIMES_NULL;
2867 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2874 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
2876 struct timespec ts[2];
2880 if ((error = getutimes(tptr, ts)) != 0)
2882 /* XXX Add NLC flag indicating modifying operation? */
2883 if ((error = nlookup(nd)) != 0)
2885 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2887 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2889 error = setutimes(vp, ts, tptr == NULL);
2895 * utimes_args(char *path, struct timeval *tptr)
2897 * Set the access and modification times of a file.
2900 sys_utimes(struct utimes_args *uap)
2902 struct timeval tv[2];
2903 struct nlookupdata nd;
2907 error = copyin(uap->tptr, tv, sizeof(tv));
2911 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2913 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2919 * lutimes_args(char *path, struct timeval *tptr)
2921 * Set the access and modification times of a file.
2924 sys_lutimes(struct lutimes_args *uap)
2926 struct timeval tv[2];
2927 struct nlookupdata nd;
2931 error = copyin(uap->tptr, tv, sizeof(tv));
2935 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2937 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2943 kern_futimes(int fd, struct timeval *tptr)
2945 struct thread *td = curthread;
2946 struct proc *p = td->td_proc;
2947 struct timespec ts[2];
2951 error = getutimes(tptr, ts);
2954 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
2956 if (fp->f_nchandle.ncp)
2957 error = ncp_writechk(&fp->f_nchandle);
2959 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2965 * futimes_args(int fd, struct timeval *tptr)
2967 * Set the access and modification times of a file.
2970 sys_futimes(struct futimes_args *uap)
2972 struct timeval tv[2];
2976 error = copyin(uap->tptr, tv, sizeof(tv));
2981 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2987 kern_truncate(struct nlookupdata *nd, off_t length)
2995 /* XXX Add NLC flag indicating modifying operation? */
2996 if ((error = nlookup(nd)) != 0)
2998 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3000 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3002 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
3006 if (vp->v_type == VDIR) {
3008 } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0 &&
3009 (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred)) == 0) {
3011 vattr.va_size = length;
3012 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3019 * truncate(char *path, int pad, off_t length)
3021 * Truncate a file given its path name.
3024 sys_truncate(struct truncate_args *uap)
3026 struct nlookupdata nd;
3029 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3031 error = kern_truncate(&nd, uap->length);
3037 kern_ftruncate(int fd, off_t length)
3039 struct thread *td = curthread;
3040 struct proc *p = td->td_proc;
3048 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3050 if (fp->f_nchandle.ncp) {
3051 error = ncp_writechk(&fp->f_nchandle);
3055 if ((fp->f_flag & FWRITE) == 0) {
3059 vp = (struct vnode *)fp->f_data;
3060 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3061 if (vp->v_type == VDIR) {
3063 } else if ((error = vn_writechk(vp, NULL)) == 0) {
3065 vattr.va_size = length;
3066 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3075 * ftruncate_args(int fd, int pad, off_t length)
3077 * Truncate a file given a file descriptor.
3080 sys_ftruncate(struct ftruncate_args *uap)
3084 error = kern_ftruncate(uap->fd, uap->length);
3092 * Sync an open file.
3096 sys_fsync(struct fsync_args *uap)
3098 struct thread *td = curthread;
3099 struct proc *p = td->td_proc;
3105 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3107 vp = (struct vnode *)fp->f_data;
3108 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3109 if ((obj = vp->v_object) != NULL)
3110 vm_object_page_clean(obj, 0, 0, 0);
3111 if ((error = VOP_FSYNC(vp, MNT_WAIT)) == 0 && vp->v_mount)
3112 error = buf_fsync(vp);
3119 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3121 struct nchandle fnchd;
3122 struct nchandle tnchd;
3123 struct namecache *ncp;
3130 fromnd->nl_flags |= NLC_REFDVP;
3131 if ((error = nlookup(fromnd)) != 0)
3133 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3135 fnchd.mount = fromnd->nl_nch.mount;
3139 * unlock the source nch so we can lookup the target nch without
3140 * deadlocking. The target may or may not exist so we do not check
3141 * for a target vp like kern_mkdir() and other creation functions do.
3143 * The source and target directories are ref'd and rechecked after
3144 * everything is relocked to determine if the source or target file
3147 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3148 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3149 cache_unlock(&fromnd->nl_nch);
3151 tond->nl_flags |= NLC_CREATE | NLC_REFDVP;
3152 if ((error = nlookup(tond)) != 0) {
3156 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3160 tnchd.mount = tond->nl_nch.mount;
3164 * If the source and target are the same there is nothing to do
3166 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3173 * Mount points cannot be renamed or overwritten
3175 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3184 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
3185 * may have become invalid while it was unlocked, nc_vp and nc_mount
3188 if (cache_lock_nonblock(&fromnd->nl_nch) == 0) {
3189 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3190 } else if (fromnd->nl_nch.ncp > tond->nl_nch.ncp) {
3191 cache_lock(&fromnd->nl_nch);
3192 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3194 cache_unlock(&tond->nl_nch);
3195 cache_lock(&fromnd->nl_nch);
3196 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3197 cache_lock(&tond->nl_nch);
3198 cache_resolve(&tond->nl_nch, tond->nl_cred);
3200 fromnd->nl_flags |= NLC_NCPISLOCKED;
3203 * make sure the parent directories linkages are the same
3205 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3206 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3213 * Both the source and target must be within the same filesystem and
3214 * in the same filesystem as their parent directories within the
3215 * namecache topology.
3217 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3220 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3221 mp != tond->nl_nch.mount) {
3228 * Make sure the mount point is writable
3230 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3237 * If the target exists and either the source or target is a directory,
3238 * then both must be directories.
3240 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3243 if (tond->nl_nch.ncp->nc_vp) {
3244 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3246 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3247 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3249 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3255 * You cannot rename a source into itself or a subdirectory of itself.
3256 * We check this by travsersing the target directory upwards looking
3257 * for a match against the source.
3260 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3261 if (fromnd->nl_nch.ncp == ncp) {
3272 * Even though the namespaces are different, they may still represent
3273 * hardlinks to the same file. The filesystem might have a hard time
3274 * with this so we issue a NREMOVE of the source instead of a NRENAME
3275 * when we detect the situation.
3278 fdvp = fromnd->nl_dvp;
3279 tdvp = tond->nl_dvp;
3280 if (fdvp == NULL || tdvp == NULL) {
3282 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3283 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3286 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3287 fdvp, tdvp, tond->nl_cred);
3294 * rename_args(char *from, char *to)
3296 * Rename files. Source and destination must either both be directories,
3297 * or both not be directories. If target is a directory, it must be empty.
3300 sys_rename(struct rename_args *uap)
3302 struct nlookupdata fromnd, tond;
3305 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3307 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3309 error = kern_rename(&fromnd, &tond);
3310 nlookup_done(&tond);
3312 nlookup_done(&fromnd);
3317 kern_mkdir(struct nlookupdata *nd, int mode)
3319 struct thread *td = curthread;
3320 struct proc *p = td->td_proc;
3326 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3327 if ((error = nlookup(nd)) != 0)
3330 if (nd->nl_nch.ncp->nc_vp)
3332 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3335 vattr.va_type = VDIR;
3336 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3339 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, p->p_ucred, &vattr);
3346 * mkdir_args(char *path, int mode)
3348 * Make a directory file.
3352 sys_mkdir(struct mkdir_args *uap)
3354 struct nlookupdata nd;
3357 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3359 error = kern_mkdir(&nd, uap->mode);
3365 kern_rmdir(struct nlookupdata *nd)
3370 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
3371 if ((error = nlookup(nd)) != 0)
3375 * Do not allow directories representing mount points to be
3376 * deleted, even if empty. Check write perms on mount point
3377 * in case the vnode is aliased (aka nullfs).
3379 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
3381 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3383 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
3388 * rmdir_args(char *path)
3390 * Remove a directory file.
3394 sys_rmdir(struct rmdir_args *uap)
3396 struct nlookupdata nd;
3399 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3401 error = kern_rmdir(&nd);
3407 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
3408 enum uio_seg direction)
3410 struct thread *td = curthread;
3411 struct proc *p = td->td_proc;
3419 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3421 if ((fp->f_flag & FREAD) == 0) {
3425 vp = (struct vnode *)fp->f_data;
3427 if (vp->v_type != VDIR) {
3431 aiov.iov_base = buf;
3432 aiov.iov_len = count;
3433 auio.uio_iov = &aiov;
3434 auio.uio_iovcnt = 1;
3435 auio.uio_rw = UIO_READ;
3436 auio.uio_segflg = direction;
3438 auio.uio_resid = count;
3439 loff = auio.uio_offset = fp->f_offset;
3440 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3441 fp->f_offset = auio.uio_offset;
3444 if (count == auio.uio_resid) {
3445 if (union_dircheckp) {
3446 error = union_dircheckp(td, &vp, fp);
3453 if ((vp->v_flag & VROOT) &&
3454 (vp->v_mount->mnt_flag & MNT_UNION)) {
3455 struct vnode *tvp = vp;
3456 vp = vp->v_mount->mnt_vnodecovered;
3467 * WARNING! *basep may not be wide enough to accomodate the
3468 * seek offset. XXX should we hack this to return the upper 32 bits
3469 * for offsets greater then 4G?
3472 *basep = (long)loff;
3474 *res = count - auio.uio_resid;
3481 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
3483 * Read a block of directory entries in a file system independent format.
3486 sys_getdirentries(struct getdirentries_args *uap)
3491 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
3492 &uap->sysmsg_result, UIO_USERSPACE);
3494 if (error == 0 && uap->basep)
3495 error = copyout(&base, uap->basep, sizeof(*uap->basep));
3500 * getdents_args(int fd, char *buf, size_t count)
3503 sys_getdents(struct getdents_args *uap)
3507 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
3508 &uap->sysmsg_result, UIO_USERSPACE);
3514 * umask(int newmask)
3516 * Set the mode mask for creation of filesystem nodes.
3521 sys_umask(struct umask_args *uap)
3523 struct thread *td = curthread;
3524 struct proc *p = td->td_proc;
3525 struct filedesc *fdp;
3528 uap->sysmsg_result = fdp->fd_cmask;
3529 fdp->fd_cmask = uap->newmask & ALLPERMS;
3534 * revoke(char *path)
3536 * Void all references to file by ripping underlying filesystem
3541 sys_revoke(struct revoke_args *uap)
3543 struct nlookupdata nd;
3550 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3552 error = nlookup(&nd);
3554 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3555 cred = crhold(nd.nl_cred);
3558 if (vp->v_type != VCHR && vp->v_type != VBLK)
3561 error = VOP_GETATTR(vp, &vattr);
3562 if (error == 0 && cred->cr_uid != vattr.va_uid)
3563 error = suser_cred(cred, PRISON_ROOT);
3564 if (error == 0 && count_udev(vp->v_umajor, vp->v_uminor) > 0) {
3567 VOP_REVOKE(vp, REVOKEALL);
3578 * getfh_args(char *fname, fhandle_t *fhp)
3580 * Get (NFS) file handle
3582 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
3583 * mount. This allows nullfs mounts to be explicitly exported.
3585 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
3587 * nullfs mounts of subdirectories are not safe. That is, it will
3588 * work, but you do not really have protection against access to
3589 * the related parent directories.
3592 sys_getfh(struct getfh_args *uap)
3594 struct thread *td = curthread;
3595 struct nlookupdata nd;
3602 * Must be super user
3604 if ((error = suser(td)) != 0)
3608 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3610 error = nlookup(&nd);
3612 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3613 mp = nd.nl_nch.mount;
3616 bzero(&fh, sizeof(fh));
3617 fh.fh_fsid = mp->mnt_stat.f_fsid;
3618 error = VFS_VPTOFH(vp, &fh.fh_fid);
3621 error = copyout(&fh, uap->fhp, sizeof(fh));
3627 * fhopen_args(const struct fhandle *u_fhp, int flags)
3629 * syscall for the rpc.lockd to use to translate a NFS file handle into
3630 * an open descriptor.
3632 * warning: do not remove the suser() call or this becomes one giant
3636 sys_fhopen(struct fhopen_args *uap)
3638 struct thread *td = curthread;
3639 struct proc *p = td->td_proc;
3644 struct vattr *vap = &vat;
3646 int fmode, mode, error, type;
3652 * Must be super user
3658 fmode = FFLAGS(uap->flags);
3659 /* why not allow a non-read/write open for our lockd? */
3660 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3662 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3665 /* find the mount point */
3666 mp = vfs_getvfs(&fhp.fh_fsid);
3669 /* now give me my vnode, it gets returned to me locked */
3670 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
3674 * from now on we have to make sure not
3675 * to forget about the vnode
3676 * any error that causes an abort must vput(vp)
3677 * just set error = err and 'goto bad;'.
3683 if (vp->v_type == VLNK) {
3687 if (vp->v_type == VSOCK) {
3692 if (fmode & (FWRITE | O_TRUNC)) {
3693 if (vp->v_type == VDIR) {
3697 error = vn_writechk(vp, NULL);
3705 error = VOP_ACCESS(vp, mode, p->p_ucred);
3709 if (fmode & O_TRUNC) {
3710 vn_unlock(vp); /* XXX */
3711 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
3714 error = VOP_SETATTR(vp, vap, p->p_ucred);
3720 * VOP_OPEN needs the file pointer so it can potentially override
3723 * WARNING! no f_nchandle will be associated when fhopen()ing a
3726 if ((error = falloc(p, &nfp, &indx)) != 0)
3730 error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
3733 * setting f_ops this way prevents VOP_CLOSE from being
3734 * called or fdrop() releasing the vp from v_data. Since
3735 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3737 fp->f_ops = &badfileops;
3743 * The fp is given its own reference, we still have our ref and lock.
3745 * Assert that all regular files must be created with a VM object.
3747 if (vp->v_type == VREG && vp->v_object == NULL) {
3748 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
3753 * The open was successful. Handle any locking requirements.
3755 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3756 lf.l_whence = SEEK_SET;
3759 if (fmode & O_EXLOCK)
3760 lf.l_type = F_WRLCK;
3762 lf.l_type = F_RDLCK;
3763 if (fmode & FNONBLOCK)
3768 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3770 * release our private reference.
3772 fsetfd(p, NULL, indx);
3777 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3778 fp->f_flag |= FHASLOCK;
3782 * Clean up. Associate the file pointer with the previously
3783 * reserved descriptor and return it.
3786 fsetfd(p, fp, indx);
3788 uap->sysmsg_result = indx;
3792 fsetfd(p, NULL, indx);
3800 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3803 sys_fhstat(struct fhstat_args *uap)
3805 struct thread *td = curthread;
3813 * Must be super user
3819 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
3823 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3825 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
3827 error = vn_stat(vp, &sb, td->td_proc->p_ucred);
3831 error = copyout(&sb, uap->sb, sizeof(sb));
3836 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3839 sys_fhstatfs(struct fhstatfs_args *uap)
3841 struct thread *td = curthread;
3842 struct proc *p = td->td_proc;
3847 char *fullpath, *freepath;
3852 * Must be super user
3854 if ((error = suser(td)))
3857 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3860 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3863 if (p != NULL && !chroot_visible_mnt(mp, p))
3866 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
3871 if ((error = VFS_STATFS(mp, sp, p->p_ucred)) != 0)
3874 error = mount_path(p, mp, &fullpath, &freepath);
3877 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3878 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
3879 kfree(freepath, M_TEMP);
3881 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3883 bcopy(sp, &sb, sizeof(sb));
3884 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3887 return (copyout(sp, uap->buf, sizeof(*sp)));
3891 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
3894 sys_fhstatvfs(struct fhstatvfs_args *uap)
3896 struct thread *td = curthread;
3897 struct proc *p = td->td_proc;
3905 * Must be super user
3907 if ((error = suser(td)))
3910 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3913 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3916 if (p != NULL && !chroot_visible_mnt(mp, p))
3919 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
3922 sp = &mp->mnt_vstat;
3924 if ((error = VFS_STATVFS(mp, sp, p->p_ucred)) != 0)
3928 if (mp->mnt_flag & MNT_RDONLY)
3929 sp->f_flag |= ST_RDONLY;
3930 if (mp->mnt_flag & MNT_NOSUID)
3931 sp->f_flag |= ST_NOSUID;
3933 return (copyout(sp, uap->buf, sizeof(*sp)));
3938 * Syscall to push extended attribute configuration information into the
3939 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3940 * a command (int cmd), and attribute name and misc data. For now, the
3941 * attribute name is left in userspace for consumption by the VFS_op.
3942 * It will probably be changed to be copied into sysspace by the
3943 * syscall in the future, once issues with various consumers of the
3944 * attribute code have raised their hands.
3946 * Currently this is used only by UFS Extended Attributes.
3949 sys_extattrctl(struct extattrctl_args *uap)
3951 struct nlookupdata nd;
3957 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3959 error = nlookup(&nd);
3961 mp = nd.nl_nch.mount;
3962 error = VFS_EXTATTRCTL(mp, uap->cmd,
3963 uap->attrname, uap->arg,
3971 * Syscall to set a named extended attribute on a file or directory.
3972 * Accepts attribute name, and a uio structure pointing to the data to set.
3973 * The uio is consumed in the style of writev(). The real work happens
3974 * in VOP_SETEXTATTR().
3977 sys_extattr_set_file(struct extattr_set_file_args *uap)
3979 char attrname[EXTATTR_MAXNAMELEN];
3980 struct iovec aiov[UIO_SMALLIOV];
3981 struct iovec *needfree;
3982 struct nlookupdata nd;
3991 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3996 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3998 error = nlookup(&nd);
4000 error = ncp_writechk(&nd.nl_nch);
4002 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4009 iovlen = uap->iovcnt * sizeof(struct iovec);
4010 if (uap->iovcnt > UIO_SMALLIOV) {
4011 if (uap->iovcnt > UIO_MAXIOV) {
4015 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
4021 auio.uio_iovcnt = uap->iovcnt;
4022 auio.uio_rw = UIO_WRITE;
4023 auio.uio_segflg = UIO_USERSPACE;
4024 auio.uio_td = nd.nl_td;
4025 auio.uio_offset = 0;
4026 if ((error = copyin(uap->iovp, iov, iovlen)))
4029 for (i = 0; i < uap->iovcnt; i++) {
4030 if (iov->iov_len > INT_MAX - auio.uio_resid) {
4034 auio.uio_resid += iov->iov_len;
4037 cnt = auio.uio_resid;
4038 error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred);
4039 cnt -= auio.uio_resid;
4040 uap->sysmsg_result = cnt;
4045 FREE(needfree, M_IOV);
4050 * Syscall to get a named extended attribute on a file or directory.
4051 * Accepts attribute name, and a uio structure pointing to a buffer for the
4052 * data. The uio is consumed in the style of readv(). The real work
4053 * happens in VOP_GETEXTATTR();
4056 sys_extattr_get_file(struct extattr_get_file_args *uap)
4058 char attrname[EXTATTR_MAXNAMELEN];
4059 struct iovec aiov[UIO_SMALLIOV];
4060 struct iovec *needfree;
4061 struct nlookupdata nd;
4070 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4075 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4077 error = nlookup(&nd);
4079 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4085 iovlen = uap->iovcnt * sizeof (struct iovec);
4087 if (uap->iovcnt > UIO_SMALLIOV) {
4088 if (uap->iovcnt > UIO_MAXIOV) {
4092 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
4098 auio.uio_iovcnt = uap->iovcnt;
4099 auio.uio_rw = UIO_READ;
4100 auio.uio_segflg = UIO_USERSPACE;
4101 auio.uio_td = nd.nl_td;
4102 auio.uio_offset = 0;
4103 if ((error = copyin(uap->iovp, iov, iovlen)))
4106 for (i = 0; i < uap->iovcnt; i++) {
4107 if (iov->iov_len > INT_MAX - auio.uio_resid) {
4111 auio.uio_resid += iov->iov_len;
4114 cnt = auio.uio_resid;
4115 error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred);
4116 cnt -= auio.uio_resid;
4117 uap->sysmsg_result = cnt;
4122 FREE(needfree, M_IOV);
4127 * Syscall to delete a named extended attribute from a file or directory.
4128 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4131 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4133 char attrname[EXTATTR_MAXNAMELEN];
4134 struct nlookupdata nd;
4138 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4143 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4145 error = nlookup(&nd);
4147 error = ncp_writechk(&nd.nl_nch);
4149 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4155 error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred);
4162 * Determine if the mount is visible to the process.
4165 chroot_visible_mnt(struct mount *mp, struct proc *p)
4167 struct nchandle nch;
4170 * Traverse from the mount point upwards. If we hit the process
4171 * root then the mount point is visible to the process.
4173 nch = mp->mnt_ncmountpt;
4175 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4176 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4179 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4180 nch = nch.mount->mnt_ncmounton;
4182 nch.ncp = nch.ncp->nc_parent;
4187 * If the mount point is not visible to the process, but the
4188 * process root is in a subdirectory of the mount, return
4191 if (p->p_fd->fd_nrdir.mount == mp)