[sheepdog] [PATCH v5 1/5] util: add a helper round_down

Liu Yuan namei.unix at gmail.com
Thu May 23 06:54:26 CEST 2013


Also changes roundup to round_up and this is a prepration patch.

Signed-off-by: Liu Yuan <namei.unix at gmail.com>
---
 collie/vdi.c    |    6 +++---
 include/util.h  |    3 ++-
 sheep/journal.c |    6 +++---
 3 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/collie/vdi.c b/collie/vdi.c
index 087ec80..ccc4e04 100644
--- a/collie/vdi.c
+++ b/collie/vdi.c
@@ -523,7 +523,7 @@ int do_vdi_create(const char *vdiname, int64_t vdi_size,
 
 	hdr.vdi.base_vdi_id = base_vid;
 	hdr.vdi.snapid = snapshot ? 1 : 0;
-	hdr.vdi.vdi_size = roundup(vdi_size, 512);
+	hdr.vdi.vdi_size = round_up(vdi_size, 512);
 	hdr.vdi.copies = nr_copies;
 
 	ret = collie_exec_req(sdhost, sdport, &hdr, buf);
@@ -1214,7 +1214,7 @@ static int vdi_read(int argc, char **argv)
 	}
 
 	total = min(total, inode->vdi_size - offset);
-	total = roundup(total, 512);
+	total = round_up(total, 512);
 	idx = offset / SD_DATA_OBJ_SIZE;
 	offset %= SD_DATA_OBJ_SIZE;
 	while (done < total) {
@@ -1295,7 +1295,7 @@ static int vdi_write(int argc, char **argv)
 	}
 
 	total = min(total, inode->vdi_size - offset);
-	total = roundup(total, 512);
+	total = round_up(total, 512);
 	idx = offset / SD_DATA_OBJ_SIZE;
 	offset %= SD_DATA_OBJ_SIZE;
 	while (done < total) {
diff --git a/include/util.h b/include/util.h
index 1603796..475c9cd 100644
--- a/include/util.h
+++ b/include/util.h
@@ -16,7 +16,8 @@
 #define SECTOR_SIZE (1U << 9)
 
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
-#define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
+#define round_up(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
+#define round_down(x, y) (((x) / (y)) * (y))
 
 #if __BYTE_ORDER == __LITTLE_ENDIAN
 #define __cpu_to_be16(x) bswap_16(x)
diff --git a/sheep/journal.c b/sheep/journal.c
index 7c260d2..b62ff58 100644
--- a/sheep/journal.c
+++ b/sheep/journal.c
@@ -128,7 +128,7 @@ out:
 static bool journal_entry_full_write(struct journal_descriptor *jd)
 {
 	char *end = (char *)jd +
-		roundup(jd->size, SECTOR_SIZE) + JOURNAL_META_SIZE;
+		round_up(jd->size, SECTOR_SIZE) + JOURNAL_META_SIZE;
 	uint32_t marker = *(((uint32_t *)end) - 1);
 
 	if (marker != JOURNAL_END_MARKER)
@@ -249,7 +249,7 @@ static int do_recover(int fd)
 		if (replay_journal_entry(jd) < 0)
 			return -1;
 skip:
-		p += JOURNAL_META_SIZE + roundup(jd->size, SECTOR_SIZE);
+		p += JOURNAL_META_SIZE + round_up(jd->size, SECTOR_SIZE);
 	}
 	munmap(map, st.st_size);
 	/* Do a final sync() to assure data is reached to the disk */
@@ -383,7 +383,7 @@ static int journal_file_write(struct journal_descriptor *jd, const char *buf)
 	uint32_t marker = JOURNAL_END_MARKER;
 	int ret = SD_RES_SUCCESS;
 	uint64_t size = jd->size;
-	ssize_t written, rusize = roundup(size, SECTOR_SIZE),
+	ssize_t written, rusize = round_up(size, SECTOR_SIZE),
 		wsize = JOURNAL_META_SIZE + rusize;
 	off_t woff;
 	char *wbuffer, *p;
-- 
1.7.9.5



More information about the sheepdog mailing list