Whamcloud - gitweb
smash the HEAD with the contents of b_cmd. HEAD_PRE_CMD_SMASH and
[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 char  *options = NULL;
21 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 /*cleanup options*/
36 void cleanup_option()
37 {
38         struct option *option;
39         while (!list_empty(&option_list)) {
40                 option = list_entry(option_list.next, struct option, list);
41                 list_del(&option->list);
42                 SNAP_FREE(option->opt, strlen(option->opt) + 1);
43                 SNAP_FREE(option->value, strlen(option->value) + 1);
44                 SNAP_FREE(option, sizeof(struct option));
45         }
46         SNAP_FREE(options, strlen(options) + 1);
47 }
48 int get_opt(struct option **option, char **pos)
49 {
50         char  *name, *value, *left;
51         struct option *tmp_opt;
52         int  length;
53
54         *pos = opt_left;
55
56         if (! *opt_left)
57                 return -ENODATA;
58
59         left = strchr(opt_left, '=');
60
61         if (left == opt_left || !left) 
62                 return -EINVAL;
63
64         SNAP_ALLOC(tmp_opt, sizeof(struct option));     
65                 
66         length = left - opt_left + 1;
67         SNAP_ALLOC(name, length);
68         tmp_opt->opt = name;
69         memset(name, 0, length);
70         while (opt_left != left) *name++ = *opt_left++;
71
72         opt_left ++; /*after '='*/
73
74         left = strchr(opt_left, ',');
75         if (left == opt_left) {
76                 SNAP_FREE(tmp_opt->opt, length);
77                 SNAP_FREE(tmp_opt, sizeof(struct option));
78                 opt_left = *pos;
79                 return -EINVAL;
80         }
81         if (!left) 
82                 left = opt_left + strlen(opt_left); 
83         length = left - opt_left + 1;
84         SNAP_ALLOC(value, length);
85         tmp_opt->value = value;
86         memset(value, 0, length);
87         while (opt_left != left) *value++ = *opt_left++;
88
89         list_add(&tmp_opt->list, &option_list);
90         
91         if (*opt_left == ',') opt_left ++; /*after ','*/        
92
93         *option = tmp_opt;
94         return 0;
95 }