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