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.
111 * 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;
122 struct vfsconf *vfsp;
123 int error, flag = 0, flag2 = 0;
126 struct nlookupdata nd;
127 char fstypename[MFSNAMELEN];
128 struct ucred *cred = p->p_ucred;
133 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
136 * Do not allow NFS export by non-root users.
138 if (uap->flags & MNT_EXPORTED) {
139 error = priv_check(td, PRIV_ROOT);
144 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
146 if (priv_check(td, PRIV_ROOT))
147 uap->flags |= MNT_NOSUID | MNT_NODEV;
150 * Lookup the requested path and extract the nch and vnode.
152 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
154 if ((error = nlookup(&nd)) == 0) {
155 if (nd.nl_nch.ncp->nc_vp == NULL)
165 * Extract the locked+refd ncp and cleanup the nd structure
168 cache_zero(&nd.nl_nch);
171 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) && cache_findmount(&nch))
178 * now we have the locked ref'd nch and unreferenced vnode.
181 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
188 * Now we have an unlocked ref'd nch and a locked ref'd vp
190 if (uap->flags & MNT_UPDATE) {
191 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
198 flag2 = mp->mnt_kern_flag;
200 * We only allow the filesystem to be reloaded if it
201 * is currently mounted read-only.
203 if ((uap->flags & MNT_RELOAD) &&
204 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
207 return (EOPNOTSUPP); /* Needs translation */
210 * Only root, or the user that did the original mount is
211 * permitted to update it.
213 if (mp->mnt_stat.f_owner != cred->cr_uid &&
214 (error = priv_check(td, PRIV_ROOT))) {
219 if (vfs_busy(mp, LK_NOWAIT)) {
224 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
230 vp->v_flag |= VMOUNT;
232 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
237 * If the user is not root, ensure that they own the directory
238 * onto which we are attempting to mount.
240 if ((error = VOP_GETATTR(vp, &va)) ||
241 (va.va_uid != cred->cr_uid && (error = priv_check(td, PRIV_ROOT)))) {
246 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
251 if (vp->v_type != VDIR) {
256 if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
261 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
266 vfsp = vfsconf_find_by_name(fstypename);
270 /* Only load modules for root (very important!) */
271 if ((error = priv_check(td, PRIV_ROOT)) != 0) {
276 error = linker_load_file(fstypename, &lf, 0);
277 if (error || lf == NULL) {
285 /* lookup again, see if the VFS was loaded */
286 vfsp = vfsconf_find_by_name(fstypename);
289 linker_file_unload(lf);
295 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
300 vp->v_flag |= VMOUNT;
303 * Allocate and initialize the filesystem.
305 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
306 TAILQ_INIT(&mp->mnt_nvnodelist);
307 TAILQ_INIT(&mp->mnt_reservedvnlist);
308 TAILQ_INIT(&mp->mnt_jlist);
309 mp->mnt_nvnodelistsize = 0;
310 lockinit(&mp->mnt_lock, "vfslock", 0, 0);
311 vfs_busy(mp, LK_NOWAIT);
312 mp->mnt_op = vfsp->vfc_vfsops;
314 vfsp->vfc_refcount++;
315 mp->mnt_stat.f_type = vfsp->vfc_typenum;
316 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
317 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
318 mp->mnt_stat.f_owner = cred->cr_uid;
319 mp->mnt_iosize_max = DFLTPHYS;
323 * Set the mount level flags.
325 if (uap->flags & MNT_RDONLY)
326 mp->mnt_flag |= MNT_RDONLY;
327 else if (mp->mnt_flag & MNT_RDONLY)
328 mp->mnt_kern_flag |= MNTK_WANTRDWR;
329 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
330 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
331 MNT_NOSYMFOLLOW | MNT_IGNORE |
332 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
333 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
334 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
335 MNT_NOSYMFOLLOW | MNT_IGNORE |
336 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
338 * Mount the filesystem.
339 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
342 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
343 if (mp->mnt_flag & MNT_UPDATE) {
344 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
345 mp->mnt_flag &= ~MNT_RDONLY;
346 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
347 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
350 mp->mnt_kern_flag = flag2;
353 vp->v_flag &= ~VMOUNT;
358 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
360 * Put the new filesystem on the mount list after root. The mount
361 * point gets its own mnt_ncmountpt (unless the VFS already set one
362 * up) which represents the root of the mount. The lookup code
363 * detects the mount point going forward and checks the root of
364 * the mount going backwards.
366 * It is not necessary to invalidate or purge the vnode underneath
367 * because elements under the mount will be given their own glue
371 if (mp->mnt_ncmountpt.ncp == NULL) {
373 * allocate, then unlock, but leave the ref intact
375 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
376 cache_unlock(&mp->mnt_ncmountpt);
378 mp->mnt_ncmounton = nch; /* inherits ref */
379 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
381 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */
382 vp->v_flag &= ~VMOUNT;
383 mountlist_insert(mp, MNTINS_LAST);
385 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
386 error = vfs_allocate_syncvnode(mp);
388 error = VFS_START(mp, 0);
391 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
392 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
393 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
394 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
395 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
396 vp->v_flag &= ~VMOUNT;
397 mp->mnt_vfc->vfc_refcount--;
407 * Scan all active processes to see if any of them have a current
408 * or root directory onto which the new filesystem has just been
409 * mounted. If so, replace them with the new mount point.
411 * The passed ncp is ref'd and locked (from the mount code) and
412 * must be associated with the vnode representing the root of the
415 struct checkdirs_info {
416 struct nchandle old_nch;
417 struct nchandle new_nch;
418 struct vnode *old_vp;
419 struct vnode *new_vp;
422 static int checkdirs_callback(struct proc *p, void *data);
425 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
427 struct checkdirs_info info;
433 * If the old mount point's vnode has a usecount of 1, it is not
434 * being held as a descriptor anywhere.
436 olddp = old_nch->ncp->nc_vp;
437 if (olddp == NULL || olddp->v_sysref.refcnt == 1)
441 * Force the root vnode of the new mount point to be resolved
442 * so we can update any matching processes.
445 if (VFS_ROOT(mp, &newdp))
446 panic("mount: lost mount");
447 cache_setunresolved(new_nch);
448 cache_setvp(new_nch, newdp);
451 * Special handling of the root node
453 if (rootvnode == olddp) {
455 vfs_cache_setroot(newdp, cache_hold(new_nch));
459 * Pass newdp separately so the callback does not have to access
460 * it via new_nch->ncp->nc_vp.
462 info.old_nch = *old_nch;
463 info.new_nch = *new_nch;
465 allproc_scan(checkdirs_callback, &info);
470 * NOTE: callback is not MP safe because the scanned process's filedesc
471 * structure can be ripped out from under us, amoung other things.
474 checkdirs_callback(struct proc *p, void *data)
476 struct checkdirs_info *info = data;
477 struct filedesc *fdp;
478 struct nchandle ncdrop1;
479 struct nchandle ncdrop2;
480 struct vnode *vprele1;
481 struct vnode *vprele2;
483 if ((fdp = p->p_fd) != NULL) {
484 cache_zero(&ncdrop1);
485 cache_zero(&ncdrop2);
490 * MPUNSAFE - XXX fdp can be pulled out from under a
493 * A shared filedesc is ok, we don't have to copy it
494 * because we are making this change globally.
496 spin_lock_wr(&fdp->fd_spin);
497 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
498 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
499 vprele1 = fdp->fd_cdir;
501 fdp->fd_cdir = info->new_vp;
502 ncdrop1 = fdp->fd_ncdir;
503 cache_copy(&info->new_nch, &fdp->fd_ncdir);
505 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
506 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
507 vprele2 = fdp->fd_rdir;
509 fdp->fd_rdir = info->new_vp;
510 ncdrop2 = fdp->fd_nrdir;
511 cache_copy(&info->new_nch, &fdp->fd_nrdir);
513 spin_unlock_wr(&fdp->fd_spin);
515 cache_drop(&ncdrop1);
517 cache_drop(&ncdrop2);
527 * Unmount a file system.
529 * Note: unmount takes a path to the vnode mounted on as argument,
530 * not special file (as before).
533 * umount_args(char *path, int flags)
537 sys_unmount(struct unmount_args *uap)
539 struct thread *td = curthread;
540 struct proc *p = td->td_proc;
541 struct mount *mp = NULL;
543 struct nlookupdata nd;
546 if (p->p_ucred->cr_prison != NULL)
548 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
551 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
553 error = nlookup(&nd);
557 mp = nd.nl_nch.mount;
560 * Only root, or the user that did the original mount is
561 * permitted to unmount this filesystem.
563 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
564 (error = priv_check(td, PRIV_ROOT)))
568 * Don't allow unmounting the root file system.
570 if (mp->mnt_flag & MNT_ROOTFS) {
576 * Must be the root of the filesystem
578 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
587 return (dounmount(mp, uap->flags));
591 * Do the actual file system unmount.
594 dounmount_interlock(struct mount *mp)
596 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
598 mp->mnt_kern_flag |= MNTK_UNMOUNT;
603 dounmount(struct mount *mp, int flags)
605 struct namecache *ncp;
614 * Exclusive access for unmounting purposes
616 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
620 * Allow filesystems to detect that a forced unmount is in progress.
622 if (flags & MNT_FORCE)
623 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
624 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_NOWAIT);
625 error = lockmgr(&mp->mnt_lock, lflags);
627 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
628 if (mp->mnt_kern_flag & MNTK_MWAIT)
633 if (mp->mnt_flag & MNT_EXPUBLIC)
634 vfs_setpublicfs(NULL, NULL, NULL);
636 vfs_msync(mp, MNT_WAIT);
637 async_flag = mp->mnt_flag & MNT_ASYNC;
638 mp->mnt_flag &=~ MNT_ASYNC;
641 * If this filesystem isn't aliasing other filesystems,
642 * try to invalidate any remaining namecache entries and
643 * check the count afterwords.
645 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
646 cache_lock(&mp->mnt_ncmountpt);
647 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
648 cache_unlock(&mp->mnt_ncmountpt);
650 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
651 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
653 if ((flags & MNT_FORCE) == 0) {
655 mount_warning(mp, "Cannot unmount: "
661 mount_warning(mp, "Forced unmount: "
672 * nchandle records ref the mount structure. Expect a count of 1
673 * (our mount->mnt_ncmountpt).
675 if (mp->mnt_refs != 1) {
676 if ((flags & MNT_FORCE) == 0) {
677 mount_warning(mp, "Cannot unmount: "
678 "%d process references still "
679 "present", mp->mnt_refs);
682 mount_warning(mp, "Forced unmount: "
683 "%d process references still "
684 "present", mp->mnt_refs);
690 * Decomission our special mnt_syncer vnode. This also stops
691 * the vnlru code. If we are unable to unmount we recommission
695 if ((vp = mp->mnt_syncer) != NULL) {
696 mp->mnt_syncer = NULL;
699 if (((mp->mnt_flag & MNT_RDONLY) ||
700 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
701 (flags & MNT_FORCE)) {
702 error = VFS_UNMOUNT(mp, flags);
706 if (mp->mnt_syncer == NULL)
707 vfs_allocate_syncvnode(mp);
708 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
709 mp->mnt_flag |= async_flag;
710 lockmgr(&mp->mnt_lock, LK_RELEASE);
711 if (mp->mnt_kern_flag & MNTK_MWAIT)
716 * Clean up any journals still associated with the mount after
717 * filesystem activity has ceased.
719 journal_remove_all_journals(mp,
720 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
722 mountlist_remove(mp);
725 * Remove any installed vnode ops here so the individual VFSs don't
728 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
729 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
730 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
731 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
732 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
734 if (mp->mnt_ncmountpt.ncp != NULL) {
735 nch = mp->mnt_ncmountpt;
736 cache_zero(&mp->mnt_ncmountpt);
737 cache_clrmountpt(&nch);
740 if (mp->mnt_ncmounton.ncp != NULL) {
741 nch = mp->mnt_ncmounton;
742 cache_zero(&mp->mnt_ncmounton);
743 cache_clrmountpt(&nch);
747 mp->mnt_vfc->vfc_refcount--;
748 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
749 panic("unmount: dangling vnode");
750 lockmgr(&mp->mnt_lock, LK_RELEASE);
751 if (mp->mnt_kern_flag & MNTK_MWAIT)
760 mount_warning(struct mount *mp, const char *ctl, ...)
767 if (cache_fullpath(NULL, &mp->mnt_ncmounton, &ptr, &buf) == 0) {
768 kprintf("unmount(%s): ", ptr);
773 kprintf("unmount(%p", mp);
774 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
775 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
784 * Shim cache_fullpath() to handle the case where a process is chrooted into
785 * a subdirectory of a mount. In this case if the root mount matches the
786 * process root directory's mount we have to specify the process's root
787 * directory instead of the mount point, because the mount point might
788 * be above the root directory.
792 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
794 struct nchandle *nch;
796 if (p && p->p_fd->fd_nrdir.mount == mp)
797 nch = &p->p_fd->fd_nrdir;
799 nch = &mp->mnt_ncmountpt;
800 return(cache_fullpath(p, nch, rb, fb));
804 * Sync each mounted filesystem.
808 static int syncprt = 0;
809 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
812 static int sync_callback(struct mount *mp, void *data);
816 sys_sync(struct sync_args *uap)
818 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
821 * print out buffer pool stat information on each sync() call.
831 sync_callback(struct mount *mp, void *data __unused)
835 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
836 asyncflag = mp->mnt_flag & MNT_ASYNC;
837 mp->mnt_flag &= ~MNT_ASYNC;
838 vfs_msync(mp, MNT_NOWAIT);
839 VFS_SYNC(mp, MNT_NOWAIT);
840 mp->mnt_flag |= asyncflag;
845 /* XXX PRISON: could be per prison flag */
846 static int prison_quotas;
848 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
852 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
854 * Change filesystem quotas.
858 sys_quotactl(struct quotactl_args *uap)
860 struct nlookupdata nd;
868 if (p->p_ucred->cr_prison && !prison_quotas)
871 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
873 error = nlookup(&nd);
875 mp = nd.nl_nch.mount;
876 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
877 uap->arg, nd.nl_cred);
884 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
885 * void *buf, int buflen)
887 * This function operates on a mount point and executes the specified
888 * operation using the specified control data, and possibly returns data.
890 * The actual number of bytes stored in the result buffer is returned, 0
891 * if none, otherwise an error is returned.
895 sys_mountctl(struct mountctl_args *uap)
897 struct thread *td = curthread;
898 struct proc *p = td->td_proc;
906 * Sanity and permissions checks. We must be root.
909 if (p->p_ucred->cr_prison != NULL)
911 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
912 (error = priv_check(td, PRIV_ROOT)) != 0)
916 * Argument length checks
918 if (uap->ctllen < 0 || uap->ctllen > 1024)
920 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
922 if (uap->path == NULL)
926 * Allocate the necessary buffers and copyin data
928 path = objcache_get(namei_oc, M_WAITOK);
929 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
934 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
935 error = copyin(uap->ctl, ctl, uap->ctllen);
940 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
943 * Validate the descriptor
946 fp = holdfp(p->p_fd, uap->fd, -1);
956 * Execute the internal kernel function and clean up.
958 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
961 if (error == 0 && uap->sysmsg_result > 0)
962 error = copyout(buf, uap->buf, uap->sysmsg_result);
965 objcache_put(namei_oc, path);
974 * Execute a mount control operation by resolving the path to a mount point
975 * and calling vop_mountctl().
977 * Use the mount point from the nch instead of the vnode so nullfs mounts
978 * can properly spike the VOP.
981 kern_mountctl(const char *path, int op, struct file *fp,
982 const void *ctl, int ctllen,
983 void *buf, int buflen, int *res)
987 struct nlookupdata nd;
992 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
994 error = nlookup(&nd);
996 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
997 mp = nd.nl_nch.mount;
1003 * Must be the root of the filesystem
1005 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1009 error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen,
1016 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1018 struct thread *td = curthread;
1019 struct proc *p = td->td_proc;
1022 char *fullpath, *freepath;
1025 if ((error = nlookup(nd)) != 0)
1027 mp = nd->nl_nch.mount;
1029 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1032 error = mount_path(p, mp, &fullpath, &freepath);
1035 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1036 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1037 kfree(freepath, M_TEMP);
1039 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1040 bcopy(sp, buf, sizeof(*buf));
1041 /* Only root should have access to the fsid's. */
1042 if (priv_check(td, PRIV_ROOT))
1043 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1048 * statfs_args(char *path, struct statfs *buf)
1050 * Get filesystem statistics.
1053 sys_statfs(struct statfs_args *uap)
1055 struct nlookupdata nd;
1059 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1061 error = kern_statfs(&nd, &buf);
1064 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1069 kern_fstatfs(int fd, struct statfs *buf)
1071 struct thread *td = curthread;
1072 struct proc *p = td->td_proc;
1076 char *fullpath, *freepath;
1080 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1082 mp = ((struct vnode *)fp->f_data)->v_mount;
1087 if (fp->f_cred == NULL) {
1092 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1095 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1097 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1098 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1099 kfree(freepath, M_TEMP);
1101 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1102 bcopy(sp, buf, sizeof(*buf));
1104 /* Only root should have access to the fsid's. */
1105 if (priv_check(td, PRIV_ROOT))
1106 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1114 * fstatfs_args(int fd, struct statfs *buf)
1116 * Get filesystem statistics.
1119 sys_fstatfs(struct fstatfs_args *uap)
1124 error = kern_fstatfs(uap->fd, &buf);
1127 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1132 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1138 if ((error = nlookup(nd)) != 0)
1140 mp = nd->nl_nch.mount;
1141 sp = &mp->mnt_vstat;
1142 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1146 if (mp->mnt_flag & MNT_RDONLY)
1147 sp->f_flag |= ST_RDONLY;
1148 if (mp->mnt_flag & MNT_NOSUID)
1149 sp->f_flag |= ST_NOSUID;
1150 bcopy(sp, buf, sizeof(*buf));
1155 * statfs_args(char *path, struct statfs *buf)
1157 * Get filesystem statistics.
1160 sys_statvfs(struct statvfs_args *uap)
1162 struct nlookupdata nd;
1166 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1168 error = kern_statvfs(&nd, &buf);
1171 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1176 kern_fstatvfs(int fd, struct statvfs *buf)
1178 struct thread *td = curthread;
1179 struct proc *p = td->td_proc;
1186 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1188 mp = ((struct vnode *)fp->f_data)->v_mount;
1193 if (fp->f_cred == NULL) {
1197 sp = &mp->mnt_vstat;
1198 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1202 if (mp->mnt_flag & MNT_RDONLY)
1203 sp->f_flag |= ST_RDONLY;
1204 if (mp->mnt_flag & MNT_NOSUID)
1205 sp->f_flag |= ST_NOSUID;
1207 bcopy(sp, buf, sizeof(*buf));
1215 * fstatfs_args(int fd, struct statfs *buf)
1217 * Get filesystem statistics.
1220 sys_fstatvfs(struct fstatvfs_args *uap)
1225 error = kern_fstatvfs(uap->fd, &buf);
1228 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1233 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1235 * Get statistics on all filesystems.
1238 struct getfsstat_info {
1239 struct statfs *sfsp;
1247 static int getfsstat_callback(struct mount *, void *);
1251 sys_getfsstat(struct getfsstat_args *uap)
1253 struct thread *td = curthread;
1254 struct proc *p = td->td_proc;
1255 struct getfsstat_info info;
1257 bzero(&info, sizeof(info));
1259 info.maxcount = uap->bufsize / sizeof(struct statfs);
1260 info.sfsp = uap->buf;
1262 info.flags = uap->flags;
1265 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1266 if (info.sfsp && info.count > info.maxcount)
1267 uap->sysmsg_result = info.maxcount;
1269 uap->sysmsg_result = info.count;
1270 return (info.error);
1274 getfsstat_callback(struct mount *mp, void *data)
1276 struct getfsstat_info *info = data;
1282 if (info->sfsp && info->count < info->maxcount) {
1283 if (info->p && !chroot_visible_mnt(mp, info->p))
1288 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1289 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1290 * overrides MNT_WAIT.
1292 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1293 (info->flags & MNT_WAIT)) &&
1294 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1297 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1299 error = mount_path(info->p, mp, &fullpath, &freepath);
1301 info->error = error;
1304 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1305 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1306 kfree(freepath, M_TEMP);
1308 error = copyout(sp, info->sfsp, sizeof(*sp));
1310 info->error = error;
1320 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1321 long bufsize, int flags)
1323 * Get statistics on all filesystems.
1326 struct getvfsstat_info {
1327 struct statfs *sfsp;
1328 struct statvfs *vsfsp;
1336 static int getvfsstat_callback(struct mount *, void *);
1340 sys_getvfsstat(struct getvfsstat_args *uap)
1342 struct thread *td = curthread;
1343 struct proc *p = td->td_proc;
1344 struct getvfsstat_info info;
1346 bzero(&info, sizeof(info));
1348 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1349 info.sfsp = uap->buf;
1350 info.vsfsp = uap->vbuf;
1352 info.flags = uap->flags;
1355 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1356 if (info.vsfsp && info.count > info.maxcount)
1357 uap->sysmsg_result = info.maxcount;
1359 uap->sysmsg_result = info.count;
1360 return (info.error);
1364 getvfsstat_callback(struct mount *mp, void *data)
1366 struct getvfsstat_info *info = data;
1368 struct statvfs *vsp;
1373 if (info->vsfsp && info->count < info->maxcount) {
1374 if (info->p && !chroot_visible_mnt(mp, info->p))
1377 vsp = &mp->mnt_vstat;
1380 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1381 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1382 * overrides MNT_WAIT.
1384 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1385 (info->flags & MNT_WAIT)) &&
1386 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1389 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1391 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1392 (info->flags & MNT_WAIT)) &&
1393 (error = VFS_STATVFS(mp, vsp, info->p->p_ucred))) {
1397 if (mp->mnt_flag & MNT_RDONLY)
1398 vsp->f_flag |= ST_RDONLY;
1399 if (mp->mnt_flag & MNT_NOSUID)
1400 vsp->f_flag |= ST_NOSUID;
1402 error = mount_path(info->p, mp, &fullpath, &freepath);
1404 info->error = error;
1407 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1408 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1409 kfree(freepath, M_TEMP);
1411 error = copyout(sp, info->sfsp, sizeof(*sp));
1413 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1415 info->error = error;
1427 * fchdir_args(int fd)
1429 * Change current working directory to a given file descriptor.
1433 sys_fchdir(struct fchdir_args *uap)
1435 struct thread *td = curthread;
1436 struct proc *p = td->td_proc;
1437 struct filedesc *fdp = p->p_fd;
1438 struct vnode *vp, *ovp;
1441 struct nchandle nch, onch, tnch;
1444 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1446 vp = (struct vnode *)fp->f_data;
1448 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1449 if (vp->v_type != VDIR || fp->f_nchandle.ncp == NULL)
1452 error = VOP_ACCESS(vp, VEXEC, p->p_ucred);
1458 cache_copy(&fp->f_nchandle, &nch);
1461 * If the ncp has become a mount point, traverse through
1465 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1466 (mp = cache_findmount(&nch)) != NULL
1468 error = nlookup_mp(mp, &tnch);
1470 cache_unlock(&tnch); /* leave ref intact */
1472 vp = tnch.ncp->nc_vp;
1473 error = vget(vp, LK_SHARED);
1474 KKASSERT(error == 0);
1481 onch = fdp->fd_ncdir;
1482 vn_unlock(vp); /* leave ref intact */
1484 fdp->fd_ncdir = nch;
1496 kern_chdir(struct nlookupdata *nd)
1498 struct thread *td = curthread;
1499 struct proc *p = td->td_proc;
1500 struct filedesc *fdp = p->p_fd;
1501 struct vnode *vp, *ovp;
1502 struct nchandle onch;
1505 if ((error = nlookup(nd)) != 0)
1507 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1509 if ((error = vget(vp, LK_SHARED)) != 0)
1512 error = checkvp_chdir(vp, td);
1516 onch = fdp->fd_ncdir;
1517 cache_unlock(&nd->nl_nch); /* leave reference intact */
1518 fdp->fd_ncdir = nd->nl_nch;
1522 cache_zero(&nd->nl_nch);
1530 * chdir_args(char *path)
1532 * Change current working directory (``.'').
1535 sys_chdir(struct chdir_args *uap)
1537 struct nlookupdata nd;
1540 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1542 error = kern_chdir(&nd);
1548 * Helper function for raised chroot(2) security function: Refuse if
1549 * any filedescriptors are open directories.
1552 chroot_refuse_vdir_fds(struct filedesc *fdp)
1559 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1560 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1562 vp = (struct vnode *)fp->f_data;
1563 if (vp->v_type != VDIR) {
1574 * This sysctl determines if we will allow a process to chroot(2) if it
1575 * has a directory open:
1576 * 0: disallowed for all processes.
1577 * 1: allowed for processes that were not already chroot(2)'ed.
1578 * 2: allowed for all processes.
1581 static int chroot_allow_open_directories = 1;
1583 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1584 &chroot_allow_open_directories, 0, "");
1587 * chroot to the specified namecache entry. We obtain the vp from the
1588 * namecache data. The passed ncp must be locked and referenced and will
1589 * remain locked and referenced on return.
1592 kern_chroot(struct nchandle *nch)
1594 struct thread *td = curthread;
1595 struct proc *p = td->td_proc;
1596 struct filedesc *fdp = p->p_fd;
1601 * Only privileged user can chroot
1603 error = priv_check_cred(p->p_ucred, PRIV_VFS_CHROOT, 0);
1608 * Disallow open directory descriptors (fchdir() breakouts).
1610 if (chroot_allow_open_directories == 0 ||
1611 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1612 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1615 if ((vp = nch->ncp->nc_vp) == NULL)
1618 if ((error = vget(vp, LK_SHARED)) != 0)
1622 * Check the validity of vp as a directory to change to and
1623 * associate it with rdir/jdir.
1625 error = checkvp_chdir(vp, td);
1626 vn_unlock(vp); /* leave reference intact */
1628 vrele(fdp->fd_rdir);
1629 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1630 cache_drop(&fdp->fd_nrdir);
1631 cache_copy(nch, &fdp->fd_nrdir);
1632 if (fdp->fd_jdir == NULL) {
1635 cache_copy(nch, &fdp->fd_njdir);
1644 * chroot_args(char *path)
1646 * Change notion of root (``/'') directory.
1650 sys_chroot(struct chroot_args *uap)
1652 struct thread *td = curthread;
1653 struct nlookupdata nd;
1656 KKASSERT(td->td_proc);
1657 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1662 nd.nl_flags |= NLC_EXEC;
1663 error = nlookup(&nd);
1665 error = kern_chroot(&nd.nl_nch);
1671 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1672 * determine whether it is legal to chdir to the vnode. The vnode's state
1673 * is not changed by this call.
1676 checkvp_chdir(struct vnode *vp, struct thread *td)
1680 if (vp->v_type != VDIR)
1683 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred);
1688 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1690 struct thread *td = curthread;
1691 struct proc *p = td->td_proc;
1692 struct lwp *lp = td->td_lwp;
1693 struct filedesc *fdp = p->p_fd;
1698 int type, indx, error;
1701 if ((oflags & O_ACCMODE) == O_ACCMODE)
1703 flags = FFLAGS(oflags);
1704 error = falloc(p, &nfp, NULL);
1708 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1711 * XXX p_dupfd is a real mess. It allows a device to return a
1712 * file descriptor to be duplicated rather then doing the open
1718 * Call vn_open() to do the lookup and assign the vnode to the
1719 * file pointer. vn_open() does not change the ref count on fp
1720 * and the vnode, on success, will be inherited by the file pointer
1723 nd->nl_flags |= NLC_LOCKVP;
1724 error = vn_open(nd, fp, flags, cmode);
1728 * handle special fdopen() case. bleh. dupfdopen() is
1729 * responsible for dropping the old contents of ofiles[indx]
1732 * Note that fsetfd() will add a ref to fp which represents
1733 * the fd_files[] assignment. We must still drop our
1736 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1737 if (fdalloc(p, 0, &indx) == 0) {
1738 error = dupfdopen(p, indx, lp->lwp_dupfd, flags, error);
1741 fdrop(fp); /* our ref */
1744 fsetfd(p, NULL, indx);
1747 fdrop(fp); /* our ref */
1748 if (error == ERESTART)
1754 * ref the vnode for ourselves so it can't be ripped out from under
1755 * is. XXX need an ND flag to request that the vnode be returned
1758 * Reserve a file descriptor but do not assign it until the open
1761 vp = (struct vnode *)fp->f_data;
1763 if ((error = fdalloc(p, 0, &indx)) != 0) {
1770 * If no error occurs the vp will have been assigned to the file
1775 if (flags & (O_EXLOCK | O_SHLOCK)) {
1776 lf.l_whence = SEEK_SET;
1779 if (flags & O_EXLOCK)
1780 lf.l_type = F_WRLCK;
1782 lf.l_type = F_RDLCK;
1783 if (flags & FNONBLOCK)
1788 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1790 * lock request failed. Clean up the reserved
1794 fsetfd(p, NULL, indx);
1798 fp->f_flag |= FHASLOCK;
1802 * Assert that all regular file vnodes were created with a object.
1804 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1805 ("open: regular file has no backing object after vn_open"));
1811 * release our private reference, leaving the one associated with the
1812 * descriptor table intact.
1814 fsetfd(p, fp, indx);
1821 * open_args(char *path, int flags, int mode)
1823 * Check permissions, allocate an open file structure,
1824 * and call the device open routine if any.
1827 sys_open(struct open_args *uap)
1829 struct nlookupdata nd;
1832 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1834 error = kern_open(&nd, uap->flags,
1835 uap->mode, &uap->sysmsg_result);
1842 * openat_args(int fd, char *path, int flags, int mode)
1845 sys_openat(struct openat_args *uap)
1847 struct nlookupdata nd;
1851 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
1853 error = kern_open(&nd, uap->flags, uap->mode,
1854 &uap->sysmsg_result);
1856 nlookup_done_at(&nd, fp);
1861 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
1863 struct thread *td = curthread;
1864 struct proc *p = td->td_proc;
1873 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1874 vattr.va_rmajor = rmajor;
1875 vattr.va_rminor = rminor;
1877 switch (mode & S_IFMT) {
1878 case S_IFMT: /* used by badsect to flag bad sectors */
1879 error = priv_check_cred(p->p_ucred, PRIV_VFS_MKNOD_BAD, 0);
1880 vattr.va_type = VBAD;
1883 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1884 vattr.va_type = VCHR;
1887 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1888 vattr.va_type = VBLK;
1891 error = priv_check_cred(p->p_ucred, PRIV_VFS_MKNOD_WHT, 0);
1894 case S_IFDIR: /* special directories support for HAMMER */
1895 error = priv_check_cred(p->p_ucred, PRIV_VFS_MKNOD_DIR, 0);
1896 vattr.va_type = VDIR;
1907 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
1908 if ((error = nlookup(nd)) != 0)
1910 if (nd->nl_nch.ncp->nc_vp)
1912 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1916 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
1917 nd->nl_cred, NAMEI_CREATE);
1920 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
1921 &vp, nd->nl_cred, &vattr);
1929 * mknod_args(char *path, int mode, int dev)
1931 * Create a special file.
1934 sys_mknod(struct mknod_args *uap)
1936 struct nlookupdata nd;
1939 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1941 error = kern_mknod(&nd, uap->mode,
1942 umajor(uap->dev), uminor(uap->dev));
1949 kern_mkfifo(struct nlookupdata *nd, int mode)
1951 struct thread *td = curthread;
1952 struct proc *p = td->td_proc;
1959 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
1960 if ((error = nlookup(nd)) != 0)
1962 if (nd->nl_nch.ncp->nc_vp)
1964 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1968 vattr.va_type = VFIFO;
1969 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1971 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
1978 * mkfifo_args(char *path, int mode)
1980 * Create a named pipe.
1983 sys_mkfifo(struct mkfifo_args *uap)
1985 struct nlookupdata nd;
1988 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1990 error = kern_mkfifo(&nd, uap->mode);
1995 static int hardlink_check_uid = 0;
1996 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1997 &hardlink_check_uid, 0,
1998 "Unprivileged processes cannot create hard links to files owned by other "
2000 static int hardlink_check_gid = 0;
2001 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2002 &hardlink_check_gid, 0,
2003 "Unprivileged processes cannot create hard links to files owned by other "
2007 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2013 * Shortcut if disabled
2015 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2019 * Privileged user can always hardlink
2021 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2025 * Otherwise only if the originating file is owned by the
2026 * same user or group. Note that any group is allowed if
2027 * the file is owned by the caller.
2029 error = VOP_GETATTR(vp, &va);
2033 if (hardlink_check_uid) {
2034 if (cred->cr_uid != va.va_uid)
2038 if (hardlink_check_gid) {
2039 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2047 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2049 struct thread *td = curthread;
2054 * Lookup the source and obtained a locked vnode.
2056 * You may only hardlink a file which you have write permission
2057 * on or which you own.
2059 * XXX relookup on vget failure / race ?
2062 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2063 if ((error = nlookup(nd)) != 0)
2065 vp = nd->nl_nch.ncp->nc_vp;
2066 KKASSERT(vp != NULL);
2067 if (vp->v_type == VDIR)
2068 return (EPERM); /* POSIX */
2069 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2071 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2075 * Unlock the source so we can lookup the target without deadlocking
2076 * (XXX vp is locked already, possible other deadlock?). The target
2079 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2080 nd->nl_flags &= ~NLC_NCPISLOCKED;
2081 cache_unlock(&nd->nl_nch);
2083 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2084 if ((error = nlookup(linknd)) != 0) {
2088 if (linknd->nl_nch.ncp->nc_vp) {
2094 * Finally run the new API VOP.
2096 error = can_hardlink(vp, td, td->td_proc->p_ucred);
2098 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2099 vp, linknd->nl_cred);
2106 * link_args(char *path, char *link)
2108 * Make a hard file link.
2111 sys_link(struct link_args *uap)
2113 struct nlookupdata nd, linknd;
2116 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2118 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2120 error = kern_link(&nd, &linknd);
2121 nlookup_done(&linknd);
2128 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2136 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2137 if ((error = nlookup(nd)) != 0)
2139 if (nd->nl_nch.ncp->nc_vp)
2141 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2145 vattr.va_mode = mode;
2146 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2153 * symlink(char *path, char *link)
2155 * Make a symbolic link.
2158 sys_symlink(struct symlink_args *uap)
2160 struct thread *td = curthread;
2161 struct nlookupdata nd;
2166 path = objcache_get(namei_oc, M_WAITOK);
2167 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2169 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2171 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2172 error = kern_symlink(&nd, path, mode);
2176 objcache_put(namei_oc, path);
2181 * undelete_args(char *path)
2183 * Delete a whiteout from the filesystem.
2187 sys_undelete(struct undelete_args *uap)
2189 struct nlookupdata nd;
2192 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2194 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2196 error = nlookup(&nd);
2198 error = ncp_writechk(&nd.nl_nch);
2200 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2208 kern_unlink(struct nlookupdata *nd)
2213 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2214 if ((error = nlookup(nd)) != 0)
2216 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2218 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2223 * unlink_args(char *path)
2225 * Delete a name from the filesystem.
2228 sys_unlink(struct unlink_args *uap)
2230 struct nlookupdata nd;
2233 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2235 error = kern_unlink(&nd);
2242 * unlinkat_args(int fd, char *path, int flags)
2244 * Delete the file or directory entry pointed to by fd/path.
2247 sys_unlinkat(struct unlinkat_args *uap)
2249 struct nlookupdata nd;
2253 if (uap->flags & ~AT_REMOVEDIR)
2256 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2258 if (uap->flags & AT_REMOVEDIR)
2259 error = kern_rmdir(&nd);
2261 error = kern_unlink(&nd);
2263 nlookup_done_at(&nd, fp);
2268 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2270 struct thread *td = curthread;
2271 struct proc *p = td->td_proc;
2278 fp = holdfp(p->p_fd, fd, -1);
2281 if (fp->f_type != DTYPE_VNODE) {
2285 vp = (struct vnode *)fp->f_data;
2289 new_offset = fp->f_offset + offset;
2293 error = VOP_GETATTR(vp, &vattr);
2294 new_offset = offset + vattr.va_size;
2297 new_offset = offset;
2307 * Validate the seek position. Negative offsets are not allowed
2308 * for regular files or directories.
2310 * Normally we would also not want to allow negative offsets for
2311 * character and block-special devices. However kvm addresses
2312 * on 64 bit architectures might appear to be negative and must
2316 if (new_offset < 0 &&
2317 (vp->v_type == VREG || vp->v_type == VDIR)) {
2320 fp->f_offset = new_offset;
2323 *res = fp->f_offset;
2330 * lseek_args(int fd, int pad, off_t offset, int whence)
2332 * Reposition read/write file offset.
2335 sys_lseek(struct lseek_args *uap)
2339 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2340 &uap->sysmsg_offset);
2346 * Check if current process can access given file. amode is a bitmask of *_OK
2347 * access bits. flags is a bitmask of AT_* flags.
2350 kern_access(struct nlookupdata *nd, int amode, int flags)
2355 if (flags & ~AT_EACCESS)
2357 if ((error = nlookup(nd)) != 0)
2360 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2364 /* Flags == 0 means only check for existence. */
2373 if ((mode & VWRITE) == 0 ||
2374 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2375 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2378 * If the file handle is stale we have to re-resolve the
2379 * entry. This is a hack at the moment.
2381 if (error == ESTALE) {
2383 cache_setunresolved(&nd->nl_nch);
2384 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2397 * access_args(char *path, int flags)
2399 * Check access permissions.
2402 sys_access(struct access_args *uap)
2404 struct nlookupdata nd;
2407 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2409 error = kern_access(&nd, uap->flags, 0);
2416 * faccessat_args(int fd, char *path, int amode, int flags)
2418 * Check access permissions.
2421 sys_faccessat(struct faccessat_args *uap)
2423 struct nlookupdata nd;
2427 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2430 error = kern_access(&nd, uap->amode, uap->flags);
2431 nlookup_done_at(&nd, fp);
2437 kern_stat(struct nlookupdata *nd, struct stat *st)
2443 if ((error = nlookup(nd)) != 0)
2446 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2450 if ((error = vget(vp, LK_SHARED)) != 0)
2452 error = vn_stat(vp, st, nd->nl_cred);
2455 * If the file handle is stale we have to re-resolve the entry. This
2456 * is a hack at the moment.
2458 if (error == ESTALE) {
2460 cache_setunresolved(&nd->nl_nch);
2461 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2471 * stat_args(char *path, struct stat *ub)
2473 * Get file status; this version follows links.
2476 sys_stat(struct stat_args *uap)
2478 struct nlookupdata nd;
2482 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2484 error = kern_stat(&nd, &st);
2486 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2493 * lstat_args(char *path, struct stat *ub)
2495 * Get file status; this version does not follow links.
2498 sys_lstat(struct lstat_args *uap)
2500 struct nlookupdata nd;
2504 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2506 error = kern_stat(&nd, &st);
2508 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2515 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2517 * Get status of file pointed to by fd/path.
2520 sys_fstatat(struct fstatat_args *uap)
2522 struct nlookupdata nd;
2528 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2531 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2533 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2534 UIO_USERSPACE, flags);
2536 error = kern_stat(&nd, &st);
2538 error = copyout(&st, uap->sb, sizeof(*uap->sb));
2540 nlookup_done_at(&nd, fp);
2545 * pathconf_Args(char *path, int name)
2547 * Get configurable pathname variables.
2551 sys_pathconf(struct pathconf_args *uap)
2553 struct nlookupdata nd;
2558 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2560 error = nlookup(&nd);
2562 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2565 error = VOP_PATHCONF(vp, uap->name, &uap->sysmsg_reg);
2573 * kern_readlink isn't properly split yet. There is a copyin burried
2574 * in VOP_READLINK().
2577 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2579 struct thread *td = curthread;
2580 struct proc *p = td->td_proc;
2586 if ((error = nlookup(nd)) != 0)
2588 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2591 if (vp->v_type != VLNK) {
2594 aiov.iov_base = buf;
2595 aiov.iov_len = count;
2596 auio.uio_iov = &aiov;
2597 auio.uio_iovcnt = 1;
2598 auio.uio_offset = 0;
2599 auio.uio_rw = UIO_READ;
2600 auio.uio_segflg = UIO_USERSPACE;
2602 auio.uio_resid = count;
2603 error = VOP_READLINK(vp, &auio, p->p_ucred);
2606 *res = count - auio.uio_resid;
2611 * readlink_args(char *path, char *buf, int count)
2613 * Return target name of a symbolic link.
2616 sys_readlink(struct readlink_args *uap)
2618 struct nlookupdata nd;
2621 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2623 error = kern_readlink(&nd, uap->buf, uap->count,
2624 &uap->sysmsg_result);
2631 setfflags(struct vnode *vp, int flags)
2633 struct thread *td = curthread;
2634 struct proc *p = td->td_proc;
2639 * Prevent non-root users from setting flags on devices. When
2640 * a device is reused, users can retain ownership of the device
2641 * if they are allowed to set flags and programs assume that
2642 * chown can't fail when done as root.
2644 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2645 ((error = priv_check_cred(p->p_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
2649 * note: vget is required for any operation that might mod the vnode
2650 * so VINACTIVE is properly cleared.
2652 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2654 vattr.va_flags = flags;
2655 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2662 * chflags(char *path, int flags)
2664 * Change flags of a file given a path name.
2668 sys_chflags(struct chflags_args *uap)
2670 struct nlookupdata nd;
2675 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2677 error = nlookup(&nd);
2679 error = ncp_writechk(&nd.nl_nch);
2681 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2684 error = setfflags(vp, uap->flags);
2691 * lchflags(char *path, int flags)
2693 * Change flags of a file given a path name, but don't follow symlinks.
2697 sys_lchflags(struct lchflags_args *uap)
2699 struct nlookupdata nd;
2704 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2706 error = nlookup(&nd);
2708 error = ncp_writechk(&nd.nl_nch);
2710 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2713 error = setfflags(vp, uap->flags);
2720 * fchflags_args(int fd, int flags)
2722 * Change flags of a file given a file descriptor.
2726 sys_fchflags(struct fchflags_args *uap)
2728 struct thread *td = curthread;
2729 struct proc *p = td->td_proc;
2733 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2735 if (fp->f_nchandle.ncp)
2736 error = ncp_writechk(&fp->f_nchandle);
2738 error = setfflags((struct vnode *) fp->f_data, uap->flags);
2744 setfmode(struct vnode *vp, int mode)
2746 struct thread *td = curthread;
2747 struct proc *p = td->td_proc;
2752 * note: vget is required for any operation that might mod the vnode
2753 * so VINACTIVE is properly cleared.
2755 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2757 vattr.va_mode = mode & ALLPERMS;
2758 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2765 kern_chmod(struct nlookupdata *nd, int mode)
2770 if ((error = nlookup(nd)) != 0)
2772 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2774 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2775 error = setfmode(vp, mode);
2781 * chmod_args(char *path, int mode)
2783 * Change mode of a file given path name.
2787 sys_chmod(struct chmod_args *uap)
2789 struct nlookupdata nd;
2792 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2794 error = kern_chmod(&nd, uap->mode);
2800 * lchmod_args(char *path, int mode)
2802 * Change mode of a file given path name (don't follow links.)
2806 sys_lchmod(struct lchmod_args *uap)
2808 struct nlookupdata nd;
2811 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2813 error = kern_chmod(&nd, uap->mode);
2819 * fchmod_args(int fd, int mode)
2821 * Change mode of a file given a file descriptor.
2825 sys_fchmod(struct fchmod_args *uap)
2827 struct thread *td = curthread;
2828 struct proc *p = td->td_proc;
2832 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2834 if (fp->f_nchandle.ncp)
2835 error = ncp_writechk(&fp->f_nchandle);
2837 error = setfmode((struct vnode *)fp->f_data, uap->mode);
2843 * fchmodat_args(char *path, int mode)
2845 * Change mode of a file pointed to by fd/path.
2848 sys_fchmodat(struct fchmodat_args *uap)
2850 struct nlookupdata nd;
2855 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2857 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2859 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2860 UIO_USERSPACE, flags);
2862 error = kern_chmod(&nd, uap->mode);
2863 nlookup_done_at(&nd, fp);
2868 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2870 struct thread *td = curthread;
2871 struct proc *p = td->td_proc;
2876 * note: vget is required for any operation that might mod the vnode
2877 * so VINACTIVE is properly cleared.
2879 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2883 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2890 kern_chown(struct nlookupdata *nd, int uid, int gid)
2895 if ((error = nlookup(nd)) != 0)
2897 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2899 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2900 error = setfown(vp, uid, gid);
2906 * chown(char *path, int uid, int gid)
2908 * Set ownership given a path name.
2911 sys_chown(struct chown_args *uap)
2913 struct nlookupdata nd;
2916 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2918 error = kern_chown(&nd, uap->uid, uap->gid);
2924 * lchown_args(char *path, int uid, int gid)
2926 * Set ownership given a path name, do not cross symlinks.
2929 sys_lchown(struct lchown_args *uap)
2931 struct nlookupdata nd;
2934 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2936 error = kern_chown(&nd, uap->uid, uap->gid);
2942 * fchown_args(int fd, int uid, int gid)
2944 * Set ownership given a file descriptor.
2948 sys_fchown(struct fchown_args *uap)
2950 struct thread *td = curthread;
2951 struct proc *p = td->td_proc;
2955 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2957 if (fp->f_nchandle.ncp)
2958 error = ncp_writechk(&fp->f_nchandle);
2960 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
2966 * fchownat(int fd, char *path, int uid, int gid, int flags)
2968 * Set ownership of file pointed to by fd/path.
2971 sys_fchownat(struct fchownat_args *uap)
2973 struct nlookupdata nd;
2978 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2980 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2982 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2983 UIO_USERSPACE, flags);
2985 error = kern_chown(&nd, uap->uid, uap->gid);
2986 nlookup_done_at(&nd, fp);
2992 getutimes(const struct timeval *tvp, struct timespec *tsp)
2994 struct timeval tv[2];
2998 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3001 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3002 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3008 setutimes(struct vnode *vp, struct vattr *vattr,
3009 const struct timespec *ts, int nullflag)
3011 struct thread *td = curthread;
3012 struct proc *p = td->td_proc;
3016 vattr->va_atime = ts[0];
3017 vattr->va_mtime = ts[1];
3019 vattr->va_vaflags |= VA_UTIMES_NULL;
3020 error = VOP_SETATTR(vp, vattr, p->p_ucred);
3026 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3028 struct timespec ts[2];
3033 if ((error = getutimes(tptr, ts)) != 0)
3037 * NOTE: utimes() succeeds for the owner even if the file
3038 * is not user-writable.
3040 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3042 if ((error = nlookup(nd)) != 0)
3044 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3046 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3050 * note: vget is required for any operation that might mod the vnode
3051 * so VINACTIVE is properly cleared.
3053 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3054 error = vget(vp, LK_EXCLUSIVE);
3056 error = setutimes(vp, &vattr, ts, (tptr == NULL));
3065 * utimes_args(char *path, struct timeval *tptr)
3067 * Set the access and modification times of a file.
3070 sys_utimes(struct utimes_args *uap)
3072 struct timeval tv[2];
3073 struct nlookupdata nd;
3077 error = copyin(uap->tptr, tv, sizeof(tv));
3081 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3083 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3089 * lutimes_args(char *path, struct timeval *tptr)
3091 * Set the access and modification times of a file.
3094 sys_lutimes(struct lutimes_args *uap)
3096 struct timeval tv[2];
3097 struct nlookupdata nd;
3101 error = copyin(uap->tptr, tv, sizeof(tv));
3105 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3107 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3113 * Set utimes on a file descriptor. The creds used to open the
3114 * file are used to determine whether the operation is allowed
3118 kern_futimes(int fd, struct timeval *tptr)
3120 struct thread *td = curthread;
3121 struct proc *p = td->td_proc;
3122 struct timespec ts[2];
3128 error = getutimes(tptr, ts);
3131 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3133 if (fp->f_nchandle.ncp)
3134 error = ncp_writechk(&fp->f_nchandle);
3137 error = vget(vp, LK_EXCLUSIVE);
3139 error = VOP_GETATTR(vp, &vattr);
3141 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3145 error = setutimes(vp, &vattr, ts,
3156 * futimes_args(int fd, struct timeval *tptr)
3158 * Set the access and modification times of a file.
3161 sys_futimes(struct futimes_args *uap)
3163 struct timeval tv[2];
3167 error = copyin(uap->tptr, tv, sizeof(tv));
3172 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3178 kern_truncate(struct nlookupdata *nd, off_t length)
3186 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3187 if ((error = nlookup(nd)) != 0)
3189 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3191 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3193 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
3197 if (vp->v_type == VDIR) {
3199 } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3201 vattr.va_size = length;
3202 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3209 * truncate(char *path, int pad, off_t length)
3211 * Truncate a file given its path name.
3214 sys_truncate(struct truncate_args *uap)
3216 struct nlookupdata nd;
3219 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3221 error = kern_truncate(&nd, uap->length);
3227 kern_ftruncate(int fd, off_t length)
3229 struct thread *td = curthread;
3230 struct proc *p = td->td_proc;
3238 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3240 if (fp->f_nchandle.ncp) {
3241 error = ncp_writechk(&fp->f_nchandle);
3245 if ((fp->f_flag & FWRITE) == 0) {
3249 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3253 vp = (struct vnode *)fp->f_data;
3254 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3255 if (vp->v_type == VDIR) {
3257 } else if ((error = vn_writechk(vp, NULL)) == 0) {
3259 vattr.va_size = length;
3260 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3269 * ftruncate_args(int fd, int pad, off_t length)
3271 * Truncate a file given a file descriptor.
3274 sys_ftruncate(struct ftruncate_args *uap)
3278 error = kern_ftruncate(uap->fd, uap->length);
3286 * Sync an open file.
3290 sys_fsync(struct fsync_args *uap)
3292 struct thread *td = curthread;
3293 struct proc *p = td->td_proc;
3299 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3301 vp = (struct vnode *)fp->f_data;
3302 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3303 if ((obj = vp->v_object) != NULL)
3304 vm_object_page_clean(obj, 0, 0, 0);
3305 error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
3306 if (error == 0 && vp->v_mount)
3307 error = buf_fsync(vp);
3314 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3316 struct nchandle fnchd;
3317 struct nchandle tnchd;
3318 struct namecache *ncp;
3325 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3326 if ((error = nlookup(fromnd)) != 0)
3328 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3330 fnchd.mount = fromnd->nl_nch.mount;
3334 * unlock the source nch so we can lookup the target nch without
3335 * deadlocking. The target may or may not exist so we do not check
3336 * for a target vp like kern_mkdir() and other creation functions do.
3338 * The source and target directories are ref'd and rechecked after
3339 * everything is relocked to determine if the source or target file
3342 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3343 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3344 cache_unlock(&fromnd->nl_nch);
3346 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3347 if ((error = nlookup(tond)) != 0) {
3351 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3355 tnchd.mount = tond->nl_nch.mount;
3359 * If the source and target are the same there is nothing to do
3361 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3368 * Mount points cannot be renamed or overwritten
3370 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3379 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
3380 * may have become invalid while it was unlocked, nc_vp and nc_mount
3383 if (cache_lock_nonblock(&fromnd->nl_nch) == 0) {
3384 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3385 } else if (fromnd->nl_nch.ncp > tond->nl_nch.ncp) {
3386 cache_lock(&fromnd->nl_nch);
3387 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3389 cache_unlock(&tond->nl_nch);
3390 cache_lock(&fromnd->nl_nch);
3391 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3392 cache_lock(&tond->nl_nch);
3393 cache_resolve(&tond->nl_nch, tond->nl_cred);
3395 fromnd->nl_flags |= NLC_NCPISLOCKED;
3398 * make sure the parent directories linkages are the same
3400 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3401 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3408 * Both the source and target must be within the same filesystem and
3409 * in the same filesystem as their parent directories within the
3410 * namecache topology.
3412 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3415 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3416 mp != tond->nl_nch.mount) {
3423 * Make sure the mount point is writable
3425 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3432 * If the target exists and either the source or target is a directory,
3433 * then both must be directories.
3435 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3438 if (tond->nl_nch.ncp->nc_vp) {
3439 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3441 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3442 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3444 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3450 * You cannot rename a source into itself or a subdirectory of itself.
3451 * We check this by travsersing the target directory upwards looking
3452 * for a match against the source.
3455 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3456 if (fromnd->nl_nch.ncp == ncp) {
3467 * Even though the namespaces are different, they may still represent
3468 * hardlinks to the same file. The filesystem might have a hard time
3469 * with this so we issue a NREMOVE of the source instead of a NRENAME
3470 * when we detect the situation.
3473 fdvp = fromnd->nl_dvp;
3474 tdvp = tond->nl_dvp;
3475 if (fdvp == NULL || tdvp == NULL) {
3477 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3478 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3481 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3482 fdvp, tdvp, tond->nl_cred);
3489 * rename_args(char *from, char *to)
3491 * Rename files. Source and destination must either both be directories,
3492 * or both not be directories. If target is a directory, it must be empty.
3495 sys_rename(struct rename_args *uap)
3497 struct nlookupdata fromnd, tond;
3500 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3502 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3504 error = kern_rename(&fromnd, &tond);
3505 nlookup_done(&tond);
3507 nlookup_done(&fromnd);
3512 kern_mkdir(struct nlookupdata *nd, int mode)
3514 struct thread *td = curthread;
3515 struct proc *p = td->td_proc;
3521 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3522 if ((error = nlookup(nd)) != 0)
3525 if (nd->nl_nch.ncp->nc_vp)
3527 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3530 vattr.va_type = VDIR;
3531 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3534 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, p->p_ucred, &vattr);
3541 * mkdir_args(char *path, int mode)
3543 * Make a directory file.
3547 sys_mkdir(struct mkdir_args *uap)
3549 struct nlookupdata nd;
3552 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3554 error = kern_mkdir(&nd, uap->mode);
3560 kern_rmdir(struct nlookupdata *nd)
3565 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
3566 if ((error = nlookup(nd)) != 0)
3570 * Do not allow directories representing mount points to be
3571 * deleted, even if empty. Check write perms on mount point
3572 * in case the vnode is aliased (aka nullfs).
3574 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
3576 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3578 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
3583 * rmdir_args(char *path)
3585 * Remove a directory file.
3589 sys_rmdir(struct rmdir_args *uap)
3591 struct nlookupdata nd;
3594 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3596 error = kern_rmdir(&nd);
3602 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
3603 enum uio_seg direction)
3605 struct thread *td = curthread;
3606 struct proc *p = td->td_proc;
3614 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3616 if ((fp->f_flag & FREAD) == 0) {
3620 vp = (struct vnode *)fp->f_data;
3622 if (vp->v_type != VDIR) {
3626 aiov.iov_base = buf;
3627 aiov.iov_len = count;
3628 auio.uio_iov = &aiov;
3629 auio.uio_iovcnt = 1;
3630 auio.uio_rw = UIO_READ;
3631 auio.uio_segflg = direction;
3633 auio.uio_resid = count;
3634 loff = auio.uio_offset = fp->f_offset;
3635 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3636 fp->f_offset = auio.uio_offset;
3639 if (count == auio.uio_resid) {
3640 if (union_dircheckp) {
3641 error = union_dircheckp(td, &vp, fp);
3648 if ((vp->v_flag & VROOT) &&
3649 (vp->v_mount->mnt_flag & MNT_UNION)) {
3650 struct vnode *tvp = vp;
3651 vp = vp->v_mount->mnt_vnodecovered;
3662 * WARNING! *basep may not be wide enough to accomodate the
3663 * seek offset. XXX should we hack this to return the upper 32 bits
3664 * for offsets greater then 4G?
3667 *basep = (long)loff;
3669 *res = count - auio.uio_resid;
3676 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
3678 * Read a block of directory entries in a file system independent format.
3681 sys_getdirentries(struct getdirentries_args *uap)
3686 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
3687 &uap->sysmsg_result, UIO_USERSPACE);
3689 if (error == 0 && uap->basep)
3690 error = copyout(&base, uap->basep, sizeof(*uap->basep));
3695 * getdents_args(int fd, char *buf, size_t count)
3698 sys_getdents(struct getdents_args *uap)
3702 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
3703 &uap->sysmsg_result, UIO_USERSPACE);
3709 * umask(int newmask)
3711 * Set the mode mask for creation of filesystem nodes.
3716 sys_umask(struct umask_args *uap)
3718 struct thread *td = curthread;
3719 struct proc *p = td->td_proc;
3720 struct filedesc *fdp;
3723 uap->sysmsg_result = fdp->fd_cmask;
3724 fdp->fd_cmask = uap->newmask & ALLPERMS;
3729 * revoke(char *path)
3731 * Void all references to file by ripping underlying filesystem
3736 sys_revoke(struct revoke_args *uap)
3738 struct nlookupdata nd;
3745 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3747 error = nlookup(&nd);
3749 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3750 cred = crhold(nd.nl_cred);
3754 error = VOP_GETATTR(vp, &vattr);
3755 if (error == 0 && cred->cr_uid != vattr.va_uid)
3756 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
3757 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
3759 error = vrevoke(vp, cred);
3760 } else if (error == 0) {
3761 error = vrevoke(vp, cred);
3771 * getfh_args(char *fname, fhandle_t *fhp)
3773 * Get (NFS) file handle
3775 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
3776 * mount. This allows nullfs mounts to be explicitly exported.
3778 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
3780 * nullfs mounts of subdirectories are not safe. That is, it will
3781 * work, but you do not really have protection against access to
3782 * the related parent directories.
3785 sys_getfh(struct getfh_args *uap)
3787 struct thread *td = curthread;
3788 struct nlookupdata nd;
3795 * Must be super user
3797 if ((error = priv_check(td, PRIV_ROOT)) != 0)
3801 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3803 error = nlookup(&nd);
3805 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3806 mp = nd.nl_nch.mount;
3809 bzero(&fh, sizeof(fh));
3810 fh.fh_fsid = mp->mnt_stat.f_fsid;
3811 error = VFS_VPTOFH(vp, &fh.fh_fid);
3814 error = copyout(&fh, uap->fhp, sizeof(fh));
3820 * fhopen_args(const struct fhandle *u_fhp, int flags)
3822 * syscall for the rpc.lockd to use to translate a NFS file handle into
3823 * an open descriptor.
3825 * warning: do not remove the priv_check() call or this becomes one giant
3829 sys_fhopen(struct fhopen_args *uap)
3831 struct thread *td = curthread;
3832 struct proc *p = td->td_proc;
3837 struct vattr *vap = &vat;
3839 int fmode, mode, error, type;
3845 * Must be super user
3847 error = priv_check(td, PRIV_ROOT);
3851 fmode = FFLAGS(uap->flags);
3852 /* why not allow a non-read/write open for our lockd? */
3853 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3855 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3858 /* find the mount point */
3859 mp = vfs_getvfs(&fhp.fh_fsid);
3862 /* now give me my vnode, it gets returned to me locked */
3863 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
3867 * from now on we have to make sure not
3868 * to forget about the vnode
3869 * any error that causes an abort must vput(vp)
3870 * just set error = err and 'goto bad;'.
3876 if (vp->v_type == VLNK) {
3880 if (vp->v_type == VSOCK) {
3885 if (fmode & (FWRITE | O_TRUNC)) {
3886 if (vp->v_type == VDIR) {
3890 error = vn_writechk(vp, NULL);
3898 error = VOP_ACCESS(vp, mode, p->p_ucred);
3902 if (fmode & O_TRUNC) {
3903 vn_unlock(vp); /* XXX */
3904 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
3907 error = VOP_SETATTR(vp, vap, p->p_ucred);
3913 * VOP_OPEN needs the file pointer so it can potentially override
3916 * WARNING! no f_nchandle will be associated when fhopen()ing a
3919 if ((error = falloc(p, &nfp, &indx)) != 0)
3923 error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
3926 * setting f_ops this way prevents VOP_CLOSE from being
3927 * called or fdrop() releasing the vp from v_data. Since
3928 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3930 fp->f_ops = &badfileops;
3936 * The fp is given its own reference, we still have our ref and lock.
3938 * Assert that all regular files must be created with a VM object.
3940 if (vp->v_type == VREG && vp->v_object == NULL) {
3941 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
3946 * The open was successful. Handle any locking requirements.
3948 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3949 lf.l_whence = SEEK_SET;
3952 if (fmode & O_EXLOCK)
3953 lf.l_type = F_WRLCK;
3955 lf.l_type = F_RDLCK;
3956 if (fmode & FNONBLOCK)
3961 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3963 * release our private reference.
3965 fsetfd(p, NULL, indx);
3970 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3971 fp->f_flag |= FHASLOCK;
3975 * Clean up. Associate the file pointer with the previously
3976 * reserved descriptor and return it.
3979 fsetfd(p, fp, indx);
3981 uap->sysmsg_result = indx;
3985 fsetfd(p, NULL, indx);
3993 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3996 sys_fhstat(struct fhstat_args *uap)
3998 struct thread *td = curthread;
4006 * Must be super user
4008 error = priv_check(td, PRIV_ROOT);
4012 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4016 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4018 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4020 error = vn_stat(vp, &sb, td->td_proc->p_ucred);
4024 error = copyout(&sb, uap->sb, sizeof(sb));
4029 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4032 sys_fhstatfs(struct fhstatfs_args *uap)
4034 struct thread *td = curthread;
4035 struct proc *p = td->td_proc;
4040 char *fullpath, *freepath;
4045 * Must be super user
4047 if ((error = priv_check(td, PRIV_ROOT)))
4050 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4053 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4056 if (p != NULL && !chroot_visible_mnt(mp, p))
4059 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4064 if ((error = VFS_STATFS(mp, sp, p->p_ucred)) != 0)
4067 error = mount_path(p, mp, &fullpath, &freepath);
4070 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4071 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4072 kfree(freepath, M_TEMP);
4074 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4075 if (priv_check(td, PRIV_ROOT)) {
4076 bcopy(sp, &sb, sizeof(sb));
4077 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4080 return (copyout(sp, uap->buf, sizeof(*sp)));
4084 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4087 sys_fhstatvfs(struct fhstatvfs_args *uap)
4089 struct thread *td = curthread;
4090 struct proc *p = td->td_proc;
4098 * Must be super user
4100 if ((error = priv_check(td, PRIV_ROOT)))
4103 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4106 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4109 if (p != NULL && !chroot_visible_mnt(mp, p))
4112 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4115 sp = &mp->mnt_vstat;
4117 if ((error = VFS_STATVFS(mp, sp, p->p_ucred)) != 0)
4121 if (mp->mnt_flag & MNT_RDONLY)
4122 sp->f_flag |= ST_RDONLY;
4123 if (mp->mnt_flag & MNT_NOSUID)
4124 sp->f_flag |= ST_NOSUID;
4126 return (copyout(sp, uap->buf, sizeof(*sp)));
4131 * Syscall to push extended attribute configuration information into the
4132 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4133 * a command (int cmd), and attribute name and misc data. For now, the
4134 * attribute name is left in userspace for consumption by the VFS_op.
4135 * It will probably be changed to be copied into sysspace by the
4136 * syscall in the future, once issues with various consumers of the
4137 * attribute code have raised their hands.
4139 * Currently this is used only by UFS Extended Attributes.
4142 sys_extattrctl(struct extattrctl_args *uap)
4144 struct nlookupdata nd;
4150 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4152 error = nlookup(&nd);
4154 mp = nd.nl_nch.mount;
4155 error = VFS_EXTATTRCTL(mp, uap->cmd,
4156 uap->attrname, uap->arg,
4164 * Syscall to set a named extended attribute on a file or directory.
4165 * Accepts attribute name, and a uio structure pointing to the data to set.
4166 * The uio is consumed in the style of writev(). The real work happens
4167 * in VOP_SETEXTATTR().
4170 sys_extattr_set_file(struct extattr_set_file_args *uap)
4172 char attrname[EXTATTR_MAXNAMELEN];
4173 struct iovec aiov[UIO_SMALLIOV];
4174 struct iovec *needfree;
4175 struct nlookupdata nd;
4184 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4189 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4191 error = nlookup(&nd);
4193 error = ncp_writechk(&nd.nl_nch);
4195 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4202 iovlen = uap->iovcnt * sizeof(struct iovec);
4203 if (uap->iovcnt > UIO_SMALLIOV) {
4204 if (uap->iovcnt > UIO_MAXIOV) {
4208 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
4214 auio.uio_iovcnt = uap->iovcnt;
4215 auio.uio_rw = UIO_WRITE;
4216 auio.uio_segflg = UIO_USERSPACE;
4217 auio.uio_td = nd.nl_td;
4218 auio.uio_offset = 0;
4219 if ((error = copyin(uap->iovp, iov, iovlen)))
4222 for (i = 0; i < uap->iovcnt; i++) {
4223 if (iov->iov_len > LONG_MAX - auio.uio_resid) {
4227 auio.uio_resid += iov->iov_len;
4230 cnt = auio.uio_resid;
4231 error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred);
4232 cnt -= auio.uio_resid;
4233 uap->sysmsg_result = cnt;
4238 FREE(needfree, M_IOV);
4243 * Syscall to get a named extended attribute on a file or directory.
4244 * Accepts attribute name, and a uio structure pointing to a buffer for the
4245 * data. The uio is consumed in the style of readv(). The real work
4246 * happens in VOP_GETEXTATTR();
4249 sys_extattr_get_file(struct extattr_get_file_args *uap)
4251 char attrname[EXTATTR_MAXNAMELEN];
4252 struct iovec aiov[UIO_SMALLIOV];
4253 struct iovec *needfree;
4254 struct nlookupdata nd;
4263 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4268 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4270 error = nlookup(&nd);
4272 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4278 iovlen = uap->iovcnt * sizeof (struct iovec);
4280 if (uap->iovcnt > UIO_SMALLIOV) {
4281 if (uap->iovcnt > UIO_MAXIOV) {
4285 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
4291 auio.uio_iovcnt = uap->iovcnt;
4292 auio.uio_rw = UIO_READ;
4293 auio.uio_segflg = UIO_USERSPACE;
4294 auio.uio_td = nd.nl_td;
4295 auio.uio_offset = 0;
4296 if ((error = copyin(uap->iovp, iov, iovlen)))
4299 for (i = 0; i < uap->iovcnt; i++) {
4300 if (iov->iov_len > LONG_MAX - auio.uio_resid) {
4304 auio.uio_resid += iov->iov_len;
4307 cnt = auio.uio_resid;
4308 error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred);
4309 cnt -= auio.uio_resid;
4310 uap->sysmsg_result = cnt;
4315 FREE(needfree, M_IOV);
4320 * Syscall to delete a named extended attribute from a file or directory.
4321 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4324 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4326 char attrname[EXTATTR_MAXNAMELEN];
4327 struct nlookupdata nd;
4331 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4336 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4338 error = nlookup(&nd);
4340 error = ncp_writechk(&nd.nl_nch);
4342 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4348 error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred);
4355 * Determine if the mount is visible to the process.
4358 chroot_visible_mnt(struct mount *mp, struct proc *p)
4360 struct nchandle nch;
4363 * Traverse from the mount point upwards. If we hit the process
4364 * root then the mount point is visible to the process.
4366 nch = mp->mnt_ncmountpt;
4368 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4369 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4372 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4373 nch = nch.mount->mnt_ncmounton;
4375 nch.ncp = nch.ncp->nc_parent;
4380 * If the mount point is not visible to the process, but the
4381 * process root is in a subdirectory of the mount, return
4384 if (p->p_fd->fd_nrdir.mount == mp)