Skip to content

Commit 6e87ed0

Browse files
AstralBobswhiteho
authored andcommitted
GFS2: move toward a generic multi-block allocator
This patch is a revision of the one I previously posted. I tried to integrate all the suggestions Steve gave. The purpose of the patch is to change function gfs2_alloc_block (allocate either a dinode block or an extent of data blocks) to a more generic gfs2_alloc_blocks function that can allocate both a dinode _and_ an extent of data blocks in the same call. This will ultimately help us create a multi-block reservation scheme to reduce file fragmentation. This patch moves more toward a generic multi-block allocator that takes a pointer to the number of data blocks to allocate, plus whether or not to allocate a dinode. In theory, it could be called to allocate (1) a single dinode block, (2) a group of one or more data blocks, or (3) a dinode plus several data blocks. Signed-off-by: Bob Peterson <rpeterso@redhat.com> Signed-off-by: Steven Whitehouse <swhiteho@redhat.com>
1 parent 4442f2e commit 6e87ed0

6 files changed

Lines changed: 39 additions & 39 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, 0, NULL);
136+
error = gfs2_alloc_blocks(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, 0, NULL);
506+
error = gfs2_alloc_blocks(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, 0, NULL);
826+
error = gfs2_alloc_blocks(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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
389389
{
390390
struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
391391
int error;
392+
int dblocks = 0;
392393

393394
if (gfs2_alloc_get(dip) == NULL)
394395
return -ENOMEM;
@@ -402,7 +403,7 @@ static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
402403
if (error)
403404
goto out_ipreserv;
404405

405-
error = gfs2_alloc_block(dip, no_addr, NULL, 1, generation);
406+
error = gfs2_alloc_blocks(dip, no_addr, &dblocks, 1, generation);
406407

407408
gfs2_trans_end(sdp);
408409

‎fs/gfs2/rgrp.c‎

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ static const char valid_change[16] = {
6565
};
6666

6767
static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
68-
unsigned char old_state, unsigned char new_state,
69-
unsigned int *n);
68+
unsigned char old_state, bool dinode,
69+
unsigned int *ndata);
7070

7171
/**
7272
* gfs2_setbit - Set a bit in the bitmaps
@@ -921,8 +921,7 @@ static void try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked, u64 skip
921921
while (goal < rgd->rd_data) {
922922
down_write(&sdp->sd_log_flush_lock);
923923
n = 1;
924-
block = rgblk_search(rgd, goal, GFS2_BLKST_UNLINKED,
925-
GFS2_BLKST_UNLINKED, &n);
924+
block = rgblk_search(rgd, goal, GFS2_BLKST_UNLINKED, 0, &n);
926925
up_write(&sdp->sd_log_flush_lock);
927926
if (block == BFITNOENT)
928927
break;
@@ -1115,7 +1114,7 @@ static unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
11151114
* @rgd: the resource group descriptor
11161115
* @goal: the goal block within the RG (start here to search for avail block)
11171116
* @old_state: GFS2_BLKST_XXX the before-allocation state to find
1118-
* @new_state: GFS2_BLKST_XXX the after-allocation block state
1117+
* @dinode: TRUE if the first block we allocate is for a dinode
11191118
* @n: The extent length
11201119
*
11211120
* Walk rgrp's bitmap to find bits that represent a block in @old_state.
@@ -1132,8 +1131,7 @@ static unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
11321131
*/
11331132

11341133
static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
1135-
unsigned char old_state, unsigned char new_state,
1136-
unsigned int *n)
1134+
unsigned char old_state, bool dinode, unsigned int *n)
11371135
{
11381136
struct gfs2_bitmap *bi = NULL;
11391137
const u32 length = rgd->rd_length;
@@ -1192,13 +1190,14 @@ static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
11921190
if (blk == BFITNOENT)
11931191
return blk;
11941192

1195-
*n = 1;
1196-
if (old_state == new_state)
1193+
if (old_state == GFS2_BLKST_UNLINKED)
11971194
goto out;
11981195

11991196
gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
12001197
gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone, bi->bi_offset,
1201-
bi, blk, new_state);
1198+
bi, blk, dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED);
1199+
if (!dinode)
1200+
(*n)++;
12021201
goal = blk;
12031202
while (*n < elen) {
12041203
goal++;
@@ -1208,7 +1207,7 @@ static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
12081207
GFS2_BLKST_FREE)
12091208
break;
12101209
gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone, bi->bi_offset,
1211-
bi, goal, new_state);
1210+
bi, goal, GFS2_BLKST_USED);
12121211
(*n)++;
12131212
}
12141213
out:
@@ -1300,45 +1299,41 @@ static void gfs2_rgrp_error(struct gfs2_rgrpd *rgd)
13001299
}
13011300

13021301
/**
1303-
* gfs2_alloc_block - Allocate one or more blocks
1302+
* gfs2_alloc_blocks - Allocate one or more blocks of data and/or a dinode
13041303
* @ip: the inode to allocate the block for
13051304
* @bn: Used to return the starting block number
1306-
* @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
1305+
* @ndata: requested number of data blocks/extent length (value/result)
1306+
* @dinode: 1 if we're allocating a dinode block, else 0
13081307
* @generation: the generation number of the inode
13091308
*
13101309
* Returns: 0 or error
13111310
*/
13121311

1313-
int gfs2_alloc_block(struct gfs2_inode *ip, u64 *bn, unsigned int *n,
1314-
int dinode, u64 *generation)
1312+
int gfs2_alloc_blocks(struct gfs2_inode *ip, u64 *bn, unsigned int *ndata,
1313+
bool dinode, u64 *generation)
13151314
{
13161315
struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
13171316
struct buffer_head *dibh;
13181317
struct gfs2_alloc *al = ip->i_alloc;
13191318
struct gfs2_rgrpd *rgd;
1320-
u32 goal, blk; /* block, within the rgrp scope */
1319+
u32 goal, extlen, blk; /* block, within the rgrp scope */
13211320
u64 block; /* block, within the file system scope */
1322-
unsigned int extn = 1;
13231321
int error;
1324-
unsigned char blk_type = dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED;
13251322

13261323
/* Only happens if there is a bug in gfs2, return something distinctive
13271324
* to ensure that it is noticed.
13281325
*/
13291326
if (al == NULL)
13301327
return -ECANCELED;
13311328

1332-
if (n == NULL)
1333-
n = &extn;
13341329
rgd = ip->i_rgd;
13351330

13361331
if (!dinode && rgrp_contains_block(rgd, ip->i_goal))
13371332
goal = ip->i_goal - rgd->rd_data0;
13381333
else
13391334
goal = rgd->rd_last_alloc;
13401335

1341-
blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, blk_type, n);
1336+
blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, dinode, ndata);
13421337

13431338
/* Since all blocks are reserved in advance, this shouldn't happen */
13441339
if (blk == BFITNOENT)
@@ -1347,7 +1342,7 @@ int gfs2_alloc_block(struct gfs2_inode *ip, u64 *bn, unsigned int *n,
13471342
rgd->rd_last_alloc = blk;
13481343
block = rgd->rd_data0 + blk;
13491344
if (!dinode) {
1350-
ip->i_goal = block + *n - 1;
1345+
ip->i_goal = block + *ndata - 1;
13511346
error = gfs2_meta_inode_buffer(ip, &dibh);
13521347
if (error == 0) {
13531348
struct gfs2_dinode *di =
@@ -1358,10 +1353,13 @@ int gfs2_alloc_block(struct gfs2_inode *ip, u64 *bn, unsigned int *n,
13581353
brelse(dibh);
13591354
}
13601355
}
1361-
if (rgd->rd_free < *n)
1356+
extlen = *ndata;
1357+
if (dinode)
1358+
extlen++;
1359+
if (rgd->rd_free < extlen)
13621360
goto rgrp_error;
13631361

1364-
rgd->rd_free -= *n;
1362+
rgd->rd_free -= extlen;
13651363
if (dinode) {
13661364
rgd->rd_dinodes++;
13671365
*generation = rgd->rd_igeneration++;
@@ -1372,15 +1370,16 @@ int gfs2_alloc_block(struct gfs2_inode *ip, u64 *bn, unsigned int *n,
13721370
gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
13731371
gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
13741372

1375-
gfs2_statfs_change(sdp, 0, -(s64)*n, dinode ? 1 : 0);
1373+
gfs2_statfs_change(sdp, 0, -(s64)extlen, dinode ? 1 : 0);
13761374
if (dinode)
13771375
gfs2_trans_add_unrevoke(sdp, block, 1);
1378-
else
1379-
gfs2_quota_change(ip, *n, ip->i_inode.i_uid,
1376+
if (*ndata)
1377+
gfs2_quota_change(ip, *ndata, ip->i_inode.i_uid,
13801378
ip->i_inode.i_gid);
13811379

1382-
rgd->rd_free_clone -= *n;
1383-
trace_gfs2_block_alloc(ip, block, *n, blk_type);
1380+
rgd->rd_free_clone -= extlen;
1381+
trace_gfs2_block_alloc(ip, block, *ndata,
1382+
dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED);
13841383
*bn = block;
13851384
return 0;
13861385

‎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-
int dinode, u64 *generation);
42+
extern int gfs2_alloc_blocks(struct gfs2_inode *ip, u64 *bn, unsigned int *n,
43+
bool 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, 0, NULL);
613+
error = gfs2_alloc_blocks(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, 0, NULL);
675+
error = gfs2_alloc_blocks(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, 0, NULL);
995+
error = gfs2_alloc_blocks(ip, &blk, &n, 0, NULL);
996996
if (error)
997997
return error;
998998
gfs2_trans_add_unrevoke(sdp, blk, 1);

0 commit comments

Comments
 (0)