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