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