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.104 2006/09/18 18:19:33 dillon Exp $
43 #include <sys/param.h>
44 #include <sys/systm.h>
47 #include <sys/sysent.h>
48 #include <sys/malloc.h>
49 #include <sys/mount.h>
50 #include <sys/mountctl.h>
51 #include <sys/sysproto.h>
52 #include <sys/filedesc.h>
53 #include <sys/kernel.h>
54 #include <sys/fcntl.h>
56 #include <sys/linker.h>
58 #include <sys/unistd.h>
59 #include <sys/vnode.h>
61 #include <sys/namei.h>
62 #include <sys/nlookup.h>
63 #include <sys/dirent.h>
64 #include <sys/extattr.h>
65 #include <sys/spinlock.h>
66 #include <sys/kern_syscall.h>
67 #include <sys/objcache.h>
69 #include <machine/limits.h>
70 #include <vfs/union/union.h>
71 #include <sys/sysctl.h>
73 #include <vm/vm_object.h>
74 #include <vm/vm_page.h>
76 #include <sys/file2.h>
77 #include <sys/spinlock2.h>
79 static int checkvp_chdir (struct vnode *vn, struct thread *td);
80 static void checkdirs (struct vnode *olddp, struct namecache *ncp);
81 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
82 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
83 static int getutimes (const struct timeval *, struct timespec *);
84 static int setfown (struct vnode *, uid_t, gid_t);
85 static int setfmode (struct vnode *, int);
86 static int setfflags (struct vnode *, int);
87 static int setutimes (struct vnode *, const struct timespec *, int);
88 static int usermount = 0; /* if 1, non-root can mount fs. */
90 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
92 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
95 * Virtual File System System Calls
99 * Mount a file system.
102 * mount_args(char *type, char *path, int flags, caddr_t data)
106 sys_mount(struct mount_args *uap)
108 struct thread *td = curthread;
109 struct proc *p = td->td_proc;
111 struct namecache *ncp;
113 struct vfsconf *vfsp;
114 int error, flag = 0, flag2 = 0;
116 struct nlookupdata nd;
117 char fstypename[MFSNAMELEN];
118 struct nlcomponent nlc;
119 struct ucred *cred = p->p_ucred;
122 if (cred->cr_prison != NULL)
124 if (usermount == 0 && (error = suser(td)))
127 * Do not allow NFS export by non-root users.
129 if (uap->flags & MNT_EXPORTED) {
135 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
138 uap->flags |= MNT_NOSUID | MNT_NODEV;
141 * Lookup the requested path and extract the ncp and vnode.
143 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
145 if ((error = nlookup(&nd)) == 0) {
146 if (nd.nl_ncp->nc_vp == NULL)
156 * Extract the locked+refd ncp and cleanup the nd structure
163 * now we have the locked ref'd ncp and unreferenced vnode.
166 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
173 * Now we have an unlocked ref'd ncp and a locked ref'd vp
175 if (uap->flags & MNT_UPDATE) {
176 if ((vp->v_flag & VROOT) == 0) {
183 flag2 = mp->mnt_kern_flag;
185 * We only allow the filesystem to be reloaded if it
186 * is currently mounted read-only.
188 if ((uap->flags & MNT_RELOAD) &&
189 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
192 return (EOPNOTSUPP); /* Needs translation */
195 * Only root, or the user that did the original mount is
196 * permitted to update it.
198 if (mp->mnt_stat.f_owner != cred->cr_uid &&
199 (error = suser(td))) {
204 if (vfs_busy(mp, LK_NOWAIT)) {
209 if ((vp->v_flag & VMOUNT) != 0 ||
210 vp->v_mountedhere != NULL) {
216 vp->v_flag |= VMOUNT;
218 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
223 * If the user is not root, ensure that they own the directory
224 * onto which we are attempting to mount.
226 if ((error = VOP_GETATTR(vp, &va)) ||
227 (va.va_uid != cred->cr_uid && (error = suser(td)))) {
232 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
237 if (vp->v_type != VDIR) {
242 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
247 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
248 if (!strcmp(vfsp->vfc_name, fstypename))
254 /* Only load modules for root (very important!) */
255 if ((error = suser(td)) != 0) {
260 error = linker_load_file(fstypename, &lf);
261 if (error || lf == NULL) {
269 /* lookup again, see if the VFS was loaded */
270 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
271 if (!strcmp(vfsp->vfc_name, fstypename))
276 linker_file_unload(lf);
282 if ((vp->v_flag & VMOUNT) != 0 ||
283 vp->v_mountedhere != NULL) {
288 vp->v_flag |= VMOUNT;
291 * Allocate and initialize the filesystem.
293 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
294 TAILQ_INIT(&mp->mnt_nvnodelist);
295 TAILQ_INIT(&mp->mnt_reservedvnlist);
296 TAILQ_INIT(&mp->mnt_jlist);
297 mp->mnt_nvnodelistsize = 0;
298 lockinit(&mp->mnt_lock, "vfslock", 0, 0);
299 vfs_busy(mp, LK_NOWAIT);
300 mp->mnt_op = vfsp->vfc_vfsops;
302 vfsp->vfc_refcount++;
303 mp->mnt_stat.f_type = vfsp->vfc_typenum;
304 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
305 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
306 mp->mnt_vnodecovered = vp;
307 mp->mnt_stat.f_owner = cred->cr_uid;
308 mp->mnt_iosize_max = DFLTPHYS;
312 * Set the mount level flags.
314 if (uap->flags & MNT_RDONLY)
315 mp->mnt_flag |= MNT_RDONLY;
316 else if (mp->mnt_flag & MNT_RDONLY)
317 mp->mnt_kern_flag |= MNTK_WANTRDWR;
318 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
319 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
320 MNT_NOSYMFOLLOW | MNT_IGNORE |
321 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
322 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
323 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
324 MNT_NOSYMFOLLOW | MNT_IGNORE |
325 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
327 * Mount the filesystem.
328 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
331 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
332 if (mp->mnt_flag & MNT_UPDATE) {
333 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
334 mp->mnt_flag &= ~MNT_RDONLY;
335 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
336 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
339 mp->mnt_kern_flag = flag2;
342 vp->v_flag &= ~VMOUNT;
347 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
349 * Put the new filesystem on the mount list after root. The mount
350 * point gets its own mnt_ncp which is a special ncp linking the
351 * vnode-under to the root of the new mount. The lookup code
352 * detects the mount point going forward and detects the special
353 * mnt_ncp via NCP_MOUNTPT going backwards.
355 * It is not necessary to invalidate or purge the vnode underneath
356 * because elements under the mount will be given their own glue
360 nlc.nlc_nameptr = "";
362 mp->mnt_ncp = cache_nlookup(ncp, &nlc);
363 cache_setunresolved(mp->mnt_ncp);
364 mp->mnt_ncp->nc_flag |= NCF_MOUNTPT;
365 mp->mnt_ncp->nc_mount = mp;
367 /* XXX get the root of the fs and cache_setvp(mnt_ncp...) */
368 vp->v_flag &= ~VMOUNT;
369 vp->v_mountedhere = mp;
370 mountlist_insert(mp, MNTINS_LAST);
371 checkdirs(vp, mp->mnt_ncp);
372 cache_unlock(mp->mnt_ncp); /* leave ref intact */
374 error = vfs_allocate_syncvnode(mp);
376 if ((error = VFS_START(mp, 0)) != 0)
379 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
380 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
381 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
382 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
383 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
384 vp->v_flag &= ~VMOUNT;
385 mp->mnt_vfc->vfc_refcount--;
395 * Scan all active processes to see if any of them have a current
396 * or root directory onto which the new filesystem has just been
397 * mounted. If so, replace them with the new mount point.
399 * The passed ncp is ref'd and locked (from the mount code) and
400 * must be associated with the vnode representing the root of the
403 struct checkdirs_info {
406 struct namecache *ncp;
409 static int checkdirs_callback(struct proc *p, void *data);
412 checkdirs(struct vnode *olddp, struct namecache *ncp)
414 struct checkdirs_info info;
418 if (olddp->v_usecount == 1)
420 mp = olddp->v_mountedhere;
421 if (VFS_ROOT(mp, &newdp))
422 panic("mount: lost mount");
423 cache_setvp(ncp, newdp);
425 if (rootvnode == olddp) {
427 vfs_cache_setroot(newdp, cache_hold(ncp));
433 allproc_scan(checkdirs_callback, &info);
438 * NOTE: callback is not MP safe because the scanned process's filedesc
439 * structure can be ripped out from under us, amoung other things.
442 checkdirs_callback(struct proc *p, void *data)
444 struct checkdirs_info *info = data;
445 struct filedesc *fdp;
446 struct namecache *ncdrop1;
447 struct namecache *ncdrop2;
448 struct vnode *vprele1;
449 struct vnode *vprele2;
451 if ((fdp = p->p_fd) != NULL) {
458 * MPUNSAFE - XXX fdp can be pulled out from under a
461 * A shared filedesc is ok, we don't have to copy it
462 * because we are making this change globally.
464 spin_lock_wr(&fdp->fd_spin);
465 if (fdp->fd_cdir == info->olddp) {
466 vprele1 = fdp->fd_cdir;
468 fdp->fd_cdir = info->newdp;
469 ncdrop1 = fdp->fd_ncdir;
470 fdp->fd_ncdir = cache_hold(info->ncp);
472 if (fdp->fd_rdir == info->olddp) {
473 vprele2 = fdp->fd_rdir;
475 fdp->fd_rdir = info->newdp;
476 ncdrop2 = fdp->fd_nrdir;
477 fdp->fd_nrdir = cache_hold(info->ncp);
479 spin_unlock_wr(&fdp->fd_spin);
493 * Unmount a file system.
495 * Note: unmount takes a path to the vnode mounted on as argument,
496 * not special file (as before).
499 * umount_args(char *path, int flags)
503 sys_unmount(struct unmount_args *uap)
505 struct thread *td = curthread;
506 struct proc *p = td->td_proc;
507 struct mount *mp = NULL;
509 struct nlookupdata nd;
512 if (p->p_ucred->cr_prison != NULL)
514 if (usermount == 0 && (error = suser(td)))
517 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
519 error = nlookup(&nd);
523 mp = nd.nl_ncp->nc_mount;
526 * Only root, or the user that did the original mount is
527 * permitted to unmount this filesystem.
529 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
534 * Don't allow unmounting the root file system.
536 if (mp->mnt_flag & MNT_ROOTFS) {
542 * Must be the root of the filesystem
544 if (! (nd.nl_ncp->nc_flag & NCF_MOUNTPT)) {
553 return (dounmount(mp, uap->flags));
557 * Do the actual file system unmount.
560 dounmount_interlock(struct mount *mp)
562 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
564 mp->mnt_kern_flag |= MNTK_UNMOUNT;
569 dounmount(struct mount *mp, int flags)
571 struct vnode *coveredvp;
577 * Exclusive access for unmounting purposes
579 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
583 * Allow filesystems to detect that a forced unmount is in progress.
585 if (flags & MNT_FORCE)
586 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
587 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_NOWAIT);
588 error = lockmgr(&mp->mnt_lock, lflags);
590 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
591 if (mp->mnt_kern_flag & MNTK_MWAIT)
596 if (mp->mnt_flag & MNT_EXPUBLIC)
597 vfs_setpublicfs(NULL, NULL, NULL);
599 vfs_msync(mp, MNT_WAIT);
600 async_flag = mp->mnt_flag & MNT_ASYNC;
601 mp->mnt_flag &=~ MNT_ASYNC;
602 cache_purgevfs(mp); /* remove cache entries for this file sys */
603 if (mp->mnt_syncer != NULL)
604 vrele(mp->mnt_syncer);
605 if (((mp->mnt_flag & MNT_RDONLY) ||
606 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
608 error = VFS_UNMOUNT(mp, flags);
610 if (mp->mnt_syncer == NULL)
611 vfs_allocate_syncvnode(mp);
612 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
613 mp->mnt_flag |= async_flag;
614 lockmgr(&mp->mnt_lock, LK_RELEASE);
615 if (mp->mnt_kern_flag & MNTK_MWAIT)
620 * Clean up any journals still associated with the mount after
621 * filesystem activity has ceased.
623 journal_remove_all_journals(mp,
624 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
626 mountlist_remove(mp);
629 * Remove any installed vnode ops here so the individual VFSs don't
632 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
633 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
634 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
635 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
636 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
638 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
639 coveredvp->v_mountedhere = NULL;
641 cache_drop(mp->mnt_ncp);
644 mp->mnt_vfc->vfc_refcount--;
645 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
646 panic("unmount: dangling vnode");
647 lockmgr(&mp->mnt_lock, LK_RELEASE);
648 if (mp->mnt_kern_flag & MNTK_MWAIT)
655 * Sync each mounted filesystem.
659 static int syncprt = 0;
660 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
663 static int sync_callback(struct mount *mp, void *data);
667 sys_sync(struct sync_args *uap)
669 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
672 * print out buffer pool stat information on each sync() call.
682 sync_callback(struct mount *mp, void *data __unused)
686 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
687 asyncflag = mp->mnt_flag & MNT_ASYNC;
688 mp->mnt_flag &= ~MNT_ASYNC;
689 vfs_msync(mp, MNT_NOWAIT);
690 VFS_SYNC(mp, MNT_NOWAIT);
691 mp->mnt_flag |= asyncflag;
696 /* XXX PRISON: could be per prison flag */
697 static int prison_quotas;
699 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
703 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
705 * Change filesystem quotas.
709 sys_quotactl(struct quotactl_args *uap)
711 struct nlookupdata nd;
719 if (p->p_ucred->cr_prison && !prison_quotas)
722 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
724 error = nlookup(&nd);
726 mp = nd.nl_ncp->nc_mount;
727 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
728 uap->arg, nd.nl_cred);
735 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
736 * void *buf, int buflen)
738 * This function operates on a mount point and executes the specified
739 * operation using the specified control data, and possibly returns data.
741 * The actual number of bytes stored in the result buffer is returned, 0
742 * if none, otherwise an error is returned.
746 sys_mountctl(struct mountctl_args *uap)
748 struct thread *td = curthread;
749 struct proc *p = td->td_proc;
757 * Sanity and permissions checks. We must be root.
760 if (p->p_ucred->cr_prison != NULL)
762 if ((error = suser(td)) != 0)
766 * Argument length checks
768 if (uap->ctllen < 0 || uap->ctllen > 1024)
770 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
772 if (uap->path == NULL)
776 * Allocate the necessary buffers and copyin data
778 path = objcache_get(namei_oc, M_WAITOK);
779 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
784 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
785 error = copyin(uap->ctl, ctl, uap->ctllen);
790 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
793 * Validate the descriptor
795 fp = holdfp(p->p_fd, uap->fd, -1);
802 * Execute the internal kernel function and clean up.
804 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
807 if (error == 0 && uap->sysmsg_result > 0)
808 error = copyout(buf, uap->buf, uap->sysmsg_result);
811 objcache_put(namei_oc, path);
820 * Execute a mount control operation by resolving the path to a mount point
821 * and calling vop_mountctl().
824 kern_mountctl(const char *path, int op, struct file *fp,
825 const void *ctl, int ctllen,
826 void *buf, int buflen, int *res)
830 struct nlookupdata nd;
835 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
837 error = nlookup(&nd);
839 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
847 * Must be the root of the filesystem
849 if ((vp->v_flag & VROOT) == 0) {
853 error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen,
860 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
862 struct thread *td = curthread;
863 struct proc *p = td->td_proc;
866 char *fullpath, *freepath;
869 if ((error = nlookup(nd)) != 0)
871 mp = nd->nl_ncp->nc_mount;
873 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
876 error = cache_fullpath(p, mp->mnt_ncp, &fullpath, &freepath);
879 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
880 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
881 kfree(freepath, M_TEMP);
883 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
884 bcopy(sp, buf, sizeof(*buf));
885 /* Only root should have access to the fsid's. */
887 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
892 * statfs_args(char *path, struct statfs *buf)
894 * Get filesystem statistics.
897 sys_statfs(struct statfs_args *uap)
899 struct nlookupdata nd;
903 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
905 error = kern_statfs(&nd, &buf);
908 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
913 kern_fstatfs(int fd, struct statfs *buf)
915 struct thread *td = curthread;
916 struct proc *p = td->td_proc;
920 char *fullpath, *freepath;
924 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
926 mp = ((struct vnode *)fp->f_data)->v_mount;
931 if (fp->f_cred == NULL) {
936 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
939 if ((error = cache_fullpath(p, mp->mnt_ncp, &fullpath, &freepath)) != 0)
941 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
942 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
943 kfree(freepath, M_TEMP);
945 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
946 bcopy(sp, buf, sizeof(*buf));
948 /* Only root should have access to the fsid's. */
950 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
958 * fstatfs_args(int fd, struct statfs *buf)
960 * Get filesystem statistics.
963 sys_fstatfs(struct fstatfs_args *uap)
968 error = kern_fstatfs(uap->fd, &buf);
971 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
976 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
978 * Get statistics on all filesystems.
981 struct getfsstat_info {
991 static int getfsstat_callback(struct mount *, void *);
995 sys_getfsstat(struct getfsstat_args *uap)
997 struct thread *td = curthread;
998 struct proc *p = td->td_proc;
999 struct getfsstat_info info;
1001 bzero(&info, sizeof(info));
1002 if (p != NULL && (p->p_fd->fd_nrdir->nc_flag & NCF_ROOT) == 0)
1003 info.is_chrooted = 1;
1005 info.is_chrooted = 0;
1007 info.maxcount = uap->bufsize / sizeof(struct statfs);
1008 info.sfsp = uap->buf;
1010 info.flags = uap->flags;
1013 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1014 if (info.sfsp && info.count > info.maxcount)
1015 uap->sysmsg_result = info.maxcount;
1017 uap->sysmsg_result = info.count;
1018 return (info.error);
1022 getfsstat_callback(struct mount *mp, void *data)
1024 struct getfsstat_info *info = data;
1030 if (info->sfsp && info->count < info->maxcount) {
1031 if (info->is_chrooted && !chroot_visible_mnt(mp, info->p))
1036 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1037 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1038 * overrides MNT_WAIT.
1040 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1041 (info->flags & MNT_WAIT)) &&
1042 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1045 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1047 error = cache_fullpath(info->p, mp->mnt_ncp,
1048 &fullpath, &freepath);
1050 info->error = error;
1053 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1054 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1055 kfree(freepath, M_TEMP);
1057 error = copyout(sp, info->sfsp, sizeof(*sp));
1059 info->error = error;
1069 * fchdir_args(int fd)
1071 * Change current working directory to a given file descriptor.
1075 sys_fchdir(struct fchdir_args *uap)
1077 struct thread *td = curthread;
1078 struct proc *p = td->td_proc;
1079 struct filedesc *fdp = p->p_fd;
1080 struct vnode *vp, *ovp;
1083 struct namecache *ncp, *oncp;
1084 struct namecache *nct;
1087 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1089 vp = (struct vnode *)fp->f_data;
1091 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1092 if (vp->v_type != VDIR || fp->f_ncp == NULL)
1095 error = VOP_ACCESS(vp, VEXEC, p->p_ucred);
1101 ncp = cache_hold(fp->f_ncp);
1102 while (!error && (mp = vp->v_mountedhere) != NULL) {
1103 error = nlookup_mp(mp, &nct);
1105 cache_unlock(nct); /* leave ref intact */
1108 error = vget(vp, LK_SHARED);
1109 KKASSERT(error == 0);
1116 oncp = fdp->fd_ncdir;
1117 vn_unlock(vp); /* leave ref intact */
1119 fdp->fd_ncdir = ncp;
1131 kern_chdir(struct nlookupdata *nd)
1133 struct thread *td = curthread;
1134 struct proc *p = td->td_proc;
1135 struct filedesc *fdp = p->p_fd;
1136 struct vnode *vp, *ovp;
1137 struct namecache *oncp;
1140 if ((error = nlookup(nd)) != 0)
1142 if ((vp = nd->nl_ncp->nc_vp) == NULL)
1144 if ((error = vget(vp, LK_SHARED)) != 0)
1147 error = checkvp_chdir(vp, td);
1151 oncp = fdp->fd_ncdir;
1152 cache_unlock(nd->nl_ncp); /* leave reference intact */
1153 fdp->fd_ncdir = nd->nl_ncp;
1165 * chdir_args(char *path)
1167 * Change current working directory (``.'').
1170 sys_chdir(struct chdir_args *uap)
1172 struct nlookupdata nd;
1175 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1177 error = kern_chdir(&nd);
1183 * Helper function for raised chroot(2) security function: Refuse if
1184 * any filedescriptors are open directories.
1187 chroot_refuse_vdir_fds(fdp)
1188 struct filedesc *fdp;
1195 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1196 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1198 vp = (struct vnode *)fp->f_data;
1199 if (vp->v_type != VDIR) {
1210 * This sysctl determines if we will allow a process to chroot(2) if it
1211 * has a directory open:
1212 * 0: disallowed for all processes.
1213 * 1: allowed for processes that were not already chroot(2)'ed.
1214 * 2: allowed for all processes.
1217 static int chroot_allow_open_directories = 1;
1219 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1220 &chroot_allow_open_directories, 0, "");
1223 * chroot to the specified namecache entry. We obtain the vp from the
1224 * namecache data. The passed ncp must be locked and referenced and will
1225 * remain locked and referenced on return.
1228 kern_chroot(struct namecache *ncp)
1230 struct thread *td = curthread;
1231 struct proc *p = td->td_proc;
1232 struct filedesc *fdp = p->p_fd;
1237 * Only root can chroot
1239 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
1243 * Disallow open directory descriptors (fchdir() breakouts).
1245 if (chroot_allow_open_directories == 0 ||
1246 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1247 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1250 if ((vp = ncp->nc_vp) == NULL)
1253 if ((error = vget(vp, LK_SHARED)) != 0)
1257 * Check the validity of vp as a directory to change to and
1258 * associate it with rdir/jdir.
1260 error = checkvp_chdir(vp, td);
1261 vn_unlock(vp); /* leave reference intact */
1263 vrele(fdp->fd_rdir);
1264 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1265 cache_drop(fdp->fd_nrdir);
1266 fdp->fd_nrdir = cache_hold(ncp);
1267 if (fdp->fd_jdir == NULL) {
1270 fdp->fd_njdir = cache_hold(ncp);
1279 * chroot_args(char *path)
1281 * Change notion of root (``/'') directory.
1285 sys_chroot(struct chroot_args *uap)
1287 struct thread *td = curthread;
1288 struct nlookupdata nd;
1291 KKASSERT(td->td_proc);
1292 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1297 error = nlookup(&nd);
1299 error = kern_chroot(nd.nl_ncp);
1305 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1306 * determine whether it is legal to chdir to the vnode. The vnode's state
1307 * is not changed by this call.
1310 checkvp_chdir(struct vnode *vp, struct thread *td)
1314 if (vp->v_type != VDIR)
1317 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred);
1322 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1324 struct thread *td = curthread;
1325 struct proc *p = td->td_proc;
1326 struct lwp *lp = td->td_lwp;
1327 struct filedesc *fdp = p->p_fd;
1332 int type, indx, error;
1335 if ((oflags & O_ACCMODE) == O_ACCMODE)
1337 flags = FFLAGS(oflags);
1338 error = falloc(p, &nfp, NULL);
1342 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1345 * XXX p_dupfd is a real mess. It allows a device to return a
1346 * file descriptor to be duplicated rather then doing the open
1352 * Call vn_open() to do the lookup and assign the vnode to the
1353 * file pointer. vn_open() does not change the ref count on fp
1354 * and the vnode, on success, will be inherited by the file pointer
1357 nd->nl_flags |= NLC_LOCKVP;
1358 error = vn_open(nd, fp, flags, cmode);
1362 * handle special fdopen() case. bleh. dupfdopen() is
1363 * responsible for dropping the old contents of ofiles[indx]
1366 * Note that fsetfd() will add a ref to fp which represents
1367 * the fd_files[] assignment. We must still drop our
1370 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1371 if (fdalloc(p, 0, &indx) == 0) {
1372 error = dupfdopen(p, indx, lp->lwp_dupfd, flags, error);
1375 fdrop(fp); /* our ref */
1378 fsetfd(p, NULL, indx);
1381 fdrop(fp); /* our ref */
1382 if (error == ERESTART)
1388 * ref the vnode for ourselves so it can't be ripped out from under
1389 * is. XXX need an ND flag to request that the vnode be returned
1392 * Reserve a file descriptor but do not assign it until the open
1395 vp = (struct vnode *)fp->f_data;
1397 if ((error = fdalloc(p, 0, &indx)) != 0) {
1404 * If no error occurs the vp will have been assigned to the file
1409 if (flags & (O_EXLOCK | O_SHLOCK)) {
1410 lf.l_whence = SEEK_SET;
1413 if (flags & O_EXLOCK)
1414 lf.l_type = F_WRLCK;
1416 lf.l_type = F_RDLCK;
1417 if (flags & FNONBLOCK)
1422 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1424 * lock request failed. Clean up the reserved
1428 fsetfd(p, NULL, indx);
1432 fp->f_flag |= FHASLOCK;
1436 * Assert that all regular file vnodes were created with a object.
1438 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1439 ("open: regular file has no backing object after vn_open"));
1445 * release our private reference, leaving the one associated with the
1446 * descriptor table intact.
1448 fsetfd(p, fp, indx);
1455 * open_args(char *path, int flags, int mode)
1457 * Check permissions, allocate an open file structure,
1458 * and call the device open routine if any.
1461 sys_open(struct open_args *uap)
1463 struct nlookupdata nd;
1466 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1468 error = kern_open(&nd, uap->flags,
1469 uap->mode, &uap->sysmsg_result);
1476 kern_mknod(struct nlookupdata *nd, int mode, int dev)
1478 struct namecache *ncp;
1479 struct thread *td = curthread;
1480 struct proc *p = td->td_proc;
1488 switch (mode & S_IFMT) {
1494 error = suser_cred(p->p_ucred, PRISON_ROOT);
1501 nd->nl_flags |= NLC_CREATE;
1502 if ((error = nlookup(nd)) != 0)
1507 if ((error = ncp_writechk(ncp)) != 0)
1511 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1512 vattr.va_rdev = dev;
1515 switch (mode & S_IFMT) {
1516 case S_IFMT: /* used by badsect to flag bad sectors */
1517 vattr.va_type = VBAD;
1520 vattr.va_type = VCHR;
1523 vattr.va_type = VBLK;
1534 error = VOP_NWHITEOUT(ncp, nd->nl_cred, NAMEI_CREATE);
1537 error = VOP_NMKNOD(ncp, &vp, nd->nl_cred, &vattr);
1546 * mknod_args(char *path, int mode, int dev)
1548 * Create a special file.
1551 sys_mknod(struct mknod_args *uap)
1553 struct nlookupdata nd;
1556 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1558 error = kern_mknod(&nd, uap->mode, uap->dev);
1564 kern_mkfifo(struct nlookupdata *nd, int mode)
1566 struct namecache *ncp;
1567 struct thread *td = curthread;
1568 struct proc *p = td->td_proc;
1575 nd->nl_flags |= NLC_CREATE;
1576 if ((error = nlookup(nd)) != 0)
1581 if ((error = ncp_writechk(ncp)) != 0)
1585 vattr.va_type = VFIFO;
1586 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1588 error = VOP_NMKNOD(ncp, &vp, nd->nl_cred, &vattr);
1595 * mkfifo_args(char *path, int mode)
1597 * Create a named pipe.
1600 sys_mkfifo(struct mkfifo_args *uap)
1602 struct nlookupdata nd;
1605 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1607 error = kern_mkfifo(&nd, uap->mode);
1612 static int hardlink_check_uid = 0;
1613 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1614 &hardlink_check_uid, 0,
1615 "Unprivileged processes cannot create hard links to files owned by other "
1617 static int hardlink_check_gid = 0;
1618 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1619 &hardlink_check_gid, 0,
1620 "Unprivileged processes cannot create hard links to files owned by other "
1624 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1630 * Shortcut if disabled
1632 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
1636 * root cred can always hardlink
1638 if (suser_cred(cred, PRISON_ROOT) == 0)
1642 * Otherwise only if the originating file is owned by the
1643 * same user or group. Note that any group is allowed if
1644 * the file is owned by the caller.
1646 error = VOP_GETATTR(vp, &va);
1650 if (hardlink_check_uid) {
1651 if (cred->cr_uid != va.va_uid)
1655 if (hardlink_check_gid) {
1656 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
1664 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
1666 struct thread *td = curthread;
1671 * Lookup the source and obtained a locked vnode.
1673 * XXX relookup on vget failure / race ?
1676 if ((error = nlookup(nd)) != 0)
1678 vp = nd->nl_ncp->nc_vp;
1679 KKASSERT(vp != NULL);
1680 if (vp->v_type == VDIR)
1681 return (EPERM); /* POSIX */
1682 if ((error = ncp_writechk(nd->nl_ncp)) != 0)
1684 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
1688 * Unlock the source so we can lookup the target without deadlocking
1689 * (XXX vp is locked already, possible other deadlock?). The target
1692 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
1693 nd->nl_flags &= ~NLC_NCPISLOCKED;
1694 cache_unlock(nd->nl_ncp);
1696 linknd->nl_flags |= NLC_CREATE;
1697 if ((error = nlookup(linknd)) != 0) {
1701 if (linknd->nl_ncp->nc_vp) {
1707 * Finally run the new API VOP.
1709 error = can_hardlink(vp, td, td->td_proc->p_ucred);
1711 error = VOP_NLINK(linknd->nl_ncp, vp, linknd->nl_cred);
1717 * link_args(char *path, char *link)
1719 * Make a hard file link.
1722 sys_link(struct link_args *uap)
1724 struct nlookupdata nd, linknd;
1727 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1729 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
1731 error = kern_link(&nd, &linknd);
1732 nlookup_done(&linknd);
1739 kern_symlink(struct nlookupdata *nd, char *path, int mode)
1741 struct namecache *ncp;
1747 nd->nl_flags |= NLC_CREATE;
1748 if ((error = nlookup(nd)) != 0)
1753 if ((error = ncp_writechk(ncp)) != 0)
1756 vattr.va_mode = mode;
1757 error = VOP_NSYMLINK(ncp, &vp, nd->nl_cred, &vattr, path);
1764 * symlink(char *path, char *link)
1766 * Make a symbolic link.
1769 sys_symlink(struct symlink_args *uap)
1771 struct thread *td = curthread;
1772 struct nlookupdata nd;
1777 path = objcache_get(namei_oc, M_WAITOK);
1778 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1780 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
1782 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
1783 error = kern_symlink(&nd, path, mode);
1787 objcache_put(namei_oc, path);
1792 * undelete_args(char *path)
1794 * Delete a whiteout from the filesystem.
1798 sys_undelete(struct undelete_args *uap)
1800 struct nlookupdata nd;
1803 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1805 nd.nl_flags |= NLC_DELETE;
1807 error = nlookup(&nd);
1809 error = ncp_writechk(nd.nl_ncp);
1811 error = VOP_NWHITEOUT(nd.nl_ncp, nd.nl_cred, NAMEI_DELETE);
1817 kern_unlink(struct nlookupdata *nd)
1819 struct namecache *ncp;
1823 nd->nl_flags |= NLC_DELETE;
1824 if ((error = nlookup(nd)) != 0)
1827 if ((error = ncp_writechk(ncp)) != 0)
1829 error = VOP_NREMOVE(ncp, nd->nl_cred);
1834 * unlink_args(char *path)
1836 * Delete a name from the filesystem.
1839 sys_unlink(struct unlink_args *uap)
1841 struct nlookupdata nd;
1844 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1846 error = kern_unlink(&nd);
1852 kern_lseek(int fd, off_t offset, int whence, off_t *res)
1854 struct thread *td = curthread;
1855 struct proc *p = td->td_proc;
1860 fp = holdfp(p->p_fd, fd, -1);
1863 if (fp->f_type != DTYPE_VNODE) {
1870 fp->f_offset += offset;
1874 error = VOP_GETATTR((struct vnode *)fp->f_data, &vattr);
1876 fp->f_offset = offset + vattr.va_size;
1879 fp->f_offset = offset;
1886 *res = fp->f_offset;
1893 * lseek_args(int fd, int pad, off_t offset, int whence)
1895 * Reposition read/write file offset.
1898 sys_lseek(struct lseek_args *uap)
1902 error = kern_lseek(uap->fd, uap->offset, uap->whence,
1903 &uap->sysmsg_offset);
1909 kern_access(struct nlookupdata *nd, int aflags)
1914 if ((error = nlookup(nd)) != 0)
1917 error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
1921 /* Flags == 0 means only check for existence. */
1930 if ((flags & VWRITE) == 0 ||
1931 (error = vn_writechk(vp, nd->nl_ncp)) == 0)
1932 error = VOP_ACCESS(vp, flags, nd->nl_cred);
1935 * If the file handle is stale we have to re-resolve the
1936 * entry. This is a hack at the moment.
1938 if (error == ESTALE) {
1939 cache_setunresolved(nd->nl_ncp);
1940 error = cache_resolve(nd->nl_ncp, nd->nl_cred);
1953 * access_args(char *path, int flags)
1955 * Check access permissions.
1958 sys_access(struct access_args *uap)
1960 struct nlookupdata nd;
1963 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1965 error = kern_access(&nd, uap->flags);
1971 kern_stat(struct nlookupdata *nd, struct stat *st)
1977 if ((error = nlookup(nd)) != 0)
1980 if ((vp = nd->nl_ncp->nc_vp) == NULL)
1984 if ((error = vget(vp, LK_SHARED)) != 0)
1986 error = vn_stat(vp, st, nd->nl_cred);
1989 * If the file handle is stale we have to re-resolve the entry. This
1990 * is a hack at the moment.
1992 if (error == ESTALE) {
1993 cache_setunresolved(nd->nl_ncp);
1994 error = cache_resolve(nd->nl_ncp, nd->nl_cred);
2005 * stat_args(char *path, struct stat *ub)
2007 * Get file status; this version follows links.
2010 sys_stat(struct stat_args *uap)
2012 struct nlookupdata nd;
2016 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2018 error = kern_stat(&nd, &st);
2020 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2027 * lstat_args(char *path, struct stat *ub)
2029 * Get file status; this version does not follow links.
2032 sys_lstat(struct lstat_args *uap)
2034 struct nlookupdata nd;
2038 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2040 error = kern_stat(&nd, &st);
2042 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2049 * pathconf_Args(char *path, int name)
2051 * Get configurable pathname variables.
2055 sys_pathconf(struct pathconf_args *uap)
2057 struct nlookupdata nd;
2062 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2064 error = nlookup(&nd);
2066 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
2069 error = VOP_PATHCONF(vp, uap->name, uap->sysmsg_fds);
2077 * kern_readlink isn't properly split yet. There is a copyin burried
2078 * in VOP_READLINK().
2081 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2083 struct thread *td = curthread;
2084 struct proc *p = td->td_proc;
2090 if ((error = nlookup(nd)) != 0)
2092 error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
2095 if (vp->v_type != VLNK) {
2098 aiov.iov_base = buf;
2099 aiov.iov_len = count;
2100 auio.uio_iov = &aiov;
2101 auio.uio_iovcnt = 1;
2102 auio.uio_offset = 0;
2103 auio.uio_rw = UIO_READ;
2104 auio.uio_segflg = UIO_USERSPACE;
2106 auio.uio_resid = count;
2107 error = VOP_READLINK(vp, &auio, p->p_ucred);
2110 *res = count - auio.uio_resid;
2115 * readlink_args(char *path, char *buf, int count)
2117 * Return target name of a symbolic link.
2120 sys_readlink(struct readlink_args *uap)
2122 struct nlookupdata nd;
2125 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2127 error = kern_readlink(&nd, uap->buf, uap->count,
2128 &uap->sysmsg_result);
2135 setfflags(struct vnode *vp, int flags)
2137 struct thread *td = curthread;
2138 struct proc *p = td->td_proc;
2143 * Prevent non-root users from setting flags on devices. When
2144 * a device is reused, users can retain ownership of the device
2145 * if they are allowed to set flags and programs assume that
2146 * chown can't fail when done as root.
2148 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2149 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
2153 * note: vget is required for any operation that might mod the vnode
2154 * so VINACTIVE is properly cleared.
2156 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2158 vattr.va_flags = flags;
2159 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2166 * chflags(char *path, int flags)
2168 * Change flags of a file given a path name.
2172 sys_chflags(struct chflags_args *uap)
2174 struct nlookupdata nd;
2179 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2180 /* XXX Add NLC flag indicating modifying operation? */
2182 error = nlookup(&nd);
2184 error = ncp_writechk(nd.nl_ncp);
2186 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
2189 error = setfflags(vp, uap->flags);
2196 * fchflags_args(int fd, int flags)
2198 * Change flags of a file given a file descriptor.
2202 sys_fchflags(struct fchflags_args *uap)
2204 struct thread *td = curthread;
2205 struct proc *p = td->td_proc;
2209 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2212 error = ncp_writechk(fp->f_ncp);
2214 error = setfflags((struct vnode *) fp->f_data, uap->flags);
2220 setfmode(struct vnode *vp, int mode)
2222 struct thread *td = curthread;
2223 struct proc *p = td->td_proc;
2228 * note: vget is required for any operation that might mod the vnode
2229 * so VINACTIVE is properly cleared.
2231 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2233 vattr.va_mode = mode & ALLPERMS;
2234 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2241 kern_chmod(struct nlookupdata *nd, int mode)
2246 /* XXX Add NLC flag indicating modifying operation? */
2247 if ((error = nlookup(nd)) != 0)
2249 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2251 if ((error = ncp_writechk(nd->nl_ncp)) == 0)
2252 error = setfmode(vp, mode);
2258 * chmod_args(char *path, int mode)
2260 * Change mode of a file given path name.
2264 sys_chmod(struct chmod_args *uap)
2266 struct nlookupdata nd;
2269 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2271 error = kern_chmod(&nd, uap->mode);
2277 * lchmod_args(char *path, int mode)
2279 * Change mode of a file given path name (don't follow links.)
2283 sys_lchmod(struct lchmod_args *uap)
2285 struct nlookupdata nd;
2288 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2290 error = kern_chmod(&nd, uap->mode);
2296 * fchmod_args(int fd, int mode)
2298 * Change mode of a file given a file descriptor.
2302 sys_fchmod(struct fchmod_args *uap)
2304 struct thread *td = curthread;
2305 struct proc *p = td->td_proc;
2309 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2312 error = ncp_writechk(fp->f_ncp);
2314 error = setfmode((struct vnode *)fp->f_data, uap->mode);
2320 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2322 struct thread *td = curthread;
2323 struct proc *p = td->td_proc;
2328 * note: vget is required for any operation that might mod the vnode
2329 * so VINACTIVE is properly cleared.
2331 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2335 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2342 kern_chown(struct nlookupdata *nd, int uid, int gid)
2347 /* XXX Add NLC flag indicating modifying operation? */
2348 if ((error = nlookup(nd)) != 0)
2350 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2352 if ((error = ncp_writechk(nd->nl_ncp)) == 0)
2353 error = setfown(vp, uid, gid);
2359 * chown(char *path, int uid, int gid)
2361 * Set ownership given a path name.
2364 sys_chown(struct chown_args *uap)
2366 struct nlookupdata nd;
2369 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2371 error = kern_chown(&nd, uap->uid, uap->gid);
2377 * lchown_args(char *path, int uid, int gid)
2379 * Set ownership given a path name, do not cross symlinks.
2382 sys_lchown(struct lchown_args *uap)
2384 struct nlookupdata nd;
2387 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2389 error = kern_chown(&nd, uap->uid, uap->gid);
2395 * fchown_args(int fd, int uid, int gid)
2397 * Set ownership given a file descriptor.
2401 sys_fchown(struct fchown_args *uap)
2403 struct thread *td = curthread;
2404 struct proc *p = td->td_proc;
2408 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2411 error = ncp_writechk(fp->f_ncp);
2413 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
2419 getutimes(const struct timeval *tvp, struct timespec *tsp)
2421 struct timeval tv[2];
2425 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2428 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2429 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2435 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2437 struct thread *td = curthread;
2438 struct proc *p = td->td_proc;
2443 * note: vget is required for any operation that might mod the vnode
2444 * so VINACTIVE is properly cleared.
2446 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2448 vattr.va_atime = ts[0];
2449 vattr.va_mtime = ts[1];
2451 vattr.va_vaflags |= VA_UTIMES_NULL;
2452 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2459 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
2461 struct timespec ts[2];
2465 if ((error = getutimes(tptr, ts)) != 0)
2467 /* XXX Add NLC flag indicating modifying operation? */
2468 if ((error = nlookup(nd)) != 0)
2470 if ((error = ncp_writechk(nd->nl_ncp)) != 0)
2472 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2474 error = setutimes(vp, ts, tptr == NULL);
2480 * utimes_args(char *path, struct timeval *tptr)
2482 * Set the access and modification times of a file.
2485 sys_utimes(struct utimes_args *uap)
2487 struct timeval tv[2];
2488 struct nlookupdata nd;
2492 error = copyin(uap->tptr, tv, sizeof(tv));
2496 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2498 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2504 * lutimes_args(char *path, struct timeval *tptr)
2506 * Set the access and modification times of a file.
2509 sys_lutimes(struct lutimes_args *uap)
2511 struct timeval tv[2];
2512 struct nlookupdata nd;
2516 error = copyin(uap->tptr, tv, sizeof(tv));
2520 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2522 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2528 kern_futimes(int fd, struct timeval *tptr)
2530 struct thread *td = curthread;
2531 struct proc *p = td->td_proc;
2532 struct timespec ts[2];
2536 error = getutimes(tptr, ts);
2539 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
2542 error = ncp_writechk(fp->f_ncp);
2544 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2550 * futimes_args(int fd, struct timeval *tptr)
2552 * Set the access and modification times of a file.
2555 sys_futimes(struct futimes_args *uap)
2557 struct timeval tv[2];
2561 error = copyin(uap->tptr, tv, sizeof(tv));
2566 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2572 kern_truncate(struct nlookupdata *nd, off_t length)
2580 /* XXX Add NLC flag indicating modifying operation? */
2581 if ((error = nlookup(nd)) != 0)
2583 if ((error = ncp_writechk(nd->nl_ncp)) != 0)
2585 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2587 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2591 if (vp->v_type == VDIR) {
2593 } else if ((error = vn_writechk(vp, nd->nl_ncp)) == 0 &&
2594 (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred)) == 0) {
2596 vattr.va_size = length;
2597 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
2604 * truncate(char *path, int pad, off_t length)
2606 * Truncate a file given its path name.
2609 sys_truncate(struct truncate_args *uap)
2611 struct nlookupdata nd;
2614 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2616 error = kern_truncate(&nd, uap->length);
2622 kern_ftruncate(int fd, off_t length)
2624 struct thread *td = curthread;
2625 struct proc *p = td->td_proc;
2633 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
2636 error = ncp_writechk(fp->f_ncp);
2640 if ((fp->f_flag & FWRITE) == 0) {
2644 vp = (struct vnode *)fp->f_data;
2645 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2646 if (vp->v_type == VDIR) {
2648 } else if ((error = vn_writechk(vp, NULL)) == 0) {
2650 vattr.va_size = length;
2651 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
2660 * ftruncate_args(int fd, int pad, off_t length)
2662 * Truncate a file given a file descriptor.
2665 sys_ftruncate(struct ftruncate_args *uap)
2669 error = kern_ftruncate(uap->fd, uap->length);
2677 * Sync an open file.
2681 sys_fsync(struct fsync_args *uap)
2683 struct thread *td = curthread;
2684 struct proc *p = td->td_proc;
2690 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2692 vp = (struct vnode *)fp->f_data;
2693 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2694 if ((obj = vp->v_object) != NULL)
2695 vm_object_page_clean(obj, 0, 0, 0);
2696 if ((error = VOP_FSYNC(vp, MNT_WAIT)) == 0 &&
2697 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2699 error = (*bioops.io_fsync)(vp);
2707 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
2709 struct namecache *fncpd;
2710 struct namecache *tncpd;
2711 struct namecache *ncp;
2716 if ((error = nlookup(fromnd)) != 0)
2718 if ((fncpd = fromnd->nl_ncp->nc_parent) == NULL)
2723 * unlock the source ncp so we can lookup the target ncp without
2724 * deadlocking. The target may or may not exist so we do not check
2725 * for a target vp like kern_mkdir() and other creation functions do.
2727 * The source and target directories are ref'd and rechecked after
2728 * everything is relocked to determine if the source or target file
2731 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
2732 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
2733 cache_unlock(fromnd->nl_ncp);
2735 tond->nl_flags |= NLC_CREATE;
2736 if ((error = nlookup(tond)) != 0) {
2740 if ((tncpd = tond->nl_ncp->nc_parent) == NULL) {
2747 * If the source and target are the same there is nothing to do
2749 if (fromnd->nl_ncp == tond->nl_ncp) {
2756 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
2757 * may have become invalid while it was unlocked, nc_vp and nc_mount
2760 if (cache_lock_nonblock(fromnd->nl_ncp) == 0) {
2761 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2762 } else if (fromnd->nl_ncp > tond->nl_ncp) {
2763 cache_lock(fromnd->nl_ncp);
2764 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2766 cache_unlock(tond->nl_ncp);
2767 cache_lock(fromnd->nl_ncp);
2768 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2769 cache_lock(tond->nl_ncp);
2770 cache_resolve(tond->nl_ncp, tond->nl_cred);
2772 fromnd->nl_flags |= NLC_NCPISLOCKED;
2775 * make sure the parent directories linkages are the same
2777 if (fncpd != fromnd->nl_ncp->nc_parent ||
2778 tncpd != tond->nl_ncp->nc_parent) {
2785 * Both the source and target must be within the same filesystem and
2786 * in the same filesystem as their parent directories within the
2787 * namecache topology.
2789 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
2791 mp = fncpd->nc_mount;
2792 if (mp != tncpd->nc_mount || mp != fromnd->nl_ncp->nc_mount ||
2793 mp != tond->nl_ncp->nc_mount) {
2800 * Make sure the mount point is writable
2802 if ((error = ncp_writechk(tond->nl_ncp)) != 0) {
2809 * If the target exists and either the source or target is a directory,
2810 * then both must be directories.
2812 * Due to relocking of the source, fromnd->nl_ncp->nc_vp might have
2815 if (tond->nl_ncp->nc_vp) {
2816 if (fromnd->nl_ncp->nc_vp == NULL) {
2818 } else if (fromnd->nl_ncp->nc_vp->v_type == VDIR) {
2819 if (tond->nl_ncp->nc_vp->v_type != VDIR)
2821 } else if (tond->nl_ncp->nc_vp->v_type == VDIR) {
2827 * You cannot rename a source into itself or a subdirectory of itself.
2828 * We check this by travsersing the target directory upwards looking
2829 * for a match against the source.
2832 for (ncp = tncpd; ncp; ncp = ncp->nc_parent) {
2833 if (fromnd->nl_ncp == ncp) {
2844 * Even though the namespaces are different, they may still represent
2845 * hardlinks to the same file. The filesystem might have a hard time
2846 * with this so we issue a NREMOVE of the source instead of a NRENAME
2847 * when we detect the situation.
2850 if (fromnd->nl_ncp->nc_vp == tond->nl_ncp->nc_vp) {
2851 error = VOP_NREMOVE(fromnd->nl_ncp, fromnd->nl_cred);
2853 error = VOP_NRENAME(fromnd->nl_ncp, tond->nl_ncp,
2861 * rename_args(char *from, char *to)
2863 * Rename files. Source and destination must either both be directories,
2864 * or both not be directories. If target is a directory, it must be empty.
2867 sys_rename(struct rename_args *uap)
2869 struct nlookupdata fromnd, tond;
2872 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
2874 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
2876 error = kern_rename(&fromnd, &tond);
2877 nlookup_done(&tond);
2879 nlookup_done(&fromnd);
2884 kern_mkdir(struct nlookupdata *nd, int mode)
2886 struct thread *td = curthread;
2887 struct proc *p = td->td_proc;
2888 struct namecache *ncp;
2894 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE;
2895 if ((error = nlookup(nd)) != 0)
2901 if ((error = ncp_writechk(ncp)) != 0)
2905 vattr.va_type = VDIR;
2906 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2909 error = VOP_NMKDIR(ncp, &vp, p->p_ucred, &vattr);
2916 * mkdir_args(char *path, int mode)
2918 * Make a directory file.
2922 sys_mkdir(struct mkdir_args *uap)
2924 struct nlookupdata nd;
2927 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2929 error = kern_mkdir(&nd, uap->mode);
2935 kern_rmdir(struct nlookupdata *nd)
2937 struct namecache *ncp;
2941 nd->nl_flags |= NLC_DELETE;
2942 if ((error = nlookup(nd)) != 0)
2944 if ((error = ncp_writechk(nd->nl_ncp)) != 0)
2948 error = VOP_NRMDIR(ncp, nd->nl_cred);
2953 * rmdir_args(char *path)
2955 * Remove a directory file.
2959 sys_rmdir(struct rmdir_args *uap)
2961 struct nlookupdata nd;
2964 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2966 error = kern_rmdir(&nd);
2972 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
2973 enum uio_seg direction)
2975 struct thread *td = curthread;
2976 struct proc *p = td->td_proc;
2984 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
2986 if ((fp->f_flag & FREAD) == 0) {
2990 vp = (struct vnode *)fp->f_data;
2992 if (vp->v_type != VDIR) {
2996 aiov.iov_base = buf;
2997 aiov.iov_len = count;
2998 auio.uio_iov = &aiov;
2999 auio.uio_iovcnt = 1;
3000 auio.uio_rw = UIO_READ;
3001 auio.uio_segflg = direction;
3003 auio.uio_resid = count;
3004 loff = auio.uio_offset = fp->f_offset;
3005 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3006 fp->f_offset = auio.uio_offset;
3009 if (count == auio.uio_resid) {
3010 if (union_dircheckp) {
3011 error = union_dircheckp(td, &vp, fp);
3017 if ((vp->v_flag & VROOT) &&
3018 (vp->v_mount->mnt_flag & MNT_UNION)) {
3019 struct vnode *tvp = vp;
3020 vp = vp->v_mount->mnt_vnodecovered;
3031 *res = count - auio.uio_resid;
3038 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
3040 * Read a block of directory entries in a file system independent format.
3043 sys_getdirentries(struct getdirentries_args *uap)
3048 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
3049 &uap->sysmsg_result, UIO_USERSPACE);
3052 error = copyout(&base, uap->basep, sizeof(*uap->basep));
3057 * getdents_args(int fd, char *buf, size_t count)
3060 sys_getdents(struct getdents_args *uap)
3064 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
3065 &uap->sysmsg_result, UIO_USERSPACE);
3071 * umask(int newmask)
3073 * Set the mode mask for creation of filesystem nodes.
3078 sys_umask(struct umask_args *uap)
3080 struct thread *td = curthread;
3081 struct proc *p = td->td_proc;
3082 struct filedesc *fdp;
3085 uap->sysmsg_result = fdp->fd_cmask;
3086 fdp->fd_cmask = uap->newmask & ALLPERMS;
3091 * revoke(char *path)
3093 * Void all references to file by ripping underlying filesystem
3098 sys_revoke(struct revoke_args *uap)
3100 struct nlookupdata nd;
3107 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3109 error = nlookup(&nd);
3111 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
3112 cred = crhold(nd.nl_cred);
3115 if (vp->v_type != VCHR && vp->v_type != VBLK)
3118 error = VOP_GETATTR(vp, &vattr);
3119 if (error == 0 && cred->cr_uid != vattr.va_uid)
3120 error = suser_cred(cred, PRISON_ROOT);
3121 if (error == 0 && count_udev(vp->v_udev) > 0) {
3124 VOP_REVOKE(vp, REVOKEALL);
3135 * getfh_args(char *fname, fhandle_t *fhp)
3137 * Get (NFS) file handle
3140 sys_getfh(struct getfh_args *uap)
3142 struct thread *td = curthread;
3143 struct nlookupdata nd;
3149 * Must be super user
3151 if ((error = suser(td)) != 0)
3155 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3157 error = nlookup(&nd);
3159 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3162 bzero(&fh, sizeof(fh));
3163 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3164 error = VFS_VPTOFH(vp, &fh.fh_fid);
3167 error = copyout(&fh, uap->fhp, sizeof(fh));
3173 * fhopen_args(const struct fhandle *u_fhp, int flags)
3175 * syscall for the rpc.lockd to use to translate a NFS file handle into
3176 * an open descriptor.
3178 * warning: do not remove the suser() call or this becomes one giant
3182 sys_fhopen(struct fhopen_args *uap)
3184 struct thread *td = curthread;
3185 struct proc *p = td->td_proc;
3190 struct vattr *vap = &vat;
3192 int fmode, mode, error, type;
3198 * Must be super user
3204 fmode = FFLAGS(uap->flags);
3205 /* why not allow a non-read/write open for our lockd? */
3206 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3208 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3211 /* find the mount point */
3212 mp = vfs_getvfs(&fhp.fh_fsid);
3215 /* now give me my vnode, it gets returned to me locked */
3216 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
3220 * from now on we have to make sure not
3221 * to forget about the vnode
3222 * any error that causes an abort must vput(vp)
3223 * just set error = err and 'goto bad;'.
3229 if (vp->v_type == VLNK) {
3233 if (vp->v_type == VSOCK) {
3238 if (fmode & (FWRITE | O_TRUNC)) {
3239 if (vp->v_type == VDIR) {
3243 error = vn_writechk(vp, NULL);
3251 error = VOP_ACCESS(vp, mode, p->p_ucred);
3255 if (fmode & O_TRUNC) {
3256 vn_unlock(vp); /* XXX */
3257 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
3260 error = VOP_SETATTR(vp, vap, p->p_ucred);
3266 * VOP_OPEN needs the file pointer so it can potentially override
3269 * WARNING! no f_ncp will be associated when fhopen()ing a directory.
3272 if ((error = falloc(p, &nfp, &indx)) != 0)
3276 error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
3279 * setting f_ops this way prevents VOP_CLOSE from being
3280 * called or fdrop() releasing the vp from v_data. Since
3281 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3283 fp->f_ops = &badfileops;
3289 * The fp is given its own reference, we still have our ref and lock.
3291 * Assert that all regular files must be created with a VM object.
3293 if (vp->v_type == VREG && vp->v_object == NULL) {
3294 printf("fhopen: regular file did not have VM object: %p\n", vp);
3299 * The open was successful. Handle any locking requirements.
3301 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3302 lf.l_whence = SEEK_SET;
3305 if (fmode & O_EXLOCK)
3306 lf.l_type = F_WRLCK;
3308 lf.l_type = F_RDLCK;
3309 if (fmode & FNONBLOCK)
3314 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3316 * release our private reference.
3318 fsetfd(p, NULL, indx);
3323 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3324 fp->f_flag |= FHASLOCK;
3328 * Clean up. Associate the file pointer with the previously
3329 * reserved descriptor and return it.
3332 fsetfd(p, fp, indx);
3334 uap->sysmsg_result = indx;
3338 fsetfd(p, NULL, indx);
3346 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3349 sys_fhstat(struct fhstat_args *uap)
3351 struct thread *td = curthread;
3359 * Must be super user
3365 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
3369 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3371 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3373 error = vn_stat(vp, &sb, td->td_proc->p_ucred);
3377 error = copyout(&sb, uap->sb, sizeof(sb));
3382 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3385 sys_fhstatfs(struct fhstatfs_args *uap)
3387 struct thread *td = curthread;
3388 struct proc *p = td->td_proc;
3393 char *fullpath, *freepath;
3398 * Must be super user
3400 if ((error = suser(td)))
3403 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3406 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3409 if (p != NULL && (p->p_fd->fd_nrdir->nc_flag & NCF_ROOT) == 0 &&
3410 !chroot_visible_mnt(mp, p))
3413 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3418 if ((error = VFS_STATFS(mp, sp, p->p_ucred)) != 0)
3421 error = cache_fullpath(p, mp->mnt_ncp, &fullpath, &freepath);
3424 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3425 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
3426 kfree(freepath, M_TEMP);
3428 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3430 bcopy(sp, &sb, sizeof(sb));
3431 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3434 return (copyout(sp, uap->buf, sizeof(*sp)));
3438 * Syscall to push extended attribute configuration information into the
3439 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3440 * a command (int cmd), and attribute name and misc data. For now, the
3441 * attribute name is left in userspace for consumption by the VFS_op.
3442 * It will probably be changed to be copied into sysspace by the
3443 * syscall in the future, once issues with various consumers of the
3444 * attribute code have raised their hands.
3446 * Currently this is used only by UFS Extended Attributes.
3449 sys_extattrctl(struct extattrctl_args *uap)
3451 struct nlookupdata nd;
3457 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3459 error = nlookup(&nd);
3461 mp = nd.nl_ncp->nc_mount;
3462 error = VFS_EXTATTRCTL(mp, uap->cmd,
3463 uap->attrname, uap->arg,
3471 * Syscall to set a named extended attribute on a file or directory.
3472 * Accepts attribute name, and a uio structure pointing to the data to set.
3473 * The uio is consumed in the style of writev(). The real work happens
3474 * in VOP_SETEXTATTR().
3477 sys_extattr_set_file(struct extattr_set_file_args *uap)
3479 char attrname[EXTATTR_MAXNAMELEN];
3480 struct iovec aiov[UIO_SMALLIOV];
3481 struct iovec *needfree;
3482 struct nlookupdata nd;
3491 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3496 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3498 error = nlookup(&nd);
3500 error = ncp_writechk(nd.nl_ncp);
3502 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3509 iovlen = uap->iovcnt * sizeof(struct iovec);
3510 if (uap->iovcnt > UIO_SMALLIOV) {
3511 if (uap->iovcnt > UIO_MAXIOV) {
3515 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3521 auio.uio_iovcnt = uap->iovcnt;
3522 auio.uio_rw = UIO_WRITE;
3523 auio.uio_segflg = UIO_USERSPACE;
3524 auio.uio_td = nd.nl_td;
3525 auio.uio_offset = 0;
3526 if ((error = copyin(uap->iovp, iov, iovlen)))
3529 for (i = 0; i < uap->iovcnt; i++) {
3530 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3534 auio.uio_resid += iov->iov_len;
3537 cnt = auio.uio_resid;
3538 error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3539 cnt -= auio.uio_resid;
3540 uap->sysmsg_result = cnt;
3545 FREE(needfree, M_IOV);
3550 * Syscall to get a named extended attribute on a file or directory.
3551 * Accepts attribute name, and a uio structure pointing to a buffer for the
3552 * data. The uio is consumed in the style of readv(). The real work
3553 * happens in VOP_GETEXTATTR();
3556 sys_extattr_get_file(struct extattr_get_file_args *uap)
3558 char attrname[EXTATTR_MAXNAMELEN];
3559 struct iovec aiov[UIO_SMALLIOV];
3560 struct iovec *needfree;
3561 struct nlookupdata nd;
3570 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3575 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3577 error = nlookup(&nd);
3579 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3585 iovlen = uap->iovcnt * sizeof (struct iovec);
3587 if (uap->iovcnt > UIO_SMALLIOV) {
3588 if (uap->iovcnt > UIO_MAXIOV) {
3592 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3598 auio.uio_iovcnt = uap->iovcnt;
3599 auio.uio_rw = UIO_READ;
3600 auio.uio_segflg = UIO_USERSPACE;
3601 auio.uio_td = nd.nl_td;
3602 auio.uio_offset = 0;
3603 if ((error = copyin(uap->iovp, iov, iovlen)))
3606 for (i = 0; i < uap->iovcnt; i++) {
3607 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3611 auio.uio_resid += iov->iov_len;
3614 cnt = auio.uio_resid;
3615 error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3616 cnt -= auio.uio_resid;
3617 uap->sysmsg_result = cnt;
3622 FREE(needfree, M_IOV);
3627 * Syscall to delete a named extended attribute from a file or directory.
3628 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3631 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
3633 char attrname[EXTATTR_MAXNAMELEN];
3634 struct nlookupdata nd;
3638 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3643 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3645 error = nlookup(&nd);
3647 error = ncp_writechk(nd.nl_ncp);
3649 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3655 error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred);
3662 chroot_visible_mnt(struct mount *mp, struct proc *p)
3664 struct namecache *ncp;
3666 * First check if this file system is below
3670 while (ncp != NULL && ncp != p->p_fd->fd_nrdir)
3671 ncp = ncp->nc_parent;
3674 * This is not below the chroot path.
3676 * Check if the chroot path is on the same filesystem,
3677 * by determing if we have to cross a mount point
3678 * before reaching mp->mnt_ncp.
3680 ncp = p->p_fd->fd_nrdir;
3681 while (ncp != NULL && ncp != mp->mnt_ncp) {
3682 if (ncp->nc_flag & NCF_MOUNTPT) {
3686 ncp = ncp->nc_parent;
3689 return(ncp != NULL);