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