aboutsummaryrefslogtreecommitdiffstats
path: root/fs/gfs2/rgrp.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/gfs2/rgrp.c')
-rw-r--r--fs/gfs2/rgrp.c152
1 files changed, 100 insertions, 52 deletions
diff --git a/fs/gfs2/rgrp.c b/fs/gfs2/rgrp.c
index 565038243fa2..daa4ae341a29 100644
--- a/fs/gfs2/rgrp.c
+++ b/fs/gfs2/rgrp.c
@@ -29,7 +29,7 @@
29#include "util.h" 29#include "util.h"
30#include "log.h" 30#include "log.h"
31#include "inode.h" 31#include "inode.h"
32#include "ops_address.h" 32#include "trace_gfs2.h"
33 33
34#define BFITNOENT ((u32)~0) 34#define BFITNOENT ((u32)~0)
35#define NO_BLOCK ((u64)~0) 35#define NO_BLOCK ((u64)~0)
@@ -442,6 +442,7 @@ static int compute_bitstructs(struct gfs2_rgrpd *rgd)
442 for (x = 0; x < length; x++) { 442 for (x = 0; x < length; x++) {
443 bi = rgd->rd_bits + x; 443 bi = rgd->rd_bits + x;
444 444
445 bi->bi_flags = 0;
445 /* small rgrp; bitmap stored completely in header block */ 446 /* small rgrp; bitmap stored completely in header block */
446 if (length == 1) { 447 if (length == 1) {
447 bytes = bytes_left; 448 bytes = bytes_left;
@@ -580,7 +581,6 @@ static int read_rindex_entry(struct gfs2_inode *ip,
580 581
581 rgd->rd_gl->gl_object = rgd; 582 rgd->rd_gl->gl_object = rgd;
582 rgd->rd_flags &= ~GFS2_RDF_UPTODATE; 583 rgd->rd_flags &= ~GFS2_RDF_UPTODATE;
583 rgd->rd_flags |= GFS2_RDF_CHECK;
584 return error; 584 return error;
585} 585}
586 586
@@ -701,10 +701,9 @@ static void gfs2_rgrp_in(struct gfs2_rgrpd *rgd, const void *buf)
701 u32 rg_flags; 701 u32 rg_flags;
702 702
703 rg_flags = be32_to_cpu(str->rg_flags); 703 rg_flags = be32_to_cpu(str->rg_flags);
704 if (rg_flags & GFS2_RGF_NOALLOC) 704 rg_flags &= ~GFS2_RDF_MASK;
705 rgd->rd_flags |= GFS2_RDF_NOALLOC; 705 rgd->rd_flags &= GFS2_RDF_MASK;
706 else 706 rgd->rd_flags |= rg_flags;
707 rgd->rd_flags &= ~GFS2_RDF_NOALLOC;
708 rgd->rd_free = be32_to_cpu(str->rg_free); 707 rgd->rd_free = be32_to_cpu(str->rg_free);
709 rgd->rd_dinodes = be32_to_cpu(str->rg_dinodes); 708 rgd->rd_dinodes = be32_to_cpu(str->rg_dinodes);
710 rgd->rd_igeneration = be64_to_cpu(str->rg_igeneration); 709 rgd->rd_igeneration = be64_to_cpu(str->rg_igeneration);
@@ -713,11 +712,8 @@ static void gfs2_rgrp_in(struct gfs2_rgrpd *rgd, const void *buf)
713static void gfs2_rgrp_out(struct gfs2_rgrpd *rgd, void *buf) 712static void gfs2_rgrp_out(struct gfs2_rgrpd *rgd, void *buf)
714{ 713{
715 struct gfs2_rgrp *str = buf; 714 struct gfs2_rgrp *str = buf;
716 u32 rg_flags = 0;
717 715
718 if (rgd->rd_flags & GFS2_RDF_NOALLOC) 716 str->rg_flags = cpu_to_be32(rgd->rd_flags & ~GFS2_RDF_MASK);
719 rg_flags |= GFS2_RGF_NOALLOC;
720 str->rg_flags = cpu_to_be32(rg_flags);
721 str->rg_free = cpu_to_be32(rgd->rd_free); 717 str->rg_free = cpu_to_be32(rgd->rd_free);
722 str->rg_dinodes = cpu_to_be32(rgd->rd_dinodes); 718 str->rg_dinodes = cpu_to_be32(rgd->rd_dinodes);
723 str->__pad = cpu_to_be32(0); 719 str->__pad = cpu_to_be32(0);
@@ -775,8 +771,10 @@ int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
775 } 771 }
776 772
777 if (!(rgd->rd_flags & GFS2_RDF_UPTODATE)) { 773 if (!(rgd->rd_flags & GFS2_RDF_UPTODATE)) {
774 for (x = 0; x < length; x++)
775 clear_bit(GBF_FULL, &rgd->rd_bits[x].bi_flags);
778 gfs2_rgrp_in(rgd, (rgd->rd_bits[0].bi_bh)->b_data); 776 gfs2_rgrp_in(rgd, (rgd->rd_bits[0].bi_bh)->b_data);
779 rgd->rd_flags |= GFS2_RDF_UPTODATE; 777 rgd->rd_flags |= (GFS2_RDF_UPTODATE | GFS2_RDF_CHECK);
780 } 778 }
781 779
782 spin_lock(&sdp->sd_rindex_spin); 780 spin_lock(&sdp->sd_rindex_spin);
@@ -845,7 +843,7 @@ static void gfs2_rgrp_send_discards(struct gfs2_sbd *sdp, u64 offset,
845 struct super_block *sb = sdp->sd_vfs; 843 struct super_block *sb = sdp->sd_vfs;
846 struct block_device *bdev = sb->s_bdev; 844 struct block_device *bdev = sb->s_bdev;
847 const unsigned int sects_per_blk = sdp->sd_sb.sb_bsize / 845 const unsigned int sects_per_blk = sdp->sd_sb.sb_bsize /
848 bdev_hardsect_size(sb->s_bdev); 846 bdev_logical_block_size(sb->s_bdev);
849 u64 blk; 847 u64 blk;
850 sector_t start = 0; 848 sector_t start = 0;
851 sector_t nr_sects = 0; 849 sector_t nr_sects = 0;
@@ -903,6 +901,7 @@ void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
903 continue; 901 continue;
904 if (sdp->sd_args.ar_discard) 902 if (sdp->sd_args.ar_discard)
905 gfs2_rgrp_send_discards(sdp, rgd->rd_data0, bi); 903 gfs2_rgrp_send_discards(sdp, rgd->rd_data0, bi);
904 clear_bit(GBF_FULL, &bi->bi_flags);
906 memcpy(bi->bi_clone + bi->bi_offset, 905 memcpy(bi->bi_clone + bi->bi_offset,
907 bi->bi_bh->b_data + bi->bi_offset, bi->bi_len); 906 bi->bi_bh->b_data + bi->bi_offset, bi->bi_len);
908 } 907 }
@@ -942,7 +941,7 @@ static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al)
942 struct gfs2_sbd *sdp = rgd->rd_sbd; 941 struct gfs2_sbd *sdp = rgd->rd_sbd;
943 int ret = 0; 942 int ret = 0;
944 943
945 if (rgd->rd_flags & GFS2_RDF_NOALLOC) 944 if (rgd->rd_flags & (GFS2_RGF_NOALLOC | GFS2_RDF_ERROR))
946 return 0; 945 return 0;
947 946
948 spin_lock(&sdp->sd_rindex_spin); 947 spin_lock(&sdp->sd_rindex_spin);
@@ -1315,30 +1314,37 @@ static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
1315{ 1314{
1316 struct gfs2_bitmap *bi = NULL; 1315 struct gfs2_bitmap *bi = NULL;
1317 const u32 length = rgd->rd_length; 1316 const u32 length = rgd->rd_length;
1318 u32 blk = 0; 1317 u32 blk = BFITNOENT;
1319 unsigned int buf, x; 1318 unsigned int buf, x;
1320 const unsigned int elen = *n; 1319 const unsigned int elen = *n;
1321 const u8 *buffer; 1320 const u8 *buffer = NULL;
1322 1321
1323 *n = 0; 1322 *n = 0;
1324 /* Find bitmap block that contains bits for goal block */ 1323 /* Find bitmap block that contains bits for goal block */
1325 for (buf = 0; buf < length; buf++) { 1324 for (buf = 0; buf < length; buf++) {
1326 bi = rgd->rd_bits + buf; 1325 bi = rgd->rd_bits + buf;
1327 if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY) 1326 /* Convert scope of "goal" from rgrp-wide to within found bit block */
1328 break; 1327 if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY) {
1328 goal -= bi->bi_start * GFS2_NBBY;
1329 goto do_search;
1330 }
1329 } 1331 }
1332 buf = 0;
1333 goal = 0;
1330 1334
1331 gfs2_assert(rgd->rd_sbd, buf < length); 1335do_search:
1332
1333 /* Convert scope of "goal" from rgrp-wide to within found bit block */
1334 goal -= bi->bi_start * GFS2_NBBY;
1335
1336 /* Search (up to entire) bitmap in this rgrp for allocatable block. 1336 /* Search (up to entire) bitmap in this rgrp for allocatable block.
1337 "x <= length", instead of "x < length", because we typically start 1337 "x <= length", instead of "x < length", because we typically start
1338 the search in the middle of a bit block, but if we can't find an 1338 the search in the middle of a bit block, but if we can't find an
1339 allocatable block anywhere else, we want to be able wrap around and 1339 allocatable block anywhere else, we want to be able wrap around and
1340 search in the first part of our first-searched bit block. */ 1340 search in the first part of our first-searched bit block. */
1341 for (x = 0; x <= length; x++) { 1341 for (x = 0; x <= length; x++) {
1342 bi = rgd->rd_bits + buf;
1343
1344 if (test_bit(GBF_FULL, &bi->bi_flags) &&
1345 (old_state == GFS2_BLKST_FREE))
1346 goto skip;
1347
1342 /* The GFS2_BLKST_UNLINKED state doesn't apply to the clone 1348 /* The GFS2_BLKST_UNLINKED state doesn't apply to the clone
1343 bitmaps, so we must search the originals for that. */ 1349 bitmaps, so we must search the originals for that. */
1344 buffer = bi->bi_bh->b_data + bi->bi_offset; 1350 buffer = bi->bi_bh->b_data + bi->bi_offset;
@@ -1349,33 +1355,39 @@ static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
1349 if (blk != BFITNOENT) 1355 if (blk != BFITNOENT)
1350 break; 1356 break;
1351 1357
1358 if ((goal == 0) && (old_state == GFS2_BLKST_FREE))
1359 set_bit(GBF_FULL, &bi->bi_flags);
1360
1352 /* Try next bitmap block (wrap back to rgrp header if at end) */ 1361 /* Try next bitmap block (wrap back to rgrp header if at end) */
1353 buf = (buf + 1) % length; 1362skip:
1354 bi = rgd->rd_bits + buf; 1363 buf++;
1364 buf %= length;
1355 goal = 0; 1365 goal = 0;
1356 } 1366 }
1357 1367
1358 if (blk != BFITNOENT && old_state != new_state) { 1368 if (blk == BFITNOENT)
1359 *n = 1; 1369 return blk;
1360 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1); 1370 *n = 1;
1371 if (old_state == new_state)
1372 goto out;
1373
1374 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1375 gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone, bi->bi_offset,
1376 bi->bi_len, blk, new_state);
1377 goal = blk;
1378 while (*n < elen) {
1379 goal++;
1380 if (goal >= (bi->bi_len * GFS2_NBBY))
1381 break;
1382 if (gfs2_testbit(rgd, buffer, bi->bi_len, goal) !=
1383 GFS2_BLKST_FREE)
1384 break;
1361 gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone, bi->bi_offset, 1385 gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone, bi->bi_offset,
1362 bi->bi_len, blk, new_state); 1386 bi->bi_len, goal, new_state);
1363 goal = blk; 1387 (*n)++;
1364 while (*n < elen) {
1365 goal++;
1366 if (goal >= (bi->bi_len * GFS2_NBBY))
1367 break;
1368 if (gfs2_testbit(rgd, buffer, bi->bi_len, goal) !=
1369 GFS2_BLKST_FREE)
1370 break;
1371 gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone,
1372 bi->bi_offset, bi->bi_len, goal,
1373 new_state);
1374 (*n)++;
1375 }
1376 } 1388 }
1377 1389out:
1378 return (blk == BFITNOENT) ? blk : (bi->bi_start * GFS2_NBBY) + blk; 1390 return (bi->bi_start * GFS2_NBBY) + blk;
1379} 1391}
1380 1392
1381/** 1393/**
@@ -1435,13 +1447,33 @@ static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
1435} 1447}
1436 1448
1437/** 1449/**
1438 * gfs2_alloc_block - Allocate a block 1450 * gfs2_rgrp_dump - print out an rgrp
1451 * @seq: The iterator
1452 * @gl: The glock in question
1453 *
1454 */
1455
1456int gfs2_rgrp_dump(struct seq_file *seq, const struct gfs2_glock *gl)
1457{
1458 const struct gfs2_rgrpd *rgd = gl->gl_object;
1459 if (rgd == NULL)
1460 return 0;
1461 gfs2_print_dbg(seq, " R: n:%llu f:%02x b:%u/%u i:%u\n",
1462 (unsigned long long)rgd->rd_addr, rgd->rd_flags,
1463 rgd->rd_free, rgd->rd_free_clone, rgd->rd_dinodes);
1464 return 0;
1465}
1466
1467/**
1468 * gfs2_alloc_block - Allocate one or more blocks
1439 * @ip: the inode to allocate the block for 1469 * @ip: the inode to allocate the block for
1470 * @bn: Used to return the starting block number
1471 * @n: requested number of blocks/extent length (value/result)
1440 * 1472 *
1441 * Returns: the allocated block 1473 * Returns: 0 or error
1442 */ 1474 */
1443 1475
1444u64 gfs2_alloc_block(struct gfs2_inode *ip, unsigned int *n) 1476int gfs2_alloc_block(struct gfs2_inode *ip, u64 *bn, unsigned int *n)
1445{ 1477{
1446 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 1478 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1447 struct buffer_head *dibh; 1479 struct buffer_head *dibh;
@@ -1457,7 +1489,10 @@ u64 gfs2_alloc_block(struct gfs2_inode *ip, unsigned int *n)
1457 goal = rgd->rd_last_alloc; 1489 goal = rgd->rd_last_alloc;
1458 1490
1459 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED, n); 1491 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED, n);
1460 BUG_ON(blk == BFITNOENT); 1492
1493 /* Since all blocks are reserved in advance, this shouldn't happen */
1494 if (blk == BFITNOENT)
1495 goto rgrp_error;
1461 1496
1462 rgd->rd_last_alloc = blk; 1497 rgd->rd_last_alloc = blk;
1463 block = rgd->rd_data0 + blk; 1498 block = rgd->rd_data0 + blk;
@@ -1469,7 +1504,9 @@ u64 gfs2_alloc_block(struct gfs2_inode *ip, unsigned int *n)
1469 di->di_goal_meta = di->di_goal_data = cpu_to_be64(ip->i_goal); 1504 di->di_goal_meta = di->di_goal_data = cpu_to_be64(ip->i_goal);
1470 brelse(dibh); 1505 brelse(dibh);
1471 } 1506 }
1472 gfs2_assert_withdraw(sdp, rgd->rd_free >= *n); 1507 if (rgd->rd_free < *n)
1508 goto rgrp_error;
1509
1473 rgd->rd_free -= *n; 1510 rgd->rd_free -= *n;
1474 1511
1475 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1); 1512 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
@@ -1483,8 +1520,17 @@ u64 gfs2_alloc_block(struct gfs2_inode *ip, unsigned int *n)
1483 spin_lock(&sdp->sd_rindex_spin); 1520 spin_lock(&sdp->sd_rindex_spin);
1484 rgd->rd_free_clone -= *n; 1521 rgd->rd_free_clone -= *n;
1485 spin_unlock(&sdp->sd_rindex_spin); 1522 spin_unlock(&sdp->sd_rindex_spin);
1523 trace_gfs2_block_alloc(ip, block, *n, GFS2_BLKST_USED);
1524 *bn = block;
1525 return 0;
1486 1526
1487 return block; 1527rgrp_error:
1528 fs_warn(sdp, "rgrp %llu has an error, marking it readonly until umount\n",
1529 (unsigned long long)rgd->rd_addr);
1530 fs_warn(sdp, "umount on all nodes and run fsck.gfs2 to fix the error\n");
1531 gfs2_rgrp_dump(NULL, rgd->rd_gl);
1532 rgd->rd_flags |= GFS2_RDF_ERROR;
1533 return -EIO;
1488} 1534}
1489 1535
1490/** 1536/**
@@ -1526,7 +1572,7 @@ u64 gfs2_alloc_di(struct gfs2_inode *dip, u64 *generation)
1526 spin_lock(&sdp->sd_rindex_spin); 1572 spin_lock(&sdp->sd_rindex_spin);
1527 rgd->rd_free_clone--; 1573 rgd->rd_free_clone--;
1528 spin_unlock(&sdp->sd_rindex_spin); 1574 spin_unlock(&sdp->sd_rindex_spin);
1529 1575 trace_gfs2_block_alloc(dip, block, 1, GFS2_BLKST_DINODE);
1530 return block; 1576 return block;
1531} 1577}
1532 1578
@@ -1546,7 +1592,7 @@ void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen)
1546 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE); 1592 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1547 if (!rgd) 1593 if (!rgd)
1548 return; 1594 return;
1549 1595 trace_gfs2_block_alloc(ip, bstart, blen, GFS2_BLKST_FREE);
1550 rgd->rd_free += blen; 1596 rgd->rd_free += blen;
1551 1597
1552 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1); 1598 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
@@ -1574,7 +1620,7 @@ void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
1574 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE); 1620 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1575 if (!rgd) 1621 if (!rgd)
1576 return; 1622 return;
1577 1623 trace_gfs2_block_alloc(ip, bstart, blen, GFS2_BLKST_FREE);
1578 rgd->rd_free += blen; 1624 rgd->rd_free += blen;
1579 1625
1580 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1); 1626 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
@@ -1597,6 +1643,7 @@ void gfs2_unlink_di(struct inode *inode)
1597 rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED); 1643 rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
1598 if (!rgd) 1644 if (!rgd)
1599 return; 1645 return;
1646 trace_gfs2_block_alloc(ip, blkno, 1, GFS2_BLKST_UNLINKED);
1600 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1); 1647 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1601 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data); 1648 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
1602 gfs2_trans_add_rg(rgd); 1649 gfs2_trans_add_rg(rgd);
@@ -1628,6 +1675,7 @@ static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
1628void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip) 1675void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
1629{ 1676{
1630 gfs2_free_uninit_di(rgd, ip->i_no_addr); 1677 gfs2_free_uninit_di(rgd, ip->i_no_addr);
1678 trace_gfs2_block_alloc(ip, ip->i_no_addr, 1, GFS2_BLKST_FREE);
1631 gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid); 1679 gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
1632 gfs2_meta_wipe(ip, ip->i_no_addr, 1); 1680 gfs2_meta_wipe(ip, ip->i_no_addr, 1);
1633} 1681}