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.27 2008/01/05 14:02:41 swildner 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>
44 #include <sys/spinlock.h>
45 #include <sys/spinlock2.h>
46 #include <sys/iconv.h>
48 #include <machine/inttypes.h>
50 #if defined(__NetBSD__)
51 #include <miscfs/specfs/specdev.h>
54 /* #define NTFS_DEBUG 1 */
56 #include "ntfsmount.h"
57 #include "ntfs_inode.h"
58 #include "ntfs_vfsops.h"
59 #include "ntfs_subr.h"
60 #include "ntfs_compr.h"
61 #include "ntfs_ihash.h"
63 #if defined(__DragonFly__)
64 MALLOC_DEFINE(M_NTFSNTVATTR, "NTFS vattr", "NTFS file attribute information");
65 MALLOC_DEFINE(M_NTFSRDATA, "NTFS res data", "NTFS resident data");
66 MALLOC_DEFINE(M_NTFSRUN, "NTFS vrun", "NTFS vrun storage");
67 MALLOC_DEFINE(M_NTFSDECOMP, "NTFS decomp", "NTFS decompression temporary");
70 static int ntfs_ntlookupattr (struct ntfsmount *, const char *, int, int *, char **);
71 static int ntfs_findvattr (struct ntfsmount *, struct ntnode *, struct ntvattr **, struct ntvattr **, u_int32_t, const char *, size_t, cn_t);
72 static int ntfs_uastricmp (struct ntfsmount *, const wchar *, size_t, const char *, size_t);
73 static int ntfs_uastrcmp (struct ntfsmount *, const wchar *, size_t, const char *, size_t);
75 /* table for mapping Unicode chars into uppercase; it's filled upon first
76 * ntfs mount, freed upon last ntfs umount */
77 static wchar *ntfs_toupper_tab;
78 #define NTFS_TOUPPER(ch) (ntfs_toupper_tab[(ch)])
79 static struct lock ntfs_toupper_lock;
80 static signed int ntfs_toupper_usecount;
81 extern struct iconv_functions *ntfs_iconv;
83 /* support macro for ntfs_ntvattrget() */
84 #define NTFS_AALPCMP(aalp,type,name,namelen) ( \
85 (aalp->al_type == type) && (aalp->al_namelen == namelen) && \
86 !NTFS_UASTRCMP(aalp->al_name,aalp->al_namelen,name,namelen) )
92 ntfs_ntvattrrele(struct ntvattr *vap)
94 dprintf(("ntfs_ntvattrrele: ino: %"PRId64", type: 0x%x\n",
95 vap->va_ip->i_number, vap->va_type));
97 ntfs_ntrele(vap->va_ip);
103 * find the attribute in the ntnode
106 ntfs_findvattr(struct ntfsmount *ntmp, struct ntnode *ip,
107 struct ntvattr **lvapp, struct ntvattr **vapp, u_int32_t type,
108 const char *name, size_t namelen, cn_t vcn)
113 if((ip->i_flag & IN_LOADED) == 0) {
114 dprintf(("ntfs_findvattr: node not loaded, ino: %"PRId64"\n",
116 error = ntfs_loadntnode(ntmp,ip);
118 kprintf("ntfs_findvattr: FAILED TO LOAD INO: %"PRId64"\n",
126 for (vap = ip->i_valist.lh_first; vap; vap = vap->va_list.le_next) {
127 ddprintf(("ntfs_findvattr: type: 0x%x, vcn: %d - %d\n", \
128 vap->va_type, (u_int32_t) vap->va_vcnstart, \
129 (u_int32_t) vap->va_vcnend));
130 if ((vap->va_type == type) &&
131 (vap->va_vcnstart <= vcn) && (vap->va_vcnend >= vcn) &&
132 (vap->va_namelen == namelen) &&
133 (strncmp(name, vap->va_name, namelen) == 0)) {
135 ntfs_ntref(vap->va_ip);
138 if (vap->va_type == NTFS_A_ATTRLIST)
146 * Search attribute specifed in ntnode (load ntnode if nessecary).
147 * If not found but ATTR_A_ATTRLIST present, read it in and search throught.
148 * VOP_VGET node needed, and lookup througth it's ntnode (load if nessesary).
150 * ntnode should be locked
153 ntfs_ntvattrget(struct ntfsmount *ntmp, struct ntnode *ip, u_int32_t type,
154 const char *name, cn_t vcn, struct ntvattr **vapp)
156 struct ntvattr *lvap = NULL;
157 struct attr_attrlist *aalp;
158 struct attr_attrlist *nextaalp;
160 struct ntnode *newip;
168 dprintf(("ntfs_ntvattrget: " \
169 "ino: %"PRId64", type: 0x%x, name: %s, vcn: %d\n", \
170 ip->i_number, type, name, (u_int32_t) vcn));
171 namelen = strlen(name);
173 dprintf(("ntfs_ntvattrget: " \
174 "ino: %"PRId64", type: 0x%x, vcn: %d\n", \
175 ip->i_number, type, (u_int32_t) vcn));
180 error = ntfs_findvattr(ntmp, ip, &lvap, vapp, type, name, namelen, vcn);
185 dprintf(("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: " \
186 "ino: %"PRId64", type: 0x%x, name: %s, vcn: %d\n", \
187 ip->i_number, type, name, (u_int32_t) vcn));
190 /* Scan $ATTRIBUTE_LIST for requested attribute */
191 len = lvap->va_datalen;
192 MALLOC(alpool, caddr_t, len, M_TEMP, M_WAITOK);
193 error = ntfs_readntvattr_plain(ntmp, ip, lvap, 0, len, alpool, &len,
198 aalp = (struct attr_attrlist *) alpool;
201 for(; len > 0; aalp = nextaalp) {
202 dprintf(("ntfs_ntvattrget: " \
203 "attrlist: ino: %d, attr: 0x%x, vcn: %d\n", \
204 aalp->al_inumber, aalp->al_type, \
205 (u_int32_t) aalp->al_vcnstart));
207 if (len > aalp->reclen) {
208 nextaalp = NTFS_NEXTREC(aalp, struct attr_attrlist *);
214 if (!NTFS_AALPCMP(aalp, type, name, namelen) ||
215 (nextaalp && (nextaalp->al_vcnstart <= vcn) &&
216 NTFS_AALPCMP(nextaalp, type, name, namelen)))
219 dprintf(("ntfs_ntvattrget: attribute in ino: %d\n",
222 /* this is not a main record, so we can't use just plain
224 error = ntfs_vgetex(ntmp->ntm_mountp, aalp->al_inumber,
225 NTFS_A_DATA, NULL, LK_EXCLUSIVE,
226 VG_EXT, curthread, &newvp);
228 kprintf("ntfs_ntvattrget: CAN'T VGET INO: %d\n",
232 newip = VTONT(newvp);
233 /* XXX have to lock ntnode */
234 error = ntfs_findvattr(ntmp, newip, &lvap, vapp,
235 type, name, namelen, vcn);
239 kprintf("ntfs_ntvattrget: ATTRLIST ERROR.\n");
244 dprintf(("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: " \
245 "ino: %"PRId64", type: 0x%x, name: %.*s, vcn: %d\n", \
246 ip->i_number, type, (int) namelen, name, (u_int32_t) vcn));
248 FREE(alpool, M_TEMP);
253 * Read ntnode from disk, make ntvattr list.
255 * ntnode should be locked
258 ntfs_loadntnode(struct ntfsmount *ntmp, struct ntnode *ip)
260 struct filerec *mfrp;
264 struct ntvattr *nvap;
266 dprintf(("ntfs_loadntnode: loading ino: %"PRId64"\n",ip->i_number));
268 MALLOC(mfrp, struct filerec *, ntfs_bntob(ntmp->ntm_bpmftrec),
271 if (ip->i_number < NTFS_SYSNODESNUM) {
274 dprintf(("ntfs_loadntnode: read system node\n"));
276 bn = ntfs_cntobn(ntmp->ntm_mftcn) +
277 ntmp->ntm_bpmftrec * ip->i_number;
279 error = bread(ntmp->ntm_devvp,
280 ntfs_bntodoff(bn), ntfs_bntob(ntmp->ntm_bpmftrec), &bp);
282 kprintf("ntfs_loadntnode: BREAD FAILED\n");
286 memcpy(mfrp, bp->b_data, ntfs_bntob(ntmp->ntm_bpmftrec));
291 vp = ntmp->ntm_sysvn[NTFS_MFTINO];
292 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
293 ip->i_number * ntfs_bntob(ntmp->ntm_bpmftrec),
294 ntfs_bntob(ntmp->ntm_bpmftrec), mfrp, NULL);
296 kprintf("ntfs_loadntnode: ntfs_readattr failed\n");
301 /* Check if magic and fixups are correct */
302 error = ntfs_procfixups(ntmp, NTFS_FILEMAGIC, (caddr_t)mfrp,
303 ntfs_bntob(ntmp->ntm_bpmftrec));
305 kprintf("ntfs_loadntnode: BAD MFT RECORD %"PRId64"\n",
310 dprintf(("ntfs_loadntnode: load attrs for ino: %"PRId64"\n",ip->i_number));
311 off = mfrp->fr_attroff;
312 ap = (struct attr *) ((caddr_t)mfrp + off);
314 LIST_INIT(&ip->i_valist);
316 while (ap->a_hdr.a_type != -1) {
317 error = ntfs_attrtontvattr(ntmp, &nvap, ap);
322 LIST_INSERT_HEAD(&ip->i_valist, nvap, va_list);
324 off += ap->a_hdr.reclen;
325 ap = (struct attr *) ((caddr_t)mfrp + off);
328 kprintf("ntfs_loadntnode: failed to load attr ino: %"PRId64"\n",
333 ip->i_mainrec = mfrp->fr_mainrec;
334 ip->i_nlink = mfrp->fr_nlink;
335 ip->i_frflag = mfrp->fr_flags;
337 ip->i_flag |= IN_LOADED;
345 * Routine locks ntnode and increase usecount, just opposite of
349 ntfs_ntget(struct ntnode *ip)
351 dprintf(("ntfs_ntget: get ntnode %"PRId64": %p, usecount: %d\n",
352 ip->i_number, ip, ip->i_usecount));
354 ip->i_usecount++; /* ZZZ */
355 LOCKMGR(&ip->i_lock, LK_EXCLUSIVE);
361 * Routine search ntnode in hash, if found: lock, inc usecount and return.
362 * If not in hash allocate structure for ntnode, prefill it, lock,
363 * inc count and return.
365 * ntnode returned locked
368 ntfs_ntlookup(struct ntfsmount *ntmp, ino_t ino, struct ntnode **ipp)
372 dprintf(("ntfs_ntlookup: looking for ntnode %d\n", ino));
375 if ((ip = ntfs_nthashlookup(ntmp->ntm_dev, ino)) != NULL) {
377 dprintf(("ntfs_ntlookup: ntnode %d: %p, usecount: %d\n",
378 ino, ip, ip->i_usecount));
382 } while (LOCKMGR(&ntfs_hashlock, LK_EXCLUSIVE | LK_SLEEPFAIL));
384 MALLOC(ip, struct ntnode *, sizeof(struct ntnode),
385 M_NTFSNTNODE, M_WAITOK | M_ZERO);
386 ddprintf(("ntfs_ntlookup: allocating ntnode: %d: %p\n", ino, ip));
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, "ntnode", 0, LK_EXCLUSIVE);
399 spin_init(&ip->i_interlock);
404 LOCKMGR(&ntfs_hashlock, LK_RELEASE);
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)
425 dprintf(("ntfs_ntput: rele ntnode %"PRId64": %p, usecount: %d\n",
426 ip->i_number, ip, ip->i_usecount));
428 spin_lock_wr(&ip->i_interlock);
432 if (ip->i_usecount < 0) {
433 spin_unlock_wr(&ip->i_interlock);
434 panic("ntfs_ntput: ino: %"PRId64" usecount: %d \n",
435 ip->i_number,ip->i_usecount);
439 if (ip->i_usecount > 0) {
440 spin_unlock_wr(&ip->i_interlock);
441 LOCKMGR(&ip->i_lock, LK_RELEASE);
445 dprintf(("ntfs_ntput: deallocating ntnode: %"PRId64"\n", ip->i_number));
447 if (ip->i_fnlist.lh_first) {
448 spin_unlock_wr(&ip->i_interlock);
449 panic("ntfs_ntput: ntnode has fnodes\n");
453 * XXX this is a bit iffy because we are making high level calls
454 * while holding a spinlock.
458 while ((vap = LIST_FIRST(&ip->i_valist)) != NULL) {
459 LIST_REMOVE(vap,va_list);
460 ntfs_freentvattr(vap);
462 spin_unlock_wr(&ip->i_interlock);
464 FREE(ip, M_NTFSNTNODE);
468 * increment usecount of ntnode
471 ntfs_ntref(struct ntnode *ip)
475 dprintf(("ntfs_ntref: ino %"PRId64", usecount: %d\n",
476 ip->i_number, ip->i_usecount));
481 * Decrement usecount of ntnode.
484 ntfs_ntrele(struct ntnode *ip)
486 dprintf(("ntfs_ntrele: rele ntnode %"PRId64": %p, usecount: %d\n",
487 ip->i_number, ip, ip->i_usecount));
489 spin_lock_wr(&ip->i_interlock);
492 if (ip->i_usecount < 0) {
493 spin_unlock_wr(&ip->i_interlock);
494 panic("ntfs_ntrele: ino: %"PRId64" usecount: %d \n",
495 ip->i_number,ip->i_usecount);
497 spin_unlock_wr(&ip->i_interlock);
501 * Deallocate all memory allocated for ntvattr
504 ntfs_freentvattr(struct ntvattr *vap)
506 if (vap->va_flag & NTFS_AF_INRUN) {
508 FREE(vap->va_vruncn, M_NTFSRUN);
510 FREE(vap->va_vruncl, M_NTFSRUN);
513 FREE(vap->va_datap, M_NTFSRDATA);
515 FREE(vap, M_NTFSNTVATTR);
519 * Convert disk image of attribute into ntvattr structure,
520 * runs are expanded also.
523 ntfs_attrtontvattr(struct ntfsmount *ntmp, struct ntvattr **rvapp,
532 MALLOC(vap, struct ntvattr *, sizeof(struct ntvattr),
533 M_NTFSNTVATTR, M_WAITOK | M_ZERO);
535 vap->va_flag = rap->a_hdr.a_flag;
536 vap->va_type = rap->a_hdr.a_type;
537 vap->va_compression = rap->a_hdr.a_compression;
538 vap->va_index = rap->a_hdr.a_index;
540 ddprintf(("type: 0x%x, index: %d", vap->va_type, vap->va_index));
542 vap->va_namelen = rap->a_hdr.a_namelen;
543 if (rap->a_hdr.a_namelen) {
544 wchar *unp = (wchar *) ((caddr_t) rap + rap->a_hdr.a_nameoff);
545 ddprintf((", name:["));
546 for (i = 0; i < vap->va_namelen; i++) {
547 vap->va_name[i] = unp[i];
548 ddprintf(("%c", vap->va_name[i]));
552 if (vap->va_flag & NTFS_AF_INRUN) {
553 ddprintf((", nonres."));
554 vap->va_datalen = rap->a_nr.a_datalen;
555 vap->va_allocated = rap->a_nr.a_allocated;
556 vap->va_vcnstart = rap->a_nr.a_vcnstart;
557 vap->va_vcnend = rap->a_nr.a_vcnend;
558 vap->va_compressalg = rap->a_nr.a_compressalg;
559 error = ntfs_runtovrun(&(vap->va_vruncn), &(vap->va_vruncl),
561 (caddr_t) rap + rap->a_nr.a_dataoff);
563 vap->va_compressalg = 0;
564 ddprintf((", res."));
565 vap->va_datalen = rap->a_r.a_datalen;
566 vap->va_allocated = rap->a_r.a_datalen;
567 vap->va_vcnstart = 0;
568 vap->va_vcnend = ntfs_btocn(vap->va_allocated);
569 MALLOC(vap->va_datap, caddr_t, vap->va_datalen,
570 M_NTFSRDATA, M_WAITOK);
571 memcpy(vap->va_datap, (caddr_t) rap + rap->a_r.a_dataoff,
574 ddprintf((", len: %d", vap->va_datalen));
577 FREE(vap, M_NTFSNTVATTR);
587 * Expand run into more utilizable and more memory eating format.
590 ntfs_runtovrun(cn_t **rcnp, cn_t **rclp, u_long *rcntp, u_int8_t *run)
604 off += (run[off] & 0xF) + ((run[off] >> 4) & 0xF) + 1;
607 MALLOC(cn, cn_t *, cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
608 MALLOC(cl, cn_t *, cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
618 for (i = 0; i < (sz & 0xF); i++)
619 cl[cnt] += (u_int32_t) run[off++] << (i << 3);
622 if (run[off + sz - 1] & 0x80) {
623 tmp = ((u_int64_t) - 1) << (sz << 3);
624 for (i = 0; i < sz; i++)
625 tmp |= (u_int64_t) run[off++] << (i << 3);
628 for (i = 0; i < sz; i++)
629 tmp |= (u_int64_t) run[off++] << (i << 3);
632 prev = cn[cnt] = prev + tmp;
645 * Compare unicode and ascii string case insens.
648 ntfs_uastricmp(struct ntfsmount *ntmp, const wchar *ustr, size_t ustrlen,
649 const char *astr, size_t astrlen)
652 size_t i, j, mbstrlen = astrlen;
656 if (ntmp->ntm_ic_l2u) {
657 for (i = 0, j = 0; i < ustrlen && j < astrlen; i++, j++) {
658 if (j < astrlen -1) {
659 wc = (wchar)astr[j]<<8 | (astr[j+1]&0xFF);
662 wc = (wchar)astr[j]<<8 & 0xFF00;
665 res = ((int) NTFS_TOUPPER(ustr[i])) -
666 ((int)NTFS_TOUPPER(NTFS_82U(wc, &len)));
675 * We use NTFS_82U(NTFS_U28(c)) to get rid of unicode
676 * symbols not covered by translation table
678 for (i = 0; i < ustrlen && i < astrlen; i++) {
679 res = ((int) NTFS_TOUPPER(NTFS_82U(NTFS_U28(ustr[i]), &len))) -
680 ((int)NTFS_TOUPPER(NTFS_82U((wchar)astr[i], &len)));
685 return (ustrlen - mbstrlen);
689 * Compare unicode and ascii string case sens.
692 ntfs_uastrcmp(struct ntfsmount *ntmp, const wchar *ustr, size_t ustrlen,
693 const char *astr, size_t astrlen)
696 size_t i, j, mbstrlen = astrlen;
700 for (i = 0, j = 0; (i < ustrlen) && (j < astrlen); i++, j++) {
702 wc = NTFS_U28(ustr[i]);
705 if (u != '\0' && j < astrlen -1) {
706 res = (int) (u - astr[j++]);
709 res = (res<<8) + (int) (l - astr[j]);
713 return (ustrlen - mbstrlen);
717 * Search fnode in ntnode, if not found allocate and preinitialize.
719 * ntnode should be locked on entry.
722 ntfs_fget(struct ntfsmount *ntmp, struct ntnode *ip, int attrtype,
723 char *attrname, struct fnode **fpp)
727 dprintf(("ntfs_fget: ino: %"PRId64", attrtype: 0x%x, attrname: %s\n",
728 ip->i_number,attrtype, attrname?attrname:""));
730 for (fp = ip->i_fnlist.lh_first; fp != NULL; fp = fp->f_fnlist.le_next){
731 dprintf(("ntfs_fget: fnode: attrtype: %d, attrname: %s\n",
732 fp->f_attrtype, fp->f_attrname?fp->f_attrname:""));
734 if ((attrtype == fp->f_attrtype) &&
735 ((!attrname && !fp->f_attrname) ||
736 (attrname && fp->f_attrname &&
737 !strcmp(attrname,fp->f_attrname)))){
738 dprintf(("ntfs_fget: found existed: %p\n",fp));
746 MALLOC(fp, struct fnode *, sizeof(struct fnode), M_NTFSFNODE,
748 dprintf(("ntfs_fget: allocating fnode: %p\n",fp));
752 fp->f_flag |= FN_AATTRNAME;
753 MALLOC(fp->f_attrname, char *, strlen(attrname)+1, M_TEMP, M_WAITOK);
754 strcpy(fp->f_attrname, attrname);
756 fp->f_attrname = NULL;
757 fp->f_attrtype = attrtype;
761 LIST_INSERT_HEAD(&ip->i_fnlist, fp, f_fnlist);
769 * Deallocate fnode, remove it from ntnode's fnode list.
771 * ntnode should be locked.
774 ntfs_frele(struct fnode *fp)
776 struct ntnode *ip = FTONT(fp);
778 dprintf(("ntfs_frele: fnode: %p for %"PRId64": %p\n", fp, ip->i_number, ip));
780 dprintf(("ntfs_frele: deallocating fnode\n"));
781 LIST_REMOVE(fp,f_fnlist);
782 if (fp->f_flag & FN_AATTRNAME)
783 FREE(fp->f_attrname, M_TEMP);
785 FREE(fp->f_dirblbuf, M_NTFSDIR);
786 FREE(fp, M_NTFSFNODE);
791 * Lookup attribute name in format: [[:$ATTR_TYPE]:$ATTR_NAME],
792 * $ATTR_TYPE is searched in attrdefs read from $AttrDefs.
793 * If $ATTR_TYPE nott specifed, ATTR_A_DATA assumed.
796 ntfs_ntlookupattr(struct ntfsmount *ntmp, const char *name, int namelen,
797 int *attrtype, char **attrname)
801 struct ntvattrdef *adp;
806 if (name[0] == '$') {
808 for (syslen = 0; syslen < namelen; syslen++) {
809 if(sys[syslen] == ':') {
819 for (i = 0; i < ntmp->ntm_adnum; i++, adp++){
820 if (syslen != adp->ad_namelen ||
821 strncmp(sys, adp->ad_name, syslen) != 0)
824 *attrtype = adp->ad_type;
829 *attrtype = NTFS_A_DATA;
833 MALLOC((*attrname), char *, namelen, M_TEMP, M_WAITOK);
834 memcpy((*attrname), name, namelen);
835 (*attrname)[namelen] = '\0';
842 * Lookup specifed node for filename, matching cnp,
843 * return fnode filled.
846 ntfs_ntlookupfile(struct ntfsmount *ntmp, struct vnode *vp,
847 struct componentname *cnp, struct vnode **vpp)
849 struct fnode *fp = VTOF(vp);
850 struct ntnode *ip = FTONT(fp);
851 struct ntvattr *vap; /* Root attribute */
852 cn_t cn; /* VCN in current attribute */
853 caddr_t rdbuf; /* Buffer to read directory's blocks */
855 u_int32_t rdsize; /* Length of data to read from current block */
856 struct attr_indexentry *iep;
857 int error, res, anamelen, fnamelen;
858 const char *fname,*aname;
860 int attrtype = NTFS_A_DATA;
861 char *attrname = NULL;
866 error = ntfs_ntget(ip);
870 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
871 if (error || (vap->va_flag & NTFS_AF_INRUN))
874 blsize = vap->va_a_iroot->ir_size;
875 rdsize = vap->va_datalen;
878 * Divide file name into: foofilefoofilefoofile[:attrspec]
879 * Store like this: fname:fnamelen [aname:anamelen]
881 fname = cnp->cn_nameptr;
884 for (fnamelen = 0; fnamelen < cnp->cn_namelen; fnamelen++)
885 if(fname[fnamelen] == ':') {
886 aname = fname + fnamelen + 1;
887 anamelen = cnp->cn_namelen - fnamelen - 1;
888 dprintf(("ntfs_ntlookupfile: %s (%d), attr: %s (%d)\n",
889 fname, fnamelen, aname, anamelen));
893 dprintf(("ntfs_ntlookupfile: blksz: %d, rdsz: %d\n", blsize, rdsize));
895 MALLOC(rdbuf, caddr_t, blsize, M_TEMP, M_WAITOK);
897 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXROOT, "$I30",
898 0, rdsize, rdbuf, NULL);
902 aoff = sizeof(struct attr_indexroot);
905 iep = (struct attr_indexentry *) (rdbuf + aoff);
907 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
909 iep = (struct attr_indexentry *) (rdbuf + aoff))
911 ddprintf(("scan: %d, %d\n",
912 (u_int32_t) iep->ie_number,
913 (u_int32_t) iep->ie_fnametype));
915 /* check the name - the case-insensitible check
916 * has to come first, to break from this for loop
917 * if needed, so we can dive correctly */
918 res = NTFS_UASTRICMP(iep->ie_fname, iep->ie_fnamelen,
921 if (res < 0) continue;
923 if (iep->ie_fnametype == 0 ||
924 !(ntmp->ntm_flag & NTFS_MFLAG_CASEINS))
926 res = NTFS_UASTRCMP(iep->ie_fname,
927 iep->ie_fnamelen, fname, fnamelen);
928 if (res != 0) continue;
932 error = ntfs_ntlookupattr(ntmp,
934 &attrtype, &attrname);
939 /* Check if we've found ourself */
940 if ((iep->ie_number == ip->i_number) &&
941 (attrtype == fp->f_attrtype) &&
942 ((!attrname && !fp->f_attrname) ||
943 (attrname && fp->f_attrname &&
944 !strcmp(attrname, fp->f_attrname))))
952 /* vget node, but don't load it */
953 error = ntfs_vgetex(ntmp->ntm_mountp,
954 iep->ie_number, attrtype, attrname,
955 LK_EXCLUSIVE, VG_DONTLOADIN | VG_DONTVALIDFN,
958 /* free the buffer returned by ntfs_ntlookupattr() */
960 FREE(attrname, M_TEMP);
969 if (nfp->f_flag & FN_VALID) {
974 nfp->f_fflag = iep->ie_fflag;
975 nfp->f_pnumber = iep->ie_fpnumber;
976 nfp->f_times = iep->ie_ftimes;
978 if((nfp->f_fflag & NTFS_FFLAG_DIR) &&
979 (nfp->f_attrtype == NTFS_A_DATA) &&
980 (nfp->f_attrname == NULL))
985 nvp->v_type = f_type;
987 if ((nfp->f_attrtype == NTFS_A_DATA) &&
988 (nfp->f_attrname == NULL))
990 /* Opening default attribute */
991 nfp->f_size = iep->ie_fsize;
992 nfp->f_allocated = iep->ie_fallocated;
993 nfp->f_flag |= FN_PRELOADED;
995 error = ntfs_filesize(ntmp, nfp,
996 &nfp->f_size, &nfp->f_allocated);
1002 nfp->f_flag &= ~FN_VALID;
1005 * Normal files use the buffer cache
1007 if (nvp->v_type == VREG)
1008 vinitvmio(nvp, nfp->f_size);
1013 /* Dive if possible */
1014 if (iep->ie_flag & NTFS_IEFLAG_SUBNODE) {
1015 dprintf(("ntfs_ntlookupfile: diving\n"));
1017 cn = *(cn_t *) (rdbuf + aoff +
1018 iep->reclen - sizeof(cn_t));
1021 error = ntfs_readattr(ntmp, ip, NTFS_A_INDX, "$I30",
1022 ntfs_cntob(cn), rdsize, rdbuf, NULL);
1026 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1031 aoff = (((struct attr_indexalloc *) rdbuf)->ia_hdrsize +
1034 dprintf(("ntfs_ntlookupfile: nowhere to dive :-(\n"));
1040 dprintf(("finish\n"));
1043 if (attrname) FREE(attrname, M_TEMP);
1044 ntfs_ntvattrrele(vap);
1046 FREE(rdbuf, M_TEMP);
1051 * Check if name type is permitted to show.
1054 ntfs_isnamepermitted(struct ntfsmount *ntmp, struct attr_indexentry *iep)
1056 if (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)
1059 switch (iep->ie_fnametype) {
1061 ddprintf(("ntfs_isnamepermitted: skiped DOS name\n"));
1063 case 0: case 1: case 3:
1066 kprintf("ntfs_isnamepermitted: " \
1067 "WARNING! Unknown file name type: %d\n",
1075 * Read ntfs dir like stream of attr_indexentry, not like btree of them.
1076 * This is done by scaning $BITMAP:$I30 for busy clusters and reading them.
1077 * Ofcouse $INDEX_ROOT:$I30 is read before. Last read values are stored in
1078 * fnode, so we can skip toward record number num almost immediatly.
1079 * Anyway this is rather slow routine. The problem is that we don't know
1080 * how many records are there in $INDEX_ALLOCATION:$I30 block.
1083 ntfs_ntreaddir(struct ntfsmount *ntmp, struct fnode *fp,
1084 u_int32_t num, struct attr_indexentry **riepp)
1086 struct ntnode *ip = FTONT(fp);
1087 struct ntvattr *vap = NULL; /* IndexRoot attribute */
1088 struct ntvattr *bmvap = NULL; /* BitMap attribute */
1089 struct ntvattr *iavap = NULL; /* IndexAllocation attribute */
1090 caddr_t rdbuf; /* Buffer to read directory's blocks */
1091 u_char *bmp = NULL; /* Bitmap */
1092 u_int32_t blsize; /* Index allocation size (2048) */
1093 u_int32_t rdsize; /* Length of data to read */
1094 u_int32_t attrnum; /* Current attribute type */
1095 u_int32_t cpbl = 1; /* Clusters per directory block */
1097 struct attr_indexentry *iep;
1099 u_int32_t aoff, cnum;
1101 dprintf(("ntfs_ntreaddir: read ino: %"PRId64", num: %d\n", ip->i_number, num));
1102 error = ntfs_ntget(ip);
1106 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
1110 if (fp->f_dirblbuf == NULL) {
1111 fp->f_dirblsz = vap->va_a_iroot->ir_size;
1112 MALLOC(fp->f_dirblbuf, caddr_t,
1113 max(vap->va_datalen,fp->f_dirblsz), M_NTFSDIR, M_WAITOK);
1116 blsize = fp->f_dirblsz;
1117 rdbuf = fp->f_dirblbuf;
1119 dprintf(("ntfs_ntreaddir: rdbuf: 0x%p, blsize: %d\n", rdbuf, blsize));
1121 if (vap->va_a_iroot->ir_flag & NTFS_IRFLAG_INDXALLOC) {
1122 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXBITMAP, "$I30",
1128 MALLOC(bmp, u_char *, bmvap->va_datalen, M_TEMP, M_WAITOK);
1129 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXBITMAP, "$I30", 0,
1130 bmvap->va_datalen, bmp, NULL);
1134 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDX, "$I30",
1140 cpbl = ntfs_btocn(blsize + ntfs_cntob(1) - 1);
1141 dprintf(("ntfs_ntreaddir: indexalloc: %d, cpbl: %d\n",
1142 iavap->va_datalen, cpbl));
1144 dprintf(("ntfs_ntreadidir: w/o BitMap and IndexAllocation\n"));
1145 iavap = bmvap = NULL;
1149 /* Try use previous values */
1150 if ((fp->f_lastdnum < num) && (fp->f_lastdnum != 0)) {
1151 attrnum = fp->f_lastdattr;
1152 aoff = fp->f_lastdoff;
1153 blnum = fp->f_lastdblnum;
1154 cnum = fp->f_lastdnum;
1156 attrnum = NTFS_A_INDXROOT;
1157 aoff = sizeof(struct attr_indexroot);
1163 dprintf(("ntfs_ntreaddir: scan: 0x%x, %d, %d, %d, %d\n",
1164 attrnum, (u_int32_t) blnum, cnum, num, aoff));
1165 rdsize = (attrnum == NTFS_A_INDXROOT) ? vap->va_datalen : blsize;
1166 error = ntfs_readattr(ntmp, ip, attrnum, "$I30",
1167 ntfs_cntob(blnum * cpbl), rdsize, rdbuf, NULL);
1171 if (attrnum == NTFS_A_INDX) {
1172 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1178 aoff = (attrnum == NTFS_A_INDX) ?
1179 (0x18 + ((struct attr_indexalloc *) rdbuf)->ia_hdrsize) :
1180 sizeof(struct attr_indexroot);
1182 iep = (struct attr_indexentry *) (rdbuf + aoff);
1183 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
1184 aoff += iep->reclen,
1185 iep = (struct attr_indexentry *) (rdbuf + aoff))
1187 if (!ntfs_isnamepermitted(ntmp, iep)) continue;
1190 fp->f_lastdnum = cnum;
1191 fp->f_lastdoff = aoff;
1192 fp->f_lastdblnum = blnum;
1193 fp->f_lastdattr = attrnum;
1204 if (attrnum == NTFS_A_INDXROOT)
1209 while (ntfs_cntob(blnum * cpbl) < iavap->va_datalen) {
1210 if (bmp[blnum >> 3] & (1 << (blnum & 3)))
1215 attrnum = NTFS_A_INDX;
1217 if (ntfs_cntob(blnum * cpbl) >= iavap->va_datalen)
1219 dprintf(("ntfs_ntreaddir: blnum: %d\n", (u_int32_t) blnum));
1228 ntfs_ntvattrrele(vap);
1230 ntfs_ntvattrrele(bmvap);
1232 ntfs_ntvattrrele(iavap);
1240 * Convert NTFS times that are in 100 ns units and begins from
1241 * 1601 Jan 1 into unix times.
1244 ntfs_nttimetounix(u_int64_t nt)
1248 /* WindowNT times are in 100 ns and from 1601 Jan 1 */
1249 t.tv_nsec = (nt % (1000 * 1000 * 10)) * 100;
1250 t.tv_sec = nt / (1000 * 1000 * 10) -
1251 369LL * 365LL * 24LL * 60LL * 60LL -
1252 89LL * 1LL * 24LL * 60LL * 60LL;
1257 * Get file times from NTFS_A_NAME attribute.
1260 ntfs_times(struct ntfsmount *ntmp, struct ntnode *ip, ntfs_times_t *tm)
1262 struct ntvattr *vap;
1265 dprintf(("ntfs_times: ino: %"PRId64"...\n", ip->i_number));
1267 error = ntfs_ntget(ip);
1271 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_NAME, NULL, 0, &vap);
1276 *tm = vap->va_a_name->n_times;
1277 ntfs_ntvattrrele(vap);
1284 * Get file sizes from corresponding attribute.
1286 * ntnode under fnode should be locked.
1289 ntfs_filesize(struct ntfsmount *ntmp, struct fnode *fp, u_int64_t *size,
1292 struct ntvattr *vap;
1293 struct ntnode *ip = FTONT(fp);
1297 dprintf(("ntfs_filesize: ino: %"PRId64"\n", ip->i_number));
1299 error = ntfs_ntvattrget(ntmp, ip,
1300 fp->f_attrtype, fp->f_attrname, 0, &vap);
1304 bn = vap->va_allocated;
1305 sz = vap->va_datalen;
1307 dprintf(("ntfs_filesize: %d bytes (%d bytes allocated)\n",
1308 (u_int32_t) sz, (u_int32_t) bn));
1315 ntfs_ntvattrrele(vap);
1321 * This is one of write routine.
1324 ntfs_writeattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1325 u_int32_t attrnum, char *attrname, off_t roff,
1326 size_t rsize, void *rdata, size_t *initp,
1331 off_t off = roff, left = rsize, towrite;
1332 caddr_t data = rdata;
1333 struct ntvattr *vap;
1337 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1338 ntfs_btocn(off), &vap);
1341 towrite = min(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1342 ddprintf(("ntfs_writeattr_plain: o: %d, s: %d (%d - %d)\n",
1343 (u_int32_t) off, (u_int32_t) towrite,
1344 (u_int32_t) vap->va_vcnstart,
1345 (u_int32_t) vap->va_vcnend));
1346 error = ntfs_writentvattr_plain(ntmp, ip, vap,
1347 off - ntfs_cntob(vap->va_vcnstart),
1348 towrite, data, &init, uio);
1350 kprintf("ntfs_writeattr_plain: " \
1351 "ntfs_writentvattr_plain failed: o: %d, s: %d\n",
1352 (u_int32_t) off, (u_int32_t) towrite);
1353 kprintf("ntfs_writeattr_plain: attrib: %d - %d\n",
1354 (u_int32_t) vap->va_vcnstart,
1355 (u_int32_t) vap->va_vcnend);
1356 ntfs_ntvattrrele(vap);
1359 ntfs_ntvattrrele(vap);
1362 data = data + towrite;
1370 * This is one of write routine.
1372 * ntnode should be locked.
1375 ntfs_writentvattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1376 struct ntvattr *vap, off_t roff, size_t rsize,
1377 void *rdata, size_t *initp, struct uio *uio)
1382 cn_t ccn, ccl, cn, left, cl;
1383 caddr_t data = rdata;
1389 if ((vap->va_flag & NTFS_AF_INRUN) == 0) {
1390 kprintf("ntfs_writevattr_plain: CAN'T WRITE RES. ATTRIBUTE\n");
1394 ddprintf(("ntfs_writentvattr_plain: data in run: %ld chains\n",
1402 for (; left && (cnt < vap->va_vruncnt); cnt++) {
1403 ccn = vap->va_vruncn[cnt];
1404 ccl = vap->va_vruncl[cnt];
1406 ddprintf(("ntfs_writentvattr_plain: " \
1407 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1408 (u_int32_t) left, (u_int32_t) ccn, \
1409 (u_int32_t) ccl, (u_int32_t) off));
1411 if (ntfs_cntob(ccl) < off) {
1412 off -= ntfs_cntob(ccl);
1416 if (!ccn && ip->i_number != NTFS_BOOTINO)
1419 ccl -= ntfs_btocn(off);
1420 cn = ccn + ntfs_btocn(off);
1421 off = ntfs_btocnoff(off);
1423 while (left && ccl) {
1424 #if defined(__DragonFly__)
1426 min(ntfs_cntob(ccl) - off, MAXBSIZE - off));
1428 /* under NetBSD, bread() can read
1429 * maximum one block worth of data */
1430 tocopy = min(left, ntmp->ntm_bps - off);
1432 cl = ntfs_btocl(tocopy + off);
1433 ddprintf(("ntfs_writentvattr_plain: write: " \
1434 "cn: 0x%x cl: %d, off: %d len: %d, left: %d\n",
1435 (u_int32_t) cn, (u_int32_t) cl,
1436 (u_int32_t) off, (u_int32_t) tocopy,
1438 if (off == 0 && tocopy == ntfs_cntob(cl) &&
1439 uio->uio_segflg != UIO_NOCOPY) {
1440 bp = getblk(ntmp->ntm_devvp, ntfs_cntodoff(cn),
1441 ntfs_cntob(cl), 0, 0);
1444 error = bread(ntmp->ntm_devvp,
1446 ntfs_cntob(cl), &bp);
1453 uiomove(bp->b_data + off, tocopy, uio);
1455 memcpy(bp->b_data + off, data, tocopy);
1457 data = data + tocopy;
1467 kprintf("ntfs_writentvattr_plain: POSSIBLE RUN ERROR\n");
1475 * This is one of read routines.
1477 * ntnode should be locked.
1480 ntfs_readntvattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1481 struct ntvattr *vap, off_t roff, size_t rsize,
1482 void *rdata, size_t *initp, struct uio *uio)
1488 if (vap->va_flag & NTFS_AF_INRUN) {
1490 cn_t ccn, ccl, cn, left, cl;
1491 caddr_t data = rdata;
1495 ddprintf(("ntfs_readntvattr_plain: data in run: %ld chains\n",
1503 while (left && (cnt < vap->va_vruncnt)) {
1504 ccn = vap->va_vruncn[cnt];
1505 ccl = vap->va_vruncl[cnt];
1507 ddprintf(("ntfs_readntvattr_plain: " \
1508 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1509 (u_int32_t) left, (u_int32_t) ccn, \
1510 (u_int32_t) ccl, (u_int32_t) off));
1512 if (ntfs_cntob(ccl) < off) {
1513 off -= ntfs_cntob(ccl);
1517 if (ccn || ip->i_number == NTFS_BOOTINO) {
1518 ccl -= ntfs_btocn(off);
1519 cn = ccn + ntfs_btocn(off);
1520 off = ntfs_btocnoff(off);
1522 while (left && ccl) {
1523 #if defined(__DragonFly__)
1525 min(ntfs_cntob(ccl) - off,
1528 /* under NetBSD, bread() can read
1529 * maximum one block worth of data */
1531 ntmp->ntm_bps - off);
1533 cl = ntfs_btocl(tocopy + off);
1534 ddprintf(("ntfs_readntvattr_plain: " \
1535 "read: cn: 0x%x cl: %d, " \
1536 "off: %d len: %d, left: %d\n",
1542 error = bread(ntmp->ntm_devvp,
1551 uiomove(bp->b_data + off,
1554 memcpy(data, bp->b_data + off,
1558 data = data + tocopy;
1566 tocopy = min(left, ntfs_cntob(ccl) - off);
1567 ddprintf(("ntfs_readntvattr_plain: "
1568 "hole: ccn: 0x%x ccl: %d, off: %d, " \
1569 " len: %d, left: %d\n",
1570 (u_int32_t) ccn, (u_int32_t) ccl,
1571 (u_int32_t) off, (u_int32_t) tocopy,
1576 size_t remains = tocopy;
1577 for(; remains; remains++)
1578 uiomove("", 1, uio);
1580 bzero(data, tocopy);
1581 data = data + tocopy;
1586 kprintf("ntfs_readntvattr_plain: POSSIBLE RUN ERROR\n");
1590 ddprintf(("ntfs_readnvattr_plain: data is in mft record\n"));
1592 uiomove(vap->va_datap + roff, rsize, uio);
1594 memcpy(rdata, vap->va_datap + roff, rsize);
1602 * This is one of read routines.
1605 ntfs_readattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1606 u_int32_t attrnum, char *attrname, off_t roff,
1607 size_t rsize, void *rdata, size_t * initp,
1612 off_t off = roff, left = rsize, toread;
1613 caddr_t data = rdata;
1614 struct ntvattr *vap;
1618 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1619 ntfs_btocn(off), &vap);
1622 toread = min(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1623 ddprintf(("ntfs_readattr_plain: o: %d, s: %d (%d - %d)\n",
1624 (u_int32_t) off, (u_int32_t) toread,
1625 (u_int32_t) vap->va_vcnstart,
1626 (u_int32_t) vap->va_vcnend));
1627 error = ntfs_readntvattr_plain(ntmp, ip, vap,
1628 off - ntfs_cntob(vap->va_vcnstart),
1629 toread, data, &init, uio);
1631 kprintf("ntfs_readattr_plain: " \
1632 "ntfs_readntvattr_plain failed: o: %d, s: %d\n",
1633 (u_int32_t) off, (u_int32_t) toread);
1634 kprintf("ntfs_readattr_plain: attrib: %d - %d\n",
1635 (u_int32_t) vap->va_vcnstart,
1636 (u_int32_t) vap->va_vcnend);
1637 ntfs_ntvattrrele(vap);
1640 ntfs_ntvattrrele(vap);
1643 data = data + toread;
1651 * This is one of read routines.
1654 ntfs_readattr(struct ntfsmount *ntmp, struct ntnode *ip, u_int32_t attrnum,
1655 char *attrname, off_t roff, size_t rsize, void *rdata,
1659 struct ntvattr *vap;
1662 ddprintf(("ntfs_readattr: reading %"PRId64": 0x%x, from %d size %d bytes\n",
1663 ip->i_number, attrnum, (u_int32_t) roff, (u_int32_t) rsize));
1665 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname, 0, &vap);
1669 if ((roff > vap->va_datalen) ||
1670 (roff + rsize > vap->va_datalen)) {
1671 ddprintf(("ntfs_readattr: offset too big\n"));
1672 ntfs_ntvattrrele(vap);
1675 if (vap->va_compression && vap->va_compressalg) {
1678 off_t off, left = rsize, tocopy;
1679 caddr_t data = rdata;
1682 ddprintf(("ntfs_ntreadattr: compression: %d\n",
1683 vap->va_compressalg));
1685 MALLOC(cup, u_int8_t *, ntfs_cntob(NTFS_COMPUNIT_CL),
1686 M_NTFSDECOMP, M_WAITOK);
1687 MALLOC(uup, u_int8_t *, ntfs_cntob(NTFS_COMPUNIT_CL),
1688 M_NTFSDECOMP, M_WAITOK);
1690 cn = (ntfs_btocn(roff)) & (~(NTFS_COMPUNIT_CL - 1));
1691 off = roff - ntfs_cntob(cn);
1694 error = ntfs_readattr_plain(ntmp, ip, attrnum,
1695 attrname, ntfs_cntob(cn),
1696 ntfs_cntob(NTFS_COMPUNIT_CL),
1701 tocopy = min(left, ntfs_cntob(NTFS_COMPUNIT_CL) - off);
1703 if (init == ntfs_cntob(NTFS_COMPUNIT_CL)) {
1705 uiomove(cup + off, tocopy, uio);
1707 memcpy(data, cup + off, tocopy);
1708 } else if (init == 0) {
1710 size_t remains = tocopy;
1711 for(; remains; remains--)
1712 uiomove("", 1, uio);
1715 bzero(data, tocopy);
1717 error = ntfs_uncompunit(ntmp, uup, cup);
1721 uiomove(uup + off, tocopy, uio);
1723 memcpy(data, uup + off, tocopy);
1727 data = data + tocopy;
1728 off += tocopy - ntfs_cntob(NTFS_COMPUNIT_CL);
1729 cn += NTFS_COMPUNIT_CL;
1732 FREE(uup, M_NTFSDECOMP);
1733 FREE(cup, M_NTFSDECOMP);
1735 error = ntfs_readattr_plain(ntmp, ip, attrnum, attrname,
1736 roff, rsize, rdata, &init, uio);
1737 ntfs_ntvattrrele(vap);
1743 ntfs_parserun(cn_t *cn, cn_t *cl, u_int8_t *run, u_long len, u_long *off)
1749 kprintf("ntfs_parsetun: run == NULL\n");
1754 kprintf("ntfs_parserun: trying to go out of run\n");
1758 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1759 kprintf("ntfs_parserun: " \
1760 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1764 for (i = 0; i < (sz & 0xF); i++)
1765 *cl += (u_int32_t) run[(*off)++] << (i << 3);
1768 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1769 kprintf("ntfs_parserun: " \
1770 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1774 for (i = 0; i < (sz & 0xF); i++)
1775 *cn += (u_int32_t) run[(*off)++] << (i << 3);
1782 * Process fixup routine on given buffer.
1785 ntfs_procfixups(struct ntfsmount *ntmp, u_int32_t magic, caddr_t buf,
1788 struct fixuphdr *fhp = (struct fixuphdr *) buf;
1794 if (fhp->fh_magic != magic) {
1795 kprintf("ntfs_procfixups: magic doesn't match: %08x != %08x\n",
1796 fhp->fh_magic, magic);
1799 if ((fhp->fh_fnum - 1) * ntmp->ntm_bps != len) {
1800 kprintf("ntfs_procfixups: " \
1801 "bad fixups number: %d for %ld bytes block\n",
1802 fhp->fh_fnum, (long)len); /* XXX kprintf kludge */
1805 if (fhp->fh_foff >= ntmp->ntm_spc * ntmp->ntm_mftrecsz * ntmp->ntm_bps) {
1806 kprintf("ntfs_procfixups: invalid offset: %x", fhp->fh_foff);
1809 fxp = (u_int16_t *) (buf + fhp->fh_foff);
1810 cfxp = (u_int16_t *) (buf + ntmp->ntm_bps - 2);
1812 for (i = 1; i < fhp->fh_fnum; i++, fxp++) {
1813 if (*cfxp != fixup) {
1814 kprintf("ntfs_procfixups: fixup %d doesn't match\n", i);
1818 cfxp = (u_int16_t *)(((caddr_t) cfxp) + ntmp->ntm_bps);
1825 ntfs_runtocn(cn_t *cn, struct ntfsmount *ntmp, u_int8_t *run, u_long len,
1835 kprintf("ntfs_runtocn: run: 0x%p, %ld bytes, vcn:%ld\n",
1836 run, len, (u_long) vcn);
1837 kprintf("ntfs_runtocn: run: ");
1838 for (i = 0; i < len; i++)
1839 kprintf("0x%02x ", run[i]);
1844 kprintf("ntfs_runtocn: run == NULL\n");
1848 if (run[off] == 0) {
1849 kprintf("ntfs_runtocn: vcn too big\n");
1853 error = ntfs_parserun(&ccn, &ccl, run, len, &off);
1855 kprintf("ntfs_runtocn: ntfs_parserun failed\n");
1858 } while (ccl <= vcn);
1865 * this initializes toupper table & dependant variables to be ready for
1869 ntfs_toupper_init(void)
1871 ntfs_toupper_tab = NULL;
1872 lockinit(&ntfs_toupper_lock, "ntfs_toupper", 0, 0);
1873 ntfs_toupper_usecount = 0;
1877 * if the ntfs_toupper_tab[] is filled already, just raise use count;
1878 * otherwise read the data from the filesystem we are currently mounting
1881 ntfs_toupper_use(struct mount *mp, struct ntfsmount *ntmp)
1886 /* get exclusive access */
1887 LOCKMGR(&ntfs_toupper_lock, LK_EXCLUSIVE);
1889 /* only read the translation data from a file if it hasn't been
1891 if (ntfs_toupper_tab)
1895 * Read in Unicode lowercase -> uppercase translation file.
1896 * XXX for now, just the first 256 entries are used anyway,
1897 * so don't bother reading more
1899 MALLOC(ntfs_toupper_tab, wchar *, 65536 * sizeof(wchar),
1900 M_NTFSRDATA, M_WAITOK);
1902 if ((error = VFS_VGET(mp, NULL, NTFS_UPCASEINO, &vp)))
1904 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
1905 0, 65536*sizeof(wchar), (char *) ntfs_toupper_tab, NULL);
1909 ntfs_toupper_usecount++;
1910 LOCKMGR(&ntfs_toupper_lock, LK_RELEASE);
1915 * lower the use count and if it reaches zero, free the memory
1916 * tied by toupper table
1919 ntfs_toupper_unuse(void)
1921 /* get exclusive access */
1922 LOCKMGR(&ntfs_toupper_lock, LK_EXCLUSIVE);
1924 ntfs_toupper_usecount--;
1925 if (ntfs_toupper_usecount == 0) {
1926 FREE(ntfs_toupper_tab, M_NTFSRDATA);
1927 ntfs_toupper_tab = NULL;
1930 else if (ntfs_toupper_usecount < 0) {
1931 panic("ntfs_toupper_unuse(): use count negative: %d\n",
1932 ntfs_toupper_usecount);
1936 /* release the lock */
1937 LOCKMGR(&ntfs_toupper_lock, LK_RELEASE);
1941 ntfs_u28_init(struct ntfsmount *ntmp, wchar *u2w, char *cs_local,
1947 if (ntfs_iconv && cs_local) {
1948 ntfs_iconv->open(cs_local, cs_ntfs, &ntmp->ntm_ic_u2l);
1952 MALLOC(u28, char **, 256 * sizeof(char*), M_TEMP, M_WAITOK | M_ZERO);
1954 for (i=0; i<256; i++) {
1955 h = (u2w[i] >> 8) & 0xFF;
1958 if (u28[h] == NULL) {
1959 MALLOC(u28[h], char *, 256 * sizeof(char), M_TEMP, M_WAITOK);
1960 for (j=0; j<256; j++)
1964 u28[h][l] = i & 0xFF;
1967 ntmp->ntm_u28 = u28;
1973 ntfs_u28_uninit(struct ntfsmount *ntmp)
1978 if (ntmp->ntm_u28 == NULL) {
1979 if (ntfs_iconv && ntmp->ntm_ic_u2l) {
1980 ntfs_iconv->close(ntmp->ntm_ic_u2l);
1985 if (ntmp->ntm_u28 == NULL)
1988 u28 = ntmp->ntm_u28;
1990 for (i=0; i<256; i++)
1992 FREE(u28[i], M_TEMP);
2000 ntfs_82u_init(struct ntfsmount *ntmp, char *cs_local, char *cs_ntfs)
2006 if (ntfs_iconv && cs_local) {
2007 ntfs_iconv->open(cs_ntfs, cs_local, &ntmp->ntm_ic_l2u);
2011 MALLOC(_82u, wchar *, 256 * sizeof(wchar), M_TEMP, M_WAITOK);
2013 for (i=0; i<256; i++)
2016 ntmp->ntm_82u = _82u;
2022 ntfs_82u_uninit(struct ntfsmount *ntmp)
2024 if (ntmp->ntm_82u == NULL) {
2025 if (ntfs_iconv && ntmp->ntm_ic_l2u) {
2026 ntfs_iconv->close(ntmp->ntm_ic_l2u);
2031 FREE(ntmp->ntm_82u, M_TEMP);
2036 * maps the Unicode char to 8bit equivalent
2037 * XXX currently only gets lower 8bit from the Unicode char
2038 * and substitutes a '_' for it if the result would be '\0';
2039 * something better has to be definitely though out
2042 ntfs_u28(struct ntfsmount *ntmp, wchar wc)
2044 char *p, *outp, inbuf[3], outbuf[3];
2047 if (ntfs_iconv && ntmp->ntm_ic_u2l) {
2049 inbuf[0] = (char)(wc>>8);
2050 inbuf[1] = (char)wc;
2054 ntfs_iconv->convchr(ntmp->ntm_ic_u2l, (const char **)&p, &ilen,
2057 return ((wchar)(outbuf[0]&0xFF));
2058 } else if (olen == 0) {
2059 return ((wchar)((outbuf[0]<<8) | (outbuf[1]&0xFF)));
2064 p = ntmp->ntm_u28[(wc>>8)&0xFF];
2067 return (p[wc&0xFF]);
2071 ntfs_82u(struct ntfsmount *ntmp,
2075 char *p, *outp, inbuf[3], outbuf[3];
2079 if (ntfs_iconv && ntmp->ntm_ic_l2u) {
2080 ilen = (size_t)*len;
2083 inbuf[0] = (char)(wc>>8);
2084 inbuf[1] = (char)wc;
2088 ntfs_iconv->convchr(ntmp->ntm_ic_l2u, (const char **)&p, &ilen,
2091 uc = (wchar)((outbuf[0]<<8) | (outbuf[1]&0xFF));
2096 if (ntmp->ntm_82u != NULL)
2097 return (ntmp->ntm_82u[wc&0xFF]);