]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
FileJournal: add testing methods to corrupt entries
authorSamuel Just <sam.just@inktank.com>
Tue, 12 Mar 2013 02:32:15 +0000 (19:32 -0700)
committerSamuel Just <sam.just@inktank.com>
Fri, 15 Mar 2013 18:21:07 +0000 (11:21 -0700)
Signed-off-by: Samuel Just <sam.just@inktank.com>
src/os/FileJournal.cc
src/os/FileJournal.h

index 887158ba36ba9fda05c5f63674d2213e202d7ae8..601f5a6e496ec8a1eb85bd6819b1098d1853d2ce 100644 (file)
@@ -1797,3 +1797,104 @@ void FileJournal::throttle()
   if (throttle_bytes.wait(g_conf->journal_queue_max_bytes))
     dout(2) << "throttle: waited for bytes" << dendl;
 }
+
+void FileJournal::get_header(
+  uint64_t wanted_seq,
+  off64_t *_pos,
+  entry_header_t *h)
+{
+  off64_t pos = header.start;
+  off64_t next_pos = pos;
+  bufferlist bl;
+  uint64_t seq = 0;
+  while (1) {
+    bl.clear();
+    pos = next_pos;
+    read_entry_result result = do_read_entry(
+      pos,
+      &next_pos,
+      &bl,
+      &seq,
+      0,
+      h);
+    if (result == FAILURE || result == MAYBE_CORRUPT)
+      assert(0);
+    if (seq == wanted_seq) {
+      if (_pos)
+       *_pos = pos;
+      return;
+    }
+  }
+  assert(0); // not reachable
+}
+
+void FileJournal::corrupt(
+  int wfd,
+  off64_t corrupt_at)
+{
+  if (corrupt_at >= header.max_size)
+    corrupt_at = corrupt_at + get_top() - header.max_size;
+
+#ifdef DARWIN
+    int64_t actual = ::lseek(fd, corrupt_at, SEEK_SET);
+#else
+    int64_t actual = ::lseek64(fd, corrupt_at, SEEK_SET);
+#endif
+    assert(actual == corrupt_at);
+
+    char buf[10];
+    int r = safe_read_exact(fd, buf, 1);
+    assert(r == 0);
+
+#ifdef DARWIN
+    actual = ::lseek(wfd, corrupt_at, SEEK_SET);
+#else
+    actual = ::lseek64(wfd, corrupt_at, SEEK_SET);
+#endif
+    assert(actual == corrupt_at);
+
+    buf[0]++;
+    r = safe_write(wfd, buf, 1);
+    assert(r == 0);
+}
+
+void FileJournal::corrupt_payload(
+  int wfd,
+  uint64_t seq)
+{
+  off64_t pos = 0;
+  entry_header_t h;
+  get_header(seq, &pos, &h);
+  off64_t corrupt_at =
+    pos + sizeof(entry_header_t) + h.pre_pad;
+  corrupt(wfd, corrupt_at);
+}
+
+
+void FileJournal::corrupt_footer_magic(
+  int wfd,
+  uint64_t seq)
+{
+  off64_t pos = 0;
+  entry_header_t h;
+  get_header(seq, &pos, &h);
+  off64_t corrupt_at =
+    pos + sizeof(entry_header_t) + h.pre_pad +
+    h.len + h.post_pad +
+    (reinterpret_cast<char*>(&h.magic2) - reinterpret_cast<char*>(&h));
+  corrupt(wfd, corrupt_at);
+}
+
+
+void FileJournal::corrupt_header_magic(
+  int wfd,
+  uint64_t seq)
+{
+  off64_t pos = 0;
+  entry_header_t h;
+  get_header(seq, &pos, &h);
+  off64_t corrupt_at =
+    pos +
+    (reinterpret_cast<char*>(&h.magic2) - reinterpret_cast<char*>(&h));
+  corrupt(wfd, corrupt_at);
+}
index fabaad65a4bf60d21c5a1109c6cdd3023bbda3ca..c5cc12de659808ffc4d58235dcbac7c37c0f3145 100644 (file)
@@ -429,6 +429,24 @@ private:
     uint64_t &last_seq) {
     return read_entry(bl, last_seq, 0);
   }
+
+  // Debug/Testing
+  void get_header(
+    uint64_t wanted_seq,
+    off64_t *_pos,
+    entry_header_t *h);
+  void corrupt(
+    int wfd,
+    off64_t corrupt_at);
+  void corrupt_payload(
+    int wfd,
+    uint64_t seq);
+  void corrupt_footer_magic(
+    int wfd,
+    uint64_t seq);
+  void corrupt_header_magic(
+    int wfd,
+    uint64_t seq);
 };
 
 WRITE_CLASS_ENCODER(FileJournal::header_t)