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