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