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