]> git.sesse.net Git - bcachefs-tools-debian/blob - linux/blkdev.c
New upstream snapshot
[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_fd             = fd;
219         bdev->bd_sync_fd        = sync_fd;
220         bdev->bd_holder         = holder;
221         bdev->bd_disk           = &bdev->__bd_disk;
222         bdev->bd_bdi            = &bdev->__bd_bdi;
223         bdev->queue.backing_dev_info = bdev->bd_bdi;
224
225         return bdev;
226 }
227
228 void bdput(struct block_device *bdev)
229 {
230         BUG();
231 }
232
233 struct block_device *lookup_bdev(const char *path)
234 {
235         return ERR_PTR(-EINVAL);
236 }
237
238 static int aio_completion_thread(void *arg)
239 {
240         struct io_event events[8], *ev;
241         int ret;
242         bool stop = false;
243
244         while (!stop) {
245                 ret = io_getevents(aio_ctx, 1, ARRAY_SIZE(events),
246                                    events, NULL);
247
248                 if (ret < 0 && ret == -EINTR)
249                         continue;
250                 if (ret < 0)
251                         die("io_getevents() error: %s", strerror(-ret));
252
253                 for (ev = events; ev < events + ret; ev++) {
254                         struct bio *bio = (struct bio *) ev->data;
255
256                         /* This should only happen during blkdev_cleanup() */
257                         if (!bio) {
258                                 BUG_ON(atomic_read(&running_requests) != 0);
259                                 stop = true;
260                                 continue;
261                         }
262
263                         if (ev->res != bio->bi_iter.bi_size)
264                                 bio->bi_status = BLK_STS_IOERR;
265
266                         bio_endio(bio);
267                         atomic_dec(&running_requests);
268                 }
269         }
270
271         return 0;
272 }
273
274 static struct task_struct *aio_task = NULL;
275
276 __attribute__((constructor(102)))
277 static void blkdev_init(void)
278 {
279         struct task_struct *p;
280
281         if (io_setup(256, &aio_ctx))
282                 die("io_setup() error: %m");
283
284         p = kthread_run(aio_completion_thread, NULL, "aio_completion");
285         BUG_ON(IS_ERR(p));
286
287         aio_task = p;
288 }
289
290 __attribute__((destructor(102)))
291 static void blkdev_cleanup(void)
292 {
293         struct task_struct *p = NULL;
294         swap(aio_task, p);
295         get_task_struct(p);
296
297         /* I mean, really?! IO_CMD_NOOP is even defined, but not implemented. */
298         int fds[2];
299         int ret = pipe(fds);
300         if (ret != 0)
301                 die("pipe err: %s", strerror(ret));
302
303         /* Wake up the completion thread with spurious work. */
304         int junk = 0;
305         struct iocb iocb = {
306                 .aio_lio_opcode = IO_CMD_PWRITE,
307                 .data = NULL, /* Signal to stop */
308                 .aio_fildes = fds[1],
309                 .u.c.buf = &junk,
310                 .u.c.nbytes = 1,
311         }, *iocbp = &iocb;
312         ret = io_submit(aio_ctx, 1, &iocbp);
313         if (ret != 1)
314                 die("io_submit cleanup err: %s", strerror(-ret));
315
316         ret = kthread_stop(p);
317         BUG_ON(ret);
318
319         put_task_struct(p);
320
321         close(fds[0]);
322         close(fds[1]);
323 }