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);
124 * @log: the log we are replaying.
125 * @entry: entry to be replayed.
127 * @return: 0 if we should replay the entry, > 0 if we should skip it.
129 * Should we skip the entry in our log or replay onto the replay device.
131 int log_should_skip(struct log *log, struct log_write_entry *entry)
133 u64 sector = le64_to_cpu(entry->sector);
134 u64 nr_sectors = le64_to_cpu(entry->nr_sectors);
138 if (sector + nr_sectors <= log->start_sector ||
139 sector > log->end_sector)
145 * @entry: entry to be replayed.
147 * @return: 1 if the entry is sane, 0 if it is invalid.
149 * Check if this is a sane log entry.
151 int log_entry_valid(struct log_write_entry *entry)
153 u64 flags = le64_to_cpu(entry->flags);
155 /* Suspect all zeroes entry */
156 if (!flags && !entry->nr_sectors)
158 /* Suspect non zero padded entry */
159 if (flags != LOG_MARK_FLAG && entry->data[0] != 0)
165 * @log: the log we are replaying.
166 * @entry: where we put the entry.
167 * @read_data: read the entry data as well, entry must be log->sectorsize sized
170 * @return: 0 if we replayed, 1 if we are at the end, -1 if there was an error.
172 * Replay the next entry in our log onto the replay device.
174 int log_replay_next_entry(struct log *log, struct log_write_entry *entry,
179 size_t read_size = read_data ? log->sectorsize :
180 sizeof(struct log_write_entry);
186 if (log->cur_entry >= log->nr_entries)
189 ret = read(log->logfd, entry, read_size);
190 if (ret != read_size) {
191 fprintf(stderr, "Error reading entry: %d\n", errno);
194 if (!log_entry_valid(entry)) {
195 fprintf(stderr, "Malformed entry @%llu\n",
196 log->cur_pos / log->sectorsize);
201 size = le64_to_cpu(entry->nr_sectors) * log->sectorsize;
202 if (read_size < log->sectorsize) {
203 log->cur_pos = lseek(log->logfd,
204 log->sectorsize - sizeof(struct log_write_entry), SEEK_CUR);
205 if (log->cur_pos == (off_t)-1) {
206 fprintf(stderr, "Error seeking in log: %d\n", errno);
210 log->cur_pos += read_size;
213 skip = log_should_skip(log, entry);
214 if (log_writes_verbose > 1 || (log_writes_verbose && !skip)) {
215 printf("%s %d@%llu: sector %llu, size %llu, flags %llu\n",
216 skip ? "skipping" : "replaying",
217 (int)log->cur_entry - 1, log->cur_pos / log->sectorsize,
218 (unsigned long long)le64_to_cpu(entry->sector),
219 (unsigned long long)size,
220 (unsigned long long)le64_to_cpu(entry->flags));
225 flags = le64_to_cpu(entry->flags);
226 if (flags & LOG_DISCARD_FLAG)
227 return log_discard(log, entry);
230 log->cur_pos = lseek(log->logfd, size, SEEK_CUR);
231 if (log->cur_pos == (off_t)-1) {
232 fprintf(stderr, "Error seeking in log: %d\n", errno);
240 fprintf(stderr, "Error allocating buffer %llu entry %llu\n", (unsigned long long)size, (unsigned long long)log->cur_entry - 1);
244 ret = read(log->logfd, buf, size);
246 fprintf(stderr, "Error reading data: %d\n", errno);
250 log->cur_pos += size;
252 offset = le64_to_cpu(entry->sector) * log->sectorsize;
253 ret = pwrite(log->replayfd, buf, size, offset);
256 fprintf(stderr, "Error writing data: %d\n", errno);
264 * @log: the log we are manipulating.
265 * @entry_num: the entry we want.
267 * Seek to the given entry in the log, starting at 0 and ending at
268 * log->nr_entries - 1.
270 int log_seek_entry(struct log *log, u64 entry_num)
274 if (entry_num >= log->nr_entries) {
275 fprintf(stderr, "Invalid entry number\n");
279 /* Skip the first sector containing the log super block */
280 log->cur_pos = lseek(log->logfd, log->sectorsize, SEEK_SET);
281 if (log->cur_pos == (off_t)-1) {
282 fprintf(stderr, "Error seeking in file: %d\n", errno);
287 for (i = 0; i < entry_num; i++) {
288 struct log_write_entry entry;
293 ret = read(log->logfd, &entry, sizeof(entry));
294 if (ret != sizeof(entry)) {
295 fprintf(stderr, "Error reading entry: %d\n", errno);
298 if (!log_entry_valid(&entry)) {
299 fprintf(stderr, "Malformed entry @%llu\n",
300 log->cur_pos / log->sectorsize);
303 if (log_writes_verbose > 1)
304 printf("seek entry %d@%llu: %llu, size %llu, flags %llu\n",
305 (int)i, log->cur_pos / log->sectorsize,
306 (unsigned long long)le64_to_cpu(entry.sector),
307 (unsigned long long)le64_to_cpu(entry.nr_sectors),
308 (unsigned long long)le64_to_cpu(entry.flags));
309 flags = le64_to_cpu(entry.flags);
310 seek_size = log->sectorsize - sizeof(entry);
311 if (!(flags & LOG_DISCARD_FLAG))
312 seek_size += le64_to_cpu(entry.nr_sectors) *
314 log->cur_pos = lseek(log->logfd, seek_size, SEEK_CUR);
315 if (log->cur_pos == (off_t)-1) {
316 fprintf(stderr, "Error seeking in file: %d\n", errno);
326 * @log: the log we are manipulating.
327 * @entry: the entry we read.
328 * @read_data: read the extra data for the entry, your entry must be
329 * log->sectorsize large.
331 * @return: 1 if we hit the end of the log, 0 we got the next entry, < 0 if
332 * there was an error.
334 * Seek to the next entry in the log.
336 int log_seek_next_entry(struct log *log, struct log_write_entry *entry,
339 size_t read_size = read_data ? log->sectorsize :
340 sizeof(struct log_write_entry);
344 if (log->cur_entry >= log->nr_entries)
347 ret = read(log->logfd, entry, read_size);
348 if (ret != read_size) {
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);
359 if (read_size < log->sectorsize) {
360 log->cur_pos = lseek(log->logfd,
361 log->sectorsize - sizeof(struct log_write_entry), SEEK_CUR);
362 if (log->cur_pos == (off_t)-1) {
363 fprintf(stderr, "Error seeking in log: %d\n", errno);
367 log->cur_pos += read_size;
369 if (log_writes_verbose > 1)
370 printf("seek entry %d@%llu: %llu, size %llu, flags %llu\n",
371 (int)log->cur_entry - 1, log->cur_pos / log->sectorsize,
372 (unsigned long long)le64_to_cpu(entry->sector),
373 (unsigned long long)le64_to_cpu(entry->nr_sectors),
374 (unsigned long long)le64_to_cpu(entry->flags));
376 flags = le64_to_cpu(entry->flags);
377 read_size = le64_to_cpu(entry->nr_sectors) * log->sectorsize;
378 if (!read_size || (flags & LOG_DISCARD_FLAG))
381 log->cur_pos = lseek(log->logfd, read_size, SEEK_CUR);
382 if (log->cur_pos == (off_t)-1) {
383 fprintf(stderr, "Error seeking in log: %d\n", errno);
391 * @logfile: the file that contains the write log.
392 * @replayfile: the file/device to replay onto, can be NULL.
394 * Opens a logfile and makes sure it is valid and returns a struct log.
396 struct log *log_open(char *logfile, char *replayfile)
399 struct log_write_super super;
402 log = malloc(sizeof(struct log));
404 fprintf(stderr, "Couldn't alloc log\n");
410 log->logfd = open(logfile, O_RDONLY);
411 if (log->logfd < 0) {
412 fprintf(stderr, "Couldn't open log %s: %d\n", logfile,
419 log->replayfd = open(replayfile, O_WRONLY);
420 if (log->replayfd < 0) {
421 fprintf(stderr, "Couldn't open replay file %s: %d\n",
428 ret = read(log->logfd, &super, sizeof(struct log_write_super));
429 if (ret < sizeof(struct log_write_super)) {
430 fprintf(stderr, "Error reading super: %d\n", errno);
435 if (le64_to_cpu(super.magic) != WRITE_LOG_MAGIC) {
436 fprintf(stderr, "Magic doesn't match\n");
441 if (le64_to_cpu(super.version) != WRITE_LOG_VERSION) {
442 fprintf(stderr, "Version mismatch, wanted %d, have %d\n",
443 WRITE_LOG_VERSION, (int)le64_to_cpu(super.version));
448 log->sectorsize = le32_to_cpu(super.sectorsize);
449 log->nr_entries = le64_to_cpu(super.nr_entries);
450 log->max_zero_size = 128 * 1024 * 1024;
452 log->cur_pos = lseek(log->logfd, log->sectorsize - sizeof(super), SEEK_CUR);
453 if (log->cur_pos == (off_t) -1) {
454 fprintf(stderr, "Error seeking to first entry: %d\n", errno);