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 static ssize_t recover_status_show(struct gdlm_ls *ls, char *buf)
114 {
115         return sprintf(buf, "%d\n", ls->recover_jid_status);
116 }
117
118 struct gdlm_attr {
119         struct attribute attr;
120         ssize_t (*show)(struct gdlm_ls *, char *);
121         ssize_t (*store)(struct gdlm_ls *, const char *, size_t);
122 };
123
124 #define GDLM_ATTR(_name,_mode,_show,_store) \
125 static struct gdlm_attr gdlm_attr_##_name = __ATTR(_name,_mode,_show,_store)
126
127 GDLM_ATTR(proto_name,     0444, proto_name_show,     NULL);
128 GDLM_ATTR(block,          0644, block_show,          block_store);
129 GDLM_ATTR(withdraw,       0644, withdraw_show,       withdraw_store);
130 GDLM_ATTR(id,             0444, id_show,             NULL);
131 GDLM_ATTR(jid,            0444, jid_show,            NULL);
132 GDLM_ATTR(first,          0444, first_show,          NULL);
133 GDLM_ATTR(first_done,     0444, first_done_show,     NULL);
134 GDLM_ATTR(recover,        0644, recover_show,        recover_store);
135 GDLM_ATTR(recover_done,   0444, recover_done_show,   NULL);
136 GDLM_ATTR(recover_status, 0444, recover_status_show, NULL);
137
138 static struct attribute *gdlm_attrs[] = {
139         &gdlm_attr_proto_name.attr,
140         &gdlm_attr_block.attr,
141         &gdlm_attr_withdraw.attr,
142         &gdlm_attr_id.attr,
143         &gdlm_attr_jid.attr,
144         &gdlm_attr_first.attr,
145         &gdlm_attr_first_done.attr,
146         &gdlm_attr_recover.attr,
147         &gdlm_attr_recover_done.attr,
148         &gdlm_attr_recover_status.attr,
149         NULL,
150 };
151
152 static ssize_t gdlm_attr_show(struct kobject *kobj, struct attribute *attr,
153                               char *buf)
154 {
155         struct gdlm_ls *ls = container_of(kobj, struct gdlm_ls, kobj);
156         struct gdlm_attr *a = container_of(attr, struct gdlm_attr, attr);
157         return a->show ? a->show(ls, buf) : 0;
158 }
159
160 static ssize_t gdlm_attr_store(struct kobject *kobj, struct attribute *attr,
161                                const char *buf, size_t len)
162 {
163         struct gdlm_ls *ls = container_of(kobj, struct gdlm_ls, kobj);
164         struct gdlm_attr *a = container_of(attr, struct gdlm_attr, attr);
165         return a->store ? a->store(ls, buf, len) : len;
166 }
167
168 static struct sysfs_ops gdlm_attr_ops = {
169         .show  = gdlm_attr_show,
170         .store = gdlm_attr_store,
171 };
172
173 static struct kobj_type gdlm_ktype = {
174         .default_attrs = gdlm_attrs,
175         .sysfs_ops     = &gdlm_attr_ops,
176 };
177
178 static struct kset gdlm_kset = {
179         .subsys = &kernel_subsys,
180         .kobj   = {.name = "lock_dlm",},
181         .ktype  = &gdlm_ktype,
182 };
183
184 int gdlm_kobject_setup(struct gdlm_ls *ls, struct kobject *fskobj)
185 {
186         int error;
187
188         error = kobject_set_name(&ls->kobj, "%s", "lock_module");
189         if (error) {
190                 log_error("can't set kobj name %d", error);
191                 return error;
192         }
193
194         ls->kobj.kset = &gdlm_kset;
195         ls->kobj.ktype = &gdlm_ktype;
196         ls->kobj.parent = fskobj;
197
198         error = kobject_register(&ls->kobj);
199         if (error)
200                 log_error("can't register kobj %d", error);
201
202         return error;
203 }
204
205 void gdlm_kobject_release(struct gdlm_ls *ls)
206 {
207         kobject_unregister(&ls->kobj);
208 }
209
210 int gdlm_sysfs_init(void)
211 {
212         int error;
213
214         error = kset_register(&gdlm_kset);
215         if (error)
216                 printk("lock_dlm: cannot register kset %d\n", error);
217
218         return error;
219 }
220
221 void gdlm_sysfs_exit(void)
222 {
223         kset_unregister(&gdlm_kset);
224 }
225