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