Change ntfs to vcache.

- Use (inumber, attrtype, attrname) as key.
- Inline ntfs_fget() and ntfs_frele() as they only get called once.
This commit is contained in:
hannken 2014-11-13 16:51:53 +00:00
parent a26eea283a
commit 0bac62655e
6 changed files with 165 additions and 242 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: ntfs.h,v 1.19 2008/05/05 17:11:16 ad Exp $ */
/* $NetBSD: ntfs.h,v 1.20 2014/11/13 16:51:53 hannken Exp $ */
/*-
* Copyright (c) 1998, 1999 Semen Ustimenko
@ -294,7 +294,6 @@ struct ntfsmount {
#ifdef _KERNEL
MALLOC_DECLARE(M_NTFSMNT);
MALLOC_DECLARE(M_NTFSNTNODE);
MALLOC_DECLARE(M_NTFSFNODE);
MALLOC_DECLARE(M_NTFSDIR);
MALLOC_DECLARE(M_NTFSNTVATTR);
MALLOC_DECLARE(M_NTFSRDATA);

View File

@ -1,4 +1,4 @@
/* $NetBSD: ntfs_inode.h,v 1.8 2014/11/13 16:49:56 hannken Exp $ */
/* $NetBSD: ntfs_inode.h,v 1.9 2014/11/13 16:51:53 hannken Exp $ */
/*-
* Copyright (c) 1998, 1999 Semen Ustimenko
@ -64,7 +64,6 @@ struct ntnode {
int i_usecount;
int i_busy;
LIST_HEAD(,fnode) i_fnlist;
LIST_HEAD(,ntvattr) i_valist;
long i_nlink; /* MFR */
@ -72,15 +71,19 @@ struct ntnode {
u_int32_t i_frflag; /* MFR */
};
#define FN_PRELOADED 0x0001
#define FN_VALID 0x0002
#define NTKEY_SIZE(attrlen) (sizeof(struct ntkey) + (attrlen))
struct ntkey {
ino_t k_ino; /* Inode number of ntnode. */
u_int32_t k_attrtype; /* Attribute type. */
char k_attrname[1]; /* Attribute name (variable length). */
} __packed;
struct fnode {
struct genfs_node f_gnode;
LIST_ENTRY(fnode) f_fnlist;
struct vnode *f_vp; /* Associatied vnode */
struct ntnode *f_ip; /* Associated ntnode */
u_long f_flag;
ntfs_times_t f_times; /* $NAME/dirinfo */
ino_t f_pnumber; /* $NAME/dirinfo */
@ -88,8 +91,11 @@ struct fnode {
u_int64_t f_size; /* defattr/dirinfo: */
u_int64_t f_allocated; /* defattr/dirinfo */
u_int32_t f_attrtype;
char *f_attrname;
struct ntkey *f_key;
struct ntkey f_smallkey;
#define f_ino f_key->k_ino
#define f_attrtype f_key->k_attrtype
#define f_attrname f_key->k_attrname
/* for ntreaddir */
u_int32_t f_lastdattr;

View File

@ -1,4 +1,4 @@
/* $NetBSD: ntfs_subr.c,v 1.53 2014/11/13 16:51:10 hannken Exp $ */
/* $NetBSD: ntfs_subr.c,v 1.54 2014/11/13 16:51:53 hannken Exp $ */
/*-
* Copyright (c) 1998, 1999 Semen Ustimenko (semenu@FreeBSD.org)
@ -29,7 +29,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ntfs_subr.c,v 1.53 2014/11/13 16:51:10 hannken Exp $");
__KERNEL_RCSID(0, "$NetBSD: ntfs_subr.c,v 1.54 2014/11/13 16:51:53 hannken Exp $");
#include <sys/param.h>
#include <sys/systm.h>
@ -428,8 +428,6 @@ ntfs_ntlookup(
ip->i_number = ino;
ip->i_mp = ntmp;
LIST_INIT(&ip->i_fnlist);
/* init lock and lock the newborn ntnode */
cv_init(&ip->i_lock, "ntfslk");
mutex_init(&ip->i_interlock, MUTEX_DEFAULT, IPL_NONE);
@ -479,9 +477,6 @@ ntfs_ntput(struct ntnode *ip)
dprintf(("%s: deallocating ntnode: %llu\n", __func__,
(unsigned long long)ip->i_number));
if (ip->i_fnlist.lh_first)
panic("ntfs_ntput: ntnode has fnodes");
ntfs_nthashrem(ip);
while (ip->i_valist.lh_first != NULL) {
@ -725,79 +720,6 @@ ntfs_uastrcmp(struct ntfsmount *ntmp, const wchar *ustr, size_t ustrlen, const c
return 1;
}
/*
* Search fnode in ntnode, if not found allocate and preinitialize.
*
* ntnode should be locked on entry.
*/
int
ntfs_fget(
struct ntfsmount *ntmp,
struct ntnode *ip,
int attrtype,
const char *attrname,
struct fnode **fpp
)
{
struct fnode *fp;
dprintf(("%s: ino: %llu, attrtype: 0x%x, attrname: %s\n", __func__,
(unsigned long long)ip->i_number, attrtype, attrname));
*fpp = NULL;
for (fp = ip->i_fnlist.lh_first; fp != NULL; fp = fp->f_fnlist.le_next){
dprintf(("%s: fnode: attrtype: %d, attrname: %s\n", __func__,
fp->f_attrtype, fp->f_attrname));
if ((attrtype == fp->f_attrtype) &&
strcmp(attrname, fp->f_attrname) == 0) {
dprintf(("%s: found existed: %p\n", __func__, fp));
*fpp = fp;
}
}
if (*fpp)
return (0);
fp = malloc(sizeof(*fp), M_NTFSFNODE, M_WAITOK|M_ZERO);
dprintf(("%s: allocating fnode: %p\n", __func__, fp));
fp->f_ip = ip;
fp->f_attrname = malloc(strlen(attrname)+1, M_TEMP, M_WAITOK);
strcpy(fp->f_attrname, attrname);
fp->f_attrtype = attrtype;
ntfs_ntref(ip);
LIST_INSERT_HEAD(&ip->i_fnlist, fp, f_fnlist);
*fpp = fp;
return (0);
}
/*
* Deallocate fnode, remove it from ntnode's fnode list.
*
* ntnode should be locked.
*/
void
ntfs_frele(
struct fnode *fp)
{
struct ntnode *ip = FTONT(fp);
dprintf(("%s: fnode: %p for %llu: %p\n", __func__, fp,
(unsigned long long)ip->i_number, ip));
dprintf(("%s: deallocating fnode\n", __func__));
LIST_REMOVE(fp,f_fnlist);
free(fp->f_attrname, M_TEMP);
if (fp->f_dirblbuf)
free(fp->f_dirblbuf, M_NTFSDIR);
free(fp, M_NTFSFNODE);
ntfs_ntrele(ip);
}
/*
* Lookup attribute name in format: [[:$ATTR_TYPE]:$ATTR_NAME],
* $ATTR_TYPE is searched in attrdefs read from $AttrDefs.
@ -855,7 +777,7 @@ ntfs_ntlookupattr(
/*
* Lookup specified node for filename, matching cnp,
* return fnode filled.
* return referenced vnode with fnode filled.
*/
int
ntfs_ntlookupfile(
@ -987,7 +909,7 @@ ntfs_ntlookupfile(
/* vget node */
error = ntfs_vgetex(ntmp->ntm_mountp, iep->ie_number,
attrtype, attrname ? attrname : "",
LK_EXCLUSIVE, &nvp);
0, &nvp);
/* free the buffer returned by ntfs_ntlookupattr() */
if (attrname) {
@ -998,8 +920,6 @@ ntfs_ntlookupfile(
if (error)
goto fail;
KASSERT(VTOF(nvp)->f_flag & FN_VALID);
*vpp = nvp;
goto fail;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: ntfs_subr.h,v 1.8 2014/11/13 16:51:10 hannken Exp $ */
/* $NetBSD: ntfs_subr.h,v 1.9 2014/11/13 16:51:53 hannken Exp $ */
/*-
* Copyright (c) 1998, 1999 Semen Ustimenko
@ -111,9 +111,6 @@ int ntfs_writeattr_plain(struct ntfsmount *, struct ntnode *, u_int32_t,
void ntfs_toupper_init(void);
int ntfs_toupper_use(struct mount *, struct ntfsmount *);
void ntfs_toupper_unuse(void);
int ntfs_fget(struct ntfsmount *, struct ntnode *, int, const char *,
struct fnode **);
void ntfs_frele(struct fnode *);
/* ntfs_conv.c stuff */
ntfs_wget_func_t ntfs_utf8_wget;

View File

@ -1,4 +1,4 @@
/* $NetBSD: ntfs_vfsops.c,v 1.96 2014/11/13 16:51:10 hannken Exp $ */
/* $NetBSD: ntfs_vfsops.c,v 1.97 2014/11/13 16:51:53 hannken Exp $ */
/*-
* Copyright (c) 1998, 1999 Semen Ustimenko
@ -29,7 +29,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ntfs_vfsops.c,v 1.96 2014/11/13 16:51:10 hannken Exp $");
__KERNEL_RCSID(0, "$NetBSD: ntfs_vfsops.c,v 1.97 2014/11/13 16:51:53 hannken Exp $");
#include <sys/param.h>
#include <sys/systm.h>
@ -63,7 +63,6 @@ MODULE(MODULE_CLASS_VFS, ntfs, NULL);
MALLOC_JUSTDEFINE(M_NTFSMNT, "NTFS mount", "NTFS mount structure");
MALLOC_JUSTDEFINE(M_NTFSNTNODE,"NTFS ntnode", "NTFS ntnode information");
MALLOC_JUSTDEFINE(M_NTFSFNODE,"NTFS fnode", "NTFS fnode information");
MALLOC_JUSTDEFINE(M_NTFSDIR,"NTFS dir", "NTFS dir buffer");
static int ntfs_mount(struct mount *, const char *, void *, size_t *);
@ -74,6 +73,8 @@ static int ntfs_sync(struct mount *, int, kauth_cred_t);
static int ntfs_unmount(struct mount *, int);
static int ntfs_vget(struct mount *mp, ino_t ino,
struct vnode **vpp);
static int ntfs_loadvnode(struct mount *, struct vnode *,
const void *, size_t, const void **);
static int ntfs_mountfs(struct vnode *, struct mount *,
struct ntfs_args *, struct lwp *);
static int ntfs_vptofh(struct vnode *, struct fid *, size_t *);
@ -130,7 +131,6 @@ ntfs_init(void)
malloc_type_attach(M_NTFSMNT);
malloc_type_attach(M_NTFSNTNODE);
malloc_type_attach(M_NTFSFNODE);
malloc_type_attach(M_NTFSDIR);
malloc_type_attach(M_NTFSNTVATTR);
malloc_type_attach(M_NTFSRDATA);
@ -152,7 +152,6 @@ ntfs_done(void)
ntfs_nthashdone();
malloc_type_detach(M_NTFSMNT);
malloc_type_detach(M_NTFSNTNODE);
malloc_type_detach(M_NTFSFNODE);
malloc_type_detach(M_NTFSDIR);
malloc_type_detach(M_NTFSNTVATTR);
malloc_type_detach(M_NTFSRDATA);
@ -703,162 +702,118 @@ ntfs_vptofh(
return (0);
}
int
ntfs_vgetex(
struct mount *mp,
ino_t ino,
u_int32_t attrtype,
const char *attrname,
u_long lkflags,
struct vnode **vpp)
static int
ntfs_loadvnode(struct mount *mp, struct vnode *vp,
const void *key, size_t key_len, const void **new_key)
{
int error;
struct ntvattr *vap;
struct ntkey small_key, *ntkey;
struct ntfsmount *ntmp;
struct ntnode *ip;
struct fnode *fp;
struct vnode *vp;
struct fnode *fp = NULL;
enum vtype f_type = VBAD;
dprintf(("ntfs_vgetex: ino: %llu, attr: 0x%x:%s, lkf: 0x%lx\n", (unsigned long long)ino, attrtype,
attrname, (u_long)lkflags));
if (key_len <= sizeof(small_key))
ntkey = &small_key;
else
ntkey = kmem_alloc(key_len, KM_SLEEP);
memcpy(ntkey, key, key_len);
dprintf(("ntfs_loadvnode: ino: %llu, attr: 0x%x:%s",
(unsigned long long)ntkey->k_ino,
ntkey->k_attrtype, ntkey->k_attrname));
ntmp = VFSTONTFS(mp);
*vpp = NULL;
loop:
/* Get ntnode */
error = ntfs_ntlookup(ntmp, ino, &ip);
error = ntfs_ntlookup(ntmp, ntkey->k_ino, &ip);
if (error) {
printf("ntfs_vget: ntfs_ntget failed\n");
return (error);
printf("ntfs_loadvnode: ntfs_ntget failed\n");
goto out;
}
/* It may be not initialized fully, so force load it */
if (!(ip->i_flag & IN_LOADED)) {
error = ntfs_loadntnode(ntmp, ip);
if(error) {
printf("ntfs_vget: CAN'T LOAD ATTRIBUTES FOR INO:"
" %llu\n", (unsigned long long)ip->i_number);
ntfs_ntput(ip);
return (error);
}
error = ntfs_loadntnode(ntmp, ip);
if(error) {
printf("ntfs_loadvnode: CAN'T LOAD ATTRIBUTES FOR INO:"
" %llu\n", (unsigned long long)ip->i_number);
ntfs_ntput(ip);
goto out;
}
}
error = ntfs_fget(ntmp, ip, attrtype, attrname, &fp);
/* Setup fnode */
fp = kmem_zalloc(sizeof(*fp), KM_SLEEP);
dprintf(("%s: allocating fnode: %p\n", __func__, fp));
error = ntfs_ntvattrget(ntmp, ip, NTFS_A_NAME, NULL, 0, &vap);
if (error) {
printf("ntfs_vget: ntfs_fget failed\n");
ntfs_ntput(ip);
return (error);
goto out;
}
fp->f_fflag = vap->va_a_name->n_flag;
fp->f_pnumber = vap->va_a_name->n_pnumber;
fp->f_times = vap->va_a_name->n_times;
ntfs_ntvattrrele(vap);
if ((ip->i_frflag & NTFS_FRFLAG_DIR) &&
(ntkey->k_attrtype == NTFS_A_DATA &&
strcmp(ntkey->k_attrname, "") == 0)) {
f_type = VDIR;
} else {
f_type = VREG;
error = ntfs_ntvattrget(ntmp, ip,
ntkey->k_attrtype, ntkey->k_attrname, 0, &vap);
if (error == 0) {
fp->f_size = vap->va_datalen;
fp->f_allocated = vap->va_allocated;
ntfs_ntvattrrele(vap);
} else if (ntkey->k_attrtype == NTFS_A_DATA &&
strcmp(ntkey->k_attrname, "") == 0 &&
error == ENOENT) {
fp->f_size = 0;
fp->f_allocated = 0;
error = 0;
} else
goto out;
}
if (!(fp->f_flag & FN_VALID)) {
struct ntvattr *vap;
error = ntfs_ntvattrget(ntmp, ip, NTFS_A_NAME, NULL, 0, &vap);
if (error) {
ntfs_ntput(ip);
return error;
}
fp->f_fflag = vap->va_a_name->n_flag;
fp->f_pnumber = vap->va_a_name->n_pnumber;
fp->f_times = vap->va_a_name->n_times;
ntfs_ntvattrrele(vap);
if ((ip->i_frflag & NTFS_FRFLAG_DIR) &&
(fp->f_attrtype == NTFS_A_DATA &&
strcmp(fp->f_attrname, "") == 0)) {
f_type = VDIR;
} else {
f_type = VREG;
error = ntfs_ntvattrget(ntmp, ip,
fp->f_attrtype, fp->f_attrname, 0, &vap);
if (error == 0) {
fp->f_size = vap->va_datalen;
fp->f_allocated = vap->va_allocated;
ntfs_ntvattrrele(vap);
} else if (fp->f_attrtype == NTFS_A_DATA &&
strcmp(fp->f_attrname, "") == 0 &&
error == ENOENT) {
fp->f_size = 0;
fp->f_allocated = 0;
error = 0;
} else {
ntfs_ntput(ip);
return (error);
}
}
fp->f_flag |= FN_VALID;
}
/*
* We may be calling vget() now. To avoid potential deadlock, we need
* to release ntnode lock, since due to locking order vnode
* lock has to be acquired first.
* ntfs_fget() bumped ntnode usecount, so ntnode won't be recycled
* prematurely.
* Take v_interlock before releasing ntnode lock to avoid races.
*/
vp = FTOV(fp);
if (vp) {
mutex_enter(vp->v_interlock);
ntfs_ntput(ip);
if (vget(vp, lkflags) != 0)
goto loop;
*vpp = vp;
return 0;
}
ntfs_ntput(ip);
error = getnewvnode(VT_NTFS, ntmp->ntm_mountp, ntfs_vnodeop_p,
NULL, &vp);
if(error) {
ntfs_frele(fp);
return (error);
}
ntfs_ntget(ip);
error = ntfs_fget(ntmp, ip, attrtype, attrname, &fp);
if (error) {
printf("ntfs_vget: ntfs_fget failed\n");
ntfs_ntput(ip);
return (error);
}
if (FTOV(fp)) {
/*
* Another thread beat us, put back freshly allocated
* vnode and retry.
*/
ntfs_ntput(ip);
ungetnewvnode(vp);
goto loop;
}
dprintf(("ntfs_vget: vnode: %p for ntnode: %llu\n", vp,
(unsigned long long)ino));
if (key_len <= sizeof(fp->f_smallkey))
fp->f_key = &fp->f_smallkey;
else
fp->f_key = kmem_alloc(key_len, KM_SLEEP);
fp->f_ip = ip;
fp->f_ino = ip->i_number;
strcpy(fp->f_attrname, ntkey->k_attrname);
fp->f_attrtype = ntkey->k_attrtype;
fp->f_vp = vp;
vp->v_data = fp;
if (f_type != VBAD)
vp->v_type = f_type;
vp->v_tag = VT_NTFS;
vp->v_type = f_type;
vp->v_op = ntfs_vnodeop_p;
ntfs_ntref(ip);
vref(ip->i_devvp);
genfs_node_init(vp, &ntfs_genfsops);
if (ino == NTFS_ROOTINO)
if (ip->i_number == NTFS_ROOTINO)
vp->v_vflag |= VV_ROOT;
uvm_vnp_setsize(vp, fp->f_size);
ntfs_ntput(ip);
if (lkflags & (LK_EXCLUSIVE | LK_SHARED)) {
error = vn_lock(vp, lkflags);
if (error) {
vput(vp);
return (error);
}
}
*new_key = fp->f_key;
uvm_vnp_setsize(vp, fp->f_size); /* XXX: mess, cf. ntfs_lookupfile() */
vref(ip->i_devvp);
*vpp = vp;
return (0);
fp = NULL;
out:
if (ntkey != &small_key)
kmem_free(ntkey, key_len);
if (fp)
kmem_free(fp, sizeof(*fp));
return error;
}
static int
@ -870,6 +825,45 @@ ntfs_vget(
return ntfs_vgetex(mp, ino, NTFS_A_DATA, "", LK_EXCLUSIVE, vpp);
}
int
ntfs_vgetex(
struct mount *mp,
ino_t ino,
u_int32_t attrtype,
const char *attrname,
u_long lkflags,
struct vnode **vpp)
{
const int attrlen = strlen(attrname);
int error;
struct ntkey small_key, *ntkey;
if (NTKEY_SIZE(attrlen) <= sizeof(small_key))
ntkey = &small_key;
else
ntkey = malloc(NTKEY_SIZE(attrlen), M_TEMP, M_WAITOK);
ntkey->k_ino = ino;
ntkey->k_attrtype = attrtype;
strcpy(ntkey->k_attrname, attrname);
error = vcache_get(mp, ntkey, NTKEY_SIZE(attrlen), vpp);
if (error)
goto out;
if ((lkflags & (LK_SHARED | LK_EXCLUSIVE)) != 0) {
error = vn_lock(*vpp, lkflags);
if (error) {
vrele(*vpp);
*vpp = NULL;
}
}
out:
if (ntkey != &small_key)
free(ntkey, M_TEMP);
return error;
}
extern const struct vnodeopv_desc ntfs_vnodeop_opv_desc;
const struct vnodeopv_desc * const ntfs_vnodeopv_descs[] = {
@ -888,6 +882,7 @@ struct vfsops ntfs_vfsops = {
.vfs_statvfs = ntfs_statvfs,
.vfs_sync = ntfs_sync,
.vfs_vget = ntfs_vget,
.vfs_loadvnode = ntfs_loadvnode,
.vfs_fhtovp = ntfs_fhtovp,
.vfs_vptofh = ntfs_vptofh,
.vfs_init = ntfs_init,

View File

@ -1,4 +1,4 @@
/* $NetBSD: ntfs_vnops.c,v 1.58 2014/11/13 16:49:56 hannken Exp $ */
/* $NetBSD: ntfs_vnops.c,v 1.59 2014/11/13 16:51:53 hannken Exp $ */
/*
* Copyright (c) 1992, 1993
@ -36,7 +36,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ntfs_vnops.c,v 1.58 2014/11/13 16:49:56 hannken Exp $");
__KERNEL_RCSID(0, "$NetBSD: ntfs_vnops.c,v 1.59 2014/11/13 16:51:53 hannken Exp $");
#include <sys/param.h>
#include <sys/systm.h>
@ -56,6 +56,7 @@ __KERNEL_RCSID(0, "$NetBSD: ntfs_vnops.c,v 1.58 2014/11/13 16:49:56 hannken Exp
#include <fs/ntfs/ntfs.h>
#include <fs/ntfs/ntfs_inode.h>
#include <fs/ntfs/ntfs_subr.h>
#include <fs/ntfs/ntfs_vfsops.h>
#include <miscfs/specfs/specdev.h>
#include <miscfs/genfs/genfs.h>
@ -235,6 +236,7 @@ ntfs_reclaim(void *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,
@ -246,16 +248,25 @@ ntfs_reclaim(void *v)
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);
ntfs_frele(fp);
ntfs_ntput(ip);
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);
}
@ -700,19 +711,17 @@ ntfs_lookup(void *v)
dprintf(("ntfs_lookup: faking .. directory in %llu\n",
(unsigned long long)dip->i_number));
VOP_UNLOCK(dvp);
error = ntfs_ntvattrget(ntmp, dip, NTFS_A_NAME, NULL, 0, &vap);
if (error) {
vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
return (error);
}
dprintf(("ntfs_lookup: parentdir: %d\n",
vap->va_a_name->n_pnumber));
error = VFS_VGET(ntmp->ntm_mountp,
vap->va_a_name->n_pnumber,ap->a_vpp);
error = ntfs_vgetex(ntmp->ntm_mountp,
vap->va_a_name->n_pnumber,
NTFS_A_DATA, "", 0, ap->a_vpp);
ntfs_ntvattrrele(vap);
vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
if (error) {
return (error);
}
@ -730,9 +739,6 @@ ntfs_lookup(void *v)
cache_enter(dvp, *ap->a_vpp, cnp->cn_nameptr, cnp->cn_namelen,
cnp->cn_flags);
if (*ap->a_vpp != dvp)
VOP_UNLOCK(*ap->a_vpp);
return error;
}