1 /* $NetBSD: ntfs_subr.c,v 1.23 1999/10/31 19:45:26 jdolecek Exp $ */
4 * Copyright (c) 1998, 1999 Semen Ustimenko (semenu@FreeBSD.org)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
28 * $FreeBSD: src/sys/ntfs/ntfs_subr.c,v 1.7.2.4 2001/10/12 22:08:49 semenu Exp $
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/systm.h>
35 #include <sys/namei.h>
36 #include <sys/kernel.h>
37 #include <sys/vnode.h>
38 #include <sys/mount.h>
41 #include <sys/malloc.h>
43 #include <sys/spinlock.h>
44 #include <sys/iconv.h>
46 #include <machine/inttypes.h>
49 #include <sys/spinlock2.h>
52 #include "ntfsmount.h"
53 #include "ntfs_inode.h"
54 #include "ntfs_vfsops.h"
55 #include "ntfs_subr.h"
56 #include "ntfs_compr.h"
57 #include "ntfs_ihash.h"
59 MALLOC_DEFINE(M_NTFSNTVATTR
, "NTFS vattr", "NTFS file attribute information");
60 MALLOC_DEFINE(M_NTFSRDATA
, "NTFS res data", "NTFS resident data");
61 MALLOC_DEFINE(M_NTFSRUN
, "NTFS vrun", "NTFS vrun storage");
62 MALLOC_DEFINE(M_NTFSDECOMP
, "NTFS decomp", "NTFS decompression temporary");
64 static int ntfs_ntlookupattr (struct ntfsmount
*, const char *, int, int *, char **);
65 static int ntfs_findvattr (struct ntfsmount
*, struct ntnode
*, struct ntvattr
**, struct ntvattr
**, u_int32_t
, const char *, size_t, cn_t
);
66 static int ntfs_uastricmp (struct ntfsmount
*, const wchar
*, size_t, const char *, size_t);
67 static int ntfs_uastrcmp (struct ntfsmount
*, const wchar
*, size_t, const char *, size_t);
69 /* table for mapping Unicode chars into uppercase; it's filled upon first
70 * ntfs mount, freed upon last ntfs umount */
71 static wchar
*ntfs_toupper_tab
;
72 #define NTFS_TOUPPER(ch) (ntfs_toupper_tab[(ch)])
73 static struct lock ntfs_toupper_lock
;
74 static signed int ntfs_toupper_usecount
;
75 extern struct iconv_functions
*ntfs_iconv
;
77 /* support macro for ntfs_ntvattrget() */
78 #define NTFS_AALPCMP(aalp,type,name,namelen) ( \
79 (aalp->al_type == type) && (aalp->al_namelen == namelen) && \
80 !NTFS_UASTRCMP(aalp->al_name,aalp->al_namelen,name,namelen) )
86 ntfs_ntvattrrele(struct ntvattr
*vap
)
88 dprintf(("ntfs_ntvattrrele: ino: %"PRId64
", type: 0x%x\n",
89 vap
->va_ip
->i_number
, vap
->va_type
));
91 ntfs_ntrele(vap
->va_ip
);
97 * find the attribute in the ntnode
100 ntfs_findvattr(struct ntfsmount
*ntmp
, struct ntnode
*ip
,
101 struct ntvattr
**lvapp
, struct ntvattr
**vapp
, u_int32_t type
,
102 const char *name
, size_t namelen
, cn_t vcn
)
107 if((ip
->i_flag
& IN_LOADED
) == 0) {
108 dprintf(("ntfs_findvattr: node not loaded, ino: %"PRId64
"\n",
110 error
= ntfs_loadntnode(ntmp
,ip
);
112 kprintf("ntfs_findvattr: FAILED TO LOAD INO: %"PRId64
"\n",
120 for (vap
= ip
->i_valist
.lh_first
; vap
; vap
= vap
->va_list.le_next
) {
121 ddprintf(("ntfs_findvattr: type: 0x%x, vcn: %d - %d\n", \
122 vap
->va_type
, (u_int32_t
) vap
->va_vcnstart
, \
123 (u_int32_t
) vap
->va_vcnend
));
124 if ((vap
->va_type
== type
) &&
125 (vap
->va_vcnstart
<= vcn
) && (vap
->va_vcnend
>= vcn
) &&
126 (vap
->va_namelen
== namelen
) &&
127 (strncmp(name
, vap
->va_name
, namelen
) == 0)) {
129 ntfs_ntref(vap
->va_ip
);
132 if (vap
->va_type
== NTFS_A_ATTRLIST
)
140 * Search attribute specifed in ntnode (load ntnode if nessecary).
141 * If not found but ATTR_A_ATTRLIST present, read it in and search throught.
142 * VOP_VGET node needed, and lookup througth it's ntnode (load if nessesary).
144 * ntnode should be locked
147 ntfs_ntvattrget(struct ntfsmount
*ntmp
, struct ntnode
*ip
, u_int32_t type
,
148 const char *name
, cn_t vcn
, struct ntvattr
**vapp
)
150 struct ntvattr
*lvap
= NULL
;
151 struct attr_attrlist
*aalp
;
152 struct attr_attrlist
*nextaalp
;
154 struct ntnode
*newip
;
162 dprintf(("ntfs_ntvattrget: " \
163 "ino: %"PRId64
", type: 0x%x, name: %s, vcn: %d\n", \
164 ip
->i_number
, type
, name
, (u_int32_t
) vcn
));
165 namelen
= strlen(name
);
167 dprintf(("ntfs_ntvattrget: " \
168 "ino: %"PRId64
", type: 0x%x, vcn: %d\n", \
169 ip
->i_number
, type
, (u_int32_t
) vcn
));
174 error
= ntfs_findvattr(ntmp
, ip
, &lvap
, vapp
, type
, name
, namelen
, vcn
);
179 dprintf(("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: " \
180 "ino: %"PRId64
", type: 0x%x, name: %s, vcn: %d\n", \
181 ip
->i_number
, type
, name
, (u_int32_t
) vcn
));
184 /* Scan $ATTRIBUTE_LIST for requested attribute */
185 len
= lvap
->va_datalen
;
186 alpool
= kmalloc(len
, M_TEMP
, M_WAITOK
);
187 error
= ntfs_readntvattr_plain(ntmp
, ip
, lvap
, 0, len
, alpool
, &len
,
192 aalp
= (struct attr_attrlist
*) alpool
;
195 for(; len
> 0; aalp
= nextaalp
) {
196 dprintf(("ntfs_ntvattrget: " \
197 "attrlist: ino: %d, attr: 0x%x, vcn: %d\n", \
198 aalp
->al_inumber
, aalp
->al_type
, \
199 (u_int32_t
) aalp
->al_vcnstart
));
201 if (len
> aalp
->reclen
) {
202 nextaalp
= NTFS_NEXTREC(aalp
, struct attr_attrlist
*);
208 if (!NTFS_AALPCMP(aalp
, type
, name
, namelen
) ||
209 (nextaalp
&& (nextaalp
->al_vcnstart
<= vcn
) &&
210 NTFS_AALPCMP(nextaalp
, type
, name
, namelen
)))
213 dprintf(("ntfs_ntvattrget: attribute in ino: %d\n",
216 /* this is not a main record, so we can't use just plain
218 error
= ntfs_vgetex(ntmp
->ntm_mountp
, aalp
->al_inumber
,
219 NTFS_A_DATA
, NULL
, LK_EXCLUSIVE
,
220 VG_EXT
, curthread
, &newvp
);
222 kprintf("ntfs_ntvattrget: CAN'T VGET INO: %d\n",
226 newip
= VTONT(newvp
);
227 /* XXX have to lock ntnode */
228 error
= ntfs_findvattr(ntmp
, newip
, &lvap
, vapp
,
229 type
, name
, namelen
, vcn
);
233 kprintf("ntfs_ntvattrget: ATTRLIST ERROR.\n");
238 dprintf(("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: " \
239 "ino: %"PRId64
", type: 0x%x, name: %.*s, vcn: %d\n", \
240 ip
->i_number
, type
, (int) namelen
, name
, (u_int32_t
) vcn
));
242 kfree(alpool
, M_TEMP
);
247 * Read ntnode from disk, make ntvattr list.
249 * ntnode should be locked
252 ntfs_loadntnode(struct ntfsmount
*ntmp
, struct ntnode
*ip
)
254 struct filerec
*mfrp
;
258 struct ntvattr
*nvap
;
260 dprintf(("ntfs_loadntnode: loading ino: %"PRId64
"\n",ip
->i_number
));
262 mfrp
= kmalloc(ntfs_bntob(ntmp
->ntm_bpmftrec
), M_TEMP
, M_WAITOK
);
264 if (ip
->i_number
< NTFS_SYSNODESNUM
) {
267 dprintf(("ntfs_loadntnode: read system node\n"));
269 bn
= ntfs_cntobn(ntmp
->ntm_mftcn
) +
270 ntmp
->ntm_bpmftrec
* ip
->i_number
;
272 error
= bread(ntmp
->ntm_devvp
,
273 ntfs_bntodoff(bn
), ntfs_bntob(ntmp
->ntm_bpmftrec
), &bp
);
275 kprintf("ntfs_loadntnode: BREAD FAILED\n");
279 memcpy(mfrp
, bp
->b_data
, ntfs_bntob(ntmp
->ntm_bpmftrec
));
284 vp
= ntmp
->ntm_sysvn
[NTFS_MFTINO
];
285 error
= ntfs_readattr(ntmp
, VTONT(vp
), NTFS_A_DATA
, NULL
,
286 ip
->i_number
* ntfs_bntob(ntmp
->ntm_bpmftrec
),
287 ntfs_bntob(ntmp
->ntm_bpmftrec
), mfrp
, NULL
);
289 kprintf("ntfs_loadntnode: ntfs_readattr failed\n");
294 /* Check if magic and fixups are correct */
295 error
= ntfs_procfixups(ntmp
, NTFS_FILEMAGIC
, (caddr_t
)mfrp
,
296 ntfs_bntob(ntmp
->ntm_bpmftrec
));
298 kprintf("ntfs_loadntnode: BAD MFT RECORD %"PRId64
"\n",
303 dprintf(("ntfs_loadntnode: load attrs for ino: %"PRId64
"\n",ip
->i_number
));
304 off
= mfrp
->fr_attroff
;
305 ap
= (struct attr
*) ((caddr_t
)mfrp
+ off
);
307 LIST_INIT(&ip
->i_valist
);
309 while (ap
->a_hdr
.a_type
!= -1) {
310 error
= ntfs_attrtontvattr(ntmp
, &nvap
, ap
);
315 LIST_INSERT_HEAD(&ip
->i_valist
, nvap
, va_list);
317 off
+= ap
->a_hdr
.reclen
;
318 ap
= (struct attr
*) ((caddr_t
)mfrp
+ off
);
321 kprintf("ntfs_loadntnode: failed to load attr ino: %"PRId64
"\n",
326 ip
->i_mainrec
= mfrp
->fr_mainrec
;
327 ip
->i_nlink
= mfrp
->fr_nlink
;
328 ip
->i_frflag
= mfrp
->fr_flags
;
330 ip
->i_flag
|= IN_LOADED
;
338 * Routine locks ntnode and increase usecount, just opposite of
342 ntfs_ntget(struct ntnode
*ip
)
344 dprintf(("ntfs_ntget: get ntnode %"PRId64
": %p, usecount: %d\n",
345 ip
->i_number
, ip
, ip
->i_usecount
));
347 ip
->i_usecount
++; /* ZZZ */
348 LOCKMGR(&ip
->i_lock
, LK_EXCLUSIVE
);
354 * Routine search ntnode in hash, if found: lock, inc usecount and return.
355 * If not in hash allocate structure for ntnode, prefill it, lock,
356 * inc count and return.
358 * ntnode returned locked
361 ntfs_ntlookup(struct ntfsmount
*ntmp
, ino_t ino
, struct ntnode
**ipp
)
365 dprintf(("ntfs_ntlookup: looking for ntnode %ju\n", (uintmax_t)ino
));
368 if ((ip
= ntfs_nthashlookup(ntmp
->ntm_dev
, ino
)) != NULL
) {
370 dprintf(("ntfs_ntlookup: ntnode %ju: %p, usecount: %d\n",
371 (uintmax_t)ino
, ip
, ip
->i_usecount
));
375 } while (LOCKMGR(&ntfs_hashlock
, LK_EXCLUSIVE
| LK_SLEEPFAIL
));
377 ip
= kmalloc(sizeof(struct ntnode
), M_NTFSNTNODE
, M_WAITOK
| M_ZERO
);
378 ddprintf(("ntfs_ntlookup: allocating ntnode: %ju: %p\n", ino
, ip
));
380 /* Generic initialization */
381 ip
->i_devvp
= ntmp
->ntm_devvp
;
382 ip
->i_dev
= ntmp
->ntm_dev
;
386 LIST_INIT(&ip
->i_fnlist
);
389 /* init lock and lock the newborn ntnode */
390 lockinit(&ip
->i_lock
, "ntnode", 0, LK_EXCLUSIVE
);
391 spin_init(&ip
->i_interlock
, "ntfsntlookup");
396 LOCKMGR(&ntfs_hashlock
, LK_RELEASE
);
400 dprintf(("ntfs_ntlookup: ntnode %ju: %p, usecount: %d\n",
401 (uintmax_t)ino
, ip
, ip
->i_usecount
));
407 * Decrement usecount of ntnode and unlock it, if usecount reach zero,
410 * ntnode should be locked on entry, and unlocked on return.
413 ntfs_ntput(struct ntnode
*ip
)
417 dprintf(("ntfs_ntput: rele ntnode %"PRId64
": %p, usecount: %d\n",
418 ip
->i_number
, ip
, ip
->i_usecount
));
420 spin_lock(&ip
->i_interlock
);
424 if (ip
->i_usecount
< 0) {
425 spin_unlock(&ip
->i_interlock
);
426 panic("ntfs_ntput: ino: %"PRId64
" usecount: %d ",
427 ip
->i_number
,ip
->i_usecount
);
431 if (ip
->i_usecount
> 0) {
432 spin_unlock(&ip
->i_interlock
);
433 LOCKMGR(&ip
->i_lock
, LK_RELEASE
);
437 dprintf(("ntfs_ntput: deallocating ntnode: %"PRId64
"\n", ip
->i_number
));
439 if (ip
->i_fnlist
.lh_first
) {
440 spin_unlock(&ip
->i_interlock
);
441 panic("ntfs_ntput: ntnode has fnodes");
445 * XXX this is a bit iffy because we are making high level calls
446 * while holding a spinlock.
450 while ((vap
= LIST_FIRST(&ip
->i_valist
)) != NULL
) {
451 LIST_REMOVE(vap
,va_list);
452 ntfs_freentvattr(vap
);
454 spin_unlock(&ip
->i_interlock
);
456 kfree(ip
, M_NTFSNTNODE
);
460 * increment usecount of ntnode
463 ntfs_ntref(struct ntnode
*ip
)
467 dprintf(("ntfs_ntref: ino %"PRId64
", usecount: %d\n",
468 ip
->i_number
, ip
->i_usecount
));
472 * Decrement usecount of ntnode.
475 ntfs_ntrele(struct ntnode
*ip
)
477 dprintf(("ntfs_ntrele: rele ntnode %"PRId64
": %p, usecount: %d\n",
478 ip
->i_number
, ip
, ip
->i_usecount
));
480 spin_lock(&ip
->i_interlock
);
483 if (ip
->i_usecount
< 0) {
484 spin_unlock(&ip
->i_interlock
);
485 panic("ntfs_ntrele: ino: %"PRId64
" usecount: %d ",
486 ip
->i_number
,ip
->i_usecount
);
488 spin_unlock(&ip
->i_interlock
);
492 * Deallocate all memory allocated for ntvattr
495 ntfs_freentvattr(struct ntvattr
*vap
)
497 if (vap
->va_flag
& NTFS_AF_INRUN
) {
499 kfree(vap
->va_vruncn
, M_NTFSRUN
);
501 kfree(vap
->va_vruncl
, M_NTFSRUN
);
504 kfree(vap
->va_datap
, M_NTFSRDATA
);
506 kfree(vap
, M_NTFSNTVATTR
);
510 * Convert disk image of attribute into ntvattr structure,
511 * runs are expanded also.
514 ntfs_attrtontvattr(struct ntfsmount
*ntmp
, struct ntvattr
**rvapp
,
523 vap
= kmalloc(sizeof(struct ntvattr
), M_NTFSNTVATTR
,
526 vap
->va_flag
= rap
->a_hdr
.a_flag
;
527 vap
->va_type
= rap
->a_hdr
.a_type
;
528 vap
->va_compression
= rap
->a_hdr
.a_compression
;
529 vap
->va_index
= rap
->a_hdr
.a_index
;
531 ddprintf(("type: 0x%x, index: %d", vap
->va_type
, vap
->va_index
));
533 vap
->va_namelen
= rap
->a_hdr
.a_namelen
;
534 if (rap
->a_hdr
.a_namelen
) {
535 wchar
*unp
= (wchar
*) ((caddr_t
) rap
+ rap
->a_hdr
.a_nameoff
);
536 ddprintf((", name:["));
537 for (i
= 0; i
< vap
->va_namelen
; i
++) {
538 vap
->va_name
[i
] = unp
[i
];
539 ddprintf(("%c", vap
->va_name
[i
]));
543 if (vap
->va_flag
& NTFS_AF_INRUN
) {
544 ddprintf((", nonres."));
545 vap
->va_datalen
= rap
->a_nr
.a_datalen
;
546 vap
->va_allocated
= rap
->a_nr
.a_allocated
;
547 vap
->va_vcnstart
= rap
->a_nr
.a_vcnstart
;
548 vap
->va_vcnend
= rap
->a_nr
.a_vcnend
;
549 vap
->va_compressalg
= rap
->a_nr
.a_compressalg
;
550 error
= ntfs_runtovrun(&(vap
->va_vruncn
), &(vap
->va_vruncl
),
552 (caddr_t
) rap
+ rap
->a_nr
.a_dataoff
);
554 vap
->va_compressalg
= 0;
555 ddprintf((", res."));
556 vap
->va_datalen
= rap
->a_r
.a_datalen
;
557 vap
->va_allocated
= rap
->a_r
.a_datalen
;
558 vap
->va_vcnstart
= 0;
559 vap
->va_vcnend
= ntfs_btocn(vap
->va_allocated
);
560 vap
->va_datap
= kmalloc(vap
->va_datalen
, M_NTFSRDATA
,
562 memcpy(vap
->va_datap
, (caddr_t
) rap
+ rap
->a_r
.a_dataoff
,
565 ddprintf((", len: %d", vap
->va_datalen
));
568 kfree(vap
, M_NTFSNTVATTR
);
578 * Expand run into more utilizable and more memory eating format.
581 ntfs_runtovrun(cn_t
**rcnp
, cn_t
**rclp
, u_long
*rcntp
, u_int8_t
*run
)
595 off
+= (run
[off
] & 0xF) + ((run
[off
] >> 4) & 0xF) + 1;
598 cn
= kmalloc(cnt
* sizeof(cn_t
), M_NTFSRUN
, M_WAITOK
);
599 cl
= kmalloc(cnt
* sizeof(cn_t
), M_NTFSRUN
, M_WAITOK
);
609 for (i
= 0; i
< (sz
& 0xF); i
++)
610 cl
[cnt
] += (u_int32_t
) run
[off
++] << (i
<< 3);
613 if (run
[off
+ sz
- 1] & 0x80) {
614 tmp
= ((u_int64_t
) - 1) << (sz
<< 3);
615 for (i
= 0; i
< sz
; i
++)
616 tmp
|= (u_int64_t
) run
[off
++] << (i
<< 3);
619 for (i
= 0; i
< sz
; i
++)
620 tmp
|= (u_int64_t
) run
[off
++] << (i
<< 3);
623 prev
= cn
[cnt
] = prev
+ tmp
;
636 * Compare unicode and ascii string case insens.
639 ntfs_uastricmp(struct ntfsmount
*ntmp
, const wchar
*ustr
, size_t ustrlen
,
640 const char *astr
, size_t astrlen
)
643 size_t i
, j
, mbstrlen
= astrlen
;
647 len
= 0; /* avoid gcc warnings */
648 if (ntmp
->ntm_ic_l2u
) {
649 for (i
= 0, j
= 0; i
< ustrlen
&& j
< astrlen
; i
++, j
++) {
650 if (j
< astrlen
-1) {
651 wc
= (wchar
)astr
[j
]<<8 | (astr
[j
+1]&0xFF);
654 wc
= (wchar
)astr
[j
]<<8 & 0xFF00;
657 res
= ((int) NTFS_TOUPPER(ustr
[i
])) -
658 ((int)NTFS_TOUPPER(NTFS_82U(wc
, &len
)));
667 * We use NTFS_82U(NTFS_U28(c)) to get rid of unicode
668 * symbols not covered by translation table
670 for (i
= 0; i
< ustrlen
&& i
< astrlen
; i
++) {
671 res
= ((int) NTFS_TOUPPER(NTFS_82U(NTFS_U28(ustr
[i
]), &len
))) -
672 ((int)NTFS_TOUPPER(NTFS_82U((wchar
)astr
[i
], &len
)));
677 return (ustrlen
- mbstrlen
);
681 * Compare unicode and ascii string case sens.
684 ntfs_uastrcmp(struct ntfsmount
*ntmp
, const wchar
*ustr
, size_t ustrlen
,
685 const char *astr
, size_t astrlen
)
688 size_t i
, j
, mbstrlen
= astrlen
;
692 for (i
= 0, j
= 0; (i
< ustrlen
) && (j
< astrlen
); i
++, j
++) {
694 wc
= NTFS_U28(ustr
[i
]);
697 if (u
!= '\0' && j
< astrlen
-1) {
698 res
= (int) (u
- astr
[j
++]);
701 res
= (res
<<8) + (int) (l
- astr
[j
]);
705 return (ustrlen
- mbstrlen
);
709 * Search fnode in ntnode, if not found allocate and preinitialize.
711 * ntnode should be locked on entry.
714 ntfs_fget(struct ntfsmount
*ntmp
, struct ntnode
*ip
, int attrtype
,
715 char *attrname
, struct fnode
**fpp
)
719 dprintf(("ntfs_fget: ino: %"PRId64
", attrtype: 0x%x, attrname: %s\n",
720 ip
->i_number
,attrtype
, attrname
?attrname
:""));
722 for (fp
= ip
->i_fnlist
.lh_first
; fp
!= NULL
; fp
= fp
->f_fnlist
.le_next
){
723 dprintf(("ntfs_fget: fnode: attrtype: %d, attrname: %s\n",
724 fp
->f_attrtype
, fp
->f_attrname
?fp
->f_attrname
:""));
726 if ((attrtype
== fp
->f_attrtype
) &&
727 ((!attrname
&& !fp
->f_attrname
) ||
728 (attrname
&& fp
->f_attrname
&&
729 !strcmp(attrname
,fp
->f_attrname
)))){
730 dprintf(("ntfs_fget: found existed: %p\n",fp
));
738 fp
= kmalloc(sizeof(struct fnode
), M_NTFSFNODE
, M_WAITOK
| M_ZERO
);
739 dprintf(("ntfs_fget: allocating fnode: %p\n",fp
));
743 fp
->f_flag
|= FN_AATTRNAME
;
744 fp
->f_attrname
= kmalloc(strlen(attrname
) + 1, M_TEMP
,
746 strcpy(fp
->f_attrname
, attrname
);
748 fp
->f_attrname
= NULL
;
749 fp
->f_attrtype
= attrtype
;
753 LIST_INSERT_HEAD(&ip
->i_fnlist
, fp
, f_fnlist
);
761 * Deallocate fnode, remove it from ntnode's fnode list.
763 * ntnode should be locked.
766 ntfs_frele(struct fnode
*fp
)
768 struct ntnode
*ip
= FTONT(fp
);
770 dprintf(("ntfs_frele: fnode: %p for %"PRId64
": %p\n", fp
, ip
->i_number
, ip
));
772 dprintf(("ntfs_frele: deallocating fnode\n"));
773 LIST_REMOVE(fp
,f_fnlist
);
774 if (fp
->f_flag
& FN_AATTRNAME
)
775 kfree(fp
->f_attrname
, M_TEMP
);
777 kfree(fp
->f_dirblbuf
, M_NTFSDIR
);
778 kfree(fp
, M_NTFSFNODE
);
783 * Lookup attribute name in format: [[:$ATTR_TYPE]:$ATTR_NAME],
784 * $ATTR_TYPE is searched in attrdefs read from $AttrDefs.
785 * If $ATTR_TYPE nott specifed, ATTR_A_DATA assumed.
788 ntfs_ntlookupattr(struct ntfsmount
*ntmp
, const char *name
, int namelen
,
789 int *attrtype
, char **attrname
)
793 struct ntvattrdef
*adp
;
798 if (name
[0] == '$') {
800 for (syslen
= 0; syslen
< namelen
; syslen
++) {
801 if(sys
[syslen
] == ':') {
811 for (i
= 0; i
< ntmp
->ntm_adnum
; i
++, adp
++){
812 if (syslen
!= adp
->ad_namelen
||
813 strncmp(sys
, adp
->ad_name
, syslen
) != 0)
816 *attrtype
= adp
->ad_type
;
821 *attrtype
= NTFS_A_DATA
;
825 (*attrname
) = kmalloc(namelen
, M_TEMP
, M_WAITOK
);
826 memcpy((*attrname
), name
, namelen
);
827 (*attrname
)[namelen
] = '\0';
834 * Lookup specifed node for filename, matching cnp,
835 * return fnode filled.
838 ntfs_ntlookupfile(struct ntfsmount
*ntmp
, struct vnode
*vp
,
839 struct componentname
*cnp
, struct vnode
**vpp
)
841 struct fnode
*fp
= VTOF(vp
);
842 struct ntnode
*ip
= FTONT(fp
);
843 struct ntvattr
*vap
; /* Root attribute */
844 cn_t cn
; /* VCN in current attribute */
845 caddr_t rdbuf
; /* Buffer to read directory's blocks */
847 u_int32_t rdsize
; /* Length of data to read from current block */
848 struct attr_indexentry
*iep
;
849 int error
, res
, anamelen
, fnamelen
;
850 const char *fname
,*aname
;
852 int attrtype
= NTFS_A_DATA
;
853 char *attrname
= NULL
;
858 error
= ntfs_ntget(ip
);
862 error
= ntfs_ntvattrget(ntmp
, ip
, NTFS_A_INDXROOT
, "$I30", 0, &vap
);
863 if (error
|| (vap
->va_flag
& NTFS_AF_INRUN
))
866 blsize
= vap
->va_a_iroot
->ir_size
;
867 rdsize
= vap
->va_datalen
;
870 * Divide file name into: foofilefoofilefoofile[:attrspec]
871 * Store like this: fname:fnamelen [aname:anamelen]
873 fname
= cnp
->cn_nameptr
;
876 for (fnamelen
= 0; fnamelen
< cnp
->cn_namelen
; fnamelen
++)
877 if(fname
[fnamelen
] == ':') {
878 aname
= fname
+ fnamelen
+ 1;
879 anamelen
= cnp
->cn_namelen
- fnamelen
- 1;
880 dprintf(("ntfs_ntlookupfile: %s (%d), attr: %s (%d)\n",
881 fname
, fnamelen
, aname
, anamelen
));
885 dprintf(("ntfs_ntlookupfile: blksz: %d, rdsz: %d\n", blsize
, rdsize
));
887 rdbuf
= kmalloc(blsize
, M_TEMP
, M_WAITOK
);
889 error
= ntfs_readattr(ntmp
, ip
, NTFS_A_INDXROOT
, "$I30",
890 0, rdsize
, rdbuf
, NULL
);
894 aoff
= sizeof(struct attr_indexroot
);
897 iep
= (struct attr_indexentry
*) (rdbuf
+ aoff
);
899 for (; !(iep
->ie_flag
& NTFS_IEFLAG_LAST
) && (rdsize
> aoff
);
901 iep
= (struct attr_indexentry
*) (rdbuf
+ aoff
))
903 ddprintf(("scan: %d, %d\n",
904 (u_int32_t
) iep
->ie_number
,
905 (u_int32_t
) iep
->ie_fnametype
));
907 /* check the name - the case-insensitible check
908 * has to come first, to break from this for loop
909 * if needed, so we can dive correctly */
910 res
= NTFS_UASTRICMP(iep
->ie_fname
, iep
->ie_fnamelen
,
913 if (res
< 0) continue;
915 if (iep
->ie_fnametype
== 0 ||
916 !(ntmp
->ntm_flag
& NTFS_MFLAG_CASEINS
))
918 res
= NTFS_UASTRCMP(iep
->ie_fname
,
919 iep
->ie_fnamelen
, fname
, fnamelen
);
920 if (res
!= 0) continue;
924 error
= ntfs_ntlookupattr(ntmp
,
926 &attrtype
, &attrname
);
931 /* Check if we've found ourself */
932 if ((iep
->ie_number
== ip
->i_number
) &&
933 (attrtype
== fp
->f_attrtype
) &&
934 ((!attrname
&& !fp
->f_attrname
) ||
935 (attrname
&& fp
->f_attrname
&&
936 !strcmp(attrname
, fp
->f_attrname
))))
944 /* vget node, but don't load it */
945 error
= ntfs_vgetex(ntmp
->ntm_mountp
,
946 iep
->ie_number
, attrtype
, attrname
,
947 LK_EXCLUSIVE
, VG_DONTLOADIN
| VG_DONTVALIDFN
,
950 /* free the buffer returned by ntfs_ntlookupattr() */
952 kfree(attrname
, M_TEMP
);
961 if (nfp
->f_flag
& FN_VALID
) {
966 nfp
->f_fflag
= iep
->ie_fflag
;
967 nfp
->f_pnumber
= iep
->ie_fpnumber
;
968 nfp
->f_times
= iep
->ie_ftimes
;
970 if((nfp
->f_fflag
& NTFS_FFLAG_DIR
) &&
971 (nfp
->f_attrtype
== NTFS_A_DATA
) &&
972 (nfp
->f_attrname
== NULL
))
977 nvp
->v_type
= f_type
;
979 if ((nfp
->f_attrtype
== NTFS_A_DATA
) &&
980 (nfp
->f_attrname
== NULL
))
982 /* Opening default attribute */
983 nfp
->f_size
= iep
->ie_fsize
;
984 nfp
->f_allocated
= iep
->ie_fallocated
;
985 nfp
->f_flag
|= FN_PRELOADED
;
987 error
= ntfs_filesize(ntmp
, nfp
,
988 &nfp
->f_size
, &nfp
->f_allocated
);
994 nfp
->f_flag
&= ~FN_VALID
;
997 * Normal files use the buffer cache
999 if (nvp
->v_type
== VREG
)
1000 vinitvmio(nvp
, nfp
->f_size
, PAGE_SIZE
, -1);
1005 /* Dive if possible */
1006 if (iep
->ie_flag
& NTFS_IEFLAG_SUBNODE
) {
1007 dprintf(("ntfs_ntlookupfile: diving\n"));
1009 cn
= *(cn_t
*) (rdbuf
+ aoff
+
1010 iep
->reclen
- sizeof(cn_t
));
1013 error
= ntfs_readattr(ntmp
, ip
, NTFS_A_INDX
, "$I30",
1014 ntfs_cntob(cn
), rdsize
, rdbuf
, NULL
);
1018 error
= ntfs_procfixups(ntmp
, NTFS_INDXMAGIC
,
1023 aoff
= (((struct attr_indexalloc
*) rdbuf
)->ia_hdrsize
+
1026 dprintf(("ntfs_ntlookupfile: nowhere to dive :-(\n"));
1032 dprintf(("finish\n"));
1035 if (attrname
) kfree(attrname
, M_TEMP
);
1036 ntfs_ntvattrrele(vap
);
1038 kfree(rdbuf
, M_TEMP
);
1043 * Check if name type is permitted to show.
1046 ntfs_isnamepermitted(struct ntfsmount
*ntmp
, struct attr_indexentry
*iep
)
1048 if (ntmp
->ntm_flag
& NTFS_MFLAG_ALLNAMES
)
1051 switch (iep
->ie_fnametype
) {
1053 ddprintf(("ntfs_isnamepermitted: skipped DOS name\n"));
1055 case 0: case 1: case 3:
1058 kprintf("ntfs_isnamepermitted: " \
1059 "WARNING! Unknown file name type: %d\n",
1067 * Read ntfs dir like stream of attr_indexentry, not like btree of them.
1068 * This is done by scaning $BITMAP:$I30 for busy clusters and reading them.
1069 * Ofcouse $INDEX_ROOT:$I30 is read before. Last read values are stored in
1070 * fnode, so we can skip toward record number num almost immediatly.
1071 * Anyway this is rather slow routine. The problem is that we don't know
1072 * how many records are there in $INDEX_ALLOCATION:$I30 block.
1075 ntfs_ntreaddir(struct ntfsmount
*ntmp
, struct fnode
*fp
,
1076 u_int32_t num
, struct attr_indexentry
**riepp
)
1078 struct ntnode
*ip
= FTONT(fp
);
1079 struct ntvattr
*vap
= NULL
; /* IndexRoot attribute */
1080 struct ntvattr
*bmvap
= NULL
; /* BitMap attribute */
1081 struct ntvattr
*iavap
= NULL
; /* IndexAllocation attribute */
1082 caddr_t rdbuf
; /* Buffer to read directory's blocks */
1083 u_char
*bmp
= NULL
; /* Bitmap */
1084 u_int32_t blsize
; /* Index allocation size (2048) */
1085 u_int32_t rdsize
; /* Length of data to read */
1086 u_int32_t attrnum
; /* Current attribute type */
1087 u_int32_t cpbl
= 1; /* Clusters per directory block */
1089 struct attr_indexentry
*iep
;
1091 u_int32_t aoff
, cnum
;
1093 dprintf(("ntfs_ntreaddir: read ino: %"PRId64
", num: %d\n", ip
->i_number
, num
));
1094 error
= ntfs_ntget(ip
);
1098 error
= ntfs_ntvattrget(ntmp
, ip
, NTFS_A_INDXROOT
, "$I30", 0, &vap
);
1102 if (fp
->f_dirblbuf
== NULL
) {
1103 fp
->f_dirblsz
= vap
->va_a_iroot
->ir_size
;
1104 fp
->f_dirblbuf
= kmalloc(max(vap
->va_datalen
, fp
->f_dirblsz
),
1105 M_NTFSDIR
, M_WAITOK
);
1108 blsize
= fp
->f_dirblsz
;
1109 rdbuf
= fp
->f_dirblbuf
;
1111 dprintf(("ntfs_ntreaddir: rdbuf: 0x%p, blsize: %d\n", rdbuf
, blsize
));
1113 if (vap
->va_a_iroot
->ir_flag
& NTFS_IRFLAG_INDXALLOC
) {
1114 error
= ntfs_ntvattrget(ntmp
, ip
, NTFS_A_INDXBITMAP
, "$I30",
1120 bmp
= kmalloc(bmvap
->va_datalen
, M_TEMP
, M_WAITOK
);
1121 error
= ntfs_readattr(ntmp
, ip
, NTFS_A_INDXBITMAP
, "$I30", 0,
1122 bmvap
->va_datalen
, bmp
, NULL
);
1126 error
= ntfs_ntvattrget(ntmp
, ip
, NTFS_A_INDX
, "$I30",
1132 cpbl
= ntfs_btocn(blsize
+ ntfs_cntob(1) - 1);
1133 dprintf(("ntfs_ntreaddir: indexalloc: %d, cpbl: %d\n",
1134 iavap
->va_datalen
, cpbl
));
1136 dprintf(("ntfs_ntreadidir: w/o BitMap and IndexAllocation\n"));
1137 iavap
= bmvap
= NULL
;
1141 /* Try use previous values */
1142 if ((fp
->f_lastdnum
< num
) && (fp
->f_lastdnum
!= 0)) {
1143 attrnum
= fp
->f_lastdattr
;
1144 aoff
= fp
->f_lastdoff
;
1145 blnum
= fp
->f_lastdblnum
;
1146 cnum
= fp
->f_lastdnum
;
1148 attrnum
= NTFS_A_INDXROOT
;
1149 aoff
= sizeof(struct attr_indexroot
);
1155 dprintf(("ntfs_ntreaddir: scan: 0x%x, %d, %d, %d, %d\n",
1156 attrnum
, blnum
, cnum
, num
, aoff
));
1157 rdsize
= (attrnum
== NTFS_A_INDXROOT
) ? vap
->va_datalen
: blsize
;
1158 error
= ntfs_readattr(ntmp
, ip
, attrnum
, "$I30",
1159 ntfs_cntob(blnum
* cpbl
), rdsize
, rdbuf
, NULL
);
1163 if (attrnum
== NTFS_A_INDX
) {
1164 error
= ntfs_procfixups(ntmp
, NTFS_INDXMAGIC
,
1170 aoff
= (attrnum
== NTFS_A_INDX
) ?
1171 (0x18 + ((struct attr_indexalloc
*) rdbuf
)->ia_hdrsize
) :
1172 sizeof(struct attr_indexroot
);
1174 iep
= (struct attr_indexentry
*) (rdbuf
+ aoff
);
1175 for (; !(iep
->ie_flag
& NTFS_IEFLAG_LAST
) && (rdsize
> aoff
);
1176 aoff
+= iep
->reclen
,
1177 iep
= (struct attr_indexentry
*) (rdbuf
+ aoff
))
1179 if (!ntfs_isnamepermitted(ntmp
, iep
)) continue;
1182 fp
->f_lastdnum
= cnum
;
1183 fp
->f_lastdoff
= aoff
;
1184 fp
->f_lastdblnum
= blnum
;
1185 fp
->f_lastdattr
= attrnum
;
1196 if (attrnum
== NTFS_A_INDXROOT
)
1201 while (ntfs_cntob(blnum
* cpbl
) < iavap
->va_datalen
) {
1202 if (bmp
[blnum
>> 3] & (1 << (blnum
& 3)))
1207 attrnum
= NTFS_A_INDX
;
1209 if (ntfs_cntob(blnum
* cpbl
) >= iavap
->va_datalen
)
1211 dprintf(("ntfs_ntreaddir: blnum: %d\n", blnum
));
1220 ntfs_ntvattrrele(vap
);
1222 ntfs_ntvattrrele(bmvap
);
1224 ntfs_ntvattrrele(iavap
);
1232 * Convert NTFS times that are in 100 ns units and begins from
1233 * 1601 Jan 1 into unix times.
1236 ntfs_nttimetounix(u_int64_t nt
)
1240 /* WindowNT times are in 100 ns and from 1601 Jan 1 */
1241 t
.tv_nsec
= (nt
% (1000 * 1000 * 10)) * 100;
1242 t
.tv_sec
= nt
/ (1000 * 1000 * 10) -
1243 369LL * 365LL * 24LL * 60LL * 60LL -
1244 89LL * 1LL * 24LL * 60LL * 60LL;
1249 * Get file times from NTFS_A_NAME attribute.
1252 ntfs_times(struct ntfsmount
*ntmp
, struct ntnode
*ip
, ntfs_times_t
*tm
)
1254 struct ntvattr
*vap
;
1257 dprintf(("ntfs_times: ino: %"PRId64
"...\n", ip
->i_number
));
1259 error
= ntfs_ntget(ip
);
1263 error
= ntfs_ntvattrget(ntmp
, ip
, NTFS_A_NAME
, NULL
, 0, &vap
);
1268 *tm
= vap
->va_a_name
->n_times
;
1269 ntfs_ntvattrrele(vap
);
1276 * Get file sizes from corresponding attribute.
1278 * ntnode under fnode should be locked.
1281 ntfs_filesize(struct ntfsmount
*ntmp
, struct fnode
*fp
, u_int64_t
*size
,
1284 struct ntvattr
*vap
;
1285 struct ntnode
*ip
= FTONT(fp
);
1289 dprintf(("ntfs_filesize: ino: %"PRId64
"\n", ip
->i_number
));
1291 error
= ntfs_ntvattrget(ntmp
, ip
,
1292 fp
->f_attrtype
, fp
->f_attrname
, 0, &vap
);
1296 bn
= vap
->va_allocated
;
1297 sz
= vap
->va_datalen
;
1299 dprintf(("ntfs_filesize: %d bytes (%d bytes allocated)\n",
1300 (u_int32_t
) sz
, (u_int32_t
) bn
));
1307 ntfs_ntvattrrele(vap
);
1313 * This is one of write routine.
1316 ntfs_writeattr_plain(struct ntfsmount
*ntmp
, struct ntnode
*ip
,
1317 u_int32_t attrnum
, char *attrname
, off_t roff
,
1318 size_t rsize
, void *rdata
, size_t *initp
,
1323 off_t off
= roff
, left
= rsize
, towrite
;
1324 caddr_t data
= rdata
;
1325 struct ntvattr
*vap
;
1329 error
= ntfs_ntvattrget(ntmp
, ip
, attrnum
, attrname
,
1330 ntfs_btocn(off
), &vap
);
1333 towrite
= min(left
, ntfs_cntob(vap
->va_vcnend
+ 1) - off
);
1334 ddprintf(("ntfs_writeattr_plain: o: %d, s: %d (%d - %d)\n",
1335 (u_int32_t
) off
, (u_int32_t
) towrite
,
1336 (u_int32_t
) vap
->va_vcnstart
,
1337 (u_int32_t
) vap
->va_vcnend
));
1338 error
= ntfs_writentvattr_plain(ntmp
, ip
, vap
,
1339 off
- ntfs_cntob(vap
->va_vcnstart
),
1340 towrite
, data
, &init
, uio
);
1342 kprintf("ntfs_writeattr_plain: " \
1343 "ntfs_writentvattr_plain failed: o: %d, s: %d\n",
1344 (u_int32_t
) off
, (u_int32_t
) towrite
);
1345 kprintf("ntfs_writeattr_plain: attrib: %d - %d\n",
1346 (u_int32_t
) vap
->va_vcnstart
,
1347 (u_int32_t
) vap
->va_vcnend
);
1348 ntfs_ntvattrrele(vap
);
1351 ntfs_ntvattrrele(vap
);
1354 data
= data
+ towrite
;
1362 * This is one of write routine.
1364 * ntnode should be locked.
1367 ntfs_writentvattr_plain(struct ntfsmount
*ntmp
, struct ntnode
*ip
,
1368 struct ntvattr
*vap
, off_t roff
, size_t rsize
,
1369 void *rdata
, size_t *initp
, struct uio
*uio
)
1374 cn_t ccn
, ccl
, cn
, left
, cl
;
1375 caddr_t data
= rdata
;
1381 if ((vap
->va_flag
& NTFS_AF_INRUN
) == 0) {
1382 kprintf("ntfs_writevattr_plain: CAN'T WRITE RES. ATTRIBUTE\n");
1386 ddprintf(("ntfs_writentvattr_plain: data in run: %ld chains\n",
1394 for (; left
&& (cnt
< vap
->va_vruncnt
); cnt
++) {
1395 ccn
= vap
->va_vruncn
[cnt
];
1396 ccl
= vap
->va_vruncl
[cnt
];
1398 ddprintf(("ntfs_writentvattr_plain: " \
1399 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1400 (u_int32_t
) left
, (u_int32_t
) ccn
, \
1401 (u_int32_t
) ccl
, (u_int32_t
) off
));
1403 if (ntfs_cntob(ccl
) < off
) {
1404 off
-= ntfs_cntob(ccl
);
1408 if (!ccn
&& ip
->i_number
!= NTFS_BOOTINO
)
1411 ccl
-= ntfs_btocn(off
);
1412 cn
= ccn
+ ntfs_btocn(off
);
1413 off
= ntfs_btocnoff(off
);
1415 while (left
&& ccl
) {
1417 * Always read and write single clusters at a time -
1418 * we need to avoid requesting differently-sized
1419 * blocks at the same disk offsets to avoid
1420 * confusing the buffer cache.
1422 tocopy
= min(left
, ntfs_cntob(1) - off
);
1423 cl
= ntfs_btocl(tocopy
+ off
);
1424 KASSERT(cl
== 1 && tocopy
<= ntfs_cntob(1),
1425 ("single cluster limit mistake"));
1426 ddprintf(("ntfs_writentvattr_plain: write: " \
1427 "cn: 0x%x cl: %d, off: %d len: %d, left: %d\n",
1428 (u_int32_t
) cn
, (u_int32_t
) cl
,
1429 (u_int32_t
) off
, (u_int32_t
) tocopy
,
1431 if (off
== 0 && tocopy
== ntfs_cntob(cl
) &&
1432 uio
->uio_segflg
!= UIO_NOCOPY
) {
1433 bp
= getblk(ntmp
->ntm_devvp
, ntfs_cntodoff(cn
),
1434 ntfs_cntob(cl
), 0, 0);
1437 error
= bread(ntmp
->ntm_devvp
,
1439 ntfs_cntob(cl
), &bp
);
1446 uiomovebp(bp
, bp
->b_data
+ off
, tocopy
, uio
);
1448 memcpy(bp
->b_data
+ off
, data
, tocopy
);
1450 data
= data
+ tocopy
;
1460 kprintf("ntfs_writentvattr_plain: POSSIBLE RUN ERROR\n");
1468 * This is one of read routines.
1470 * ntnode should be locked.
1473 ntfs_readntvattr_plain(struct ntfsmount
*ntmp
, struct ntnode
*ip
,
1474 struct ntvattr
*vap
, off_t roff
, size_t rsize
,
1475 void *rdata
, size_t *initp
, struct uio
*uio
)
1481 if (vap
->va_flag
& NTFS_AF_INRUN
) {
1483 cn_t ccn
, ccl
, cn
, left
, cl
;
1484 caddr_t data
= rdata
;
1488 ddprintf(("ntfs_readntvattr_plain: data in run: %ld chains\n",
1496 while (left
&& (cnt
< vap
->va_vruncnt
)) {
1497 ccn
= vap
->va_vruncn
[cnt
];
1498 ccl
= vap
->va_vruncl
[cnt
];
1500 ddprintf(("ntfs_readntvattr_plain: " \
1501 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1502 (u_int32_t
) left
, (u_int32_t
) ccn
, \
1503 (u_int32_t
) ccl
, (u_int32_t
) off
));
1505 if (ntfs_cntob(ccl
) < off
) {
1506 off
-= ntfs_cntob(ccl
);
1510 if (ccn
|| ip
->i_number
== NTFS_BOOTINO
) {
1511 ccl
-= ntfs_btocn(off
);
1512 cn
= ccn
+ ntfs_btocn(off
);
1513 off
= ntfs_btocnoff(off
);
1515 while (left
&& ccl
) {
1517 min(ntfs_cntob(ccl
) - off
,
1519 cl
= ntfs_btocl(tocopy
+ off
);
1522 * Always read single clusters at a
1523 * time - we need to avoid reading
1524 * differently-sized blocks at the
1525 * same disk offsets to avoid
1526 * confusing the buffer cache.
1529 ntfs_cntob(1) - off
);
1530 cl
= ntfs_btocl(tocopy
+ off
);
1532 tocopy
<= ntfs_cntob(1),
1533 ("single cluster limit mistake"));
1535 ddprintf(("ntfs_readntvattr_plain: " \
1536 "read: cn: 0x%x cl: %d, " \
1537 "off: %d len: %d, left: %d\n",
1543 error
= bread(ntmp
->ntm_devvp
,
1552 uiomovebp(bp
, bp
->b_data
+ off
,
1555 memcpy(data
, bp
->b_data
+ off
,
1559 data
= data
+ tocopy
;
1567 tocopy
= min(left
, ntfs_cntob(ccl
) - off
);
1568 ddprintf(("ntfs_readntvattr_plain: "
1569 "hole: ccn: 0x%x ccl: %d, off: %d, " \
1570 " len: %d, left: %d\n",
1571 (u_int32_t
) ccn
, (u_int32_t
) ccl
,
1572 (u_int32_t
) off
, (u_int32_t
) tocopy
,
1577 size_t remains
= tocopy
;
1578 for(; remains
; remains
--)
1579 uiomove("", 1, uio
);
1581 bzero(data
, tocopy
);
1582 data
= data
+ tocopy
;
1587 kprintf("ntfs_readntvattr_plain: POSSIBLE RUN ERROR\n");
1591 ddprintf(("ntfs_readnvattr_plain: data is in mft record\n"));
1593 uiomove(vap
->va_datap
+ roff
, rsize
, uio
);
1595 memcpy(rdata
, vap
->va_datap
+ roff
, rsize
);
1603 * This is one of read routines.
1606 ntfs_readattr_plain(struct ntfsmount
*ntmp
, struct ntnode
*ip
,
1607 u_int32_t attrnum
, char *attrname
, off_t roff
,
1608 size_t rsize
, void *rdata
, size_t * initp
,
1613 off_t off
= roff
, left
= rsize
, toread
;
1614 caddr_t data
= rdata
;
1615 struct ntvattr
*vap
;
1619 error
= ntfs_ntvattrget(ntmp
, ip
, attrnum
, attrname
,
1620 ntfs_btocn(off
), &vap
);
1623 toread
= min(left
, ntfs_cntob(vap
->va_vcnend
+ 1) - off
);
1624 ddprintf(("ntfs_readattr_plain: o: %d, s: %d (%d - %d)\n",
1625 (u_int32_t
) off
, (u_int32_t
) toread
,
1626 (u_int32_t
) vap
->va_vcnstart
,
1627 (u_int32_t
) vap
->va_vcnend
));
1628 error
= ntfs_readntvattr_plain(ntmp
, ip
, vap
,
1629 off
- ntfs_cntob(vap
->va_vcnstart
),
1630 toread
, data
, &init
, uio
);
1632 kprintf("ntfs_readattr_plain: " \
1633 "ntfs_readntvattr_plain failed: o: %d, s: %d\n",
1634 (u_int32_t
) off
, (u_int32_t
) toread
);
1635 kprintf("ntfs_readattr_plain: attrib: %d - %d\n",
1636 (u_int32_t
) vap
->va_vcnstart
,
1637 (u_int32_t
) vap
->va_vcnend
);
1638 ntfs_ntvattrrele(vap
);
1641 ntfs_ntvattrrele(vap
);
1644 data
= data
+ toread
;
1652 * This is one of read routines.
1655 ntfs_readattr(struct ntfsmount
*ntmp
, struct ntnode
*ip
, u_int32_t attrnum
,
1656 char *attrname
, off_t roff
, size_t rsize
, void *rdata
,
1660 struct ntvattr
*vap
;
1663 ddprintf(("ntfs_readattr: reading %"PRId64
": 0x%x, from %d size %d bytes\n",
1664 ip
->i_number
, attrnum
, (u_int32_t
) roff
, (u_int32_t
) rsize
));
1666 error
= ntfs_ntvattrget(ntmp
, ip
, attrnum
, attrname
, 0, &vap
);
1670 if ((roff
> vap
->va_datalen
) ||
1671 (roff
+ rsize
> vap
->va_datalen
)) {
1672 ddprintf(("ntfs_readattr: offset too big\n"));
1673 ntfs_ntvattrrele(vap
);
1676 if (vap
->va_compression
&& vap
->va_compressalg
) {
1679 off_t off
, left
= rsize
, tocopy
;
1680 caddr_t data
= rdata
;
1683 ddprintf(("ntfs_ntreadattr: compression: %d\n",
1684 vap
->va_compressalg
));
1686 cup
= kmalloc(ntfs_cntob(NTFS_COMPUNIT_CL
), M_NTFSDECOMP
,
1688 uup
= kmalloc(ntfs_cntob(NTFS_COMPUNIT_CL
), M_NTFSDECOMP
,
1691 cn
= (ntfs_btocn(roff
)) & (~(NTFS_COMPUNIT_CL
- 1));
1692 off
= roff
- ntfs_cntob(cn
);
1695 error
= ntfs_readattr_plain(ntmp
, ip
, attrnum
,
1696 attrname
, ntfs_cntob(cn
),
1697 ntfs_cntob(NTFS_COMPUNIT_CL
),
1702 tocopy
= min(left
, ntfs_cntob(NTFS_COMPUNIT_CL
) - off
);
1704 if (init
== ntfs_cntob(NTFS_COMPUNIT_CL
)) {
1706 uiomove(cup
+ off
, tocopy
, uio
);
1708 memcpy(data
, cup
+ off
, tocopy
);
1709 } else if (init
== 0) {
1711 size_t remains
= tocopy
;
1712 for(; remains
; remains
--)
1713 uiomove("", 1, uio
);
1716 bzero(data
, tocopy
);
1718 error
= ntfs_uncompunit(ntmp
, uup
, cup
);
1722 uiomove(uup
+ off
, tocopy
, uio
);
1724 memcpy(data
, uup
+ off
, tocopy
);
1728 data
= data
+ tocopy
;
1729 off
+= tocopy
- ntfs_cntob(NTFS_COMPUNIT_CL
);
1730 cn
+= NTFS_COMPUNIT_CL
;
1733 kfree(uup
, M_NTFSDECOMP
);
1734 kfree(cup
, M_NTFSDECOMP
);
1736 error
= ntfs_readattr_plain(ntmp
, ip
, attrnum
, attrname
,
1737 roff
, rsize
, rdata
, &init
, uio
);
1738 ntfs_ntvattrrele(vap
);
1744 ntfs_parserun(cn_t
*cn
, cn_t
*cl
, u_int8_t
*run
, u_long len
, u_long
*off
)
1750 kprintf("ntfs_parsetun: run == NULL\n");
1755 kprintf("ntfs_parserun: trying to go out of run\n");
1759 if ((sz
& 0xF) > 8 || (*off
) + (sz
& 0xF) > len
) {
1760 kprintf("ntfs_parserun: " \
1761 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1765 for (i
= 0; i
< (sz
& 0xF); i
++)
1766 *cl
+= (u_int32_t
) run
[(*off
)++] << (i
<< 3);
1769 if ((sz
& 0xF) > 8 || (*off
) + (sz
& 0xF) > len
) {
1770 kprintf("ntfs_parserun: " \
1771 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1775 for (i
= 0; i
< (sz
& 0xF); i
++)
1776 *cn
+= (u_int32_t
) run
[(*off
)++] << (i
<< 3);
1783 * Process fixup routine on given buffer.
1786 ntfs_procfixups(struct ntfsmount
*ntmp
, u_int32_t magic
, caddr_t buf
,
1789 struct fixuphdr
*fhp
= (struct fixuphdr
*) buf
;
1795 if (fhp
->fh_magic
!= magic
) {
1796 kprintf("ntfs_procfixups: magic doesn't match: %08x != %08x\n",
1797 fhp
->fh_magic
, magic
);
1800 if ((fhp
->fh_fnum
- 1) * ntmp
->ntm_bps
!= len
) {
1801 kprintf("ntfs_procfixups: " \
1802 "bad fixups number: %d for %ld bytes block\n",
1803 fhp
->fh_fnum
, (long)len
); /* XXX kprintf kludge */
1806 if (fhp
->fh_foff
>= ntmp
->ntm_spc
* ntmp
->ntm_mftrecsz
* ntmp
->ntm_bps
) {
1807 kprintf("ntfs_procfixups: invalid offset: %x", fhp
->fh_foff
);
1810 fxp
= (u_int16_t
*) (buf
+ fhp
->fh_foff
);
1811 cfxp
= (u_int16_t
*) (buf
+ ntmp
->ntm_bps
- 2);
1813 for (i
= 1; i
< fhp
->fh_fnum
; i
++, fxp
++) {
1814 if (*cfxp
!= fixup
) {
1815 kprintf("ntfs_procfixups: fixup %d doesn't match\n", i
);
1819 cfxp
= (u_int16_t
*)(((caddr_t
) cfxp
) + ntmp
->ntm_bps
);
1826 ntfs_runtocn(cn_t
*cn
, struct ntfsmount
*ntmp
, u_int8_t
*run
, u_long len
,
1836 kprintf("ntfs_runtocn: run: 0x%p, %ld bytes, vcn:%ld\n",
1837 run
, len
, (u_long
) vcn
);
1838 kprintf("ntfs_runtocn: run: ");
1839 for (i
= 0; i
< len
; i
++)
1840 kprintf("0x%02x ", run
[i
]);
1845 kprintf("ntfs_runtocn: run == NULL\n");
1849 if (run
[off
] == 0) {
1850 kprintf("ntfs_runtocn: vcn too big\n");
1854 error
= ntfs_parserun(&ccn
, &ccl
, run
, len
, &off
);
1856 kprintf("ntfs_runtocn: ntfs_parserun failed\n");
1859 } while (ccl
<= vcn
);
1866 * this initializes toupper table & dependant variables to be ready for
1870 ntfs_toupper_init(void)
1872 ntfs_toupper_tab
= NULL
;
1873 lockinit(&ntfs_toupper_lock
, "ntfs_toupper", 0, 0);
1874 ntfs_toupper_usecount
= 0;
1878 * if the ntfs_toupper_tab[] is filled already, just raise use count;
1879 * otherwise read the data from the filesystem we are currently mounting
1882 ntfs_toupper_use(struct mount
*mp
, struct ntfsmount
*ntmp
)
1887 /* get exclusive access */
1888 LOCKMGR(&ntfs_toupper_lock
, LK_EXCLUSIVE
);
1890 /* only read the translation data from a file if it hasn't been
1892 if (ntfs_toupper_tab
)
1896 * Read in Unicode lowercase -> uppercase translation file.
1897 * XXX for now, just the first 256 entries are used anyway,
1898 * so don't bother reading more
1900 ntfs_toupper_tab
= kmalloc(65536 * sizeof(wchar
), M_NTFSRDATA
,
1903 if ((error
= VFS_VGET(mp
, NULL
, NTFS_UPCASEINO
, &vp
)))
1905 error
= ntfs_readattr(ntmp
, VTONT(vp
), NTFS_A_DATA
, NULL
,
1906 0, 65536*sizeof(wchar
), (char *) ntfs_toupper_tab
, NULL
);
1910 ntfs_toupper_usecount
++;
1911 LOCKMGR(&ntfs_toupper_lock
, LK_RELEASE
);
1916 * lower the use count and if it reaches zero, free the memory
1917 * tied by toupper table
1920 ntfs_toupper_unuse(void)
1922 /* get exclusive access */
1923 LOCKMGR(&ntfs_toupper_lock
, LK_EXCLUSIVE
);
1925 ntfs_toupper_usecount
--;
1926 if (ntfs_toupper_usecount
== 0) {
1927 kfree(ntfs_toupper_tab
, M_NTFSRDATA
);
1928 ntfs_toupper_tab
= NULL
;
1931 else if (ntfs_toupper_usecount
< 0) {
1932 panic("ntfs_toupper_unuse(): use count negative: %d",
1933 ntfs_toupper_usecount
);
1937 /* release the lock */
1938 LOCKMGR(&ntfs_toupper_lock
, LK_RELEASE
);
1942 ntfs_u28_init(struct ntfsmount
*ntmp
, wchar
*u2w
, char *cs_local
,
1948 if (ntfs_iconv
&& cs_local
) {
1949 ntfs_iconv
->open(cs_local
, cs_ntfs
, &ntmp
->ntm_ic_u2l
);
1953 u28
= kmalloc(256 * sizeof(char *), M_TEMP
, M_WAITOK
| M_ZERO
);
1955 for (i
=0; i
<256; i
++) {
1956 h
= (u2w
[i
] >> 8) & 0xFF;
1959 if (u28
[h
] == NULL
) {
1960 u28
[h
] = kmalloc(256 * sizeof(char), M_TEMP
, M_WAITOK
);
1961 for (j
=0; j
<256; j
++)
1965 u28
[h
][l
] = i
& 0xFF;
1968 ntmp
->ntm_u28
= u28
;
1974 ntfs_u28_uninit(struct ntfsmount
*ntmp
)
1979 if (ntmp
->ntm_u28
== NULL
) {
1980 if (ntfs_iconv
&& ntmp
->ntm_ic_u2l
) {
1981 ntfs_iconv
->close(ntmp
->ntm_ic_u2l
);
1986 if (ntmp
->ntm_u28
== NULL
)
1989 u28
= ntmp
->ntm_u28
;
1991 for (i
=0; i
<256; i
++)
1993 kfree(u28
[i
], M_TEMP
);
2001 ntfs_82u_init(struct ntfsmount
*ntmp
, char *cs_local
, char *cs_ntfs
)
2007 if (ntfs_iconv
&& cs_local
) {
2008 ntfs_iconv
->open(cs_ntfs
, cs_local
, &ntmp
->ntm_ic_l2u
);
2012 _82u
= kmalloc(256 * sizeof(wchar
), M_TEMP
, M_WAITOK
);
2014 for (i
=0; i
<256; i
++)
2017 ntmp
->ntm_82u
= _82u
;
2023 ntfs_82u_uninit(struct ntfsmount
*ntmp
)
2025 if (ntmp
->ntm_82u
== NULL
) {
2026 if (ntfs_iconv
&& ntmp
->ntm_ic_l2u
) {
2027 ntfs_iconv
->close(ntmp
->ntm_ic_l2u
);
2032 kfree(ntmp
->ntm_82u
, M_TEMP
);
2037 * maps the Unicode char to 8bit equivalent
2038 * XXX currently only gets lower 8bit from the Unicode char
2039 * and substitutes a '_' for it if the result would be '\0';
2040 * something better has to be definitely though out
2043 ntfs_u28(struct ntfsmount
*ntmp
, wchar wc
)
2045 char *p
, *outp
, inbuf
[3], outbuf
[3];
2048 if (ntfs_iconv
&& ntmp
->ntm_ic_u2l
) {
2050 inbuf
[0] = (char)(wc
>>8);
2051 inbuf
[1] = (char)wc
;
2055 ntfs_iconv
->convchr(ntmp
->ntm_ic_u2l
,
2056 (const char **)(void *)&p
, &ilen
, &outp
, &olen
);
2058 return ((wchar
)(outbuf
[0]&0xFF));
2059 } else if (olen
== 0) {
2060 return ((wchar
)((outbuf
[0]<<8) | (outbuf
[1]&0xFF)));
2065 p
= ntmp
->ntm_u28
[(wc
>>8)&0xFF];
2068 return (p
[wc
&0xFF]);
2072 ntfs_82u(struct ntfsmount
*ntmp
,
2076 char *p
, *outp
, inbuf
[3], outbuf
[3];
2080 if (ntfs_iconv
&& ntmp
->ntm_ic_l2u
) {
2081 ilen
= (size_t)*len
;
2084 inbuf
[0] = (char)(wc
>>8);
2085 inbuf
[1] = (char)wc
;
2089 ntfs_iconv
->convchr(ntmp
->ntm_ic_l2u
,
2090 (const char **)(void *)&p
, &ilen
, &outp
, &olen
);
2092 uc
= (wchar
)((outbuf
[0]<<8) | (outbuf
[1]&0xFF));
2097 if (ntmp
->ntm_82u
!= NULL
)
2098 return (ntmp
->ntm_82u
[wc
&0xFF]);