int compat_20_netbsd32_fstatfs(struct lwp *l, const struct compat_20_netbsd32_fstatfs_args *uap, register_t *retval) { /* { syscallarg(int) fd; syscallarg(netbsd32_statfsp_t) buf; } */ file_t *fp; struct mount *mp; struct statvfs *sp; struct netbsd32_statfs s32; int error; /* fd_getvnode() will use the descriptor for us */ if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0) return (error); mp = ((struct vnode *)fp->f_data)->v_mount; sp = &mp->mnt_stat; if ((error = VFS_STATVFS(mp, sp)) != 0) goto out; sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK; compat_20_netbsd32_from_statvfs(sp, &s32); error = copyout(&s32, SCARG_P32(uap, buf), sizeof(s32)); out: fd_putfile(SCARG(uap, fd)); return (error); }
/* * Read a block of directory entries in a file system independent format. */ int compat_12_sys_getdirentries(struct lwp *l, const struct compat_12_sys_getdirentries_args *uap, register_t *retval) { /* { syscallarg(int) fd; syscallarg(char *) buf; syscallarg(u_int) count; syscallarg(long *) basep; } */ struct file *fp; int error, done; long loff; /* fd_getvnode() will use the descriptor for us */ if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0) return error; if ((fp->f_flag & FREAD) == 0) { error = EBADF; goto out; } loff = fp->f_offset; error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE, SCARG(uap, count), &done, l, 0, 0); error = copyout(&loff, SCARG(uap, basep), sizeof(long)); *retval = done; out: fd_putfile(SCARG(uap, fd)); return error; }
int compat_30_netbsd32_getdents(struct lwp *l, const struct compat_30_netbsd32_getdents_args *uap, register_t *retval) { /* { syscallarg(int) fd; syscallarg(netbsd32_charp) buf; syscallarg(netbsd32_size_t) count; } */ file_t *fp; int error, done; char *buf; netbsd32_size_t count; /* Limit the size on any kernel buffers used by VOP_READDIR */ count = min(MAXBSIZE, SCARG(uap, count)); /* fd_getvnode() will use the descriptor for us */ if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0) return (error); if ((fp->f_flag & FREAD) == 0) { error = EBADF; goto out; } buf = kmem_alloc(count, KM_SLEEP); error = vn_readdir(fp, buf, UIO_SYSSPACE, count, &done, l, 0, 0); if (error == 0) { *retval = netbsd32_to_dirent12(buf, done); error = copyout(buf, SCARG_P32(uap, buf), *retval); } kmem_free(buf, count); out: fd_putfile(SCARG(uap, fd)); return (error); }
int compat_50_netbsd32_futimes(struct lwp *l, const struct compat_50_netbsd32_futimes_args *uap, register_t *retval) { /* { syscallarg(int) fd; syscallarg(const netbsd32_timeval50p_t) tptr; } */ int error; file_t *fp; struct timeval tv[2], *tvp; error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp); if (error != 0) return error; /* fd_getvnode() will use the descriptor for us */ if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0) return error; error = do_sys_utimes(l, fp->f_data, NULL, 0, tvp, UIO_SYSSPACE); fd_putfile(SCARG(uap, fd)); return error; }
int ibcs2_sys_fstatvfs(struct lwp *l, const struct ibcs2_sys_fstatvfs_args *uap, register_t *retval) { /* { syscallarg(int) fd; syscallarg(struct ibcs2_statvfs *) buf; } */ file_t *fp; struct mount *mp; struct statvfs *sp; int error; /* fd_getvnode() will use the descriptor for us */ if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0) return (error); mp = ((struct vnode *)fp->f_data)->v_mount; sp = &mp->mnt_stat; if ((error = VFS_STATVFS(mp, sp)) != 0) goto out; sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK; error = cvt_statvfs(sp, SCARG(uap, buf), sizeof(struct ibcs2_statvfs)); out: fd_putfile(SCARG(uap, fd)); return (error); }
int darwin_sys_ioctl(struct lwp *l, const struct darwin_sys_ioctl_args *uap, register_t *retval) { /* { syscallarg(int) fd; syscallarg(u_long) com; syscallarg(void *) data; } */ struct sys_ioctl_args cup; int error; switch (SCARG(uap, com)) { case DARWIN_FIODTYPE: { /* Get file d_type */ file_t *fp; struct vnode *vp; int *data = SCARG(uap, data); int type; /* fd_getvnode() will use the descriptor for us */ if ((error = fd_getvnode(SCARG(uap, fd), &fp))) return (error); vp = fp->f_data; type = vtype_to_dtype(vp->v_type); fd_putfile(SCARG(uap, fd)); error = copyout(&type, data, sizeof(*data)); return error; break; } default: /* Try native ioctl */ break; } SCARG(&cup, fd) = SCARG(uap, fd); SCARG(&cup, com) = SCARG(uap, com); SCARG(&cup, data) = SCARG(uap, data); error = sys_ioctl(l, &cup, retval); return error; }
/* * sys_fstatfs() takes an fd, not a path, and so needs no emul * pathname processing; but it's similar enough to sys_statvfs() that * it goes here anyway. */ int ultrix_sys_fstatfs(struct lwp *l, const struct ultrix_sys_fstatfs_args *uap, register_t *retval) { file_t *fp; struct mount *mp; struct statvfs *sp; int error; /* fd_getvnode() will use the descriptor for us */ if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0) return error; mp = ((struct vnode *)fp->f_data)->v_mount; sp = &mp->mnt_stat; if ((error = VFS_STATVFS(mp, sp)) != 0) goto out; sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK; error = ultrixstatfs(sp, (void *)SCARG(uap, buf)); out: fd_putfile(SCARG(uap, fd)); return error; }
/* * Read a block of directory entries in a file system independent format. */ int compat_43_sys_getdirentries(struct lwp *l, const struct compat_43_sys_getdirentries_args *uap, register_t *retval) { /* { syscallarg(int) fd; syscallarg(char *) buf; syscallarg(u_int) count; syscallarg(long *) basep; } */ struct dirent *bdp; struct vnode *vp; char *inp, *tbuf; /* Current-format */ int len, reclen; /* Current-format */ char *outp; /* Dirent12-format */ int resid, old_reclen = 0; /* Dirent12-format */ struct file *fp; struct uio auio; struct iovec aiov; struct dirent43 idb; off_t off; /* true file offset */ int buflen, error, eofflag, nbytes; struct vattr va; off_t *cookiebuf = NULL, *cookie; int ncookies; long loff; /* fd_getvnode() will use the descriptor for us */ if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0) return (error); if ((fp->f_flag & FREAD) == 0) { error = EBADF; goto out1; } vp = (struct vnode *)fp->f_data; if (vp->v_type != VDIR) { error = ENOTDIR; goto out1; } vn_lock(vp, LK_SHARED | LK_RETRY); error = VOP_GETATTR(vp, &va, l->l_cred); VOP_UNLOCK(vp); if (error) goto out1; loff = fp->f_offset; nbytes = SCARG(uap, count); buflen = min(MAXBSIZE, nbytes); if (buflen < va.va_blocksize) buflen = va.va_blocksize; tbuf = malloc(buflen, M_TEMP, M_WAITOK); vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); off = fp->f_offset; again: aiov.iov_base = tbuf; aiov.iov_len = buflen; auio.uio_iov = &aiov; auio.uio_iovcnt = 1; auio.uio_rw = UIO_READ; auio.uio_resid = buflen; auio.uio_offset = off; UIO_SETUP_SYSSPACE(&auio); /* * First we read into the malloc'ed buffer, then * we massage it into user space, one record at a time. */ error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf, &ncookies); if (error) goto out; inp = tbuf; outp = SCARG(uap, buf); resid = nbytes; if ((len = buflen - auio.uio_resid) == 0) goto eof; for (cookie = cookiebuf; len > 0; len -= reclen) { bdp = (struct dirent *)inp; reclen = bdp->d_reclen; if (reclen & 3) panic(__func__); if (bdp->d_fileno == 0) { inp += reclen; /* it is a hole; squish it out */ if (cookie) off = *cookie++; else off += reclen; continue; } old_reclen = _DIRENT_RECLEN(&idb, bdp->d_namlen); if (reclen > len || resid < old_reclen) { /* entry too big for buffer, so just stop */ outp++; break; } /* * Massage in place to make a Dirent12-shaped dirent (otherwise * we have to worry about touching user memory outside of * the copyout() call). */ idb.d_fileno = (uint32_t)bdp->d_fileno; idb.d_reclen = (uint16_t)old_reclen; idb.d_namlen = (uint16_t)bdp->d_namlen; strcpy(idb.d_name, bdp->d_name); if ((error = copyout(&idb, outp, old_reclen))) goto out; /* advance past this real entry */ inp += reclen; if (cookie) off = *cookie++; /* each entry points to itself */ else off += reclen; /* advance output past Dirent12-shaped entry */ outp += old_reclen; resid -= old_reclen; } /* if we squished out the whole block, try again */ if (outp == SCARG(uap, buf)) { if (cookiebuf) free(cookiebuf, M_TEMP); cookiebuf = NULL; goto again; } fp->f_offset = off; /* update the vnode offset */ eof: *retval = nbytes - resid; out: VOP_UNLOCK(vp); if (cookiebuf) free(cookiebuf, M_TEMP); free(tbuf, M_TEMP); out1: fd_putfile(SCARG(uap, fd)); if (error) return error; return copyout(&loff, SCARG(uap, basep), sizeof(long)); }
/* * Linux 'readdir' call. This code is mostly taken from the * SunOS getdents call (see compat/sunos/sunos_misc.c), though * an attempt has been made to keep it a little cleaner. * * The d_off field contains the offset of the next valid entry, * unless the older Linux getdents(2), which used to have it set * to the offset of the entry itself. This function also doesn't * need to deal with the old count == 1 glibc problem. * * Read in BSD-style entries, convert them, and copy them out. * * Note that this doesn't handle union-mounted filesystems. */ int linux_sys_getdents64(struct lwp *l, const struct linux_sys_getdents64_args *uap, register_t *retval) { /* { syscallarg(int) fd; syscallarg(struct linux_dirent64 *) dent; syscallarg(unsigned int) count; } */ struct dirent *bdp; struct vnode *vp; char *inp, *tbuf; /* BSD-format */ int len, reclen; /* BSD-format */ char *outp; /* Linux-format */ int resid, linux_reclen = 0; /* Linux-format */ file_t *fp; struct uio auio; struct iovec aiov; struct linux_dirent64 idb; off_t off; /* true file offset */ int buflen, error, eofflag, nbytes; struct vattr va; off_t *cookiebuf = NULL, *cookie; int ncookies; /* fd_getvnode() will use the descriptor for us */ if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0) return (error); if ((fp->f_flag & FREAD) == 0) { error = EBADF; goto out1; } vp = (struct vnode *)fp->f_data; if (vp->v_type != VDIR) { error = EINVAL; goto out1; } if ((error = VOP_GETATTR(vp, &va, l->l_cred))) goto out1; nbytes = SCARG(uap, count); buflen = min(MAXBSIZE, nbytes); if (buflen < va.va_blocksize) buflen = va.va_blocksize; tbuf = malloc(buflen, M_TEMP, M_WAITOK); vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); off = fp->f_offset; again: aiov.iov_base = tbuf; aiov.iov_len = buflen; auio.uio_iov = &aiov; auio.uio_iovcnt = 1; auio.uio_rw = UIO_READ; auio.uio_resid = buflen; auio.uio_offset = off; UIO_SETUP_SYSSPACE(&auio); /* * First we read into the malloc'ed buffer, then * we massage it into user space, one record at a time. */ error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf, &ncookies); if (error) goto out; inp = tbuf; outp = (void *)SCARG(uap, dent); resid = nbytes; if ((len = buflen - auio.uio_resid) == 0) goto eof; for (cookie = cookiebuf; len > 0; len -= reclen) { bdp = (struct dirent *)inp; reclen = bdp->d_reclen; if (reclen & 3) panic("linux_readdir"); if (bdp->d_fileno == 0) { inp += reclen; /* it is a hole; squish it out */ if (cookie) off = *cookie++; else off += reclen; continue; } linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen); if (reclen > len || resid < linux_reclen) { /* entry too big for buffer, so just stop */ outp++; break; } if (cookie) off = *cookie++; /* each entry points to next */ else off += reclen; /* * Massage in place to make a Linux-shaped dirent (otherwise * we have to worry about touching user memory outside of * the copyout() call). */ idb.d_ino = bdp->d_fileno; idb.d_type = bdp->d_type; idb.d_off = off; idb.d_reclen = (u_short)linux_reclen; strcpy(idb.d_name, bdp->d_name); if ((error = copyout((void *)&idb, outp, linux_reclen))) goto out; /* advance past this real entry */ inp += reclen; /* advance output past Linux-shaped entry */ outp += linux_reclen; resid -= linux_reclen; } /* if we squished out the whole block, try again */ if (outp == (void *)SCARG(uap, dent)) { if (cookiebuf) free(cookiebuf, M_TEMP); cookiebuf = NULL; goto again; } fp->f_offset = off; /* update the vnode offset */ eof: *retval = nbytes - resid; out: VOP_UNLOCK(vp, 0); if (cookiebuf) free(cookiebuf, M_TEMP); free(tbuf, M_TEMP); out1: fd_putfile(SCARG(uap, fd)); return error; }
/* * Read a block of directory entries in a file system independent format. */ int compat_43_sys_getdirentries(struct lwp *l, const struct compat_43_sys_getdirentries_args *uap, register_t *retval) { /* { syscallarg(int) fd; syscallarg(char *) buf; syscallarg(u_int) count; syscallarg(long *) basep; } */ struct vnode *vp; struct file *fp; struct uio auio, kuio; struct iovec aiov, kiov; struct dirent *dp, *edp; char *dirbuf; size_t count = min(MAXBSIZE, (size_t)SCARG(uap, count)); int error, eofflag, readcnt; long loff; /* fd_getvnode() will use the descriptor for us */ if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0) return (error); if ((fp->f_flag & FREAD) == 0) { error = EBADF; goto out; } vp = (struct vnode *)fp->f_data; unionread: if (vp->v_type != VDIR) { error = EINVAL; goto out; } aiov.iov_base = SCARG(uap, buf); aiov.iov_len = count; auio.uio_iov = &aiov; auio.uio_iovcnt = 1; auio.uio_rw = UIO_READ; auio.uio_resid = count; KASSERT(l == curlwp); auio.uio_vmspace = curproc->p_vmspace; vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); loff = auio.uio_offset = fp->f_offset; # if (BYTE_ORDER != LITTLE_ENDIAN) if ((vp->v_mount->mnt_iflag & IMNT_DTYPE) == 0) { error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, (off_t **)0, (int *)0); fp->f_offset = auio.uio_offset; } else # endif { kuio = auio; kuio.uio_iov = &kiov; kiov.iov_len = count; dirbuf = malloc(count, M_TEMP, M_WAITOK); kiov.iov_base = dirbuf; UIO_SETUP_SYSSPACE(&kuio); error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag, (off_t **)0, (int *)0); fp->f_offset = kuio.uio_offset; if (error == 0) { readcnt = 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); if (error) goto out; if ((count == auio.uio_resid) && (vp->v_vflag & VV_ROOT) && (vp->v_mount->mnt_flag & MNT_UNION)) { struct vnode *tvp = vp; vp = vp->v_mount->mnt_vnodecovered; vref(vp); fp->f_data = (void *) vp; fp->f_offset = 0; vrele(tvp); goto unionread; } error = copyout((void *)&loff, (void *)SCARG(uap, basep), sizeof(long)); *retval = count - auio.uio_resid; out: fd_putfile(SCARG(uap, fd)); return (error); }