]> git.sesse.net Git - bcachefs-tools-debian/blob - linux/blkdev.c
fix sync writes - don't use O_EXCL
[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 <linux/bio.h>
14 #include <linux/blkdev.h>
15 #include <linux/completion.h>
16 #include <linux/fs.h>
17 #include <linux/kthread.h>
18
19 #include "tools-util.h"
20
21 static io_context_t aio_ctx;
22
23 void generic_make_request(struct bio *bio)
24 {
25         struct iovec *iov;
26         struct bvec_iter iter;
27         struct bio_vec bv;
28         ssize_t ret;
29         unsigned i;
30
31         if (bio->bi_opf & REQ_PREFLUSH) {
32                 ret = fdatasync(bio->bi_bdev->bd_fd);
33                 if (ret) {
34                         fprintf(stderr, "fsync error: %m\n");
35                         bio->bi_error = -EIO;
36                         bio_endio(bio);
37                         return;
38                 }
39         }
40
41         i = 0;
42         bio_for_each_segment(bv, bio, iter)
43                 i++;
44
45         iov = alloca(sizeof(*iov) * i);
46
47         i = 0;
48         bio_for_each_segment(bv, bio, iter)
49                 iov[i++] = (struct iovec) {
50                         .iov_base = page_address(bv.bv_page) + bv.bv_offset,
51                         .iov_len = bv.bv_len,
52                 };
53
54         struct iocb iocb = {
55                 .data           = bio,
56                 .aio_fildes     = bio->bi_opf & REQ_FUA
57                         ? bio->bi_bdev->bd_sync_fd
58                         : bio->bi_bdev->bd_fd,
59         }, *iocbp = &iocb;
60
61         switch (bio_op(bio)) {
62         case REQ_OP_READ:
63                 iocb.aio_lio_opcode     = IO_CMD_PREADV;
64                 iocb.u.v.vec            = iov;
65                 iocb.u.v.nr             = i;
66                 iocb.u.v.offset         = bio->bi_iter.bi_sector << 9;
67
68                 if (io_submit(aio_ctx, 1, &iocbp) != 1)
69                         die("io_submit err: %m");
70                 break;
71         case REQ_OP_WRITE:
72                 iocb.aio_lio_opcode     = IO_CMD_PWRITEV;
73                 iocb.u.v.vec            = iov;
74                 iocb.u.v.nr             = i;
75                 iocb.u.v.offset         = bio->bi_iter.bi_sector << 9;
76
77                 if (io_submit(aio_ctx, 1, &iocbp) != 1)
78                         die("io_submit err: %m");
79                 break;
80         default:
81                 BUG();
82         }
83 }
84
85 static void submit_bio_wait_endio(struct bio *bio)
86 {
87         complete(bio->bi_private);
88 }
89
90 int submit_bio_wait(struct bio *bio)
91 {
92         struct completion done;
93
94         init_completion(&done);
95         bio->bi_private = &done;
96         bio->bi_end_io = submit_bio_wait_endio;
97         bio->bi_opf |= REQ_SYNC;
98         submit_bio(bio);
99         wait_for_completion(&done);
100
101         return bio->bi_error;
102 }
103
104 int blkdev_issue_discard(struct block_device *bdev,
105                          sector_t sector, sector_t nr_sects,
106                          gfp_t gfp_mask, unsigned long flags)
107 {
108         return 0;
109 }
110
111 unsigned bdev_logical_block_size(struct block_device *bdev)
112 {
113         struct stat statbuf;
114         unsigned blksize;
115         int ret;
116
117         ret = fstat(bdev->bd_fd, &statbuf);
118         BUG_ON(ret);
119
120         if (!S_ISBLK(statbuf.st_mode))
121                 return statbuf.st_blksize >> 9;
122
123         ret = ioctl(bdev->bd_fd, BLKPBSZGET, &blksize);
124         BUG_ON(ret);
125
126         return blksize >> 9;
127 }
128
129 sector_t get_capacity(struct gendisk *disk)
130 {
131         struct block_device *bdev =
132                 container_of(disk, struct block_device, __bd_disk);
133         struct stat statbuf;
134         u64 bytes;
135         int ret;
136
137         ret = fstat(bdev->bd_fd, &statbuf);
138         BUG_ON(ret);
139
140         if (!S_ISBLK(statbuf.st_mode))
141                 return statbuf.st_size >> 9;
142
143         ret = ioctl(bdev->bd_fd, BLKGETSIZE64, &bytes);
144         BUG_ON(ret);
145
146         return bytes >> 9;
147 }
148
149 void blkdev_put(struct block_device *bdev, fmode_t mode)
150 {
151         fdatasync(bdev->bd_fd);
152         close(bdev->bd_sync_fd);
153         close(bdev->bd_fd);
154         free(bdev);
155 }
156
157 struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
158                                         void *holder)
159 {
160         struct block_device *bdev;
161         int fd, sync_fd, flags = O_DIRECT;
162
163         if ((mode & (FMODE_READ|FMODE_WRITE)) == (FMODE_READ|FMODE_WRITE))
164                 flags = O_RDWR;
165         else if (mode & FMODE_READ)
166                 flags = O_RDONLY;
167         else if (mode & FMODE_WRITE)
168                 flags = O_WRONLY;
169
170 #if 0
171         /* using O_EXCL doesn't work with opening twice for an O_SYNC fd: */
172         if (mode & FMODE_EXCL)
173                 flags |= O_EXCL;
174 #endif
175
176         fd = open(path, flags);
177         if (fd < 0)
178                 return ERR_PTR(-errno);
179
180         sync_fd = open(path, flags|O_SYNC);
181         if (sync_fd < 0) {
182                 assert(0);
183                 close(fd);
184                 return ERR_PTR(-errno);
185         }
186
187         bdev = malloc(sizeof(*bdev));
188         memset(bdev, 0, sizeof(*bdev));
189
190         strncpy(bdev->name, path, sizeof(bdev->name));
191         bdev->name[sizeof(bdev->name) - 1] = '\0';
192
193         bdev->bd_fd             = fd;
194         bdev->bd_sync_fd        = sync_fd;
195         bdev->bd_holder         = holder;
196         bdev->bd_disk           = &bdev->__bd_disk;
197
198         return bdev;
199 }
200
201 void bdput(struct block_device *bdev)
202 {
203         BUG();
204 }
205
206 struct block_device *lookup_bdev(const char *path)
207 {
208         return ERR_PTR(-EINVAL);
209 }
210
211 static int aio_completion_thread(void *arg)
212 {
213         struct io_event events[8], *ev;
214         int ret;
215
216         while (1) {
217                 ret = io_getevents(aio_ctx, 1, ARRAY_SIZE(events),
218                                    events, NULL);
219
220                 if (ret < 0 && errno == EINTR)
221                         continue;
222                 if (ret < 0)
223                         die("io_getevents() error: %m");
224
225                 for (ev = events; ev < events + ret; ev++) {
226                         struct bio *bio = (struct bio *) ev->data;
227
228                         if (ev->res < 0)
229                                 bio->bi_error = ev->res;
230                         else if (ev->res != bio->bi_iter.bi_size)
231                                 bio->bi_error = -EIO;
232
233                         bio_endio(bio);
234                 }
235         }
236
237         return 0;
238 }
239
240 __attribute__((constructor(102)))
241 static void blkdev_init(void)
242 {
243         struct task_struct *p;
244
245         if (io_setup(256, &aio_ctx))
246                 die("io_setup() error: %m");
247
248         p = kthread_run(aio_completion_thread, NULL, "aio_completion");
249         BUG_ON(IS_ERR(p));
250 }