10 #include <linux/slab.h>
16 #include <linux/prefetch.h>
18 #include <linux/rbtree.h>
35 #define BFITNOENT ((u32)~0)
36 #define NO_BLOCK ((u64)~0)
38 #if BITS_PER_LONG == 32
39 #define LBITMASK (0x55555555UL)
40 #define LBITSKIP55 (0x55555555UL)
41 #define LBITSKIP00 (0x00000000UL)
43 #define LBITMASK (0x5555555555555555UL)
44 #define LBITSKIP55 (0x5555555555555555UL)
45 #define LBITSKIP00 (0x0000000000000000UL)
59 static const char valid_change[16] = {
79 static inline void gfs2_setbit(
const struct gfs2_rbm *rbm,
bool do_clone,
82 unsigned char *byte1, *byte2, *
end, cur_state;
83 unsigned int buflen = rbm->
bi->bi_len;
87 end = rbm->
bi->bi_bh->b_data + rbm->
bi->bi_offset +
buflen;
93 if (
unlikely(!valid_change[new_state * 4 + cur_state])) {
95 "new_state=%d\n", rbm->
offset, cur_state, new_state);
97 (
unsigned long long)rbm->
rgd->rd_addr,
100 rbm->
bi->bi_offset, rbm->
bi->bi_len);
105 *byte1 ^= (cur_state ^
new_state) << bit;
107 if (do_clone && rbm->
bi->bi_clone) {
110 *byte2 ^= (cur_state ^
new_state) << bit;
121 static inline u8 gfs2_testbit(
const struct gfs2_rbm *rbm)
123 const u8 *
buffer = rbm->
bi->bi_bh->b_data + rbm->
bi->bi_offset;
155 [0] = 0xffffffffffffffffULL,
156 [1] = 0xaaaaaaaaaaaaaaaaULL,
157 [2] = 0x5555555555555555ULL,
158 [3] = 0x0000000000000000ULL,
178 u64 startblk = gfs2_rbm_to_block(&rs->
rs_rbm);
180 if (blk >= startblk + rs->
rs_free)
182 if (blk + len - 1 < startblk)
208 static u32 gfs2_bitfit(
const u8 *
buf,
const unsigned int len,
211 u32 spoint = (goal << 1) & ((8*
sizeof(
u64)) - 1);
215 u64 mask = 0x5555555555555555ULL;
220 tmp = gfs2_bit_search(ptr, mask, state);
222 while(tmp == 0 && ptr < end) {
223 tmp = gfs2_bit_search(ptr, 0x5555555555555555ULL, state);
227 if (ptr == end && (len & (
sizeof(
u64) - 1)))
228 tmp &= (((
u64)~0) >> (64 - 8*(len & (
sizeof(
u64) - 1))));
235 return (((
const unsigned char *)ptr - buf) *
GFS2_NBBY) +
bit;
253 u64 rblock = block - rbm->
rgd->rd_data0;
259 if (block >= rbm->
rgd->rd_data0 + rbm->
rgd->rd_data)
262 for (x = 0; x < rbm->
rgd->rd_length; x++) {
263 rbm->
bi = rbm->
rgd->rd_bits +
x;
264 if (goal < (rbm->
bi->bi_start + rbm->
bi->bi_len) *
GFS2_NBBY) {
282 static bool gfs2_unaligned_extlen(
struct gfs2_rbm *rbm,
u32 n_unaligned,
u32 *len)
288 for (n = 0; n < n_unaligned; n++) {
289 res = gfs2_testbit(rbm);
295 block = gfs2_rbm_to_block(rbm);
296 if (gfs2_rbm_from_block(rbm, block + 1))
329 gfs2_unaligned_extlen(&rbm, 4 - n_unaligned, &len))
332 n_unaligned = len & 3;
335 start = rbm.
bi->bi_bh->b_data;
336 if (rbm.
bi->bi_clone)
337 start = rbm.
bi->bi_clone;
338 end = start + rbm.
bi->bi_bh->b_size;
339 start += rbm.
bi->bi_offset;
344 chunk_size = ((ptr ==
NULL) ? bytes : (ptr - start));
348 block = gfs2_rbm_to_block(&rbm);
349 gfs2_rbm_from_block(&rbm, block + chunk_size);
353 n_unaligned = len & 3;
358 gfs2_unaligned_extlen(&rbm, n_unaligned, &len);
374 unsigned int buflen,
u8 state)
383 for (; byte <
end; byte++) {
384 if (((*byte) & 0x03) == state)
386 if (((*byte) & 0x0C) == state1)
388 if (((*byte) & 0x30) == state2)
390 if (((*byte) & 0xC0) == state3)
414 for (buf = 0; buf <
length; buf++) {
416 for (x = 0; x < 4; x++)
417 count[x] += gfs2_bitcount(rgd,
423 if (count[0] != rgd->
rd_free) {
425 fs_err(sdp,
"free data mismatch: %u != %u\n",
431 if (count[1] != tmp) {
433 fs_err(sdp,
"used data mismatch: %u != %u\n",
440 fs_err(sdp,
"used metadata mismatch: %u != %u\n",
446 static inline int rgrp_contains_block(
struct gfs2_rgrpd *rgd,
u64 block)
450 return first <= block && block < last;
479 if (blk < cur->rd_addr)
579 (
unsigned long long)rs->
rs_inum,
580 (
unsigned long long)gfs2_rbm_to_block(&rs->
rs_rbm),
593 if (!gfs2_rs_active(rs))
623 __rs_deltree(ip, rs);
653 static void return_all_reservations(
struct gfs2_rgrpd *rgd)
661 __rs_deltree(
NULL, rs);
688 return_all_reservations(rgd);
693 static void gfs2_rindex_print(
const struct gfs2_rgrpd *rgd)
711 static int compute_bitstructs(
struct gfs2_rgrpd *rgd)
728 for (x = 0; x <
length; x++) {
745 }
else if (x + 1 == length) {
752 bytes = sdp->
sd_sb.sb_bsize -
766 bi = rgd->
rd_bits + (length - 1);
769 gfs2_rindex_print(rgd);
770 fs_err(sdp,
"start=%u len=%u offset=%u\n",
792 for (rgrps = 0;; rgrps++) {
795 if (pos +
sizeof(
struct gfs2_rindex) > i_size_read(inode))
818 newn = &((*newn)->rb_left);
820 newn = &((*newn)->rb_right);
825 rb_link_node(&rgd->
rd_node, parent, newn);
838 static int read_rindex_entry(
struct gfs2_inode *ip)
846 if (pos >= i_size_read(&ip->
i_inode))
853 return (error == 0) ? 1 :
error;
855 rgd = kmem_cache_zalloc(gfs2_rgrpd_cachep,
GFP_NOFS);
868 error = compute_bitstructs(rgd);
877 rgd->
rd_gl->gl_object = rgd;
883 error = rgd_insert(rgd);
904 static int gfs2_ri_update(
struct gfs2_inode *ip)
910 error = read_rindex_entry(ip);
911 }
while (error == 0);
943 int unlock_required = 0;
947 if (!gfs2_glock_is_locked_by_me(gl)) {
948 error = gfs2_glock_nq_init(gl,
LM_ST_SHARED, 0, &ri_gh);
954 error = gfs2_ri_update(ip);
962 static void gfs2_rgrp_in(
struct gfs2_rgrpd *rgd,
const void *buf)
976 static void gfs2_rgrp_out(
struct gfs2_rgrpd *rgd,
void *buf)
988 static int gfs2_rgrp_lvb_valid(
struct gfs2_rgrpd *rgd)
1000 static void gfs2_rgrp_ondisk2lvb(
struct gfs2_rgrp_lvb *rgl,
const void *buf)
1012 static void update_rgrp_lvb_unlinked(
struct gfs2_rgrpd *rgd,
u32 change)
1024 u32 i, goal, count = 0;
1026 for (i = 0, bi = rgd->
rd_bits; i < length; i++, bi++) {
1031 goal = gfs2_bitfit(buffer, bi->
bi_len, goal,
1066 for (x = 0; x <
length; x++) {
1073 for (y = length; y--;) {
1086 for (x = 0; x <
length; x++)
1088 gfs2_rgrp_in(rgd, (rgd->
rd_bits[0].bi_bh)->b_data);
1094 gfs2_rgrp_ondisk2lvb(rgd->
rd_rgl,
1095 rgd->
rd_bits[0].bi_bh->b_data);
1097 else if (sdp->
sd_args.ar_rgrplvb) {
1098 if (!gfs2_rgrp_lvb_valid(rgd)){
1103 if (rgd->
rd_rgl->rl_unlinked == 0)
1133 if (rgd->
rd_rgl->rl_unlinked == 0)
1163 for (x = 0; x <
length; x++) {
1174 struct buffer_head *bh,
1179 const unsigned int sects_per_blk = sdp->
sd_sb.sb_bsize /
1180 bdev_logical_block_size(sb->
s_bdev);
1189 for (x = 0; x < bi->
bi_len; x++) {
1195 diff = ~(*orig | (*orig >> 1)) & (*clone | (*clone >> 1));
1197 diff = ~(*clone | (*clone >> 1));
1203 blk *= sects_per_blk;
1207 goto start_new_extent;
1208 if ((start + nr_sects) != blk) {
1209 if (nr_sects >= minlen) {
1215 trimmed += nr_sects;
1221 nr_sects += sects_per_blk;
1224 blk += sects_per_blk;
1227 if (nr_sects >= minlen) {
1231 trimmed += nr_sects;
1234 *ptrimmed = trimmed;
1239 fs_warn(sdp,
"error %d on discard request, turning discards off for this filesystem", rv);
1254 struct inode *
inode = filp->f_dentry->d_inode;
1255 struct gfs2_sbd *sdp = GFS2_SB(inode);
1257 struct buffer_head *bh;
1267 unsigned bs_shift = sdp->
sd_sb.sb_bsize_shift;
1272 if (!blk_queue_discard(q))
1282 start = r.
start >> bs_shift;
1283 end = start + (r.
len >> bs_shift);
1285 q->limits.discard_granularity) >> bs_shift;
1321 gfs2_rgrp_out(rgd, bh->b_data);
1322 gfs2_rgrp_ondisk2lvb(rgd->
rd_rgl, bh->b_data);
1335 r.
len = trimmed << 9;
1353 u64 fsblock = gfs2_rbm_to_block(&rs->
rs_rbm);
1355 BUG_ON(gfs2_rs_active(rs));
1364 rc = rs_cmp(fsblock, rs->
rs_free, cur);
1366 newn = &((*newn)->rb_right);
1368 newn = &((*newn)->rb_left);
1376 rb_link_node(&rs->
rs_node, parent, newn);
1409 if (rgrp_contains_block(rgd, ip->
i_goal))
1414 if (
WARN_ON(gfs2_rbm_from_block(&rbm, goal)))
1451 rc = rs_cmp(block, length, rs);
1461 while ((rs_cmp(block, length, rs) == 0) && (ip->
i_res != rs)) {
1489 static int gfs2_reservation_check_and_update(
struct gfs2_rbm *rbm,
1493 u64 block = gfs2_rbm_to_block(rbm);
1503 extlen = gfs2_free_extlen(rbm, minext);
1504 nblock = block + extlen;
1505 if (extlen < minext)
1513 nblock = gfs2_next_unreserved_block(rbm->
rgd, block, extlen, ip);
1514 if (nblock == block)
1517 ret = gfs2_rbm_from_block(rbm, nblock);
1539 static int gfs2_rbm_find(
struct gfs2_rbm *rbm,
u8 state,
u32 minext,
1542 struct buffer_head *bh;
1549 int iters = rbm->
rgd->rd_length;
1564 bh = rbm->
bi->bi_bh;
1565 buffer = bh->b_data + rbm->
bi->bi_offset;
1566 WARN_ON(!buffer_uptodate(bh));
1568 buffer = rbm->
bi->bi_clone + rbm->
bi->bi_offset;
1569 initial_offset = rbm->
offset;
1570 offset = gfs2_bitfit(buffer, rbm->
bi->bi_len, rbm->
offset, state);
1577 initial_bi = rbm->
bi;
1578 ret = gfs2_reservation_check_and_update(rbm, ip, minext);
1582 n += (rbm->
bi - initial_bi);
1585 if (ret == -
E2BIG) {
1588 n += (rbm->
bi - initial_bi);
1589 goto res_covered_end_of_rgrp;
1599 index = rbm->
bi - rbm->
rgd->rd_bits;
1601 if (index == rbm->
rgd->rd_length)
1603 res_covered_end_of_rgrp:
1605 if ((index == 0) && nowrap)
1645 block = gfs2_rbm_to_block(&rbm);
1646 if (gfs2_rbm_from_block(&rbm, block + 1))
1648 if (*last_unlinked !=
NO_BLOCK && block <= *last_unlinked)
1652 *last_unlinked =
block;
1715 if (gfs2_rs_active(rs)) {
1729 if (!gfs2_glock_is_locked_by_me(rs->
rs_rbm.rgd->rd_gl)) {
1731 error = gfs2_glock_nq_init(rs->
rs_rbm.rgd->rd_gl,
1738 if (sdp->
sd_args.ar_rgrplvb) {
1755 if (!gfs2_rs_active(rs))
1756 rg_mblk_search(rs->
rs_rbm.rgd, ip, requested);
1759 if (!gfs2_rs_active(rs) && (loops < 1))
1763 if (rs->
rs_rbm.rgd->rd_free_clone >= requested) {
1769 if (gfs2_rs_active(rs))
1774 try_rgrp_unlink(rs->
rs_rbm.rgd, &last_unlinked,
1782 if (gfs2_select_rgrp(&rs->
rs_rbm.rgd, begin))
1793 error = gfs2_ri_update(ip);
1828 static unsigned char gfs2_get_block_type(
struct gfs2_rgrpd *rgd,
u64 block)
1833 ret = gfs2_rbm_from_block(&rbm, block);
1836 return gfs2_testbit(&rbm);
1849 static void gfs2_alloc_extent(
const struct gfs2_rbm *rbm,
bool dinode,
1853 const unsigned int elen = *
n;
1858 block = gfs2_rbm_to_block(rbm);
1863 ret = gfs2_rbm_from_block(&pos, block);
1884 u32 blen,
unsigned char new_state)
1891 fs_err(sdp,
"block = %llu\n", (
unsigned long long)bstart);
1896 gfs2_rbm_from_block(&rbm, bstart);
1898 if (!rbm.
bi->bi_clone) {
1902 rbm.
bi->bi_bh->b_data + rbm.
bi->bi_offset,
1906 gfs2_setbit(&rbm,
false, new_state);
1940 static void gfs2_rgrp_error(
struct gfs2_rgrpd *rgd)
1943 fs_warn(sdp,
"rgrp %llu has an error, marking it readonly until umount\n",
1944 (
unsigned long long)rgd->
rd_addr);
1945 fs_warn(sdp,
"umount on all nodes and run fsck.gfs2 to fix the error\n");
1961 static void gfs2_adjust_reservation(
struct gfs2_inode *ip,
1962 const struct gfs2_rbm *rbm,
unsigned len)
1971 if (gfs2_rs_active(rs)) {
1972 if (gfs2_rbm_eq(&rs->
rs_rbm, rbm)) {
1973 block = gfs2_rbm_to_block(rbm);
1974 ret = gfs2_rbm_from_block(&rs->
rs_rbm, block + len);
1982 __rs_deltree(ip, rs);
2003 struct buffer_head *dibh;
2010 if (gfs2_rs_active(ip->
i_res))
2011 goal = gfs2_rbm_to_block(&ip->
i_res->rs_rbm);
2012 else if (!dinode && rgrp_contains_block(rbm.
rgd, ip->
i_goal))
2015 goal = rbm.
rgd->rd_last_alloc + rbm.
rgd->rd_data0;
2017 gfs2_rbm_from_block(&rbm, goal);
2021 gfs2_rbm_from_block(&rbm, goal);
2027 fs_warn(sdp,
"inum=%llu error=%d, nblocks=%u, full=%d\n",
2028 (
unsigned long long)ip->
i_no_addr, error, *nblocks,
2033 gfs2_alloc_extent(&rbm, dinode, nblocks);
2034 block = gfs2_rbm_to_block(&rbm);
2035 rbm.
rgd->rd_last_alloc = block - rbm.
rgd->rd_data0;
2036 if (gfs2_rs_active(ip->
i_res))
2037 gfs2_adjust_reservation(ip, &rbm, *nblocks);
2043 ip->
i_goal = block + ndata - 1;
2044 error = gfs2_meta_inode_buffer(ip, &dibh);
2054 if (rbm.
rgd->rd_free < *nblocks) {
2059 rbm.
rgd->rd_free -= *nblocks;
2061 rbm.
rgd->rd_dinodes++;
2062 *generation = rbm.
rgd->rd_igeneration++;
2063 if (*generation == 0)
2064 *generation = rbm.
rgd->rd_igeneration++;
2068 gfs2_rgrp_out(rbm.
rgd, rbm.
rgd->rd_bits[0].bi_bh->b_data);
2069 gfs2_rgrp_ondisk2lvb(rbm.
rgd->rd_rgl, rbm.
rgd->rd_bits[0].bi_bh->b_data);
2083 rbm.
rgd->rd_free_clone -= *nblocks;
2084 trace_gfs2_block_alloc(ip, rbm.
rgd, block, *nblocks,
2090 gfs2_rgrp_error(rbm.
rgd);
2115 gfs2_rgrp_out(rgd, rgd->
rd_bits[0].bi_bh->b_data);
2116 gfs2_rgrp_ondisk2lvb(rgd->
rd_rgl, rgd->
rd_bits[0].bi_bh->b_data);
2143 struct gfs2_sbd *sdp = GFS2_SB(inode);
2152 gfs2_rgrp_out(rgd, rgd->
rd_bits[0].bi_bh->b_data);
2153 gfs2_rgrp_ondisk2lvb(rgd->
rd_rgl, rgd->
rd_bits[0].bi_bh->b_data);
2154 update_rgrp_lvb_unlinked(rgd, 1);
2157 static void gfs2_free_uninit_di(
struct gfs2_rgrpd *rgd,
u64 blkno)
2173 gfs2_rgrp_out(rgd, rgd->
rd_bits[0].bi_bh->b_data);
2174 gfs2_rgrp_ondisk2lvb(rgd->
rd_rgl, rgd->
rd_bits[0].bi_bh->b_data);
2175 update_rgrp_lvb_unlinked(rgd, -1);
2183 gfs2_free_uninit_di(rgd, ip->
i_no_addr);
2214 if (gfs2_get_block_type(rgd, no_addr) != type)
2240 unsigned int new_space;
2246 if (ip->
i_rgd && rgrp_contains_block(ip->
i_rgd, block))
2251 fs_err(sdp,
"rlist_add: no rgrp for block %llu\n", (
unsigned long long)block);
2256 for (x = 0; x < rlist->
rl_rgrps; x++)
2257 if (rlist->
rl_rgd[x] == rgd)
2263 tmp = kcalloc(new_space,
sizeof(
struct gfs2_rgrpd *),
2295 for (x = 0; x < rlist->
rl_rgrps; x++)
2314 for (x = 0; x < rlist->
rl_rgrps; x++)