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