]> git.proxmox.com Git - mirror_ubuntu-kernels.git/blame - fs/ntfs3/index.c
fs/ntfs3: Fix error handling in indx_insert_into_root()
[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;
b8155e95 1558 struct INDEX_ROOT *root, *a_root;
82cae269
KK
1559
1560 /* Get the record this root placed in */
1561 root = indx_get_root(indx, ni, &attr, &mi);
1562 if (!root)
b8155e95 1563 return -EINVAL;
82cae269
KK
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);
b8155e95
DC
1595 if (!a_root)
1596 return -ENOMEM;
82cae269
KK
1597
1598 /* copy all the non-end entries from the index root to the new buffer.*/
1599 to_move = 0;
1600 e0 = hdr_first_de(hdr);
1601
1602 /* Calculate the size to copy */
1603 for (e = e0;; e = hdr_next_de(hdr, e)) {
1604 if (!e) {
1605 err = -EINVAL;
b8155e95 1606 goto out_free_root;
82cae269
KK
1607 }
1608
1609 if (de_is_last(e))
1610 break;
1611 to_move += le16_to_cpu(e->size);
1612 }
1613
82cae269
KK
1614 if (!to_move) {
1615 re = NULL;
1616 } else {
195c52bd 1617 re = kmemdup(e0, to_move, GFP_NOFS);
82cae269
KK
1618 if (!re) {
1619 err = -ENOMEM;
b8155e95 1620 goto out_free_root;
82cae269
KK
1621 }
1622 }
1623
1624 sub_vbn = NULL;
1625 if (de_has_vcn(e)) {
1626 t_vbn = de_get_vbn_le(e);
1627 sub_vbn = &t_vbn;
1628 }
1629
1630 new_root_size = sizeof(struct INDEX_ROOT) + sizeof(struct NTFS_DE) +
1631 sizeof(u64);
1632 ds_root = new_root_size - root_size;
1633
1634 if (ds_root > 0 && used + ds_root > sbi->max_bytes_per_attr) {
1635 /* make root external */
1636 err = -EOPNOTSUPP;
b8155e95 1637 goto out_free_re;
82cae269
KK
1638 }
1639
1640 if (ds_root)
1641 mi_resize_attr(mi, attr, ds_root);
1642
1643 /* Fill first entry (vcn will be set later) */
1644 e = (struct NTFS_DE *)(root + 1);
1645 memset(e, 0, sizeof(struct NTFS_DE));
1646 e->size = cpu_to_le16(sizeof(struct NTFS_DE) + sizeof(u64));
1647 e->flags = NTFS_IE_HAS_SUBNODES | NTFS_IE_LAST;
1648
1649 hdr->flags = 1;
1650 hdr->used = hdr->total =
1651 cpu_to_le32(new_root_size - offsetof(struct INDEX_ROOT, ihdr));
1652
1653 fnd->root_de = hdr_first_de(hdr);
1654 mi->dirty = true;
1655
1656 /* Create alloc and bitmap attributes (if not) */
1657 err = run_is_empty(&indx->alloc_run)
1658 ? indx_create_allocate(indx, ni, &new_vbn)
1659 : indx_add_allocate(indx, ni, &new_vbn);
1660
1661 /* layout of record may be changed, so rescan root */
1662 root = indx_get_root(indx, ni, &attr, &mi);
1663 if (!root) {
1664 /* bug? */
1665 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
1666 err = -EINVAL;
b8155e95 1667 goto out_free_re;
82cae269
KK
1668 }
1669
1670 if (err) {
1671 /* restore root */
1672 if (mi_resize_attr(mi, attr, -ds_root))
1673 memcpy(attr, a_root, asize);
1674 else {
1675 /* bug? */
1676 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
1677 }
b8155e95 1678 goto out_free_re;
82cae269
KK
1679 }
1680
1681 e = (struct NTFS_DE *)(root + 1);
1682 *(__le64 *)(e + 1) = cpu_to_le64(new_vbn);
1683 mi->dirty = true;
1684
1685 /* now we can create/format the new buffer and copy the entries into */
1686 n = indx_new(indx, ni, new_vbn, sub_vbn);
1687 if (IS_ERR(n)) {
1688 err = PTR_ERR(n);
b8155e95 1689 goto out_free_re;
82cae269
KK
1690 }
1691
1692 hdr = &n->index->ihdr;
1693 hdr_used = le32_to_cpu(hdr->used);
1694 hdr_total = le32_to_cpu(hdr->total);
1695
1696 /* Copy root entries into new buffer */
1697 hdr_insert_head(hdr, re, to_move);
1698
1699 /* Update bitmap attribute */
1700 indx_mark_used(indx, ni, new_vbn >> indx->idx2vbn_bits);
1701
1702 /* Check if we can insert new entry new index buffer */
1703 if (hdr_used + new_de_size > hdr_total) {
1704 /*
1705 * This occurs if mft record is the same or bigger than index
1706 * buffer. Move all root new index and have no space to add
1707 * new entry classic case when mft record is 1K and index
1708 * buffer 4K the problem should not occurs
1709 */
195c52bd 1710 kfree(re);
82cae269
KK
1711 indx_write(indx, ni, n, 0);
1712
1713 put_indx_node(n);
1714 fnd_clear(fnd);
1715 err = indx_insert_entry(indx, ni, new_de, ctx, fnd);
b8155e95 1716 goto out_free_root;
82cae269
KK
1717 }
1718
1719 /*
1720 * Now root is a parent for new index buffer
1721 * Insert NewEntry a new buffer
1722 */
1723 e = hdr_insert_de(indx, hdr, new_de, NULL, ctx);
1724 if (!e) {
1725 err = -EINVAL;
b8155e95 1726 goto out_put_n;
82cae269
KK
1727 }
1728 fnd_push(fnd, n, e);
1729
1730 /* Just write updates index into disk */
1731 indx_write(indx, ni, n, 0);
1732
1733 n = NULL;
1734
b8155e95
DC
1735out_put_n:
1736 put_indx_node(n);
1737out_free_re:
195c52bd 1738 kfree(re);
b8155e95 1739out_free_root:
195c52bd 1740 kfree(a_root);
82cae269
KK
1741 return err;
1742}
1743
1744/*
1745 * indx_insert_into_buffer
1746 *
1747 * attempts to insert an entry into an Index Allocation Buffer.
1748 * If necessary, it will split the buffer.
1749 */
1750static int
1751indx_insert_into_buffer(struct ntfs_index *indx, struct ntfs_inode *ni,
1752 struct INDEX_ROOT *root, const struct NTFS_DE *new_de,
1753 const void *ctx, int level, struct ntfs_fnd *fnd)
1754{
1755 int err;
1756 const struct NTFS_DE *sp;
1757 struct NTFS_DE *e, *de_t, *up_e = NULL;
1758 struct indx_node *n2 = NULL;
1759 struct indx_node *n1 = fnd->nodes[level];
1760 struct INDEX_HDR *hdr1 = &n1->index->ihdr;
1761 struct INDEX_HDR *hdr2;
1762 u32 to_copy, used;
1763 CLST new_vbn;
1764 __le64 t_vbn, *sub_vbn;
1765 u16 sp_size;
1766
1767 /* Try the most easy case */
1768 e = fnd->level - 1 == level ? fnd->de[level] : NULL;
1769 e = hdr_insert_de(indx, hdr1, new_de, e, ctx);
1770 fnd->de[level] = e;
1771 if (e) {
1772 /* Just write updated index into disk */
1773 indx_write(indx, ni, n1, 0);
1774 return 0;
1775 }
1776
1777 /*
1778 * No space to insert into buffer. Split it.
1779 * To split we:
1780 * - Save split point ('cause index buffers will be changed)
1781 * - Allocate NewBuffer and copy all entries <= sp into new buffer
1782 * - Remove all entries (sp including) from TargetBuffer
1783 * - Insert NewEntry into left or right buffer (depending on sp <=>
1784 * NewEntry)
1785 * - Insert sp into parent buffer (or root)
1786 * - Make sp a parent for new buffer
1787 */
1788 sp = hdr_find_split(hdr1);
1789 if (!sp)
1790 return -EINVAL;
1791
1792 sp_size = le16_to_cpu(sp->size);
195c52bd 1793 up_e = kmalloc(sp_size + sizeof(u64), GFP_NOFS);
82cae269
KK
1794 if (!up_e)
1795 return -ENOMEM;
1796 memcpy(up_e, sp, sp_size);
1797
1798 if (!hdr1->flags) {
1799 up_e->flags |= NTFS_IE_HAS_SUBNODES;
1800 up_e->size = cpu_to_le16(sp_size + sizeof(u64));
1801 sub_vbn = NULL;
1802 } else {
1803 t_vbn = de_get_vbn_le(up_e);
1804 sub_vbn = &t_vbn;
1805 }
1806
1807 /* Allocate on disk a new index allocation buffer. */
1808 err = indx_add_allocate(indx, ni, &new_vbn);
1809 if (err)
1810 goto out;
1811
1812 /* Allocate and format memory a new index buffer */
1813 n2 = indx_new(indx, ni, new_vbn, sub_vbn);
1814 if (IS_ERR(n2)) {
1815 err = PTR_ERR(n2);
1816 goto out;
1817 }
1818
1819 hdr2 = &n2->index->ihdr;
1820
1821 /* Make sp a parent for new buffer */
1822 de_set_vbn(up_e, new_vbn);
1823
1824 /* copy all the entries <= sp into the new buffer. */
1825 de_t = hdr_first_de(hdr1);
1826 to_copy = PtrOffset(de_t, sp);
1827 hdr_insert_head(hdr2, de_t, to_copy);
1828
1829 /* remove all entries (sp including) from hdr1 */
1830 used = le32_to_cpu(hdr1->used) - to_copy - sp_size;
1831 memmove(de_t, Add2Ptr(sp, sp_size), used - le32_to_cpu(hdr1->de_off));
1832 hdr1->used = cpu_to_le32(used);
1833
1834 /* Insert new entry into left or right buffer (depending on sp <=> new_de) */
1835 hdr_insert_de(indx,
1836 (*indx->cmp)(new_de + 1, le16_to_cpu(new_de->key_size),
1837 up_e + 1, le16_to_cpu(up_e->key_size),
1838 ctx) < 0
1839 ? hdr2
1840 : hdr1,
1841 new_de, NULL, ctx);
1842
1843 indx_mark_used(indx, ni, new_vbn >> indx->idx2vbn_bits);
1844
1845 indx_write(indx, ni, n1, 0);
1846 indx_write(indx, ni, n2, 0);
1847
1848 put_indx_node(n2);
1849
1850 /*
1851 * we've finished splitting everybody, so we are ready to
1852 * insert the promoted entry into the parent.
1853 */
1854 if (!level) {
1855 /* Insert in root */
1856 err = indx_insert_into_root(indx, ni, up_e, NULL, ctx, fnd);
1857 if (err)
1858 goto out;
1859 } else {
1860 /*
1861 * The target buffer's parent is another index buffer
1862 * TODO: Remove recursion
1863 */
1864 err = indx_insert_into_buffer(indx, ni, root, up_e, ctx,
1865 level - 1, fnd);
1866 if (err)
1867 goto out;
1868 }
1869
1870out:
195c52bd 1871 kfree(up_e);
82cae269
KK
1872
1873 return err;
1874}
1875
1876/*
1877 * indx_insert_entry
1878 *
1879 * inserts new entry into index
1880 */
1881int indx_insert_entry(struct ntfs_index *indx, struct ntfs_inode *ni,
1882 const struct NTFS_DE *new_de, const void *ctx,
1883 struct ntfs_fnd *fnd)
1884{
1885 int err;
1886 int diff;
1887 struct NTFS_DE *e;
1888 struct ntfs_fnd *fnd_a = NULL;
1889 struct INDEX_ROOT *root;
1890
1891 if (!fnd) {
1892 fnd_a = fnd_get();
1893 if (!fnd_a) {
1894 err = -ENOMEM;
1895 goto out1;
1896 }
1897 fnd = fnd_a;
1898 }
1899
1900 root = indx_get_root(indx, ni, NULL, NULL);
1901 if (!root) {
1902 err = -EINVAL;
1903 goto out;
1904 }
1905
1906 if (fnd_is_empty(fnd)) {
1907 /* Find the spot the tree where we want to insert the new entry. */
1908 err = indx_find(indx, ni, root, new_de + 1,
1909 le16_to_cpu(new_de->key_size), ctx, &diff, &e,
1910 fnd);
1911 if (err)
1912 goto out;
1913
1914 if (!diff) {
1915 err = -EEXIST;
1916 goto out;
1917 }
1918 }
1919
1920 if (!fnd->level) {
1921 /* The root is also a leaf, so we'll insert the new entry into it. */
1922 err = indx_insert_into_root(indx, ni, new_de, fnd->root_de, ctx,
1923 fnd);
1924 if (err)
1925 goto out;
1926 } else {
1927 /* found a leaf buffer, so we'll insert the new entry into it.*/
1928 err = indx_insert_into_buffer(indx, ni, root, new_de, ctx,
1929 fnd->level - 1, fnd);
1930 if (err)
1931 goto out;
1932 }
1933
1934out:
1935 fnd_put(fnd_a);
1936out1:
1937 return err;
1938}
1939
1940/*
1941 * indx_find_buffer
1942 *
1943 * locates a buffer the tree.
1944 */
1945static struct indx_node *indx_find_buffer(struct ntfs_index *indx,
1946 struct ntfs_inode *ni,
1947 const struct INDEX_ROOT *root,
1948 __le64 vbn, struct indx_node *n)
1949{
1950 int err;
1951 const struct NTFS_DE *e;
1952 struct indx_node *r;
1953 const struct INDEX_HDR *hdr = n ? &n->index->ihdr : &root->ihdr;
1954
1955 /* Step 1: Scan one level */
1956 for (e = hdr_first_de(hdr);; e = hdr_next_de(hdr, e)) {
1957 if (!e)
1958 return ERR_PTR(-EINVAL);
1959
1960 if (de_has_vcn(e) && vbn == de_get_vbn_le(e))
1961 return n;
1962
1963 if (de_is_last(e))
1964 break;
1965 }
1966
1967 /* Step2: Do recursion */
1968 e = Add2Ptr(hdr, le32_to_cpu(hdr->de_off));
1969 for (;;) {
1970 if (de_has_vcn_ex(e)) {
1971 err = indx_read(indx, ni, de_get_vbn(e), &n);
1972 if (err)
1973 return ERR_PTR(err);
1974
1975 r = indx_find_buffer(indx, ni, root, vbn, n);
1976 if (r)
1977 return r;
1978 }
1979
1980 if (de_is_last(e))
1981 break;
1982
1983 e = Add2Ptr(e, le16_to_cpu(e->size));
1984 }
1985
1986 return NULL;
1987}
1988
1989/*
1990 * indx_shrink
1991 *
1992 * deallocates unused tail indexes
1993 */
1994static int indx_shrink(struct ntfs_index *indx, struct ntfs_inode *ni,
1995 size_t bit)
1996{
1997 int err = 0;
1998 u64 bpb, new_data;
1999 size_t nbits;
2000 struct ATTRIB *b;
2001 struct ATTR_LIST_ENTRY *le = NULL;
2002 const struct INDEX_NAMES *in = &s_index_names[indx->type];
2003
2004 b = ni_find_attr(ni, NULL, &le, ATTR_BITMAP, in->name, in->name_len,
2005 NULL, NULL);
2006
2007 if (!b)
2008 return -ENOENT;
2009
2010 if (!b->non_res) {
2011 unsigned long pos;
2012 const unsigned long *bm = resident_data(b);
2013
71eeb6ac 2014 nbits = (size_t)le32_to_cpu(b->res.data_size) * 8;
82cae269
KK
2015
2016 if (bit >= nbits)
2017 return 0;
2018
2019 pos = find_next_bit(bm, nbits, bit);
2020 if (pos < nbits)
2021 return 0;
2022 } else {
2023 size_t used = MINUS_ONE_T;
2024
2025 nbits = le64_to_cpu(b->nres.data_size) * 8;
2026
2027 if (bit >= nbits)
2028 return 0;
2029
2030 err = scan_nres_bitmap(ni, b, indx, bit, &scan_for_used, &used);
2031 if (err)
2032 return err;
2033
2034 if (used != MINUS_ONE_T)
2035 return 0;
2036 }
2037
2038 new_data = (u64)bit << indx->index_bits;
2039
2040 err = attr_set_size(ni, ATTR_ALLOC, in->name, in->name_len,
2041 &indx->alloc_run, new_data, &new_data, false, NULL);
2042 if (err)
2043 return err;
2044
2045 bpb = bitmap_size(bit);
2046 if (bpb * 8 == nbits)
2047 return 0;
2048
2049 err = attr_set_size(ni, ATTR_BITMAP, in->name, in->name_len,
2050 &indx->bitmap_run, bpb, &bpb, false, NULL);
2051
2052 return err;
2053}
2054
2055static int indx_free_children(struct ntfs_index *indx, struct ntfs_inode *ni,
2056 const struct NTFS_DE *e, bool trim)
2057{
2058 int err;
2059 struct indx_node *n;
2060 struct INDEX_HDR *hdr;
2061 CLST vbn = de_get_vbn(e);
2062 size_t i;
2063
2064 err = indx_read(indx, ni, vbn, &n);
2065 if (err)
2066 return err;
2067
2068 hdr = &n->index->ihdr;
2069 /* First, recurse into the children, if any.*/
2070 if (hdr_has_subnode(hdr)) {
2071 for (e = hdr_first_de(hdr); e; e = hdr_next_de(hdr, e)) {
2072 indx_free_children(indx, ni, e, false);
2073 if (de_is_last(e))
2074 break;
2075 }
2076 }
2077
2078 put_indx_node(n);
2079
2080 i = vbn >> indx->idx2vbn_bits;
2081 /* We've gotten rid of the children; add this buffer to the free list. */
2082 indx_mark_free(indx, ni, i);
2083
2084 if (!trim)
2085 return 0;
2086
2087 /*
2088 * If there are no used indexes after current free index
2089 * then we can truncate allocation and bitmap
2090 * Use bitmap to estimate the case
2091 */
2092 indx_shrink(indx, ni, i + 1);
2093 return 0;
2094}
2095
2096/*
2097 * indx_get_entry_to_replace
2098 *
2099 * finds a replacement entry for a deleted entry
2100 * always returns a node entry:
2101 * NTFS_IE_HAS_SUBNODES is set the flags and the size includes the sub_vcn
2102 */
2103static int indx_get_entry_to_replace(struct ntfs_index *indx,
2104 struct ntfs_inode *ni,
2105 const struct NTFS_DE *de_next,
2106 struct NTFS_DE **de_to_replace,
2107 struct ntfs_fnd *fnd)
2108{
2109 int err;
2110 int level = -1;
2111 CLST vbn;
2112 struct NTFS_DE *e, *te, *re;
2113 struct indx_node *n;
2114 struct INDEX_BUFFER *ib;
2115
2116 *de_to_replace = NULL;
2117
2118 /* Find first leaf entry down from de_next */
2119 vbn = de_get_vbn(de_next);
2120 for (;;) {
2121 n = NULL;
2122 err = indx_read(indx, ni, vbn, &n);
2123 if (err)
2124 goto out;
2125
2126 e = hdr_first_de(&n->index->ihdr);
2127 fnd_push(fnd, n, e);
2128
2129 if (!de_is_last(e)) {
2130 /*
2131 * This buffer is non-empty, so its first entry could be used as the
2132 * replacement entry.
2133 */
2134 level = fnd->level - 1;
2135 }
2136
2137 if (!de_has_vcn(e))
2138 break;
2139
2140 /* This buffer is a node. Continue to go down */
2141 vbn = de_get_vbn(e);
2142 }
2143
2144 if (level == -1)
2145 goto out;
2146
2147 n = fnd->nodes[level];
2148 te = hdr_first_de(&n->index->ihdr);
2149 /* Copy the candidate entry into the replacement entry buffer. */
195c52bd 2150 re = kmalloc(le16_to_cpu(te->size) + sizeof(u64), GFP_NOFS);
82cae269
KK
2151 if (!re) {
2152 err = -ENOMEM;
2153 goto out;
2154 }
2155
2156 *de_to_replace = re;
2157 memcpy(re, te, le16_to_cpu(te->size));
2158
2159 if (!de_has_vcn(re)) {
2160 /*
2161 * The replacement entry we found doesn't have a sub_vcn. increase its size
2162 * to hold one.
2163 */
2164 le16_add_cpu(&re->size, sizeof(u64));
2165 re->flags |= NTFS_IE_HAS_SUBNODES;
2166 } else {
2167 /*
2168 * The replacement entry we found was a node entry, which means that all
2169 * its child buffers are empty. Return them to the free pool.
2170 */
2171 indx_free_children(indx, ni, te, true);
2172 }
2173
2174 /*
2175 * Expunge the replacement entry from its former location,
2176 * and then write that buffer.
2177 */
2178 ib = n->index;
2179 e = hdr_delete_de(&ib->ihdr, te);
2180
2181 fnd->de[level] = e;
2182 indx_write(indx, ni, n, 0);
2183
2184 /* Check to see if this action created an empty leaf. */
2185 if (ib_is_leaf(ib) && ib_is_empty(ib))
2186 return 0;
2187
2188out:
2189 fnd_clear(fnd);
2190 return err;
2191}
2192
2193/*
2194 * indx_delete_entry
2195 *
2196 * deletes an entry from the index.
2197 */
2198int indx_delete_entry(struct ntfs_index *indx, struct ntfs_inode *ni,
2199 const void *key, u32 key_len, const void *ctx)
2200{
2201 int err, diff;
2202 struct INDEX_ROOT *root;
2203 struct INDEX_HDR *hdr;
2204 struct ntfs_fnd *fnd, *fnd2;
2205 struct INDEX_BUFFER *ib;
2206 struct NTFS_DE *e, *re, *next, *prev, *me;
2207 struct indx_node *n, *n2d = NULL;
2208 __le64 sub_vbn;
2209 int level, level2;
2210 struct ATTRIB *attr;
2211 struct mft_inode *mi;
2212 u32 e_size, root_size, new_root_size;
2213 size_t trim_bit;
2214 const struct INDEX_NAMES *in;
2215
2216 fnd = fnd_get();
2217 if (!fnd) {
2218 err = -ENOMEM;
2219 goto out2;
2220 }
2221
2222 fnd2 = fnd_get();
2223 if (!fnd2) {
2224 err = -ENOMEM;
2225 goto out1;
2226 }
2227
2228 root = indx_get_root(indx, ni, &attr, &mi);
2229 if (!root) {
2230 err = -EINVAL;
2231 goto out;
2232 }
2233
2234 /* Locate the entry to remove. */
2235 err = indx_find(indx, ni, root, key, key_len, ctx, &diff, &e, fnd);
2236 if (err)
2237 goto out;
2238
2239 if (!e || diff) {
2240 err = -ENOENT;
2241 goto out;
2242 }
2243
2244 level = fnd->level;
2245
2246 if (level) {
2247 n = fnd->nodes[level - 1];
2248 e = fnd->de[level - 1];
2249 ib = n->index;
2250 hdr = &ib->ihdr;
2251 } else {
2252 hdr = &root->ihdr;
2253 e = fnd->root_de;
2254 n = NULL;
2255 }
2256
2257 e_size = le16_to_cpu(e->size);
2258
2259 if (!de_has_vcn_ex(e)) {
2260 /* The entry to delete is a leaf, so we can just rip it out */
2261 hdr_delete_de(hdr, e);
2262
2263 if (!level) {
2264 hdr->total = hdr->used;
2265
2266 /* Shrink resident root attribute */
2267 mi_resize_attr(mi, attr, 0 - e_size);
2268 goto out;
2269 }
2270
2271 indx_write(indx, ni, n, 0);
2272
2273 /*
2274 * Check to see if removing that entry made
2275 * the leaf empty.
2276 */
2277 if (ib_is_leaf(ib) && ib_is_empty(ib)) {
2278 fnd_pop(fnd);
2279 fnd_push(fnd2, n, e);
2280 }
2281 } else {
2282 /*
2283 * The entry we wish to delete is a node buffer, so we
2284 * have to find a replacement for it.
2285 */
2286 next = de_get_next(e);
2287
2288 err = indx_get_entry_to_replace(indx, ni, next, &re, fnd2);
2289 if (err)
2290 goto out;
2291
2292 if (re) {
2293 de_set_vbn_le(re, de_get_vbn_le(e));
2294 hdr_delete_de(hdr, e);
2295
2296 err = level ? indx_insert_into_buffer(indx, ni, root,
2297 re, ctx,
2298 fnd->level - 1,
2299 fnd)
2300 : indx_insert_into_root(indx, ni, re, e,
2301 ctx, fnd);
195c52bd 2302 kfree(re);
82cae269
KK
2303
2304 if (err)
2305 goto out;
2306 } else {
2307 /*
2308 * There is no replacement for the current entry.
2309 * This means that the subtree rooted at its node is empty,
2310 * and can be deleted, which turn means that the node can
2311 * just inherit the deleted entry sub_vcn
2312 */
2313 indx_free_children(indx, ni, next, true);
2314
2315 de_set_vbn_le(next, de_get_vbn_le(e));
2316 hdr_delete_de(hdr, e);
2317 if (level) {
2318 indx_write(indx, ni, n, 0);
2319 } else {
2320 hdr->total = hdr->used;
2321
2322 /* Shrink resident root attribute */
2323 mi_resize_attr(mi, attr, 0 - e_size);
2324 }
2325 }
2326 }
2327
2328 /* Delete a branch of tree */
2329 if (!fnd2 || !fnd2->level)
2330 goto out;
2331
2332 /* Reinit root 'cause it can be changed */
2333 root = indx_get_root(indx, ni, &attr, &mi);
2334 if (!root) {
2335 err = -EINVAL;
2336 goto out;
2337 }
2338
2339 n2d = NULL;
2340 sub_vbn = fnd2->nodes[0]->index->vbn;
2341 level2 = 0;
2342 level = fnd->level;
2343
2344 hdr = level ? &fnd->nodes[level - 1]->index->ihdr : &root->ihdr;
2345
2346 /* Scan current level */
2347 for (e = hdr_first_de(hdr);; e = hdr_next_de(hdr, e)) {
2348 if (!e) {
2349 err = -EINVAL;
2350 goto out;
2351 }
2352
2353 if (de_has_vcn(e) && sub_vbn == de_get_vbn_le(e))
2354 break;
2355
2356 if (de_is_last(e)) {
2357 e = NULL;
2358 break;
2359 }
2360 }
2361
2362 if (!e) {
2363 /* Do slow search from root */
2364 struct indx_node *in;
2365
2366 fnd_clear(fnd);
2367
2368 in = indx_find_buffer(indx, ni, root, sub_vbn, NULL);
2369 if (IS_ERR(in)) {
2370 err = PTR_ERR(in);
2371 goto out;
2372 }
2373
2374 if (in)
2375 fnd_push(fnd, in, NULL);
2376 }
2377
2378 /* Merge fnd2 -> fnd */
2379 for (level = 0; level < fnd2->level; level++) {
2380 fnd_push(fnd, fnd2->nodes[level], fnd2->de[level]);
2381 fnd2->nodes[level] = NULL;
2382 }
2383 fnd2->level = 0;
2384
2385 hdr = NULL;
2386 for (level = fnd->level; level; level--) {
2387 struct indx_node *in = fnd->nodes[level - 1];
2388
2389 ib = in->index;
2390 if (ib_is_empty(ib)) {
2391 sub_vbn = ib->vbn;
2392 } else {
2393 hdr = &ib->ihdr;
2394 n2d = in;
2395 level2 = level;
2396 break;
2397 }
2398 }
2399
2400 if (!hdr)
2401 hdr = &root->ihdr;
2402
2403 e = hdr_first_de(hdr);
2404 if (!e) {
2405 err = -EINVAL;
2406 goto out;
2407 }
2408
2409 if (hdr != &root->ihdr || !de_is_last(e)) {
2410 prev = NULL;
2411 while (!de_is_last(e)) {
2412 if (de_has_vcn(e) && sub_vbn == de_get_vbn_le(e))
2413 break;
2414 prev = e;
2415 e = hdr_next_de(hdr, e);
2416 if (!e) {
2417 err = -EINVAL;
2418 goto out;
2419 }
2420 }
2421
2422 if (sub_vbn != de_get_vbn_le(e)) {
2423 /*
2424 * Didn't find the parent entry, although this buffer is the parent trail.
2425 * Something is corrupt.
2426 */
2427 err = -EINVAL;
2428 goto out;
2429 }
2430
2431 if (de_is_last(e)) {
2432 /*
2433 * Since we can't remove the end entry, we'll remove its
2434 * predecessor instead. This means we have to transfer the
2435 * predecessor's sub_vcn to the end entry.
2436 * Note: that this index block is not empty, so the
2437 * predecessor must exist
2438 */
2439 if (!prev) {
2440 err = -EINVAL;
2441 goto out;
2442 }
2443
2444 if (de_has_vcn(prev)) {
2445 de_set_vbn_le(e, de_get_vbn_le(prev));
2446 } else if (de_has_vcn(e)) {
2447 le16_sub_cpu(&e->size, sizeof(u64));
2448 e->flags &= ~NTFS_IE_HAS_SUBNODES;
2449 le32_sub_cpu(&hdr->used, sizeof(u64));
2450 }
2451 e = prev;
2452 }
2453
2454 /*
2455 * Copy the current entry into a temporary buffer (stripping off its
2456 * down-pointer, if any) and delete it from the current buffer or root,
2457 * as appropriate.
2458 */
2459 e_size = le16_to_cpu(e->size);
195c52bd 2460 me = kmemdup(e, e_size, GFP_NOFS);
82cae269
KK
2461 if (!me) {
2462 err = -ENOMEM;
2463 goto out;
2464 }
2465
2466 if (de_has_vcn(me)) {
2467 me->flags &= ~NTFS_IE_HAS_SUBNODES;
2468 le16_sub_cpu(&me->size, sizeof(u64));
2469 }
2470
2471 hdr_delete_de(hdr, e);
2472
2473 if (hdr == &root->ihdr) {
2474 level = 0;
2475 hdr->total = hdr->used;
2476
2477 /* Shrink resident root attribute */
2478 mi_resize_attr(mi, attr, 0 - e_size);
2479 } else {
2480 indx_write(indx, ni, n2d, 0);
2481 level = level2;
2482 }
2483
2484 /* Mark unused buffers as free */
2485 trim_bit = -1;
2486 for (; level < fnd->level; level++) {
2487 ib = fnd->nodes[level]->index;
2488 if (ib_is_empty(ib)) {
2489 size_t k = le64_to_cpu(ib->vbn) >>
2490 indx->idx2vbn_bits;
2491
2492 indx_mark_free(indx, ni, k);
2493 if (k < trim_bit)
2494 trim_bit = k;
2495 }
2496 }
2497
2498 fnd_clear(fnd);
2499 /*fnd->root_de = NULL;*/
2500
2501 /*
2502 * Re-insert the entry into the tree.
2503 * Find the spot the tree where we want to insert the new entry.
2504 */
2505 err = indx_insert_entry(indx, ni, me, ctx, fnd);
195c52bd 2506 kfree(me);
82cae269
KK
2507 if (err)
2508 goto out;
2509
2510 if (trim_bit != -1)
2511 indx_shrink(indx, ni, trim_bit);
2512 } else {
2513 /*
2514 * This tree needs to be collapsed down to an empty root.
2515 * Recreate the index root as an empty leaf and free all the bits the
2516 * index allocation bitmap.
2517 */
2518 fnd_clear(fnd);
2519 fnd_clear(fnd2);
2520
2521 in = &s_index_names[indx->type];
2522
2523 err = attr_set_size(ni, ATTR_ALLOC, in->name, in->name_len,
2524 &indx->alloc_run, 0, NULL, false, NULL);
2525 err = ni_remove_attr(ni, ATTR_ALLOC, in->name, in->name_len,
2526 false, NULL);
2527 run_close(&indx->alloc_run);
2528
2529 err = attr_set_size(ni, ATTR_BITMAP, in->name, in->name_len,
2530 &indx->bitmap_run, 0, NULL, false, NULL);
2531 err = ni_remove_attr(ni, ATTR_BITMAP, in->name, in->name_len,
2532 false, NULL);
2533 run_close(&indx->bitmap_run);
2534
2535 root = indx_get_root(indx, ni, &attr, &mi);
2536 if (!root) {
2537 err = -EINVAL;
2538 goto out;
2539 }
2540
2541 root_size = le32_to_cpu(attr->res.data_size);
2542 new_root_size =
2543 sizeof(struct INDEX_ROOT) + sizeof(struct NTFS_DE);
2544
2545 if (new_root_size != root_size &&
2546 !mi_resize_attr(mi, attr, new_root_size - root_size)) {
2547 err = -EINVAL;
2548 goto out;
2549 }
2550
2551 /* Fill first entry */
2552 e = (struct NTFS_DE *)(root + 1);
2553 e->ref.low = 0;
2554 e->ref.high = 0;
2555 e->ref.seq = 0;
2556 e->size = cpu_to_le16(sizeof(struct NTFS_DE));
2557 e->flags = NTFS_IE_LAST; // 0x02
2558 e->key_size = 0;
2559 e->res = 0;
2560
2561 hdr = &root->ihdr;
2562 hdr->flags = 0;
2563 hdr->used = hdr->total = cpu_to_le32(
2564 new_root_size - offsetof(struct INDEX_ROOT, ihdr));
2565 mi->dirty = true;
2566 }
2567
2568out:
2569 fnd_put(fnd2);
2570out1:
2571 fnd_put(fnd);
2572out2:
2573 return err;
2574}
2575
2576/*
2577 * Update duplicated information in directory entry
2578 * 'dup' - info from MFT record
2579 */
2580int indx_update_dup(struct ntfs_inode *ni, struct ntfs_sb_info *sbi,
2581 const struct ATTR_FILE_NAME *fname,
2582 const struct NTFS_DUP_INFO *dup, int sync)
2583{
2584 int err, diff;
2585 struct NTFS_DE *e = NULL;
2586 struct ATTR_FILE_NAME *e_fname;
2587 struct ntfs_fnd *fnd;
2588 struct INDEX_ROOT *root;
2589 struct mft_inode *mi;
2590 struct ntfs_index *indx = &ni->dir;
2591
2592 fnd = fnd_get();
2593 if (!fnd) {
2594 err = -ENOMEM;
2595 goto out1;
2596 }
2597
2598 root = indx_get_root(indx, ni, NULL, &mi);
2599 if (!root) {
2600 err = -EINVAL;
2601 goto out;
2602 }
2603
2604 /* Find entry in directory */
2605 err = indx_find(indx, ni, root, fname, fname_full_size(fname), sbi,
2606 &diff, &e, fnd);
2607 if (err)
2608 goto out;
2609
2610 if (!e) {
2611 err = -EINVAL;
2612 goto out;
2613 }
2614
2615 if (diff) {
2616 err = -EINVAL;
2617 goto out;
2618 }
2619
2620 e_fname = (struct ATTR_FILE_NAME *)(e + 1);
2621
2622 if (!memcmp(&e_fname->dup, dup, sizeof(*dup))) {
2623 /* nothing to update in index! Try to avoid this call */
2624 goto out;
2625 }
2626
2627 memcpy(&e_fname->dup, dup, sizeof(*dup));
2628
2629 if (fnd->level) {
2630 /* directory entry in index */
2631 err = indx_write(indx, ni, fnd->nodes[fnd->level - 1], sync);
2632 } else {
2633 /* directory entry in directory MFT record */
2634 mi->dirty = true;
2635 if (sync)
2636 err = mi_write(mi, 1);
2637 else
2638 mark_inode_dirty(&ni->vfs_inode);
2639 }
2640
2641out:
2642 fnd_put(fnd);
2643
2644out1:
2645 return err;
2646}