Home | History | Annotate | Line # | Download | only in ntfs
ntfs_subr.c revision 1.28
      1 /*	$NetBSD: ntfs_subr.c,v 1.28 2007/02/15 15:40:52 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 1999 Semen Ustimenko (semenu (at) FreeBSD.org)
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  *
     28  *	Id: ntfs_subr.c,v 1.4 1999/05/12 09:43:01 semenu Exp
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: ntfs_subr.c,v 1.28 2007/02/15 15:40:52 ad Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 #include <sys/namei.h>
     37 #include <sys/proc.h>
     38 #include <sys/kernel.h>
     39 #include <sys/vnode.h>
     40 #include <sys/mount.h>
     41 #include <sys/buf.h>
     42 #include <sys/file.h>
     43 #include <sys/malloc.h>
     44 #include <sys/lock.h>
     45 #include <sys/kauth.h>
     46 #if defined(__FreeBSD__)
     47 #include <machine/clock.h>
     48 #endif
     49 
     50 #include <miscfs/specfs/specdev.h>
     51 
     52 #include <fs/ntfs/ntfs.h>
     53 #include <fs/ntfs/ntfsmount.h>
     54 #include <fs/ntfs/ntfs_inode.h>
     55 #include <fs/ntfs/ntfs_vfsops.h>
     56 #include <fs/ntfs/ntfs_subr.h>
     57 #include <fs/ntfs/ntfs_compr.h>
     58 #include <fs/ntfs/ntfs_ihash.h>
     59 
     60 #ifdef NTFS_DEBUG
     61 int ntfs_debug = NTFS_DEBUG;
     62 #endif
     63 
     64 MALLOC_DEFINE(M_NTFSNTVATTR, "NTFS vattr", "NTFS file attribute information");
     65 MALLOC_DEFINE(M_NTFSRDATA, "NTFS res data", "NTFS resident data");
     66 MALLOC_DEFINE(M_NTFSRUN, "NTFS vrun", "NTFS vrun storage");
     67 MALLOC_DEFINE(M_NTFSDECOMP, "NTFS decomp", "NTFS decompression temporary");
     68 
     69 /* Local struct used in ntfs_ntlookupfile() */
     70 struct ntfs_lookup_ctx {
     71 	u_int32_t	aoff;
     72 	u_int32_t	rdsize;
     73 	cn_t		cn;
     74 	struct ntfs_lookup_ctx *prev;
     75 };
     76 
     77 static int ntfs_ntlookupattr(struct ntfsmount *, const char *, int,
     78 	int *, char **);
     79 static int ntfs_findvattr(struct ntfsmount *, struct ntnode *,
     80 	struct ntvattr **, struct ntvattr **, u_int32_t, const char *,
     81 	size_t, cn_t);
     82 static int ntfs_uastricmp(struct ntfsmount *, const wchar *, size_t,
     83 	const char *, size_t);
     84 static int ntfs_uastrcmp(struct ntfsmount *, const wchar *, size_t,
     85 	const char *, size_t);
     86 
     87 /* table for mapping Unicode chars into uppercase; it's filled upon first
     88  * ntfs mount, freed upon last ntfs umount */
     89 static wchar *ntfs_toupper_tab;
     90 #define NTFS_U28(ch)		((((ch) & 0xE0) == 0) ? '_' : (ch) & 0xFF)
     91 #define NTFS_TOUPPER(ch)	(ntfs_toupper_tab[(unsigned char)(ch)])
     92 static kmutex_t ntfs_toupper_lock;
     93 static signed int ntfs_toupper_usecount;
     94 
     95 /* support macro for ntfs_ntvattrget() */
     96 #define NTFS_AALPCMP(aalp,type,name,namelen) (				\
     97   (aalp->al_type == type) && (aalp->al_namelen == namelen) &&		\
     98   !ntfs_uastrcmp(ntmp, aalp->al_name,aalp->al_namelen,name,namelen) )
     99 
    100 /*
    101  *
    102  */
    103 int
    104 ntfs_ntvattrrele(vap)
    105 	struct ntvattr * vap;
    106 {
    107 	dprintf(("ntfs_ntvattrrele: ino: %llu, type: 0x%x\n",
    108 	    (unsigned long long)vap->va_ip->i_number, vap->va_type));
    109 
    110 	ntfs_ntrele(vap->va_ip);
    111 
    112 	return (0);
    113 }
    114 
    115 /*
    116  * find the attribute in the ntnode
    117  */
    118 static int
    119 ntfs_findvattr(ntmp, ip, lvapp, vapp, type, name, namelen, vcn)
    120 	struct ntfsmount *ntmp;
    121 	struct ntnode *ip;
    122 	struct ntvattr **lvapp, **vapp;
    123 	u_int32_t type;
    124 	const char *name;
    125 	size_t namelen;
    126 	cn_t vcn;
    127 {
    128 	int error;
    129 	struct ntvattr *vap;
    130 
    131 	if((ip->i_flag & IN_LOADED) == 0) {
    132 		dprintf(("ntfs_findvattr: node not loaded, ino: %llu\n",
    133 		    (unsigned long long)ip->i_number));
    134 		error = ntfs_loadntnode(ntmp,ip);
    135 		if (error) {
    136 			printf("ntfs_findvattr: FAILED TO LOAD INO: %llu\n",
    137 			    (unsigned long long)ip->i_number);
    138 			return (error);
    139 		}
    140 	}
    141 
    142 	*lvapp = NULL;
    143 	*vapp = NULL;
    144 	for (vap = ip->i_valist.lh_first; vap; vap = vap->va_list.le_next) {
    145 		ddprintf(("ntfs_findvattr: type: 0x%x, vcn: %qu - %qu\n",
    146 			  vap->va_type, (long long) vap->va_vcnstart,
    147 			  (long long) vap->va_vcnend));
    148 		if ((vap->va_type == type) &&
    149 		    (vap->va_vcnstart <= vcn) && (vap->va_vcnend >= vcn) &&
    150 		    (vap->va_namelen == namelen) &&
    151 		    (strncmp(name, vap->va_name, namelen) == 0)) {
    152 			*vapp = vap;
    153 			ntfs_ntref(vap->va_ip);
    154 			return (0);
    155 		}
    156 		if (vap->va_type == NTFS_A_ATTRLIST)
    157 			*lvapp = vap;
    158 	}
    159 
    160 	return (-1);
    161 }
    162 
    163 /*
    164  * Search attribute specified in ntnode (load ntnode if necessary).
    165  * If not found but ATTR_A_ATTRLIST present, read it in and search through.
    166  * VOP_VGET node needed, and lookup through its ntnode (load if nessesary).
    167  *
    168  * ntnode should be locked
    169  */
    170 int
    171 ntfs_ntvattrget(
    172 		struct ntfsmount * ntmp,
    173 		struct ntnode * ip,
    174 		u_int32_t type,
    175 		const char *name,
    176 		cn_t vcn,
    177 		struct ntvattr ** vapp)
    178 {
    179 	struct ntvattr *lvap = NULL;
    180 	struct attr_attrlist *aalp;
    181 	struct attr_attrlist *nextaalp;
    182 	struct vnode   *newvp;
    183 	struct ntnode  *newip;
    184 	caddr_t         alpool;
    185 	size_t		namelen, len;
    186 	int             error;
    187 
    188 	*vapp = NULL;
    189 
    190 	if (name) {
    191 		dprintf(("ntfs_ntvattrget: "
    192 		    "ino: %llu, type: 0x%x, name: %s, vcn: %qu\n",
    193 		    (unsigned long long)ip->i_number, type, name,
    194 		    (long long)vcn));
    195 		namelen = strlen(name);
    196 	} else {
    197 		dprintf(("ntfs_ntvattrget: "
    198 		    "ino: %llu, type: 0x%x, vcn: %qu\n",
    199 		    (unsigned long long)ip->i_number, type, (long long)vcn));
    200 		name = "";
    201 		namelen = 0;
    202 	}
    203 
    204 	error = ntfs_findvattr(ntmp, ip, &lvap, vapp, type, name, namelen, vcn);
    205 	if (error >= 0)
    206 		return (error);
    207 
    208 	if (!lvap) {
    209 		dprintf(("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: "
    210 		    "ino: %llu, type: 0x%x, name: %s, vcn: %qu\n",
    211 		    (unsigned long long)ip->i_number, type, name,
    212 		    (long long)vcn));
    213 		return (ENOENT);
    214 	}
    215 	/* Scan $ATTRIBUTE_LIST for requested attribute */
    216 	len = lvap->va_datalen;
    217 	alpool = (caddr_t) malloc(len, M_TEMP, M_WAITOK);
    218 	error = ntfs_readntvattr_plain(ntmp, ip, lvap, 0, len, alpool, &len,
    219 			NULL);
    220 	if (error)
    221 		goto out;
    222 
    223 	aalp = (struct attr_attrlist *) alpool;
    224 	nextaalp = NULL;
    225 
    226 	for(; len > 0; aalp = nextaalp) {
    227 		KASSERT(aalp != NULL);
    228 		dprintf(("ntfs_ntvattrget: "
    229 		    "attrlist: ino: %d, attr: 0x%x, vcn: %qu\n",
    230 		    aalp->al_inumber, aalp->al_type,
    231 			 (long long) aalp->al_vcnstart));
    232 
    233 		if (len > aalp->reclen) {
    234 			nextaalp = NTFS_NEXTREC(aalp, struct attr_attrlist *);
    235 		} else {
    236 			nextaalp = NULL;
    237 		}
    238 		len -= aalp->reclen;
    239 
    240 		if (!NTFS_AALPCMP(aalp, type, name, namelen) ||
    241 		    (nextaalp && (nextaalp->al_vcnstart <= vcn) &&
    242 		     NTFS_AALPCMP(nextaalp, type, name, namelen)))
    243 			continue;
    244 
    245 		dprintf(("ntfs_ntvattrget: attribute in ino: %d\n",
    246 				 aalp->al_inumber));
    247 
    248 		/* this is not a main record, so we can't use just plain
    249 		   vget() */
    250 		error = ntfs_vgetex(ntmp->ntm_mountp, aalp->al_inumber,
    251 				NTFS_A_DATA, NULL, LK_EXCLUSIVE,
    252 				VG_EXT, &newvp);
    253 		if (error) {
    254 			printf("ntfs_ntvattrget: CAN'T VGET INO: %d\n",
    255 			       aalp->al_inumber);
    256 			goto out;
    257 		}
    258 		newip = VTONT(newvp);
    259 		/* XXX have to lock ntnode */
    260 		error = ntfs_findvattr(ntmp, newip, &lvap, vapp,
    261 				type, name, namelen, vcn);
    262 		vput(newvp);
    263 		if (error == 0)
    264 			goto out;
    265 		printf("ntfs_ntvattrget: ATTRLIST ERROR.\n");
    266 		break;
    267 	}
    268 	error = ENOENT;
    269 
    270 	dprintf(("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: "
    271 	    "ino: %llu, type: 0x%x, name: %.*s, vcn: %qu\n",
    272 	    (unsigned long long)ip->i_number, type, (int)namelen,
    273 	    name, (long long)vcn));
    274 out:
    275 	free(alpool, M_TEMP);
    276 	return (error);
    277 }
    278 
    279 /*
    280  * Read ntnode from disk, make ntvattr list.
    281  *
    282  * ntnode should be locked
    283  */
    284 int
    285 ntfs_loadntnode(
    286 	      struct ntfsmount * ntmp,
    287 	      struct ntnode * ip)
    288 {
    289 	struct filerec  *mfrp;
    290 	daddr_t         bn;
    291 	int		error,off;
    292 	struct attr    *ap;
    293 	struct ntvattr *nvap;
    294 
    295 	dprintf(("ntfs_loadntnode: loading ino: %llu\n",
    296 	    (unsigned long long)ip->i_number));
    297 
    298 	mfrp = (struct filerec *) malloc(ntfs_bntob(ntmp->ntm_bpmftrec),
    299 	       M_TEMP, M_WAITOK);
    300 
    301 	if (ip->i_number < NTFS_SYSNODESNUM) {
    302 		struct buf     *bp;
    303 
    304 		dprintf(("ntfs_loadntnode: read system node\n"));
    305 
    306 		bn = ntfs_cntobn(ntmp->ntm_mftcn) +
    307 			ntmp->ntm_bpmftrec * ip->i_number;
    308 
    309 		error = bread(ntmp->ntm_devvp,
    310 			      bn, ntfs_bntob(ntmp->ntm_bpmftrec),
    311 			      NOCRED, &bp);
    312 		if (error) {
    313 			printf("ntfs_loadntnode: BREAD FAILED\n");
    314 			brelse(bp);
    315 			goto out;
    316 		}
    317 		memcpy(mfrp, bp->b_data, ntfs_bntob(ntmp->ntm_bpmftrec));
    318 		bqrelse(bp);
    319 	} else {
    320 		struct vnode   *vp;
    321 
    322 		vp = ntmp->ntm_sysvn[NTFS_MFTINO];
    323 		error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
    324 			       ip->i_number * ntfs_bntob(ntmp->ntm_bpmftrec),
    325 			       ntfs_bntob(ntmp->ntm_bpmftrec), mfrp, NULL);
    326 		if (error) {
    327 			printf("ntfs_loadntnode: ntfs_readattr failed\n");
    328 			goto out;
    329 		}
    330 	}
    331 
    332 	/* Check if magic and fixups are correct */
    333 	error = ntfs_procfixups(ntmp, NTFS_FILEMAGIC, (caddr_t)mfrp,
    334 				ntfs_bntob(ntmp->ntm_bpmftrec));
    335 	if (error) {
    336 		printf("ntfs_loadntnode: BAD MFT RECORD %d\n",
    337 		       (u_int32_t) ip->i_number);
    338 		goto out;
    339 	}
    340 
    341 	dprintf(("ntfs_loadntnode: load attrs for ino: %llu\n",
    342 	    (unsigned long long)ip->i_number));
    343 	off = mfrp->fr_attroff;
    344 	ap = (struct attr *) ((caddr_t)mfrp + off);
    345 
    346 	LIST_INIT(&ip->i_valist);
    347 
    348 	while (ap->a_hdr.a_type != -1) {
    349 		error = ntfs_attrtontvattr(ntmp, &nvap, ap);
    350 		if (error)
    351 			break;
    352 		nvap->va_ip = ip;
    353 
    354 		LIST_INSERT_HEAD(&ip->i_valist, nvap, va_list);
    355 
    356 		off += ap->a_hdr.reclen;
    357 		ap = (struct attr *) ((caddr_t)mfrp + off);
    358 	}
    359 	if (error) {
    360 		printf("ntfs_loadntnode: failed to load attr ino: %llu\n",
    361 		    (unsigned long long)ip->i_number);
    362 		goto out;
    363 	}
    364 
    365 	ip->i_mainrec = mfrp->fr_mainrec;
    366 	ip->i_nlink = mfrp->fr_nlink;
    367 	ip->i_frflag = mfrp->fr_flags;
    368 
    369 	ip->i_flag |= IN_LOADED;
    370 
    371 out:
    372 	free(mfrp, M_TEMP);
    373 	return (error);
    374 }
    375 
    376 /*
    377  * Routine locks ntnode and increase usecount, just opposite of
    378  * ntfs_ntput().
    379  */
    380 int
    381 ntfs_ntget(ip)
    382 	struct ntnode *ip;
    383 {
    384 	dprintf(("ntfs_ntget: get ntnode %llu: %p, usecount: %d\n",
    385 	    (unsigned long long)ip->i_number, ip, ip->i_usecount));
    386 
    387 	simple_lock(&ip->i_interlock);
    388 	ip->i_usecount++;
    389 	lockmgr(&ip->i_lock, LK_EXCLUSIVE | LK_INTERLOCK, &ip->i_interlock);
    390 
    391 	return 0;
    392 }
    393 
    394 /*
    395  * Routine search ntnode in hash, if found: lock, inc usecount and return.
    396  * If not in hash allocate structure for ntnode, prefill it, lock,
    397  * inc count and return.
    398  *
    399  * ntnode returned locked
    400  */
    401 int
    402 ntfs_ntlookup(
    403 	   struct ntfsmount * ntmp,
    404 	   ino_t ino,
    405 	   struct ntnode ** ipp)
    406 {
    407 	struct ntnode  *ip;
    408 
    409 	dprintf(("ntfs_ntlookup: looking for ntnode %llu\n",
    410 	    (unsigned long long)ino));
    411 
    412 	if ((ip = ntfs_nthashlookup(ntmp->ntm_dev, ino)) != NULL) {
    413 		ntfs_ntget(ip);
    414 		dprintf(("ntfs_ntlookup: ntnode %llu: %p,"
    415 		    " usecount: %d\n",
    416 		    (unsigned long long)ino, ip, ip->i_usecount));
    417 		*ipp = ip;
    418 		return (0);
    419 	}
    420 
    421 	MALLOC(ip, struct ntnode *, sizeof(struct ntnode),
    422 	       M_NTFSNTNODE, M_WAITOK);
    423 	ddprintf(("ntfs_ntlookup: allocating ntnode: %llu: %p\n",
    424 	    (unsigned long long)ino, ip));
    425 	bzero(ip, sizeof(struct ntnode));
    426 
    427 	mutex_enter(&ntfs_hashlock);
    428 	if ((ip = ntfs_nthashlookup(ntmp->ntm_dev, ino)) != NULL) {
    429 		mutex_exit(&ntfs_hashlock);
    430 		ntfs_ntget(ip);
    431 		FREE(ip, M_NTFSNTNODE);
    432 		dprintf(("ntfs_ntlookup: ntnode %llu: %p,"
    433 		    " usecount: %d\n",
    434 		    (unsigned long long)ino, ip, ip->i_usecount));
    435 		*ipp = ip;
    436 		return (0);
    437 	}
    438 
    439 	/* Generic initialization */
    440 	ip->i_devvp = ntmp->ntm_devvp;
    441 	ip->i_dev = ntmp->ntm_dev;
    442 	ip->i_number = ino;
    443 	ip->i_mp = ntmp;
    444 
    445 	LIST_INIT(&ip->i_fnlist);
    446 
    447 	/* init lock and lock the newborn ntnode */
    448 	lockinit(&ip->i_lock, PINOD, "ntnode", 0, LK_EXCLUSIVE);
    449 	simple_lock_init(&ip->i_interlock);
    450 	ntfs_ntget(ip);
    451 
    452 	ntfs_nthashins(ip);
    453 
    454 	mutex_exit(&ntfs_hashlock);
    455 
    456 	*ipp = ip;
    457 
    458 	dprintf(("ntfs_ntlookup: ntnode %llu: %p, usecount: %d\n",
    459 	    (unsigned long long)ino, ip, ip->i_usecount));
    460 
    461 	return (0);
    462 }
    463 
    464 /*
    465  * Decrement usecount of ntnode and unlock it, if usecount reach zero,
    466  * deallocate ntnode.
    467  *
    468  * ntnode should be locked on entry, and unlocked on return.
    469  */
    470 void
    471 ntfs_ntput(ip)
    472 	struct ntnode *ip;
    473 {
    474 	struct ntvattr *vap;
    475 
    476 	dprintf(("ntfs_ntput: rele ntnode %llu: %p, usecount: %d\n",
    477 	    (unsigned long long)ip->i_number, ip, ip->i_usecount));
    478 
    479 	simple_lock(&ip->i_interlock);
    480 	ip->i_usecount--;
    481 
    482 #ifdef DIAGNOSTIC
    483 	if (ip->i_usecount < 0) {
    484 		panic("ntfs_ntput: ino: %llu usecount: %d ",
    485 		    (unsigned long long)ip->i_number, ip->i_usecount);
    486 	}
    487 #endif
    488 
    489 	lockmgr(&ip->i_lock, LK_RELEASE|LK_INTERLOCK, &ip->i_interlock);
    490 
    491 	if (ip->i_usecount == 0) {
    492 		dprintf(("ntfs_ntput: deallocating ntnode: %llu\n",
    493 		    (unsigned long long)ip->i_number));
    494 
    495 		if (ip->i_fnlist.lh_first)
    496 			panic("ntfs_ntput: ntnode has fnodes");
    497 
    498 		ntfs_nthashrem(ip);
    499 
    500 		while (ip->i_valist.lh_first != NULL) {
    501 			vap = ip->i_valist.lh_first;
    502 			LIST_REMOVE(vap,va_list);
    503 			ntfs_freentvattr(vap);
    504 		}
    505 		FREE(ip, M_NTFSNTNODE);
    506 	}
    507 }
    508 
    509 /*
    510  * increment usecount of ntnode
    511  */
    512 void
    513 ntfs_ntref(ip)
    514 	struct ntnode *ip;
    515 {
    516 	simple_lock(&ip->i_interlock);
    517 	ip->i_usecount++;
    518 	simple_unlock(&ip->i_interlock);
    519 
    520 	dprintf(("ntfs_ntref: ino %llu, usecount: %d\n",
    521 	    (unsigned long long)ip->i_number, ip->i_usecount));
    522 
    523 }
    524 
    525 /*
    526  * Decrement usecount of ntnode.
    527  */
    528 void
    529 ntfs_ntrele(ip)
    530 	struct ntnode *ip;
    531 {
    532 	dprintf(("ntfs_ntrele: rele ntnode %llu: %p, usecount: %d\n",
    533 	    (unsigned long long)ip->i_number, ip, ip->i_usecount));
    534 
    535 	simple_lock(&ip->i_interlock);
    536 	ip->i_usecount--;
    537 
    538 	if (ip->i_usecount < 0)
    539 		panic("ntfs_ntrele: ino: %llu usecount: %d ",
    540 		    (unsigned long long)ip->i_number, ip->i_usecount);
    541 	simple_unlock(&ip->i_interlock);
    542 }
    543 
    544 /*
    545  * Deallocate all memory allocated for ntvattr
    546  */
    547 void
    548 ntfs_freentvattr(vap)
    549 	struct ntvattr * vap;
    550 {
    551 	if (vap->va_flag & NTFS_AF_INRUN) {
    552 		if (vap->va_vruncn)
    553 			free(vap->va_vruncn, M_NTFSRUN);
    554 		if (vap->va_vruncl)
    555 			free(vap->va_vruncl, M_NTFSRUN);
    556 	} else {
    557 		if (vap->va_datap)
    558 			free(vap->va_datap, M_NTFSRDATA);
    559 	}
    560 	FREE(vap, M_NTFSNTVATTR);
    561 }
    562 
    563 /*
    564  * Convert disk image of attribute into ntvattr structure,
    565  * runs are expanded also.
    566  */
    567 int
    568 ntfs_attrtontvattr(
    569 		   struct ntfsmount * ntmp,
    570 		   struct ntvattr ** rvapp,
    571 		   struct attr * rap)
    572 {
    573 	int             error, i;
    574 	struct ntvattr *vap;
    575 
    576 	error = 0;
    577 	*rvapp = NULL;
    578 
    579 	MALLOC(vap, struct ntvattr *, sizeof(struct ntvattr),
    580 		M_NTFSNTVATTR, M_WAITOK);
    581 	bzero(vap, sizeof(struct ntvattr));
    582 	vap->va_ip = NULL;
    583 	vap->va_flag = rap->a_hdr.a_flag;
    584 	vap->va_type = rap->a_hdr.a_type;
    585 	vap->va_compression = rap->a_hdr.a_compression;
    586 	vap->va_index = rap->a_hdr.a_index;
    587 
    588 	ddprintf(("type: 0x%x, index: %d", vap->va_type, vap->va_index));
    589 
    590 	vap->va_namelen = rap->a_hdr.a_namelen;
    591 	if (rap->a_hdr.a_namelen) {
    592 		wchar *unp = (wchar *) ((caddr_t) rap + rap->a_hdr.a_nameoff);
    593 		ddprintf((", name:["));
    594 		for (i = 0; i < vap->va_namelen; i++) {
    595 			vap->va_name[i] = unp[i];
    596 			ddprintf(("%c", vap->va_name[i]));
    597 		}
    598 		ddprintf(("]"));
    599 	}
    600 	if (vap->va_flag & NTFS_AF_INRUN) {
    601 		ddprintf((", nonres."));
    602 		vap->va_datalen = rap->a_nr.a_datalen;
    603 		vap->va_allocated = rap->a_nr.a_allocated;
    604 		vap->va_vcnstart = rap->a_nr.a_vcnstart;
    605 		vap->va_vcnend = rap->a_nr.a_vcnend;
    606 		vap->va_compressalg = rap->a_nr.a_compressalg;
    607 		error = ntfs_runtovrun(&(vap->va_vruncn), &(vap->va_vruncl),
    608 				       &(vap->va_vruncnt),
    609 				       (u_int8_t *) rap + rap->a_nr.a_dataoff);
    610 	} else {
    611 		vap->va_compressalg = 0;
    612 		ddprintf((", res."));
    613 		vap->va_datalen = rap->a_r.a_datalen;
    614 		vap->va_allocated = rap->a_r.a_datalen;
    615 		vap->va_vcnstart = 0;
    616 		vap->va_vcnend = ntfs_btocn(vap->va_allocated);
    617 		vap->va_datap = (caddr_t) malloc(vap->va_datalen,
    618 		       M_NTFSRDATA, M_WAITOK);
    619 		memcpy(vap->va_datap, (caddr_t) rap + rap->a_r.a_dataoff,
    620 		       rap->a_r.a_datalen);
    621 	}
    622 	ddprintf((", len: %qu", (long long)vap->va_datalen));
    623 
    624 	if (error)
    625 		FREE(vap, M_NTFSNTVATTR);
    626 	else
    627 		*rvapp = vap;
    628 
    629 	ddprintf(("\n"));
    630 
    631 	return (error);
    632 }
    633 
    634 /*
    635  * Expand run into more utilizable and more memory eating format.
    636  */
    637 int
    638 ntfs_runtovrun(
    639 	       cn_t ** rcnp,
    640 	       cn_t ** rclp,
    641 	       u_long * rcntp,
    642 	       u_int8_t * run)
    643 {
    644 	u_int32_t       off;
    645 	u_int32_t       sz, i;
    646 	cn_t           *cn;
    647 	cn_t           *cl;
    648 	u_long		cnt;
    649 	cn_t		prev;
    650 	cn_t		tmp;
    651 
    652 	off = 0;
    653 	cnt = 0;
    654 	i = 0;
    655 	while (run[off]) {
    656 		off += (run[off] & 0xF) + ((run[off] >> 4) & 0xF) + 1;
    657 		cnt++;
    658 	}
    659 	cn = (cn_t *) malloc(cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
    660 	cl = (cn_t *) malloc(cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
    661 
    662 	off = 0;
    663 	cnt = 0;
    664 	prev = 0;
    665 	while (run[off]) {
    666 
    667 		sz = run[off++];
    668 		cl[cnt] = 0;
    669 
    670 		for (i = 0; i < (sz & 0xF); i++)
    671 			cl[cnt] += (u_int32_t) run[off++] << (i << 3);
    672 
    673 		sz >>= 4;
    674 		if (run[off + sz - 1] & 0x80) {
    675 			tmp = ((u_int64_t) - 1) << (sz << 3);
    676 			for (i = 0; i < sz; i++)
    677 				tmp |= (u_int64_t) run[off++] << (i << 3);
    678 		} else {
    679 			tmp = 0;
    680 			for (i = 0; i < sz; i++)
    681 				tmp |= (u_int64_t) run[off++] << (i << 3);
    682 		}
    683 		if (tmp)
    684 			prev = cn[cnt] = prev + tmp;
    685 		else
    686 			cn[cnt] = tmp;
    687 
    688 		cnt++;
    689 	}
    690 	*rcnp = cn;
    691 	*rclp = cl;
    692 	*rcntp = cnt;
    693 	return (0);
    694 }
    695 
    696 /*
    697  * Compare unicode and ascii string case insens.
    698  */
    699 static int
    700 ntfs_uastricmp(ntmp, ustr, ustrlen, astr, astrlen)
    701 	struct ntfsmount *ntmp;
    702 	const wchar *ustr;
    703 	size_t ustrlen;
    704 	const char *astr;
    705 	size_t astrlen;
    706 {
    707 	size_t  i;
    708 	int res;
    709 
    710 	for (i = 0; i < ustrlen && astrlen > 0; i++) {
    711 		res = (*ntmp->ntm_wcmp)(NTFS_TOUPPER(ustr[i]),
    712 		    NTFS_TOUPPER((*ntmp->ntm_wget)(&astr, &astrlen)) );
    713 		if (res)
    714 			return res;
    715 	}
    716 
    717 	if (i == ustrlen && astrlen == 0)
    718 		return 0;
    719 	else if (i == ustrlen)
    720 		return -1;
    721 	else
    722 		return 1;
    723 }
    724 
    725 /*
    726  * Compare unicode and ascii string case sens.
    727  */
    728 static int
    729 ntfs_uastrcmp(ntmp, ustr, ustrlen, astr, astrlen)
    730 	struct ntfsmount *ntmp;
    731 	const wchar *ustr;
    732 	size_t ustrlen;
    733 	const char *astr;
    734 	size_t astrlen;
    735 {
    736 	size_t i;
    737 	int res;
    738 
    739 	for (i = 0; (i < ustrlen) && astrlen > 0; i++) {
    740 		res = (*ntmp->ntm_wcmp)(ustr[i],
    741 		     (*ntmp->ntm_wget)(&astr, &astrlen));
    742 		if (res)
    743 			return res;
    744 	}
    745 
    746 	if (i == ustrlen && astrlen == 0)
    747 		return 0;
    748 	else if (i == ustrlen)
    749 		return -1;
    750 	else
    751 		return 1;
    752 }
    753 
    754 /*
    755  * Search fnode in ntnode, if not found allocate and preinitialize.
    756  *
    757  * ntnode should be locked on entry.
    758  */
    759 int
    760 ntfs_fget(
    761     struct ntfsmount *ntmp,
    762     struct ntnode *ip,
    763     int attrtype,
    764     char *attrname,
    765     struct fnode **fpp
    766 )
    767 {
    768 	struct fnode *fp;
    769 
    770 	dprintf(("ntfs_fget: ino: %llu, attrtype: 0x%x, attrname: %s\n",
    771 	    (unsigned long long)ip->i_number, attrtype, attrname?attrname:""));
    772 	*fpp = NULL;
    773 	for (fp = ip->i_fnlist.lh_first; fp != NULL; fp = fp->f_fnlist.le_next){
    774 		dprintf(("ntfs_fget: fnode: attrtype: %d, attrname: %s\n",
    775 			fp->f_attrtype, fp->f_attrname?fp->f_attrname:""));
    776 
    777 		if ((attrtype == fp->f_attrtype) &&
    778 		    ((!attrname && !fp->f_attrname) ||
    779 		     (attrname && fp->f_attrname &&
    780 		      !strcmp(attrname,fp->f_attrname)))){
    781 			dprintf(("ntfs_fget: found existed: %p\n",fp));
    782 			*fpp = fp;
    783 		}
    784 	}
    785 
    786 	if (*fpp)
    787 		return (0);
    788 
    789 	MALLOC(fp, struct fnode *, sizeof(struct fnode), M_NTFSFNODE, M_WAITOK);
    790 	bzero(fp, sizeof(struct fnode));
    791 	dprintf(("ntfs_fget: allocating fnode: %p\n",fp));
    792 
    793 	fp->f_ip = ip;
    794 	fp->f_attrname = attrname;
    795 	if (fp->f_attrname) fp->f_flag |= FN_AATTRNAME;
    796 	fp->f_attrtype = attrtype;
    797 
    798 	ntfs_ntref(ip);
    799 
    800 	LIST_INSERT_HEAD(&ip->i_fnlist, fp, f_fnlist);
    801 
    802 	*fpp = fp;
    803 
    804 	return (0);
    805 }
    806 
    807 /*
    808  * Deallocate fnode, remove it from ntnode's fnode list.
    809  *
    810  * ntnode should be locked.
    811  */
    812 void
    813 ntfs_frele(
    814 	struct fnode *fp)
    815 {
    816 	struct ntnode *ip = FTONT(fp);
    817 
    818 	dprintf(("ntfs_frele: fnode: %p for %llu: %p\n", fp,
    819 	    (unsigned long long)ip->i_number, ip));
    820 
    821 	dprintf(("ntfs_frele: deallocating fnode\n"));
    822 	LIST_REMOVE(fp,f_fnlist);
    823 	if (fp->f_flag & FN_AATTRNAME)
    824 		FREE(fp->f_attrname, M_TEMP);
    825 	if (fp->f_dirblbuf)
    826 		FREE(fp->f_dirblbuf, M_NTFSDIR);
    827 	FREE(fp, M_NTFSFNODE);
    828 	ntfs_ntrele(ip);
    829 }
    830 
    831 /*
    832  * Lookup attribute name in format: [[:$ATTR_TYPE]:$ATTR_NAME],
    833  * $ATTR_TYPE is searched in attrdefs read from $AttrDefs.
    834  * If $ATTR_TYPE not specified, ATTR_A_DATA assumed.
    835  */
    836 static int
    837 ntfs_ntlookupattr(
    838 		struct ntfsmount * ntmp,
    839 		const char * name,
    840 		int namelen,
    841 		int *attrtype,
    842 		char **attrname)
    843 {
    844 	const char *sys;
    845 	size_t syslen, i;
    846 	struct ntvattrdef *adp;
    847 
    848 	if (namelen == 0)
    849 		return (0);
    850 
    851 	if (name[0] == '$') {
    852 		sys = name;
    853 		for (syslen = 0; syslen < namelen; syslen++) {
    854 			if(sys[syslen] == ':') {
    855 				name++;
    856 				namelen--;
    857 				break;
    858 			}
    859 		}
    860 		name += syslen;
    861 		namelen -= syslen;
    862 
    863 		adp = ntmp->ntm_ad;
    864 		for (i = 0; i < ntmp->ntm_adnum; i++, adp++){
    865 			if (syslen != adp->ad_namelen ||
    866 			   strncmp(sys, adp->ad_name, syslen) != 0)
    867 				continue;
    868 
    869 			*attrtype = adp->ad_type;
    870 			goto out;
    871 		}
    872 		return (ENOENT);
    873 	}
    874 
    875     out:
    876 	if (namelen) {
    877 		*attrname = (char *) malloc(namelen, M_TEMP, M_WAITOK);
    878 		memcpy((*attrname), name, namelen);
    879 		(*attrname)[namelen] = '\0';
    880 		*attrtype = NTFS_A_DATA;
    881 	}
    882 
    883 	return (0);
    884 }
    885 
    886 /*
    887  * Lookup specified node for filename, matching cnp,
    888  * return fnode filled.
    889  */
    890 int
    891 ntfs_ntlookupfile(
    892 	      struct ntfsmount * ntmp,
    893 	      struct vnode * vp,
    894 	      struct componentname * cnp,
    895 	      struct vnode ** vpp)
    896 {
    897 	struct fnode   *fp = VTOF(vp);
    898 	struct ntnode  *ip = FTONT(fp);
    899 	struct ntvattr *vap;	/* Root attribute */
    900 	cn_t            cn = 0;	/* VCN in current attribute */
    901 	caddr_t         rdbuf;	/* Buffer to read directory's blocks  */
    902 	u_int32_t       blsize;
    903 	u_int32_t       rdsize;	/* Length of data to read from current block */
    904 	struct attr_indexentry *iep;
    905 	int             error, res, anamelen, fnamelen;
    906 	const char     *fname,*aname;
    907 	u_int32_t       aoff;
    908 	int attrtype = NTFS_A_DATA;
    909 	char *attrname = NULL;
    910 	struct fnode   *nfp;
    911 	struct vnode   *nvp;
    912 	enum vtype	f_type;
    913 	int fullscan = 0;
    914 	struct ntfs_lookup_ctx *lookup_ctx = NULL, *tctx;
    915 
    916 	error = ntfs_ntget(ip);
    917 	if (error)
    918 		return (error);
    919 
    920 	error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
    921 	if (error || (vap->va_flag & NTFS_AF_INRUN))
    922 		return (ENOTDIR);
    923 
    924 	/*
    925 	 * Divide file name into: foofilefoofilefoofile[:attrspec]
    926 	 * Store like this:       fname:fnamelen       [aname:anamelen]
    927 	 */
    928 	fname = cnp->cn_nameptr;
    929 	aname = NULL;
    930 	anamelen = 0;
    931 	for (fnamelen = 0; fnamelen < cnp->cn_namelen; fnamelen++)
    932 		if(fname[fnamelen] == ':') {
    933 			aname = fname + fnamelen + 1;
    934 			anamelen = cnp->cn_namelen - fnamelen - 1;
    935 			dprintf(("ntfs_ntlookupfile: %s (%d), attr: %s (%d)\n",
    936 				fname, fnamelen, aname, anamelen));
    937 			break;
    938 		}
    939 
    940 	blsize = vap->va_a_iroot->ir_size;
    941 	dprintf(("ntfs_ntlookupfile: blksz: %d\n", blsize));
    942 
    943 	rdbuf = (caddr_t) malloc(blsize, M_TEMP, M_WAITOK);
    944 
    945     loop:
    946 	rdsize = vap->va_datalen;
    947 	dprintf(("ntfs_ntlookupfile: rdsz: %d\n", rdsize));
    948 
    949 	error = ntfs_readattr(ntmp, ip, NTFS_A_INDXROOT, "$I30",
    950 			       0, rdsize, rdbuf, NULL);
    951 	if (error)
    952 		goto fail;
    953 
    954 	aoff = sizeof(struct attr_indexroot);
    955 
    956 	do {
    957 		iep = (struct attr_indexentry *) (rdbuf + aoff);
    958 
    959 		for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
    960 			aoff += iep->reclen,
    961 			iep = (struct attr_indexentry *) (rdbuf + aoff))
    962 		{
    963 			ddprintf(("scan: %d, %d\n",
    964 				  (u_int32_t) iep->ie_number,
    965 				  (u_int32_t) iep->ie_fnametype));
    966 
    967 			/* check the name - the case-insensitive check
    968 			 * has to come first, to break from this for loop
    969 			 * if needed, so we can dive correctly */
    970 			res = ntfs_uastricmp(ntmp, iep->ie_fname,
    971 				iep->ie_fnamelen, fname, fnamelen);
    972 			if (!fullscan) {
    973 				if (res > 0) break;
    974 				if (res < 0) continue;
    975 			}
    976 
    977 			if (iep->ie_fnametype == 0 ||
    978 			    !(ntmp->ntm_flag & NTFS_MFLAG_CASEINS))
    979 			{
    980 				res = ntfs_uastrcmp(ntmp, iep->ie_fname,
    981 					iep->ie_fnamelen, fname, fnamelen);
    982 				if (res != 0 && !fullscan) continue;
    983 			}
    984 
    985 			/* if we perform full scan, the file does not match
    986 			 * and this is subnode, dive */
    987 			if (fullscan && res != 0) {
    988 			    if (iep->ie_flag & NTFS_IEFLAG_SUBNODE) {
    989 				MALLOC(tctx, struct ntfs_lookup_ctx *,
    990 					sizeof(struct ntfs_lookup_ctx),
    991 					M_TEMP, M_WAITOK);
    992 				tctx->aoff	= aoff + iep->reclen;
    993 				tctx->rdsize	= rdsize;
    994 				tctx->cn	= cn;
    995 				tctx->prev	= lookup_ctx;
    996 				lookup_ctx = tctx;
    997 				break;
    998 			    } else
    999 				continue;
   1000 			}
   1001 
   1002 			if (aname) {
   1003 				error = ntfs_ntlookupattr(ntmp,
   1004 					aname, anamelen,
   1005 					&attrtype, &attrname);
   1006 				if (error)
   1007 					goto fail;
   1008 			}
   1009 
   1010 			/* Check if we've found ourselves */
   1011 			if ((iep->ie_number == ip->i_number) &&
   1012 			    (attrtype == fp->f_attrtype) &&
   1013 			    ((!attrname && !fp->f_attrname) ||
   1014 			     (attrname && fp->f_attrname &&
   1015 			      !strcmp(attrname, fp->f_attrname))))
   1016 			{
   1017 				VREF(vp);
   1018 				*vpp = vp;
   1019 				error = 0;
   1020 				goto fail;
   1021 			}
   1022 
   1023 			/* free the buffer returned by ntfs_ntlookupattr() */
   1024 			if (attrname) {
   1025 				FREE(attrname, M_TEMP);
   1026 				attrname = NULL;
   1027 			}
   1028 
   1029 			/* vget node, but don't load it */
   1030 			error = ntfs_vgetex(ntmp->ntm_mountp,
   1031 				   iep->ie_number, attrtype, attrname,
   1032 				   LK_EXCLUSIVE, VG_DONTLOADIN | VG_DONTVALIDFN,
   1033 				   &nvp);
   1034 			if (error)
   1035 				goto fail;
   1036 
   1037 			nfp = VTOF(nvp);
   1038 
   1039 			if (nfp->f_flag & FN_VALID) {
   1040 				*vpp = nvp;
   1041 				goto fail;
   1042 			}
   1043 
   1044 			nfp->f_fflag = iep->ie_fflag;
   1045 			nfp->f_pnumber = iep->ie_fpnumber;
   1046 			nfp->f_times = iep->ie_ftimes;
   1047 
   1048 			if((nfp->f_fflag & NTFS_FFLAG_DIR) &&
   1049 			   (nfp->f_attrtype == NTFS_A_DATA) &&
   1050 			   (nfp->f_attrname == NULL))
   1051 				f_type = VDIR;
   1052 			else
   1053 				f_type = VREG;
   1054 
   1055 			nvp->v_type = f_type;
   1056 
   1057 			if ((nfp->f_attrtype == NTFS_A_DATA) &&
   1058 			    (nfp->f_attrname == NULL))
   1059 			{
   1060 				/* Opening default attribute */
   1061 				nfp->f_size = iep->ie_fsize;
   1062 				nfp->f_allocated = iep->ie_fallocated;
   1063 				nfp->f_flag |= FN_PRELOADED;
   1064 			} else {
   1065 				error = ntfs_filesize(ntmp, nfp,
   1066 					    &nfp->f_size, &nfp->f_allocated);
   1067 				if (error) {
   1068 					vput(nvp);
   1069 					goto fail;
   1070 				}
   1071 			}
   1072 
   1073 			nfp->f_flag &= ~FN_VALID;
   1074 			*vpp = nvp;
   1075 			goto fail;
   1076 		}
   1077 
   1078 		/* Dive if possible */
   1079 		if (iep->ie_flag & NTFS_IEFLAG_SUBNODE) {
   1080 			dprintf(("ntfs_ntlookupfile: diving\n"));
   1081 
   1082 			cn = *(cn_t *) (rdbuf + aoff +
   1083 					iep->reclen - sizeof(cn_t));
   1084 			rdsize = blsize;
   1085 
   1086 			error = ntfs_readattr(ntmp, ip, NTFS_A_INDX, "$I30",
   1087 					ntfs_cntob(cn), rdsize, rdbuf, NULL);
   1088 			if (error)
   1089 				goto fail;
   1090 
   1091 			error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
   1092 						rdbuf, rdsize);
   1093 			if (error)
   1094 				goto fail;
   1095 
   1096 			aoff = (((struct attr_indexalloc *) rdbuf)->ia_hdrsize +
   1097 				0x18);
   1098 		} else if (fullscan && lookup_ctx) {
   1099 			cn = lookup_ctx->cn;
   1100 			aoff = lookup_ctx->aoff;
   1101 			rdsize = lookup_ctx->rdsize;
   1102 
   1103 			error = ntfs_readattr(ntmp, ip,
   1104 				(cn == 0) ? NTFS_A_INDXROOT : NTFS_A_INDX,
   1105 				"$I30", ntfs_cntob(cn), rdsize, rdbuf, NULL);
   1106 			if (error)
   1107 				goto fail;
   1108 
   1109 			if (cn != 0) {
   1110 				error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
   1111 						rdbuf, rdsize);
   1112 				if (error)
   1113 					goto fail;
   1114 			}
   1115 
   1116 			tctx = lookup_ctx;
   1117 			lookup_ctx = lookup_ctx->prev;
   1118 			FREE(tctx, M_TEMP);
   1119 		} else {
   1120 			dprintf(("ntfs_ntlookupfile: nowhere to dive :-(\n"));
   1121 			error = ENOENT;
   1122 			break;
   1123 		}
   1124 	} while (1);
   1125 
   1126 	/* perform full scan if no entry was found */
   1127 	if (!fullscan && error == ENOENT) {
   1128 		fullscan = 1;
   1129 		cn = 0;		/* need zero, used by lookup_ctx */
   1130 
   1131 		ddprintf(("ntfs_ntlookupfile: fullscan performed for: %.*s\n",
   1132 			(int) fnamelen, fname));
   1133 		goto loop;
   1134 	}
   1135 
   1136 	dprintf(("finish\n"));
   1137 
   1138 fail:
   1139 	if (attrname)
   1140 		FREE(attrname, M_TEMP);
   1141 	if (lookup_ctx) {
   1142 		while(lookup_ctx) {
   1143 			tctx = lookup_ctx;
   1144 			lookup_ctx = lookup_ctx->prev;
   1145 			FREE(tctx, M_TEMP);
   1146 		}
   1147 	}
   1148 	ntfs_ntvattrrele(vap);
   1149 	ntfs_ntput(ip);
   1150 	free(rdbuf, M_TEMP);
   1151 	return (error);
   1152 }
   1153 
   1154 /*
   1155  * Check if name type is permitted to show.
   1156  */
   1157 int
   1158 ntfs_isnamepermitted(
   1159 		     struct ntfsmount * ntmp,
   1160 		     struct attr_indexentry * iep)
   1161 {
   1162 	if (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)
   1163 		return 1;
   1164 
   1165 	switch (iep->ie_fnametype) {
   1166 	case 2:
   1167 		ddprintf(("ntfs_isnamepermitted: skipped DOS name\n"));
   1168 		return 0;
   1169 	case 0: case 1: case 3:
   1170 		return 1;
   1171 	default:
   1172 		printf("ntfs_isnamepermitted: "
   1173 		    "WARNING! Unknown file name type: %d\n",
   1174 		    iep->ie_fnametype);
   1175 		break;
   1176 	}
   1177 	return 0;
   1178 }
   1179 
   1180 /*
   1181  * Read ntfs dir like stream of attr_indexentry, not like btree of them.
   1182  * This is done by scanning $BITMAP:$I30 for busy clusters and reading them.
   1183  * Of course $INDEX_ROOT:$I30 is read before. Last read values are stored in
   1184  * fnode, so we can skip toward record number num almost immediately.
   1185  * Anyway this is rather slow routine. The problem is that we don't know
   1186  * how many records are there in $INDEX_ALLOCATION:$I30 block.
   1187  */
   1188 int
   1189 ntfs_ntreaddir(
   1190 	       struct ntfsmount * ntmp,
   1191 	       struct fnode * fp,
   1192 	       u_int32_t num,
   1193 	       struct attr_indexentry ** riepp)
   1194 {
   1195 	struct ntnode  *ip = FTONT(fp);
   1196 	struct ntvattr *vap = NULL;	/* IndexRoot attribute */
   1197 	struct ntvattr *bmvap = NULL;	/* BitMap attribute */
   1198 	struct ntvattr *iavap = NULL;	/* IndexAllocation attribute */
   1199 	caddr_t         rdbuf;		/* Buffer to read directory's blocks  */
   1200 	u_char         *bmp = NULL;	/* Bitmap */
   1201 	u_int32_t       blsize;		/* Index allocation size (2048) */
   1202 	u_int32_t       rdsize;		/* Length of data to read */
   1203 	u_int32_t       attrnum;	/* Current attribute type */
   1204 	u_int32_t       cpbl = 1;	/* Clusters per directory block */
   1205 	u_int32_t       blnum;
   1206 	struct attr_indexentry *iep;
   1207 	int             error = ENOENT;
   1208 	u_int32_t       aoff, cnum;
   1209 
   1210 	dprintf(("ntfs_ntreaddir: read ino: %llu, num: %d\n",
   1211 	    (unsigned long long)ip->i_number, num));
   1212 	error = ntfs_ntget(ip);
   1213 	if (error)
   1214 		return (error);
   1215 
   1216 	error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
   1217 	if (error)
   1218 		return (ENOTDIR);
   1219 
   1220 	if (fp->f_dirblbuf == NULL) {
   1221 		fp->f_dirblsz = vap->va_a_iroot->ir_size;
   1222 		fp->f_dirblbuf = (caddr_t) malloc(
   1223 		       MAX(vap->va_datalen,fp->f_dirblsz), M_NTFSDIR, M_WAITOK);
   1224 	}
   1225 
   1226 	blsize = fp->f_dirblsz;
   1227 	rdbuf = fp->f_dirblbuf;
   1228 
   1229 	dprintf(("ntfs_ntreaddir: rdbuf: %p, blsize: %d\n", rdbuf, blsize));
   1230 
   1231 	if (vap->va_a_iroot->ir_flag & NTFS_IRFLAG_INDXALLOC) {
   1232 		error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXBITMAP, "$I30",
   1233 					0, &bmvap);
   1234 		if (error) {
   1235 			error = ENOTDIR;
   1236 			goto fail;
   1237 		}
   1238 		bmp = (u_char *) malloc(bmvap->va_datalen, M_TEMP, M_WAITOK);
   1239 		error = ntfs_readattr(ntmp, ip, NTFS_A_INDXBITMAP, "$I30", 0,
   1240 				       bmvap->va_datalen, bmp, NULL);
   1241 		if (error)
   1242 			goto fail;
   1243 
   1244 		error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDX, "$I30",
   1245 					0, &iavap);
   1246 		if (error) {
   1247 			error = ENOTDIR;
   1248 			goto fail;
   1249 		}
   1250 		cpbl = ntfs_btocn(blsize + ntfs_cntob(1) - 1);
   1251 		dprintf(("ntfs_ntreaddir: indexalloc: %qu, cpbl: %d\n",
   1252 			 (long long)iavap->va_datalen, cpbl));
   1253 	} else {
   1254 		dprintf(("ntfs_ntreadidir: w/o BitMap and IndexAllocation\n"));
   1255 		iavap = bmvap = NULL;
   1256 		bmp = NULL;
   1257 	}
   1258 
   1259 	/* Try use previous values */
   1260 	if ((fp->f_lastdnum < num) && (fp->f_lastdnum != 0)) {
   1261 		attrnum = fp->f_lastdattr;
   1262 		aoff = fp->f_lastdoff;
   1263 		blnum = fp->f_lastdblnum;
   1264 		cnum = fp->f_lastdnum;
   1265 	} else {
   1266 		attrnum = NTFS_A_INDXROOT;
   1267 		aoff = sizeof(struct attr_indexroot);
   1268 		blnum = 0;
   1269 		cnum = 0;
   1270 	}
   1271 
   1272 	do {
   1273 		dprintf(("ntfs_ntreaddir: scan: 0x%x, %d, %d, %d, %d\n",
   1274 			 attrnum, (u_int32_t) blnum, cnum, num, aoff));
   1275 		rdsize = (attrnum == NTFS_A_INDXROOT) ? vap->va_datalen : blsize;
   1276 		error = ntfs_readattr(ntmp, ip, attrnum, "$I30",
   1277 				ntfs_cntob(blnum * cpbl), rdsize, rdbuf, NULL);
   1278 		if (error)
   1279 			goto fail;
   1280 
   1281 		if (attrnum == NTFS_A_INDX) {
   1282 			error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
   1283 						rdbuf, rdsize);
   1284 			if (error)
   1285 				goto fail;
   1286 		}
   1287 		if (aoff == 0)
   1288 			aoff = (attrnum == NTFS_A_INDX) ?
   1289 				(0x18 + ((struct attr_indexalloc *) rdbuf)->ia_hdrsize) :
   1290 				sizeof(struct attr_indexroot);
   1291 
   1292 		iep = (struct attr_indexentry *) (rdbuf + aoff);
   1293 		for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
   1294 			aoff += iep->reclen,
   1295 			iep = (struct attr_indexentry *) (rdbuf + aoff))
   1296 		{
   1297 			if (!ntfs_isnamepermitted(ntmp, iep)) continue;
   1298 
   1299 			if (cnum >= num) {
   1300 				fp->f_lastdnum = cnum;
   1301 				fp->f_lastdoff = aoff;
   1302 				fp->f_lastdblnum = blnum;
   1303 				fp->f_lastdattr = attrnum;
   1304 
   1305 				*riepp = iep;
   1306 
   1307 				error = 0;
   1308 				goto fail;
   1309 			}
   1310 			cnum++;
   1311 		}
   1312 
   1313 		if (iavap) {
   1314 			if (attrnum == NTFS_A_INDXROOT)
   1315 				blnum = 0;
   1316 			else
   1317 				blnum++;
   1318 
   1319 			while (ntfs_cntob(blnum * cpbl) < iavap->va_datalen) {
   1320 				if (bmp[blnum >> 3] & (1 << (blnum & 3)))
   1321 					break;
   1322 				blnum++;
   1323 			}
   1324 
   1325 			attrnum = NTFS_A_INDX;
   1326 			aoff = 0;
   1327 			if (ntfs_cntob(blnum * cpbl) >= iavap->va_datalen)
   1328 				break;
   1329 			dprintf(("ntfs_ntreaddir: blnum: %d\n", (u_int32_t) blnum));
   1330 		}
   1331 	} while (iavap);
   1332 
   1333 	*riepp = NULL;
   1334 	fp->f_lastdnum = 0;
   1335 
   1336 fail:
   1337 	if (vap)
   1338 		ntfs_ntvattrrele(vap);
   1339 	if (bmvap)
   1340 		ntfs_ntvattrrele(bmvap);
   1341 	if (iavap)
   1342 		ntfs_ntvattrrele(iavap);
   1343 	if (bmp)
   1344 		FREE(bmp, M_TEMP);
   1345 	ntfs_ntput(ip);
   1346 	return (error);
   1347 }
   1348 
   1349 /*
   1350  * Convert NTFS times that are in 100 ns units and begins from
   1351  * 1601 Jan 1 into unix times.
   1352  */
   1353 struct timespec
   1354 ntfs_nttimetounix(
   1355 		  u_int64_t nt)
   1356 {
   1357 	struct timespec t;
   1358 
   1359 	/* WindowNT times are in 100 ns and from 1601 Jan 1 */
   1360 	t.tv_nsec = (nt % (1000 * 1000 * 10)) * 100;
   1361 	t.tv_sec = nt / (1000 * 1000 * 10) -
   1362 		369LL * 365LL * 24LL * 60LL * 60LL -
   1363 		89LL * 1LL * 24LL * 60LL * 60LL;
   1364 	return (t);
   1365 }
   1366 
   1367 /*
   1368  * Get file times from NTFS_A_NAME attribute.
   1369  */
   1370 int
   1371 ntfs_times(
   1372 	   struct ntfsmount * ntmp,
   1373 	   struct ntnode * ip,
   1374 	   ntfs_times_t * tm)
   1375 {
   1376 	struct ntvattr *vap;
   1377 	int             error;
   1378 
   1379 	dprintf(("ntfs_times: ino: %llu...\n",
   1380 	    (unsigned long long)ip->i_number));
   1381 
   1382 	error = ntfs_ntget(ip);
   1383 	if (error)
   1384 		return (error);
   1385 
   1386 	error = ntfs_ntvattrget(ntmp, ip, NTFS_A_NAME, NULL, 0, &vap);
   1387 	if (error) {
   1388 		ntfs_ntput(ip);
   1389 		return (error);
   1390 	}
   1391 	*tm = vap->va_a_name->n_times;
   1392 	ntfs_ntvattrrele(vap);
   1393 	ntfs_ntput(ip);
   1394 
   1395 	return (0);
   1396 }
   1397 
   1398 /*
   1399  * Get file sizes from corresponding attribute.
   1400  *
   1401  * ntnode under fnode should be locked.
   1402  */
   1403 int
   1404 ntfs_filesize(
   1405 	      struct ntfsmount * ntmp,
   1406 	      struct fnode * fp,
   1407 	      u_int64_t * size,
   1408 	      u_int64_t * bytes)
   1409 {
   1410 	struct ntvattr *vap;
   1411 	struct ntnode *ip = FTONT(fp);
   1412 	u_int64_t       sz, bn;
   1413 	int             error;
   1414 
   1415 	dprintf(("ntfs_filesize: ino: %llu\n",
   1416 	    (unsigned long long)ip->i_number));
   1417 
   1418 	error = ntfs_ntvattrget(ntmp, ip,
   1419 		fp->f_attrtype, fp->f_attrname, 0, &vap);
   1420 	if (error)
   1421 		return (error);
   1422 
   1423 	bn = vap->va_allocated;
   1424 	sz = vap->va_datalen;
   1425 
   1426 	dprintf(("ntfs_filesize: %d bytes (%d bytes allocated)\n",
   1427 		(u_int32_t) sz, (u_int32_t) bn));
   1428 
   1429 	if (size)
   1430 		*size = sz;
   1431 	if (bytes)
   1432 		*bytes = bn;
   1433 
   1434 	ntfs_ntvattrrele(vap);
   1435 
   1436 	return (0);
   1437 }
   1438 
   1439 /*
   1440  * This is one of write routine.
   1441  */
   1442 int
   1443 ntfs_writeattr_plain(
   1444 	struct ntfsmount * ntmp,
   1445 	struct ntnode * ip,
   1446 	u_int32_t attrnum,
   1447 	char *attrname,
   1448 	off_t roff,
   1449 	size_t rsize,
   1450 	void *rdata,
   1451 	size_t * initp,
   1452 	struct uio *uio)
   1453 {
   1454 	size_t          init;
   1455 	int             error = 0;
   1456 	off_t           off = roff, left = rsize, towrite;
   1457 	caddr_t         data = rdata;
   1458 	struct ntvattr *vap;
   1459 	*initp = 0;
   1460 
   1461 	while (left) {
   1462 		error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
   1463 					ntfs_btocn(off), &vap);
   1464 		if (error)
   1465 			return (error);
   1466 		towrite = MIN(left, ntfs_cntob(vap->va_vcnend + 1) - off);
   1467 		ddprintf(("ntfs_writeattr_plain: o: %qd, s: %qd (%qu - %qu)\n",
   1468 			 (long long) off, (long long) towrite,
   1469 			 (long long) vap->va_vcnstart,
   1470 			 (long long) vap->va_vcnend));
   1471 		error = ntfs_writentvattr_plain(ntmp, ip, vap,
   1472 					 off - ntfs_cntob(vap->va_vcnstart),
   1473 					 towrite, data, &init, uio);
   1474 		if (error) {
   1475 			dprintf(("ntfs_writeattr_plain: "
   1476 			    "ntfs_writentvattr_plain failed: o: %qd, s: %qd\n",
   1477 			    (long long) off, (long long) towrite));
   1478 			dprintf(("ntfs_writeattr_plain: attrib: %qu - %qu\n",
   1479 			       (long long) vap->va_vcnstart,
   1480 			       (long long) vap->va_vcnend));
   1481 			ntfs_ntvattrrele(vap);
   1482 			break;
   1483 		}
   1484 		ntfs_ntvattrrele(vap);
   1485 		left -= towrite;
   1486 		off += towrite;
   1487 		data = data + towrite;
   1488 		*initp += init;
   1489 	}
   1490 
   1491 	return (error);
   1492 }
   1493 
   1494 /*
   1495  * This is one of write routine.
   1496  *
   1497  * ntnode should be locked.
   1498  */
   1499 int
   1500 ntfs_writentvattr_plain(
   1501 	struct ntfsmount * ntmp,
   1502 	struct ntnode * ip,
   1503 	struct ntvattr * vap,
   1504 	off_t roff,
   1505 	size_t rsize,
   1506 	void *rdata,
   1507 	size_t * initp,
   1508 	struct uio *uio)
   1509 {
   1510 	int             error = 0;
   1511 	off_t           off;
   1512 	int             cnt;
   1513 	cn_t            ccn, ccl, cn, left, cl;
   1514 	caddr_t         data = rdata;
   1515 	struct buf     *bp;
   1516 	size_t          tocopy;
   1517 
   1518 	*initp = 0;
   1519 
   1520 	if ((vap->va_flag & NTFS_AF_INRUN) == 0) {
   1521 		dprintf(("ntfs_writevattr_plain: CAN'T WRITE RES. ATTRIBUTE\n"));
   1522 		return ENOTTY;
   1523 	}
   1524 
   1525 	ddprintf(("ntfs_writentvattr_plain: data in run: %lu chains\n",
   1526 		 vap->va_vruncnt));
   1527 
   1528 	off = roff;
   1529 	left = rsize;
   1530 	ccl = 0;
   1531 	ccn = 0;
   1532 	cnt = 0;
   1533 	for (; left && (cnt < vap->va_vruncnt); cnt++) {
   1534 		ccn = vap->va_vruncn[cnt];
   1535 		ccl = vap->va_vruncl[cnt];
   1536 
   1537 		ddprintf(("ntfs_writentvattr_plain: "
   1538 		    "left %qu, cn: 0x%qx, cl: %qu, off: %qd\n",
   1539 		    (long long) left, (long long) ccn,
   1540 		    (long long) ccl, (long long) off));
   1541 
   1542 		if (ntfs_cntob(ccl) < off) {
   1543 			off -= ntfs_cntob(ccl);
   1544 			cnt++;
   1545 			continue;
   1546 		}
   1547 		if (!ccn && ip->i_number != NTFS_BOOTINO)
   1548 			continue; /* XXX */
   1549 
   1550 		ccl -= ntfs_btocn(off);
   1551 		cn = ccn + ntfs_btocn(off);
   1552 		off = ntfs_btocnoff(off);
   1553 
   1554 		while (left && ccl) {
   1555 			/*
   1556 			 * Always read and write single clusters at a time -
   1557 			 * we need to avoid requesting differently-sized
   1558 			 * blocks at the same disk offsets to avoid
   1559 			 * confusing the buffer cache.
   1560 			 */
   1561 			tocopy = MIN(left, ntfs_cntob(1) - off);
   1562 			cl = ntfs_btocl(tocopy + off);
   1563 			KASSERT(cl == 1 && tocopy <= ntfs_cntob(1));
   1564 			ddprintf(("ntfs_writentvattr_plain: write: "
   1565 				"cn: 0x%qx cl: %qu, off: %qd len: %qu, left: %qu\n",
   1566 				(long long) cn, (long long) cl,
   1567 				(long long) off, (long long) tocopy,
   1568 				(long long) left));
   1569 			if ((off == 0) && (tocopy == ntfs_cntob(cl)))
   1570 			{
   1571 				bp = getblk(ntmp->ntm_devvp, ntfs_cntobn(cn),
   1572 					    ntfs_cntob(cl), 0, 0);
   1573 				clrbuf(bp);
   1574 			} else {
   1575 				error = bread(ntmp->ntm_devvp, ntfs_cntobn(cn),
   1576 					      ntfs_cntob(cl), NOCRED, &bp);
   1577 				if (error) {
   1578 					brelse(bp);
   1579 					return (error);
   1580 				}
   1581 			}
   1582 			if (uio)
   1583 				uiomove(bp->b_data + off, tocopy, uio);
   1584 			else
   1585 				memcpy(bp->b_data + off, data, tocopy);
   1586 			bawrite(bp);
   1587 			data = data + tocopy;
   1588 			*initp += tocopy;
   1589 			off = 0;
   1590 			left -= tocopy;
   1591 			cn += cl;
   1592 			ccl -= cl;
   1593 		}
   1594 	}
   1595 
   1596 	if (left) {
   1597 		printf("ntfs_writentvattr_plain: POSSIBLE RUN ERROR\n");
   1598 		error = EINVAL;
   1599 	}
   1600 
   1601 	return (error);
   1602 }
   1603 
   1604 /*
   1605  * This is one of read routines.
   1606  *
   1607  * ntnode should be locked.
   1608  */
   1609 int
   1610 ntfs_readntvattr_plain(
   1611 	struct ntfsmount * ntmp,
   1612 	struct ntnode * ip,
   1613 	struct ntvattr * vap,
   1614 	off_t roff,
   1615 	size_t rsize,
   1616 	void *rdata,
   1617 	size_t * initp,
   1618 	struct uio *uio)
   1619 {
   1620 	int             error = 0;
   1621 	off_t           off;
   1622 
   1623 	*initp = 0;
   1624 	if (vap->va_flag & NTFS_AF_INRUN) {
   1625 		int             cnt;
   1626 		cn_t            ccn, ccl, cn, left, cl;
   1627 		caddr_t         data = rdata;
   1628 		struct buf     *bp;
   1629 		size_t          tocopy;
   1630 
   1631 		ddprintf(("ntfs_readntvattr_plain: data in run: %lu chains\n",
   1632 			 vap->va_vruncnt));
   1633 
   1634 		off = roff;
   1635 		left = rsize;
   1636 		ccl = 0;
   1637 		ccn = 0;
   1638 		cnt = 0;
   1639 		while (left && (cnt < vap->va_vruncnt)) {
   1640 			ccn = vap->va_vruncn[cnt];
   1641 			ccl = vap->va_vruncl[cnt];
   1642 
   1643 			ddprintf(("ntfs_readntvattr_plain: "
   1644 				 "left %qu, cn: 0x%qx, cl: %qu, off: %qd\n",
   1645 				 (long long) left, (long long) ccn,
   1646 				 (long long) ccl, (long long) off));
   1647 
   1648 			if (ntfs_cntob(ccl) < off) {
   1649 				off -= ntfs_cntob(ccl);
   1650 				cnt++;
   1651 				continue;
   1652 			}
   1653 			if (ccn || ip->i_number == NTFS_BOOTINO) {
   1654 				ccl -= ntfs_btocn(off);
   1655 				cn = ccn + ntfs_btocn(off);
   1656 				off = ntfs_btocnoff(off);
   1657 
   1658 				while (left && ccl) {
   1659 					/*
   1660 					 * Always read single clusters at a
   1661 					 * time - we need to avoid reading
   1662 					 * differently-sized blocks at the
   1663 					 * same disk offsets to avoid
   1664 					 * confusing the buffer cache.
   1665 					 */
   1666 					tocopy = MIN(left,
   1667 					    ntfs_cntob(1) - off);
   1668 					cl = ntfs_btocl(tocopy + off);
   1669 					KASSERT(cl == 1 &&
   1670 					    tocopy <= ntfs_cntob(1));
   1671 
   1672 					ddprintf(("ntfs_readntvattr_plain: "
   1673 						"read: cn: 0x%qx cl: %qu, "
   1674 						"off: %qd len: %qu, left: %qu\n",
   1675 						(long long) cn,
   1676 						(long long) cl,
   1677 						(long long) off,
   1678 						(long long) tocopy,
   1679 						(long long) left));
   1680 					error = bread(ntmp->ntm_devvp,
   1681 						      ntfs_cntobn(cn),
   1682 						      ntfs_cntob(cl),
   1683 						      NOCRED, &bp);
   1684 					if (error) {
   1685 						brelse(bp);
   1686 						return (error);
   1687 					}
   1688 					if (uio) {
   1689 						uiomove(bp->b_data + off,
   1690 							tocopy, uio);
   1691 					} else {
   1692 						memcpy(data, bp->b_data + off,
   1693 							tocopy);
   1694 					}
   1695 					brelse(bp);
   1696 					data = data + tocopy;
   1697 					*initp += tocopy;
   1698 					off = 0;
   1699 					left -= tocopy;
   1700 					cn += cl;
   1701 					ccl -= cl;
   1702 				}
   1703 			} else {
   1704 				tocopy = MIN(left, ntfs_cntob(ccl) - off);
   1705 				ddprintf(("ntfs_readntvattr_plain: "
   1706 					"hole: ccn: 0x%qx ccl: %qu, off: %qd, "
   1707 					" len: %qu, left: %qu\n",
   1708 					(long long) ccn, (long long) ccl,
   1709 					(long long) off, (long long) tocopy,
   1710 					(long long) left));
   1711 				left -= tocopy;
   1712 				off = 0;
   1713 				if (uio) {
   1714 					char vbuf[] = "";
   1715 					size_t remains = tocopy;
   1716 					for(; remains; remains--)
   1717 						uiomove(vbuf, 1, uio);
   1718 				} else
   1719 					bzero(data, tocopy);
   1720 				data = data + tocopy;
   1721 			}
   1722 			cnt++;
   1723 		}
   1724 		if (left) {
   1725 			printf("ntfs_readntvattr_plain: POSSIBLE RUN ERROR\n");
   1726 			error = E2BIG;
   1727 		}
   1728 	} else {
   1729 		ddprintf(("ntfs_readnvattr_plain: data is in mft record\n"));
   1730 		if (uio)
   1731 			uiomove(vap->va_datap + roff, rsize, uio);
   1732 		else
   1733 			memcpy(rdata, vap->va_datap + roff, rsize);
   1734 		*initp += rsize;
   1735 	}
   1736 
   1737 	return (error);
   1738 }
   1739 
   1740 /*
   1741  * This is one of read routines.
   1742  */
   1743 int
   1744 ntfs_readattr_plain(
   1745 	struct ntfsmount * ntmp,
   1746 	struct ntnode * ip,
   1747 	u_int32_t attrnum,
   1748 	const char *attrname,
   1749 	off_t roff,
   1750 	size_t rsize,
   1751 	void *rdata,
   1752 	size_t * initp,
   1753 	struct uio *uio)
   1754 {
   1755 	size_t          init;
   1756 	int             error = 0;
   1757 	off_t           off = roff, left = rsize, toread;
   1758 	caddr_t         data = rdata;
   1759 	struct ntvattr *vap;
   1760 	*initp = 0;
   1761 
   1762 	while (left) {
   1763 		error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
   1764 					ntfs_btocn(off), &vap);
   1765 		if (error)
   1766 			return (error);
   1767 		toread = MIN(left, ntfs_cntob(vap->va_vcnend + 1) - off);
   1768 		ddprintf(("ntfs_readattr_plain: o: %qd, s: %qd (%qu - %qu)\n",
   1769 			 (long long) off, (long long) toread,
   1770 			 (long long) vap->va_vcnstart,
   1771 			 (long long) vap->va_vcnend));
   1772 		error = ntfs_readntvattr_plain(ntmp, ip, vap,
   1773 					 off - ntfs_cntob(vap->va_vcnstart),
   1774 					 toread, data, &init, uio);
   1775 		if (error) {
   1776 			printf("ntfs_readattr_plain: "
   1777 			       "ntfs_readntvattr_plain failed: o: %qd, s: %qd\n",
   1778 			       (long long) off, (long long) toread);
   1779 			printf("ntfs_readattr_plain: attrib: %qu - %qu\n",
   1780 			       (long long) vap->va_vcnstart,
   1781 			       (long long) vap->va_vcnend);
   1782 			ntfs_ntvattrrele(vap);
   1783 			break;
   1784 		}
   1785 		ntfs_ntvattrrele(vap);
   1786 		left -= toread;
   1787 		off += toread;
   1788 		data = data + toread;
   1789 		*initp += init;
   1790 	}
   1791 
   1792 	return (error);
   1793 }
   1794 
   1795 /*
   1796  * This is one of read routines.
   1797  */
   1798 int
   1799 ntfs_readattr(
   1800 	struct ntfsmount * ntmp,
   1801 	struct ntnode * ip,
   1802 	u_int32_t attrnum,
   1803 	const char *attrname,
   1804 	off_t roff,
   1805 	size_t rsize,
   1806 	void *rdata,
   1807 	struct uio *uio)
   1808 {
   1809 	int             error = 0;
   1810 	struct ntvattr *vap;
   1811 	size_t          init;
   1812 
   1813 	ddprintf(("ntfs_readattr: reading %llu: 0x%x, from %qd size %qu"
   1814 	    " bytes\n", (unsigned long long)ip->i_number, attrnum,
   1815 	    (long long)roff, (long long)rsize));
   1816 
   1817 	error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname, 0, &vap);
   1818 	if (error)
   1819 		return (error);
   1820 
   1821 	if ((roff > vap->va_datalen) ||
   1822 	    (roff + rsize > vap->va_datalen)) {
   1823 		printf("ntfs_readattr: offset too big: %qd (%qd) > %qu\n",
   1824 			(long long) roff, (long long) (roff + rsize),
   1825 			(long long) vap->va_datalen);
   1826 		ntfs_ntvattrrele(vap);
   1827 		return (E2BIG);
   1828 	}
   1829 	if (vap->va_compression && vap->va_compressalg) {
   1830 		u_int8_t       *cup;
   1831 		u_int8_t       *uup;
   1832 		off_t           off = roff, left = rsize, tocopy;
   1833 		caddr_t         data = rdata;
   1834 		cn_t            cn;
   1835 
   1836 		ddprintf(("ntfs_ntreadattr: compression: %d\n",
   1837 			 vap->va_compressalg));
   1838 
   1839 		cup = malloc(ntfs_cntob(NTFS_COMPUNIT_CL),
   1840 		       M_NTFSDECOMP, M_WAITOK);
   1841 		uup = malloc(ntfs_cntob(NTFS_COMPUNIT_CL),
   1842 		       M_NTFSDECOMP, M_WAITOK);
   1843 
   1844 		cn = (ntfs_btocn(roff)) & (~(NTFS_COMPUNIT_CL - 1));
   1845 		off = roff - ntfs_cntob(cn);
   1846 
   1847 		while (left) {
   1848 			error = ntfs_readattr_plain(ntmp, ip, attrnum,
   1849 						  attrname, ntfs_cntob(cn),
   1850 					          ntfs_cntob(NTFS_COMPUNIT_CL),
   1851 						  cup, &init, NULL);
   1852 			if (error)
   1853 				break;
   1854 
   1855 			tocopy = MIN(left, ntfs_cntob(NTFS_COMPUNIT_CL) - off);
   1856 
   1857 			if (init == ntfs_cntob(NTFS_COMPUNIT_CL)) {
   1858 				if (uio)
   1859 					uiomove(cup + off, tocopy, uio);
   1860 				else
   1861 					memcpy(data, cup + off, tocopy);
   1862 			} else if (init == 0) {
   1863 				if (uio) {
   1864 					char vbuf[] = "";
   1865 					size_t remains = tocopy;
   1866 					for(; remains; remains--)
   1867 						uiomove(vbuf, 1, uio);
   1868 				}
   1869 				else
   1870 					bzero(data, tocopy);
   1871 			} else {
   1872 				error = ntfs_uncompunit(ntmp, uup, cup);
   1873 				if (error)
   1874 					break;
   1875 				if (uio)
   1876 					uiomove(uup + off, tocopy, uio);
   1877 				else
   1878 					memcpy(data, uup + off, tocopy);
   1879 			}
   1880 
   1881 			left -= tocopy;
   1882 			data = data + tocopy;
   1883 			off += tocopy - ntfs_cntob(NTFS_COMPUNIT_CL);
   1884 			cn += NTFS_COMPUNIT_CL;
   1885 		}
   1886 
   1887 		FREE(uup, M_NTFSDECOMP);
   1888 		FREE(cup, M_NTFSDECOMP);
   1889 	} else
   1890 		error = ntfs_readattr_plain(ntmp, ip, attrnum, attrname,
   1891 					     roff, rsize, rdata, &init, uio);
   1892 	ntfs_ntvattrrele(vap);
   1893 	return (error);
   1894 }
   1895 
   1896 #if UNUSED_CODE
   1897 int
   1898 ntfs_parserun(
   1899 	      cn_t * cn,
   1900 	      cn_t * cl,
   1901 	      u_int8_t * run,
   1902 	      u_long len,
   1903 	      u_long *off)
   1904 {
   1905 	u_int8_t        sz;
   1906 	int             i;
   1907 
   1908 	if (NULL == run) {
   1909 		printf("ntfs_parsetun: run == NULL\n");
   1910 		return (EINVAL);
   1911 	}
   1912 	sz = run[(*off)++];
   1913 	if (0 == sz) {
   1914 		printf("ntfs_parserun: trying to go out of run\n");
   1915 		return (E2BIG);
   1916 	}
   1917 	*cl = 0;
   1918 	if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
   1919 		printf("ntfs_parserun: "
   1920 		       "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
   1921 		       sz, len, *off);
   1922 		return (EINVAL);
   1923 	}
   1924 	for (i = 0; i < (sz & 0xF); i++)
   1925 		*cl += (u_int32_t) run[(*off)++] << (i << 3);
   1926 
   1927 	sz >>= 4;
   1928 	if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
   1929 		printf("ntfs_parserun: "
   1930 		       "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
   1931 		       sz, len, *off);
   1932 		return (EINVAL);
   1933 	}
   1934 	for (i = 0; i < (sz & 0xF); i++)
   1935 		*cn += (u_int32_t) run[(*off)++] << (i << 3);
   1936 
   1937 	return (0);
   1938 }
   1939 #endif
   1940 
   1941 /*
   1942  * Process fixup routine on given buffer.
   1943  */
   1944 int
   1945 ntfs_procfixups(
   1946 		struct ntfsmount * ntmp,
   1947 		u_int32_t magic,
   1948 		caddr_t xbuf,
   1949 		size_t len)
   1950 {
   1951 	struct fixuphdr *fhp = (struct fixuphdr *) xbuf;
   1952 	int             i;
   1953 	u_int16_t       fixup;
   1954 	u_int16_t      *fxp;
   1955 	u_int16_t      *cfxp;
   1956 
   1957 	if (fhp->fh_magic != magic) {
   1958 		printf("ntfs_procfixups: magic doesn't match: %08x != %08x\n",
   1959 		       fhp->fh_magic, magic);
   1960 		return (EINVAL);
   1961 	}
   1962 	if ((fhp->fh_fnum - 1) * ntmp->ntm_bps != len) {
   1963 		printf("ntfs_procfixups: "
   1964 		       "bad fixups number: %d for %ld bytes block\n",
   1965 		       fhp->fh_fnum, (long)len);	/* XXX printf kludge */
   1966 		return (EINVAL);
   1967 	}
   1968 	if (fhp->fh_foff >= ntmp->ntm_spc * ntmp->ntm_mftrecsz * ntmp->ntm_bps) {
   1969 		printf("ntfs_procfixups: invalid offset: %x", fhp->fh_foff);
   1970 		return (EINVAL);
   1971 	}
   1972 	fxp = (u_int16_t *) (xbuf + fhp->fh_foff);
   1973 	cfxp = (u_int16_t *) (xbuf + ntmp->ntm_bps - 2);
   1974 	fixup = *fxp++;
   1975 	for (i = 1; i < fhp->fh_fnum; i++, fxp++) {
   1976 		if (*cfxp != fixup) {
   1977 			printf("ntfs_procfixups: fixup %d doesn't match\n", i);
   1978 			return (EINVAL);
   1979 		}
   1980 		*cfxp = *fxp;
   1981 		cfxp = (u_int16_t *)((caddr_t)cfxp + ntmp->ntm_bps);
   1982 	}
   1983 	return (0);
   1984 }
   1985 
   1986 #if UNUSED_CODE
   1987 int
   1988 ntfs_runtocn(
   1989 	     cn_t * cn,
   1990 	     struct ntfsmount * ntmp,
   1991 	     u_int8_t * run,
   1992 	     u_long len,
   1993 	     cn_t vcn)
   1994 {
   1995 	cn_t            ccn = 0;
   1996 	cn_t            ccl = 0;
   1997 	u_long          off = 0;
   1998 	int             error = 0;
   1999 
   2000 #ifdef NTFS_DEBUG
   2001 	int             i;
   2002 	printf("ntfs_runtocn: run: %p, %ld bytes, vcn:%ld\n",
   2003 		run, len, (u_long) vcn);
   2004 	printf("ntfs_runtocn: run: ");
   2005 	for (i = 0; i < len; i++)
   2006 		printf("0x%02x ", run[i]);
   2007 	printf("\n");
   2008 #endif
   2009 
   2010 	if (NULL == run) {
   2011 		printf("ntfs_runtocn: run == NULL\n");
   2012 		return (EINVAL);
   2013 	}
   2014 	do {
   2015 		if (run[off] == 0) {
   2016 			printf("ntfs_runtocn: vcn too big\n");
   2017 			return (E2BIG);
   2018 		}
   2019 		vcn -= ccl;
   2020 		error = ntfs_parserun(&ccn, &ccl, run, len, &off);
   2021 		if (error) {
   2022 			printf("ntfs_runtocn: ntfs_parserun failed\n");
   2023 			return (error);
   2024 		}
   2025 	} while (ccl <= vcn);
   2026 	*cn = ccn + vcn;
   2027 	return (0);
   2028 }
   2029 #endif
   2030 
   2031 /*
   2032  * this initializes toupper table & dependant variables to be ready for
   2033  * later work
   2034  */
   2035 void
   2036 ntfs_toupper_init()
   2037 {
   2038 	ntfs_toupper_tab = (wchar *) NULL;
   2039 	mutex_init(&ntfs_toupper_lock, MUTEX_DEFAULT, IPL_NONE);
   2040 	ntfs_toupper_usecount = 0;
   2041 }
   2042 
   2043 /*
   2044  * if the ntfs_toupper_tab[] is filled already, just raise use count;
   2045  * otherwise read the data from the filesystem we are currently mounting
   2046  */
   2047 int
   2048 ntfs_toupper_use(mp, ntmp)
   2049 	struct mount *mp;
   2050 	struct ntfsmount *ntmp;
   2051 {
   2052 	int error = 0;
   2053 	struct vnode *vp;
   2054 
   2055 	/* get exclusive access */
   2056 	mutex_enter(&ntfs_toupper_lock);
   2057 
   2058 	/* only read the translation data from a file if it hasn't been
   2059 	 * read already */
   2060 	if (ntfs_toupper_tab)
   2061 		goto out;
   2062 
   2063 	/*
   2064 	 * Read in Unicode lowercase -> uppercase translation file.
   2065 	 * XXX for now, just the first 256 entries are used anyway,
   2066 	 * so don't bother reading more
   2067 	 */
   2068 	MALLOC(ntfs_toupper_tab, wchar *, 256 * 256 * sizeof(wchar),
   2069 		M_NTFSRDATA, M_WAITOK);
   2070 
   2071 	if ((error = VFS_VGET(mp, NTFS_UPCASEINO, &vp)))
   2072 		goto out;
   2073 	error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
   2074 			0, 256*256*sizeof(wchar), (char *) ntfs_toupper_tab,
   2075 			NULL);
   2076 	vput(vp);
   2077 
   2078     out:
   2079 	ntfs_toupper_usecount++;
   2080 	mutex_exit(&ntfs_toupper_lock);
   2081 	return (error);
   2082 }
   2083 
   2084 /*
   2085  * lower the use count and if it reaches zero, free the memory
   2086  * tied by toupper table
   2087  */
   2088 void
   2089 ntfs_toupper_unuse()
   2090 {
   2091 	/* get exclusive access */
   2092 	mutex_enter(&ntfs_toupper_lock);
   2093 
   2094 	ntfs_toupper_usecount--;
   2095 	if (ntfs_toupper_usecount == 0) {
   2096 		FREE(ntfs_toupper_tab, M_NTFSRDATA);
   2097 		ntfs_toupper_tab = NULL;
   2098 	}
   2099 #ifdef DIAGNOSTIC
   2100 	else if (ntfs_toupper_usecount < 0) {
   2101 		panic("ntfs_toupper_unuse(): use count negative: %d",
   2102 			ntfs_toupper_usecount);
   2103 	}
   2104 #endif
   2105 
   2106 	/* release the lock */
   2107 	mutex_exit(&ntfs_toupper_lock);
   2108 }
   2109