Skip to content

Commit 3a2141b

Browse files
Saphereyeaalexandrovich
authored andcommitted
fs/ntfs3: resolve compare function in public index APIs
Previously the comparator was stored in struct ntfs_index and used by low-level helpers such as hdr_find_e(). This creates a dependency on index state in private helpers. Resolve the compare function in the public index APIs and pass it explicitly to internal helpers. This should make the ownership of the comparator explicit and keeps low-level index code independent of index-root policy. This also resolves the TODO comment about dropping the stored comparator from struct ntfs_index. Signed-off-by: Adarsh Das <adarshdas950@gmail.com> Signed-off-by: Konstantin Komarov <almaz.alexandrovich@paragon-software.com>
1 parent e10e72f commit 3a2141b

2 files changed

Lines changed: 49 additions & 30 deletions

File tree

fs/ntfs3/index.c

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -714,10 +714,10 @@ static bool fnd_is_empty(struct ntfs_fnd *fnd)
714714
*/
715715
static struct NTFS_DE *hdr_find_e(const struct ntfs_index *indx,
716716
const struct INDEX_HDR *hdr, const void *key,
717-
size_t key_len, const void *ctx, int *diff)
717+
size_t key_len, const void *ctx, int *diff,
718+
NTFS_CMP_FUNC cmp)
718719
{
719720
struct NTFS_DE *e, *found = NULL;
720-
NTFS_CMP_FUNC cmp = indx->cmp;
721721
int min_idx = 0, mid_idx, max_idx = 0;
722722
int diff2;
723723
int table_size = 8;
@@ -727,9 +727,6 @@ static struct NTFS_DE *hdr_find_e(const struct ntfs_index *indx,
727727
u32 total = le32_to_cpu(hdr->total);
728728
u16 offs[128];
729729

730-
if (unlikely(!cmp))
731-
return NULL;
732-
733730
fill_table:
734731
if (end > total)
735732
return NULL;
@@ -800,7 +797,8 @@ static struct NTFS_DE *hdr_find_e(const struct ntfs_index *indx,
800797
static struct NTFS_DE *hdr_insert_de(const struct ntfs_index *indx,
801798
struct INDEX_HDR *hdr,
802799
const struct NTFS_DE *de,
803-
struct NTFS_DE *before, const void *ctx)
800+
struct NTFS_DE *before, const void *ctx,
801+
NTFS_CMP_FUNC cmp)
804802
{
805803
int diff;
806804
size_t off = PtrOffset(hdr, before);
@@ -823,7 +821,7 @@ static struct NTFS_DE *hdr_insert_de(const struct ntfs_index *indx,
823821
}
824822
/* No insert point is applied. Get it manually. */
825823
before = hdr_find_e(indx, hdr, de + 1, le16_to_cpu(de->key_size), ctx,
826-
&diff);
824+
&diff, cmp);
827825
if (!before)
828826
return NULL;
829827
off = PtrOffset(hdr, before);
@@ -915,10 +913,6 @@ int indx_init(struct ntfs_index *indx, struct ntfs_sb_info *sbi,
915913

916914
init_rwsem(&indx->run_lock);
917915

918-
indx->cmp = get_cmp_func(root);
919-
if (!indx->cmp)
920-
goto out;
921-
922916
return 0;
923917

924918
out:
@@ -1141,6 +1135,7 @@ int indx_find(struct ntfs_index *indx, struct ntfs_inode *ni,
11411135
int err;
11421136
struct NTFS_DE *e;
11431137
struct indx_node *node;
1138+
NTFS_CMP_FUNC cmp;
11441139

11451140
if (!root)
11461141
root = indx_get_root(&ni->dir, ni, NULL, NULL);
@@ -1150,10 +1145,16 @@ int indx_find(struct ntfs_index *indx, struct ntfs_inode *ni,
11501145
return -EINVAL;
11511146
}
11521147

1148+
cmp = get_cmp_func(root);
1149+
if (unlikely(!cmp)) {
1150+
WARN_ON_ONCE(1);
1151+
return -EINVAL;
1152+
}
1153+
11531154
/* Check cache. */
11541155
e = fnd->level ? fnd->de[fnd->level - 1] : fnd->root_de;
11551156
if (e && !de_is_last(e) &&
1156-
!(*indx->cmp)(key, key_len, e + 1, le16_to_cpu(e->key_size), ctx)) {
1157+
!(*cmp)(key, key_len, e + 1, le16_to_cpu(e->key_size), ctx)) {
11571158
*entry = e;
11581159
*diff = 0;
11591160
return 0;
@@ -1163,7 +1164,7 @@ int indx_find(struct ntfs_index *indx, struct ntfs_inode *ni,
11631164
fnd_clear(fnd);
11641165

11651166
/* Lookup entry that is <= to the search value. */
1166-
e = hdr_find_e(indx, &root->ihdr, key, key_len, ctx, diff);
1167+
e = hdr_find_e(indx, &root->ihdr, key, key_len, ctx, diff, cmp);
11671168
if (!e)
11681169
return -EINVAL;
11691170

@@ -1183,7 +1184,7 @@ int indx_find(struct ntfs_index *indx, struct ntfs_inode *ni,
11831184

11841185
/* Lookup entry that is <= to the search value. */
11851186
e = hdr_find_e(indx, &node->index->ihdr, key, key_len, ctx,
1186-
diff);
1187+
diff, cmp);
11871188
if (!e) {
11881189
put_indx_node(node);
11891190
return -EINVAL;
@@ -1585,7 +1586,7 @@ static int indx_add_allocate(struct ntfs_index *indx, struct ntfs_inode *ni,
15851586
static int indx_insert_into_root(struct ntfs_index *indx, struct ntfs_inode *ni,
15861587
const struct NTFS_DE *new_de,
15871588
struct NTFS_DE *root_de, const void *ctx,
1588-
struct ntfs_fnd *fnd, bool undo)
1589+
struct ntfs_fnd *fnd, bool undo, NTFS_CMP_FUNC cmp)
15891590
{
15901591
int err = 0;
15911592
struct NTFS_DE *e, *e0, *re;
@@ -1626,7 +1627,7 @@ static int indx_insert_into_root(struct ntfs_index *indx, struct ntfs_inode *ni,
16261627
if ((undo || asize + ds_root < sbi->max_bytes_per_attr) &&
16271628
mi_resize_attr(mi, attr, ds_root)) {
16281629
hdr->total = cpu_to_le32(hdr_total + ds_root);
1629-
e = hdr_insert_de(indx, hdr, new_de, root_de, ctx);
1630+
e = hdr_insert_de(indx, hdr, new_de, root_de, ctx, cmp);
16301631
WARN_ON(!e);
16311632
fnd_clear(fnd);
16321633
fnd->root_de = e;
@@ -1767,7 +1768,7 @@ static int indx_insert_into_root(struct ntfs_index *indx, struct ntfs_inode *ni,
17671768
* Now root is a parent for new index buffer.
17681769
* Insert NewEntry a new buffer.
17691770
*/
1770-
e = hdr_insert_de(indx, hdr, new_de, NULL, ctx);
1771+
e = hdr_insert_de(indx, hdr, new_de, NULL, ctx, cmp);
17711772
if (!e) {
17721773
err = -EINVAL;
17731774
goto out_put_n;
@@ -1797,7 +1798,7 @@ static int indx_insert_into_root(struct ntfs_index *indx, struct ntfs_inode *ni,
17971798
static int
17981799
indx_insert_into_buffer(struct ntfs_index *indx, struct ntfs_inode *ni,
17991800
struct INDEX_ROOT *root, const struct NTFS_DE *new_de,
1800-
const void *ctx, int level, struct ntfs_fnd *fnd)
1801+
const void *ctx, int level, struct ntfs_fnd *fnd, NTFS_CMP_FUNC cmp)
18011802
{
18021803
int err;
18031804
const struct NTFS_DE *sp;
@@ -1814,7 +1815,7 @@ indx_insert_into_buffer(struct ntfs_index *indx, struct ntfs_inode *ni,
18141815

18151816
/* Try the most easy case. */
18161817
e = fnd->level - 1 == level ? fnd->de[level] : NULL;
1817-
e = hdr_insert_de(indx, hdr1, new_de, e, ctx);
1818+
e = hdr_insert_de(indx, hdr1, new_de, e, ctx, cmp);
18181819
fnd->de[level] = e;
18191820
if (e) {
18201821
/* Just write updated index into disk. */
@@ -1891,12 +1892,12 @@ indx_insert_into_buffer(struct ntfs_index *indx, struct ntfs_inode *ni,
18911892
* (depending on sp <=> new_de).
18921893
*/
18931894
hdr_insert_de(indx,
1894-
(*indx->cmp)(new_de + 1, le16_to_cpu(new_de->key_size),
1895+
(*cmp)(new_de + 1, le16_to_cpu(new_de->key_size),
18951896
up_e + 1, le16_to_cpu(up_e->key_size),
18961897
ctx) < 0 ?
18971898
hdr2 :
18981899
hdr1,
1899-
new_de, NULL, ctx);
1900+
new_de, NULL, ctx, cmp);
19001901

19011902
indx_mark_used(indx, ni, new_vbn >> indx->idx2vbn_bits);
19021903

@@ -1911,14 +1912,14 @@ indx_insert_into_buffer(struct ntfs_index *indx, struct ntfs_inode *ni,
19111912
*/
19121913
if (!level) {
19131914
/* Insert in root. */
1914-
err = indx_insert_into_root(indx, ni, up_e, NULL, ctx, fnd, 0);
1915+
err = indx_insert_into_root(indx, ni, up_e, NULL, ctx, fnd, 0, cmp);
19151916
} else {
19161917
/*
19171918
* The target buffer's parent is another index buffer.
19181919
* TODO: Remove recursion.
19191920
*/
19201921
err = indx_insert_into_buffer(indx, ni, root, up_e, ctx,
1921-
level - 1, fnd);
1922+
level - 1, fnd, cmp);
19221923
}
19231924

19241925
if (err) {
@@ -1952,6 +1953,7 @@ int indx_insert_entry(struct ntfs_index *indx, struct ntfs_inode *ni,
19521953
struct NTFS_DE *e;
19531954
struct ntfs_fnd *fnd_a = NULL;
19541955
struct INDEX_ROOT *root;
1956+
NTFS_CMP_FUNC cmp;
19551957

19561958
if (!fnd) {
19571959
fnd_a = fnd_get();
@@ -1968,6 +1970,12 @@ int indx_insert_entry(struct ntfs_index *indx, struct ntfs_inode *ni,
19681970
goto out;
19691971
}
19701972

1973+
cmp = get_cmp_func(root);
1974+
if (unlikely(!cmp)) {
1975+
WARN_ON_ONCE(1);
1976+
return -EINVAL;
1977+
}
1978+
19711979
if (fnd_is_empty(fnd)) {
19721980
/*
19731981
* Find the spot the tree where we want to
@@ -1991,13 +1999,13 @@ int indx_insert_entry(struct ntfs_index *indx, struct ntfs_inode *ni,
19911999
* new entry into it.
19922000
*/
19932001
err = indx_insert_into_root(indx, ni, new_de, fnd->root_de, ctx,
1994-
fnd, undo);
2002+
fnd, undo, cmp);
19952003
} else {
19962004
/*
19972005
* Found a leaf buffer, so we'll insert the new entry into it.
19982006
*/
19992007
err = indx_insert_into_buffer(indx, ni, root, new_de, ctx,
2000-
fnd->level - 1, fnd);
2008+
fnd->level - 1, fnd, cmp);
20012009
}
20022010

20032011
indx->version += 1;
@@ -2291,6 +2299,7 @@ int indx_delete_entry(struct ntfs_index *indx, struct ntfs_inode *ni,
22912299
u32 e_size, root_size, new_root_size;
22922300
size_t trim_bit;
22932301
const struct INDEX_NAMES *in;
2302+
NTFS_CMP_FUNC cmp;
22942303

22952304
fnd = fnd_get();
22962305
if (!fnd) {
@@ -2310,6 +2319,12 @@ int indx_delete_entry(struct ntfs_index *indx, struct ntfs_inode *ni,
23102319
goto out;
23112320
}
23122321

2322+
cmp = get_cmp_func(root);
2323+
if (unlikely(!cmp)) {
2324+
WARN_ON_ONCE(1);
2325+
return -EINVAL;
2326+
}
2327+
23132328
/* Locate the entry to remove. */
23142329
err = indx_find(indx, ni, root, key, key_len, ctx, &diff, &e, fnd);
23152330
if (err)
@@ -2376,9 +2391,9 @@ int indx_delete_entry(struct ntfs_index *indx, struct ntfs_inode *ni,
23762391
err = level ? indx_insert_into_buffer(indx, ni, root,
23772392
re, ctx,
23782393
fnd->level - 1,
2379-
fnd) :
2394+
fnd, cmp) :
23802395
indx_insert_into_root(indx, ni, re, e,
2381-
ctx, fnd, 0);
2396+
ctx, fnd, 0, cmp);
23822397
kfree(re);
23832398

23842399
if (err)
@@ -2673,6 +2688,7 @@ int indx_update_dup(struct ntfs_inode *ni, struct ntfs_sb_info *sbi,
26732688
struct INDEX_ROOT *root;
26742689
struct mft_inode *mi;
26752690
struct ntfs_index *indx = &ni->dir;
2691+
NTFS_CMP_FUNC cmp;
26762692

26772693
fnd = fnd_get();
26782694
if (!fnd)
@@ -2684,6 +2700,12 @@ int indx_update_dup(struct ntfs_inode *ni, struct ntfs_sb_info *sbi,
26842700
goto out;
26852701
}
26862702

2703+
cmp = get_cmp_func(root);
2704+
if (unlikely(!cmp)) {
2705+
WARN_ON_ONCE(1);
2706+
return -EINVAL;
2707+
}
2708+
26872709
/* Find entry in directory. */
26882710
err = indx_find(indx, ni, root, fname, fname_full_size(fname), sbi,
26892711
&diff, &e, fnd);

fs/ntfs3/ntfs_fs.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,6 @@ struct ntfs_index {
196196
struct rw_semaphore run_lock;
197197
size_t version; /* increment each change */
198198

199-
/*TODO: Remove 'cmp'. */
200-
NTFS_CMP_FUNC cmp;
201-
202199
u8 index_bits; // log2(root->index_block_size)
203200
u8 idx2vbn_bits; // log2(root->index_block_clst)
204201
u8 vbn2vbo_bits; // index_block_size < cluster? 9 : cluster_bits

0 commit comments

Comments
 (0)