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.9 2003/12/29 18:04:50 dillon 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>
45 #if defined(__NetBSD__)
46 #include <miscfs/specfs/specdev.h>
49 /* #define NTFS_DEBUG 1 */
51 #include "ntfsmount.h"
52 #include "ntfs_inode.h"
53 #include "ntfs_vfsops.h"
54 #include "ntfs_subr.h"
55 #include "ntfs_compr.h"
56 #include "ntfs_ihash.h"
58 #if defined(__FreeBSD__)
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");
65 static int ntfs_ntlookupattr (struct ntfsmount *, const char *, int, int *, char **);
66 static int ntfs_findvattr (struct ntfsmount *, struct ntnode *, struct ntvattr **, struct ntvattr **, u_int32_t, const char *, size_t, cn_t);
67 static int ntfs_uastricmp (struct ntfsmount *, const wchar *, size_t, const char *, size_t);
68 static int ntfs_uastrcmp (struct ntfsmount *, const wchar *, size_t, const char *, size_t);
70 /* table for mapping Unicode chars into uppercase; it's filled upon first
71 * ntfs mount, freed upon last ntfs umount */
72 static wchar *ntfs_toupper_tab;
73 #define NTFS_TOUPPER(ch) (ntfs_toupper_tab[(ch)])
74 static struct lock ntfs_toupper_lock;
75 static signed int ntfs_toupper_usecount;
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) )
89 dprintf(("ntfs_ntvattrrele: ino: %d, type: 0x%x\n",
90 vap->va_ip->i_number, vap->va_type));
92 ntfs_ntrele(vap->va_ip);
98 * find the attribute in the ntnode
101 ntfs_findvattr(ntmp, ip, lvapp, vapp, type, name, namelen, vcn)
102 struct ntfsmount *ntmp;
104 struct ntvattr **lvapp, **vapp;
113 if((ip->i_flag & IN_LOADED) == 0) {
114 dprintf(("ntfs_findvattr: node not loaded, ino: %d\n",
116 error = ntfs_loadntnode(ntmp,ip);
118 printf("ntfs_findvattr: FAILED TO LOAD INO: %d\n",
126 for (vap = ip->i_valist.lh_first; vap; vap = vap->va_list.le_next) {
127 ddprintf(("ntfs_findvattr: type: 0x%x, vcn: %d - %d\n", \
128 vap->va_type, (u_int32_t) vap->va_vcnstart, \
129 (u_int32_t) vap->va_vcnend));
130 if ((vap->va_type == type) &&
131 (vap->va_vcnstart <= vcn) && (vap->va_vcnend >= vcn) &&
132 (vap->va_namelen == namelen) &&
133 (strncmp(name, vap->va_name, namelen) == 0)) {
135 ntfs_ntref(vap->va_ip);
138 if (vap->va_type == NTFS_A_ATTRLIST)
146 * Search attribute specifed in ntnode (load ntnode if nessecary).
147 * If not found but ATTR_A_ATTRLIST present, read it in and search throught.
148 * VOP_VGET node needed, and lookup througth it's ntnode (load if nessesary).
150 * ntnode should be locked
154 struct ntfsmount * ntmp,
159 struct ntvattr ** vapp)
161 struct ntvattr *lvap = NULL;
162 struct attr_attrlist *aalp;
163 struct attr_attrlist *nextaalp;
165 struct ntnode *newip;
173 dprintf(("ntfs_ntvattrget: " \
174 "ino: %d, type: 0x%x, name: %s, vcn: %d\n", \
175 ip->i_number, type, name, (u_int32_t) vcn));
176 namelen = strlen(name);
178 dprintf(("ntfs_ntvattrget: " \
179 "ino: %d, type: 0x%x, vcn: %d\n", \
180 ip->i_number, type, (u_int32_t) vcn));
185 error = ntfs_findvattr(ntmp, ip, &lvap, vapp, type, name, namelen, vcn);
190 dprintf(("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: " \
191 "ino: %d, type: 0x%x, name: %s, vcn: %d\n", \
192 ip->i_number, type, name, (u_int32_t) vcn));
195 /* Scan $ATTRIBUTE_LIST for requested attribute */
196 len = lvap->va_datalen;
197 MALLOC(alpool, caddr_t, len, M_TEMP, M_WAITOK);
198 error = ntfs_readntvattr_plain(ntmp, ip, lvap, 0, len, alpool, &len,
203 aalp = (struct attr_attrlist *) alpool;
206 for(; len > 0; aalp = nextaalp) {
207 dprintf(("ntfs_ntvattrget: " \
208 "attrlist: ino: %d, attr: 0x%x, vcn: %d\n", \
209 aalp->al_inumber, aalp->al_type, \
210 (u_int32_t) aalp->al_vcnstart));
212 if (len > aalp->reclen) {
213 nextaalp = NTFS_NEXTREC(aalp, struct attr_attrlist *);
219 if (!NTFS_AALPCMP(aalp, type, name, namelen) ||
220 (nextaalp && (nextaalp->al_vcnstart <= vcn) &&
221 NTFS_AALPCMP(nextaalp, type, name, namelen)))
224 dprintf(("ntfs_ntvattrget: attribute in ino: %d\n",
227 /* this is not a main record, so we can't use just plain
229 error = ntfs_vgetex(ntmp->ntm_mountp, aalp->al_inumber,
230 NTFS_A_DATA, NULL, LK_EXCLUSIVE,
231 VG_EXT, curthread, &newvp);
233 printf("ntfs_ntvattrget: CAN'T VGET INO: %d\n",
237 newip = VTONT(newvp);
238 /* XXX have to lock ntnode */
239 error = ntfs_findvattr(ntmp, newip, &lvap, vapp,
240 type, name, namelen, vcn);
244 printf("ntfs_ntvattrget: ATTRLIST ERROR.\n");
249 dprintf(("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: " \
250 "ino: %d, type: 0x%x, name: %.*s, vcn: %d\n", \
251 ip->i_number, type, (int) namelen, name, (u_int32_t) vcn));
253 FREE(alpool, M_TEMP);
258 * Read ntnode from disk, make ntvattr list.
260 * ntnode should be locked
264 struct ntfsmount * ntmp,
267 struct filerec *mfrp;
271 struct ntvattr *nvap;
273 dprintf(("ntfs_loadntnode: loading ino: %d\n",ip->i_number));
275 MALLOC(mfrp, struct filerec *, ntfs_bntob(ntmp->ntm_bpmftrec),
278 if (ip->i_number < NTFS_SYSNODESNUM) {
281 dprintf(("ntfs_loadntnode: read system node\n"));
283 bn = ntfs_cntobn(ntmp->ntm_mftcn) +
284 ntmp->ntm_bpmftrec * ip->i_number;
286 error = bread(ntmp->ntm_devvp,
287 bn, ntfs_bntob(ntmp->ntm_bpmftrec), &bp);
289 printf("ntfs_loadntnode: BREAD FAILED\n");
293 memcpy(mfrp, bp->b_data, ntfs_bntob(ntmp->ntm_bpmftrec));
298 vp = ntmp->ntm_sysvn[NTFS_MFTINO];
299 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
300 ip->i_number * ntfs_bntob(ntmp->ntm_bpmftrec),
301 ntfs_bntob(ntmp->ntm_bpmftrec), mfrp, NULL);
303 printf("ntfs_loadntnode: ntfs_readattr failed\n");
308 /* Check if magic and fixups are correct */
309 error = ntfs_procfixups(ntmp, NTFS_FILEMAGIC, (caddr_t)mfrp,
310 ntfs_bntob(ntmp->ntm_bpmftrec));
312 printf("ntfs_loadntnode: BAD MFT RECORD %d\n",
313 (u_int32_t) ip->i_number);
317 dprintf(("ntfs_loadntnode: load attrs for ino: %d\n",ip->i_number));
318 off = mfrp->fr_attroff;
319 ap = (struct attr *) ((caddr_t)mfrp + off);
321 LIST_INIT(&ip->i_valist);
323 while (ap->a_hdr.a_type != -1) {
324 error = ntfs_attrtontvattr(ntmp, &nvap, ap);
329 LIST_INSERT_HEAD(&ip->i_valist, nvap, va_list);
331 off += ap->a_hdr.reclen;
332 ap = (struct attr *) ((caddr_t)mfrp + off);
335 printf("ntfs_loadntnode: failed to load attr ino: %d\n",
340 ip->i_mainrec = mfrp->fr_mainrec;
341 ip->i_nlink = mfrp->fr_nlink;
342 ip->i_frflag = mfrp->fr_flags;
344 ip->i_flag |= IN_LOADED;
352 * Routine locks ntnode and increase usecount, just opposite of
359 dprintf(("ntfs_ntget: get ntnode %d: %p, usecount: %d\n",
360 ip->i_number, ip, ip->i_usecount));
363 lwkt_gettoken(&ip->i_interlock);
364 LOCKMGR(&ip->i_lock, LK_EXCLUSIVE | LK_INTERLOCK, &ip->i_interlock);
370 * Routine search ntnode in hash, if found: lock, inc usecount and return.
371 * If not in hash allocate structure for ntnode, prefill it, lock,
372 * inc count and return.
374 * ntnode returned locked
378 struct ntfsmount * ntmp,
380 struct ntnode ** ipp)
384 dprintf(("ntfs_ntlookup: looking for ntnode %d\n", ino));
387 if ((ip = ntfs_nthashlookup(ntmp->ntm_dev, ino)) != NULL) {
389 dprintf(("ntfs_ntlookup: ntnode %d: %p, usecount: %d\n",
390 ino, ip, ip->i_usecount));
394 } while (LOCKMGR(&ntfs_hashlock, LK_EXCLUSIVE | LK_SLEEPFAIL, NULL));
396 MALLOC(ip, struct ntnode *, sizeof(struct ntnode),
397 M_NTFSNTNODE, M_WAITOK);
398 ddprintf(("ntfs_ntlookup: allocating ntnode: %d: %p\n", ino, ip));
399 bzero((caddr_t) ip, sizeof(struct ntnode));
401 /* Generic initialization */
402 ip->i_devvp = ntmp->ntm_devvp;
403 ip->i_dev = ntmp->ntm_dev;
407 LIST_INIT(&ip->i_fnlist);
410 /* init lock and lock the newborn ntnode */
411 lockinit(&ip->i_lock, 0, "ntnode", 0, LK_EXCLUSIVE);
412 lwkt_inittoken(&ip->i_interlock);
417 LOCKMGR(&ntfs_hashlock, LK_RELEASE, NULL);
421 dprintf(("ntfs_ntlookup: ntnode %d: %p, usecount: %d\n",
422 ino, ip, ip->i_usecount));
428 * Decrement usecount of ntnode and unlock it, if usecount reach zero,
431 * ntnode should be locked on entry, and unlocked on return.
439 dprintf(("ntfs_ntput: rele ntnode %d: %p, usecount: %d\n",
440 ip->i_number, ip, ip->i_usecount));
442 lwkt_gettoken(&ip->i_interlock);
446 if (ip->i_usecount < 0) {
447 panic("ntfs_ntput: ino: %d usecount: %d \n",
448 ip->i_number,ip->i_usecount);
452 if (ip->i_usecount > 0) {
453 LOCKMGR(&ip->i_lock, LK_RELEASE|LK_INTERLOCK, &ip->i_interlock);
457 dprintf(("ntfs_ntput: deallocating ntnode: %d\n", ip->i_number));
459 if (ip->i_fnlist.lh_first)
460 panic("ntfs_ntput: ntnode has fnodes\n");
464 while ((vap = LIST_FIRST(&ip->i_valist)) != NULL) {
465 LIST_REMOVE(vap,va_list);
466 ntfs_freentvattr(vap);
468 lwkt_reltoken(&ip->i_interlock);
470 FREE(ip, M_NTFSNTNODE);
474 * increment usecount of ntnode
482 dprintf(("ntfs_ntref: ino %d, usecount: %d\n",
483 ip->i_number, ip->i_usecount));
488 * Decrement usecount of ntnode.
494 dprintf(("ntfs_ntrele: rele ntnode %d: %p, usecount: %d\n",
495 ip->i_number, ip, ip->i_usecount));
497 lwkt_gettoken(&ip->i_interlock);
500 if (ip->i_usecount < 0) {
501 panic("ntfs_ntrele: ino: %d usecount: %d \n",
502 ip->i_number,ip->i_usecount);
504 lwkt_reltoken(&ip->i_interlock);
508 * Deallocate all memory allocated for ntvattr
511 ntfs_freentvattr(vap)
512 struct ntvattr * vap;
514 if (vap->va_flag & NTFS_AF_INRUN) {
516 FREE(vap->va_vruncn, M_NTFSRUN);
518 FREE(vap->va_vruncl, M_NTFSRUN);
521 FREE(vap->va_datap, M_NTFSRDATA);
523 FREE(vap, M_NTFSNTVATTR);
527 * Convert disk image of attribute into ntvattr structure,
528 * runs are expanded also.
532 struct ntfsmount * ntmp,
533 struct ntvattr ** rvapp,
542 MALLOC(vap, struct ntvattr *, sizeof(struct ntvattr),
543 M_NTFSNTVATTR, M_WAITOK);
544 bzero(vap, sizeof(struct ntvattr));
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;
551 ddprintf(("type: 0x%x, index: %d", vap->va_type, vap->va_index));
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]));
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),
572 (caddr_t) rap + rap->a_nr.a_dataoff);
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 MALLOC(vap->va_datap, caddr_t, vap->va_datalen,
581 M_NTFSRDATA, M_WAITOK);
582 memcpy(vap->va_datap, (caddr_t) rap + rap->a_r.a_dataoff,
585 ddprintf((", len: %d", vap->va_datalen));
588 FREE(vap, M_NTFSNTVATTR);
598 * Expand run into more utilizable and more memory eating format.
619 off += (run[off] & 0xF) + ((run[off] >> 4) & 0xF) + 1;
622 MALLOC(cn, cn_t *, cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
623 MALLOC(cl, cn_t *, cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
633 for (i = 0; i < (sz & 0xF); i++)
634 cl[cnt] += (u_int32_t) run[off++] << (i << 3);
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);
643 for (i = 0; i < sz; i++)
644 tmp |= (u_int64_t) run[off++] << (i << 3);
647 prev = cn[cnt] = prev + tmp;
660 * Compare unicode and ascii string case insens.
663 ntfs_uastricmp(ntmp, ustr, ustrlen, astr, astrlen)
664 struct ntfsmount *ntmp;
674 * XXX We use NTFS_82U(NTFS_U28(c)) to get rid of unicode
675 * symbols not covered by translation table
677 for (i = 0; i < ustrlen && i < astrlen; i++) {
678 res = ((int) NTFS_TOUPPER(NTFS_82U(NTFS_U28(ustr[i])))) -
679 ((int)NTFS_TOUPPER(NTFS_82U(astr[i])));
683 return (ustrlen - astrlen);
687 * Compare unicode and ascii string case sens.
690 ntfs_uastrcmp(ntmp, ustr, ustrlen, astr, astrlen)
691 struct ntfsmount *ntmp;
700 for (i = 0; (i < ustrlen) && (i < astrlen); i++) {
701 res = (int) (((char)NTFS_U28(ustr[i])) - astr[i]);
705 return (ustrlen - astrlen);
709 * Search fnode in ntnode, if not found allocate and preinitialize.
711 * ntnode should be locked on entry.
715 struct ntfsmount *ntmp,
723 dprintf(("ntfs_fget: ino: %d, attrtype: 0x%x, attrname: %s\n",
724 ip->i_number,attrtype, attrname?attrname:""));
726 for (fp = ip->i_fnlist.lh_first; fp != NULL; fp = fp->f_fnlist.le_next){
727 dprintf(("ntfs_fget: fnode: attrtype: %d, attrname: %s\n",
728 fp->f_attrtype, fp->f_attrname?fp->f_attrname:""));
730 if ((attrtype == fp->f_attrtype) &&
731 ((!attrname && !fp->f_attrname) ||
732 (attrname && fp->f_attrname &&
733 !strcmp(attrname,fp->f_attrname)))){
734 dprintf(("ntfs_fget: found existed: %p\n",fp));
742 MALLOC(fp, struct fnode *, sizeof(struct fnode), M_NTFSFNODE, M_WAITOK);
743 bzero(fp, sizeof(struct fnode));
744 dprintf(("ntfs_fget: allocating fnode: %p\n",fp));
748 fp->f_flag |= FN_AATTRNAME;
749 MALLOC(fp->f_attrname, char *, strlen(attrname)+1, M_TEMP, M_WAITOK);
750 strcpy(fp->f_attrname, attrname);
752 fp->f_attrname = NULL;
753 fp->f_attrtype = attrtype;
757 LIST_INSERT_HEAD(&ip->i_fnlist, fp, f_fnlist);
765 * Deallocate fnode, remove it from ntnode's fnode list.
767 * ntnode should be locked.
773 struct ntnode *ip = FTONT(fp);
775 dprintf(("ntfs_frele: fnode: %p for %d: %p\n", fp, ip->i_number, ip));
777 dprintf(("ntfs_frele: deallocating fnode\n"));
778 LIST_REMOVE(fp,f_fnlist);
779 if (fp->f_flag & FN_AATTRNAME)
780 FREE(fp->f_attrname, M_TEMP);
782 FREE(fp->f_dirblbuf, M_NTFSDIR);
783 FREE(fp, M_NTFSFNODE);
788 * Lookup attribute name in format: [[:$ATTR_TYPE]:$ATTR_NAME],
789 * $ATTR_TYPE is searched in attrdefs read from $AttrDefs.
790 * If $ATTR_TYPE nott specifed, ATTR_A_DATA assumed.
794 struct ntfsmount * ntmp,
802 struct ntvattrdef *adp;
807 if (name[0] == '$') {
809 for (syslen = 0; syslen < namelen; syslen++) {
810 if(sys[syslen] == ':') {
820 for (i = 0; i < ntmp->ntm_adnum; i++, adp++){
821 if (syslen != adp->ad_namelen ||
822 strncmp(sys, adp->ad_name, syslen) != 0)
825 *attrtype = adp->ad_type;
830 *attrtype = NTFS_A_DATA;
834 MALLOC((*attrname), char *, namelen, M_TEMP, M_WAITOK);
835 memcpy((*attrname), name, namelen);
836 (*attrname)[namelen] = '\0';
843 * Lookup specifed node for filename, matching cnp,
844 * return fnode filled.
848 struct ntfsmount * ntmp,
850 struct componentname * cnp,
853 struct fnode *fp = VTOF(vp);
854 struct ntnode *ip = FTONT(fp);
855 struct ntvattr *vap; /* Root attribute */
856 cn_t cn; /* VCN in current attribute */
857 caddr_t rdbuf; /* Buffer to read directory's blocks */
859 u_int32_t rdsize; /* Length of data to read from current block */
860 struct attr_indexentry *iep;
861 int error, res, anamelen, fnamelen;
862 const char *fname,*aname;
864 int attrtype = NTFS_A_DATA;
865 char *attrname = NULL;
870 error = ntfs_ntget(ip);
874 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
875 if (error || (vap->va_flag & NTFS_AF_INRUN))
878 blsize = vap->va_a_iroot->ir_size;
879 rdsize = vap->va_datalen;
882 * Divide file name into: foofilefoofilefoofile[:attrspec]
883 * Store like this: fname:fnamelen [aname:anamelen]
885 fname = cnp->cn_nameptr;
888 for (fnamelen = 0; fnamelen < cnp->cn_namelen; fnamelen++)
889 if(fname[fnamelen] == ':') {
890 aname = fname + fnamelen + 1;
891 anamelen = cnp->cn_namelen - fnamelen - 1;
892 dprintf(("ntfs_ntlookupfile: %s (%d), attr: %s (%d)\n",
893 fname, fnamelen, aname, anamelen));
897 dprintf(("ntfs_ntlookupfile: blksz: %d, rdsz: %d\n", blsize, rdsize));
899 MALLOC(rdbuf, caddr_t, blsize, M_TEMP, M_WAITOK);
901 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXROOT, "$I30",
902 0, rdsize, rdbuf, NULL);
906 aoff = sizeof(struct attr_indexroot);
909 iep = (struct attr_indexentry *) (rdbuf + aoff);
911 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
913 iep = (struct attr_indexentry *) (rdbuf + aoff))
915 ddprintf(("scan: %d, %d\n",
916 (u_int32_t) iep->ie_number,
917 (u_int32_t) iep->ie_fnametype));
919 /* check the name - the case-insensitible check
920 * has to come first, to break from this for loop
921 * if needed, so we can dive correctly */
922 res = NTFS_UASTRICMP(iep->ie_fname, iep->ie_fnamelen,
925 if (res < 0) continue;
927 if (iep->ie_fnametype == 0 ||
928 !(ntmp->ntm_flag & NTFS_MFLAG_CASEINS))
930 res = NTFS_UASTRCMP(iep->ie_fname,
931 iep->ie_fnamelen, fname, fnamelen);
932 if (res != 0) continue;
936 error = ntfs_ntlookupattr(ntmp,
938 &attrtype, &attrname);
943 /* Check if we've found ourself */
944 if ((iep->ie_number == ip->i_number) &&
945 (attrtype == fp->f_attrtype) &&
946 ((!attrname && !fp->f_attrname) ||
947 (attrname && fp->f_attrname &&
948 !strcmp(attrname, fp->f_attrname))))
956 /* vget node, but don't load it */
957 error = ntfs_vgetex(ntmp->ntm_mountp,
958 iep->ie_number, attrtype, attrname,
959 LK_EXCLUSIVE, VG_DONTLOADIN | VG_DONTVALIDFN,
962 /* free the buffer returned by ntfs_ntlookupattr() */
964 FREE(attrname, M_TEMP);
973 if (nfp->f_flag & FN_VALID) {
978 nfp->f_fflag = iep->ie_fflag;
979 nfp->f_pnumber = iep->ie_fpnumber;
980 nfp->f_times = iep->ie_ftimes;
982 if((nfp->f_fflag & NTFS_FFLAG_DIR) &&
983 (nfp->f_attrtype == NTFS_A_DATA) &&
984 (nfp->f_attrname == NULL))
989 nvp->v_type = f_type;
991 if ((nfp->f_attrtype == NTFS_A_DATA) &&
992 (nfp->f_attrname == NULL))
994 /* Opening default attribute */
995 nfp->f_size = iep->ie_fsize;
996 nfp->f_allocated = iep->ie_fallocated;
997 nfp->f_flag |= FN_PRELOADED;
999 error = ntfs_filesize(ntmp, nfp,
1000 &nfp->f_size, &nfp->f_allocated);
1007 nfp->f_flag &= ~FN_VALID;
1012 /* Dive if possible */
1013 if (iep->ie_flag & NTFS_IEFLAG_SUBNODE) {
1014 dprintf(("ntfs_ntlookupfile: diving\n"));
1016 cn = *(cn_t *) (rdbuf + aoff +
1017 iep->reclen - sizeof(cn_t));
1020 error = ntfs_readattr(ntmp, ip, NTFS_A_INDX, "$I30",
1021 ntfs_cntob(cn), rdsize, rdbuf, NULL);
1025 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1030 aoff = (((struct attr_indexalloc *) rdbuf)->ia_hdrsize +
1033 dprintf(("ntfs_ntlookupfile: nowhere to dive :-(\n"));
1039 dprintf(("finish\n"));
1042 if (attrname) FREE(attrname, M_TEMP);
1043 ntfs_ntvattrrele(vap);
1045 FREE(rdbuf, M_TEMP);
1050 * Check if name type is permitted to show.
1053 ntfs_isnamepermitted(
1054 struct ntfsmount * ntmp,
1055 struct attr_indexentry * iep)
1057 if (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)
1060 switch (iep->ie_fnametype) {
1062 ddprintf(("ntfs_isnamepermitted: skiped DOS name\n"));
1064 case 0: case 1: case 3:
1067 printf("ntfs_isnamepermitted: " \
1068 "WARNING! Unknown file name type: %d\n",
1076 * Read ntfs dir like stream of attr_indexentry, not like btree of them.
1077 * This is done by scaning $BITMAP:$I30 for busy clusters and reading them.
1078 * Ofcouse $INDEX_ROOT:$I30 is read before. Last read values are stored in
1079 * fnode, so we can skip toward record number num almost immediatly.
1080 * Anyway this is rather slow routine. The problem is that we don't know
1081 * how many records are there in $INDEX_ALLOCATION:$I30 block.
1085 struct ntfsmount * ntmp,
1088 struct attr_indexentry ** riepp)
1090 struct ntnode *ip = FTONT(fp);
1091 struct ntvattr *vap = NULL; /* IndexRoot attribute */
1092 struct ntvattr *bmvap = NULL; /* BitMap attribute */
1093 struct ntvattr *iavap = NULL; /* IndexAllocation attribute */
1094 caddr_t rdbuf; /* Buffer to read directory's blocks */
1095 u_char *bmp = NULL; /* Bitmap */
1096 u_int32_t blsize; /* Index allocation size (2048) */
1097 u_int32_t rdsize; /* Length of data to read */
1098 u_int32_t attrnum; /* Current attribute type */
1099 u_int32_t cpbl = 1; /* Clusters per directory block */
1101 struct attr_indexentry *iep;
1103 u_int32_t aoff, cnum;
1105 dprintf(("ntfs_ntreaddir: read ino: %d, num: %d\n", ip->i_number, num));
1106 error = ntfs_ntget(ip);
1110 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
1114 if (fp->f_dirblbuf == NULL) {
1115 fp->f_dirblsz = vap->va_a_iroot->ir_size;
1116 MALLOC(fp->f_dirblbuf, caddr_t,
1117 max(vap->va_datalen,fp->f_dirblsz), M_NTFSDIR, M_WAITOK);
1120 blsize = fp->f_dirblsz;
1121 rdbuf = fp->f_dirblbuf;
1123 dprintf(("ntfs_ntreaddir: rdbuf: 0x%p, blsize: %d\n", rdbuf, blsize));
1125 if (vap->va_a_iroot->ir_flag & NTFS_IRFLAG_INDXALLOC) {
1126 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXBITMAP, "$I30",
1132 MALLOC(bmp, u_char *, bmvap->va_datalen, M_TEMP, M_WAITOK);
1133 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXBITMAP, "$I30", 0,
1134 bmvap->va_datalen, bmp, NULL);
1138 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDX, "$I30",
1144 cpbl = ntfs_btocn(blsize + ntfs_cntob(1) - 1);
1145 dprintf(("ntfs_ntreaddir: indexalloc: %d, cpbl: %d\n",
1146 iavap->va_datalen, cpbl));
1148 dprintf(("ntfs_ntreadidir: w/o BitMap and IndexAllocation\n"));
1149 iavap = bmvap = NULL;
1153 /* Try use previous values */
1154 if ((fp->f_lastdnum < num) && (fp->f_lastdnum != 0)) {
1155 attrnum = fp->f_lastdattr;
1156 aoff = fp->f_lastdoff;
1157 blnum = fp->f_lastdblnum;
1158 cnum = fp->f_lastdnum;
1160 attrnum = NTFS_A_INDXROOT;
1161 aoff = sizeof(struct attr_indexroot);
1167 dprintf(("ntfs_ntreaddir: scan: 0x%x, %d, %d, %d, %d\n",
1168 attrnum, (u_int32_t) blnum, cnum, num, aoff));
1169 rdsize = (attrnum == NTFS_A_INDXROOT) ? vap->va_datalen : blsize;
1170 error = ntfs_readattr(ntmp, ip, attrnum, "$I30",
1171 ntfs_cntob(blnum * cpbl), rdsize, rdbuf, NULL);
1175 if (attrnum == NTFS_A_INDX) {
1176 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1182 aoff = (attrnum == NTFS_A_INDX) ?
1183 (0x18 + ((struct attr_indexalloc *) rdbuf)->ia_hdrsize) :
1184 sizeof(struct attr_indexroot);
1186 iep = (struct attr_indexentry *) (rdbuf + aoff);
1187 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
1188 aoff += iep->reclen,
1189 iep = (struct attr_indexentry *) (rdbuf + aoff))
1191 if (!ntfs_isnamepermitted(ntmp, iep)) continue;
1194 fp->f_lastdnum = cnum;
1195 fp->f_lastdoff = aoff;
1196 fp->f_lastdblnum = blnum;
1197 fp->f_lastdattr = attrnum;
1208 if (attrnum == NTFS_A_INDXROOT)
1213 while (ntfs_cntob(blnum * cpbl) < iavap->va_datalen) {
1214 if (bmp[blnum >> 3] & (1 << (blnum & 3)))
1219 attrnum = NTFS_A_INDX;
1221 if (ntfs_cntob(blnum * cpbl) >= iavap->va_datalen)
1223 dprintf(("ntfs_ntreaddir: blnum: %d\n", (u_int32_t) blnum));
1232 ntfs_ntvattrrele(vap);
1234 ntfs_ntvattrrele(bmvap);
1236 ntfs_ntvattrrele(iavap);
1244 * Convert NTFS times that are in 100 ns units and begins from
1245 * 1601 Jan 1 into unix times.
1253 /* WindowNT times are in 100 ns and from 1601 Jan 1 */
1254 t.tv_nsec = (nt % (1000 * 1000 * 10)) * 100;
1255 t.tv_sec = nt / (1000 * 1000 * 10) -
1256 369LL * 365LL * 24LL * 60LL * 60LL -
1257 89LL * 1LL * 24LL * 60LL * 60LL;
1262 * Get file times from NTFS_A_NAME attribute.
1266 struct ntfsmount * ntmp,
1270 struct ntvattr *vap;
1273 dprintf(("ntfs_times: ino: %d...\n", ip->i_number));
1275 error = ntfs_ntget(ip);
1279 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_NAME, NULL, 0, &vap);
1284 *tm = vap->va_a_name->n_times;
1285 ntfs_ntvattrrele(vap);
1292 * Get file sizes from corresponding attribute.
1294 * ntnode under fnode should be locked.
1298 struct ntfsmount * ntmp,
1303 struct ntvattr *vap;
1304 struct ntnode *ip = FTONT(fp);
1308 dprintf(("ntfs_filesize: ino: %d\n", ip->i_number));
1310 error = ntfs_ntvattrget(ntmp, ip,
1311 fp->f_attrtype, fp->f_attrname, 0, &vap);
1315 bn = vap->va_allocated;
1316 sz = vap->va_datalen;
1318 dprintf(("ntfs_filesize: %d bytes (%d bytes allocated)\n",
1319 (u_int32_t) sz, (u_int32_t) bn));
1326 ntfs_ntvattrrele(vap);
1332 * This is one of write routine.
1335 ntfs_writeattr_plain(
1336 struct ntfsmount * ntmp,
1348 off_t off = roff, left = rsize, towrite;
1349 caddr_t data = rdata;
1350 struct ntvattr *vap;
1354 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1355 ntfs_btocn(off), &vap);
1358 towrite = min(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1359 ddprintf(("ntfs_writeattr_plain: o: %d, s: %d (%d - %d)\n",
1360 (u_int32_t) off, (u_int32_t) towrite,
1361 (u_int32_t) vap->va_vcnstart,
1362 (u_int32_t) vap->va_vcnend));
1363 error = ntfs_writentvattr_plain(ntmp, ip, vap,
1364 off - ntfs_cntob(vap->va_vcnstart),
1365 towrite, data, &init, uio);
1367 printf("ntfs_writeattr_plain: " \
1368 "ntfs_writentvattr_plain failed: o: %d, s: %d\n",
1369 (u_int32_t) off, (u_int32_t) towrite);
1370 printf("ntfs_writeattr_plain: attrib: %d - %d\n",
1371 (u_int32_t) vap->va_vcnstart,
1372 (u_int32_t) vap->va_vcnend);
1373 ntfs_ntvattrrele(vap);
1376 ntfs_ntvattrrele(vap);
1379 data = data + towrite;
1387 * This is one of write routine.
1389 * ntnode should be locked.
1392 ntfs_writentvattr_plain(
1393 struct ntfsmount * ntmp,
1395 struct ntvattr * vap,
1405 cn_t ccn, ccl, cn, left, cl;
1406 caddr_t data = rdata;
1412 if ((vap->va_flag & NTFS_AF_INRUN) == 0) {
1413 printf("ntfs_writevattr_plain: CAN'T WRITE RES. ATTRIBUTE\n");
1417 ddprintf(("ntfs_writentvattr_plain: data in run: %ld chains\n",
1425 for (; left && (cnt < vap->va_vruncnt); cnt++) {
1426 ccn = vap->va_vruncn[cnt];
1427 ccl = vap->va_vruncl[cnt];
1429 ddprintf(("ntfs_writentvattr_plain: " \
1430 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1431 (u_int32_t) left, (u_int32_t) ccn, \
1432 (u_int32_t) ccl, (u_int32_t) off));
1434 if (ntfs_cntob(ccl) < off) {
1435 off -= ntfs_cntob(ccl);
1439 if (!ccn && ip->i_number != NTFS_BOOTINO)
1442 ccl -= ntfs_btocn(off);
1443 cn = ccn + ntfs_btocn(off);
1444 off = ntfs_btocnoff(off);
1446 while (left && ccl) {
1447 #if defined(__FreeBSD__)
1449 min(ntfs_cntob(ccl) - off, MAXBSIZE - off));
1451 /* under NetBSD, bread() can read
1452 * maximum one block worth of data */
1453 tocopy = min(left, ntmp->ntm_bps - off);
1455 cl = ntfs_btocl(tocopy + off);
1456 ddprintf(("ntfs_writentvattr_plain: write: " \
1457 "cn: 0x%x cl: %d, off: %d len: %d, left: %d\n",
1458 (u_int32_t) cn, (u_int32_t) cl,
1459 (u_int32_t) off, (u_int32_t) tocopy,
1461 if ((off == 0) && (tocopy == ntfs_cntob(cl)))
1463 bp = getblk(ntmp->ntm_devvp, ntfs_cntobn(cn),
1464 ntfs_cntob(cl), 0, 0);
1467 error = bread(ntmp->ntm_devvp, ntfs_cntobn(cn),
1468 ntfs_cntob(cl), &bp);
1475 uiomove(bp->b_data + off, tocopy, uio);
1477 memcpy(bp->b_data + off, data, tocopy);
1479 data = data + tocopy;
1489 printf("ntfs_writentvattr_plain: POSSIBLE RUN ERROR\n");
1497 * This is one of read routines.
1499 * ntnode should be locked.
1502 ntfs_readntvattr_plain(
1503 struct ntfsmount * ntmp,
1505 struct ntvattr * vap,
1516 if (vap->va_flag & NTFS_AF_INRUN) {
1518 cn_t ccn, ccl, cn, left, cl;
1519 caddr_t data = rdata;
1523 ddprintf(("ntfs_readntvattr_plain: data in run: %ld chains\n",
1531 while (left && (cnt < vap->va_vruncnt)) {
1532 ccn = vap->va_vruncn[cnt];
1533 ccl = vap->va_vruncl[cnt];
1535 ddprintf(("ntfs_readntvattr_plain: " \
1536 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1537 (u_int32_t) left, (u_int32_t) ccn, \
1538 (u_int32_t) ccl, (u_int32_t) off));
1540 if (ntfs_cntob(ccl) < off) {
1541 off -= ntfs_cntob(ccl);
1545 if (ccn || ip->i_number == NTFS_BOOTINO) {
1546 ccl -= ntfs_btocn(off);
1547 cn = ccn + ntfs_btocn(off);
1548 off = ntfs_btocnoff(off);
1550 while (left && ccl) {
1551 #if defined(__FreeBSD__)
1553 min(ntfs_cntob(ccl) - off,
1556 /* under NetBSD, bread() can read
1557 * maximum one block worth of data */
1559 ntmp->ntm_bps - off);
1561 cl = ntfs_btocl(tocopy + off);
1562 ddprintf(("ntfs_readntvattr_plain: " \
1563 "read: cn: 0x%x cl: %d, " \
1564 "off: %d len: %d, left: %d\n",
1570 error = bread(ntmp->ntm_devvp,
1579 uiomove(bp->b_data + off,
1582 memcpy(data, bp->b_data + off,
1586 data = data + tocopy;
1594 tocopy = min(left, ntfs_cntob(ccl) - off);
1595 ddprintf(("ntfs_readntvattr_plain: "
1596 "hole: ccn: 0x%x ccl: %d, off: %d, " \
1597 " len: %d, left: %d\n",
1598 (u_int32_t) ccn, (u_int32_t) ccl,
1599 (u_int32_t) off, (u_int32_t) tocopy,
1604 size_t remains = tocopy;
1605 for(; remains; remains++)
1606 uiomove("", 1, uio);
1608 bzero(data, tocopy);
1609 data = data + tocopy;
1614 printf("ntfs_readntvattr_plain: POSSIBLE RUN ERROR\n");
1618 ddprintf(("ntfs_readnvattr_plain: data is in mft record\n"));
1620 uiomove(vap->va_datap + roff, rsize, uio);
1622 memcpy(rdata, vap->va_datap + roff, rsize);
1630 * This is one of read routines.
1633 ntfs_readattr_plain(
1634 struct ntfsmount * ntmp,
1646 off_t off = roff, left = rsize, toread;
1647 caddr_t data = rdata;
1648 struct ntvattr *vap;
1652 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1653 ntfs_btocn(off), &vap);
1656 toread = min(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1657 ddprintf(("ntfs_readattr_plain: o: %d, s: %d (%d - %d)\n",
1658 (u_int32_t) off, (u_int32_t) toread,
1659 (u_int32_t) vap->va_vcnstart,
1660 (u_int32_t) vap->va_vcnend));
1661 error = ntfs_readntvattr_plain(ntmp, ip, vap,
1662 off - ntfs_cntob(vap->va_vcnstart),
1663 toread, data, &init, uio);
1665 printf("ntfs_readattr_plain: " \
1666 "ntfs_readntvattr_plain failed: o: %d, s: %d\n",
1667 (u_int32_t) off, (u_int32_t) toread);
1668 printf("ntfs_readattr_plain: attrib: %d - %d\n",
1669 (u_int32_t) vap->va_vcnstart,
1670 (u_int32_t) vap->va_vcnend);
1671 ntfs_ntvattrrele(vap);
1674 ntfs_ntvattrrele(vap);
1677 data = data + toread;
1685 * This is one of read routines.
1689 struct ntfsmount * ntmp,
1699 struct ntvattr *vap;
1702 ddprintf(("ntfs_readattr: reading %d: 0x%x, from %d size %d bytes\n",
1703 ip->i_number, attrnum, (u_int32_t) roff, (u_int32_t) rsize));
1705 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname, 0, &vap);
1709 if ((roff > vap->va_datalen) ||
1710 (roff + rsize > vap->va_datalen)) {
1711 ddprintf(("ntfs_readattr: offset too big\n"));
1712 ntfs_ntvattrrele(vap);
1715 if (vap->va_compression && vap->va_compressalg) {
1718 off_t off = roff, left = rsize, tocopy;
1719 caddr_t data = rdata;
1722 ddprintf(("ntfs_ntreadattr: compression: %d\n",
1723 vap->va_compressalg));
1725 MALLOC(cup, u_int8_t *, ntfs_cntob(NTFS_COMPUNIT_CL),
1726 M_NTFSDECOMP, M_WAITOK);
1727 MALLOC(uup, u_int8_t *, ntfs_cntob(NTFS_COMPUNIT_CL),
1728 M_NTFSDECOMP, M_WAITOK);
1730 cn = (ntfs_btocn(roff)) & (~(NTFS_COMPUNIT_CL - 1));
1731 off = roff - ntfs_cntob(cn);
1734 error = ntfs_readattr_plain(ntmp, ip, attrnum,
1735 attrname, ntfs_cntob(cn),
1736 ntfs_cntob(NTFS_COMPUNIT_CL),
1741 tocopy = min(left, ntfs_cntob(NTFS_COMPUNIT_CL) - off);
1743 if (init == ntfs_cntob(NTFS_COMPUNIT_CL)) {
1745 uiomove(cup + off, tocopy, uio);
1747 memcpy(data, cup + off, tocopy);
1748 } else if (init == 0) {
1750 size_t remains = tocopy;
1751 for(; remains; remains--)
1752 uiomove("", 1, uio);
1755 bzero(data, tocopy);
1757 error = ntfs_uncompunit(ntmp, uup, cup);
1761 uiomove(uup + off, tocopy, uio);
1763 memcpy(data, uup + off, tocopy);
1767 data = data + tocopy;
1768 off += tocopy - ntfs_cntob(NTFS_COMPUNIT_CL);
1769 cn += NTFS_COMPUNIT_CL;
1772 FREE(uup, M_NTFSDECOMP);
1773 FREE(cup, M_NTFSDECOMP);
1775 error = ntfs_readattr_plain(ntmp, ip, attrnum, attrname,
1776 roff, rsize, rdata, &init, uio);
1777 ntfs_ntvattrrele(vap);
1794 printf("ntfs_parsetun: run == NULL\n");
1799 printf("ntfs_parserun: trying to go out of run\n");
1803 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1804 printf("ntfs_parserun: " \
1805 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1809 for (i = 0; i < (sz & 0xF); i++)
1810 *cl += (u_int32_t) run[(*off)++] << (i << 3);
1813 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1814 printf("ntfs_parserun: " \
1815 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1819 for (i = 0; i < (sz & 0xF); i++)
1820 *cn += (u_int32_t) run[(*off)++] << (i << 3);
1827 * Process fixup routine on given buffer.
1831 struct ntfsmount * ntmp,
1836 struct fixuphdr *fhp = (struct fixuphdr *) buf;
1842 if (fhp->fh_magic != magic) {
1843 printf("ntfs_procfixups: magic doesn't match: %08x != %08x\n",
1844 fhp->fh_magic, magic);
1847 if ((fhp->fh_fnum - 1) * ntmp->ntm_bps != len) {
1848 printf("ntfs_procfixups: " \
1849 "bad fixups number: %d for %ld bytes block\n",
1850 fhp->fh_fnum, (long)len); /* XXX printf kludge */
1853 if (fhp->fh_foff >= ntmp->ntm_spc * ntmp->ntm_mftrecsz * ntmp->ntm_bps) {
1854 printf("ntfs_procfixups: invalid offset: %x", fhp->fh_foff);
1857 fxp = (u_int16_t *) (buf + fhp->fh_foff);
1858 cfxp = (u_int16_t *) (buf + ntmp->ntm_bps - 2);
1860 for (i = 1; i < fhp->fh_fnum; i++, fxp++) {
1861 if (*cfxp != fixup) {
1862 printf("ntfs_procfixups: fixup %d doesn't match\n", i);
1866 ((caddr_t) cfxp) += ntmp->ntm_bps;
1875 struct ntfsmount * ntmp,
1887 printf("ntfs_runtocn: run: 0x%p, %ld bytes, vcn:%ld\n",
1888 run, len, (u_long) vcn);
1889 printf("ntfs_runtocn: run: ");
1890 for (i = 0; i < len; i++)
1891 printf("0x%02x ", run[i]);
1896 printf("ntfs_runtocn: run == NULL\n");
1900 if (run[off] == 0) {
1901 printf("ntfs_runtocn: vcn too big\n");
1905 error = ntfs_parserun(&ccn, &ccl, run, len, &off);
1907 printf("ntfs_runtocn: ntfs_parserun failed\n");
1910 } while (ccl <= vcn);
1917 * this initializes toupper table & dependant variables to be ready for
1923 ntfs_toupper_tab = (wchar *) NULL;
1924 lockinit(&ntfs_toupper_lock, 0, "ntfs_toupper", 0, 0);
1925 ntfs_toupper_usecount = 0;
1929 * if the ntfs_toupper_tab[] is filled already, just raise use count;
1930 * otherwise read the data from the filesystem we are currently mounting
1933 ntfs_toupper_use(mp, ntmp)
1935 struct ntfsmount *ntmp;
1940 /* get exclusive access */
1941 LOCKMGR(&ntfs_toupper_lock, LK_EXCLUSIVE, NULL);
1943 /* only read the translation data from a file if it hasn't been
1945 if (ntfs_toupper_tab)
1949 * Read in Unicode lowercase -> uppercase translation file.
1950 * XXX for now, just the first 256 entries are used anyway,
1951 * so don't bother reading more
1953 MALLOC(ntfs_toupper_tab, wchar *, 65536 * sizeof(wchar),
1954 M_NTFSRDATA, M_WAITOK);
1956 if ((error = VFS_VGET(mp, NTFS_UPCASEINO, &vp)))
1958 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
1959 0, 65536*sizeof(wchar), (char *) ntfs_toupper_tab, NULL);
1963 ntfs_toupper_usecount++;
1964 LOCKMGR(&ntfs_toupper_lock, LK_RELEASE, NULL);
1969 * lower the use count and if it reaches zero, free the memory
1970 * tied by toupper table
1973 ntfs_toupper_unuse()
1975 /* get exclusive access */
1976 LOCKMGR(&ntfs_toupper_lock, LK_EXCLUSIVE, NULL);
1978 ntfs_toupper_usecount--;
1979 if (ntfs_toupper_usecount == 0) {
1980 FREE(ntfs_toupper_tab, M_NTFSRDATA);
1981 ntfs_toupper_tab = NULL;
1984 else if (ntfs_toupper_usecount < 0) {
1985 panic("ntfs_toupper_unuse(): use count negative: %d\n",
1986 ntfs_toupper_usecount);
1990 /* release the lock */
1991 LOCKMGR(&ntfs_toupper_lock, LK_RELEASE, NULL);
1996 struct ntfsmount *ntmp,
2002 MALLOC(u28, char **, 256 * sizeof(char*), M_TEMP, M_WAITOK | M_ZERO);
2004 for (i=0; i<256; i++) {
2005 h = (u2w[i] >> 8) & 0xFF;
2008 if (u28[h] == NULL) {
2009 MALLOC(u28[h], char *, 256 * sizeof(char), M_TEMP, M_WAITOK);
2010 for (j=0; j<256; j++)
2014 u28[h][l] = i & 0xFF;
2017 ntmp->ntm_u28 = u28;
2023 ntfs_u28_uninit(struct ntfsmount *ntmp)
2028 if (ntmp->ntm_u28 == NULL)
2031 u28 = ntmp->ntm_u28;
2033 for (i=0; i<256; i++)
2035 FREE(u28[i], M_TEMP);
2044 struct ntfsmount *ntmp,
2050 MALLOC(_82u, wchar *, 256 * sizeof(wchar), M_TEMP, M_WAITOK);
2053 for (i=0; i<256; i++)
2056 for (i=0; i<128; i++)
2058 for (i=0; i<128; i++)
2059 _82u[i+128] = u2w[i];
2062 ntmp->ntm_82u = _82u;
2068 ntfs_82u_uninit(struct ntfsmount *ntmp)
2070 FREE(ntmp->ntm_82u, M_TEMP);
2075 * maps the Unicode char to 8bit equivalent
2076 * XXX currently only gets lower 8bit from the Unicode char
2077 * and substitutes a '_' for it if the result would be '\0';
2078 * something better has to be definitely though out
2082 struct ntfsmount *ntmp,
2087 p = ntmp->ntm_u28[(wc>>8)&0xFF];
2090 return (p[wc&0xFF]);