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