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