ntfs_subr.c revision 1.29 1 /* $NetBSD: ntfs_subr.c,v 1.29 2007/02/15 16:18:23 ad Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 1999 Semen Ustimenko (semenu (at) FreeBSD.org)
5 * All rights reserved.
6 *
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.
15 *
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.
27 *
28 * Id: ntfs_subr.c,v 1.4 1999/05/12 09:43:01 semenu Exp
29 */
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: ntfs_subr.c,v 1.29 2007/02/15 16:18:23 ad Exp $");
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/namei.h>
37 #include <sys/proc.h>
38 #include <sys/kernel.h>
39 #include <sys/vnode.h>
40 #include <sys/mount.h>
41 #include <sys/buf.h>
42 #include <sys/file.h>
43 #include <sys/malloc.h>
44 #include <sys/lock.h>
45 #include <sys/kauth.h>
46 #if defined(__FreeBSD__)
47 #include <machine/clock.h>
48 #endif
49
50 #include <miscfs/specfs/specdev.h>
51
52 #include <fs/ntfs/ntfs.h>
53 #include <fs/ntfs/ntfsmount.h>
54 #include <fs/ntfs/ntfs_inode.h>
55 #include <fs/ntfs/ntfs_vfsops.h>
56 #include <fs/ntfs/ntfs_subr.h>
57 #include <fs/ntfs/ntfs_compr.h>
58 #include <fs/ntfs/ntfs_ihash.h>
59
60 #ifdef NTFS_DEBUG
61 int ntfs_debug = NTFS_DEBUG;
62 #endif
63
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");
68
69 /* Local struct used in ntfs_ntlookupfile() */
70 struct ntfs_lookup_ctx {
71 u_int32_t aoff;
72 u_int32_t rdsize;
73 cn_t cn;
74 struct ntfs_lookup_ctx *prev;
75 };
76
77 static int ntfs_ntlookupattr(struct ntfsmount *, const char *, int,
78 int *, char **);
79 static int ntfs_findvattr(struct ntfsmount *, struct ntnode *,
80 struct ntvattr **, struct ntvattr **, u_int32_t, const char *,
81 size_t, cn_t);
82 static int ntfs_uastricmp(struct ntfsmount *, const wchar *, size_t,
83 const char *, size_t);
84 static int ntfs_uastrcmp(struct ntfsmount *, const wchar *, size_t,
85 const char *, size_t);
86
87 /* table for mapping Unicode chars into uppercase; it's filled upon first
88 * ntfs mount, freed upon last ntfs umount */
89 static wchar *ntfs_toupper_tab;
90 #define NTFS_U28(ch) ((((ch) & 0xE0) == 0) ? '_' : (ch) & 0xFF)
91 #define NTFS_TOUPPER(ch) (ntfs_toupper_tab[(unsigned char)(ch)])
92 static kmutex_t ntfs_toupper_lock;
93 static signed int ntfs_toupper_usecount;
94
95 /* support macro for ntfs_ntvattrget() */
96 #define NTFS_AALPCMP(aalp,type,name,namelen) ( \
97 (aalp->al_type == type) && (aalp->al_namelen == namelen) && \
98 !ntfs_uastrcmp(ntmp, aalp->al_name,aalp->al_namelen,name,namelen) )
99
100 /*
101 *
102 */
103 int
104 ntfs_ntvattrrele(vap)
105 struct ntvattr * vap;
106 {
107 dprintf(("ntfs_ntvattrrele: ino: %llu, type: 0x%x\n",
108 (unsigned long long)vap->va_ip->i_number, vap->va_type));
109
110 ntfs_ntrele(vap->va_ip);
111
112 return (0);
113 }
114
115 /*
116 * find the attribute in the ntnode
117 */
118 static int
119 ntfs_findvattr(ntmp, ip, lvapp, vapp, type, name, namelen, vcn)
120 struct ntfsmount *ntmp;
121 struct ntnode *ip;
122 struct ntvattr **lvapp, **vapp;
123 u_int32_t type;
124 const char *name;
125 size_t namelen;
126 cn_t vcn;
127 {
128 int error;
129 struct ntvattr *vap;
130
131 if((ip->i_flag & IN_LOADED) == 0) {
132 dprintf(("ntfs_findvattr: node not loaded, ino: %llu\n",
133 (unsigned long long)ip->i_number));
134 error = ntfs_loadntnode(ntmp,ip);
135 if (error) {
136 printf("ntfs_findvattr: FAILED TO LOAD INO: %llu\n",
137 (unsigned long long)ip->i_number);
138 return (error);
139 }
140 }
141
142 *lvapp = NULL;
143 *vapp = NULL;
144 for (vap = ip->i_valist.lh_first; vap; vap = vap->va_list.le_next) {
145 ddprintf(("ntfs_findvattr: type: 0x%x, vcn: %qu - %qu\n",
146 vap->va_type, (long long) vap->va_vcnstart,
147 (long long) vap->va_vcnend));
148 if ((vap->va_type == type) &&
149 (vap->va_vcnstart <= vcn) && (vap->va_vcnend >= vcn) &&
150 (vap->va_namelen == namelen) &&
151 (strncmp(name, vap->va_name, namelen) == 0)) {
152 *vapp = vap;
153 ntfs_ntref(vap->va_ip);
154 return (0);
155 }
156 if (vap->va_type == NTFS_A_ATTRLIST)
157 *lvapp = vap;
158 }
159
160 return (-1);
161 }
162
163 /*
164 * Search attribute specified in ntnode (load ntnode if necessary).
165 * If not found but ATTR_A_ATTRLIST present, read it in and search through.
166 * VOP_VGET node needed, and lookup through its ntnode (load if nessesary).
167 *
168 * ntnode should be locked
169 */
170 int
171 ntfs_ntvattrget(
172 struct ntfsmount * ntmp,
173 struct ntnode * ip,
174 u_int32_t type,
175 const char *name,
176 cn_t vcn,
177 struct ntvattr ** vapp)
178 {
179 struct ntvattr *lvap = NULL;
180 struct attr_attrlist *aalp;
181 struct attr_attrlist *nextaalp;
182 struct vnode *newvp;
183 struct ntnode *newip;
184 caddr_t alpool;
185 size_t namelen, len;
186 int error;
187
188 *vapp = NULL;
189
190 if (name) {
191 dprintf(("ntfs_ntvattrget: "
192 "ino: %llu, type: 0x%x, name: %s, vcn: %qu\n",
193 (unsigned long long)ip->i_number, type, name,
194 (long long)vcn));
195 namelen = strlen(name);
196 } else {
197 dprintf(("ntfs_ntvattrget: "
198 "ino: %llu, type: 0x%x, vcn: %qu\n",
199 (unsigned long long)ip->i_number, type, (long long)vcn));
200 name = "";
201 namelen = 0;
202 }
203
204 error = ntfs_findvattr(ntmp, ip, &lvap, vapp, type, name, namelen, vcn);
205 if (error >= 0)
206 return (error);
207
208 if (!lvap) {
209 dprintf(("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: "
210 "ino: %llu, type: 0x%x, name: %s, vcn: %qu\n",
211 (unsigned long long)ip->i_number, type, name,
212 (long long)vcn));
213 return (ENOENT);
214 }
215 /* Scan $ATTRIBUTE_LIST for requested attribute */
216 len = lvap->va_datalen;
217 alpool = (caddr_t) malloc(len, M_TEMP, M_WAITOK);
218 error = ntfs_readntvattr_plain(ntmp, ip, lvap, 0, len, alpool, &len,
219 NULL);
220 if (error)
221 goto out;
222
223 aalp = (struct attr_attrlist *) alpool;
224 nextaalp = NULL;
225
226 for(; len > 0; aalp = nextaalp) {
227 KASSERT(aalp != NULL);
228 dprintf(("ntfs_ntvattrget: "
229 "attrlist: ino: %d, attr: 0x%x, vcn: %qu\n",
230 aalp->al_inumber, aalp->al_type,
231 (long long) aalp->al_vcnstart));
232
233 if (len > aalp->reclen) {
234 nextaalp = NTFS_NEXTREC(aalp, struct attr_attrlist *);
235 } else {
236 nextaalp = NULL;
237 }
238 len -= aalp->reclen;
239
240 if (!NTFS_AALPCMP(aalp, type, name, namelen) ||
241 (nextaalp && (nextaalp->al_vcnstart <= vcn) &&
242 NTFS_AALPCMP(nextaalp, type, name, namelen)))
243 continue;
244
245 dprintf(("ntfs_ntvattrget: attribute in ino: %d\n",
246 aalp->al_inumber));
247
248 /* this is not a main record, so we can't use just plain
249 vget() */
250 error = ntfs_vgetex(ntmp->ntm_mountp, aalp->al_inumber,
251 NTFS_A_DATA, NULL, LK_EXCLUSIVE,
252 VG_EXT, &newvp);
253 if (error) {
254 printf("ntfs_ntvattrget: CAN'T VGET INO: %d\n",
255 aalp->al_inumber);
256 goto out;
257 }
258 newip = VTONT(newvp);
259 /* XXX have to lock ntnode */
260 error = ntfs_findvattr(ntmp, newip, &lvap, vapp,
261 type, name, namelen, vcn);
262 vput(newvp);
263 if (error == 0)
264 goto out;
265 printf("ntfs_ntvattrget: ATTRLIST ERROR.\n");
266 break;
267 }
268 error = ENOENT;
269
270 dprintf(("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: "
271 "ino: %llu, type: 0x%x, name: %.*s, vcn: %qu\n",
272 (unsigned long long)ip->i_number, type, (int)namelen,
273 name, (long long)vcn));
274 out:
275 free(alpool, M_TEMP);
276 return (error);
277 }
278
279 /*
280 * Read ntnode from disk, make ntvattr list.
281 *
282 * ntnode should be locked
283 */
284 int
285 ntfs_loadntnode(
286 struct ntfsmount * ntmp,
287 struct ntnode * ip)
288 {
289 struct filerec *mfrp;
290 daddr_t bn;
291 int error,off;
292 struct attr *ap;
293 struct ntvattr *nvap;
294
295 dprintf(("ntfs_loadntnode: loading ino: %llu\n",
296 (unsigned long long)ip->i_number));
297
298 mfrp = (struct filerec *) malloc(ntfs_bntob(ntmp->ntm_bpmftrec),
299 M_TEMP, M_WAITOK);
300
301 if (ip->i_number < NTFS_SYSNODESNUM) {
302 struct buf *bp;
303
304 dprintf(("ntfs_loadntnode: read system node\n"));
305
306 bn = ntfs_cntobn(ntmp->ntm_mftcn) +
307 ntmp->ntm_bpmftrec * ip->i_number;
308
309 error = bread(ntmp->ntm_devvp,
310 bn, ntfs_bntob(ntmp->ntm_bpmftrec),
311 NOCRED, &bp);
312 if (error) {
313 printf("ntfs_loadntnode: BREAD FAILED\n");
314 brelse(bp);
315 goto out;
316 }
317 memcpy(mfrp, bp->b_data, ntfs_bntob(ntmp->ntm_bpmftrec));
318 bqrelse(bp);
319 } else {
320 struct vnode *vp;
321
322 vp = ntmp->ntm_sysvn[NTFS_MFTINO];
323 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
324 ip->i_number * ntfs_bntob(ntmp->ntm_bpmftrec),
325 ntfs_bntob(ntmp->ntm_bpmftrec), mfrp, NULL);
326 if (error) {
327 printf("ntfs_loadntnode: ntfs_readattr failed\n");
328 goto out;
329 }
330 }
331
332 /* Check if magic and fixups are correct */
333 error = ntfs_procfixups(ntmp, NTFS_FILEMAGIC, (caddr_t)mfrp,
334 ntfs_bntob(ntmp->ntm_bpmftrec));
335 if (error) {
336 printf("ntfs_loadntnode: BAD MFT RECORD %d\n",
337 (u_int32_t) ip->i_number);
338 goto out;
339 }
340
341 dprintf(("ntfs_loadntnode: load attrs for ino: %llu\n",
342 (unsigned long long)ip->i_number));
343 off = mfrp->fr_attroff;
344 ap = (struct attr *) ((caddr_t)mfrp + off);
345
346 LIST_INIT(&ip->i_valist);
347
348 while (ap->a_hdr.a_type != -1) {
349 error = ntfs_attrtontvattr(ntmp, &nvap, ap);
350 if (error)
351 break;
352 nvap->va_ip = ip;
353
354 LIST_INSERT_HEAD(&ip->i_valist, nvap, va_list);
355
356 off += ap->a_hdr.reclen;
357 ap = (struct attr *) ((caddr_t)mfrp + off);
358 }
359 if (error) {
360 printf("ntfs_loadntnode: failed to load attr ino: %llu\n",
361 (unsigned long long)ip->i_number);
362 goto out;
363 }
364
365 ip->i_mainrec = mfrp->fr_mainrec;
366 ip->i_nlink = mfrp->fr_nlink;
367 ip->i_frflag = mfrp->fr_flags;
368
369 ip->i_flag |= IN_LOADED;
370
371 out:
372 free(mfrp, M_TEMP);
373 return (error);
374 }
375
376 /*
377 * Routine locks ntnode and increase usecount, just opposite of
378 * ntfs_ntput().
379 */
380 int
381 ntfs_ntget(ip)
382 struct ntnode *ip;
383 {
384 dprintf(("ntfs_ntget: get ntnode %llu: %p, usecount: %d\n",
385 (unsigned long long)ip->i_number, ip, ip->i_usecount));
386
387 simple_lock(&ip->i_interlock);
388 ip->i_usecount++;
389 lockmgr(&ip->i_lock, LK_EXCLUSIVE | LK_INTERLOCK, &ip->i_interlock);
390
391 return 0;
392 }
393
394 /*
395 * Routine search ntnode in hash, if found: lock, inc usecount and return.
396 * If not in hash allocate structure for ntnode, prefill it, lock,
397 * inc count and return.
398 *
399 * ntnode returned locked
400 */
401 int
402 ntfs_ntlookup(
403 struct ntfsmount * ntmp,
404 ino_t ino,
405 struct ntnode ** ipp)
406 {
407 struct ntnode *ip;
408
409 dprintf(("ntfs_ntlookup: looking for ntnode %llu\n",
410 (unsigned long long)ino));
411
412 if ((*ipp = ntfs_nthashlookup(ntmp->ntm_dev, ino)) != NULL) {
413 ntfs_ntget(*ipp);
414 dprintf(("ntfs_ntlookup: ntnode %llu: %p,"
415 " usecount: %d\n",
416 (unsigned long long)ino, *ipp, (*ipp)->i_usecount));
417 return (0);
418 }
419
420 MALLOC(ip, struct ntnode *, sizeof(struct ntnode),
421 M_NTFSNTNODE, M_WAITOK);
422 ddprintf(("ntfs_ntlookup: allocating ntnode: %llu: %p\n",
423 (unsigned long long)ino, ip));
424 bzero(ip, sizeof(struct ntnode));
425
426 mutex_enter(&ntfs_hashlock);
427 if ((*ipp = ntfs_nthashlookup(ntmp->ntm_dev, ino)) != NULL) {
428 mutex_exit(&ntfs_hashlock);
429 ntfs_ntget(*ipp);
430 FREE(ip, M_NTFSNTNODE);
431 dprintf(("ntfs_ntlookup: ntnode %llu: %p,"
432 " usecount: %d\n",
433 (unsigned long long)ino, *ipp, (*ipp)->i_usecount));
434 return (0);
435 }
436
437 /* Generic initialization */
438 ip->i_devvp = ntmp->ntm_devvp;
439 ip->i_dev = ntmp->ntm_dev;
440 ip->i_number = ino;
441 ip->i_mp = ntmp;
442
443 LIST_INIT(&ip->i_fnlist);
444
445 /* init lock and lock the newborn ntnode */
446 lockinit(&ip->i_lock, PINOD, "ntnode", 0, LK_EXCLUSIVE);
447 simple_lock_init(&ip->i_interlock);
448 ntfs_ntget(ip);
449
450 ntfs_nthashins(ip);
451
452 mutex_exit(&ntfs_hashlock);
453
454 *ipp = ip;
455
456 dprintf(("ntfs_ntlookup: ntnode %llu: %p, usecount: %d\n",
457 (unsigned long long)ino, ip, ip->i_usecount));
458
459 return (0);
460 }
461
462 /*
463 * Decrement usecount of ntnode and unlock it, if usecount reach zero,
464 * deallocate ntnode.
465 *
466 * ntnode should be locked on entry, and unlocked on return.
467 */
468 void
469 ntfs_ntput(ip)
470 struct ntnode *ip;
471 {
472 struct ntvattr *vap;
473
474 dprintf(("ntfs_ntput: rele ntnode %llu: %p, usecount: %d\n",
475 (unsigned long long)ip->i_number, ip, ip->i_usecount));
476
477 simple_lock(&ip->i_interlock);
478 ip->i_usecount--;
479
480 #ifdef DIAGNOSTIC
481 if (ip->i_usecount < 0) {
482 panic("ntfs_ntput: ino: %llu usecount: %d ",
483 (unsigned long long)ip->i_number, ip->i_usecount);
484 }
485 #endif
486
487 lockmgr(&ip->i_lock, LK_RELEASE|LK_INTERLOCK, &ip->i_interlock);
488
489 if (ip->i_usecount == 0) {
490 dprintf(("ntfs_ntput: deallocating ntnode: %llu\n",
491 (unsigned long long)ip->i_number));
492
493 if (ip->i_fnlist.lh_first)
494 panic("ntfs_ntput: ntnode has fnodes");
495
496 ntfs_nthashrem(ip);
497
498 while (ip->i_valist.lh_first != NULL) {
499 vap = ip->i_valist.lh_first;
500 LIST_REMOVE(vap,va_list);
501 ntfs_freentvattr(vap);
502 }
503 FREE(ip, M_NTFSNTNODE);
504 }
505 }
506
507 /*
508 * increment usecount of ntnode
509 */
510 void
511 ntfs_ntref(ip)
512 struct ntnode *ip;
513 {
514 simple_lock(&ip->i_interlock);
515 ip->i_usecount++;
516 simple_unlock(&ip->i_interlock);
517
518 dprintf(("ntfs_ntref: ino %llu, usecount: %d\n",
519 (unsigned long long)ip->i_number, ip->i_usecount));
520
521 }
522
523 /*
524 * Decrement usecount of ntnode.
525 */
526 void
527 ntfs_ntrele(ip)
528 struct ntnode *ip;
529 {
530 dprintf(("ntfs_ntrele: rele ntnode %llu: %p, usecount: %d\n",
531 (unsigned long long)ip->i_number, ip, ip->i_usecount));
532
533 simple_lock(&ip->i_interlock);
534 ip->i_usecount--;
535
536 if (ip->i_usecount < 0)
537 panic("ntfs_ntrele: ino: %llu usecount: %d ",
538 (unsigned long long)ip->i_number, ip->i_usecount);
539 simple_unlock(&ip->i_interlock);
540 }
541
542 /*
543 * Deallocate all memory allocated for ntvattr
544 */
545 void
546 ntfs_freentvattr(vap)
547 struct ntvattr * vap;
548 {
549 if (vap->va_flag & NTFS_AF_INRUN) {
550 if (vap->va_vruncn)
551 free(vap->va_vruncn, M_NTFSRUN);
552 if (vap->va_vruncl)
553 free(vap->va_vruncl, M_NTFSRUN);
554 } else {
555 if (vap->va_datap)
556 free(vap->va_datap, M_NTFSRDATA);
557 }
558 FREE(vap, M_NTFSNTVATTR);
559 }
560
561 /*
562 * Convert disk image of attribute into ntvattr structure,
563 * runs are expanded also.
564 */
565 int
566 ntfs_attrtontvattr(
567 struct ntfsmount * ntmp,
568 struct ntvattr ** rvapp,
569 struct attr * rap)
570 {
571 int error, i;
572 struct ntvattr *vap;
573
574 error = 0;
575 *rvapp = NULL;
576
577 MALLOC(vap, struct ntvattr *, sizeof(struct ntvattr),
578 M_NTFSNTVATTR, M_WAITOK);
579 bzero(vap, sizeof(struct ntvattr));
580 vap->va_ip = NULL;
581 vap->va_flag = rap->a_hdr.a_flag;
582 vap->va_type = rap->a_hdr.a_type;
583 vap->va_compression = rap->a_hdr.a_compression;
584 vap->va_index = rap->a_hdr.a_index;
585
586 ddprintf(("type: 0x%x, index: %d", vap->va_type, vap->va_index));
587
588 vap->va_namelen = rap->a_hdr.a_namelen;
589 if (rap->a_hdr.a_namelen) {
590 wchar *unp = (wchar *) ((caddr_t) rap + rap->a_hdr.a_nameoff);
591 ddprintf((", name:["));
592 for (i = 0; i < vap->va_namelen; i++) {
593 vap->va_name[i] = unp[i];
594 ddprintf(("%c", vap->va_name[i]));
595 }
596 ddprintf(("]"));
597 }
598 if (vap->va_flag & NTFS_AF_INRUN) {
599 ddprintf((", nonres."));
600 vap->va_datalen = rap->a_nr.a_datalen;
601 vap->va_allocated = rap->a_nr.a_allocated;
602 vap->va_vcnstart = rap->a_nr.a_vcnstart;
603 vap->va_vcnend = rap->a_nr.a_vcnend;
604 vap->va_compressalg = rap->a_nr.a_compressalg;
605 error = ntfs_runtovrun(&(vap->va_vruncn), &(vap->va_vruncl),
606 &(vap->va_vruncnt),
607 (u_int8_t *) rap + rap->a_nr.a_dataoff);
608 } else {
609 vap->va_compressalg = 0;
610 ddprintf((", res."));
611 vap->va_datalen = rap->a_r.a_datalen;
612 vap->va_allocated = rap->a_r.a_datalen;
613 vap->va_vcnstart = 0;
614 vap->va_vcnend = ntfs_btocn(vap->va_allocated);
615 vap->va_datap = (caddr_t) malloc(vap->va_datalen,
616 M_NTFSRDATA, M_WAITOK);
617 memcpy(vap->va_datap, (caddr_t) rap + rap->a_r.a_dataoff,
618 rap->a_r.a_datalen);
619 }
620 ddprintf((", len: %qu", (long long)vap->va_datalen));
621
622 if (error)
623 FREE(vap, M_NTFSNTVATTR);
624 else
625 *rvapp = vap;
626
627 ddprintf(("\n"));
628
629 return (error);
630 }
631
632 /*
633 * Expand run into more utilizable and more memory eating format.
634 */
635 int
636 ntfs_runtovrun(
637 cn_t ** rcnp,
638 cn_t ** rclp,
639 u_long * rcntp,
640 u_int8_t * run)
641 {
642 u_int32_t off;
643 u_int32_t sz, i;
644 cn_t *cn;
645 cn_t *cl;
646 u_long cnt;
647 cn_t prev;
648 cn_t tmp;
649
650 off = 0;
651 cnt = 0;
652 i = 0;
653 while (run[off]) {
654 off += (run[off] & 0xF) + ((run[off] >> 4) & 0xF) + 1;
655 cnt++;
656 }
657 cn = (cn_t *) malloc(cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
658 cl = (cn_t *) malloc(cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
659
660 off = 0;
661 cnt = 0;
662 prev = 0;
663 while (run[off]) {
664
665 sz = run[off++];
666 cl[cnt] = 0;
667
668 for (i = 0; i < (sz & 0xF); i++)
669 cl[cnt] += (u_int32_t) run[off++] << (i << 3);
670
671 sz >>= 4;
672 if (run[off + sz - 1] & 0x80) {
673 tmp = ((u_int64_t) - 1) << (sz << 3);
674 for (i = 0; i < sz; i++)
675 tmp |= (u_int64_t) run[off++] << (i << 3);
676 } else {
677 tmp = 0;
678 for (i = 0; i < sz; i++)
679 tmp |= (u_int64_t) run[off++] << (i << 3);
680 }
681 if (tmp)
682 prev = cn[cnt] = prev + tmp;
683 else
684 cn[cnt] = tmp;
685
686 cnt++;
687 }
688 *rcnp = cn;
689 *rclp = cl;
690 *rcntp = cnt;
691 return (0);
692 }
693
694 /*
695 * Compare unicode and ascii string case insens.
696 */
697 static int
698 ntfs_uastricmp(ntmp, ustr, ustrlen, astr, astrlen)
699 struct ntfsmount *ntmp;
700 const wchar *ustr;
701 size_t ustrlen;
702 const char *astr;
703 size_t astrlen;
704 {
705 size_t i;
706 int res;
707
708 for (i = 0; i < ustrlen && astrlen > 0; i++) {
709 res = (*ntmp->ntm_wcmp)(NTFS_TOUPPER(ustr[i]),
710 NTFS_TOUPPER((*ntmp->ntm_wget)(&astr, &astrlen)) );
711 if (res)
712 return res;
713 }
714
715 if (i == ustrlen && astrlen == 0)
716 return 0;
717 else if (i == ustrlen)
718 return -1;
719 else
720 return 1;
721 }
722
723 /*
724 * Compare unicode and ascii string case sens.
725 */
726 static int
727 ntfs_uastrcmp(ntmp, ustr, ustrlen, astr, astrlen)
728 struct ntfsmount *ntmp;
729 const wchar *ustr;
730 size_t ustrlen;
731 const char *astr;
732 size_t astrlen;
733 {
734 size_t i;
735 int res;
736
737 for (i = 0; (i < ustrlen) && astrlen > 0; i++) {
738 res = (*ntmp->ntm_wcmp)(ustr[i],
739 (*ntmp->ntm_wget)(&astr, &astrlen));
740 if (res)
741 return res;
742 }
743
744 if (i == ustrlen && astrlen == 0)
745 return 0;
746 else if (i == ustrlen)
747 return -1;
748 else
749 return 1;
750 }
751
752 /*
753 * Search fnode in ntnode, if not found allocate and preinitialize.
754 *
755 * ntnode should be locked on entry.
756 */
757 int
758 ntfs_fget(
759 struct ntfsmount *ntmp,
760 struct ntnode *ip,
761 int attrtype,
762 char *attrname,
763 struct fnode **fpp
764 )
765 {
766 struct fnode *fp;
767
768 dprintf(("ntfs_fget: ino: %llu, attrtype: 0x%x, attrname: %s\n",
769 (unsigned long long)ip->i_number, attrtype, attrname?attrname:""));
770 *fpp = NULL;
771 for (fp = ip->i_fnlist.lh_first; fp != NULL; fp = fp->f_fnlist.le_next){
772 dprintf(("ntfs_fget: fnode: attrtype: %d, attrname: %s\n",
773 fp->f_attrtype, fp->f_attrname?fp->f_attrname:""));
774
775 if ((attrtype == fp->f_attrtype) &&
776 ((!attrname && !fp->f_attrname) ||
777 (attrname && fp->f_attrname &&
778 !strcmp(attrname,fp->f_attrname)))){
779 dprintf(("ntfs_fget: found existed: %p\n",fp));
780 *fpp = fp;
781 }
782 }
783
784 if (*fpp)
785 return (0);
786
787 MALLOC(fp, struct fnode *, sizeof(struct fnode), M_NTFSFNODE, M_WAITOK);
788 bzero(fp, sizeof(struct fnode));
789 dprintf(("ntfs_fget: allocating fnode: %p\n",fp));
790
791 fp->f_ip = ip;
792 fp->f_attrname = attrname;
793 if (fp->f_attrname) fp->f_flag |= FN_AATTRNAME;
794 fp->f_attrtype = attrtype;
795
796 ntfs_ntref(ip);
797
798 LIST_INSERT_HEAD(&ip->i_fnlist, fp, f_fnlist);
799
800 *fpp = fp;
801
802 return (0);
803 }
804
805 /*
806 * Deallocate fnode, remove it from ntnode's fnode list.
807 *
808 * ntnode should be locked.
809 */
810 void
811 ntfs_frele(
812 struct fnode *fp)
813 {
814 struct ntnode *ip = FTONT(fp);
815
816 dprintf(("ntfs_frele: fnode: %p for %llu: %p\n", fp,
817 (unsigned long long)ip->i_number, ip));
818
819 dprintf(("ntfs_frele: deallocating fnode\n"));
820 LIST_REMOVE(fp,f_fnlist);
821 if (fp->f_flag & FN_AATTRNAME)
822 FREE(fp->f_attrname, M_TEMP);
823 if (fp->f_dirblbuf)
824 FREE(fp->f_dirblbuf, M_NTFSDIR);
825 FREE(fp, M_NTFSFNODE);
826 ntfs_ntrele(ip);
827 }
828
829 /*
830 * Lookup attribute name in format: [[:$ATTR_TYPE]:$ATTR_NAME],
831 * $ATTR_TYPE is searched in attrdefs read from $AttrDefs.
832 * If $ATTR_TYPE not specified, ATTR_A_DATA assumed.
833 */
834 static int
835 ntfs_ntlookupattr(
836 struct ntfsmount * ntmp,
837 const char * name,
838 int namelen,
839 int *attrtype,
840 char **attrname)
841 {
842 const char *sys;
843 size_t syslen, i;
844 struct ntvattrdef *adp;
845
846 if (namelen == 0)
847 return (0);
848
849 if (name[0] == '$') {
850 sys = name;
851 for (syslen = 0; syslen < namelen; syslen++) {
852 if(sys[syslen] == ':') {
853 name++;
854 namelen--;
855 break;
856 }
857 }
858 name += syslen;
859 namelen -= syslen;
860
861 adp = ntmp->ntm_ad;
862 for (i = 0; i < ntmp->ntm_adnum; i++, adp++){
863 if (syslen != adp->ad_namelen ||
864 strncmp(sys, adp->ad_name, syslen) != 0)
865 continue;
866
867 *attrtype = adp->ad_type;
868 goto out;
869 }
870 return (ENOENT);
871 }
872
873 out:
874 if (namelen) {
875 *attrname = (char *) malloc(namelen, M_TEMP, M_WAITOK);
876 memcpy((*attrname), name, namelen);
877 (*attrname)[namelen] = '\0';
878 *attrtype = NTFS_A_DATA;
879 }
880
881 return (0);
882 }
883
884 /*
885 * Lookup specified node for filename, matching cnp,
886 * return fnode filled.
887 */
888 int
889 ntfs_ntlookupfile(
890 struct ntfsmount * ntmp,
891 struct vnode * vp,
892 struct componentname * cnp,
893 struct vnode ** vpp)
894 {
895 struct fnode *fp = VTOF(vp);
896 struct ntnode *ip = FTONT(fp);
897 struct ntvattr *vap; /* Root attribute */
898 cn_t cn = 0; /* VCN in current attribute */
899 caddr_t rdbuf; /* Buffer to read directory's blocks */
900 u_int32_t blsize;
901 u_int32_t rdsize; /* Length of data to read from current block */
902 struct attr_indexentry *iep;
903 int error, res, anamelen, fnamelen;
904 const char *fname,*aname;
905 u_int32_t aoff;
906 int attrtype = NTFS_A_DATA;
907 char *attrname = NULL;
908 struct fnode *nfp;
909 struct vnode *nvp;
910 enum vtype f_type;
911 int fullscan = 0;
912 struct ntfs_lookup_ctx *lookup_ctx = NULL, *tctx;
913
914 error = ntfs_ntget(ip);
915 if (error)
916 return (error);
917
918 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
919 if (error || (vap->va_flag & NTFS_AF_INRUN))
920 return (ENOTDIR);
921
922 /*
923 * Divide file name into: foofilefoofilefoofile[:attrspec]
924 * Store like this: fname:fnamelen [aname:anamelen]
925 */
926 fname = cnp->cn_nameptr;
927 aname = NULL;
928 anamelen = 0;
929 for (fnamelen = 0; fnamelen < cnp->cn_namelen; fnamelen++)
930 if(fname[fnamelen] == ':') {
931 aname = fname + fnamelen + 1;
932 anamelen = cnp->cn_namelen - fnamelen - 1;
933 dprintf(("ntfs_ntlookupfile: %s (%d), attr: %s (%d)\n",
934 fname, fnamelen, aname, anamelen));
935 break;
936 }
937
938 blsize = vap->va_a_iroot->ir_size;
939 dprintf(("ntfs_ntlookupfile: blksz: %d\n", blsize));
940
941 rdbuf = (caddr_t) malloc(blsize, M_TEMP, M_WAITOK);
942
943 loop:
944 rdsize = vap->va_datalen;
945 dprintf(("ntfs_ntlookupfile: rdsz: %d\n", rdsize));
946
947 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXROOT, "$I30",
948 0, rdsize, rdbuf, NULL);
949 if (error)
950 goto fail;
951
952 aoff = sizeof(struct attr_indexroot);
953
954 do {
955 iep = (struct attr_indexentry *) (rdbuf + aoff);
956
957 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
958 aoff += iep->reclen,
959 iep = (struct attr_indexentry *) (rdbuf + aoff))
960 {
961 ddprintf(("scan: %d, %d\n",
962 (u_int32_t) iep->ie_number,
963 (u_int32_t) iep->ie_fnametype));
964
965 /* check the name - the case-insensitive check
966 * has to come first, to break from this for loop
967 * if needed, so we can dive correctly */
968 res = ntfs_uastricmp(ntmp, iep->ie_fname,
969 iep->ie_fnamelen, fname, fnamelen);
970 if (!fullscan) {
971 if (res > 0) break;
972 if (res < 0) continue;
973 }
974
975 if (iep->ie_fnametype == 0 ||
976 !(ntmp->ntm_flag & NTFS_MFLAG_CASEINS))
977 {
978 res = ntfs_uastrcmp(ntmp, iep->ie_fname,
979 iep->ie_fnamelen, fname, fnamelen);
980 if (res != 0 && !fullscan) continue;
981 }
982
983 /* if we perform full scan, the file does not match
984 * and this is subnode, dive */
985 if (fullscan && res != 0) {
986 if (iep->ie_flag & NTFS_IEFLAG_SUBNODE) {
987 MALLOC(tctx, struct ntfs_lookup_ctx *,
988 sizeof(struct ntfs_lookup_ctx),
989 M_TEMP, M_WAITOK);
990 tctx->aoff = aoff + iep->reclen;
991 tctx->rdsize = rdsize;
992 tctx->cn = cn;
993 tctx->prev = lookup_ctx;
994 lookup_ctx = tctx;
995 break;
996 } else
997 continue;
998 }
999
1000 if (aname) {
1001 error = ntfs_ntlookupattr(ntmp,
1002 aname, anamelen,
1003 &attrtype, &attrname);
1004 if (error)
1005 goto fail;
1006 }
1007
1008 /* Check if we've found ourselves */
1009 if ((iep->ie_number == ip->i_number) &&
1010 (attrtype == fp->f_attrtype) &&
1011 ((!attrname && !fp->f_attrname) ||
1012 (attrname && fp->f_attrname &&
1013 !strcmp(attrname, fp->f_attrname))))
1014 {
1015 VREF(vp);
1016 *vpp = vp;
1017 error = 0;
1018 goto fail;
1019 }
1020
1021 /* free the buffer returned by ntfs_ntlookupattr() */
1022 if (attrname) {
1023 FREE(attrname, M_TEMP);
1024 attrname = NULL;
1025 }
1026
1027 /* vget node, but don't load it */
1028 error = ntfs_vgetex(ntmp->ntm_mountp,
1029 iep->ie_number, attrtype, attrname,
1030 LK_EXCLUSIVE, VG_DONTLOADIN | VG_DONTVALIDFN,
1031 &nvp);
1032 if (error)
1033 goto fail;
1034
1035 nfp = VTOF(nvp);
1036
1037 if (nfp->f_flag & FN_VALID) {
1038 *vpp = nvp;
1039 goto fail;
1040 }
1041
1042 nfp->f_fflag = iep->ie_fflag;
1043 nfp->f_pnumber = iep->ie_fpnumber;
1044 nfp->f_times = iep->ie_ftimes;
1045
1046 if((nfp->f_fflag & NTFS_FFLAG_DIR) &&
1047 (nfp->f_attrtype == NTFS_A_DATA) &&
1048 (nfp->f_attrname == NULL))
1049 f_type = VDIR;
1050 else
1051 f_type = VREG;
1052
1053 nvp->v_type = f_type;
1054
1055 if ((nfp->f_attrtype == NTFS_A_DATA) &&
1056 (nfp->f_attrname == NULL))
1057 {
1058 /* Opening default attribute */
1059 nfp->f_size = iep->ie_fsize;
1060 nfp->f_allocated = iep->ie_fallocated;
1061 nfp->f_flag |= FN_PRELOADED;
1062 } else {
1063 error = ntfs_filesize(ntmp, nfp,
1064 &nfp->f_size, &nfp->f_allocated);
1065 if (error) {
1066 vput(nvp);
1067 goto fail;
1068 }
1069 }
1070
1071 nfp->f_flag &= ~FN_VALID;
1072 *vpp = nvp;
1073 goto fail;
1074 }
1075
1076 /* Dive if possible */
1077 if (iep->ie_flag & NTFS_IEFLAG_SUBNODE) {
1078 dprintf(("ntfs_ntlookupfile: diving\n"));
1079
1080 cn = *(cn_t *) (rdbuf + aoff +
1081 iep->reclen - sizeof(cn_t));
1082 rdsize = blsize;
1083
1084 error = ntfs_readattr(ntmp, ip, NTFS_A_INDX, "$I30",
1085 ntfs_cntob(cn), rdsize, rdbuf, NULL);
1086 if (error)
1087 goto fail;
1088
1089 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1090 rdbuf, rdsize);
1091 if (error)
1092 goto fail;
1093
1094 aoff = (((struct attr_indexalloc *) rdbuf)->ia_hdrsize +
1095 0x18);
1096 } else if (fullscan && lookup_ctx) {
1097 cn = lookup_ctx->cn;
1098 aoff = lookup_ctx->aoff;
1099 rdsize = lookup_ctx->rdsize;
1100
1101 error = ntfs_readattr(ntmp, ip,
1102 (cn == 0) ? NTFS_A_INDXROOT : NTFS_A_INDX,
1103 "$I30", ntfs_cntob(cn), rdsize, rdbuf, NULL);
1104 if (error)
1105 goto fail;
1106
1107 if (cn != 0) {
1108 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1109 rdbuf, rdsize);
1110 if (error)
1111 goto fail;
1112 }
1113
1114 tctx = lookup_ctx;
1115 lookup_ctx = lookup_ctx->prev;
1116 FREE(tctx, M_TEMP);
1117 } else {
1118 dprintf(("ntfs_ntlookupfile: nowhere to dive :-(\n"));
1119 error = ENOENT;
1120 break;
1121 }
1122 } while (1);
1123
1124 /* perform full scan if no entry was found */
1125 if (!fullscan && error == ENOENT) {
1126 fullscan = 1;
1127 cn = 0; /* need zero, used by lookup_ctx */
1128
1129 ddprintf(("ntfs_ntlookupfile: fullscan performed for: %.*s\n",
1130 (int) fnamelen, fname));
1131 goto loop;
1132 }
1133
1134 dprintf(("finish\n"));
1135
1136 fail:
1137 if (attrname)
1138 FREE(attrname, M_TEMP);
1139 if (lookup_ctx) {
1140 while(lookup_ctx) {
1141 tctx = lookup_ctx;
1142 lookup_ctx = lookup_ctx->prev;
1143 FREE(tctx, M_TEMP);
1144 }
1145 }
1146 ntfs_ntvattrrele(vap);
1147 ntfs_ntput(ip);
1148 free(rdbuf, M_TEMP);
1149 return (error);
1150 }
1151
1152 /*
1153 * Check if name type is permitted to show.
1154 */
1155 int
1156 ntfs_isnamepermitted(
1157 struct ntfsmount * ntmp,
1158 struct attr_indexentry * iep)
1159 {
1160 if (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)
1161 return 1;
1162
1163 switch (iep->ie_fnametype) {
1164 case 2:
1165 ddprintf(("ntfs_isnamepermitted: skipped DOS name\n"));
1166 return 0;
1167 case 0: case 1: case 3:
1168 return 1;
1169 default:
1170 printf("ntfs_isnamepermitted: "
1171 "WARNING! Unknown file name type: %d\n",
1172 iep->ie_fnametype);
1173 break;
1174 }
1175 return 0;
1176 }
1177
1178 /*
1179 * Read ntfs dir like stream of attr_indexentry, not like btree of them.
1180 * This is done by scanning $BITMAP:$I30 for busy clusters and reading them.
1181 * Of course $INDEX_ROOT:$I30 is read before. Last read values are stored in
1182 * fnode, so we can skip toward record number num almost immediately.
1183 * Anyway this is rather slow routine. The problem is that we don't know
1184 * how many records are there in $INDEX_ALLOCATION:$I30 block.
1185 */
1186 int
1187 ntfs_ntreaddir(
1188 struct ntfsmount * ntmp,
1189 struct fnode * fp,
1190 u_int32_t num,
1191 struct attr_indexentry ** riepp)
1192 {
1193 struct ntnode *ip = FTONT(fp);
1194 struct ntvattr *vap = NULL; /* IndexRoot attribute */
1195 struct ntvattr *bmvap = NULL; /* BitMap attribute */
1196 struct ntvattr *iavap = NULL; /* IndexAllocation attribute */
1197 caddr_t rdbuf; /* Buffer to read directory's blocks */
1198 u_char *bmp = NULL; /* Bitmap */
1199 u_int32_t blsize; /* Index allocation size (2048) */
1200 u_int32_t rdsize; /* Length of data to read */
1201 u_int32_t attrnum; /* Current attribute type */
1202 u_int32_t cpbl = 1; /* Clusters per directory block */
1203 u_int32_t blnum;
1204 struct attr_indexentry *iep;
1205 int error = ENOENT;
1206 u_int32_t aoff, cnum;
1207
1208 dprintf(("ntfs_ntreaddir: read ino: %llu, num: %d\n",
1209 (unsigned long long)ip->i_number, num));
1210 error = ntfs_ntget(ip);
1211 if (error)
1212 return (error);
1213
1214 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
1215 if (error)
1216 return (ENOTDIR);
1217
1218 if (fp->f_dirblbuf == NULL) {
1219 fp->f_dirblsz = vap->va_a_iroot->ir_size;
1220 fp->f_dirblbuf = (caddr_t) malloc(
1221 MAX(vap->va_datalen,fp->f_dirblsz), M_NTFSDIR, M_WAITOK);
1222 }
1223
1224 blsize = fp->f_dirblsz;
1225 rdbuf = fp->f_dirblbuf;
1226
1227 dprintf(("ntfs_ntreaddir: rdbuf: %p, blsize: %d\n", rdbuf, blsize));
1228
1229 if (vap->va_a_iroot->ir_flag & NTFS_IRFLAG_INDXALLOC) {
1230 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXBITMAP, "$I30",
1231 0, &bmvap);
1232 if (error) {
1233 error = ENOTDIR;
1234 goto fail;
1235 }
1236 bmp = (u_char *) malloc(bmvap->va_datalen, M_TEMP, M_WAITOK);
1237 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXBITMAP, "$I30", 0,
1238 bmvap->va_datalen, bmp, NULL);
1239 if (error)
1240 goto fail;
1241
1242 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDX, "$I30",
1243 0, &iavap);
1244 if (error) {
1245 error = ENOTDIR;
1246 goto fail;
1247 }
1248 cpbl = ntfs_btocn(blsize + ntfs_cntob(1) - 1);
1249 dprintf(("ntfs_ntreaddir: indexalloc: %qu, cpbl: %d\n",
1250 (long long)iavap->va_datalen, cpbl));
1251 } else {
1252 dprintf(("ntfs_ntreadidir: w/o BitMap and IndexAllocation\n"));
1253 iavap = bmvap = NULL;
1254 bmp = NULL;
1255 }
1256
1257 /* Try use previous values */
1258 if ((fp->f_lastdnum < num) && (fp->f_lastdnum != 0)) {
1259 attrnum = fp->f_lastdattr;
1260 aoff = fp->f_lastdoff;
1261 blnum = fp->f_lastdblnum;
1262 cnum = fp->f_lastdnum;
1263 } else {
1264 attrnum = NTFS_A_INDXROOT;
1265 aoff = sizeof(struct attr_indexroot);
1266 blnum = 0;
1267 cnum = 0;
1268 }
1269
1270 do {
1271 dprintf(("ntfs_ntreaddir: scan: 0x%x, %d, %d, %d, %d\n",
1272 attrnum, (u_int32_t) blnum, cnum, num, aoff));
1273 rdsize = (attrnum == NTFS_A_INDXROOT) ? vap->va_datalen : blsize;
1274 error = ntfs_readattr(ntmp, ip, attrnum, "$I30",
1275 ntfs_cntob(blnum * cpbl), rdsize, rdbuf, NULL);
1276 if (error)
1277 goto fail;
1278
1279 if (attrnum == NTFS_A_INDX) {
1280 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1281 rdbuf, rdsize);
1282 if (error)
1283 goto fail;
1284 }
1285 if (aoff == 0)
1286 aoff = (attrnum == NTFS_A_INDX) ?
1287 (0x18 + ((struct attr_indexalloc *) rdbuf)->ia_hdrsize) :
1288 sizeof(struct attr_indexroot);
1289
1290 iep = (struct attr_indexentry *) (rdbuf + aoff);
1291 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
1292 aoff += iep->reclen,
1293 iep = (struct attr_indexentry *) (rdbuf + aoff))
1294 {
1295 if (!ntfs_isnamepermitted(ntmp, iep)) continue;
1296
1297 if (cnum >= num) {
1298 fp->f_lastdnum = cnum;
1299 fp->f_lastdoff = aoff;
1300 fp->f_lastdblnum = blnum;
1301 fp->f_lastdattr = attrnum;
1302
1303 *riepp = iep;
1304
1305 error = 0;
1306 goto fail;
1307 }
1308 cnum++;
1309 }
1310
1311 if (iavap) {
1312 if (attrnum == NTFS_A_INDXROOT)
1313 blnum = 0;
1314 else
1315 blnum++;
1316
1317 while (ntfs_cntob(blnum * cpbl) < iavap->va_datalen) {
1318 if (bmp[blnum >> 3] & (1 << (blnum & 3)))
1319 break;
1320 blnum++;
1321 }
1322
1323 attrnum = NTFS_A_INDX;
1324 aoff = 0;
1325 if (ntfs_cntob(blnum * cpbl) >= iavap->va_datalen)
1326 break;
1327 dprintf(("ntfs_ntreaddir: blnum: %d\n", (u_int32_t) blnum));
1328 }
1329 } while (iavap);
1330
1331 *riepp = NULL;
1332 fp->f_lastdnum = 0;
1333
1334 fail:
1335 if (vap)
1336 ntfs_ntvattrrele(vap);
1337 if (bmvap)
1338 ntfs_ntvattrrele(bmvap);
1339 if (iavap)
1340 ntfs_ntvattrrele(iavap);
1341 if (bmp)
1342 FREE(bmp, M_TEMP);
1343 ntfs_ntput(ip);
1344 return (error);
1345 }
1346
1347 /*
1348 * Convert NTFS times that are in 100 ns units and begins from
1349 * 1601 Jan 1 into unix times.
1350 */
1351 struct timespec
1352 ntfs_nttimetounix(
1353 u_int64_t nt)
1354 {
1355 struct timespec t;
1356
1357 /* WindowNT times are in 100 ns and from 1601 Jan 1 */
1358 t.tv_nsec = (nt % (1000 * 1000 * 10)) * 100;
1359 t.tv_sec = nt / (1000 * 1000 * 10) -
1360 369LL * 365LL * 24LL * 60LL * 60LL -
1361 89LL * 1LL * 24LL * 60LL * 60LL;
1362 return (t);
1363 }
1364
1365 /*
1366 * Get file times from NTFS_A_NAME attribute.
1367 */
1368 int
1369 ntfs_times(
1370 struct ntfsmount * ntmp,
1371 struct ntnode * ip,
1372 ntfs_times_t * tm)
1373 {
1374 struct ntvattr *vap;
1375 int error;
1376
1377 dprintf(("ntfs_times: ino: %llu...\n",
1378 (unsigned long long)ip->i_number));
1379
1380 error = ntfs_ntget(ip);
1381 if (error)
1382 return (error);
1383
1384 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_NAME, NULL, 0, &vap);
1385 if (error) {
1386 ntfs_ntput(ip);
1387 return (error);
1388 }
1389 *tm = vap->va_a_name->n_times;
1390 ntfs_ntvattrrele(vap);
1391 ntfs_ntput(ip);
1392
1393 return (0);
1394 }
1395
1396 /*
1397 * Get file sizes from corresponding attribute.
1398 *
1399 * ntnode under fnode should be locked.
1400 */
1401 int
1402 ntfs_filesize(
1403 struct ntfsmount * ntmp,
1404 struct fnode * fp,
1405 u_int64_t * size,
1406 u_int64_t * bytes)
1407 {
1408 struct ntvattr *vap;
1409 struct ntnode *ip = FTONT(fp);
1410 u_int64_t sz, bn;
1411 int error;
1412
1413 dprintf(("ntfs_filesize: ino: %llu\n",
1414 (unsigned long long)ip->i_number));
1415
1416 error = ntfs_ntvattrget(ntmp, ip,
1417 fp->f_attrtype, fp->f_attrname, 0, &vap);
1418 if (error)
1419 return (error);
1420
1421 bn = vap->va_allocated;
1422 sz = vap->va_datalen;
1423
1424 dprintf(("ntfs_filesize: %d bytes (%d bytes allocated)\n",
1425 (u_int32_t) sz, (u_int32_t) bn));
1426
1427 if (size)
1428 *size = sz;
1429 if (bytes)
1430 *bytes = bn;
1431
1432 ntfs_ntvattrrele(vap);
1433
1434 return (0);
1435 }
1436
1437 /*
1438 * This is one of write routine.
1439 */
1440 int
1441 ntfs_writeattr_plain(
1442 struct ntfsmount * ntmp,
1443 struct ntnode * ip,
1444 u_int32_t attrnum,
1445 char *attrname,
1446 off_t roff,
1447 size_t rsize,
1448 void *rdata,
1449 size_t * initp,
1450 struct uio *uio)
1451 {
1452 size_t init;
1453 int error = 0;
1454 off_t off = roff, left = rsize, towrite;
1455 caddr_t data = rdata;
1456 struct ntvattr *vap;
1457 *initp = 0;
1458
1459 while (left) {
1460 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1461 ntfs_btocn(off), &vap);
1462 if (error)
1463 return (error);
1464 towrite = MIN(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1465 ddprintf(("ntfs_writeattr_plain: o: %qd, s: %qd (%qu - %qu)\n",
1466 (long long) off, (long long) towrite,
1467 (long long) vap->va_vcnstart,
1468 (long long) vap->va_vcnend));
1469 error = ntfs_writentvattr_plain(ntmp, ip, vap,
1470 off - ntfs_cntob(vap->va_vcnstart),
1471 towrite, data, &init, uio);
1472 if (error) {
1473 dprintf(("ntfs_writeattr_plain: "
1474 "ntfs_writentvattr_plain failed: o: %qd, s: %qd\n",
1475 (long long) off, (long long) towrite));
1476 dprintf(("ntfs_writeattr_plain: attrib: %qu - %qu\n",
1477 (long long) vap->va_vcnstart,
1478 (long long) vap->va_vcnend));
1479 ntfs_ntvattrrele(vap);
1480 break;
1481 }
1482 ntfs_ntvattrrele(vap);
1483 left -= towrite;
1484 off += towrite;
1485 data = data + towrite;
1486 *initp += init;
1487 }
1488
1489 return (error);
1490 }
1491
1492 /*
1493 * This is one of write routine.
1494 *
1495 * ntnode should be locked.
1496 */
1497 int
1498 ntfs_writentvattr_plain(
1499 struct ntfsmount * ntmp,
1500 struct ntnode * ip,
1501 struct ntvattr * vap,
1502 off_t roff,
1503 size_t rsize,
1504 void *rdata,
1505 size_t * initp,
1506 struct uio *uio)
1507 {
1508 int error = 0;
1509 off_t off;
1510 int cnt;
1511 cn_t ccn, ccl, cn, left, cl;
1512 caddr_t data = rdata;
1513 struct buf *bp;
1514 size_t tocopy;
1515
1516 *initp = 0;
1517
1518 if ((vap->va_flag & NTFS_AF_INRUN) == 0) {
1519 dprintf(("ntfs_writevattr_plain: CAN'T WRITE RES. ATTRIBUTE\n"));
1520 return ENOTTY;
1521 }
1522
1523 ddprintf(("ntfs_writentvattr_plain: data in run: %lu chains\n",
1524 vap->va_vruncnt));
1525
1526 off = roff;
1527 left = rsize;
1528 ccl = 0;
1529 ccn = 0;
1530 cnt = 0;
1531 for (; left && (cnt < vap->va_vruncnt); cnt++) {
1532 ccn = vap->va_vruncn[cnt];
1533 ccl = vap->va_vruncl[cnt];
1534
1535 ddprintf(("ntfs_writentvattr_plain: "
1536 "left %qu, cn: 0x%qx, cl: %qu, off: %qd\n",
1537 (long long) left, (long long) ccn,
1538 (long long) ccl, (long long) off));
1539
1540 if (ntfs_cntob(ccl) < off) {
1541 off -= ntfs_cntob(ccl);
1542 cnt++;
1543 continue;
1544 }
1545 if (!ccn && ip->i_number != NTFS_BOOTINO)
1546 continue; /* XXX */
1547
1548 ccl -= ntfs_btocn(off);
1549 cn = ccn + ntfs_btocn(off);
1550 off = ntfs_btocnoff(off);
1551
1552 while (left && ccl) {
1553 /*
1554 * Always read and write single clusters at a time -
1555 * we need to avoid requesting differently-sized
1556 * blocks at the same disk offsets to avoid
1557 * confusing the buffer cache.
1558 */
1559 tocopy = MIN(left, ntfs_cntob(1) - off);
1560 cl = ntfs_btocl(tocopy + off);
1561 KASSERT(cl == 1 && tocopy <= ntfs_cntob(1));
1562 ddprintf(("ntfs_writentvattr_plain: write: "
1563 "cn: 0x%qx cl: %qu, off: %qd len: %qu, left: %qu\n",
1564 (long long) cn, (long long) cl,
1565 (long long) off, (long long) tocopy,
1566 (long long) left));
1567 if ((off == 0) && (tocopy == ntfs_cntob(cl)))
1568 {
1569 bp = getblk(ntmp->ntm_devvp, ntfs_cntobn(cn),
1570 ntfs_cntob(cl), 0, 0);
1571 clrbuf(bp);
1572 } else {
1573 error = bread(ntmp->ntm_devvp, ntfs_cntobn(cn),
1574 ntfs_cntob(cl), NOCRED, &bp);
1575 if (error) {
1576 brelse(bp);
1577 return (error);
1578 }
1579 }
1580 if (uio)
1581 uiomove(bp->b_data + off, tocopy, uio);
1582 else
1583 memcpy(bp->b_data + off, data, tocopy);
1584 bawrite(bp);
1585 data = data + tocopy;
1586 *initp += tocopy;
1587 off = 0;
1588 left -= tocopy;
1589 cn += cl;
1590 ccl -= cl;
1591 }
1592 }
1593
1594 if (left) {
1595 printf("ntfs_writentvattr_plain: POSSIBLE RUN ERROR\n");
1596 error = EINVAL;
1597 }
1598
1599 return (error);
1600 }
1601
1602 /*
1603 * This is one of read routines.
1604 *
1605 * ntnode should be locked.
1606 */
1607 int
1608 ntfs_readntvattr_plain(
1609 struct ntfsmount * ntmp,
1610 struct ntnode * ip,
1611 struct ntvattr * vap,
1612 off_t roff,
1613 size_t rsize,
1614 void *rdata,
1615 size_t * initp,
1616 struct uio *uio)
1617 {
1618 int error = 0;
1619 off_t off;
1620
1621 *initp = 0;
1622 if (vap->va_flag & NTFS_AF_INRUN) {
1623 int cnt;
1624 cn_t ccn, ccl, cn, left, cl;
1625 caddr_t data = rdata;
1626 struct buf *bp;
1627 size_t tocopy;
1628
1629 ddprintf(("ntfs_readntvattr_plain: data in run: %lu chains\n",
1630 vap->va_vruncnt));
1631
1632 off = roff;
1633 left = rsize;
1634 ccl = 0;
1635 ccn = 0;
1636 cnt = 0;
1637 while (left && (cnt < vap->va_vruncnt)) {
1638 ccn = vap->va_vruncn[cnt];
1639 ccl = vap->va_vruncl[cnt];
1640
1641 ddprintf(("ntfs_readntvattr_plain: "
1642 "left %qu, cn: 0x%qx, cl: %qu, off: %qd\n",
1643 (long long) left, (long long) ccn,
1644 (long long) ccl, (long long) off));
1645
1646 if (ntfs_cntob(ccl) < off) {
1647 off -= ntfs_cntob(ccl);
1648 cnt++;
1649 continue;
1650 }
1651 if (ccn || ip->i_number == NTFS_BOOTINO) {
1652 ccl -= ntfs_btocn(off);
1653 cn = ccn + ntfs_btocn(off);
1654 off = ntfs_btocnoff(off);
1655
1656 while (left && ccl) {
1657 /*
1658 * Always read single clusters at a
1659 * time - we need to avoid reading
1660 * differently-sized blocks at the
1661 * same disk offsets to avoid
1662 * confusing the buffer cache.
1663 */
1664 tocopy = MIN(left,
1665 ntfs_cntob(1) - off);
1666 cl = ntfs_btocl(tocopy + off);
1667 KASSERT(cl == 1 &&
1668 tocopy <= ntfs_cntob(1));
1669
1670 ddprintf(("ntfs_readntvattr_plain: "
1671 "read: cn: 0x%qx cl: %qu, "
1672 "off: %qd len: %qu, left: %qu\n",
1673 (long long) cn,
1674 (long long) cl,
1675 (long long) off,
1676 (long long) tocopy,
1677 (long long) left));
1678 error = bread(ntmp->ntm_devvp,
1679 ntfs_cntobn(cn),
1680 ntfs_cntob(cl),
1681 NOCRED, &bp);
1682 if (error) {
1683 brelse(bp);
1684 return (error);
1685 }
1686 if (uio) {
1687 uiomove(bp->b_data + off,
1688 tocopy, uio);
1689 } else {
1690 memcpy(data, bp->b_data + off,
1691 tocopy);
1692 }
1693 brelse(bp);
1694 data = data + tocopy;
1695 *initp += tocopy;
1696 off = 0;
1697 left -= tocopy;
1698 cn += cl;
1699 ccl -= cl;
1700 }
1701 } else {
1702 tocopy = MIN(left, ntfs_cntob(ccl) - off);
1703 ddprintf(("ntfs_readntvattr_plain: "
1704 "hole: ccn: 0x%qx ccl: %qu, off: %qd, "
1705 " len: %qu, left: %qu\n",
1706 (long long) ccn, (long long) ccl,
1707 (long long) off, (long long) tocopy,
1708 (long long) left));
1709 left -= tocopy;
1710 off = 0;
1711 if (uio) {
1712 char vbuf[] = "";
1713 size_t remains = tocopy;
1714 for(; remains; remains--)
1715 uiomove(vbuf, 1, uio);
1716 } else
1717 bzero(data, tocopy);
1718 data = data + tocopy;
1719 }
1720 cnt++;
1721 }
1722 if (left) {
1723 printf("ntfs_readntvattr_plain: POSSIBLE RUN ERROR\n");
1724 error = E2BIG;
1725 }
1726 } else {
1727 ddprintf(("ntfs_readnvattr_plain: data is in mft record\n"));
1728 if (uio)
1729 uiomove(vap->va_datap + roff, rsize, uio);
1730 else
1731 memcpy(rdata, vap->va_datap + roff, rsize);
1732 *initp += rsize;
1733 }
1734
1735 return (error);
1736 }
1737
1738 /*
1739 * This is one of read routines.
1740 */
1741 int
1742 ntfs_readattr_plain(
1743 struct ntfsmount * ntmp,
1744 struct ntnode * ip,
1745 u_int32_t attrnum,
1746 const char *attrname,
1747 off_t roff,
1748 size_t rsize,
1749 void *rdata,
1750 size_t * initp,
1751 struct uio *uio)
1752 {
1753 size_t init;
1754 int error = 0;
1755 off_t off = roff, left = rsize, toread;
1756 caddr_t data = rdata;
1757 struct ntvattr *vap;
1758 *initp = 0;
1759
1760 while (left) {
1761 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1762 ntfs_btocn(off), &vap);
1763 if (error)
1764 return (error);
1765 toread = MIN(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1766 ddprintf(("ntfs_readattr_plain: o: %qd, s: %qd (%qu - %qu)\n",
1767 (long long) off, (long long) toread,
1768 (long long) vap->va_vcnstart,
1769 (long long) vap->va_vcnend));
1770 error = ntfs_readntvattr_plain(ntmp, ip, vap,
1771 off - ntfs_cntob(vap->va_vcnstart),
1772 toread, data, &init, uio);
1773 if (error) {
1774 printf("ntfs_readattr_plain: "
1775 "ntfs_readntvattr_plain failed: o: %qd, s: %qd\n",
1776 (long long) off, (long long) toread);
1777 printf("ntfs_readattr_plain: attrib: %qu - %qu\n",
1778 (long long) vap->va_vcnstart,
1779 (long long) vap->va_vcnend);
1780 ntfs_ntvattrrele(vap);
1781 break;
1782 }
1783 ntfs_ntvattrrele(vap);
1784 left -= toread;
1785 off += toread;
1786 data = data + toread;
1787 *initp += init;
1788 }
1789
1790 return (error);
1791 }
1792
1793 /*
1794 * This is one of read routines.
1795 */
1796 int
1797 ntfs_readattr(
1798 struct ntfsmount * ntmp,
1799 struct ntnode * ip,
1800 u_int32_t attrnum,
1801 const char *attrname,
1802 off_t roff,
1803 size_t rsize,
1804 void *rdata,
1805 struct uio *uio)
1806 {
1807 int error = 0;
1808 struct ntvattr *vap;
1809 size_t init;
1810
1811 ddprintf(("ntfs_readattr: reading %llu: 0x%x, from %qd size %qu"
1812 " bytes\n", (unsigned long long)ip->i_number, attrnum,
1813 (long long)roff, (long long)rsize));
1814
1815 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname, 0, &vap);
1816 if (error)
1817 return (error);
1818
1819 if ((roff > vap->va_datalen) ||
1820 (roff + rsize > vap->va_datalen)) {
1821 printf("ntfs_readattr: offset too big: %qd (%qd) > %qu\n",
1822 (long long) roff, (long long) (roff + rsize),
1823 (long long) vap->va_datalen);
1824 ntfs_ntvattrrele(vap);
1825 return (E2BIG);
1826 }
1827 if (vap->va_compression && vap->va_compressalg) {
1828 u_int8_t *cup;
1829 u_int8_t *uup;
1830 off_t off = roff, left = rsize, tocopy;
1831 caddr_t data = rdata;
1832 cn_t cn;
1833
1834 ddprintf(("ntfs_ntreadattr: compression: %d\n",
1835 vap->va_compressalg));
1836
1837 cup = malloc(ntfs_cntob(NTFS_COMPUNIT_CL),
1838 M_NTFSDECOMP, M_WAITOK);
1839 uup = malloc(ntfs_cntob(NTFS_COMPUNIT_CL),
1840 M_NTFSDECOMP, M_WAITOK);
1841
1842 cn = (ntfs_btocn(roff)) & (~(NTFS_COMPUNIT_CL - 1));
1843 off = roff - ntfs_cntob(cn);
1844
1845 while (left) {
1846 error = ntfs_readattr_plain(ntmp, ip, attrnum,
1847 attrname, ntfs_cntob(cn),
1848 ntfs_cntob(NTFS_COMPUNIT_CL),
1849 cup, &init, NULL);
1850 if (error)
1851 break;
1852
1853 tocopy = MIN(left, ntfs_cntob(NTFS_COMPUNIT_CL) - off);
1854
1855 if (init == ntfs_cntob(NTFS_COMPUNIT_CL)) {
1856 if (uio)
1857 uiomove(cup + off, tocopy, uio);
1858 else
1859 memcpy(data, cup + off, tocopy);
1860 } else if (init == 0) {
1861 if (uio) {
1862 char vbuf[] = "";
1863 size_t remains = tocopy;
1864 for(; remains; remains--)
1865 uiomove(vbuf, 1, uio);
1866 }
1867 else
1868 bzero(data, tocopy);
1869 } else {
1870 error = ntfs_uncompunit(ntmp, uup, cup);
1871 if (error)
1872 break;
1873 if (uio)
1874 uiomove(uup + off, tocopy, uio);
1875 else
1876 memcpy(data, uup + off, tocopy);
1877 }
1878
1879 left -= tocopy;
1880 data = data + tocopy;
1881 off += tocopy - ntfs_cntob(NTFS_COMPUNIT_CL);
1882 cn += NTFS_COMPUNIT_CL;
1883 }
1884
1885 FREE(uup, M_NTFSDECOMP);
1886 FREE(cup, M_NTFSDECOMP);
1887 } else
1888 error = ntfs_readattr_plain(ntmp, ip, attrnum, attrname,
1889 roff, rsize, rdata, &init, uio);
1890 ntfs_ntvattrrele(vap);
1891 return (error);
1892 }
1893
1894 #if UNUSED_CODE
1895 int
1896 ntfs_parserun(
1897 cn_t * cn,
1898 cn_t * cl,
1899 u_int8_t * run,
1900 u_long len,
1901 u_long *off)
1902 {
1903 u_int8_t sz;
1904 int i;
1905
1906 if (NULL == run) {
1907 printf("ntfs_parsetun: run == NULL\n");
1908 return (EINVAL);
1909 }
1910 sz = run[(*off)++];
1911 if (0 == sz) {
1912 printf("ntfs_parserun: trying to go out of run\n");
1913 return (E2BIG);
1914 }
1915 *cl = 0;
1916 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1917 printf("ntfs_parserun: "
1918 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1919 sz, len, *off);
1920 return (EINVAL);
1921 }
1922 for (i = 0; i < (sz & 0xF); i++)
1923 *cl += (u_int32_t) run[(*off)++] << (i << 3);
1924
1925 sz >>= 4;
1926 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1927 printf("ntfs_parserun: "
1928 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1929 sz, len, *off);
1930 return (EINVAL);
1931 }
1932 for (i = 0; i < (sz & 0xF); i++)
1933 *cn += (u_int32_t) run[(*off)++] << (i << 3);
1934
1935 return (0);
1936 }
1937 #endif
1938
1939 /*
1940 * Process fixup routine on given buffer.
1941 */
1942 int
1943 ntfs_procfixups(
1944 struct ntfsmount * ntmp,
1945 u_int32_t magic,
1946 caddr_t xbuf,
1947 size_t len)
1948 {
1949 struct fixuphdr *fhp = (struct fixuphdr *) xbuf;
1950 int i;
1951 u_int16_t fixup;
1952 u_int16_t *fxp;
1953 u_int16_t *cfxp;
1954
1955 if (fhp->fh_magic != magic) {
1956 printf("ntfs_procfixups: magic doesn't match: %08x != %08x\n",
1957 fhp->fh_magic, magic);
1958 return (EINVAL);
1959 }
1960 if ((fhp->fh_fnum - 1) * ntmp->ntm_bps != len) {
1961 printf("ntfs_procfixups: "
1962 "bad fixups number: %d for %ld bytes block\n",
1963 fhp->fh_fnum, (long)len); /* XXX printf kludge */
1964 return (EINVAL);
1965 }
1966 if (fhp->fh_foff >= ntmp->ntm_spc * ntmp->ntm_mftrecsz * ntmp->ntm_bps) {
1967 printf("ntfs_procfixups: invalid offset: %x", fhp->fh_foff);
1968 return (EINVAL);
1969 }
1970 fxp = (u_int16_t *) (xbuf + fhp->fh_foff);
1971 cfxp = (u_int16_t *) (xbuf + ntmp->ntm_bps - 2);
1972 fixup = *fxp++;
1973 for (i = 1; i < fhp->fh_fnum; i++, fxp++) {
1974 if (*cfxp != fixup) {
1975 printf("ntfs_procfixups: fixup %d doesn't match\n", i);
1976 return (EINVAL);
1977 }
1978 *cfxp = *fxp;
1979 cfxp = (u_int16_t *)((caddr_t)cfxp + ntmp->ntm_bps);
1980 }
1981 return (0);
1982 }
1983
1984 #if UNUSED_CODE
1985 int
1986 ntfs_runtocn(
1987 cn_t * cn,
1988 struct ntfsmount * ntmp,
1989 u_int8_t * run,
1990 u_long len,
1991 cn_t vcn)
1992 {
1993 cn_t ccn = 0;
1994 cn_t ccl = 0;
1995 u_long off = 0;
1996 int error = 0;
1997
1998 #ifdef NTFS_DEBUG
1999 int i;
2000 printf("ntfs_runtocn: run: %p, %ld bytes, vcn:%ld\n",
2001 run, len, (u_long) vcn);
2002 printf("ntfs_runtocn: run: ");
2003 for (i = 0; i < len; i++)
2004 printf("0x%02x ", run[i]);
2005 printf("\n");
2006 #endif
2007
2008 if (NULL == run) {
2009 printf("ntfs_runtocn: run == NULL\n");
2010 return (EINVAL);
2011 }
2012 do {
2013 if (run[off] == 0) {
2014 printf("ntfs_runtocn: vcn too big\n");
2015 return (E2BIG);
2016 }
2017 vcn -= ccl;
2018 error = ntfs_parserun(&ccn, &ccl, run, len, &off);
2019 if (error) {
2020 printf("ntfs_runtocn: ntfs_parserun failed\n");
2021 return (error);
2022 }
2023 } while (ccl <= vcn);
2024 *cn = ccn + vcn;
2025 return (0);
2026 }
2027 #endif
2028
2029 /*
2030 * this initializes toupper table & dependant variables to be ready for
2031 * later work
2032 */
2033 void
2034 ntfs_toupper_init()
2035 {
2036 ntfs_toupper_tab = (wchar *) NULL;
2037 mutex_init(&ntfs_toupper_lock, MUTEX_DEFAULT, IPL_NONE);
2038 ntfs_toupper_usecount = 0;
2039 }
2040
2041 /*
2042 * if the ntfs_toupper_tab[] is filled already, just raise use count;
2043 * otherwise read the data from the filesystem we are currently mounting
2044 */
2045 int
2046 ntfs_toupper_use(mp, ntmp)
2047 struct mount *mp;
2048 struct ntfsmount *ntmp;
2049 {
2050 int error = 0;
2051 struct vnode *vp;
2052
2053 /* get exclusive access */
2054 mutex_enter(&ntfs_toupper_lock);
2055
2056 /* only read the translation data from a file if it hasn't been
2057 * read already */
2058 if (ntfs_toupper_tab)
2059 goto out;
2060
2061 /*
2062 * Read in Unicode lowercase -> uppercase translation file.
2063 * XXX for now, just the first 256 entries are used anyway,
2064 * so don't bother reading more
2065 */
2066 MALLOC(ntfs_toupper_tab, wchar *, 256 * 256 * sizeof(wchar),
2067 M_NTFSRDATA, M_WAITOK);
2068
2069 if ((error = VFS_VGET(mp, NTFS_UPCASEINO, &vp)))
2070 goto out;
2071 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
2072 0, 256*256*sizeof(wchar), (char *) ntfs_toupper_tab,
2073 NULL);
2074 vput(vp);
2075
2076 out:
2077 ntfs_toupper_usecount++;
2078 mutex_exit(&ntfs_toupper_lock);
2079 return (error);
2080 }
2081
2082 /*
2083 * lower the use count and if it reaches zero, free the memory
2084 * tied by toupper table
2085 */
2086 void
2087 ntfs_toupper_unuse()
2088 {
2089 /* get exclusive access */
2090 mutex_enter(&ntfs_toupper_lock);
2091
2092 ntfs_toupper_usecount--;
2093 if (ntfs_toupper_usecount == 0) {
2094 FREE(ntfs_toupper_tab, M_NTFSRDATA);
2095 ntfs_toupper_tab = NULL;
2096 }
2097 #ifdef DIAGNOSTIC
2098 else if (ntfs_toupper_usecount < 0) {
2099 panic("ntfs_toupper_unuse(): use count negative: %d",
2100 ntfs_toupper_usecount);
2101 }
2102 #endif
2103
2104 /* release the lock */
2105 mutex_exit(&ntfs_toupper_lock);
2106 }
2107