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
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/systm.h>
34 #include <sys/namei.h>
35 #include <sys/kernel.h>
36 #include <sys/vnode.h>
37 #include <sys/mount.h>
41 #include <sys/malloc.h>
43 #include <sys/iconv.h>
45 /* #define NTFS_DEBUG 1 */
46 #include <fs/ntfs/ntfs.h>
47 #include <fs/ntfs/ntfsmount.h>
48 #include <fs/ntfs/ntfs_inode.h>
49 #include <fs/ntfs/ntfs_vfsops.h>
50 #include <fs/ntfs/ntfs_subr.h>
51 #include <fs/ntfs/ntfs_compr.h>
52 #include <fs/ntfs/ntfs_ihash.h>
54 static MALLOC_DEFINE(M_NTFSNTVATTR, "ntfs_vattr",
55 "NTFS file attribute information");
56 static MALLOC_DEFINE(M_NTFSRDATA, "ntfsd_resdata", "NTFS resident data");
57 static MALLOC_DEFINE(M_NTFSRUN, "ntfs_vrun", "NTFS vrun storage");
58 static MALLOC_DEFINE(M_NTFSDECOMP, "ntfs_decomp",
59 "NTFS decompression temporary");
61 static int ntfs_ntlookupattr(struct ntfsmount *, const char *, int, int *, char **);
62 static int ntfs_findvattr(struct ntfsmount *, struct ntnode *, struct ntvattr **, struct ntvattr **, u_int32_t, const char *, size_t, cn_t);
63 static int ntfs_uastricmp(struct ntfsmount *, const wchar *, size_t, const char *, size_t);
64 static int ntfs_uastrcmp(struct ntfsmount *, const wchar *, size_t, const char *, size_t);
66 /* table for mapping Unicode chars into uppercase; it's filled upon first
67 * ntfs mount, freed upon last ntfs umount */
68 static wchar *ntfs_toupper_tab;
69 #define NTFS_TOUPPER(ch) (ntfs_toupper_tab[(ch)])
70 static struct lock ntfs_toupper_lock;
71 static signed int ntfs_toupper_usecount;
73 struct iconv_functions *ntfs_iconv = NULL;
75 /* support macro for ntfs_ntvattrget() */
76 #define NTFS_AALPCMP(aalp,type,name,namelen) ( \
77 (aalp->al_type == type) && (aalp->al_namelen == namelen) && \
78 !NTFS_UASTRCMP(aalp->al_name,aalp->al_namelen,name,namelen) )
87 dprintf(("ntfs_ntvattrrele: ino: %d, type: 0x%x\n",
88 vap->va_ip->i_number, vap->va_type));
90 ntfs_ntrele(vap->va_ip);
96 * find the attribute in the ntnode
99 ntfs_findvattr(ntmp, ip, lvapp, vapp, type, name, namelen, vcn)
100 struct ntfsmount *ntmp;
102 struct ntvattr **lvapp, **vapp;
111 if((ip->i_flag & IN_LOADED) == 0) {
112 dprintf(("ntfs_findvattr: node not loaded, ino: %d\n",
114 error = ntfs_loadntnode(ntmp,ip);
116 printf("ntfs_findvattr: FAILED TO LOAD INO: %d\n",
124 LIST_FOREACH(vap, &ip->i_valist, va_list) {
125 ddprintf(("ntfs_findvattr: type: 0x%x, vcn: %d - %d\n", \
126 vap->va_type, (u_int32_t) vap->va_vcnstart, \
127 (u_int32_t) vap->va_vcnend));
128 if ((vap->va_type == type) &&
129 (vap->va_vcnstart <= vcn) && (vap->va_vcnend >= vcn) &&
130 (vap->va_namelen == namelen) &&
131 (strncmp(name, vap->va_name, namelen) == 0)) {
133 ntfs_ntref(vap->va_ip);
136 if (vap->va_type == NTFS_A_ATTRLIST)
144 * Search attribute specifed in ntnode (load ntnode if nessecary).
145 * If not found but ATTR_A_ATTRLIST present, read it in and search throught.
146 * VOP_VGET node needed, and lookup througth it's ntnode (load if nessesary).
148 * ntnode should be locked
152 struct ntfsmount * ntmp,
157 struct ntvattr ** vapp)
159 struct ntvattr *lvap = NULL;
160 struct attr_attrlist *aalp;
161 struct attr_attrlist *nextaalp;
163 struct ntnode *newip;
171 dprintf(("ntfs_ntvattrget: " \
172 "ino: %d, type: 0x%x, name: %s, vcn: %d\n", \
173 ip->i_number, type, name, (u_int32_t) vcn));
174 namelen = strlen(name);
176 dprintf(("ntfs_ntvattrget: " \
177 "ino: %d, type: 0x%x, vcn: %d\n", \
178 ip->i_number, type, (u_int32_t) vcn));
183 error = ntfs_findvattr(ntmp, ip, &lvap, vapp, type, name, namelen, vcn);
188 dprintf(("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: " \
189 "ino: %d, type: 0x%x, name: %s, vcn: %d\n", \
190 ip->i_number, type, name, (u_int32_t) vcn));
193 /* Scan $ATTRIBUTE_LIST for requested attribute */
194 len = lvap->va_datalen;
195 alpool = malloc(len, M_TEMP, M_WAITOK);
196 error = ntfs_readntvattr_plain(ntmp, ip, lvap, 0, len, alpool, &len,
201 aalp = (struct attr_attrlist *) alpool;
204 for(; len > 0; aalp = nextaalp) {
205 dprintf(("ntfs_ntvattrget: " \
206 "attrlist: ino: %d, attr: 0x%x, vcn: %d\n", \
207 aalp->al_inumber, aalp->al_type, \
208 (u_int32_t) aalp->al_vcnstart));
210 if (len > aalp->reclen) {
211 nextaalp = NTFS_NEXTREC(aalp, struct attr_attrlist *);
217 if (!NTFS_AALPCMP(aalp, type, name, namelen) ||
218 (nextaalp && (nextaalp->al_vcnstart <= vcn) &&
219 NTFS_AALPCMP(nextaalp, type, name, namelen)))
222 dprintf(("ntfs_ntvattrget: attribute in ino: %d\n",
225 /* this is not a main record, so we can't use just plain
227 error = ntfs_vgetex(ntmp->ntm_mountp, aalp->al_inumber,
228 NTFS_A_DATA, NULL, LK_EXCLUSIVE,
229 VG_EXT, curthread, &newvp);
231 printf("ntfs_ntvattrget: CAN'T VGET INO: %d\n",
235 newip = VTONT(newvp);
236 /* XXX have to lock ntnode */
237 error = ntfs_findvattr(ntmp, newip, &lvap, vapp,
238 type, name, namelen, vcn);
242 printf("ntfs_ntvattrget: ATTRLIST ERROR.\n");
247 dprintf(("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: " \
248 "ino: %d, type: 0x%x, name: %.*s, vcn: %d\n", \
249 ip->i_number, type, (int) namelen, name, (u_int32_t) vcn));
251 free(alpool, M_TEMP);
256 * Read ntnode from disk, make ntvattr list.
258 * ntnode should be locked
262 struct ntfsmount * ntmp,
265 struct filerec *mfrp;
269 struct ntvattr *nvap;
271 dprintf(("ntfs_loadntnode: loading ino: %d\n",ip->i_number));
273 mfrp = malloc(ntfs_bntob(ntmp->ntm_bpmftrec),
276 if (ip->i_number < NTFS_SYSNODESNUM) {
279 dprintf(("ntfs_loadntnode: read system node\n"));
281 bn = ntfs_cntobn(ntmp->ntm_mftcn) +
282 ntmp->ntm_bpmftrec * ip->i_number;
283 bn *= ntmp->ntm_multiplier;
285 error = bread(ntmp->ntm_devvp,
286 bn, ntfs_bntob(ntmp->ntm_bpmftrec),
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 mtx_lock(&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 ip = ntfs_nthashlookup(ntmp->ntm_devvp->v_rdev, ino);
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 ip = malloc(sizeof(struct ntnode), M_NTFSNTNODE,
399 ddprintf(("ntfs_ntlookup: allocating ntnode: %d: %p\n", ino, ip));
401 /* Generic initialization */
402 ip->i_devvp = ntmp->ntm_devvp;
403 ip->i_dev = ntmp->ntm_devvp->v_rdev;
407 LIST_INIT(&ip->i_fnlist);
410 /* init lock and lock the newborn ntnode */
411 lockinit(&ip->i_lock, PINOD, "ntnode", 0, 0);
412 mtx_init(&ip->i_interlock, "ntnode interlock", NULL, MTX_DEF);
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 mtx_lock(&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 (LIST_FIRST(&ip->i_fnlist))
460 panic("ntfs_ntput: ntnode has fnodes\n");
464 while ((vap = LIST_FIRST(&ip->i_valist)) != NULL) {
465 LIST_REMOVE(vap,va_list);
466 ntfs_freentvattr(vap);
468 lockmgr(&ip->i_lock, LK_RELEASE | LK_INTERLOCK, &ip->i_interlock);
469 mtx_destroy(&ip->i_interlock);
470 lockdestroy(&ip->i_lock);
472 free(ip, M_NTFSNTNODE);
476 * increment usecount of ntnode
482 mtx_lock(&ip->i_interlock);
484 mtx_unlock(&ip->i_interlock);
486 dprintf(("ntfs_ntref: ino %d, usecount: %d\n",
487 ip->i_number, ip->i_usecount));
492 * Decrement usecount of ntnode.
498 dprintf(("ntfs_ntrele: rele ntnode %d: %p, usecount: %d\n",
499 ip->i_number, ip, ip->i_usecount));
501 mtx_lock(&ip->i_interlock);
504 if (ip->i_usecount < 0)
505 panic("ntfs_ntrele: ino: %d usecount: %d \n",
506 ip->i_number,ip->i_usecount);
507 mtx_unlock(&ip->i_interlock);
511 * Deallocate all memory allocated for ntvattr
514 ntfs_freentvattr(vap)
515 struct ntvattr * vap;
517 if (vap->va_flag & NTFS_AF_INRUN) {
519 free(vap->va_vruncn, M_NTFSRUN);
521 free(vap->va_vruncl, M_NTFSRUN);
524 free(vap->va_datap, M_NTFSRDATA);
526 free(vap, M_NTFSNTVATTR);
530 * Convert disk image of attribute into ntvattr structure,
531 * runs are expanded also.
535 struct ntfsmount * ntmp,
536 struct ntvattr ** rvapp,
545 vap = malloc(sizeof(struct ntvattr),
546 M_NTFSNTVATTR, M_WAITOK | M_ZERO);
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 vap->va_datap = malloc(vap->va_datalen,
583 M_NTFSRDATA, M_WAITOK);
584 memcpy(vap->va_datap, (caddr_t) rap + rap->a_r.a_dataoff,
587 ddprintf((", len: %lld", 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 cn = malloc(cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
625 cl = malloc(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;
672 const char *astrp = astr;
675 size_t i, j, mbstrlen = astrlen;
677 if (ntmp->ntm_ic_l2u) {
678 for (i = 0, j = 0; i < ustrlen && j < astrlen; i++) {
680 res = ((int) NTFS_TOUPPER(ustr[i])) -
681 ((int)NTFS_TOUPPER(NTFS_82U(astrp, &len)));
691 * We use NTFS_82U(NTFS_U28(c)) to get rid of unicode
692 * symbols not covered by translation table
694 for (i = 0; i < ustrlen && i < astrlen; i++) {
695 res = ((int) NTFS_TOUPPER(NTFS_82U(NTFS_U28(ustr[i]), &len))) -
696 ((int)NTFS_TOUPPER(NTFS_82U(astrp, &len)));
702 return (ustrlen - mbstrlen);
706 * Compare unicode and ascii string case sens.
709 ntfs_uastrcmp(ntmp, ustr, ustrlen, astr, astrlen)
710 struct ntfsmount *ntmp;
717 size_t i, j, mbstrlen = astrlen;
720 for (i = 0, j = 0; (i < ustrlen) && (j < astrlen); i++, mbstrlen++) {
721 c = NTFS_U28(ustr[i]);
723 res = (int) (*c++ - astr[j++]);
729 return (ustrlen - mbstrlen);
733 * Search fnode in ntnode, if not found allocate and preinitialize.
735 * ntnode should be locked on entry.
739 struct ntfsmount *ntmp,
747 dprintf(("ntfs_fget: ino: %d, attrtype: 0x%x, attrname: %s\n",
748 ip->i_number,attrtype, attrname?attrname:""));
750 LIST_FOREACH(fp, &ip->i_fnlist, f_fnlist){
751 dprintf(("ntfs_fget: fnode: attrtype: %d, attrname: %s\n",
752 fp->f_attrtype, fp->f_attrname?fp->f_attrname:""));
754 if ((attrtype == fp->f_attrtype) &&
755 ((!attrname && !fp->f_attrname) ||
756 (attrname && fp->f_attrname &&
757 !strcmp(attrname,fp->f_attrname)))){
758 dprintf(("ntfs_fget: found existed: %p\n",fp));
766 fp = malloc(sizeof(struct fnode), M_NTFSFNODE,
768 dprintf(("ntfs_fget: allocating fnode: %p\n",fp));
772 fp->f_flag |= FN_AATTRNAME;
773 fp->f_attrname = malloc(strlen(attrname)+1, M_TEMP, M_WAITOK);
774 strcpy(fp->f_attrname, attrname);
776 fp->f_attrname = NULL;
777 fp->f_attrtype = attrtype;
781 LIST_INSERT_HEAD(&ip->i_fnlist, fp, f_fnlist);
789 * Deallocate fnode, remove it from ntnode's fnode list.
791 * ntnode should be locked.
797 struct ntnode *ip = FTONT(fp);
799 dprintf(("ntfs_frele: fnode: %p for %d: %p\n", fp, ip->i_number, ip));
801 dprintf(("ntfs_frele: deallocating fnode\n"));
802 LIST_REMOVE(fp,f_fnlist);
803 if (fp->f_flag & FN_AATTRNAME)
804 free(fp->f_attrname, M_TEMP);
806 free(fp->f_dirblbuf, M_NTFSDIR);
807 free(fp, M_NTFSFNODE);
812 * Lookup attribute name in format: [[:$ATTR_TYPE]:$ATTR_NAME],
813 * $ATTR_TYPE is searched in attrdefs read from $AttrDefs.
814 * If $ATTR_TYPE nott specifed, ATTR_A_DATA assumed.
818 struct ntfsmount * ntmp,
826 struct ntvattrdef *adp;
831 if (name[0] == '$') {
833 for (syslen = 0; syslen < namelen; syslen++) {
834 if(sys[syslen] == ':') {
844 for (i = 0; i < ntmp->ntm_adnum; i++, adp++){
845 if (syslen != adp->ad_namelen ||
846 strncmp(sys, adp->ad_name, syslen) != 0)
849 *attrtype = adp->ad_type;
854 *attrtype = NTFS_A_DATA;
858 (*attrname) = malloc(namelen, M_TEMP, M_WAITOK);
859 memcpy((*attrname), name, namelen);
860 (*attrname)[namelen] = '\0';
867 * Lookup specifed node for filename, matching cnp,
868 * return fnode filled.
872 struct ntfsmount * ntmp,
874 struct componentname * cnp,
877 struct fnode *fp = VTOF(vp);
878 struct ntnode *ip = FTONT(fp);
879 struct ntvattr *vap; /* Root attribute */
880 cn_t cn; /* VCN in current attribute */
881 caddr_t rdbuf; /* Buffer to read directory's blocks */
883 u_int64_t rdsize; /* Length of data to read from current block */
884 struct attr_indexentry *iep;
885 int error, res, anamelen, fnamelen;
886 const char *fname,*aname;
888 int attrtype = NTFS_A_DATA;
889 char *attrname = NULL;
894 error = ntfs_ntget(ip);
898 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
899 if (error || (vap->va_flag & NTFS_AF_INRUN))
902 blsize = vap->va_a_iroot->ir_size;
903 rdsize = vap->va_datalen;
906 * Divide file name into: foofilefoofilefoofile[:attrspec]
907 * Store like this: fname:fnamelen [aname:anamelen]
909 fname = cnp->cn_nameptr;
912 for (fnamelen = 0; fnamelen < cnp->cn_namelen; fnamelen++)
913 if(fname[fnamelen] == ':') {
914 aname = fname + fnamelen + 1;
915 anamelen = cnp->cn_namelen - fnamelen - 1;
916 dprintf(("ntfs_ntlookupfile: %s (%d), attr: %s (%d)\n",
917 fname, fnamelen, aname, anamelen));
921 dprintf(("ntfs_ntlookupfile: blksz: %d, rdsz: %jd\n", blsize, rdsize));
923 rdbuf = malloc(blsize, M_TEMP, M_WAITOK);
925 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXROOT, "$I30",
926 0, rdsize, rdbuf, NULL);
930 aoff = sizeof(struct attr_indexroot);
933 iep = (struct attr_indexentry *) (rdbuf + aoff);
935 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
937 iep = (struct attr_indexentry *) (rdbuf + aoff))
939 ddprintf(("scan: %d, %d\n",
940 (u_int32_t) iep->ie_number,
941 (u_int32_t) iep->ie_fnametype));
943 /* check the name - the case-insensitible check
944 * has to come first, to break from this for loop
945 * if needed, so we can dive correctly */
946 res = NTFS_UASTRICMP(iep->ie_fname, iep->ie_fnamelen,
949 if (res < 0) continue;
951 if (iep->ie_fnametype == 0 ||
952 !(ntmp->ntm_flag & NTFS_MFLAG_CASEINS))
954 res = NTFS_UASTRCMP(iep->ie_fname,
955 iep->ie_fnamelen, fname, fnamelen);
956 if (res != 0) continue;
960 error = ntfs_ntlookupattr(ntmp,
962 &attrtype, &attrname);
967 /* Check if we've found ourself */
968 if ((iep->ie_number == ip->i_number) &&
969 (attrtype == fp->f_attrtype) &&
970 ((!attrname && !fp->f_attrname) ||
971 (attrname && fp->f_attrname &&
972 !strcmp(attrname, fp->f_attrname))))
980 /* vget node, but don't load it */
981 error = ntfs_vgetex(ntmp->ntm_mountp,
982 iep->ie_number, attrtype, attrname,
983 LK_EXCLUSIVE, VG_DONTLOADIN | VG_DONTVALIDFN,
986 /* free the buffer returned by ntfs_ntlookupattr() */
988 free(attrname, M_TEMP);
997 if (nfp->f_flag & FN_VALID) {
1002 nfp->f_fflag = iep->ie_fflag;
1003 nfp->f_pnumber = iep->ie_fpnumber;
1004 nfp->f_times = iep->ie_ftimes;
1006 if((nfp->f_fflag & NTFS_FFLAG_DIR) &&
1007 (nfp->f_attrtype == NTFS_A_DATA) &&
1008 (nfp->f_attrname == NULL))
1013 nvp->v_type = f_type;
1015 if ((nfp->f_attrtype == NTFS_A_DATA) &&
1016 (nfp->f_attrname == NULL))
1018 /* Opening default attribute */
1019 nfp->f_size = iep->ie_fsize;
1020 nfp->f_allocated = iep->ie_fallocated;
1021 nfp->f_flag |= FN_PRELOADED;
1023 error = ntfs_filesize(ntmp, nfp,
1024 &nfp->f_size, &nfp->f_allocated);
1031 nfp->f_flag &= ~FN_VALID;
1036 /* Dive if possible */
1037 if (iep->ie_flag & NTFS_IEFLAG_SUBNODE) {
1038 dprintf(("ntfs_ntlookupfile: diving\n"));
1040 cn = *(cn_t *) (rdbuf + aoff +
1041 iep->reclen - sizeof(cn_t));
1044 error = ntfs_readattr(ntmp, ip, NTFS_A_INDX, "$I30",
1045 ntfs_cntob(cn), rdsize, rdbuf, NULL);
1049 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1054 aoff = (((struct attr_indexalloc *) rdbuf)->ia_hdrsize +
1057 dprintf(("ntfs_ntlookupfile: nowhere to dive :-(\n"));
1063 dprintf(("finish\n"));
1066 if (attrname) free(attrname, M_TEMP);
1067 ntfs_ntvattrrele(vap);
1069 free(rdbuf, M_TEMP);
1074 * Check if name type is permitted to show.
1077 ntfs_isnamepermitted(
1078 struct ntfsmount * ntmp,
1079 struct attr_indexentry * iep)
1081 if (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)
1084 switch (iep->ie_fnametype) {
1086 ddprintf(("ntfs_isnamepermitted: skipped DOS name\n"));
1088 case 0: case 1: case 3:
1091 printf("ntfs_isnamepermitted: " \
1092 "WARNING! Unknown file name type: %d\n",
1100 * Read ntfs dir like stream of attr_indexentry, not like btree of them.
1101 * This is done by scaning $BITMAP:$I30 for busy clusters and reading them.
1102 * Ofcouse $INDEX_ROOT:$I30 is read before. Last read values are stored in
1103 * fnode, so we can skip toward record number num almost immediatly.
1104 * Anyway this is rather slow routine. The problem is that we don't know
1105 * how many records are there in $INDEX_ALLOCATION:$I30 block.
1109 struct ntfsmount * ntmp,
1112 struct attr_indexentry ** riepp)
1114 struct ntnode *ip = FTONT(fp);
1115 struct ntvattr *vap = NULL; /* IndexRoot attribute */
1116 struct ntvattr *bmvap = NULL; /* BitMap attribute */
1117 struct ntvattr *iavap = NULL; /* IndexAllocation attribute */
1118 caddr_t rdbuf; /* Buffer to read directory's blocks */
1119 u_int8_t *bmp = NULL; /* Bitmap */
1120 u_int32_t blsize; /* Index allocation size (2048) */
1121 u_int32_t rdsize; /* Length of data to read */
1122 u_int32_t attrnum; /* Current attribute type */
1123 u_int32_t cpbl = 1; /* Clusters per directory block */
1125 struct attr_indexentry *iep;
1127 u_int32_t aoff, cnum;
1129 dprintf(("ntfs_ntreaddir: read ino: %d, num: %d\n", ip->i_number, num));
1130 error = ntfs_ntget(ip);
1134 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
1138 if (fp->f_dirblbuf == NULL) {
1139 fp->f_dirblsz = vap->va_a_iroot->ir_size;
1140 fp->f_dirblbuf = malloc(max(vap->va_datalen,fp->f_dirblsz),
1141 M_NTFSDIR, M_WAITOK);
1144 blsize = fp->f_dirblsz;
1145 rdbuf = fp->f_dirblbuf;
1147 dprintf(("ntfs_ntreaddir: rdbuf: %p, blsize: %d\n", rdbuf, blsize));
1149 if (vap->va_a_iroot->ir_flag & NTFS_IRFLAG_INDXALLOC) {
1150 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXBITMAP, "$I30",
1156 bmp = malloc(bmvap->va_datalen, M_TEMP, M_WAITOK);
1157 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXBITMAP, "$I30", 0,
1158 bmvap->va_datalen, bmp, NULL);
1162 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDX, "$I30",
1168 cpbl = ntfs_btocn(blsize + ntfs_cntob(1) - 1);
1169 dprintf(("ntfs_ntreaddir: indexalloc: %jd, cpbl: %d\n",
1170 iavap->va_datalen, cpbl));
1172 dprintf(("ntfs_ntreadidir: w/o BitMap and IndexAllocation\n"));
1173 iavap = bmvap = NULL;
1177 /* Try use previous values */
1178 if ((fp->f_lastdnum < num) && (fp->f_lastdnum != 0)) {
1179 attrnum = fp->f_lastdattr;
1180 aoff = fp->f_lastdoff;
1181 blnum = fp->f_lastdblnum;
1182 cnum = fp->f_lastdnum;
1184 attrnum = NTFS_A_INDXROOT;
1185 aoff = sizeof(struct attr_indexroot);
1191 dprintf(("ntfs_ntreaddir: scan: 0x%x, %d, %d, %d, %d\n",
1192 attrnum, (u_int32_t) blnum, cnum, num, aoff));
1193 rdsize = (attrnum == NTFS_A_INDXROOT) ? vap->va_datalen : blsize;
1194 error = ntfs_readattr(ntmp, ip, attrnum, "$I30",
1195 ntfs_cntob(blnum * cpbl), rdsize, rdbuf, NULL);
1199 if (attrnum == NTFS_A_INDX) {
1200 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1206 aoff = (attrnum == NTFS_A_INDX) ?
1207 (0x18 + ((struct attr_indexalloc *) rdbuf)->ia_hdrsize) :
1208 sizeof(struct attr_indexroot);
1210 iep = (struct attr_indexentry *) (rdbuf + aoff);
1211 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
1212 aoff += iep->reclen,
1213 iep = (struct attr_indexentry *) (rdbuf + aoff))
1215 if (!ntfs_isnamepermitted(ntmp, iep)) continue;
1218 fp->f_lastdnum = cnum;
1219 fp->f_lastdoff = aoff;
1220 fp->f_lastdblnum = blnum;
1221 fp->f_lastdattr = attrnum;
1232 if (attrnum == NTFS_A_INDXROOT)
1237 while (ntfs_cntob(blnum * cpbl) < iavap->va_datalen) {
1238 if (bmp[blnum >> 3] & (1 << (blnum & 7)))
1243 attrnum = NTFS_A_INDX;
1245 if (ntfs_cntob(blnum * cpbl) >= iavap->va_datalen)
1247 dprintf(("ntfs_ntreaddir: blnum: %d\n", (u_int32_t) blnum));
1256 ntfs_ntvattrrele(vap);
1258 ntfs_ntvattrrele(bmvap);
1260 ntfs_ntvattrrele(iavap);
1268 * Convert NTFS times that are in 100 ns units and begins from
1269 * 1601 Jan 1 into unix times.
1277 /* WindowNT times are in 100 ns and from 1601 Jan 1 */
1278 t.tv_nsec = (nt % (1000 * 1000 * 10)) * 100;
1279 t.tv_sec = nt / (1000 * 1000 * 10) -
1280 369LL * 365LL * 24LL * 60LL * 60LL -
1281 89LL * 1LL * 24LL * 60LL * 60LL;
1286 * Get file times from NTFS_A_NAME attribute.
1290 struct ntfsmount * ntmp,
1294 struct ntvattr *vap;
1297 dprintf(("ntfs_times: ino: %d...\n", ip->i_number));
1299 error = ntfs_ntget(ip);
1303 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_NAME, NULL, 0, &vap);
1308 *tm = vap->va_a_name->n_times;
1309 ntfs_ntvattrrele(vap);
1316 * Get file sizes from corresponding attribute.
1318 * ntnode under fnode should be locked.
1322 struct ntfsmount * ntmp,
1327 struct ntvattr *vap;
1328 struct ntnode *ip = FTONT(fp);
1332 dprintf(("ntfs_filesize: ino: %d\n", ip->i_number));
1334 error = ntfs_ntvattrget(ntmp, ip,
1335 fp->f_attrtype, fp->f_attrname, 0, &vap);
1339 bn = vap->va_allocated;
1340 sz = vap->va_datalen;
1342 dprintf(("ntfs_filesize: %d bytes (%d bytes allocated)\n",
1343 (u_int32_t) sz, (u_int32_t) bn));
1350 ntfs_ntvattrrele(vap);
1356 * This is one of write routine.
1359 ntfs_writeattr_plain(
1360 struct ntfsmount * ntmp,
1372 off_t off = roff, left = rsize, towrite;
1373 caddr_t data = rdata;
1374 struct ntvattr *vap;
1378 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1379 ntfs_btocn(off), &vap);
1382 towrite = MIN(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1383 ddprintf(("ntfs_writeattr_plain: o: %d, s: %d (%d - %d)\n",
1384 (u_int32_t) off, (u_int32_t) towrite,
1385 (u_int32_t) vap->va_vcnstart,
1386 (u_int32_t) vap->va_vcnend));
1387 error = ntfs_writentvattr_plain(ntmp, ip, vap,
1388 off - ntfs_cntob(vap->va_vcnstart),
1389 towrite, data, &init, uio);
1391 printf("ntfs_writeattr_plain: " \
1392 "ntfs_writentvattr_plain failed: o: %d, s: %d\n",
1393 (u_int32_t) off, (u_int32_t) towrite);
1394 printf("ntfs_writeattr_plain: attrib: %d - %d\n",
1395 (u_int32_t) vap->va_vcnstart,
1396 (u_int32_t) vap->va_vcnend);
1397 ntfs_ntvattrrele(vap);
1400 ntfs_ntvattrrele(vap);
1403 data = data + towrite;
1411 * This is one of write routine.
1413 * ntnode should be locked.
1416 ntfs_writentvattr_plain(
1417 struct ntfsmount * ntmp,
1419 struct ntvattr * vap,
1429 cn_t ccn, ccl, cn, left, cl;
1430 caddr_t data = rdata;
1436 if ((vap->va_flag & NTFS_AF_INRUN) == 0) {
1437 printf("ntfs_writevattr_plain: CAN'T WRITE RES. ATTRIBUTE\n");
1441 ddprintf(("ntfs_writentvattr_plain: data in run: %ld chains\n",
1449 for (; left && (cnt < vap->va_vruncnt); cnt++) {
1450 ccn = vap->va_vruncn[cnt];
1451 ccl = vap->va_vruncl[cnt];
1453 ddprintf(("ntfs_writentvattr_plain: " \
1454 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1455 (u_int32_t) left, (u_int32_t) ccn, \
1456 (u_int32_t) ccl, (u_int32_t) off));
1458 if (ntfs_cntob(ccl) < off) {
1459 off -= ntfs_cntob(ccl);
1463 if (!ccn && ip->i_number != NTFS_BOOTINO)
1466 ccl -= ntfs_btocn(off);
1467 cn = ccn + ntfs_btocn(off);
1468 off = ntfs_btocnoff(off);
1470 while (left && ccl) {
1472 * Always read and write single clusters at a time -
1473 * we need to avoid requesting differently-sized
1474 * blocks at the same disk offsets to avoid
1475 * confusing the buffer cache.
1477 tocopy = MIN(left, ntfs_cntob(1) - off);
1478 cl = ntfs_btocl(tocopy + off);
1479 KASSERT(cl == 1 && tocopy <= ntfs_cntob(1),
1480 ("single cluster limit mistake"));
1481 ddprintf(("ntfs_writentvattr_plain: write: " \
1482 "cn: 0x%x cl: %d, off: %d len: %d, left: %d\n",
1483 (u_int32_t) cn, (u_int32_t) cl,
1484 (u_int32_t) off, (u_int32_t) tocopy,
1486 if ((off == 0) && (tocopy == ntfs_cntob(cl)))
1488 bp = getblk(ntmp->ntm_devvp, ntfs_cntobn(cn)
1489 * ntmp->ntm_multiplier,
1490 ntfs_cntob(cl), 0, 0, 0);
1493 error = bread(ntmp->ntm_devvp, ntfs_cntobn(cn)
1494 * ntmp->ntm_multiplier,
1495 ntfs_cntob(cl), NOCRED, &bp);
1502 uiomove(bp->b_data + off, tocopy, uio);
1504 memcpy(bp->b_data + off, data, tocopy);
1506 data = data + tocopy;
1516 printf("ntfs_writentvattr_plain: POSSIBLE RUN ERROR\n");
1524 * This is one of read routines.
1526 * ntnode should be locked.
1529 ntfs_readntvattr_plain(
1530 struct ntfsmount * ntmp,
1532 struct ntvattr * vap,
1543 if (vap->va_flag & NTFS_AF_INRUN) {
1545 cn_t ccn, ccl, cn, left, cl;
1546 caddr_t data = rdata;
1550 ddprintf(("ntfs_readntvattr_plain: data in run: %ld chains\n",
1558 while (left && (cnt < vap->va_vruncnt)) {
1559 ccn = vap->va_vruncn[cnt];
1560 ccl = vap->va_vruncl[cnt];
1562 ddprintf(("ntfs_readntvattr_plain: " \
1563 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1564 (u_int32_t) left, (u_int32_t) ccn, \
1565 (u_int32_t) ccl, (u_int32_t) off));
1567 if (ntfs_cntob(ccl) < off) {
1568 off -= ntfs_cntob(ccl);
1572 if (ccn || ip->i_number == NTFS_BOOTINO) {
1573 ccl -= ntfs_btocn(off);
1574 cn = ccn + ntfs_btocn(off);
1575 off = ntfs_btocnoff(off);
1577 while (left && ccl) {
1579 * Always read single clusters at a
1580 * time - we need to avoid reading
1581 * differently-sized blocks at the
1582 * same disk offsets to avoid
1583 * confusing the buffer cache.
1586 ntfs_cntob(1) - off);
1587 cl = ntfs_btocl(tocopy + off);
1589 tocopy <= ntfs_cntob(1),
1590 ("single cluster limit mistake"));
1592 ddprintf(("ntfs_readntvattr_plain: " \
1593 "read: cn: 0x%x cl: %d, " \
1594 "off: %d len: %d, left: %d\n",
1600 error = bread(ntmp->ntm_devvp,
1602 * ntmp->ntm_multiplier,
1610 uiomove(bp->b_data + off,
1613 memcpy(data, bp->b_data + off,
1617 data = data + tocopy;
1625 tocopy = MIN(left, ntfs_cntob(ccl) - off);
1626 ddprintf(("ntfs_readntvattr_plain: "
1627 "hole: ccn: 0x%x ccl: %d, off: %d, " \
1628 " len: %d, left: %d\n",
1629 (u_int32_t) ccn, (u_int32_t) ccl,
1630 (u_int32_t) off, (u_int32_t) tocopy,
1635 size_t remains = tocopy;
1636 for(; remains; remains--)
1637 uiomove("", 1, uio);
1639 bzero(data, tocopy);
1640 data = data + tocopy;
1645 printf("ntfs_readntvattr_plain: POSSIBLE RUN ERROR\n");
1649 ddprintf(("ntfs_readnvattr_plain: data is in mft record\n"));
1651 uiomove(vap->va_datap + roff, rsize, uio);
1653 memcpy(rdata, vap->va_datap + roff, rsize);
1661 * This is one of read routines.
1664 ntfs_readattr_plain(
1665 struct ntfsmount * ntmp,
1677 off_t off = roff, left = rsize, toread;
1678 caddr_t data = rdata;
1679 struct ntvattr *vap;
1683 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1684 ntfs_btocn(off), &vap);
1687 toread = MIN(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1688 ddprintf(("ntfs_readattr_plain: o: %d, s: %d (%d - %d)\n",
1689 (u_int32_t) off, (u_int32_t) toread,
1690 (u_int32_t) vap->va_vcnstart,
1691 (u_int32_t) vap->va_vcnend));
1692 error = ntfs_readntvattr_plain(ntmp, ip, vap,
1693 off - ntfs_cntob(vap->va_vcnstart),
1694 toread, data, &init, uio);
1696 printf("ntfs_readattr_plain: " \
1697 "ntfs_readntvattr_plain failed: o: %d, s: %d\n",
1698 (u_int32_t) off, (u_int32_t) toread);
1699 printf("ntfs_readattr_plain: attrib: %d - %d\n",
1700 (u_int32_t) vap->va_vcnstart,
1701 (u_int32_t) vap->va_vcnend);
1702 ntfs_ntvattrrele(vap);
1705 ntfs_ntvattrrele(vap);
1708 data = data + toread;
1716 * This is one of read routines.
1720 struct ntfsmount * ntmp,
1730 struct ntvattr *vap;
1733 ddprintf(("ntfs_readattr: reading %d: 0x%x, from %d size %d bytes\n",
1734 ip->i_number, attrnum, (u_int32_t) roff, (u_int32_t) rsize));
1736 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname, 0, &vap);
1740 if ((roff > vap->va_datalen) ||
1741 (roff + rsize > vap->va_datalen)) {
1742 ddprintf(("ntfs_readattr: offset too big\n"));
1743 ntfs_ntvattrrele(vap);
1746 if (vap->va_compression && vap->va_compressalg) {
1749 off_t off = roff, left = rsize, tocopy;
1750 caddr_t data = rdata;
1753 ddprintf(("ntfs_ntreadattr: compression: %d\n",
1754 vap->va_compressalg));
1756 cup = malloc(ntfs_cntob(NTFS_COMPUNIT_CL),
1757 M_NTFSDECOMP, M_WAITOK);
1758 uup = malloc(ntfs_cntob(NTFS_COMPUNIT_CL),
1759 M_NTFSDECOMP, M_WAITOK);
1761 cn = (ntfs_btocn(roff)) & (~(NTFS_COMPUNIT_CL - 1));
1762 off = roff - ntfs_cntob(cn);
1765 error = ntfs_readattr_plain(ntmp, ip, attrnum,
1766 attrname, ntfs_cntob(cn),
1767 ntfs_cntob(NTFS_COMPUNIT_CL),
1772 tocopy = MIN(left, ntfs_cntob(NTFS_COMPUNIT_CL) - off);
1774 if (init == ntfs_cntob(NTFS_COMPUNIT_CL)) {
1776 uiomove(cup + off, tocopy, uio);
1778 memcpy(data, cup + off, tocopy);
1779 } else if (init == 0) {
1781 size_t remains = tocopy;
1782 for(; remains; remains--)
1783 uiomove("", 1, uio);
1786 bzero(data, tocopy);
1788 error = ntfs_uncompunit(ntmp, uup, cup);
1792 uiomove(uup + off, tocopy, uio);
1794 memcpy(data, uup + off, tocopy);
1798 data = data + tocopy;
1799 off += tocopy - ntfs_cntob(NTFS_COMPUNIT_CL);
1800 cn += NTFS_COMPUNIT_CL;
1803 free(uup, M_NTFSDECOMP);
1804 free(cup, M_NTFSDECOMP);
1806 error = ntfs_readattr_plain(ntmp, ip, attrnum, attrname,
1807 roff, rsize, rdata, &init, uio);
1808 ntfs_ntvattrrele(vap);
1825 printf("ntfs_parsetun: run == NULL\n");
1830 printf("ntfs_parserun: trying to go out of run\n");
1834 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1835 printf("ntfs_parserun: " \
1836 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1840 for (i = 0; i < (sz & 0xF); i++)
1841 *cl += (u_int32_t) run[(*off)++] << (i << 3);
1844 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1845 printf("ntfs_parserun: " \
1846 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1850 for (i = 0; i < (sz & 0xF); i++)
1851 *cn += (u_int32_t) run[(*off)++] << (i << 3);
1858 * Process fixup routine on given buffer.
1862 struct ntfsmount * ntmp,
1867 struct fixuphdr *fhp = (struct fixuphdr *) buf;
1873 if (fhp->fh_magic != magic) {
1874 printf("ntfs_procfixups: magic doesn't match: %08x != %08x\n",
1875 fhp->fh_magic, magic);
1878 if ((fhp->fh_fnum - 1) * NTFS_BLOCK_SIZE != len) {
1879 printf("ntfs_procfixups: " \
1880 "bad fixups number: %d for %ld bytes block\n",
1881 fhp->fh_fnum, (long)len); /* XXX printf kludge */
1884 if (fhp->fh_foff >= ntmp->ntm_spc * ntmp->ntm_mftrecsz * ntmp->ntm_bps) {
1885 printf("ntfs_procfixups: invalid offset: %x", fhp->fh_foff);
1888 fxp = (u_int16_t *) (buf + fhp->fh_foff);
1889 cfxp = (u_int16_t *) (buf + NTFS_BLOCK_SIZE - 2);
1891 for (i = 1; i < fhp->fh_fnum; i++, fxp++) {
1892 if (*cfxp != fixup) {
1893 printf("ntfs_procfixups: fixup %d doesn't match\n", i);
1897 cfxp = (u_int16_t *) ((caddr_t) cfxp + NTFS_BLOCK_SIZE);
1906 struct ntfsmount * ntmp,
1918 printf("ntfs_runtocn: run: %p, %ld bytes, vcn:%ld\n",
1919 run, len, (u_long) vcn);
1920 printf("ntfs_runtocn: run: ");
1921 for (i = 0; i < len; i++)
1922 printf("0x%02x ", run[i]);
1927 printf("ntfs_runtocn: run == NULL\n");
1931 if (run[off] == 0) {
1932 printf("ntfs_runtocn: vcn too big\n");
1936 error = ntfs_parserun(&ccn, &ccl, run, len, &off);
1938 printf("ntfs_runtocn: ntfs_parserun failed\n");
1941 } while (ccl <= vcn);
1948 * this initializes toupper table & dependant variables to be ready for
1954 ntfs_toupper_tab = (wchar *) NULL;
1955 lockinit(&ntfs_toupper_lock, PVFS, "ntfs_toupper", 0, 0);
1956 ntfs_toupper_usecount = 0;
1960 ntfs_toupper_destroy(void)
1963 lockdestroy(&ntfs_toupper_lock);
1967 * if the ntfs_toupper_tab[] is filled already, just raise use count;
1968 * otherwise read the data from the filesystem we are currently mounting
1971 ntfs_toupper_use(mp, ntmp)
1973 struct ntfsmount *ntmp;
1978 /* get exclusive access */
1979 lockmgr(&ntfs_toupper_lock, LK_EXCLUSIVE, NULL);
1981 /* only read the translation data from a file if it hasn't been
1983 if (ntfs_toupper_tab)
1987 * Read in Unicode lowercase -> uppercase translation file.
1988 * XXX for now, just the first 256 entries are used anyway,
1989 * so don't bother reading more
1991 ntfs_toupper_tab = malloc(65536 * sizeof(wchar),
1992 M_NTFSRDATA, M_WAITOK);
1994 if ((error = VFS_VGET(mp, NTFS_UPCASEINO, LK_EXCLUSIVE, &vp)))
1996 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
1997 0, 65536*sizeof(wchar), (char *) ntfs_toupper_tab, NULL);
2001 ntfs_toupper_usecount++;
2002 lockmgr(&ntfs_toupper_lock, LK_RELEASE, NULL);
2007 * lower the use count and if it reaches zero, free the memory
2008 * tied by toupper table
2011 ntfs_toupper_unuse()
2013 /* get exclusive access */
2014 lockmgr(&ntfs_toupper_lock, LK_EXCLUSIVE, NULL);
2016 ntfs_toupper_usecount--;
2017 if (ntfs_toupper_usecount == 0) {
2018 free(ntfs_toupper_tab, M_NTFSRDATA);
2019 ntfs_toupper_tab = NULL;
2022 else if (ntfs_toupper_usecount < 0) {
2023 panic("ntfs_toupper_unuse(): use count negative: %d\n",
2024 ntfs_toupper_usecount);
2028 /* release the lock */
2029 lockmgr(&ntfs_toupper_lock, LK_RELEASE, NULL);
2034 struct ntfsmount *ntmp,
2042 if (ntfs_iconv && cs_local) {
2043 ntfs_iconv->open(cs_local, cs_ntfs, &ntmp->ntm_ic_u2l);
2047 u28 = malloc(256 * sizeof(char*), M_TEMP, M_WAITOK | M_ZERO);
2049 for (i=0; i<256; i++) {
2050 h = (u2w[i] >> 8) & 0xFF;
2053 if (u28[h] == NULL) {
2054 u28[h] = malloc(256 * sizeof(char), M_TEMP, M_WAITOK);
2055 for (j=0; j<256; j++)
2059 u28[h][l] = i & 0xFF;
2062 ntmp->ntm_u28 = u28;
2068 ntfs_u28_uninit(struct ntfsmount *ntmp)
2073 if (ntmp->ntm_u28 == NULL) {
2074 if (ntfs_iconv && ntmp->ntm_ic_u2l) {
2075 ntfs_iconv->close(ntmp->ntm_ic_u2l);
2080 u28 = ntmp->ntm_u28;
2082 for (i=0; i<256; i++)
2084 free(u28[i], M_TEMP);
2093 struct ntfsmount *ntmp,
2100 if (ntfs_iconv && cs_local) {
2101 ntfs_iconv->open(cs_ntfs, cs_local, &ntmp->ntm_ic_l2u);
2105 _82u = malloc(256 * sizeof(wchar), M_TEMP, M_WAITOK);
2107 for (i=0; i<256; i++)
2110 ntmp->ntm_82u = _82u;
2116 ntfs_82u_uninit(struct ntfsmount *ntmp)
2119 if (ntmp->ntm_82u == NULL) {
2120 if (ntfs_iconv && ntmp->ntm_ic_l2u) {
2121 ntfs_iconv->close(ntmp->ntm_ic_l2u);
2126 free(ntmp->ntm_82u, M_TEMP);
2131 * maps the Unicode char to local character
2136 struct ntfsmount *ntmp,
2139 char *p, *outp, inbuf[3];
2143 if (ntfs_iconv && ntmp->ntm_ic_u2l) {
2147 inbuf[0] = (char)(wc>>8);
2148 inbuf[1] = (char)wc;
2151 ntfs_iconv->convchr(ntmp->ntm_ic_u2l, (const char **)&p, &ilen,
2160 p = ntmp->ntm_u28[(wc>>8)&0xFF];
2161 outbuf[0] = (p == NULL) ? '_' : p[wc&0xFF] & 0xFF;
2168 struct ntfsmount *ntmp,
2172 char *outp, outbuf[3];
2176 if (ntfs_iconv && ntmp->ntm_ic_l2u) {
2177 ilen = (size_t)*len;
2181 ntfs_iconv->convchr(ntmp->ntm_ic_l2u, &c, &ilen, &outp, &olen);
2183 uc = (wchar)((outbuf[0]<<8) | (outbuf[1]&0xFF));
2188 if (ntmp->ntm_82u != NULL)
2189 return (ntmp->ntm_82u[*c&0xFF]);