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