[Ocfs2-tools-devel] [RFC 1/4] dx_dirs v1: libocfs2/dealloc.[ch]

Coly Li coly.li at suse.de
Sun Dec 20 10:02:06 PST 2009


This patch adds libocfs2/dealloc.c and libocfs2/dealloc.h for truncate log.

Signed-off-by: Coly Li <coly.li at suse.de>
---
diff --git a/libocfs2/dealloc.c b/libocfs2/dealloc.c
new file mode 100644
index 0000000..48ca70c
--- /dev/null
+++ b/libocfs2/dealloc.c
@@ -0,0 +1,187 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * Copyright (C) 2009 Novell.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License, version 2,  as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <ocfs2/ocfs2.h>
+
+#include "dealloc.h"
+
+int ocfs2_truncate_log_needs_flush(struct ocfs2_truncate_log *tl)
+{
+	return (tl->tl_used == tl->tl_count);
+}
+
+static int ocfs2_replay_truncate_records(ocfs2_filesys *fs,
+		struct ocfs2_dinode *bitmap_di, struct ocfs2_truncate_log *tl)
+{
+	struct ocfs2_truncate_rec rec;
+	uint64_t start_blk;
+	unsigned int num_clusters;
+	int i, ret = 0;
+
+	i = tl->tl_used - 1;
+	while (i >=0) {
+		tl->tl_used = i;
+		rec = tl->tl_recs[i];
+		start_blk = ocfs2_clusters_to_blocks(fs, rec.t_start);
+		num_clusters = rec.t_clusters;
+
+		if (start_blk) {
+			ret = ocfs2_free_clusters(fs, num_clusters, start_blk);
+			if (ret) {
+				goto bail;
+			}
+		}
+		i--;
+	}
+bail:
+	return ret;
+}
+
+static int __ocfs2_flush_truncate_log(ocfs2_filesys *fs,
+		struct ocfs2_truncate_log *tl)
+{
+	int ret = 0;
+	uint64_t blkno;
+	int num_to_flush;
+	char *buf = NULL;
+	struct ocfs2_dinode *bitmap_di;
+
+	num_to_flush = tl->tl_used;
+	if (!num_to_flush)
+		goto bail;
+
+	ret = ocfs2_lookup_system_inode(fs, GLOBAL_BITMAP_SYSTEM_INODE,
+			OCFS2_INVALID_SLOT, &blkno);
+	if (ret)
+		goto bail;
+
+	ret = ocfs2_read_inode(fs, blkno, buf);
+	if (ret)
+		goto bail;
+
+	bitmap_di = (struct ocfs2_dinode *)buf;
+
+	ret = ocfs2_replay_truncate_records(fs, bitmap_di, tl);
+	if (ret < 0)
+		goto bail;
+
+bail:
+	if (buf)
+		ocfs2_free(&buf);
+	return ret;
+}
+
+int ocfs2_flush_truncate_log(ocfs2_filesys *fs,
+		struct ocfs2_truncate_log *tl)
+{
+	int ret;
+	ret = __ocfs2_flush_truncate_log(fs, tl);
+	return ret;
+}
+
+static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
+				unsigned int new_start)
+{
+	unsigned int tail_index;
+	unsigned int current_tail;
+
+	if (!tl->tl_used)
+		return 0;
+
+	tail_index = tl->tl_used -1;
+	current_tail = tl->tl_recs[tail_index].t_start;
+	current_tail += tl->tl_recs[tail_index].t_clusters;
+
+	return (current_tail == new_start);
+}
+
+errcode_t ocfs2_truncate_log_append(ocfs2_filesys *fs,
+				struct ocfs2_dinode *di,
+				struct ocfs2_truncate_log *tl,
+				uint64_t start_blk,
+				unsigned int num_clusters)
+{
+	unsigned int start_cluster, tl_count;
+	int index, ret = 0;
+
+	start_cluster = ocfs2_blocks_to_clusters(fs, start_blk);
+	tl_count = tl->tl_count;
+	if (tl_count > ocfs2_truncate_recs_per_inode(fs->fs_blocksize) || tl_count == 0) {
+		printf("truncate log error.\n");
+		goto out;
+	}
+
+	index = tl->tl_used;
+
+	if (index >= tl_count) {
+		ret = OCFS2_ET_NO_SPACE;
+		goto out;
+	}
+
+	if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
+		index --;
+		num_clusters += tl->tl_recs[index].t_clusters;
+	} else {
+		tl->tl_recs[index].t_start = start_cluster;
+		tl->tl_used = index + 1;
+	}
+	tl->tl_recs[index].t_clusters = num_clusters;
+out:
+	return ret;
+}
+
+int ocfs2_flush_truncate_log_all(ocfs2_filesys *fs)
+{
+	struct ocfs2_super_block *super;
+	struct ocfs2_dinode *di;
+	struct ocfs2_truncate_log *tl;
+	int i, ret = 0;
+	uint64_t blkno;
+	char *buf = NULL;
+
+	ret = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (ret)
+		goto out;
+
+	super = OCFS2_RAW_SB(fs->fs_super);
+
+	for (i = 0; i < super->s_max_slots; i++) {
+		ret = ocfs2_lookup_system_inode(fs, TRUNCATE_LOG_SYSTEM_INODE,
+				i, &blkno);
+		if (ret)
+			goto out;
+		ret = ocfs2_read_inode(fs, blkno, buf);
+		if (ret)
+			goto out;
+		di = (struct ocfs2_dinode *)buf;
+		tl = &di->id2.i_dealloc;
+		ret = ocfs2_flush_truncate_log(fs, tl);
+		if (ret)
+			goto out;
+	}
+
+out:
+	if (buf)
+		ocfs2_free(&buf);
+	return ret;
+}
diff --git a/libocfs2/dealloc.h b/libocfs2/dealloc.h
new file mode 100644
index 0000000..72303e6
--- /dev/null
+++ b/libocfs2/dealloc.h
@@ -0,0 +1,22 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * dealloc.h
+ *
+ * Copyright (C) 2009 Novell.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ */
+
+int ocfs2_truncate_log_needs_flush(struct ocfs2_truncate_log *tl);
+int ocfs2_flush_truncate_log(ocfs2_filesys *fs, struct ocfs2_truncate_log *tl);
+errcode_t ocfs2_truncate_log_append(ocfs2_filesys *fs, struct ocfs2_dinode *di,
+				struct ocfs2_truncate_log *tl, uint64_t start_blk,
+				unsigned int num_clusters);

-- 
Coly Li
SuSE Labs



More information about the Ocfs2-tools-devel mailing list