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