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 $
29 * $DragonFly: src/sys/vfs/ntfs/ntfs_subr.c,v 1.17 2005/11/24 02:16:17 y0netan1 Exp $
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/systm.h>
36 #include <sys/namei.h>
37 #include <sys/kernel.h>
38 #include <sys/vnode.h>
39 #include <sys/mount.h>
42 #include <sys/malloc.h>
44 #include <sys/spinlock.h>
45 #include <sys/spinlock2.h>
47 #include <machine/inttypes.h>
49 #if defined(__NetBSD__)
50 #include <miscfs/specfs/specdev.h>
53 /* #define NTFS_DEBUG 1 */
55 #include "ntfsmount.h"
56 #include "ntfs_inode.h"
57 #include "ntfs_vfsops.h"
58 #include "ntfs_subr.h"
59 #include "ntfs_compr.h"
60 #include "ntfs_ihash.h"
62 #if defined(__DragonFly__)
63 MALLOC_DEFINE(M_NTFSNTVATTR
, "NTFS vattr", "NTFS file attribute information");
64 MALLOC_DEFINE(M_NTFSRDATA
, "NTFS res data", "NTFS resident data");
65 MALLOC_DEFINE(M_NTFSRUN
, "NTFS vrun", "NTFS vrun storage");
66 MALLOC_DEFINE(M_NTFSDECOMP
, "NTFS decomp", "NTFS decompression temporary");
69 static int ntfs_ntlookupattr (struct ntfsmount
*, const char *, int, int *, char **);
70 static int ntfs_findvattr (struct ntfsmount
*, struct ntnode
*, struct ntvattr
**, struct ntvattr
**, u_int32_t
, const char *, size_t, cn_t
);
71 static int ntfs_uastricmp (struct ntfsmount
*, const wchar
*, size_t, const char *, size_t);
72 static int ntfs_uastrcmp (struct ntfsmount
*, const wchar
*, size_t, const char *, size_t);
74 /* table for mapping Unicode chars into uppercase; it's filled upon first
75 * ntfs mount, freed upon last ntfs umount */
76 static wchar
*ntfs_toupper_tab
;
77 #define NTFS_TOUPPER(ch) (ntfs_toupper_tab[(ch)])
78 static struct lock ntfs_toupper_lock
;
79 static signed int ntfs_toupper_usecount
;
81 /* support macro for ntfs_ntvattrget() */
82 #define NTFS_AALPCMP(aalp,type,name,namelen) ( \
83 (aalp->al_type == type) && (aalp->al_namelen == namelen) && \
84 !NTFS_UASTRCMP(aalp->al_name,aalp->al_namelen,name,namelen) )
90 ntfs_ntvattrrele(struct ntvattr
*vap
)
92 dprintf(("ntfs_ntvattrrele: ino: %"PRId64
", type: 0x%x\n",
93 vap
->va_ip
->i_number
, vap
->va_type
));
95 ntfs_ntrele(vap
->va_ip
);
101 * find the attribute in the ntnode
104 ntfs_findvattr(struct ntfsmount
*ntmp
, struct ntnode
*ip
,
105 struct ntvattr
**lvapp
, struct ntvattr
**vapp
, u_int32_t type
,
106 const char *name
, size_t namelen
, cn_t vcn
)
111 if((ip
->i_flag
& IN_LOADED
) == 0) {
112 dprintf(("ntfs_findvattr: node not loaded, ino: %"PRId64
"\n",
114 error
= ntfs_loadntnode(ntmp
,ip
);
116 printf("ntfs_findvattr: FAILED TO LOAD INO: %"PRId64
"\n",
124 for (vap
= ip
->i_valist
.lh_first
; vap
; vap
= vap
->va_list.le_next
) {
125 ddprintf(("ntfs_findvattr: type: 0x%x, vcn: %d - %d\n", \
126 vap
->va_type
, (u_int32_t
) vap
->va_vcnstart
, \
127 (u_int32_t
) vap
->va_vcnend
));
128 if ((vap
->va_type
== type
) &&
129 (vap
->va_vcnstart
<= vcn
) && (vap
->va_vcnend
>= vcn
) &&
130 (vap
->va_namelen
== namelen
) &&
131 (strncmp(name
, vap
->va_name
, namelen
) == 0)) {
133 ntfs_ntref(vap
->va_ip
);
136 if (vap
->va_type
== NTFS_A_ATTRLIST
)
144 * Search attribute specifed in ntnode (load ntnode if nessecary).
145 * If not found but ATTR_A_ATTRLIST present, read it in and search throught.
146 * VOP_VGET node needed, and lookup througth it's ntnode (load if nessesary).
148 * ntnode should be locked
151 ntfs_ntvattrget(struct ntfsmount
*ntmp
, struct ntnode
*ip
, u_int32_t type
,
152 const char *name
, cn_t vcn
, struct ntvattr
**vapp
)
154 struct ntvattr
*lvap
= NULL
;
155 struct attr_attrlist
*aalp
;
156 struct attr_attrlist
*nextaalp
;
158 struct ntnode
*newip
;
166 dprintf(("ntfs_ntvattrget: " \
167 "ino: %"PRId64
", type: 0x%x, name: %s, vcn: %d\n", \
168 ip
->i_number
, type
, name
, (u_int32_t
) vcn
));
169 namelen
= strlen(name
);
171 dprintf(("ntfs_ntvattrget: " \
172 "ino: %"PRId64
", type: 0x%x, vcn: %d\n", \
173 ip
->i_number
, type
, (u_int32_t
) vcn
));
178 error
= ntfs_findvattr(ntmp
, ip
, &lvap
, vapp
, type
, name
, namelen
, vcn
);
183 dprintf(("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: " \
184 "ino: %"PRId64
", type: 0x%x, name: %s, vcn: %d\n", \
185 ip
->i_number
, type
, name
, (u_int32_t
) vcn
));
188 /* Scan $ATTRIBUTE_LIST for requested attribute */
189 len
= lvap
->va_datalen
;
190 MALLOC(alpool
, caddr_t
, len
, M_TEMP
, M_WAITOK
);
191 error
= ntfs_readntvattr_plain(ntmp
, ip
, lvap
, 0, len
, alpool
, &len
,
196 aalp
= (struct attr_attrlist
*) alpool
;
199 for(; len
> 0; aalp
= nextaalp
) {
200 dprintf(("ntfs_ntvattrget: " \
201 "attrlist: ino: %d, attr: 0x%x, vcn: %d\n", \
202 aalp
->al_inumber
, aalp
->al_type
, \
203 (u_int32_t
) aalp
->al_vcnstart
));
205 if (len
> aalp
->reclen
) {
206 nextaalp
= NTFS_NEXTREC(aalp
, struct attr_attrlist
*);
212 if (!NTFS_AALPCMP(aalp
, type
, name
, namelen
) ||
213 (nextaalp
&& (nextaalp
->al_vcnstart
<= vcn
) &&
214 NTFS_AALPCMP(nextaalp
, type
, name
, namelen
)))
217 dprintf(("ntfs_ntvattrget: attribute in ino: %d\n",
220 /* this is not a main record, so we can't use just plain
222 error
= ntfs_vgetex(ntmp
->ntm_mountp
, aalp
->al_inumber
,
223 NTFS_A_DATA
, NULL
, LK_EXCLUSIVE
,
224 VG_EXT
, curthread
, &newvp
);
226 printf("ntfs_ntvattrget: CAN'T VGET INO: %d\n",
230 newip
= VTONT(newvp
);
231 /* XXX have to lock ntnode */
232 error
= ntfs_findvattr(ntmp
, newip
, &lvap
, vapp
,
233 type
, name
, namelen
, vcn
);
237 printf("ntfs_ntvattrget: ATTRLIST ERROR.\n");
242 dprintf(("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: " \
243 "ino: %"PRId64
", type: 0x%x, name: %.*s, vcn: %d\n", \
244 ip
->i_number
, type
, (int) namelen
, name
, (u_int32_t
) vcn
));
246 FREE(alpool
, M_TEMP
);
251 * Read ntnode from disk, make ntvattr list.
253 * ntnode should be locked
256 ntfs_loadntnode(struct ntfsmount
*ntmp
, struct ntnode
*ip
)
258 struct filerec
*mfrp
;
262 struct ntvattr
*nvap
;
264 dprintf(("ntfs_loadntnode: loading ino: %"PRId64
"\n",ip
->i_number
));
266 MALLOC(mfrp
, struct filerec
*, ntfs_bntob(ntmp
->ntm_bpmftrec
),
269 if (ip
->i_number
< NTFS_SYSNODESNUM
) {
272 dprintf(("ntfs_loadntnode: read system node\n"));
274 bn
= ntfs_cntobn(ntmp
->ntm_mftcn
) +
275 ntmp
->ntm_bpmftrec
* ip
->i_number
;
277 error
= bread(ntmp
->ntm_devvp
,
278 bn
, ntfs_bntob(ntmp
->ntm_bpmftrec
), &bp
);
280 printf("ntfs_loadntnode: BREAD FAILED\n");
284 memcpy(mfrp
, bp
->b_data
, ntfs_bntob(ntmp
->ntm_bpmftrec
));
289 vp
= ntmp
->ntm_sysvn
[NTFS_MFTINO
];
290 error
= ntfs_readattr(ntmp
, VTONT(vp
), NTFS_A_DATA
, NULL
,
291 ip
->i_number
* ntfs_bntob(ntmp
->ntm_bpmftrec
),
292 ntfs_bntob(ntmp
->ntm_bpmftrec
), mfrp
, NULL
);
294 printf("ntfs_loadntnode: ntfs_readattr failed\n");
299 /* Check if magic and fixups are correct */
300 error
= ntfs_procfixups(ntmp
, NTFS_FILEMAGIC
, (caddr_t
)mfrp
,
301 ntfs_bntob(ntmp
->ntm_bpmftrec
));
303 printf("ntfs_loadntnode: BAD MFT RECORD %"PRId64
"\n",
308 dprintf(("ntfs_loadntnode: load attrs for ino: %"PRId64
"\n",ip
->i_number
));
309 off
= mfrp
->fr_attroff
;
310 ap
= (struct attr
*) ((caddr_t
)mfrp
+ off
);
312 LIST_INIT(&ip
->i_valist
);
314 while (ap
->a_hdr
.a_type
!= -1) {
315 error
= ntfs_attrtontvattr(ntmp
, &nvap
, ap
);
320 LIST_INSERT_HEAD(&ip
->i_valist
, nvap
, va_list);
322 off
+= ap
->a_hdr
.reclen
;
323 ap
= (struct attr
*) ((caddr_t
)mfrp
+ off
);
326 printf("ntfs_loadntnode: failed to load attr ino: %"PRId64
"\n",
331 ip
->i_mainrec
= mfrp
->fr_mainrec
;
332 ip
->i_nlink
= mfrp
->fr_nlink
;
333 ip
->i_frflag
= mfrp
->fr_flags
;
335 ip
->i_flag
|= IN_LOADED
;
343 * Routine locks ntnode and increase usecount, just opposite of
347 ntfs_ntget(struct ntnode
*ip
)
351 dprintf(("ntfs_ntget: get ntnode %"PRId64
": %p, usecount: %d\n",
352 ip
->i_number
, ip
, ip
->i_usecount
));
354 ip
->i_usecount
++; /* ZZZ */
355 LOCKMGR(&ip
->i_lock
, LK_EXCLUSIVE
, NULL
);
361 * Routine search ntnode in hash, if found: lock, inc usecount and return.
362 * If not in hash allocate structure for ntnode, prefill it, lock,
363 * inc count and return.
365 * ntnode returned locked
368 ntfs_ntlookup(struct ntfsmount
*ntmp
, ino_t ino
, struct ntnode
**ipp
)
372 dprintf(("ntfs_ntlookup: looking for ntnode %d\n", ino
));
375 if ((ip
= ntfs_nthashlookup(ntmp
->ntm_dev
, ino
)) != NULL
) {
377 dprintf(("ntfs_ntlookup: ntnode %d: %p, usecount: %d\n",
378 ino
, ip
, ip
->i_usecount
));
382 } while (LOCKMGR(&ntfs_hashlock
, LK_EXCLUSIVE
| LK_SLEEPFAIL
, NULL
));
384 MALLOC(ip
, struct ntnode
*, sizeof(struct ntnode
),
385 M_NTFSNTNODE
, M_WAITOK
);
386 ddprintf(("ntfs_ntlookup: allocating ntnode: %d: %p\n", ino
, ip
));
387 bzero((caddr_t
) ip
, sizeof(struct ntnode
));
389 /* Generic initialization */
390 ip
->i_devvp
= ntmp
->ntm_devvp
;
391 ip
->i_dev
= ntmp
->ntm_dev
;
395 LIST_INIT(&ip
->i_fnlist
);
398 /* init lock and lock the newborn ntnode */
399 lockinit(&ip
->i_lock
, 0, "ntnode", 0, LK_EXCLUSIVE
);
400 spin_init(&ip
->i_interlock
);
405 LOCKMGR(&ntfs_hashlock
, LK_RELEASE
, NULL
);
409 dprintf(("ntfs_ntlookup: ntnode %d: %p, usecount: %d\n",
410 ino
, ip
, ip
->i_usecount
));
416 * Decrement usecount of ntnode and unlock it, if usecount reach zero,
419 * ntnode should be locked on entry, and unlocked on return.
422 ntfs_ntput(struct ntnode
*ip
)
426 dprintf(("ntfs_ntput: rele ntnode %"PRId64
": %p, usecount: %d\n",
427 ip
->i_number
, ip
, ip
->i_usecount
));
429 spin_lock(&ip
->i_interlock
);
433 if (ip
->i_usecount
< 0) {
434 spin_unlock(&ip
->i_interlock
);
435 panic("ntfs_ntput: ino: %"PRId64
" usecount: %d \n",
436 ip
->i_number
,ip
->i_usecount
);
440 if (ip
->i_usecount
> 0) {
441 LOCKMGR(&ip
->i_lock
, LK_RELEASE
|LK_INTERLOCK
, &ip
->i_interlock
);
445 dprintf(("ntfs_ntput: deallocating ntnode: %"PRId64
"\n", ip
->i_number
));
447 if (ip
->i_fnlist
.lh_first
) {
448 spin_unlock(&ip
->i_interlock
);
449 panic("ntfs_ntput: ntnode has fnodes\n");
453 * XXX this is a bit iffy because we are making high level calls
454 * while holding a spinlock.
458 while ((vap
= LIST_FIRST(&ip
->i_valist
)) != NULL
) {
459 LIST_REMOVE(vap
,va_list);
460 ntfs_freentvattr(vap
);
462 spin_unlock(&ip
->i_interlock
);
464 FREE(ip
, M_NTFSNTNODE
);
468 * increment usecount of ntnode
471 ntfs_ntref(struct ntnode
*ip
)
475 dprintf(("ntfs_ntref: ino %"PRId64
", usecount: %d\n",
476 ip
->i_number
, ip
->i_usecount
));
481 * Decrement usecount of ntnode.
484 ntfs_ntrele(struct ntnode
*ip
)
488 dprintf(("ntfs_ntrele: rele ntnode %"PRId64
": %p, usecount: %d\n",
489 ip
->i_number
, ip
, ip
->i_usecount
));
491 spin_lock(&ip
->i_interlock
);
494 if (ip
->i_usecount
< 0) {
495 spin_unlock(&ip
->i_interlock
);
496 panic("ntfs_ntrele: ino: %"PRId64
" usecount: %d \n",
497 ip
->i_number
,ip
->i_usecount
);
499 spin_unlock(&ip
->i_interlock
);
503 * Deallocate all memory allocated for ntvattr
506 ntfs_freentvattr(struct ntvattr
*vap
)
508 if (vap
->va_flag
& NTFS_AF_INRUN
) {
510 FREE(vap
->va_vruncn
, M_NTFSRUN
);
512 FREE(vap
->va_vruncl
, M_NTFSRUN
);
515 FREE(vap
->va_datap
, M_NTFSRDATA
);
517 FREE(vap
, M_NTFSNTVATTR
);
521 * Convert disk image of attribute into ntvattr structure,
522 * runs are expanded also.
525 ntfs_attrtontvattr(struct ntfsmount
*ntmp
, struct ntvattr
**rvapp
,
534 MALLOC(vap
, struct ntvattr
*, sizeof(struct ntvattr
),
535 M_NTFSNTVATTR
, M_WAITOK
);
536 bzero(vap
, sizeof(struct ntvattr
));
538 vap
->va_flag
= rap
->a_hdr
.a_flag
;
539 vap
->va_type
= rap
->a_hdr
.a_type
;
540 vap
->va_compression
= rap
->a_hdr
.a_compression
;
541 vap
->va_index
= rap
->a_hdr
.a_index
;
543 ddprintf(("type: 0x%x, index: %d", vap
->va_type
, vap
->va_index
));
545 vap
->va_namelen
= rap
->a_hdr
.a_namelen
;
546 if (rap
->a_hdr
.a_namelen
) {
547 wchar
*unp
= (wchar
*) ((caddr_t
) rap
+ rap
->a_hdr
.a_nameoff
);
548 ddprintf((", name:["));
549 for (i
= 0; i
< vap
->va_namelen
; i
++) {
550 vap
->va_name
[i
] = unp
[i
];
551 ddprintf(("%c", vap
->va_name
[i
]));
555 if (vap
->va_flag
& NTFS_AF_INRUN
) {
556 ddprintf((", nonres."));
557 vap
->va_datalen
= rap
->a_nr
.a_datalen
;
558 vap
->va_allocated
= rap
->a_nr
.a_allocated
;
559 vap
->va_vcnstart
= rap
->a_nr
.a_vcnstart
;
560 vap
->va_vcnend
= rap
->a_nr
.a_vcnend
;
561 vap
->va_compressalg
= rap
->a_nr
.a_compressalg
;
562 error
= ntfs_runtovrun(&(vap
->va_vruncn
), &(vap
->va_vruncl
),
564 (caddr_t
) rap
+ rap
->a_nr
.a_dataoff
);
566 vap
->va_compressalg
= 0;
567 ddprintf((", res."));
568 vap
->va_datalen
= rap
->a_r
.a_datalen
;
569 vap
->va_allocated
= rap
->a_r
.a_datalen
;
570 vap
->va_vcnstart
= 0;
571 vap
->va_vcnend
= ntfs_btocn(vap
->va_allocated
);
572 MALLOC(vap
->va_datap
, caddr_t
, vap
->va_datalen
,
573 M_NTFSRDATA
, M_WAITOK
);
574 memcpy(vap
->va_datap
, (caddr_t
) rap
+ rap
->a_r
.a_dataoff
,
577 ddprintf((", len: %d", vap
->va_datalen
));
580 FREE(vap
, M_NTFSNTVATTR
);
590 * Expand run into more utilizable and more memory eating format.
593 ntfs_runtovrun(cn_t
**rcnp
, cn_t
**rclp
, u_long
*rcntp
, u_int8_t
*run
)
607 off
+= (run
[off
] & 0xF) + ((run
[off
] >> 4) & 0xF) + 1;
610 MALLOC(cn
, cn_t
*, cnt
* sizeof(cn_t
), M_NTFSRUN
, M_WAITOK
);
611 MALLOC(cl
, cn_t
*, cnt
* sizeof(cn_t
), M_NTFSRUN
, M_WAITOK
);
621 for (i
= 0; i
< (sz
& 0xF); i
++)
622 cl
[cnt
] += (u_int32_t
) run
[off
++] << (i
<< 3);
625 if (run
[off
+ sz
- 1] & 0x80) {
626 tmp
= ((u_int64_t
) - 1) << (sz
<< 3);
627 for (i
= 0; i
< sz
; i
++)
628 tmp
|= (u_int64_t
) run
[off
++] << (i
<< 3);
631 for (i
= 0; i
< sz
; i
++)
632 tmp
|= (u_int64_t
) run
[off
++] << (i
<< 3);
635 prev
= cn
[cnt
] = prev
+ tmp
;
648 * Compare unicode and ascii string case insens.
651 ntfs_uastricmp(struct ntfsmount
*ntmp
, const wchar
*ustr
, size_t ustrlen
,
652 const char *astr
, size_t astrlen
)
658 * XXX We use NTFS_82U(NTFS_U28(c)) to get rid of unicode
659 * symbols not covered by translation table
661 for (i
= 0; i
< ustrlen
&& i
< astrlen
; i
++) {
662 res
= ((int) NTFS_TOUPPER(NTFS_82U(NTFS_U28(ustr
[i
])))) -
663 ((int)NTFS_TOUPPER(NTFS_82U(astr
[i
])));
667 return (ustrlen
- astrlen
);
671 * Compare unicode and ascii string case sens.
674 ntfs_uastrcmp(struct ntfsmount
*ntmp
, const wchar
*ustr
, size_t ustrlen
,
675 const char *astr
, size_t astrlen
)
680 for (i
= 0; (i
< ustrlen
) && (i
< astrlen
); i
++) {
681 res
= (int) (((char)NTFS_U28(ustr
[i
])) - astr
[i
]);
685 return (ustrlen
- astrlen
);
689 * Search fnode in ntnode, if not found allocate and preinitialize.
691 * ntnode should be locked on entry.
694 ntfs_fget(struct ntfsmount
*ntmp
, struct ntnode
*ip
, int attrtype
,
695 char *attrname
, struct fnode
**fpp
)
699 dprintf(("ntfs_fget: ino: %"PRId64
", attrtype: 0x%x, attrname: %s\n",
700 ip
->i_number
,attrtype
, attrname
?attrname
:""));
702 for (fp
= ip
->i_fnlist
.lh_first
; fp
!= NULL
; fp
= fp
->f_fnlist
.le_next
){
703 dprintf(("ntfs_fget: fnode: attrtype: %d, attrname: %s\n",
704 fp
->f_attrtype
, fp
->f_attrname
?fp
->f_attrname
:""));
706 if ((attrtype
== fp
->f_attrtype
) &&
707 ((!attrname
&& !fp
->f_attrname
) ||
708 (attrname
&& fp
->f_attrname
&&
709 !strcmp(attrname
,fp
->f_attrname
)))){
710 dprintf(("ntfs_fget: found existed: %p\n",fp
));
718 MALLOC(fp
, struct fnode
*, sizeof(struct fnode
), M_NTFSFNODE
, M_WAITOK
);
719 bzero(fp
, sizeof(struct fnode
));
720 dprintf(("ntfs_fget: allocating fnode: %p\n",fp
));
724 fp
->f_flag
|= FN_AATTRNAME
;
725 MALLOC(fp
->f_attrname
, char *, strlen(attrname
)+1, M_TEMP
, M_WAITOK
);
726 strcpy(fp
->f_attrname
, attrname
);
728 fp
->f_attrname
= NULL
;
729 fp
->f_attrtype
= attrtype
;
733 LIST_INSERT_HEAD(&ip
->i_fnlist
, fp
, f_fnlist
);
741 * Deallocate fnode, remove it from ntnode's fnode list.
743 * ntnode should be locked.
746 ntfs_frele(struct fnode
*fp
)
748 struct ntnode
*ip
= FTONT(fp
);
750 dprintf(("ntfs_frele: fnode: %p for %"PRId64
": %p\n", fp
, ip
->i_number
, ip
));
752 dprintf(("ntfs_frele: deallocating fnode\n"));
753 LIST_REMOVE(fp
,f_fnlist
);
754 if (fp
->f_flag
& FN_AATTRNAME
)
755 FREE(fp
->f_attrname
, M_TEMP
);
757 FREE(fp
->f_dirblbuf
, M_NTFSDIR
);
758 FREE(fp
, M_NTFSFNODE
);
763 * Lookup attribute name in format: [[:$ATTR_TYPE]:$ATTR_NAME],
764 * $ATTR_TYPE is searched in attrdefs read from $AttrDefs.
765 * If $ATTR_TYPE nott specifed, ATTR_A_DATA assumed.
768 ntfs_ntlookupattr(struct ntfsmount
*ntmp
, const char *name
, int namelen
,
769 int *attrtype
, char **attrname
)
773 struct ntvattrdef
*adp
;
778 if (name
[0] == '$') {
780 for (syslen
= 0; syslen
< namelen
; syslen
++) {
781 if(sys
[syslen
] == ':') {
791 for (i
= 0; i
< ntmp
->ntm_adnum
; i
++, adp
++){
792 if (syslen
!= adp
->ad_namelen
||
793 strncmp(sys
, adp
->ad_name
, syslen
) != 0)
796 *attrtype
= adp
->ad_type
;
801 *attrtype
= NTFS_A_DATA
;
805 MALLOC((*attrname
), char *, namelen
, M_TEMP
, M_WAITOK
);
806 memcpy((*attrname
), name
, namelen
);
807 (*attrname
)[namelen
] = '\0';
814 * Lookup specifed node for filename, matching cnp,
815 * return fnode filled.
818 ntfs_ntlookupfile(struct ntfsmount
*ntmp
, struct vnode
*vp
,
819 struct componentname
*cnp
, struct vnode
**vpp
)
821 struct fnode
*fp
= VTOF(vp
);
822 struct ntnode
*ip
= FTONT(fp
);
823 struct ntvattr
*vap
; /* Root attribute */
824 cn_t cn
; /* VCN in current attribute */
825 caddr_t rdbuf
; /* Buffer to read directory's blocks */
827 u_int32_t rdsize
; /* Length of data to read from current block */
828 struct attr_indexentry
*iep
;
829 int error
, res
, anamelen
, fnamelen
;
830 const char *fname
,*aname
;
832 int attrtype
= NTFS_A_DATA
;
833 char *attrname
= NULL
;
838 error
= ntfs_ntget(ip
);
842 error
= ntfs_ntvattrget(ntmp
, ip
, NTFS_A_INDXROOT
, "$I30", 0, &vap
);
843 if (error
|| (vap
->va_flag
& NTFS_AF_INRUN
))
846 blsize
= vap
->va_a_iroot
->ir_size
;
847 rdsize
= vap
->va_datalen
;
850 * Divide file name into: foofilefoofilefoofile[:attrspec]
851 * Store like this: fname:fnamelen [aname:anamelen]
853 fname
= cnp
->cn_nameptr
;
856 for (fnamelen
= 0; fnamelen
< cnp
->cn_namelen
; fnamelen
++)
857 if(fname
[fnamelen
] == ':') {
858 aname
= fname
+ fnamelen
+ 1;
859 anamelen
= cnp
->cn_namelen
- fnamelen
- 1;
860 dprintf(("ntfs_ntlookupfile: %s (%d), attr: %s (%d)\n",
861 fname
, fnamelen
, aname
, anamelen
));
865 dprintf(("ntfs_ntlookupfile: blksz: %d, rdsz: %d\n", blsize
, rdsize
));
867 MALLOC(rdbuf
, caddr_t
, blsize
, M_TEMP
, M_WAITOK
);
869 error
= ntfs_readattr(ntmp
, ip
, NTFS_A_INDXROOT
, "$I30",
870 0, rdsize
, rdbuf
, NULL
);
874 aoff
= sizeof(struct attr_indexroot
);
877 iep
= (struct attr_indexentry
*) (rdbuf
+ aoff
);
879 for (; !(iep
->ie_flag
& NTFS_IEFLAG_LAST
) && (rdsize
> aoff
);
881 iep
= (struct attr_indexentry
*) (rdbuf
+ aoff
))
883 ddprintf(("scan: %d, %d\n",
884 (u_int32_t
) iep
->ie_number
,
885 (u_int32_t
) iep
->ie_fnametype
));
887 /* check the name - the case-insensitible check
888 * has to come first, to break from this for loop
889 * if needed, so we can dive correctly */
890 res
= NTFS_UASTRICMP(iep
->ie_fname
, iep
->ie_fnamelen
,
893 if (res
< 0) continue;
895 if (iep
->ie_fnametype
== 0 ||
896 !(ntmp
->ntm_flag
& NTFS_MFLAG_CASEINS
))
898 res
= NTFS_UASTRCMP(iep
->ie_fname
,
899 iep
->ie_fnamelen
, fname
, fnamelen
);
900 if (res
!= 0) continue;
904 error
= ntfs_ntlookupattr(ntmp
,
906 &attrtype
, &attrname
);
911 /* Check if we've found ourself */
912 if ((iep
->ie_number
== ip
->i_number
) &&
913 (attrtype
== fp
->f_attrtype
) &&
914 ((!attrname
&& !fp
->f_attrname
) ||
915 (attrname
&& fp
->f_attrname
&&
916 !strcmp(attrname
, fp
->f_attrname
))))
924 /* vget node, but don't load it */
925 error
= ntfs_vgetex(ntmp
->ntm_mountp
,
926 iep
->ie_number
, attrtype
, attrname
,
927 LK_EXCLUSIVE
, VG_DONTLOADIN
| VG_DONTVALIDFN
,
930 /* free the buffer returned by ntfs_ntlookupattr() */
932 FREE(attrname
, M_TEMP
);
941 if (nfp
->f_flag
& FN_VALID
) {
946 nfp
->f_fflag
= iep
->ie_fflag
;
947 nfp
->f_pnumber
= iep
->ie_fpnumber
;
948 nfp
->f_times
= iep
->ie_ftimes
;
950 if((nfp
->f_fflag
& NTFS_FFLAG_DIR
) &&
951 (nfp
->f_attrtype
== NTFS_A_DATA
) &&
952 (nfp
->f_attrname
== NULL
))
957 nvp
->v_type
= f_type
;
959 if ((nfp
->f_attrtype
== NTFS_A_DATA
) &&
960 (nfp
->f_attrname
== NULL
))
962 /* Opening default attribute */
963 nfp
->f_size
= iep
->ie_fsize
;
964 nfp
->f_allocated
= iep
->ie_fallocated
;
965 nfp
->f_flag
|= FN_PRELOADED
;
967 error
= ntfs_filesize(ntmp
, nfp
,
968 &nfp
->f_size
, &nfp
->f_allocated
);
975 nfp
->f_flag
&= ~FN_VALID
;
980 /* Dive if possible */
981 if (iep
->ie_flag
& NTFS_IEFLAG_SUBNODE
) {
982 dprintf(("ntfs_ntlookupfile: diving\n"));
984 cn
= *(cn_t
*) (rdbuf
+ aoff
+
985 iep
->reclen
- sizeof(cn_t
));
988 error
= ntfs_readattr(ntmp
, ip
, NTFS_A_INDX
, "$I30",
989 ntfs_cntob(cn
), rdsize
, rdbuf
, NULL
);
993 error
= ntfs_procfixups(ntmp
, NTFS_INDXMAGIC
,
998 aoff
= (((struct attr_indexalloc
*) rdbuf
)->ia_hdrsize
+
1001 dprintf(("ntfs_ntlookupfile: nowhere to dive :-(\n"));
1007 dprintf(("finish\n"));
1010 if (attrname
) FREE(attrname
, M_TEMP
);
1011 ntfs_ntvattrrele(vap
);
1013 FREE(rdbuf
, M_TEMP
);
1018 * Check if name type is permitted to show.
1021 ntfs_isnamepermitted(struct ntfsmount
*ntmp
, struct attr_indexentry
*iep
)
1023 if (ntmp
->ntm_flag
& NTFS_MFLAG_ALLNAMES
)
1026 switch (iep
->ie_fnametype
) {
1028 ddprintf(("ntfs_isnamepermitted: skiped DOS name\n"));
1030 case 0: case 1: case 3:
1033 printf("ntfs_isnamepermitted: " \
1034 "WARNING! Unknown file name type: %d\n",
1042 * Read ntfs dir like stream of attr_indexentry, not like btree of them.
1043 * This is done by scaning $BITMAP:$I30 for busy clusters and reading them.
1044 * Ofcouse $INDEX_ROOT:$I30 is read before. Last read values are stored in
1045 * fnode, so we can skip toward record number num almost immediatly.
1046 * Anyway this is rather slow routine. The problem is that we don't know
1047 * how many records are there in $INDEX_ALLOCATION:$I30 block.
1050 ntfs_ntreaddir(struct ntfsmount
*ntmp
, struct fnode
*fp
,
1051 u_int32_t num
, struct attr_indexentry
**riepp
)
1053 struct ntnode
*ip
= FTONT(fp
);
1054 struct ntvattr
*vap
= NULL
; /* IndexRoot attribute */
1055 struct ntvattr
*bmvap
= NULL
; /* BitMap attribute */
1056 struct ntvattr
*iavap
= NULL
; /* IndexAllocation attribute */
1057 caddr_t rdbuf
; /* Buffer to read directory's blocks */
1058 u_char
*bmp
= NULL
; /* Bitmap */
1059 u_int32_t blsize
; /* Index allocation size (2048) */
1060 u_int32_t rdsize
; /* Length of data to read */
1061 u_int32_t attrnum
; /* Current attribute type */
1062 u_int32_t cpbl
= 1; /* Clusters per directory block */
1064 struct attr_indexentry
*iep
;
1066 u_int32_t aoff
, cnum
;
1068 dprintf(("ntfs_ntreaddir: read ino: %"PRId64
", num: %d\n", ip
->i_number
, num
));
1069 error
= ntfs_ntget(ip
);
1073 error
= ntfs_ntvattrget(ntmp
, ip
, NTFS_A_INDXROOT
, "$I30", 0, &vap
);
1077 if (fp
->f_dirblbuf
== NULL
) {
1078 fp
->f_dirblsz
= vap
->va_a_iroot
->ir_size
;
1079 MALLOC(fp
->f_dirblbuf
, caddr_t
,
1080 max(vap
->va_datalen
,fp
->f_dirblsz
), M_NTFSDIR
, M_WAITOK
);
1083 blsize
= fp
->f_dirblsz
;
1084 rdbuf
= fp
->f_dirblbuf
;
1086 dprintf(("ntfs_ntreaddir: rdbuf: 0x%p, blsize: %d\n", rdbuf
, blsize
));
1088 if (vap
->va_a_iroot
->ir_flag
& NTFS_IRFLAG_INDXALLOC
) {
1089 error
= ntfs_ntvattrget(ntmp
, ip
, NTFS_A_INDXBITMAP
, "$I30",
1095 MALLOC(bmp
, u_char
*, bmvap
->va_datalen
, M_TEMP
, M_WAITOK
);
1096 error
= ntfs_readattr(ntmp
, ip
, NTFS_A_INDXBITMAP
, "$I30", 0,
1097 bmvap
->va_datalen
, bmp
, NULL
);
1101 error
= ntfs_ntvattrget(ntmp
, ip
, NTFS_A_INDX
, "$I30",
1107 cpbl
= ntfs_btocn(blsize
+ ntfs_cntob(1) - 1);
1108 dprintf(("ntfs_ntreaddir: indexalloc: %d, cpbl: %d\n",
1109 iavap
->va_datalen
, cpbl
));
1111 dprintf(("ntfs_ntreadidir: w/o BitMap and IndexAllocation\n"));
1112 iavap
= bmvap
= NULL
;
1116 /* Try use previous values */
1117 if ((fp
->f_lastdnum
< num
) && (fp
->f_lastdnum
!= 0)) {
1118 attrnum
= fp
->f_lastdattr
;
1119 aoff
= fp
->f_lastdoff
;
1120 blnum
= fp
->f_lastdblnum
;
1121 cnum
= fp
->f_lastdnum
;
1123 attrnum
= NTFS_A_INDXROOT
;
1124 aoff
= sizeof(struct attr_indexroot
);
1130 dprintf(("ntfs_ntreaddir: scan: 0x%x, %d, %d, %d, %d\n",
1131 attrnum
, (u_int32_t
) blnum
, cnum
, num
, aoff
));
1132 rdsize
= (attrnum
== NTFS_A_INDXROOT
) ? vap
->va_datalen
: blsize
;
1133 error
= ntfs_readattr(ntmp
, ip
, attrnum
, "$I30",
1134 ntfs_cntob(blnum
* cpbl
), rdsize
, rdbuf
, NULL
);
1138 if (attrnum
== NTFS_A_INDX
) {
1139 error
= ntfs_procfixups(ntmp
, NTFS_INDXMAGIC
,
1145 aoff
= (attrnum
== NTFS_A_INDX
) ?
1146 (0x18 + ((struct attr_indexalloc
*) rdbuf
)->ia_hdrsize
) :
1147 sizeof(struct attr_indexroot
);
1149 iep
= (struct attr_indexentry
*) (rdbuf
+ aoff
);
1150 for (; !(iep
->ie_flag
& NTFS_IEFLAG_LAST
) && (rdsize
> aoff
);
1151 aoff
+= iep
->reclen
,
1152 iep
= (struct attr_indexentry
*) (rdbuf
+ aoff
))
1154 if (!ntfs_isnamepermitted(ntmp
, iep
)) continue;
1157 fp
->f_lastdnum
= cnum
;
1158 fp
->f_lastdoff
= aoff
;
1159 fp
->f_lastdblnum
= blnum
;
1160 fp
->f_lastdattr
= attrnum
;
1171 if (attrnum
== NTFS_A_INDXROOT
)
1176 while (ntfs_cntob(blnum
* cpbl
) < iavap
->va_datalen
) {
1177 if (bmp
[blnum
>> 3] & (1 << (blnum
& 3)))
1182 attrnum
= NTFS_A_INDX
;
1184 if (ntfs_cntob(blnum
* cpbl
) >= iavap
->va_datalen
)
1186 dprintf(("ntfs_ntreaddir: blnum: %d\n", (u_int32_t
) blnum
));
1195 ntfs_ntvattrrele(vap
);
1197 ntfs_ntvattrrele(bmvap
);
1199 ntfs_ntvattrrele(iavap
);
1207 * Convert NTFS times that are in 100 ns units and begins from
1208 * 1601 Jan 1 into unix times.
1211 ntfs_nttimetounix(u_int64_t nt
)
1215 /* WindowNT times are in 100 ns and from 1601 Jan 1 */
1216 t
.tv_nsec
= (nt
% (1000 * 1000 * 10)) * 100;
1217 t
.tv_sec
= nt
/ (1000 * 1000 * 10) -
1218 369LL * 365LL * 24LL * 60LL * 60LL -
1219 89LL * 1LL * 24LL * 60LL * 60LL;
1224 * Get file times from NTFS_A_NAME attribute.
1227 ntfs_times(struct ntfsmount
*ntmp
, struct ntnode
*ip
, ntfs_times_t
*tm
)
1229 struct ntvattr
*vap
;
1232 dprintf(("ntfs_times: ino: %"PRId64
"...\n", ip
->i_number
));
1234 error
= ntfs_ntget(ip
);
1238 error
= ntfs_ntvattrget(ntmp
, ip
, NTFS_A_NAME
, NULL
, 0, &vap
);
1243 *tm
= vap
->va_a_name
->n_times
;
1244 ntfs_ntvattrrele(vap
);
1251 * Get file sizes from corresponding attribute.
1253 * ntnode under fnode should be locked.
1256 ntfs_filesize(struct ntfsmount
*ntmp
, struct fnode
*fp
, u_int64_t
*size
,
1259 struct ntvattr
*vap
;
1260 struct ntnode
*ip
= FTONT(fp
);
1264 dprintf(("ntfs_filesize: ino: %"PRId64
"\n", ip
->i_number
));
1266 error
= ntfs_ntvattrget(ntmp
, ip
,
1267 fp
->f_attrtype
, fp
->f_attrname
, 0, &vap
);
1271 bn
= vap
->va_allocated
;
1272 sz
= vap
->va_datalen
;
1274 dprintf(("ntfs_filesize: %d bytes (%d bytes allocated)\n",
1275 (u_int32_t
) sz
, (u_int32_t
) bn
));
1282 ntfs_ntvattrrele(vap
);
1288 * This is one of write routine.
1291 ntfs_writeattr_plain(struct ntfsmount
*ntmp
, struct ntnode
*ip
,
1292 u_int32_t attrnum
, char *attrname
, off_t roff
,
1293 size_t rsize
, void *rdata
, size_t *initp
,
1298 off_t off
= roff
, left
= rsize
, towrite
;
1299 caddr_t data
= rdata
;
1300 struct ntvattr
*vap
;
1304 error
= ntfs_ntvattrget(ntmp
, ip
, attrnum
, attrname
,
1305 ntfs_btocn(off
), &vap
);
1308 towrite
= min(left
, ntfs_cntob(vap
->va_vcnend
+ 1) - off
);
1309 ddprintf(("ntfs_writeattr_plain: o: %d, s: %d (%d - %d)\n",
1310 (u_int32_t
) off
, (u_int32_t
) towrite
,
1311 (u_int32_t
) vap
->va_vcnstart
,
1312 (u_int32_t
) vap
->va_vcnend
));
1313 error
= ntfs_writentvattr_plain(ntmp
, ip
, vap
,
1314 off
- ntfs_cntob(vap
->va_vcnstart
),
1315 towrite
, data
, &init
, uio
);
1317 printf("ntfs_writeattr_plain: " \
1318 "ntfs_writentvattr_plain failed: o: %d, s: %d\n",
1319 (u_int32_t
) off
, (u_int32_t
) towrite
);
1320 printf("ntfs_writeattr_plain: attrib: %d - %d\n",
1321 (u_int32_t
) vap
->va_vcnstart
,
1322 (u_int32_t
) vap
->va_vcnend
);
1323 ntfs_ntvattrrele(vap
);
1326 ntfs_ntvattrrele(vap
);
1329 data
= data
+ towrite
;
1337 * This is one of write routine.
1339 * ntnode should be locked.
1342 ntfs_writentvattr_plain(struct ntfsmount
*ntmp
, struct ntnode
*ip
,
1343 struct ntvattr
*vap
, off_t roff
, size_t rsize
,
1344 void *rdata
, size_t *initp
, struct uio
*uio
)
1349 cn_t ccn
, ccl
, cn
, left
, cl
;
1350 caddr_t data
= rdata
;
1356 if ((vap
->va_flag
& NTFS_AF_INRUN
) == 0) {
1357 printf("ntfs_writevattr_plain: CAN'T WRITE RES. ATTRIBUTE\n");
1361 ddprintf(("ntfs_writentvattr_plain: data in run: %ld chains\n",
1369 for (; left
&& (cnt
< vap
->va_vruncnt
); cnt
++) {
1370 ccn
= vap
->va_vruncn
[cnt
];
1371 ccl
= vap
->va_vruncl
[cnt
];
1373 ddprintf(("ntfs_writentvattr_plain: " \
1374 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1375 (u_int32_t
) left
, (u_int32_t
) ccn
, \
1376 (u_int32_t
) ccl
, (u_int32_t
) off
));
1378 if (ntfs_cntob(ccl
) < off
) {
1379 off
-= ntfs_cntob(ccl
);
1383 if (!ccn
&& ip
->i_number
!= NTFS_BOOTINO
)
1386 ccl
-= ntfs_btocn(off
);
1387 cn
= ccn
+ ntfs_btocn(off
);
1388 off
= ntfs_btocnoff(off
);
1390 while (left
&& ccl
) {
1391 #if defined(__DragonFly__)
1393 min(ntfs_cntob(ccl
) - off
, MAXBSIZE
- off
));
1395 /* under NetBSD, bread() can read
1396 * maximum one block worth of data */
1397 tocopy
= min(left
, ntmp
->ntm_bps
- off
);
1399 cl
= ntfs_btocl(tocopy
+ off
);
1400 ddprintf(("ntfs_writentvattr_plain: write: " \
1401 "cn: 0x%x cl: %d, off: %d len: %d, left: %d\n",
1402 (u_int32_t
) cn
, (u_int32_t
) cl
,
1403 (u_int32_t
) off
, (u_int32_t
) tocopy
,
1405 if ((off
== 0) && (tocopy
== ntfs_cntob(cl
)))
1407 bp
= getblk(ntmp
->ntm_devvp
, ntfs_cntobn(cn
),
1408 ntfs_cntob(cl
), 0, 0);
1411 error
= bread(ntmp
->ntm_devvp
, ntfs_cntobn(cn
),
1412 ntfs_cntob(cl
), &bp
);
1419 uiomove(bp
->b_data
+ off
, tocopy
, uio
);
1421 memcpy(bp
->b_data
+ off
, data
, tocopy
);
1423 data
= data
+ tocopy
;
1433 printf("ntfs_writentvattr_plain: POSSIBLE RUN ERROR\n");
1441 * This is one of read routines.
1443 * ntnode should be locked.
1446 ntfs_readntvattr_plain(struct ntfsmount
*ntmp
, struct ntnode
*ip
,
1447 struct ntvattr
*vap
, off_t roff
, size_t rsize
,
1448 void *rdata
, size_t *initp
, struct uio
*uio
)
1454 if (vap
->va_flag
& NTFS_AF_INRUN
) {
1456 cn_t ccn
, ccl
, cn
, left
, cl
;
1457 caddr_t data
= rdata
;
1461 ddprintf(("ntfs_readntvattr_plain: data in run: %ld chains\n",
1469 while (left
&& (cnt
< vap
->va_vruncnt
)) {
1470 ccn
= vap
->va_vruncn
[cnt
];
1471 ccl
= vap
->va_vruncl
[cnt
];
1473 ddprintf(("ntfs_readntvattr_plain: " \
1474 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1475 (u_int32_t
) left
, (u_int32_t
) ccn
, \
1476 (u_int32_t
) ccl
, (u_int32_t
) off
));
1478 if (ntfs_cntob(ccl
) < off
) {
1479 off
-= ntfs_cntob(ccl
);
1483 if (ccn
|| ip
->i_number
== NTFS_BOOTINO
) {
1484 ccl
-= ntfs_btocn(off
);
1485 cn
= ccn
+ ntfs_btocn(off
);
1486 off
= ntfs_btocnoff(off
);
1488 while (left
&& ccl
) {
1489 #if defined(__DragonFly__)
1491 min(ntfs_cntob(ccl
) - off
,
1494 /* under NetBSD, bread() can read
1495 * maximum one block worth of data */
1497 ntmp
->ntm_bps
- off
);
1499 cl
= ntfs_btocl(tocopy
+ off
);
1500 ddprintf(("ntfs_readntvattr_plain: " \
1501 "read: cn: 0x%x cl: %d, " \
1502 "off: %d len: %d, left: %d\n",
1508 error
= bread(ntmp
->ntm_devvp
,
1517 uiomove(bp
->b_data
+ off
,
1520 memcpy(data
, bp
->b_data
+ off
,
1524 data
= data
+ tocopy
;
1532 tocopy
= min(left
, ntfs_cntob(ccl
) - off
);
1533 ddprintf(("ntfs_readntvattr_plain: "
1534 "hole: ccn: 0x%x ccl: %d, off: %d, " \
1535 " len: %d, left: %d\n",
1536 (u_int32_t
) ccn
, (u_int32_t
) ccl
,
1537 (u_int32_t
) off
, (u_int32_t
) tocopy
,
1542 size_t remains
= tocopy
;
1543 for(; remains
; remains
++)
1544 uiomove("", 1, uio
);
1546 bzero(data
, tocopy
);
1547 data
= data
+ tocopy
;
1552 printf("ntfs_readntvattr_plain: POSSIBLE RUN ERROR\n");
1556 ddprintf(("ntfs_readnvattr_plain: data is in mft record\n"));
1558 uiomove(vap
->va_datap
+ roff
, rsize
, uio
);
1560 memcpy(rdata
, vap
->va_datap
+ roff
, rsize
);
1568 * This is one of read routines.
1571 ntfs_readattr_plain(struct ntfsmount
*ntmp
, struct ntnode
*ip
,
1572 u_int32_t attrnum
, char *attrname
, off_t roff
,
1573 size_t rsize
, void *rdata
, size_t * initp
,
1578 off_t off
= roff
, left
= rsize
, toread
;
1579 caddr_t data
= rdata
;
1580 struct ntvattr
*vap
;
1584 error
= ntfs_ntvattrget(ntmp
, ip
, attrnum
, attrname
,
1585 ntfs_btocn(off
), &vap
);
1588 toread
= min(left
, ntfs_cntob(vap
->va_vcnend
+ 1) - off
);
1589 ddprintf(("ntfs_readattr_plain: o: %d, s: %d (%d - %d)\n",
1590 (u_int32_t
) off
, (u_int32_t
) toread
,
1591 (u_int32_t
) vap
->va_vcnstart
,
1592 (u_int32_t
) vap
->va_vcnend
));
1593 error
= ntfs_readntvattr_plain(ntmp
, ip
, vap
,
1594 off
- ntfs_cntob(vap
->va_vcnstart
),
1595 toread
, data
, &init
, uio
);
1597 printf("ntfs_readattr_plain: " \
1598 "ntfs_readntvattr_plain failed: o: %d, s: %d\n",
1599 (u_int32_t
) off
, (u_int32_t
) toread
);
1600 printf("ntfs_readattr_plain: attrib: %d - %d\n",
1601 (u_int32_t
) vap
->va_vcnstart
,
1602 (u_int32_t
) vap
->va_vcnend
);
1603 ntfs_ntvattrrele(vap
);
1606 ntfs_ntvattrrele(vap
);
1609 data
= data
+ toread
;
1617 * This is one of read routines.
1620 ntfs_readattr(struct ntfsmount
*ntmp
, struct ntnode
*ip
, u_int32_t attrnum
,
1621 char *attrname
, off_t roff
, size_t rsize
, void *rdata
,
1625 struct ntvattr
*vap
;
1628 ddprintf(("ntfs_readattr: reading %"PRId64
": 0x%x, from %d size %d bytes\n",
1629 ip
->i_number
, attrnum
, (u_int32_t
) roff
, (u_int32_t
) rsize
));
1631 error
= ntfs_ntvattrget(ntmp
, ip
, attrnum
, attrname
, 0, &vap
);
1635 if ((roff
> vap
->va_datalen
) ||
1636 (roff
+ rsize
> vap
->va_datalen
)) {
1637 ddprintf(("ntfs_readattr: offset too big\n"));
1638 ntfs_ntvattrrele(vap
);
1641 if (vap
->va_compression
&& vap
->va_compressalg
) {
1644 off_t off
= roff
, left
= rsize
, tocopy
;
1645 caddr_t data
= rdata
;
1648 ddprintf(("ntfs_ntreadattr: compression: %d\n",
1649 vap
->va_compressalg
));
1651 MALLOC(cup
, u_int8_t
*, ntfs_cntob(NTFS_COMPUNIT_CL
),
1652 M_NTFSDECOMP
, M_WAITOK
);
1653 MALLOC(uup
, u_int8_t
*, ntfs_cntob(NTFS_COMPUNIT_CL
),
1654 M_NTFSDECOMP
, M_WAITOK
);
1656 cn
= (ntfs_btocn(roff
)) & (~(NTFS_COMPUNIT_CL
- 1));
1657 off
= roff
- ntfs_cntob(cn
);
1660 error
= ntfs_readattr_plain(ntmp
, ip
, attrnum
,
1661 attrname
, ntfs_cntob(cn
),
1662 ntfs_cntob(NTFS_COMPUNIT_CL
),
1667 tocopy
= min(left
, ntfs_cntob(NTFS_COMPUNIT_CL
) - off
);
1669 if (init
== ntfs_cntob(NTFS_COMPUNIT_CL
)) {
1671 uiomove(cup
+ off
, tocopy
, uio
);
1673 memcpy(data
, cup
+ off
, tocopy
);
1674 } else if (init
== 0) {
1676 size_t remains
= tocopy
;
1677 for(; remains
; remains
--)
1678 uiomove("", 1, uio
);
1681 bzero(data
, tocopy
);
1683 error
= ntfs_uncompunit(ntmp
, uup
, cup
);
1687 uiomove(uup
+ off
, tocopy
, uio
);
1689 memcpy(data
, uup
+ off
, tocopy
);
1693 data
= data
+ tocopy
;
1694 off
+= tocopy
- ntfs_cntob(NTFS_COMPUNIT_CL
);
1695 cn
+= NTFS_COMPUNIT_CL
;
1698 FREE(uup
, M_NTFSDECOMP
);
1699 FREE(cup
, M_NTFSDECOMP
);
1701 error
= ntfs_readattr_plain(ntmp
, ip
, attrnum
, attrname
,
1702 roff
, rsize
, rdata
, &init
, uio
);
1703 ntfs_ntvattrrele(vap
);
1709 ntfs_parserun(cn_t
*cn
, cn_t
*cl
, u_int8_t
*run
, u_long len
, u_long
*off
)
1715 printf("ntfs_parsetun: run == NULL\n");
1720 printf("ntfs_parserun: trying to go out of run\n");
1724 if ((sz
& 0xF) > 8 || (*off
) + (sz
& 0xF) > len
) {
1725 printf("ntfs_parserun: " \
1726 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1730 for (i
= 0; i
< (sz
& 0xF); i
++)
1731 *cl
+= (u_int32_t
) run
[(*off
)++] << (i
<< 3);
1734 if ((sz
& 0xF) > 8 || (*off
) + (sz
& 0xF) > len
) {
1735 printf("ntfs_parserun: " \
1736 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1740 for (i
= 0; i
< (sz
& 0xF); i
++)
1741 *cn
+= (u_int32_t
) run
[(*off
)++] << (i
<< 3);
1748 * Process fixup routine on given buffer.
1751 ntfs_procfixups(struct ntfsmount
*ntmp
, u_int32_t magic
, caddr_t buf
,
1754 struct fixuphdr
*fhp
= (struct fixuphdr
*) buf
;
1760 if (fhp
->fh_magic
!= magic
) {
1761 printf("ntfs_procfixups: magic doesn't match: %08x != %08x\n",
1762 fhp
->fh_magic
, magic
);
1765 if ((fhp
->fh_fnum
- 1) * ntmp
->ntm_bps
!= len
) {
1766 printf("ntfs_procfixups: " \
1767 "bad fixups number: %d for %ld bytes block\n",
1768 fhp
->fh_fnum
, (long)len
); /* XXX printf kludge */
1771 if (fhp
->fh_foff
>= ntmp
->ntm_spc
* ntmp
->ntm_mftrecsz
* ntmp
->ntm_bps
) {
1772 printf("ntfs_procfixups: invalid offset: %x", fhp
->fh_foff
);
1775 fxp
= (u_int16_t
*) (buf
+ fhp
->fh_foff
);
1776 cfxp
= (u_int16_t
*) (buf
+ ntmp
->ntm_bps
- 2);
1778 for (i
= 1; i
< fhp
->fh_fnum
; i
++, fxp
++) {
1779 if (*cfxp
!= fixup
) {
1780 printf("ntfs_procfixups: fixup %d doesn't match\n", i
);
1784 cfxp
= (u_int16_t
*)(((caddr_t
) cfxp
) + ntmp
->ntm_bps
);
1791 ntfs_runtocn(cn_t
*cn
, struct ntfsmount
*ntmp
, u_int8_t
*run
, u_long len
,
1801 printf("ntfs_runtocn: run: 0x%p, %ld bytes, vcn:%ld\n",
1802 run
, len
, (u_long
) vcn
);
1803 printf("ntfs_runtocn: run: ");
1804 for (i
= 0; i
< len
; i
++)
1805 printf("0x%02x ", run
[i
]);
1810 printf("ntfs_runtocn: run == NULL\n");
1814 if (run
[off
] == 0) {
1815 printf("ntfs_runtocn: vcn too big\n");
1819 error
= ntfs_parserun(&ccn
, &ccl
, run
, len
, &off
);
1821 printf("ntfs_runtocn: ntfs_parserun failed\n");
1824 } while (ccl
<= vcn
);
1831 * this initializes toupper table & dependant variables to be ready for
1835 ntfs_toupper_init(void)
1837 ntfs_toupper_tab
= (wchar
*) NULL
;
1838 lockinit(&ntfs_toupper_lock
, 0, "ntfs_toupper", 0, 0);
1839 ntfs_toupper_usecount
= 0;
1843 * if the ntfs_toupper_tab[] is filled already, just raise use count;
1844 * otherwise read the data from the filesystem we are currently mounting
1847 ntfs_toupper_use(struct mount
*mp
, struct ntfsmount
*ntmp
)
1852 /* get exclusive access */
1853 LOCKMGR(&ntfs_toupper_lock
, LK_EXCLUSIVE
, NULL
);
1855 /* only read the translation data from a file if it hasn't been
1857 if (ntfs_toupper_tab
)
1861 * Read in Unicode lowercase -> uppercase translation file.
1862 * XXX for now, just the first 256 entries are used anyway,
1863 * so don't bother reading more
1865 MALLOC(ntfs_toupper_tab
, wchar
*, 65536 * sizeof(wchar
),
1866 M_NTFSRDATA
, M_WAITOK
);
1868 if ((error
= VFS_VGET(mp
, NTFS_UPCASEINO
, &vp
)))
1870 error
= ntfs_readattr(ntmp
, VTONT(vp
), NTFS_A_DATA
, NULL
,
1871 0, 65536*sizeof(wchar
), (char *) ntfs_toupper_tab
, NULL
);
1875 ntfs_toupper_usecount
++;
1876 LOCKMGR(&ntfs_toupper_lock
, LK_RELEASE
, NULL
);
1881 * lower the use count and if it reaches zero, free the memory
1882 * tied by toupper table
1885 ntfs_toupper_unuse(void)
1887 /* get exclusive access */
1888 LOCKMGR(&ntfs_toupper_lock
, LK_EXCLUSIVE
, NULL
);
1890 ntfs_toupper_usecount
--;
1891 if (ntfs_toupper_usecount
== 0) {
1892 FREE(ntfs_toupper_tab
, M_NTFSRDATA
);
1893 ntfs_toupper_tab
= NULL
;
1896 else if (ntfs_toupper_usecount
< 0) {
1897 panic("ntfs_toupper_unuse(): use count negative: %d\n",
1898 ntfs_toupper_usecount
);
1902 /* release the lock */
1903 LOCKMGR(&ntfs_toupper_lock
, LK_RELEASE
, NULL
);
1907 ntfs_u28_init(struct ntfsmount
*ntmp
, wchar
*u2w
)
1912 MALLOC(u28
, char **, 256 * sizeof(char*), M_TEMP
, M_WAITOK
| M_ZERO
);
1914 for (i
=0; i
<256; i
++) {
1915 h
= (u2w
[i
] >> 8) & 0xFF;
1918 if (u28
[h
] == NULL
) {
1919 MALLOC(u28
[h
], char *, 256 * sizeof(char), M_TEMP
, M_WAITOK
);
1920 for (j
=0; j
<256; j
++)
1924 u28
[h
][l
] = i
& 0xFF;
1927 ntmp
->ntm_u28
= u28
;
1933 ntfs_u28_uninit(struct ntfsmount
*ntmp
)
1938 if (ntmp
->ntm_u28
== NULL
)
1941 u28
= ntmp
->ntm_u28
;
1943 for (i
=0; i
<256; i
++)
1945 FREE(u28
[i
], M_TEMP
);
1953 ntfs_82u_init(struct ntfsmount
*ntmp
, u_int16_t
*u2w
)
1958 MALLOC(_82u
, wchar
*, 256 * sizeof(wchar
), M_TEMP
, M_WAITOK
);
1961 for (i
=0; i
<256; i
++)
1964 for (i
=0; i
<128; i
++)
1966 for (i
=0; i
<128; i
++)
1967 _82u
[i
+128] = u2w
[i
];
1970 ntmp
->ntm_82u
= _82u
;
1976 ntfs_82u_uninit(struct ntfsmount
*ntmp
)
1978 FREE(ntmp
->ntm_82u
, M_TEMP
);
1983 * maps the Unicode char to 8bit equivalent
1984 * XXX currently only gets lower 8bit from the Unicode char
1985 * and substitutes a '_' for it if the result would be '\0';
1986 * something better has to be definitely though out
1989 ntfs_u28(struct ntfsmount
*ntmp
, wchar wc
)
1993 p
= ntmp
->ntm_u28
[(wc
>>8)&0xFF];
1996 return (p
[wc
&0xFF]);