]> git.proxmox.com Git - mirror_ubuntu-kernels.git/blame - fs/ntfs3/index.c
fs/ntfs3: Potential NULL dereference in hdr_find_split()
[mirror_ubuntu-kernels.git] / fs / ntfs3 / index.c
CommitLineData
82cae269
KK
1// SPDX-License-Identifier: GPL-2.0
2/*
3 *
4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
5 *
6 */
7
8#include <linux/blkdev.h>
9#include <linux/buffer_head.h>
10#include <linux/fs.h>
11#include <linux/nls.h>
12
13#include "debug.h"
14#include "ntfs.h"
15#include "ntfs_fs.h"
16
17static const struct INDEX_NAMES {
18 const __le16 *name;
19 u8 name_len;
20} s_index_names[INDEX_MUTEX_TOTAL] = {
21 { I30_NAME, ARRAY_SIZE(I30_NAME) }, { SII_NAME, ARRAY_SIZE(SII_NAME) },
22 { SDH_NAME, ARRAY_SIZE(SDH_NAME) }, { SO_NAME, ARRAY_SIZE(SO_NAME) },
23 { SQ_NAME, ARRAY_SIZE(SQ_NAME) }, { SR_NAME, ARRAY_SIZE(SR_NAME) },
24};
25
26/*
27 * compare two names in index
28 * if l1 != 0
29 * both names are little endian on-disk ATTR_FILE_NAME structs
30 * else
31 * key1 - cpu_str, key2 - ATTR_FILE_NAME
32 */
33static int cmp_fnames(const void *key1, size_t l1, const void *key2, size_t l2,
34 const void *data)
35{
36 const struct ATTR_FILE_NAME *f2 = key2;
37 const struct ntfs_sb_info *sbi = data;
38 const struct ATTR_FILE_NAME *f1;
39 u16 fsize2;
40 bool both_case;
41
42 if (l2 <= offsetof(struct ATTR_FILE_NAME, name))
43 return -1;
44
45 fsize2 = fname_full_size(f2);
46 if (l2 < fsize2)
47 return -1;
48
49 both_case = f2->type != FILE_NAME_DOS /*&& !sbi->options.nocase*/;
50 if (!l1) {
51 const struct le_str *s2 = (struct le_str *)&f2->name_len;
52
53 /*
54 * If names are equal (case insensitive)
55 * try to compare it case sensitive
56 */
57 return ntfs_cmp_names_cpu(key1, s2, sbi->upcase, both_case);
58 }
59
60 f1 = key1;
61 return ntfs_cmp_names(f1->name, f1->name_len, f2->name, f2->name_len,
62 sbi->upcase, both_case);
63}
64
65/* $SII of $Secure and $Q of Quota */
66static int cmp_uint(const void *key1, size_t l1, const void *key2, size_t l2,
67 const void *data)
68{
69 const u32 *k1 = key1;
70 const u32 *k2 = key2;
71
72 if (l2 < sizeof(u32))
73 return -1;
74
75 if (*k1 < *k2)
76 return -1;
77 if (*k1 > *k2)
78 return 1;
79 return 0;
80}
81
82/* $SDH of $Secure */
83static int cmp_sdh(const void *key1, size_t l1, const void *key2, size_t l2,
84 const void *data)
85{
86 const struct SECURITY_KEY *k1 = key1;
87 const struct SECURITY_KEY *k2 = key2;
88 u32 t1, t2;
89
90 if (l2 < sizeof(struct SECURITY_KEY))
91 return -1;
92
93 t1 = le32_to_cpu(k1->hash);
94 t2 = le32_to_cpu(k2->hash);
95
96 /* First value is a hash value itself */
97 if (t1 < t2)
98 return -1;
99 if (t1 > t2)
100 return 1;
101
102 /* Second value is security Id */
103 if (data) {
104 t1 = le32_to_cpu(k1->sec_id);
105 t2 = le32_to_cpu(k2->sec_id);
106 if (t1 < t2)
107 return -1;
108 if (t1 > t2)
109 return 1;
110 }
111
112 return 0;
113}
114
115/* $O of ObjId and "$R" for Reparse */
116static int cmp_uints(const void *key1, size_t l1, const void *key2, size_t l2,
117 const void *data)
118{
119 const __le32 *k1 = key1;
120 const __le32 *k2 = key2;
121 size_t count;
122
123 if ((size_t)data == 1) {
124 /*
125 * ni_delete_all -> ntfs_remove_reparse -> delete all with this reference
126 * k1, k2 - pointers to REPARSE_KEY
127 */
128
129 k1 += 1; // skip REPARSE_KEY.ReparseTag
130 k2 += 1; // skip REPARSE_KEY.ReparseTag
131 if (l2 <= sizeof(int))
132 return -1;
133 l2 -= sizeof(int);
134 if (l1 <= sizeof(int))
135 return 1;
136 l1 -= sizeof(int);
137 }
138
139 if (l2 < sizeof(int))
140 return -1;
141
142 for (count = min(l1, l2) >> 2; count > 0; --count, ++k1, ++k2) {
143 u32 t1 = le32_to_cpu(*k1);
144 u32 t2 = le32_to_cpu(*k2);
145
146 if (t1 > t2)
147 return 1;
148 if (t1 < t2)
149 return -1;
150 }
151
152 if (l1 > l2)
153 return 1;
154 if (l1 < l2)
155 return -1;
156
157 return 0;
158}
159
160static inline NTFS_CMP_FUNC get_cmp_func(const struct INDEX_ROOT *root)
161{
162 switch (root->type) {
163 case ATTR_NAME:
164 if (root->rule == NTFS_COLLATION_TYPE_FILENAME)
165 return &cmp_fnames;
166 break;
167 case ATTR_ZERO:
168 switch (root->rule) {
169 case NTFS_COLLATION_TYPE_UINT:
170 return &cmp_uint;
171 case NTFS_COLLATION_TYPE_SECURITY_HASH:
172 return &cmp_sdh;
173 case NTFS_COLLATION_TYPE_UINTS:
174 return &cmp_uints;
175 default:
176 break;
177 }
abfeb2ee 178 break;
82cae269
KK
179 default:
180 break;
181 }
182
183 return NULL;
184}
185
186struct bmp_buf {
187 struct ATTRIB *b;
188 struct mft_inode *mi;
189 struct buffer_head *bh;
190 ulong *buf;
191 size_t bit;
192 u32 nbits;
193 u64 new_valid;
194};
195
196static int bmp_buf_get(struct ntfs_index *indx, struct ntfs_inode *ni,
197 size_t bit, struct bmp_buf *bbuf)
198{
199 struct ATTRIB *b;
200 size_t data_size, valid_size, vbo, off = bit >> 3;
201 struct ntfs_sb_info *sbi = ni->mi.sbi;
202 CLST vcn = off >> sbi->cluster_bits;
203 struct ATTR_LIST_ENTRY *le = NULL;
204 struct buffer_head *bh;
205 struct super_block *sb;
206 u32 blocksize;
207 const struct INDEX_NAMES *in = &s_index_names[indx->type];
208
209 bbuf->bh = NULL;
210
211 b = ni_find_attr(ni, NULL, &le, ATTR_BITMAP, in->name, in->name_len,
212 &vcn, &bbuf->mi);
213 bbuf->b = b;
214 if (!b)
215 return -EINVAL;
216
217 if (!b->non_res) {
218 data_size = le32_to_cpu(b->res.data_size);
219
220 if (off >= data_size)
221 return -EINVAL;
222
223 bbuf->buf = (ulong *)resident_data(b);
224 bbuf->bit = 0;
225 bbuf->nbits = data_size * 8;
226
227 return 0;
228 }
229
230 data_size = le64_to_cpu(b->nres.data_size);
231 if (WARN_ON(off >= data_size)) {
232 /* looks like filesystem error */
233 return -EINVAL;
234 }
235
236 valid_size = le64_to_cpu(b->nres.valid_size);
237
238 bh = ntfs_bread_run(sbi, &indx->bitmap_run, off);
239 if (!bh)
240 return -EIO;
241
242 if (IS_ERR(bh))
243 return PTR_ERR(bh);
244
245 bbuf->bh = bh;
246
247 if (buffer_locked(bh))
248 __wait_on_buffer(bh);
249
250 lock_buffer(bh);
251
252 sb = sbi->sb;
253 blocksize = sb->s_blocksize;
254
255 vbo = off & ~(size_t)sbi->block_mask;
256
257 bbuf->new_valid = vbo + blocksize;
258 if (bbuf->new_valid <= valid_size)
259 bbuf->new_valid = 0;
260 else if (bbuf->new_valid > data_size)
261 bbuf->new_valid = data_size;
262
263 if (vbo >= valid_size) {
264 memset(bh->b_data, 0, blocksize);
265 } else if (vbo + blocksize > valid_size) {
266 u32 voff = valid_size & sbi->block_mask;
267
268 memset(bh->b_data + voff, 0, blocksize - voff);
269 }
270
271 bbuf->buf = (ulong *)bh->b_data;
272 bbuf->bit = 8 * (off & ~(size_t)sbi->block_mask);
273 bbuf->nbits = 8 * blocksize;
274
275 return 0;
276}
277
278static void bmp_buf_put(struct bmp_buf *bbuf, bool dirty)
279{
280 struct buffer_head *bh = bbuf->bh;
281 struct ATTRIB *b = bbuf->b;
282
283 if (!bh) {
284 if (b && !b->non_res && dirty)
285 bbuf->mi->dirty = true;
286 return;
287 }
288
289 if (!dirty)
290 goto out;
291
292 if (bbuf->new_valid) {
293 b->nres.valid_size = cpu_to_le64(bbuf->new_valid);
294 bbuf->mi->dirty = true;
295 }
296
297 set_buffer_uptodate(bh);
298 mark_buffer_dirty(bh);
299
300out:
301 unlock_buffer(bh);
302 put_bh(bh);
303}
304
305/*
306 * indx_mark_used
307 *
308 * marks the bit 'bit' as used
309 */
310static int indx_mark_used(struct ntfs_index *indx, struct ntfs_inode *ni,
311 size_t bit)
312{
313 int err;
314 struct bmp_buf bbuf;
315
316 err = bmp_buf_get(indx, ni, bit, &bbuf);
317 if (err)
318 return err;
319
320 __set_bit(bit - bbuf.bit, bbuf.buf);
321
322 bmp_buf_put(&bbuf, true);
323
324 return 0;
325}
326
327/*
328 * indx_mark_free
329 *
330 * the bit 'bit' as free
331 */
332static int indx_mark_free(struct ntfs_index *indx, struct ntfs_inode *ni,
333 size_t bit)
334{
335 int err;
336 struct bmp_buf bbuf;
337
338 err = bmp_buf_get(indx, ni, bit, &bbuf);
339 if (err)
340 return err;
341
342 __clear_bit(bit - bbuf.bit, bbuf.buf);
343
344 bmp_buf_put(&bbuf, true);
345
346 return 0;
347}
348
349/*
350 * if ntfs_readdir calls this function (indx_used_bit -> scan_nres_bitmap),
351 * inode is shared locked and no ni_lock
352 * use rw_semaphore for read/write access to bitmap_run
353 */
354static int scan_nres_bitmap(struct ntfs_inode *ni, struct ATTRIB *bitmap,
355 struct ntfs_index *indx, size_t from,
356 bool (*fn)(const ulong *buf, u32 bit, u32 bits,
357 size_t *ret),
358 size_t *ret)
359{
360 struct ntfs_sb_info *sbi = ni->mi.sbi;
361 struct super_block *sb = sbi->sb;
362 struct runs_tree *run = &indx->bitmap_run;
363 struct rw_semaphore *lock = &indx->run_lock;
364 u32 nbits = sb->s_blocksize * 8;
365 u32 blocksize = sb->s_blocksize;
366 u64 valid_size = le64_to_cpu(bitmap->nres.valid_size);
367 u64 data_size = le64_to_cpu(bitmap->nres.data_size);
368 sector_t eblock = bytes_to_block(sb, data_size);
369 size_t vbo = from >> 3;
370 sector_t blk = (vbo & sbi->cluster_mask) >> sb->s_blocksize_bits;
371 sector_t vblock = vbo >> sb->s_blocksize_bits;
372 sector_t blen, block;
373 CLST lcn, clen, vcn, vcn_next;
374 size_t idx;
375 struct buffer_head *bh;
376 bool ok;
377
378 *ret = MINUS_ONE_T;
379
380 if (vblock >= eblock)
381 return 0;
382
383 from &= nbits - 1;
384 vcn = vbo >> sbi->cluster_bits;
385
386 down_read(lock);
387 ok = run_lookup_entry(run, vcn, &lcn, &clen, &idx);
388 up_read(lock);
389
390next_run:
391 if (!ok) {
392 int err;
393 const struct INDEX_NAMES *name = &s_index_names[indx->type];
394
395 down_write(lock);
396 err = attr_load_runs_vcn(ni, ATTR_BITMAP, name->name,
397 name->name_len, run, vcn);
398 up_write(lock);
399 if (err)
400 return err;
401 down_read(lock);
402 ok = run_lookup_entry(run, vcn, &lcn, &clen, &idx);
403 up_read(lock);
404 if (!ok)
405 return -EINVAL;
406 }
407
408 blen = (sector_t)clen * sbi->blocks_per_cluster;
409 block = (sector_t)lcn * sbi->blocks_per_cluster;
410
411 for (; blk < blen; blk++, from = 0) {
412 bh = ntfs_bread(sb, block + blk);
413 if (!bh)
414 return -EIO;
415
416 vbo = (u64)vblock << sb->s_blocksize_bits;
417 if (vbo >= valid_size) {
418 memset(bh->b_data, 0, blocksize);
419 } else if (vbo + blocksize > valid_size) {
420 u32 voff = valid_size & sbi->block_mask;
421
422 memset(bh->b_data + voff, 0, blocksize - voff);
423 }
424
425 if (vbo + blocksize > data_size)
426 nbits = 8 * (data_size - vbo);
427
428 ok = nbits > from ? (*fn)((ulong *)bh->b_data, from, nbits, ret)
429 : false;
430 put_bh(bh);
431
432 if (ok) {
433 *ret += 8 * vbo;
434 return 0;
435 }
436
437 if (++vblock >= eblock) {
438 *ret = MINUS_ONE_T;
439 return 0;
440 }
441 }
442 blk = 0;
443 vcn_next = vcn + clen;
444 down_read(lock);
445 ok = run_get_entry(run, ++idx, &vcn, &lcn, &clen) && vcn == vcn_next;
446 if (!ok)
447 vcn = vcn_next;
448 up_read(lock);
449 goto next_run;
450}
451
452static bool scan_for_free(const ulong *buf, u32 bit, u32 bits, size_t *ret)
453{
454 size_t pos = find_next_zero_bit(buf, bits, bit);
455
456 if (pos >= bits)
457 return false;
458 *ret = pos;
459 return true;
460}
461
462/*
463 * indx_find_free
464 *
465 * looks for free bit
466 * returns -1 if no free bits
467 */
468static int indx_find_free(struct ntfs_index *indx, struct ntfs_inode *ni,
469 size_t *bit, struct ATTRIB **bitmap)
470{
471 struct ATTRIB *b;
472 struct ATTR_LIST_ENTRY *le = NULL;
473 const struct INDEX_NAMES *in = &s_index_names[indx->type];
474 int err;
475
476 b = ni_find_attr(ni, NULL, &le, ATTR_BITMAP, in->name, in->name_len,
477 NULL, NULL);
478
479 if (!b)
480 return -ENOENT;
481
482 *bitmap = b;
483 *bit = MINUS_ONE_T;
484
485 if (!b->non_res) {
486 u32 nbits = 8 * le32_to_cpu(b->res.data_size);
487 size_t pos = find_next_zero_bit(resident_data(b), nbits, 0);
488
489 if (pos < nbits)
490 *bit = pos;
491 } else {
492 err = scan_nres_bitmap(ni, b, indx, 0, &scan_for_free, bit);
493
494 if (err)
495 return err;
496 }
497
498 return 0;
499}
500
501static bool scan_for_used(const ulong *buf, u32 bit, u32 bits, size_t *ret)
502{
503 size_t pos = find_next_bit(buf, bits, bit);
504
505 if (pos >= bits)
506 return false;
507 *ret = pos;
508 return true;
509}
510
511/*
512 * indx_used_bit
513 *
514 * looks for used bit
515 * returns MINUS_ONE_T if no used bits
516 */
517int indx_used_bit(struct ntfs_index *indx, struct ntfs_inode *ni, size_t *bit)
518{
519 struct ATTRIB *b;
520 struct ATTR_LIST_ENTRY *le = NULL;
521 size_t from = *bit;
522 const struct INDEX_NAMES *in = &s_index_names[indx->type];
523 int err;
524
525 b = ni_find_attr(ni, NULL, &le, ATTR_BITMAP, in->name, in->name_len,
526 NULL, NULL);
527
528 if (!b)
529 return -ENOENT;
530
531 *bit = MINUS_ONE_T;
532
533 if (!b->non_res) {
534 u32 nbits = le32_to_cpu(b->res.data_size) * 8;
535 size_t pos = find_next_bit(resident_data(b), nbits, from);
536
537 if (pos < nbits)
538 *bit = pos;
539 } else {
540 err = scan_nres_bitmap(ni, b, indx, from, &scan_for_used, bit);
541 if (err)
542 return err;
543 }
544
545 return 0;
546}
547
548/*
549 * hdr_find_split
550 *
551 * finds a point at which the index allocation buffer would like to
552 * be split.
553 * NOTE: This function should never return 'END' entry NULL returns on error
554 */
555static const struct NTFS_DE *hdr_find_split(const struct INDEX_HDR *hdr)
556{
557 size_t o;
558 const struct NTFS_DE *e = hdr_first_de(hdr);
559 u32 used_2 = le32_to_cpu(hdr->used) >> 1;
8c83a485 560 u16 esize;
82cae269
KK
561
562 if (!e || de_is_last(e))
563 return NULL;
564
8c83a485 565 esize = le16_to_cpu(e->size);
82cae269
KK
566 for (o = le32_to_cpu(hdr->de_off) + esize; o < used_2; o += esize) {
567 const struct NTFS_DE *p = e;
568
569 e = Add2Ptr(hdr, o);
570
571 /* We must not return END entry */
572 if (de_is_last(e))
573 return p;
574
575 esize = le16_to_cpu(e->size);
576 }
577
578 return e;
579}
580
581/*
582 * hdr_insert_head
583 *
584 * inserts some entries at the beginning of the buffer.
585 * It is used to insert entries into a newly-created buffer.
586 */
587static const struct NTFS_DE *hdr_insert_head(struct INDEX_HDR *hdr,
588 const void *ins, u32 ins_bytes)
589{
590 u32 to_move;
591 struct NTFS_DE *e = hdr_first_de(hdr);
592 u32 used = le32_to_cpu(hdr->used);
593
594 if (!e)
595 return NULL;
596
597 /* Now we just make room for the inserted entries and jam it in. */
598 to_move = used - le32_to_cpu(hdr->de_off);
599 memmove(Add2Ptr(e, ins_bytes), e, to_move);
600 memcpy(e, ins, ins_bytes);
601 hdr->used = cpu_to_le32(used + ins_bytes);
602
603 return e;
604}
605
606void fnd_clear(struct ntfs_fnd *fnd)
607{
608 int i;
609
610 for (i = 0; i < fnd->level; i++) {
611 struct indx_node *n = fnd->nodes[i];
612
613 if (!n)
614 continue;
615
616 put_indx_node(n);
617 fnd->nodes[i] = NULL;
618 }
619 fnd->level = 0;
620 fnd->root_de = NULL;
621}
622
623static int fnd_push(struct ntfs_fnd *fnd, struct indx_node *n,
624 struct NTFS_DE *e)
625{
626 int i;
627
628 i = fnd->level;
629 if (i < 0 || i >= ARRAY_SIZE(fnd->nodes))
630 return -EINVAL;
631 fnd->nodes[i] = n;
632 fnd->de[i] = e;
633 fnd->level += 1;
634 return 0;
635}
636
637static struct indx_node *fnd_pop(struct ntfs_fnd *fnd)
638{
639 struct indx_node *n;
640 int i = fnd->level;
641
642 i -= 1;
643 n = fnd->nodes[i];
644 fnd->nodes[i] = NULL;
645 fnd->level = i;
646
647 return n;
648}
649
650static bool fnd_is_empty(struct ntfs_fnd *fnd)
651{
652 if (!fnd->level)
653 return !fnd->root_de;
654
655 return !fnd->de[fnd->level - 1];
656}
657
658/*
659 * hdr_find_e
660 *
661 * locates an entry the index buffer.
662 * If no matching entry is found, it returns the first entry which is greater
663 * than the desired entry If the search key is greater than all the entries the
664 * buffer, it returns the 'end' entry. This function does a binary search of the
665 * current index buffer, for the first entry that is <= to the search value
666 * Returns NULL if error
667 */
668static struct NTFS_DE *hdr_find_e(const struct ntfs_index *indx,
669 const struct INDEX_HDR *hdr, const void *key,
670 size_t key_len, const void *ctx, int *diff)
671{
672 struct NTFS_DE *e;
673 NTFS_CMP_FUNC cmp = indx->cmp;
674 u32 e_size, e_key_len;
675 u32 end = le32_to_cpu(hdr->used);
676 u32 off = le32_to_cpu(hdr->de_off);
677
678#ifdef NTFS3_INDEX_BINARY_SEARCH
679 int max_idx = 0, fnd, min_idx;
680 int nslots = 64;
681 u16 *offs;
682
683 if (end > 0x10000)
684 goto next;
685
195c52bd 686 offs = kmalloc(sizeof(u16) * nslots, GFP_NOFS);
82cae269
KK
687 if (!offs)
688 goto next;
689
690 /* use binary search algorithm */
691next1:
692 if (off + sizeof(struct NTFS_DE) > end) {
693 e = NULL;
694 goto out1;
695 }
696 e = Add2Ptr(hdr, off);
697 e_size = le16_to_cpu(e->size);
698
699 if (e_size < sizeof(struct NTFS_DE) || off + e_size > end) {
700 e = NULL;
701 goto out1;
702 }
703
704 if (max_idx >= nslots) {
705 u16 *ptr;
fa3cacf5 706 int new_slots = ALIGN(2 * nslots, 8);
82cae269 707
195c52bd 708 ptr = kmalloc(sizeof(u16) * new_slots, GFP_NOFS);
82cae269
KK
709 if (ptr)
710 memcpy(ptr, offs, sizeof(u16) * max_idx);
195c52bd 711 kfree(offs);
82cae269
KK
712 offs = ptr;
713 nslots = new_slots;
714 if (!ptr)
715 goto next;
716 }
717
718 /* Store entry table */
719 offs[max_idx] = off;
720
721 if (!de_is_last(e)) {
722 off += e_size;
723 max_idx += 1;
724 goto next1;
725 }
726
727 /*
728 * Table of pointers is created
729 * Use binary search to find entry that is <= to the search value
730 */
731 fnd = -1;
732 min_idx = 0;
733
734 while (min_idx <= max_idx) {
735 int mid_idx = min_idx + ((max_idx - min_idx) >> 1);
736 int diff2;
737
738 e = Add2Ptr(hdr, offs[mid_idx]);
739
740 e_key_len = le16_to_cpu(e->key_size);
741
742 diff2 = (*cmp)(key, key_len, e + 1, e_key_len, ctx);
743
744 if (!diff2) {
745 *diff = 0;
746 goto out1;
747 }
748
749 if (diff2 < 0) {
750 max_idx = mid_idx - 1;
751 fnd = mid_idx;
752 if (!fnd)
753 break;
754 } else {
755 min_idx = mid_idx + 1;
756 }
757 }
758
759 if (fnd == -1) {
760 e = NULL;
761 goto out1;
762 }
763
764 *diff = -1;
765 e = Add2Ptr(hdr, offs[fnd]);
766
767out1:
195c52bd 768 kfree(offs);
82cae269
KK
769
770 return e;
771#endif
772
773next:
774 /*
775 * Entries index are sorted
776 * Enumerate all entries until we find entry that is <= to the search value
777 */
778 if (off + sizeof(struct NTFS_DE) > end)
779 return NULL;
780
781 e = Add2Ptr(hdr, off);
782 e_size = le16_to_cpu(e->size);
783
784 if (e_size < sizeof(struct NTFS_DE) || off + e_size > end)
785 return NULL;
786
787 off += e_size;
788
789 e_key_len = le16_to_cpu(e->key_size);
790
791 *diff = (*cmp)(key, key_len, e + 1, e_key_len, ctx);
792 if (!*diff)
793 return e;
794
795 if (*diff <= 0)
796 return e;
797
798 if (de_is_last(e)) {
799 *diff = 1;
800 return e;
801 }
802 goto next;
803}
804
805/*
806 * hdr_insert_de
807 *
808 * inserts an index entry into the buffer.
809 * 'before' should be a pointer previously returned from hdr_find_e
810 */
811static struct NTFS_DE *hdr_insert_de(const struct ntfs_index *indx,
812 struct INDEX_HDR *hdr,
813 const struct NTFS_DE *de,
814 struct NTFS_DE *before, const void *ctx)
815{
816 int diff;
817 size_t off = PtrOffset(hdr, before);
818 u32 used = le32_to_cpu(hdr->used);
819 u32 total = le32_to_cpu(hdr->total);
820 u16 de_size = le16_to_cpu(de->size);
821
822 /* First, check to see if there's enough room */
823 if (used + de_size > total)
824 return NULL;
825
826 /* We know there's enough space, so we know we'll succeed. */
827 if (before) {
828 /* Check that before is inside Index */
829 if (off >= used || off < le32_to_cpu(hdr->de_off) ||
830 off + le16_to_cpu(before->size) > total) {
831 return NULL;
832 }
833 goto ok;
834 }
835 /* No insert point is applied. Get it manually */
836 before = hdr_find_e(indx, hdr, de + 1, le16_to_cpu(de->key_size), ctx,
837 &diff);
838 if (!before)
839 return NULL;
840 off = PtrOffset(hdr, before);
841
842ok:
843 /* Now we just make room for the entry and jam it in. */
844 memmove(Add2Ptr(before, de_size), before, used - off);
845
846 hdr->used = cpu_to_le32(used + de_size);
847 memcpy(before, de, de_size);
848
849 return before;
850}
851
852/*
853 * hdr_delete_de
854 *
855 * removes an entry from the index buffer
856 */
857static inline struct NTFS_DE *hdr_delete_de(struct INDEX_HDR *hdr,
858 struct NTFS_DE *re)
859{
860 u32 used = le32_to_cpu(hdr->used);
861 u16 esize = le16_to_cpu(re->size);
862 u32 off = PtrOffset(hdr, re);
863 int bytes = used - (off + esize);
864
865 if (off >= used || esize < sizeof(struct NTFS_DE) ||
866 bytes < sizeof(struct NTFS_DE))
867 return NULL;
868
869 hdr->used = cpu_to_le32(used - esize);
870 memmove(re, Add2Ptr(re, esize), bytes);
871
872 return re;
873}
874
875void indx_clear(struct ntfs_index *indx)
876{
877 run_close(&indx->alloc_run);
878 run_close(&indx->bitmap_run);
879}
880
881int indx_init(struct ntfs_index *indx, struct ntfs_sb_info *sbi,
882 const struct ATTRIB *attr, enum index_mutex_classed type)
883{
884 u32 t32;
885 const struct INDEX_ROOT *root = resident_data(attr);
886
887 /* Check root fields */
888 if (!root->index_block_clst)
889 return -EINVAL;
890
891 indx->type = type;
892 indx->idx2vbn_bits = __ffs(root->index_block_clst);
893
894 t32 = le32_to_cpu(root->index_block_size);
895 indx->index_bits = blksize_bits(t32);
896
897 /* Check index record size */
898 if (t32 < sbi->cluster_size) {
899 /* index record is smaller than a cluster, use 512 blocks */
900 if (t32 != root->index_block_clst * SECTOR_SIZE)
901 return -EINVAL;
902
903 /* Check alignment to a cluster */
904 if ((sbi->cluster_size >> SECTOR_SHIFT) &
905 (root->index_block_clst - 1)) {
906 return -EINVAL;
907 }
908
909 indx->vbn2vbo_bits = SECTOR_SHIFT;
910 } else {
911 /* index record must be a multiple of cluster size */
912 if (t32 != root->index_block_clst << sbi->cluster_bits)
913 return -EINVAL;
914
915 indx->vbn2vbo_bits = sbi->cluster_bits;
916 }
917
918 init_rwsem(&indx->run_lock);
919
920 indx->cmp = get_cmp_func(root);
921 return indx->cmp ? 0 : -EINVAL;
922}
923
924static struct indx_node *indx_new(struct ntfs_index *indx,
925 struct ntfs_inode *ni, CLST vbn,
926 const __le64 *sub_vbn)
927{
928 int err;
929 struct NTFS_DE *e;
930 struct indx_node *r;
931 struct INDEX_HDR *hdr;
932 struct INDEX_BUFFER *index;
933 u64 vbo = (u64)vbn << indx->vbn2vbo_bits;
934 u32 bytes = 1u << indx->index_bits;
935 u16 fn;
936 u32 eo;
937
195c52bd 938 r = kzalloc(sizeof(struct indx_node), GFP_NOFS);
82cae269
KK
939 if (!r)
940 return ERR_PTR(-ENOMEM);
941
195c52bd 942 index = kzalloc(bytes, GFP_NOFS);
82cae269 943 if (!index) {
195c52bd 944 kfree(r);
82cae269
KK
945 return ERR_PTR(-ENOMEM);
946 }
947
948 err = ntfs_get_bh(ni->mi.sbi, &indx->alloc_run, vbo, bytes, &r->nb);
949
950 if (err) {
195c52bd
KA
951 kfree(index);
952 kfree(r);
82cae269
KK
953 return ERR_PTR(err);
954 }
955
956 /* Create header */
957 index->rhdr.sign = NTFS_INDX_SIGNATURE;
958 index->rhdr.fix_off = cpu_to_le16(sizeof(struct INDEX_BUFFER)); // 0x28
959 fn = (bytes >> SECTOR_SHIFT) + 1; // 9
960 index->rhdr.fix_num = cpu_to_le16(fn);
961 index->vbn = cpu_to_le64(vbn);
962 hdr = &index->ihdr;
fa3cacf5 963 eo = ALIGN(sizeof(struct INDEX_BUFFER) + fn * sizeof(short), 8);
82cae269
KK
964 hdr->de_off = cpu_to_le32(eo);
965
966 e = Add2Ptr(hdr, eo);
967
968 if (sub_vbn) {
969 e->flags = NTFS_IE_LAST | NTFS_IE_HAS_SUBNODES;
970 e->size = cpu_to_le16(sizeof(struct NTFS_DE) + sizeof(u64));
971 hdr->used =
972 cpu_to_le32(eo + sizeof(struct NTFS_DE) + sizeof(u64));
973 de_set_vbn_le(e, *sub_vbn);
974 hdr->flags = 1;
975 } else {
976 e->size = cpu_to_le16(sizeof(struct NTFS_DE));
977 hdr->used = cpu_to_le32(eo + sizeof(struct NTFS_DE));
978 e->flags = NTFS_IE_LAST;
979 }
980
981 hdr->total = cpu_to_le32(bytes - offsetof(struct INDEX_BUFFER, ihdr));
982
983 r->index = index;
984 return r;
985}
986
987struct INDEX_ROOT *indx_get_root(struct ntfs_index *indx, struct ntfs_inode *ni,
988 struct ATTRIB **attr, struct mft_inode **mi)
989{
990 struct ATTR_LIST_ENTRY *le = NULL;
991 struct ATTRIB *a;
992 const struct INDEX_NAMES *in = &s_index_names[indx->type];
993
994 a = ni_find_attr(ni, NULL, &le, ATTR_ROOT, in->name, in->name_len, NULL,
995 mi);
996 if (!a)
997 return NULL;
998
999 if (attr)
1000 *attr = a;
1001
1002 return resident_data_ex(a, sizeof(struct INDEX_ROOT));
1003}
1004
1005static int indx_write(struct ntfs_index *indx, struct ntfs_inode *ni,
1006 struct indx_node *node, int sync)
1007{
1008 struct INDEX_BUFFER *ib = node->index;
1009
1010 return ntfs_write_bh(ni->mi.sbi, &ib->rhdr, &node->nb, sync);
1011}
1012
1013/*
1014 * if ntfs_readdir calls this function
1015 * inode is shared locked and no ni_lock
1016 * use rw_semaphore for read/write access to alloc_run
1017 */
1018int indx_read(struct ntfs_index *indx, struct ntfs_inode *ni, CLST vbn,
1019 struct indx_node **node)
1020{
1021 int err;
1022 struct INDEX_BUFFER *ib;
1023 struct runs_tree *run = &indx->alloc_run;
1024 struct rw_semaphore *lock = &indx->run_lock;
1025 u64 vbo = (u64)vbn << indx->vbn2vbo_bits;
1026 u32 bytes = 1u << indx->index_bits;
1027 struct indx_node *in = *node;
1028 const struct INDEX_NAMES *name;
1029
1030 if (!in) {
195c52bd 1031 in = kzalloc(sizeof(struct indx_node), GFP_NOFS);
82cae269
KK
1032 if (!in)
1033 return -ENOMEM;
1034 } else {
1035 nb_put(&in->nb);
1036 }
1037
1038 ib = in->index;
1039 if (!ib) {
195c52bd 1040 ib = kmalloc(bytes, GFP_NOFS);
82cae269
KK
1041 if (!ib) {
1042 err = -ENOMEM;
1043 goto out;
1044 }
1045 }
1046
1047 down_read(lock);
1048 err = ntfs_read_bh(ni->mi.sbi, run, vbo, &ib->rhdr, bytes, &in->nb);
1049 up_read(lock);
1050 if (!err)
1051 goto ok;
1052
1053 if (err == -E_NTFS_FIXUP)
1054 goto ok;
1055
1056 if (err != -ENOENT)
1057 goto out;
1058
1059 name = &s_index_names[indx->type];
1060 down_write(lock);
1061 err = attr_load_runs_range(ni, ATTR_ALLOC, name->name, name->name_len,
1062 run, vbo, vbo + bytes);
1063 up_write(lock);
1064 if (err)
1065 goto out;
1066
1067 down_read(lock);
1068 err = ntfs_read_bh(ni->mi.sbi, run, vbo, &ib->rhdr, bytes, &in->nb);
1069 up_read(lock);
1070 if (err == -E_NTFS_FIXUP)
1071 goto ok;
1072
1073 if (err)
1074 goto out;
1075
1076ok:
1077 if (err == -E_NTFS_FIXUP) {
1078 ntfs_write_bh(ni->mi.sbi, &ib->rhdr, &in->nb, 0);
1079 err = 0;
1080 }
1081
1082 in->index = ib;
1083 *node = in;
1084
1085out:
1086 if (ib != in->index)
195c52bd 1087 kfree(ib);
82cae269
KK
1088
1089 if (*node != in) {
1090 nb_put(&in->nb);
195c52bd 1091 kfree(in);
82cae269
KK
1092 }
1093
1094 return err;
1095}
1096
1097/*
1098 * indx_find
1099 *
1100 * scans NTFS directory for given entry
1101 */
1102int indx_find(struct ntfs_index *indx, struct ntfs_inode *ni,
1103 const struct INDEX_ROOT *root, const void *key, size_t key_len,
1104 const void *ctx, int *diff, struct NTFS_DE **entry,
1105 struct ntfs_fnd *fnd)
1106{
1107 int err;
1108 struct NTFS_DE *e;
1109 const struct INDEX_HDR *hdr;
1110 struct indx_node *node;
1111
1112 if (!root)
1113 root = indx_get_root(&ni->dir, ni, NULL, NULL);
1114
1115 if (!root) {
1116 err = -EINVAL;
1117 goto out;
1118 }
1119
1120 hdr = &root->ihdr;
1121
1122 /* Check cache */
1123 e = fnd->level ? fnd->de[fnd->level - 1] : fnd->root_de;
1124 if (e && !de_is_last(e) &&
1125 !(*indx->cmp)(key, key_len, e + 1, le16_to_cpu(e->key_size), ctx)) {
1126 *entry = e;
1127 *diff = 0;
1128 return 0;
1129 }
1130
1131 /* Soft finder reset */
1132 fnd_clear(fnd);
1133
1134 /* Lookup entry that is <= to the search value */
1135 e = hdr_find_e(indx, hdr, key, key_len, ctx, diff);
1136 if (!e)
1137 return -EINVAL;
1138
1139 if (fnd)
1140 fnd->root_de = e;
1141
1142 err = 0;
1143
1144 for (;;) {
1145 node = NULL;
1146 if (*diff >= 0 || !de_has_vcn_ex(e)) {
1147 *entry = e;
1148 goto out;
1149 }
1150
1151 /* Read next level. */
1152 err = indx_read(indx, ni, de_get_vbn(e), &node);
1153 if (err)
1154 goto out;
1155
1156 /* Lookup entry that is <= to the search value */
1157 e = hdr_find_e(indx, &node->index->ihdr, key, key_len, ctx,
1158 diff);
1159 if (!e) {
1160 err = -EINVAL;
1161 put_indx_node(node);
1162 goto out;
1163 }
1164
1165 fnd_push(fnd, node, e);
1166 }
1167
1168out:
1169 return err;
1170}
1171
1172int indx_find_sort(struct ntfs_index *indx, struct ntfs_inode *ni,
1173 const struct INDEX_ROOT *root, struct NTFS_DE **entry,
1174 struct ntfs_fnd *fnd)
1175{
1176 int err;
1177 struct indx_node *n = NULL;
1178 struct NTFS_DE *e;
1179 size_t iter = 0;
1180 int level = fnd->level;
1181
1182 if (!*entry) {
1183 /* Start find */
1184 e = hdr_first_de(&root->ihdr);
1185 if (!e)
1186 return 0;
1187 fnd_clear(fnd);
1188 fnd->root_de = e;
1189 } else if (!level) {
1190 if (de_is_last(fnd->root_de)) {
1191 *entry = NULL;
1192 return 0;
1193 }
1194
1195 e = hdr_next_de(&root->ihdr, fnd->root_de);
1196 if (!e)
1197 return -EINVAL;
1198 fnd->root_de = e;
1199 } else {
1200 n = fnd->nodes[level - 1];
1201 e = fnd->de[level - 1];
1202
1203 if (de_is_last(e))
1204 goto pop_level;
1205
1206 e = hdr_next_de(&n->index->ihdr, e);
1207 if (!e)
1208 return -EINVAL;
1209
1210 fnd->de[level - 1] = e;
1211 }
1212
1213 /* Just to avoid tree cycle */
1214next_iter:
1215 if (iter++ >= 1000)
1216 return -EINVAL;
1217
1218 while (de_has_vcn_ex(e)) {
1219 if (le16_to_cpu(e->size) <
1220 sizeof(struct NTFS_DE) + sizeof(u64)) {
1221 if (n) {
1222 fnd_pop(fnd);
195c52bd 1223 kfree(n);
82cae269
KK
1224 }
1225 return -EINVAL;
1226 }
1227
1228 /* Read next level */
1229 err = indx_read(indx, ni, de_get_vbn(e), &n);
1230 if (err)
1231 return err;
1232
1233 /* Try next level */
1234 e = hdr_first_de(&n->index->ihdr);
1235 if (!e) {
195c52bd 1236 kfree(n);
82cae269
KK
1237 return -EINVAL;
1238 }
1239
1240 fnd_push(fnd, n, e);
1241 }
1242
1243 if (le16_to_cpu(e->size) > sizeof(struct NTFS_DE)) {
1244 *entry = e;
1245 return 0;
1246 }
1247
1248pop_level:
1249 for (;;) {
1250 if (!de_is_last(e))
1251 goto next_iter;
1252
1253 /* Pop one level */
1254 if (n) {
1255 fnd_pop(fnd);
195c52bd 1256 kfree(n);
82cae269
KK
1257 }
1258
1259 level = fnd->level;
1260
1261 if (level) {
1262 n = fnd->nodes[level - 1];
1263 e = fnd->de[level - 1];
1264 } else if (fnd->root_de) {
1265 n = NULL;
1266 e = fnd->root_de;
1267 fnd->root_de = NULL;
1268 } else {
1269 *entry = NULL;
1270 return 0;
1271 }
1272
1273 if (le16_to_cpu(e->size) > sizeof(struct NTFS_DE)) {
1274 *entry = e;
1275 if (!fnd->root_de)
1276 fnd->root_de = e;
1277 return 0;
1278 }
1279 }
1280}
1281
1282int indx_find_raw(struct ntfs_index *indx, struct ntfs_inode *ni,
1283 const struct INDEX_ROOT *root, struct NTFS_DE **entry,
1284 size_t *off, struct ntfs_fnd *fnd)
1285{
1286 int err;
1287 struct indx_node *n = NULL;
1288 struct NTFS_DE *e = NULL;
1289 struct NTFS_DE *e2;
1290 size_t bit;
1291 CLST next_used_vbn;
1292 CLST next_vbn;
1293 u32 record_size = ni->mi.sbi->record_size;
1294
1295 /* Use non sorted algorithm */
1296 if (!*entry) {
1297 /* This is the first call */
1298 e = hdr_first_de(&root->ihdr);
1299 if (!e)
1300 return 0;
1301 fnd_clear(fnd);
1302 fnd->root_de = e;
1303
1304 /* The first call with setup of initial element */
1305 if (*off >= record_size) {
1306 next_vbn = (((*off - record_size) >> indx->index_bits))
1307 << indx->idx2vbn_bits;
1308 /* jump inside cycle 'for'*/
1309 goto next;
1310 }
1311
1312 /* Start enumeration from root */
1313 *off = 0;
1314 } else if (!fnd->root_de)
1315 return -EINVAL;
1316
1317 for (;;) {
1318 /* Check if current entry can be used */
1319 if (e && le16_to_cpu(e->size) > sizeof(struct NTFS_DE))
1320 goto ok;
1321
1322 if (!fnd->level) {
1323 /* Continue to enumerate root */
1324 if (!de_is_last(fnd->root_de)) {
1325 e = hdr_next_de(&root->ihdr, fnd->root_de);
1326 if (!e)
1327 return -EINVAL;
1328 fnd->root_de = e;
1329 continue;
1330 }
1331
1332 /* Start to enumerate indexes from 0 */
1333 next_vbn = 0;
1334 } else {
1335 /* Continue to enumerate indexes */
1336 e2 = fnd->de[fnd->level - 1];
1337
1338 n = fnd->nodes[fnd->level - 1];
1339
1340 if (!de_is_last(e2)) {
1341 e = hdr_next_de(&n->index->ihdr, e2);
1342 if (!e)
1343 return -EINVAL;
1344 fnd->de[fnd->level - 1] = e;
1345 continue;
1346 }
1347
1348 /* Continue with next index */
1349 next_vbn = le64_to_cpu(n->index->vbn) +
1350 root->index_block_clst;
1351 }
1352
1353next:
1354 /* Release current index */
1355 if (n) {
1356 fnd_pop(fnd);
1357 put_indx_node(n);
1358 n = NULL;
1359 }
1360
1361 /* Skip all free indexes */
1362 bit = next_vbn >> indx->idx2vbn_bits;
1363 err = indx_used_bit(indx, ni, &bit);
1364 if (err == -ENOENT || bit == MINUS_ONE_T) {
1365 /* No used indexes */
1366 *entry = NULL;
1367 return 0;
1368 }
1369
1370 next_used_vbn = bit << indx->idx2vbn_bits;
1371
1372 /* Read buffer into memory */
1373 err = indx_read(indx, ni, next_used_vbn, &n);
1374 if (err)
1375 return err;
1376
1377 e = hdr_first_de(&n->index->ihdr);
1378 fnd_push(fnd, n, e);
1379 if (!e)
1380 return -EINVAL;
1381 }
1382
1383ok:
1384 /* return offset to restore enumerator if necessary */
1385 if (!n) {
1386 /* 'e' points in root */
1387 *off = PtrOffset(&root->ihdr, e);
1388 } else {
1389 /* 'e' points in index */
1390 *off = (le64_to_cpu(n->index->vbn) << indx->vbn2vbo_bits) +
1391 record_size + PtrOffset(&n->index->ihdr, e);
1392 }
1393
1394 *entry = e;
1395 return 0;
1396}
1397
1398/*
1399 * indx_create_allocate
1400 *
1401 * create "Allocation + Bitmap" attributes
1402 */
1403static int indx_create_allocate(struct ntfs_index *indx, struct ntfs_inode *ni,
1404 CLST *vbn)
1405{
1406 int err = -ENOMEM;
1407 struct ntfs_sb_info *sbi = ni->mi.sbi;
1408 struct ATTRIB *bitmap;
1409 struct ATTRIB *alloc;
1410 u32 data_size = 1u << indx->index_bits;
1411 u32 alloc_size = ntfs_up_cluster(sbi, data_size);
1412 CLST len = alloc_size >> sbi->cluster_bits;
1413 const struct INDEX_NAMES *in = &s_index_names[indx->type];
1414 CLST alen;
1415 struct runs_tree run;
1416
1417 run_init(&run);
1418
1419 err = attr_allocate_clusters(sbi, &run, 0, 0, len, NULL, 0, &alen, 0,
1420 NULL);
1421 if (err)
1422 goto out;
1423
1424 err = ni_insert_nonresident(ni, ATTR_ALLOC, in->name, in->name_len,
1425 &run, 0, len, 0, &alloc, NULL);
1426 if (err)
1427 goto out1;
1428
1429 alloc->nres.valid_size = alloc->nres.data_size = cpu_to_le64(data_size);
1430
1431 err = ni_insert_resident(ni, bitmap_size(1), ATTR_BITMAP, in->name,
1432 in->name_len, &bitmap, NULL);
1433 if (err)
1434 goto out2;
1435
1436 if (in->name == I30_NAME) {
1437 ni->vfs_inode.i_size = data_size;
1438 inode_set_bytes(&ni->vfs_inode, alloc_size);
1439 }
1440
1441 memcpy(&indx->alloc_run, &run, sizeof(run));
1442
1443 *vbn = 0;
1444
1445 return 0;
1446
1447out2:
1448 mi_remove_attr(&ni->mi, alloc);
1449
1450out1:
1451 run_deallocate(sbi, &run, false);
1452
1453out:
1454 return err;
1455}
1456
1457/*
1458 * indx_add_allocate
1459 *
1460 * add clusters to index
1461 */
1462static int indx_add_allocate(struct ntfs_index *indx, struct ntfs_inode *ni,
1463 CLST *vbn)
1464{
1465 int err;
1466 size_t bit;
1467 u64 data_size;
1468 u64 bmp_size, bmp_size_v;
1469 struct ATTRIB *bmp, *alloc;
1470 struct mft_inode *mi;
1471 const struct INDEX_NAMES *in = &s_index_names[indx->type];
1472
1473 err = indx_find_free(indx, ni, &bit, &bmp);
1474 if (err)
1475 goto out1;
1476
1477 if (bit != MINUS_ONE_T) {
1478 bmp = NULL;
1479 } else {
1480 if (bmp->non_res) {
1481 bmp_size = le64_to_cpu(bmp->nres.data_size);
1482 bmp_size_v = le64_to_cpu(bmp->nres.valid_size);
1483 } else {
1484 bmp_size = bmp_size_v = le32_to_cpu(bmp->res.data_size);
1485 }
1486
1487 bit = bmp_size << 3;
1488 }
1489
1490 data_size = (u64)(bit + 1) << indx->index_bits;
1491
1492 if (bmp) {
1493 /* Increase bitmap */
1494 err = attr_set_size(ni, ATTR_BITMAP, in->name, in->name_len,
1495 &indx->bitmap_run, bitmap_size(bit + 1),
1496 NULL, true, NULL);
1497 if (err)
1498 goto out1;
1499 }
1500
1501 alloc = ni_find_attr(ni, NULL, NULL, ATTR_ALLOC, in->name, in->name_len,
1502 NULL, &mi);
1503 if (!alloc) {
04810f00 1504 err = -EINVAL;
82cae269
KK
1505 if (bmp)
1506 goto out2;
1507 goto out1;
1508 }
1509
1510 /* Increase allocation */
1511 err = attr_set_size(ni, ATTR_ALLOC, in->name, in->name_len,
1512 &indx->alloc_run, data_size, &data_size, true,
1513 NULL);
1514 if (err) {
1515 if (bmp)
1516 goto out2;
1517 goto out1;
1518 }
1519
1520 *vbn = bit << indx->idx2vbn_bits;
1521
1522 return 0;
1523
1524out2:
1525 /* Ops (no space?) */
1526 attr_set_size(ni, ATTR_BITMAP, in->name, in->name_len,
1527 &indx->bitmap_run, bmp_size, &bmp_size_v, false, NULL);
1528
1529out1:
1530 return err;
1531}
1532
1533/*
1534 * indx_insert_into_root
1535 *
1536 * attempts to insert an entry into the index root
1537 * If necessary, it will twiddle the index b-tree.
1538 */
1539static int indx_insert_into_root(struct ntfs_index *indx, struct ntfs_inode *ni,
1540 const struct NTFS_DE *new_de,
1541 struct NTFS_DE *root_de, const void *ctx,
1542 struct ntfs_fnd *fnd)
1543{
1544 int err = 0;
1545 struct NTFS_DE *e, *e0, *re;
1546 struct mft_inode *mi;
1547 struct ATTRIB *attr;
1548 struct MFT_REC *rec;
1549 struct INDEX_HDR *hdr;
1550 struct indx_node *n;
1551 CLST new_vbn;
1552 __le64 *sub_vbn, t_vbn;
1553 u16 new_de_size;
1554 u32 hdr_used, hdr_total, asize, used, to_move;
1555 u32 root_size, new_root_size;
1556 struct ntfs_sb_info *sbi;
1557 int ds_root;
1558 struct INDEX_ROOT *root, *a_root = NULL;
1559
1560 /* Get the record this root placed in */
1561 root = indx_get_root(indx, ni, &attr, &mi);
1562 if (!root)
1563 goto out;
1564
1565 /*
1566 * Try easy case:
1567 * hdr_insert_de will succeed if there's room the root for the new entry.
1568 */
1569 hdr = &root->ihdr;
1570 sbi = ni->mi.sbi;
1571 rec = mi->mrec;
1572 used = le32_to_cpu(rec->used);
1573 new_de_size = le16_to_cpu(new_de->size);
1574 hdr_used = le32_to_cpu(hdr->used);
1575 hdr_total = le32_to_cpu(hdr->total);
1576 asize = le32_to_cpu(attr->size);
1577 root_size = le32_to_cpu(attr->res.data_size);
1578
1579 ds_root = new_de_size + hdr_used - hdr_total;
1580
1581 if (used + ds_root < sbi->max_bytes_per_attr) {
1582 /* make a room for new elements */
1583 mi_resize_attr(mi, attr, ds_root);
1584 hdr->total = cpu_to_le32(hdr_total + ds_root);
1585 e = hdr_insert_de(indx, hdr, new_de, root_de, ctx);
1586 WARN_ON(!e);
1587 fnd_clear(fnd);
1588 fnd->root_de = e;
1589
1590 return 0;
1591 }
1592
1593 /* Make a copy of root attribute to restore if error */
195c52bd 1594 a_root = kmemdup(attr, asize, GFP_NOFS);
82cae269
KK
1595 if (!a_root) {
1596 err = -ENOMEM;
1597 goto out;
1598 }
1599
1600 /* copy all the non-end entries from the index root to the new buffer.*/
1601 to_move = 0;
1602 e0 = hdr_first_de(hdr);
1603
1604 /* Calculate the size to copy */
1605 for (e = e0;; e = hdr_next_de(hdr, e)) {
1606 if (!e) {
1607 err = -EINVAL;
1608 goto out;
1609 }
1610
1611 if (de_is_last(e))
1612 break;
1613 to_move += le16_to_cpu(e->size);
1614 }
1615
1616 n = NULL;
1617 if (!to_move) {
1618 re = NULL;
1619 } else {
195c52bd 1620 re = kmemdup(e0, to_move, GFP_NOFS);
82cae269
KK
1621 if (!re) {
1622 err = -ENOMEM;
1623 goto out;
1624 }
1625 }
1626
1627 sub_vbn = NULL;
1628 if (de_has_vcn(e)) {
1629 t_vbn = de_get_vbn_le(e);
1630 sub_vbn = &t_vbn;
1631 }
1632
1633 new_root_size = sizeof(struct INDEX_ROOT) + sizeof(struct NTFS_DE) +
1634 sizeof(u64);
1635 ds_root = new_root_size - root_size;
1636
1637 if (ds_root > 0 && used + ds_root > sbi->max_bytes_per_attr) {
1638 /* make root external */
1639 err = -EOPNOTSUPP;
1640 goto out;
1641 }
1642
1643 if (ds_root)
1644 mi_resize_attr(mi, attr, ds_root);
1645
1646 /* Fill first entry (vcn will be set later) */
1647 e = (struct NTFS_DE *)(root + 1);
1648 memset(e, 0, sizeof(struct NTFS_DE));
1649 e->size = cpu_to_le16(sizeof(struct NTFS_DE) + sizeof(u64));
1650 e->flags = NTFS_IE_HAS_SUBNODES | NTFS_IE_LAST;
1651
1652 hdr->flags = 1;
1653 hdr->used = hdr->total =
1654 cpu_to_le32(new_root_size - offsetof(struct INDEX_ROOT, ihdr));
1655
1656 fnd->root_de = hdr_first_de(hdr);
1657 mi->dirty = true;
1658
1659 /* Create alloc and bitmap attributes (if not) */
1660 err = run_is_empty(&indx->alloc_run)
1661 ? indx_create_allocate(indx, ni, &new_vbn)
1662 : indx_add_allocate(indx, ni, &new_vbn);
1663
1664 /* layout of record may be changed, so rescan root */
1665 root = indx_get_root(indx, ni, &attr, &mi);
1666 if (!root) {
1667 /* bug? */
1668 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
1669 err = -EINVAL;
1670 goto out1;
1671 }
1672
1673 if (err) {
1674 /* restore root */
1675 if (mi_resize_attr(mi, attr, -ds_root))
1676 memcpy(attr, a_root, asize);
1677 else {
1678 /* bug? */
1679 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
1680 }
1681 goto out1;
1682 }
1683
1684 e = (struct NTFS_DE *)(root + 1);
1685 *(__le64 *)(e + 1) = cpu_to_le64(new_vbn);
1686 mi->dirty = true;
1687
1688 /* now we can create/format the new buffer and copy the entries into */
1689 n = indx_new(indx, ni, new_vbn, sub_vbn);
1690 if (IS_ERR(n)) {
1691 err = PTR_ERR(n);
1692 goto out1;
1693 }
1694
1695 hdr = &n->index->ihdr;
1696 hdr_used = le32_to_cpu(hdr->used);
1697 hdr_total = le32_to_cpu(hdr->total);
1698
1699 /* Copy root entries into new buffer */
1700 hdr_insert_head(hdr, re, to_move);
1701
1702 /* Update bitmap attribute */
1703 indx_mark_used(indx, ni, new_vbn >> indx->idx2vbn_bits);
1704
1705 /* Check if we can insert new entry new index buffer */
1706 if (hdr_used + new_de_size > hdr_total) {
1707 /*
1708 * This occurs if mft record is the same or bigger than index
1709 * buffer. Move all root new index and have no space to add
1710 * new entry classic case when mft record is 1K and index
1711 * buffer 4K the problem should not occurs
1712 */
195c52bd 1713 kfree(re);
82cae269
KK
1714 indx_write(indx, ni, n, 0);
1715
1716 put_indx_node(n);
1717 fnd_clear(fnd);
1718 err = indx_insert_entry(indx, ni, new_de, ctx, fnd);
1719 goto out;
1720 }
1721
1722 /*
1723 * Now root is a parent for new index buffer
1724 * Insert NewEntry a new buffer
1725 */
1726 e = hdr_insert_de(indx, hdr, new_de, NULL, ctx);
1727 if (!e) {
1728 err = -EINVAL;
1729 goto out1;
1730 }
1731 fnd_push(fnd, n, e);
1732
1733 /* Just write updates index into disk */
1734 indx_write(indx, ni, n, 0);
1735
1736 n = NULL;
1737
1738out1:
195c52bd 1739 kfree(re);
82cae269
KK
1740 if (n)
1741 put_indx_node(n);
1742
1743out:
195c52bd 1744 kfree(a_root);
82cae269
KK
1745 return err;
1746}
1747
1748/*
1749 * indx_insert_into_buffer
1750 *
1751 * attempts to insert an entry into an Index Allocation Buffer.
1752 * If necessary, it will split the buffer.
1753 */
1754static int
1755indx_insert_into_buffer(struct ntfs_index *indx, struct ntfs_inode *ni,
1756 struct INDEX_ROOT *root, const struct NTFS_DE *new_de,
1757 const void *ctx, int level, struct ntfs_fnd *fnd)
1758{
1759 int err;
1760 const struct NTFS_DE *sp;
1761 struct NTFS_DE *e, *de_t, *up_e = NULL;
1762 struct indx_node *n2 = NULL;
1763 struct indx_node *n1 = fnd->nodes[level];
1764 struct INDEX_HDR *hdr1 = &n1->index->ihdr;
1765 struct INDEX_HDR *hdr2;
1766 u32 to_copy, used;
1767 CLST new_vbn;
1768 __le64 t_vbn, *sub_vbn;
1769 u16 sp_size;
1770
1771 /* Try the most easy case */
1772 e = fnd->level - 1 == level ? fnd->de[level] : NULL;
1773 e = hdr_insert_de(indx, hdr1, new_de, e, ctx);
1774 fnd->de[level] = e;
1775 if (e) {
1776 /* Just write updated index into disk */
1777 indx_write(indx, ni, n1, 0);
1778 return 0;
1779 }
1780
1781 /*
1782 * No space to insert into buffer. Split it.
1783 * To split we:
1784 * - Save split point ('cause index buffers will be changed)
1785 * - Allocate NewBuffer and copy all entries <= sp into new buffer
1786 * - Remove all entries (sp including) from TargetBuffer
1787 * - Insert NewEntry into left or right buffer (depending on sp <=>
1788 * NewEntry)
1789 * - Insert sp into parent buffer (or root)
1790 * - Make sp a parent for new buffer
1791 */
1792 sp = hdr_find_split(hdr1);
1793 if (!sp)
1794 return -EINVAL;
1795
1796 sp_size = le16_to_cpu(sp->size);
195c52bd 1797 up_e = kmalloc(sp_size + sizeof(u64), GFP_NOFS);
82cae269
KK
1798 if (!up_e)
1799 return -ENOMEM;
1800 memcpy(up_e, sp, sp_size);
1801
1802 if (!hdr1->flags) {
1803 up_e->flags |= NTFS_IE_HAS_SUBNODES;
1804 up_e->size = cpu_to_le16(sp_size + sizeof(u64));
1805 sub_vbn = NULL;
1806 } else {
1807 t_vbn = de_get_vbn_le(up_e);
1808 sub_vbn = &t_vbn;
1809 }
1810
1811 /* Allocate on disk a new index allocation buffer. */
1812 err = indx_add_allocate(indx, ni, &new_vbn);
1813 if (err)
1814 goto out;
1815
1816 /* Allocate and format memory a new index buffer */
1817 n2 = indx_new(indx, ni, new_vbn, sub_vbn);
1818 if (IS_ERR(n2)) {
1819 err = PTR_ERR(n2);
1820 goto out;
1821 }
1822
1823 hdr2 = &n2->index->ihdr;
1824
1825 /* Make sp a parent for new buffer */
1826 de_set_vbn(up_e, new_vbn);
1827
1828 /* copy all the entries <= sp into the new buffer. */
1829 de_t = hdr_first_de(hdr1);
1830 to_copy = PtrOffset(de_t, sp);
1831 hdr_insert_head(hdr2, de_t, to_copy);
1832
1833 /* remove all entries (sp including) from hdr1 */
1834 used = le32_to_cpu(hdr1->used) - to_copy - sp_size;
1835 memmove(de_t, Add2Ptr(sp, sp_size), used - le32_to_cpu(hdr1->de_off));
1836 hdr1->used = cpu_to_le32(used);
1837
1838 /* Insert new entry into left or right buffer (depending on sp <=> new_de) */
1839 hdr_insert_de(indx,
1840 (*indx->cmp)(new_de + 1, le16_to_cpu(new_de->key_size),
1841 up_e + 1, le16_to_cpu(up_e->key_size),
1842 ctx) < 0
1843 ? hdr2
1844 : hdr1,
1845 new_de, NULL, ctx);
1846
1847 indx_mark_used(indx, ni, new_vbn >> indx->idx2vbn_bits);
1848
1849 indx_write(indx, ni, n1, 0);
1850 indx_write(indx, ni, n2, 0);
1851
1852 put_indx_node(n2);
1853
1854 /*
1855 * we've finished splitting everybody, so we are ready to
1856 * insert the promoted entry into the parent.
1857 */
1858 if (!level) {
1859 /* Insert in root */
1860 err = indx_insert_into_root(indx, ni, up_e, NULL, ctx, fnd);
1861 if (err)
1862 goto out;
1863 } else {
1864 /*
1865 * The target buffer's parent is another index buffer
1866 * TODO: Remove recursion
1867 */
1868 err = indx_insert_into_buffer(indx, ni, root, up_e, ctx,
1869 level - 1, fnd);
1870 if (err)
1871 goto out;
1872 }
1873
1874out:
195c52bd 1875 kfree(up_e);
82cae269
KK
1876
1877 return err;
1878}
1879
1880/*
1881 * indx_insert_entry
1882 *
1883 * inserts new entry into index
1884 */
1885int indx_insert_entry(struct ntfs_index *indx, struct ntfs_inode *ni,
1886 const struct NTFS_DE *new_de, const void *ctx,
1887 struct ntfs_fnd *fnd)
1888{
1889 int err;
1890 int diff;
1891 struct NTFS_DE *e;
1892 struct ntfs_fnd *fnd_a = NULL;
1893 struct INDEX_ROOT *root;
1894
1895 if (!fnd) {
1896 fnd_a = fnd_get();
1897 if (!fnd_a) {
1898 err = -ENOMEM;
1899 goto out1;
1900 }
1901 fnd = fnd_a;
1902 }
1903
1904 root = indx_get_root(indx, ni, NULL, NULL);
1905 if (!root) {
1906 err = -EINVAL;
1907 goto out;
1908 }
1909
1910 if (fnd_is_empty(fnd)) {
1911 /* Find the spot the tree where we want to insert the new entry. */
1912 err = indx_find(indx, ni, root, new_de + 1,
1913 le16_to_cpu(new_de->key_size), ctx, &diff, &e,
1914 fnd);
1915 if (err)
1916 goto out;
1917
1918 if (!diff) {
1919 err = -EEXIST;
1920 goto out;
1921 }
1922 }
1923
1924 if (!fnd->level) {
1925 /* The root is also a leaf, so we'll insert the new entry into it. */
1926 err = indx_insert_into_root(indx, ni, new_de, fnd->root_de, ctx,
1927 fnd);
1928 if (err)
1929 goto out;
1930 } else {
1931 /* found a leaf buffer, so we'll insert the new entry into it.*/
1932 err = indx_insert_into_buffer(indx, ni, root, new_de, ctx,
1933 fnd->level - 1, fnd);
1934 if (err)
1935 goto out;
1936 }
1937
1938out:
1939 fnd_put(fnd_a);
1940out1:
1941 return err;
1942}
1943
1944/*
1945 * indx_find_buffer
1946 *
1947 * locates a buffer the tree.
1948 */
1949static struct indx_node *indx_find_buffer(struct ntfs_index *indx,
1950 struct ntfs_inode *ni,
1951 const struct INDEX_ROOT *root,
1952 __le64 vbn, struct indx_node *n)
1953{
1954 int err;
1955 const struct NTFS_DE *e;
1956 struct indx_node *r;
1957 const struct INDEX_HDR *hdr = n ? &n->index->ihdr : &root->ihdr;
1958
1959 /* Step 1: Scan one level */
1960 for (e = hdr_first_de(hdr);; e = hdr_next_de(hdr, e)) {
1961 if (!e)
1962 return ERR_PTR(-EINVAL);
1963
1964 if (de_has_vcn(e) && vbn == de_get_vbn_le(e))
1965 return n;
1966
1967 if (de_is_last(e))
1968 break;
1969 }
1970
1971 /* Step2: Do recursion */
1972 e = Add2Ptr(hdr, le32_to_cpu(hdr->de_off));
1973 for (;;) {
1974 if (de_has_vcn_ex(e)) {
1975 err = indx_read(indx, ni, de_get_vbn(e), &n);
1976 if (err)
1977 return ERR_PTR(err);
1978
1979 r = indx_find_buffer(indx, ni, root, vbn, n);
1980 if (r)
1981 return r;
1982 }
1983
1984 if (de_is_last(e))
1985 break;
1986
1987 e = Add2Ptr(e, le16_to_cpu(e->size));
1988 }
1989
1990 return NULL;
1991}
1992
1993/*
1994 * indx_shrink
1995 *
1996 * deallocates unused tail indexes
1997 */
1998static int indx_shrink(struct ntfs_index *indx, struct ntfs_inode *ni,
1999 size_t bit)
2000{
2001 int err = 0;
2002 u64 bpb, new_data;
2003 size_t nbits;
2004 struct ATTRIB *b;
2005 struct ATTR_LIST_ENTRY *le = NULL;
2006 const struct INDEX_NAMES *in = &s_index_names[indx->type];
2007
2008 b = ni_find_attr(ni, NULL, &le, ATTR_BITMAP, in->name, in->name_len,
2009 NULL, NULL);
2010
2011 if (!b)
2012 return -ENOENT;
2013
2014 if (!b->non_res) {
2015 unsigned long pos;
2016 const unsigned long *bm = resident_data(b);
2017
71eeb6ac 2018 nbits = (size_t)le32_to_cpu(b->res.data_size) * 8;
82cae269
KK
2019
2020 if (bit >= nbits)
2021 return 0;
2022
2023 pos = find_next_bit(bm, nbits, bit);
2024 if (pos < nbits)
2025 return 0;
2026 } else {
2027 size_t used = MINUS_ONE_T;
2028
2029 nbits = le64_to_cpu(b->nres.data_size) * 8;
2030
2031 if (bit >= nbits)
2032 return 0;
2033
2034 err = scan_nres_bitmap(ni, b, indx, bit, &scan_for_used, &used);
2035 if (err)
2036 return err;
2037
2038 if (used != MINUS_ONE_T)
2039 return 0;
2040 }
2041
2042 new_data = (u64)bit << indx->index_bits;
2043
2044 err = attr_set_size(ni, ATTR_ALLOC, in->name, in->name_len,
2045 &indx->alloc_run, new_data, &new_data, false, NULL);
2046 if (err)
2047 return err;
2048
2049 bpb = bitmap_size(bit);
2050 if (bpb * 8 == nbits)
2051 return 0;
2052
2053 err = attr_set_size(ni, ATTR_BITMAP, in->name, in->name_len,
2054 &indx->bitmap_run, bpb, &bpb, false, NULL);
2055
2056 return err;
2057}
2058
2059static int indx_free_children(struct ntfs_index *indx, struct ntfs_inode *ni,
2060 const struct NTFS_DE *e, bool trim)
2061{
2062 int err;
2063 struct indx_node *n;
2064 struct INDEX_HDR *hdr;
2065 CLST vbn = de_get_vbn(e);
2066 size_t i;
2067
2068 err = indx_read(indx, ni, vbn, &n);
2069 if (err)
2070 return err;
2071
2072 hdr = &n->index->ihdr;
2073 /* First, recurse into the children, if any.*/
2074 if (hdr_has_subnode(hdr)) {
2075 for (e = hdr_first_de(hdr); e; e = hdr_next_de(hdr, e)) {
2076 indx_free_children(indx, ni, e, false);
2077 if (de_is_last(e))
2078 break;
2079 }
2080 }
2081
2082 put_indx_node(n);
2083
2084 i = vbn >> indx->idx2vbn_bits;
2085 /* We've gotten rid of the children; add this buffer to the free list. */
2086 indx_mark_free(indx, ni, i);
2087
2088 if (!trim)
2089 return 0;
2090
2091 /*
2092 * If there are no used indexes after current free index
2093 * then we can truncate allocation and bitmap
2094 * Use bitmap to estimate the case
2095 */
2096 indx_shrink(indx, ni, i + 1);
2097 return 0;
2098}
2099
2100/*
2101 * indx_get_entry_to_replace
2102 *
2103 * finds a replacement entry for a deleted entry
2104 * always returns a node entry:
2105 * NTFS_IE_HAS_SUBNODES is set the flags and the size includes the sub_vcn
2106 */
2107static int indx_get_entry_to_replace(struct ntfs_index *indx,
2108 struct ntfs_inode *ni,
2109 const struct NTFS_DE *de_next,
2110 struct NTFS_DE **de_to_replace,
2111 struct ntfs_fnd *fnd)
2112{
2113 int err;
2114 int level = -1;
2115 CLST vbn;
2116 struct NTFS_DE *e, *te, *re;
2117 struct indx_node *n;
2118 struct INDEX_BUFFER *ib;
2119
2120 *de_to_replace = NULL;
2121
2122 /* Find first leaf entry down from de_next */
2123 vbn = de_get_vbn(de_next);
2124 for (;;) {
2125 n = NULL;
2126 err = indx_read(indx, ni, vbn, &n);
2127 if (err)
2128 goto out;
2129
2130 e = hdr_first_de(&n->index->ihdr);
2131 fnd_push(fnd, n, e);
2132
2133 if (!de_is_last(e)) {
2134 /*
2135 * This buffer is non-empty, so its first entry could be used as the
2136 * replacement entry.
2137 */
2138 level = fnd->level - 1;
2139 }
2140
2141 if (!de_has_vcn(e))
2142 break;
2143
2144 /* This buffer is a node. Continue to go down */
2145 vbn = de_get_vbn(e);
2146 }
2147
2148 if (level == -1)
2149 goto out;
2150
2151 n = fnd->nodes[level];
2152 te = hdr_first_de(&n->index->ihdr);
2153 /* Copy the candidate entry into the replacement entry buffer. */
195c52bd 2154 re = kmalloc(le16_to_cpu(te->size) + sizeof(u64), GFP_NOFS);
82cae269
KK
2155 if (!re) {
2156 err = -ENOMEM;
2157 goto out;
2158 }
2159
2160 *de_to_replace = re;
2161 memcpy(re, te, le16_to_cpu(te->size));
2162
2163 if (!de_has_vcn(re)) {
2164 /*
2165 * The replacement entry we found doesn't have a sub_vcn. increase its size
2166 * to hold one.
2167 */
2168 le16_add_cpu(&re->size, sizeof(u64));
2169 re->flags |= NTFS_IE_HAS_SUBNODES;
2170 } else {
2171 /*
2172 * The replacement entry we found was a node entry, which means that all
2173 * its child buffers are empty. Return them to the free pool.
2174 */
2175 indx_free_children(indx, ni, te, true);
2176 }
2177
2178 /*
2179 * Expunge the replacement entry from its former location,
2180 * and then write that buffer.
2181 */
2182 ib = n->index;
2183 e = hdr_delete_de(&ib->ihdr, te);
2184
2185 fnd->de[level] = e;
2186 indx_write(indx, ni, n, 0);
2187
2188 /* Check to see if this action created an empty leaf. */
2189 if (ib_is_leaf(ib) && ib_is_empty(ib))
2190 return 0;
2191
2192out:
2193 fnd_clear(fnd);
2194 return err;
2195}
2196
2197/*
2198 * indx_delete_entry
2199 *
2200 * deletes an entry from the index.
2201 */
2202int indx_delete_entry(struct ntfs_index *indx, struct ntfs_inode *ni,
2203 const void *key, u32 key_len, const void *ctx)
2204{
2205 int err, diff;
2206 struct INDEX_ROOT *root;
2207 struct INDEX_HDR *hdr;
2208 struct ntfs_fnd *fnd, *fnd2;
2209 struct INDEX_BUFFER *ib;
2210 struct NTFS_DE *e, *re, *next, *prev, *me;
2211 struct indx_node *n, *n2d = NULL;
2212 __le64 sub_vbn;
2213 int level, level2;
2214 struct ATTRIB *attr;
2215 struct mft_inode *mi;
2216 u32 e_size, root_size, new_root_size;
2217 size_t trim_bit;
2218 const struct INDEX_NAMES *in;
2219
2220 fnd = fnd_get();
2221 if (!fnd) {
2222 err = -ENOMEM;
2223 goto out2;
2224 }
2225
2226 fnd2 = fnd_get();
2227 if (!fnd2) {
2228 err = -ENOMEM;
2229 goto out1;
2230 }
2231
2232 root = indx_get_root(indx, ni, &attr, &mi);
2233 if (!root) {
2234 err = -EINVAL;
2235 goto out;
2236 }
2237
2238 /* Locate the entry to remove. */
2239 err = indx_find(indx, ni, root, key, key_len, ctx, &diff, &e, fnd);
2240 if (err)
2241 goto out;
2242
2243 if (!e || diff) {
2244 err = -ENOENT;
2245 goto out;
2246 }
2247
2248 level = fnd->level;
2249
2250 if (level) {
2251 n = fnd->nodes[level - 1];
2252 e = fnd->de[level - 1];
2253 ib = n->index;
2254 hdr = &ib->ihdr;
2255 } else {
2256 hdr = &root->ihdr;
2257 e = fnd->root_de;
2258 n = NULL;
2259 }
2260
2261 e_size = le16_to_cpu(e->size);
2262
2263 if (!de_has_vcn_ex(e)) {
2264 /* The entry to delete is a leaf, so we can just rip it out */
2265 hdr_delete_de(hdr, e);
2266
2267 if (!level) {
2268 hdr->total = hdr->used;
2269
2270 /* Shrink resident root attribute */
2271 mi_resize_attr(mi, attr, 0 - e_size);
2272 goto out;
2273 }
2274
2275 indx_write(indx, ni, n, 0);
2276
2277 /*
2278 * Check to see if removing that entry made
2279 * the leaf empty.
2280 */
2281 if (ib_is_leaf(ib) && ib_is_empty(ib)) {
2282 fnd_pop(fnd);
2283 fnd_push(fnd2, n, e);
2284 }
2285 } else {
2286 /*
2287 * The entry we wish to delete is a node buffer, so we
2288 * have to find a replacement for it.
2289 */
2290 next = de_get_next(e);
2291
2292 err = indx_get_entry_to_replace(indx, ni, next, &re, fnd2);
2293 if (err)
2294 goto out;
2295
2296 if (re) {
2297 de_set_vbn_le(re, de_get_vbn_le(e));
2298 hdr_delete_de(hdr, e);
2299
2300 err = level ? indx_insert_into_buffer(indx, ni, root,
2301 re, ctx,
2302 fnd->level - 1,
2303 fnd)
2304 : indx_insert_into_root(indx, ni, re, e,
2305 ctx, fnd);
195c52bd 2306 kfree(re);
82cae269
KK
2307
2308 if (err)
2309 goto out;
2310 } else {
2311 /*
2312 * There is no replacement for the current entry.
2313 * This means that the subtree rooted at its node is empty,
2314 * and can be deleted, which turn means that the node can
2315 * just inherit the deleted entry sub_vcn
2316 */
2317 indx_free_children(indx, ni, next, true);
2318
2319 de_set_vbn_le(next, de_get_vbn_le(e));
2320 hdr_delete_de(hdr, e);
2321 if (level) {
2322 indx_write(indx, ni, n, 0);
2323 } else {
2324 hdr->total = hdr->used;
2325
2326 /* Shrink resident root attribute */
2327 mi_resize_attr(mi, attr, 0 - e_size);
2328 }
2329 }
2330 }
2331
2332 /* Delete a branch of tree */
2333 if (!fnd2 || !fnd2->level)
2334 goto out;
2335
2336 /* Reinit root 'cause it can be changed */
2337 root = indx_get_root(indx, ni, &attr, &mi);
2338 if (!root) {
2339 err = -EINVAL;
2340 goto out;
2341 }
2342
2343 n2d = NULL;
2344 sub_vbn = fnd2->nodes[0]->index->vbn;
2345 level2 = 0;
2346 level = fnd->level;
2347
2348 hdr = level ? &fnd->nodes[level - 1]->index->ihdr : &root->ihdr;
2349
2350 /* Scan current level */
2351 for (e = hdr_first_de(hdr);; e = hdr_next_de(hdr, e)) {
2352 if (!e) {
2353 err = -EINVAL;
2354 goto out;
2355 }
2356
2357 if (de_has_vcn(e) && sub_vbn == de_get_vbn_le(e))
2358 break;
2359
2360 if (de_is_last(e)) {
2361 e = NULL;
2362 break;
2363 }
2364 }
2365
2366 if (!e) {
2367 /* Do slow search from root */
2368 struct indx_node *in;
2369
2370 fnd_clear(fnd);
2371
2372 in = indx_find_buffer(indx, ni, root, sub_vbn, NULL);
2373 if (IS_ERR(in)) {
2374 err = PTR_ERR(in);
2375 goto out;
2376 }
2377
2378 if (in)
2379 fnd_push(fnd, in, NULL);
2380 }
2381
2382 /* Merge fnd2 -> fnd */
2383 for (level = 0; level < fnd2->level; level++) {
2384 fnd_push(fnd, fnd2->nodes[level], fnd2->de[level]);
2385 fnd2->nodes[level] = NULL;
2386 }
2387 fnd2->level = 0;
2388
2389 hdr = NULL;
2390 for (level = fnd->level; level; level--) {
2391 struct indx_node *in = fnd->nodes[level - 1];
2392
2393 ib = in->index;
2394 if (ib_is_empty(ib)) {
2395 sub_vbn = ib->vbn;
2396 } else {
2397 hdr = &ib->ihdr;
2398 n2d = in;
2399 level2 = level;
2400 break;
2401 }
2402 }
2403
2404 if (!hdr)
2405 hdr = &root->ihdr;
2406
2407 e = hdr_first_de(hdr);
2408 if (!e) {
2409 err = -EINVAL;
2410 goto out;
2411 }
2412
2413 if (hdr != &root->ihdr || !de_is_last(e)) {
2414 prev = NULL;
2415 while (!de_is_last(e)) {
2416 if (de_has_vcn(e) && sub_vbn == de_get_vbn_le(e))
2417 break;
2418 prev = e;
2419 e = hdr_next_de(hdr, e);
2420 if (!e) {
2421 err = -EINVAL;
2422 goto out;
2423 }
2424 }
2425
2426 if (sub_vbn != de_get_vbn_le(e)) {
2427 /*
2428 * Didn't find the parent entry, although this buffer is the parent trail.
2429 * Something is corrupt.
2430 */
2431 err = -EINVAL;
2432 goto out;
2433 }
2434
2435 if (de_is_last(e)) {
2436 /*
2437 * Since we can't remove the end entry, we'll remove its
2438 * predecessor instead. This means we have to transfer the
2439 * predecessor's sub_vcn to the end entry.
2440 * Note: that this index block is not empty, so the
2441 * predecessor must exist
2442 */
2443 if (!prev) {
2444 err = -EINVAL;
2445 goto out;
2446 }
2447
2448 if (de_has_vcn(prev)) {
2449 de_set_vbn_le(e, de_get_vbn_le(prev));
2450 } else if (de_has_vcn(e)) {
2451 le16_sub_cpu(&e->size, sizeof(u64));
2452 e->flags &= ~NTFS_IE_HAS_SUBNODES;
2453 le32_sub_cpu(&hdr->used, sizeof(u64));
2454 }
2455 e = prev;
2456 }
2457
2458 /*
2459 * Copy the current entry into a temporary buffer (stripping off its
2460 * down-pointer, if any) and delete it from the current buffer or root,
2461 * as appropriate.
2462 */
2463 e_size = le16_to_cpu(e->size);
195c52bd 2464 me = kmemdup(e, e_size, GFP_NOFS);
82cae269
KK
2465 if (!me) {
2466 err = -ENOMEM;
2467 goto out;
2468 }
2469
2470 if (de_has_vcn(me)) {
2471 me->flags &= ~NTFS_IE_HAS_SUBNODES;
2472 le16_sub_cpu(&me->size, sizeof(u64));
2473 }
2474
2475 hdr_delete_de(hdr, e);
2476
2477 if (hdr == &root->ihdr) {
2478 level = 0;
2479 hdr->total = hdr->used;
2480
2481 /* Shrink resident root attribute */
2482 mi_resize_attr(mi, attr, 0 - e_size);
2483 } else {
2484 indx_write(indx, ni, n2d, 0);
2485 level = level2;
2486 }
2487
2488 /* Mark unused buffers as free */
2489 trim_bit = -1;
2490 for (; level < fnd->level; level++) {
2491 ib = fnd->nodes[level]->index;
2492 if (ib_is_empty(ib)) {
2493 size_t k = le64_to_cpu(ib->vbn) >>
2494 indx->idx2vbn_bits;
2495
2496 indx_mark_free(indx, ni, k);
2497 if (k < trim_bit)
2498 trim_bit = k;
2499 }
2500 }
2501
2502 fnd_clear(fnd);
2503 /*fnd->root_de = NULL;*/
2504
2505 /*
2506 * Re-insert the entry into the tree.
2507 * Find the spot the tree where we want to insert the new entry.
2508 */
2509 err = indx_insert_entry(indx, ni, me, ctx, fnd);
195c52bd 2510 kfree(me);
82cae269
KK
2511 if (err)
2512 goto out;
2513
2514 if (trim_bit != -1)
2515 indx_shrink(indx, ni, trim_bit);
2516 } else {
2517 /*
2518 * This tree needs to be collapsed down to an empty root.
2519 * Recreate the index root as an empty leaf and free all the bits the
2520 * index allocation bitmap.
2521 */
2522 fnd_clear(fnd);
2523 fnd_clear(fnd2);
2524
2525 in = &s_index_names[indx->type];
2526
2527 err = attr_set_size(ni, ATTR_ALLOC, in->name, in->name_len,
2528 &indx->alloc_run, 0, NULL, false, NULL);
2529 err = ni_remove_attr(ni, ATTR_ALLOC, in->name, in->name_len,
2530 false, NULL);
2531 run_close(&indx->alloc_run);
2532
2533 err = attr_set_size(ni, ATTR_BITMAP, in->name, in->name_len,
2534 &indx->bitmap_run, 0, NULL, false, NULL);
2535 err = ni_remove_attr(ni, ATTR_BITMAP, in->name, in->name_len,
2536 false, NULL);
2537 run_close(&indx->bitmap_run);
2538
2539 root = indx_get_root(indx, ni, &attr, &mi);
2540 if (!root) {
2541 err = -EINVAL;
2542 goto out;
2543 }
2544
2545 root_size = le32_to_cpu(attr->res.data_size);
2546 new_root_size =
2547 sizeof(struct INDEX_ROOT) + sizeof(struct NTFS_DE);
2548
2549 if (new_root_size != root_size &&
2550 !mi_resize_attr(mi, attr, new_root_size - root_size)) {
2551 err = -EINVAL;
2552 goto out;
2553 }
2554
2555 /* Fill first entry */
2556 e = (struct NTFS_DE *)(root + 1);
2557 e->ref.low = 0;
2558 e->ref.high = 0;
2559 e->ref.seq = 0;
2560 e->size = cpu_to_le16(sizeof(struct NTFS_DE));
2561 e->flags = NTFS_IE_LAST; // 0x02
2562 e->key_size = 0;
2563 e->res = 0;
2564
2565 hdr = &root->ihdr;
2566 hdr->flags = 0;
2567 hdr->used = hdr->total = cpu_to_le32(
2568 new_root_size - offsetof(struct INDEX_ROOT, ihdr));
2569 mi->dirty = true;
2570 }
2571
2572out:
2573 fnd_put(fnd2);
2574out1:
2575 fnd_put(fnd);
2576out2:
2577 return err;
2578}
2579
2580/*
2581 * Update duplicated information in directory entry
2582 * 'dup' - info from MFT record
2583 */
2584int indx_update_dup(struct ntfs_inode *ni, struct ntfs_sb_info *sbi,
2585 const struct ATTR_FILE_NAME *fname,
2586 const struct NTFS_DUP_INFO *dup, int sync)
2587{
2588 int err, diff;
2589 struct NTFS_DE *e = NULL;
2590 struct ATTR_FILE_NAME *e_fname;
2591 struct ntfs_fnd *fnd;
2592 struct INDEX_ROOT *root;
2593 struct mft_inode *mi;
2594 struct ntfs_index *indx = &ni->dir;
2595
2596 fnd = fnd_get();
2597 if (!fnd) {
2598 err = -ENOMEM;
2599 goto out1;
2600 }
2601
2602 root = indx_get_root(indx, ni, NULL, &mi);
2603 if (!root) {
2604 err = -EINVAL;
2605 goto out;
2606 }
2607
2608 /* Find entry in directory */
2609 err = indx_find(indx, ni, root, fname, fname_full_size(fname), sbi,
2610 &diff, &e, fnd);
2611 if (err)
2612 goto out;
2613
2614 if (!e) {
2615 err = -EINVAL;
2616 goto out;
2617 }
2618
2619 if (diff) {
2620 err = -EINVAL;
2621 goto out;
2622 }
2623
2624 e_fname = (struct ATTR_FILE_NAME *)(e + 1);
2625
2626 if (!memcmp(&e_fname->dup, dup, sizeof(*dup))) {
2627 /* nothing to update in index! Try to avoid this call */
2628 goto out;
2629 }
2630
2631 memcpy(&e_fname->dup, dup, sizeof(*dup));
2632
2633 if (fnd->level) {
2634 /* directory entry in index */
2635 err = indx_write(indx, ni, fnd->nodes[fnd->level - 1], sync);
2636 } else {
2637 /* directory entry in directory MFT record */
2638 mi->dirty = true;
2639 if (sync)
2640 err = mi_write(mi, 1);
2641 else
2642 mark_inode_dirty(&ni->vfs_inode);
2643 }
2644
2645out:
2646 fnd_put(fnd);
2647
2648out1:
2649 return err;
2650}