[PATCH 1/1] tgtimg: use posic_fallocate when creating the big files

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



List, Tomo,

Please find attached a trivial patch for tgtimg.
It changes to use posix_fallocate() when creating raw disk images
instead of using a loop with pwrite().

This makes it less intrusive to the system when creating many large
disk images at the same time.
I did try using posix_fadvise() as an alternative but that did not
work well. (it did not have much effect on the vm trashing)



regards
ronnie sahlberg
From 1a3ab72cac0110d5463a4460db3c02521334d528 Mon Sep 17 00:00:00 2001
From: Ronnie Sahlberg <ronniesahlberg@xxxxxxxxx>
Date: Tue, 12 Oct 2010 14:22:50 +1100
Subject: [PATCH] Use posix_fallocate() to create the disk image file instead
 of writing the data.
 This plays nicer with the vm system.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@xxxxxxxxx>
---
 usr/tgtimg.c |   11 ++++++-----
 1 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/usr/tgtimg.c b/usr/tgtimg.c
index 758f1fb..169207a 100644
--- a/usr/tgtimg.c
+++ b/usr/tgtimg.c
@@ -19,6 +19,7 @@
  *
  */
 
+#define _XOPEN_SOURCE 600
 #include <errno.h>
 #include <fcntl.h>
 #include <getopt.h>
@@ -416,10 +417,9 @@ static int mmc_ops(int op, char *path, char *media_type)
 static int sbc_new(int op, char *path, char *capacity, char *media_type)
 {
 	int fd;
-	ssize_t ignored;
 
 	if (!strncasecmp("disk", media_type, 4)) {
-		uint32_t pos, size;
+		uint32_t size;
 		char *buf;
 
 		sscanf(capacity, "%d", &size);
@@ -438,9 +438,10 @@ static int sbc_new(int op, char *path, char *capacity, char *media_type)
 			perror("Failed creating file");
 			exit(2);
 		}
-
-		for (pos = 0; pos < size; pos++)
-			ignored = pwrite(fd, buf, 1024*1024, pos*1024*1024LL);
+		if (posix_fallocate(fd, 0, size*1024*1024LL) == -1) {
+			perror("posix_fallocate failed.");
+			exit(3);
+		}
 
 		free(buf);
 		close(fd);
-- 
1.7.3.1

Attachment: 0001-Use-posix_fallocate-to-create-the-disk-image-file-in.patch.gz
Description: GNU Zip compressed data


[Index of Archives]     [Linux SCSI]     [Linux RAID]     [Linux Clusters]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]

  Powered by Linux