2003-09-05 19:18:31

by Randy.Dunlap

[permalink] [raw]
Subject: [CFT] [1/15] table-driven filesystems option parsing


Hi,

This is based on a patch that was begun by Al Viro and
Al asked that it be added to the must-fix list in the
May 21 "must fix" IRC discussion.
<http://lwn.net/Articles/33220/>

Current (full) patch is at
http://developer.osdl.org/rddunlap/patches/linux-260t4g-fsoptions.patch

These patches apply to 2.6.0-test4 or -current (Sept. 5/2003).

I have tested ext3, ext3, fat, isofs, jfs, & proc.

I'd appreciate others testing all of these, please, especially
the ones that I can't test (adfs, affs, hfs, hpfs, ufd, ufs,
autofs[4]).

--
~Randy


This is the parser library and header file.


diff -Naurp -X /home/rddunlap/doc/dontdiff-osdl linux-260-test4-pv/include/linux/parser.h linux-260-test4-fs/include/linux/parser.h
--- linux-260-test4-pv/include/linux/parser.h 1969-12-31 16:00:00.000000000 -0800
+++ linux-260-test4-fs/include/linux/parser.h 2003-09-03 13:01:25.000000000 -0700
@@ -0,0 +1,21 @@
+struct match_token {
+ int token;
+ char *pattern;
+};
+
+typedef struct match_token match_table_t[];
+
+enum {MAX_OPT_ARGS = 3};
+
+typedef struct {
+ char *from;
+ char *to;
+} substring_t;
+
+int match_token(char *s, match_table_t table, substring_t args[]);
+
+int match_int(substring_t *);
+int match_octal(substring_t *);
+int match_hex(substring_t *);
+void match_strcpy(char *, substring_t *);
+char *match_strdup(substring_t *);
diff -Naurp -X /home/rddunlap/doc/dontdiff-osdl linux-260-test4-pv/lib/Makefile linux-260-test4-fs/lib/Makefile
--- linux-260-test4-pv/lib/Makefile 2003-08-22 16:51:34.000000000 -0700
+++ linux-260-test4-fs/lib/Makefile 2003-08-27 11:19:07.000000000 -0700
@@ -5,7 +5,7 @@

lib-y := errno.o ctype.o string.o vsprintf.o cmdline.o \
bust_spinlocks.o rbtree.o radix-tree.o dump_stack.o \
- kobject.o idr.o div64.o
+ kobject.o idr.o div64.o parser.o

lib-$(CONFIG_RWSEM_GENERIC_SPINLOCK) += rwsem-spinlock.o
lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o
diff -Naurp -X /home/rddunlap/doc/dontdiff-osdl linux-260-test4-pv/lib/parser.c linux-260-test4-fs/lib/parser.c
--- linux-260-test4-pv/lib/parser.c 1969-12-31 16:00:00.000000000 -0800
+++ linux-260-test4-fs/lib/parser.c 2003-09-03 13:21:24.000000000 -0700
@@ -0,0 +1,131 @@
+/*
+ * 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 <linux/ctype.h>
+#include <linux/module.h>
+#include <linux/parser.h>
+#include <linux/slab.h>
+#include <linux/string.h>
+
+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 = simple_strtoul(p, &p, 10);
+ else if (*p == '%') {
+ if (*s++ != '%')
+ return 0;
+ continue;
+ }
+
+ if (argc >= MAX_OPT_ARGS)
+ return 0;
+
+ args[argc].from = s;
+ switch (*p++) {
+ case 's':
+ if (len == -1 || len > strlen(s))
+ len = strlen(s);
+ args[argc].to = s + len;
+ break;
+ case 'd':
+ simple_strtol(s, &args[argc].to, 0);
+ goto num;
+ case 'u':
+ simple_strtoul(s, &args[argc].to, 0);
+ goto num;
+ case 'o':
+ simple_strtoul(s, &args[argc].to, 8);
+ goto num;
+ case 'x':
+ simple_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++;
+ }
+}
+
+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;
+}
+
+int match_int(substring_t *s)
+{
+ char buf[s->to - s->from + 1];
+
+ memcpy(buf, s->from, s->to - s->from);
+ buf[s->to - s->from] = '\0';
+ return simple_strtol(buf, NULL, 0);
+}
+
+int match_octal(substring_t *s)
+{
+ char buf[s->to - s->from + 1];
+
+ memcpy(buf, s->from, s->to - s->from);
+ buf[s->to - s->from] = '\0';
+ return simple_strtoul(buf, NULL, 8);
+}
+
+int match_hex(substring_t *s)
+{
+ char buf[s->to - s->from + 1];
+
+ memcpy(buf, s->from, s->to - s->from);
+ buf[s->to - s->from] = '\0';
+ return simple_strtoul(buf, NULL, 16);
+}
+
+void match_strcpy(char *to, substring_t *s)
+{
+ memcpy(to, s->from, s->to - s->from);
+ to[s->to - s->from] = '\0';
+}
+
+char *match_strdup(substring_t *s)
+{
+ char *p = kmalloc(s->to - s->from + 1, GFP_KERNEL);
+ if (p)
+ match_strcpy(p, s);
+ return p;
+}
+
+EXPORT_SYMBOL(match_token);
+EXPORT_SYMBOL(match_int);
+EXPORT_SYMBOL(match_octal);
+EXPORT_SYMBOL(match_hex);
+EXPORT_SYMBOL(match_strcpy);
+EXPORT_SYMBOL(match_strdup);


2003-09-17 19:21:39

by Will Dyson

[permalink] [raw]
Subject: Re: [CFT] [1/15] table-driven filesystems option parsing

On Fri, 2003-09-05 at 14:56, Randy.Dunlap wrote:

> Current (full) patch is at
> http://developer.osdl.org/rddunlap/patches/linux-260t4g-fsoptions.patch
>
> These patches apply to 2.6.0-test4 or -current (Sept. 5/2003).
>
> I have tested ext3, ext3, fat, isofs, jfs, & proc.
>
> I'd appreciate others testing all of these, please, especially
> the ones that I can't test (adfs, affs, hfs, hpfs, ufd, ufs,
> autofs[4]).

As a filesystem maintainer, I like the idea of this patch! However, I
think it could use some comments on useage in the header file. There are
already too many filesystem interfaces where the only documentation on
how to use them is the way that existing filesystems use them.

Below is a patch to convert befs to use parser.h. It kills some of my
least favorite code (yay!).

The patch is tested a certain minimal amount (booted, mounted a
filesystem with options).


# This is a BitKeeper generated patch for the following project:
# Project Name: Linux kernel tree
# This patch format is intended for GNU patch command version 2.5 or
higher.
# This patch includes the following deltas:
# ChangeSet 1.1316 -> 1.1318
# fs/befs/linuxvfs.c 1.12 -> 1.14
#
# The following is the BitKeeper ChangeSet Log
# --------------------------------------------
# 03/09/15 will@thalience.(none) 1.1317
# convert befs to use the parser.h functions to manage its mount options
# --------------------------------------------
# 03/09/17 will@thalience.(none) 1.1318
# Fix formatting and a silly {} error
# --------------------------------------------
#
diff -Nru a/fs/befs/linuxvfs.c b/fs/befs/linuxvfs.c
--- a/fs/befs/linuxvfs.c Wed Sep 17 00:56:25 2003
+++ b/fs/befs/linuxvfs.c Wed Sep 17 00:56:25 2003
@@ -13,6 +13,7 @@
#include <linux/nls.h>
#include <linux/buffer_head.h>
#include <linux/vfs.h>
+#include <linux/parser.h>

#include "befs.h"
#include "btree.h"
@@ -667,12 +668,26 @@
return -EILSEQ;
}

+/**
+ * Use the
+ *
+ */
+enum {
+ Opt_uid, Opt_gid, Opt_charset, Opt_debug,
+};
+
+static match_table_t befs_tokens = {
+ {Opt_uid, "uid=%d"},
+ {Opt_gid, "gid=%d"},
+ {Opt_charset, "iocharset=%s"},
+ {Opt_debug, "debug"}
+};
+
static int
parse_options(char *options, befs_mount_options * opts)
{
- char *this_char;
- char *value;
- int ret = 1;
+ char *p;
+ substring_t args[MAX_OPT_ARGS];

/* Initialize options */
opts->uid = 0;
@@ -683,64 +698,60 @@
opts->debug = 0;

if (!options)
- return ret;
+ return 1;

- while ((this_char = strsep(&options, ",")) != NULL) {
-
- if ((value = strchr(this_char, '=')) != NULL)
- *value++ = 0;
-
- if (!strcmp(this_char, "uid")) {
- if (!value || !*value) {
- ret = 0;
- } else {
- opts->uid = simple_strtoul(value, &value, 0);
- opts->use_uid = 1;
- if (*value) {
- printk(KERN_ERR "BEFS: Invalid uid "
- "option: %s\n", value);
- ret = 0;
+ while ((p = strsep(&options, ",")) != NULL) {
+ int token;
+ if (!*p)
+ continue;
+
+ token = match_token(p, befs_tokens, args);
+ switch (token) {
+ case Opt_uid:
+ {
+ int uid = match_int(&args[0]);
+ if (uid < 0) {
+ printk(KERN_ERR "BeFS: Invalid uid %d, "
+ "using default\n", uid);
+ break;
}
+ opts->uid = uid;
+ opts->use_uid = 1;
+ break;
}
- } else if (!strcmp(this_char, "gid")) {
- if (!value || !*value)
- ret = 0;
- else {
- opts->gid = simple_strtoul(value, &value, 0);
- opts->use_gid = 1;
- if (*value) {
- printk(KERN_ERR
- "BEFS: Invalid gid option: "
- "%s\n", value);
- ret = 0;
+ case Opt_gid:
+ {
+ int gid = match_int(&args[0]);
+ if (gid < 0) {
+ printk(KERN_ERR "BeFS: Invalid gid %d, "
+ "using default\n", gid);
+ break;
}
+ opts->gid = gid;
+ opts->use_gid = 1;
+ break;
}
- } else if (!strcmp(this_char, "iocharset") && value) {
- char *p = value;
- int len;
-
- while (*value && *value != ',')
- value++;
- len = value - p;
- if (len) {
- char *buffer = kmalloc(len + 1, GFP_NOFS);
- if (buffer) {
- opts->iocharset = buffer;
- memcpy(buffer, p, len);
- buffer[len] = 0;
-
- } else {
- printk(KERN_ERR "BEFS: "
- "cannot allocate memory\n");
- ret = 0;
+ case Opt_charset:
+ {
+ kfree(opts->iocharset);
+ opts->iocharset = match_strdup(&args[0]);
+ if (!opts->iocharset) {
+ printk(KERN_ERR "BeFS: allocation failure for "
+ "iocharset string\n");
+ return 0;
}
+ break;
}
- } else if (!strcmp(this_char, "debug")) {
- opts->debug = 1;
+ case Opt_debug:
+ opts->debug = 1;
+ break;
+ default:
+ printk(KERN_ERR "BeFS: Unrecognized mount option \"%s\" "
+ "or missing value\n", p);
+ return 0;
}
}
-
- return ret;
+ return 1;
}

/* This function has the responsibiltiy of getting the



--
Will Dyson
"Back off man, I'm a scientist!" -Dr. Peter Venkman

2003-09-19 20:43:46

by Randy.Dunlap

[permalink] [raw]
Subject: Re: [CFT] [1/15] table-driven filesystems option parsing

On Wed, 17 Sep 2003 15:21:05 -0400 Will Dyson <[email protected]> wrote:

| On Fri, 2003-09-05 at 14:56, Randy.Dunlap wrote:
|
| > Current (full) patch is at
| > http://developer.osdl.org/rddunlap/patches/linux-260t4g-fsoptions.patch
| >
| > These patches apply to 2.6.0-test4 or -current (Sept. 5/2003).
| >
| > I have tested ext3, ext3, fat, isofs, jfs, & proc.
| >
| > I'd appreciate others testing all of these, please, especially
| > the ones that I can't test (adfs, affs, hfs, hpfs, ufd, ufs,
| > autofs[4]).
|
| As a filesystem maintainer, I like the idea of this patch! However, I
| think it could use some comments on useage in the header file. There are
| already too many filesystem interfaces where the only documentation on
| how to use them is the way that existing filesystems use them.
|
| Below is a patch to convert befs to use parser.h. It kills some of my
| least favorite code (yay!).
|
| The patch is tested a certain minimal amount (booted, mounted a
| filesystem with options).

Thanks for the patch and comments. I'll try to add some usage docs
to it also.

--
~Randy