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