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)
136 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
137 #define LOG_FLAGS_BUF_SIZE 128
139 * Convert numeric flags to human readable flags.
140 * @flags: numeric flags
141 * @buf: output buffer for human readable string.
142 * must have enough space (LOG_FLAGS_BUF_SIZE) to contain all
145 static void entry_flags_to_str(u64 flags, char *buf)
152 for (i = 0; i < ARRAY_SIZE(log_flags_table); i++) {
153 if (flags & log_flags_table[i].flags) {
155 strncat(buf, "|", LOG_FLAGS_BUF_SIZE);
157 strncat(buf, log_flags_table[i].str, LOG_FLAGS_BUF_SIZE);
158 flags &= ~log_flags_table[i].flags;
163 strncat(buf, "|", LOG_FLAGS_BUF_SIZE);
165 left_len = LOG_FLAGS_BUF_SIZE - strnlen(buf,
168 snprintf(buf + strnlen(buf, LOG_FLAGS_BUF_SIZE),
169 left_len, "UNKNOWN.0x%llx", flags);
172 strncpy(buf, "NONE", LOG_FLAGS_BUF_SIZE);
176 * @log: the log we are replaying.
177 * @entry: entry to be replayed.
179 * @return: 0 if we should replay the entry, > 0 if we should skip it.
181 * Should we skip the entry in our log or replay onto the replay device.
183 int log_should_skip(struct log *log, struct log_write_entry *entry)
185 u64 sector = le64_to_cpu(entry->sector);
186 u64 nr_sectors = le64_to_cpu(entry->nr_sectors);
190 if (sector + nr_sectors <= log->start_sector ||
191 sector > log->end_sector)
197 * @entry: entry to be replayed.
199 * @return: 1 if the entry is sane, 0 if it is invalid.
201 * Check if this is a sane log entry.
203 int log_entry_valid(struct log_write_entry *entry)
205 u64 flags = le64_to_cpu(entry->flags);
207 /* Suspect all zeroes entry */
208 if (!flags && !entry->nr_sectors)
210 /* Suspect non zero padded entry */
211 if (flags != LOG_MARK_FLAG && entry->data[0] != 0)
217 * @log: the log we are replaying.
218 * @entry: where we put the entry.
219 * @read_data: read the entry data as well, entry must be log->sectorsize sized
222 * @return: 0 if we replayed, 1 if we are at the end, -1 if there was an error.
224 * Replay the next entry in our log onto the replay device.
226 int log_replay_next_entry(struct log *log, struct log_write_entry *entry,
231 size_t read_size = read_data ? log->sectorsize :
232 sizeof(struct log_write_entry);
234 char flags_buf[LOG_FLAGS_BUF_SIZE];
239 if (log->cur_entry >= log->nr_entries)
242 ret = read(log->logfd, entry, read_size);
243 if (ret != read_size) {
244 fprintf(stderr, "Error reading entry: %d\n", errno);
247 if (!log_entry_valid(entry)) {
248 fprintf(stderr, "Malformed entry @%llu\n",
249 log->cur_pos / log->sectorsize);
254 size = le64_to_cpu(entry->nr_sectors) * log->sectorsize;
255 if (read_size < log->sectorsize) {
256 log->cur_pos = lseek(log->logfd,
257 log->sectorsize - sizeof(struct log_write_entry), SEEK_CUR);
258 if (log->cur_pos == (off_t)-1) {
259 fprintf(stderr, "Error seeking in log: %d\n", errno);
263 log->cur_pos += read_size;
266 flags = le64_to_cpu(entry->flags);
267 entry_flags_to_str(flags, flags_buf);
268 skip = log_should_skip(log, entry);
269 if (log_writes_verbose > 1 || (log_writes_verbose && !skip)) {
270 printf("%s %d@%llu: sector %llu, size %llu, flags 0x%llx(%s)\n",
271 skip ? "skipping" : "replaying",
272 (int)log->cur_entry - 1, log->cur_pos / log->sectorsize,
273 (unsigned long long)le64_to_cpu(entry->sector),
274 (unsigned long long)size,
275 (unsigned long long)flags, flags_buf);
280 if (flags & LOG_DISCARD_FLAG)
281 return log_discard(log, entry);
284 log->cur_pos = lseek(log->logfd, size, SEEK_CUR);
285 if (log->cur_pos == (off_t)-1) {
286 fprintf(stderr, "Error seeking in log: %d\n", errno);
294 fprintf(stderr, "Error allocating buffer %llu entry %llu\n", (unsigned long long)size, (unsigned long long)log->cur_entry - 1);
298 ret = read(log->logfd, buf, size);
300 fprintf(stderr, "Error reading data: %d\n", errno);
304 log->cur_pos += size;
306 offset = le64_to_cpu(entry->sector) * log->sectorsize;
307 ret = pwrite(log->replayfd, buf, size, offset);
310 fprintf(stderr, "Error writing data: %d\n", errno);
318 * @log: the log we are manipulating.
319 * @entry_num: the entry we want.
321 * Seek to the given entry in the log, starting at 0 and ending at
322 * log->nr_entries - 1.
324 int log_seek_entry(struct log *log, u64 entry_num)
328 if (entry_num >= log->nr_entries) {
329 fprintf(stderr, "Invalid entry number\n");
333 /* Skip the first sector containing the log super block */
334 log->cur_pos = lseek(log->logfd, log->sectorsize, SEEK_SET);
335 if (log->cur_pos == (off_t)-1) {
336 fprintf(stderr, "Error seeking in file: %d\n", errno);
341 for (i = 0; i < entry_num; i++) {
342 struct log_write_entry entry;
347 ret = read(log->logfd, &entry, sizeof(entry));
348 if (ret != sizeof(entry)) {
349 fprintf(stderr, "Error reading entry: %d\n", errno);
352 if (!log_entry_valid(&entry)) {
353 fprintf(stderr, "Malformed entry @%llu\n",
354 log->cur_pos / log->sectorsize);
357 if (log_writes_verbose > 1)
358 printf("seek entry %d@%llu: %llu, size %llu, flags 0x%llx\n",
359 (int)i, log->cur_pos / log->sectorsize,
360 (unsigned long long)le64_to_cpu(entry.sector),
361 (unsigned long long)le64_to_cpu(entry.nr_sectors),
362 (unsigned long long)le64_to_cpu(entry.flags));
363 flags = le64_to_cpu(entry.flags);
364 seek_size = log->sectorsize - sizeof(entry);
365 if (!(flags & LOG_DISCARD_FLAG))
366 seek_size += le64_to_cpu(entry.nr_sectors) *
368 log->cur_pos = lseek(log->logfd, seek_size, SEEK_CUR);
369 if (log->cur_pos == (off_t)-1) {
370 fprintf(stderr, "Error seeking in file: %d\n", errno);
380 * @log: the log we are manipulating.
381 * @entry: the entry we read.
382 * @read_data: read the extra data for the entry, your entry must be
383 * log->sectorsize large.
385 * @return: 1 if we hit the end of the log, 0 we got the next entry, < 0 if
386 * there was an error.
388 * Seek to the next entry in the log.
390 int log_seek_next_entry(struct log *log, struct log_write_entry *entry,
393 size_t read_size = read_data ? log->sectorsize :
394 sizeof(struct log_write_entry);
396 char flags_buf[LOG_FLAGS_BUF_SIZE];
399 if (log->cur_entry >= log->nr_entries)
402 ret = read(log->logfd, entry, read_size);
403 if (ret != read_size) {
404 fprintf(stderr, "Error reading entry: %d\n", errno);
407 if (!log_entry_valid(entry)) {
408 fprintf(stderr, "Malformed entry @%llu\n",
409 log->cur_pos / log->sectorsize);
414 if (read_size < log->sectorsize) {
415 log->cur_pos = lseek(log->logfd,
416 log->sectorsize - sizeof(struct log_write_entry), SEEK_CUR);
417 if (log->cur_pos == (off_t)-1) {
418 fprintf(stderr, "Error seeking in log: %d\n", errno);
422 log->cur_pos += read_size;
424 flags = le64_to_cpu(entry->flags);
425 entry_flags_to_str(flags, flags_buf);
426 if (log_writes_verbose > 1)
427 printf("seek entry %d@%llu: %llu, size %llu, flags 0x%llx(%s)\n",
428 (int)log->cur_entry - 1, log->cur_pos / log->sectorsize,
429 (unsigned long long)le64_to_cpu(entry->sector),
430 (unsigned long long)le64_to_cpu(entry->nr_sectors),
431 (unsigned long long)flags, flags_buf);
433 read_size = le64_to_cpu(entry->nr_sectors) * log->sectorsize;
434 if (!read_size || (flags & LOG_DISCARD_FLAG))
437 log->cur_pos = lseek(log->logfd, read_size, SEEK_CUR);
438 if (log->cur_pos == (off_t)-1) {
439 fprintf(stderr, "Error seeking in log: %d\n", errno);
447 * @logfile: the file that contains the write log.
448 * @replayfile: the file/device to replay onto, can be NULL.
450 * Opens a logfile and makes sure it is valid and returns a struct log.
452 struct log *log_open(char *logfile, char *replayfile)
455 struct log_write_super super;
458 log = malloc(sizeof(struct log));
460 fprintf(stderr, "Couldn't alloc log\n");
466 log->logfd = open(logfile, O_RDONLY);
467 if (log->logfd < 0) {
468 fprintf(stderr, "Couldn't open log %s: %d\n", logfile,
475 log->replayfd = open(replayfile, O_WRONLY);
476 if (log->replayfd < 0) {
477 fprintf(stderr, "Couldn't open replay file %s: %d\n",
484 ret = read(log->logfd, &super, sizeof(struct log_write_super));
485 if (ret < sizeof(struct log_write_super)) {
486 fprintf(stderr, "Error reading super: %d\n", errno);
491 if (le64_to_cpu(super.magic) != WRITE_LOG_MAGIC) {
492 fprintf(stderr, "Magic doesn't match\n");
497 if (le64_to_cpu(super.version) != WRITE_LOG_VERSION) {
498 fprintf(stderr, "Version mismatch, wanted %d, have %d\n",
499 WRITE_LOG_VERSION, (int)le64_to_cpu(super.version));
504 log->sectorsize = le32_to_cpu(super.sectorsize);
505 log->nr_entries = le64_to_cpu(super.nr_entries);
506 log->max_zero_size = 128 * 1024 * 1024;
508 log->cur_pos = lseek(log->logfd, log->sectorsize - sizeof(super), SEEK_CUR);
509 if (log->cur_pos == (off_t) -1) {
510 fprintf(stderr, "Error seeking to first entry: %d\n", errno);