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.15 2005/08/02 13:03:55 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 #include <machine/inttypes.h>
47 #if defined(__NetBSD__)
48 #include <miscfs/specfs/specdev.h>
51 /* #define NTFS_DEBUG 1 */
53 #include "ntfsmount.h"
54 #include "ntfs_inode.h"
55 #include "ntfs_vfsops.h"
56 #include "ntfs_subr.h"
57 #include "ntfs_compr.h"
58 #include "ntfs_ihash.h"
60 #if defined(__DragonFly__)
61 MALLOC_DEFINE(M_NTFSNTVATTR, "NTFS vattr", "NTFS file attribute information");
62 MALLOC_DEFINE(M_NTFSRDATA, "NTFS res data", "NTFS resident data");
63 MALLOC_DEFINE(M_NTFSRUN, "NTFS vrun", "NTFS vrun storage");
64 MALLOC_DEFINE(M_NTFSDECOMP, "NTFS decomp", "NTFS decompression temporary");
67 static int ntfs_ntlookupattr (struct ntfsmount *, const char *, int, int *, char **);
68 static int ntfs_findvattr (struct ntfsmount *, struct ntnode *, struct ntvattr **, struct ntvattr **, u_int32_t, const char *, size_t, cn_t);
69 static int ntfs_uastricmp (struct ntfsmount *, const wchar *, size_t, const char *, size_t);
70 static int ntfs_uastrcmp (struct ntfsmount *, const wchar *, size_t, const char *, size_t);
72 /* table for mapping Unicode chars into uppercase; it's filled upon first
73 * ntfs mount, freed upon last ntfs umount */
74 static wchar *ntfs_toupper_tab;
75 #define NTFS_TOUPPER(ch) (ntfs_toupper_tab[(ch)])
76 static struct lock ntfs_toupper_lock;
77 static signed int ntfs_toupper_usecount;
79 /* support macro for ntfs_ntvattrget() */
80 #define NTFS_AALPCMP(aalp,type,name,namelen) ( \
81 (aalp->al_type == type) && (aalp->al_namelen == namelen) && \
82 !NTFS_UASTRCMP(aalp->al_name,aalp->al_namelen,name,namelen) )
88 ntfs_ntvattrrele(struct ntvattr *vap)
90 dprintf(("ntfs_ntvattrrele: ino: %"PRId64", type: 0x%x\n",
91 vap->va_ip->i_number, vap->va_type));
93 ntfs_ntrele(vap->va_ip);
99 * find the attribute in the ntnode
102 ntfs_findvattr(struct ntfsmount *ntmp, struct ntnode *ip,
103 struct ntvattr **lvapp, struct ntvattr **vapp, u_int32_t type,
104 const char *name, size_t namelen, cn_t vcn)
109 if((ip->i_flag & IN_LOADED) == 0) {
110 dprintf(("ntfs_findvattr: node not loaded, ino: %"PRId64"\n",
112 error = ntfs_loadntnode(ntmp,ip);
114 printf("ntfs_findvattr: FAILED TO LOAD INO: %"PRId64"\n",
122 for (vap = ip->i_valist.lh_first; vap; vap = vap->va_list.le_next) {
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
149 ntfs_ntvattrget(struct ntfsmount *ntmp, struct ntnode *ip, u_int32_t type,
150 const char *name, cn_t vcn, struct ntvattr **vapp)
152 struct ntvattr *lvap = NULL;
153 struct attr_attrlist *aalp;
154 struct attr_attrlist *nextaalp;
156 struct ntnode *newip;
164 dprintf(("ntfs_ntvattrget: " \
165 "ino: %"PRId64", type: 0x%x, name: %s, vcn: %d\n", \
166 ip->i_number, type, name, (u_int32_t) vcn));
167 namelen = strlen(name);
169 dprintf(("ntfs_ntvattrget: " \
170 "ino: %"PRId64", type: 0x%x, vcn: %d\n", \
171 ip->i_number, type, (u_int32_t) vcn));
176 error = ntfs_findvattr(ntmp, ip, &lvap, vapp, type, name, namelen, vcn);
181 dprintf(("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: " \
182 "ino: %"PRId64", type: 0x%x, name: %s, vcn: %d\n", \
183 ip->i_number, type, name, (u_int32_t) vcn));
186 /* Scan $ATTRIBUTE_LIST for requested attribute */
187 len = lvap->va_datalen;
188 MALLOC(alpool, caddr_t, len, M_TEMP, M_WAITOK);
189 error = ntfs_readntvattr_plain(ntmp, ip, lvap, 0, len, alpool, &len,
194 aalp = (struct attr_attrlist *) alpool;
197 for(; len > 0; aalp = nextaalp) {
198 dprintf(("ntfs_ntvattrget: " \
199 "attrlist: ino: %d, attr: 0x%x, vcn: %d\n", \
200 aalp->al_inumber, aalp->al_type, \
201 (u_int32_t) aalp->al_vcnstart));
203 if (len > aalp->reclen) {
204 nextaalp = NTFS_NEXTREC(aalp, struct attr_attrlist *);
210 if (!NTFS_AALPCMP(aalp, type, name, namelen) ||
211 (nextaalp && (nextaalp->al_vcnstart <= vcn) &&
212 NTFS_AALPCMP(nextaalp, type, name, namelen)))
215 dprintf(("ntfs_ntvattrget: attribute in ino: %d\n",
218 /* this is not a main record, so we can't use just plain
220 error = ntfs_vgetex(ntmp->ntm_mountp, aalp->al_inumber,
221 NTFS_A_DATA, NULL, LK_EXCLUSIVE,
222 VG_EXT, curthread, &newvp);
224 printf("ntfs_ntvattrget: CAN'T VGET INO: %d\n",
228 newip = VTONT(newvp);
229 /* XXX have to lock ntnode */
230 error = ntfs_findvattr(ntmp, newip, &lvap, vapp,
231 type, name, namelen, vcn);
235 printf("ntfs_ntvattrget: ATTRLIST ERROR.\n");
240 dprintf(("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: " \
241 "ino: %"PRId64", type: 0x%x, name: %.*s, vcn: %d\n", \
242 ip->i_number, type, (int) namelen, name, (u_int32_t) vcn));
244 FREE(alpool, M_TEMP);
249 * Read ntnode from disk, make ntvattr list.
251 * ntnode should be locked
254 ntfs_loadntnode(struct ntfsmount *ntmp, struct ntnode *ip)
256 struct filerec *mfrp;
260 struct ntvattr *nvap;
262 dprintf(("ntfs_loadntnode: loading ino: %"PRId64"\n",ip->i_number));
264 MALLOC(mfrp, struct filerec *, ntfs_bntob(ntmp->ntm_bpmftrec),
267 if (ip->i_number < NTFS_SYSNODESNUM) {
270 dprintf(("ntfs_loadntnode: read system node\n"));
272 bn = ntfs_cntobn(ntmp->ntm_mftcn) +
273 ntmp->ntm_bpmftrec * ip->i_number;
275 error = bread(ntmp->ntm_devvp,
276 bn, ntfs_bntob(ntmp->ntm_bpmftrec), &bp);
278 printf("ntfs_loadntnode: BREAD FAILED\n");
282 memcpy(mfrp, bp->b_data, ntfs_bntob(ntmp->ntm_bpmftrec));
287 vp = ntmp->ntm_sysvn[NTFS_MFTINO];
288 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
289 ip->i_number * ntfs_bntob(ntmp->ntm_bpmftrec),
290 ntfs_bntob(ntmp->ntm_bpmftrec), mfrp, NULL);
292 printf("ntfs_loadntnode: ntfs_readattr failed\n");
297 /* Check if magic and fixups are correct */
298 error = ntfs_procfixups(ntmp, NTFS_FILEMAGIC, (caddr_t)mfrp,
299 ntfs_bntob(ntmp->ntm_bpmftrec));
301 printf("ntfs_loadntnode: BAD MFT RECORD %"PRId64"\n",
306 dprintf(("ntfs_loadntnode: load attrs for ino: %"PRId64"\n",ip->i_number));
307 off = mfrp->fr_attroff;
308 ap = (struct attr *) ((caddr_t)mfrp + off);
310 LIST_INIT(&ip->i_valist);
312 while (ap->a_hdr.a_type != -1) {
313 error = ntfs_attrtontvattr(ntmp, &nvap, ap);
318 LIST_INSERT_HEAD(&ip->i_valist, nvap, va_list);
320 off += ap->a_hdr.reclen;
321 ap = (struct attr *) ((caddr_t)mfrp + off);
324 printf("ntfs_loadntnode: failed to load attr ino: %"PRId64"\n",
329 ip->i_mainrec = mfrp->fr_mainrec;
330 ip->i_nlink = mfrp->fr_nlink;
331 ip->i_frflag = mfrp->fr_flags;
333 ip->i_flag |= IN_LOADED;
341 * Routine locks ntnode and increase usecount, just opposite of
345 ntfs_ntget(struct ntnode *ip)
349 dprintf(("ntfs_ntget: get ntnode %"PRId64": %p, usecount: %d\n",
350 ip->i_number, ip, ip->i_usecount));
352 ip->i_usecount++; /* ZZZ */
353 lwkt_gettoken(&ilock, &ip->i_interlock);
354 LOCKMGR(&ip->i_lock, LK_EXCLUSIVE | LK_INTERLOCK, &ilock);
360 * Routine search ntnode in hash, if found: lock, inc usecount and return.
361 * If not in hash allocate structure for ntnode, prefill it, lock,
362 * inc count and return.
364 * ntnode returned locked
367 ntfs_ntlookup(struct ntfsmount *ntmp, ino_t ino, struct ntnode **ipp)
371 dprintf(("ntfs_ntlookup: looking for ntnode %d\n", ino));
374 if ((ip = ntfs_nthashlookup(ntmp->ntm_dev, ino)) != NULL) {
376 dprintf(("ntfs_ntlookup: ntnode %d: %p, usecount: %d\n",
377 ino, ip, ip->i_usecount));
381 } while (LOCKMGR(&ntfs_hashlock, LK_EXCLUSIVE | LK_SLEEPFAIL, NULL));
383 MALLOC(ip, struct ntnode *, sizeof(struct ntnode),
384 M_NTFSNTNODE, M_WAITOK);
385 ddprintf(("ntfs_ntlookup: allocating ntnode: %d: %p\n", ino, ip));
386 bzero((caddr_t) ip, sizeof(struct ntnode));
388 /* Generic initialization */
389 ip->i_devvp = ntmp->ntm_devvp;
390 ip->i_dev = ntmp->ntm_dev;
394 LIST_INIT(&ip->i_fnlist);
397 /* init lock and lock the newborn ntnode */
398 lockinit(&ip->i_lock, 0, "ntnode", 0, LK_EXCLUSIVE);
399 lwkt_token_init(&ip->i_interlock);
404 LOCKMGR(&ntfs_hashlock, LK_RELEASE, NULL);
408 dprintf(("ntfs_ntlookup: ntnode %d: %p, usecount: %d\n",
409 ino, ip, ip->i_usecount));
415 * Decrement usecount of ntnode and unlock it, if usecount reach zero,
418 * ntnode should be locked on entry, and unlocked on return.
421 ntfs_ntput(struct ntnode *ip)
426 dprintf(("ntfs_ntput: rele ntnode %"PRId64": %p, usecount: %d\n",
427 ip->i_number, ip, ip->i_usecount));
429 lwkt_gettoken(&ilock, &ip->i_interlock);
433 if (ip->i_usecount < 0) {
434 panic("ntfs_ntput: ino: %"PRId64" usecount: %d \n",
435 ip->i_number,ip->i_usecount);
439 if (ip->i_usecount > 0) {
440 LOCKMGR(&ip->i_lock, LK_RELEASE|LK_INTERLOCK, &ilock);
444 dprintf(("ntfs_ntput: deallocating ntnode: %"PRId64"\n", ip->i_number));
446 if (ip->i_fnlist.lh_first)
447 panic("ntfs_ntput: ntnode has fnodes\n");
451 while ((vap = LIST_FIRST(&ip->i_valist)) != NULL) {
452 LIST_REMOVE(vap,va_list);
453 ntfs_freentvattr(vap);
455 lwkt_reltoken(&ilock);
457 FREE(ip, M_NTFSNTNODE);
461 * increment usecount of ntnode
464 ntfs_ntref(struct ntnode *ip)
468 dprintf(("ntfs_ntref: ino %"PRId64", usecount: %d\n",
469 ip->i_number, ip->i_usecount));
474 * Decrement usecount of ntnode.
477 ntfs_ntrele(struct ntnode *ip)
481 dprintf(("ntfs_ntrele: rele ntnode %"PRId64": %p, usecount: %d\n",
482 ip->i_number, ip, ip->i_usecount));
484 lwkt_gettoken(&ilock, &ip->i_interlock);
487 if (ip->i_usecount < 0) {
488 panic("ntfs_ntrele: ino: %"PRId64" usecount: %d \n",
489 ip->i_number,ip->i_usecount);
491 lwkt_reltoken(&ilock);
495 * Deallocate all memory allocated for ntvattr
498 ntfs_freentvattr(struct ntvattr *vap)
500 if (vap->va_flag & NTFS_AF_INRUN) {
502 FREE(vap->va_vruncn, M_NTFSRUN);
504 FREE(vap->va_vruncl, M_NTFSRUN);
507 FREE(vap->va_datap, M_NTFSRDATA);
509 FREE(vap, M_NTFSNTVATTR);
513 * Convert disk image of attribute into ntvattr structure,
514 * runs are expanded also.
517 ntfs_attrtontvattr(struct ntfsmount *ntmp, struct ntvattr **rvapp,
526 MALLOC(vap, struct ntvattr *, sizeof(struct ntvattr),
527 M_NTFSNTVATTR, M_WAITOK);
528 bzero(vap, sizeof(struct ntvattr));
530 vap->va_flag = rap->a_hdr.a_flag;
531 vap->va_type = rap->a_hdr.a_type;
532 vap->va_compression = rap->a_hdr.a_compression;
533 vap->va_index = rap->a_hdr.a_index;
535 ddprintf(("type: 0x%x, index: %d", vap->va_type, vap->va_index));
537 vap->va_namelen = rap->a_hdr.a_namelen;
538 if (rap->a_hdr.a_namelen) {
539 wchar *unp = (wchar *) ((caddr_t) rap + rap->a_hdr.a_nameoff);
540 ddprintf((", name:["));
541 for (i = 0; i < vap->va_namelen; i++) {
542 vap->va_name[i] = unp[i];
543 ddprintf(("%c", vap->va_name[i]));
547 if (vap->va_flag & NTFS_AF_INRUN) {
548 ddprintf((", nonres."));
549 vap->va_datalen = rap->a_nr.a_datalen;
550 vap->va_allocated = rap->a_nr.a_allocated;
551 vap->va_vcnstart = rap->a_nr.a_vcnstart;
552 vap->va_vcnend = rap->a_nr.a_vcnend;
553 vap->va_compressalg = rap->a_nr.a_compressalg;
554 error = ntfs_runtovrun(&(vap->va_vruncn), &(vap->va_vruncl),
556 (caddr_t) rap + rap->a_nr.a_dataoff);
558 vap->va_compressalg = 0;
559 ddprintf((", res."));
560 vap->va_datalen = rap->a_r.a_datalen;
561 vap->va_allocated = rap->a_r.a_datalen;
562 vap->va_vcnstart = 0;
563 vap->va_vcnend = ntfs_btocn(vap->va_allocated);
564 MALLOC(vap->va_datap, caddr_t, vap->va_datalen,
565 M_NTFSRDATA, M_WAITOK);
566 memcpy(vap->va_datap, (caddr_t) rap + rap->a_r.a_dataoff,
569 ddprintf((", len: %d", vap->va_datalen));
572 FREE(vap, M_NTFSNTVATTR);
582 * Expand run into more utilizable and more memory eating format.
585 ntfs_runtovrun(cn_t **rcnp, cn_t **rclp, u_long *rcntp, u_int8_t *run)
599 off += (run[off] & 0xF) + ((run[off] >> 4) & 0xF) + 1;
602 MALLOC(cn, cn_t *, cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
603 MALLOC(cl, cn_t *, cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
613 for (i = 0; i < (sz & 0xF); i++)
614 cl[cnt] += (u_int32_t) run[off++] << (i << 3);
617 if (run[off + sz - 1] & 0x80) {
618 tmp = ((u_int64_t) - 1) << (sz << 3);
619 for (i = 0; i < sz; i++)
620 tmp |= (u_int64_t) run[off++] << (i << 3);
623 for (i = 0; i < sz; i++)
624 tmp |= (u_int64_t) run[off++] << (i << 3);
627 prev = cn[cnt] = prev + tmp;
640 * Compare unicode and ascii string case insens.
643 ntfs_uastricmp(struct ntfsmount *ntmp, const wchar *ustr, size_t ustrlen,
644 const char *astr, size_t astrlen)
650 * XXX We use NTFS_82U(NTFS_U28(c)) to get rid of unicode
651 * symbols not covered by translation table
653 for (i = 0; i < ustrlen && i < astrlen; i++) {
654 res = ((int) NTFS_TOUPPER(NTFS_82U(NTFS_U28(ustr[i])))) -
655 ((int)NTFS_TOUPPER(NTFS_82U(astr[i])));
659 return (ustrlen - astrlen);
663 * Compare unicode and ascii string case sens.
666 ntfs_uastrcmp(struct ntfsmount *ntmp, const wchar *ustr, size_t ustrlen,
667 const char *astr, size_t astrlen)
672 for (i = 0; (i < ustrlen) && (i < astrlen); i++) {
673 res = (int) (((char)NTFS_U28(ustr[i])) - astr[i]);
677 return (ustrlen - astrlen);
681 * Search fnode in ntnode, if not found allocate and preinitialize.
683 * ntnode should be locked on entry.
686 ntfs_fget(struct ntfsmount *ntmp, struct ntnode *ip, int attrtype,
687 char *attrname, struct fnode **fpp)
691 dprintf(("ntfs_fget: ino: %"PRId64", attrtype: 0x%x, attrname: %s\n",
692 ip->i_number,attrtype, attrname?attrname:""));
694 for (fp = ip->i_fnlist.lh_first; fp != NULL; fp = fp->f_fnlist.le_next){
695 dprintf(("ntfs_fget: fnode: attrtype: %d, attrname: %s\n",
696 fp->f_attrtype, fp->f_attrname?fp->f_attrname:""));
698 if ((attrtype == fp->f_attrtype) &&
699 ((!attrname && !fp->f_attrname) ||
700 (attrname && fp->f_attrname &&
701 !strcmp(attrname,fp->f_attrname)))){
702 dprintf(("ntfs_fget: found existed: %p\n",fp));
710 MALLOC(fp, struct fnode *, sizeof(struct fnode), M_NTFSFNODE, M_WAITOK);
711 bzero(fp, sizeof(struct fnode));
712 dprintf(("ntfs_fget: allocating fnode: %p\n",fp));
716 fp->f_flag |= FN_AATTRNAME;
717 MALLOC(fp->f_attrname, char *, strlen(attrname)+1, M_TEMP, M_WAITOK);
718 strcpy(fp->f_attrname, attrname);
720 fp->f_attrname = NULL;
721 fp->f_attrtype = attrtype;
725 LIST_INSERT_HEAD(&ip->i_fnlist, fp, f_fnlist);
733 * Deallocate fnode, remove it from ntnode's fnode list.
735 * ntnode should be locked.
738 ntfs_frele(struct fnode *fp)
740 struct ntnode *ip = FTONT(fp);
742 dprintf(("ntfs_frele: fnode: %p for %"PRId64": %p\n", fp, ip->i_number, ip));
744 dprintf(("ntfs_frele: deallocating fnode\n"));
745 LIST_REMOVE(fp,f_fnlist);
746 if (fp->f_flag & FN_AATTRNAME)
747 FREE(fp->f_attrname, M_TEMP);
749 FREE(fp->f_dirblbuf, M_NTFSDIR);
750 FREE(fp, M_NTFSFNODE);
755 * Lookup attribute name in format: [[:$ATTR_TYPE]:$ATTR_NAME],
756 * $ATTR_TYPE is searched in attrdefs read from $AttrDefs.
757 * If $ATTR_TYPE nott specifed, ATTR_A_DATA assumed.
760 ntfs_ntlookupattr(struct ntfsmount *ntmp, const char *name, int namelen,
761 int *attrtype, char **attrname)
765 struct ntvattrdef *adp;
770 if (name[0] == '$') {
772 for (syslen = 0; syslen < namelen; syslen++) {
773 if(sys[syslen] == ':') {
783 for (i = 0; i < ntmp->ntm_adnum; i++, adp++){
784 if (syslen != adp->ad_namelen ||
785 strncmp(sys, adp->ad_name, syslen) != 0)
788 *attrtype = adp->ad_type;
793 *attrtype = NTFS_A_DATA;
797 MALLOC((*attrname), char *, namelen, M_TEMP, M_WAITOK);
798 memcpy((*attrname), name, namelen);
799 (*attrname)[namelen] = '\0';
806 * Lookup specifed node for filename, matching cnp,
807 * return fnode filled.
810 ntfs_ntlookupfile(struct ntfsmount *ntmp, struct vnode *vp,
811 struct componentname *cnp, struct vnode **vpp)
813 struct fnode *fp = VTOF(vp);
814 struct ntnode *ip = FTONT(fp);
815 struct ntvattr *vap; /* Root attribute */
816 cn_t cn; /* VCN in current attribute */
817 caddr_t rdbuf; /* Buffer to read directory's blocks */
819 u_int32_t rdsize; /* Length of data to read from current block */
820 struct attr_indexentry *iep;
821 int error, res, anamelen, fnamelen;
822 const char *fname,*aname;
824 int attrtype = NTFS_A_DATA;
825 char *attrname = NULL;
830 error = ntfs_ntget(ip);
834 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
835 if (error || (vap->va_flag & NTFS_AF_INRUN))
838 blsize = vap->va_a_iroot->ir_size;
839 rdsize = vap->va_datalen;
842 * Divide file name into: foofilefoofilefoofile[:attrspec]
843 * Store like this: fname:fnamelen [aname:anamelen]
845 fname = cnp->cn_nameptr;
848 for (fnamelen = 0; fnamelen < cnp->cn_namelen; fnamelen++)
849 if(fname[fnamelen] == ':') {
850 aname = fname + fnamelen + 1;
851 anamelen = cnp->cn_namelen - fnamelen - 1;
852 dprintf(("ntfs_ntlookupfile: %s (%d), attr: %s (%d)\n",
853 fname, fnamelen, aname, anamelen));
857 dprintf(("ntfs_ntlookupfile: blksz: %d, rdsz: %d\n", blsize, rdsize));
859 MALLOC(rdbuf, caddr_t, blsize, M_TEMP, M_WAITOK);
861 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXROOT, "$I30",
862 0, rdsize, rdbuf, NULL);
866 aoff = sizeof(struct attr_indexroot);
869 iep = (struct attr_indexentry *) (rdbuf + aoff);
871 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
873 iep = (struct attr_indexentry *) (rdbuf + aoff))
875 ddprintf(("scan: %d, %d\n",
876 (u_int32_t) iep->ie_number,
877 (u_int32_t) iep->ie_fnametype));
879 /* check the name - the case-insensitible check
880 * has to come first, to break from this for loop
881 * if needed, so we can dive correctly */
882 res = NTFS_UASTRICMP(iep->ie_fname, iep->ie_fnamelen,
885 if (res < 0) continue;
887 if (iep->ie_fnametype == 0 ||
888 !(ntmp->ntm_flag & NTFS_MFLAG_CASEINS))
890 res = NTFS_UASTRCMP(iep->ie_fname,
891 iep->ie_fnamelen, fname, fnamelen);
892 if (res != 0) continue;
896 error = ntfs_ntlookupattr(ntmp,
898 &attrtype, &attrname);
903 /* Check if we've found ourself */
904 if ((iep->ie_number == ip->i_number) &&
905 (attrtype == fp->f_attrtype) &&
906 ((!attrname && !fp->f_attrname) ||
907 (attrname && fp->f_attrname &&
908 !strcmp(attrname, fp->f_attrname))))
916 /* vget node, but don't load it */
917 error = ntfs_vgetex(ntmp->ntm_mountp,
918 iep->ie_number, attrtype, attrname,
919 LK_EXCLUSIVE, VG_DONTLOADIN | VG_DONTVALIDFN,
922 /* free the buffer returned by ntfs_ntlookupattr() */
924 FREE(attrname, M_TEMP);
933 if (nfp->f_flag & FN_VALID) {
938 nfp->f_fflag = iep->ie_fflag;
939 nfp->f_pnumber = iep->ie_fpnumber;
940 nfp->f_times = iep->ie_ftimes;
942 if((nfp->f_fflag & NTFS_FFLAG_DIR) &&
943 (nfp->f_attrtype == NTFS_A_DATA) &&
944 (nfp->f_attrname == NULL))
949 nvp->v_type = f_type;
951 if ((nfp->f_attrtype == NTFS_A_DATA) &&
952 (nfp->f_attrname == NULL))
954 /* Opening default attribute */
955 nfp->f_size = iep->ie_fsize;
956 nfp->f_allocated = iep->ie_fallocated;
957 nfp->f_flag |= FN_PRELOADED;
959 error = ntfs_filesize(ntmp, nfp,
960 &nfp->f_size, &nfp->f_allocated);
967 nfp->f_flag &= ~FN_VALID;
972 /* Dive if possible */
973 if (iep->ie_flag & NTFS_IEFLAG_SUBNODE) {
974 dprintf(("ntfs_ntlookupfile: diving\n"));
976 cn = *(cn_t *) (rdbuf + aoff +
977 iep->reclen - sizeof(cn_t));
980 error = ntfs_readattr(ntmp, ip, NTFS_A_INDX, "$I30",
981 ntfs_cntob(cn), rdsize, rdbuf, NULL);
985 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
990 aoff = (((struct attr_indexalloc *) rdbuf)->ia_hdrsize +
993 dprintf(("ntfs_ntlookupfile: nowhere to dive :-(\n"));
999 dprintf(("finish\n"));
1002 if (attrname) FREE(attrname, M_TEMP);
1003 ntfs_ntvattrrele(vap);
1005 FREE(rdbuf, M_TEMP);
1010 * Check if name type is permitted to show.
1013 ntfs_isnamepermitted(struct ntfsmount *ntmp, struct attr_indexentry *iep)
1015 if (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)
1018 switch (iep->ie_fnametype) {
1020 ddprintf(("ntfs_isnamepermitted: skiped DOS name\n"));
1022 case 0: case 1: case 3:
1025 printf("ntfs_isnamepermitted: " \
1026 "WARNING! Unknown file name type: %d\n",
1034 * Read ntfs dir like stream of attr_indexentry, not like btree of them.
1035 * This is done by scaning $BITMAP:$I30 for busy clusters and reading them.
1036 * Ofcouse $INDEX_ROOT:$I30 is read before. Last read values are stored in
1037 * fnode, so we can skip toward record number num almost immediatly.
1038 * Anyway this is rather slow routine. The problem is that we don't know
1039 * how many records are there in $INDEX_ALLOCATION:$I30 block.
1042 ntfs_ntreaddir(struct ntfsmount *ntmp, struct fnode *fp,
1043 u_int32_t num, struct attr_indexentry **riepp)
1045 struct ntnode *ip = FTONT(fp);
1046 struct ntvattr *vap = NULL; /* IndexRoot attribute */
1047 struct ntvattr *bmvap = NULL; /* BitMap attribute */
1048 struct ntvattr *iavap = NULL; /* IndexAllocation attribute */
1049 caddr_t rdbuf; /* Buffer to read directory's blocks */
1050 u_char *bmp = NULL; /* Bitmap */
1051 u_int32_t blsize; /* Index allocation size (2048) */
1052 u_int32_t rdsize; /* Length of data to read */
1053 u_int32_t attrnum; /* Current attribute type */
1054 u_int32_t cpbl = 1; /* Clusters per directory block */
1056 struct attr_indexentry *iep;
1058 u_int32_t aoff, cnum;
1060 dprintf(("ntfs_ntreaddir: read ino: %"PRId64", num: %d\n", ip->i_number, num));
1061 error = ntfs_ntget(ip);
1065 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
1069 if (fp->f_dirblbuf == NULL) {
1070 fp->f_dirblsz = vap->va_a_iroot->ir_size;
1071 MALLOC(fp->f_dirblbuf, caddr_t,
1072 max(vap->va_datalen,fp->f_dirblsz), M_NTFSDIR, M_WAITOK);
1075 blsize = fp->f_dirblsz;
1076 rdbuf = fp->f_dirblbuf;
1078 dprintf(("ntfs_ntreaddir: rdbuf: 0x%p, blsize: %d\n", rdbuf, blsize));
1080 if (vap->va_a_iroot->ir_flag & NTFS_IRFLAG_INDXALLOC) {
1081 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXBITMAP, "$I30",
1087 MALLOC(bmp, u_char *, bmvap->va_datalen, M_TEMP, M_WAITOK);
1088 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXBITMAP, "$I30", 0,
1089 bmvap->va_datalen, bmp, NULL);
1093 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDX, "$I30",
1099 cpbl = ntfs_btocn(blsize + ntfs_cntob(1) - 1);
1100 dprintf(("ntfs_ntreaddir: indexalloc: %d, cpbl: %d\n",
1101 iavap->va_datalen, cpbl));
1103 dprintf(("ntfs_ntreadidir: w/o BitMap and IndexAllocation\n"));
1104 iavap = bmvap = NULL;
1108 /* Try use previous values */
1109 if ((fp->f_lastdnum < num) && (fp->f_lastdnum != 0)) {
1110 attrnum = fp->f_lastdattr;
1111 aoff = fp->f_lastdoff;
1112 blnum = fp->f_lastdblnum;
1113 cnum = fp->f_lastdnum;
1115 attrnum = NTFS_A_INDXROOT;
1116 aoff = sizeof(struct attr_indexroot);
1122 dprintf(("ntfs_ntreaddir: scan: 0x%x, %d, %d, %d, %d\n",
1123 attrnum, (u_int32_t) blnum, cnum, num, aoff));
1124 rdsize = (attrnum == NTFS_A_INDXROOT) ? vap->va_datalen : blsize;
1125 error = ntfs_readattr(ntmp, ip, attrnum, "$I30",
1126 ntfs_cntob(blnum * cpbl), rdsize, rdbuf, NULL);
1130 if (attrnum == NTFS_A_INDX) {
1131 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1137 aoff = (attrnum == NTFS_A_INDX) ?
1138 (0x18 + ((struct attr_indexalloc *) rdbuf)->ia_hdrsize) :
1139 sizeof(struct attr_indexroot);
1141 iep = (struct attr_indexentry *) (rdbuf + aoff);
1142 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
1143 aoff += iep->reclen,
1144 iep = (struct attr_indexentry *) (rdbuf + aoff))
1146 if (!ntfs_isnamepermitted(ntmp, iep)) continue;
1149 fp->f_lastdnum = cnum;
1150 fp->f_lastdoff = aoff;
1151 fp->f_lastdblnum = blnum;
1152 fp->f_lastdattr = attrnum;
1163 if (attrnum == NTFS_A_INDXROOT)
1168 while (ntfs_cntob(blnum * cpbl) < iavap->va_datalen) {
1169 if (bmp[blnum >> 3] & (1 << (blnum & 3)))
1174 attrnum = NTFS_A_INDX;
1176 if (ntfs_cntob(blnum * cpbl) >= iavap->va_datalen)
1178 dprintf(("ntfs_ntreaddir: blnum: %d\n", (u_int32_t) blnum));
1187 ntfs_ntvattrrele(vap);
1189 ntfs_ntvattrrele(bmvap);
1191 ntfs_ntvattrrele(iavap);
1199 * Convert NTFS times that are in 100 ns units and begins from
1200 * 1601 Jan 1 into unix times.
1203 ntfs_nttimetounix(u_int64_t nt)
1207 /* WindowNT times are in 100 ns and from 1601 Jan 1 */
1208 t.tv_nsec = (nt % (1000 * 1000 * 10)) * 100;
1209 t.tv_sec = nt / (1000 * 1000 * 10) -
1210 369LL * 365LL * 24LL * 60LL * 60LL -
1211 89LL * 1LL * 24LL * 60LL * 60LL;
1216 * Get file times from NTFS_A_NAME attribute.
1219 ntfs_times(struct ntfsmount *ntmp, struct ntnode *ip, ntfs_times_t *tm)
1221 struct ntvattr *vap;
1224 dprintf(("ntfs_times: ino: %"PRId64"...\n", ip->i_number));
1226 error = ntfs_ntget(ip);
1230 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_NAME, NULL, 0, &vap);
1235 *tm = vap->va_a_name->n_times;
1236 ntfs_ntvattrrele(vap);
1243 * Get file sizes from corresponding attribute.
1245 * ntnode under fnode should be locked.
1248 ntfs_filesize(struct ntfsmount *ntmp, struct fnode *fp, u_int64_t *size,
1251 struct ntvattr *vap;
1252 struct ntnode *ip = FTONT(fp);
1256 dprintf(("ntfs_filesize: ino: %"PRId64"\n", ip->i_number));
1258 error = ntfs_ntvattrget(ntmp, ip,
1259 fp->f_attrtype, fp->f_attrname, 0, &vap);
1263 bn = vap->va_allocated;
1264 sz = vap->va_datalen;
1266 dprintf(("ntfs_filesize: %d bytes (%d bytes allocated)\n",
1267 (u_int32_t) sz, (u_int32_t) bn));
1274 ntfs_ntvattrrele(vap);
1280 * This is one of write routine.
1283 ntfs_writeattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1284 u_int32_t attrnum, char *attrname, off_t roff,
1285 size_t rsize, void *rdata, size_t *initp,
1290 off_t off = roff, left = rsize, towrite;
1291 caddr_t data = rdata;
1292 struct ntvattr *vap;
1296 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1297 ntfs_btocn(off), &vap);
1300 towrite = min(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1301 ddprintf(("ntfs_writeattr_plain: o: %d, s: %d (%d - %d)\n",
1302 (u_int32_t) off, (u_int32_t) towrite,
1303 (u_int32_t) vap->va_vcnstart,
1304 (u_int32_t) vap->va_vcnend));
1305 error = ntfs_writentvattr_plain(ntmp, ip, vap,
1306 off - ntfs_cntob(vap->va_vcnstart),
1307 towrite, data, &init, uio);
1309 printf("ntfs_writeattr_plain: " \
1310 "ntfs_writentvattr_plain failed: o: %d, s: %d\n",
1311 (u_int32_t) off, (u_int32_t) towrite);
1312 printf("ntfs_writeattr_plain: attrib: %d - %d\n",
1313 (u_int32_t) vap->va_vcnstart,
1314 (u_int32_t) vap->va_vcnend);
1315 ntfs_ntvattrrele(vap);
1318 ntfs_ntvattrrele(vap);
1321 data = data + towrite;
1329 * This is one of write routine.
1331 * ntnode should be locked.
1334 ntfs_writentvattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1335 struct ntvattr *vap, off_t roff, size_t rsize,
1336 void *rdata, size_t *initp, struct uio *uio)
1341 cn_t ccn, ccl, cn, left, cl;
1342 caddr_t data = rdata;
1348 if ((vap->va_flag & NTFS_AF_INRUN) == 0) {
1349 printf("ntfs_writevattr_plain: CAN'T WRITE RES. ATTRIBUTE\n");
1353 ddprintf(("ntfs_writentvattr_plain: data in run: %ld chains\n",
1361 for (; left && (cnt < vap->va_vruncnt); cnt++) {
1362 ccn = vap->va_vruncn[cnt];
1363 ccl = vap->va_vruncl[cnt];
1365 ddprintf(("ntfs_writentvattr_plain: " \
1366 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1367 (u_int32_t) left, (u_int32_t) ccn, \
1368 (u_int32_t) ccl, (u_int32_t) off));
1370 if (ntfs_cntob(ccl) < off) {
1371 off -= ntfs_cntob(ccl);
1375 if (!ccn && ip->i_number != NTFS_BOOTINO)
1378 ccl -= ntfs_btocn(off);
1379 cn = ccn + ntfs_btocn(off);
1380 off = ntfs_btocnoff(off);
1382 while (left && ccl) {
1383 #if defined(__DragonFly__)
1385 min(ntfs_cntob(ccl) - off, MAXBSIZE - off));
1387 /* under NetBSD, bread() can read
1388 * maximum one block worth of data */
1389 tocopy = min(left, ntmp->ntm_bps - off);
1391 cl = ntfs_btocl(tocopy + off);
1392 ddprintf(("ntfs_writentvattr_plain: write: " \
1393 "cn: 0x%x cl: %d, off: %d len: %d, left: %d\n",
1394 (u_int32_t) cn, (u_int32_t) cl,
1395 (u_int32_t) off, (u_int32_t) tocopy,
1397 if ((off == 0) && (tocopy == ntfs_cntob(cl)))
1399 bp = getblk(ntmp->ntm_devvp, ntfs_cntobn(cn),
1400 ntfs_cntob(cl), 0, 0);
1403 error = bread(ntmp->ntm_devvp, ntfs_cntobn(cn),
1404 ntfs_cntob(cl), &bp);
1411 uiomove(bp->b_data + off, tocopy, uio);
1413 memcpy(bp->b_data + off, data, tocopy);
1415 data = data + tocopy;
1425 printf("ntfs_writentvattr_plain: POSSIBLE RUN ERROR\n");
1433 * This is one of read routines.
1435 * ntnode should be locked.
1438 ntfs_readntvattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1439 struct ntvattr *vap, off_t roff, size_t rsize,
1440 void *rdata, size_t *initp, struct uio *uio)
1446 if (vap->va_flag & NTFS_AF_INRUN) {
1448 cn_t ccn, ccl, cn, left, cl;
1449 caddr_t data = rdata;
1453 ddprintf(("ntfs_readntvattr_plain: data in run: %ld chains\n",
1461 while (left && (cnt < vap->va_vruncnt)) {
1462 ccn = vap->va_vruncn[cnt];
1463 ccl = vap->va_vruncl[cnt];
1465 ddprintf(("ntfs_readntvattr_plain: " \
1466 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1467 (u_int32_t) left, (u_int32_t) ccn, \
1468 (u_int32_t) ccl, (u_int32_t) off));
1470 if (ntfs_cntob(ccl) < off) {
1471 off -= ntfs_cntob(ccl);
1475 if (ccn || ip->i_number == NTFS_BOOTINO) {
1476 ccl -= ntfs_btocn(off);
1477 cn = ccn + ntfs_btocn(off);
1478 off = ntfs_btocnoff(off);
1480 while (left && ccl) {
1481 #if defined(__DragonFly__)
1483 min(ntfs_cntob(ccl) - off,
1486 /* under NetBSD, bread() can read
1487 * maximum one block worth of data */
1489 ntmp->ntm_bps - off);
1491 cl = ntfs_btocl(tocopy + off);
1492 ddprintf(("ntfs_readntvattr_plain: " \
1493 "read: cn: 0x%x cl: %d, " \
1494 "off: %d len: %d, left: %d\n",
1500 error = bread(ntmp->ntm_devvp,
1509 uiomove(bp->b_data + off,
1512 memcpy(data, bp->b_data + off,
1516 data = data + tocopy;
1524 tocopy = min(left, ntfs_cntob(ccl) - off);
1525 ddprintf(("ntfs_readntvattr_plain: "
1526 "hole: ccn: 0x%x ccl: %d, off: %d, " \
1527 " len: %d, left: %d\n",
1528 (u_int32_t) ccn, (u_int32_t) ccl,
1529 (u_int32_t) off, (u_int32_t) tocopy,
1534 size_t remains = tocopy;
1535 for(; remains; remains++)
1536 uiomove("", 1, uio);
1538 bzero(data, tocopy);
1539 data = data + tocopy;
1544 printf("ntfs_readntvattr_plain: POSSIBLE RUN ERROR\n");
1548 ddprintf(("ntfs_readnvattr_plain: data is in mft record\n"));
1550 uiomove(vap->va_datap + roff, rsize, uio);
1552 memcpy(rdata, vap->va_datap + roff, rsize);
1560 * This is one of read routines.
1563 ntfs_readattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1564 u_int32_t attrnum, char *attrname, off_t roff,
1565 size_t rsize, void *rdata, size_t * initp,
1570 off_t off = roff, left = rsize, toread;
1571 caddr_t data = rdata;
1572 struct ntvattr *vap;
1576 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1577 ntfs_btocn(off), &vap);
1580 toread = min(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1581 ddprintf(("ntfs_readattr_plain: o: %d, s: %d (%d - %d)\n",
1582 (u_int32_t) off, (u_int32_t) toread,
1583 (u_int32_t) vap->va_vcnstart,
1584 (u_int32_t) vap->va_vcnend));
1585 error = ntfs_readntvattr_plain(ntmp, ip, vap,
1586 off - ntfs_cntob(vap->va_vcnstart),
1587 toread, data, &init, uio);
1589 printf("ntfs_readattr_plain: " \
1590 "ntfs_readntvattr_plain failed: o: %d, s: %d\n",
1591 (u_int32_t) off, (u_int32_t) toread);
1592 printf("ntfs_readattr_plain: attrib: %d - %d\n",
1593 (u_int32_t) vap->va_vcnstart,
1594 (u_int32_t) vap->va_vcnend);
1595 ntfs_ntvattrrele(vap);
1598 ntfs_ntvattrrele(vap);
1601 data = data + toread;
1609 * This is one of read routines.
1612 ntfs_readattr(struct ntfsmount *ntmp, struct ntnode *ip, u_int32_t attrnum,
1613 char *attrname, off_t roff, size_t rsize, void *rdata,
1617 struct ntvattr *vap;
1620 ddprintf(("ntfs_readattr: reading %"PRId64": 0x%x, from %d size %d bytes\n",
1621 ip->i_number, attrnum, (u_int32_t) roff, (u_int32_t) rsize));
1623 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname, 0, &vap);
1627 if ((roff > vap->va_datalen) ||
1628 (roff + rsize > vap->va_datalen)) {
1629 ddprintf(("ntfs_readattr: offset too big\n"));
1630 ntfs_ntvattrrele(vap);
1633 if (vap->va_compression && vap->va_compressalg) {
1636 off_t off = roff, left = rsize, tocopy;
1637 caddr_t data = rdata;
1640 ddprintf(("ntfs_ntreadattr: compression: %d\n",
1641 vap->va_compressalg));
1643 MALLOC(cup, u_int8_t *, ntfs_cntob(NTFS_COMPUNIT_CL),
1644 M_NTFSDECOMP, M_WAITOK);
1645 MALLOC(uup, u_int8_t *, ntfs_cntob(NTFS_COMPUNIT_CL),
1646 M_NTFSDECOMP, M_WAITOK);
1648 cn = (ntfs_btocn(roff)) & (~(NTFS_COMPUNIT_CL - 1));
1649 off = roff - ntfs_cntob(cn);
1652 error = ntfs_readattr_plain(ntmp, ip, attrnum,
1653 attrname, ntfs_cntob(cn),
1654 ntfs_cntob(NTFS_COMPUNIT_CL),
1659 tocopy = min(left, ntfs_cntob(NTFS_COMPUNIT_CL) - off);
1661 if (init == ntfs_cntob(NTFS_COMPUNIT_CL)) {
1663 uiomove(cup + off, tocopy, uio);
1665 memcpy(data, cup + off, tocopy);
1666 } else if (init == 0) {
1668 size_t remains = tocopy;
1669 for(; remains; remains--)
1670 uiomove("", 1, uio);
1673 bzero(data, tocopy);
1675 error = ntfs_uncompunit(ntmp, uup, cup);
1679 uiomove(uup + off, tocopy, uio);
1681 memcpy(data, uup + off, tocopy);
1685 data = data + tocopy;
1686 off += tocopy - ntfs_cntob(NTFS_COMPUNIT_CL);
1687 cn += NTFS_COMPUNIT_CL;
1690 FREE(uup, M_NTFSDECOMP);
1691 FREE(cup, M_NTFSDECOMP);
1693 error = ntfs_readattr_plain(ntmp, ip, attrnum, attrname,
1694 roff, rsize, rdata, &init, uio);
1695 ntfs_ntvattrrele(vap);
1701 ntfs_parserun(cn_t *cn, cn_t *cl, u_int8_t *run, u_long len, u_long *off)
1707 printf("ntfs_parsetun: run == NULL\n");
1712 printf("ntfs_parserun: trying to go out of run\n");
1716 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1717 printf("ntfs_parserun: " \
1718 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1722 for (i = 0; i < (sz & 0xF); i++)
1723 *cl += (u_int32_t) run[(*off)++] << (i << 3);
1726 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1727 printf("ntfs_parserun: " \
1728 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1732 for (i = 0; i < (sz & 0xF); i++)
1733 *cn += (u_int32_t) run[(*off)++] << (i << 3);
1740 * Process fixup routine on given buffer.
1743 ntfs_procfixups(struct ntfsmount *ntmp, u_int32_t magic, caddr_t buf,
1746 struct fixuphdr *fhp = (struct fixuphdr *) buf;
1752 if (fhp->fh_magic != magic) {
1753 printf("ntfs_procfixups: magic doesn't match: %08x != %08x\n",
1754 fhp->fh_magic, magic);
1757 if ((fhp->fh_fnum - 1) * ntmp->ntm_bps != len) {
1758 printf("ntfs_procfixups: " \
1759 "bad fixups number: %d for %ld bytes block\n",
1760 fhp->fh_fnum, (long)len); /* XXX printf kludge */
1763 if (fhp->fh_foff >= ntmp->ntm_spc * ntmp->ntm_mftrecsz * ntmp->ntm_bps) {
1764 printf("ntfs_procfixups: invalid offset: %x", fhp->fh_foff);
1767 fxp = (u_int16_t *) (buf + fhp->fh_foff);
1768 cfxp = (u_int16_t *) (buf + ntmp->ntm_bps - 2);
1770 for (i = 1; i < fhp->fh_fnum; i++, fxp++) {
1771 if (*cfxp != fixup) {
1772 printf("ntfs_procfixups: fixup %d doesn't match\n", i);
1776 cfxp = (u_int16_t *)(((caddr_t) cfxp) + ntmp->ntm_bps);
1783 ntfs_runtocn(cn_t *cn, struct ntfsmount *ntmp, u_int8_t *run, u_long len,
1793 printf("ntfs_runtocn: run: 0x%p, %ld bytes, vcn:%ld\n",
1794 run, len, (u_long) vcn);
1795 printf("ntfs_runtocn: run: ");
1796 for (i = 0; i < len; i++)
1797 printf("0x%02x ", run[i]);
1802 printf("ntfs_runtocn: run == NULL\n");
1806 if (run[off] == 0) {
1807 printf("ntfs_runtocn: vcn too big\n");
1811 error = ntfs_parserun(&ccn, &ccl, run, len, &off);
1813 printf("ntfs_runtocn: ntfs_parserun failed\n");
1816 } while (ccl <= vcn);
1823 * this initializes toupper table & dependant variables to be ready for
1827 ntfs_toupper_init(void)
1829 ntfs_toupper_tab = (wchar *) NULL;
1830 lockinit(&ntfs_toupper_lock, 0, "ntfs_toupper", 0, 0);
1831 ntfs_toupper_usecount = 0;
1835 * if the ntfs_toupper_tab[] is filled already, just raise use count;
1836 * otherwise read the data from the filesystem we are currently mounting
1839 ntfs_toupper_use(struct mount *mp, struct ntfsmount *ntmp)
1844 /* get exclusive access */
1845 LOCKMGR(&ntfs_toupper_lock, LK_EXCLUSIVE, NULL);
1847 /* only read the translation data from a file if it hasn't been
1849 if (ntfs_toupper_tab)
1853 * Read in Unicode lowercase -> uppercase translation file.
1854 * XXX for now, just the first 256 entries are used anyway,
1855 * so don't bother reading more
1857 MALLOC(ntfs_toupper_tab, wchar *, 65536 * sizeof(wchar),
1858 M_NTFSRDATA, M_WAITOK);
1860 if ((error = VFS_VGET(mp, NTFS_UPCASEINO, &vp)))
1862 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
1863 0, 65536*sizeof(wchar), (char *) ntfs_toupper_tab, NULL);
1867 ntfs_toupper_usecount++;
1868 LOCKMGR(&ntfs_toupper_lock, LK_RELEASE, NULL);
1873 * lower the use count and if it reaches zero, free the memory
1874 * tied by toupper table
1877 ntfs_toupper_unuse(void)
1879 /* get exclusive access */
1880 LOCKMGR(&ntfs_toupper_lock, LK_EXCLUSIVE, NULL);
1882 ntfs_toupper_usecount--;
1883 if (ntfs_toupper_usecount == 0) {
1884 FREE(ntfs_toupper_tab, M_NTFSRDATA);
1885 ntfs_toupper_tab = NULL;
1888 else if (ntfs_toupper_usecount < 0) {
1889 panic("ntfs_toupper_unuse(): use count negative: %d\n",
1890 ntfs_toupper_usecount);
1894 /* release the lock */
1895 LOCKMGR(&ntfs_toupper_lock, LK_RELEASE, NULL);
1899 ntfs_u28_init(struct ntfsmount *ntmp, wchar *u2w)
1904 MALLOC(u28, char **, 256 * sizeof(char*), M_TEMP, M_WAITOK | M_ZERO);
1906 for (i=0; i<256; i++) {
1907 h = (u2w[i] >> 8) & 0xFF;
1910 if (u28[h] == NULL) {
1911 MALLOC(u28[h], char *, 256 * sizeof(char), M_TEMP, M_WAITOK);
1912 for (j=0; j<256; j++)
1916 u28[h][l] = i & 0xFF;
1919 ntmp->ntm_u28 = u28;
1925 ntfs_u28_uninit(struct ntfsmount *ntmp)
1930 if (ntmp->ntm_u28 == NULL)
1933 u28 = ntmp->ntm_u28;
1935 for (i=0; i<256; i++)
1937 FREE(u28[i], M_TEMP);
1945 ntfs_82u_init(struct ntfsmount *ntmp, u_int16_t *u2w)
1950 MALLOC(_82u, wchar *, 256 * sizeof(wchar), M_TEMP, M_WAITOK);
1953 for (i=0; i<256; i++)
1956 for (i=0; i<128; i++)
1958 for (i=0; i<128; i++)
1959 _82u[i+128] = u2w[i];
1962 ntmp->ntm_82u = _82u;
1968 ntfs_82u_uninit(struct ntfsmount *ntmp)
1970 FREE(ntmp->ntm_82u, M_TEMP);
1975 * maps the Unicode char to 8bit equivalent
1976 * XXX currently only gets lower 8bit from the Unicode char
1977 * and substitutes a '_' for it if the result would be '\0';
1978 * something better has to be definitely though out
1981 ntfs_u28(struct ntfsmount *ntmp, wchar wc)
1985 p = ntmp->ntm_u28[(wc>>8)&0xFF];
1988 return (p[wc&0xFF]);