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 ((error = priv_check(td, PRIV_ROOT)) != 0)
915 * Argument length checks
917 if (uap->ctllen < 0 || uap->ctllen > 1024)
919 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
921 if (uap->path == NULL)
925 * Allocate the necessary buffers and copyin data
927 path = objcache_get(namei_oc, M_WAITOK);
928 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
933 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
934 error = copyin(uap->ctl, ctl, uap->ctllen);
939 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
942 * Validate the descriptor
945 fp = holdfp(p->p_fd, uap->fd, -1);
955 * Execute the internal kernel function and clean up.
957 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
960 if (error == 0 && uap->sysmsg_result > 0)
961 error = copyout(buf, uap->buf, uap->sysmsg_result);
964 objcache_put(namei_oc, path);
973 * Execute a mount control operation by resolving the path to a mount point
974 * and calling vop_mountctl().
976 * Use the mount point from the nch instead of the vnode so nullfs mounts
977 * can properly spike the VOP.
980 kern_mountctl(const char *path, int op, struct file *fp,
981 const void *ctl, int ctllen,
982 void *buf, int buflen, int *res)
986 struct nlookupdata nd;
991 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
993 error = nlookup(&nd);
995 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
996 mp = nd.nl_nch.mount;
1002 * Must be the root of the filesystem
1004 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1008 error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen,
1015 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1017 struct thread *td = curthread;
1018 struct proc *p = td->td_proc;
1021 char *fullpath, *freepath;
1024 if ((error = nlookup(nd)) != 0)
1026 mp = nd->nl_nch.mount;
1028 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1031 error = mount_path(p, mp, &fullpath, &freepath);
1034 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1035 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1036 kfree(freepath, M_TEMP);
1038 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1039 bcopy(sp, buf, sizeof(*buf));
1040 /* Only root should have access to the fsid's. */
1041 if (priv_check(td, PRIV_ROOT))
1042 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1047 * statfs_args(char *path, struct statfs *buf)
1049 * Get filesystem statistics.
1052 sys_statfs(struct statfs_args *uap)
1054 struct nlookupdata nd;
1058 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1060 error = kern_statfs(&nd, &buf);
1063 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1068 kern_fstatfs(int fd, struct statfs *buf)
1070 struct thread *td = curthread;
1071 struct proc *p = td->td_proc;
1075 char *fullpath, *freepath;
1079 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1081 mp = ((struct vnode *)fp->f_data)->v_mount;
1086 if (fp->f_cred == NULL) {
1091 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1094 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1096 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1097 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1098 kfree(freepath, M_TEMP);
1100 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1101 bcopy(sp, buf, sizeof(*buf));
1103 /* Only root should have access to the fsid's. */
1104 if (priv_check(td, PRIV_ROOT))
1105 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1113 * fstatfs_args(int fd, struct statfs *buf)
1115 * Get filesystem statistics.
1118 sys_fstatfs(struct fstatfs_args *uap)
1123 error = kern_fstatfs(uap->fd, &buf);
1126 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1131 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1137 if ((error = nlookup(nd)) != 0)
1139 mp = nd->nl_nch.mount;
1140 sp = &mp->mnt_vstat;
1141 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1145 if (mp->mnt_flag & MNT_RDONLY)
1146 sp->f_flag |= ST_RDONLY;
1147 if (mp->mnt_flag & MNT_NOSUID)
1148 sp->f_flag |= ST_NOSUID;
1149 bcopy(sp, buf, sizeof(*buf));
1154 * statfs_args(char *path, struct statfs *buf)
1156 * Get filesystem statistics.
1159 sys_statvfs(struct statvfs_args *uap)
1161 struct nlookupdata nd;
1165 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1167 error = kern_statvfs(&nd, &buf);
1170 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1175 kern_fstatvfs(int fd, struct statvfs *buf)
1177 struct thread *td = curthread;
1178 struct proc *p = td->td_proc;
1185 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1187 mp = ((struct vnode *)fp->f_data)->v_mount;
1192 if (fp->f_cred == NULL) {
1196 sp = &mp->mnt_vstat;
1197 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1201 if (mp->mnt_flag & MNT_RDONLY)
1202 sp->f_flag |= ST_RDONLY;
1203 if (mp->mnt_flag & MNT_NOSUID)
1204 sp->f_flag |= ST_NOSUID;
1206 bcopy(sp, buf, sizeof(*buf));
1214 * fstatfs_args(int fd, struct statfs *buf)
1216 * Get filesystem statistics.
1219 sys_fstatvfs(struct fstatvfs_args *uap)
1224 error = kern_fstatvfs(uap->fd, &buf);
1227 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1232 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1234 * Get statistics on all filesystems.
1237 struct getfsstat_info {
1238 struct statfs *sfsp;
1246 static int getfsstat_callback(struct mount *, void *);
1250 sys_getfsstat(struct getfsstat_args *uap)
1252 struct thread *td = curthread;
1253 struct proc *p = td->td_proc;
1254 struct getfsstat_info info;
1256 bzero(&info, sizeof(info));
1258 info.maxcount = uap->bufsize / sizeof(struct statfs);
1259 info.sfsp = uap->buf;
1261 info.flags = uap->flags;
1264 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1265 if (info.sfsp && info.count > info.maxcount)
1266 uap->sysmsg_result = info.maxcount;
1268 uap->sysmsg_result = info.count;
1269 return (info.error);
1273 getfsstat_callback(struct mount *mp, void *data)
1275 struct getfsstat_info *info = data;
1281 if (info->sfsp && info->count < info->maxcount) {
1282 if (info->p && !chroot_visible_mnt(mp, info->p))
1287 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1288 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1289 * overrides MNT_WAIT.
1291 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1292 (info->flags & MNT_WAIT)) &&
1293 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1296 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1298 error = mount_path(info->p, mp, &fullpath, &freepath);
1300 info->error = error;
1303 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1304 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1305 kfree(freepath, M_TEMP);
1307 error = copyout(sp, info->sfsp, sizeof(*sp));
1309 info->error = error;
1319 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1320 long bufsize, int flags)
1322 * Get statistics on all filesystems.
1325 struct getvfsstat_info {
1326 struct statfs *sfsp;
1327 struct statvfs *vsfsp;
1335 static int getvfsstat_callback(struct mount *, void *);
1339 sys_getvfsstat(struct getvfsstat_args *uap)
1341 struct thread *td = curthread;
1342 struct proc *p = td->td_proc;
1343 struct getvfsstat_info info;
1345 bzero(&info, sizeof(info));
1347 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1348 info.sfsp = uap->buf;
1349 info.vsfsp = uap->vbuf;
1351 info.flags = uap->flags;
1354 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1355 if (info.vsfsp && info.count > info.maxcount)
1356 uap->sysmsg_result = info.maxcount;
1358 uap->sysmsg_result = info.count;
1359 return (info.error);
1363 getvfsstat_callback(struct mount *mp, void *data)
1365 struct getvfsstat_info *info = data;
1367 struct statvfs *vsp;
1372 if (info->vsfsp && info->count < info->maxcount) {
1373 if (info->p && !chroot_visible_mnt(mp, info->p))
1376 vsp = &mp->mnt_vstat;
1379 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1380 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1381 * overrides MNT_WAIT.
1383 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1384 (info->flags & MNT_WAIT)) &&
1385 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1388 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1390 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1391 (info->flags & MNT_WAIT)) &&
1392 (error = VFS_STATVFS(mp, vsp, info->p->p_ucred))) {
1396 if (mp->mnt_flag & MNT_RDONLY)
1397 vsp->f_flag |= ST_RDONLY;
1398 if (mp->mnt_flag & MNT_NOSUID)
1399 vsp->f_flag |= ST_NOSUID;
1401 error = mount_path(info->p, mp, &fullpath, &freepath);
1403 info->error = error;
1406 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1407 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1408 kfree(freepath, M_TEMP);
1410 error = copyout(sp, info->sfsp, sizeof(*sp));
1412 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1414 info->error = error;
1426 * fchdir_args(int fd)
1428 * Change current working directory to a given file descriptor.
1432 sys_fchdir(struct fchdir_args *uap)
1434 struct thread *td = curthread;
1435 struct proc *p = td->td_proc;
1436 struct filedesc *fdp = p->p_fd;
1437 struct vnode *vp, *ovp;
1440 struct nchandle nch, onch, tnch;
1443 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1445 vp = (struct vnode *)fp->f_data;
1447 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1448 if (vp->v_type != VDIR || fp->f_nchandle.ncp == NULL)
1451 error = VOP_ACCESS(vp, VEXEC, p->p_ucred);
1457 cache_copy(&fp->f_nchandle, &nch);
1460 * If the ncp has become a mount point, traverse through
1464 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1465 (mp = cache_findmount(&nch)) != NULL
1467 error = nlookup_mp(mp, &tnch);
1469 cache_unlock(&tnch); /* leave ref intact */
1471 vp = tnch.ncp->nc_vp;
1472 error = vget(vp, LK_SHARED);
1473 KKASSERT(error == 0);
1480 onch = fdp->fd_ncdir;
1481 vn_unlock(vp); /* leave ref intact */
1483 fdp->fd_ncdir = nch;
1495 kern_chdir(struct nlookupdata *nd)
1497 struct thread *td = curthread;
1498 struct proc *p = td->td_proc;
1499 struct filedesc *fdp = p->p_fd;
1500 struct vnode *vp, *ovp;
1501 struct nchandle onch;
1504 if ((error = nlookup(nd)) != 0)
1506 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1508 if ((error = vget(vp, LK_SHARED)) != 0)
1511 error = checkvp_chdir(vp, td);
1515 onch = fdp->fd_ncdir;
1516 cache_unlock(&nd->nl_nch); /* leave reference intact */
1517 fdp->fd_ncdir = nd->nl_nch;
1521 cache_zero(&nd->nl_nch);
1529 * chdir_args(char *path)
1531 * Change current working directory (``.'').
1534 sys_chdir(struct chdir_args *uap)
1536 struct nlookupdata nd;
1539 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1541 error = kern_chdir(&nd);
1547 * Helper function for raised chroot(2) security function: Refuse if
1548 * any filedescriptors are open directories.
1551 chroot_refuse_vdir_fds(struct filedesc *fdp)
1558 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1559 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1561 vp = (struct vnode *)fp->f_data;
1562 if (vp->v_type != VDIR) {
1573 * This sysctl determines if we will allow a process to chroot(2) if it
1574 * has a directory open:
1575 * 0: disallowed for all processes.
1576 * 1: allowed for processes that were not already chroot(2)'ed.
1577 * 2: allowed for all processes.
1580 static int chroot_allow_open_directories = 1;
1582 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1583 &chroot_allow_open_directories, 0, "");
1586 * chroot to the specified namecache entry. We obtain the vp from the
1587 * namecache data. The passed ncp must be locked and referenced and will
1588 * remain locked and referenced on return.
1591 kern_chroot(struct nchandle *nch)
1593 struct thread *td = curthread;
1594 struct proc *p = td->td_proc;
1595 struct filedesc *fdp = p->p_fd;
1600 * Only privileged user can chroot
1602 error = priv_check_cred(p->p_ucred, PRIV_VFS_CHROOT, 0);
1607 * Disallow open directory descriptors (fchdir() breakouts).
1609 if (chroot_allow_open_directories == 0 ||
1610 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1611 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1614 if ((vp = nch->ncp->nc_vp) == NULL)
1617 if ((error = vget(vp, LK_SHARED)) != 0)
1621 * Check the validity of vp as a directory to change to and
1622 * associate it with rdir/jdir.
1624 error = checkvp_chdir(vp, td);
1625 vn_unlock(vp); /* leave reference intact */
1627 vrele(fdp->fd_rdir);
1628 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1629 cache_drop(&fdp->fd_nrdir);
1630 cache_copy(nch, &fdp->fd_nrdir);
1631 if (fdp->fd_jdir == NULL) {
1634 cache_copy(nch, &fdp->fd_njdir);
1643 * chroot_args(char *path)
1645 * Change notion of root (``/'') directory.
1649 sys_chroot(struct chroot_args *uap)
1651 struct thread *td = curthread;
1652 struct nlookupdata nd;
1655 KKASSERT(td->td_proc);
1656 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1661 nd.nl_flags |= NLC_EXEC;
1662 error = nlookup(&nd);
1664 error = kern_chroot(&nd.nl_nch);
1670 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1671 * determine whether it is legal to chdir to the vnode. The vnode's state
1672 * is not changed by this call.
1675 checkvp_chdir(struct vnode *vp, struct thread *td)
1679 if (vp->v_type != VDIR)
1682 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred);
1687 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1689 struct thread *td = curthread;
1690 struct proc *p = td->td_proc;
1691 struct lwp *lp = td->td_lwp;
1692 struct filedesc *fdp = p->p_fd;
1697 int type, indx, error;
1700 if ((oflags & O_ACCMODE) == O_ACCMODE)
1702 flags = FFLAGS(oflags);
1703 error = falloc(p, &nfp, NULL);
1707 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1710 * XXX p_dupfd is a real mess. It allows a device to return a
1711 * file descriptor to be duplicated rather then doing the open
1717 * Call vn_open() to do the lookup and assign the vnode to the
1718 * file pointer. vn_open() does not change the ref count on fp
1719 * and the vnode, on success, will be inherited by the file pointer
1722 nd->nl_flags |= NLC_LOCKVP;
1723 error = vn_open(nd, fp, flags, cmode);
1727 * handle special fdopen() case. bleh. dupfdopen() is
1728 * responsible for dropping the old contents of ofiles[indx]
1731 * Note that fsetfd() will add a ref to fp which represents
1732 * the fd_files[] assignment. We must still drop our
1735 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1736 if (fdalloc(p, 0, &indx) == 0) {
1737 error = dupfdopen(p, indx, lp->lwp_dupfd, flags, error);
1740 fdrop(fp); /* our ref */
1743 fsetfd(p, NULL, indx);
1746 fdrop(fp); /* our ref */
1747 if (error == ERESTART)
1753 * ref the vnode for ourselves so it can't be ripped out from under
1754 * is. XXX need an ND flag to request that the vnode be returned
1757 * Reserve a file descriptor but do not assign it until the open
1760 vp = (struct vnode *)fp->f_data;
1762 if ((error = fdalloc(p, 0, &indx)) != 0) {
1769 * If no error occurs the vp will have been assigned to the file
1774 if (flags & (O_EXLOCK | O_SHLOCK)) {
1775 lf.l_whence = SEEK_SET;
1778 if (flags & O_EXLOCK)
1779 lf.l_type = F_WRLCK;
1781 lf.l_type = F_RDLCK;
1782 if (flags & FNONBLOCK)
1787 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1789 * lock request failed. Clean up the reserved
1793 fsetfd(p, NULL, indx);
1797 fp->f_flag |= FHASLOCK;
1801 * Assert that all regular file vnodes were created with a object.
1803 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1804 ("open: regular file has no backing object after vn_open"));
1810 * release our private reference, leaving the one associated with the
1811 * descriptor table intact.
1813 fsetfd(p, fp, indx);
1820 * open_args(char *path, int flags, int mode)
1822 * Check permissions, allocate an open file structure,
1823 * and call the device open routine if any.
1826 sys_open(struct open_args *uap)
1828 struct nlookupdata nd;
1831 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1833 error = kern_open(&nd, uap->flags,
1834 uap->mode, &uap->sysmsg_result);
1841 * openat_args(int fd, char *path, int flags, int mode)
1844 sys_openat(struct openat_args *uap)
1846 struct nlookupdata nd;
1850 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
1852 error = kern_open(&nd, uap->flags, uap->mode,
1853 &uap->sysmsg_result);
1855 nlookup_done_at(&nd, fp);
1860 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
1862 struct thread *td = curthread;
1863 struct proc *p = td->td_proc;
1872 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1873 vattr.va_rmajor = rmajor;
1874 vattr.va_rminor = rminor;
1876 switch (mode & S_IFMT) {
1877 case S_IFMT: /* used by badsect to flag bad sectors */
1878 error = priv_check_cred(p->p_ucred, PRIV_VFS_MKNOD_BAD, 0);
1879 vattr.va_type = VBAD;
1882 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1883 vattr.va_type = VCHR;
1886 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1887 vattr.va_type = VBLK;
1890 error = priv_check_cred(p->p_ucred, PRIV_VFS_MKNOD_WHT, 0);
1893 case S_IFDIR: /* special directories support for HAMMER */
1894 error = priv_check_cred(p->p_ucred, PRIV_VFS_MKNOD_DIR, 0);
1895 vattr.va_type = VDIR;
1906 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
1907 if ((error = nlookup(nd)) != 0)
1909 if (nd->nl_nch.ncp->nc_vp)
1911 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1915 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
1916 nd->nl_cred, NAMEI_CREATE);
1919 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
1920 &vp, nd->nl_cred, &vattr);
1928 * mknod_args(char *path, int mode, int dev)
1930 * Create a special file.
1933 sys_mknod(struct mknod_args *uap)
1935 struct nlookupdata nd;
1938 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1940 error = kern_mknod(&nd, uap->mode,
1941 umajor(uap->dev), uminor(uap->dev));
1948 kern_mkfifo(struct nlookupdata *nd, int mode)
1950 struct thread *td = curthread;
1951 struct proc *p = td->td_proc;
1958 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
1959 if ((error = nlookup(nd)) != 0)
1961 if (nd->nl_nch.ncp->nc_vp)
1963 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1967 vattr.va_type = VFIFO;
1968 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1970 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
1977 * mkfifo_args(char *path, int mode)
1979 * Create a named pipe.
1982 sys_mkfifo(struct mkfifo_args *uap)
1984 struct nlookupdata nd;
1987 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1989 error = kern_mkfifo(&nd, uap->mode);
1994 static int hardlink_check_uid = 0;
1995 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1996 &hardlink_check_uid, 0,
1997 "Unprivileged processes cannot create hard links to files owned by other "
1999 static int hardlink_check_gid = 0;
2000 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2001 &hardlink_check_gid, 0,
2002 "Unprivileged processes cannot create hard links to files owned by other "
2006 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2012 * Shortcut if disabled
2014 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2018 * Privileged user can always hardlink
2020 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2024 * Otherwise only if the originating file is owned by the
2025 * same user or group. Note that any group is allowed if
2026 * the file is owned by the caller.
2028 error = VOP_GETATTR(vp, &va);
2032 if (hardlink_check_uid) {
2033 if (cred->cr_uid != va.va_uid)
2037 if (hardlink_check_gid) {
2038 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2046 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2048 struct thread *td = curthread;
2053 * Lookup the source and obtained a locked vnode.
2055 * You may only hardlink a file which you have write permission
2056 * on or which you own.
2058 * XXX relookup on vget failure / race ?
2061 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2062 if ((error = nlookup(nd)) != 0)
2064 vp = nd->nl_nch.ncp->nc_vp;
2065 KKASSERT(vp != NULL);
2066 if (vp->v_type == VDIR)
2067 return (EPERM); /* POSIX */
2068 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2070 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2074 * Unlock the source so we can lookup the target without deadlocking
2075 * (XXX vp is locked already, possible other deadlock?). The target
2078 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2079 nd->nl_flags &= ~NLC_NCPISLOCKED;
2080 cache_unlock(&nd->nl_nch);
2082 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2083 if ((error = nlookup(linknd)) != 0) {
2087 if (linknd->nl_nch.ncp->nc_vp) {
2093 * Finally run the new API VOP.
2095 error = can_hardlink(vp, td, td->td_proc->p_ucred);
2097 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2098 vp, linknd->nl_cred);
2105 * link_args(char *path, char *link)
2107 * Make a hard file link.
2110 sys_link(struct link_args *uap)
2112 struct nlookupdata nd, linknd;
2115 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2117 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2119 error = kern_link(&nd, &linknd);
2120 nlookup_done(&linknd);
2127 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2135 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2136 if ((error = nlookup(nd)) != 0)
2138 if (nd->nl_nch.ncp->nc_vp)
2140 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2144 vattr.va_mode = mode;
2145 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2152 * symlink(char *path, char *link)
2154 * Make a symbolic link.
2157 sys_symlink(struct symlink_args *uap)
2159 struct thread *td = curthread;
2160 struct nlookupdata nd;
2165 path = objcache_get(namei_oc, M_WAITOK);
2166 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2168 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2170 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2171 error = kern_symlink(&nd, path, mode);
2175 objcache_put(namei_oc, path);
2180 * undelete_args(char *path)
2182 * Delete a whiteout from the filesystem.
2186 sys_undelete(struct undelete_args *uap)
2188 struct nlookupdata nd;
2191 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2193 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2195 error = nlookup(&nd);
2197 error = ncp_writechk(&nd.nl_nch);
2199 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2207 kern_unlink(struct nlookupdata *nd)
2212 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2213 if ((error = nlookup(nd)) != 0)
2215 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2217 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2222 * unlink_args(char *path)
2224 * Delete a name from the filesystem.
2227 sys_unlink(struct unlink_args *uap)
2229 struct nlookupdata nd;
2232 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2234 error = kern_unlink(&nd);
2240 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2242 struct thread *td = curthread;
2243 struct proc *p = td->td_proc;
2250 fp = holdfp(p->p_fd, fd, -1);
2253 if (fp->f_type != DTYPE_VNODE) {
2257 vp = (struct vnode *)fp->f_data;
2261 new_offset = fp->f_offset + offset;
2265 error = VOP_GETATTR(vp, &vattr);
2266 new_offset = offset + vattr.va_size;
2269 new_offset = offset;
2279 * Validate the seek position. Negative offsets are not allowed
2280 * for regular files or directories.
2282 * Normally we would also not want to allow negative offsets for
2283 * character and block-special devices. However kvm addresses
2284 * on 64 bit architectures might appear to be negative and must
2288 if (new_offset < 0 &&
2289 (vp->v_type == VREG || vp->v_type == VDIR)) {
2292 fp->f_offset = new_offset;
2295 *res = fp->f_offset;
2302 * lseek_args(int fd, int pad, off_t offset, int whence)
2304 * Reposition read/write file offset.
2307 sys_lseek(struct lseek_args *uap)
2311 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2312 &uap->sysmsg_offset);
2318 kern_access(struct nlookupdata *nd, int aflags)
2323 if ((error = nlookup(nd)) != 0)
2326 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2330 /* Flags == 0 means only check for existence. */
2339 if ((flags & VWRITE) == 0 ||
2340 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2341 error = VOP_ACCESS(vp, flags, nd->nl_cred);
2344 * If the file handle is stale we have to re-resolve the
2345 * entry. This is a hack at the moment.
2347 if (error == ESTALE) {
2349 cache_setunresolved(&nd->nl_nch);
2350 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2363 * access_args(char *path, int flags)
2365 * Check access permissions.
2368 sys_access(struct access_args *uap)
2370 struct nlookupdata nd;
2373 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2375 error = kern_access(&nd, uap->flags);
2381 kern_stat(struct nlookupdata *nd, struct stat *st)
2387 if ((error = nlookup(nd)) != 0)
2390 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2394 if ((error = vget(vp, LK_SHARED)) != 0)
2396 error = vn_stat(vp, st, nd->nl_cred);
2399 * If the file handle is stale we have to re-resolve the entry. This
2400 * is a hack at the moment.
2402 if (error == ESTALE) {
2404 cache_setunresolved(&nd->nl_nch);
2405 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2415 * stat_args(char *path, struct stat *ub)
2417 * Get file status; this version follows links.
2420 sys_stat(struct stat_args *uap)
2422 struct nlookupdata nd;
2426 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2428 error = kern_stat(&nd, &st);
2430 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2437 * lstat_args(char *path, struct stat *ub)
2439 * Get file status; this version does not follow links.
2442 sys_lstat(struct lstat_args *uap)
2444 struct nlookupdata nd;
2448 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2450 error = kern_stat(&nd, &st);
2452 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2459 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2461 * Get status of file pointed to by fd/path.
2464 sys_fstatat(struct fstatat_args *uap)
2466 struct nlookupdata nd;
2472 if (uap->flags & ~_AT_SYMLINK_MASK)
2475 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2477 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2478 UIO_USERSPACE, flags);
2480 error = kern_stat(&nd, &st);
2482 error = copyout(&st, uap->sb, sizeof(*uap->sb));
2484 nlookup_done_at(&nd, fp);
2489 * pathconf_Args(char *path, int name)
2491 * Get configurable pathname variables.
2495 sys_pathconf(struct pathconf_args *uap)
2497 struct nlookupdata nd;
2502 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2504 error = nlookup(&nd);
2506 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2509 error = VOP_PATHCONF(vp, uap->name, &uap->sysmsg_reg);
2517 * kern_readlink isn't properly split yet. There is a copyin burried
2518 * in VOP_READLINK().
2521 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2523 struct thread *td = curthread;
2524 struct proc *p = td->td_proc;
2530 if ((error = nlookup(nd)) != 0)
2532 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2535 if (vp->v_type != VLNK) {
2538 aiov.iov_base = buf;
2539 aiov.iov_len = count;
2540 auio.uio_iov = &aiov;
2541 auio.uio_iovcnt = 1;
2542 auio.uio_offset = 0;
2543 auio.uio_rw = UIO_READ;
2544 auio.uio_segflg = UIO_USERSPACE;
2546 auio.uio_resid = count;
2547 error = VOP_READLINK(vp, &auio, p->p_ucred);
2550 *res = count - auio.uio_resid;
2555 * readlink_args(char *path, char *buf, int count)
2557 * Return target name of a symbolic link.
2560 sys_readlink(struct readlink_args *uap)
2562 struct nlookupdata nd;
2565 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2567 error = kern_readlink(&nd, uap->buf, uap->count,
2568 &uap->sysmsg_result);
2575 setfflags(struct vnode *vp, int flags)
2577 struct thread *td = curthread;
2578 struct proc *p = td->td_proc;
2583 * Prevent non-root users from setting flags on devices. When
2584 * a device is reused, users can retain ownership of the device
2585 * if they are allowed to set flags and programs assume that
2586 * chown can't fail when done as root.
2588 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2589 ((error = priv_check_cred(p->p_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
2593 * note: vget is required for any operation that might mod the vnode
2594 * so VINACTIVE is properly cleared.
2596 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2598 vattr.va_flags = flags;
2599 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2606 * chflags(char *path, int flags)
2608 * Change flags of a file given a path name.
2612 sys_chflags(struct chflags_args *uap)
2614 struct nlookupdata nd;
2619 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2621 error = nlookup(&nd);
2623 error = ncp_writechk(&nd.nl_nch);
2625 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2628 error = setfflags(vp, uap->flags);
2635 * lchflags(char *path, int flags)
2637 * Change flags of a file given a path name, but don't follow symlinks.
2641 sys_lchflags(struct lchflags_args *uap)
2643 struct nlookupdata nd;
2648 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2650 error = nlookup(&nd);
2652 error = ncp_writechk(&nd.nl_nch);
2654 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2657 error = setfflags(vp, uap->flags);
2664 * fchflags_args(int fd, int flags)
2666 * Change flags of a file given a file descriptor.
2670 sys_fchflags(struct fchflags_args *uap)
2672 struct thread *td = curthread;
2673 struct proc *p = td->td_proc;
2677 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2679 if (fp->f_nchandle.ncp)
2680 error = ncp_writechk(&fp->f_nchandle);
2682 error = setfflags((struct vnode *) fp->f_data, uap->flags);
2688 setfmode(struct vnode *vp, int mode)
2690 struct thread *td = curthread;
2691 struct proc *p = td->td_proc;
2696 * note: vget is required for any operation that might mod the vnode
2697 * so VINACTIVE is properly cleared.
2699 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2701 vattr.va_mode = mode & ALLPERMS;
2702 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2709 kern_chmod(struct nlookupdata *nd, int mode)
2714 if ((error = nlookup(nd)) != 0)
2716 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2718 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2719 error = setfmode(vp, mode);
2725 * chmod_args(char *path, int mode)
2727 * Change mode of a file given path name.
2731 sys_chmod(struct chmod_args *uap)
2733 struct nlookupdata nd;
2736 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2738 error = kern_chmod(&nd, uap->mode);
2744 * lchmod_args(char *path, int mode)
2746 * Change mode of a file given path name (don't follow links.)
2750 sys_lchmod(struct lchmod_args *uap)
2752 struct nlookupdata nd;
2755 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2757 error = kern_chmod(&nd, uap->mode);
2763 * fchmod_args(int fd, int mode)
2765 * Change mode of a file given a file descriptor.
2769 sys_fchmod(struct fchmod_args *uap)
2771 struct thread *td = curthread;
2772 struct proc *p = td->td_proc;
2776 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2778 if (fp->f_nchandle.ncp)
2779 error = ncp_writechk(&fp->f_nchandle);
2781 error = setfmode((struct vnode *)fp->f_data, uap->mode);
2787 * fchmodat_args(char *path, int mode)
2789 * Change mode of a file pointed to by fd/path.
2792 sys_fchmodat(struct fchmodat_args *uap)
2794 struct nlookupdata nd;
2799 if (uap->flags & ~_AT_SYMLINK_MASK)
2801 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2803 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2804 UIO_USERSPACE, flags);
2806 error = kern_chmod(&nd, uap->mode);
2807 nlookup_done_at(&nd, fp);
2812 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2814 struct thread *td = curthread;
2815 struct proc *p = td->td_proc;
2820 * note: vget is required for any operation that might mod the vnode
2821 * so VINACTIVE is properly cleared.
2823 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2827 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2834 kern_chown(struct nlookupdata *nd, int uid, int gid)
2839 if ((error = nlookup(nd)) != 0)
2841 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2843 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2844 error = setfown(vp, uid, gid);
2850 * chown(char *path, int uid, int gid)
2852 * Set ownership given a path name.
2855 sys_chown(struct chown_args *uap)
2857 struct nlookupdata nd;
2860 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2862 error = kern_chown(&nd, uap->uid, uap->gid);
2868 * lchown_args(char *path, int uid, int gid)
2870 * Set ownership given a path name, do not cross symlinks.
2873 sys_lchown(struct lchown_args *uap)
2875 struct nlookupdata nd;
2878 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2880 error = kern_chown(&nd, uap->uid, uap->gid);
2886 * fchown_args(int fd, int uid, int gid)
2888 * Set ownership given a file descriptor.
2892 sys_fchown(struct fchown_args *uap)
2894 struct thread *td = curthread;
2895 struct proc *p = td->td_proc;
2899 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2901 if (fp->f_nchandle.ncp)
2902 error = ncp_writechk(&fp->f_nchandle);
2904 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
2910 * fchownat(int fd, char *path, int uid, int gid, int flags)
2912 * Set ownership of file pointed to by fd/path.
2915 sys_fchownat(struct fchownat_args *uap)
2917 struct nlookupdata nd;
2922 if (uap->flags & ~_AT_SYMLINK_MASK)
2924 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2926 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2927 UIO_USERSPACE, flags);
2929 error = kern_chown(&nd, uap->uid, uap->gid);
2930 nlookup_done_at(&nd, fp);
2936 getutimes(const struct timeval *tvp, struct timespec *tsp)
2938 struct timeval tv[2];
2942 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2945 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2946 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2952 setutimes(struct vnode *vp, struct vattr *vattr,
2953 const struct timespec *ts, int nullflag)
2955 struct thread *td = curthread;
2956 struct proc *p = td->td_proc;
2960 vattr->va_atime = ts[0];
2961 vattr->va_mtime = ts[1];
2963 vattr->va_vaflags |= VA_UTIMES_NULL;
2964 error = VOP_SETATTR(vp, vattr, p->p_ucred);
2970 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
2972 struct timespec ts[2];
2977 if ((error = getutimes(tptr, ts)) != 0)
2981 * NOTE: utimes() succeeds for the owner even if the file
2982 * is not user-writable.
2984 nd->nl_flags |= NLC_OWN | NLC_WRITE;
2986 if ((error = nlookup(nd)) != 0)
2988 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2990 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2994 * note: vget is required for any operation that might mod the vnode
2995 * so VINACTIVE is properly cleared.
2997 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
2998 error = vget(vp, LK_EXCLUSIVE);
3000 error = setutimes(vp, &vattr, ts, (tptr == NULL));
3009 * utimes_args(char *path, struct timeval *tptr)
3011 * Set the access and modification times of a file.
3014 sys_utimes(struct utimes_args *uap)
3016 struct timeval tv[2];
3017 struct nlookupdata nd;
3021 error = copyin(uap->tptr, tv, sizeof(tv));
3025 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3027 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3033 * lutimes_args(char *path, struct timeval *tptr)
3035 * Set the access and modification times of a file.
3038 sys_lutimes(struct lutimes_args *uap)
3040 struct timeval tv[2];
3041 struct nlookupdata nd;
3045 error = copyin(uap->tptr, tv, sizeof(tv));
3049 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3051 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3057 * Set utimes on a file descriptor. The creds used to open the
3058 * file are used to determine whether the operation is allowed
3062 kern_futimes(int fd, struct timeval *tptr)
3064 struct thread *td = curthread;
3065 struct proc *p = td->td_proc;
3066 struct timespec ts[2];
3072 error = getutimes(tptr, ts);
3075 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3077 if (fp->f_nchandle.ncp)
3078 error = ncp_writechk(&fp->f_nchandle);
3081 error = vget(vp, LK_EXCLUSIVE);
3083 error = VOP_GETATTR(vp, &vattr);
3085 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3089 error = setutimes(vp, &vattr, ts,
3100 * futimes_args(int fd, struct timeval *tptr)
3102 * Set the access and modification times of a file.
3105 sys_futimes(struct futimes_args *uap)
3107 struct timeval tv[2];
3111 error = copyin(uap->tptr, tv, sizeof(tv));
3116 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3122 kern_truncate(struct nlookupdata *nd, off_t length)
3130 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3131 if ((error = nlookup(nd)) != 0)
3133 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3135 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3137 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
3141 if (vp->v_type == VDIR) {
3143 } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3145 vattr.va_size = length;
3146 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3153 * truncate(char *path, int pad, off_t length)
3155 * Truncate a file given its path name.
3158 sys_truncate(struct truncate_args *uap)
3160 struct nlookupdata nd;
3163 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3165 error = kern_truncate(&nd, uap->length);
3171 kern_ftruncate(int fd, off_t length)
3173 struct thread *td = curthread;
3174 struct proc *p = td->td_proc;
3182 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3184 if (fp->f_nchandle.ncp) {
3185 error = ncp_writechk(&fp->f_nchandle);
3189 if ((fp->f_flag & FWRITE) == 0) {
3193 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3197 vp = (struct vnode *)fp->f_data;
3198 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3199 if (vp->v_type == VDIR) {
3201 } else if ((error = vn_writechk(vp, NULL)) == 0) {
3203 vattr.va_size = length;
3204 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3213 * ftruncate_args(int fd, int pad, off_t length)
3215 * Truncate a file given a file descriptor.
3218 sys_ftruncate(struct ftruncate_args *uap)
3222 error = kern_ftruncate(uap->fd, uap->length);
3230 * Sync an open file.
3234 sys_fsync(struct fsync_args *uap)
3236 struct thread *td = curthread;
3237 struct proc *p = td->td_proc;
3243 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3245 vp = (struct vnode *)fp->f_data;
3246 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3247 if ((obj = vp->v_object) != NULL)
3248 vm_object_page_clean(obj, 0, 0, 0);
3249 if ((error = VOP_FSYNC(vp, MNT_WAIT)) == 0 && vp->v_mount)
3250 error = buf_fsync(vp);
3257 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3259 struct nchandle fnchd;
3260 struct nchandle tnchd;
3261 struct namecache *ncp;
3268 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3269 if ((error = nlookup(fromnd)) != 0)
3271 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3273 fnchd.mount = fromnd->nl_nch.mount;
3277 * unlock the source nch so we can lookup the target nch without
3278 * deadlocking. The target may or may not exist so we do not check
3279 * for a target vp like kern_mkdir() and other creation functions do.
3281 * The source and target directories are ref'd and rechecked after
3282 * everything is relocked to determine if the source or target file
3285 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3286 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3287 cache_unlock(&fromnd->nl_nch);
3289 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3290 if ((error = nlookup(tond)) != 0) {
3294 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3298 tnchd.mount = tond->nl_nch.mount;
3302 * If the source and target are the same there is nothing to do
3304 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3311 * Mount points cannot be renamed or overwritten
3313 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3322 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
3323 * may have become invalid while it was unlocked, nc_vp and nc_mount
3326 if (cache_lock_nonblock(&fromnd->nl_nch) == 0) {
3327 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3328 } else if (fromnd->nl_nch.ncp > tond->nl_nch.ncp) {
3329 cache_lock(&fromnd->nl_nch);
3330 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3332 cache_unlock(&tond->nl_nch);
3333 cache_lock(&fromnd->nl_nch);
3334 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3335 cache_lock(&tond->nl_nch);
3336 cache_resolve(&tond->nl_nch, tond->nl_cred);
3338 fromnd->nl_flags |= NLC_NCPISLOCKED;
3341 * make sure the parent directories linkages are the same
3343 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3344 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3351 * Both the source and target must be within the same filesystem and
3352 * in the same filesystem as their parent directories within the
3353 * namecache topology.
3355 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3358 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3359 mp != tond->nl_nch.mount) {
3366 * Make sure the mount point is writable
3368 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3375 * If the target exists and either the source or target is a directory,
3376 * then both must be directories.
3378 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3381 if (tond->nl_nch.ncp->nc_vp) {
3382 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3384 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3385 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3387 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3393 * You cannot rename a source into itself or a subdirectory of itself.
3394 * We check this by travsersing the target directory upwards looking
3395 * for a match against the source.
3398 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3399 if (fromnd->nl_nch.ncp == ncp) {
3410 * Even though the namespaces are different, they may still represent
3411 * hardlinks to the same file. The filesystem might have a hard time
3412 * with this so we issue a NREMOVE of the source instead of a NRENAME
3413 * when we detect the situation.
3416 fdvp = fromnd->nl_dvp;
3417 tdvp = tond->nl_dvp;
3418 if (fdvp == NULL || tdvp == NULL) {
3420 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3421 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3424 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3425 fdvp, tdvp, tond->nl_cred);
3432 * rename_args(char *from, char *to)
3434 * Rename files. Source and destination must either both be directories,
3435 * or both not be directories. If target is a directory, it must be empty.
3438 sys_rename(struct rename_args *uap)
3440 struct nlookupdata fromnd, tond;
3443 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3445 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3447 error = kern_rename(&fromnd, &tond);
3448 nlookup_done(&tond);
3450 nlookup_done(&fromnd);
3455 kern_mkdir(struct nlookupdata *nd, int mode)
3457 struct thread *td = curthread;
3458 struct proc *p = td->td_proc;
3464 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3465 if ((error = nlookup(nd)) != 0)
3468 if (nd->nl_nch.ncp->nc_vp)
3470 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3473 vattr.va_type = VDIR;
3474 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3477 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, p->p_ucred, &vattr);
3484 * mkdir_args(char *path, int mode)
3486 * Make a directory file.
3490 sys_mkdir(struct mkdir_args *uap)
3492 struct nlookupdata nd;
3495 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3497 error = kern_mkdir(&nd, uap->mode);
3503 kern_rmdir(struct nlookupdata *nd)
3508 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
3509 if ((error = nlookup(nd)) != 0)
3513 * Do not allow directories representing mount points to be
3514 * deleted, even if empty. Check write perms on mount point
3515 * in case the vnode is aliased (aka nullfs).
3517 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
3519 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3521 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
3526 * rmdir_args(char *path)
3528 * Remove a directory file.
3532 sys_rmdir(struct rmdir_args *uap)
3534 struct nlookupdata nd;
3537 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3539 error = kern_rmdir(&nd);
3545 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
3546 enum uio_seg direction)
3548 struct thread *td = curthread;
3549 struct proc *p = td->td_proc;
3557 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3559 if ((fp->f_flag & FREAD) == 0) {
3563 vp = (struct vnode *)fp->f_data;
3565 if (vp->v_type != VDIR) {
3569 aiov.iov_base = buf;
3570 aiov.iov_len = count;
3571 auio.uio_iov = &aiov;
3572 auio.uio_iovcnt = 1;
3573 auio.uio_rw = UIO_READ;
3574 auio.uio_segflg = direction;
3576 auio.uio_resid = count;
3577 loff = auio.uio_offset = fp->f_offset;
3578 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3579 fp->f_offset = auio.uio_offset;
3582 if (count == auio.uio_resid) {
3583 if (union_dircheckp) {
3584 error = union_dircheckp(td, &vp, fp);
3591 if ((vp->v_flag & VROOT) &&
3592 (vp->v_mount->mnt_flag & MNT_UNION)) {
3593 struct vnode *tvp = vp;
3594 vp = vp->v_mount->mnt_vnodecovered;
3605 * WARNING! *basep may not be wide enough to accomodate the
3606 * seek offset. XXX should we hack this to return the upper 32 bits
3607 * for offsets greater then 4G?
3610 *basep = (long)loff;
3612 *res = count - auio.uio_resid;
3619 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
3621 * Read a block of directory entries in a file system independent format.
3624 sys_getdirentries(struct getdirentries_args *uap)
3629 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
3630 &uap->sysmsg_result, UIO_USERSPACE);
3632 if (error == 0 && uap->basep)
3633 error = copyout(&base, uap->basep, sizeof(*uap->basep));
3638 * getdents_args(int fd, char *buf, size_t count)
3641 sys_getdents(struct getdents_args *uap)
3645 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
3646 &uap->sysmsg_result, UIO_USERSPACE);
3652 * umask(int newmask)
3654 * Set the mode mask for creation of filesystem nodes.
3659 sys_umask(struct umask_args *uap)
3661 struct thread *td = curthread;
3662 struct proc *p = td->td_proc;
3663 struct filedesc *fdp;
3666 uap->sysmsg_result = fdp->fd_cmask;
3667 fdp->fd_cmask = uap->newmask & ALLPERMS;
3672 * revoke(char *path)
3674 * Void all references to file by ripping underlying filesystem
3679 sys_revoke(struct revoke_args *uap)
3681 struct nlookupdata nd;
3688 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3690 error = nlookup(&nd);
3692 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3693 cred = crhold(nd.nl_cred);
3697 error = VOP_GETATTR(vp, &vattr);
3698 if (error == 0 && cred->cr_uid != vattr.va_uid)
3699 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
3700 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
3702 error = vrevoke(vp, cred);
3703 } else if (error == 0) {
3704 error = vrevoke(vp, cred);
3714 * getfh_args(char *fname, fhandle_t *fhp)
3716 * Get (NFS) file handle
3718 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
3719 * mount. This allows nullfs mounts to be explicitly exported.
3721 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
3723 * nullfs mounts of subdirectories are not safe. That is, it will
3724 * work, but you do not really have protection against access to
3725 * the related parent directories.
3728 sys_getfh(struct getfh_args *uap)
3730 struct thread *td = curthread;
3731 struct nlookupdata nd;
3738 * Must be super user
3740 if ((error = priv_check(td, PRIV_ROOT)) != 0)
3744 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3746 error = nlookup(&nd);
3748 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3749 mp = nd.nl_nch.mount;
3752 bzero(&fh, sizeof(fh));
3753 fh.fh_fsid = mp->mnt_stat.f_fsid;
3754 error = VFS_VPTOFH(vp, &fh.fh_fid);
3757 error = copyout(&fh, uap->fhp, sizeof(fh));
3763 * fhopen_args(const struct fhandle *u_fhp, int flags)
3765 * syscall for the rpc.lockd to use to translate a NFS file handle into
3766 * an open descriptor.
3768 * warning: do not remove the priv_check() call or this becomes one giant
3772 sys_fhopen(struct fhopen_args *uap)
3774 struct thread *td = curthread;
3775 struct proc *p = td->td_proc;
3780 struct vattr *vap = &vat;
3782 int fmode, mode, error, type;
3788 * Must be super user
3790 error = priv_check(td, PRIV_ROOT);
3794 fmode = FFLAGS(uap->flags);
3795 /* why not allow a non-read/write open for our lockd? */
3796 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3798 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3801 /* find the mount point */
3802 mp = vfs_getvfs(&fhp.fh_fsid);
3805 /* now give me my vnode, it gets returned to me locked */
3806 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
3810 * from now on we have to make sure not
3811 * to forget about the vnode
3812 * any error that causes an abort must vput(vp)
3813 * just set error = err and 'goto bad;'.
3819 if (vp->v_type == VLNK) {
3823 if (vp->v_type == VSOCK) {
3828 if (fmode & (FWRITE | O_TRUNC)) {
3829 if (vp->v_type == VDIR) {
3833 error = vn_writechk(vp, NULL);
3841 error = VOP_ACCESS(vp, mode, p->p_ucred);
3845 if (fmode & O_TRUNC) {
3846 vn_unlock(vp); /* XXX */
3847 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
3850 error = VOP_SETATTR(vp, vap, p->p_ucred);
3856 * VOP_OPEN needs the file pointer so it can potentially override
3859 * WARNING! no f_nchandle will be associated when fhopen()ing a
3862 if ((error = falloc(p, &nfp, &indx)) != 0)
3866 error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
3869 * setting f_ops this way prevents VOP_CLOSE from being
3870 * called or fdrop() releasing the vp from v_data. Since
3871 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3873 fp->f_ops = &badfileops;
3879 * The fp is given its own reference, we still have our ref and lock.
3881 * Assert that all regular files must be created with a VM object.
3883 if (vp->v_type == VREG && vp->v_object == NULL) {
3884 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
3889 * The open was successful. Handle any locking requirements.
3891 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3892 lf.l_whence = SEEK_SET;
3895 if (fmode & O_EXLOCK)
3896 lf.l_type = F_WRLCK;
3898 lf.l_type = F_RDLCK;
3899 if (fmode & FNONBLOCK)
3904 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3906 * release our private reference.
3908 fsetfd(p, NULL, indx);
3913 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3914 fp->f_flag |= FHASLOCK;
3918 * Clean up. Associate the file pointer with the previously
3919 * reserved descriptor and return it.
3922 fsetfd(p, fp, indx);
3924 uap->sysmsg_result = indx;
3928 fsetfd(p, NULL, indx);
3936 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3939 sys_fhstat(struct fhstat_args *uap)
3941 struct thread *td = curthread;
3949 * Must be super user
3951 error = priv_check(td, PRIV_ROOT);
3955 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
3959 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3961 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
3963 error = vn_stat(vp, &sb, td->td_proc->p_ucred);
3967 error = copyout(&sb, uap->sb, sizeof(sb));
3972 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3975 sys_fhstatfs(struct fhstatfs_args *uap)
3977 struct thread *td = curthread;
3978 struct proc *p = td->td_proc;
3983 char *fullpath, *freepath;
3988 * Must be super user
3990 if ((error = priv_check(td, PRIV_ROOT)))
3993 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3996 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3999 if (p != NULL && !chroot_visible_mnt(mp, p))
4002 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4007 if ((error = VFS_STATFS(mp, sp, p->p_ucred)) != 0)
4010 error = mount_path(p, mp, &fullpath, &freepath);
4013 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4014 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4015 kfree(freepath, M_TEMP);
4017 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4018 if (priv_check(td, PRIV_ROOT)) {
4019 bcopy(sp, &sb, sizeof(sb));
4020 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4023 return (copyout(sp, uap->buf, sizeof(*sp)));
4027 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4030 sys_fhstatvfs(struct fhstatvfs_args *uap)
4032 struct thread *td = curthread;
4033 struct proc *p = td->td_proc;
4041 * Must be super user
4043 if ((error = priv_check(td, PRIV_ROOT)))
4046 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4049 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4052 if (p != NULL && !chroot_visible_mnt(mp, p))
4055 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4058 sp = &mp->mnt_vstat;
4060 if ((error = VFS_STATVFS(mp, sp, p->p_ucred)) != 0)
4064 if (mp->mnt_flag & MNT_RDONLY)
4065 sp->f_flag |= ST_RDONLY;
4066 if (mp->mnt_flag & MNT_NOSUID)
4067 sp->f_flag |= ST_NOSUID;
4069 return (copyout(sp, uap->buf, sizeof(*sp)));
4074 * Syscall to push extended attribute configuration information into the
4075 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4076 * a command (int cmd), and attribute name and misc data. For now, the
4077 * attribute name is left in userspace for consumption by the VFS_op.
4078 * It will probably be changed to be copied into sysspace by the
4079 * syscall in the future, once issues with various consumers of the
4080 * attribute code have raised their hands.
4082 * Currently this is used only by UFS Extended Attributes.
4085 sys_extattrctl(struct extattrctl_args *uap)
4087 struct nlookupdata nd;
4093 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4095 error = nlookup(&nd);
4097 mp = nd.nl_nch.mount;
4098 error = VFS_EXTATTRCTL(mp, uap->cmd,
4099 uap->attrname, uap->arg,
4107 * Syscall to set a named extended attribute on a file or directory.
4108 * Accepts attribute name, and a uio structure pointing to the data to set.
4109 * The uio is consumed in the style of writev(). The real work happens
4110 * in VOP_SETEXTATTR().
4113 sys_extattr_set_file(struct extattr_set_file_args *uap)
4115 char attrname[EXTATTR_MAXNAMELEN];
4116 struct iovec aiov[UIO_SMALLIOV];
4117 struct iovec *needfree;
4118 struct nlookupdata nd;
4127 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4132 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4134 error = nlookup(&nd);
4136 error = ncp_writechk(&nd.nl_nch);
4138 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4145 iovlen = uap->iovcnt * sizeof(struct iovec);
4146 if (uap->iovcnt > UIO_SMALLIOV) {
4147 if (uap->iovcnt > UIO_MAXIOV) {
4151 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
4157 auio.uio_iovcnt = uap->iovcnt;
4158 auio.uio_rw = UIO_WRITE;
4159 auio.uio_segflg = UIO_USERSPACE;
4160 auio.uio_td = nd.nl_td;
4161 auio.uio_offset = 0;
4162 if ((error = copyin(uap->iovp, iov, iovlen)))
4165 for (i = 0; i < uap->iovcnt; i++) {
4166 if (iov->iov_len > INT_MAX - auio.uio_resid) {
4170 auio.uio_resid += iov->iov_len;
4173 cnt = auio.uio_resid;
4174 error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred);
4175 cnt -= auio.uio_resid;
4176 uap->sysmsg_result = cnt;
4181 FREE(needfree, M_IOV);
4186 * Syscall to get a named extended attribute on a file or directory.
4187 * Accepts attribute name, and a uio structure pointing to a buffer for the
4188 * data. The uio is consumed in the style of readv(). The real work
4189 * happens in VOP_GETEXTATTR();
4192 sys_extattr_get_file(struct extattr_get_file_args *uap)
4194 char attrname[EXTATTR_MAXNAMELEN];
4195 struct iovec aiov[UIO_SMALLIOV];
4196 struct iovec *needfree;
4197 struct nlookupdata nd;
4206 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4211 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4213 error = nlookup(&nd);
4215 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4221 iovlen = uap->iovcnt * sizeof (struct iovec);
4223 if (uap->iovcnt > UIO_SMALLIOV) {
4224 if (uap->iovcnt > UIO_MAXIOV) {
4228 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
4234 auio.uio_iovcnt = uap->iovcnt;
4235 auio.uio_rw = UIO_READ;
4236 auio.uio_segflg = UIO_USERSPACE;
4237 auio.uio_td = nd.nl_td;
4238 auio.uio_offset = 0;
4239 if ((error = copyin(uap->iovp, iov, iovlen)))
4242 for (i = 0; i < uap->iovcnt; i++) {
4243 if (iov->iov_len > INT_MAX - auio.uio_resid) {
4247 auio.uio_resid += iov->iov_len;
4250 cnt = auio.uio_resid;
4251 error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred);
4252 cnt -= auio.uio_resid;
4253 uap->sysmsg_result = cnt;
4258 FREE(needfree, M_IOV);
4263 * Syscall to delete a named extended attribute from a file or directory.
4264 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4267 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4269 char attrname[EXTATTR_MAXNAMELEN];
4270 struct nlookupdata nd;
4274 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4279 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4281 error = nlookup(&nd);
4283 error = ncp_writechk(&nd.nl_nch);
4285 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4291 error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred);
4298 * Determine if the mount is visible to the process.
4301 chroot_visible_mnt(struct mount *mp, struct proc *p)
4303 struct nchandle nch;
4306 * Traverse from the mount point upwards. If we hit the process
4307 * root then the mount point is visible to the process.
4309 nch = mp->mnt_ncmountpt;
4311 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4312 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4315 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4316 nch = nch.mount->mnt_ncmounton;
4318 nch.ncp = nch.ncp->nc_parent;
4323 * If the mount point is not visible to the process, but the
4324 * process root is in a subdirectory of the mount, return
4327 if (p->p_fd->fd_nrdir.mount == mp)