[PATCH] fio: extend file if it's too small
authorJens Axboe <axboe@suse.de>
Tue, 29 Nov 2005 19:30:41 +0000 (20:30 +0100)
committerJens Axboe <axboe@suse.de>
Tue, 29 Nov 2005 19:30:41 +0000 (20:30 +0100)
fio.c

diff --git a/fio.c b/fio.c
index f2ec8bbe8a1314ce675e707393b145f6f69ddcbe..52b3aaeba33b527e7ac7012ef5e56de902bcc5fc 100644 (file)
--- a/fio.c
+++ b/fio.c
@@ -1238,12 +1238,13 @@ static int init_io_u(struct thread_data *td)
        return 0;
 }
 
-static int create_file(struct thread_data *td)
+static int create_file(struct thread_data *td, unsigned long long size,
+                      int extend)
 {
        unsigned long long left;
        unsigned int bs;
+       int r, oflags;
        char *b;
-       int r;
 
        /*
         * unless specifically asked for overwrite, let normal io extend it
@@ -1251,21 +1252,27 @@ static int create_file(struct thread_data *td)
        if (td_write(td) && !td->overwrite)
                return 0;
 
-       if (!td->file_size) {
+       if (!size) {
                fprintf(stderr, "Need size for create\n");
                td->error = EINVAL;
                return 1;
        }
 
-       printf("Client%d: Laying out IO file\n", td->thread_number);
+       if (!extend) {
+               oflags = O_CREAT | O_TRUNC;
+               printf("Client%d: Laying out IO file (%LuMiB)\n", td->thread_number, size >> 20);
+       } else {
+               oflags = O_APPEND;
+               printf("Client%d: Extending IO file (%Lu -> %LuMiB)\n", td->thread_number, (td->file_size - size) >> 20, td->file_size >> 20);
+       }
 
-       td->fd = open(td->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
+       td->fd = open(td->file_name, O_WRONLY | oflags, 0644);
        if (td->fd < 0) {
                td->error = errno;
                return 1;
        }
 
-       if (ftruncate(td->fd, td->file_size) == -1) {
+       if (!extend && ftruncate(td->fd, td->file_size) == -1) {
                td->error = errno;
                return 1;
        }
@@ -1274,7 +1281,7 @@ static int create_file(struct thread_data *td)
        b = malloc(td->max_bs);
        memset(b, 0, td->max_bs);
 
-       left = td->file_size;
+       left = size;
        while (left && !td->terminate) {
                bs = td->max_bs;
                if (bs > left)
@@ -1306,16 +1313,6 @@ static int create_file(struct thread_data *td)
        return 0;
 }
 
-static int file_exists(struct thread_data *td)
-{
-       struct stat st;
-
-       if (stat(td->file_name, &st) != -1)
-               return 1;
-
-       return errno != ENOENT;
-}
-
 static int file_size(struct thread_data *td)
 {
        struct stat st;
@@ -1446,14 +1443,22 @@ static int setup_file_plain(struct thread_data *td)
 
 static int setup_file(struct thread_data *td)
 {
+       struct stat st;
        int flags = 0;
 
-       if (!file_exists(td)) {
+       if (stat(td->file_name, &st) == -1) {
+               if (errno != ENOENT) {
+                       td->error = errno;
+                       return 1;
+               }
                if (!td->create_file) {
                        td->error = ENOENT;
                        return 1;
                }
-               if (create_file(td))
+               if (create_file(td, td->file_size, 0))
+                       return 1;
+       } else if (st.st_size < td->file_size) {
+               if (create_file(td, td->file_size - st.st_size, 1))
                        return 1;
        }