Whamcloud - gitweb
Small fix on the description of some lctl commands.
[fs/lustre-release.git] / lustre / smfs / options.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2004 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #define DEBUG_SUBSYSTEM S_SM
24
25 #include <linux/module.h>
26 #include <linux/kmod.h>
27 #include <linux/init.h>
28 #include <linux/fs.h>
29 #include <linux/slab.h>
30 #include <linux/string.h>
31 #include <linux/errno.h>
32 #include <linux/obd_class.h>
33 #include <linux/obd_support.h>
34 #include <linux/lustre_lib.h>
35 #include <linux/lustre_idl.h>
36 #include <linux/lustre_smfs.h>
37 #include "smfs_internal.h"
38
39
40 static struct list_head option_list;
41 static char *options = NULL;
42 static char *opt_left = NULL;
43
44 int init_option(char *data)
45 {
46         INIT_LIST_HEAD(&option_list);
47         OBD_ALLOC(options, strlen(data) + 1);
48         if (!options) {
49                 CERROR("Can not allocate memory \n");
50                 return -ENOMEM;
51         }
52         memcpy(options, data, strlen(data));
53         opt_left = options;
54         return 0;
55 }
56
57 /*cleanup options*/
58 void cleanup_option(void)
59 {
60         struct option *option;
61         while (!list_empty(&option_list)) {
62                 option = list_entry(option_list.next, struct option, list);
63                 list_del(&option->list);
64                 OBD_FREE(option->opt, strlen(option->opt) + 1);
65                 if (option->value)
66                         OBD_FREE(option->value, strlen(option->value) + 1);
67                 OBD_FREE(option, sizeof(struct option));
68         }
69         OBD_FREE(options, strlen(options) + 1);
70 }
71
72 int get_opt(struct option **option, char **pos)
73 {
74         char *name, *value, *left, *tmp;
75         struct option *tmp_opt;
76         int length = 0;
77
78         *pos = opt_left;
79
80         if (!*opt_left)
81                 return -ENODATA;
82         left = strchr(opt_left, ',');
83         if (left == opt_left)
84                 return -EINVAL;
85         if (!left)
86                 left = opt_left + strlen(opt_left);
87
88         OBD_ALLOC(tmp_opt, sizeof(struct option));
89         tmp_opt->opt = NULL;
90         tmp_opt->value = NULL;
91
92         tmp = opt_left;
93         while(tmp != left && *tmp != '=') {
94                 length++;
95                 tmp++;
96         }
97         OBD_ALLOC(name, length + 1);
98         tmp_opt->opt = name;
99         while (opt_left != tmp) *name++ = *opt_left++;
100
101         if (*tmp == '=') {
102                 /*this option has value*/
103                 opt_left ++; /*after '='*/
104                 if (left == opt_left) {
105                         OBD_FREE(tmp_opt->opt, strlen(tmp_opt->opt) + 1);
106                         OBD_FREE(tmp_opt, sizeof(struct option));
107                         opt_left = *pos;
108                         return -EINVAL;
109                 }
110                 length = left - opt_left;
111                 OBD_ALLOC(value, length + 1);
112                 if (!value) {
113                         OBD_FREE(tmp_opt->opt, strlen(tmp_opt->opt) + 1);
114                         OBD_FREE(tmp_opt, sizeof(struct option));
115                         return -ENOMEM;
116                 }
117                 tmp_opt->value = value;
118                 while (opt_left != left) *value++ = *opt_left++;
119         }
120         list_add(&tmp_opt->list, &option_list);
121         if (*opt_left == ',') opt_left ++; /*after ','*/
122         *option = tmp_opt;
123         return 0;
124 }