2 * UCW Library -- Fast Buffered I/O on O_DIRECT Files
4 * (c) 2006--2007 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: unaligned seeks and partial writes?
28 #include "lib/fastbuf.h"
32 #include "lib/threads.h"
41 static struct cf_section fbdir_cf = {
43 CF_UNS("Cheat", &fbdir_cheat),
48 #define FBDIR_ALIGN 512
50 enum fbdir_mode { // Current operating mode
58 int fd; // File descriptor
60 struct asio_queue *io_queue; // I/O queue to use
61 struct asio_queue *user_queue; // If io_queue was supplied by the user
62 struct asio_request *pending_read;
63 struct asio_request *done_read;
64 struct asio_request *active_buffer;
68 #define FB_DIRECT(f) ((struct fb_direct *)(f)->is_fastbuf)
70 static void CONSTRUCTOR
71 fbdir_global_init(void)
73 cf_declare_section("FBDirect", &fbdir_cf, 0);
77 fbdir_read_sync(struct fb_direct *F)
79 while (F->pending_read)
81 struct asio_request *r = asio_wait(F->io_queue);
83 struct fb_direct *G = r->user_data;
85 ASSERT(G->pending_read == r && !G->done_read);
86 G->pending_read = NULL;
92 fbdir_change_mode(struct fb_direct *F, enum fbdir_mode mode)
96 DBG("FB-DIRECT: Switching mode to %d", mode);
102 fbdir_read_sync(F); // Wait for read-ahead requests to finish
103 if (F->done_read) // Return read-ahead requests if any
105 asio_put(F->done_read);
110 asio_sync(F->io_queue); // Wait for pending writebacks
113 if (F->active_buffer)
115 asio_put(F->active_buffer);
116 F->active_buffer = NULL;
122 fbdir_submit_read(struct fb_direct *F)
124 struct asio_request *r = asio_get(F->io_queue);
127 r->len = F->io_queue->buffer_size;
134 fbdir_refill(struct fastbuf *f)
136 struct fb_direct *F = FB_DIRECT(f);
138 DBG("FB-DIRECT: Refill");
142 if (!F->pending_read)
144 fbdir_change_mode(F, M_READ);
145 fbdir_submit_read(F);
148 ASSERT(F->done_read);
151 struct asio_request *r = F->done_read;
153 if (F->active_buffer)
154 asio_put(F->active_buffer);
155 F->active_buffer = r;
159 die("Error reading %s: %s", f->name, strerror(r->returned_errno));
160 f->bptr = f->buffer = r->buffer;
161 f->bstop = f->bufend = f->buffer + r->status;
164 fbdir_submit_read(F); // Read-ahead the next block
170 fbdir_spout(struct fastbuf *f)
172 struct fb_direct *F = FB_DIRECT(f);
173 struct asio_request *r;
175 DBG("FB-DIRECT: Spout");
177 fbdir_change_mode(F, M_WRITE);
178 r = F->active_buffer;
179 if (r && f->bptr > f->bstop)
181 r->op = ASIO_WRITE_BACK;
183 r->len = f->bptr - f->bstop;
184 ASSERT(!(f->pos % FBDIR_ALIGN) || fbdir_cheat);
186 if (!fbdir_cheat && r->len % FBDIR_ALIGN) // Have to simulate incomplete writes
188 r->len = ALIGN_TO(r->len, FBDIR_ALIGN);
190 asio_sync(F->io_queue);
191 DBG("FB-DIRECT: Truncating at %llu", (long long)f->pos);
192 if (sh_ftruncate(F->fd, f->pos) < 0)
193 die("Error truncating %s: %m", f->name);
200 r = asio_get(F->io_queue);
201 f->bstop = f->bptr = f->buffer = r->buffer;
202 f->bufend = f->buffer + F->io_queue->buffer_size;
203 F->active_buffer = r;
207 fbdir_seek(struct fastbuf *f, sh_off_t pos, int whence)
209 DBG("FB-DIRECT: Seek %llu %d", (long long)pos, whence);
211 if (whence == SEEK_SET && pos == f->pos)
214 fbdir_change_mode(FB_DIRECT(f), M_NULL); // Wait for all async requests to finish
215 sh_off_t l = sh_seek(FB_DIRECT(f)->fd, pos, whence);
222 static struct asio_queue *
223 fbdir_get_io_queue(uns buffer_size, uns write_back)
225 struct ucwlib_context *ctx = ucwlib_thread_context();
226 struct asio_queue *q = ctx->io_queue;
229 q = xmalloc_zero(sizeof(struct asio_queue));
230 q->buffer_size = buffer_size;
231 q->max_writebacks = write_back;
236 DBG("FB-DIRECT: Got I/O queue, uc=%d", q->use_count);
241 fbdir_put_io_queue(void)
243 struct ucwlib_context *ctx = ucwlib_thread_context();
244 struct asio_queue *q = ctx->io_queue;
246 DBG("FB-DIRECT: Put I/O queue, uc=%d", q->use_count);
249 asio_cleanup_queue(q);
251 ctx->io_queue = NULL;
256 fbdir_close(struct fastbuf *f)
258 struct fb_direct *F = FB_DIRECT(f);
260 DBG("FB-DIRECT: Close");
262 fbdir_change_mode(F, M_NULL);
264 fbdir_put_io_queue();
266 bclose_file_helper(f, F->fd, F->is_temp_file);
271 fbdir_config(struct fastbuf *f, uns item, int value)
277 case BCONFIG_IS_TEMP_FILE:
278 orig = FB_DIRECT(f)->is_temp_file;
279 FB_DIRECT(f)->is_temp_file = value;
287 fbdir_open_fd_internal(int fd, const char *name, struct asio_queue *q, uns buffer_size, uns read_ahead UNUSED, uns write_back)
289 int namelen = strlen(name) + 1;
290 struct fb_direct *F = xmalloc(sizeof(struct fb_direct) + namelen);
291 struct fastbuf *f = &F->fb;
293 DBG("FB-DIRECT: Open");
294 bzero(F, sizeof(*F));
296 memcpy(f->name, name, namelen);
299 F->io_queue = F->user_queue = q;
301 F->io_queue = fbdir_get_io_queue(buffer_size, write_back);
302 f->refill = fbdir_refill;
303 f->spout = fbdir_spout;
304 f->seek = fbdir_seek;
305 f->close = fbdir_close;
306 f->config = fbdir_config;
307 f->can_overwrite_buffer = 2;
313 #include "lib/getopt.h"
315 int main(int argc, char **argv)
317 struct fb_params par = { .type = FB_DIRECT };
318 struct fastbuf *f, *t;
321 if (cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) >= 0)
322 die("Hey, whaddya want?");
323 f = (optind < argc) ? bopen_file(argv[optind++], O_RDONLY, &par) : bopen_fd(0, &par);
324 t = (optind < argc) ? bopen_file(argv[optind++], O_RDWR | O_CREAT | O_TRUNC, &par) : bopen_fd(1, &par);
327 ASSERT(btell(f) == btell(t));
329 #if 0 // This triggers unaligned write
336 ASSERT(btell(t) == 1);