13 #include <valgrind/memcheck.h>
15 #include <linux/bio.h>
16 #include <linux/blkdev.h>
17 #include <linux/completion.h>
19 #include <linux/kthread.h>
21 #include "tools-util.h"
23 static io_context_t aio_ctx;
24 static atomic_t running_requests;
26 void generic_make_request(struct bio *bio)
29 struct bvec_iter iter;
34 if (bio->bi_opf & REQ_PREFLUSH) {
35 ret = fdatasync(bio->bi_bdev->bd_fd);
37 fprintf(stderr, "fsync error: %m\n");
38 bio->bi_status = BLK_STS_IOERR;
45 bio_for_each_segment(bv, bio, iter)
48 iov = alloca(sizeof(*iov) * i);
51 bio_for_each_segment(bv, bio, iter) {
52 void *start = page_address(bv.bv_page) + bv.bv_offset;
53 size_t len = bv.bv_len;
55 iov[i++] = (struct iovec) {
60 /* To be pedantic it should only be on IO completion. */
61 if (bio_op(bio) == REQ_OP_READ)
62 VALGRIND_MAKE_MEM_DEFINED(start, len);
67 .aio_fildes = bio->bi_opf & REQ_FUA
68 ? bio->bi_bdev->bd_sync_fd
69 : bio->bi_bdev->bd_fd,
72 switch (bio_op(bio)) {
74 iocb.aio_lio_opcode = IO_CMD_PREADV;
77 iocb.u.v.offset = bio->bi_iter.bi_sector << 9;
79 atomic_inc(&running_requests);
80 ret = io_submit(aio_ctx, 1, &iocbp);
82 die("io_submit err: %s", strerror(-ret));
85 iocb.aio_lio_opcode = IO_CMD_PWRITEV;
88 iocb.u.v.offset = bio->bi_iter.bi_sector << 9;
90 atomic_inc(&running_requests);
91 ret = io_submit(aio_ctx, 1, &iocbp);
93 die("io_submit err: %s", strerror(-ret));
96 ret = fsync(bio->bi_bdev->bd_fd);
98 die("fsync error: %m");
106 static void submit_bio_wait_endio(struct bio *bio)
108 complete(bio->bi_private);
111 int submit_bio_wait(struct bio *bio)
113 struct completion done;
115 init_completion(&done);
116 bio->bi_private = &done;
117 bio->bi_end_io = submit_bio_wait_endio;
118 bio->bi_opf |= REQ_SYNC;
120 wait_for_completion(&done);
122 return blk_status_to_errno(bio->bi_status);
125 int blkdev_issue_discard(struct block_device *bdev,
126 sector_t sector, sector_t nr_sects,
127 gfp_t gfp_mask, unsigned long flags)
132 unsigned bdev_logical_block_size(struct block_device *bdev)
138 ret = fstat(bdev->bd_fd, &statbuf);
141 if (!S_ISBLK(statbuf.st_mode))
142 return statbuf.st_blksize >> 9;
144 ret = ioctl(bdev->bd_fd, BLKPBSZGET, &blksize);
150 sector_t get_capacity(struct gendisk *disk)
152 struct block_device *bdev =
153 container_of(disk, struct block_device, __bd_disk);
158 ret = fstat(bdev->bd_fd, &statbuf);
161 if (!S_ISBLK(statbuf.st_mode))
162 return statbuf.st_size >> 9;
164 ret = ioctl(bdev->bd_fd, BLKGETSIZE64, &bytes);
170 void blkdev_put(struct block_device *bdev, fmode_t mode)
172 fdatasync(bdev->bd_fd);
173 close(bdev->bd_sync_fd);
178 struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
181 struct block_device *bdev;
182 int fd, sync_fd, flags = O_DIRECT;
184 if ((mode & (FMODE_READ|FMODE_WRITE)) == (FMODE_READ|FMODE_WRITE))
186 else if (mode & FMODE_READ)
188 else if (mode & FMODE_WRITE)
192 /* using O_EXCL doesn't work with opening twice for an O_SYNC fd: */
193 if (mode & FMODE_EXCL)
197 fd = open(path, flags);
199 return ERR_PTR(-errno);
201 sync_fd = open(path, flags|O_SYNC);
205 return ERR_PTR(-errno);
208 bdev = malloc(sizeof(*bdev));
209 memset(bdev, 0, sizeof(*bdev));
211 strncpy(bdev->name, path, sizeof(bdev->name));
212 bdev->name[sizeof(bdev->name) - 1] = '\0';
215 bdev->bd_sync_fd = sync_fd;
216 bdev->bd_holder = holder;
217 bdev->bd_disk = &bdev->__bd_disk;
218 bdev->bd_bdi = &bdev->__bd_bdi;
219 bdev->queue.backing_dev_info = bdev->bd_bdi;
224 void bdput(struct block_device *bdev)
229 struct block_device *lookup_bdev(const char *path)
231 return ERR_PTR(-EINVAL);
234 static int aio_completion_thread(void *arg)
236 struct io_event events[8], *ev;
241 ret = io_getevents(aio_ctx, 1, ARRAY_SIZE(events),
244 if (ret < 0 && ret == -EINTR)
247 die("io_getevents() error: %s", strerror(-ret));
249 for (ev = events; ev < events + ret; ev++) {
250 struct bio *bio = (struct bio *) ev->data;
252 /* This should only happen during blkdev_cleanup() */
254 BUG_ON(atomic_read(&running_requests) != 0);
259 if (ev->res != bio->bi_iter.bi_size)
260 bio->bi_status = BLK_STS_IOERR;
263 atomic_dec(&running_requests);
270 static struct task_struct *aio_task = NULL;
272 __attribute__((constructor(102)))
273 static void blkdev_init(void)
275 struct task_struct *p;
277 if (io_setup(256, &aio_ctx))
278 die("io_setup() error: %m");
280 p = kthread_run(aio_completion_thread, NULL, "aio_completion");
286 __attribute__((destructor(102)))
287 static void blkdev_cleanup(void)
289 struct task_struct *p = NULL;
293 /* I mean, really?! IO_CMD_NOOP is even defined, but not implemented. */
297 die("pipe err: %s", strerror(ret));
299 /* Wake up the completion thread with spurious work. */
302 .aio_lio_opcode = IO_CMD_PWRITE,
303 .data = NULL, /* Signal to stop */
304 .aio_fildes = fds[1],
308 ret = io_submit(aio_ctx, 1, &iocbp);
310 die("io_submit cleanup err: %s", strerror(-ret));
312 ret = kthread_stop(p);