]> mj.ucw.cz Git - libucw.git/blob - lib/fb-mmap.c
d7309321f9e50eb7c6582c941d3df803f5309fb7
[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   if (ll > PAGE_SIZE)
68     madvise(f->buffer, ll, MADV_SEQUENTIAL);
69   f->bufend = f->buffer + l;
70   f->bptr = f->buffer + (f->pos - pos0);
71   F->window_pos = f->pos;
72 }
73
74 static int
75 bfmm_refill(struct fastbuf *f)
76 {
77   struct fb_mmap *F = FB_MMAP(f);
78
79   DBG("Refill <- %p %p %p %p", f->buffer, f->bptr, f->bstop, f->bufend);
80   if (f->pos >= F->file_size)
81     return 0;
82   if (f->bstop >= f->bufend)
83     bfmm_map_window(f);
84   if (F->window_pos + (f->bufend - f->buffer) > F->file_size)
85     f->bstop = f->buffer + (F->file_size - F->window_pos);
86   else
87     f->bstop = f->bufend;
88   f->pos = F->window_pos + (f->bstop - f->buffer);
89   DBG(" -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
90   return 1;
91 }
92
93 static void
94 bfmm_spout(struct fastbuf *f)
95 {
96   struct fb_mmap *F = FB_MMAP(f);
97   sh_off_t end = f->pos + (f->bptr - f->bstop);
98
99   DBG("Spout <- %p %p %p %p", f->buffer, f->bptr, f->bstop, f->bufend);
100   if (end > F->file_size)
101     F->file_size = end;
102   if (f->bptr < f->bufend)
103     return;
104   f->pos = end;
105   if (f->pos >= F->file_extend)
106     {
107       F->file_extend = ALIGN(F->file_extend + MMAP_EXTEND_SIZE, (sh_off_t)PAGE_SIZE);
108       if (sh_ftruncate(F->fd, F->file_extend))
109         die("ftruncate(%s): %m", f->name);
110     }
111   bfmm_map_window(f);
112   f->bstop = f->bptr;
113   DBG(" -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
114 }
115
116 static void
117 bfmm_seek(struct fastbuf *f, sh_off_t pos, int whence)
118 {
119   if (whence == SEEK_END)
120     pos += FB_MMAP(f)->file_size;
121   else
122     ASSERT(whence == SEEK_SET);
123   ASSERT(pos >= 0 && pos <= FB_MMAP(f)->file_size);
124   f->pos = pos;
125   f->bptr = f->bstop = f->bufend;       /* force refill/spout call */
126   DBG("Seek -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
127 }
128
129 static void
130 bfmm_close(struct fastbuf *f)
131 {
132   struct fb_mmap *F = FB_MMAP(f);
133
134   if (f->buffer)
135     munmap(f->buffer, ALIGN(f->bufend-f->buffer, PAGE_SIZE));
136   if (F->file_extend > F->file_size &&
137       sh_ftruncate(F->fd, F->file_size))
138     die("ftruncate(%s): %m", f->name);
139   switch (F->is_temp_file)
140     {
141     case 1:
142       if (unlink(f->name) < 0)
143         log(L_ERROR, "unlink(%s): %m", f->name);
144     case 0:
145       close(F->fd);
146     }
147   xfree(f);
148 }
149
150 static int
151 bfmm_config(struct fastbuf *f, uns item, int value)
152 {
153   switch (item)
154     {
155     case BCONFIG_IS_TEMP_FILE:
156       FB_MMAP(f)->is_temp_file = value;
157       return 0;
158     default:
159       return -1;
160     }
161 }
162
163 static struct fastbuf *
164 bfmmopen_internal(int fd, byte *name, uns mode)
165 {
166   int namelen = strlen(name) + 1;
167   struct fb_mmap *F = xmalloc(sizeof(struct fb_mmap) + namelen);
168   struct fastbuf *f = &F->fb;
169
170   bzero(F, sizeof(*F));
171   f->name = (byte *)(F+1);
172   memcpy(f->name, name, namelen);
173   F->fd = fd;
174   F->file_extend = F->file_size = sh_seek(fd, 0, SEEK_END);
175   if (mode & O_APPEND)
176     f->pos = F->file_size;
177   mode &= ~O_APPEND;
178   if (mode & O_WRONLY || mode & O_RDWR)
179     F->is_writeable = 1;
180
181   f->refill = bfmm_refill;
182   f->spout = bfmm_spout;
183   f->seek = bfmm_seek;
184   f->close = bfmm_close;
185   f->config = bfmm_config;
186   return f;
187 }
188
189 struct fastbuf *
190 bopen_mm(byte *name, uns mode)
191 {
192   int fd = sh_open(name, mode, 0666);
193   if (fd < 0)
194     die("Unable to %s file %s: %m",
195         (mode & O_CREAT) ? "create" : "open", name);
196   return bfmmopen_internal(fd, name, mode);
197 }
198
199 #ifdef TEST
200
201 int main(int argc, char **argv)
202 {
203   struct fastbuf *f = bopen_mm(argv[1], O_RDONLY);
204   struct fastbuf *g = bopen_mm(argv[2], O_RDWR | O_CREAT | O_TRUNC);
205   int c;
206
207   DBG("Copying");
208   while ((c = bgetc(f)) >= 0)
209     bputc(g, c);
210   bclose(f);
211   DBG("Seek inside last block");
212   bsetpos(g, btell(g)-1333);
213   bputc(g, 13);
214   DBG("Seek to the beginning & write");
215   bsetpos(g, 1333);
216   bputc(g, 13);
217   DBG("flush");
218   bflush(g);
219   bputc(g, 13);
220   bflush(g);
221   DBG("Seek nearby & read");
222   bsetpos(g, 133);
223   bgetc(g);
224   DBG("Seek far & read");
225   bsetpos(g, 133333);
226   bgetc(g);
227   DBG("Closing");
228   bclose(g);
229
230   return 0;
231 }
232
233 #endif