[CRIU] [PATCH 13/15] img: Prepare to use bfd engine

Pavel Emelyanov xemul at parallels.com
Mon Sep 29 01:50:13 PDT 2014


Signed-off-by: Pavel Emelyanov <xemul at parallels.com>
---
 bfd.c           | 19 ++++++++++++++++++-
 image.c         | 22 ++++++++++++++--------
 include/bfd.h   | 14 ++++++++++++++
 include/image.h |  7 +++++--
 protobuf.c      | 12 +++++-------
 5 files changed, 56 insertions(+), 18 deletions(-)

diff --git a/bfd.c b/bfd.c
index 1494aa9..2572679 100644
--- a/bfd.c
+++ b/bfd.c
@@ -4,6 +4,7 @@
 #include <stdio.h>
 #include <sys/mman.h>
 #include <fcntl.h>
+#include <sys/uio.h>
 
 #include "log.h"
 #include "bfd.h"
@@ -85,7 +86,8 @@ int bfdopen(struct bfd *f)
 
 void bclose(struct bfd *f)
 {
-	buf_put(&f->b);
+	if (bfd_buffered(f))
+		buf_put(&f->b);
 	close(f->fd);
 }
 
@@ -175,3 +177,18 @@ again:
 
 	goto again;
 }
+
+int bwrite(struct bfd *bfd, const void *buf, int size)
+{
+	return write(bfd->fd, buf, size);
+}
+
+int bwritev(struct bfd *bfd, const struct iovec *iov, int cnt)
+{
+	return writev(bfd->fd, iov, cnt);
+}
+
+int bread(struct bfd *bfd, void *buf, int size)
+{
+	return read(bfd->fd, buf, size);
+}
diff --git a/image.c b/image.c
index 246c91b..99cabb3 100644
--- a/image.c
+++ b/image.c
@@ -229,7 +229,12 @@ struct cr_img *open_image_at(int dfd, int type, unsigned long flags, ...)
 		goto err;
 	}
 
-	img->_fd = ret;
+	img->_x.fd = ret;
+	if (oflags & O_NOBUF)
+		bfd_setraw(&img->_x);
+	else if (bfdopen(&img->_x))
+		goto err;
+
 	if (imgset_template[type].magic == RAW_IMAGE_MAGIC)
 		goto skip_magic;
 
@@ -258,7 +263,7 @@ errn:
 
 void close_image(struct cr_img *img)
 {
-	close(img->_fd);
+	bclose(&img->_x);
 	xfree(img);
 }
 
@@ -267,8 +272,11 @@ struct cr_img *img_from_fd(int fd)
 	struct cr_img *img;
 
 	img = xmalloc(sizeof(*img));
-	if (img)
-		img->_fd = fd;
+	if (img) {
+		img->_x.fd = fd;
+		bfd_setraw(&img->_x);
+	}
+
 	return img;
 }
 
@@ -355,10 +363,9 @@ struct cr_img *open_pages_image(unsigned long flags, struct cr_img *pmi)
  */
 int write_img_buf(struct cr_img *img, const void *ptr, int size)
 {
-	int fd = img->_fd;
 	int ret;
 
-	ret = write(fd, ptr, size);
+	ret = bwrite(&img->_x, ptr, size);
 	if (ret == size)
 		return 0;
 
@@ -378,10 +385,9 @@ int write_img_buf(struct cr_img *img, const void *ptr, int size)
  */
 int read_img_buf_eof(struct cr_img *img, void *ptr, int size)
 {
-	int fd = img->_fd;
 	int ret;
 
-	ret = read(fd, ptr, size);
+	ret = bread(&img->_x, ptr, size);
 	if (ret == size)
 		return 1;
 	if (ret == 0)
diff --git a/include/bfd.h b/include/bfd.h
index 11dc484..0800455 100644
--- a/include/bfd.h
+++ b/include/bfd.h
@@ -13,8 +13,22 @@ struct bfd {
 	struct xbuf b;
 };
 
+static inline bool bfd_buffered(struct bfd *b)
+{
+	return b->b.mem != NULL;
+}
+
+static inline void bfd_setraw(struct bfd *b)
+{
+	b->b.mem = NULL;
+}
+
 #define BREADERR	((char *)-1)
 int bfdopen(struct bfd *f);
 void bclose(struct bfd *f);
 char *breadline(struct bfd *f);
+int bwrite(struct bfd *f, const void *buf, int sz);
+struct iovec;
+int bwritev(struct bfd *f, const struct iovec *iov, int cnt);
+int bread(struct bfd *f, void *buf, int sz);
 #endif
diff --git a/include/image.h b/include/image.h
index a1925c8..e02fa0e 100644
--- a/include/image.h
+++ b/include/image.h
@@ -8,6 +8,8 @@
 #include "image-desc.h"
 #include "fcntl.h"
 #include "magic.h"
+#include "bfd.h"
+#include "bug.h"
 
 #define PAGE_IMAGE_SIZE	4096
 #define PAGE_RSS	1
@@ -77,12 +79,13 @@ extern bool ns_per_id;
 #define O_RSTR	(O_RDONLY)
 
 struct cr_img {
-	int _fd;
+	struct bfd _x;
 };
 
 static inline int img_raw_fd(struct cr_img *img)
 {
-	return img->_fd;
+	BUG_ON(bfd_buffered(&img->_x));
+	return img->_x.fd;
 }
 
 extern int open_image_dir(char *dir);
diff --git a/protobuf.c b/protobuf.c
index 0688988..b5f5975 100644
--- a/protobuf.c
+++ b/protobuf.c
@@ -17,7 +17,7 @@
 #include "string.h"
 #include "sockets.h"
 #include "cr_options.h"
-
+#include "bfd.h"
 #include "protobuf.h"
 
 /*
@@ -494,7 +494,7 @@ void do_pb_show_plain(struct cr_img *img, int type, int single_entry,
 
 static char *image_name(struct cr_img *img)
 {
-	int fd = img->_fd;
+	int fd = img->_x.fd;
 	static char image_path[PATH_MAX];
 
 	if (read_fd_link(fd, image_path, sizeof(image_path)) > 0)
@@ -515,7 +515,6 @@ static char *image_name(struct cr_img *img)
 
 int do_pb_read_one(struct cr_img *img, void **pobj, int type, bool eof)
 {
-	int fd = img->_fd;
 	u8 local[PB_PKOBJ_LOCAL_SIZE];
 	void *buf = (void *)&local;
 	u32 size;
@@ -529,7 +528,7 @@ int do_pb_read_one(struct cr_img *img, void **pobj, int type, bool eof)
 
 	*pobj = NULL;
 
-	ret = read(fd, &size, sizeof(size));
+	ret = bread(&img->_x, &size, sizeof(size));
 	if (ret == 0) {
 		if (eof) {
 			return 0;
@@ -552,7 +551,7 @@ int do_pb_read_one(struct cr_img *img, void **pobj, int type, bool eof)
 			goto err;
 	}
 
-	ret = read(fd, buf, size);
+	ret = bread(&img->_x, buf, size);
 	if (ret < 0) {
 		pr_perror("Can't read %d bytes from file %s",
 			  size, image_name(img));
@@ -590,7 +589,6 @@ err:
  */
 int pb_write_one(struct cr_img *img, void *obj, int type)
 {
-	int fd = img->_fd;
 	u8 local[PB_PKOBJ_LOCAL_SIZE];
 	void *buf = (void *)&local;
 	u32 size, packed;
@@ -620,7 +618,7 @@ int pb_write_one(struct cr_img *img, void *obj, int type)
 	iov[1].iov_base = buf;
 	iov[1].iov_len = size;
 
-	ret = writev(fd, iov, 2);
+	ret = bwritev(&img->_x, iov, 2);
 	if (ret != size + sizeof(size)) {
 		pr_perror("Can't write %d bytes", (int)(size + sizeof(size)));
 		goto err;
-- 
1.8.4.2




More information about the CRIU mailing list