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