[Stgt-devel] VTL patch, take 2
FUJITA Tomonori
fujita.tomonori
Fri Apr 20 07:43:41 CEST 2007
From: "Mark Harvey" <markh794 at gmail.com>
Subject: [Stgt-devel] VTL patch, take 2
Date: Fri, 13 Apr 2007 07:28:04 +1000
> SSC / SMC patches to SCSI Tgt code.
>
> It is based from git commit :
> ================================
> commit 47e15c234ca298d31b1709db592eef9db4bce0ca
> Author: Albert Pauw <albert.pauw at gmail.com>
> Date: Sat Apr 7 17:31:54 2007 +0900
>
> some shortcut options missing from tgtadm
>
> Signed-off-by: Albert Pauw <albert.pauw at gmail.com>
> ================================
>
>
> Additions from last patch include :
> - Updated syntax to add/configure limited set of SMC / SSC parameters
> - Start implementing structures for MODE SELECT / MODE SENSE op codes
> - Start implementing structures for LOG SELECT / LOG SENSE op codes
>
> - SMC module includes:
> Allocation of MODE pages and initialization to default values.
> Ability to change Element Address Assignment
>
>
>
> This is a work-in-progress and is being posted as an example of
> where/how I'm attempting to implement a VTL.
>
> I need to update command parameter passing to recommended use of
> "--params ....."
I've attached a parser patch (taken from Linux kernel). See
tgt_device_update() about how it works if you are not familiar with
the Linux parser. I think that it works for you. And if so, I'll merge
this patch shortly (though I need to change it slightly).
> Next step is to implement the SCSI MODE SENSE op code so I can
> actually test what is being set is correct..
>
> Note: in root of src is a 'tgt-setup' script which will (should)
> correctly configure the SSC & SMC modules
I think that it would be better to create a new directory for example
scripts.
diff --git a/usr/Makefile b/usr/Makefile
index 96535f9..2e299dc 100644
--- a/usr/Makefile
+++ b/usr/Makefile
@@ -45,7 +45,7 @@ CFLAGS += -Wall -g -O2 -Wstrict-prototyp
PROGRAMS += tgtd tgtadm
TGTD_OBJS += tgtd.o mgmt.o target.o spc.o sbc.o mmc.o osd.o spt.o scsi.o log.o \
- driver.o util.o work.o
+ driver.o util.o work.o parser.o
all: $(PROGRAMS)
diff --git a/usr/parser.c b/usr/parser.c
new file mode 100644
index 0000000..75fe12c
--- /dev/null
+++ b/usr/parser.c
@@ -0,0 +1,216 @@
+/*
+ * lib/parser.c - simple parser for mount, etc. options.
+ *
+ * This source code is licensed under the GNU General Public License,
+ * Version 2. See the file COPYING for more details.
+ */
+
+#include <ctype.h>
+#include <errno.h>
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "parser.h"
+
+/**
+ * match_one: - Determines if a string matches a simple pattern
+ * @s: the string to examine for presense of the pattern
+ * @p: the string containing the pattern
+ * @args: array of %MAX_OPT_ARGS &substring_t elements. Used to return match
+ * locations.
+ *
+ * Description: Determines if the pattern @p is present in string @s. Can only
+ * match extremely simple token=arg style patterns. If the pattern is found,
+ * the location(s) of the arguments will be returned in the @args array.
+ */
+static int match_one(char *s, char *p, substring_t args[])
+{
+ char *meta;
+ int argc = 0;
+
+ if (!p)
+ return 1;
+
+ while(1) {
+ int len = -1;
+ meta = strchr(p, '%');
+ if (!meta)
+ return strcmp(p, s) == 0;
+
+ if (strncmp(p, s, meta-p))
+ return 0;
+
+ s += meta - p;
+ p = meta + 1;
+
+ if (isdigit(*p))
+ len = strtoul(p, &p, 10);
+ else if (*p == '%') {
+ if (*s++ != '%')
+ return 0;
+ p++;
+ continue;
+ }
+
+ if (argc >= MAX_OPT_ARGS)
+ return 0;
+
+ args[argc].from = s;
+ switch (*p++) {
+ case 's':
+ if (strlen(s) == 0)
+ return 0;
+ else if (len == -1 || len > strlen(s))
+ len = strlen(s);
+ args[argc].to = s + len;
+ break;
+ case 'd':
+ strtol(s, &args[argc].to, 0);
+ goto num;
+ case 'u':
+ strtoul(s, &args[argc].to, 0);
+ goto num;
+ case 'o':
+ strtoul(s, &args[argc].to, 8);
+ goto num;
+ case 'x':
+ strtoul(s, &args[argc].to, 16);
+ num:
+ if (args[argc].to == args[argc].from)
+ return 0;
+ break;
+ default:
+ return 0;
+ }
+ s = args[argc].to;
+ argc++;
+ }
+}
+
+/**
+ * match_token: - Find a token (and optional args) in a string
+ * @s: the string to examine for token/argument pairs
+ * @table: match_table_t describing the set of allowed option tokens and the
+ * arguments that may be associated with them. Must be terminated with a
+ * &struct match_token whose pattern is set to the NULL pointer.
+ * @args: array of %MAX_OPT_ARGS &substring_t elements. Used to return match
+ * locations.
+ *
+ * Description: Detects which if any of a set of token strings has been passed
+ * to it. Tokens can include up to MAX_OPT_ARGS instances of basic c-style
+ * format identifiers which will be taken into account when matching the
+ * tokens, and whose locations will be returned in the @args array.
+ */
+int match_token(char *s, match_table_t table, substring_t args[])
+{
+ struct match_token *p;
+
+ for (p = table; !match_one(s, p->pattern, args) ; p++)
+ ;
+
+ return p->token;
+}
+
+/**
+ * match_number: scan a number in the given base from a substring_t
+ * @s: substring to be scanned
+ * @result: resulting integer on success
+ * @base: base to use when converting string
+ *
+ * Description: Given a &substring_t and a base, attempts to parse the substring
+ * as a number in that base. On success, sets @result to the integer represented
+ * by the string and returns 0. Returns either -ENOMEM or -EINVAL on failure.
+ */
+static int match_number(substring_t *s, int *result, int base)
+{
+ char *endp;
+ char *buf;
+ int ret;
+
+ buf = malloc(s->to - s->from + 1);
+ if (!buf)
+ return -ENOMEM;
+ memcpy(buf, s->from, s->to - s->from);
+ buf[s->to - s->from] = '\0';
+ *result = strtol(buf, &endp, base);
+ ret = 0;
+ if (endp == buf)
+ ret = -EINVAL;
+ free(buf);
+ return ret;
+}
+
+/**
+ * match_int: - scan a decimal representation of an integer from a substring_t
+ * @s: substring_t to be scanned
+ * @result: resulting integer on success
+ *
+ * Description: Attempts to parse the &substring_t @s as a decimal integer. On
+ * success, sets @result to the integer represented by the string and returns 0.
+ * Returns either -ENOMEM or -EINVAL on failure.
+ */
+int match_int(substring_t *s, int *result)
+{
+ return match_number(s, result, 0);
+}
+
+/**
+ * match_octal: - scan an octal representation of an integer from a substring_t
+ * @s: substring_t to be scanned
+ * @result: resulting integer on success
+ *
+ * Description: Attempts to parse the &substring_t @s as an octal integer. On
+ * success, sets @result to the integer represented by the string and returns
+ * 0. Returns either -ENOMEM or -EINVAL on failure.
+ */
+int match_octal(substring_t *s, int *result)
+{
+ return match_number(s, result, 8);
+}
+
+/**
+ * match_hex: - scan a hex representation of an integer from a substring_t
+ * @s: substring_t to be scanned
+ * @result: resulting integer on success
+ *
+ * Description: Attempts to parse the &substring_t @s as a hexadecimal integer.
+ * On success, sets @result to the integer represented by the string and
+ * returns 0. Returns either -ENOMEM or -EINVAL on failure.
+ */
+int match_hex(substring_t *s, int *result)
+{
+ return match_number(s, result, 16);
+}
+
+/**
+ * match_strcpy: - copies the characters from a substring_t to a string
+ * @to: string to copy characters to.
+ * @s: &substring_t to copy
+ *
+ * Description: Copies the set of characters represented by the given
+ * &substring_t @s to the c-style string @to. Caller guarantees that @to is
+ * large enough to hold the characters of @s.
+ */
+void match_strcpy(char *to, substring_t *s)
+{
+ memcpy(to, s->from, s->to - s->from);
+ to[s->to - s->from] = '\0';
+}
+
+/**
+ * match_strdup: - allocate a new string with the contents of a substring_t
+ * @s: &substring_t to copy
+ *
+ * Description: Allocates and returns a string filled with the contents of
+ * the &substring_t @s. The caller is responsible for freeing the returned
+ * string with kfree().
+ */
+char *match_strdup(substring_t *s)
+{
+ char *p = malloc(s->to - s->from + 1);
+ if (p)
+ match_strcpy(p, s);
+ return p;
+}
diff --git a/usr/parser.h b/usr/parser.h
new file mode 100644
index 0000000..fa33328
--- /dev/null
+++ b/usr/parser.h
@@ -0,0 +1,33 @@
+/*
+ * linux/include/linux/parser.h
+ *
+ * Header for lib/parser.c
+ * Intended use of these functions is parsing filesystem argument lists,
+ * but could potentially be used anywhere else that simple option=arg
+ * parsing is required.
+ */
+
+
+/* associates an integer enumerator with a pattern string. */
+struct match_token {
+ int token;
+ char *pattern;
+};
+
+typedef struct match_token match_table_t[];
+
+/* Maximum number of arguments that match_token will find in a pattern */
+enum {MAX_OPT_ARGS = 3};
+
+/* Describe the location within a string of a substring */
+typedef struct {
+ char *from;
+ char *to;
+} substring_t;
+
+int match_token(char *, match_table_t table, substring_t args[]);
+int match_int(substring_t *, int *result);
+int match_octal(substring_t *, int *result);
+int match_hex(substring_t *, int *result);
+void match_strcpy(char *, substring_t *);
+char *match_strdup(substring_t *);
diff --git a/usr/target.c b/usr/target.c
index 1a9fe68..1510078 100644
--- a/usr/target.c
+++ b/usr/target.c
@@ -29,6 +29,7 @@ #include <unistd.h>
#include <sys/socket.h>
#include "list.h"
+#include "parser.h"
#include "util.h"
#include "tgtd.h"
#include "driver.h"
@@ -338,9 +339,20 @@ int device_reserved(struct scsi_cmd *cmd
return -EBUSY;
}
-int tgt_device_update(int tid, uint64_t dev_id, char *name)
+enum {
+ Opt_scsiid, Opt_scsisn, Opt_err,
+};
+
+static match_table_t tokens = {
+ {Opt_scsiid, "Scsi_id=%s"},
+ {Opt_scsisn, "Scsi_sn=%s"},
+ {Opt_err, NULL},
+};
+
+int tgt_device_update(int tid, uint64_t dev_id, char *params)
{
int err = 0;
+ char *p;
struct target *target;
struct scsi_lu *lu;
@@ -354,12 +366,24 @@ int tgt_device_update(int tid, uint64_t
return TGTADM_NO_LUN;
}
- if (!strncmp(name, "scsi_id=", 8))
- memcpy(lu->scsi_id, name + 8, sizeof(lu->scsi_id) - 1);
- else if (!strncmp(name, "scsi_sn=", 8))
- memcpy(lu->scsi_sn, name + 8, sizeof(lu->scsi_sn) - 1);
- else
- err = TGTADM_INVALID_REQUEST;
+ while ((p = strsep(¶ms, ",")) != NULL) {
+ substring_t args[MAX_OPT_ARGS];
+ int token;
+ if (!*p)
+ continue;
+ token = match_token(p, tokens, args);
+
+ switch (token) {
+ case Opt_scsiid:
+ match_strcpy(lu->scsi_id, &args[0]);
+ break;
+ case Opt_scsisn:
+ match_strcpy(lu->scsi_sn, &args[0]);
+ break;
+ default:
+ err = TGTADM_INVALID_REQUEST;
+ }
+ }
return err;
}
More information about the stgt
mailing list