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