[Ocfs2-tools-devel] [PATCH 4/5] Abstarct two functions for future
"clear sparse" use,take 1
Tao Ma
tao.ma at oracle.com
Wed Oct 17 02:26:42 PDT 2007
Two main functions are abstracted so that they can be used by "clear sparse":
1. The process for iterating all the regular files.
2. The process for get the free clusters in global bitmap.
Signed-off-by: Tao Ma <tao.ma at oracle.com>
---
tunefs.ocfs2/sparse_file.c | 88 ++++++++++++++++++++++++++++++++------------
1 files changed, 64 insertions(+), 24 deletions(-)
diff --git a/tunefs.ocfs2/sparse_file.c b/tunefs.ocfs2/sparse_file.c
index 3499ae4..f56ff93 100644
--- a/tunefs.ocfs2/sparse_file.c
+++ b/tunefs.ocfs2/sparse_file.c
@@ -45,6 +45,41 @@ struct list_ctxt {
struct rb_root multi_link_files;
};
+static errcode_t get_total_free_clusters(ocfs2_filesys *fs, uint32_t *clusters)
+{
+ errcode_t ret;
+ uint64_t blkno;
+ char *buf = NULL;
+ struct ocfs2_dinode *di = NULL;
+ char file_name[OCFS2_MAX_FILENAME_LEN];
+ struct ocfs2_super_block *sb = OCFS2_RAW_SB(fs->fs_super);
+
+ ret = ocfs2_malloc_block(fs->fs_io, &buf);
+ if (ret)
+ goto bail;
+
+ snprintf(file_name, sizeof(file_name),
+ ocfs2_system_inodes[GLOBAL_BITMAP_SYSTEM_INODE].si_name);
+
+ ret = ocfs2_lookup(fs, sb->s_system_dir_blkno, file_name,
+ strlen(file_name), NULL, &blkno);
+ if (ret)
+ goto bail;
+
+ ret = ocfs2_read_inode(fs, blkno, buf);
+ if (ret)
+ goto bail;
+
+ di = (struct ocfs2_dinode *)buf;
+
+ if (clusters)
+ *clusters = di->id1.bitmap1.i_total - di->id1.bitmap1.i_used;
+bail:
+ if (buf)
+ ocfs2_free(&buf);
+ return ret;
+}
+
static void inline empty_multi_link_files(struct list_ctxt *ctxt)
{
struct multi_link_file *lf;
@@ -379,7 +414,7 @@ errcode_t list_sparse(ocfs2_filesys *fs)
struct ocfs2_super_block *sb = OCFS2_RAW_SB(fs->fs_super);
char *buf = NULL;
struct ocfs2_dinode *di = NULL;
- uint32_t total_holes = 0;
+ uint32_t total_holes = 0, free_clusters = 0;
ret = ocfs2_malloc_block(fs->fs_io, &buf);
if (ret)
@@ -441,21 +476,10 @@ errcode_t list_sparse(ocfs2_filesys *fs)
printf("Total hole clusters in the volume: %u\n\n", total_holes);
/* Get the total free bits in the global_bitmap. */
- snprintf(file_name, sizeof(file_name),
- ocfs2_system_inodes[GLOBAL_BITMAP_SYSTEM_INODE].si_name);
-
- ret = ocfs2_lookup(fs, sb->s_system_dir_blkno, file_name,
- strlen(file_name), NULL, &blkno);
- if (ret)
- goto bail;
-
- ret = ocfs2_read_inode(fs, blkno, buf);
+ ret = get_total_free_clusters(fs, &free_clusters);
if (ret)
goto bail;
-
- di = (struct ocfs2_dinode *)buf;
- printf("Total free %u clusters in the volume.\n",
- di->id1.bitmap1.i_total - di->id1.bitmap1.i_used);
+ printf("Total free %u clusters in the volume.\n",free_clusters);
bail:
empty_multi_link_files(&ctxt);
@@ -474,6 +498,9 @@ static errcode_t fill_file_end(ocfs2_filesys *fs, struct ocfs2_dinode *di)
uint64_t blkno, len_in_blk, last_cluster_start;
uint16_t offset, blk_off, bpc = fs->fs_clustersize / fs->fs_blocksize;
+ if (!(di->i_size % fs->fs_clustersize))
+ return 0;
+
ret = ocfs2_malloc_block(fs->fs_io, &data_buf);
if (ret)
goto bail;
@@ -537,14 +564,16 @@ bail:
return ret;
}
-errcode_t set_sparse_file_flag(ocfs2_filesys *fs, char *progname)
+static errcode_t iterate_all_regular(ocfs2_filesys *fs,
+ char *progname,
+ errcode_t (*func)(ocfs2_filesys *fs,
+ struct ocfs2_dinode *di))
{
errcode_t ret;
uint64_t blkno;
char *buf;
struct ocfs2_dinode *di;
ocfs2_inode_scan *scan;
- struct ocfs2_super_block *super = OCFS2_RAW_SB(fs->fs_super);
ret = ocfs2_malloc_block(fs->fs_io, &buf);
if (ret)
@@ -580,18 +609,13 @@ errcode_t set_sparse_file_flag(ocfs2_filesys *fs, char *progname)
if (di->i_flags & OCFS2_SYSTEM_FL)
continue;
- if (S_ISREG(di->i_mode)) {
- if (!(di->i_size % fs->fs_clustersize))
- continue;
-
- ret = fill_file_end(fs, di);
+ if (S_ISREG(di->i_mode) && func) {
+ ret = func(fs, di);
if (ret)
- goto out_close_scan;
+ break;
}
}
- OCFS2_SET_INCOMPAT_FEATURE(super, OCFS2_FEATURE_INCOMPAT_SPARSE_ALLOC);
-
out_close_scan:
ocfs2_close_inode_scan(scan);
out_free:
@@ -600,3 +624,19 @@ out_free:
out:
return ret;
}
+
+errcode_t set_sparse_file_flag(ocfs2_filesys *fs, char *progname)
+{
+ errcode_t ret;
+ struct ocfs2_super_block *super = OCFS2_RAW_SB(fs->fs_super);
+
+ ret = iterate_all_regular(fs, progname, fill_file_end);
+
+ if (ret)
+ goto bail;
+
+ OCFS2_SET_INCOMPAT_FEATURE(super, OCFS2_FEATURE_INCOMPAT_SPARSE_ALLOC);
+
+bail:
+ return ret;
+}
--
1.5.3.2.g4f337
More information about the Ocfs2-tools-devel
mailing list