Merge branch 'master'
[linux-2.6] / fs / gfs2 / locking / dlm / sysfs.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2005 Red Hat, Inc.  All rights reserved.
4  *
5  * This copyrighted material is made available to anyone wishing to use,
6  * modify, copy, or redistribute it subject to the terms and conditions
7  * of the GNU General Public License v.2.
8  */
9
10 #include <linux/ctype.h>
11 #include <linux/stat.h>
12
13 #include "lock_dlm.h"
14
15 extern struct lm_lockops gdlm_ops;
16
17 static ssize_t proto_name_show(struct gdlm_ls *ls, char *buf)
18 {
19         return sprintf(buf, "%s\n", gdlm_ops.lm_proto_name);
20 }
21
22 static ssize_t block_show(struct gdlm_ls *ls, char *buf)
23 {
24         ssize_t ret;
25         int val = 0;
26
27         if (test_bit(DFL_BLOCK_LOCKS, &ls->flags))
28                 val = 1;
29         ret = sprintf(buf, "%d\n", val);
30         return ret;
31 }
32
33 static ssize_t block_store(struct gdlm_ls *ls, const char *buf, size_t len)
34 {
35         ssize_t ret = len;
36         int val;
37
38         val = simple_strtol(buf, NULL, 0);
39
40         if (val == 1)
41                 set_bit(DFL_BLOCK_LOCKS, &ls->flags);
42         else if (val == 0) {
43                 clear_bit(DFL_BLOCK_LOCKS, &ls->flags);
44                 gdlm_submit_delayed(ls);
45         } else
46                 ret = -EINVAL;
47         return ret;
48 }
49
50 static ssize_t withdraw_show(struct gdlm_ls *ls, char *buf)
51 {
52         ssize_t ret;
53         int val = 0;
54
55         if (test_bit(DFL_WITHDRAW, &ls->flags))
56                 val = 1;
57         ret = sprintf(buf, "%d\n", val);
58         return ret;
59 }
60
61 static ssize_t withdraw_store(struct gdlm_ls *ls, const char *buf, size_t len)
62 {
63         ssize_t ret = len;
64         int val;
65
66         val = simple_strtol(buf, NULL, 0);
67
68         if (val == 1)
69                 set_bit(DFL_WITHDRAW, &ls->flags);
70         else
71                 ret = -EINVAL;
72         wake_up(&ls->wait_control);
73         return ret;
74 }
75
76 static ssize_t id_show(struct gdlm_ls *ls, char *buf)
77 {
78         return sprintf(buf, "%u\n", ls->id);
79 }
80
81 static ssize_t jid_show(struct gdlm_ls *ls, char *buf)
82 {
83         return sprintf(buf, "%d\n", ls->jid);
84 }
85
86 static ssize_t first_show(struct gdlm_ls *ls, char *buf)
87 {
88         return sprintf(buf, "%d\n", ls->first);
89 }
90
91 static ssize_t first_done_show(struct gdlm_ls *ls, char *buf)
92 {
93         return sprintf(buf, "%d\n", ls->first_done);
94 }
95
96 static ssize_t recover_show(struct gdlm_ls *ls, char *buf)
97 {
98         return sprintf(buf, "%d\n", ls->recover_jid);
99 }
100
101 static ssize_t recover_store(struct gdlm_ls *ls, const char *buf, size_t len)
102 {
103         ls->recover_jid = simple_strtol(buf, NULL, 0);
104         ls->fscb(ls->fsdata, LM_CB_NEED_RECOVERY, &ls->recover_jid);
105         return len;
106 }
107
108 static ssize_t recover_done_show(struct gdlm_ls *ls, char *buf)
109 {
110         return sprintf(buf, "%d\n", ls->recover_jid_done);
111 }
112
113 struct gdlm_attr {
114         struct attribute attr;
115         ssize_t (*show)(struct gdlm_ls *, char *);
116         ssize_t (*store)(struct gdlm_ls *, const char *, size_t);
117 };
118
119 #define GDLM_ATTR(_name,_mode,_show,_store) \
120 static struct gdlm_attr gdlm_attr_##_name = __ATTR(_name,_mode,_show,_store)
121
122 GDLM_ATTR(proto_name,   0444, proto_name_show,   NULL);
123 GDLM_ATTR(block,        0644, block_show,        block_store);
124 GDLM_ATTR(withdraw,     0644, withdraw_show,     withdraw_store);
125 GDLM_ATTR(id,           0444, id_show,           NULL);
126 GDLM_ATTR(jid,          0444, jid_show,          NULL);
127 GDLM_ATTR(first,        0444, first_show,        NULL);
128 GDLM_ATTR(first_done,   0444, first_done_show,   NULL);
129 GDLM_ATTR(recover,      0644, recover_show,      recover_store);
130 GDLM_ATTR(recover_done, 0444, recover_done_show, NULL);
131
132 static struct attribute *gdlm_attrs[] = {
133         &gdlm_attr_proto_name.attr,
134         &gdlm_attr_block.attr,
135         &gdlm_attr_withdraw.attr,
136         &gdlm_attr_id.attr,
137         &gdlm_attr_jid.attr,
138         &gdlm_attr_first.attr,
139         &gdlm_attr_first_done.attr,
140         &gdlm_attr_recover.attr,
141         &gdlm_attr_recover_done.attr,
142         NULL,
143 };
144
145 static ssize_t gdlm_attr_show(struct kobject *kobj, struct attribute *attr,
146                               char *buf)
147 {
148         struct gdlm_ls *ls = container_of(kobj, struct gdlm_ls, kobj);
149         struct gdlm_attr *a = container_of(attr, struct gdlm_attr, attr);
150         return a->show ? a->show(ls, buf) : 0;
151 }
152
153 static ssize_t gdlm_attr_store(struct kobject *kobj, struct attribute *attr,
154                                const char *buf, size_t len)
155 {
156         struct gdlm_ls *ls = container_of(kobj, struct gdlm_ls, kobj);
157         struct gdlm_attr *a = container_of(attr, struct gdlm_attr, attr);
158         return a->store ? a->store(ls, buf, len) : len;
159 }
160
161 static struct sysfs_ops gdlm_attr_ops = {
162         .show  = gdlm_attr_show,
163         .store = gdlm_attr_store,
164 };
165
166 static struct kobj_type gdlm_ktype = {
167         .default_attrs = gdlm_attrs,
168         .sysfs_ops     = &gdlm_attr_ops,
169 };
170
171 static struct kset gdlm_kset = {
172         .subsys = &kernel_subsys,
173         .kobj   = {.name = "lock_dlm",},
174         .ktype  = &gdlm_ktype,
175 };
176
177 int gdlm_kobject_setup(struct gdlm_ls *ls, struct kobject *fskobj)
178 {
179         int error;
180
181         error = kobject_set_name(&ls->kobj, "%s", "lock_module");
182         if (error) {
183                 log_error("can't set kobj name %d", error);
184                 return error;
185         }
186
187         ls->kobj.kset = &gdlm_kset;
188         ls->kobj.ktype = &gdlm_ktype;
189         ls->kobj.parent = fskobj;
190
191         error = kobject_register(&ls->kobj);
192         if (error)
193                 log_error("can't register kobj %d", error);
194
195         return error;
196 }
197
198 void gdlm_kobject_release(struct gdlm_ls *ls)
199 {
200         kobject_unregister(&ls->kobj);
201 }
202
203 int gdlm_sysfs_init(void)
204 {
205         int error;
206
207         error = kset_register(&gdlm_kset);
208         if (error)
209                 printk("lock_dlm: cannot register kset %d\n", error);
210
211         return error;
212 }
213
214 void gdlm_sysfs_exit(void)
215 {
216         kset_unregister(&gdlm_kset);
217 }
218