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