]> git.sesse.net Git - bcachefs-tools-debian/blob - linux/blkdev.c
Update bcachefs sources to f9c612bbf82d bcachefs: Fixes for building in userspace
[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)
117 {
118         return 0;
119 }
120
121 int blkdev_issue_zeroout(struct block_device *bdev,
122                          sector_t sector, sector_t nr_sects,
123                          gfp_t gfp_mask, unsigned flags)
124 {
125         /* Not yet implemented: */
126         BUG();
127 }
128
129 unsigned bdev_logical_block_size(struct block_device *bdev)
130 {
131         struct stat statbuf;
132         unsigned blksize;
133         int ret;
134
135         ret = fstat(bdev->bd_fd, &statbuf);
136         BUG_ON(ret);
137
138         if (!S_ISBLK(statbuf.st_mode))
139                 return statbuf.st_blksize;
140
141         xioctl(bdev->bd_fd, BLKPBSZGET, &blksize);
142         return blksize;
143 }
144
145 sector_t get_capacity(struct gendisk *disk)
146 {
147         struct block_device *bdev =
148                 container_of(disk, struct block_device, __bd_disk);
149         struct stat statbuf;
150         u64 bytes;
151         int ret;
152
153         ret = fstat(bdev->bd_fd, &statbuf);
154         BUG_ON(ret);
155
156         if (!S_ISBLK(statbuf.st_mode))
157                 return statbuf.st_size >> 9;
158
159         ret = ioctl(bdev->bd_fd, BLKGETSIZE64, &bytes);
160         BUG_ON(ret);
161
162         return bytes >> 9;
163 }
164
165 void blkdev_put(struct block_device *bdev, void *holder)
166 {
167         fdatasync(bdev->bd_fd);
168         close(bdev->bd_sync_fd);
169         close(bdev->bd_fd);
170         free(bdev);
171 }
172
173 struct block_device *blkdev_get_by_path(const char *path, blk_mode_t mode,
174                                         void *holder, const struct blk_holder_ops *hop)
175 {
176         struct block_device *bdev;
177         int fd, sync_fd, buffered_fd, flags = 0;
178
179         if ((mode & (BLK_OPEN_READ|BLK_OPEN_WRITE)) == (BLK_OPEN_READ|BLK_OPEN_WRITE))
180                 flags = O_RDWR;
181         else if (mode & BLK_OPEN_READ)
182                 flags = O_RDONLY;
183         else if (mode & BLK_OPEN_WRITE)
184                 flags = O_WRONLY;
185
186         if (!(mode & BLK_OPEN_BUFFERED))
187                 flags |= O_DIRECT;
188
189 #if 0
190         /* using O_EXCL doesn't work with opening twice for an O_SYNC fd: */
191         if (mode & BLK_OPEN_EXCL)
192                 flags |= O_EXCL;
193 #endif
194         buffered_fd = open(path, flags & ~O_DIRECT);
195         if (buffered_fd < 0)
196                 return ERR_PTR(-errno);
197
198         fd = open(path, flags);
199         if (fd < 0)
200                 fd = dup(buffered_fd);
201         if (fd < 0) {
202                 close(buffered_fd);
203                 return ERR_PTR(-errno);
204         }
205
206         sync_fd = open(path, flags|O_SYNC);
207         if (sync_fd < 0)
208                 sync_fd = open(path, (flags & ~O_DIRECT)|O_SYNC);
209         if (sync_fd < 0) {
210                 close(fd);
211                 close(buffered_fd);
212                 return ERR_PTR(-errno);
213         }
214
215         bdev = malloc(sizeof(*bdev));
216         memset(bdev, 0, sizeof(*bdev));
217
218         strncpy(bdev->name, path, sizeof(bdev->name));
219         bdev->name[sizeof(bdev->name) - 1] = '\0';
220
221         bdev->bd_dev            = xfstat(fd).st_rdev;
222         bdev->bd_fd             = fd;
223         bdev->bd_sync_fd        = sync_fd;
224         bdev->bd_buffered_fd    = buffered_fd;
225         bdev->bd_holder         = holder;
226         bdev->bd_disk           = &bdev->__bd_disk;
227         bdev->bd_disk->bdi      = &bdev->bd_disk->__bdi;
228         bdev->queue.backing_dev_info = bdev->bd_disk->bdi;
229
230         return bdev;
231 }
232
233 void bdput(struct block_device *bdev)
234 {
235         BUG();
236 }
237
238 int lookup_bdev(const char *path, dev_t *dev)
239 {
240         return -EINVAL;
241 }
242
243 static void io_fallback(void)
244 {
245         fops++;
246         if (fops->init == NULL)
247                 die("no fallback possible, something is very wrong");
248         fops->init();
249 }
250
251 static void sync_check(struct bio *bio, int ret)
252 {
253         if (ret != bio->bi_iter.bi_size) {
254                 die("IO error: %s\n", strerror(-ret));
255         }
256
257         if (bio->bi_opf & REQ_FUA) {
258                 ret = fdatasync(bio->bi_bdev->bd_fd);
259                 if (ret)
260                         die("fsync error: %s\n", strerror(-ret));
261         }
262         bio_endio(bio);
263 }
264
265 static void sync_init(void) {}
266
267 static void sync_cleanup(void)
268 {
269         /* not necessary? */
270         sync();
271 }
272
273 static void sync_read(struct bio *bio, struct iovec * iov, unsigned i)
274 {
275
276         int fd = bio->bi_opf & REQ_FUA
277                         ? bio->bi_bdev->bd_sync_fd
278                         : bio->bi_bdev->bd_fd;
279         ssize_t ret = preadv(fd, iov, i, bio->bi_iter.bi_sector << 9);
280         sync_check(bio, ret);
281 }
282
283 static void sync_write(struct bio *bio, struct iovec * iov, unsigned i)
284 {
285         int fd = bio->bi_opf & REQ_FUA
286                         ? bio->bi_bdev->bd_sync_fd
287                         : bio->bi_bdev->bd_fd;
288         ssize_t ret = pwritev(fd, iov, i, bio->bi_iter.bi_sector << 9);
289         sync_check(bio, ret);
290 }
291
292 static int aio_completion_thread(void *arg)
293 {
294         struct io_event events[8], *ev;
295         int ret;
296         bool stop = false;
297
298         while (!stop) {
299                 ret = io_getevents(aio_ctx, 1, ARRAY_SIZE(events),
300                                    events, NULL);
301
302                 if (ret < 0 && ret == -EINTR)
303                         continue;
304                 if (ret < 0)
305                         die("io_getevents() error: %s", strerror(-ret));
306
307                 for (ev = events; ev < events + ret; ev++) {
308                         struct bio *bio = (struct bio *) ev->data;
309
310                         /* This should only happen during blkdev_cleanup() */
311                         if (!bio) {
312                                 BUG_ON(atomic_read(&running_requests) != 0);
313                                 stop = true;
314                                 continue;
315                         }
316
317                         if (ev->res != bio->bi_iter.bi_size)
318                                 bio->bi_status = BLK_STS_IOERR;
319
320                         bio_endio(bio);
321                         atomic_dec(&running_requests);
322                 }
323         }
324
325         return 0;
326 }
327
328 static struct task_struct *aio_task = NULL;
329
330 static void aio_init(void)
331 {
332         struct task_struct *p;
333         long err = io_setup(256, &aio_ctx);
334         if (!err) {
335                 p = kthread_run(aio_completion_thread, NULL, "aio_completion");
336                 BUG_ON(IS_ERR(p));
337
338                 aio_task = p;
339
340         } else if (err == -ENOSYS) {
341                 io_fallback();
342         } else {
343                 die("io_setup() error: %s", strerror(err));
344         }
345 }
346
347 static void aio_cleanup(void)
348 {
349         struct task_struct *p = NULL;
350         swap(aio_task, p);
351         get_task_struct(p);
352
353         /* I mean, really?! IO_CMD_NOOP is even defined, but not implemented. */
354         int fds[2];
355         int ret = pipe(fds);
356         if (ret != 0)
357                 die("pipe err: %s", strerror(ret));
358
359         /* Wake up the completion thread with spurious work. */
360         int junk = 0;
361         struct iocb iocb = {
362                 .aio_lio_opcode = IO_CMD_PWRITE,
363                 .data = NULL, /* Signal to stop */
364                 .aio_fildes = fds[1],
365                 .u.c.buf = &junk,
366                 .u.c.nbytes = 1,
367         }, *iocbp = &iocb;
368         ret = io_submit(aio_ctx, 1, &iocbp);
369         if (ret != 1)
370                 die("io_submit cleanup err: %s", strerror(-ret));
371
372         ret = kthread_stop(p);
373         BUG_ON(ret);
374
375         put_task_struct(p);
376
377         close(fds[0]);
378         close(fds[1]);
379 }
380
381 static void aio_op(struct bio *bio, struct iovec *iov, unsigned i, int opcode)
382 {
383         ssize_t ret;
384         struct iocb iocb = {
385                 .data           = bio,
386                 .aio_fildes     = bio->bi_opf & REQ_FUA
387                         ? bio->bi_bdev->bd_sync_fd
388                         : bio->bi_bdev->bd_fd,
389                 .aio_lio_opcode = opcode,
390                 .u.c.buf        = iov,
391                 .u.c.nbytes     = i,
392                 .u.c.offset     = bio->bi_iter.bi_sector << 9,
393
394         }, *iocbp = &iocb;
395
396         atomic_inc(&running_requests);
397         ret = io_submit(aio_ctx, 1, &iocbp);
398         if (ret != 1)
399                 die("io_submit err: %s", strerror(-ret));
400 }
401
402 static void aio_read(struct bio *bio, struct iovec *iov, unsigned i)
403 {
404         aio_op(bio, iov, i, IO_CMD_PREADV);
405 }
406
407 static void aio_write(struct bio *bio, struct iovec * iov, unsigned i)
408 {
409         aio_op(bio, iov, i, IO_CMD_PWRITEV);
410 }
411
412
413 /* not implemented */
414 static void uring_init(void) {
415         io_fallback();
416 }
417
418 struct fops fops_list[] = {
419         {
420                 .init           = uring_init,
421         }, {
422                 .init           = aio_init,
423                 .cleanup        = aio_cleanup,
424                 .read           = aio_read,
425                 .write          = aio_write,
426         }, {
427                 .init           = sync_init,
428                 .cleanup        = sync_cleanup,
429                 .read           = sync_read,
430                 .write          = sync_write,
431         }, {
432                 /* NULL */
433         }
434 };
435
436 __attribute__((constructor(102)))
437 static void blkdev_init(void)
438 {
439         fops = fops_list;
440         fops->init();
441 }
442
443 __attribute__((destructor(102)))
444 static void blkdev_cleanup(void)
445 {
446         fops->cleanup();
447 }