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>
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 alpool = kmalloc(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 kfree(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 mfrp = kmalloc(ntfs_bntob(ntmp->ntm_bpmftrec), M_TEMP, M_WAITOK);
270 if (ip->i_number < NTFS_SYSNODESNUM) {
273 dprintf(("ntfs_loadntnode: read system node\n"));
275 bn = ntfs_cntobn(ntmp->ntm_mftcn) +
276 ntmp->ntm_bpmftrec * ip->i_number;
278 error = bread(ntmp->ntm_devvp,
279 ntfs_bntodoff(bn), ntfs_bntob(ntmp->ntm_bpmftrec), &bp);
281 kprintf("ntfs_loadntnode: BREAD FAILED\n");
285 memcpy(mfrp, bp->b_data, ntfs_bntob(ntmp->ntm_bpmftrec));
290 vp = ntmp->ntm_sysvn[NTFS_MFTINO];
291 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
292 ip->i_number * ntfs_bntob(ntmp->ntm_bpmftrec),
293 ntfs_bntob(ntmp->ntm_bpmftrec), mfrp, NULL);
295 kprintf("ntfs_loadntnode: ntfs_readattr failed\n");
300 /* Check if magic and fixups are correct */
301 error = ntfs_procfixups(ntmp, NTFS_FILEMAGIC, (caddr_t)mfrp,
302 ntfs_bntob(ntmp->ntm_bpmftrec));
304 kprintf("ntfs_loadntnode: BAD MFT RECORD %"PRId64"\n",
309 dprintf(("ntfs_loadntnode: load attrs for ino: %"PRId64"\n",ip->i_number));
310 off = mfrp->fr_attroff;
311 ap = (struct attr *) ((caddr_t)mfrp + off);
313 LIST_INIT(&ip->i_valist);
315 while (ap->a_hdr.a_type != -1) {
316 error = ntfs_attrtontvattr(ntmp, &nvap, ap);
321 LIST_INSERT_HEAD(&ip->i_valist, nvap, va_list);
323 off += ap->a_hdr.reclen;
324 ap = (struct attr *) ((caddr_t)mfrp + off);
327 kprintf("ntfs_loadntnode: failed to load attr ino: %"PRId64"\n",
332 ip->i_mainrec = mfrp->fr_mainrec;
333 ip->i_nlink = mfrp->fr_nlink;
334 ip->i_frflag = mfrp->fr_flags;
336 ip->i_flag |= IN_LOADED;
344 * Routine locks ntnode and increase usecount, just opposite of
348 ntfs_ntget(struct ntnode *ip)
350 dprintf(("ntfs_ntget: get ntnode %"PRId64": %p, usecount: %d\n",
351 ip->i_number, ip, ip->i_usecount));
353 ip->i_usecount++; /* ZZZ */
354 LOCKMGR(&ip->i_lock, LK_EXCLUSIVE);
360 * Routine search ntnode in hash, if found: lock, inc usecount and return.
361 * If not in hash allocate structure for ntnode, prefill it, lock,
362 * inc count and return.
364 * ntnode returned locked
367 ntfs_ntlookup(struct ntfsmount *ntmp, ino_t ino, struct ntnode **ipp)
371 dprintf(("ntfs_ntlookup: looking for ntnode %ju\n", (uintmax_t)ino));
374 if ((ip = ntfs_nthashlookup(ntmp->ntm_dev, ino)) != NULL) {
376 dprintf(("ntfs_ntlookup: ntnode %ju: %p, usecount: %d\n",
377 (uintmax_t)ino, ip, ip->i_usecount));
381 } while (LOCKMGR(&ntfs_hashlock, LK_EXCLUSIVE | LK_SLEEPFAIL));
383 ip = kmalloc(sizeof(struct ntnode), M_NTFSNTNODE, M_WAITOK | M_ZERO);
384 ddprintf(("ntfs_ntlookup: allocating ntnode: %d: %p\n", ino, ip));
386 /* Generic initialization */
387 ip->i_devvp = ntmp->ntm_devvp;
388 ip->i_dev = ntmp->ntm_dev;
392 LIST_INIT(&ip->i_fnlist);
395 /* init lock and lock the newborn ntnode */
396 lockinit(&ip->i_lock, "ntnode", 0, LK_EXCLUSIVE);
397 spin_init(&ip->i_interlock);
402 LOCKMGR(&ntfs_hashlock, LK_RELEASE);
406 dprintf(("ntfs_ntlookup: ntnode %ju: %p, usecount: %d\n",
407 (uintmax_t)ino, ip, ip->i_usecount));
413 * Decrement usecount of ntnode and unlock it, if usecount reach zero,
416 * ntnode should be locked on entry, and unlocked on return.
419 ntfs_ntput(struct ntnode *ip)
423 dprintf(("ntfs_ntput: rele ntnode %"PRId64": %p, usecount: %d\n",
424 ip->i_number, ip, ip->i_usecount));
426 spin_lock(&ip->i_interlock);
430 if (ip->i_usecount < 0) {
431 spin_unlock(&ip->i_interlock);
432 panic("ntfs_ntput: ino: %"PRId64" usecount: %d ",
433 ip->i_number,ip->i_usecount);
437 if (ip->i_usecount > 0) {
438 spin_unlock(&ip->i_interlock);
439 LOCKMGR(&ip->i_lock, LK_RELEASE);
443 dprintf(("ntfs_ntput: deallocating ntnode: %"PRId64"\n", ip->i_number));
445 if (ip->i_fnlist.lh_first) {
446 spin_unlock(&ip->i_interlock);
447 panic("ntfs_ntput: ntnode has fnodes");
451 * XXX this is a bit iffy because we are making high level calls
452 * while holding a spinlock.
456 while ((vap = LIST_FIRST(&ip->i_valist)) != NULL) {
457 LIST_REMOVE(vap,va_list);
458 ntfs_freentvattr(vap);
460 spin_unlock(&ip->i_interlock);
462 kfree(ip, M_NTFSNTNODE);
466 * increment usecount of ntnode
469 ntfs_ntref(struct ntnode *ip)
473 dprintf(("ntfs_ntref: ino %"PRId64", usecount: %d\n",
474 ip->i_number, ip->i_usecount));
479 * Decrement usecount of ntnode.
482 ntfs_ntrele(struct ntnode *ip)
484 dprintf(("ntfs_ntrele: rele ntnode %"PRId64": %p, usecount: %d\n",
485 ip->i_number, ip, ip->i_usecount));
487 spin_lock(&ip->i_interlock);
490 if (ip->i_usecount < 0) {
491 spin_unlock(&ip->i_interlock);
492 panic("ntfs_ntrele: ino: %"PRId64" usecount: %d ",
493 ip->i_number,ip->i_usecount);
495 spin_unlock(&ip->i_interlock);
499 * Deallocate all memory allocated for ntvattr
502 ntfs_freentvattr(struct ntvattr *vap)
504 if (vap->va_flag & NTFS_AF_INRUN) {
506 kfree(vap->va_vruncn, M_NTFSRUN);
508 kfree(vap->va_vruncl, M_NTFSRUN);
511 kfree(vap->va_datap, M_NTFSRDATA);
513 kfree(vap, M_NTFSNTVATTR);
517 * Convert disk image of attribute into ntvattr structure,
518 * runs are expanded also.
521 ntfs_attrtontvattr(struct ntfsmount *ntmp, struct ntvattr **rvapp,
530 vap = kmalloc(sizeof(struct ntvattr), M_NTFSNTVATTR,
533 vap->va_flag = rap->a_hdr.a_flag;
534 vap->va_type = rap->a_hdr.a_type;
535 vap->va_compression = rap->a_hdr.a_compression;
536 vap->va_index = rap->a_hdr.a_index;
538 ddprintf(("type: 0x%x, index: %d", vap->va_type, vap->va_index));
540 vap->va_namelen = rap->a_hdr.a_namelen;
541 if (rap->a_hdr.a_namelen) {
542 wchar *unp = (wchar *) ((caddr_t) rap + rap->a_hdr.a_nameoff);
543 ddprintf((", name:["));
544 for (i = 0; i < vap->va_namelen; i++) {
545 vap->va_name[i] = unp[i];
546 ddprintf(("%c", vap->va_name[i]));
550 if (vap->va_flag & NTFS_AF_INRUN) {
551 ddprintf((", nonres."));
552 vap->va_datalen = rap->a_nr.a_datalen;
553 vap->va_allocated = rap->a_nr.a_allocated;
554 vap->va_vcnstart = rap->a_nr.a_vcnstart;
555 vap->va_vcnend = rap->a_nr.a_vcnend;
556 vap->va_compressalg = rap->a_nr.a_compressalg;
557 error = ntfs_runtovrun(&(vap->va_vruncn), &(vap->va_vruncl),
559 (caddr_t) rap + rap->a_nr.a_dataoff);
561 vap->va_compressalg = 0;
562 ddprintf((", res."));
563 vap->va_datalen = rap->a_r.a_datalen;
564 vap->va_allocated = rap->a_r.a_datalen;
565 vap->va_vcnstart = 0;
566 vap->va_vcnend = ntfs_btocn(vap->va_allocated);
567 vap->va_datap = kmalloc(vap->va_datalen, M_NTFSRDATA,
569 memcpy(vap->va_datap, (caddr_t) rap + rap->a_r.a_dataoff,
572 ddprintf((", len: %d", vap->va_datalen));
575 kfree(vap, M_NTFSNTVATTR);
585 * Expand run into more utilizable and more memory eating format.
588 ntfs_runtovrun(cn_t **rcnp, cn_t **rclp, u_long *rcntp, u_int8_t *run)
602 off += (run[off] & 0xF) + ((run[off] >> 4) & 0xF) + 1;
605 cn = kmalloc(cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
606 cl = kmalloc(cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
616 for (i = 0; i < (sz & 0xF); i++)
617 cl[cnt] += (u_int32_t) run[off++] << (i << 3);
620 if (run[off + sz - 1] & 0x80) {
621 tmp = ((u_int64_t) - 1) << (sz << 3);
622 for (i = 0; i < sz; i++)
623 tmp |= (u_int64_t) run[off++] << (i << 3);
626 for (i = 0; i < sz; i++)
627 tmp |= (u_int64_t) run[off++] << (i << 3);
630 prev = cn[cnt] = prev + tmp;
643 * Compare unicode and ascii string case insens.
646 ntfs_uastricmp(struct ntfsmount *ntmp, const wchar *ustr, size_t ustrlen,
647 const char *astr, size_t astrlen)
650 size_t i, j, mbstrlen = astrlen;
654 if (ntmp->ntm_ic_l2u) {
655 for (i = 0, j = 0; i < ustrlen && j < astrlen; i++, j++) {
656 if (j < astrlen -1) {
657 wc = (wchar)astr[j]<<8 | (astr[j+1]&0xFF);
660 wc = (wchar)astr[j]<<8 & 0xFF00;
663 res = ((int) NTFS_TOUPPER(ustr[i])) -
664 ((int)NTFS_TOUPPER(NTFS_82U(wc, &len)));
673 * We use NTFS_82U(NTFS_U28(c)) to get rid of unicode
674 * symbols not covered by translation table
676 for (i = 0; i < ustrlen && i < astrlen; i++) {
677 res = ((int) NTFS_TOUPPER(NTFS_82U(NTFS_U28(ustr[i]), &len))) -
678 ((int)NTFS_TOUPPER(NTFS_82U((wchar)astr[i], &len)));
683 return (ustrlen - mbstrlen);
687 * Compare unicode and ascii string case sens.
690 ntfs_uastrcmp(struct ntfsmount *ntmp, const wchar *ustr, size_t ustrlen,
691 const char *astr, size_t astrlen)
694 size_t i, j, mbstrlen = astrlen;
698 for (i = 0, j = 0; (i < ustrlen) && (j < astrlen); i++, j++) {
700 wc = NTFS_U28(ustr[i]);
703 if (u != '\0' && j < astrlen -1) {
704 res = (int) (u - astr[j++]);
707 res = (res<<8) + (int) (l - astr[j]);
711 return (ustrlen - mbstrlen);
715 * Search fnode in ntnode, if not found allocate and preinitialize.
717 * ntnode should be locked on entry.
720 ntfs_fget(struct ntfsmount *ntmp, struct ntnode *ip, int attrtype,
721 char *attrname, struct fnode **fpp)
725 dprintf(("ntfs_fget: ino: %"PRId64", attrtype: 0x%x, attrname: %s\n",
726 ip->i_number,attrtype, attrname?attrname:""));
728 for (fp = ip->i_fnlist.lh_first; fp != NULL; fp = fp->f_fnlist.le_next){
729 dprintf(("ntfs_fget: fnode: attrtype: %d, attrname: %s\n",
730 fp->f_attrtype, fp->f_attrname?fp->f_attrname:""));
732 if ((attrtype == fp->f_attrtype) &&
733 ((!attrname && !fp->f_attrname) ||
734 (attrname && fp->f_attrname &&
735 !strcmp(attrname,fp->f_attrname)))){
736 dprintf(("ntfs_fget: found existed: %p\n",fp));
744 fp = kmalloc(sizeof(struct fnode), M_NTFSFNODE, M_WAITOK | M_ZERO);
745 dprintf(("ntfs_fget: allocating fnode: %p\n",fp));
749 fp->f_flag |= FN_AATTRNAME;
750 fp->f_attrname = kmalloc(strlen(attrname) + 1, M_TEMP,
752 strcpy(fp->f_attrname, attrname);
754 fp->f_attrname = NULL;
755 fp->f_attrtype = attrtype;
759 LIST_INSERT_HEAD(&ip->i_fnlist, fp, f_fnlist);
767 * Deallocate fnode, remove it from ntnode's fnode list.
769 * ntnode should be locked.
772 ntfs_frele(struct fnode *fp)
774 struct ntnode *ip = FTONT(fp);
776 dprintf(("ntfs_frele: fnode: %p for %"PRId64": %p\n", fp, ip->i_number, ip));
778 dprintf(("ntfs_frele: deallocating fnode\n"));
779 LIST_REMOVE(fp,f_fnlist);
780 if (fp->f_flag & FN_AATTRNAME)
781 kfree(fp->f_attrname, M_TEMP);
783 kfree(fp->f_dirblbuf, M_NTFSDIR);
784 kfree(fp, M_NTFSFNODE);
789 * Lookup attribute name in format: [[:$ATTR_TYPE]:$ATTR_NAME],
790 * $ATTR_TYPE is searched in attrdefs read from $AttrDefs.
791 * If $ATTR_TYPE nott specifed, ATTR_A_DATA assumed.
794 ntfs_ntlookupattr(struct ntfsmount *ntmp, const char *name, int namelen,
795 int *attrtype, char **attrname)
799 struct ntvattrdef *adp;
804 if (name[0] == '$') {
806 for (syslen = 0; syslen < namelen; syslen++) {
807 if(sys[syslen] == ':') {
817 for (i = 0; i < ntmp->ntm_adnum; i++, adp++){
818 if (syslen != adp->ad_namelen ||
819 strncmp(sys, adp->ad_name, syslen) != 0)
822 *attrtype = adp->ad_type;
827 *attrtype = NTFS_A_DATA;
831 (*attrname) = kmalloc(namelen, M_TEMP, M_WAITOK);
832 memcpy((*attrname), name, namelen);
833 (*attrname)[namelen] = '\0';
840 * Lookup specifed node for filename, matching cnp,
841 * return fnode filled.
844 ntfs_ntlookupfile(struct ntfsmount *ntmp, struct vnode *vp,
845 struct componentname *cnp, struct vnode **vpp)
847 struct fnode *fp = VTOF(vp);
848 struct ntnode *ip = FTONT(fp);
849 struct ntvattr *vap; /* Root attribute */
850 cn_t cn; /* VCN in current attribute */
851 caddr_t rdbuf; /* Buffer to read directory's blocks */
853 u_int32_t rdsize; /* Length of data to read from current block */
854 struct attr_indexentry *iep;
855 int error, res, anamelen, fnamelen;
856 const char *fname,*aname;
858 int attrtype = NTFS_A_DATA;
859 char *attrname = NULL;
864 error = ntfs_ntget(ip);
868 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
869 if (error || (vap->va_flag & NTFS_AF_INRUN))
872 blsize = vap->va_a_iroot->ir_size;
873 rdsize = vap->va_datalen;
876 * Divide file name into: foofilefoofilefoofile[:attrspec]
877 * Store like this: fname:fnamelen [aname:anamelen]
879 fname = cnp->cn_nameptr;
882 for (fnamelen = 0; fnamelen < cnp->cn_namelen; fnamelen++)
883 if(fname[fnamelen] == ':') {
884 aname = fname + fnamelen + 1;
885 anamelen = cnp->cn_namelen - fnamelen - 1;
886 dprintf(("ntfs_ntlookupfile: %s (%d), attr: %s (%d)\n",
887 fname, fnamelen, aname, anamelen));
891 dprintf(("ntfs_ntlookupfile: blksz: %d, rdsz: %d\n", blsize, rdsize));
893 rdbuf = kmalloc(blsize, M_TEMP, M_WAITOK);
895 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXROOT, "$I30",
896 0, rdsize, rdbuf, NULL);
900 aoff = sizeof(struct attr_indexroot);
903 iep = (struct attr_indexentry *) (rdbuf + aoff);
905 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
907 iep = (struct attr_indexentry *) (rdbuf + aoff))
909 ddprintf(("scan: %d, %d\n",
910 (u_int32_t) iep->ie_number,
911 (u_int32_t) iep->ie_fnametype));
913 /* check the name - the case-insensitible check
914 * has to come first, to break from this for loop
915 * if needed, so we can dive correctly */
916 res = NTFS_UASTRICMP(iep->ie_fname, iep->ie_fnamelen,
919 if (res < 0) continue;
921 if (iep->ie_fnametype == 0 ||
922 !(ntmp->ntm_flag & NTFS_MFLAG_CASEINS))
924 res = NTFS_UASTRCMP(iep->ie_fname,
925 iep->ie_fnamelen, fname, fnamelen);
926 if (res != 0) continue;
930 error = ntfs_ntlookupattr(ntmp,
932 &attrtype, &attrname);
937 /* Check if we've found ourself */
938 if ((iep->ie_number == ip->i_number) &&
939 (attrtype == fp->f_attrtype) &&
940 ((!attrname && !fp->f_attrname) ||
941 (attrname && fp->f_attrname &&
942 !strcmp(attrname, fp->f_attrname))))
950 /* vget node, but don't load it */
951 error = ntfs_vgetex(ntmp->ntm_mountp,
952 iep->ie_number, attrtype, attrname,
953 LK_EXCLUSIVE, VG_DONTLOADIN | VG_DONTVALIDFN,
956 /* free the buffer returned by ntfs_ntlookupattr() */
958 kfree(attrname, M_TEMP);
967 if (nfp->f_flag & FN_VALID) {
972 nfp->f_fflag = iep->ie_fflag;
973 nfp->f_pnumber = iep->ie_fpnumber;
974 nfp->f_times = iep->ie_ftimes;
976 if((nfp->f_fflag & NTFS_FFLAG_DIR) &&
977 (nfp->f_attrtype == NTFS_A_DATA) &&
978 (nfp->f_attrname == NULL))
983 nvp->v_type = f_type;
985 if ((nfp->f_attrtype == NTFS_A_DATA) &&
986 (nfp->f_attrname == NULL))
988 /* Opening default attribute */
989 nfp->f_size = iep->ie_fsize;
990 nfp->f_allocated = iep->ie_fallocated;
991 nfp->f_flag |= FN_PRELOADED;
993 error = ntfs_filesize(ntmp, nfp,
994 &nfp->f_size, &nfp->f_allocated);
1000 nfp->f_flag &= ~FN_VALID;
1003 * Normal files use the buffer cache
1005 if (nvp->v_type == VREG)
1006 vinitvmio(nvp, nfp->f_size, PAGE_SIZE, -1);
1011 /* Dive if possible */
1012 if (iep->ie_flag & NTFS_IEFLAG_SUBNODE) {
1013 dprintf(("ntfs_ntlookupfile: diving\n"));
1015 cn = *(cn_t *) (rdbuf + aoff +
1016 iep->reclen - sizeof(cn_t));
1019 error = ntfs_readattr(ntmp, ip, NTFS_A_INDX, "$I30",
1020 ntfs_cntob(cn), rdsize, rdbuf, NULL);
1024 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1029 aoff = (((struct attr_indexalloc *) rdbuf)->ia_hdrsize +
1032 dprintf(("ntfs_ntlookupfile: nowhere to dive :-(\n"));
1038 dprintf(("finish\n"));
1041 if (attrname) kfree(attrname, M_TEMP);
1042 ntfs_ntvattrrele(vap);
1044 kfree(rdbuf, M_TEMP);
1049 * Check if name type is permitted to show.
1052 ntfs_isnamepermitted(struct ntfsmount *ntmp, struct attr_indexentry *iep)
1054 if (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)
1057 switch (iep->ie_fnametype) {
1059 ddprintf(("ntfs_isnamepermitted: skipped DOS name\n"));
1061 case 0: case 1: case 3:
1064 kprintf("ntfs_isnamepermitted: " \
1065 "WARNING! Unknown file name type: %d\n",
1073 * Read ntfs dir like stream of attr_indexentry, not like btree of them.
1074 * This is done by scaning $BITMAP:$I30 for busy clusters and reading them.
1075 * Ofcouse $INDEX_ROOT:$I30 is read before. Last read values are stored in
1076 * fnode, so we can skip toward record number num almost immediatly.
1077 * Anyway this is rather slow routine. The problem is that we don't know
1078 * how many records are there in $INDEX_ALLOCATION:$I30 block.
1081 ntfs_ntreaddir(struct ntfsmount *ntmp, struct fnode *fp,
1082 u_int32_t num, struct attr_indexentry **riepp)
1084 struct ntnode *ip = FTONT(fp);
1085 struct ntvattr *vap = NULL; /* IndexRoot attribute */
1086 struct ntvattr *bmvap = NULL; /* BitMap attribute */
1087 struct ntvattr *iavap = NULL; /* IndexAllocation attribute */
1088 caddr_t rdbuf; /* Buffer to read directory's blocks */
1089 u_char *bmp = NULL; /* Bitmap */
1090 u_int32_t blsize; /* Index allocation size (2048) */
1091 u_int32_t rdsize; /* Length of data to read */
1092 u_int32_t attrnum; /* Current attribute type */
1093 u_int32_t cpbl = 1; /* Clusters per directory block */
1095 struct attr_indexentry *iep;
1097 u_int32_t aoff, cnum;
1099 dprintf(("ntfs_ntreaddir: read ino: %"PRId64", num: %d\n", ip->i_number, num));
1100 error = ntfs_ntget(ip);
1104 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
1108 if (fp->f_dirblbuf == NULL) {
1109 fp->f_dirblsz = vap->va_a_iroot->ir_size;
1110 fp->f_dirblbuf = kmalloc(max(vap->va_datalen, fp->f_dirblsz),
1111 M_NTFSDIR, M_WAITOK);
1114 blsize = fp->f_dirblsz;
1115 rdbuf = fp->f_dirblbuf;
1117 dprintf(("ntfs_ntreaddir: rdbuf: 0x%p, blsize: %d\n", rdbuf, blsize));
1119 if (vap->va_a_iroot->ir_flag & NTFS_IRFLAG_INDXALLOC) {
1120 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXBITMAP, "$I30",
1126 bmp = kmalloc(bmvap->va_datalen, M_TEMP, M_WAITOK);
1127 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXBITMAP, "$I30", 0,
1128 bmvap->va_datalen, bmp, NULL);
1132 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDX, "$I30",
1138 cpbl = ntfs_btocn(blsize + ntfs_cntob(1) - 1);
1139 dprintf(("ntfs_ntreaddir: indexalloc: %d, cpbl: %d\n",
1140 iavap->va_datalen, cpbl));
1142 dprintf(("ntfs_ntreadidir: w/o BitMap and IndexAllocation\n"));
1143 iavap = bmvap = NULL;
1147 /* Try use previous values */
1148 if ((fp->f_lastdnum < num) && (fp->f_lastdnum != 0)) {
1149 attrnum = fp->f_lastdattr;
1150 aoff = fp->f_lastdoff;
1151 blnum = fp->f_lastdblnum;
1152 cnum = fp->f_lastdnum;
1154 attrnum = NTFS_A_INDXROOT;
1155 aoff = sizeof(struct attr_indexroot);
1161 dprintf(("ntfs_ntreaddir: scan: 0x%x, %d, %d, %d, %d\n",
1162 attrnum, blnum, cnum, num, aoff));
1163 rdsize = (attrnum == NTFS_A_INDXROOT) ? vap->va_datalen : blsize;
1164 error = ntfs_readattr(ntmp, ip, attrnum, "$I30",
1165 ntfs_cntob(blnum * cpbl), rdsize, rdbuf, NULL);
1169 if (attrnum == NTFS_A_INDX) {
1170 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1176 aoff = (attrnum == NTFS_A_INDX) ?
1177 (0x18 + ((struct attr_indexalloc *) rdbuf)->ia_hdrsize) :
1178 sizeof(struct attr_indexroot);
1180 iep = (struct attr_indexentry *) (rdbuf + aoff);
1181 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
1182 aoff += iep->reclen,
1183 iep = (struct attr_indexentry *) (rdbuf + aoff))
1185 if (!ntfs_isnamepermitted(ntmp, iep)) continue;
1188 fp->f_lastdnum = cnum;
1189 fp->f_lastdoff = aoff;
1190 fp->f_lastdblnum = blnum;
1191 fp->f_lastdattr = attrnum;
1202 if (attrnum == NTFS_A_INDXROOT)
1207 while (ntfs_cntob(blnum * cpbl) < iavap->va_datalen) {
1208 if (bmp[blnum >> 3] & (1 << (blnum & 3)))
1213 attrnum = NTFS_A_INDX;
1215 if (ntfs_cntob(blnum * cpbl) >= iavap->va_datalen)
1217 dprintf(("ntfs_ntreaddir: blnum: %d\n", blnum));
1226 ntfs_ntvattrrele(vap);
1228 ntfs_ntvattrrele(bmvap);
1230 ntfs_ntvattrrele(iavap);
1238 * Convert NTFS times that are in 100 ns units and begins from
1239 * 1601 Jan 1 into unix times.
1242 ntfs_nttimetounix(u_int64_t nt)
1246 /* WindowNT times are in 100 ns and from 1601 Jan 1 */
1247 t.tv_nsec = (nt % (1000 * 1000 * 10)) * 100;
1248 t.tv_sec = nt / (1000 * 1000 * 10) -
1249 369LL * 365LL * 24LL * 60LL * 60LL -
1250 89LL * 1LL * 24LL * 60LL * 60LL;
1255 * Get file times from NTFS_A_NAME attribute.
1258 ntfs_times(struct ntfsmount *ntmp, struct ntnode *ip, ntfs_times_t *tm)
1260 struct ntvattr *vap;
1263 dprintf(("ntfs_times: ino: %"PRId64"...\n", ip->i_number));
1265 error = ntfs_ntget(ip);
1269 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_NAME, NULL, 0, &vap);
1274 *tm = vap->va_a_name->n_times;
1275 ntfs_ntvattrrele(vap);
1282 * Get file sizes from corresponding attribute.
1284 * ntnode under fnode should be locked.
1287 ntfs_filesize(struct ntfsmount *ntmp, struct fnode *fp, u_int64_t *size,
1290 struct ntvattr *vap;
1291 struct ntnode *ip = FTONT(fp);
1295 dprintf(("ntfs_filesize: ino: %"PRId64"\n", ip->i_number));
1297 error = ntfs_ntvattrget(ntmp, ip,
1298 fp->f_attrtype, fp->f_attrname, 0, &vap);
1302 bn = vap->va_allocated;
1303 sz = vap->va_datalen;
1305 dprintf(("ntfs_filesize: %d bytes (%d bytes allocated)\n",
1306 (u_int32_t) sz, (u_int32_t) bn));
1313 ntfs_ntvattrrele(vap);
1319 * This is one of write routine.
1322 ntfs_writeattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1323 u_int32_t attrnum, char *attrname, off_t roff,
1324 size_t rsize, void *rdata, size_t *initp,
1329 off_t off = roff, left = rsize, towrite;
1330 caddr_t data = rdata;
1331 struct ntvattr *vap;
1335 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1336 ntfs_btocn(off), &vap);
1339 towrite = min(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1340 ddprintf(("ntfs_writeattr_plain: o: %d, s: %d (%d - %d)\n",
1341 (u_int32_t) off, (u_int32_t) towrite,
1342 (u_int32_t) vap->va_vcnstart,
1343 (u_int32_t) vap->va_vcnend));
1344 error = ntfs_writentvattr_plain(ntmp, ip, vap,
1345 off - ntfs_cntob(vap->va_vcnstart),
1346 towrite, data, &init, uio);
1348 kprintf("ntfs_writeattr_plain: " \
1349 "ntfs_writentvattr_plain failed: o: %d, s: %d\n",
1350 (u_int32_t) off, (u_int32_t) towrite);
1351 kprintf("ntfs_writeattr_plain: attrib: %d - %d\n",
1352 (u_int32_t) vap->va_vcnstart,
1353 (u_int32_t) vap->va_vcnend);
1354 ntfs_ntvattrrele(vap);
1357 ntfs_ntvattrrele(vap);
1360 data = data + towrite;
1368 * This is one of write routine.
1370 * ntnode should be locked.
1373 ntfs_writentvattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1374 struct ntvattr *vap, off_t roff, size_t rsize,
1375 void *rdata, size_t *initp, struct uio *uio)
1380 cn_t ccn, ccl, cn, left, cl;
1381 caddr_t data = rdata;
1387 if ((vap->va_flag & NTFS_AF_INRUN) == 0) {
1388 kprintf("ntfs_writevattr_plain: CAN'T WRITE RES. ATTRIBUTE\n");
1392 ddprintf(("ntfs_writentvattr_plain: data in run: %ld chains\n",
1400 for (; left && (cnt < vap->va_vruncnt); cnt++) {
1401 ccn = vap->va_vruncn[cnt];
1402 ccl = vap->va_vruncl[cnt];
1404 ddprintf(("ntfs_writentvattr_plain: " \
1405 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1406 (u_int32_t) left, (u_int32_t) ccn, \
1407 (u_int32_t) ccl, (u_int32_t) off));
1409 if (ntfs_cntob(ccl) < off) {
1410 off -= ntfs_cntob(ccl);
1414 if (!ccn && ip->i_number != NTFS_BOOTINO)
1417 ccl -= ntfs_btocn(off);
1418 cn = ccn + ntfs_btocn(off);
1419 off = ntfs_btocnoff(off);
1421 while (left && ccl) {
1422 #if defined(__DragonFly__)
1424 min(ntfs_cntob(ccl) - off, MAXBSIZE - off));
1426 /* under NetBSD, bread() can read
1427 * maximum one block worth of data */
1428 tocopy = min(left, ntmp->ntm_bps - off);
1430 cl = ntfs_btocl(tocopy + off);
1431 ddprintf(("ntfs_writentvattr_plain: write: " \
1432 "cn: 0x%x cl: %d, off: %d len: %d, left: %d\n",
1433 (u_int32_t) cn, (u_int32_t) cl,
1434 (u_int32_t) off, (u_int32_t) tocopy,
1436 if (off == 0 && tocopy == ntfs_cntob(cl) &&
1437 uio->uio_segflg != UIO_NOCOPY) {
1438 bp = getblk(ntmp->ntm_devvp, ntfs_cntodoff(cn),
1439 ntfs_cntob(cl), 0, 0);
1442 error = bread(ntmp->ntm_devvp,
1444 ntfs_cntob(cl), &bp);
1451 uiomovebp(bp, bp->b_data + off, tocopy, uio);
1453 memcpy(bp->b_data + off, data, tocopy);
1455 data = data + tocopy;
1465 kprintf("ntfs_writentvattr_plain: POSSIBLE RUN ERROR\n");
1473 * This is one of read routines.
1475 * ntnode should be locked.
1478 ntfs_readntvattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1479 struct ntvattr *vap, off_t roff, size_t rsize,
1480 void *rdata, size_t *initp, struct uio *uio)
1486 if (vap->va_flag & NTFS_AF_INRUN) {
1488 cn_t ccn, ccl, cn, left, cl;
1489 caddr_t data = rdata;
1493 ddprintf(("ntfs_readntvattr_plain: data in run: %ld chains\n",
1501 while (left && (cnt < vap->va_vruncnt)) {
1502 ccn = vap->va_vruncn[cnt];
1503 ccl = vap->va_vruncl[cnt];
1505 ddprintf(("ntfs_readntvattr_plain: " \
1506 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1507 (u_int32_t) left, (u_int32_t) ccn, \
1508 (u_int32_t) ccl, (u_int32_t) off));
1510 if (ntfs_cntob(ccl) < off) {
1511 off -= ntfs_cntob(ccl);
1515 if (ccn || ip->i_number == NTFS_BOOTINO) {
1516 ccl -= ntfs_btocn(off);
1517 cn = ccn + ntfs_btocn(off);
1518 off = ntfs_btocnoff(off);
1520 while (left && ccl) {
1521 #if defined(__DragonFly__)
1523 min(ntfs_cntob(ccl) - off,
1526 /* under NetBSD, bread() can read
1527 * maximum one block worth of data */
1529 ntmp->ntm_bps - off);
1531 cl = ntfs_btocl(tocopy + off);
1532 ddprintf(("ntfs_readntvattr_plain: " \
1533 "read: cn: 0x%x cl: %d, " \
1534 "off: %d len: %d, left: %d\n",
1540 error = bread(ntmp->ntm_devvp,
1549 uiomovebp(bp, bp->b_data + off,
1552 memcpy(data, bp->b_data + off,
1556 data = data + tocopy;
1564 tocopy = min(left, ntfs_cntob(ccl) - off);
1565 ddprintf(("ntfs_readntvattr_plain: "
1566 "hole: ccn: 0x%x ccl: %d, off: %d, " \
1567 " len: %d, left: %d\n",
1568 (u_int32_t) ccn, (u_int32_t) ccl,
1569 (u_int32_t) off, (u_int32_t) tocopy,
1574 size_t remains = tocopy;
1575 for(; remains; remains++)
1576 uiomove("", 1, uio);
1578 bzero(data, tocopy);
1579 data = data + tocopy;
1584 kprintf("ntfs_readntvattr_plain: POSSIBLE RUN ERROR\n");
1588 ddprintf(("ntfs_readnvattr_plain: data is in mft record\n"));
1590 uiomove(vap->va_datap + roff, rsize, uio);
1592 memcpy(rdata, vap->va_datap + roff, rsize);
1600 * This is one of read routines.
1603 ntfs_readattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1604 u_int32_t attrnum, char *attrname, off_t roff,
1605 size_t rsize, void *rdata, size_t * initp,
1610 off_t off = roff, left = rsize, toread;
1611 caddr_t data = rdata;
1612 struct ntvattr *vap;
1616 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1617 ntfs_btocn(off), &vap);
1620 toread = min(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1621 ddprintf(("ntfs_readattr_plain: o: %d, s: %d (%d - %d)\n",
1622 (u_int32_t) off, (u_int32_t) toread,
1623 (u_int32_t) vap->va_vcnstart,
1624 (u_int32_t) vap->va_vcnend));
1625 error = ntfs_readntvattr_plain(ntmp, ip, vap,
1626 off - ntfs_cntob(vap->va_vcnstart),
1627 toread, data, &init, uio);
1629 kprintf("ntfs_readattr_plain: " \
1630 "ntfs_readntvattr_plain failed: o: %d, s: %d\n",
1631 (u_int32_t) off, (u_int32_t) toread);
1632 kprintf("ntfs_readattr_plain: attrib: %d - %d\n",
1633 (u_int32_t) vap->va_vcnstart,
1634 (u_int32_t) vap->va_vcnend);
1635 ntfs_ntvattrrele(vap);
1638 ntfs_ntvattrrele(vap);
1641 data = data + toread;
1649 * This is one of read routines.
1652 ntfs_readattr(struct ntfsmount *ntmp, struct ntnode *ip, u_int32_t attrnum,
1653 char *attrname, off_t roff, size_t rsize, void *rdata,
1657 struct ntvattr *vap;
1660 ddprintf(("ntfs_readattr: reading %"PRId64": 0x%x, from %d size %d bytes\n",
1661 ip->i_number, attrnum, (u_int32_t) roff, (u_int32_t) rsize));
1663 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname, 0, &vap);
1667 if ((roff > vap->va_datalen) ||
1668 (roff + rsize > vap->va_datalen)) {
1669 ddprintf(("ntfs_readattr: offset too big\n"));
1670 ntfs_ntvattrrele(vap);
1673 if (vap->va_compression && vap->va_compressalg) {
1676 off_t off, left = rsize, tocopy;
1677 caddr_t data = rdata;
1680 ddprintf(("ntfs_ntreadattr: compression: %d\n",
1681 vap->va_compressalg));
1683 cup = kmalloc(ntfs_cntob(NTFS_COMPUNIT_CL), M_NTFSDECOMP,
1685 uup = kmalloc(ntfs_cntob(NTFS_COMPUNIT_CL), M_NTFSDECOMP,
1688 cn = (ntfs_btocn(roff)) & (~(NTFS_COMPUNIT_CL - 1));
1689 off = roff - ntfs_cntob(cn);
1692 error = ntfs_readattr_plain(ntmp, ip, attrnum,
1693 attrname, ntfs_cntob(cn),
1694 ntfs_cntob(NTFS_COMPUNIT_CL),
1699 tocopy = min(left, ntfs_cntob(NTFS_COMPUNIT_CL) - off);
1701 if (init == ntfs_cntob(NTFS_COMPUNIT_CL)) {
1703 uiomove(cup + off, tocopy, uio);
1705 memcpy(data, cup + off, tocopy);
1706 } else if (init == 0) {
1708 size_t remains = tocopy;
1709 for(; remains; remains--)
1710 uiomove("", 1, uio);
1713 bzero(data, tocopy);
1715 error = ntfs_uncompunit(ntmp, uup, cup);
1719 uiomove(uup + off, tocopy, uio);
1721 memcpy(data, uup + off, tocopy);
1725 data = data + tocopy;
1726 off += tocopy - ntfs_cntob(NTFS_COMPUNIT_CL);
1727 cn += NTFS_COMPUNIT_CL;
1730 kfree(uup, M_NTFSDECOMP);
1731 kfree(cup, M_NTFSDECOMP);
1733 error = ntfs_readattr_plain(ntmp, ip, attrnum, attrname,
1734 roff, rsize, rdata, &init, uio);
1735 ntfs_ntvattrrele(vap);
1741 ntfs_parserun(cn_t *cn, cn_t *cl, u_int8_t *run, u_long len, u_long *off)
1747 kprintf("ntfs_parsetun: run == NULL\n");
1752 kprintf("ntfs_parserun: trying to go out of run\n");
1756 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1757 kprintf("ntfs_parserun: " \
1758 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1762 for (i = 0; i < (sz & 0xF); i++)
1763 *cl += (u_int32_t) run[(*off)++] << (i << 3);
1766 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1767 kprintf("ntfs_parserun: " \
1768 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1772 for (i = 0; i < (sz & 0xF); i++)
1773 *cn += (u_int32_t) run[(*off)++] << (i << 3);
1780 * Process fixup routine on given buffer.
1783 ntfs_procfixups(struct ntfsmount *ntmp, u_int32_t magic, caddr_t buf,
1786 struct fixuphdr *fhp = (struct fixuphdr *) buf;
1792 if (fhp->fh_magic != magic) {
1793 kprintf("ntfs_procfixups: magic doesn't match: %08x != %08x\n",
1794 fhp->fh_magic, magic);
1797 if ((fhp->fh_fnum - 1) * ntmp->ntm_bps != len) {
1798 kprintf("ntfs_procfixups: " \
1799 "bad fixups number: %d for %ld bytes block\n",
1800 fhp->fh_fnum, (long)len); /* XXX kprintf kludge */
1803 if (fhp->fh_foff >= ntmp->ntm_spc * ntmp->ntm_mftrecsz * ntmp->ntm_bps) {
1804 kprintf("ntfs_procfixups: invalid offset: %x", fhp->fh_foff);
1807 fxp = (u_int16_t *) (buf + fhp->fh_foff);
1808 cfxp = (u_int16_t *) (buf + ntmp->ntm_bps - 2);
1810 for (i = 1; i < fhp->fh_fnum; i++, fxp++) {
1811 if (*cfxp != fixup) {
1812 kprintf("ntfs_procfixups: fixup %d doesn't match\n", i);
1816 cfxp = (u_int16_t *)(((caddr_t) cfxp) + ntmp->ntm_bps);
1823 ntfs_runtocn(cn_t *cn, struct ntfsmount *ntmp, u_int8_t *run, u_long len,
1833 kprintf("ntfs_runtocn: run: 0x%p, %ld bytes, vcn:%ld\n",
1834 run, len, (u_long) vcn);
1835 kprintf("ntfs_runtocn: run: ");
1836 for (i = 0; i < len; i++)
1837 kprintf("0x%02x ", run[i]);
1842 kprintf("ntfs_runtocn: run == NULL\n");
1846 if (run[off] == 0) {
1847 kprintf("ntfs_runtocn: vcn too big\n");
1851 error = ntfs_parserun(&ccn, &ccl, run, len, &off);
1853 kprintf("ntfs_runtocn: ntfs_parserun failed\n");
1856 } while (ccl <= vcn);
1863 * this initializes toupper table & dependant variables to be ready for
1867 ntfs_toupper_init(void)
1869 ntfs_toupper_tab = NULL;
1870 lockinit(&ntfs_toupper_lock, "ntfs_toupper", 0, 0);
1871 ntfs_toupper_usecount = 0;
1875 * if the ntfs_toupper_tab[] is filled already, just raise use count;
1876 * otherwise read the data from the filesystem we are currently mounting
1879 ntfs_toupper_use(struct mount *mp, struct ntfsmount *ntmp)
1884 /* get exclusive access */
1885 LOCKMGR(&ntfs_toupper_lock, LK_EXCLUSIVE);
1887 /* only read the translation data from a file if it hasn't been
1889 if (ntfs_toupper_tab)
1893 * Read in Unicode lowercase -> uppercase translation file.
1894 * XXX for now, just the first 256 entries are used anyway,
1895 * so don't bother reading more
1897 ntfs_toupper_tab = kmalloc(65536 * sizeof(wchar), M_NTFSRDATA,
1900 if ((error = VFS_VGET(mp, NULL, NTFS_UPCASEINO, &vp)))
1902 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
1903 0, 65536*sizeof(wchar), (char *) ntfs_toupper_tab, NULL);
1907 ntfs_toupper_usecount++;
1908 LOCKMGR(&ntfs_toupper_lock, LK_RELEASE);
1913 * lower the use count and if it reaches zero, free the memory
1914 * tied by toupper table
1917 ntfs_toupper_unuse(void)
1919 /* get exclusive access */
1920 LOCKMGR(&ntfs_toupper_lock, LK_EXCLUSIVE);
1922 ntfs_toupper_usecount--;
1923 if (ntfs_toupper_usecount == 0) {
1924 kfree(ntfs_toupper_tab, M_NTFSRDATA);
1925 ntfs_toupper_tab = NULL;
1928 else if (ntfs_toupper_usecount < 0) {
1929 panic("ntfs_toupper_unuse(): use count negative: %d",
1930 ntfs_toupper_usecount);
1934 /* release the lock */
1935 LOCKMGR(&ntfs_toupper_lock, LK_RELEASE);
1939 ntfs_u28_init(struct ntfsmount *ntmp, wchar *u2w, char *cs_local,
1945 if (ntfs_iconv && cs_local) {
1946 ntfs_iconv->open(cs_local, cs_ntfs, &ntmp->ntm_ic_u2l);
1950 u28 = kmalloc(256 * sizeof(char *), M_TEMP, M_WAITOK | M_ZERO);
1952 for (i=0; i<256; i++) {
1953 h = (u2w[i] >> 8) & 0xFF;
1956 if (u28[h] == NULL) {
1957 u28[h] = kmalloc(256 * sizeof(char), M_TEMP, M_WAITOK);
1958 for (j=0; j<256; j++)
1962 u28[h][l] = i & 0xFF;
1965 ntmp->ntm_u28 = u28;
1971 ntfs_u28_uninit(struct ntfsmount *ntmp)
1976 if (ntmp->ntm_u28 == NULL) {
1977 if (ntfs_iconv && ntmp->ntm_ic_u2l) {
1978 ntfs_iconv->close(ntmp->ntm_ic_u2l);
1983 if (ntmp->ntm_u28 == NULL)
1986 u28 = ntmp->ntm_u28;
1988 for (i=0; i<256; i++)
1990 kfree(u28[i], M_TEMP);
1998 ntfs_82u_init(struct ntfsmount *ntmp, char *cs_local, char *cs_ntfs)
2004 if (ntfs_iconv && cs_local) {
2005 ntfs_iconv->open(cs_ntfs, cs_local, &ntmp->ntm_ic_l2u);
2009 _82u = kmalloc(256 * sizeof(wchar), M_TEMP, M_WAITOK);
2011 for (i=0; i<256; i++)
2014 ntmp->ntm_82u = _82u;
2020 ntfs_82u_uninit(struct ntfsmount *ntmp)
2022 if (ntmp->ntm_82u == NULL) {
2023 if (ntfs_iconv && ntmp->ntm_ic_l2u) {
2024 ntfs_iconv->close(ntmp->ntm_ic_l2u);
2029 kfree(ntmp->ntm_82u, M_TEMP);
2034 * maps the Unicode char to 8bit equivalent
2035 * XXX currently only gets lower 8bit from the Unicode char
2036 * and substitutes a '_' for it if the result would be '\0';
2037 * something better has to be definitely though out
2040 ntfs_u28(struct ntfsmount *ntmp, wchar wc)
2042 char *p, *outp, inbuf[3], outbuf[3];
2045 if (ntfs_iconv && ntmp->ntm_ic_u2l) {
2047 inbuf[0] = (char)(wc>>8);
2048 inbuf[1] = (char)wc;
2052 ntfs_iconv->convchr(ntmp->ntm_ic_u2l,
2053 (const char **)(void *)&p, &ilen, &outp, &olen);
2055 return ((wchar)(outbuf[0]&0xFF));
2056 } else if (olen == 0) {
2057 return ((wchar)((outbuf[0]<<8) | (outbuf[1]&0xFF)));
2062 p = ntmp->ntm_u28[(wc>>8)&0xFF];
2065 return (p[wc&0xFF]);
2069 ntfs_82u(struct ntfsmount *ntmp,
2073 char *p, *outp, inbuf[3], outbuf[3];
2077 if (ntfs_iconv && ntmp->ntm_ic_l2u) {
2078 ilen = (size_t)*len;
2081 inbuf[0] = (char)(wc>>8);
2082 inbuf[1] = (char)wc;
2086 ntfs_iconv->convchr(ntmp->ntm_ic_l2u,
2087 (const char **)(void *)&p, &ilen, &outp, &olen);
2089 uc = (wchar)((outbuf[0]<<8) | (outbuf[1]&0xFF));
2094 if (ntmp->ntm_82u != NULL)
2095 return (ntmp->ntm_82u[wc&0xFF]);