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