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