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