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