11 #include "log-writes.h"
13 int log_writes_verbose = 0;
16 * @log: the log to free.
18 * This will close any open fd's the log has and free up its memory.
20 void log_free(struct log *log)
22 if (log->replayfd >= 0)
29 static int discard_range(struct log *log, u64 start, u64 len)
31 u64 range[2] = { start, len };
33 if (ioctl(log->replayfd, BLKDISCARD, &range) < 0) {
34 if (log_writes_verbose)
35 printf("replay device doesn't support discard, "
36 "switching to writing zeros\n");
37 log->flags |= LOG_DISCARD_NOT_SUPP;
42 static int zero_range(struct log *log, u64 start, u64 len)
48 if (log->max_zero_size < len) {
49 if (log_writes_verbose)
50 printf("discard len %llu larger than max %llu\n",
51 (unsigned long long)len,
52 (unsigned long long)log->max_zero_size);
57 buf = malloc(bufsize);
61 fprintf(stderr, "Couldn't allocate zero buffer");
66 memset(buf, 0, bufsize);
71 ret = pwrite(log->replayfd, buf, bufsize, start);
73 fprintf(stderr, "Error zeroing file: %d\n", errno);
85 * @log: the log we are replaying.
86 * @entry: the discard entry.
88 * Discard the given length. If the device supports discard we will call that
89 * ioctl, otherwise we will write 0's to emulate discard. If the discard size
90 * is larger than log->max_zero_size then we will simply skip the zero'ing if
91 * the drive doesn't support discard.
93 int log_discard(struct log *log, struct log_write_entry *entry)
95 u64 start = le64_to_cpu(entry->sector) * log->sectorsize;
96 u64 size = le64_to_cpu(entry->nr_sectors) * log->sectorsize;
97 u64 max_chunk = 1 * 1024 * 1024 * 1024;
99 if (log->flags & LOG_IGNORE_DISCARD)
103 u64 len = size > max_chunk ? max_chunk : size;
107 * Do this check first in case it is our first discard, that way
108 * if we return EOPNOTSUPP we will fall back to the 0 method
111 if (!(log->flags & LOG_DISCARD_NOT_SUPP))
112 ret = discard_range(log, start, len);
113 if (log->flags & LOG_DISCARD_NOT_SUPP)
114 ret = zero_range(log, start, len);
123 #define DEFINE_LOG_FLAGS_STR_ENTRY(x) \
126 struct flags_to_str_entry {
129 } log_flags_table[] = {
130 DEFINE_LOG_FLAGS_STR_ENTRY(FLUSH),
131 DEFINE_LOG_FLAGS_STR_ENTRY(FUA),
132 DEFINE_LOG_FLAGS_STR_ENTRY(DISCARD),
133 DEFINE_LOG_FLAGS_STR_ENTRY(MARK),
134 DEFINE_LOG_FLAGS_STR_ENTRY(METADATA)
137 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
138 #define LOG_FLAGS_BUF_SIZE 128
140 * Convert numeric flags to human readable flags.
141 * @flags: numeric flags
142 * @buf: output buffer for human readable string.
143 * must have enough space (LOG_FLAGS_BUF_SIZE) to contain all
146 static void entry_flags_to_str(u64 flags, char *buf)
153 for (i = 0; i < ARRAY_SIZE(log_flags_table); i++) {
154 if (flags & log_flags_table[i].flags) {
156 strncat(buf, "|", LOG_FLAGS_BUF_SIZE);
158 strncat(buf, log_flags_table[i].str, LOG_FLAGS_BUF_SIZE);
159 flags &= ~log_flags_table[i].flags;
164 strncat(buf, "|", LOG_FLAGS_BUF_SIZE);
166 left_len = LOG_FLAGS_BUF_SIZE - strnlen(buf,
169 snprintf(buf + strnlen(buf, LOG_FLAGS_BUF_SIZE),
170 left_len, "UNKNOWN.0x%llx", flags);
173 strncpy(buf, "NONE", LOG_FLAGS_BUF_SIZE);
177 * @log: the log we are replaying.
178 * @entry: entry to be replayed.
180 * @return: 0 if we should replay the entry, > 0 if we should skip it.
182 * Should we skip the entry in our log or replay onto the replay device.
184 int log_should_skip(struct log *log, struct log_write_entry *entry)
186 u64 sector = le64_to_cpu(entry->sector);
187 u64 nr_sectors = le64_to_cpu(entry->nr_sectors);
191 if (sector + nr_sectors <= log->start_sector ||
192 sector > log->end_sector)
198 * @entry: entry to be replayed.
200 * @return: 1 if the entry is sane, 0 if it is invalid.
202 * Check if this is a sane log entry.
204 int log_entry_valid(struct log_write_entry *entry)
206 u64 flags = le64_to_cpu(entry->flags);
208 /* Suspect all zeroes entry */
209 if (!flags && !entry->nr_sectors)
211 /* Suspect non zero padded entry */
212 if (flags != LOG_MARK_FLAG && entry->data[0] != 0)
218 * @log: the log we are replaying.
219 * @entry: where we put the entry.
220 * @read_data: read the entry data as well, entry must be log->sectorsize sized
223 * @return: 0 if we replayed, 1 if we are at the end, -1 if there was an error.
225 * Replay the next entry in our log onto the replay device.
227 int log_replay_next_entry(struct log *log, struct log_write_entry *entry,
232 size_t read_size = read_data ? log->sectorsize :
233 sizeof(struct log_write_entry);
235 char flags_buf[LOG_FLAGS_BUF_SIZE];
240 if (log->cur_entry >= log->nr_entries)
243 ret = read(log->logfd, entry, read_size);
244 if (ret != read_size) {
245 fprintf(stderr, "Error reading entry: %d\n", errno);
248 if (!log_entry_valid(entry)) {
249 fprintf(stderr, "Malformed entry @%llu\n",
250 log->cur_pos / log->sectorsize);
255 size = le64_to_cpu(entry->nr_sectors) * log->sectorsize;
256 if (read_size < log->sectorsize) {
257 log->cur_pos = lseek(log->logfd,
258 log->sectorsize - sizeof(struct log_write_entry), SEEK_CUR);
259 if (log->cur_pos == (off_t)-1) {
260 fprintf(stderr, "Error seeking in log: %d\n", errno);
264 log->cur_pos += read_size;
267 flags = le64_to_cpu(entry->flags);
268 entry_flags_to_str(flags, flags_buf);
269 skip = log_should_skip(log, entry);
270 if (log_writes_verbose > 1 || (log_writes_verbose && !skip)) {
271 printf("%s %d@%llu: sector %llu, size %llu, flags 0x%llx(%s)\n",
272 skip ? "skipping" : "replaying",
273 (int)log->cur_entry - 1, log->cur_pos / log->sectorsize,
274 (unsigned long long)le64_to_cpu(entry->sector),
275 (unsigned long long)size,
276 (unsigned long long)flags, flags_buf);
281 if (flags & LOG_DISCARD_FLAG)
282 return log_discard(log, entry);
285 log->cur_pos = lseek(log->logfd, size, SEEK_CUR);
286 if (log->cur_pos == (off_t)-1) {
287 fprintf(stderr, "Error seeking in log: %d\n", errno);
295 fprintf(stderr, "Error allocating buffer %llu entry %llu\n", (unsigned long long)size, (unsigned long long)log->cur_entry - 1);
299 ret = read(log->logfd, buf, size);
301 fprintf(stderr, "Error reading data: %d\n", errno);
305 log->cur_pos += size;
307 offset = le64_to_cpu(entry->sector) * log->sectorsize;
308 ret = pwrite(log->replayfd, buf, size, offset);
311 fprintf(stderr, "Error writing data: %d\n", errno);
319 * @log: the log we are manipulating.
320 * @entry_num: the entry we want.
322 * Seek to the given entry in the log, starting at 0 and ending at
323 * log->nr_entries - 1.
325 int log_seek_entry(struct log *log, u64 entry_num)
329 if (entry_num >= log->nr_entries) {
330 fprintf(stderr, "Invalid entry number\n");
334 /* Skip the first sector containing the log super block */
335 log->cur_pos = lseek(log->logfd, log->sectorsize, SEEK_SET);
336 if (log->cur_pos == (off_t)-1) {
337 fprintf(stderr, "Error seeking in file: %d\n", errno);
342 for (i = 0; i < entry_num; i++) {
343 struct log_write_entry entry;
348 ret = read(log->logfd, &entry, sizeof(entry));
349 if (ret != sizeof(entry)) {
350 fprintf(stderr, "Error reading entry: %d\n", errno);
353 if (!log_entry_valid(&entry)) {
354 fprintf(stderr, "Malformed entry @%llu\n",
355 log->cur_pos / log->sectorsize);
358 if (log_writes_verbose > 1)
359 printf("seek entry %d@%llu: %llu, size %llu, flags 0x%llx\n",
360 (int)i, log->cur_pos / log->sectorsize,
361 (unsigned long long)le64_to_cpu(entry.sector),
362 (unsigned long long)le64_to_cpu(entry.nr_sectors),
363 (unsigned long long)le64_to_cpu(entry.flags));
364 flags = le64_to_cpu(entry.flags);
365 seek_size = log->sectorsize - sizeof(entry);
366 if (!(flags & LOG_DISCARD_FLAG))
367 seek_size += le64_to_cpu(entry.nr_sectors) *
369 log->cur_pos = lseek(log->logfd, seek_size, SEEK_CUR);
370 if (log->cur_pos == (off_t)-1) {
371 fprintf(stderr, "Error seeking in file: %d\n", errno);
381 * @log: the log we are manipulating.
382 * @entry: the entry we read.
383 * @read_data: read the extra data for the entry, your entry must be
384 * log->sectorsize large.
386 * @return: 1 if we hit the end of the log, 0 we got the next entry, < 0 if
387 * there was an error.
389 * Seek to the next entry in the log.
391 int log_seek_next_entry(struct log *log, struct log_write_entry *entry,
394 size_t read_size = read_data ? log->sectorsize :
395 sizeof(struct log_write_entry);
397 char flags_buf[LOG_FLAGS_BUF_SIZE];
400 if (log->cur_entry >= log->nr_entries)
403 ret = read(log->logfd, entry, read_size);
404 if (ret != read_size) {
405 fprintf(stderr, "Error reading entry: %d\n", errno);
408 if (!log_entry_valid(entry)) {
409 fprintf(stderr, "Malformed entry @%llu\n",
410 log->cur_pos / log->sectorsize);
415 if (read_size < log->sectorsize) {
416 log->cur_pos = lseek(log->logfd,
417 log->sectorsize - sizeof(struct log_write_entry), SEEK_CUR);
418 if (log->cur_pos == (off_t)-1) {
419 fprintf(stderr, "Error seeking in log: %d\n", errno);
423 log->cur_pos += read_size;
425 flags = le64_to_cpu(entry->flags);
426 entry_flags_to_str(flags, flags_buf);
427 if (log_writes_verbose > 1)
428 printf("seek entry %d@%llu: %llu, size %llu, flags 0x%llx(%s)\n",
429 (int)log->cur_entry - 1, log->cur_pos / log->sectorsize,
430 (unsigned long long)le64_to_cpu(entry->sector),
431 (unsigned long long)le64_to_cpu(entry->nr_sectors),
432 (unsigned long long)flags, flags_buf);
434 read_size = le64_to_cpu(entry->nr_sectors) * log->sectorsize;
435 if (!read_size || (flags & LOG_DISCARD_FLAG))
438 log->cur_pos = lseek(log->logfd, read_size, SEEK_CUR);
439 if (log->cur_pos == (off_t)-1) {
440 fprintf(stderr, "Error seeking in log: %d\n", errno);
448 * @logfile: the file that contains the write log.
449 * @replayfile: the file/device to replay onto, can be NULL.
451 * Opens a logfile and makes sure it is valid and returns a struct log.
453 struct log *log_open(char *logfile, char *replayfile)
456 struct log_write_super super;
459 log = malloc(sizeof(struct log));
461 fprintf(stderr, "Couldn't alloc log\n");
467 log->logfd = open(logfile, O_RDONLY);
468 if (log->logfd < 0) {
469 fprintf(stderr, "Couldn't open log %s: %d\n", logfile,
476 log->replayfd = open(replayfile, O_WRONLY);
477 if (log->replayfd < 0) {
478 fprintf(stderr, "Couldn't open replay file %s: %d\n",
485 ret = read(log->logfd, &super, sizeof(struct log_write_super));
486 if (ret < sizeof(struct log_write_super)) {
487 fprintf(stderr, "Error reading super: %d\n", errno);
492 if (le64_to_cpu(super.magic) != WRITE_LOG_MAGIC) {
493 fprintf(stderr, "Magic doesn't match\n");
498 if (le64_to_cpu(super.version) != WRITE_LOG_VERSION) {
499 fprintf(stderr, "Version mismatch, wanted %d, have %d\n",
500 WRITE_LOG_VERSION, (int)le64_to_cpu(super.version));
505 log->sectorsize = le32_to_cpu(super.sectorsize);
506 log->nr_entries = le64_to_cpu(super.nr_entries);
507 log->max_zero_size = 128 * 1024 * 1024;
509 log->cur_pos = lseek(log->logfd, log->sectorsize - sizeof(super), SEEK_CUR);
510 if (log->cur_pos == (off_t) -1) {
511 fprintf(stderr, "Error seeking to first entry: %d\n", errno);