Patchwork [BUG:2847,2/2] glusterd: Checked brick path length in 'staging'.

login
register
Submitter Krishnan P
Date 2011-05-03 10:34:57
Message ID <1304418897-6141-1-git-send-email-kp@gluster.com>
Download mbox | patch
Permalink /patch/7127/
State Accepted
Headers show

Comments

Krishnan P - 2011-05-03 10:34:57
From: Krishnan Parthasarathi <kp@gluster.com>

Added checks for brick path length in 'staging' of add/replace
brick and create_volume subcommands.

Signed-off-by: Krishnan Parthasarathi <kp@gluster.com>
---
 xlators/mgmt/glusterd/src/glusterd-op-sm.c  |   35 +++++++++++++++++++++++++++
 xlators/mgmt/glusterd/src/glusterd-store.c  |   35 +++++++++++++++++++++++++++
 xlators/mgmt/glusterd/src/glusterd-store.h  |    2 +
 xlators/mgmt/glusterd/src/glusterd-volgen.c |   34 ++++++++++++++++++++++++++
 xlators/mgmt/glusterd/src/glusterd-volgen.h |    3 +-
 5 files changed, 108 insertions(+), 1 deletions(-)

Patch

diff --git a/xlators/mgmt/glusterd/src/glusterd-op-sm.c b/xlators/mgmt/glusterd/src/glusterd-op-sm.c
index b89e9b6..5954a7b 100644
--- a/xlators/mgmt/glusterd/src/glusterd-op-sm.c
+++ b/xlators/mgmt/glusterd/src/glusterd-op-sm.c
@@ -355,6 +355,18 @@  glusterd_op_stage_create_volume (dict_t *dict, char **op_errstr)
                 i++;
                 brick= strtok_r (brick_list, " \n", &tmpptr);
                 brick_list = tmpptr;
+
+                if (!glusterd_store_is_valid_brickpath (volname, brick) ||
+                        !glusterd_is_valid_volfpath (volname, brick)) {
+                        snprintf (msg, sizeof (msg), "brick path %s is too "
+                                  "long.", brick);
+                        gf_log ("", GF_LOG_ERROR, "%s", msg);
+                        *op_errstr = gf_strdup (msg);
+
+                        ret = -1;
+                        goto out;
+                }
+
                 ret = glusterd_brickinfo_from_brick (brick, &brick_info);
                 if (ret)
                         goto out;
@@ -691,6 +703,18 @@  glusterd_op_stage_add_brick (dict_t *dict, char **op_errstr)
 
 
         while ( i < count) {
+                if (!glusterd_store_is_valid_brickpath (volname, brick) ||
+                        !glusterd_is_valid_volfpath (volname, brick)) {
+                        snprintf (msg, sizeof (msg), "brick path %s is too "
+                                  "long.", brick);
+                        gf_log ("", GF_LOG_ERROR, "%s", msg);
+                        *op_errstr = gf_strdup (msg);
+
+                        ret = -1;
+                        goto out;
+
+                }
+
                 ret = glusterd_volume_brickinfo_get_by_brick (brick, volinfo,
                                                               &brickinfo);
                 if (!ret) {
@@ -888,6 +912,17 @@  glusterd_op_stage_replace_brick (dict_t *dict, char **op_errstr,
                 goto out;
         }
 
+        if (!glusterd_store_is_valid_brickpath (volname, dst_brick) ||
+                !glusterd_is_valid_volfpath (volname, dst_brick)) {
+                snprintf (msg, sizeof (msg), "brick path %s is too "
+                          "long.", dst_brick);
+                gf_log ("", GF_LOG_ERROR, "%s", msg);
+                *op_errstr = gf_strdup (msg);
+
+                ret = -1;
+                goto out;
+        }
+
         ret = glusterd_check_gsync_running (volinfo, &is_run);
         if (ret && (is_run == _gf_false))
                 gf_log ("", GF_LOG_WARNING, "Unable to get the status"
diff --git a/xlators/mgmt/glusterd/src/glusterd-store.c b/xlators/mgmt/glusterd/src/glusterd-store.c
index 4f296c5..ab1dad8 100644
--- a/xlators/mgmt/glusterd/src/glusterd-store.c
+++ b/xlators/mgmt/glusterd/src/glusterd-store.c
@@ -227,6 +227,41 @@  glusterd_store_brickinfopath_set (glusterd_volinfo_t *volinfo,
         snprintf (brickpath, len, "%s/%s", brickdirpath, brickfname);
 }
 
+gf_boolean_t
+glusterd_store_is_valid_brickpath (char *volname, char *brick)
+{
+        char                    brickpath[PATH_MAX] = {0};
+        glusterd_brickinfo_t    *brickinfo = NULL;
+        glusterd_volinfo_t      *volinfo = NULL;
+        int32_t                 ret = 0;
+
+        ret = glusterd_brickinfo_from_brick (brick, &brickinfo);
+        if (ret) {
+                gf_log ("", GF_LOG_WARNING, "brick path validation failed");
+                ret = 0;
+                goto out;
+        }
+        ret = glusterd_volinfo_new (&volinfo);
+        if (ret) {
+                gf_log ("", GF_LOG_WARNING, "brick path validation failed");
+                ret = 0;
+                goto out;
+        }
+        strncpy (volinfo->volname, volname, sizeof (volinfo->volname));
+        glusterd_store_brickinfopath_set (volinfo, brickinfo, brickpath,
+                                                sizeof (brickpath));
+
+        ret = (strlen (brickpath) < _POSIX_PATH_MAX);
+
+out:
+        if (brickinfo)
+                glusterd_brickinfo_delete (brickinfo);
+        if (volinfo)
+                glusterd_volinfo_delete (volinfo);
+
+        return ret;
+}
+
 int32_t
 glusterd_store_volinfo_brick_fname_write (int vol_fd,
                                          glusterd_brickinfo_t *brickinfo,
diff --git a/xlators/mgmt/glusterd/src/glusterd-store.h b/xlators/mgmt/glusterd/src/glusterd-store.h
index 0403c10..9d64623 100644
--- a/xlators/mgmt/glusterd/src/glusterd-store.h
+++ b/xlators/mgmt/glusterd/src/glusterd-store.h
@@ -125,4 +125,6 @@  glusterd_restore ();
 void
 glusterd_perform_volinfo_version_action (glusterd_volinfo_t *volinfo,
                                          glusterd_volinfo_ver_ac_t ac);
+gf_boolean_t
+glusterd_store_is_valid_brickpath (char *volname, char *brick);
 #endif
diff --git a/xlators/mgmt/glusterd/src/glusterd-volgen.c b/xlators/mgmt/glusterd/src/glusterd-volgen.c
index f7f3ddd..4c7d066 100644
--- a/xlators/mgmt/glusterd/src/glusterd-volgen.c
+++ b/xlators/mgmt/glusterd/src/glusterd-volgen.c
@@ -36,6 +36,7 @@ 
 #include "cli1.h"
 #include "glusterd-volgen.h"
 #include "glusterd-op-sm.h"
+#include "glusterd-utils.h"
 
 
 /* dispatch table for VOLUME SET
@@ -1867,6 +1868,39 @@  get_brick_filepath (char *filename, glusterd_volinfo_t *volinfo,
                   brick);
 }
 
+gf_boolean_t
+glusterd_is_valid_volfpath (char *volname, char *brick)
+{
+        char                    volfpath[PATH_MAX] = {0,};
+        glusterd_brickinfo_t    *brickinfo = NULL;
+        glusterd_volinfo_t      *volinfo = NULL;
+        int32_t                 ret = 0;
+
+        ret = glusterd_brickinfo_from_brick (brick, &brickinfo);
+        if (ret) {
+                gf_log ("", GF_LOG_WARNING, "brick path validation failed");
+                ret = 0;
+                goto out;
+        }
+        ret = glusterd_volinfo_new (&volinfo);
+        if (ret) {
+                gf_log ("", GF_LOG_WARNING, "brick path validation failed");
+                ret = 0;
+                goto out;
+        }
+        strncpy (volinfo->volname, volname, sizeof (volinfo->volname));
+        get_brick_filepath (volfpath, volinfo, brickinfo);
+
+        ret = (strlen (volfpath) < _POSIX_PATH_MAX);
+
+out:
+        if (brickinfo)
+                glusterd_brickinfo_delete (brickinfo);
+        if (volinfo)
+                glusterd_volinfo_delete (volinfo);
+        return ret;
+}
+
 static int
 glusterd_generate_brick_volfile (glusterd_volinfo_t *volinfo,
                                  glusterd_brickinfo_t *brickinfo)
diff --git a/xlators/mgmt/glusterd/src/glusterd-volgen.h b/xlators/mgmt/glusterd/src/glusterd-volgen.h
index 518ae94..014e64d 100644
--- a/xlators/mgmt/glusterd/src/glusterd-volgen.h
+++ b/xlators/mgmt/glusterd/src/glusterd-volgen.h
@@ -62,6 +62,7 @@  int glusterd_validate_localopts (dict_t *val_dict, char **op_errstr);
 gf_boolean_t glusterd_check_globaloption (char *key);
 gf_boolean_t
 glusterd_check_voloption_flags (char *key, int32_t flags);
-
+gf_boolean_t
+glusterd_is_valid_volfpath (char *volname, char *brick);
 
 #endif