#include <sys/param.h>
#include <sys/systm.h>
#include <sys/uio.h>
#include <sys/kernel.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/vnode.h>
#include <sys/mount.h>
#include <sys/proc.h>
#include <sys/namei.h>
#include <sys/malloc.h>
#include <sys/buf.h>
#include <sys/dirent.h>
#include <machine/limits.h>
#include <vm/vm.h>
#include <vm/vm_param.h>
#include <vm/vm_page.h>
#include <vm/vm_object.h>
#include <vm/vm_pager.h>
#include <vm/vnode_pager.h>
#include <vm/vm_extern.h>
#include <sys/sysctl.h>
#include <sys/buf2.h>
#include "ntfs.h"
#include "ntfs_inode.h"
#include "ntfs_subr.h"
#include <sys/unistd.h>
static int ntfs_read (struct vop_read_args *);
static int ntfs_write (struct vop_write_args *ap);
static int ntfs_getattr (struct vop_getattr_args *ap);
static int ntfs_inactive (struct vop_inactive_args *ap);
static int ntfs_print (struct vop_print_args *ap);
static int ntfs_reclaim (struct vop_reclaim_args *ap);
static int ntfs_strategy (struct vop_strategy_args *ap);
static int ntfs_access (struct vop_access_args *ap);
static int ntfs_open (struct vop_open_args *ap);
static int ntfs_close (struct vop_close_args *ap);
static int ntfs_readdir (struct vop_readdir_args *ap);
static int ntfs_lookup (struct vop_old_lookup_args *ap);
static int ntfs_bmap (struct vop_bmap_args *ap);
static int ntfs_fsync (struct vop_fsync_args *ap);
static int ntfs_pathconf (struct vop_pathconf_args *);
int ntfs_prtactive = 1;
int
ntfs_bmap(struct vop_bmap_args *ap)
{
dprintf(("ntfs_bmap: vn: %p, blk: %u\n", ap->a_vp,
(u_int32_t)ap->a_loffset));
if (ap->a_doffsetp != NULL)
*ap->a_doffsetp = ap->a_loffset;
if (ap->a_runp != NULL)
*ap->a_runp = 0;
if (ap->a_runb != NULL)
*ap->a_runb = 0;
return (0);
}
static int
ntfs_read(struct vop_read_args *ap)
{
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;
struct buf *bp;
daddr_t cn;
int resid, off, toread;
int error;
dprintf(("ntfs_read: ino: %ju, off: %u resid: %zd, segflg: %d\n",
(uintmax_t)ip->i_number, (uint32_t)uio->uio_offset,
uio->uio_resid, uio->uio_segflg));
dprintf(("ntfs_read: filesize: %ju", (uintmax_t)fp->f_size));
if (uio->uio_offset > fp->f_size)
return (0);
resid = (int)szmin(uio->uio_resid, fp->f_size - uio->uio_offset);
dprintf((", resid: %d\n", resid));
error = 0;
while (resid) {
cn = ntfs_btocn(uio->uio_offset);
off = ntfs_btocnoff(uio->uio_offset);
toread = min(off + resid, ntfs_cntob(1));
error = bread(vp, ntfs_cntodoff(cn), ntfs_cntob(1), &bp);
if (error) {
brelse(bp);
break;
}
error = uiomovebp(bp, bp->b_data + off, toread - off, uio);
if(error) {
brelse(bp);
break;
}
brelse(bp);
resid -= toread - off;
}
return (error);
}
static int
ntfs_getattr(struct vop_getattr_args *ap)
{
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: %ju, flags: %d\n", (uintmax_t)ip->i_number,
ip->i_flag));
vap->va_fsid = devid_from_dev(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_rmajor = VNOVAL;
vap->va_rminor = VNOVAL;
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);
}
int
ntfs_inactive(struct vop_inactive_args *ap)
{
struct vnode *vp = ap->a_vp;
#ifdef NTFS_DEBUG
struct ntnode *ip = VTONT(vp);
#endif
dprintf(("ntfs_inactive: vnode: %p, ntnode: %ju\n", vp, (uintmax_t)ip->i_number));
if (ntfs_prtactive && VREFCNT(vp) > 1)
vprint("ntfs_inactive: pushing active", vp);
return (0);
}
int
ntfs_reclaim(struct vop_reclaim_args *ap)
{
struct vnode *vp = ap->a_vp;
struct fnode *fp = VTOF(vp);
struct ntnode *ip = FTONT(fp);
int error;
dprintf(("ntfs_reclaim: vnode: %p, ntnode: %ju\n", vp, (uintmax_t)ip->i_number));
if (ntfs_prtactive && VREFCNT(vp) > 1)
vprint("ntfs_reclaim: pushing active", vp);
if ((error = ntfs_ntget(ip)) != 0)
return (error);
ntfs_frele(fp);
ntfs_ntput(ip);
vp->v_data = NULL;
return (0);
}
static int
ntfs_print(struct vop_print_args *ap)
{
return (0);
}
int
ntfs_strategy(struct vop_strategy_args *ap)
{
struct bio *bio = ap->a_bio;
struct buf *bp = bio->bio_buf;
struct vnode *vp = ap->a_vp;
struct fnode *fp = VTOF(vp);
struct ntnode *ip = FTONT(fp);
struct ntfsmount *ntmp = ip->i_mp;
u_int32_t toread;
u_int32_t towrite;
size_t tmp;
int error;
dprintf(("ntfs_strategy: loffset: %u, doffset: %u\n",
(uint32_t)bp->b_loffset, (uint32_t)bio->bio_offset));
dprintf(("strategy: bcount: %u flags: 0x%x\n",
bp->b_bcount, bp->b_flags));
bp->b_error = 0;
switch(bp->b_cmd) {
case BUF_CMD_READ:
if (bio->bio_offset >= fp->f_size) {
clrbuf(bp);
error = 0;
} else {
toread = min(bp->b_bcount,
fp->f_size - bio->bio_offset);
dprintf(("ntfs_strategy: toread: %u, fsize: %ju\n",
toread, (uintmax_t)fp->f_size));
error = ntfs_readattr(ntmp, ip, fp->f_attrtype,
fp->f_attrname, bio->bio_offset,
toread, bp->b_data, NULL);
if (error) {
kprintf("ntfs_strategy: ntfs_readattr failed\n");
bp->b_error = error;
bp->b_flags |= B_ERROR;
}
bzero(bp->b_data + toread, bp->b_bcount - toread);
}
break;
case BUF_CMD_WRITE:
if (bio->bio_offset + bp->b_bcount >= fp->f_size) {
kprintf("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 - bio->bio_offset);
dprintf(("ntfs_strategy: towrite: %d, fsize: %ju\n",
towrite, (uintmax_t)fp->f_size));
error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype,
fp->f_attrname, bio->bio_offset,towrite,
bp->b_data, &tmp, NULL);
if (error) {
kprintf("ntfs_strategy: ntfs_writeattr fail\n");
bp->b_error = error;
bp->b_flags |= B_ERROR;
}
}
break;
default:
panic("ntfs: bad b_cmd %d", bp->b_cmd);
}
biodone(bio);
return (error);
}
static int
ntfs_write(struct vop_write_args *ap)
{
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;
size_t towrite;
size_t written;
int error;
dprintf(("ntfs_write: ino: %ju, off: %u resid: %zd, segflg: %d\n",
(uintmax_t)ip->i_number, (uint32_t)uio->uio_offset,
uio->uio_resid, uio->uio_segflg));
dprintf(("ntfs_write: filesize: %ju ", (uintmax_t)fp->f_size));
if (uio->uio_resid + uio->uio_offset > fp->f_size) {
kprintf("ntfs_write: CAN'T WRITE BEYOND END OF FILE\n");
return (EFBIG);
}
if (uio->uio_offset > fp->f_size)
return (EFBIG);
towrite = szmin(uio->uio_resid, fp->f_size - uio->uio_offset);
dprintf((", towrite: %zd\n", 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)
kprintf("ntfs_write: ntfs_writeattr failed: %d\n", error);
#endif
return (error);
}
int
ntfs_access(struct vop_access_args *ap)
{
struct vnode *vp = ap->a_vp;
struct ntnode *ip = VTONT(vp);
struct ucred *cred = ap->a_cred;
mode_t mask, mode = ap->a_mode;
gid_t *gp;
int i;
#ifdef QUOTA
int error;
#endif
dprintf(("ntfs_access: %ju\n", (uintmax_t)ip->i_number));
if (mode & VWRITE) {
switch ((int)vp->v_type) {
case VDIR:
case VLNK:
case VREG:
if (vp->v_mount->mnt_flag & MNT_RDONLY)
return (EROFS);
#ifdef QUOTA
if (error = getinoquota(ip))
return (error);
#endif
break;
}
}
if (cred->cr_uid == 0)
return (0);
mask = 0;
if (cred->cr_uid == ip->i_mp->ntm_uid) {
if (mode & VEXEC)
mask |= S_IXUSR;
if (mode & VREAD)
mask |= S_IRUSR;
if (mode & VWRITE)
mask |= S_IWUSR;
return ((ip->i_mp->ntm_mode & mask) == mask ? 0 : EACCES);
}
for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
if (ip->i_mp->ntm_gid == *gp) {
if (mode & VEXEC)
mask |= S_IXGRP;
if (mode & VREAD)
mask |= S_IRGRP;
if (mode & VWRITE)
mask |= S_IWGRP;
return ((ip->i_mp->ntm_mode&mask) == mask ? 0 : EACCES);
}
if (mode & VEXEC)
mask |= S_IXOTH;
if (mode & VREAD)
mask |= S_IROTH;
if (mode & VWRITE)
mask |= S_IWOTH;
return ((ip->i_mp->ntm_mode & mask) == mask ? 0 : EACCES);
}
static int
ntfs_open(struct vop_open_args *ap)
{
return (vop_stdopen(ap));
}
static int
ntfs_close(struct vop_close_args *ap)
{
#ifdef NTFS_DEBUG
struct vnode *vp = ap->a_vp;
struct ntnode *ip = VTONT(vp);
kprintf("ntfs_close: %ju\n", (uintmax_t)ip->i_number);
#endif
return (vop_stdclose(ap));
}
int
ntfs_readdir(struct vop_readdir_args *ap)
{
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, j, error = 0;
wchar c;
u_int32_t faked = 0, num, off;
int ncookies = 0;
char convname[NTFS_MAXFILENAME + 1];
dprintf(("ntfs_readdir %ju off: %u resid: %zd\n",
(uintmax_t)ip->i_number, (uint32_t)uio->uio_offset,
uio->uio_resid));
if (uio->uio_offset < 0 || uio->uio_offset > INT_MAX)
return (EINVAL);
error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
if (error)
return (error);
off = num = uio->uio_offset;
faked = (ip->i_number == NTFS_ROOTINO) ? 1 : 2;
if (ip->i_number != NTFS_ROOTINO && num == 0) {
if (vop_write_dirent(&error, uio, ip->i_number,
DT_DIR, 1, "."))
goto done;
if (error)
goto done;
num++;
ncookies++;
}
if (num == faked - 1) {
if (vop_write_dirent(&error, uio, NTFS_ROOTINO,
DT_DIR, 2, ".."))
goto readdone;
if (error)
goto done;
num++;
ncookies++;
}
for (;;) {
struct attr_indexentry *iep;
error = ntfs_ntreaddir(ntmp, fp, num - faked, &iep);
if (error)
goto done;
if( NULL == iep )
break;
for (; !(iep->ie_flag & NTFS_IEFLAG_LAST);
iep = NTFS_NEXTREC(iep, struct attr_indexentry *))
{
if(!ntfs_isnamepermitted(ntmp,iep))
continue;
for(i=0, j=0; i < iep->ie_fnamelen; i++, j++) {
c = NTFS_U28(iep->ie_fname[i]);
if (c&0xFF00)
convname[j++] = (char)(c>>8);
convname[j] = (char)c&0xFF;
}
convname[j] = '\0';
if (vop_write_dirent(&error, uio, iep->ie_number,
(iep->ie_fflag & NTFS_FFLAG_DIR) ? DT_DIR : DT_REG,
j, convname))
goto readdone;
dprintf(("ntfs_readdir: elem: %d, fname:[%s] type: %d, "
"flag: %d, %s\n",
ncookies, convname, iep->ie_fnametype,
iep->ie_flag,
(iep->ie_fflag & NTFS_FFLAG_DIR) ?
"dir" : "reg"));
if (error)
goto done;
ncookies++;
num++;
}
}
readdone:
uio->uio_offset = num;
dprintf(("ntfs_readdir: %d entries (%d bytes) read\n",
ncookies,(u_int)(uio->uio_offset - off)));
dprintf(("ntfs_readdir: off: %u resid: %zd\n",
(uint32_t)uio->uio_offset, uio->uio_resid));
if (!error && ap->a_ncookies != NULL) {
off_t *cookies;
off_t *cookiep;
ddprintf(("ntfs_readdir: %d cookies\n",ncookies));
if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
panic("ntfs_readdir: unexpected uio from NFS server");
cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
cookiep = cookies;
while (off < num)
*cookiep++ = ++off;
*ap->a_ncookies = ncookies;
*ap->a_cookies = cookies;
}
done:
vn_unlock(vp);
return (error);
}
int
ntfs_lookup(struct vop_old_lookup_args *ap)
{
struct vnode *dvp = ap->a_dvp;
struct ntnode *dip = VTONT(dvp);
struct ntfsmount *ntmp = dip->i_mp;
struct componentname *cnp = ap->a_cnp;
int error;
int lockparent = cnp->cn_flags & CNP_LOCKPARENT;
#ifdef NTFS_DEBUG
int wantparent = cnp->cn_flags & (CNP_LOCKPARENT | CNP_WANTPARENT);
#endif
dprintf(("ntfs_lookup: \"%.*s\" (%ld bytes) in %ju, lp: %d, wp: %d \n",
(int)cnp->cn_namelen, cnp->cn_nameptr, cnp->cn_namelen,
(uintmax_t)dip->i_number, lockparent, wantparent));
*ap->a_vpp = NULL;
if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
dprintf(("ntfs_lookup: faking . directory in %u\n",
(uint32_t)dip->i_number));
vref(dvp);
*ap->a_vpp = dvp;
error = 0;
} else if (cnp->cn_flags & CNP_ISDOTDOT) {
struct ntvattr *vap;
dprintf(("ntfs_lookup: faking .. directory in %d\n",
(uint32_t)dip->i_number));
error = ntfs_ntvattrget(ntmp, dip, NTFS_A_NAME, NULL, 0, &vap);
if(error)
return (error);
VOP__UNLOCK(dvp, 0);
cnp->cn_flags |= CNP_PDIRUNLOCK;
dprintf(("ntfs_lookup: parentdir: %d\n",
vap->va_a_name->n_pnumber));
error = VFS_VGET(ntmp->ntm_mountp, NULL,
vap->va_a_name->n_pnumber,ap->a_vpp);
ntfs_ntvattrrele(vap);
if (error) {
if (VOP_LOCK(dvp, LK_EXCLUSIVE | LK_RETRY) == 0)
cnp->cn_flags &= ~CNP_PDIRUNLOCK;
return (error);
}
if (lockparent) {
error = VOP_LOCK(dvp, LK_EXCLUSIVE);
if (error) {
vput(*ap->a_vpp);
*ap->a_vpp = NULL;
return (error);
}
cnp->cn_flags &= ~CNP_PDIRUNLOCK;
}
} else {
error = ntfs_ntlookupfile(ntmp, dvp, cnp, ap->a_vpp);
if (error) {
dprintf(("ntfs_ntlookupfile: returned %d\n", error));
return (error);
}
dprintf(("ntfs_lookup: found ino: %u\n",
(uint32_t)VTONT(*ap->a_vpp)->i_number));
if (!lockparent) {
VOP__UNLOCK(dvp, 0);
cnp->cn_flags |= CNP_PDIRUNLOCK;
}
}
return (error);
}
static int
ntfs_fsync(struct vop_fsync_args *ap)
{
return (0);
}
int
ntfs_pathconf(struct vop_pathconf_args *ap)
{
switch (ap->a_name) {
case _PC_LINK_MAX:
*ap->a_retval = 1;
return (0);
case _PC_NAME_MAX:
*ap->a_retval = NTFS_MAXFILENAME;
return (0);
case _PC_PATH_MAX:
*ap->a_retval = PATH_MAX;
return (0);
case _PC_CHOWN_RESTRICTED:
*ap->a_retval = 1;
return (0);
case _PC_NO_TRUNC:
*ap->a_retval = 0;
return (0);
default:
return (EINVAL);
}
}
struct vop_ops ntfs_vnode_vops = {
.vop_default = vop_defaultop,
.vop_getattr = ntfs_getattr,
.vop_inactive = ntfs_inactive,
.vop_reclaim = ntfs_reclaim,
.vop_print = ntfs_print,
.vop_pathconf = ntfs_pathconf,
.vop_old_lookup = ntfs_lookup,
.vop_access = ntfs_access,
.vop_close = ntfs_close,
.vop_open = ntfs_open,
.vop_readdir = ntfs_readdir,
.vop_fsync = ntfs_fsync,
.vop_bmap = ntfs_bmap,
.vop_getpages = vop_stdgetpages,
.vop_putpages = vop_stdputpages,
.vop_strategy = ntfs_strategy,
.vop_read = ntfs_read,
.vop_write = ntfs_write
};