blob: 70ef59455b72c927b40bbb76c5420b8dfc72e24a [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/*
Kari Argillandere8b8e972021-08-03 14:57:09 +030027 * cmp_fnames - Compare two names in index.
28 *
Konstantin Komarov82cae262021-08-13 17:21:29 +030029 * if l1 != 0
Kari Argillandere8b8e972021-08-03 14:57:09 +030030 * Both names are little endian on-disk ATTR_FILE_NAME structs.
Konstantin Komarov82cae262021-08-13 17:21:29 +030031 * else
32 * key1 - cpu_str, key2 - ATTR_FILE_NAME
33 */
34static int cmp_fnames(const void *key1, size_t l1, const void *key2, size_t l2,
35 const void *data)
36{
37 const struct ATTR_FILE_NAME *f2 = key2;
38 const struct ntfs_sb_info *sbi = data;
39 const struct ATTR_FILE_NAME *f1;
40 u16 fsize2;
41 bool both_case;
42
43 if (l2 <= offsetof(struct ATTR_FILE_NAME, name))
44 return -1;
45
46 fsize2 = fname_full_size(f2);
47 if (l2 < fsize2)
48 return -1;
49
50 both_case = f2->type != FILE_NAME_DOS /*&& !sbi->options.nocase*/;
51 if (!l1) {
52 const struct le_str *s2 = (struct le_str *)&f2->name_len;
53
54 /*
55 * If names are equal (case insensitive)
Kari Argillandere8b8e972021-08-03 14:57:09 +030056 * try to compare it case sensitive.
Konstantin Komarov82cae262021-08-13 17:21:29 +030057 */
58 return ntfs_cmp_names_cpu(key1, s2, sbi->upcase, both_case);
59 }
60
61 f1 = key1;
62 return ntfs_cmp_names(f1->name, f1->name_len, f2->name, f2->name_len,
63 sbi->upcase, both_case);
64}
65
Kari Argillandere8b8e972021-08-03 14:57:09 +030066/*
67 * cmp_uint - $SII of $Secure and $Q of Quota
68 */
Konstantin Komarov82cae262021-08-13 17:21:29 +030069static int cmp_uint(const void *key1, size_t l1, const void *key2, size_t l2,
70 const void *data)
71{
72 const u32 *k1 = key1;
73 const u32 *k2 = key2;
74
75 if (l2 < sizeof(u32))
76 return -1;
77
78 if (*k1 < *k2)
79 return -1;
80 if (*k1 > *k2)
81 return 1;
82 return 0;
83}
84
Kari Argillandere8b8e972021-08-03 14:57:09 +030085/*
86 * cmp_sdh - $SDH of $Secure
87 */
Konstantin Komarov82cae262021-08-13 17:21:29 +030088static int cmp_sdh(const void *key1, size_t l1, const void *key2, size_t l2,
89 const void *data)
90{
91 const struct SECURITY_KEY *k1 = key1;
92 const struct SECURITY_KEY *k2 = key2;
93 u32 t1, t2;
94
95 if (l2 < sizeof(struct SECURITY_KEY))
96 return -1;
97
98 t1 = le32_to_cpu(k1->hash);
99 t2 = le32_to_cpu(k2->hash);
100
Kari Argillandere8b8e972021-08-03 14:57:09 +0300101 /* First value is a hash value itself. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300102 if (t1 < t2)
103 return -1;
104 if (t1 > t2)
105 return 1;
106
Kari Argillandere8b8e972021-08-03 14:57:09 +0300107 /* Second value is security Id. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300108 if (data) {
109 t1 = le32_to_cpu(k1->sec_id);
110 t2 = le32_to_cpu(k2->sec_id);
111 if (t1 < t2)
112 return -1;
113 if (t1 > t2)
114 return 1;
115 }
116
117 return 0;
118}
119
Kari Argillandere8b8e972021-08-03 14:57:09 +0300120/*
121 * cmp_uints - $O of ObjId and "$R" for Reparse.
122 */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300123static int cmp_uints(const void *key1, size_t l1, const void *key2, size_t l2,
124 const void *data)
125{
126 const __le32 *k1 = key1;
127 const __le32 *k2 = key2;
128 size_t count;
129
130 if ((size_t)data == 1) {
131 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +0300132 * ni_delete_all -> ntfs_remove_reparse ->
133 * delete all with this reference.
Konstantin Komarov82cae262021-08-13 17:21:29 +0300134 * k1, k2 - pointers to REPARSE_KEY
135 */
136
Kari Argillandere8b8e972021-08-03 14:57:09 +0300137 k1 += 1; // Skip REPARSE_KEY.ReparseTag
138 k2 += 1; // Skip REPARSE_KEY.ReparseTag
Konstantin Komarov82cae262021-08-13 17:21:29 +0300139 if (l2 <= sizeof(int))
140 return -1;
141 l2 -= sizeof(int);
142 if (l1 <= sizeof(int))
143 return 1;
144 l1 -= sizeof(int);
145 }
146
147 if (l2 < sizeof(int))
148 return -1;
149
150 for (count = min(l1, l2) >> 2; count > 0; --count, ++k1, ++k2) {
151 u32 t1 = le32_to_cpu(*k1);
152 u32 t2 = le32_to_cpu(*k2);
153
154 if (t1 > t2)
155 return 1;
156 if (t1 < t2)
157 return -1;
158 }
159
160 if (l1 > l2)
161 return 1;
162 if (l1 < l2)
163 return -1;
164
165 return 0;
166}
167
168static inline NTFS_CMP_FUNC get_cmp_func(const struct INDEX_ROOT *root)
169{
170 switch (root->type) {
171 case ATTR_NAME:
172 if (root->rule == NTFS_COLLATION_TYPE_FILENAME)
173 return &cmp_fnames;
174 break;
175 case ATTR_ZERO:
176 switch (root->rule) {
177 case NTFS_COLLATION_TYPE_UINT:
178 return &cmp_uint;
179 case NTFS_COLLATION_TYPE_SECURITY_HASH:
180 return &cmp_sdh;
181 case NTFS_COLLATION_TYPE_UINTS:
182 return &cmp_uints;
183 default:
184 break;
185 }
Gustavo A. R. Silvaabfeb2e2021-08-18 17:21:46 -0500186 break;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300187 default:
188 break;
189 }
190
191 return NULL;
192}
193
194struct bmp_buf {
195 struct ATTRIB *b;
196 struct mft_inode *mi;
197 struct buffer_head *bh;
198 ulong *buf;
199 size_t bit;
200 u32 nbits;
201 u64 new_valid;
202};
203
204static int bmp_buf_get(struct ntfs_index *indx, struct ntfs_inode *ni,
205 size_t bit, struct bmp_buf *bbuf)
206{
207 struct ATTRIB *b;
208 size_t data_size, valid_size, vbo, off = bit >> 3;
209 struct ntfs_sb_info *sbi = ni->mi.sbi;
210 CLST vcn = off >> sbi->cluster_bits;
211 struct ATTR_LIST_ENTRY *le = NULL;
212 struct buffer_head *bh;
213 struct super_block *sb;
214 u32 blocksize;
215 const struct INDEX_NAMES *in = &s_index_names[indx->type];
216
217 bbuf->bh = NULL;
218
219 b = ni_find_attr(ni, NULL, &le, ATTR_BITMAP, in->name, in->name_len,
220 &vcn, &bbuf->mi);
221 bbuf->b = b;
222 if (!b)
223 return -EINVAL;
224
225 if (!b->non_res) {
226 data_size = le32_to_cpu(b->res.data_size);
227
228 if (off >= data_size)
229 return -EINVAL;
230
231 bbuf->buf = (ulong *)resident_data(b);
232 bbuf->bit = 0;
233 bbuf->nbits = data_size * 8;
234
235 return 0;
236 }
237
238 data_size = le64_to_cpu(b->nres.data_size);
239 if (WARN_ON(off >= data_size)) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300240 /* Looks like filesystem error. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300241 return -EINVAL;
242 }
243
244 valid_size = le64_to_cpu(b->nres.valid_size);
245
246 bh = ntfs_bread_run(sbi, &indx->bitmap_run, off);
247 if (!bh)
248 return -EIO;
249
250 if (IS_ERR(bh))
251 return PTR_ERR(bh);
252
253 bbuf->bh = bh;
254
255 if (buffer_locked(bh))
256 __wait_on_buffer(bh);
257
258 lock_buffer(bh);
259
260 sb = sbi->sb;
261 blocksize = sb->s_blocksize;
262
263 vbo = off & ~(size_t)sbi->block_mask;
264
265 bbuf->new_valid = vbo + blocksize;
266 if (bbuf->new_valid <= valid_size)
267 bbuf->new_valid = 0;
268 else if (bbuf->new_valid > data_size)
269 bbuf->new_valid = data_size;
270
271 if (vbo >= valid_size) {
272 memset(bh->b_data, 0, blocksize);
273 } else if (vbo + blocksize > valid_size) {
274 u32 voff = valid_size & sbi->block_mask;
275
276 memset(bh->b_data + voff, 0, blocksize - voff);
277 }
278
279 bbuf->buf = (ulong *)bh->b_data;
280 bbuf->bit = 8 * (off & ~(size_t)sbi->block_mask);
281 bbuf->nbits = 8 * blocksize;
282
283 return 0;
284}
285
286static void bmp_buf_put(struct bmp_buf *bbuf, bool dirty)
287{
288 struct buffer_head *bh = bbuf->bh;
289 struct ATTRIB *b = bbuf->b;
290
291 if (!bh) {
292 if (b && !b->non_res && dirty)
293 bbuf->mi->dirty = true;
294 return;
295 }
296
297 if (!dirty)
298 goto out;
299
300 if (bbuf->new_valid) {
301 b->nres.valid_size = cpu_to_le64(bbuf->new_valid);
302 bbuf->mi->dirty = true;
303 }
304
305 set_buffer_uptodate(bh);
306 mark_buffer_dirty(bh);
307
308out:
309 unlock_buffer(bh);
310 put_bh(bh);
311}
312
313/*
Kari Argillandere8b8e972021-08-03 14:57:09 +0300314 * indx_mark_used - Mark the bit @bit as used.
Konstantin Komarov82cae262021-08-13 17:21:29 +0300315 */
316static int indx_mark_used(struct ntfs_index *indx, struct ntfs_inode *ni,
317 size_t bit)
318{
319 int err;
320 struct bmp_buf bbuf;
321
322 err = bmp_buf_get(indx, ni, bit, &bbuf);
323 if (err)
324 return err;
325
326 __set_bit(bit - bbuf.bit, bbuf.buf);
327
328 bmp_buf_put(&bbuf, true);
329
330 return 0;
331}
332
333/*
Kari Argillandere8b8e972021-08-03 14:57:09 +0300334 * indx_mark_free - Mark the bit @bit as free.
Konstantin Komarov82cae262021-08-13 17:21:29 +0300335 */
336static int indx_mark_free(struct ntfs_index *indx, struct ntfs_inode *ni,
337 size_t bit)
338{
339 int err;
340 struct bmp_buf bbuf;
341
342 err = bmp_buf_get(indx, ni, bit, &bbuf);
343 if (err)
344 return err;
345
346 __clear_bit(bit - bbuf.bit, bbuf.buf);
347
348 bmp_buf_put(&bbuf, true);
349
350 return 0;
351}
352
353/*
Kari Argillandere8b8e972021-08-03 14:57:09 +0300354 * scan_nres_bitmap
355 *
356 * If ntfs_readdir calls this function (indx_used_bit -> scan_nres_bitmap),
357 * inode is shared locked and no ni_lock.
358 * Use rw_semaphore for read/write access to bitmap_run.
Konstantin Komarov82cae262021-08-13 17:21:29 +0300359 */
360static int scan_nres_bitmap(struct ntfs_inode *ni, struct ATTRIB *bitmap,
361 struct ntfs_index *indx, size_t from,
362 bool (*fn)(const ulong *buf, u32 bit, u32 bits,
363 size_t *ret),
364 size_t *ret)
365{
366 struct ntfs_sb_info *sbi = ni->mi.sbi;
367 struct super_block *sb = sbi->sb;
368 struct runs_tree *run = &indx->bitmap_run;
369 struct rw_semaphore *lock = &indx->run_lock;
370 u32 nbits = sb->s_blocksize * 8;
371 u32 blocksize = sb->s_blocksize;
372 u64 valid_size = le64_to_cpu(bitmap->nres.valid_size);
373 u64 data_size = le64_to_cpu(bitmap->nres.data_size);
374 sector_t eblock = bytes_to_block(sb, data_size);
375 size_t vbo = from >> 3;
376 sector_t blk = (vbo & sbi->cluster_mask) >> sb->s_blocksize_bits;
377 sector_t vblock = vbo >> sb->s_blocksize_bits;
378 sector_t blen, block;
379 CLST lcn, clen, vcn, vcn_next;
380 size_t idx;
381 struct buffer_head *bh;
382 bool ok;
383
384 *ret = MINUS_ONE_T;
385
386 if (vblock >= eblock)
387 return 0;
388
389 from &= nbits - 1;
390 vcn = vbo >> sbi->cluster_bits;
391
392 down_read(lock);
393 ok = run_lookup_entry(run, vcn, &lcn, &clen, &idx);
394 up_read(lock);
395
396next_run:
397 if (!ok) {
398 int err;
399 const struct INDEX_NAMES *name = &s_index_names[indx->type];
400
401 down_write(lock);
402 err = attr_load_runs_vcn(ni, ATTR_BITMAP, name->name,
403 name->name_len, run, vcn);
404 up_write(lock);
405 if (err)
406 return err;
407 down_read(lock);
408 ok = run_lookup_entry(run, vcn, &lcn, &clen, &idx);
409 up_read(lock);
410 if (!ok)
411 return -EINVAL;
412 }
413
414 blen = (sector_t)clen * sbi->blocks_per_cluster;
415 block = (sector_t)lcn * sbi->blocks_per_cluster;
416
417 for (; blk < blen; blk++, from = 0) {
418 bh = ntfs_bread(sb, block + blk);
419 if (!bh)
420 return -EIO;
421
422 vbo = (u64)vblock << sb->s_blocksize_bits;
423 if (vbo >= valid_size) {
424 memset(bh->b_data, 0, blocksize);
425 } else if (vbo + blocksize > valid_size) {
426 u32 voff = valid_size & sbi->block_mask;
427
428 memset(bh->b_data + voff, 0, blocksize - voff);
429 }
430
431 if (vbo + blocksize > data_size)
432 nbits = 8 * (data_size - vbo);
433
434 ok = nbits > from ? (*fn)((ulong *)bh->b_data, from, nbits, ret)
435 : false;
436 put_bh(bh);
437
438 if (ok) {
439 *ret += 8 * vbo;
440 return 0;
441 }
442
443 if (++vblock >= eblock) {
444 *ret = MINUS_ONE_T;
445 return 0;
446 }
447 }
448 blk = 0;
449 vcn_next = vcn + clen;
450 down_read(lock);
451 ok = run_get_entry(run, ++idx, &vcn, &lcn, &clen) && vcn == vcn_next;
452 if (!ok)
453 vcn = vcn_next;
454 up_read(lock);
455 goto next_run;
456}
457
458static bool scan_for_free(const ulong *buf, u32 bit, u32 bits, size_t *ret)
459{
460 size_t pos = find_next_zero_bit(buf, bits, bit);
461
462 if (pos >= bits)
463 return false;
464 *ret = pos;
465 return true;
466}
467
468/*
Kari Argillandere8b8e972021-08-03 14:57:09 +0300469 * indx_find_free - Look for free bit.
Konstantin Komarov82cae262021-08-13 17:21:29 +0300470 *
Kari Argillandere8b8e972021-08-03 14:57:09 +0300471 * Return: -1 if no free bits.
Konstantin Komarov82cae262021-08-13 17:21:29 +0300472 */
473static int indx_find_free(struct ntfs_index *indx, struct ntfs_inode *ni,
474 size_t *bit, struct ATTRIB **bitmap)
475{
476 struct ATTRIB *b;
477 struct ATTR_LIST_ENTRY *le = NULL;
478 const struct INDEX_NAMES *in = &s_index_names[indx->type];
479 int err;
480
481 b = ni_find_attr(ni, NULL, &le, ATTR_BITMAP, in->name, in->name_len,
482 NULL, NULL);
483
484 if (!b)
485 return -ENOENT;
486
487 *bitmap = b;
488 *bit = MINUS_ONE_T;
489
490 if (!b->non_res) {
491 u32 nbits = 8 * le32_to_cpu(b->res.data_size);
492 size_t pos = find_next_zero_bit(resident_data(b), nbits, 0);
493
494 if (pos < nbits)
495 *bit = pos;
496 } else {
497 err = scan_nres_bitmap(ni, b, indx, 0, &scan_for_free, bit);
498
499 if (err)
500 return err;
501 }
502
503 return 0;
504}
505
506static bool scan_for_used(const ulong *buf, u32 bit, u32 bits, size_t *ret)
507{
508 size_t pos = find_next_bit(buf, bits, bit);
509
510 if (pos >= bits)
511 return false;
512 *ret = pos;
513 return true;
514}
515
516/*
Kari Argillandere8b8e972021-08-03 14:57:09 +0300517 * indx_used_bit - Look for used bit.
Konstantin Komarov82cae262021-08-13 17:21:29 +0300518 *
Kari Argillandere8b8e972021-08-03 14:57:09 +0300519 * Return: MINUS_ONE_T if no used bits.
Konstantin Komarov82cae262021-08-13 17:21:29 +0300520 */
521int indx_used_bit(struct ntfs_index *indx, struct ntfs_inode *ni, size_t *bit)
522{
523 struct ATTRIB *b;
524 struct ATTR_LIST_ENTRY *le = NULL;
525 size_t from = *bit;
526 const struct INDEX_NAMES *in = &s_index_names[indx->type];
527 int err;
528
529 b = ni_find_attr(ni, NULL, &le, ATTR_BITMAP, in->name, in->name_len,
530 NULL, NULL);
531
532 if (!b)
533 return -ENOENT;
534
535 *bit = MINUS_ONE_T;
536
537 if (!b->non_res) {
538 u32 nbits = le32_to_cpu(b->res.data_size) * 8;
539 size_t pos = find_next_bit(resident_data(b), nbits, from);
540
541 if (pos < nbits)
542 *bit = pos;
543 } else {
544 err = scan_nres_bitmap(ni, b, indx, from, &scan_for_used, bit);
545 if (err)
546 return err;
547 }
548
549 return 0;
550}
551
552/*
553 * hdr_find_split
554 *
Kari Argillandere8b8e972021-08-03 14:57:09 +0300555 * Find a point at which the index allocation buffer would like to be split.
556 * NOTE: This function should never return 'END' entry NULL returns on error.
Konstantin Komarov82cae262021-08-13 17:21:29 +0300557 */
558static const struct NTFS_DE *hdr_find_split(const struct INDEX_HDR *hdr)
559{
560 size_t o;
561 const struct NTFS_DE *e = hdr_first_de(hdr);
562 u32 used_2 = le32_to_cpu(hdr->used) >> 1;
Dan Carpenter8c83a482021-08-24 10:50:15 +0300563 u16 esize;
Konstantin Komarov82cae262021-08-13 17:21:29 +0300564
565 if (!e || de_is_last(e))
566 return NULL;
567
Dan Carpenter8c83a482021-08-24 10:50:15 +0300568 esize = le16_to_cpu(e->size);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300569 for (o = le32_to_cpu(hdr->de_off) + esize; o < used_2; o += esize) {
570 const struct NTFS_DE *p = e;
571
572 e = Add2Ptr(hdr, o);
573
Kari Argillandere8b8e972021-08-03 14:57:09 +0300574 /* We must not return END entry. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300575 if (de_is_last(e))
576 return p;
577
578 esize = le16_to_cpu(e->size);
579 }
580
581 return e;
582}
583
584/*
Kari Argillandere8b8e972021-08-03 14:57:09 +0300585 * hdr_insert_head - Insert some entries at the beginning of the buffer.
Konstantin Komarov82cae262021-08-13 17:21:29 +0300586 *
Konstantin Komarov82cae262021-08-13 17:21:29 +0300587 * It is used to insert entries into a newly-created buffer.
588 */
589static const struct NTFS_DE *hdr_insert_head(struct INDEX_HDR *hdr,
590 const void *ins, u32 ins_bytes)
591{
592 u32 to_move;
593 struct NTFS_DE *e = hdr_first_de(hdr);
594 u32 used = le32_to_cpu(hdr->used);
595
596 if (!e)
597 return NULL;
598
599 /* Now we just make room for the inserted entries and jam it in. */
600 to_move = used - le32_to_cpu(hdr->de_off);
601 memmove(Add2Ptr(e, ins_bytes), e, to_move);
602 memcpy(e, ins, ins_bytes);
603 hdr->used = cpu_to_le32(used + ins_bytes);
604
605 return e;
606}
607
608void fnd_clear(struct ntfs_fnd *fnd)
609{
610 int i;
611
612 for (i = 0; i < fnd->level; i++) {
613 struct indx_node *n = fnd->nodes[i];
614
615 if (!n)
616 continue;
617
618 put_indx_node(n);
619 fnd->nodes[i] = NULL;
620 }
621 fnd->level = 0;
622 fnd->root_de = NULL;
623}
624
625static int fnd_push(struct ntfs_fnd *fnd, struct indx_node *n,
626 struct NTFS_DE *e)
627{
628 int i;
629
630 i = fnd->level;
631 if (i < 0 || i >= ARRAY_SIZE(fnd->nodes))
632 return -EINVAL;
633 fnd->nodes[i] = n;
634 fnd->de[i] = e;
635 fnd->level += 1;
636 return 0;
637}
638
639static struct indx_node *fnd_pop(struct ntfs_fnd *fnd)
640{
641 struct indx_node *n;
642 int i = fnd->level;
643
644 i -= 1;
645 n = fnd->nodes[i];
646 fnd->nodes[i] = NULL;
647 fnd->level = i;
648
649 return n;
650}
651
652static bool fnd_is_empty(struct ntfs_fnd *fnd)
653{
654 if (!fnd->level)
655 return !fnd->root_de;
656
657 return !fnd->de[fnd->level - 1];
658}
659
660/*
Kari Argillandere8b8e972021-08-03 14:57:09 +0300661 * hdr_find_e - Locate an entry the index buffer.
Konstantin Komarov82cae262021-08-13 17:21:29 +0300662 *
Konstantin Komarov82cae262021-08-13 17:21:29 +0300663 * If no matching entry is found, it returns the first entry which is greater
664 * than the desired entry If the search key is greater than all the entries the
665 * buffer, it returns the 'end' entry. This function does a binary search of the
Kari Argillandere8b8e972021-08-03 14:57:09 +0300666 * current index buffer, for the first entry that is <= to the search value.
667 *
668 * Return: NULL if error.
Konstantin Komarov82cae262021-08-13 17:21:29 +0300669 */
670static struct NTFS_DE *hdr_find_e(const struct ntfs_index *indx,
671 const struct INDEX_HDR *hdr, const void *key,
672 size_t key_len, const void *ctx, int *diff)
673{
674 struct NTFS_DE *e;
675 NTFS_CMP_FUNC cmp = indx->cmp;
676 u32 e_size, e_key_len;
677 u32 end = le32_to_cpu(hdr->used);
678 u32 off = le32_to_cpu(hdr->de_off);
679
680#ifdef NTFS3_INDEX_BINARY_SEARCH
681 int max_idx = 0, fnd, min_idx;
682 int nslots = 64;
683 u16 *offs;
684
685 if (end > 0x10000)
686 goto next;
687
Kari Argillander195c52b2021-08-24 21:37:07 +0300688 offs = kmalloc(sizeof(u16) * nslots, GFP_NOFS);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300689 if (!offs)
690 goto next;
691
Kari Argillandere8b8e972021-08-03 14:57:09 +0300692 /* Use binary search algorithm. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300693next1:
694 if (off + sizeof(struct NTFS_DE) > end) {
695 e = NULL;
696 goto out1;
697 }
698 e = Add2Ptr(hdr, off);
699 e_size = le16_to_cpu(e->size);
700
701 if (e_size < sizeof(struct NTFS_DE) || off + e_size > end) {
702 e = NULL;
703 goto out1;
704 }
705
706 if (max_idx >= nslots) {
707 u16 *ptr;
Kari Argillanderfa3cacf2021-08-26 11:56:29 +0300708 int new_slots = ALIGN(2 * nslots, 8);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300709
Kari Argillander195c52b2021-08-24 21:37:07 +0300710 ptr = kmalloc(sizeof(u16) * new_slots, GFP_NOFS);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300711 if (ptr)
712 memcpy(ptr, offs, sizeof(u16) * max_idx);
Kari Argillander195c52b2021-08-24 21:37:07 +0300713 kfree(offs);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300714 offs = ptr;
715 nslots = new_slots;
716 if (!ptr)
717 goto next;
718 }
719
Kari Argillandere8b8e972021-08-03 14:57:09 +0300720 /* Store entry table. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300721 offs[max_idx] = off;
722
723 if (!de_is_last(e)) {
724 off += e_size;
725 max_idx += 1;
726 goto next1;
727 }
728
729 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +0300730 * Table of pointers is created.
731 * Use binary search to find entry that is <= to the search value.
Konstantin Komarov82cae262021-08-13 17:21:29 +0300732 */
733 fnd = -1;
734 min_idx = 0;
735
736 while (min_idx <= max_idx) {
737 int mid_idx = min_idx + ((max_idx - min_idx) >> 1);
738 int diff2;
739
740 e = Add2Ptr(hdr, offs[mid_idx]);
741
742 e_key_len = le16_to_cpu(e->key_size);
743
744 diff2 = (*cmp)(key, key_len, e + 1, e_key_len, ctx);
745
746 if (!diff2) {
747 *diff = 0;
748 goto out1;
749 }
750
751 if (diff2 < 0) {
752 max_idx = mid_idx - 1;
753 fnd = mid_idx;
754 if (!fnd)
755 break;
756 } else {
757 min_idx = mid_idx + 1;
758 }
759 }
760
761 if (fnd == -1) {
762 e = NULL;
763 goto out1;
764 }
765
766 *diff = -1;
767 e = Add2Ptr(hdr, offs[fnd]);
768
769out1:
Kari Argillander195c52b2021-08-24 21:37:07 +0300770 kfree(offs);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300771
772 return e;
773#endif
774
775next:
776 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +0300777 * Entries index are sorted.
778 * Enumerate all entries until we find entry
779 * that is <= to the search value.
Konstantin Komarov82cae262021-08-13 17:21:29 +0300780 */
781 if (off + sizeof(struct NTFS_DE) > end)
782 return NULL;
783
784 e = Add2Ptr(hdr, off);
785 e_size = le16_to_cpu(e->size);
786
787 if (e_size < sizeof(struct NTFS_DE) || off + e_size > end)
788 return NULL;
789
790 off += e_size;
791
792 e_key_len = le16_to_cpu(e->key_size);
793
794 *diff = (*cmp)(key, key_len, e + 1, e_key_len, ctx);
795 if (!*diff)
796 return e;
797
798 if (*diff <= 0)
799 return e;
800
801 if (de_is_last(e)) {
802 *diff = 1;
803 return e;
804 }
805 goto next;
806}
807
808/*
Kari Argillandere8b8e972021-08-03 14:57:09 +0300809 * hdr_insert_de - Insert an index entry into the buffer.
Konstantin Komarov82cae262021-08-13 17:21:29 +0300810 *
Kari Argillandere8b8e972021-08-03 14:57:09 +0300811 * 'before' should be a pointer previously returned from hdr_find_e.
Konstantin Komarov82cae262021-08-13 17:21:29 +0300812 */
813static struct NTFS_DE *hdr_insert_de(const struct ntfs_index *indx,
814 struct INDEX_HDR *hdr,
815 const struct NTFS_DE *de,
816 struct NTFS_DE *before, const void *ctx)
817{
818 int diff;
819 size_t off = PtrOffset(hdr, before);
820 u32 used = le32_to_cpu(hdr->used);
821 u32 total = le32_to_cpu(hdr->total);
822 u16 de_size = le16_to_cpu(de->size);
823
Kari Argillandere8b8e972021-08-03 14:57:09 +0300824 /* First, check to see if there's enough room. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300825 if (used + de_size > total)
826 return NULL;
827
828 /* We know there's enough space, so we know we'll succeed. */
829 if (before) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300830 /* Check that before is inside Index. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300831 if (off >= used || off < le32_to_cpu(hdr->de_off) ||
832 off + le16_to_cpu(before->size) > total) {
833 return NULL;
834 }
835 goto ok;
836 }
Kari Argillandere8b8e972021-08-03 14:57:09 +0300837 /* No insert point is applied. Get it manually. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300838 before = hdr_find_e(indx, hdr, de + 1, le16_to_cpu(de->key_size), ctx,
839 &diff);
840 if (!before)
841 return NULL;
842 off = PtrOffset(hdr, before);
843
844ok:
845 /* Now we just make room for the entry and jam it in. */
846 memmove(Add2Ptr(before, de_size), before, used - off);
847
848 hdr->used = cpu_to_le32(used + de_size);
849 memcpy(before, de, de_size);
850
851 return before;
852}
853
854/*
Kari Argillandere8b8e972021-08-03 14:57:09 +0300855 * hdr_delete_de - Remove an entry from the index buffer.
Konstantin Komarov82cae262021-08-13 17:21:29 +0300856 */
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
Kari Argillandere8b8e972021-08-03 14:57:09 +0300887 /* Check root fields. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300888 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
Kari Argillandere8b8e972021-08-03 14:57:09 +0300897 /* Check index record size. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300898 if (t32 < sbi->cluster_size) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300899 /* Index record is smaller than a cluster, use 512 blocks. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300900 if (t32 != root->index_block_clst * SECTOR_SIZE)
901 return -EINVAL;
902
Kari Argillandere8b8e972021-08-03 14:57:09 +0300903 /* Check alignment to a cluster. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300904 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 {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300911 /* Index record must be a multiple of cluster size. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300912 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
Kari Argillander195c52b2021-08-24 21:37:07 +0300938 r = kzalloc(sizeof(struct indx_node), GFP_NOFS);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300939 if (!r)
940 return ERR_PTR(-ENOMEM);
941
Kari Argillander195c52b2021-08-24 21:37:07 +0300942 index = kzalloc(bytes, GFP_NOFS);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300943 if (!index) {
Kari Argillander195c52b2021-08-24 21:37:07 +0300944 kfree(r);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300945 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) {
Kari Argillander195c52b2021-08-24 21:37:07 +0300951 kfree(index);
952 kfree(r);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300953 return ERR_PTR(err);
954 }
955
Kari Argillandere8b8e972021-08-03 14:57:09 +0300956 /* Create header. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300957 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;
Kari Argillanderfa3cacf2021-08-26 11:56:29 +0300963 eo = ALIGN(sizeof(struct INDEX_BUFFER) + fn * sizeof(short), 8);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300964 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/*
Kari Argillandere8b8e972021-08-03 14:57:09 +03001014 * indx_read
1015 *
1016 * If ntfs_readdir calls this function
1017 * inode is shared locked and no ni_lock.
1018 * Use rw_semaphore for read/write access to alloc_run.
Konstantin Komarov82cae262021-08-13 17:21:29 +03001019 */
1020int indx_read(struct ntfs_index *indx, struct ntfs_inode *ni, CLST vbn,
1021 struct indx_node **node)
1022{
1023 int err;
1024 struct INDEX_BUFFER *ib;
1025 struct runs_tree *run = &indx->alloc_run;
1026 struct rw_semaphore *lock = &indx->run_lock;
1027 u64 vbo = (u64)vbn << indx->vbn2vbo_bits;
1028 u32 bytes = 1u << indx->index_bits;
1029 struct indx_node *in = *node;
1030 const struct INDEX_NAMES *name;
1031
1032 if (!in) {
Kari Argillander195c52b2021-08-24 21:37:07 +03001033 in = kzalloc(sizeof(struct indx_node), GFP_NOFS);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001034 if (!in)
1035 return -ENOMEM;
1036 } else {
1037 nb_put(&in->nb);
1038 }
1039
1040 ib = in->index;
1041 if (!ib) {
Kari Argillander195c52b2021-08-24 21:37:07 +03001042 ib = kmalloc(bytes, GFP_NOFS);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001043 if (!ib) {
1044 err = -ENOMEM;
1045 goto out;
1046 }
1047 }
1048
1049 down_read(lock);
1050 err = ntfs_read_bh(ni->mi.sbi, run, vbo, &ib->rhdr, bytes, &in->nb);
1051 up_read(lock);
1052 if (!err)
1053 goto ok;
1054
1055 if (err == -E_NTFS_FIXUP)
1056 goto ok;
1057
1058 if (err != -ENOENT)
1059 goto out;
1060
1061 name = &s_index_names[indx->type];
1062 down_write(lock);
1063 err = attr_load_runs_range(ni, ATTR_ALLOC, name->name, name->name_len,
1064 run, vbo, vbo + bytes);
1065 up_write(lock);
1066 if (err)
1067 goto out;
1068
1069 down_read(lock);
1070 err = ntfs_read_bh(ni->mi.sbi, run, vbo, &ib->rhdr, bytes, &in->nb);
1071 up_read(lock);
1072 if (err == -E_NTFS_FIXUP)
1073 goto ok;
1074
1075 if (err)
1076 goto out;
1077
1078ok:
1079 if (err == -E_NTFS_FIXUP) {
1080 ntfs_write_bh(ni->mi.sbi, &ib->rhdr, &in->nb, 0);
1081 err = 0;
1082 }
1083
1084 in->index = ib;
1085 *node = in;
1086
1087out:
1088 if (ib != in->index)
Kari Argillander195c52b2021-08-24 21:37:07 +03001089 kfree(ib);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001090
1091 if (*node != in) {
1092 nb_put(&in->nb);
Kari Argillander195c52b2021-08-24 21:37:07 +03001093 kfree(in);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001094 }
1095
1096 return err;
1097}
1098
1099/*
Kari Argillandere8b8e972021-08-03 14:57:09 +03001100 * indx_find - Scan NTFS directory for given entry.
Konstantin Komarov82cae262021-08-13 17:21:29 +03001101 */
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
Kari Argillandere8b8e972021-08-03 14:57:09 +03001122 /* Check cache. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001123 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
Kari Argillandere8b8e972021-08-03 14:57:09 +03001131 /* Soft finder reset. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001132 fnd_clear(fnd);
1133
Kari Argillandere8b8e972021-08-03 14:57:09 +03001134 /* Lookup entry that is <= to the search value. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001135 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
Kari Argillandere8b8e972021-08-03 14:57:09 +03001156 /* Lookup entry that is <= to the search value. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001157 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) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001183 /* Start find. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001184 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
Kari Argillandere8b8e972021-08-03 14:57:09 +03001213 /* Just to avoid tree cycle. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001214next_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);
Kari Argillander195c52b2021-08-24 21:37:07 +03001223 kfree(n);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001224 }
1225 return -EINVAL;
1226 }
1227
Kari Argillandere8b8e972021-08-03 14:57:09 +03001228 /* Read next level. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001229 err = indx_read(indx, ni, de_get_vbn(e), &n);
1230 if (err)
1231 return err;
1232
Kari Argillandere8b8e972021-08-03 14:57:09 +03001233 /* Try next level. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001234 e = hdr_first_de(&n->index->ihdr);
1235 if (!e) {
Kari Argillander195c52b2021-08-24 21:37:07 +03001236 kfree(n);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001237 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
Kari Argillandere8b8e972021-08-03 14:57:09 +03001253 /* Pop one level. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001254 if (n) {
1255 fnd_pop(fnd);
Kari Argillander195c52b2021-08-24 21:37:07 +03001256 kfree(n);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001257 }
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
Kari Argillandere8b8e972021-08-03 14:57:09 +03001295 /* Use non sorted algorithm. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001296 if (!*entry) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001297 /* This is the first call. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001298 e = hdr_first_de(&root->ihdr);
1299 if (!e)
1300 return 0;
1301 fnd_clear(fnd);
1302 fnd->root_de = e;
1303
Kari Argillandere8b8e972021-08-03 14:57:09 +03001304 /* The first call with setup of initial element. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001305 if (*off >= record_size) {
1306 next_vbn = (((*off - record_size) >> indx->index_bits))
1307 << indx->idx2vbn_bits;
Kari Argillandere8b8e972021-08-03 14:57:09 +03001308 /* Jump inside cycle 'for'. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001309 goto next;
1310 }
1311
Kari Argillandere8b8e972021-08-03 14:57:09 +03001312 /* Start enumeration from root. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001313 *off = 0;
1314 } else if (!fnd->root_de)
1315 return -EINVAL;
1316
1317 for (;;) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001318 /* Check if current entry can be used. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001319 if (e && le16_to_cpu(e->size) > sizeof(struct NTFS_DE))
1320 goto ok;
1321
1322 if (!fnd->level) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001323 /* Continue to enumerate root. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001324 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
Kari Argillandere8b8e972021-08-03 14:57:09 +03001332 /* Start to enumerate indexes from 0. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001333 next_vbn = 0;
1334 } else {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001335 /* Continue to enumerate indexes. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001336 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
Kari Argillandere8b8e972021-08-03 14:57:09 +03001348 /* Continue with next index. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001349 next_vbn = le64_to_cpu(n->index->vbn) +
1350 root->index_block_clst;
1351 }
1352
1353next:
Kari Argillandere8b8e972021-08-03 14:57:09 +03001354 /* Release current index. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001355 if (n) {
1356 fnd_pop(fnd);
1357 put_indx_node(n);
1358 n = NULL;
1359 }
1360
Kari Argillandere8b8e972021-08-03 14:57:09 +03001361 /* Skip all free indexes. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001362 bit = next_vbn >> indx->idx2vbn_bits;
1363 err = indx_used_bit(indx, ni, &bit);
1364 if (err == -ENOENT || bit == MINUS_ONE_T) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001365 /* No used indexes. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001366 *entry = NULL;
1367 return 0;
1368 }
1369
1370 next_used_vbn = bit << indx->idx2vbn_bits;
1371
Kari Argillandere8b8e972021-08-03 14:57:09 +03001372 /* Read buffer into memory. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001373 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:
Kari Argillandere8b8e972021-08-03 14:57:09 +03001384 /* Return offset to restore enumerator if necessary. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001385 if (!n) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001386 /* 'e' points in root, */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001387 *off = PtrOffset(&root->ihdr, e);
1388 } else {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001389 /* 'e' points in index, */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001390 *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/*
Kari Argillandere8b8e972021-08-03 14:57:09 +03001399 * indx_create_allocate - Create "Allocation + Bitmap" attributes.
Konstantin Komarov82cae262021-08-13 17:21:29 +03001400 */
1401static int indx_create_allocate(struct ntfs_index *indx, struct ntfs_inode *ni,
1402 CLST *vbn)
1403{
1404 int err = -ENOMEM;
1405 struct ntfs_sb_info *sbi = ni->mi.sbi;
1406 struct ATTRIB *bitmap;
1407 struct ATTRIB *alloc;
1408 u32 data_size = 1u << indx->index_bits;
1409 u32 alloc_size = ntfs_up_cluster(sbi, data_size);
1410 CLST len = alloc_size >> sbi->cluster_bits;
1411 const struct INDEX_NAMES *in = &s_index_names[indx->type];
1412 CLST alen;
1413 struct runs_tree run;
1414
1415 run_init(&run);
1416
1417 err = attr_allocate_clusters(sbi, &run, 0, 0, len, NULL, 0, &alen, 0,
1418 NULL);
1419 if (err)
1420 goto out;
1421
1422 err = ni_insert_nonresident(ni, ATTR_ALLOC, in->name, in->name_len,
1423 &run, 0, len, 0, &alloc, NULL);
1424 if (err)
1425 goto out1;
1426
1427 alloc->nres.valid_size = alloc->nres.data_size = cpu_to_le64(data_size);
1428
1429 err = ni_insert_resident(ni, bitmap_size(1), ATTR_BITMAP, in->name,
1430 in->name_len, &bitmap, NULL);
1431 if (err)
1432 goto out2;
1433
1434 if (in->name == I30_NAME) {
1435 ni->vfs_inode.i_size = data_size;
1436 inode_set_bytes(&ni->vfs_inode, alloc_size);
1437 }
1438
1439 memcpy(&indx->alloc_run, &run, sizeof(run));
1440
1441 *vbn = 0;
1442
1443 return 0;
1444
1445out2:
1446 mi_remove_attr(&ni->mi, alloc);
1447
1448out1:
1449 run_deallocate(sbi, &run, false);
1450
1451out:
1452 return err;
1453}
1454
1455/*
Kari Argillandere8b8e972021-08-03 14:57:09 +03001456 * indx_add_allocate - Add clusters to index.
Konstantin Komarov82cae262021-08-13 17:21:29 +03001457 */
1458static int indx_add_allocate(struct ntfs_index *indx, struct ntfs_inode *ni,
1459 CLST *vbn)
1460{
1461 int err;
1462 size_t bit;
1463 u64 data_size;
1464 u64 bmp_size, bmp_size_v;
1465 struct ATTRIB *bmp, *alloc;
1466 struct mft_inode *mi;
1467 const struct INDEX_NAMES *in = &s_index_names[indx->type];
1468
1469 err = indx_find_free(indx, ni, &bit, &bmp);
1470 if (err)
1471 goto out1;
1472
1473 if (bit != MINUS_ONE_T) {
1474 bmp = NULL;
1475 } else {
1476 if (bmp->non_res) {
1477 bmp_size = le64_to_cpu(bmp->nres.data_size);
1478 bmp_size_v = le64_to_cpu(bmp->nres.valid_size);
1479 } else {
1480 bmp_size = bmp_size_v = le32_to_cpu(bmp->res.data_size);
1481 }
1482
1483 bit = bmp_size << 3;
1484 }
1485
1486 data_size = (u64)(bit + 1) << indx->index_bits;
1487
1488 if (bmp) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001489 /* Increase bitmap. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001490 err = attr_set_size(ni, ATTR_BITMAP, in->name, in->name_len,
1491 &indx->bitmap_run, bitmap_size(bit + 1),
1492 NULL, true, NULL);
1493 if (err)
1494 goto out1;
1495 }
1496
1497 alloc = ni_find_attr(ni, NULL, NULL, ATTR_ALLOC, in->name, in->name_len,
1498 NULL, &mi);
1499 if (!alloc) {
Dan Carpenter04810f02021-08-24 10:49:32 +03001500 err = -EINVAL;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001501 if (bmp)
1502 goto out2;
1503 goto out1;
1504 }
1505
Kari Argillandere8b8e972021-08-03 14:57:09 +03001506 /* Increase allocation. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001507 err = attr_set_size(ni, ATTR_ALLOC, in->name, in->name_len,
1508 &indx->alloc_run, data_size, &data_size, true,
1509 NULL);
1510 if (err) {
1511 if (bmp)
1512 goto out2;
1513 goto out1;
1514 }
1515
1516 *vbn = bit << indx->idx2vbn_bits;
1517
1518 return 0;
1519
1520out2:
Kari Argillandere8b8e972021-08-03 14:57:09 +03001521 /* Ops. No space? */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001522 attr_set_size(ni, ATTR_BITMAP, in->name, in->name_len,
1523 &indx->bitmap_run, bmp_size, &bmp_size_v, false, NULL);
1524
1525out1:
1526 return err;
1527}
1528
1529/*
Kari Argillandere8b8e972021-08-03 14:57:09 +03001530 * indx_insert_into_root - Attempt to insert an entry into the index root.
Konstantin Komarov82cae262021-08-13 17:21:29 +03001531 *
Konstantin Komarov82cae262021-08-13 17:21:29 +03001532 * If necessary, it will twiddle the index b-tree.
1533 */
1534static int indx_insert_into_root(struct ntfs_index *indx, struct ntfs_inode *ni,
1535 const struct NTFS_DE *new_de,
1536 struct NTFS_DE *root_de, const void *ctx,
1537 struct ntfs_fnd *fnd)
1538{
1539 int err = 0;
1540 struct NTFS_DE *e, *e0, *re;
1541 struct mft_inode *mi;
1542 struct ATTRIB *attr;
1543 struct MFT_REC *rec;
1544 struct INDEX_HDR *hdr;
1545 struct indx_node *n;
1546 CLST new_vbn;
1547 __le64 *sub_vbn, t_vbn;
1548 u16 new_de_size;
1549 u32 hdr_used, hdr_total, asize, used, to_move;
1550 u32 root_size, new_root_size;
1551 struct ntfs_sb_info *sbi;
1552 int ds_root;
Dan Carpenterb8155e92021-08-24 10:51:04 +03001553 struct INDEX_ROOT *root, *a_root;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001554
Kari Argillandere8b8e972021-08-03 14:57:09 +03001555 /* Get the record this root placed in. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001556 root = indx_get_root(indx, ni, &attr, &mi);
1557 if (!root)
Dan Carpenterb8155e92021-08-24 10:51:04 +03001558 return -EINVAL;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001559
1560 /*
1561 * Try easy case:
1562 * hdr_insert_de will succeed if there's room the root for the new entry.
1563 */
1564 hdr = &root->ihdr;
1565 sbi = ni->mi.sbi;
1566 rec = mi->mrec;
1567 used = le32_to_cpu(rec->used);
1568 new_de_size = le16_to_cpu(new_de->size);
1569 hdr_used = le32_to_cpu(hdr->used);
1570 hdr_total = le32_to_cpu(hdr->total);
1571 asize = le32_to_cpu(attr->size);
1572 root_size = le32_to_cpu(attr->res.data_size);
1573
1574 ds_root = new_de_size + hdr_used - hdr_total;
1575
1576 if (used + ds_root < sbi->max_bytes_per_attr) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001577 /* Make a room for new elements. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001578 mi_resize_attr(mi, attr, ds_root);
1579 hdr->total = cpu_to_le32(hdr_total + ds_root);
1580 e = hdr_insert_de(indx, hdr, new_de, root_de, ctx);
1581 WARN_ON(!e);
1582 fnd_clear(fnd);
1583 fnd->root_de = e;
1584
1585 return 0;
1586 }
1587
Kari Argillandere8b8e972021-08-03 14:57:09 +03001588 /* Make a copy of root attribute to restore if error. */
Kari Argillander195c52b2021-08-24 21:37:07 +03001589 a_root = kmemdup(attr, asize, GFP_NOFS);
Dan Carpenterb8155e92021-08-24 10:51:04 +03001590 if (!a_root)
1591 return -ENOMEM;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001592
Kari Argillandere8b8e972021-08-03 14:57:09 +03001593 /*
1594 * Copy all the non-end entries from
1595 * the index root to the new buffer.
1596 */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001597 to_move = 0;
1598 e0 = hdr_first_de(hdr);
1599
Kari Argillandere8b8e972021-08-03 14:57:09 +03001600 /* Calculate the size to copy. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001601 for (e = e0;; e = hdr_next_de(hdr, e)) {
1602 if (!e) {
1603 err = -EINVAL;
Dan Carpenterb8155e92021-08-24 10:51:04 +03001604 goto out_free_root;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001605 }
1606
1607 if (de_is_last(e))
1608 break;
1609 to_move += le16_to_cpu(e->size);
1610 }
1611
Konstantin Komarov82cae262021-08-13 17:21:29 +03001612 if (!to_move) {
1613 re = NULL;
1614 } else {
Kari Argillander195c52b2021-08-24 21:37:07 +03001615 re = kmemdup(e0, to_move, GFP_NOFS);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001616 if (!re) {
1617 err = -ENOMEM;
Dan Carpenterb8155e92021-08-24 10:51:04 +03001618 goto out_free_root;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001619 }
1620 }
1621
1622 sub_vbn = NULL;
1623 if (de_has_vcn(e)) {
1624 t_vbn = de_get_vbn_le(e);
1625 sub_vbn = &t_vbn;
1626 }
1627
1628 new_root_size = sizeof(struct INDEX_ROOT) + sizeof(struct NTFS_DE) +
1629 sizeof(u64);
1630 ds_root = new_root_size - root_size;
1631
1632 if (ds_root > 0 && used + ds_root > sbi->max_bytes_per_attr) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001633 /* Make root external. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001634 err = -EOPNOTSUPP;
Dan Carpenterb8155e92021-08-24 10:51:04 +03001635 goto out_free_re;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001636 }
1637
1638 if (ds_root)
1639 mi_resize_attr(mi, attr, ds_root);
1640
Kari Argillandere8b8e972021-08-03 14:57:09 +03001641 /* Fill first entry (vcn will be set later). */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001642 e = (struct NTFS_DE *)(root + 1);
1643 memset(e, 0, sizeof(struct NTFS_DE));
1644 e->size = cpu_to_le16(sizeof(struct NTFS_DE) + sizeof(u64));
1645 e->flags = NTFS_IE_HAS_SUBNODES | NTFS_IE_LAST;
1646
1647 hdr->flags = 1;
1648 hdr->used = hdr->total =
1649 cpu_to_le32(new_root_size - offsetof(struct INDEX_ROOT, ihdr));
1650
1651 fnd->root_de = hdr_first_de(hdr);
1652 mi->dirty = true;
1653
Kari Argillandere8b8e972021-08-03 14:57:09 +03001654 /* Create alloc and bitmap attributes (if not). */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001655 err = run_is_empty(&indx->alloc_run)
1656 ? indx_create_allocate(indx, ni, &new_vbn)
1657 : indx_add_allocate(indx, ni, &new_vbn);
1658
Kari Argillandere8b8e972021-08-03 14:57:09 +03001659 /* Layout of record may be changed, so rescan root. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001660 root = indx_get_root(indx, ni, &attr, &mi);
1661 if (!root) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001662 /* Bug? */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001663 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
1664 err = -EINVAL;
Dan Carpenterb8155e92021-08-24 10:51:04 +03001665 goto out_free_re;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001666 }
1667
1668 if (err) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001669 /* Restore root. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001670 if (mi_resize_attr(mi, attr, -ds_root))
1671 memcpy(attr, a_root, asize);
1672 else {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001673 /* Bug? */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001674 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
1675 }
Dan Carpenterb8155e92021-08-24 10:51:04 +03001676 goto out_free_re;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001677 }
1678
1679 e = (struct NTFS_DE *)(root + 1);
1680 *(__le64 *)(e + 1) = cpu_to_le64(new_vbn);
1681 mi->dirty = true;
1682
Kari Argillandere8b8e972021-08-03 14:57:09 +03001683 /* Now we can create/format the new buffer and copy the entries into. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001684 n = indx_new(indx, ni, new_vbn, sub_vbn);
1685 if (IS_ERR(n)) {
1686 err = PTR_ERR(n);
Dan Carpenterb8155e92021-08-24 10:51:04 +03001687 goto out_free_re;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001688 }
1689
1690 hdr = &n->index->ihdr;
1691 hdr_used = le32_to_cpu(hdr->used);
1692 hdr_total = le32_to_cpu(hdr->total);
1693
Kari Argillandere8b8e972021-08-03 14:57:09 +03001694 /* Copy root entries into new buffer. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001695 hdr_insert_head(hdr, re, to_move);
1696
Kari Argillandere8b8e972021-08-03 14:57:09 +03001697 /* Update bitmap attribute. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001698 indx_mark_used(indx, ni, new_vbn >> indx->idx2vbn_bits);
1699
Kari Argillandere8b8e972021-08-03 14:57:09 +03001700 /* Check if we can insert new entry new index buffer. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001701 if (hdr_used + new_de_size > hdr_total) {
1702 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +03001703 * This occurs if MFT record is the same or bigger than index
Konstantin Komarov82cae262021-08-13 17:21:29 +03001704 * buffer. Move all root new index and have no space to add
Kari Argillandere8b8e972021-08-03 14:57:09 +03001705 * new entry classic case when MFT record is 1K and index
1706 * buffer 4K the problem should not occurs.
Konstantin Komarov82cae262021-08-13 17:21:29 +03001707 */
Kari Argillander195c52b2021-08-24 21:37:07 +03001708 kfree(re);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001709 indx_write(indx, ni, n, 0);
1710
1711 put_indx_node(n);
1712 fnd_clear(fnd);
1713 err = indx_insert_entry(indx, ni, new_de, ctx, fnd);
Dan Carpenterb8155e92021-08-24 10:51:04 +03001714 goto out_free_root;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001715 }
1716
1717 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +03001718 * Now root is a parent for new index buffer.
1719 * Insert NewEntry a new buffer.
Konstantin Komarov82cae262021-08-13 17:21:29 +03001720 */
1721 e = hdr_insert_de(indx, hdr, new_de, NULL, ctx);
1722 if (!e) {
1723 err = -EINVAL;
Dan Carpenterb8155e92021-08-24 10:51:04 +03001724 goto out_put_n;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001725 }
1726 fnd_push(fnd, n, e);
1727
Kari Argillandere8b8e972021-08-03 14:57:09 +03001728 /* Just write updates index into disk. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001729 indx_write(indx, ni, n, 0);
1730
1731 n = NULL;
1732
Dan Carpenterb8155e92021-08-24 10:51:04 +03001733out_put_n:
1734 put_indx_node(n);
1735out_free_re:
Kari Argillander195c52b2021-08-24 21:37:07 +03001736 kfree(re);
Dan Carpenterb8155e92021-08-24 10:51:04 +03001737out_free_root:
Kari Argillander195c52b2021-08-24 21:37:07 +03001738 kfree(a_root);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001739 return err;
1740}
1741
1742/*
1743 * indx_insert_into_buffer
1744 *
Kari Argillandere8b8e972021-08-03 14:57:09 +03001745 * Attempt to insert an entry into an Index Allocation Buffer.
Konstantin Komarov82cae262021-08-13 17:21:29 +03001746 * If necessary, it will split the buffer.
1747 */
1748static int
1749indx_insert_into_buffer(struct ntfs_index *indx, struct ntfs_inode *ni,
1750 struct INDEX_ROOT *root, const struct NTFS_DE *new_de,
1751 const void *ctx, int level, struct ntfs_fnd *fnd)
1752{
1753 int err;
1754 const struct NTFS_DE *sp;
1755 struct NTFS_DE *e, *de_t, *up_e = NULL;
1756 struct indx_node *n2 = NULL;
1757 struct indx_node *n1 = fnd->nodes[level];
1758 struct INDEX_HDR *hdr1 = &n1->index->ihdr;
1759 struct INDEX_HDR *hdr2;
1760 u32 to_copy, used;
1761 CLST new_vbn;
1762 __le64 t_vbn, *sub_vbn;
1763 u16 sp_size;
1764
Kari Argillandere8b8e972021-08-03 14:57:09 +03001765 /* Try the most easy case. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001766 e = fnd->level - 1 == level ? fnd->de[level] : NULL;
1767 e = hdr_insert_de(indx, hdr1, new_de, e, ctx);
1768 fnd->de[level] = e;
1769 if (e) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001770 /* Just write updated index into disk. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001771 indx_write(indx, ni, n1, 0);
1772 return 0;
1773 }
1774
1775 /*
1776 * No space to insert into buffer. Split it.
1777 * To split we:
1778 * - Save split point ('cause index buffers will be changed)
1779 * - Allocate NewBuffer and copy all entries <= sp into new buffer
1780 * - Remove all entries (sp including) from TargetBuffer
1781 * - Insert NewEntry into left or right buffer (depending on sp <=>
1782 * NewEntry)
1783 * - Insert sp into parent buffer (or root)
1784 * - Make sp a parent for new buffer
1785 */
1786 sp = hdr_find_split(hdr1);
1787 if (!sp)
1788 return -EINVAL;
1789
1790 sp_size = le16_to_cpu(sp->size);
Kari Argillander195c52b2021-08-24 21:37:07 +03001791 up_e = kmalloc(sp_size + sizeof(u64), GFP_NOFS);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001792 if (!up_e)
1793 return -ENOMEM;
1794 memcpy(up_e, sp, sp_size);
1795
1796 if (!hdr1->flags) {
1797 up_e->flags |= NTFS_IE_HAS_SUBNODES;
1798 up_e->size = cpu_to_le16(sp_size + sizeof(u64));
1799 sub_vbn = NULL;
1800 } else {
1801 t_vbn = de_get_vbn_le(up_e);
1802 sub_vbn = &t_vbn;
1803 }
1804
1805 /* Allocate on disk a new index allocation buffer. */
1806 err = indx_add_allocate(indx, ni, &new_vbn);
1807 if (err)
1808 goto out;
1809
Kari Argillandere8b8e972021-08-03 14:57:09 +03001810 /* Allocate and format memory a new index buffer. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001811 n2 = indx_new(indx, ni, new_vbn, sub_vbn);
1812 if (IS_ERR(n2)) {
1813 err = PTR_ERR(n2);
1814 goto out;
1815 }
1816
1817 hdr2 = &n2->index->ihdr;
1818
Kari Argillandere8b8e972021-08-03 14:57:09 +03001819 /* Make sp a parent for new buffer. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001820 de_set_vbn(up_e, new_vbn);
1821
Kari Argillandere8b8e972021-08-03 14:57:09 +03001822 /* Copy all the entries <= sp into the new buffer. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001823 de_t = hdr_first_de(hdr1);
1824 to_copy = PtrOffset(de_t, sp);
1825 hdr_insert_head(hdr2, de_t, to_copy);
1826
Kari Argillandere8b8e972021-08-03 14:57:09 +03001827 /* Remove all entries (sp including) from hdr1. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001828 used = le32_to_cpu(hdr1->used) - to_copy - sp_size;
1829 memmove(de_t, Add2Ptr(sp, sp_size), used - le32_to_cpu(hdr1->de_off));
1830 hdr1->used = cpu_to_le32(used);
1831
Kari Argillandere8b8e972021-08-03 14:57:09 +03001832 /*
1833 * Insert new entry into left or right buffer
1834 * (depending on sp <=> new_de).
1835 */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001836 hdr_insert_de(indx,
1837 (*indx->cmp)(new_de + 1, le16_to_cpu(new_de->key_size),
1838 up_e + 1, le16_to_cpu(up_e->key_size),
1839 ctx) < 0
1840 ? hdr2
1841 : hdr1,
1842 new_de, NULL, ctx);
1843
1844 indx_mark_used(indx, ni, new_vbn >> indx->idx2vbn_bits);
1845
1846 indx_write(indx, ni, n1, 0);
1847 indx_write(indx, ni, n2, 0);
1848
1849 put_indx_node(n2);
1850
1851 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +03001852 * We've finished splitting everybody, so we are ready to
Konstantin Komarov82cae262021-08-13 17:21:29 +03001853 * insert the promoted entry into the parent.
1854 */
1855 if (!level) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001856 /* Insert in root. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001857 err = indx_insert_into_root(indx, ni, up_e, NULL, ctx, fnd);
1858 if (err)
1859 goto out;
1860 } else {
1861 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +03001862 * The target buffer's parent is another index buffer.
1863 * TODO: Remove recursion.
Konstantin Komarov82cae262021-08-13 17:21:29 +03001864 */
1865 err = indx_insert_into_buffer(indx, ni, root, up_e, ctx,
1866 level - 1, fnd);
1867 if (err)
1868 goto out;
1869 }
1870
1871out:
Kari Argillander195c52b2021-08-24 21:37:07 +03001872 kfree(up_e);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001873
1874 return err;
1875}
1876
1877/*
Kari Argillandere8b8e972021-08-03 14:57:09 +03001878 * indx_insert_entry - Insert new entry into index.
Konstantin Komarov82cae262021-08-13 17:21:29 +03001879 */
1880int indx_insert_entry(struct ntfs_index *indx, struct ntfs_inode *ni,
1881 const struct NTFS_DE *new_de, const void *ctx,
1882 struct ntfs_fnd *fnd)
1883{
1884 int err;
1885 int diff;
1886 struct NTFS_DE *e;
1887 struct ntfs_fnd *fnd_a = NULL;
1888 struct INDEX_ROOT *root;
1889
1890 if (!fnd) {
1891 fnd_a = fnd_get();
1892 if (!fnd_a) {
1893 err = -ENOMEM;
1894 goto out1;
1895 }
1896 fnd = fnd_a;
1897 }
1898
1899 root = indx_get_root(indx, ni, NULL, NULL);
1900 if (!root) {
1901 err = -EINVAL;
1902 goto out;
1903 }
1904
1905 if (fnd_is_empty(fnd)) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001906 /*
1907 * Find the spot the tree where we want to
1908 * insert the new entry.
1909 */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001910 err = indx_find(indx, ni, root, new_de + 1,
1911 le16_to_cpu(new_de->key_size), ctx, &diff, &e,
1912 fnd);
1913 if (err)
1914 goto out;
1915
1916 if (!diff) {
1917 err = -EEXIST;
1918 goto out;
1919 }
1920 }
1921
1922 if (!fnd->level) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001923 /*
1924 * The root is also a leaf, so we'll insert the
1925 * new entry into it.
1926 */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001927 err = indx_insert_into_root(indx, ni, new_de, fnd->root_de, ctx,
1928 fnd);
1929 if (err)
1930 goto out;
1931 } else {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001932 /*
1933 * Found a leaf buffer, so we'll insert the new entry into it.
1934 */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001935 err = indx_insert_into_buffer(indx, ni, root, new_de, ctx,
1936 fnd->level - 1, fnd);
1937 if (err)
1938 goto out;
1939 }
1940
1941out:
1942 fnd_put(fnd_a);
1943out1:
1944 return err;
1945}
1946
1947/*
Kari Argillandere8b8e972021-08-03 14:57:09 +03001948 * indx_find_buffer - Locate a buffer from the tree.
Konstantin Komarov82cae262021-08-13 17:21:29 +03001949 */
1950static struct indx_node *indx_find_buffer(struct ntfs_index *indx,
1951 struct ntfs_inode *ni,
1952 const struct INDEX_ROOT *root,
1953 __le64 vbn, struct indx_node *n)
1954{
1955 int err;
1956 const struct NTFS_DE *e;
1957 struct indx_node *r;
1958 const struct INDEX_HDR *hdr = n ? &n->index->ihdr : &root->ihdr;
1959
Kari Argillandere8b8e972021-08-03 14:57:09 +03001960 /* Step 1: Scan one level. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001961 for (e = hdr_first_de(hdr);; e = hdr_next_de(hdr, e)) {
1962 if (!e)
1963 return ERR_PTR(-EINVAL);
1964
1965 if (de_has_vcn(e) && vbn == de_get_vbn_le(e))
1966 return n;
1967
1968 if (de_is_last(e))
1969 break;
1970 }
1971
Kari Argillandere8b8e972021-08-03 14:57:09 +03001972 /* Step2: Do recursion. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001973 e = Add2Ptr(hdr, le32_to_cpu(hdr->de_off));
1974 for (;;) {
1975 if (de_has_vcn_ex(e)) {
1976 err = indx_read(indx, ni, de_get_vbn(e), &n);
1977 if (err)
1978 return ERR_PTR(err);
1979
1980 r = indx_find_buffer(indx, ni, root, vbn, n);
1981 if (r)
1982 return r;
1983 }
1984
1985 if (de_is_last(e))
1986 break;
1987
1988 e = Add2Ptr(e, le16_to_cpu(e->size));
1989 }
1990
1991 return NULL;
1992}
1993
1994/*
Kari Argillandere8b8e972021-08-03 14:57:09 +03001995 * indx_shrink - Deallocate unused tail indexes.
Konstantin Komarov82cae262021-08-13 17:21:29 +03001996 */
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;
Kari Argillandere8b8e972021-08-03 14:57:09 +03002072 /* First, recurse into the children, if any. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03002073 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;
Kari Argillandere8b8e972021-08-03 14:57:09 +03002084 /*
2085 * We've gotten rid of the children; add this buffer to the free list.
2086 */
Konstantin Komarov82cae262021-08-13 17:21:29 +03002087 indx_mark_free(indx, ni, i);
2088
2089 if (!trim)
2090 return 0;
2091
2092 /*
2093 * If there are no used indexes after current free index
Kari Argillandere8b8e972021-08-03 14:57:09 +03002094 * then we can truncate allocation and bitmap.
2095 * Use bitmap to estimate the case.
Konstantin Komarov82cae262021-08-13 17:21:29 +03002096 */
2097 indx_shrink(indx, ni, i + 1);
2098 return 0;
2099}
2100
2101/*
2102 * indx_get_entry_to_replace
2103 *
Kari Argillandere8b8e972021-08-03 14:57:09 +03002104 * Find a replacement entry for a deleted entry.
2105 * Always returns a node entry:
2106 * NTFS_IE_HAS_SUBNODES is set the flags and the size includes the sub_vcn.
Konstantin Komarov82cae262021-08-13 17:21:29 +03002107 */
2108static int indx_get_entry_to_replace(struct ntfs_index *indx,
2109 struct ntfs_inode *ni,
2110 const struct NTFS_DE *de_next,
2111 struct NTFS_DE **de_to_replace,
2112 struct ntfs_fnd *fnd)
2113{
2114 int err;
2115 int level = -1;
2116 CLST vbn;
2117 struct NTFS_DE *e, *te, *re;
2118 struct indx_node *n;
2119 struct INDEX_BUFFER *ib;
2120
2121 *de_to_replace = NULL;
2122
Kari Argillandere8b8e972021-08-03 14:57:09 +03002123 /* Find first leaf entry down from de_next. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03002124 vbn = de_get_vbn(de_next);
2125 for (;;) {
2126 n = NULL;
2127 err = indx_read(indx, ni, vbn, &n);
2128 if (err)
2129 goto out;
2130
2131 e = hdr_first_de(&n->index->ihdr);
2132 fnd_push(fnd, n, e);
2133
2134 if (!de_is_last(e)) {
2135 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +03002136 * This buffer is non-empty, so its first entry
2137 * could be used as the replacement entry.
Konstantin Komarov82cae262021-08-13 17:21:29 +03002138 */
2139 level = fnd->level - 1;
2140 }
2141
2142 if (!de_has_vcn(e))
2143 break;
2144
Kari Argillandere8b8e972021-08-03 14:57:09 +03002145 /* This buffer is a node. Continue to go down. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03002146 vbn = de_get_vbn(e);
2147 }
2148
2149 if (level == -1)
2150 goto out;
2151
2152 n = fnd->nodes[level];
2153 te = hdr_first_de(&n->index->ihdr);
2154 /* Copy the candidate entry into the replacement entry buffer. */
Kari Argillander195c52b2021-08-24 21:37:07 +03002155 re = kmalloc(le16_to_cpu(te->size) + sizeof(u64), GFP_NOFS);
Konstantin Komarov82cae262021-08-13 17:21:29 +03002156 if (!re) {
2157 err = -ENOMEM;
2158 goto out;
2159 }
2160
2161 *de_to_replace = re;
2162 memcpy(re, te, le16_to_cpu(te->size));
2163
2164 if (!de_has_vcn(re)) {
2165 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +03002166 * The replacement entry we found doesn't have a sub_vcn.
2167 * increase its size to hold one.
Konstantin Komarov82cae262021-08-13 17:21:29 +03002168 */
2169 le16_add_cpu(&re->size, sizeof(u64));
2170 re->flags |= NTFS_IE_HAS_SUBNODES;
2171 } else {
2172 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +03002173 * The replacement entry we found was a node entry, which
2174 * means that all its child buffers are empty. Return them
2175 * to the free pool.
Konstantin Komarov82cae262021-08-13 17:21:29 +03002176 */
2177 indx_free_children(indx, ni, te, true);
2178 }
2179
2180 /*
2181 * Expunge the replacement entry from its former location,
2182 * and then write that buffer.
2183 */
2184 ib = n->index;
2185 e = hdr_delete_de(&ib->ihdr, te);
2186
2187 fnd->de[level] = e;
2188 indx_write(indx, ni, n, 0);
2189
2190 /* Check to see if this action created an empty leaf. */
2191 if (ib_is_leaf(ib) && ib_is_empty(ib))
2192 return 0;
2193
2194out:
2195 fnd_clear(fnd);
2196 return err;
2197}
2198
2199/*
Kari Argillandere8b8e972021-08-03 14:57:09 +03002200 * indx_delete_entry - Delete an entry from the index.
Konstantin Komarov82cae262021-08-13 17:21:29 +03002201 */
2202int indx_delete_entry(struct ntfs_index *indx, struct ntfs_inode *ni,
2203 const void *key, u32 key_len, const void *ctx)
2204{
2205 int err, diff;
2206 struct INDEX_ROOT *root;
2207 struct INDEX_HDR *hdr;
2208 struct ntfs_fnd *fnd, *fnd2;
2209 struct INDEX_BUFFER *ib;
2210 struct NTFS_DE *e, *re, *next, *prev, *me;
2211 struct indx_node *n, *n2d = NULL;
2212 __le64 sub_vbn;
2213 int level, level2;
2214 struct ATTRIB *attr;
2215 struct mft_inode *mi;
2216 u32 e_size, root_size, new_root_size;
2217 size_t trim_bit;
2218 const struct INDEX_NAMES *in;
2219
2220 fnd = fnd_get();
2221 if (!fnd) {
2222 err = -ENOMEM;
2223 goto out2;
2224 }
2225
2226 fnd2 = fnd_get();
2227 if (!fnd2) {
2228 err = -ENOMEM;
2229 goto out1;
2230 }
2231
2232 root = indx_get_root(indx, ni, &attr, &mi);
2233 if (!root) {
2234 err = -EINVAL;
2235 goto out;
2236 }
2237
2238 /* Locate the entry to remove. */
2239 err = indx_find(indx, ni, root, key, key_len, ctx, &diff, &e, fnd);
2240 if (err)
2241 goto out;
2242
2243 if (!e || diff) {
2244 err = -ENOENT;
2245 goto out;
2246 }
2247
2248 level = fnd->level;
2249
2250 if (level) {
2251 n = fnd->nodes[level - 1];
2252 e = fnd->de[level - 1];
2253 ib = n->index;
2254 hdr = &ib->ihdr;
2255 } else {
2256 hdr = &root->ihdr;
2257 e = fnd->root_de;
2258 n = NULL;
2259 }
2260
2261 e_size = le16_to_cpu(e->size);
2262
2263 if (!de_has_vcn_ex(e)) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03002264 /* The entry to delete is a leaf, so we can just rip it out. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03002265 hdr_delete_de(hdr, e);
2266
2267 if (!level) {
2268 hdr->total = hdr->used;
2269
Kari Argillandere8b8e972021-08-03 14:57:09 +03002270 /* Shrink resident root attribute. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03002271 mi_resize_attr(mi, attr, 0 - e_size);
2272 goto out;
2273 }
2274
2275 indx_write(indx, ni, n, 0);
2276
2277 /*
2278 * Check to see if removing that entry made
2279 * the leaf empty.
2280 */
2281 if (ib_is_leaf(ib) && ib_is_empty(ib)) {
2282 fnd_pop(fnd);
2283 fnd_push(fnd2, n, e);
2284 }
2285 } else {
2286 /*
2287 * The entry we wish to delete is a node buffer, so we
2288 * have to find a replacement for it.
2289 */
2290 next = de_get_next(e);
2291
2292 err = indx_get_entry_to_replace(indx, ni, next, &re, fnd2);
2293 if (err)
2294 goto out;
2295
2296 if (re) {
2297 de_set_vbn_le(re, de_get_vbn_le(e));
2298 hdr_delete_de(hdr, e);
2299
2300 err = level ? indx_insert_into_buffer(indx, ni, root,
2301 re, ctx,
2302 fnd->level - 1,
2303 fnd)
2304 : indx_insert_into_root(indx, ni, re, e,
2305 ctx, fnd);
Kari Argillander195c52b2021-08-24 21:37:07 +03002306 kfree(re);
Konstantin Komarov82cae262021-08-13 17:21:29 +03002307
2308 if (err)
2309 goto out;
2310 } else {
2311 /*
2312 * There is no replacement for the current entry.
Kari Argillandere8b8e972021-08-03 14:57:09 +03002313 * This means that the subtree rooted at its node
2314 * is empty, and can be deleted, which turn means
2315 * that the node can just inherit the deleted
2316 * entry sub_vcn.
Konstantin Komarov82cae262021-08-13 17:21:29 +03002317 */
2318 indx_free_children(indx, ni, next, true);
2319
2320 de_set_vbn_le(next, de_get_vbn_le(e));
2321 hdr_delete_de(hdr, e);
2322 if (level) {
2323 indx_write(indx, ni, n, 0);
2324 } else {
2325 hdr->total = hdr->used;
2326
Kari Argillandere8b8e972021-08-03 14:57:09 +03002327 /* Shrink resident root attribute. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03002328 mi_resize_attr(mi, attr, 0 - e_size);
2329 }
2330 }
2331 }
2332
Kari Argillandere8b8e972021-08-03 14:57:09 +03002333 /* Delete a branch of tree. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03002334 if (!fnd2 || !fnd2->level)
2335 goto out;
2336
Kari Argillandere8b8e972021-08-03 14:57:09 +03002337 /* Reinit root 'cause it can be changed. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03002338 root = indx_get_root(indx, ni, &attr, &mi);
2339 if (!root) {
2340 err = -EINVAL;
2341 goto out;
2342 }
2343
2344 n2d = NULL;
2345 sub_vbn = fnd2->nodes[0]->index->vbn;
2346 level2 = 0;
2347 level = fnd->level;
2348
2349 hdr = level ? &fnd->nodes[level - 1]->index->ihdr : &root->ihdr;
2350
Kari Argillandere8b8e972021-08-03 14:57:09 +03002351 /* Scan current level. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03002352 for (e = hdr_first_de(hdr);; e = hdr_next_de(hdr, e)) {
2353 if (!e) {
2354 err = -EINVAL;
2355 goto out;
2356 }
2357
2358 if (de_has_vcn(e) && sub_vbn == de_get_vbn_le(e))
2359 break;
2360
2361 if (de_is_last(e)) {
2362 e = NULL;
2363 break;
2364 }
2365 }
2366
2367 if (!e) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03002368 /* Do slow search from root. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03002369 struct indx_node *in;
2370
2371 fnd_clear(fnd);
2372
2373 in = indx_find_buffer(indx, ni, root, sub_vbn, NULL);
2374 if (IS_ERR(in)) {
2375 err = PTR_ERR(in);
2376 goto out;
2377 }
2378
2379 if (in)
2380 fnd_push(fnd, in, NULL);
2381 }
2382
Kari Argillandere8b8e972021-08-03 14:57:09 +03002383 /* Merge fnd2 -> fnd. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03002384 for (level = 0; level < fnd2->level; level++) {
2385 fnd_push(fnd, fnd2->nodes[level], fnd2->de[level]);
2386 fnd2->nodes[level] = NULL;
2387 }
2388 fnd2->level = 0;
2389
2390 hdr = NULL;
2391 for (level = fnd->level; level; level--) {
2392 struct indx_node *in = fnd->nodes[level - 1];
2393
2394 ib = in->index;
2395 if (ib_is_empty(ib)) {
2396 sub_vbn = ib->vbn;
2397 } else {
2398 hdr = &ib->ihdr;
2399 n2d = in;
2400 level2 = level;
2401 break;
2402 }
2403 }
2404
2405 if (!hdr)
2406 hdr = &root->ihdr;
2407
2408 e = hdr_first_de(hdr);
2409 if (!e) {
2410 err = -EINVAL;
2411 goto out;
2412 }
2413
2414 if (hdr != &root->ihdr || !de_is_last(e)) {
2415 prev = NULL;
2416 while (!de_is_last(e)) {
2417 if (de_has_vcn(e) && sub_vbn == de_get_vbn_le(e))
2418 break;
2419 prev = e;
2420 e = hdr_next_de(hdr, e);
2421 if (!e) {
2422 err = -EINVAL;
2423 goto out;
2424 }
2425 }
2426
2427 if (sub_vbn != de_get_vbn_le(e)) {
2428 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +03002429 * Didn't find the parent entry, although this buffer
2430 * is the parent trail. Something is corrupt.
Konstantin Komarov82cae262021-08-13 17:21:29 +03002431 */
2432 err = -EINVAL;
2433 goto out;
2434 }
2435
2436 if (de_is_last(e)) {
2437 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +03002438 * Since we can't remove the end entry, we'll remove
2439 * its predecessor instead. This means we have to
2440 * transfer the predecessor's sub_vcn to the end entry.
2441 * Note: This index block is not empty, so the
2442 * predecessor must exist.
Konstantin Komarov82cae262021-08-13 17:21:29 +03002443 */
2444 if (!prev) {
2445 err = -EINVAL;
2446 goto out;
2447 }
2448
2449 if (de_has_vcn(prev)) {
2450 de_set_vbn_le(e, de_get_vbn_le(prev));
2451 } else if (de_has_vcn(e)) {
2452 le16_sub_cpu(&e->size, sizeof(u64));
2453 e->flags &= ~NTFS_IE_HAS_SUBNODES;
2454 le32_sub_cpu(&hdr->used, sizeof(u64));
2455 }
2456 e = prev;
2457 }
2458
2459 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +03002460 * Copy the current entry into a temporary buffer (stripping
2461 * off its down-pointer, if any) and delete it from the current
2462 * buffer or root, as appropriate.
Konstantin Komarov82cae262021-08-13 17:21:29 +03002463 */
2464 e_size = le16_to_cpu(e->size);
Kari Argillander195c52b2021-08-24 21:37:07 +03002465 me = kmemdup(e, e_size, GFP_NOFS);
Konstantin Komarov82cae262021-08-13 17:21:29 +03002466 if (!me) {
2467 err = -ENOMEM;
2468 goto out;
2469 }
2470
2471 if (de_has_vcn(me)) {
2472 me->flags &= ~NTFS_IE_HAS_SUBNODES;
2473 le16_sub_cpu(&me->size, sizeof(u64));
2474 }
2475
2476 hdr_delete_de(hdr, e);
2477
2478 if (hdr == &root->ihdr) {
2479 level = 0;
2480 hdr->total = hdr->used;
2481
Kari Argillandere8b8e972021-08-03 14:57:09 +03002482 /* Shrink resident root attribute. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03002483 mi_resize_attr(mi, attr, 0 - e_size);
2484 } else {
2485 indx_write(indx, ni, n2d, 0);
2486 level = level2;
2487 }
2488
Kari Argillandere8b8e972021-08-03 14:57:09 +03002489 /* Mark unused buffers as free. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03002490 trim_bit = -1;
2491 for (; level < fnd->level; level++) {
2492 ib = fnd->nodes[level]->index;
2493 if (ib_is_empty(ib)) {
2494 size_t k = le64_to_cpu(ib->vbn) >>
2495 indx->idx2vbn_bits;
2496
2497 indx_mark_free(indx, ni, k);
2498 if (k < trim_bit)
2499 trim_bit = k;
2500 }
2501 }
2502
2503 fnd_clear(fnd);
2504 /*fnd->root_de = NULL;*/
2505
2506 /*
2507 * Re-insert the entry into the tree.
2508 * Find the spot the tree where we want to insert the new entry.
2509 */
2510 err = indx_insert_entry(indx, ni, me, ctx, fnd);
Kari Argillander195c52b2021-08-24 21:37:07 +03002511 kfree(me);
Konstantin Komarov82cae262021-08-13 17:21:29 +03002512 if (err)
2513 goto out;
2514
2515 if (trim_bit != -1)
2516 indx_shrink(indx, ni, trim_bit);
2517 } else {
2518 /*
2519 * This tree needs to be collapsed down to an empty root.
Kari Argillandere8b8e972021-08-03 14:57:09 +03002520 * Recreate the index root as an empty leaf and free all
2521 * the bits the index allocation bitmap.
Konstantin Komarov82cae262021-08-13 17:21:29 +03002522 */
2523 fnd_clear(fnd);
2524 fnd_clear(fnd2);
2525
2526 in = &s_index_names[indx->type];
2527
2528 err = attr_set_size(ni, ATTR_ALLOC, in->name, in->name_len,
2529 &indx->alloc_run, 0, NULL, false, NULL);
2530 err = ni_remove_attr(ni, ATTR_ALLOC, in->name, in->name_len,
2531 false, NULL);
2532 run_close(&indx->alloc_run);
2533
2534 err = attr_set_size(ni, ATTR_BITMAP, in->name, in->name_len,
2535 &indx->bitmap_run, 0, NULL, false, NULL);
2536 err = ni_remove_attr(ni, ATTR_BITMAP, in->name, in->name_len,
2537 false, NULL);
2538 run_close(&indx->bitmap_run);
2539
2540 root = indx_get_root(indx, ni, &attr, &mi);
2541 if (!root) {
2542 err = -EINVAL;
2543 goto out;
2544 }
2545
2546 root_size = le32_to_cpu(attr->res.data_size);
2547 new_root_size =
2548 sizeof(struct INDEX_ROOT) + sizeof(struct NTFS_DE);
2549
2550 if (new_root_size != root_size &&
2551 !mi_resize_attr(mi, attr, new_root_size - root_size)) {
2552 err = -EINVAL;
2553 goto out;
2554 }
2555
Kari Argillandere8b8e972021-08-03 14:57:09 +03002556 /* Fill first entry. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03002557 e = (struct NTFS_DE *)(root + 1);
2558 e->ref.low = 0;
2559 e->ref.high = 0;
2560 e->ref.seq = 0;
2561 e->size = cpu_to_le16(sizeof(struct NTFS_DE));
2562 e->flags = NTFS_IE_LAST; // 0x02
2563 e->key_size = 0;
2564 e->res = 0;
2565
2566 hdr = &root->ihdr;
2567 hdr->flags = 0;
2568 hdr->used = hdr->total = cpu_to_le32(
2569 new_root_size - offsetof(struct INDEX_ROOT, ihdr));
2570 mi->dirty = true;
2571 }
2572
2573out:
2574 fnd_put(fnd2);
2575out1:
2576 fnd_put(fnd);
2577out2:
2578 return err;
2579}
2580
2581/*
2582 * Update duplicated information in directory entry
2583 * 'dup' - info from MFT record
2584 */
2585int indx_update_dup(struct ntfs_inode *ni, struct ntfs_sb_info *sbi,
2586 const struct ATTR_FILE_NAME *fname,
2587 const struct NTFS_DUP_INFO *dup, int sync)
2588{
2589 int err, diff;
2590 struct NTFS_DE *e = NULL;
2591 struct ATTR_FILE_NAME *e_fname;
2592 struct ntfs_fnd *fnd;
2593 struct INDEX_ROOT *root;
2594 struct mft_inode *mi;
2595 struct ntfs_index *indx = &ni->dir;
2596
2597 fnd = fnd_get();
2598 if (!fnd) {
2599 err = -ENOMEM;
2600 goto out1;
2601 }
2602
2603 root = indx_get_root(indx, ni, NULL, &mi);
2604 if (!root) {
2605 err = -EINVAL;
2606 goto out;
2607 }
2608
Kari Argillandere8b8e972021-08-03 14:57:09 +03002609 /* Find entry in directory. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03002610 err = indx_find(indx, ni, root, fname, fname_full_size(fname), sbi,
2611 &diff, &e, fnd);
2612 if (err)
2613 goto out;
2614
2615 if (!e) {
2616 err = -EINVAL;
2617 goto out;
2618 }
2619
2620 if (diff) {
2621 err = -EINVAL;
2622 goto out;
2623 }
2624
2625 e_fname = (struct ATTR_FILE_NAME *)(e + 1);
2626
2627 if (!memcmp(&e_fname->dup, dup, sizeof(*dup))) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03002628 /* Nothing to update in index! Try to avoid this call. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03002629 goto out;
2630 }
2631
2632 memcpy(&e_fname->dup, dup, sizeof(*dup));
2633
2634 if (fnd->level) {
2635 /* directory entry in index */
2636 err = indx_write(indx, ni, fnd->nodes[fnd->level - 1], sync);
2637 } else {
2638 /* directory entry in directory MFT record */
2639 mi->dirty = true;
2640 if (sync)
2641 err = mi_write(mi, 1);
2642 else
2643 mark_inode_dirty(&ni->vfs_inode);
2644 }
2645
2646out:
2647 fnd_put(fnd);
2648
2649out1:
2650 return err;
2651}