ReadOnly: add a new control to activate readonly lock capability for a database.
[sahlberg/ctdb.git] / server / ctdb_ltdb_server.c
index bf8ecdd0e16ec3b280ee0d3423d2aca8543f274d..dc93c3f262beec1f3657a5d80814e785c0bcbef0 100644 (file)
@@ -723,6 +723,44 @@ int32_t ctdb_control_db_get_health(struct ctdb_context *ctdb,
        return 0;
 }
 
+
+int ctdb_set_db_readonly(struct ctdb_context *ctdb, struct ctdb_db_context *ctdb_db)
+{
+       char *ropath;
+
+       DEBUG(DEBUG_ERR,("XXX set db readonly %s\n", ctdb_db->db_name));
+
+       if (ctdb_db->readonly) {
+               return 0;
+       }
+
+       if (ctdb_db->persistent) {
+               DEBUG(DEBUG_ERR,("Trying to set persistent database with readonly property\n"));
+               return -1;
+       }
+
+       ropath = talloc_asprintf(ctdb_db, "%s.RO", ctdb_db->db_path);
+       if (ropath == NULL) {
+               DEBUG(DEBUG_CRIT,("Failed to asprintf the tracking database\n"));
+               return -1;
+       }
+       ctdb_db->rottdb = tdb_open(ropath, 
+                             ctdb->tunable.database_hash_size, 
+                             TDB_NOLOCK|TDB_CLEAR_IF_FIRST|TDB_NOSYNC,
+                             O_CREAT|O_RDWR, 0);
+       if (ctdb_db->rottdb == NULL) {
+               DEBUG(DEBUG_CRIT,("Failed to open/create the tracking database '%s'\n", ropath));
+               talloc_free(ropath);
+               return -1;
+       }
+
+       DEBUG(DEBUG_NOTICE,("OPENED tracking database : '%s'\n", ropath));
+
+       ctdb_db->readonly = true;
+       talloc_free(ropath);
+       return 0;
+}
+
 /*
   attach to a database, handling both persistent and non-persistent databases
   return 0 on success, -1 on failure
@@ -937,35 +975,6 @@ again:
                return -1;
        }
 
-       /* Assume all non-persistent databases support read only delegations */
-       if (!ctdb_db->persistent) {
-               ctdb_db->readonly = true;
-       }
-
-       if (ctdb_db->readonly) {
-               char *ropath;
-
-               ropath = talloc_asprintf(ctdb_db, "%s.RO", ctdb_db->db_path);
-               if (ropath == NULL) {
-                       DEBUG(DEBUG_CRIT,("Failed to asprintf the tracking database\n"));
-                       ctdb_db->readonly = false;
-                       talloc_free(ctdb_db);
-                       return -1;
-               }
-               ctdb_db->rottdb = tdb_open(ropath, 
-                                     ctdb->tunable.database_hash_size, 
-                                     TDB_NOLOCK|TDB_CLEAR_IF_FIRST|TDB_NOSYNC,
-                                     O_CREAT|O_RDWR, 0);
-               if (ctdb_db->rottdb == NULL) {
-                       DEBUG(DEBUG_CRIT,("Failed to open/create the tracking database '%s'\n", ropath));
-                       ctdb_db->readonly = false;
-                       talloc_free(ctdb_db);
-                       return -1;
-               }
-               DEBUG(DEBUG_NOTICE,("OPENED tracking database : '%s'\n", ropath));
-       }
-
-
        DLIST_ADD(ctdb->db_list, ctdb_db);
 
        /* setting this can help some high churn databases */