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