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