Skip to content

Commit 08811ba

Browse files
fs/ntfs3: Add ntfs_bitmap_weight_le function and refactoring
Added ntfs_bitmap_weight_le function. Changed argument types of bits/bitmap functions. Signed-off-by: Konstantin Komarov <[email protected]>
1 parent 095d8ce commit 08811ba

File tree

3 files changed

+63
-52
lines changed

3 files changed

+63
-52
lines changed

fs/ntfs3/bitfunc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static const u8 zero_mask[] = { 0xFF, 0xFE, 0xFC, 0xF8, 0xF0,
3030
*
3131
* Return: True if all bits [bit, bit+nbits) are zeros "0".
3232
*/
33-
bool are_bits_clear(const ulong *lmap, size_t bit, size_t nbits)
33+
bool are_bits_clear(const void *lmap, size_t bit, size_t nbits)
3434
{
3535
size_t pos = bit & 7;
3636
const u8 *map = (u8 *)lmap + (bit >> 3);
@@ -78,7 +78,7 @@ bool are_bits_clear(const ulong *lmap, size_t bit, size_t nbits)
7878
*
7979
* Return: True if all bits [bit, bit+nbits) are ones "1".
8080
*/
81-
bool are_bits_set(const ulong *lmap, size_t bit, size_t nbits)
81+
bool are_bits_set(const void *lmap, size_t bit, size_t nbits)
8282
{
8383
u8 mask;
8484
size_t pos = bit & 7;

fs/ntfs3/bitmap.c

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void ntfs3_exit_bitmap(void)
5959
*
6060
* Return: -1 if not found.
6161
*/
62-
static size_t wnd_scan(const ulong *buf, size_t wbit, u32 wpos, u32 wend,
62+
static size_t wnd_scan(const void *buf, size_t wbit, u32 wpos, u32 wend,
6363
size_t to_alloc, size_t *prev_tail, size_t *b_pos,
6464
size_t *b_len)
6565
{
@@ -504,7 +504,6 @@ static int wnd_rescan(struct wnd_bitmap *wnd)
504504
u8 cluster_bits = sbi->cluster_bits;
505505
u32 wbits = 8 * sb->s_blocksize;
506506
u32 used, frb;
507-
const ulong *buf;
508507
size_t wpos, wbit, iw, vbo;
509508
struct buffer_head *bh = NULL;
510509
CLST lcn, clen;
@@ -558,9 +557,7 @@ static int wnd_rescan(struct wnd_bitmap *wnd)
558557
goto out;
559558
}
560559

561-
buf = (ulong *)bh->b_data;
562-
563-
used = __bitmap_weight(buf, wbits);
560+
used = ntfs_bitmap_weight_le(bh->b_data, wbits);
564561
if (used < wbits) {
565562
frb = wbits - used;
566563
wnd->free_bits[iw] = frb;
@@ -574,7 +571,7 @@ static int wnd_rescan(struct wnd_bitmap *wnd)
574571
wbits = wnd->nbits - wbit;
575572

576573
do {
577-
used = find_next_zero_bit_le(buf, wbits, wpos);
574+
used = find_next_zero_bit_le(bh->b_data, wbits, wpos);
578575

579576
if (used > wpos && prev_tail) {
580577
wnd_add_free_ext(wnd, wbit + wpos - prev_tail,
@@ -590,7 +587,7 @@ static int wnd_rescan(struct wnd_bitmap *wnd)
590587
break;
591588
}
592589

593-
frb = find_next_bit_le(buf, wbits, wpos);
590+
frb = find_next_bit_le(bh->b_data, wbits, wpos);
594591
if (frb >= wbits) {
595592
/* Keep last free block. */
596593
prev_tail += frb - wpos;
@@ -718,7 +715,6 @@ int wnd_set_free(struct wnd_bitmap *wnd, size_t bit, size_t bits)
718715

719716
while (iw < wnd->nwnd && bits) {
720717
u32 tail, op;
721-
ulong *buf;
722718

723719
if (iw + 1 == wnd->nwnd)
724720
wbits = wnd->bits_last;
@@ -732,11 +728,9 @@ int wnd_set_free(struct wnd_bitmap *wnd, size_t bit, size_t bits)
732728
break;
733729
}
734730

735-
buf = (ulong *)bh->b_data;
736-
737731
lock_buffer(bh);
738732

739-
ntfs_bitmap_clear_le(buf, wbit, op);
733+
ntfs_bitmap_clear_le(bh->b_data, wbit, op);
740734

741735
wnd->free_bits[iw] += op;
742736

@@ -771,7 +765,6 @@ int wnd_set_used(struct wnd_bitmap *wnd, size_t bit, size_t bits)
771765

772766
while (iw < wnd->nwnd && bits) {
773767
u32 tail, op;
774-
ulong *buf;
775768

776769
if (unlikely(iw + 1 == wnd->nwnd))
777770
wbits = wnd->bits_last;
@@ -784,11 +777,10 @@ int wnd_set_used(struct wnd_bitmap *wnd, size_t bit, size_t bits)
784777
err = PTR_ERR(bh);
785778
break;
786779
}
787-
buf = (ulong *)bh->b_data;
788780

789781
lock_buffer(bh);
790782

791-
ntfs_bitmap_set_le(buf, wbit, op);
783+
ntfs_bitmap_set_le(bh->b_data, wbit, op);
792784
wnd->free_bits[iw] -= op;
793785

794786
set_buffer_uptodate(bh);
@@ -836,7 +828,7 @@ static bool wnd_is_free_hlp(struct wnd_bitmap *wnd, size_t bit, size_t bits)
836828
if (IS_ERR(bh))
837829
return false;
838830

839-
ret = are_bits_clear((ulong *)bh->b_data, wbit, op);
831+
ret = are_bits_clear(bh->b_data, wbit, op);
840832

841833
put_bh(bh);
842834
if (!ret)
@@ -928,7 +920,7 @@ bool wnd_is_used(struct wnd_bitmap *wnd, size_t bit, size_t bits)
928920
if (IS_ERR(bh))
929921
goto out;
930922

931-
ret = are_bits_set((ulong *)bh->b_data, wbit, op);
923+
ret = are_bits_set(bh->b_data, wbit, op);
932924
put_bh(bh);
933925
if (!ret)
934926
goto out;
@@ -959,7 +951,6 @@ size_t wnd_find(struct wnd_bitmap *wnd, size_t to_alloc, size_t hint,
959951
size_t fnd, max_alloc, b_len, b_pos;
960952
size_t iw, prev_tail, nwnd, wbit, ebit, zbit, zend;
961953
size_t to_alloc0 = to_alloc;
962-
const ulong *buf;
963954
const struct e_node *e;
964955
const struct rb_node *pr, *cr;
965956
u8 log2_bits;
@@ -1185,14 +1176,13 @@ size_t wnd_find(struct wnd_bitmap *wnd, size_t to_alloc, size_t hint,
11851176
continue;
11861177
}
11871178

1188-
buf = (ulong *)bh->b_data;
1189-
11901179
/* Scan range [wbit, zbit). */
11911180
if (wpos < wzbit) {
11921181
/* Scan range [wpos, zbit). */
1193-
fnd = wnd_scan(buf, wbit, wpos, wzbit,
1194-
to_alloc, &prev_tail,
1195-
&b_pos, &b_len);
1182+
fnd = wnd_scan(bh->b_data, wbit, wpos,
1183+
wzbit, to_alloc,
1184+
&prev_tail, &b_pos,
1185+
&b_len);
11961186
if (fnd != MINUS_ONE_T) {
11971187
put_bh(bh);
11981188
goto found;
@@ -1203,7 +1193,7 @@ size_t wnd_find(struct wnd_bitmap *wnd, size_t to_alloc, size_t hint,
12031193

12041194
/* Scan range [zend, ebit). */
12051195
if (wzend < wbits) {
1206-
fnd = wnd_scan(buf, wbit,
1196+
fnd = wnd_scan(bh->b_data, wbit,
12071197
max(wzend, wpos), wbits,
12081198
to_alloc, &prev_tail,
12091199
&b_pos, &b_len);
@@ -1242,11 +1232,9 @@ size_t wnd_find(struct wnd_bitmap *wnd, size_t to_alloc, size_t hint,
12421232
continue;
12431233
}
12441234

1245-
buf = (ulong *)bh->b_data;
1246-
12471235
/* Scan range [wpos, eBits). */
1248-
fnd = wnd_scan(buf, wbit, wpos, wbits, to_alloc, &prev_tail,
1249-
&b_pos, &b_len);
1236+
fnd = wnd_scan(bh->b_data, wbit, wpos, wbits, to_alloc,
1237+
&prev_tail, &b_pos, &b_len);
12501238
put_bh(bh);
12511239
if (fnd != MINUS_ONE_T)
12521240
goto found;
@@ -1344,7 +1332,6 @@ int wnd_extend(struct wnd_bitmap *wnd, size_t new_bits)
13441332
size_t frb;
13451333
u64 vbo, lbo, bytes;
13461334
struct buffer_head *bh;
1347-
ulong *buf;
13481335

13491336
if (iw + 1 == new_wnd)
13501337
wbits = new_last;
@@ -1361,10 +1348,9 @@ int wnd_extend(struct wnd_bitmap *wnd, size_t new_bits)
13611348
return -EIO;
13621349

13631350
lock_buffer(bh);
1364-
buf = (ulong *)bh->b_data;
13651351

1366-
ntfs_bitmap_clear_le(buf, b0, blocksize * 8 - b0);
1367-
frb = wbits - __bitmap_weight(buf, wbits);
1352+
ntfs_bitmap_clear_le(bh->b_data, b0, blocksize * 8 - b0);
1353+
frb = wbits - ntfs_bitmap_weight_le(bh->b_data, wbits);
13681354
wnd->total_zeroes += frb - wnd->free_bits[iw];
13691355
wnd->free_bits[iw] = frb;
13701356

@@ -1411,7 +1397,6 @@ int ntfs_trim_fs(struct ntfs_sb_info *sbi, struct fstrim_range *range)
14111397
CLST lcn_from = bytes_to_cluster(sbi, range->start);
14121398
size_t iw = lcn_from >> (sb->s_blocksize_bits + 3);
14131399
u32 wbit = lcn_from & (wbits - 1);
1414-
const ulong *buf;
14151400
CLST lcn_to;
14161401

14171402
if (!minlen)
@@ -1446,10 +1431,8 @@ int ntfs_trim_fs(struct ntfs_sb_info *sbi, struct fstrim_range *range)
14461431
break;
14471432
}
14481433

1449-
buf = (ulong *)bh->b_data;
1450-
14511434
for (; wbit < wbits; wbit++) {
1452-
if (!test_bit_le(wbit, buf)) {
1435+
if (!test_bit_le(wbit, bh->b_data)) {
14531436
if (!len)
14541437
lcn = lcn_wnd + wbit;
14551438
len += 1;
@@ -1482,42 +1465,69 @@ int ntfs_trim_fs(struct ntfs_sb_info *sbi, struct fstrim_range *range)
14821465
return err;
14831466
}
14841467

1485-
void ntfs_bitmap_set_le(unsigned long *map, unsigned int start, int len)
1468+
#if BITS_PER_LONG == 64
1469+
typedef __le64 bitmap_ulong;
1470+
#define cpu_to_ul(x) cpu_to_le64(x)
1471+
#define ul_to_cpu(x) le64_to_cpu(x)
1472+
#else
1473+
typedef __le32 bitmap_ulong;
1474+
#define cpu_to_ul(x) cpu_to_le32(x)
1475+
#define ul_to_cpu(x) le32_to_cpu(x)
1476+
#endif
1477+
1478+
void ntfs_bitmap_set_le(void *map, unsigned int start, int len)
14861479
{
1487-
unsigned long *p = map + BIT_WORD(start);
1480+
bitmap_ulong *p = (bitmap_ulong *)map + BIT_WORD(start);
14881481
const unsigned int size = start + len;
14891482
int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
1490-
unsigned long mask_to_set = cpu_to_le32(BITMAP_FIRST_WORD_MASK(start));
1483+
bitmap_ulong mask_to_set = cpu_to_ul(BITMAP_FIRST_WORD_MASK(start));
14911484

14921485
while (len - bits_to_set >= 0) {
14931486
*p |= mask_to_set;
14941487
len -= bits_to_set;
14951488
bits_to_set = BITS_PER_LONG;
1496-
mask_to_set = ~0UL;
1489+
mask_to_set = cpu_to_ul(~0UL);
14971490
p++;
14981491
}
14991492
if (len) {
1500-
mask_to_set &= cpu_to_le32(BITMAP_LAST_WORD_MASK(size));
1493+
mask_to_set &= cpu_to_ul(BITMAP_LAST_WORD_MASK(size));
15011494
*p |= mask_to_set;
15021495
}
15031496
}
15041497

1505-
void ntfs_bitmap_clear_le(unsigned long *map, unsigned int start, int len)
1498+
void ntfs_bitmap_clear_le(void *map, unsigned int start, int len)
15061499
{
1507-
unsigned long *p = map + BIT_WORD(start);
1500+
bitmap_ulong *p = (bitmap_ulong *)map + BIT_WORD(start);
15081501
const unsigned int size = start + len;
15091502
int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
1510-
unsigned long mask_to_clear = cpu_to_le32(BITMAP_FIRST_WORD_MASK(start));
1503+
bitmap_ulong mask_to_clear = cpu_to_ul(BITMAP_FIRST_WORD_MASK(start));
15111504

15121505
while (len - bits_to_clear >= 0) {
15131506
*p &= ~mask_to_clear;
15141507
len -= bits_to_clear;
15151508
bits_to_clear = BITS_PER_LONG;
1516-
mask_to_clear = ~0UL;
1509+
mask_to_clear = cpu_to_ul(~0UL);
15171510
p++;
15181511
}
15191512
if (len) {
1520-
mask_to_clear &= cpu_to_le32(BITMAP_LAST_WORD_MASK(size));
1513+
mask_to_clear &= cpu_to_ul(BITMAP_LAST_WORD_MASK(size));
15211514
*p &= ~mask_to_clear;
15221515
}
15231516
}
1517+
1518+
unsigned int ntfs_bitmap_weight_le(const void *bitmap, int bits)
1519+
{
1520+
const ulong *bmp = bitmap;
1521+
unsigned int k, lim = bits / BITS_PER_LONG;
1522+
unsigned int w = 0;
1523+
1524+
for (k = 0; k < lim; k++)
1525+
w += hweight_long(bmp[k]);
1526+
1527+
if (bits % BITS_PER_LONG) {
1528+
w += hweight_long(ul_to_cpu(((bitmap_ulong *)bitmap)[k]) &
1529+
BITMAP_LAST_WORD_MASK(bits));
1530+
}
1531+
1532+
return w;
1533+
}

fs/ntfs3/ntfs_fs.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -472,9 +472,9 @@ static inline size_t al_aligned(size_t size)
472472
}
473473

474474
/* Globals from bitfunc.c */
475-
bool are_bits_clear(const ulong *map, size_t bit, size_t nbits);
476-
bool are_bits_set(const ulong *map, size_t bit, size_t nbits);
477-
size_t get_set_bits_ex(const ulong *map, size_t bit, size_t nbits);
475+
bool are_bits_clear(const void *map, size_t bit, size_t nbits);
476+
bool are_bits_set(const void *map, size_t bit, size_t nbits);
477+
size_t get_set_bits_ex(const void *map, size_t bit, size_t nbits);
478478

479479
/* Globals from dir.c */
480480
int ntfs_utf16_to_nls(struct ntfs_sb_info *sbi, const __le16 *name, u32 len,
@@ -839,8 +839,9 @@ int wnd_extend(struct wnd_bitmap *wnd, size_t new_bits);
839839
void wnd_zone_set(struct wnd_bitmap *wnd, size_t Lcn, size_t Len);
840840
int ntfs_trim_fs(struct ntfs_sb_info *sbi, struct fstrim_range *range);
841841

842-
void ntfs_bitmap_set_le(unsigned long *map, unsigned int start, int len);
843-
void ntfs_bitmap_clear_le(unsigned long *map, unsigned int start, int len);
842+
void ntfs_bitmap_set_le(void *map, unsigned int start, int len);
843+
void ntfs_bitmap_clear_le(void *map, unsigned int start, int len);
844+
unsigned int ntfs_bitmap_weight_le(const void *bitmap, int bits);
844845

845846
/* Globals from upcase.c */
846847
int ntfs_cmp_names(const __le16 *s1, size_t l1, const __le16 *s2, size_t l2,

0 commit comments

Comments
 (0)