HAMMER: MFC all changes through 20080924
[dragonfly.git] / sys / vfs / ntfs / ntfs_subr.c
blob1ace1149f23b6787480dd9519893efd2b19a2e34
1 /* $NetBSD: ntfs_subr.c,v 1.23 1999/10/31 19:45:26 jdolecek Exp $ */
3 /*-
4 * Copyright (c) 1998, 1999 Semen Ustimenko (semenu@FreeBSD.org)
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
26 * SUCH DAMAGE.
28 * $FreeBSD: src/sys/ntfs/ntfs_subr.c,v 1.7.2.4 2001/10/12 22:08:49 semenu Exp $
29 * $DragonFly: src/sys/vfs/ntfs/ntfs_subr.c,v 1.27 2008/01/05 14:02:41 swildner Exp $
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/systm.h>
35 #include <sys/proc.h>
36 #include <sys/namei.h>
37 #include <sys/kernel.h>
38 #include <sys/vnode.h>
39 #include <sys/mount.h>
40 #include <sys/buf.h>
41 #include <sys/file.h>
42 #include <sys/malloc.h>
43 #include <sys/lock.h>
44 #include <sys/spinlock.h>
45 #include <sys/spinlock2.h>
47 #include <machine/inttypes.h>
49 #if defined(__NetBSD__)
50 #include <miscfs/specfs/specdev.h>
51 #endif
53 /* #define NTFS_DEBUG 1 */
54 #include "ntfs.h"
55 #include "ntfsmount.h"
56 #include "ntfs_inode.h"
57 #include "ntfs_vfsops.h"
58 #include "ntfs_subr.h"
59 #include "ntfs_compr.h"
60 #include "ntfs_ihash.h"
62 #if defined(__DragonFly__)
63 MALLOC_DEFINE(M_NTFSNTVATTR, "NTFS vattr", "NTFS file attribute information");
64 MALLOC_DEFINE(M_NTFSRDATA, "NTFS res data", "NTFS resident data");
65 MALLOC_DEFINE(M_NTFSRUN, "NTFS vrun", "NTFS vrun storage");
66 MALLOC_DEFINE(M_NTFSDECOMP, "NTFS decomp", "NTFS decompression temporary");
67 #endif
69 static int ntfs_ntlookupattr (struct ntfsmount *, const char *, int, int *, char **);
70 static int ntfs_findvattr (struct ntfsmount *, struct ntnode *, struct ntvattr **, struct ntvattr **, u_int32_t, const char *, size_t, cn_t);
71 static int ntfs_uastricmp (struct ntfsmount *, const wchar *, size_t, const char *, size_t);
72 static int ntfs_uastrcmp (struct ntfsmount *, const wchar *, size_t, const char *, size_t);
74 /* table for mapping Unicode chars into uppercase; it's filled upon first
75 * ntfs mount, freed upon last ntfs umount */
76 static wchar *ntfs_toupper_tab;
77 #define NTFS_TOUPPER(ch) (ntfs_toupper_tab[(ch)])
78 static struct lock ntfs_toupper_lock;
79 static signed int ntfs_toupper_usecount;
81 /* support macro for ntfs_ntvattrget() */
82 #define NTFS_AALPCMP(aalp,type,name,namelen) ( \
83 (aalp->al_type == type) && (aalp->al_namelen == namelen) && \
84 !NTFS_UASTRCMP(aalp->al_name,aalp->al_namelen,name,namelen) )
89 int
90 ntfs_ntvattrrele(struct ntvattr *vap)
92 dprintf(("ntfs_ntvattrrele: ino: %"PRId64", type: 0x%x\n",
93 vap->va_ip->i_number, vap->va_type));
95 ntfs_ntrele(vap->va_ip);
97 return (0);
101 * find the attribute in the ntnode
103 static int
104 ntfs_findvattr(struct ntfsmount *ntmp, struct ntnode *ip,
105 struct ntvattr **lvapp, struct ntvattr **vapp, u_int32_t type,
106 const char *name, size_t namelen, cn_t vcn)
108 int error;
109 struct ntvattr *vap;
111 if((ip->i_flag & IN_LOADED) == 0) {
112 dprintf(("ntfs_findvattr: node not loaded, ino: %"PRId64"\n",
113 ip->i_number));
114 error = ntfs_loadntnode(ntmp,ip);
115 if (error) {
116 kprintf("ntfs_findvattr: FAILED TO LOAD INO: %"PRId64"\n",
117 ip->i_number);
118 return (error);
122 *lvapp = NULL;
123 *vapp = NULL;
124 for (vap = ip->i_valist.lh_first; vap; vap = vap->va_list.le_next) {
125 ddprintf(("ntfs_findvattr: type: 0x%x, vcn: %d - %d\n", \
126 vap->va_type, (u_int32_t) vap->va_vcnstart, \
127 (u_int32_t) vap->va_vcnend));
128 if ((vap->va_type == type) &&
129 (vap->va_vcnstart <= vcn) && (vap->va_vcnend >= vcn) &&
130 (vap->va_namelen == namelen) &&
131 (strncmp(name, vap->va_name, namelen) == 0)) {
132 *vapp = vap;
133 ntfs_ntref(vap->va_ip);
134 return (0);
136 if (vap->va_type == NTFS_A_ATTRLIST)
137 *lvapp = vap;
140 return (-1);
144 * Search attribute specifed in ntnode (load ntnode if nessecary).
145 * If not found but ATTR_A_ATTRLIST present, read it in and search throught.
146 * VOP_VGET node needed, and lookup througth it's ntnode (load if nessesary).
148 * ntnode should be locked
151 ntfs_ntvattrget(struct ntfsmount *ntmp, struct ntnode *ip, u_int32_t type,
152 const char *name, cn_t vcn, struct ntvattr **vapp)
154 struct ntvattr *lvap = NULL;
155 struct attr_attrlist *aalp;
156 struct attr_attrlist *nextaalp;
157 struct vnode *newvp;
158 struct ntnode *newip;
159 caddr_t alpool;
160 size_t namelen, len;
161 int error;
163 *vapp = NULL;
165 if (name) {
166 dprintf(("ntfs_ntvattrget: " \
167 "ino: %"PRId64", type: 0x%x, name: %s, vcn: %d\n", \
168 ip->i_number, type, name, (u_int32_t) vcn));
169 namelen = strlen(name);
170 } else {
171 dprintf(("ntfs_ntvattrget: " \
172 "ino: %"PRId64", type: 0x%x, vcn: %d\n", \
173 ip->i_number, type, (u_int32_t) vcn));
174 name = "";
175 namelen = 0;
178 error = ntfs_findvattr(ntmp, ip, &lvap, vapp, type, name, namelen, vcn);
179 if (error >= 0)
180 return (error);
182 if (!lvap) {
183 dprintf(("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: " \
184 "ino: %"PRId64", type: 0x%x, name: %s, vcn: %d\n", \
185 ip->i_number, type, name, (u_int32_t) vcn));
186 return (ENOENT);
188 /* Scan $ATTRIBUTE_LIST for requested attribute */
189 len = lvap->va_datalen;
190 MALLOC(alpool, caddr_t, len, M_TEMP, M_WAITOK);
191 error = ntfs_readntvattr_plain(ntmp, ip, lvap, 0, len, alpool, &len,
192 NULL);
193 if (error)
194 goto out;
196 aalp = (struct attr_attrlist *) alpool;
197 nextaalp = NULL;
199 for(; len > 0; aalp = nextaalp) {
200 dprintf(("ntfs_ntvattrget: " \
201 "attrlist: ino: %d, attr: 0x%x, vcn: %d\n", \
202 aalp->al_inumber, aalp->al_type, \
203 (u_int32_t) aalp->al_vcnstart));
205 if (len > aalp->reclen) {
206 nextaalp = NTFS_NEXTREC(aalp, struct attr_attrlist *);
207 } else {
208 nextaalp = NULL;
210 len -= aalp->reclen;
212 if (!NTFS_AALPCMP(aalp, type, name, namelen) ||
213 (nextaalp && (nextaalp->al_vcnstart <= vcn) &&
214 NTFS_AALPCMP(nextaalp, type, name, namelen)))
215 continue;
217 dprintf(("ntfs_ntvattrget: attribute in ino: %d\n",
218 aalp->al_inumber));
220 /* this is not a main record, so we can't use just plain
221 vget() */
222 error = ntfs_vgetex(ntmp->ntm_mountp, aalp->al_inumber,
223 NTFS_A_DATA, NULL, LK_EXCLUSIVE,
224 VG_EXT, curthread, &newvp);
225 if (error) {
226 kprintf("ntfs_ntvattrget: CAN'T VGET INO: %d\n",
227 aalp->al_inumber);
228 goto out;
230 newip = VTONT(newvp);
231 /* XXX have to lock ntnode */
232 error = ntfs_findvattr(ntmp, newip, &lvap, vapp,
233 type, name, namelen, vcn);
234 vput(newvp);
235 if (error == 0)
236 goto out;
237 kprintf("ntfs_ntvattrget: ATTRLIST ERROR.\n");
238 break;
240 error = ENOENT;
242 dprintf(("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: " \
243 "ino: %"PRId64", type: 0x%x, name: %.*s, vcn: %d\n", \
244 ip->i_number, type, (int) namelen, name, (u_int32_t) vcn));
245 out:
246 FREE(alpool, M_TEMP);
247 return (error);
251 * Read ntnode from disk, make ntvattr list.
253 * ntnode should be locked
256 ntfs_loadntnode(struct ntfsmount *ntmp, struct ntnode *ip)
258 struct filerec *mfrp;
259 daddr_t bn;
260 int error,off;
261 struct attr *ap;
262 struct ntvattr *nvap;
264 dprintf(("ntfs_loadntnode: loading ino: %"PRId64"\n",ip->i_number));
266 MALLOC(mfrp, struct filerec *, ntfs_bntob(ntmp->ntm_bpmftrec),
267 M_TEMP, M_WAITOK);
269 if (ip->i_number < NTFS_SYSNODESNUM) {
270 struct buf *bp;
272 dprintf(("ntfs_loadntnode: read system node\n"));
274 bn = ntfs_cntobn(ntmp->ntm_mftcn) +
275 ntmp->ntm_bpmftrec * ip->i_number;
277 error = bread(ntmp->ntm_devvp,
278 ntfs_bntodoff(bn), ntfs_bntob(ntmp->ntm_bpmftrec), &bp);
279 if (error) {
280 kprintf("ntfs_loadntnode: BREAD FAILED\n");
281 brelse(bp);
282 goto out;
284 memcpy(mfrp, bp->b_data, ntfs_bntob(ntmp->ntm_bpmftrec));
285 bqrelse(bp);
286 } else {
287 struct vnode *vp;
289 vp = ntmp->ntm_sysvn[NTFS_MFTINO];
290 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
291 ip->i_number * ntfs_bntob(ntmp->ntm_bpmftrec),
292 ntfs_bntob(ntmp->ntm_bpmftrec), mfrp, NULL);
293 if (error) {
294 kprintf("ntfs_loadntnode: ntfs_readattr failed\n");
295 goto out;
299 /* Check if magic and fixups are correct */
300 error = ntfs_procfixups(ntmp, NTFS_FILEMAGIC, (caddr_t)mfrp,
301 ntfs_bntob(ntmp->ntm_bpmftrec));
302 if (error) {
303 kprintf("ntfs_loadntnode: BAD MFT RECORD %"PRId64"\n",
304 ip->i_number);
305 goto out;
308 dprintf(("ntfs_loadntnode: load attrs for ino: %"PRId64"\n",ip->i_number));
309 off = mfrp->fr_attroff;
310 ap = (struct attr *) ((caddr_t)mfrp + off);
312 LIST_INIT(&ip->i_valist);
314 while (ap->a_hdr.a_type != -1) {
315 error = ntfs_attrtontvattr(ntmp, &nvap, ap);
316 if (error)
317 break;
318 nvap->va_ip = ip;
320 LIST_INSERT_HEAD(&ip->i_valist, nvap, va_list);
322 off += ap->a_hdr.reclen;
323 ap = (struct attr *) ((caddr_t)mfrp + off);
325 if (error) {
326 kprintf("ntfs_loadntnode: failed to load attr ino: %"PRId64"\n",
327 ip->i_number);
328 goto out;
331 ip->i_mainrec = mfrp->fr_mainrec;
332 ip->i_nlink = mfrp->fr_nlink;
333 ip->i_frflag = mfrp->fr_flags;
335 ip->i_flag |= IN_LOADED;
337 out:
338 FREE(mfrp, M_TEMP);
339 return (error);
343 * Routine locks ntnode and increase usecount, just opposite of
344 * ntfs_ntput().
347 ntfs_ntget(struct ntnode *ip)
349 dprintf(("ntfs_ntget: get ntnode %"PRId64": %p, usecount: %d\n",
350 ip->i_number, ip, ip->i_usecount));
352 ip->i_usecount++; /* ZZZ */
353 LOCKMGR(&ip->i_lock, LK_EXCLUSIVE);
355 return 0;
359 * Routine search ntnode in hash, if found: lock, inc usecount and return.
360 * If not in hash allocate structure for ntnode, prefill it, lock,
361 * inc count and return.
363 * ntnode returned locked
366 ntfs_ntlookup(struct ntfsmount *ntmp, ino_t ino, struct ntnode **ipp)
368 struct ntnode *ip;
370 dprintf(("ntfs_ntlookup: looking for ntnode %d\n", ino));
372 do {
373 if ((ip = ntfs_nthashlookup(ntmp->ntm_dev, ino)) != NULL) {
374 ntfs_ntget(ip);
375 dprintf(("ntfs_ntlookup: ntnode %d: %p, usecount: %d\n",
376 ino, ip, ip->i_usecount));
377 *ipp = ip;
378 return (0);
380 } while (LOCKMGR(&ntfs_hashlock, LK_EXCLUSIVE | LK_SLEEPFAIL));
382 MALLOC(ip, struct ntnode *, sizeof(struct ntnode),
383 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;
389 ip->i_number = ino;
390 ip->i_mp = ntmp;
392 LIST_INIT(&ip->i_fnlist);
393 vref(ip->i_devvp);
395 /* init lock and lock the newborn ntnode */
396 lockinit(&ip->i_lock, "ntnode", 0, LK_EXCLUSIVE);
397 spin_init(&ip->i_interlock);
398 ntfs_ntget(ip);
400 ntfs_nthashins(ip);
402 LOCKMGR(&ntfs_hashlock, LK_RELEASE);
404 *ipp = ip;
406 dprintf(("ntfs_ntlookup: ntnode %d: %p, usecount: %d\n",
407 ino, ip, ip->i_usecount));
409 return (0);
413 * Decrement usecount of ntnode and unlock it, if usecount reach zero,
414 * deallocate ntnode.
416 * ntnode should be locked on entry, and unlocked on return.
418 void
419 ntfs_ntput(struct ntnode *ip)
421 struct ntvattr *vap;
423 dprintf(("ntfs_ntput: rele ntnode %"PRId64": %p, usecount: %d\n",
424 ip->i_number, ip, ip->i_usecount));
426 spin_lock_wr(&ip->i_interlock);
427 ip->i_usecount--;
429 #ifdef DIAGNOSTIC
430 if (ip->i_usecount < 0) {
431 spin_unlock_wr(&ip->i_interlock);
432 panic("ntfs_ntput: ino: %"PRId64" usecount: %d \n",
433 ip->i_number,ip->i_usecount);
435 #endif
437 if (ip->i_usecount > 0) {
438 spin_unlock_wr(&ip->i_interlock);
439 LOCKMGR(&ip->i_lock, LK_RELEASE);
440 return;
443 dprintf(("ntfs_ntput: deallocating ntnode: %"PRId64"\n", ip->i_number));
445 if (ip->i_fnlist.lh_first) {
446 spin_unlock_wr(&ip->i_interlock);
447 panic("ntfs_ntput: ntnode has fnodes\n");
451 * XXX this is a bit iffy because we are making high level calls
452 * while holding a spinlock.
454 ntfs_nthashrem(ip);
456 while ((vap = LIST_FIRST(&ip->i_valist)) != NULL) {
457 LIST_REMOVE(vap,va_list);
458 ntfs_freentvattr(vap);
460 spin_unlock_wr(&ip->i_interlock);
461 vrele(ip->i_devvp);
462 FREE(ip, M_NTFSNTNODE);
466 * increment usecount of ntnode
468 void
469 ntfs_ntref(struct ntnode *ip)
471 ip->i_usecount++;
473 dprintf(("ntfs_ntref: ino %"PRId64", usecount: %d\n",
474 ip->i_number, ip->i_usecount));
479 * Decrement usecount of ntnode.
481 void
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_wr(&ip->i_interlock);
488 ip->i_usecount--;
490 if (ip->i_usecount < 0) {
491 spin_unlock_wr(&ip->i_interlock);
492 panic("ntfs_ntrele: ino: %"PRId64" usecount: %d \n",
493 ip->i_number,ip->i_usecount);
495 spin_unlock_wr(&ip->i_interlock);
499 * Deallocate all memory allocated for ntvattr
501 void
502 ntfs_freentvattr(struct ntvattr *vap)
504 if (vap->va_flag & NTFS_AF_INRUN) {
505 if (vap->va_vruncn)
506 FREE(vap->va_vruncn, M_NTFSRUN);
507 if (vap->va_vruncl)
508 FREE(vap->va_vruncl, M_NTFSRUN);
509 } else {
510 if (vap->va_datap)
511 FREE(vap->va_datap, M_NTFSRDATA);
513 FREE(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,
522 struct attr *rap)
524 int error, i;
525 struct ntvattr *vap;
527 error = 0;
528 *rvapp = NULL;
530 MALLOC(vap, struct ntvattr *, sizeof(struct ntvattr),
531 M_NTFSNTVATTR, M_WAITOK | M_ZERO);
532 vap->va_ip = NULL;
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]));
548 ddprintf(("]"));
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),
558 &(vap->va_vruncnt),
559 (caddr_t) rap + rap->a_nr.a_dataoff);
560 } else {
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 MALLOC(vap->va_datap, caddr_t, vap->va_datalen,
568 M_NTFSRDATA, M_WAITOK);
569 memcpy(vap->va_datap, (caddr_t) rap + rap->a_r.a_dataoff,
570 rap->a_r.a_datalen);
572 ddprintf((", len: %d", vap->va_datalen));
574 if (error)
575 FREE(vap, M_NTFSNTVATTR);
576 else
577 *rvapp = vap;
579 ddprintf(("\n"));
581 return (error);
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)
590 u_int32_t off;
591 u_int32_t sz, i;
592 cn_t *cn;
593 cn_t *cl;
594 u_long cnt;
595 cn_t prev;
596 cn_t tmp;
598 off = 0;
599 cnt = 0;
600 i = 0;
601 while (run[off]) {
602 off += (run[off] & 0xF) + ((run[off] >> 4) & 0xF) + 1;
603 cnt++;
605 MALLOC(cn, cn_t *, cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
606 MALLOC(cl, cn_t *, cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
608 off = 0;
609 cnt = 0;
610 prev = 0;
611 while (run[off]) {
613 sz = run[off++];
614 cl[cnt] = 0;
616 for (i = 0; i < (sz & 0xF); i++)
617 cl[cnt] += (u_int32_t) run[off++] << (i << 3);
619 sz >>= 4;
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);
624 } else {
625 tmp = 0;
626 for (i = 0; i < sz; i++)
627 tmp |= (u_int64_t) run[off++] << (i << 3);
629 if (tmp)
630 prev = cn[cnt] = prev + tmp;
631 else
632 cn[cnt] = tmp;
634 cnt++;
636 *rcnp = cn;
637 *rclp = cl;
638 *rcntp = cnt;
639 return (0);
643 * Compare unicode and ascii string case insens.
645 static int
646 ntfs_uastricmp(struct ntfsmount *ntmp, const wchar *ustr, size_t ustrlen,
647 const char *astr, size_t astrlen)
649 size_t i;
650 int res;
653 * XXX We use NTFS_82U(NTFS_U28(c)) to get rid of unicode
654 * symbols not covered by translation table
656 for (i = 0; i < ustrlen && i < astrlen; i++) {
657 res = ((int) NTFS_TOUPPER(NTFS_82U(NTFS_U28(ustr[i])))) -
658 ((int)NTFS_TOUPPER(NTFS_82U(astr[i])));
659 if (res)
660 return res;
662 return (ustrlen - astrlen);
666 * Compare unicode and ascii string case sens.
668 static int
669 ntfs_uastrcmp(struct ntfsmount *ntmp, const wchar *ustr, size_t ustrlen,
670 const char *astr, size_t astrlen)
672 size_t i;
673 int res;
675 for (i = 0; (i < ustrlen) && (i < astrlen); i++) {
676 res = (int) (((char)NTFS_U28(ustr[i])) - astr[i]);
677 if (res)
678 return res;
680 return (ustrlen - astrlen);
684 * Search fnode in ntnode, if not found allocate and preinitialize.
686 * ntnode should be locked on entry.
689 ntfs_fget(struct ntfsmount *ntmp, struct ntnode *ip, int attrtype,
690 char *attrname, struct fnode **fpp)
692 struct fnode *fp;
694 dprintf(("ntfs_fget: ino: %"PRId64", attrtype: 0x%x, attrname: %s\n",
695 ip->i_number,attrtype, attrname?attrname:""));
696 *fpp = NULL;
697 for (fp = ip->i_fnlist.lh_first; fp != NULL; fp = fp->f_fnlist.le_next){
698 dprintf(("ntfs_fget: fnode: attrtype: %d, attrname: %s\n",
699 fp->f_attrtype, fp->f_attrname?fp->f_attrname:""));
701 if ((attrtype == fp->f_attrtype) &&
702 ((!attrname && !fp->f_attrname) ||
703 (attrname && fp->f_attrname &&
704 !strcmp(attrname,fp->f_attrname)))){
705 dprintf(("ntfs_fget: found existed: %p\n",fp));
706 *fpp = fp;
710 if (*fpp)
711 return (0);
713 MALLOC(fp, struct fnode *, sizeof(struct fnode), M_NTFSFNODE,
714 M_WAITOK | M_ZERO);
715 dprintf(("ntfs_fget: allocating fnode: %p\n",fp));
717 fp->f_ip = ip;
718 if (attrname) {
719 fp->f_flag |= FN_AATTRNAME;
720 MALLOC(fp->f_attrname, char *, strlen(attrname)+1, M_TEMP, M_WAITOK);
721 strcpy(fp->f_attrname, attrname);
722 } else
723 fp->f_attrname = NULL;
724 fp->f_attrtype = attrtype;
726 ntfs_ntref(ip);
728 LIST_INSERT_HEAD(&ip->i_fnlist, fp, f_fnlist);
730 *fpp = fp;
732 return (0);
736 * Deallocate fnode, remove it from ntnode's fnode list.
738 * ntnode should be locked.
740 void
741 ntfs_frele(struct fnode *fp)
743 struct ntnode *ip = FTONT(fp);
745 dprintf(("ntfs_frele: fnode: %p for %"PRId64": %p\n", fp, ip->i_number, ip));
747 dprintf(("ntfs_frele: deallocating fnode\n"));
748 LIST_REMOVE(fp,f_fnlist);
749 if (fp->f_flag & FN_AATTRNAME)
750 FREE(fp->f_attrname, M_TEMP);
751 if (fp->f_dirblbuf)
752 FREE(fp->f_dirblbuf, M_NTFSDIR);
753 FREE(fp, M_NTFSFNODE);
754 ntfs_ntrele(ip);
758 * Lookup attribute name in format: [[:$ATTR_TYPE]:$ATTR_NAME],
759 * $ATTR_TYPE is searched in attrdefs read from $AttrDefs.
760 * If $ATTR_TYPE nott specifed, ATTR_A_DATA assumed.
762 static int
763 ntfs_ntlookupattr(struct ntfsmount *ntmp, const char *name, int namelen,
764 int *attrtype, char **attrname)
766 const char *sys;
767 size_t syslen, i;
768 struct ntvattrdef *adp;
770 if (namelen == 0)
771 return (0);
773 if (name[0] == '$') {
774 sys = name;
775 for (syslen = 0; syslen < namelen; syslen++) {
776 if(sys[syslen] == ':') {
777 name++;
778 namelen--;
779 break;
782 name += syslen;
783 namelen -= syslen;
785 adp = ntmp->ntm_ad;
786 for (i = 0; i < ntmp->ntm_adnum; i++, adp++){
787 if (syslen != adp->ad_namelen ||
788 strncmp(sys, adp->ad_name, syslen) != 0)
789 continue;
791 *attrtype = adp->ad_type;
792 goto out;
794 return (ENOENT);
795 } else
796 *attrtype = NTFS_A_DATA;
798 out:
799 if (namelen) {
800 MALLOC((*attrname), char *, namelen, M_TEMP, M_WAITOK);
801 memcpy((*attrname), name, namelen);
802 (*attrname)[namelen] = '\0';
805 return (0);
809 * Lookup specifed node for filename, matching cnp,
810 * return fnode filled.
813 ntfs_ntlookupfile(struct ntfsmount *ntmp, struct vnode *vp,
814 struct componentname *cnp, struct vnode **vpp)
816 struct fnode *fp = VTOF(vp);
817 struct ntnode *ip = FTONT(fp);
818 struct ntvattr *vap; /* Root attribute */
819 cn_t cn; /* VCN in current attribute */
820 caddr_t rdbuf; /* Buffer to read directory's blocks */
821 u_int32_t blsize;
822 u_int32_t rdsize; /* Length of data to read from current block */
823 struct attr_indexentry *iep;
824 int error, res, anamelen, fnamelen;
825 const char *fname,*aname;
826 u_int32_t aoff;
827 int attrtype = NTFS_A_DATA;
828 char *attrname = NULL;
829 struct fnode *nfp;
830 struct vnode *nvp;
831 enum vtype f_type;
833 error = ntfs_ntget(ip);
834 if (error)
835 return (error);
837 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
838 if (error || (vap->va_flag & NTFS_AF_INRUN))
839 return (ENOTDIR);
841 blsize = vap->va_a_iroot->ir_size;
842 rdsize = vap->va_datalen;
845 * Divide file name into: foofilefoofilefoofile[:attrspec]
846 * Store like this: fname:fnamelen [aname:anamelen]
848 fname = cnp->cn_nameptr;
849 aname = NULL;
850 anamelen = 0;
851 for (fnamelen = 0; fnamelen < cnp->cn_namelen; fnamelen++)
852 if(fname[fnamelen] == ':') {
853 aname = fname + fnamelen + 1;
854 anamelen = cnp->cn_namelen - fnamelen - 1;
855 dprintf(("ntfs_ntlookupfile: %s (%d), attr: %s (%d)\n",
856 fname, fnamelen, aname, anamelen));
857 break;
860 dprintf(("ntfs_ntlookupfile: blksz: %d, rdsz: %d\n", blsize, rdsize));
862 MALLOC(rdbuf, caddr_t, blsize, M_TEMP, M_WAITOK);
864 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXROOT, "$I30",
865 0, rdsize, rdbuf, NULL);
866 if (error)
867 goto fail;
869 aoff = sizeof(struct attr_indexroot);
871 do {
872 iep = (struct attr_indexentry *) (rdbuf + aoff);
874 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
875 aoff += iep->reclen,
876 iep = (struct attr_indexentry *) (rdbuf + aoff))
878 ddprintf(("scan: %d, %d\n",
879 (u_int32_t) iep->ie_number,
880 (u_int32_t) iep->ie_fnametype));
882 /* check the name - the case-insensitible check
883 * has to come first, to break from this for loop
884 * if needed, so we can dive correctly */
885 res = NTFS_UASTRICMP(iep->ie_fname, iep->ie_fnamelen,
886 fname, fnamelen);
887 if (res > 0) break;
888 if (res < 0) continue;
890 if (iep->ie_fnametype == 0 ||
891 !(ntmp->ntm_flag & NTFS_MFLAG_CASEINS))
893 res = NTFS_UASTRCMP(iep->ie_fname,
894 iep->ie_fnamelen, fname, fnamelen);
895 if (res != 0) continue;
898 if (aname) {
899 error = ntfs_ntlookupattr(ntmp,
900 aname, anamelen,
901 &attrtype, &attrname);
902 if (error)
903 goto fail;
906 /* Check if we've found ourself */
907 if ((iep->ie_number == ip->i_number) &&
908 (attrtype == fp->f_attrtype) &&
909 ((!attrname && !fp->f_attrname) ||
910 (attrname && fp->f_attrname &&
911 !strcmp(attrname, fp->f_attrname))))
913 vref(vp);
914 *vpp = vp;
915 error = 0;
916 goto fail;
919 /* vget node, but don't load it */
920 error = ntfs_vgetex(ntmp->ntm_mountp,
921 iep->ie_number, attrtype, attrname,
922 LK_EXCLUSIVE, VG_DONTLOADIN | VG_DONTVALIDFN,
923 curthread, &nvp);
925 /* free the buffer returned by ntfs_ntlookupattr() */
926 if (attrname) {
927 FREE(attrname, M_TEMP);
928 attrname = NULL;
931 if (error)
932 goto fail;
934 nfp = VTOF(nvp);
936 if (nfp->f_flag & FN_VALID) {
937 *vpp = nvp;
938 goto fail;
941 nfp->f_fflag = iep->ie_fflag;
942 nfp->f_pnumber = iep->ie_fpnumber;
943 nfp->f_times = iep->ie_ftimes;
945 if((nfp->f_fflag & NTFS_FFLAG_DIR) &&
946 (nfp->f_attrtype == NTFS_A_DATA) &&
947 (nfp->f_attrname == NULL))
948 f_type = VDIR;
949 else
950 f_type = VREG;
952 nvp->v_type = f_type;
954 if ((nfp->f_attrtype == NTFS_A_DATA) &&
955 (nfp->f_attrname == NULL))
957 /* Opening default attribute */
958 nfp->f_size = iep->ie_fsize;
959 nfp->f_allocated = iep->ie_fallocated;
960 nfp->f_flag |= FN_PRELOADED;
961 } else {
962 error = ntfs_filesize(ntmp, nfp,
963 &nfp->f_size, &nfp->f_allocated);
964 if (error) {
965 vput(nvp);
966 goto fail;
969 nfp->f_flag &= ~FN_VALID;
972 * Normal files use the buffer cache
974 if (nvp->v_type == VREG)
975 vinitvmio(nvp, nfp->f_size);
976 *vpp = nvp;
977 goto fail;
980 /* Dive if possible */
981 if (iep->ie_flag & NTFS_IEFLAG_SUBNODE) {
982 dprintf(("ntfs_ntlookupfile: diving\n"));
984 cn = *(cn_t *) (rdbuf + aoff +
985 iep->reclen - sizeof(cn_t));
986 rdsize = blsize;
988 error = ntfs_readattr(ntmp, ip, NTFS_A_INDX, "$I30",
989 ntfs_cntob(cn), rdsize, rdbuf, NULL);
990 if (error)
991 goto fail;
993 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
994 rdbuf, rdsize);
995 if (error)
996 goto fail;
998 aoff = (((struct attr_indexalloc *) rdbuf)->ia_hdrsize +
999 0x18);
1000 } else {
1001 dprintf(("ntfs_ntlookupfile: nowhere to dive :-(\n"));
1002 error = ENOENT;
1003 break;
1005 } while (1);
1007 dprintf(("finish\n"));
1009 fail:
1010 if (attrname) FREE(attrname, M_TEMP);
1011 ntfs_ntvattrrele(vap);
1012 ntfs_ntput(ip);
1013 FREE(rdbuf, M_TEMP);
1014 return (error);
1018 * Check if name type is permitted to show.
1021 ntfs_isnamepermitted(struct ntfsmount *ntmp, struct attr_indexentry *iep)
1023 if (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)
1024 return 1;
1026 switch (iep->ie_fnametype) {
1027 case 2:
1028 ddprintf(("ntfs_isnamepermitted: skiped DOS name\n"));
1029 return 0;
1030 case 0: case 1: case 3:
1031 return 1;
1032 default:
1033 kprintf("ntfs_isnamepermitted: " \
1034 "WARNING! Unknown file name type: %d\n",
1035 iep->ie_fnametype);
1036 break;
1038 return 0;
1042 * Read ntfs dir like stream of attr_indexentry, not like btree of them.
1043 * This is done by scaning $BITMAP:$I30 for busy clusters and reading them.
1044 * Ofcouse $INDEX_ROOT:$I30 is read before. Last read values are stored in
1045 * fnode, so we can skip toward record number num almost immediatly.
1046 * Anyway this is rather slow routine. The problem is that we don't know
1047 * how many records are there in $INDEX_ALLOCATION:$I30 block.
1050 ntfs_ntreaddir(struct ntfsmount *ntmp, struct fnode *fp,
1051 u_int32_t num, struct attr_indexentry **riepp)
1053 struct ntnode *ip = FTONT(fp);
1054 struct ntvattr *vap = NULL; /* IndexRoot attribute */
1055 struct ntvattr *bmvap = NULL; /* BitMap attribute */
1056 struct ntvattr *iavap = NULL; /* IndexAllocation attribute */
1057 caddr_t rdbuf; /* Buffer to read directory's blocks */
1058 u_char *bmp = NULL; /* Bitmap */
1059 u_int32_t blsize; /* Index allocation size (2048) */
1060 u_int32_t rdsize; /* Length of data to read */
1061 u_int32_t attrnum; /* Current attribute type */
1062 u_int32_t cpbl = 1; /* Clusters per directory block */
1063 u_int32_t blnum;
1064 struct attr_indexentry *iep;
1065 int error = ENOENT;
1066 u_int32_t aoff, cnum;
1068 dprintf(("ntfs_ntreaddir: read ino: %"PRId64", num: %d\n", ip->i_number, num));
1069 error = ntfs_ntget(ip);
1070 if (error)
1071 return (error);
1073 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
1074 if (error)
1075 return (ENOTDIR);
1077 if (fp->f_dirblbuf == NULL) {
1078 fp->f_dirblsz = vap->va_a_iroot->ir_size;
1079 MALLOC(fp->f_dirblbuf, caddr_t,
1080 max(vap->va_datalen,fp->f_dirblsz), M_NTFSDIR, M_WAITOK);
1083 blsize = fp->f_dirblsz;
1084 rdbuf = fp->f_dirblbuf;
1086 dprintf(("ntfs_ntreaddir: rdbuf: 0x%p, blsize: %d\n", rdbuf, blsize));
1088 if (vap->va_a_iroot->ir_flag & NTFS_IRFLAG_INDXALLOC) {
1089 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXBITMAP, "$I30",
1090 0, &bmvap);
1091 if (error) {
1092 error = ENOTDIR;
1093 goto fail;
1095 MALLOC(bmp, u_char *, bmvap->va_datalen, M_TEMP, M_WAITOK);
1096 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXBITMAP, "$I30", 0,
1097 bmvap->va_datalen, bmp, NULL);
1098 if (error)
1099 goto fail;
1101 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDX, "$I30",
1102 0, &iavap);
1103 if (error) {
1104 error = ENOTDIR;
1105 goto fail;
1107 cpbl = ntfs_btocn(blsize + ntfs_cntob(1) - 1);
1108 dprintf(("ntfs_ntreaddir: indexalloc: %d, cpbl: %d\n",
1109 iavap->va_datalen, cpbl));
1110 } else {
1111 dprintf(("ntfs_ntreadidir: w/o BitMap and IndexAllocation\n"));
1112 iavap = bmvap = NULL;
1113 bmp = NULL;
1116 /* Try use previous values */
1117 if ((fp->f_lastdnum < num) && (fp->f_lastdnum != 0)) {
1118 attrnum = fp->f_lastdattr;
1119 aoff = fp->f_lastdoff;
1120 blnum = fp->f_lastdblnum;
1121 cnum = fp->f_lastdnum;
1122 } else {
1123 attrnum = NTFS_A_INDXROOT;
1124 aoff = sizeof(struct attr_indexroot);
1125 blnum = 0;
1126 cnum = 0;
1129 do {
1130 dprintf(("ntfs_ntreaddir: scan: 0x%x, %d, %d, %d, %d\n",
1131 attrnum, (u_int32_t) blnum, cnum, num, aoff));
1132 rdsize = (attrnum == NTFS_A_INDXROOT) ? vap->va_datalen : blsize;
1133 error = ntfs_readattr(ntmp, ip, attrnum, "$I30",
1134 ntfs_cntob(blnum * cpbl), rdsize, rdbuf, NULL);
1135 if (error)
1136 goto fail;
1138 if (attrnum == NTFS_A_INDX) {
1139 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1140 rdbuf, rdsize);
1141 if (error)
1142 goto fail;
1144 if (aoff == 0)
1145 aoff = (attrnum == NTFS_A_INDX) ?
1146 (0x18 + ((struct attr_indexalloc *) rdbuf)->ia_hdrsize) :
1147 sizeof(struct attr_indexroot);
1149 iep = (struct attr_indexentry *) (rdbuf + aoff);
1150 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
1151 aoff += iep->reclen,
1152 iep = (struct attr_indexentry *) (rdbuf + aoff))
1154 if (!ntfs_isnamepermitted(ntmp, iep)) continue;
1156 if (cnum >= num) {
1157 fp->f_lastdnum = cnum;
1158 fp->f_lastdoff = aoff;
1159 fp->f_lastdblnum = blnum;
1160 fp->f_lastdattr = attrnum;
1162 *riepp = iep;
1164 error = 0;
1165 goto fail;
1167 cnum++;
1170 if (iavap) {
1171 if (attrnum == NTFS_A_INDXROOT)
1172 blnum = 0;
1173 else
1174 blnum++;
1176 while (ntfs_cntob(blnum * cpbl) < iavap->va_datalen) {
1177 if (bmp[blnum >> 3] & (1 << (blnum & 3)))
1178 break;
1179 blnum++;
1182 attrnum = NTFS_A_INDX;
1183 aoff = 0;
1184 if (ntfs_cntob(blnum * cpbl) >= iavap->va_datalen)
1185 break;
1186 dprintf(("ntfs_ntreaddir: blnum: %d\n", (u_int32_t) blnum));
1188 } while (iavap);
1190 *riepp = NULL;
1191 fp->f_lastdnum = 0;
1193 fail:
1194 if (vap)
1195 ntfs_ntvattrrele(vap);
1196 if (bmvap)
1197 ntfs_ntvattrrele(bmvap);
1198 if (iavap)
1199 ntfs_ntvattrrele(iavap);
1200 if (bmp)
1201 FREE(bmp, M_TEMP);
1202 ntfs_ntput(ip);
1203 return (error);
1207 * Convert NTFS times that are in 100 ns units and begins from
1208 * 1601 Jan 1 into unix times.
1210 struct timespec
1211 ntfs_nttimetounix(u_int64_t nt)
1213 struct timespec t;
1215 /* WindowNT times are in 100 ns and from 1601 Jan 1 */
1216 t.tv_nsec = (nt % (1000 * 1000 * 10)) * 100;
1217 t.tv_sec = nt / (1000 * 1000 * 10) -
1218 369LL * 365LL * 24LL * 60LL * 60LL -
1219 89LL * 1LL * 24LL * 60LL * 60LL;
1220 return (t);
1224 * Get file times from NTFS_A_NAME attribute.
1227 ntfs_times(struct ntfsmount *ntmp, struct ntnode *ip, ntfs_times_t *tm)
1229 struct ntvattr *vap;
1230 int error;
1232 dprintf(("ntfs_times: ino: %"PRId64"...\n", ip->i_number));
1234 error = ntfs_ntget(ip);
1235 if (error)
1236 return (error);
1238 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_NAME, NULL, 0, &vap);
1239 if (error) {
1240 ntfs_ntput(ip);
1241 return (error);
1243 *tm = vap->va_a_name->n_times;
1244 ntfs_ntvattrrele(vap);
1245 ntfs_ntput(ip);
1247 return (0);
1251 * Get file sizes from corresponding attribute.
1253 * ntnode under fnode should be locked.
1256 ntfs_filesize(struct ntfsmount *ntmp, struct fnode *fp, u_int64_t *size,
1257 u_int64_t *bytes)
1259 struct ntvattr *vap;
1260 struct ntnode *ip = FTONT(fp);
1261 u_int64_t sz, bn;
1262 int error;
1264 dprintf(("ntfs_filesize: ino: %"PRId64"\n", ip->i_number));
1266 error = ntfs_ntvattrget(ntmp, ip,
1267 fp->f_attrtype, fp->f_attrname, 0, &vap);
1268 if (error)
1269 return (error);
1271 bn = vap->va_allocated;
1272 sz = vap->va_datalen;
1274 dprintf(("ntfs_filesize: %d bytes (%d bytes allocated)\n",
1275 (u_int32_t) sz, (u_int32_t) bn));
1277 if (size)
1278 *size = sz;
1279 if (bytes)
1280 *bytes = bn;
1282 ntfs_ntvattrrele(vap);
1284 return (0);
1288 * This is one of write routine.
1291 ntfs_writeattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1292 u_int32_t attrnum, char *attrname, off_t roff,
1293 size_t rsize, void *rdata, size_t *initp,
1294 struct uio *uio)
1296 size_t init;
1297 int error = 0;
1298 off_t off = roff, left = rsize, towrite;
1299 caddr_t data = rdata;
1300 struct ntvattr *vap;
1301 *initp = 0;
1303 while (left) {
1304 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1305 ntfs_btocn(off), &vap);
1306 if (error)
1307 return (error);
1308 towrite = min(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1309 ddprintf(("ntfs_writeattr_plain: o: %d, s: %d (%d - %d)\n",
1310 (u_int32_t) off, (u_int32_t) towrite,
1311 (u_int32_t) vap->va_vcnstart,
1312 (u_int32_t) vap->va_vcnend));
1313 error = ntfs_writentvattr_plain(ntmp, ip, vap,
1314 off - ntfs_cntob(vap->va_vcnstart),
1315 towrite, data, &init, uio);
1316 if (error) {
1317 kprintf("ntfs_writeattr_plain: " \
1318 "ntfs_writentvattr_plain failed: o: %d, s: %d\n",
1319 (u_int32_t) off, (u_int32_t) towrite);
1320 kprintf("ntfs_writeattr_plain: attrib: %d - %d\n",
1321 (u_int32_t) vap->va_vcnstart,
1322 (u_int32_t) vap->va_vcnend);
1323 ntfs_ntvattrrele(vap);
1324 break;
1326 ntfs_ntvattrrele(vap);
1327 left -= towrite;
1328 off += towrite;
1329 data = data + towrite;
1330 *initp += init;
1333 return (error);
1337 * This is one of write routine.
1339 * ntnode should be locked.
1342 ntfs_writentvattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1343 struct ntvattr *vap, off_t roff, size_t rsize,
1344 void *rdata, size_t *initp, struct uio *uio)
1346 int error = 0;
1347 int off;
1348 int cnt;
1349 cn_t ccn, ccl, cn, left, cl;
1350 caddr_t data = rdata;
1351 struct buf *bp;
1352 size_t tocopy;
1354 *initp = 0;
1356 if ((vap->va_flag & NTFS_AF_INRUN) == 0) {
1357 kprintf("ntfs_writevattr_plain: CAN'T WRITE RES. ATTRIBUTE\n");
1358 return ENOTTY;
1361 ddprintf(("ntfs_writentvattr_plain: data in run: %ld chains\n",
1362 vap->va_vruncnt));
1364 off = roff;
1365 left = rsize;
1366 ccl = 0;
1367 ccn = 0;
1368 cnt = 0;
1369 for (; left && (cnt < vap->va_vruncnt); cnt++) {
1370 ccn = vap->va_vruncn[cnt];
1371 ccl = vap->va_vruncl[cnt];
1373 ddprintf(("ntfs_writentvattr_plain: " \
1374 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1375 (u_int32_t) left, (u_int32_t) ccn, \
1376 (u_int32_t) ccl, (u_int32_t) off));
1378 if (ntfs_cntob(ccl) < off) {
1379 off -= ntfs_cntob(ccl);
1380 cnt++;
1381 continue;
1383 if (!ccn && ip->i_number != NTFS_BOOTINO)
1384 continue; /* XXX */
1386 ccl -= ntfs_btocn(off);
1387 cn = ccn + ntfs_btocn(off);
1388 off = ntfs_btocnoff(off);
1390 while (left && ccl) {
1391 #if defined(__DragonFly__)
1392 tocopy = min(left,
1393 min(ntfs_cntob(ccl) - off, MAXBSIZE - off));
1394 #else
1395 /* under NetBSD, bread() can read
1396 * maximum one block worth of data */
1397 tocopy = min(left, ntmp->ntm_bps - off);
1398 #endif
1399 cl = ntfs_btocl(tocopy + off);
1400 ddprintf(("ntfs_writentvattr_plain: write: " \
1401 "cn: 0x%x cl: %d, off: %d len: %d, left: %d\n",
1402 (u_int32_t) cn, (u_int32_t) cl,
1403 (u_int32_t) off, (u_int32_t) tocopy,
1404 (u_int32_t) left));
1405 if (off == 0 && tocopy == ntfs_cntob(cl) &&
1406 uio->uio_segflg != UIO_NOCOPY) {
1407 bp = getblk(ntmp->ntm_devvp, ntfs_cntodoff(cn),
1408 ntfs_cntob(cl), 0, 0);
1409 clrbuf(bp);
1410 } else {
1411 error = bread(ntmp->ntm_devvp,
1412 ntfs_cntodoff(cn),
1413 ntfs_cntob(cl), &bp);
1414 if (error) {
1415 brelse(bp);
1416 return (error);
1419 if (uio)
1420 uiomove(bp->b_data + off, tocopy, uio);
1421 else
1422 memcpy(bp->b_data + off, data, tocopy);
1423 bawrite(bp);
1424 data = data + tocopy;
1425 *initp += tocopy;
1426 off = 0;
1427 left -= tocopy;
1428 cn += cl;
1429 ccl -= cl;
1433 if (left) {
1434 kprintf("ntfs_writentvattr_plain: POSSIBLE RUN ERROR\n");
1435 error = EINVAL;
1438 return (error);
1442 * This is one of read routines.
1444 * ntnode should be locked.
1447 ntfs_readntvattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1448 struct ntvattr *vap, off_t roff, size_t rsize,
1449 void *rdata, size_t *initp, struct uio *uio)
1451 int error = 0;
1452 int off;
1454 *initp = 0;
1455 if (vap->va_flag & NTFS_AF_INRUN) {
1456 int cnt;
1457 cn_t ccn, ccl, cn, left, cl;
1458 caddr_t data = rdata;
1459 struct buf *bp;
1460 size_t tocopy;
1462 ddprintf(("ntfs_readntvattr_plain: data in run: %ld chains\n",
1463 vap->va_vruncnt));
1465 off = roff;
1466 left = rsize;
1467 ccl = 0;
1468 ccn = 0;
1469 cnt = 0;
1470 while (left && (cnt < vap->va_vruncnt)) {
1471 ccn = vap->va_vruncn[cnt];
1472 ccl = vap->va_vruncl[cnt];
1474 ddprintf(("ntfs_readntvattr_plain: " \
1475 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1476 (u_int32_t) left, (u_int32_t) ccn, \
1477 (u_int32_t) ccl, (u_int32_t) off));
1479 if (ntfs_cntob(ccl) < off) {
1480 off -= ntfs_cntob(ccl);
1481 cnt++;
1482 continue;
1484 if (ccn || ip->i_number == NTFS_BOOTINO) {
1485 ccl -= ntfs_btocn(off);
1486 cn = ccn + ntfs_btocn(off);
1487 off = ntfs_btocnoff(off);
1489 while (left && ccl) {
1490 #if defined(__DragonFly__)
1491 tocopy = min(left,
1492 min(ntfs_cntob(ccl) - off,
1493 MAXBSIZE - off));
1494 #else
1495 /* under NetBSD, bread() can read
1496 * maximum one block worth of data */
1497 tocopy = min(left,
1498 ntmp->ntm_bps - off);
1499 #endif
1500 cl = ntfs_btocl(tocopy + off);
1501 ddprintf(("ntfs_readntvattr_plain: " \
1502 "read: cn: 0x%x cl: %d, " \
1503 "off: %d len: %d, left: %d\n",
1504 (u_int32_t) cn,
1505 (u_int32_t) cl,
1506 (u_int32_t) off,
1507 (u_int32_t) tocopy,
1508 (u_int32_t) left));
1509 error = bread(ntmp->ntm_devvp,
1510 ntfs_cntodoff(cn),
1511 ntfs_cntob(cl),
1512 &bp);
1513 if (error) {
1514 brelse(bp);
1515 return (error);
1517 if (uio) {
1518 uiomove(bp->b_data + off,
1519 tocopy, uio);
1520 } else {
1521 memcpy(data, bp->b_data + off,
1522 tocopy);
1524 brelse(bp);
1525 data = data + tocopy;
1526 *initp += tocopy;
1527 off = 0;
1528 left -= tocopy;
1529 cn += cl;
1530 ccl -= cl;
1532 } else {
1533 tocopy = min(left, ntfs_cntob(ccl) - off);
1534 ddprintf(("ntfs_readntvattr_plain: "
1535 "hole: ccn: 0x%x ccl: %d, off: %d, " \
1536 " len: %d, left: %d\n",
1537 (u_int32_t) ccn, (u_int32_t) ccl,
1538 (u_int32_t) off, (u_int32_t) tocopy,
1539 (u_int32_t) left));
1540 left -= tocopy;
1541 off = 0;
1542 if (uio) {
1543 size_t remains = tocopy;
1544 for(; remains; remains++)
1545 uiomove("", 1, uio);
1546 } else
1547 bzero(data, tocopy);
1548 data = data + tocopy;
1550 cnt++;
1552 if (left) {
1553 kprintf("ntfs_readntvattr_plain: POSSIBLE RUN ERROR\n");
1554 error = E2BIG;
1556 } else {
1557 ddprintf(("ntfs_readnvattr_plain: data is in mft record\n"));
1558 if (uio)
1559 uiomove(vap->va_datap + roff, rsize, uio);
1560 else
1561 memcpy(rdata, vap->va_datap + roff, rsize);
1562 *initp += rsize;
1565 return (error);
1569 * This is one of read routines.
1572 ntfs_readattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1573 u_int32_t attrnum, char *attrname, off_t roff,
1574 size_t rsize, void *rdata, size_t * initp,
1575 struct uio *uio)
1577 size_t init;
1578 int error = 0;
1579 off_t off = roff, left = rsize, toread;
1580 caddr_t data = rdata;
1581 struct ntvattr *vap;
1582 *initp = 0;
1584 while (left) {
1585 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1586 ntfs_btocn(off), &vap);
1587 if (error)
1588 return (error);
1589 toread = min(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1590 ddprintf(("ntfs_readattr_plain: o: %d, s: %d (%d - %d)\n",
1591 (u_int32_t) off, (u_int32_t) toread,
1592 (u_int32_t) vap->va_vcnstart,
1593 (u_int32_t) vap->va_vcnend));
1594 error = ntfs_readntvattr_plain(ntmp, ip, vap,
1595 off - ntfs_cntob(vap->va_vcnstart),
1596 toread, data, &init, uio);
1597 if (error) {
1598 kprintf("ntfs_readattr_plain: " \
1599 "ntfs_readntvattr_plain failed: o: %d, s: %d\n",
1600 (u_int32_t) off, (u_int32_t) toread);
1601 kprintf("ntfs_readattr_plain: attrib: %d - %d\n",
1602 (u_int32_t) vap->va_vcnstart,
1603 (u_int32_t) vap->va_vcnend);
1604 ntfs_ntvattrrele(vap);
1605 break;
1607 ntfs_ntvattrrele(vap);
1608 left -= toread;
1609 off += toread;
1610 data = data + toread;
1611 *initp += init;
1614 return (error);
1618 * This is one of read routines.
1621 ntfs_readattr(struct ntfsmount *ntmp, struct ntnode *ip, u_int32_t attrnum,
1622 char *attrname, off_t roff, size_t rsize, void *rdata,
1623 struct uio *uio)
1625 int error = 0;
1626 struct ntvattr *vap;
1627 size_t init;
1629 ddprintf(("ntfs_readattr: reading %"PRId64": 0x%x, from %d size %d bytes\n",
1630 ip->i_number, attrnum, (u_int32_t) roff, (u_int32_t) rsize));
1632 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname, 0, &vap);
1633 if (error)
1634 return (error);
1636 if ((roff > vap->va_datalen) ||
1637 (roff + rsize > vap->va_datalen)) {
1638 ddprintf(("ntfs_readattr: offset too big\n"));
1639 ntfs_ntvattrrele(vap);
1640 return (E2BIG);
1642 if (vap->va_compression && vap->va_compressalg) {
1643 u_int8_t *cup;
1644 u_int8_t *uup;
1645 off_t off = roff, left = rsize, tocopy;
1646 caddr_t data = rdata;
1647 cn_t cn;
1649 ddprintf(("ntfs_ntreadattr: compression: %d\n",
1650 vap->va_compressalg));
1652 MALLOC(cup, u_int8_t *, ntfs_cntob(NTFS_COMPUNIT_CL),
1653 M_NTFSDECOMP, M_WAITOK);
1654 MALLOC(uup, u_int8_t *, ntfs_cntob(NTFS_COMPUNIT_CL),
1655 M_NTFSDECOMP, M_WAITOK);
1657 cn = (ntfs_btocn(roff)) & (~(NTFS_COMPUNIT_CL - 1));
1658 off = roff - ntfs_cntob(cn);
1660 while (left) {
1661 error = ntfs_readattr_plain(ntmp, ip, attrnum,
1662 attrname, ntfs_cntob(cn),
1663 ntfs_cntob(NTFS_COMPUNIT_CL),
1664 cup, &init, NULL);
1665 if (error)
1666 break;
1668 tocopy = min(left, ntfs_cntob(NTFS_COMPUNIT_CL) - off);
1670 if (init == ntfs_cntob(NTFS_COMPUNIT_CL)) {
1671 if (uio)
1672 uiomove(cup + off, tocopy, uio);
1673 else
1674 memcpy(data, cup + off, tocopy);
1675 } else if (init == 0) {
1676 if (uio) {
1677 size_t remains = tocopy;
1678 for(; remains; remains--)
1679 uiomove("", 1, uio);
1681 else
1682 bzero(data, tocopy);
1683 } else {
1684 error = ntfs_uncompunit(ntmp, uup, cup);
1685 if (error)
1686 break;
1687 if (uio)
1688 uiomove(uup + off, tocopy, uio);
1689 else
1690 memcpy(data, uup + off, tocopy);
1693 left -= tocopy;
1694 data = data + tocopy;
1695 off += tocopy - ntfs_cntob(NTFS_COMPUNIT_CL);
1696 cn += NTFS_COMPUNIT_CL;
1699 FREE(uup, M_NTFSDECOMP);
1700 FREE(cup, M_NTFSDECOMP);
1701 } else
1702 error = ntfs_readattr_plain(ntmp, ip, attrnum, attrname,
1703 roff, rsize, rdata, &init, uio);
1704 ntfs_ntvattrrele(vap);
1705 return (error);
1708 #if UNUSED_CODE
1710 ntfs_parserun(cn_t *cn, cn_t *cl, u_int8_t *run, u_long len, u_long *off)
1712 u_int8_t sz;
1713 int i;
1715 if (NULL == run) {
1716 kprintf("ntfs_parsetun: run == NULL\n");
1717 return (EINVAL);
1719 sz = run[(*off)++];
1720 if (0 == sz) {
1721 kprintf("ntfs_parserun: trying to go out of run\n");
1722 return (E2BIG);
1724 *cl = 0;
1725 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1726 kprintf("ntfs_parserun: " \
1727 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1728 sz, len, *off);
1729 return (EINVAL);
1731 for (i = 0; i < (sz & 0xF); i++)
1732 *cl += (u_int32_t) run[(*off)++] << (i << 3);
1734 sz >>= 4;
1735 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1736 kprintf("ntfs_parserun: " \
1737 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1738 sz, len, *off);
1739 return (EINVAL);
1741 for (i = 0; i < (sz & 0xF); i++)
1742 *cn += (u_int32_t) run[(*off)++] << (i << 3);
1744 return (0);
1746 #endif
1749 * Process fixup routine on given buffer.
1752 ntfs_procfixups(struct ntfsmount *ntmp, u_int32_t magic, caddr_t buf,
1753 size_t len)
1755 struct fixuphdr *fhp = (struct fixuphdr *) buf;
1756 int i;
1757 u_int16_t fixup;
1758 u_int16_t *fxp;
1759 u_int16_t *cfxp;
1761 if (fhp->fh_magic != magic) {
1762 kprintf("ntfs_procfixups: magic doesn't match: %08x != %08x\n",
1763 fhp->fh_magic, magic);
1764 return (EINVAL);
1766 if ((fhp->fh_fnum - 1) * ntmp->ntm_bps != len) {
1767 kprintf("ntfs_procfixups: " \
1768 "bad fixups number: %d for %ld bytes block\n",
1769 fhp->fh_fnum, (long)len); /* XXX kprintf kludge */
1770 return (EINVAL);
1772 if (fhp->fh_foff >= ntmp->ntm_spc * ntmp->ntm_mftrecsz * ntmp->ntm_bps) {
1773 kprintf("ntfs_procfixups: invalid offset: %x", fhp->fh_foff);
1774 return (EINVAL);
1776 fxp = (u_int16_t *) (buf + fhp->fh_foff);
1777 cfxp = (u_int16_t *) (buf + ntmp->ntm_bps - 2);
1778 fixup = *fxp++;
1779 for (i = 1; i < fhp->fh_fnum; i++, fxp++) {
1780 if (*cfxp != fixup) {
1781 kprintf("ntfs_procfixups: fixup %d doesn't match\n", i);
1782 return (EINVAL);
1784 *cfxp = *fxp;
1785 cfxp = (u_int16_t *)(((caddr_t) cfxp) + ntmp->ntm_bps);
1787 return (0);
1790 #if UNUSED_CODE
1792 ntfs_runtocn(cn_t *cn, struct ntfsmount *ntmp, u_int8_t *run, u_long len,
1793 cn_t vcn)
1795 cn_t ccn = 0;
1796 cn_t ccl = 0;
1797 u_long off = 0;
1798 int error = 0;
1800 #if NTFS_DEBUG
1801 int i;
1802 kprintf("ntfs_runtocn: run: 0x%p, %ld bytes, vcn:%ld\n",
1803 run, len, (u_long) vcn);
1804 kprintf("ntfs_runtocn: run: ");
1805 for (i = 0; i < len; i++)
1806 kprintf("0x%02x ", run[i]);
1807 kprintf("\n");
1808 #endif
1810 if (NULL == run) {
1811 kprintf("ntfs_runtocn: run == NULL\n");
1812 return (EINVAL);
1814 do {
1815 if (run[off] == 0) {
1816 kprintf("ntfs_runtocn: vcn too big\n");
1817 return (E2BIG);
1819 vcn -= ccl;
1820 error = ntfs_parserun(&ccn, &ccl, run, len, &off);
1821 if (error) {
1822 kprintf("ntfs_runtocn: ntfs_parserun failed\n");
1823 return (error);
1825 } while (ccl <= vcn);
1826 *cn = ccn + vcn;
1827 return (0);
1829 #endif
1832 * this initializes toupper table & dependant variables to be ready for
1833 * later work
1835 void
1836 ntfs_toupper_init(void)
1838 ntfs_toupper_tab = (wchar *) NULL;
1839 lockinit(&ntfs_toupper_lock, "ntfs_toupper", 0, 0);
1840 ntfs_toupper_usecount = 0;
1844 * if the ntfs_toupper_tab[] is filled already, just raise use count;
1845 * otherwise read the data from the filesystem we are currently mounting
1848 ntfs_toupper_use(struct mount *mp, struct ntfsmount *ntmp)
1850 int error = 0;
1851 struct vnode *vp;
1853 /* get exclusive access */
1854 LOCKMGR(&ntfs_toupper_lock, LK_EXCLUSIVE);
1856 /* only read the translation data from a file if it hasn't been
1857 * read already */
1858 if (ntfs_toupper_tab)
1859 goto out;
1862 * Read in Unicode lowercase -> uppercase translation file.
1863 * XXX for now, just the first 256 entries are used anyway,
1864 * so don't bother reading more
1866 MALLOC(ntfs_toupper_tab, wchar *, 65536 * sizeof(wchar),
1867 M_NTFSRDATA, M_WAITOK);
1869 if ((error = VFS_VGET(mp, NTFS_UPCASEINO, &vp)))
1870 goto out;
1871 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
1872 0, 65536*sizeof(wchar), (char *) ntfs_toupper_tab, NULL);
1873 vput(vp);
1875 out:
1876 ntfs_toupper_usecount++;
1877 LOCKMGR(&ntfs_toupper_lock, LK_RELEASE);
1878 return (error);
1882 * lower the use count and if it reaches zero, free the memory
1883 * tied by toupper table
1885 void
1886 ntfs_toupper_unuse(void)
1888 /* get exclusive access */
1889 LOCKMGR(&ntfs_toupper_lock, LK_EXCLUSIVE);
1891 ntfs_toupper_usecount--;
1892 if (ntfs_toupper_usecount == 0) {
1893 FREE(ntfs_toupper_tab, M_NTFSRDATA);
1894 ntfs_toupper_tab = NULL;
1896 #ifdef DIAGNOSTIC
1897 else if (ntfs_toupper_usecount < 0) {
1898 panic("ntfs_toupper_unuse(): use count negative: %d\n",
1899 ntfs_toupper_usecount);
1901 #endif
1903 /* release the lock */
1904 LOCKMGR(&ntfs_toupper_lock, LK_RELEASE);
1908 ntfs_u28_init(struct ntfsmount *ntmp, wchar *u2w)
1910 char ** u28;
1911 int i, j, h, l;
1913 MALLOC(u28, char **, 256 * sizeof(char*), M_TEMP, M_WAITOK | M_ZERO);
1915 for (i=0; i<256; i++) {
1916 h = (u2w[i] >> 8) & 0xFF;
1917 l = (u2w[i]) &0xFF;
1919 if (u28[h] == NULL) {
1920 MALLOC(u28[h], char *, 256 * sizeof(char), M_TEMP, M_WAITOK);
1921 for (j=0; j<256; j++)
1922 u28[h][j] = '_';
1925 u28[h][l] = i & 0xFF;
1928 ntmp->ntm_u28 = u28;
1930 return (0);
1934 ntfs_u28_uninit(struct ntfsmount *ntmp)
1936 char ** u28;
1937 int i;
1939 if (ntmp->ntm_u28 == NULL)
1940 return (0);
1942 u28 = ntmp->ntm_u28;
1944 for (i=0; i<256; i++)
1945 if (u28[i] != NULL)
1946 FREE(u28[i], M_TEMP);
1948 FREE(u28, M_TEMP);
1950 return (0);
1954 ntfs_82u_init(struct ntfsmount *ntmp, u_int16_t *u2w)
1956 wchar * _82u;
1957 int i;
1959 MALLOC(_82u, wchar *, 256 * sizeof(wchar), M_TEMP, M_WAITOK);
1961 if (u2w == NULL) {
1962 for (i=0; i<256; i++)
1963 _82u[i] = i;
1964 } else {
1965 for (i=0; i<128; i++)
1966 _82u[i] = i;
1967 for (i=0; i<128; i++)
1968 _82u[i+128] = u2w[i];
1971 ntmp->ntm_82u = _82u;
1973 return (0);
1977 ntfs_82u_uninit(struct ntfsmount *ntmp)
1979 FREE(ntmp->ntm_82u, M_TEMP);
1980 return (0);
1984 * maps the Unicode char to 8bit equivalent
1985 * XXX currently only gets lower 8bit from the Unicode char
1986 * and substitutes a '_' for it if the result would be '\0';
1987 * something better has to be definitely though out
1989 char
1990 ntfs_u28(struct ntfsmount *ntmp, wchar wc)
1992 char * p;
1994 p = ntmp->ntm_u28[(wc>>8)&0xFF];
1995 if (p == NULL)
1996 return ('_');
1997 return (p[wc&0xFF]);