From mboxrd@z Thu Jan 1 00:00:00 1970 From: Andrew Price Date: Thu, 3 Apr 2014 16:12:35 +0100 Subject: [Cluster-devel] [PATCH 02/14] libgfs2: Add lgfs2_space_for_data() In-Reply-To: <1396537967-12399-1-git-send-email-anprice@redhat.com> References: <1396537967-12399-1-git-send-email-anprice@redhat.com> Message-ID: <1396537967-12399-3-git-send-email-anprice@redhat.com> List-Id: To: cluster-devel.redhat.com MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Add a function which calculates the total number of blocks required for a file given the size of its contents. Signed-off-by: Andrew Price --- gfs2/libgfs2/fs_ops.c | 18 ++++++++++++++++++ gfs2/libgfs2/libgfs2.h | 2 ++ 2 files changed, 20 insertions(+) diff --git a/gfs2/libgfs2/fs_ops.c b/gfs2/libgfs2/fs_ops.c index 198f6eb..b95f2ed 100644 --- a/gfs2/libgfs2/fs_ops.c +++ b/gfs2/libgfs2/fs_ops.c @@ -273,6 +273,24 @@ void unstuff_dinode(struct gfs2_inode *ip) ip->i_di.di_height = 1; } +/** + * Calculate the total number of blocks required by a file containing 'bytes' bytes of data. + */ +uint64_t lgfs2_space_for_data(const struct gfs2_sbd *sdp, const unsigned bsize, const uint64_t bytes) +{ + uint64_t blks = (bytes + bsize - 1) / bsize; + uint64_t ptrs = blks; + + if (bytes <= bsize - sizeof(struct gfs2_dinode)) + return 1; + + while (ptrs > sdp->sd_diptrs) { + ptrs = (ptrs + sdp->sd_inptrs - 1) / sdp->sd_inptrs; + blks += ptrs; + } + return blks + 1; +} + unsigned int calc_tree_height(struct gfs2_inode *ip, uint64_t size) { struct gfs2_sbd *sdp = ip->i_sbd; diff --git a/gfs2/libgfs2/libgfs2.h b/gfs2/libgfs2/libgfs2.h index 6248e4b..ce51e8c 100644 --- a/gfs2/libgfs2/libgfs2.h +++ b/gfs2/libgfs2/libgfs2.h @@ -450,6 +450,8 @@ extern void inode_put(struct gfs2_inode **ip); extern uint64_t data_alloc(struct gfs2_inode *ip); extern int lgfs2_meta_alloc(struct gfs2_inode *ip, uint64_t *blkno); extern int lgfs2_dinode_alloc(struct gfs2_sbd *sdp, const uint64_t blksreq, uint64_t *blkno); +extern uint64_t lgfs2_space_for_data(const struct gfs2_sbd *sdp, unsigned bsize, uint64_t bytes); + extern int gfs2_readi(struct gfs2_inode *ip, void *buf, uint64_t offset, unsigned int size); #define gfs2_writei(ip, buf, offset, size) \ -- 1.8.5.3