int _kvm_stat_ntfs(kvm_t *kd, struct kinfo_file *kf, struct vnode *vp) { struct ntnode ntnode; struct fnode fn; struct ntfsmount ntm; /* * To get the ntnode, we have to go in two steps - firstly * to read appropriate struct fnode and then getting the address * of ntnode and reading it's contents */ if (KREAD(kd, (u_long)VTOF(vp), &fn)) { _kvm_err(kd, kd->program, "can't read fnode at %p", VTOF(vp)); return (-1); } if (KREAD(kd, (u_long)FTONT(&fn), &ntnode)) { _kvm_err(kd, kd->program, "can't read ntnode at %p", FTONT(&fn)); return (-1); } if (KREAD(kd, (u_long)ntnode.i_mp, &ntm)) { _kvm_err(kd, kd->program, "can't read ntfsmount at %p", ntnode.i_mp); return (-1); } kf->va_fsid = ntnode.i_dev & 0xffff; kf->va_fileid = (long)ntnode.i_number; kf->va_mode = (mode_t)ntm.ntm_mode | _kvm_getftype(vp->v_type); kf->va_size = fn.f_size; kf->va_rdev = 0; /* XXX */ return (0); }
/* * Reclaim an fnode/ntnode so that it can be used for other purposes. */ int ntfs_reclaim(void *v) { struct vop_reclaim_args /* { struct vnode *a_vp; } */ *ap = v; struct vnode *vp = ap->a_vp; struct fnode *fp = VTOF(vp); struct ntnode *ip = FTONT(fp); int error; dprintf(("ntfs_reclaim: vnode: %p, ntnode: %llu\n", vp, (unsigned long long)ip->i_number)); if (prtactive && vp->v_usecount > 1) vprint("ntfs_reclaim: pushing active", vp); if ((error = ntfs_ntget(ip)) != 0) return (error); if (ip->i_devvp) { vrele(ip->i_devvp); ip->i_devvp = NULL; } genfs_node_destroy(vp); ntfs_frele(fp); ntfs_ntput(ip); vp->v_data = NULL; return (0); }
static int ntfs_getattr(void *v) { struct vop_getattr_args /* { struct vnode *a_vp; struct vattr *a_vap; kauth_cred_t a_cred; } */ *ap = v; struct vnode *vp = ap->a_vp; struct fnode *fp = VTOF(vp); struct ntnode *ip = FTONT(fp); struct vattr *vap = ap->a_vap; dprintf(("ntfs_getattr: %llu, flags: %d\n", (unsigned long long)ip->i_number, ip->i_flag)); vap->va_fsid = ip->i_dev; vap->va_fileid = ip->i_number; vap->va_mode = ip->i_mp->ntm_mode; vap->va_nlink = ip->i_nlink; vap->va_uid = ip->i_mp->ntm_uid; vap->va_gid = ip->i_mp->ntm_gid; vap->va_rdev = 0; /* XXX UNODEV ? */ vap->va_size = fp->f_size; vap->va_bytes = fp->f_allocated; vap->va_atime = ntfs_nttimetounix(fp->f_times.t_access); vap->va_mtime = ntfs_nttimetounix(fp->f_times.t_write); vap->va_ctime = ntfs_nttimetounix(fp->f_times.t_create); vap->va_flags = ip->i_flag; vap->va_gen = 0; vap->va_blocksize = ip->i_mp->ntm_spc * ip->i_mp->ntm_bps; vap->va_type = vp->v_type; vap->va_filerev = 0; return (0); }
static int ntfs_write(void *v) { struct vop_write_args *ap = v; struct vnode *vp = ap->a_vp; struct fnode *fp = VTOF(vp); struct ntnode *ip = FTONT(fp); struct uio *uio = ap->a_uio; struct ntfsmount *ntmp = ip->i_mp; u_int64_t towrite; size_t written; int error; dprintf(("ntfs_write: ino: %d, off: %d resid: %d, segflg: %d\n",ip->i_number,(u_int32_t)uio->uio_offset,uio->uio_resid,uio->uio_segflg)); dprintf(("ntfs_write: filesize: %d",(u_int32_t)fp->f_size)); if (uio->uio_resid + uio->uio_offset > fp->f_size) { printf("ntfs_write: CAN'T WRITE BEYOND END OF FILE\n"); return (EFBIG); } towrite = MIN(uio->uio_resid, fp->f_size - uio->uio_offset); dprintf((", towrite: %d\n",(u_int32_t)towrite)); error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype, fp->f_attrname, uio->uio_offset, towrite, NULL, &written, uio); #ifdef NTFS_DEBUG if (error) printf("ntfs_write: ntfs_writeattr failed: %d\n", error); #endif return (error); }
/* * Reclaim an fnode/ntnode so that it can be used for other purposes. */ int ntfs_reclaim(void *v) { struct vop_reclaim_args *ap = v; struct vnode *vp = ap->a_vp; struct fnode *fp = VTOF(vp); struct ntnode *ip = FTONT(fp); struct proc *p = ap->a_p; int error; dprintf(("ntfs_reclaim: vnode: %p, ntnode: %d\n", vp, ip->i_number)); #ifdef DIAGNOSTIC if (ntfs_prtactive && vp->v_usecount != 0) vprint("ntfs_reclaim: pushing active", vp); #endif if ((error = ntfs_ntget(ip, p)) != 0) return (error); /* Purge old data structures associated with the inode. */ cache_purge(vp); ntfs_frele(fp); ntfs_ntput(ip, p); vp->v_data = NULL; return (0); }
static int ntfs_read(void *v) { struct vop_read_args /* { struct vnode *a_vp; struct uio *a_uio; int a_ioflag; kauth_cred_t a_cred; } */ *ap = v; struct vnode *vp = ap->a_vp; struct fnode *fp = VTOF(vp); struct ntnode *ip = FTONT(fp); struct uio *uio = ap->a_uio; struct ntfsmount *ntmp = ip->i_mp; u_int64_t toread; int error; dprintf(("ntfs_read: ino: %llu, off: %qd resid: %qd\n", (unsigned long long)ip->i_number, (long long)uio->uio_offset, (long long)uio->uio_resid)); dprintf(("ntfs_read: filesize: %qu",(long long)fp->f_size)); /* don't allow reading after end of file */ if (uio->uio_offset > fp->f_size) toread = 0; else toread = MIN(uio->uio_resid, fp->f_size - uio->uio_offset ); dprintf((", toread: %qu\n",(long long)toread)); if (toread == 0) return (0); error = ntfs_readattr(ntmp, ip, fp->f_attrtype, fp->f_attrname, uio->uio_offset, toread, NULL, uio); if (error) { printf("ntfs_read: ntfs_readattr failed: %d\n",error); return (error); } return (0); }
/* * Reclaim an fnode/ntnode so that it can be used for other purposes. */ int ntfs_reclaim(void *v) { struct vop_reclaim_args /* { struct vnode *a_vp; } */ *ap = v; struct vnode *vp = ap->a_vp; struct fnode *fp = VTOF(vp); struct ntnode *ip = FTONT(fp); const int attrlen = strlen(fp->f_attrname); int error; dprintf(("ntfs_reclaim: vnode: %p, ntnode: %llu\n", vp, (unsigned long long)ip->i_number)); if (prtactive && vp->v_usecount > 1) vprint("ntfs_reclaim: pushing active", vp); if ((error = ntfs_ntget(ip)) != 0) return (error); vcache_remove(vp->v_mount, fp->f_key, NTKEY_SIZE(attrlen)); if (ip->i_devvp) { vrele(ip->i_devvp); ip->i_devvp = NULL; } genfs_node_destroy(vp); vp->v_data = NULL; /* Destroy fnode. */ if (fp->f_key != &fp->f_smallkey) kmem_free(fp->f_key, NTKEY_SIZE(attrlen)); if (fp->f_dirblbuf) free(fp->f_dirblbuf, M_NTFSDIR); kmem_free(fp, sizeof(*fp)); ntfs_ntrele(ip); ntfs_ntput(ip); return (0); }
static int ntfs_write(void *v) { struct vop_write_args /* { struct vnode *a_vp; struct uio *a_uio; int a_ioflag; kauth_cred_t a_cred; } */ *ap = v; struct vnode *vp = ap->a_vp; struct fnode *fp = VTOF(vp); struct ntnode *ip = FTONT(fp); struct uio *uio = ap->a_uio; struct ntfsmount *ntmp = ip->i_mp; u_int64_t towrite; size_t written; int error; dprintf(("ntfs_write: ino: %llu, off: %qd resid: %qd\n", (unsigned long long)ip->i_number, (long long)uio->uio_offset, (long long)uio->uio_resid)); dprintf(("ntfs_write: filesize: %qu",(long long)fp->f_size)); if (uio->uio_resid + uio->uio_offset > fp->f_size) { printf("ntfs_write: CAN'T WRITE BEYOND END OF FILE\n"); return (EFBIG); } towrite = MIN(uio->uio_resid, fp->f_size - uio->uio_offset); dprintf((", towrite: %qu\n",(long long)towrite)); error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype, fp->f_attrname, uio->uio_offset, towrite, NULL, &written, uio); #ifdef NTFS_DEBUG if (error) printf("ntfs_write: ntfs_writeattr failed: %d\n", error); #endif return (error); }
int ntfs_getattr(void *v) { struct vop_getattr_args *ap = v; struct vnode *vp = ap->a_vp; struct fnode *fp = VTOF(vp); struct ntnode *ip = FTONT(fp); struct vattr *vap = ap->a_vap; DPRINTF("ntfs_getattr: %u, flags: %u\n", ip->i_number, ip->i_flag); vap->va_fsid = ip->i_dev; vap->va_fileid = ip->i_number; vap->va_mode = ip->i_mp->ntm_mode; vap->va_nlink = ip->i_nlink; vap->va_uid = ip->i_mp->ntm_uid; vap->va_gid = ip->i_mp->ntm_gid; vap->va_rdev = 0; /* XXX UNODEV ? */ vap->va_size = fp->f_size; vap->va_bytes = fp->f_allocated; vap->va_atime = ntfs_nttimetounix(fp->f_times.t_access); vap->va_mtime = ntfs_nttimetounix(fp->f_times.t_write); vap->va_ctime = ntfs_nttimetounix(fp->f_times.t_create); vap->va_flags = ip->i_flag; vap->va_gen = 0; vap->va_blocksize = ip->i_mp->ntm_spc * ip->i_mp->ntm_bps; vap->va_type = vp->v_type; vap->va_filerev = 0; /* * Ensure that a directory link count is always 1 so that things * like fts_read() do not try to be smart and end up skipping over * directories. Additionally, ip->i_nlink will not be initialised * until the ntnode has been loaded for the file. */ if (vp->v_type == VDIR || ip->i_nlink < 1) vap->va_nlink = 1; return (0); }
int ntfs_read(void *v) { struct vop_read_args *ap = v; struct vnode *vp = ap->a_vp; struct fnode *fp = VTOF(vp); struct ntnode *ip = FTONT(fp); struct uio *uio = ap->a_uio; struct ntfsmount *ntmp = ip->i_mp; u_int64_t toread; int error; DPRINTF("ntfs_read: ino: %u, off: %lld resid: %zu, segflg: %d\n", ip->i_number, uio->uio_offset, uio->uio_resid, uio->uio_segflg); DPRINTF("ntfs_read: filesize: %llu", fp->f_size); /* don't allow reading after end of file */ if (uio->uio_offset > fp->f_size) toread = 0; else toread = MIN(uio->uio_resid, fp->f_size - uio->uio_offset); DPRINTF(", toread: %llu\n", toread); if (toread == 0) return (0); error = ntfs_readattr(ntmp, ip, fp->f_attrtype, fp->f_attrname, uio->uio_offset, toread, NULL, uio); if (error) { printf("ntfs_read: ntfs_readattr failed: %d\n",error); return (error); } return (0); }
int ntfs_readdir(void *v) { struct vop_readdir_args /* { struct vnode *a_vp; struct uio *a_uio; kauth_cred_t a_cred; int *a_ncookies; u_int **cookies; } */ *ap = v; struct vnode *vp = ap->a_vp; struct fnode *fp = VTOF(vp); struct ntnode *ip = FTONT(fp); struct uio *uio = ap->a_uio; struct ntfsmount *ntmp = ip->i_mp; int i, error = 0; u_int32_t faked = 0, num; int ncookies = 0; struct dirent *cde; off_t off; dprintf(("ntfs_readdir %llu off: %qd resid: %qd\n", (unsigned long long)ip->i_number, (long long)uio->uio_offset, (long long)uio->uio_resid)); off = uio->uio_offset; cde = malloc(sizeof(struct dirent), M_TEMP, M_WAITOK); /* Simulate . in every dir except ROOT */ if (ip->i_number != NTFS_ROOTINO && uio->uio_offset < sizeof(struct dirent)) { cde->d_fileno = ip->i_number; cde->d_reclen = sizeof(struct dirent); cde->d_type = DT_DIR; cde->d_namlen = 1; strncpy(cde->d_name, ".", 2); error = uiomove((void *)cde, sizeof(struct dirent), uio); if (error) goto out; ncookies++; } /* Simulate .. in every dir including ROOT */ if (uio->uio_offset < 2 * sizeof(struct dirent)) { cde->d_fileno = NTFS_ROOTINO; /* XXX */ cde->d_reclen = sizeof(struct dirent); cde->d_type = DT_DIR; cde->d_namlen = 2; strncpy(cde->d_name, "..", 3); error = uiomove((void *) cde, sizeof(struct dirent), uio); if (error) goto out; ncookies++; } faked = (ip->i_number == NTFS_ROOTINO) ? 1 : 2; num = uio->uio_offset / sizeof(struct dirent) - faked; while (uio->uio_resid >= sizeof(struct dirent)) { struct attr_indexentry *iep; char *fname; size_t remains; int sz; error = ntfs_ntreaddir(ntmp, fp, num, &iep); if (error) goto out; if (NULL == iep) break; for(; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (uio->uio_resid >= sizeof(struct dirent)); iep = NTFS_NEXTREC(iep, struct attr_indexentry *)) { if(!ntfs_isnamepermitted(ntmp,iep)) continue; remains = sizeof(cde->d_name) - 1; fname = cde->d_name; for(i=0; i<iep->ie_fnamelen; i++) { sz = (*ntmp->ntm_wput)(fname, remains, iep->ie_fname[i]); fname += sz; remains -= sz; } *fname = '\0'; dprintf(("ntfs_readdir: elem: %d, fname:[%s] type: %d, flag: %d, ", num, cde->d_name, iep->ie_fnametype, iep->ie_flag)); cde->d_namlen = fname - (char *) cde->d_name; cde->d_fileno = iep->ie_number; cde->d_type = (iep->ie_fflag & NTFS_FFLAG_DIR) ? DT_DIR : DT_REG; cde->d_reclen = sizeof(struct dirent); dprintf(("%s\n", (cde->d_type == DT_DIR) ? "dir":"reg")); error = uiomove((void *)cde, sizeof(struct dirent), uio); if (error) goto out; ncookies++; num++; } } dprintf(("ntfs_readdir: %d entries (%d bytes) read\n", ncookies,(u_int)(uio->uio_offset - off))); dprintf(("ntfs_readdir: off: %qd resid: %qu\n", (long long)uio->uio_offset,(long long)uio->uio_resid)); if (!error && ap->a_ncookies != NULL) { struct dirent* dpStart; struct dirent* dp; off_t *cookies; off_t *cookiep; dprintf(("ntfs_readdir: %d cookies\n",ncookies)); dpStart = (struct dirent *) ((char *)uio->uio_iov->iov_base - (uio->uio_offset - off)); cookies = malloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK); for (dp = dpStart, cookiep = cookies, i=0; i < ncookies; dp = (struct dirent *)((char *) dp + dp->d_reclen), i++) { off += dp->d_reclen; *cookiep++ = (u_int) off; } *ap->a_ncookies = ncookies; *ap->a_cookies = cookies; } /* if (ap->a_eofflag) *ap->a_eofflag = VTONT(ap->a_vp)->i_size <= uio->uio_offset; */ out: free(cde, M_TEMP); return (error); }
/* * Calculate the logical to physical mapping if not done already, * then call the device strategy routine. */ int ntfs_strategy(void *v) { struct vop_strategy_args /* { struct vnode *a_vp; struct buf *a_bp; } */ *ap = v; struct buf *bp = ap->a_bp; struct vnode *vp = ap->a_vp; struct fnode *fp = VTOF(vp); struct ntnode *ip = FTONT(fp); struct ntfsmount *ntmp = ip->i_mp; int error; dprintf(("ntfs_strategy: blkno: %d, lblkno: %d\n", (u_int32_t)bp->b_blkno, (u_int32_t)bp->b_lblkno)); dprintf(("strategy: bcount: %u flags: 0x%x\n", (u_int32_t)bp->b_bcount,bp->b_flags)); if (bp->b_flags & B_READ) { u_int32_t toread; if (ntfs_cntob(bp->b_blkno) >= fp->f_size) { clrbuf(bp); error = 0; } else { toread = MIN(bp->b_bcount, fp->f_size - ntfs_cntob(bp->b_blkno)); dprintf(("ntfs_strategy: toread: %d, fsize: %d\n", toread,(u_int32_t)fp->f_size)); error = ntfs_readattr(ntmp, ip, fp->f_attrtype, fp->f_attrname, ntfs_cntob(bp->b_blkno), toread, bp->b_data, NULL); if (error) { printf("ntfs_strategy: ntfs_readattr failed\n"); bp->b_error = error; } memset((char *)bp->b_data + toread, 0, bp->b_bcount - toread); } } else { size_t tmp; u_int32_t towrite; if (ntfs_cntob(bp->b_blkno) + bp->b_bcount >= fp->f_size) { printf("ntfs_strategy: CAN'T EXTEND FILE\n"); bp->b_error = error = EFBIG; } else { towrite = MIN(bp->b_bcount, fp->f_size - ntfs_cntob(bp->b_blkno)); dprintf(("ntfs_strategy: towrite: %d, fsize: %d\n", towrite,(u_int32_t)fp->f_size)); error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype, fp->f_attrname, ntfs_cntob(bp->b_blkno),towrite, bp->b_data, &tmp, NULL); if (error) { printf("ntfs_strategy: ntfs_writeattr fail\n"); bp->b_error = error; } } } biodone(bp); return (error); }
int ntfs_readdir(void *v) { struct vop_readdir_args *ap = v; struct vnode *vp = ap->a_vp; struct fnode *fp = VTOF(vp); struct ntnode *ip = FTONT(fp); struct uio *uio = ap->a_uio; struct ntfsmount *ntmp = ip->i_mp; int i, error = 0; u_int32_t faked = 0, num; struct dirent cde; off_t off; DPRINTF("ntfs_readdir %u off: %lld resid: %zu\n", ip->i_number, uio->uio_offset, uio->uio_resid); off = uio->uio_offset; memset(&cde, 0, sizeof(cde)); /* Simulate . in every dir except ROOT */ if (ip->i_number != NTFS_ROOTINO && uio->uio_offset == 0) { cde.d_fileno = ip->i_number; cde.d_reclen = sizeof(struct dirent); cde.d_type = DT_DIR; cde.d_namlen = 1; cde.d_off = sizeof(struct dirent); cde.d_name[0] = '.'; cde.d_name[1] = '\0'; error = uiomove(&cde, sizeof(struct dirent), uio); if (error) goto out; } /* Simulate .. in every dir including ROOT */ if (uio->uio_offset < 2 * sizeof(struct dirent)) { cde.d_fileno = NTFS_ROOTINO; /* XXX */ cde.d_reclen = sizeof(struct dirent); cde.d_type = DT_DIR; cde.d_namlen = 2; cde.d_off = 2 * sizeof(struct dirent); cde.d_name[0] = '.'; cde.d_name[1] = '.'; cde.d_name[2] = '\0'; error = uiomove(&cde, sizeof(struct dirent), uio); if (error) goto out; } faked = (ip->i_number == NTFS_ROOTINO) ? 1 : 2; num = uio->uio_offset / sizeof(struct dirent) - faked; while (uio->uio_resid >= sizeof(struct dirent)) { struct attr_indexentry *iep; char *fname; size_t remains; int sz; error = ntfs_ntreaddir(ntmp, fp, num, &iep, uio->uio_procp); if (error) goto out; if (NULL == iep) break; for(; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (uio->uio_resid >= sizeof(struct dirent)); iep = NTFS_NEXTREC(iep, struct attr_indexentry *)) { if(!ntfs_isnamepermitted(ntmp,iep)) continue; remains = sizeof(cde.d_name) - 1; fname = cde.d_name; for(i=0; i<iep->ie_fnamelen; i++) { sz = (*ntmp->ntm_wput)(fname, remains, iep->ie_fname[i]); fname += sz; remains -= sz; } *fname = '\0'; DPRINTF("ntfs_readdir: elem: %u, fname:[%s] type: %u, " "flag: %u, ", num, cde.d_name, iep->ie_fnametype, iep->ie_flag); cde.d_namlen = fname - (char *) cde.d_name; cde.d_fileno = iep->ie_number; cde.d_type = (iep->ie_fflag & NTFS_FFLAG_DIR) ? DT_DIR : DT_REG; cde.d_reclen = sizeof(struct dirent); cde.d_off = uio->uio_offset + sizeof(struct dirent); DPRINTF("%s\n", cde.d_type == DT_DIR ? "dir" : "reg"); error = uiomove(&cde, sizeof(struct dirent), uio); if (error) goto out; num++; } } DPRINTF("ntfs_readdir: %u entries (%lld bytes) read\n", num, uio->uio_offset - off); DPRINTF("ntfs_readdir: off: %lld resid: %zu\n", uio->uio_offset, uio->uio_resid); /* if (ap->a_eofflag) *ap->a_eofflag = VTONT(ap->a_vp)->i_size <= uio->uio_offset; */ out: if (fp->f_dirblbuf != NULL) { free(fp->f_dirblbuf, M_NTFSDIR, 0); fp->f_dirblbuf = NULL; } return (error); }
/* * Calculate the logical to physical mapping if not done already, * then call the device strategy routine. */ int ntfs_strategy(void *v) { struct vop_strategy_args *ap = v; struct buf *bp = ap->a_bp; struct vnode *vp = bp->b_vp; struct fnode *fp = VTOF(vp); struct ntnode *ip = FTONT(fp); struct ntfsmount *ntmp = ip->i_mp; int error, s; DPRINTF("ntfs_strategy: blkno: %lld, lblkno: %lld\n", (long long)bp->b_blkno, (long long)bp->b_lblkno); DPRINTF("strategy: bcount: %ld flags: 0x%lx\n", bp->b_bcount, bp->b_flags); if (bp->b_flags & B_READ) { u_int32_t toread; if (ntfs_cntob(bp->b_blkno) >= fp->f_size) { clrbuf(bp); error = 0; } else { toread = MIN(bp->b_bcount, fp->f_size - ntfs_cntob(bp->b_blkno)); DPRINTF("ntfs_strategy: toread: %u, fsize: %llu\n", toread, fp->f_size); error = ntfs_readattr(ntmp, ip, fp->f_attrtype, fp->f_attrname, ntfs_cntob(bp->b_blkno), toread, bp->b_data, NULL); if (error) { printf("ntfs_strategy: ntfs_readattr failed\n"); bp->b_error = error; bp->b_flags |= B_ERROR; } bzero(bp->b_data + toread, bp->b_bcount - toread); } } else { size_t tmp; u_int32_t towrite; if (ntfs_cntob(bp->b_blkno) + bp->b_bcount >= fp->f_size) { printf("ntfs_strategy: CAN'T EXTEND FILE\n"); bp->b_error = error = EFBIG; bp->b_flags |= B_ERROR; } else { towrite = MIN(bp->b_bcount, fp->f_size - ntfs_cntob(bp->b_blkno)); DPRINTF("ntfs_strategy: towrite: %u, fsize: %llu\n", towrite, fp->f_size); error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype, fp->f_attrname, ntfs_cntob(bp->b_blkno),towrite, bp->b_data, &tmp, NULL); if (error) { printf("ntfs_strategy: ntfs_writeattr fail\n"); bp->b_error = error; bp->b_flags |= B_ERROR; } } } s = splbio(); biodone(bp); splx(s); return (error); }