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