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