X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/a3d08fcd5120d2aa8303b6349ca8b14e3f284af3..21362eb3e66fd2c787aee132bce100a44d71a99c:/bsd/vfs/vfs_syscalls.c diff --git a/bsd/vfs/vfs_syscalls.c b/bsd/vfs/vfs_syscalls.c index a44f963af..340c6ab9d 100644 --- a/bsd/vfs/vfs_syscalls.c +++ b/bsd/vfs/vfs_syscalls.c @@ -1,23 +1,29 @@ /* - * Copyright (c) 1995-2004 Apple Computer, Inc. All rights reserved. + * Copyright (c) 1995-2005 Apple Computer, Inc. All rights reserved. * - * @APPLE_LICENSE_HEADER_START@ + * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * - * The contents of this file constitute Original Code as defined in and - * are subject to the Apple Public Source License Version 1.1 (the - * "License"). You may not use this file except in compliance with the - * License. Please obtain a copy of the License at - * http://www.apple.com/publicsource and read it before using this file. + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. The rights granted to you under the License + * may not be used to create, or enable the creation or redistribution of, + * unlawful or unlicensed copies of an Apple operating system, or to + * circumvent, violate, or enable the circumvention or violation of, any + * terms of an Apple operating system software license agreement. * - * This Original Code and all software distributed under the License are - * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the - * License for the specific language governing rights and limitations - * under the License. + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. * - * @APPLE_LICENSE_HEADER_END@ + * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ /* * Copyright (c) 1989, 1993 @@ -64,28 +70,41 @@ #include #include #include -#include +#include #include -#include -#include -#include -#include +#include +#include +#include +#include +#include #include +#include #include #include #include #include #include +#include +#include +#include +#include +#include +#include +#include +#include #include #include -#include -#include +#include +#include +#include + +#include #include +#include -struct lock__bsd__ exchangelock; /* * The currently logged-in user, for ownership of files/directories whose on-disk @@ -93,14 +112,61 @@ struct lock__bsd__ exchangelock; */ uid_t console_user; -static int change_dir __P((struct nameidata *ndp, struct proc *p)); -static void checkdirs __P((struct vnode *olddp)); -static void enablequotas __P((struct proc *p, struct mount *mp)); -void notify_filemod_watchers(struct vnode *vp, struct proc *p); +static int change_dir(struct nameidata *ndp, vfs_context_t ctx); +static void checkdirs(struct vnode *olddp, vfs_context_t ctx); +void enablequotas(struct mount *mp, vfs_context_t ctx); +static int getfsstat_callback(mount_t mp, void * arg); +static int getutimes(user_addr_t usrtvp, struct timespec *tsp); +static int setutimes(vfs_context_t ctx, struct vnode *vp, const struct timespec *ts, int nullflag); +static int sync_callback(mount_t, void *); +static int munge_statfs(struct mount *mp, struct vfsstatfs *sfsp, + user_addr_t bufp, int *sizep, boolean_t is_64_bit, + boolean_t partial_copy); + +__private_extern__ int sync_internal(void); + +#ifdef __APPLE_API_OBSOLETE +struct fstatv_args { + int fd; /* file descriptor of the target file */ + struct vstat *vsb; /* vstat structure for returned info */ +}; +struct lstatv_args { + const char *path; /* pathname of the target file */ + struct vstat *vsb; /* vstat structure for returned info */ +}; +struct mkcomplex_args { + const char *path; /* pathname of the file to be created */ + mode_t mode; /* access mode for the newly created file */ + u_long type; /* format of the complex file */ +}; +struct statv_args { + const char *path; /* pathname of the target file */ + struct vstat *vsb; /* vstat structure for returned info */ +}; + +int fstatv(struct proc *p, struct fstatv_args *uap, register_t *retval); +int lstatv(struct proc *p, struct lstatv_args *uap, register_t *retval); +int mkcomplex(struct proc *p, struct mkcomplex_args *uap, register_t *retval); +int statv(struct proc *p, struct statv_args *uap, register_t *retval); + +#endif /* __APPLE_API_OBSOLETE */ + +#if UNION +extern int (**union_vnodeop_p)(void *); +extern struct vnode *union_dircache(struct vnode*, struct proc*); +#endif /* UNION */ /* counts number of mount and unmount operations */ unsigned int vfs_nummntops=0; +extern struct fileops vnops; + +extern void mount_list_add(mount_t mp); +extern void mount_list_remove(mount_t mp); +extern int mount_refdrain(mount_t mp); +extern int vcount(struct vnode *vp); + + /* * Virtual File System System Calls */ @@ -108,36 +174,41 @@ unsigned int vfs_nummntops=0; /* * Mount a file system. */ -struct mount_args { - char *type; - char *path; - int flags; - caddr_t data; -}; /* ARGSUSED */ int -mount(p, uap, retval) - struct proc *p; - register struct mount_args *uap; - register_t *retval; +mount(struct proc *p, register struct mount_args *uap, __unused register_t *retval) { struct vnode *vp; + struct vnode *devvp = NULLVP; + struct vnode *device_vnode = NULLVP; struct mount *mp; - struct vfsconf *vfsp; - int error, flag, err2; - struct vattr va; - u_long fstypenum; + struct vfstable *vfsp; + int error, flag = 0; + struct vnode_attr va; + struct vfs_context context; struct nameidata nd; + struct nameidata nd1; char fstypename[MFSNAMELEN]; size_t dummy=0; + user_addr_t devpath = USER_ADDR_NULL; + user_addr_t fsmountargs = uap->data; + int ronly = 0; + int mntalloc = 0; + mode_t accessmode; + boolean_t is_64bit; + boolean_t is_rwlock_locked = FALSE; AUDIT_ARG(fflags, uap->flags); + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + is_64bit = proc_is64bit(p); + /* * Get vnode to be covered */ - NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNPATH1, UIO_USERSPACE, - uap->path, p); + NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNPATH1, + UIO_USERSPACE, uap->path, &context); error = namei(&nd); if (error) return (error); @@ -149,46 +220,43 @@ mount(p, uap, retval) if (uap->flags & MNT_UPDATE) { if ((vp->v_flag & VROOT) == 0) { - vput(vp); - return (EINVAL); + error = EINVAL; + goto out1; } mp = vp->v_mount; - if (vfs_busy(mp, LK_NOWAIT, 0, p)) { - vput(vp); - return (EBUSY); + /* unmount in progress return error */ + mount_lock(mp); + if (mp->mnt_lflag & MNT_LUNMOUNT) { + mount_unlock(mp); + error = EBUSY; + goto out1; } + mount_unlock(mp); + lck_rw_lock_exclusive(&mp->mnt_rwlock); + is_rwlock_locked = TRUE; /* * We only allow the filesystem to be reloaded if it * is currently mounted read-only. */ if ((uap->flags & MNT_RELOAD) && ((mp->mnt_flag & MNT_RDONLY) == 0)) { - vfs_unbusy(mp, p); - vput(vp); - return (EOPNOTSUPP); /* Needs translation */ + error = ENOTSUP; + goto out1; } /* * Only root, or the user that did the original mount is * permitted to update it. */ - if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid && - (error = suser(p->p_ucred, &p->p_acflag))) { - vfs_unbusy(mp, p); - vput(vp); - return (error); + if (mp->mnt_vfsstat.f_owner != kauth_cred_getuid(context.vc_ucred) && + (error = suser(context.vc_ucred, &p->p_acflag))) { + goto out1; } /* - * Do not allow NFS export by non-root users. FOr non-root - * users, silently enforce MNT_NOSUID and MNT_NODEV, and - * MNT_NOEXEC if mount point is already MNT_NOEXEC. + * For non-root users, silently enforce MNT_NOSUID and MNT_NODEV, + * and MNT_NOEXEC if mount point is already MNT_NOEXEC. */ - if (p->p_ucred->cr_uid != 0) { - if (uap->flags & MNT_EXPORTED) { - vfs_unbusy(mp, p); - vput(vp); - return (EPERM); - } + if (suser(context.vc_ucred, NULL)) { uap->flags |= MNT_NOSUID | MNT_NODEV; if (mp->mnt_flag & MNT_NOEXEC) uap->flags |= MNT_NOEXEC; @@ -198,81 +266,58 @@ mount(p, uap, retval) mp->mnt_flag |= uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE); - VOP_UNLOCK(vp, 0, p); - + vfsp = mp->mnt_vtable; goto update; } /* * If the user is not root, ensure that they own the directory * onto which we are attempting to mount. */ - if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) || - (va.va_uid != p->p_ucred->cr_uid && - (error = suser(p->p_ucred, &p->p_acflag)))) { - vput(vp); - return (error); + VATTR_INIT(&va); + VATTR_WANTED(&va, va_uid); + if ((error = vnode_getattr(vp, &va, &context)) || + (va.va_uid != kauth_cred_getuid(context.vc_ucred) && + (error = suser(context.vc_ucred, &p->p_acflag)))) { + goto out1; } /* - * Do not allow NFS export by non-root users. FOr non-root - * users, silently enforce MNT_NOSUID and MNT_NODEV, and + * For non-root users, silently enforce MNT_NOSUID and MNT_NODEV, and * MNT_NOEXEC if mount point is already MNT_NOEXEC. */ - if (p->p_ucred->cr_uid != 0) { - if (uap->flags & MNT_EXPORTED) { - vput(vp); - return (EPERM); - } + if (suser(context.vc_ucred, NULL)) { uap->flags |= MNT_NOSUID | MNT_NODEV; if (vp->v_mount->mnt_flag & MNT_NOEXEC) uap->flags |= MNT_NOEXEC; } - if (error = vinvalbuf(vp, V_SAVE, p->p_ucred, p, 0, 0)) { - vput(vp); - return (error); - } + if ( (error = VNOP_FSYNC(vp, MNT_WAIT, &context)) ) + goto out1; + + if ( (error = buf_invalidateblks(vp, BUF_WRITE_DATA, 0, 0)) ) + goto out1; + if (vp->v_type != VDIR) { - vput(vp); - return (ENOTDIR); - } -#if COMPAT_43 - /* - * Historically filesystem types were identified by number. If we - * get an integer for the filesystem type instead of a string, we - * check to see if it matches one of the historic filesystem types. - */ - fstypenum = (u_long)uap->type; - if (fstypenum < maxvfsconf) { - for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) - if (vfsp->vfc_typenum == fstypenum) - break; - if (vfsp == NULL) { - vput(vp); - return (ENODEV); - } - strncpy(fstypename, vfsp->vfc_name, MFSNAMELEN); - } else -#endif /* COMPAT_43 */ - if (error = copyinstr(uap->type, fstypename, MFSNAMELEN, &dummy)) { - vput(vp); - return (error); + error = ENOTDIR; + goto out1; } + if ( (error = copyinstr(uap->type, fstypename, MFSNAMELEN, &dummy)) ) + goto out1; + /* XXXAUDIT: Should we capture the type on the error path as well? */ AUDIT_ARG(text, fstypename); + mount_list_lock(); for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) if (!strcmp(vfsp->vfc_name, fstypename)) break; + mount_list_unlock(); if (vfsp == NULL) { - vput(vp); - return (ENODEV); + error = ENODEV; + goto out1; } - simple_lock(&vp->v_interlock); if (ISSET(vp->v_flag, VMOUNT) && (vp->v_mountedhere != NULL)) { - simple_unlock(&vp->v_interlock); - vput(vp); - return (EBUSY); + error = EBUSY; + goto out1; } SET(vp->v_flag, VMOUNT); - simple_unlock(&vp->v_interlock); /* * Allocate and initialize the filesystem. @@ -280,23 +325,36 @@ mount(p, uap, retval) MALLOC_ZONE(mp, struct mount *, (u_long)sizeof(struct mount), M_MOUNT, M_WAITOK); bzero((char *)mp, (u_long)sizeof(struct mount)); + mntalloc = 1; /* Initialize the default IO constraints */ mp->mnt_maxreadcnt = mp->mnt_maxwritecnt = MAXPHYS; mp->mnt_segreadcnt = mp->mnt_segwritecnt = 32; - - lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0); - (void)vfs_busy(mp, LK_NOWAIT, 0, p); + mp->mnt_maxsegreadsize = mp->mnt_maxreadcnt; + mp->mnt_maxsegwritesize = mp->mnt_maxwritecnt; + mp->mnt_devblocksize = DEV_BSIZE; + + TAILQ_INIT(&mp->mnt_vnodelist); + TAILQ_INIT(&mp->mnt_workerqueue); + TAILQ_INIT(&mp->mnt_newvnodes); + mount_lock_init(mp); + lck_rw_lock_exclusive(&mp->mnt_rwlock); + is_rwlock_locked = TRUE; mp->mnt_op = vfsp->vfc_vfsops; - mp->mnt_vfc = vfsp; + mp->mnt_vtable = vfsp; + mount_list_lock(); vfsp->vfc_refcount++; - mp->mnt_stat.f_type = vfsp->vfc_typenum; + mount_list_unlock(); + //mp->mnt_stat.f_type = vfsp->vfc_typenum; mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK; - strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN); + strncpy(mp->mnt_vfsstat.f_fstypename, vfsp->vfc_name, MFSTYPENAMELEN); + strncpy(mp->mnt_vfsstat.f_mntonname, nd.ni_cnd.cn_pnbuf, MAXPATHLEN); mp->mnt_vnodecovered = vp; - mp->mnt_stat.f_owner = p->p_ucred->cr_uid; - VOP_UNLOCK(vp, 0, p); + mp->mnt_vfsstat.f_owner = kauth_cred_getuid(context.vc_ucred); + /* XXX 3762912 hack to support HFS filesystem 'owner' - filesystem may update later */ + vfs_setowner(mp, KAUTH_UID_NONE, KAUTH_GID_NONE); + update: /* * Set the mount level flags. @@ -310,14 +368,107 @@ update: MNT_UNKNOWNPERMISSIONS | MNT_DONTBROWSE | MNT_AUTOMOUNTED); mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | - MNT_UNKNOWNPERMISSIONS | MNT_DONTBROWSE | MNT_AUTOMOUNTED); + MNT_UNKNOWNPERMISSIONS | MNT_DONTBROWSE | MNT_AUTOMOUNTED | + MNT_DEFWRITE); + + if (vfsp->vfc_vfsflags & VFC_VFSLOCALARGS) { + if (is_64bit) { + if ( (error = copyin(fsmountargs, (caddr_t)&devpath, sizeof(devpath))) ) + goto out1; + fsmountargs += sizeof(devpath); + } else { + char *tmp; + if ( (error = copyin(fsmountargs, (caddr_t)&tmp, sizeof(tmp))) ) + goto out1; + /* munge into LP64 addr */ + devpath = CAST_USER_ADDR_T(tmp); + fsmountargs += sizeof(tmp); + } + + /* if it is not update and device name needs to be parsed */ + if ((devpath)) { + NDINIT(&nd1, LOOKUP, FOLLOW, UIO_USERSPACE, devpath, &context); + if ( (error = namei(&nd1)) ) + goto out1; + + strncpy(mp->mnt_vfsstat.f_mntfromname, nd1.ni_cnd.cn_pnbuf, MAXPATHLEN); + devvp = nd1.ni_vp; + + nameidone(&nd1); + + if (devvp->v_type != VBLK) { + error = ENOTBLK; + goto out2; + } + if (major(devvp->v_rdev) >= nblkdev) { + error = ENXIO; + goto out2; + } + /* + * If mount by non-root, then verify that user has necessary + * permissions on the device. + */ + if (suser(context.vc_ucred, NULL) != 0) { + accessmode = KAUTH_VNODE_READ_DATA; + if ((mp->mnt_flag & MNT_RDONLY) == 0) + accessmode |= KAUTH_VNODE_WRITE_DATA; + if ((error = vnode_authorize(devvp, NULL, accessmode, &context)) != 0) + goto out2; + } + } + if (devpath && ((uap->flags & MNT_UPDATE) == 0)) { + if ( (error = vnode_ref(devvp)) ) + goto out2; + /* + * Disallow multiple mounts of the same device. + * Disallow mounting of a device that is currently in use + * (except for root, which might share swap device for miniroot). + * Flush out any old buffers remaining from a previous use. + */ + if ( (error = vfs_mountedon(devvp)) ) + goto out3; + + if (vcount(devvp) > 1 && !(vfs_flags(mp) & MNT_ROOTFS)) { + error = EBUSY; + goto out3; + } + if ( (error = VNOP_FSYNC(devvp, MNT_WAIT, &context)) ) { + error = ENOTBLK; + goto out3; + } + if ( (error = buf_invalidateblks(devvp, BUF_WRITE_DATA, 0, 0)) ) + goto out3; + + ronly = (mp->mnt_flag & MNT_RDONLY) != 0; + if ( (error = VNOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, &context)) ) + goto out3; + + mp->mnt_devvp = devvp; + device_vnode = devvp; + } else { + if ((mp->mnt_flag & MNT_RDONLY) && (mp->mnt_kern_flag & MNTK_WANTRDWR)) { + /* + * If upgrade to read-write by non-root, then verify + * that user has necessary permissions on the device. + */ + device_vnode = mp->mnt_devvp; + if (device_vnode && suser(context.vc_ucred, NULL)) { + if ((error = vnode_authorize(device_vnode, NULL, + KAUTH_VNODE_READ_DATA | KAUTH_VNODE_WRITE_DATA, &context)) != 0) + goto out2; + } + } + device_vnode = NULLVP; + } + } + + /* * Mount the filesystem. */ - error = VFS_MOUNT(mp, uap->path, uap->data, &nd, p); + error = VFS_MOUNT(mp, device_vnode, fsmountargs, &context); if (uap->flags & MNT_UPDATE) { - vrele(vp); if (mp->mnt_kern_flag & MNTK_WANTRDWR) mp->mnt_flag &= ~MNT_RDONLY; mp->mnt_flag &=~ @@ -325,73 +476,109 @@ update: mp->mnt_kern_flag &=~ MNTK_WANTRDWR; if (error) mp->mnt_flag = flag; - vfs_unbusy(mp, p); + vfs_event_signal(NULL, VQ_UPDATE, (intptr_t)NULL); + lck_rw_done(&mp->mnt_rwlock); + is_rwlock_locked = FALSE; if (!error) - enablequotas(p, mp); - return (error); + enablequotas(mp,&context); + goto out2; } - - /* get the vnode lock */ - err2 = vn_lock(vp, LK_EXCLUSIVE|LK_RETRY, p); - /* * Put the new filesystem on the mount list after root. */ - cache_purge(vp); - if (!error && !err2) { - simple_lock(&vp->v_interlock); + if (!error) { CLR(vp->v_flag, VMOUNT); - vp->v_mountedhere =mp; - simple_unlock(&vp->v_interlock); - simple_lock(&mountlist_slock); - CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list); - simple_unlock(&mountlist_slock); - vfs_event_signal(NULL, VQ_MOUNT, NULL); - checkdirs(vp); - VOP_UNLOCK(vp, 0, p); - vfs_unbusy(mp, p); - if (error = VFS_START(mp, 0, p)) - vrele(vp); + + vnode_lock(vp); + vp->v_mountedhere = mp; + vnode_unlock(vp); + + vnode_ref(vp); + + vfs_event_signal(NULL, VQ_MOUNT, (intptr_t)NULL); + checkdirs(vp, &context); + lck_rw_done(&mp->mnt_rwlock); + is_rwlock_locked = FALSE; + mount_list_add(mp); + /* + * there is no cleanup code here so I have made it void + * we need to revisit this + */ + (void)VFS_START(mp, 0, &context); /* increment the operations count */ - if (!error) { - vfs_nummntops++; - enablequotas(p, mp); - } + OSAddAtomic(1, (SInt32 *)&vfs_nummntops); + enablequotas(mp,&context); + + if (device_vnode) { + device_vnode->v_specflags |= SI_MOUNTEDON; + + /* + * cache the IO attributes for the underlying physical media... + * an error return indicates the underlying driver doesn't + * support all the queries necessary... however, reasonable + * defaults will have been set, so no reason to bail or care + */ + vfs_init_io_attributes(device_vnode, mp); + } } else { - simple_lock(&vp->v_interlock); CLR(vp->v_flag, VMOUNT); - simple_unlock(&vp->v_interlock); - mp->mnt_vfc->vfc_refcount--; + mount_list_lock(); + mp->mnt_vtable->vfc_refcount--; + mount_list_unlock(); - if (mp->mnt_kern_flag & MNTK_IO_XINFO) - FREE(mp->mnt_xinfo_ptr, M_TEMP); - vfs_unbusy(mp, p); + if (device_vnode ) { + VNOP_CLOSE(device_vnode, ronly ? FREAD : FREAD|FWRITE, &context); + vnode_rele(device_vnode); + } + lck_rw_done(&mp->mnt_rwlock); + is_rwlock_locked = FALSE; + mount_lock_destroy(mp); FREE_ZONE((caddr_t)mp, sizeof (struct mount), M_MOUNT); - if (err2) - vrele(vp); - else - vput(vp); } - return (error); + nameidone(&nd); + + /* + * drop I/O count on covered 'vp' and + * on the device vp if there was one + */ + if (devpath && devvp) + vnode_put(devvp); + vnode_put(vp); + + return(error); + +out3: + vnode_rele(devvp); +out2: + if (devpath && devvp) + vnode_put(devvp); +out1: + /* Release mnt_rwlock only when it was taken */ + if (is_rwlock_locked == TRUE) { + lck_rw_done(&mp->mnt_rwlock); + } + if (mntalloc) + FREE_ZONE((caddr_t)mp, sizeof (struct mount), M_MOUNT); + vnode_put(vp); + nameidone(&nd); + + return(error); + } -static void -enablequotas(p, mp) - struct proc *p; - struct mount *mp; +void +enablequotas(struct mount *mp, vfs_context_t context) { - struct vnode *vp; struct nameidata qnd; int type; char qfpath[MAXPATHLEN]; - char *qfname = QUOTAFILENAME; - char *qfopsname = QUOTAOPSNAME; - char *qfextension[] = INITQFNAMES; - + const char *qfname = QUOTAFILENAME; + const char *qfopsname = QUOTAOPSNAME; + const char *qfextension[] = INITQFNAMES; - if ((strcmp(mp->mnt_stat.f_fstypename, "hfs") != 0 ) - && (strcmp( mp->mnt_stat.f_fstypename, "ufs") != 0)) + if ((strcmp(mp->mnt_vfsstat.f_fstypename, "hfs") != 0 ) + && (strcmp( mp->mnt_vfsstat.f_fstypename, "ufs") != 0)) return; /* @@ -399,21 +586,15 @@ enablequotas(p, mp) * We ignore errors as this should not interfere with final mount */ for (type=0; type < MAXQUOTAS; type++) { - sprintf(qfpath, "%s/%s.%s", mp->mnt_stat.f_mntonname, qfopsname, qfextension[type]); - NDINIT(&qnd, LOOKUP, FOLLOW, UIO_SYSSPACE, qfpath, p); - if (namei(&qnd) != 0) - continue; /* option file to trigger quotas is not present */ - vp = qnd.ni_vp; - sprintf(qfpath, "%s/%s.%s", mp->mnt_stat.f_mntonname, qfname, qfextension[type]); - if (vp->v_tag == VT_HFS) { - vrele(vp); - (void)hfs_quotaon(p, mp, type, qfpath, UIO_SYSSPACE); - } else if (vp->v_tag == VT_UFS) { - vrele(vp); - (void)quotaon(p, mp, type, qfpath, UIO_SYSSPACE); - } else { - vrele(vp); - } + sprintf(qfpath, "%s/%s.%s", mp->mnt_vfsstat.f_mntonname, qfopsname, qfextension[type]); + NDINIT(&qnd, LOOKUP, FOLLOW, UIO_SYSSPACE32, CAST_USER_ADDR_T(qfpath), context); + if (namei(&qnd) != 0) + continue; /* option file to trigger quotas is not present */ + vnode_put(qnd.ni_vp); + nameidone(&qnd); + sprintf(qfpath, "%s/%s.%s", mp->mnt_vfsstat.f_mntonname, qfname, qfextension[type]); + + (void) VFS_QUOTACTL(mp, QCMD(Q_QUOTAON, type), 0, qfpath, context); } return; } @@ -424,40 +605,67 @@ enablequotas(p, mp) * mounted. If so, replace them with the new mount point. */ static void -checkdirs(olddp) +checkdirs(olddp, context) struct vnode *olddp; + vfs_context_t context; { struct filedesc *fdp; struct vnode *newdp; struct proc *p; struct vnode *tvp; + struct vnode *fdp_cvp; + struct vnode *fdp_rvp; + int cdir_changed = 0; + int rdir_changed = 0; + boolean_t funnel_state; if (olddp->v_usecount == 1) return; - if (VFS_ROOT(olddp->v_mountedhere, &newdp)) + if (VFS_ROOT(olddp->v_mountedhere, &newdp, context)) panic("mount: lost mount"); + funnel_state = thread_funnel_set(kernel_flock, TRUE); + for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { + proc_fdlock(p); fdp = p->p_fd; - if (fdp->fd_cdir == olddp) { - VREF(newdp); + if (fdp == (struct filedesc *)0) { + proc_fdunlock(p); + continue; + } + fdp_cvp = fdp->fd_cdir; + fdp_rvp = fdp->fd_rdir; + proc_fdunlock(p); + + if (fdp_cvp == olddp) { + vnode_ref(newdp); tvp = fdp->fd_cdir; - fdp->fd_cdir = newdp; - vrele(tvp); + fdp_cvp = newdp; + cdir_changed = 1; + vnode_rele(tvp); } - if (fdp->fd_rdir == olddp) { - VREF(newdp); + if (fdp_rvp == olddp) { + vnode_ref(newdp); tvp = fdp->fd_rdir; - fdp->fd_rdir = newdp; - vrele(tvp); + fdp_rvp = newdp; + rdir_changed = 1; + vnode_rele(tvp); + } + if (cdir_changed || rdir_changed) { + proc_fdlock(p); + fdp->fd_cdir = fdp_cvp; + fdp->fd_rdir = fdp_rvp; + proc_fdunlock(p); } } if (rootvnode == olddp) { - VREF(newdp); + vnode_ref(newdp); tvp = rootvnode; rootvnode = newdp; - vrele(tvp); + vnode_rele(tvp); } - vput(newdp); + thread_funnel_set(kernel_flock, funnel_state); + + vnode_put(newdp); } /* @@ -466,43 +674,43 @@ checkdirs(olddp) * Note: unmount takes a path to the vnode mounted on as argument, * not special file (as before). */ -struct unmount_args { - char *path; - int flags; -}; /* ARGSUSED */ int -unmount(p, uap, retval) - struct proc *p; - register struct unmount_args *uap; - register_t *retval; +unmount(struct proc *p, register struct unmount_args *uap, __unused register_t *retval) { register struct vnode *vp; struct mount *mp; int error; struct nameidata nd; + struct vfs_context context; + + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); - NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNPATH1, UIO_USERSPACE, - uap->path, p); + NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNPATH1, + UIO_USERSPACE, uap->path, &context); error = namei(&nd); if (error) return (error); vp = nd.ni_vp; mp = vp->v_mount; + nameidone(&nd); /* * Must be the root of the filesystem */ if ((vp->v_flag & VROOT) == 0) { - vput(vp); + vnode_put(vp); return (EINVAL); } - vput(vp); + vnode_put(vp); return (safedounmount(mp, uap->flags, p)); } /* * Do the actual file system unmount, prevent some common foot shooting. + * + * XXX Should take a "vfs_context_t" instead of a "struct proc *" */ int safedounmount(mp, flags, p) @@ -516,8 +724,8 @@ safedounmount(mp, flags, p) * Only root, or the user that did the original mount is * permitted to unmount this filesystem. */ - if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) && - (error = suser(p->p_ucred, &p->p_acflag))) + if ((mp->mnt_vfsstat.f_owner != kauth_cred_getuid(kauth_cred_get())) && + (error = suser(kauth_cred_get(), &p->p_acflag))) return (error); /* @@ -538,18 +746,27 @@ dounmount(mp, flags, p) int flags; struct proc *p; { - struct vnode *coveredvp; + struct vnode *coveredvp = (vnode_t)0; int error; + int needwakeup = 0; + struct vfs_context context; + int forcedunmount = 0; + int lflags = 0; + + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); - simple_lock(&mountlist_slock); + if (flags & MNT_FORCE) + forcedunmount = 1; + mount_lock(mp); /* XXX post jaguar fix LK_DRAIN - then clean this up */ - if ((flags & MNT_FORCE)) + if ((flags & MNT_FORCE)) { mp->mnt_kern_flag |= MNTK_FRCUNMOUNT; - if (mp->mnt_kern_flag & MNTK_UNMOUNT) { - simple_unlock(&mountlist_slock); - mp->mnt_kern_flag |= MNTK_MWAIT; - if ((error = tsleep((void *)mp, PRIBIO, "dounmount", 0))) - return (error); + mp->mnt_lflag |= MNT_LFORCE; + } + if (mp->mnt_lflag & MNT_LUNMOUNT) { + mp->mnt_lflag |= MNT_LWAIT; + msleep((caddr_t)mp, &mp->mnt_mlock, (PVFS | PDROP), "dounmount", 0 ); /* * The prior unmount attempt has probably succeeded. * Do not dereference mp here - returning EBUSY is safest. @@ -557,54 +774,149 @@ dounmount(mp, flags, p) return (EBUSY); } mp->mnt_kern_flag |= MNTK_UNMOUNT; - error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK, - &mountlist_slock, p); - if (error) { + mp->mnt_lflag |= MNT_LUNMOUNT; + mp->mnt_flag &=~ MNT_ASYNC; + mount_unlock(mp); + lck_rw_lock_exclusive(&mp->mnt_rwlock); + fsevent_unmount(mp); /* has to come first! */ + error = 0; + if (forcedunmount == 0) { + ubc_umount(mp); /* release cached vnodes */ + if ((mp->mnt_flag & MNT_RDONLY) == 0) { + error = VFS_SYNC(mp, MNT_WAIT, &context); + if (error) { + mount_lock(mp); + mp->mnt_kern_flag &= ~MNTK_UNMOUNT; + mp->mnt_lflag &= ~MNT_LUNMOUNT; + mp->mnt_lflag &= ~MNT_LFORCE; + goto out; + } + } + } + + if (forcedunmount) + lflags |= FORCECLOSE; + error = vflush(mp, NULLVP, SKIPSWAP | SKIPSYSTEM | SKIPROOT | lflags); + if ((forcedunmount == 0) && error) { + mount_lock(mp); mp->mnt_kern_flag &= ~MNTK_UNMOUNT; + mp->mnt_lflag &= ~MNT_LUNMOUNT; + mp->mnt_lflag &= ~MNT_LFORCE; goto out; } - mp->mnt_flag &=~ MNT_ASYNC; - ubc_umount(mp); /* release cached vnodes */ - cache_purgevfs(mp); /* remove cache entries for this file sys */ - if (((mp->mnt_flag & MNT_RDONLY) || - (error = VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p)) == 0) || - (flags & MNT_FORCE)) - error = VFS_UNMOUNT(mp, flags, p); - simple_lock(&mountlist_slock); + + /* make sure there are no one in the mount iterations or lookup */ + mount_iterdrain(mp); + + error = VFS_UNMOUNT(mp, flags, &context); if (error) { + mount_iterreset(mp); + mount_lock(mp); mp->mnt_kern_flag &= ~MNTK_UNMOUNT; - lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE, - &mountlist_slock, p); + mp->mnt_lflag &= ~MNT_LUNMOUNT; + mp->mnt_lflag &= ~MNT_LFORCE; goto out; } /* increment the operations count */ if (!error) - vfs_nummntops++; - CIRCLEQ_REMOVE(&mountlist, mp, mnt_list); + OSAddAtomic(1, (SInt32 *)&vfs_nummntops); + + if ( mp->mnt_devvp && mp->mnt_vtable->vfc_vfsflags & VFC_VFSLOCALARGS) { + mp->mnt_devvp->v_specflags &= ~SI_MOUNTEDON; + VNOP_CLOSE(mp->mnt_devvp, mp->mnt_flag & MNT_RDONLY ? FREAD : FREAD|FWRITE, + &context); + vnode_rele(mp->mnt_devvp); + } + lck_rw_done(&mp->mnt_rwlock); + mount_list_remove(mp); + lck_rw_lock_exclusive(&mp->mnt_rwlock); + + /* mark the mount point hook in the vp but not drop the ref yet */ if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) { - coveredvp->v_mountedhere = (struct mount *)0; - simple_unlock(&mountlist_slock); - vrele(coveredvp); - simple_lock(&mountlist_slock); + vnode_getwithref(coveredvp); + vnode_lock(coveredvp); + coveredvp->v_mountedhere = (struct mount *)0; + vnode_unlock(coveredvp); + vnode_put(coveredvp); } - mp->mnt_vfc->vfc_refcount--; - if (mp->mnt_vnodelist.lh_first != NULL) { - panic("unmount: dangling vnode"); + + mount_list_lock(); + mp->mnt_vtable->vfc_refcount--; + mount_list_unlock(); + + cache_purgevfs(mp); /* remove cache entries for this file sys */ + vfs_event_signal(NULL, VQ_UNMOUNT, (intptr_t)NULL); + mount_lock(mp); + mp->mnt_lflag |= MNT_LDEAD; + + if (mp->mnt_lflag & MNT_LWAIT) { + /* + * do the wakeup here + * in case we block in mount_refdrain + * which will drop the mount lock + * and allow anyone blocked in vfs_busy + * to wakeup and see the LDEAD state + */ + mp->mnt_lflag &= ~MNT_LWAIT; + wakeup((caddr_t)mp); } - lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &mountlist_slock, p); - vfs_event_signal(NULL, VQ_UNMOUNT, NULL); + mount_refdrain(mp); out: - if (mp->mnt_kern_flag & MNTK_MWAIT) + if (mp->mnt_lflag & MNT_LWAIT) { + mp->mnt_lflag &= ~MNT_LWAIT; + needwakeup = 1; + } + mount_unlock(mp); + lck_rw_done(&mp->mnt_rwlock); + + if (needwakeup) wakeup((caddr_t)mp); if (!error) { - if (mp->mnt_kern_flag & MNTK_IO_XINFO) - FREE(mp->mnt_xinfo_ptr, M_TEMP); - FREE_ZONE((caddr_t)mp, sizeof (struct mount), M_MOUNT); + if ((coveredvp != NULLVP)) { + vnode_getwithref(coveredvp); + vnode_rele(coveredvp); + vnode_lock(coveredvp); + if(mp->mnt_crossref == 0) { + vnode_unlock(coveredvp); + mount_lock_destroy(mp); + FREE_ZONE((caddr_t)mp, sizeof (struct mount), M_MOUNT); + } else { + coveredvp->v_lflag |= VL_MOUNTDEAD; + vnode_unlock(coveredvp); + } + vnode_put(coveredvp); + } else if (mp->mnt_flag & MNT_ROOTFS) { + mount_lock_destroy(mp); + FREE_ZONE((caddr_t)mp, sizeof (struct mount), M_MOUNT); + } else + panic("dounmount: no coveredvp"); } return (error); } +void +mount_dropcrossref(mount_t mp, vnode_t dp, int need_put) +{ + vnode_lock(dp); + mp->mnt_crossref--; + if (mp->mnt_crossref < 0) + panic("mount cross refs -ve"); + if (((dp->v_lflag & VL_MOUNTDEAD) == VL_MOUNTDEAD) && (mp->mnt_crossref == 0)) { + dp->v_lflag &= ~VL_MOUNTDEAD; + if (need_put) + vnode_put_locked(dp); + vnode_unlock(dp); + mount_lock_destroy(mp); + FREE_ZONE((caddr_t)mp, sizeof (struct mount), M_MOUNT); + return; + } + if (need_put) + vnode_put_locked(dp); + vnode_unlock(dp); +} + + /* * Sync each mounted filesystem. */ @@ -613,44 +925,39 @@ int syncprt = 0; struct ctldebug debug0 = { "syncprt", &syncprt }; #endif -struct sync_args { - int dummy; -}; int print_vmpage_stat=0; -/* ARGSUSED */ -int -sync(p, uap, retval) - struct proc *p; - struct sync_args *uap; - register_t *retval; +static int +sync_callback(mount_t mp, __unused void * arg) { - register struct mount *mp, *nmp; + struct proc * p = current_proc(); int asyncflag; + struct vfs_context context; - simple_lock(&mountlist_slock); - for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) { - if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) { - nmp = mp->mnt_list.cqe_next; - continue; - } - if ((mp->mnt_flag & MNT_RDONLY) == 0) { + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + + if ((mp->mnt_flag & MNT_RDONLY) == 0) { asyncflag = mp->mnt_flag & MNT_ASYNC; mp->mnt_flag &= ~MNT_ASYNC; - VFS_SYNC(mp, MNT_NOWAIT, p->p_ucred, p); + VFS_SYNC(mp, MNT_NOWAIT, &context); if (asyncflag) mp->mnt_flag |= MNT_ASYNC; - } - simple_lock(&mountlist_slock); - nmp = mp->mnt_list.cqe_next; - vfs_unbusy(mp, p); } - simple_unlock(&mountlist_slock); + return(VFS_RETURNED); +} + + +extern unsigned int vp_pagein, vp_pgodirty, vp_pgoclean; +extern unsigned int dp_pgins, dp_pgouts; + +/* ARGSUSED */ +int +sync(__unused struct proc *p, __unused struct sync_args *uap, __unused register_t *retval) +{ + vfs_iterate(LK_NOWAIT, sync_callback, (void *)0); { - extern void vm_countdirtypages(void); - extern unsigned int vp_pagein, vp_pgodirty, vp_pgoclean; - extern unsigned int dp_pgins, dp_pgouts; if(print_vmpage_stat) { vm_countdirtypages(); printf("VP: %d: %d: %d: %d: %d\n", vp_pgodirty, vp_pgoclean, vp_pagein, @@ -667,164 +974,267 @@ sync(p, uap, retval) /* * Change filesystem quotas. */ -struct quotactl_args { - char *path; - int cmd; - int uid; - caddr_t arg; -}; /* ARGSUSED */ int -quotactl(p, uap, retval) - struct proc *p; - register struct quotactl_args *uap; - register_t *retval; +quotactl(struct proc *p, register struct quotactl_args *uap, __unused register_t *retval) { register struct mount *mp; - int error; + int error, quota_cmd, quota_status; + caddr_t datap; + size_t fnamelen; struct nameidata nd; + struct vfs_context context; + struct dqblk my_dqblk; + + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); AUDIT_ARG(uid, uap->uid, 0, 0, 0); AUDIT_ARG(cmd, uap->cmd); - NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNPATH1, UIO_USERSPACE, uap->path, p); + NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNPATH1, + UIO_USERSPACE, uap->path, &context); error = namei(&nd); if (error) return (error); mp = nd.ni_vp->v_mount; - vrele(nd.ni_vp); - return (VFS_QUOTACTL(mp, uap->cmd, uap->uid, - uap->arg, p)); + vnode_put(nd.ni_vp); + nameidone(&nd); + + /* copyin any data we will need for downstream code */ + quota_cmd = uap->cmd >> SUBCMDSHIFT; + + switch (quota_cmd) { + case Q_QUOTAON: + /* uap->arg specifies a file from which to take the quotas */ + fnamelen = MAXPATHLEN; + datap = kalloc(MAXPATHLEN); + error = copyinstr(uap->arg, datap, MAXPATHLEN, &fnamelen); + break; + case Q_GETQUOTA: + /* uap->arg is a pointer to a dqblk structure. */ + datap = (caddr_t) &my_dqblk; + break; + case Q_SETQUOTA: + case Q_SETUSE: + /* uap->arg is a pointer to a dqblk structure. */ + datap = (caddr_t) &my_dqblk; + if (proc_is64bit(p)) { + struct user_dqblk my_dqblk64; + error = copyin(uap->arg, (caddr_t)&my_dqblk64, sizeof (my_dqblk64)); + if (error == 0) { + munge_dqblk(&my_dqblk, &my_dqblk64, FALSE); + } + } + else { + error = copyin(uap->arg, (caddr_t)&my_dqblk, sizeof (my_dqblk)); + } + break; + case Q_QUOTASTAT: + /* uap->arg is a pointer to an integer */ + datap = (caddr_t) "a_status; + break; + default: + datap = NULL; + break; + } /* switch */ + + if (error == 0) { + error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, datap, &context); + } + + switch (quota_cmd) { + case Q_QUOTAON: + if (datap != NULL) + kfree(datap, MAXPATHLEN); + break; + case Q_GETQUOTA: + /* uap->arg is a pointer to a dqblk structure we need to copy out to */ + if (error == 0) { + if (proc_is64bit(p)) { + struct user_dqblk my_dqblk64; + munge_dqblk(&my_dqblk, &my_dqblk64, TRUE); + error = copyout((caddr_t)&my_dqblk64, uap->arg, sizeof (my_dqblk64)); + } + else { + error = copyout(datap, uap->arg, sizeof (struct dqblk)); + } + } + break; + case Q_QUOTASTAT: + /* uap->arg is a pointer to an integer */ + if (error == 0) { + error = copyout(datap, uap->arg, sizeof(quota_status)); + } + break; + default: + break; + } /* switch */ + + return (error); } /* * Get filesystem statistics. */ -struct statfs_args { - char *path; - struct statfs *buf; -}; /* ARGSUSED */ int -statfs(p, uap, retval) - struct proc *p; - register struct statfs_args *uap; - register_t *retval; +statfs(struct proc *p, register struct statfs_args *uap, __unused register_t *retval) { - register struct mount *mp; - register struct statfs *sp; + struct mount *mp; + struct vfsstatfs *sp; int error; struct nameidata nd; + struct vfs_context context; + vnode_t vp; - NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNPATH1, UIO_USERSPACE, uap->path, p); + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + + NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNPATH1, + UIO_USERSPACE, uap->path, &context); error = namei(&nd); if (error) return (error); - mp = nd.ni_vp->v_mount; - sp = &mp->mnt_stat; - vrele(nd.ni_vp); - if (error = VFS_STATFS(mp, sp, p)) + vp = nd.ni_vp; + mp = vp->v_mount; + sp = &mp->mnt_vfsstat; + nameidone(&nd); + + error = vfs_update_vfsstat(mp, &context); + vnode_put(vp); + if (error != 0) return (error); - sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; - return (copyout((caddr_t)sp, (caddr_t)uap->buf, - sizeof(*sp)-sizeof(sp->f_reserved3)-sizeof(sp->f_reserved4))); + + error = munge_statfs(mp, sp, uap->buf, NULL, IS_64BIT_PROCESS(p), TRUE); + return (error); } /* * Get filesystem statistics. */ -struct fstatfs_args { - int fd; - struct statfs *buf; -}; /* ARGSUSED */ int -fstatfs(p, uap, retval) - struct proc *p; - register struct fstatfs_args *uap; - register_t *retval; +fstatfs(struct proc *p, register struct fstatfs_args *uap, __unused register_t *retval) { - struct file *fp; + struct vnode *vp; struct mount *mp; - register struct statfs *sp; + struct vfsstatfs *sp; int error; + struct vfs_context context; + + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); AUDIT_ARG(fd, uap->fd); - if (error = getvnode(p, uap->fd, &fp)) + if ( (error = file_vnode(uap->fd, &vp)) ) return (error); - AUDIT_ARG(vnpath, (struct vnode *)fp->f_data, ARG_VNODE1); + AUDIT_ARG(vnpath_withref, vp, ARG_VNODE1); - mp = ((struct vnode *)fp->f_data)->v_mount; - if (!mp) + mp = vp->v_mount; + if (!mp) { + file_drop(uap->fd); return (EBADF); - sp = &mp->mnt_stat; - if (error = VFS_STATFS(mp, sp, p)) + } + sp = &mp->mnt_vfsstat; + if ((error = vfs_update_vfsstat(mp, &context)) != 0) { + file_drop(uap->fd); return (error); - sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; - return (copyout((caddr_t)sp, (caddr_t)uap->buf, - sizeof(*sp)-sizeof(sp->f_reserved3)-sizeof(sp->f_reserved4))); + } + file_drop(uap->fd); + + error = munge_statfs(mp, sp, uap->buf, NULL, IS_64BIT_PROCESS(p), TRUE); + + return (error); } -/* - * Get statistics on all filesystems. - */ -struct getfsstat_args { - struct statfs *buf; - long bufsize; - int flags; + +struct getfsstat_struct { + user_addr_t sfsp; + int count; + int maxcount; + int flags; + int error; }; -int -getfsstat(p, uap, retval) - struct proc *p; - register struct getfsstat_args *uap; - register_t *retval; + + +static int +getfsstat_callback(mount_t mp, void * arg) { - register struct mount *mp, *nmp; - register struct statfs *sp; - caddr_t sfsp; - long count, maxcount, error; + + struct getfsstat_struct *fstp = (struct getfsstat_struct *)arg; + struct vfsstatfs *sp; + struct proc * p = current_proc(); + int error, my_size; + struct vfs_context context; - maxcount = uap->bufsize / sizeof(struct statfs); - sfsp = (caddr_t)uap->buf; - count = 0; - simple_lock(&mountlist_slock); - for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) { - if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) { - nmp = mp->mnt_list.cqe_next; - continue; + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + + if (fstp->sfsp && fstp->count < fstp->maxcount) { + sp = &mp->mnt_vfsstat; + /* + * If MNT_NOWAIT is specified, do not refresh the + * fsstat cache. MNT_WAIT overrides MNT_NOWAIT. + */ + if (((fstp->flags & MNT_NOWAIT) == 0 || (fstp->flags & MNT_WAIT)) && + (error = vfs_update_vfsstat(mp, &context))) { + KAUTH_DEBUG("vfs_update_vfsstat returned %d", error); + return(VFS_RETURNED); } - if (sfsp && count < maxcount) { - sp = &mp->mnt_stat; - /* - * If MNT_NOWAIT is specified, do not refresh the - * fsstat cache. MNT_WAIT overrides MNT_NOWAIT. - */ - if (((uap->flags & MNT_NOWAIT) == 0 || - (uap->flags & MNT_WAIT)) && - (error = VFS_STATFS(mp, sp, p))) { - simple_lock(&mountlist_slock); - nmp = mp->mnt_list.cqe_next; - vfs_unbusy(mp, p); - continue; - } - sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; - if (error = copyout((caddr_t)sp, sfsp, sizeof(*sp))) { - vfs_unbusy(mp, p); - return (error); - } - sfsp += sizeof(*sp); + + /* + * Need to handle LP64 version of struct statfs + */ + error = munge_statfs(mp, sp, fstp->sfsp, &my_size, IS_64BIT_PROCESS(p), FALSE); + if (error) { + fstp->error = error; + return(VFS_RETURNED_DONE); } - count++; - simple_lock(&mountlist_slock); - nmp = mp->mnt_list.cqe_next; - vfs_unbusy(mp, p); - } - simple_unlock(&mountlist_slock); - if (sfsp && count > maxcount) - *retval = maxcount; + fstp->sfsp += my_size; + } + fstp->count++; + return(VFS_RETURNED); +} + +/* + * Get statistics on all filesystems. + */ +int +getfsstat(__unused proc_t p, struct getfsstat_args *uap, int *retval) +{ + user_addr_t sfsp; + int count, maxcount; + struct getfsstat_struct fst; + + if (IS_64BIT_PROCESS(p)) { + maxcount = uap->bufsize / sizeof(struct user_statfs); + } + else { + maxcount = uap->bufsize / sizeof(struct statfs); + } + sfsp = uap->buf; + count = 0; + + fst.sfsp = sfsp; + fst.flags = uap->flags; + fst.count = 0; + fst.error = 0; + fst.maxcount = maxcount; + + + vfs_iterate(0, getfsstat_callback, &fst); + + if (fst.error ) { + KAUTH_DEBUG("ERROR - %s gets %d", p->p_comm, fst.error); + return(fst.error); + } + + if (fst.sfsp && fst.count > fst.maxcount) + *retval = fst.maxcount; else - *retval = count; + *retval = fst.count; return (0); } @@ -834,166 +1244,138 @@ ogetfsstat(p, uap, retval) register struct getfsstat_args *uap; register_t *retval; { - register struct mount *mp, *nmp; - register struct statfs *sp; - caddr_t sfsp; - long count, maxcount, error; - - maxcount = uap->bufsize / (sizeof(struct statfs) - sizeof(sp->f_reserved4)); - sfsp = (caddr_t)uap->buf; - count = 0; - simple_lock(&mountlist_slock); - for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) { - if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) { - nmp = mp->mnt_list.cqe_next; - continue; - } - if (sfsp && count < maxcount) { - sp = &mp->mnt_stat; - /* - * If MNT_NOWAIT is specified, do not refresh the - * fsstat cache. MNT_WAIT overrides MNT_NOWAIT. - */ - if (((uap->flags & MNT_NOWAIT) == 0 || - (uap->flags & MNT_WAIT)) && - (error = VFS_STATFS(mp, sp, p))) { - simple_lock(&mountlist_slock); - nmp = mp->mnt_list.cqe_next; - vfs_unbusy(mp, p); - continue; - } - sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; - error = copyout((caddr_t)sp, sfsp, - sizeof(*sp) - sizeof(sp->f_reserved3) - - sizeof(sp->f_reserved4)); - if (error) { - vfs_unbusy(mp, p); - return (error); - } - sfsp += sizeof(*sp) - sizeof(sp->f_reserved4); - } - count++; - simple_lock(&mountlist_slock); - nmp = mp->mnt_list.cqe_next; - vfs_unbusy(mp, p); - } - simple_unlock(&mountlist_slock); - if (sfsp && count > maxcount) - *retval = maxcount; - else - *retval = count; - return (0); + return (ENOTSUP); } #endif /* * Change current working directory to a given file descriptor. */ -struct fchdir_args { - int fd; -}; /* ARGSUSED */ int -fchdir(p, uap, retval) - struct proc *p; - struct fchdir_args *uap; - register_t *retval; +fchdir(struct proc *p, struct fchdir_args *uap, __unused register_t *retval) { register struct filedesc *fdp = p->p_fd; struct vnode *vp, *tdp, *tvp; struct mount *mp; - struct file *fp; int error; + struct vfs_context context; - if (error = getvnode(p, uap->fd, &fp)) - return (error); - vp = (struct vnode *)fp->f_data; - VREF(vp); - vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + + if ( (error = file_vnode(uap->fd, &vp)) ) + return(error); + if ( (error = vnode_getwithref(vp)) ) { + file_drop(uap->fd); + return(error); + } AUDIT_ARG(vnpath, vp, ARG_VNODE1); if (vp->v_type != VDIR) error = ENOTDIR; else - error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p); + error = vnode_authorize(vp, NULL, KAUTH_VNODE_SEARCH, &context); while (!error && (mp = vp->v_mountedhere) != NULL) { - if (vfs_busy(mp, LK_NOWAIT, 0, p)) { - vput(vp); - return (EACCES); + if (vfs_busy(mp, LK_NOWAIT)) { + error = EACCES; + goto out; } - error = VFS_ROOT(mp, &tdp); - vfs_unbusy(mp, p); + error = VFS_ROOT(mp, &tdp, &context); + vfs_unbusy(mp); if (error) break; - vput(vp); + vnode_put(vp); vp = tdp; } - if (error) { - vput(vp); - return (error); - } - VOP_UNLOCK(vp, 0, p); + if (error) + goto out; + if ( (error = vnode_ref(vp)) ) + goto out; + vnode_put(vp); + + proc_fdlock(p); tvp = fdp->fd_cdir; fdp->fd_cdir = vp; - vrele(tvp); + proc_fdunlock(p); + + if (tvp) + vnode_rele(tvp); + file_drop(uap->fd); + return (0); +out: + vnode_put(vp); + file_drop(uap->fd); + + return(error); } /* * Change current working directory (``.''). */ -struct chdir_args { - char *path; -}; /* ARGSUSED */ int -chdir(p, uap, retval) - struct proc *p; - struct chdir_args *uap; - register_t *retval; +chdir(struct proc *p, struct chdir_args *uap, __unused register_t *retval) { register struct filedesc *fdp = p->p_fd; int error; struct nameidata nd; struct vnode *tvp; + struct vfs_context context; - NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNPATH1, UIO_USERSPACE, - uap->path, p); - error = change_dir(&nd, p); + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + + NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNPATH1, + UIO_USERSPACE, uap->path, &context); + error = change_dir(&nd, &context); if (error) return (error); + if ( (error = vnode_ref(nd.ni_vp)) ) { + vnode_put(nd.ni_vp); + return (error); + } + /* + * drop the iocount we picked up in change_dir + */ + vnode_put(nd.ni_vp); + + proc_fdlock(p); tvp = fdp->fd_cdir; fdp->fd_cdir = nd.ni_vp; - vrele(tvp); + proc_fdunlock(p); + + if (tvp) + vnode_rele(tvp); + return (0); } /* * Change notion of root (``/'') directory. */ -struct chroot_args { - char *path; -}; /* ARGSUSED */ int -chroot(p, uap, retval) - struct proc *p; - struct chroot_args *uap; - register_t *retval; +chroot(struct proc *p, struct chroot_args *uap, __unused register_t *retval) { register struct filedesc *fdp = p->p_fd; int error; struct nameidata nd; boolean_t shared_regions_active; struct vnode *tvp; + struct vfs_context context; + + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); - if (error = suser(p->p_ucred, &p->p_acflag)) + if ((error = suser(kauth_cred_get(), &p->p_acflag))) return (error); - NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNPATH1, UIO_USERSPACE, - uap->path, p); - error = change_dir(&nd, p); + NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNPATH1, + UIO_USERSPACE, uap->path, &context); + error = change_dir(&nd, &context); if (error) return (error); @@ -1002,16 +1384,27 @@ chroot(p, uap, retval) } else { shared_regions_active = TRUE; } - - if(error = clone_system_shared_regions(shared_regions_active, nd.ni_vp)) { - vrele(nd.ni_vp); + if ((error = clone_system_shared_regions(shared_regions_active, + TRUE, /* chain_regions */ + (int)nd.ni_vp))) { + vnode_put(nd.ni_vp); + return (error); + } + if ( (error = vnode_ref(nd.ni_vp)) ) { + vnode_put(nd.ni_vp); return (error); } + vnode_put(nd.ni_vp); + proc_fdlock(p); tvp = fdp->fd_rdir; fdp->fd_rdir = nd.ni_vp; + fdp->fd_flags |= FD_CHROOT; + proc_fdunlock(p); + if (tvp != NULL) - vrele(tvp); + vnode_rele(tvp); + return (0); } @@ -1019,24 +1412,22 @@ chroot(p, uap, retval) * Common routine for chroot and chdir. */ static int -change_dir(ndp, p) - register struct nameidata *ndp; - struct proc *p; +change_dir(struct nameidata *ndp, vfs_context_t ctx) { struct vnode *vp; int error; - if (error = namei(ndp)) + if ((error = namei(ndp))) return (error); + nameidone(ndp); vp = ndp->ni_vp; if (vp->v_type != VDIR) error = ENOTDIR; else - error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p); + error = vnode_authorize(vp, NULL, KAUTH_VNODE_SEARCH, ctx); if (error) - vput(vp); - else - VOP_UNLOCK(vp, 0, p); + vnode_put(vp); + return (error); } @@ -1044,64 +1435,60 @@ change_dir(ndp, p) * Check permissions, allocate an open file structure, * and call the device open routine if any. */ -struct open_args { - char *path; - int flags; - int mode; -}; -int -open(p, uap, retval) - struct proc *p; - register struct open_args *uap; - register_t *retval; + +#warning XXX implement uid, gid +static int +open1(vfs_context_t ctx, user_addr_t upath, int uflags, struct vnode_attr *vap, register_t *retval) { + struct proc *p = vfs_context_proc(ctx); register struct filedesc *fdp = p->p_fd; - register struct file *fp; + register struct fileproc *fp; register struct vnode *vp; - int flags, cmode, oflags; - struct file *nfp; + int flags, oflags; + struct fileproc *nfp; int type, indx, error; struct flock lf; struct nameidata nd; - extern struct fileops vnops; - - oflags = uap->flags; - flags = FFLAGS(uap->flags); - - AUDIT_ARG(fflags, oflags); - AUDIT_ARG(mode, uap->mode); - cmode = ((uap->mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT; + oflags = uflags; if ((oflags & O_ACCMODE) == O_ACCMODE) return(EINVAL); - if (error = falloc(p, &nfp, &indx)) + flags = FFLAGS(uflags); + + AUDIT_ARG(fflags, oflags); + AUDIT_ARG(mode, vap->va_mode); + + if ( (error = falloc(p, &nfp, &indx)) ) { return (error); + } fp = nfp; - NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNPATH1, UIO_USERSPACE, uap->path, p); + NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNPATH1, + UIO_USERSPACE, upath, ctx); p->p_dupfd = -indx - 1; /* XXX check for fdopen */ - if (error = vn_open_modflags(&nd, &flags, cmode)) { - ffree(fp); - if ((error == ENODEV || error == ENXIO) && - p->p_dupfd >= 0 && /* XXX from fdopen */ - (error = - dupfdopen(fdp, indx, p->p_dupfd, flags, error)) == 0) { - *retval = indx; - return (0); + + if ((error = vn_open_auth(&nd, &flags, vap))) { + if ((error == ENODEV || error == ENXIO) && (p->p_dupfd >= 0)) { /* XXX from fdopen */ + if ((error = dupfdopen(fdp, indx, p->p_dupfd, flags, error)) == 0) { + fp_drop(p, indx, 0, 0); + *retval = indx; + return (0); + } } if (error == ERESTART) - error = EINTR; - fdrelse(p, indx); + error = EINTR; + fp_free(p, indx, fp); + return (error); } p->p_dupfd = 0; vp = nd.ni_vp; - fp->f_flag = flags & FMASK; - fp->f_type = DTYPE_VNODE; - fp->f_ops = &vnops; - fp->f_data = (caddr_t)vp; - VOP_UNLOCK(vp, 0, p); + fp->f_fglob->fg_flag = flags & (FMASK | O_EVTONLY); + fp->f_fglob->fg_type = DTYPE_VNODE; + fp->f_fglob->fg_ops = &vnops; + fp->f_fglob->fg_data = (caddr_t)vp; + if (flags & (O_EXLOCK | O_SHLOCK)) { lf.l_whence = SEEK_SET; lf.l_start = 0; @@ -1113,450 +1500,642 @@ open(p, uap, retval) type = F_FLOCK; if ((flags & FNONBLOCK) == 0) type |= F_WAIT; - if (error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) + if ((error = VNOP_ADVLOCK(vp, (caddr_t)fp->f_fglob, F_SETLK, &lf, type, ctx))) goto bad; - fp->f_flag |= FHASLOCK; + fp->f_fglob->fg_flag |= FHASLOCK; } - if (flags & O_TRUNC) { - struct vattr vat; - struct vattr *vap = &vat; + /* try to truncate by setting the size attribute */ + if ((flags & O_TRUNC) && ((error = vnode_setsize(vp, (off_t)0, 0, ctx)) != 0)) + goto bad; - VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE); - (void)vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); /* XXX */ - VATTR_NULL(vap); - vap->va_size = 0; - /* try to truncate by setting the size attribute */ - error = VOP_SETATTR(vp, vap, p->p_ucred, p); - VOP_UNLOCK(vp, 0, p); /* XXX */ - if (error) - goto bad; - } + vnode_put(vp); + proc_fdlock(p); *fdflags(p, indx) &= ~UF_RESERVED; + fp_drop(p, indx, fp, 1); + proc_fdunlock(p); + *retval = indx; + return (0); bad: - vn_close(vp, fp->f_flag, fp->f_cred, p); - ffree(fp); - fdrelse(p, indx); + vn_close(vp, fp->f_fglob->fg_flag, fp->f_fglob->fg_cred, p); + vnode_put(vp); + fp_free(p, indx, fp); + return (error); + } -#if COMPAT_43 -/* - * Create a file. - */ -struct ocreat_args { - char *path; - int mode; -}; int -ocreat(p, uap, retval) - struct proc *p; - register struct ocreat_args *uap; - register_t *retval; +open_extended(struct proc *p, struct open_extended_args *uap, register_t *retval) +{ + struct vfs_context context; + register struct filedesc *fdp = p->p_fd; + int ciferror; + kauth_filesec_t xsecdst; + struct vnode_attr va; + int cmode; + + xsecdst = NULL; + if ((uap->xsecurity != USER_ADDR_NULL) && + ((ciferror = kauth_copyinfilesec(uap->xsecurity, &xsecdst)) != 0)) + return ciferror; + + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + + VATTR_INIT(&va); + cmode = ((uap->mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT; + VATTR_SET(&va, va_mode, cmode); + if (uap->uid != KAUTH_UID_NONE) + VATTR_SET(&va, va_uid, uap->uid); + if (uap->gid != KAUTH_GID_NONE) + VATTR_SET(&va, va_gid, uap->gid); + if (xsecdst != NULL) + VATTR_SET(&va, va_acl, &xsecdst->fsec_acl); + + ciferror = open1(&context, uap->path, uap->flags, &va, retval); + if (xsecdst != NULL) + kauth_filesec_free(xsecdst); + + return ciferror; +} + +int +open(struct proc *p, struct open_args *uap, register_t *retval) { - struct open_args nuap; + struct vfs_context context; + register struct filedesc *fdp = p->p_fd; + struct vnode_attr va; + int cmode; - nuap.path = uap->path; - nuap.mode = uap->mode; - nuap.flags = O_WRONLY | O_CREAT | O_TRUNC; - return (open(p, &nuap, retval)); + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + + VATTR_INIT(&va); + /* Mask off all but regular access permissions */ + cmode = ((uap->mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT; + VATTR_SET(&va, va_mode, cmode & ACCESSPERMS); + + return(open1(&context, uap->path, uap->flags, &va, retval)); } -#endif /* COMPAT_43 */ + /* * Create a special file. */ -struct mknod_args { - char *path; - int mode; - int dev; -}; -/* ARGSUSED */ +static int mkfifo1(vfs_context_t ctx, user_addr_t upath, struct vnode_attr *vap); + int -mknod(p, uap, retval) - struct proc *p; - register struct mknod_args *uap; - register_t *retval; +mknod(struct proc *p, register struct mknod_args *uap, __unused register_t *retval) { - register struct vnode *vp; - struct vattr vattr; - int cmode, error; - int whiteout; + struct vnode_attr va; + struct vfs_context context; + int error; + int whiteout = 0; struct nameidata nd; + vnode_t vp, dvp; + + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + + VATTR_INIT(&va); + VATTR_SET(&va, va_mode, (uap->mode & ALLPERMS) & ~p->p_fd->fd_cmask); + VATTR_SET(&va, va_rdev, uap->dev); + + /* If it's a mknod() of a FIFO, call mkfifo1() instead */ + if ((uap->mode & S_IFMT) == S_IFIFO) + return(mkfifo1(&context, uap->path, &va)); AUDIT_ARG(mode, uap->mode); AUDIT_ARG(dev, uap->dev); - cmode = (uap->mode & ALLPERMS) &~ p->p_fd->fd_cmask; - if (error = suser(p->p_ucred, &p->p_acflag)) + + if ((error = suser(context.vc_ucred, &p->p_acflag))) return (error); - bwillwrite(); - NDINIT(&nd, CREATE, LOCKPARENT | AUDITVNPATH1, UIO_USERSPACE, uap->path, p); + NDINIT(&nd, CREATE, LOCKPARENT | AUDITVNPATH1, + UIO_USERSPACE, uap->path, &context); error = namei(&nd); if (error) return (error); + dvp = nd.ni_dvp; vp = nd.ni_vp; - if (vp != NULL) + + if (vp != NULL) { error = EEXIST; - else { - VATTR_NULL(&vattr); - vattr.va_mode = cmode; - vattr.va_rdev = uap->dev; - whiteout = 0; - - switch (uap->mode & S_IFMT) { - case S_IFMT: /* used by badsect to flag bad sectors */ - vattr.va_type = VBAD; - break; - case S_IFCHR: - vattr.va_type = VCHR; - break; - case S_IFBLK: - vattr.va_type = VBLK; - break; - case S_IFWHT: - whiteout = 1; - break; - default: - error = EINVAL; - break; - } + goto out; } - if (!error) { - char *nameptr; - nameptr = add_name(nd.ni_cnd.cn_nameptr, nd.ni_cnd.cn_namelen, nd.ni_cnd.cn_hash, 0); - VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE); - if (whiteout) { - error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE); - if (error) - VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); - vput(nd.ni_dvp); - } else { - error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, - &nd.ni_cnd, &vattr); - } - if (error == 0 && nd.ni_vp) { - if (VNAME(nd.ni_vp) == NULL) { - VNAME(nd.ni_vp) = nameptr; - nameptr = NULL; - } - if (VPARENT(nd.ni_vp) == NULL) { - if (vget(nd.ni_dvp, 0, p) == 0) { - VPARENT(nd.ni_vp) = nd.ni_dvp; - } - } - } - if (nameptr) { - remove_name(nameptr); - nameptr = NULL; - } + if ((error = vnode_authorize(dvp, NULL, KAUTH_VNODE_ADD_FILE, &context)) != 0) + goto out; + + switch (uap->mode & S_IFMT) { + case S_IFMT: /* used by badsect to flag bad sectors */ + VATTR_SET(&va, va_type, VBAD); + break; + case S_IFCHR: + VATTR_SET(&va, va_type, VCHR); + break; + case S_IFBLK: + VATTR_SET(&va, va_type, VBLK); + break; + case S_IFWHT: + whiteout = 1; + break; + default: + error = EINVAL; + goto out; + } + if (whiteout) { + error = VNOP_WHITEOUT(dvp, &nd.ni_cnd, CREATE, &context); } else { - VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); - if (nd.ni_dvp == vp) - vrele(nd.ni_dvp); - else - vput(nd.ni_dvp); - if (vp) - vrele(vp); + error = vn_create(dvp, &vp, &nd.ni_cnd, &va, 0, &context); + } + if (error) + goto out; + + if (vp) { + int update_flags = 0; + + // Make sure the name & parent pointers are hooked up + if (vp->v_name == NULL) + update_flags |= VNODE_UPDATE_NAME; + if (vp->v_parent == NULLVP) + update_flags |= VNODE_UPDATE_PARENT; + + if (update_flags) + vnode_update_identity(vp, dvp, nd.ni_cnd.cn_nameptr, nd.ni_cnd.cn_namelen, nd.ni_cnd.cn_hash, update_flags); + + add_fsevent(FSE_CREATE_FILE, &context, + FSE_ARG_VNODE, vp, + FSE_ARG_DONE); } + +out: + /* + * nameidone has to happen before we vnode_put(dvp) + * since it may need to release the fs_nodelock on the dvp + */ + nameidone(&nd); + + if (vp) + vnode_put(vp); + vnode_put(dvp); + return (error); } /* * Create a named pipe. */ -struct mkfifo_args { - char *path; - int mode; -}; -/* ARGSUSED */ -int -mkfifo(p, uap, retval) - struct proc *p; - register struct mkfifo_args *uap; - register_t *retval; +static int +mkfifo1(vfs_context_t ctx, user_addr_t upath, struct vnode_attr *vap) { - struct vattr vattr; + vnode_t vp, dvp; int error; struct nameidata nd; - char *nameptr=NULL; - -#if !FIFO - return (EOPNOTSUPP); -#else - bwillwrite(); - NDINIT(&nd, CREATE, LOCKPARENT | AUDITVNPATH1, UIO_USERSPACE, uap->path, p); + NDINIT(&nd, CREATE, LOCKPARENT | AUDITVNPATH1, + UIO_USERSPACE, upath, ctx); error = namei(&nd); if (error) return (error); - if (nd.ni_vp != NULL) { - VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); - if (nd.ni_dvp == nd.ni_vp) - vrele(nd.ni_dvp); - else - vput(nd.ni_dvp); - vrele(nd.ni_vp); - return (EEXIST); - } - - nameptr = add_name(nd.ni_cnd.cn_nameptr, - nd.ni_cnd.cn_namelen, - nd.ni_cnd.cn_hash, 0); - VATTR_NULL(&vattr); - vattr.va_type = VFIFO; - vattr.va_mode = (uap->mode & ALLPERMS) &~ p->p_fd->fd_cmask; - VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE); - error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); - - if (error == 0 && nd.ni_vp && nd.ni_vp->v_type == VFIFO) { - int vpid = nd.ni_vp->v_id; - if (vget(nd.ni_vp, 0, p) == 0) { - if (vpid == nd.ni_vp->v_id && nd.ni_vp->v_type == VFIFO) { - VNAME(nd.ni_vp) = nameptr; - nameptr = NULL; - - if (VPARENT(nd.ni_vp) == NULL) { - if (vget(nd.ni_dvp, 0, p) == 0) { - VPARENT(nd.ni_vp) = nd.ni_dvp; - } - } - } - } - } - if (nameptr) { - remove_name(nameptr); - } + dvp = nd.ni_dvp; + vp = nd.ni_vp; + + /* check that this is a new file and authorize addition */ + if (vp != NULL) { + error = EEXIST; + goto out; + } + if ((error = vnode_authorize(dvp, NULL, KAUTH_VNODE_ADD_FILE, ctx)) != 0) + goto out; + + VATTR_SET(vap, va_type, VFIFO); + + error = vn_create(dvp, &vp, &nd.ni_cnd, vap, 0, ctx); +out: + /* + * nameidone has to happen before we vnode_put(dvp) + * since it may need to release the fs_nodelock on the dvp + */ + nameidone(&nd); + + if (vp) + vnode_put(vp); + vnode_put(dvp); + return error; -#endif /* FIFO */ +} + +int +mkfifo_extended(struct proc *p, struct mkfifo_extended_args *uap, __unused register_t *retval) +{ + int ciferror; + kauth_filesec_t xsecdst; + struct vfs_context context; + struct vnode_attr va; + + xsecdst = KAUTH_FILESEC_NONE; + if (uap->xsecurity != USER_ADDR_NULL) { + if ((ciferror = kauth_copyinfilesec(uap->xsecurity, &xsecdst)) != 0) + return ciferror; + } + + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + + VATTR_INIT(&va); + VATTR_SET(&va, va_mode, (uap->mode & ALLPERMS) & ~p->p_fd->fd_cmask); + if (uap->uid != KAUTH_UID_NONE) + VATTR_SET(&va, va_uid, uap->uid); + if (uap->gid != KAUTH_GID_NONE) + VATTR_SET(&va, va_gid, uap->gid); + if (xsecdst != KAUTH_FILESEC_NONE) + VATTR_SET(&va, va_acl, &xsecdst->fsec_acl); + + ciferror = mkfifo1(&context, uap->path, &va); + + if (xsecdst != KAUTH_FILESEC_NONE) + kauth_filesec_free(xsecdst); + return ciferror; +} + +/* ARGSUSED */ +int +mkfifo(struct proc *p, register struct mkfifo_args *uap, __unused register_t *retval) +{ + struct vfs_context context; + struct vnode_attr va; + + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + + VATTR_INIT(&va); + VATTR_SET(&va, va_mode, (uap->mode & ALLPERMS) & ~p->p_fd->fd_cmask); + + return(mkfifo1(&context, uap->path, &va)); } /* * Make a hard file link. */ -struct link_args { - char *path; - char *link; -}; /* ARGSUSED */ int -link(p, uap, retval) - struct proc *p; - register struct link_args *uap; - register_t *retval; +link(struct proc *p, register struct link_args *uap, __unused register_t *retval) { - register struct vnode *vp; + vnode_t vp, dvp, lvp; struct nameidata nd; + struct vfs_context context; int error; + fse_info finfo; + int need_event, has_listeners; - bwillwrite(); - NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNPATH1, UIO_USERSPACE, uap->path, p); + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + vp = dvp = lvp = NULLVP; + + /* look up the object we are linking to */ + NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNPATH1, + UIO_USERSPACE, uap->path, &context); error = namei(&nd); if (error) return (error); vp = nd.ni_vp; - if (vp->v_type == VDIR) + + nameidone(&nd); + + /* we're not allowed to link to directories */ + if (vp->v_type == VDIR) { error = EPERM; /* POSIX */ - else { - nd.ni_cnd.cn_nameiop = CREATE; - nd.ni_cnd.cn_flags = LOCKPARENT | AUDITVNPATH2; - nd.ni_dirp = uap->link; - error = namei(&nd); - if (error == 0) { - if (nd.ni_vp != NULL) - error = EEXIST; - if (!error) { - VOP_LEASE(nd.ni_dvp, p, p->p_ucred, - LEASE_WRITE); - VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE); - error = VOP_LINK(vp, nd.ni_dvp, &nd.ni_cnd); - } else { - VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); - if (nd.ni_dvp == nd.ni_vp) - vrele(nd.ni_dvp); - else - vput(nd.ni_dvp); - if (nd.ni_vp) - vrele(nd.ni_vp); + goto out; + } + + /* or to anything that kauth doesn't want us to (eg. immutable items) */ + if ((error = vnode_authorize(vp, NULL, KAUTH_VNODE_LINKTARGET, &context)) != 0) + goto out; + + /* lookup the target node */ + nd.ni_cnd.cn_nameiop = CREATE; + nd.ni_cnd.cn_flags = LOCKPARENT | AUDITVNPATH2; + nd.ni_dirp = uap->link; + error = namei(&nd); + if (error != 0) + goto out; + dvp = nd.ni_dvp; + lvp = nd.ni_vp; + /* target node must not exist */ + if (lvp != NULLVP) { + error = EEXIST; + goto out2; + } + /* cannot link across mountpoints */ + if (vnode_mount(vp) != vnode_mount(dvp)) { + error = EXDEV; + goto out2; + } + + /* authorize creation of the target note */ + if ((error = vnode_authorize(dvp, NULL, KAUTH_VNODE_ADD_FILE, &context)) != 0) + goto out2; + + /* and finally make the link */ + error = VNOP_LINK(vp, dvp, &nd.ni_cnd, &context); + if (error) + goto out2; + + need_event = need_fsevent(FSE_CREATE_FILE, dvp); + has_listeners = kauth_authorize_fileop_has_listeners(); + + if (need_event || has_listeners) { + char *target_path = NULL; + char *link_to_path = NULL; + int len, link_name_len; + + /* build the path to the new link file */ + target_path = get_pathbuff(); + len = MAXPATHLEN; + vn_getpath(dvp, target_path, &len); + target_path[len-1] = '/'; + strcpy(&target_path[len], nd.ni_cnd.cn_nameptr); + len += nd.ni_cnd.cn_namelen; + + if (has_listeners) { + /* build the path to file we are linking to */ + link_to_path = get_pathbuff(); + link_name_len = MAXPATHLEN; + vn_getpath(vp, link_to_path, &link_name_len); + + /* call out to allow 3rd party notification of rename. + * Ignore result of kauth_authorize_fileop call. + */ + kauth_authorize_fileop(vfs_context_ucred(&context), KAUTH_FILEOP_LINK, + (uintptr_t)link_to_path, (uintptr_t)target_path); + if (link_to_path != NULL) + release_pathbuff(link_to_path); + } + if (need_event) { + /* construct fsevent */ + if (get_fse_info(vp, &finfo, &context) == 0) { + // build the path to the destination of the link + add_fsevent(FSE_CREATE_FILE, &context, + FSE_ARG_STRING, len, target_path, + FSE_ARG_FINFO, &finfo, + FSE_ARG_DONE); } } + release_pathbuff(target_path); } - vrele(vp); - return (error); -} +out2: + /* + * nameidone has to happen before we vnode_put(dvp) + * since it may need to release the fs_nodelock on the dvp + */ + nameidone(&nd); +out: + if (lvp) + vnode_put(lvp); + if (dvp) + vnode_put(dvp); + vnode_put(vp); + return (error); +} /* * Make a symbolic link. + * + * We could add support for ACLs here too... */ -struct symlink_args { - char *path; - char *link; -}; /* ARGSUSED */ int -symlink(p, uap, retval) - struct proc *p; - register struct symlink_args *uap; - register_t *retval; +symlink(struct proc *p, register struct symlink_args *uap, __unused register_t *retval) { - struct vattr vattr; - char *path, *nameptr; + struct vnode_attr va; + char *path; int error; struct nameidata nd; + struct vfs_context context; + vnode_t vp, dvp; size_t dummy=0; - u_long vpid; + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + MALLOC_ZONE(path, char *, MAXPATHLEN, M_NAMEI, M_WAITOK); - if (error = copyinstr(uap->path, path, MAXPATHLEN, &dummy)) + error = copyinstr(uap->path, path, MAXPATHLEN, &dummy); + if (error) goto out; AUDIT_ARG(text, path); /* This is the link string */ - bwillwrite(); - NDINIT(&nd, CREATE, LOCKPARENT | AUDITVNPATH1, UIO_USERSPACE, uap->link, p); + + NDINIT(&nd, CREATE, LOCKPARENT | AUDITVNPATH1, + UIO_USERSPACE, uap->link, &context); error = namei(&nd); if (error) goto out; - if (nd.ni_vp) { - VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); - if (nd.ni_dvp == nd.ni_vp) - vrele(nd.ni_dvp); - else - vput(nd.ni_dvp); - vrele(nd.ni_vp); - error = EEXIST; - goto out; - } - VATTR_NULL(&vattr); - vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask; - VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE); - - nameptr = add_name(nd.ni_cnd.cn_nameptr, nd.ni_cnd.cn_namelen, nd.ni_cnd.cn_hash, 0); + dvp = nd.ni_dvp; + vp = nd.ni_vp; - error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path); + if (vp == NULL) { + VATTR_INIT(&va); + VATTR_SET(&va, va_type, VLNK); + VATTR_SET(&va, va_mode, ACCESSPERMS & ~p->p_fd->fd_cmask); + + /* authorize */ + error = vnode_authorize(dvp, NULL, KAUTH_VNODE_ADD_FILE, &context); + /* get default ownership, etc. */ + if (error == 0) + error = vnode_authattr_new(dvp, &va, 0, &context); + if (error == 0) + error = VNOP_SYMLINK(dvp, &vp, &nd.ni_cnd, &va, path, &context); + + /* do fallback attribute handling */ + if (error == 0) + error = vnode_setattr_fallback(vp, &va, &context); + + if (error == 0) { + int update_flags = 0; - // have to do this little dance because nd.ni_vp is not locked - // on return from the VOP_SYMLINK() call. - // - if (error == 0 && nd.ni_vp && nd.ni_vp->v_type == VLNK) { - vpid = nd.ni_vp->v_id; - if (vget(nd.ni_vp, 0, p) == 0) { - if (vpid == nd.ni_vp->v_id && nd.ni_vp->v_type == VLNK) { - VNAME(nd.ni_vp) = nameptr; - nameptr = NULL; + if (vp == NULL) { + nd.ni_cnd.cn_nameiop = LOOKUP; + nd.ni_cnd.cn_flags = 0; + error = namei(&nd); + vp = nd.ni_vp; - if (VPARENT(nd.ni_vp) == NULL && vget(nd.ni_dvp, 0, p) == 0) { - VPARENT(nd.ni_vp) = nd.ni_dvp; - } + if (vp == NULL) + goto skipit; + } + +#if 0 /* XXX - kauth_todo - is KAUTH_FILEOP_SYMLINK needed? */ + /* call out to allow 3rd party notification of rename. + * Ignore result of kauth_authorize_fileop call. + */ + if (kauth_authorize_fileop_has_listeners() && + namei(&nd) == 0) { + char *new_link_path = NULL; + int len; + + /* build the path to the new link file */ + new_link_path = get_pathbuff(); + len = MAXPATHLEN; + vn_getpath(dvp, new_link_path, &len); + new_link_path[len - 1] = '/'; + strcpy(&new_link_path[len], nd.ni_cnd.cn_nameptr); + + kauth_authorize_fileop(vfs_context_ucred(&context), KAUTH_FILEOP_SYMLINK, + (uintptr_t)path, (uintptr_t)new_link_path); + if (new_link_path != NULL) + release_pathbuff(new_link_path); + } +#endif + // Make sure the name & parent pointers are hooked up + if (vp->v_name == NULL) + update_flags |= VNODE_UPDATE_NAME; + if (vp->v_parent == NULLVP) + update_flags |= VNODE_UPDATE_PARENT; + + if (update_flags) + vnode_update_identity(vp, dvp, nd.ni_cnd.cn_nameptr, nd.ni_cnd.cn_namelen, nd.ni_cnd.cn_hash, update_flags); + + add_fsevent(FSE_CREATE_FILE, &context, + FSE_ARG_VNODE, vp, + FSE_ARG_DONE); } - vrele(nd.ni_vp); - } - } - if (nameptr) { // only true if we didn't add it to the vnode - remove_name(nameptr); - } + } else + error = EEXIST; + +skipit: + /* + * nameidone has to happen before we vnode_put(dvp) + * since it may need to release the fs_nodelock on the dvp + */ + nameidone(&nd); + + if (vp) + vnode_put(vp); + vnode_put(dvp); out: FREE_ZONE(path, MAXPATHLEN, M_NAMEI); + return (error); } /* * Delete a whiteout from the filesystem. */ -struct undelete_args { - char *path; -}; /* ARGSUSED */ +#warning XXX authorization not implmented for whiteouts int -undelete(p, uap, retval) - struct proc *p; - register struct undelete_args *uap; - register_t *retval; +undelete(struct proc *p, register struct undelete_args *uap, __unused register_t *retval) { int error; struct nameidata nd; + struct vfs_context context; + vnode_t vp, dvp; + + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); - bwillwrite(); - NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT|AUDITVNPATH1, UIO_USERSPACE, - uap->path, p); + NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT|AUDITVNPATH1, + UIO_USERSPACE, uap->path, &context); error = namei(&nd); if (error) return (error); + dvp = nd.ni_dvp; + vp = nd.ni_vp; - if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) { - VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); - if (nd.ni_dvp == nd.ni_vp) - vrele(nd.ni_dvp); - else - vput(nd.ni_dvp); - if (nd.ni_vp) - vrele(nd.ni_vp); - return (EEXIST); - } + if (vp == NULLVP && (nd.ni_cnd.cn_flags & ISWHITEOUT)) { + error = VNOP_WHITEOUT(dvp, &nd.ni_cnd, DELETE, &context); + } else + error = EEXIST; + + /* + * nameidone has to happen before we vnode_put(dvp) + * since it may need to release the fs_nodelock on the dvp + */ + nameidone(&nd); + + if (vp) + vnode_put(vp); + vnode_put(dvp); - VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE); - if (error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) - VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); - vput(nd.ni_dvp); return (error); } /* * Delete a name from the filesystem. */ -struct unlink_args { - char *path; -}; /* ARGSUSED */ static int -_unlink(p, uap, retval, nodelbusy) - struct proc *p; - struct unlink_args *uap; - register_t *retval; - int nodelbusy; +_unlink(struct proc *p, struct unlink_args *uap, __unused register_t *retval, int nodelbusy) { - register struct vnode *vp; + vnode_t vp, dvp; int error; struct nameidata nd; + struct vfs_context context; + struct componentname *cnp; + int flags = 0; - bwillwrite(); - NDINIT(&nd, DELETE, LOCKPARENT | AUDITVNPATH1, UIO_USERSPACE, uap->path, p); - /* with Carbon semantics, busy files cannot be deleted */ + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + + NDINIT(&nd, DELETE, LOCKPARENT | AUDITVNPATH1, + UIO_USERSPACE, uap->path, &context); + cnp = &nd.ni_cnd; + + /* With Carbon delete semantics, busy files cannot be deleted */ if (nodelbusy) - nd.ni_cnd.cn_flags |= NODELETEBUSY; + flags |= VNODE_REMOVE_NODELETEBUSY; + error = namei(&nd); if (error) return (error); - + dvp = nd.ni_dvp; vp = nd.ni_vp; - VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE); - vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); - if (vp->v_type == VDIR) + if (vp->v_type == VDIR) { error = EPERM; /* POSIX */ - else { + } else { /* * The root of a mounted filesystem cannot be deleted. - * - * XXX: can this only be a VDIR case? */ - if (vp->v_flag & VROOT) + if (vp->v_flag & VROOT) { error = EBUSY; + } } - + /* authorize the delete operation */ + if (!error) + error = vnode_authorize(vp, nd.ni_dvp, KAUTH_VNODE_DELETE, &context); + if (!error) { - VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE); - error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd); - } else { - VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); - if (nd.ni_dvp == vp) - vrele(nd.ni_dvp); - else - vput(nd.ni_dvp); - if (vp != NULLVP) - vput(vp); + char *path = NULL; + int len; + fse_info finfo; + + if (need_fsevent(FSE_DELETE, dvp)) { + path = get_pathbuff(); + len = MAXPATHLEN; + vn_getpath(vp, path, &len); + get_fse_info(vp, &finfo, &context); + } + error = VNOP_REMOVE(dvp, vp, &nd.ni_cnd, flags, &context); + + if ( !error && path != NULL) { + add_fsevent(FSE_DELETE, &context, + FSE_ARG_STRING, len, path, + FSE_ARG_FINFO, &finfo, + FSE_ARG_DONE); + } + if (path != NULL) + release_pathbuff(path); } + /* + * nameidone has to happen before we vnode_put(dvp) + * since it may need to release the fs_nodelock on the dvp + */ + nameidone(&nd); + vnode_put(dvp); + vnode_put(vp); return (error); } @@ -1578,53 +2157,51 @@ unlink(p, uap, retval) int delete(p, uap, retval) struct proc *p; - struct unlink_args *uap; + struct delete_args *uap; register_t *retval; { - return _unlink(p, uap, retval, 1); + return _unlink(p, (struct unlink_args *)uap, retval, 1); } /* * Reposition read/write file offset. */ -struct lseek_args { - int fd; -#ifdef DOUBLE_ALIGN_PARAMS - int pad; -#endif - off_t offset; - int whence; -}; int lseek(p, uap, retval) struct proc *p; register struct lseek_args *uap; - register_t *retval; + off_t *retval; { - struct ucred *cred = p->p_ucred; - struct file *fp; + struct fileproc *fp; struct vnode *vp; - struct vattr vattr; - off_t offset = uap->offset; + struct vfs_context context; + off_t offset = uap->offset, file_size; int error; - if (error = fdgetf(p, uap->fd, &fp)) + if ( (error = fp_getfvp(p,uap->fd, &fp, &vp)) ) { + if (error == ENOTSUP) + return (ESPIPE); return (error); - if (fref(fp) == -1) - return (EBADF); - if (fp->f_type != DTYPE_VNODE) { - frele(fp); - return (ESPIPE); } - vp = (struct vnode *)fp->f_data; + if (vnode_isfifo(vp)) { + file_drop(uap->fd); + return(ESPIPE); + } + if ( (error = vnode_getwithref(vp)) ) { + file_drop(uap->fd); + return(error); + } + switch (uap->whence) { case L_INCR: - offset += fp->f_offset; + offset += fp->f_fglob->fg_offset; break; case L_XTND: - if (error = VOP_GETATTR(vp, &vattr, cred, p)) + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + if ((error = vnode_size(vp, &file_size, &context)) != 0) break; - offset += vattr.va_size; + offset += file_size; break; case L_SET: break; @@ -1646,327 +2223,425 @@ lseek(p, uap, retval) error = EINVAL; } else { /* Success */ - fp->f_offset = offset; - *(off_t *)retval = fp->f_offset; + fp->f_fglob->fg_offset = offset; + *retval = fp->f_fglob->fg_offset; } } } - frele(fp); + (void)vnode_put(vp); + file_drop(uap->fd); return (error); } -#if COMPAT_43 + /* - * Reposition read/write file offset. + * Check access permissions. */ -struct olseek_args { - int fd; - long offset; - int whence; -}; -int -olseek(p, uap, retval) - struct proc *p; - register struct olseek_args *uap; - register_t *retval; +static int +access1(vnode_t vp, vnode_t dvp, int uflags, vfs_context_t ctx) { - struct lseek_args /* { - syscallarg(int) fd; -#ifdef DOUBLE_ALIGN_PARAMS - syscallarg(int) pad; -#endif - syscallarg(off_t) offset; - syscallarg(int) whence; - } */ nuap; - off_t qret; + kauth_action_t action; int error; - nuap.fd = uap->fd; - nuap.offset = uap->offset; - nuap.whence = uap->whence; - error = lseek(p, &nuap, &qret); - *(long *)retval = qret; - return (error); + /* + * If just the regular access bits, convert them to something + * that vnode_authorize will understand. + */ + if (!(uflags & _ACCESS_EXTENDED_MASK)) { + action = 0; + if (uflags & R_OK) + action |= KAUTH_VNODE_READ_DATA; /* aka KAUTH_VNODE_LIST_DIRECTORY */ + if (uflags & W_OK) { + if (vnode_isdir(vp)) { + action |= KAUTH_VNODE_ADD_FILE | + KAUTH_VNODE_ADD_SUBDIRECTORY; + /* might want delete rights here too */ + } else { + action |= KAUTH_VNODE_WRITE_DATA; + } + } + if (uflags & X_OK) { + if (vnode_isdir(vp)) { + action |= KAUTH_VNODE_SEARCH; + } else { + action |= KAUTH_VNODE_EXECUTE; + } + } + } else { + /* take advantage of definition of uflags */ + action = uflags >> 8; + } + + /* action == 0 means only check for existence */ + if (action != 0) { + error = vnode_authorize(vp, dvp, action | KAUTH_VNODE_ACCESS, ctx); + } else { + error = 0; + } + + return(error); } -#endif /* COMPAT_43 */ -/* - * Check access permissions. - */ -struct access_args { - char *path; - int flags; -}; + + +/* XXX need to support the check-as uid argument */ int -access(p, uap, retval) - struct proc *p; - register struct access_args *uap; - register_t *retval; +access_extended(__unused struct proc *p, struct access_extended_args *uap, __unused register_t *retval) { - register struct ucred *cred = p->p_ucred; - register struct vnode *vp; - int error, flags, t_gid, t_uid; + struct accessx_descriptor *input; + errno_t *result; + int error, limit, nent, i, j, wantdelete; + struct vfs_context context; struct nameidata nd; - - t_uid = cred->cr_uid; - t_gid = cred->cr_groups[0]; - cred->cr_uid = p->p_cred->p_ruid; - cred->cr_groups[0] = p->p_cred->p_rgid; - NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNPATH1, UIO_USERSPACE, - uap->path, p); - error = namei(&nd); + int niopts; + vnode_t vp, dvp; + + input = NULL; + result = NULL; + error = 0; + vp = NULL; + dvp = NULL; + context.vc_ucred = NULL; + + /* check input size and fetch descriptor array into allocated storage */ + if (uap->size > ACCESSX_MAX_TABLESIZE) + return(ENOMEM); + if (uap->size < sizeof(struct accessx_descriptor)) + return(EINVAL); + MALLOC(input, struct accessx_descriptor *, uap->size, M_TEMP, M_WAITOK); + if (input == NULL) { + error = ENOMEM; + goto out; + } + error = copyin(uap->entries, input, uap->size); if (error) - goto out1; - vp = nd.ni_vp; + goto out; - /* Flags == 0 means only check for existence. */ - if (uap->flags) { - flags = 0; - if (uap->flags & R_OK) - flags |= VREAD; - if (uap->flags & W_OK) - flags |= VWRITE; - if (uap->flags & X_OK) - flags |= VEXEC; - if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0) - error = VOP_ACCESS(vp, flags, cred, p); - } - vput(vp); -out1: - cred->cr_uid = t_uid; - cred->cr_groups[0] = t_gid; - return (error); + /* + * Access is defined as checking against the process' + * real identity, even if operations are checking the + * effective identity. So we need to tweak the credential + * in the context. + */ + context.vc_ucred = kauth_cred_copy_real(kauth_cred_get()); + context.vc_proc = current_proc(); + + /* + * Find out how many entries we have, so we can allocate the result array. + */ + limit = uap->size / sizeof(struct accessx_descriptor); + nent = limit; + wantdelete = 0; + for (i = 0; i < nent; i++) { + /* + * Take the offset to the name string for this entry and convert to an + * input array index, which would be one off the end of the array if this + * was the lowest-addressed name string. + */ + j = input[i].ad_name_offset / sizeof(struct accessx_descriptor); + /* bad input */ + if (j > limit) { + error = EINVAL; + goto out; + } + /* implicit reference to previous name, not a real offset */ + if (j == 0) { + /* first entry must have a name string */ + if (i == 0) { + error = EINVAL; + goto out; + } + continue; + } + if (j < nent) + nent = j; + } + if (nent > ACCESSX_MAX_DESCRIPTORS) { + error = ENOMEM; + goto out; + } + MALLOC(result, errno_t *, nent * sizeof(errno_t), M_TEMP, M_WAITOK); + if (result == NULL) { + error = ENOMEM; + goto out; + } + + /* + * Do the work. + */ + error = 0; + for (i = 0; i < nent; i++) { + /* + * Looking up a new name? + */ + if (input[i].ad_name_offset != 0) { + /* discard old vnodes */ + if (vp) { + vnode_put(vp); + vp = NULL; + } + if (dvp) { + vnode_put(dvp); + dvp = NULL; + } + + /* scan forwards to see if we need the parent this time */ + wantdelete = input[i].ad_flags & _DELETE_OK; + for (j = i + 1; (j < nent) && (input[j].ad_name_offset == 0); j++) + if (input[j].ad_flags & _DELETE_OK) + wantdelete = 1; + + niopts = FOLLOW | AUDITVNPATH1; + /* need parent for vnode_authorize for deletion test */ + if (wantdelete) + niopts |= WANTPARENT; + + /* do the lookup */ + NDINIT(&nd, LOOKUP, niopts, UIO_SYSSPACE, CAST_USER_ADDR_T((const char *)input + input[i].ad_name_offset), &context); + error = namei(&nd); + if (!error) { + vp = nd.ni_vp; + if (wantdelete) + dvp = nd.ni_dvp; + } + nameidone(&nd); + } + + /* + * Handle lookup errors. + */ + switch(error) { + case ENOENT: + case EACCES: + case EPERM: + case ENOTDIR: + result[i] = error; + break; + case 0: + /* run this access check */ + result[i] = access1(vp, dvp, input[i].ad_flags, &context); + break; + default: + /* fatal lookup error */ + + goto out; + } + } + + /* copy out results */ + error = copyout(result, uap->results, nent * sizeof(errno_t)); + +out: + if (input) + FREE(input, M_TEMP); + if (result) + FREE(result, M_TEMP); + if (vp) + vnode_put(vp); + if (dvp) + vnode_put(dvp); + if (context.vc_ucred) + kauth_cred_rele(context.vc_ucred); + return(error); } -#if COMPAT_43 -/* - * Get file status; this version follows links. - */ -struct ostat_args { - char *path; - struct ostat *ub; -}; -/* ARGSUSED */ int -ostat(p, uap, retval) - struct proc *p; - register struct ostat_args *uap; - register_t *retval; +access(__unused struct proc *p, register struct access_args *uap, __unused register_t *retval) { - struct stat sb; - struct ostat osb; int error; struct nameidata nd; + int niopts; + struct vfs_context context; + + /* + * Access is defined as checking against the process' + * real identity, even if operations are checking the + * effective identity. So we need to tweak the credential + * in the context. + */ + context.vc_ucred = kauth_cred_copy_real(kauth_cred_get()); + context.vc_proc = current_proc(); + + niopts = FOLLOW | AUDITVNPATH1; + /* need parent for vnode_authorize for deletion test */ + if (uap->flags & _DELETE_OK) + niopts |= WANTPARENT; + NDINIT(&nd, LOOKUP, niopts, UIO_USERSPACE, uap->path, &context); + error = namei(&nd); + if (error) + goto out; + + error = access1(nd.ni_vp, nd.ni_dvp, uap->flags, &context); + + vnode_put(nd.ni_vp); + if (uap->flags & _DELETE_OK) + vnode_put(nd.ni_dvp); + nameidone(&nd); + +out: + kauth_cred_rele(context.vc_ucred); + return(error); +} + + +static int +stat2(vfs_context_t ctx, struct nameidata *ndp, user_addr_t ub, user_addr_t xsecurity, user_addr_t xsecurity_size) +{ + struct stat sb; + struct user_stat user_sb; + caddr_t sbp; + int error, my_size; + kauth_filesec_t fsec; + size_t xsecurity_bufsize; - NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNPATH1, UIO_USERSPACE, - uap->path, p); - if (error = namei(&nd)) + error = namei(ndp); + if (error) return (error); - error = vn_stat(nd.ni_vp, &sb, p); - vput(nd.ni_vp); + fsec = KAUTH_FILESEC_NONE; + error = vn_stat(ndp->ni_vp, &sb, (xsecurity != USER_ADDR_NULL ? &fsec : NULL), ctx); + vnode_put(ndp->ni_vp); + nameidone(ndp); + if (error) return (error); - cvtstat(&sb, &osb); - error = copyout((caddr_t)&osb, (caddr_t)uap->ub, sizeof (osb)); + /* Zap spare fields */ + sb.st_lspare = 0; + sb.st_qspare[0] = 0LL; + sb.st_qspare[1] = 0LL; + if (IS_64BIT_PROCESS(vfs_context_proc(ctx))) { + munge_stat(&sb, &user_sb); + my_size = sizeof(user_sb); + sbp = (caddr_t)&user_sb; + } + else { + my_size = sizeof(sb); + sbp = (caddr_t)&sb; + } + if ((error = copyout(sbp, ub, my_size)) != 0) + goto out; + + /* caller wants extended security information? */ + if (xsecurity != USER_ADDR_NULL) { + + /* did we get any? */ + if (fsec == KAUTH_FILESEC_NONE) { + if (susize(xsecurity_size, 0) != 0) { + error = EFAULT; + goto out; + } + } else { + /* find the user buffer size */ + xsecurity_bufsize = fusize(xsecurity_size); + + /* copy out the actual data size */ + if (susize(xsecurity_size, KAUTH_FILESEC_COPYSIZE(fsec)) != 0) { + error = EFAULT; + goto out; + } + + /* if the caller supplied enough room, copy out to it */ + if (xsecurity_bufsize >= KAUTH_FILESEC_COPYSIZE(fsec)) + error = copyout(fsec, xsecurity, KAUTH_FILESEC_COPYSIZE(fsec)); + } + } +out: + if (fsec != KAUTH_FILESEC_NONE) + kauth_filesec_free(fsec); return (error); } /* - * Get file status; this version does not follow links. + * Get file status; this version follows links. */ -struct olstat_args { - char *path; - struct ostat *ub; -}; -/* ARGSUSED */ -int -olstat(p, uap, retval) - struct proc *p; - register struct olstat_args *uap; - register_t *retval; +static int +stat1(struct proc *p, user_addr_t path, user_addr_t ub, user_addr_t xsecurity, user_addr_t xsecurity_size) { - struct vnode *vp, *dvp; - struct stat sb, sb1; - struct ostat osb; - int error; struct nameidata nd; + struct vfs_context context; - NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKPARENT | AUDITVNPATH1, - UIO_USERSPACE, uap->path, p); - if (error = namei(&nd)) - return (error); - /* - * For symbolic links, always return the attributes of its - * containing directory, except for mode, size, and links. - */ - vp = nd.ni_vp; - dvp = nd.ni_dvp; - if (vp->v_type != VLNK) { - if (dvp == vp) - vrele(dvp); - else - vput(dvp); - error = vn_stat(vp, &sb, p); - vput(vp); - if (error) - return (error); - } else { - error = vn_stat(dvp, &sb, p); - vput(dvp); - if (error) { - vput(vp); - return (error); - } - error = vn_stat(vp, &sb1, p); - vput(vp); - if (error) - return (error); - sb.st_mode &= ~S_IFDIR; - sb.st_mode |= S_IFLNK; - sb.st_nlink = sb1.st_nlink; - sb.st_size = sb1.st_size; - sb.st_blocks = sb1.st_blocks; - } - cvtstat(&sb, &osb); - error = copyout((caddr_t)&osb, (caddr_t)uap->ub, sizeof (osb)); - return (error); + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + + NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNPATH1, + UIO_USERSPACE, path, &context); + return(stat2(&context, &nd, ub, xsecurity, xsecurity_size)); } -/* - * Convert from an old to a new stat structure. - */ -void -cvtstat(st, ost) - struct stat *st; - struct ostat *ost; +int +stat_extended(struct proc *p, struct stat_extended_args *uap, __unused register_t *retval) { + return (stat1(p, uap->path, uap->ub, uap->xsecurity, uap->xsecurity_size)); +} - ost->st_dev = st->st_dev; - ost->st_ino = st->st_ino; - ost->st_mode = st->st_mode; - ost->st_nlink = st->st_nlink; - ost->st_uid = st->st_uid; - ost->st_gid = st->st_gid; - ost->st_rdev = st->st_rdev; - if (st->st_size < (quad_t)1 << 32) - ost->st_size = st->st_size; - else - ost->st_size = -2; - ost->st_atime = st->st_atime; - ost->st_mtime = st->st_mtime; - ost->st_ctime = st->st_ctime; - ost->st_blksize = st->st_blksize; - ost->st_blocks = st->st_blocks; - ost->st_flags = st->st_flags; - ost->st_gen = st->st_gen; +int +stat(struct proc *p, struct stat_args *uap, __unused register_t *retval) +{ + return(stat1(p, uap->path, uap->ub, 0, 0)); } -#endif /* COMPAT_43 */ /* - * The stat buffer spare fields are uninitialized - * so don't include them in the copyout. + * Get file status; this version does not follow links. */ -#define STATBUFSIZE \ - (sizeof(struct stat) - sizeof(int32_t) - 2 * sizeof(int64_t)) +static int +lstat1(struct proc *p, user_addr_t path, user_addr_t ub, user_addr_t xsecurity, user_addr_t xsecurity_size) +{ + struct nameidata nd; + struct vfs_context context; + + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + + NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNPATH1, + UIO_USERSPACE, path, &context); + + return(stat2(&context, &nd, ub, xsecurity, xsecurity_size)); +} + +int +lstat_extended(struct proc *p, struct lstat_extended_args *uap, __unused register_t *retval) +{ + return (lstat1(p, uap->path, uap->ub, uap->xsecurity, uap->xsecurity_size)); +} + +int +lstat(struct proc *p, struct lstat_args *uap, __unused register_t *retval) +{ + return(lstat1(p, uap->path, uap->ub, 0, 0)); +} + /* - * Get file status; this version follows links. + * Get configurable pathname variables. */ -struct stat_args { - char *path; - struct stat *ub; -}; /* ARGSUSED */ int -stat(p, uap, retval) +pathconf(p, uap, retval) struct proc *p; - register struct stat_args *uap; + register struct pathconf_args *uap; register_t *retval; { - struct stat sb; int error; struct nameidata nd; + struct vfs_context context; - NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | SHAREDLEAF | AUDITVNPATH1, - UIO_USERSPACE, uap->path, p); + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + + NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNPATH1, + UIO_USERSPACE, uap->path, &context); error = namei(&nd); if (error) return (error); - error = vn_stat(nd.ni_vp, &sb, p); - vput(nd.ni_vp); - if (error) - return (error); - error = copyout((caddr_t)&sb, (caddr_t)uap->ub, STATBUFSIZE); - return (error); -} - -/* - * Get file status; this version does not follow links. - */ -struct lstat_args { - char *path; - struct stat *ub; -}; -/* ARGSUSED */ -int -lstat(p, uap, retval) - struct proc *p; - register struct lstat_args *uap; - register_t *retval; -{ - int error; - struct vnode *vp; - struct stat sb; - struct nameidata nd; - - NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNPATH1, UIO_USERSPACE, - uap->path, p); - error = namei(&nd); - if (error) - return (error); - vp = nd.ni_vp; - error = vn_stat(vp, &sb, p); - vput(vp); - if (error) - return (error); - error = copyout((caddr_t)&sb, (caddr_t)uap->ub, STATBUFSIZE); - return (error); -} -/* - * Get configurable pathname variables. - */ -struct pathconf_args { - char *path; - int name; -}; -/* ARGSUSED */ -int -pathconf(p, uap, retval) - struct proc *p; - register struct pathconf_args *uap; - register_t *retval; -{ - int error; - struct nameidata nd; + error = vn_pathconf(nd.ni_vp, uap->name, retval, &context); - NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNPATH1, UIO_USERSPACE, - uap->path, p); - error = namei(&nd); - if (error) - return (error); - error = VOP_PATHCONF(nd.ni_vp, uap->name, retval); - vput(nd.ni_vp); + vnode_put(nd.ni_vp); + nameidone(&nd); return (error); } /* * Return target name of a symbolic link. */ -struct readlink_args { - char *path; - char *buf; - int count; -}; /* ARGSUSED */ int readlink(p, uap, retval) @@ -1975,212 +2650,341 @@ readlink(p, uap, retval) register_t *retval; { register struct vnode *vp; - struct iovec aiov; - struct uio auio; + uio_t auio; + int spacetype = proc_is64bit(p) ? UIO_USERSPACE64 : UIO_USERSPACE32; int error; struct nameidata nd; + struct vfs_context context; + char uio_buf[ UIO_SIZEOF(1) ]; + + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); - NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNPATH1, UIO_USERSPACE, - uap->path, p); + NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNPATH1, + UIO_USERSPACE, uap->path, &context); error = namei(&nd); if (error) return (error); vp = nd.ni_vp; + + nameidone(&nd); + + auio = uio_createwithbuffer(1, 0, spacetype, UIO_READ, + &uio_buf[0], sizeof(uio_buf)); + uio_addiov(auio, uap->buf, uap->count); if (vp->v_type != VLNK) error = EINVAL; else { - aiov.iov_base = uap->buf; - aiov.iov_len = uap->count; - auio.uio_iov = &aiov; - auio.uio_iovcnt = 1; - auio.uio_offset = 0; - auio.uio_rw = UIO_READ; - auio.uio_segflg = UIO_USERSPACE; - auio.uio_procp = p; - auio.uio_resid = uap->count; - error = VOP_READLINK(vp, &auio, p->p_ucred); - } - vput(vp); - *retval = uap->count - auio.uio_resid; + error = vnode_authorize(vp, NULL, KAUTH_VNODE_READ_DATA, &context); + if (error == 0) + error = VNOP_READLINK(vp, auio, &context); + } + vnode_put(vp); + // LP64todo - fix this + *retval = uap->count - (int)uio_resid(auio); return (error); } +/* + * Change file flags. + */ +static int +chflags1(vnode_t vp, int flags, vfs_context_t ctx) +{ + struct vnode_attr va; + kauth_action_t action; + int error; + + VATTR_INIT(&va); + VATTR_SET(&va, va_flags, flags); + + /* request authorisation, disregard immutability */ + if ((error = vnode_authattr(vp, &va, &action, ctx)) != 0) + goto out; + /* + * Request that the auth layer disregard those file flags it's allowed to when + * authorizing this operation; we need to do this in order to be able to + * clear immutable flags. + */ + if (action && ((error = vnode_authorize(vp, NULL, action | KAUTH_VNODE_NOIMMUTABLE, ctx)) != 0)) + goto out; + error = vnode_setattr(vp, &va, ctx); + +out: + vnode_put(vp); + return(error); +} + /* * Change flags of a file given a path name. */ -struct chflags_args { - char *path; - int flags; -}; /* ARGSUSED */ int -chflags(p, uap, retval) - struct proc *p; - register struct chflags_args *uap; - register_t *retval; +chflags(struct proc *p, register struct chflags_args *uap, __unused register_t *retval) { register struct vnode *vp; - struct vattr vattr; + struct vfs_context context; int error; struct nameidata nd; + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + AUDIT_ARG(fflags, uap->flags); - NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNPATH1, UIO_USERSPACE, uap->path, p); + NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNPATH1, + UIO_USERSPACE, uap->path, &context); error = namei(&nd); if (error) return (error); vp = nd.ni_vp; - VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE); - vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); - VATTR_NULL(&vattr); - vattr.va_flags = uap->flags; - error = VOP_SETATTR(vp, &vattr, p->p_ucred, p); - vput(vp); - return (error); + nameidone(&nd); + + error = chflags1(vp, uap->flags, &context); + + return(error); } /* * Change flags of a file given a file descriptor. */ -struct fchflags_args { - int fd; - int flags; -}; /* ARGSUSED */ int -fchflags(p, uap, retval) - struct proc *p; - register struct fchflags_args *uap; - register_t *retval; +fchflags(struct proc *p, register struct fchflags_args *uap, __unused register_t *retval) { - struct vattr vattr; + struct vfs_context context; struct vnode *vp; - struct file *fp; int error; AUDIT_ARG(fd, uap->fd); AUDIT_ARG(fflags, uap->flags); - if (error = getvnode(p, uap->fd, &fp)) + if ( (error = file_vnode(uap->fd, &vp)) ) return (error); - vp = (struct vnode *)fp->f_data; - - VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE); - vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); + if ((error = vnode_getwithref(vp))) { + file_drop(uap->fd); + return(error); + } AUDIT_ARG(vnpath, vp, ARG_VNODE1); - VATTR_NULL(&vattr); - vattr.va_flags = uap->flags; - error = VOP_SETATTR(vp, &vattr, p->p_ucred, p); - VOP_UNLOCK(vp, 0, p); + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + + error = chflags1(vp, uap->flags, &context); + + file_drop(uap->fd); + return (error); +} + +/* + * Change security information on a filesystem object. + */ +static int +chmod2(vfs_context_t ctx, struct vnode *vp, struct vnode_attr *vap) +{ + kauth_action_t action; + int error; + + AUDIT_ARG(mode, (mode_t)vap->va_mode); +#warning XXX audit new args + + /* make sure that the caller is allowed to set this security information */ + if (((error = vnode_authattr(vp, vap, &action, ctx)) != 0) || + ((error = vnode_authorize(vp, NULL, action, ctx)) != 0)) { + if (error == EACCES) + error = EPERM; + return(error); + } + + error = vnode_setattr(vp, vap, ctx); + return (error); } + /* * Change mode of a file given path name. */ -struct chmod_args { - char *path; - int mode; -}; -/* ARGSUSED */ +static int +chmod1(vfs_context_t ctx, user_addr_t path, struct vnode_attr *vap) +{ + struct nameidata nd; + int error; + + NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNPATH1, + UIO_USERSPACE, path, ctx); + if ((error = namei(&nd))) + return (error); + error = chmod2(ctx, nd.ni_vp, vap); + vnode_put(nd.ni_vp); + nameidone(&nd); + return(error); +} + int -chmod(p, uap, retval) - struct proc *p; - register struct chmod_args *uap; - register_t *retval; +chmod_extended(struct proc *p, struct chmod_extended_args *uap, __unused register_t *retval) { - register struct vnode *vp; - struct vattr vattr; + struct vfs_context context; int error; - struct nameidata nd; + struct vnode_attr va; + kauth_filesec_t xsecdst; + + VATTR_INIT(&va); + if (uap->mode != -1) + VATTR_SET(&va, va_mode, uap->mode & ALLPERMS); + if (uap->uid != KAUTH_UID_NONE) + VATTR_SET(&va, va_uid, uap->uid); + if (uap->gid != KAUTH_GID_NONE) + VATTR_SET(&va, va_gid, uap->gid); + + xsecdst = NULL; + switch(uap->xsecurity) { + /* explicit remove request */ + case CAST_USER_ADDR_T((void *)1): /* _FILESEC_REMOVE_ACL */ + VATTR_SET(&va, va_acl, NULL); + break; + /* not being set */ + case USER_ADDR_NULL: + break; + default: + if ((error = kauth_copyinfilesec(uap->xsecurity, &xsecdst)) != 0) + return(error); + VATTR_SET(&va, va_acl, &xsecdst->fsec_acl); + KAUTH_DEBUG("CHMOD - setting ACL with %d entries", va.va_acl->acl_entrycount); + } + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); - AUDIT_ARG(mode, (mode_t)uap->mode); + error = chmod1(&context, uap->path, &va); - NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNPATH1, UIO_USERSPACE, uap->path, p); - error = namei(&nd); - if (error) - return (error); - vp = nd.ni_vp; - VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE); - vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); - VATTR_NULL(&vattr); - vattr.va_mode = uap->mode & ALLPERMS; - error = VOP_SETATTR(vp, &vattr, p->p_ucred, p); + if (xsecdst != NULL) + kauth_filesec_free(xsecdst); + return(error); +} - vput(vp); - return (error); +int +chmod(struct proc *p, register struct chmod_args *uap, __unused register_t *retval) +{ + struct vfs_context context; + struct vnode_attr va; + + VATTR_INIT(&va); + VATTR_SET(&va, va_mode, uap->mode & ALLPERMS); + + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + + return(chmod1(&context, uap->path, &va)); } /* * Change mode of a file given a file descriptor. */ -struct fchmod_args { - int fd; - int mode; -}; -/* ARGSUSED */ -int -fchmod(p, uap, retval) - struct proc *p; - register struct fchmod_args *uap; - register_t *retval; +static int +fchmod1(struct proc *p, int fd, struct vnode_attr *vap) { - struct vattr vattr; struct vnode *vp; - struct file *fp; int error; + struct vfs_context context; - AUDIT_ARG(fd, uap->fd); - AUDIT_ARG(mode, (mode_t)uap->mode); - if (error = getvnode(p, uap->fd, &fp)) - return (error); + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); - vp = (struct vnode *)fp->f_data; - VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE); - vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); + AUDIT_ARG(fd, fd); + if ((error = file_vnode(fd, &vp)) != 0) + return (error); + if ((error = vnode_getwithref(vp)) != 0) { + file_drop(fd); + return(error); + } AUDIT_ARG(vnpath, vp, ARG_VNODE1); - VATTR_NULL(&vattr); - vattr.va_mode = uap->mode & ALLPERMS; - AUDIT_ARG(mode, (mode_t)vattr.va_mode); - error = VOP_SETATTR(vp, &vattr, p->p_ucred, p); - - VOP_UNLOCK(vp, 0, p); + error = chmod2(&context, vp, vap); + (void)vnode_put(vp); + file_drop(fd); return (error); } +int +fchmod_extended(struct proc *p, struct fchmod_extended_args *uap, __unused register_t *retval) +{ + int error; + struct vnode_attr va; + kauth_filesec_t xsecdst; + + VATTR_INIT(&va); + if (uap->mode != -1) + VATTR_SET(&va, va_mode, uap->mode & ALLPERMS); + if (uap->uid != KAUTH_UID_NONE) + VATTR_SET(&va, va_uid, uap->uid); + if (uap->gid != KAUTH_GID_NONE) + VATTR_SET(&va, va_gid, uap->gid); + + xsecdst = NULL; + switch(uap->xsecurity) { + case USER_ADDR_NULL: + VATTR_SET(&va, va_acl, NULL); + break; + case CAST_USER_ADDR_T(-1): + break; + default: + if ((error = kauth_copyinfilesec(uap->xsecurity, &xsecdst)) != 0) + return(error); + VATTR_SET(&va, va_acl, &xsecdst->fsec_acl); + } + + error = fchmod1(p, uap->fd, &va); + + + switch(uap->xsecurity) { + case USER_ADDR_NULL: + case CAST_USER_ADDR_T(-1): + break; + default: + if (xsecdst != NULL) + kauth_filesec_free(xsecdst); + } + return(error); +} + +int +fchmod(struct proc *p, register struct fchmod_args *uap, __unused register_t *retval) +{ + struct vnode_attr va; + + VATTR_INIT(&va); + VATTR_SET(&va, va_mode, uap->mode & ALLPERMS); + + return(fchmod1(p, uap->fd, &va)); +} + + /* * Set ownership given a path name. */ -struct chown_args { - char *path; - int uid; - int gid; -}; /* ARGSUSED */ -int -chown(p, uap, retval) - struct proc *p; - register struct chown_args *uap; - register_t *retval; +static int +chown1(vfs_context_t ctx, register struct chown_args *uap, __unused register_t *retval, int follow) { register struct vnode *vp; - struct vattr vattr; + struct vnode_attr va; int error; struct nameidata nd; + kauth_action_t action; AUDIT_ARG(owner, uap->uid, uap->gid); - NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNPATH1, UIO_USERSPACE, uap->path, p); + NDINIT(&nd, LOOKUP, (follow ? FOLLOW : 0) | AUDITVNPATH1, + UIO_USERSPACE, uap->path, ctx); error = namei(&nd); if (error) return (error); vp = nd.ni_vp; + nameidone(&nd); + /* * XXX A TEMPORARY HACK FOR NOW: Try to track console_user * by looking for chown() calls on /dev/console from a console process. @@ -2190,73 +2994,130 @@ chown(p, uap, retval) (minor(vp->v_specinfo->si_rdev) == 0)) { console_user = uap->uid; }; + VATTR_INIT(&va); + if (uap->uid != VNOVAL) + VATTR_SET(&va, va_uid, uap->uid); + if (uap->gid != VNOVAL) + VATTR_SET(&va, va_gid, uap->gid); + + /* preflight and authorize attribute changes */ + if ((error = vnode_authattr(vp, &va, &action, ctx)) != 0) + goto out; + if (action && ((error = vnode_authorize(vp, NULL, action, ctx)) != 0)) + goto out; + error = vnode_setattr(vp, &va, ctx); + +out: + /* + * EACCES is only allowed from namei(); permissions failure should + * return EPERM, so we need to translate the error code. + */ + if (error == EACCES) + error = EPERM; - VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE); - vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); - VATTR_NULL(&vattr); - vattr.va_uid = uap->uid; - vattr.va_gid = uap->gid; - error = VOP_SETATTR(vp, &vattr, p->p_ucred, p); - - vput(vp); + vnode_put(vp); return (error); } +int +chown(struct proc *p, register struct chown_args *uap, register_t *retval) +{ + struct vfs_context context; + + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + + return chown1(&context, uap, retval, 1); +} + +int +lchown(struct proc *p, register struct lchown_args *uap, register_t *retval) +{ + struct vfs_context context; + + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + + /* Argument list identical, but machine generated; cast for chown1() */ + return chown1(&context, (struct chown_args *)uap, retval, 0); +} + /* * Set ownership given a file descriptor. */ -struct fchown_args { - int fd; - int uid; - int gid; -}; /* ARGSUSED */ int -fchown(p, uap, retval) - struct proc *p; - register struct fchown_args *uap; - register_t *retval; +fchown(struct proc *p, register struct fchown_args *uap, __unused register_t *retval) { - struct vattr vattr; + struct vnode_attr va; + struct vfs_context context; struct vnode *vp; - struct file *fp; int error; + kauth_action_t action; AUDIT_ARG(owner, uap->uid, uap->gid); AUDIT_ARG(fd, uap->fd); - if (error = getvnode(p, uap->fd, &fp)) + if ( (error = file_vnode(uap->fd, &vp)) ) return (error); - vp = (struct vnode *)fp->f_data; - VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE); - vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); - + if ( (error = vnode_getwithref(vp)) ) { + file_drop(uap->fd); + return(error); + } AUDIT_ARG(vnpath, vp, ARG_VNODE1); - VATTR_NULL(&vattr); - vattr.va_uid = uap->uid; - vattr.va_gid = uap->gid; - error = VOP_SETATTR(vp, &vattr, p->p_ucred, p); + VATTR_INIT(&va); + if (uap->uid != VNOVAL) + VATTR_SET(&va, va_uid, uap->uid); + if (uap->gid != VNOVAL) + VATTR_SET(&va, va_gid, uap->gid); + + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + + /* preflight and authorize attribute changes */ + if ((error = vnode_authattr(vp, &va, &action, &context)) != 0) + goto out; + if (action && ((error = vnode_authorize(vp, NULL, action, &context)) != 0)) { + if (error == EACCES) + error = EPERM; + goto out; + } + error = vnode_setattr(vp, &va, &context); - VOP_UNLOCK(vp, 0, p); +out: + (void)vnode_put(vp); + file_drop(uap->fd); return (error); } static int getutimes(usrtvp, tsp) - const struct timeval *usrtvp; + user_addr_t usrtvp; struct timespec *tsp; { - struct timeval tv[2]; + struct user_timeval tv[2]; int error; - if (usrtvp == NULL) { - microtime(&tv[0]); - TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]); + if (usrtvp == USER_ADDR_NULL) { + struct timeval old_tv; + /* XXX Y2038 bug because of microtime argument */ + microtime(&old_tv); + TIMEVAL_TO_TIMESPEC(&old_tv, &tsp[0]); tsp[1] = tsp[0]; } else { - if ((error = copyin((void *)usrtvp, (void *)tv, sizeof (tv))) != 0) + if (IS_64BIT_PROCESS(current_proc())) { + error = copyin(usrtvp, (void *)tv, sizeof(tv)); + } else { + struct timeval old_tv[2]; + error = copyin(usrtvp, (void *)old_tv, sizeof(old_tv)); + tv[0].tv_sec = old_tv[0].tv_sec; + tv[0].tv_usec = old_tv[0].tv_usec; + tv[1].tv_sec = old_tv[1].tv_sec; + tv[1].tv_usec = old_tv[1].tv_usec; + } + if (error) return (error); TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]); TIMEVAL_TO_TIMESPEC(&tv[1], &tsp[1]); @@ -2265,30 +3126,28 @@ getutimes(usrtvp, tsp) } static int -setutimes(p, vp, ts, nullflag) - struct proc *p; - struct vnode *vp; - const struct timespec *ts; - int nullflag; +setutimes(vfs_context_t ctx, struct vnode *vp, const struct timespec *ts, + int nullflag) { int error; - struct vattr vattr; - - VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE); - error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); - if (error) - goto out; + struct vnode_attr va; + kauth_action_t action; AUDIT_ARG(vnpath, vp, ARG_VNODE1); - VATTR_NULL(&vattr); - vattr.va_atime = ts[0]; - vattr.va_mtime = ts[1]; + VATTR_INIT(&va); + VATTR_SET(&va, va_access_time, ts[0]); + VATTR_SET(&va, va_modify_time, ts[1]); if (nullflag) - vattr.va_vaflags |= VA_UTIMES_NULL; - error = VOP_SETATTR(vp, &vattr, p->p_ucred, p); + va.va_vaflags |= VA_UTIMES_NULL; + + if ((error = vnode_authattr(vp, &va, &action, ctx)) != 0) + goto out; + /* since we may not need to auth anything, check here */ + if ((action != 0) && ((error = vnode_authorize(vp, NULL, action, ctx)) != 0)) + goto out; + error = vnode_setattr(vp, &va, ctx); - VOP_UNLOCK(vp, 0, p); out: return error; } @@ -2296,121 +3155,119 @@ out: /* * Set the access and modification times of a file. */ -struct utimes_args { - char *path; - struct timeval *tptr; -}; /* ARGSUSED */ int -utimes(p, uap, retval) - struct proc *p; - register struct utimes_args *uap; - register_t *retval; +utimes(struct proc *p, register struct utimes_args *uap, __unused register_t *retval) { struct timespec ts[2]; - struct timeval *usrtvp; + user_addr_t usrtvp; int error; struct nameidata nd; + struct vfs_context context; + + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); /* AUDIT: Needed to change the order of operations to do the * name lookup first because auditing wants the path. */ - NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNPATH1, UIO_USERSPACE, uap->path, p); + NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNPATH1, + UIO_USERSPACE, uap->path, &context); error = namei(&nd); if (error) return (error); + nameidone(&nd); + /* + * Fetch the user-supplied time. If usrtvp is USER_ADDR_NULL, we fetch + * the current time instead. + */ usrtvp = uap->tptr; - if ((error = getutimes(usrtvp, ts)) != 0) { - vrele(nd.ni_vp); - return (error); - } - error = setutimes(p, nd.ni_vp, ts, usrtvp == NULL); - vrele(nd.ni_vp); + if ((error = getutimes(usrtvp, ts)) != 0) + goto out; + + error = setutimes(&context, nd.ni_vp, ts, usrtvp == USER_ADDR_NULL); + +out: + vnode_put(nd.ni_vp); return (error); } /* * Set the access and modification times of a file. */ -struct futimes_args { - int fd; - struct timeval *tptr; -}; /* ARGSUSED */ int -futimes(p, uap, retval) - struct proc *p; - register struct futimes_args *uap; - register_t *retval; +futimes(struct proc *p, register struct futimes_args *uap, __unused register_t *retval) { struct timespec ts[2]; - struct file *fp; - struct timeval *usrtvp; + struct vnode *vp; + user_addr_t usrtvp; int error; + struct vfs_context context; + + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); AUDIT_ARG(fd, uap->fd); usrtvp = uap->tptr; if ((error = getutimes(usrtvp, ts)) != 0) return (error); - if ((error = getvnode(p, uap->fd, &fp)) != 0) + if ((error = file_vnode(uap->fd, &vp)) != 0) return (error); + if((error = vnode_getwithref(vp))) { + file_drop(uap->fd); + return(error); + } - return setutimes(p, (struct vnode *)fp->f_data, ts, usrtvp == NULL); + error = setutimes(&context, vp, ts, usrtvp == 0); + vnode_put(vp); + file_drop(uap->fd); + return(error); } /* * Truncate a file given its path name. */ -struct truncate_args { - char *path; -#ifdef DOUBLE_ALIGN_PARAMS - int pad; -#endif - off_t length; -}; /* ARGSUSED */ int -truncate(p, uap, retval) - struct proc *p; - register struct truncate_args *uap; - register_t *retval; +truncate(struct proc *p, register struct truncate_args *uap, __unused register_t *retval) { register struct vnode *vp; - struct vattr vattr; + struct vnode_attr va; + struct vfs_context context; int error; struct nameidata nd; + kauth_action_t action; + + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); if (uap->length < 0) return(EINVAL); - NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNPATH1, UIO_USERSPACE, uap->path, p); - if (error = namei(&nd)) + NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNPATH1, + UIO_USERSPACE, uap->path, &context); + if ((error = namei(&nd))) return (error); vp = nd.ni_vp; - VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE); - vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); - if (vp->v_type == VDIR) - error = EISDIR; - else if ((error = vn_writechk(vp)) == 0 && - (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) { - VATTR_NULL(&vattr); - vattr.va_size = uap->length; - error = VOP_SETATTR(vp, &vattr, p->p_ucred, p); - } - vput(vp); + + nameidone(&nd); + + VATTR_INIT(&va); + VATTR_SET(&va, va_data_size, uap->length); + if ((error = vnode_authattr(vp, &va, &action, &context)) != 0) + goto out; + if ((action != 0) && ((error = vnode_authorize(vp, NULL, action, &context)) != 0)) + goto out; + error = vnode_setattr(vp, &va, &context); +out: + vnode_put(vp); return (error); } /* * Truncate a file given a file descriptor. */ -struct ftruncate_args { - int fd; -#ifdef DOUBLE_ALIGN_PARAMS - int pad; -#endif - off_t length; -}; /* ARGSUSED */ int ftruncate(p, uap, retval) @@ -2418,149 +3275,103 @@ ftruncate(p, uap, retval) register struct ftruncate_args *uap; register_t *retval; { - struct vattr vattr; + struct vfs_context context; + struct vnode_attr va; struct vnode *vp; - struct file *fp; - int error; + struct fileproc *fp; + int error ; + int fd = uap->fd; + context.vc_proc = current_proc(); + context.vc_ucred = kauth_cred_get(); + AUDIT_ARG(fd, uap->fd); if (uap->length < 0) return(EINVAL); - if (error = fdgetf(p, uap->fd, &fp)) - return (error); + if ( (error = fp_lookup(p,fd,&fp,0)) ) { + return(error); + } - if (fp->f_type == DTYPE_PSXSHM) { - return(pshm_truncate(p, fp, uap->fd, uap->length, retval)); + if (fp->f_fglob->fg_type == DTYPE_PSXSHM) { + error = pshm_truncate(p, fp, uap->fd, uap->length, retval); + goto out; + } + if (fp->f_fglob->fg_type != DTYPE_VNODE) { + error = EINVAL; + goto out; } - if (fp->f_type != DTYPE_VNODE) - return (EINVAL); - AUDIT_ARG(vnpath, (struct vnode *)fp->f_data, ARG_VNODE1); + vp = (struct vnode *)fp->f_fglob->fg_data; - if ((fp->f_flag & FWRITE) == 0) - return (EINVAL); - vp = (struct vnode *)fp->f_data; - VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE); - vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); - if (vp->v_type == VDIR) - error = EISDIR; - else if ((error = vn_writechk(vp)) == 0) { - VATTR_NULL(&vattr); - vattr.va_size = uap->length; - error = VOP_SETATTR(vp, &vattr, fp->f_cred, p); + if ((fp->f_fglob->fg_flag & FWRITE) == 0) { + AUDIT_ARG(vnpath_withref, vp, ARG_VNODE1); + error = EINVAL; + goto out; } - VOP_UNLOCK(vp, 0, p); - return (error); -} - -#if COMPAT_43 -/* - * Truncate a file given its path name. - */ -struct otruncate_args { - char *path; - long length; -}; -/* ARGSUSED */ -int -otruncate(p, uap, retval) - struct proc *p; - register struct otruncate_args *uap; - register_t *retval; -{ - struct truncate_args /* { - syscallarg(char *) path; -#ifdef DOUBLE_ALIGN_PARAMS - syscallarg(int) pad; -#endif - syscallarg(off_t) length; - } */ nuap; - nuap.path = uap->path; - nuap.length = uap->length; - return (truncate(p, &nuap, retval)); -} + if ((error = vnode_getwithref(vp)) != 0) { + goto out; + } -/* - * Truncate a file given a file descriptor. - */ -struct oftruncate_args { - int fd; - long length; -}; -/* ARGSUSED */ -int -oftruncate(p, uap, retval) - struct proc *p; - register struct oftruncate_args *uap; - register_t *retval; -{ - struct ftruncate_args /* { - syscallarg(int) fd; -#ifdef DOUBLE_ALIGN_PARAMS - syscallarg(int) pad; -#endif - syscallarg(off_t) length; - } */ nuap; + AUDIT_ARG(vnpath, vp, ARG_VNODE1); - nuap.fd = uap->fd; - nuap.length = uap->length; - return (ftruncate(p, &nuap, retval)); + VATTR_INIT(&va); + VATTR_SET(&va, va_data_size, uap->length); + error = vnode_setattr(vp, &va, &context); + (void)vnode_put(vp); +out: + file_drop(fd); + return (error); } -#endif /* COMPAT_43 */ + /* * Sync an open file. */ -struct fsync_args { - int fd; -}; /* ARGSUSED */ int -fsync(p, uap, retval) - struct proc *p; - struct fsync_args *uap; - register_t *retval; +fsync(struct proc *p, struct fsync_args *uap, __unused register_t *retval) { - register struct vnode *vp; - struct file *fp; + struct vnode *vp; + struct fileproc *fp; + struct vfs_context context; int error; - if (error = getvnode(p, uap->fd, &fp)) + if ( (error = fp_getfvp(p, uap->fd, &fp, &vp)) ) return (error); - if (fref(fp) == -1) - return (EBADF); - vp = (struct vnode *)fp->f_data; - vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); - error = VOP_FSYNC(vp, fp->f_cred, MNT_WAIT, p); - VOP_UNLOCK(vp, 0, p); - frele(fp); + if ( (error = vnode_getwithref(vp)) ) { + file_drop(uap->fd); + return(error); + } + context.vc_proc = p; + context.vc_ucred = fp->f_fglob->fg_cred; + + error = VNOP_FSYNC(vp, MNT_WAIT, &context); + + (void)vnode_put(vp); + file_drop(uap->fd); return (error); } /* * Duplicate files. Source must be a file, target must be a file or * must not exist. + * + * XXX Copyfile authorisation checking is woefully inadequate, and will not + * perform inheritance correctly. */ - -struct copyfile_args { - char *from; - char *to; - int mode; - int flags; -}; /* ARGSUSED */ int -copyfile(p, uap, retval) - struct proc *p; - register struct copyfile_args *uap; - register_t *retval; +copyfile(struct proc *p, register struct copyfile_args *uap, __unused register_t *retval) { - register struct vnode *tvp, *fvp, *tdvp; - register struct ucred *cred = p->p_ucred; + vnode_t tvp, fvp, tdvp, sdvp; struct nameidata fromnd, tond; int error; + struct vfs_context context; + + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); /* Check that the flags are valid. */ @@ -2569,32 +3380,31 @@ copyfile(p, uap, retval) } NDINIT(&fromnd, LOOKUP, SAVESTART | AUDITVNPATH1, - UIO_USERSPACE, uap->from, p); - if (error = namei(&fromnd)) + UIO_USERSPACE, uap->from, &context); + if ((error = namei(&fromnd))) return (error); fvp = fromnd.ni_vp; - NDINIT(&tond, CREATE, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | AUDITVNPATH2, - UIO_USERSPACE, uap->to, p); - if (error = namei(&tond)) { - vrele(fvp); + NDINIT(&tond, CREATE, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | AUDITVNPATH2, + UIO_USERSPACE, uap->to, &context); + if ((error = namei(&tond))) { goto out1; } tdvp = tond.ni_dvp; tvp = tond.ni_vp; + if (tvp != NULL) { if (!(uap->flags & CPF_OVERWRITE)) { error = EEXIST; goto out; } } - if (fvp->v_type == VDIR || (tvp && tvp->v_type == VDIR)) { error = EISDIR; goto out; } - if (error = VOP_ACCESS(tdvp, VWRITE, cred, p)) + if ((error = vnode_authorize(tdvp, NULL, KAUTH_VNODE_ADD_FILE, &context)) != 0) goto out; if (fvp == tdvp) @@ -2606,121 +3416,151 @@ copyfile(p, uap, retval) */ if (fvp == tvp) error = -1; + if (!error) + error = VNOP_COPYFILE(fvp,tdvp,tvp,&tond.ni_cnd,uap->mode,uap->flags,&context); out: - if (!error) { - error = VOP_COPYFILE(fvp,tdvp,tvp,&tond.ni_cnd,uap->mode,uap->flags); - } else { - VOP_ABORTOP(tdvp, &tond.ni_cnd); - if (tdvp == tvp) - vrele(tdvp); - else - vput(tdvp); - if (tvp) - vput(tvp); - vrele(fvp); - } - vrele(tond.ni_startdir); - FREE_ZONE(tond.ni_cnd.cn_pnbuf, tond.ni_cnd.cn_pnlen, M_NAMEI); + sdvp = tond.ni_startdir; + /* + * nameidone has to happen before we vnode_put(tdvp) + * since it may need to release the fs_nodelock on the tdvp + */ + nameidone(&tond); + + if (tvp) + vnode_put(tvp); + vnode_put(tdvp); + vnode_put(sdvp); out1: + vnode_put(fvp); + if (fromnd.ni_startdir) - vrele(fromnd.ni_startdir); - FREE_ZONE(fromnd.ni_cnd.cn_pnbuf, fromnd.ni_cnd.cn_pnlen, M_NAMEI); + vnode_put(fromnd.ni_startdir); + nameidone(&fromnd); + if (error == -1) return (0); return (error); } + /* * Rename files. Source and destination must either both be directories, * or both not be directories. If target is a directory, it must be empty. */ -struct rename_args { - char *from; - char *to; -}; /* ARGSUSED */ int -rename(p, uap, retval) - struct proc *p; - register struct rename_args *uap; - register_t *retval; +rename(proc_t p, register struct rename_args *uap, __unused register_t *retval) { - register struct vnode *tvp, *fvp, *tdvp; + vnode_t tvp, tdvp; + vnode_t fvp, fdvp; struct nameidata fromnd, tond; + struct vfs_context context; int error; int mntrename; - int casesense,casepres; - char *nameptr=NULL, *oname; - struct vnode *oparent; + char *oname, *from_name, *to_name; + int from_len, to_len; + int holding_mntlock; + mount_t locked_mp = NULL; + vnode_t oparent; + fse_info from_finfo, to_finfo; + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + holding_mntlock = 0; +retry: + fvp = tvp = NULL; + fdvp = tdvp = NULL; mntrename = FALSE; - bwillwrite(); - NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNPATH1, - UIO_USERSPACE, uap->from, p); - error = namei(&fromnd); - if (error) - return (error); - fvp = fromnd.ni_vp; + NDINIT(&fromnd, DELETE, WANTPARENT | AUDITVNPATH1, UIO_USERSPACE, uap->from, &context); + + if ( (error = namei(&fromnd)) ) + goto out1; + fdvp = fromnd.ni_dvp; + fvp = fromnd.ni_vp; - NDINIT(&tond, RENAME, - LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | AUDITVNPATH2, - UIO_USERSPACE, uap->to, p); - if (fromnd.ni_vp->v_type == VDIR) + NDINIT(&tond, RENAME, WANTPARENT | AUDITVNPATH2, UIO_USERSPACE, uap->to, &context); + if (fvp->v_type == VDIR) tond.ni_cnd.cn_flags |= WILLBEDIR; - if (error = namei(&tond)) { - /* Translate error code for rename("dir1", "dir2/."). */ - if (error == EISDIR && fvp->v_type == VDIR) - error = EINVAL; - VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd); - vrele(fromnd.ni_dvp); - vrele(fvp); - goto out2; + + if ( (error = namei(&tond)) ) { + /* + * Translate error code for rename("dir1", "dir2/."). + */ + if (error == EISDIR && fvp->v_type == VDIR) + error = EINVAL; + goto out1; } tdvp = tond.ni_dvp; - tvp = tond.ni_vp; + tvp = tond.ni_vp; if (tvp != NULL) { if (fvp->v_type == VDIR && tvp->v_type != VDIR) { error = ENOTDIR; - goto out; + goto out1; } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) { error = EISDIR; - goto out; + goto out1; } } - if (fvp == tdvp) + if (fvp == tdvp) { error = EINVAL; + goto out1; + } + /* - * If source is the same as the destination (that is the - * same inode number) then there is nothing to do... EXCEPT if the - * underlying file system supports case insensitivity and is case - * preserving. Then a special case is made, i.e. foo -> Foo. + * Authorization. * - * Only file systems that support pathconf selectors _PC_CASE_SENSITIVE - * and _PC_CASE_PRESERVING can have this exception, and they need to - * handle the special case of getting the same vnode as target and - * source. NOTE: Then the target is unlocked going into VOP_RENAME, - * so not to cause locking problems. There is a single reference on tvp. + * If tvp is a directory and not the same as fdvp, or tdvp is not the same as fdvp, + * the node is moving between directories and we need rights to remove from the + * old and add to the new. * - * NOTE - that fvp == tvp also occurs if they are hard linked - NOTE - * that correct behaviour then is just to remove the source (link) + * If tvp already exists and is not a directory, we need to be allowed to delete it. + * + * Note that we do not inherit when renaming. XXX this needs to be revisited to + * implement the deferred-inherit bit. */ - if (fvp == tvp && fromnd.ni_dvp == tdvp) { - if (fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen && - !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr, - fromnd.ni_cnd.cn_namelen)) { - error = -1; /* Default "unix" behavior */ - } else { /* probe for file system specifics */ - if (VOP_PATHCONF(tdvp, _PC_CASE_SENSITIVE, &casesense)) - casesense = 1; - if (VOP_PATHCONF(tdvp, _PC_CASE_PRESERVING, &casepres)) - casepres = 1; - if (!casesense && casepres) - vput(tvp); /* Unlock target and drop ref */ + { + int moving = 0; + + error = 0; + if ((tvp != NULL) && vnode_isdir(tvp)) { + if (tvp != fdvp) + moving = 1; + } else if (tdvp != fdvp) { + moving = 1; + } + /* + * must have delete rights to remove the old name even in the simple case of + * fdvp == tdvp + */ + if ((error = vnode_authorize(fvp, fdvp, KAUTH_VNODE_DELETE, &context)) != 0) + goto auth_exit; + if (moving) { + /* moving into tdvp or tvp, must have rights to add */ + if ((error = vnode_authorize(((tvp != NULL) && vnode_isdir(tvp)) ? tvp : tdvp, + NULL, + vnode_isdir(fvp) ? KAUTH_VNODE_ADD_SUBDIRECTORY : KAUTH_VNODE_ADD_FILE, + &context)) != 0) + goto auth_exit; + } else { + /* node staying in same directory, must be allowed to add new name */ + if ((error = vnode_authorize(fdvp, NULL, + vnode_isdir(fvp) ? KAUTH_VNODE_ADD_SUBDIRECTORY : KAUTH_VNODE_ADD_FILE, &context)) != 0) + goto auth_exit; } + /* overwriting tvp */ + if ((tvp != NULL) && !vnode_isdir(tvp) && + ((error = vnode_authorize(tvp, tdvp, KAUTH_VNODE_DELETE, &context)) != 0)) + goto auth_exit; + + /* XXX more checks? */ + +auth_exit: + /* authorization denied */ + if (error != 0) + goto out1; } - /* * Allow the renaming of mount points. * - target must not exist @@ -2728,534 +3568,608 @@ rename(p, uap, retval) * - union mounts cannot be renamed * - "/" cannot be renamed */ - if (!error && - (fvp->v_flag & VROOT) && + if ((fvp->v_flag & VROOT) && (fvp->v_type == VDIR) && (tvp == NULL) && (fvp->v_mountedhere == NULL) && - (fromnd.ni_dvp == tond.ni_dvp) && + (fdvp == tdvp) && ((fvp->v_mount->mnt_flag & (MNT_UNION | MNT_ROOTFS)) == 0) && (fvp->v_mount->mnt_vnodecovered != NULLVP)) { + struct vnode *coveredvp; /* switch fvp to the covered vnode */ - fromnd.ni_vp = fvp->v_mount->mnt_vnodecovered; - vrele(fvp); - fvp = fromnd.ni_vp; - VREF(fvp); + coveredvp = fvp->v_mount->mnt_vnodecovered; + if ( (vnode_getwithref(coveredvp)) ) { + error = ENOENT; + goto out1; + } + vnode_put(fvp); + + fvp = coveredvp; mntrename = TRUE; } -out: - if (!error) { - VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE); - if (fromnd.ni_dvp != tdvp) - VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE); - if (tvp) - VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE); + /* + * Check for cross-device rename. + */ + if ((fvp->v_mount != tdvp->v_mount) || + (tvp && (fvp->v_mount != tvp->v_mount))) { + error = EXDEV; + goto out1; + } + /* + * Avoid renaming "." and "..". + */ + if (fvp->v_type == VDIR && + ((fdvp == fvp) || + (fromnd.ni_cnd.cn_namelen == 1 && fromnd.ni_cnd.cn_nameptr[0] == '.') || + ((fromnd.ni_cnd.cn_flags | tond.ni_cnd.cn_flags) & ISDOTDOT)) ) { + error = EINVAL; + goto out1; + } + /* + * The following edge case is caught here: + * (to cannot be a descendent of from) + * + * o fdvp + * / + * / + * o fvp + * \ + * \ + * o tdvp + * / + * / + * o tvp + */ + if (tdvp->v_parent == fvp) { + error = EINVAL; + goto out1; + } - // XXXdbg - so that the fs won't block when it vrele()'s - // these nodes before returning - if (fromnd.ni_dvp != tdvp) { - vget(tdvp, 0, p); + /* + * If source is the same as the destination (that is the + * same inode number) then there is nothing to do... + * EXCEPT if the underlying file system supports case + * insensitivity and is case preserving. In this case + * the file system needs to handle the special case of + * getting the same vnode as target (fvp) and source (tvp). + * + * Only file systems that support pathconf selectors _PC_CASE_SENSITIVE + * and _PC_CASE_PRESERVING can have this exception, and they need to + * handle the special case of getting the same vnode as target and + * source. NOTE: Then the target is unlocked going into vnop_rename, + * so not to cause locking problems. There is a single reference on tvp. + * + * NOTE - that fvp == tvp also occurs if they are hard linked - NOTE + * that correct behaviour then is just to remove the source (link) + */ + if (fvp == tvp && fdvp == tdvp) { + if (fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen && + !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr, + fromnd.ni_cnd.cn_namelen)) { + goto out1; } - - // save these off so we can later verify that fvp is the same - oname = VNAME(fvp); - oparent = VPARENT(fvp); + } - nameptr = add_name(tond.ni_cnd.cn_nameptr, - tond.ni_cnd.cn_namelen, - tond.ni_cnd.cn_hash, 0); + if (holding_mntlock && fvp->v_mount != locked_mp) { + /* + * we're holding a reference and lock + * on locked_mp, but it no longer matches + * what we want to do... so drop our hold + */ + mount_unlock_renames(locked_mp); + mount_drop(locked_mp, 0); + holding_mntlock = 0; + } + if (tdvp != fdvp && fvp->v_type == VDIR) { + /* + * serialize renames that re-shape + * the tree... if holding_mntlock is + * set, then we're ready to go... + * otherwise we + * first need to drop the iocounts + * we picked up, second take the + * lock to serialize the access, + * then finally start the lookup + * process over with the lock held + */ + if (!holding_mntlock) { + /* + * need to grab a reference on + * the mount point before we + * drop all the iocounts... once + * the iocounts are gone, the mount + * could follow + */ + locked_mp = fvp->v_mount; + mount_ref(locked_mp, 0); + /* + * nameidone has to happen before we vnode_put(tvp) + * since it may need to release the fs_nodelock on the tvp + */ + nameidone(&tond); - error = VOP_RENAME(fromnd.ni_dvp, fvp, &fromnd.ni_cnd, - tond.ni_dvp, tvp, &tond.ni_cnd); - if (error) { - remove_name(nameptr); - nameptr = NULL; - if (fromnd.ni_dvp != tdvp) { - vrele(tdvp); - } + if (tvp) + vnode_put(tvp); + vnode_put(tdvp); + + /* + * nameidone has to happen before we vnode_put(fdvp) + * since it may need to release the fs_nodelock on the fvp + */ + nameidone(&fromnd); - goto out1; + vnode_put(fvp); + vnode_put(fdvp); + + mount_lock_renames(locked_mp); + holding_mntlock = 1; + + goto retry; } - - /* - * update filesystem's mount point data + } else { + /* + * when we dropped the iocounts to take + * the lock, we allowed the identity of + * the various vnodes to change... if they did, + * we may no longer be dealing with a rename + * that reshapes the tree... once we're holding + * the iocounts, the vnodes can't change type + * so we're free to drop the lock at this point + * and continue on */ - if (mntrename) { - char *cp, *pathend, *mpname; - char * tobuf; - struct mount *mp; - int maxlen; - size_t len = 0; - - VREF(fvp); - vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY, p); - mp = fvp->v_mountedhere; - - if (vfs_busy(mp, LK_NOWAIT, 0, p)) { - vput(fvp); - error = EBUSY; - goto out1; - } - VOP_UNLOCK(fvp, 0, p); - - MALLOC_ZONE(tobuf, char *, MAXPATHLEN, M_NAMEI, M_WAITOK); - error = copyinstr(uap->to, tobuf, MAXPATHLEN, &len); - if (!error) { - /* find current mount point prefix */ - pathend = &mp->mnt_stat.f_mntonname[0]; - for (cp = pathend; *cp != '\0'; ++cp) { - if (*cp == '/') - pathend = cp + 1; - } - /* find last component of target name */ - for (mpname = cp = tobuf; *cp != '\0'; ++cp) { - if (*cp == '/') - mpname = cp + 1; - } - /* append name to prefix */ - maxlen = MNAMELEN - (pathend - mp->mnt_stat.f_mntonname); - bzero(pathend, maxlen); - strncpy(pathend, mpname, maxlen - 1); - } - FREE_ZONE(tobuf, MAXPATHLEN, M_NAMEI); - - vrele(fvp); - vfs_unbusy(mp, p); + if (holding_mntlock) { + mount_unlock_renames(locked_mp); + mount_drop(locked_mp, 0); + holding_mntlock = 0; } + } + // save these off so we can later verify that fvp is the same + oname = fvp->v_name; + oparent = fvp->v_parent; + if (need_fsevent(FSE_RENAME, fvp)) { + get_fse_info(fvp, &from_finfo, &context); - // fix up name & parent pointers. note that we first - // check that fvp has the same name/parent pointers it - // had before the rename call and then we lock fvp so - // that it won't go away on us when we hit blocking - // points like remove_name() or vrele() where fvp could - // get recycled. - if (oname == VNAME(fvp) && oparent == VPARENT(fvp) && vget(fvp, LK_EXCLUSIVE | LK_INTERLOCK, p) == 0) { - if (VNAME(fvp)) { - char *tmp = VNAME(fvp); - VNAME(fvp) = NULL; - remove_name(tmp); - } + if (tvp) { + get_fse_info(tvp, &to_finfo, &context); + } + from_name = get_pathbuff(); + from_len = MAXPATHLEN; + vn_getpath(fvp, from_name, &from_len); - VNAME(fvp) = nameptr; - nameptr = NULL; - - if (fromnd.ni_dvp != tdvp) { - struct vnode *tmpvp; - - tmpvp = VPARENT(fvp); - VPARENT(fvp) = NULL; - vrele(tmpvp); + to_name = get_pathbuff(); + to_len = MAXPATHLEN; + + if (tvp && tvp->v_type != VDIR) { + vn_getpath(tvp, to_name, &to_len); + } else { + vn_getpath(tdvp, to_name, &to_len); + // if the path is not just "/", then append a "/" + if (to_len > 2) { + to_name[to_len-1] = '/'; + } else { + to_len--; + } + strcpy(&to_name[to_len], tond.ni_cnd.cn_nameptr); + to_len += tond.ni_cnd.cn_namelen + 1; + to_name[to_len] = '\0'; + } + } else { + from_name = NULL; + to_name = NULL; + } + error = VNOP_RENAME(fdvp, fvp, &fromnd.ni_cnd, + tdvp, tvp, &tond.ni_cnd, + &context); - VPARENT(fvp) = tdvp; + if (holding_mntlock) { + /* + * we can drop our serialization + * lock now + */ + mount_unlock_renames(locked_mp); + mount_drop(locked_mp, 0); + holding_mntlock = 0; + } + if (error) { + if (to_name != NULL) + release_pathbuff(to_name); + if (from_name != NULL) + release_pathbuff(from_name); + from_name = to_name = NULL; - // note: we don't vrele() tdvp because we want to keep - // the reference until fvp gets recycled - } - - vput(fvp); - + goto out1; + } + + /* call out to allow 3rd party notification of rename. + * Ignore result of kauth_authorize_fileop call. + */ + kauth_authorize_fileop(vfs_context_ucred(&context), KAUTH_FILEOP_RENAME, + (uintptr_t)from_name, (uintptr_t)to_name); + + if (from_name != NULL && to_name != NULL) { + if (tvp) { + add_fsevent(FSE_RENAME, &context, + FSE_ARG_STRING, from_len, from_name, + FSE_ARG_FINFO, &from_finfo, + FSE_ARG_STRING, to_len, to_name, + FSE_ARG_FINFO, &to_finfo, + FSE_ARG_DONE); } else { - // if fvp isn't kosher anymore and we locked tdvp, - // release tdvp - if (fromnd.ni_dvp != tdvp) { - vrele(tdvp); - } - remove_name(nameptr); - nameptr = NULL; + add_fsevent(FSE_RENAME, &context, + FSE_ARG_STRING, from_len, from_name, + FSE_ARG_FINFO, &from_finfo, + FSE_ARG_STRING, to_len, to_name, + FSE_ARG_DONE); + } + } + if (to_name != NULL) + release_pathbuff(to_name); + if (from_name != NULL) + release_pathbuff(from_name); + from_name = to_name = NULL; + + /* + * update filesystem's mount point data + */ + if (mntrename) { + char *cp, *pathend, *mpname; + char * tobuf; + struct mount *mp; + int maxlen; + size_t len = 0; + + mp = fvp->v_mountedhere; + + if (vfs_busy(mp, LK_NOWAIT)) { + error = EBUSY; + goto out1; } + MALLOC_ZONE(tobuf, char *, MAXPATHLEN, M_NAMEI, M_WAITOK); - } else { - VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd); - if (tdvp == tvp) - vrele(tdvp); - else - vput(tdvp); - if (tvp) - vput(tvp); - VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd); - vrele(fromnd.ni_dvp); - vrele(fvp); + error = copyinstr(uap->to, tobuf, MAXPATHLEN, &len); + if (!error) { + /* find current mount point prefix */ + pathend = &mp->mnt_vfsstat.f_mntonname[0]; + for (cp = pathend; *cp != '\0'; ++cp) { + if (*cp == '/') + pathend = cp + 1; + } + /* find last component of target name */ + for (mpname = cp = tobuf; *cp != '\0'; ++cp) { + if (*cp == '/') + mpname = cp + 1; + } + /* append name to prefix */ + maxlen = MAXPATHLEN - (pathend - mp->mnt_vfsstat.f_mntonname); + bzero(pathend, maxlen); + strncpy(pathend, mpname, maxlen - 1); + } + FREE_ZONE(tobuf, MAXPATHLEN, M_NAMEI); + + vfs_unbusy(mp); + } + /* + * fix up name & parent pointers. note that we first + * check that fvp has the same name/parent pointers it + * had before the rename call... this is a 'weak' check + * at best... + */ + if (oname == fvp->v_name && oparent == fvp->v_parent) { + int update_flags; + + update_flags = VNODE_UPDATE_NAME; + + if (fdvp != tdvp) + update_flags |= VNODE_UPDATE_PARENT; + + vnode_update_identity(fvp, tdvp, tond.ni_cnd.cn_nameptr, tond.ni_cnd.cn_namelen, tond.ni_cnd.cn_hash, update_flags); } out1: - vrele(tond.ni_startdir); - FREE_ZONE(tond.ni_cnd.cn_pnbuf, tond.ni_cnd.cn_pnlen, M_NAMEI); -out2: - if (fromnd.ni_startdir) - vrele(fromnd.ni_startdir); - FREE_ZONE(fromnd.ni_cnd.cn_pnbuf, fromnd.ni_cnd.cn_pnlen, M_NAMEI); - if (error == -1) - return (0); + if (holding_mntlock) { + mount_unlock_renames(locked_mp); + mount_drop(locked_mp, 0); + } + if (tdvp) { + /* + * nameidone has to happen before we vnode_put(tdvp) + * since it may need to release the fs_nodelock on the tdvp + */ + nameidone(&tond); + + if (tvp) + vnode_put(tvp); + vnode_put(tdvp); + } + if (fdvp) { + /* + * nameidone has to happen before we vnode_put(fdvp) + * since it may need to release the fs_nodelock on the fdvp + */ + nameidone(&fromnd); + + if (fvp) + vnode_put(fvp); + vnode_put(fdvp); + } return (error); } /* * Make a directory file. */ -struct mkdir_args { - char *path; - int mode; -}; /* ARGSUSED */ -int -mkdir(p, uap, retval) - struct proc *p; - register struct mkdir_args *uap; - register_t *retval; +static int +mkdir1(vfs_context_t ctx, user_addr_t path, struct vnode_attr *vap) { - register struct vnode *vp; - struct vattr vattr; + vnode_t vp, dvp; int error; + int update_flags = 0; struct nameidata nd; - char *nameptr; - AUDIT_ARG(mode, (mode_t)uap->mode); - bwillwrite(); - NDINIT(&nd, CREATE, LOCKPARENT | AUDITVNPATH1, UIO_USERSPACE, uap->path, p); + AUDIT_ARG(mode, vap->va_mode); + NDINIT(&nd, CREATE, LOCKPARENT | AUDITVNPATH1, + UIO_USERSPACE, path, ctx); nd.ni_cnd.cn_flags |= WILLBEDIR; error = namei(&nd); if (error) return (error); + dvp = nd.ni_dvp; vp = nd.ni_vp; - if (vp != NULL) { - VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); - if (nd.ni_dvp == vp) - vrele(nd.ni_dvp); - else - vput(nd.ni_dvp); - vrele(vp); - return (EEXIST); - } - VATTR_NULL(&vattr); - vattr.va_type = VDIR; - vattr.va_mode = (uap->mode & ACCESSPERMS) &~ p->p_fd->fd_cmask; - VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE); - nameptr = add_name(nd.ni_cnd.cn_nameptr, nd.ni_cnd.cn_namelen, nd.ni_cnd.cn_hash, 0); + if (vp != NULL) { + error = EEXIST; + goto out; + } + + /* authorize addition of a directory to the parent */ + if ((error = vnode_authorize(dvp, NULL, KAUTH_VNODE_ADD_SUBDIRECTORY, ctx)) != 0) + goto out; + + VATTR_SET(vap, va_type, VDIR); + + /* make the directory */ + if ((error = vn_create(dvp, &vp, &nd.ni_cnd, vap, 0, ctx)) != 0) + goto out; + + // Make sure the name & parent pointers are hooked up + if (vp->v_name == NULL) + update_flags |= VNODE_UPDATE_NAME; + if (vp->v_parent == NULLVP) + update_flags |= VNODE_UPDATE_PARENT; + + if (update_flags) + vnode_update_identity(vp, dvp, nd.ni_cnd.cn_nameptr, nd.ni_cnd.cn_namelen, nd.ni_cnd.cn_hash, update_flags); - error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); - if (!error) { - VNAME(nd.ni_vp) = nameptr; - if (VPARENT(nd.ni_vp) == NULL && vget(nd.ni_dvp, 0, p) == 0) { - VPARENT(nd.ni_vp) = nd.ni_dvp; - } + add_fsevent(FSE_CREATE_DIR, ctx, FSE_ARG_VNODE, vp, FSE_ARG_DONE); + +out: + /* + * nameidone has to happen before we vnode_put(dvp) + * since it may need to release the fs_nodelock on the dvp + */ + nameidone(&nd); + + if (vp) + vnode_put(vp); + vnode_put(dvp); - vput(nd.ni_vp); - } return (error); } -/* - * Remove a directory file. - */ -struct rmdir_args { - char *path; -}; -/* ARGSUSED */ + int -rmdir(p, uap, retval) - struct proc *p; - struct rmdir_args *uap; - register_t *retval; +mkdir_extended(struct proc *p, register struct mkdir_extended_args *uap, __unused register_t *retval) { - register struct vnode *vp; - int error; - struct nameidata nd; - - bwillwrite(); - NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNPATH1, UIO_USERSPACE, - uap->path, p); - error = namei(&nd); - if (error) - return (error); - vp = nd.ni_vp; - if (vp->v_type != VDIR) { - error = ENOTDIR; - goto out; - } - /* - * No rmdir "." please. - */ - if (nd.ni_dvp == vp) { - error = EINVAL; - goto out; - } - /* - * The root of a mounted filesystem cannot be deleted. - */ - if (vp->v_flag & VROOT) - error = EBUSY; -out: - if (!error) { - VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE); - VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE); - error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd); - } else { - VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); - if (nd.ni_dvp == vp) - vrele(nd.ni_dvp); - else - vput(nd.ni_dvp); - vput(vp); - } - return (error); + struct vfs_context context; + int ciferror; + kauth_filesec_t xsecdst; + struct vnode_attr va; + + xsecdst = NULL; + if ((uap->xsecurity != USER_ADDR_NULL) && + ((ciferror = kauth_copyinfilesec(uap->xsecurity, &xsecdst)) != 0)) + return ciferror; + + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + + VATTR_INIT(&va); + VATTR_SET(&va, va_mode, (uap->mode & ACCESSPERMS) & ~p->p_fd->fd_cmask); + if (xsecdst != NULL) + VATTR_SET(&va, va_acl, &xsecdst->fsec_acl); + + ciferror = mkdir1(&context, uap->path, &va); + if (xsecdst != NULL) + kauth_filesec_free(xsecdst); + return ciferror; } -#if COMPAT_43 -/* - * Read a block of directory entries in a file system independent format. - */ -struct ogetdirentries_args { - int fd; - char *buf; - u_int count; - long *basep; -}; int -ogetdirentries(p, uap, retval) - struct proc *p; - register struct ogetdirentries_args *uap; - register_t *retval; +mkdir(struct proc *p, register struct mkdir_args *uap, __unused register_t *retval) { - register struct vnode *vp; - struct file *fp; - struct uio auio, kuio; - struct iovec aiov, kiov; - struct dirent *dp, *edp; - caddr_t dirbuf; - int error, eofflag, readcnt; - long loff; + struct vfs_context context; + struct vnode_attr va; - AUDIT_ARG(fd, uap->fd); - if (error = getvnode(p, uap->fd, &fp)) - return (error); + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); - AUDIT_ARG(vnpath, (struct vnode *)fp->f_data, ARG_VNODE1); + VATTR_INIT(&va); + VATTR_SET(&va, va_mode, (uap->mode & ACCESSPERMS) & ~p->p_fd->fd_cmask); - if ((fp->f_flag & FREAD) == 0) - return (EBADF); - vp = (struct vnode *)fp->f_data; -unionread: - if (vp->v_type != VDIR) - return (EINVAL); - aiov.iov_base = uap->buf; - aiov.iov_len = uap->count; - auio.uio_iov = &aiov; - auio.uio_iovcnt = 1; - auio.uio_rw = UIO_READ; - auio.uio_segflg = UIO_USERSPACE; - auio.uio_procp = p; - auio.uio_resid = uap->count; - vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); - loff = auio.uio_offset = fp->f_offset; -# if (BYTE_ORDER != LITTLE_ENDIAN) - if (vp->v_mount->mnt_maxsymlinklen <= 0) { - error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, - (int *)0, (u_long **)0); - fp->f_offset = auio.uio_offset; - } else -# endif - { - kuio = auio; - kuio.uio_iov = &kiov; - kuio.uio_segflg = UIO_SYSSPACE; - kiov.iov_len = uap->count; - MALLOC(dirbuf, caddr_t, uap->count, M_TEMP, M_WAITOK); - kiov.iov_base = dirbuf; - error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag, - (int *)0, (u_long **)0); - fp->f_offset = kuio.uio_offset; - if (error == 0) { - readcnt = uap->count - kuio.uio_resid; - edp = (struct dirent *)&dirbuf[readcnt]; - for (dp = (struct dirent *)dirbuf; dp < edp; ) { -# if (BYTE_ORDER == LITTLE_ENDIAN) - /* - * The expected low byte of - * dp->d_namlen is our dp->d_type. - * The high MBZ byte of dp->d_namlen - * is our dp->d_namlen. - */ - dp->d_type = dp->d_namlen; - dp->d_namlen = 0; -# else - /* - * The dp->d_type is the high byte - * of the expected dp->d_namlen, - * so must be zero'ed. - */ - dp->d_type = 0; -# endif - if (dp->d_reclen > 0) { - dp = (struct dirent *) - ((char *)dp + dp->d_reclen); - } else { - error = EIO; - break; - } - } - if (dp >= edp) - error = uiomove(dirbuf, readcnt, &auio); - } - FREE(dirbuf, M_TEMP); - } - VOP_UNLOCK(vp, 0, p); - if (error) - return (error); + return(mkdir1(&context, uap->path, &va)); +} -#if UNION +/* + * Remove a directory file. + */ +/* ARGSUSED */ +int +rmdir(struct proc *p, struct rmdir_args *uap, __unused register_t *retval) { - extern int (**union_vnodeop_p)(void *); - extern struct vnode *union_dircache __P((struct vnode*, struct proc*)); - - if ((uap->count == auio.uio_resid) && - (vp->v_op == union_vnodeop_p)) { - struct vnode *lvp; - - lvp = union_dircache(vp, p); - if (lvp != NULLVP) { - struct vattr va; - - /* - * If the directory is opaque, - * then don't show lower entries - */ - error = VOP_GETATTR(vp, &va, fp->f_cred, p); - if (va.va_flags & OPAQUE) { - vput(lvp); - lvp = NULL; - } - } + vnode_t vp, dvp; + int error; + struct nameidata nd; + struct vfs_context context; + + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + + NDINIT(&nd, DELETE, LOCKPARENT | AUDITVNPATH1, + UIO_USERSPACE, uap->path, &context); + error = namei(&nd); + if (error) + return (error); + dvp = nd.ni_dvp; + vp = nd.ni_vp; + + if (vp->v_type != VDIR) { + /* + * rmdir only deals with directories + */ + error = ENOTDIR; + } else if (dvp == vp) { + /* + * No rmdir "." please. + */ + error = EINVAL; + } else if (vp->v_flag & VROOT) { + /* + * The root of a mounted filesystem cannot be deleted. + */ + error = EBUSY; + } else { + error = vnode_authorize(vp, nd.ni_dvp, KAUTH_VNODE_DELETE, &context); + } + if (!error) { + char *path = NULL; + int len; + fse_info finfo; - if (lvp != NULLVP) { - error = VOP_OPEN(lvp, FREAD, fp->f_cred, p); - if (error) { - vput(lvp); - return (error); - } - VOP_UNLOCK(lvp, 0, p); - fp->f_data = (caddr_t) lvp; - fp->f_offset = 0; - error = VOP_CLOSE(vp, FREAD, fp->f_cred, p); - vrele(vp); - if (error) - return (error); - vp = lvp; - goto unionread; + if (need_fsevent(FSE_DELETE, dvp)) { + path = get_pathbuff(); + len = MAXPATHLEN; + vn_getpath(vp, path, &len); + get_fse_info(vp, &finfo, &context); } - } -} -#endif /* UNION */ + error = VNOP_RMDIR(dvp, vp, &nd.ni_cnd, &context); - if ((uap->count == auio.uio_resid) && - (vp->v_flag & VROOT) && - (vp->v_mount->mnt_flag & MNT_UNION)) { - struct vnode *tvp = vp; - vp = vp->v_mount->mnt_vnodecovered; - VREF(vp); - fp->f_data = (caddr_t) vp; - fp->f_offset = 0; - vrele(tvp); - goto unionread; + if (!error && path != NULL) { + add_fsevent(FSE_DELETE, &context, + FSE_ARG_STRING, len, path, + FSE_ARG_FINFO, &finfo, + FSE_ARG_DONE); + } + if (path != NULL) + release_pathbuff(path); } - error = copyout((caddr_t)&loff, (caddr_t)uap->basep, - sizeof(long)); - *retval = uap->count - auio.uio_resid; + /* + * nameidone has to happen before we vnode_put(dvp) + * since it may need to release the fs_nodelock on the dvp + */ + nameidone(&nd); + + vnode_put(dvp); + vnode_put(vp); + return (error); } -#endif /* COMPAT_43 */ + /* * Read a block of directory entries in a file system independent format. */ -struct getdirentries_args { - int fd; - char *buf; - u_int count; - long *basep; -}; int getdirentries(p, uap, retval) struct proc *p; register struct getdirentries_args *uap; register_t *retval; { - register struct vnode *vp; - struct file *fp; - struct uio auio; - struct iovec aiov; + struct vnode *vp; + struct vfs_context context; + struct fileproc *fp; + uio_t auio; + int spacetype = proc_is64bit(p) ? UIO_USERSPACE64 : UIO_USERSPACE32; long loff; int error, eofflag; + int fd = uap->fd; + char uio_buf[ UIO_SIZEOF(1) ]; AUDIT_ARG(fd, uap->fd); - error = getvnode(p, uap->fd, &fp); + error = fp_getfvp(p, fd, &fp, &vp); if (error) return (error); - AUDIT_ARG(vnpath, (struct vnode *)fp->f_data, ARG_VNODE1); + if ((fp->f_fglob->fg_flag & FREAD) == 0) { + AUDIT_ARG(vnpath_withref, vp, ARG_VNODE1); + error = EBADF; + goto out; + } + if ( (error = vnode_getwithref(vp)) ) { + goto out; + } + + AUDIT_ARG(vnpath, vp, ARG_VNODE1); - if ((fp->f_flag & FREAD) == 0) - return (EBADF); - vp = (struct vnode *)fp->f_data; unionread: - if (vp->v_type != VDIR) - return (EINVAL); - aiov.iov_base = uap->buf; - aiov.iov_len = uap->count; - auio.uio_iov = &aiov; - auio.uio_iovcnt = 1; - auio.uio_rw = UIO_READ; - auio.uio_segflg = UIO_USERSPACE; - auio.uio_procp = p; - auio.uio_resid = uap->count; - vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); - loff = auio.uio_offset = fp->f_offset; - error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, - (int *)0, (u_long **)0); - fp->f_offset = auio.uio_offset; - VOP_UNLOCK(vp, 0, p); - if (error) - return (error); + if (vp->v_type != VDIR) { + (void)vnode_put(vp); + error = EINVAL; + goto out; + } + context.vc_proc = p; + context.vc_ucred = fp->f_fglob->fg_cred; + + loff = fp->f_fglob->fg_offset; + auio = uio_createwithbuffer(1, loff, spacetype, UIO_READ, + &uio_buf[0], sizeof(uio_buf)); + uio_addiov(auio, uap->buf, uap->count); + + error = VNOP_READDIR(vp, auio, 0, &eofflag, (int *)NULL, &context); + fp->f_fglob->fg_offset = uio_offset(auio); + if (error) { + (void)vnode_put(vp); + goto out; + } #if UNION { - extern int (**union_vnodeop_p)(void *); - extern struct vnode *union_dircache __P((struct vnode*, struct proc*)); - - if ((uap->count == auio.uio_resid) && + if ((uap->count == uio_resid(auio)) && (vp->v_op == union_vnodeop_p)) { struct vnode *lvp; lvp = union_dircache(vp, p); if (lvp != NULLVP) { - struct vattr va; - + struct vnode_attr va; /* * If the directory is opaque, * then don't show lower entries */ - error = VOP_GETATTR(vp, &va, fp->f_cred, p); + VATTR_INIT(&va); + VATTR_WANTED(&va, va_flags); + error = vnode_getattr(vp, &va, &context); if (va.va_flags & OPAQUE) { - vput(lvp); + vnode_put(lvp); lvp = NULL; } } if (lvp != NULLVP) { - error = VOP_OPEN(lvp, FREAD, fp->f_cred, p); + error = VNOP_OPEN(lvp, FREAD, &context); if (error) { - vput(lvp); - return (error); + vnode_put(lvp); + goto out; } - VOP_UNLOCK(lvp, 0, p); - fp->f_data = (caddr_t) lvp; - fp->f_offset = 0; - error = VOP_CLOSE(vp, FREAD, fp->f_cred, p); - vrele(vp); + vnode_ref(lvp); + fp->f_fglob->fg_data = (caddr_t) lvp; + fp->f_fglob->fg_offset = 0; + error = VNOP_CLOSE(vp, FREAD, &context); + vnode_rele(vp); + vnode_put(vp); if (error) - return (error); + goto out; vp = lvp; goto unionread; } @@ -3263,99 +4177,114 @@ unionread: } #endif /* UNION */ - if ((uap->count == auio.uio_resid) && + if (((user_ssize_t)uap->count == uio_resid(auio)) && (vp->v_flag & VROOT) && (vp->v_mount->mnt_flag & MNT_UNION)) { struct vnode *tvp = vp; vp = vp->v_mount->mnt_vnodecovered; - VREF(vp); - fp->f_data = (caddr_t) vp; - fp->f_offset = 0; - vrele(tvp); + vnode_getwithref(vp); + vnode_ref(vp); + fp->f_fglob->fg_data = (caddr_t) vp; + fp->f_fglob->fg_offset = 0; + vnode_rele(tvp); + vnode_put(tvp); goto unionread; } - error = copyout((caddr_t)&loff, (caddr_t)uap->basep, - sizeof(long)); - *retval = uap->count - auio.uio_resid; + vnode_put(vp); + error = copyout((caddr_t)&loff, uap->basep, sizeof(long)); + // LP64todo - fix this + *retval = uap->count - uio_resid(auio); +out: + file_drop(fd); return (error); } /* * Set the mode mask for creation of filesystem nodes. */ -struct umask_args { - int newmask; -}; -int -umask(p, uap, retval) - struct proc *p; - struct umask_args *uap; - register_t *retval; +#warning XXX implement xsecurity + +#define UMASK_NOXSECURITY (void *)1 /* leave existing xsecurity alone */ +static int +umask1(struct proc *p, int newmask, __unused kauth_filesec_t fsec, register_t *retval) { register struct filedesc *fdp; - AUDIT_ARG(mask, uap->newmask); + AUDIT_ARG(mask, newmask); fdp = p->p_fd; *retval = fdp->fd_cmask; - fdp->fd_cmask = uap->newmask & ALLPERMS; + fdp->fd_cmask = newmask & ALLPERMS; return (0); } + +int +umask_extended(struct proc *p, struct umask_extended_args *uap, register_t *retval) +{ + int ciferror; + kauth_filesec_t xsecdst; + + xsecdst = KAUTH_FILESEC_NONE; + if (uap->xsecurity != USER_ADDR_NULL) { + if ((ciferror = kauth_copyinfilesec(uap->xsecurity, &xsecdst)) != 0) + return ciferror; + } else { + xsecdst = KAUTH_FILESEC_NONE; + } + + ciferror = umask1(p, uap->newmask, xsecdst, retval); + + if (xsecdst != KAUTH_FILESEC_NONE) + kauth_filesec_free(xsecdst); + return ciferror; +} + +int +umask(struct proc *p, struct umask_args *uap, register_t *retval) +{ + return(umask1(p, uap->newmask, UMASK_NOXSECURITY, retval)); +} + /* * Void all references to file by ripping underlying filesystem * away from vnode. */ -struct revoke_args { - char *path; -}; /* ARGSUSED */ int -revoke(p, uap, retval) - struct proc *p; - register struct revoke_args *uap; - register_t *retval; +revoke(struct proc *p, register struct revoke_args *uap, __unused register_t *retval) { register struct vnode *vp; - struct vattr vattr; + struct vnode_attr va; + struct vfs_context context; int error; struct nameidata nd; - NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNPATH1, UIO_USERSPACE, uap->path, p); + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + + NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNPATH1, + UIO_USERSPACE, uap->path, &context); error = namei(&nd); if (error) return (error); vp = nd.ni_vp; - if (error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) + + nameidone(&nd); + + VATTR_INIT(&va); + VATTR_WANTED(&va, va_uid); + if ((error = vnode_getattr(vp, &va, &context))) goto out; - if (p->p_ucred->cr_uid != vattr.va_uid && - (error = suser(p->p_ucred, &p->p_acflag))) + if (kauth_cred_getuid(context.vc_ucred) != va.va_uid && + (error = suser(context.vc_ucred, &p->p_acflag))) goto out; if (vp->v_usecount > 1 || (vp->v_flag & VALIASED)) - VOP_REVOKE(vp, REVOKEALL); + VNOP_REVOKE(vp, REVOKEALL, &context); out: - vrele(vp); + vnode_put(vp); return (error); } -/* - * Convert a user file descriptor to a kernel file entry. - */ -int -getvnode(p, fd, fpp) - struct proc *p; - int fd; - struct file **fpp; -{ - struct file *fp; - int error; - - if (error = fdgetf(p, fd, &fp)) - return (error); - if (fp->f_type != DTYPE_VNODE) - return (EINVAL); - *fpp = fp; - return (0); -} /* * HFS/HFS PlUS SPECIFIC SYSTEM CALLS @@ -3372,105 +4301,48 @@ getvnode(p, fd, fpp) /* * Make a complex file. A complex file is one with multiple forks (data streams) */ -struct mkcomplex_args { - const char *path; /* pathname of the file to be created */ - mode_t mode; /* access mode for the newly created file */ - u_long type; /* format of the complex file */ -}; /* ARGSUSED */ int -mkcomplex(p,uap,retval) - struct proc *p; - register struct mkcomplex_args *uap; - register_t *retval; +mkcomplex(__unused struct proc *p, __unused struct mkcomplex_args *uap, __unused register_t *retval) { - struct vnode *vp; - struct vattr vattr; - int error; - struct nameidata nd; - - /* mkcomplex wants the directory vnode locked so do that here */ - - NDINIT(&nd, CREATE, FOLLOW | LOCKPARENT, UIO_USERSPACE, (char *)uap->path, p); - if (error = namei(&nd)) - return (error); - - /* Set the attributes as specified by the user */ - - VATTR_NULL(&vattr); - vattr.va_mode = (uap->mode & ACCESSPERMS); - error = VOP_MKCOMPLEX(nd.ni_dvp, &vp, &nd.ni_cnd, &vattr, uap->type); - - /* The mkcomplex call promises to release the parent vnode pointer - * even an an error case so don't do it here unless the operation - * is not supported. In that case, there isn't anyone to unlock the parent - * The vnode pointer to the file will also be released. - */ - - if (error) - { - if (error == EOPNOTSUPP) - vput(nd.ni_dvp); - return (error); - } - - return (0); - -} /* end of mkcomplex system call */ + return (ENOTSUP); +} /* * Extended stat call which returns volumeid and vnodeid as well as other info */ -struct statv_args { - const char *path; /* pathname of the target file */ - struct vstat *vsb; /* vstat structure for returned info */ -}; /* ARGSUSED */ int -statv(p,uap,retval) - struct proc *p; - register struct statv_args *uap; - register_t *retval; - +statv(__unused struct proc *p, + __unused struct statv_args *uap, + __unused register_t *retval) { - return (EOPNOTSUPP); /* We'll just return an error for now */ + return (ENOTSUP); /* We'll just return an error for now */ } /* end of statv system call */ /* * Extended lstat call which returns volumeid and vnodeid as well as other info */ -struct lstatv_args { - const char *path; /* pathname of the target file */ - struct vstat *vsb; /* vstat structure for returned info */ -}; /* ARGSUSED */ int -lstatv(p,uap,retval) - struct proc *p; - register struct lstatv_args *uap; - register_t *retval; - +lstatv(__unused struct proc *p, + __unused struct lstatv_args *uap, + __unused register_t *retval) { - return (EOPNOTSUPP); /* We'll just return an error for now */ + return (ENOTSUP); /* We'll just return an error for now */ } /* end of lstatv system call */ /* * Extended fstat call which returns volumeid and vnodeid as well as other info */ -struct fstatv_args { - int fd; /* file descriptor of the target file */ - struct vstat *vsb; /* vstat structure for returned info */ -}; /* ARGSUSED */ int -fstatv(p,uap,retval) - struct proc *p; - register struct fstatv_args *uap; - register_t *retval; - +fstatv(__unused struct proc *p, + __unused struct fstatv_args *uap, + __unused register_t *retval) { - return (EOPNOTSUPP); /* We'll just return an error for now */ + return (ENOTSUP); /* We'll just return an error for now */ } /* end of fstatv system call */ @@ -3480,161 +4352,6 @@ fstatv(p,uap,retval) #endif /* __APPLE_API_OBSOLETE */ - -/* -* Obtain attribute information about a file system object -*/ - -struct getattrlist_args { - const char *path; /* pathname of the target object */ - struct attrlist * alist; /* Attributes desired by the user */ - void * attributeBuffer; /* buffer to hold returned attributes */ - size_t bufferSize; /* size of the return buffer */ - unsigned long options; /* options (follow/don't follow) */ -}; -/* ARGSUSED */ -int -getattrlist (p,uap,retval) - struct proc *p; - register struct getattrlist_args *uap; - register_t *retval; - -{ - int error; - struct nameidata nd; - struct iovec aiov; - struct uio auio; - struct attrlist attributelist; - u_long nameiflags; - - /* Get the attributes desire and do our parameter checking */ - - if (error = copyin((caddr_t)uap->alist, (caddr_t) &attributelist, - sizeof (attributelist))) - { - return(error); - } - - if (attributelist.bitmapcount != ATTR_BIT_MAP_COUNT -#if 0 - || attributelist.commonattr & ~ATTR_CMN_VALIDMASK || - attributelist.volattr & ~ATTR_VOL_VALIDMASK || - attributelist.dirattr & ~ATTR_DIR_VALIDMASK || - attributelist.fileattr & ~ATTR_FILE_VALIDMASK || - attributelist.forkattr & ~ATTR_FORK_VALIDMASK -#endif - ) - { - return (EINVAL); - } - - /* Get the vnode for the file we are getting info on. */ - nameiflags = LOCKLEAF | SHAREDLEAF; - if ((uap->options & FSOPT_NOFOLLOW) == 0) nameiflags |= FOLLOW; - NDINIT(&nd, LOOKUP, nameiflags | AUDITVNPATH1, UIO_USERSPACE, - (char *)uap->path, p); - - error = namei(&nd); - if (error) - return (error); - - /* Set up the UIO structure for use by the vfs routine */ - - aiov.iov_base = uap->attributeBuffer; - aiov.iov_len = uap->bufferSize; - auio.uio_iov = &aiov; - auio.uio_iovcnt = 1; - auio.uio_offset = 0; - auio.uio_rw = UIO_READ; - auio.uio_segflg = UIO_USERSPACE; - auio.uio_procp = p; - auio.uio_resid = uap->bufferSize; - - - error = VOP_GETATTRLIST(nd.ni_vp, &attributelist, &auio, p->p_ucred, p); - - /* Unlock and release the vnode which will have been locked by namei */ - - vput(nd.ni_vp); - - /* return the effort if we got one, otherwise return success */ - - if (error) - { - return (error); - } - - return(0); - -} /* end of getattrlist system call */ - - - -/* - * Set attribute information about a file system object - */ - -struct setattrlist_args { - const char *path; /* pathname of the target object */ - struct attrlist * alist; /* Attributes being set by the user */ - void * attributeBuffer; /* buffer with attribute values to be set */ - size_t bufferSize; /* size of the return buffer */ - unsigned long options; /* options (follow/don't follow) */ -}; -/* ARGSUSED */ -int -setattrlist (p,uap,retval) - struct proc *p; - register struct setattrlist_args *uap; - register_t *retval; - -{ - int error; - struct nameidata nd; - struct iovec aiov; - struct uio auio; - struct attrlist alist; - u_long nameiflags; - - /* Get the attributes desired and do our parameter checking */ - - if ((error = copyin((caddr_t)uap->alist, (caddr_t) &alist, - sizeof (alist)))) { - return (error); - } - - if (alist.bitmapcount != ATTR_BIT_MAP_COUNT) - return (EINVAL); - - /* Get the vnode for the file whose attributes are being set. */ - nameiflags = LOCKLEAF; - if ((uap->options & FSOPT_NOFOLLOW) == 0) nameiflags |= FOLLOW; - NDINIT(&nd, LOOKUP, nameiflags | AUDITVNPATH1, UIO_USERSPACE, - (char *)uap->path, p); - error = namei(&nd); - if (error) - return (error); - - /* Set up the UIO structure for use by the vfs routine */ - aiov.iov_base = uap->attributeBuffer; - aiov.iov_len = uap->bufferSize; - auio.uio_iov = &aiov; - auio.uio_iovcnt = 1; - auio.uio_offset = 0; - auio.uio_rw = UIO_WRITE; - auio.uio_segflg = UIO_USERSPACE; - auio.uio_procp = p; - auio.uio_resid = uap->bufferSize; - - error = VOP_SETATTRLIST(nd.ni_vp, &alist, &auio, p->p_ucred, p); - - vput(nd.ni_vp); - - return (error); - -} /* end of setattrlist system call */ - - /* * Obtain attribute information on objects in a directory while enumerating * the directory. This call does not yet support union mounted directories. @@ -3642,83 +4359,96 @@ setattrlist (p,uap,retval) * 1.union mounted directories. */ -struct getdirentriesattr_args { - int fd; /* file descriptor */ - struct attrlist *alist; /* bit map of requested attributes */ - void *buffer; /* buffer to hold returned attribute info */ - size_t buffersize; /* size of the return buffer */ - u_long *count; /* the count of entries requested/returned */ - u_long *basep; /* the offset of where we are leaving off in buffer */ - u_long *newstate; /* a flag to inform of changes in directory */ - u_long options; /* maybe unused for now */ -}; /* ARGSUSED */ int -getdirentriesattr (p,uap,retval) - struct proc *p; - register struct getdirentriesattr_args *uap; - register_t *retval; - +getdirentriesattr (struct proc *p, struct getdirentriesattr_args *uap, register_t *retval) { - register struct vnode *vp; - struct file *fp; - struct uio auio; - struct iovec aiov; - u_long actualcount; - u_long newstate; - int error, eofflag; - long loff; - struct attrlist attributelist; + struct vnode *vp; + struct fileproc *fp; + uio_t auio = NULL; + int spacetype = proc_is64bit(p) ? UIO_USERSPACE64 : UIO_USERSPACE32; + uint64_t actualcount; + u_long tmpcount; + u_long newstate; + int error, eofflag; + u_long loff; + struct attrlist attributelist; + struct vfs_context context; + int fd = uap->fd; + char uio_buf[ UIO_SIZEOF(1) ]; + kauth_action_t action; + + AUDIT_ARG(fd, fd); + + /* Get the attributes into kernel space */ + if ((error = copyin(uap->alist, (caddr_t) &attributelist, sizeof (attributelist)))) + return(error); + actualcount = fuulong(uap->count); + if (actualcount == -1ULL) + return(-1); - AUDIT_ARG(fd, uap->fd); + if ( (error = fp_getfvp(p, fd, &fp, &vp)) ) + return (error); + + if ((fp->f_fglob->fg_flag & FREAD) == 0) { + AUDIT_ARG(vnpath_withref, vp, ARG_VNODE1); + error = EBADF; + goto out; + } + if ( (error = vnode_getwithref(vp)) ) + goto out; - /* Get the attributes into kernel space */ - if (error = copyin((caddr_t)uap->alist, (caddr_t) &attributelist, sizeof (attributelist))) - return(error); - if (error = copyin((caddr_t)uap->count, (caddr_t) &actualcount, sizeof (u_long))) - return(error); + AUDIT_ARG(vnpath, vp, ARG_VNODE1); - if (error = getvnode(p, uap->fd, &fp)) - return (error); + if (vp->v_type != VDIR) { + (void)vnode_put(vp); + error = EINVAL; + goto out; + } - AUDIT_ARG(vnpath, (struct vnode *)fp->f_data, ARG_VNODE1); + /* set up the uio structure which will contain the users return buffer */ + loff = fp->f_fglob->fg_offset; + auio = uio_createwithbuffer(1, loff, spacetype, UIO_READ, + &uio_buf[0], sizeof(uio_buf)); + uio_addiov(auio, uap->buffer, uap->buffersize); + + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + tmpcount = (u_long) actualcount; - if ((fp->f_flag & FREAD) == 0) - return(EBADF); - vp = (struct vnode *)fp->f_data; + /* + * If the only item requested is file names, we can let that past with + * just LIST_DIRECTORY. If they want any other attributes, that means + * they need SEARCH as well. + */ + action = KAUTH_VNODE_LIST_DIRECTORY; + if ((attributelist.commonattr & ~ATTR_CMN_NAME) || + attributelist.fileattr || attributelist.dirattr) + action |= KAUTH_VNODE_SEARCH; + + if ((error = vnode_authorize(vp, NULL, action, &context)) == 0) + error = VNOP_READDIRATTR(vp, &attributelist, auio, + tmpcount, uap->options, &newstate, &eofflag, + &tmpcount, &context); + (void)vnode_put(vp); + actualcount = tmpcount; - if (vp->v_type != VDIR) - return(EINVAL); + if (error) + goto out; + fp->f_fglob->fg_offset = uio_offset(auio); /* should be multiple of dirent, not variable */ - /* set up the uio structure which will contain the users return buffer */ - aiov.iov_base = uap->buffer; - aiov.iov_len = uap->buffersize; - auio.uio_iov = &aiov; - auio.uio_iovcnt = 1; - auio.uio_rw = UIO_READ; - auio.uio_segflg = UIO_USERSPACE; - auio.uio_procp = p; - auio.uio_resid = uap->buffersize; - - loff = auio.uio_offset = fp->f_offset; - vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); - error = VOP_READDIRATTR (vp, &attributelist, &auio, - actualcount, uap->options, &newstate, &eofflag, - &actualcount, ((u_long **)0), p->p_ucred); - - VOP_UNLOCK(vp, 0, p); - if (error) return (error); - fp->f_offset = auio.uio_offset; /* should be multiple of dirent, not variable */ - - if (error = copyout((caddr_t) &actualcount, (caddr_t) uap->count, sizeof(u_long))) - return (error); - if (error = copyout((caddr_t) &newstate, (caddr_t) uap->newstate, sizeof(u_long))) - return (error); - if (error = copyout((caddr_t)&loff, (caddr_t)uap->basep, sizeof(long))) - return (error); + if ((error = suulong(uap->count, actualcount)) != 0) + goto out; + if ((error = suulong(uap->newstate, (uint64_t)newstate)) != 0) + goto out; + if ((error = suulong(uap->basep, (uint64_t)loff)) != 0) + goto out; *retval = eofflag; /* similar to getdirentries */ - return (0); /* return error earlier, an retval of 0 or 1 now */ + error = 0; + out: + file_drop(fd); + return (error); /* return error earlier, an retval of 0 or 1 now */ } /* end of getdirentryattr system call */ @@ -3726,93 +4456,132 @@ getdirentriesattr (p,uap,retval) * Exchange data between two files */ -struct exchangedata_args { - const char *path1; /* pathname of the first swapee */ - const char *path2; /* pathname of the second swapee */ - unsigned long options; /* options */ -}; /* ARGSUSED */ int -exchangedata (p,uap,retval) - struct proc *p; - register struct exchangedata_args *uap; - register_t *retval; - +exchangedata (struct proc *p, register struct exchangedata_args *uap, __unused register_t *retval) { struct nameidata fnd, snd; + struct vfs_context context; struct vnode *fvp, *svp; - int error; + int error; u_long nameiflags; + char *fpath = NULL; + char *spath = NULL; + int flen, slen; + fse_info f_finfo, s_finfo; + + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); nameiflags = 0; if ((uap->options & FSOPT_NOFOLLOW) == 0) nameiflags |= FOLLOW; - /* Global lock, to prevent race condition, only one exchange at a time */ - lockmgr(&exchangelock, LK_EXCLUSIVE , (struct slock *)0, p); - - NDINIT(&fnd, LOOKUP, nameiflags | AUDITVNPATH1, UIO_USERSPACE, - (char *) uap->path1, p); + NDINIT(&fnd, LOOKUP, nameiflags | AUDITVNPATH1, + UIO_USERSPACE, uap->path1, &context); - error = namei(&fnd); - if (error) - goto out2; + error = namei(&fnd); + if (error) + goto out2; - fvp = fnd.ni_vp; + nameidone(&fnd); + fvp = fnd.ni_vp; - NDINIT(&snd, LOOKUP, nameiflags | AUDITVNPATH2, UIO_USERSPACE, - (char *)uap->path2, p); + NDINIT(&snd, LOOKUP, nameiflags | AUDITVNPATH2, + UIO_USERSPACE, uap->path2, &context); - error = namei(&snd); - if (error) { - vrele(fvp); + error = namei(&snd); + if (error) { + vnode_put(fvp); goto out2; - } - + } + nameidone(&snd); svp = snd.ni_vp; - /* if the files are the same, return an inval error */ + /* + * if the files are the same, return an inval error + */ if (svp == fvp) { - vrele(fvp); - vrele(svp); - error = EINVAL; - goto out2; - } - - vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY, p); - vn_lock(svp, LK_EXCLUSIVE | LK_RETRY, p); - - error = VOP_ACCESS(fvp, VWRITE, p->p_ucred, p); - if (error) goto out; + error = EINVAL; + goto out; + } - error = VOP_ACCESS(svp, VWRITE, p->p_ucred, p); - if (error) goto out; + /* + * if the files are on different volumes, return an error + */ + if (svp->v_mount != fvp->v_mount) { + error = EXDEV; + goto out; + } + if (((error = vnode_authorize(fvp, NULL, KAUTH_VNODE_READ_DATA | KAUTH_VNODE_WRITE_DATA, &context)) != 0) || + ((error = vnode_authorize(svp, NULL, KAUTH_VNODE_READ_DATA | KAUTH_VNODE_WRITE_DATA, &context)) != 0)) + goto out; + if (need_fsevent(FSE_EXCHANGE, fvp) || kauth_authorize_fileop_has_listeners()) { + fpath = get_pathbuff(); + spath = get_pathbuff(); + flen = MAXPATHLEN; + slen = MAXPATHLEN; + if (vn_getpath(fvp, fpath, &flen) != 0 || fpath[0] == '\0') { + printf("exchange: vn_getpath(fvp=0x%x) failed <<%s>>\n", + fvp, fpath); + } + if (vn_getpath(svp, spath, &slen) != 0 || spath[0] == '\0') { + printf("exchange: vn_getpath(svp=0x%x) failed <<%s>>\n", + svp, spath); + } + get_fse_info(fvp, &f_finfo, &context); + get_fse_info(svp, &s_finfo, &context); + } /* Ok, make the call */ - error = VOP_EXCHANGE (fvp, svp, p->p_ucred, p); - - if (error == 0 && VPARENT(fvp) != VPARENT(svp)) { - struct vnode *tmp; + error = VNOP_EXCHANGE(fvp, svp, 0, &context); - tmp = VPARENT(fvp); - VPARENT(fvp) = VPARENT(svp); - VPARENT(svp) = tmp; + if (error == 0) { + char *tmpname; + + if (fpath != NULL && spath != NULL) { + /* call out to allow 3rd party notification of exchangedata. + * Ignore result of kauth_authorize_fileop call. + */ + kauth_authorize_fileop(vfs_context_ucred(&context), KAUTH_FILEOP_EXCHANGE, + (uintptr_t)fpath, (uintptr_t)spath); + } + name_cache_lock(); + + tmpname = fvp->v_name; + fvp->v_name = svp->v_name; + svp->v_name = tmpname; + + if (fvp->v_parent != svp->v_parent) { + struct vnode *tmp; + + tmp = fvp->v_parent; + fvp->v_parent = svp->v_parent; + svp->v_parent = tmp; + } + name_cache_unlock(); + + if (fpath != NULL && spath != NULL) { + add_fsevent(FSE_EXCHANGE, &context, + FSE_ARG_STRING, flen, fpath, + FSE_ARG_FINFO, &f_finfo, + FSE_ARG_STRING, slen, spath, + FSE_ARG_FINFO, &s_finfo, + FSE_ARG_DONE); + } } + if (spath != NULL) + release_pathbuff(spath); + if (fpath != NULL) + release_pathbuff(fpath); out: - vput (svp); - vput (fvp); - + vnode_put(svp); + vnode_put(fvp); out2: - lockmgr(&exchangelock, LK_RELEASE, (struct slock *)0, p); - - if (error) { return (error); - } - - return (0); +} -} /* end of exchangedata system call */ #ifdef __APPLE_API_OBSOLETE @@ -3822,31 +4591,22 @@ out2: /* * Check users access to a file -*/ - -struct checkuseraccess_args { - const char *path; /* pathname of the target file */ - uid_t userid; /* user for whom we are checking access */ - gid_t *groups; /* Group that we are checking for */ - int ngroups; /* Number of groups being checked */ - int accessrequired; /* needed access to the file */ - unsigned long options; /* options */ -}; +*/ /* ARGSUSED */ +#warning "checkuseraccess copies a cred in from user space but" +#warning "user space has no way of knowing what one looks like" +#warning "this code should use the access_extended spoof-as functionality" int -checkuseraccess (p,uap,retval) - struct proc *p; - register struct checkuseraccess_args *uap; - register_t *retval; - +checkuseraccess (struct proc *p, register struct checkuseraccess_args *uap, __unused register_t *retval) { register struct vnode *vp; int error; struct nameidata nd; - struct ucred cred; + struct ucred cred; /* XXX ILLEGAL */ int flags; /*what will actually get passed to access*/ u_long nameiflags; + struct vfs_context context; /* Make sure that the number of groups is correct before we do anything */ @@ -3855,7 +4615,7 @@ checkuseraccess (p,uap,retval) /* Verify that the caller is root */ - if (error = suser(p->p_ucred, &p->p_acflag)) + if ((error = suser(kauth_cred_get(), &p->p_acflag))) return(error); /* Fill in the credential structure */ @@ -3863,17 +4623,21 @@ checkuseraccess (p,uap,retval) cred.cr_ref = 0; cred.cr_uid = uap->userid; cred.cr_ngroups = uap->ngroups; - if (error = copyin((caddr_t) uap->groups, (caddr_t) &(cred.cr_groups), (sizeof(gid_t))*uap->ngroups)) + if ((error = copyin(CAST_USER_ADDR_T(uap->groups), (caddr_t) &(cred.cr_groups), (sizeof(gid_t))*uap->ngroups))) return (error); - /* Get our hands on the file */ + context.vc_proc = p; + context.vc_ucred = &cred; - nameiflags = LOCKLEAF; + /* Get our hands on the file */ + nameiflags = 0; if ((uap->options & FSOPT_NOFOLLOW) == 0) nameiflags |= FOLLOW; - NDINIT(&nd, LOOKUP, nameiflags | AUDITVNPATH1, UIO_USERSPACE, (char *)uap->path, p); + NDINIT(&nd, LOOKUP, nameiflags | AUDITVNPATH1, + UIO_USERSPACE, CAST_USER_ADDR_T(uap->path), &context); - if (error = namei(&nd)) + if ((error = namei(&nd))) return (error); + nameidone(&nd); vp = nd.ni_vp; /* Flags == 0 means only check for existence. */ @@ -3882,15 +4646,15 @@ checkuseraccess (p,uap,retval) if (uap->accessrequired) { if (uap->accessrequired & R_OK) - flags |= VREAD; + flags |= KAUTH_VNODE_READ_DATA; if (uap->accessrequired & W_OK) - flags |= VWRITE; + flags |= KAUTH_VNODE_WRITE_DATA; if (uap->accessrequired & X_OK) - flags |= VEXEC; + flags |= KAUTH_VNODE_EXECUTE; } - error = VOP_ACCESS(vp, flags, &cred, p); + error = vnode_authorize(vp, NULL, flags, &context); - vput(vp); + vnode_put(vp); if (error) return (error); @@ -3907,41 +4671,50 @@ checkuseraccess (p,uap,retval) -struct searchfs_args { - const char *path; - struct fssearchblock *searchblock; - u_long *nummatches; - u_long scriptcode; - u_long options; - struct searchstate *state; - }; /* ARGSUSED */ int -searchfs (p,uap,retval) - struct proc *p; - register struct searchfs_args *uap; - register_t *retval; - +searchfs (struct proc *p, register struct searchfs_args *uap, __unused register_t *retval) { register struct vnode *vp; int error=0; int fserror = 0; struct nameidata nd; - struct fssearchblock searchblock; + struct user_fssearchblock searchblock; struct searchstate *state; struct attrlist *returnattrs; void *searchparams1,*searchparams2; - struct iovec aiov; - struct uio auio; + uio_t auio = NULL; + int spacetype = proc_is64bit(p) ? UIO_USERSPACE64 : UIO_USERSPACE32; u_long nummatches; int mallocsize; u_long nameiflags; - + struct vfs_context context; + char uio_buf[ UIO_SIZEOF(1) ]; - /* Start by copying in fsearchblock paramater list */ + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); - if (error = copyin((caddr_t) uap->searchblock, (caddr_t) &searchblock,sizeof(struct fssearchblock))) + /* Start by copying in fsearchblock paramater list */ + if (IS_64BIT_PROCESS(p)) { + error = copyin(uap->searchblock, (caddr_t) &searchblock, sizeof(searchblock)); + } + else { + struct fssearchblock tmp_searchblock; + error = copyin(uap->searchblock, (caddr_t) &tmp_searchblock, sizeof(tmp_searchblock)); + // munge into 64-bit version + searchblock.returnattrs = CAST_USER_ADDR_T(tmp_searchblock.returnattrs); + searchblock.returnbuffer = CAST_USER_ADDR_T(tmp_searchblock.returnbuffer); + searchblock.returnbuffersize = tmp_searchblock.returnbuffersize; + searchblock.maxmatches = tmp_searchblock.maxmatches; + searchblock.timelimit = tmp_searchblock.timelimit; + searchblock.searchparams1 = CAST_USER_ADDR_T(tmp_searchblock.searchparams1); + searchblock.sizeofsearchparams1 = tmp_searchblock.sizeofsearchparams1; + searchblock.searchparams2 = CAST_USER_ADDR_T(tmp_searchblock.searchparams2); + searchblock.sizeofsearchparams2 = tmp_searchblock.sizeofsearchparams2; + searchblock.searchattrs = tmp_searchblock.searchattrs; + } + if (error) return(error); /* Do a sanity check on sizeofsearchparams1 and sizeofsearchparams2. @@ -3949,13 +4722,13 @@ searchfs (p,uap,retval) if (searchblock.sizeofsearchparams1 > SEARCHFS_MAX_SEARCHPARMS || searchblock.sizeofsearchparams2 > SEARCHFS_MAX_SEARCHPARMS) return(EINVAL); - + /* Now malloc a big bunch of space to hold the search parameters, the attrlists and the search state. */ /* It all has to do into local memory and it's not that big so we might as well put it all together. */ /* Searchparams1 shall be first so we might as well use that to hold the base address of the allocated*/ /* block. */ - mallocsize = searchblock.sizeofsearchparams1+searchblock.sizeofsearchparams2 + + mallocsize = searchblock.sizeofsearchparams1 + searchblock.sizeofsearchparams2 + sizeof(struct attrlist) + sizeof(struct searchstate); MALLOC(searchparams1, void *, mallocsize, M_TEMP, M_WAITOK); @@ -3968,38 +4741,34 @@ searchfs (p,uap,retval) /* Now copy in the stuff given our local variables. */ - if (error = copyin((caddr_t) searchblock.searchparams1, searchparams1,searchblock.sizeofsearchparams1)) + if ((error = copyin(searchblock.searchparams1, searchparams1, searchblock.sizeofsearchparams1))) goto freeandexit; - if (error = copyin((caddr_t) searchblock.searchparams2, searchparams2,searchblock.sizeofsearchparams2)) + if ((error = copyin(searchblock.searchparams2, searchparams2, searchblock.sizeofsearchparams2))) goto freeandexit; - if (error = copyin((caddr_t) searchblock.returnattrs, (caddr_t) returnattrs, sizeof(struct attrlist))) + if ((error = copyin(searchblock.returnattrs, (caddr_t) returnattrs, sizeof(struct attrlist)))) goto freeandexit; - if (error = copyin((caddr_t) uap->state, (caddr_t) state, sizeof(struct searchstate))) + if ((error = copyin(uap->state, (caddr_t) state, sizeof(struct searchstate)))) goto freeandexit; /* set up the uio structure which will contain the users return buffer */ - aiov.iov_base = searchblock.returnbuffer; - aiov.iov_len = searchblock.returnbuffersize; - auio.uio_iov = &aiov; - auio.uio_iovcnt = 1; - auio.uio_rw = UIO_READ; - auio.uio_segflg = UIO_USERSPACE; - auio.uio_procp = p; - auio.uio_resid = searchblock.returnbuffersize; + auio = uio_createwithbuffer(1, 0, spacetype, UIO_READ, + &uio_buf[0], sizeof(uio_buf)); + uio_addiov(auio, searchblock.returnbuffer, searchblock.returnbuffersize); - nameiflags = LOCKLEAF; + nameiflags = 0; if ((uap->options & FSOPT_NOFOLLOW) == 0) nameiflags |= FOLLOW; - NDINIT(&nd, LOOKUP, nameiflags | AUDITVNPATH1, UIO_USERSPACE, - (char *)uap->path, p); + NDINIT(&nd, LOOKUP, nameiflags | AUDITVNPATH1, + UIO_USERSPACE, uap->path, &context); error = namei(&nd); if (error) goto freeandexit; + nameidone(&nd); vp = nd.ni_vp; @@ -4020,7 +4789,7 @@ searchfs (p,uap,retval) from copying out any results... */ - fserror = VOP_SEARCHFS(vp, + fserror = VNOP_SEARCHFS(vp, searchparams1, searchparams2, &searchblock.searchattrs, @@ -4030,20 +4799,21 @@ searchfs (p,uap,retval) &nummatches, uap->scriptcode, uap->options, - &auio, - state); + auio, + state, + &context); saveandexit: - vput(vp); + vnode_put(vp); /* Now copy out the stuff that needs copying out. That means the number of matches, the search state. Everything was already put into he return buffer by the vop call. */ - if (error = copyout((caddr_t) state, (caddr_t) uap->state, sizeof(struct searchstate))) + if ((error = copyout((caddr_t) state, uap->state, sizeof(struct searchstate))) != 0) goto freeandexit; - if (error = copyout((caddr_t) &nummatches, (caddr_t) uap->nummatches, sizeof(u_long))) + if ((error = suulong(uap->nummatches, (uint64_t)nummatches)) != 0) goto freeandexit; error = fserror; @@ -4061,21 +4831,12 @@ freeandexit: /* * Make a filesystem-specific control call: */ -struct fsctl_args { - const char *path; /* pathname of the target object */ - u_long cmd; /* cmd (also encodes size/direction of arguments a la ioctl) */ - caddr_t data; /* pointer to argument buffer */ - u_long options; /* options for fsctl processing */ -}; /* ARGSUSED */ int -fsctl (p,uap,retval) - struct proc *p; - struct fsctl_args *uap; - register_t *retval; - +fsctl (struct proc *p, struct fsctl_args *uap, __unused register_t *retval) { int error; + boolean_t is64bit; struct nameidata nd; u_long nameiflags; u_long cmd = uap->cmd; @@ -4083,24 +4844,35 @@ fsctl (p,uap,retval) #define STK_PARAMS 128 char stkbuf[STK_PARAMS]; caddr_t data, memp; + struct vfs_context context; + + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); size = IOCPARM_LEN(cmd); if (size > IOCPARM_MAX) return (EINVAL); + is64bit = proc_is64bit(p); + memp = NULL; if (size > sizeof (stkbuf)) { if ((memp = (caddr_t)kalloc(size)) == 0) return ENOMEM; data = memp; } else { - data = stkbuf; + data = &stkbuf[0]; }; if (cmd & IOC_IN) { if (size) { - error = copyin(uap->data, data, (u_int)size); + error = copyin(uap->data, data, size); if (error) goto FSCtl_Exit; } else { - *(caddr_t *)data = uap->data; + if (is64bit) { + *(user_addr_t *)data = uap->data; + } + else { + *(uint32_t *)data = (uint32_t)uap->data; + } }; } else if ((cmd & IOC_OUT) && size) { /* @@ -4108,25 +4880,33 @@ fsctl (p,uap,retval) * gets back something deterministic. */ bzero(data, size); - } else if (cmd & IOC_VOID) - *(caddr_t *)data = uap->data; + } else if (cmd & IOC_VOID) { + if (is64bit) { + *(user_addr_t *)data = uap->data; + } + else { + *(uint32_t *)data = (uint32_t)uap->data; + } + } /* Get the vnode for the file we are getting info on: */ - nameiflags = LOCKLEAF; + nameiflags = 0; if ((uap->options & FSOPT_NOFOLLOW) == 0) nameiflags |= FOLLOW; - NDINIT(&nd, LOOKUP, nameiflags, UIO_USERSPACE, (char *)uap->path, p); - if (error = namei(&nd)) goto FSCtl_Exit; - + NDINIT(&nd, LOOKUP, nameiflags, UIO_USERSPACE, uap->path, &context); + if ((error = namei(&nd))) goto FSCtl_Exit; + /* Invoke the filesystem-specific code */ - error = VOP_IOCTL(nd.ni_vp, IOCBASECMD(cmd), data, uap->options, p->p_ucred, p); + error = VNOP_IOCTL(nd.ni_vp, IOCBASECMD(cmd), data, uap->options, &context); - vput(nd.ni_vp); + vnode_put(nd.ni_vp); + nameidone(&nd); /* * Copy any data to user, size was * already set and checked above. */ - if (error == 0 && (cmd & IOC_OUT) && size) error = copyout(data, uap->data, (u_int)size); + if (error == 0 && (cmd & IOC_OUT) && size) + error = copyout(data, uap->data, size); FSCtl_Exit: if (memp) kfree(memp, size); @@ -4141,337 +4921,552 @@ FSCtl_Exit: __private_extern__ int sync_internal(void) { - boolean_t funnel_state; int error; struct sync_args data; int retval[2]; - funnel_state = thread_funnel_set(kernel_flock, TRUE); - error = sync(current_proc(), &data, &retval); + error = sync(current_proc(), &data, &retval[0]); - thread_funnel_set(kernel_flock, funnel_state); return (error); } /* end of sync_internal call */ +/* + * Retrieve the data of an extended attribute. + */ +int +getxattr(struct proc *p, struct getxattr_args *uap, user_ssize_t *retval) +{ + struct vnode *vp; + struct nameidata nd; + char attrname[XATTR_MAXNAMELEN+1]; + struct vfs_context context; + uio_t auio = NULL; + int spacetype = IS_64BIT_PROCESS(p) ? UIO_USERSPACE64 : UIO_USERSPACE32; + size_t attrsize = 0; + size_t namelen; + u_long nameiflags; + int error; + char uio_buf[ UIO_SIZEOF(1) ]; -// XXXdbg fmod watching calls -#define NUM_CHANGE_NODES 256 -static int changed_init=0; -static volatile int fmod_watch_enabled = 0; -static pid_t fmod_watch_owner; -static simple_lock_data_t changed_nodes_lock; // guard access -static volatile struct vnode *changed_nodes[NUM_CHANGE_NODES]; -static volatile pid_t changed_nodes_pid[NUM_CHANGE_NODES]; -static volatile int changed_rd_index=0, changed_wr_index=0; -static volatile int notifier_sleeping=0; - + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); -void -notify_filemod_watchers(struct vnode *vp, struct proc *p) -{ - int ret; - - // only want notification on regular files. - if (fmod_watch_enabled == 0 || (vp->v_type != VREG && vp->v_type != VDIR)) { - return; - } + if (uap->options & XATTR_NOSECURITY) + return (EINVAL); - // grab a reference so it doesn't go away - if (vget(vp, 0, p) != 0) { - return; - } + nameiflags = (uap->options & XATTR_NOFOLLOW) ? 0 : FOLLOW; + NDINIT(&nd, LOOKUP, nameiflags, spacetype, uap->path, &context); + if ((error = namei(&nd))) { + return (error); + } + vp = nd.ni_vp; + nameidone(&nd); - retry: - simple_lock(&changed_nodes_lock); + if ((error = copyinstr(uap->attrname, attrname, sizeof(attrname), &namelen) != 0)) { + goto out; + } + if (xattr_protected(attrname)) { + error = EPERM; + goto out; + } + if (uap->value && uap->size > 0) { + auio = uio_createwithbuffer(1, uap->position, spacetype, UIO_READ, + &uio_buf[0], sizeof(uio_buf)); + uio_addiov(auio, uap->value, uap->size); + } - // If the table is full, block until it clears up - if (((changed_wr_index+1) % NUM_CHANGE_NODES) == changed_rd_index) { - simple_unlock(&changed_nodes_lock); + error = vn_getxattr(vp, attrname, auio, &attrsize, uap->options, &context); +out: + vnode_put(vp); - notifier_sleeping++; - // wait up to 10 seconds for the queue to drain - ret = tsleep((caddr_t)&changed_wr_index, PINOD, "changed_nodes_full", 10*hz); - if (ret != 0 || fmod_watch_enabled == 0) { - notifier_sleeping--; - printf("notify_filemod: err %d from tsleep/enabled %d. bailing out (vp 0x%x).\n", - ret, fmod_watch_enabled, vp); - vrele(vp); - return; + if (auio) { + *retval = uap->size - uio_resid(auio); + } else { + *retval = (user_ssize_t)attrsize; } - notifier_sleeping--; - goto retry; - } + return (error); +} - // insert our new guy - if (changed_nodes[changed_wr_index] != NULL) { - panic("notify_fmod_watchers: index %d is 0x%x, not null!\n", - changed_wr_index, changed_nodes[changed_wr_index]); - } - changed_nodes[changed_wr_index] = vp; - changed_nodes_pid[changed_wr_index] = current_proc()->p_pid; - changed_wr_index = (changed_wr_index + 1) % NUM_CHANGE_NODES; +/* + * Retrieve the data of an extended attribute. + */ +int +fgetxattr(struct proc *p, struct fgetxattr_args *uap, user_ssize_t *retval) +{ + struct vnode *vp; + char attrname[XATTR_MAXNAMELEN+1]; + struct vfs_context context; + uio_t auio = NULL; + int spacetype = IS_64BIT_PROCESS(p) ? UIO_USERSPACE64 : UIO_USERSPACE32; + size_t attrsize = 0; + size_t namelen; + int error; + char uio_buf[ UIO_SIZEOF(1) ]; - simple_unlock(&changed_nodes_lock); + if (uap->options & (XATTR_NOFOLLOW | XATTR_NOSECURITY)) + return (EINVAL); - wakeup((caddr_t)&changed_rd_index); -} + if ( (error = file_vnode(uap->fd, &vp)) ) { + return (error); + } + if ( (error = vnode_getwithref(vp)) ) { + file_drop(uap->fd); + return(error); + } + if ((error = copyinstr(uap->attrname, attrname, sizeof(attrname), &namelen) != 0)) { + goto out; + } + if (xattr_protected(attrname)) { + error = EPERM; + goto out; + } + if (uap->value && uap->size > 0) { + auio = uio_createwithbuffer(1, uap->position, spacetype, UIO_READ, + &uio_buf[0], sizeof(uio_buf)); + uio_addiov(auio, uap->value, uap->size); + } + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + error = vn_getxattr(vp, attrname, auio, &attrsize, uap->options, &context); +out: + (void)vnode_put(vp); + file_drop(uap->fd); -struct fmod_watch_args { - int *new_fd; - char *pathbuf; - int len; - pid_t pid; -}; + if (auio) { + *retval = uap->size - uio_resid(auio); + } else { + *retval = (user_ssize_t)attrsize; + } + return (error); +} +/* + * Set the data of an extended attribute. + */ int -fmod_watch(struct proc *p, struct fmod_watch_args *uap, register_t *retval) +setxattr(struct proc *p, struct setxattr_args *uap, int *retval) { - int fd, didhold = 0; - struct filedesc *fdp; - struct file *fp; - struct vnode *vp; - int flags; - int type, indx, error, need_wakeup=0; - struct flock lf; - struct nameidata nd; - extern struct fileops vnops; - pid_t pid; - - if (fmod_watch_enabled == 0) { - *retval = -1; - return EINVAL; - } + struct vnode *vp; + struct nameidata nd; + char attrname[XATTR_MAXNAMELEN+1]; + struct vfs_context context; + uio_t auio = NULL; + int spacetype = IS_64BIT_PROCESS(p) ? UIO_USERSPACE64 : UIO_USERSPACE32; + size_t namelen; + u_long nameiflags; + int error; + char uio_buf[ UIO_SIZEOF(1) ]; - p = current_proc(); + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); - if (changed_init == 0) { - changed_init = 1; - simple_lock_init(&changed_nodes_lock); - } + if (uap->options & XATTR_NOSECURITY) + return (EINVAL); - if (changed_rd_index == changed_wr_index) { - // there's nothing to do, go to sleep - error = tsleep((caddr_t)&changed_rd_index, PUSER|PCATCH, "changed_nodes_empty", 0); - if (error != 0) { - // XXXdbg - what if after we unblock the changed_nodes - // table is full? We should wakeup() the writer. - *retval = -1; - return error; + if ((error = copyinstr(uap->attrname, attrname, sizeof(attrname), &namelen) != 0)) { + return (error); + } + if (xattr_protected(attrname)) + return(EPERM); + if (uap->value == 0 || uap->size == 0) { + return (EINVAL); } - } - simple_lock(&changed_nodes_lock); + nameiflags = (uap->options & XATTR_NOFOLLOW) ? 0 : FOLLOW; + NDINIT(&nd, LOOKUP, nameiflags, spacetype, uap->path, &context); + if ((error = namei(&nd))) { + return (error); + } + vp = nd.ni_vp; + nameidone(&nd); - vp = (struct vnode *)changed_nodes[changed_rd_index]; - pid = changed_nodes_pid[changed_rd_index]; - - changed_nodes[changed_rd_index] = NULL; - changed_rd_index = (changed_rd_index + 1) % NUM_CHANGE_NODES; - - if (vp == NULL) { - printf("watch_file_changes: Someone put a null vnode in my table! (%d %d)\n", - changed_rd_index, changed_wr_index); - error = EINVAL; - goto err0; - } + auio = uio_createwithbuffer(1, uap->position, spacetype, UIO_WRITE, + &uio_buf[0], sizeof(uio_buf)); + uio_addiov(auio, uap->value, uap->size); - simple_unlock(&changed_nodes_lock); - - // if the writers are blocked, wake them up as we just freed up - // some space for them. - if (notifier_sleeping > 0) { - wakeup((caddr_t)&changed_wr_index); - } + error = vn_setxattr(vp, attrname, auio, uap->options, &context); + vnode_put(vp); + *retval = 0; + return (error); +} - if (vp->v_type != VREG && vp->v_type != VDIR) { - error = EBADF; - goto err1; - } +/* + * Set the data of an extended attribute. + */ +int +fsetxattr(struct proc *p, struct fsetxattr_args *uap, int *retval) +{ + struct vnode *vp; + char attrname[XATTR_MAXNAMELEN+1]; + struct vfs_context context; + uio_t auio = NULL; + int spacetype = IS_64BIT_PROCESS(p) ? UIO_USERSPACE64 : UIO_USERSPACE32; + size_t namelen; + int error; + char uio_buf[ UIO_SIZEOF(1) ]; - if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p)) != 0) { - printf("fmod_watch: vn_lock returned %d\n", error); - goto err1; - } + if (uap->options & (XATTR_NOFOLLOW | XATTR_NOSECURITY)) + return (EINVAL); - // first copy out the name - if (uap->pathbuf) { - char *buff; - int len=MAXPATHLEN; - - MALLOC(buff, char *, len, M_TEMP, M_WAITOK); - error = vn_getpath(vp, buff, &len); - if (error == 0) { - if (len < uap->len) - error = copyout(buff, (void *)uap->pathbuf, len); - else - error = ENOSPC; + if ((error = copyinstr(uap->attrname, attrname, sizeof(attrname), &namelen) != 0)) { + return (error); } - FREE(buff, M_TEMP); - if (error) { - goto err1; + if (xattr_protected(attrname)) + return(EPERM); + if (uap->value == 0 || uap->size == 0) { + return (EINVAL); } - } - - // now copy out the pid of the person that changed the file - if (uap->pid) { - if ((error = copyout((caddr_t)&pid, (void *)uap->pid, sizeof(pid_t))) != 0) { - printf("fmod_watch: failed to copy out the pid (%d)\n", pid); - goto err1; + if ( (error = file_vnode(uap->fd, &vp)) ) { + return (error); } - } - - // now create a file descriptor for this vnode - fdp = p->p_fd; - flags = FREAD; - if (error = falloc(p, &fp, &indx)) { - printf("fmod_watch: failed to allocate an fd...\n"); - goto err2; - } - - if ((error = copyout((caddr_t)&indx, (void *)uap->new_fd, sizeof(int))) != 0) { - printf("fmod_watch: failed to copy out the new fd (%d)\n", indx); - goto err3; - } - - fp->f_flag = flags & FMASK; - fp->f_type = DTYPE_VNODE; - fp->f_ops = &vnops; - fp->f_data = (caddr_t)vp; - - if (UBCINFOEXISTS(vp) && ((didhold = ubc_hold(vp)) == 0)) { - goto err3; - } + if ( (error = vnode_getwithref(vp)) ) { + file_drop(uap->fd); + return(error); + } + auio = uio_createwithbuffer(1, uap->position, spacetype, UIO_WRITE, + &uio_buf[0], sizeof(uio_buf)); + uio_addiov(auio, uap->value, uap->size); + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + + error = vn_setxattr(vp, attrname, auio, uap->options, &context); + vnode_put(vp); + file_drop(uap->fd); + *retval = 0; + return (error); +} - error = VOP_OPEN(vp, flags, p->p_ucred, p); - if (error) { - goto err4; - } +/* + * Remove an extended attribute. + */ +#warning "code duplication" +int +removexattr(struct proc *p, struct removexattr_args *uap, int *retval) +{ + struct vnode *vp; + struct nameidata nd; + char attrname[XATTR_MAXNAMELEN+1]; + int spacetype = IS_64BIT_PROCESS(p) ? UIO_USERSPACE64 : UIO_USERSPACE32; + struct vfs_context context; + size_t namelen; + u_long nameiflags; + int error; - VOP_UNLOCK(vp, 0, p); - - *fdflags(p, indx) &= ~UF_RESERVED; + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); - // note: we explicitly don't vrele() here because it - // happens when the fd is closed. + if (uap->options & XATTR_NOSECURITY) + return (EINVAL); - return error; + error = copyinstr(uap->attrname, attrname, sizeof(attrname), &namelen); + if (error != 0) { + return (error); + } + if (xattr_protected(attrname)) + return(EPERM); + nameiflags = (uap->options & XATTR_NOFOLLOW) ? 0 : FOLLOW; + NDINIT(&nd, LOOKUP, nameiflags, spacetype, uap->path, &context); + if ((error = namei(&nd))) { + return (error); + } + vp = nd.ni_vp; + nameidone(&nd); - err4: - if (didhold) { - ubc_rele(vp); - } - err3: - ffree(fp); - fdrelse(p, indx); - err2: - VOP_UNLOCK(vp, 0, p); - err1: - vrele(vp); // undoes the vref() in notify_filemod_watchers() - - err0: - *retval = -1; - return error; + error = vn_removexattr(vp, attrname, uap->options, &context); + vnode_put(vp); + *retval = 0; + return (error); } -static int -enable_fmod_watching(register_t *retval) +/* + * Remove an extended attribute. + */ +#warning "code duplication" +int +fremovexattr(struct proc *p, struct fremovexattr_args *uap, int *retval) { - *retval = -1; + struct vnode *vp; + char attrname[XATTR_MAXNAMELEN+1]; + struct vfs_context context; + size_t namelen; + int error; - if (!is_suser()) { - return EPERM; - } - - // XXXdbg for now we only allow one watcher at a time. - if (fmod_watch_enabled) { - return EBUSY; - } - - fmod_watch_enabled++; - fmod_watch_owner = current_proc()->p_pid; + if (uap->options & (XATTR_NOFOLLOW | XATTR_NOSECURITY)) + return (EINVAL); + + error = copyinstr(uap->attrname, attrname, sizeof(attrname), &namelen); + if (error != 0) { + return (error); + } + if (xattr_protected(attrname)) + return(EPERM); + if ( (error = file_vnode(uap->fd, &vp)) ) { + return (error); + } + if ( (error = vnode_getwithref(vp)) ) { + file_drop(uap->fd); + return(error); + } + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); - *retval = 0; - return 0; + error = vn_removexattr(vp, attrname, uap->options, &context); + vnode_put(vp); + file_drop(uap->fd); + *retval = 0; + return (error); } -static int -disable_fmod_watching(register_t *retval) +/* + * Retrieve the list of extended attribute names. + */ +#warning "code duplication" +int +listxattr(struct proc *p, struct listxattr_args *uap, user_ssize_t *retval) { - if (!is_suser()) { - return EPERM; - } - - if (fmod_watch_enabled < 1) { - printf("fmod_watching: too many disables! (%d)\n", fmod_watch_enabled); - return EINVAL; - } + struct vnode *vp; + struct nameidata nd; + struct vfs_context context; + uio_t auio = NULL; + int spacetype = IS_64BIT_PROCESS(p) ? UIO_USERSPACE64 : UIO_USERSPACE32; + size_t attrsize = 0; + u_long nameiflags; + int error; + char uio_buf[ UIO_SIZEOF(1) ]; - fmod_watch_enabled--; - - // if we're the last guy, clear out any remaining vnodes - // in the table so they don't remain referenced. - // - if (fmod_watch_enabled == 0) { - int i; - for(i=changed_rd_index; i != changed_wr_index; ) { - if (changed_nodes[i] == NULL) { - panic("disable_fmod_watch: index %d is NULL!\n", i); - } - vrele((struct vnode *)changed_nodes[i]); - changed_nodes[i] = NULL; - i = (i + 1) % NUM_CHANGE_NODES; - } - changed_wr_index = changed_rd_index = 0; + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); - fmod_watch_owner = 0; - } + if (uap->options & XATTR_NOSECURITY) + return (EINVAL); - // wake up anyone that may be waiting for the - // queue to clear out. - // - while(notifier_sleeping) { - wakeup((caddr_t)&changed_wr_index); + nameiflags = (uap->options & XATTR_NOFOLLOW) ? 0 : FOLLOW; + NDINIT(&nd, LOOKUP, nameiflags, spacetype, uap->path, &context); + if ((error = namei(&nd))) { + return (error); + } + vp = nd.ni_vp; + nameidone(&nd); + if (uap->namebuf != 0 && uap->bufsize > 0) { + // LP64todo - fix this! + auio = uio_createwithbuffer(1, 0, spacetype, + UIO_READ, &uio_buf[0], sizeof(uio_buf)); + uio_addiov(auio, uap->namebuf, uap->bufsize); + } - // yield the cpu so the notifiers can run - tsleep((caddr_t)&fmod_watch_enabled, PINOD, "disable_fmod_watch", 1); - } + error = vn_listxattr(vp, auio, &attrsize, uap->options, &context); - *retval = 0; - return 0; + vnode_put(vp); + if (auio) { + *retval = (user_ssize_t)uap->bufsize - uio_resid(auio); + } else { + *retval = (user_ssize_t)attrsize; + } + return (error); } - -struct fmod_watch_enable_args { - int on_or_off; -}; - +/* + * Retrieve the list of extended attribute names. + */ +#warning "code duplication" int -fmod_watch_enable(struct proc *p, struct fmod_watch_enable_args *uap, register_t *retval) +flistxattr(struct proc *p, struct flistxattr_args *uap, user_ssize_t *retval) { - int ret; - - if (uap->on_or_off != 0) { - ret = enable_fmod_watching(retval); - } else { - ret = disable_fmod_watching(retval); - } + struct vnode *vp; + struct vfs_context context; + uio_t auio = NULL; + int spacetype = proc_is64bit(p) ? UIO_USERSPACE64 : UIO_USERSPACE32; + size_t attrsize = 0; + int error; + char uio_buf[ UIO_SIZEOF(1) ]; + + if (uap->options & (XATTR_NOFOLLOW | XATTR_NOSECURITY)) + return (EINVAL); + + if ( (error = file_vnode(uap->fd, &vp)) ) { + return (error); + } + if ( (error = vnode_getwithref(vp)) ) { + file_drop(uap->fd); + return(error); + } + if (uap->namebuf != 0 && uap->bufsize > 0) { + // LP64todo - fix this! + auio = uio_createwithbuffer(1, 0, spacetype, + UIO_READ, &uio_buf[0], sizeof(uio_buf)); + uio_addiov(auio, uap->namebuf, uap->bufsize); + } + context.vc_proc = p; + context.vc_ucred = kauth_cred_get(); + + error = vn_listxattr(vp, auio, &attrsize, uap->options, &context); - return ret; + vnode_put(vp); + file_drop(uap->fd); + if (auio) { + *retval = (user_ssize_t)uap->bufsize - uio_resid(auio); + } else { + *retval = (user_ssize_t)attrsize; + } + return (error); } -void -clean_up_fmod_watch(struct proc *p) +/* + * Common routine to handle various flavors of statfs data heading out + * to user space. + */ +static int +munge_statfs(struct mount *mp, struct vfsstatfs *sfsp, + user_addr_t bufp, int *sizep, boolean_t is_64_bit, + boolean_t partial_copy) { - if (fmod_watch_enabled && fmod_watch_owner == p->p_pid) { - register_t *retval; + int error; + int my_size, copy_size; + + if (is_64_bit) { + struct user_statfs sfs; + my_size = copy_size = sizeof(sfs); + bzero(&sfs, my_size); + sfs.f_flags = mp->mnt_flag & MNT_VISFLAGMASK; + sfs.f_type = mp->mnt_vtable->vfc_typenum; + sfs.f_reserved1 = (short)sfsp->f_fssubtype; + sfs.f_bsize = (user_long_t)sfsp->f_bsize; + sfs.f_iosize = (user_long_t)sfsp->f_iosize; + sfs.f_blocks = (user_long_t)sfsp->f_blocks; + sfs.f_bfree = (user_long_t)sfsp->f_bfree; + sfs.f_bavail = (user_long_t)sfsp->f_bavail; + sfs.f_files = (user_long_t)sfsp->f_files; + sfs.f_ffree = (user_long_t)sfsp->f_ffree; + sfs.f_fsid = sfsp->f_fsid; + sfs.f_owner = sfsp->f_owner; + strncpy(&sfs.f_fstypename[0], &sfsp->f_fstypename[0], MFSNAMELEN-1); + strncpy(&sfs.f_mntonname[0], &sfsp->f_mntonname[0], MNAMELEN-1); + strncpy(&sfs.f_mntfromname[0], &sfsp->f_mntfromname[0], MNAMELEN-1); + + if (partial_copy) { + copy_size -= (sizeof(sfs.f_reserved3) + sizeof(sfs.f_reserved4)); + } + error = copyout((caddr_t)&sfs, bufp, copy_size); + } + else { + struct statfs sfs; + my_size = copy_size = sizeof(sfs); + bzero(&sfs, my_size); + + sfs.f_flags = mp->mnt_flag & MNT_VISFLAGMASK; + sfs.f_type = mp->mnt_vtable->vfc_typenum; + sfs.f_reserved1 = (short)sfsp->f_fssubtype; + + /* + * It's possible for there to be more than 2^^31 blocks in the filesystem, so we + * have to fudge the numbers here in that case. We inflate the blocksize in order + * to reflect the filesystem size as best we can. + */ + if ((sfsp->f_blocks > LONG_MAX) + /* Hack for 4061702 . I think the real fix is for Carbon to + * look for some volume capability and not depend on hidden + * semantics agreed between a FS and carbon. + * f_blocks, f_bfree, and f_bavail set to -1 is the trigger + * for Carbon to set bNoVolumeSizes volume attribute. + * Without this the webdavfs files cannot be copied onto + * disk as they look huge. This change should not affect + * XSAN as they should not setting these to -1.. + */ + && (sfsp->f_blocks != 0xffffffffffffffff) + && (sfsp->f_bfree != 0xffffffffffffffff) + && (sfsp->f_bavail != 0xffffffffffffffff)) { + int shift; + + /* + * Work out how far we have to shift the block count down to make it fit. + * Note that it's possible to have to shift so far that the resulting + * blocksize would be unreportably large. At that point, we will clip + * any values that don't fit. + * + * For safety's sake, we also ensure that f_iosize is never reported as + * being smaller than f_bsize. + */ + for (shift = 0; shift < 32; shift++) { + if ((sfsp->f_blocks >> shift) <= LONG_MAX) + break; + if ((sfsp->f_bsize << (shift + 1)) > LONG_MAX) + break; + } +#define __SHIFT_OR_CLIP(x, s) ((((x) >> (s)) > LONG_MAX) ? LONG_MAX : ((x) >> (s))) + sfs.f_blocks = (long)__SHIFT_OR_CLIP(sfsp->f_blocks, shift); + sfs.f_bfree = (long)__SHIFT_OR_CLIP(sfsp->f_bfree, shift); + sfs.f_bavail = (long)__SHIFT_OR_CLIP(sfsp->f_bavail, shift); +#undef __SHIFT_OR_CLIP + sfs.f_bsize = (long)(sfsp->f_bsize << shift); + sfs.f_iosize = lmax(sfsp->f_iosize, sfsp->f_bsize); + } else { + /* filesystem is small enough to be reported honestly */ + sfs.f_bsize = (long)sfsp->f_bsize; + sfs.f_iosize = (long)sfsp->f_iosize; + sfs.f_blocks = (long)sfsp->f_blocks; + sfs.f_bfree = (long)sfsp->f_bfree; + sfs.f_bavail = (long)sfsp->f_bavail; + } + sfs.f_files = (long)sfsp->f_files; + sfs.f_ffree = (long)sfsp->f_ffree; + sfs.f_fsid = sfsp->f_fsid; + sfs.f_owner = sfsp->f_owner; + strncpy(&sfs.f_fstypename[0], &sfsp->f_fstypename[0], MFSNAMELEN-1); + strncpy(&sfs.f_mntonname[0], &sfsp->f_mntonname[0], MNAMELEN-1); + strncpy(&sfs.f_mntfromname[0], &sfsp->f_mntfromname[0], MNAMELEN-1); + + if (partial_copy) { + copy_size -= (sizeof(sfs.f_reserved3) + sizeof(sfs.f_reserved4)); + } + error = copyout((caddr_t)&sfs, bufp, copy_size); + } - disable_fmod_watching(&retval); - } + if (sizep != NULL) { + *sizep = my_size; + } + return(error); +} + +/* + * copy stat structure into user_stat structure. + */ +void munge_stat(struct stat *sbp, struct user_stat *usbp) +{ + usbp->st_dev = sbp->st_dev; + usbp->st_ino = sbp->st_ino; + usbp->st_mode = sbp->st_mode; + usbp->st_nlink = sbp->st_nlink; + usbp->st_uid = sbp->st_uid; + usbp->st_gid = sbp->st_gid; + usbp->st_rdev = sbp->st_rdev; +#ifndef _POSIX_SOURCE + usbp->st_atimespec.tv_sec = sbp->st_atimespec.tv_sec; + usbp->st_atimespec.tv_nsec = sbp->st_atimespec.tv_nsec; + usbp->st_mtimespec.tv_sec = sbp->st_mtimespec.tv_sec; + usbp->st_mtimespec.tv_nsec = sbp->st_mtimespec.tv_nsec; + usbp->st_ctimespec.tv_sec = sbp->st_ctimespec.tv_sec; + usbp->st_ctimespec.tv_nsec = sbp->st_ctimespec.tv_nsec; +#else + usbp->st_atime = sbp->st_atime; + usbp->st_atimensec = sbp->st_atimensec; + usbp->st_mtime = sbp->st_mtime; + usbp->st_mtimensec = sbp->st_mtimensec; + usbp->st_ctime = sbp->st_ctime; + usbp->st_ctimensec = sbp->st_ctimensec; +#endif + usbp->st_size = sbp->st_size; + usbp->st_blocks = sbp->st_blocks; + usbp->st_blksize = sbp->st_blksize; + usbp->st_flags = sbp->st_flags; + usbp->st_gen = sbp->st_gen; + usbp->st_lspare = sbp->st_lspare; + usbp->st_qspare[0] = sbp->st_qspare[0]; + usbp->st_qspare[1] = sbp->st_qspare[1]; }