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;
120 struct mount *mp, *nullmp;
121 struct vfsconf *vfsp;
122 int error, flag = 0, flag2 = 0;
125 struct nlookupdata nd;
126 char fstypename[MFSNAMELEN];
135 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
139 * Do not allow NFS export by non-root users.
141 if (uap->flags & MNT_EXPORTED) {
142 error = priv_check(td, PRIV_ROOT);
147 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
149 if (priv_check(td, PRIV_ROOT))
150 uap->flags |= MNT_NOSUID | MNT_NODEV;
153 * Lookup the requested path and extract the nch and vnode.
155 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
157 if ((error = nlookup(&nd)) == 0) {
158 if (nd.nl_nch.ncp->nc_vp == NULL)
168 * If the target filesystem is resolved via a nullfs mount, then
169 * nd.nl_nch.mount will be pointing to the nullfs mount structure
170 * instead of the target file system. We need it in case we are
173 nullmp = nd.nl_nch.mount;
176 * Extract the locked+refd ncp and cleanup the nd structure
179 cache_zero(&nd.nl_nch);
182 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) && cache_findmount(&nch))
189 * now we have the locked ref'd nch and unreferenced vnode.
192 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
199 * Extract the file system type. We need to know this early, to take
200 * appropriate actions if we are dealing with a nullfs.
202 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
209 * Now we have an unlocked ref'd nch and a locked ref'd vp
211 if (uap->flags & MNT_UPDATE) {
212 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
219 if (strncmp(fstypename, "null", 5) == 0) {
227 flag2 = mp->mnt_kern_flag;
229 * We only allow the filesystem to be reloaded if it
230 * is currently mounted read-only.
232 if ((uap->flags & MNT_RELOAD) &&
233 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
236 error = EOPNOTSUPP; /* Needs translation */
240 * Only root, or the user that did the original mount is
241 * permitted to update it.
243 if (mp->mnt_stat.f_owner != cred->cr_uid &&
244 (error = priv_check(td, PRIV_ROOT))) {
249 if (vfs_busy(mp, LK_NOWAIT)) {
255 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
262 vp->v_flag |= VMOUNT;
264 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
269 * If the user is not root, ensure that they own the directory
270 * onto which we are attempting to mount.
272 if ((error = VOP_GETATTR(vp, &va)) ||
273 (va.va_uid != cred->cr_uid && (error = priv_check(td, PRIV_ROOT)))) {
278 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
283 if (vp->v_type != VDIR) {
289 if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
295 vfsp = vfsconf_find_by_name(fstypename);
299 /* Only load modules for root (very important!) */
300 if ((error = priv_check(td, PRIV_ROOT)) != 0) {
305 error = linker_load_file(fstypename, &lf);
306 if (error || lf == NULL) {
314 /* lookup again, see if the VFS was loaded */
315 vfsp = vfsconf_find_by_name(fstypename);
318 linker_file_unload(lf);
325 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
331 vp->v_flag |= VMOUNT;
334 * Allocate and initialize the filesystem.
336 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
337 TAILQ_INIT(&mp->mnt_nvnodelist);
338 TAILQ_INIT(&mp->mnt_reservedvnlist);
339 TAILQ_INIT(&mp->mnt_jlist);
340 mp->mnt_nvnodelistsize = 0;
341 lockinit(&mp->mnt_lock, "vfslock", 0, 0);
342 vfs_busy(mp, LK_NOWAIT);
343 mp->mnt_op = vfsp->vfc_vfsops;
345 vfsp->vfc_refcount++;
346 mp->mnt_stat.f_type = vfsp->vfc_typenum;
347 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
348 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
349 mp->mnt_stat.f_owner = cred->cr_uid;
350 mp->mnt_iosize_max = DFLTPHYS;
354 * Set the mount level flags.
356 if (uap->flags & MNT_RDONLY)
357 mp->mnt_flag |= MNT_RDONLY;
358 else if (mp->mnt_flag & MNT_RDONLY)
359 mp->mnt_kern_flag |= MNTK_WANTRDWR;
360 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
361 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
362 MNT_NOSYMFOLLOW | MNT_IGNORE |
363 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
364 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
365 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
366 MNT_NOSYMFOLLOW | MNT_IGNORE |
367 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
369 * Mount the filesystem.
370 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
373 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
374 if (mp->mnt_flag & MNT_UPDATE) {
375 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
376 mp->mnt_flag &= ~MNT_RDONLY;
377 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
378 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
381 mp->mnt_kern_flag = flag2;
384 vp->v_flag &= ~VMOUNT;
389 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
391 * Put the new filesystem on the mount list after root. The mount
392 * point gets its own mnt_ncmountpt (unless the VFS already set one
393 * up) which represents the root of the mount. The lookup code
394 * detects the mount point going forward and checks the root of
395 * the mount going backwards.
397 * It is not necessary to invalidate or purge the vnode underneath
398 * because elements under the mount will be given their own glue
402 if (mp->mnt_ncmountpt.ncp == NULL) {
404 * allocate, then unlock, but leave the ref intact
406 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
407 cache_unlock(&mp->mnt_ncmountpt);
409 mp->mnt_ncmounton = nch; /* inherits ref */
410 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
412 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */
413 vp->v_flag &= ~VMOUNT;
414 mountlist_insert(mp, MNTINS_LAST);
416 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
417 error = vfs_allocate_syncvnode(mp);
419 error = VFS_START(mp, 0);
422 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
423 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
424 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
425 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
426 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
427 vp->v_flag &= ~VMOUNT;
428 mp->mnt_vfc->vfc_refcount--;
440 * Scan all active processes to see if any of them have a current
441 * or root directory onto which the new filesystem has just been
442 * mounted. If so, replace them with the new mount point.
444 * The passed ncp is ref'd and locked (from the mount code) and
445 * must be associated with the vnode representing the root of the
448 struct checkdirs_info {
449 struct nchandle old_nch;
450 struct nchandle new_nch;
451 struct vnode *old_vp;
452 struct vnode *new_vp;
455 static int checkdirs_callback(struct proc *p, void *data);
458 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
460 struct checkdirs_info info;
466 * If the old mount point's vnode has a usecount of 1, it is not
467 * being held as a descriptor anywhere.
469 olddp = old_nch->ncp->nc_vp;
470 if (olddp == NULL || olddp->v_sysref.refcnt == 1)
474 * Force the root vnode of the new mount point to be resolved
475 * so we can update any matching processes.
478 if (VFS_ROOT(mp, &newdp))
479 panic("mount: lost mount");
480 cache_setunresolved(new_nch);
481 cache_setvp(new_nch, newdp);
484 * Special handling of the root node
486 if (rootvnode == olddp) {
488 vfs_cache_setroot(newdp, cache_hold(new_nch));
492 * Pass newdp separately so the callback does not have to access
493 * it via new_nch->ncp->nc_vp.
495 info.old_nch = *old_nch;
496 info.new_nch = *new_nch;
498 allproc_scan(checkdirs_callback, &info);
503 * NOTE: callback is not MP safe because the scanned process's filedesc
504 * structure can be ripped out from under us, amoung other things.
507 checkdirs_callback(struct proc *p, void *data)
509 struct checkdirs_info *info = data;
510 struct filedesc *fdp;
511 struct nchandle ncdrop1;
512 struct nchandle ncdrop2;
513 struct vnode *vprele1;
514 struct vnode *vprele2;
516 if ((fdp = p->p_fd) != NULL) {
517 cache_zero(&ncdrop1);
518 cache_zero(&ncdrop2);
523 * MPUNSAFE - XXX fdp can be pulled out from under a
526 * A shared filedesc is ok, we don't have to copy it
527 * because we are making this change globally.
529 spin_lock_wr(&fdp->fd_spin);
530 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
531 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
532 vprele1 = fdp->fd_cdir;
534 fdp->fd_cdir = info->new_vp;
535 ncdrop1 = fdp->fd_ncdir;
536 cache_copy(&info->new_nch, &fdp->fd_ncdir);
538 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
539 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
540 vprele2 = fdp->fd_rdir;
542 fdp->fd_rdir = info->new_vp;
543 ncdrop2 = fdp->fd_nrdir;
544 cache_copy(&info->new_nch, &fdp->fd_nrdir);
546 spin_unlock_wr(&fdp->fd_spin);
548 cache_drop(&ncdrop1);
550 cache_drop(&ncdrop2);
560 * Unmount a file system.
562 * Note: unmount takes a path to the vnode mounted on as argument,
563 * not special file (as before).
565 * umount_args(char *path, int flags)
570 sys_unmount(struct unmount_args *uap)
572 struct thread *td = curthread;
573 struct proc *p = td->td_proc;
574 struct mount *mp = NULL;
575 struct nlookupdata nd;
580 if (td->td_ucred->cr_prison != NULL) {
584 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
587 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
589 error = nlookup(&nd);
593 mp = nd.nl_nch.mount;
596 * Only root, or the user that did the original mount is
597 * permitted to unmount this filesystem.
599 if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) &&
600 (error = priv_check(td, PRIV_ROOT)))
604 * Don't allow unmounting the root file system.
606 if (mp->mnt_flag & MNT_ROOTFS) {
612 * Must be the root of the filesystem
614 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
622 error = dounmount(mp, uap->flags);
629 * Do the actual file system unmount.
632 dounmount_interlock(struct mount *mp)
634 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
636 mp->mnt_kern_flag |= MNTK_UNMOUNT;
641 dounmount(struct mount *mp, int flags)
643 struct namecache *ncp;
652 * Exclusive access for unmounting purposes
654 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
658 * Allow filesystems to detect that a forced unmount is in progress.
660 if (flags & MNT_FORCE)
661 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
662 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_NOWAIT);
663 error = lockmgr(&mp->mnt_lock, lflags);
665 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
666 if (mp->mnt_kern_flag & MNTK_MWAIT)
671 if (mp->mnt_flag & MNT_EXPUBLIC)
672 vfs_setpublicfs(NULL, NULL, NULL);
674 vfs_msync(mp, MNT_WAIT);
675 async_flag = mp->mnt_flag & MNT_ASYNC;
676 mp->mnt_flag &=~ MNT_ASYNC;
679 * If this filesystem isn't aliasing other filesystems,
680 * try to invalidate any remaining namecache entries and
681 * check the count afterwords.
683 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
684 cache_lock(&mp->mnt_ncmountpt);
685 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
686 cache_unlock(&mp->mnt_ncmountpt);
688 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
689 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
691 if ((flags & MNT_FORCE) == 0) {
693 mount_warning(mp, "Cannot unmount: "
699 mount_warning(mp, "Forced unmount: "
710 * nchandle records ref the mount structure. Expect a count of 1
711 * (our mount->mnt_ncmountpt).
713 if (mp->mnt_refs != 1) {
714 if ((flags & MNT_FORCE) == 0) {
715 mount_warning(mp, "Cannot unmount: "
716 "%d process references still "
717 "present", mp->mnt_refs);
720 mount_warning(mp, "Forced unmount: "
721 "%d process references still "
722 "present", mp->mnt_refs);
728 * Decomission our special mnt_syncer vnode. This also stops
729 * the vnlru code. If we are unable to unmount we recommission
733 if ((vp = mp->mnt_syncer) != NULL) {
734 mp->mnt_syncer = NULL;
737 if (((mp->mnt_flag & MNT_RDONLY) ||
738 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
739 (flags & MNT_FORCE)) {
740 error = VFS_UNMOUNT(mp, flags);
744 if (mp->mnt_syncer == NULL)
745 vfs_allocate_syncvnode(mp);
746 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
747 mp->mnt_flag |= async_flag;
748 lockmgr(&mp->mnt_lock, LK_RELEASE);
749 if (mp->mnt_kern_flag & MNTK_MWAIT)
754 * Clean up any journals still associated with the mount after
755 * filesystem activity has ceased.
757 journal_remove_all_journals(mp,
758 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
760 mountlist_remove(mp);
763 * Remove any installed vnode ops here so the individual VFSs don't
766 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
767 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
768 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
769 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
770 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
772 if (mp->mnt_ncmountpt.ncp != NULL) {
773 nch = mp->mnt_ncmountpt;
774 cache_zero(&mp->mnt_ncmountpt);
775 cache_clrmountpt(&nch);
778 if (mp->mnt_ncmounton.ncp != NULL) {
779 nch = mp->mnt_ncmounton;
780 cache_zero(&mp->mnt_ncmounton);
781 cache_clrmountpt(&nch);
785 mp->mnt_vfc->vfc_refcount--;
786 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
787 panic("unmount: dangling vnode");
788 lockmgr(&mp->mnt_lock, LK_RELEASE);
789 if (mp->mnt_kern_flag & MNTK_MWAIT)
798 mount_warning(struct mount *mp, const char *ctl, ...)
805 if (cache_fullpath(NULL, &mp->mnt_ncmounton, &ptr, &buf) == 0) {
806 kprintf("unmount(%s): ", ptr);
811 kprintf("unmount(%p", mp);
812 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
813 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
822 * Shim cache_fullpath() to handle the case where a process is chrooted into
823 * a subdirectory of a mount. In this case if the root mount matches the
824 * process root directory's mount we have to specify the process's root
825 * directory instead of the mount point, because the mount point might
826 * be above the root directory.
830 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
832 struct nchandle *nch;
834 if (p && p->p_fd->fd_nrdir.mount == mp)
835 nch = &p->p_fd->fd_nrdir;
837 nch = &mp->mnt_ncmountpt;
838 return(cache_fullpath(p, nch, rb, fb));
842 * Sync each mounted filesystem.
846 static int syncprt = 0;
847 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
850 static int sync_callback(struct mount *mp, void *data);
856 sys_sync(struct sync_args *uap)
859 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
862 * print out buffer pool stat information on each sync() call.
873 sync_callback(struct mount *mp, void *data __unused)
877 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
878 asyncflag = mp->mnt_flag & MNT_ASYNC;
879 mp->mnt_flag &= ~MNT_ASYNC;
880 vfs_msync(mp, MNT_NOWAIT);
881 VFS_SYNC(mp, MNT_NOWAIT);
882 mp->mnt_flag |= asyncflag;
887 /* XXX PRISON: could be per prison flag */
888 static int prison_quotas;
890 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
894 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
896 * Change filesystem quotas.
901 sys_quotactl(struct quotactl_args *uap)
903 struct nlookupdata nd;
912 if (td->td_ucred->cr_prison && !prison_quotas) {
917 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
919 error = nlookup(&nd);
921 mp = nd.nl_nch.mount;
922 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
923 uap->arg, nd.nl_cred);
932 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
933 * void *buf, int buflen)
935 * This function operates on a mount point and executes the specified
936 * operation using the specified control data, and possibly returns data.
938 * The actual number of bytes stored in the result buffer is returned, 0
939 * if none, otherwise an error is returned.
944 sys_mountctl(struct mountctl_args *uap)
946 struct thread *td = curthread;
947 struct proc *p = td->td_proc;
955 * Sanity and permissions checks. We must be root.
958 if (td->td_ucred->cr_prison != NULL)
960 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
961 (error = priv_check(td, PRIV_ROOT)) != 0)
965 * Argument length checks
967 if (uap->ctllen < 0 || uap->ctllen > 1024)
969 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
971 if (uap->path == NULL)
975 * Allocate the necessary buffers and copyin data
977 path = objcache_get(namei_oc, M_WAITOK);
978 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
983 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
984 error = copyin(uap->ctl, ctl, uap->ctllen);
989 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
992 * Validate the descriptor
995 fp = holdfp(p->p_fd, uap->fd, -1);
1005 * Execute the internal kernel function and clean up.
1008 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
1012 if (error == 0 && uap->sysmsg_result > 0)
1013 error = copyout(buf, uap->buf, uap->sysmsg_result);
1016 objcache_put(namei_oc, path);
1025 * Execute a mount control operation by resolving the path to a mount point
1026 * and calling vop_mountctl().
1028 * Use the mount point from the nch instead of the vnode so nullfs mounts
1029 * can properly spike the VOP.
1032 kern_mountctl(const char *path, int op, struct file *fp,
1033 const void *ctl, int ctllen,
1034 void *buf, int buflen, int *res)
1038 struct nlookupdata nd;
1043 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1045 error = nlookup(&nd);
1047 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1048 mp = nd.nl_nch.mount;
1055 * Must be the root of the filesystem
1057 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1061 error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen,
1068 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1070 struct thread *td = curthread;
1071 struct proc *p = td->td_proc;
1074 char *fullpath, *freepath;
1077 if ((error = nlookup(nd)) != 0)
1079 mp = nd->nl_nch.mount;
1081 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1084 error = mount_path(p, mp, &fullpath, &freepath);
1087 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1088 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1089 kfree(freepath, M_TEMP);
1091 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1092 bcopy(sp, buf, sizeof(*buf));
1093 /* Only root should have access to the fsid's. */
1094 if (priv_check(td, PRIV_ROOT))
1095 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1100 * statfs_args(char *path, struct statfs *buf)
1102 * Get filesystem statistics.
1107 sys_statfs(struct statfs_args *uap)
1109 struct nlookupdata nd;
1114 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1116 error = kern_statfs(&nd, &buf);
1119 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1128 kern_fstatfs(int fd, struct statfs *buf)
1130 struct thread *td = curthread;
1131 struct proc *p = td->td_proc;
1135 char *fullpath, *freepath;
1139 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1142 mp = ((struct vnode *)fp->f_data)->v_mount;
1147 if (fp->f_cred == NULL) {
1152 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1155 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1157 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1158 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1159 kfree(freepath, M_TEMP);
1161 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1162 bcopy(sp, buf, sizeof(*buf));
1164 /* Only root should have access to the fsid's. */
1165 if (priv_check(td, PRIV_ROOT))
1166 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1175 * fstatfs_args(int fd, struct statfs *buf)
1177 * Get filesystem statistics.
1182 sys_fstatfs(struct fstatfs_args *uap)
1187 error = kern_fstatfs(uap->fd, &buf);
1190 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1195 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1201 if ((error = nlookup(nd)) != 0)
1203 mp = nd->nl_nch.mount;
1204 sp = &mp->mnt_vstat;
1205 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1209 if (mp->mnt_flag & MNT_RDONLY)
1210 sp->f_flag |= ST_RDONLY;
1211 if (mp->mnt_flag & MNT_NOSUID)
1212 sp->f_flag |= ST_NOSUID;
1213 bcopy(sp, buf, sizeof(*buf));
1218 * statfs_args(char *path, struct statfs *buf)
1220 * Get filesystem statistics.
1225 sys_statvfs(struct statvfs_args *uap)
1227 struct nlookupdata nd;
1232 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1234 error = kern_statvfs(&nd, &buf);
1237 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1243 kern_fstatvfs(int fd, struct statvfs *buf)
1245 struct thread *td = curthread;
1246 struct proc *p = td->td_proc;
1253 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1255 mp = ((struct vnode *)fp->f_data)->v_mount;
1260 if (fp->f_cred == NULL) {
1264 sp = &mp->mnt_vstat;
1265 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1269 if (mp->mnt_flag & MNT_RDONLY)
1270 sp->f_flag |= ST_RDONLY;
1271 if (mp->mnt_flag & MNT_NOSUID)
1272 sp->f_flag |= ST_NOSUID;
1274 bcopy(sp, buf, sizeof(*buf));
1282 * fstatfs_args(int fd, struct statfs *buf)
1284 * Get filesystem statistics.
1289 sys_fstatvfs(struct fstatvfs_args *uap)
1295 error = kern_fstatvfs(uap->fd, &buf);
1299 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1304 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1306 * Get statistics on all filesystems.
1309 struct getfsstat_info {
1310 struct statfs *sfsp;
1318 static int getfsstat_callback(struct mount *, void *);
1324 sys_getfsstat(struct getfsstat_args *uap)
1326 struct thread *td = curthread;
1327 struct getfsstat_info info;
1329 bzero(&info, sizeof(info));
1331 info.maxcount = uap->bufsize / sizeof(struct statfs);
1332 info.sfsp = uap->buf;
1334 info.flags = uap->flags;
1338 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1340 if (info.sfsp && info.count > info.maxcount)
1341 uap->sysmsg_result = info.maxcount;
1343 uap->sysmsg_result = info.count;
1344 return (info.error);
1348 getfsstat_callback(struct mount *mp, void *data)
1350 struct getfsstat_info *info = data;
1356 if (info->sfsp && info->count < info->maxcount) {
1357 if (info->td->td_proc &&
1358 !chroot_visible_mnt(mp, info->td->td_proc)) {
1364 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1365 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1366 * overrides MNT_WAIT.
1368 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1369 (info->flags & MNT_WAIT)) &&
1370 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1373 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1375 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1377 info->error = error;
1380 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1381 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1382 kfree(freepath, M_TEMP);
1384 error = copyout(sp, info->sfsp, sizeof(*sp));
1386 info->error = error;
1396 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1397 long bufsize, int flags)
1399 * Get statistics on all filesystems.
1402 struct getvfsstat_info {
1403 struct statfs *sfsp;
1404 struct statvfs *vsfsp;
1412 static int getvfsstat_callback(struct mount *, void *);
1418 sys_getvfsstat(struct getvfsstat_args *uap)
1420 struct thread *td = curthread;
1421 struct getvfsstat_info info;
1423 bzero(&info, sizeof(info));
1425 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1426 info.sfsp = uap->buf;
1427 info.vsfsp = uap->vbuf;
1429 info.flags = uap->flags;
1433 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1434 if (info.vsfsp && info.count > info.maxcount)
1435 uap->sysmsg_result = info.maxcount;
1437 uap->sysmsg_result = info.count;
1439 return (info.error);
1443 getvfsstat_callback(struct mount *mp, void *data)
1445 struct getvfsstat_info *info = data;
1447 struct statvfs *vsp;
1452 if (info->vsfsp && info->count < info->maxcount) {
1453 if (info->td->td_proc &&
1454 !chroot_visible_mnt(mp, info->td->td_proc)) {
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->td->td_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->td->td_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->td->td_proc, 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(td->td_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_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(lp, &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(fdp, indx, lp->lwp_dupfd, flags, error);
1830 fdrop(fp); /* our ref */
1833 fsetfd(fdp, 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(fdp, 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(fdp, 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(td->td_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(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
1991 case S_IFDIR: /* special directories support for HAMMER */
1992 error = priv_check_cred(td->td_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_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;
2749 if ((error = nlookup(nd)) != 0)
2751 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2754 if (vp->v_type != VLNK) {
2757 aiov.iov_base = buf;
2758 aiov.iov_len = count;
2759 auio.uio_iov = &aiov;
2760 auio.uio_iovcnt = 1;
2761 auio.uio_offset = 0;
2762 auio.uio_rw = UIO_READ;
2763 auio.uio_segflg = UIO_USERSPACE;
2765 auio.uio_resid = count;
2766 error = VOP_READLINK(vp, &auio, td->td_ucred);
2769 *res = count - auio.uio_resid;
2774 * readlink_args(char *path, char *buf, int count)
2776 * Return target name of a symbolic link.
2781 sys_readlink(struct readlink_args *uap)
2783 struct nlookupdata nd;
2787 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2789 error = kern_readlink(&nd, uap->buf, uap->count,
2790 &uap->sysmsg_result);
2798 setfflags(struct vnode *vp, int flags)
2800 struct thread *td = curthread;
2805 * Prevent non-root users from setting flags on devices. When
2806 * a device is reused, users can retain ownership of the device
2807 * if they are allowed to set flags and programs assume that
2808 * chown can't fail when done as root.
2810 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2811 ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
2815 * note: vget is required for any operation that might mod the vnode
2816 * so VINACTIVE is properly cleared.
2818 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2820 vattr.va_flags = flags;
2821 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2828 * chflags(char *path, int flags)
2830 * Change flags of a file given a path name.
2835 sys_chflags(struct chflags_args *uap)
2837 struct nlookupdata nd;
2843 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2845 error = nlookup(&nd);
2847 error = ncp_writechk(&nd.nl_nch);
2849 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2852 error = setfflags(vp, uap->flags);
2860 * lchflags(char *path, int flags)
2862 * Change flags of a file given a path name, but don't follow symlinks.
2867 sys_lchflags(struct lchflags_args *uap)
2869 struct nlookupdata nd;
2875 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2877 error = nlookup(&nd);
2879 error = ncp_writechk(&nd.nl_nch);
2881 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2884 error = setfflags(vp, uap->flags);
2892 * fchflags_args(int fd, int flags)
2894 * Change flags of a file given a file descriptor.
2899 sys_fchflags(struct fchflags_args *uap)
2901 struct thread *td = curthread;
2902 struct proc *p = td->td_proc;
2906 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2909 if (fp->f_nchandle.ncp)
2910 error = ncp_writechk(&fp->f_nchandle);
2912 error = setfflags((struct vnode *) fp->f_data, uap->flags);
2919 setfmode(struct vnode *vp, int mode)
2921 struct thread *td = curthread;
2926 * note: vget is required for any operation that might mod the vnode
2927 * so VINACTIVE is properly cleared.
2929 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2931 vattr.va_mode = mode & ALLPERMS;
2932 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2939 kern_chmod(struct nlookupdata *nd, int mode)
2944 if ((error = nlookup(nd)) != 0)
2946 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2948 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2949 error = setfmode(vp, mode);
2955 * chmod_args(char *path, int mode)
2957 * Change mode of a file given path name.
2962 sys_chmod(struct chmod_args *uap)
2964 struct nlookupdata nd;
2968 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2970 error = kern_chmod(&nd, uap->mode);
2977 * lchmod_args(char *path, int mode)
2979 * Change mode of a file given path name (don't follow links.)
2984 sys_lchmod(struct lchmod_args *uap)
2986 struct nlookupdata nd;
2990 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2992 error = kern_chmod(&nd, uap->mode);
2999 * fchmod_args(int fd, int mode)
3001 * Change mode of a file given a file descriptor.
3006 sys_fchmod(struct fchmod_args *uap)
3008 struct thread *td = curthread;
3009 struct proc *p = td->td_proc;
3013 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3016 if (fp->f_nchandle.ncp)
3017 error = ncp_writechk(&fp->f_nchandle);
3019 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3026 * fchmodat_args(char *path, int mode)
3028 * Change mode of a file pointed to by fd/path.
3033 sys_fchmodat(struct fchmodat_args *uap)
3035 struct nlookupdata nd;
3040 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3042 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3045 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3046 UIO_USERSPACE, flags);
3048 error = kern_chmod(&nd, uap->mode);
3049 nlookup_done_at(&nd, fp);
3055 setfown(struct vnode *vp, uid_t uid, gid_t gid)
3057 struct thread *td = curthread;
3062 * note: vget is required for any operation that might mod the vnode
3063 * so VINACTIVE is properly cleared.
3065 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3069 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3076 kern_chown(struct nlookupdata *nd, int uid, int gid)
3081 if ((error = nlookup(nd)) != 0)
3083 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3085 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3086 error = setfown(vp, uid, gid);
3092 * chown(char *path, int uid, int gid)
3094 * Set ownership given a path name.
3099 sys_chown(struct chown_args *uap)
3101 struct nlookupdata nd;
3105 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3107 error = kern_chown(&nd, uap->uid, uap->gid);
3114 * lchown_args(char *path, int uid, int gid)
3116 * Set ownership given a path name, do not cross symlinks.
3121 sys_lchown(struct lchown_args *uap)
3123 struct nlookupdata nd;
3127 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3129 error = kern_chown(&nd, uap->uid, uap->gid);
3136 * fchown_args(int fd, int uid, int gid)
3138 * Set ownership given a file descriptor.
3143 sys_fchown(struct fchown_args *uap)
3145 struct thread *td = curthread;
3146 struct proc *p = td->td_proc;
3150 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3153 if (fp->f_nchandle.ncp)
3154 error = ncp_writechk(&fp->f_nchandle);
3156 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
3163 * fchownat(int fd, char *path, int uid, int gid, int flags)
3165 * Set ownership of file pointed to by fd/path.
3170 sys_fchownat(struct fchownat_args *uap)
3172 struct nlookupdata nd;
3177 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3179 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3182 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3183 UIO_USERSPACE, flags);
3185 error = kern_chown(&nd, uap->uid, uap->gid);
3186 nlookup_done_at(&nd, fp);
3193 getutimes(const struct timeval *tvp, struct timespec *tsp)
3195 struct timeval tv[2];
3199 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3202 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3203 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3209 setutimes(struct vnode *vp, struct vattr *vattr,
3210 const struct timespec *ts, int nullflag)
3212 struct thread *td = curthread;
3216 vattr->va_atime = ts[0];
3217 vattr->va_mtime = ts[1];
3219 vattr->va_vaflags |= VA_UTIMES_NULL;
3220 error = VOP_SETATTR(vp, vattr, td->td_ucred);
3226 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3228 struct timespec ts[2];
3233 if ((error = getutimes(tptr, ts)) != 0)
3237 * NOTE: utimes() succeeds for the owner even if the file
3238 * is not user-writable.
3240 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3242 if ((error = nlookup(nd)) != 0)
3244 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3246 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3250 * note: vget is required for any operation that might mod the vnode
3251 * so VINACTIVE is properly cleared.
3253 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3254 error = vget(vp, LK_EXCLUSIVE);
3256 error = setutimes(vp, &vattr, ts, (tptr == NULL));
3265 * utimes_args(char *path, struct timeval *tptr)
3267 * Set the access and modification times of a file.
3272 sys_utimes(struct utimes_args *uap)
3274 struct timeval tv[2];
3275 struct nlookupdata nd;
3279 error = copyin(uap->tptr, tv, sizeof(tv));
3284 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3286 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3293 * lutimes_args(char *path, struct timeval *tptr)
3295 * Set the access and modification times of a file.
3300 sys_lutimes(struct lutimes_args *uap)
3302 struct timeval tv[2];
3303 struct nlookupdata nd;
3307 error = copyin(uap->tptr, tv, sizeof(tv));
3312 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3314 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3321 * Set utimes on a file descriptor. The creds used to open the
3322 * file are used to determine whether the operation is allowed
3326 kern_futimes(int fd, struct timeval *tptr)
3328 struct thread *td = curthread;
3329 struct proc *p = td->td_proc;
3330 struct timespec ts[2];
3336 error = getutimes(tptr, ts);
3339 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3341 if (fp->f_nchandle.ncp)
3342 error = ncp_writechk(&fp->f_nchandle);
3345 error = vget(vp, LK_EXCLUSIVE);
3347 error = VOP_GETATTR(vp, &vattr);
3349 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3353 error = setutimes(vp, &vattr, ts,
3364 * futimes_args(int fd, struct timeval *tptr)
3366 * Set the access and modification times of a file.
3371 sys_futimes(struct futimes_args *uap)
3373 struct timeval tv[2];
3377 error = copyin(uap->tptr, tv, sizeof(tv));
3382 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3389 kern_truncate(struct nlookupdata *nd, off_t length)
3397 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3398 if ((error = nlookup(nd)) != 0)
3400 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3402 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3404 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
3408 if (vp->v_type == VDIR) {
3410 } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3412 vattr.va_size = length;
3413 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3420 * truncate(char *path, int pad, off_t length)
3422 * Truncate a file given its path name.
3427 sys_truncate(struct truncate_args *uap)
3429 struct nlookupdata nd;
3433 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3435 error = kern_truncate(&nd, uap->length);
3442 kern_ftruncate(int fd, off_t length)
3444 struct thread *td = curthread;
3445 struct proc *p = td->td_proc;
3453 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3455 if (fp->f_nchandle.ncp) {
3456 error = ncp_writechk(&fp->f_nchandle);
3460 if ((fp->f_flag & FWRITE) == 0) {
3464 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3468 vp = (struct vnode *)fp->f_data;
3469 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3470 if (vp->v_type == VDIR) {
3472 } else if ((error = vn_writechk(vp, NULL)) == 0) {
3474 vattr.va_size = length;
3475 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3484 * ftruncate_args(int fd, int pad, off_t length)
3486 * Truncate a file given a file descriptor.
3491 sys_ftruncate(struct ftruncate_args *uap)
3496 error = kern_ftruncate(uap->fd, uap->length);
3505 * Sync an open file.
3510 sys_fsync(struct fsync_args *uap)
3512 struct thread *td = curthread;
3513 struct proc *p = td->td_proc;
3519 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3522 vp = (struct vnode *)fp->f_data;
3523 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3524 if ((obj = vp->v_object) != NULL)
3525 vm_object_page_clean(obj, 0, 0, 0);
3526 error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
3527 if (error == 0 && vp->v_mount)
3528 error = buf_fsync(vp);
3537 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3539 struct nchandle fnchd;
3540 struct nchandle tnchd;
3541 struct namecache *ncp;
3548 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3549 if ((error = nlookup(fromnd)) != 0)
3551 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3553 fnchd.mount = fromnd->nl_nch.mount;
3557 * unlock the source nch so we can lookup the target nch without
3558 * deadlocking. The target may or may not exist so we do not check
3559 * for a target vp like kern_mkdir() and other creation functions do.
3561 * The source and target directories are ref'd and rechecked after
3562 * everything is relocked to determine if the source or target file
3565 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3566 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3567 cache_unlock(&fromnd->nl_nch);
3569 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3570 if ((error = nlookup(tond)) != 0) {
3574 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3578 tnchd.mount = tond->nl_nch.mount;
3582 * If the source and target are the same there is nothing to do
3584 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3591 * Mount points cannot be renamed or overwritten
3593 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3602 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
3603 * may have become invalid while it was unlocked, nc_vp and nc_mount
3606 if (cache_lock_nonblock(&fromnd->nl_nch) == 0) {
3607 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3608 } else if (fromnd->nl_nch.ncp > tond->nl_nch.ncp) {
3609 cache_lock(&fromnd->nl_nch);
3610 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3612 cache_unlock(&tond->nl_nch);
3613 cache_lock(&fromnd->nl_nch);
3614 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3615 cache_lock(&tond->nl_nch);
3616 cache_resolve(&tond->nl_nch, tond->nl_cred);
3618 fromnd->nl_flags |= NLC_NCPISLOCKED;
3621 * make sure the parent directories linkages are the same
3623 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3624 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3631 * Both the source and target must be within the same filesystem and
3632 * in the same filesystem as their parent directories within the
3633 * namecache topology.
3635 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3638 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3639 mp != tond->nl_nch.mount) {
3646 * Make sure the mount point is writable
3648 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3655 * If the target exists and either the source or target is a directory,
3656 * then both must be directories.
3658 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3661 if (tond->nl_nch.ncp->nc_vp) {
3662 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3664 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3665 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3667 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3673 * You cannot rename a source into itself or a subdirectory of itself.
3674 * We check this by travsersing the target directory upwards looking
3675 * for a match against the source.
3678 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3679 if (fromnd->nl_nch.ncp == ncp) {
3690 * Even though the namespaces are different, they may still represent
3691 * hardlinks to the same file. The filesystem might have a hard time
3692 * with this so we issue a NREMOVE of the source instead of a NRENAME
3693 * when we detect the situation.
3696 fdvp = fromnd->nl_dvp;
3697 tdvp = tond->nl_dvp;
3698 if (fdvp == NULL || tdvp == NULL) {
3700 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3701 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3704 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3705 fdvp, tdvp, tond->nl_cred);
3712 * rename_args(char *from, char *to)
3714 * Rename files. Source and destination must either both be directories,
3715 * or both not be directories. If target is a directory, it must be empty.
3720 sys_rename(struct rename_args *uap)
3722 struct nlookupdata fromnd, tond;
3726 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3728 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3730 error = kern_rename(&fromnd, &tond);
3731 nlookup_done(&tond);
3733 nlookup_done(&fromnd);
3739 kern_mkdir(struct nlookupdata *nd, int mode)
3741 struct thread *td = curthread;
3742 struct proc *p = td->td_proc;
3748 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3749 if ((error = nlookup(nd)) != 0)
3752 if (nd->nl_nch.ncp->nc_vp)
3754 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3757 vattr.va_type = VDIR;
3758 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3761 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
3768 * mkdir_args(char *path, int mode)
3770 * Make a directory file.
3775 sys_mkdir(struct mkdir_args *uap)
3777 struct nlookupdata nd;
3781 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3783 error = kern_mkdir(&nd, uap->mode);
3790 kern_rmdir(struct nlookupdata *nd)
3795 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
3796 if ((error = nlookup(nd)) != 0)
3800 * Do not allow directories representing mount points to be
3801 * deleted, even if empty. Check write perms on mount point
3802 * in case the vnode is aliased (aka nullfs).
3804 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
3806 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3808 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
3813 * rmdir_args(char *path)
3815 * Remove a directory file.
3820 sys_rmdir(struct rmdir_args *uap)
3822 struct nlookupdata nd;
3826 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3828 error = kern_rmdir(&nd);
3835 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
3836 enum uio_seg direction)
3838 struct thread *td = curthread;
3839 struct proc *p = td->td_proc;
3847 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3849 if ((fp->f_flag & FREAD) == 0) {
3853 vp = (struct vnode *)fp->f_data;
3855 if (vp->v_type != VDIR) {
3859 aiov.iov_base = buf;
3860 aiov.iov_len = count;
3861 auio.uio_iov = &aiov;
3862 auio.uio_iovcnt = 1;
3863 auio.uio_rw = UIO_READ;
3864 auio.uio_segflg = direction;
3866 auio.uio_resid = count;
3867 loff = auio.uio_offset = fp->f_offset;
3868 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3869 fp->f_offset = auio.uio_offset;
3872 if (count == auio.uio_resid) {
3873 if (union_dircheckp) {
3874 error = union_dircheckp(td, &vp, fp);
3881 if ((vp->v_flag & VROOT) &&
3882 (vp->v_mount->mnt_flag & MNT_UNION)) {
3883 struct vnode *tvp = vp;
3884 vp = vp->v_mount->mnt_vnodecovered;
3895 * WARNING! *basep may not be wide enough to accomodate the
3896 * seek offset. XXX should we hack this to return the upper 32 bits
3897 * for offsets greater then 4G?
3900 *basep = (long)loff;
3902 *res = count - auio.uio_resid;
3909 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
3911 * Read a block of directory entries in a file system independent format.
3916 sys_getdirentries(struct getdirentries_args *uap)
3922 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
3923 &uap->sysmsg_result, UIO_USERSPACE);
3926 if (error == 0 && uap->basep)
3927 error = copyout(&base, uap->basep, sizeof(*uap->basep));
3932 * getdents_args(int fd, char *buf, size_t count)
3937 sys_getdents(struct getdents_args *uap)
3942 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
3943 &uap->sysmsg_result, UIO_USERSPACE);
3950 * Set the mode mask for creation of filesystem nodes.
3952 * umask(int newmask)
3957 sys_umask(struct umask_args *uap)
3959 struct thread *td = curthread;
3960 struct proc *p = td->td_proc;
3961 struct filedesc *fdp;
3964 uap->sysmsg_result = fdp->fd_cmask;
3965 fdp->fd_cmask = uap->newmask & ALLPERMS;
3970 * revoke(char *path)
3972 * Void all references to file by ripping underlying filesystem
3978 sys_revoke(struct revoke_args *uap)
3980 struct nlookupdata nd;
3988 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3990 error = nlookup(&nd);
3992 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3993 cred = crhold(nd.nl_cred);
3997 error = VOP_GETATTR(vp, &vattr);
3998 if (error == 0 && cred->cr_uid != vattr.va_uid)
3999 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4000 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4002 error = vrevoke(vp, cred);
4003 } else if (error == 0) {
4004 error = vrevoke(vp, cred);
4015 * getfh_args(char *fname, fhandle_t *fhp)
4017 * Get (NFS) file handle
4019 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4020 * mount. This allows nullfs mounts to be explicitly exported.
4022 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4024 * nullfs mounts of subdirectories are not safe. That is, it will
4025 * work, but you do not really have protection against access to
4026 * the related parent directories.
4031 sys_getfh(struct getfh_args *uap)
4033 struct thread *td = curthread;
4034 struct nlookupdata nd;
4041 * Must be super user
4043 if ((error = priv_check(td, PRIV_ROOT)) != 0)
4048 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4050 error = nlookup(&nd);
4052 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4053 mp = nd.nl_nch.mount;
4056 bzero(&fh, sizeof(fh));
4057 fh.fh_fsid = mp->mnt_stat.f_fsid;
4058 error = VFS_VPTOFH(vp, &fh.fh_fid);
4061 error = copyout(&fh, uap->fhp, sizeof(fh));
4068 * fhopen_args(const struct fhandle *u_fhp, int flags)
4070 * syscall for the rpc.lockd to use to translate a NFS file handle into
4071 * an open descriptor.
4073 * warning: do not remove the priv_check() call or this becomes one giant
4079 sys_fhopen(struct fhopen_args *uap)
4081 struct thread *td = curthread;
4082 struct filedesc *fdp = td->td_proc->p_fd;
4087 struct vattr *vap = &vat;
4089 int fmode, mode, error, type;
4095 * Must be super user
4097 error = priv_check(td, PRIV_ROOT);
4101 fmode = FFLAGS(uap->flags);
4104 * Why not allow a non-read/write open for our lockd?
4106 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4108 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4113 * Find the mount point
4116 mp = vfs_getvfs(&fhp.fh_fsid);
4121 /* now give me my vnode, it gets returned to me locked */
4122 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4126 * from now on we have to make sure not
4127 * to forget about the vnode
4128 * any error that causes an abort must vput(vp)
4129 * just set error = err and 'goto bad;'.
4135 if (vp->v_type == VLNK) {
4139 if (vp->v_type == VSOCK) {
4144 if (fmode & (FWRITE | O_TRUNC)) {
4145 if (vp->v_type == VDIR) {
4149 error = vn_writechk(vp, NULL);
4157 error = VOP_ACCESS(vp, mode, td->td_ucred);
4161 if (fmode & O_TRUNC) {
4162 vn_unlock(vp); /* XXX */
4163 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4166 error = VOP_SETATTR(vp, vap, td->td_ucred);
4172 * VOP_OPEN needs the file pointer so it can potentially override
4175 * WARNING! no f_nchandle will be associated when fhopen()ing a
4178 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4182 error = VOP_OPEN(vp, fmode, td->td_ucred, fp);
4185 * setting f_ops this way prevents VOP_CLOSE from being
4186 * called or fdrop() releasing the vp from v_data. Since
4187 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4189 fp->f_ops = &badfileops;
4195 * The fp is given its own reference, we still have our ref and lock.
4197 * Assert that all regular files must be created with a VM object.
4199 if (vp->v_type == VREG && vp->v_object == NULL) {
4200 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
4205 * The open was successful. Handle any locking requirements.
4207 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4208 lf.l_whence = SEEK_SET;
4211 if (fmode & O_EXLOCK)
4212 lf.l_type = F_WRLCK;
4214 lf.l_type = F_RDLCK;
4215 if (fmode & FNONBLOCK)
4220 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
4222 * release our private reference.
4224 fsetfd(fdp, NULL, indx);
4229 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4230 fp->f_flag |= FHASLOCK;
4234 * Clean up. Associate the file pointer with the previously
4235 * reserved descriptor and return it.
4239 fsetfd(fdp, fp, indx);
4241 uap->sysmsg_result = indx;
4245 fsetfd(fdp, NULL, indx);
4255 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4260 sys_fhstat(struct fhstat_args *uap)
4262 struct thread *td = curthread;
4270 * Must be super user
4272 error = priv_check(td, PRIV_ROOT);
4276 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4281 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4284 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4285 error = vn_stat(vp, &sb, td->td_ucred);
4291 error = copyout(&sb, uap->sb, sizeof(sb));
4296 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4301 sys_fhstatfs(struct fhstatfs_args *uap)
4303 struct thread *td = curthread;
4304 struct proc *p = td->td_proc;
4309 char *fullpath, *freepath;
4314 * Must be super user
4316 if ((error = priv_check(td, PRIV_ROOT)))
4319 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4324 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4328 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4333 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4338 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4341 error = mount_path(p, mp, &fullpath, &freepath);
4344 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4345 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4346 kfree(freepath, M_TEMP);
4348 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4349 if (priv_check(td, PRIV_ROOT)) {
4350 bcopy(sp, &sb, sizeof(sb));
4351 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4354 error = copyout(sp, uap->buf, sizeof(*sp));
4361 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4366 sys_fhstatvfs(struct fhstatvfs_args *uap)
4368 struct thread *td = curthread;
4369 struct proc *p = td->td_proc;
4377 * Must be super user
4379 if ((error = priv_check(td, PRIV_ROOT)))
4382 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4387 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4391 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4396 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4399 sp = &mp->mnt_vstat;
4401 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
4405 if (mp->mnt_flag & MNT_RDONLY)
4406 sp->f_flag |= ST_RDONLY;
4407 if (mp->mnt_flag & MNT_NOSUID)
4408 sp->f_flag |= ST_NOSUID;
4409 error = copyout(sp, uap->buf, sizeof(*sp));
4417 * Syscall to push extended attribute configuration information into the
4418 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4419 * a command (int cmd), and attribute name and misc data. For now, the
4420 * attribute name is left in userspace for consumption by the VFS_op.
4421 * It will probably be changed to be copied into sysspace by the
4422 * syscall in the future, once issues with various consumers of the
4423 * attribute code have raised their hands.
4425 * Currently this is used only by UFS Extended Attributes.
4430 sys_extattrctl(struct extattrctl_args *uap)
4432 struct nlookupdata nd;
4439 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4441 error = nlookup(&nd);
4443 mp = nd.nl_nch.mount;
4444 error = VFS_EXTATTRCTL(mp, uap->cmd,
4445 uap->attrname, uap->arg,
4455 * Syscall to set a named extended attribute on a file or directory.
4456 * Accepts attribute name, and a uio structure pointing to the data to set.
4457 * The uio is consumed in the style of writev(). The real work happens
4458 * in VOP_SETEXTATTR().
4463 sys_extattr_set_file(struct extattr_set_file_args *uap)
4465 char attrname[EXTATTR_MAXNAMELEN];
4466 struct iovec aiov[UIO_SMALLIOV];
4467 struct iovec *needfree;
4468 struct nlookupdata nd;
4477 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4483 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4485 error = nlookup(&nd);
4487 error = ncp_writechk(&nd.nl_nch);
4489 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4497 iovlen = uap->iovcnt * sizeof(struct iovec);
4498 if (uap->iovcnt > UIO_SMALLIOV) {
4499 if (uap->iovcnt > UIO_MAXIOV) {
4503 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
4509 auio.uio_iovcnt = uap->iovcnt;
4510 auio.uio_rw = UIO_WRITE;
4511 auio.uio_segflg = UIO_USERSPACE;
4512 auio.uio_td = nd.nl_td;
4513 auio.uio_offset = 0;
4514 if ((error = copyin(uap->iovp, iov, iovlen)))
4517 for (i = 0; i < uap->iovcnt; i++) {
4518 if (iov->iov_len > LONG_MAX - auio.uio_resid) {
4522 auio.uio_resid += iov->iov_len;
4525 cnt = auio.uio_resid;
4526 error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred);
4527 cnt -= auio.uio_resid;
4528 uap->sysmsg_result = cnt;
4534 FREE(needfree, M_IOV);
4539 * Syscall to get a named extended attribute on a file or directory.
4540 * Accepts attribute name, and a uio structure pointing to a buffer for the
4541 * data. The uio is consumed in the style of readv(). The real work
4542 * happens in VOP_GETEXTATTR();
4547 sys_extattr_get_file(struct extattr_get_file_args *uap)
4549 char attrname[EXTATTR_MAXNAMELEN];
4550 struct iovec aiov[UIO_SMALLIOV];
4551 struct iovec *needfree;
4552 struct nlookupdata nd;
4561 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4567 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4569 error = nlookup(&nd);
4571 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4578 iovlen = uap->iovcnt * sizeof (struct iovec);
4580 if (uap->iovcnt > UIO_SMALLIOV) {
4581 if (uap->iovcnt > UIO_MAXIOV) {
4585 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
4591 auio.uio_iovcnt = uap->iovcnt;
4592 auio.uio_rw = UIO_READ;
4593 auio.uio_segflg = UIO_USERSPACE;
4594 auio.uio_td = nd.nl_td;
4595 auio.uio_offset = 0;
4596 if ((error = copyin(uap->iovp, iov, iovlen)))
4599 for (i = 0; i < uap->iovcnt; i++) {
4600 if (iov->iov_len > LONG_MAX - auio.uio_resid) {
4604 auio.uio_resid += iov->iov_len;
4607 cnt = auio.uio_resid;
4608 error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred);
4609 cnt -= auio.uio_resid;
4610 uap->sysmsg_result = cnt;
4616 FREE(needfree, M_IOV);
4621 * Syscall to delete a named extended attribute from a file or directory.
4622 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4627 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4629 char attrname[EXTATTR_MAXNAMELEN];
4630 struct nlookupdata nd;
4634 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4639 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4641 error = nlookup(&nd);
4643 error = ncp_writechk(&nd.nl_nch);
4645 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4647 error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred);
4657 * Determine if the mount is visible to the process.
4660 chroot_visible_mnt(struct mount *mp, struct proc *p)
4662 struct nchandle nch;
4665 * Traverse from the mount point upwards. If we hit the process
4666 * root then the mount point is visible to the process.
4668 nch = mp->mnt_ncmountpt;
4670 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4671 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4674 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4675 nch = nch.mount->mnt_ncmounton;
4677 nch.ncp = nch.ncp->nc_parent;
4682 * If the mount point is not visible to the process, but the
4683 * process root is in a subdirectory of the mount, return
4686 if (p->p_fd->fd_nrdir.mount == mp)