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.106 2006/09/19 18:17:46 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;
117 struct nlookupdata nd;
118 char fstypename[MFSNAMELEN];
119 struct nlcomponent nlc;
120 struct ucred *cred = p->p_ucred;
123 if (cred->cr_prison != NULL)
125 if (usermount == 0 && (error = suser(td)))
128 * Do not allow NFS export by non-root users.
130 if (uap->flags & MNT_EXPORTED) {
136 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
139 uap->flags |= MNT_NOSUID | MNT_NODEV;
142 * Lookup the requested path and extract the ncp and vnode.
144 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
146 if ((error = nlookup(&nd)) == 0) {
147 if (nd.nl_ncp->nc_vp == NULL)
157 * Extract the locked+refd ncp and cleanup the nd structure
163 if ((ncp->nc_flag & NCF_MOUNTEDHERE) && cache_findmount(ncp))
170 * now we have the locked ref'd ncp and unreferenced vnode.
173 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
180 * Now we have an unlocked ref'd ncp and a locked ref'd vp
182 if (uap->flags & MNT_UPDATE) {
183 if ((vp->v_flag & VROOT) == 0) {
190 flag2 = mp->mnt_kern_flag;
192 * We only allow the filesystem to be reloaded if it
193 * is currently mounted read-only.
195 if ((uap->flags & MNT_RELOAD) &&
196 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
199 return (EOPNOTSUPP); /* Needs translation */
202 * Only root, or the user that did the original mount is
203 * permitted to update it.
205 if (mp->mnt_stat.f_owner != cred->cr_uid &&
206 (error = suser(td))) {
211 if (vfs_busy(mp, LK_NOWAIT)) {
216 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
222 vp->v_flag |= VMOUNT;
224 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
229 * If the user is not root, ensure that they own the directory
230 * onto which we are attempting to mount.
232 if ((error = VOP_GETATTR(vp, &va)) ||
233 (va.va_uid != cred->cr_uid && (error = suser(td)))) {
238 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
243 if (vp->v_type != VDIR) {
248 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
253 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
254 if (!strcmp(vfsp->vfc_name, fstypename))
260 /* Only load modules for root (very important!) */
261 if ((error = suser(td)) != 0) {
266 error = linker_load_file(fstypename, &lf);
267 if (error || lf == NULL) {
275 /* lookup again, see if the VFS was loaded */
276 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
277 if (!strcmp(vfsp->vfc_name, fstypename))
282 linker_file_unload(lf);
288 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
293 vp->v_flag |= VMOUNT;
296 * Allocate and initialize the filesystem.
298 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
299 TAILQ_INIT(&mp->mnt_nvnodelist);
300 TAILQ_INIT(&mp->mnt_reservedvnlist);
301 TAILQ_INIT(&mp->mnt_jlist);
302 mp->mnt_nvnodelistsize = 0;
303 lockinit(&mp->mnt_lock, "vfslock", 0, 0);
304 vfs_busy(mp, LK_NOWAIT);
305 mp->mnt_op = vfsp->vfc_vfsops;
307 vfsp->vfc_refcount++;
308 mp->mnt_stat.f_type = vfsp->vfc_typenum;
309 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
310 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
311 mp->mnt_stat.f_owner = cred->cr_uid;
312 mp->mnt_iosize_max = DFLTPHYS;
316 * Set the mount level flags.
318 if (uap->flags & MNT_RDONLY)
319 mp->mnt_flag |= MNT_RDONLY;
320 else if (mp->mnt_flag & MNT_RDONLY)
321 mp->mnt_kern_flag |= MNTK_WANTRDWR;
322 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
323 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
324 MNT_NOSYMFOLLOW | MNT_IGNORE |
325 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
326 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
327 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
328 MNT_NOSYMFOLLOW | MNT_IGNORE |
329 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
331 * Mount the filesystem.
332 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
335 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
336 if (mp->mnt_flag & MNT_UPDATE) {
337 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
338 mp->mnt_flag &= ~MNT_RDONLY;
339 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
340 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
343 mp->mnt_kern_flag = flag2;
346 vp->v_flag &= ~VMOUNT;
351 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
353 * Put the new filesystem on the mount list after root. The mount
354 * point gets its own mnt_ncp which is a special ncp linking the
355 * vnode-under to the root of the new mount. The lookup code
356 * detects the mount point going forward and detects the special
357 * mnt_ncp via NCP_MOUNTPT going backwards.
359 * It is not necessary to invalidate or purge the vnode underneath
360 * because elements under the mount will be given their own glue
364 nlc.nlc_nameptr = "";
366 mp->mnt_ncp = cache_nlookup(ncp, &nlc);
367 cache_setunresolved(mp->mnt_ncp);
368 cache_setmountpt(mp->mnt_ncp, mp);
370 /* XXX get the root of the fs and cache_setvp(mnt_ncp...) */
371 vp->v_flag &= ~VMOUNT;
372 mountlist_insert(mp, MNTINS_LAST);
373 checkdirs(vp, mp->mnt_ncp);
374 cache_unlock(mp->mnt_ncp); /* leave ref intact */
376 error = vfs_allocate_syncvnode(mp);
378 error = VFS_START(mp, 0);
381 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
382 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
383 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
384 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
385 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
386 vp->v_flag &= ~VMOUNT;
387 mp->mnt_vfc->vfc_refcount--;
397 * Scan all active processes to see if any of them have a current
398 * or root directory onto which the new filesystem has just been
399 * mounted. If so, replace them with the new mount point.
401 * The passed ncp is ref'd and locked (from the mount code) and
402 * must be associated with the vnode representing the root of the
405 struct checkdirs_info {
408 struct namecache *ncp;
411 static int checkdirs_callback(struct proc *p, void *data);
414 checkdirs(struct vnode *olddp, struct namecache *ncp)
416 struct checkdirs_info info;
420 if (olddp->v_usecount == 1)
423 if (VFS_ROOT(mp, &newdp))
424 panic("mount: lost mount");
425 cache_setvp(ncp, newdp);
427 if (rootvnode == olddp) {
429 vfs_cache_setroot(newdp, cache_hold(ncp));
435 allproc_scan(checkdirs_callback, &info);
440 * NOTE: callback is not MP safe because the scanned process's filedesc
441 * structure can be ripped out from under us, amoung other things.
444 checkdirs_callback(struct proc *p, void *data)
446 struct checkdirs_info *info = data;
447 struct filedesc *fdp;
448 struct namecache *ncdrop1;
449 struct namecache *ncdrop2;
450 struct vnode *vprele1;
451 struct vnode *vprele2;
453 if ((fdp = p->p_fd) != NULL) {
460 * MPUNSAFE - XXX fdp can be pulled out from under a
463 * A shared filedesc is ok, we don't have to copy it
464 * because we are making this change globally.
466 spin_lock_wr(&fdp->fd_spin);
467 if (fdp->fd_cdir == info->olddp) {
468 vprele1 = fdp->fd_cdir;
470 fdp->fd_cdir = info->newdp;
471 ncdrop1 = fdp->fd_ncdir;
472 fdp->fd_ncdir = cache_hold(info->ncp);
474 if (fdp->fd_rdir == info->olddp) {
475 vprele2 = fdp->fd_rdir;
477 fdp->fd_rdir = info->newdp;
478 ncdrop2 = fdp->fd_nrdir;
479 fdp->fd_nrdir = cache_hold(info->ncp);
481 spin_unlock_wr(&fdp->fd_spin);
495 * Unmount a file system.
497 * Note: unmount takes a path to the vnode mounted on as argument,
498 * not special file (as before).
501 * umount_args(char *path, int flags)
505 sys_unmount(struct unmount_args *uap)
507 struct thread *td = curthread;
508 struct proc *p = td->td_proc;
509 struct mount *mp = NULL;
511 struct nlookupdata nd;
514 if (p->p_ucred->cr_prison != NULL)
516 if (usermount == 0 && (error = suser(td)))
519 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
521 error = nlookup(&nd);
525 mp = nd.nl_ncp->nc_mount;
528 * Only root, or the user that did the original mount is
529 * permitted to unmount this filesystem.
531 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
536 * Don't allow unmounting the root file system.
538 if (mp->mnt_flag & MNT_ROOTFS) {
544 * Must be the root of the filesystem
546 if (!(nd.nl_ncp->nc_flag & NCF_MOUNTPT)) {
555 return (dounmount(mp, uap->flags));
559 * Do the actual file system unmount.
562 dounmount_interlock(struct mount *mp)
564 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
566 mp->mnt_kern_flag |= MNTK_UNMOUNT;
571 dounmount(struct mount *mp, int flags)
573 struct namecache *ncp;
579 * Exclusive access for unmounting purposes
581 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
585 * Allow filesystems to detect that a forced unmount is in progress.
587 if (flags & MNT_FORCE)
588 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
589 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_NOWAIT);
590 error = lockmgr(&mp->mnt_lock, lflags);
592 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
593 if (mp->mnt_kern_flag & MNTK_MWAIT)
598 if (mp->mnt_flag & MNT_EXPUBLIC)
599 vfs_setpublicfs(NULL, NULL, NULL);
601 vfs_msync(mp, MNT_WAIT);
602 async_flag = mp->mnt_flag & MNT_ASYNC;
603 mp->mnt_flag &=~ MNT_ASYNC;
606 * remove cache entries for this file sys and determine if anyone
607 * other then us is still holding onto any namecache references.
609 * XXX need separate ref counter on mount structure to delay
613 if ((ncp = mp->mnt_ncp) != NULL) {
614 if (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list)) {
618 if ((flags & MNT_FORCE) == 0) {
620 } else if (cache_fullpath(NULL, ncp, &ptr, &buf)) {
621 printf("Warning: forced unmount - "
622 "namecache references still present\n");
624 printf("Warning: forced unmount of %s - "
625 "namecache references still present\n",
634 if (mp->mnt_syncer != NULL)
635 vrele(mp->mnt_syncer);
636 if (((mp->mnt_flag & MNT_RDONLY) ||
637 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
638 (flags & MNT_FORCE)) {
639 error = VFS_UNMOUNT(mp, flags);
643 if (mp->mnt_syncer == NULL)
644 vfs_allocate_syncvnode(mp);
645 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
646 mp->mnt_flag |= async_flag;
647 lockmgr(&mp->mnt_lock, LK_RELEASE);
648 if (mp->mnt_kern_flag & MNTK_MWAIT)
653 * Clean up any journals still associated with the mount after
654 * filesystem activity has ceased.
656 journal_remove_all_journals(mp,
657 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
659 mountlist_remove(mp);
662 * Remove any installed vnode ops here so the individual VFSs don't
665 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
666 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
667 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
668 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
669 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
671 if ((ncp = mp->mnt_ncp) != NULL) {
672 cache_clrmountpt(ncp);
677 mp->mnt_vfc->vfc_refcount--;
678 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
679 panic("unmount: dangling vnode");
680 lockmgr(&mp->mnt_lock, LK_RELEASE);
681 if (mp->mnt_kern_flag & MNTK_MWAIT)
688 * Sync each mounted filesystem.
692 static int syncprt = 0;
693 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
696 static int sync_callback(struct mount *mp, void *data);
700 sys_sync(struct sync_args *uap)
702 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
705 * print out buffer pool stat information on each sync() call.
715 sync_callback(struct mount *mp, void *data __unused)
719 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
720 asyncflag = mp->mnt_flag & MNT_ASYNC;
721 mp->mnt_flag &= ~MNT_ASYNC;
722 vfs_msync(mp, MNT_NOWAIT);
723 VFS_SYNC(mp, MNT_NOWAIT);
724 mp->mnt_flag |= asyncflag;
729 /* XXX PRISON: could be per prison flag */
730 static int prison_quotas;
732 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
736 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
738 * Change filesystem quotas.
742 sys_quotactl(struct quotactl_args *uap)
744 struct nlookupdata nd;
752 if (p->p_ucred->cr_prison && !prison_quotas)
755 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
757 error = nlookup(&nd);
759 mp = nd.nl_ncp->nc_mount;
760 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
761 uap->arg, nd.nl_cred);
768 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
769 * void *buf, int buflen)
771 * This function operates on a mount point and executes the specified
772 * operation using the specified control data, and possibly returns data.
774 * The actual number of bytes stored in the result buffer is returned, 0
775 * if none, otherwise an error is returned.
779 sys_mountctl(struct mountctl_args *uap)
781 struct thread *td = curthread;
782 struct proc *p = td->td_proc;
790 * Sanity and permissions checks. We must be root.
793 if (p->p_ucred->cr_prison != NULL)
795 if ((error = suser(td)) != 0)
799 * Argument length checks
801 if (uap->ctllen < 0 || uap->ctllen > 1024)
803 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
805 if (uap->path == NULL)
809 * Allocate the necessary buffers and copyin data
811 path = objcache_get(namei_oc, M_WAITOK);
812 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
817 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
818 error = copyin(uap->ctl, ctl, uap->ctllen);
823 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
826 * Validate the descriptor
828 fp = holdfp(p->p_fd, uap->fd, -1);
835 * Execute the internal kernel function and clean up.
837 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
840 if (error == 0 && uap->sysmsg_result > 0)
841 error = copyout(buf, uap->buf, uap->sysmsg_result);
844 objcache_put(namei_oc, path);
853 * Execute a mount control operation by resolving the path to a mount point
854 * and calling vop_mountctl().
857 kern_mountctl(const char *path, int op, struct file *fp,
858 const void *ctl, int ctllen,
859 void *buf, int buflen, int *res)
863 struct nlookupdata nd;
868 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
870 error = nlookup(&nd);
872 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
880 * Must be the root of the filesystem
882 if ((vp->v_flag & VROOT) == 0) {
886 error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen,
893 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
895 struct thread *td = curthread;
896 struct proc *p = td->td_proc;
899 char *fullpath, *freepath;
902 if ((error = nlookup(nd)) != 0)
904 mp = nd->nl_ncp->nc_mount;
906 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
909 error = cache_fullpath(p, mp->mnt_ncp, &fullpath, &freepath);
912 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
913 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
914 kfree(freepath, M_TEMP);
916 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
917 bcopy(sp, buf, sizeof(*buf));
918 /* Only root should have access to the fsid's. */
920 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
925 * statfs_args(char *path, struct statfs *buf)
927 * Get filesystem statistics.
930 sys_statfs(struct statfs_args *uap)
932 struct nlookupdata nd;
936 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
938 error = kern_statfs(&nd, &buf);
941 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
946 kern_fstatfs(int fd, struct statfs *buf)
948 struct thread *td = curthread;
949 struct proc *p = td->td_proc;
953 char *fullpath, *freepath;
957 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
959 mp = ((struct vnode *)fp->f_data)->v_mount;
964 if (fp->f_cred == NULL) {
969 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
972 if ((error = cache_fullpath(p, mp->mnt_ncp, &fullpath, &freepath)) != 0)
974 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
975 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
976 kfree(freepath, M_TEMP);
978 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
979 bcopy(sp, buf, sizeof(*buf));
981 /* Only root should have access to the fsid's. */
983 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
991 * fstatfs_args(int fd, struct statfs *buf)
993 * Get filesystem statistics.
996 sys_fstatfs(struct fstatfs_args *uap)
1001 error = kern_fstatfs(uap->fd, &buf);
1004 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1009 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1011 * Get statistics on all filesystems.
1014 struct getfsstat_info {
1015 struct statfs *sfsp;
1024 static int getfsstat_callback(struct mount *, void *);
1028 sys_getfsstat(struct getfsstat_args *uap)
1030 struct thread *td = curthread;
1031 struct proc *p = td->td_proc;
1032 struct getfsstat_info info;
1034 bzero(&info, sizeof(info));
1035 if (p != NULL && (p->p_fd->fd_nrdir->nc_flag & NCF_ROOT) == 0)
1036 info.is_chrooted = 1;
1038 info.is_chrooted = 0;
1040 info.maxcount = uap->bufsize / sizeof(struct statfs);
1041 info.sfsp = uap->buf;
1043 info.flags = uap->flags;
1046 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1047 if (info.sfsp && info.count > info.maxcount)
1048 uap->sysmsg_result = info.maxcount;
1050 uap->sysmsg_result = info.count;
1051 return (info.error);
1055 getfsstat_callback(struct mount *mp, void *data)
1057 struct getfsstat_info *info = data;
1063 if (info->sfsp && info->count < info->maxcount) {
1064 if (info->is_chrooted && !chroot_visible_mnt(mp, info->p))
1069 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1070 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1071 * overrides MNT_WAIT.
1073 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1074 (info->flags & MNT_WAIT)) &&
1075 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1078 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1080 error = cache_fullpath(info->p, mp->mnt_ncp,
1081 &fullpath, &freepath);
1083 info->error = error;
1086 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1087 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1088 kfree(freepath, M_TEMP);
1090 error = copyout(sp, info->sfsp, sizeof(*sp));
1092 info->error = error;
1102 * fchdir_args(int fd)
1104 * Change current working directory to a given file descriptor.
1108 sys_fchdir(struct fchdir_args *uap)
1110 struct thread *td = curthread;
1111 struct proc *p = td->td_proc;
1112 struct filedesc *fdp = p->p_fd;
1113 struct vnode *vp, *ovp;
1116 struct namecache *ncp, *oncp;
1117 struct namecache *nct;
1120 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1122 vp = (struct vnode *)fp->f_data;
1124 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1125 if (vp->v_type != VDIR || fp->f_ncp == NULL)
1128 error = VOP_ACCESS(vp, VEXEC, p->p_ucred);
1134 ncp = cache_hold(fp->f_ncp);
1137 * If the ncp has become a mount point, traverse through
1141 while (!error && (ncp->nc_flag & NCF_MOUNTEDHERE) &&
1142 (mp = cache_findmount(ncp)) != NULL
1144 error = nlookup_mp(mp, &nct);
1146 cache_unlock(nct); /* leave ref intact */
1149 error = vget(vp, LK_SHARED);
1150 KKASSERT(error == 0);
1157 oncp = fdp->fd_ncdir;
1158 vn_unlock(vp); /* leave ref intact */
1160 fdp->fd_ncdir = ncp;
1172 kern_chdir(struct nlookupdata *nd)
1174 struct thread *td = curthread;
1175 struct proc *p = td->td_proc;
1176 struct filedesc *fdp = p->p_fd;
1177 struct vnode *vp, *ovp;
1178 struct namecache *oncp;
1181 if ((error = nlookup(nd)) != 0)
1183 if ((vp = nd->nl_ncp->nc_vp) == NULL)
1185 if ((error = vget(vp, LK_SHARED)) != 0)
1188 error = checkvp_chdir(vp, td);
1192 oncp = fdp->fd_ncdir;
1193 cache_unlock(nd->nl_ncp); /* leave reference intact */
1194 fdp->fd_ncdir = nd->nl_ncp;
1206 * chdir_args(char *path)
1208 * Change current working directory (``.'').
1211 sys_chdir(struct chdir_args *uap)
1213 struct nlookupdata nd;
1216 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1218 error = kern_chdir(&nd);
1224 * Helper function for raised chroot(2) security function: Refuse if
1225 * any filedescriptors are open directories.
1228 chroot_refuse_vdir_fds(fdp)
1229 struct filedesc *fdp;
1236 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1237 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1239 vp = (struct vnode *)fp->f_data;
1240 if (vp->v_type != VDIR) {
1251 * This sysctl determines if we will allow a process to chroot(2) if it
1252 * has a directory open:
1253 * 0: disallowed for all processes.
1254 * 1: allowed for processes that were not already chroot(2)'ed.
1255 * 2: allowed for all processes.
1258 static int chroot_allow_open_directories = 1;
1260 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1261 &chroot_allow_open_directories, 0, "");
1264 * chroot to the specified namecache entry. We obtain the vp from the
1265 * namecache data. The passed ncp must be locked and referenced and will
1266 * remain locked and referenced on return.
1269 kern_chroot(struct namecache *ncp)
1271 struct thread *td = curthread;
1272 struct proc *p = td->td_proc;
1273 struct filedesc *fdp = p->p_fd;
1278 * Only root can chroot
1280 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
1284 * Disallow open directory descriptors (fchdir() breakouts).
1286 if (chroot_allow_open_directories == 0 ||
1287 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1288 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1291 if ((vp = ncp->nc_vp) == NULL)
1294 if ((error = vget(vp, LK_SHARED)) != 0)
1298 * Check the validity of vp as a directory to change to and
1299 * associate it with rdir/jdir.
1301 error = checkvp_chdir(vp, td);
1302 vn_unlock(vp); /* leave reference intact */
1304 vrele(fdp->fd_rdir);
1305 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1306 cache_drop(fdp->fd_nrdir);
1307 fdp->fd_nrdir = cache_hold(ncp);
1308 if (fdp->fd_jdir == NULL) {
1311 fdp->fd_njdir = cache_hold(ncp);
1320 * chroot_args(char *path)
1322 * Change notion of root (``/'') directory.
1326 sys_chroot(struct chroot_args *uap)
1328 struct thread *td = curthread;
1329 struct nlookupdata nd;
1332 KKASSERT(td->td_proc);
1333 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1338 error = nlookup(&nd);
1340 error = kern_chroot(nd.nl_ncp);
1346 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1347 * determine whether it is legal to chdir to the vnode. The vnode's state
1348 * is not changed by this call.
1351 checkvp_chdir(struct vnode *vp, struct thread *td)
1355 if (vp->v_type != VDIR)
1358 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred);
1363 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1365 struct thread *td = curthread;
1366 struct proc *p = td->td_proc;
1367 struct lwp *lp = td->td_lwp;
1368 struct filedesc *fdp = p->p_fd;
1373 int type, indx, error;
1376 if ((oflags & O_ACCMODE) == O_ACCMODE)
1378 flags = FFLAGS(oflags);
1379 error = falloc(p, &nfp, NULL);
1383 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1386 * XXX p_dupfd is a real mess. It allows a device to return a
1387 * file descriptor to be duplicated rather then doing the open
1393 * Call vn_open() to do the lookup and assign the vnode to the
1394 * file pointer. vn_open() does not change the ref count on fp
1395 * and the vnode, on success, will be inherited by the file pointer
1398 nd->nl_flags |= NLC_LOCKVP;
1399 error = vn_open(nd, fp, flags, cmode);
1403 * handle special fdopen() case. bleh. dupfdopen() is
1404 * responsible for dropping the old contents of ofiles[indx]
1407 * Note that fsetfd() will add a ref to fp which represents
1408 * the fd_files[] assignment. We must still drop our
1411 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1412 if (fdalloc(p, 0, &indx) == 0) {
1413 error = dupfdopen(p, indx, lp->lwp_dupfd, flags, error);
1416 fdrop(fp); /* our ref */
1419 fsetfd(p, NULL, indx);
1422 fdrop(fp); /* our ref */
1423 if (error == ERESTART)
1429 * ref the vnode for ourselves so it can't be ripped out from under
1430 * is. XXX need an ND flag to request that the vnode be returned
1433 * Reserve a file descriptor but do not assign it until the open
1436 vp = (struct vnode *)fp->f_data;
1438 if ((error = fdalloc(p, 0, &indx)) != 0) {
1445 * If no error occurs the vp will have been assigned to the file
1450 if (flags & (O_EXLOCK | O_SHLOCK)) {
1451 lf.l_whence = SEEK_SET;
1454 if (flags & O_EXLOCK)
1455 lf.l_type = F_WRLCK;
1457 lf.l_type = F_RDLCK;
1458 if (flags & FNONBLOCK)
1463 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1465 * lock request failed. Clean up the reserved
1469 fsetfd(p, NULL, indx);
1473 fp->f_flag |= FHASLOCK;
1477 * Assert that all regular file vnodes were created with a object.
1479 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1480 ("open: regular file has no backing object after vn_open"));
1486 * release our private reference, leaving the one associated with the
1487 * descriptor table intact.
1489 fsetfd(p, fp, indx);
1496 * open_args(char *path, int flags, int mode)
1498 * Check permissions, allocate an open file structure,
1499 * and call the device open routine if any.
1502 sys_open(struct open_args *uap)
1504 struct nlookupdata nd;
1507 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1509 error = kern_open(&nd, uap->flags,
1510 uap->mode, &uap->sysmsg_result);
1517 kern_mknod(struct nlookupdata *nd, int mode, int dev)
1519 struct namecache *ncp;
1520 struct thread *td = curthread;
1521 struct proc *p = td->td_proc;
1529 switch (mode & S_IFMT) {
1535 error = suser_cred(p->p_ucred, PRISON_ROOT);
1542 nd->nl_flags |= NLC_CREATE;
1543 if ((error = nlookup(nd)) != 0)
1548 if ((error = ncp_writechk(ncp)) != 0)
1552 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1553 vattr.va_rdev = dev;
1556 switch (mode & S_IFMT) {
1557 case S_IFMT: /* used by badsect to flag bad sectors */
1558 vattr.va_type = VBAD;
1561 vattr.va_type = VCHR;
1564 vattr.va_type = VBLK;
1575 error = VOP_NWHITEOUT(ncp, nd->nl_cred, NAMEI_CREATE);
1578 error = VOP_NMKNOD(ncp, &vp, nd->nl_cred, &vattr);
1587 * mknod_args(char *path, int mode, int dev)
1589 * Create a special file.
1592 sys_mknod(struct mknod_args *uap)
1594 struct nlookupdata nd;
1597 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1599 error = kern_mknod(&nd, uap->mode, uap->dev);
1605 kern_mkfifo(struct nlookupdata *nd, int mode)
1607 struct namecache *ncp;
1608 struct thread *td = curthread;
1609 struct proc *p = td->td_proc;
1616 nd->nl_flags |= NLC_CREATE;
1617 if ((error = nlookup(nd)) != 0)
1622 if ((error = ncp_writechk(ncp)) != 0)
1626 vattr.va_type = VFIFO;
1627 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1629 error = VOP_NMKNOD(ncp, &vp, nd->nl_cred, &vattr);
1636 * mkfifo_args(char *path, int mode)
1638 * Create a named pipe.
1641 sys_mkfifo(struct mkfifo_args *uap)
1643 struct nlookupdata nd;
1646 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1648 error = kern_mkfifo(&nd, uap->mode);
1653 static int hardlink_check_uid = 0;
1654 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1655 &hardlink_check_uid, 0,
1656 "Unprivileged processes cannot create hard links to files owned by other "
1658 static int hardlink_check_gid = 0;
1659 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1660 &hardlink_check_gid, 0,
1661 "Unprivileged processes cannot create hard links to files owned by other "
1665 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1671 * Shortcut if disabled
1673 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
1677 * root cred can always hardlink
1679 if (suser_cred(cred, PRISON_ROOT) == 0)
1683 * Otherwise only if the originating file is owned by the
1684 * same user or group. Note that any group is allowed if
1685 * the file is owned by the caller.
1687 error = VOP_GETATTR(vp, &va);
1691 if (hardlink_check_uid) {
1692 if (cred->cr_uid != va.va_uid)
1696 if (hardlink_check_gid) {
1697 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
1705 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
1707 struct thread *td = curthread;
1712 * Lookup the source and obtained a locked vnode.
1714 * XXX relookup on vget failure / race ?
1717 if ((error = nlookup(nd)) != 0)
1719 vp = nd->nl_ncp->nc_vp;
1720 KKASSERT(vp != NULL);
1721 if (vp->v_type == VDIR)
1722 return (EPERM); /* POSIX */
1723 if ((error = ncp_writechk(nd->nl_ncp)) != 0)
1725 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
1729 * Unlock the source so we can lookup the target without deadlocking
1730 * (XXX vp is locked already, possible other deadlock?). The target
1733 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
1734 nd->nl_flags &= ~NLC_NCPISLOCKED;
1735 cache_unlock(nd->nl_ncp);
1737 linknd->nl_flags |= NLC_CREATE;
1738 if ((error = nlookup(linknd)) != 0) {
1742 if (linknd->nl_ncp->nc_vp) {
1748 * Finally run the new API VOP.
1750 error = can_hardlink(vp, td, td->td_proc->p_ucred);
1752 error = VOP_NLINK(linknd->nl_ncp, vp, linknd->nl_cred);
1758 * link_args(char *path, char *link)
1760 * Make a hard file link.
1763 sys_link(struct link_args *uap)
1765 struct nlookupdata nd, linknd;
1768 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1770 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
1772 error = kern_link(&nd, &linknd);
1773 nlookup_done(&linknd);
1780 kern_symlink(struct nlookupdata *nd, char *path, int mode)
1782 struct namecache *ncp;
1788 nd->nl_flags |= NLC_CREATE;
1789 if ((error = nlookup(nd)) != 0)
1794 if ((error = ncp_writechk(ncp)) != 0)
1797 vattr.va_mode = mode;
1798 error = VOP_NSYMLINK(ncp, &vp, nd->nl_cred, &vattr, path);
1805 * symlink(char *path, char *link)
1807 * Make a symbolic link.
1810 sys_symlink(struct symlink_args *uap)
1812 struct thread *td = curthread;
1813 struct nlookupdata nd;
1818 path = objcache_get(namei_oc, M_WAITOK);
1819 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1821 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
1823 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
1824 error = kern_symlink(&nd, path, mode);
1828 objcache_put(namei_oc, path);
1833 * undelete_args(char *path)
1835 * Delete a whiteout from the filesystem.
1839 sys_undelete(struct undelete_args *uap)
1841 struct nlookupdata nd;
1844 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1846 nd.nl_flags |= NLC_DELETE;
1848 error = nlookup(&nd);
1850 error = ncp_writechk(nd.nl_ncp);
1852 error = VOP_NWHITEOUT(nd.nl_ncp, nd.nl_cred, NAMEI_DELETE);
1858 kern_unlink(struct nlookupdata *nd)
1860 struct namecache *ncp;
1864 nd->nl_flags |= NLC_DELETE;
1865 if ((error = nlookup(nd)) != 0)
1868 if ((error = ncp_writechk(ncp)) != 0)
1870 error = VOP_NREMOVE(ncp, nd->nl_cred);
1875 * unlink_args(char *path)
1877 * Delete a name from the filesystem.
1880 sys_unlink(struct unlink_args *uap)
1882 struct nlookupdata nd;
1885 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1887 error = kern_unlink(&nd);
1893 kern_lseek(int fd, off_t offset, int whence, off_t *res)
1895 struct thread *td = curthread;
1896 struct proc *p = td->td_proc;
1901 fp = holdfp(p->p_fd, fd, -1);
1904 if (fp->f_type != DTYPE_VNODE) {
1911 fp->f_offset += offset;
1915 error = VOP_GETATTR((struct vnode *)fp->f_data, &vattr);
1917 fp->f_offset = offset + vattr.va_size;
1920 fp->f_offset = offset;
1927 *res = fp->f_offset;
1934 * lseek_args(int fd, int pad, off_t offset, int whence)
1936 * Reposition read/write file offset.
1939 sys_lseek(struct lseek_args *uap)
1943 error = kern_lseek(uap->fd, uap->offset, uap->whence,
1944 &uap->sysmsg_offset);
1950 kern_access(struct nlookupdata *nd, int aflags)
1955 if ((error = nlookup(nd)) != 0)
1958 error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
1962 /* Flags == 0 means only check for existence. */
1971 if ((flags & VWRITE) == 0 ||
1972 (error = vn_writechk(vp, nd->nl_ncp)) == 0)
1973 error = VOP_ACCESS(vp, flags, nd->nl_cred);
1976 * If the file handle is stale we have to re-resolve the
1977 * entry. This is a hack at the moment.
1979 if (error == ESTALE) {
1980 cache_setunresolved(nd->nl_ncp);
1981 error = cache_resolve(nd->nl_ncp, nd->nl_cred);
1994 * access_args(char *path, int flags)
1996 * Check access permissions.
1999 sys_access(struct access_args *uap)
2001 struct nlookupdata nd;
2004 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2006 error = kern_access(&nd, uap->flags);
2012 kern_stat(struct nlookupdata *nd, struct stat *st)
2018 if ((error = nlookup(nd)) != 0)
2021 if ((vp = nd->nl_ncp->nc_vp) == NULL)
2025 if ((error = vget(vp, LK_SHARED)) != 0)
2027 error = vn_stat(vp, st, nd->nl_cred);
2030 * If the file handle is stale we have to re-resolve the entry. This
2031 * is a hack at the moment.
2033 if (error == ESTALE) {
2034 cache_setunresolved(nd->nl_ncp);
2035 error = cache_resolve(nd->nl_ncp, nd->nl_cred);
2046 * stat_args(char *path, struct stat *ub)
2048 * Get file status; this version follows links.
2051 sys_stat(struct stat_args *uap)
2053 struct nlookupdata nd;
2057 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2059 error = kern_stat(&nd, &st);
2061 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2068 * lstat_args(char *path, struct stat *ub)
2070 * Get file status; this version does not follow links.
2073 sys_lstat(struct lstat_args *uap)
2075 struct nlookupdata nd;
2079 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2081 error = kern_stat(&nd, &st);
2083 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2090 * pathconf_Args(char *path, int name)
2092 * Get configurable pathname variables.
2096 sys_pathconf(struct pathconf_args *uap)
2098 struct nlookupdata nd;
2103 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2105 error = nlookup(&nd);
2107 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
2110 error = VOP_PATHCONF(vp, uap->name, uap->sysmsg_fds);
2118 * kern_readlink isn't properly split yet. There is a copyin burried
2119 * in VOP_READLINK().
2122 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2124 struct thread *td = curthread;
2125 struct proc *p = td->td_proc;
2131 if ((error = nlookup(nd)) != 0)
2133 error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
2136 if (vp->v_type != VLNK) {
2139 aiov.iov_base = buf;
2140 aiov.iov_len = count;
2141 auio.uio_iov = &aiov;
2142 auio.uio_iovcnt = 1;
2143 auio.uio_offset = 0;
2144 auio.uio_rw = UIO_READ;
2145 auio.uio_segflg = UIO_USERSPACE;
2147 auio.uio_resid = count;
2148 error = VOP_READLINK(vp, &auio, p->p_ucred);
2151 *res = count - auio.uio_resid;
2156 * readlink_args(char *path, char *buf, int count)
2158 * Return target name of a symbolic link.
2161 sys_readlink(struct readlink_args *uap)
2163 struct nlookupdata nd;
2166 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2168 error = kern_readlink(&nd, uap->buf, uap->count,
2169 &uap->sysmsg_result);
2176 setfflags(struct vnode *vp, int flags)
2178 struct thread *td = curthread;
2179 struct proc *p = td->td_proc;
2184 * Prevent non-root users from setting flags on devices. When
2185 * a device is reused, users can retain ownership of the device
2186 * if they are allowed to set flags and programs assume that
2187 * chown can't fail when done as root.
2189 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2190 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
2194 * note: vget is required for any operation that might mod the vnode
2195 * so VINACTIVE is properly cleared.
2197 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2199 vattr.va_flags = flags;
2200 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2207 * chflags(char *path, int flags)
2209 * Change flags of a file given a path name.
2213 sys_chflags(struct chflags_args *uap)
2215 struct nlookupdata nd;
2220 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2221 /* XXX Add NLC flag indicating modifying operation? */
2223 error = nlookup(&nd);
2225 error = ncp_writechk(nd.nl_ncp);
2227 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
2230 error = setfflags(vp, uap->flags);
2237 * fchflags_args(int fd, int flags)
2239 * Change flags of a file given a file descriptor.
2243 sys_fchflags(struct fchflags_args *uap)
2245 struct thread *td = curthread;
2246 struct proc *p = td->td_proc;
2250 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2253 error = ncp_writechk(fp->f_ncp);
2255 error = setfflags((struct vnode *) fp->f_data, uap->flags);
2261 setfmode(struct vnode *vp, int mode)
2263 struct thread *td = curthread;
2264 struct proc *p = td->td_proc;
2269 * note: vget is required for any operation that might mod the vnode
2270 * so VINACTIVE is properly cleared.
2272 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2274 vattr.va_mode = mode & ALLPERMS;
2275 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2282 kern_chmod(struct nlookupdata *nd, int mode)
2287 /* XXX Add NLC flag indicating modifying operation? */
2288 if ((error = nlookup(nd)) != 0)
2290 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2292 if ((error = ncp_writechk(nd->nl_ncp)) == 0)
2293 error = setfmode(vp, mode);
2299 * chmod_args(char *path, int mode)
2301 * Change mode of a file given path name.
2305 sys_chmod(struct chmod_args *uap)
2307 struct nlookupdata nd;
2310 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2312 error = kern_chmod(&nd, uap->mode);
2318 * lchmod_args(char *path, int mode)
2320 * Change mode of a file given path name (don't follow links.)
2324 sys_lchmod(struct lchmod_args *uap)
2326 struct nlookupdata nd;
2329 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2331 error = kern_chmod(&nd, uap->mode);
2337 * fchmod_args(int fd, int mode)
2339 * Change mode of a file given a file descriptor.
2343 sys_fchmod(struct fchmod_args *uap)
2345 struct thread *td = curthread;
2346 struct proc *p = td->td_proc;
2350 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2353 error = ncp_writechk(fp->f_ncp);
2355 error = setfmode((struct vnode *)fp->f_data, uap->mode);
2361 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2363 struct thread *td = curthread;
2364 struct proc *p = td->td_proc;
2369 * note: vget is required for any operation that might mod the vnode
2370 * so VINACTIVE is properly cleared.
2372 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2376 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2383 kern_chown(struct nlookupdata *nd, int uid, int gid)
2388 /* XXX Add NLC flag indicating modifying operation? */
2389 if ((error = nlookup(nd)) != 0)
2391 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2393 if ((error = ncp_writechk(nd->nl_ncp)) == 0)
2394 error = setfown(vp, uid, gid);
2400 * chown(char *path, int uid, int gid)
2402 * Set ownership given a path name.
2405 sys_chown(struct chown_args *uap)
2407 struct nlookupdata nd;
2410 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2412 error = kern_chown(&nd, uap->uid, uap->gid);
2418 * lchown_args(char *path, int uid, int gid)
2420 * Set ownership given a path name, do not cross symlinks.
2423 sys_lchown(struct lchown_args *uap)
2425 struct nlookupdata nd;
2428 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2430 error = kern_chown(&nd, uap->uid, uap->gid);
2436 * fchown_args(int fd, int uid, int gid)
2438 * Set ownership given a file descriptor.
2442 sys_fchown(struct fchown_args *uap)
2444 struct thread *td = curthread;
2445 struct proc *p = td->td_proc;
2449 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2452 error = ncp_writechk(fp->f_ncp);
2454 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
2460 getutimes(const struct timeval *tvp, struct timespec *tsp)
2462 struct timeval tv[2];
2466 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2469 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2470 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2476 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2478 struct thread *td = curthread;
2479 struct proc *p = td->td_proc;
2484 * note: vget is required for any operation that might mod the vnode
2485 * so VINACTIVE is properly cleared.
2487 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2489 vattr.va_atime = ts[0];
2490 vattr.va_mtime = ts[1];
2492 vattr.va_vaflags |= VA_UTIMES_NULL;
2493 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2500 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
2502 struct timespec ts[2];
2506 if ((error = getutimes(tptr, ts)) != 0)
2508 /* XXX Add NLC flag indicating modifying operation? */
2509 if ((error = nlookup(nd)) != 0)
2511 if ((error = ncp_writechk(nd->nl_ncp)) != 0)
2513 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2515 error = setutimes(vp, ts, tptr == NULL);
2521 * utimes_args(char *path, struct timeval *tptr)
2523 * Set the access and modification times of a file.
2526 sys_utimes(struct utimes_args *uap)
2528 struct timeval tv[2];
2529 struct nlookupdata nd;
2533 error = copyin(uap->tptr, tv, sizeof(tv));
2537 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2539 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2545 * lutimes_args(char *path, struct timeval *tptr)
2547 * Set the access and modification times of a file.
2550 sys_lutimes(struct lutimes_args *uap)
2552 struct timeval tv[2];
2553 struct nlookupdata nd;
2557 error = copyin(uap->tptr, tv, sizeof(tv));
2561 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2563 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2569 kern_futimes(int fd, struct timeval *tptr)
2571 struct thread *td = curthread;
2572 struct proc *p = td->td_proc;
2573 struct timespec ts[2];
2577 error = getutimes(tptr, ts);
2580 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
2583 error = ncp_writechk(fp->f_ncp);
2585 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2591 * futimes_args(int fd, struct timeval *tptr)
2593 * Set the access and modification times of a file.
2596 sys_futimes(struct futimes_args *uap)
2598 struct timeval tv[2];
2602 error = copyin(uap->tptr, tv, sizeof(tv));
2607 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2613 kern_truncate(struct nlookupdata *nd, off_t length)
2621 /* XXX Add NLC flag indicating modifying operation? */
2622 if ((error = nlookup(nd)) != 0)
2624 if ((error = ncp_writechk(nd->nl_ncp)) != 0)
2626 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2628 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2632 if (vp->v_type == VDIR) {
2634 } else if ((error = vn_writechk(vp, nd->nl_ncp)) == 0 &&
2635 (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred)) == 0) {
2637 vattr.va_size = length;
2638 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
2645 * truncate(char *path, int pad, off_t length)
2647 * Truncate a file given its path name.
2650 sys_truncate(struct truncate_args *uap)
2652 struct nlookupdata nd;
2655 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2657 error = kern_truncate(&nd, uap->length);
2663 kern_ftruncate(int fd, off_t length)
2665 struct thread *td = curthread;
2666 struct proc *p = td->td_proc;
2674 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
2677 error = ncp_writechk(fp->f_ncp);
2681 if ((fp->f_flag & FWRITE) == 0) {
2685 vp = (struct vnode *)fp->f_data;
2686 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2687 if (vp->v_type == VDIR) {
2689 } else if ((error = vn_writechk(vp, NULL)) == 0) {
2691 vattr.va_size = length;
2692 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
2701 * ftruncate_args(int fd, int pad, off_t length)
2703 * Truncate a file given a file descriptor.
2706 sys_ftruncate(struct ftruncate_args *uap)
2710 error = kern_ftruncate(uap->fd, uap->length);
2718 * Sync an open file.
2722 sys_fsync(struct fsync_args *uap)
2724 struct thread *td = curthread;
2725 struct proc *p = td->td_proc;
2731 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2733 vp = (struct vnode *)fp->f_data;
2734 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2735 if ((obj = vp->v_object) != NULL)
2736 vm_object_page_clean(obj, 0, 0, 0);
2737 if ((error = VOP_FSYNC(vp, MNT_WAIT)) == 0 &&
2738 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2740 error = (*bioops.io_fsync)(vp);
2748 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
2750 struct namecache *fncpd;
2751 struct namecache *tncpd;
2752 struct namecache *ncp;
2757 if ((error = nlookup(fromnd)) != 0)
2759 if ((fncpd = fromnd->nl_ncp->nc_parent) == NULL)
2764 * unlock the source ncp so we can lookup the target ncp without
2765 * deadlocking. The target may or may not exist so we do not check
2766 * for a target vp like kern_mkdir() and other creation functions do.
2768 * The source and target directories are ref'd and rechecked after
2769 * everything is relocked to determine if the source or target file
2772 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
2773 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
2774 cache_unlock(fromnd->nl_ncp);
2776 tond->nl_flags |= NLC_CREATE;
2777 if ((error = nlookup(tond)) != 0) {
2781 if ((tncpd = tond->nl_ncp->nc_parent) == NULL) {
2788 * If the source and target are the same there is nothing to do
2790 if (fromnd->nl_ncp == tond->nl_ncp) {
2797 * Mount points cannot be renamed or overwritten
2799 if ((fromnd->nl_ncp->nc_flag | tond->nl_ncp->nc_flag) &
2800 (NCF_MOUNTPT|NCF_MOUNTEDHERE)
2808 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
2809 * may have become invalid while it was unlocked, nc_vp and nc_mount
2812 if (cache_lock_nonblock(fromnd->nl_ncp) == 0) {
2813 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2814 } else if (fromnd->nl_ncp > tond->nl_ncp) {
2815 cache_lock(fromnd->nl_ncp);
2816 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2818 cache_unlock(tond->nl_ncp);
2819 cache_lock(fromnd->nl_ncp);
2820 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2821 cache_lock(tond->nl_ncp);
2822 cache_resolve(tond->nl_ncp, tond->nl_cred);
2824 fromnd->nl_flags |= NLC_NCPISLOCKED;
2827 * make sure the parent directories linkages are the same
2829 if (fncpd != fromnd->nl_ncp->nc_parent ||
2830 tncpd != tond->nl_ncp->nc_parent) {
2837 * Both the source and target must be within the same filesystem and
2838 * in the same filesystem as their parent directories within the
2839 * namecache topology.
2841 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
2843 mp = fncpd->nc_mount;
2844 if (mp != tncpd->nc_mount || mp != fromnd->nl_ncp->nc_mount ||
2845 mp != tond->nl_ncp->nc_mount) {
2852 * Make sure the mount point is writable
2854 if ((error = ncp_writechk(tond->nl_ncp)) != 0) {
2861 * If the target exists and either the source or target is a directory,
2862 * then both must be directories.
2864 * Due to relocking of the source, fromnd->nl_ncp->nc_vp might have
2867 if (tond->nl_ncp->nc_vp) {
2868 if (fromnd->nl_ncp->nc_vp == NULL) {
2870 } else if (fromnd->nl_ncp->nc_vp->v_type == VDIR) {
2871 if (tond->nl_ncp->nc_vp->v_type != VDIR)
2873 } else if (tond->nl_ncp->nc_vp->v_type == VDIR) {
2879 * You cannot rename a source into itself or a subdirectory of itself.
2880 * We check this by travsersing the target directory upwards looking
2881 * for a match against the source.
2884 for (ncp = tncpd; ncp; ncp = ncp->nc_parent) {
2885 if (fromnd->nl_ncp == ncp) {
2896 * Even though the namespaces are different, they may still represent
2897 * hardlinks to the same file. The filesystem might have a hard time
2898 * with this so we issue a NREMOVE of the source instead of a NRENAME
2899 * when we detect the situation.
2902 if (fromnd->nl_ncp->nc_vp == tond->nl_ncp->nc_vp) {
2903 error = VOP_NREMOVE(fromnd->nl_ncp, fromnd->nl_cred);
2905 error = VOP_NRENAME(fromnd->nl_ncp, tond->nl_ncp,
2913 * rename_args(char *from, char *to)
2915 * Rename files. Source and destination must either both be directories,
2916 * or both not be directories. If target is a directory, it must be empty.
2919 sys_rename(struct rename_args *uap)
2921 struct nlookupdata fromnd, tond;
2924 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
2926 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
2928 error = kern_rename(&fromnd, &tond);
2929 nlookup_done(&tond);
2931 nlookup_done(&fromnd);
2936 kern_mkdir(struct nlookupdata *nd, int mode)
2938 struct thread *td = curthread;
2939 struct proc *p = td->td_proc;
2940 struct namecache *ncp;
2946 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE;
2947 if ((error = nlookup(nd)) != 0)
2953 if ((error = ncp_writechk(ncp)) != 0)
2957 vattr.va_type = VDIR;
2958 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2961 error = VOP_NMKDIR(ncp, &vp, p->p_ucred, &vattr);
2968 * mkdir_args(char *path, int mode)
2970 * Make a directory file.
2974 sys_mkdir(struct mkdir_args *uap)
2976 struct nlookupdata nd;
2979 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2981 error = kern_mkdir(&nd, uap->mode);
2987 kern_rmdir(struct nlookupdata *nd)
2989 struct namecache *ncp;
2993 nd->nl_flags |= NLC_DELETE;
2994 if ((error = nlookup(nd)) != 0)
2998 * Do not allow directories representing mount points to be
2999 * deleted, even if empty. Check write perms on mount point
3000 * in case the vnode is aliased (aka nullfs).
3002 if (nd->nl_ncp->nc_flag & (NCF_MOUNTEDHERE|NCF_MOUNTPT))
3004 if ((error = ncp_writechk(nd->nl_ncp)) != 0)
3008 error = VOP_NRMDIR(ncp, nd->nl_cred);
3013 * rmdir_args(char *path)
3015 * Remove a directory file.
3019 sys_rmdir(struct rmdir_args *uap)
3021 struct nlookupdata nd;
3024 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3026 error = kern_rmdir(&nd);
3032 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
3033 enum uio_seg direction)
3035 struct thread *td = curthread;
3036 struct proc *p = td->td_proc;
3044 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3046 if ((fp->f_flag & FREAD) == 0) {
3050 vp = (struct vnode *)fp->f_data;
3052 if (vp->v_type != VDIR) {
3056 aiov.iov_base = buf;
3057 aiov.iov_len = count;
3058 auio.uio_iov = &aiov;
3059 auio.uio_iovcnt = 1;
3060 auio.uio_rw = UIO_READ;
3061 auio.uio_segflg = direction;
3063 auio.uio_resid = count;
3064 loff = auio.uio_offset = fp->f_offset;
3065 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3066 fp->f_offset = auio.uio_offset;
3069 if (count == auio.uio_resid) {
3070 if (union_dircheckp) {
3071 error = union_dircheckp(td, &vp, fp);
3078 if ((vp->v_flag & VROOT) &&
3079 (vp->v_mount->mnt_flag & MNT_UNION)) {
3080 struct vnode *tvp = vp;
3081 vp = vp->v_mount->mnt_vnodecovered;
3093 *res = count - auio.uio_resid;
3100 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
3102 * Read a block of directory entries in a file system independent format.
3105 sys_getdirentries(struct getdirentries_args *uap)
3110 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
3111 &uap->sysmsg_result, UIO_USERSPACE);
3114 error = copyout(&base, uap->basep, sizeof(*uap->basep));
3119 * getdents_args(int fd, char *buf, size_t count)
3122 sys_getdents(struct getdents_args *uap)
3126 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
3127 &uap->sysmsg_result, UIO_USERSPACE);
3133 * umask(int newmask)
3135 * Set the mode mask for creation of filesystem nodes.
3140 sys_umask(struct umask_args *uap)
3142 struct thread *td = curthread;
3143 struct proc *p = td->td_proc;
3144 struct filedesc *fdp;
3147 uap->sysmsg_result = fdp->fd_cmask;
3148 fdp->fd_cmask = uap->newmask & ALLPERMS;
3153 * revoke(char *path)
3155 * Void all references to file by ripping underlying filesystem
3160 sys_revoke(struct revoke_args *uap)
3162 struct nlookupdata nd;
3169 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3171 error = nlookup(&nd);
3173 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
3174 cred = crhold(nd.nl_cred);
3177 if (vp->v_type != VCHR && vp->v_type != VBLK)
3180 error = VOP_GETATTR(vp, &vattr);
3181 if (error == 0 && cred->cr_uid != vattr.va_uid)
3182 error = suser_cred(cred, PRISON_ROOT);
3183 if (error == 0 && count_udev(vp->v_udev) > 0) {
3186 VOP_REVOKE(vp, REVOKEALL);
3197 * getfh_args(char *fname, fhandle_t *fhp)
3199 * Get (NFS) file handle
3202 sys_getfh(struct getfh_args *uap)
3204 struct thread *td = curthread;
3205 struct nlookupdata nd;
3211 * Must be super user
3213 if ((error = suser(td)) != 0)
3217 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3219 error = nlookup(&nd);
3221 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3224 bzero(&fh, sizeof(fh));
3225 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3226 error = VFS_VPTOFH(vp, &fh.fh_fid);
3229 error = copyout(&fh, uap->fhp, sizeof(fh));
3235 * fhopen_args(const struct fhandle *u_fhp, int flags)
3237 * syscall for the rpc.lockd to use to translate a NFS file handle into
3238 * an open descriptor.
3240 * warning: do not remove the suser() call or this becomes one giant
3244 sys_fhopen(struct fhopen_args *uap)
3246 struct thread *td = curthread;
3247 struct proc *p = td->td_proc;
3252 struct vattr *vap = &vat;
3254 int fmode, mode, error, type;
3260 * Must be super user
3266 fmode = FFLAGS(uap->flags);
3267 /* why not allow a non-read/write open for our lockd? */
3268 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3270 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3273 /* find the mount point */
3274 mp = vfs_getvfs(&fhp.fh_fsid);
3277 /* now give me my vnode, it gets returned to me locked */
3278 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
3282 * from now on we have to make sure not
3283 * to forget about the vnode
3284 * any error that causes an abort must vput(vp)
3285 * just set error = err and 'goto bad;'.
3291 if (vp->v_type == VLNK) {
3295 if (vp->v_type == VSOCK) {
3300 if (fmode & (FWRITE | O_TRUNC)) {
3301 if (vp->v_type == VDIR) {
3305 error = vn_writechk(vp, NULL);
3313 error = VOP_ACCESS(vp, mode, p->p_ucred);
3317 if (fmode & O_TRUNC) {
3318 vn_unlock(vp); /* XXX */
3319 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
3322 error = VOP_SETATTR(vp, vap, p->p_ucred);
3328 * VOP_OPEN needs the file pointer so it can potentially override
3331 * WARNING! no f_ncp will be associated when fhopen()ing a directory.
3334 if ((error = falloc(p, &nfp, &indx)) != 0)
3338 error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
3341 * setting f_ops this way prevents VOP_CLOSE from being
3342 * called or fdrop() releasing the vp from v_data. Since
3343 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3345 fp->f_ops = &badfileops;
3351 * The fp is given its own reference, we still have our ref and lock.
3353 * Assert that all regular files must be created with a VM object.
3355 if (vp->v_type == VREG && vp->v_object == NULL) {
3356 printf("fhopen: regular file did not have VM object: %p\n", vp);
3361 * The open was successful. Handle any locking requirements.
3363 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3364 lf.l_whence = SEEK_SET;
3367 if (fmode & O_EXLOCK)
3368 lf.l_type = F_WRLCK;
3370 lf.l_type = F_RDLCK;
3371 if (fmode & FNONBLOCK)
3376 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3378 * release our private reference.
3380 fsetfd(p, NULL, indx);
3385 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3386 fp->f_flag |= FHASLOCK;
3390 * Clean up. Associate the file pointer with the previously
3391 * reserved descriptor and return it.
3394 fsetfd(p, fp, indx);
3396 uap->sysmsg_result = indx;
3400 fsetfd(p, NULL, indx);
3408 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3411 sys_fhstat(struct fhstat_args *uap)
3413 struct thread *td = curthread;
3421 * Must be super user
3427 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
3431 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3433 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3435 error = vn_stat(vp, &sb, td->td_proc->p_ucred);
3439 error = copyout(&sb, uap->sb, sizeof(sb));
3444 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3447 sys_fhstatfs(struct fhstatfs_args *uap)
3449 struct thread *td = curthread;
3450 struct proc *p = td->td_proc;
3455 char *fullpath, *freepath;
3460 * Must be super user
3462 if ((error = suser(td)))
3465 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3468 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3471 if (p != NULL && (p->p_fd->fd_nrdir->nc_flag & NCF_ROOT) == 0 &&
3472 !chroot_visible_mnt(mp, p))
3475 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3480 if ((error = VFS_STATFS(mp, sp, p->p_ucred)) != 0)
3483 error = cache_fullpath(p, mp->mnt_ncp, &fullpath, &freepath);
3486 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3487 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
3488 kfree(freepath, M_TEMP);
3490 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3492 bcopy(sp, &sb, sizeof(sb));
3493 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3496 return (copyout(sp, uap->buf, sizeof(*sp)));
3500 * Syscall to push extended attribute configuration information into the
3501 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3502 * a command (int cmd), and attribute name and misc data. For now, the
3503 * attribute name is left in userspace for consumption by the VFS_op.
3504 * It will probably be changed to be copied into sysspace by the
3505 * syscall in the future, once issues with various consumers of the
3506 * attribute code have raised their hands.
3508 * Currently this is used only by UFS Extended Attributes.
3511 sys_extattrctl(struct extattrctl_args *uap)
3513 struct nlookupdata nd;
3519 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3521 error = nlookup(&nd);
3523 mp = nd.nl_ncp->nc_mount;
3524 error = VFS_EXTATTRCTL(mp, uap->cmd,
3525 uap->attrname, uap->arg,
3533 * Syscall to set a named extended attribute on a file or directory.
3534 * Accepts attribute name, and a uio structure pointing to the data to set.
3535 * The uio is consumed in the style of writev(). The real work happens
3536 * in VOP_SETEXTATTR().
3539 sys_extattr_set_file(struct extattr_set_file_args *uap)
3541 char attrname[EXTATTR_MAXNAMELEN];
3542 struct iovec aiov[UIO_SMALLIOV];
3543 struct iovec *needfree;
3544 struct nlookupdata nd;
3553 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3558 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3560 error = nlookup(&nd);
3562 error = ncp_writechk(nd.nl_ncp);
3564 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3571 iovlen = uap->iovcnt * sizeof(struct iovec);
3572 if (uap->iovcnt > UIO_SMALLIOV) {
3573 if (uap->iovcnt > UIO_MAXIOV) {
3577 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3583 auio.uio_iovcnt = uap->iovcnt;
3584 auio.uio_rw = UIO_WRITE;
3585 auio.uio_segflg = UIO_USERSPACE;
3586 auio.uio_td = nd.nl_td;
3587 auio.uio_offset = 0;
3588 if ((error = copyin(uap->iovp, iov, iovlen)))
3591 for (i = 0; i < uap->iovcnt; i++) {
3592 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3596 auio.uio_resid += iov->iov_len;
3599 cnt = auio.uio_resid;
3600 error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3601 cnt -= auio.uio_resid;
3602 uap->sysmsg_result = cnt;
3607 FREE(needfree, M_IOV);
3612 * Syscall to get a named extended attribute on a file or directory.
3613 * Accepts attribute name, and a uio structure pointing to a buffer for the
3614 * data. The uio is consumed in the style of readv(). The real work
3615 * happens in VOP_GETEXTATTR();
3618 sys_extattr_get_file(struct extattr_get_file_args *uap)
3620 char attrname[EXTATTR_MAXNAMELEN];
3621 struct iovec aiov[UIO_SMALLIOV];
3622 struct iovec *needfree;
3623 struct nlookupdata nd;
3632 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3637 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3639 error = nlookup(&nd);
3641 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3647 iovlen = uap->iovcnt * sizeof (struct iovec);
3649 if (uap->iovcnt > UIO_SMALLIOV) {
3650 if (uap->iovcnt > UIO_MAXIOV) {
3654 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3660 auio.uio_iovcnt = uap->iovcnt;
3661 auio.uio_rw = UIO_READ;
3662 auio.uio_segflg = UIO_USERSPACE;
3663 auio.uio_td = nd.nl_td;
3664 auio.uio_offset = 0;
3665 if ((error = copyin(uap->iovp, iov, iovlen)))
3668 for (i = 0; i < uap->iovcnt; i++) {
3669 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3673 auio.uio_resid += iov->iov_len;
3676 cnt = auio.uio_resid;
3677 error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3678 cnt -= auio.uio_resid;
3679 uap->sysmsg_result = cnt;
3684 FREE(needfree, M_IOV);
3689 * Syscall to delete a named extended attribute from a file or directory.
3690 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3693 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
3695 char attrname[EXTATTR_MAXNAMELEN];
3696 struct nlookupdata nd;
3700 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3705 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3707 error = nlookup(&nd);
3709 error = ncp_writechk(nd.nl_ncp);
3711 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3717 error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred);
3724 chroot_visible_mnt(struct mount *mp, struct proc *p)
3726 struct namecache *ncp;
3728 * First check if this file system is below
3732 while (ncp != NULL && ncp != p->p_fd->fd_nrdir)
3733 ncp = ncp->nc_parent;
3736 * This is not below the chroot path.
3738 * Check if the chroot path is on the same filesystem,
3739 * by determing if we have to cross a mount point
3740 * before reaching mp->mnt_ncp.
3742 ncp = p->p_fd->fd_nrdir;
3743 while (ncp != NULL && ncp != mp->mnt_ncp) {
3744 if (ncp->nc_flag & NCF_MOUNTPT) {
3748 ncp = ncp->nc_parent;
3751 return(ncp != NULL);