summaryrefslogtreecommitdiff
path: root/core/fs/ntfs/ntfs.c
blob: a69401e4ef47258a904b3d2ae716223a70259167 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2011 Paulo Alcantara <pcacjr@gmail.com>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
 *   Boston MA 02111-1307, USA; either version 2 of the License, or
 *   (at your option) any later version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * ntfs.c - The NTFS filesystem functions
 */

#include <dprintf.h>
#include <stdio.h>
#include <string.h>
#include <sys/dirent.h>
#include <cache.h>
#include <core.h>
#include <disk.h>
#include <fs.h>
#include <ilog2.h>
#include <klibc/compiler.h>

#include "codepage.h"
#include "ntfs.h"

/* Check if there are specific zero fields in an NTFS boot sector */
static inline int ntfs_check_zero_fields(const struct ntfs_bpb *sb)
{
    return !sb->res_sectors && (!sb->zero_0[0] && !sb->zero_0[1] &&
            !sb->zero_0[2]) && !sb->zero_1 && !sb->zero_2 &&
            !sb->zero_3;
}

static inline int ntfs_check_sb_fields(const struct ntfs_bpb *sb)
{
    return ntfs_check_zero_fields(sb) &&
            (!memcmp(sb->oem_name, "NTFS    ", 8) ||
             !memcmp(sb->oem_name, "MSWIN4.0", 8) ||
             !memcmp(sb->oem_name, "MSWIN4.1", 8));
}

static inline struct inode *new_ntfs_inode(struct fs_info *fs)
{
    struct inode *inode;

    inode = alloc_inode(fs, 0, sizeof(struct ntfs_inode));
    if (!inode)
        malloc_error("inode structure");

    return inode;
}

static inline const void *get_right_block(struct fs_info *fs,
                                                        block_t block)
{
    return get_cache(fs->fs_dev, NTFS_SB(fs)->mft_block + block);
}

static int fixups_copyback(struct fs_info *fs, void *buf, NTFS_RECORD *nrec,
                            uint64_t size)
{
    uint8_t *usa_start;
    uint16_t usa_no;
    uint8_t *usa_end;
    unsigned sectors;
    uint64_t offset = 0;
    uint16_t *val;
    const unsigned byte_shift = 8;

    if (nrec->magic != NTFS_MAGIC_FILE && nrec->magic != NTFS_MAGIC_INDX) {
        printf("Not a NTFS record\n");
        goto out;
    }

    /* sectors per block */
    sectors = size >> SECTOR_SHIFT(fs);

    /* get the Update Sequence Array start address */
    usa_start = (uint8_t *)nrec + nrec->usa_ofs;
    /* get the Update Sequence Number */
    usa_no = *(uint16_t *)usa_start;
    /* get the Update Sequence Array end address */
    usa_end = usa_start + nrec->usa_count + 1;

    do {
        offset += SECTOR_SIZE(fs) - 2;

        val = (uint16_t *)((uint8_t *)buf + offset);
        /* compare it against the last two bytes of every sector */
        if (*val == usa_no) {
            if (usa_start < usa_end && usa_start + 1 < usa_end) {
                *val = *usa_start++;
                *val <<= byte_shift | *usa_start++;
            }
        }
    } while ((offset + SECTOR_SIZE(fs)) >> SECTOR_SHIFT(fs) < sectors);

    return 0;

out:
    return -1;
}

static int64_t mft_record_lookup(uint32_t file, struct fs_info *fs,
                                    block_t *block, void *data)
{
    int64_t offset = 0;
    const uint8_t *ret;
    int err;
    const uint64_t blk_size = UINT64_C(1) << BLOCK_SHIFT(fs);
    const uint64_t mft_record_size = NTFS_SB(fs)->mft_record_size;
    MFT_RECORD *mrec;

    goto jump_in;

    for (;;) {
        err = fixups_copyback(fs, (uint8_t *)data + offset,
                                (NTFS_RECORD *)data, mft_record_size);
        if (err)
            break;

        mrec = (MFT_RECORD *)((uint8_t *)data + offset);
        if (mrec->mft_record_no == file)
            return offset;   /* MFT record found! */

        offset += mrec->bytes_allocated;
        if (offset >= BLOCK_SIZE(fs)) {
            ++*block;
            offset -= BLOCK_SIZE(fs);
jump_in:
            ret = get_right_block(fs, *block);
            if (!ret)
                break;

            memcpy(data, ret, blk_size);
        }
    }

    return -1;
}

static ATTR_RECORD *attr_lookup(uint32_t type, const MFT_RECORD *mrec)
{
    ATTR_RECORD *attr;

    /* sanity check */
    if (!mrec || type == NTFS_AT_END)
        return NULL;

    attr = (ATTR_RECORD *)((uint8_t *)mrec + mrec->attrs_offset);
    /* walk through the file attribute records */
    for (;; attr = (ATTR_RECORD *)((uint8_t *)attr + attr->len)) {
        if (attr->type == NTFS_AT_END)
            return NULL;

        if (attr->type == type)
            break;
    }

    return attr;
}

static bool ntfs_match_longname(const char *str, unsigned long mft_no,
                                    struct fs_info *fs)
{
    uint8_t data[1 << BLOCK_SHIFT(fs)];
    int64_t offset;
    MFT_RECORD *mrec;
    block_t block = 0;
    ATTR_RECORD *attr;
    FILE_NAME_ATTR *fn;
    uint8_t len;
    unsigned char c = -1;	/* Nonzero: we have not yet seen NULL */
    uint16_t cp;
    const uint16_t *match;

    dprintf("Matching: %s\n", str);

    offset = mft_record_lookup(mft_no, fs, &block, &data);
    if (offset < 0) {
        printf("No MFT record found!\n");
        goto out;
    }

    mrec = (MFT_RECORD *)&data[offset];

    attr = attr_lookup(NTFS_AT_FILENAME, mrec);
    if (!attr) {
        printf("No attribute found!\n");
        goto out;
    }

    fn = (FILE_NAME_ATTR *)((uint8_t *)attr + attr->data.resident.value_offset);
    len = fn->file_name_len;
    match = fn->file_name;

    /*
    if (!strcmp("syslinux", str)) {
        const char *s = (const char *)fn->file_name;
        printf("Filename:                   ");
        while (*s) {
            printf("%c", *s);
            s += 2;
        }
        printf("\n");
    }
    */

    while (len) {
        cp = *match++;
        len--;
        if (!cp)
            break;

        c = *str++;
        if (cp != codepage.uni[0][c] && cp != codepage.uni[1][c])
            goto out;
    }

    if (*str)
        goto out;

    while (len--)
        if (*match++ != 0xffff)
            goto out;

    return true;

out:
    return false;
}

enum {
    MAP_UNSPEC,
    MAP_START           = 1 << 0,
    MAP_END             = 1 << 1,
    MAP_ALLOCATED       = 1 << 2,
    MAP_UNALLOCATED     = 1 << 3,
    MAP_MASK            = 0x0000000F,
};

struct mapping_chunk {
    uint64_t cur_vcn;   /* Current Virtual Cluster Number */
    uint8_t vcn_len;    /* Virtual Cluster Number length in bytes */
    uint64_t next_vcn;  /* Next Virtual Cluster Number */
    uint8_t lcn_len;    /* Logical Cluster Number length in bytes */
    int64_t cur_lcn;    /* Logical Cluster Number offset */
    uint32_t flags;     /* Specific flags of this chunk */
};

/* Parse data runs.
 *
 * return 0 on success or -1 on failure.
 */
static int parse_data_run(const void *stream, uint32_t *offset,
                            uint8_t *attr_len, struct mapping_chunk *chunk)
{
    uint8_t *buf;   /* Pointer to the zero-terminated byte stream */
    uint8_t count;  /* The count byte */
    uint8_t v, l;   /* v is the number of changed low-order VCN bytes;
                     * l is the number of changed low-order LCN bytes
                     */
    uint8_t *byte;
    int byte_shift = 8;
    int mask;
    uint8_t val;
    int64_t res;

    chunk->flags &= ~MAP_MASK;

    buf = (uint8_t *)stream + *offset;
    if (buf > attr_len || !*buf) {
        chunk->flags |= MAP_END;    /* we're done */
        return 0;
    }

    if (!*offset)
        chunk->flags |= MAP_START;  /* initial chunk */

    chunk->cur_vcn = chunk->next_vcn;

    count = *buf;
    v = count & 0x0F;
    l = count >> 4;

    if (v > 8 || l > 8) /* more than 8 bytes ? */
        goto out;

    chunk->vcn_len = v;
    chunk->lcn_len = l;

    byte = (uint8_t *)buf + v;
    count = v;

    res = 0LL;
    while (count--) {
        val = *byte--;
        mask = val >> (byte_shift - 1);
        res = (res << byte_shift) | ((val + mask) ^ mask);
    }

    chunk->next_vcn += res;

    byte = (uint8_t *)buf + v + l;
    count = l;

    mask = 0xFFFFFFFF;
    res = 0LL;
    if (*byte & 0x80)
        res |= (int64_t)mask;   /* sign-extend it */

    while (count--)
        res = (res << byte_shift) | *byte--;

    chunk->cur_lcn += res;
    if (!chunk->cur_lcn) {  /* is LCN 0 ? */
        /* then VCNS from cur_vcn to next_vcn - 1 are unallocated */
        chunk->flags |= MAP_UNALLOCATED;
    } else {
        /* otherwise they're all allocated */
        chunk->flags |= MAP_ALLOCATED;
    }

    *offset += v + l + 1;

    return 0;

out:
    return -1;
}

static enum dirent_type get_inode_mode(MFT_RECORD *mrec)
{
    ATTR_RECORD *attr;
    FILE_NAME_ATTR *fn;
    bool infile = false;
    uint32_t dir_mask, root_mask, file_mask;
    uint32_t dir, root, file;

    attr = attr_lookup(NTFS_AT_FILENAME, mrec);
    if (!attr) {
        dprintf("No attribute found!\n");
        return DT_UNKNOWN;
    }

    fn = (FILE_NAME_ATTR *)((uint8_t *)attr +
                                attr->data.resident.value_offset);
    dprintf("File attributes:        0x%X\n", fn->file_attrs);

    dir_mask = NTFS_FILE_ATTR_ARCHIVE |
                NTFS_FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT;
    root_mask = NTFS_FILE_ATTR_READONLY | NTFS_FILE_ATTR_HIDDEN |
                NTFS_FILE_ATTR_SYSTEM |
                NTFS_FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT;
    file_mask = NTFS_FILE_ATTR_ARCHIVE;

    dir = fn->file_attrs & ~dir_mask;
    root = fn->file_attrs & ~root_mask;
    file = fn->file_attrs & ~file_mask;

    dprintf("dir = 0x%X\n", dir);
    dprintf("root= 0x%X\n", root);
    dprintf("file = 0x%X\n", file);
    if (((!dir && root) || (!dir && !root)) && !file)
        infile = true;

    return infile ? DT_REG : DT_DIR;
}

static int index_inode_setup(struct fs_info *fs, unsigned long mft_no,
                                struct inode *inode)
{
    uint8_t data[1 << BLOCK_SHIFT(fs)];
    int64_t offset;
    MFT_RECORD *mrec;
    block_t block = 0;
    ATTR_RECORD *attr;
    enum dirent_type d_type;
    uint32_t len;
    INDEX_ROOT *ir;
    uint32_t clust_size;
    uint8_t *attr_len;
    struct mapping_chunk chunk;
    int err;
    uint8_t *stream;
    uint32_t droffset;

    offset = mft_record_lookup(mft_no, fs, &block, &data);
    if (offset < 0) {
        dprintf("No MFT record found!\n");
        goto out;
    }

    mrec = (MFT_RECORD *)&data[offset];

    NTFS_PVT(inode)->mft_no = mft_no;
    NTFS_PVT(inode)->seq_no = mrec->seq_no;

    NTFS_PVT(inode)->start_cluster = block >> NTFS_SB(fs)->clust_shift;
    NTFS_PVT(inode)->here = block;

    d_type = get_inode_mode(mrec);
    if (d_type == DT_UNKNOWN) {
        dprintf("Failed on determining inode's mode\n");
        goto out;
    }

    if (d_type == DT_DIR) {    /* directory stuff */
        printf("Got a directory.\n");
        attr = attr_lookup(NTFS_AT_INDEX_ROOT, mrec);
        if (!attr) {
            dprintf("No attribute found!\n");
            goto out;
        }

        printf("here!\n");

        /* note: INDEX_ROOT is always resident */
        ir = (INDEX_ROOT *)((uint8_t *)attr +
                                    attr->data.resident.value_offset);
        len = attr->data.resident.value_len;
        if ((uint8_t *)ir + len > (uint8_t *)mrec +
                        NTFS_SB(fs)->mft_record_size) {
            dprintf("Corrupt index\n");
            goto out;
        }

        NTFS_PVT(inode)->itype.index.collation_rule = ir->collation_rule;
        NTFS_PVT(inode)->itype.index.block_size = ir->index_block_size;
        NTFS_PVT(inode)->itype.index.block_size_shift =
                            ilog2(NTFS_PVT(inode)->itype.index.block_size);

        /* determine the size of a vcn in the index */
        clust_size = NTFS_PVT(inode)->itype.index.block_size;
        if (NTFS_SB(fs)->clust_size <= clust_size) {
            NTFS_PVT(inode)->itype.index.vcn_size = NTFS_SB(fs)->clust_size;
            NTFS_PVT(inode)->itype.index.vcn_size_shift =
                                        NTFS_SB(fs)->clust_shift;
        } else {
            NTFS_PVT(inode)->itype.index.vcn_size = BLOCK_SIZE(fs);
            NTFS_PVT(inode)->itype.index.vcn_size_shift = BLOCK_SHIFT(fs);
        }
    } else if (d_type == DT_REG) {        /* file stuff */
        printf("Got a file.\n");
        attr = attr_lookup(NTFS_AT_DATA, mrec);
        if (!attr) {
            dprintf("No attribute found!\n");
            goto out;
        }

        NTFS_PVT(inode)->non_resident = attr->non_resident;
        NTFS_PVT(inode)->type = attr->type;

        if (!attr->non_resident) {
            NTFS_PVT(inode)->data.resident.offset =
                (uint32_t)((uint8_t *)attr + attr->data.resident.value_offset);
            inode->size = attr->data.resident.value_len;
        } else {
            attr_len = (uint8_t *)attr + attr->len;

            memset((void *)&chunk, 0, sizeof(chunk));
            chunk.cur_vcn = attr->data.non_resident.lowest_vcn;
            chunk.cur_lcn = 0LL;

            stream = (uint8_t *)attr +
                            attr->data.non_resident.mapping_pairs_offset;
            droffset = 0U;

            for (;;) {
                err = parse_data_run(stream, &droffset, attr_len, &chunk);
                if (err) {
                    printf("Non-resident $DATA attribute without any run\n");
                    goto out;
                }

                if (chunk.flags & MAP_UNALLOCATED)
                    continue;
                if (chunk.flags & (MAP_ALLOCATED | MAP_END))
                    break;
            }

            if (chunk.flags & MAP_END) {
                dprintf("No mapping found\n");
                goto out;
            }

            NTFS_PVT(inode)->data.non_resident.start_vcn = chunk.cur_vcn;
            NTFS_PVT(inode)->data.non_resident.next_vcn = chunk.next_vcn;
            NTFS_PVT(inode)->data.non_resident.vcn_no = chunk.vcn_len;
            NTFS_PVT(inode)->data.non_resident.lcn = chunk.cur_lcn;
            inode->size = attr->data.non_resident.initialized_size;
        }
    }

    inode->mode = d_type;

    return 0;

out:
    return -1;
}

static struct inode *index_lookup(const char *dname, struct inode *dir)
{
    struct fs_info *fs = dir->fs;
    const uint64_t blk_size = UINT64_C(1) << BLOCK_SHIFT(fs);
    uint8_t data[blk_size];
    int64_t offset;
    MFT_RECORD *mrec;
    block_t block;
    ATTR_RECORD *attr;
    INDEX_ROOT *ir;
    uint32_t len;
    INDEX_ENTRY *ie;
    uint8_t vcn_count;
    uint8_t *ret;
    INDEX_BLOCK *iblock;
    int64_t vcn;
    uint8_t *stream;
    uint8_t *attr_len;
    struct mapping_chunk chunk;
    uint32_t droffset;
    int err;
    struct inode *inode;

    block = NTFS_PVT(dir)->start;
    dprintf("index_lookup() - mft record number: %d\n", NTFS_PVT(dir)->mft_no);
    offset = mft_record_lookup(NTFS_PVT(dir)->mft_no, fs, &block, &data);
    if (offset < 0) {
        dprintf("No MFT record found!\n");
        goto out;
    }

    mrec = (MFT_RECORD *)&data[offset];

    attr = attr_lookup(NTFS_AT_INDEX_ROOT, mrec);
    if (!attr) {
        dprintf("No attribute found!\n");
        goto out;
    }

    ir = (INDEX_ROOT *)((uint8_t *)attr +
                            attr->data.resident.value_offset);
    len = attr->data.resident.value_len;
    /* sanity check */
    if ((uint8_t *)ir + len > (uint8_t *)mrec + NTFS_SB(fs)->mft_record_size)
        goto index_err;

    ie = (INDEX_ENTRY *)((uint8_t *)&ir->index +
                                ir->index.entries_offset);
    for (;; ie = (INDEX_ENTRY *)((uint8_t *)ie + ie->len)) {
        /* bounds checks */
        if ((uint8_t *)ie < (uint8_t *)mrec ||
            (uint8_t *)ie + sizeof(INDEX_ENTRY_HEADER) >
            (uint8_t *)&ir->index + ir->index.index_len ||
            (uint8_t *)ie + ie->len >
            (uint8_t *)&ir->index + ir->index.index_len)
            goto index_err;

        /* last entry cannot contain a key. it can however contain
         * a pointer to a child node in the B+ tree so we just break out
         */
        dprintf("(0) ie->flags:          0x%X\n", ie->flags);
        if (ie->flags & INDEX_ENTRY_END)
            break;

        if (ntfs_match_longname(dname, ie->data.dir.indexed_file, fs)) {
            dprintf("Filename matches up!\n");
            dprintf("MFT record number = %d\n", ie->data.dir.indexed_file);
            goto found;
        }
    }

    /* check for the presence of a child node */
    if (!(ie->flags & INDEX_ENTRY_NODE)) {
        dprintf("No child node, aborting...\n");
        goto out;
    }

    /* then descend into child node */

    attr = attr_lookup(NTFS_AT_INDEX_ALLOCATION, mrec);
    if (!attr) {
        printf("No attribute found!\n");
        goto out;
    }

    if (!attr->non_resident) {
        printf("WTF ?! $INDEX_ALLOCATION isn't really resident.\n");
        goto out;
    }

    attr_len = (uint8_t *)attr + attr->len;

    memset((void *)&chunk, 0, sizeof(chunk));
    chunk.cur_vcn = attr->data.non_resident.lowest_vcn;
    chunk.cur_lcn = 0LL;

    stream = (uint8_t *)attr + attr->data.non_resident.mapping_pairs_offset;
    droffset = 0U;

    for (;;) {
        err = parse_data_run(stream, &droffset, attr_len, &chunk);
        if (err)
            goto not_found;

        if (chunk.flags & MAP_UNALLOCATED)
            continue;
        if (chunk.flags & MAP_END)
            break;

        if (chunk.flags & MAP_ALLOCATED) {
            chunk.cur_lcn = 0x24de8;
            printf("%d cluster(s) starting at 0x%X\n", chunk.vcn_len,
                    chunk.cur_lcn);

            vcn_count = 0;
            vcn = chunk.cur_vcn;        /* HUMMMMMMM!!!!! */
            while (vcn_count++ < chunk.vcn_len) {
                block = ((chunk.cur_lcn + vcn) << NTFS_SB(fs)->clust_shift) <<
                        SECTOR_SHIFT(fs) >> BLOCK_SHIFT(fs);

                ret = (uint8_t *)get_cache(fs->fs_dev, block);
                if (!ret) {
                    printf("get_cache() returned NULL\n");
                    goto not_found;
                }

                memcpy(data, ret, blk_size);

                err = fixups_copyback(fs, data, (NTFS_RECORD *)data, blk_size);
                if (err)
                    goto not_found;

                iblock = (INDEX_BLOCK *)data;
                if (iblock->magic != NTFS_MAGIC_INDX) {
                    printf("Not a valid INDX record\n");
                    goto not_found;
                }

                ie = (INDEX_ENTRY *)((uint8_t *)&iblock->index +
                                            iblock->index.entries_offset);
                for (;; ie = (INDEX_ENTRY *)((uint8_t *)ie + ie->len)) {
                    /* bounds checks */
                    if ((uint8_t *)ie < (uint8_t *)iblock || (uint8_t *)ie +
                        sizeof(INDEX_ENTRY_HEADER) >
                        (uint8_t *)&iblock->index + iblock->index.index_len ||
                        (uint8_t *)ie + ie->len >
                        (uint8_t *)&iblock->index + iblock->index.index_len)
                        goto index_err;

                    /* last entry cannot contain a key */
                    if (ie->flags & INDEX_ENTRY_END)
                        break;

                    if (ntfs_match_longname(dname, ie->data.dir.indexed_file,
                                            fs)) {
                        dprintf("Filename matches up!\n");
                        dprintf("MFT record number = %d\n",
                                ie->data.dir.indexed_file);
                        goto found;
                    }
                }

                ++vcn;  /* go to the next VCN */
            }
        }
    }

not_found:
    dprintf("Index not found\n");

out:
    printf("%s not found!\n", dname);

    return NULL;

found:
    dprintf("--------------- Found index -------------------\n");
    inode = new_ntfs_inode(fs);
    err = index_inode_setup(fs, ie->data.dir.indexed_file, inode);
    if (err) {
        free(inode);
        goto out;
    }

    printf("%s found!\n", dname);

    return inode;

index_err:
    printf("Corrupt index. Aborting lookup...\n");
    goto out;
}

/*
 * Convert an UTF-16LE longname to the system codepage; return
 * the length on success or -1 on failure.
 */
static int ntfs_cvt_longname(char *entry_name, const uint16_t *long_name)
{
    struct unicache {
        uint16_t utf16;
        uint8_t cp;
    };
    static struct unicache unicache[256];
    struct unicache *uc;
    uint16_t cp;
    unsigned int c;
    char *p = entry_name;

    do {
        cp = *long_name++;
        uc = &unicache[cp % 256];

        if (__likely(uc->utf16 == cp)) {
            *p++ = uc->cp;
        } else {
            for (c = 0; c < 512; c++) {
                if (codepage.uni[0][c] == cp) {
                    uc->utf16 = cp;
                    *p++ = uc->cp = (uint8_t)c;
                    goto found;
                }
            }

            return -1;
            found:
                ;
        }
    } while (cp);

    return (p - entry_name) - 1;
}

static int ntfs_next_extent(struct inode *inode, uint32_t lstart)
{
    struct fs_info *fs = inode->fs;
    struct ntfs_sb_info *sbi = NTFS_SB(fs);
    uint32_t mcluster = lstart >> sbi->clust_shift;
    uint32_t tcluster;
    const uint32_t cluster_bytes = UINT32_C(1) << sbi->clust_byte_shift;
    sector_t pstart;
    const uint32_t sec_size = SECTOR_SIZE(fs);
    const uint32_t sec_shift = SECTOR_SHIFT(fs);

    tcluster = (inode->size + cluster_bytes - 1) >> sbi->clust_byte_shift;
    if (mcluster >= tcluster)
        goto out;       /* Requested cluster beyond end of file */

    if (!NTFS_PVT(inode)->non_resident) {
        pstart = sbi->mft_block + NTFS_PVT(inode)->here;
        pstart <<= BLOCK_SHIFT(fs) >> sec_shift;
    } else {
        pstart = NTFS_PVT(inode)->data.non_resident.lcn << sbi->clust_shift;
    }

    inode->next_extent.len = (inode->size + sec_size - 1) >> sec_shift;
    inode->next_extent.pstart = pstart;

    return 0;

out:
    return -1;
}

static uint32_t ntfs_getfssec(struct file *file, char *buf, int sectors,
                                bool *have_more)
{
    uint8_t non_resident;
    uint32_t ret;
    int64_t offset;
    struct fs_info *fs = file->fs;
    uint8_t data[1 << BLOCK_SHIFT(fs)];
    struct inode *inode = file->inode;
    block_t block = 0;
    MFT_RECORD *mrec;
    ATTR_RECORD *attr;
    char *p;
    int err;

    non_resident = NTFS_PVT(inode)->non_resident;

    ret = generic_getfssec(file, buf, sectors, have_more);
    if (!ret)
        return ret;

    if (!non_resident) {
        dprintf("mft_no:     %d\n", NTFS_PVT(inode)->mft_no);
        offset = mft_record_lookup(NTFS_PVT(inode)->mft_no, fs, &block, &data);
        if (offset < 0) {
            dprintf("No MFT record found!\n");
            goto out;
        }

        mrec = (MFT_RECORD *)&data[offset];

        attr = attr_lookup(NTFS_AT_DATA, mrec);
        if (!attr) {
            dprintf("No attribute found!\n");
            goto out;
        }

        p = (char *)attr + attr->data.resident.value_offset;

        /* p now points to the data offset, so let's copy it into buf */
        memcpy(buf, p, inode->size);

        err = fixups_copyback(fs, buf, (NTFS_RECORD *)mrec, inode->size);
        if (err)
            goto out;

        ret = inode->size;
    }

    return ret;

out:
    return 0;
}

static int ntfs_readdir(struct file *file, struct dirent *dirent)
{
    int64_t offset;
    struct fs_info *fs = file->fs;
    uint8_t data[1 << BLOCK_SHIFT(fs)];
    MFT_RECORD *mrec;
    struct inode *inode = file->inode;
    block_t block = 0;
    ATTR_RECORD *attr;
    FILE_NAME_ATTR *fn;
    char filename[NTFS_MAX_FILE_NAME_LEN + 1];
    int len;

    printf("in readdir()\n");

    offset = mft_record_lookup(NTFS_PVT(inode)->mft_no, fs, &block, &data);
    if (offset < 0) {
        dprintf("No MFT record found!\n");
        goto out;
    }

    mrec = (MFT_RECORD *)&data[offset];

    attr = attr_lookup(NTFS_AT_FILENAME, mrec);
    if (!attr) {
        dprintf("No attribute found!\n");
        goto out;
    }

    fn = (FILE_NAME_ATTR *)((uint8_t *)attr +
                            attr->data.resident.value_offset);

    len = ntfs_cvt_longname(filename, fn->file_name);
    if (len < 0 || len != fn->file_name_len) {
        dprintf("Failed on converting UTF-16LE LFN to OEM LFN\n");
        goto out;
    }

    dirent->d_ino = NTFS_PVT(inode)->mft_no;
    dirent->d_off = file->offset;
    dirent->d_reclen = offsetof(struct dirent, d_name) + len + 1;
    dirent->d_type = get_inode_mode(mrec);
    memcpy(dirent->d_name, filename, len + 1);

    return 0;

out:
    return -1;
}

static struct inode *ntfs_iget(const char *dname, struct inode *parent)
{
    return index_lookup(dname, parent);
}

static struct inode *ntfs_iget_root(struct fs_info *fs)
{
    struct inode *inode = new_ntfs_inode(fs);
    int err;

    inode->fs = fs;

    err = index_inode_setup(fs, FILE_root, inode);
    if (err)
        goto free_out;

    NTFS_PVT(inode)->start = NTFS_PVT(inode)->here;

    return inode;

free_out:
    free(inode);

    return NULL;
}

/* Initialize the filesystem metadata and return block size in bits */
static int ntfs_fs_init(struct fs_info *fs)
{
    struct ntfs_bpb ntfs;
    struct ntfs_sb_info *sbi;
    struct disk *disk = fs->fs_dev->disk;
    uint8_t clust_per_mft_record;

    disk->rdwr_sectors(disk, &ntfs, 0, 1, 0);

    /* sanity check */
    if (!ntfs_check_sb_fields(&ntfs))
        return -1;

    /* Note: clust_per_mft_record can be a negative number */
    clust_per_mft_record = ntfs.clust_per_mft_record < 0 ?
                    -ntfs.clust_per_mft_record : ntfs.clust_per_mft_record;

    SECTOR_SHIFT(fs) = disk->sector_shift;

    /* We need _at least_ 1 KiB to read the whole MFT record */
    BLOCK_SHIFT(fs) = ilog2(ntfs.sec_per_clust) + SECTOR_SHIFT(fs);
    if (BLOCK_SHIFT(fs) < clust_per_mft_record)
        BLOCK_SHIFT(fs) = clust_per_mft_record;

    SECTOR_SIZE(fs) = 1 << SECTOR_SHIFT(fs);
    BLOCK_SIZE(fs) = 1 << BLOCK_SHIFT(fs);

    sbi = malloc(sizeof(*sbi));
    if (!sbi)
        malloc_error("ntfs_sb_info structure");

    fs->fs_info = sbi;

    sbi->clust_shift        = ilog2(ntfs.sec_per_clust);
    sbi->clust_byte_shift   = sbi->clust_shift + SECTOR_SHIFT(fs);
    sbi->clust_mask         = ntfs.sec_per_clust - 1;
    sbi->clust_size         = ntfs.sec_per_clust << SECTOR_SHIFT(fs);
    sbi->mft_record_size    = 1 << clust_per_mft_record;

    sbi->mft_block = ntfs.mft_lclust << sbi->clust_shift <<
                    SECTOR_SHIFT(fs) >> BLOCK_SHIFT(fs);
    /* 16 MFT entries reserved for metadata files (approximately 16 KiB) */
    sbi->mft_size = (clust_per_mft_record << sbi->clust_shift) << 4;

    sbi->clusters = ntfs.total_sectors << SECTOR_SHIFT(fs) >> sbi->clust_shift;
    if (sbi->clusters > 0xFFFFFFFFFFF4ULL)
        sbi->clusters = 0xFFFFFFFFFFF4ULL;

    /* Initialize the cache */
    cache_init(fs->fs_dev, BLOCK_SHIFT(fs));

    return BLOCK_SHIFT(fs);
}

const struct fs_ops ntfs_fs_ops = {
    .fs_name        = "ntfs",
    .fs_flags       = FS_USEMEM | FS_THISIND,
    .fs_init        = ntfs_fs_init,
    .searchdir      = NULL,
    .getfssec       = ntfs_getfssec,
    .close_file     = generic_close_file,
    .mangle_name    = generic_mangle_name,
    .load_config    = generic_load_config,
    .readdir        = ntfs_readdir,
    .iget_root      = ntfs_iget_root,
    .iget           = ntfs_iget,
    .next_extent    = ntfs_next_extent,
};