Create a new option --unexport=<path>" to be used to perform a
authorroot <root@rcn1.VSOFS1.COM>
Mon, 30 Mar 2009 00:24:52 +0000 (11:24 +1100)
committerroot <root@rcn1.VSOFS1.COM>
Mon, 30 Mar 2009 00:24:52 +0000 (11:24 +1100)
controlled shutdown and unexport of a remote-cache filesystem in the new two process model.

migrate/migrate.h
migrate/remote-cache.c
migrate/remote-cached.c

index c439adbb65dfa686389fb400eb078366dc3ef294..8946c28c9400f91e73f87c00f88fbc974303eef3 100644 (file)
@@ -20,6 +20,7 @@
 #define discard_const(ptr) ((void *)((intptr_t)(ptr)))
 
 extern struct tdb_context *migrate_db;
+extern const char *migrate_db_path;
 extern const char *cache;
 extern const char *remote;
 extern const char *export;
@@ -52,11 +53,13 @@ extern int log_fd;
 
 
 int start_migration_daemon(void);
+int open_migrate_db(void);
+pid_t fetch_migd_pid(void);
+
+const char *get_migrate_db_path(const char *export);
 
 #define RCVARDIR "/var/lib/remote-cache"
 
-#define MIGRATE_DB_FILE "/migrate.tdb"
-#define MIGRATE_DB RCVARDIR MIGRATE_DB_FILE
 /* entries for files that are requested to be migrated are stored in a circular
    list.
 */
index 2d9e6d718530675787f9eb3a4a8a33f39b15bb5c..ab361994907b5d5ae330f70e345c1ce9742e8ae5 100644 (file)
@@ -60,6 +60,8 @@ const char *remote = NULL;
    we re-export through our local nfs server to the clients */
 const char *export = NULL;
 
+const char *unexport = NULL;
+
 
 enum deb_lev debug_level = DEBUG_ERR;
 
@@ -126,6 +128,7 @@ static int file_migrate_small_file = 131072;
 static pid_t migpid = -1;
 
 struct tdb_context *migrate_db = NULL;
+const char *migrate_db_path = NULL;
 
 int debug_initialized = 0;
 int log_fd = -1;
@@ -144,6 +147,40 @@ struct fh_struct {
        struct write_fragment *fragments;
 };
 
+const char *get_migrate_db_path(const char *exp)
+{
+       char *str, *e, *ptr;
+
+       e = strdup(exp);
+       while((ptr = index(e, '/')) != NULL) {
+               *ptr = '_';
+       }
+
+       asprintf(&str, "%s/migratedb_%s.tdb", RCVARDIR, e);
+       free(e);
+       return str;
+}
+
+static void remote_cache_unexport(const char *unexp)
+{
+       pid_t migdpid;
+
+       export = unexp;
+       migdpid = fetch_migd_pid();
+       if (migdpid == 0) {
+               DEBUG(DEBUG_ERR,("Failed to fetch migd pid\n"));
+               exit(10);
+       }
+
+       if (kill(migdpid, SIGUSR2) != 0) {
+               DEBUG(DEBUG_ERR,("Failed to kill migd daemon\n"));
+               exit(0);
+       }
+       DEBUG(DEBUG_ERR,("Unexporting %s\n", unexp));
+       exit(0);
+}
+
+
 static int rate_limit_is_ok(void)
 {
        /* only print one message every 60 seconds */
@@ -1370,6 +1407,20 @@ finished:
        return ret;
 }
 
+int open_migrate_db(void)
+{
+       /* Open the migrate db database */
+       if (migrate_db == NULL) {
+               migrate_db_path = get_migrate_db_path(export);
+               DEBUG(DEBUG_INFO,("Initializing migration database %s\n", migrate_db_path));
+               migrate_db = tdb_open(migrate_db_path, 10000, TDB_CLEAR_IF_FIRST|TDB_NOSYNC, O_RDWR|O_CREAT, 0600);
+               if (migrate_db == NULL) {
+                       DEBUG(DEBUG_ERR,("Failed to initialize migration database.\n"));
+                       return -1;
+               }
+       }
+       return 0;
+}
 
 int mark_file_for_migration(const char *path)
 {
@@ -1379,16 +1430,7 @@ int mark_file_for_migration(const char *path)
        int ret = 0;
 
        DEBUG(DEBUG_DEBUG,("Marking file %s for migration\n", path));
-
-       /* Open the migrate db database */
-       if (migrate_db == NULL) {
-               DEBUG(DEBUG_INFO,("Initializing migration database %s\n", MIGRATE_DB));
-               migrate_db = tdb_open(MIGRATE_DB, 10000, TDB_CLEAR_IF_FIRST|TDB_NOSYNC, O_RDWR|O_CREAT, 0600);
-               if (migrate_db == NULL) {
-                       DEBUG(DEBUG_ERR,("Failed to initialize migration database.\n"));
-                       return -1;
-               }
-       }
+       open_migrate_db();
 
        /* get the header */
        hdrkey.dptr  = (uint8_t *)discard_const(MIGRATE_HEADER_KEY);
@@ -3057,6 +3099,7 @@ int main(int argc, const char *argv[])
        struct poptOption popt_options[] = {
                POPT_AUTOHELP
                { "export", 'e', POPT_ARG_STRING, &export, 0, "export directory", "directory" },
+               { "unexport", 'u', POPT_ARG_STRING, &unexport, 0, "directory to unexport", "directory" },
                { "cache", 'c', POPT_ARG_STRING, &cache, 0, "directory to store the cached data", "directory" },
                { "remote", 'r', POPT_ARG_STRING, &remote, 0, "directory where the remote nfs data is mounted", "directory" },
                { "max-dir-cache", 0, POPT_ARG_INT, &max_dir_cache, 0, "maximum number of seconds to cache a directory.", "integer" },
@@ -3102,6 +3145,11 @@ int main(int argc, const char *argv[])
                while (extra_argv[extra_argc]) extra_argc++;
        }
 
+       if (unexport != NULL) {
+               remote_cache_unexport(unexport);
+               exit(0);
+       }
+
        if (export == NULL) {
                fprintf(stderr, "You must specify --export\n");
                usage();
index e202d765b6267d7e5d1728770d6028874651853b..91d5b007435d56d49b76981e847b71e23e851622 100644 (file)
@@ -51,14 +51,7 @@ int migrate_files(void)
        int cache_fd = -1;
 
        /* Open the migrate db database */
-       if (migrate_db == NULL) {
-               DEBUG(DEBUG_INFO,("Initializing migration database %s in migrate daemon\n", MIGRATE_DB));
-               migrate_db = tdb_open(MIGRATE_DB, 10000, TDB_CLEAR_IF_FIRST|TDB_NOSYNC, O_RDWR|O_CREAT, 0600);
-               if (migrate_db == NULL) {
-                       DEBUG(DEBUG_ERR,("Failed to initialize migration database in migrate daemon.\n"));
-                       return -1;
-               }
-       }
+       open_migrate_db();
 
        mem_ctx = talloc_new(NULL);
 
@@ -231,6 +224,83 @@ static void migd_sigusr2_handler(struct event_context *ev, struct signal_event *
        _exit(10);
 }
 
+#define MIGD_PID_KEY "migd_pid"
+
+static int store_migd_pid(pid_t mypid)
+{
+       TALLOC_CTX *mem_ctx = NULL;
+       TDB_DATA migdkey, data;
+       int ret = 0;
+
+       /* Open the migrate db database */
+       open_migrate_db();
+
+       mem_ctx = talloc_new(NULL);
+
+       migdkey.dptr  = discard_const(MIGD_PID_KEY);
+       migdkey.dsize = strlen(MIGD_PID_KEY);
+
+       data.dptr  = (uint8_t *)&mypid;
+       data.dsize = sizeof(mypid);
+
+       ret = tdb_lockall(migrate_db);
+       if (ret != 0) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to lock migration db in migrate daemon\n"));
+               ret = -1;
+               goto finished;
+       }
+
+       ret = tdb_store(migrate_db, migdkey, data, TDB_REPLACE);
+       if (ret != 0 ){
+               DEBUG(DEBUG_ERR,(__location__ " Failed to store migd pid in migrate daemon\n"));
+               goto finished;
+       }
+
+finished:
+       tdb_unlockall(migrate_db);
+       talloc_free(mem_ctx);
+       return ret;
+}
+
+pid_t fetch_migd_pid(pid_t mypid)
+{
+       TALLOC_CTX *mem_ctx = NULL;
+       TDB_DATA migdkey, data;
+       int ret = 0;
+       pid_t migdpid = 0;
+
+       /* Open the migrate db database */
+       open_migrate_db();
+
+       mem_ctx = talloc_new(NULL);
+
+       migdkey.dptr  = discard_const(MIGD_PID_KEY);
+       migdkey.dsize = strlen(MIGD_PID_KEY);
+
+       ret = tdb_lockall(migrate_db);
+       if (ret != 0) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to lock migration db in migrate daemon\n"));
+               goto finished;
+       }
+
+       data = tdb_fetch(migrate_db, migdkey);
+       if (data.dptr == NULL ){
+               DEBUG(DEBUG_ERR,(__location__ " Failed to fetch migd pid from database\n"));
+               goto finished;
+       }
+       if (data.dsize != sizeof(migdpid)) {
+               DEBUG(DEBUG_ERR,("Wrong size for migd pid data\n"));
+               goto finished;
+       }
+       migdpid = *((pid_t *)data.dptr);
+       free(data.dptr);
+
+finished:
+       tdb_unlockall(migrate_db);
+       talloc_free(mem_ctx);
+       return migdpid;
+}
+
 int start_migration_daemon(void)
 {
        TALLOC_CTX *migd;
@@ -256,6 +326,13 @@ int start_migration_daemon(void)
                return -1;
        }
 
+       /* store out pid in the migration database */
+       if (store_migd_pid(mypid) != 0) {
+               DEBUG(DEBUG_CRIT,("Failed to store migd pid in database\n"));
+               return -1;
+       }
+
+
        ev = event_context_init(NULL);
 
        DEBUG(DEBUG_NOTICE,("Starting remote-cache migrate daemon, pid:%u\n", mypid));