]> mj.ucw.cz Git - libucw.git/blobdiff - lib/fb-mmap.c
Merge with git+ssh://git.ucw.cz/projects/sherlock/GIT/sherlock.git
[libucw.git] / lib / fb-mmap.c
index c9d7b10cd7b0b0547bbea25a167bb1f56e825fa8..a57d1034c32b04cfdaa4aec66912a633d7b4ee5d 100644 (file)
@@ -1,5 +1,5 @@
 /*
 /*
- *     Sherlock Library -- Fast Buffered I/O on Memory-Mapped Files
+ *     UCW Library -- Fast Buffered I/O on Memory-Mapped Files
  *
  *     (c) 2002 Martin Mares <mj@ucw.cz>
  *
  *
  *     (c) 2002 Martin Mares <mj@ucw.cz>
  *
@@ -7,39 +7,43 @@
  *     of the GNU Lesser General Public License.
  */
 
  *     of the GNU Lesser General Public License.
  */
 
-/*
- *  FIXME:
- *  - problems with temp files
- *  - O_WRONLY ? (& generally processing of mode bits)
- */
-
-#ifdef TEST
-#define MMAP_WINDOW_SIZE 16*PAGE_SIZE
-#define MMAP_EXTEND_SIZE 4*PAGE_SIZE
-#else
-#define MMAP_WINDOW_SIZE 256*PAGE_SIZE
-#define MMAP_EXTEND_SIZE 256*PAGE_SIZE
-#endif
+#undef LOCAL_DEBUG
 
 #include "lib/lib.h"
 #include "lib/fastbuf.h"
 #include "lib/lfs.h"
 
 #include "lib/lib.h"
 #include "lib/fastbuf.h"
 #include "lib/lfs.h"
+#include "lib/conf.h"
 
 
-#include <stdlib.h>
 #include <string.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <sys/mman.h>
 #include <string.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <sys/mman.h>
-#include <sys/user.h>
+
+static uns mmap_window_size = 16*CPU_PAGE_SIZE;
+static uns mmap_extend_size = 4*CPU_PAGE_SIZE;
+
+static struct cf_section fbmm_config = {
+  CF_ITEMS {
+    CF_UNS("WindowSize", &mmap_window_size),
+    CF_UNS("ExtendSize", &mmap_extend_size),
+    CF_END
+  }
+};
+
+static void CONSTRUCTOR fbmm_init_config(void)
+{
+  cf_declare_section("FBMMap", &fbmm_config, 0);
+}
 
 struct fb_mmap {
   struct fastbuf fb;
   int fd;
 
 struct fb_mmap {
   struct fastbuf fb;
   int fd;
-  int dummy;                           /* FIXME: dirty hack for is_temp_file, remove */
+  int is_temp_file;
   sh_off_t file_size;
   sh_off_t file_extend;
   sh_off_t window_pos;
   sh_off_t file_size;
   sh_off_t file_extend;
   sh_off_t window_pos;
-  int is_writeable;
+  uns window_size;
+  int mode;
 };
 #define FB_MMAP(f) ((struct fb_mmap *)(f)->is_fastbuf)
 
 };
 #define FB_MMAP(f) ((struct fb_mmap *)(f)->is_fastbuf)
 
@@ -47,29 +51,31 @@ static void
 bfmm_map_window(struct fastbuf *f)
 {
   struct fb_mmap *F = FB_MMAP(f);
 bfmm_map_window(struct fastbuf *f)
 {
   struct fb_mmap *F = FB_MMAP(f);
-  sh_off_t pos0 = f->pos & ~(sh_off_t)(PAGE_SIZE-1);
-  int l = MIN(MMAP_WINDOW_SIZE, F->file_extend - pos0);
-  uns ll = ALIGN(l, PAGE_SIZE);
-  uns oll = ALIGN(f->bufend - f->buffer, PAGE_SIZE);
-  int prot = F->is_writeable ? (PROT_READ | PROT_WRITE) : PROT_READ;
+  sh_off_t pos0 = f->pos & ~(sh_off_t)(CPU_PAGE_SIZE-1);
+  int l = MIN((sh_off_t)mmap_window_size, F->file_extend - pos0);
+  uns ll = ALIGN_TO(l, CPU_PAGE_SIZE);
+  int prot = ((F->mode & O_ACCMODE) == O_RDONLY) ? PROT_READ : (PROT_READ | PROT_WRITE);
 
   DBG(" ... Mapping %x(%x)+%x(%x) len=%x extend=%x", (int)pos0, (int)f->pos, ll, l, (int)F->file_size, (int)F->file_extend);
 
   DBG(" ... Mapping %x(%x)+%x(%x) len=%x extend=%x", (int)pos0, (int)f->pos, ll, l, (int)F->file_size, (int)F->file_extend);
-  if (ll != oll)
+  if (ll != F->window_size && f->buffer)
     {
     {
-      munmap(f->buffer, oll);
+      munmap(f->buffer, F->window_size);
       f->buffer = NULL;
     }
       f->buffer = NULL;
     }
+  F->window_size = ll;
   if (!f->buffer)
     f->buffer = sh_mmap(NULL, ll, prot, MAP_SHARED, F->fd, pos0);
   else
     f->buffer = sh_mmap(f->buffer, ll, prot, MAP_SHARED | MAP_FIXED, F->fd, pos0);
   if (f->buffer == (byte *) MAP_FAILED)
     die("mmap(%s): %m", f->name);
   if (!f->buffer)
     f->buffer = sh_mmap(NULL, ll, prot, MAP_SHARED, F->fd, pos0);
   else
     f->buffer = sh_mmap(f->buffer, ll, prot, MAP_SHARED | MAP_FIXED, F->fd, pos0);
   if (f->buffer == (byte *) MAP_FAILED)
     die("mmap(%s): %m", f->name);
-  if (ll > PAGE_SIZE)
+#ifdef MADV_SEQUENTIAL
+  if (ll > CPU_PAGE_SIZE)
     madvise(f->buffer, ll, MADV_SEQUENTIAL);
     madvise(f->buffer, ll, MADV_SEQUENTIAL);
+#endif
   f->bufend = f->buffer + l;
   f->bptr = f->buffer + (f->pos - pos0);
   f->bufend = f->buffer + l;
   f->bptr = f->buffer + (f->pos - pos0);
-  F->window_pos = f->pos;
+  F->window_pos = pos0;
 }
 
 static int
 }
 
 static int
@@ -105,7 +111,7 @@ bfmm_spout(struct fastbuf *f)
   f->pos = end;
   if (f->pos >= F->file_extend)
     {
   f->pos = end;
   if (f->pos >= F->file_extend)
     {
-      F->file_extend = ALIGN(F->file_extend + MMAP_EXTEND_SIZE, (sh_off_t)PAGE_SIZE);
+      F->file_extend = ALIGN_TO(F->file_extend + mmap_extend_size, (sh_off_t)CPU_PAGE_SIZE);
       if (sh_ftruncate(F->fd, F->file_extend))
        die("ftruncate(%s): %m", f->name);
     }
       if (sh_ftruncate(F->fd, F->file_extend))
        die("ftruncate(%s): %m", f->name);
     }
@@ -114,7 +120,7 @@ bfmm_spout(struct fastbuf *f)
   DBG(" -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
 }
 
   DBG(" -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
 }
 
-static void
+static int
 bfmm_seek(struct fastbuf *f, sh_off_t pos, int whence)
 {
   if (whence == SEEK_END)
 bfmm_seek(struct fastbuf *f, sh_off_t pos, int whence)
 {
   if (whence == SEEK_END)
@@ -123,8 +129,9 @@ bfmm_seek(struct fastbuf *f, sh_off_t pos, int whence)
     ASSERT(whence == SEEK_SET);
   ASSERT(pos >= 0 && pos <= FB_MMAP(f)->file_size);
   f->pos = pos;
     ASSERT(whence == SEEK_SET);
   ASSERT(pos >= 0 && pos <= FB_MMAP(f)->file_size);
   f->pos = pos;
-  f->bptr = f->bstop = f->bufend;      /* force refill/spout call */
+  f->bptr = f->bstop = f->bufend = f->buffer;  /* force refill/spout call */
   DBG("Seek -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
   DBG("Seek -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
+  return 1;
 }
 
 static void
 }
 
 static void
@@ -133,16 +140,32 @@ bfmm_close(struct fastbuf *f)
   struct fb_mmap *F = FB_MMAP(f);
 
   if (f->buffer)
   struct fb_mmap *F = FB_MMAP(f);
 
   if (f->buffer)
-    munmap(f->buffer, ALIGN(f->bufend-f->buffer, PAGE_SIZE));
+    munmap(f->buffer, F->window_size);
   if (F->file_extend > F->file_size &&
       sh_ftruncate(F->fd, F->file_size))
     die("ftruncate(%s): %m", f->name);
   if (F->file_extend > F->file_size &&
       sh_ftruncate(F->fd, F->file_size))
     die("ftruncate(%s): %m", f->name);
-  close(F->fd);
+  bclose_file_helper(f, F->fd, F->is_temp_file);
   xfree(f);
 }
 
   xfree(f);
 }
 
-static struct fastbuf *
-bfmmopen_internal(int fd, byte *name, uns mode)
+static int
+bfmm_config(struct fastbuf *f, uns item, int value)
+{
+  int orig;
+
+  switch (item)
+    {
+    case BCONFIG_IS_TEMP_FILE:
+      orig = FB_MMAP(f)->is_temp_file;
+      FB_MMAP(f)->is_temp_file = value;
+      return orig;
+    default:
+      return -1;
+    }
+}
+
+struct fastbuf *
+bfmmopen_internal(int fd, const char *name, uns mode)
 {
   int namelen = strlen(name) + 1;
   struct fb_mmap *F = xmalloc(sizeof(struct fb_mmap) + namelen);
 {
   int namelen = strlen(name) + 1;
   struct fb_mmap *F = xmalloc(sizeof(struct fb_mmap) + namelen);
@@ -153,35 +176,27 @@ bfmmopen_internal(int fd, byte *name, uns mode)
   memcpy(f->name, name, namelen);
   F->fd = fd;
   F->file_extend = F->file_size = sh_seek(fd, 0, SEEK_END);
   memcpy(f->name, name, namelen);
   F->fd = fd;
   F->file_extend = F->file_size = sh_seek(fd, 0, SEEK_END);
+  if (F->file_size < 0)
+    die("seek(%s): %m", name);
   if (mode & O_APPEND)
     f->pos = F->file_size;
   if (mode & O_APPEND)
     f->pos = F->file_size;
-  mode &= ~O_APPEND;
-  if (mode & O_WRONLY || mode & O_RDWR)
-    F->is_writeable = 1;
+  F->mode = mode;
 
   f->refill = bfmm_refill;
   f->spout = bfmm_spout;
   f->seek = bfmm_seek;
   f->close = bfmm_close;
 
   f->refill = bfmm_refill;
   f->spout = bfmm_spout;
   f->seek = bfmm_seek;
   f->close = bfmm_close;
+  f->config = bfmm_config;
   return f;
 }
 
   return f;
 }
 
-struct fastbuf *
-bopen_mm(byte *name, uns mode)
-{
-  int fd = sh_open(name, mode, 0666);
-  if (fd < 0)
-    die("Unable to %s file %s: %m",
-       (mode & O_CREAT) ? "create" : "open", name);
-  return bfmmopen_internal(fd, name, mode);
-}
-
 #ifdef TEST
 
 int main(int argc, char **argv)
 {
 #ifdef TEST
 
 int main(int argc, char **argv)
 {
-  struct fastbuf *f = bopen_mm(argv[1], O_RDONLY);
-  struct fastbuf *g = bopen_mm(argv[2], O_RDWR | O_CREAT | O_TRUNC);
+  struct fb_params par = { .type = FB_MMAP };
+  struct fastbuf *f = bopen_file(argv[1], O_RDONLY, &par);
+  struct fastbuf *g = bopen_file(argv[2], O_RDWR | O_CREAT | O_TRUNC, &par);
   int c;
 
   DBG("Copying");
   int c;
 
   DBG("Copying");