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.11 2004/03/01 06:33:22 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(__DragonFly__)
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
361 dprintf(("ntfs_ntget: get ntnode %d: %p, usecount: %d\n",
362 ip->i_number, ip, ip->i_usecount));
364 ip->i_usecount++; /* ZZZ */
365 lwkt_gettoken(&ilock, &ip->i_interlock);
366 LOCKMGR(&ip->i_lock, LK_EXCLUSIVE | LK_INTERLOCK, &ilock);
372 * Routine search ntnode in hash, if found: lock, inc usecount and return.
373 * If not in hash allocate structure for ntnode, prefill it, lock,
374 * inc count and return.
376 * ntnode returned locked
380 struct ntfsmount * ntmp,
382 struct ntnode ** ipp)
386 dprintf(("ntfs_ntlookup: looking for ntnode %d\n", ino));
389 if ((ip = ntfs_nthashlookup(ntmp->ntm_dev, ino)) != NULL) {
391 dprintf(("ntfs_ntlookup: ntnode %d: %p, usecount: %d\n",
392 ino, ip, ip->i_usecount));
396 } while (LOCKMGR(&ntfs_hashlock, LK_EXCLUSIVE | LK_SLEEPFAIL, NULL));
398 MALLOC(ip, struct ntnode *, sizeof(struct ntnode),
399 M_NTFSNTNODE, M_WAITOK);
400 ddprintf(("ntfs_ntlookup: allocating ntnode: %d: %p\n", ino, ip));
401 bzero((caddr_t) ip, sizeof(struct ntnode));
403 /* Generic initialization */
404 ip->i_devvp = ntmp->ntm_devvp;
405 ip->i_dev = ntmp->ntm_dev;
409 LIST_INIT(&ip->i_fnlist);
412 /* init lock and lock the newborn ntnode */
413 lockinit(&ip->i_lock, 0, "ntnode", 0, LK_EXCLUSIVE);
414 lwkt_token_init(&ip->i_interlock);
419 LOCKMGR(&ntfs_hashlock, LK_RELEASE, NULL);
423 dprintf(("ntfs_ntlookup: ntnode %d: %p, usecount: %d\n",
424 ino, ip, ip->i_usecount));
430 * Decrement usecount of ntnode and unlock it, if usecount reach zero,
433 * ntnode should be locked on entry, and unlocked on return.
442 dprintf(("ntfs_ntput: rele ntnode %d: %p, usecount: %d\n",
443 ip->i_number, ip, ip->i_usecount));
445 lwkt_gettoken(&ilock, &ip->i_interlock);
449 if (ip->i_usecount < 0) {
450 panic("ntfs_ntput: ino: %d usecount: %d \n",
451 ip->i_number,ip->i_usecount);
455 if (ip->i_usecount > 0) {
456 LOCKMGR(&ip->i_lock, LK_RELEASE|LK_INTERLOCK, &ilock);
460 dprintf(("ntfs_ntput: deallocating ntnode: %d\n", ip->i_number));
462 if (ip->i_fnlist.lh_first)
463 panic("ntfs_ntput: ntnode has fnodes\n");
467 while ((vap = LIST_FIRST(&ip->i_valist)) != NULL) {
468 LIST_REMOVE(vap,va_list);
469 ntfs_freentvattr(vap);
471 lwkt_reltoken(&ilock);
473 FREE(ip, M_NTFSNTNODE);
477 * increment usecount of ntnode
485 dprintf(("ntfs_ntref: ino %d, usecount: %d\n",
486 ip->i_number, ip->i_usecount));
491 * Decrement usecount of ntnode.
499 dprintf(("ntfs_ntrele: rele ntnode %d: %p, usecount: %d\n",
500 ip->i_number, ip, ip->i_usecount));
502 lwkt_gettoken(&ilock, &ip->i_interlock);
505 if (ip->i_usecount < 0) {
506 panic("ntfs_ntrele: ino: %d usecount: %d \n",
507 ip->i_number,ip->i_usecount);
509 lwkt_reltoken(&ilock);
513 * Deallocate all memory allocated for ntvattr
516 ntfs_freentvattr(vap)
517 struct ntvattr * vap;
519 if (vap->va_flag & NTFS_AF_INRUN) {
521 FREE(vap->va_vruncn, M_NTFSRUN);
523 FREE(vap->va_vruncl, M_NTFSRUN);
526 FREE(vap->va_datap, M_NTFSRDATA);
528 FREE(vap, M_NTFSNTVATTR);
532 * Convert disk image of attribute into ntvattr structure,
533 * runs are expanded also.
537 struct ntfsmount * ntmp,
538 struct ntvattr ** rvapp,
547 MALLOC(vap, struct ntvattr *, sizeof(struct ntvattr),
548 M_NTFSNTVATTR, M_WAITOK);
549 bzero(vap, sizeof(struct ntvattr));
551 vap->va_flag = rap->a_hdr.a_flag;
552 vap->va_type = rap->a_hdr.a_type;
553 vap->va_compression = rap->a_hdr.a_compression;
554 vap->va_index = rap->a_hdr.a_index;
556 ddprintf(("type: 0x%x, index: %d", vap->va_type, vap->va_index));
558 vap->va_namelen = rap->a_hdr.a_namelen;
559 if (rap->a_hdr.a_namelen) {
560 wchar *unp = (wchar *) ((caddr_t) rap + rap->a_hdr.a_nameoff);
561 ddprintf((", name:["));
562 for (i = 0; i < vap->va_namelen; i++) {
563 vap->va_name[i] = unp[i];
564 ddprintf(("%c", vap->va_name[i]));
568 if (vap->va_flag & NTFS_AF_INRUN) {
569 ddprintf((", nonres."));
570 vap->va_datalen = rap->a_nr.a_datalen;
571 vap->va_allocated = rap->a_nr.a_allocated;
572 vap->va_vcnstart = rap->a_nr.a_vcnstart;
573 vap->va_vcnend = rap->a_nr.a_vcnend;
574 vap->va_compressalg = rap->a_nr.a_compressalg;
575 error = ntfs_runtovrun(&(vap->va_vruncn), &(vap->va_vruncl),
577 (caddr_t) rap + rap->a_nr.a_dataoff);
579 vap->va_compressalg = 0;
580 ddprintf((", res."));
581 vap->va_datalen = rap->a_r.a_datalen;
582 vap->va_allocated = rap->a_r.a_datalen;
583 vap->va_vcnstart = 0;
584 vap->va_vcnend = ntfs_btocn(vap->va_allocated);
585 MALLOC(vap->va_datap, caddr_t, vap->va_datalen,
586 M_NTFSRDATA, M_WAITOK);
587 memcpy(vap->va_datap, (caddr_t) rap + rap->a_r.a_dataoff,
590 ddprintf((", len: %d", vap->va_datalen));
593 FREE(vap, M_NTFSNTVATTR);
603 * Expand run into more utilizable and more memory eating format.
624 off += (run[off] & 0xF) + ((run[off] >> 4) & 0xF) + 1;
627 MALLOC(cn, cn_t *, cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
628 MALLOC(cl, cn_t *, cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
638 for (i = 0; i < (sz & 0xF); i++)
639 cl[cnt] += (u_int32_t) run[off++] << (i << 3);
642 if (run[off + sz - 1] & 0x80) {
643 tmp = ((u_int64_t) - 1) << (sz << 3);
644 for (i = 0; i < sz; i++)
645 tmp |= (u_int64_t) run[off++] << (i << 3);
648 for (i = 0; i < sz; i++)
649 tmp |= (u_int64_t) run[off++] << (i << 3);
652 prev = cn[cnt] = prev + tmp;
665 * Compare unicode and ascii string case insens.
668 ntfs_uastricmp(ntmp, ustr, ustrlen, astr, astrlen)
669 struct ntfsmount *ntmp;
679 * XXX We use NTFS_82U(NTFS_U28(c)) to get rid of unicode
680 * symbols not covered by translation table
682 for (i = 0; i < ustrlen && i < astrlen; i++) {
683 res = ((int) NTFS_TOUPPER(NTFS_82U(NTFS_U28(ustr[i])))) -
684 ((int)NTFS_TOUPPER(NTFS_82U(astr[i])));
688 return (ustrlen - astrlen);
692 * Compare unicode and ascii string case sens.
695 ntfs_uastrcmp(ntmp, ustr, ustrlen, astr, astrlen)
696 struct ntfsmount *ntmp;
705 for (i = 0; (i < ustrlen) && (i < astrlen); i++) {
706 res = (int) (((char)NTFS_U28(ustr[i])) - astr[i]);
710 return (ustrlen - astrlen);
714 * Search fnode in ntnode, if not found allocate and preinitialize.
716 * ntnode should be locked on entry.
720 struct ntfsmount *ntmp,
728 dprintf(("ntfs_fget: ino: %d, attrtype: 0x%x, attrname: %s\n",
729 ip->i_number,attrtype, attrname?attrname:""));
731 for (fp = ip->i_fnlist.lh_first; fp != NULL; fp = fp->f_fnlist.le_next){
732 dprintf(("ntfs_fget: fnode: attrtype: %d, attrname: %s\n",
733 fp->f_attrtype, fp->f_attrname?fp->f_attrname:""));
735 if ((attrtype == fp->f_attrtype) &&
736 ((!attrname && !fp->f_attrname) ||
737 (attrname && fp->f_attrname &&
738 !strcmp(attrname,fp->f_attrname)))){
739 dprintf(("ntfs_fget: found existed: %p\n",fp));
747 MALLOC(fp, struct fnode *, sizeof(struct fnode), M_NTFSFNODE, M_WAITOK);
748 bzero(fp, sizeof(struct fnode));
749 dprintf(("ntfs_fget: allocating fnode: %p\n",fp));
753 fp->f_flag |= FN_AATTRNAME;
754 MALLOC(fp->f_attrname, char *, strlen(attrname)+1, M_TEMP, M_WAITOK);
755 strcpy(fp->f_attrname, attrname);
757 fp->f_attrname = NULL;
758 fp->f_attrtype = attrtype;
762 LIST_INSERT_HEAD(&ip->i_fnlist, fp, f_fnlist);
770 * Deallocate fnode, remove it from ntnode's fnode list.
772 * ntnode should be locked.
778 struct ntnode *ip = FTONT(fp);
780 dprintf(("ntfs_frele: fnode: %p for %d: %p\n", fp, ip->i_number, ip));
782 dprintf(("ntfs_frele: deallocating fnode\n"));
783 LIST_REMOVE(fp,f_fnlist);
784 if (fp->f_flag & FN_AATTRNAME)
785 FREE(fp->f_attrname, M_TEMP);
787 FREE(fp->f_dirblbuf, M_NTFSDIR);
788 FREE(fp, M_NTFSFNODE);
793 * Lookup attribute name in format: [[:$ATTR_TYPE]:$ATTR_NAME],
794 * $ATTR_TYPE is searched in attrdefs read from $AttrDefs.
795 * If $ATTR_TYPE nott specifed, ATTR_A_DATA assumed.
799 struct ntfsmount * ntmp,
807 struct ntvattrdef *adp;
812 if (name[0] == '$') {
814 for (syslen = 0; syslen < namelen; syslen++) {
815 if(sys[syslen] == ':') {
825 for (i = 0; i < ntmp->ntm_adnum; i++, adp++){
826 if (syslen != adp->ad_namelen ||
827 strncmp(sys, adp->ad_name, syslen) != 0)
830 *attrtype = adp->ad_type;
835 *attrtype = NTFS_A_DATA;
839 MALLOC((*attrname), char *, namelen, M_TEMP, M_WAITOK);
840 memcpy((*attrname), name, namelen);
841 (*attrname)[namelen] = '\0';
848 * Lookup specifed node for filename, matching cnp,
849 * return fnode filled.
853 struct ntfsmount * ntmp,
855 struct componentname * cnp,
858 struct fnode *fp = VTOF(vp);
859 struct ntnode *ip = FTONT(fp);
860 struct ntvattr *vap; /* Root attribute */
861 cn_t cn; /* VCN in current attribute */
862 caddr_t rdbuf; /* Buffer to read directory's blocks */
864 u_int32_t rdsize; /* Length of data to read from current block */
865 struct attr_indexentry *iep;
866 int error, res, anamelen, fnamelen;
867 const char *fname,*aname;
869 int attrtype = NTFS_A_DATA;
870 char *attrname = NULL;
875 error = ntfs_ntget(ip);
879 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
880 if (error || (vap->va_flag & NTFS_AF_INRUN))
883 blsize = vap->va_a_iroot->ir_size;
884 rdsize = vap->va_datalen;
887 * Divide file name into: foofilefoofilefoofile[:attrspec]
888 * Store like this: fname:fnamelen [aname:anamelen]
890 fname = cnp->cn_nameptr;
893 for (fnamelen = 0; fnamelen < cnp->cn_namelen; fnamelen++)
894 if(fname[fnamelen] == ':') {
895 aname = fname + fnamelen + 1;
896 anamelen = cnp->cn_namelen - fnamelen - 1;
897 dprintf(("ntfs_ntlookupfile: %s (%d), attr: %s (%d)\n",
898 fname, fnamelen, aname, anamelen));
902 dprintf(("ntfs_ntlookupfile: blksz: %d, rdsz: %d\n", blsize, rdsize));
904 MALLOC(rdbuf, caddr_t, blsize, M_TEMP, M_WAITOK);
906 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXROOT, "$I30",
907 0, rdsize, rdbuf, NULL);
911 aoff = sizeof(struct attr_indexroot);
914 iep = (struct attr_indexentry *) (rdbuf + aoff);
916 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
918 iep = (struct attr_indexentry *) (rdbuf + aoff))
920 ddprintf(("scan: %d, %d\n",
921 (u_int32_t) iep->ie_number,
922 (u_int32_t) iep->ie_fnametype));
924 /* check the name - the case-insensitible check
925 * has to come first, to break from this for loop
926 * if needed, so we can dive correctly */
927 res = NTFS_UASTRICMP(iep->ie_fname, iep->ie_fnamelen,
930 if (res < 0) continue;
932 if (iep->ie_fnametype == 0 ||
933 !(ntmp->ntm_flag & NTFS_MFLAG_CASEINS))
935 res = NTFS_UASTRCMP(iep->ie_fname,
936 iep->ie_fnamelen, fname, fnamelen);
937 if (res != 0) continue;
941 error = ntfs_ntlookupattr(ntmp,
943 &attrtype, &attrname);
948 /* Check if we've found ourself */
949 if ((iep->ie_number == ip->i_number) &&
950 (attrtype == fp->f_attrtype) &&
951 ((!attrname && !fp->f_attrname) ||
952 (attrname && fp->f_attrname &&
953 !strcmp(attrname, fp->f_attrname))))
961 /* vget node, but don't load it */
962 error = ntfs_vgetex(ntmp->ntm_mountp,
963 iep->ie_number, attrtype, attrname,
964 LK_EXCLUSIVE, VG_DONTLOADIN | VG_DONTVALIDFN,
967 /* free the buffer returned by ntfs_ntlookupattr() */
969 FREE(attrname, M_TEMP);
978 if (nfp->f_flag & FN_VALID) {
983 nfp->f_fflag = iep->ie_fflag;
984 nfp->f_pnumber = iep->ie_fpnumber;
985 nfp->f_times = iep->ie_ftimes;
987 if((nfp->f_fflag & NTFS_FFLAG_DIR) &&
988 (nfp->f_attrtype == NTFS_A_DATA) &&
989 (nfp->f_attrname == NULL))
994 nvp->v_type = f_type;
996 if ((nfp->f_attrtype == NTFS_A_DATA) &&
997 (nfp->f_attrname == NULL))
999 /* Opening default attribute */
1000 nfp->f_size = iep->ie_fsize;
1001 nfp->f_allocated = iep->ie_fallocated;
1002 nfp->f_flag |= FN_PRELOADED;
1004 error = ntfs_filesize(ntmp, nfp,
1005 &nfp->f_size, &nfp->f_allocated);
1012 nfp->f_flag &= ~FN_VALID;
1017 /* Dive if possible */
1018 if (iep->ie_flag & NTFS_IEFLAG_SUBNODE) {
1019 dprintf(("ntfs_ntlookupfile: diving\n"));
1021 cn = *(cn_t *) (rdbuf + aoff +
1022 iep->reclen - sizeof(cn_t));
1025 error = ntfs_readattr(ntmp, ip, NTFS_A_INDX, "$I30",
1026 ntfs_cntob(cn), rdsize, rdbuf, NULL);
1030 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1035 aoff = (((struct attr_indexalloc *) rdbuf)->ia_hdrsize +
1038 dprintf(("ntfs_ntlookupfile: nowhere to dive :-(\n"));
1044 dprintf(("finish\n"));
1047 if (attrname) FREE(attrname, M_TEMP);
1048 ntfs_ntvattrrele(vap);
1050 FREE(rdbuf, M_TEMP);
1055 * Check if name type is permitted to show.
1058 ntfs_isnamepermitted(
1059 struct ntfsmount * ntmp,
1060 struct attr_indexentry * iep)
1062 if (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)
1065 switch (iep->ie_fnametype) {
1067 ddprintf(("ntfs_isnamepermitted: skiped DOS name\n"));
1069 case 0: case 1: case 3:
1072 printf("ntfs_isnamepermitted: " \
1073 "WARNING! Unknown file name type: %d\n",
1081 * Read ntfs dir like stream of attr_indexentry, not like btree of them.
1082 * This is done by scaning $BITMAP:$I30 for busy clusters and reading them.
1083 * Ofcouse $INDEX_ROOT:$I30 is read before. Last read values are stored in
1084 * fnode, so we can skip toward record number num almost immediatly.
1085 * Anyway this is rather slow routine. The problem is that we don't know
1086 * how many records are there in $INDEX_ALLOCATION:$I30 block.
1090 struct ntfsmount * ntmp,
1093 struct attr_indexentry ** riepp)
1095 struct ntnode *ip = FTONT(fp);
1096 struct ntvattr *vap = NULL; /* IndexRoot attribute */
1097 struct ntvattr *bmvap = NULL; /* BitMap attribute */
1098 struct ntvattr *iavap = NULL; /* IndexAllocation attribute */
1099 caddr_t rdbuf; /* Buffer to read directory's blocks */
1100 u_char *bmp = NULL; /* Bitmap */
1101 u_int32_t blsize; /* Index allocation size (2048) */
1102 u_int32_t rdsize; /* Length of data to read */
1103 u_int32_t attrnum; /* Current attribute type */
1104 u_int32_t cpbl = 1; /* Clusters per directory block */
1106 struct attr_indexentry *iep;
1108 u_int32_t aoff, cnum;
1110 dprintf(("ntfs_ntreaddir: read ino: %d, num: %d\n", ip->i_number, num));
1111 error = ntfs_ntget(ip);
1115 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
1119 if (fp->f_dirblbuf == NULL) {
1120 fp->f_dirblsz = vap->va_a_iroot->ir_size;
1121 MALLOC(fp->f_dirblbuf, caddr_t,
1122 max(vap->va_datalen,fp->f_dirblsz), M_NTFSDIR, M_WAITOK);
1125 blsize = fp->f_dirblsz;
1126 rdbuf = fp->f_dirblbuf;
1128 dprintf(("ntfs_ntreaddir: rdbuf: 0x%p, blsize: %d\n", rdbuf, blsize));
1130 if (vap->va_a_iroot->ir_flag & NTFS_IRFLAG_INDXALLOC) {
1131 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXBITMAP, "$I30",
1137 MALLOC(bmp, u_char *, bmvap->va_datalen, M_TEMP, M_WAITOK);
1138 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXBITMAP, "$I30", 0,
1139 bmvap->va_datalen, bmp, NULL);
1143 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDX, "$I30",
1149 cpbl = ntfs_btocn(blsize + ntfs_cntob(1) - 1);
1150 dprintf(("ntfs_ntreaddir: indexalloc: %d, cpbl: %d\n",
1151 iavap->va_datalen, cpbl));
1153 dprintf(("ntfs_ntreadidir: w/o BitMap and IndexAllocation\n"));
1154 iavap = bmvap = NULL;
1158 /* Try use previous values */
1159 if ((fp->f_lastdnum < num) && (fp->f_lastdnum != 0)) {
1160 attrnum = fp->f_lastdattr;
1161 aoff = fp->f_lastdoff;
1162 blnum = fp->f_lastdblnum;
1163 cnum = fp->f_lastdnum;
1165 attrnum = NTFS_A_INDXROOT;
1166 aoff = sizeof(struct attr_indexroot);
1172 dprintf(("ntfs_ntreaddir: scan: 0x%x, %d, %d, %d, %d\n",
1173 attrnum, (u_int32_t) blnum, cnum, num, aoff));
1174 rdsize = (attrnum == NTFS_A_INDXROOT) ? vap->va_datalen : blsize;
1175 error = ntfs_readattr(ntmp, ip, attrnum, "$I30",
1176 ntfs_cntob(blnum * cpbl), rdsize, rdbuf, NULL);
1180 if (attrnum == NTFS_A_INDX) {
1181 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1187 aoff = (attrnum == NTFS_A_INDX) ?
1188 (0x18 + ((struct attr_indexalloc *) rdbuf)->ia_hdrsize) :
1189 sizeof(struct attr_indexroot);
1191 iep = (struct attr_indexentry *) (rdbuf + aoff);
1192 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
1193 aoff += iep->reclen,
1194 iep = (struct attr_indexentry *) (rdbuf + aoff))
1196 if (!ntfs_isnamepermitted(ntmp, iep)) continue;
1199 fp->f_lastdnum = cnum;
1200 fp->f_lastdoff = aoff;
1201 fp->f_lastdblnum = blnum;
1202 fp->f_lastdattr = attrnum;
1213 if (attrnum == NTFS_A_INDXROOT)
1218 while (ntfs_cntob(blnum * cpbl) < iavap->va_datalen) {
1219 if (bmp[blnum >> 3] & (1 << (blnum & 3)))
1224 attrnum = NTFS_A_INDX;
1226 if (ntfs_cntob(blnum * cpbl) >= iavap->va_datalen)
1228 dprintf(("ntfs_ntreaddir: blnum: %d\n", (u_int32_t) blnum));
1237 ntfs_ntvattrrele(vap);
1239 ntfs_ntvattrrele(bmvap);
1241 ntfs_ntvattrrele(iavap);
1249 * Convert NTFS times that are in 100 ns units and begins from
1250 * 1601 Jan 1 into unix times.
1258 /* WindowNT times are in 100 ns and from 1601 Jan 1 */
1259 t.tv_nsec = (nt % (1000 * 1000 * 10)) * 100;
1260 t.tv_sec = nt / (1000 * 1000 * 10) -
1261 369LL * 365LL * 24LL * 60LL * 60LL -
1262 89LL * 1LL * 24LL * 60LL * 60LL;
1267 * Get file times from NTFS_A_NAME attribute.
1271 struct ntfsmount * ntmp,
1275 struct ntvattr *vap;
1278 dprintf(("ntfs_times: ino: %d...\n", ip->i_number));
1280 error = ntfs_ntget(ip);
1284 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_NAME, NULL, 0, &vap);
1289 *tm = vap->va_a_name->n_times;
1290 ntfs_ntvattrrele(vap);
1297 * Get file sizes from corresponding attribute.
1299 * ntnode under fnode should be locked.
1303 struct ntfsmount * ntmp,
1308 struct ntvattr *vap;
1309 struct ntnode *ip = FTONT(fp);
1313 dprintf(("ntfs_filesize: ino: %d\n", ip->i_number));
1315 error = ntfs_ntvattrget(ntmp, ip,
1316 fp->f_attrtype, fp->f_attrname, 0, &vap);
1320 bn = vap->va_allocated;
1321 sz = vap->va_datalen;
1323 dprintf(("ntfs_filesize: %d bytes (%d bytes allocated)\n",
1324 (u_int32_t) sz, (u_int32_t) bn));
1331 ntfs_ntvattrrele(vap);
1337 * This is one of write routine.
1340 ntfs_writeattr_plain(
1341 struct ntfsmount * ntmp,
1353 off_t off = roff, left = rsize, towrite;
1354 caddr_t data = rdata;
1355 struct ntvattr *vap;
1359 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1360 ntfs_btocn(off), &vap);
1363 towrite = min(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1364 ddprintf(("ntfs_writeattr_plain: o: %d, s: %d (%d - %d)\n",
1365 (u_int32_t) off, (u_int32_t) towrite,
1366 (u_int32_t) vap->va_vcnstart,
1367 (u_int32_t) vap->va_vcnend));
1368 error = ntfs_writentvattr_plain(ntmp, ip, vap,
1369 off - ntfs_cntob(vap->va_vcnstart),
1370 towrite, data, &init, uio);
1372 printf("ntfs_writeattr_plain: " \
1373 "ntfs_writentvattr_plain failed: o: %d, s: %d\n",
1374 (u_int32_t) off, (u_int32_t) towrite);
1375 printf("ntfs_writeattr_plain: attrib: %d - %d\n",
1376 (u_int32_t) vap->va_vcnstart,
1377 (u_int32_t) vap->va_vcnend);
1378 ntfs_ntvattrrele(vap);
1381 ntfs_ntvattrrele(vap);
1384 data = data + towrite;
1392 * This is one of write routine.
1394 * ntnode should be locked.
1397 ntfs_writentvattr_plain(
1398 struct ntfsmount * ntmp,
1400 struct ntvattr * vap,
1410 cn_t ccn, ccl, cn, left, cl;
1411 caddr_t data = rdata;
1417 if ((vap->va_flag & NTFS_AF_INRUN) == 0) {
1418 printf("ntfs_writevattr_plain: CAN'T WRITE RES. ATTRIBUTE\n");
1422 ddprintf(("ntfs_writentvattr_plain: data in run: %ld chains\n",
1430 for (; left && (cnt < vap->va_vruncnt); cnt++) {
1431 ccn = vap->va_vruncn[cnt];
1432 ccl = vap->va_vruncl[cnt];
1434 ddprintf(("ntfs_writentvattr_plain: " \
1435 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1436 (u_int32_t) left, (u_int32_t) ccn, \
1437 (u_int32_t) ccl, (u_int32_t) off));
1439 if (ntfs_cntob(ccl) < off) {
1440 off -= ntfs_cntob(ccl);
1444 if (!ccn && ip->i_number != NTFS_BOOTINO)
1447 ccl -= ntfs_btocn(off);
1448 cn = ccn + ntfs_btocn(off);
1449 off = ntfs_btocnoff(off);
1451 while (left && ccl) {
1452 #if defined(__DragonFly__)
1454 min(ntfs_cntob(ccl) - off, MAXBSIZE - off));
1456 /* under NetBSD, bread() can read
1457 * maximum one block worth of data */
1458 tocopy = min(left, ntmp->ntm_bps - off);
1460 cl = ntfs_btocl(tocopy + off);
1461 ddprintf(("ntfs_writentvattr_plain: write: " \
1462 "cn: 0x%x cl: %d, off: %d len: %d, left: %d\n",
1463 (u_int32_t) cn, (u_int32_t) cl,
1464 (u_int32_t) off, (u_int32_t) tocopy,
1466 if ((off == 0) && (tocopy == ntfs_cntob(cl)))
1468 bp = getblk(ntmp->ntm_devvp, ntfs_cntobn(cn),
1469 ntfs_cntob(cl), 0, 0);
1472 error = bread(ntmp->ntm_devvp, ntfs_cntobn(cn),
1473 ntfs_cntob(cl), &bp);
1480 uiomove(bp->b_data + off, tocopy, uio);
1482 memcpy(bp->b_data + off, data, tocopy);
1484 data = data + tocopy;
1494 printf("ntfs_writentvattr_plain: POSSIBLE RUN ERROR\n");
1502 * This is one of read routines.
1504 * ntnode should be locked.
1507 ntfs_readntvattr_plain(
1508 struct ntfsmount * ntmp,
1510 struct ntvattr * vap,
1521 if (vap->va_flag & NTFS_AF_INRUN) {
1523 cn_t ccn, ccl, cn, left, cl;
1524 caddr_t data = rdata;
1528 ddprintf(("ntfs_readntvattr_plain: data in run: %ld chains\n",
1536 while (left && (cnt < vap->va_vruncnt)) {
1537 ccn = vap->va_vruncn[cnt];
1538 ccl = vap->va_vruncl[cnt];
1540 ddprintf(("ntfs_readntvattr_plain: " \
1541 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1542 (u_int32_t) left, (u_int32_t) ccn, \
1543 (u_int32_t) ccl, (u_int32_t) off));
1545 if (ntfs_cntob(ccl) < off) {
1546 off -= ntfs_cntob(ccl);
1550 if (ccn || ip->i_number == NTFS_BOOTINO) {
1551 ccl -= ntfs_btocn(off);
1552 cn = ccn + ntfs_btocn(off);
1553 off = ntfs_btocnoff(off);
1555 while (left && ccl) {
1556 #if defined(__DragonFly__)
1558 min(ntfs_cntob(ccl) - off,
1561 /* under NetBSD, bread() can read
1562 * maximum one block worth of data */
1564 ntmp->ntm_bps - off);
1566 cl = ntfs_btocl(tocopy + off);
1567 ddprintf(("ntfs_readntvattr_plain: " \
1568 "read: cn: 0x%x cl: %d, " \
1569 "off: %d len: %d, left: %d\n",
1575 error = bread(ntmp->ntm_devvp,
1584 uiomove(bp->b_data + off,
1587 memcpy(data, bp->b_data + off,
1591 data = data + tocopy;
1599 tocopy = min(left, ntfs_cntob(ccl) - off);
1600 ddprintf(("ntfs_readntvattr_plain: "
1601 "hole: ccn: 0x%x ccl: %d, off: %d, " \
1602 " len: %d, left: %d\n",
1603 (u_int32_t) ccn, (u_int32_t) ccl,
1604 (u_int32_t) off, (u_int32_t) tocopy,
1609 size_t remains = tocopy;
1610 for(; remains; remains++)
1611 uiomove("", 1, uio);
1613 bzero(data, tocopy);
1614 data = data + tocopy;
1619 printf("ntfs_readntvattr_plain: POSSIBLE RUN ERROR\n");
1623 ddprintf(("ntfs_readnvattr_plain: data is in mft record\n"));
1625 uiomove(vap->va_datap + roff, rsize, uio);
1627 memcpy(rdata, vap->va_datap + roff, rsize);
1635 * This is one of read routines.
1638 ntfs_readattr_plain(
1639 struct ntfsmount * ntmp,
1651 off_t off = roff, left = rsize, toread;
1652 caddr_t data = rdata;
1653 struct ntvattr *vap;
1657 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1658 ntfs_btocn(off), &vap);
1661 toread = min(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1662 ddprintf(("ntfs_readattr_plain: o: %d, s: %d (%d - %d)\n",
1663 (u_int32_t) off, (u_int32_t) toread,
1664 (u_int32_t) vap->va_vcnstart,
1665 (u_int32_t) vap->va_vcnend));
1666 error = ntfs_readntvattr_plain(ntmp, ip, vap,
1667 off - ntfs_cntob(vap->va_vcnstart),
1668 toread, data, &init, uio);
1670 printf("ntfs_readattr_plain: " \
1671 "ntfs_readntvattr_plain failed: o: %d, s: %d\n",
1672 (u_int32_t) off, (u_int32_t) toread);
1673 printf("ntfs_readattr_plain: attrib: %d - %d\n",
1674 (u_int32_t) vap->va_vcnstart,
1675 (u_int32_t) vap->va_vcnend);
1676 ntfs_ntvattrrele(vap);
1679 ntfs_ntvattrrele(vap);
1682 data = data + toread;
1690 * This is one of read routines.
1694 struct ntfsmount * ntmp,
1704 struct ntvattr *vap;
1707 ddprintf(("ntfs_readattr: reading %d: 0x%x, from %d size %d bytes\n",
1708 ip->i_number, attrnum, (u_int32_t) roff, (u_int32_t) rsize));
1710 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname, 0, &vap);
1714 if ((roff > vap->va_datalen) ||
1715 (roff + rsize > vap->va_datalen)) {
1716 ddprintf(("ntfs_readattr: offset too big\n"));
1717 ntfs_ntvattrrele(vap);
1720 if (vap->va_compression && vap->va_compressalg) {
1723 off_t off = roff, left = rsize, tocopy;
1724 caddr_t data = rdata;
1727 ddprintf(("ntfs_ntreadattr: compression: %d\n",
1728 vap->va_compressalg));
1730 MALLOC(cup, u_int8_t *, ntfs_cntob(NTFS_COMPUNIT_CL),
1731 M_NTFSDECOMP, M_WAITOK);
1732 MALLOC(uup, u_int8_t *, ntfs_cntob(NTFS_COMPUNIT_CL),
1733 M_NTFSDECOMP, M_WAITOK);
1735 cn = (ntfs_btocn(roff)) & (~(NTFS_COMPUNIT_CL - 1));
1736 off = roff - ntfs_cntob(cn);
1739 error = ntfs_readattr_plain(ntmp, ip, attrnum,
1740 attrname, ntfs_cntob(cn),
1741 ntfs_cntob(NTFS_COMPUNIT_CL),
1746 tocopy = min(left, ntfs_cntob(NTFS_COMPUNIT_CL) - off);
1748 if (init == ntfs_cntob(NTFS_COMPUNIT_CL)) {
1750 uiomove(cup + off, tocopy, uio);
1752 memcpy(data, cup + off, tocopy);
1753 } else if (init == 0) {
1755 size_t remains = tocopy;
1756 for(; remains; remains--)
1757 uiomove("", 1, uio);
1760 bzero(data, tocopy);
1762 error = ntfs_uncompunit(ntmp, uup, cup);
1766 uiomove(uup + off, tocopy, uio);
1768 memcpy(data, uup + off, tocopy);
1772 data = data + tocopy;
1773 off += tocopy - ntfs_cntob(NTFS_COMPUNIT_CL);
1774 cn += NTFS_COMPUNIT_CL;
1777 FREE(uup, M_NTFSDECOMP);
1778 FREE(cup, M_NTFSDECOMP);
1780 error = ntfs_readattr_plain(ntmp, ip, attrnum, attrname,
1781 roff, rsize, rdata, &init, uio);
1782 ntfs_ntvattrrele(vap);
1799 printf("ntfs_parsetun: run == NULL\n");
1804 printf("ntfs_parserun: trying to go out of run\n");
1808 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1809 printf("ntfs_parserun: " \
1810 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1814 for (i = 0; i < (sz & 0xF); i++)
1815 *cl += (u_int32_t) run[(*off)++] << (i << 3);
1818 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1819 printf("ntfs_parserun: " \
1820 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1824 for (i = 0; i < (sz & 0xF); i++)
1825 *cn += (u_int32_t) run[(*off)++] << (i << 3);
1832 * Process fixup routine on given buffer.
1836 struct ntfsmount * ntmp,
1841 struct fixuphdr *fhp = (struct fixuphdr *) buf;
1847 if (fhp->fh_magic != magic) {
1848 printf("ntfs_procfixups: magic doesn't match: %08x != %08x\n",
1849 fhp->fh_magic, magic);
1852 if ((fhp->fh_fnum - 1) * ntmp->ntm_bps != len) {
1853 printf("ntfs_procfixups: " \
1854 "bad fixups number: %d for %ld bytes block\n",
1855 fhp->fh_fnum, (long)len); /* XXX printf kludge */
1858 if (fhp->fh_foff >= ntmp->ntm_spc * ntmp->ntm_mftrecsz * ntmp->ntm_bps) {
1859 printf("ntfs_procfixups: invalid offset: %x", fhp->fh_foff);
1862 fxp = (u_int16_t *) (buf + fhp->fh_foff);
1863 cfxp = (u_int16_t *) (buf + ntmp->ntm_bps - 2);
1865 for (i = 1; i < fhp->fh_fnum; i++, fxp++) {
1866 if (*cfxp != fixup) {
1867 printf("ntfs_procfixups: fixup %d doesn't match\n", i);
1871 ((caddr_t) cfxp) += ntmp->ntm_bps;
1880 struct ntfsmount * ntmp,
1892 printf("ntfs_runtocn: run: 0x%p, %ld bytes, vcn:%ld\n",
1893 run, len, (u_long) vcn);
1894 printf("ntfs_runtocn: run: ");
1895 for (i = 0; i < len; i++)
1896 printf("0x%02x ", run[i]);
1901 printf("ntfs_runtocn: run == NULL\n");
1905 if (run[off] == 0) {
1906 printf("ntfs_runtocn: vcn too big\n");
1910 error = ntfs_parserun(&ccn, &ccl, run, len, &off);
1912 printf("ntfs_runtocn: ntfs_parserun failed\n");
1915 } while (ccl <= vcn);
1922 * this initializes toupper table & dependant variables to be ready for
1928 ntfs_toupper_tab = (wchar *) NULL;
1929 lockinit(&ntfs_toupper_lock, 0, "ntfs_toupper", 0, 0);
1930 ntfs_toupper_usecount = 0;
1934 * if the ntfs_toupper_tab[] is filled already, just raise use count;
1935 * otherwise read the data from the filesystem we are currently mounting
1938 ntfs_toupper_use(mp, ntmp)
1940 struct ntfsmount *ntmp;
1945 /* get exclusive access */
1946 LOCKMGR(&ntfs_toupper_lock, LK_EXCLUSIVE, NULL);
1948 /* only read the translation data from a file if it hasn't been
1950 if (ntfs_toupper_tab)
1954 * Read in Unicode lowercase -> uppercase translation file.
1955 * XXX for now, just the first 256 entries are used anyway,
1956 * so don't bother reading more
1958 MALLOC(ntfs_toupper_tab, wchar *, 65536 * sizeof(wchar),
1959 M_NTFSRDATA, M_WAITOK);
1961 if ((error = VFS_VGET(mp, NTFS_UPCASEINO, &vp)))
1963 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
1964 0, 65536*sizeof(wchar), (char *) ntfs_toupper_tab, NULL);
1968 ntfs_toupper_usecount++;
1969 LOCKMGR(&ntfs_toupper_lock, LK_RELEASE, NULL);
1974 * lower the use count and if it reaches zero, free the memory
1975 * tied by toupper table
1978 ntfs_toupper_unuse()
1980 /* get exclusive access */
1981 LOCKMGR(&ntfs_toupper_lock, LK_EXCLUSIVE, NULL);
1983 ntfs_toupper_usecount--;
1984 if (ntfs_toupper_usecount == 0) {
1985 FREE(ntfs_toupper_tab, M_NTFSRDATA);
1986 ntfs_toupper_tab = NULL;
1989 else if (ntfs_toupper_usecount < 0) {
1990 panic("ntfs_toupper_unuse(): use count negative: %d\n",
1991 ntfs_toupper_usecount);
1995 /* release the lock */
1996 LOCKMGR(&ntfs_toupper_lock, LK_RELEASE, NULL);
2001 struct ntfsmount *ntmp,
2007 MALLOC(u28, char **, 256 * sizeof(char*), M_TEMP, M_WAITOK | M_ZERO);
2009 for (i=0; i<256; i++) {
2010 h = (u2w[i] >> 8) & 0xFF;
2013 if (u28[h] == NULL) {
2014 MALLOC(u28[h], char *, 256 * sizeof(char), M_TEMP, M_WAITOK);
2015 for (j=0; j<256; j++)
2019 u28[h][l] = i & 0xFF;
2022 ntmp->ntm_u28 = u28;
2028 ntfs_u28_uninit(struct ntfsmount *ntmp)
2033 if (ntmp->ntm_u28 == NULL)
2036 u28 = ntmp->ntm_u28;
2038 for (i=0; i<256; i++)
2040 FREE(u28[i], M_TEMP);
2049 struct ntfsmount *ntmp,
2055 MALLOC(_82u, wchar *, 256 * sizeof(wchar), M_TEMP, M_WAITOK);
2058 for (i=0; i<256; i++)
2061 for (i=0; i<128; i++)
2063 for (i=0; i<128; i++)
2064 _82u[i+128] = u2w[i];
2067 ntmp->ntm_82u = _82u;
2073 ntfs_82u_uninit(struct ntfsmount *ntmp)
2075 FREE(ntmp->ntm_82u, M_TEMP);
2080 * maps the Unicode char to 8bit equivalent
2081 * XXX currently only gets lower 8bit from the Unicode char
2082 * and substitutes a '_' for it if the result would be '\0';
2083 * something better has to be definitely though out
2087 struct ntfsmount *ntmp,
2092 p = ntmp->ntm_u28[(wc>>8)&0xFF];
2095 return (p[wc&0xFF]);