]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
create and check omap fsid
authorChendi.Xue <chendi.xue@intel.com>
Thu, 18 Feb 2016 02:36:45 +0000 (10:36 +0800)
committerBoris Ranto <branto@redhat.com>
Fri, 6 May 2016 11:44:18 +0000 (13:44 +0200)
1. write osd_uuid to omap dir when doing filestore mkfs
2. check if omap fsid matches osd fsid when doing filestore mount
   (if there is no osd_uuid under omap, assume this as match)

Signed-off-by: Chendi.Xue <chendi.xue@intel.com>
src/os/filestore/FileStore.cc

index ba5d877b7f4a005d73fadf334209f3c12747f455..c759f105c2949d110dd82331e26037df27969572 100644 (file)
@@ -790,7 +790,9 @@ int FileStore::mkfs()
 {
   int ret = 0;
   char fsid_fn[PATH_MAX];
+  char fsid_str[40];
   uuid_d old_fsid;
+  uuid_d old_omap_fsid;
 
   dout(1) << "mkfs in " << basedir << dendl;
   basedir_fd = ::open(basedir.c_str(), O_RDONLY);
@@ -822,7 +824,6 @@ int FileStore::mkfs()
       dout(1) << "mkfs using provided fsid " << fsid << dendl;
     }
 
-    char fsid_str[40];
     fsid.print(fsid_str);
     strcat(fsid_str, "\n");
     ret = ::ftruncate(fsid_fd, 0);
@@ -931,6 +932,52 @@ int FileStore::mkfs()
     ret = -1;
     goto close_fsid_fd;
   }
+  //create fsid under omap
+  // open+lock fsid
+  int omap_fsid_fd;
+  char omap_fsid_fn[PATH_MAX];
+  snprintf(omap_fsid_fn, sizeof(omap_fsid_fn), "%s/osd_uuid", omap_dir.c_str());
+  omap_fsid_fd = ::open(omap_fsid_fn, O_RDWR|O_CREAT, 0644);
+  if (omap_fsid_fd < 0) {
+    ret = -errno;
+    derr << "mkfs: failed to open " << omap_fsid_fn << ": " << cpp_strerror(ret) << dendl;
+    goto close_basedir_fd;
+  }
+
+  if (read_fsid(omap_fsid_fd, &old_omap_fsid) < 0 || old_omap_fsid.is_zero()) {
+    assert(!fsid.is_zero());
+    fsid.print(fsid_str);
+    strcat(fsid_str, "\n");
+    ret = ::ftruncate(omap_fsid_fd, 0);
+    if (ret < 0) {
+      ret = -errno;
+      derr << "mkfs: failed to truncate fsid: "
+          << cpp_strerror(ret) << dendl;
+      goto close_fsid_fd;
+    }
+    ret = safe_write(omap_fsid_fd, fsid_str, strlen(fsid_str));
+    if (ret < 0) {
+      derr << "mkfs: failed to write fsid: "
+          << cpp_strerror(ret) << dendl;
+      goto close_fsid_fd;
+    }
+    dout(10) << "mkfs: write success, fsid:" << fsid_str << ", ret:" << ret << dendl;
+    if (::fsync(omap_fsid_fd) < 0) {
+      ret = errno;
+      derr << "mkfs: close failed: can't write fsid: "
+          << cpp_strerror(ret) << dendl;
+      goto close_fsid_fd;
+    }
+    dout(10) << "mkfs omap fsid is " << fsid << dendl;
+  } else {
+    if (fsid != old_omap_fsid) {
+      derr << "FileStore::mkfs: " << omap_fsid_fn << " has existed omap fsid " << old_omap_fsid << " != expected osd fsid " << fsid << dendl;
+      ret = -EINVAL;
+      goto close_fsid_fd;
+    }
+    dout(1) << "FileStore::mkfs: omap fsid is already set to " << fsid << dendl;
+  }
+
   dout(1) << g_conf->filestore_omap_backend << " db exists/created" << dendl;
 
   // journal?
@@ -1284,6 +1331,7 @@ int FileStore::mount()
   int ret;
   char buf[PATH_MAX];
   uint64_t initial_op_seq;
+  uuid_d omap_fsid;
   set<string> cluster_snaps;
   CompatSet supported_compat_set = get_fs_supported_compat_set();
 
@@ -1514,6 +1562,38 @@ int FileStore::mount()
     ::unlink(nosnapfn);
   }
 
+  //check fsid with omap
+  // get omap fsid
+  int omap_fsid_fd;
+  char omap_fsid_buf[PATH_MAX];
+  struct ::stat omap_fsid_stat;
+  snprintf(omap_fsid_buf, sizeof(omap_fsid_buf), "%s/osd_uuid", omap_dir.c_str());
+  // if osd_uuid not exists, assume as this omap matchs corresponding osd
+  if (::stat(omap_fsid_buf, &omap_fsid_stat) != 0){
+    dout(10) << "Filestore::mount osd_uuid not found under omap, assume as matched." << dendl;
+  }else{
+    // if osd_uuid exists, compares osd_uuid with fsid
+    omap_fsid_fd = ::open(omap_fsid_buf, O_RDONLY, 0644);
+    if (omap_fsid_fd < 0) {
+        ret = -errno;
+        derr << "FileStore::mount: error opening '" << omap_fsid_buf << "': "
+        << cpp_strerror(ret) << dendl;
+        goto done;
+    }
+    ret = read_fsid(omap_fsid_fd, &omap_fsid);
+    if (ret < 0) {
+      derr << "FileStore::mount: error reading omap_fsid_fd" << ", omap_fsid = " << omap_fsid
+      << cpp_strerror(ret) << dendl;
+      goto close_fsid_fd;
+    }
+    if (fsid != omap_fsid) {
+      derr << "FileStore::mount: " << omap_fsid_buf << " has existed omap fsid " << omap_fsid << " != expected osd fsid " << fsid << dendl;
+      ret = -EINVAL;
+      goto close_fsid_fd;
+    }
+  }
+
+  dout(0) << "start omap initiation" << dendl;
   if (!(generic_flags & SKIP_MOUNT_OMAP)) {
     KeyValueDB * omap_store = KeyValueDB::create(g_ceph_context,
                                                 superblock.omap_backend,