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);
68 ret = pwrite(log->replayfd, buf, bufsize, start);
70 fprintf(stderr, "Error zeroing file: %d\n", errno);
82 * @log: the log we are replaying.
83 * @entry: the discard entry.
85 * Discard the given length. If the device supports discard we will call that
86 * ioctl, otherwise we will write 0's to emulate discard. If the discard size
87 * is larger than log->max_zero_size then we will simply skip the zero'ing if
88 * the drive doesn't support discard.
90 int log_discard(struct log *log, struct log_write_entry *entry)
92 u64 start = le64_to_cpu(entry->sector) * log->sectorsize;
93 u64 size = le64_to_cpu(entry->nr_sectors) * log->sectorsize;
94 u64 max_chunk = 1 * 1024 * 1024 * 1024;
96 if (log->flags & LOG_IGNORE_DISCARD)
100 u64 len = size > max_chunk ? max_chunk : size;
104 * Do this check first in case it is our first discard, that way
105 * if we return EOPNOTSUPP we will fall back to the 0 method
108 if (!(log->flags & LOG_DISCARD_NOT_SUPP))
109 ret = discard_range(log, start, len);
110 if (log->flags & LOG_DISCARD_NOT_SUPP)
111 ret = zero_range(log, start, len);
121 * @log: the log we are replaying.
122 * @entry: entry to be replayed.
124 * @return: 0 if we should replay the entry, > 0 if we should skip it.
126 * Should we skip the entry in our log or replay onto the replay device.
128 int log_should_skip(struct log *log, struct log_write_entry *entry)
130 u64 sector = le64_to_cpu(entry->sector);
131 u64 nr_sectors = le64_to_cpu(entry->nr_sectors);
135 if (sector + nr_sectors <= log->start_sector ||
136 sector > log->end_sector)
142 * @entry: entry to be replayed.
144 * @return: 1 if the entry is sane, 0 if it is invalid.
146 * Check if this is a sane log entry.
148 int log_entry_valid(struct log_write_entry *entry)
150 u64 flags = le64_to_cpu(entry->flags);
152 /* Suspect all zeroes entry */
153 if (!flags && !entry->nr_sectors)
155 /* Suspect non zero padded entry */
156 if (flags != LOG_MARK_FLAG && entry->data[0] != 0)
162 * @log: the log we are replaying.
163 * @entry: where we put the entry.
164 * @read_data: read the entry data as well, entry must be log->sectorsize sized
167 * @return: 0 if we replayed, 1 if we are at the end, -1 if there was an error.
169 * Replay the next entry in our log onto the replay device.
171 int log_replay_next_entry(struct log *log, struct log_write_entry *entry,
176 size_t read_size = read_data ? log->sectorsize :
177 sizeof(struct log_write_entry);
183 if (log->cur_entry >= log->nr_entries)
186 ret = read(log->logfd, entry, read_size);
187 if (ret != read_size) {
188 fprintf(stderr, "Error reading entry: %d\n", errno);
191 if (!log_entry_valid(entry)) {
192 fprintf(stderr, "Malformed entry @%llu\n",
193 log->cur_pos / log->sectorsize);
198 size = le64_to_cpu(entry->nr_sectors) * log->sectorsize;
199 if (read_size < log->sectorsize) {
200 log->cur_pos = lseek(log->logfd,
201 log->sectorsize - sizeof(struct log_write_entry), SEEK_CUR);
202 if (log->cur_pos == (off_t)-1) {
203 fprintf(stderr, "Error seeking in log: %d\n", errno);
207 log->cur_pos += read_size;
210 skip = log_should_skip(log, entry);
211 if (log_writes_verbose > 1 || (log_writes_verbose && !skip)) {
212 printf("%s %d@%llu: sector %llu, size %llu, flags %llu\n",
213 skip ? "skipping" : "replaying",
214 (int)log->cur_entry - 1, log->cur_pos / log->sectorsize,
215 (unsigned long long)le64_to_cpu(entry->sector),
216 (unsigned long long)size,
217 (unsigned long long)le64_to_cpu(entry->flags));
222 flags = le64_to_cpu(entry->flags);
223 if (flags & LOG_DISCARD_FLAG)
224 return log_discard(log, entry);
227 log->cur_pos = lseek(log->logfd, size, SEEK_CUR);
228 if (log->cur_pos == (off_t)-1) {
229 fprintf(stderr, "Error seeking in log: %d\n", errno);
237 fprintf(stderr, "Error allocating buffer %llu entry %llu\n", (unsigned long long)size, (unsigned long long)log->cur_entry - 1);
241 ret = read(log->logfd, buf, size);
243 fprintf(stderr, "Error reading data: %d\n", errno);
247 log->cur_pos += size;
249 offset = le64_to_cpu(entry->sector) * log->sectorsize;
250 ret = pwrite(log->replayfd, buf, size, offset);
253 fprintf(stderr, "Error writing data: %d\n", errno);
261 * @log: the log we are manipulating.
262 * @entry_num: the entry we want.
264 * Seek to the given entry in the log, starting at 0 and ending at
265 * log->nr_entries - 1.
267 int log_seek_entry(struct log *log, u64 entry_num)
271 if (entry_num >= log->nr_entries) {
272 fprintf(stderr, "Invalid entry number\n");
276 /* Skip the first sector containing the log super block */
277 log->cur_pos = lseek(log->logfd, log->sectorsize, SEEK_SET);
278 if (log->cur_pos == (off_t)-1) {
279 fprintf(stderr, "Error seeking in file: %d\n", errno);
284 for (i = 0; i < entry_num; i++) {
285 struct log_write_entry entry;
290 ret = read(log->logfd, &entry, sizeof(entry));
291 if (ret != sizeof(entry)) {
292 fprintf(stderr, "Error reading entry: %d\n", errno);
295 if (!log_entry_valid(&entry)) {
296 fprintf(stderr, "Malformed entry @%llu\n",
297 log->cur_pos / log->sectorsize);
300 if (log_writes_verbose > 1)
301 printf("seek entry %d@%llu: %llu, size %llu, flags %llu\n",
302 (int)i, log->cur_pos / log->sectorsize,
303 (unsigned long long)le64_to_cpu(entry.sector),
304 (unsigned long long)le64_to_cpu(entry.nr_sectors),
305 (unsigned long long)le64_to_cpu(entry.flags));
306 flags = le64_to_cpu(entry.flags);
307 seek_size = log->sectorsize - sizeof(entry);
308 if (!(flags & LOG_DISCARD_FLAG))
309 seek_size += le64_to_cpu(entry.nr_sectors) *
311 log->cur_pos = lseek(log->logfd, seek_size, SEEK_CUR);
312 if (log->cur_pos == (off_t)-1) {
313 fprintf(stderr, "Error seeking in file: %d\n", errno);
323 * @log: the log we are manipulating.
324 * @entry: the entry we read.
325 * @read_data: read the extra data for the entry, your entry must be
326 * log->sectorsize large.
328 * @return: 1 if we hit the end of the log, 0 we got the next entry, < 0 if
329 * there was an error.
331 * Seek to the next entry in the log.
333 int log_seek_next_entry(struct log *log, struct log_write_entry *entry,
336 size_t read_size = read_data ? log->sectorsize :
337 sizeof(struct log_write_entry);
341 if (log->cur_entry >= log->nr_entries)
344 ret = read(log->logfd, entry, read_size);
345 if (ret != read_size) {
346 fprintf(stderr, "Error reading entry: %d\n", errno);
349 if (!log_entry_valid(entry)) {
350 fprintf(stderr, "Malformed entry @%llu\n",
351 log->cur_pos / log->sectorsize);
356 if (read_size < log->sectorsize) {
357 log->cur_pos = lseek(log->logfd,
358 log->sectorsize - sizeof(struct log_write_entry), SEEK_CUR);
359 if (log->cur_pos == (off_t)-1) {
360 fprintf(stderr, "Error seeking in log: %d\n", errno);
364 log->cur_pos += read_size;
366 if (log_writes_verbose > 1)
367 printf("seek entry %d@%llu: %llu, size %llu, flags %llu\n",
368 (int)log->cur_entry - 1, log->cur_pos / log->sectorsize,
369 (unsigned long long)le64_to_cpu(entry->sector),
370 (unsigned long long)le64_to_cpu(entry->nr_sectors),
371 (unsigned long long)le64_to_cpu(entry->flags));
373 flags = le64_to_cpu(entry->flags);
374 read_size = le64_to_cpu(entry->nr_sectors) * log->sectorsize;
375 if (!read_size || (flags & LOG_DISCARD_FLAG))
378 log->cur_pos = lseek(log->logfd, read_size, SEEK_CUR);
379 if (log->cur_pos == (off_t)-1) {
380 fprintf(stderr, "Error seeking in log: %d\n", errno);
388 * @logfile: the file that contains the write log.
389 * @replayfile: the file/device to replay onto, can be NULL.
391 * Opens a logfile and makes sure it is valid and returns a struct log.
393 struct log *log_open(char *logfile, char *replayfile)
396 struct log_write_super super;
399 log = malloc(sizeof(struct log));
401 fprintf(stderr, "Couldn't alloc log\n");
407 log->logfd = open(logfile, O_RDONLY);
408 if (log->logfd < 0) {
409 fprintf(stderr, "Couldn't open log %s: %d\n", logfile,
416 log->replayfd = open(replayfile, O_WRONLY);
417 if (log->replayfd < 0) {
418 fprintf(stderr, "Couldn't open replay file %s: %d\n",
425 ret = read(log->logfd, &super, sizeof(struct log_write_super));
426 if (ret < sizeof(struct log_write_super)) {
427 fprintf(stderr, "Error reading super: %d\n", errno);
432 if (le64_to_cpu(super.magic) != WRITE_LOG_MAGIC) {
433 fprintf(stderr, "Magic doesn't match\n");
438 if (le64_to_cpu(super.version) != WRITE_LOG_VERSION) {
439 fprintf(stderr, "Version mismatch, wanted %d, have %d\n",
440 WRITE_LOG_VERSION, (int)le64_to_cpu(super.version));
445 log->sectorsize = le32_to_cpu(super.sectorsize);
446 log->nr_entries = le64_to_cpu(super.nr_entries);
447 log->max_zero_size = 128 * 1024 * 1024;
449 log->cur_pos = lseek(log->logfd, log->sectorsize - sizeof(super), SEEK_CUR);
450 if (log->cur_pos == (off_t) -1) {
451 fprintf(stderr, "Error seeking to first entry: %d\n", errno);