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>
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/iconv.h>
47 #include <machine/inttypes.h>
50 #include <sys/spinlock2.h>
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 MALLOC_DEFINE(M_NTFSNTVATTR, "NTFS vattr", "NTFS file attribute information");
61 MALLOC_DEFINE(M_NTFSRDATA, "NTFS res data", "NTFS resident data");
62 MALLOC_DEFINE(M_NTFSRUN, "NTFS vrun", "NTFS vrun storage");
63 MALLOC_DEFINE(M_NTFSDECOMP, "NTFS decomp", "NTFS decompression temporary");
65 static int ntfs_ntlookupattr (struct ntfsmount *, const char *, int, int *, char **);
66 static int ntfs_findvattr (struct ntfsmount *, struct ntnode *, struct ntvattr **, struct ntvattr **, u_int32_t, const char *, size_t, cn_t);
67 static int ntfs_uastricmp (struct ntfsmount *, const wchar *, size_t, const char *, size_t);
68 static int ntfs_uastrcmp (struct ntfsmount *, const wchar *, size_t, const char *, size_t);
70 /* table for mapping Unicode chars into uppercase; it's filled upon first
71 * ntfs mount, freed upon last ntfs umount */
72 static wchar *ntfs_toupper_tab;
73 #define NTFS_TOUPPER(ch) (ntfs_toupper_tab[(ch)])
74 static struct lock ntfs_toupper_lock;
75 static signed int ntfs_toupper_usecount;
76 extern struct iconv_functions *ntfs_iconv;
78 /* support macro for ntfs_ntvattrget() */
79 #define NTFS_AALPCMP(aalp,type,name,namelen) ( \
80 (aalp->al_type == type) && (aalp->al_namelen == namelen) && \
81 !NTFS_UASTRCMP(aalp->al_name,aalp->al_namelen,name,namelen) )
87 ntfs_ntvattrrele(struct ntvattr *vap)
89 dprintf(("ntfs_ntvattrrele: ino: %"PRId64", type: 0x%x\n",
90 vap->va_ip->i_number, vap->va_type));
92 ntfs_ntrele(vap->va_ip);
98 * find the attribute in the ntnode
101 ntfs_findvattr(struct ntfsmount *ntmp, struct ntnode *ip,
102 struct ntvattr **lvapp, struct ntvattr **vapp, u_int32_t type,
103 const char *name, size_t namelen, cn_t vcn)
108 if((ip->i_flag & IN_LOADED) == 0) {
109 dprintf(("ntfs_findvattr: node not loaded, ino: %"PRId64"\n",
111 error = ntfs_loadntnode(ntmp,ip);
113 kprintf("ntfs_findvattr: FAILED TO LOAD INO: %"PRId64"\n",
121 for (vap = ip->i_valist.lh_first; vap; vap = vap->va_list.le_next) {
122 ddprintf(("ntfs_findvattr: type: 0x%x, vcn: %d - %d\n", \
123 vap->va_type, (u_int32_t) vap->va_vcnstart, \
124 (u_int32_t) vap->va_vcnend));
125 if ((vap->va_type == type) &&
126 (vap->va_vcnstart <= vcn) && (vap->va_vcnend >= vcn) &&
127 (vap->va_namelen == namelen) &&
128 (strncmp(name, vap->va_name, namelen) == 0)) {
130 ntfs_ntref(vap->va_ip);
133 if (vap->va_type == NTFS_A_ATTRLIST)
141 * Search attribute specifed in ntnode (load ntnode if nessecary).
142 * If not found but ATTR_A_ATTRLIST present, read it in and search throught.
143 * VOP_VGET node needed, and lookup througth it's ntnode (load if nessesary).
145 * ntnode should be locked
148 ntfs_ntvattrget(struct ntfsmount *ntmp, struct ntnode *ip, u_int32_t type,
149 const char *name, cn_t vcn, struct ntvattr **vapp)
151 struct ntvattr *lvap = NULL;
152 struct attr_attrlist *aalp;
153 struct attr_attrlist *nextaalp;
155 struct ntnode *newip;
163 dprintf(("ntfs_ntvattrget: " \
164 "ino: %"PRId64", type: 0x%x, name: %s, vcn: %d\n", \
165 ip->i_number, type, name, (u_int32_t) vcn));
166 namelen = strlen(name);
168 dprintf(("ntfs_ntvattrget: " \
169 "ino: %"PRId64", type: 0x%x, vcn: %d\n", \
170 ip->i_number, type, (u_int32_t) vcn));
175 error = ntfs_findvattr(ntmp, ip, &lvap, vapp, type, name, namelen, vcn);
180 dprintf(("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: " \
181 "ino: %"PRId64", type: 0x%x, name: %s, vcn: %d\n", \
182 ip->i_number, type, name, (u_int32_t) vcn));
185 /* Scan $ATTRIBUTE_LIST for requested attribute */
186 len = lvap->va_datalen;
187 alpool = kmalloc(len, M_TEMP, M_WAITOK);
188 error = ntfs_readntvattr_plain(ntmp, ip, lvap, 0, len, alpool, &len,
193 aalp = (struct attr_attrlist *) alpool;
196 for(; len > 0; aalp = nextaalp) {
197 dprintf(("ntfs_ntvattrget: " \
198 "attrlist: ino: %d, attr: 0x%x, vcn: %d\n", \
199 aalp->al_inumber, aalp->al_type, \
200 (u_int32_t) aalp->al_vcnstart));
202 if (len > aalp->reclen) {
203 nextaalp = NTFS_NEXTREC(aalp, struct attr_attrlist *);
209 if (!NTFS_AALPCMP(aalp, type, name, namelen) ||
210 (nextaalp && (nextaalp->al_vcnstart <= vcn) &&
211 NTFS_AALPCMP(nextaalp, type, name, namelen)))
214 dprintf(("ntfs_ntvattrget: attribute in ino: %d\n",
217 /* this is not a main record, so we can't use just plain
219 error = ntfs_vgetex(ntmp->ntm_mountp, aalp->al_inumber,
220 NTFS_A_DATA, NULL, LK_EXCLUSIVE,
221 VG_EXT, curthread, &newvp);
223 kprintf("ntfs_ntvattrget: CAN'T VGET INO: %d\n",
227 newip = VTONT(newvp);
228 /* XXX have to lock ntnode */
229 error = ntfs_findvattr(ntmp, newip, &lvap, vapp,
230 type, name, namelen, vcn);
234 kprintf("ntfs_ntvattrget: ATTRLIST ERROR.\n");
239 dprintf(("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: " \
240 "ino: %"PRId64", type: 0x%x, name: %.*s, vcn: %d\n", \
241 ip->i_number, type, (int) namelen, name, (u_int32_t) vcn));
243 kfree(alpool, M_TEMP);
248 * Read ntnode from disk, make ntvattr list.
250 * ntnode should be locked
253 ntfs_loadntnode(struct ntfsmount *ntmp, struct ntnode *ip)
255 struct filerec *mfrp;
259 struct ntvattr *nvap;
261 dprintf(("ntfs_loadntnode: loading ino: %"PRId64"\n",ip->i_number));
263 mfrp = kmalloc(ntfs_bntob(ntmp->ntm_bpmftrec), M_TEMP, M_WAITOK);
265 if (ip->i_number < NTFS_SYSNODESNUM) {
268 dprintf(("ntfs_loadntnode: read system node\n"));
270 bn = ntfs_cntobn(ntmp->ntm_mftcn) +
271 ntmp->ntm_bpmftrec * ip->i_number;
273 error = bread(ntmp->ntm_devvp,
274 ntfs_bntodoff(bn), ntfs_bntob(ntmp->ntm_bpmftrec), &bp);
276 kprintf("ntfs_loadntnode: BREAD FAILED\n");
280 memcpy(mfrp, bp->b_data, ntfs_bntob(ntmp->ntm_bpmftrec));
285 vp = ntmp->ntm_sysvn[NTFS_MFTINO];
286 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
287 ip->i_number * ntfs_bntob(ntmp->ntm_bpmftrec),
288 ntfs_bntob(ntmp->ntm_bpmftrec), mfrp, NULL);
290 kprintf("ntfs_loadntnode: ntfs_readattr failed\n");
295 /* Check if magic and fixups are correct */
296 error = ntfs_procfixups(ntmp, NTFS_FILEMAGIC, (caddr_t)mfrp,
297 ntfs_bntob(ntmp->ntm_bpmftrec));
299 kprintf("ntfs_loadntnode: BAD MFT RECORD %"PRId64"\n",
304 dprintf(("ntfs_loadntnode: load attrs for ino: %"PRId64"\n",ip->i_number));
305 off = mfrp->fr_attroff;
306 ap = (struct attr *) ((caddr_t)mfrp + off);
308 LIST_INIT(&ip->i_valist);
310 while (ap->a_hdr.a_type != -1) {
311 error = ntfs_attrtontvattr(ntmp, &nvap, ap);
316 LIST_INSERT_HEAD(&ip->i_valist, nvap, va_list);
318 off += ap->a_hdr.reclen;
319 ap = (struct attr *) ((caddr_t)mfrp + off);
322 kprintf("ntfs_loadntnode: failed to load attr ino: %"PRId64"\n",
327 ip->i_mainrec = mfrp->fr_mainrec;
328 ip->i_nlink = mfrp->fr_nlink;
329 ip->i_frflag = mfrp->fr_flags;
331 ip->i_flag |= IN_LOADED;
339 * Routine locks ntnode and increase usecount, just opposite of
343 ntfs_ntget(struct ntnode *ip)
345 dprintf(("ntfs_ntget: get ntnode %"PRId64": %p, usecount: %d\n",
346 ip->i_number, ip, ip->i_usecount));
348 ip->i_usecount++; /* ZZZ */
349 LOCKMGR(&ip->i_lock, LK_EXCLUSIVE);
355 * Routine search ntnode in hash, if found: lock, inc usecount and return.
356 * If not in hash allocate structure for ntnode, prefill it, lock,
357 * inc count and return.
359 * ntnode returned locked
362 ntfs_ntlookup(struct ntfsmount *ntmp, ino_t ino, struct ntnode **ipp)
366 dprintf(("ntfs_ntlookup: looking for ntnode %ju\n", (uintmax_t)ino));
369 if ((ip = ntfs_nthashlookup(ntmp->ntm_dev, ino)) != NULL) {
371 dprintf(("ntfs_ntlookup: ntnode %ju: %p, usecount: %d\n",
372 (uintmax_t)ino, ip, ip->i_usecount));
376 } while (LOCKMGR(&ntfs_hashlock, LK_EXCLUSIVE | LK_SLEEPFAIL));
378 ip = kmalloc(sizeof(struct ntnode), M_NTFSNTNODE, M_WAITOK | M_ZERO);
379 ddprintf(("ntfs_ntlookup: allocating ntnode: %ju: %p\n", ino, ip));
381 /* Generic initialization */
382 ip->i_devvp = ntmp->ntm_devvp;
383 ip->i_dev = ntmp->ntm_dev;
387 LIST_INIT(&ip->i_fnlist);
390 /* init lock and lock the newborn ntnode */
391 lockinit(&ip->i_lock, "ntnode", 0, 0);
392 spin_init(&ip->i_interlock, "ntfsntlookup");
397 LOCKMGR(&ntfs_hashlock, LK_RELEASE);
401 dprintf(("ntfs_ntlookup: ntnode %ju: %p, usecount: %d\n",
402 (uintmax_t)ino, ip, ip->i_usecount));
408 * Decrement usecount of ntnode and unlock it, if usecount reach zero,
411 * ntnode should be locked on entry, and unlocked on return.
414 ntfs_ntput(struct ntnode *ip)
418 dprintf(("ntfs_ntput: rele ntnode %"PRId64": %p, usecount: %d\n",
419 ip->i_number, ip, ip->i_usecount));
421 spin_lock(&ip->i_interlock);
425 if (ip->i_usecount < 0) {
426 spin_unlock(&ip->i_interlock);
427 panic("ntfs_ntput: ino: %"PRId64" usecount: %d ",
428 ip->i_number,ip->i_usecount);
432 if (ip->i_usecount > 0) {
433 spin_unlock(&ip->i_interlock);
434 LOCKMGR(&ip->i_lock, LK_RELEASE);
438 dprintf(("ntfs_ntput: deallocating ntnode: %"PRId64"\n", ip->i_number));
440 if (ip->i_fnlist.lh_first) {
441 spin_unlock(&ip->i_interlock);
442 panic("ntfs_ntput: ntnode has fnodes");
446 * XXX this is a bit iffy because we are making high level calls
447 * while holding a spinlock.
451 while ((vap = LIST_FIRST(&ip->i_valist)) != NULL) {
452 LIST_REMOVE(vap,va_list);
453 ntfs_freentvattr(vap);
455 spin_unlock(&ip->i_interlock);
457 kfree(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));
473 * Decrement usecount of ntnode.
476 ntfs_ntrele(struct ntnode *ip)
478 dprintf(("ntfs_ntrele: rele ntnode %"PRId64": %p, usecount: %d\n",
479 ip->i_number, ip, ip->i_usecount));
481 spin_lock(&ip->i_interlock);
484 if (ip->i_usecount < 0) {
485 spin_unlock(&ip->i_interlock);
486 panic("ntfs_ntrele: ino: %"PRId64" usecount: %d ",
487 ip->i_number,ip->i_usecount);
489 spin_unlock(&ip->i_interlock);
493 * Deallocate all memory allocated for ntvattr
496 ntfs_freentvattr(struct ntvattr *vap)
498 if (vap->va_flag & NTFS_AF_INRUN) {
500 kfree(vap->va_vruncn, M_NTFSRUN);
502 kfree(vap->va_vruncl, M_NTFSRUN);
505 kfree(vap->va_datap, M_NTFSRDATA);
507 kfree(vap, M_NTFSNTVATTR);
511 * Convert disk image of attribute into ntvattr structure,
512 * runs are expanded also.
515 ntfs_attrtontvattr(struct ntfsmount *ntmp, struct ntvattr **rvapp,
524 vap = kmalloc(sizeof(struct ntvattr), M_NTFSNTVATTR,
527 vap->va_flag = rap->a_hdr.a_flag;
528 vap->va_type = rap->a_hdr.a_type;
529 vap->va_compression = rap->a_hdr.a_compression;
530 vap->va_index = rap->a_hdr.a_index;
532 ddprintf(("type: 0x%x, index: %d", vap->va_type, vap->va_index));
534 vap->va_namelen = rap->a_hdr.a_namelen;
535 if (rap->a_hdr.a_namelen) {
536 wchar *unp = (wchar *) ((caddr_t) rap + rap->a_hdr.a_nameoff);
537 ddprintf((", name:["));
538 for (i = 0; i < vap->va_namelen; i++) {
539 vap->va_name[i] = unp[i];
540 ddprintf(("%c", vap->va_name[i]));
544 if (vap->va_flag & NTFS_AF_INRUN) {
545 ddprintf((", nonres."));
546 vap->va_datalen = rap->a_nr.a_datalen;
547 vap->va_allocated = rap->a_nr.a_allocated;
548 vap->va_vcnstart = rap->a_nr.a_vcnstart;
549 vap->va_vcnend = rap->a_nr.a_vcnend;
550 vap->va_compressalg = rap->a_nr.a_compressalg;
551 error = ntfs_runtovrun(&(vap->va_vruncn), &(vap->va_vruncl),
553 (caddr_t) rap + rap->a_nr.a_dataoff);
555 vap->va_compressalg = 0;
556 ddprintf((", res."));
557 vap->va_datalen = rap->a_r.a_datalen;
558 vap->va_allocated = rap->a_r.a_datalen;
559 vap->va_vcnstart = 0;
560 vap->va_vcnend = ntfs_btocn(vap->va_allocated);
561 vap->va_datap = kmalloc(vap->va_datalen, M_NTFSRDATA,
563 memcpy(vap->va_datap, (caddr_t) rap + rap->a_r.a_dataoff,
566 ddprintf((", len: %d", vap->va_datalen));
569 kfree(vap, M_NTFSNTVATTR);
579 * Expand run into more utilizable and more memory eating format.
582 ntfs_runtovrun(cn_t **rcnp, cn_t **rclp, u_long *rcntp, u_int8_t *run)
596 off += (run[off] & 0xF) + ((run[off] >> 4) & 0xF) + 1;
599 cn = kmalloc(cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
600 cl = kmalloc(cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
610 for (i = 0; i < (sz & 0xF); i++)
611 cl[cnt] += (u_int32_t) run[off++] << (i << 3);
614 if (run[off + sz - 1] & 0x80) {
615 tmp = ((u_int64_t) - 1) << (sz << 3);
616 for (i = 0; i < sz; i++)
617 tmp |= (u_int64_t) run[off++] << (i << 3);
620 for (i = 0; i < sz; i++)
621 tmp |= (u_int64_t) run[off++] << (i << 3);
624 prev = cn[cnt] = prev + tmp;
637 * Compare unicode and ascii string case insens.
640 ntfs_uastricmp(struct ntfsmount *ntmp, const wchar *ustr, size_t ustrlen,
641 const char *astr, size_t astrlen)
644 size_t i, j, mbstrlen = astrlen;
648 len = 0; /* avoid gcc warnings */
649 if (ntmp->ntm_ic_l2u) {
650 for (i = 0, j = 0; i < ustrlen && j < astrlen; i++, j++) {
651 if (j < astrlen -1) {
652 wc = (wchar)astr[j]<<8 | (astr[j+1]&0xFF);
655 wc = (wchar)astr[j]<<8 & 0xFF00;
658 res = ((int) NTFS_TOUPPER(ustr[i])) -
659 ((int)NTFS_TOUPPER(NTFS_82U(wc, &len)));
668 * We use NTFS_82U(NTFS_U28(c)) to get rid of unicode
669 * symbols not covered by translation table
671 for (i = 0; i < ustrlen && i < astrlen; i++) {
672 res = ((int) NTFS_TOUPPER(NTFS_82U(NTFS_U28(ustr[i]), &len))) -
673 ((int)NTFS_TOUPPER(NTFS_82U((wchar)astr[i], &len)));
678 return (ustrlen - mbstrlen);
682 * Compare unicode and ascii string case sens.
685 ntfs_uastrcmp(struct ntfsmount *ntmp, const wchar *ustr, size_t ustrlen,
686 const char *astr, size_t astrlen)
689 size_t i, j, mbstrlen = astrlen;
693 for (i = 0, j = 0; (i < ustrlen) && (j < astrlen); i++, j++) {
695 wc = NTFS_U28(ustr[i]);
698 if (u != '\0' && j < astrlen -1) {
699 res = (int) (u - astr[j++]);
702 res = (res<<8) + (int) (l - astr[j]);
706 return (ustrlen - mbstrlen);
710 * Search fnode in ntnode, if not found allocate and preinitialize.
712 * ntnode should be locked on entry.
715 ntfs_fget(struct ntfsmount *ntmp, struct ntnode *ip, int attrtype,
716 char *attrname, struct fnode **fpp)
720 dprintf(("ntfs_fget: ino: %"PRId64", attrtype: 0x%x, attrname: %s\n",
721 ip->i_number,attrtype, attrname?attrname:""));
723 for (fp = ip->i_fnlist.lh_first; fp != NULL; fp = fp->f_fnlist.le_next){
724 dprintf(("ntfs_fget: fnode: attrtype: %d, attrname: %s\n",
725 fp->f_attrtype, fp->f_attrname?fp->f_attrname:""));
727 if ((attrtype == fp->f_attrtype) &&
728 ((!attrname && !fp->f_attrname) ||
729 (attrname && fp->f_attrname &&
730 !strcmp(attrname,fp->f_attrname)))){
731 dprintf(("ntfs_fget: found existed: %p\n",fp));
739 fp = kmalloc(sizeof(struct fnode), M_NTFSFNODE, M_WAITOK | M_ZERO);
740 dprintf(("ntfs_fget: allocating fnode: %p\n",fp));
744 fp->f_flag |= FN_AATTRNAME;
745 fp->f_attrname = kmalloc(strlen(attrname) + 1, M_TEMP,
747 strcpy(fp->f_attrname, attrname);
749 fp->f_attrname = NULL;
750 fp->f_attrtype = attrtype;
754 LIST_INSERT_HEAD(&ip->i_fnlist, fp, f_fnlist);
762 * Deallocate fnode, remove it from ntnode's fnode list.
764 * ntnode should be locked.
767 ntfs_frele(struct fnode *fp)
769 struct ntnode *ip = FTONT(fp);
771 dprintf(("ntfs_frele: fnode: %p for %"PRId64": %p\n", fp, ip->i_number, ip));
773 dprintf(("ntfs_frele: deallocating fnode\n"));
774 LIST_REMOVE(fp,f_fnlist);
775 if (fp->f_flag & FN_AATTRNAME)
776 kfree(fp->f_attrname, M_TEMP);
778 kfree(fp->f_dirblbuf, M_NTFSDIR);
779 kfree(fp, M_NTFSFNODE);
784 * Lookup attribute name in format: [[:$ATTR_TYPE]:$ATTR_NAME],
785 * $ATTR_TYPE is searched in attrdefs read from $AttrDefs.
786 * If $ATTR_TYPE nott specifed, ATTR_A_DATA assumed.
789 ntfs_ntlookupattr(struct ntfsmount *ntmp, const char *name, int namelen,
790 int *attrtype, char **attrname)
794 struct ntvattrdef *adp;
799 if (name[0] == '$') {
801 for (syslen = 0; syslen < namelen; syslen++) {
802 if(sys[syslen] == ':') {
812 for (i = 0; i < ntmp->ntm_adnum; i++, adp++){
813 if (syslen != adp->ad_namelen ||
814 strncmp(sys, adp->ad_name, syslen) != 0)
817 *attrtype = adp->ad_type;
822 *attrtype = NTFS_A_DATA;
826 (*attrname) = kmalloc(namelen, M_TEMP, M_WAITOK);
827 memcpy((*attrname), name, namelen);
828 (*attrname)[namelen] = '\0';
835 * Lookup specifed node for filename, matching cnp,
836 * return fnode filled.
839 ntfs_ntlookupfile(struct ntfsmount *ntmp, struct vnode *vp,
840 struct componentname *cnp, struct vnode **vpp)
842 struct fnode *fp = VTOF(vp);
843 struct ntnode *ip = FTONT(fp);
844 struct ntvattr *vap; /* Root attribute */
845 cn_t cn; /* VCN in current attribute */
846 caddr_t rdbuf; /* Buffer to read directory's blocks */
848 u_int32_t rdsize; /* Length of data to read from current block */
849 struct attr_indexentry *iep;
850 int error, res, anamelen, fnamelen;
851 const char *fname,*aname;
853 int attrtype = NTFS_A_DATA;
854 char *attrname = NULL;
859 error = ntfs_ntget(ip);
863 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
864 if (error || (vap->va_flag & NTFS_AF_INRUN))
867 blsize = vap->va_a_iroot->ir_size;
868 rdsize = vap->va_datalen;
871 * Divide file name into: foofilefoofilefoofile[:attrspec]
872 * Store like this: fname:fnamelen [aname:anamelen]
874 fname = cnp->cn_nameptr;
877 for (fnamelen = 0; fnamelen < cnp->cn_namelen; fnamelen++)
878 if(fname[fnamelen] == ':') {
879 aname = fname + fnamelen + 1;
880 anamelen = cnp->cn_namelen - fnamelen - 1;
881 dprintf(("ntfs_ntlookupfile: %s (%d), attr: %s (%d)\n",
882 fname, fnamelen, aname, anamelen));
886 dprintf(("ntfs_ntlookupfile: blksz: %d, rdsz: %d\n", blsize, rdsize));
888 rdbuf = kmalloc(blsize, M_TEMP, M_WAITOK);
890 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXROOT, "$I30",
891 0, rdsize, rdbuf, NULL);
895 aoff = sizeof(struct attr_indexroot);
898 iep = (struct attr_indexentry *) (rdbuf + aoff);
900 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
902 iep = (struct attr_indexentry *) (rdbuf + aoff))
904 ddprintf(("scan: %d, %d\n",
905 (u_int32_t) iep->ie_number,
906 (u_int32_t) iep->ie_fnametype));
908 /* check the name - the case-insensitible check
909 * has to come first, to break from this for loop
910 * if needed, so we can dive correctly */
911 res = NTFS_UASTRICMP(iep->ie_fname, iep->ie_fnamelen,
914 if (res < 0) continue;
916 if (iep->ie_fnametype == 0 ||
917 !(ntmp->ntm_flag & NTFS_MFLAG_CASEINS))
919 res = NTFS_UASTRCMP(iep->ie_fname,
920 iep->ie_fnamelen, fname, fnamelen);
921 if (res != 0) continue;
925 error = ntfs_ntlookupattr(ntmp,
927 &attrtype, &attrname);
932 /* Check if we've found ourself */
933 if ((iep->ie_number == ip->i_number) &&
934 (attrtype == fp->f_attrtype) &&
935 ((!attrname && !fp->f_attrname) ||
936 (attrname && fp->f_attrname &&
937 !strcmp(attrname, fp->f_attrname))))
945 /* vget node, but don't load it */
946 error = ntfs_vgetex(ntmp->ntm_mountp,
947 iep->ie_number, attrtype, attrname,
948 LK_EXCLUSIVE, VG_DONTLOADIN | VG_DONTVALIDFN,
951 /* free the buffer returned by ntfs_ntlookupattr() */
953 kfree(attrname, M_TEMP);
962 if (nfp->f_flag & FN_VALID) {
967 nfp->f_fflag = iep->ie_fflag;
968 nfp->f_pnumber = iep->ie_fpnumber;
969 nfp->f_times = iep->ie_ftimes;
971 if((nfp->f_fflag & NTFS_FFLAG_DIR) &&
972 (nfp->f_attrtype == NTFS_A_DATA) &&
973 (nfp->f_attrname == NULL))
978 nvp->v_type = f_type;
980 if ((nfp->f_attrtype == NTFS_A_DATA) &&
981 (nfp->f_attrname == NULL))
983 /* Opening default attribute */
984 nfp->f_size = iep->ie_fsize;
985 nfp->f_allocated = iep->ie_fallocated;
986 nfp->f_flag |= FN_PRELOADED;
988 error = ntfs_filesize(ntmp, nfp,
989 &nfp->f_size, &nfp->f_allocated);
995 nfp->f_flag &= ~FN_VALID;
998 * Normal files use the buffer cache
1000 if (nvp->v_type == VREG)
1001 vinitvmio(nvp, nfp->f_size, PAGE_SIZE, -1);
1006 /* Dive if possible */
1007 if (iep->ie_flag & NTFS_IEFLAG_SUBNODE) {
1008 dprintf(("ntfs_ntlookupfile: diving\n"));
1010 cn = *(cn_t *) (rdbuf + aoff +
1011 iep->reclen - sizeof(cn_t));
1014 error = ntfs_readattr(ntmp, ip, NTFS_A_INDX, "$I30",
1015 ntfs_cntob(cn), rdsize, rdbuf, NULL);
1019 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1024 aoff = (((struct attr_indexalloc *) rdbuf)->ia_hdrsize +
1027 dprintf(("ntfs_ntlookupfile: nowhere to dive :-(\n"));
1033 dprintf(("finish\n"));
1036 if (attrname) kfree(attrname, M_TEMP);
1037 ntfs_ntvattrrele(vap);
1039 kfree(rdbuf, M_TEMP);
1044 * Check if name type is permitted to show.
1047 ntfs_isnamepermitted(struct ntfsmount *ntmp, struct attr_indexentry *iep)
1049 if (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)
1052 switch (iep->ie_fnametype) {
1054 ddprintf(("ntfs_isnamepermitted: skipped DOS name\n"));
1056 case 0: case 1: case 3:
1059 kprintf("ntfs_isnamepermitted: " \
1060 "WARNING! Unknown file name type: %d\n",
1068 * Read ntfs dir like stream of attr_indexentry, not like btree of them.
1069 * This is done by scaning $BITMAP:$I30 for busy clusters and reading them.
1070 * Ofcouse $INDEX_ROOT:$I30 is read before. Last read values are stored in
1071 * fnode, so we can skip toward record number num almost immediatly.
1072 * Anyway this is rather slow routine. The problem is that we don't know
1073 * how many records are there in $INDEX_ALLOCATION:$I30 block.
1076 ntfs_ntreaddir(struct ntfsmount *ntmp, struct fnode *fp,
1077 u_int32_t num, struct attr_indexentry **riepp)
1079 struct ntnode *ip = FTONT(fp);
1080 struct ntvattr *vap = NULL; /* IndexRoot attribute */
1081 struct ntvattr *bmvap = NULL; /* BitMap attribute */
1082 struct ntvattr *iavap = NULL; /* IndexAllocation attribute */
1083 caddr_t rdbuf; /* Buffer to read directory's blocks */
1084 u_char *bmp = NULL; /* Bitmap */
1085 u_int32_t blsize; /* Index allocation size (2048) */
1086 u_int32_t rdsize; /* Length of data to read */
1087 u_int32_t attrnum; /* Current attribute type */
1088 u_int32_t cpbl = 1; /* Clusters per directory block */
1090 struct attr_indexentry *iep;
1092 u_int32_t aoff, cnum;
1094 dprintf(("ntfs_ntreaddir: read ino: %"PRId64", num: %d\n", ip->i_number, num));
1095 error = ntfs_ntget(ip);
1099 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
1103 if (fp->f_dirblbuf == NULL) {
1104 fp->f_dirblsz = vap->va_a_iroot->ir_size;
1105 fp->f_dirblbuf = kmalloc(max(vap->va_datalen, fp->f_dirblsz),
1106 M_NTFSDIR, M_WAITOK);
1109 blsize = fp->f_dirblsz;
1110 rdbuf = fp->f_dirblbuf;
1112 dprintf(("ntfs_ntreaddir: rdbuf: 0x%p, blsize: %d\n", rdbuf, blsize));
1114 if (vap->va_a_iroot->ir_flag & NTFS_IRFLAG_INDXALLOC) {
1115 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXBITMAP, "$I30",
1121 bmp = kmalloc(bmvap->va_datalen, M_TEMP, M_WAITOK);
1122 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXBITMAP, "$I30", 0,
1123 bmvap->va_datalen, bmp, NULL);
1127 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDX, "$I30",
1133 cpbl = ntfs_btocn(blsize + ntfs_cntob(1) - 1);
1134 dprintf(("ntfs_ntreaddir: indexalloc: %d, cpbl: %d\n",
1135 iavap->va_datalen, cpbl));
1137 dprintf(("ntfs_ntreadidir: w/o BitMap and IndexAllocation\n"));
1138 iavap = bmvap = NULL;
1142 /* Try use previous values */
1143 if ((fp->f_lastdnum < num) && (fp->f_lastdnum != 0)) {
1144 attrnum = fp->f_lastdattr;
1145 aoff = fp->f_lastdoff;
1146 blnum = fp->f_lastdblnum;
1147 cnum = fp->f_lastdnum;
1149 attrnum = NTFS_A_INDXROOT;
1150 aoff = sizeof(struct attr_indexroot);
1156 dprintf(("ntfs_ntreaddir: scan: 0x%x, %d, %d, %d, %d\n",
1157 attrnum, blnum, cnum, num, aoff));
1158 rdsize = (attrnum == NTFS_A_INDXROOT) ? vap->va_datalen : blsize;
1159 error = ntfs_readattr(ntmp, ip, attrnum, "$I30",
1160 ntfs_cntob(blnum * cpbl), rdsize, rdbuf, NULL);
1164 if (attrnum == NTFS_A_INDX) {
1165 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1171 aoff = (attrnum == NTFS_A_INDX) ?
1172 (0x18 + ((struct attr_indexalloc *) rdbuf)->ia_hdrsize) :
1173 sizeof(struct attr_indexroot);
1175 iep = (struct attr_indexentry *) (rdbuf + aoff);
1176 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
1177 aoff += iep->reclen,
1178 iep = (struct attr_indexentry *) (rdbuf + aoff))
1180 if (!ntfs_isnamepermitted(ntmp, iep)) continue;
1183 fp->f_lastdnum = cnum;
1184 fp->f_lastdoff = aoff;
1185 fp->f_lastdblnum = blnum;
1186 fp->f_lastdattr = attrnum;
1197 if (attrnum == NTFS_A_INDXROOT)
1202 while (ntfs_cntob(blnum * cpbl) < iavap->va_datalen) {
1203 if (bmp[blnum >> 3] & (1 << (blnum & 3)))
1208 attrnum = NTFS_A_INDX;
1210 if (ntfs_cntob(blnum * cpbl) >= iavap->va_datalen)
1212 dprintf(("ntfs_ntreaddir: blnum: %d\n", blnum));
1221 ntfs_ntvattrrele(vap);
1223 ntfs_ntvattrrele(bmvap);
1225 ntfs_ntvattrrele(iavap);
1233 * Convert NTFS times that are in 100 ns units and begins from
1234 * 1601 Jan 1 into unix times.
1237 ntfs_nttimetounix(u_int64_t nt)
1241 /* WindowNT times are in 100 ns and from 1601 Jan 1 */
1242 t.tv_nsec = (nt % (1000 * 1000 * 10)) * 100;
1243 t.tv_sec = nt / (1000 * 1000 * 10) -
1244 369LL * 365LL * 24LL * 60LL * 60LL -
1245 89LL * 1LL * 24LL * 60LL * 60LL;
1250 * Get file times from NTFS_A_NAME attribute.
1253 ntfs_times(struct ntfsmount *ntmp, struct ntnode *ip, ntfs_times_t *tm)
1255 struct ntvattr *vap;
1258 dprintf(("ntfs_times: ino: %"PRId64"...\n", ip->i_number));
1260 error = ntfs_ntget(ip);
1264 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_NAME, NULL, 0, &vap);
1269 *tm = vap->va_a_name->n_times;
1270 ntfs_ntvattrrele(vap);
1277 * Get file sizes from corresponding attribute.
1279 * ntnode under fnode should be locked.
1282 ntfs_filesize(struct ntfsmount *ntmp, struct fnode *fp, u_int64_t *size,
1285 struct ntvattr *vap;
1286 struct ntnode *ip = FTONT(fp);
1290 dprintf(("ntfs_filesize: ino: %"PRId64"\n", ip->i_number));
1292 error = ntfs_ntvattrget(ntmp, ip,
1293 fp->f_attrtype, fp->f_attrname, 0, &vap);
1297 bn = vap->va_allocated;
1298 sz = vap->va_datalen;
1300 dprintf(("ntfs_filesize: %d bytes (%d bytes allocated)\n",
1301 (u_int32_t) sz, (u_int32_t) bn));
1308 ntfs_ntvattrrele(vap);
1314 * This is one of write routine.
1317 ntfs_writeattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1318 u_int32_t attrnum, char *attrname, off_t roff,
1319 size_t rsize, void *rdata, size_t *initp,
1324 off_t off = roff, left = rsize, towrite;
1325 caddr_t data = rdata;
1326 struct ntvattr *vap;
1330 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1331 ntfs_btocn(off), &vap);
1334 towrite = min(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1335 ddprintf(("ntfs_writeattr_plain: o: %d, s: %d (%d - %d)\n",
1336 (u_int32_t) off, (u_int32_t) towrite,
1337 (u_int32_t) vap->va_vcnstart,
1338 (u_int32_t) vap->va_vcnend));
1339 error = ntfs_writentvattr_plain(ntmp, ip, vap,
1340 off - ntfs_cntob(vap->va_vcnstart),
1341 towrite, data, &init, uio);
1343 kprintf("ntfs_writeattr_plain: " \
1344 "ntfs_writentvattr_plain failed: o: %d, s: %d\n",
1345 (u_int32_t) off, (u_int32_t) towrite);
1346 kprintf("ntfs_writeattr_plain: attrib: %d - %d\n",
1347 (u_int32_t) vap->va_vcnstart,
1348 (u_int32_t) vap->va_vcnend);
1349 ntfs_ntvattrrele(vap);
1352 ntfs_ntvattrrele(vap);
1355 data = data + towrite;
1363 * This is one of write routine.
1365 * ntnode should be locked.
1368 ntfs_writentvattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1369 struct ntvattr *vap, off_t roff, size_t rsize,
1370 void *rdata, size_t *initp, struct uio *uio)
1375 cn_t ccn, ccl, cn, left, cl;
1376 caddr_t data = rdata;
1382 if ((vap->va_flag & NTFS_AF_INRUN) == 0) {
1383 kprintf("ntfs_writevattr_plain: CAN'T WRITE RES. ATTRIBUTE\n");
1387 ddprintf(("ntfs_writentvattr_plain: data in run: %ld chains\n",
1395 for (; left && (cnt < vap->va_vruncnt); cnt++) {
1396 ccn = vap->va_vruncn[cnt];
1397 ccl = vap->va_vruncl[cnt];
1399 ddprintf(("ntfs_writentvattr_plain: " \
1400 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1401 (u_int32_t) left, (u_int32_t) ccn, \
1402 (u_int32_t) ccl, (u_int32_t) off));
1404 if (ntfs_cntob(ccl) < off) {
1405 off -= ntfs_cntob(ccl);
1409 if (!ccn && ip->i_number != NTFS_BOOTINO)
1412 ccl -= ntfs_btocn(off);
1413 cn = ccn + ntfs_btocn(off);
1414 off = ntfs_btocnoff(off);
1416 while (left && ccl) {
1418 * Always read and write single clusters at a time -
1419 * we need to avoid requesting differently-sized
1420 * blocks at the same disk offsets to avoid
1421 * confusing the buffer cache.
1423 tocopy = min(left, ntfs_cntob(1) - off);
1424 cl = ntfs_btocl(tocopy + off);
1425 KASSERT(cl == 1 && tocopy <= ntfs_cntob(1),
1426 ("single cluster limit mistake"));
1427 ddprintf(("ntfs_writentvattr_plain: write: " \
1428 "cn: 0x%x cl: %d, off: %d len: %d, left: %d\n",
1429 (u_int32_t) cn, (u_int32_t) cl,
1430 (u_int32_t) off, (u_int32_t) tocopy,
1432 if (off == 0 && tocopy == ntfs_cntob(cl) &&
1433 uio->uio_segflg != UIO_NOCOPY) {
1434 bp = getblk(ntmp->ntm_devvp, ntfs_cntodoff(cn),
1435 ntfs_cntob(cl), 0, 0);
1438 error = bread(ntmp->ntm_devvp,
1440 ntfs_cntob(cl), &bp);
1447 uiomovebp(bp, bp->b_data + off, tocopy, uio);
1449 memcpy(bp->b_data + off, data, tocopy);
1451 data = data + tocopy;
1461 kprintf("ntfs_writentvattr_plain: POSSIBLE RUN ERROR\n");
1469 * This is one of read routines.
1471 * ntnode should be locked.
1474 ntfs_readntvattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1475 struct ntvattr *vap, off_t roff, size_t rsize,
1476 void *rdata, size_t *initp, struct uio *uio)
1482 if (vap->va_flag & NTFS_AF_INRUN) {
1484 cn_t ccn, ccl, cn, left, cl;
1485 caddr_t data = rdata;
1489 ddprintf(("ntfs_readntvattr_plain: data in run: %ld chains\n",
1497 while (left && (cnt < vap->va_vruncnt)) {
1498 ccn = vap->va_vruncn[cnt];
1499 ccl = vap->va_vruncl[cnt];
1501 ddprintf(("ntfs_readntvattr_plain: " \
1502 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1503 (u_int32_t) left, (u_int32_t) ccn, \
1504 (u_int32_t) ccl, (u_int32_t) off));
1506 if (ntfs_cntob(ccl) < off) {
1507 off -= ntfs_cntob(ccl);
1511 if (ccn || ip->i_number == NTFS_BOOTINO) {
1512 ccl -= ntfs_btocn(off);
1513 cn = ccn + ntfs_btocn(off);
1514 off = ntfs_btocnoff(off);
1516 while (left && ccl) {
1518 min(ntfs_cntob(ccl) - off,
1520 cl = ntfs_btocl(tocopy + off);
1523 * Always read single clusters at a
1524 * time - we need to avoid reading
1525 * differently-sized blocks at the
1526 * same disk offsets to avoid
1527 * confusing the buffer cache.
1530 ntfs_cntob(1) - off);
1531 cl = ntfs_btocl(tocopy + off);
1533 tocopy <= ntfs_cntob(1),
1534 ("single cluster limit mistake"));
1536 ddprintf(("ntfs_readntvattr_plain: " \
1537 "read: cn: 0x%x cl: %d, " \
1538 "off: %d len: %d, left: %d\n",
1544 error = bread(ntmp->ntm_devvp,
1553 uiomovebp(bp, bp->b_data + off,
1556 memcpy(data, bp->b_data + off,
1560 data = data + tocopy;
1568 tocopy = min(left, ntfs_cntob(ccl) - off);
1569 ddprintf(("ntfs_readntvattr_plain: "
1570 "hole: ccn: 0x%x ccl: %d, off: %d, " \
1571 " len: %d, left: %d\n",
1572 (u_int32_t) ccn, (u_int32_t) ccl,
1573 (u_int32_t) off, (u_int32_t) tocopy,
1578 size_t remains = tocopy;
1579 for(; remains; remains--)
1580 uiomove("", 1, uio);
1582 bzero(data, tocopy);
1583 data = data + tocopy;
1588 kprintf("ntfs_readntvattr_plain: POSSIBLE RUN ERROR\n");
1592 ddprintf(("ntfs_readnvattr_plain: data is in mft record\n"));
1594 uiomove(vap->va_datap + roff, rsize, uio);
1596 memcpy(rdata, vap->va_datap + roff, rsize);
1604 * This is one of read routines.
1607 ntfs_readattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1608 u_int32_t attrnum, char *attrname, off_t roff,
1609 size_t rsize, void *rdata, size_t * initp,
1614 off_t off = roff, left = rsize, toread;
1615 caddr_t data = rdata;
1616 struct ntvattr *vap;
1620 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1621 ntfs_btocn(off), &vap);
1624 toread = min(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1625 ddprintf(("ntfs_readattr_plain: o: %d, s: %d (%d - %d)\n",
1626 (u_int32_t) off, (u_int32_t) toread,
1627 (u_int32_t) vap->va_vcnstart,
1628 (u_int32_t) vap->va_vcnend));
1629 error = ntfs_readntvattr_plain(ntmp, ip, vap,
1630 off - ntfs_cntob(vap->va_vcnstart),
1631 toread, data, &init, uio);
1633 kprintf("ntfs_readattr_plain: " \
1634 "ntfs_readntvattr_plain failed: o: %d, s: %d\n",
1635 (u_int32_t) off, (u_int32_t) toread);
1636 kprintf("ntfs_readattr_plain: attrib: %d - %d\n",
1637 (u_int32_t) vap->va_vcnstart,
1638 (u_int32_t) vap->va_vcnend);
1639 ntfs_ntvattrrele(vap);
1642 ntfs_ntvattrrele(vap);
1645 data = data + toread;
1653 * This is one of read routines.
1656 ntfs_readattr(struct ntfsmount *ntmp, struct ntnode *ip, u_int32_t attrnum,
1657 char *attrname, off_t roff, size_t rsize, void *rdata,
1661 struct ntvattr *vap;
1664 ddprintf(("ntfs_readattr: reading %"PRId64": 0x%x, from %d size %d bytes\n",
1665 ip->i_number, attrnum, (u_int32_t) roff, (u_int32_t) rsize));
1667 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname, 0, &vap);
1671 if ((roff > vap->va_datalen) ||
1672 (roff + rsize > vap->va_datalen)) {
1673 ddprintf(("ntfs_readattr: offset too big\n"));
1674 ntfs_ntvattrrele(vap);
1677 if (vap->va_compression && vap->va_compressalg) {
1680 off_t off, left = rsize, tocopy;
1681 caddr_t data = rdata;
1684 ddprintf(("ntfs_ntreadattr: compression: %d\n",
1685 vap->va_compressalg));
1687 cup = kmalloc(ntfs_cntob(NTFS_COMPUNIT_CL), M_NTFSDECOMP,
1689 uup = kmalloc(ntfs_cntob(NTFS_COMPUNIT_CL), M_NTFSDECOMP,
1692 cn = rounddown2(ntfs_btocn(roff), NTFS_COMPUNIT_CL);
1693 off = roff - ntfs_cntob(cn);
1696 error = ntfs_readattr_plain(ntmp, ip, attrnum,
1697 attrname, ntfs_cntob(cn),
1698 ntfs_cntob(NTFS_COMPUNIT_CL),
1703 tocopy = min(left, ntfs_cntob(NTFS_COMPUNIT_CL) - off);
1705 if (init == ntfs_cntob(NTFS_COMPUNIT_CL)) {
1707 uiomove(cup + off, tocopy, uio);
1709 memcpy(data, cup + off, tocopy);
1710 } else if (init == 0) {
1712 size_t remains = tocopy;
1713 for(; remains; remains--)
1714 uiomove("", 1, uio);
1717 bzero(data, tocopy);
1719 error = ntfs_uncompunit(ntmp, uup, cup);
1723 uiomove(uup + off, tocopy, uio);
1725 memcpy(data, uup + off, tocopy);
1729 data = data + tocopy;
1730 off += tocopy - ntfs_cntob(NTFS_COMPUNIT_CL);
1731 cn += NTFS_COMPUNIT_CL;
1734 kfree(uup, M_NTFSDECOMP);
1735 kfree(cup, M_NTFSDECOMP);
1737 error = ntfs_readattr_plain(ntmp, ip, attrnum, attrname,
1738 roff, rsize, rdata, &init, uio);
1739 ntfs_ntvattrrele(vap);
1745 ntfs_parserun(cn_t *cn, cn_t *cl, u_int8_t *run, u_long len, u_long *off)
1751 kprintf("ntfs_parsetun: run == NULL\n");
1756 kprintf("ntfs_parserun: trying to go out of run\n");
1760 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1761 kprintf("ntfs_parserun: " \
1762 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1766 for (i = 0; i < (sz & 0xF); i++)
1767 *cl += (u_int32_t) run[(*off)++] << (i << 3);
1770 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1771 kprintf("ntfs_parserun: " \
1772 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1776 for (i = 0; i < (sz & 0xF); i++)
1777 *cn += (u_int32_t) run[(*off)++] << (i << 3);
1784 * Process fixup routine on given buffer.
1787 ntfs_procfixups(struct ntfsmount *ntmp, u_int32_t magic, caddr_t buf,
1790 struct fixuphdr *fhp = (struct fixuphdr *) buf;
1796 if (fhp->fh_magic != magic) {
1797 kprintf("ntfs_procfixups: magic doesn't match: %08x != %08x\n",
1798 fhp->fh_magic, magic);
1801 if ((fhp->fh_fnum - 1) * ntmp->ntm_bps != len) {
1802 kprintf("ntfs_procfixups: " \
1803 "bad fixups number: %d for %ld bytes block\n",
1804 fhp->fh_fnum, (long)len); /* XXX kprintf kludge */
1807 if (fhp->fh_foff >= ntmp->ntm_spc * ntmp->ntm_mftrecsz * ntmp->ntm_bps) {
1808 kprintf("ntfs_procfixups: invalid offset: %x", fhp->fh_foff);
1811 fxp = (u_int16_t *) (buf + fhp->fh_foff);
1812 cfxp = (u_int16_t *) (buf + ntmp->ntm_bps - 2);
1814 for (i = 1; i < fhp->fh_fnum; i++, fxp++) {
1815 if (*cfxp != fixup) {
1816 kprintf("ntfs_procfixups: fixup %d doesn't match\n", i);
1820 cfxp = (u_int16_t *)(((caddr_t) cfxp) + ntmp->ntm_bps);
1827 ntfs_runtocn(cn_t *cn, struct ntfsmount *ntmp, u_int8_t *run, u_long len,
1837 kprintf("ntfs_runtocn: run: 0x%p, %ld bytes, vcn:%ld\n",
1838 run, len, (u_long) vcn);
1839 kprintf("ntfs_runtocn: run: ");
1840 for (i = 0; i < len; i++)
1841 kprintf("0x%02x ", run[i]);
1846 kprintf("ntfs_runtocn: run == NULL\n");
1850 if (run[off] == 0) {
1851 kprintf("ntfs_runtocn: vcn too big\n");
1855 error = ntfs_parserun(&ccn, &ccl, run, len, &off);
1857 kprintf("ntfs_runtocn: ntfs_parserun failed\n");
1860 } while (ccl <= vcn);
1867 * this initializes toupper table & dependant variables to be ready for
1871 ntfs_toupper_init(void)
1873 ntfs_toupper_tab = NULL;
1874 lockinit(&ntfs_toupper_lock, "ntfs_toupper", 0, 0);
1875 ntfs_toupper_usecount = 0;
1879 * if the ntfs_toupper_tab[] is filled already, just raise use count;
1880 * otherwise read the data from the filesystem we are currently mounting
1883 ntfs_toupper_use(struct mount *mp, struct ntfsmount *ntmp)
1888 /* get exclusive access */
1889 LOCKMGR(&ntfs_toupper_lock, LK_EXCLUSIVE);
1891 /* only read the translation data from a file if it hasn't been
1893 if (ntfs_toupper_tab)
1897 * Read in Unicode lowercase -> uppercase translation file.
1898 * XXX for now, just the first 256 entries are used anyway,
1899 * so don't bother reading more
1901 ntfs_toupper_tab = kmalloc(65536 * sizeof(wchar), M_NTFSRDATA,
1904 if ((error = VFS_VGET(mp, NULL, NTFS_UPCASEINO, &vp)))
1906 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
1907 0, 65536*sizeof(wchar), (char *) ntfs_toupper_tab, NULL);
1911 ntfs_toupper_usecount++;
1912 LOCKMGR(&ntfs_toupper_lock, LK_RELEASE);
1917 * lower the use count and if it reaches zero, free the memory
1918 * tied by toupper table
1921 ntfs_toupper_unuse(void)
1923 /* get exclusive access */
1924 LOCKMGR(&ntfs_toupper_lock, LK_EXCLUSIVE);
1926 ntfs_toupper_usecount--;
1927 if (ntfs_toupper_usecount == 0) {
1928 kfree(ntfs_toupper_tab, M_NTFSRDATA);
1929 ntfs_toupper_tab = NULL;
1932 else if (ntfs_toupper_usecount < 0) {
1933 panic("ntfs_toupper_unuse(): use count negative: %d",
1934 ntfs_toupper_usecount);
1938 /* release the lock */
1939 LOCKMGR(&ntfs_toupper_lock, LK_RELEASE);
1943 ntfs_u28_init(struct ntfsmount *ntmp, wchar *u2w, char *cs_local,
1949 if (ntfs_iconv && cs_local) {
1950 ntfs_iconv->open(cs_local, cs_ntfs, &ntmp->ntm_ic_u2l);
1954 u28 = kmalloc(256 * sizeof(char *), M_TEMP, M_WAITOK | M_ZERO);
1956 for (i=0; i<256; i++) {
1957 h = (u2w[i] >> 8) & 0xFF;
1960 if (u28[h] == NULL) {
1961 u28[h] = kmalloc(256 * sizeof(char), M_TEMP, M_WAITOK);
1962 for (j=0; j<256; j++)
1966 u28[h][l] = i & 0xFF;
1969 ntmp->ntm_u28 = u28;
1975 ntfs_u28_uninit(struct ntfsmount *ntmp)
1980 if (ntmp->ntm_u28 == NULL) {
1981 if (ntfs_iconv && ntmp->ntm_ic_u2l) {
1982 ntfs_iconv->close(ntmp->ntm_ic_u2l);
1987 if (ntmp->ntm_u28 == NULL)
1990 u28 = ntmp->ntm_u28;
1992 for (i=0; i<256; i++)
1994 kfree(u28[i], M_TEMP);
2002 ntfs_82u_init(struct ntfsmount *ntmp, char *cs_local, char *cs_ntfs)
2008 if (ntfs_iconv && cs_local) {
2009 ntfs_iconv->open(cs_ntfs, cs_local, &ntmp->ntm_ic_l2u);
2013 _82u = kmalloc(256 * sizeof(wchar), M_TEMP, M_WAITOK);
2015 for (i=0; i<256; i++)
2018 ntmp->ntm_82u = _82u;
2024 ntfs_82u_uninit(struct ntfsmount *ntmp)
2026 if (ntmp->ntm_82u == NULL) {
2027 if (ntfs_iconv && ntmp->ntm_ic_l2u) {
2028 ntfs_iconv->close(ntmp->ntm_ic_l2u);
2033 kfree(ntmp->ntm_82u, M_TEMP);
2038 * maps the Unicode char to 8bit equivalent
2039 * XXX currently only gets lower 8bit from the Unicode char
2040 * and substitutes a '_' for it if the result would be '\0';
2041 * something better has to be definitely though out
2044 ntfs_u28(struct ntfsmount *ntmp, wchar wc)
2046 char *p, *outp, inbuf[3], outbuf[3];
2049 if (ntfs_iconv && ntmp->ntm_ic_u2l) {
2051 inbuf[0] = (char)(wc>>8);
2052 inbuf[1] = (char)wc;
2056 ntfs_iconv->convchr(ntmp->ntm_ic_u2l,
2057 (const char **)(void *)&p, &ilen, &outp, &olen);
2059 return ((wchar)(outbuf[0]&0xFF));
2060 } else if (olen == 0) {
2061 return ((wchar)((outbuf[0]<<8) | (outbuf[1]&0xFF)));
2066 p = ntmp->ntm_u28[(wc>>8)&0xFF];
2069 return (p[wc&0xFF]);
2073 ntfs_82u(struct ntfsmount *ntmp,
2077 char *p, *outp, inbuf[3], outbuf[3];
2081 if (ntfs_iconv && ntmp->ntm_ic_l2u) {
2082 ilen = (size_t)*len;
2085 inbuf[0] = (char)(wc>>8);
2086 inbuf[1] = (char)wc;
2090 ntfs_iconv->convchr(ntmp->ntm_ic_l2u,
2091 (const char **)(void *)&p, &ilen, &outp, &olen);
2093 uc = (wchar)((outbuf[0]<<8) | (outbuf[1]&0xFF));
2098 if (ntmp->ntm_82u != NULL)
2099 return (ntmp->ntm_82u[wc&0xFF]);