[Tux3] Userland cleanups

OGAWA Hirofumi hirofumi at mail.parknet.co.jp
Fri Jan 9 11:59:11 PST 2009


OGAWA Hirofumi <hirofumi at mail.parknet.co.jp> writes:

> Daniel Phillips <phillips at phunq.net> writes:
>
>> One thing I was trying to do is keep buffer.c from knowing details of
>> Tux3, such as inode format.  Mainly so that it could be used for some
>> other project, the same way I borrowed it from ddsnap.  I don't know
>> how important that is, perhaps not very important.  Anyway, an easy
>> resolution is to make map_dev external.  Then buffer can go back to
>> having its own includes and not including tux3.h.
>
> I see. I was trying to include buffer.c to tux3 rather. I'll try to
> think about it.

So, I removed inode from buffer.*. Maybe, user of new_map() should be
careful to set inode->map->inode properly. Currently xattr.c only.

	static-http://userweb.kernel.org/~hirofumi/tux3/

Now, my repo is including the following patch.
Please review.


diff -puN user/buffer.c~standalone-buffer-c user/buffer.c
--- tux3/user/buffer.c~standalone-buffer-c	2009-01-10 04:34:07.000000000 +0900
+++ tux3-hirofumi/user/buffer.c	2009-01-10 04:45:03.000000000 +0900
@@ -1,14 +1,14 @@
-#include "tux3.h"
+#include <stdlib.h>
+#include <stddef.h>
+#include <errno.h>
 #include "diskio.h"
+#include "buffer.h"
+#include "trace.h"
+#include "err.h"
 
 #define BUFFER_PARANOIA_DEBUG
 #define buftrace trace_off
 
-static inline struct dev *map_dev(map_t *map)
-{
-	return map->inode->i_sb->dev;
-}
-
 /*
  * Emulate kernel buffers in userspace
  *
@@ -28,6 +28,8 @@ static inline struct dev *map_dev(map_t 
  * add async IO.
  */
 
+typedef long long L; // widen for printf on 64 bit systems
+
 struct list_head free_buffers;
 struct list_head lru_buffers;
 unsigned buffer_count;
@@ -261,7 +263,7 @@ alloc_buffer:
 	if (!buffer)
 		return ERR_PTR(-ENOMEM);
 	*buffer = (struct buffer_head){ .state = BUFFER_EMPTY };
-	if ((err = -posix_memalign((void **)&(buffer->data), SECTOR_SIZE, 1 << map_dev(map)->bits))) {
+	if ((err = -posix_memalign((void **)&(buffer->data), SECTOR_SIZE, 1 << map->dev->bits))) {
 		warn("Error: %s unable to expand buffer pool", strerror(err));
 		free(buffer);
 		return ERR_PTR(err);
@@ -445,7 +447,7 @@ void init_buffers(struct dev *dev, unsig
 int dev_blockread(struct buffer_head *buffer)
 {
 	warn("read [%Lx]", (L)buffer->index);
-	struct dev *dev = map_dev(buffer->map);
+	struct dev *dev = buffer->map->dev;
 	assert(dev->bits >= 8 && dev->fd);
 	return diskread(dev->fd, buffer->data, bufsize(buffer), buffer->index << dev->bits);
 }
@@ -453,19 +455,18 @@ int dev_blockread(struct buffer_head *bu
 int dev_blockwrite(struct buffer_head *buffer)
 {
 	warn("write [%Lx]", (L)buffer->index);
-	struct dev *dev = map_dev(buffer->map);
+	struct dev *dev = buffer->map->dev;
 	assert(dev->bits >= 8 && dev->fd);
 	return diskwrite(dev->fd, buffer->data, bufsize(buffer), buffer->index << dev->bits);
 }
 
 struct map_ops volmap_ops = { .blockread = dev_blockread, .blockwrite = dev_blockwrite };
 
-map_t *new_map(struct inode *inode, struct map_ops *ops)
+map_t *new_map(struct dev *dev, struct map_ops *ops)
 {
 	map_t *map = malloc(sizeof(*map)); // error???
-	*map = (map_t){ .ops = ops ? ops : &volmap_ops };
+	*map = (map_t){ .dev = dev, .ops = ops ? ops : &volmap_ops };
 	INIT_LIST_HEAD(&map->dirty);
-	map->inode = inode;
 	return map;
 }
 
@@ -479,17 +480,15 @@ void free_map(map_t *map)
 int main(int argc, char *argv[])
 {
 	struct dev *dev = &(struct dev){ .bits = 12 };
-	struct sb *sb = &(struct sb){ RAPID_INIT_SB(dev), };
-	struct inode *volmap = rapid_new_inode(sb, NULL, 0);
-
+	map_t *map = new_map(dev, NULL);
 	init_buffers(dev, 1 << 20);
-	show_dirty_buffers(volmap->map);
-	mark_buffer_dirty(blockget(volmap->map, 1));
-	show_dirty_buffers(volmap->map);
-	printf("get %p\n", blockget(volmap->map, 0));
-	printf("get %p\n", blockget(volmap->map, 1));
-	printf("get %p\n", blockget(volmap->map, 2));
-	printf("get %p\n", blockget(volmap->map, 1));
+	show_dirty_buffers(map);
+	mark_buffer_dirty(blockget(map, 1));
+	show_dirty_buffers(map);
+	printf("get %p\n", blockget(map, 0));
+	printf("get %p\n", blockget(map, 1));
+	printf("get %p\n", blockget(map, 2));
+	printf("get %p\n", blockget(map, 1));
 	exit(0);
 }
 #endif
diff -puN user/buffer.h~standalone-buffer-c user/buffer.h
--- tux3/user/buffer.h~standalone-buffer-c	2009-01-10 04:36:31.000000000 +0900
+++ tux3-hirofumi/user/buffer.h	2009-01-10 04:37:48.000000000 +0900
@@ -25,8 +25,11 @@ struct map_ops
 };
 
 struct map {
-	struct list_head dirty;
+#if 1 /* tux3 only */
 	struct inode *inode;
+#endif
+	struct list_head dirty;
+	struct dev *dev;
 	struct map_ops *ops;
 	struct buffer_head *hash[BUFFER_BUCKETS];
 	unsigned dirty_count;
@@ -74,6 +77,11 @@ static inline void *bufdata(struct buffe
 	return buffer->data;
 }
 
+static inline unsigned bufsize(struct buffer_head *buffer)
+{
+	return 1 << buffer->map->dev->bits;
+}
+
 static inline block_t bufindex(struct buffer_head *buffer)
 {
 	return buffer->index;
@@ -104,6 +112,6 @@ static inline int buffer_dirty(struct bu
 	return buffer->state == BUFFER_DIRTY;
 }
 
-map_t *new_map(struct inode *inode, struct map_ops *ops);
+map_t *new_map(struct dev *dev, struct map_ops *ops);
 void free_map(map_t *map);
 #endif
diff -puN user/tux3.h~standalone-buffer-c user/tux3.h
--- tux3/user/tux3.h~standalone-buffer-c	2009-01-10 04:36:59.000000000 +0900
+++ tux3-hirofumi/user/tux3.h	2009-01-10 04:39:51.000000000 +0900
@@ -136,11 +136,6 @@ static inline struct inode *buffer_inode
 	return buffer->map->inode;
 }
 
-static inline unsigned bufsize(struct buffer_head *buffer)
-{
-	return buffer->map->inode->i_sb->blocksize;
-}
-
 static inline struct timespec gettime(void)
 {
 	struct timeval now;
@@ -167,8 +162,9 @@ static inline struct buffer_head *sb_bre
 		.i_sb = sb,					\
 		.i_mode = mode,					\
 	};							\
-	__inode->map = new_map(__inode, ops);			\
+	__inode->map = new_map((sb)->dev, ops);			\
 	assert(__inode->map);					\
+	__inode->map->inode = __inode;				\
 	__inode;						\
 })
 
diff -puN user/inode.c~standalone-buffer-c user/inode.c
--- tux3/user/inode.c~standalone-buffer-c	2009-01-10 04:38:55.000000000 +0900
+++ tux3-hirofumi/user/inode.c	2009-01-10 04:39:09.000000000 +0900
@@ -24,9 +24,10 @@ struct inode *new_inode(struct sb *sb)
 	if (!inode)
 		goto error;
 	*inode = (struct inode){ .i_sb = sb, .i_version = 1, .i_nlink = 1, };
-	inode->map = new_map(inode, NULL);
+	inode->map = new_map(sb->dev, NULL);
 	if (!inode->map)
 		goto error_map;
+	inode->map->inode = inode;
 	return inode;
 
 error_map:
diff -puN user/xattr.c~standalone-buffer-c user/xattr.c
--- tux3/user/xattr.c~standalone-buffer-c	2009-01-10 04:41:59.000000000 +0900
+++ tux3-hirofumi/user/xattr.c	2009-01-10 04:41:03.000000000 +0900
@@ -55,7 +55,8 @@ int main(int argc, char *argv[])
 		.btree = { .root = { .block = 0xcaba1f00dULL, .depth = 3 } },
 		.i_ctime = spectime(0xdec0debeadULL),
 		.i_mtime = spectime(0xbadfaced00dULL) };
-	inode->map = new_map(inode, NULL);
+	inode->map = new_map(dev, NULL);
+	inode->map->inode = inode;
 	sb->atable = inode;
 
 	for (int i = 0; i < 2; i++) {
_

-- 
OGAWA Hirofumi <hirofumi at mail.parknet.co.jp>

_______________________________________________
Tux3 mailing list
Tux3 at tux3.org
http://mailman.tux3.org/cgi-bin/mailman/listinfo/tux3



More information about the Tux3 mailing list