]> git.sesse.net Git - bcachefs-tools-debian/blob - linux/blkdev.c
Fix 32 bit io regression
[bcachefs-tools-debian] / linux / blkdev.c
1
2 #include <alloca.h>
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <sys/ioctl.h>
6 #include <sys/stat.h>
7 #include <sys/types.h>
8 #include <sys/uio.h>
9 #include <unistd.h>
10
11 #include <libaio.h>
12
13 #ifdef CONFIG_VALGRIND
14 #include <valgrind/memcheck.h>
15 #endif
16
17 #include <linux/bio.h>
18 #include <linux/blkdev.h>
19 #include <linux/completion.h>
20 #include <linux/fs.h>
21 #include <linux/kthread.h>
22
23 #include "tools-util.h"
24
25 struct fops {
26         void (*init)(void);
27         void (*cleanup)(void);
28         void (*read)(struct bio *bio, struct iovec * iov, unsigned i);
29         void (*write)(struct bio *bio, struct iovec * iov, unsigned i);
30 };
31
32 static struct fops *fops;
33 static io_context_t aio_ctx;
34 static atomic_t running_requests;
35
36 void generic_make_request(struct bio *bio)
37 {
38         struct iovec *iov;
39         struct bvec_iter iter;
40         struct bio_vec bv;
41         ssize_t ret;
42         unsigned i;
43
44         if (bio->bi_opf & REQ_PREFLUSH) {
45                 ret = fdatasync(bio->bi_bdev->bd_fd);
46                 if (ret) {
47                         fprintf(stderr, "fsync error: %m\n");
48                         bio->bi_status = BLK_STS_IOERR;
49                         bio_endio(bio);
50                         return;
51                 }
52         }
53
54         i = 0;
55         bio_for_each_segment(bv, bio, iter)
56                 i++;
57
58         iov = alloca(sizeof(*iov) * i);
59
60         i = 0;
61         bio_for_each_segment(bv, bio, iter) {
62                 void *start = page_address(bv.bv_page) + bv.bv_offset;
63                 size_t len = bv.bv_len;
64
65                 iov[i++] = (struct iovec) {
66                         .iov_base = start,
67                         .iov_len = len,
68                 };
69
70 #ifdef CONFIG_VALGRIND
71                 /* To be pedantic it should only be on IO completion. */
72                 if (bio_op(bio) == REQ_OP_READ)
73                         VALGRIND_MAKE_MEM_DEFINED(start, len);
74 #endif
75         }
76
77         switch (bio_op(bio)) {
78         case REQ_OP_READ:
79                 fops->read(bio, iov, i);
80                 break;
81         case REQ_OP_WRITE:
82                 fops->write(bio, iov, i);
83                 break;
84         case REQ_OP_FLUSH:
85                 ret = fsync(bio->bi_bdev->bd_fd);
86                 if (ret)
87                         die("fsync error: %m");
88                 bio_endio(bio);
89                 break;
90         default:
91                 BUG();
92         }
93 }
94
95 static void submit_bio_wait_endio(struct bio *bio)
96 {
97         complete(bio->bi_private);
98 }
99
100 int submit_bio_wait(struct bio *bio)
101 {
102         struct completion done;
103
104         init_completion(&done);
105         bio->bi_private = &done;
106         bio->bi_end_io = submit_bio_wait_endio;
107         bio->bi_opf |= REQ_SYNC;
108         submit_bio(bio);
109         wait_for_completion(&done);
110
111         return blk_status_to_errno(bio->bi_status);
112 }
113
114 int blkdev_issue_discard(struct block_device *bdev,
115                          sector_t sector, sector_t nr_sects,
116                          gfp_t gfp_mask, unsigned long flags)
117 {
118         return 0;
119 }
120
121 unsigned bdev_logical_block_size(struct block_device *bdev)
122 {
123         struct stat statbuf;
124         unsigned blksize;
125         int ret;
126
127         ret = fstat(bdev->bd_fd, &statbuf);
128         BUG_ON(ret);
129
130         if (!S_ISBLK(statbuf.st_mode))
131                 return statbuf.st_blksize >> 9;
132
133         ret = ioctl(bdev->bd_fd, BLKPBSZGET, &blksize);
134         BUG_ON(ret);
135
136         return blksize >> 9;
137 }
138
139 sector_t get_capacity(struct gendisk *disk)
140 {
141         struct block_device *bdev =
142                 container_of(disk, struct block_device, __bd_disk);
143         struct stat statbuf;
144         u64 bytes;
145         int ret;
146
147         ret = fstat(bdev->bd_fd, &statbuf);
148         BUG_ON(ret);
149
150         if (!S_ISBLK(statbuf.st_mode))
151                 return statbuf.st_size >> 9;
152
153         ret = ioctl(bdev->bd_fd, BLKGETSIZE64, &bytes);
154         BUG_ON(ret);
155
156         return bytes >> 9;
157 }
158
159 void blkdev_put(struct block_device *bdev, fmode_t mode)
160 {
161         fdatasync(bdev->bd_fd);
162         close(bdev->bd_sync_fd);
163         close(bdev->bd_fd);
164         free(bdev);
165 }
166
167 struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
168                                         void *holder)
169 {
170         struct block_device *bdev;
171         int fd, sync_fd, flags = O_DIRECT;
172
173         if ((mode & (FMODE_READ|FMODE_WRITE)) == (FMODE_READ|FMODE_WRITE))
174                 flags = O_RDWR;
175         else if (mode & FMODE_READ)
176                 flags = O_RDONLY;
177         else if (mode & FMODE_WRITE)
178                 flags = O_WRONLY;
179
180 #if 0
181         /* using O_EXCL doesn't work with opening twice for an O_SYNC fd: */
182         if (mode & FMODE_EXCL)
183                 flags |= O_EXCL;
184 #endif
185
186         fd = open(path, flags);
187         if (fd < 0)
188                 return ERR_PTR(-errno);
189
190         sync_fd = open(path, flags|O_SYNC);
191         if (sync_fd < 0) {
192                 assert(0);
193                 close(fd);
194                 return ERR_PTR(-errno);
195         }
196
197         bdev = malloc(sizeof(*bdev));
198         memset(bdev, 0, sizeof(*bdev));
199
200         strncpy(bdev->name, path, sizeof(bdev->name));
201         bdev->name[sizeof(bdev->name) - 1] = '\0';
202
203         bdev->bd_dev            = xfstat(fd).st_rdev;
204         bdev->bd_fd             = fd;
205         bdev->bd_sync_fd        = sync_fd;
206         bdev->bd_holder         = holder;
207         bdev->bd_disk           = &bdev->__bd_disk;
208         bdev->bd_bdi            = &bdev->__bd_bdi;
209         bdev->queue.backing_dev_info = bdev->bd_bdi;
210
211         return bdev;
212 }
213
214 void bdput(struct block_device *bdev)
215 {
216         BUG();
217 }
218
219 int lookup_bdev(const char *path, dev_t *dev)
220 {
221         return -EINVAL;
222 }
223
224 static void io_fallback(void)
225 {
226         fops++;
227         if (fops->init == NULL)
228                 die("no fallback possible, something is very wrong");
229         fops->init();
230 }
231
232 static void sync_check(struct bio *bio, int ret)
233 {
234         if (ret != bio->bi_iter.bi_size) {
235                 die("IO error: %s\n", strerror(-ret));
236         }
237
238         if (bio->bi_opf & REQ_FUA) {
239                 ret = fdatasync(bio->bi_bdev->bd_fd);
240                 if (ret)
241                         die("fsync error: %s\n", strerror(-ret));
242         }
243         bio_endio(bio);
244 }
245
246 static void sync_init(void) {}
247
248 static void sync_cleanup(void)
249 {
250         /* not necessary? */
251         sync();
252 }
253
254 static void sync_read(struct bio *bio, struct iovec * iov, unsigned i)
255 {
256
257         int fd = bio->bi_opf & REQ_FUA
258                         ? bio->bi_bdev->bd_sync_fd
259                         : bio->bi_bdev->bd_fd;
260         ssize_t ret = preadv(fd, iov, i, bio->bi_iter.bi_sector << 9);
261         sync_check(bio, ret);
262 }
263
264 static void sync_write(struct bio *bio, struct iovec * iov, unsigned i)
265 {
266         int fd = bio->bi_opf & REQ_FUA
267                         ? bio->bi_bdev->bd_sync_fd
268                         : bio->bi_bdev->bd_fd;
269         ssize_t ret = pwritev(fd, iov, i, bio->bi_iter.bi_sector << 9);
270         sync_check(bio, ret);
271 }
272
273 static int aio_completion_thread(void *arg)
274 {
275         struct io_event events[8], *ev;
276         int ret;
277         bool stop = false;
278
279         while (!stop) {
280                 ret = io_getevents(aio_ctx, 1, ARRAY_SIZE(events),
281                                    events, NULL);
282
283                 if (ret < 0 && ret == -EINTR)
284                         continue;
285                 if (ret < 0)
286                         die("io_getevents() error: %s", strerror(-ret));
287
288                 for (ev = events; ev < events + ret; ev++) {
289                         struct bio *bio = (struct bio *) ev->data;
290
291                         /* This should only happen during blkdev_cleanup() */
292                         if (!bio) {
293                                 BUG_ON(atomic_read(&running_requests) != 0);
294                                 stop = true;
295                                 continue;
296                         }
297
298                         if (ev->res != bio->bi_iter.bi_size)
299                                 bio->bi_status = BLK_STS_IOERR;
300
301                         bio_endio(bio);
302                         atomic_dec(&running_requests);
303                 }
304         }
305
306         return 0;
307 }
308
309 static struct task_struct *aio_task = NULL;
310
311 static void aio_init(void)
312 {
313         struct task_struct *p;
314         long err = io_setup(256, &aio_ctx);
315         if (!err) {
316                 p = kthread_run(aio_completion_thread, NULL, "aio_completion");
317                 BUG_ON(IS_ERR(p));
318
319                 aio_task = p;
320
321         } else if (err == -ENOSYS) {
322                 io_fallback();
323         } else {
324                 die("io_setup() error: %s", strerror(err));
325         }
326 }
327
328 static void aio_cleanup(void)
329 {
330         struct task_struct *p = NULL;
331         swap(aio_task, p);
332         get_task_struct(p);
333
334         /* I mean, really?! IO_CMD_NOOP is even defined, but not implemented. */
335         int fds[2];
336         int ret = pipe(fds);
337         if (ret != 0)
338                 die("pipe err: %s", strerror(ret));
339
340         /* Wake up the completion thread with spurious work. */
341         int junk = 0;
342         struct iocb iocb = {
343                 .aio_lio_opcode = IO_CMD_PWRITE,
344                 .data = NULL, /* Signal to stop */
345                 .aio_fildes = fds[1],
346                 .u.c.buf = &junk,
347                 .u.c.nbytes = 1,
348         }, *iocbp = &iocb;
349         ret = io_submit(aio_ctx, 1, &iocbp);
350         if (ret != 1)
351                 die("io_submit cleanup err: %s", strerror(-ret));
352
353         ret = kthread_stop(p);
354         BUG_ON(ret);
355
356         put_task_struct(p);
357
358         close(fds[0]);
359         close(fds[1]);
360 }
361
362 static void aio_op(struct bio *bio, struct iovec *iov, unsigned i, int opcode)
363 {
364         ssize_t ret;
365         struct iocb iocb = {
366                 .data           = bio,
367                 .aio_fildes     = bio->bi_opf & REQ_FUA
368                         ? bio->bi_bdev->bd_sync_fd
369                         : bio->bi_bdev->bd_fd,
370                 .aio_lio_opcode = opcode,
371                 .u.c.buf        = iov,
372                 .u.c.nbytes     = i,
373                 .u.c.offset     = bio->bi_iter.bi_sector << 9,
374
375         }, *iocbp = &iocb;
376
377         atomic_inc(&running_requests);
378         ret = io_submit(aio_ctx, 1, &iocbp);
379         if (ret != 1)
380                 die("io_submit err: %s", strerror(-ret));
381 }
382
383 static void aio_read(struct bio *bio, struct iovec *iov, unsigned i)
384 {
385         aio_op(bio, iov, i, IO_CMD_PREADV);
386 }
387
388 static void aio_write(struct bio *bio, struct iovec * iov, unsigned i)
389 {
390         aio_op(bio, iov, i, IO_CMD_PWRITEV);
391 }
392
393
394 /* not implemented */
395 static void uring_init(void) {
396         io_fallback();
397 }
398
399 struct fops fops_list[] = {
400         {
401                 .init           = uring_init,
402         }, {
403                 .init           = aio_init,
404                 .cleanup        = aio_cleanup,
405                 .read           = aio_read,
406                 .write          = aio_write,
407         }, {
408                 .init           = sync_init,
409                 .cleanup        = sync_cleanup,
410                 .read           = sync_read,
411                 .write          = sync_write,
412         }, {
413                 /* NULL */
414         }
415 };
416
417 __attribute__((constructor(102)))
418 static void blkdev_init(void)
419 {
420         fops = fops_list;
421         fops->init();
422 }
423
424 __attribute__((destructor(102)))
425 static void blkdev_cleanup(void)
426 {
427         fops->cleanup();
428 }