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