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