Home | History | Annotate | Line # | Download | only in ntfs
ntfs_subr.c revision 1.34
      1 /*	$NetBSD: ntfs_subr.c,v 1.34 2008/01/02 11:48:42 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.34 2008/01/02 11:48:42 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 		lockdestroy(&ip->i_lock);
    506 		FREE(ip, M_NTFSNTNODE);
    507 	}
    508 }
    509 
    510 /*
    511  * increment usecount of ntnode
    512  */
    513 void
    514 ntfs_ntref(ip)
    515 	struct ntnode *ip;
    516 {
    517 	mutex_enter(&ip->i_interlock);
    518 	ip->i_usecount++;
    519 	mutex_exit(&ip->i_interlock);
    520 
    521 	dprintf(("ntfs_ntref: ino %llu, usecount: %d\n",
    522 	    (unsigned long long)ip->i_number, ip->i_usecount));
    523 
    524 }
    525 
    526 /*
    527  * Decrement usecount of ntnode.
    528  */
    529 void
    530 ntfs_ntrele(ip)
    531 	struct ntnode *ip;
    532 {
    533 	dprintf(("ntfs_ntrele: rele ntnode %llu: %p, usecount: %d\n",
    534 	    (unsigned long long)ip->i_number, ip, ip->i_usecount));
    535 
    536 	mutex_enter(&ip->i_interlock);
    537 	ip->i_usecount--;
    538 
    539 	if (ip->i_usecount < 0)
    540 		panic("ntfs_ntrele: ino: %llu usecount: %d ",
    541 		    (unsigned long long)ip->i_number, ip->i_usecount);
    542 	mutex_exit(&ip->i_interlock);
    543 }
    544 
    545 /*
    546  * Deallocate all memory allocated for ntvattr
    547  */
    548 void
    549 ntfs_freentvattr(vap)
    550 	struct ntvattr * vap;
    551 {
    552 	if (vap->va_flag & NTFS_AF_INRUN) {
    553 		if (vap->va_vruncn)
    554 			free(vap->va_vruncn, M_NTFSRUN);
    555 		if (vap->va_vruncl)
    556 			free(vap->va_vruncl, M_NTFSRUN);
    557 	} else {
    558 		if (vap->va_datap)
    559 			free(vap->va_datap, M_NTFSRDATA);
    560 	}
    561 	FREE(vap, M_NTFSNTVATTR);
    562 }
    563 
    564 /*
    565  * Convert disk image of attribute into ntvattr structure,
    566  * runs are expanded also.
    567  */
    568 int
    569 ntfs_attrtontvattr(
    570 		   struct ntfsmount * ntmp,
    571 		   struct ntvattr ** rvapp,
    572 		   struct attr * rap)
    573 {
    574 	int             error, i;
    575 	struct ntvattr *vap;
    576 
    577 	error = 0;
    578 	*rvapp = NULL;
    579 
    580 	MALLOC(vap, struct ntvattr *, sizeof(struct ntvattr),
    581 		M_NTFSNTVATTR, M_WAITOK);
    582 	bzero(vap, sizeof(struct ntvattr));
    583 	vap->va_ip = NULL;
    584 	vap->va_flag = rap->a_hdr.a_flag;
    585 	vap->va_type = rap->a_hdr.a_type;
    586 	vap->va_compression = rap->a_hdr.a_compression;
    587 	vap->va_index = rap->a_hdr.a_index;
    588 
    589 	ddprintf(("type: 0x%x, index: %d", vap->va_type, vap->va_index));
    590 
    591 	vap->va_namelen = rap->a_hdr.a_namelen;
    592 	if (rap->a_hdr.a_namelen) {
    593 		wchar *unp = (wchar *)((char *)rap + rap->a_hdr.a_nameoff);
    594 		ddprintf((", name:["));
    595 		for (i = 0; i < vap->va_namelen; i++) {
    596 			vap->va_name[i] = unp[i];
    597 			ddprintf(("%c", vap->va_name[i]));
    598 		}
    599 		ddprintf(("]"));
    600 	}
    601 	if (vap->va_flag & NTFS_AF_INRUN) {
    602 		ddprintf((", nonres."));
    603 		vap->va_datalen = rap->a_nr.a_datalen;
    604 		vap->va_allocated = rap->a_nr.a_allocated;
    605 		vap->va_vcnstart = rap->a_nr.a_vcnstart;
    606 		vap->va_vcnend = rap->a_nr.a_vcnend;
    607 		vap->va_compressalg = rap->a_nr.a_compressalg;
    608 		error = ntfs_runtovrun(&(vap->va_vruncn), &(vap->va_vruncl),
    609 				       &(vap->va_vruncnt),
    610 				       (u_int8_t *) rap + rap->a_nr.a_dataoff);
    611 	} else {
    612 		vap->va_compressalg = 0;
    613 		ddprintf((", res."));
    614 		vap->va_datalen = rap->a_r.a_datalen;
    615 		vap->va_allocated = rap->a_r.a_datalen;
    616 		vap->va_vcnstart = 0;
    617 		vap->va_vcnend = ntfs_btocn(vap->va_allocated);
    618 		vap->va_datap = (void *) malloc(vap->va_datalen,
    619 		       M_NTFSRDATA, M_WAITOK);
    620 		memcpy(vap->va_datap, (char *)rap + rap->a_r.a_dataoff,
    621 		       rap->a_r.a_datalen);
    622 	}
    623 	ddprintf((", len: %qu", (long long)vap->va_datalen));
    624 
    625 	if (error)
    626 		FREE(vap, M_NTFSNTVATTR);
    627 	else
    628 		*rvapp = vap;
    629 
    630 	ddprintf(("\n"));
    631 
    632 	return (error);
    633 }
    634 
    635 /*
    636  * Expand run into more utilizable and more memory eating format.
    637  */
    638 int
    639 ntfs_runtovrun(
    640 	       cn_t ** rcnp,
    641 	       cn_t ** rclp,
    642 	       u_long * rcntp,
    643 	       u_int8_t * run)
    644 {
    645 	u_int32_t       off;
    646 	u_int32_t       sz, i;
    647 	cn_t           *cn;
    648 	cn_t           *cl;
    649 	u_long		cnt;
    650 	cn_t		prev;
    651 	cn_t		tmp;
    652 
    653 	off = 0;
    654 	cnt = 0;
    655 	i = 0;
    656 	while (run[off]) {
    657 		off += (run[off] & 0xF) + ((run[off] >> 4) & 0xF) + 1;
    658 		cnt++;
    659 	}
    660 	cn = (cn_t *) malloc(cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
    661 	cl = (cn_t *) malloc(cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
    662 
    663 	off = 0;
    664 	cnt = 0;
    665 	prev = 0;
    666 	while (run[off]) {
    667 
    668 		sz = run[off++];
    669 		cl[cnt] = 0;
    670 
    671 		for (i = 0; i < (sz & 0xF); i++)
    672 			cl[cnt] += (u_int32_t) run[off++] << (i << 3);
    673 
    674 		sz >>= 4;
    675 		if (run[off + sz - 1] & 0x80) {
    676 			tmp = ((u_int64_t) - 1) << (sz << 3);
    677 			for (i = 0; i < sz; i++)
    678 				tmp |= (u_int64_t) run[off++] << (i << 3);
    679 		} else {
    680 			tmp = 0;
    681 			for (i = 0; i < sz; i++)
    682 				tmp |= (u_int64_t) run[off++] << (i << 3);
    683 		}
    684 		if (tmp)
    685 			prev = cn[cnt] = prev + tmp;
    686 		else
    687 			cn[cnt] = tmp;
    688 
    689 		cnt++;
    690 	}
    691 	*rcnp = cn;
    692 	*rclp = cl;
    693 	*rcntp = cnt;
    694 	return (0);
    695 }
    696 
    697 /*
    698  * Compare unicode and ascii string case insens.
    699  */
    700 static int
    701 ntfs_uastricmp(ntmp, ustr, ustrlen, astr, astrlen)
    702 	struct ntfsmount *ntmp;
    703 	const wchar *ustr;
    704 	size_t ustrlen;
    705 	const char *astr;
    706 	size_t astrlen;
    707 {
    708 	size_t  i;
    709 	int res;
    710 
    711 	for (i = 0; i < ustrlen && astrlen > 0; i++) {
    712 		res = (*ntmp->ntm_wcmp)(NTFS_TOUPPER(ustr[i]),
    713 		    NTFS_TOUPPER((*ntmp->ntm_wget)(&astr, &astrlen)) );
    714 		if (res)
    715 			return res;
    716 	}
    717 
    718 	if (i == ustrlen && astrlen == 0)
    719 		return 0;
    720 	else if (i == ustrlen)
    721 		return -1;
    722 	else
    723 		return 1;
    724 }
    725 
    726 /*
    727  * Compare unicode and ascii string case sens.
    728  */
    729 static int
    730 ntfs_uastrcmp(ntmp, ustr, ustrlen, astr, astrlen)
    731 	struct ntfsmount *ntmp;
    732 	const wchar *ustr;
    733 	size_t ustrlen;
    734 	const char *astr;
    735 	size_t astrlen;
    736 {
    737 	size_t i;
    738 	int res;
    739 
    740 	for (i = 0; (i < ustrlen) && astrlen > 0; i++) {
    741 		res = (*ntmp->ntm_wcmp)(ustr[i],
    742 		     (*ntmp->ntm_wget)(&astr, &astrlen));
    743 		if (res)
    744 			return res;
    745 	}
    746 
    747 	if (i == ustrlen && astrlen == 0)
    748 		return 0;
    749 	else if (i == ustrlen)
    750 		return -1;
    751 	else
    752 		return 1;
    753 }
    754 
    755 /*
    756  * Search fnode in ntnode, if not found allocate and preinitialize.
    757  *
    758  * ntnode should be locked on entry.
    759  */
    760 int
    761 ntfs_fget(
    762     struct ntfsmount *ntmp,
    763     struct ntnode *ip,
    764     int attrtype,
    765     char *attrname,
    766     struct fnode **fpp
    767 )
    768 {
    769 	struct fnode *fp;
    770 
    771 	dprintf(("ntfs_fget: ino: %llu, attrtype: 0x%x, attrname: %s\n",
    772 	    (unsigned long long)ip->i_number, attrtype, attrname?attrname:""));
    773 	*fpp = NULL;
    774 	for (fp = ip->i_fnlist.lh_first; fp != NULL; fp = fp->f_fnlist.le_next){
    775 		dprintf(("ntfs_fget: fnode: attrtype: %d, attrname: %s\n",
    776 			fp->f_attrtype, fp->f_attrname?fp->f_attrname:""));
    777 
    778 		if ((attrtype == fp->f_attrtype) &&
    779 		    ((!attrname && !fp->f_attrname) ||
    780 		     (attrname && fp->f_attrname &&
    781 		      !strcmp(attrname,fp->f_attrname)))){
    782 			dprintf(("ntfs_fget: found existed: %p\n",fp));
    783 			*fpp = fp;
    784 		}
    785 	}
    786 
    787 	if (*fpp)
    788 		return (0);
    789 
    790 	MALLOC(fp, struct fnode *, sizeof(struct fnode), M_NTFSFNODE, M_WAITOK);
    791 	bzero(fp, sizeof(struct fnode));
    792 	dprintf(("ntfs_fget: allocating fnode: %p\n",fp));
    793 
    794 	fp->f_ip = ip;
    795 	fp->f_attrname = attrname;
    796 	if (fp->f_attrname) fp->f_flag |= FN_AATTRNAME;
    797 	fp->f_attrtype = attrtype;
    798 
    799 	ntfs_ntref(ip);
    800 
    801 	LIST_INSERT_HEAD(&ip->i_fnlist, fp, f_fnlist);
    802 
    803 	*fpp = fp;
    804 
    805 	return (0);
    806 }
    807 
    808 /*
    809  * Deallocate fnode, remove it from ntnode's fnode list.
    810  *
    811  * ntnode should be locked.
    812  */
    813 void
    814 ntfs_frele(
    815 	struct fnode *fp)
    816 {
    817 	struct ntnode *ip = FTONT(fp);
    818 
    819 	dprintf(("ntfs_frele: fnode: %p for %llu: %p\n", fp,
    820 	    (unsigned long long)ip->i_number, ip));
    821 
    822 	dprintf(("ntfs_frele: deallocating fnode\n"));
    823 	LIST_REMOVE(fp,f_fnlist);
    824 	if (fp->f_flag & FN_AATTRNAME)
    825 		FREE(fp->f_attrname, M_TEMP);
    826 	if (fp->f_dirblbuf)
    827 		FREE(fp->f_dirblbuf, M_NTFSDIR);
    828 	FREE(fp, M_NTFSFNODE);
    829 	ntfs_ntrele(ip);
    830 }
    831 
    832 /*
    833  * Lookup attribute name in format: [[:$ATTR_TYPE]:$ATTR_NAME],
    834  * $ATTR_TYPE is searched in attrdefs read from $AttrDefs.
    835  * If $ATTR_TYPE not specified, ATTR_A_DATA assumed.
    836  */
    837 static int
    838 ntfs_ntlookupattr(
    839 		struct ntfsmount * ntmp,
    840 		const char * name,
    841 		int namelen,
    842 		int *attrtype,
    843 		char **attrname)
    844 {
    845 	const char *sys;
    846 	size_t syslen, i;
    847 	struct ntvattrdef *adp;
    848 
    849 	if (namelen == 0)
    850 		return (0);
    851 
    852 	if (name[0] == '$') {
    853 		sys = name;
    854 		for (syslen = 0; syslen < namelen; syslen++) {
    855 			if(sys[syslen] == ':') {
    856 				name++;
    857 				namelen--;
    858 				break;
    859 			}
    860 		}
    861 		name += syslen;
    862 		namelen -= syslen;
    863 
    864 		adp = ntmp->ntm_ad;
    865 		for (i = 0; i < ntmp->ntm_adnum; i++, adp++){
    866 			if (syslen != adp->ad_namelen ||
    867 			   strncmp(sys, adp->ad_name, syslen) != 0)
    868 				continue;
    869 
    870 			*attrtype = adp->ad_type;
    871 			goto out;
    872 		}
    873 		return (ENOENT);
    874 	}
    875 
    876     out:
    877 	if (namelen) {
    878 		*attrname = (char *) malloc(namelen, M_TEMP, M_WAITOK);
    879 		memcpy((*attrname), name, namelen);
    880 		(*attrname)[namelen] = '\0';
    881 		*attrtype = NTFS_A_DATA;
    882 	}
    883 
    884 	return (0);
    885 }
    886 
    887 /*
    888  * Lookup specified node for filename, matching cnp,
    889  * return fnode filled.
    890  */
    891 int
    892 ntfs_ntlookupfile(
    893 	      struct ntfsmount * ntmp,
    894 	      struct vnode * vp,
    895 	      struct componentname * cnp,
    896 	      struct vnode ** vpp)
    897 {
    898 	struct fnode   *fp = VTOF(vp);
    899 	struct ntnode  *ip = FTONT(fp);
    900 	struct ntvattr *vap;	/* Root attribute */
    901 	cn_t            cn = 0;	/* VCN in current attribute */
    902 	void *        rdbuf;	/* Buffer to read directory's blocks  */
    903 	u_int32_t       blsize;
    904 	u_int32_t       rdsize;	/* Length of data to read from current block */
    905 	struct attr_indexentry *iep;
    906 	int             error, res, anamelen, fnamelen;
    907 	const char     *fname,*aname;
    908 	u_int32_t       aoff;
    909 	int attrtype = NTFS_A_DATA;
    910 	char *attrname = NULL;
    911 	struct fnode   *nfp;
    912 	struct vnode   *nvp;
    913 	enum vtype	f_type;
    914 	int fullscan = 0;
    915 	struct ntfs_lookup_ctx *lookup_ctx = NULL, *tctx;
    916 
    917 	error = ntfs_ntget(ip);
    918 	if (error)
    919 		return (error);
    920 
    921 	error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
    922 	if (error || (vap->va_flag & NTFS_AF_INRUN))
    923 		return (ENOTDIR);
    924 
    925 	/*
    926 	 * Divide file name into: foofilefoofilefoofile[:attrspec]
    927 	 * Store like this:       fname:fnamelen       [aname:anamelen]
    928 	 */
    929 	fname = cnp->cn_nameptr;
    930 	aname = NULL;
    931 	anamelen = 0;
    932 	for (fnamelen = 0; fnamelen < cnp->cn_namelen; fnamelen++)
    933 		if(fname[fnamelen] == ':') {
    934 			aname = fname + fnamelen + 1;
    935 			anamelen = cnp->cn_namelen - fnamelen - 1;
    936 			dprintf(("ntfs_ntlookupfile: %s (%d), attr: %s (%d)\n",
    937 				fname, fnamelen, aname, anamelen));
    938 			break;
    939 		}
    940 
    941 	blsize = vap->va_a_iroot->ir_size;
    942 	dprintf(("ntfs_ntlookupfile: blksz: %d\n", blsize));
    943 
    944 	rdbuf = (void *) malloc(blsize, M_TEMP, M_WAITOK);
    945 
    946     loop:
    947 	rdsize = vap->va_datalen;
    948 	dprintf(("ntfs_ntlookupfile: rdsz: %d\n", rdsize));
    949 
    950 	error = ntfs_readattr(ntmp, ip, NTFS_A_INDXROOT, "$I30",
    951 			       0, rdsize, rdbuf, NULL);
    952 	if (error)
    953 		goto fail;
    954 
    955 	aoff = sizeof(struct attr_indexroot);
    956 
    957 	do {
    958 		iep = (struct attr_indexentry *) ((char *)rdbuf + aoff);
    959 
    960 		for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
    961 			aoff += iep->reclen,
    962 			iep = (struct attr_indexentry *) ((char *)rdbuf + aoff))
    963 		{
    964 			ddprintf(("scan: %d, %d\n",
    965 				  (u_int32_t) iep->ie_number,
    966 				  (u_int32_t) iep->ie_fnametype));
    967 
    968 			/* check the name - the case-insensitive check
    969 			 * has to come first, to break from this for loop
    970 			 * if needed, so we can dive correctly */
    971 			res = ntfs_uastricmp(ntmp, iep->ie_fname,
    972 				iep->ie_fnamelen, fname, fnamelen);
    973 			if (!fullscan) {
    974 				if (res > 0) break;
    975 				if (res < 0) continue;
    976 			}
    977 
    978 			if (iep->ie_fnametype == 0 ||
    979 			    !(ntmp->ntm_flag & NTFS_MFLAG_CASEINS))
    980 			{
    981 				res = ntfs_uastrcmp(ntmp, iep->ie_fname,
    982 					iep->ie_fnamelen, fname, fnamelen);
    983 				if (res != 0 && !fullscan) continue;
    984 			}
    985 
    986 			/* if we perform full scan, the file does not match
    987 			 * and this is subnode, dive */
    988 			if (fullscan && res != 0) {
    989 			    if (iep->ie_flag & NTFS_IEFLAG_SUBNODE) {
    990 				MALLOC(tctx, struct ntfs_lookup_ctx *,
    991 					sizeof(struct ntfs_lookup_ctx),
    992 					M_TEMP, M_WAITOK);
    993 				tctx->aoff	= aoff + iep->reclen;
    994 				tctx->rdsize	= rdsize;
    995 				tctx->cn	= cn;
    996 				tctx->prev	= lookup_ctx;
    997 				lookup_ctx = tctx;
    998 				break;
    999 			    } else
   1000 				continue;
   1001 			}
   1002 
   1003 			if (aname) {
   1004 				error = ntfs_ntlookupattr(ntmp,
   1005 					aname, anamelen,
   1006 					&attrtype, &attrname);
   1007 				if (error)
   1008 					goto fail;
   1009 			}
   1010 
   1011 			/* Check if we've found ourselves */
   1012 			if ((iep->ie_number == ip->i_number) &&
   1013 			    (attrtype == fp->f_attrtype) &&
   1014 			    ((!attrname && !fp->f_attrname) ||
   1015 			     (attrname && fp->f_attrname &&
   1016 			      !strcmp(attrname, fp->f_attrname))))
   1017 			{
   1018 				VREF(vp);
   1019 				*vpp = vp;
   1020 				error = 0;
   1021 				goto fail;
   1022 			}
   1023 
   1024 			/* free the buffer returned by ntfs_ntlookupattr() */
   1025 			if (attrname) {
   1026 				FREE(attrname, M_TEMP);
   1027 				attrname = NULL;
   1028 			}
   1029 
   1030 			/* vget node, but don't load it */
   1031 			error = ntfs_vgetex(ntmp->ntm_mountp,
   1032 				   iep->ie_number, attrtype, attrname,
   1033 				   LK_EXCLUSIVE, VG_DONTLOADIN | VG_DONTVALIDFN,
   1034 				   &nvp);
   1035 			if (error)
   1036 				goto fail;
   1037 
   1038 			nfp = VTOF(nvp);
   1039 
   1040 			if (nfp->f_flag & FN_VALID) {
   1041 				*vpp = nvp;
   1042 				goto fail;
   1043 			}
   1044 
   1045 			nfp->f_fflag = iep->ie_fflag;
   1046 			nfp->f_pnumber = iep->ie_fpnumber;
   1047 			nfp->f_times = iep->ie_ftimes;
   1048 
   1049 			if((nfp->f_fflag & NTFS_FFLAG_DIR) &&
   1050 			   (nfp->f_attrtype == NTFS_A_DATA) &&
   1051 			   (nfp->f_attrname == NULL))
   1052 				f_type = VDIR;
   1053 			else
   1054 				f_type = VREG;
   1055 
   1056 			nvp->v_type = f_type;
   1057 
   1058 			if ((nfp->f_attrtype == NTFS_A_DATA) &&
   1059 			    (nfp->f_attrname == NULL))
   1060 			{
   1061 				/* Opening default attribute */
   1062 				nfp->f_size = iep->ie_fsize;
   1063 				nfp->f_allocated = iep->ie_fallocated;
   1064 				nfp->f_flag |= FN_PRELOADED;
   1065 			} else {
   1066 				error = ntfs_filesize(ntmp, nfp,
   1067 					    &nfp->f_size, &nfp->f_allocated);
   1068 				if (error) {
   1069 					vput(nvp);
   1070 					goto fail;
   1071 				}
   1072 			}
   1073 
   1074 			nfp->f_flag &= ~FN_VALID;
   1075 			*vpp = nvp;
   1076 			goto fail;
   1077 		}
   1078 
   1079 		/* Dive if possible */
   1080 		if (iep->ie_flag & NTFS_IEFLAG_SUBNODE) {
   1081 			dprintf(("ntfs_ntlookupfile: diving\n"));
   1082 
   1083 			cn = *(cn_t *) ((char *)rdbuf + aoff +
   1084 					iep->reclen - sizeof(cn_t));
   1085 			rdsize = blsize;
   1086 
   1087 			error = ntfs_readattr(ntmp, ip, NTFS_A_INDX, "$I30",
   1088 					ntfs_cntob(cn), rdsize, rdbuf, NULL);
   1089 			if (error)
   1090 				goto fail;
   1091 
   1092 			error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
   1093 						rdbuf, rdsize);
   1094 			if (error)
   1095 				goto fail;
   1096 
   1097 			aoff = (((struct attr_indexalloc *) rdbuf)->ia_hdrsize +
   1098 				0x18);
   1099 		} else if (fullscan && lookup_ctx) {
   1100 			cn = lookup_ctx->cn;
   1101 			aoff = lookup_ctx->aoff;
   1102 			rdsize = lookup_ctx->rdsize;
   1103 
   1104 			error = ntfs_readattr(ntmp, ip,
   1105 				(cn == 0) ? NTFS_A_INDXROOT : NTFS_A_INDX,
   1106 				"$I30", ntfs_cntob(cn), rdsize, rdbuf, NULL);
   1107 			if (error)
   1108 				goto fail;
   1109 
   1110 			if (cn != 0) {
   1111 				error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
   1112 						rdbuf, rdsize);
   1113 				if (error)
   1114 					goto fail;
   1115 			}
   1116 
   1117 			tctx = lookup_ctx;
   1118 			lookup_ctx = lookup_ctx->prev;
   1119 			FREE(tctx, M_TEMP);
   1120 		} else {
   1121 			dprintf(("ntfs_ntlookupfile: nowhere to dive :-(\n"));
   1122 			error = ENOENT;
   1123 			break;
   1124 		}
   1125 	} while (1);
   1126 
   1127 	/* perform full scan if no entry was found */
   1128 	if (!fullscan && error == ENOENT) {
   1129 		fullscan = 1;
   1130 		cn = 0;		/* need zero, used by lookup_ctx */
   1131 
   1132 		ddprintf(("ntfs_ntlookupfile: fullscan performed for: %.*s\n",
   1133 			(int) fnamelen, fname));
   1134 		goto loop;
   1135 	}
   1136 
   1137 	dprintf(("finish\n"));
   1138 
   1139 fail:
   1140 	if (attrname)
   1141 		FREE(attrname, M_TEMP);
   1142 	if (lookup_ctx) {
   1143 		while(lookup_ctx) {
   1144 			tctx = lookup_ctx;
   1145 			lookup_ctx = lookup_ctx->prev;
   1146 			FREE(tctx, M_TEMP);
   1147 		}
   1148 	}
   1149 	ntfs_ntvattrrele(vap);
   1150 	ntfs_ntput(ip);
   1151 	free(rdbuf, M_TEMP);
   1152 	return (error);
   1153 }
   1154 
   1155 /*
   1156  * Check if name type is permitted to show.
   1157  */
   1158 int
   1159 ntfs_isnamepermitted(
   1160 		     struct ntfsmount * ntmp,
   1161 		     struct attr_indexentry * iep)
   1162 {
   1163 	if (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)
   1164 		return 1;
   1165 
   1166 	switch (iep->ie_fnametype) {
   1167 	case 2:
   1168 		ddprintf(("ntfs_isnamepermitted: skipped DOS name\n"));
   1169 		return 0;
   1170 	case 0: case 1: case 3:
   1171 		return 1;
   1172 	default:
   1173 		printf("ntfs_isnamepermitted: "
   1174 		    "WARNING! Unknown file name type: %d\n",
   1175 		    iep->ie_fnametype);
   1176 		break;
   1177 	}
   1178 	return 0;
   1179 }
   1180 
   1181 /*
   1182  * Read ntfs dir like stream of attr_indexentry, not like btree of them.
   1183  * This is done by scanning $BITMAP:$I30 for busy clusters and reading them.
   1184  * Of course $INDEX_ROOT:$I30 is read before. Last read values are stored in
   1185  * fnode, so we can skip toward record number num almost immediately.
   1186  * Anyway this is rather slow routine. The problem is that we don't know
   1187  * how many records are there in $INDEX_ALLOCATION:$I30 block.
   1188  */
   1189 int
   1190 ntfs_ntreaddir(
   1191 	       struct ntfsmount * ntmp,
   1192 	       struct fnode * fp,
   1193 	       u_int32_t num,
   1194 	       struct attr_indexentry ** riepp)
   1195 {
   1196 	struct ntnode  *ip = FTONT(fp);
   1197 	struct ntvattr *vap = NULL;	/* IndexRoot attribute */
   1198 	struct ntvattr *bmvap = NULL;	/* BitMap attribute */
   1199 	struct ntvattr *iavap = NULL;	/* IndexAllocation attribute */
   1200 	void *        rdbuf;		/* Buffer to read directory's blocks  */
   1201 	u_char         *bmp = NULL;	/* Bitmap */
   1202 	u_int32_t       blsize;		/* Index allocation size (2048) */
   1203 	u_int32_t       rdsize;		/* Length of data to read */
   1204 	u_int32_t       attrnum;	/* Current attribute type */
   1205 	u_int32_t       cpbl = 1;	/* Clusters per directory block */
   1206 	u_int32_t       blnum;
   1207 	struct attr_indexentry *iep;
   1208 	int             error = ENOENT;
   1209 	u_int32_t       aoff, cnum;
   1210 
   1211 	dprintf(("ntfs_ntreaddir: read ino: %llu, num: %d\n",
   1212 	    (unsigned long long)ip->i_number, num));
   1213 	error = ntfs_ntget(ip);
   1214 	if (error)
   1215 		return (error);
   1216 
   1217 	error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
   1218 	if (error)
   1219 		return (ENOTDIR);
   1220 
   1221 	if (fp->f_dirblbuf == NULL) {
   1222 		fp->f_dirblsz = vap->va_a_iroot->ir_size;
   1223 		fp->f_dirblbuf = (void *) malloc(
   1224 		       MAX(vap->va_datalen,fp->f_dirblsz), M_NTFSDIR, M_WAITOK);
   1225 	}
   1226 
   1227 	blsize = fp->f_dirblsz;
   1228 	rdbuf = fp->f_dirblbuf;
   1229 
   1230 	dprintf(("ntfs_ntreaddir: rdbuf: %p, blsize: %d\n", rdbuf, blsize));
   1231 
   1232 	if (vap->va_a_iroot->ir_flag & NTFS_IRFLAG_INDXALLOC) {
   1233 		error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXBITMAP, "$I30",
   1234 					0, &bmvap);
   1235 		if (error) {
   1236 			error = ENOTDIR;
   1237 			goto fail;
   1238 		}
   1239 		bmp = (u_char *) malloc(bmvap->va_datalen, M_TEMP, M_WAITOK);
   1240 		error = ntfs_readattr(ntmp, ip, NTFS_A_INDXBITMAP, "$I30", 0,
   1241 				       bmvap->va_datalen, bmp, NULL);
   1242 		if (error)
   1243 			goto fail;
   1244 
   1245 		error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDX, "$I30",
   1246 					0, &iavap);
   1247 		if (error) {
   1248 			error = ENOTDIR;
   1249 			goto fail;
   1250 		}
   1251 		cpbl = ntfs_btocn(blsize + ntfs_cntob(1) - 1);
   1252 		dprintf(("ntfs_ntreaddir: indexalloc: %qu, cpbl: %d\n",
   1253 			 (long long)iavap->va_datalen, cpbl));
   1254 	} else {
   1255 		dprintf(("ntfs_ntreadidir: w/o BitMap and IndexAllocation\n"));
   1256 		iavap = bmvap = NULL;
   1257 		bmp = NULL;
   1258 	}
   1259 
   1260 	/* Try use previous values */
   1261 	if ((fp->f_lastdnum < num) && (fp->f_lastdnum != 0)) {
   1262 		attrnum = fp->f_lastdattr;
   1263 		aoff = fp->f_lastdoff;
   1264 		blnum = fp->f_lastdblnum;
   1265 		cnum = fp->f_lastdnum;
   1266 	} else {
   1267 		attrnum = NTFS_A_INDXROOT;
   1268 		aoff = sizeof(struct attr_indexroot);
   1269 		blnum = 0;
   1270 		cnum = 0;
   1271 	}
   1272 
   1273 	do {
   1274 		dprintf(("ntfs_ntreaddir: scan: 0x%x, %d, %d, %d, %d\n",
   1275 			 attrnum, (u_int32_t) blnum, cnum, num, aoff));
   1276 		rdsize = (attrnum == NTFS_A_INDXROOT) ? vap->va_datalen : blsize;
   1277 		error = ntfs_readattr(ntmp, ip, attrnum, "$I30",
   1278 				ntfs_cntob(blnum * cpbl), rdsize, rdbuf, NULL);
   1279 		if (error)
   1280 			goto fail;
   1281 
   1282 		if (attrnum == NTFS_A_INDX) {
   1283 			error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
   1284 						rdbuf, rdsize);
   1285 			if (error)
   1286 				goto fail;
   1287 		}
   1288 		if (aoff == 0)
   1289 			aoff = (attrnum == NTFS_A_INDX) ?
   1290 				(0x18 + ((struct attr_indexalloc *) rdbuf)->ia_hdrsize) :
   1291 				sizeof(struct attr_indexroot);
   1292 
   1293 		iep = (struct attr_indexentry *) ((char *)rdbuf + aoff);
   1294 		for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
   1295 			aoff += iep->reclen,
   1296 			iep = (struct attr_indexentry *) ((char *)rdbuf + aoff))
   1297 		{
   1298 			if (!ntfs_isnamepermitted(ntmp, iep)) continue;
   1299 
   1300 			if (cnum >= num) {
   1301 				fp->f_lastdnum = cnum;
   1302 				fp->f_lastdoff = aoff;
   1303 				fp->f_lastdblnum = blnum;
   1304 				fp->f_lastdattr = attrnum;
   1305 
   1306 				*riepp = iep;
   1307 
   1308 				error = 0;
   1309 				goto fail;
   1310 			}
   1311 			cnum++;
   1312 		}
   1313 
   1314 		if (iavap) {
   1315 			if (attrnum == NTFS_A_INDXROOT)
   1316 				blnum = 0;
   1317 			else
   1318 				blnum++;
   1319 
   1320 			while (ntfs_cntob(blnum * cpbl) < iavap->va_datalen) {
   1321 				if (bmp[blnum >> 3] & (1 << (blnum & 3)))
   1322 					break;
   1323 				blnum++;
   1324 			}
   1325 
   1326 			attrnum = NTFS_A_INDX;
   1327 			aoff = 0;
   1328 			if (ntfs_cntob(blnum * cpbl) >= iavap->va_datalen)
   1329 				break;
   1330 			dprintf(("ntfs_ntreaddir: blnum: %d\n", (u_int32_t) blnum));
   1331 		}
   1332 	} while (iavap);
   1333 
   1334 	*riepp = NULL;
   1335 	fp->f_lastdnum = 0;
   1336 
   1337 fail:
   1338 	if (vap)
   1339 		ntfs_ntvattrrele(vap);
   1340 	if (bmvap)
   1341 		ntfs_ntvattrrele(bmvap);
   1342 	if (iavap)
   1343 		ntfs_ntvattrrele(iavap);
   1344 	if (bmp)
   1345 		FREE(bmp, M_TEMP);
   1346 	ntfs_ntput(ip);
   1347 	return (error);
   1348 }
   1349 
   1350 /*
   1351  * Convert NTFS times that are in 100 ns units and begins from
   1352  * 1601 Jan 1 into unix times.
   1353  */
   1354 struct timespec
   1355 ntfs_nttimetounix(
   1356 		  u_int64_t nt)
   1357 {
   1358 	struct timespec t;
   1359 
   1360 	/* WindowNT times are in 100 ns and from 1601 Jan 1 */
   1361 	t.tv_nsec = (nt % (1000 * 1000 * 10)) * 100;
   1362 	t.tv_sec = nt / (1000 * 1000 * 10) -
   1363 		369LL * 365LL * 24LL * 60LL * 60LL -
   1364 		89LL * 1LL * 24LL * 60LL * 60LL;
   1365 	return (t);
   1366 }
   1367 
   1368 /*
   1369  * Get file times from NTFS_A_NAME attribute.
   1370  */
   1371 int
   1372 ntfs_times(
   1373 	   struct ntfsmount * ntmp,
   1374 	   struct ntnode * ip,
   1375 	   ntfs_times_t * tm)
   1376 {
   1377 	struct ntvattr *vap;
   1378 	int             error;
   1379 
   1380 	dprintf(("ntfs_times: ino: %llu...\n",
   1381 	    (unsigned long long)ip->i_number));
   1382 
   1383 	error = ntfs_ntget(ip);
   1384 	if (error)
   1385 		return (error);
   1386 
   1387 	error = ntfs_ntvattrget(ntmp, ip, NTFS_A_NAME, NULL, 0, &vap);
   1388 	if (error) {
   1389 		ntfs_ntput(ip);
   1390 		return (error);
   1391 	}
   1392 	*tm = vap->va_a_name->n_times;
   1393 	ntfs_ntvattrrele(vap);
   1394 	ntfs_ntput(ip);
   1395 
   1396 	return (0);
   1397 }
   1398 
   1399 /*
   1400  * Get file sizes from corresponding attribute.
   1401  *
   1402  * ntnode under fnode should be locked.
   1403  */
   1404 int
   1405 ntfs_filesize(
   1406 	      struct ntfsmount * ntmp,
   1407 	      struct fnode * fp,
   1408 	      u_int64_t * size,
   1409 	      u_int64_t * bytes)
   1410 {
   1411 	struct ntvattr *vap;
   1412 	struct ntnode *ip = FTONT(fp);
   1413 	u_int64_t       sz, bn;
   1414 	int             error;
   1415 
   1416 	dprintf(("ntfs_filesize: ino: %llu\n",
   1417 	    (unsigned long long)ip->i_number));
   1418 
   1419 	error = ntfs_ntvattrget(ntmp, ip,
   1420 		fp->f_attrtype, fp->f_attrname, 0, &vap);
   1421 	if (error)
   1422 		return (error);
   1423 
   1424 	bn = vap->va_allocated;
   1425 	sz = vap->va_datalen;
   1426 
   1427 	dprintf(("ntfs_filesize: %d bytes (%d bytes allocated)\n",
   1428 		(u_int32_t) sz, (u_int32_t) bn));
   1429 
   1430 	if (size)
   1431 		*size = sz;
   1432 	if (bytes)
   1433 		*bytes = bn;
   1434 
   1435 	ntfs_ntvattrrele(vap);
   1436 
   1437 	return (0);
   1438 }
   1439 
   1440 /*
   1441  * This is one of write routine.
   1442  */
   1443 int
   1444 ntfs_writeattr_plain(
   1445 	struct ntfsmount * ntmp,
   1446 	struct ntnode * ip,
   1447 	u_int32_t attrnum,
   1448 	char *attrname,
   1449 	off_t roff,
   1450 	size_t rsize,
   1451 	void *rdata,
   1452 	size_t * initp,
   1453 	struct uio *uio)
   1454 {
   1455 	size_t          init;
   1456 	int             error = 0;
   1457 	off_t           off = roff, left = rsize, towrite;
   1458 	void *        data = rdata;
   1459 	struct ntvattr *vap;
   1460 	*initp = 0;
   1461 
   1462 	while (left) {
   1463 		error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
   1464 					ntfs_btocn(off), &vap);
   1465 		if (error)
   1466 			return (error);
   1467 		towrite = MIN(left, ntfs_cntob(vap->va_vcnend + 1) - off);
   1468 		ddprintf(("ntfs_writeattr_plain: o: %qd, s: %qd (%qu - %qu)\n",
   1469 			 (long long) off, (long long) towrite,
   1470 			 (long long) vap->va_vcnstart,
   1471 			 (long long) vap->va_vcnend));
   1472 		error = ntfs_writentvattr_plain(ntmp, ip, vap,
   1473 					 off - ntfs_cntob(vap->va_vcnstart),
   1474 					 towrite, data, &init, uio);
   1475 		if (error) {
   1476 			dprintf(("ntfs_writeattr_plain: "
   1477 			    "ntfs_writentvattr_plain failed: o: %qd, s: %qd\n",
   1478 			    (long long) off, (long long) towrite));
   1479 			dprintf(("ntfs_writeattr_plain: attrib: %qu - %qu\n",
   1480 			       (long long) vap->va_vcnstart,
   1481 			       (long long) vap->va_vcnend));
   1482 			ntfs_ntvattrrele(vap);
   1483 			break;
   1484 		}
   1485 		ntfs_ntvattrrele(vap);
   1486 		left -= towrite;
   1487 		off += towrite;
   1488 		data = (char *)data + towrite;
   1489 		*initp += init;
   1490 	}
   1491 
   1492 	return (error);
   1493 }
   1494 
   1495 /*
   1496  * This is one of write routine.
   1497  *
   1498  * ntnode should be locked.
   1499  */
   1500 int
   1501 ntfs_writentvattr_plain(
   1502 	struct ntfsmount * ntmp,
   1503 	struct ntnode * ip,
   1504 	struct ntvattr * vap,
   1505 	off_t roff,
   1506 	size_t rsize,
   1507 	void *rdata,
   1508 	size_t * initp,
   1509 	struct uio *uio)
   1510 {
   1511 	int             error = 0;
   1512 	off_t           off;
   1513 	int             cnt;
   1514 	cn_t            ccn, ccl, cn, left, cl;
   1515 	void *        data = rdata;
   1516 	struct buf     *bp;
   1517 	size_t          tocopy;
   1518 
   1519 	*initp = 0;
   1520 
   1521 	if ((vap->va_flag & NTFS_AF_INRUN) == 0) {
   1522 		dprintf(("ntfs_writevattr_plain: CAN'T WRITE RES. ATTRIBUTE\n"));
   1523 		return ENOTTY;
   1524 	}
   1525 
   1526 	ddprintf(("ntfs_writentvattr_plain: data in run: %lu chains\n",
   1527 		 vap->va_vruncnt));
   1528 
   1529 	off = roff;
   1530 	left = rsize;
   1531 	ccl = 0;
   1532 	ccn = 0;
   1533 	cnt = 0;
   1534 	for (; left && (cnt < vap->va_vruncnt); cnt++) {
   1535 		ccn = vap->va_vruncn[cnt];
   1536 		ccl = vap->va_vruncl[cnt];
   1537 
   1538 		ddprintf(("ntfs_writentvattr_plain: "
   1539 		    "left %qu, cn: 0x%qx, cl: %qu, off: %qd\n",
   1540 		    (long long) left, (long long) ccn,
   1541 		    (long long) ccl, (long long) off));
   1542 
   1543 		if (ntfs_cntob(ccl) < off) {
   1544 			off -= ntfs_cntob(ccl);
   1545 			cnt++;
   1546 			continue;
   1547 		}
   1548 		if (!ccn && ip->i_number != NTFS_BOOTINO)
   1549 			continue; /* XXX */
   1550 
   1551 		ccl -= ntfs_btocn(off);
   1552 		cn = ccn + ntfs_btocn(off);
   1553 		off = ntfs_btocnoff(off);
   1554 
   1555 		while (left && ccl) {
   1556 			/*
   1557 			 * Always read and write single clusters at a time -
   1558 			 * we need to avoid requesting differently-sized
   1559 			 * blocks at the same disk offsets to avoid
   1560 			 * confusing the buffer cache.
   1561 			 */
   1562 			tocopy = MIN(left, ntfs_cntob(1) - off);
   1563 			cl = ntfs_btocl(tocopy + off);
   1564 			KASSERT(cl == 1 && tocopy <= ntfs_cntob(1));
   1565 			ddprintf(("ntfs_writentvattr_plain: write: "
   1566 				"cn: 0x%qx cl: %qu, off: %qd len: %qu, left: %qu\n",
   1567 				(long long) cn, (long long) cl,
   1568 				(long long) off, (long long) tocopy,
   1569 				(long long) left));
   1570 			if ((off == 0) && (tocopy == ntfs_cntob(cl)))
   1571 			{
   1572 				bp = getblk(ntmp->ntm_devvp, ntfs_cntobn(cn),
   1573 					    ntfs_cntob(cl), 0, 0);
   1574 				clrbuf(bp);
   1575 			} else {
   1576 				error = bread(ntmp->ntm_devvp, ntfs_cntobn(cn),
   1577 					      ntfs_cntob(cl), NOCRED, &bp);
   1578 				if (error) {
   1579 					brelse(bp, 0);
   1580 					return (error);
   1581 				}
   1582 			}
   1583 			if (uio)
   1584 				uiomove((char *)bp->b_data + off, tocopy, uio);
   1585 			else
   1586 				memcpy((char *)bp->b_data + off, data, tocopy);
   1587 			bawrite(bp);
   1588 			data = (char *)data + tocopy;
   1589 			*initp += tocopy;
   1590 			off = 0;
   1591 			left -= tocopy;
   1592 			cn += cl;
   1593 			ccl -= cl;
   1594 		}
   1595 	}
   1596 
   1597 	if (left) {
   1598 		printf("ntfs_writentvattr_plain: POSSIBLE RUN ERROR\n");
   1599 		error = EINVAL;
   1600 	}
   1601 
   1602 	return (error);
   1603 }
   1604 
   1605 /*
   1606  * This is one of read routines.
   1607  *
   1608  * ntnode should be locked.
   1609  */
   1610 int
   1611 ntfs_readntvattr_plain(
   1612 	struct ntfsmount * ntmp,
   1613 	struct ntnode * ip,
   1614 	struct ntvattr * vap,
   1615 	off_t roff,
   1616 	size_t rsize,
   1617 	void *rdata,
   1618 	size_t * initp,
   1619 	struct uio *uio)
   1620 {
   1621 	int             error = 0;
   1622 	off_t           off;
   1623 
   1624 	*initp = 0;
   1625 	if (vap->va_flag & NTFS_AF_INRUN) {
   1626 		int             cnt;
   1627 		cn_t            ccn, ccl, cn, left, cl;
   1628 		void *        data = rdata;
   1629 		struct buf     *bp;
   1630 		size_t          tocopy;
   1631 
   1632 		ddprintf(("ntfs_readntvattr_plain: data in run: %lu chains\n",
   1633 			 vap->va_vruncnt));
   1634 
   1635 		off = roff;
   1636 		left = rsize;
   1637 		ccl = 0;
   1638 		ccn = 0;
   1639 		cnt = 0;
   1640 		while (left && (cnt < vap->va_vruncnt)) {
   1641 			ccn = vap->va_vruncn[cnt];
   1642 			ccl = vap->va_vruncl[cnt];
   1643 
   1644 			ddprintf(("ntfs_readntvattr_plain: "
   1645 				 "left %qu, cn: 0x%qx, cl: %qu, off: %qd\n",
   1646 				 (long long) left, (long long) ccn,
   1647 				 (long long) ccl, (long long) off));
   1648 
   1649 			if (ntfs_cntob(ccl) < off) {
   1650 				off -= ntfs_cntob(ccl);
   1651 				cnt++;
   1652 				continue;
   1653 			}
   1654 			if (ccn || ip->i_number == NTFS_BOOTINO) {
   1655 				ccl -= ntfs_btocn(off);
   1656 				cn = ccn + ntfs_btocn(off);
   1657 				off = ntfs_btocnoff(off);
   1658 
   1659 				while (left && ccl) {
   1660 					/*
   1661 					 * Always read single clusters at a
   1662 					 * time - we need to avoid reading
   1663 					 * differently-sized blocks at the
   1664 					 * same disk offsets to avoid
   1665 					 * confusing the buffer cache.
   1666 					 */
   1667 					tocopy = MIN(left,
   1668 					    ntfs_cntob(1) - off);
   1669 					cl = ntfs_btocl(tocopy + off);
   1670 					KASSERT(cl == 1 &&
   1671 					    tocopy <= ntfs_cntob(1));
   1672 
   1673 					ddprintf(("ntfs_readntvattr_plain: "
   1674 						"read: cn: 0x%qx cl: %qu, "
   1675 						"off: %qd len: %qu, left: %qu\n",
   1676 						(long long) cn,
   1677 						(long long) cl,
   1678 						(long long) off,
   1679 						(long long) tocopy,
   1680 						(long long) left));
   1681 					error = bread(ntmp->ntm_devvp,
   1682 						      ntfs_cntobn(cn),
   1683 						      ntfs_cntob(cl),
   1684 						      NOCRED, &bp);
   1685 					if (error) {
   1686 						brelse(bp, 0);
   1687 						return (error);
   1688 					}
   1689 					if (uio) {
   1690 						uiomove((char *)bp->b_data + off,
   1691 							tocopy, uio);
   1692 					} else {
   1693 						memcpy(data, (char *)bp->b_data + off,
   1694 							tocopy);
   1695 					}
   1696 					brelse(bp, 0);
   1697 					data = (char *)data + tocopy;
   1698 					*initp += tocopy;
   1699 					off = 0;
   1700 					left -= tocopy;
   1701 					cn += cl;
   1702 					ccl -= cl;
   1703 				}
   1704 			} else {
   1705 				tocopy = MIN(left, ntfs_cntob(ccl) - off);
   1706 				ddprintf(("ntfs_readntvattr_plain: "
   1707 					"hole: ccn: 0x%qx ccl: %qu, off: %qd, "
   1708 					" len: %qu, left: %qu\n",
   1709 					(long long) ccn, (long long) ccl,
   1710 					(long long) off, (long long) tocopy,
   1711 					(long long) left));
   1712 				left -= tocopy;
   1713 				off = 0;
   1714 				if (uio) {
   1715 					char vbuf[] = "";
   1716 					size_t remains = tocopy;
   1717 					for(; remains; remains--)
   1718 						uiomove(vbuf, 1, uio);
   1719 				} else
   1720 					bzero(data, tocopy);
   1721 				data = (char *)data + tocopy;
   1722 			}
   1723 			cnt++;
   1724 		}
   1725 		if (left) {
   1726 			printf("ntfs_readntvattr_plain: POSSIBLE RUN ERROR\n");
   1727 			error = E2BIG;
   1728 		}
   1729 	} else {
   1730 		ddprintf(("ntfs_readnvattr_plain: data is in mft record\n"));
   1731 		if (uio)
   1732 			uiomove((char *)vap->va_datap + roff, rsize, uio);
   1733 		else
   1734 			memcpy(rdata, (char *)vap->va_datap + roff, rsize);
   1735 		*initp += rsize;
   1736 	}
   1737 
   1738 	return (error);
   1739 }
   1740 
   1741 /*
   1742  * This is one of read routines.
   1743  */
   1744 int
   1745 ntfs_readattr_plain(
   1746 	struct ntfsmount * ntmp,
   1747 	struct ntnode * ip,
   1748 	u_int32_t attrnum,
   1749 	const char *attrname,
   1750 	off_t roff,
   1751 	size_t rsize,
   1752 	void *rdata,
   1753 	size_t * initp,
   1754 	struct uio *uio)
   1755 {
   1756 	size_t          init;
   1757 	int             error = 0;
   1758 	off_t           off = roff, left = rsize, toread;
   1759 	void *        data = rdata;
   1760 	struct ntvattr *vap;
   1761 	*initp = 0;
   1762 
   1763 	while (left) {
   1764 		error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
   1765 					ntfs_btocn(off), &vap);
   1766 		if (error)
   1767 			return (error);
   1768 		toread = MIN(left, ntfs_cntob(vap->va_vcnend + 1) - off);
   1769 		ddprintf(("ntfs_readattr_plain: o: %qd, s: %qd (%qu - %qu)\n",
   1770 			 (long long) off, (long long) toread,
   1771 			 (long long) vap->va_vcnstart,
   1772 			 (long long) vap->va_vcnend));
   1773 		error = ntfs_readntvattr_plain(ntmp, ip, vap,
   1774 					 off - ntfs_cntob(vap->va_vcnstart),
   1775 					 toread, data, &init, uio);
   1776 		if (error) {
   1777 			printf("ntfs_readattr_plain: "
   1778 			       "ntfs_readntvattr_plain failed: o: %qd, s: %qd\n",
   1779 			       (long long) off, (long long) toread);
   1780 			printf("ntfs_readattr_plain: attrib: %qu - %qu\n",
   1781 			       (long long) vap->va_vcnstart,
   1782 			       (long long) vap->va_vcnend);
   1783 			ntfs_ntvattrrele(vap);
   1784 			break;
   1785 		}
   1786 		ntfs_ntvattrrele(vap);
   1787 		left -= toread;
   1788 		off += toread;
   1789 		data = (char *)data + toread;
   1790 		*initp += init;
   1791 	}
   1792 
   1793 	return (error);
   1794 }
   1795 
   1796 /*
   1797  * This is one of read routines.
   1798  */
   1799 int
   1800 ntfs_readattr(
   1801 	struct ntfsmount * ntmp,
   1802 	struct ntnode * ip,
   1803 	u_int32_t attrnum,
   1804 	const char *attrname,
   1805 	off_t roff,
   1806 	size_t rsize,
   1807 	void *rdata,
   1808 	struct uio *uio)
   1809 {
   1810 	int             error = 0;
   1811 	struct ntvattr *vap;
   1812 	size_t          init;
   1813 
   1814 	ddprintf(("ntfs_readattr: reading %llu: 0x%x, from %qd size %qu"
   1815 	    " bytes\n", (unsigned long long)ip->i_number, attrnum,
   1816 	    (long long)roff, (long long)rsize));
   1817 
   1818 	error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname, 0, &vap);
   1819 	if (error)
   1820 		return (error);
   1821 
   1822 	if ((roff > vap->va_datalen) ||
   1823 	    (roff + rsize > vap->va_datalen)) {
   1824 		printf("ntfs_readattr: offset too big: %qd (%qd) > %qu\n",
   1825 			(long long) roff, (long long) (roff + rsize),
   1826 			(long long) vap->va_datalen);
   1827 		ntfs_ntvattrrele(vap);
   1828 		return (E2BIG);
   1829 	}
   1830 	if (vap->va_compression && vap->va_compressalg) {
   1831 		u_int8_t       *cup;
   1832 		u_int8_t       *uup;
   1833 		off_t           off = roff, left = rsize, tocopy;
   1834 		void *        data = rdata;
   1835 		cn_t            cn;
   1836 
   1837 		ddprintf(("ntfs_ntreadattr: compression: %d\n",
   1838 			 vap->va_compressalg));
   1839 
   1840 		cup = malloc(ntfs_cntob(NTFS_COMPUNIT_CL),
   1841 		       M_NTFSDECOMP, M_WAITOK);
   1842 		uup = malloc(ntfs_cntob(NTFS_COMPUNIT_CL),
   1843 		       M_NTFSDECOMP, M_WAITOK);
   1844 
   1845 		cn = (ntfs_btocn(roff)) & (~(NTFS_COMPUNIT_CL - 1));
   1846 		off = roff - ntfs_cntob(cn);
   1847 
   1848 		while (left) {
   1849 			error = ntfs_readattr_plain(ntmp, ip, attrnum,
   1850 						  attrname, ntfs_cntob(cn),
   1851 					          ntfs_cntob(NTFS_COMPUNIT_CL),
   1852 						  cup, &init, NULL);
   1853 			if (error)
   1854 				break;
   1855 
   1856 			tocopy = MIN(left, ntfs_cntob(NTFS_COMPUNIT_CL) - off);
   1857 
   1858 			if (init == ntfs_cntob(NTFS_COMPUNIT_CL)) {
   1859 				if (uio)
   1860 					uiomove(cup + off, tocopy, uio);
   1861 				else
   1862 					memcpy(data, cup + off, tocopy);
   1863 			} else if (init == 0) {
   1864 				if (uio) {
   1865 					char vbuf[] = "";
   1866 					size_t remains = tocopy;
   1867 					for(; remains; remains--)
   1868 						uiomove(vbuf, 1, uio);
   1869 				}
   1870 				else
   1871 					bzero(data, tocopy);
   1872 			} else {
   1873 				error = ntfs_uncompunit(ntmp, uup, cup);
   1874 				if (error)
   1875 					break;
   1876 				if (uio)
   1877 					uiomove(uup + off, tocopy, uio);
   1878 				else
   1879 					memcpy(data, uup + off, tocopy);
   1880 			}
   1881 
   1882 			left -= tocopy;
   1883 			data = (char *)data + tocopy;
   1884 			off += tocopy - ntfs_cntob(NTFS_COMPUNIT_CL);
   1885 			cn += NTFS_COMPUNIT_CL;
   1886 		}
   1887 
   1888 		FREE(uup, M_NTFSDECOMP);
   1889 		FREE(cup, M_NTFSDECOMP);
   1890 	} else
   1891 		error = ntfs_readattr_plain(ntmp, ip, attrnum, attrname,
   1892 					     roff, rsize, rdata, &init, uio);
   1893 	ntfs_ntvattrrele(vap);
   1894 	return (error);
   1895 }
   1896 
   1897 #if UNUSED_CODE
   1898 int
   1899 ntfs_parserun(
   1900 	      cn_t * cn,
   1901 	      cn_t * cl,
   1902 	      u_int8_t * run,
   1903 	      u_long len,
   1904 	      u_long *off)
   1905 {
   1906 	u_int8_t        sz;
   1907 	int             i;
   1908 
   1909 	if (NULL == run) {
   1910 		printf("ntfs_parsetun: run == NULL\n");
   1911 		return (EINVAL);
   1912 	}
   1913 	sz = run[(*off)++];
   1914 	if (0 == sz) {
   1915 		printf("ntfs_parserun: trying to go out of run\n");
   1916 		return (E2BIG);
   1917 	}
   1918 	*cl = 0;
   1919 	if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
   1920 		printf("ntfs_parserun: "
   1921 		       "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
   1922 		       sz, len, *off);
   1923 		return (EINVAL);
   1924 	}
   1925 	for (i = 0; i < (sz & 0xF); i++)
   1926 		*cl += (u_int32_t) run[(*off)++] << (i << 3);
   1927 
   1928 	sz >>= 4;
   1929 	if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
   1930 		printf("ntfs_parserun: "
   1931 		       "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
   1932 		       sz, len, *off);
   1933 		return (EINVAL);
   1934 	}
   1935 	for (i = 0; i < (sz & 0xF); i++)
   1936 		*cn += (u_int32_t) run[(*off)++] << (i << 3);
   1937 
   1938 	return (0);
   1939 }
   1940 #endif
   1941 
   1942 /*
   1943  * Process fixup routine on given buffer.
   1944  */
   1945 int
   1946 ntfs_procfixups(
   1947 		struct ntfsmount * ntmp,
   1948 		u_int32_t magic,
   1949 		void *xbufv,
   1950 		size_t len)
   1951 {
   1952 	char *xbuf = xbufv;
   1953 	struct fixuphdr *fhp = (struct fixuphdr *) xbuf;
   1954 	int             i;
   1955 	u_int16_t       fixup;
   1956 	u_int16_t      *fxp;
   1957 	u_int16_t      *cfxp;
   1958 
   1959 	if (fhp->fh_magic != magic) {
   1960 		printf("ntfs_procfixups: magic doesn't match: %08x != %08x\n",
   1961 		       fhp->fh_magic, magic);
   1962 		return (EINVAL);
   1963 	}
   1964 	if ((fhp->fh_fnum - 1) * ntmp->ntm_bps != len) {
   1965 		printf("ntfs_procfixups: "
   1966 		       "bad fixups number: %d for %ld bytes block\n",
   1967 		       fhp->fh_fnum, (long)len);	/* XXX printf kludge */
   1968 		return (EINVAL);
   1969 	}
   1970 	if (fhp->fh_foff >= ntmp->ntm_spc * ntmp->ntm_mftrecsz * ntmp->ntm_bps) {
   1971 		printf("ntfs_procfixups: invalid offset: %x", fhp->fh_foff);
   1972 		return (EINVAL);
   1973 	}
   1974 	fxp = (u_int16_t *) (xbuf + fhp->fh_foff);
   1975 	cfxp = (u_int16_t *) (xbuf + ntmp->ntm_bps - 2);
   1976 	fixup = *fxp++;
   1977 	for (i = 1; i < fhp->fh_fnum; i++, fxp++) {
   1978 		if (*cfxp != fixup) {
   1979 			printf("ntfs_procfixups: fixup %d doesn't match\n", i);
   1980 			return (EINVAL);
   1981 		}
   1982 		*cfxp = *fxp;
   1983 		cfxp = (u_int16_t *)((char *)cfxp + ntmp->ntm_bps);
   1984 	}
   1985 	return (0);
   1986 }
   1987 
   1988 #if UNUSED_CODE
   1989 int
   1990 ntfs_runtocn(
   1991 	     cn_t * cn,
   1992 	     struct ntfsmount * ntmp,
   1993 	     u_int8_t * run,
   1994 	     u_long len,
   1995 	     cn_t vcn)
   1996 {
   1997 	cn_t            ccn = 0;
   1998 	cn_t            ccl = 0;
   1999 	u_long          off = 0;
   2000 	int             error = 0;
   2001 
   2002 #ifdef NTFS_DEBUG
   2003 	int             i;
   2004 	printf("ntfs_runtocn: run: %p, %ld bytes, vcn:%ld\n",
   2005 		run, len, (u_long) vcn);
   2006 	printf("ntfs_runtocn: run: ");
   2007 	for (i = 0; i < len; i++)
   2008 		printf("0x%02x ", run[i]);
   2009 	printf("\n");
   2010 #endif
   2011 
   2012 	if (NULL == run) {
   2013 		printf("ntfs_runtocn: run == NULL\n");
   2014 		return (EINVAL);
   2015 	}
   2016 	do {
   2017 		if (run[off] == 0) {
   2018 			printf("ntfs_runtocn: vcn too big\n");
   2019 			return (E2BIG);
   2020 		}
   2021 		vcn -= ccl;
   2022 		error = ntfs_parserun(&ccn, &ccl, run, len, &off);
   2023 		if (error) {
   2024 			printf("ntfs_runtocn: ntfs_parserun failed\n");
   2025 			return (error);
   2026 		}
   2027 	} while (ccl <= vcn);
   2028 	*cn = ccn + vcn;
   2029 	return (0);
   2030 }
   2031 #endif
   2032 
   2033 /*
   2034  * this initializes toupper table & dependant variables to be ready for
   2035  * later work
   2036  */
   2037 void
   2038 ntfs_toupper_init()
   2039 {
   2040 	ntfs_toupper_tab = (wchar *) NULL;
   2041 	mutex_init(&ntfs_toupper_lock, MUTEX_DEFAULT, IPL_NONE);
   2042 	ntfs_toupper_usecount = 0;
   2043 }
   2044 
   2045 /*
   2046  * if the ntfs_toupper_tab[] is filled already, just raise use count;
   2047  * otherwise read the data from the filesystem we are currently mounting
   2048  */
   2049 int
   2050 ntfs_toupper_use(mp, ntmp)
   2051 	struct mount *mp;
   2052 	struct ntfsmount *ntmp;
   2053 {
   2054 	int error = 0;
   2055 	struct vnode *vp;
   2056 
   2057 	/* get exclusive access */
   2058 	mutex_enter(&ntfs_toupper_lock);
   2059 
   2060 	/* only read the translation data from a file if it hasn't been
   2061 	 * read already */
   2062 	if (ntfs_toupper_tab)
   2063 		goto out;
   2064 
   2065 	/*
   2066 	 * Read in Unicode lowercase -> uppercase translation file.
   2067 	 * XXX for now, just the first 256 entries are used anyway,
   2068 	 * so don't bother reading more
   2069 	 */
   2070 	MALLOC(ntfs_toupper_tab, wchar *, 256 * 256 * sizeof(wchar),
   2071 		M_NTFSRDATA, M_WAITOK);
   2072 
   2073 	if ((error = VFS_VGET(mp, NTFS_UPCASEINO, &vp)))
   2074 		goto out;
   2075 	error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
   2076 			0, 256*256*sizeof(wchar), (char *) ntfs_toupper_tab,
   2077 			NULL);
   2078 	vput(vp);
   2079 
   2080     out:
   2081 	ntfs_toupper_usecount++;
   2082 	mutex_exit(&ntfs_toupper_lock);
   2083 	return (error);
   2084 }
   2085 
   2086 /*
   2087  * lower the use count and if it reaches zero, free the memory
   2088  * tied by toupper table
   2089  */
   2090 void
   2091 ntfs_toupper_unuse()
   2092 {
   2093 	/* get exclusive access */
   2094 	mutex_enter(&ntfs_toupper_lock);
   2095 
   2096 	ntfs_toupper_usecount--;
   2097 	if (ntfs_toupper_usecount == 0) {
   2098 		FREE(ntfs_toupper_tab, M_NTFSRDATA);
   2099 		ntfs_toupper_tab = NULL;
   2100 	}
   2101 #ifdef DIAGNOSTIC
   2102 	else if (ntfs_toupper_usecount < 0) {
   2103 		panic("ntfs_toupper_unuse(): use count negative: %d",
   2104 			ntfs_toupper_usecount);
   2105 	}
   2106 #endif
   2107 
   2108 	/* release the lock */
   2109 	mutex_exit(&ntfs_toupper_lock);
   2110 }
   2111