ntfs_subr.c revision 1.55 1 /* $NetBSD: ntfs_subr.c,v 1.55 2014/12/28 14:42:56 maxv Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 1999 Semen Ustimenko (semenu (at) 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.55 2014/12/28 14:42:56 maxv 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 int
99 ntfs_ntvattrrele(struct ntvattr *vap)
100 {
101 dprintf(("%s: ino: %llu, type: 0x%x\n", __func__,
102 (unsigned long long)vap->va_ip->i_number, vap->va_type));
103 ntfs_ntrele(vap->va_ip);
104 return (0);
105 }
106
107 /*
108 * find the attribute in the ntnode
109 */
110 static int
111 ntfs_findvattr(struct ntfsmount *ntmp, struct ntnode *ip, struct ntvattr **lvapp,
112 struct ntvattr **vapp, u_int32_t type, const char *name, size_t namelen,
113 cn_t vcn)
114 {
115 int error;
116 struct ntvattr *vap;
117
118 if ((ip->i_flag & IN_LOADED) == 0) {
119 dprintf(("%s: node not loaded, ino: %llu\n", __func__,
120 (unsigned long long)ip->i_number));
121 error = ntfs_loadntnode(ntmp,ip);
122 if (error) {
123 printf("%s: FAILED TO LOAD INO: %llu\n", __func__,
124 (unsigned long long)ip->i_number);
125 return (error);
126 }
127 }
128
129 *lvapp = NULL;
130 *vapp = NULL;
131 for (vap = ip->i_valist.lh_first; vap; vap = vap->va_list.le_next) {
132 ddprintf(("%s: type: 0x%x, vcn: %qu - %qu\n", __func__,
133 vap->va_type, (long long) vap->va_vcnstart,
134 (long long) vap->va_vcnend));
135 if ((vap->va_type == type) &&
136 (vap->va_vcnstart <= vcn) && (vap->va_vcnend >= vcn) &&
137 (vap->va_namelen == namelen) &&
138 (strncmp(name, vap->va_name, namelen) == 0)) {
139 *vapp = vap;
140 ntfs_ntref(vap->va_ip);
141 return (0);
142 }
143 if (vap->va_type == NTFS_A_ATTRLIST)
144 *lvapp = vap;
145 }
146
147 return (-1);
148 }
149
150 /*
151 * Search attribute specified in ntnode (load ntnode if necessary).
152 * If not found but ATTR_A_ATTRLIST present, read it in and search through.
153 * VOP_VGET node needed, and lookup through its ntnode (load if nessesary).
154 *
155 * ntnode should be locked
156 */
157 int
158 ntfs_ntvattrget(struct ntfsmount *ntmp, struct ntnode *ip, u_int32_t type,
159 const char *name, cn_t vcn, struct ntvattr **vapp)
160 {
161 struct ntvattr *lvap = NULL;
162 struct attr_attrlist *aalp;
163 struct attr_attrlist *nextaalp;
164 struct vnode *newvp;
165 struct ntnode *newip;
166 void *alpool;
167 size_t namelen, len;
168 int error;
169
170 *vapp = NULL;
171
172 if (name) {
173 dprintf(("%s: ino: %llu, type: 0x%x, name: %s, vcn: %qu\n",
174 __func__, (unsigned long long)ip->i_number, type, name,
175 (long long)vcn));
176 namelen = strlen(name);
177 } else {
178 dprintf(("%s: ino: %llu, type: 0x%x, vcn: %qu\n", __func__,
179 (unsigned long long)ip->i_number, type, (long long)vcn));
180 name = "";
181 namelen = 0;
182 }
183
184 error = ntfs_findvattr(ntmp, ip, &lvap, vapp, type, name, namelen, vcn);
185 if (error >= 0)
186 return (error);
187
188 if (!lvap) {
189 dprintf(("%s: UNEXISTED ATTRIBUTE: "
190 "ino: %llu, type: 0x%x, name: %s, vcn: %qu\n", __func__,
191 (unsigned long long)ip->i_number, type, name,
192 (long long)vcn));
193 return (ENOENT);
194 }
195 /* Scan $ATTRIBUTE_LIST for requested attribute */
196 len = lvap->va_datalen;
197 alpool = malloc(len, M_TEMP, M_WAITOK);
198 error = ntfs_readntvattr_plain(ntmp, ip, lvap, 0, len, alpool, &len,
199 NULL);
200 if (error)
201 goto out;
202
203 aalp = (struct attr_attrlist *) alpool;
204 nextaalp = NULL;
205
206 for (; len > 0; aalp = nextaalp) {
207 KASSERT(aalp != NULL);
208 dprintf(("%s: attrlist: ino: %d, attr: 0x%x, vcn: %qu\n",
209 __func__, aalp->al_inumber, aalp->al_type,
210 (long long) aalp->al_vcnstart));
211
212 if (len > aalp->reclen) {
213 nextaalp = NTFS_NEXTREC(aalp, struct attr_attrlist *);
214 } else {
215 nextaalp = NULL;
216 }
217 len -= aalp->reclen;
218
219 if (!NTFS_AALPCMP(aalp, type, name, namelen) ||
220 (nextaalp && (nextaalp->al_vcnstart <= vcn) &&
221 NTFS_AALPCMP(nextaalp, type, name, namelen)))
222 continue;
223
224 dprintf(("%s: attribute in ino: %d\n", __func__,
225 aalp->al_inumber));
226
227 error = ntfs_vgetex(ntmp->ntm_mountp, aalp->al_inumber,
228 NTFS_A_DATA, "", LK_EXCLUSIVE, &newvp);
229 if (error) {
230 printf("%s: CAN'T VGET INO: %d\n", __func__,
231 aalp->al_inumber);
232 goto out;
233 }
234 newip = VTONT(newvp);
235 /* XXX have to lock ntnode */
236 error = ntfs_findvattr(ntmp, newip, &lvap, vapp,
237 type, name, namelen, vcn);
238 vput(newvp);
239 if (error == 0)
240 goto out;
241 printf("%s: ATTRLIST ERROR.\n", __func__);
242 break;
243 }
244 error = ENOENT;
245
246 dprintf(("%s: NON-EXISTANT ATTRIBUTE: ino: %llu, type: 0x%x, "
247 "name: %.*s, vcn: %qu\n", __func__,
248 (unsigned long long)ip->i_number, type, (int)namelen,
249 name, (long long)vcn));
250 out:
251 free(alpool, M_TEMP);
252 return (error);
253 }
254
255 /*
256 * Read ntnode from disk, make ntvattr list.
257 *
258 * ntnode should be locked
259 */
260 int
261 ntfs_loadntnode(struct ntfsmount *ntmp, struct ntnode *ip)
262 {
263 struct filerec *mfrp;
264 int error, off;
265 struct attr *ap;
266 struct ntvattr *nvap;
267
268 dprintf(("%s: loading ino: %llu\n", __func__,
269 (unsigned long long)ip->i_number));
270
271 mfrp = malloc(ntfs_bntob(ntmp->ntm_bpmftrec), M_TEMP, M_WAITOK);
272
273 if (ip->i_number < NTFS_SYSNODESNUM) {
274 struct buf *bp;
275 daddr_t bn;
276 off_t boff;
277
278 dprintf(("%s: read system node\n", __func__));
279
280 /*
281 * Make sure we always read full cluster to
282 * prevent buffer cache inconsistency.
283 */
284 boff = ntfs_cntob(ntmp->ntm_mftcn) +
285 ntfs_bntob(ntmp->ntm_bpmftrec) * ip->i_number;
286 bn = ntfs_cntobn(ntfs_btocn(boff));
287 off = ntfs_btocnoff(boff);
288
289 error = bread(ntmp->ntm_devvp, bn, ntfs_cntob(1),
290 NOCRED, 0, &bp);
291 if (error) {
292 printf("%s: BREAD FAILED\n", __func__);
293 goto out;
294 }
295 memcpy(mfrp, (char *)bp->b_data + off,
296 ntfs_bntob(ntmp->ntm_bpmftrec));
297 bqrelse(bp);
298 } else {
299 struct vnode *vp;
300
301 vp = ntmp->ntm_sysvn[NTFS_MFTINO];
302 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
303 ip->i_number * ntfs_bntob(ntmp->ntm_bpmftrec),
304 ntfs_bntob(ntmp->ntm_bpmftrec), mfrp, NULL);
305 if (error) {
306 printf("%s: ntfs_readattr failed\n", __func__);
307 goto out;
308 }
309 }
310
311 /* Check if magic and fixups are correct */
312 error = ntfs_procfixups(ntmp, NTFS_FILEMAGIC, (void *)mfrp,
313 ntfs_bntob(ntmp->ntm_bpmftrec));
314 if (error) {
315 printf("%s: BAD MFT RECORD %d\n", __func__,
316 (u_int32_t) ip->i_number);
317 goto out;
318 }
319
320 dprintf(("%s: load attrs for ino: %llu\n", __func__,
321 (unsigned long long)ip->i_number));
322 off = mfrp->fr_attroff;
323 ap = (struct attr *) ((char *)mfrp + off);
324
325 LIST_INIT(&ip->i_valist);
326
327 while (ap->a_hdr.a_type != -1) {
328 error = ntfs_attrtontvattr(ntmp, &nvap, ap);
329 if (error)
330 break;
331 nvap->va_ip = ip;
332
333 LIST_INSERT_HEAD(&ip->i_valist, nvap, va_list);
334
335 off += ap->a_hdr.reclen;
336 ap = (struct attr *) ((char *)mfrp + off);
337 }
338 if (error) {
339 printf("%s: failed to load attr ino: %llu\n", __func__,
340 (unsigned long long)ip->i_number);
341 goto out;
342 }
343
344 ip->i_mainrec = mfrp->fr_mainrec;
345 ip->i_nlink = mfrp->fr_nlink;
346 ip->i_frflag = mfrp->fr_flags;
347
348 ip->i_flag |= IN_LOADED;
349
350 out:
351 free(mfrp, M_TEMP);
352 return (error);
353 }
354
355 /*
356 * Routine locks ntnode and increase usecount, just opposite of
357 * ntfs_ntput().
358 */
359 int
360 ntfs_ntget(struct ntnode *ip)
361 {
362 dprintf(("%s: get ntnode %llu: %p, usecount: %d\n", __func__,
363 (unsigned long long)ip->i_number, ip, ip->i_usecount));
364
365 mutex_enter(&ip->i_interlock);
366 ip->i_usecount++;
367 while (ip->i_busy != 0) {
368 cv_wait(&ip->i_lock, &ip->i_interlock);
369 }
370 ip->i_busy = 1;
371 mutex_exit(&ip->i_interlock);
372
373 return 0;
374 }
375
376 /*
377 * Routine search ntnode in hash, if found: lock, inc usecount and return.
378 * If not in hash allocate structure for ntnode, prefill it, lock,
379 * inc count and return.
380 *
381 * ntnode returned locked
382 */
383 int
384 ntfs_ntlookup(struct ntfsmount *ntmp, ino_t ino, struct ntnode **ipp)
385 {
386 struct ntnode *ip;
387
388 dprintf(("%s: looking for ntnode %llu\n", __func__,
389 (unsigned long long)ino));
390
391 if ((*ipp = ntfs_nthashlookup(ntmp->ntm_dev, ino)) != NULL) {
392 ntfs_ntget(*ipp);
393 dprintf(("%s: ntnode %llu: %p, usecount: %d\n", __func__,
394 (unsigned long long)ino, *ipp, (*ipp)->i_usecount));
395 return (0);
396 }
397
398 ip = malloc(sizeof(*ip), M_NTFSNTNODE, M_WAITOK|M_ZERO);
399 ddprintf(("%s: allocating ntnode: %llu: %p\n", __func__,
400 (unsigned long long)ino, ip));
401
402 mutex_enter(&ntfs_hashlock);
403 if ((*ipp = ntfs_nthashlookup(ntmp->ntm_dev, ino)) != NULL) {
404 mutex_exit(&ntfs_hashlock);
405 ntfs_ntget(*ipp);
406 free(ip, M_NTFSNTNODE);
407 dprintf(("%s: ntnode %llu: %p, usecount: %d\n", __func__,
408 (unsigned long long)ino, *ipp, (*ipp)->i_usecount));
409 return (0);
410 }
411
412 /* Generic initialization */
413 ip->i_devvp = ntmp->ntm_devvp;
414 ip->i_dev = ntmp->ntm_dev;
415 ip->i_number = ino;
416 ip->i_mp = ntmp;
417
418 /* init lock and lock the newborn ntnode */
419 cv_init(&ip->i_lock, "ntfslk");
420 mutex_init(&ip->i_interlock, MUTEX_DEFAULT, IPL_NONE);
421 ntfs_ntget(ip);
422
423 ntfs_nthashins(ip);
424
425 mutex_exit(&ntfs_hashlock);
426
427 *ipp = ip;
428
429 dprintf(("%s: ntnode %llu: %p, usecount: %d\n", __func__,
430 (unsigned long long)ino, ip, ip->i_usecount));
431
432 return (0);
433 }
434
435 /*
436 * Decrement usecount of ntnode and unlock it, if usecount reach zero,
437 * deallocate ntnode.
438 *
439 * ntnode should be locked on entry, and unlocked on return.
440 */
441 void
442 ntfs_ntput(struct ntnode *ip)
443 {
444 struct ntvattr *vap;
445
446 dprintf(("%s: rele ntnode %llu: %p, usecount: %d\n", __func__,
447 (unsigned long long)ip->i_number, ip, ip->i_usecount));
448
449 mutex_enter(&ip->i_interlock);
450 ip->i_usecount--;
451
452 #ifdef DIAGNOSTIC
453 if (ip->i_usecount < 0) {
454 panic("ntfs_ntput: ino: %llu usecount: %d ",
455 (unsigned long long)ip->i_number, ip->i_usecount);
456 }
457 #endif
458
459 ip->i_busy = 0;
460 cv_signal(&ip->i_lock);
461 mutex_exit(&ip->i_interlock);
462
463 if (ip->i_usecount == 0) {
464 dprintf(("%s: deallocating ntnode: %llu\n", __func__,
465 (unsigned long long)ip->i_number));
466
467 ntfs_nthashrem(ip);
468
469 while (ip->i_valist.lh_first != NULL) {
470 vap = ip->i_valist.lh_first;
471 LIST_REMOVE(vap,va_list);
472 ntfs_freentvattr(vap);
473 }
474 mutex_destroy(&ip->i_interlock);
475 cv_destroy(&ip->i_lock);
476 free(ip, M_NTFSNTNODE);
477 }
478 }
479
480 /*
481 * increment usecount of ntnode
482 */
483 void
484 ntfs_ntref(struct ntnode *ip)
485 {
486 mutex_enter(&ip->i_interlock);
487 ip->i_usecount++;
488 mutex_exit(&ip->i_interlock);
489
490 dprintf(("%s: ino %llu, usecount: %d\n", __func__,
491 (unsigned long long)ip->i_number, ip->i_usecount));
492 }
493
494 /*
495 * Decrement usecount of ntnode.
496 */
497 void
498 ntfs_ntrele(struct ntnode *ip)
499 {
500 dprintf(("%s: rele ntnode %llu: %p, usecount: %d\n", __func__,
501 (unsigned long long)ip->i_number, ip, ip->i_usecount));
502
503 mutex_enter(&ip->i_interlock);
504 ip->i_usecount--;
505
506 if (ip->i_usecount < 0)
507 panic("%s: ino: %llu usecount: %d ", __func__,
508 (unsigned long long)ip->i_number, ip->i_usecount);
509 mutex_exit(&ip->i_interlock);
510 }
511
512 /*
513 * Deallocate all memory allocated for ntvattr
514 */
515 void
516 ntfs_freentvattr(struct ntvattr *vap)
517 {
518 if (vap->va_flag & NTFS_AF_INRUN) {
519 if (vap->va_vruncn)
520 free(vap->va_vruncn, M_NTFSRUN);
521 if (vap->va_vruncl)
522 free(vap->va_vruncl, M_NTFSRUN);
523 } else {
524 if (vap->va_datap)
525 free(vap->va_datap, M_NTFSRDATA);
526 }
527 free(vap, M_NTFSNTVATTR);
528 }
529
530 /*
531 * Convert disk image of attribute into ntvattr structure,
532 * runs are expanded also.
533 */
534 int
535 ntfs_attrtontvattr(struct ntfsmount *ntmp, struct ntvattr **rvapp,
536 struct attr *rap)
537 {
538 int error, i;
539 struct ntvattr *vap;
540
541 error = 0;
542 *rvapp = NULL;
543
544 vap = malloc(sizeof(*vap), M_NTFSNTVATTR, M_WAITOK|M_ZERO);
545 vap->va_ip = NULL;
546 vap->va_flag = rap->a_hdr.a_flag;
547 vap->va_type = rap->a_hdr.a_type;
548 vap->va_compression = rap->a_hdr.a_compression;
549 vap->va_index = rap->a_hdr.a_index;
550
551 ddprintf(("%s: type: 0x%x, index: %d", __func__,
552 vap->va_type, vap->va_index));
553
554 vap->va_namelen = rap->a_hdr.a_namelen;
555 if (rap->a_hdr.a_namelen) {
556 wchar *unp = (wchar *)((char *)rap + rap->a_hdr.a_nameoff);
557 ddprintf((", name:["));
558 for (i = 0; i < vap->va_namelen; i++) {
559 vap->va_name[i] = unp[i];
560 ddprintf(("%c", vap->va_name[i]));
561 }
562 ddprintf(("]"));
563 }
564 if (vap->va_flag & NTFS_AF_INRUN) {
565 ddprintf((", nonres."));
566 vap->va_datalen = rap->a_nr.a_datalen;
567 vap->va_allocated = rap->a_nr.a_allocated;
568 vap->va_vcnstart = rap->a_nr.a_vcnstart;
569 vap->va_vcnend = rap->a_nr.a_vcnend;
570 vap->va_compressalg = rap->a_nr.a_compressalg;
571 error = ntfs_runtovrun(&(vap->va_vruncn), &(vap->va_vruncl),
572 &(vap->va_vruncnt),
573 (u_int8_t *) rap + rap->a_nr.a_dataoff);
574 } else {
575 vap->va_compressalg = 0;
576 ddprintf((", res."));
577 vap->va_datalen = rap->a_r.a_datalen;
578 vap->va_allocated = rap->a_r.a_datalen;
579 vap->va_vcnstart = 0;
580 vap->va_vcnend = ntfs_btocn(vap->va_allocated);
581 vap->va_datap = malloc(vap->va_datalen, M_NTFSRDATA, M_WAITOK);
582 memcpy(vap->va_datap, (char *)rap + rap->a_r.a_dataoff,
583 rap->a_r.a_datalen);
584 }
585 ddprintf((", len: %qu", (long long)vap->va_datalen));
586
587 if (error)
588 free(vap, M_NTFSNTVATTR);
589 else
590 *rvapp = vap;
591
592 ddprintf(("\n"));
593
594 return (error);
595 }
596
597 /*
598 * Expand run into more utilizable and more memory eating format.
599 */
600 int
601 ntfs_runtovrun(cn_t **rcnp, cn_t **rclp, u_long *rcntp, u_int8_t *run)
602 {
603 u_int32_t off, sz, i;
604 cn_t *cn, *cl;
605 u_long cnt;
606 cn_t prev, tmp;
607
608 off = 0;
609 cnt = 0;
610 i = 0;
611 while (run[off]) {
612 off += (run[off] & 0xF) + ((run[off] >> 4) & 0xF) + 1;
613 cnt++;
614 }
615 cn = malloc(cnt * sizeof(*cn), M_NTFSRUN, M_WAITOK);
616 cl = malloc(cnt * sizeof(*cl), M_NTFSRUN, M_WAITOK);
617
618 off = 0;
619 cnt = 0;
620 prev = 0;
621 while (run[off]) {
622 sz = run[off++];
623 cl[cnt] = 0;
624
625 for (i = 0; i < (sz & 0xF); i++)
626 cl[cnt] += (u_int32_t) run[off++] << (i << 3);
627
628 sz >>= 4;
629 if (run[off + sz - 1] & 0x80) {
630 tmp = ((u_int64_t) - 1) << (sz << 3);
631 for (i = 0; i < sz; i++)
632 tmp |= (u_int64_t) run[off++] << (i << 3);
633 } else {
634 tmp = 0;
635 for (i = 0; i < sz; i++)
636 tmp |= (u_int64_t) run[off++] << (i << 3);
637 }
638 if (tmp)
639 prev = cn[cnt] = prev + tmp;
640 else
641 cn[cnt] = tmp;
642
643 cnt++;
644 }
645 *rcnp = cn;
646 *rclp = cl;
647 *rcntp = cnt;
648 return (0);
649 }
650
651 /*
652 * Compare unicode and ascii string case insens.
653 */
654 static int
655 ntfs_uastricmp(struct ntfsmount *ntmp, const wchar *ustr, size_t ustrlen,
656 const char *astr, size_t astrlen)
657 {
658 size_t i;
659 int res;
660
661 for (i = 0; i < ustrlen && astrlen > 0; i++) {
662 res = (*ntmp->ntm_wcmp)(NTFS_TOUPPER(ustr[i]),
663 NTFS_TOUPPER((*ntmp->ntm_wget)(&astr, &astrlen)) );
664 if (res)
665 return res;
666 }
667
668 if (i == ustrlen && astrlen == 0)
669 return 0;
670 else if (i == ustrlen)
671 return -1;
672 else
673 return 1;
674 }
675
676 /*
677 * Compare unicode and ascii string case sens.
678 */
679 static int
680 ntfs_uastrcmp(struct ntfsmount *ntmp, const wchar *ustr, size_t ustrlen,
681 const char *astr, size_t astrlen)
682 {
683 size_t i;
684 int res;
685
686 for (i = 0; (i < ustrlen) && astrlen > 0; i++) {
687 res = (*ntmp->ntm_wcmp)(ustr[i],
688 (*ntmp->ntm_wget)(&astr, &astrlen));
689 if (res)
690 return res;
691 }
692
693 if (i == ustrlen && astrlen == 0)
694 return 0;
695 else if (i == ustrlen)
696 return -1;
697 else
698 return 1;
699 }
700
701 /*
702 * Lookup attribute name in format: [[:$ATTR_TYPE]:$ATTR_NAME],
703 * $ATTR_TYPE is searched in attrdefs read from $AttrDefs.
704 * If $ATTR_TYPE not specified, ATTR_A_DATA assumed.
705 */
706 static int
707 ntfs_ntlookupattr(struct ntfsmount *ntmp, const char *name, int namelen,
708 int *attrtype, char **attrname)
709 {
710 const char *sys;
711 size_t syslen, i;
712 struct ntvattrdef *adp;
713
714 if (namelen == 0)
715 return (0);
716
717 if (name[0] == '$') {
718 sys = name;
719 for (syslen = 0; syslen < namelen; syslen++) {
720 if (sys[syslen] == ':') {
721 name++;
722 namelen--;
723 break;
724 }
725 }
726 name += syslen;
727 namelen -= syslen;
728
729 adp = ntmp->ntm_ad;
730 for (i = 0; i < ntmp->ntm_adnum; i++, adp++){
731 if (syslen != adp->ad_namelen ||
732 strncmp(sys, adp->ad_name, syslen) != 0)
733 continue;
734
735 *attrtype = adp->ad_type;
736 goto out;
737 }
738 return (ENOENT);
739 } else
740 *attrtype = NTFS_A_DATA;
741
742 out:
743 if (namelen) {
744 *attrname = malloc(namelen+1, M_TEMP, M_WAITOK);
745 memcpy((*attrname), name, namelen);
746 (*attrname)[namelen] = '\0';
747 }
748
749 return (0);
750 }
751
752 /*
753 * Lookup specified node for filename, matching cnp,
754 * return referenced vnode with fnode filled.
755 */
756 int
757 ntfs_ntlookupfile(struct ntfsmount *ntmp, struct vnode *vp,
758 struct componentname *cnp, struct vnode **vpp)
759 {
760 struct fnode *fp = VTOF(vp);
761 struct ntnode *ip = FTONT(fp);
762 struct ntvattr *vap; /* Root attribute */
763 cn_t cn = 0; /* VCN in current attribute */
764 void * rdbuf; /* Buffer to read directory's blocks */
765 u_int32_t blsize;
766 u_int32_t rdsize; /* Length of data to read from current block */
767 struct attr_indexentry *iep;
768 int error, res, anamelen, fnamelen;
769 const char *fname,*aname;
770 u_int32_t aoff;
771 int attrtype = NTFS_A_DATA;
772 char *attrname = NULL;
773 struct vnode *nvp;
774 int fullscan = 0;
775 struct ntfs_lookup_ctx *lookup_ctx = NULL, *tctx;
776
777 error = ntfs_ntget(ip);
778 if (error)
779 return (error);
780
781 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
782 if (error || (vap->va_flag & NTFS_AF_INRUN))
783 return (ENOTDIR);
784
785 /*
786 * Divide file name into: foofilefoofilefoofile[:attrspec]
787 * Store like this: fname:fnamelen [aname:anamelen]
788 */
789 fname = cnp->cn_nameptr;
790 aname = NULL;
791 anamelen = 0;
792 for (fnamelen = 0; fnamelen < cnp->cn_namelen; fnamelen++)
793 if (fname[fnamelen] == ':') {
794 aname = fname + fnamelen + 1;
795 anamelen = cnp->cn_namelen - fnamelen - 1;
796 dprintf(("%s: %s (%d), attr: %s (%d)\n", __func__,
797 fname, fnamelen, aname, anamelen));
798 break;
799 }
800
801 blsize = vap->va_a_iroot->ir_size;
802 dprintf(("%s: blksz: %d\n", __func__, blsize));
803 rdbuf = malloc(blsize, M_TEMP, M_WAITOK);
804
805 loop:
806 rdsize = vap->va_datalen;
807 dprintf(("%s: rdsz: %d\n", __func__, rdsize));
808
809 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXROOT, "$I30",
810 0, rdsize, rdbuf, NULL);
811 if (error)
812 goto fail;
813
814 aoff = sizeof(struct attr_indexroot);
815
816 do {
817 iep = (struct attr_indexentry *) ((char *)rdbuf + aoff);
818
819 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
820 aoff += iep->reclen,
821 iep = (struct attr_indexentry *) ((char *)rdbuf + aoff))
822 {
823 ddprintf(("%s: fscan: %d, %d\n", __func__,
824 (u_int32_t) iep->ie_number,
825 (u_int32_t) iep->ie_fnametype));
826
827 /* check the name - the case-insensitive check
828 * has to come first, to break from this for loop
829 * if needed, so we can dive correctly */
830 res = ntfs_uastricmp(ntmp, iep->ie_fname,
831 iep->ie_fnamelen, fname, fnamelen);
832 if (!fullscan) {
833 if (res > 0)
834 break;
835 if (res < 0)
836 continue;
837 }
838
839 if (iep->ie_fnametype == 0 ||
840 !(ntmp->ntm_flag & NTFS_MFLAG_CASEINS))
841 {
842 res = ntfs_uastrcmp(ntmp, iep->ie_fname,
843 iep->ie_fnamelen, fname, fnamelen);
844 if (res != 0 && !fullscan)
845 continue;
846 }
847
848 /* if we perform full scan, the file does not match
849 * and this is subnode, dive */
850 if (fullscan && res != 0) {
851 if (iep->ie_flag & NTFS_IEFLAG_SUBNODE) {
852 tctx = malloc(sizeof(*tctx), M_TEMP,
853 M_WAITOK);
854 tctx->aoff = aoff + iep->reclen;
855 tctx->rdsize = rdsize;
856 tctx->cn = cn;
857 tctx->prev = lookup_ctx;
858 lookup_ctx = tctx;
859 break;
860 } else
861 continue;
862 }
863
864 if (aname) {
865 error = ntfs_ntlookupattr(ntmp, aname, anamelen,
866 &attrtype, &attrname);
867 if (error)
868 goto fail;
869 }
870
871 /* Check if we've found ourselves */
872 if ((iep->ie_number == ip->i_number) &&
873 (attrtype == fp->f_attrtype) &&
874 !strcmp(attrname ? attrname : "", fp->f_attrname))
875 {
876 vref(vp);
877 *vpp = vp;
878 error = 0;
879 goto fail;
880 }
881
882 /* vget node */
883 error = ntfs_vgetex(ntmp->ntm_mountp, iep->ie_number,
884 attrtype, attrname ? attrname : "", 0, &nvp);
885
886 /* free the buffer returned by ntfs_ntlookupattr() */
887 if (attrname) {
888 free(attrname, M_TEMP);
889 attrname = NULL;
890 }
891
892 if (error)
893 goto fail;
894
895 *vpp = nvp;
896 goto fail;
897 }
898
899 /* Dive if possible */
900 if (iep->ie_flag & NTFS_IEFLAG_SUBNODE) {
901 dprintf(("%s: diving\n", __func__));
902
903 cn = *(cn_t *) ((char *)rdbuf + aoff +
904 iep->reclen - sizeof(cn_t));
905 rdsize = blsize;
906
907 error = ntfs_readattr(ntmp, ip, NTFS_A_INDX, "$I30",
908 ntfs_cntob(cn), rdsize, rdbuf, NULL);
909 if (error)
910 goto fail;
911
912 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
913 rdbuf, rdsize);
914 if (error)
915 goto fail;
916
917 aoff = (((struct attr_indexalloc *) rdbuf)->ia_hdrsize +
918 0x18);
919 } else if (fullscan && lookup_ctx) {
920 cn = lookup_ctx->cn;
921 aoff = lookup_ctx->aoff;
922 rdsize = lookup_ctx->rdsize;
923
924 error = ntfs_readattr(ntmp, ip,
925 (cn == 0) ? NTFS_A_INDXROOT : NTFS_A_INDX,
926 "$I30", ntfs_cntob(cn), rdsize, rdbuf, NULL);
927 if (error)
928 goto fail;
929
930 if (cn != 0) {
931 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
932 rdbuf, rdsize);
933 if (error)
934 goto fail;
935 }
936
937 tctx = lookup_ctx;
938 lookup_ctx = lookup_ctx->prev;
939 free(tctx, M_TEMP);
940 } else {
941 dprintf(("%s: nowhere to dive :-(\n", __func__));
942 error = ENOENT;
943 break;
944 }
945 } while (1);
946
947 /* perform full scan if no entry was found */
948 if (!fullscan && error == ENOENT) {
949 fullscan = 1;
950 cn = 0; /* need zero, used by lookup_ctx */
951
952 ddprintf(("%s: fullscan performed for: %.*s\n", __func__,
953 (int) fnamelen, fname));
954 goto loop;
955 }
956
957 dprintf(("finish\n"));
958
959 fail:
960 if (attrname)
961 free(attrname, M_TEMP);
962 if (lookup_ctx) {
963 while(lookup_ctx) {
964 tctx = lookup_ctx;
965 lookup_ctx = lookup_ctx->prev;
966 free(tctx, M_TEMP);
967 }
968 }
969 ntfs_ntvattrrele(vap);
970 ntfs_ntput(ip);
971 free(rdbuf, M_TEMP);
972 return (error);
973 }
974
975 /*
976 * Check if name type is permitted to show.
977 */
978 int
979 ntfs_isnamepermitted(struct ntfsmount *ntmp, struct attr_indexentry *iep)
980 {
981 if (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)
982 return 1;
983
984 switch (iep->ie_fnametype) {
985 case 2:
986 ddprintf(("%s: skipped DOS name\n", __func__));
987 return 0;
988 case 0: case 1: case 3:
989 return 1;
990 default:
991 printf("%s: WARNING! Unknown file name type: %d\n", __func__,
992 iep->ie_fnametype);
993 break;
994 }
995 return 0;
996 }
997
998 /*
999 * Read ntfs dir like stream of attr_indexentry, not like btree of them.
1000 * This is done by scanning $BITMAP:$I30 for busy clusters and reading them.
1001 * Of course $INDEX_ROOT:$I30 is read before. Last read values are stored in
1002 * fnode, so we can skip toward record number num almost immediately.
1003 * Anyway this is rather slow routine. The problem is that we don't know
1004 * how many records are there in $INDEX_ALLOCATION:$I30 block.
1005 */
1006 int
1007 ntfs_ntreaddir(struct ntfsmount *ntmp, struct fnode *fp, u_int32_t num,
1008 struct attr_indexentry **riepp)
1009 {
1010 struct ntnode *ip = FTONT(fp);
1011 struct ntvattr *vap = NULL; /* IndexRoot attribute */
1012 struct ntvattr *bmvap = NULL; /* BitMap attribute */
1013 struct ntvattr *iavap = NULL; /* IndexAllocation attribute */
1014 void * rdbuf; /* Buffer to read directory's blocks */
1015 u_char *bmp = NULL; /* Bitmap */
1016 u_int32_t blsize; /* Index allocation size (2048) */
1017 u_int32_t rdsize; /* Length of data to read */
1018 u_int32_t attrnum; /* Current attribute type */
1019 u_int32_t cpbl = 1; /* Clusters per directory block */
1020 u_int32_t blnum;
1021 struct attr_indexentry *iep;
1022 int error = ENOENT;
1023 u_int32_t aoff, cnum;
1024
1025 dprintf(("%s: read ino: %llu, num: %d\n", __func__,
1026 (unsigned long long)ip->i_number, num));
1027 error = ntfs_ntget(ip);
1028 if (error)
1029 return (error);
1030
1031 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
1032 if (error)
1033 return (ENOTDIR);
1034
1035 if (fp->f_dirblbuf == NULL) {
1036 fp->f_dirblsz = vap->va_a_iroot->ir_size;
1037 fp->f_dirblbuf = malloc(MAX(vap->va_datalen, fp->f_dirblsz),
1038 M_NTFSDIR, M_WAITOK);
1039 }
1040
1041 blsize = fp->f_dirblsz;
1042 rdbuf = fp->f_dirblbuf;
1043
1044 dprintf(("%s: rdbuf: %p, blsize: %d\n", __func__, rdbuf, blsize));
1045
1046 if (vap->va_a_iroot->ir_flag & NTFS_IRFLAG_INDXALLOC) {
1047 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXBITMAP, "$I30",
1048 0, &bmvap);
1049 if (error) {
1050 error = ENOTDIR;
1051 goto fail;
1052 }
1053 bmp = (u_char *) malloc(bmvap->va_datalen, M_TEMP, M_WAITOK);
1054 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXBITMAP, "$I30", 0,
1055 bmvap->va_datalen, bmp, NULL);
1056 if (error)
1057 goto fail;
1058
1059 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDX, "$I30",
1060 0, &iavap);
1061 if (error) {
1062 error = ENOTDIR;
1063 goto fail;
1064 }
1065 cpbl = ntfs_btocn(blsize + ntfs_cntob(1) - 1);
1066 dprintf(("%s: indexalloc: %qu, cpbl: %d\n", __func__,
1067 (long long)iavap->va_datalen, cpbl));
1068 } else {
1069 dprintf(("%s: w/o BitMap and IndexAllocation\n", __func__));
1070 iavap = bmvap = NULL;
1071 bmp = NULL;
1072 }
1073
1074 /* Try use previous values */
1075 if ((fp->f_lastdnum < num) && (fp->f_lastdnum != 0)) {
1076 attrnum = fp->f_lastdattr;
1077 aoff = fp->f_lastdoff;
1078 blnum = fp->f_lastdblnum;
1079 cnum = fp->f_lastdnum;
1080 } else {
1081 attrnum = NTFS_A_INDXROOT;
1082 aoff = sizeof(struct attr_indexroot);
1083 blnum = 0;
1084 cnum = 0;
1085 }
1086
1087 do {
1088 dprintf(("%s: scan: 0x%x, %d, %d, %d, %d\n", __func__,
1089 attrnum, (u_int32_t) blnum, cnum, num, aoff));
1090 rdsize = (attrnum == NTFS_A_INDXROOT) ? vap->va_datalen : blsize;
1091 error = ntfs_readattr(ntmp, ip, attrnum, "$I30",
1092 ntfs_cntob(blnum * cpbl), rdsize, rdbuf, NULL);
1093 if (error)
1094 goto fail;
1095
1096 if (attrnum == NTFS_A_INDX) {
1097 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1098 rdbuf, rdsize);
1099 if (error)
1100 goto fail;
1101 }
1102 if (aoff == 0)
1103 aoff = (attrnum == NTFS_A_INDX) ?
1104 (0x18 + ((struct attr_indexalloc *) rdbuf)->ia_hdrsize) :
1105 sizeof(struct attr_indexroot);
1106
1107 iep = (struct attr_indexentry *) ((char *)rdbuf + aoff);
1108 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
1109 aoff += iep->reclen,
1110 iep = (struct attr_indexentry *) ((char *)rdbuf + aoff))
1111 {
1112 if (!ntfs_isnamepermitted(ntmp, iep))
1113 continue;
1114 if (cnum >= num) {
1115 fp->f_lastdnum = cnum;
1116 fp->f_lastdoff = aoff;
1117 fp->f_lastdblnum = blnum;
1118 fp->f_lastdattr = attrnum;
1119
1120 *riepp = iep;
1121
1122 error = 0;
1123 goto fail;
1124 }
1125 cnum++;
1126 }
1127
1128 if (iavap) {
1129 if (attrnum == NTFS_A_INDXROOT)
1130 blnum = 0;
1131 else
1132 blnum++;
1133
1134 while (ntfs_cntob(blnum * cpbl) < iavap->va_datalen) {
1135 if (bmp[blnum >> 3] & (1 << (blnum & 3)))
1136 break;
1137 blnum++;
1138 }
1139
1140 attrnum = NTFS_A_INDX;
1141 aoff = 0;
1142 if (ntfs_cntob(blnum * cpbl) >= iavap->va_datalen)
1143 break;
1144 dprintf(("%s: blnum: %d\n", __func__,
1145 (u_int32_t) blnum));
1146 }
1147 } while (iavap);
1148
1149 *riepp = NULL;
1150 fp->f_lastdnum = 0;
1151
1152 fail:
1153 if (vap)
1154 ntfs_ntvattrrele(vap);
1155 if (bmvap)
1156 ntfs_ntvattrrele(bmvap);
1157 if (iavap)
1158 ntfs_ntvattrrele(iavap);
1159 if (bmp)
1160 free(bmp, M_TEMP);
1161 ntfs_ntput(ip);
1162 return (error);
1163 }
1164
1165 /*
1166 * Convert NTFS times that are in 100 ns units and begins from
1167 * 1601 Jan 1 into unix times.
1168 */
1169 struct timespec
1170 ntfs_nttimetounix(u_int64_t nt)
1171 {
1172 struct timespec t;
1173
1174 /* WindowNT times are in 100 ns and from 1601 Jan 1 */
1175 t.tv_nsec = (nt % (1000 * 1000 * 10)) * 100;
1176 t.tv_sec = nt / (1000 * 1000 * 10) -
1177 369LL * 365LL * 24LL * 60LL * 60LL -
1178 89LL * 1LL * 24LL * 60LL * 60LL;
1179 return (t);
1180 }
1181
1182 /*
1183 * This is one of write routine.
1184 */
1185 int
1186 ntfs_writeattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1187 u_int32_t attrnum, char *attrname, off_t roff, size_t rsize, void *rdata,
1188 size_t *initp, struct uio *uio)
1189 {
1190 size_t init;
1191 int error = 0;
1192 off_t off = roff, left = rsize, towrite;
1193 void *data = rdata;
1194 struct ntvattr *vap;
1195 *initp = 0;
1196
1197 while (left) {
1198 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1199 ntfs_btocn(off), &vap);
1200 if (error)
1201 return (error);
1202 towrite = MIN(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1203 ddprintf(("%s: o: %qd, s: %qd (%qu - %qu)\n", __func__,
1204 (long long) off, (long long) towrite,
1205 (long long) vap->va_vcnstart,
1206 (long long) vap->va_vcnend));
1207 error = ntfs_writentvattr_plain(ntmp, ip, vap,
1208 off - ntfs_cntob(vap->va_vcnstart),
1209 towrite, data, &init, uio);
1210 if (error) {
1211 dprintf(("%s: "
1212 "ntfs_writentvattr_plain failed: o: %qd, s: %qd\n",
1213 __func__, (long long) off, (long long) towrite));
1214 dprintf(("%s: attrib: %qu - %qu\n", __func__,
1215 (long long) vap->va_vcnstart,
1216 (long long) vap->va_vcnend));
1217 ntfs_ntvattrrele(vap);
1218 break;
1219 }
1220 ntfs_ntvattrrele(vap);
1221 left -= towrite;
1222 off += towrite;
1223 data = (char *)data + towrite;
1224 *initp += init;
1225 }
1226
1227 return (error);
1228 }
1229
1230 /*
1231 * This is one of write routine.
1232 *
1233 * ntnode should be locked.
1234 */
1235 int
1236 ntfs_writentvattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1237 struct ntvattr *vap, off_t roff, size_t rsize, void *rdata, size_t *initp,
1238 struct uio *uio)
1239 {
1240 int error = 0;
1241 off_t off;
1242 int cnt;
1243 cn_t ccn, ccl, cn, left, cl;
1244 void *data = rdata;
1245 daddr_t lbn;
1246 struct buf *bp;
1247 size_t tocopy;
1248
1249 *initp = 0;
1250
1251 if ((vap->va_flag & NTFS_AF_INRUN) == 0) {
1252 dprintf(("%s: CAN'T WRITE RES. ATTRIBUTE\n", __func__));
1253 return ENOTTY;
1254 }
1255
1256 ddprintf(("%s: data in run: %lu chains\n", __func__,
1257 vap->va_vruncnt));
1258
1259 off = roff;
1260 left = rsize;
1261 ccl = 0;
1262 ccn = 0;
1263 cnt = 0;
1264 for (; left && (cnt < vap->va_vruncnt); cnt++) {
1265 ccn = vap->va_vruncn[cnt];
1266 ccl = vap->va_vruncl[cnt];
1267
1268 ddprintf(("%s: left %qu, cn: 0x%qx, cl: %qu, off: %qd\n",
1269 __func__, (long long) left, (long long) ccn,
1270 (long long) ccl, (long long) off));
1271
1272 if (ntfs_cntob(ccl) < off) {
1273 off -= ntfs_cntob(ccl);
1274 cnt++;
1275 continue;
1276 }
1277 if (!ccn && ip->i_number != NTFS_BOOTINO)
1278 continue; /* XXX */
1279
1280 ccl -= ntfs_btocn(off);
1281 cn = ccn + ntfs_btocn(off);
1282 off = ntfs_btocnoff(off);
1283
1284 while (left && ccl) {
1285 /*
1286 * Always read and write single clusters at a time -
1287 * we need to avoid requesting differently-sized
1288 * blocks at the same disk offsets to avoid
1289 * confusing the buffer cache.
1290 */
1291 tocopy = MIN(left, ntfs_cntob(1) - off);
1292 cl = ntfs_btocl(tocopy + off);
1293 KASSERT(cl == 1 && tocopy <= ntfs_cntob(1));
1294 ddprintf(("%s: write: cn: 0x%qx cl: %qu, off: %qd "
1295 "len: %qu, left: %qu\n", __func__,
1296 (long long) cn, (long long) cl,
1297 (long long) off, (long long) tocopy,
1298 (long long) left));
1299 if ((off == 0) && (tocopy == ntfs_cntob(cl))) {
1300 lbn = ntfs_cntobn(cn);
1301 bp = getblk(ntmp->ntm_devvp, lbn,
1302 ntfs_cntob(cl), 0, 0);
1303 clrbuf(bp);
1304 } else {
1305 error = bread(ntmp->ntm_devvp, ntfs_cntobn(cn),
1306 ntfs_cntob(cl), NOCRED, B_MODIFY, &bp);
1307 if (error)
1308 return (error);
1309 }
1310 if (uio)
1311 uiomove((char *)bp->b_data + off, tocopy, uio);
1312 else
1313 memcpy((char *)bp->b_data + off, data, tocopy);
1314 bawrite(bp);
1315 data = (char *)data + tocopy;
1316 *initp += tocopy;
1317 off = 0;
1318 left -= tocopy;
1319 cn += cl;
1320 ccl -= cl;
1321 }
1322 }
1323
1324 if (left) {
1325 printf("%s: POSSIBLE RUN ERROR\n", __func__);
1326 error = EINVAL;
1327 }
1328
1329 return (error);
1330 }
1331
1332 /*
1333 * This is one of read routines.
1334 *
1335 * ntnode should be locked.
1336 */
1337 int
1338 ntfs_readntvattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1339 struct ntvattr *vap, off_t roff, size_t rsize, void *rdata, size_t *initp,
1340 struct uio *uio)
1341 {
1342 int error = 0;
1343 off_t off;
1344
1345 *initp = 0;
1346 if (vap->va_flag & NTFS_AF_INRUN) {
1347 int cnt;
1348 cn_t ccn, ccl, cn, left, cl;
1349 void *data = rdata;
1350 struct buf *bp;
1351 size_t tocopy;
1352
1353 ddprintf(("%s: data in run: %lu chains\n", __func__,
1354 vap->va_vruncnt));
1355
1356 off = roff;
1357 left = rsize;
1358 ccl = 0;
1359 ccn = 0;
1360 cnt = 0;
1361 while (left && (cnt < vap->va_vruncnt)) {
1362 ccn = vap->va_vruncn[cnt];
1363 ccl = vap->va_vruncl[cnt];
1364
1365 ddprintf(("%s: left %qu, cn: 0x%qx, cl: %qu, "
1366 "off: %qd\n", __func__,
1367 (long long) left, (long long) ccn,
1368 (long long) ccl, (long long) off));
1369
1370 if (ntfs_cntob(ccl) < off) {
1371 off -= ntfs_cntob(ccl);
1372 cnt++;
1373 continue;
1374 }
1375 if (ccn || ip->i_number == NTFS_BOOTINO) {
1376 ccl -= ntfs_btocn(off);
1377 cn = ccn + ntfs_btocn(off);
1378 off = ntfs_btocnoff(off);
1379
1380 while (left && ccl) {
1381 /*
1382 * Always read single clusters at a
1383 * time - we need to avoid reading
1384 * differently-sized blocks at the
1385 * same disk offsets to avoid
1386 * confusing the buffer cache.
1387 */
1388 tocopy = MIN(left,
1389 ntfs_cntob(1) - off);
1390 cl = ntfs_btocl(tocopy + off);
1391 KASSERT(cl == 1 &&
1392 tocopy <= ntfs_cntob(1));
1393
1394 ddprintf(("%s: read: cn: 0x%qx cl: %qu,"
1395 " off: %qd len: %qu, left: %qu\n",
1396 __func__, (long long) cn,
1397 (long long) cl,
1398 (long long) off,
1399 (long long) tocopy,
1400 (long long) left));
1401 error = bread(ntmp->ntm_devvp,
1402 ntfs_cntobn(cn),
1403 ntfs_cntob(cl),
1404 NOCRED, 0, &bp);
1405 if (error) {
1406 return (error);
1407 }
1408 if (uio) {
1409 uiomove((char *)bp->b_data + off,
1410 tocopy, uio);
1411 } else {
1412 memcpy(data, (char *)bp->b_data + off,
1413 tocopy);
1414 }
1415 brelse(bp, 0);
1416 data = (char *)data + tocopy;
1417 *initp += tocopy;
1418 off = 0;
1419 left -= tocopy;
1420 cn += cl;
1421 ccl -= cl;
1422 }
1423 } else {
1424 tocopy = MIN(left, ntfs_cntob(ccl) - off);
1425 ddprintf(("%s: hole: ccn: 0x%qx ccl: %qu, "
1426 "off: %qd, len: %qu, left: %qu\n", __func__,
1427 (long long) ccn, (long long) ccl,
1428 (long long) off, (long long) tocopy,
1429 (long long) left));
1430 left -= tocopy;
1431 off = 0;
1432 if (uio) {
1433 char vbuf[] = "";
1434 size_t remains = tocopy;
1435 for (; remains; remains--)
1436 uiomove(vbuf, 1, uio);
1437 } else
1438 memset(data, 0, tocopy);
1439 data = (char *)data + tocopy;
1440 }
1441 cnt++;
1442 }
1443 if (left) {
1444 printf("%s: POSSIBLE RUN ERROR\n", __func__);
1445 error = E2BIG;
1446 }
1447 } else {
1448 ddprintf(("%s: data is in mft record\n", __func__));
1449 if (uio)
1450 uiomove((char *)vap->va_datap + roff, rsize, uio);
1451 else
1452 memcpy(rdata, (char *)vap->va_datap + roff, rsize);
1453 *initp += rsize;
1454 }
1455
1456 return (error);
1457 }
1458
1459 /*
1460 * This is one of read routines.
1461 */
1462 int
1463 ntfs_readattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1464 u_int32_t attrnum, const char *attrname, off_t roff, size_t rsize,
1465 void *rdata, size_t *initp, struct uio *uio)
1466 {
1467 size_t init;
1468 int error = 0;
1469 off_t off = roff, left = rsize, toread;
1470 void *data = rdata;
1471 struct ntvattr *vap;
1472 *initp = 0;
1473
1474 while (left) {
1475 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1476 ntfs_btocn(off), &vap);
1477 if (error)
1478 return (error);
1479 toread = MIN(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1480 ddprintf(("%s: o: %qd, s: %qd (%qu - %qu)\n", __func__,
1481 (long long) off, (long long) toread,
1482 (long long) vap->va_vcnstart,
1483 (long long) vap->va_vcnend));
1484 error = ntfs_readntvattr_plain(ntmp, ip, vap,
1485 off - ntfs_cntob(vap->va_vcnstart),
1486 toread, data, &init, uio);
1487 if (error) {
1488 printf("%s: ntfs_readntvattr_plain failed: o: %qd, "
1489 "s: %qd\n", __func__,
1490 (long long) off, (long long) toread);
1491 printf("%s: attrib: %qu - %qu\n", __func__,
1492 (long long) vap->va_vcnstart,
1493 (long long) vap->va_vcnend);
1494 ntfs_ntvattrrele(vap);
1495 break;
1496 }
1497 ntfs_ntvattrrele(vap);
1498 left -= toread;
1499 off += toread;
1500 data = (char *)data + toread;
1501 *initp += init;
1502 }
1503
1504 return (error);
1505 }
1506
1507 /*
1508 * This is one of read routines.
1509 */
1510 int
1511 ntfs_readattr(struct ntfsmount *ntmp, struct ntnode *ip, u_int32_t attrnum,
1512 const char *attrname, off_t roff, size_t rsize, void *rdata,
1513 struct uio *uio)
1514 {
1515 int error = 0;
1516 struct ntvattr *vap;
1517 size_t init;
1518
1519 ddprintf(("%s: reading %llu: 0x%x, from %qd size %qu bytes\n",
1520 __func__, (unsigned long long)ip->i_number, attrnum,
1521 (long long)roff, (long long)rsize));
1522
1523 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname, 0, &vap);
1524 if (error)
1525 return (error);
1526
1527 if ((roff > vap->va_datalen) ||
1528 (roff + rsize > vap->va_datalen)) {
1529 printf("%s: offset too big: %qd (%qd) > %qu\n", __func__,
1530 (long long) roff, (long long) (roff + rsize),
1531 (long long) vap->va_datalen);
1532 ntfs_ntvattrrele(vap);
1533 return (E2BIG);
1534 }
1535 if (vap->va_compression && vap->va_compressalg) {
1536 u_int8_t *cup, *uup;
1537 off_t off, left, tocopy;
1538 void *data;
1539 cn_t cn;
1540
1541 left = rsize;
1542 data = rdata;
1543 ddprintf(("%s: compression: %d\n", __func__,
1544 vap->va_compressalg));
1545
1546 cup = malloc(ntfs_cntob(NTFS_COMPUNIT_CL),
1547 M_NTFSDECOMP, M_WAITOK);
1548 uup = malloc(ntfs_cntob(NTFS_COMPUNIT_CL),
1549 M_NTFSDECOMP, M_WAITOK);
1550
1551 cn = (ntfs_btocn(roff)) & (~(NTFS_COMPUNIT_CL - 1));
1552 off = roff - ntfs_cntob(cn);
1553
1554 while (left) {
1555 error = ntfs_readattr_plain(ntmp, ip, attrnum,
1556 attrname, ntfs_cntob(cn),
1557 ntfs_cntob(NTFS_COMPUNIT_CL), cup, &init, NULL);
1558 if (error)
1559 break;
1560
1561 tocopy = MIN(left, ntfs_cntob(NTFS_COMPUNIT_CL) - off);
1562
1563 if (init == ntfs_cntob(NTFS_COMPUNIT_CL)) {
1564 if (uio)
1565 uiomove(cup + off, tocopy, uio);
1566 else
1567 memcpy(data, cup + off, tocopy);
1568 } else if (init == 0) {
1569 if (uio) {
1570 char vbuf[] = "";
1571 size_t remains = tocopy;
1572 for (; remains; remains--)
1573 uiomove(vbuf, 1, uio);
1574 }
1575 else
1576 memset(data, 0, tocopy);
1577 } else {
1578 error = ntfs_uncompunit(ntmp, uup, cup);
1579 if (error)
1580 break;
1581 if (uio)
1582 uiomove(uup + off, tocopy, uio);
1583 else
1584 memcpy(data, uup + off, tocopy);
1585 }
1586
1587 left -= tocopy;
1588 data = (char *)data + tocopy;
1589 off += tocopy - ntfs_cntob(NTFS_COMPUNIT_CL);
1590 cn += NTFS_COMPUNIT_CL;
1591 }
1592
1593 free(uup, M_NTFSDECOMP);
1594 free(cup, M_NTFSDECOMP);
1595 } else
1596 error = ntfs_readattr_plain(ntmp, ip, attrnum, attrname,
1597 roff, rsize, rdata, &init, uio);
1598 ntfs_ntvattrrele(vap);
1599 return (error);
1600 }
1601
1602 #if UNUSED_CODE
1603 int
1604 ntfs_parserun(cn_t *cn, cn_t *cl, u_int8_t *run, u_long len, u_long *off)
1605 {
1606 u_int8_t sz;
1607 int i;
1608
1609 if (NULL == run) {
1610 printf("%s: run == NULL\n", __func__);
1611 return (EINVAL);
1612 }
1613 sz = run[(*off)++];
1614 if (0 == sz) {
1615 printf("%s: trying to go out of run\n", __func__);
1616 return (E2BIG);
1617 }
1618 *cl = 0;
1619 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1620 printf("%s: bad run: length too big: sz: 0x%02x "
1621 "(%ld < %ld + sz)\n", __func__, sz, len, *off);
1622 return (EINVAL);
1623 }
1624 for (i = 0; i < (sz & 0xF); i++)
1625 *cl += (u_int32_t) run[(*off)++] << (i << 3);
1626
1627 sz >>= 4;
1628 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1629 printf("%s: bad run: length too big: sz: 0x%02x "
1630 "(%ld < %ld + sz)\n", __func__, sz, len, *off);
1631 return (EINVAL);
1632 }
1633 for (i = 0; i < (sz & 0xF); i++)
1634 *cn += (u_int32_t) run[(*off)++] << (i << 3);
1635
1636 return (0);
1637 }
1638 #endif
1639
1640 /*
1641 * Process fixup routine on given buffer.
1642 */
1643 int
1644 ntfs_procfixups(struct ntfsmount *ntmp, u_int32_t magic, void *xbufv,
1645 size_t len)
1646 {
1647 char *xbuf = xbufv;
1648 struct fixuphdr *fhp = (struct fixuphdr *) xbuf;
1649 int i;
1650 u_int16_t fixup;
1651 u_int16_t *fxp, *cfxp;
1652
1653 if (fhp->fh_magic == 0)
1654 return (EINVAL);
1655 if (fhp->fh_magic != magic) {
1656 printf("%s: magic doesn't match: %08x != %08x\n", __func__,
1657 fhp->fh_magic, magic);
1658 return (EINVAL);
1659 }
1660 if ((fhp->fh_fnum - 1) * ntmp->ntm_bps != len) {
1661 printf("%s: bad fixups number: %d for %ld bytes block\n",
1662 __func__, fhp->fh_fnum, (long)len); /* XXX printf kludge */
1663 return (EINVAL);
1664 }
1665 if (fhp->fh_foff >= ntmp->ntm_spc * ntmp->ntm_mftrecsz * ntmp->ntm_bps) {
1666 printf("%s: invalid offset: %x", __func__, fhp->fh_foff);
1667 return (EINVAL);
1668 }
1669 fxp = (u_int16_t *) (xbuf + fhp->fh_foff);
1670 cfxp = (u_int16_t *) (xbuf + ntmp->ntm_bps - 2);
1671 fixup = *fxp++;
1672 for (i = 1; i < fhp->fh_fnum; i++, fxp++) {
1673 if (*cfxp != fixup) {
1674 printf("%s: fixup %d doesn't match\n", __func__, i);
1675 return (EINVAL);
1676 }
1677 *cfxp = *fxp;
1678 cfxp = (u_int16_t *)((char *)cfxp + ntmp->ntm_bps);
1679 }
1680 return (0);
1681 }
1682
1683 #if UNUSED_CODE
1684 int
1685 ntfs_runtocn(cn_t *cn, struct ntfsmount *ntmp, u_int8_t *run, u_long len,
1686 cn_t vcn)
1687 {
1688 cn_t ccn = 0, ccl = 0;
1689 u_long off = 0;
1690 int error = 0;
1691
1692 #ifdef NTFS_DEBUG
1693 int i;
1694 printf("%s: run: %p, %ld bytes, vcn:%ld\n", __func__,
1695 run, len, (u_long) vcn);
1696 printf("%s: run: ", __func__);
1697 for (i = 0; i < len; i++)
1698 printf("0x%02x ", run[i]);
1699 printf("\n");
1700 #endif
1701
1702 if (NULL == run) {
1703 printf("%s: run == NULL\n", __func__);
1704 return (EINVAL);
1705 }
1706 do {
1707 if (run[off] == 0) {
1708 printf("%s: vcn too big\n", __func__);
1709 return (E2BIG);
1710 }
1711 vcn -= ccl;
1712 error = ntfs_parserun(&ccn, &ccl, run, len, &off);
1713 if (error) {
1714 printf("%s: ntfs_parserun failed\n", __func__);
1715 return (error);
1716 }
1717 } while (ccl <= vcn);
1718 *cn = ccn + vcn;
1719 return (0);
1720 }
1721 #endif
1722
1723 /*
1724 * this initializes toupper table & dependent variables to be ready for
1725 * later work
1726 */
1727 void
1728 ntfs_toupper_init(void)
1729 {
1730 ntfs_toupper_tab = NULL;
1731 mutex_init(&ntfs_toupper_lock, MUTEX_DEFAULT, IPL_NONE);
1732 ntfs_toupper_usecount = 0;
1733 }
1734
1735 /*
1736 * if the ntfs_toupper_tab[] is filled already, just raise use count;
1737 * otherwise read the data from the filesystem we are currently mounting
1738 */
1739 int
1740 ntfs_toupper_use(struct mount *mp, struct ntfsmount *ntmp)
1741 {
1742 int error = 0;
1743 struct vnode *vp;
1744
1745 /* get exclusive access */
1746 mutex_enter(&ntfs_toupper_lock);
1747
1748 /* only read the translation data from a file if it hasn't been
1749 * read already */
1750 if (ntfs_toupper_tab)
1751 goto out;
1752
1753 /*
1754 * Read in Unicode lowercase -> uppercase translation file.
1755 * XXX for now, just the first 256 entries are used anyway,
1756 * so don't bother reading more
1757 */
1758 ntfs_toupper_tab = malloc(256 * 256 * sizeof(*ntfs_toupper_tab),
1759 M_NTFSRDATA, M_WAITOK);
1760
1761 if ((error = VFS_VGET(mp, NTFS_UPCASEINO, &vp)))
1762 goto out;
1763 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
1764 0, 256 * 256 * sizeof(*ntfs_toupper_tab), (char *)ntfs_toupper_tab,
1765 NULL);
1766 vput(vp);
1767
1768 out:
1769 ntfs_toupper_usecount++;
1770 mutex_exit(&ntfs_toupper_lock);
1771 return (error);
1772 }
1773
1774 /*
1775 * lower the use count and if it reaches zero, free the memory
1776 * tied by toupper table
1777 */
1778 void
1779 ntfs_toupper_unuse(void)
1780 {
1781 /* get exclusive access */
1782 mutex_enter(&ntfs_toupper_lock);
1783
1784 ntfs_toupper_usecount--;
1785 if (ntfs_toupper_usecount == 0) {
1786 free(ntfs_toupper_tab, M_NTFSRDATA);
1787 ntfs_toupper_tab = NULL;
1788 }
1789 #ifdef DIAGNOSTIC
1790 else if (ntfs_toupper_usecount < 0) {
1791 panic("ntfs_toupper_unuse(): use count negative: %d",
1792 ntfs_toupper_usecount);
1793 }
1794 #endif
1795
1796 /* release the lock */
1797 mutex_exit(&ntfs_toupper_lock);
1798 }
1799