1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
// SPDX-License-Identifier: Apache-2.0
#define _LARGEFILE64_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/prctl.h>
#include <unistd.h>
#include <iostream>
#include <string>
#define SECTOR_SIZE ((__u64)512)
#define BUFFER_BYTES 4096
#define MAX(a, b) ((a) > (b) ? (a) : (b))
/* This should be replaced with linux/dm-user.h. */
#ifndef _LINUX_DM_USER_H
#define _LINUX_DM_USER_H
#include <linux/types.h>
#define DM_USER_REQ_MAP_READ 0
#define DM_USER_REQ_MAP_WRITE 1
#define DM_USER_REQ_MAP_FLUSH 2
#define DM_USER_REQ_MAP_DISCARD 3
#define DM_USER_REQ_MAP_SECURE_ERASE 4
#define DM_USER_REQ_MAP_WRITE_SAME 5
#define DM_USER_REQ_MAP_WRITE_ZEROES 6
#define DM_USER_REQ_MAP_ZONE_OPEN 7
#define DM_USER_REQ_MAP_ZONE_CLOSE 8
#define DM_USER_REQ_MAP_ZONE_FINISH 9
#define DM_USER_REQ_MAP_ZONE_APPEND 10
#define DM_USER_REQ_MAP_ZONE_RESET 11
#define DM_USER_REQ_MAP_ZONE_RESET_ALL 12
#define DM_USER_REQ_MAP_FLAG_FAILFAST_DEV 0x00001
#define DM_USER_REQ_MAP_FLAG_FAILFAST_TRANSPORT 0x00002
#define DM_USER_REQ_MAP_FLAG_FAILFAST_DRIVER 0x00004
#define DM_USER_REQ_MAP_FLAG_SYNC 0x00008
#define DM_USER_REQ_MAP_FLAG_META 0x00010
#define DM_USER_REQ_MAP_FLAG_PRIO 0x00020
#define DM_USER_REQ_MAP_FLAG_NOMERGE 0x00040
#define DM_USER_REQ_MAP_FLAG_IDLE 0x00080
#define DM_USER_REQ_MAP_FLAG_INTEGRITY 0x00100
#define DM_USER_REQ_MAP_FLAG_FUA 0x00200
#define DM_USER_REQ_MAP_FLAG_PREFLUSH 0x00400
#define DM_USER_REQ_MAP_FLAG_RAHEAD 0x00800
#define DM_USER_REQ_MAP_FLAG_BACKGROUND 0x01000
#define DM_USER_REQ_MAP_FLAG_NOWAIT 0x02000
#define DM_USER_REQ_MAP_FLAG_CGROUP_PUNT 0x04000
#define DM_USER_REQ_MAP_FLAG_NOUNMAP 0x08000
#define DM_USER_REQ_MAP_FLAG_HIPRI 0x10000
#define DM_USER_REQ_MAP_FLAG_DRV 0x20000
#define DM_USER_REQ_MAP_FLAG_SWAP 0x40000
#define DM_USER_RESP_SUCCESS 0
#define DM_USER_RESP_ERROR 1
#define DM_USER_RESP_UNSUPPORTED 2
struct dm_user_message {
__u64 seq;
__u64 type;
__u64 flags;
__u64 sector;
__u64 len;
__u8 buf[];
};
#endif
static bool verbose = false;
ssize_t write_all(int fd, void* buf, size_t len) {
char* buf_c = (char*)buf;
ssize_t total = 0;
ssize_t once;
while (total < static_cast<ssize_t>(len)) {
once = write(fd, buf_c + total, len - total);
if (once < 0) return once;
if (once == 0) {
errno = ENOSPC;
return 0;
}
total += once;
}
return total;
}
ssize_t read_all(int fd, void* buf, size_t len) {
char* buf_c = (char*)buf;
ssize_t total = 0;
ssize_t once;
while (total < static_cast<ssize_t>(len)) {
once = read(fd, buf_c + total, len - total);
if (once < 0) return once;
if (once == 0) {
errno = ENOSPC;
return 0;
}
total += once;
}
return total;
}
int not_splice(int from, int to, __u64 count) {
while (count > 0) {
char buf[BUFFER_BYTES];
__u64 max = count > BUFFER_BYTES ? BUFFER_BYTES : count;
if (read_all(from, buf, max) <= 0) {
perror("Unable to read");
return -EIO;
}
if (write_all(to, buf, max) <= 0) {
perror("Unable to write");
return -EIO;
}
count -= max;
}
return 0;
}
static int simple_daemon(const std::string& control_path, const std::string& backing_path) {
int control_fd = open(control_path.c_str(), O_RDWR);
if (control_fd < 0) {
fprintf(stderr, "Unable to open control device %s\n", control_path.c_str());
return -1;
}
int backing_fd = open(backing_path.c_str(), O_RDWR);
if (backing_fd < 0) {
fprintf(stderr, "Unable to open backing device %s\n", backing_path.c_str());
return -1;
}
while (1) {
struct dm_user_message msg;
char* base;
__u64 type;
if (verbose) std::cerr << "dmuserd: Waiting for message...\n";
if (read_all(control_fd, &msg, sizeof(msg)) < 0) {
if (errno == ENOTBLK) return 0;
perror("unable to read msg");
return -1;
}
if (verbose) {
std::string type;
switch (msg.type) {
case DM_USER_REQ_MAP_WRITE:
type = "write";
break;
case DM_USER_REQ_MAP_READ:
type = "read";
break;
case DM_USER_REQ_MAP_FLUSH:
type = "flush";
break;
default:
/*
* FIXME: Can't I do "whatever"s here rather that
* std::string("whatever")?
*/
type = std::string("(unknown, id=") + std::to_string(msg.type) + ")";
break;
}
std::string flags;
if (msg.flags & DM_USER_REQ_MAP_FLAG_SYNC) {
if (!flags.empty()) flags += "|";
flags += "S";
}
if (msg.flags & DM_USER_REQ_MAP_FLAG_META) {
if (!flags.empty()) flags += "|";
flags += "M";
}
if (msg.flags & DM_USER_REQ_MAP_FLAG_FUA) {
if (!flags.empty()) flags += "|";
flags += "FUA";
}
if (msg.flags & DM_USER_REQ_MAP_FLAG_PREFLUSH) {
if (!flags.empty()) flags += "|";
flags += "F";
}
std::cerr << "dmuserd: Got " << type << " request " << flags << " for sector "
<< std::to_string(msg.sector) << " with length " << std::to_string(msg.len)
<< "\n";
}
type = msg.type;
switch (type) {
case DM_USER_REQ_MAP_READ:
msg.type = DM_USER_RESP_SUCCESS;
break;
case DM_USER_REQ_MAP_WRITE:
if (msg.flags & DM_USER_REQ_MAP_FLAG_PREFLUSH ||
msg.flags & DM_USER_REQ_MAP_FLAG_FUA) {
if (fsync(backing_fd) < 0) {
perror("Unable to fsync(), just sync()ing instead");
sync();
}
}
msg.type = DM_USER_RESP_SUCCESS;
if (lseek64(backing_fd, msg.sector * SECTOR_SIZE, SEEK_SET) < 0) {
perror("Unable to seek");
return -1;
}
if (not_splice(control_fd, backing_fd, msg.len) < 0) {
if (errno == ENOTBLK) return 0;
std::cerr << "unable to handle write data\n";
return -1;
}
if (msg.flags & DM_USER_REQ_MAP_FLAG_FUA) {
if (fsync(backing_fd) < 0) {
perror("Unable to fsync(), just sync()ing instead");
sync();
}
}
break;
case DM_USER_REQ_MAP_FLUSH:
msg.type = DM_USER_RESP_SUCCESS;
if (fsync(backing_fd) < 0) {
perror("Unable to fsync(), just sync()ing instead");
sync();
}
break;
default:
std::cerr << "dmuserd: unsupported op " << std::to_string(msg.type) << "\n";
msg.type = DM_USER_RESP_UNSUPPORTED;
break;
}
if (verbose) std::cerr << "dmuserd: Responding to message\n";
if (write_all(control_fd, &msg, sizeof(msg)) < 0) {
if (errno == ENOTBLK) return 0;
perror("unable to write msg");
return -1;
}
switch (type) {
case DM_USER_REQ_MAP_READ:
if (verbose) std::cerr << "dmuserd: Sending read data\n";
if (lseek64(backing_fd, msg.sector * SECTOR_SIZE, SEEK_SET) < 0) {
perror("Unable to seek");
return -1;
}
if (not_splice(backing_fd, control_fd, msg.len) < 0) {
if (errno == ENOTBLK) return 0;
std::cerr << "unable to handle read data\n";
return -1;
}
break;
}
}
/* The daemon doesn't actully terminate for this test. */
perror("Unable to read from control device");
return -1;
}
void usage(char* prog) {
printf("Usage: %s\n", prog);
printf(" Handles block requests in userspace, backed by memory\n");
printf(" -h Display this help message\n");
printf(" -c <control dev> Control device to use for the test\n");
printf(" -b <store path> The file to use as a backing store, otherwise memory\n");
printf(" -v Enable verbose mode\n");
}
int main(int argc, char* argv[]) {
std::string control_path;
std::string backing_path;
char* store;
int c;
prctl(PR_SET_IO_FLUSHER, 0, 0, 0, 0);
while ((c = getopt(argc, argv, "h:c:s:b:v")) != -1) {
switch (c) {
case 'h':
usage(basename(argv[0]));
exit(0);
case 'c':
control_path = optarg;
break;
case 'b':
backing_path = optarg;
break;
case 'v':
verbose = true;
break;
default:
usage(basename(argv[0]));
exit(1);
}
}
int r = simple_daemon(control_path, backing_path);
if (r) fprintf(stderr, "simple_daemon() errored out\n");
return r;
}
|