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>
63 #include <sys/namei.h>
64 #include <sys/nlookup.h>
65 #include <sys/dirent.h>
66 #include <sys/extattr.h>
67 #include <sys/spinlock.h>
68 #include <sys/kern_syscall.h>
69 #include <sys/objcache.h>
70 #include <sys/sysctl.h>
73 #include <sys/file2.h>
74 #include <sys/spinlock2.h>
77 #include <vm/vm_object.h>
78 #include <vm/vm_page.h>
80 #include <machine/limits.h>
81 #include <machine/stdarg.h>
83 #include <vfs/union/union.h>
85 static void mount_warning(struct mount *mp, const char *ctl, ...);
86 static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb);
87 static int checkvp_chdir (struct vnode *vn, struct thread *td);
88 static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch);
89 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
90 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
91 static int getutimes (const struct timeval *, struct timespec *);
92 static int setfown (struct vnode *, uid_t, gid_t);
93 static int setfmode (struct vnode *, int);
94 static int setfflags (struct vnode *, int);
95 static int setutimes (struct vnode *, struct vattr *,
96 const struct timespec *, int);
97 static int usermount = 0; /* if 1, non-root can mount fs. */
99 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
101 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
104 * Virtual File System System Calls
108 * Mount a file system.
110 * mount_args(char *type, char *path, int flags, caddr_t data)
115 sys_mount(struct mount_args *uap)
117 struct thread *td = curthread;
118 struct proc *p = td->td_proc;
121 struct mount *mp, *nullmp;
122 struct vfsconf *vfsp;
123 int error, flag = 0, flag2 = 0;
126 struct nlookupdata nd;
127 char fstypename[MFSNAMELEN];
137 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
141 * Do not allow NFS export by non-root users.
143 if (uap->flags & MNT_EXPORTED) {
144 error = priv_check(td, PRIV_ROOT);
149 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
151 if (priv_check(td, PRIV_ROOT))
152 uap->flags |= MNT_NOSUID | MNT_NODEV;
155 * Lookup the requested path and extract the nch and vnode.
157 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
159 if ((error = nlookup(&nd)) == 0) {
160 if (nd.nl_nch.ncp->nc_vp == NULL)
170 * If the target filesystem is resolved via a nullfs mount, then
171 * nd.nl_nch.mount will be pointing to the nullfs mount structure
172 * instead of the target file system. We need it in case we are
175 nullmp = nd.nl_nch.mount;
178 * Extract the locked+refd ncp and cleanup the nd structure
181 cache_zero(&nd.nl_nch);
184 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) && cache_findmount(&nch))
191 * now we have the locked ref'd nch and unreferenced vnode.
194 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
201 * Extract the file system type. We need to know this early, to take
202 * appropriate actions if we are dealing with a nullfs.
204 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
211 * Now we have an unlocked ref'd nch and a locked ref'd vp
213 if (uap->flags & MNT_UPDATE) {
214 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
221 if (strncmp(fstypename, "null", 5) == 0) {
229 flag2 = mp->mnt_kern_flag;
231 * We only allow the filesystem to be reloaded if it
232 * is currently mounted read-only.
234 if ((uap->flags & MNT_RELOAD) &&
235 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
238 error = EOPNOTSUPP; /* Needs translation */
242 * Only root, or the user that did the original mount is
243 * permitted to update it.
245 if (mp->mnt_stat.f_owner != cred->cr_uid &&
246 (error = priv_check(td, PRIV_ROOT))) {
251 if (vfs_busy(mp, LK_NOWAIT)) {
257 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
264 vp->v_flag |= VMOUNT;
266 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
271 * If the user is not root, ensure that they own the directory
272 * onto which we are attempting to mount.
274 if ((error = VOP_GETATTR(vp, &va)) ||
275 (va.va_uid != cred->cr_uid && (error = priv_check(td, PRIV_ROOT)))) {
280 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
285 if (vp->v_type != VDIR) {
291 if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
297 vfsp = vfsconf_find_by_name(fstypename);
301 /* Only load modules for root (very important!) */
302 if ((error = priv_check(td, PRIV_ROOT)) != 0) {
307 error = linker_load_file(fstypename, &lf);
308 if (error || lf == NULL) {
316 /* lookup again, see if the VFS was loaded */
317 vfsp = vfsconf_find_by_name(fstypename);
320 linker_file_unload(lf);
327 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
333 vp->v_flag |= VMOUNT;
336 * Allocate and initialize the filesystem.
338 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
339 TAILQ_INIT(&mp->mnt_nvnodelist);
340 TAILQ_INIT(&mp->mnt_reservedvnlist);
341 TAILQ_INIT(&mp->mnt_jlist);
342 mp->mnt_nvnodelistsize = 0;
343 lockinit(&mp->mnt_lock, "vfslock", 0, 0);
344 vfs_busy(mp, LK_NOWAIT);
345 mp->mnt_op = vfsp->vfc_vfsops;
347 vfsp->vfc_refcount++;
348 mp->mnt_stat.f_type = vfsp->vfc_typenum;
349 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
350 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
351 mp->mnt_stat.f_owner = cred->cr_uid;
352 mp->mnt_iosize_max = DFLTPHYS;
356 * Set the mount level flags.
358 if (uap->flags & MNT_RDONLY)
359 mp->mnt_flag |= MNT_RDONLY;
360 else if (mp->mnt_flag & MNT_RDONLY)
361 mp->mnt_kern_flag |= MNTK_WANTRDWR;
362 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
363 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
364 MNT_NOSYMFOLLOW | MNT_IGNORE |
365 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
366 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
367 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
368 MNT_NOSYMFOLLOW | MNT_IGNORE |
369 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
371 * Mount the filesystem.
372 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
375 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
376 if (mp->mnt_flag & MNT_UPDATE) {
377 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
378 mp->mnt_flag &= ~MNT_RDONLY;
379 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
380 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
383 mp->mnt_kern_flag = flag2;
386 vp->v_flag &= ~VMOUNT;
391 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
393 * Put the new filesystem on the mount list after root. The mount
394 * point gets its own mnt_ncmountpt (unless the VFS already set one
395 * up) which represents the root of the mount. The lookup code
396 * detects the mount point going forward and checks the root of
397 * the mount going backwards.
399 * It is not necessary to invalidate or purge the vnode underneath
400 * because elements under the mount will be given their own glue
404 if (mp->mnt_ncmountpt.ncp == NULL) {
406 * allocate, then unlock, but leave the ref intact
408 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
409 cache_unlock(&mp->mnt_ncmountpt);
411 mp->mnt_ncmounton = nch; /* inherits ref */
412 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
414 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */
415 vp->v_flag &= ~VMOUNT;
416 mountlist_insert(mp, MNTINS_LAST);
418 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
419 error = vfs_allocate_syncvnode(mp);
421 error = VFS_START(mp, 0);
424 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
425 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
426 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
427 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
428 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
429 vp->v_flag &= ~VMOUNT;
430 mp->mnt_vfc->vfc_refcount--;
442 * Scan all active processes to see if any of them have a current
443 * or root directory onto which the new filesystem has just been
444 * mounted. If so, replace them with the new mount point.
446 * The passed ncp is ref'd and locked (from the mount code) and
447 * must be associated with the vnode representing the root of the
450 struct checkdirs_info {
451 struct nchandle old_nch;
452 struct nchandle new_nch;
453 struct vnode *old_vp;
454 struct vnode *new_vp;
457 static int checkdirs_callback(struct proc *p, void *data);
460 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
462 struct checkdirs_info info;
468 * If the old mount point's vnode has a usecount of 1, it is not
469 * being held as a descriptor anywhere.
471 olddp = old_nch->ncp->nc_vp;
472 if (olddp == NULL || olddp->v_sysref.refcnt == 1)
476 * Force the root vnode of the new mount point to be resolved
477 * so we can update any matching processes.
480 if (VFS_ROOT(mp, &newdp))
481 panic("mount: lost mount");
482 cache_setunresolved(new_nch);
483 cache_setvp(new_nch, newdp);
486 * Special handling of the root node
488 if (rootvnode == olddp) {
490 vfs_cache_setroot(newdp, cache_hold(new_nch));
494 * Pass newdp separately so the callback does not have to access
495 * it via new_nch->ncp->nc_vp.
497 info.old_nch = *old_nch;
498 info.new_nch = *new_nch;
500 allproc_scan(checkdirs_callback, &info);
505 * NOTE: callback is not MP safe because the scanned process's filedesc
506 * structure can be ripped out from under us, amoung other things.
509 checkdirs_callback(struct proc *p, void *data)
511 struct checkdirs_info *info = data;
512 struct filedesc *fdp;
513 struct nchandle ncdrop1;
514 struct nchandle ncdrop2;
515 struct vnode *vprele1;
516 struct vnode *vprele2;
518 if ((fdp = p->p_fd) != NULL) {
519 cache_zero(&ncdrop1);
520 cache_zero(&ncdrop2);
525 * MPUNSAFE - XXX fdp can be pulled out from under a
528 * A shared filedesc is ok, we don't have to copy it
529 * because we are making this change globally.
531 spin_lock_wr(&fdp->fd_spin);
532 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
533 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
534 vprele1 = fdp->fd_cdir;
536 fdp->fd_cdir = info->new_vp;
537 ncdrop1 = fdp->fd_ncdir;
538 cache_copy(&info->new_nch, &fdp->fd_ncdir);
540 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
541 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
542 vprele2 = fdp->fd_rdir;
544 fdp->fd_rdir = info->new_vp;
545 ncdrop2 = fdp->fd_nrdir;
546 cache_copy(&info->new_nch, &fdp->fd_nrdir);
548 spin_unlock_wr(&fdp->fd_spin);
550 cache_drop(&ncdrop1);
552 cache_drop(&ncdrop2);
562 * Unmount a file system.
564 * Note: unmount takes a path to the vnode mounted on as argument,
565 * not special file (as before).
567 * umount_args(char *path, int flags)
572 sys_unmount(struct unmount_args *uap)
574 struct thread *td = curthread;
575 struct proc *p = td->td_proc;
576 struct mount *mp = NULL;
577 struct nlookupdata nd;
582 if (p->p_ucred->cr_prison != NULL) {
586 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
589 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
591 error = nlookup(&nd);
595 mp = nd.nl_nch.mount;
598 * Only root, or the user that did the original mount is
599 * permitted to unmount this filesystem.
601 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
602 (error = priv_check(td, PRIV_ROOT)))
606 * Don't allow unmounting the root file system.
608 if (mp->mnt_flag & MNT_ROOTFS) {
614 * Must be the root of the filesystem
616 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
624 error = dounmount(mp, uap->flags);
631 * Do the actual file system unmount.
634 dounmount_interlock(struct mount *mp)
636 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
638 mp->mnt_kern_flag |= MNTK_UNMOUNT;
643 dounmount(struct mount *mp, int flags)
645 struct namecache *ncp;
654 * Exclusive access for unmounting purposes
656 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
660 * Allow filesystems to detect that a forced unmount is in progress.
662 if (flags & MNT_FORCE)
663 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
664 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_NOWAIT);
665 error = lockmgr(&mp->mnt_lock, lflags);
667 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
668 if (mp->mnt_kern_flag & MNTK_MWAIT)
673 if (mp->mnt_flag & MNT_EXPUBLIC)
674 vfs_setpublicfs(NULL, NULL, NULL);
676 vfs_msync(mp, MNT_WAIT);
677 async_flag = mp->mnt_flag & MNT_ASYNC;
678 mp->mnt_flag &=~ MNT_ASYNC;
681 * If this filesystem isn't aliasing other filesystems,
682 * try to invalidate any remaining namecache entries and
683 * check the count afterwords.
685 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
686 cache_lock(&mp->mnt_ncmountpt);
687 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
688 cache_unlock(&mp->mnt_ncmountpt);
690 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
691 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
693 if ((flags & MNT_FORCE) == 0) {
695 mount_warning(mp, "Cannot unmount: "
701 mount_warning(mp, "Forced unmount: "
712 * nchandle records ref the mount structure. Expect a count of 1
713 * (our mount->mnt_ncmountpt).
715 if (mp->mnt_refs != 1) {
716 if ((flags & MNT_FORCE) == 0) {
717 mount_warning(mp, "Cannot unmount: "
718 "%d process references still "
719 "present", mp->mnt_refs);
722 mount_warning(mp, "Forced unmount: "
723 "%d process references still "
724 "present", mp->mnt_refs);
730 * Decomission our special mnt_syncer vnode. This also stops
731 * the vnlru code. If we are unable to unmount we recommission
735 if ((vp = mp->mnt_syncer) != NULL) {
736 mp->mnt_syncer = NULL;
739 if (((mp->mnt_flag & MNT_RDONLY) ||
740 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
741 (flags & MNT_FORCE)) {
742 error = VFS_UNMOUNT(mp, flags);
746 if (mp->mnt_syncer == NULL)
747 vfs_allocate_syncvnode(mp);
748 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
749 mp->mnt_flag |= async_flag;
750 lockmgr(&mp->mnt_lock, LK_RELEASE);
751 if (mp->mnt_kern_flag & MNTK_MWAIT)
756 * Clean up any journals still associated with the mount after
757 * filesystem activity has ceased.
759 journal_remove_all_journals(mp,
760 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
762 mountlist_remove(mp);
765 * Remove any installed vnode ops here so the individual VFSs don't
768 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
769 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
770 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
771 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
772 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
774 if (mp->mnt_ncmountpt.ncp != NULL) {
775 nch = mp->mnt_ncmountpt;
776 cache_zero(&mp->mnt_ncmountpt);
777 cache_clrmountpt(&nch);
780 if (mp->mnt_ncmounton.ncp != NULL) {
781 nch = mp->mnt_ncmounton;
782 cache_zero(&mp->mnt_ncmounton);
783 cache_clrmountpt(&nch);
787 mp->mnt_vfc->vfc_refcount--;
788 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
789 panic("unmount: dangling vnode");
790 lockmgr(&mp->mnt_lock, LK_RELEASE);
791 if (mp->mnt_kern_flag & MNTK_MWAIT)
800 mount_warning(struct mount *mp, const char *ctl, ...)
807 if (cache_fullpath(NULL, &mp->mnt_ncmounton, &ptr, &buf) == 0) {
808 kprintf("unmount(%s): ", ptr);
813 kprintf("unmount(%p", mp);
814 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
815 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
824 * Shim cache_fullpath() to handle the case where a process is chrooted into
825 * a subdirectory of a mount. In this case if the root mount matches the
826 * process root directory's mount we have to specify the process's root
827 * directory instead of the mount point, because the mount point might
828 * be above the root directory.
832 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
834 struct nchandle *nch;
836 if (p && p->p_fd->fd_nrdir.mount == mp)
837 nch = &p->p_fd->fd_nrdir;
839 nch = &mp->mnt_ncmountpt;
840 return(cache_fullpath(p, nch, rb, fb));
844 * Sync each mounted filesystem.
848 static int syncprt = 0;
849 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
852 static int sync_callback(struct mount *mp, void *data);
858 sys_sync(struct sync_args *uap)
861 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
864 * print out buffer pool stat information on each sync() call.
875 sync_callback(struct mount *mp, void *data __unused)
879 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
880 asyncflag = mp->mnt_flag & MNT_ASYNC;
881 mp->mnt_flag &= ~MNT_ASYNC;
882 vfs_msync(mp, MNT_NOWAIT);
883 VFS_SYNC(mp, MNT_NOWAIT);
884 mp->mnt_flag |= asyncflag;
889 /* XXX PRISON: could be per prison flag */
890 static int prison_quotas;
892 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
896 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
898 * Change filesystem quotas.
903 sys_quotactl(struct quotactl_args *uap)
905 struct nlookupdata nd;
914 if (p->p_ucred->cr_prison && !prison_quotas) {
919 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
921 error = nlookup(&nd);
923 mp = nd.nl_nch.mount;
924 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
925 uap->arg, nd.nl_cred);
934 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
935 * void *buf, int buflen)
937 * This function operates on a mount point and executes the specified
938 * operation using the specified control data, and possibly returns data.
940 * The actual number of bytes stored in the result buffer is returned, 0
941 * if none, otherwise an error is returned.
946 sys_mountctl(struct mountctl_args *uap)
948 struct thread *td = curthread;
949 struct proc *p = td->td_proc;
957 * Sanity and permissions checks. We must be root.
960 if (p->p_ucred->cr_prison != NULL)
962 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
963 (error = priv_check(td, PRIV_ROOT)) != 0)
967 * Argument length checks
969 if (uap->ctllen < 0 || uap->ctllen > 1024)
971 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
973 if (uap->path == NULL)
977 * Allocate the necessary buffers and copyin data
979 path = objcache_get(namei_oc, M_WAITOK);
980 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
985 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
986 error = copyin(uap->ctl, ctl, uap->ctllen);
991 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
994 * Validate the descriptor
997 fp = holdfp(p->p_fd, uap->fd, -1);
1007 * Execute the internal kernel function and clean up.
1010 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
1014 if (error == 0 && uap->sysmsg_result > 0)
1015 error = copyout(buf, uap->buf, uap->sysmsg_result);
1018 objcache_put(namei_oc, path);
1027 * Execute a mount control operation by resolving the path to a mount point
1028 * and calling vop_mountctl().
1030 * Use the mount point from the nch instead of the vnode so nullfs mounts
1031 * can properly spike the VOP.
1034 kern_mountctl(const char *path, int op, struct file *fp,
1035 const void *ctl, int ctllen,
1036 void *buf, int buflen, int *res)
1040 struct nlookupdata nd;
1045 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1047 error = nlookup(&nd);
1049 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1050 mp = nd.nl_nch.mount;
1057 * Must be the root of the filesystem
1059 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1063 error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen,
1070 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1072 struct thread *td = curthread;
1073 struct proc *p = td->td_proc;
1076 char *fullpath, *freepath;
1079 if ((error = nlookup(nd)) != 0)
1081 mp = nd->nl_nch.mount;
1083 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1086 error = mount_path(p, mp, &fullpath, &freepath);
1089 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1090 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1091 kfree(freepath, M_TEMP);
1093 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1094 bcopy(sp, buf, sizeof(*buf));
1095 /* Only root should have access to the fsid's. */
1096 if (priv_check(td, PRIV_ROOT))
1097 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1102 * statfs_args(char *path, struct statfs *buf)
1104 * Get filesystem statistics.
1109 sys_statfs(struct statfs_args *uap)
1111 struct nlookupdata nd;
1116 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1118 error = kern_statfs(&nd, &buf);
1121 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1130 kern_fstatfs(int fd, struct statfs *buf)
1132 struct thread *td = curthread;
1133 struct proc *p = td->td_proc;
1137 char *fullpath, *freepath;
1141 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1144 mp = ((struct vnode *)fp->f_data)->v_mount;
1149 if (fp->f_cred == NULL) {
1154 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1157 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1159 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1160 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1161 kfree(freepath, M_TEMP);
1163 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1164 bcopy(sp, buf, sizeof(*buf));
1166 /* Only root should have access to the fsid's. */
1167 if (priv_check(td, PRIV_ROOT))
1168 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1177 * fstatfs_args(int fd, struct statfs *buf)
1179 * Get filesystem statistics.
1184 sys_fstatfs(struct fstatfs_args *uap)
1189 error = kern_fstatfs(uap->fd, &buf);
1192 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1197 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1203 if ((error = nlookup(nd)) != 0)
1205 mp = nd->nl_nch.mount;
1206 sp = &mp->mnt_vstat;
1207 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1211 if (mp->mnt_flag & MNT_RDONLY)
1212 sp->f_flag |= ST_RDONLY;
1213 if (mp->mnt_flag & MNT_NOSUID)
1214 sp->f_flag |= ST_NOSUID;
1215 bcopy(sp, buf, sizeof(*buf));
1220 * statfs_args(char *path, struct statfs *buf)
1222 * Get filesystem statistics.
1227 sys_statvfs(struct statvfs_args *uap)
1229 struct nlookupdata nd;
1234 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1236 error = kern_statvfs(&nd, &buf);
1239 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1245 kern_fstatvfs(int fd, struct statvfs *buf)
1247 struct thread *td = curthread;
1248 struct proc *p = td->td_proc;
1255 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1257 mp = ((struct vnode *)fp->f_data)->v_mount;
1262 if (fp->f_cred == NULL) {
1266 sp = &mp->mnt_vstat;
1267 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1271 if (mp->mnt_flag & MNT_RDONLY)
1272 sp->f_flag |= ST_RDONLY;
1273 if (mp->mnt_flag & MNT_NOSUID)
1274 sp->f_flag |= ST_NOSUID;
1276 bcopy(sp, buf, sizeof(*buf));
1284 * fstatfs_args(int fd, struct statfs *buf)
1286 * Get filesystem statistics.
1291 sys_fstatvfs(struct fstatvfs_args *uap)
1297 error = kern_fstatvfs(uap->fd, &buf);
1301 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1306 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1308 * Get statistics on all filesystems.
1311 struct getfsstat_info {
1312 struct statfs *sfsp;
1320 static int getfsstat_callback(struct mount *, void *);
1326 sys_getfsstat(struct getfsstat_args *uap)
1328 struct thread *td = curthread;
1329 struct proc *p = td->td_proc;
1330 struct getfsstat_info info;
1332 bzero(&info, sizeof(info));
1334 info.maxcount = uap->bufsize / sizeof(struct statfs);
1335 info.sfsp = uap->buf;
1337 info.flags = uap->flags;
1341 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1343 if (info.sfsp && info.count > info.maxcount)
1344 uap->sysmsg_result = info.maxcount;
1346 uap->sysmsg_result = info.count;
1347 return (info.error);
1351 getfsstat_callback(struct mount *mp, void *data)
1353 struct getfsstat_info *info = data;
1359 if (info->sfsp && info->count < info->maxcount) {
1360 if (info->p && !chroot_visible_mnt(mp, info->p))
1365 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1366 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1367 * overrides MNT_WAIT.
1369 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1370 (info->flags & MNT_WAIT)) &&
1371 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1374 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1376 error = mount_path(info->p, mp, &fullpath, &freepath);
1378 info->error = error;
1381 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1382 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1383 kfree(freepath, M_TEMP);
1385 error = copyout(sp, info->sfsp, sizeof(*sp));
1387 info->error = error;
1397 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1398 long bufsize, int flags)
1400 * Get statistics on all filesystems.
1403 struct getvfsstat_info {
1404 struct statfs *sfsp;
1405 struct statvfs *vsfsp;
1413 static int getvfsstat_callback(struct mount *, void *);
1419 sys_getvfsstat(struct getvfsstat_args *uap)
1421 struct thread *td = curthread;
1422 struct proc *p = td->td_proc;
1423 struct getvfsstat_info info;
1425 bzero(&info, sizeof(info));
1427 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1428 info.sfsp = uap->buf;
1429 info.vsfsp = uap->vbuf;
1431 info.flags = uap->flags;
1435 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1436 if (info.vsfsp && info.count > info.maxcount)
1437 uap->sysmsg_result = info.maxcount;
1439 uap->sysmsg_result = info.count;
1441 return (info.error);
1445 getvfsstat_callback(struct mount *mp, void *data)
1447 struct getvfsstat_info *info = data;
1449 struct statvfs *vsp;
1454 if (info->vsfsp && info->count < info->maxcount) {
1455 if (info->p && !chroot_visible_mnt(mp, info->p))
1458 vsp = &mp->mnt_vstat;
1461 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1462 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1463 * overrides MNT_WAIT.
1465 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1466 (info->flags & MNT_WAIT)) &&
1467 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1470 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1472 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1473 (info->flags & MNT_WAIT)) &&
1474 (error = VFS_STATVFS(mp, vsp, info->p->p_ucred))) {
1478 if (mp->mnt_flag & MNT_RDONLY)
1479 vsp->f_flag |= ST_RDONLY;
1480 if (mp->mnt_flag & MNT_NOSUID)
1481 vsp->f_flag |= ST_NOSUID;
1483 error = mount_path(info->p, mp, &fullpath, &freepath);
1485 info->error = error;
1488 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1489 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1490 kfree(freepath, M_TEMP);
1492 error = copyout(sp, info->sfsp, sizeof(*sp));
1494 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1496 info->error = error;
1508 * fchdir_args(int fd)
1510 * Change current working directory to a given file descriptor.
1515 sys_fchdir(struct fchdir_args *uap)
1517 struct thread *td = curthread;
1518 struct proc *p = td->td_proc;
1519 struct filedesc *fdp = p->p_fd;
1520 struct vnode *vp, *ovp;
1523 struct nchandle nch, onch, tnch;
1526 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1529 vp = (struct vnode *)fp->f_data;
1531 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1532 if (fp->f_nchandle.ncp == NULL)
1535 error = checkvp_chdir(vp, td);
1540 cache_copy(&fp->f_nchandle, &nch);
1543 * If the ncp has become a mount point, traverse through
1547 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1548 (mp = cache_findmount(&nch)) != NULL
1550 error = nlookup_mp(mp, &tnch);
1552 cache_unlock(&tnch); /* leave ref intact */
1554 vp = tnch.ncp->nc_vp;
1555 error = vget(vp, LK_SHARED);
1556 KKASSERT(error == 0);
1563 onch = fdp->fd_ncdir;
1564 vn_unlock(vp); /* leave ref intact */
1566 fdp->fd_ncdir = nch;
1580 kern_chdir(struct nlookupdata *nd)
1582 struct thread *td = curthread;
1583 struct proc *p = td->td_proc;
1584 struct filedesc *fdp = p->p_fd;
1585 struct vnode *vp, *ovp;
1586 struct nchandle onch;
1589 if ((error = nlookup(nd)) != 0)
1591 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1593 if ((error = vget(vp, LK_SHARED)) != 0)
1596 error = checkvp_chdir(vp, td);
1600 onch = fdp->fd_ncdir;
1601 cache_unlock(&nd->nl_nch); /* leave reference intact */
1602 fdp->fd_ncdir = nd->nl_nch;
1606 cache_zero(&nd->nl_nch);
1614 * chdir_args(char *path)
1616 * Change current working directory (``.'').
1621 sys_chdir(struct chdir_args *uap)
1623 struct nlookupdata nd;
1627 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1629 error = kern_chdir(&nd);
1636 * Helper function for raised chroot(2) security function: Refuse if
1637 * any filedescriptors are open directories.
1640 chroot_refuse_vdir_fds(struct filedesc *fdp)
1647 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1648 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1650 vp = (struct vnode *)fp->f_data;
1651 if (vp->v_type != VDIR) {
1662 * This sysctl determines if we will allow a process to chroot(2) if it
1663 * has a directory open:
1664 * 0: disallowed for all processes.
1665 * 1: allowed for processes that were not already chroot(2)'ed.
1666 * 2: allowed for all processes.
1669 static int chroot_allow_open_directories = 1;
1671 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1672 &chroot_allow_open_directories, 0, "");
1675 * chroot to the specified namecache entry. We obtain the vp from the
1676 * namecache data. The passed ncp must be locked and referenced and will
1677 * remain locked and referenced on return.
1680 kern_chroot(struct nchandle *nch)
1682 struct thread *td = curthread;
1683 struct proc *p = td->td_proc;
1684 struct filedesc *fdp = p->p_fd;
1689 * Only privileged user can chroot
1691 error = priv_check_cred(p->p_ucred, PRIV_VFS_CHROOT, 0);
1696 * Disallow open directory descriptors (fchdir() breakouts).
1698 if (chroot_allow_open_directories == 0 ||
1699 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1700 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1703 if ((vp = nch->ncp->nc_vp) == NULL)
1706 if ((error = vget(vp, LK_SHARED)) != 0)
1710 * Check the validity of vp as a directory to change to and
1711 * associate it with rdir/jdir.
1713 error = checkvp_chdir(vp, td);
1714 vn_unlock(vp); /* leave reference intact */
1716 vrele(fdp->fd_rdir);
1717 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1718 cache_drop(&fdp->fd_nrdir);
1719 cache_copy(nch, &fdp->fd_nrdir);
1720 if (fdp->fd_jdir == NULL) {
1723 cache_copy(nch, &fdp->fd_njdir);
1732 * chroot_args(char *path)
1734 * Change notion of root (``/'') directory.
1739 sys_chroot(struct chroot_args *uap)
1741 struct thread *td = curthread;
1742 struct nlookupdata nd;
1745 KKASSERT(td->td_proc);
1747 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1749 nd.nl_flags |= NLC_EXEC;
1750 error = nlookup(&nd);
1752 error = kern_chroot(&nd.nl_nch);
1760 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1761 * determine whether it is legal to chdir to the vnode. The vnode's state
1762 * is not changed by this call.
1765 checkvp_chdir(struct vnode *vp, struct thread *td)
1769 if (vp->v_type != VDIR)
1772 error = VOP_EACCESS(vp, VEXEC, td->td_proc->p_ucred);
1777 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1779 struct thread *td = curthread;
1780 struct proc *p = td->td_proc;
1781 struct lwp *lp = td->td_lwp;
1782 struct filedesc *fdp = p->p_fd;
1787 int type, indx, error;
1790 if ((oflags & O_ACCMODE) == O_ACCMODE)
1792 flags = FFLAGS(oflags);
1793 error = falloc(p, &nfp, NULL);
1797 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1800 * XXX p_dupfd is a real mess. It allows a device to return a
1801 * file descriptor to be duplicated rather then doing the open
1807 * Call vn_open() to do the lookup and assign the vnode to the
1808 * file pointer. vn_open() does not change the ref count on fp
1809 * and the vnode, on success, will be inherited by the file pointer
1812 nd->nl_flags |= NLC_LOCKVP;
1813 error = vn_open(nd, fp, flags, cmode);
1817 * handle special fdopen() case. bleh. dupfdopen() is
1818 * responsible for dropping the old contents of ofiles[indx]
1821 * Note that fsetfd() will add a ref to fp which represents
1822 * the fd_files[] assignment. We must still drop our
1825 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1826 if (fdalloc(p, 0, &indx) == 0) {
1827 error = dupfdopen(p, indx, lp->lwp_dupfd, flags, error);
1830 fdrop(fp); /* our ref */
1833 fsetfd(p, NULL, indx);
1836 fdrop(fp); /* our ref */
1837 if (error == ERESTART)
1843 * ref the vnode for ourselves so it can't be ripped out from under
1844 * is. XXX need an ND flag to request that the vnode be returned
1847 * Reserve a file descriptor but do not assign it until the open
1850 vp = (struct vnode *)fp->f_data;
1852 if ((error = fdalloc(p, 0, &indx)) != 0) {
1859 * If no error occurs the vp will have been assigned to the file
1864 if (flags & (O_EXLOCK | O_SHLOCK)) {
1865 lf.l_whence = SEEK_SET;
1868 if (flags & O_EXLOCK)
1869 lf.l_type = F_WRLCK;
1871 lf.l_type = F_RDLCK;
1872 if (flags & FNONBLOCK)
1877 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1879 * lock request failed. Clean up the reserved
1883 fsetfd(p, NULL, indx);
1887 fp->f_flag |= FHASLOCK;
1891 * Assert that all regular file vnodes were created with a object.
1893 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1894 ("open: regular file has no backing object after vn_open"));
1900 * release our private reference, leaving the one associated with the
1901 * descriptor table intact.
1903 fsetfd(p, fp, indx);
1910 * open_args(char *path, int flags, int mode)
1912 * Check permissions, allocate an open file structure,
1913 * and call the device open routine if any.
1918 sys_open(struct open_args *uap)
1920 struct nlookupdata nd;
1924 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1926 error = kern_open(&nd, uap->flags,
1927 uap->mode, &uap->sysmsg_result);
1935 * openat_args(int fd, char *path, int flags, int mode)
1940 sys_openat(struct openat_args *uap)
1942 struct nlookupdata nd;
1947 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
1949 error = kern_open(&nd, uap->flags, uap->mode,
1950 &uap->sysmsg_result);
1952 nlookup_done_at(&nd, fp);
1958 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
1960 struct thread *td = curthread;
1961 struct proc *p = td->td_proc;
1970 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1971 vattr.va_rmajor = rmajor;
1972 vattr.va_rminor = rminor;
1974 switch (mode & S_IFMT) {
1975 case S_IFMT: /* used by badsect to flag bad sectors */
1976 error = priv_check_cred(p->p_ucred, PRIV_VFS_MKNOD_BAD, 0);
1977 vattr.va_type = VBAD;
1980 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1981 vattr.va_type = VCHR;
1984 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1985 vattr.va_type = VBLK;
1988 error = priv_check_cred(p->p_ucred, PRIV_VFS_MKNOD_WHT, 0);
1991 case S_IFDIR: /* special directories support for HAMMER */
1992 error = priv_check_cred(p->p_ucred, PRIV_VFS_MKNOD_DIR, 0);
1993 vattr.va_type = VDIR;
2004 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2005 if ((error = nlookup(nd)) != 0)
2007 if (nd->nl_nch.ncp->nc_vp)
2009 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2013 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2014 nd->nl_cred, NAMEI_CREATE);
2017 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2018 &vp, nd->nl_cred, &vattr);
2026 * mknod_args(char *path, int mode, int dev)
2028 * Create a special file.
2033 sys_mknod(struct mknod_args *uap)
2035 struct nlookupdata nd;
2039 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2041 error = kern_mknod(&nd, uap->mode,
2042 umajor(uap->dev), uminor(uap->dev));
2050 kern_mkfifo(struct nlookupdata *nd, int mode)
2052 struct thread *td = curthread;
2053 struct proc *p = td->td_proc;
2060 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2061 if ((error = nlookup(nd)) != 0)
2063 if (nd->nl_nch.ncp->nc_vp)
2065 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2069 vattr.va_type = VFIFO;
2070 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2072 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2079 * mkfifo_args(char *path, int mode)
2081 * Create a named pipe.
2086 sys_mkfifo(struct mkfifo_args *uap)
2088 struct nlookupdata nd;
2092 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2094 error = kern_mkfifo(&nd, uap->mode);
2100 static int hardlink_check_uid = 0;
2101 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2102 &hardlink_check_uid, 0,
2103 "Unprivileged processes cannot create hard links to files owned by other "
2105 static int hardlink_check_gid = 0;
2106 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2107 &hardlink_check_gid, 0,
2108 "Unprivileged processes cannot create hard links to files owned by other "
2112 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2118 * Shortcut if disabled
2120 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2124 * Privileged user can always hardlink
2126 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2130 * Otherwise only if the originating file is owned by the
2131 * same user or group. Note that any group is allowed if
2132 * the file is owned by the caller.
2134 error = VOP_GETATTR(vp, &va);
2138 if (hardlink_check_uid) {
2139 if (cred->cr_uid != va.va_uid)
2143 if (hardlink_check_gid) {
2144 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2152 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2154 struct thread *td = curthread;
2159 * Lookup the source and obtained a locked vnode.
2161 * You may only hardlink a file which you have write permission
2162 * on or which you own.
2164 * XXX relookup on vget failure / race ?
2167 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2168 if ((error = nlookup(nd)) != 0)
2170 vp = nd->nl_nch.ncp->nc_vp;
2171 KKASSERT(vp != NULL);
2172 if (vp->v_type == VDIR)
2173 return (EPERM); /* POSIX */
2174 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2176 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2180 * Unlock the source so we can lookup the target without deadlocking
2181 * (XXX vp is locked already, possible other deadlock?). The target
2184 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2185 nd->nl_flags &= ~NLC_NCPISLOCKED;
2186 cache_unlock(&nd->nl_nch);
2189 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2190 if ((error = nlookup(linknd)) != 0) {
2194 if (linknd->nl_nch.ncp->nc_vp) {
2198 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2204 * Finally run the new API VOP.
2206 error = can_hardlink(vp, td, td->td_proc->p_ucred);
2208 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2209 vp, linknd->nl_cred);
2216 * link_args(char *path, char *link)
2218 * Make a hard file link.
2223 sys_link(struct link_args *uap)
2225 struct nlookupdata nd, linknd;
2229 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2231 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2233 error = kern_link(&nd, &linknd);
2234 nlookup_done(&linknd);
2242 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2250 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2251 if ((error = nlookup(nd)) != 0)
2253 if (nd->nl_nch.ncp->nc_vp)
2255 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2259 vattr.va_mode = mode;
2260 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2267 * symlink(char *path, char *link)
2269 * Make a symbolic link.
2274 sys_symlink(struct symlink_args *uap)
2276 struct thread *td = curthread;
2277 struct nlookupdata nd;
2282 path = objcache_get(namei_oc, M_WAITOK);
2283 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2286 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2288 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2289 error = kern_symlink(&nd, path, mode);
2294 objcache_put(namei_oc, path);
2299 * undelete_args(char *path)
2301 * Delete a whiteout from the filesystem.
2306 sys_undelete(struct undelete_args *uap)
2308 struct nlookupdata nd;
2312 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2314 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2316 error = nlookup(&nd);
2318 error = ncp_writechk(&nd.nl_nch);
2320 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2329 kern_unlink(struct nlookupdata *nd)
2334 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2335 if ((error = nlookup(nd)) != 0)
2337 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2339 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2344 * unlink_args(char *path)
2346 * Delete a name from the filesystem.
2351 sys_unlink(struct unlink_args *uap)
2353 struct nlookupdata nd;
2357 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2359 error = kern_unlink(&nd);
2367 * unlinkat_args(int fd, char *path, int flags)
2369 * Delete the file or directory entry pointed to by fd/path.
2374 sys_unlinkat(struct unlinkat_args *uap)
2376 struct nlookupdata nd;
2380 if (uap->flags & ~AT_REMOVEDIR)
2384 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2386 if (uap->flags & AT_REMOVEDIR)
2387 error = kern_rmdir(&nd);
2389 error = kern_unlink(&nd);
2391 nlookup_done_at(&nd, fp);
2400 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2402 struct thread *td = curthread;
2403 struct proc *p = td->td_proc;
2410 fp = holdfp(p->p_fd, fd, -1);
2413 if (fp->f_type != DTYPE_VNODE) {
2417 vp = (struct vnode *)fp->f_data;
2421 spin_lock_wr(&fp->f_spin);
2422 new_offset = fp->f_offset + offset;
2427 error = VOP_GETATTR(vp, &vattr);
2429 spin_lock_wr(&fp->f_spin);
2430 new_offset = offset + vattr.va_size;
2433 new_offset = offset;
2435 spin_lock_wr(&fp->f_spin);
2440 spin_lock_wr(&fp->f_spin);
2445 * Validate the seek position. Negative offsets are not allowed
2446 * for regular files or directories.
2448 * Normally we would also not want to allow negative offsets for
2449 * character and block-special devices. However kvm addresses
2450 * on 64 bit architectures might appear to be negative and must
2454 if (new_offset < 0 &&
2455 (vp->v_type == VREG || vp->v_type == VDIR)) {
2458 fp->f_offset = new_offset;
2461 *res = fp->f_offset;
2462 spin_unlock_wr(&fp->f_spin);
2469 * lseek_args(int fd, int pad, off_t offset, int whence)
2471 * Reposition read/write file offset.
2476 sys_lseek(struct lseek_args *uap)
2480 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2481 &uap->sysmsg_offset);
2487 * Check if current process can access given file. amode is a bitmask of *_OK
2488 * access bits. flags is a bitmask of AT_* flags.
2491 kern_access(struct nlookupdata *nd, int amode, int flags)
2496 if (flags & ~AT_EACCESS)
2498 if ((error = nlookup(nd)) != 0)
2501 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2505 /* Flags == 0 means only check for existence. */
2514 if ((mode & VWRITE) == 0 ||
2515 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2516 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2519 * If the file handle is stale we have to re-resolve the
2520 * entry. This is a hack at the moment.
2522 if (error == ESTALE) {
2524 cache_setunresolved(&nd->nl_nch);
2525 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2538 * access_args(char *path, int flags)
2540 * Check access permissions.
2545 sys_access(struct access_args *uap)
2547 struct nlookupdata nd;
2551 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2553 error = kern_access(&nd, uap->flags, 0);
2561 * faccessat_args(int fd, char *path, int amode, int flags)
2563 * Check access permissions.
2568 sys_faccessat(struct faccessat_args *uap)
2570 struct nlookupdata nd;
2575 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2578 error = kern_access(&nd, uap->amode, uap->flags);
2579 nlookup_done_at(&nd, fp);
2586 kern_stat(struct nlookupdata *nd, struct stat *st)
2592 if ((error = nlookup(nd)) != 0)
2595 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2599 if ((error = vget(vp, LK_SHARED)) != 0)
2601 error = vn_stat(vp, st, nd->nl_cred);
2604 * If the file handle is stale we have to re-resolve the entry. This
2605 * is a hack at the moment.
2607 if (error == ESTALE) {
2609 cache_setunresolved(&nd->nl_nch);
2610 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2620 * stat_args(char *path, struct stat *ub)
2622 * Get file status; this version follows links.
2627 sys_stat(struct stat_args *uap)
2629 struct nlookupdata nd;
2634 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2636 error = kern_stat(&nd, &st);
2638 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2646 * lstat_args(char *path, struct stat *ub)
2648 * Get file status; this version does not follow links.
2653 sys_lstat(struct lstat_args *uap)
2655 struct nlookupdata nd;
2660 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2662 error = kern_stat(&nd, &st);
2664 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2672 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2674 * Get status of file pointed to by fd/path.
2679 sys_fstatat(struct fstatat_args *uap)
2681 struct nlookupdata nd;
2687 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2690 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2693 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2694 UIO_USERSPACE, flags);
2696 error = kern_stat(&nd, &st);
2698 error = copyout(&st, uap->sb, sizeof(*uap->sb));
2700 nlookup_done_at(&nd, fp);
2706 * pathconf_Args(char *path, int name)
2708 * Get configurable pathname variables.
2713 sys_pathconf(struct pathconf_args *uap)
2715 struct nlookupdata nd;
2721 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2723 error = nlookup(&nd);
2725 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2728 error = VOP_PATHCONF(vp, uap->name, &uap->sysmsg_reg);
2737 * kern_readlink isn't properly split yet. There is a copyin burried
2738 * in VOP_READLINK().
2741 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2743 struct thread *td = curthread;
2744 struct proc *p = td->td_proc;
2750 if ((error = nlookup(nd)) != 0)
2752 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2755 if (vp->v_type != VLNK) {
2758 aiov.iov_base = buf;
2759 aiov.iov_len = count;
2760 auio.uio_iov = &aiov;
2761 auio.uio_iovcnt = 1;
2762 auio.uio_offset = 0;
2763 auio.uio_rw = UIO_READ;
2764 auio.uio_segflg = UIO_USERSPACE;
2766 auio.uio_resid = count;
2767 error = VOP_READLINK(vp, &auio, p->p_ucred);
2770 *res = count - auio.uio_resid;
2775 * readlink_args(char *path, char *buf, int count)
2777 * Return target name of a symbolic link.
2782 sys_readlink(struct readlink_args *uap)
2784 struct nlookupdata nd;
2788 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2790 error = kern_readlink(&nd, uap->buf, uap->count,
2791 &uap->sysmsg_result);
2799 setfflags(struct vnode *vp, int flags)
2801 struct thread *td = curthread;
2802 struct proc *p = td->td_proc;
2807 * Prevent non-root users from setting flags on devices. When
2808 * a device is reused, users can retain ownership of the device
2809 * if they are allowed to set flags and programs assume that
2810 * chown can't fail when done as root.
2812 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2813 ((error = priv_check_cred(p->p_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
2817 * note: vget is required for any operation that might mod the vnode
2818 * so VINACTIVE is properly cleared.
2820 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2822 vattr.va_flags = flags;
2823 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2830 * chflags(char *path, int flags)
2832 * Change flags of a file given a path name.
2837 sys_chflags(struct chflags_args *uap)
2839 struct nlookupdata nd;
2845 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2847 error = nlookup(&nd);
2849 error = ncp_writechk(&nd.nl_nch);
2851 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2854 error = setfflags(vp, uap->flags);
2862 * lchflags(char *path, int flags)
2864 * Change flags of a file given a path name, but don't follow symlinks.
2869 sys_lchflags(struct lchflags_args *uap)
2871 struct nlookupdata nd;
2877 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2879 error = nlookup(&nd);
2881 error = ncp_writechk(&nd.nl_nch);
2883 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2886 error = setfflags(vp, uap->flags);
2894 * fchflags_args(int fd, int flags)
2896 * Change flags of a file given a file descriptor.
2901 sys_fchflags(struct fchflags_args *uap)
2903 struct thread *td = curthread;
2904 struct proc *p = td->td_proc;
2908 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2911 if (fp->f_nchandle.ncp)
2912 error = ncp_writechk(&fp->f_nchandle);
2914 error = setfflags((struct vnode *) fp->f_data, uap->flags);
2921 setfmode(struct vnode *vp, int mode)
2923 struct thread *td = curthread;
2924 struct proc *p = td->td_proc;
2929 * note: vget is required for any operation that might mod the vnode
2930 * so VINACTIVE is properly cleared.
2932 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2934 vattr.va_mode = mode & ALLPERMS;
2935 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2942 kern_chmod(struct nlookupdata *nd, int mode)
2947 if ((error = nlookup(nd)) != 0)
2949 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2951 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2952 error = setfmode(vp, mode);
2958 * chmod_args(char *path, int mode)
2960 * Change mode of a file given path name.
2965 sys_chmod(struct chmod_args *uap)
2967 struct nlookupdata nd;
2971 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2973 error = kern_chmod(&nd, uap->mode);
2980 * lchmod_args(char *path, int mode)
2982 * Change mode of a file given path name (don't follow links.)
2987 sys_lchmod(struct lchmod_args *uap)
2989 struct nlookupdata nd;
2993 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2995 error = kern_chmod(&nd, uap->mode);
3002 * fchmod_args(int fd, int mode)
3004 * Change mode of a file given a file descriptor.
3009 sys_fchmod(struct fchmod_args *uap)
3011 struct thread *td = curthread;
3012 struct proc *p = td->td_proc;
3016 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3019 if (fp->f_nchandle.ncp)
3020 error = ncp_writechk(&fp->f_nchandle);
3022 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3029 * fchmodat_args(char *path, int mode)
3031 * Change mode of a file pointed to by fd/path.
3036 sys_fchmodat(struct fchmodat_args *uap)
3038 struct nlookupdata nd;
3043 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3045 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3048 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3049 UIO_USERSPACE, flags);
3051 error = kern_chmod(&nd, uap->mode);
3052 nlookup_done_at(&nd, fp);
3058 setfown(struct vnode *vp, uid_t uid, gid_t gid)
3060 struct thread *td = curthread;
3061 struct proc *p = td->td_proc;
3066 * note: vget is required for any operation that might mod the vnode
3067 * so VINACTIVE is properly cleared.
3069 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3073 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
3080 kern_chown(struct nlookupdata *nd, int uid, int gid)
3085 if ((error = nlookup(nd)) != 0)
3087 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3089 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3090 error = setfown(vp, uid, gid);
3096 * chown(char *path, int uid, int gid)
3098 * Set ownership given a path name.
3103 sys_chown(struct chown_args *uap)
3105 struct nlookupdata nd;
3109 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3111 error = kern_chown(&nd, uap->uid, uap->gid);
3118 * lchown_args(char *path, int uid, int gid)
3120 * Set ownership given a path name, do not cross symlinks.
3125 sys_lchown(struct lchown_args *uap)
3127 struct nlookupdata nd;
3131 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3133 error = kern_chown(&nd, uap->uid, uap->gid);
3140 * fchown_args(int fd, int uid, int gid)
3142 * Set ownership given a file descriptor.
3147 sys_fchown(struct fchown_args *uap)
3149 struct thread *td = curthread;
3150 struct proc *p = td->td_proc;
3154 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3157 if (fp->f_nchandle.ncp)
3158 error = ncp_writechk(&fp->f_nchandle);
3160 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
3167 * fchownat(int fd, char *path, int uid, int gid, int flags)
3169 * Set ownership of file pointed to by fd/path.
3174 sys_fchownat(struct fchownat_args *uap)
3176 struct nlookupdata nd;
3181 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3183 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3186 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3187 UIO_USERSPACE, flags);
3189 error = kern_chown(&nd, uap->uid, uap->gid);
3190 nlookup_done_at(&nd, fp);
3197 getutimes(const struct timeval *tvp, struct timespec *tsp)
3199 struct timeval tv[2];
3203 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3206 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3207 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3213 setutimes(struct vnode *vp, struct vattr *vattr,
3214 const struct timespec *ts, int nullflag)
3216 struct thread *td = curthread;
3217 struct proc *p = td->td_proc;
3221 vattr->va_atime = ts[0];
3222 vattr->va_mtime = ts[1];
3224 vattr->va_vaflags |= VA_UTIMES_NULL;
3225 error = VOP_SETATTR(vp, vattr, p->p_ucred);
3231 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3233 struct timespec ts[2];
3238 if ((error = getutimes(tptr, ts)) != 0)
3242 * NOTE: utimes() succeeds for the owner even if the file
3243 * is not user-writable.
3245 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3247 if ((error = nlookup(nd)) != 0)
3249 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3251 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3255 * note: vget is required for any operation that might mod the vnode
3256 * so VINACTIVE is properly cleared.
3258 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3259 error = vget(vp, LK_EXCLUSIVE);
3261 error = setutimes(vp, &vattr, ts, (tptr == NULL));
3270 * utimes_args(char *path, struct timeval *tptr)
3272 * Set the access and modification times of a file.
3277 sys_utimes(struct utimes_args *uap)
3279 struct timeval tv[2];
3280 struct nlookupdata nd;
3284 error = copyin(uap->tptr, tv, sizeof(tv));
3289 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3291 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3298 * lutimes_args(char *path, struct timeval *tptr)
3300 * Set the access and modification times of a file.
3305 sys_lutimes(struct lutimes_args *uap)
3307 struct timeval tv[2];
3308 struct nlookupdata nd;
3312 error = copyin(uap->tptr, tv, sizeof(tv));
3317 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3319 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3326 * Set utimes on a file descriptor. The creds used to open the
3327 * file are used to determine whether the operation is allowed
3331 kern_futimes(int fd, struct timeval *tptr)
3333 struct thread *td = curthread;
3334 struct proc *p = td->td_proc;
3335 struct timespec ts[2];
3341 error = getutimes(tptr, ts);
3344 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3346 if (fp->f_nchandle.ncp)
3347 error = ncp_writechk(&fp->f_nchandle);
3350 error = vget(vp, LK_EXCLUSIVE);
3352 error = VOP_GETATTR(vp, &vattr);
3354 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3358 error = setutimes(vp, &vattr, ts,
3369 * futimes_args(int fd, struct timeval *tptr)
3371 * Set the access and modification times of a file.
3376 sys_futimes(struct futimes_args *uap)
3378 struct timeval tv[2];
3382 error = copyin(uap->tptr, tv, sizeof(tv));
3387 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3394 kern_truncate(struct nlookupdata *nd, off_t length)
3402 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3403 if ((error = nlookup(nd)) != 0)
3405 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3407 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3409 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
3413 if (vp->v_type == VDIR) {
3415 } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3417 vattr.va_size = length;
3418 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3425 * truncate(char *path, int pad, off_t length)
3427 * Truncate a file given its path name.
3432 sys_truncate(struct truncate_args *uap)
3434 struct nlookupdata nd;
3438 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3440 error = kern_truncate(&nd, uap->length);
3447 kern_ftruncate(int fd, off_t length)
3449 struct thread *td = curthread;
3450 struct proc *p = td->td_proc;
3458 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3460 if (fp->f_nchandle.ncp) {
3461 error = ncp_writechk(&fp->f_nchandle);
3465 if ((fp->f_flag & FWRITE) == 0) {
3469 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3473 vp = (struct vnode *)fp->f_data;
3474 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3475 if (vp->v_type == VDIR) {
3477 } else if ((error = vn_writechk(vp, NULL)) == 0) {
3479 vattr.va_size = length;
3480 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3489 * ftruncate_args(int fd, int pad, off_t length)
3491 * Truncate a file given a file descriptor.
3496 sys_ftruncate(struct ftruncate_args *uap)
3501 error = kern_ftruncate(uap->fd, uap->length);
3510 * Sync an open file.
3515 sys_fsync(struct fsync_args *uap)
3517 struct thread *td = curthread;
3518 struct proc *p = td->td_proc;
3524 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3527 vp = (struct vnode *)fp->f_data;
3528 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3529 if ((obj = vp->v_object) != NULL)
3530 vm_object_page_clean(obj, 0, 0, 0);
3531 error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
3532 if (error == 0 && vp->v_mount)
3533 error = buf_fsync(vp);
3542 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3544 struct nchandle fnchd;
3545 struct nchandle tnchd;
3546 struct namecache *ncp;
3553 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3554 if ((error = nlookup(fromnd)) != 0)
3556 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3558 fnchd.mount = fromnd->nl_nch.mount;
3562 * unlock the source nch so we can lookup the target nch without
3563 * deadlocking. The target may or may not exist so we do not check
3564 * for a target vp like kern_mkdir() and other creation functions do.
3566 * The source and target directories are ref'd and rechecked after
3567 * everything is relocked to determine if the source or target file
3570 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3571 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3572 cache_unlock(&fromnd->nl_nch);
3574 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3575 if ((error = nlookup(tond)) != 0) {
3579 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3583 tnchd.mount = tond->nl_nch.mount;
3587 * If the source and target are the same there is nothing to do
3589 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3596 * Mount points cannot be renamed or overwritten
3598 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3607 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
3608 * may have become invalid while it was unlocked, nc_vp and nc_mount
3611 if (cache_lock_nonblock(&fromnd->nl_nch) == 0) {
3612 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3613 } else if (fromnd->nl_nch.ncp > tond->nl_nch.ncp) {
3614 cache_lock(&fromnd->nl_nch);
3615 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3617 cache_unlock(&tond->nl_nch);
3618 cache_lock(&fromnd->nl_nch);
3619 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3620 cache_lock(&tond->nl_nch);
3621 cache_resolve(&tond->nl_nch, tond->nl_cred);
3623 fromnd->nl_flags |= NLC_NCPISLOCKED;
3626 * make sure the parent directories linkages are the same
3628 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3629 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3636 * Both the source and target must be within the same filesystem and
3637 * in the same filesystem as their parent directories within the
3638 * namecache topology.
3640 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3643 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3644 mp != tond->nl_nch.mount) {
3651 * Make sure the mount point is writable
3653 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3660 * If the target exists and either the source or target is a directory,
3661 * then both must be directories.
3663 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3666 if (tond->nl_nch.ncp->nc_vp) {
3667 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3669 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3670 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3672 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3678 * You cannot rename a source into itself or a subdirectory of itself.
3679 * We check this by travsersing the target directory upwards looking
3680 * for a match against the source.
3683 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3684 if (fromnd->nl_nch.ncp == ncp) {
3695 * Even though the namespaces are different, they may still represent
3696 * hardlinks to the same file. The filesystem might have a hard time
3697 * with this so we issue a NREMOVE of the source instead of a NRENAME
3698 * when we detect the situation.
3701 fdvp = fromnd->nl_dvp;
3702 tdvp = tond->nl_dvp;
3703 if (fdvp == NULL || tdvp == NULL) {
3705 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3706 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3709 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3710 fdvp, tdvp, tond->nl_cred);
3717 * rename_args(char *from, char *to)
3719 * Rename files. Source and destination must either both be directories,
3720 * or both not be directories. If target is a directory, it must be empty.
3725 sys_rename(struct rename_args *uap)
3727 struct nlookupdata fromnd, tond;
3731 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3733 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3735 error = kern_rename(&fromnd, &tond);
3736 nlookup_done(&tond);
3738 nlookup_done(&fromnd);
3744 kern_mkdir(struct nlookupdata *nd, int mode)
3746 struct thread *td = curthread;
3747 struct proc *p = td->td_proc;
3753 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3754 if ((error = nlookup(nd)) != 0)
3757 if (nd->nl_nch.ncp->nc_vp)
3759 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3762 vattr.va_type = VDIR;
3763 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3766 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, p->p_ucred, &vattr);
3773 * mkdir_args(char *path, int mode)
3775 * Make a directory file.
3780 sys_mkdir(struct mkdir_args *uap)
3782 struct nlookupdata nd;
3786 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3788 error = kern_mkdir(&nd, uap->mode);
3795 kern_rmdir(struct nlookupdata *nd)
3800 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
3801 if ((error = nlookup(nd)) != 0)
3805 * Do not allow directories representing mount points to be
3806 * deleted, even if empty. Check write perms on mount point
3807 * in case the vnode is aliased (aka nullfs).
3809 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
3811 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3813 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
3818 * rmdir_args(char *path)
3820 * Remove a directory file.
3825 sys_rmdir(struct rmdir_args *uap)
3827 struct nlookupdata nd;
3831 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3833 error = kern_rmdir(&nd);
3840 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
3841 enum uio_seg direction)
3843 struct thread *td = curthread;
3844 struct proc *p = td->td_proc;
3852 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3854 if ((fp->f_flag & FREAD) == 0) {
3858 vp = (struct vnode *)fp->f_data;
3860 if (vp->v_type != VDIR) {
3864 aiov.iov_base = buf;
3865 aiov.iov_len = count;
3866 auio.uio_iov = &aiov;
3867 auio.uio_iovcnt = 1;
3868 auio.uio_rw = UIO_READ;
3869 auio.uio_segflg = direction;
3871 auio.uio_resid = count;
3872 loff = auio.uio_offset = fp->f_offset;
3873 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3874 fp->f_offset = auio.uio_offset;
3877 if (count == auio.uio_resid) {
3878 if (union_dircheckp) {
3879 error = union_dircheckp(td, &vp, fp);
3886 if ((vp->v_flag & VROOT) &&
3887 (vp->v_mount->mnt_flag & MNT_UNION)) {
3888 struct vnode *tvp = vp;
3889 vp = vp->v_mount->mnt_vnodecovered;
3900 * WARNING! *basep may not be wide enough to accomodate the
3901 * seek offset. XXX should we hack this to return the upper 32 bits
3902 * for offsets greater then 4G?
3905 *basep = (long)loff;
3907 *res = count - auio.uio_resid;
3914 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
3916 * Read a block of directory entries in a file system independent format.
3921 sys_getdirentries(struct getdirentries_args *uap)
3927 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
3928 &uap->sysmsg_result, UIO_USERSPACE);
3931 if (error == 0 && uap->basep)
3932 error = copyout(&base, uap->basep, sizeof(*uap->basep));
3937 * getdents_args(int fd, char *buf, size_t count)
3942 sys_getdents(struct getdents_args *uap)
3947 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
3948 &uap->sysmsg_result, UIO_USERSPACE);
3955 * Set the mode mask for creation of filesystem nodes.
3957 * umask(int newmask)
3962 sys_umask(struct umask_args *uap)
3964 struct thread *td = curthread;
3965 struct proc *p = td->td_proc;
3966 struct filedesc *fdp;
3969 uap->sysmsg_result = fdp->fd_cmask;
3970 fdp->fd_cmask = uap->newmask & ALLPERMS;
3975 * revoke(char *path)
3977 * Void all references to file by ripping underlying filesystem
3983 sys_revoke(struct revoke_args *uap)
3985 struct nlookupdata nd;
3993 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3995 error = nlookup(&nd);
3997 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3998 cred = crhold(nd.nl_cred);
4002 error = VOP_GETATTR(vp, &vattr);
4003 if (error == 0 && cred->cr_uid != vattr.va_uid)
4004 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4005 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4007 error = vrevoke(vp, cred);
4008 } else if (error == 0) {
4009 error = vrevoke(vp, cred);
4020 * getfh_args(char *fname, fhandle_t *fhp)
4022 * Get (NFS) file handle
4024 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4025 * mount. This allows nullfs mounts to be explicitly exported.
4027 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4029 * nullfs mounts of subdirectories are not safe. That is, it will
4030 * work, but you do not really have protection against access to
4031 * the related parent directories.
4036 sys_getfh(struct getfh_args *uap)
4038 struct thread *td = curthread;
4039 struct nlookupdata nd;
4046 * Must be super user
4048 if ((error = priv_check(td, PRIV_ROOT)) != 0)
4053 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4055 error = nlookup(&nd);
4057 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4058 mp = nd.nl_nch.mount;
4061 bzero(&fh, sizeof(fh));
4062 fh.fh_fsid = mp->mnt_stat.f_fsid;
4063 error = VFS_VPTOFH(vp, &fh.fh_fid);
4066 error = copyout(&fh, uap->fhp, sizeof(fh));
4073 * fhopen_args(const struct fhandle *u_fhp, int flags)
4075 * syscall for the rpc.lockd to use to translate a NFS file handle into
4076 * an open descriptor.
4078 * warning: do not remove the priv_check() call or this becomes one giant
4084 sys_fhopen(struct fhopen_args *uap)
4086 struct thread *td = curthread;
4087 struct proc *p = td->td_proc;
4092 struct vattr *vap = &vat;
4094 int fmode, mode, error, type;
4100 * Must be super user
4102 error = priv_check(td, PRIV_ROOT);
4106 fmode = FFLAGS(uap->flags);
4109 * Why not allow a non-read/write open for our lockd?
4111 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4113 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4118 * Find the mount point
4121 mp = vfs_getvfs(&fhp.fh_fsid);
4126 /* now give me my vnode, it gets returned to me locked */
4127 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4131 * from now on we have to make sure not
4132 * to forget about the vnode
4133 * any error that causes an abort must vput(vp)
4134 * just set error = err and 'goto bad;'.
4140 if (vp->v_type == VLNK) {
4144 if (vp->v_type == VSOCK) {
4149 if (fmode & (FWRITE | O_TRUNC)) {
4150 if (vp->v_type == VDIR) {
4154 error = vn_writechk(vp, NULL);
4162 error = VOP_ACCESS(vp, mode, p->p_ucred);
4166 if (fmode & O_TRUNC) {
4167 vn_unlock(vp); /* XXX */
4168 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4171 error = VOP_SETATTR(vp, vap, p->p_ucred);
4177 * VOP_OPEN needs the file pointer so it can potentially override
4180 * WARNING! no f_nchandle will be associated when fhopen()ing a
4183 if ((error = falloc(p, &nfp, &indx)) != 0)
4187 error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
4190 * setting f_ops this way prevents VOP_CLOSE from being
4191 * called or fdrop() releasing the vp from v_data. Since
4192 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4194 fp->f_ops = &badfileops;
4200 * The fp is given its own reference, we still have our ref and lock.
4202 * Assert that all regular files must be created with a VM object.
4204 if (vp->v_type == VREG && vp->v_object == NULL) {
4205 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
4210 * The open was successful. Handle any locking requirements.
4212 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4213 lf.l_whence = SEEK_SET;
4216 if (fmode & O_EXLOCK)
4217 lf.l_type = F_WRLCK;
4219 lf.l_type = F_RDLCK;
4220 if (fmode & FNONBLOCK)
4225 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
4227 * release our private reference.
4229 fsetfd(p, NULL, indx);
4234 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4235 fp->f_flag |= FHASLOCK;
4239 * Clean up. Associate the file pointer with the previously
4240 * reserved descriptor and return it.
4244 fsetfd(p, fp, indx);
4246 uap->sysmsg_result = indx;
4250 fsetfd(p, NULL, indx);
4260 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4265 sys_fhstat(struct fhstat_args *uap)
4267 struct thread *td = curthread;
4275 * Must be super user
4277 error = priv_check(td, PRIV_ROOT);
4281 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4286 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4289 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4290 error = vn_stat(vp, &sb, td->td_proc->p_ucred);
4296 error = copyout(&sb, uap->sb, sizeof(sb));
4301 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4306 sys_fhstatfs(struct fhstatfs_args *uap)
4308 struct thread *td = curthread;
4309 struct proc *p = td->td_proc;
4314 char *fullpath, *freepath;
4319 * Must be super user
4321 if ((error = priv_check(td, PRIV_ROOT)))
4324 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4329 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4333 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4338 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4343 if ((error = VFS_STATFS(mp, sp, p->p_ucred)) != 0)
4346 error = mount_path(p, mp, &fullpath, &freepath);
4349 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4350 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4351 kfree(freepath, M_TEMP);
4353 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4354 if (priv_check(td, PRIV_ROOT)) {
4355 bcopy(sp, &sb, sizeof(sb));
4356 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4359 error = copyout(sp, uap->buf, sizeof(*sp));
4366 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4371 sys_fhstatvfs(struct fhstatvfs_args *uap)
4373 struct thread *td = curthread;
4374 struct proc *p = td->td_proc;
4382 * Must be super user
4384 if ((error = priv_check(td, PRIV_ROOT)))
4387 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4392 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4396 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4401 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4404 sp = &mp->mnt_vstat;
4406 if ((error = VFS_STATVFS(mp, sp, p->p_ucred)) != 0)
4410 if (mp->mnt_flag & MNT_RDONLY)
4411 sp->f_flag |= ST_RDONLY;
4412 if (mp->mnt_flag & MNT_NOSUID)
4413 sp->f_flag |= ST_NOSUID;
4414 error = copyout(sp, uap->buf, sizeof(*sp));
4422 * Syscall to push extended attribute configuration information into the
4423 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4424 * a command (int cmd), and attribute name and misc data. For now, the
4425 * attribute name is left in userspace for consumption by the VFS_op.
4426 * It will probably be changed to be copied into sysspace by the
4427 * syscall in the future, once issues with various consumers of the
4428 * attribute code have raised their hands.
4430 * Currently this is used only by UFS Extended Attributes.
4435 sys_extattrctl(struct extattrctl_args *uap)
4437 struct nlookupdata nd;
4444 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4446 error = nlookup(&nd);
4448 mp = nd.nl_nch.mount;
4449 error = VFS_EXTATTRCTL(mp, uap->cmd,
4450 uap->attrname, uap->arg,
4460 * Syscall to set a named extended attribute on a file or directory.
4461 * Accepts attribute name, and a uio structure pointing to the data to set.
4462 * The uio is consumed in the style of writev(). The real work happens
4463 * in VOP_SETEXTATTR().
4468 sys_extattr_set_file(struct extattr_set_file_args *uap)
4470 char attrname[EXTATTR_MAXNAMELEN];
4471 struct iovec aiov[UIO_SMALLIOV];
4472 struct iovec *needfree;
4473 struct nlookupdata nd;
4482 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4488 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4490 error = nlookup(&nd);
4492 error = ncp_writechk(&nd.nl_nch);
4494 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4502 iovlen = uap->iovcnt * sizeof(struct iovec);
4503 if (uap->iovcnt > UIO_SMALLIOV) {
4504 if (uap->iovcnt > UIO_MAXIOV) {
4508 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
4514 auio.uio_iovcnt = uap->iovcnt;
4515 auio.uio_rw = UIO_WRITE;
4516 auio.uio_segflg = UIO_USERSPACE;
4517 auio.uio_td = nd.nl_td;
4518 auio.uio_offset = 0;
4519 if ((error = copyin(uap->iovp, iov, iovlen)))
4522 for (i = 0; i < uap->iovcnt; i++) {
4523 if (iov->iov_len > LONG_MAX - auio.uio_resid) {
4527 auio.uio_resid += iov->iov_len;
4530 cnt = auio.uio_resid;
4531 error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred);
4532 cnt -= auio.uio_resid;
4533 uap->sysmsg_result = cnt;
4539 FREE(needfree, M_IOV);
4544 * Syscall to get a named extended attribute on a file or directory.
4545 * Accepts attribute name, and a uio structure pointing to a buffer for the
4546 * data. The uio is consumed in the style of readv(). The real work
4547 * happens in VOP_GETEXTATTR();
4552 sys_extattr_get_file(struct extattr_get_file_args *uap)
4554 char attrname[EXTATTR_MAXNAMELEN];
4555 struct iovec aiov[UIO_SMALLIOV];
4556 struct iovec *needfree;
4557 struct nlookupdata nd;
4566 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4572 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4574 error = nlookup(&nd);
4576 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4583 iovlen = uap->iovcnt * sizeof (struct iovec);
4585 if (uap->iovcnt > UIO_SMALLIOV) {
4586 if (uap->iovcnt > UIO_MAXIOV) {
4590 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
4596 auio.uio_iovcnt = uap->iovcnt;
4597 auio.uio_rw = UIO_READ;
4598 auio.uio_segflg = UIO_USERSPACE;
4599 auio.uio_td = nd.nl_td;
4600 auio.uio_offset = 0;
4601 if ((error = copyin(uap->iovp, iov, iovlen)))
4604 for (i = 0; i < uap->iovcnt; i++) {
4605 if (iov->iov_len > LONG_MAX - auio.uio_resid) {
4609 auio.uio_resid += iov->iov_len;
4612 cnt = auio.uio_resid;
4613 error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred);
4614 cnt -= auio.uio_resid;
4615 uap->sysmsg_result = cnt;
4621 FREE(needfree, M_IOV);
4626 * Syscall to delete a named extended attribute from a file or directory.
4627 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4632 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4634 char attrname[EXTATTR_MAXNAMELEN];
4635 struct nlookupdata nd;
4639 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4644 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4646 error = nlookup(&nd);
4648 error = ncp_writechk(&nd.nl_nch);
4650 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4652 error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred);
4662 * Determine if the mount is visible to the process.
4665 chroot_visible_mnt(struct mount *mp, struct proc *p)
4667 struct nchandle nch;
4670 * Traverse from the mount point upwards. If we hit the process
4671 * root then the mount point is visible to the process.
4673 nch = mp->mnt_ncmountpt;
4675 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4676 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4679 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4680 nch = nch.mount->mnt_ncmounton;
4682 nch.ncp = nch.ncp->nc_parent;
4687 * If the mount point is not visible to the process, but the
4688 * process root is in a subdirectory of the mount, return
4691 if (p->p_fd->fd_nrdir.mount == mp)