Whamcloud - gitweb
smash the HEAD with the contents of b_cmd. HEAD_PRE_CMD_SMASH and
[fs/lustre-release.git] / lustre / smfs / options.c
1 /*
2  *  snapfs/options.c
3  */
4 #define DEBUG_SUBSYSTEM S_SM
5
6 #include <linux/module.h>
7 #include <linux/kmod.h>
8 #include <linux/init.h>
9 #include <linux/fs.h>
10 #include <linux/slab.h>
11 #include <linux/string.h>
12 #include <linux/errno.h>
13 #include <linux/lustre_idl.h>
14 #include "smfs_internal.h" 
15
16
17 static struct list_head option_list;
18 char  *options = NULL;
19 char  *opt_left = NULL;
20
21 int init_option(char *data)
22 {
23         INIT_LIST_HEAD(&option_list);
24         SM_ALLOC(options, strlen(data) + 1);
25         if (!options) {
26                 CERROR("Can not allocate memory \n");
27                 return -ENOMEM;
28         }
29         memcpy(options, data, strlen(data));
30         opt_left = options;
31         return 0;
32 }
33 /*cleanup options*/
34 void cleanup_option(void)
35 {
36         struct option *option;
37         while (!list_empty(&option_list)) {
38                 option = list_entry(option_list.next, struct option, list);
39                 list_del(&option->list);
40                 SM_FREE(option->opt, strlen(option->opt) + 1);
41                 if (option->value)
42                         SM_FREE(option->value, strlen(option->value) + 1);
43                 SM_FREE(option, sizeof(struct option));
44         }
45         SM_FREE(options, strlen(options) + 1);
46 }
47 int get_opt(struct option **option, char **pos)
48 {
49         char  *name, *value, *left, *tmp;
50         struct option *tmp_opt;
51         int  length = 0;
52
53         *pos = opt_left;
54
55         if (! *opt_left)
56                 return -ENODATA;
57         left = strchr(opt_left, ',');
58         if (left == opt_left) 
59                 return -EINVAL;
60         if (!left){
61                 left = opt_left + strlen(opt_left);     
62         }       
63
64         SM_ALLOC(tmp_opt, sizeof(struct option));       
65         tmp_opt->opt = NULL;
66         tmp_opt->value = NULL;
67
68         tmp = opt_left;
69         while(tmp != left && *tmp != '=') {
70                 length++;
71                 tmp++;
72         }       
73         SM_ALLOC(name, length + 1);
74         tmp_opt->opt = name;
75         memset(name, 0, length + 1);
76         while (opt_left != tmp) *name++ = *opt_left++;
77
78         if (*tmp == '=') {
79                 /*this option has value*/
80                 opt_left ++; /*after '='*/
81                 if (left == opt_left) {
82                         SM_FREE(tmp_opt->opt, length);
83                         SM_FREE(tmp_opt, sizeof(struct option));
84                         opt_left = *pos;
85                         return -EINVAL;
86                 }
87                 length = left - opt_left + 1;
88                 SM_ALLOC(value, length);
89                 tmp_opt->value = value;
90                 memset(value, 0, length);
91                 while (opt_left != left) *value++ = *opt_left++;
92         }
93         list_add(&tmp_opt->list, &option_list);
94         if (*opt_left == ',') opt_left ++; /*after ','*/        
95         *option = tmp_opt;
96         return 0;
97 }