]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
dynamically allocate log page buffers
authorChristoph Hellwig <hch@lst.de>
Fri, 23 Sep 2016 03:22:55 +0000 (20:22 -0700)
committerKeith Busch <keith.busch@intel.com>
Fri, 23 Sep 2016 14:40:25 +0000 (10:40 -0400)
A device can report large logs.  Better dynamically allocate the buffer we
pass to the kernel instead of doing a gigantic stack allocation.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Keith Busch <keith.busch@intel.com>
nvme.c

diff --git a/nvme.c b/nvme.c
index e7b50b8606d743c88851e153d4614dc71113b1b0..dac9d7d494aceb7a6a093fa261915918442c09da 100644 (file)
--- a/nvme.c
+++ b/nvme.c
@@ -299,7 +299,13 @@ static int get_error_log(int argc, char **argv, struct command *cmd, struct plug
                fprintf(stderr, "could not identify controller\n");
                return ENODEV;
        } else {
-               struct nvme_error_log_page err_log[cfg.log_entries];
+               struct nvme_error_log_page *err_log;
+
+               err_log = calloc(cfg.log_entries, sizeof(struct nvme_error_log_page));
+               if (!err_log) {
+                       fprintf(stderr, "could not alloc buffer for error log\n");
+                       return ENOMEM;
+               }
 
                err = nvme_error_log(fd, cfg.namespace_id, cfg.log_entries, err_log);
                if (!err) {
@@ -313,6 +319,7 @@ static int get_error_log(int argc, char **argv, struct command *cmd, struct plug
                else if (err > 0)
                        fprintf(stderr, "NVMe Status:%s(%x)\n",
                                                nvme_status_to_string(err), err);
+               free(err_log);
        }
        return err;
 }
@@ -403,7 +410,13 @@ static int get_log(int argc, char **argv, struct command *cmd, struct plugin *pl
                fprintf(stderr, "non-zero log-len is required param\n");
                return EINVAL;
        } else {
-               unsigned char log[cfg.log_len];
+               unsigned char *log;
+
+               log = malloc(cfg.log_len);
+               if (!log) {
+                       fprintf(stderr, "could not alloc buffer for log\n");
+                       return EINVAL;
+               }
 
                err = nvme_get_log(fd, cfg.namespace_id, cfg.log_id, cfg.log_len, log);
                if (!err) {
@@ -417,6 +430,7 @@ static int get_log(int argc, char **argv, struct command *cmd, struct plugin *pl
                } else if (err > 0)
                        fprintf(stderr, "NVMe Status:%s(%x)\n",
                                                nvme_status_to_string(err), err);
+               free(log);
                return err;
        }
 }