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