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.5 2003/07/06 21:23:53 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 */
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, 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));
362 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, PINOD, "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);
470 FREE(ip, M_NTFSNTNODE);
474 * increment usecount of ntnode
480 lwkt_gettoken(&ip->i_interlock);
482 lwkt_reltoken(&ip->i_interlock);
484 dprintf(("ntfs_ntref: ino %d, usecount: %d\n",
485 ip->i_number, ip->i_usecount));
490 * Decrement usecount of ntnode.
496 dprintf(("ntfs_ntrele: rele ntnode %d: %p, usecount: %d\n",
497 ip->i_number, ip, ip->i_usecount));
499 lwkt_gettoken(&ip->i_interlock);
502 if (ip->i_usecount < 0)
503 panic("ntfs_ntrele: ino: %d usecount: %d \n",
504 ip->i_number,ip->i_usecount);
505 lwkt_reltoken(&ip->i_interlock);
509 * Deallocate all memory allocated for ntvattr
512 ntfs_freentvattr(vap)
513 struct ntvattr * vap;
515 if (vap->va_flag & NTFS_AF_INRUN) {
517 FREE(vap->va_vruncn, M_NTFSRUN);
519 FREE(vap->va_vruncl, M_NTFSRUN);
522 FREE(vap->va_datap, M_NTFSRDATA);
524 FREE(vap, M_NTFSNTVATTR);
528 * Convert disk image of attribute into ntvattr structure,
529 * runs are expanded also.
533 struct ntfsmount * ntmp,
534 struct ntvattr ** rvapp,
543 MALLOC(vap, struct ntvattr *, sizeof(struct ntvattr),
544 M_NTFSNTVATTR, M_WAITOK);
545 bzero(vap, sizeof(struct ntvattr));
547 vap->va_flag = rap->a_hdr.a_flag;
548 vap->va_type = rap->a_hdr.a_type;
549 vap->va_compression = rap->a_hdr.a_compression;
550 vap->va_index = rap->a_hdr.a_index;
552 ddprintf(("type: 0x%x, index: %d", vap->va_type, vap->va_index));
554 vap->va_namelen = rap->a_hdr.a_namelen;
555 if (rap->a_hdr.a_namelen) {
556 wchar *unp = (wchar *) ((caddr_t) rap + rap->a_hdr.a_nameoff);
557 ddprintf((", name:["));
558 for (i = 0; i < vap->va_namelen; i++) {
559 vap->va_name[i] = unp[i];
560 ddprintf(("%c", vap->va_name[i]));
564 if (vap->va_flag & NTFS_AF_INRUN) {
565 ddprintf((", nonres."));
566 vap->va_datalen = rap->a_nr.a_datalen;
567 vap->va_allocated = rap->a_nr.a_allocated;
568 vap->va_vcnstart = rap->a_nr.a_vcnstart;
569 vap->va_vcnend = rap->a_nr.a_vcnend;
570 vap->va_compressalg = rap->a_nr.a_compressalg;
571 error = ntfs_runtovrun(&(vap->va_vruncn), &(vap->va_vruncl),
573 (caddr_t) rap + rap->a_nr.a_dataoff);
575 vap->va_compressalg = 0;
576 ddprintf((", res."));
577 vap->va_datalen = rap->a_r.a_datalen;
578 vap->va_allocated = rap->a_r.a_datalen;
579 vap->va_vcnstart = 0;
580 vap->va_vcnend = ntfs_btocn(vap->va_allocated);
581 MALLOC(vap->va_datap, caddr_t, vap->va_datalen,
582 M_NTFSRDATA, M_WAITOK);
583 memcpy(vap->va_datap, (caddr_t) rap + rap->a_r.a_dataoff,
586 ddprintf((", len: %d", vap->va_datalen));
589 FREE(vap, M_NTFSNTVATTR);
599 * Expand run into more utilizable and more memory eating format.
620 off += (run[off] & 0xF) + ((run[off] >> 4) & 0xF) + 1;
623 MALLOC(cn, cn_t *, cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
624 MALLOC(cl, cn_t *, cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
634 for (i = 0; i < (sz & 0xF); i++)
635 cl[cnt] += (u_int32_t) run[off++] << (i << 3);
638 if (run[off + sz - 1] & 0x80) {
639 tmp = ((u_int64_t) - 1) << (sz << 3);
640 for (i = 0; i < sz; i++)
641 tmp |= (u_int64_t) run[off++] << (i << 3);
644 for (i = 0; i < sz; i++)
645 tmp |= (u_int64_t) run[off++] << (i << 3);
648 prev = cn[cnt] = prev + tmp;
661 * Compare unicode and ascii string case insens.
664 ntfs_uastricmp(ntmp, ustr, ustrlen, astr, astrlen)
665 struct ntfsmount *ntmp;
675 * XXX We use NTFS_82U(NTFS_U28(c)) to get rid of unicode
676 * symbols not covered by translation table
678 for (i = 0; i < ustrlen && i < astrlen; i++) {
679 res = ((int) NTFS_TOUPPER(NTFS_82U(NTFS_U28(ustr[i])))) -
680 ((int)NTFS_TOUPPER(NTFS_82U(astr[i])));
684 return (ustrlen - astrlen);
688 * Compare unicode and ascii string case sens.
691 ntfs_uastrcmp(ntmp, ustr, ustrlen, astr, astrlen)
692 struct ntfsmount *ntmp;
701 for (i = 0; (i < ustrlen) && (i < astrlen); i++) {
702 res = (int) (((char)NTFS_U28(ustr[i])) - astr[i]);
706 return (ustrlen - astrlen);
710 * Search fnode in ntnode, if not found allocate and preinitialize.
712 * ntnode should be locked on entry.
716 struct ntfsmount *ntmp,
724 dprintf(("ntfs_fget: ino: %d, attrtype: 0x%x, attrname: %s\n",
725 ip->i_number,attrtype, attrname?attrname:""));
727 for (fp = ip->i_fnlist.lh_first; fp != NULL; fp = fp->f_fnlist.le_next){
728 dprintf(("ntfs_fget: fnode: attrtype: %d, attrname: %s\n",
729 fp->f_attrtype, fp->f_attrname?fp->f_attrname:""));
731 if ((attrtype == fp->f_attrtype) &&
732 ((!attrname && !fp->f_attrname) ||
733 (attrname && fp->f_attrname &&
734 !strcmp(attrname,fp->f_attrname)))){
735 dprintf(("ntfs_fget: found existed: %p\n",fp));
743 MALLOC(fp, struct fnode *, sizeof(struct fnode), M_NTFSFNODE, M_WAITOK);
744 bzero(fp, sizeof(struct fnode));
745 dprintf(("ntfs_fget: allocating fnode: %p\n",fp));
749 fp->f_flag |= FN_AATTRNAME;
750 MALLOC(fp->f_attrname, char *, strlen(attrname)+1, M_TEMP, M_WAITOK);
751 strcpy(fp->f_attrname, attrname);
753 fp->f_attrname = NULL;
754 fp->f_attrtype = attrtype;
758 LIST_INSERT_HEAD(&ip->i_fnlist, fp, f_fnlist);
766 * Deallocate fnode, remove it from ntnode's fnode list.
768 * ntnode should be locked.
774 struct ntnode *ip = FTONT(fp);
776 dprintf(("ntfs_frele: fnode: %p for %d: %p\n", fp, ip->i_number, ip));
778 dprintf(("ntfs_frele: deallocating fnode\n"));
779 LIST_REMOVE(fp,f_fnlist);
780 if (fp->f_flag & FN_AATTRNAME)
781 FREE(fp->f_attrname, M_TEMP);
783 FREE(fp->f_dirblbuf, M_NTFSDIR);
784 FREE(fp, M_NTFSFNODE);
789 * Lookup attribute name in format: [[:$ATTR_TYPE]:$ATTR_NAME],
790 * $ATTR_TYPE is searched in attrdefs read from $AttrDefs.
791 * If $ATTR_TYPE nott specifed, ATTR_A_DATA assumed.
795 struct ntfsmount * ntmp,
803 struct ntvattrdef *adp;
808 if (name[0] == '$') {
810 for (syslen = 0; syslen < namelen; syslen++) {
811 if(sys[syslen] == ':') {
821 for (i = 0; i < ntmp->ntm_adnum; i++, adp++){
822 if (syslen != adp->ad_namelen ||
823 strncmp(sys, adp->ad_name, syslen) != 0)
826 *attrtype = adp->ad_type;
831 *attrtype = NTFS_A_DATA;
835 MALLOC((*attrname), char *, namelen, M_TEMP, M_WAITOK);
836 memcpy((*attrname), name, namelen);
837 (*attrname)[namelen] = '\0';
844 * Lookup specifed node for filename, matching cnp,
845 * return fnode filled.
849 struct ntfsmount * ntmp,
851 struct componentname * cnp,
854 struct fnode *fp = VTOF(vp);
855 struct ntnode *ip = FTONT(fp);
856 struct ntvattr *vap; /* Root attribute */
857 cn_t cn; /* VCN in current attribute */
858 caddr_t rdbuf; /* Buffer to read directory's blocks */
860 u_int32_t rdsize; /* Length of data to read from current block */
861 struct attr_indexentry *iep;
862 int error, res, anamelen, fnamelen;
863 const char *fname,*aname;
865 int attrtype = NTFS_A_DATA;
866 char *attrname = NULL;
871 error = ntfs_ntget(ip);
875 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
876 if (error || (vap->va_flag & NTFS_AF_INRUN))
879 blsize = vap->va_a_iroot->ir_size;
880 rdsize = vap->va_datalen;
883 * Divide file name into: foofilefoofilefoofile[:attrspec]
884 * Store like this: fname:fnamelen [aname:anamelen]
886 fname = cnp->cn_nameptr;
889 for (fnamelen = 0; fnamelen < cnp->cn_namelen; fnamelen++)
890 if(fname[fnamelen] == ':') {
891 aname = fname + fnamelen + 1;
892 anamelen = cnp->cn_namelen - fnamelen - 1;
893 dprintf(("ntfs_ntlookupfile: %s (%d), attr: %s (%d)\n",
894 fname, fnamelen, aname, anamelen));
898 dprintf(("ntfs_ntlookupfile: blksz: %d, rdsz: %d\n", blsize, rdsize));
900 MALLOC(rdbuf, caddr_t, blsize, M_TEMP, M_WAITOK);
902 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXROOT, "$I30",
903 0, rdsize, rdbuf, NULL);
907 aoff = sizeof(struct attr_indexroot);
910 iep = (struct attr_indexentry *) (rdbuf + aoff);
912 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
914 iep = (struct attr_indexentry *) (rdbuf + aoff))
916 ddprintf(("scan: %d, %d\n",
917 (u_int32_t) iep->ie_number,
918 (u_int32_t) iep->ie_fnametype));
920 /* check the name - the case-insensitible check
921 * has to come first, to break from this for loop
922 * if needed, so we can dive correctly */
923 res = NTFS_UASTRICMP(iep->ie_fname, iep->ie_fnamelen,
926 if (res < 0) continue;
928 if (iep->ie_fnametype == 0 ||
929 !(ntmp->ntm_flag & NTFS_MFLAG_CASEINS))
931 res = NTFS_UASTRCMP(iep->ie_fname,
932 iep->ie_fnamelen, fname, fnamelen);
933 if (res != 0) continue;
937 error = ntfs_ntlookupattr(ntmp,
939 &attrtype, &attrname);
944 /* Check if we've found ourself */
945 if ((iep->ie_number == ip->i_number) &&
946 (attrtype == fp->f_attrtype) &&
947 ((!attrname && !fp->f_attrname) ||
948 (attrname && fp->f_attrname &&
949 !strcmp(attrname, fp->f_attrname))))
957 /* vget node, but don't load it */
958 error = ntfs_vgetex(ntmp->ntm_mountp,
959 iep->ie_number, attrtype, attrname,
960 LK_EXCLUSIVE, VG_DONTLOADIN | VG_DONTVALIDFN,
963 /* free the buffer returned by ntfs_ntlookupattr() */
965 FREE(attrname, M_TEMP);
974 if (nfp->f_flag & FN_VALID) {
979 nfp->f_fflag = iep->ie_fflag;
980 nfp->f_pnumber = iep->ie_fpnumber;
981 nfp->f_times = iep->ie_ftimes;
983 if((nfp->f_fflag & NTFS_FFLAG_DIR) &&
984 (nfp->f_attrtype == NTFS_A_DATA) &&
985 (nfp->f_attrname == NULL))
990 nvp->v_type = f_type;
992 if ((nfp->f_attrtype == NTFS_A_DATA) &&
993 (nfp->f_attrname == NULL))
995 /* Opening default attribute */
996 nfp->f_size = iep->ie_fsize;
997 nfp->f_allocated = iep->ie_fallocated;
998 nfp->f_flag |= FN_PRELOADED;
1000 error = ntfs_filesize(ntmp, nfp,
1001 &nfp->f_size, &nfp->f_allocated);
1008 nfp->f_flag &= ~FN_VALID;
1013 /* Dive if possible */
1014 if (iep->ie_flag & NTFS_IEFLAG_SUBNODE) {
1015 dprintf(("ntfs_ntlookupfile: diving\n"));
1017 cn = *(cn_t *) (rdbuf + aoff +
1018 iep->reclen - sizeof(cn_t));
1021 error = ntfs_readattr(ntmp, ip, NTFS_A_INDX, "$I30",
1022 ntfs_cntob(cn), rdsize, rdbuf, NULL);
1026 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1031 aoff = (((struct attr_indexalloc *) rdbuf)->ia_hdrsize +
1034 dprintf(("ntfs_ntlookupfile: nowhere to dive :-(\n"));
1040 dprintf(("finish\n"));
1043 if (attrname) FREE(attrname, M_TEMP);
1044 ntfs_ntvattrrele(vap);
1046 FREE(rdbuf, M_TEMP);
1051 * Check if name type is permitted to show.
1054 ntfs_isnamepermitted(
1055 struct ntfsmount * ntmp,
1056 struct attr_indexentry * iep)
1058 if (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)
1061 switch (iep->ie_fnametype) {
1063 ddprintf(("ntfs_isnamepermitted: skiped DOS name\n"));
1065 case 0: case 1: case 3:
1068 printf("ntfs_isnamepermitted: " \
1069 "WARNING! Unknown file name type: %d\n",
1077 * Read ntfs dir like stream of attr_indexentry, not like btree of them.
1078 * This is done by scaning $BITMAP:$I30 for busy clusters and reading them.
1079 * Ofcouse $INDEX_ROOT:$I30 is read before. Last read values are stored in
1080 * fnode, so we can skip toward record number num almost immediatly.
1081 * Anyway this is rather slow routine. The problem is that we don't know
1082 * how many records are there in $INDEX_ALLOCATION:$I30 block.
1086 struct ntfsmount * ntmp,
1089 struct attr_indexentry ** riepp)
1091 struct ntnode *ip = FTONT(fp);
1092 struct ntvattr *vap = NULL; /* IndexRoot attribute */
1093 struct ntvattr *bmvap = NULL; /* BitMap attribute */
1094 struct ntvattr *iavap = NULL; /* IndexAllocation attribute */
1095 caddr_t rdbuf; /* Buffer to read directory's blocks */
1096 u_char *bmp = NULL; /* Bitmap */
1097 u_int32_t blsize; /* Index allocation size (2048) */
1098 u_int32_t rdsize; /* Length of data to read */
1099 u_int32_t attrnum; /* Current attribute type */
1100 u_int32_t cpbl = 1; /* Clusters per directory block */
1102 struct attr_indexentry *iep;
1104 u_int32_t aoff, cnum;
1106 dprintf(("ntfs_ntreaddir: read ino: %d, num: %d\n", ip->i_number, num));
1107 error = ntfs_ntget(ip);
1111 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
1115 if (fp->f_dirblbuf == NULL) {
1116 fp->f_dirblsz = vap->va_a_iroot->ir_size;
1117 MALLOC(fp->f_dirblbuf, caddr_t,
1118 max(vap->va_datalen,fp->f_dirblsz), M_NTFSDIR, M_WAITOK);
1121 blsize = fp->f_dirblsz;
1122 rdbuf = fp->f_dirblbuf;
1124 dprintf(("ntfs_ntreaddir: rdbuf: 0x%p, blsize: %d\n", rdbuf, blsize));
1126 if (vap->va_a_iroot->ir_flag & NTFS_IRFLAG_INDXALLOC) {
1127 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXBITMAP, "$I30",
1133 MALLOC(bmp, u_char *, bmvap->va_datalen, M_TEMP, M_WAITOK);
1134 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXBITMAP, "$I30", 0,
1135 bmvap->va_datalen, bmp, NULL);
1139 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDX, "$I30",
1145 cpbl = ntfs_btocn(blsize + ntfs_cntob(1) - 1);
1146 dprintf(("ntfs_ntreaddir: indexalloc: %d, cpbl: %d\n",
1147 iavap->va_datalen, cpbl));
1149 dprintf(("ntfs_ntreadidir: w/o BitMap and IndexAllocation\n"));
1150 iavap = bmvap = NULL;
1154 /* Try use previous values */
1155 if ((fp->f_lastdnum < num) && (fp->f_lastdnum != 0)) {
1156 attrnum = fp->f_lastdattr;
1157 aoff = fp->f_lastdoff;
1158 blnum = fp->f_lastdblnum;
1159 cnum = fp->f_lastdnum;
1161 attrnum = NTFS_A_INDXROOT;
1162 aoff = sizeof(struct attr_indexroot);
1168 dprintf(("ntfs_ntreaddir: scan: 0x%x, %d, %d, %d, %d\n",
1169 attrnum, (u_int32_t) blnum, cnum, num, aoff));
1170 rdsize = (attrnum == NTFS_A_INDXROOT) ? vap->va_datalen : blsize;
1171 error = ntfs_readattr(ntmp, ip, attrnum, "$I30",
1172 ntfs_cntob(blnum * cpbl), rdsize, rdbuf, NULL);
1176 if (attrnum == NTFS_A_INDX) {
1177 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1183 aoff = (attrnum == NTFS_A_INDX) ?
1184 (0x18 + ((struct attr_indexalloc *) rdbuf)->ia_hdrsize) :
1185 sizeof(struct attr_indexroot);
1187 iep = (struct attr_indexentry *) (rdbuf + aoff);
1188 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
1189 aoff += iep->reclen,
1190 iep = (struct attr_indexentry *) (rdbuf + aoff))
1192 if (!ntfs_isnamepermitted(ntmp, iep)) continue;
1195 fp->f_lastdnum = cnum;
1196 fp->f_lastdoff = aoff;
1197 fp->f_lastdblnum = blnum;
1198 fp->f_lastdattr = attrnum;
1209 if (attrnum == NTFS_A_INDXROOT)
1214 while (ntfs_cntob(blnum * cpbl) < iavap->va_datalen) {
1215 if (bmp[blnum >> 3] & (1 << (blnum & 3)))
1220 attrnum = NTFS_A_INDX;
1222 if (ntfs_cntob(blnum * cpbl) >= iavap->va_datalen)
1224 dprintf(("ntfs_ntreaddir: blnum: %d\n", (u_int32_t) blnum));
1233 ntfs_ntvattrrele(vap);
1235 ntfs_ntvattrrele(bmvap);
1237 ntfs_ntvattrrele(iavap);
1245 * Convert NTFS times that are in 100 ns units and begins from
1246 * 1601 Jan 1 into unix times.
1254 /* WindowNT times are in 100 ns and from 1601 Jan 1 */
1255 t.tv_nsec = (nt % (1000 * 1000 * 10)) * 100;
1256 t.tv_sec = nt / (1000 * 1000 * 10) -
1257 369LL * 365LL * 24LL * 60LL * 60LL -
1258 89LL * 1LL * 24LL * 60LL * 60LL;
1263 * Get file times from NTFS_A_NAME attribute.
1267 struct ntfsmount * ntmp,
1271 struct ntvattr *vap;
1274 dprintf(("ntfs_times: ino: %d...\n", ip->i_number));
1276 error = ntfs_ntget(ip);
1280 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_NAME, NULL, 0, &vap);
1285 *tm = vap->va_a_name->n_times;
1286 ntfs_ntvattrrele(vap);
1293 * Get file sizes from corresponding attribute.
1295 * ntnode under fnode should be locked.
1299 struct ntfsmount * ntmp,
1304 struct ntvattr *vap;
1305 struct ntnode *ip = FTONT(fp);
1309 dprintf(("ntfs_filesize: ino: %d\n", ip->i_number));
1311 error = ntfs_ntvattrget(ntmp, ip,
1312 fp->f_attrtype, fp->f_attrname, 0, &vap);
1316 bn = vap->va_allocated;
1317 sz = vap->va_datalen;
1319 dprintf(("ntfs_filesize: %d bytes (%d bytes allocated)\n",
1320 (u_int32_t) sz, (u_int32_t) bn));
1327 ntfs_ntvattrrele(vap);
1333 * This is one of write routine.
1336 ntfs_writeattr_plain(
1337 struct ntfsmount * ntmp,
1349 off_t off = roff, left = rsize, towrite;
1350 caddr_t data = rdata;
1351 struct ntvattr *vap;
1355 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1356 ntfs_btocn(off), &vap);
1359 towrite = min(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1360 ddprintf(("ntfs_writeattr_plain: o: %d, s: %d (%d - %d)\n",
1361 (u_int32_t) off, (u_int32_t) towrite,
1362 (u_int32_t) vap->va_vcnstart,
1363 (u_int32_t) vap->va_vcnend));
1364 error = ntfs_writentvattr_plain(ntmp, ip, vap,
1365 off - ntfs_cntob(vap->va_vcnstart),
1366 towrite, data, &init, uio);
1368 printf("ntfs_writeattr_plain: " \
1369 "ntfs_writentvattr_plain failed: o: %d, s: %d\n",
1370 (u_int32_t) off, (u_int32_t) towrite);
1371 printf("ntfs_writeattr_plain: attrib: %d - %d\n",
1372 (u_int32_t) vap->va_vcnstart,
1373 (u_int32_t) vap->va_vcnend);
1374 ntfs_ntvattrrele(vap);
1377 ntfs_ntvattrrele(vap);
1380 data = data + towrite;
1388 * This is one of write routine.
1390 * ntnode should be locked.
1393 ntfs_writentvattr_plain(
1394 struct ntfsmount * ntmp,
1396 struct ntvattr * vap,
1406 cn_t ccn, ccl, cn, left, cl;
1407 caddr_t data = rdata;
1413 if ((vap->va_flag & NTFS_AF_INRUN) == 0) {
1414 printf("ntfs_writevattr_plain: CAN'T WRITE RES. ATTRIBUTE\n");
1418 ddprintf(("ntfs_writentvattr_plain: data in run: %ld chains\n",
1426 for (; left && (cnt < vap->va_vruncnt); cnt++) {
1427 ccn = vap->va_vruncn[cnt];
1428 ccl = vap->va_vruncl[cnt];
1430 ddprintf(("ntfs_writentvattr_plain: " \
1431 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1432 (u_int32_t) left, (u_int32_t) ccn, \
1433 (u_int32_t) ccl, (u_int32_t) off));
1435 if (ntfs_cntob(ccl) < off) {
1436 off -= ntfs_cntob(ccl);
1440 if (!ccn && ip->i_number != NTFS_BOOTINO)
1443 ccl -= ntfs_btocn(off);
1444 cn = ccn + ntfs_btocn(off);
1445 off = ntfs_btocnoff(off);
1447 while (left && ccl) {
1448 #if defined(__FreeBSD__)
1450 min(ntfs_cntob(ccl) - off, MAXBSIZE - off));
1452 /* under NetBSD, bread() can read
1453 * maximum one block worth of data */
1454 tocopy = min(left, ntmp->ntm_bps - off);
1456 cl = ntfs_btocl(tocopy + off);
1457 ddprintf(("ntfs_writentvattr_plain: write: " \
1458 "cn: 0x%x cl: %d, off: %d len: %d, left: %d\n",
1459 (u_int32_t) cn, (u_int32_t) cl,
1460 (u_int32_t) off, (u_int32_t) tocopy,
1462 if ((off == 0) && (tocopy == ntfs_cntob(cl)))
1464 bp = getblk(ntmp->ntm_devvp, ntfs_cntobn(cn),
1465 ntfs_cntob(cl), 0, 0);
1468 error = bread(ntmp->ntm_devvp, ntfs_cntobn(cn),
1469 ntfs_cntob(cl), &bp);
1476 uiomove(bp->b_data + off, tocopy, uio);
1478 memcpy(bp->b_data + off, data, tocopy);
1480 data = data + tocopy;
1490 printf("ntfs_writentvattr_plain: POSSIBLE RUN ERROR\n");
1498 * This is one of read routines.
1500 * ntnode should be locked.
1503 ntfs_readntvattr_plain(
1504 struct ntfsmount * ntmp,
1506 struct ntvattr * vap,
1517 if (vap->va_flag & NTFS_AF_INRUN) {
1519 cn_t ccn, ccl, cn, left, cl;
1520 caddr_t data = rdata;
1524 ddprintf(("ntfs_readntvattr_plain: data in run: %ld chains\n",
1532 while (left && (cnt < vap->va_vruncnt)) {
1533 ccn = vap->va_vruncn[cnt];
1534 ccl = vap->va_vruncl[cnt];
1536 ddprintf(("ntfs_readntvattr_plain: " \
1537 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1538 (u_int32_t) left, (u_int32_t) ccn, \
1539 (u_int32_t) ccl, (u_int32_t) off));
1541 if (ntfs_cntob(ccl) < off) {
1542 off -= ntfs_cntob(ccl);
1546 if (ccn || ip->i_number == NTFS_BOOTINO) {
1547 ccl -= ntfs_btocn(off);
1548 cn = ccn + ntfs_btocn(off);
1549 off = ntfs_btocnoff(off);
1551 while (left && ccl) {
1552 #if defined(__FreeBSD__)
1554 min(ntfs_cntob(ccl) - off,
1557 /* under NetBSD, bread() can read
1558 * maximum one block worth of data */
1560 ntmp->ntm_bps - off);
1562 cl = ntfs_btocl(tocopy + off);
1563 ddprintf(("ntfs_readntvattr_plain: " \
1564 "read: cn: 0x%x cl: %d, " \
1565 "off: %d len: %d, left: %d\n",
1571 error = bread(ntmp->ntm_devvp,
1580 uiomove(bp->b_data + off,
1583 memcpy(data, bp->b_data + off,
1587 data = data + tocopy;
1595 tocopy = min(left, ntfs_cntob(ccl) - off);
1596 ddprintf(("ntfs_readntvattr_plain: "
1597 "hole: ccn: 0x%x ccl: %d, off: %d, " \
1598 " len: %d, left: %d\n",
1599 (u_int32_t) ccn, (u_int32_t) ccl,
1600 (u_int32_t) off, (u_int32_t) tocopy,
1605 size_t remains = tocopy;
1606 for(; remains; remains++)
1607 uiomove("", 1, uio);
1609 bzero(data, tocopy);
1610 data = data + tocopy;
1615 printf("ntfs_readntvattr_plain: POSSIBLE RUN ERROR\n");
1619 ddprintf(("ntfs_readnvattr_plain: data is in mft record\n"));
1621 uiomove(vap->va_datap + roff, rsize, uio);
1623 memcpy(rdata, vap->va_datap + roff, rsize);
1631 * This is one of read routines.
1634 ntfs_readattr_plain(
1635 struct ntfsmount * ntmp,
1647 off_t off = roff, left = rsize, toread;
1648 caddr_t data = rdata;
1649 struct ntvattr *vap;
1653 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1654 ntfs_btocn(off), &vap);
1657 toread = min(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1658 ddprintf(("ntfs_readattr_plain: o: %d, s: %d (%d - %d)\n",
1659 (u_int32_t) off, (u_int32_t) toread,
1660 (u_int32_t) vap->va_vcnstart,
1661 (u_int32_t) vap->va_vcnend));
1662 error = ntfs_readntvattr_plain(ntmp, ip, vap,
1663 off - ntfs_cntob(vap->va_vcnstart),
1664 toread, data, &init, uio);
1666 printf("ntfs_readattr_plain: " \
1667 "ntfs_readntvattr_plain failed: o: %d, s: %d\n",
1668 (u_int32_t) off, (u_int32_t) toread);
1669 printf("ntfs_readattr_plain: attrib: %d - %d\n",
1670 (u_int32_t) vap->va_vcnstart,
1671 (u_int32_t) vap->va_vcnend);
1672 ntfs_ntvattrrele(vap);
1675 ntfs_ntvattrrele(vap);
1678 data = data + toread;
1686 * This is one of read routines.
1690 struct ntfsmount * ntmp,
1700 struct ntvattr *vap;
1703 ddprintf(("ntfs_readattr: reading %d: 0x%x, from %d size %d bytes\n",
1704 ip->i_number, attrnum, (u_int32_t) roff, (u_int32_t) rsize));
1706 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname, 0, &vap);
1710 if ((roff > vap->va_datalen) ||
1711 (roff + rsize > vap->va_datalen)) {
1712 ddprintf(("ntfs_readattr: offset too big\n"));
1713 ntfs_ntvattrrele(vap);
1716 if (vap->va_compression && vap->va_compressalg) {
1719 off_t off = roff, left = rsize, tocopy;
1720 caddr_t data = rdata;
1723 ddprintf(("ntfs_ntreadattr: compression: %d\n",
1724 vap->va_compressalg));
1726 MALLOC(cup, u_int8_t *, ntfs_cntob(NTFS_COMPUNIT_CL),
1727 M_NTFSDECOMP, M_WAITOK);
1728 MALLOC(uup, u_int8_t *, ntfs_cntob(NTFS_COMPUNIT_CL),
1729 M_NTFSDECOMP, M_WAITOK);
1731 cn = (ntfs_btocn(roff)) & (~(NTFS_COMPUNIT_CL - 1));
1732 off = roff - ntfs_cntob(cn);
1735 error = ntfs_readattr_plain(ntmp, ip, attrnum,
1736 attrname, ntfs_cntob(cn),
1737 ntfs_cntob(NTFS_COMPUNIT_CL),
1742 tocopy = min(left, ntfs_cntob(NTFS_COMPUNIT_CL) - off);
1744 if (init == ntfs_cntob(NTFS_COMPUNIT_CL)) {
1746 uiomove(cup + off, tocopy, uio);
1748 memcpy(data, cup + off, tocopy);
1749 } else if (init == 0) {
1751 size_t remains = tocopy;
1752 for(; remains; remains--)
1753 uiomove("", 1, uio);
1756 bzero(data, tocopy);
1758 error = ntfs_uncompunit(ntmp, uup, cup);
1762 uiomove(uup + off, tocopy, uio);
1764 memcpy(data, uup + off, tocopy);
1768 data = data + tocopy;
1769 off += tocopy - ntfs_cntob(NTFS_COMPUNIT_CL);
1770 cn += NTFS_COMPUNIT_CL;
1773 FREE(uup, M_NTFSDECOMP);
1774 FREE(cup, M_NTFSDECOMP);
1776 error = ntfs_readattr_plain(ntmp, ip, attrnum, attrname,
1777 roff, rsize, rdata, &init, uio);
1778 ntfs_ntvattrrele(vap);
1795 printf("ntfs_parsetun: run == NULL\n");
1800 printf("ntfs_parserun: trying to go out of run\n");
1804 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1805 printf("ntfs_parserun: " \
1806 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1810 for (i = 0; i < (sz & 0xF); i++)
1811 *cl += (u_int32_t) run[(*off)++] << (i << 3);
1814 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1815 printf("ntfs_parserun: " \
1816 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1820 for (i = 0; i < (sz & 0xF); i++)
1821 *cn += (u_int32_t) run[(*off)++] << (i << 3);
1828 * Process fixup routine on given buffer.
1832 struct ntfsmount * ntmp,
1837 struct fixuphdr *fhp = (struct fixuphdr *) buf;
1843 if (fhp->fh_magic != magic) {
1844 printf("ntfs_procfixups: magic doesn't match: %08x != %08x\n",
1845 fhp->fh_magic, magic);
1848 if ((fhp->fh_fnum - 1) * ntmp->ntm_bps != len) {
1849 printf("ntfs_procfixups: " \
1850 "bad fixups number: %d for %ld bytes block\n",
1851 fhp->fh_fnum, (long)len); /* XXX printf kludge */
1854 if (fhp->fh_foff >= ntmp->ntm_spc * ntmp->ntm_mftrecsz * ntmp->ntm_bps) {
1855 printf("ntfs_procfixups: invalid offset: %x", fhp->fh_foff);
1858 fxp = (u_int16_t *) (buf + fhp->fh_foff);
1859 cfxp = (u_int16_t *) (buf + ntmp->ntm_bps - 2);
1861 for (i = 1; i < fhp->fh_fnum; i++, fxp++) {
1862 if (*cfxp != fixup) {
1863 printf("ntfs_procfixups: fixup %d doesn't match\n", i);
1867 ((caddr_t) cfxp) += ntmp->ntm_bps;
1876 struct ntfsmount * ntmp,
1888 printf("ntfs_runtocn: run: 0x%p, %ld bytes, vcn:%ld\n",
1889 run, len, (u_long) vcn);
1890 printf("ntfs_runtocn: run: ");
1891 for (i = 0; i < len; i++)
1892 printf("0x%02x ", run[i]);
1897 printf("ntfs_runtocn: run == NULL\n");
1901 if (run[off] == 0) {
1902 printf("ntfs_runtocn: vcn too big\n");
1906 error = ntfs_parserun(&ccn, &ccl, run, len, &off);
1908 printf("ntfs_runtocn: ntfs_parserun failed\n");
1911 } while (ccl <= vcn);
1918 * this initializes toupper table & dependant variables to be ready for
1924 ntfs_toupper_tab = (wchar *) NULL;
1925 lockinit(&ntfs_toupper_lock, PVFS, "ntfs_toupper", 0, 0);
1926 ntfs_toupper_usecount = 0;
1930 * if the ntfs_toupper_tab[] is filled already, just raise use count;
1931 * otherwise read the data from the filesystem we are currently mounting
1934 ntfs_toupper_use(mp, ntmp)
1936 struct ntfsmount *ntmp;
1941 /* get exclusive access */
1942 LOCKMGR(&ntfs_toupper_lock, LK_EXCLUSIVE, NULL);
1944 /* only read the translation data from a file if it hasn't been
1946 if (ntfs_toupper_tab)
1950 * Read in Unicode lowercase -> uppercase translation file.
1951 * XXX for now, just the first 256 entries are used anyway,
1952 * so don't bother reading more
1954 MALLOC(ntfs_toupper_tab, wchar *, 65536 * sizeof(wchar),
1955 M_NTFSRDATA, M_WAITOK);
1957 if ((error = VFS_VGET(mp, NTFS_UPCASEINO, &vp)))
1959 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
1960 0, 65536*sizeof(wchar), (char *) ntfs_toupper_tab, NULL);
1964 ntfs_toupper_usecount++;
1965 LOCKMGR(&ntfs_toupper_lock, LK_RELEASE, NULL);
1970 * lower the use count and if it reaches zero, free the memory
1971 * tied by toupper table
1974 ntfs_toupper_unuse()
1976 /* get exclusive access */
1977 LOCKMGR(&ntfs_toupper_lock, LK_EXCLUSIVE, NULL);
1979 ntfs_toupper_usecount--;
1980 if (ntfs_toupper_usecount == 0) {
1981 FREE(ntfs_toupper_tab, M_NTFSRDATA);
1982 ntfs_toupper_tab = NULL;
1985 else if (ntfs_toupper_usecount < 0) {
1986 panic("ntfs_toupper_unuse(): use count negative: %d\n",
1987 ntfs_toupper_usecount);
1991 /* release the lock */
1992 LOCKMGR(&ntfs_toupper_lock, LK_RELEASE, NULL);
1997 struct ntfsmount *ntmp,
2003 MALLOC(u28, char **, 256 * sizeof(char*), M_TEMP, M_WAITOK | M_ZERO);
2005 for (i=0; i<256; i++) {
2006 h = (u2w[i] >> 8) & 0xFF;
2009 if (u28[h] == NULL) {
2010 MALLOC(u28[h], char *, 256 * sizeof(char), M_TEMP, M_WAITOK);
2011 for (j=0; j<256; j++)
2015 u28[h][l] = i & 0xFF;
2018 ntmp->ntm_u28 = u28;
2024 ntfs_u28_uninit(struct ntfsmount *ntmp)
2029 if (ntmp->ntm_u28 == NULL)
2032 u28 = ntmp->ntm_u28;
2034 for (i=0; i<256; i++)
2036 FREE(u28[i], M_TEMP);
2045 struct ntfsmount *ntmp,
2051 MALLOC(_82u, wchar *, 256 * sizeof(wchar), M_TEMP, M_WAITOK);
2054 for (i=0; i<256; i++)
2057 for (i=0; i<128; i++)
2059 for (i=0; i<128; i++)
2060 _82u[i+128] = u2w[i];
2063 ntmp->ntm_82u = _82u;
2069 ntfs_82u_uninit(struct ntfsmount *ntmp)
2071 FREE(ntmp->ntm_82u, M_TEMP);
2076 * maps the Unicode char to 8bit equivalent
2077 * XXX currently only gets lower 8bit from the Unicode char
2078 * and substitutes a '_' for it if the result would be '\0';
2079 * something better has to be definitely though out
2083 struct ntfsmount *ntmp,
2088 p = ntmp->ntm_u28[(wc>>8)&0xFF];
2091 return (p[wc&0xFF]);