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 alpool = kmalloc(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 kfree(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 mfrp = kmalloc(ntfs_bntob(ntmp->ntm_bpmftrec), M_TEMP, M_WAITOK);
271 if (ip->i_number < NTFS_SYSNODESNUM) {
274 dprintf(("ntfs_loadntnode: read system node\n"));
276 bn = ntfs_cntobn(ntmp->ntm_mftcn) +
277 ntmp->ntm_bpmftrec * ip->i_number;
279 error = bread(ntmp->ntm_devvp,
280 ntfs_bntodoff(bn), ntfs_bntob(ntmp->ntm_bpmftrec), &bp);
282 kprintf("ntfs_loadntnode: BREAD FAILED\n");
286 memcpy(mfrp, bp->b_data, ntfs_bntob(ntmp->ntm_bpmftrec));
291 vp = ntmp->ntm_sysvn[NTFS_MFTINO];
292 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
293 ip->i_number * ntfs_bntob(ntmp->ntm_bpmftrec),
294 ntfs_bntob(ntmp->ntm_bpmftrec), mfrp, NULL);
296 kprintf("ntfs_loadntnode: ntfs_readattr failed\n");
301 /* Check if magic and fixups are correct */
302 error = ntfs_procfixups(ntmp, NTFS_FILEMAGIC, (caddr_t)mfrp,
303 ntfs_bntob(ntmp->ntm_bpmftrec));
305 kprintf("ntfs_loadntnode: BAD MFT RECORD %"PRId64"\n",
310 dprintf(("ntfs_loadntnode: load attrs for ino: %"PRId64"\n",ip->i_number));
311 off = mfrp->fr_attroff;
312 ap = (struct attr *) ((caddr_t)mfrp + off);
314 LIST_INIT(&ip->i_valist);
316 while (ap->a_hdr.a_type != -1) {
317 error = ntfs_attrtontvattr(ntmp, &nvap, ap);
322 LIST_INSERT_HEAD(&ip->i_valist, nvap, va_list);
324 off += ap->a_hdr.reclen;
325 ap = (struct attr *) ((caddr_t)mfrp + off);
328 kprintf("ntfs_loadntnode: failed to load attr ino: %"PRId64"\n",
333 ip->i_mainrec = mfrp->fr_mainrec;
334 ip->i_nlink = mfrp->fr_nlink;
335 ip->i_frflag = mfrp->fr_flags;
337 ip->i_flag |= IN_LOADED;
345 * Routine locks ntnode and increase usecount, just opposite of
349 ntfs_ntget(struct ntnode *ip)
351 dprintf(("ntfs_ntget: get ntnode %"PRId64": %p, usecount: %d\n",
352 ip->i_number, ip, ip->i_usecount));
354 ip->i_usecount++; /* ZZZ */
355 LOCKMGR(&ip->i_lock, LK_EXCLUSIVE);
361 * Routine search ntnode in hash, if found: lock, inc usecount and return.
362 * If not in hash allocate structure for ntnode, prefill it, lock,
363 * inc count and return.
365 * ntnode returned locked
368 ntfs_ntlookup(struct ntfsmount *ntmp, ino_t ino, struct ntnode **ipp)
372 dprintf(("ntfs_ntlookup: looking for ntnode %ju\n", (uintmax_t)ino));
375 if ((ip = ntfs_nthashlookup(ntmp->ntm_dev, ino)) != NULL) {
377 dprintf(("ntfs_ntlookup: ntnode %ju: %p, usecount: %d\n",
378 (uintmax_t)ino, ip, ip->i_usecount));
382 } while (LOCKMGR(&ntfs_hashlock, LK_EXCLUSIVE | LK_SLEEPFAIL));
384 ip = kmalloc(sizeof(struct ntnode), M_NTFSNTNODE, M_WAITOK | M_ZERO);
385 ddprintf(("ntfs_ntlookup: allocating ntnode: %d: %p\n", ino, ip));
387 /* Generic initialization */
388 ip->i_devvp = ntmp->ntm_devvp;
389 ip->i_dev = ntmp->ntm_dev;
393 LIST_INIT(&ip->i_fnlist);
396 /* init lock and lock the newborn ntnode */
397 lockinit(&ip->i_lock, "ntnode", 0, LK_EXCLUSIVE);
398 spin_init(&ip->i_interlock);
403 LOCKMGR(&ntfs_hashlock, LK_RELEASE);
407 dprintf(("ntfs_ntlookup: ntnode %ju: %p, usecount: %d\n",
408 (uintmax_t)ino, ip, ip->i_usecount));
414 * Decrement usecount of ntnode and unlock it, if usecount reach zero,
417 * ntnode should be locked on entry, and unlocked on return.
420 ntfs_ntput(struct ntnode *ip)
424 dprintf(("ntfs_ntput: rele ntnode %"PRId64": %p, usecount: %d\n",
425 ip->i_number, ip, ip->i_usecount));
427 spin_lock(&ip->i_interlock);
431 if (ip->i_usecount < 0) {
432 spin_unlock(&ip->i_interlock);
433 panic("ntfs_ntput: ino: %"PRId64" usecount: %d ",
434 ip->i_number,ip->i_usecount);
438 if (ip->i_usecount > 0) {
439 spin_unlock(&ip->i_interlock);
440 LOCKMGR(&ip->i_lock, LK_RELEASE);
444 dprintf(("ntfs_ntput: deallocating ntnode: %"PRId64"\n", ip->i_number));
446 if (ip->i_fnlist.lh_first) {
447 spin_unlock(&ip->i_interlock);
448 panic("ntfs_ntput: ntnode has fnodes");
452 * XXX this is a bit iffy because we are making high level calls
453 * while holding a spinlock.
457 while ((vap = LIST_FIRST(&ip->i_valist)) != NULL) {
458 LIST_REMOVE(vap,va_list);
459 ntfs_freentvattr(vap);
461 spin_unlock(&ip->i_interlock);
463 kfree(ip, M_NTFSNTNODE);
467 * increment usecount of ntnode
470 ntfs_ntref(struct ntnode *ip)
474 dprintf(("ntfs_ntref: ino %"PRId64", usecount: %d\n",
475 ip->i_number, ip->i_usecount));
480 * Decrement usecount of ntnode.
483 ntfs_ntrele(struct ntnode *ip)
485 dprintf(("ntfs_ntrele: rele ntnode %"PRId64": %p, usecount: %d\n",
486 ip->i_number, ip, ip->i_usecount));
488 spin_lock(&ip->i_interlock);
491 if (ip->i_usecount < 0) {
492 spin_unlock(&ip->i_interlock);
493 panic("ntfs_ntrele: ino: %"PRId64" usecount: %d ",
494 ip->i_number,ip->i_usecount);
496 spin_unlock(&ip->i_interlock);
500 * Deallocate all memory allocated for ntvattr
503 ntfs_freentvattr(struct ntvattr *vap)
505 if (vap->va_flag & NTFS_AF_INRUN) {
507 kfree(vap->va_vruncn, M_NTFSRUN);
509 kfree(vap->va_vruncl, M_NTFSRUN);
512 kfree(vap->va_datap, M_NTFSRDATA);
514 kfree(vap, M_NTFSNTVATTR);
518 * Convert disk image of attribute into ntvattr structure,
519 * runs are expanded also.
522 ntfs_attrtontvattr(struct ntfsmount *ntmp, struct ntvattr **rvapp,
531 vap = kmalloc(sizeof(struct ntvattr), M_NTFSNTVATTR,
534 vap->va_flag = rap->a_hdr.a_flag;
535 vap->va_type = rap->a_hdr.a_type;
536 vap->va_compression = rap->a_hdr.a_compression;
537 vap->va_index = rap->a_hdr.a_index;
539 ddprintf(("type: 0x%x, index: %d", vap->va_type, vap->va_index));
541 vap->va_namelen = rap->a_hdr.a_namelen;
542 if (rap->a_hdr.a_namelen) {
543 wchar *unp = (wchar *) ((caddr_t) rap + rap->a_hdr.a_nameoff);
544 ddprintf((", name:["));
545 for (i = 0; i < vap->va_namelen; i++) {
546 vap->va_name[i] = unp[i];
547 ddprintf(("%c", vap->va_name[i]));
551 if (vap->va_flag & NTFS_AF_INRUN) {
552 ddprintf((", nonres."));
553 vap->va_datalen = rap->a_nr.a_datalen;
554 vap->va_allocated = rap->a_nr.a_allocated;
555 vap->va_vcnstart = rap->a_nr.a_vcnstart;
556 vap->va_vcnend = rap->a_nr.a_vcnend;
557 vap->va_compressalg = rap->a_nr.a_compressalg;
558 error = ntfs_runtovrun(&(vap->va_vruncn), &(vap->va_vruncl),
560 (caddr_t) rap + rap->a_nr.a_dataoff);
562 vap->va_compressalg = 0;
563 ddprintf((", res."));
564 vap->va_datalen = rap->a_r.a_datalen;
565 vap->va_allocated = rap->a_r.a_datalen;
566 vap->va_vcnstart = 0;
567 vap->va_vcnend = ntfs_btocn(vap->va_allocated);
568 vap->va_datap = kmalloc(vap->va_datalen, M_NTFSRDATA,
570 memcpy(vap->va_datap, (caddr_t) rap + rap->a_r.a_dataoff,
573 ddprintf((", len: %d", vap->va_datalen));
576 kfree(vap, M_NTFSNTVATTR);
586 * Expand run into more utilizable and more memory eating format.
589 ntfs_runtovrun(cn_t **rcnp, cn_t **rclp, u_long *rcntp, u_int8_t *run)
603 off += (run[off] & 0xF) + ((run[off] >> 4) & 0xF) + 1;
606 cn = kmalloc(cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
607 cl = kmalloc(cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
617 for (i = 0; i < (sz & 0xF); i++)
618 cl[cnt] += (u_int32_t) run[off++] << (i << 3);
621 if (run[off + sz - 1] & 0x80) {
622 tmp = ((u_int64_t) - 1) << (sz << 3);
623 for (i = 0; i < sz; i++)
624 tmp |= (u_int64_t) run[off++] << (i << 3);
627 for (i = 0; i < sz; i++)
628 tmp |= (u_int64_t) run[off++] << (i << 3);
631 prev = cn[cnt] = prev + tmp;
644 * Compare unicode and ascii string case insens.
647 ntfs_uastricmp(struct ntfsmount *ntmp, const wchar *ustr, size_t ustrlen,
648 const char *astr, size_t astrlen)
651 size_t i, j, mbstrlen = astrlen;
655 if (ntmp->ntm_ic_l2u) {
656 for (i = 0, j = 0; i < ustrlen && j < astrlen; i++, j++) {
657 if (j < astrlen -1) {
658 wc = (wchar)astr[j]<<8 | (astr[j+1]&0xFF);
661 wc = (wchar)astr[j]<<8 & 0xFF00;
664 res = ((int) NTFS_TOUPPER(ustr[i])) -
665 ((int)NTFS_TOUPPER(NTFS_82U(wc, &len)));
674 * We use NTFS_82U(NTFS_U28(c)) to get rid of unicode
675 * symbols not covered by translation table
677 for (i = 0; i < ustrlen && i < astrlen; i++) {
678 res = ((int) NTFS_TOUPPER(NTFS_82U(NTFS_U28(ustr[i]), &len))) -
679 ((int)NTFS_TOUPPER(NTFS_82U((wchar)astr[i], &len)));
684 return (ustrlen - mbstrlen);
688 * Compare unicode and ascii string case sens.
691 ntfs_uastrcmp(struct ntfsmount *ntmp, const wchar *ustr, size_t ustrlen,
692 const char *astr, size_t astrlen)
695 size_t i, j, mbstrlen = astrlen;
699 for (i = 0, j = 0; (i < ustrlen) && (j < astrlen); i++, j++) {
701 wc = NTFS_U28(ustr[i]);
704 if (u != '\0' && j < astrlen -1) {
705 res = (int) (u - astr[j++]);
708 res = (res<<8) + (int) (l - astr[j]);
712 return (ustrlen - mbstrlen);
716 * Search fnode in ntnode, if not found allocate and preinitialize.
718 * ntnode should be locked on entry.
721 ntfs_fget(struct ntfsmount *ntmp, struct ntnode *ip, int attrtype,
722 char *attrname, struct fnode **fpp)
726 dprintf(("ntfs_fget: ino: %"PRId64", attrtype: 0x%x, attrname: %s\n",
727 ip->i_number,attrtype, attrname?attrname:""));
729 for (fp = ip->i_fnlist.lh_first; fp != NULL; fp = fp->f_fnlist.le_next){
730 dprintf(("ntfs_fget: fnode: attrtype: %d, attrname: %s\n",
731 fp->f_attrtype, fp->f_attrname?fp->f_attrname:""));
733 if ((attrtype == fp->f_attrtype) &&
734 ((!attrname && !fp->f_attrname) ||
735 (attrname && fp->f_attrname &&
736 !strcmp(attrname,fp->f_attrname)))){
737 dprintf(("ntfs_fget: found existed: %p\n",fp));
745 fp = kmalloc(sizeof(struct fnode), M_NTFSFNODE, M_WAITOK | M_ZERO);
746 dprintf(("ntfs_fget: allocating fnode: %p\n",fp));
750 fp->f_flag |= FN_AATTRNAME;
751 fp->f_attrname = kmalloc(strlen(attrname) + 1, M_TEMP,
753 strcpy(fp->f_attrname, attrname);
755 fp->f_attrname = NULL;
756 fp->f_attrtype = attrtype;
760 LIST_INSERT_HEAD(&ip->i_fnlist, fp, f_fnlist);
768 * Deallocate fnode, remove it from ntnode's fnode list.
770 * ntnode should be locked.
773 ntfs_frele(struct fnode *fp)
775 struct ntnode *ip = FTONT(fp);
777 dprintf(("ntfs_frele: fnode: %p for %"PRId64": %p\n", fp, ip->i_number, ip));
779 dprintf(("ntfs_frele: deallocating fnode\n"));
780 LIST_REMOVE(fp,f_fnlist);
781 if (fp->f_flag & FN_AATTRNAME)
782 kfree(fp->f_attrname, M_TEMP);
784 kfree(fp->f_dirblbuf, M_NTFSDIR);
785 kfree(fp, M_NTFSFNODE);
790 * Lookup attribute name in format: [[:$ATTR_TYPE]:$ATTR_NAME],
791 * $ATTR_TYPE is searched in attrdefs read from $AttrDefs.
792 * If $ATTR_TYPE nott specifed, ATTR_A_DATA assumed.
795 ntfs_ntlookupattr(struct ntfsmount *ntmp, const char *name, int namelen,
796 int *attrtype, char **attrname)
800 struct ntvattrdef *adp;
805 if (name[0] == '$') {
807 for (syslen = 0; syslen < namelen; syslen++) {
808 if(sys[syslen] == ':') {
818 for (i = 0; i < ntmp->ntm_adnum; i++, adp++){
819 if (syslen != adp->ad_namelen ||
820 strncmp(sys, adp->ad_name, syslen) != 0)
823 *attrtype = adp->ad_type;
828 *attrtype = NTFS_A_DATA;
832 (*attrname) = kmalloc(namelen, M_TEMP, M_WAITOK);
833 memcpy((*attrname), name, namelen);
834 (*attrname)[namelen] = '\0';
841 * Lookup specifed node for filename, matching cnp,
842 * return fnode filled.
845 ntfs_ntlookupfile(struct ntfsmount *ntmp, struct vnode *vp,
846 struct componentname *cnp, struct vnode **vpp)
848 struct fnode *fp = VTOF(vp);
849 struct ntnode *ip = FTONT(fp);
850 struct ntvattr *vap; /* Root attribute */
851 cn_t cn; /* VCN in current attribute */
852 caddr_t rdbuf; /* Buffer to read directory's blocks */
854 u_int32_t rdsize; /* Length of data to read from current block */
855 struct attr_indexentry *iep;
856 int error, res, anamelen, fnamelen;
857 const char *fname,*aname;
859 int attrtype = NTFS_A_DATA;
860 char *attrname = NULL;
865 error = ntfs_ntget(ip);
869 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
870 if (error || (vap->va_flag & NTFS_AF_INRUN))
873 blsize = vap->va_a_iroot->ir_size;
874 rdsize = vap->va_datalen;
877 * Divide file name into: foofilefoofilefoofile[:attrspec]
878 * Store like this: fname:fnamelen [aname:anamelen]
880 fname = cnp->cn_nameptr;
883 for (fnamelen = 0; fnamelen < cnp->cn_namelen; fnamelen++)
884 if(fname[fnamelen] == ':') {
885 aname = fname + fnamelen + 1;
886 anamelen = cnp->cn_namelen - fnamelen - 1;
887 dprintf(("ntfs_ntlookupfile: %s (%d), attr: %s (%d)\n",
888 fname, fnamelen, aname, anamelen));
892 dprintf(("ntfs_ntlookupfile: blksz: %d, rdsz: %d\n", blsize, rdsize));
894 rdbuf = kmalloc(blsize, M_TEMP, M_WAITOK);
896 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXROOT, "$I30",
897 0, rdsize, rdbuf, NULL);
901 aoff = sizeof(struct attr_indexroot);
904 iep = (struct attr_indexentry *) (rdbuf + aoff);
906 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
908 iep = (struct attr_indexentry *) (rdbuf + aoff))
910 ddprintf(("scan: %d, %d\n",
911 (u_int32_t) iep->ie_number,
912 (u_int32_t) iep->ie_fnametype));
914 /* check the name - the case-insensitible check
915 * has to come first, to break from this for loop
916 * if needed, so we can dive correctly */
917 res = NTFS_UASTRICMP(iep->ie_fname, iep->ie_fnamelen,
920 if (res < 0) continue;
922 if (iep->ie_fnametype == 0 ||
923 !(ntmp->ntm_flag & NTFS_MFLAG_CASEINS))
925 res = NTFS_UASTRCMP(iep->ie_fname,
926 iep->ie_fnamelen, fname, fnamelen);
927 if (res != 0) continue;
931 error = ntfs_ntlookupattr(ntmp,
933 &attrtype, &attrname);
938 /* Check if we've found ourself */
939 if ((iep->ie_number == ip->i_number) &&
940 (attrtype == fp->f_attrtype) &&
941 ((!attrname && !fp->f_attrname) ||
942 (attrname && fp->f_attrname &&
943 !strcmp(attrname, fp->f_attrname))))
951 /* vget node, but don't load it */
952 error = ntfs_vgetex(ntmp->ntm_mountp,
953 iep->ie_number, attrtype, attrname,
954 LK_EXCLUSIVE, VG_DONTLOADIN | VG_DONTVALIDFN,
957 /* free the buffer returned by ntfs_ntlookupattr() */
959 kfree(attrname, M_TEMP);
968 if (nfp->f_flag & FN_VALID) {
973 nfp->f_fflag = iep->ie_fflag;
974 nfp->f_pnumber = iep->ie_fpnumber;
975 nfp->f_times = iep->ie_ftimes;
977 if((nfp->f_fflag & NTFS_FFLAG_DIR) &&
978 (nfp->f_attrtype == NTFS_A_DATA) &&
979 (nfp->f_attrname == NULL))
984 nvp->v_type = f_type;
986 if ((nfp->f_attrtype == NTFS_A_DATA) &&
987 (nfp->f_attrname == NULL))
989 /* Opening default attribute */
990 nfp->f_size = iep->ie_fsize;
991 nfp->f_allocated = iep->ie_fallocated;
992 nfp->f_flag |= FN_PRELOADED;
994 error = ntfs_filesize(ntmp, nfp,
995 &nfp->f_size, &nfp->f_allocated);
1001 nfp->f_flag &= ~FN_VALID;
1004 * Normal files use the buffer cache
1006 if (nvp->v_type == VREG)
1007 vinitvmio(nvp, nfp->f_size, PAGE_SIZE, -1);
1012 /* Dive if possible */
1013 if (iep->ie_flag & NTFS_IEFLAG_SUBNODE) {
1014 dprintf(("ntfs_ntlookupfile: diving\n"));
1016 cn = *(cn_t *) (rdbuf + aoff +
1017 iep->reclen - sizeof(cn_t));
1020 error = ntfs_readattr(ntmp, ip, NTFS_A_INDX, "$I30",
1021 ntfs_cntob(cn), rdsize, rdbuf, NULL);
1025 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1030 aoff = (((struct attr_indexalloc *) rdbuf)->ia_hdrsize +
1033 dprintf(("ntfs_ntlookupfile: nowhere to dive :-(\n"));
1039 dprintf(("finish\n"));
1042 if (attrname) kfree(attrname, M_TEMP);
1043 ntfs_ntvattrrele(vap);
1045 kfree(rdbuf, M_TEMP);
1050 * Check if name type is permitted to show.
1053 ntfs_isnamepermitted(struct ntfsmount *ntmp, struct attr_indexentry *iep)
1055 if (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)
1058 switch (iep->ie_fnametype) {
1060 ddprintf(("ntfs_isnamepermitted: skipped DOS name\n"));
1062 case 0: case 1: case 3:
1065 kprintf("ntfs_isnamepermitted: " \
1066 "WARNING! Unknown file name type: %d\n",
1074 * Read ntfs dir like stream of attr_indexentry, not like btree of them.
1075 * This is done by scaning $BITMAP:$I30 for busy clusters and reading them.
1076 * Ofcouse $INDEX_ROOT:$I30 is read before. Last read values are stored in
1077 * fnode, so we can skip toward record number num almost immediatly.
1078 * Anyway this is rather slow routine. The problem is that we don't know
1079 * how many records are there in $INDEX_ALLOCATION:$I30 block.
1082 ntfs_ntreaddir(struct ntfsmount *ntmp, struct fnode *fp,
1083 u_int32_t num, struct attr_indexentry **riepp)
1085 struct ntnode *ip = FTONT(fp);
1086 struct ntvattr *vap = NULL; /* IndexRoot attribute */
1087 struct ntvattr *bmvap = NULL; /* BitMap attribute */
1088 struct ntvattr *iavap = NULL; /* IndexAllocation attribute */
1089 caddr_t rdbuf; /* Buffer to read directory's blocks */
1090 u_char *bmp = NULL; /* Bitmap */
1091 u_int32_t blsize; /* Index allocation size (2048) */
1092 u_int32_t rdsize; /* Length of data to read */
1093 u_int32_t attrnum; /* Current attribute type */
1094 u_int32_t cpbl = 1; /* Clusters per directory block */
1096 struct attr_indexentry *iep;
1098 u_int32_t aoff, cnum;
1100 dprintf(("ntfs_ntreaddir: read ino: %"PRId64", num: %d\n", ip->i_number, num));
1101 error = ntfs_ntget(ip);
1105 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
1109 if (fp->f_dirblbuf == NULL) {
1110 fp->f_dirblsz = vap->va_a_iroot->ir_size;
1111 fp->f_dirblbuf = kmalloc(max(vap->va_datalen, fp->f_dirblsz),
1112 M_NTFSDIR, M_WAITOK);
1115 blsize = fp->f_dirblsz;
1116 rdbuf = fp->f_dirblbuf;
1118 dprintf(("ntfs_ntreaddir: rdbuf: 0x%p, blsize: %d\n", rdbuf, blsize));
1120 if (vap->va_a_iroot->ir_flag & NTFS_IRFLAG_INDXALLOC) {
1121 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXBITMAP, "$I30",
1127 bmp = kmalloc(bmvap->va_datalen, M_TEMP, M_WAITOK);
1128 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXBITMAP, "$I30", 0,
1129 bmvap->va_datalen, bmp, NULL);
1133 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDX, "$I30",
1139 cpbl = ntfs_btocn(blsize + ntfs_cntob(1) - 1);
1140 dprintf(("ntfs_ntreaddir: indexalloc: %d, cpbl: %d\n",
1141 iavap->va_datalen, cpbl));
1143 dprintf(("ntfs_ntreadidir: w/o BitMap and IndexAllocation\n"));
1144 iavap = bmvap = NULL;
1148 /* Try use previous values */
1149 if ((fp->f_lastdnum < num) && (fp->f_lastdnum != 0)) {
1150 attrnum = fp->f_lastdattr;
1151 aoff = fp->f_lastdoff;
1152 blnum = fp->f_lastdblnum;
1153 cnum = fp->f_lastdnum;
1155 attrnum = NTFS_A_INDXROOT;
1156 aoff = sizeof(struct attr_indexroot);
1162 dprintf(("ntfs_ntreaddir: scan: 0x%x, %d, %d, %d, %d\n",
1163 attrnum, blnum, cnum, num, aoff));
1164 rdsize = (attrnum == NTFS_A_INDXROOT) ? vap->va_datalen : blsize;
1165 error = ntfs_readattr(ntmp, ip, attrnum, "$I30",
1166 ntfs_cntob(blnum * cpbl), rdsize, rdbuf, NULL);
1170 if (attrnum == NTFS_A_INDX) {
1171 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1177 aoff = (attrnum == NTFS_A_INDX) ?
1178 (0x18 + ((struct attr_indexalloc *) rdbuf)->ia_hdrsize) :
1179 sizeof(struct attr_indexroot);
1181 iep = (struct attr_indexentry *) (rdbuf + aoff);
1182 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
1183 aoff += iep->reclen,
1184 iep = (struct attr_indexentry *) (rdbuf + aoff))
1186 if (!ntfs_isnamepermitted(ntmp, iep)) continue;
1189 fp->f_lastdnum = cnum;
1190 fp->f_lastdoff = aoff;
1191 fp->f_lastdblnum = blnum;
1192 fp->f_lastdattr = attrnum;
1203 if (attrnum == NTFS_A_INDXROOT)
1208 while (ntfs_cntob(blnum * cpbl) < iavap->va_datalen) {
1209 if (bmp[blnum >> 3] & (1 << (blnum & 3)))
1214 attrnum = NTFS_A_INDX;
1216 if (ntfs_cntob(blnum * cpbl) >= iavap->va_datalen)
1218 dprintf(("ntfs_ntreaddir: blnum: %d\n", blnum));
1227 ntfs_ntvattrrele(vap);
1229 ntfs_ntvattrrele(bmvap);
1231 ntfs_ntvattrrele(iavap);
1239 * Convert NTFS times that are in 100 ns units and begins from
1240 * 1601 Jan 1 into unix times.
1243 ntfs_nttimetounix(u_int64_t nt)
1247 /* WindowNT times are in 100 ns and from 1601 Jan 1 */
1248 t.tv_nsec = (nt % (1000 * 1000 * 10)) * 100;
1249 t.tv_sec = nt / (1000 * 1000 * 10) -
1250 369LL * 365LL * 24LL * 60LL * 60LL -
1251 89LL * 1LL * 24LL * 60LL * 60LL;
1256 * Get file times from NTFS_A_NAME attribute.
1259 ntfs_times(struct ntfsmount *ntmp, struct ntnode *ip, ntfs_times_t *tm)
1261 struct ntvattr *vap;
1264 dprintf(("ntfs_times: ino: %"PRId64"...\n", ip->i_number));
1266 error = ntfs_ntget(ip);
1270 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_NAME, NULL, 0, &vap);
1275 *tm = vap->va_a_name->n_times;
1276 ntfs_ntvattrrele(vap);
1283 * Get file sizes from corresponding attribute.
1285 * ntnode under fnode should be locked.
1288 ntfs_filesize(struct ntfsmount *ntmp, struct fnode *fp, u_int64_t *size,
1291 struct ntvattr *vap;
1292 struct ntnode *ip = FTONT(fp);
1296 dprintf(("ntfs_filesize: ino: %"PRId64"\n", ip->i_number));
1298 error = ntfs_ntvattrget(ntmp, ip,
1299 fp->f_attrtype, fp->f_attrname, 0, &vap);
1303 bn = vap->va_allocated;
1304 sz = vap->va_datalen;
1306 dprintf(("ntfs_filesize: %d bytes (%d bytes allocated)\n",
1307 (u_int32_t) sz, (u_int32_t) bn));
1314 ntfs_ntvattrrele(vap);
1320 * This is one of write routine.
1323 ntfs_writeattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1324 u_int32_t attrnum, char *attrname, off_t roff,
1325 size_t rsize, void *rdata, size_t *initp,
1330 off_t off = roff, left = rsize, towrite;
1331 caddr_t data = rdata;
1332 struct ntvattr *vap;
1336 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1337 ntfs_btocn(off), &vap);
1340 towrite = min(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1341 ddprintf(("ntfs_writeattr_plain: o: %d, s: %d (%d - %d)\n",
1342 (u_int32_t) off, (u_int32_t) towrite,
1343 (u_int32_t) vap->va_vcnstart,
1344 (u_int32_t) vap->va_vcnend));
1345 error = ntfs_writentvattr_plain(ntmp, ip, vap,
1346 off - ntfs_cntob(vap->va_vcnstart),
1347 towrite, data, &init, uio);
1349 kprintf("ntfs_writeattr_plain: " \
1350 "ntfs_writentvattr_plain failed: o: %d, s: %d\n",
1351 (u_int32_t) off, (u_int32_t) towrite);
1352 kprintf("ntfs_writeattr_plain: attrib: %d - %d\n",
1353 (u_int32_t) vap->va_vcnstart,
1354 (u_int32_t) vap->va_vcnend);
1355 ntfs_ntvattrrele(vap);
1358 ntfs_ntvattrrele(vap);
1361 data = data + towrite;
1369 * This is one of write routine.
1371 * ntnode should be locked.
1374 ntfs_writentvattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1375 struct ntvattr *vap, off_t roff, size_t rsize,
1376 void *rdata, size_t *initp, struct uio *uio)
1381 cn_t ccn, ccl, cn, left, cl;
1382 caddr_t data = rdata;
1388 if ((vap->va_flag & NTFS_AF_INRUN) == 0) {
1389 kprintf("ntfs_writevattr_plain: CAN'T WRITE RES. ATTRIBUTE\n");
1393 ddprintf(("ntfs_writentvattr_plain: data in run: %ld chains\n",
1401 for (; left && (cnt < vap->va_vruncnt); cnt++) {
1402 ccn = vap->va_vruncn[cnt];
1403 ccl = vap->va_vruncl[cnt];
1405 ddprintf(("ntfs_writentvattr_plain: " \
1406 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1407 (u_int32_t) left, (u_int32_t) ccn, \
1408 (u_int32_t) ccl, (u_int32_t) off));
1410 if (ntfs_cntob(ccl) < off) {
1411 off -= ntfs_cntob(ccl);
1415 if (!ccn && ip->i_number != NTFS_BOOTINO)
1418 ccl -= ntfs_btocn(off);
1419 cn = ccn + ntfs_btocn(off);
1420 off = ntfs_btocnoff(off);
1422 while (left && ccl) {
1423 #if defined(__DragonFly__)
1425 min(ntfs_cntob(ccl) - off, MAXBSIZE - off));
1427 /* under NetBSD, bread() can read
1428 * maximum one block worth of data */
1429 tocopy = min(left, ntmp->ntm_bps - off);
1431 cl = ntfs_btocl(tocopy + off);
1432 ddprintf(("ntfs_writentvattr_plain: write: " \
1433 "cn: 0x%x cl: %d, off: %d len: %d, left: %d\n",
1434 (u_int32_t) cn, (u_int32_t) cl,
1435 (u_int32_t) off, (u_int32_t) tocopy,
1437 if (off == 0 && tocopy == ntfs_cntob(cl) &&
1438 uio->uio_segflg != UIO_NOCOPY) {
1439 bp = getblk(ntmp->ntm_devvp, ntfs_cntodoff(cn),
1440 ntfs_cntob(cl), 0, 0);
1443 error = bread(ntmp->ntm_devvp,
1445 ntfs_cntob(cl), &bp);
1452 uiomove(bp->b_data + off, tocopy, uio);
1454 memcpy(bp->b_data + off, data, tocopy);
1456 data = data + tocopy;
1466 kprintf("ntfs_writentvattr_plain: POSSIBLE RUN ERROR\n");
1474 * This is one of read routines.
1476 * ntnode should be locked.
1479 ntfs_readntvattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1480 struct ntvattr *vap, off_t roff, size_t rsize,
1481 void *rdata, size_t *initp, struct uio *uio)
1487 if (vap->va_flag & NTFS_AF_INRUN) {
1489 cn_t ccn, ccl, cn, left, cl;
1490 caddr_t data = rdata;
1494 ddprintf(("ntfs_readntvattr_plain: data in run: %ld chains\n",
1502 while (left && (cnt < vap->va_vruncnt)) {
1503 ccn = vap->va_vruncn[cnt];
1504 ccl = vap->va_vruncl[cnt];
1506 ddprintf(("ntfs_readntvattr_plain: " \
1507 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1508 (u_int32_t) left, (u_int32_t) ccn, \
1509 (u_int32_t) ccl, (u_int32_t) off));
1511 if (ntfs_cntob(ccl) < off) {
1512 off -= ntfs_cntob(ccl);
1516 if (ccn || ip->i_number == NTFS_BOOTINO) {
1517 ccl -= ntfs_btocn(off);
1518 cn = ccn + ntfs_btocn(off);
1519 off = ntfs_btocnoff(off);
1521 while (left && ccl) {
1522 #if defined(__DragonFly__)
1524 min(ntfs_cntob(ccl) - off,
1527 /* under NetBSD, bread() can read
1528 * maximum one block worth of data */
1530 ntmp->ntm_bps - off);
1532 cl = ntfs_btocl(tocopy + off);
1533 ddprintf(("ntfs_readntvattr_plain: " \
1534 "read: cn: 0x%x cl: %d, " \
1535 "off: %d len: %d, left: %d\n",
1541 error = bread(ntmp->ntm_devvp,
1550 uiomove(bp->b_data + off,
1553 memcpy(data, bp->b_data + off,
1557 data = data + tocopy;
1565 tocopy = min(left, ntfs_cntob(ccl) - off);
1566 ddprintf(("ntfs_readntvattr_plain: "
1567 "hole: ccn: 0x%x ccl: %d, off: %d, " \
1568 " len: %d, left: %d\n",
1569 (u_int32_t) ccn, (u_int32_t) ccl,
1570 (u_int32_t) off, (u_int32_t) tocopy,
1575 size_t remains = tocopy;
1576 for(; remains; remains++)
1577 uiomove("", 1, uio);
1579 bzero(data, tocopy);
1580 data = data + tocopy;
1585 kprintf("ntfs_readntvattr_plain: POSSIBLE RUN ERROR\n");
1589 ddprintf(("ntfs_readnvattr_plain: data is in mft record\n"));
1591 uiomove(vap->va_datap + roff, rsize, uio);
1593 memcpy(rdata, vap->va_datap + roff, rsize);
1601 * This is one of read routines.
1604 ntfs_readattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1605 u_int32_t attrnum, char *attrname, off_t roff,
1606 size_t rsize, void *rdata, size_t * initp,
1611 off_t off = roff, left = rsize, toread;
1612 caddr_t data = rdata;
1613 struct ntvattr *vap;
1617 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1618 ntfs_btocn(off), &vap);
1621 toread = min(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1622 ddprintf(("ntfs_readattr_plain: o: %d, s: %d (%d - %d)\n",
1623 (u_int32_t) off, (u_int32_t) toread,
1624 (u_int32_t) vap->va_vcnstart,
1625 (u_int32_t) vap->va_vcnend));
1626 error = ntfs_readntvattr_plain(ntmp, ip, vap,
1627 off - ntfs_cntob(vap->va_vcnstart),
1628 toread, data, &init, uio);
1630 kprintf("ntfs_readattr_plain: " \
1631 "ntfs_readntvattr_plain failed: o: %d, s: %d\n",
1632 (u_int32_t) off, (u_int32_t) toread);
1633 kprintf("ntfs_readattr_plain: attrib: %d - %d\n",
1634 (u_int32_t) vap->va_vcnstart,
1635 (u_int32_t) vap->va_vcnend);
1636 ntfs_ntvattrrele(vap);
1639 ntfs_ntvattrrele(vap);
1642 data = data + toread;
1650 * This is one of read routines.
1653 ntfs_readattr(struct ntfsmount *ntmp, struct ntnode *ip, u_int32_t attrnum,
1654 char *attrname, off_t roff, size_t rsize, void *rdata,
1658 struct ntvattr *vap;
1661 ddprintf(("ntfs_readattr: reading %"PRId64": 0x%x, from %d size %d bytes\n",
1662 ip->i_number, attrnum, (u_int32_t) roff, (u_int32_t) rsize));
1664 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname, 0, &vap);
1668 if ((roff > vap->va_datalen) ||
1669 (roff + rsize > vap->va_datalen)) {
1670 ddprintf(("ntfs_readattr: offset too big\n"));
1671 ntfs_ntvattrrele(vap);
1674 if (vap->va_compression && vap->va_compressalg) {
1677 off_t off, left = rsize, tocopy;
1678 caddr_t data = rdata;
1681 ddprintf(("ntfs_ntreadattr: compression: %d\n",
1682 vap->va_compressalg));
1684 cup = kmalloc(ntfs_cntob(NTFS_COMPUNIT_CL), M_NTFSDECOMP,
1686 uup = kmalloc(ntfs_cntob(NTFS_COMPUNIT_CL), M_NTFSDECOMP,
1689 cn = (ntfs_btocn(roff)) & (~(NTFS_COMPUNIT_CL - 1));
1690 off = roff - ntfs_cntob(cn);
1693 error = ntfs_readattr_plain(ntmp, ip, attrnum,
1694 attrname, ntfs_cntob(cn),
1695 ntfs_cntob(NTFS_COMPUNIT_CL),
1700 tocopy = min(left, ntfs_cntob(NTFS_COMPUNIT_CL) - off);
1702 if (init == ntfs_cntob(NTFS_COMPUNIT_CL)) {
1704 uiomove(cup + off, tocopy, uio);
1706 memcpy(data, cup + off, tocopy);
1707 } else if (init == 0) {
1709 size_t remains = tocopy;
1710 for(; remains; remains--)
1711 uiomove("", 1, uio);
1714 bzero(data, tocopy);
1716 error = ntfs_uncompunit(ntmp, uup, cup);
1720 uiomove(uup + off, tocopy, uio);
1722 memcpy(data, uup + off, tocopy);
1726 data = data + tocopy;
1727 off += tocopy - ntfs_cntob(NTFS_COMPUNIT_CL);
1728 cn += NTFS_COMPUNIT_CL;
1731 kfree(uup, M_NTFSDECOMP);
1732 kfree(cup, M_NTFSDECOMP);
1734 error = ntfs_readattr_plain(ntmp, ip, attrnum, attrname,
1735 roff, rsize, rdata, &init, uio);
1736 ntfs_ntvattrrele(vap);
1742 ntfs_parserun(cn_t *cn, cn_t *cl, u_int8_t *run, u_long len, u_long *off)
1748 kprintf("ntfs_parsetun: run == NULL\n");
1753 kprintf("ntfs_parserun: trying to go out of run\n");
1757 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1758 kprintf("ntfs_parserun: " \
1759 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1763 for (i = 0; i < (sz & 0xF); i++)
1764 *cl += (u_int32_t) run[(*off)++] << (i << 3);
1767 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1768 kprintf("ntfs_parserun: " \
1769 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1773 for (i = 0; i < (sz & 0xF); i++)
1774 *cn += (u_int32_t) run[(*off)++] << (i << 3);
1781 * Process fixup routine on given buffer.
1784 ntfs_procfixups(struct ntfsmount *ntmp, u_int32_t magic, caddr_t buf,
1787 struct fixuphdr *fhp = (struct fixuphdr *) buf;
1793 if (fhp->fh_magic != magic) {
1794 kprintf("ntfs_procfixups: magic doesn't match: %08x != %08x\n",
1795 fhp->fh_magic, magic);
1798 if ((fhp->fh_fnum - 1) * ntmp->ntm_bps != len) {
1799 kprintf("ntfs_procfixups: " \
1800 "bad fixups number: %d for %ld bytes block\n",
1801 fhp->fh_fnum, (long)len); /* XXX kprintf kludge */
1804 if (fhp->fh_foff >= ntmp->ntm_spc * ntmp->ntm_mftrecsz * ntmp->ntm_bps) {
1805 kprintf("ntfs_procfixups: invalid offset: %x", fhp->fh_foff);
1808 fxp = (u_int16_t *) (buf + fhp->fh_foff);
1809 cfxp = (u_int16_t *) (buf + ntmp->ntm_bps - 2);
1811 for (i = 1; i < fhp->fh_fnum; i++, fxp++) {
1812 if (*cfxp != fixup) {
1813 kprintf("ntfs_procfixups: fixup %d doesn't match\n", i);
1817 cfxp = (u_int16_t *)(((caddr_t) cfxp) + ntmp->ntm_bps);
1824 ntfs_runtocn(cn_t *cn, struct ntfsmount *ntmp, u_int8_t *run, u_long len,
1834 kprintf("ntfs_runtocn: run: 0x%p, %ld bytes, vcn:%ld\n",
1835 run, len, (u_long) vcn);
1836 kprintf("ntfs_runtocn: run: ");
1837 for (i = 0; i < len; i++)
1838 kprintf("0x%02x ", run[i]);
1843 kprintf("ntfs_runtocn: run == NULL\n");
1847 if (run[off] == 0) {
1848 kprintf("ntfs_runtocn: vcn too big\n");
1852 error = ntfs_parserun(&ccn, &ccl, run, len, &off);
1854 kprintf("ntfs_runtocn: ntfs_parserun failed\n");
1857 } while (ccl <= vcn);
1864 * this initializes toupper table & dependant variables to be ready for
1868 ntfs_toupper_init(void)
1870 ntfs_toupper_tab = NULL;
1871 lockinit(&ntfs_toupper_lock, "ntfs_toupper", 0, 0);
1872 ntfs_toupper_usecount = 0;
1876 * if the ntfs_toupper_tab[] is filled already, just raise use count;
1877 * otherwise read the data from the filesystem we are currently mounting
1880 ntfs_toupper_use(struct mount *mp, struct ntfsmount *ntmp)
1885 /* get exclusive access */
1886 LOCKMGR(&ntfs_toupper_lock, LK_EXCLUSIVE);
1888 /* only read the translation data from a file if it hasn't been
1890 if (ntfs_toupper_tab)
1894 * Read in Unicode lowercase -> uppercase translation file.
1895 * XXX for now, just the first 256 entries are used anyway,
1896 * so don't bother reading more
1898 ntfs_toupper_tab = kmalloc(65536 * sizeof(wchar), M_NTFSRDATA,
1901 if ((error = VFS_VGET(mp, NULL, NTFS_UPCASEINO, &vp)))
1903 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
1904 0, 65536*sizeof(wchar), (char *) ntfs_toupper_tab, NULL);
1908 ntfs_toupper_usecount++;
1909 LOCKMGR(&ntfs_toupper_lock, LK_RELEASE);
1914 * lower the use count and if it reaches zero, free the memory
1915 * tied by toupper table
1918 ntfs_toupper_unuse(void)
1920 /* get exclusive access */
1921 LOCKMGR(&ntfs_toupper_lock, LK_EXCLUSIVE);
1923 ntfs_toupper_usecount--;
1924 if (ntfs_toupper_usecount == 0) {
1925 kfree(ntfs_toupper_tab, M_NTFSRDATA);
1926 ntfs_toupper_tab = NULL;
1929 else if (ntfs_toupper_usecount < 0) {
1930 panic("ntfs_toupper_unuse(): use count negative: %d",
1931 ntfs_toupper_usecount);
1935 /* release the lock */
1936 LOCKMGR(&ntfs_toupper_lock, LK_RELEASE);
1940 ntfs_u28_init(struct ntfsmount *ntmp, wchar *u2w, char *cs_local,
1946 if (ntfs_iconv && cs_local) {
1947 ntfs_iconv->open(cs_local, cs_ntfs, &ntmp->ntm_ic_u2l);
1951 u28 = kmalloc(256 * sizeof(char *), M_TEMP, M_WAITOK | M_ZERO);
1953 for (i=0; i<256; i++) {
1954 h = (u2w[i] >> 8) & 0xFF;
1957 if (u28[h] == NULL) {
1958 u28[h] = kmalloc(256 * sizeof(char), M_TEMP, M_WAITOK);
1959 for (j=0; j<256; j++)
1963 u28[h][l] = i & 0xFF;
1966 ntmp->ntm_u28 = u28;
1972 ntfs_u28_uninit(struct ntfsmount *ntmp)
1977 if (ntmp->ntm_u28 == NULL) {
1978 if (ntfs_iconv && ntmp->ntm_ic_u2l) {
1979 ntfs_iconv->close(ntmp->ntm_ic_u2l);
1984 if (ntmp->ntm_u28 == NULL)
1987 u28 = ntmp->ntm_u28;
1989 for (i=0; i<256; i++)
1991 kfree(u28[i], M_TEMP);
1999 ntfs_82u_init(struct ntfsmount *ntmp, char *cs_local, char *cs_ntfs)
2005 if (ntfs_iconv && cs_local) {
2006 ntfs_iconv->open(cs_ntfs, cs_local, &ntmp->ntm_ic_l2u);
2010 _82u = kmalloc(256 * sizeof(wchar), M_TEMP, M_WAITOK);
2012 for (i=0; i<256; i++)
2015 ntmp->ntm_82u = _82u;
2021 ntfs_82u_uninit(struct ntfsmount *ntmp)
2023 if (ntmp->ntm_82u == NULL) {
2024 if (ntfs_iconv && ntmp->ntm_ic_l2u) {
2025 ntfs_iconv->close(ntmp->ntm_ic_l2u);
2030 kfree(ntmp->ntm_82u, M_TEMP);
2035 * maps the Unicode char to 8bit equivalent
2036 * XXX currently only gets lower 8bit from the Unicode char
2037 * and substitutes a '_' for it if the result would be '\0';
2038 * something better has to be definitely though out
2041 ntfs_u28(struct ntfsmount *ntmp, wchar wc)
2043 char *p, *outp, inbuf[3], outbuf[3];
2046 if (ntfs_iconv && ntmp->ntm_ic_u2l) {
2048 inbuf[0] = (char)(wc>>8);
2049 inbuf[1] = (char)wc;
2053 ntfs_iconv->convchr(ntmp->ntm_ic_u2l, (const char **)&p, &ilen,
2056 return ((wchar)(outbuf[0]&0xFF));
2057 } else if (olen == 0) {
2058 return ((wchar)((outbuf[0]<<8) | (outbuf[1]&0xFF)));
2063 p = ntmp->ntm_u28[(wc>>8)&0xFF];
2066 return (p[wc&0xFF]);
2070 ntfs_82u(struct ntfsmount *ntmp,
2074 char *p, *outp, inbuf[3], outbuf[3];
2078 if (ntfs_iconv && ntmp->ntm_ic_l2u) {
2079 ilen = (size_t)*len;
2082 inbuf[0] = (char)(wc>>8);
2083 inbuf[1] = (char)wc;
2087 ntfs_iconv->convchr(ntmp->ntm_ic_l2u, (const char **)&p, &ilen,
2090 uc = (wchar)((outbuf[0]<<8) | (outbuf[1]&0xFF));
2095 if (ntmp->ntm_82u != NULL)
2096 return (ntmp->ntm_82u[wc&0xFF]);