]> git.sesse.net Git - bcachefs-tools-debian/blob - linux/blkdev.c
Update bcachefs sources to dbee44d5ab bcachefs: add bcachefs xxhash support
[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 static io_context_t aio_ctx;
26 static atomic_t running_requests;
27
28 void generic_make_request(struct bio *bio)
29 {
30         struct iovec *iov;
31         struct bvec_iter iter;
32         struct bio_vec bv;
33         ssize_t ret;
34         unsigned i;
35
36         if (bio->bi_opf & REQ_PREFLUSH) {
37                 ret = fdatasync(bio->bi_bdev->bd_fd);
38                 if (ret) {
39                         fprintf(stderr, "fsync error: %m\n");
40                         bio->bi_status = BLK_STS_IOERR;
41                         bio_endio(bio);
42                         return;
43                 }
44         }
45
46         i = 0;
47         bio_for_each_segment(bv, bio, iter)
48                 i++;
49
50         iov = alloca(sizeof(*iov) * i);
51
52         i = 0;
53         bio_for_each_segment(bv, bio, iter) {
54                 void *start = page_address(bv.bv_page) + bv.bv_offset;
55                 size_t len = bv.bv_len;
56
57                 iov[i++] = (struct iovec) {
58                         .iov_base = start,
59                         .iov_len = len,
60                 };
61
62 #ifdef CONFIG_VALGRIND
63                 /* To be pedantic it should only be on IO completion. */
64                 if (bio_op(bio) == REQ_OP_READ)
65                         VALGRIND_MAKE_MEM_DEFINED(start, len);
66 #endif
67         }
68
69         struct iocb iocb = {
70                 .data           = bio,
71                 .aio_fildes     = bio->bi_opf & REQ_FUA
72                         ? bio->bi_bdev->bd_sync_fd
73                         : bio->bi_bdev->bd_fd,
74         }, *iocbp = &iocb;
75
76         switch (bio_op(bio)) {
77         case REQ_OP_READ:
78                 iocb.aio_lio_opcode     = IO_CMD_PREADV;
79                 iocb.u.v.vec            = iov;
80                 iocb.u.v.nr             = i;
81                 iocb.u.v.offset         = bio->bi_iter.bi_sector << 9;
82
83                 atomic_inc(&running_requests);
84                 ret = io_submit(aio_ctx, 1, &iocbp);
85                 if (ret != 1)
86                         die("io_submit err: %s", strerror(-ret));
87                 break;
88         case REQ_OP_WRITE:
89                 iocb.aio_lio_opcode     = IO_CMD_PWRITEV;
90                 iocb.u.v.vec            = iov;
91                 iocb.u.v.nr             = i;
92                 iocb.u.v.offset         = bio->bi_iter.bi_sector << 9;
93
94                 atomic_inc(&running_requests);
95                 ret = io_submit(aio_ctx, 1, &iocbp);
96                 if (ret != 1)
97                         die("io_submit err: %s", strerror(-ret));
98                 break;
99         case REQ_OP_FLUSH:
100                 ret = fsync(bio->bi_bdev->bd_fd);
101                 if (ret)
102                         die("fsync error: %m");
103                 bio_endio(bio);
104                 break;
105         default:
106                 BUG();
107         }
108 }
109
110 static void submit_bio_wait_endio(struct bio *bio)
111 {
112         complete(bio->bi_private);
113 }
114
115 int submit_bio_wait(struct bio *bio)
116 {
117         struct completion done;
118
119         init_completion(&done);
120         bio->bi_private = &done;
121         bio->bi_end_io = submit_bio_wait_endio;
122         bio->bi_opf |= REQ_SYNC;
123         submit_bio(bio);
124         wait_for_completion(&done);
125
126         return blk_status_to_errno(bio->bi_status);
127 }
128
129 int blkdev_issue_discard(struct block_device *bdev,
130                          sector_t sector, sector_t nr_sects,
131                          gfp_t gfp_mask, unsigned long flags)
132 {
133         return 0;
134 }
135
136 unsigned bdev_logical_block_size(struct block_device *bdev)
137 {
138         struct stat statbuf;
139         unsigned blksize;
140         int ret;
141
142         ret = fstat(bdev->bd_fd, &statbuf);
143         BUG_ON(ret);
144
145         if (!S_ISBLK(statbuf.st_mode))
146                 return statbuf.st_blksize >> 9;
147
148         ret = ioctl(bdev->bd_fd, BLKPBSZGET, &blksize);
149         BUG_ON(ret);
150
151         return blksize >> 9;
152 }
153
154 sector_t get_capacity(struct gendisk *disk)
155 {
156         struct block_device *bdev =
157                 container_of(disk, struct block_device, __bd_disk);
158         struct stat statbuf;
159         u64 bytes;
160         int ret;
161
162         ret = fstat(bdev->bd_fd, &statbuf);
163         BUG_ON(ret);
164
165         if (!S_ISBLK(statbuf.st_mode))
166                 return statbuf.st_size >> 9;
167
168         ret = ioctl(bdev->bd_fd, BLKGETSIZE64, &bytes);
169         BUG_ON(ret);
170
171         return bytes >> 9;
172 }
173
174 void blkdev_put(struct block_device *bdev, fmode_t mode)
175 {
176         fdatasync(bdev->bd_fd);
177         close(bdev->bd_sync_fd);
178         close(bdev->bd_fd);
179         free(bdev);
180 }
181
182 struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
183                                         void *holder)
184 {
185         struct block_device *bdev;
186         int fd, sync_fd, flags = O_DIRECT;
187
188         if ((mode & (FMODE_READ|FMODE_WRITE)) == (FMODE_READ|FMODE_WRITE))
189                 flags = O_RDWR;
190         else if (mode & FMODE_READ)
191                 flags = O_RDONLY;
192         else if (mode & FMODE_WRITE)
193                 flags = O_WRONLY;
194
195 #if 0
196         /* using O_EXCL doesn't work with opening twice for an O_SYNC fd: */
197         if (mode & FMODE_EXCL)
198                 flags |= O_EXCL;
199 #endif
200
201         fd = open(path, flags);
202         if (fd < 0)
203                 return ERR_PTR(-errno);
204
205         sync_fd = open(path, flags|O_SYNC);
206         if (sync_fd < 0) {
207                 assert(0);
208                 close(fd);
209                 return ERR_PTR(-errno);
210         }
211
212         bdev = malloc(sizeof(*bdev));
213         memset(bdev, 0, sizeof(*bdev));
214
215         strncpy(bdev->name, path, sizeof(bdev->name));
216         bdev->name[sizeof(bdev->name) - 1] = '\0';
217
218         bdev->bd_dev            = xfstat(fd).st_rdev;
219         bdev->bd_fd             = fd;
220         bdev->bd_sync_fd        = sync_fd;
221         bdev->bd_holder         = holder;
222         bdev->bd_disk           = &bdev->__bd_disk;
223         bdev->bd_bdi            = &bdev->__bd_bdi;
224         bdev->queue.backing_dev_info = bdev->bd_bdi;
225
226         return bdev;
227 }
228
229 void bdput(struct block_device *bdev)
230 {
231         BUG();
232 }
233
234 int lookup_bdev(const char *path, dev_t *dev)
235 {
236         return -EINVAL;
237 }
238
239 static int aio_completion_thread(void *arg)
240 {
241         struct io_event events[8], *ev;
242         int ret;
243         bool stop = false;
244
245         while (!stop) {
246                 ret = io_getevents(aio_ctx, 1, ARRAY_SIZE(events),
247                                    events, NULL);
248
249                 if (ret < 0 && ret == -EINTR)
250                         continue;
251                 if (ret < 0)
252                         die("io_getevents() error: %s", strerror(-ret));
253
254                 for (ev = events; ev < events + ret; ev++) {
255                         struct bio *bio = (struct bio *) ev->data;
256
257                         /* This should only happen during blkdev_cleanup() */
258                         if (!bio) {
259                                 BUG_ON(atomic_read(&running_requests) != 0);
260                                 stop = true;
261                                 continue;
262                         }
263
264                         if (ev->res != bio->bi_iter.bi_size)
265                                 bio->bi_status = BLK_STS_IOERR;
266
267                         bio_endio(bio);
268                         atomic_dec(&running_requests);
269                 }
270         }
271
272         return 0;
273 }
274
275 static struct task_struct *aio_task = NULL;
276
277 __attribute__((constructor(102)))
278 static void blkdev_init(void)
279 {
280         struct task_struct *p;
281
282         if (io_setup(256, &aio_ctx))
283                 die("io_setup() error: %m");
284
285         p = kthread_run(aio_completion_thread, NULL, "aio_completion");
286         BUG_ON(IS_ERR(p));
287
288         aio_task = p;
289 }
290
291 __attribute__((destructor(102)))
292 static void blkdev_cleanup(void)
293 {
294         struct task_struct *p = NULL;
295         swap(aio_task, p);
296         get_task_struct(p);
297
298         /* I mean, really?! IO_CMD_NOOP is even defined, but not implemented. */
299         int fds[2];
300         int ret = pipe(fds);
301         if (ret != 0)
302                 die("pipe err: %s", strerror(ret));
303
304         /* Wake up the completion thread with spurious work. */
305         int junk = 0;
306         struct iocb iocb = {
307                 .aio_lio_opcode = IO_CMD_PWRITE,
308                 .data = NULL, /* Signal to stop */
309                 .aio_fildes = fds[1],
310                 .u.c.buf = &junk,
311                 .u.c.nbytes = 1,
312         }, *iocbp = &iocb;
313         ret = io_submit(aio_ctx, 1, &iocbp);
314         if (ret != 1)
315                 die("io_submit cleanup err: %s", strerror(-ret));
316
317         ret = kthread_stop(p);
318         BUG_ON(ret);
319
320         put_task_struct(p);
321
322         close(fds[0]);
323         close(fds[1]);
324 }