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.14 2004/07/02 15:26:26 joerg 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) )
86 ntfs_ntvattrrele(struct ntvattr *vap)
88 dprintf(("ntfs_ntvattrrele: ino: %d, type: 0x%x\n",
89 vap->va_ip->i_number, vap->va_type));
91 ntfs_ntrele(vap->va_ip);
97 * find the attribute in the ntnode
100 ntfs_findvattr(struct ntfsmount *ntmp, struct ntnode *ip,
101 struct ntvattr **lvapp, struct ntvattr **vapp, u_int32_t type,
102 const char *name, size_t namelen, cn_t vcn)
107 if((ip->i_flag & IN_LOADED) == 0) {
108 dprintf(("ntfs_findvattr: node not loaded, ino: %d\n",
110 error = ntfs_loadntnode(ntmp,ip);
112 printf("ntfs_findvattr: FAILED TO LOAD INO: %d\n",
120 for (vap = ip->i_valist.lh_first; vap; vap = vap->va_list.le_next) {
121 ddprintf(("ntfs_findvattr: type: 0x%x, vcn: %d - %d\n", \
122 vap->va_type, (u_int32_t) vap->va_vcnstart, \
123 (u_int32_t) vap->va_vcnend));
124 if ((vap->va_type == type) &&
125 (vap->va_vcnstart <= vcn) && (vap->va_vcnend >= vcn) &&
126 (vap->va_namelen == namelen) &&
127 (strncmp(name, vap->va_name, namelen) == 0)) {
129 ntfs_ntref(vap->va_ip);
132 if (vap->va_type == NTFS_A_ATTRLIST)
140 * Search attribute specifed in ntnode (load ntnode if nessecary).
141 * If not found but ATTR_A_ATTRLIST present, read it in and search throught.
142 * VOP_VGET node needed, and lookup througth it's ntnode (load if nessesary).
144 * ntnode should be locked
147 ntfs_ntvattrget(struct ntfsmount *ntmp, struct ntnode *ip, u_int32_t type,
148 const char *name, cn_t vcn, struct ntvattr **vapp)
150 struct ntvattr *lvap = NULL;
151 struct attr_attrlist *aalp;
152 struct attr_attrlist *nextaalp;
154 struct ntnode *newip;
162 dprintf(("ntfs_ntvattrget: " \
163 "ino: %d, type: 0x%x, name: %s, vcn: %d\n", \
164 ip->i_number, type, name, (u_int32_t) vcn));
165 namelen = strlen(name);
167 dprintf(("ntfs_ntvattrget: " \
168 "ino: %d, type: 0x%x, vcn: %d\n", \
169 ip->i_number, type, (u_int32_t) vcn));
174 error = ntfs_findvattr(ntmp, ip, &lvap, vapp, type, name, namelen, vcn);
179 dprintf(("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: " \
180 "ino: %d, type: 0x%x, name: %s, vcn: %d\n", \
181 ip->i_number, type, name, (u_int32_t) vcn));
184 /* Scan $ATTRIBUTE_LIST for requested attribute */
185 len = lvap->va_datalen;
186 MALLOC(alpool, caddr_t, len, M_TEMP, M_WAITOK);
187 error = ntfs_readntvattr_plain(ntmp, ip, lvap, 0, len, alpool, &len,
192 aalp = (struct attr_attrlist *) alpool;
195 for(; len > 0; aalp = nextaalp) {
196 dprintf(("ntfs_ntvattrget: " \
197 "attrlist: ino: %d, attr: 0x%x, vcn: %d\n", \
198 aalp->al_inumber, aalp->al_type, \
199 (u_int32_t) aalp->al_vcnstart));
201 if (len > aalp->reclen) {
202 nextaalp = NTFS_NEXTREC(aalp, struct attr_attrlist *);
208 if (!NTFS_AALPCMP(aalp, type, name, namelen) ||
209 (nextaalp && (nextaalp->al_vcnstart <= vcn) &&
210 NTFS_AALPCMP(nextaalp, type, name, namelen)))
213 dprintf(("ntfs_ntvattrget: attribute in ino: %d\n",
216 /* this is not a main record, so we can't use just plain
218 error = ntfs_vgetex(ntmp->ntm_mountp, aalp->al_inumber,
219 NTFS_A_DATA, NULL, LK_EXCLUSIVE,
220 VG_EXT, curthread, &newvp);
222 printf("ntfs_ntvattrget: CAN'T VGET INO: %d\n",
226 newip = VTONT(newvp);
227 /* XXX have to lock ntnode */
228 error = ntfs_findvattr(ntmp, newip, &lvap, vapp,
229 type, name, namelen, vcn);
233 printf("ntfs_ntvattrget: ATTRLIST ERROR.\n");
238 dprintf(("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: " \
239 "ino: %d, type: 0x%x, name: %.*s, vcn: %d\n", \
240 ip->i_number, type, (int) namelen, name, (u_int32_t) vcn));
242 FREE(alpool, M_TEMP);
247 * Read ntnode from disk, make ntvattr list.
249 * ntnode should be locked
252 ntfs_loadntnode(struct ntfsmount *ntmp, struct ntnode *ip)
254 struct filerec *mfrp;
258 struct ntvattr *nvap;
260 dprintf(("ntfs_loadntnode: loading ino: %d\n",ip->i_number));
262 MALLOC(mfrp, struct filerec *, ntfs_bntob(ntmp->ntm_bpmftrec),
265 if (ip->i_number < NTFS_SYSNODESNUM) {
268 dprintf(("ntfs_loadntnode: read system node\n"));
270 bn = ntfs_cntobn(ntmp->ntm_mftcn) +
271 ntmp->ntm_bpmftrec * ip->i_number;
273 error = bread(ntmp->ntm_devvp,
274 bn, ntfs_bntob(ntmp->ntm_bpmftrec), &bp);
276 printf("ntfs_loadntnode: BREAD FAILED\n");
280 memcpy(mfrp, bp->b_data, ntfs_bntob(ntmp->ntm_bpmftrec));
285 vp = ntmp->ntm_sysvn[NTFS_MFTINO];
286 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
287 ip->i_number * ntfs_bntob(ntmp->ntm_bpmftrec),
288 ntfs_bntob(ntmp->ntm_bpmftrec), mfrp, NULL);
290 printf("ntfs_loadntnode: ntfs_readattr failed\n");
295 /* Check if magic and fixups are correct */
296 error = ntfs_procfixups(ntmp, NTFS_FILEMAGIC, (caddr_t)mfrp,
297 ntfs_bntob(ntmp->ntm_bpmftrec));
299 printf("ntfs_loadntnode: BAD MFT RECORD %d\n",
300 (u_int32_t) ip->i_number);
304 dprintf(("ntfs_loadntnode: load attrs for ino: %d\n",ip->i_number));
305 off = mfrp->fr_attroff;
306 ap = (struct attr *) ((caddr_t)mfrp + off);
308 LIST_INIT(&ip->i_valist);
310 while (ap->a_hdr.a_type != -1) {
311 error = ntfs_attrtontvattr(ntmp, &nvap, ap);
316 LIST_INSERT_HEAD(&ip->i_valist, nvap, va_list);
318 off += ap->a_hdr.reclen;
319 ap = (struct attr *) ((caddr_t)mfrp + off);
322 printf("ntfs_loadntnode: failed to load attr ino: %d\n",
327 ip->i_mainrec = mfrp->fr_mainrec;
328 ip->i_nlink = mfrp->fr_nlink;
329 ip->i_frflag = mfrp->fr_flags;
331 ip->i_flag |= IN_LOADED;
339 * Routine locks ntnode and increase usecount, just opposite of
343 ntfs_ntget(struct ntnode *ip)
347 dprintf(("ntfs_ntget: get ntnode %d: %p, usecount: %d\n",
348 ip->i_number, ip, ip->i_usecount));
350 ip->i_usecount++; /* ZZZ */
351 lwkt_gettoken(&ilock, &ip->i_interlock);
352 LOCKMGR(&ip->i_lock, LK_EXCLUSIVE | LK_INTERLOCK, &ilock);
358 * Routine search ntnode in hash, if found: lock, inc usecount and return.
359 * If not in hash allocate structure for ntnode, prefill it, lock,
360 * inc count and return.
362 * ntnode returned locked
365 ntfs_ntlookup(struct ntfsmount *ntmp, ino_t ino, struct ntnode **ipp)
369 dprintf(("ntfs_ntlookup: looking for ntnode %d\n", ino));
372 if ((ip = ntfs_nthashlookup(ntmp->ntm_dev, ino)) != NULL) {
374 dprintf(("ntfs_ntlookup: ntnode %d: %p, usecount: %d\n",
375 ino, ip, ip->i_usecount));
379 } while (LOCKMGR(&ntfs_hashlock, LK_EXCLUSIVE | LK_SLEEPFAIL, NULL));
381 MALLOC(ip, struct ntnode *, sizeof(struct ntnode),
382 M_NTFSNTNODE, M_WAITOK);
383 ddprintf(("ntfs_ntlookup: allocating ntnode: %d: %p\n", ino, ip));
384 bzero((caddr_t) ip, sizeof(struct ntnode));
386 /* Generic initialization */
387 ip->i_devvp = ntmp->ntm_devvp;
388 ip->i_dev = ntmp->ntm_dev;
392 LIST_INIT(&ip->i_fnlist);
395 /* init lock and lock the newborn ntnode */
396 lockinit(&ip->i_lock, 0, "ntnode", 0, LK_EXCLUSIVE);
397 lwkt_token_init(&ip->i_interlock);
402 LOCKMGR(&ntfs_hashlock, LK_RELEASE, NULL);
406 dprintf(("ntfs_ntlookup: ntnode %d: %p, usecount: %d\n",
407 ino, ip, ip->i_usecount));
413 * Decrement usecount of ntnode and unlock it, if usecount reach zero,
416 * ntnode should be locked on entry, and unlocked on return.
419 ntfs_ntput(struct ntnode *ip)
424 dprintf(("ntfs_ntput: rele ntnode %d: %p, usecount: %d\n",
425 ip->i_number, ip, ip->i_usecount));
427 lwkt_gettoken(&ilock, &ip->i_interlock);
431 if (ip->i_usecount < 0) {
432 panic("ntfs_ntput: ino: %d usecount: %d \n",
433 ip->i_number,ip->i_usecount);
437 if (ip->i_usecount > 0) {
438 LOCKMGR(&ip->i_lock, LK_RELEASE|LK_INTERLOCK, &ilock);
442 dprintf(("ntfs_ntput: deallocating ntnode: %d\n", ip->i_number));
444 if (ip->i_fnlist.lh_first)
445 panic("ntfs_ntput: ntnode has fnodes\n");
449 while ((vap = LIST_FIRST(&ip->i_valist)) != NULL) {
450 LIST_REMOVE(vap,va_list);
451 ntfs_freentvattr(vap);
453 lwkt_reltoken(&ilock);
455 FREE(ip, M_NTFSNTNODE);
459 * increment usecount of ntnode
462 ntfs_ntref(struct ntnode *ip)
466 dprintf(("ntfs_ntref: ino %d, usecount: %d\n",
467 ip->i_number, ip->i_usecount));
472 * Decrement usecount of ntnode.
475 ntfs_ntrele(struct ntnode *ip)
479 dprintf(("ntfs_ntrele: rele ntnode %d: %p, usecount: %d\n",
480 ip->i_number, ip, ip->i_usecount));
482 lwkt_gettoken(&ilock, &ip->i_interlock);
485 if (ip->i_usecount < 0) {
486 panic("ntfs_ntrele: ino: %d usecount: %d \n",
487 ip->i_number,ip->i_usecount);
489 lwkt_reltoken(&ilock);
493 * Deallocate all memory allocated for ntvattr
496 ntfs_freentvattr(struct ntvattr *vap)
498 if (vap->va_flag & NTFS_AF_INRUN) {
500 FREE(vap->va_vruncn, M_NTFSRUN);
502 FREE(vap->va_vruncl, M_NTFSRUN);
505 FREE(vap->va_datap, M_NTFSRDATA);
507 FREE(vap, M_NTFSNTVATTR);
511 * Convert disk image of attribute into ntvattr structure,
512 * runs are expanded also.
515 ntfs_attrtontvattr(struct ntfsmount *ntmp, struct ntvattr **rvapp,
524 MALLOC(vap, struct ntvattr *, sizeof(struct ntvattr),
525 M_NTFSNTVATTR, M_WAITOK);
526 bzero(vap, sizeof(struct ntvattr));
528 vap->va_flag = rap->a_hdr.a_flag;
529 vap->va_type = rap->a_hdr.a_type;
530 vap->va_compression = rap->a_hdr.a_compression;
531 vap->va_index = rap->a_hdr.a_index;
533 ddprintf(("type: 0x%x, index: %d", vap->va_type, vap->va_index));
535 vap->va_namelen = rap->a_hdr.a_namelen;
536 if (rap->a_hdr.a_namelen) {
537 wchar *unp = (wchar *) ((caddr_t) rap + rap->a_hdr.a_nameoff);
538 ddprintf((", name:["));
539 for (i = 0; i < vap->va_namelen; i++) {
540 vap->va_name[i] = unp[i];
541 ddprintf(("%c", vap->va_name[i]));
545 if (vap->va_flag & NTFS_AF_INRUN) {
546 ddprintf((", nonres."));
547 vap->va_datalen = rap->a_nr.a_datalen;
548 vap->va_allocated = rap->a_nr.a_allocated;
549 vap->va_vcnstart = rap->a_nr.a_vcnstart;
550 vap->va_vcnend = rap->a_nr.a_vcnend;
551 vap->va_compressalg = rap->a_nr.a_compressalg;
552 error = ntfs_runtovrun(&(vap->va_vruncn), &(vap->va_vruncl),
554 (caddr_t) rap + rap->a_nr.a_dataoff);
556 vap->va_compressalg = 0;
557 ddprintf((", res."));
558 vap->va_datalen = rap->a_r.a_datalen;
559 vap->va_allocated = rap->a_r.a_datalen;
560 vap->va_vcnstart = 0;
561 vap->va_vcnend = ntfs_btocn(vap->va_allocated);
562 MALLOC(vap->va_datap, caddr_t, vap->va_datalen,
563 M_NTFSRDATA, M_WAITOK);
564 memcpy(vap->va_datap, (caddr_t) rap + rap->a_r.a_dataoff,
567 ddprintf((", len: %d", vap->va_datalen));
570 FREE(vap, M_NTFSNTVATTR);
580 * Expand run into more utilizable and more memory eating format.
583 ntfs_runtovrun(cn_t **rcnp, cn_t **rclp, u_long *rcntp, u_int8_t *run)
597 off += (run[off] & 0xF) + ((run[off] >> 4) & 0xF) + 1;
600 MALLOC(cn, cn_t *, cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
601 MALLOC(cl, cn_t *, cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
611 for (i = 0; i < (sz & 0xF); i++)
612 cl[cnt] += (u_int32_t) run[off++] << (i << 3);
615 if (run[off + sz - 1] & 0x80) {
616 tmp = ((u_int64_t) - 1) << (sz << 3);
617 for (i = 0; i < sz; i++)
618 tmp |= (u_int64_t) run[off++] << (i << 3);
621 for (i = 0; i < sz; i++)
622 tmp |= (u_int64_t) run[off++] << (i << 3);
625 prev = cn[cnt] = prev + tmp;
638 * Compare unicode and ascii string case insens.
641 ntfs_uastricmp(struct ntfsmount *ntmp, const wchar *ustr, size_t ustrlen,
642 const char *astr, size_t astrlen)
648 * XXX We use NTFS_82U(NTFS_U28(c)) to get rid of unicode
649 * symbols not covered by translation table
651 for (i = 0; i < ustrlen && i < astrlen; i++) {
652 res = ((int) NTFS_TOUPPER(NTFS_82U(NTFS_U28(ustr[i])))) -
653 ((int)NTFS_TOUPPER(NTFS_82U(astr[i])));
657 return (ustrlen - astrlen);
661 * Compare unicode and ascii string case sens.
664 ntfs_uastrcmp(struct ntfsmount *ntmp, const wchar *ustr, size_t ustrlen,
665 const char *astr, size_t astrlen)
670 for (i = 0; (i < ustrlen) && (i < astrlen); i++) {
671 res = (int) (((char)NTFS_U28(ustr[i])) - astr[i]);
675 return (ustrlen - astrlen);
679 * Search fnode in ntnode, if not found allocate and preinitialize.
681 * ntnode should be locked on entry.
684 ntfs_fget(struct ntfsmount *ntmp, struct ntnode *ip, int attrtype,
685 char *attrname, struct fnode **fpp)
689 dprintf(("ntfs_fget: ino: %d, attrtype: 0x%x, attrname: %s\n",
690 ip->i_number,attrtype, attrname?attrname:""));
692 for (fp = ip->i_fnlist.lh_first; fp != NULL; fp = fp->f_fnlist.le_next){
693 dprintf(("ntfs_fget: fnode: attrtype: %d, attrname: %s\n",
694 fp->f_attrtype, fp->f_attrname?fp->f_attrname:""));
696 if ((attrtype == fp->f_attrtype) &&
697 ((!attrname && !fp->f_attrname) ||
698 (attrname && fp->f_attrname &&
699 !strcmp(attrname,fp->f_attrname)))){
700 dprintf(("ntfs_fget: found existed: %p\n",fp));
708 MALLOC(fp, struct fnode *, sizeof(struct fnode), M_NTFSFNODE, M_WAITOK);
709 bzero(fp, sizeof(struct fnode));
710 dprintf(("ntfs_fget: allocating fnode: %p\n",fp));
714 fp->f_flag |= FN_AATTRNAME;
715 MALLOC(fp->f_attrname, char *, strlen(attrname)+1, M_TEMP, M_WAITOK);
716 strcpy(fp->f_attrname, attrname);
718 fp->f_attrname = NULL;
719 fp->f_attrtype = attrtype;
723 LIST_INSERT_HEAD(&ip->i_fnlist, fp, f_fnlist);
731 * Deallocate fnode, remove it from ntnode's fnode list.
733 * ntnode should be locked.
736 ntfs_frele(struct fnode *fp)
738 struct ntnode *ip = FTONT(fp);
740 dprintf(("ntfs_frele: fnode: %p for %d: %p\n", fp, ip->i_number, ip));
742 dprintf(("ntfs_frele: deallocating fnode\n"));
743 LIST_REMOVE(fp,f_fnlist);
744 if (fp->f_flag & FN_AATTRNAME)
745 FREE(fp->f_attrname, M_TEMP);
747 FREE(fp->f_dirblbuf, M_NTFSDIR);
748 FREE(fp, M_NTFSFNODE);
753 * Lookup attribute name in format: [[:$ATTR_TYPE]:$ATTR_NAME],
754 * $ATTR_TYPE is searched in attrdefs read from $AttrDefs.
755 * If $ATTR_TYPE nott specifed, ATTR_A_DATA assumed.
758 ntfs_ntlookupattr(struct ntfsmount *ntmp, const char *name, int namelen,
759 int *attrtype, char **attrname)
763 struct ntvattrdef *adp;
768 if (name[0] == '$') {
770 for (syslen = 0; syslen < namelen; syslen++) {
771 if(sys[syslen] == ':') {
781 for (i = 0; i < ntmp->ntm_adnum; i++, adp++){
782 if (syslen != adp->ad_namelen ||
783 strncmp(sys, adp->ad_name, syslen) != 0)
786 *attrtype = adp->ad_type;
791 *attrtype = NTFS_A_DATA;
795 MALLOC((*attrname), char *, namelen, M_TEMP, M_WAITOK);
796 memcpy((*attrname), name, namelen);
797 (*attrname)[namelen] = '\0';
804 * Lookup specifed node for filename, matching cnp,
805 * return fnode filled.
808 ntfs_ntlookupfile(struct ntfsmount *ntmp, struct vnode *vp,
809 struct componentname *cnp, struct vnode **vpp)
811 struct fnode *fp = VTOF(vp);
812 struct ntnode *ip = FTONT(fp);
813 struct ntvattr *vap; /* Root attribute */
814 cn_t cn; /* VCN in current attribute */
815 caddr_t rdbuf; /* Buffer to read directory's blocks */
817 u_int32_t rdsize; /* Length of data to read from current block */
818 struct attr_indexentry *iep;
819 int error, res, anamelen, fnamelen;
820 const char *fname,*aname;
822 int attrtype = NTFS_A_DATA;
823 char *attrname = NULL;
828 error = ntfs_ntget(ip);
832 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
833 if (error || (vap->va_flag & NTFS_AF_INRUN))
836 blsize = vap->va_a_iroot->ir_size;
837 rdsize = vap->va_datalen;
840 * Divide file name into: foofilefoofilefoofile[:attrspec]
841 * Store like this: fname:fnamelen [aname:anamelen]
843 fname = cnp->cn_nameptr;
846 for (fnamelen = 0; fnamelen < cnp->cn_namelen; fnamelen++)
847 if(fname[fnamelen] == ':') {
848 aname = fname + fnamelen + 1;
849 anamelen = cnp->cn_namelen - fnamelen - 1;
850 dprintf(("ntfs_ntlookupfile: %s (%d), attr: %s (%d)\n",
851 fname, fnamelen, aname, anamelen));
855 dprintf(("ntfs_ntlookupfile: blksz: %d, rdsz: %d\n", blsize, rdsize));
857 MALLOC(rdbuf, caddr_t, blsize, M_TEMP, M_WAITOK);
859 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXROOT, "$I30",
860 0, rdsize, rdbuf, NULL);
864 aoff = sizeof(struct attr_indexroot);
867 iep = (struct attr_indexentry *) (rdbuf + aoff);
869 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
871 iep = (struct attr_indexentry *) (rdbuf + aoff))
873 ddprintf(("scan: %d, %d\n",
874 (u_int32_t) iep->ie_number,
875 (u_int32_t) iep->ie_fnametype));
877 /* check the name - the case-insensitible check
878 * has to come first, to break from this for loop
879 * if needed, so we can dive correctly */
880 res = NTFS_UASTRICMP(iep->ie_fname, iep->ie_fnamelen,
883 if (res < 0) continue;
885 if (iep->ie_fnametype == 0 ||
886 !(ntmp->ntm_flag & NTFS_MFLAG_CASEINS))
888 res = NTFS_UASTRCMP(iep->ie_fname,
889 iep->ie_fnamelen, fname, fnamelen);
890 if (res != 0) continue;
894 error = ntfs_ntlookupattr(ntmp,
896 &attrtype, &attrname);
901 /* Check if we've found ourself */
902 if ((iep->ie_number == ip->i_number) &&
903 (attrtype == fp->f_attrtype) &&
904 ((!attrname && !fp->f_attrname) ||
905 (attrname && fp->f_attrname &&
906 !strcmp(attrname, fp->f_attrname))))
914 /* vget node, but don't load it */
915 error = ntfs_vgetex(ntmp->ntm_mountp,
916 iep->ie_number, attrtype, attrname,
917 LK_EXCLUSIVE, VG_DONTLOADIN | VG_DONTVALIDFN,
920 /* free the buffer returned by ntfs_ntlookupattr() */
922 FREE(attrname, M_TEMP);
931 if (nfp->f_flag & FN_VALID) {
936 nfp->f_fflag = iep->ie_fflag;
937 nfp->f_pnumber = iep->ie_fpnumber;
938 nfp->f_times = iep->ie_ftimes;
940 if((nfp->f_fflag & NTFS_FFLAG_DIR) &&
941 (nfp->f_attrtype == NTFS_A_DATA) &&
942 (nfp->f_attrname == NULL))
947 nvp->v_type = f_type;
949 if ((nfp->f_attrtype == NTFS_A_DATA) &&
950 (nfp->f_attrname == NULL))
952 /* Opening default attribute */
953 nfp->f_size = iep->ie_fsize;
954 nfp->f_allocated = iep->ie_fallocated;
955 nfp->f_flag |= FN_PRELOADED;
957 error = ntfs_filesize(ntmp, nfp,
958 &nfp->f_size, &nfp->f_allocated);
965 nfp->f_flag &= ~FN_VALID;
970 /* Dive if possible */
971 if (iep->ie_flag & NTFS_IEFLAG_SUBNODE) {
972 dprintf(("ntfs_ntlookupfile: diving\n"));
974 cn = *(cn_t *) (rdbuf + aoff +
975 iep->reclen - sizeof(cn_t));
978 error = ntfs_readattr(ntmp, ip, NTFS_A_INDX, "$I30",
979 ntfs_cntob(cn), rdsize, rdbuf, NULL);
983 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
988 aoff = (((struct attr_indexalloc *) rdbuf)->ia_hdrsize +
991 dprintf(("ntfs_ntlookupfile: nowhere to dive :-(\n"));
997 dprintf(("finish\n"));
1000 if (attrname) FREE(attrname, M_TEMP);
1001 ntfs_ntvattrrele(vap);
1003 FREE(rdbuf, M_TEMP);
1008 * Check if name type is permitted to show.
1011 ntfs_isnamepermitted(struct ntfsmount *ntmp, struct attr_indexentry *iep)
1013 if (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)
1016 switch (iep->ie_fnametype) {
1018 ddprintf(("ntfs_isnamepermitted: skiped DOS name\n"));
1020 case 0: case 1: case 3:
1023 printf("ntfs_isnamepermitted: " \
1024 "WARNING! Unknown file name type: %d\n",
1032 * Read ntfs dir like stream of attr_indexentry, not like btree of them.
1033 * This is done by scaning $BITMAP:$I30 for busy clusters and reading them.
1034 * Ofcouse $INDEX_ROOT:$I30 is read before. Last read values are stored in
1035 * fnode, so we can skip toward record number num almost immediatly.
1036 * Anyway this is rather slow routine. The problem is that we don't know
1037 * how many records are there in $INDEX_ALLOCATION:$I30 block.
1040 ntfs_ntreaddir(struct ntfsmount *ntmp, struct fnode *fp,
1041 u_int32_t num, struct attr_indexentry **riepp)
1043 struct ntnode *ip = FTONT(fp);
1044 struct ntvattr *vap = NULL; /* IndexRoot attribute */
1045 struct ntvattr *bmvap = NULL; /* BitMap attribute */
1046 struct ntvattr *iavap = NULL; /* IndexAllocation attribute */
1047 caddr_t rdbuf; /* Buffer to read directory's blocks */
1048 u_char *bmp = NULL; /* Bitmap */
1049 u_int32_t blsize; /* Index allocation size (2048) */
1050 u_int32_t rdsize; /* Length of data to read */
1051 u_int32_t attrnum; /* Current attribute type */
1052 u_int32_t cpbl = 1; /* Clusters per directory block */
1054 struct attr_indexentry *iep;
1056 u_int32_t aoff, cnum;
1058 dprintf(("ntfs_ntreaddir: read ino: %d, num: %d\n", ip->i_number, num));
1059 error = ntfs_ntget(ip);
1063 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
1067 if (fp->f_dirblbuf == NULL) {
1068 fp->f_dirblsz = vap->va_a_iroot->ir_size;
1069 MALLOC(fp->f_dirblbuf, caddr_t,
1070 max(vap->va_datalen,fp->f_dirblsz), M_NTFSDIR, M_WAITOK);
1073 blsize = fp->f_dirblsz;
1074 rdbuf = fp->f_dirblbuf;
1076 dprintf(("ntfs_ntreaddir: rdbuf: 0x%p, blsize: %d\n", rdbuf, blsize));
1078 if (vap->va_a_iroot->ir_flag & NTFS_IRFLAG_INDXALLOC) {
1079 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXBITMAP, "$I30",
1085 MALLOC(bmp, u_char *, bmvap->va_datalen, M_TEMP, M_WAITOK);
1086 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXBITMAP, "$I30", 0,
1087 bmvap->va_datalen, bmp, NULL);
1091 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDX, "$I30",
1097 cpbl = ntfs_btocn(blsize + ntfs_cntob(1) - 1);
1098 dprintf(("ntfs_ntreaddir: indexalloc: %d, cpbl: %d\n",
1099 iavap->va_datalen, cpbl));
1101 dprintf(("ntfs_ntreadidir: w/o BitMap and IndexAllocation\n"));
1102 iavap = bmvap = NULL;
1106 /* Try use previous values */
1107 if ((fp->f_lastdnum < num) && (fp->f_lastdnum != 0)) {
1108 attrnum = fp->f_lastdattr;
1109 aoff = fp->f_lastdoff;
1110 blnum = fp->f_lastdblnum;
1111 cnum = fp->f_lastdnum;
1113 attrnum = NTFS_A_INDXROOT;
1114 aoff = sizeof(struct attr_indexroot);
1120 dprintf(("ntfs_ntreaddir: scan: 0x%x, %d, %d, %d, %d\n",
1121 attrnum, (u_int32_t) blnum, cnum, num, aoff));
1122 rdsize = (attrnum == NTFS_A_INDXROOT) ? vap->va_datalen : blsize;
1123 error = ntfs_readattr(ntmp, ip, attrnum, "$I30",
1124 ntfs_cntob(blnum * cpbl), rdsize, rdbuf, NULL);
1128 if (attrnum == NTFS_A_INDX) {
1129 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1135 aoff = (attrnum == NTFS_A_INDX) ?
1136 (0x18 + ((struct attr_indexalloc *) rdbuf)->ia_hdrsize) :
1137 sizeof(struct attr_indexroot);
1139 iep = (struct attr_indexentry *) (rdbuf + aoff);
1140 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
1141 aoff += iep->reclen,
1142 iep = (struct attr_indexentry *) (rdbuf + aoff))
1144 if (!ntfs_isnamepermitted(ntmp, iep)) continue;
1147 fp->f_lastdnum = cnum;
1148 fp->f_lastdoff = aoff;
1149 fp->f_lastdblnum = blnum;
1150 fp->f_lastdattr = attrnum;
1161 if (attrnum == NTFS_A_INDXROOT)
1166 while (ntfs_cntob(blnum * cpbl) < iavap->va_datalen) {
1167 if (bmp[blnum >> 3] & (1 << (blnum & 3)))
1172 attrnum = NTFS_A_INDX;
1174 if (ntfs_cntob(blnum * cpbl) >= iavap->va_datalen)
1176 dprintf(("ntfs_ntreaddir: blnum: %d\n", (u_int32_t) blnum));
1185 ntfs_ntvattrrele(vap);
1187 ntfs_ntvattrrele(bmvap);
1189 ntfs_ntvattrrele(iavap);
1197 * Convert NTFS times that are in 100 ns units and begins from
1198 * 1601 Jan 1 into unix times.
1201 ntfs_nttimetounix(u_int64_t nt)
1205 /* WindowNT times are in 100 ns and from 1601 Jan 1 */
1206 t.tv_nsec = (nt % (1000 * 1000 * 10)) * 100;
1207 t.tv_sec = nt / (1000 * 1000 * 10) -
1208 369LL * 365LL * 24LL * 60LL * 60LL -
1209 89LL * 1LL * 24LL * 60LL * 60LL;
1214 * Get file times from NTFS_A_NAME attribute.
1217 ntfs_times(struct ntfsmount *ntmp, struct ntnode *ip, ntfs_times_t *tm)
1219 struct ntvattr *vap;
1222 dprintf(("ntfs_times: ino: %d...\n", ip->i_number));
1224 error = ntfs_ntget(ip);
1228 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_NAME, NULL, 0, &vap);
1233 *tm = vap->va_a_name->n_times;
1234 ntfs_ntvattrrele(vap);
1241 * Get file sizes from corresponding attribute.
1243 * ntnode under fnode should be locked.
1246 ntfs_filesize(struct ntfsmount *ntmp, struct fnode *fp, u_int64_t *size,
1249 struct ntvattr *vap;
1250 struct ntnode *ip = FTONT(fp);
1254 dprintf(("ntfs_filesize: ino: %d\n", ip->i_number));
1256 error = ntfs_ntvattrget(ntmp, ip,
1257 fp->f_attrtype, fp->f_attrname, 0, &vap);
1261 bn = vap->va_allocated;
1262 sz = vap->va_datalen;
1264 dprintf(("ntfs_filesize: %d bytes (%d bytes allocated)\n",
1265 (u_int32_t) sz, (u_int32_t) bn));
1272 ntfs_ntvattrrele(vap);
1278 * This is one of write routine.
1281 ntfs_writeattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1282 u_int32_t attrnum, char *attrname, off_t roff,
1283 size_t rsize, void *rdata, size_t *initp,
1288 off_t off = roff, left = rsize, towrite;
1289 caddr_t data = rdata;
1290 struct ntvattr *vap;
1294 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1295 ntfs_btocn(off), &vap);
1298 towrite = min(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1299 ddprintf(("ntfs_writeattr_plain: o: %d, s: %d (%d - %d)\n",
1300 (u_int32_t) off, (u_int32_t) towrite,
1301 (u_int32_t) vap->va_vcnstart,
1302 (u_int32_t) vap->va_vcnend));
1303 error = ntfs_writentvattr_plain(ntmp, ip, vap,
1304 off - ntfs_cntob(vap->va_vcnstart),
1305 towrite, data, &init, uio);
1307 printf("ntfs_writeattr_plain: " \
1308 "ntfs_writentvattr_plain failed: o: %d, s: %d\n",
1309 (u_int32_t) off, (u_int32_t) towrite);
1310 printf("ntfs_writeattr_plain: attrib: %d - %d\n",
1311 (u_int32_t) vap->va_vcnstart,
1312 (u_int32_t) vap->va_vcnend);
1313 ntfs_ntvattrrele(vap);
1316 ntfs_ntvattrrele(vap);
1319 data = data + towrite;
1327 * This is one of write routine.
1329 * ntnode should be locked.
1332 ntfs_writentvattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1333 struct ntvattr *vap, off_t roff, size_t rsize,
1334 void *rdata, size_t *initp, struct uio *uio)
1339 cn_t ccn, ccl, cn, left, cl;
1340 caddr_t data = rdata;
1346 if ((vap->va_flag & NTFS_AF_INRUN) == 0) {
1347 printf("ntfs_writevattr_plain: CAN'T WRITE RES. ATTRIBUTE\n");
1351 ddprintf(("ntfs_writentvattr_plain: data in run: %ld chains\n",
1359 for (; left && (cnt < vap->va_vruncnt); cnt++) {
1360 ccn = vap->va_vruncn[cnt];
1361 ccl = vap->va_vruncl[cnt];
1363 ddprintf(("ntfs_writentvattr_plain: " \
1364 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1365 (u_int32_t) left, (u_int32_t) ccn, \
1366 (u_int32_t) ccl, (u_int32_t) off));
1368 if (ntfs_cntob(ccl) < off) {
1369 off -= ntfs_cntob(ccl);
1373 if (!ccn && ip->i_number != NTFS_BOOTINO)
1376 ccl -= ntfs_btocn(off);
1377 cn = ccn + ntfs_btocn(off);
1378 off = ntfs_btocnoff(off);
1380 while (left && ccl) {
1381 #if defined(__DragonFly__)
1383 min(ntfs_cntob(ccl) - off, MAXBSIZE - off));
1385 /* under NetBSD, bread() can read
1386 * maximum one block worth of data */
1387 tocopy = min(left, ntmp->ntm_bps - off);
1389 cl = ntfs_btocl(tocopy + off);
1390 ddprintf(("ntfs_writentvattr_plain: write: " \
1391 "cn: 0x%x cl: %d, off: %d len: %d, left: %d\n",
1392 (u_int32_t) cn, (u_int32_t) cl,
1393 (u_int32_t) off, (u_int32_t) tocopy,
1395 if ((off == 0) && (tocopy == ntfs_cntob(cl)))
1397 bp = getblk(ntmp->ntm_devvp, ntfs_cntobn(cn),
1398 ntfs_cntob(cl), 0, 0);
1401 error = bread(ntmp->ntm_devvp, ntfs_cntobn(cn),
1402 ntfs_cntob(cl), &bp);
1409 uiomove(bp->b_data + off, tocopy, uio);
1411 memcpy(bp->b_data + off, data, tocopy);
1413 data = data + tocopy;
1423 printf("ntfs_writentvattr_plain: POSSIBLE RUN ERROR\n");
1431 * This is one of read routines.
1433 * ntnode should be locked.
1436 ntfs_readntvattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1437 struct ntvattr *vap, off_t roff, size_t rsize,
1438 void *rdata, size_t *initp, struct uio *uio)
1444 if (vap->va_flag & NTFS_AF_INRUN) {
1446 cn_t ccn, ccl, cn, left, cl;
1447 caddr_t data = rdata;
1451 ddprintf(("ntfs_readntvattr_plain: data in run: %ld chains\n",
1459 while (left && (cnt < vap->va_vruncnt)) {
1460 ccn = vap->va_vruncn[cnt];
1461 ccl = vap->va_vruncl[cnt];
1463 ddprintf(("ntfs_readntvattr_plain: " \
1464 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1465 (u_int32_t) left, (u_int32_t) ccn, \
1466 (u_int32_t) ccl, (u_int32_t) off));
1468 if (ntfs_cntob(ccl) < off) {
1469 off -= ntfs_cntob(ccl);
1473 if (ccn || ip->i_number == NTFS_BOOTINO) {
1474 ccl -= ntfs_btocn(off);
1475 cn = ccn + ntfs_btocn(off);
1476 off = ntfs_btocnoff(off);
1478 while (left && ccl) {
1479 #if defined(__DragonFly__)
1481 min(ntfs_cntob(ccl) - off,
1484 /* under NetBSD, bread() can read
1485 * maximum one block worth of data */
1487 ntmp->ntm_bps - off);
1489 cl = ntfs_btocl(tocopy + off);
1490 ddprintf(("ntfs_readntvattr_plain: " \
1491 "read: cn: 0x%x cl: %d, " \
1492 "off: %d len: %d, left: %d\n",
1498 error = bread(ntmp->ntm_devvp,
1507 uiomove(bp->b_data + off,
1510 memcpy(data, bp->b_data + off,
1514 data = data + tocopy;
1522 tocopy = min(left, ntfs_cntob(ccl) - off);
1523 ddprintf(("ntfs_readntvattr_plain: "
1524 "hole: ccn: 0x%x ccl: %d, off: %d, " \
1525 " len: %d, left: %d\n",
1526 (u_int32_t) ccn, (u_int32_t) ccl,
1527 (u_int32_t) off, (u_int32_t) tocopy,
1532 size_t remains = tocopy;
1533 for(; remains; remains++)
1534 uiomove("", 1, uio);
1536 bzero(data, tocopy);
1537 data = data + tocopy;
1542 printf("ntfs_readntvattr_plain: POSSIBLE RUN ERROR\n");
1546 ddprintf(("ntfs_readnvattr_plain: data is in mft record\n"));
1548 uiomove(vap->va_datap + roff, rsize, uio);
1550 memcpy(rdata, vap->va_datap + roff, rsize);
1558 * This is one of read routines.
1561 ntfs_readattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1562 u_int32_t attrnum, char *attrname, off_t roff,
1563 size_t rsize, void *rdata, size_t * initp,
1568 off_t off = roff, left = rsize, toread;
1569 caddr_t data = rdata;
1570 struct ntvattr *vap;
1574 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1575 ntfs_btocn(off), &vap);
1578 toread = min(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1579 ddprintf(("ntfs_readattr_plain: o: %d, s: %d (%d - %d)\n",
1580 (u_int32_t) off, (u_int32_t) toread,
1581 (u_int32_t) vap->va_vcnstart,
1582 (u_int32_t) vap->va_vcnend));
1583 error = ntfs_readntvattr_plain(ntmp, ip, vap,
1584 off - ntfs_cntob(vap->va_vcnstart),
1585 toread, data, &init, uio);
1587 printf("ntfs_readattr_plain: " \
1588 "ntfs_readntvattr_plain failed: o: %d, s: %d\n",
1589 (u_int32_t) off, (u_int32_t) toread);
1590 printf("ntfs_readattr_plain: attrib: %d - %d\n",
1591 (u_int32_t) vap->va_vcnstart,
1592 (u_int32_t) vap->va_vcnend);
1593 ntfs_ntvattrrele(vap);
1596 ntfs_ntvattrrele(vap);
1599 data = data + toread;
1607 * This is one of read routines.
1610 ntfs_readattr(struct ntfsmount *ntmp, struct ntnode *ip, u_int32_t attrnum,
1611 char *attrname, off_t roff, size_t rsize, void *rdata,
1615 struct ntvattr *vap;
1618 ddprintf(("ntfs_readattr: reading %d: 0x%x, from %d size %d bytes\n",
1619 ip->i_number, attrnum, (u_int32_t) roff, (u_int32_t) rsize));
1621 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname, 0, &vap);
1625 if ((roff > vap->va_datalen) ||
1626 (roff + rsize > vap->va_datalen)) {
1627 ddprintf(("ntfs_readattr: offset too big\n"));
1628 ntfs_ntvattrrele(vap);
1631 if (vap->va_compression && vap->va_compressalg) {
1634 off_t off = roff, left = rsize, tocopy;
1635 caddr_t data = rdata;
1638 ddprintf(("ntfs_ntreadattr: compression: %d\n",
1639 vap->va_compressalg));
1641 MALLOC(cup, u_int8_t *, ntfs_cntob(NTFS_COMPUNIT_CL),
1642 M_NTFSDECOMP, M_WAITOK);
1643 MALLOC(uup, u_int8_t *, ntfs_cntob(NTFS_COMPUNIT_CL),
1644 M_NTFSDECOMP, M_WAITOK);
1646 cn = (ntfs_btocn(roff)) & (~(NTFS_COMPUNIT_CL - 1));
1647 off = roff - ntfs_cntob(cn);
1650 error = ntfs_readattr_plain(ntmp, ip, attrnum,
1651 attrname, ntfs_cntob(cn),
1652 ntfs_cntob(NTFS_COMPUNIT_CL),
1657 tocopy = min(left, ntfs_cntob(NTFS_COMPUNIT_CL) - off);
1659 if (init == ntfs_cntob(NTFS_COMPUNIT_CL)) {
1661 uiomove(cup + off, tocopy, uio);
1663 memcpy(data, cup + off, tocopy);
1664 } else if (init == 0) {
1666 size_t remains = tocopy;
1667 for(; remains; remains--)
1668 uiomove("", 1, uio);
1671 bzero(data, tocopy);
1673 error = ntfs_uncompunit(ntmp, uup, cup);
1677 uiomove(uup + off, tocopy, uio);
1679 memcpy(data, uup + off, tocopy);
1683 data = data + tocopy;
1684 off += tocopy - ntfs_cntob(NTFS_COMPUNIT_CL);
1685 cn += NTFS_COMPUNIT_CL;
1688 FREE(uup, M_NTFSDECOMP);
1689 FREE(cup, M_NTFSDECOMP);
1691 error = ntfs_readattr_plain(ntmp, ip, attrnum, attrname,
1692 roff, rsize, rdata, &init, uio);
1693 ntfs_ntvattrrele(vap);
1699 ntfs_parserun(cn_t *cn, cn_t *cl, u_int8_t *run, u_long len, u_long *off)
1705 printf("ntfs_parsetun: run == NULL\n");
1710 printf("ntfs_parserun: trying to go out of run\n");
1714 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1715 printf("ntfs_parserun: " \
1716 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1720 for (i = 0; i < (sz & 0xF); i++)
1721 *cl += (u_int32_t) run[(*off)++] << (i << 3);
1724 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1725 printf("ntfs_parserun: " \
1726 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1730 for (i = 0; i < (sz & 0xF); i++)
1731 *cn += (u_int32_t) run[(*off)++] << (i << 3);
1738 * Process fixup routine on given buffer.
1741 ntfs_procfixups(struct ntfsmount *ntmp, u_int32_t magic, caddr_t buf,
1744 struct fixuphdr *fhp = (struct fixuphdr *) buf;
1750 if (fhp->fh_magic != magic) {
1751 printf("ntfs_procfixups: magic doesn't match: %08x != %08x\n",
1752 fhp->fh_magic, magic);
1755 if ((fhp->fh_fnum - 1) * ntmp->ntm_bps != len) {
1756 printf("ntfs_procfixups: " \
1757 "bad fixups number: %d for %ld bytes block\n",
1758 fhp->fh_fnum, (long)len); /* XXX printf kludge */
1761 if (fhp->fh_foff >= ntmp->ntm_spc * ntmp->ntm_mftrecsz * ntmp->ntm_bps) {
1762 printf("ntfs_procfixups: invalid offset: %x", fhp->fh_foff);
1765 fxp = (u_int16_t *) (buf + fhp->fh_foff);
1766 cfxp = (u_int16_t *) (buf + ntmp->ntm_bps - 2);
1768 for (i = 1; i < fhp->fh_fnum; i++, fxp++) {
1769 if (*cfxp != fixup) {
1770 printf("ntfs_procfixups: fixup %d doesn't match\n", i);
1774 cfxp = (u_int16_t *)(((caddr_t) cfxp) + ntmp->ntm_bps);
1781 ntfs_runtocn(cn_t *cn, struct ntfsmount *ntmp, u_int8_t *run, u_long len,
1791 printf("ntfs_runtocn: run: 0x%p, %ld bytes, vcn:%ld\n",
1792 run, len, (u_long) vcn);
1793 printf("ntfs_runtocn: run: ");
1794 for (i = 0; i < len; i++)
1795 printf("0x%02x ", run[i]);
1800 printf("ntfs_runtocn: run == NULL\n");
1804 if (run[off] == 0) {
1805 printf("ntfs_runtocn: vcn too big\n");
1809 error = ntfs_parserun(&ccn, &ccl, run, len, &off);
1811 printf("ntfs_runtocn: ntfs_parserun failed\n");
1814 } while (ccl <= vcn);
1821 * this initializes toupper table & dependant variables to be ready for
1825 ntfs_toupper_init(void)
1827 ntfs_toupper_tab = (wchar *) NULL;
1828 lockinit(&ntfs_toupper_lock, 0, "ntfs_toupper", 0, 0);
1829 ntfs_toupper_usecount = 0;
1833 * if the ntfs_toupper_tab[] is filled already, just raise use count;
1834 * otherwise read the data from the filesystem we are currently mounting
1837 ntfs_toupper_use(struct mount *mp, struct ntfsmount *ntmp)
1842 /* get exclusive access */
1843 LOCKMGR(&ntfs_toupper_lock, LK_EXCLUSIVE, NULL);
1845 /* only read the translation data from a file if it hasn't been
1847 if (ntfs_toupper_tab)
1851 * Read in Unicode lowercase -> uppercase translation file.
1852 * XXX for now, just the first 256 entries are used anyway,
1853 * so don't bother reading more
1855 MALLOC(ntfs_toupper_tab, wchar *, 65536 * sizeof(wchar),
1856 M_NTFSRDATA, M_WAITOK);
1858 if ((error = VFS_VGET(mp, NTFS_UPCASEINO, &vp)))
1860 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
1861 0, 65536*sizeof(wchar), (char *) ntfs_toupper_tab, NULL);
1865 ntfs_toupper_usecount++;
1866 LOCKMGR(&ntfs_toupper_lock, LK_RELEASE, NULL);
1871 * lower the use count and if it reaches zero, free the memory
1872 * tied by toupper table
1875 ntfs_toupper_unuse(void)
1877 /* get exclusive access */
1878 LOCKMGR(&ntfs_toupper_lock, LK_EXCLUSIVE, NULL);
1880 ntfs_toupper_usecount--;
1881 if (ntfs_toupper_usecount == 0) {
1882 FREE(ntfs_toupper_tab, M_NTFSRDATA);
1883 ntfs_toupper_tab = NULL;
1886 else if (ntfs_toupper_usecount < 0) {
1887 panic("ntfs_toupper_unuse(): use count negative: %d\n",
1888 ntfs_toupper_usecount);
1892 /* release the lock */
1893 LOCKMGR(&ntfs_toupper_lock, LK_RELEASE, NULL);
1897 ntfs_u28_init(struct ntfsmount *ntmp, wchar *u2w)
1902 MALLOC(u28, char **, 256 * sizeof(char*), M_TEMP, M_WAITOK | M_ZERO);
1904 for (i=0; i<256; i++) {
1905 h = (u2w[i] >> 8) & 0xFF;
1908 if (u28[h] == NULL) {
1909 MALLOC(u28[h], char *, 256 * sizeof(char), M_TEMP, M_WAITOK);
1910 for (j=0; j<256; j++)
1914 u28[h][l] = i & 0xFF;
1917 ntmp->ntm_u28 = u28;
1923 ntfs_u28_uninit(struct ntfsmount *ntmp)
1928 if (ntmp->ntm_u28 == NULL)
1931 u28 = ntmp->ntm_u28;
1933 for (i=0; i<256; i++)
1935 FREE(u28[i], M_TEMP);
1943 ntfs_82u_init(struct ntfsmount *ntmp, u_int16_t *u2w)
1948 MALLOC(_82u, wchar *, 256 * sizeof(wchar), M_TEMP, M_WAITOK);
1951 for (i=0; i<256; i++)
1954 for (i=0; i<128; i++)
1956 for (i=0; i<128; i++)
1957 _82u[i+128] = u2w[i];
1960 ntmp->ntm_82u = _82u;
1966 ntfs_82u_uninit(struct ntfsmount *ntmp)
1968 FREE(ntmp->ntm_82u, M_TEMP);
1973 * maps the Unicode char to 8bit equivalent
1974 * XXX currently only gets lower 8bit from the Unicode char
1975 * and substitutes a '_' for it if the result would be '\0';
1976 * something better has to be definitely though out
1979 ntfs_u28(struct ntfsmount *ntmp, wchar wc)
1983 p = ntmp->ntm_u28[(wc>>8)&0xFF];
1986 return (p[wc&0xFF]);