ntfs_subr.c revision 1.50 1 /* $NetBSD: ntfs_subr.c,v 1.50 2013/06/28 15:46:37 christos 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.50 2013/06/28 15:46:37 christos 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; /* Root attribute */
869 cn_t cn = 0; /* VCN in current attribute */
870 void * rdbuf; /* 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 return (ENOTDIR);
892
893 /*
894 * Divide file name into: foofilefoofilefoofile[:attrspec]
895 * Store like this: fname:fnamelen [aname:anamelen]
896 */
897 fname = cnp->cn_nameptr;
898 aname = NULL;
899 anamelen = 0;
900 for (fnamelen = 0; fnamelen < cnp->cn_namelen; fnamelen++)
901 if(fname[fnamelen] == ':') {
902 aname = fname + fnamelen + 1;
903 anamelen = cnp->cn_namelen - fnamelen - 1;
904 dprintf(("%s: %s (%d), attr: %s (%d)\n", __func__,
905 fname, fnamelen, aname, anamelen));
906 break;
907 }
908
909 blsize = vap->va_a_iroot->ir_size;
910 dprintf(("%s: blksz: %d\n", __func__, blsize));
911
912 rdbuf = malloc(blsize, M_TEMP, M_WAITOK);
913
914 loop:
915 rdsize = vap->va_datalen;
916 dprintf(("%s: rdsz: %d\n", __func__, rdsize));
917
918 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXROOT, "$I30",
919 0, rdsize, rdbuf, NULL);
920 if (error)
921 goto fail;
922
923 aoff = sizeof(struct attr_indexroot);
924
925 do {
926 iep = (struct attr_indexentry *) ((char *)rdbuf + aoff);
927
928 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
929 aoff += iep->reclen,
930 iep = (struct attr_indexentry *) ((char *)rdbuf + aoff))
931 {
932 ddprintf(("%s: fscan: %d, %d\n", __func__,
933 (u_int32_t) iep->ie_number,
934 (u_int32_t) iep->ie_fnametype));
935
936 /* check the name - the case-insensitive check
937 * has to come first, to break from this for loop
938 * if needed, so we can dive correctly */
939 res = ntfs_uastricmp(ntmp, iep->ie_fname,
940 iep->ie_fnamelen, fname, fnamelen);
941 if (!fullscan) {
942 if (res > 0) break;
943 if (res < 0) continue;
944 }
945
946 if (iep->ie_fnametype == 0 ||
947 !(ntmp->ntm_flag & NTFS_MFLAG_CASEINS))
948 {
949 res = ntfs_uastrcmp(ntmp, iep->ie_fname,
950 iep->ie_fnamelen, fname, fnamelen);
951 if (res != 0 && !fullscan) continue;
952 }
953
954 /* if we perform full scan, the file does not match
955 * and this is subnode, dive */
956 if (fullscan && res != 0) {
957 if (iep->ie_flag & NTFS_IEFLAG_SUBNODE) {
958 tctx = malloc(sizeof(*tctx), M_TEMP, M_WAITOK);
959 tctx->aoff = aoff + iep->reclen;
960 tctx->rdsize = rdsize;
961 tctx->cn = cn;
962 tctx->prev = lookup_ctx;
963 lookup_ctx = tctx;
964 break;
965 } else
966 continue;
967 }
968
969 if (aname) {
970 error = ntfs_ntlookupattr(ntmp,
971 aname, anamelen,
972 &attrtype, &attrname);
973 if (error)
974 goto fail;
975 }
976
977 /* Check if we've found ourselves */
978 if ((iep->ie_number == ip->i_number) &&
979 (attrtype == fp->f_attrtype) &&
980 ((!attrname && !fp->f_attrname) ||
981 (attrname && fp->f_attrname &&
982 !strcmp(attrname, fp->f_attrname))))
983 {
984 vref(vp);
985 *vpp = vp;
986 error = 0;
987 goto fail;
988 }
989
990 /* free the buffer returned by ntfs_ntlookupattr() */
991 if (attrname) {
992 free(attrname, M_TEMP);
993 attrname = NULL;
994 }
995
996 /* vget node, but don't load it */
997 error = ntfs_vgetex(ntmp->ntm_mountp,
998 iep->ie_number, attrtype, attrname,
999 LK_EXCLUSIVE, VG_DONTLOADIN | VG_DONTVALIDFN,
1000 &nvp);
1001 if (error)
1002 goto fail;
1003
1004 nfp = VTOF(nvp);
1005
1006 if (nfp->f_flag & FN_VALID) {
1007 *vpp = nvp;
1008 goto fail;
1009 }
1010
1011 nfp->f_fflag = iep->ie_fflag;
1012 nfp->f_pnumber = iep->ie_fpnumber;
1013 nfp->f_times = iep->ie_ftimes;
1014
1015 if((nfp->f_fflag & NTFS_FFLAG_DIR) &&
1016 (nfp->f_attrtype == NTFS_A_DATA) &&
1017 (nfp->f_attrname == NULL))
1018 f_type = VDIR;
1019 else
1020 f_type = VREG;
1021
1022 nvp->v_type = f_type;
1023
1024 if ((nfp->f_attrtype == NTFS_A_DATA) &&
1025 (nfp->f_attrname == NULL))
1026 {
1027 /* Opening default attribute */
1028 nfp->f_size = iep->ie_fsize;
1029 nfp->f_allocated = iep->ie_fallocated;
1030 nfp->f_flag |= FN_PRELOADED;
1031 uvm_vnp_setsize(nvp, iep->ie_fsize);
1032 } else {
1033 error = ntfs_filesize(ntmp, nfp,
1034 &nfp->f_size, &nfp->f_allocated);
1035 if (error) {
1036 vput(nvp);
1037 goto fail;
1038 }
1039 uvm_vnp_setsize(nvp, nfp->f_size);
1040 }
1041
1042 nfp->f_flag &= ~FN_VALID;
1043 *vpp = nvp;
1044 goto fail;
1045 }
1046
1047 /* Dive if possible */
1048 if (iep->ie_flag & NTFS_IEFLAG_SUBNODE) {
1049 dprintf(("%s: diving\n", __func__));
1050
1051 cn = *(cn_t *) ((char *)rdbuf + aoff +
1052 iep->reclen - sizeof(cn_t));
1053 rdsize = blsize;
1054
1055 error = ntfs_readattr(ntmp, ip, NTFS_A_INDX, "$I30",
1056 ntfs_cntob(cn), rdsize, rdbuf, NULL);
1057 if (error)
1058 goto fail;
1059
1060 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1061 rdbuf, rdsize);
1062 if (error)
1063 goto fail;
1064
1065 aoff = (((struct attr_indexalloc *) rdbuf)->ia_hdrsize +
1066 0x18);
1067 } else if (fullscan && lookup_ctx) {
1068 cn = lookup_ctx->cn;
1069 aoff = lookup_ctx->aoff;
1070 rdsize = lookup_ctx->rdsize;
1071
1072 error = ntfs_readattr(ntmp, ip,
1073 (cn == 0) ? NTFS_A_INDXROOT : NTFS_A_INDX,
1074 "$I30", ntfs_cntob(cn), rdsize, rdbuf, NULL);
1075 if (error)
1076 goto fail;
1077
1078 if (cn != 0) {
1079 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1080 rdbuf, rdsize);
1081 if (error)
1082 goto fail;
1083 }
1084
1085 tctx = lookup_ctx;
1086 lookup_ctx = lookup_ctx->prev;
1087 free(tctx, M_TEMP);
1088 } else {
1089 dprintf(("%s: nowhere to dive :-(\n", __func__));
1090 error = ENOENT;
1091 break;
1092 }
1093 } while (1);
1094
1095 /* perform full scan if no entry was found */
1096 if (!fullscan && error == ENOENT) {
1097 fullscan = 1;
1098 cn = 0; /* need zero, used by lookup_ctx */
1099
1100 ddprintf(("%s: fullscan performed for: %.*s\n", __func__,
1101 (int) fnamelen, fname));
1102 goto loop;
1103 }
1104
1105 dprintf(("finish\n"));
1106
1107 fail:
1108 if (attrname)
1109 free(attrname, M_TEMP);
1110 if (lookup_ctx) {
1111 while(lookup_ctx) {
1112 tctx = lookup_ctx;
1113 lookup_ctx = lookup_ctx->prev;
1114 free(tctx, M_TEMP);
1115 }
1116 }
1117 ntfs_ntvattrrele(vap);
1118 ntfs_ntput(ip);
1119 free(rdbuf, M_TEMP);
1120 return (error);
1121 }
1122
1123 /*
1124 * Check if name type is permitted to show.
1125 */
1126 int
1127 ntfs_isnamepermitted(
1128 struct ntfsmount * ntmp,
1129 struct attr_indexentry * iep)
1130 {
1131 if (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)
1132 return 1;
1133
1134 switch (iep->ie_fnametype) {
1135 case 2:
1136 ddprintf(("%s: skipped DOS name\n", __func__));
1137 return 0;
1138 case 0: case 1: case 3:
1139 return 1;
1140 default:
1141 printf("%s: WARNING! Unknown file name type: %d\n", __func__,
1142 iep->ie_fnametype);
1143 break;
1144 }
1145 return 0;
1146 }
1147
1148 /*
1149 * Read ntfs dir like stream of attr_indexentry, not like btree of them.
1150 * This is done by scanning $BITMAP:$I30 for busy clusters and reading them.
1151 * Of course $INDEX_ROOT:$I30 is read before. Last read values are stored in
1152 * fnode, so we can skip toward record number num almost immediately.
1153 * Anyway this is rather slow routine. The problem is that we don't know
1154 * how many records are there in $INDEX_ALLOCATION:$I30 block.
1155 */
1156 int
1157 ntfs_ntreaddir(
1158 struct ntfsmount * ntmp,
1159 struct fnode * fp,
1160 u_int32_t num,
1161 struct attr_indexentry ** riepp)
1162 {
1163 struct ntnode *ip = FTONT(fp);
1164 struct ntvattr *vap = NULL; /* IndexRoot attribute */
1165 struct ntvattr *bmvap = NULL; /* BitMap attribute */
1166 struct ntvattr *iavap = NULL; /* IndexAllocation attribute */
1167 void * rdbuf; /* Buffer to read directory's blocks */
1168 u_char *bmp = NULL; /* Bitmap */
1169 u_int32_t blsize; /* Index allocation size (2048) */
1170 u_int32_t rdsize; /* Length of data to read */
1171 u_int32_t attrnum; /* Current attribute type */
1172 u_int32_t cpbl = 1; /* Clusters per directory block */
1173 u_int32_t blnum;
1174 struct attr_indexentry *iep;
1175 int error = ENOENT;
1176 u_int32_t aoff, cnum;
1177
1178 dprintf(("%s: read ino: %llu, num: %d\n", __func__,
1179 (unsigned long long)ip->i_number, num));
1180 error = ntfs_ntget(ip);
1181 if (error)
1182 return (error);
1183
1184 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
1185 if (error)
1186 return (ENOTDIR);
1187
1188 if (fp->f_dirblbuf == NULL) {
1189 fp->f_dirblsz = vap->va_a_iroot->ir_size;
1190 fp->f_dirblbuf = malloc(MAX(vap->va_datalen, fp->f_dirblsz),
1191 M_NTFSDIR, M_WAITOK);
1192 }
1193
1194 blsize = fp->f_dirblsz;
1195 rdbuf = fp->f_dirblbuf;
1196
1197 dprintf(("%s: rdbuf: %p, blsize: %d\n", __func__, rdbuf, blsize));
1198
1199 if (vap->va_a_iroot->ir_flag & NTFS_IRFLAG_INDXALLOC) {
1200 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXBITMAP, "$I30",
1201 0, &bmvap);
1202 if (error) {
1203 error = ENOTDIR;
1204 goto fail;
1205 }
1206 bmp = (u_char *) malloc(bmvap->va_datalen, M_TEMP, M_WAITOK);
1207 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXBITMAP, "$I30", 0,
1208 bmvap->va_datalen, bmp, NULL);
1209 if (error)
1210 goto fail;
1211
1212 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDX, "$I30",
1213 0, &iavap);
1214 if (error) {
1215 error = ENOTDIR;
1216 goto fail;
1217 }
1218 cpbl = ntfs_btocn(blsize + ntfs_cntob(1) - 1);
1219 dprintf(("%s: indexalloc: %qu, cpbl: %d\n", __func__,
1220 (long long)iavap->va_datalen, cpbl));
1221 } else {
1222 dprintf(("%s: w/o BitMap and IndexAllocation\n", __func__));
1223 iavap = bmvap = NULL;
1224 bmp = NULL;
1225 }
1226
1227 /* Try use previous values */
1228 if ((fp->f_lastdnum < num) && (fp->f_lastdnum != 0)) {
1229 attrnum = fp->f_lastdattr;
1230 aoff = fp->f_lastdoff;
1231 blnum = fp->f_lastdblnum;
1232 cnum = fp->f_lastdnum;
1233 } else {
1234 attrnum = NTFS_A_INDXROOT;
1235 aoff = sizeof(struct attr_indexroot);
1236 blnum = 0;
1237 cnum = 0;
1238 }
1239
1240 do {
1241 dprintf(("%s: scan: 0x%x, %d, %d, %d, %d\n", __func__,
1242 attrnum, (u_int32_t) blnum, cnum, num, aoff));
1243 rdsize = (attrnum == NTFS_A_INDXROOT) ? vap->va_datalen : blsize;
1244 error = ntfs_readattr(ntmp, ip, attrnum, "$I30",
1245 ntfs_cntob(blnum * cpbl), rdsize, rdbuf, NULL);
1246 if (error)
1247 goto fail;
1248
1249 if (attrnum == NTFS_A_INDX) {
1250 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1251 rdbuf, rdsize);
1252 if (error)
1253 goto fail;
1254 }
1255 if (aoff == 0)
1256 aoff = (attrnum == NTFS_A_INDX) ?
1257 (0x18 + ((struct attr_indexalloc *) rdbuf)->ia_hdrsize) :
1258 sizeof(struct attr_indexroot);
1259
1260 iep = (struct attr_indexentry *) ((char *)rdbuf + aoff);
1261 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
1262 aoff += iep->reclen,
1263 iep = (struct attr_indexentry *) ((char *)rdbuf + aoff))
1264 {
1265 if (!ntfs_isnamepermitted(ntmp, iep)) continue;
1266
1267 if (cnum >= num) {
1268 fp->f_lastdnum = cnum;
1269 fp->f_lastdoff = aoff;
1270 fp->f_lastdblnum = blnum;
1271 fp->f_lastdattr = attrnum;
1272
1273 *riepp = iep;
1274
1275 error = 0;
1276 goto fail;
1277 }
1278 cnum++;
1279 }
1280
1281 if (iavap) {
1282 if (attrnum == NTFS_A_INDXROOT)
1283 blnum = 0;
1284 else
1285 blnum++;
1286
1287 while (ntfs_cntob(blnum * cpbl) < iavap->va_datalen) {
1288 if (bmp[blnum >> 3] & (1 << (blnum & 3)))
1289 break;
1290 blnum++;
1291 }
1292
1293 attrnum = NTFS_A_INDX;
1294 aoff = 0;
1295 if (ntfs_cntob(blnum * cpbl) >= iavap->va_datalen)
1296 break;
1297 dprintf(("%s: blnum: %d\n", __func__,
1298 (u_int32_t) blnum));
1299 }
1300 } while (iavap);
1301
1302 *riepp = NULL;
1303 fp->f_lastdnum = 0;
1304
1305 fail:
1306 if (vap)
1307 ntfs_ntvattrrele(vap);
1308 if (bmvap)
1309 ntfs_ntvattrrele(bmvap);
1310 if (iavap)
1311 ntfs_ntvattrrele(iavap);
1312 if (bmp)
1313 free(bmp, M_TEMP);
1314 ntfs_ntput(ip);
1315 return (error);
1316 }
1317
1318 /*
1319 * Convert NTFS times that are in 100 ns units and begins from
1320 * 1601 Jan 1 into unix times.
1321 */
1322 struct timespec
1323 ntfs_nttimetounix(
1324 u_int64_t nt)
1325 {
1326 struct timespec t;
1327
1328 /* WindowNT times are in 100 ns and from 1601 Jan 1 */
1329 t.tv_nsec = (nt % (1000 * 1000 * 10)) * 100;
1330 t.tv_sec = nt / (1000 * 1000 * 10) -
1331 369LL * 365LL * 24LL * 60LL * 60LL -
1332 89LL * 1LL * 24LL * 60LL * 60LL;
1333 return (t);
1334 }
1335
1336 /*
1337 * Get file times from NTFS_A_NAME attribute.
1338 */
1339 int
1340 ntfs_times(
1341 struct ntfsmount * ntmp,
1342 struct ntnode * ip,
1343 ntfs_times_t * tm)
1344 {
1345 struct ntvattr *vap;
1346 int error;
1347
1348 dprintf(("%s: ino: %llu...\n", __func__,
1349 (unsigned long long)ip->i_number));
1350
1351 error = ntfs_ntget(ip);
1352 if (error)
1353 return (error);
1354
1355 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_NAME, NULL, 0, &vap);
1356 if (error) {
1357 ntfs_ntput(ip);
1358 return (error);
1359 }
1360 *tm = vap->va_a_name->n_times;
1361 ntfs_ntvattrrele(vap);
1362 ntfs_ntput(ip);
1363
1364 return (0);
1365 }
1366
1367 /*
1368 * Get file sizes from corresponding attribute.
1369 *
1370 * ntnode under fnode should be locked.
1371 */
1372 int
1373 ntfs_filesize(
1374 struct ntfsmount * ntmp,
1375 struct fnode * fp,
1376 u_int64_t * size,
1377 u_int64_t * bytes)
1378 {
1379 struct ntvattr *vap;
1380 struct ntnode *ip = FTONT(fp);
1381 u_int64_t sz, bn;
1382 int error;
1383
1384 dprintf(("%s: ino: %llu\n", __func__,
1385 (unsigned long long)ip->i_number));
1386
1387 error = ntfs_ntvattrget(ntmp, ip,
1388 fp->f_attrtype, fp->f_attrname, 0, &vap);
1389 if (error)
1390 return (error);
1391
1392 bn = vap->va_allocated;
1393 sz = vap->va_datalen;
1394
1395 dprintf(("%s: %d bytes (%d bytes allocated)\n", __func__,
1396 (u_int32_t) sz, (u_int32_t) bn));
1397
1398 if (size)
1399 *size = sz;
1400 if (bytes)
1401 *bytes = bn;
1402
1403 ntfs_ntvattrrele(vap);
1404
1405 return (0);
1406 }
1407
1408 /*
1409 * This is one of write routine.
1410 */
1411 int
1412 ntfs_writeattr_plain(
1413 struct ntfsmount * ntmp,
1414 struct ntnode * ip,
1415 u_int32_t attrnum,
1416 char *attrname,
1417 off_t roff,
1418 size_t rsize,
1419 void *rdata,
1420 size_t * initp,
1421 struct uio *uio)
1422 {
1423 size_t init;
1424 int error = 0;
1425 off_t off = roff, left = rsize, towrite;
1426 void * data = rdata;
1427 struct ntvattr *vap;
1428 *initp = 0;
1429
1430 while (left) {
1431 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1432 ntfs_btocn(off), &vap);
1433 if (error)
1434 return (error);
1435 towrite = MIN(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1436 ddprintf(("%s: o: %qd, s: %qd (%qu - %qu)\n", __func__,
1437 (long long) off, (long long) towrite,
1438 (long long) vap->va_vcnstart,
1439 (long long) vap->va_vcnend));
1440 error = ntfs_writentvattr_plain(ntmp, ip, vap,
1441 off - ntfs_cntob(vap->va_vcnstart),
1442 towrite, data, &init, uio);
1443 if (error) {
1444 dprintf(("%s: "
1445 "ntfs_writentvattr_plain failed: o: %qd, s: %qd\n",
1446 __func__, (long long) off, (long long) towrite));
1447 dprintf(("%s: attrib: %qu - %qu\n", __func__,
1448 (long long) vap->va_vcnstart,
1449 (long long) vap->va_vcnend));
1450 ntfs_ntvattrrele(vap);
1451 break;
1452 }
1453 ntfs_ntvattrrele(vap);
1454 left -= towrite;
1455 off += towrite;
1456 data = (char *)data + towrite;
1457 *initp += init;
1458 }
1459
1460 return (error);
1461 }
1462
1463 /*
1464 * This is one of write routine.
1465 *
1466 * ntnode should be locked.
1467 */
1468 int
1469 ntfs_writentvattr_plain(
1470 struct ntfsmount * ntmp,
1471 struct ntnode * ip,
1472 struct ntvattr * vap,
1473 off_t roff,
1474 size_t rsize,
1475 void *rdata,
1476 size_t * initp,
1477 struct uio *uio)
1478 {
1479 int error = 0;
1480 off_t off;
1481 int cnt;
1482 cn_t ccn, ccl, cn, left, cl;
1483 void * data = rdata;
1484 daddr_t lbn;
1485 struct buf *bp;
1486 size_t tocopy;
1487
1488 *initp = 0;
1489
1490 if ((vap->va_flag & NTFS_AF_INRUN) == 0) {
1491 dprintf(("%s: CAN'T WRITE RES. ATTRIBUTE\n", __func__));
1492 return ENOTTY;
1493 }
1494
1495 ddprintf(("%s: data in run: %lu chains\n", __func__,
1496 vap->va_vruncnt));
1497
1498 off = roff;
1499 left = rsize;
1500 ccl = 0;
1501 ccn = 0;
1502 cnt = 0;
1503 for (; left && (cnt < vap->va_vruncnt); cnt++) {
1504 ccn = vap->va_vruncn[cnt];
1505 ccl = vap->va_vruncl[cnt];
1506
1507 ddprintf(("%s: left %qu, cn: 0x%qx, cl: %qu, off: %qd\n",
1508 __func__, (long long) left, (long long) ccn,
1509 (long long) ccl, (long long) off));
1510
1511 if (ntfs_cntob(ccl) < off) {
1512 off -= ntfs_cntob(ccl);
1513 cnt++;
1514 continue;
1515 }
1516 if (!ccn && ip->i_number != NTFS_BOOTINO)
1517 continue; /* XXX */
1518
1519 ccl -= ntfs_btocn(off);
1520 cn = ccn + ntfs_btocn(off);
1521 off = ntfs_btocnoff(off);
1522
1523 while (left && ccl) {
1524 /*
1525 * Always read and write single clusters at a time -
1526 * we need to avoid requesting differently-sized
1527 * blocks at the same disk offsets to avoid
1528 * confusing the buffer cache.
1529 */
1530 tocopy = MIN(left, ntfs_cntob(1) - off);
1531 cl = ntfs_btocl(tocopy + off);
1532 KASSERT(cl == 1 && tocopy <= ntfs_cntob(1));
1533 ddprintf(("%s: write: cn: 0x%qx cl: %qu, off: %qd "
1534 "len: %qu, left: %qu\n", __func__,
1535 (long long) cn, (long long) cl,
1536 (long long) off, (long long) tocopy,
1537 (long long) left));
1538 if ((off == 0) && (tocopy == ntfs_cntob(cl)))
1539 {
1540 lbn = ntfs_cntobn(cn);
1541 bp = getblk(ntmp->ntm_devvp, lbn,
1542 ntfs_cntob(cl), 0, 0);
1543 clrbuf(bp);
1544 } else {
1545 error = bread(ntmp->ntm_devvp, ntfs_cntobn(cn),
1546 ntfs_cntob(cl), NOCRED, B_MODIFY, &bp);
1547 if (error) {
1548 return (error);
1549 }
1550 }
1551 if (uio)
1552 uiomove((char *)bp->b_data + off, tocopy, uio);
1553 else
1554 memcpy((char *)bp->b_data + off, data, tocopy);
1555 bawrite(bp);
1556 data = (char *)data + tocopy;
1557 *initp += tocopy;
1558 off = 0;
1559 left -= tocopy;
1560 cn += cl;
1561 ccl -= cl;
1562 }
1563 }
1564
1565 if (left) {
1566 printf("%s: POSSIBLE RUN ERROR\n", __func__);
1567 error = EINVAL;
1568 }
1569
1570 return (error);
1571 }
1572
1573 /*
1574 * This is one of read routines.
1575 *
1576 * ntnode should be locked.
1577 */
1578 int
1579 ntfs_readntvattr_plain(
1580 struct ntfsmount * ntmp,
1581 struct ntnode * ip,
1582 struct ntvattr * vap,
1583 off_t roff,
1584 size_t rsize,
1585 void *rdata,
1586 size_t * initp,
1587 struct uio *uio)
1588 {
1589 int error = 0;
1590 off_t off;
1591
1592 *initp = 0;
1593 if (vap->va_flag & NTFS_AF_INRUN) {
1594 int cnt;
1595 cn_t ccn, ccl, cn, left, cl;
1596 void * data = rdata;
1597 struct buf *bp;
1598 size_t tocopy;
1599
1600 ddprintf(("%s: data in run: %lu chains\n", __func__,
1601 vap->va_vruncnt));
1602
1603 off = roff;
1604 left = rsize;
1605 ccl = 0;
1606 ccn = 0;
1607 cnt = 0;
1608 while (left && (cnt < vap->va_vruncnt)) {
1609 ccn = vap->va_vruncn[cnt];
1610 ccl = vap->va_vruncl[cnt];
1611
1612 ddprintf(("%s: left %qu, cn: 0x%qx, cl: %qu, "
1613 "off: %qd\n", __func__,
1614 (long long) left, (long long) ccn,
1615 (long long) ccl, (long long) off));
1616
1617 if (ntfs_cntob(ccl) < off) {
1618 off -= ntfs_cntob(ccl);
1619 cnt++;
1620 continue;
1621 }
1622 if (ccn || ip->i_number == NTFS_BOOTINO) {
1623 ccl -= ntfs_btocn(off);
1624 cn = ccn + ntfs_btocn(off);
1625 off = ntfs_btocnoff(off);
1626
1627 while (left && ccl) {
1628 /*
1629 * Always read single clusters at a
1630 * time - we need to avoid reading
1631 * differently-sized blocks at the
1632 * same disk offsets to avoid
1633 * confusing the buffer cache.
1634 */
1635 tocopy = MIN(left,
1636 ntfs_cntob(1) - off);
1637 cl = ntfs_btocl(tocopy + off);
1638 KASSERT(cl == 1 &&
1639 tocopy <= ntfs_cntob(1));
1640
1641 ddprintf(("%s: read: cn: 0x%qx cl: %qu,"
1642 " off: %qd len: %qu, left: %qu\n",
1643 __func__, (long long) cn,
1644 (long long) cl,
1645 (long long) off,
1646 (long long) tocopy,
1647 (long long) left));
1648 error = bread(ntmp->ntm_devvp,
1649 ntfs_cntobn(cn),
1650 ntfs_cntob(cl),
1651 NOCRED, 0, &bp);
1652 if (error) {
1653 return (error);
1654 }
1655 if (uio) {
1656 uiomove((char *)bp->b_data + off,
1657 tocopy, uio);
1658 } else {
1659 memcpy(data, (char *)bp->b_data + off,
1660 tocopy);
1661 }
1662 brelse(bp, 0);
1663 data = (char *)data + tocopy;
1664 *initp += tocopy;
1665 off = 0;
1666 left -= tocopy;
1667 cn += cl;
1668 ccl -= cl;
1669 }
1670 } else {
1671 tocopy = MIN(left, ntfs_cntob(ccl) - off);
1672 ddprintf(("%s: hole: ccn: 0x%qx ccl: %qu, "
1673 "off: %qd, len: %qu, left: %qu\n", __func__,
1674 (long long) ccn, (long long) ccl,
1675 (long long) off, (long long) tocopy,
1676 (long long) left));
1677 left -= tocopy;
1678 off = 0;
1679 if (uio) {
1680 char vbuf[] = "";
1681 size_t remains = tocopy;
1682 for(; remains; remains--)
1683 uiomove(vbuf, 1, uio);
1684 } else
1685 memset(data, 0, tocopy);
1686 data = (char *)data + tocopy;
1687 }
1688 cnt++;
1689 }
1690 if (left) {
1691 printf("%s: POSSIBLE RUN ERROR\n", __func__);
1692 error = E2BIG;
1693 }
1694 } else {
1695 ddprintf(("%s: data is in mft record\n", __func__));
1696 if (uio)
1697 uiomove((char *)vap->va_datap + roff, rsize, uio);
1698 else
1699 memcpy(rdata, (char *)vap->va_datap + roff, rsize);
1700 *initp += rsize;
1701 }
1702
1703 return (error);
1704 }
1705
1706 /*
1707 * This is one of read routines.
1708 */
1709 int
1710 ntfs_readattr_plain(
1711 struct ntfsmount * ntmp,
1712 struct ntnode * ip,
1713 u_int32_t attrnum,
1714 const char *attrname,
1715 off_t roff,
1716 size_t rsize,
1717 void *rdata,
1718 size_t * initp,
1719 struct uio *uio)
1720 {
1721 size_t init;
1722 int error = 0;
1723 off_t off = roff, left = rsize, toread;
1724 void * data = rdata;
1725 struct ntvattr *vap;
1726 *initp = 0;
1727
1728 while (left) {
1729 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1730 ntfs_btocn(off), &vap);
1731 if (error)
1732 return (error);
1733 toread = MIN(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1734 ddprintf(("%s: o: %qd, s: %qd (%qu - %qu)\n", __func__,
1735 (long long) off, (long long) toread,
1736 (long long) vap->va_vcnstart,
1737 (long long) vap->va_vcnend));
1738 error = ntfs_readntvattr_plain(ntmp, ip, vap,
1739 off - ntfs_cntob(vap->va_vcnstart),
1740 toread, data, &init, uio);
1741 if (error) {
1742 printf("%s: ntfs_readntvattr_plain failed: o: %qd, "
1743 "s: %qd\n", __func__,
1744 (long long) off, (long long) toread);
1745 printf("%s: attrib: %qu - %qu\n", __func__,
1746 (long long) vap->va_vcnstart,
1747 (long long) vap->va_vcnend);
1748 ntfs_ntvattrrele(vap);
1749 break;
1750 }
1751 ntfs_ntvattrrele(vap);
1752 left -= toread;
1753 off += toread;
1754 data = (char *)data + toread;
1755 *initp += init;
1756 }
1757
1758 return (error);
1759 }
1760
1761 /*
1762 * This is one of read routines.
1763 */
1764 int
1765 ntfs_readattr(
1766 struct ntfsmount * ntmp,
1767 struct ntnode * ip,
1768 u_int32_t attrnum,
1769 const char *attrname,
1770 off_t roff,
1771 size_t rsize,
1772 void *rdata,
1773 struct uio *uio)
1774 {
1775 int error = 0;
1776 struct ntvattr *vap;
1777 size_t init;
1778
1779 ddprintf(("%s: reading %llu: 0x%x, from %qd size %qu bytes\n",
1780 __func__, (unsigned long long)ip->i_number, attrnum,
1781 (long long)roff, (long long)rsize));
1782
1783 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname, 0, &vap);
1784 if (error)
1785 return (error);
1786
1787 if ((roff > vap->va_datalen) ||
1788 (roff + rsize > vap->va_datalen)) {
1789 printf("%s: offset too big: %qd (%qd) > %qu\n", __func__,
1790 (long long) roff, (long long) (roff + rsize),
1791 (long long) vap->va_datalen);
1792 ntfs_ntvattrrele(vap);
1793 return (E2BIG);
1794 }
1795 if (vap->va_compression && vap->va_compressalg) {
1796 u_int8_t *cup;
1797 u_int8_t *uup;
1798 off_t off, roff, left, tocopy;
1799 void *data
1800 cn_t cn;
1801
1802 left = rsize;
1803 data = rdata;
1804 ddprintf(("%s: compression: %d\n", __func__,
1805 vap->va_compressalg));
1806
1807 cup = malloc(ntfs_cntob(NTFS_COMPUNIT_CL),
1808 M_NTFSDECOMP, M_WAITOK);
1809 uup = malloc(ntfs_cntob(NTFS_COMPUNIT_CL),
1810 M_NTFSDECOMP, M_WAITOK);
1811
1812 cn = (ntfs_btocn(roff)) & (~(NTFS_COMPUNIT_CL - 1));
1813 off = roff - ntfs_cntob(cn);
1814
1815 while (left) {
1816 error = ntfs_readattr_plain(ntmp, ip, attrnum,
1817 attrname, ntfs_cntob(cn),
1818 ntfs_cntob(NTFS_COMPUNIT_CL),
1819 cup, &init, NULL);
1820 if (error)
1821 break;
1822
1823 tocopy = MIN(left, ntfs_cntob(NTFS_COMPUNIT_CL) - off);
1824
1825 if (init == ntfs_cntob(NTFS_COMPUNIT_CL)) {
1826 if (uio)
1827 uiomove(cup + off, tocopy, uio);
1828 else
1829 memcpy(data, cup + off, tocopy);
1830 } else if (init == 0) {
1831 if (uio) {
1832 char vbuf[] = "";
1833 size_t remains = tocopy;
1834 for(; remains; remains--)
1835 uiomove(vbuf, 1, uio);
1836 }
1837 else
1838 memset(data, 0, tocopy);
1839 } else {
1840 error = ntfs_uncompunit(ntmp, uup, cup);
1841 if (error)
1842 break;
1843 if (uio)
1844 uiomove(uup + off, tocopy, uio);
1845 else
1846 memcpy(data, uup + off, tocopy);
1847 }
1848
1849 left -= tocopy;
1850 data = (char *)data + tocopy;
1851 off += tocopy - ntfs_cntob(NTFS_COMPUNIT_CL);
1852 cn += NTFS_COMPUNIT_CL;
1853 }
1854
1855 free(uup, M_NTFSDECOMP);
1856 free(cup, M_NTFSDECOMP);
1857 } else
1858 error = ntfs_readattr_plain(ntmp, ip, attrnum, attrname,
1859 roff, rsize, rdata, &init, uio);
1860 ntfs_ntvattrrele(vap);
1861 return (error);
1862 }
1863
1864 #if UNUSED_CODE
1865 int
1866 ntfs_parserun(
1867 cn_t * cn,
1868 cn_t * cl,
1869 u_int8_t * run,
1870 u_long len,
1871 u_long *off)
1872 {
1873 u_int8_t sz;
1874 int i;
1875
1876 if (NULL == run) {
1877 printf("%s: run == NULL\n", __func__);
1878 return (EINVAL);
1879 }
1880 sz = run[(*off)++];
1881 if (0 == sz) {
1882 printf("%s: trying to go out of run\n", __func__);
1883 return (E2BIG);
1884 }
1885 *cl = 0;
1886 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1887 printf("%s: bad run: length too big: sz: 0x%02x "
1888 "(%ld < %ld + sz)\n", __func__, sz, len, *off);
1889 return (EINVAL);
1890 }
1891 for (i = 0; i < (sz & 0xF); i++)
1892 *cl += (u_int32_t) run[(*off)++] << (i << 3);
1893
1894 sz >>= 4;
1895 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1896 printf("%s: bad run: length too big: sz: 0x%02x "
1897 "(%ld < %ld + sz)\n", __func__, sz, len, *off);
1898 return (EINVAL);
1899 }
1900 for (i = 0; i < (sz & 0xF); i++)
1901 *cn += (u_int32_t) run[(*off)++] << (i << 3);
1902
1903 return (0);
1904 }
1905 #endif
1906
1907 /*
1908 * Process fixup routine on given buffer.
1909 */
1910 int
1911 ntfs_procfixups(
1912 struct ntfsmount * ntmp,
1913 u_int32_t magic,
1914 void *xbufv,
1915 size_t len)
1916 {
1917 char *xbuf = xbufv;
1918 struct fixuphdr *fhp = (struct fixuphdr *) xbuf;
1919 int i;
1920 u_int16_t fixup;
1921 u_int16_t *fxp;
1922 u_int16_t *cfxp;
1923
1924 if (fhp->fh_magic == 0)
1925 return (EINVAL);
1926 if (fhp->fh_magic != magic) {
1927 printf("%s: magic doesn't match: %08x != %08x\n", __func__,
1928 fhp->fh_magic, magic);
1929 return (EINVAL);
1930 }
1931 if ((fhp->fh_fnum - 1) * ntmp->ntm_bps != len) {
1932 printf("%s: bad fixups number: %d for %ld bytes block\n",
1933 __func__, fhp->fh_fnum, (long)len); /* XXX printf kludge */
1934 return (EINVAL);
1935 }
1936 if (fhp->fh_foff >= ntmp->ntm_spc * ntmp->ntm_mftrecsz * ntmp->ntm_bps) {
1937 printf("%s: invalid offset: %x", __func__, fhp->fh_foff);
1938 return (EINVAL);
1939 }
1940 fxp = (u_int16_t *) (xbuf + fhp->fh_foff);
1941 cfxp = (u_int16_t *) (xbuf + ntmp->ntm_bps - 2);
1942 fixup = *fxp++;
1943 for (i = 1; i < fhp->fh_fnum; i++, fxp++) {
1944 if (*cfxp != fixup) {
1945 printf("%s: fixup %d doesn't match\n", __func__, i);
1946 return (EINVAL);
1947 }
1948 *cfxp = *fxp;
1949 cfxp = (u_int16_t *)((char *)cfxp + ntmp->ntm_bps);
1950 }
1951 return (0);
1952 }
1953
1954 #if UNUSED_CODE
1955 int
1956 ntfs_runtocn(
1957 cn_t * cn,
1958 struct ntfsmount * ntmp,
1959 u_int8_t * run,
1960 u_long len,
1961 cn_t vcn)
1962 {
1963 cn_t ccn = 0;
1964 cn_t ccl = 0;
1965 u_long off = 0;
1966 int error = 0;
1967
1968 #ifdef NTFS_DEBUG
1969 int i;
1970 printf("%s: run: %p, %ld bytes, vcn:%ld\n", __func__,
1971 run, len, (u_long) vcn);
1972 printf("%s: run: ", __func__);
1973 for (i = 0; i < len; i++)
1974 printf("0x%02x ", run[i]);
1975 printf("\n");
1976 #endif
1977
1978 if (NULL == run) {
1979 printf("%s: run == NULL\n", __func__);
1980 return (EINVAL);
1981 }
1982 do {
1983 if (run[off] == 0) {
1984 printf("%s: vcn too big\n", __func__);
1985 return (E2BIG);
1986 }
1987 vcn -= ccl;
1988 error = ntfs_parserun(&ccn, &ccl, run, len, &off);
1989 if (error) {
1990 printf("%s: ntfs_parserun failed\n", __func__);
1991 return (error);
1992 }
1993 } while (ccl <= vcn);
1994 *cn = ccn + vcn;
1995 return (0);
1996 }
1997 #endif
1998
1999 /*
2000 * this initializes toupper table & dependent variables to be ready for
2001 * later work
2002 */
2003 void
2004 ntfs_toupper_init(void)
2005 {
2006 ntfs_toupper_tab = NULL;
2007 mutex_init(&ntfs_toupper_lock, MUTEX_DEFAULT, IPL_NONE);
2008 ntfs_toupper_usecount = 0;
2009 }
2010
2011 /*
2012 * if the ntfs_toupper_tab[] is filled already, just raise use count;
2013 * otherwise read the data from the filesystem we are currently mounting
2014 */
2015 int
2016 ntfs_toupper_use(struct mount *mp, struct ntfsmount *ntmp)
2017 {
2018 int error = 0;
2019 struct vnode *vp;
2020
2021 /* get exclusive access */
2022 mutex_enter(&ntfs_toupper_lock);
2023
2024 /* only read the translation data from a file if it hasn't been
2025 * read already */
2026 if (ntfs_toupper_tab)
2027 goto out;
2028
2029 /*
2030 * Read in Unicode lowercase -> uppercase translation file.
2031 * XXX for now, just the first 256 entries are used anyway,
2032 * so don't bother reading more
2033 */
2034 ntfs_toupper_tab = malloc(256 * 256 * sizeof(*ntfs_toupper_tab),
2035 M_NTFSRDATA, M_WAITOK);
2036
2037 if ((error = VFS_VGET(mp, NTFS_UPCASEINO, &vp)))
2038 goto out;
2039 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
2040 0, 256 * 256 * sizeof(*ntfs_toupper_tab), (char *)ntfs_toupper_tab,
2041 NULL);
2042 vput(vp);
2043
2044 out:
2045 ntfs_toupper_usecount++;
2046 mutex_exit(&ntfs_toupper_lock);
2047 return (error);
2048 }
2049
2050 /*
2051 * lower the use count and if it reaches zero, free the memory
2052 * tied by toupper table
2053 */
2054 void
2055 ntfs_toupper_unuse(void)
2056 {
2057 /* get exclusive access */
2058 mutex_enter(&ntfs_toupper_lock);
2059
2060 ntfs_toupper_usecount--;
2061 if (ntfs_toupper_usecount == 0) {
2062 free(ntfs_toupper_tab, M_NTFSRDATA);
2063 ntfs_toupper_tab = NULL;
2064 }
2065 #ifdef DIAGNOSTIC
2066 else if (ntfs_toupper_usecount < 0) {
2067 panic("ntfs_toupper_unuse(): use count negative: %d",
2068 ntfs_toupper_usecount);
2069 }
2070 #endif
2071
2072 /* release the lock */
2073 mutex_exit(&ntfs_toupper_lock);
2074 }
2075