]> mj.ucw.cz Git - libucw.git/blob - lib/fb-mmap.c
c9d7b10cd7b0b0547bbea25a167bb1f56e825fa8
[libucw.git] / lib / fb-mmap.c
1 /*
2  *      Sherlock Library -- Fast Buffered I/O on Memory-Mapped Files
3  *
4  *      (c) 2002 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 /*
11  *  FIXME:
12  *  - problems with temp files
13  *  - O_WRONLY ? (& generally processing of mode bits)
14  */
15
16 #ifdef TEST
17 #define MMAP_WINDOW_SIZE 16*PAGE_SIZE
18 #define MMAP_EXTEND_SIZE 4*PAGE_SIZE
19 #else
20 #define MMAP_WINDOW_SIZE 256*PAGE_SIZE
21 #define MMAP_EXTEND_SIZE 256*PAGE_SIZE
22 #endif
23
24 #include "lib/lib.h"
25 #include "lib/fastbuf.h"
26 #include "lib/lfs.h"
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <sys/mman.h>
33 #include <sys/user.h>
34
35 struct fb_mmap {
36   struct fastbuf fb;
37   int fd;
38   int dummy;                            /* FIXME: dirty hack for is_temp_file, remove */
39   sh_off_t file_size;
40   sh_off_t file_extend;
41   sh_off_t window_pos;
42   int is_writeable;
43 };
44 #define FB_MMAP(f) ((struct fb_mmap *)(f)->is_fastbuf)
45
46 static void
47 bfmm_map_window(struct fastbuf *f)
48 {
49   struct fb_mmap *F = FB_MMAP(f);
50   sh_off_t pos0 = f->pos & ~(sh_off_t)(PAGE_SIZE-1);
51   int l = MIN(MMAP_WINDOW_SIZE, F->file_extend - pos0);
52   uns ll = ALIGN(l, PAGE_SIZE);
53   uns oll = ALIGN(f->bufend - f->buffer, PAGE_SIZE);
54   int prot = F->is_writeable ? (PROT_READ | PROT_WRITE) : PROT_READ;
55
56   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);
57   if (ll != oll)
58     {
59       munmap(f->buffer, oll);
60       f->buffer = NULL;
61     }
62   if (!f->buffer)
63     f->buffer = sh_mmap(NULL, ll, prot, MAP_SHARED, F->fd, pos0);
64   else
65     f->buffer = sh_mmap(f->buffer, ll, prot, MAP_SHARED | MAP_FIXED, F->fd, pos0);
66   if (f->buffer == (byte *) MAP_FAILED)
67     die("mmap(%s): %m", f->name);
68   if (ll > PAGE_SIZE)
69     madvise(f->buffer, ll, MADV_SEQUENTIAL);
70   f->bufend = f->buffer + l;
71   f->bptr = f->buffer + (f->pos - pos0);
72   F->window_pos = f->pos;
73 }
74
75 static int
76 bfmm_refill(struct fastbuf *f)
77 {
78   struct fb_mmap *F = FB_MMAP(f);
79
80   DBG("Refill <- %p %p %p %p", f->buffer, f->bptr, f->bstop, f->bufend);
81   if (f->pos >= F->file_size)
82     return 0;
83   if (f->bstop >= f->bufend)
84     bfmm_map_window(f);
85   if (F->window_pos + (f->bufend - f->buffer) > F->file_size)
86     f->bstop = f->buffer + (F->file_size - F->window_pos);
87   else
88     f->bstop = f->bufend;
89   f->pos = F->window_pos + (f->bstop - f->buffer);
90   DBG(" -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
91   return 1;
92 }
93
94 static void
95 bfmm_spout(struct fastbuf *f)
96 {
97   struct fb_mmap *F = FB_MMAP(f);
98   sh_off_t end = f->pos + (f->bptr - f->bstop);
99
100   DBG("Spout <- %p %p %p %p", f->buffer, f->bptr, f->bstop, f->bufend);
101   if (end > F->file_size)
102     F->file_size = end;
103   if (f->bptr < f->bufend)
104     return;
105   f->pos = end;
106   if (f->pos >= F->file_extend)
107     {
108       F->file_extend = ALIGN(F->file_extend + MMAP_EXTEND_SIZE, (sh_off_t)PAGE_SIZE);
109       if (sh_ftruncate(F->fd, F->file_extend))
110         die("ftruncate(%s): %m", f->name);
111     }
112   bfmm_map_window(f);
113   f->bstop = f->bptr;
114   DBG(" -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
115 }
116
117 static void
118 bfmm_seek(struct fastbuf *f, sh_off_t pos, int whence)
119 {
120   if (whence == SEEK_END)
121     pos += FB_MMAP(f)->file_size;
122   else
123     ASSERT(whence == SEEK_SET);
124   ASSERT(pos >= 0 && pos <= FB_MMAP(f)->file_size);
125   f->pos = pos;
126   f->bptr = f->bstop = f->bufend;       /* force refill/spout call */
127   DBG("Seek -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
128 }
129
130 static void
131 bfmm_close(struct fastbuf *f)
132 {
133   struct fb_mmap *F = FB_MMAP(f);
134
135   if (f->buffer)
136     munmap(f->buffer, ALIGN(f->bufend-f->buffer, PAGE_SIZE));
137   if (F->file_extend > F->file_size &&
138       sh_ftruncate(F->fd, F->file_size))
139     die("ftruncate(%s): %m", f->name);
140   close(F->fd);
141   xfree(f);
142 }
143
144 static struct fastbuf *
145 bfmmopen_internal(int fd, byte *name, uns mode)
146 {
147   int namelen = strlen(name) + 1;
148   struct fb_mmap *F = xmalloc(sizeof(struct fb_mmap) + namelen);
149   struct fastbuf *f = &F->fb;
150
151   bzero(F, sizeof(*F));
152   f->name = (byte *)(F+1);
153   memcpy(f->name, name, namelen);
154   F->fd = fd;
155   F->file_extend = F->file_size = sh_seek(fd, 0, SEEK_END);
156   if (mode & O_APPEND)
157     f->pos = F->file_size;
158   mode &= ~O_APPEND;
159   if (mode & O_WRONLY || mode & O_RDWR)
160     F->is_writeable = 1;
161
162   f->refill = bfmm_refill;
163   f->spout = bfmm_spout;
164   f->seek = bfmm_seek;
165   f->close = bfmm_close;
166   return f;
167 }
168
169 struct fastbuf *
170 bopen_mm(byte *name, uns mode)
171 {
172   int fd = sh_open(name, mode, 0666);
173   if (fd < 0)
174     die("Unable to %s file %s: %m",
175         (mode & O_CREAT) ? "create" : "open", name);
176   return bfmmopen_internal(fd, name, mode);
177 }
178
179 #ifdef TEST
180
181 int main(int argc, char **argv)
182 {
183   struct fastbuf *f = bopen_mm(argv[1], O_RDONLY);
184   struct fastbuf *g = bopen_mm(argv[2], O_RDWR | O_CREAT | O_TRUNC);
185   int c;
186
187   DBG("Copying");
188   while ((c = bgetc(f)) >= 0)
189     bputc(g, c);
190   bclose(f);
191   DBG("Seek inside last block");
192   bsetpos(g, btell(g)-1333);
193   bputc(g, 13);
194   DBG("Seek to the beginning & write");
195   bsetpos(g, 1333);
196   bputc(g, 13);
197   DBG("flush");
198   bflush(g);
199   bputc(g, 13);
200   bflush(g);
201   DBG("Seek nearby & read");
202   bsetpos(g, 133);
203   bgetc(g);
204   DBG("Seek far & read");
205   bsetpos(g, 133333);
206   bgetc(g);
207   DBG("Closing");
208   bclose(g);
209
210   return 0;
211 }
212
213 #endif