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.2 2003/06/17 04:28:54 dillon Exp $
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/systm.h>
35 #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 */
50 #include <ntfs/ntfs.h>
51 #include <ntfs/ntfsmount.h>
52 #include <ntfs/ntfs_inode.h>
53 #include <ntfs/ntfs_vfsops.h>
54 #include <ntfs/ntfs_subr.h>
55 #include <ntfs/ntfs_compr.h>
56 #include <ntfs/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 __P((struct ntfsmount *, const char *, int, int *, char **));
66 static int ntfs_findvattr __P((struct ntfsmount *, struct ntnode *, struct ntvattr **, struct ntvattr **, u_int32_t, const char *, size_t, cn_t));
67 static int ntfs_uastricmp __P((struct ntfsmount *, const wchar *, size_t, const char *, size_t));
68 static int ntfs_uastrcmp __P((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, curproc, &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),
290 printf("ntfs_loadntnode: BREAD FAILED\n");
294 memcpy(mfrp, bp->b_data, ntfs_bntob(ntmp->ntm_bpmftrec));
299 vp = ntmp->ntm_sysvn[NTFS_MFTINO];
300 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
301 ip->i_number * ntfs_bntob(ntmp->ntm_bpmftrec),
302 ntfs_bntob(ntmp->ntm_bpmftrec), mfrp, NULL);
304 printf("ntfs_loadntnode: ntfs_readattr failed\n");
309 /* Check if magic and fixups are correct */
310 error = ntfs_procfixups(ntmp, NTFS_FILEMAGIC, (caddr_t)mfrp,
311 ntfs_bntob(ntmp->ntm_bpmftrec));
313 printf("ntfs_loadntnode: BAD MFT RECORD %d\n",
314 (u_int32_t) ip->i_number);
318 dprintf(("ntfs_loadntnode: load attrs for ino: %d\n",ip->i_number));
319 off = mfrp->fr_attroff;
320 ap = (struct attr *) ((caddr_t)mfrp + off);
322 LIST_INIT(&ip->i_valist);
324 while (ap->a_hdr.a_type != -1) {
325 error = ntfs_attrtontvattr(ntmp, &nvap, ap);
330 LIST_INSERT_HEAD(&ip->i_valist, nvap, va_list);
332 off += ap->a_hdr.reclen;
333 ap = (struct attr *) ((caddr_t)mfrp + off);
336 printf("ntfs_loadntnode: failed to load attr ino: %d\n",
341 ip->i_mainrec = mfrp->fr_mainrec;
342 ip->i_nlink = mfrp->fr_nlink;
343 ip->i_frflag = mfrp->fr_flags;
345 ip->i_flag |= IN_LOADED;
353 * Routine locks ntnode and increase usecount, just opposite of
360 dprintf(("ntfs_ntget: get ntnode %d: %p, usecount: %d\n",
361 ip->i_number, ip, ip->i_usecount));
363 simple_lock(&ip->i_interlock);
365 LOCKMGR(&ip->i_lock, LK_EXCLUSIVE | LK_INTERLOCK, &ip->i_interlock);
371 * Routine search ntnode in hash, if found: lock, inc usecount and return.
372 * If not in hash allocate structure for ntnode, prefill it, lock,
373 * inc count and return.
375 * ntnode returned locked
379 struct ntfsmount * ntmp,
381 struct ntnode ** ipp)
385 dprintf(("ntfs_ntlookup: looking for ntnode %d\n", ino));
388 if ((ip = ntfs_nthashlookup(ntmp->ntm_dev, ino)) != NULL) {
390 dprintf(("ntfs_ntlookup: ntnode %d: %p, usecount: %d\n",
391 ino, ip, ip->i_usecount));
395 } while (LOCKMGR(&ntfs_hashlock, LK_EXCLUSIVE | LK_SLEEPFAIL, NULL));
397 MALLOC(ip, struct ntnode *, sizeof(struct ntnode),
398 M_NTFSNTNODE, M_WAITOK);
399 ddprintf(("ntfs_ntlookup: allocating ntnode: %d: %p\n", ino, ip));
400 bzero((caddr_t) ip, sizeof(struct ntnode));
402 /* Generic initialization */
403 ip->i_devvp = ntmp->ntm_devvp;
404 ip->i_dev = ntmp->ntm_dev;
408 LIST_INIT(&ip->i_fnlist);
411 /* init lock and lock the newborn ntnode */
412 lockinit(&ip->i_lock, PINOD, "ntnode", 0, LK_EXCLUSIVE);
413 simple_lock_init(&ip->i_interlock);
418 LOCKMGR(&ntfs_hashlock, LK_RELEASE, NULL);
422 dprintf(("ntfs_ntlookup: ntnode %d: %p, usecount: %d\n",
423 ino, ip, ip->i_usecount));
429 * Decrement usecount of ntnode and unlock it, if usecount reach zero,
432 * ntnode should be locked on entry, and unlocked on return.
440 dprintf(("ntfs_ntput: rele ntnode %d: %p, usecount: %d\n",
441 ip->i_number, ip, ip->i_usecount));
443 simple_lock(&ip->i_interlock);
447 if (ip->i_usecount < 0) {
448 panic("ntfs_ntput: ino: %d usecount: %d \n",
449 ip->i_number,ip->i_usecount);
453 if (ip->i_usecount > 0) {
454 LOCKMGR(&ip->i_lock, LK_RELEASE|LK_INTERLOCK, &ip->i_interlock);
458 dprintf(("ntfs_ntput: deallocating ntnode: %d\n", ip->i_number));
460 if (ip->i_fnlist.lh_first)
461 panic("ntfs_ntput: ntnode has fnodes\n");
465 while ((vap = LIST_FIRST(&ip->i_valist)) != NULL) {
466 LIST_REMOVE(vap,va_list);
467 ntfs_freentvattr(vap);
471 FREE(ip, M_NTFSNTNODE);
475 * increment usecount of ntnode
481 simple_lock(&ip->i_interlock);
483 simple_unlock(&ip->i_interlock);
485 dprintf(("ntfs_ntref: ino %d, usecount: %d\n",
486 ip->i_number, ip->i_usecount));
491 * Decrement usecount of ntnode.
497 dprintf(("ntfs_ntrele: rele ntnode %d: %p, usecount: %d\n",
498 ip->i_number, ip, ip->i_usecount));
500 simple_lock(&ip->i_interlock);
503 if (ip->i_usecount < 0)
504 panic("ntfs_ntrele: ino: %d usecount: %d \n",
505 ip->i_number,ip->i_usecount);
506 simple_unlock(&ip->i_interlock);
510 * Deallocate all memory allocated for ntvattr
513 ntfs_freentvattr(vap)
514 struct ntvattr * vap;
516 if (vap->va_flag & NTFS_AF_INRUN) {
518 FREE(vap->va_vruncn, M_NTFSRUN);
520 FREE(vap->va_vruncl, M_NTFSRUN);
523 FREE(vap->va_datap, M_NTFSRDATA);
525 FREE(vap, M_NTFSNTVATTR);
529 * Convert disk image of attribute into ntvattr structure,
530 * runs are expanded also.
534 struct ntfsmount * ntmp,
535 struct ntvattr ** rvapp,
544 MALLOC(vap, struct ntvattr *, sizeof(struct ntvattr),
545 M_NTFSNTVATTR, M_WAITOK);
546 bzero(vap, sizeof(struct ntvattr));
548 vap->va_flag = rap->a_hdr.a_flag;
549 vap->va_type = rap->a_hdr.a_type;
550 vap->va_compression = rap->a_hdr.a_compression;
551 vap->va_index = rap->a_hdr.a_index;
553 ddprintf(("type: 0x%x, index: %d", vap->va_type, vap->va_index));
555 vap->va_namelen = rap->a_hdr.a_namelen;
556 if (rap->a_hdr.a_namelen) {
557 wchar *unp = (wchar *) ((caddr_t) rap + rap->a_hdr.a_nameoff);
558 ddprintf((", name:["));
559 for (i = 0; i < vap->va_namelen; i++) {
560 vap->va_name[i] = unp[i];
561 ddprintf(("%c", vap->va_name[i]));
565 if (vap->va_flag & NTFS_AF_INRUN) {
566 ddprintf((", nonres."));
567 vap->va_datalen = rap->a_nr.a_datalen;
568 vap->va_allocated = rap->a_nr.a_allocated;
569 vap->va_vcnstart = rap->a_nr.a_vcnstart;
570 vap->va_vcnend = rap->a_nr.a_vcnend;
571 vap->va_compressalg = rap->a_nr.a_compressalg;
572 error = ntfs_runtovrun(&(vap->va_vruncn), &(vap->va_vruncl),
574 (caddr_t) rap + rap->a_nr.a_dataoff);
576 vap->va_compressalg = 0;
577 ddprintf((", res."));
578 vap->va_datalen = rap->a_r.a_datalen;
579 vap->va_allocated = rap->a_r.a_datalen;
580 vap->va_vcnstart = 0;
581 vap->va_vcnend = ntfs_btocn(vap->va_allocated);
582 MALLOC(vap->va_datap, caddr_t, vap->va_datalen,
583 M_NTFSRDATA, M_WAITOK);
584 memcpy(vap->va_datap, (caddr_t) rap + rap->a_r.a_dataoff,
587 ddprintf((", len: %d", vap->va_datalen));
590 FREE(vap, M_NTFSNTVATTR);
600 * Expand run into more utilizable and more memory eating format.
621 off += (run[off] & 0xF) + ((run[off] >> 4) & 0xF) + 1;
624 MALLOC(cn, cn_t *, cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
625 MALLOC(cl, cn_t *, cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
635 for (i = 0; i < (sz & 0xF); i++)
636 cl[cnt] += (u_int32_t) run[off++] << (i << 3);
639 if (run[off + sz - 1] & 0x80) {
640 tmp = ((u_int64_t) - 1) << (sz << 3);
641 for (i = 0; i < sz; i++)
642 tmp |= (u_int64_t) run[off++] << (i << 3);
645 for (i = 0; i < sz; i++)
646 tmp |= (u_int64_t) run[off++] << (i << 3);
649 prev = cn[cnt] = prev + tmp;
662 * Compare unicode and ascii string case insens.
665 ntfs_uastricmp(ntmp, ustr, ustrlen, astr, astrlen)
666 struct ntfsmount *ntmp;
676 * XXX We use NTFS_82U(NTFS_U28(c)) to get rid of unicode
677 * symbols not covered by translation table
679 for (i = 0; i < ustrlen && i < astrlen; i++) {
680 res = ((int) NTFS_TOUPPER(NTFS_82U(NTFS_U28(ustr[i])))) -
681 ((int)NTFS_TOUPPER(NTFS_82U(astr[i])));
685 return (ustrlen - astrlen);
689 * Compare unicode and ascii string case sens.
692 ntfs_uastrcmp(ntmp, ustr, ustrlen, astr, astrlen)
693 struct ntfsmount *ntmp;
702 for (i = 0; (i < ustrlen) && (i < astrlen); i++) {
703 res = (int) (((char)NTFS_U28(ustr[i])) - astr[i]);
707 return (ustrlen - astrlen);
711 * Search fnode in ntnode, if not found allocate and preinitialize.
713 * ntnode should be locked on entry.
717 struct ntfsmount *ntmp,
725 dprintf(("ntfs_fget: ino: %d, attrtype: 0x%x, attrname: %s\n",
726 ip->i_number,attrtype, attrname?attrname:""));
728 for (fp = ip->i_fnlist.lh_first; fp != NULL; fp = fp->f_fnlist.le_next){
729 dprintf(("ntfs_fget: fnode: attrtype: %d, attrname: %s\n",
730 fp->f_attrtype, fp->f_attrname?fp->f_attrname:""));
732 if ((attrtype == fp->f_attrtype) &&
733 ((!attrname && !fp->f_attrname) ||
734 (attrname && fp->f_attrname &&
735 !strcmp(attrname,fp->f_attrname)))){
736 dprintf(("ntfs_fget: found existed: %p\n",fp));
744 MALLOC(fp, struct fnode *, sizeof(struct fnode), M_NTFSFNODE, M_WAITOK);
745 bzero(fp, sizeof(struct fnode));
746 dprintf(("ntfs_fget: allocating fnode: %p\n",fp));
750 fp->f_flag |= FN_AATTRNAME;
751 MALLOC(fp->f_attrname, char *, strlen(attrname)+1, M_TEMP, M_WAITOK);
752 strcpy(fp->f_attrname, attrname);
754 fp->f_attrname = NULL;
755 fp->f_attrtype = attrtype;
759 LIST_INSERT_HEAD(&ip->i_fnlist, fp, f_fnlist);
767 * Deallocate fnode, remove it from ntnode's fnode list.
769 * ntnode should be locked.
775 struct ntnode *ip = FTONT(fp);
777 dprintf(("ntfs_frele: fnode: %p for %d: %p\n", fp, ip->i_number, ip));
779 dprintf(("ntfs_frele: deallocating fnode\n"));
780 LIST_REMOVE(fp,f_fnlist);
781 if (fp->f_flag & FN_AATTRNAME)
782 FREE(fp->f_attrname, M_TEMP);
784 FREE(fp->f_dirblbuf, M_NTFSDIR);
785 FREE(fp, M_NTFSFNODE);
790 * Lookup attribute name in format: [[:$ATTR_TYPE]:$ATTR_NAME],
791 * $ATTR_TYPE is searched in attrdefs read from $AttrDefs.
792 * If $ATTR_TYPE nott specifed, ATTR_A_DATA assumed.
796 struct ntfsmount * ntmp,
804 struct ntvattrdef *adp;
809 if (name[0] == '$') {
811 for (syslen = 0; syslen < namelen; syslen++) {
812 if(sys[syslen] == ':') {
822 for (i = 0; i < ntmp->ntm_adnum; i++, adp++){
823 if (syslen != adp->ad_namelen ||
824 strncmp(sys, adp->ad_name, syslen) != 0)
827 *attrtype = adp->ad_type;
832 *attrtype = NTFS_A_DATA;
836 MALLOC((*attrname), char *, namelen, M_TEMP, M_WAITOK);
837 memcpy((*attrname), name, namelen);
838 (*attrname)[namelen] = '\0';
845 * Lookup specifed node for filename, matching cnp,
846 * return fnode filled.
850 struct ntfsmount * ntmp,
852 struct componentname * cnp,
855 struct fnode *fp = VTOF(vp);
856 struct ntnode *ip = FTONT(fp);
857 struct ntvattr *vap; /* Root attribute */
858 cn_t cn; /* VCN in current attribute */
859 caddr_t rdbuf; /* Buffer to read directory's blocks */
861 u_int32_t rdsize; /* Length of data to read from current block */
862 struct attr_indexentry *iep;
863 int error, res, anamelen, fnamelen;
864 const char *fname,*aname;
866 int attrtype = NTFS_A_DATA;
867 char *attrname = NULL;
872 error = ntfs_ntget(ip);
876 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
877 if (error || (vap->va_flag & NTFS_AF_INRUN))
880 blsize = vap->va_a_iroot->ir_size;
881 rdsize = vap->va_datalen;
884 * Divide file name into: foofilefoofilefoofile[:attrspec]
885 * Store like this: fname:fnamelen [aname:anamelen]
887 fname = cnp->cn_nameptr;
890 for (fnamelen = 0; fnamelen < cnp->cn_namelen; fnamelen++)
891 if(fname[fnamelen] == ':') {
892 aname = fname + fnamelen + 1;
893 anamelen = cnp->cn_namelen - fnamelen - 1;
894 dprintf(("ntfs_ntlookupfile: %s (%d), attr: %s (%d)\n",
895 fname, fnamelen, aname, anamelen));
899 dprintf(("ntfs_ntlookupfile: blksz: %d, rdsz: %d\n", blsize, rdsize));
901 MALLOC(rdbuf, caddr_t, blsize, M_TEMP, M_WAITOK);
903 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXROOT, "$I30",
904 0, rdsize, rdbuf, NULL);
908 aoff = sizeof(struct attr_indexroot);
911 iep = (struct attr_indexentry *) (rdbuf + aoff);
913 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
915 iep = (struct attr_indexentry *) (rdbuf + aoff))
917 ddprintf(("scan: %d, %d\n",
918 (u_int32_t) iep->ie_number,
919 (u_int32_t) iep->ie_fnametype));
921 /* check the name - the case-insensitible check
922 * has to come first, to break from this for loop
923 * if needed, so we can dive correctly */
924 res = NTFS_UASTRICMP(iep->ie_fname, iep->ie_fnamelen,
927 if (res < 0) continue;
929 if (iep->ie_fnametype == 0 ||
930 !(ntmp->ntm_flag & NTFS_MFLAG_CASEINS))
932 res = NTFS_UASTRCMP(iep->ie_fname,
933 iep->ie_fnamelen, fname, fnamelen);
934 if (res != 0) continue;
938 error = ntfs_ntlookupattr(ntmp,
940 &attrtype, &attrname);
945 /* Check if we've found ourself */
946 if ((iep->ie_number == ip->i_number) &&
947 (attrtype == fp->f_attrtype) &&
948 ((!attrname && !fp->f_attrname) ||
949 (attrname && fp->f_attrname &&
950 !strcmp(attrname, fp->f_attrname))))
958 /* vget node, but don't load it */
959 error = ntfs_vgetex(ntmp->ntm_mountp,
960 iep->ie_number, attrtype, attrname,
961 LK_EXCLUSIVE, VG_DONTLOADIN | VG_DONTVALIDFN,
964 /* free the buffer returned by ntfs_ntlookupattr() */
966 FREE(attrname, M_TEMP);
975 if (nfp->f_flag & FN_VALID) {
980 nfp->f_fflag = iep->ie_fflag;
981 nfp->f_pnumber = iep->ie_fpnumber;
982 nfp->f_times = iep->ie_ftimes;
984 if((nfp->f_fflag & NTFS_FFLAG_DIR) &&
985 (nfp->f_attrtype == NTFS_A_DATA) &&
986 (nfp->f_attrname == NULL))
991 nvp->v_type = f_type;
993 if ((nfp->f_attrtype == NTFS_A_DATA) &&
994 (nfp->f_attrname == NULL))
996 /* Opening default attribute */
997 nfp->f_size = iep->ie_fsize;
998 nfp->f_allocated = iep->ie_fallocated;
999 nfp->f_flag |= FN_PRELOADED;
1001 error = ntfs_filesize(ntmp, nfp,
1002 &nfp->f_size, &nfp->f_allocated);
1009 nfp->f_flag &= ~FN_VALID;
1014 /* Dive if possible */
1015 if (iep->ie_flag & NTFS_IEFLAG_SUBNODE) {
1016 dprintf(("ntfs_ntlookupfile: diving\n"));
1018 cn = *(cn_t *) (rdbuf + aoff +
1019 iep->reclen - sizeof(cn_t));
1022 error = ntfs_readattr(ntmp, ip, NTFS_A_INDX, "$I30",
1023 ntfs_cntob(cn), rdsize, rdbuf, NULL);
1027 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1032 aoff = (((struct attr_indexalloc *) rdbuf)->ia_hdrsize +
1035 dprintf(("ntfs_ntlookupfile: nowhere to dive :-(\n"));
1041 dprintf(("finish\n"));
1044 if (attrname) FREE(attrname, M_TEMP);
1045 ntfs_ntvattrrele(vap);
1047 FREE(rdbuf, M_TEMP);
1052 * Check if name type is permitted to show.
1055 ntfs_isnamepermitted(
1056 struct ntfsmount * ntmp,
1057 struct attr_indexentry * iep)
1059 if (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)
1062 switch (iep->ie_fnametype) {
1064 ddprintf(("ntfs_isnamepermitted: skiped DOS name\n"));
1066 case 0: case 1: case 3:
1069 printf("ntfs_isnamepermitted: " \
1070 "WARNING! Unknown file name type: %d\n",
1078 * Read ntfs dir like stream of attr_indexentry, not like btree of them.
1079 * This is done by scaning $BITMAP:$I30 for busy clusters and reading them.
1080 * Ofcouse $INDEX_ROOT:$I30 is read before. Last read values are stored in
1081 * fnode, so we can skip toward record number num almost immediatly.
1082 * Anyway this is rather slow routine. The problem is that we don't know
1083 * how many records are there in $INDEX_ALLOCATION:$I30 block.
1087 struct ntfsmount * ntmp,
1090 struct attr_indexentry ** riepp)
1092 struct ntnode *ip = FTONT(fp);
1093 struct ntvattr *vap = NULL; /* IndexRoot attribute */
1094 struct ntvattr *bmvap = NULL; /* BitMap attribute */
1095 struct ntvattr *iavap = NULL; /* IndexAllocation attribute */
1096 caddr_t rdbuf; /* Buffer to read directory's blocks */
1097 u_char *bmp = NULL; /* Bitmap */
1098 u_int32_t blsize; /* Index allocation size (2048) */
1099 u_int32_t rdsize; /* Length of data to read */
1100 u_int32_t attrnum; /* Current attribute type */
1101 u_int32_t cpbl = 1; /* Clusters per directory block */
1103 struct attr_indexentry *iep;
1105 u_int32_t aoff, cnum;
1107 dprintf(("ntfs_ntreaddir: read ino: %d, num: %d\n", ip->i_number, num));
1108 error = ntfs_ntget(ip);
1112 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
1116 if (fp->f_dirblbuf == NULL) {
1117 fp->f_dirblsz = vap->va_a_iroot->ir_size;
1118 MALLOC(fp->f_dirblbuf, caddr_t,
1119 max(vap->va_datalen,fp->f_dirblsz), M_NTFSDIR, M_WAITOK);
1122 blsize = fp->f_dirblsz;
1123 rdbuf = fp->f_dirblbuf;
1125 dprintf(("ntfs_ntreaddir: rdbuf: 0x%p, blsize: %d\n", rdbuf, blsize));
1127 if (vap->va_a_iroot->ir_flag & NTFS_IRFLAG_INDXALLOC) {
1128 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXBITMAP, "$I30",
1134 MALLOC(bmp, u_char *, bmvap->va_datalen, M_TEMP, M_WAITOK);
1135 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXBITMAP, "$I30", 0,
1136 bmvap->va_datalen, bmp, NULL);
1140 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDX, "$I30",
1146 cpbl = ntfs_btocn(blsize + ntfs_cntob(1) - 1);
1147 dprintf(("ntfs_ntreaddir: indexalloc: %d, cpbl: %d\n",
1148 iavap->va_datalen, cpbl));
1150 dprintf(("ntfs_ntreadidir: w/o BitMap and IndexAllocation\n"));
1151 iavap = bmvap = NULL;
1155 /* Try use previous values */
1156 if ((fp->f_lastdnum < num) && (fp->f_lastdnum != 0)) {
1157 attrnum = fp->f_lastdattr;
1158 aoff = fp->f_lastdoff;
1159 blnum = fp->f_lastdblnum;
1160 cnum = fp->f_lastdnum;
1162 attrnum = NTFS_A_INDXROOT;
1163 aoff = sizeof(struct attr_indexroot);
1169 dprintf(("ntfs_ntreaddir: scan: 0x%x, %d, %d, %d, %d\n",
1170 attrnum, (u_int32_t) blnum, cnum, num, aoff));
1171 rdsize = (attrnum == NTFS_A_INDXROOT) ? vap->va_datalen : blsize;
1172 error = ntfs_readattr(ntmp, ip, attrnum, "$I30",
1173 ntfs_cntob(blnum * cpbl), rdsize, rdbuf, NULL);
1177 if (attrnum == NTFS_A_INDX) {
1178 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1184 aoff = (attrnum == NTFS_A_INDX) ?
1185 (0x18 + ((struct attr_indexalloc *) rdbuf)->ia_hdrsize) :
1186 sizeof(struct attr_indexroot);
1188 iep = (struct attr_indexentry *) (rdbuf + aoff);
1189 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
1190 aoff += iep->reclen,
1191 iep = (struct attr_indexentry *) (rdbuf + aoff))
1193 if (!ntfs_isnamepermitted(ntmp, iep)) continue;
1196 fp->f_lastdnum = cnum;
1197 fp->f_lastdoff = aoff;
1198 fp->f_lastdblnum = blnum;
1199 fp->f_lastdattr = attrnum;
1210 if (attrnum == NTFS_A_INDXROOT)
1215 while (ntfs_cntob(blnum * cpbl) < iavap->va_datalen) {
1216 if (bmp[blnum >> 3] & (1 << (blnum & 3)))
1221 attrnum = NTFS_A_INDX;
1223 if (ntfs_cntob(blnum * cpbl) >= iavap->va_datalen)
1225 dprintf(("ntfs_ntreaddir: blnum: %d\n", (u_int32_t) blnum));
1234 ntfs_ntvattrrele(vap);
1236 ntfs_ntvattrrele(bmvap);
1238 ntfs_ntvattrrele(iavap);
1246 * Convert NTFS times that are in 100 ns units and begins from
1247 * 1601 Jan 1 into unix times.
1255 /* WindowNT times are in 100 ns and from 1601 Jan 1 */
1256 t.tv_nsec = (nt % (1000 * 1000 * 10)) * 100;
1257 t.tv_sec = nt / (1000 * 1000 * 10) -
1258 369LL * 365LL * 24LL * 60LL * 60LL -
1259 89LL * 1LL * 24LL * 60LL * 60LL;
1264 * Get file times from NTFS_A_NAME attribute.
1268 struct ntfsmount * ntmp,
1272 struct ntvattr *vap;
1275 dprintf(("ntfs_times: ino: %d...\n", ip->i_number));
1277 error = ntfs_ntget(ip);
1281 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_NAME, NULL, 0, &vap);
1286 *tm = vap->va_a_name->n_times;
1287 ntfs_ntvattrrele(vap);
1294 * Get file sizes from corresponding attribute.
1296 * ntnode under fnode should be locked.
1300 struct ntfsmount * ntmp,
1305 struct ntvattr *vap;
1306 struct ntnode *ip = FTONT(fp);
1310 dprintf(("ntfs_filesize: ino: %d\n", ip->i_number));
1312 error = ntfs_ntvattrget(ntmp, ip,
1313 fp->f_attrtype, fp->f_attrname, 0, &vap);
1317 bn = vap->va_allocated;
1318 sz = vap->va_datalen;
1320 dprintf(("ntfs_filesize: %d bytes (%d bytes allocated)\n",
1321 (u_int32_t) sz, (u_int32_t) bn));
1328 ntfs_ntvattrrele(vap);
1334 * This is one of write routine.
1337 ntfs_writeattr_plain(
1338 struct ntfsmount * ntmp,
1350 off_t off = roff, left = rsize, towrite;
1351 caddr_t data = rdata;
1352 struct ntvattr *vap;
1356 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1357 ntfs_btocn(off), &vap);
1360 towrite = min(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1361 ddprintf(("ntfs_writeattr_plain: o: %d, s: %d (%d - %d)\n",
1362 (u_int32_t) off, (u_int32_t) towrite,
1363 (u_int32_t) vap->va_vcnstart,
1364 (u_int32_t) vap->va_vcnend));
1365 error = ntfs_writentvattr_plain(ntmp, ip, vap,
1366 off - ntfs_cntob(vap->va_vcnstart),
1367 towrite, data, &init, uio);
1369 printf("ntfs_writeattr_plain: " \
1370 "ntfs_writentvattr_plain failed: o: %d, s: %d\n",
1371 (u_int32_t) off, (u_int32_t) towrite);
1372 printf("ntfs_writeattr_plain: attrib: %d - %d\n",
1373 (u_int32_t) vap->va_vcnstart,
1374 (u_int32_t) vap->va_vcnend);
1375 ntfs_ntvattrrele(vap);
1378 ntfs_ntvattrrele(vap);
1381 data = data + towrite;
1389 * This is one of write routine.
1391 * ntnode should be locked.
1394 ntfs_writentvattr_plain(
1395 struct ntfsmount * ntmp,
1397 struct ntvattr * vap,
1407 cn_t ccn, ccl, cn, left, cl;
1408 caddr_t data = rdata;
1414 if ((vap->va_flag & NTFS_AF_INRUN) == 0) {
1415 printf("ntfs_writevattr_plain: CAN'T WRITE RES. ATTRIBUTE\n");
1419 ddprintf(("ntfs_writentvattr_plain: data in run: %ld chains\n",
1427 for (; left && (cnt < vap->va_vruncnt); cnt++) {
1428 ccn = vap->va_vruncn[cnt];
1429 ccl = vap->va_vruncl[cnt];
1431 ddprintf(("ntfs_writentvattr_plain: " \
1432 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1433 (u_int32_t) left, (u_int32_t) ccn, \
1434 (u_int32_t) ccl, (u_int32_t) off));
1436 if (ntfs_cntob(ccl) < off) {
1437 off -= ntfs_cntob(ccl);
1441 if (!ccn && ip->i_number != NTFS_BOOTINO)
1444 ccl -= ntfs_btocn(off);
1445 cn = ccn + ntfs_btocn(off);
1446 off = ntfs_btocnoff(off);
1448 while (left && ccl) {
1449 #if defined(__FreeBSD__)
1451 min(ntfs_cntob(ccl) - off, MAXBSIZE - off));
1453 /* under NetBSD, bread() can read
1454 * maximum one block worth of data */
1455 tocopy = min(left, ntmp->ntm_bps - off);
1457 cl = ntfs_btocl(tocopy + off);
1458 ddprintf(("ntfs_writentvattr_plain: write: " \
1459 "cn: 0x%x cl: %d, off: %d len: %d, left: %d\n",
1460 (u_int32_t) cn, (u_int32_t) cl,
1461 (u_int32_t) off, (u_int32_t) tocopy,
1463 if ((off == 0) && (tocopy == ntfs_cntob(cl)))
1465 bp = getblk(ntmp->ntm_devvp, ntfs_cntobn(cn),
1466 ntfs_cntob(cl), 0, 0);
1469 error = bread(ntmp->ntm_devvp, ntfs_cntobn(cn),
1470 ntfs_cntob(cl), NOCRED, &bp);
1477 uiomove(bp->b_data + off, tocopy, uio);
1479 memcpy(bp->b_data + off, data, tocopy);
1481 data = data + tocopy;
1491 printf("ntfs_writentvattr_plain: POSSIBLE RUN ERROR\n");
1499 * This is one of read routines.
1501 * ntnode should be locked.
1504 ntfs_readntvattr_plain(
1505 struct ntfsmount * ntmp,
1507 struct ntvattr * vap,
1518 if (vap->va_flag & NTFS_AF_INRUN) {
1520 cn_t ccn, ccl, cn, left, cl;
1521 caddr_t data = rdata;
1525 ddprintf(("ntfs_readntvattr_plain: data in run: %ld chains\n",
1533 while (left && (cnt < vap->va_vruncnt)) {
1534 ccn = vap->va_vruncn[cnt];
1535 ccl = vap->va_vruncl[cnt];
1537 ddprintf(("ntfs_readntvattr_plain: " \
1538 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1539 (u_int32_t) left, (u_int32_t) ccn, \
1540 (u_int32_t) ccl, (u_int32_t) off));
1542 if (ntfs_cntob(ccl) < off) {
1543 off -= ntfs_cntob(ccl);
1547 if (ccn || ip->i_number == NTFS_BOOTINO) {
1548 ccl -= ntfs_btocn(off);
1549 cn = ccn + ntfs_btocn(off);
1550 off = ntfs_btocnoff(off);
1552 while (left && ccl) {
1553 #if defined(__FreeBSD__)
1555 min(ntfs_cntob(ccl) - off,
1558 /* under NetBSD, bread() can read
1559 * maximum one block worth of data */
1561 ntmp->ntm_bps - off);
1563 cl = ntfs_btocl(tocopy + off);
1564 ddprintf(("ntfs_readntvattr_plain: " \
1565 "read: cn: 0x%x cl: %d, " \
1566 "off: %d len: %d, left: %d\n",
1572 error = bread(ntmp->ntm_devvp,
1581 uiomove(bp->b_data + off,
1584 memcpy(data, bp->b_data + off,
1588 data = data + tocopy;
1596 tocopy = min(left, ntfs_cntob(ccl) - off);
1597 ddprintf(("ntfs_readntvattr_plain: "
1598 "hole: ccn: 0x%x ccl: %d, off: %d, " \
1599 " len: %d, left: %d\n",
1600 (u_int32_t) ccn, (u_int32_t) ccl,
1601 (u_int32_t) off, (u_int32_t) tocopy,
1606 size_t remains = tocopy;
1607 for(; remains; remains++)
1608 uiomove("", 1, uio);
1610 bzero(data, tocopy);
1611 data = data + tocopy;
1616 printf("ntfs_readntvattr_plain: POSSIBLE RUN ERROR\n");
1620 ddprintf(("ntfs_readnvattr_plain: data is in mft record\n"));
1622 uiomove(vap->va_datap + roff, rsize, uio);
1624 memcpy(rdata, vap->va_datap + roff, rsize);
1632 * This is one of read routines.
1635 ntfs_readattr_plain(
1636 struct ntfsmount * ntmp,
1648 off_t off = roff, left = rsize, toread;
1649 caddr_t data = rdata;
1650 struct ntvattr *vap;
1654 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1655 ntfs_btocn(off), &vap);
1658 toread = min(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1659 ddprintf(("ntfs_readattr_plain: o: %d, s: %d (%d - %d)\n",
1660 (u_int32_t) off, (u_int32_t) toread,
1661 (u_int32_t) vap->va_vcnstart,
1662 (u_int32_t) vap->va_vcnend));
1663 error = ntfs_readntvattr_plain(ntmp, ip, vap,
1664 off - ntfs_cntob(vap->va_vcnstart),
1665 toread, data, &init, uio);
1667 printf("ntfs_readattr_plain: " \
1668 "ntfs_readntvattr_plain failed: o: %d, s: %d\n",
1669 (u_int32_t) off, (u_int32_t) toread);
1670 printf("ntfs_readattr_plain: attrib: %d - %d\n",
1671 (u_int32_t) vap->va_vcnstart,
1672 (u_int32_t) vap->va_vcnend);
1673 ntfs_ntvattrrele(vap);
1676 ntfs_ntvattrrele(vap);
1679 data = data + toread;
1687 * This is one of read routines.
1691 struct ntfsmount * ntmp,
1701 struct ntvattr *vap;
1704 ddprintf(("ntfs_readattr: reading %d: 0x%x, from %d size %d bytes\n",
1705 ip->i_number, attrnum, (u_int32_t) roff, (u_int32_t) rsize));
1707 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname, 0, &vap);
1711 if ((roff > vap->va_datalen) ||
1712 (roff + rsize > vap->va_datalen)) {
1713 ddprintf(("ntfs_readattr: offset too big\n"));
1714 ntfs_ntvattrrele(vap);
1717 if (vap->va_compression && vap->va_compressalg) {
1720 off_t off = roff, left = rsize, tocopy;
1721 caddr_t data = rdata;
1724 ddprintf(("ntfs_ntreadattr: compression: %d\n",
1725 vap->va_compressalg));
1727 MALLOC(cup, u_int8_t *, ntfs_cntob(NTFS_COMPUNIT_CL),
1728 M_NTFSDECOMP, M_WAITOK);
1729 MALLOC(uup, u_int8_t *, ntfs_cntob(NTFS_COMPUNIT_CL),
1730 M_NTFSDECOMP, M_WAITOK);
1732 cn = (ntfs_btocn(roff)) & (~(NTFS_COMPUNIT_CL - 1));
1733 off = roff - ntfs_cntob(cn);
1736 error = ntfs_readattr_plain(ntmp, ip, attrnum,
1737 attrname, ntfs_cntob(cn),
1738 ntfs_cntob(NTFS_COMPUNIT_CL),
1743 tocopy = min(left, ntfs_cntob(NTFS_COMPUNIT_CL) - off);
1745 if (init == ntfs_cntob(NTFS_COMPUNIT_CL)) {
1747 uiomove(cup + off, tocopy, uio);
1749 memcpy(data, cup + off, tocopy);
1750 } else if (init == 0) {
1752 size_t remains = tocopy;
1753 for(; remains; remains--)
1754 uiomove("", 1, uio);
1757 bzero(data, tocopy);
1759 error = ntfs_uncompunit(ntmp, uup, cup);
1763 uiomove(uup + off, tocopy, uio);
1765 memcpy(data, uup + off, tocopy);
1769 data = data + tocopy;
1770 off += tocopy - ntfs_cntob(NTFS_COMPUNIT_CL);
1771 cn += NTFS_COMPUNIT_CL;
1774 FREE(uup, M_NTFSDECOMP);
1775 FREE(cup, M_NTFSDECOMP);
1777 error = ntfs_readattr_plain(ntmp, ip, attrnum, attrname,
1778 roff, rsize, rdata, &init, uio);
1779 ntfs_ntvattrrele(vap);
1796 printf("ntfs_parsetun: run == NULL\n");
1801 printf("ntfs_parserun: trying to go out of run\n");
1805 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1806 printf("ntfs_parserun: " \
1807 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1811 for (i = 0; i < (sz & 0xF); i++)
1812 *cl += (u_int32_t) run[(*off)++] << (i << 3);
1815 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1816 printf("ntfs_parserun: " \
1817 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1821 for (i = 0; i < (sz & 0xF); i++)
1822 *cn += (u_int32_t) run[(*off)++] << (i << 3);
1829 * Process fixup routine on given buffer.
1833 struct ntfsmount * ntmp,
1838 struct fixuphdr *fhp = (struct fixuphdr *) buf;
1844 if (fhp->fh_magic != magic) {
1845 printf("ntfs_procfixups: magic doesn't match: %08x != %08x\n",
1846 fhp->fh_magic, magic);
1849 if ((fhp->fh_fnum - 1) * ntmp->ntm_bps != len) {
1850 printf("ntfs_procfixups: " \
1851 "bad fixups number: %d for %ld bytes block\n",
1852 fhp->fh_fnum, (long)len); /* XXX printf kludge */
1855 if (fhp->fh_foff >= ntmp->ntm_spc * ntmp->ntm_mftrecsz * ntmp->ntm_bps) {
1856 printf("ntfs_procfixups: invalid offset: %x", fhp->fh_foff);
1859 fxp = (u_int16_t *) (buf + fhp->fh_foff);
1860 cfxp = (u_int16_t *) (buf + ntmp->ntm_bps - 2);
1862 for (i = 1; i < fhp->fh_fnum; i++, fxp++) {
1863 if (*cfxp != fixup) {
1864 printf("ntfs_procfixups: fixup %d doesn't match\n", i);
1868 ((caddr_t) cfxp) += ntmp->ntm_bps;
1877 struct ntfsmount * ntmp,
1889 printf("ntfs_runtocn: run: 0x%p, %ld bytes, vcn:%ld\n",
1890 run, len, (u_long) vcn);
1891 printf("ntfs_runtocn: run: ");
1892 for (i = 0; i < len; i++)
1893 printf("0x%02x ", run[i]);
1898 printf("ntfs_runtocn: run == NULL\n");
1902 if (run[off] == 0) {
1903 printf("ntfs_runtocn: vcn too big\n");
1907 error = ntfs_parserun(&ccn, &ccl, run, len, &off);
1909 printf("ntfs_runtocn: ntfs_parserun failed\n");
1912 } while (ccl <= vcn);
1919 * this initializes toupper table & dependant variables to be ready for
1925 ntfs_toupper_tab = (wchar *) NULL;
1926 lockinit(&ntfs_toupper_lock, PVFS, "ntfs_toupper", 0, 0);
1927 ntfs_toupper_usecount = 0;
1931 * if the ntfs_toupper_tab[] is filled already, just raise use count;
1932 * otherwise read the data from the filesystem we are currently mounting
1935 ntfs_toupper_use(mp, ntmp)
1937 struct ntfsmount *ntmp;
1942 /* get exclusive access */
1943 LOCKMGR(&ntfs_toupper_lock, LK_EXCLUSIVE, NULL);
1945 /* only read the translation data from a file if it hasn't been
1947 if (ntfs_toupper_tab)
1951 * Read in Unicode lowercase -> uppercase translation file.
1952 * XXX for now, just the first 256 entries are used anyway,
1953 * so don't bother reading more
1955 MALLOC(ntfs_toupper_tab, wchar *, 65536 * sizeof(wchar),
1956 M_NTFSRDATA, M_WAITOK);
1958 if ((error = VFS_VGET(mp, NTFS_UPCASEINO, &vp)))
1960 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
1961 0, 65536*sizeof(wchar), (char *) ntfs_toupper_tab, NULL);
1965 ntfs_toupper_usecount++;
1966 LOCKMGR(&ntfs_toupper_lock, LK_RELEASE, NULL);
1971 * lower the use count and if it reaches zero, free the memory
1972 * tied by toupper table
1975 ntfs_toupper_unuse()
1977 /* get exclusive access */
1978 LOCKMGR(&ntfs_toupper_lock, LK_EXCLUSIVE, NULL);
1980 ntfs_toupper_usecount--;
1981 if (ntfs_toupper_usecount == 0) {
1982 FREE(ntfs_toupper_tab, M_NTFSRDATA);
1983 ntfs_toupper_tab = NULL;
1986 else if (ntfs_toupper_usecount < 0) {
1987 panic("ntfs_toupper_unuse(): use count negative: %d\n",
1988 ntfs_toupper_usecount);
1992 /* release the lock */
1993 LOCKMGR(&ntfs_toupper_lock, LK_RELEASE, NULL);
1998 struct ntfsmount *ntmp,
2004 MALLOC(u28, char **, 256 * sizeof(char*), M_TEMP, M_WAITOK | M_ZERO);
2006 for (i=0; i<256; i++) {
2007 h = (u2w[i] >> 8) & 0xFF;
2010 if (u28[h] == NULL) {
2011 MALLOC(u28[h], char *, 256 * sizeof(char), M_TEMP, M_WAITOK);
2012 for (j=0; j<256; j++)
2016 u28[h][l] = i & 0xFF;
2019 ntmp->ntm_u28 = u28;
2025 ntfs_u28_uninit(struct ntfsmount *ntmp)
2030 if (ntmp->ntm_u28 == NULL)
2033 u28 = ntmp->ntm_u28;
2035 for (i=0; i<256; i++)
2037 FREE(u28[i], M_TEMP);
2046 struct ntfsmount *ntmp,
2052 MALLOC(_82u, wchar *, 256 * sizeof(wchar), M_TEMP, M_WAITOK);
2055 for (i=0; i<256; i++)
2058 for (i=0; i<128; i++)
2060 for (i=0; i<128; i++)
2061 _82u[i+128] = u2w[i];
2064 ntmp->ntm_82u = _82u;
2070 ntfs_82u_uninit(struct ntfsmount *ntmp)
2072 FREE(ntmp->ntm_82u, M_TEMP);
2077 * maps the Unicode char to 8bit equivalent
2078 * XXX currently only gets lower 8bit from the Unicode char
2079 * and substitutes a '_' for it if the result would be '\0';
2080 * something better has to be definitely though out
2084 struct ntfsmount *ntmp,
2089 p = ntmp->ntm_u28[(wc>>8)&0xFF];
2092 return (p[wc&0xFF]);