13 #include <xseg/xseg.h>
15 static int usage(void)
17 printf("Usage: ./blockd <path_to_disk_image> [options]\n"
18 "Options: [-p portno]\n"
19 " [-s image size in bytes]\n"
20 " [-g type:name:nr_ports:nr_requests:request_size:extra_size:page_shift]\n"
21 " [-n nr_parallel_ops]\n");
27 struct xseg_request *req;
33 struct xseg_port *xport;
40 struct xq pending_ops;
43 struct sigevent sigevent;
46 static unsigned long sigaction_count;
48 static void sigaction_handler(int sig, siginfo_t *siginfo, void *arg)
53 static struct io *alloc_io(struct store *store)
55 xqindex idx = xq_pop_head(&store->free_ops);
58 return store->ios + idx;
61 static inline void free_io(struct store *store, struct io *io)
63 xqindex idx = io - store->ios;
65 xq_append_head(&store->free_ops, idx);
68 static inline void pending_io(struct store *store, struct io *io)
70 xqindex idx = io - store->ios;
71 xq_append_tail(&store->pending_ops, idx);
74 static inline struct io *get_pending_io(struct store *store)
76 xqindex idx = xq_pop_head(&store->pending_ops);
79 return store->ios + idx;
82 static void log_io(char *msg, struct io *io)
84 char name[64], data[64];
85 /* null terminate name in case of req->name is less than 63 characters,
86 * and next character after name (aka first byte of next buffer) is not
89 unsigned int end = (io->req->namesize > 63) ? 63 : io->req->namesize;
90 strncpy(name, io->req->name, end);
92 strncpy(data, io->req->data, 63);
94 printf("%s: fd:%u, op:%u %llu:%lu retval: %lu, reqstate: %u\n"
95 "name[%u]:'%s', data[%llu]:\n%s------------------\n\n",
97 (unsigned int)io->cb.aio_fildes,
98 (unsigned int)io->req->op,
99 (unsigned long long)io->cb.aio_offset,
100 (unsigned long)io->cb.aio_nbytes,
101 (unsigned long)io->retval,
102 (unsigned int)io->req->state,
103 (unsigned int)io->req->namesize, name,
104 (unsigned long long)io->req->datasize, data);
107 static void complete(struct store *store, struct io *io)
109 struct xseg_request *req = io->req;
110 req->state |= XS_SERVED;
111 log_io("complete", io);
112 xseg_respond(store->xseg, req->portno, req);
113 xseg_signal(store->xseg, req->portno);
117 static void fail(struct store *store, struct io *io)
119 struct xseg_request *req = io->req;
120 req->state |= XS_ERROR;
122 xseg_respond(store->xseg, req->portno, req);
123 xseg_signal(store->xseg, req->portno);
127 static void pending(struct store *store, struct io *io)
129 io->req->state = XS_PENDING;
130 pending_io(store, io);
133 static void handle_unknown(struct store *store, struct io *io)
135 struct xseg_request *req = io->req;
136 snprintf(req->data, req->datasize, "unknown request op");
140 static inline void prepare_io(struct store *store, struct io *io)
142 io->cb.aio_fildes = store->fd;
143 io->cb.aio_sigevent = store->sigevent;
144 /* cb->aio_sigevent.sigev_value.sival_int = fd; */
147 static void handle_read_write(struct store *store, struct io *io)
150 struct xseg_request *req = io->req;
151 struct aiocb *cb = &io->cb;
153 if (req->state != XS_ACCEPTED) {
155 req->serviced += io->retval;
157 req->datasize = req->serviced;
159 if (req->serviced >= req->datasize) {
166 printf("0.%p vs %p!\n", (void *)req, (void *)io->req);
168 if (req->flags & (XF_FLUSH | XF_FUA)) {
169 /* for now, no FLUSH/FUA support.
170 * note that with FLUSH/size == 0
171 * there will probably be a (uint64_t)-1 offset */
180 prepare_io(store, io);
181 cb->aio_buf = req->data + req->serviced;
182 cb->aio_nbytes = req->datasize - req->serviced;
183 cb->aio_offset = req->offset + req->serviced;
193 snprintf(req->data, req->datasize,
194 "wtf, corrupt op %u?\n", req->op);
200 strerror_r(errno, req->data, req->datasize);
208 static void handle_info(struct store *store, struct io *io)
210 struct xseg_request *req = io->req;
212 *((off_t *) req->data) = store->size;
213 req->datasize = sizeof(store->size);
218 static void dispatch(struct store *store, struct io *io)
220 switch (io->req->op) {
223 handle_read_write(store, io); break;
225 handle_info(store, io); break;
228 handle_unknown(store, io);
232 static void handle_pending(struct store *store, struct io *io)
234 int r = aio_error(&io->cb);
235 if (r == EINPROGRESS) {
240 io->retval = aio_return(&io->cb);
249 static void handle_accepted(struct store *store, struct io *io)
251 struct xseg_request *req = io->req;
253 req->state = XS_ACCEPTED;
258 static int blockd_loop(struct store *store)
260 struct xseg *xseg = store->xseg;
261 uint32_t portno = store->portno;
263 struct xseg_request *accepted;
267 xseg_prepare_wait(xseg, portno);
268 io = alloc_io(store);
270 accepted = xseg_accept(xseg, portno);
272 xseg_cancel_wait(xseg, portno);
274 handle_accepted(store, io);
279 io = get_pending_io(store);
281 xseg_cancel_wait(xseg, portno);
282 handle_pending(store, io);
285 if (!io && !accepted)
286 xseg_wait_signal(xseg, portno, 10000);
292 static struct xseg *join(char *spec)
294 struct xseg_config config;
297 (void)xseg_parse_spec(spec, &config);
298 xseg = xseg_join(config.type, config.name);
302 (void)xseg_create(&config);
303 return xseg_join(config.type, config.name);
306 static int blockd(char *path, off_t size, uint32_t nr_ops,
307 char *spec, long portno)
314 store = malloc(sizeof(struct store));
320 store->fd = open(path, O_RDWR);
321 while (store->fd < 0) {
322 if (errno == ENOENT && size)
323 store->fd = open(path, O_RDWR | O_CREAT, 0600);
331 r = fstat(store->fd, &stat);
338 fprintf(stderr, "size cannot be zero\n");
343 lseek(store->fd, size-1, SEEK_SET);
344 if (write(store->fd, &r, 1) != 1) {
357 store->sigevent.sigev_notify = SIGEV_SIGNAL;
358 store->sigevent.sigev_signo = SIGIO;
359 sa.sa_sigaction = sigaction_handler;
360 sa.sa_flags = SA_SIGINFO;
361 if (sigemptyset(&sa.sa_mask))
362 perror("sigemptyset");
364 if (sigaction(SIGIO, &sa, NULL)) {
369 store->nr_ops = nr_ops;
370 store->free_bufs = calloc(nr_ops, sizeof(xqindex));
371 if (!store->free_bufs)
374 store->pending_bufs = calloc(nr_ops, sizeof(xqindex));
375 if (!store->pending_bufs)
378 store->ios = calloc(nr_ops, sizeof(struct io));
385 xq_init_seq(&store->free_ops, nr_ops, nr_ops, store->free_bufs);
386 xq_init_empty(&store->pending_ops, nr_ops, store->pending_bufs);
388 if (xseg_initialize("posix")) {
389 printf("cannot initialize library\n");
392 store->xseg = join(spec);
396 store->xport = xseg_bind_port(store->xseg, portno);
398 printf("cannot bind to port %ld\n", portno);
402 store->portno = xseg_portno(store->xseg, store->xport);
403 printf("blockd on port %u/%u\n",
404 store->portno, store->xseg->config.nr_ports);
406 return blockd_loop(store);
409 int main(int argc, char **argv)
411 char *path, *spec = "";
425 for (i = 2; i < argc; i++) {
426 if (!strcmp(argv[i], "-g") && i + 1 < argc) {
432 if (!strcmp(argv[i], "-s") && i + 1 < argc) {
433 size = strtoull(argv[i+1], NULL, 10);
438 if (!strcmp(argv[i], "-p") && i + 1 < argc) {
439 portno = strtoul(argv[i+1], NULL, 10);
444 if (!strcmp(argv[i], "-p") && i + 1 < argc) {
445 nr_ops = strtoul(argv[i+1], NULL, 10);
454 return blockd(path, size, nr_ops, spec, portno);