blob: 5fb41c9c89100aee4d6d0e8f7cf65556aec11250 [file] [log] [blame]
Konstantin Komarov82cae262021-08-13 17:21:29 +03001// 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 }
Gustavo A. R. Silvaabfeb2e2021-08-18 17:21:46 -0500178 break;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300179 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;
560 u16 esize = le16_to_cpu(e->size);
561
562 if (!e || de_is_last(e))
563 return NULL;
564
565 for (o = le32_to_cpu(hdr->de_off) + esize; o < used_2; o += esize) {
566 const struct NTFS_DE *p = e;
567
568 e = Add2Ptr(hdr, o);
569
570 /* We must not return END entry */
571 if (de_is_last(e))
572 return p;
573
574 esize = le16_to_cpu(e->size);
575 }
576
577 return e;
578}
579
580/*
581 * hdr_insert_head
582 *
583 * inserts some entries at the beginning of the buffer.
584 * It is used to insert entries into a newly-created buffer.
585 */
586static const struct NTFS_DE *hdr_insert_head(struct INDEX_HDR *hdr,
587 const void *ins, u32 ins_bytes)
588{
589 u32 to_move;
590 struct NTFS_DE *e = hdr_first_de(hdr);
591 u32 used = le32_to_cpu(hdr->used);
592
593 if (!e)
594 return NULL;
595
596 /* Now we just make room for the inserted entries and jam it in. */
597 to_move = used - le32_to_cpu(hdr->de_off);
598 memmove(Add2Ptr(e, ins_bytes), e, to_move);
599 memcpy(e, ins, ins_bytes);
600 hdr->used = cpu_to_le32(used + ins_bytes);
601
602 return e;
603}
604
605void fnd_clear(struct ntfs_fnd *fnd)
606{
607 int i;
608
609 for (i = 0; i < fnd->level; i++) {
610 struct indx_node *n = fnd->nodes[i];
611
612 if (!n)
613 continue;
614
615 put_indx_node(n);
616 fnd->nodes[i] = NULL;
617 }
618 fnd->level = 0;
619 fnd->root_de = NULL;
620}
621
622static int fnd_push(struct ntfs_fnd *fnd, struct indx_node *n,
623 struct NTFS_DE *e)
624{
625 int i;
626
627 i = fnd->level;
628 if (i < 0 || i >= ARRAY_SIZE(fnd->nodes))
629 return -EINVAL;
630 fnd->nodes[i] = n;
631 fnd->de[i] = e;
632 fnd->level += 1;
633 return 0;
634}
635
636static struct indx_node *fnd_pop(struct ntfs_fnd *fnd)
637{
638 struct indx_node *n;
639 int i = fnd->level;
640
641 i -= 1;
642 n = fnd->nodes[i];
643 fnd->nodes[i] = NULL;
644 fnd->level = i;
645
646 return n;
647}
648
649static bool fnd_is_empty(struct ntfs_fnd *fnd)
650{
651 if (!fnd->level)
652 return !fnd->root_de;
653
654 return !fnd->de[fnd->level - 1];
655}
656
657/*
658 * hdr_find_e
659 *
660 * locates an entry the index buffer.
661 * If no matching entry is found, it returns the first entry which is greater
662 * than the desired entry If the search key is greater than all the entries the
663 * buffer, it returns the 'end' entry. This function does a binary search of the
664 * current index buffer, for the first entry that is <= to the search value
665 * Returns NULL if error
666 */
667static struct NTFS_DE *hdr_find_e(const struct ntfs_index *indx,
668 const struct INDEX_HDR *hdr, const void *key,
669 size_t key_len, const void *ctx, int *diff)
670{
671 struct NTFS_DE *e;
672 NTFS_CMP_FUNC cmp = indx->cmp;
673 u32 e_size, e_key_len;
674 u32 end = le32_to_cpu(hdr->used);
675 u32 off = le32_to_cpu(hdr->de_off);
676
677#ifdef NTFS3_INDEX_BINARY_SEARCH
678 int max_idx = 0, fnd, min_idx;
679 int nslots = 64;
680 u16 *offs;
681
682 if (end > 0x10000)
683 goto next;
684
Kari Argillander195c52b2021-08-24 21:37:07 +0300685 offs = kmalloc(sizeof(u16) * nslots, GFP_NOFS);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300686 if (!offs)
687 goto next;
688
689 /* use binary search algorithm */
690next1:
691 if (off + sizeof(struct NTFS_DE) > end) {
692 e = NULL;
693 goto out1;
694 }
695 e = Add2Ptr(hdr, off);
696 e_size = le16_to_cpu(e->size);
697
698 if (e_size < sizeof(struct NTFS_DE) || off + e_size > end) {
699 e = NULL;
700 goto out1;
701 }
702
703 if (max_idx >= nslots) {
704 u16 *ptr;
Kari Argillanderfa3cacf2021-08-26 11:56:29 +0300705 int new_slots = ALIGN(2 * nslots, 8);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300706
Kari Argillander195c52b2021-08-24 21:37:07 +0300707 ptr = kmalloc(sizeof(u16) * new_slots, GFP_NOFS);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300708 if (ptr)
709 memcpy(ptr, offs, sizeof(u16) * max_idx);
Kari Argillander195c52b2021-08-24 21:37:07 +0300710 kfree(offs);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300711 offs = ptr;
712 nslots = new_slots;
713 if (!ptr)
714 goto next;
715 }
716
717 /* Store entry table */
718 offs[max_idx] = off;
719
720 if (!de_is_last(e)) {
721 off += e_size;
722 max_idx += 1;
723 goto next1;
724 }
725
726 /*
727 * Table of pointers is created
728 * Use binary search to find entry that is <= to the search value
729 */
730 fnd = -1;
731 min_idx = 0;
732
733 while (min_idx <= max_idx) {
734 int mid_idx = min_idx + ((max_idx - min_idx) >> 1);
735 int diff2;
736
737 e = Add2Ptr(hdr, offs[mid_idx]);
738
739 e_key_len = le16_to_cpu(e->key_size);
740
741 diff2 = (*cmp)(key, key_len, e + 1, e_key_len, ctx);
742
743 if (!diff2) {
744 *diff = 0;
745 goto out1;
746 }
747
748 if (diff2 < 0) {
749 max_idx = mid_idx - 1;
750 fnd = mid_idx;
751 if (!fnd)
752 break;
753 } else {
754 min_idx = mid_idx + 1;
755 }
756 }
757
758 if (fnd == -1) {
759 e = NULL;
760 goto out1;
761 }
762
763 *diff = -1;
764 e = Add2Ptr(hdr, offs[fnd]);
765
766out1:
Kari Argillander195c52b2021-08-24 21:37:07 +0300767 kfree(offs);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300768
769 return e;
770#endif
771
772next:
773 /*
774 * Entries index are sorted
775 * Enumerate all entries until we find entry that is <= to the search value
776 */
777 if (off + sizeof(struct NTFS_DE) > end)
778 return NULL;
779
780 e = Add2Ptr(hdr, off);
781 e_size = le16_to_cpu(e->size);
782
783 if (e_size < sizeof(struct NTFS_DE) || off + e_size > end)
784 return NULL;
785
786 off += e_size;
787
788 e_key_len = le16_to_cpu(e->key_size);
789
790 *diff = (*cmp)(key, key_len, e + 1, e_key_len, ctx);
791 if (!*diff)
792 return e;
793
794 if (*diff <= 0)
795 return e;
796
797 if (de_is_last(e)) {
798 *diff = 1;
799 return e;
800 }
801 goto next;
802}
803
804/*
805 * hdr_insert_de
806 *
807 * inserts an index entry into the buffer.
808 * 'before' should be a pointer previously returned from hdr_find_e
809 */
810static struct NTFS_DE *hdr_insert_de(const struct ntfs_index *indx,
811 struct INDEX_HDR *hdr,
812 const struct NTFS_DE *de,
813 struct NTFS_DE *before, const void *ctx)
814{
815 int diff;
816 size_t off = PtrOffset(hdr, before);
817 u32 used = le32_to_cpu(hdr->used);
818 u32 total = le32_to_cpu(hdr->total);
819 u16 de_size = le16_to_cpu(de->size);
820
821 /* First, check to see if there's enough room */
822 if (used + de_size > total)
823 return NULL;
824
825 /* We know there's enough space, so we know we'll succeed. */
826 if (before) {
827 /* Check that before is inside Index */
828 if (off >= used || off < le32_to_cpu(hdr->de_off) ||
829 off + le16_to_cpu(before->size) > total) {
830 return NULL;
831 }
832 goto ok;
833 }
834 /* No insert point is applied. Get it manually */
835 before = hdr_find_e(indx, hdr, de + 1, le16_to_cpu(de->key_size), ctx,
836 &diff);
837 if (!before)
838 return NULL;
839 off = PtrOffset(hdr, before);
840
841ok:
842 /* Now we just make room for the entry and jam it in. */
843 memmove(Add2Ptr(before, de_size), before, used - off);
844
845 hdr->used = cpu_to_le32(used + de_size);
846 memcpy(before, de, de_size);
847
848 return before;
849}
850
851/*
852 * hdr_delete_de
853 *
854 * removes an entry from the index buffer
855 */
856static inline struct NTFS_DE *hdr_delete_de(struct INDEX_HDR *hdr,
857 struct NTFS_DE *re)
858{
859 u32 used = le32_to_cpu(hdr->used);
860 u16 esize = le16_to_cpu(re->size);
861 u32 off = PtrOffset(hdr, re);
862 int bytes = used - (off + esize);
863
864 if (off >= used || esize < sizeof(struct NTFS_DE) ||
865 bytes < sizeof(struct NTFS_DE))
866 return NULL;
867
868 hdr->used = cpu_to_le32(used - esize);
869 memmove(re, Add2Ptr(re, esize), bytes);
870
871 return re;
872}
873
874void indx_clear(struct ntfs_index *indx)
875{
876 run_close(&indx->alloc_run);
877 run_close(&indx->bitmap_run);
878}
879
880int indx_init(struct ntfs_index *indx, struct ntfs_sb_info *sbi,
881 const struct ATTRIB *attr, enum index_mutex_classed type)
882{
883 u32 t32;
884 const struct INDEX_ROOT *root = resident_data(attr);
885
886 /* Check root fields */
887 if (!root->index_block_clst)
888 return -EINVAL;
889
890 indx->type = type;
891 indx->idx2vbn_bits = __ffs(root->index_block_clst);
892
893 t32 = le32_to_cpu(root->index_block_size);
894 indx->index_bits = blksize_bits(t32);
895
896 /* Check index record size */
897 if (t32 < sbi->cluster_size) {
898 /* index record is smaller than a cluster, use 512 blocks */
899 if (t32 != root->index_block_clst * SECTOR_SIZE)
900 return -EINVAL;
901
902 /* Check alignment to a cluster */
903 if ((sbi->cluster_size >> SECTOR_SHIFT) &
904 (root->index_block_clst - 1)) {
905 return -EINVAL;
906 }
907
908 indx->vbn2vbo_bits = SECTOR_SHIFT;
909 } else {
910 /* index record must be a multiple of cluster size */
911 if (t32 != root->index_block_clst << sbi->cluster_bits)
912 return -EINVAL;
913
914 indx->vbn2vbo_bits = sbi->cluster_bits;
915 }
916
917 init_rwsem(&indx->run_lock);
918
919 indx->cmp = get_cmp_func(root);
920 return indx->cmp ? 0 : -EINVAL;
921}
922
923static struct indx_node *indx_new(struct ntfs_index *indx,
924 struct ntfs_inode *ni, CLST vbn,
925 const __le64 *sub_vbn)
926{
927 int err;
928 struct NTFS_DE *e;
929 struct indx_node *r;
930 struct INDEX_HDR *hdr;
931 struct INDEX_BUFFER *index;
932 u64 vbo = (u64)vbn << indx->vbn2vbo_bits;
933 u32 bytes = 1u << indx->index_bits;
934 u16 fn;
935 u32 eo;
936
Kari Argillander195c52b2021-08-24 21:37:07 +0300937 r = kzalloc(sizeof(struct indx_node), GFP_NOFS);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300938 if (!r)
939 return ERR_PTR(-ENOMEM);
940
Kari Argillander195c52b2021-08-24 21:37:07 +0300941 index = kzalloc(bytes, GFP_NOFS);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300942 if (!index) {
Kari Argillander195c52b2021-08-24 21:37:07 +0300943 kfree(r);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300944 return ERR_PTR(-ENOMEM);
945 }
946
947 err = ntfs_get_bh(ni->mi.sbi, &indx->alloc_run, vbo, bytes, &r->nb);
948
949 if (err) {
Kari Argillander195c52b2021-08-24 21:37:07 +0300950 kfree(index);
951 kfree(r);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300952 return ERR_PTR(err);
953 }
954
955 /* Create header */
956 index->rhdr.sign = NTFS_INDX_SIGNATURE;
957 index->rhdr.fix_off = cpu_to_le16(sizeof(struct INDEX_BUFFER)); // 0x28
958 fn = (bytes >> SECTOR_SHIFT) + 1; // 9
959 index->rhdr.fix_num = cpu_to_le16(fn);
960 index->vbn = cpu_to_le64(vbn);
961 hdr = &index->ihdr;
Kari Argillanderfa3cacf2021-08-26 11:56:29 +0300962 eo = ALIGN(sizeof(struct INDEX_BUFFER) + fn * sizeof(short), 8);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300963 hdr->de_off = cpu_to_le32(eo);
964
965 e = Add2Ptr(hdr, eo);
966
967 if (sub_vbn) {
968 e->flags = NTFS_IE_LAST | NTFS_IE_HAS_SUBNODES;
969 e->size = cpu_to_le16(sizeof(struct NTFS_DE) + sizeof(u64));
970 hdr->used =
971 cpu_to_le32(eo + sizeof(struct NTFS_DE) + sizeof(u64));
972 de_set_vbn_le(e, *sub_vbn);
973 hdr->flags = 1;
974 } else {
975 e->size = cpu_to_le16(sizeof(struct NTFS_DE));
976 hdr->used = cpu_to_le32(eo + sizeof(struct NTFS_DE));
977 e->flags = NTFS_IE_LAST;
978 }
979
980 hdr->total = cpu_to_le32(bytes - offsetof(struct INDEX_BUFFER, ihdr));
981
982 r->index = index;
983 return r;
984}
985
986struct INDEX_ROOT *indx_get_root(struct ntfs_index *indx, struct ntfs_inode *ni,
987 struct ATTRIB **attr, struct mft_inode **mi)
988{
989 struct ATTR_LIST_ENTRY *le = NULL;
990 struct ATTRIB *a;
991 const struct INDEX_NAMES *in = &s_index_names[indx->type];
992
993 a = ni_find_attr(ni, NULL, &le, ATTR_ROOT, in->name, in->name_len, NULL,
994 mi);
995 if (!a)
996 return NULL;
997
998 if (attr)
999 *attr = a;
1000
1001 return resident_data_ex(a, sizeof(struct INDEX_ROOT));
1002}
1003
1004static int indx_write(struct ntfs_index *indx, struct ntfs_inode *ni,
1005 struct indx_node *node, int sync)
1006{
1007 struct INDEX_BUFFER *ib = node->index;
1008
1009 return ntfs_write_bh(ni->mi.sbi, &ib->rhdr, &node->nb, sync);
1010}
1011
1012/*
1013 * if ntfs_readdir calls this function
1014 * inode is shared locked and no ni_lock
1015 * use rw_semaphore for read/write access to alloc_run
1016 */
1017int indx_read(struct ntfs_index *indx, struct ntfs_inode *ni, CLST vbn,
1018 struct indx_node **node)
1019{
1020 int err;
1021 struct INDEX_BUFFER *ib;
1022 struct runs_tree *run = &indx->alloc_run;
1023 struct rw_semaphore *lock = &indx->run_lock;
1024 u64 vbo = (u64)vbn << indx->vbn2vbo_bits;
1025 u32 bytes = 1u << indx->index_bits;
1026 struct indx_node *in = *node;
1027 const struct INDEX_NAMES *name;
1028
1029 if (!in) {
Kari Argillander195c52b2021-08-24 21:37:07 +03001030 in = kzalloc(sizeof(struct indx_node), GFP_NOFS);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001031 if (!in)
1032 return -ENOMEM;
1033 } else {
1034 nb_put(&in->nb);
1035 }
1036
1037 ib = in->index;
1038 if (!ib) {
Kari Argillander195c52b2021-08-24 21:37:07 +03001039 ib = kmalloc(bytes, GFP_NOFS);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001040 if (!ib) {
1041 err = -ENOMEM;
1042 goto out;
1043 }
1044 }
1045
1046 down_read(lock);
1047 err = ntfs_read_bh(ni->mi.sbi, run, vbo, &ib->rhdr, bytes, &in->nb);
1048 up_read(lock);
1049 if (!err)
1050 goto ok;
1051
1052 if (err == -E_NTFS_FIXUP)
1053 goto ok;
1054
1055 if (err != -ENOENT)
1056 goto out;
1057
1058 name = &s_index_names[indx->type];
1059 down_write(lock);
1060 err = attr_load_runs_range(ni, ATTR_ALLOC, name->name, name->name_len,
1061 run, vbo, vbo + bytes);
1062 up_write(lock);
1063 if (err)
1064 goto out;
1065
1066 down_read(lock);
1067 err = ntfs_read_bh(ni->mi.sbi, run, vbo, &ib->rhdr, bytes, &in->nb);
1068 up_read(lock);
1069 if (err == -E_NTFS_FIXUP)
1070 goto ok;
1071
1072 if (err)
1073 goto out;
1074
1075ok:
1076 if (err == -E_NTFS_FIXUP) {
1077 ntfs_write_bh(ni->mi.sbi, &ib->rhdr, &in->nb, 0);
1078 err = 0;
1079 }
1080
1081 in->index = ib;
1082 *node = in;
1083
1084out:
1085 if (ib != in->index)
Kari Argillander195c52b2021-08-24 21:37:07 +03001086 kfree(ib);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001087
1088 if (*node != in) {
1089 nb_put(&in->nb);
Kari Argillander195c52b2021-08-24 21:37:07 +03001090 kfree(in);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001091 }
1092
1093 return err;
1094}
1095
1096/*
1097 * indx_find
1098 *
1099 * scans NTFS directory for given entry
1100 */
1101int indx_find(struct ntfs_index *indx, struct ntfs_inode *ni,
1102 const struct INDEX_ROOT *root, const void *key, size_t key_len,
1103 const void *ctx, int *diff, struct NTFS_DE **entry,
1104 struct ntfs_fnd *fnd)
1105{
1106 int err;
1107 struct NTFS_DE *e;
1108 const struct INDEX_HDR *hdr;
1109 struct indx_node *node;
1110
1111 if (!root)
1112 root = indx_get_root(&ni->dir, ni, NULL, NULL);
1113
1114 if (!root) {
1115 err = -EINVAL;
1116 goto out;
1117 }
1118
1119 hdr = &root->ihdr;
1120
1121 /* Check cache */
1122 e = fnd->level ? fnd->de[fnd->level - 1] : fnd->root_de;
1123 if (e && !de_is_last(e) &&
1124 !(*indx->cmp)(key, key_len, e + 1, le16_to_cpu(e->key_size), ctx)) {
1125 *entry = e;
1126 *diff = 0;
1127 return 0;
1128 }
1129
1130 /* Soft finder reset */
1131 fnd_clear(fnd);
1132
1133 /* Lookup entry that is <= to the search value */
1134 e = hdr_find_e(indx, hdr, key, key_len, ctx, diff);
1135 if (!e)
1136 return -EINVAL;
1137
1138 if (fnd)
1139 fnd->root_de = e;
1140
1141 err = 0;
1142
1143 for (;;) {
1144 node = NULL;
1145 if (*diff >= 0 || !de_has_vcn_ex(e)) {
1146 *entry = e;
1147 goto out;
1148 }
1149
1150 /* Read next level. */
1151 err = indx_read(indx, ni, de_get_vbn(e), &node);
1152 if (err)
1153 goto out;
1154
1155 /* Lookup entry that is <= to the search value */
1156 e = hdr_find_e(indx, &node->index->ihdr, key, key_len, ctx,
1157 diff);
1158 if (!e) {
1159 err = -EINVAL;
1160 put_indx_node(node);
1161 goto out;
1162 }
1163
1164 fnd_push(fnd, node, e);
1165 }
1166
1167out:
1168 return err;
1169}
1170
1171int indx_find_sort(struct ntfs_index *indx, struct ntfs_inode *ni,
1172 const struct INDEX_ROOT *root, struct NTFS_DE **entry,
1173 struct ntfs_fnd *fnd)
1174{
1175 int err;
1176 struct indx_node *n = NULL;
1177 struct NTFS_DE *e;
1178 size_t iter = 0;
1179 int level = fnd->level;
1180
1181 if (!*entry) {
1182 /* Start find */
1183 e = hdr_first_de(&root->ihdr);
1184 if (!e)
1185 return 0;
1186 fnd_clear(fnd);
1187 fnd->root_de = e;
1188 } else if (!level) {
1189 if (de_is_last(fnd->root_de)) {
1190 *entry = NULL;
1191 return 0;
1192 }
1193
1194 e = hdr_next_de(&root->ihdr, fnd->root_de);
1195 if (!e)
1196 return -EINVAL;
1197 fnd->root_de = e;
1198 } else {
1199 n = fnd->nodes[level - 1];
1200 e = fnd->de[level - 1];
1201
1202 if (de_is_last(e))
1203 goto pop_level;
1204
1205 e = hdr_next_de(&n->index->ihdr, e);
1206 if (!e)
1207 return -EINVAL;
1208
1209 fnd->de[level - 1] = e;
1210 }
1211
1212 /* Just to avoid tree cycle */
1213next_iter:
1214 if (iter++ >= 1000)
1215 return -EINVAL;
1216
1217 while (de_has_vcn_ex(e)) {
1218 if (le16_to_cpu(e->size) <
1219 sizeof(struct NTFS_DE) + sizeof(u64)) {
1220 if (n) {
1221 fnd_pop(fnd);
Kari Argillander195c52b2021-08-24 21:37:07 +03001222 kfree(n);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001223 }
1224 return -EINVAL;
1225 }
1226
1227 /* Read next level */
1228 err = indx_read(indx, ni, de_get_vbn(e), &n);
1229 if (err)
1230 return err;
1231
1232 /* Try next level */
1233 e = hdr_first_de(&n->index->ihdr);
1234 if (!e) {
Kari Argillander195c52b2021-08-24 21:37:07 +03001235 kfree(n);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001236 return -EINVAL;
1237 }
1238
1239 fnd_push(fnd, n, e);
1240 }
1241
1242 if (le16_to_cpu(e->size) > sizeof(struct NTFS_DE)) {
1243 *entry = e;
1244 return 0;
1245 }
1246
1247pop_level:
1248 for (;;) {
1249 if (!de_is_last(e))
1250 goto next_iter;
1251
1252 /* Pop one level */
1253 if (n) {
1254 fnd_pop(fnd);
Kari Argillander195c52b2021-08-24 21:37:07 +03001255 kfree(n);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001256 }
1257
1258 level = fnd->level;
1259
1260 if (level) {
1261 n = fnd->nodes[level - 1];
1262 e = fnd->de[level - 1];
1263 } else if (fnd->root_de) {
1264 n = NULL;
1265 e = fnd->root_de;
1266 fnd->root_de = NULL;
1267 } else {
1268 *entry = NULL;
1269 return 0;
1270 }
1271
1272 if (le16_to_cpu(e->size) > sizeof(struct NTFS_DE)) {
1273 *entry = e;
1274 if (!fnd->root_de)
1275 fnd->root_de = e;
1276 return 0;
1277 }
1278 }
1279}
1280
1281int indx_find_raw(struct ntfs_index *indx, struct ntfs_inode *ni,
1282 const struct INDEX_ROOT *root, struct NTFS_DE **entry,
1283 size_t *off, struct ntfs_fnd *fnd)
1284{
1285 int err;
1286 struct indx_node *n = NULL;
1287 struct NTFS_DE *e = NULL;
1288 struct NTFS_DE *e2;
1289 size_t bit;
1290 CLST next_used_vbn;
1291 CLST next_vbn;
1292 u32 record_size = ni->mi.sbi->record_size;
1293
1294 /* Use non sorted algorithm */
1295 if (!*entry) {
1296 /* This is the first call */
1297 e = hdr_first_de(&root->ihdr);
1298 if (!e)
1299 return 0;
1300 fnd_clear(fnd);
1301 fnd->root_de = e;
1302
1303 /* The first call with setup of initial element */
1304 if (*off >= record_size) {
1305 next_vbn = (((*off - record_size) >> indx->index_bits))
1306 << indx->idx2vbn_bits;
1307 /* jump inside cycle 'for'*/
1308 goto next;
1309 }
1310
1311 /* Start enumeration from root */
1312 *off = 0;
1313 } else if (!fnd->root_de)
1314 return -EINVAL;
1315
1316 for (;;) {
1317 /* Check if current entry can be used */
1318 if (e && le16_to_cpu(e->size) > sizeof(struct NTFS_DE))
1319 goto ok;
1320
1321 if (!fnd->level) {
1322 /* Continue to enumerate root */
1323 if (!de_is_last(fnd->root_de)) {
1324 e = hdr_next_de(&root->ihdr, fnd->root_de);
1325 if (!e)
1326 return -EINVAL;
1327 fnd->root_de = e;
1328 continue;
1329 }
1330
1331 /* Start to enumerate indexes from 0 */
1332 next_vbn = 0;
1333 } else {
1334 /* Continue to enumerate indexes */
1335 e2 = fnd->de[fnd->level - 1];
1336
1337 n = fnd->nodes[fnd->level - 1];
1338
1339 if (!de_is_last(e2)) {
1340 e = hdr_next_de(&n->index->ihdr, e2);
1341 if (!e)
1342 return -EINVAL;
1343 fnd->de[fnd->level - 1] = e;
1344 continue;
1345 }
1346
1347 /* Continue with next index */
1348 next_vbn = le64_to_cpu(n->index->vbn) +
1349 root->index_block_clst;
1350 }
1351
1352next:
1353 /* Release current index */
1354 if (n) {
1355 fnd_pop(fnd);
1356 put_indx_node(n);
1357 n = NULL;
1358 }
1359
1360 /* Skip all free indexes */
1361 bit = next_vbn >> indx->idx2vbn_bits;
1362 err = indx_used_bit(indx, ni, &bit);
1363 if (err == -ENOENT || bit == MINUS_ONE_T) {
1364 /* No used indexes */
1365 *entry = NULL;
1366 return 0;
1367 }
1368
1369 next_used_vbn = bit << indx->idx2vbn_bits;
1370
1371 /* Read buffer into memory */
1372 err = indx_read(indx, ni, next_used_vbn, &n);
1373 if (err)
1374 return err;
1375
1376 e = hdr_first_de(&n->index->ihdr);
1377 fnd_push(fnd, n, e);
1378 if (!e)
1379 return -EINVAL;
1380 }
1381
1382ok:
1383 /* return offset to restore enumerator if necessary */
1384 if (!n) {
1385 /* 'e' points in root */
1386 *off = PtrOffset(&root->ihdr, e);
1387 } else {
1388 /* 'e' points in index */
1389 *off = (le64_to_cpu(n->index->vbn) << indx->vbn2vbo_bits) +
1390 record_size + PtrOffset(&n->index->ihdr, e);
1391 }
1392
1393 *entry = e;
1394 return 0;
1395}
1396
1397/*
1398 * indx_create_allocate
1399 *
1400 * create "Allocation + Bitmap" attributes
1401 */
1402static int indx_create_allocate(struct ntfs_index *indx, struct ntfs_inode *ni,
1403 CLST *vbn)
1404{
1405 int err = -ENOMEM;
1406 struct ntfs_sb_info *sbi = ni->mi.sbi;
1407 struct ATTRIB *bitmap;
1408 struct ATTRIB *alloc;
1409 u32 data_size = 1u << indx->index_bits;
1410 u32 alloc_size = ntfs_up_cluster(sbi, data_size);
1411 CLST len = alloc_size >> sbi->cluster_bits;
1412 const struct INDEX_NAMES *in = &s_index_names[indx->type];
1413 CLST alen;
1414 struct runs_tree run;
1415
1416 run_init(&run);
1417
1418 err = attr_allocate_clusters(sbi, &run, 0, 0, len, NULL, 0, &alen, 0,
1419 NULL);
1420 if (err)
1421 goto out;
1422
1423 err = ni_insert_nonresident(ni, ATTR_ALLOC, in->name, in->name_len,
1424 &run, 0, len, 0, &alloc, NULL);
1425 if (err)
1426 goto out1;
1427
1428 alloc->nres.valid_size = alloc->nres.data_size = cpu_to_le64(data_size);
1429
1430 err = ni_insert_resident(ni, bitmap_size(1), ATTR_BITMAP, in->name,
1431 in->name_len, &bitmap, NULL);
1432 if (err)
1433 goto out2;
1434
1435 if (in->name == I30_NAME) {
1436 ni->vfs_inode.i_size = data_size;
1437 inode_set_bytes(&ni->vfs_inode, alloc_size);
1438 }
1439
1440 memcpy(&indx->alloc_run, &run, sizeof(run));
1441
1442 *vbn = 0;
1443
1444 return 0;
1445
1446out2:
1447 mi_remove_attr(&ni->mi, alloc);
1448
1449out1:
1450 run_deallocate(sbi, &run, false);
1451
1452out:
1453 return err;
1454}
1455
1456/*
1457 * indx_add_allocate
1458 *
1459 * add clusters to index
1460 */
1461static int indx_add_allocate(struct ntfs_index *indx, struct ntfs_inode *ni,
1462 CLST *vbn)
1463{
1464 int err;
1465 size_t bit;
1466 u64 data_size;
1467 u64 bmp_size, bmp_size_v;
1468 struct ATTRIB *bmp, *alloc;
1469 struct mft_inode *mi;
1470 const struct INDEX_NAMES *in = &s_index_names[indx->type];
1471
1472 err = indx_find_free(indx, ni, &bit, &bmp);
1473 if (err)
1474 goto out1;
1475
1476 if (bit != MINUS_ONE_T) {
1477 bmp = NULL;
1478 } else {
1479 if (bmp->non_res) {
1480 bmp_size = le64_to_cpu(bmp->nres.data_size);
1481 bmp_size_v = le64_to_cpu(bmp->nres.valid_size);
1482 } else {
1483 bmp_size = bmp_size_v = le32_to_cpu(bmp->res.data_size);
1484 }
1485
1486 bit = bmp_size << 3;
1487 }
1488
1489 data_size = (u64)(bit + 1) << indx->index_bits;
1490
1491 if (bmp) {
1492 /* Increase bitmap */
1493 err = attr_set_size(ni, ATTR_BITMAP, in->name, in->name_len,
1494 &indx->bitmap_run, bitmap_size(bit + 1),
1495 NULL, true, NULL);
1496 if (err)
1497 goto out1;
1498 }
1499
1500 alloc = ni_find_attr(ni, NULL, NULL, ATTR_ALLOC, in->name, in->name_len,
1501 NULL, &mi);
1502 if (!alloc) {
Dan Carpenter04810f02021-08-24 10:49:32 +03001503 err = -EINVAL;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001504 if (bmp)
1505 goto out2;
1506 goto out1;
1507 }
1508
1509 /* Increase allocation */
1510 err = attr_set_size(ni, ATTR_ALLOC, in->name, in->name_len,
1511 &indx->alloc_run, data_size, &data_size, true,
1512 NULL);
1513 if (err) {
1514 if (bmp)
1515 goto out2;
1516 goto out1;
1517 }
1518
1519 *vbn = bit << indx->idx2vbn_bits;
1520
1521 return 0;
1522
1523out2:
1524 /* Ops (no space?) */
1525 attr_set_size(ni, ATTR_BITMAP, in->name, in->name_len,
1526 &indx->bitmap_run, bmp_size, &bmp_size_v, false, NULL);
1527
1528out1:
1529 return err;
1530}
1531
1532/*
1533 * indx_insert_into_root
1534 *
1535 * attempts to insert an entry into the index root
1536 * If necessary, it will twiddle the index b-tree.
1537 */
1538static int indx_insert_into_root(struct ntfs_index *indx, struct ntfs_inode *ni,
1539 const struct NTFS_DE *new_de,
1540 struct NTFS_DE *root_de, const void *ctx,
1541 struct ntfs_fnd *fnd)
1542{
1543 int err = 0;
1544 struct NTFS_DE *e, *e0, *re;
1545 struct mft_inode *mi;
1546 struct ATTRIB *attr;
1547 struct MFT_REC *rec;
1548 struct INDEX_HDR *hdr;
1549 struct indx_node *n;
1550 CLST new_vbn;
1551 __le64 *sub_vbn, t_vbn;
1552 u16 new_de_size;
1553 u32 hdr_used, hdr_total, asize, used, to_move;
1554 u32 root_size, new_root_size;
1555 struct ntfs_sb_info *sbi;
1556 int ds_root;
1557 struct INDEX_ROOT *root, *a_root = NULL;
1558
1559 /* Get the record this root placed in */
1560 root = indx_get_root(indx, ni, &attr, &mi);
1561 if (!root)
1562 goto out;
1563
1564 /*
1565 * Try easy case:
1566 * hdr_insert_de will succeed if there's room the root for the new entry.
1567 */
1568 hdr = &root->ihdr;
1569 sbi = ni->mi.sbi;
1570 rec = mi->mrec;
1571 used = le32_to_cpu(rec->used);
1572 new_de_size = le16_to_cpu(new_de->size);
1573 hdr_used = le32_to_cpu(hdr->used);
1574 hdr_total = le32_to_cpu(hdr->total);
1575 asize = le32_to_cpu(attr->size);
1576 root_size = le32_to_cpu(attr->res.data_size);
1577
1578 ds_root = new_de_size + hdr_used - hdr_total;
1579
1580 if (used + ds_root < sbi->max_bytes_per_attr) {
1581 /* make a room for new elements */
1582 mi_resize_attr(mi, attr, ds_root);
1583 hdr->total = cpu_to_le32(hdr_total + ds_root);
1584 e = hdr_insert_de(indx, hdr, new_de, root_de, ctx);
1585 WARN_ON(!e);
1586 fnd_clear(fnd);
1587 fnd->root_de = e;
1588
1589 return 0;
1590 }
1591
1592 /* Make a copy of root attribute to restore if error */
Kari Argillander195c52b2021-08-24 21:37:07 +03001593 a_root = kmemdup(attr, asize, GFP_NOFS);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001594 if (!a_root) {
1595 err = -ENOMEM;
1596 goto out;
1597 }
1598
1599 /* copy all the non-end entries from the index root to the new buffer.*/
1600 to_move = 0;
1601 e0 = hdr_first_de(hdr);
1602
1603 /* Calculate the size to copy */
1604 for (e = e0;; e = hdr_next_de(hdr, e)) {
1605 if (!e) {
1606 err = -EINVAL;
1607 goto out;
1608 }
1609
1610 if (de_is_last(e))
1611 break;
1612 to_move += le16_to_cpu(e->size);
1613 }
1614
1615 n = NULL;
1616 if (!to_move) {
1617 re = NULL;
1618 } else {
Kari Argillander195c52b2021-08-24 21:37:07 +03001619 re = kmemdup(e0, to_move, GFP_NOFS);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001620 if (!re) {
1621 err = -ENOMEM;
1622 goto out;
1623 }
1624 }
1625
1626 sub_vbn = NULL;
1627 if (de_has_vcn(e)) {
1628 t_vbn = de_get_vbn_le(e);
1629 sub_vbn = &t_vbn;
1630 }
1631
1632 new_root_size = sizeof(struct INDEX_ROOT) + sizeof(struct NTFS_DE) +
1633 sizeof(u64);
1634 ds_root = new_root_size - root_size;
1635
1636 if (ds_root > 0 && used + ds_root > sbi->max_bytes_per_attr) {
1637 /* make root external */
1638 err = -EOPNOTSUPP;
1639 goto out;
1640 }
1641
1642 if (ds_root)
1643 mi_resize_attr(mi, attr, ds_root);
1644
1645 /* Fill first entry (vcn will be set later) */
1646 e = (struct NTFS_DE *)(root + 1);
1647 memset(e, 0, sizeof(struct NTFS_DE));
1648 e->size = cpu_to_le16(sizeof(struct NTFS_DE) + sizeof(u64));
1649 e->flags = NTFS_IE_HAS_SUBNODES | NTFS_IE_LAST;
1650
1651 hdr->flags = 1;
1652 hdr->used = hdr->total =
1653 cpu_to_le32(new_root_size - offsetof(struct INDEX_ROOT, ihdr));
1654
1655 fnd->root_de = hdr_first_de(hdr);
1656 mi->dirty = true;
1657
1658 /* Create alloc and bitmap attributes (if not) */
1659 err = run_is_empty(&indx->alloc_run)
1660 ? indx_create_allocate(indx, ni, &new_vbn)
1661 : indx_add_allocate(indx, ni, &new_vbn);
1662
1663 /* layout of record may be changed, so rescan root */
1664 root = indx_get_root(indx, ni, &attr, &mi);
1665 if (!root) {
1666 /* bug? */
1667 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
1668 err = -EINVAL;
1669 goto out1;
1670 }
1671
1672 if (err) {
1673 /* restore root */
1674 if (mi_resize_attr(mi, attr, -ds_root))
1675 memcpy(attr, a_root, asize);
1676 else {
1677 /* bug? */
1678 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
1679 }
1680 goto out1;
1681 }
1682
1683 e = (struct NTFS_DE *)(root + 1);
1684 *(__le64 *)(e + 1) = cpu_to_le64(new_vbn);
1685 mi->dirty = true;
1686
1687 /* now we can create/format the new buffer and copy the entries into */
1688 n = indx_new(indx, ni, new_vbn, sub_vbn);
1689 if (IS_ERR(n)) {
1690 err = PTR_ERR(n);
1691 goto out1;
1692 }
1693
1694 hdr = &n->index->ihdr;
1695 hdr_used = le32_to_cpu(hdr->used);
1696 hdr_total = le32_to_cpu(hdr->total);
1697
1698 /* Copy root entries into new buffer */
1699 hdr_insert_head(hdr, re, to_move);
1700
1701 /* Update bitmap attribute */
1702 indx_mark_used(indx, ni, new_vbn >> indx->idx2vbn_bits);
1703
1704 /* Check if we can insert new entry new index buffer */
1705 if (hdr_used + new_de_size > hdr_total) {
1706 /*
1707 * This occurs if mft record is the same or bigger than index
1708 * buffer. Move all root new index and have no space to add
1709 * new entry classic case when mft record is 1K and index
1710 * buffer 4K the problem should not occurs
1711 */
Kari Argillander195c52b2021-08-24 21:37:07 +03001712 kfree(re);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001713 indx_write(indx, ni, n, 0);
1714
1715 put_indx_node(n);
1716 fnd_clear(fnd);
1717 err = indx_insert_entry(indx, ni, new_de, ctx, fnd);
1718 goto out;
1719 }
1720
1721 /*
1722 * Now root is a parent for new index buffer
1723 * Insert NewEntry a new buffer
1724 */
1725 e = hdr_insert_de(indx, hdr, new_de, NULL, ctx);
1726 if (!e) {
1727 err = -EINVAL;
1728 goto out1;
1729 }
1730 fnd_push(fnd, n, e);
1731
1732 /* Just write updates index into disk */
1733 indx_write(indx, ni, n, 0);
1734
1735 n = NULL;
1736
1737out1:
Kari Argillander195c52b2021-08-24 21:37:07 +03001738 kfree(re);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001739 if (n)
1740 put_indx_node(n);
1741
1742out:
Kari Argillander195c52b2021-08-24 21:37:07 +03001743 kfree(a_root);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001744 return err;
1745}
1746
1747/*
1748 * indx_insert_into_buffer
1749 *
1750 * attempts to insert an entry into an Index Allocation Buffer.
1751 * If necessary, it will split the buffer.
1752 */
1753static int
1754indx_insert_into_buffer(struct ntfs_index *indx, struct ntfs_inode *ni,
1755 struct INDEX_ROOT *root, const struct NTFS_DE *new_de,
1756 const void *ctx, int level, struct ntfs_fnd *fnd)
1757{
1758 int err;
1759 const struct NTFS_DE *sp;
1760 struct NTFS_DE *e, *de_t, *up_e = NULL;
1761 struct indx_node *n2 = NULL;
1762 struct indx_node *n1 = fnd->nodes[level];
1763 struct INDEX_HDR *hdr1 = &n1->index->ihdr;
1764 struct INDEX_HDR *hdr2;
1765 u32 to_copy, used;
1766 CLST new_vbn;
1767 __le64 t_vbn, *sub_vbn;
1768 u16 sp_size;
1769
1770 /* Try the most easy case */
1771 e = fnd->level - 1 == level ? fnd->de[level] : NULL;
1772 e = hdr_insert_de(indx, hdr1, new_de, e, ctx);
1773 fnd->de[level] = e;
1774 if (e) {
1775 /* Just write updated index into disk */
1776 indx_write(indx, ni, n1, 0);
1777 return 0;
1778 }
1779
1780 /*
1781 * No space to insert into buffer. Split it.
1782 * To split we:
1783 * - Save split point ('cause index buffers will be changed)
1784 * - Allocate NewBuffer and copy all entries <= sp into new buffer
1785 * - Remove all entries (sp including) from TargetBuffer
1786 * - Insert NewEntry into left or right buffer (depending on sp <=>
1787 * NewEntry)
1788 * - Insert sp into parent buffer (or root)
1789 * - Make sp a parent for new buffer
1790 */
1791 sp = hdr_find_split(hdr1);
1792 if (!sp)
1793 return -EINVAL;
1794
1795 sp_size = le16_to_cpu(sp->size);
Kari Argillander195c52b2021-08-24 21:37:07 +03001796 up_e = kmalloc(sp_size + sizeof(u64), GFP_NOFS);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001797 if (!up_e)
1798 return -ENOMEM;
1799 memcpy(up_e, sp, sp_size);
1800
1801 if (!hdr1->flags) {
1802 up_e->flags |= NTFS_IE_HAS_SUBNODES;
1803 up_e->size = cpu_to_le16(sp_size + sizeof(u64));
1804 sub_vbn = NULL;
1805 } else {
1806 t_vbn = de_get_vbn_le(up_e);
1807 sub_vbn = &t_vbn;
1808 }
1809
1810 /* Allocate on disk a new index allocation buffer. */
1811 err = indx_add_allocate(indx, ni, &new_vbn);
1812 if (err)
1813 goto out;
1814
1815 /* Allocate and format memory a new index buffer */
1816 n2 = indx_new(indx, ni, new_vbn, sub_vbn);
1817 if (IS_ERR(n2)) {
1818 err = PTR_ERR(n2);
1819 goto out;
1820 }
1821
1822 hdr2 = &n2->index->ihdr;
1823
1824 /* Make sp a parent for new buffer */
1825 de_set_vbn(up_e, new_vbn);
1826
1827 /* copy all the entries <= sp into the new buffer. */
1828 de_t = hdr_first_de(hdr1);
1829 to_copy = PtrOffset(de_t, sp);
1830 hdr_insert_head(hdr2, de_t, to_copy);
1831
1832 /* remove all entries (sp including) from hdr1 */
1833 used = le32_to_cpu(hdr1->used) - to_copy - sp_size;
1834 memmove(de_t, Add2Ptr(sp, sp_size), used - le32_to_cpu(hdr1->de_off));
1835 hdr1->used = cpu_to_le32(used);
1836
1837 /* Insert new entry into left or right buffer (depending on sp <=> new_de) */
1838 hdr_insert_de(indx,
1839 (*indx->cmp)(new_de + 1, le16_to_cpu(new_de->key_size),
1840 up_e + 1, le16_to_cpu(up_e->key_size),
1841 ctx) < 0
1842 ? hdr2
1843 : hdr1,
1844 new_de, NULL, ctx);
1845
1846 indx_mark_used(indx, ni, new_vbn >> indx->idx2vbn_bits);
1847
1848 indx_write(indx, ni, n1, 0);
1849 indx_write(indx, ni, n2, 0);
1850
1851 put_indx_node(n2);
1852
1853 /*
1854 * we've finished splitting everybody, so we are ready to
1855 * insert the promoted entry into the parent.
1856 */
1857 if (!level) {
1858 /* Insert in root */
1859 err = indx_insert_into_root(indx, ni, up_e, NULL, ctx, fnd);
1860 if (err)
1861 goto out;
1862 } else {
1863 /*
1864 * The target buffer's parent is another index buffer
1865 * TODO: Remove recursion
1866 */
1867 err = indx_insert_into_buffer(indx, ni, root, up_e, ctx,
1868 level - 1, fnd);
1869 if (err)
1870 goto out;
1871 }
1872
1873out:
Kari Argillander195c52b2021-08-24 21:37:07 +03001874 kfree(up_e);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001875
1876 return err;
1877}
1878
1879/*
1880 * indx_insert_entry
1881 *
1882 * inserts new entry into index
1883 */
1884int indx_insert_entry(struct ntfs_index *indx, struct ntfs_inode *ni,
1885 const struct NTFS_DE *new_de, const void *ctx,
1886 struct ntfs_fnd *fnd)
1887{
1888 int err;
1889 int diff;
1890 struct NTFS_DE *e;
1891 struct ntfs_fnd *fnd_a = NULL;
1892 struct INDEX_ROOT *root;
1893
1894 if (!fnd) {
1895 fnd_a = fnd_get();
1896 if (!fnd_a) {
1897 err = -ENOMEM;
1898 goto out1;
1899 }
1900 fnd = fnd_a;
1901 }
1902
1903 root = indx_get_root(indx, ni, NULL, NULL);
1904 if (!root) {
1905 err = -EINVAL;
1906 goto out;
1907 }
1908
1909 if (fnd_is_empty(fnd)) {
1910 /* Find the spot the tree where we want to insert the new entry. */
1911 err = indx_find(indx, ni, root, new_de + 1,
1912 le16_to_cpu(new_de->key_size), ctx, &diff, &e,
1913 fnd);
1914 if (err)
1915 goto out;
1916
1917 if (!diff) {
1918 err = -EEXIST;
1919 goto out;
1920 }
1921 }
1922
1923 if (!fnd->level) {
1924 /* The root is also a leaf, so we'll insert the new entry into it. */
1925 err = indx_insert_into_root(indx, ni, new_de, fnd->root_de, ctx,
1926 fnd);
1927 if (err)
1928 goto out;
1929 } else {
1930 /* found a leaf buffer, so we'll insert the new entry into it.*/
1931 err = indx_insert_into_buffer(indx, ni, root, new_de, ctx,
1932 fnd->level - 1, fnd);
1933 if (err)
1934 goto out;
1935 }
1936
1937out:
1938 fnd_put(fnd_a);
1939out1:
1940 return err;
1941}
1942
1943/*
1944 * indx_find_buffer
1945 *
1946 * locates a buffer the tree.
1947 */
1948static struct indx_node *indx_find_buffer(struct ntfs_index *indx,
1949 struct ntfs_inode *ni,
1950 const struct INDEX_ROOT *root,
1951 __le64 vbn, struct indx_node *n)
1952{
1953 int err;
1954 const struct NTFS_DE *e;
1955 struct indx_node *r;
1956 const struct INDEX_HDR *hdr = n ? &n->index->ihdr : &root->ihdr;
1957
1958 /* Step 1: Scan one level */
1959 for (e = hdr_first_de(hdr);; e = hdr_next_de(hdr, e)) {
1960 if (!e)
1961 return ERR_PTR(-EINVAL);
1962
1963 if (de_has_vcn(e) && vbn == de_get_vbn_le(e))
1964 return n;
1965
1966 if (de_is_last(e))
1967 break;
1968 }
1969
1970 /* Step2: Do recursion */
1971 e = Add2Ptr(hdr, le32_to_cpu(hdr->de_off));
1972 for (;;) {
1973 if (de_has_vcn_ex(e)) {
1974 err = indx_read(indx, ni, de_get_vbn(e), &n);
1975 if (err)
1976 return ERR_PTR(err);
1977
1978 r = indx_find_buffer(indx, ni, root, vbn, n);
1979 if (r)
1980 return r;
1981 }
1982
1983 if (de_is_last(e))
1984 break;
1985
1986 e = Add2Ptr(e, le16_to_cpu(e->size));
1987 }
1988
1989 return NULL;
1990}
1991
1992/*
1993 * indx_shrink
1994 *
1995 * deallocates unused tail indexes
1996 */
1997static int indx_shrink(struct ntfs_index *indx, struct ntfs_inode *ni,
1998 size_t bit)
1999{
2000 int err = 0;
2001 u64 bpb, new_data;
2002 size_t nbits;
2003 struct ATTRIB *b;
2004 struct ATTR_LIST_ENTRY *le = NULL;
2005 const struct INDEX_NAMES *in = &s_index_names[indx->type];
2006
2007 b = ni_find_attr(ni, NULL, &le, ATTR_BITMAP, in->name, in->name_len,
2008 NULL, NULL);
2009
2010 if (!b)
2011 return -ENOENT;
2012
2013 if (!b->non_res) {
2014 unsigned long pos;
2015 const unsigned long *bm = resident_data(b);
2016
Colin Ian King71eeb6a2021-08-16 17:30:25 +01002017 nbits = (size_t)le32_to_cpu(b->res.data_size) * 8;
Konstantin Komarov82cae262021-08-13 17:21:29 +03002018
2019 if (bit >= nbits)
2020 return 0;
2021
2022 pos = find_next_bit(bm, nbits, bit);
2023 if (pos < nbits)
2024 return 0;
2025 } else {
2026 size_t used = MINUS_ONE_T;
2027
2028 nbits = le64_to_cpu(b->nres.data_size) * 8;
2029
2030 if (bit >= nbits)
2031 return 0;
2032
2033 err = scan_nres_bitmap(ni, b, indx, bit, &scan_for_used, &used);
2034 if (err)
2035 return err;
2036
2037 if (used != MINUS_ONE_T)
2038 return 0;
2039 }
2040
2041 new_data = (u64)bit << indx->index_bits;
2042
2043 err = attr_set_size(ni, ATTR_ALLOC, in->name, in->name_len,
2044 &indx->alloc_run, new_data, &new_data, false, NULL);
2045 if (err)
2046 return err;
2047
2048 bpb = bitmap_size(bit);
2049 if (bpb * 8 == nbits)
2050 return 0;
2051
2052 err = attr_set_size(ni, ATTR_BITMAP, in->name, in->name_len,
2053 &indx->bitmap_run, bpb, &bpb, false, NULL);
2054
2055 return err;
2056}
2057
2058static int indx_free_children(struct ntfs_index *indx, struct ntfs_inode *ni,
2059 const struct NTFS_DE *e, bool trim)
2060{
2061 int err;
2062 struct indx_node *n;
2063 struct INDEX_HDR *hdr;
2064 CLST vbn = de_get_vbn(e);
2065 size_t i;
2066
2067 err = indx_read(indx, ni, vbn, &n);
2068 if (err)
2069 return err;
2070
2071 hdr = &n->index->ihdr;
2072 /* First, recurse into the children, if any.*/
2073 if (hdr_has_subnode(hdr)) {
2074 for (e = hdr_first_de(hdr); e; e = hdr_next_de(hdr, e)) {
2075 indx_free_children(indx, ni, e, false);
2076 if (de_is_last(e))
2077 break;
2078 }
2079 }
2080
2081 put_indx_node(n);
2082
2083 i = vbn >> indx->idx2vbn_bits;
2084 /* We've gotten rid of the children; add this buffer to the free list. */
2085 indx_mark_free(indx, ni, i);
2086
2087 if (!trim)
2088 return 0;
2089
2090 /*
2091 * If there are no used indexes after current free index
2092 * then we can truncate allocation and bitmap
2093 * Use bitmap to estimate the case
2094 */
2095 indx_shrink(indx, ni, i + 1);
2096 return 0;
2097}
2098
2099/*
2100 * indx_get_entry_to_replace
2101 *
2102 * finds a replacement entry for a deleted entry
2103 * always returns a node entry:
2104 * NTFS_IE_HAS_SUBNODES is set the flags and the size includes the sub_vcn
2105 */
2106static int indx_get_entry_to_replace(struct ntfs_index *indx,
2107 struct ntfs_inode *ni,
2108 const struct NTFS_DE *de_next,
2109 struct NTFS_DE **de_to_replace,
2110 struct ntfs_fnd *fnd)
2111{
2112 int err;
2113 int level = -1;
2114 CLST vbn;
2115 struct NTFS_DE *e, *te, *re;
2116 struct indx_node *n;
2117 struct INDEX_BUFFER *ib;
2118
2119 *de_to_replace = NULL;
2120
2121 /* Find first leaf entry down from de_next */
2122 vbn = de_get_vbn(de_next);
2123 for (;;) {
2124 n = NULL;
2125 err = indx_read(indx, ni, vbn, &n);
2126 if (err)
2127 goto out;
2128
2129 e = hdr_first_de(&n->index->ihdr);
2130 fnd_push(fnd, n, e);
2131
2132 if (!de_is_last(e)) {
2133 /*
2134 * This buffer is non-empty, so its first entry could be used as the
2135 * replacement entry.
2136 */
2137 level = fnd->level - 1;
2138 }
2139
2140 if (!de_has_vcn(e))
2141 break;
2142
2143 /* This buffer is a node. Continue to go down */
2144 vbn = de_get_vbn(e);
2145 }
2146
2147 if (level == -1)
2148 goto out;
2149
2150 n = fnd->nodes[level];
2151 te = hdr_first_de(&n->index->ihdr);
2152 /* Copy the candidate entry into the replacement entry buffer. */
Kari Argillander195c52b2021-08-24 21:37:07 +03002153 re = kmalloc(le16_to_cpu(te->size) + sizeof(u64), GFP_NOFS);
Konstantin Komarov82cae262021-08-13 17:21:29 +03002154 if (!re) {
2155 err = -ENOMEM;
2156 goto out;
2157 }
2158
2159 *de_to_replace = re;
2160 memcpy(re, te, le16_to_cpu(te->size));
2161
2162 if (!de_has_vcn(re)) {
2163 /*
2164 * The replacement entry we found doesn't have a sub_vcn. increase its size
2165 * to hold one.
2166 */
2167 le16_add_cpu(&re->size, sizeof(u64));
2168 re->flags |= NTFS_IE_HAS_SUBNODES;
2169 } else {
2170 /*
2171 * The replacement entry we found was a node entry, which means that all
2172 * its child buffers are empty. Return them to the free pool.
2173 */
2174 indx_free_children(indx, ni, te, true);
2175 }
2176
2177 /*
2178 * Expunge the replacement entry from its former location,
2179 * and then write that buffer.
2180 */
2181 ib = n->index;
2182 e = hdr_delete_de(&ib->ihdr, te);
2183
2184 fnd->de[level] = e;
2185 indx_write(indx, ni, n, 0);
2186
2187 /* Check to see if this action created an empty leaf. */
2188 if (ib_is_leaf(ib) && ib_is_empty(ib))
2189 return 0;
2190
2191out:
2192 fnd_clear(fnd);
2193 return err;
2194}
2195
2196/*
2197 * indx_delete_entry
2198 *
2199 * deletes an entry from the index.
2200 */
2201int indx_delete_entry(struct ntfs_index *indx, struct ntfs_inode *ni,
2202 const void *key, u32 key_len, const void *ctx)
2203{
2204 int err, diff;
2205 struct INDEX_ROOT *root;
2206 struct INDEX_HDR *hdr;
2207 struct ntfs_fnd *fnd, *fnd2;
2208 struct INDEX_BUFFER *ib;
2209 struct NTFS_DE *e, *re, *next, *prev, *me;
2210 struct indx_node *n, *n2d = NULL;
2211 __le64 sub_vbn;
2212 int level, level2;
2213 struct ATTRIB *attr;
2214 struct mft_inode *mi;
2215 u32 e_size, root_size, new_root_size;
2216 size_t trim_bit;
2217 const struct INDEX_NAMES *in;
2218
2219 fnd = fnd_get();
2220 if (!fnd) {
2221 err = -ENOMEM;
2222 goto out2;
2223 }
2224
2225 fnd2 = fnd_get();
2226 if (!fnd2) {
2227 err = -ENOMEM;
2228 goto out1;
2229 }
2230
2231 root = indx_get_root(indx, ni, &attr, &mi);
2232 if (!root) {
2233 err = -EINVAL;
2234 goto out;
2235 }
2236
2237 /* Locate the entry to remove. */
2238 err = indx_find(indx, ni, root, key, key_len, ctx, &diff, &e, fnd);
2239 if (err)
2240 goto out;
2241
2242 if (!e || diff) {
2243 err = -ENOENT;
2244 goto out;
2245 }
2246
2247 level = fnd->level;
2248
2249 if (level) {
2250 n = fnd->nodes[level - 1];
2251 e = fnd->de[level - 1];
2252 ib = n->index;
2253 hdr = &ib->ihdr;
2254 } else {
2255 hdr = &root->ihdr;
2256 e = fnd->root_de;
2257 n = NULL;
2258 }
2259
2260 e_size = le16_to_cpu(e->size);
2261
2262 if (!de_has_vcn_ex(e)) {
2263 /* The entry to delete is a leaf, so we can just rip it out */
2264 hdr_delete_de(hdr, e);
2265
2266 if (!level) {
2267 hdr->total = hdr->used;
2268
2269 /* Shrink resident root attribute */
2270 mi_resize_attr(mi, attr, 0 - e_size);
2271 goto out;
2272 }
2273
2274 indx_write(indx, ni, n, 0);
2275
2276 /*
2277 * Check to see if removing that entry made
2278 * the leaf empty.
2279 */
2280 if (ib_is_leaf(ib) && ib_is_empty(ib)) {
2281 fnd_pop(fnd);
2282 fnd_push(fnd2, n, e);
2283 }
2284 } else {
2285 /*
2286 * The entry we wish to delete is a node buffer, so we
2287 * have to find a replacement for it.
2288 */
2289 next = de_get_next(e);
2290
2291 err = indx_get_entry_to_replace(indx, ni, next, &re, fnd2);
2292 if (err)
2293 goto out;
2294
2295 if (re) {
2296 de_set_vbn_le(re, de_get_vbn_le(e));
2297 hdr_delete_de(hdr, e);
2298
2299 err = level ? indx_insert_into_buffer(indx, ni, root,
2300 re, ctx,
2301 fnd->level - 1,
2302 fnd)
2303 : indx_insert_into_root(indx, ni, re, e,
2304 ctx, fnd);
Kari Argillander195c52b2021-08-24 21:37:07 +03002305 kfree(re);
Konstantin Komarov82cae262021-08-13 17:21:29 +03002306
2307 if (err)
2308 goto out;
2309 } else {
2310 /*
2311 * There is no replacement for the current entry.
2312 * This means that the subtree rooted at its node is empty,
2313 * and can be deleted, which turn means that the node can
2314 * just inherit the deleted entry sub_vcn
2315 */
2316 indx_free_children(indx, ni, next, true);
2317
2318 de_set_vbn_le(next, de_get_vbn_le(e));
2319 hdr_delete_de(hdr, e);
2320 if (level) {
2321 indx_write(indx, ni, n, 0);
2322 } else {
2323 hdr->total = hdr->used;
2324
2325 /* Shrink resident root attribute */
2326 mi_resize_attr(mi, attr, 0 - e_size);
2327 }
2328 }
2329 }
2330
2331 /* Delete a branch of tree */
2332 if (!fnd2 || !fnd2->level)
2333 goto out;
2334
2335 /* Reinit root 'cause it can be changed */
2336 root = indx_get_root(indx, ni, &attr, &mi);
2337 if (!root) {
2338 err = -EINVAL;
2339 goto out;
2340 }
2341
2342 n2d = NULL;
2343 sub_vbn = fnd2->nodes[0]->index->vbn;
2344 level2 = 0;
2345 level = fnd->level;
2346
2347 hdr = level ? &fnd->nodes[level - 1]->index->ihdr : &root->ihdr;
2348
2349 /* Scan current level */
2350 for (e = hdr_first_de(hdr);; e = hdr_next_de(hdr, e)) {
2351 if (!e) {
2352 err = -EINVAL;
2353 goto out;
2354 }
2355
2356 if (de_has_vcn(e) && sub_vbn == de_get_vbn_le(e))
2357 break;
2358
2359 if (de_is_last(e)) {
2360 e = NULL;
2361 break;
2362 }
2363 }
2364
2365 if (!e) {
2366 /* Do slow search from root */
2367 struct indx_node *in;
2368
2369 fnd_clear(fnd);
2370
2371 in = indx_find_buffer(indx, ni, root, sub_vbn, NULL);
2372 if (IS_ERR(in)) {
2373 err = PTR_ERR(in);
2374 goto out;
2375 }
2376
2377 if (in)
2378 fnd_push(fnd, in, NULL);
2379 }
2380
2381 /* Merge fnd2 -> fnd */
2382 for (level = 0; level < fnd2->level; level++) {
2383 fnd_push(fnd, fnd2->nodes[level], fnd2->de[level]);
2384 fnd2->nodes[level] = NULL;
2385 }
2386 fnd2->level = 0;
2387
2388 hdr = NULL;
2389 for (level = fnd->level; level; level--) {
2390 struct indx_node *in = fnd->nodes[level - 1];
2391
2392 ib = in->index;
2393 if (ib_is_empty(ib)) {
2394 sub_vbn = ib->vbn;
2395 } else {
2396 hdr = &ib->ihdr;
2397 n2d = in;
2398 level2 = level;
2399 break;
2400 }
2401 }
2402
2403 if (!hdr)
2404 hdr = &root->ihdr;
2405
2406 e = hdr_first_de(hdr);
2407 if (!e) {
2408 err = -EINVAL;
2409 goto out;
2410 }
2411
2412 if (hdr != &root->ihdr || !de_is_last(e)) {
2413 prev = NULL;
2414 while (!de_is_last(e)) {
2415 if (de_has_vcn(e) && sub_vbn == de_get_vbn_le(e))
2416 break;
2417 prev = e;
2418 e = hdr_next_de(hdr, e);
2419 if (!e) {
2420 err = -EINVAL;
2421 goto out;
2422 }
2423 }
2424
2425 if (sub_vbn != de_get_vbn_le(e)) {
2426 /*
2427 * Didn't find the parent entry, although this buffer is the parent trail.
2428 * Something is corrupt.
2429 */
2430 err = -EINVAL;
2431 goto out;
2432 }
2433
2434 if (de_is_last(e)) {
2435 /*
2436 * Since we can't remove the end entry, we'll remove its
2437 * predecessor instead. This means we have to transfer the
2438 * predecessor's sub_vcn to the end entry.
2439 * Note: that this index block is not empty, so the
2440 * predecessor must exist
2441 */
2442 if (!prev) {
2443 err = -EINVAL;
2444 goto out;
2445 }
2446
2447 if (de_has_vcn(prev)) {
2448 de_set_vbn_le(e, de_get_vbn_le(prev));
2449 } else if (de_has_vcn(e)) {
2450 le16_sub_cpu(&e->size, sizeof(u64));
2451 e->flags &= ~NTFS_IE_HAS_SUBNODES;
2452 le32_sub_cpu(&hdr->used, sizeof(u64));
2453 }
2454 e = prev;
2455 }
2456
2457 /*
2458 * Copy the current entry into a temporary buffer (stripping off its
2459 * down-pointer, if any) and delete it from the current buffer or root,
2460 * as appropriate.
2461 */
2462 e_size = le16_to_cpu(e->size);
Kari Argillander195c52b2021-08-24 21:37:07 +03002463 me = kmemdup(e, e_size, GFP_NOFS);
Konstantin Komarov82cae262021-08-13 17:21:29 +03002464 if (!me) {
2465 err = -ENOMEM;
2466 goto out;
2467 }
2468
2469 if (de_has_vcn(me)) {
2470 me->flags &= ~NTFS_IE_HAS_SUBNODES;
2471 le16_sub_cpu(&me->size, sizeof(u64));
2472 }
2473
2474 hdr_delete_de(hdr, e);
2475
2476 if (hdr == &root->ihdr) {
2477 level = 0;
2478 hdr->total = hdr->used;
2479
2480 /* Shrink resident root attribute */
2481 mi_resize_attr(mi, attr, 0 - e_size);
2482 } else {
2483 indx_write(indx, ni, n2d, 0);
2484 level = level2;
2485 }
2486
2487 /* Mark unused buffers as free */
2488 trim_bit = -1;
2489 for (; level < fnd->level; level++) {
2490 ib = fnd->nodes[level]->index;
2491 if (ib_is_empty(ib)) {
2492 size_t k = le64_to_cpu(ib->vbn) >>
2493 indx->idx2vbn_bits;
2494
2495 indx_mark_free(indx, ni, k);
2496 if (k < trim_bit)
2497 trim_bit = k;
2498 }
2499 }
2500
2501 fnd_clear(fnd);
2502 /*fnd->root_de = NULL;*/
2503
2504 /*
2505 * Re-insert the entry into the tree.
2506 * Find the spot the tree where we want to insert the new entry.
2507 */
2508 err = indx_insert_entry(indx, ni, me, ctx, fnd);
Kari Argillander195c52b2021-08-24 21:37:07 +03002509 kfree(me);
Konstantin Komarov82cae262021-08-13 17:21:29 +03002510 if (err)
2511 goto out;
2512
2513 if (trim_bit != -1)
2514 indx_shrink(indx, ni, trim_bit);
2515 } else {
2516 /*
2517 * This tree needs to be collapsed down to an empty root.
2518 * Recreate the index root as an empty leaf and free all the bits the
2519 * index allocation bitmap.
2520 */
2521 fnd_clear(fnd);
2522 fnd_clear(fnd2);
2523
2524 in = &s_index_names[indx->type];
2525
2526 err = attr_set_size(ni, ATTR_ALLOC, in->name, in->name_len,
2527 &indx->alloc_run, 0, NULL, false, NULL);
2528 err = ni_remove_attr(ni, ATTR_ALLOC, in->name, in->name_len,
2529 false, NULL);
2530 run_close(&indx->alloc_run);
2531
2532 err = attr_set_size(ni, ATTR_BITMAP, in->name, in->name_len,
2533 &indx->bitmap_run, 0, NULL, false, NULL);
2534 err = ni_remove_attr(ni, ATTR_BITMAP, in->name, in->name_len,
2535 false, NULL);
2536 run_close(&indx->bitmap_run);
2537
2538 root = indx_get_root(indx, ni, &attr, &mi);
2539 if (!root) {
2540 err = -EINVAL;
2541 goto out;
2542 }
2543
2544 root_size = le32_to_cpu(attr->res.data_size);
2545 new_root_size =
2546 sizeof(struct INDEX_ROOT) + sizeof(struct NTFS_DE);
2547
2548 if (new_root_size != root_size &&
2549 !mi_resize_attr(mi, attr, new_root_size - root_size)) {
2550 err = -EINVAL;
2551 goto out;
2552 }
2553
2554 /* Fill first entry */
2555 e = (struct NTFS_DE *)(root + 1);
2556 e->ref.low = 0;
2557 e->ref.high = 0;
2558 e->ref.seq = 0;
2559 e->size = cpu_to_le16(sizeof(struct NTFS_DE));
2560 e->flags = NTFS_IE_LAST; // 0x02
2561 e->key_size = 0;
2562 e->res = 0;
2563
2564 hdr = &root->ihdr;
2565 hdr->flags = 0;
2566 hdr->used = hdr->total = cpu_to_le32(
2567 new_root_size - offsetof(struct INDEX_ROOT, ihdr));
2568 mi->dirty = true;
2569 }
2570
2571out:
2572 fnd_put(fnd2);
2573out1:
2574 fnd_put(fnd);
2575out2:
2576 return err;
2577}
2578
2579/*
2580 * Update duplicated information in directory entry
2581 * 'dup' - info from MFT record
2582 */
2583int indx_update_dup(struct ntfs_inode *ni, struct ntfs_sb_info *sbi,
2584 const struct ATTR_FILE_NAME *fname,
2585 const struct NTFS_DUP_INFO *dup, int sync)
2586{
2587 int err, diff;
2588 struct NTFS_DE *e = NULL;
2589 struct ATTR_FILE_NAME *e_fname;
2590 struct ntfs_fnd *fnd;
2591 struct INDEX_ROOT *root;
2592 struct mft_inode *mi;
2593 struct ntfs_index *indx = &ni->dir;
2594
2595 fnd = fnd_get();
2596 if (!fnd) {
2597 err = -ENOMEM;
2598 goto out1;
2599 }
2600
2601 root = indx_get_root(indx, ni, NULL, &mi);
2602 if (!root) {
2603 err = -EINVAL;
2604 goto out;
2605 }
2606
2607 /* Find entry in directory */
2608 err = indx_find(indx, ni, root, fname, fname_full_size(fname), sbi,
2609 &diff, &e, fnd);
2610 if (err)
2611 goto out;
2612
2613 if (!e) {
2614 err = -EINVAL;
2615 goto out;
2616 }
2617
2618 if (diff) {
2619 err = -EINVAL;
2620 goto out;
2621 }
2622
2623 e_fname = (struct ATTR_FILE_NAME *)(e + 1);
2624
2625 if (!memcmp(&e_fname->dup, dup, sizeof(*dup))) {
2626 /* nothing to update in index! Try to avoid this call */
2627 goto out;
2628 }
2629
2630 memcpy(&e_fname->dup, dup, sizeof(*dup));
2631
2632 if (fnd->level) {
2633 /* directory entry in index */
2634 err = indx_write(indx, ni, fnd->nodes[fnd->level - 1], sync);
2635 } else {
2636 /* directory entry in directory MFT record */
2637 mi->dirty = true;
2638 if (sync)
2639 err = mi_write(mi, 1);
2640 else
2641 mark_inode_dirty(&ni->vfs_inode);
2642 }
2643
2644out:
2645 fnd_put(fnd);
2646
2647out1:
2648 return err;
2649}