GFS2: Streamline alloc calculations for writes

This patch removes some unused code, and make the calculation
of the number of blocks required conditional in order to reduce
the number of times this (potentially expensive) calculation
is done.

Signed-off-by: Steven Whitehouse <swhiteho@redhat.com>

+42 -49
+7 -42
fs/gfs2/bmap.c
··· 1231 1231 } 1232 1232 1233 1233 /** 1234 - * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file 1235 - * @ip: the file 1236 - * @len: the number of bytes to be written to the file 1237 - * @data_blocks: returns the number of data blocks required 1238 - * @ind_blocks: returns the number of indirect blocks required 1239 - * 1240 - */ 1241 - 1242 - void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len, 1243 - unsigned int *data_blocks, unsigned int *ind_blocks) 1244 - { 1245 - struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 1246 - unsigned int tmp; 1247 - 1248 - if (gfs2_is_dir(ip)) { 1249 - *data_blocks = DIV_ROUND_UP(len, sdp->sd_jbsize) + 2; 1250 - *ind_blocks = 3 * (sdp->sd_max_jheight - 1); 1251 - } else { 1252 - *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3; 1253 - *ind_blocks = 3 * (sdp->sd_max_height - 1); 1254 - } 1255 - 1256 - for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) { 1257 - tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs); 1258 - *ind_blocks += tmp; 1259 - } 1260 - } 1261 - 1262 - /** 1263 1234 * gfs2_write_alloc_required - figure out if a write will require an allocation 1264 1235 * @ip: the file being written to 1265 1236 * @offset: the offset to write to ··· 1247 1276 struct buffer_head bh; 1248 1277 unsigned int shift; 1249 1278 u64 lblock, lblock_stop, size; 1279 + u64 end_of_file; 1250 1280 1251 1281 *alloc_required = 0; 1252 1282 ··· 1263 1291 1264 1292 *alloc_required = 1; 1265 1293 shift = sdp->sd_sb.sb_bsize_shift; 1266 - if (gfs2_is_dir(ip)) { 1267 - unsigned int bsize = sdp->sd_jbsize; 1268 - lblock = offset; 1269 - do_div(lblock, bsize); 1270 - lblock_stop = offset + len + bsize - 1; 1271 - do_div(lblock_stop, bsize); 1272 - } else { 1273 - u64 end_of_file = (ip->i_disksize + sdp->sd_sb.sb_bsize - 1) >> shift; 1274 - lblock = offset >> shift; 1275 - lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift; 1276 - if (lblock_stop > end_of_file) 1277 - return 0; 1278 - } 1294 + BUG_ON(gfs2_is_dir(ip)); 1295 + end_of_file = (ip->i_disksize + sdp->sd_sb.sb_bsize - 1) >> shift; 1296 + lblock = offset >> shift; 1297 + lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift; 1298 + if (lblock_stop > end_of_file) 1299 + return 0; 1279 1300 1280 1301 size = (lblock_stop - lblock) << shift; 1281 1302 do {
+30 -4
fs/gfs2/bmap.h
··· 10 10 #ifndef __BMAP_DOT_H__ 11 11 #define __BMAP_DOT_H__ 12 12 13 + #include "inode.h" 14 + 13 15 struct inode; 14 16 struct gfs2_inode; 15 17 struct page; 18 + 19 + 20 + /** 21 + * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file 22 + * @ip: the file 23 + * @len: the number of bytes to be written to the file 24 + * @data_blocks: returns the number of data blocks required 25 + * @ind_blocks: returns the number of indirect blocks required 26 + * 27 + */ 28 + 29 + static inline void gfs2_write_calc_reserv(const struct gfs2_inode *ip, 30 + unsigned int len, 31 + unsigned int *data_blocks, 32 + unsigned int *ind_blocks) 33 + { 34 + const struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 35 + unsigned int tmp; 36 + 37 + BUG_ON(gfs2_is_dir(ip)); 38 + *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3; 39 + *ind_blocks = 3 * (sdp->sd_max_height - 1); 40 + 41 + for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) { 42 + tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs); 43 + *ind_blocks += tmp; 44 + } 45 + } 16 46 17 47 int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page); 18 48 int gfs2_block_map(struct inode *inode, sector_t lblock, struct buffer_head *bh, int create); ··· 51 21 int gfs2_truncatei(struct gfs2_inode *ip, u64 size); 52 22 int gfs2_truncatei_resume(struct gfs2_inode *ip); 53 23 int gfs2_file_dealloc(struct gfs2_inode *ip); 54 - 55 - void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len, 56 - unsigned int *data_blocks, 57 - unsigned int *ind_blocks); 58 24 int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset, 59 25 unsigned int len, int *alloc_required); 60 26
+4 -2
fs/gfs2/ops_address.c
··· 625 625 { 626 626 struct gfs2_inode *ip = GFS2_I(mapping->host); 627 627 struct gfs2_sbd *sdp = GFS2_SB(mapping->host); 628 - unsigned int data_blocks, ind_blocks, rblocks; 628 + unsigned int data_blocks = 0, ind_blocks = 0, rblocks; 629 629 int alloc_required; 630 630 int error = 0; 631 631 struct gfs2_alloc *al; ··· 639 639 if (unlikely(error)) 640 640 goto out_uninit; 641 641 642 - gfs2_write_calc_reserv(ip, len, &data_blocks, &ind_blocks); 643 642 error = gfs2_write_alloc_required(ip, pos, len, &alloc_required); 644 643 if (error) 645 644 goto out_unlock; 645 + 646 + if (alloc_required || gfs2_is_jdata(ip)) 647 + gfs2_write_calc_reserv(ip, len, &data_blocks, &ind_blocks); 646 648 647 649 if (alloc_required) { 648 650 al = gfs2_alloc_get(ip);
+1 -1
fs/gfs2/ops_file.c
··· 355 355 goto out; 356 356 357 357 set_bit(GIF_SW_PAGED, &ip->i_flags); 358 - gfs2_write_calc_reserv(ip, PAGE_CACHE_SIZE, &data_blocks, &ind_blocks); 359 358 ret = gfs2_write_alloc_required(ip, pos, PAGE_CACHE_SIZE, &alloc_required); 360 359 if (ret || !alloc_required) 361 360 goto out_unlock; ··· 366 367 ret = gfs2_quota_lock_check(ip); 367 368 if (ret) 368 369 goto out_alloc_put; 370 + gfs2_write_calc_reserv(ip, PAGE_CACHE_SIZE, &data_blocks, &ind_blocks); 369 371 al->al_requested = data_blocks + ind_blocks; 370 372 ret = gfs2_inplace_reserve(ip); 371 373 if (ret)