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