Skip to content

Commit 3c5d785

Browse files
AstralBobswhiteho
authored andcommitted
GFS2: combine gfs2_alloc_block and gfs2_alloc_di
GFS2 functions gfs2_alloc_block and gfs2_alloc_di do basically the same things, with a few exceptions. This patch combines the two functions into a slightly more generic gfs2_alloc_block. Having one centralized block allocation function will reduce code redundancy and make it easier to implement multi-block reservations to reduce file fragmentation in the future. Signed-off-by: Bob Peterson <rpeterso@redhat.com> Signed-off-by: Steven Whitehouse <swhiteho@redhat.com>
1 parent c688b8b commit 3c5d785

6 files changed

Lines changed: 45 additions & 77 deletions

File tree

‎fs/gfs2/bmap.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page)
133133
and write it out to disk */
134134

135135
unsigned int n = 1;
136-
error = gfs2_alloc_block(ip, &block, &n);
136+
error = gfs2_alloc_block(ip, &block, &n, 0, NULL);
137137
if (error)
138138
goto out_brelse;
139139
if (isdir) {
@@ -503,7 +503,7 @@ static int gfs2_bmap_alloc(struct inode *inode, const sector_t lblock,
503503
do {
504504
int error;
505505
n = blks - alloced;
506-
error = gfs2_alloc_block(ip, &bn, &n);
506+
error = gfs2_alloc_block(ip, &bn, &n, 0, NULL);
507507
if (error)
508508
return error;
509509
alloced += n;

‎fs/gfs2/dir.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh,
823823
struct gfs2_dirent *dent;
824824
struct qstr name = { .name = "", .len = 0, .hash = 0 };
825825

826-
error = gfs2_alloc_block(ip, &bn, &n);
826+
error = gfs2_alloc_block(ip, &bn, &n, 0, NULL);
827827
if (error)
828828
return NULL;
829829
bh = gfs2_meta_new(ip->i_gl, bn);

‎fs/gfs2/inode.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
402402
if (error)
403403
goto out_ipreserv;
404404

405-
error = gfs2_alloc_di(dip, no_addr, generation);
405+
error = gfs2_alloc_block(dip, no_addr, NULL, 1, generation);
406406

407407
gfs2_trans_end(sdp);
408408

‎fs/gfs2/rgrp.c‎

Lines changed: 36 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,117 +1304,85 @@ static void gfs2_rgrp_error(struct gfs2_rgrpd *rgd)
13041304
* @ip: the inode to allocate the block for
13051305
* @bn: Used to return the starting block number
13061306
* @n: requested number of blocks/extent length (value/result)
1307+
* dinode: 1 if we're allocating a dinode, 0 if it's a data block
1308+
* @generation: the generation number of the inode
13071309
*
13081310
* Returns: 0 or error
13091311
*/
13101312

1311-
int gfs2_alloc_block(struct gfs2_inode *ip, u64 *bn, unsigned int *n)
1313+
int gfs2_alloc_block(struct gfs2_inode *ip, u64 *bn, unsigned int *n,
1314+
int dinode, u64 *generation)
13121315
{
13131316
struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
13141317
struct buffer_head *dibh;
13151318
struct gfs2_alloc *al = ip->i_alloc;
13161319
struct gfs2_rgrpd *rgd;
1317-
u32 goal, blk;
1318-
u64 block;
1320+
u32 goal, blk; /* block, within the rgrp scope */
1321+
u64 block; /* block, within the file system scope */
1322+
unsigned int extn = 1;
13191323
int error;
1324+
unsigned char blk_type = dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED;
13201325

13211326
/* Only happens if there is a bug in gfs2, return something distinctive
13221327
* to ensure that it is noticed.
13231328
*/
13241329
if (al == NULL)
13251330
return -ECANCELED;
13261331

1332+
if (n == NULL)
1333+
n = &extn;
13271334
rgd = ip->i_rgd;
13281335

1329-
if (rgrp_contains_block(rgd, ip->i_goal))
1336+
if (!dinode && rgrp_contains_block(rgd, ip->i_goal))
13301337
goal = ip->i_goal - rgd->rd_data0;
13311338
else
13321339
goal = rgd->rd_last_alloc;
13331340

1334-
blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED, n);
1341+
blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, blk_type, n);
13351342

13361343
/* Since all blocks are reserved in advance, this shouldn't happen */
13371344
if (blk == BFITNOENT)
13381345
goto rgrp_error;
13391346

13401347
rgd->rd_last_alloc = blk;
13411348
block = rgd->rd_data0 + blk;
1342-
ip->i_goal = block + *n - 1;
1343-
error = gfs2_meta_inode_buffer(ip, &dibh);
1344-
if (error == 0) {
1345-
struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
1346-
gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1347-
di->di_goal_meta = di->di_goal_data = cpu_to_be64(ip->i_goal);
1348-
brelse(dibh);
1349+
if (!dinode) {
1350+
ip->i_goal = block + *n - 1;
1351+
error = gfs2_meta_inode_buffer(ip, &dibh);
1352+
if (error == 0) {
1353+
struct gfs2_dinode *di =
1354+
(struct gfs2_dinode *)dibh->b_data;
1355+
gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1356+
di->di_goal_meta = di->di_goal_data =
1357+
cpu_to_be64(ip->i_goal);
1358+
brelse(dibh);
1359+
}
13491360
}
13501361
if (rgd->rd_free < *n)
13511362
goto rgrp_error;
13521363

13531364
rgd->rd_free -= *n;
1365+
if (dinode) {
1366+
rgd->rd_dinodes++;
1367+
*generation = rgd->rd_igeneration++;
1368+
if (*generation == 0)
1369+
*generation = rgd->rd_igeneration++;
1370+
}
13541371

13551372
gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
13561373
gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
13571374

13581375
al->al_alloced += *n;
13591376

1360-
gfs2_statfs_change(sdp, 0, -(s64)*n, 0);
1361-
gfs2_quota_change(ip, *n, ip->i_inode.i_uid, ip->i_inode.i_gid);
1377+
gfs2_statfs_change(sdp, 0, -(s64)*n, dinode ? 1 : 0);
1378+
if (dinode)
1379+
gfs2_trans_add_unrevoke(sdp, block, 1);
1380+
else
1381+
gfs2_quota_change(ip, *n, ip->i_inode.i_uid,
1382+
ip->i_inode.i_gid);
13621383

13631384
rgd->rd_free_clone -= *n;
1364-
trace_gfs2_block_alloc(ip, block, *n, GFS2_BLKST_USED);
1365-
*bn = block;
1366-
return 0;
1367-
1368-
rgrp_error:
1369-
gfs2_rgrp_error(rgd);
1370-
return -EIO;
1371-
}
1372-
1373-
/**
1374-
* gfs2_alloc_di - Allocate a dinode
1375-
* @dip: the directory that the inode is going in
1376-
* @bn: the block number which is allocated
1377-
* @generation: the generation number of the inode
1378-
*
1379-
* Returns: 0 on success or error
1380-
*/
1381-
1382-
int gfs2_alloc_di(struct gfs2_inode *dip, u64 *bn, u64 *generation)
1383-
{
1384-
struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1385-
struct gfs2_alloc *al = dip->i_alloc;
1386-
struct gfs2_rgrpd *rgd = dip->i_rgd;
1387-
u32 blk;
1388-
u64 block;
1389-
unsigned int n = 1;
1390-
1391-
blk = rgblk_search(rgd, rgd->rd_last_alloc,
1392-
GFS2_BLKST_FREE, GFS2_BLKST_DINODE, &n);
1393-
1394-
/* Since all blocks are reserved in advance, this shouldn't happen */
1395-
if (blk == BFITNOENT)
1396-
goto rgrp_error;
1397-
1398-
rgd->rd_last_alloc = blk;
1399-
block = rgd->rd_data0 + blk;
1400-
if (rgd->rd_free == 0)
1401-
goto rgrp_error;
1402-
1403-
rgd->rd_free--;
1404-
rgd->rd_dinodes++;
1405-
*generation = rgd->rd_igeneration++;
1406-
if (*generation == 0)
1407-
*generation = rgd->rd_igeneration++;
1408-
gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1409-
gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
1410-
1411-
al->al_alloced++;
1412-
1413-
gfs2_statfs_change(sdp, 0, -1, +1);
1414-
gfs2_trans_add_unrevoke(sdp, block, 1);
1415-
1416-
rgd->rd_free_clone--;
1417-
trace_gfs2_block_alloc(dip, block, 1, GFS2_BLKST_DINODE);
1385+
trace_gfs2_block_alloc(ip, block, *n, blk_type);
14181386
*bn = block;
14191387
return 0;
14201388

‎fs/gfs2/rgrp.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ static inline void gfs2_alloc_put(struct gfs2_inode *ip)
3939
extern int gfs2_inplace_reserve(struct gfs2_inode *ip);
4040
extern void gfs2_inplace_release(struct gfs2_inode *ip);
4141

42-
extern int gfs2_alloc_block(struct gfs2_inode *ip, u64 *bn, unsigned int *n);
43-
extern int gfs2_alloc_di(struct gfs2_inode *ip, u64 *bn, u64 *generation);
42+
extern int gfs2_alloc_block(struct gfs2_inode *ip, u64 *bn, unsigned int *n,
43+
int dinode, u64 *generation);
4444

4545
extern void __gfs2_free_blocks(struct gfs2_inode *ip, u64 bstart, u32 blen, int meta);
4646
extern void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen);

‎fs/gfs2/xattr.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ static int ea_alloc_blk(struct gfs2_inode *ip, struct buffer_head **bhp)
610610
u64 block;
611611
int error;
612612

613-
error = gfs2_alloc_block(ip, &block, &n);
613+
error = gfs2_alloc_block(ip, &block, &n, 0, NULL);
614614
if (error)
615615
return error;
616616
gfs2_trans_add_unrevoke(sdp, block, 1);
@@ -672,7 +672,7 @@ static int ea_write(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
672672
int mh_size = sizeof(struct gfs2_meta_header);
673673
unsigned int n = 1;
674674

675-
error = gfs2_alloc_block(ip, &block, &n);
675+
error = gfs2_alloc_block(ip, &block, &n, 0, NULL);
676676
if (error)
677677
return error;
678678
gfs2_trans_add_unrevoke(sdp, block, 1);
@@ -992,7 +992,7 @@ static int ea_set_block(struct gfs2_inode *ip, struct gfs2_ea_request *er,
992992
} else {
993993
u64 blk;
994994
unsigned int n = 1;
995-
error = gfs2_alloc_block(ip, &blk, &n);
995+
error = gfs2_alloc_block(ip, &blk, &n, 0, NULL);
996996
if (error)
997997
return error;
998998
gfs2_trans_add_unrevoke(sdp, blk, 1);

0 commit comments

Comments
 (0)