forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.c
2584 lines (2151 loc) · 53.7 KB
/
index.c
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
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0
/*
*
* Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
*
*/
#include <linux/blkdev.h>
#include <linux/buffer_head.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include "debug.h"
#include "ntfs.h"
#include "ntfs_fs.h"
static const struct INDEX_NAMES {
const __le16 *name;
u8 name_len;
} s_index_names[INDEX_MUTEX_TOTAL] = {
{ I30_NAME, ARRAY_SIZE(I30_NAME) }, { SII_NAME, ARRAY_SIZE(SII_NAME) },
{ SDH_NAME, ARRAY_SIZE(SDH_NAME) }, { SO_NAME, ARRAY_SIZE(SO_NAME) },
{ SQ_NAME, ARRAY_SIZE(SQ_NAME) }, { SR_NAME, ARRAY_SIZE(SR_NAME) },
};
/*
* cmp_fnames - Compare two names in index.
*
* if l1 != 0
* Both names are little endian on-disk ATTR_FILE_NAME structs.
* else
* key1 - cpu_str, key2 - ATTR_FILE_NAME
*/
static int cmp_fnames(const void *key1, size_t l1, const void *key2, size_t l2,
const void *data)
{
const struct ATTR_FILE_NAME *f2 = key2;
const struct ntfs_sb_info *sbi = data;
const struct ATTR_FILE_NAME *f1;
u16 fsize2;
bool both_case;
if (l2 <= offsetof(struct ATTR_FILE_NAME, name))
return -1;
fsize2 = fname_full_size(f2);
if (l2 < fsize2)
return -1;
both_case = f2->type != FILE_NAME_DOS /*&& !sbi->options.nocase*/;
if (!l1) {
const struct le_str *s2 = (struct le_str *)&f2->name_len;
/*
* If names are equal (case insensitive)
* try to compare it case sensitive.
*/
return ntfs_cmp_names_cpu(key1, s2, sbi->upcase, both_case);
}
f1 = key1;
return ntfs_cmp_names(f1->name, f1->name_len, f2->name, f2->name_len,
sbi->upcase, both_case);
}
/*
* cmp_uint - $SII of $Secure and $Q of Quota
*/
static int cmp_uint(const void *key1, size_t l1, const void *key2, size_t l2,
const void *data)
{
const u32 *k1 = key1;
const u32 *k2 = key2;
if (l2 < sizeof(u32))
return -1;
if (*k1 < *k2)
return -1;
if (*k1 > *k2)
return 1;
return 0;
}
/*
* cmp_sdh - $SDH of $Secure
*/
static int cmp_sdh(const void *key1, size_t l1, const void *key2, size_t l2,
const void *data)
{
const struct SECURITY_KEY *k1 = key1;
const struct SECURITY_KEY *k2 = key2;
u32 t1, t2;
if (l2 < sizeof(struct SECURITY_KEY))
return -1;
t1 = le32_to_cpu(k1->hash);
t2 = le32_to_cpu(k2->hash);
/* First value is a hash value itself. */
if (t1 < t2)
return -1;
if (t1 > t2)
return 1;
/* Second value is security Id. */
if (data) {
t1 = le32_to_cpu(k1->sec_id);
t2 = le32_to_cpu(k2->sec_id);
if (t1 < t2)
return -1;
if (t1 > t2)
return 1;
}
return 0;
}
/*
* cmp_uints - $O of ObjId and "$R" for Reparse.
*/
static int cmp_uints(const void *key1, size_t l1, const void *key2, size_t l2,
const void *data)
{
const __le32 *k1 = key1;
const __le32 *k2 = key2;
size_t count;
if ((size_t)data == 1) {
/*
* ni_delete_all -> ntfs_remove_reparse ->
* delete all with this reference.
* k1, k2 - pointers to REPARSE_KEY
*/
k1 += 1; // Skip REPARSE_KEY.ReparseTag
k2 += 1; // Skip REPARSE_KEY.ReparseTag
if (l2 <= sizeof(int))
return -1;
l2 -= sizeof(int);
if (l1 <= sizeof(int))
return 1;
l1 -= sizeof(int);
}
if (l2 < sizeof(int))
return -1;
for (count = min(l1, l2) >> 2; count > 0; --count, ++k1, ++k2) {
u32 t1 = le32_to_cpu(*k1);
u32 t2 = le32_to_cpu(*k2);
if (t1 > t2)
return 1;
if (t1 < t2)
return -1;
}
if (l1 > l2)
return 1;
if (l1 < l2)
return -1;
return 0;
}
static inline NTFS_CMP_FUNC get_cmp_func(const struct INDEX_ROOT *root)
{
switch (root->type) {
case ATTR_NAME:
if (root->rule == NTFS_COLLATION_TYPE_FILENAME)
return &cmp_fnames;
break;
case ATTR_ZERO:
switch (root->rule) {
case NTFS_COLLATION_TYPE_UINT:
return &cmp_uint;
case NTFS_COLLATION_TYPE_SECURITY_HASH:
return &cmp_sdh;
case NTFS_COLLATION_TYPE_UINTS:
return &cmp_uints;
default:
break;
}
break;
default:
break;
}
return NULL;
}
struct bmp_buf {
struct ATTRIB *b;
struct mft_inode *mi;
struct buffer_head *bh;
ulong *buf;
size_t bit;
u32 nbits;
u64 new_valid;
};
static int bmp_buf_get(struct ntfs_index *indx, struct ntfs_inode *ni,
size_t bit, struct bmp_buf *bbuf)
{
struct ATTRIB *b;
size_t data_size, valid_size, vbo, off = bit >> 3;
struct ntfs_sb_info *sbi = ni->mi.sbi;
CLST vcn = off >> sbi->cluster_bits;
struct ATTR_LIST_ENTRY *le = NULL;
struct buffer_head *bh;
struct super_block *sb;
u32 blocksize;
const struct INDEX_NAMES *in = &s_index_names[indx->type];
bbuf->bh = NULL;
b = ni_find_attr(ni, NULL, &le, ATTR_BITMAP, in->name, in->name_len,
&vcn, &bbuf->mi);
bbuf->b = b;
if (!b)
return -EINVAL;
if (!b->non_res) {
data_size = le32_to_cpu(b->res.data_size);
if (off >= data_size)
return -EINVAL;
bbuf->buf = (ulong *)resident_data(b);
bbuf->bit = 0;
bbuf->nbits = data_size * 8;
return 0;
}
data_size = le64_to_cpu(b->nres.data_size);
if (WARN_ON(off >= data_size)) {
/* Looks like filesystem error. */
return -EINVAL;
}
valid_size = le64_to_cpu(b->nres.valid_size);
bh = ntfs_bread_run(sbi, &indx->bitmap_run, off);
if (!bh)
return -EIO;
if (IS_ERR(bh))
return PTR_ERR(bh);
bbuf->bh = bh;
if (buffer_locked(bh))
__wait_on_buffer(bh);
lock_buffer(bh);
sb = sbi->sb;
blocksize = sb->s_blocksize;
vbo = off & ~(size_t)sbi->block_mask;
bbuf->new_valid = vbo + blocksize;
if (bbuf->new_valid <= valid_size)
bbuf->new_valid = 0;
else if (bbuf->new_valid > data_size)
bbuf->new_valid = data_size;
if (vbo >= valid_size) {
memset(bh->b_data, 0, blocksize);
} else if (vbo + blocksize > valid_size) {
u32 voff = valid_size & sbi->block_mask;
memset(bh->b_data + voff, 0, blocksize - voff);
}
bbuf->buf = (ulong *)bh->b_data;
bbuf->bit = 8 * (off & ~(size_t)sbi->block_mask);
bbuf->nbits = 8 * blocksize;
return 0;
}
static void bmp_buf_put(struct bmp_buf *bbuf, bool dirty)
{
struct buffer_head *bh = bbuf->bh;
struct ATTRIB *b = bbuf->b;
if (!bh) {
if (b && !b->non_res && dirty)
bbuf->mi->dirty = true;
return;
}
if (!dirty)
goto out;
if (bbuf->new_valid) {
b->nres.valid_size = cpu_to_le64(bbuf->new_valid);
bbuf->mi->dirty = true;
}
set_buffer_uptodate(bh);
mark_buffer_dirty(bh);
out:
unlock_buffer(bh);
put_bh(bh);
}
/*
* indx_mark_used - Mark the bit @bit as used.
*/
static int indx_mark_used(struct ntfs_index *indx, struct ntfs_inode *ni,
size_t bit)
{
int err;
struct bmp_buf bbuf;
err = bmp_buf_get(indx, ni, bit, &bbuf);
if (err)
return err;
__set_bit(bit - bbuf.bit, bbuf.buf);
bmp_buf_put(&bbuf, true);
return 0;
}
/*
* indx_mark_free - Mark the bit @bit as free.
*/
static int indx_mark_free(struct ntfs_index *indx, struct ntfs_inode *ni,
size_t bit)
{
int err;
struct bmp_buf bbuf;
err = bmp_buf_get(indx, ni, bit, &bbuf);
if (err)
return err;
__clear_bit(bit - bbuf.bit, bbuf.buf);
bmp_buf_put(&bbuf, true);
return 0;
}
/*
* scan_nres_bitmap
*
* If ntfs_readdir calls this function (indx_used_bit -> scan_nres_bitmap),
* inode is shared locked and no ni_lock.
* Use rw_semaphore for read/write access to bitmap_run.
*/
static int scan_nres_bitmap(struct ntfs_inode *ni, struct ATTRIB *bitmap,
struct ntfs_index *indx, size_t from,
bool (*fn)(const ulong *buf, u32 bit, u32 bits,
size_t *ret),
size_t *ret)
{
struct ntfs_sb_info *sbi = ni->mi.sbi;
struct super_block *sb = sbi->sb;
struct runs_tree *run = &indx->bitmap_run;
struct rw_semaphore *lock = &indx->run_lock;
u32 nbits = sb->s_blocksize * 8;
u32 blocksize = sb->s_blocksize;
u64 valid_size = le64_to_cpu(bitmap->nres.valid_size);
u64 data_size = le64_to_cpu(bitmap->nres.data_size);
sector_t eblock = bytes_to_block(sb, data_size);
size_t vbo = from >> 3;
sector_t blk = (vbo & sbi->cluster_mask) >> sb->s_blocksize_bits;
sector_t vblock = vbo >> sb->s_blocksize_bits;
sector_t blen, block;
CLST lcn, clen, vcn, vcn_next;
size_t idx;
struct buffer_head *bh;
bool ok;
*ret = MINUS_ONE_T;
if (vblock >= eblock)
return 0;
from &= nbits - 1;
vcn = vbo >> sbi->cluster_bits;
down_read(lock);
ok = run_lookup_entry(run, vcn, &lcn, &clen, &idx);
up_read(lock);
next_run:
if (!ok) {
int err;
const struct INDEX_NAMES *name = &s_index_names[indx->type];
down_write(lock);
err = attr_load_runs_vcn(ni, ATTR_BITMAP, name->name,
name->name_len, run, vcn);
up_write(lock);
if (err)
return err;
down_read(lock);
ok = run_lookup_entry(run, vcn, &lcn, &clen, &idx);
up_read(lock);
if (!ok)
return -EINVAL;
}
blen = (sector_t)clen * sbi->blocks_per_cluster;
block = (sector_t)lcn * sbi->blocks_per_cluster;
for (; blk < blen; blk++, from = 0) {
bh = ntfs_bread(sb, block + blk);
if (!bh)
return -EIO;
vbo = (u64)vblock << sb->s_blocksize_bits;
if (vbo >= valid_size) {
memset(bh->b_data, 0, blocksize);
} else if (vbo + blocksize > valid_size) {
u32 voff = valid_size & sbi->block_mask;
memset(bh->b_data + voff, 0, blocksize - voff);
}
if (vbo + blocksize > data_size)
nbits = 8 * (data_size - vbo);
ok = nbits > from ? (*fn)((ulong *)bh->b_data, from, nbits, ret)
: false;
put_bh(bh);
if (ok) {
*ret += 8 * vbo;
return 0;
}
if (++vblock >= eblock) {
*ret = MINUS_ONE_T;
return 0;
}
}
blk = 0;
vcn_next = vcn + clen;
down_read(lock);
ok = run_get_entry(run, ++idx, &vcn, &lcn, &clen) && vcn == vcn_next;
if (!ok)
vcn = vcn_next;
up_read(lock);
goto next_run;
}
static bool scan_for_free(const ulong *buf, u32 bit, u32 bits, size_t *ret)
{
size_t pos = find_next_zero_bit(buf, bits, bit);
if (pos >= bits)
return false;
*ret = pos;
return true;
}
/*
* indx_find_free - Look for free bit.
*
* Return: -1 if no free bits.
*/
static int indx_find_free(struct ntfs_index *indx, struct ntfs_inode *ni,
size_t *bit, struct ATTRIB **bitmap)
{
struct ATTRIB *b;
struct ATTR_LIST_ENTRY *le = NULL;
const struct INDEX_NAMES *in = &s_index_names[indx->type];
int err;
b = ni_find_attr(ni, NULL, &le, ATTR_BITMAP, in->name, in->name_len,
NULL, NULL);
if (!b)
return -ENOENT;
*bitmap = b;
*bit = MINUS_ONE_T;
if (!b->non_res) {
u32 nbits = 8 * le32_to_cpu(b->res.data_size);
size_t pos = find_next_zero_bit(resident_data(b), nbits, 0);
if (pos < nbits)
*bit = pos;
} else {
err = scan_nres_bitmap(ni, b, indx, 0, &scan_for_free, bit);
if (err)
return err;
}
return 0;
}
static bool scan_for_used(const ulong *buf, u32 bit, u32 bits, size_t *ret)
{
size_t pos = find_next_bit(buf, bits, bit);
if (pos >= bits)
return false;
*ret = pos;
return true;
}
/*
* indx_used_bit - Look for used bit.
*
* Return: MINUS_ONE_T if no used bits.
*/
int indx_used_bit(struct ntfs_index *indx, struct ntfs_inode *ni, size_t *bit)
{
struct ATTRIB *b;
struct ATTR_LIST_ENTRY *le = NULL;
size_t from = *bit;
const struct INDEX_NAMES *in = &s_index_names[indx->type];
int err;
b = ni_find_attr(ni, NULL, &le, ATTR_BITMAP, in->name, in->name_len,
NULL, NULL);
if (!b)
return -ENOENT;
*bit = MINUS_ONE_T;
if (!b->non_res) {
u32 nbits = le32_to_cpu(b->res.data_size) * 8;
size_t pos = find_next_bit(resident_data(b), nbits, from);
if (pos < nbits)
*bit = pos;
} else {
err = scan_nres_bitmap(ni, b, indx, from, &scan_for_used, bit);
if (err)
return err;
}
return 0;
}
/*
* hdr_find_split
*
* Find a point at which the index allocation buffer would like to be split.
* NOTE: This function should never return 'END' entry NULL returns on error.
*/
static const struct NTFS_DE *hdr_find_split(const struct INDEX_HDR *hdr)
{
size_t o;
const struct NTFS_DE *e = hdr_first_de(hdr);
u32 used_2 = le32_to_cpu(hdr->used) >> 1;
u16 esize;
if (!e || de_is_last(e))
return NULL;
esize = le16_to_cpu(e->size);
for (o = le32_to_cpu(hdr->de_off) + esize; o < used_2; o += esize) {
const struct NTFS_DE *p = e;
e = Add2Ptr(hdr, o);
/* We must not return END entry. */
if (de_is_last(e))
return p;
esize = le16_to_cpu(e->size);
}
return e;
}
/*
* hdr_insert_head - Insert some entries at the beginning of the buffer.
*
* It is used to insert entries into a newly-created buffer.
*/
static const struct NTFS_DE *hdr_insert_head(struct INDEX_HDR *hdr,
const void *ins, u32 ins_bytes)
{
u32 to_move;
struct NTFS_DE *e = hdr_first_de(hdr);
u32 used = le32_to_cpu(hdr->used);
if (!e)
return NULL;
/* Now we just make room for the inserted entries and jam it in. */
to_move = used - le32_to_cpu(hdr->de_off);
memmove(Add2Ptr(e, ins_bytes), e, to_move);
memcpy(e, ins, ins_bytes);
hdr->used = cpu_to_le32(used + ins_bytes);
return e;
}
void fnd_clear(struct ntfs_fnd *fnd)
{
int i;
for (i = 0; i < fnd->level; i++) {
struct indx_node *n = fnd->nodes[i];
if (!n)
continue;
put_indx_node(n);
fnd->nodes[i] = NULL;
}
fnd->level = 0;
fnd->root_de = NULL;
}
static int fnd_push(struct ntfs_fnd *fnd, struct indx_node *n,
struct NTFS_DE *e)
{
int i;
i = fnd->level;
if (i < 0 || i >= ARRAY_SIZE(fnd->nodes))
return -EINVAL;
fnd->nodes[i] = n;
fnd->de[i] = e;
fnd->level += 1;
return 0;
}
static struct indx_node *fnd_pop(struct ntfs_fnd *fnd)
{
struct indx_node *n;
int i = fnd->level;
i -= 1;
n = fnd->nodes[i];
fnd->nodes[i] = NULL;
fnd->level = i;
return n;
}
static bool fnd_is_empty(struct ntfs_fnd *fnd)
{
if (!fnd->level)
return !fnd->root_de;
return !fnd->de[fnd->level - 1];
}
/*
* hdr_find_e - Locate an entry the index buffer.
*
* If no matching entry is found, it returns the first entry which is greater
* than the desired entry If the search key is greater than all the entries the
* buffer, it returns the 'end' entry. This function does a binary search of the
* current index buffer, for the first entry that is <= to the search value.
*
* Return: NULL if error.
*/
static struct NTFS_DE *hdr_find_e(const struct ntfs_index *indx,
const struct INDEX_HDR *hdr, const void *key,
size_t key_len, const void *ctx, int *diff)
{
struct NTFS_DE *e, *found = NULL;
NTFS_CMP_FUNC cmp = indx->cmp;
int min_idx = 0, mid_idx, max_idx = 0;
int diff2;
int table_size = 8;
u32 e_size, e_key_len;
u32 end = le32_to_cpu(hdr->used);
u32 off = le32_to_cpu(hdr->de_off);
u16 offs[128];
fill_table:
if (off + sizeof(struct NTFS_DE) > end)
return NULL;
e = Add2Ptr(hdr, off);
e_size = le16_to_cpu(e->size);
if (e_size < sizeof(struct NTFS_DE) || off + e_size > end)
return NULL;
if (!de_is_last(e)) {
offs[max_idx] = off;
off += e_size;
max_idx++;
if (max_idx < table_size)
goto fill_table;
max_idx--;
}
binary_search:
e_key_len = le16_to_cpu(e->key_size);
diff2 = (*cmp)(key, key_len, e + 1, e_key_len, ctx);
if (diff2 > 0) {
if (found) {
min_idx = mid_idx + 1;
} else {
if (de_is_last(e))
return NULL;
max_idx = 0;
table_size = min(table_size * 2,
(int)ARRAY_SIZE(offs));
goto fill_table;
}
} else if (diff2 < 0) {
if (found)
max_idx = mid_idx - 1;
else
max_idx--;
found = e;
} else {
*diff = 0;
return e;
}
if (min_idx > max_idx) {
*diff = -1;
return found;
}
mid_idx = (min_idx + max_idx) >> 1;
e = Add2Ptr(hdr, offs[mid_idx]);
goto binary_search;
}
/*
* hdr_insert_de - Insert an index entry into the buffer.
*
* 'before' should be a pointer previously returned from hdr_find_e.
*/
static struct NTFS_DE *hdr_insert_de(const struct ntfs_index *indx,
struct INDEX_HDR *hdr,
const struct NTFS_DE *de,
struct NTFS_DE *before, const void *ctx)
{
int diff;
size_t off = PtrOffset(hdr, before);
u32 used = le32_to_cpu(hdr->used);
u32 total = le32_to_cpu(hdr->total);
u16 de_size = le16_to_cpu(de->size);
/* First, check to see if there's enough room. */
if (used + de_size > total)
return NULL;
/* We know there's enough space, so we know we'll succeed. */
if (before) {
/* Check that before is inside Index. */
if (off >= used || off < le32_to_cpu(hdr->de_off) ||
off + le16_to_cpu(before->size) > total) {
return NULL;
}
goto ok;
}
/* No insert point is applied. Get it manually. */
before = hdr_find_e(indx, hdr, de + 1, le16_to_cpu(de->key_size), ctx,
&diff);
if (!before)
return NULL;
off = PtrOffset(hdr, before);
ok:
/* Now we just make room for the entry and jam it in. */
memmove(Add2Ptr(before, de_size), before, used - off);
hdr->used = cpu_to_le32(used + de_size);
memcpy(before, de, de_size);
return before;
}
/*
* hdr_delete_de - Remove an entry from the index buffer.
*/
static inline struct NTFS_DE *hdr_delete_de(struct INDEX_HDR *hdr,
struct NTFS_DE *re)
{
u32 used = le32_to_cpu(hdr->used);
u16 esize = le16_to_cpu(re->size);
u32 off = PtrOffset(hdr, re);
int bytes = used - (off + esize);
if (off >= used || esize < sizeof(struct NTFS_DE) ||
bytes < sizeof(struct NTFS_DE))
return NULL;
hdr->used = cpu_to_le32(used - esize);
memmove(re, Add2Ptr(re, esize), bytes);
return re;
}
void indx_clear(struct ntfs_index *indx)
{
run_close(&indx->alloc_run);
run_close(&indx->bitmap_run);
}
int indx_init(struct ntfs_index *indx, struct ntfs_sb_info *sbi,
const struct ATTRIB *attr, enum index_mutex_classed type)
{
u32 t32;
const struct INDEX_ROOT *root = resident_data(attr);
/* Check root fields. */
if (!root->index_block_clst)
return -EINVAL;
indx->type = type;
indx->idx2vbn_bits = __ffs(root->index_block_clst);
t32 = le32_to_cpu(root->index_block_size);
indx->index_bits = blksize_bits(t32);
/* Check index record size. */
if (t32 < sbi->cluster_size) {
/* Index record is smaller than a cluster, use 512 blocks. */
if (t32 != root->index_block_clst * SECTOR_SIZE)
return -EINVAL;
/* Check alignment to a cluster. */
if ((sbi->cluster_size >> SECTOR_SHIFT) &
(root->index_block_clst - 1)) {
return -EINVAL;
}
indx->vbn2vbo_bits = SECTOR_SHIFT;
} else {
/* Index record must be a multiple of cluster size. */
if (t32 != root->index_block_clst << sbi->cluster_bits)
return -EINVAL;
indx->vbn2vbo_bits = sbi->cluster_bits;
}
init_rwsem(&indx->run_lock);
indx->cmp = get_cmp_func(root);
return indx->cmp ? 0 : -EINVAL;
}
static struct indx_node *indx_new(struct ntfs_index *indx,
struct ntfs_inode *ni, CLST vbn,
const __le64 *sub_vbn)
{
int err;
struct NTFS_DE *e;
struct indx_node *r;
struct INDEX_HDR *hdr;
struct INDEX_BUFFER *index;
u64 vbo = (u64)vbn << indx->vbn2vbo_bits;
u32 bytes = 1u << indx->index_bits;
u16 fn;
u32 eo;
r = kzalloc(sizeof(struct indx_node), GFP_NOFS);
if (!r)
return ERR_PTR(-ENOMEM);
index = kzalloc(bytes, GFP_NOFS);
if (!index) {
kfree(r);
return ERR_PTR(-ENOMEM);
}
err = ntfs_get_bh(ni->mi.sbi, &indx->alloc_run, vbo, bytes, &r->nb);
if (err) {
kfree(index);
kfree(r);
return ERR_PTR(err);
}
/* Create header. */
index->rhdr.sign = NTFS_INDX_SIGNATURE;
index->rhdr.fix_off = cpu_to_le16(sizeof(struct INDEX_BUFFER)); // 0x28
fn = (bytes >> SECTOR_SHIFT) + 1; // 9
index->rhdr.fix_num = cpu_to_le16(fn);
index->vbn = cpu_to_le64(vbn);
hdr = &index->ihdr;
eo = ALIGN(sizeof(struct INDEX_BUFFER) + fn * sizeof(short), 8);
hdr->de_off = cpu_to_le32(eo);
e = Add2Ptr(hdr, eo);
if (sub_vbn) {
e->flags = NTFS_IE_LAST | NTFS_IE_HAS_SUBNODES;
e->size = cpu_to_le16(sizeof(struct NTFS_DE) + sizeof(u64));
hdr->used =
cpu_to_le32(eo + sizeof(struct NTFS_DE) + sizeof(u64));
de_set_vbn_le(e, *sub_vbn);
hdr->flags = 1;
} else {
e->size = cpu_to_le16(sizeof(struct NTFS_DE));
hdr->used = cpu_to_le32(eo + sizeof(struct NTFS_DE));
e->flags = NTFS_IE_LAST;
}
hdr->total = cpu_to_le32(bytes - offsetof(struct INDEX_BUFFER, ihdr));
r->index = index;
return r;
}
struct INDEX_ROOT *indx_get_root(struct ntfs_index *indx, struct ntfs_inode *ni,
struct ATTRIB **attr, struct mft_inode **mi)
{
struct ATTR_LIST_ENTRY *le = NULL;
struct ATTRIB *a;
const struct INDEX_NAMES *in = &s_index_names[indx->type];
a = ni_find_attr(ni, NULL, &le, ATTR_ROOT, in->name, in->name_len, NULL,
mi);
if (!a)
return NULL;
if (attr)
*attr = a;
return resident_data_ex(a, sizeof(struct INDEX_ROOT));
}
static int indx_write(struct ntfs_index *indx, struct ntfs_inode *ni,
struct indx_node *node, int sync)
{
struct INDEX_BUFFER *ib = node->index;
return ntfs_write_bh(ni->mi.sbi, &ib->rhdr, &node->nb, sync);
}
/*
* indx_read
*
* If ntfs_readdir calls this function
* inode is shared locked and no ni_lock.
* Use rw_semaphore for read/write access to alloc_run.
*/
int indx_read(struct ntfs_index *indx, struct ntfs_inode *ni, CLST vbn,
struct indx_node **node)
{
int err;
struct INDEX_BUFFER *ib;
struct runs_tree *run = &indx->alloc_run;
struct rw_semaphore *lock = &indx->run_lock;
u64 vbo = (u64)vbn << indx->vbn2vbo_bits;
u32 bytes = 1u << indx->index_bits;
struct indx_node *in = *node;
const struct INDEX_NAMES *name;
if (!in) {
in = kzalloc(sizeof(struct indx_node), GFP_NOFS);
if (!in)
return -ENOMEM;
} else {
nb_put(&in->nb);
}
ib = in->index;
if (!ib) {
ib = kmalloc(bytes, GFP_NOFS);
if (!ib) {
err = -ENOMEM;
goto out;
}
}
down_read(lock);
err = ntfs_read_bh(ni->mi.sbi, run, vbo, &ib->rhdr, bytes, &in->nb);
up_read(lock);
if (!err)
goto ok;
if (err == -E_NTFS_FIXUP)
goto ok;
if (err != -ENOENT)
goto out;
name = &s_index_names[indx->type];
down_write(lock);
err = attr_load_runs_range(ni, ATTR_ALLOC, name->name, name->name_len,
run, vbo, vbo + bytes);