[Ocfs2-tools-devel] [RFC 1/4] dx_dirs v2: indexed dir for fsck.ocfs2
Coly Li
coly.li at suse.de
Fri Dec 25 11:22:42 PST 2009
The patch add indexed dir related code for fsck.ocfs2.
Signed-off-by: Coly Li <coly.li at suse.de>
---
diff --git a/fsck.ocfs2/dirblocks.c b/fsck.ocfs2/dirblocks.c
index 1fd5560..7e0c45f 100644
--- a/fsck.ocfs2/dirblocks.c
+++ b/fsck.ocfs2/dirblocks.c
@@ -134,6 +134,79 @@ static int try_to_cache(ocfs2_filesys *fs, struct rb_node *node,
return cached_blocks;
}
+uint64_t o2fsck_search_reidx_dir(struct rb_root *root, uint64_t dino)
+{
+ struct rb_node *node = root->rb_node;
+ o2fsck_dirblock_entry *dbe;
+
+ while (node) {
+ dbe = rb_entry(node, o2fsck_dirblock_entry, e_node);
+
+ if (dino < dbe->e_ino)
+ node = node->rb_left;
+ else if (dino > dbe->e_ino)
+ node = node->rb_right;
+ else
+ return dbe->e_ino;
+ }
+ return 0;
+}
+
+static errcode_t o2fsck_add_reidx_dir_ino(struct rb_root *root, uint64_t dino)
+{
+ struct rb_node **p = &root->rb_node;
+ struct rb_node *parent = NULL;
+ o2fsck_dirblock_entry *dp, *tmp_dp;
+ errcode_t ret = 0;
+
+ ret = ocfs2_malloc0(sizeof(o2fsck_dirblock_entry), &dp);
+ if (ret)
+ goto out;
+
+ dp->e_ino = dino;
+
+ while(*p)
+ {
+ parent = *p;
+ tmp_dp = rb_entry(parent, o2fsck_dirblock_entry, e_node);
+
+ if (dp->e_ino < tmp_dp->e_ino)
+ p = &(*p)->rb_left;
+ else if (dp->e_ino > tmp_dp->e_ino)
+ p = &(*p)->rb_right;
+ else {
+ ret = OCFS2_ET_INTERNAL_FAILURE;
+ ocfs2_free(&dp);
+ goto out;
+ }
+ }
+
+ rb_link_node(&dp->e_node, parent, p);
+ rb_insert_color(&dp->e_node, root);
+
+out:
+ return ret;
+}
+
+errcode_t o2fsck_try_add_reidx_dir(struct rb_root *root, uint64_t dino)
+{
+ errcode_t ret = 1;
+ uint64_t ino;
+ ino = o2fsck_search_reidx_dir(root, dino);
+ if (ino) {
+ ret = 0;
+ goto out;
+ }
+ ret = o2fsck_add_reidx_dir_ino(root, dino);
+ if (ret)
+ printf("some error happens.\n");
+
+out:
+ return ret;
+}
+
void o2fsck_dir_block_iterate(o2fsck_state *ost, dirblock_iterator func,
void *priv_data)
{
@@ -174,3 +247,58 @@ void o2fsck_dir_block_iterate(o2fsck_state *ost, dirblock_iterator func,
if (pre_cache_buf)
ocfs2_free(&pre_cache_buf);
}
+
+static int ocfs2_rebuild_indexed_dir(ocfs2_filesys *fs, uint64_t ino)
+{
+ int ret = 0;
+ char *di_buf = NULL, *dx_root_buf = NULL;
+ struct ocfs2_dinode *di;
+
+ ret = ocfs2_malloc_block(fs->fs_io, &di_buf);
+ if (ret)
+ goto out;
+
+ ret = ocfs2_read_inode(fs, ino, di_buf);
+ if (ret)
+ goto out;
+ di = (struct ocfs2_dinode *)di_buf;
+
+ ret = ocfs2_dx_dir_truncate(fs, di);
+ if (ret)
+ goto out;
+
+
+ /* do not rebuild indexed tree for inline directory */
+ if (di->i_dyn_features & OCFS2_INLINE_DATA_FL) {
+ printf("inode with OCFS2_INLINE_DATA_FL, do nothing.\n");
+ goto out;
+ }
+
+//# build new indexed-tree
+ ret = ocfs2_write_inode(fs, ino, di_buf);
+out:
+ if (di_buf)
+ ocfs2_free(&di_buf);
+ if (dx_root_buf)
+ ocfs2_free(&dx_root_buf);
+ return ret;
+}
+
+
+void o2fsck_rebuild_indexed_dirs(ocfs2_filesys *fs, struct rb_root *root)
+{
+ struct rb_node *node;
+ o2fsck_dirblock_entry *dbe;
+ uint64_t ino;
+ int ret;
+
+ for (node = rb_first(root); node; node = rb_next(node)) {
+ dbe = rb_entry(node, o2fsck_dirblock_entry, e_node);
+ ino = dbe->e_ino;
+ ret = ocfs2_rebuild_indexed_dir(fs, ino);
+ if (ret)
+ printf("o2fsck_rebuilt_indexed_dir failed on %lu\n", ino);
+ }
+}
+
+
diff --git a/fsck.ocfs2/include/dirblocks.h b/fsck.ocfs2/include/dirblocks.h
index 7b3a2e9..2940b6d 100644
--- a/fsck.ocfs2/include/dirblocks.h
+++ b/fsck.ocfs2/include/dirblocks.h
@@ -48,6 +48,9 @@ struct _o2fsck_state;
void o2fsck_dir_block_iterate(struct _o2fsck_state *ost, dirblock_iterator func,
void *priv_data);
+uint64_t o2fsck_search_reidx_dir(struct rb_root *root, uint64_t dino);
+errcode_t o2fsck_try_add_reidx_dir(struct rb_root *root, uint64_t dino);
+void o2fsck_rebuild_indexed_dirs(ocfs2_filesys *fs, struct rb_root *root);
#endif /* __O2FSCK_DIRBLOCKS_H__ */
diff --git a/fsck.ocfs2/pass2.c b/fsck.ocfs2/pass2.c
index d61c501..c98feab 100644
--- a/fsck.ocfs2/pass2.c
+++ b/fsck.ocfs2/pass2.c
@@ -36,6 +36,7 @@
#include <time.h>
#include "ocfs2/ocfs2.h"
+#include "ocfs2/kernel-rbtree.h"
#include "dirparents.h"
#include "icount.h"
@@ -70,6 +71,7 @@ struct dirblock_data {
errcode_t ret;
o2fsck_strings strings;
uint64_t last_ino;
+ struct rb_root re_idx_dirs;
};
static int dirent_has_dots(struct ocfs2_dir_entry *dirent, int num_dots)
@@ -851,6 +853,14 @@ next:
com_err(whoami, ret, "while writing dir block %"PRIu64,
dbe->e_blkno);
dd->ost->ost_write_error = 1;
+ goto out;
+ }
+
+ if (ocfs2_supports_indexed_dirs(OCFS2_RAW_SB(dd->fs->fs_super)) &&
+ !(di->i_dyn_features & OCFS2_INLINE_DATA_FL)) {
+ ret = o2fsck_try_add_reidx_dir(&dd->re_idx_dirs, dbe->e_ino);
+ if (ret)
+ printf("there is something wrong\n");
}
}
@@ -860,6 +870,18 @@ out:
return ret_flags;
}
+static void release_re_idx_dirs_rbtree(struct rb_root * root)
+{
+ struct rb_node *node;
+ o2fsck_dirblock_entry *dp;
+
+ while ((node = rb_first(root)) != NULL) {
+ dp = rb_entry(node, o2fsck_dirblock_entry, e_node);
+ rb_erase(&dp->e_node, root);
+ free(dp);
+ }
+}
+
errcode_t o2fsck_pass2(o2fsck_state *ost)
{
o2fsck_dir_parent *dp;
@@ -868,6 +890,7 @@ errcode_t o2fsck_pass2(o2fsck_state *ost)
.ost = ost,
.fs = ost->ost_fs,
.last_ino = 0,
+ .re_idx_dirs = RB_ROOT,
};
printf("Pass 2: Checking directory entries.\n");
@@ -905,6 +928,11 @@ errcode_t o2fsck_pass2(o2fsck_state *ost)
dp->dp_dirent = ost->ost_fs->fs_sysdir_blkno;
o2fsck_dir_block_iterate(ost, pass2_dir_block_iterate, &dd);
+
+ if (dd.re_idx_dirs.rb_node)
+ o2fsck_rebuild_indexed_dirs(ost->ost_fs, &dd.re_idx_dirs);
+ release_re_idx_dirs_rbtree(&dd.re_idx_dirs);
+
o2fsck_strings_free(&dd.strings);
out:
if (dd.dirblock_buf)
--
Coly Li
SuSE Labs
More information about the Ocfs2-tools-devel
mailing list