Branch 'uidmap' - xlators/features

Kaleb KEITHLEY kkeithle at fedoraproject.org
Thu Jun 2 17:33:05 UTC 2011


 xlators/features/uidmap/src/rbmap.c  |  195 +++++++++++--------
 xlators/features/uidmap/src/uidmap.c |  355 +++++++++++++++++++++--------------
 2 files changed, 339 insertions(+), 211 deletions(-)

New commits:
commit 36263cdf41a45f5261af325e167fb10d521279c8
Author: Kaleb S. KEITHLEY <kkeithle at cloudfs-node01.kkeithle.redhat.com>
Date:   Thu Jun 2 13:28:50 2011 -0400

    no functional change, refactor to minimize long lines
    init returns -1 if mandatory options are not provided in config

diff --git a/xlators/features/uidmap/src/rbmap.c b/xlators/features/uidmap/src/rbmap.c
index 59a227a..7c2f810 100644
--- a/xlators/features/uidmap/src/rbmap.c
+++ b/xlators/features/uidmap/src/rbmap.c
@@ -87,18 +87,21 @@ rbmap_serialize(char *tenant)
                 union tuple *data;
                 lockfd = open(lkname, O_CREAT|O_WRONLY, 0644);
                 if (lockfd == -1) {
-                        gf_log("serialize", GF_LOG_CRITICAL, "open %s: %s", lkname, strerror(errno));
+                        gf_log("serialize", GF_LOG_CRITICAL,
+                               "open %s: %s", lkname, strerror(errno));
                         break;
                 }
                 status = flock(lockfd, LOCK_EX);
                 if (status == -1) {
-                        gf_log("serialize", GF_LOG_CRITICAL, "flock failed %s", strerror(errno));
+                        gf_log("serialize", GF_LOG_CRITICAL,
+                               "flock failed %s", strerror(errno));
                         break;
                 }
                 file = fopen(fname, "w");
                 if (file == NULL) {
                         (void) flock(lockfd, LOCK_UN);
-                        gf_log("serialize", GF_LOG_CRITICAL, "fopen failed %s", strerror(errno));
+                        gf_log("serialize", GF_LOG_CRITICAL,
+                               "fopen failed %s", strerror(errno));
                         break;
                 }
                 (void) fprintf(file, RBMAP_SIGNATURE);
@@ -132,6 +135,88 @@ rbmap_serialize(char *tenant)
         }
 }
 
+
+static int
+rbmap_readmapfile(FILE *file)
+{
+        char scratch[128];
+        char* sts;
+        while ((sts = fgets(scratch, sizeof scratch, file)) != (char *) EOF && sts != NULL) {
+                if (strncmp(scratch, RBMAP_UID_MAP_ENTRY, RBMAP_UID_MAP_ENTRY_LEN) == 0) {
+                        union tuple *map_data;
+                        union tuple *revmap_data =
+                                (union tuple *) malloc(sizeof(union tuple));
+                        if (revmap_data != NULL) {
+                                map_data = (union tuple *) malloc(sizeof(union tuple));
+                        } else {
+                                return -1;
+                        }
+                        if (map_data != NULL) {
+                                int num = sscanf(&scratch[RBMAP_UID_MAP_ENTRY_LEN+1],
+                                                 " %u %u", &map_data->u1, &map_data->u2);
+                                if (num == 2) {
+                                        rb_insert(rbmap.uid_map, map_data);
+                                        revmap_data->u1 = map_data->u2;
+                                        revmap_data->u2 = map_data->u1;
+                                        rb_insert(rbmap.uid_revmap, revmap_data);
+                                } else {
+                                        (void) free(map_data);
+                                        (void) free(revmap_data);
+                                        return -1;
+                                }
+                        } else {
+                                (void) free(revmap_data);
+                                return -1;
+                        }
+                } else if (strncmp(scratch, RBMAP_UID_LOW, RBMAP_UID_LOW_LEN) == 0) {
+                        rbmap.uid_low =
+                                (uid_t) strtoul(&scratch[RBMAP_UID_LOW_LEN+1], NULL, 10);
+                } else if (strncmp(scratch, RBMAP_UID_HIGH, RBMAP_UID_HIGH_LEN) == 0) {
+                        rbmap.uid_high =
+                                (uid_t) strtoul(&scratch[RBMAP_UID_HIGH_LEN+1], NULL, 10);
+                } else if (strncmp(scratch, RBMAP_UID_NEXT, RBMAP_UID_NEXT_LEN) == 0) {
+                        rbmap.uid_next =
+                                (uid_t) strtoul(&scratch[RBMAP_UID_NEXT_LEN+1], NULL, 10);
+                } else if (strncmp(scratch, RBMAP_GID_MAP_ENTRY, RBMAP_GID_MAP_ENTRY_LEN) == 0) {
+                        union tuple *map_data;
+                        union tuple *revmap_data =
+                                (union tuple *) malloc(sizeof(union tuple));
+                        if (revmap_data != NULL) {
+                                map_data = (union tuple *) malloc(sizeof(union tuple));
+                        } else {
+                                return -1;
+                        }
+                        if (map_data != NULL) {
+                                int num = sscanf(&scratch[RBMAP_GID_MAP_ENTRY_LEN+1],
+                                                 " %u %u", &map_data->g1, &map_data->g2);
+                                if (num == 2) {
+                                        rb_insert(rbmap.gid_map, map_data);
+                                        revmap_data->g1 = map_data->g2;
+                                        revmap_data->g2 = map_data->g1;
+                                        rb_insert(rbmap.gid_revmap, revmap_data);
+                                } else {
+                                        (void) free(map_data);
+                                        (void) free(revmap_data);
+                                        return -1;
+                                }
+                        } else {
+                                (void) free(revmap_data);
+                                return -1;
+                        }
+                } else if (strncmp(scratch, RBMAP_GID_LOW, RBMAP_GID_LOW_LEN) == 0) {
+                        rbmap.gid_low =
+                                (uid_t) strtoul(&scratch[RBMAP_GID_LOW_LEN+1], NULL, 10);
+                } else if (strncmp(scratch, RBMAP_GID_HIGH, RBMAP_GID_HIGH_LEN) == 0) {
+                        rbmap.gid_high =
+                                (uid_t) strtoul(&scratch[RBMAP_GID_HIGH_LEN+1], NULL, 10);
+                } else if (strncmp(scratch, RBMAP_GID_NEXT, RBMAP_GID_NEXT_LEN) == 0) {
+                        rbmap.gid_next =
+                                (uid_t) strtoul(&scratch[RBMAP_GID_NEXT_LEN+1], NULL, 10);
+                }
+        }
+        return 0;
+}
+
 static int
 rbmap_deserialize(char *tenant)
 {
@@ -147,90 +232,35 @@ rbmap_deserialize(char *tenant)
                 int status;
                 lockfd = open(lkname, O_CREAT|O_RDONLY, 0644);
                 if (lockfd == -1) {
-                        gf_log("deserialize", GF_LOG_CRITICAL, "open %s: %s", lkname, strerror(errno));
+                        gf_log("deserialize", GF_LOG_CRITICAL,
+                               "open %s: %s", lkname, strerror(errno));
                         break;
                 }
                 status = flock(lockfd, LOCK_EX);
                 if (status == -1) {
-                        gf_log("deserialize", GF_LOG_CRITICAL, "flock: %s", strerror(errno));
+                        gf_log("deserialize", GF_LOG_CRITICAL,
+                               "flock: %s", strerror(errno));
                         break;
                 }
                 file = fopen(fname, "r");
                 if (file == NULL) {
-                        gf_log("deserialize", GF_LOG_CRITICAL, "fopen failed: %s", strerror(errno));
+                        gf_log("deserialize", GF_LOG_CRITICAL,
+                               "fopen failed: %s", strerror(errno));
                         break;
                 }
                 /* read the first line (signature) of the file) */
                 if ((sts = fgets(scratch, sizeof scratch, file)) == (char *) EOF || sts == NULL) {
-                        gf_log("deserialize", GF_LOG_CRITICAL, "fgets signature failed: %s", strerror(errno));
+                        gf_log("deserialize", GF_LOG_CRITICAL,
+                               "fgets signature failed: %s", strerror(errno));
                         break;
                 }
                 if (strcmp(RBMAP_SIGNATURE, scratch) != 0) {
-                        gf_log("deserialize", GF_LOG_CRITICAL, " signature mismatch: %s", scratch);
+                        gf_log("deserialize", GF_LOG_CRITICAL,
+                               "signature mismatch: %s", scratch);
                         break;
                 }
-                while ((sts = fgets(scratch, sizeof scratch, file)) != (char *) EOF && sts != NULL) {
-                        if (strncmp(scratch, RBMAP_UID_MAP_ENTRY, RBMAP_UID_MAP_ENTRY_LEN) == 0) {
-                                union tuple *map_data;
-                                union tuple *revmap_data = (union tuple *) malloc(sizeof(union tuple));
-                                if (revmap_data != NULL) {
-                                        map_data = (union tuple *) malloc(sizeof(union tuple));
-                                } else {
-                                        return -1;
-                                }
-                                if (map_data != NULL) {
-                                        int num = sscanf(&scratch[RBMAP_UID_MAP_ENTRY_LEN+1], " %u %u", &map_data->u1, &map_data->u2);
-                                        if (num == 2) {
-                                                rb_insert(rbmap.uid_map, map_data);
-                                                revmap_data->u1 = map_data->u2;
-                                                revmap_data->u2 = map_data->u1;
-                                                rb_insert(rbmap.uid_revmap, revmap_data);
-                                        } else {
-                                                (void) free(map_data);
-                                                (void) free(revmap_data);
-                                                return -1;
-                                        }
-                                } else {
-                                        (void) free(revmap_data);
-                                        return -1;
-                                }
-                        } else if (strncmp(scratch, RBMAP_UID_LOW, RBMAP_UID_LOW_LEN) == 0) {
-                                rbmap.uid_low = (uid_t) strtoul(&scratch[RBMAP_UID_LOW_LEN+1], NULL, 10);
-                        } else if (strncmp(scratch, RBMAP_UID_HIGH, RBMAP_UID_HIGH_LEN) == 0) {
-                                rbmap.uid_high = (uid_t) strtoul(&scratch[RBMAP_UID_HIGH_LEN+1], NULL, 10);
-                        } else if (strncmp(scratch, RBMAP_UID_NEXT, RBMAP_UID_NEXT_LEN) == 0) {
-                                rbmap.uid_next = (uid_t) strtoul(&scratch[RBMAP_UID_NEXT_LEN+1], NULL, 10);
-                        } else if (strncmp(scratch, RBMAP_GID_MAP_ENTRY, RBMAP_GID_MAP_ENTRY_LEN) == 0) {
-                                union tuple *map_data;
-                                union tuple *revmap_data = (union tuple *) malloc(sizeof(union tuple));
-                                if (revmap_data != NULL) {
-                                        map_data = (union tuple *) malloc(sizeof(union tuple));
-                                } else {
-                                        return -1;
-                                }
-                                if (map_data != NULL) {
-                                        int num = sscanf(&scratch[RBMAP_GID_MAP_ENTRY_LEN+1], " %u %u", &map_data->g1, &map_data->g2);
-                                        if (num == 2) {
-                                                rb_insert(rbmap.gid_map, map_data);
-                                                revmap_data->g1 = map_data->g2;
-                                                revmap_data->g2 = map_data->g1;
-                                                rb_insert(rbmap.gid_revmap, revmap_data);
-                                        } else {
-                                                (void) free(map_data);
-                                                (void) free(revmap_data);
-                                                return -1;
-                                        }
-                                } else {
-                                        (void) free(revmap_data);
-                                        return -1;
-                                }
-                        } else if (strncmp(scratch, RBMAP_GID_LOW, RBMAP_GID_LOW_LEN) == 0) {
-                                rbmap.gid_low = (uid_t) strtoul(&scratch[RBMAP_GID_LOW_LEN+1], NULL, 10);
-                        } else if (strncmp(scratch, RBMAP_GID_HIGH, RBMAP_GID_HIGH_LEN) == 0) {
-                                rbmap.gid_high = (uid_t) strtoul(&scratch[RBMAP_GID_HIGH_LEN+1], NULL, 10);
-                        } else if (strncmp(scratch, RBMAP_GID_NEXT, RBMAP_GID_NEXT_LEN) == 0) {
-                                rbmap.gid_next = (uid_t) strtoul(&scratch[RBMAP_GID_NEXT_LEN+1], NULL, 10);
-                        }
+                if (rbmap_readmapfile(file) == -1) {
+                        return -1;
                 }
         } while (0);
         if (file != NULL) {
@@ -271,7 +301,8 @@ map(struct _call_stack_t *stack, struct _xlator *xlator)
                                         return -1;
                                 }
                                 new_map = (union tuple *) malloc(sizeof(union tuple));
-                                new_revmap = (union tuple *) malloc(sizeof(union tuple));
+                                new_revmap =
+                                        (union tuple *) malloc(sizeof(union tuple));
                                 if (new_map == NULL || new_revmap == NULL) {
                                         pthread_mutex_unlock(&rbmap.mtx);
                                         if (new_map != NULL) (void) free(new_map);
@@ -318,7 +349,8 @@ map(struct _call_stack_t *stack, struct _xlator *xlator)
                                         return -1;
                                 }
                                 new_map = (union tuple *) malloc(sizeof(union tuple));
-                                new_revmap = (union tuple *) malloc(sizeof(union tuple));
+                                new_revmap =
+                                        (union tuple *) malloc(sizeof(union tuple));
                                 if (new_map == NULL || new_revmap == NULL) {
                                         pthread_mutex_unlock(&rbmap.mtx);
                                         if (new_map != NULL) (void) free(new_map);
@@ -447,7 +479,18 @@ fini(struct _xlator *xlator)
 
 /* convenience symbols for debugging. */
 /* Why weak? Dunno, just because I guess; I don't think it matters  */
-extern int _rb_map(struct _call_stack_t *, struct _xlator *) __attribute__ ((weak, alias ("map")));
-extern void _rb_revmap(uid_t *, gid_t *) __attribute__ ((weak, alias ("revmap")));
-extern int32_t _rb_init(struct _xlator *, char *, uid_t, uid_t, gid_t, gid_t) __attribute__ ((weak, alias ("init")));
-extern void _rb_fini(struct _xlator *) __attribute__ ((weak, alias ("fini")));
+extern int
+_rb_map(struct _call_stack_t *, struct _xlator *)
+        __attribute__ ((weak, alias ("map")));
+
+extern void
+_rb_revmap(uid_t *, gid_t *)
+        __attribute__ ((weak, alias ("revmap")));
+
+extern int32_t
+_rb_init(struct _xlator *, char *, uid_t, uid_t, gid_t, gid_t)
+        __attribute__ ((weak, alias ("init")));
+
+extern void
+_rb_fini(struct _xlator *)
+        __attribute__ ((weak, alias ("fini")));
diff --git a/xlators/features/uidmap/src/uidmap.c b/xlators/features/uidmap/src/uidmap.c
index 4000d8b..18dbdaa 100644
--- a/xlators/features/uidmap/src/uidmap.c
+++ b/xlators/features/uidmap/src/uidmap.c
@@ -116,18 +116,21 @@ uidmap_serialize_default(char *tenant)
                 unsigned short i;
                 lockfd = open(lkname, O_CREAT|O_WRONLY, 0644);
                 if (lockfd == -1) {
-                        gf_log("serialize", GF_LOG_CRITICAL, "open %s: %s", lkname, strerror(errno));
+                        gf_log("serialize", GF_LOG_CRITICAL,
+                               "open %s: %s", lkname, strerror(errno));
                         break;
                 }
                 status = flock(lockfd, LOCK_EX);
                 if (status == -1) {
-                        gf_log("serialize", GF_LOG_CRITICAL, "flock failed %s", strerror(errno));
+                        gf_log("serialize", GF_LOG_CRITICAL,
+                               "flock failed %s", strerror(errno));
                         break;
                 }
                 file = fopen(fname, "w");
                 if (file == NULL) {
                         (void) flock(lockfd, LOCK_UN);
-                        gf_log("serialize", GF_LOG_CRITICAL, "fopen failed %s", strerror(errno));
+                        gf_log("serialize", GF_LOG_CRITICAL,
+                               "fopen failed %s", strerror(errno));
                         break;
                 }
                 (void) fprintf(file, CFS_SIGNATURE);
@@ -137,17 +140,23 @@ uidmap_serialize_default(char *tenant)
                                        uidmap_mapping.im_uid_map[i].me_client_uid,
                                        uidmap_mapping.im_uid_map[i].me_server_uid);
                 }
-                (void) fprintf(file, "%s %u\n", CFS_UID_LOW, uidmap_mapping.im_uid_low);
-                (void) fprintf(file, "%s %u\n", CFS_UID_HIGH, uidmap_mapping.im_uid_high);
-                (void) fprintf(file, "%s %u\n", CFS_UID_NEXT, uidmap_mapping.im_uid_next);
+                (void) fprintf(file, "%s %u\n",
+                               CFS_UID_LOW, uidmap_mapping.im_uid_low);
+                (void) fprintf(file, "%s %u\n",
+                               CFS_UID_HIGH, uidmap_mapping.im_uid_high);
+                (void) fprintf(file, "%s %u\n",
+                               CFS_UID_NEXT, uidmap_mapping.im_uid_next);
                 for (i = 0; i < uidmap_mapping.im_gid_map_len; i++) {
                         (void) fprintf(file, "%s %u %u\n", CFS_GID_MAP_ENTRY,
                                        uidmap_mapping.im_gid_map[i].me_client_gid,
                                        uidmap_mapping.im_gid_map[i].me_server_gid);
                 }
-                (void) fprintf(file, "%s %u\n", CFS_GID_LOW, uidmap_mapping.im_gid_low);
-                (void) fprintf(file, "%s %u\n", CFS_GID_HIGH, uidmap_mapping.im_gid_high);
-                (void) fprintf(file, "%s %u\n", CFS_GID_NEXT, uidmap_mapping.im_gid_next);
+                (void) fprintf(file, "%s %u\n", CFS_GID_LOW,
+                               uidmap_mapping.im_gid_low);
+                (void) fprintf(file, "%s %u\n", CFS_GID_HIGH,
+                               uidmap_mapping.im_gid_high);
+                (void) fprintf(file, "%s %u\n", CFS_GID_NEXT,
+                               uidmap_mapping.im_gid_next);
         } while (0);
         if (file != NULL) {
                 (void) fclose(file);
@@ -160,11 +169,98 @@ uidmap_serialize_default(char *tenant)
 
 
 static int
+uidmap_adduidentry(uid_t server_uid, uid_t client_uid)
+{
+        uid_map_entry_t *uid_map_entry = NULL;
+        uidmap_mapping.im_uid_map =
+                REALLOC(uidmap_mapping.im_uid_map,
+                        sizeof(uid_map_entry_t) * (uidmap_mapping.im_uid_map_len + 1));
+        if (uidmap_mapping.im_uid_map == NULL) {
+                return -1;
+        }
+        uid_map_entry =
+                &uidmap_mapping.im_uid_map[uidmap_mapping.im_uid_map_len++];
+        uid_map_entry->me_client_uid = client_uid;
+        uid_map_entry->me_server_uid = server_uid;
+        return 0;
+}
+
+
+static int
+uidmap_addgidentry(uid_t server_gid, uid_t client_gid)
+{
+        gid_map_entry_t *gid_map_entry = NULL;
+        uidmap_mapping.im_gid_map =
+                REALLOC(uidmap_mapping.im_gid_map,
+                        sizeof(gid_map_entry_t) * (uidmap_mapping.im_gid_map_len + 1));
+        if (uidmap_mapping.im_gid_map == NULL) {
+                return -1;
+        }
+        gid_map_entry =
+                &uidmap_mapping.im_gid_map[uidmap_mapping.im_gid_map_len++];
+        gid_map_entry->me_client_gid = client_gid;
+        gid_map_entry->me_server_gid = server_gid;
+        return 0;
+}
+
+
+static int
+uidmap_readmapfile(FILE *file)
+{
+        char* sts;
+        char scratch[128];
+        while ((sts = fgets(scratch, sizeof scratch, file)) != (char *) EOF && sts != NULL) {
+                if (strncmp(scratch, CFS_UID_MAP_ENTRY, CFS_UID_MAP_ENTRY_LEN) == 0) {
+                        uid_t server_uid;
+                        uid_t client_uid;
+                        int num = sscanf(&scratch[CFS_UID_MAP_ENTRY_LEN+1],
+                                         " %u %u", &client_uid, &server_uid);
+                        if (num == 2) {
+                                if (uidmap_adduidentry(server_uid, client_uid) == -1) {
+                                        return -1;
+                                }
+                        }
+                } else if (strncmp(scratch, CFS_UID_LOW, CFS_UID_LOW_LEN) == 0) {
+                        uidmap_mapping.im_uid_low =
+                                (uid_t) strtoul(&scratch[CFS_UID_LOW_LEN+1], NULL, 10);
+                } else if (strncmp(scratch, CFS_UID_HIGH, CFS_UID_HIGH_LEN) == 0) {
+                        uidmap_mapping.im_uid_high =
+                                (uid_t) strtoul(&scratch[CFS_UID_HIGH_LEN+1], NULL, 10);
+                } else if (strncmp(scratch, CFS_UID_NEXT, CFS_UID_NEXT_LEN) == 0) {
+                        uidmap_mapping.im_uid_next =
+                                (uid_t) strtoul(&scratch[CFS_UID_NEXT_LEN+1], NULL, 10);
+                } else if (strncmp(scratch, CFS_GID_MAP_ENTRY, CFS_GID_MAP_ENTRY_LEN) == 0) {
+                        gid_t server_gid;
+                        gid_t client_gid;
+                        int num = sscanf(&scratch[CFS_GID_MAP_ENTRY_LEN+1],
+                                         " %u %u", &client_gid, &server_gid);
+                        if (num == 2) {
+                                if (uidmap_addgidentry(server_gid, client_gid) == -1) {
+                                        return -1;
+                                }
+                        }
+                } else if (strncmp(scratch, CFS_GID_LOW, CFS_GID_LOW_LEN) == 0) {
+                        uidmap_mapping.im_gid_low =
+                                (uid_t) strtoul(&scratch[CFS_GID_LOW_LEN+1], NULL, 10);
+                } else if (strncmp(scratch, CFS_GID_HIGH, CFS_GID_HIGH_LEN) == 0) {
+                        uidmap_mapping.im_gid_high =
+                                (uid_t) strtoul(&scratch[CFS_GID_HIGH_LEN+1], NULL, 10);
+                } else if (strncmp(scratch, CFS_GID_NEXT, CFS_GID_NEXT_LEN) == 0) {
+                        uidmap_mapping.im_gid_next =
+                                (uid_t) strtoul(&scratch[CFS_GID_NEXT_LEN+1], NULL, 10);
+                }
+        }
+	return 0;
+}
+
+
+static int
 uidmap_deserialize_default(char *tenant)
 {
         char fname[128], lkname[128];
         FILE* file = NULL;
         int lockfd = -1;
+        int ret = 0;
         (void) snprintf(lkname, sizeof lkname, UIDMAP_MAP_LOCK_FILE, tenant);
         (void) snprintf(fname, sizeof fname, UIDMAP_MAP_FILE, tenant);
 
@@ -174,71 +270,34 @@ uidmap_deserialize_default(char *tenant)
                 int status;
                 lockfd = open(lkname, O_CREAT|O_RDONLY, 0644);
                 if (lockfd == -1) {
-                        gf_log("deserialize", GF_LOG_CRITICAL, "open %s: %s", lkname, strerror(errno));
+                        gf_log("deserialize", GF_LOG_CRITICAL,
+                               "open %s: %s", lkname, strerror(errno));
                         break;
                 }
                 status = flock(lockfd, LOCK_EX);
                 if (status == -1) {
-                        gf_log("deserialize", GF_LOG_CRITICAL, "flock: %s", strerror(errno));
+                        gf_log("deserialize", GF_LOG_CRITICAL,
+                               "flock: %s", strerror(errno));
                         break;
                 }
                 file = fopen(fname, "r");
                 if (file == NULL) {
-                        gf_log("deserialize", GF_LOG_CRITICAL, "fopen failed: %s", strerror(errno));
+                        gf_log("deserialize", GF_LOG_CRITICAL,
+                               "fopen failed: %s", strerror(errno));
                         break;
                 }
                 /* read the first line (signature) of the file) */
                 if ((sts = fgets(scratch, sizeof scratch, file)) == (char *) EOF || sts == NULL) {
-                        gf_log("deserialize", GF_LOG_CRITICAL, "fgets signature failed: %s", strerror(errno));
+                        gf_log("deserialize", GF_LOG_CRITICAL,
+                               "fgets signature failed: %s", strerror(errno));
                         break;
                 }
                 if (strcmp(CFS_SIGNATURE, scratch) != 0) {
-                        gf_log("deserialize", GF_LOG_CRITICAL, " signature mismatch: %s", scratch);
+                        gf_log("deserialize", GF_LOG_CRITICAL,
+                               "signature mismatch: %s", scratch);
                         break;
                 }
-                while ((sts = fgets(scratch, sizeof scratch, file)) != (char *) EOF && sts != NULL) {
-                        if (strncmp(scratch, CFS_UID_MAP_ENTRY, CFS_UID_MAP_ENTRY_LEN) == 0) {
-                                uid_t server_uid;
-                                uid_t client_uid;
-                                int num = sscanf(&scratch[CFS_UID_MAP_ENTRY_LEN+1], " %u %u", &client_uid, &server_uid);
-                                if (num == 2) {
-                                        uid_map_entry_t *uid_map_entry = NULL;
-                                        uidmap_mapping.im_uid_map = REALLOC(uidmap_mapping.im_uid_map, sizeof(uid_map_entry_t) * (uidmap_mapping.im_uid_map_len + 1));
-                                        if (uidmap_mapping.im_uid_map == NULL) {
-                                                return -1;
-                                        }
-                                        uid_map_entry = &uidmap_mapping.im_uid_map[uidmap_mapping.im_uid_map_len++];
-                                        uid_map_entry->me_client_uid = client_uid;
-                                        uid_map_entry->me_server_uid = server_uid;
-                                }
-                        } else if (strncmp(scratch, CFS_UID_LOW, CFS_UID_LOW_LEN) == 0) {
-                                uidmap_mapping.im_uid_low = (uid_t) strtoul(&scratch[CFS_UID_LOW_LEN+1], NULL, 10);
-                        } else if (strncmp(scratch, CFS_UID_HIGH, CFS_UID_HIGH_LEN) == 0) {
-                                uidmap_mapping.im_uid_high = (uid_t) strtoul(&scratch[CFS_UID_HIGH_LEN+1], NULL, 10);
-                        } else if (strncmp(scratch, CFS_UID_NEXT, CFS_UID_NEXT_LEN) == 0) {
-                                uidmap_mapping.im_uid_next = (uid_t) strtoul(&scratch[CFS_UID_NEXT_LEN+1], NULL, 10);
-                        } else if (strncmp(scratch, CFS_GID_MAP_ENTRY, CFS_GID_MAP_ENTRY_LEN) == 0) {
-                                gid_t server_gid;
-                                gid_t client_gid;
-                                int num = sscanf(&scratch[CFS_GID_MAP_ENTRY_LEN+1], " %u %u", &client_gid, &server_gid);
-                                if (num == 2) {
-                                        gid_map_entry_t *gid_map_entry = NULL;
-                                        uidmap_mapping.im_gid_map = REALLOC(uidmap_mapping.im_gid_map, sizeof(gid_map_entry_t) * (uidmap_mapping.im_gid_map_len + 1));
-                                        if (uidmap_mapping.im_gid_map == NULL) {
-                                                return -1;
-                                        }
-                                        gid_map_entry = &uidmap_mapping.im_gid_map[uidmap_mapping.im_gid_map_len++];
-                                        gid_map_entry->me_client_gid = client_gid;
-                                        gid_map_entry->me_server_gid = server_gid;
-                                }
-                        } else if (strncmp(scratch, CFS_GID_LOW, CFS_GID_LOW_LEN) == 0) {
-                                uidmap_mapping.im_gid_low = (uid_t) strtoul(&scratch[CFS_GID_LOW_LEN+1], NULL, 10);
-                        } else if (strncmp(scratch, CFS_GID_HIGH, CFS_GID_HIGH_LEN) == 0) {
-                                uidmap_mapping.im_gid_high = (uid_t) strtoul(&scratch[CFS_GID_HIGH_LEN+1], NULL, 10);
-                        } else if (strncmp(scratch, CFS_GID_NEXT, CFS_GID_NEXT_LEN) == 0) {
-                                uidmap_mapping.im_gid_next = (uid_t) strtoul(&scratch[CFS_GID_NEXT_LEN+1], NULL, 10);
-                        }
-                }
+                ret = uidmap_readmapfile(file);
         } while (0);
         if (file != NULL) {
                 (void) fclose(file);
@@ -247,7 +306,7 @@ uidmap_deserialize_default(char *tenant)
                 (void) flock(lockfd, LOCK_UN);
                 (void) close(lockfd);
         }
-        return 0;
+        return ret;
 }
 
 
@@ -342,7 +401,6 @@ uidmap_map_default(struct _call_stack_t *stack, struct _xlator *xlator)
         if (do_persist > 0)
                 uidmap_serialize_default(tenant);
         pthread_mutex_unlock(&uidmap_mapping.im_mtx);
-
         return 0;
 }
 
@@ -1739,20 +1797,60 @@ uidmap_lk(call_frame_t *frame, xlator_t *this, fd_t *fd,
         return 0;
 }
 
+static init_fn
+uidmap_loadsharedlib(char* xltrname, char *libname)
+{
+        init_fn plugin_init  = NULL;
+        void *handle         = NULL;
+
+        gf_log(xltrname, CFS_LOG_LEVEL, "loading: %s", libname);
+        if ((handle = dlopen(libname, RTLD_NOW|RTLD_NODELETE)) != NULL) {
+                do {
+                        if ((uidmap_map = (map_fn) dlsym(handle, "map")) == NULL) {
+                                gf_log(xltrname, GF_LOG_CRITICAL,
+                                       "plugin missing map: %s", dlerror());
+                                break;
+                        }
+
+                        if ((uidmap_revmap = (revmap_fn) dlsym(handle, "revmap")) == NULL) {
+                                gf_log(xltrname, GF_LOG_CRITICAL,
+                                       "plugin missing revmap: %s", dlerror());
+                                break;
+                        }
+
+                        if ((uidmap_plugin_fini = (fini_fn) dlsym(handle, "fini")) == NULL) {
+                                gf_log(xltrname, GF_LOG_CRITICAL,
+                                       "plugin missing fini: %s", dlerror());
+                                break;
+                        }
+
+                        if ((plugin_init = (init_fn) dlsym(handle, "init")) == NULL) {
+                                gf_log(xltrname, GF_LOG_CRITICAL,
+                                       "plugin missing init: %s", dlerror());
+                        }
+                } while (0);
+
+                if (dlclose(handle)) {
+                        gf_log(xltrname, GF_LOG_ERROR,
+                               "dlclose plugin: %s", dlerror());
+                }
+        }
+	return plugin_init;
+}
 
 int32_t
 init(xlator_t *this)
 {
         char *uid_range = NULL, *gid_range = NULL;
-        char *plugin         = NULL;
-        void *handle         = NULL;
         char *root_squash    = NULL;
+        char *plugin         = NULL;
         char *tenant         = NULL;
+        init_fn plugin_init  = NULL;
         uid_t uidmap_low_uid = CFS_UID_LOW_DEFAULT;
         uid_t uidmap_hi_uid  = CFS_UID_HIGH_DEFAULT;
         gid_t uidmap_low_gid = CFS_UID_LOW_DEFAULT;
         gid_t uidmap_hi_gid  = CFS_UID_HIGH_DEFAULT;
-        init_fn plugin_init  = NULL;
+        data_t *data;
 
         if (!this)
                 return -1;
@@ -1769,92 +1867,79 @@ init(xlator_t *this)
                        "dangling volume. check volfile ");
         }
 
-        uid_range = data_to_str(dict_get(this->options, "uid-range"));
-        if (uid_range != NULL && *uid_range != 0) {
-                uid_t low, hi;
-                if (sscanf(uid_range, "%u-%u", &low, &hi) == 2) {
-                        uidmap_low_uid = low; uidmap_hi_uid = hi;
-                        uidmap_mapping.im_uid_low = uidmap_mapping.im_uid_next = low;
-                        uidmap_mapping.im_uid_high = hi;
-                } else {
-                        gf_log(this->name, GF_LOG_ERROR,
-                               "invalid uid-range in config");
+        if ((data = dict_get(this->options, "uid-range")) != NULL) {
+                uid_range = data_to_str(data);
+                if (uid_range != NULL && *uid_range != 0) {
+                        uid_t low, hi;
+                        if (sscanf(uid_range, "%u-%u", &low, &hi) == 2) {
+                                uidmap_low_uid = low; uidmap_hi_uid = hi;
+                                uidmap_mapping.im_uid_low =
+                                        uidmap_mapping.im_uid_next = low;
+                                uidmap_mapping.im_uid_high = hi;
+                        } else {
+                                gf_log(this->name, GF_LOG_ERROR,
+                                       "invalid uid-range in config");
+                        }
                 }
+        } else {
+                gf_log(this->name, GF_LOG_ERROR,
+                       "you must specify the uid-range in the config");
+                return -1;
         }
 
-        gid_range = data_to_str(dict_get(this->options, "gid-range"));
-        if (gid_range != NULL && *gid_range != 0) {
-                gid_t low, hi;
-                if (sscanf(gid_range, "%u-%u", &low, &hi) == 2) {
-                        uidmap_low_gid = low; uidmap_hi_gid = hi;
-                        uidmap_mapping.im_gid_low = uidmap_mapping.im_gid_next = low;
-                        uidmap_mapping.im_gid_high = hi;
-                } else {
-                        gf_log(this->name, GF_LOG_ERROR,
-                               "invalid gid-range in config");
+        if ((data = dict_get(this->options, "gid-range")) != NULL) {
+                gid_range = data_to_str(data);
+                if (gid_range != NULL && *gid_range != 0) {
+                        gid_t low, hi;
+                        if (sscanf(gid_range, "%u-%u", &low, &hi) == 2) {
+                                uidmap_low_gid = low; uidmap_hi_gid = hi;
+                                uidmap_mapping.im_gid_low =
+                                        uidmap_mapping.im_gid_next = low;
+                                uidmap_mapping.im_gid_high = hi;
+                        } else {
+                                gf_log(this->name, GF_LOG_ERROR,
+                                       "invalid gid-range in config");
+                        }
                 }
+        } else {
+                gf_log(this->name, GF_LOG_ERROR,
+                       "you must specify the gid-range in the config");
+                return -1;
         }
 
-        root_squash = data_to_str(dict_get(this->options, "root-squash"));
-        if (root_squash != NULL && *root_squash != 0) {
-                char* ptr = root_squash;
-                for (; *ptr != 0; ptr++)
-                        *ptr = tolower(*ptr);
-                if (strcmp(root_squash, "yes") == 0)
-                        uidmap_root_squash = 1;
+        if ((data = dict_get(this->options, "root-squash")) != NULL) {
+                root_squash = data_to_str(data);
+                if (root_squash != NULL && *root_squash != 0) {
+                        char* ptr = root_squash;
+                        for (; *ptr != 0; ptr++)
+                                *ptr = tolower(*ptr);
+                        if (strcmp(root_squash, "yes") == 0)
+                                uidmap_root_squash = 1;
+                }
         }
 
-        tenant = data_to_str(dict_get(this->options, "tenant"));
-        if (tenant != NULL) {
-            uidmap_tenant = strdup(tenant);
+        if ((data = dict_get(this->options, "tenant")) != NULL) {
+                tenant = data_to_str(data);
+                if (tenant != NULL) {
+                    uidmap_tenant = strdup(tenant);
+                }
+        } else {
+                gf_log(this->name, GF_LOG_ERROR,
+                       "you must specify a tenant name in the config");
+                return -1;
         }
 
-        plugin = data_to_str(dict_get(this->options, "uidmap-plugin"));
-
-        if (plugin != NULL && *plugin != 0) {
-                gf_log(this->name, CFS_LOG_LEVEL, "loading: %s", plugin);
-                handle = dlopen(plugin, RTLD_NOW|RTLD_NODELETE);
-                if (handle) {
-                        do {
-                                uidmap_map = (map_fn) dlsym(handle, "map");
-                                if (uidmap_map == NULL) {
-                                        gf_log(this->name, GF_LOG_CRITICAL,
-                                               "plugin missing map: %s", dlerror());
-                                        break;
-                                }
-
-                                uidmap_revmap = (revmap_fn) dlsym(handle, "revmap");
-                                if (uidmap_revmap == NULL) {
-                                        gf_log(this->name, GF_LOG_CRITICAL,
-                                               "plugin missing revmap: %s", dlerror());
-                                        break;
-                                }
-
-                                uidmap_plugin_fini = (fini_fn) dlsym(handle, "fini");
-                                if (uidmap_plugin_fini == NULL) {
-                                        gf_log(this->name, GF_LOG_CRITICAL,
-                                               "plugin missing fini: %s", dlerror());
-                                        break;
-                                }
-
-                                plugin_init = (init_fn) dlsym(handle, "init");
-                                if (plugin_init == NULL) {
-                                        gf_log(this->name, GF_LOG_CRITICAL,
-                                               "plugin missing init: %s", dlerror());
-                                } else {
-                                    if ((*plugin_init)(this, uidmap_tenant,
-                                                       uidmap_low_uid, uidmap_hi_uid,
-                                                       uidmap_low_gid, uidmap_hi_gid,
-                                                       uidmap_root_squash) != 0) {
+        if ((data = dict_get(this->options, "uidmap-plugin")) != NULL) {
+                plugin = data_to_str(data);
+                if (plugin != NULL && *plugin != 0) {
+                        if ((plugin_init = uidmap_loadsharedlib(this->name, plugin)) != NULL) {
+                                if ((*plugin_init)(this, uidmap_tenant,
+                                                   uidmap_low_uid, uidmap_hi_uid,
+                                                   uidmap_low_gid, uidmap_hi_gid,
+                                                   uidmap_root_squash) != 0) {
                                         gf_log(this->name, GF_LOG_CRITICAL,
                                                "plugin init error");
-                                    }
                                 }
-                        } while (0);
-
-                        if (dlclose(handle)) {
-                                gf_log(this->name, GF_LOG_ERROR,
-                                       "dlclose plugin: %s", dlerror());
                         }
                 }
         }




More information about the cloudfs-devel mailing list