From: Pavel Charvat Date: Tue, 29 May 2007 10:57:50 +0000 (+0200) Subject: unfinished parametrized fastbufs X-Git-Tag: holmes-import~506^2~13^2~119 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=0aca534da2155e77602f26c094f9eb31d5febf83;p=libucw.git unfinished parametrized fastbufs --- diff --git a/lib/fastbuf.h b/lib/fastbuf.h index 8d7a164e..14b403c7 100644 --- a/lib/fastbuf.h +++ b/lib/fastbuf.h @@ -75,6 +75,7 @@ struct fastbuf { /* FastIO on standard files (specify buffer size 0 to enable mmaping) */ +struct fastbuf *bfdopen_internal(int fd, uns buflen, byte *name); struct fastbuf *bopen(byte *name, uns mode, uns buflen); struct fastbuf *bopen_try(byte *name, uns mode, uns buflen); struct fastbuf *bopen_tmp(uns buflen); @@ -96,7 +97,10 @@ struct fastbuf *bopen_mm(byte *name, uns mode); /* FastIO on files opened with O_DIRECT (see fb-direct.c for description) */ +extern uns fbdir_cheat; + struct asio_queue; +struct fastbuf *fbdir_open_fd_internal(int fd, struct asio_queue *io_queue, byte *name); struct fastbuf *fbdir_open(byte *name, uns mode, struct asio_queue *io_queue); struct fastbuf *fbdir_open_try(byte *name, uns mode, struct asio_queue *io_queue); struct fastbuf *fbdir_open_fd(int fd, struct asio_queue *io_queue); @@ -113,11 +117,12 @@ enum fb_type { struct fb_params { enum fb_type type; uns buffer_size; + struct asio_queue *asio; }; struct cf_section; extern struct cf_section fbpar_cf; -extern struct fb_params fbpar_defaults; +extern struct fb_params fbpar_def; struct fastbuf *bopen_file(byte *name, int mode, struct fb_params *params); struct fastbuf *bopen_file_try(byte *name, int mode, struct fb_params *params); diff --git a/lib/fb-direct.c b/lib/fb-direct.c index 9eca31fd..e56d9840 100644 --- a/lib/fb-direct.c +++ b/lib/fb-direct.c @@ -37,7 +37,7 @@ #include #include -static uns fbdir_cheat; +uns fbdir_cheat; static uns fbdir_buffer_size = 65536; static uns fbdir_read_ahead = 1; static uns fbdir_write_back = 1; @@ -295,8 +295,8 @@ fbdir_config(struct fastbuf *f, uns item, int value) } } -static struct fastbuf * -fbdir_open_internal(byte *name, int fd, struct asio_queue *q) +struct fastbuf * +fbdir_open_fd_internal(int fd, struct asio_queue *q, byte *name) { int namelen = strlen(name) + 1; struct fb_direct *F = xmalloc(sizeof(struct fb_direct) + namelen); @@ -323,48 +323,25 @@ fbdir_open_internal(byte *name, int fd, struct asio_queue *q) struct fastbuf * fbdir_open_try(byte *name, uns mode, struct asio_queue *q) { - if (!fbdir_cheat) - mode |= O_DIRECT; - int fd = sh_open(name, mode, 0666); - if (fd < 0) - return NULL; - struct fastbuf *b = fbdir_open_internal(name, fd, q); - if (mode & O_APPEND) - fbdir_seek(b, 0, SEEK_END); - return b; + return bopen_file_try(name, mode, &(struct fb_params){ .type = FB_DIRECT, .asio = q }); } struct fastbuf * fbdir_open(byte *name, uns mode, struct asio_queue *q) { - struct fastbuf *b = fbdir_open_try(name, mode, q); - if (!b) - die("Unable to %s file %s: %m", - (mode & O_CREAT) ? "create" : "open", name); - return b; + return bopen_file(name, mode, &(struct fb_params){ .type = FB_DIRECT, .asio = q }); } struct fastbuf * fbdir_open_fd(int fd, struct asio_queue *q) { - byte x[32]; - - sprintf(x, "fd%d", fd); - if (!fbdir_cheat && fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_DIRECT) < 0) - log(L_WARN, "Cannot set O_DIRECT on fd %d: %m", fd); - return fbdir_open_internal(x, fd, q); + return bopen_fd(fd, &(struct fb_params){ .type = FB_DIRECT, .asio = q }); } struct fastbuf * fbdir_open_tmp(struct asio_queue *q) { - byte buf[TEMP_FILE_NAME_LEN]; - struct fastbuf *f; - - temp_file_name(buf); - f = fbdir_open(buf, O_RDWR | O_CREAT | O_TRUNC, q); - bconfig(f, BCONFIG_IS_TEMP_FILE, 1); - return f; + return bopen_tmp_file(&(struct fb_params){ .type = FB_DIRECT, .asio = q }); } #ifdef TEST diff --git a/lib/fb-file.c b/lib/fb-file.c index 0d1cc96c..cb479f3b 100644 --- a/lib/fb-file.c +++ b/lib/fb-file.c @@ -91,7 +91,7 @@ bfd_config(struct fastbuf *f, uns item, int value) } } -static struct fastbuf * +struct fastbuf * bfdopen_internal(int fd, uns buflen, byte *name) { int namelen = strlen(name) + 1; @@ -118,34 +118,19 @@ bfdopen_internal(int fd, uns buflen, byte *name) struct fastbuf * bopen_try(byte *name, uns mode, uns buflen) { - int fd = sh_open(name, mode, 0666); - if (fd < 0) - return NULL; - struct fastbuf *b = bfdopen_internal(fd, buflen, name); - if (mode & O_APPEND) - bfd_seek(b, 0, SEEK_END); - return b; + return bopen_file_try(name, mode, &(struct fb_params){ .type = FB_STD, .buffer_size = buflen }); } struct fastbuf * bopen(byte *name, uns mode, uns buflen) { - if (!buflen) - return bopen_mm(name, mode); - struct fastbuf *b = bopen_try(name, mode, buflen); - if (!b) - die("Unable to %s file %s: %m", - (mode & O_CREAT) ? "create" : "open", name); - return b; + return bopen_file(name, mode, &(struct fb_params){ .type = FB_STD, .buffer_size = buflen }); } struct fastbuf * bfdopen(int fd, uns buflen) { - byte x[32]; - - sprintf(x, "fd%d", fd); - return bfdopen_internal(fd, buflen, x); + return bopen_fd(fd, &(struct fb_params){ .type = FB_STD, .buffer_size = buflen }); } struct fastbuf * diff --git a/lib/fb-param.c b/lib/fb-param.c index 6fc5f27a..714ef647 100644 --- a/lib/fb-param.c +++ b/lib/fb-param.c @@ -9,9 +9,13 @@ #include "lib/lib.h" #include "lib/conf.h" +#include "lib/lfs.h" #include "lib/fastbuf.h" -struct fb_params fbpar_defaults = { +#include +#include + +struct fb_params fbpar_def = { .buffer_size = 65536, }; @@ -29,7 +33,7 @@ struct cf_section fbpar_cf = { static struct cf_section fbpar_global_cf = { CF_ITEMS { - CF_SECTION("Defaults", &fbpar_defaults, &fbpar_cf), + CF_SECTION("Defaults", &fbpar_def, &fbpar_cf), CF_END } }; @@ -40,62 +44,70 @@ fbpar_global_init(void) cf_declare_section("FBParam", &fbpar_global_cf, 0); } -struct fastbuf * -bopen_file(byte *name, int mode, struct fb_params *params) +static struct fastbuf * +bopen_fd_internal(int fd, struct fb_params *params, byte *name) { - params = params ? : &fbpar_defaults; + struct fastbuf *fb; switch (params->type) { case FB_STD: - return bopen(name, mode, params->buffer_size); + return bfdopen_internal(fd, params->buffer_size, name); case FB_DIRECT: - return fbdir_open(name, mode, NULL); - default: + fb = fbdir_open_fd_internal(fd, params->asio, name); + if (!fbdir_cheat && fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_DIRECT) < 0) + log(L_WARN, "Cannot set O_DIRECT on fd %d: %m", fd); + return fb; + case FB_MMAP: + // FIXME ASSERT(0); } + ASSERT(0); +} + +static struct fastbuf * +bopen_file_internal(byte *name, int mode, struct fb_params *params, int try) +{ + if (params->type == FB_DIRECT && !fbdir_cheat) + mode |= O_DIRECT; + int fd = sh_open(name, mode, 0666); + if (fd < 0) + if (try) + return NULL; + else + die("Unable to %s file %s: %m", (mode & O_CREAT) ? "create" : "open", name); + struct fastbuf *fb = bopen_fd_internal(fd, params, name); + ASSERT(fb); + if (mode & O_APPEND) + bseek(fb, 0, SEEK_END); + return fb; +} + +struct fastbuf * +bopen_file(byte *name, int mode, struct fb_params *params) +{ + return bopen_file_internal(name, mode, params ? : &fbpar_def, 0); } struct fastbuf * bopen_file_try(byte *name, int mode, struct fb_params *params) { - params = params ? : &fbpar_defaults; - switch (params->type) - { - case FB_STD: - return bopen_try(name, mode, params->buffer_size); - case FB_DIRECT: - return fbdir_open_try(name, mode, NULL); - default: - ASSERT(0); - } + return bopen_file_internal(name, mode, params ? : &fbpar_def, 1); } struct fastbuf * bopen_fd(int fd, struct fb_params *params) { - params = params ? : &fbpar_defaults; - switch (params->type) - { - case FB_STD: - return bfdopen(fd, params->buffer_size); - case FB_DIRECT: - return fbdir_open_fd(fd, NULL); - default: - ASSERT(0); - } + byte x[32]; + sprintf(x, "fd%d", fd); + return bopen_fd_internal(fd, params ? : &fbpar_def, x); } struct fastbuf * bopen_tmp_file(struct fb_params *params) { - params = params ? : &fbpar_defaults; - switch (params->type) - { - case FB_STD: - return bopen_tmp(params->buffer_size); - case FB_DIRECT: - return fbdir_open_tmp(NULL); - default: - ASSERT(0); - } + byte buf[TEMP_FILE_NAME_LEN]; + temp_file_name(buf); + struct fastbuf *fb = bopen_file_internal(buf, O_RDWR | O_CREAT | O_TRUNC, params, 0); + bconfig(fb, BCONFIG_IS_TEMP_FILE, 1); + return fb; }