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 MALLOC_DEFINE(M_NTFSNTVATTR, "ntfs_vattr", "NTFS file attribute information");
55 MALLOC_DEFINE(M_NTFSRDATA, "ntfsd_resdata", "NTFS resident data");
56 MALLOC_DEFINE(M_NTFSRUN, "ntfs_vrun", "NTFS vrun storage");
57 MALLOC_DEFINE(M_NTFSDECOMP, "ntfs_decomp", "NTFS decompression temporary");
59 static int ntfs_ntlookupattr(struct ntfsmount *, const char *, int, int *, char **);
60 static int ntfs_findvattr(struct ntfsmount *, struct ntnode *, struct ntvattr **, struct ntvattr **, u_int32_t, const char *, size_t, cn_t);
61 static int ntfs_uastricmp(struct ntfsmount *, const wchar *, size_t, const char *, size_t);
62 static int ntfs_uastrcmp(struct ntfsmount *, const wchar *, size_t, const char *, size_t);
64 /* table for mapping Unicode chars into uppercase; it's filled upon first
65 * ntfs mount, freed upon last ntfs umount */
66 static wchar *ntfs_toupper_tab;
67 #define NTFS_TOUPPER(ch) (ntfs_toupper_tab[(ch)])
68 static struct lock ntfs_toupper_lock;
69 static signed int ntfs_toupper_usecount;
71 struct iconv_functions *ntfs_iconv = NULL;
73 /* support macro for ntfs_ntvattrget() */
74 #define NTFS_AALPCMP(aalp,type,name,namelen) ( \
75 (aalp->al_type == type) && (aalp->al_namelen == namelen) && \
76 !NTFS_UASTRCMP(aalp->al_name,aalp->al_namelen,name,namelen) )
85 dprintf(("ntfs_ntvattrrele: ino: %d, type: 0x%x\n",
86 vap->va_ip->i_number, vap->va_type));
88 ntfs_ntrele(vap->va_ip);
94 * find the attribute in the ntnode
97 ntfs_findvattr(ntmp, ip, lvapp, vapp, type, name, namelen, vcn)
98 struct ntfsmount *ntmp;
100 struct ntvattr **lvapp, **vapp;
109 if((ip->i_flag & IN_LOADED) == 0) {
110 dprintf(("ntfs_findvattr: node not loaded, ino: %d\n",
112 error = ntfs_loadntnode(ntmp,ip);
114 printf("ntfs_findvattr: FAILED TO LOAD INO: %d\n",
122 LIST_FOREACH(vap, &ip->i_valist, va_list) {
123 ddprintf(("ntfs_findvattr: type: 0x%x, vcn: %d - %d\n", \
124 vap->va_type, (u_int32_t) vap->va_vcnstart, \
125 (u_int32_t) vap->va_vcnend));
126 if ((vap->va_type == type) &&
127 (vap->va_vcnstart <= vcn) && (vap->va_vcnend >= vcn) &&
128 (vap->va_namelen == namelen) &&
129 (strncmp(name, vap->va_name, namelen) == 0)) {
131 ntfs_ntref(vap->va_ip);
134 if (vap->va_type == NTFS_A_ATTRLIST)
142 * Search attribute specifed in ntnode (load ntnode if nessecary).
143 * If not found but ATTR_A_ATTRLIST present, read it in and search throught.
144 * VOP_VGET node needed, and lookup througth it's ntnode (load if nessesary).
146 * ntnode should be locked
150 struct ntfsmount * ntmp,
155 struct ntvattr ** vapp)
157 struct ntvattr *lvap = NULL;
158 struct attr_attrlist *aalp;
159 struct attr_attrlist *nextaalp;
161 struct ntnode *newip;
169 dprintf(("ntfs_ntvattrget: " \
170 "ino: %d, type: 0x%x, name: %s, vcn: %d\n", \
171 ip->i_number, type, name, (u_int32_t) vcn));
172 namelen = strlen(name);
174 dprintf(("ntfs_ntvattrget: " \
175 "ino: %d, type: 0x%x, vcn: %d\n", \
176 ip->i_number, type, (u_int32_t) vcn));
181 error = ntfs_findvattr(ntmp, ip, &lvap, vapp, type, name, namelen, vcn);
186 dprintf(("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: " \
187 "ino: %d, type: 0x%x, name: %s, vcn: %d\n", \
188 ip->i_number, type, name, (u_int32_t) vcn));
191 /* Scan $ATTRIBUTE_LIST for requested attribute */
192 len = lvap->va_datalen;
193 alpool = malloc(len, M_TEMP, M_WAITOK);
194 error = ntfs_readntvattr_plain(ntmp, ip, lvap, 0, len, alpool, &len,
199 aalp = (struct attr_attrlist *) alpool;
202 for(; len > 0; aalp = nextaalp) {
203 dprintf(("ntfs_ntvattrget: " \
204 "attrlist: ino: %d, attr: 0x%x, vcn: %d\n", \
205 aalp->al_inumber, aalp->al_type, \
206 (u_int32_t) aalp->al_vcnstart));
208 if (len > aalp->reclen) {
209 nextaalp = NTFS_NEXTREC(aalp, struct attr_attrlist *);
215 if (!NTFS_AALPCMP(aalp, type, name, namelen) ||
216 (nextaalp && (nextaalp->al_vcnstart <= vcn) &&
217 NTFS_AALPCMP(nextaalp, type, name, namelen)))
220 dprintf(("ntfs_ntvattrget: attribute in ino: %d\n",
223 /* this is not a main record, so we can't use just plain
225 error = ntfs_vgetex(ntmp->ntm_mountp, aalp->al_inumber,
226 NTFS_A_DATA, NULL, LK_EXCLUSIVE,
227 VG_EXT, curthread, &newvp);
229 printf("ntfs_ntvattrget: CAN'T VGET INO: %d\n",
233 newip = VTONT(newvp);
234 /* XXX have to lock ntnode */
235 error = ntfs_findvattr(ntmp, newip, &lvap, vapp,
236 type, name, namelen, vcn);
240 printf("ntfs_ntvattrget: ATTRLIST ERROR.\n");
245 dprintf(("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: " \
246 "ino: %d, type: 0x%x, name: %.*s, vcn: %d\n", \
247 ip->i_number, type, (int) namelen, name, (u_int32_t) vcn));
249 free(alpool, M_TEMP);
254 * Read ntnode from disk, make ntvattr list.
256 * ntnode should be locked
260 struct ntfsmount * ntmp,
263 struct filerec *mfrp;
267 struct ntvattr *nvap;
269 dprintf(("ntfs_loadntnode: loading ino: %d\n",ip->i_number));
271 mfrp = malloc(ntfs_bntob(ntmp->ntm_bpmftrec),
274 if (ip->i_number < NTFS_SYSNODESNUM) {
277 dprintf(("ntfs_loadntnode: read system node\n"));
279 bn = ntfs_cntobn(ntmp->ntm_mftcn) +
280 ntmp->ntm_bpmftrec * ip->i_number;
282 error = bread(ntmp->ntm_devvp,
283 bn, ntfs_bntob(ntmp->ntm_bpmftrec),
286 printf("ntfs_loadntnode: BREAD FAILED\n");
290 memcpy(mfrp, bp->b_data, ntfs_bntob(ntmp->ntm_bpmftrec));
295 vp = ntmp->ntm_sysvn[NTFS_MFTINO];
296 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
297 ip->i_number * ntfs_bntob(ntmp->ntm_bpmftrec),
298 ntfs_bntob(ntmp->ntm_bpmftrec), mfrp, NULL);
300 printf("ntfs_loadntnode: ntfs_readattr failed\n");
305 /* Check if magic and fixups are correct */
306 error = ntfs_procfixups(ntmp, NTFS_FILEMAGIC, (caddr_t)mfrp,
307 ntfs_bntob(ntmp->ntm_bpmftrec));
309 printf("ntfs_loadntnode: BAD MFT RECORD %d\n",
310 (u_int32_t) ip->i_number);
314 dprintf(("ntfs_loadntnode: load attrs for ino: %d\n",ip->i_number));
315 off = mfrp->fr_attroff;
316 ap = (struct attr *) ((caddr_t)mfrp + off);
318 LIST_INIT(&ip->i_valist);
320 while (ap->a_hdr.a_type != -1) {
321 error = ntfs_attrtontvattr(ntmp, &nvap, ap);
326 LIST_INSERT_HEAD(&ip->i_valist, nvap, va_list);
328 off += ap->a_hdr.reclen;
329 ap = (struct attr *) ((caddr_t)mfrp + off);
332 printf("ntfs_loadntnode: failed to load attr ino: %d\n",
337 ip->i_mainrec = mfrp->fr_mainrec;
338 ip->i_nlink = mfrp->fr_nlink;
339 ip->i_frflag = mfrp->fr_flags;
341 ip->i_flag |= IN_LOADED;
349 * Routine locks ntnode and increase usecount, just opposite of
356 dprintf(("ntfs_ntget: get ntnode %d: %p, usecount: %d\n",
357 ip->i_number, ip, ip->i_usecount));
359 mtx_lock(&ip->i_interlock);
361 lockmgr(&ip->i_lock, LK_EXCLUSIVE | LK_INTERLOCK, &ip->i_interlock);
367 * Routine search ntnode in hash, if found: lock, inc usecount and return.
368 * If not in hash allocate structure for ntnode, prefill it, lock,
369 * inc count and return.
371 * ntnode returned locked
375 struct ntfsmount * ntmp,
377 struct ntnode ** ipp)
381 dprintf(("ntfs_ntlookup: looking for ntnode %d\n", ino));
384 ip = ntfs_nthashlookup(ntmp->ntm_devvp->v_rdev, ino);
387 dprintf(("ntfs_ntlookup: ntnode %d: %p, usecount: %d\n",
388 ino, ip, ip->i_usecount));
392 } while (lockmgr(&ntfs_hashlock, LK_EXCLUSIVE | LK_SLEEPFAIL, NULL));
394 ip = malloc(sizeof(struct ntnode), M_NTFSNTNODE,
396 ddprintf(("ntfs_ntlookup: allocating ntnode: %d: %p\n", ino, ip));
398 /* Generic initialization */
399 ip->i_devvp = ntmp->ntm_devvp;
400 ip->i_dev = ntmp->ntm_devvp->v_rdev;
404 LIST_INIT(&ip->i_fnlist);
407 /* init lock and lock the newborn ntnode */
408 lockinit(&ip->i_lock, PINOD, "ntnode", 0, 0);
409 mtx_init(&ip->i_interlock, "ntnode interlock", NULL, MTX_DEF);
414 lockmgr(&ntfs_hashlock, LK_RELEASE, NULL);
418 dprintf(("ntfs_ntlookup: ntnode %d: %p, usecount: %d\n",
419 ino, ip, ip->i_usecount));
425 * Decrement usecount of ntnode and unlock it, if usecount reach zero,
428 * ntnode should be locked on entry, and unlocked on return.
436 dprintf(("ntfs_ntput: rele ntnode %d: %p, usecount: %d\n",
437 ip->i_number, ip, ip->i_usecount));
439 mtx_lock(&ip->i_interlock);
443 if (ip->i_usecount < 0) {
444 panic("ntfs_ntput: ino: %d usecount: %d \n",
445 ip->i_number,ip->i_usecount);
449 if (ip->i_usecount > 0) {
450 lockmgr(&ip->i_lock, LK_RELEASE|LK_INTERLOCK, &ip->i_interlock);
454 dprintf(("ntfs_ntput: deallocating ntnode: %d\n", ip->i_number));
456 if (LIST_FIRST(&ip->i_fnlist))
457 panic("ntfs_ntput: ntnode has fnodes\n");
461 while ((vap = LIST_FIRST(&ip->i_valist)) != NULL) {
462 LIST_REMOVE(vap,va_list);
463 ntfs_freentvattr(vap);
465 lockmgr(&ip->i_lock, LK_RELEASE | LK_INTERLOCK, &ip->i_interlock);
466 mtx_destroy(&ip->i_interlock);
467 lockdestroy(&ip->i_lock);
469 free(ip, M_NTFSNTNODE);
473 * increment usecount of ntnode
479 mtx_lock(&ip->i_interlock);
481 mtx_unlock(&ip->i_interlock);
483 dprintf(("ntfs_ntref: ino %d, usecount: %d\n",
484 ip->i_number, ip->i_usecount));
489 * Decrement usecount of ntnode.
495 dprintf(("ntfs_ntrele: rele ntnode %d: %p, usecount: %d\n",
496 ip->i_number, ip, ip->i_usecount));
498 mtx_lock(&ip->i_interlock);
501 if (ip->i_usecount < 0)
502 panic("ntfs_ntrele: ino: %d usecount: %d \n",
503 ip->i_number,ip->i_usecount);
504 mtx_unlock(&ip->i_interlock);
508 * Deallocate all memory allocated for ntvattr
511 ntfs_freentvattr(vap)
512 struct ntvattr * vap;
514 if (vap->va_flag & NTFS_AF_INRUN) {
516 free(vap->va_vruncn, M_NTFSRUN);
518 free(vap->va_vruncl, M_NTFSRUN);
521 free(vap->va_datap, M_NTFSRDATA);
523 free(vap, M_NTFSNTVATTR);
527 * Convert disk image of attribute into ntvattr structure,
528 * runs are expanded also.
532 struct ntfsmount * ntmp,
533 struct ntvattr ** rvapp,
542 vap = malloc(sizeof(struct ntvattr),
543 M_NTFSNTVATTR, M_WAITOK | M_ZERO);
545 vap->va_flag = rap->a_hdr.a_flag;
546 vap->va_type = rap->a_hdr.a_type;
547 vap->va_compression = rap->a_hdr.a_compression;
548 vap->va_index = rap->a_hdr.a_index;
550 ddprintf(("type: 0x%x, index: %d", vap->va_type, vap->va_index));
552 vap->va_namelen = rap->a_hdr.a_namelen;
553 if (rap->a_hdr.a_namelen) {
554 wchar *unp = (wchar *) ((caddr_t) rap + rap->a_hdr.a_nameoff);
555 ddprintf((", name:["));
556 for (i = 0; i < vap->va_namelen; i++) {
557 vap->va_name[i] = unp[i];
558 ddprintf(("%c", vap->va_name[i]));
562 if (vap->va_flag & NTFS_AF_INRUN) {
563 ddprintf((", nonres."));
564 vap->va_datalen = rap->a_nr.a_datalen;
565 vap->va_allocated = rap->a_nr.a_allocated;
566 vap->va_vcnstart = rap->a_nr.a_vcnstart;
567 vap->va_vcnend = rap->a_nr.a_vcnend;
568 vap->va_compressalg = rap->a_nr.a_compressalg;
569 error = ntfs_runtovrun(&(vap->va_vruncn), &(vap->va_vruncl),
571 (caddr_t) rap + rap->a_nr.a_dataoff);
573 vap->va_compressalg = 0;
574 ddprintf((", res."));
575 vap->va_datalen = rap->a_r.a_datalen;
576 vap->va_allocated = rap->a_r.a_datalen;
577 vap->va_vcnstart = 0;
578 vap->va_vcnend = ntfs_btocn(vap->va_allocated);
579 vap->va_datap = malloc(vap->va_datalen,
580 M_NTFSRDATA, M_WAITOK);
581 memcpy(vap->va_datap, (caddr_t) rap + rap->a_r.a_dataoff,
584 ddprintf((", len: %d", vap->va_datalen));
587 free(vap, M_NTFSNTVATTR);
597 * Expand run into more utilizable and more memory eating format.
618 off += (run[off] & 0xF) + ((run[off] >> 4) & 0xF) + 1;
621 cn = malloc(cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
622 cl = malloc(cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
632 for (i = 0; i < (sz & 0xF); i++)
633 cl[cnt] += (u_int32_t) run[off++] << (i << 3);
636 if (run[off + sz - 1] & 0x80) {
637 tmp = ((u_int64_t) - 1) << (sz << 3);
638 for (i = 0; i < sz; i++)
639 tmp |= (u_int64_t) run[off++] << (i << 3);
642 for (i = 0; i < sz; i++)
643 tmp |= (u_int64_t) run[off++] << (i << 3);
646 prev = cn[cnt] = prev + tmp;
659 * Compare unicode and ascii string case insens.
662 ntfs_uastricmp(ntmp, ustr, ustrlen, astr, astrlen)
663 struct ntfsmount *ntmp;
669 const char *astrp = astr;
672 size_t i, j, mbstrlen = astrlen;
674 if (ntmp->ntm_ic_l2u) {
675 for (i = 0, j = 0; i < ustrlen && j < astrlen; i++) {
677 res = ((int) NTFS_TOUPPER(ustr[i])) -
678 ((int)NTFS_TOUPPER(NTFS_82U(astrp, &len)));
688 * We use NTFS_82U(NTFS_U28(c)) to get rid of unicode
689 * symbols not covered by translation table
691 for (i = 0; i < ustrlen && i < astrlen; i++) {
692 res = ((int) NTFS_TOUPPER(NTFS_82U(NTFS_U28(ustr[i]), &len))) -
693 ((int)NTFS_TOUPPER(NTFS_82U(astrp, &len)));
699 return (ustrlen - mbstrlen);
703 * Compare unicode and ascii string case sens.
706 ntfs_uastrcmp(ntmp, ustr, ustrlen, astr, astrlen)
707 struct ntfsmount *ntmp;
714 size_t i, j, mbstrlen = astrlen;
717 for (i = 0, j = 0; (i < ustrlen) && (j < astrlen); i++, mbstrlen++) {
718 c = NTFS_U28(ustr[i]);
720 res = (int) (*c++ - astr[j++]);
726 return (ustrlen - mbstrlen);
730 * Search fnode in ntnode, if not found allocate and preinitialize.
732 * ntnode should be locked on entry.
736 struct ntfsmount *ntmp,
744 dprintf(("ntfs_fget: ino: %d, attrtype: 0x%x, attrname: %s\n",
745 ip->i_number,attrtype, attrname?attrname:""));
747 LIST_FOREACH(fp, &ip->i_fnlist, f_fnlist){
748 dprintf(("ntfs_fget: fnode: attrtype: %d, attrname: %s\n",
749 fp->f_attrtype, fp->f_attrname?fp->f_attrname:""));
751 if ((attrtype == fp->f_attrtype) &&
752 ((!attrname && !fp->f_attrname) ||
753 (attrname && fp->f_attrname &&
754 !strcmp(attrname,fp->f_attrname)))){
755 dprintf(("ntfs_fget: found existed: %p\n",fp));
763 fp = malloc(sizeof(struct fnode), M_NTFSFNODE,
765 dprintf(("ntfs_fget: allocating fnode: %p\n",fp));
769 fp->f_flag |= FN_AATTRNAME;
770 fp->f_attrname = malloc(strlen(attrname)+1, M_TEMP, M_WAITOK);
771 strcpy(fp->f_attrname, attrname);
773 fp->f_attrname = NULL;
774 fp->f_attrtype = attrtype;
778 LIST_INSERT_HEAD(&ip->i_fnlist, fp, f_fnlist);
786 * Deallocate fnode, remove it from ntnode's fnode list.
788 * ntnode should be locked.
794 struct ntnode *ip = FTONT(fp);
796 dprintf(("ntfs_frele: fnode: %p for %d: %p\n", fp, ip->i_number, ip));
798 dprintf(("ntfs_frele: deallocating fnode\n"));
799 LIST_REMOVE(fp,f_fnlist);
800 if (fp->f_flag & FN_AATTRNAME)
801 free(fp->f_attrname, M_TEMP);
803 free(fp->f_dirblbuf, M_NTFSDIR);
804 free(fp, M_NTFSFNODE);
809 * Lookup attribute name in format: [[:$ATTR_TYPE]:$ATTR_NAME],
810 * $ATTR_TYPE is searched in attrdefs read from $AttrDefs.
811 * If $ATTR_TYPE nott specifed, ATTR_A_DATA assumed.
815 struct ntfsmount * ntmp,
823 struct ntvattrdef *adp;
828 if (name[0] == '$') {
830 for (syslen = 0; syslen < namelen; syslen++) {
831 if(sys[syslen] == ':') {
841 for (i = 0; i < ntmp->ntm_adnum; i++, adp++){
842 if (syslen != adp->ad_namelen ||
843 strncmp(sys, adp->ad_name, syslen) != 0)
846 *attrtype = adp->ad_type;
851 *attrtype = NTFS_A_DATA;
855 (*attrname) = malloc(namelen, M_TEMP, M_WAITOK);
856 memcpy((*attrname), name, namelen);
857 (*attrname)[namelen] = '\0';
864 * Lookup specifed node for filename, matching cnp,
865 * return fnode filled.
869 struct ntfsmount * ntmp,
871 struct componentname * cnp,
874 struct fnode *fp = VTOF(vp);
875 struct ntnode *ip = FTONT(fp);
876 struct ntvattr *vap; /* Root attribute */
877 cn_t cn; /* VCN in current attribute */
878 caddr_t rdbuf; /* Buffer to read directory's blocks */
880 u_int64_t rdsize; /* Length of data to read from current block */
881 struct attr_indexentry *iep;
882 int error, res, anamelen, fnamelen;
883 const char *fname,*aname;
885 int attrtype = NTFS_A_DATA;
886 char *attrname = NULL;
891 error = ntfs_ntget(ip);
895 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
896 if (error || (vap->va_flag & NTFS_AF_INRUN))
899 blsize = vap->va_a_iroot->ir_size;
900 rdsize = vap->va_datalen;
903 * Divide file name into: foofilefoofilefoofile[:attrspec]
904 * Store like this: fname:fnamelen [aname:anamelen]
906 fname = cnp->cn_nameptr;
909 for (fnamelen = 0; fnamelen < cnp->cn_namelen; fnamelen++)
910 if(fname[fnamelen] == ':') {
911 aname = fname + fnamelen + 1;
912 anamelen = cnp->cn_namelen - fnamelen - 1;
913 dprintf(("ntfs_ntlookupfile: %s (%d), attr: %s (%d)\n",
914 fname, fnamelen, aname, anamelen));
918 dprintf(("ntfs_ntlookupfile: blksz: %d, rdsz: %jd\n", blsize, rdsize));
920 rdbuf = malloc(blsize, M_TEMP, M_WAITOK);
922 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXROOT, "$I30",
923 0, rdsize, rdbuf, NULL);
927 aoff = sizeof(struct attr_indexroot);
930 iep = (struct attr_indexentry *) (rdbuf + aoff);
932 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
934 iep = (struct attr_indexentry *) (rdbuf + aoff))
936 ddprintf(("scan: %d, %d\n",
937 (u_int32_t) iep->ie_number,
938 (u_int32_t) iep->ie_fnametype));
940 /* check the name - the case-insensitible check
941 * has to come first, to break from this for loop
942 * if needed, so we can dive correctly */
943 res = NTFS_UASTRICMP(iep->ie_fname, iep->ie_fnamelen,
946 if (res < 0) continue;
948 if (iep->ie_fnametype == 0 ||
949 !(ntmp->ntm_flag & NTFS_MFLAG_CASEINS))
951 res = NTFS_UASTRCMP(iep->ie_fname,
952 iep->ie_fnamelen, fname, fnamelen);
953 if (res != 0) continue;
957 error = ntfs_ntlookupattr(ntmp,
959 &attrtype, &attrname);
964 /* Check if we've found ourself */
965 if ((iep->ie_number == ip->i_number) &&
966 (attrtype == fp->f_attrtype) &&
967 ((!attrname && !fp->f_attrname) ||
968 (attrname && fp->f_attrname &&
969 !strcmp(attrname, fp->f_attrname))))
977 /* vget node, but don't load it */
978 error = ntfs_vgetex(ntmp->ntm_mountp,
979 iep->ie_number, attrtype, attrname,
980 LK_EXCLUSIVE, VG_DONTLOADIN | VG_DONTVALIDFN,
983 /* free the buffer returned by ntfs_ntlookupattr() */
985 free(attrname, M_TEMP);
994 if (nfp->f_flag & FN_VALID) {
999 nfp->f_fflag = iep->ie_fflag;
1000 nfp->f_pnumber = iep->ie_fpnumber;
1001 nfp->f_times = iep->ie_ftimes;
1003 if((nfp->f_fflag & NTFS_FFLAG_DIR) &&
1004 (nfp->f_attrtype == NTFS_A_DATA) &&
1005 (nfp->f_attrname == NULL))
1010 nvp->v_type = f_type;
1012 if ((nfp->f_attrtype == NTFS_A_DATA) &&
1013 (nfp->f_attrname == NULL))
1015 /* Opening default attribute */
1016 nfp->f_size = iep->ie_fsize;
1017 nfp->f_allocated = iep->ie_fallocated;
1018 nfp->f_flag |= FN_PRELOADED;
1020 error = ntfs_filesize(ntmp, nfp,
1021 &nfp->f_size, &nfp->f_allocated);
1028 nfp->f_flag &= ~FN_VALID;
1033 /* Dive if possible */
1034 if (iep->ie_flag & NTFS_IEFLAG_SUBNODE) {
1035 dprintf(("ntfs_ntlookupfile: diving\n"));
1037 cn = *(cn_t *) (rdbuf + aoff +
1038 iep->reclen - sizeof(cn_t));
1041 error = ntfs_readattr(ntmp, ip, NTFS_A_INDX, "$I30",
1042 ntfs_cntob(cn), rdsize, rdbuf, NULL);
1046 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1051 aoff = (((struct attr_indexalloc *) rdbuf)->ia_hdrsize +
1054 dprintf(("ntfs_ntlookupfile: nowhere to dive :-(\n"));
1060 dprintf(("finish\n"));
1063 if (attrname) free(attrname, M_TEMP);
1064 ntfs_ntvattrrele(vap);
1066 free(rdbuf, M_TEMP);
1071 * Check if name type is permitted to show.
1074 ntfs_isnamepermitted(
1075 struct ntfsmount * ntmp,
1076 struct attr_indexentry * iep)
1078 if (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)
1081 switch (iep->ie_fnametype) {
1083 ddprintf(("ntfs_isnamepermitted: skipped DOS name\n"));
1085 case 0: case 1: case 3:
1088 printf("ntfs_isnamepermitted: " \
1089 "WARNING! Unknown file name type: %d\n",
1097 * Read ntfs dir like stream of attr_indexentry, not like btree of them.
1098 * This is done by scaning $BITMAP:$I30 for busy clusters and reading them.
1099 * Ofcouse $INDEX_ROOT:$I30 is read before. Last read values are stored in
1100 * fnode, so we can skip toward record number num almost immediatly.
1101 * Anyway this is rather slow routine. The problem is that we don't know
1102 * how many records are there in $INDEX_ALLOCATION:$I30 block.
1106 struct ntfsmount * ntmp,
1109 struct attr_indexentry ** riepp)
1111 struct ntnode *ip = FTONT(fp);
1112 struct ntvattr *vap = NULL; /* IndexRoot attribute */
1113 struct ntvattr *bmvap = NULL; /* BitMap attribute */
1114 struct ntvattr *iavap = NULL; /* IndexAllocation attribute */
1115 caddr_t rdbuf; /* Buffer to read directory's blocks */
1116 u_int8_t *bmp = NULL; /* Bitmap */
1117 u_int32_t blsize; /* Index allocation size (2048) */
1118 u_int32_t rdsize; /* Length of data to read */
1119 u_int32_t attrnum; /* Current attribute type */
1120 u_int32_t cpbl = 1; /* Clusters per directory block */
1122 struct attr_indexentry *iep;
1124 u_int32_t aoff, cnum;
1126 dprintf(("ntfs_ntreaddir: read ino: %d, num: %d\n", ip->i_number, num));
1127 error = ntfs_ntget(ip);
1131 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
1135 if (fp->f_dirblbuf == NULL) {
1136 fp->f_dirblsz = vap->va_a_iroot->ir_size;
1137 fp->f_dirblbuf = malloc(max(vap->va_datalen,fp->f_dirblsz),
1138 M_NTFSDIR, M_WAITOK);
1141 blsize = fp->f_dirblsz;
1142 rdbuf = fp->f_dirblbuf;
1144 dprintf(("ntfs_ntreaddir: rdbuf: %p, blsize: %d\n", rdbuf, blsize));
1146 if (vap->va_a_iroot->ir_flag & NTFS_IRFLAG_INDXALLOC) {
1147 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXBITMAP, "$I30",
1153 bmp = malloc(bmvap->va_datalen, M_TEMP, M_WAITOK);
1154 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXBITMAP, "$I30", 0,
1155 bmvap->va_datalen, bmp, NULL);
1159 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDX, "$I30",
1165 cpbl = ntfs_btocn(blsize + ntfs_cntob(1) - 1);
1166 dprintf(("ntfs_ntreaddir: indexalloc: %jd, cpbl: %d\n",
1167 iavap->va_datalen, cpbl));
1169 dprintf(("ntfs_ntreadidir: w/o BitMap and IndexAllocation\n"));
1170 iavap = bmvap = NULL;
1174 /* Try use previous values */
1175 if ((fp->f_lastdnum < num) && (fp->f_lastdnum != 0)) {
1176 attrnum = fp->f_lastdattr;
1177 aoff = fp->f_lastdoff;
1178 blnum = fp->f_lastdblnum;
1179 cnum = fp->f_lastdnum;
1181 attrnum = NTFS_A_INDXROOT;
1182 aoff = sizeof(struct attr_indexroot);
1188 dprintf(("ntfs_ntreaddir: scan: 0x%x, %d, %d, %d, %d\n",
1189 attrnum, (u_int32_t) blnum, cnum, num, aoff));
1190 rdsize = (attrnum == NTFS_A_INDXROOT) ? vap->va_datalen : blsize;
1191 error = ntfs_readattr(ntmp, ip, attrnum, "$I30",
1192 ntfs_cntob(blnum * cpbl), rdsize, rdbuf, NULL);
1196 if (attrnum == NTFS_A_INDX) {
1197 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1203 aoff = (attrnum == NTFS_A_INDX) ?
1204 (0x18 + ((struct attr_indexalloc *) rdbuf)->ia_hdrsize) :
1205 sizeof(struct attr_indexroot);
1207 iep = (struct attr_indexentry *) (rdbuf + aoff);
1208 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
1209 aoff += iep->reclen,
1210 iep = (struct attr_indexentry *) (rdbuf + aoff))
1212 if (!ntfs_isnamepermitted(ntmp, iep)) continue;
1215 fp->f_lastdnum = cnum;
1216 fp->f_lastdoff = aoff;
1217 fp->f_lastdblnum = blnum;
1218 fp->f_lastdattr = attrnum;
1229 if (attrnum == NTFS_A_INDXROOT)
1234 while (ntfs_cntob(blnum * cpbl) < iavap->va_datalen) {
1235 if (bmp[blnum >> 3] & (1 << (blnum & 7)))
1240 attrnum = NTFS_A_INDX;
1242 if (ntfs_cntob(blnum * cpbl) >= iavap->va_datalen)
1244 dprintf(("ntfs_ntreaddir: blnum: %d\n", (u_int32_t) blnum));
1253 ntfs_ntvattrrele(vap);
1255 ntfs_ntvattrrele(bmvap);
1257 ntfs_ntvattrrele(iavap);
1265 * Convert NTFS times that are in 100 ns units and begins from
1266 * 1601 Jan 1 into unix times.
1274 /* WindowNT times are in 100 ns and from 1601 Jan 1 */
1275 t.tv_nsec = (nt % (1000 * 1000 * 10)) * 100;
1276 t.tv_sec = nt / (1000 * 1000 * 10) -
1277 369LL * 365LL * 24LL * 60LL * 60LL -
1278 89LL * 1LL * 24LL * 60LL * 60LL;
1283 * Get file times from NTFS_A_NAME attribute.
1287 struct ntfsmount * ntmp,
1291 struct ntvattr *vap;
1294 dprintf(("ntfs_times: ino: %d...\n", ip->i_number));
1296 error = ntfs_ntget(ip);
1300 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_NAME, NULL, 0, &vap);
1305 *tm = vap->va_a_name->n_times;
1306 ntfs_ntvattrrele(vap);
1313 * Get file sizes from corresponding attribute.
1315 * ntnode under fnode should be locked.
1319 struct ntfsmount * ntmp,
1324 struct ntvattr *vap;
1325 struct ntnode *ip = FTONT(fp);
1329 dprintf(("ntfs_filesize: ino: %d\n", ip->i_number));
1331 error = ntfs_ntvattrget(ntmp, ip,
1332 fp->f_attrtype, fp->f_attrname, 0, &vap);
1336 bn = vap->va_allocated;
1337 sz = vap->va_datalen;
1339 dprintf(("ntfs_filesize: %d bytes (%d bytes allocated)\n",
1340 (u_int32_t) sz, (u_int32_t) bn));
1347 ntfs_ntvattrrele(vap);
1353 * This is one of write routine.
1356 ntfs_writeattr_plain(
1357 struct ntfsmount * ntmp,
1369 off_t off = roff, left = rsize, towrite;
1370 caddr_t data = rdata;
1371 struct ntvattr *vap;
1375 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1376 ntfs_btocn(off), &vap);
1379 towrite = MIN(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1380 ddprintf(("ntfs_writeattr_plain: o: %d, s: %d (%d - %d)\n",
1381 (u_int32_t) off, (u_int32_t) towrite,
1382 (u_int32_t) vap->va_vcnstart,
1383 (u_int32_t) vap->va_vcnend));
1384 error = ntfs_writentvattr_plain(ntmp, ip, vap,
1385 off - ntfs_cntob(vap->va_vcnstart),
1386 towrite, data, &init, uio);
1388 printf("ntfs_writeattr_plain: " \
1389 "ntfs_writentvattr_plain failed: o: %d, s: %d\n",
1390 (u_int32_t) off, (u_int32_t) towrite);
1391 printf("ntfs_writeattr_plain: attrib: %d - %d\n",
1392 (u_int32_t) vap->va_vcnstart,
1393 (u_int32_t) vap->va_vcnend);
1394 ntfs_ntvattrrele(vap);
1397 ntfs_ntvattrrele(vap);
1400 data = data + towrite;
1408 * This is one of write routine.
1410 * ntnode should be locked.
1413 ntfs_writentvattr_plain(
1414 struct ntfsmount * ntmp,
1416 struct ntvattr * vap,
1426 cn_t ccn, ccl, cn, left, cl;
1427 caddr_t data = rdata;
1433 if ((vap->va_flag & NTFS_AF_INRUN) == 0) {
1434 printf("ntfs_writevattr_plain: CAN'T WRITE RES. ATTRIBUTE\n");
1438 ddprintf(("ntfs_writentvattr_plain: data in run: %ld chains\n",
1446 for (; left && (cnt < vap->va_vruncnt); cnt++) {
1447 ccn = vap->va_vruncn[cnt];
1448 ccl = vap->va_vruncl[cnt];
1450 ddprintf(("ntfs_writentvattr_plain: " \
1451 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1452 (u_int32_t) left, (u_int32_t) ccn, \
1453 (u_int32_t) ccl, (u_int32_t) off));
1455 if (ntfs_cntob(ccl) < off) {
1456 off -= ntfs_cntob(ccl);
1460 if (!ccn && ip->i_number != NTFS_BOOTINO)
1463 ccl -= ntfs_btocn(off);
1464 cn = ccn + ntfs_btocn(off);
1465 off = ntfs_btocnoff(off);
1467 while (left && ccl) {
1469 * Always read and write single clusters at a time -
1470 * we need to avoid requesting differently-sized
1471 * blocks at the same disk offsets to avoid
1472 * confusing the buffer cache.
1474 tocopy = MIN(left, ntfs_cntob(1) - off);
1475 cl = ntfs_btocl(tocopy + off);
1476 KASSERT(cl == 1 && tocopy <= ntfs_cntob(1),
1477 ("single cluster limit mistake"));
1478 ddprintf(("ntfs_writentvattr_plain: write: " \
1479 "cn: 0x%x cl: %d, off: %d len: %d, left: %d\n",
1480 (u_int32_t) cn, (u_int32_t) cl,
1481 (u_int32_t) off, (u_int32_t) tocopy,
1483 if ((off == 0) && (tocopy == ntfs_cntob(cl)))
1485 bp = getblk(ntmp->ntm_devvp, ntfs_cntobn(cn),
1486 ntfs_cntob(cl), 0, 0, 0);
1489 error = bread(ntmp->ntm_devvp, ntfs_cntobn(cn),
1490 ntfs_cntob(cl), NOCRED, &bp);
1497 uiomove(bp->b_data + off, tocopy, uio);
1499 memcpy(bp->b_data + off, data, tocopy);
1501 data = data + tocopy;
1511 printf("ntfs_writentvattr_plain: POSSIBLE RUN ERROR\n");
1519 * This is one of read routines.
1521 * ntnode should be locked.
1524 ntfs_readntvattr_plain(
1525 struct ntfsmount * ntmp,
1527 struct ntvattr * vap,
1538 if (vap->va_flag & NTFS_AF_INRUN) {
1540 cn_t ccn, ccl, cn, left, cl;
1541 caddr_t data = rdata;
1545 ddprintf(("ntfs_readntvattr_plain: data in run: %ld chains\n",
1553 while (left && (cnt < vap->va_vruncnt)) {
1554 ccn = vap->va_vruncn[cnt];
1555 ccl = vap->va_vruncl[cnt];
1557 ddprintf(("ntfs_readntvattr_plain: " \
1558 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1559 (u_int32_t) left, (u_int32_t) ccn, \
1560 (u_int32_t) ccl, (u_int32_t) off));
1562 if (ntfs_cntob(ccl) < off) {
1563 off -= ntfs_cntob(ccl);
1567 if (ccn || ip->i_number == NTFS_BOOTINO) {
1568 ccl -= ntfs_btocn(off);
1569 cn = ccn + ntfs_btocn(off);
1570 off = ntfs_btocnoff(off);
1572 while (left && ccl) {
1574 * Always read single clusters at a
1575 * time - we need to avoid reading
1576 * differently-sized blocks at the
1577 * same disk offsets to avoid
1578 * confusing the buffer cache.
1581 ntfs_cntob(1) - off);
1582 cl = ntfs_btocl(tocopy + off);
1584 tocopy <= ntfs_cntob(1),
1585 ("single cluster limit mistake"));
1587 ddprintf(("ntfs_readntvattr_plain: " \
1588 "read: cn: 0x%x cl: %d, " \
1589 "off: %d len: %d, left: %d\n",
1595 error = bread(ntmp->ntm_devvp,
1604 uiomove(bp->b_data + off,
1607 memcpy(data, bp->b_data + off,
1611 data = data + tocopy;
1619 tocopy = MIN(left, ntfs_cntob(ccl) - off);
1620 ddprintf(("ntfs_readntvattr_plain: "
1621 "hole: ccn: 0x%x ccl: %d, off: %d, " \
1622 " len: %d, left: %d\n",
1623 (u_int32_t) ccn, (u_int32_t) ccl,
1624 (u_int32_t) off, (u_int32_t) tocopy,
1629 size_t remains = tocopy;
1630 for(; remains; remains--)
1631 uiomove("", 1, uio);
1633 bzero(data, tocopy);
1634 data = data + tocopy;
1639 printf("ntfs_readntvattr_plain: POSSIBLE RUN ERROR\n");
1643 ddprintf(("ntfs_readnvattr_plain: data is in mft record\n"));
1645 uiomove(vap->va_datap + roff, rsize, uio);
1647 memcpy(rdata, vap->va_datap + roff, rsize);
1655 * This is one of read routines.
1658 ntfs_readattr_plain(
1659 struct ntfsmount * ntmp,
1671 off_t off = roff, left = rsize, toread;
1672 caddr_t data = rdata;
1673 struct ntvattr *vap;
1677 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1678 ntfs_btocn(off), &vap);
1681 toread = MIN(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1682 ddprintf(("ntfs_readattr_plain: o: %d, s: %d (%d - %d)\n",
1683 (u_int32_t) off, (u_int32_t) toread,
1684 (u_int32_t) vap->va_vcnstart,
1685 (u_int32_t) vap->va_vcnend));
1686 error = ntfs_readntvattr_plain(ntmp, ip, vap,
1687 off - ntfs_cntob(vap->va_vcnstart),
1688 toread, data, &init, uio);
1690 printf("ntfs_readattr_plain: " \
1691 "ntfs_readntvattr_plain failed: o: %d, s: %d\n",
1692 (u_int32_t) off, (u_int32_t) toread);
1693 printf("ntfs_readattr_plain: attrib: %d - %d\n",
1694 (u_int32_t) vap->va_vcnstart,
1695 (u_int32_t) vap->va_vcnend);
1696 ntfs_ntvattrrele(vap);
1699 ntfs_ntvattrrele(vap);
1702 data = data + toread;
1710 * This is one of read routines.
1714 struct ntfsmount * ntmp,
1724 struct ntvattr *vap;
1727 ddprintf(("ntfs_readattr: reading %d: 0x%x, from %d size %d bytes\n",
1728 ip->i_number, attrnum, (u_int32_t) roff, (u_int32_t) rsize));
1730 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname, 0, &vap);
1734 if ((roff > vap->va_datalen) ||
1735 (roff + rsize > vap->va_datalen)) {
1736 ddprintf(("ntfs_readattr: offset too big\n"));
1737 ntfs_ntvattrrele(vap);
1740 if (vap->va_compression && vap->va_compressalg) {
1743 off_t off = roff, left = rsize, tocopy;
1744 caddr_t data = rdata;
1747 ddprintf(("ntfs_ntreadattr: compression: %d\n",
1748 vap->va_compressalg));
1750 cup = malloc(ntfs_cntob(NTFS_COMPUNIT_CL),
1751 M_NTFSDECOMP, M_WAITOK);
1752 uup = malloc(ntfs_cntob(NTFS_COMPUNIT_CL),
1753 M_NTFSDECOMP, M_WAITOK);
1755 cn = (ntfs_btocn(roff)) & (~(NTFS_COMPUNIT_CL - 1));
1756 off = roff - ntfs_cntob(cn);
1759 error = ntfs_readattr_plain(ntmp, ip, attrnum,
1760 attrname, ntfs_cntob(cn),
1761 ntfs_cntob(NTFS_COMPUNIT_CL),
1766 tocopy = MIN(left, ntfs_cntob(NTFS_COMPUNIT_CL) - off);
1768 if (init == ntfs_cntob(NTFS_COMPUNIT_CL)) {
1770 uiomove(cup + off, tocopy, uio);
1772 memcpy(data, cup + off, tocopy);
1773 } else if (init == 0) {
1775 size_t remains = tocopy;
1776 for(; remains; remains--)
1777 uiomove("", 1, uio);
1780 bzero(data, tocopy);
1782 error = ntfs_uncompunit(ntmp, uup, cup);
1786 uiomove(uup + off, tocopy, uio);
1788 memcpy(data, uup + off, tocopy);
1792 data = data + tocopy;
1793 off += tocopy - ntfs_cntob(NTFS_COMPUNIT_CL);
1794 cn += NTFS_COMPUNIT_CL;
1797 free(uup, M_NTFSDECOMP);
1798 free(cup, M_NTFSDECOMP);
1800 error = ntfs_readattr_plain(ntmp, ip, attrnum, attrname,
1801 roff, rsize, rdata, &init, uio);
1802 ntfs_ntvattrrele(vap);
1819 printf("ntfs_parsetun: run == NULL\n");
1824 printf("ntfs_parserun: trying to go out of run\n");
1828 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1829 printf("ntfs_parserun: " \
1830 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1834 for (i = 0; i < (sz & 0xF); i++)
1835 *cl += (u_int32_t) run[(*off)++] << (i << 3);
1838 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1839 printf("ntfs_parserun: " \
1840 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1844 for (i = 0; i < (sz & 0xF); i++)
1845 *cn += (u_int32_t) run[(*off)++] << (i << 3);
1852 * Process fixup routine on given buffer.
1856 struct ntfsmount * ntmp,
1861 struct fixuphdr *fhp = (struct fixuphdr *) buf;
1867 if (fhp->fh_magic != magic) {
1868 printf("ntfs_procfixups: magic doesn't match: %08x != %08x\n",
1869 fhp->fh_magic, magic);
1872 if ((fhp->fh_fnum - 1) * ntmp->ntm_bps != len) {
1873 printf("ntfs_procfixups: " \
1874 "bad fixups number: %d for %ld bytes block\n",
1875 fhp->fh_fnum, (long)len); /* XXX printf kludge */
1878 if (fhp->fh_foff >= ntmp->ntm_spc * ntmp->ntm_mftrecsz * ntmp->ntm_bps) {
1879 printf("ntfs_procfixups: invalid offset: %x", fhp->fh_foff);
1882 fxp = (u_int16_t *) (buf + fhp->fh_foff);
1883 cfxp = (u_int16_t *) (buf + ntmp->ntm_bps - 2);
1885 for (i = 1; i < fhp->fh_fnum; i++, fxp++) {
1886 if (*cfxp != fixup) {
1887 printf("ntfs_procfixups: fixup %d doesn't match\n", i);
1891 cfxp = (u_int16_t *) ((caddr_t) cfxp + ntmp->ntm_bps);
1900 struct ntfsmount * ntmp,
1912 printf("ntfs_runtocn: run: %p, %ld bytes, vcn:%ld\n",
1913 run, len, (u_long) vcn);
1914 printf("ntfs_runtocn: run: ");
1915 for (i = 0; i < len; i++)
1916 printf("0x%02x ", run[i]);
1921 printf("ntfs_runtocn: run == NULL\n");
1925 if (run[off] == 0) {
1926 printf("ntfs_runtocn: vcn too big\n");
1930 error = ntfs_parserun(&ccn, &ccl, run, len, &off);
1932 printf("ntfs_runtocn: ntfs_parserun failed\n");
1935 } while (ccl <= vcn);
1942 * this initializes toupper table & dependant variables to be ready for
1948 ntfs_toupper_tab = (wchar *) NULL;
1949 lockinit(&ntfs_toupper_lock, PVFS, "ntfs_toupper", 0, 0);
1950 ntfs_toupper_usecount = 0;
1954 ntfs_toupper_destroy(void)
1957 lockdestroy(&ntfs_toupper_lock);
1961 * if the ntfs_toupper_tab[] is filled already, just raise use count;
1962 * otherwise read the data from the filesystem we are currently mounting
1965 ntfs_toupper_use(mp, ntmp)
1967 struct ntfsmount *ntmp;
1972 /* get exclusive access */
1973 lockmgr(&ntfs_toupper_lock, LK_EXCLUSIVE, NULL);
1975 /* only read the translation data from a file if it hasn't been
1977 if (ntfs_toupper_tab)
1981 * Read in Unicode lowercase -> uppercase translation file.
1982 * XXX for now, just the first 256 entries are used anyway,
1983 * so don't bother reading more
1985 ntfs_toupper_tab = malloc(65536 * sizeof(wchar),
1986 M_NTFSRDATA, M_WAITOK);
1988 if ((error = VFS_VGET(mp, NTFS_UPCASEINO, LK_EXCLUSIVE, &vp)))
1990 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
1991 0, 65536*sizeof(wchar), (char *) ntfs_toupper_tab, NULL);
1995 ntfs_toupper_usecount++;
1996 lockmgr(&ntfs_toupper_lock, LK_RELEASE, NULL);
2001 * lower the use count and if it reaches zero, free the memory
2002 * tied by toupper table
2005 ntfs_toupper_unuse()
2007 /* get exclusive access */
2008 lockmgr(&ntfs_toupper_lock, LK_EXCLUSIVE, NULL);
2010 ntfs_toupper_usecount--;
2011 if (ntfs_toupper_usecount == 0) {
2012 free(ntfs_toupper_tab, M_NTFSRDATA);
2013 ntfs_toupper_tab = NULL;
2016 else if (ntfs_toupper_usecount < 0) {
2017 panic("ntfs_toupper_unuse(): use count negative: %d\n",
2018 ntfs_toupper_usecount);
2022 /* release the lock */
2023 lockmgr(&ntfs_toupper_lock, LK_RELEASE, NULL);
2028 struct ntfsmount *ntmp,
2036 if (ntfs_iconv && cs_local) {
2037 ntfs_iconv->open(cs_local, cs_ntfs, &ntmp->ntm_ic_u2l);
2041 u28 = malloc(256 * sizeof(char*), M_TEMP, M_WAITOK | M_ZERO);
2043 for (i=0; i<256; i++) {
2044 h = (u2w[i] >> 8) & 0xFF;
2047 if (u28[h] == NULL) {
2048 u28[h] = malloc(256 * sizeof(char), M_TEMP, M_WAITOK);
2049 for (j=0; j<256; j++)
2053 u28[h][l] = i & 0xFF;
2056 ntmp->ntm_u28 = u28;
2062 ntfs_u28_uninit(struct ntfsmount *ntmp)
2067 if (ntmp->ntm_u28 == NULL) {
2068 if (ntfs_iconv && ntmp->ntm_ic_u2l) {
2069 ntfs_iconv->close(ntmp->ntm_ic_u2l);
2074 u28 = ntmp->ntm_u28;
2076 for (i=0; i<256; i++)
2078 free(u28[i], M_TEMP);
2087 struct ntfsmount *ntmp,
2094 if (ntfs_iconv && cs_local) {
2095 ntfs_iconv->open(cs_ntfs, cs_local, &ntmp->ntm_ic_l2u);
2099 _82u = malloc(256 * sizeof(wchar), M_TEMP, M_WAITOK);
2101 for (i=0; i<256; i++)
2104 ntmp->ntm_82u = _82u;
2110 ntfs_82u_uninit(struct ntfsmount *ntmp)
2113 if (ntmp->ntm_82u == NULL) {
2114 if (ntfs_iconv && ntmp->ntm_ic_l2u) {
2115 ntfs_iconv->close(ntmp->ntm_ic_l2u);
2120 free(ntmp->ntm_82u, M_TEMP);
2125 * maps the Unicode char to local character
2130 struct ntfsmount *ntmp,
2133 char *p, *outp, inbuf[3];
2137 if (ntfs_iconv && ntmp->ntm_ic_u2l) {
2141 inbuf[0] = (char)(wc>>8);
2142 inbuf[1] = (char)wc;
2145 ntfs_iconv->convchr(ntmp->ntm_ic_u2l, (const char **)&p, &ilen,
2154 p = ntmp->ntm_u28[(wc>>8)&0xFF];
2155 outbuf[0] = (p == NULL) ? '_' : p[wc&0xFF] & 0xFF;
2162 struct ntfsmount *ntmp,
2166 char *outp, outbuf[3];
2170 if (ntfs_iconv && ntmp->ntm_ic_l2u) {
2171 ilen = (size_t)*len;
2175 ntfs_iconv->convchr(ntmp->ntm_ic_l2u, &c, &ilen, &outp, &olen);
2177 uc = (wchar)((outbuf[0]<<8) | (outbuf[1]&0xFF));
2182 if (ntmp->ntm_82u != NULL)
2183 return (ntmp->ntm_82u[*c&0xFF]);