Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *
0004  * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
0005  *
0006  */
0007 
0008 #include <linux/blkdev.h>
0009 #include <linux/buffer_head.h>
0010 #include <linux/fs.h>
0011 #include <linux/kernel.h>
0012 
0013 #include "debug.h"
0014 #include "ntfs.h"
0015 #include "ntfs_fs.h"
0016 
0017 static const struct INDEX_NAMES {
0018     const __le16 *name;
0019     u8 name_len;
0020 } s_index_names[INDEX_MUTEX_TOTAL] = {
0021     { I30_NAME, ARRAY_SIZE(I30_NAME) }, { SII_NAME, ARRAY_SIZE(SII_NAME) },
0022     { SDH_NAME, ARRAY_SIZE(SDH_NAME) }, { SO_NAME, ARRAY_SIZE(SO_NAME) },
0023     { SQ_NAME, ARRAY_SIZE(SQ_NAME) },   { SR_NAME, ARRAY_SIZE(SR_NAME) },
0024 };
0025 
0026 /*
0027  * cmp_fnames - Compare two names in index.
0028  *
0029  * if l1 != 0
0030  *   Both names are little endian on-disk ATTR_FILE_NAME structs.
0031  * else
0032  *   key1 - cpu_str, key2 - ATTR_FILE_NAME
0033  */
0034 static int cmp_fnames(const void *key1, size_t l1, const void *key2, size_t l2,
0035               const void *data)
0036 {
0037     const struct ATTR_FILE_NAME *f2 = key2;
0038     const struct ntfs_sb_info *sbi = data;
0039     const struct ATTR_FILE_NAME *f1;
0040     u16 fsize2;
0041     bool both_case;
0042 
0043     if (l2 <= offsetof(struct ATTR_FILE_NAME, name))
0044         return -1;
0045 
0046     fsize2 = fname_full_size(f2);
0047     if (l2 < fsize2)
0048         return -1;
0049 
0050     both_case = f2->type != FILE_NAME_DOS /*&& !sbi->options.nocase*/;
0051     if (!l1) {
0052         const struct le_str *s2 = (struct le_str *)&f2->name_len;
0053 
0054         /*
0055          * If names are equal (case insensitive)
0056          * try to compare it case sensitive.
0057          */
0058         return ntfs_cmp_names_cpu(key1, s2, sbi->upcase, both_case);
0059     }
0060 
0061     f1 = key1;
0062     return ntfs_cmp_names(f1->name, f1->name_len, f2->name, f2->name_len,
0063                   sbi->upcase, both_case);
0064 }
0065 
0066 /*
0067  * cmp_uint - $SII of $Secure and $Q of Quota
0068  */
0069 static int cmp_uint(const void *key1, size_t l1, const void *key2, size_t l2,
0070             const void *data)
0071 {
0072     const u32 *k1 = key1;
0073     const u32 *k2 = key2;
0074 
0075     if (l2 < sizeof(u32))
0076         return -1;
0077 
0078     if (*k1 < *k2)
0079         return -1;
0080     if (*k1 > *k2)
0081         return 1;
0082     return 0;
0083 }
0084 
0085 /*
0086  * cmp_sdh - $SDH of $Secure
0087  */
0088 static int cmp_sdh(const void *key1, size_t l1, const void *key2, size_t l2,
0089            const void *data)
0090 {
0091     const struct SECURITY_KEY *k1 = key1;
0092     const struct SECURITY_KEY *k2 = key2;
0093     u32 t1, t2;
0094 
0095     if (l2 < sizeof(struct SECURITY_KEY))
0096         return -1;
0097 
0098     t1 = le32_to_cpu(k1->hash);
0099     t2 = le32_to_cpu(k2->hash);
0100 
0101     /* First value is a hash value itself. */
0102     if (t1 < t2)
0103         return -1;
0104     if (t1 > t2)
0105         return 1;
0106 
0107     /* Second value is security Id. */
0108     if (data) {
0109         t1 = le32_to_cpu(k1->sec_id);
0110         t2 = le32_to_cpu(k2->sec_id);
0111         if (t1 < t2)
0112             return -1;
0113         if (t1 > t2)
0114             return 1;
0115     }
0116 
0117     return 0;
0118 }
0119 
0120 /*
0121  * cmp_uints - $O of ObjId and "$R" for Reparse.
0122  */
0123 static int cmp_uints(const void *key1, size_t l1, const void *key2, size_t l2,
0124              const void *data)
0125 {
0126     const __le32 *k1 = key1;
0127     const __le32 *k2 = key2;
0128     size_t count;
0129 
0130     if ((size_t)data == 1) {
0131         /*
0132          * ni_delete_all -> ntfs_remove_reparse ->
0133          * delete all with this reference.
0134          * k1, k2 - pointers to REPARSE_KEY
0135          */
0136 
0137         k1 += 1; // Skip REPARSE_KEY.ReparseTag
0138         k2 += 1; // Skip REPARSE_KEY.ReparseTag
0139         if (l2 <= sizeof(int))
0140             return -1;
0141         l2 -= sizeof(int);
0142         if (l1 <= sizeof(int))
0143             return 1;
0144         l1 -= sizeof(int);
0145     }
0146 
0147     if (l2 < sizeof(int))
0148         return -1;
0149 
0150     for (count = min(l1, l2) >> 2; count > 0; --count, ++k1, ++k2) {
0151         u32 t1 = le32_to_cpu(*k1);
0152         u32 t2 = le32_to_cpu(*k2);
0153 
0154         if (t1 > t2)
0155             return 1;
0156         if (t1 < t2)
0157             return -1;
0158     }
0159 
0160     if (l1 > l2)
0161         return 1;
0162     if (l1 < l2)
0163         return -1;
0164 
0165     return 0;
0166 }
0167 
0168 static inline NTFS_CMP_FUNC get_cmp_func(const struct INDEX_ROOT *root)
0169 {
0170     switch (root->type) {
0171     case ATTR_NAME:
0172         if (root->rule == NTFS_COLLATION_TYPE_FILENAME)
0173             return &cmp_fnames;
0174         break;
0175     case ATTR_ZERO:
0176         switch (root->rule) {
0177         case NTFS_COLLATION_TYPE_UINT:
0178             return &cmp_uint;
0179         case NTFS_COLLATION_TYPE_SECURITY_HASH:
0180             return &cmp_sdh;
0181         case NTFS_COLLATION_TYPE_UINTS:
0182             return &cmp_uints;
0183         default:
0184             break;
0185         }
0186         break;
0187     default:
0188         break;
0189     }
0190 
0191     return NULL;
0192 }
0193 
0194 struct bmp_buf {
0195     struct ATTRIB *b;
0196     struct mft_inode *mi;
0197     struct buffer_head *bh;
0198     ulong *buf;
0199     size_t bit;
0200     u32 nbits;
0201     u64 new_valid;
0202 };
0203 
0204 static int bmp_buf_get(struct ntfs_index *indx, struct ntfs_inode *ni,
0205                size_t bit, struct bmp_buf *bbuf)
0206 {
0207     struct ATTRIB *b;
0208     size_t data_size, valid_size, vbo, off = bit >> 3;
0209     struct ntfs_sb_info *sbi = ni->mi.sbi;
0210     CLST vcn = off >> sbi->cluster_bits;
0211     struct ATTR_LIST_ENTRY *le = NULL;
0212     struct buffer_head *bh;
0213     struct super_block *sb;
0214     u32 blocksize;
0215     const struct INDEX_NAMES *in = &s_index_names[indx->type];
0216 
0217     bbuf->bh = NULL;
0218 
0219     b = ni_find_attr(ni, NULL, &le, ATTR_BITMAP, in->name, in->name_len,
0220              &vcn, &bbuf->mi);
0221     bbuf->b = b;
0222     if (!b)
0223         return -EINVAL;
0224 
0225     if (!b->non_res) {
0226         data_size = le32_to_cpu(b->res.data_size);
0227 
0228         if (off >= data_size)
0229             return -EINVAL;
0230 
0231         bbuf->buf = (ulong *)resident_data(b);
0232         bbuf->bit = 0;
0233         bbuf->nbits = data_size * 8;
0234 
0235         return 0;
0236     }
0237 
0238     data_size = le64_to_cpu(b->nres.data_size);
0239     if (WARN_ON(off >= data_size)) {
0240         /* Looks like filesystem error. */
0241         return -EINVAL;
0242     }
0243 
0244     valid_size = le64_to_cpu(b->nres.valid_size);
0245 
0246     bh = ntfs_bread_run(sbi, &indx->bitmap_run, off);
0247     if (!bh)
0248         return -EIO;
0249 
0250     if (IS_ERR(bh))
0251         return PTR_ERR(bh);
0252 
0253     bbuf->bh = bh;
0254 
0255     if (buffer_locked(bh))
0256         __wait_on_buffer(bh);
0257 
0258     lock_buffer(bh);
0259 
0260     sb = sbi->sb;
0261     blocksize = sb->s_blocksize;
0262 
0263     vbo = off & ~(size_t)sbi->block_mask;
0264 
0265     bbuf->new_valid = vbo + blocksize;
0266     if (bbuf->new_valid <= valid_size)
0267         bbuf->new_valid = 0;
0268     else if (bbuf->new_valid > data_size)
0269         bbuf->new_valid = data_size;
0270 
0271     if (vbo >= valid_size) {
0272         memset(bh->b_data, 0, blocksize);
0273     } else if (vbo + blocksize > valid_size) {
0274         u32 voff = valid_size & sbi->block_mask;
0275 
0276         memset(bh->b_data + voff, 0, blocksize - voff);
0277     }
0278 
0279     bbuf->buf = (ulong *)bh->b_data;
0280     bbuf->bit = 8 * (off & ~(size_t)sbi->block_mask);
0281     bbuf->nbits = 8 * blocksize;
0282 
0283     return 0;
0284 }
0285 
0286 static void bmp_buf_put(struct bmp_buf *bbuf, bool dirty)
0287 {
0288     struct buffer_head *bh = bbuf->bh;
0289     struct ATTRIB *b = bbuf->b;
0290 
0291     if (!bh) {
0292         if (b && !b->non_res && dirty)
0293             bbuf->mi->dirty = true;
0294         return;
0295     }
0296 
0297     if (!dirty)
0298         goto out;
0299 
0300     if (bbuf->new_valid) {
0301         b->nres.valid_size = cpu_to_le64(bbuf->new_valid);
0302         bbuf->mi->dirty = true;
0303     }
0304 
0305     set_buffer_uptodate(bh);
0306     mark_buffer_dirty(bh);
0307 
0308 out:
0309     unlock_buffer(bh);
0310     put_bh(bh);
0311 }
0312 
0313 /*
0314  * indx_mark_used - Mark the bit @bit as used.
0315  */
0316 static int indx_mark_used(struct ntfs_index *indx, struct ntfs_inode *ni,
0317               size_t bit)
0318 {
0319     int err;
0320     struct bmp_buf bbuf;
0321 
0322     err = bmp_buf_get(indx, ni, bit, &bbuf);
0323     if (err)
0324         return err;
0325 
0326     __set_bit(bit - bbuf.bit, bbuf.buf);
0327 
0328     bmp_buf_put(&bbuf, true);
0329 
0330     return 0;
0331 }
0332 
0333 /*
0334  * indx_mark_free - Mark the bit @bit as free.
0335  */
0336 static int indx_mark_free(struct ntfs_index *indx, struct ntfs_inode *ni,
0337               size_t bit)
0338 {
0339     int err;
0340     struct bmp_buf bbuf;
0341 
0342     err = bmp_buf_get(indx, ni, bit, &bbuf);
0343     if (err)
0344         return err;
0345 
0346     __clear_bit(bit - bbuf.bit, bbuf.buf);
0347 
0348     bmp_buf_put(&bbuf, true);
0349 
0350     return 0;
0351 }
0352 
0353 /*
0354  * scan_nres_bitmap
0355  *
0356  * If ntfs_readdir calls this function (indx_used_bit -> scan_nres_bitmap),
0357  * inode is shared locked and no ni_lock.
0358  * Use rw_semaphore for read/write access to bitmap_run.
0359  */
0360 static int scan_nres_bitmap(struct ntfs_inode *ni, struct ATTRIB *bitmap,
0361                 struct ntfs_index *indx, size_t from,
0362                 bool (*fn)(const ulong *buf, u32 bit, u32 bits,
0363                        size_t *ret),
0364                 size_t *ret)
0365 {
0366     struct ntfs_sb_info *sbi = ni->mi.sbi;
0367     struct super_block *sb = sbi->sb;
0368     struct runs_tree *run = &indx->bitmap_run;
0369     struct rw_semaphore *lock = &indx->run_lock;
0370     u32 nbits = sb->s_blocksize * 8;
0371     u32 blocksize = sb->s_blocksize;
0372     u64 valid_size = le64_to_cpu(bitmap->nres.valid_size);
0373     u64 data_size = le64_to_cpu(bitmap->nres.data_size);
0374     sector_t eblock = bytes_to_block(sb, data_size);
0375     size_t vbo = from >> 3;
0376     sector_t blk = (vbo & sbi->cluster_mask) >> sb->s_blocksize_bits;
0377     sector_t vblock = vbo >> sb->s_blocksize_bits;
0378     sector_t blen, block;
0379     CLST lcn, clen, vcn, vcn_next;
0380     size_t idx;
0381     struct buffer_head *bh;
0382     bool ok;
0383 
0384     *ret = MINUS_ONE_T;
0385 
0386     if (vblock >= eblock)
0387         return 0;
0388 
0389     from &= nbits - 1;
0390     vcn = vbo >> sbi->cluster_bits;
0391 
0392     down_read(lock);
0393     ok = run_lookup_entry(run, vcn, &lcn, &clen, &idx);
0394     up_read(lock);
0395 
0396 next_run:
0397     if (!ok) {
0398         int err;
0399         const struct INDEX_NAMES *name = &s_index_names[indx->type];
0400 
0401         down_write(lock);
0402         err = attr_load_runs_vcn(ni, ATTR_BITMAP, name->name,
0403                      name->name_len, run, vcn);
0404         up_write(lock);
0405         if (err)
0406             return err;
0407         down_read(lock);
0408         ok = run_lookup_entry(run, vcn, &lcn, &clen, &idx);
0409         up_read(lock);
0410         if (!ok)
0411             return -EINVAL;
0412     }
0413 
0414     blen = (sector_t)clen * sbi->blocks_per_cluster;
0415     block = (sector_t)lcn * sbi->blocks_per_cluster;
0416 
0417     for (; blk < blen; blk++, from = 0) {
0418         bh = ntfs_bread(sb, block + blk);
0419         if (!bh)
0420             return -EIO;
0421 
0422         vbo = (u64)vblock << sb->s_blocksize_bits;
0423         if (vbo >= valid_size) {
0424             memset(bh->b_data, 0, blocksize);
0425         } else if (vbo + blocksize > valid_size) {
0426             u32 voff = valid_size & sbi->block_mask;
0427 
0428             memset(bh->b_data + voff, 0, blocksize - voff);
0429         }
0430 
0431         if (vbo + blocksize > data_size)
0432             nbits = 8 * (data_size - vbo);
0433 
0434         ok = nbits > from ? (*fn)((ulong *)bh->b_data, from, nbits, ret)
0435                   : false;
0436         put_bh(bh);
0437 
0438         if (ok) {
0439             *ret += 8 * vbo;
0440             return 0;
0441         }
0442 
0443         if (++vblock >= eblock) {
0444             *ret = MINUS_ONE_T;
0445             return 0;
0446         }
0447     }
0448     blk = 0;
0449     vcn_next = vcn + clen;
0450     down_read(lock);
0451     ok = run_get_entry(run, ++idx, &vcn, &lcn, &clen) && vcn == vcn_next;
0452     if (!ok)
0453         vcn = vcn_next;
0454     up_read(lock);
0455     goto next_run;
0456 }
0457 
0458 static bool scan_for_free(const ulong *buf, u32 bit, u32 bits, size_t *ret)
0459 {
0460     size_t pos = find_next_zero_bit(buf, bits, bit);
0461 
0462     if (pos >= bits)
0463         return false;
0464     *ret = pos;
0465     return true;
0466 }
0467 
0468 /*
0469  * indx_find_free - Look for free bit.
0470  *
0471  * Return: -1 if no free bits.
0472  */
0473 static int indx_find_free(struct ntfs_index *indx, struct ntfs_inode *ni,
0474               size_t *bit, struct ATTRIB **bitmap)
0475 {
0476     struct ATTRIB *b;
0477     struct ATTR_LIST_ENTRY *le = NULL;
0478     const struct INDEX_NAMES *in = &s_index_names[indx->type];
0479     int err;
0480 
0481     b = ni_find_attr(ni, NULL, &le, ATTR_BITMAP, in->name, in->name_len,
0482              NULL, NULL);
0483 
0484     if (!b)
0485         return -ENOENT;
0486 
0487     *bitmap = b;
0488     *bit = MINUS_ONE_T;
0489 
0490     if (!b->non_res) {
0491         u32 nbits = 8 * le32_to_cpu(b->res.data_size);
0492         size_t pos = find_next_zero_bit(resident_data(b), nbits, 0);
0493 
0494         if (pos < nbits)
0495             *bit = pos;
0496     } else {
0497         err = scan_nres_bitmap(ni, b, indx, 0, &scan_for_free, bit);
0498 
0499         if (err)
0500             return err;
0501     }
0502 
0503     return 0;
0504 }
0505 
0506 static bool scan_for_used(const ulong *buf, u32 bit, u32 bits, size_t *ret)
0507 {
0508     size_t pos = find_next_bit(buf, bits, bit);
0509 
0510     if (pos >= bits)
0511         return false;
0512     *ret = pos;
0513     return true;
0514 }
0515 
0516 /*
0517  * indx_used_bit - Look for used bit.
0518  *
0519  * Return: MINUS_ONE_T if no used bits.
0520  */
0521 int indx_used_bit(struct ntfs_index *indx, struct ntfs_inode *ni, size_t *bit)
0522 {
0523     struct ATTRIB *b;
0524     struct ATTR_LIST_ENTRY *le = NULL;
0525     size_t from = *bit;
0526     const struct INDEX_NAMES *in = &s_index_names[indx->type];
0527     int err;
0528 
0529     b = ni_find_attr(ni, NULL, &le, ATTR_BITMAP, in->name, in->name_len,
0530              NULL, NULL);
0531 
0532     if (!b)
0533         return -ENOENT;
0534 
0535     *bit = MINUS_ONE_T;
0536 
0537     if (!b->non_res) {
0538         u32 nbits = le32_to_cpu(b->res.data_size) * 8;
0539         size_t pos = find_next_bit(resident_data(b), nbits, from);
0540 
0541         if (pos < nbits)
0542             *bit = pos;
0543     } else {
0544         err = scan_nres_bitmap(ni, b, indx, from, &scan_for_used, bit);
0545         if (err)
0546             return err;
0547     }
0548 
0549     return 0;
0550 }
0551 
0552 /*
0553  * hdr_find_split
0554  *
0555  * Find a point at which the index allocation buffer would like to be split.
0556  * NOTE: This function should never return 'END' entry NULL returns on error.
0557  */
0558 static const struct NTFS_DE *hdr_find_split(const struct INDEX_HDR *hdr)
0559 {
0560     size_t o;
0561     const struct NTFS_DE *e = hdr_first_de(hdr);
0562     u32 used_2 = le32_to_cpu(hdr->used) >> 1;
0563     u16 esize;
0564 
0565     if (!e || de_is_last(e))
0566         return NULL;
0567 
0568     esize = le16_to_cpu(e->size);
0569     for (o = le32_to_cpu(hdr->de_off) + esize; o < used_2; o += esize) {
0570         const struct NTFS_DE *p = e;
0571 
0572         e = Add2Ptr(hdr, o);
0573 
0574         /* We must not return END entry. */
0575         if (de_is_last(e))
0576             return p;
0577 
0578         esize = le16_to_cpu(e->size);
0579     }
0580 
0581     return e;
0582 }
0583 
0584 /*
0585  * hdr_insert_head - Insert some entries at the beginning of the buffer.
0586  *
0587  * It is used to insert entries into a newly-created buffer.
0588  */
0589 static const struct NTFS_DE *hdr_insert_head(struct INDEX_HDR *hdr,
0590                          const void *ins, u32 ins_bytes)
0591 {
0592     u32 to_move;
0593     struct NTFS_DE *e = hdr_first_de(hdr);
0594     u32 used = le32_to_cpu(hdr->used);
0595 
0596     if (!e)
0597         return NULL;
0598 
0599     /* Now we just make room for the inserted entries and jam it in. */
0600     to_move = used - le32_to_cpu(hdr->de_off);
0601     memmove(Add2Ptr(e, ins_bytes), e, to_move);
0602     memcpy(e, ins, ins_bytes);
0603     hdr->used = cpu_to_le32(used + ins_bytes);
0604 
0605     return e;
0606 }
0607 
0608 void fnd_clear(struct ntfs_fnd *fnd)
0609 {
0610     int i;
0611 
0612     for (i = 0; i < fnd->level; i++) {
0613         struct indx_node *n = fnd->nodes[i];
0614 
0615         if (!n)
0616             continue;
0617 
0618         put_indx_node(n);
0619         fnd->nodes[i] = NULL;
0620     }
0621     fnd->level = 0;
0622     fnd->root_de = NULL;
0623 }
0624 
0625 static int fnd_push(struct ntfs_fnd *fnd, struct indx_node *n,
0626             struct NTFS_DE *e)
0627 {
0628     int i;
0629 
0630     i = fnd->level;
0631     if (i < 0 || i >= ARRAY_SIZE(fnd->nodes))
0632         return -EINVAL;
0633     fnd->nodes[i] = n;
0634     fnd->de[i] = e;
0635     fnd->level += 1;
0636     return 0;
0637 }
0638 
0639 static struct indx_node *fnd_pop(struct ntfs_fnd *fnd)
0640 {
0641     struct indx_node *n;
0642     int i = fnd->level;
0643 
0644     i -= 1;
0645     n = fnd->nodes[i];
0646     fnd->nodes[i] = NULL;
0647     fnd->level = i;
0648 
0649     return n;
0650 }
0651 
0652 static bool fnd_is_empty(struct ntfs_fnd *fnd)
0653 {
0654     if (!fnd->level)
0655         return !fnd->root_de;
0656 
0657     return !fnd->de[fnd->level - 1];
0658 }
0659 
0660 /*
0661  * hdr_find_e - Locate an entry the index buffer.
0662  *
0663  * If no matching entry is found, it returns the first entry which is greater
0664  * than the desired entry If the search key is greater than all the entries the
0665  * buffer, it returns the 'end' entry. This function does a binary search of the
0666  * current index buffer, for the first entry that is <= to the search value.
0667  *
0668  * Return: NULL if error.
0669  */
0670 static struct NTFS_DE *hdr_find_e(const struct ntfs_index *indx,
0671                   const struct INDEX_HDR *hdr, const void *key,
0672                   size_t key_len, const void *ctx, int *diff)
0673 {
0674     struct NTFS_DE *e, *found = NULL;
0675     NTFS_CMP_FUNC cmp = indx->cmp;
0676     int min_idx = 0, mid_idx, max_idx = 0;
0677     int diff2;
0678     int table_size = 8;
0679     u32 e_size, e_key_len;
0680     u32 end = le32_to_cpu(hdr->used);
0681     u32 off = le32_to_cpu(hdr->de_off);
0682     u16 offs[128];
0683 
0684 fill_table:
0685     if (off + sizeof(struct NTFS_DE) > end)
0686         return NULL;
0687 
0688     e = Add2Ptr(hdr, off);
0689     e_size = le16_to_cpu(e->size);
0690 
0691     if (e_size < sizeof(struct NTFS_DE) || off + e_size > end)
0692         return NULL;
0693 
0694     if (!de_is_last(e)) {
0695         offs[max_idx] = off;
0696         off += e_size;
0697 
0698         max_idx++;
0699         if (max_idx < table_size)
0700             goto fill_table;
0701 
0702         max_idx--;
0703     }
0704 
0705 binary_search:
0706     e_key_len = le16_to_cpu(e->key_size);
0707 
0708     diff2 = (*cmp)(key, key_len, e + 1, e_key_len, ctx);
0709     if (diff2 > 0) {
0710         if (found) {
0711             min_idx = mid_idx + 1;
0712         } else {
0713             if (de_is_last(e))
0714                 return NULL;
0715 
0716             max_idx = 0;
0717             table_size = min(table_size * 2,
0718                      (int)ARRAY_SIZE(offs));
0719             goto fill_table;
0720         }
0721     } else if (diff2 < 0) {
0722         if (found)
0723             max_idx = mid_idx - 1;
0724         else
0725             max_idx--;
0726 
0727         found = e;
0728     } else {
0729         *diff = 0;
0730         return e;
0731     }
0732 
0733     if (min_idx > max_idx) {
0734         *diff = -1;
0735         return found;
0736     }
0737 
0738     mid_idx = (min_idx + max_idx) >> 1;
0739     e = Add2Ptr(hdr, offs[mid_idx]);
0740 
0741     goto binary_search;
0742 }
0743 
0744 /*
0745  * hdr_insert_de - Insert an index entry into the buffer.
0746  *
0747  * 'before' should be a pointer previously returned from hdr_find_e.
0748  */
0749 static struct NTFS_DE *hdr_insert_de(const struct ntfs_index *indx,
0750                      struct INDEX_HDR *hdr,
0751                      const struct NTFS_DE *de,
0752                      struct NTFS_DE *before, const void *ctx)
0753 {
0754     int diff;
0755     size_t off = PtrOffset(hdr, before);
0756     u32 used = le32_to_cpu(hdr->used);
0757     u32 total = le32_to_cpu(hdr->total);
0758     u16 de_size = le16_to_cpu(de->size);
0759 
0760     /* First, check to see if there's enough room. */
0761     if (used + de_size > total)
0762         return NULL;
0763 
0764     /* We know there's enough space, so we know we'll succeed. */
0765     if (before) {
0766         /* Check that before is inside Index. */
0767         if (off >= used || off < le32_to_cpu(hdr->de_off) ||
0768             off + le16_to_cpu(before->size) > total) {
0769             return NULL;
0770         }
0771         goto ok;
0772     }
0773     /* No insert point is applied. Get it manually. */
0774     before = hdr_find_e(indx, hdr, de + 1, le16_to_cpu(de->key_size), ctx,
0775                 &diff);
0776     if (!before)
0777         return NULL;
0778     off = PtrOffset(hdr, before);
0779 
0780 ok:
0781     /* Now we just make room for the entry and jam it in. */
0782     memmove(Add2Ptr(before, de_size), before, used - off);
0783 
0784     hdr->used = cpu_to_le32(used + de_size);
0785     memcpy(before, de, de_size);
0786 
0787     return before;
0788 }
0789 
0790 /*
0791  * hdr_delete_de - Remove an entry from the index buffer.
0792  */
0793 static inline struct NTFS_DE *hdr_delete_de(struct INDEX_HDR *hdr,
0794                         struct NTFS_DE *re)
0795 {
0796     u32 used = le32_to_cpu(hdr->used);
0797     u16 esize = le16_to_cpu(re->size);
0798     u32 off = PtrOffset(hdr, re);
0799     int bytes = used - (off + esize);
0800 
0801     if (off >= used || esize < sizeof(struct NTFS_DE) ||
0802         bytes < sizeof(struct NTFS_DE))
0803         return NULL;
0804 
0805     hdr->used = cpu_to_le32(used - esize);
0806     memmove(re, Add2Ptr(re, esize), bytes);
0807 
0808     return re;
0809 }
0810 
0811 void indx_clear(struct ntfs_index *indx)
0812 {
0813     run_close(&indx->alloc_run);
0814     run_close(&indx->bitmap_run);
0815 }
0816 
0817 int indx_init(struct ntfs_index *indx, struct ntfs_sb_info *sbi,
0818           const struct ATTRIB *attr, enum index_mutex_classed type)
0819 {
0820     u32 t32;
0821     const struct INDEX_ROOT *root = resident_data(attr);
0822 
0823     /* Check root fields. */
0824     if (!root->index_block_clst)
0825         return -EINVAL;
0826 
0827     indx->type = type;
0828     indx->idx2vbn_bits = __ffs(root->index_block_clst);
0829 
0830     t32 = le32_to_cpu(root->index_block_size);
0831     indx->index_bits = blksize_bits(t32);
0832 
0833     /* Check index record size. */
0834     if (t32 < sbi->cluster_size) {
0835         /* Index record is smaller than a cluster, use 512 blocks. */
0836         if (t32 != root->index_block_clst * SECTOR_SIZE)
0837             return -EINVAL;
0838 
0839         /* Check alignment to a cluster. */
0840         if ((sbi->cluster_size >> SECTOR_SHIFT) &
0841             (root->index_block_clst - 1)) {
0842             return -EINVAL;
0843         }
0844 
0845         indx->vbn2vbo_bits = SECTOR_SHIFT;
0846     } else {
0847         /* Index record must be a multiple of cluster size. */
0848         if (t32 != root->index_block_clst << sbi->cluster_bits)
0849             return -EINVAL;
0850 
0851         indx->vbn2vbo_bits = sbi->cluster_bits;
0852     }
0853 
0854     init_rwsem(&indx->run_lock);
0855 
0856     indx->cmp = get_cmp_func(root);
0857     return indx->cmp ? 0 : -EINVAL;
0858 }
0859 
0860 static struct indx_node *indx_new(struct ntfs_index *indx,
0861                   struct ntfs_inode *ni, CLST vbn,
0862                   const __le64 *sub_vbn)
0863 {
0864     int err;
0865     struct NTFS_DE *e;
0866     struct indx_node *r;
0867     struct INDEX_HDR *hdr;
0868     struct INDEX_BUFFER *index;
0869     u64 vbo = (u64)vbn << indx->vbn2vbo_bits;
0870     u32 bytes = 1u << indx->index_bits;
0871     u16 fn;
0872     u32 eo;
0873 
0874     r = kzalloc(sizeof(struct indx_node), GFP_NOFS);
0875     if (!r)
0876         return ERR_PTR(-ENOMEM);
0877 
0878     index = kzalloc(bytes, GFP_NOFS);
0879     if (!index) {
0880         kfree(r);
0881         return ERR_PTR(-ENOMEM);
0882     }
0883 
0884     err = ntfs_get_bh(ni->mi.sbi, &indx->alloc_run, vbo, bytes, &r->nb);
0885 
0886     if (err) {
0887         kfree(index);
0888         kfree(r);
0889         return ERR_PTR(err);
0890     }
0891 
0892     /* Create header. */
0893     index->rhdr.sign = NTFS_INDX_SIGNATURE;
0894     index->rhdr.fix_off = cpu_to_le16(sizeof(struct INDEX_BUFFER)); // 0x28
0895     fn = (bytes >> SECTOR_SHIFT) + 1; // 9
0896     index->rhdr.fix_num = cpu_to_le16(fn);
0897     index->vbn = cpu_to_le64(vbn);
0898     hdr = &index->ihdr;
0899     eo = ALIGN(sizeof(struct INDEX_BUFFER) + fn * sizeof(short), 8);
0900     hdr->de_off = cpu_to_le32(eo);
0901 
0902     e = Add2Ptr(hdr, eo);
0903 
0904     if (sub_vbn) {
0905         e->flags = NTFS_IE_LAST | NTFS_IE_HAS_SUBNODES;
0906         e->size = cpu_to_le16(sizeof(struct NTFS_DE) + sizeof(u64));
0907         hdr->used =
0908             cpu_to_le32(eo + sizeof(struct NTFS_DE) + sizeof(u64));
0909         de_set_vbn_le(e, *sub_vbn);
0910         hdr->flags = 1;
0911     } else {
0912         e->size = cpu_to_le16(sizeof(struct NTFS_DE));
0913         hdr->used = cpu_to_le32(eo + sizeof(struct NTFS_DE));
0914         e->flags = NTFS_IE_LAST;
0915     }
0916 
0917     hdr->total = cpu_to_le32(bytes - offsetof(struct INDEX_BUFFER, ihdr));
0918 
0919     r->index = index;
0920     return r;
0921 }
0922 
0923 struct INDEX_ROOT *indx_get_root(struct ntfs_index *indx, struct ntfs_inode *ni,
0924                  struct ATTRIB **attr, struct mft_inode **mi)
0925 {
0926     struct ATTR_LIST_ENTRY *le = NULL;
0927     struct ATTRIB *a;
0928     const struct INDEX_NAMES *in = &s_index_names[indx->type];
0929 
0930     a = ni_find_attr(ni, NULL, &le, ATTR_ROOT, in->name, in->name_len, NULL,
0931              mi);
0932     if (!a)
0933         return NULL;
0934 
0935     if (attr)
0936         *attr = a;
0937 
0938     return resident_data_ex(a, sizeof(struct INDEX_ROOT));
0939 }
0940 
0941 static int indx_write(struct ntfs_index *indx, struct ntfs_inode *ni,
0942               struct indx_node *node, int sync)
0943 {
0944     struct INDEX_BUFFER *ib = node->index;
0945 
0946     return ntfs_write_bh(ni->mi.sbi, &ib->rhdr, &node->nb, sync);
0947 }
0948 
0949 /*
0950  * indx_read
0951  *
0952  * If ntfs_readdir calls this function
0953  * inode is shared locked and no ni_lock.
0954  * Use rw_semaphore for read/write access to alloc_run.
0955  */
0956 int indx_read(struct ntfs_index *indx, struct ntfs_inode *ni, CLST vbn,
0957           struct indx_node **node)
0958 {
0959     int err;
0960     struct INDEX_BUFFER *ib;
0961     struct runs_tree *run = &indx->alloc_run;
0962     struct rw_semaphore *lock = &indx->run_lock;
0963     u64 vbo = (u64)vbn << indx->vbn2vbo_bits;
0964     u32 bytes = 1u << indx->index_bits;
0965     struct indx_node *in = *node;
0966     const struct INDEX_NAMES *name;
0967 
0968     if (!in) {
0969         in = kzalloc(sizeof(struct indx_node), GFP_NOFS);
0970         if (!in)
0971             return -ENOMEM;
0972     } else {
0973         nb_put(&in->nb);
0974     }
0975 
0976     ib = in->index;
0977     if (!ib) {
0978         ib = kmalloc(bytes, GFP_NOFS);
0979         if (!ib) {
0980             err = -ENOMEM;
0981             goto out;
0982         }
0983     }
0984 
0985     down_read(lock);
0986     err = ntfs_read_bh(ni->mi.sbi, run, vbo, &ib->rhdr, bytes, &in->nb);
0987     up_read(lock);
0988     if (!err)
0989         goto ok;
0990 
0991     if (err == -E_NTFS_FIXUP)
0992         goto ok;
0993 
0994     if (err != -ENOENT)
0995         goto out;
0996 
0997     name = &s_index_names[indx->type];
0998     down_write(lock);
0999     err = attr_load_runs_range(ni, ATTR_ALLOC, name->name, name->name_len,
1000                    run, vbo, vbo + bytes);
1001     up_write(lock);
1002     if (err)
1003         goto out;
1004 
1005     down_read(lock);
1006     err = ntfs_read_bh(ni->mi.sbi, run, vbo, &ib->rhdr, bytes, &in->nb);
1007     up_read(lock);
1008     if (err == -E_NTFS_FIXUP)
1009         goto ok;
1010 
1011     if (err)
1012         goto out;
1013 
1014 ok:
1015     if (err == -E_NTFS_FIXUP) {
1016         ntfs_write_bh(ni->mi.sbi, &ib->rhdr, &in->nb, 0);
1017         err = 0;
1018     }
1019 
1020     in->index = ib;
1021     *node = in;
1022 
1023 out:
1024     if (ib != in->index)
1025         kfree(ib);
1026 
1027     if (*node != in) {
1028         nb_put(&in->nb);
1029         kfree(in);
1030     }
1031 
1032     return err;
1033 }
1034 
1035 /*
1036  * indx_find - Scan NTFS directory for given entry.
1037  */
1038 int indx_find(struct ntfs_index *indx, struct ntfs_inode *ni,
1039           const struct INDEX_ROOT *root, const void *key, size_t key_len,
1040           const void *ctx, int *diff, struct NTFS_DE **entry,
1041           struct ntfs_fnd *fnd)
1042 {
1043     int err;
1044     struct NTFS_DE *e;
1045     struct indx_node *node;
1046 
1047     if (!root)
1048         root = indx_get_root(&ni->dir, ni, NULL, NULL);
1049 
1050     if (!root) {
1051         /* Should not happen. */
1052         return -EINVAL;
1053     }
1054 
1055     /* Check cache. */
1056     e = fnd->level ? fnd->de[fnd->level - 1] : fnd->root_de;
1057     if (e && !de_is_last(e) &&
1058         !(*indx->cmp)(key, key_len, e + 1, le16_to_cpu(e->key_size), ctx)) {
1059         *entry = e;
1060         *diff = 0;
1061         return 0;
1062     }
1063 
1064     /* Soft finder reset. */
1065     fnd_clear(fnd);
1066 
1067     /* Lookup entry that is <= to the search value. */
1068     e = hdr_find_e(indx, &root->ihdr, key, key_len, ctx, diff);
1069     if (!e)
1070         return -EINVAL;
1071 
1072     fnd->root_de = e;
1073 
1074     for (;;) {
1075         node = NULL;
1076         if (*diff >= 0 || !de_has_vcn_ex(e))
1077             break;
1078 
1079         /* Read next level. */
1080         err = indx_read(indx, ni, de_get_vbn(e), &node);
1081         if (err)
1082             return err;
1083 
1084         /* Lookup entry that is <= to the search value. */
1085         e = hdr_find_e(indx, &node->index->ihdr, key, key_len, ctx,
1086                    diff);
1087         if (!e) {
1088             put_indx_node(node);
1089             return -EINVAL;
1090         }
1091 
1092         fnd_push(fnd, node, e);
1093     }
1094 
1095     *entry = e;
1096     return 0;
1097 }
1098 
1099 int indx_find_sort(struct ntfs_index *indx, struct ntfs_inode *ni,
1100            const struct INDEX_ROOT *root, struct NTFS_DE **entry,
1101            struct ntfs_fnd *fnd)
1102 {
1103     int err;
1104     struct indx_node *n = NULL;
1105     struct NTFS_DE *e;
1106     size_t iter = 0;
1107     int level = fnd->level;
1108 
1109     if (!*entry) {
1110         /* Start find. */
1111         e = hdr_first_de(&root->ihdr);
1112         if (!e)
1113             return 0;
1114         fnd_clear(fnd);
1115         fnd->root_de = e;
1116     } else if (!level) {
1117         if (de_is_last(fnd->root_de)) {
1118             *entry = NULL;
1119             return 0;
1120         }
1121 
1122         e = hdr_next_de(&root->ihdr, fnd->root_de);
1123         if (!e)
1124             return -EINVAL;
1125         fnd->root_de = e;
1126     } else {
1127         n = fnd->nodes[level - 1];
1128         e = fnd->de[level - 1];
1129 
1130         if (de_is_last(e))
1131             goto pop_level;
1132 
1133         e = hdr_next_de(&n->index->ihdr, e);
1134         if (!e)
1135             return -EINVAL;
1136 
1137         fnd->de[level - 1] = e;
1138     }
1139 
1140     /* Just to avoid tree cycle. */
1141 next_iter:
1142     if (iter++ >= 1000)
1143         return -EINVAL;
1144 
1145     while (de_has_vcn_ex(e)) {
1146         if (le16_to_cpu(e->size) <
1147             sizeof(struct NTFS_DE) + sizeof(u64)) {
1148             if (n) {
1149                 fnd_pop(fnd);
1150                 kfree(n);
1151             }
1152             return -EINVAL;
1153         }
1154 
1155         /* Read next level. */
1156         err = indx_read(indx, ni, de_get_vbn(e), &n);
1157         if (err)
1158             return err;
1159 
1160         /* Try next level. */
1161         e = hdr_first_de(&n->index->ihdr);
1162         if (!e) {
1163             kfree(n);
1164             return -EINVAL;
1165         }
1166 
1167         fnd_push(fnd, n, e);
1168     }
1169 
1170     if (le16_to_cpu(e->size) > sizeof(struct NTFS_DE)) {
1171         *entry = e;
1172         return 0;
1173     }
1174 
1175 pop_level:
1176     for (;;) {
1177         if (!de_is_last(e))
1178             goto next_iter;
1179 
1180         /* Pop one level. */
1181         if (n) {
1182             fnd_pop(fnd);
1183             kfree(n);
1184         }
1185 
1186         level = fnd->level;
1187 
1188         if (level) {
1189             n = fnd->nodes[level - 1];
1190             e = fnd->de[level - 1];
1191         } else if (fnd->root_de) {
1192             n = NULL;
1193             e = fnd->root_de;
1194             fnd->root_de = NULL;
1195         } else {
1196             *entry = NULL;
1197             return 0;
1198         }
1199 
1200         if (le16_to_cpu(e->size) > sizeof(struct NTFS_DE)) {
1201             *entry = e;
1202             if (!fnd->root_de)
1203                 fnd->root_de = e;
1204             return 0;
1205         }
1206     }
1207 }
1208 
1209 int indx_find_raw(struct ntfs_index *indx, struct ntfs_inode *ni,
1210           const struct INDEX_ROOT *root, struct NTFS_DE **entry,
1211           size_t *off, struct ntfs_fnd *fnd)
1212 {
1213     int err;
1214     struct indx_node *n = NULL;
1215     struct NTFS_DE *e = NULL;
1216     struct NTFS_DE *e2;
1217     size_t bit;
1218     CLST next_used_vbn;
1219     CLST next_vbn;
1220     u32 record_size = ni->mi.sbi->record_size;
1221 
1222     /* Use non sorted algorithm. */
1223     if (!*entry) {
1224         /* This is the first call. */
1225         e = hdr_first_de(&root->ihdr);
1226         if (!e)
1227             return 0;
1228         fnd_clear(fnd);
1229         fnd->root_de = e;
1230 
1231         /* The first call with setup of initial element. */
1232         if (*off >= record_size) {
1233             next_vbn = (((*off - record_size) >> indx->index_bits))
1234                    << indx->idx2vbn_bits;
1235             /* Jump inside cycle 'for'. */
1236             goto next;
1237         }
1238 
1239         /* Start enumeration from root. */
1240         *off = 0;
1241     } else if (!fnd->root_de)
1242         return -EINVAL;
1243 
1244     for (;;) {
1245         /* Check if current entry can be used. */
1246         if (e && le16_to_cpu(e->size) > sizeof(struct NTFS_DE))
1247             goto ok;
1248 
1249         if (!fnd->level) {
1250             /* Continue to enumerate root. */
1251             if (!de_is_last(fnd->root_de)) {
1252                 e = hdr_next_de(&root->ihdr, fnd->root_de);
1253                 if (!e)
1254                     return -EINVAL;
1255                 fnd->root_de = e;
1256                 continue;
1257             }
1258 
1259             /* Start to enumerate indexes from 0. */
1260             next_vbn = 0;
1261         } else {
1262             /* Continue to enumerate indexes. */
1263             e2 = fnd->de[fnd->level - 1];
1264 
1265             n = fnd->nodes[fnd->level - 1];
1266 
1267             if (!de_is_last(e2)) {
1268                 e = hdr_next_de(&n->index->ihdr, e2);
1269                 if (!e)
1270                     return -EINVAL;
1271                 fnd->de[fnd->level - 1] = e;
1272                 continue;
1273             }
1274 
1275             /* Continue with next index. */
1276             next_vbn = le64_to_cpu(n->index->vbn) +
1277                    root->index_block_clst;
1278         }
1279 
1280 next:
1281         /* Release current index. */
1282         if (n) {
1283             fnd_pop(fnd);
1284             put_indx_node(n);
1285             n = NULL;
1286         }
1287 
1288         /* Skip all free indexes. */
1289         bit = next_vbn >> indx->idx2vbn_bits;
1290         err = indx_used_bit(indx, ni, &bit);
1291         if (err == -ENOENT || bit == MINUS_ONE_T) {
1292             /* No used indexes. */
1293             *entry = NULL;
1294             return 0;
1295         }
1296 
1297         next_used_vbn = bit << indx->idx2vbn_bits;
1298 
1299         /* Read buffer into memory. */
1300         err = indx_read(indx, ni, next_used_vbn, &n);
1301         if (err)
1302             return err;
1303 
1304         e = hdr_first_de(&n->index->ihdr);
1305         fnd_push(fnd, n, e);
1306         if (!e)
1307             return -EINVAL;
1308     }
1309 
1310 ok:
1311     /* Return offset to restore enumerator if necessary. */
1312     if (!n) {
1313         /* 'e' points in root, */
1314         *off = PtrOffset(&root->ihdr, e);
1315     } else {
1316         /* 'e' points in index, */
1317         *off = (le64_to_cpu(n->index->vbn) << indx->vbn2vbo_bits) +
1318                record_size + PtrOffset(&n->index->ihdr, e);
1319     }
1320 
1321     *entry = e;
1322     return 0;
1323 }
1324 
1325 /*
1326  * indx_create_allocate - Create "Allocation + Bitmap" attributes.
1327  */
1328 static int indx_create_allocate(struct ntfs_index *indx, struct ntfs_inode *ni,
1329                 CLST *vbn)
1330 {
1331     int err;
1332     struct ntfs_sb_info *sbi = ni->mi.sbi;
1333     struct ATTRIB *bitmap;
1334     struct ATTRIB *alloc;
1335     u32 data_size = 1u << indx->index_bits;
1336     u32 alloc_size = ntfs_up_cluster(sbi, data_size);
1337     CLST len = alloc_size >> sbi->cluster_bits;
1338     const struct INDEX_NAMES *in = &s_index_names[indx->type];
1339     CLST alen;
1340     struct runs_tree run;
1341 
1342     run_init(&run);
1343 
1344     err = attr_allocate_clusters(sbi, &run, 0, 0, len, NULL, 0, &alen, 0,
1345                      NULL);
1346     if (err)
1347         goto out;
1348 
1349     err = ni_insert_nonresident(ni, ATTR_ALLOC, in->name, in->name_len,
1350                     &run, 0, len, 0, &alloc, NULL, NULL);
1351     if (err)
1352         goto out1;
1353 
1354     alloc->nres.valid_size = alloc->nres.data_size = cpu_to_le64(data_size);
1355 
1356     err = ni_insert_resident(ni, bitmap_size(1), ATTR_BITMAP, in->name,
1357                  in->name_len, &bitmap, NULL, NULL);
1358     if (err)
1359         goto out2;
1360 
1361     if (in->name == I30_NAME) {
1362         ni->vfs_inode.i_size = data_size;
1363         inode_set_bytes(&ni->vfs_inode, alloc_size);
1364     }
1365 
1366     memcpy(&indx->alloc_run, &run, sizeof(run));
1367 
1368     *vbn = 0;
1369 
1370     return 0;
1371 
1372 out2:
1373     mi_remove_attr(NULL, &ni->mi, alloc);
1374 
1375 out1:
1376     run_deallocate(sbi, &run, false);
1377 
1378 out:
1379     return err;
1380 }
1381 
1382 /*
1383  * indx_add_allocate - Add clusters to index.
1384  */
1385 static int indx_add_allocate(struct ntfs_index *indx, struct ntfs_inode *ni,
1386                  CLST *vbn)
1387 {
1388     int err;
1389     size_t bit;
1390     u64 data_size;
1391     u64 bmp_size, bmp_size_v;
1392     struct ATTRIB *bmp, *alloc;
1393     struct mft_inode *mi;
1394     const struct INDEX_NAMES *in = &s_index_names[indx->type];
1395 
1396     err = indx_find_free(indx, ni, &bit, &bmp);
1397     if (err)
1398         goto out1;
1399 
1400     if (bit != MINUS_ONE_T) {
1401         bmp = NULL;
1402     } else {
1403         if (bmp->non_res) {
1404             bmp_size = le64_to_cpu(bmp->nres.data_size);
1405             bmp_size_v = le64_to_cpu(bmp->nres.valid_size);
1406         } else {
1407             bmp_size = bmp_size_v = le32_to_cpu(bmp->res.data_size);
1408         }
1409 
1410         bit = bmp_size << 3;
1411     }
1412 
1413     data_size = (u64)(bit + 1) << indx->index_bits;
1414 
1415     if (bmp) {
1416         /* Increase bitmap. */
1417         err = attr_set_size(ni, ATTR_BITMAP, in->name, in->name_len,
1418                     &indx->bitmap_run, bitmap_size(bit + 1),
1419                     NULL, true, NULL);
1420         if (err)
1421             goto out1;
1422     }
1423 
1424     alloc = ni_find_attr(ni, NULL, NULL, ATTR_ALLOC, in->name, in->name_len,
1425                  NULL, &mi);
1426     if (!alloc) {
1427         err = -EINVAL;
1428         if (bmp)
1429             goto out2;
1430         goto out1;
1431     }
1432 
1433     /* Increase allocation. */
1434     err = attr_set_size(ni, ATTR_ALLOC, in->name, in->name_len,
1435                 &indx->alloc_run, data_size, &data_size, true,
1436                 NULL);
1437     if (err) {
1438         if (bmp)
1439             goto out2;
1440         goto out1;
1441     }
1442 
1443     *vbn = bit << indx->idx2vbn_bits;
1444 
1445     return 0;
1446 
1447 out2:
1448     /* Ops. No space? */
1449     attr_set_size(ni, ATTR_BITMAP, in->name, in->name_len,
1450               &indx->bitmap_run, bmp_size, &bmp_size_v, false, NULL);
1451 
1452 out1:
1453     return err;
1454 }
1455 
1456 /*
1457  * indx_insert_into_root - Attempt to insert an entry into the index root.
1458  *
1459  * @undo - True if we undoing previous remove.
1460  * If necessary, it will twiddle the index b-tree.
1461  */
1462 static int indx_insert_into_root(struct ntfs_index *indx, struct ntfs_inode *ni,
1463                  const struct NTFS_DE *new_de,
1464                  struct NTFS_DE *root_de, const void *ctx,
1465                  struct ntfs_fnd *fnd, bool undo)
1466 {
1467     int err = 0;
1468     struct NTFS_DE *e, *e0, *re;
1469     struct mft_inode *mi;
1470     struct ATTRIB *attr;
1471     struct INDEX_HDR *hdr;
1472     struct indx_node *n;
1473     CLST new_vbn;
1474     __le64 *sub_vbn, t_vbn;
1475     u16 new_de_size;
1476     u32 hdr_used, hdr_total, asize, to_move;
1477     u32 root_size, new_root_size;
1478     struct ntfs_sb_info *sbi;
1479     int ds_root;
1480     struct INDEX_ROOT *root, *a_root;
1481 
1482     /* Get the record this root placed in. */
1483     root = indx_get_root(indx, ni, &attr, &mi);
1484     if (!root)
1485         return -EINVAL;
1486 
1487     /*
1488      * Try easy case:
1489      * hdr_insert_de will succeed if there's
1490      * room the root for the new entry.
1491      */
1492     hdr = &root->ihdr;
1493     sbi = ni->mi.sbi;
1494     new_de_size = le16_to_cpu(new_de->size);
1495     hdr_used = le32_to_cpu(hdr->used);
1496     hdr_total = le32_to_cpu(hdr->total);
1497     asize = le32_to_cpu(attr->size);
1498     root_size = le32_to_cpu(attr->res.data_size);
1499 
1500     ds_root = new_de_size + hdr_used - hdr_total;
1501 
1502     /* If 'undo' is set then reduce requirements. */
1503     if ((undo || asize + ds_root < sbi->max_bytes_per_attr) &&
1504         mi_resize_attr(mi, attr, ds_root)) {
1505         hdr->total = cpu_to_le32(hdr_total + ds_root);
1506         e = hdr_insert_de(indx, hdr, new_de, root_de, ctx);
1507         WARN_ON(!e);
1508         fnd_clear(fnd);
1509         fnd->root_de = e;
1510 
1511         return 0;
1512     }
1513 
1514     /* Make a copy of root attribute to restore if error. */
1515     a_root = kmemdup(attr, asize, GFP_NOFS);
1516     if (!a_root)
1517         return -ENOMEM;
1518 
1519     /*
1520      * Copy all the non-end entries from
1521      * the index root to the new buffer.
1522      */
1523     to_move = 0;
1524     e0 = hdr_first_de(hdr);
1525 
1526     /* Calculate the size to copy. */
1527     for (e = e0;; e = hdr_next_de(hdr, e)) {
1528         if (!e) {
1529             err = -EINVAL;
1530             goto out_free_root;
1531         }
1532 
1533         if (de_is_last(e))
1534             break;
1535         to_move += le16_to_cpu(e->size);
1536     }
1537 
1538     if (!to_move) {
1539         re = NULL;
1540     } else {
1541         re = kmemdup(e0, to_move, GFP_NOFS);
1542         if (!re) {
1543             err = -ENOMEM;
1544             goto out_free_root;
1545         }
1546     }
1547 
1548     sub_vbn = NULL;
1549     if (de_has_vcn(e)) {
1550         t_vbn = de_get_vbn_le(e);
1551         sub_vbn = &t_vbn;
1552     }
1553 
1554     new_root_size = sizeof(struct INDEX_ROOT) + sizeof(struct NTFS_DE) +
1555             sizeof(u64);
1556     ds_root = new_root_size - root_size;
1557 
1558     if (ds_root > 0 && asize + ds_root > sbi->max_bytes_per_attr) {
1559         /* Make root external. */
1560         err = -EOPNOTSUPP;
1561         goto out_free_re;
1562     }
1563 
1564     if (ds_root)
1565         mi_resize_attr(mi, attr, ds_root);
1566 
1567     /* Fill first entry (vcn will be set later). */
1568     e = (struct NTFS_DE *)(root + 1);
1569     memset(e, 0, sizeof(struct NTFS_DE));
1570     e->size = cpu_to_le16(sizeof(struct NTFS_DE) + sizeof(u64));
1571     e->flags = NTFS_IE_HAS_SUBNODES | NTFS_IE_LAST;
1572 
1573     hdr->flags = 1;
1574     hdr->used = hdr->total =
1575         cpu_to_le32(new_root_size - offsetof(struct INDEX_ROOT, ihdr));
1576 
1577     fnd->root_de = hdr_first_de(hdr);
1578     mi->dirty = true;
1579 
1580     /* Create alloc and bitmap attributes (if not). */
1581     err = run_is_empty(&indx->alloc_run)
1582               ? indx_create_allocate(indx, ni, &new_vbn)
1583               : indx_add_allocate(indx, ni, &new_vbn);
1584 
1585     /* Layout of record may be changed, so rescan root. */
1586     root = indx_get_root(indx, ni, &attr, &mi);
1587     if (!root) {
1588         /* Bug? */
1589         ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
1590         err = -EINVAL;
1591         goto out_free_re;
1592     }
1593 
1594     if (err) {
1595         /* Restore root. */
1596         if (mi_resize_attr(mi, attr, -ds_root))
1597             memcpy(attr, a_root, asize);
1598         else {
1599             /* Bug? */
1600             ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
1601         }
1602         goto out_free_re;
1603     }
1604 
1605     e = (struct NTFS_DE *)(root + 1);
1606     *(__le64 *)(e + 1) = cpu_to_le64(new_vbn);
1607     mi->dirty = true;
1608 
1609     /* Now we can create/format the new buffer and copy the entries into. */
1610     n = indx_new(indx, ni, new_vbn, sub_vbn);
1611     if (IS_ERR(n)) {
1612         err = PTR_ERR(n);
1613         goto out_free_re;
1614     }
1615 
1616     hdr = &n->index->ihdr;
1617     hdr_used = le32_to_cpu(hdr->used);
1618     hdr_total = le32_to_cpu(hdr->total);
1619 
1620     /* Copy root entries into new buffer. */
1621     hdr_insert_head(hdr, re, to_move);
1622 
1623     /* Update bitmap attribute. */
1624     indx_mark_used(indx, ni, new_vbn >> indx->idx2vbn_bits);
1625 
1626     /* Check if we can insert new entry new index buffer. */
1627     if (hdr_used + new_de_size > hdr_total) {
1628         /*
1629          * This occurs if MFT record is the same or bigger than index
1630          * buffer. Move all root new index and have no space to add
1631          * new entry classic case when MFT record is 1K and index
1632          * buffer 4K the problem should not occurs.
1633          */
1634         kfree(re);
1635         indx_write(indx, ni, n, 0);
1636 
1637         put_indx_node(n);
1638         fnd_clear(fnd);
1639         err = indx_insert_entry(indx, ni, new_de, ctx, fnd, undo);
1640         goto out_free_root;
1641     }
1642 
1643     /*
1644      * Now root is a parent for new index buffer.
1645      * Insert NewEntry a new buffer.
1646      */
1647     e = hdr_insert_de(indx, hdr, new_de, NULL, ctx);
1648     if (!e) {
1649         err = -EINVAL;
1650         goto out_put_n;
1651     }
1652     fnd_push(fnd, n, e);
1653 
1654     /* Just write updates index into disk. */
1655     indx_write(indx, ni, n, 0);
1656 
1657     n = NULL;
1658 
1659 out_put_n:
1660     put_indx_node(n);
1661 out_free_re:
1662     kfree(re);
1663 out_free_root:
1664     kfree(a_root);
1665     return err;
1666 }
1667 
1668 /*
1669  * indx_insert_into_buffer
1670  *
1671  * Attempt to insert an entry into an Index Allocation Buffer.
1672  * If necessary, it will split the buffer.
1673  */
1674 static int
1675 indx_insert_into_buffer(struct ntfs_index *indx, struct ntfs_inode *ni,
1676             struct INDEX_ROOT *root, const struct NTFS_DE *new_de,
1677             const void *ctx, int level, struct ntfs_fnd *fnd)
1678 {
1679     int err;
1680     const struct NTFS_DE *sp;
1681     struct NTFS_DE *e, *de_t, *up_e;
1682     struct indx_node *n2;
1683     struct indx_node *n1 = fnd->nodes[level];
1684     struct INDEX_HDR *hdr1 = &n1->index->ihdr;
1685     struct INDEX_HDR *hdr2;
1686     u32 to_copy, used;
1687     CLST new_vbn;
1688     __le64 t_vbn, *sub_vbn;
1689     u16 sp_size;
1690 
1691     /* Try the most easy case. */
1692     e = fnd->level - 1 == level ? fnd->de[level] : NULL;
1693     e = hdr_insert_de(indx, hdr1, new_de, e, ctx);
1694     fnd->de[level] = e;
1695     if (e) {
1696         /* Just write updated index into disk. */
1697         indx_write(indx, ni, n1, 0);
1698         return 0;
1699     }
1700 
1701     /*
1702      * No space to insert into buffer. Split it.
1703      * To split we:
1704      *  - Save split point ('cause index buffers will be changed)
1705      * - Allocate NewBuffer and copy all entries <= sp into new buffer
1706      * - Remove all entries (sp including) from TargetBuffer
1707      * - Insert NewEntry into left or right buffer (depending on sp <=>
1708      *     NewEntry)
1709      * - Insert sp into parent buffer (or root)
1710      * - Make sp a parent for new buffer
1711      */
1712     sp = hdr_find_split(hdr1);
1713     if (!sp)
1714         return -EINVAL;
1715 
1716     sp_size = le16_to_cpu(sp->size);
1717     up_e = kmalloc(sp_size + sizeof(u64), GFP_NOFS);
1718     if (!up_e)
1719         return -ENOMEM;
1720     memcpy(up_e, sp, sp_size);
1721 
1722     if (!hdr1->flags) {
1723         up_e->flags |= NTFS_IE_HAS_SUBNODES;
1724         up_e->size = cpu_to_le16(sp_size + sizeof(u64));
1725         sub_vbn = NULL;
1726     } else {
1727         t_vbn = de_get_vbn_le(up_e);
1728         sub_vbn = &t_vbn;
1729     }
1730 
1731     /* Allocate on disk a new index allocation buffer. */
1732     err = indx_add_allocate(indx, ni, &new_vbn);
1733     if (err)
1734         goto out;
1735 
1736     /* Allocate and format memory a new index buffer. */
1737     n2 = indx_new(indx, ni, new_vbn, sub_vbn);
1738     if (IS_ERR(n2)) {
1739         err = PTR_ERR(n2);
1740         goto out;
1741     }
1742 
1743     hdr2 = &n2->index->ihdr;
1744 
1745     /* Make sp a parent for new buffer. */
1746     de_set_vbn(up_e, new_vbn);
1747 
1748     /* Copy all the entries <= sp into the new buffer. */
1749     de_t = hdr_first_de(hdr1);
1750     to_copy = PtrOffset(de_t, sp);
1751     hdr_insert_head(hdr2, de_t, to_copy);
1752 
1753     /* Remove all entries (sp including) from hdr1. */
1754     used = le32_to_cpu(hdr1->used) - to_copy - sp_size;
1755     memmove(de_t, Add2Ptr(sp, sp_size), used - le32_to_cpu(hdr1->de_off));
1756     hdr1->used = cpu_to_le32(used);
1757 
1758     /*
1759      * Insert new entry into left or right buffer
1760      * (depending on sp <=> new_de).
1761      */
1762     hdr_insert_de(indx,
1763               (*indx->cmp)(new_de + 1, le16_to_cpu(new_de->key_size),
1764                    up_e + 1, le16_to_cpu(up_e->key_size),
1765                    ctx) < 0
1766                   ? hdr2
1767                   : hdr1,
1768               new_de, NULL, ctx);
1769 
1770     indx_mark_used(indx, ni, new_vbn >> indx->idx2vbn_bits);
1771 
1772     indx_write(indx, ni, n1, 0);
1773     indx_write(indx, ni, n2, 0);
1774 
1775     put_indx_node(n2);
1776 
1777     /*
1778      * We've finished splitting everybody, so we are ready to
1779      * insert the promoted entry into the parent.
1780      */
1781     if (!level) {
1782         /* Insert in root. */
1783         err = indx_insert_into_root(indx, ni, up_e, NULL, ctx, fnd, 0);
1784         if (err)
1785             goto out;
1786     } else {
1787         /*
1788          * The target buffer's parent is another index buffer.
1789          * TODO: Remove recursion.
1790          */
1791         err = indx_insert_into_buffer(indx, ni, root, up_e, ctx,
1792                           level - 1, fnd);
1793         if (err)
1794             goto out;
1795     }
1796 
1797 out:
1798     kfree(up_e);
1799 
1800     return err;
1801 }
1802 
1803 /*
1804  * indx_insert_entry - Insert new entry into index.
1805  *
1806  * @undo - True if we undoing previous remove.
1807  */
1808 int indx_insert_entry(struct ntfs_index *indx, struct ntfs_inode *ni,
1809               const struct NTFS_DE *new_de, const void *ctx,
1810               struct ntfs_fnd *fnd, bool undo)
1811 {
1812     int err;
1813     int diff;
1814     struct NTFS_DE *e;
1815     struct ntfs_fnd *fnd_a = NULL;
1816     struct INDEX_ROOT *root;
1817 
1818     if (!fnd) {
1819         fnd_a = fnd_get();
1820         if (!fnd_a) {
1821             err = -ENOMEM;
1822             goto out1;
1823         }
1824         fnd = fnd_a;
1825     }
1826 
1827     root = indx_get_root(indx, ni, NULL, NULL);
1828     if (!root) {
1829         err = -EINVAL;
1830         goto out;
1831     }
1832 
1833     if (fnd_is_empty(fnd)) {
1834         /*
1835          * Find the spot the tree where we want to
1836          * insert the new entry.
1837          */
1838         err = indx_find(indx, ni, root, new_de + 1,
1839                 le16_to_cpu(new_de->key_size), ctx, &diff, &e,
1840                 fnd);
1841         if (err)
1842             goto out;
1843 
1844         if (!diff) {
1845             err = -EEXIST;
1846             goto out;
1847         }
1848     }
1849 
1850     if (!fnd->level) {
1851         /*
1852          * The root is also a leaf, so we'll insert the
1853          * new entry into it.
1854          */
1855         err = indx_insert_into_root(indx, ni, new_de, fnd->root_de, ctx,
1856                         fnd, undo);
1857         if (err)
1858             goto out;
1859     } else {
1860         /*
1861          * Found a leaf buffer, so we'll insert the new entry into it.
1862          */
1863         err = indx_insert_into_buffer(indx, ni, root, new_de, ctx,
1864                           fnd->level - 1, fnd);
1865         if (err)
1866             goto out;
1867     }
1868 
1869 out:
1870     fnd_put(fnd_a);
1871 out1:
1872     return err;
1873 }
1874 
1875 /*
1876  * indx_find_buffer - Locate a buffer from the tree.
1877  */
1878 static struct indx_node *indx_find_buffer(struct ntfs_index *indx,
1879                       struct ntfs_inode *ni,
1880                       const struct INDEX_ROOT *root,
1881                       __le64 vbn, struct indx_node *n)
1882 {
1883     int err;
1884     const struct NTFS_DE *e;
1885     struct indx_node *r;
1886     const struct INDEX_HDR *hdr = n ? &n->index->ihdr : &root->ihdr;
1887 
1888     /* Step 1: Scan one level. */
1889     for (e = hdr_first_de(hdr);; e = hdr_next_de(hdr, e)) {
1890         if (!e)
1891             return ERR_PTR(-EINVAL);
1892 
1893         if (de_has_vcn(e) && vbn == de_get_vbn_le(e))
1894             return n;
1895 
1896         if (de_is_last(e))
1897             break;
1898     }
1899 
1900     /* Step2: Do recursion. */
1901     e = Add2Ptr(hdr, le32_to_cpu(hdr->de_off));
1902     for (;;) {
1903         if (de_has_vcn_ex(e)) {
1904             err = indx_read(indx, ni, de_get_vbn(e), &n);
1905             if (err)
1906                 return ERR_PTR(err);
1907 
1908             r = indx_find_buffer(indx, ni, root, vbn, n);
1909             if (r)
1910                 return r;
1911         }
1912 
1913         if (de_is_last(e))
1914             break;
1915 
1916         e = Add2Ptr(e, le16_to_cpu(e->size));
1917     }
1918 
1919     return NULL;
1920 }
1921 
1922 /*
1923  * indx_shrink - Deallocate unused tail indexes.
1924  */
1925 static int indx_shrink(struct ntfs_index *indx, struct ntfs_inode *ni,
1926                size_t bit)
1927 {
1928     int err = 0;
1929     u64 bpb, new_data;
1930     size_t nbits;
1931     struct ATTRIB *b;
1932     struct ATTR_LIST_ENTRY *le = NULL;
1933     const struct INDEX_NAMES *in = &s_index_names[indx->type];
1934 
1935     b = ni_find_attr(ni, NULL, &le, ATTR_BITMAP, in->name, in->name_len,
1936              NULL, NULL);
1937 
1938     if (!b)
1939         return -ENOENT;
1940 
1941     if (!b->non_res) {
1942         unsigned long pos;
1943         const unsigned long *bm = resident_data(b);
1944 
1945         nbits = (size_t)le32_to_cpu(b->res.data_size) * 8;
1946 
1947         if (bit >= nbits)
1948             return 0;
1949 
1950         pos = find_next_bit(bm, nbits, bit);
1951         if (pos < nbits)
1952             return 0;
1953     } else {
1954         size_t used = MINUS_ONE_T;
1955 
1956         nbits = le64_to_cpu(b->nres.data_size) * 8;
1957 
1958         if (bit >= nbits)
1959             return 0;
1960 
1961         err = scan_nres_bitmap(ni, b, indx, bit, &scan_for_used, &used);
1962         if (err)
1963             return err;
1964 
1965         if (used != MINUS_ONE_T)
1966             return 0;
1967     }
1968 
1969     new_data = (u64)bit << indx->index_bits;
1970 
1971     err = attr_set_size(ni, ATTR_ALLOC, in->name, in->name_len,
1972                 &indx->alloc_run, new_data, &new_data, false, NULL);
1973     if (err)
1974         return err;
1975 
1976     bpb = bitmap_size(bit);
1977     if (bpb * 8 == nbits)
1978         return 0;
1979 
1980     err = attr_set_size(ni, ATTR_BITMAP, in->name, in->name_len,
1981                 &indx->bitmap_run, bpb, &bpb, false, NULL);
1982 
1983     return err;
1984 }
1985 
1986 static int indx_free_children(struct ntfs_index *indx, struct ntfs_inode *ni,
1987                   const struct NTFS_DE *e, bool trim)
1988 {
1989     int err;
1990     struct indx_node *n = NULL;
1991     struct INDEX_HDR *hdr;
1992     CLST vbn = de_get_vbn(e);
1993     size_t i;
1994 
1995     err = indx_read(indx, ni, vbn, &n);
1996     if (err)
1997         return err;
1998 
1999     hdr = &n->index->ihdr;
2000     /* First, recurse into the children, if any. */
2001     if (hdr_has_subnode(hdr)) {
2002         for (e = hdr_first_de(hdr); e; e = hdr_next_de(hdr, e)) {
2003             indx_free_children(indx, ni, e, false);
2004             if (de_is_last(e))
2005                 break;
2006         }
2007     }
2008 
2009     put_indx_node(n);
2010 
2011     i = vbn >> indx->idx2vbn_bits;
2012     /*
2013      * We've gotten rid of the children; add this buffer to the free list.
2014      */
2015     indx_mark_free(indx, ni, i);
2016 
2017     if (!trim)
2018         return 0;
2019 
2020     /*
2021      * If there are no used indexes after current free index
2022      * then we can truncate allocation and bitmap.
2023      * Use bitmap to estimate the case.
2024      */
2025     indx_shrink(indx, ni, i + 1);
2026     return 0;
2027 }
2028 
2029 /*
2030  * indx_get_entry_to_replace
2031  *
2032  * Find a replacement entry for a deleted entry.
2033  * Always returns a node entry:
2034  * NTFS_IE_HAS_SUBNODES is set the flags and the size includes the sub_vcn.
2035  */
2036 static int indx_get_entry_to_replace(struct ntfs_index *indx,
2037                      struct ntfs_inode *ni,
2038                      const struct NTFS_DE *de_next,
2039                      struct NTFS_DE **de_to_replace,
2040                      struct ntfs_fnd *fnd)
2041 {
2042     int err;
2043     int level = -1;
2044     CLST vbn;
2045     struct NTFS_DE *e, *te, *re;
2046     struct indx_node *n;
2047     struct INDEX_BUFFER *ib;
2048 
2049     *de_to_replace = NULL;
2050 
2051     /* Find first leaf entry down from de_next. */
2052     vbn = de_get_vbn(de_next);
2053     for (;;) {
2054         n = NULL;
2055         err = indx_read(indx, ni, vbn, &n);
2056         if (err)
2057             goto out;
2058 
2059         e = hdr_first_de(&n->index->ihdr);
2060         fnd_push(fnd, n, e);
2061 
2062         if (!de_is_last(e)) {
2063             /*
2064              * This buffer is non-empty, so its first entry
2065              * could be used as the replacement entry.
2066              */
2067             level = fnd->level - 1;
2068         }
2069 
2070         if (!de_has_vcn(e))
2071             break;
2072 
2073         /* This buffer is a node. Continue to go down. */
2074         vbn = de_get_vbn(e);
2075     }
2076 
2077     if (level == -1)
2078         goto out;
2079 
2080     n = fnd->nodes[level];
2081     te = hdr_first_de(&n->index->ihdr);
2082     /* Copy the candidate entry into the replacement entry buffer. */
2083     re = kmalloc(le16_to_cpu(te->size) + sizeof(u64), GFP_NOFS);
2084     if (!re) {
2085         err = -ENOMEM;
2086         goto out;
2087     }
2088 
2089     *de_to_replace = re;
2090     memcpy(re, te, le16_to_cpu(te->size));
2091 
2092     if (!de_has_vcn(re)) {
2093         /*
2094          * The replacement entry we found doesn't have a sub_vcn.
2095          * increase its size to hold one.
2096          */
2097         le16_add_cpu(&re->size, sizeof(u64));
2098         re->flags |= NTFS_IE_HAS_SUBNODES;
2099     } else {
2100         /*
2101          * The replacement entry we found was a node entry, which
2102          * means that all its child buffers are empty. Return them
2103          * to the free pool.
2104          */
2105         indx_free_children(indx, ni, te, true);
2106     }
2107 
2108     /*
2109      * Expunge the replacement entry from its former location,
2110      * and then write that buffer.
2111      */
2112     ib = n->index;
2113     e = hdr_delete_de(&ib->ihdr, te);
2114 
2115     fnd->de[level] = e;
2116     indx_write(indx, ni, n, 0);
2117 
2118     /* Check to see if this action created an empty leaf. */
2119     if (ib_is_leaf(ib) && ib_is_empty(ib))
2120         return 0;
2121 
2122 out:
2123     fnd_clear(fnd);
2124     return err;
2125 }
2126 
2127 /*
2128  * indx_delete_entry - Delete an entry from the index.
2129  */
2130 int indx_delete_entry(struct ntfs_index *indx, struct ntfs_inode *ni,
2131               const void *key, u32 key_len, const void *ctx)
2132 {
2133     int err, diff;
2134     struct INDEX_ROOT *root;
2135     struct INDEX_HDR *hdr;
2136     struct ntfs_fnd *fnd, *fnd2;
2137     struct INDEX_BUFFER *ib;
2138     struct NTFS_DE *e, *re, *next, *prev, *me;
2139     struct indx_node *n, *n2d = NULL;
2140     __le64 sub_vbn;
2141     int level, level2;
2142     struct ATTRIB *attr;
2143     struct mft_inode *mi;
2144     u32 e_size, root_size, new_root_size;
2145     size_t trim_bit;
2146     const struct INDEX_NAMES *in;
2147 
2148     fnd = fnd_get();
2149     if (!fnd) {
2150         err = -ENOMEM;
2151         goto out2;
2152     }
2153 
2154     fnd2 = fnd_get();
2155     if (!fnd2) {
2156         err = -ENOMEM;
2157         goto out1;
2158     }
2159 
2160     root = indx_get_root(indx, ni, &attr, &mi);
2161     if (!root) {
2162         err = -EINVAL;
2163         goto out;
2164     }
2165 
2166     /* Locate the entry to remove. */
2167     err = indx_find(indx, ni, root, key, key_len, ctx, &diff, &e, fnd);
2168     if (err)
2169         goto out;
2170 
2171     if (!e || diff) {
2172         err = -ENOENT;
2173         goto out;
2174     }
2175 
2176     level = fnd->level;
2177 
2178     if (level) {
2179         n = fnd->nodes[level - 1];
2180         e = fnd->de[level - 1];
2181         ib = n->index;
2182         hdr = &ib->ihdr;
2183     } else {
2184         hdr = &root->ihdr;
2185         e = fnd->root_de;
2186         n = NULL;
2187     }
2188 
2189     e_size = le16_to_cpu(e->size);
2190 
2191     if (!de_has_vcn_ex(e)) {
2192         /* The entry to delete is a leaf, so we can just rip it out. */
2193         hdr_delete_de(hdr, e);
2194 
2195         if (!level) {
2196             hdr->total = hdr->used;
2197 
2198             /* Shrink resident root attribute. */
2199             mi_resize_attr(mi, attr, 0 - e_size);
2200             goto out;
2201         }
2202 
2203         indx_write(indx, ni, n, 0);
2204 
2205         /*
2206          * Check to see if removing that entry made
2207          * the leaf empty.
2208          */
2209         if (ib_is_leaf(ib) && ib_is_empty(ib)) {
2210             fnd_pop(fnd);
2211             fnd_push(fnd2, n, e);
2212         }
2213     } else {
2214         /*
2215          * The entry we wish to delete is a node buffer, so we
2216          * have to find a replacement for it.
2217          */
2218         next = de_get_next(e);
2219 
2220         err = indx_get_entry_to_replace(indx, ni, next, &re, fnd2);
2221         if (err)
2222             goto out;
2223 
2224         if (re) {
2225             de_set_vbn_le(re, de_get_vbn_le(e));
2226             hdr_delete_de(hdr, e);
2227 
2228             err = level ? indx_insert_into_buffer(indx, ni, root,
2229                                   re, ctx,
2230                                   fnd->level - 1,
2231                                   fnd)
2232                     : indx_insert_into_root(indx, ni, re, e,
2233                                 ctx, fnd, 0);
2234             kfree(re);
2235 
2236             if (err)
2237                 goto out;
2238         } else {
2239             /*
2240              * There is no replacement for the current entry.
2241              * This means that the subtree rooted at its node
2242              * is empty, and can be deleted, which turn means
2243              * that the node can just inherit the deleted
2244              * entry sub_vcn.
2245              */
2246             indx_free_children(indx, ni, next, true);
2247 
2248             de_set_vbn_le(next, de_get_vbn_le(e));
2249             hdr_delete_de(hdr, e);
2250             if (level) {
2251                 indx_write(indx, ni, n, 0);
2252             } else {
2253                 hdr->total = hdr->used;
2254 
2255                 /* Shrink resident root attribute. */
2256                 mi_resize_attr(mi, attr, 0 - e_size);
2257             }
2258         }
2259     }
2260 
2261     /* Delete a branch of tree. */
2262     if (!fnd2 || !fnd2->level)
2263         goto out;
2264 
2265     /* Reinit root 'cause it can be changed. */
2266     root = indx_get_root(indx, ni, &attr, &mi);
2267     if (!root) {
2268         err = -EINVAL;
2269         goto out;
2270     }
2271 
2272     n2d = NULL;
2273     sub_vbn = fnd2->nodes[0]->index->vbn;
2274     level2 = 0;
2275     level = fnd->level;
2276 
2277     hdr = level ? &fnd->nodes[level - 1]->index->ihdr : &root->ihdr;
2278 
2279     /* Scan current level. */
2280     for (e = hdr_first_de(hdr);; e = hdr_next_de(hdr, e)) {
2281         if (!e) {
2282             err = -EINVAL;
2283             goto out;
2284         }
2285 
2286         if (de_has_vcn(e) && sub_vbn == de_get_vbn_le(e))
2287             break;
2288 
2289         if (de_is_last(e)) {
2290             e = NULL;
2291             break;
2292         }
2293     }
2294 
2295     if (!e) {
2296         /* Do slow search from root. */
2297         struct indx_node *in;
2298 
2299         fnd_clear(fnd);
2300 
2301         in = indx_find_buffer(indx, ni, root, sub_vbn, NULL);
2302         if (IS_ERR(in)) {
2303             err = PTR_ERR(in);
2304             goto out;
2305         }
2306 
2307         if (in)
2308             fnd_push(fnd, in, NULL);
2309     }
2310 
2311     /* Merge fnd2 -> fnd. */
2312     for (level = 0; level < fnd2->level; level++) {
2313         fnd_push(fnd, fnd2->nodes[level], fnd2->de[level]);
2314         fnd2->nodes[level] = NULL;
2315     }
2316     fnd2->level = 0;
2317 
2318     hdr = NULL;
2319     for (level = fnd->level; level; level--) {
2320         struct indx_node *in = fnd->nodes[level - 1];
2321 
2322         ib = in->index;
2323         if (ib_is_empty(ib)) {
2324             sub_vbn = ib->vbn;
2325         } else {
2326             hdr = &ib->ihdr;
2327             n2d = in;
2328             level2 = level;
2329             break;
2330         }
2331     }
2332 
2333     if (!hdr)
2334         hdr = &root->ihdr;
2335 
2336     e = hdr_first_de(hdr);
2337     if (!e) {
2338         err = -EINVAL;
2339         goto out;
2340     }
2341 
2342     if (hdr != &root->ihdr || !de_is_last(e)) {
2343         prev = NULL;
2344         while (!de_is_last(e)) {
2345             if (de_has_vcn(e) && sub_vbn == de_get_vbn_le(e))
2346                 break;
2347             prev = e;
2348             e = hdr_next_de(hdr, e);
2349             if (!e) {
2350                 err = -EINVAL;
2351                 goto out;
2352             }
2353         }
2354 
2355         if (sub_vbn != de_get_vbn_le(e)) {
2356             /*
2357              * Didn't find the parent entry, although this buffer
2358              * is the parent trail. Something is corrupt.
2359              */
2360             err = -EINVAL;
2361             goto out;
2362         }
2363 
2364         if (de_is_last(e)) {
2365             /*
2366              * Since we can't remove the end entry, we'll remove
2367              * its predecessor instead. This means we have to
2368              * transfer the predecessor's sub_vcn to the end entry.
2369              * Note: This index block is not empty, so the
2370              * predecessor must exist.
2371              */
2372             if (!prev) {
2373                 err = -EINVAL;
2374                 goto out;
2375             }
2376 
2377             if (de_has_vcn(prev)) {
2378                 de_set_vbn_le(e, de_get_vbn_le(prev));
2379             } else if (de_has_vcn(e)) {
2380                 le16_sub_cpu(&e->size, sizeof(u64));
2381                 e->flags &= ~NTFS_IE_HAS_SUBNODES;
2382                 le32_sub_cpu(&hdr->used, sizeof(u64));
2383             }
2384             e = prev;
2385         }
2386 
2387         /*
2388          * Copy the current entry into a temporary buffer (stripping
2389          * off its down-pointer, if any) and delete it from the current
2390          * buffer or root, as appropriate.
2391          */
2392         e_size = le16_to_cpu(e->size);
2393         me = kmemdup(e, e_size, GFP_NOFS);
2394         if (!me) {
2395             err = -ENOMEM;
2396             goto out;
2397         }
2398 
2399         if (de_has_vcn(me)) {
2400             me->flags &= ~NTFS_IE_HAS_SUBNODES;
2401             le16_sub_cpu(&me->size, sizeof(u64));
2402         }
2403 
2404         hdr_delete_de(hdr, e);
2405 
2406         if (hdr == &root->ihdr) {
2407             level = 0;
2408             hdr->total = hdr->used;
2409 
2410             /* Shrink resident root attribute. */
2411             mi_resize_attr(mi, attr, 0 - e_size);
2412         } else {
2413             indx_write(indx, ni, n2d, 0);
2414             level = level2;
2415         }
2416 
2417         /* Mark unused buffers as free. */
2418         trim_bit = -1;
2419         for (; level < fnd->level; level++) {
2420             ib = fnd->nodes[level]->index;
2421             if (ib_is_empty(ib)) {
2422                 size_t k = le64_to_cpu(ib->vbn) >>
2423                        indx->idx2vbn_bits;
2424 
2425                 indx_mark_free(indx, ni, k);
2426                 if (k < trim_bit)
2427                     trim_bit = k;
2428             }
2429         }
2430 
2431         fnd_clear(fnd);
2432         /*fnd->root_de = NULL;*/
2433 
2434         /*
2435          * Re-insert the entry into the tree.
2436          * Find the spot the tree where we want to insert the new entry.
2437          */
2438         err = indx_insert_entry(indx, ni, me, ctx, fnd, 0);
2439         kfree(me);
2440         if (err)
2441             goto out;
2442 
2443         if (trim_bit != -1)
2444             indx_shrink(indx, ni, trim_bit);
2445     } else {
2446         /*
2447          * This tree needs to be collapsed down to an empty root.
2448          * Recreate the index root as an empty leaf and free all
2449          * the bits the index allocation bitmap.
2450          */
2451         fnd_clear(fnd);
2452         fnd_clear(fnd2);
2453 
2454         in = &s_index_names[indx->type];
2455 
2456         err = attr_set_size(ni, ATTR_ALLOC, in->name, in->name_len,
2457                     &indx->alloc_run, 0, NULL, false, NULL);
2458         err = ni_remove_attr(ni, ATTR_ALLOC, in->name, in->name_len,
2459                      false, NULL);
2460         run_close(&indx->alloc_run);
2461 
2462         err = attr_set_size(ni, ATTR_BITMAP, in->name, in->name_len,
2463                     &indx->bitmap_run, 0, NULL, false, NULL);
2464         err = ni_remove_attr(ni, ATTR_BITMAP, in->name, in->name_len,
2465                      false, NULL);
2466         run_close(&indx->bitmap_run);
2467 
2468         root = indx_get_root(indx, ni, &attr, &mi);
2469         if (!root) {
2470             err = -EINVAL;
2471             goto out;
2472         }
2473 
2474         root_size = le32_to_cpu(attr->res.data_size);
2475         new_root_size =
2476             sizeof(struct INDEX_ROOT) + sizeof(struct NTFS_DE);
2477 
2478         if (new_root_size != root_size &&
2479             !mi_resize_attr(mi, attr, new_root_size - root_size)) {
2480             err = -EINVAL;
2481             goto out;
2482         }
2483 
2484         /* Fill first entry. */
2485         e = (struct NTFS_DE *)(root + 1);
2486         e->ref.low = 0;
2487         e->ref.high = 0;
2488         e->ref.seq = 0;
2489         e->size = cpu_to_le16(sizeof(struct NTFS_DE));
2490         e->flags = NTFS_IE_LAST; // 0x02
2491         e->key_size = 0;
2492         e->res = 0;
2493 
2494         hdr = &root->ihdr;
2495         hdr->flags = 0;
2496         hdr->used = hdr->total = cpu_to_le32(
2497             new_root_size - offsetof(struct INDEX_ROOT, ihdr));
2498         mi->dirty = true;
2499     }
2500 
2501 out:
2502     fnd_put(fnd2);
2503 out1:
2504     fnd_put(fnd);
2505 out2:
2506     return err;
2507 }
2508 
2509 /*
2510  * Update duplicated information in directory entry
2511  * 'dup' - info from MFT record
2512  */
2513 int indx_update_dup(struct ntfs_inode *ni, struct ntfs_sb_info *sbi,
2514             const struct ATTR_FILE_NAME *fname,
2515             const struct NTFS_DUP_INFO *dup, int sync)
2516 {
2517     int err, diff;
2518     struct NTFS_DE *e = NULL;
2519     struct ATTR_FILE_NAME *e_fname;
2520     struct ntfs_fnd *fnd;
2521     struct INDEX_ROOT *root;
2522     struct mft_inode *mi;
2523     struct ntfs_index *indx = &ni->dir;
2524 
2525     fnd = fnd_get();
2526     if (!fnd)
2527         return -ENOMEM;
2528 
2529     root = indx_get_root(indx, ni, NULL, &mi);
2530     if (!root) {
2531         err = -EINVAL;
2532         goto out;
2533     }
2534 
2535     /* Find entry in directory. */
2536     err = indx_find(indx, ni, root, fname, fname_full_size(fname), sbi,
2537             &diff, &e, fnd);
2538     if (err)
2539         goto out;
2540 
2541     if (!e) {
2542         err = -EINVAL;
2543         goto out;
2544     }
2545 
2546     if (diff) {
2547         err = -EINVAL;
2548         goto out;
2549     }
2550 
2551     e_fname = (struct ATTR_FILE_NAME *)(e + 1);
2552 
2553     if (!memcmp(&e_fname->dup, dup, sizeof(*dup))) {
2554         /*
2555          * Nothing to update in index! Try to avoid this call.
2556          */
2557         goto out;
2558     }
2559 
2560     memcpy(&e_fname->dup, dup, sizeof(*dup));
2561 
2562     if (fnd->level) {
2563         /* Directory entry in index. */
2564         err = indx_write(indx, ni, fnd->nodes[fnd->level - 1], sync);
2565     } else {
2566         /* Directory entry in directory MFT record. */
2567         mi->dirty = true;
2568         if (sync)
2569             err = mi_write(mi, 1);
2570         else
2571             mark_inode_dirty(&ni->vfs_inode);
2572     }
2573 
2574 out:
2575     fnd_put(fnd);
2576     return err;
2577 }