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