#include <sys/param.h>
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/file_internal.h>
#include <sys/stat.h>
#include <sys/proc_internal.h>
#include <sys/kauth.h>
#include <sys/mount_internal.h>
#include <sys/namei.h>
#include <sys/vnode_internal.h>
#include <sys/ioctl.h>
#include <sys/tty.h>
#define ubc_setcred ubc_setcred_deprecated
#include <sys/ubc.h>
#undef ubc_setcred
int ubc_setcred(struct vnode *, struct proc *);
#include <sys/conf.h>
#include <sys/disk.h>
#include <sys/fsevents.h>
#include <sys/kdebug.h>
#include <sys/xattr.h>
#include <sys/ubc_internal.h>
#include <sys/uio_internal.h>
#include <sys/resourcevar.h>
#include <sys/signalvar.h>
#include <vm/vm_kern.h>
#include <vm/vm_map.h>
#include <miscfs/specfs/specdev.h>
#include <miscfs/fifofs/fifo.h>
#if CONFIG_MACF
#include <security/mac_framework.h>
#endif
static int vn_closefile(struct fileglob *fp, vfs_context_t ctx);
static int vn_ioctl(struct fileproc *fp, u_long com, caddr_t data,
vfs_context_t ctx);
static int vn_read(struct fileproc *fp, struct uio *uio, int flags,
vfs_context_t ctx);
static int vn_write(struct fileproc *fp, struct uio *uio, int flags,
vfs_context_t ctx);
static int vn_select( struct fileproc *fp, int which, void * wql,
vfs_context_t ctx);
static int vn_kqfilt_add(struct fileproc *fp, struct knote *kn,
vfs_context_t ctx);
static void filt_vndetach(struct knote *kn);
static int filt_vnode(struct knote *kn, long hint);
#if 0
static int vn_kqfilt_remove(struct vnode *vp, uintptr_t ident,
vfs_context_t ctx);
#endif
struct fileops vnops =
{ vn_read, vn_write, vn_ioctl, vn_select, vn_closefile, vn_kqfilt_add, NULL };
struct filterops vnode_filtops = {
.f_isfd = 1,
.f_attach = NULL,
.f_detach = filt_vndetach,
.f_event = filt_vnode
};
int
vn_open(struct nameidata *ndp, int fmode, int cmode)
{
return(vn_open_modflags(ndp, &fmode, cmode));
}
int
vn_open_modflags(struct nameidata *ndp, int *fmodep, int cmode)
{
struct vnode_attr va;
VATTR_INIT(&va);
VATTR_SET(&va, va_mode, cmode);
return(vn_open_auth(ndp, fmodep, &va));
}
int
vn_open_auth(struct nameidata *ndp, int *fmodep, struct vnode_attr *vap)
{
struct vnode *vp;
struct vnode *dvp;
vfs_context_t ctx = ndp->ni_cnd.cn_context;
int error;
int fmode;
uint32_t origcnflags;
kauth_action_t action;
again:
vp = NULL;
dvp = NULL;
fmode = *fmodep;
origcnflags = ndp->ni_cnd.cn_flags;
if (fmode & O_CREAT) {
if ( (fmode & O_DIRECTORY) ) {
error = EINVAL;
goto out;
}
ndp->ni_cnd.cn_nameiop = CREATE;
ndp->ni_cnd.cn_flags &= (USEDVP | NOCROSSMOUNT | DOWHITEOUT);
ndp->ni_cnd.cn_flags |= LOCKPARENT | LOCKLEAF | AUDITVNPATH1;
#if NAMEDRSRCFORK
ndp->ni_cnd.cn_flags |= CN_ALLOWRSRCFORK;
#endif
if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0 && (origcnflags & FOLLOW) != 0)
ndp->ni_cnd.cn_flags |= FOLLOW;
if ( (error = namei(ndp)) )
goto out;
dvp = ndp->ni_dvp;
vp = ndp->ni_vp;
if (vp == NULL) {
if (vap == NULL) {
error = EINVAL;
goto badcreate;
}
VATTR_SET(vap, va_type, VREG);
#if CONFIG_MACF
error = mac_vnode_check_create(ctx,
dvp, &ndp->ni_cnd, vap);
if (error)
goto badcreate;
#endif
if ((error = vnode_authorize(dvp, NULL, KAUTH_VNODE_ADD_FILE, ctx)) != 0)
goto badcreate;
if (fmode & O_EXCL)
vap->va_vaflags |= VA_EXCLUSIVE;
#if NAMEDRSRCFORK
if (ndp->ni_cnd.cn_flags & CN_WANTSRSRCFORK) {
if ((error = vnode_makenamedstream(dvp, &ndp->ni_vp, XATTR_RESOURCEFORK_NAME, 0, ctx)) != 0)
goto badcreate;
} else
#endif
if ((error = vn_create(dvp, &ndp->ni_vp, &ndp->ni_cnd, vap, 0, ctx)) != 0)
goto badcreate;
vp = ndp->ni_vp;
if (vp) {
int update_flags = 0;
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, ndp->ni_cnd.cn_nameptr, ndp->ni_cnd.cn_namelen, ndp->ni_cnd.cn_hash, update_flags);
#if CONFIG_FSE
if (need_fsevent(FSE_CREATE_FILE, vp)) {
vnode_put(dvp);
dvp = NULL;
add_fsevent(FSE_CREATE_FILE, ctx,
FSE_ARG_VNODE, vp,
FSE_ARG_DONE);
}
#endif
}
badcreate:
nameidone(ndp);
ndp->ni_dvp = NULL;
if (dvp) {
vnode_put(dvp);
}
if (error) {
if (((error == EEXIST) && !(fmode & O_EXCL)) ||
((error == ENOENT) && (fmode & O_CREAT))){
goto again;
}
goto bad;
}
fmode &= ~O_TRUNC;
} else {
nameidone(ndp);
ndp->ni_dvp = NULL;
vnode_put(dvp);
if (fmode & O_EXCL) {
error = EEXIST;
goto bad;
}
fmode &= ~O_CREAT;
}
} else {
ndp->ni_cnd.cn_nameiop = LOOKUP;
ndp->ni_cnd.cn_flags &= (USEDVP | NOCROSSMOUNT | DOWHITEOUT);
ndp->ni_cnd.cn_flags |= FOLLOW | LOCKLEAF | AUDITVNPATH1;
#if NAMEDRSRCFORK
ndp->ni_cnd.cn_flags |= CN_ALLOWRSRCFORK;
#endif
if (fmode & O_NOFOLLOW || fmode & O_SYMLINK || (origcnflags & FOLLOW) == 0) {
ndp->ni_cnd.cn_flags &= ~FOLLOW;
}
if ( (error = namei(ndp)) )
goto out;
vp = ndp->ni_vp;
nameidone(ndp);
ndp->ni_dvp = NULL;
if ( (fmode & O_DIRECTORY) && vp->v_type != VDIR ) {
error = ENOTDIR;
goto bad;
}
}
if (vp->v_type == VSOCK && vp->v_tag != VT_FDESC) {
error = EOPNOTSUPP;
goto bad;
}
if (vp->v_type == VLNK && (fmode & O_NOFOLLOW) != 0) {
error = ELOOP;
goto bad;
}
if ((fmode & O_CREAT) == 0) {
if (vnode_isdir(vp) && (fmode & (FWRITE | O_TRUNC))) {
error = EISDIR;
goto bad;
}
#if CONFIG_MACF
error = mac_vnode_check_open(ctx, vp, fmode);
if (error)
goto bad;
#endif
action = 0;
if (fmode & FREAD) {
action |= KAUTH_VNODE_READ_DATA;
}
if (fmode & (FWRITE | O_TRUNC)) {
if ((fmode & O_APPEND) && !(fmode & O_TRUNC)) {
action |= KAUTH_VNODE_APPEND_DATA;
} else {
action |= KAUTH_VNODE_WRITE_DATA;
}
}
if ((error = vnode_authorize(vp, NULL, action, ctx)) != 0)
goto bad;
if ((vp->v_flag & VOPENEVT) && (current_proc()->p_flag & P_CHECKOPENEVT)) {
fmode |= O_EVTONLY;
}
}
if ( (error = VNOP_OPEN(vp, fmode, ctx)) ) {
goto bad;
}
if ( (error = vnode_ref_ext(vp, fmode)) ) {
goto bad2;
}
kauth_authorize_fileop(vfs_context_ucred(ctx), KAUTH_FILEOP_OPEN,
(uintptr_t)vp, 0);
*fmodep = fmode;
return (0);
bad2:
VNOP_CLOSE(vp, fmode, ctx);
bad:
ndp->ni_vp = NULL;
if (vp) {
#if NAMEDRSRCFORK
if ((vnode_isnamedstream(vp)) &&
(vp->v_parent != NULLVP) &&
(vnode_isshadow(vp))) {
vnode_recycle(vp);
}
#endif
vnode_put(vp);
if (((error == ENOENT) && (*fmodep & O_CREAT)) || (error == EREDRIVEOPEN)) {
goto again;
}
}
out:
return (error);
}
#if vn_access_DEPRECATED
int
vn_access(vnode_t vp, int mode, vfs_context_t context)
{
kauth_action_t action;
action = 0;
if (mode & VREAD)
action |= KAUTH_VNODE_READ_DATA;
if (mode & VWRITE)
action |= KAUTH_VNODE_WRITE_DATA;
if (mode & VEXEC)
action |= KAUTH_VNODE_EXECUTE;
return(vnode_authorize(vp, NULL, action, context));
}
#endif
int
vn_close(struct vnode *vp, int flags, vfs_context_t ctx)
{
int error;
#if CONFIG_FSE
if (flags & FWASWRITTEN) {
if (need_fsevent(FSE_CONTENT_MODIFIED, vp)) {
add_fsevent(FSE_CONTENT_MODIFIED, ctx,
FSE_ARG_VNODE, vp,
FSE_ARG_DONE);
}
}
#endif
#if NAMEDRSRCFORK
if ((vp->v_flag & VISNAMEDSTREAM) &&
(vp->v_parent != NULLVP) &&
vnode_isshadow(vp)) {
if (flags & FWASWRITTEN) {
(void) vnode_flushnamedstream(vp->v_parent, vp, ctx);
}
}
#endif
if (vnode_isspec(vp))
(void)vnode_rele_ext(vp, flags, 0);
error = VNOP_CLOSE(vp, flags, ctx);
if (!vnode_isspec(vp))
(void)vnode_rele_ext(vp, flags, 0);
return (error);
}
static int
vn_read_swapfile(
struct vnode *vp,
uio_t uio)
{
int error;
off_t swap_count, this_count;
off_t file_end, read_end;
off_t prev_resid;
char *my_swap_page;
my_swap_page = NULL;
error = 0;
swap_count = uio_resid(uio);
file_end = ubc_getsize(vp);
read_end = uio->uio_offset + uio_resid(uio);
if (uio->uio_offset >= file_end) {
swap_count = 0;
} else if (read_end > file_end) {
swap_count -= (read_end - file_end);
}
while (swap_count > 0) {
if (my_swap_page == NULL) {
MALLOC(my_swap_page, char *, PAGE_SIZE,
M_TEMP, M_WAITOK);
memset(my_swap_page, '\0', PAGE_SIZE);
my_swap_page[PAGE_SIZE-1] = '\n';
}
this_count = swap_count;
if (this_count > PAGE_SIZE) {
this_count = PAGE_SIZE;
}
prev_resid = uio_resid(uio);
error = uiomove((caddr_t) my_swap_page,
this_count,
uio);
if (error) {
break;
}
swap_count -= (prev_resid - uio_resid(uio));
}
if (my_swap_page != NULL) {
FREE(my_swap_page, M_TEMP);
my_swap_page = NULL;
}
return error;
}
int
vn_rdwr(
enum uio_rw rw,
struct vnode *vp,
caddr_t base,
int len,
off_t offset,
enum uio_seg segflg,
int ioflg,
kauth_cred_t cred,
int *aresid,
proc_t p)
{
int64_t resid;
int result;
result = vn_rdwr_64(rw,
vp,
(uint64_t)(uintptr_t)base,
(int64_t)len,
offset,
segflg,
ioflg,
cred,
&resid,
p);
if (aresid != NULL) {
*aresid = resid;
}
return result;
}
int
vn_rdwr_64(
enum uio_rw rw,
struct vnode *vp,
uint64_t base,
int64_t len,
off_t offset,
enum uio_seg segflg,
int ioflg,
kauth_cred_t cred,
int64_t *aresid,
proc_t p)
{
uio_t auio;
int spacetype;
struct vfs_context context;
int error=0;
char uio_buf[ UIO_SIZEOF(1) ];
context.vc_thread = current_thread();
context.vc_ucred = cred;
if (UIO_SEG_IS_USER_SPACE(segflg)) {
spacetype = proc_is64bit(p) ? UIO_USERSPACE64 : UIO_USERSPACE32;
}
else {
spacetype = UIO_SYSSPACE;
}
auio = uio_createwithbuffer(1, offset, spacetype, rw,
&uio_buf[0], sizeof(uio_buf));
uio_addiov(auio, base, len);
#if CONFIG_MACF
if ((ioflg & IO_NOAUTH) == 0) {
if (rw == UIO_READ)
error = mac_vnode_check_read(&context, cred, vp);
else
error = mac_vnode_check_write(&context, cred, vp);
}
#endif
if (error == 0) {
if (rw == UIO_READ) {
if (vnode_isswap(vp)) {
error = vn_read_swapfile(vp, auio);
} else {
error = VNOP_READ(vp, auio, ioflg, &context);
}
} else {
error = VNOP_WRITE(vp, auio, ioflg, &context);
}
}
if (aresid)
*aresid = uio_resid(auio);
else
if (uio_resid(auio) && error == 0)
error = EIO;
return (error);
}
static int
vn_read(struct fileproc *fp, struct uio *uio, int flags, vfs_context_t ctx)
{
struct vnode *vp;
int error, ioflag;
off_t count;
vp = (struct vnode *)fp->f_fglob->fg_data;
if ( (error = vnode_getwithref(vp)) ) {
return(error);
}
#if CONFIG_MACF
error = mac_vnode_check_read(ctx, vfs_context_ucred(ctx), vp);
if (error) {
(void)vnode_put(vp);
return (error);
}
#endif
ioflag = 0;
if (fp->f_fglob->fg_flag & FNONBLOCK)
ioflag |= IO_NDELAY;
if ((fp->f_fglob->fg_flag & FNOCACHE) || vnode_isnocache(vp))
ioflag |= IO_NOCACHE;
if (fp->f_fglob->fg_flag & FNORDAHEAD)
ioflag |= IO_RAOFF;
if ((flags & FOF_OFFSET) == 0)
uio->uio_offset = fp->f_fglob->fg_offset;
count = uio_resid(uio);
if (vnode_isswap(vp)) {
error = vn_read_swapfile(vp, uio);
} else {
error = VNOP_READ(vp, uio, ioflag, ctx);
}
if ((flags & FOF_OFFSET) == 0)
fp->f_fglob->fg_offset += count - uio_resid(uio);
(void)vnode_put(vp);
return (error);
}
static int
vn_write(struct fileproc *fp, struct uio *uio, int flags, vfs_context_t ctx)
{
struct vnode *vp;
int error, ioflag;
off_t count;
int clippedsize = 0;
int partialwrite=0;
int residcount, oldcount;
proc_t p = vfs_context_proc(ctx);
count = 0;
vp = (struct vnode *)fp->f_fglob->fg_data;
if ( (error = vnode_getwithref(vp)) ) {
return(error);
}
#if CONFIG_MACF
error = mac_vnode_check_write(ctx, vfs_context_ucred(ctx), vp);
if (error) {
(void)vnode_put(vp);
return (error);
}
#endif
ioflag = IO_UNIT;
if (vp->v_type == VREG && (fp->f_fglob->fg_flag & O_APPEND))
ioflag |= IO_APPEND;
if (fp->f_fglob->fg_flag & FNONBLOCK)
ioflag |= IO_NDELAY;
if ((fp->f_fglob->fg_flag & FNOCACHE) || vnode_isnocache(vp))
ioflag |= IO_NOCACHE;
if ((fp->f_fglob->fg_flag & (O_FSYNC|O_DSYNC)) ||
(vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))) {
ioflag |= IO_SYNC;
}
if ((flags & FOF_OFFSET) == 0) {
uio->uio_offset = fp->f_fglob->fg_offset;
count = uio_resid(uio);
}
if (((flags & FOF_OFFSET) == 0) &&
vfs_context_proc(ctx) && (vp->v_type == VREG) &&
(((rlim_t)(uio->uio_offset + uio_resid(uio)) > p->p_rlimit[RLIMIT_FSIZE].rlim_cur) ||
((rlim_t)uio_resid(uio) > (p->p_rlimit[RLIMIT_FSIZE].rlim_cur - uio->uio_offset)))) {
residcount = uio_resid(uio);
if ((rlim_t)(uio->uio_offset + uio_resid(uio)) > p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
clippedsize = (uio->uio_offset + uio_resid(uio)) - p->p_rlimit[RLIMIT_FSIZE].rlim_cur;
} else if ((rlim_t)uio_resid(uio) > (p->p_rlimit[RLIMIT_FSIZE].rlim_cur - uio->uio_offset)) {
clippedsize = (p->p_rlimit[RLIMIT_FSIZE].rlim_cur - uio->uio_offset);
}
if (clippedsize >= residcount) {
psignal(p, SIGXFSZ);
vnode_put(vp);
return (EFBIG);
}
partialwrite = 1;
uio_setresid(uio, residcount-clippedsize);
}
if ((flags & FOF_OFFSET) != 0) {
ioflag &= ~IO_APPEND;
if (p && (vp->v_type == VREG) &&
((rlim_t)uio->uio_offset >= p->p_rlimit[RLIMIT_FSIZE].rlim_cur)) {
psignal(p, SIGXFSZ);
vnode_put(vp);
return (EFBIG);
}
if (p && (vp->v_type == VREG) &&
((rlim_t)(uio->uio_offset + uio_resid(uio)) > p->p_rlimit[RLIMIT_FSIZE].rlim_cur)) {
residcount = uio_resid(uio);
clippedsize = (uio->uio_offset + uio_resid(uio)) - p->p_rlimit[RLIMIT_FSIZE].rlim_cur;
partialwrite = 1;
uio_setresid(uio, residcount-clippedsize);
}
}
error = VNOP_WRITE(vp, uio, ioflag, ctx);
if (partialwrite) {
oldcount = uio_resid(uio);
uio_setresid(uio, oldcount + clippedsize);
}
if ((flags & FOF_OFFSET) == 0) {
if (ioflag & IO_APPEND)
fp->f_fglob->fg_offset = uio->uio_offset;
else
fp->f_fglob->fg_offset += count - uio_resid(uio);
}
if ((error == 0) && (vp->v_tag == VT_NFS) && (UBCINFOEXISTS(vp))) {
if ((flags & FOF_PCRED) == 0) {
ubc_setthreadcred(vp, p, current_thread());
} else {
ubc_setcred(vp, p);
}
}
(void)vnode_put(vp);
return (error);
}
int
vn_stat_noauth(struct vnode *vp, void *sbptr, kauth_filesec_t *xsec, int isstat64, vfs_context_t ctx)
{
struct vnode_attr va;
int error;
u_short mode;
kauth_filesec_t fsec;
struct stat *sb = (struct stat *)0;
struct stat64 * sb64 = (struct stat64 *)0;
if (isstat64 != 0)
sb64 = (struct stat64 *)sbptr;
else
sb = (struct stat *)sbptr;
memset(&va, 0, sizeof(va));
VATTR_INIT(&va);
VATTR_WANTED(&va, va_fsid);
VATTR_WANTED(&va, va_fileid);
VATTR_WANTED(&va, va_mode);
VATTR_WANTED(&va, va_type);
VATTR_WANTED(&va, va_nlink);
VATTR_WANTED(&va, va_uid);
VATTR_WANTED(&va, va_gid);
VATTR_WANTED(&va, va_rdev);
VATTR_WANTED(&va, va_data_size);
VATTR_WANTED(&va, va_access_time);
VATTR_WANTED(&va, va_modify_time);
VATTR_WANTED(&va, va_change_time);
VATTR_WANTED(&va, va_create_time);
VATTR_WANTED(&va, va_flags);
VATTR_WANTED(&va, va_gen);
VATTR_WANTED(&va, va_iosize);
VATTR_WANTED(&va, va_total_alloc);
if (xsec != NULL) {
VATTR_WANTED(&va, va_uuuid);
VATTR_WANTED(&va, va_guuid);
VATTR_WANTED(&va, va_acl);
}
error = vnode_getattr(vp, &va, ctx);
if (error)
goto out;
if (isstat64 != 0) {
sb64->st_dev = va.va_fsid;
sb64->st_ino = (ino64_t)va.va_fileid;
} else {
sb->st_dev = va.va_fsid;
sb->st_ino = (ino_t)va.va_fileid;
}
mode = va.va_mode;
switch (vp->v_type) {
case VREG:
mode |= S_IFREG;
break;
case VDIR:
mode |= S_IFDIR;
break;
case VBLK:
mode |= S_IFBLK;
break;
case VCHR:
mode |= S_IFCHR;
break;
case VLNK:
mode |= S_IFLNK;
break;
case VSOCK:
mode |= S_IFSOCK;
break;
case VFIFO:
mode |= S_IFIFO;
break;
default:
error = EBADF;
goto out;
};
if (isstat64 != 0) {
sb64->st_mode = mode;
sb64->st_nlink = VATTR_IS_SUPPORTED(&va, va_nlink) ? (u_int16_t)va.va_nlink : 1;
sb64->st_uid = va.va_uid;
sb64->st_gid = va.va_gid;
sb64->st_rdev = va.va_rdev;
sb64->st_size = va.va_data_size;
sb64->st_atimespec = va.va_access_time;
sb64->st_mtimespec = va.va_modify_time;
sb64->st_ctimespec = va.va_change_time;
sb64->st_birthtimespec =
VATTR_IS_SUPPORTED(&va, va_create_time) ? va.va_create_time : va.va_change_time;
sb64->st_blksize = va.va_iosize;
sb64->st_flags = va.va_flags;
sb64->st_blocks = roundup(va.va_total_alloc, 512) / 512;
} else {
sb->st_mode = mode;
sb->st_nlink = VATTR_IS_SUPPORTED(&va, va_nlink) ? (u_int16_t)va.va_nlink : 1;
sb->st_uid = va.va_uid;
sb->st_gid = va.va_gid;
sb->st_rdev = va.va_rdev;
sb->st_size = va.va_data_size;
sb->st_atimespec = va.va_access_time;
sb->st_mtimespec = va.va_modify_time;
sb->st_ctimespec = va.va_change_time;
sb->st_blksize = va.va_iosize;
sb->st_flags = va.va_flags;
sb->st_blocks = roundup(va.va_total_alloc, 512) / 512;
}
if (xsec != NULL) {
if (!VATTR_IS_SUPPORTED(&va, va_acl) &&
!VATTR_IS_SUPPORTED(&va, va_uuuid) &&
!VATTR_IS_SUPPORTED(&va, va_guuid)) {
*xsec = KAUTH_FILESEC_NONE;
} else {
if (VATTR_IS_SUPPORTED(&va, va_acl) && (va.va_acl != NULL)) {
fsec = kauth_filesec_alloc(va.va_acl->acl_entrycount);
} else {
fsec = kauth_filesec_alloc(0);
}
if (fsec == NULL) {
error = ENOMEM;
goto out;
}
fsec->fsec_magic = KAUTH_FILESEC_MAGIC;
if (VATTR_IS_SUPPORTED(&va, va_uuuid)) {
fsec->fsec_owner = va.va_uuuid;
} else {
fsec->fsec_owner = kauth_null_guid;
}
if (VATTR_IS_SUPPORTED(&va, va_guuid)) {
fsec->fsec_group = va.va_guuid;
} else {
fsec->fsec_group = kauth_null_guid;
}
if (VATTR_IS_SUPPORTED(&va, va_acl) && (va.va_acl != NULL)) {
bcopy(va.va_acl, &(fsec->fsec_acl), KAUTH_ACL_COPYSIZE(va.va_acl));
} else {
fsec->fsec_acl.acl_entrycount = KAUTH_FILESEC_NOACL;
}
*xsec = fsec;
}
}
if (va.va_gen && !vfs_context_issuser(ctx)) {
if (isstat64 != 0)
sb64->st_gen = 0;
else
sb->st_gen = 0;
} else {
if (isstat64 != 0)
sb64->st_gen = va.va_gen;
else
sb->st_gen = va.va_gen;
}
error = 0;
out:
if (VATTR_IS_SUPPORTED(&va, va_acl) && va.va_acl != NULL)
kauth_acl_free(va.va_acl);
return (error);
}
int
vn_stat(struct vnode *vp, void *sb, kauth_filesec_t *xsec, int isstat64, vfs_context_t ctx)
{
int error;
#if CONFIG_MACF
error = mac_vnode_check_stat(ctx, NOCRED, vp);
if (error)
return (error);
#endif
if ((error = vnode_authorize(vp, NULL, KAUTH_VNODE_READ_ATTRIBUTES | KAUTH_VNODE_READ_SECURITY, ctx)) != 0)
return(error);
return(vn_stat_noauth(vp, sb, xsec, isstat64, ctx));
}
static int
vn_ioctl(struct fileproc *fp, u_long com, caddr_t data, vfs_context_t ctx)
{
struct vnode *vp = ((struct vnode *)fp->f_fglob->fg_data);
off_t file_size;
int error;
struct vnode *ttyvp;
int funnel_state;
struct session * sessp;
if ( (error = vnode_getwithref(vp)) ) {
return(error);
}
#if CONFIG_MACF
error = mac_vnode_check_ioctl(ctx, vp, com);
if (error)
goto out;
#endif
switch (vp->v_type) {
case VREG:
case VDIR:
if (com == FIONREAD) {
if ((error = vnode_size(vp, &file_size, ctx)) != 0)
goto out;
*(int *)data = file_size - fp->f_fglob->fg_offset;
goto out;
}
if (com == FIONBIO || com == FIOASYNC) {
goto out;
}
default:
error = ENOTTY;
goto out;
case VFIFO:
case VCHR:
case VBLK:
if (com == DKIOCSETBLOCKSIZE) {
error = EPERM;
goto out;
}
if (com == FIODTYPE) {
if (vp->v_type == VBLK) {
if (major(vp->v_rdev) >= nblkdev) {
error = ENXIO;
goto out;
}
*(int *)data = bdevsw[major(vp->v_rdev)].d_type;
} else if (vp->v_type == VCHR) {
if (major(vp->v_rdev) >= nchrdev) {
error = ENXIO;
goto out;
}
*(int *)data = cdevsw[major(vp->v_rdev)].d_type;
} else {
error = ENOTTY;
goto out;
}
goto out;
}
error = VNOP_IOCTL(vp, com, data, fp->f_fglob->fg_flag, ctx);
if (error == 0 && com == TIOCSCTTY) {
vnode_ref(vp);
funnel_state = thread_funnel_set(kernel_flock, TRUE);
sessp = proc_session(vfs_context_proc(ctx));
session_lock(sessp);
ttyvp = sessp->s_ttyvp;
sessp->s_ttyvp = vp;
sessp->s_ttyvid = vnode_vid(vp);
session_unlock(sessp);
session_rele(sessp);
thread_funnel_set(kernel_flock, funnel_state);
if (ttyvp)
vnode_rele(ttyvp);
}
}
out:
(void)vnode_put(vp);
return(error);
}
static int
vn_select(struct fileproc *fp, int which, void *wql, __unused vfs_context_t ctx)
{
int error;
struct vnode * vp = (struct vnode *)fp->f_fglob->fg_data;
struct vfs_context context;
if ( (error = vnode_getwithref(vp)) == 0 ) {
context.vc_thread = current_thread();
context.vc_ucred = fp->f_fglob->fg_cred;
#if CONFIG_MACF
error = mac_vnode_check_select(ctx, vp, which);
if (error == 0)
#endif
error = VNOP_SELECT(vp, which, fp->f_fglob->fg_flag, wql, ctx);
(void)vnode_put(vp);
}
return(error);
}
static int
vn_closefile(struct fileglob *fg, vfs_context_t ctx)
{
struct vnode *vp = (struct vnode *)fg->fg_data;
int error;
struct flock lf;
if ( (error = vnode_getwithref(vp)) == 0 ) {
if ((fg->fg_flag & FHASLOCK) && fg->fg_type == DTYPE_VNODE) {
lf.l_whence = SEEK_SET;
lf.l_start = 0;
lf.l_len = 0;
lf.l_type = F_UNLCK;
(void)VNOP_ADVLOCK(vp, (caddr_t)fg, F_UNLCK, &lf, F_FLOCK, ctx);
}
error = vn_close(vp, fg->fg_flag, ctx);
(void)vnode_put(vp);
}
return(error);
}
int
vn_pathconf(vnode_t vp, int name, int32_t *retval, vfs_context_t ctx)
{
int error = 0;
switch(name) {
case _PC_EXTENDED_SECURITY_NP:
*retval = vfs_extendedsecurity(vnode_mount(vp)) ? 1 : 0;
break;
case _PC_AUTH_OPAQUE_NP:
*retval = vfs_authopaque(vnode_mount(vp));
break;
case _PC_2_SYMLINKS:
*retval = 1;
break;
case _PC_ALLOC_SIZE_MIN:
*retval = 1;
break;
case _PC_ASYNC_IO:
*retval = 1;
break;
case _PC_PRIO_IO:
*retval = 0;
break;
case _PC_REC_INCR_XFER_SIZE:
*retval = 4096;
break;
case _PC_REC_MIN_XFER_SIZE:
*retval = 4096;
break;
case _PC_REC_MAX_XFER_SIZE:
*retval = 65536;
break;
case _PC_REC_XFER_ALIGN:
*retval = 4096;
break;
case _PC_SYMLINK_MAX:
*retval = 255;
break;
case _PC_SYNC_IO:
*retval = 0;
break;
default:
error = VNOP_PATHCONF(vp, name, retval, ctx);
break;
}
return (error);
}
static int
vn_kqfilt_add(struct fileproc *fp, struct knote *kn, vfs_context_t ctx)
{
int error;
struct vnode *vp;
vp = (struct vnode *)fp->f_fglob->fg_data;
if ((error = vget_internal(vp, 0, VNODE_NODEAD)) == 0) {
switch (kn->kn_filter) {
case EVFILT_READ:
case EVFILT_WRITE:
if (vnode_isfifo(vp)) {
if (!(vp->v_fifoinfo && vp->v_fifoinfo->fi_readsock)) {
error = ENOTSUP;
}
} else if (!vnode_isreg(vp)) {
if (vnode_isspec(vp) &&
(error = spec_kqfilter(vp, kn)) == 0) {
vnode_put(vp);
return 0;
}
error = EINVAL;
}
break;
case EVFILT_VNODE:
break;
default:
error = EINVAL;
}
if (error) {
vnode_put(vp);
return error;
}
#if CONFIG_MACF
error = mac_vnode_check_kqfilter(ctx, fp->f_fglob->fg_cred, kn, vp);
if (error) {
vnode_put(vp);
return error;
}
#endif
kn->kn_hook = (void*)vp;
kn->kn_hookid = vnode_vid(vp);
kn->kn_fop = &vnode_filtops;
vnode_lock(vp);
KNOTE_ATTACH(&vp->v_knotes, kn);
vnode_unlock(vp);
VNOP_MONITOR(vp, 0, VNODE_MONITOR_BEGIN, (void*) kn, ctx);
vnode_put(vp);
}
return (error);
}
static void
filt_vndetach(struct knote *kn)
{
vfs_context_t ctx = vfs_context_current();
struct vnode *vp;
vp = (struct vnode *)kn->kn_hook;
if (vnode_getwithvid(vp, kn->kn_hookid))
return;
vnode_lock(vp);
KNOTE_DETACH(&vp->v_knotes, kn);
vnode_unlock(vp);
VNOP_MONITOR(vp, 0, VNODE_MONITOR_END, (void*)kn, ctx);
vnode_put(vp);
}
static intptr_t
vnode_readable_data_count(vnode_t vp, off_t current_offset, int ispoll)
{
if (vnode_isfifo(vp)) {
int cnt;
int err = fifo_charcount(vp, &cnt);
if (err == 0) {
return (intptr_t)cnt;
} else {
return (intptr_t)0;
}
} else if (vnode_isreg(vp)) {
if (ispoll) {
return (intptr_t)1;
}
off_t amount;
amount = vp->v_un.vu_ubcinfo->ui_size - current_offset;
if (amount > (off_t)INTPTR_MAX) {
return INTPTR_MAX;
} else if (amount < (off_t)INTPTR_MIN) {
return INTPTR_MIN;
} else {
return (intptr_t)amount;
}
} else {
panic("Should never have an EVFILT_READ except for reg or fifo.");
return 0;
}
}
static intptr_t
vnode_writable_space_count(vnode_t vp)
{
if (vnode_isfifo(vp)) {
long spc;
int err = fifo_freespace(vp, &spc);
if (err == 0) {
return (intptr_t)spc;
} else {
return (intptr_t)0;
}
} else if (vnode_isreg(vp)) {
return (intptr_t)1;
} else {
panic("Should never have an EVFILT_READ except for reg or fifo.");
return 0;
}
}
static int
filt_vnode(struct knote *kn, long hint)
{
struct vnode *vp = (struct vnode *)kn->kn_hook;
int activate = 0;
if (0 == hint) {
if ((vnode_getwithvid(vp, kn->kn_hookid) != 0)) {
hint = NOTE_REVOKE;
} else {
vnode_put(vp);
}
}
if (NOTE_REVOKE == hint) {
kn->kn_flags |= (EV_EOF | EV_ONESHOT);
activate = 1;
if ((kn->kn_filter == EVFILT_VNODE) && (kn->kn_sfflags & NOTE_REVOKE)) {
kn->kn_fflags |= NOTE_REVOKE;
}
} else {
switch(kn->kn_filter) {
case EVFILT_READ:
kn->kn_data = vnode_readable_data_count(vp, kn->kn_fp->f_fglob->fg_offset, (kn->kn_flags & EV_POLL));
if (kn->kn_data != 0) {
activate = 1;
}
break;
case EVFILT_WRITE:
kn->kn_data = vnode_writable_space_count(vp);
if (kn->kn_data != 0) {
activate = 1;
}
break;
case EVFILT_VNODE:
if (kn->kn_sfflags & hint) {
kn->kn_fflags |= hint;
}
if (kn->kn_fflags != 0) {
activate = 1;
}
break;
default:
panic("Invalid knote filter on a vnode!\n");
}
}
return (activate);
}