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