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