Skip to contentSkip to conversation

fs: add roundup_to_blocksize and bytes_to_blocks helper functions

PATCH In review Marcin Slusarz 3 participants 8 messages Apr 19, 2009 – Apr 26, 2009

Subsystems (11)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs: add roundup_to_blocksize and bytes_to_blocks helper functions

bytes_to_blocks is open coded in more than 30 places roundup_to_blocksize is open coded in more than 15 places

---
 include/linux/fs.h |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)
include/linux/fs.h +12 -0
index e766be0..1b4e5b6 100644
@@ -2451,5 +2451,17 @@ int proc_nr_files(struct ctl_table *table, int write, struct file *filp,
 
 int get_filesystem_list(char * buf);
 
+static inline
+unsigned long long roundup_to_blocksize(unsigned long long bytes, struct super_block *sb)
+{
+	return (bytes + sb->s_blocksize - 1) & ~(sb->s_blocksize - 1);
+}
+
+static inline
+blkcnt_t bytes_to_blocks(unsigned long long bytes, struct super_block *sb)
+{
+	return (bytes + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
+}
+
 #endif /* __KERNEL__ */
 #endif /* _LINUX_FS_H */
Signature · 1 line
1.6.0.6
#1 lore raw

PATCH 1/1 filesystems: start using roundup_to_blocksize and bytes_to_blocks

---
If requested I can create one patch per filesystem, but it would be easier if the first patch could be applied to 2.6.30.

---
 fs/adfs/inode.c         |    3 +-
 fs/btrfs/extent_io.c    |   10 ++----
 fs/btrfs/ioctl.c        |    2 +-
 fs/fat/cache.c          |    7 +---
 fs/hfs/extent.c         |    2 +-
 fs/hfs/inode.c          |    2 +-
 fs/hfsplus/extents.c    |    2 +-
 fs/hfsplus/inode.c      |    2 +-
 fs/jfs/xattr.c          |   25 +++++-----------
 fs/minix/itree_common.c |    2 +-
 fs/nfs/super.c          |   10 ++----
 fs/nilfs2/inode.c       |    4 +--
 fs/ntfs/aops.c          |    6 ++--
 fs/ocfs2/namei.c        |    2 +-
 fs/reiserfs/inode.c     |    5 +--
 fs/sysv/itree.c         |    7 +---
 fs/udf/inode.c          |   74 ++++++++++++++++++++--------------------------
 fs/udf/namei.c          |    2 +-
 fs/udf/truncate.c       |    9 ++----
 19 files changed, 67 insertions(+), 109 deletions(-)
fs/adfs/inode.c +1 -2
index e647200..8baec96 100644
@@ -273,8 +273,7 @@ adfs_iget(struct super_block *sb, struct object_info *obj)
 	inode->i_ino	 = obj->file_id;
 	inode->i_size	 = obj->size;
 	inode->i_nlink	 = 2;
-	inode->i_blocks	 = (inode->i_size + sb->s_blocksize - 1) >>
-			    sb->s_blocksize_bits;
+	inode->i_blocks	 = bytes_to_blocks(inode->i_size, sb);
 
 	/*
 	 * we need to save the parent directory ID so that
fs/btrfs/extent_io.c +3 -7
index eb2bee8..582bf76 100644
@@ -1959,7 +1959,6 @@ static int __extent_read_full_page(struct extent_io_tree *tree,
 	size_t page_offset = 0;
 	size_t iosize;
 	size_t disk_io_size;
-	size_t blocksize = inode->i_sb->s_blocksize;
 	unsigned long this_bio_flag = 0;
 
 	set_page_extent_mapped(page);
@@ -2008,7 +2007,7 @@ static int __extent_read_full_page(struct extent_io_tree *tree,
 
 		iosize = min(extent_map_end(em) - cur, end - cur + 1);
 		cur_end = min(extent_map_end(em) - 1, end);
-		iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
+		iosize = roundup_to_blocksize(iosize, inode->i_sb);
 		if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
 			disk_io_size = em->block_len;
 			sector = em->block_start >> 9;
@@ -2129,7 +2128,6 @@ static int __extent_writepage(struct page *page, struct writeback_control *wbc,
 	int ret;
 	int nr = 0;
 	size_t pg_offset = 0;
-	size_t blocksize;
 	loff_t i_size = i_size_read(inode);
 	unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
 	u64 nr_delalloc;
@@ -2221,7 +2219,6 @@ static int __extent_writepage(struct page *page, struct writeback_control *wbc,
 	}
 
 	set_extent_uptodate(tree, start, page_end, GFP_NOFS);
-	blocksize = inode->i_sb->s_blocksize;
 
 	while (cur <= end) {
 		if (cur >= last_byte) {
@@ -2244,7 +2241,7 @@ static int __extent_writepage(struct page *page, struct writeback_control *wbc,
 		BUG_ON(extent_map_end(em) <= cur);
 		BUG_ON(end < cur);
 		iosize = min(extent_map_end(em) - cur, end - cur + 1);
-		iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
+		iosize = roundup_to_blocksize(iosize, inode->i_sb);
 		sector = (em->block_start + extent_offset) >> 9;
 		bdev = em->bdev;
 		block_start = em->block_start;
@@ -2616,9 +2613,8 @@ int extent_invalidatepage(struct extent_io_tree *tree,
 {
 	u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
 	u64 end = start + PAGE_CACHE_SIZE - 1;
-	size_t blocksize = page->mapping->host->i_sb->s_blocksize;
 
-	start += (offset + blocksize - 1) & ~(blocksize - 1);
+	start += roundup_to_blocksize(offset, page->mapping->host->i_sb);
 	if (start > end)
 		return 0;
 
fs/btrfs/ioctl.c +1 -1
index 7594bec..1eeedbe 100644
@@ -804,7 +804,7 @@ static long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
 		olen = len = src->i_size - off;
 	/* if we extend to eof, continue to block boundary */
 	if (off + len == src->i_size)
-		len = ((src->i_size + bs-1) & ~(bs-1))
+		len = roundup_to_blocksize(src->i_size, root->fs_info->sb)
 			- off;
 
 	/* verify the end result is block aligned */
fs/fat/cache.c +2 -5
index b426022..93fe0ce 100644
@@ -297,8 +297,6 @@ int fat_bmap(struct inode *inode, sector_t sector, sector_t *phys,
 {
 	struct super_block *sb = inode->i_sb;
 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
-	const unsigned long blocksize = sb->s_blocksize;
-	const unsigned char blocksize_bits = sb->s_blocksize_bits;
 	sector_t last_block;
 	int cluster, offset;
 
@@ -312,7 +310,7 @@ int fat_bmap(struct inode *inode, sector_t sector, sector_t *phys,
 		return 0;
 	}
 
-	last_block = (i_size_read(inode) + (blocksize - 1)) >> blocksize_bits;
+	last_block = bytes_to_blocks(i_size_read(inode), inode->i_sb);
 	if (sector >= last_block) {
 		if (!create)
 			return 0;
@@ -321,8 +319,7 @@ int fat_bmap(struct inode *inode, sector_t sector, sector_t *phys,
 		 * ->mmu_private can access on only allocation path.
 		 * (caller must hold ->i_mutex)
 		 */
-		last_block = (MSDOS_I(inode)->mmu_private + (blocksize - 1))
-			>> blocksize_bits;
+		last_block = bytes_to_blocks(MSDOS_I(inode)->mmu_private, inode->i_sb);
 		if (sector >= last_block)
 			return 0;
 	}
fs/hfs/extent.c +1 -1
index 2c16316..6b0860d 100644
@@ -519,7 +519,7 @@ void hfs_file_truncate(struct inode *inode)
 	HFS_I(inode)->alloc_blocks = blk_cnt;
 out:
 	HFS_I(inode)->phys_size = inode->i_size;
-	HFS_I(inode)->fs_blocks = (inode->i_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
+	HFS_I(inode)->fs_blocks = bytes_to_blocks(inode->i_size, sb);
 	inode_set_bytes(inode, HFS_I(inode)->fs_blocks << sb->s_blocksize_bits);
 	mark_inode_dirty(inode);
 }
fs/hfs/inode.c +1 -1
index a1cbff2..25b1e2a 100644
@@ -243,7 +243,7 @@ void hfs_inode_read_fork(struct inode *inode, struct hfs_extent *ext,
 	HFS_I(inode)->first_blocks = count;
 
 	inode->i_size = HFS_I(inode)->phys_size = log_size;
-	HFS_I(inode)->fs_blocks = (log_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
+	HFS_I(inode)->fs_blocks = bytes_to_blocks(log_size, sb);
 	inode_set_bytes(inode, HFS_I(inode)->fs_blocks << sb->s_blocksize_bits);
 	HFS_I(inode)->alloc_blocks = be32_to_cpu(phys_size) /
 				     HFS_SB(sb)->alloc_blksz;
fs/hfsplus/extents.c +1 -1
index 0022eec..9245716 100644
@@ -500,7 +500,7 @@ void hfsplus_file_truncate(struct inode *inode)
 	HFSPLUS_I(inode).alloc_blocks = blk_cnt;
 out:
 	HFSPLUS_I(inode).phys_size = inode->i_size;
-	HFSPLUS_I(inode).fs_blocks = (inode->i_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
+	HFSPLUS_I(inode).fs_blocks = bytes_to_blocks(inode->i_size, sb);
 	inode_set_bytes(inode, HFSPLUS_I(inode).fs_blocks << sb->s_blocksize_bits);
 	mark_inode_dirty(inode);
 }
fs/hfsplus/inode.c +1 -1
index 1bcf597..0c95bd5 100644
@@ -377,7 +377,7 @@ void hfsplus_inode_read_fork(struct inode *inode, struct hfsplus_fork_raw *fork)
 
 	HFSPLUS_I(inode).alloc_blocks = be32_to_cpu(fork->total_blocks);
 	inode->i_size = HFSPLUS_I(inode).phys_size = be64_to_cpu(fork->total_size);
-	HFSPLUS_I(inode).fs_blocks = (inode->i_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
+	HFSPLUS_I(inode).fs_blocks = bytes_to_blocks(inode->i_size, sb);
 	inode_set_bytes(inode, HFSPLUS_I(inode).fs_blocks << sb->s_blocksize_bits);
 	HFSPLUS_I(inode).clump_blocks = be32_to_cpu(fork->clump_size) >> HFSPLUS_SB(sb).alloc_blksz_shift;
 	if (!HFSPLUS_I(inode).clump_blocks)
fs/jfs/xattr.c +8 -17
index 61dfa81..59aefb1 100644
@@ -257,7 +257,7 @@ static int ea_write(struct inode *ip, struct jfs_ea_list *ealist, int size,
 	}
 
 	/* figure out how many blocks we need */
-	nblocks = (size + (sb->s_blocksize - 1)) >> sb->s_blocksize_bits;
+	nblocks = bytes_to_blocks(size, sb);
 
 	/* Allocate new blocks to quota. */
 	if (vfs_dq_alloc_block(ip, nblocks)) {
@@ -284,9 +284,7 @@ static int ea_write(struct inode *ip, struct jfs_ea_list *ealist, int size,
 		 * the nearest aggregate block size
 		 */
 		nb = min(PSIZE, nbytes);
-		bytes_to_write =
-		    ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
-		    << sb->s_blocksize_bits;
+		bytes_to_write = roundup_to_blocksize(nb, sb);
 
 		if (!(mp = get_metapage(ip, blkno + i, bytes_to_write, 1))) {
 			rc = -EIO;
@@ -424,9 +422,7 @@ static int ea_read(struct inode *ip, struct jfs_ea_list *ealist)
 		 * the nearest aggregate block size
 		 */
 		nb = min(PSIZE, nbytes);
-		bytes_to_read =
-		    ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
-		    << sb->s_blocksize_bits;
+		bytes_to_read = roundup_to_blocksize(nb, sb);
 
 		if (!(mp = read_metapage(ip, blkno + i, bytes_to_read, 1)))
 			return -EIO;
@@ -505,8 +501,7 @@ static int ea_get(struct inode *inode, struct ea_buffer *ea_buf, int min_size)
 			jfs_error(sb, "ea_get: invalid ea.flag)");
 			return -EIO;
 		}
-		current_blocks = (ea_size + sb->s_blocksize - 1) >>
-		    sb->s_blocksize_bits;
+		current_blocks = bytes_to_blocks(ea_size, sb);
 	}
 	size = max(min_size, ea_size);
 
@@ -520,8 +515,7 @@ static int ea_get(struct inode *inode, struct ea_buffer *ea_buf, int min_size)
 			return -ENOMEM;
 
 		ea_buf->flag = EA_MALLOC;
-		ea_buf->max_size = (size + sb->s_blocksize - 1) &
-		    ~(sb->s_blocksize - 1);
+		ea_buf->max_size = roundup_to_blocksize(size, sb);
 
 		if (ea_size == 0)
 			return 0;
@@ -533,8 +527,7 @@ static int ea_get(struct inode *inode, struct ea_buffer *ea_buf, int min_size)
 		}
 		goto size_check;
 	}
-	blocks_needed = (min_size + sb->s_blocksize - 1) >>
-	    sb->s_blocksize_bits;
+	blocks_needed = bytes_to_blocks(min_size, sb);
 
 	if (blocks_needed > current_blocks) {
 		/* Allocate new blocks to quota. */
@@ -564,8 +557,7 @@ static int ea_get(struct inode *inode, struct ea_buffer *ea_buf, int min_size)
 			goto clean_up;
 		}
 		ea_buf->xattr = ea_buf->mp->data;
-		ea_buf->max_size = (min_size + sb->s_blocksize - 1) &
-		    ~(sb->s_blocksize - 1);
+		ea_buf->max_size = roundup_to_blocksize(min_size, sb);
 		if (ea_size == 0)
 			return 0;
 		if ((rc = ea_read(inode, ea_buf->xattr))) {
@@ -584,8 +576,7 @@ static int ea_get(struct inode *inode, struct ea_buffer *ea_buf, int min_size)
 		goto clean_up;
 	}
 	ea_buf->xattr = ea_buf->mp->data;
-	ea_buf->max_size = (ea_size + sb->s_blocksize - 1) &
-	    ~(sb->s_blocksize - 1);
+	ea_buf->max_size = roundup_to_blocksize(ea_size, sb);
 
       size_check:
 	if (EALIST_SIZE(ea_buf->xattr) != ea_size) {
fs/minix/itree_common.c +1 -1
index a731cab..52832b4 100644
@@ -302,7 +302,7 @@ static inline void truncate (struct inode * inode)
 	int first_whole;
 	long iblock;
 
-	iblock = (inode->i_size + sb->s_blocksize -1) >> sb->s_blocksize_bits;
+	iblock = bytes_to_blocks(inode->i_size, sb);
 	block_truncate_page(inode->i_mapping, inode->i_size, get_block);
 
 	n = block_to_path(inode, iblock, offsets);
fs/nfs/super.c +3 -7
index 6717200..b9be272 100644
@@ -385,8 +385,6 @@ void nfs_sb_deactive(struct super_block *sb)
 static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
 {
 	struct nfs_server *server = NFS_SB(dentry->d_sb);
-	unsigned char blockbits;
-	unsigned long blockres;
 	struct nfs_fh *fh = NFS_FH(dentry->d_inode);
 	struct nfs_fattr fattr;
 	struct nfs_fsstat res = {
@@ -414,11 +412,9 @@ static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
 	 * fields in units of f_bsize.
 	 */
 	buf->f_bsize = dentry->d_sb->s_blocksize;
-	blockbits = dentry->d_sb->s_blocksize_bits;
-	blockres = (1 << blockbits) - 1;
-	buf->f_blocks = (res.tbytes + blockres) >> blockbits;
-	buf->f_bfree = (res.fbytes + blockres) >> blockbits;
-	buf->f_bavail = (res.abytes + blockres) >> blockbits;
+	buf->f_blocks = roundup_to_blocksize(res.tbytes, dentry->d_sb);
+	buf->f_bfree = roundup_to_blocksize(res.fbytes, dentry->d_sb);
+	buf->f_bavail = roundup_to_blocksize(res.abytes, dentry->d_sb);
 
 	buf->f_files = res.tfiles;
 	buf->f_ffree = res.afiles;
fs/nilfs2/inode.c +1 -3
index 49ab4a4..0fae142 100644
@@ -581,7 +581,6 @@ static void nilfs_truncate_bmap(struct nilfs_inode_info *ii,
 void nilfs_truncate(struct inode *inode)
 {
 	unsigned long blkoff;
-	unsigned int blocksize;
 	struct nilfs_transaction_info ti;
 	struct super_block *sb = inode->i_sb;
 	struct nilfs_inode_info *ii = NILFS_I(inode);
@@ -591,8 +590,7 @@ void nilfs_truncate(struct inode *inode)
 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
 		return;
 
-	blocksize = sb->s_blocksize;
-	blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits;
+	blkoff = bytes_to_blocks(inode->i_size, sb);
 	nilfs_transaction_begin(sb, &ti, 0); /* never fails */
 
 	block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block);
fs/ntfs/aops.c +3 -3
index b38f944..3316812 100644
@@ -231,7 +231,7 @@ static int ntfs_read_block(struct page *page)
 	 */
 	iblock = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
 	read_lock_irqsave(&ni->size_lock, flags);
-	lblock = (ni->allocated_size + blocksize - 1) >> blocksize_bits;
+	lblock = bytes_to_blocks(ni->allocated_size, vol->sb);
 	init_size = ni->initialized_size;
 	i_size = i_size_read(vi);
 	read_unlock_irqrestore(&ni->size_lock, flags);
@@ -239,7 +239,7 @@ static int ntfs_read_block(struct page *page)
 		/* Race with shrinking truncate. */
 		init_size = i_size;
 	}
-	zblock = (init_size + blocksize - 1) >> blocksize_bits;
+	zblock = bytes_to_blocks(init_size, vol->sb);
 
 	/* Loop through all the buffers in the page. */
 	rl = NULL;
@@ -970,7 +970,7 @@ static int ntfs_write_mst_block(struct page *page,
 			(PAGE_CACHE_SHIFT - bh_size_bits);
 
 	/* The first out of bounds block for the data size. */
-	dblock = (i_size_read(vi) + bh_size - 1) >> bh_size_bits;
+	dblock = bytes_to_blocks(i_size_read(vi), vol->sb);
 
 	rl = NULL;
 	err = err2 = nr_bhs = nr_recs = nr_locked_nis = 0;
fs/ocfs2/namei.c +1 -1
index 2220f93..51ca438 100644
@@ -1481,7 +1481,7 @@ static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
 	bytes_left = i_size_read(inode) + 1;
 	/* we can't trust i_blocks because we're actually going to
 	 * write i_size + 1 bytes. */
-	blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
+	blocks = bytes_to_blocks(bytes_left, sb);
 
 	mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n",
 			(unsigned long long)inode->i_blocks,
fs/reiserfs/inode.c +1 -4
index 6fd0f47..6f1616f 100644
@@ -1053,7 +1053,6 @@ reiserfs_readpages(struct file *file, struct address_space *mapping,
 static int real_space_diff(struct inode *inode, int sd_size)
 {
 	int bytes;
-	loff_t blocksize = inode->i_sb->s_blocksize;
 
 	if (S_ISLNK(inode->i_mode) || S_ISDIR(inode->i_mode))
 		return sd_size;
@@ -1066,9 +1065,7 @@ static int real_space_diff(struct inode *inode, int sd_size)
 	 ** tail, we add 4 bytes to pretend there really is an unformatted
 	 ** node pointer
 	 */
-	bytes =
-	    ((inode->i_size +
-	      (blocksize - 1)) >> inode->i_sb->s_blocksize_bits) * UNFM_P_SIZE +
+	bytes = bytes_to_blocks(inode->i_size, inode->i_sb) * UNFM_P_SIZE +
 	    sd_size;
 	return bytes;
 }
fs/sysv/itree.c +2 -5
index f042eec..f221b6b 100644
@@ -369,15 +369,12 @@ void sysv_truncate (struct inode * inode)
 	sysv_zone_t nr = 0;
 	int n;
 	long iblock;
-	unsigned blocksize;
 
 	if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
 	    S_ISLNK(inode->i_mode)))
 		return;
 
-	blocksize = inode->i_sb->s_blocksize;
-	iblock = (inode->i_size + blocksize-1)
-					>> inode->i_sb->s_blocksize_bits;
+	iblock = bytes_to_blocks(inode->i_size, inode->i_sb);
 
 	block_truncate_page(inode->i_mapping, inode->i_size, get_block);
 
@@ -430,7 +427,7 @@ static unsigned sysv_nblocks(struct super_block *s, loff_t size)
 	struct sysv_sb_info *sbi = SYSV_SB(s);
 	int ptrs_bits = sbi->s_ind_per_block_bits;
 	unsigned blocks, res, direct = DIRECT, i = DEPTH;
-	blocks = (size + s->s_blocksize - 1) >> s->s_blocksize_bits;
+	blocks = bytes_to_blocks(size, s);
 	res = blocks;
 	while (--i && blocks > direct) {
 		blocks = ((blocks - direct - 1) >> ptrs_bits) + 1;
fs/udf/inode.c +32 -42

152 lines not on this page · raw message

fs/udf/namei.c +1 -1
index 6a29fa3..78550ef 100644
@@ -410,7 +410,7 @@ static struct fileIdentDesc *udf_add_entry(struct inode *dir,
 add:
 	/* Is there any extent whose size we need to round up? */
 	if (dinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB && elen) {
-		elen = (elen + sb->s_blocksize - 1) & ~(sb->s_blocksize - 1);
+		elen = roundup_to_blocksize(elen, sb);
 		if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
 			epos.offset -= sizeof(struct short_ad);
 		else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
fs/udf/truncate.c +3 -6
index 225527c..6fa3839 100644
@@ -32,10 +32,8 @@ static void extent_trunc(struct inode *inode, struct extent_position *epos,
 			 uint32_t nelen)
 {
 	struct kernel_lb_addr neloc = {};
-	int last_block = (elen + inode->i_sb->s_blocksize - 1) >>
-		inode->i_sb->s_blocksize_bits;
-	int first_block = (nelen + inode->i_sb->s_blocksize - 1) >>
-		inode->i_sb->s_blocksize_bits;
+	int last_block = bytes_to_blocks(elen, inode->i_sb);
+	int first_block = bytes_to_blocks(nelen, inode->i_sb);
 
 	if (nelen) {
 		if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
@@ -256,8 +254,7 @@ void udf_truncate_extents(struct inode *inode)
 						udf_get_lb_pblock(sb, &eloc, 0));
 				if (elen)
 					indirect_ext_len =
-						(elen + sb->s_blocksize - 1) >>
-						sb->s_blocksize_bits;
+						bytes_to_blocks(elen, sb);
 				else
 					indirect_ext_len = 1;
 			} else {
Signature · 1 line
1.6.0.6
#2 lore raw

Re: [PATCH] filesystems: start using roundup_to_blocksize and bytes_to_blocks

Looks good from a jfs perspective. Really cleans up that code.

Quoted 1 line · Marcin Slusarz
Quoted 3 lines

---
If requested I can create one patch per filesystem, but it would be easier if the first patch could be applied to 2.6.30.

If the first patch goes in by itself, I'd be happy to break out and push the jfs piece.

Thanks,
Shaggy

Quoted 643 lines
---
 fs/adfs/inode.c         |    3 +-
 fs/btrfs/extent_io.c    |   10 ++----
 fs/btrfs/ioctl.c        |    2 +-
 fs/fat/cache.c          |    7 +---
 fs/hfs/extent.c         |    2 +-
 fs/hfs/inode.c          |    2 +-
 fs/hfsplus/extents.c    |    2 +-
 fs/hfsplus/inode.c      |    2 +-
 fs/jfs/xattr.c          |   25 +++++-----------
 fs/minix/itree_common.c |    2 +-
 fs/nfs/super.c          |   10 ++----
 fs/nilfs2/inode.c       |    4 +--
 fs/ntfs/aops.c          |    6 ++--
 fs/ocfs2/namei.c        |    2 +-
 fs/reiserfs/inode.c     |    5 +--
 fs/sysv/itree.c         |    7 +---
 fs/udf/inode.c          |   74 ++++++++++++++++++++--------------------------
 fs/udf/namei.c          |    2 +-
 fs/udf/truncate.c       |    9 ++----
 19 files changed, 67 insertions(+), 109 deletions(-)
fs/adfs/inode.c +1 -2
index e647200..8baec96 100644
@@ -273,8 +273,7 @@ adfs_iget(struct super_block *sb, struct object_info *obj)
 	inode->i_ino	 = obj->file_id;
 	inode->i_size	 = obj->size;
 	inode->i_nlink	 = 2;
-	inode->i_blocks	 = (inode->i_size + sb->s_blocksize - 1) >>
-			    sb->s_blocksize_bits;
+	inode->i_blocks	 = bytes_to_blocks(inode->i_size, sb);
 
 	/*
 	 * we need to save the parent directory ID so that
fs/btrfs/extent_io.c +3 -7
index eb2bee8..582bf76 100644
@@ -1959,7 +1959,6 @@ static int __extent_read_full_page(struct extent_io_tree *tree,
 	size_t page_offset = 0;
 	size_t iosize;
 	size_t disk_io_size;
-	size_t blocksize = inode->i_sb->s_blocksize;
 	unsigned long this_bio_flag = 0;
 
 	set_page_extent_mapped(page);
@@ -2008,7 +2007,7 @@ static int __extent_read_full_page(struct extent_io_tree *tree,
 
 		iosize = min(extent_map_end(em) - cur, end - cur + 1);
 		cur_end = min(extent_map_end(em) - 1, end);
-		iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
+		iosize = roundup_to_blocksize(iosize, inode->i_sb);
 		if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
 			disk_io_size = em->block_len;
 			sector = em->block_start >> 9;
@@ -2129,7 +2128,6 @@ static int __extent_writepage(struct page *page, struct writeback_control *wbc,
 	int ret;
 	int nr = 0;
 	size_t pg_offset = 0;
-	size_t blocksize;
 	loff_t i_size = i_size_read(inode);
 	unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
 	u64 nr_delalloc;
@@ -2221,7 +2219,6 @@ static int __extent_writepage(struct page *page, struct writeback_control *wbc,
 	}
 
 	set_extent_uptodate(tree, start, page_end, GFP_NOFS);
-	blocksize = inode->i_sb->s_blocksize;
 
 	while (cur <= end) {
 		if (cur >= last_byte) {
@@ -2244,7 +2241,7 @@ static int __extent_writepage(struct page *page, struct writeback_control *wbc,
 		BUG_ON(extent_map_end(em) <= cur);
 		BUG_ON(end < cur);
 		iosize = min(extent_map_end(em) - cur, end - cur + 1);
-		iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
+		iosize = roundup_to_blocksize(iosize, inode->i_sb);
 		sector = (em->block_start + extent_offset) >> 9;
 		bdev = em->bdev;
 		block_start = em->block_start;
@@ -2616,9 +2613,8 @@ int extent_invalidatepage(struct extent_io_tree *tree,
 {
 	u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
 	u64 end = start + PAGE_CACHE_SIZE - 1;
-	size_t blocksize = page->mapping->host->i_sb->s_blocksize;
 
-	start += (offset + blocksize - 1) & ~(blocksize - 1);
+	start += roundup_to_blocksize(offset, page->mapping->host->i_sb);
 	if (start > end)
 		return 0;
 
fs/btrfs/ioctl.c +1 -1
index 7594bec..1eeedbe 100644
@@ -804,7 +804,7 @@ static long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
 		olen = len = src->i_size - off;
 	/* if we extend to eof, continue to block boundary */
 	if (off + len == src->i_size)
-		len = ((src->i_size + bs-1) & ~(bs-1))
+		len = roundup_to_blocksize(src->i_size, root->fs_info->sb)
 			- off;
 
 	/* verify the end result is block aligned */
fs/fat/cache.c +2 -5
index b426022..93fe0ce 100644
@@ -297,8 +297,6 @@ int fat_bmap(struct inode *inode, sector_t sector, sector_t *phys,
 {
 	struct super_block *sb = inode->i_sb;
 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
-	const unsigned long blocksize = sb->s_blocksize;
-	const unsigned char blocksize_bits = sb->s_blocksize_bits;
 	sector_t last_block;
 	int cluster, offset;
 
@@ -312,7 +310,7 @@ int fat_bmap(struct inode *inode, sector_t sector, sector_t *phys,
 		return 0;
 	}
 
-	last_block = (i_size_read(inode) + (blocksize - 1)) >> blocksize_bits;
+	last_block = bytes_to_blocks(i_size_read(inode), inode->i_sb);
 	if (sector >= last_block) {
 		if (!create)
 			return 0;
@@ -321,8 +319,7 @@ int fat_bmap(struct inode *inode, sector_t sector, sector_t *phys,
 		 * ->mmu_private can access on only allocation path.
 		 * (caller must hold ->i_mutex)
 		 */
-		last_block = (MSDOS_I(inode)->mmu_private + (blocksize - 1))
-			>> blocksize_bits;
+		last_block = bytes_to_blocks(MSDOS_I(inode)->mmu_private, inode->i_sb);
 		if (sector >= last_block)
 			return 0;
 	}
fs/hfs/extent.c +1 -1
index 2c16316..6b0860d 100644
@@ -519,7 +519,7 @@ void hfs_file_truncate(struct inode *inode)
 	HFS_I(inode)->alloc_blocks = blk_cnt;
 out:
 	HFS_I(inode)->phys_size = inode->i_size;
-	HFS_I(inode)->fs_blocks = (inode->i_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
+	HFS_I(inode)->fs_blocks = bytes_to_blocks(inode->i_size, sb);
 	inode_set_bytes(inode, HFS_I(inode)->fs_blocks << sb->s_blocksize_bits);
 	mark_inode_dirty(inode);
 }
fs/hfs/inode.c +1 -1
index a1cbff2..25b1e2a 100644
@@ -243,7 +243,7 @@ void hfs_inode_read_fork(struct inode *inode, struct hfs_extent *ext,
 	HFS_I(inode)->first_blocks = count;
 
 	inode->i_size = HFS_I(inode)->phys_size = log_size;
-	HFS_I(inode)->fs_blocks = (log_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
+	HFS_I(inode)->fs_blocks = bytes_to_blocks(log_size, sb);
 	inode_set_bytes(inode, HFS_I(inode)->fs_blocks << sb->s_blocksize_bits);
 	HFS_I(inode)->alloc_blocks = be32_to_cpu(phys_size) /
 				     HFS_SB(sb)->alloc_blksz;
fs/hfsplus/extents.c +1 -1
index 0022eec..9245716 100644
@@ -500,7 +500,7 @@ void hfsplus_file_truncate(struct inode *inode)
 	HFSPLUS_I(inode).alloc_blocks = blk_cnt;
 out:
 	HFSPLUS_I(inode).phys_size = inode->i_size;
-	HFSPLUS_I(inode).fs_blocks = (inode->i_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
+	HFSPLUS_I(inode).fs_blocks = bytes_to_blocks(inode->i_size, sb);
 	inode_set_bytes(inode, HFSPLUS_I(inode).fs_blocks << sb->s_blocksize_bits);
 	mark_inode_dirty(inode);
 }
fs/hfsplus/inode.c +1 -1
index 1bcf597..0c95bd5 100644
@@ -377,7 +377,7 @@ void hfsplus_inode_read_fork(struct inode *inode, struct hfsplus_fork_raw *fork)
 
 	HFSPLUS_I(inode).alloc_blocks = be32_to_cpu(fork->total_blocks);
 	inode->i_size = HFSPLUS_I(inode).phys_size = be64_to_cpu(fork->total_size);
-	HFSPLUS_I(inode).fs_blocks = (inode->i_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
+	HFSPLUS_I(inode).fs_blocks = bytes_to_blocks(inode->i_size, sb);
 	inode_set_bytes(inode, HFSPLUS_I(inode).fs_blocks << sb->s_blocksize_bits);
 	HFSPLUS_I(inode).clump_blocks = be32_to_cpu(fork->clump_size) >> HFSPLUS_SB(sb).alloc_blksz_shift;
 	if (!HFSPLUS_I(inode).clump_blocks)
fs/jfs/xattr.c +8 -17
index 61dfa81..59aefb1 100644
@@ -257,7 +257,7 @@ static int ea_write(struct inode *ip, struct jfs_ea_list *ealist, int size,
 	}
 
 	/* figure out how many blocks we need */
-	nblocks = (size + (sb->s_blocksize - 1)) >> sb->s_blocksize_bits;
+	nblocks = bytes_to_blocks(size, sb);
 
 	/* Allocate new blocks to quota. */
 	if (vfs_dq_alloc_block(ip, nblocks)) {
@@ -284,9 +284,7 @@ static int ea_write(struct inode *ip, struct jfs_ea_list *ealist, int size,
 		 * the nearest aggregate block size
 		 */
 		nb = min(PSIZE, nbytes);
-		bytes_to_write =
-		    ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
-		    << sb->s_blocksize_bits;
+		bytes_to_write = roundup_to_blocksize(nb, sb);
 
 		if (!(mp = get_metapage(ip, blkno + i, bytes_to_write, 1))) {
 			rc = -EIO;
@@ -424,9 +422,7 @@ static int ea_read(struct inode *ip, struct jfs_ea_list *ealist)
 		 * the nearest aggregate block size
 		 */
 		nb = min(PSIZE, nbytes);
-		bytes_to_read =
-		    ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
-		    << sb->s_blocksize_bits;
+		bytes_to_read = roundup_to_blocksize(nb, sb);
 
 		if (!(mp = read_metapage(ip, blkno + i, bytes_to_read, 1)))
 			return -EIO;
@@ -505,8 +501,7 @@ static int ea_get(struct inode *inode, struct ea_buffer *ea_buf, int min_size)
 			jfs_error(sb, "ea_get: invalid ea.flag)");
 			return -EIO;
 		}
-		current_blocks = (ea_size + sb->s_blocksize - 1) >>
-		    sb->s_blocksize_bits;
+		current_blocks = bytes_to_blocks(ea_size, sb);
 	}
 	size = max(min_size, ea_size);
 
@@ -520,8 +515,7 @@ static int ea_get(struct inode *inode, struct ea_buffer *ea_buf, int min_size)
 			return -ENOMEM;
 
 		ea_buf->flag = EA_MALLOC;
-		ea_buf->max_size = (size + sb->s_blocksize - 1) &
-		    ~(sb->s_blocksize - 1);
+		ea_buf->max_size = roundup_to_blocksize(size, sb);
 
 		if (ea_size == 0)
 			return 0;
@@ -533,8 +527,7 @@ static int ea_get(struct inode *inode, struct ea_buffer *ea_buf, int min_size)
 		}
 		goto size_check;
 	}
-	blocks_needed = (min_size + sb->s_blocksize - 1) >>
-	    sb->s_blocksize_bits;
+	blocks_needed = bytes_to_blocks(min_size, sb);
 
 	if (blocks_needed > current_blocks) {
 		/* Allocate new blocks to quota. */
@@ -564,8 +557,7 @@ static int ea_get(struct inode *inode, struct ea_buffer *ea_buf, int min_size)
 			goto clean_up;
 		}
 		ea_buf->xattr = ea_buf->mp->data;
-		ea_buf->max_size = (min_size + sb->s_blocksize - 1) &
-		    ~(sb->s_blocksize - 1);
+		ea_buf->max_size = roundup_to_blocksize(min_size, sb);
 		if (ea_size == 0)
 			return 0;
 		if ((rc = ea_read(inode, ea_buf->xattr))) {
@@ -584,8 +576,7 @@ static int ea_get(struct inode *inode, struct ea_buffer *ea_buf, int min_size)
 		goto clean_up;
 	}
 	ea_buf->xattr = ea_buf->mp->data;
-	ea_buf->max_size = (ea_size + sb->s_blocksize - 1) &
-	    ~(sb->s_blocksize - 1);
+	ea_buf->max_size = roundup_to_blocksize(ea_size, sb);
 
       size_check:
 	if (EALIST_SIZE(ea_buf->xattr) != ea_size) {
fs/minix/itree_common.c +1 -1
index a731cab..52832b4 100644
@@ -302,7 +302,7 @@ static inline void truncate (struct inode * inode)
 	int first_whole;
 	long iblock;
 
-	iblock = (inode->i_size + sb->s_blocksize -1) >> sb->s_blocksize_bits;
+	iblock = bytes_to_blocks(inode->i_size, sb);
 	block_truncate_page(inode->i_mapping, inode->i_size, get_block);
 
 	n = block_to_path(inode, iblock, offsets);
fs/nfs/super.c +3 -7
index 6717200..b9be272 100644
@@ -385,8 +385,6 @@ void nfs_sb_deactive(struct super_block *sb)
 static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
 {
 	struct nfs_server *server = NFS_SB(dentry->d_sb);
-	unsigned char blockbits;
-	unsigned long blockres;
 	struct nfs_fh *fh = NFS_FH(dentry->d_inode);
 	struct nfs_fattr fattr;
 	struct nfs_fsstat res = {
@@ -414,11 +412,9 @@ static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
 	 * fields in units of f_bsize.
 	 */
 	buf->f_bsize = dentry->d_sb->s_blocksize;
-	blockbits = dentry->d_sb->s_blocksize_bits;
-	blockres = (1 << blockbits) - 1;
-	buf->f_blocks = (res.tbytes + blockres) >> blockbits;
-	buf->f_bfree = (res.fbytes + blockres) >> blockbits;
-	buf->f_bavail = (res.abytes + blockres) >> blockbits;
+	buf->f_blocks = roundup_to_blocksize(res.tbytes, dentry->d_sb);
+	buf->f_bfree = roundup_to_blocksize(res.fbytes, dentry->d_sb);
+	buf->f_bavail = roundup_to_blocksize(res.abytes, dentry->d_sb);
 
 	buf->f_files = res.tfiles;
 	buf->f_ffree = res.afiles;
fs/nilfs2/inode.c +1 -3
index 49ab4a4..0fae142 100644
@@ -581,7 +581,6 @@ static void nilfs_truncate_bmap(struct nilfs_inode_info *ii,
 void nilfs_truncate(struct inode *inode)
 {
 	unsigned long blkoff;
-	unsigned int blocksize;
 	struct nilfs_transaction_info ti;
 	struct super_block *sb = inode->i_sb;
 	struct nilfs_inode_info *ii = NILFS_I(inode);
@@ -591,8 +590,7 @@ void nilfs_truncate(struct inode *inode)
 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
 		return;
 
-	blocksize = sb->s_blocksize;
-	blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits;
+	blkoff = bytes_to_blocks(inode->i_size, sb);
 	nilfs_transaction_begin(sb, &ti, 0); /* never fails */
 
 	block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block);
fs/ntfs/aops.c +3 -3
index b38f944..3316812 100644
@@ -231,7 +231,7 @@ static int ntfs_read_block(struct page *page)
 	 */
 	iblock = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
 	read_lock_irqsave(&ni->size_lock, flags);
-	lblock = (ni->allocated_size + blocksize - 1) >> blocksize_bits;
+	lblock = bytes_to_blocks(ni->allocated_size, vol->sb);
 	init_size = ni->initialized_size;
 	i_size = i_size_read(vi);
 	read_unlock_irqrestore(&ni->size_lock, flags);
@@ -239,7 +239,7 @@ static int ntfs_read_block(struct page *page)
 		/* Race with shrinking truncate. */
 		init_size = i_size;
 	}
-	zblock = (init_size + blocksize - 1) >> blocksize_bits;
+	zblock = bytes_to_blocks(init_size, vol->sb);
 
 	/* Loop through all the buffers in the page. */
 	rl = NULL;
@@ -970,7 +970,7 @@ static int ntfs_write_mst_block(struct page *page,
 			(PAGE_CACHE_SHIFT - bh_size_bits);
 
 	/* The first out of bounds block for the data size. */
-	dblock = (i_size_read(vi) + bh_size - 1) >> bh_size_bits;
+	dblock = bytes_to_blocks(i_size_read(vi), vol->sb);
 
 	rl = NULL;
 	err = err2 = nr_bhs = nr_recs = nr_locked_nis = 0;
fs/ocfs2/namei.c +1 -1
index 2220f93..51ca438 100644
@@ -1481,7 +1481,7 @@ static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
 	bytes_left = i_size_read(inode) + 1;
 	/* we can't trust i_blocks because we're actually going to
 	 * write i_size + 1 bytes. */
-	blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
+	blocks = bytes_to_blocks(bytes_left, sb);
 
 	mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n",
 			(unsigned long long)inode->i_blocks,
fs/reiserfs/inode.c +1 -4
index 6fd0f47..6f1616f 100644
@@ -1053,7 +1053,6 @@ reiserfs_readpages(struct file *file, struct address_space *mapping,
 static int real_space_diff(struct inode *inode, int sd_size)
 {
 	int bytes;
-	loff_t blocksize = inode->i_sb->s_blocksize;
 
 	if (S_ISLNK(inode->i_mode) || S_ISDIR(inode->i_mode))
 		return sd_size;
@@ -1066,9 +1065,7 @@ static int real_space_diff(struct inode *inode, int sd_size)
 	 ** tail, we add 4 bytes to pretend there really is an unformatted
 	 ** node pointer
 	 */
-	bytes =
-	    ((inode->i_size +
-	      (blocksize - 1)) >> inode->i_sb->s_blocksize_bits) * UNFM_P_SIZE +
+	bytes = bytes_to_blocks(inode->i_size, inode->i_sb) * UNFM_P_SIZE +
 	    sd_size;
 	return bytes;
 }
fs/sysv/itree.c +2 -5
index f042eec..f221b6b 100644
@@ -369,15 +369,12 @@ void sysv_truncate (struct inode * inode)
 	sysv_zone_t nr = 0;
 	int n;
 	long iblock;
-	unsigned blocksize;
 
 	if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
 	    S_ISLNK(inode->i_mode)))
 		return;
 
-	blocksize = inode->i_sb->s_blocksize;
-	iblock = (inode->i_size + blocksize-1)
-					>> inode->i_sb->s_blocksize_bits;
+	iblock = bytes_to_blocks(inode->i_size, inode->i_sb);
 
 	block_truncate_page(inode->i_mapping, inode->i_size, get_block);
 
@@ -430,7 +427,7 @@ static unsigned sysv_nblocks(struct super_block *s, loff_t size)
 	struct sysv_sb_info *sbi = SYSV_SB(s);
 	int ptrs_bits = sbi->s_ind_per_block_bits;
 	unsigned blocks, res, direct = DIRECT, i = DEPTH;
-	blocks = (size + s->s_blocksize - 1) >> s->s_blocksize_bits;
+	blocks = bytes_to_blocks(size, s);
 	res = blocks;
 	while (--i && blocks > direct) {
 		blocks = ((blocks - direct - 1) >> ptrs_bits) + 1;
fs/udf/inode.c +32 -42
index e7533f7..55b20e2 100644
@@ -376,13 +376,9 @@ int udf_extend_file(struct inode *inode, struct extent_position *last_pos,
 	iinfo = UDF_I(inode);
 	/* Round the last extent up to a multiple of block size */
 	if (last_ext->extLength & (sb->s_blocksize - 1)) {
-		last_ext->extLength =
-			(last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
-			(((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
-			  sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
-		iinfo->i_lenExtents =
-			(iinfo->i_lenExtents + sb->s_blocksize - 1) &
-			~(sb->s_blocksize - 1);
+		last_ext->extLength = (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
+			roundup_to_blocksize(last_ext->extLength & UDF_EXTENT_LENGTH_MASK, sb);
+		iinfo->i_lenExtents = roundup_to_blocksize(iinfo->i_lenExtents, sb);
 	}
 
 	/* Last extent are just preallocated blocks? */
@@ -527,8 +523,7 @@ static struct buffer_head *inode_getblk(struct inode *inode, sector_t block,
 
 		if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
 			pgoal = eloc.logicalBlockNum +
-				((elen + inode->i_sb->s_blocksize - 1) >>
-				 inode->i_sb->s_blocksize_bits);
+				bytes_to_blocks(elen, inode->i_sb);
 
 		count++;
 	} while (lbcount + elen <= b_off);
@@ -548,8 +543,7 @@ static struct buffer_head *inode_getblk(struct inode *inode, sector_t block,
 	if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
 		if (elen & (inode->i_sb->s_blocksize - 1)) {
 			elen = EXT_RECORDED_ALLOCATED |
-				((elen + inode->i_sb->s_blocksize - 1) &
-				 ~(inode->i_sb->s_blocksize - 1));
+				roundup_to_blocksize(elen, inode->i_sb);
 			etype = udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
 		}
 		brelse(prev_epos.bh);
@@ -709,8 +703,9 @@ static void udf_split_extents(struct inode *inode, int *c, int offset,
 	    (laarr[*c].extLength >> 30) ==
 				(EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
 		int curr = *c;
-		int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
-			    blocksize - 1) >> blocksize_bits;
+		int blen = bytes_to_blocks(
+				laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK,
+				inode->i_sb);
 		int8_t etype = (laarr[curr].extLength >> 30);
 
 		if (blen == 1)
@@ -778,10 +773,8 @@ static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
 					(EXT_NOT_RECORDED_ALLOCATED >> 30)) {
 			start = c + 1;
 			length = currlength =
-				(((laarr[c + 1].extLength &
-					UDF_EXTENT_LENGTH_MASK) +
-				inode->i_sb->s_blocksize - 1) >>
-				inode->i_sb->s_blocksize_bits);
+				bytes_to_blocks(laarr[c + 1].extLength &
+					UDF_EXTENT_LENGTH_MASK, inode->i_sb);
 		} else
 			start = c;
 	}
@@ -792,19 +785,17 @@ static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
 				length += UDF_DEFAULT_PREALLOC_BLOCKS;
 		} else if ((laarr[i].extLength >> 30) ==
 				(EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
-			length += (((laarr[i].extLength &
-						UDF_EXTENT_LENGTH_MASK) +
-				    inode->i_sb->s_blocksize - 1) >>
-				    inode->i_sb->s_blocksize_bits);
+			length += bytes_to_blocks(laarr[i].extLength &
+						UDF_EXTENT_LENGTH_MASK,
+						inode->i_sb);
 		} else
 			break;
 	}
 
 	if (length) {
 		int next = laarr[start].extLocation.logicalBlockNum +
-			(((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
-			  inode->i_sb->s_blocksize - 1) >>
-			  inode->i_sb->s_blocksize_bits);
+			bytes_to_blocks(laarr[start].extLength &
+					UDF_EXTENT_LENGTH_MASK, inode->i_sb);
 		int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
 				laarr[start].extLocation.partitionReferenceNum,
 				next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
@@ -831,10 +822,9 @@ static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
 			}
 
 			for (i = start + 1; numalloc && i < *endnum; i++) {
-				int elen = ((laarr[i].extLength &
-						UDF_EXTENT_LENGTH_MASK) +
-					    inode->i_sb->s_blocksize - 1) >>
-					    inode->i_sb->s_blocksize_bits;
+				int elen = bytes_to_blocks(laarr[i].extLength &
+						UDF_EXTENT_LENGTH_MASK,
+						inode->i_sb);
 
 				if (elen > numalloc) {
 					laarr[i].extLength -=
@@ -875,8 +865,9 @@ static void udf_merge_extents(struct inode *inode,
 				(EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
 			((lip1->extLocation.logicalBlockNum -
 			  li->extLocation.logicalBlockNum) ==
-			(((li->extLength & UDF_EXTENT_LENGTH_MASK) +
-			blocksize - 1) >> blocksize_bits)))) {
+					bytes_to_blocks(li->extLength &
+							UDF_EXTENT_LENGTH_MASK,
+							inode->i_sb)))) {
 
 			if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
 				(lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
@@ -897,9 +888,9 @@ static void udf_merge_extents(struct inode *inode,
 						blocksize_bits);
 			} else {
 				li->extLength = lip1->extLength +
-					(((li->extLength &
-						UDF_EXTENT_LENGTH_MASK) +
-					 blocksize - 1) & ~(blocksize - 1));
+					roundup_to_blocksize(li->extLength &
+						UDF_EXTENT_LENGTH_MASK,
+						inode->i_sb);
 				if (*endnum > (i + 2))
 					memmove(&laarr[i + 1], &laarr[i + 2],
 						sizeof(struct long_ad) *
@@ -912,9 +903,8 @@ static void udf_merge_extents(struct inode *inode,
 			   ((lip1->extLength >> 30) ==
 				(EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
 			udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
-					((li->extLength &
-					  UDF_EXTENT_LENGTH_MASK) +
-					 blocksize - 1) >> blocksize_bits);
+					bytes_to_blocks(li->extLength &
+					  UDF_EXTENT_LENGTH_MASK, inode->i_sb));
 			li->extLocation.logicalBlockNum = 0;
 			li->extLocation.partitionReferenceNum = 0;
 
@@ -932,9 +922,9 @@ static void udf_merge_extents(struct inode *inode,
 						blocksize;
 			} else {
 				li->extLength = lip1->extLength +
-					(((li->extLength &
-						UDF_EXTENT_LENGTH_MASK) +
-					  blocksize - 1) & ~(blocksize - 1));
+					roundup_to_blocksize(li->extLength &
+						UDF_EXTENT_LENGTH_MASK,
+						inode->i_sb);
 				if (*endnum > (i + 2))
 					memmove(&laarr[i + 1], &laarr[i + 2],
 						sizeof(struct long_ad) *
@@ -946,9 +936,9 @@ static void udf_merge_extents(struct inode *inode,
 					(EXT_NOT_RECORDED_ALLOCATED >> 30)) {
 			udf_free_blocks(inode->i_sb, inode,
 					&li->extLocation, 0,
-					((li->extLength &
-						UDF_EXTENT_LENGTH_MASK) +
-					 blocksize - 1) >> blocksize_bits);
+					bytes_to_blocks(li->extLength &
+						UDF_EXTENT_LENGTH_MASK,
+						inode->i_sb));
 			li->extLocation.logicalBlockNum = 0;
 			li->extLocation.partitionReferenceNum = 0;
 			li->extLength = (li->extLength &
fs/udf/namei.c +1 -1
index 6a29fa3..78550ef 100644
@@ -410,7 +410,7 @@ static struct fileIdentDesc *udf_add_entry(struct inode *dir,
 add:
 	/* Is there any extent whose size we need to round up? */
 	if (dinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB && elen) {
-		elen = (elen + sb->s_blocksize - 1) & ~(sb->s_blocksize - 1);
+		elen = roundup_to_blocksize(elen, sb);
 		if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
 			epos.offset -= sizeof(struct short_ad);
 		else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
fs/udf/truncate.c +3 -6
index 225527c..6fa3839 100644
@@ -32,10 +32,8 @@ static void extent_trunc(struct inode *inode, struct extent_position *epos,
 			 uint32_t nelen)
 {
 	struct kernel_lb_addr neloc = {};
-	int last_block = (elen + inode->i_sb->s_blocksize - 1) >>
-		inode->i_sb->s_blocksize_bits;
-	int first_block = (nelen + inode->i_sb->s_blocksize - 1) >>
-		inode->i_sb->s_blocksize_bits;
+	int last_block = bytes_to_blocks(elen, inode->i_sb);
+	int first_block = bytes_to_blocks(nelen, inode->i_sb);
 
 	if (nelen) {
 		if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
@@ -256,8 +254,7 @@ void udf_truncate_extents(struct inode *inode)
 						udf_get_lb_pblock(sb, &eloc, 0));
 				if (elen)
 					indirect_ext_len =
-						(elen + sb->s_blocksize - 1) >>
-						sb->s_blocksize_bits;
+						bytes_to_blocks(elen, sb);
 				else
 					indirect_ext_len = 1;
 			} else {
Signature · 2 lines
David Kleikamp
IBM Linux Technology Center
Acked-by
#3 lore raw

Re: [PATCH] filesystems: start using roundup_to_blocksize and bytes_to_blocks

Quoted 4 lines · Dave Kleikamp

Looks good from a jfs perspective. Really cleans up that code.

Quoted 1 line · Marcin Slusarz

Sorry. Should be acked-by rather than signed-off-by, and I really didn't need to quote the whole patch. Blame it on Monday.

Thanks,
Shaggy

Signature · 2 lines
David Kleikamp
IBM Linux Technology Center
#4 lore raw
In reply to Marcin Slusarz
Quoted 3 lines · On Sun, 19 Apr 2009 20:42:15 +0200 Marcin Slusarz <[email protected]>…

bytes_to_blocks is open coded in more than 30 places roundup_to_blocksize is open coded in more than 15 places

It would be nice to document these a bit. Kernel-wide helpers..

Quoted 9 lines
---
 include/linux/fs.h |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)
include/linux/fs.h +0 -0
index e766be0..1b4e5b6 100644

include/linux/fs.h int proc_nr_files(struct ctl_table *table, int write, struct file *filp, · line 2457

Quoted 7 lines
include/linux/fs.h +4 -0
@@ -2451,5 +2451,17 @@ int proc_nr_files(struct ctl_table *table, int write, struct file *filp,
 
 int get_filesystem_list(char * buf);
 
+static inline
+unsigned long long roundup_to_blocksize(unsigned long long bytes, struct super_block *sb)
+{
+	return (bytes + sb->s_blocksize - 1) & ~(sb->s_blocksize - 1);

This is ALIGN(bytes, sb->s_blocksize). Dunno if that's any clearer though.

include/linux/fs.h int proc_nr_files(struct ctl_table *table, int write, struct file *filp, · line 2464

Quoted 7 lines
include/linux/fs.h +7 -0
+}
+
+static inline
+blkcnt_t bytes_to_blocks(unsigned long long bytes, struct super_block *sb)
+{
+	return (bytes + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
+}

I wonder whether `unsigned long long' was the best choice of type here.

Probably `bytes' should be loff_t.

roundup_to_blocksize() could/should return loff_t too, I think.

roundup_to_blocksize() isn't a terribly good name, IMO. The name conveys no sense of what the function returns. It _sounds_ like it returns a "block size". But it doesn't - it returns a loff_t. round_up_file_offset_to_block_size() is a bit long though :(

#5 lore raw
Quoted 4 lines · Andrew Morton

roundup_to_blocksize() isn't a terribly good name, IMO. The name conveys no sense of what the function returns. It _sounds_ like it returns a "block size". But it doesn't - it returns a loff_t. round_up_file_offset_to_block_size() is a bit long though :(

Is round_up_to_block() any better? I don't think the trailing "size" helps.

Shaggy

Signature · 2 lines
David Kleikamp
IBM Linux Technology Center
#6 lore raw
Quoted 9 lines · On Wed, 22 Apr 2009 16:40:00 -0500 Dave Kleikamp <[email protected]>…
Quoted 4 lines · Andrew Morton

roundup_to_blocksize() isn't a terribly good name, IMO. The name conveys no sense of what the function returns. It _sounds_ like it returns a "block size". But it doesn't - it returns a loff_t. round_up_file_offset_to_block_size() is a bit long though :(

Is round_up_to_block() any better? I don't think the trailing "size" helps.

dunno.

If the function is documented and if its arg is a loff_t rather than a bare ull then I guess that makes things sufficiently clear that we don't have to fuss about its name too much.

#7 lore raw
In reply to Andrew Morton

Andrew Morton pisze:

Quoted 8 lines
Quoted 3 lines · On Sun, 19 Apr 2009 20:42:15 +0200 Marcin Slusarz <[email protected]>…

bytes_to_blocks is open coded in more than 30 places roundup_to_blocksize is open coded in more than 15 places

It would be nice to document these a bit. Kernel-wide helpers..

ok

Quoted 11 lines
Quoted 11 lines
---
 include/linux/fs.h |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)
include/linux/fs.h +0 -0
index e766be0..1b4e5b6 100644
@@ -2451,5 +2451,17 @@ int proc_nr_files(struct ctl_table *table, int write, struct file *filp,
 
Quoted 8 lines

include/linux/fs.h int proc_nr_files(struct ctl_table *table, int write, struct file *filp, · line 2457

Quoted 6 lines
include/linux/fs.h +4 -0
 int get_filesystem_list(char * buf);
 
+static inline
+unsigned long long roundup_to_blocksize(unsigned long long bytes, struct super_block *sb)
+{
+	return (bytes + sb->s_blocksize - 1) & ~(sb->s_blocksize - 1);

This is ALIGN(bytes, sb->s_blocksize). Dunno if that's any clearer though.

thanks, I didn't know about it

Quoted 11 lines

include/linux/fs.h int proc_nr_files(struct ctl_table *table, int write, struct file *filp, · line 2464

Quoted 7 lines
include/linux/fs.h +7 -0
+}
+
+static inline
+blkcnt_t bytes_to_blocks(unsigned long long bytes, struct super_block *sb)
+{
+	return (bytes + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
+}

I wonder whether `unsigned long long' was the best choice of type here.

Probably `bytes' should be loff_t.

loff_t is long long.

So bytes_to_blocks(-6000, sb/*bs=4096, bsb=12*/) would return 4503599627370495

Quoted 1 line

roundup_to_blocksize() could/should return loff_t too, I think.

And roundup_to_blocksize(-6000, sb/*bs=4096, bsb=12*/) would return -4096

Bit surprising ;)

Quoted 4 lines

roundup_to_blocksize() isn't a terribly good name, IMO. The name conveys no sense of what the function returns. It _sounds_ like it returns a "block size". But it doesn't - it returns a loff_t. round_up_file_offset_to_block_size() is a bit long though :(

I can't find any better name, sorry.

Marcin