Whamcloud - gitweb
- many fixes about using ENTRY, RETURN, GOTO and EXIT.
[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  *  lustre/smfs/options.c
5  *  Lustre filesystem abstraction routines
6  *
7  *  Copyright (C) 2004 Cluster File Systems, Inc.
8  *
9  *   This file is part of Lustre, http://www.lustre.org.
10  *
11  *   Lustre is free software; you can redistribute it and/or
12  *   modify it under the terms of version 2 of the GNU General Public
13  *   License as published by the Free Software Foundation.
14  *
15  *   Lustre is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with Lustre; if not, write to the Free Software
22  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 #define DEBUG_SUBSYSTEM S_SM
26
27 #include <linux/module.h>
28 #include <linux/kmod.h>
29 #include <linux/init.h>
30 #include <linux/fs.h>
31 #include <linux/slab.h>
32 #include <linux/string.h>
33 #include <linux/errno.h>
34 #include <linux/obd_class.h>
35 #include <linux/obd_support.h>
36 #include <linux/lustre_lib.h>
37 #include <linux/lustre_idl.h>
38 #include <linux/lustre_smfs.h>
39 #include <linux/lustre_fsfilt.h>
40 #include "smfs_internal.h"
41
42 static struct list_head option_list;
43 static char *options = NULL;
44 static char *opt_left = NULL;
45
46 int init_option(char *data)
47 {
48         INIT_LIST_HEAD(&option_list);
49         OBD_ALLOC(options, strlen(data) + 1);
50         if (!options) {
51                 CERROR("Can not allocate memory \n");
52                 return -ENOMEM;
53         }
54         memcpy(options, data, strlen(data));
55         opt_left = options;
56         return 0;
57 }
58
59 /*cleanup options*/
60 void cleanup_option(void)
61 {
62         struct option *option;
63         while (!list_empty(&option_list)) {
64                 option = list_entry(option_list.next, struct option, list);
65                 list_del(&option->list);
66                 OBD_FREE(option->opt, strlen(option->opt) + 1);
67                 if (option->value)
68                         OBD_FREE(option->value, strlen(option->value) + 1);
69                 OBD_FREE(option, sizeof(struct option));
70         }
71         OBD_FREE(options, strlen(options) + 1);
72 }
73
74 int get_opt(struct option **option, char **pos)
75 {
76         char *name, *value, *left, *tmp;
77         struct option *tmp_opt;
78         int length = 0;
79
80         *pos = opt_left;
81
82         if (!*opt_left)
83                 return -ENODATA;
84         
85         left = strchr(opt_left, ',');
86         if (left == opt_left)
87                 return -EINVAL;
88         if (!left)
89                 left = opt_left + strlen(opt_left);
90
91         OBD_ALLOC(tmp_opt, sizeof(*tmp_opt));
92         if (!tmp_opt)
93                 return -ENOMEM;
94         
95         tmp_opt->opt = NULL;
96         tmp_opt->value = NULL;
97
98         tmp = opt_left;
99         while(tmp != left && *tmp != '=') {
100                 length++;
101                 tmp++;
102         }
103         OBD_ALLOC(name, length + 1);
104         if (!name) {
105                 OBD_GREE(tmp_opt, sizeof(*tmp_opt));
106                 return -ENOMEM;
107         }
108                 
109         tmp_opt->opt = name;
110         while (opt_left != tmp) *name++ = *opt_left++;
111
112         if (*tmp == '=') {
113                 /*this option has value*/
114                 opt_left ++; /*after '='*/
115                 if (left == opt_left) {
116                         OBD_FREE(tmp_opt->opt, strlen(tmp_opt->opt) + 1);
117                         OBD_FREE(tmp_opt, sizeof(struct option));
118                         opt_left = *pos;
119                         return -EINVAL;
120                 }
121                 length = left - opt_left;
122                 OBD_ALLOC(value, length + 1);
123                 if (!value) {
124                         OBD_FREE(tmp_opt->opt, strlen(tmp_opt->opt) + 1);
125                         OBD_FREE(tmp_opt, sizeof(struct option));
126                         return -ENOMEM;
127                 }
128                 tmp_opt->value = value;
129                 while (opt_left != left) *value++ = *opt_left++;
130         }
131         list_add(&tmp_opt->list, &option_list);
132         if (*opt_left == ',') opt_left ++; /*after ','*/
133         *option = tmp_opt;
134         printk("Option: %s=%s\n", tmp_opt->opt, tmp_opt->value);
135         return 0;
136 }
137
138 char *smfs_options(char *data, char **devstr, char **namestr,
139                    char *opts, int *flags)  
140 {
141         struct option *opt_value = NULL;
142         char   *pos;
143         
144         LASSERT(opts && flags);
145         init_option(data);
146         
147         while (!(get_opt(&opt_value, &pos))) {
148                 if (!strcmp(opt_value->opt, "dev")) {
149                         if (devstr != NULL)
150                                 *devstr = opt_value->value;
151                 } else if (!strcmp(opt_value->opt, "type")) {
152                         if (namestr != NULL)
153                                 *namestr = opt_value->value;
154                 } else if (!strcmp(opt_value->opt, "kml")) {
155                         *flags |= SM_DO_REC;
156                 } else if (!strcmp(opt_value->opt, "cache")) {
157                         *flags |= SM_CACHE_HOOK;
158                 } else if (!strcmp(opt_value->opt, "snap")) {
159                         *flags |= SM_DO_COW;
160                 } else if (!strcmp(opt_value->opt, "options")) {
161                         if (strlen(opts) == 0)
162                                 sprintf((char *)opts + strlen(opts), "%s",
163                                         opt_value->value);
164                         else  
165                                 sprintf((char *)opts + strlen(opts), ",%s",
166                                         opt_value->value);
167                 } else {
168                         /* FIXME-WANGDI: how about the opt_value->value */
169                         if (strlen(opts) == 0)
170                                 sprintf((char *)opts + strlen(opts), "%s",
171                                         opt_value->opt);
172                         else  
173                                 sprintf((char *)opts + strlen(opts), ",%s",
174                                         opt_value->opt);
175                 }
176         }
177         return pos;
178 }
179