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 $
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/systm.h>
35 #include <sys/namei.h>
36 #include <sys/kernel.h>
37 #include <sys/vnode.h>
38 #include <sys/mount.h>
41 #include <sys/malloc.h>
43 #include <sys/spinlock.h>
44 #include <sys/iconv.h>
46 #include <machine/inttypes.h>
49 #include <sys/spinlock2.h>
51 #if defined(__NetBSD__)
52 #include <miscfs/specfs/specdev.h>
55 /* #define NTFS_DEBUG 1 */
57 #include "ntfsmount.h"
58 #include "ntfs_inode.h"
59 #include "ntfs_vfsops.h"
60 #include "ntfs_subr.h"
61 #include "ntfs_compr.h"
62 #include "ntfs_ihash.h"
64 #if defined(__DragonFly__)
65 MALLOC_DEFINE(M_NTFSNTVATTR, "NTFS vattr", "NTFS file attribute information");
66 MALLOC_DEFINE(M_NTFSRDATA, "NTFS res data", "NTFS resident data");
67 MALLOC_DEFINE(M_NTFSRUN, "NTFS vrun", "NTFS vrun storage");
68 MALLOC_DEFINE(M_NTFSDECOMP, "NTFS decomp", "NTFS decompression temporary");
71 static int ntfs_ntlookupattr (struct ntfsmount *, const char *, int, int *, char **);
72 static int ntfs_findvattr (struct ntfsmount *, struct ntnode *, struct ntvattr **, struct ntvattr **, u_int32_t, const char *, size_t, cn_t);
73 static int ntfs_uastricmp (struct ntfsmount *, const wchar *, size_t, const char *, size_t);
74 static int ntfs_uastrcmp (struct ntfsmount *, const wchar *, size_t, const char *, size_t);
76 /* table for mapping Unicode chars into uppercase; it's filled upon first
77 * ntfs mount, freed upon last ntfs umount */
78 static wchar *ntfs_toupper_tab;
79 #define NTFS_TOUPPER(ch) (ntfs_toupper_tab[(ch)])
80 static struct lock ntfs_toupper_lock;
81 static signed int ntfs_toupper_usecount;
82 extern struct iconv_functions *ntfs_iconv;
84 /* support macro for ntfs_ntvattrget() */
85 #define NTFS_AALPCMP(aalp,type,name,namelen) ( \
86 (aalp->al_type == type) && (aalp->al_namelen == namelen) && \
87 !NTFS_UASTRCMP(aalp->al_name,aalp->al_namelen,name,namelen) )
93 ntfs_ntvattrrele(struct ntvattr *vap)
95 dprintf(("ntfs_ntvattrrele: ino: %"PRId64", type: 0x%x\n",
96 vap->va_ip->i_number, vap->va_type));
98 ntfs_ntrele(vap->va_ip);
104 * find the attribute in the ntnode
107 ntfs_findvattr(struct ntfsmount *ntmp, struct ntnode *ip,
108 struct ntvattr **lvapp, struct ntvattr **vapp, u_int32_t type,
109 const char *name, size_t namelen, cn_t vcn)
114 if((ip->i_flag & IN_LOADED) == 0) {
115 dprintf(("ntfs_findvattr: node not loaded, ino: %"PRId64"\n",
117 error = ntfs_loadntnode(ntmp,ip);
119 kprintf("ntfs_findvattr: FAILED TO LOAD INO: %"PRId64"\n",
127 for (vap = ip->i_valist.lh_first; vap; vap = vap->va_list.le_next) {
128 ddprintf(("ntfs_findvattr: type: 0x%x, vcn: %d - %d\n", \
129 vap->va_type, (u_int32_t) vap->va_vcnstart, \
130 (u_int32_t) vap->va_vcnend));
131 if ((vap->va_type == type) &&
132 (vap->va_vcnstart <= vcn) && (vap->va_vcnend >= vcn) &&
133 (vap->va_namelen == namelen) &&
134 (strncmp(name, vap->va_name, namelen) == 0)) {
136 ntfs_ntref(vap->va_ip);
139 if (vap->va_type == NTFS_A_ATTRLIST)
147 * Search attribute specifed in ntnode (load ntnode if nessecary).
148 * If not found but ATTR_A_ATTRLIST present, read it in and search throught.
149 * VOP_VGET node needed, and lookup througth it's ntnode (load if nessesary).
151 * ntnode should be locked
154 ntfs_ntvattrget(struct ntfsmount *ntmp, struct ntnode *ip, u_int32_t type,
155 const char *name, cn_t vcn, struct ntvattr **vapp)
157 struct ntvattr *lvap = NULL;
158 struct attr_attrlist *aalp;
159 struct attr_attrlist *nextaalp;
161 struct ntnode *newip;
169 dprintf(("ntfs_ntvattrget: " \
170 "ino: %"PRId64", type: 0x%x, name: %s, vcn: %d\n", \
171 ip->i_number, type, name, (u_int32_t) vcn));
172 namelen = strlen(name);
174 dprintf(("ntfs_ntvattrget: " \
175 "ino: %"PRId64", type: 0x%x, vcn: %d\n", \
176 ip->i_number, type, (u_int32_t) vcn));
181 error = ntfs_findvattr(ntmp, ip, &lvap, vapp, type, name, namelen, vcn);
186 dprintf(("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: " \
187 "ino: %"PRId64", type: 0x%x, name: %s, vcn: %d\n", \
188 ip->i_number, type, name, (u_int32_t) vcn));
191 /* Scan $ATTRIBUTE_LIST for requested attribute */
192 len = lvap->va_datalen;
193 MALLOC(alpool, caddr_t, len, M_TEMP, M_WAITOK);
194 error = ntfs_readntvattr_plain(ntmp, ip, lvap, 0, len, alpool, &len,
199 aalp = (struct attr_attrlist *) alpool;
202 for(; len > 0; aalp = nextaalp) {
203 dprintf(("ntfs_ntvattrget: " \
204 "attrlist: ino: %d, attr: 0x%x, vcn: %d\n", \
205 aalp->al_inumber, aalp->al_type, \
206 (u_int32_t) aalp->al_vcnstart));
208 if (len > aalp->reclen) {
209 nextaalp = NTFS_NEXTREC(aalp, struct attr_attrlist *);
215 if (!NTFS_AALPCMP(aalp, type, name, namelen) ||
216 (nextaalp && (nextaalp->al_vcnstart <= vcn) &&
217 NTFS_AALPCMP(nextaalp, type, name, namelen)))
220 dprintf(("ntfs_ntvattrget: attribute in ino: %d\n",
223 /* this is not a main record, so we can't use just plain
225 error = ntfs_vgetex(ntmp->ntm_mountp, aalp->al_inumber,
226 NTFS_A_DATA, NULL, LK_EXCLUSIVE,
227 VG_EXT, curthread, &newvp);
229 kprintf("ntfs_ntvattrget: CAN'T VGET INO: %d\n",
233 newip = VTONT(newvp);
234 /* XXX have to lock ntnode */
235 error = ntfs_findvattr(ntmp, newip, &lvap, vapp,
236 type, name, namelen, vcn);
240 kprintf("ntfs_ntvattrget: ATTRLIST ERROR.\n");
245 dprintf(("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: " \
246 "ino: %"PRId64", type: 0x%x, name: %.*s, vcn: %d\n", \
247 ip->i_number, type, (int) namelen, name, (u_int32_t) vcn));
249 FREE(alpool, M_TEMP);
254 * Read ntnode from disk, make ntvattr list.
256 * ntnode should be locked
259 ntfs_loadntnode(struct ntfsmount *ntmp, struct ntnode *ip)
261 struct filerec *mfrp;
265 struct ntvattr *nvap;
267 dprintf(("ntfs_loadntnode: loading ino: %"PRId64"\n",ip->i_number));
269 MALLOC(mfrp, struct filerec *, ntfs_bntob(ntmp->ntm_bpmftrec),
272 if (ip->i_number < NTFS_SYSNODESNUM) {
275 dprintf(("ntfs_loadntnode: read system node\n"));
277 bn = ntfs_cntobn(ntmp->ntm_mftcn) +
278 ntmp->ntm_bpmftrec * ip->i_number;
280 error = bread(ntmp->ntm_devvp,
281 ntfs_bntodoff(bn), ntfs_bntob(ntmp->ntm_bpmftrec), &bp);
283 kprintf("ntfs_loadntnode: BREAD FAILED\n");
287 memcpy(mfrp, bp->b_data, ntfs_bntob(ntmp->ntm_bpmftrec));
292 vp = ntmp->ntm_sysvn[NTFS_MFTINO];
293 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
294 ip->i_number * ntfs_bntob(ntmp->ntm_bpmftrec),
295 ntfs_bntob(ntmp->ntm_bpmftrec), mfrp, NULL);
297 kprintf("ntfs_loadntnode: ntfs_readattr failed\n");
302 /* Check if magic and fixups are correct */
303 error = ntfs_procfixups(ntmp, NTFS_FILEMAGIC, (caddr_t)mfrp,
304 ntfs_bntob(ntmp->ntm_bpmftrec));
306 kprintf("ntfs_loadntnode: BAD MFT RECORD %"PRId64"\n",
311 dprintf(("ntfs_loadntnode: load attrs for ino: %"PRId64"\n",ip->i_number));
312 off = mfrp->fr_attroff;
313 ap = (struct attr *) ((caddr_t)mfrp + off);
315 LIST_INIT(&ip->i_valist);
317 while (ap->a_hdr.a_type != -1) {
318 error = ntfs_attrtontvattr(ntmp, &nvap, ap);
323 LIST_INSERT_HEAD(&ip->i_valist, nvap, va_list);
325 off += ap->a_hdr.reclen;
326 ap = (struct attr *) ((caddr_t)mfrp + off);
329 kprintf("ntfs_loadntnode: failed to load attr ino: %"PRId64"\n",
334 ip->i_mainrec = mfrp->fr_mainrec;
335 ip->i_nlink = mfrp->fr_nlink;
336 ip->i_frflag = mfrp->fr_flags;
338 ip->i_flag |= IN_LOADED;
346 * Routine locks ntnode and increase usecount, just opposite of
350 ntfs_ntget(struct ntnode *ip)
352 dprintf(("ntfs_ntget: get ntnode %"PRId64": %p, usecount: %d\n",
353 ip->i_number, ip, ip->i_usecount));
355 ip->i_usecount++; /* ZZZ */
356 LOCKMGR(&ip->i_lock, LK_EXCLUSIVE);
362 * Routine search ntnode in hash, if found: lock, inc usecount and return.
363 * If not in hash allocate structure for ntnode, prefill it, lock,
364 * inc count and return.
366 * ntnode returned locked
369 ntfs_ntlookup(struct ntfsmount *ntmp, ino_t ino, struct ntnode **ipp)
373 dprintf(("ntfs_ntlookup: looking for ntnode %d\n", ino));
376 if ((ip = ntfs_nthashlookup(ntmp->ntm_dev, ino)) != NULL) {
378 dprintf(("ntfs_ntlookup: ntnode %d: %p, usecount: %d\n",
379 ino, ip, ip->i_usecount));
383 } while (LOCKMGR(&ntfs_hashlock, LK_EXCLUSIVE | LK_SLEEPFAIL));
385 MALLOC(ip, struct ntnode *, sizeof(struct ntnode),
386 M_NTFSNTNODE, M_WAITOK | M_ZERO);
387 ddprintf(("ntfs_ntlookup: allocating ntnode: %d: %p\n", ino, ip));
389 /* Generic initialization */
390 ip->i_devvp = ntmp->ntm_devvp;
391 ip->i_dev = ntmp->ntm_dev;
395 LIST_INIT(&ip->i_fnlist);
398 /* init lock and lock the newborn ntnode */
399 lockinit(&ip->i_lock, "ntnode", 0, LK_EXCLUSIVE);
400 spin_init(&ip->i_interlock);
405 LOCKMGR(&ntfs_hashlock, LK_RELEASE);
409 dprintf(("ntfs_ntlookup: ntnode %d: %p, usecount: %d\n",
410 ino, ip, ip->i_usecount));
416 * Decrement usecount of ntnode and unlock it, if usecount reach zero,
419 * ntnode should be locked on entry, and unlocked on return.
422 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 spin_lock(&ip->i_interlock);
433 if (ip->i_usecount < 0) {
434 spin_unlock(&ip->i_interlock);
435 panic("ntfs_ntput: ino: %"PRId64" usecount: %d \n",
436 ip->i_number,ip->i_usecount);
440 if (ip->i_usecount > 0) {
441 spin_unlock(&ip->i_interlock);
442 LOCKMGR(&ip->i_lock, LK_RELEASE);
446 dprintf(("ntfs_ntput: deallocating ntnode: %"PRId64"\n", ip->i_number));
448 if (ip->i_fnlist.lh_first) {
449 spin_unlock(&ip->i_interlock);
450 panic("ntfs_ntput: ntnode has fnodes\n");
454 * XXX this is a bit iffy because we are making high level calls
455 * while holding a spinlock.
459 while ((vap = LIST_FIRST(&ip->i_valist)) != NULL) {
460 LIST_REMOVE(vap,va_list);
461 ntfs_freentvattr(vap);
463 spin_unlock(&ip->i_interlock);
465 FREE(ip, M_NTFSNTNODE);
469 * increment usecount of ntnode
472 ntfs_ntref(struct ntnode *ip)
476 dprintf(("ntfs_ntref: ino %"PRId64", usecount: %d\n",
477 ip->i_number, ip->i_usecount));
482 * Decrement usecount of ntnode.
485 ntfs_ntrele(struct ntnode *ip)
487 dprintf(("ntfs_ntrele: rele ntnode %"PRId64": %p, usecount: %d\n",
488 ip->i_number, ip, ip->i_usecount));
490 spin_lock(&ip->i_interlock);
493 if (ip->i_usecount < 0) {
494 spin_unlock(&ip->i_interlock);
495 panic("ntfs_ntrele: ino: %"PRId64" usecount: %d \n",
496 ip->i_number,ip->i_usecount);
498 spin_unlock(&ip->i_interlock);
502 * Deallocate all memory allocated for ntvattr
505 ntfs_freentvattr(struct ntvattr *vap)
507 if (vap->va_flag & NTFS_AF_INRUN) {
509 FREE(vap->va_vruncn, M_NTFSRUN);
511 FREE(vap->va_vruncl, M_NTFSRUN);
514 FREE(vap->va_datap, M_NTFSRDATA);
516 FREE(vap, M_NTFSNTVATTR);
520 * Convert disk image of attribute into ntvattr structure,
521 * runs are expanded also.
524 ntfs_attrtontvattr(struct ntfsmount *ntmp, struct ntvattr **rvapp,
533 MALLOC(vap, struct ntvattr *, sizeof(struct ntvattr),
534 M_NTFSNTVATTR, M_WAITOK | M_ZERO);
536 vap->va_flag = rap->a_hdr.a_flag;
537 vap->va_type = rap->a_hdr.a_type;
538 vap->va_compression = rap->a_hdr.a_compression;
539 vap->va_index = rap->a_hdr.a_index;
541 ddprintf(("type: 0x%x, index: %d", vap->va_type, vap->va_index));
543 vap->va_namelen = rap->a_hdr.a_namelen;
544 if (rap->a_hdr.a_namelen) {
545 wchar *unp = (wchar *) ((caddr_t) rap + rap->a_hdr.a_nameoff);
546 ddprintf((", name:["));
547 for (i = 0; i < vap->va_namelen; i++) {
548 vap->va_name[i] = unp[i];
549 ddprintf(("%c", vap->va_name[i]));
553 if (vap->va_flag & NTFS_AF_INRUN) {
554 ddprintf((", nonres."));
555 vap->va_datalen = rap->a_nr.a_datalen;
556 vap->va_allocated = rap->a_nr.a_allocated;
557 vap->va_vcnstart = rap->a_nr.a_vcnstart;
558 vap->va_vcnend = rap->a_nr.a_vcnend;
559 vap->va_compressalg = rap->a_nr.a_compressalg;
560 error = ntfs_runtovrun(&(vap->va_vruncn), &(vap->va_vruncl),
562 (caddr_t) rap + rap->a_nr.a_dataoff);
564 vap->va_compressalg = 0;
565 ddprintf((", res."));
566 vap->va_datalen = rap->a_r.a_datalen;
567 vap->va_allocated = rap->a_r.a_datalen;
568 vap->va_vcnstart = 0;
569 vap->va_vcnend = ntfs_btocn(vap->va_allocated);
570 MALLOC(vap->va_datap, caddr_t, vap->va_datalen,
571 M_NTFSRDATA, M_WAITOK);
572 memcpy(vap->va_datap, (caddr_t) rap + rap->a_r.a_dataoff,
575 ddprintf((", len: %d", vap->va_datalen));
578 FREE(vap, M_NTFSNTVATTR);
588 * Expand run into more utilizable and more memory eating format.
591 ntfs_runtovrun(cn_t **rcnp, cn_t **rclp, u_long *rcntp, u_int8_t *run)
605 off += (run[off] & 0xF) + ((run[off] >> 4) & 0xF) + 1;
608 MALLOC(cn, cn_t *, cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
609 MALLOC(cl, cn_t *, cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
619 for (i = 0; i < (sz & 0xF); i++)
620 cl[cnt] += (u_int32_t) run[off++] << (i << 3);
623 if (run[off + sz - 1] & 0x80) {
624 tmp = ((u_int64_t) - 1) << (sz << 3);
625 for (i = 0; i < sz; i++)
626 tmp |= (u_int64_t) run[off++] << (i << 3);
629 for (i = 0; i < sz; i++)
630 tmp |= (u_int64_t) run[off++] << (i << 3);
633 prev = cn[cnt] = prev + tmp;
646 * Compare unicode and ascii string case insens.
649 ntfs_uastricmp(struct ntfsmount *ntmp, const wchar *ustr, size_t ustrlen,
650 const char *astr, size_t astrlen)
653 size_t i, j, mbstrlen = astrlen;
657 if (ntmp->ntm_ic_l2u) {
658 for (i = 0, j = 0; i < ustrlen && j < astrlen; i++, j++) {
659 if (j < astrlen -1) {
660 wc = (wchar)astr[j]<<8 | (astr[j+1]&0xFF);
663 wc = (wchar)astr[j]<<8 & 0xFF00;
666 res = ((int) NTFS_TOUPPER(ustr[i])) -
667 ((int)NTFS_TOUPPER(NTFS_82U(wc, &len)));
676 * We use NTFS_82U(NTFS_U28(c)) to get rid of unicode
677 * symbols not covered by translation table
679 for (i = 0; i < ustrlen && i < astrlen; i++) {
680 res = ((int) NTFS_TOUPPER(NTFS_82U(NTFS_U28(ustr[i]), &len))) -
681 ((int)NTFS_TOUPPER(NTFS_82U((wchar)astr[i], &len)));
686 return (ustrlen - mbstrlen);
690 * Compare unicode and ascii string case sens.
693 ntfs_uastrcmp(struct ntfsmount *ntmp, const wchar *ustr, size_t ustrlen,
694 const char *astr, size_t astrlen)
697 size_t i, j, mbstrlen = astrlen;
701 for (i = 0, j = 0; (i < ustrlen) && (j < astrlen); i++, j++) {
703 wc = NTFS_U28(ustr[i]);
706 if (u != '\0' && j < astrlen -1) {
707 res = (int) (u - astr[j++]);
710 res = (res<<8) + (int) (l - astr[j]);
714 return (ustrlen - mbstrlen);
718 * Search fnode in ntnode, if not found allocate and preinitialize.
720 * ntnode should be locked on entry.
723 ntfs_fget(struct ntfsmount *ntmp, struct ntnode *ip, int attrtype,
724 char *attrname, struct fnode **fpp)
728 dprintf(("ntfs_fget: ino: %"PRId64", attrtype: 0x%x, attrname: %s\n",
729 ip->i_number,attrtype, attrname?attrname:""));
731 for (fp = ip->i_fnlist.lh_first; fp != NULL; fp = fp->f_fnlist.le_next){
732 dprintf(("ntfs_fget: fnode: attrtype: %d, attrname: %s\n",
733 fp->f_attrtype, fp->f_attrname?fp->f_attrname:""));
735 if ((attrtype == fp->f_attrtype) &&
736 ((!attrname && !fp->f_attrname) ||
737 (attrname && fp->f_attrname &&
738 !strcmp(attrname,fp->f_attrname)))){
739 dprintf(("ntfs_fget: found existed: %p\n",fp));
747 MALLOC(fp, struct fnode *, sizeof(struct fnode), M_NTFSFNODE,
749 dprintf(("ntfs_fget: allocating fnode: %p\n",fp));
753 fp->f_flag |= FN_AATTRNAME;
754 MALLOC(fp->f_attrname, char *, strlen(attrname)+1, M_TEMP, M_WAITOK);
755 strcpy(fp->f_attrname, attrname);
757 fp->f_attrname = NULL;
758 fp->f_attrtype = attrtype;
762 LIST_INSERT_HEAD(&ip->i_fnlist, fp, f_fnlist);
770 * Deallocate fnode, remove it from ntnode's fnode list.
772 * ntnode should be locked.
775 ntfs_frele(struct fnode *fp)
777 struct ntnode *ip = FTONT(fp);
779 dprintf(("ntfs_frele: fnode: %p for %"PRId64": %p\n", fp, ip->i_number, ip));
781 dprintf(("ntfs_frele: deallocating fnode\n"));
782 LIST_REMOVE(fp,f_fnlist);
783 if (fp->f_flag & FN_AATTRNAME)
784 FREE(fp->f_attrname, M_TEMP);
786 FREE(fp->f_dirblbuf, M_NTFSDIR);
787 FREE(fp, M_NTFSFNODE);
792 * Lookup attribute name in format: [[:$ATTR_TYPE]:$ATTR_NAME],
793 * $ATTR_TYPE is searched in attrdefs read from $AttrDefs.
794 * If $ATTR_TYPE nott specifed, ATTR_A_DATA assumed.
797 ntfs_ntlookupattr(struct ntfsmount *ntmp, const char *name, int namelen,
798 int *attrtype, char **attrname)
802 struct ntvattrdef *adp;
807 if (name[0] == '$') {
809 for (syslen = 0; syslen < namelen; syslen++) {
810 if(sys[syslen] == ':') {
820 for (i = 0; i < ntmp->ntm_adnum; i++, adp++){
821 if (syslen != adp->ad_namelen ||
822 strncmp(sys, adp->ad_name, syslen) != 0)
825 *attrtype = adp->ad_type;
830 *attrtype = NTFS_A_DATA;
834 MALLOC((*attrname), char *, namelen, M_TEMP, M_WAITOK);
835 memcpy((*attrname), name, namelen);
836 (*attrname)[namelen] = '\0';
843 * Lookup specifed node for filename, matching cnp,
844 * return fnode filled.
847 ntfs_ntlookupfile(struct ntfsmount *ntmp, struct vnode *vp,
848 struct componentname *cnp, struct vnode **vpp)
850 struct fnode *fp = VTOF(vp);
851 struct ntnode *ip = FTONT(fp);
852 struct ntvattr *vap; /* Root attribute */
853 cn_t cn; /* VCN in current attribute */
854 caddr_t rdbuf; /* Buffer to read directory's blocks */
856 u_int32_t rdsize; /* Length of data to read from current block */
857 struct attr_indexentry *iep;
858 int error, res, anamelen, fnamelen;
859 const char *fname,*aname;
861 int attrtype = NTFS_A_DATA;
862 char *attrname = NULL;
867 error = ntfs_ntget(ip);
871 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
872 if (error || (vap->va_flag & NTFS_AF_INRUN))
875 blsize = vap->va_a_iroot->ir_size;
876 rdsize = vap->va_datalen;
879 * Divide file name into: foofilefoofilefoofile[:attrspec]
880 * Store like this: fname:fnamelen [aname:anamelen]
882 fname = cnp->cn_nameptr;
885 for (fnamelen = 0; fnamelen < cnp->cn_namelen; fnamelen++)
886 if(fname[fnamelen] == ':') {
887 aname = fname + fnamelen + 1;
888 anamelen = cnp->cn_namelen - fnamelen - 1;
889 dprintf(("ntfs_ntlookupfile: %s (%d), attr: %s (%d)\n",
890 fname, fnamelen, aname, anamelen));
894 dprintf(("ntfs_ntlookupfile: blksz: %d, rdsz: %d\n", blsize, rdsize));
896 MALLOC(rdbuf, caddr_t, blsize, M_TEMP, M_WAITOK);
898 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXROOT, "$I30",
899 0, rdsize, rdbuf, NULL);
903 aoff = sizeof(struct attr_indexroot);
906 iep = (struct attr_indexentry *) (rdbuf + aoff);
908 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
910 iep = (struct attr_indexentry *) (rdbuf + aoff))
912 ddprintf(("scan: %d, %d\n",
913 (u_int32_t) iep->ie_number,
914 (u_int32_t) iep->ie_fnametype));
916 /* check the name - the case-insensitible check
917 * has to come first, to break from this for loop
918 * if needed, so we can dive correctly */
919 res = NTFS_UASTRICMP(iep->ie_fname, iep->ie_fnamelen,
922 if (res < 0) continue;
924 if (iep->ie_fnametype == 0 ||
925 !(ntmp->ntm_flag & NTFS_MFLAG_CASEINS))
927 res = NTFS_UASTRCMP(iep->ie_fname,
928 iep->ie_fnamelen, fname, fnamelen);
929 if (res != 0) continue;
933 error = ntfs_ntlookupattr(ntmp,
935 &attrtype, &attrname);
940 /* Check if we've found ourself */
941 if ((iep->ie_number == ip->i_number) &&
942 (attrtype == fp->f_attrtype) &&
943 ((!attrname && !fp->f_attrname) ||
944 (attrname && fp->f_attrname &&
945 !strcmp(attrname, fp->f_attrname))))
953 /* vget node, but don't load it */
954 error = ntfs_vgetex(ntmp->ntm_mountp,
955 iep->ie_number, attrtype, attrname,
956 LK_EXCLUSIVE, VG_DONTLOADIN | VG_DONTVALIDFN,
959 /* free the buffer returned by ntfs_ntlookupattr() */
961 FREE(attrname, M_TEMP);
970 if (nfp->f_flag & FN_VALID) {
975 nfp->f_fflag = iep->ie_fflag;
976 nfp->f_pnumber = iep->ie_fpnumber;
977 nfp->f_times = iep->ie_ftimes;
979 if((nfp->f_fflag & NTFS_FFLAG_DIR) &&
980 (nfp->f_attrtype == NTFS_A_DATA) &&
981 (nfp->f_attrname == NULL))
986 nvp->v_type = f_type;
988 if ((nfp->f_attrtype == NTFS_A_DATA) &&
989 (nfp->f_attrname == NULL))
991 /* Opening default attribute */
992 nfp->f_size = iep->ie_fsize;
993 nfp->f_allocated = iep->ie_fallocated;
994 nfp->f_flag |= FN_PRELOADED;
996 error = ntfs_filesize(ntmp, nfp,
997 &nfp->f_size, &nfp->f_allocated);
1003 nfp->f_flag &= ~FN_VALID;
1006 * Normal files use the buffer cache
1008 if (nvp->v_type == VREG)
1009 vinitvmio(nvp, nfp->f_size, PAGE_SIZE, -1);
1014 /* Dive if possible */
1015 if (iep->ie_flag & NTFS_IEFLAG_SUBNODE) {
1016 dprintf(("ntfs_ntlookupfile: diving\n"));
1018 cn = *(cn_t *) (rdbuf + aoff +
1019 iep->reclen - sizeof(cn_t));
1022 error = ntfs_readattr(ntmp, ip, NTFS_A_INDX, "$I30",
1023 ntfs_cntob(cn), rdsize, rdbuf, NULL);
1027 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1032 aoff = (((struct attr_indexalloc *) rdbuf)->ia_hdrsize +
1035 dprintf(("ntfs_ntlookupfile: nowhere to dive :-(\n"));
1041 dprintf(("finish\n"));
1044 if (attrname) FREE(attrname, M_TEMP);
1045 ntfs_ntvattrrele(vap);
1047 FREE(rdbuf, M_TEMP);
1052 * Check if name type is permitted to show.
1055 ntfs_isnamepermitted(struct ntfsmount *ntmp, struct attr_indexentry *iep)
1057 if (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)
1060 switch (iep->ie_fnametype) {
1062 ddprintf(("ntfs_isnamepermitted: skiped DOS name\n"));
1064 case 0: case 1: case 3:
1067 kprintf("ntfs_isnamepermitted: " \
1068 "WARNING! Unknown file name type: %d\n",
1076 * Read ntfs dir like stream of attr_indexentry, not like btree of them.
1077 * This is done by scaning $BITMAP:$I30 for busy clusters and reading them.
1078 * Ofcouse $INDEX_ROOT:$I30 is read before. Last read values are stored in
1079 * fnode, so we can skip toward record number num almost immediatly.
1080 * Anyway this is rather slow routine. The problem is that we don't know
1081 * how many records are there in $INDEX_ALLOCATION:$I30 block.
1084 ntfs_ntreaddir(struct ntfsmount *ntmp, struct fnode *fp,
1085 u_int32_t num, struct attr_indexentry **riepp)
1087 struct ntnode *ip = FTONT(fp);
1088 struct ntvattr *vap = NULL; /* IndexRoot attribute */
1089 struct ntvattr *bmvap = NULL; /* BitMap attribute */
1090 struct ntvattr *iavap = NULL; /* IndexAllocation attribute */
1091 caddr_t rdbuf; /* Buffer to read directory's blocks */
1092 u_char *bmp = NULL; /* Bitmap */
1093 u_int32_t blsize; /* Index allocation size (2048) */
1094 u_int32_t rdsize; /* Length of data to read */
1095 u_int32_t attrnum; /* Current attribute type */
1096 u_int32_t cpbl = 1; /* Clusters per directory block */
1098 struct attr_indexentry *iep;
1100 u_int32_t aoff, cnum;
1102 dprintf(("ntfs_ntreaddir: read ino: %"PRId64", num: %d\n", ip->i_number, num));
1103 error = ntfs_ntget(ip);
1107 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
1111 if (fp->f_dirblbuf == NULL) {
1112 fp->f_dirblsz = vap->va_a_iroot->ir_size;
1113 MALLOC(fp->f_dirblbuf, caddr_t,
1114 max(vap->va_datalen,fp->f_dirblsz), M_NTFSDIR, M_WAITOK);
1117 blsize = fp->f_dirblsz;
1118 rdbuf = fp->f_dirblbuf;
1120 dprintf(("ntfs_ntreaddir: rdbuf: 0x%p, blsize: %d\n", rdbuf, blsize));
1122 if (vap->va_a_iroot->ir_flag & NTFS_IRFLAG_INDXALLOC) {
1123 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXBITMAP, "$I30",
1129 MALLOC(bmp, u_char *, bmvap->va_datalen, M_TEMP, M_WAITOK);
1130 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXBITMAP, "$I30", 0,
1131 bmvap->va_datalen, bmp, NULL);
1135 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDX, "$I30",
1141 cpbl = ntfs_btocn(blsize + ntfs_cntob(1) - 1);
1142 dprintf(("ntfs_ntreaddir: indexalloc: %d, cpbl: %d\n",
1143 iavap->va_datalen, cpbl));
1145 dprintf(("ntfs_ntreadidir: w/o BitMap and IndexAllocation\n"));
1146 iavap = bmvap = NULL;
1150 /* Try use previous values */
1151 if ((fp->f_lastdnum < num) && (fp->f_lastdnum != 0)) {
1152 attrnum = fp->f_lastdattr;
1153 aoff = fp->f_lastdoff;
1154 blnum = fp->f_lastdblnum;
1155 cnum = fp->f_lastdnum;
1157 attrnum = NTFS_A_INDXROOT;
1158 aoff = sizeof(struct attr_indexroot);
1164 dprintf(("ntfs_ntreaddir: scan: 0x%x, %d, %d, %d, %d\n",
1165 attrnum, (u_int32_t) blnum, cnum, num, aoff));
1166 rdsize = (attrnum == NTFS_A_INDXROOT) ? vap->va_datalen : blsize;
1167 error = ntfs_readattr(ntmp, ip, attrnum, "$I30",
1168 ntfs_cntob(blnum * cpbl), rdsize, rdbuf, NULL);
1172 if (attrnum == NTFS_A_INDX) {
1173 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1179 aoff = (attrnum == NTFS_A_INDX) ?
1180 (0x18 + ((struct attr_indexalloc *) rdbuf)->ia_hdrsize) :
1181 sizeof(struct attr_indexroot);
1183 iep = (struct attr_indexentry *) (rdbuf + aoff);
1184 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
1185 aoff += iep->reclen,
1186 iep = (struct attr_indexentry *) (rdbuf + aoff))
1188 if (!ntfs_isnamepermitted(ntmp, iep)) continue;
1191 fp->f_lastdnum = cnum;
1192 fp->f_lastdoff = aoff;
1193 fp->f_lastdblnum = blnum;
1194 fp->f_lastdattr = attrnum;
1205 if (attrnum == NTFS_A_INDXROOT)
1210 while (ntfs_cntob(blnum * cpbl) < iavap->va_datalen) {
1211 if (bmp[blnum >> 3] & (1 << (blnum & 3)))
1216 attrnum = NTFS_A_INDX;
1218 if (ntfs_cntob(blnum * cpbl) >= iavap->va_datalen)
1220 dprintf(("ntfs_ntreaddir: blnum: %d\n", (u_int32_t) blnum));
1229 ntfs_ntvattrrele(vap);
1231 ntfs_ntvattrrele(bmvap);
1233 ntfs_ntvattrrele(iavap);
1241 * Convert NTFS times that are in 100 ns units and begins from
1242 * 1601 Jan 1 into unix times.
1245 ntfs_nttimetounix(u_int64_t nt)
1249 /* WindowNT times are in 100 ns and from 1601 Jan 1 */
1250 t.tv_nsec = (nt % (1000 * 1000 * 10)) * 100;
1251 t.tv_sec = nt / (1000 * 1000 * 10) -
1252 369LL * 365LL * 24LL * 60LL * 60LL -
1253 89LL * 1LL * 24LL * 60LL * 60LL;
1258 * Get file times from NTFS_A_NAME attribute.
1261 ntfs_times(struct ntfsmount *ntmp, struct ntnode *ip, ntfs_times_t *tm)
1263 struct ntvattr *vap;
1266 dprintf(("ntfs_times: ino: %"PRId64"...\n", ip->i_number));
1268 error = ntfs_ntget(ip);
1272 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_NAME, NULL, 0, &vap);
1277 *tm = vap->va_a_name->n_times;
1278 ntfs_ntvattrrele(vap);
1285 * Get file sizes from corresponding attribute.
1287 * ntnode under fnode should be locked.
1290 ntfs_filesize(struct ntfsmount *ntmp, struct fnode *fp, u_int64_t *size,
1293 struct ntvattr *vap;
1294 struct ntnode *ip = FTONT(fp);
1298 dprintf(("ntfs_filesize: ino: %"PRId64"\n", ip->i_number));
1300 error = ntfs_ntvattrget(ntmp, ip,
1301 fp->f_attrtype, fp->f_attrname, 0, &vap);
1305 bn = vap->va_allocated;
1306 sz = vap->va_datalen;
1308 dprintf(("ntfs_filesize: %d bytes (%d bytes allocated)\n",
1309 (u_int32_t) sz, (u_int32_t) bn));
1316 ntfs_ntvattrrele(vap);
1322 * This is one of write routine.
1325 ntfs_writeattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1326 u_int32_t attrnum, char *attrname, off_t roff,
1327 size_t rsize, void *rdata, size_t *initp,
1332 off_t off = roff, left = rsize, towrite;
1333 caddr_t data = rdata;
1334 struct ntvattr *vap;
1338 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1339 ntfs_btocn(off), &vap);
1342 towrite = min(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1343 ddprintf(("ntfs_writeattr_plain: o: %d, s: %d (%d - %d)\n",
1344 (u_int32_t) off, (u_int32_t) towrite,
1345 (u_int32_t) vap->va_vcnstart,
1346 (u_int32_t) vap->va_vcnend));
1347 error = ntfs_writentvattr_plain(ntmp, ip, vap,
1348 off - ntfs_cntob(vap->va_vcnstart),
1349 towrite, data, &init, uio);
1351 kprintf("ntfs_writeattr_plain: " \
1352 "ntfs_writentvattr_plain failed: o: %d, s: %d\n",
1353 (u_int32_t) off, (u_int32_t) towrite);
1354 kprintf("ntfs_writeattr_plain: attrib: %d - %d\n",
1355 (u_int32_t) vap->va_vcnstart,
1356 (u_int32_t) vap->va_vcnend);
1357 ntfs_ntvattrrele(vap);
1360 ntfs_ntvattrrele(vap);
1363 data = data + towrite;
1371 * This is one of write routine.
1373 * ntnode should be locked.
1376 ntfs_writentvattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1377 struct ntvattr *vap, off_t roff, size_t rsize,
1378 void *rdata, size_t *initp, struct uio *uio)
1383 cn_t ccn, ccl, cn, left, cl;
1384 caddr_t data = rdata;
1390 if ((vap->va_flag & NTFS_AF_INRUN) == 0) {
1391 kprintf("ntfs_writevattr_plain: CAN'T WRITE RES. ATTRIBUTE\n");
1395 ddprintf(("ntfs_writentvattr_plain: data in run: %ld chains\n",
1403 for (; left && (cnt < vap->va_vruncnt); cnt++) {
1404 ccn = vap->va_vruncn[cnt];
1405 ccl = vap->va_vruncl[cnt];
1407 ddprintf(("ntfs_writentvattr_plain: " \
1408 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1409 (u_int32_t) left, (u_int32_t) ccn, \
1410 (u_int32_t) ccl, (u_int32_t) off));
1412 if (ntfs_cntob(ccl) < off) {
1413 off -= ntfs_cntob(ccl);
1417 if (!ccn && ip->i_number != NTFS_BOOTINO)
1420 ccl -= ntfs_btocn(off);
1421 cn = ccn + ntfs_btocn(off);
1422 off = ntfs_btocnoff(off);
1424 while (left && ccl) {
1425 #if defined(__DragonFly__)
1427 min(ntfs_cntob(ccl) - off, MAXBSIZE - off));
1429 /* under NetBSD, bread() can read
1430 * maximum one block worth of data */
1431 tocopy = min(left, ntmp->ntm_bps - off);
1433 cl = ntfs_btocl(tocopy + off);
1434 ddprintf(("ntfs_writentvattr_plain: write: " \
1435 "cn: 0x%x cl: %d, off: %d len: %d, left: %d\n",
1436 (u_int32_t) cn, (u_int32_t) cl,
1437 (u_int32_t) off, (u_int32_t) tocopy,
1439 if (off == 0 && tocopy == ntfs_cntob(cl) &&
1440 uio->uio_segflg != UIO_NOCOPY) {
1441 bp = getblk(ntmp->ntm_devvp, ntfs_cntodoff(cn),
1442 ntfs_cntob(cl), 0, 0);
1445 error = bread(ntmp->ntm_devvp,
1447 ntfs_cntob(cl), &bp);
1454 uiomove(bp->b_data + off, tocopy, uio);
1456 memcpy(bp->b_data + off, data, tocopy);
1458 data = data + tocopy;
1468 kprintf("ntfs_writentvattr_plain: POSSIBLE RUN ERROR\n");
1476 * This is one of read routines.
1478 * ntnode should be locked.
1481 ntfs_readntvattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1482 struct ntvattr *vap, off_t roff, size_t rsize,
1483 void *rdata, size_t *initp, struct uio *uio)
1489 if (vap->va_flag & NTFS_AF_INRUN) {
1491 cn_t ccn, ccl, cn, left, cl;
1492 caddr_t data = rdata;
1496 ddprintf(("ntfs_readntvattr_plain: data in run: %ld chains\n",
1504 while (left && (cnt < vap->va_vruncnt)) {
1505 ccn = vap->va_vruncn[cnt];
1506 ccl = vap->va_vruncl[cnt];
1508 ddprintf(("ntfs_readntvattr_plain: " \
1509 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1510 (u_int32_t) left, (u_int32_t) ccn, \
1511 (u_int32_t) ccl, (u_int32_t) off));
1513 if (ntfs_cntob(ccl) < off) {
1514 off -= ntfs_cntob(ccl);
1518 if (ccn || ip->i_number == NTFS_BOOTINO) {
1519 ccl -= ntfs_btocn(off);
1520 cn = ccn + ntfs_btocn(off);
1521 off = ntfs_btocnoff(off);
1523 while (left && ccl) {
1524 #if defined(__DragonFly__)
1526 min(ntfs_cntob(ccl) - off,
1529 /* under NetBSD, bread() can read
1530 * maximum one block worth of data */
1532 ntmp->ntm_bps - off);
1534 cl = ntfs_btocl(tocopy + off);
1535 ddprintf(("ntfs_readntvattr_plain: " \
1536 "read: cn: 0x%x cl: %d, " \
1537 "off: %d len: %d, left: %d\n",
1543 error = bread(ntmp->ntm_devvp,
1552 uiomove(bp->b_data + off,
1555 memcpy(data, bp->b_data + off,
1559 data = data + tocopy;
1567 tocopy = min(left, ntfs_cntob(ccl) - off);
1568 ddprintf(("ntfs_readntvattr_plain: "
1569 "hole: ccn: 0x%x ccl: %d, off: %d, " \
1570 " len: %d, left: %d\n",
1571 (u_int32_t) ccn, (u_int32_t) ccl,
1572 (u_int32_t) off, (u_int32_t) tocopy,
1577 size_t remains = tocopy;
1578 for(; remains; remains++)
1579 uiomove("", 1, uio);
1581 bzero(data, tocopy);
1582 data = data + tocopy;
1587 kprintf("ntfs_readntvattr_plain: POSSIBLE RUN ERROR\n");
1591 ddprintf(("ntfs_readnvattr_plain: data is in mft record\n"));
1593 uiomove(vap->va_datap + roff, rsize, uio);
1595 memcpy(rdata, vap->va_datap + roff, rsize);
1603 * This is one of read routines.
1606 ntfs_readattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1607 u_int32_t attrnum, char *attrname, off_t roff,
1608 size_t rsize, void *rdata, size_t * initp,
1613 off_t off = roff, left = rsize, toread;
1614 caddr_t data = rdata;
1615 struct ntvattr *vap;
1619 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1620 ntfs_btocn(off), &vap);
1623 toread = min(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1624 ddprintf(("ntfs_readattr_plain: o: %d, s: %d (%d - %d)\n",
1625 (u_int32_t) off, (u_int32_t) toread,
1626 (u_int32_t) vap->va_vcnstart,
1627 (u_int32_t) vap->va_vcnend));
1628 error = ntfs_readntvattr_plain(ntmp, ip, vap,
1629 off - ntfs_cntob(vap->va_vcnstart),
1630 toread, data, &init, uio);
1632 kprintf("ntfs_readattr_plain: " \
1633 "ntfs_readntvattr_plain failed: o: %d, s: %d\n",
1634 (u_int32_t) off, (u_int32_t) toread);
1635 kprintf("ntfs_readattr_plain: attrib: %d - %d\n",
1636 (u_int32_t) vap->va_vcnstart,
1637 (u_int32_t) vap->va_vcnend);
1638 ntfs_ntvattrrele(vap);
1641 ntfs_ntvattrrele(vap);
1644 data = data + toread;
1652 * This is one of read routines.
1655 ntfs_readattr(struct ntfsmount *ntmp, struct ntnode *ip, u_int32_t attrnum,
1656 char *attrname, off_t roff, size_t rsize, void *rdata,
1660 struct ntvattr *vap;
1663 ddprintf(("ntfs_readattr: reading %"PRId64": 0x%x, from %d size %d bytes\n",
1664 ip->i_number, attrnum, (u_int32_t) roff, (u_int32_t) rsize));
1666 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname, 0, &vap);
1670 if ((roff > vap->va_datalen) ||
1671 (roff + rsize > vap->va_datalen)) {
1672 ddprintf(("ntfs_readattr: offset too big\n"));
1673 ntfs_ntvattrrele(vap);
1676 if (vap->va_compression && vap->va_compressalg) {
1679 off_t off, left = rsize, tocopy;
1680 caddr_t data = rdata;
1683 ddprintf(("ntfs_ntreadattr: compression: %d\n",
1684 vap->va_compressalg));
1686 MALLOC(cup, u_int8_t *, ntfs_cntob(NTFS_COMPUNIT_CL),
1687 M_NTFSDECOMP, M_WAITOK);
1688 MALLOC(uup, u_int8_t *, ntfs_cntob(NTFS_COMPUNIT_CL),
1689 M_NTFSDECOMP, M_WAITOK);
1691 cn = (ntfs_btocn(roff)) & (~(NTFS_COMPUNIT_CL - 1));
1692 off = roff - ntfs_cntob(cn);
1695 error = ntfs_readattr_plain(ntmp, ip, attrnum,
1696 attrname, ntfs_cntob(cn),
1697 ntfs_cntob(NTFS_COMPUNIT_CL),
1702 tocopy = min(left, ntfs_cntob(NTFS_COMPUNIT_CL) - off);
1704 if (init == ntfs_cntob(NTFS_COMPUNIT_CL)) {
1706 uiomove(cup + off, tocopy, uio);
1708 memcpy(data, cup + off, tocopy);
1709 } else if (init == 0) {
1711 size_t remains = tocopy;
1712 for(; remains; remains--)
1713 uiomove("", 1, uio);
1716 bzero(data, tocopy);
1718 error = ntfs_uncompunit(ntmp, uup, cup);
1722 uiomove(uup + off, tocopy, uio);
1724 memcpy(data, uup + off, tocopy);
1728 data = data + tocopy;
1729 off += tocopy - ntfs_cntob(NTFS_COMPUNIT_CL);
1730 cn += NTFS_COMPUNIT_CL;
1733 FREE(uup, M_NTFSDECOMP);
1734 FREE(cup, M_NTFSDECOMP);
1736 error = ntfs_readattr_plain(ntmp, ip, attrnum, attrname,
1737 roff, rsize, rdata, &init, uio);
1738 ntfs_ntvattrrele(vap);
1744 ntfs_parserun(cn_t *cn, cn_t *cl, u_int8_t *run, u_long len, u_long *off)
1750 kprintf("ntfs_parsetun: run == NULL\n");
1755 kprintf("ntfs_parserun: trying to go out of run\n");
1759 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1760 kprintf("ntfs_parserun: " \
1761 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1765 for (i = 0; i < (sz & 0xF); i++)
1766 *cl += (u_int32_t) run[(*off)++] << (i << 3);
1769 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1770 kprintf("ntfs_parserun: " \
1771 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1775 for (i = 0; i < (sz & 0xF); i++)
1776 *cn += (u_int32_t) run[(*off)++] << (i << 3);
1783 * Process fixup routine on given buffer.
1786 ntfs_procfixups(struct ntfsmount *ntmp, u_int32_t magic, caddr_t buf,
1789 struct fixuphdr *fhp = (struct fixuphdr *) buf;
1795 if (fhp->fh_magic != magic) {
1796 kprintf("ntfs_procfixups: magic doesn't match: %08x != %08x\n",
1797 fhp->fh_magic, magic);
1800 if ((fhp->fh_fnum - 1) * ntmp->ntm_bps != len) {
1801 kprintf("ntfs_procfixups: " \
1802 "bad fixups number: %d for %ld bytes block\n",
1803 fhp->fh_fnum, (long)len); /* XXX kprintf kludge */
1806 if (fhp->fh_foff >= ntmp->ntm_spc * ntmp->ntm_mftrecsz * ntmp->ntm_bps) {
1807 kprintf("ntfs_procfixups: invalid offset: %x", fhp->fh_foff);
1810 fxp = (u_int16_t *) (buf + fhp->fh_foff);
1811 cfxp = (u_int16_t *) (buf + ntmp->ntm_bps - 2);
1813 for (i = 1; i < fhp->fh_fnum; i++, fxp++) {
1814 if (*cfxp != fixup) {
1815 kprintf("ntfs_procfixups: fixup %d doesn't match\n", i);
1819 cfxp = (u_int16_t *)(((caddr_t) cfxp) + ntmp->ntm_bps);
1826 ntfs_runtocn(cn_t *cn, struct ntfsmount *ntmp, u_int8_t *run, u_long len,
1836 kprintf("ntfs_runtocn: run: 0x%p, %ld bytes, vcn:%ld\n",
1837 run, len, (u_long) vcn);
1838 kprintf("ntfs_runtocn: run: ");
1839 for (i = 0; i < len; i++)
1840 kprintf("0x%02x ", run[i]);
1845 kprintf("ntfs_runtocn: run == NULL\n");
1849 if (run[off] == 0) {
1850 kprintf("ntfs_runtocn: vcn too big\n");
1854 error = ntfs_parserun(&ccn, &ccl, run, len, &off);
1856 kprintf("ntfs_runtocn: ntfs_parserun failed\n");
1859 } while (ccl <= vcn);
1866 * this initializes toupper table & dependant variables to be ready for
1870 ntfs_toupper_init(void)
1872 ntfs_toupper_tab = NULL;
1873 lockinit(&ntfs_toupper_lock, "ntfs_toupper", 0, 0);
1874 ntfs_toupper_usecount = 0;
1878 * if the ntfs_toupper_tab[] is filled already, just raise use count;
1879 * otherwise read the data from the filesystem we are currently mounting
1882 ntfs_toupper_use(struct mount *mp, struct ntfsmount *ntmp)
1887 /* get exclusive access */
1888 LOCKMGR(&ntfs_toupper_lock, LK_EXCLUSIVE);
1890 /* only read the translation data from a file if it hasn't been
1892 if (ntfs_toupper_tab)
1896 * Read in Unicode lowercase -> uppercase translation file.
1897 * XXX for now, just the first 256 entries are used anyway,
1898 * so don't bother reading more
1900 MALLOC(ntfs_toupper_tab, wchar *, 65536 * sizeof(wchar),
1901 M_NTFSRDATA, M_WAITOK);
1903 if ((error = VFS_VGET(mp, NULL, NTFS_UPCASEINO, &vp)))
1905 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
1906 0, 65536*sizeof(wchar), (char *) ntfs_toupper_tab, NULL);
1910 ntfs_toupper_usecount++;
1911 LOCKMGR(&ntfs_toupper_lock, LK_RELEASE);
1916 * lower the use count and if it reaches zero, free the memory
1917 * tied by toupper table
1920 ntfs_toupper_unuse(void)
1922 /* get exclusive access */
1923 LOCKMGR(&ntfs_toupper_lock, LK_EXCLUSIVE);
1925 ntfs_toupper_usecount--;
1926 if (ntfs_toupper_usecount == 0) {
1927 FREE(ntfs_toupper_tab, M_NTFSRDATA);
1928 ntfs_toupper_tab = NULL;
1931 else if (ntfs_toupper_usecount < 0) {
1932 panic("ntfs_toupper_unuse(): use count negative: %d\n",
1933 ntfs_toupper_usecount);
1937 /* release the lock */
1938 LOCKMGR(&ntfs_toupper_lock, LK_RELEASE);
1942 ntfs_u28_init(struct ntfsmount *ntmp, wchar *u2w, char *cs_local,
1948 if (ntfs_iconv && cs_local) {
1949 ntfs_iconv->open(cs_local, cs_ntfs, &ntmp->ntm_ic_u2l);
1953 MALLOC(u28, char **, 256 * sizeof(char*), M_TEMP, M_WAITOK | M_ZERO);
1955 for (i=0; i<256; i++) {
1956 h = (u2w[i] >> 8) & 0xFF;
1959 if (u28[h] == NULL) {
1960 MALLOC(u28[h], char *, 256 * sizeof(char), M_TEMP, M_WAITOK);
1961 for (j=0; j<256; j++)
1965 u28[h][l] = i & 0xFF;
1968 ntmp->ntm_u28 = u28;
1974 ntfs_u28_uninit(struct ntfsmount *ntmp)
1979 if (ntmp->ntm_u28 == NULL) {
1980 if (ntfs_iconv && ntmp->ntm_ic_u2l) {
1981 ntfs_iconv->close(ntmp->ntm_ic_u2l);
1986 if (ntmp->ntm_u28 == NULL)
1989 u28 = ntmp->ntm_u28;
1991 for (i=0; i<256; i++)
1993 FREE(u28[i], M_TEMP);
2001 ntfs_82u_init(struct ntfsmount *ntmp, char *cs_local, char *cs_ntfs)
2007 if (ntfs_iconv && cs_local) {
2008 ntfs_iconv->open(cs_ntfs, cs_local, &ntmp->ntm_ic_l2u);
2012 MALLOC(_82u, wchar *, 256 * sizeof(wchar), M_TEMP, M_WAITOK);
2014 for (i=0; i<256; i++)
2017 ntmp->ntm_82u = _82u;
2023 ntfs_82u_uninit(struct ntfsmount *ntmp)
2025 if (ntmp->ntm_82u == NULL) {
2026 if (ntfs_iconv && ntmp->ntm_ic_l2u) {
2027 ntfs_iconv->close(ntmp->ntm_ic_l2u);
2032 FREE(ntmp->ntm_82u, M_TEMP);
2037 * maps the Unicode char to 8bit equivalent
2038 * XXX currently only gets lower 8bit from the Unicode char
2039 * and substitutes a '_' for it if the result would be '\0';
2040 * something better has to be definitely though out
2043 ntfs_u28(struct ntfsmount *ntmp, wchar wc)
2045 char *p, *outp, inbuf[3], outbuf[3];
2048 if (ntfs_iconv && ntmp->ntm_ic_u2l) {
2050 inbuf[0] = (char)(wc>>8);
2051 inbuf[1] = (char)wc;
2055 ntfs_iconv->convchr(ntmp->ntm_ic_u2l, (const char **)&p, &ilen,
2058 return ((wchar)(outbuf[0]&0xFF));
2059 } else if (olen == 0) {
2060 return ((wchar)((outbuf[0]<<8) | (outbuf[1]&0xFF)));
2065 p = ntmp->ntm_u28[(wc>>8)&0xFF];
2068 return (p[wc&0xFF]);
2072 ntfs_82u(struct ntfsmount *ntmp,
2076 char *p, *outp, inbuf[3], outbuf[3];
2080 if (ntfs_iconv && ntmp->ntm_ic_l2u) {
2081 ilen = (size_t)*len;
2084 inbuf[0] = (char)(wc>>8);
2085 inbuf[1] = (char)wc;
2089 ntfs_iconv->convchr(ntmp->ntm_ic_l2u, (const char **)&p, &ilen,
2092 uc = (wchar)((outbuf[0]<<8) | (outbuf[1]&0xFF));
2097 if (ntmp->ntm_82u != NULL)
2098 return (ntmp->ntm_82u[wc&0xFF]);