2 * UCW Library -- Fast Buffered I/O on O_DIRECT Files
4 * (c) 2006 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 * This is a fastbuf backend for fast streaming I/O using O_DIRECT and
12 * the asynchronous I/O module. It's designed for use on large files
13 * which don't fit in the disk cache.
17 * - All operations with a single fbdirect handle must be done
18 * within a single thread, unless you provide a custom I/O queue
19 * and take care of locking.
21 * FIXME: what if the OS doesn't support O_DIRECT?
22 * FIXME: doc: don't mix threads
23 * FIXME: unaligned seeks and partial writes?
24 * FIXME: merge with other file-oriented fastbufs
30 #include "lib/fastbuf.h"
34 #include "lib/getopt.h"
41 static uns fbdir_cheat;
42 static uns fbdir_buffer_size = 65536;
43 static uns fbdir_read_ahead = 1;
44 static uns fbdir_write_back = 1;
46 static struct cf_section fbdir_cf = {
48 CF_UNS("Cheat", &fbdir_cheat),
49 CF_UNS("BufferSize", &fbdir_buffer_size),
50 CF_UNS("ReadAhead", &fbdir_read_ahead),
51 CF_UNS("WriteBack", &fbdir_write_back),
56 #define FBDIR_ALIGN 512
58 static pthread_key_t fbdir_queue_key;
60 enum fbdir_mode { // Current operating mode
68 int fd; // File descriptor
69 int is_temp_file; // 0=normal file, 1=temporary file, delete on close, -1=shared FD
70 struct asio_queue *io_queue; // I/O queue to use
71 struct asio_queue *user_queue; // If io_queue was supplied by the user
72 struct asio_request *pending_read;
73 struct asio_request *done_read;
74 struct asio_request *active_buffer;
78 #define FB_DIRECT(f) ((struct fb_direct *)(f)->is_fastbuf)
80 static void CONSTRUCTOR
81 fbdir_global_init(void)
83 cf_declare_section("FBDirect", &fbdir_cf, 0);
84 if (pthread_key_create(&fbdir_queue_key, NULL) < 0)
85 die("Cannot create fbdir_queue_key: %m");
89 fbdir_read_sync(struct fb_direct *F)
91 while (F->pending_read)
93 struct asio_request *r = asio_wait(F->io_queue);
95 struct fb_direct *G = r->user_data;
97 ASSERT(G->pending_read == r && !G->done_read);
98 G->pending_read = NULL;
104 fbdir_change_mode(struct fb_direct *F, enum fbdir_mode mode)
108 DBG("FB-DIRECT: Switching mode to %d", mode);
114 fbdir_read_sync(F); // Wait for read-ahead requests to finish
115 if (F->done_read) // Return read-ahead requests if any
117 asio_put(F->done_read);
122 asio_sync(F->io_queue); // Wait for pending writebacks
125 if (F->active_buffer)
127 asio_put(F->active_buffer);
128 F->active_buffer = NULL;
134 fbdir_submit_read(struct fb_direct *F)
136 struct asio_request *r = asio_get(F->io_queue);
139 r->len = F->io_queue->buffer_size;
146 fbdir_refill(struct fastbuf *f)
148 struct fb_direct *F = FB_DIRECT(f);
150 DBG("FB-DIRECT: Refill");
154 if (!F->pending_read)
156 fbdir_change_mode(F, M_READ);
157 fbdir_submit_read(F);
160 ASSERT(F->done_read);
163 struct asio_request *r = F->done_read;
165 if (F->active_buffer)
166 asio_put(F->active_buffer);
167 F->active_buffer = r;
171 die("Error reading %s: %s", f->name, strerror(r->returned_errno));
172 f->bptr = f->buffer = r->buffer;
173 f->bstop = f->bufend = f->buffer + r->status;
176 fbdir_submit_read(F); // Read-ahead the next block
182 fbdir_spout(struct fastbuf *f)
184 struct fb_direct *F = FB_DIRECT(f);
185 struct asio_request *r;
187 DBG("FB-DIRECT: Spout");
189 fbdir_change_mode(F, M_WRITE);
190 r = F->active_buffer;
191 if (r && f->bptr > f->bstop)
193 r->op = ASIO_WRITE_BACK;
195 r->len = f->bptr - f->bstop;
196 ASSERT(!(f->pos % FBDIR_ALIGN) || fbdir_cheat);
198 if (!fbdir_cheat && r->len % FBDIR_ALIGN) // Have to simulate incomplete writes
200 r->len = ALIGN_TO(r->len, FBDIR_ALIGN);
202 asio_sync(F->io_queue);
203 DBG("FB-DIRECT: Truncating at %Ld", (long long)f->pos);
204 if (sh_ftruncate(F->fd, f->pos) < 0)
205 die("Error truncating %s: %m", f->name);
212 r = asio_get(F->io_queue);
213 f->bstop = f->bptr = f->buffer = r->buffer;
214 f->bufend = f->buffer + F->io_queue->buffer_size;
215 F->active_buffer = r;
219 fbdir_seek(struct fastbuf *f, sh_off_t pos, int whence)
221 DBG("FB-DIRECT: Seek %Ld %d", (long long)pos, whence);
223 if (whence == SEEK_SET && pos == f->pos)
226 fbdir_change_mode(FB_DIRECT(f), M_NULL); // Wait for all async requests to finish
227 sh_off_t l = sh_seek(FB_DIRECT(f)->fd, pos, whence);
229 die("lseek on %s: %m", f->name);
233 static struct asio_queue *
234 fbdir_get_io_queue(void)
236 struct asio_queue *q = pthread_getspecific(fbdir_queue_key);
239 q = xmalloc_zero(sizeof(struct asio_queue));
240 q->buffer_size = fbdir_buffer_size;
241 q->max_writebacks = fbdir_write_back;
243 pthread_setspecific(fbdir_queue_key, q);
246 DBG("FB-DIRECT: Got I/O queue, uc=%d", q->use_count);
251 fbdir_put_io_queue(void)
253 struct asio_queue *q = pthread_getspecific(fbdir_queue_key);
255 DBG("FB-DIRECT: Put I/O queue, uc=%d", q->use_count);
258 asio_cleanup_queue(q);
260 pthread_setspecific(fbdir_queue_key, NULL);
265 fbdir_close(struct fastbuf *f)
267 struct fb_direct *F = FB_DIRECT(f);
269 DBG("FB-DIRECT: Close");
271 fbdir_change_mode(F, M_NULL);
273 fbdir_put_io_queue();
275 switch (F->is_temp_file)
278 if (unlink(f->name) < 0)
279 log(L_ERROR, "unlink(%s): %m", f->name);
288 fbdir_config(struct fastbuf *f, uns item, int value)
292 case BCONFIG_IS_TEMP_FILE:
293 FB_DIRECT(f)->is_temp_file = value;
300 static struct fastbuf *
301 fbdir_open_internal(byte *name, int fd, struct asio_queue *q)
303 int namelen = strlen(name) + 1;
304 struct fb_direct *F = xmalloc(sizeof(struct fb_direct) + namelen);
305 struct fastbuf *f = &F->fb;
307 DBG("FB-DIRECT: Open");
308 bzero(F, sizeof(*F));
310 memcpy(f->name, name, namelen);
313 F->io_queue = F->user_queue = q;
315 F->io_queue = fbdir_get_io_queue();
316 f->refill = fbdir_refill;
317 f->spout = fbdir_spout;
318 f->seek = fbdir_seek;
319 f->close = fbdir_close;
320 f->config = fbdir_config;
321 f->can_overwrite_buffer = 2;
326 fbdirect_open_try(byte *name, uns mode, struct asio_queue *q)
330 int fd = sh_open(name, mode, 0666);
333 struct fastbuf *b = fbdir_open_internal(name, fd, q);
335 fbdir_seek(b, 0, SEEK_END);
340 fbdirect_open(byte *name, uns mode, struct asio_queue *q)
342 struct fastbuf *b = fbdirect_open_try(name, mode, q);
344 die("Unable to %s file %s: %m",
345 (mode & O_CREAT) ? "create" : "open", name);
350 fbdirect_open_fd(int fd, struct asio_queue *q)
354 sprintf(x, "fd%d", fd);
355 if (!fbdir_cheat && fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_DIRECT) < 0)
356 log(L_WARN, "Cannot set O_DIRECT on fd %d: %m", fd);
357 return fbdir_open_internal(x, fd, q);
362 int main(int argc, char **argv)
364 struct fastbuf *f, *t;
367 if (cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) >= 0)
368 die("Hey, whaddya want?");
369 f = (optind < argc) ? fbdirect_open(argv[optind++], O_RDONLY, NULL) : fbdirect_open_fd(0, NULL);
370 t = (optind < argc) ? fbdirect_open(argv[optind++], O_RDWR | O_CREAT | O_TRUNC, NULL) : fbdirect_open_fd(1, NULL);
373 ASSERT(btell(f) == btell(t));
375 #if 0 // This triggers unaligned write
382 ASSERT(btell(t) == 1);