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