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