Commit | Line | Data |
---|---|---|
aebe17f6 VD |
1 | /* |
2 | * sysfs.c - sysfs support implementation. | |
3 | * | |
4 | * Copyright (C) 2005-2014 Nippon Telegraph and Telephone Corporation. | |
5 | * Copyright (C) 2014 HGST, Inc., a Western Digital Company. | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * Written by Vyacheslav Dubeyko <Vyacheslav.Dubeyko@hgst.com> | |
18 | */ | |
19 | ||
20 | #include <linux/kobject.h> | |
21 | ||
22 | #include "nilfs.h" | |
23 | #include "mdt.h" | |
24 | #include "sufile.h" | |
25 | #include "cpfile.h" | |
26 | #include "sysfs.h" | |
27 | ||
28 | /* /sys/fs/<nilfs>/ */ | |
29 | static struct kset *nilfs_kset; | |
30 | ||
31 | #define NILFS_SHOW_TIME(time_t_val, buf) ({ \ | |
32 | struct tm res; \ | |
33 | int count = 0; \ | |
34 | time_to_tm(time_t_val, 0, &res); \ | |
35 | res.tm_year += 1900; \ | |
36 | res.tm_mon += 1; \ | |
37 | count = scnprintf(buf, PAGE_SIZE, \ | |
38 | "%ld-%.2d-%.2d %.2d:%.2d:%.2d\n", \ | |
39 | res.tm_year, res.tm_mon, res.tm_mday, \ | |
40 | res.tm_hour, res.tm_min, res.tm_sec);\ | |
41 | count; \ | |
42 | }) | |
43 | ||
caa05d49 VD |
44 | #define NILFS_DEV_INT_GROUP_OPS(name, parent_name) \ |
45 | static ssize_t nilfs_##name##_attr_show(struct kobject *kobj, \ | |
46 | struct attribute *attr, char *buf) \ | |
47 | { \ | |
48 | struct the_nilfs *nilfs = container_of(kobj->parent, \ | |
49 | struct the_nilfs, \ | |
50 | ns_##parent_name##_kobj); \ | |
51 | struct nilfs_##name##_attr *a = container_of(attr, \ | |
52 | struct nilfs_##name##_attr, \ | |
53 | attr); \ | |
54 | return a->show ? a->show(a, nilfs, buf) : 0; \ | |
55 | } \ | |
56 | static ssize_t nilfs_##name##_attr_store(struct kobject *kobj, \ | |
57 | struct attribute *attr, \ | |
58 | const char *buf, size_t len) \ | |
59 | { \ | |
60 | struct the_nilfs *nilfs = container_of(kobj->parent, \ | |
61 | struct the_nilfs, \ | |
62 | ns_##parent_name##_kobj); \ | |
63 | struct nilfs_##name##_attr *a = container_of(attr, \ | |
64 | struct nilfs_##name##_attr, \ | |
65 | attr); \ | |
66 | return a->store ? a->store(a, nilfs, buf, len) : 0; \ | |
67 | } \ | |
68 | static const struct sysfs_ops nilfs_##name##_attr_ops = { \ | |
69 | .show = nilfs_##name##_attr_show, \ | |
70 | .store = nilfs_##name##_attr_store, \ | |
71 | }; | |
72 | ||
73 | #define NILFS_DEV_INT_GROUP_TYPE(name, parent_name) \ | |
74 | static void nilfs_##name##_attr_release(struct kobject *kobj) \ | |
75 | { \ | |
76 | struct nilfs_sysfs_##parent_name##_subgroups *subgroups; \ | |
77 | struct the_nilfs *nilfs = container_of(kobj->parent, \ | |
78 | struct the_nilfs, \ | |
79 | ns_##parent_name##_kobj); \ | |
80 | subgroups = nilfs->ns_##parent_name##_subgroups; \ | |
81 | complete(&subgroups->sg_##name##_kobj_unregister); \ | |
82 | } \ | |
83 | static struct kobj_type nilfs_##name##_ktype = { \ | |
84 | .default_attrs = nilfs_##name##_attrs, \ | |
85 | .sysfs_ops = &nilfs_##name##_attr_ops, \ | |
86 | .release = nilfs_##name##_attr_release, \ | |
87 | }; | |
88 | ||
89 | #define NILFS_DEV_INT_GROUP_FNS(name, parent_name) \ | |
90 | int nilfs_sysfs_create_##name##_group(struct the_nilfs *nilfs) \ | |
91 | { \ | |
92 | struct kobject *parent; \ | |
93 | struct kobject *kobj; \ | |
94 | struct completion *kobj_unregister; \ | |
95 | struct nilfs_sysfs_##parent_name##_subgroups *subgroups; \ | |
96 | int err; \ | |
97 | subgroups = nilfs->ns_##parent_name##_subgroups; \ | |
98 | kobj = &subgroups->sg_##name##_kobj; \ | |
99 | kobj_unregister = &subgroups->sg_##name##_kobj_unregister; \ | |
100 | parent = &nilfs->ns_##parent_name##_kobj; \ | |
101 | kobj->kset = nilfs_kset; \ | |
102 | init_completion(kobj_unregister); \ | |
103 | err = kobject_init_and_add(kobj, &nilfs_##name##_ktype, parent, \ | |
104 | #name); \ | |
105 | if (err) \ | |
106 | return err; \ | |
107 | return 0; \ | |
108 | } \ | |
109 | void nilfs_sysfs_delete_##name##_group(struct the_nilfs *nilfs) \ | |
110 | { \ | |
111 | kobject_del(&nilfs->ns_##parent_name##_subgroups->sg_##name##_kobj); \ | |
112 | } | |
113 | ||
abc968db VD |
114 | /************************************************************************ |
115 | * NILFS segctor attrs * | |
116 | ************************************************************************/ | |
117 | ||
118 | static ssize_t | |
119 | nilfs_segctor_last_pseg_block_show(struct nilfs_segctor_attr *attr, | |
120 | struct the_nilfs *nilfs, | |
121 | char *buf) | |
122 | { | |
123 | sector_t last_pseg; | |
124 | ||
125 | spin_lock(&nilfs->ns_last_segment_lock); | |
126 | last_pseg = nilfs->ns_last_pseg; | |
127 | spin_unlock(&nilfs->ns_last_segment_lock); | |
128 | ||
129 | return snprintf(buf, PAGE_SIZE, "%llu\n", | |
130 | (unsigned long long)last_pseg); | |
131 | } | |
132 | ||
133 | static ssize_t | |
134 | nilfs_segctor_last_seg_sequence_show(struct nilfs_segctor_attr *attr, | |
135 | struct the_nilfs *nilfs, | |
136 | char *buf) | |
137 | { | |
138 | u64 last_seq; | |
139 | ||
140 | spin_lock(&nilfs->ns_last_segment_lock); | |
141 | last_seq = nilfs->ns_last_seq; | |
142 | spin_unlock(&nilfs->ns_last_segment_lock); | |
143 | ||
144 | return snprintf(buf, PAGE_SIZE, "%llu\n", last_seq); | |
145 | } | |
146 | ||
147 | static ssize_t | |
148 | nilfs_segctor_last_seg_checkpoint_show(struct nilfs_segctor_attr *attr, | |
149 | struct the_nilfs *nilfs, | |
150 | char *buf) | |
151 | { | |
152 | __u64 last_cno; | |
153 | ||
154 | spin_lock(&nilfs->ns_last_segment_lock); | |
155 | last_cno = nilfs->ns_last_cno; | |
156 | spin_unlock(&nilfs->ns_last_segment_lock); | |
157 | ||
158 | return snprintf(buf, PAGE_SIZE, "%llu\n", last_cno); | |
159 | } | |
160 | ||
161 | static ssize_t | |
162 | nilfs_segctor_current_seg_sequence_show(struct nilfs_segctor_attr *attr, | |
163 | struct the_nilfs *nilfs, | |
164 | char *buf) | |
165 | { | |
166 | u64 seg_seq; | |
167 | ||
168 | down_read(&nilfs->ns_sem); | |
169 | seg_seq = nilfs->ns_seg_seq; | |
170 | up_read(&nilfs->ns_sem); | |
171 | ||
172 | return snprintf(buf, PAGE_SIZE, "%llu\n", seg_seq); | |
173 | } | |
174 | ||
175 | static ssize_t | |
176 | nilfs_segctor_current_last_full_seg_show(struct nilfs_segctor_attr *attr, | |
177 | struct the_nilfs *nilfs, | |
178 | char *buf) | |
179 | { | |
180 | __u64 segnum; | |
181 | ||
182 | down_read(&nilfs->ns_sem); | |
183 | segnum = nilfs->ns_segnum; | |
184 | up_read(&nilfs->ns_sem); | |
185 | ||
186 | return snprintf(buf, PAGE_SIZE, "%llu\n", segnum); | |
187 | } | |
188 | ||
189 | static ssize_t | |
190 | nilfs_segctor_next_full_seg_show(struct nilfs_segctor_attr *attr, | |
191 | struct the_nilfs *nilfs, | |
192 | char *buf) | |
193 | { | |
194 | __u64 nextnum; | |
195 | ||
196 | down_read(&nilfs->ns_sem); | |
197 | nextnum = nilfs->ns_nextnum; | |
198 | up_read(&nilfs->ns_sem); | |
199 | ||
200 | return snprintf(buf, PAGE_SIZE, "%llu\n", nextnum); | |
201 | } | |
202 | ||
203 | static ssize_t | |
204 | nilfs_segctor_next_pseg_offset_show(struct nilfs_segctor_attr *attr, | |
205 | struct the_nilfs *nilfs, | |
206 | char *buf) | |
207 | { | |
208 | unsigned long pseg_offset; | |
209 | ||
210 | down_read(&nilfs->ns_sem); | |
211 | pseg_offset = nilfs->ns_pseg_offset; | |
212 | up_read(&nilfs->ns_sem); | |
213 | ||
214 | return snprintf(buf, PAGE_SIZE, "%lu\n", pseg_offset); | |
215 | } | |
216 | ||
217 | static ssize_t | |
218 | nilfs_segctor_next_checkpoint_show(struct nilfs_segctor_attr *attr, | |
219 | struct the_nilfs *nilfs, | |
220 | char *buf) | |
221 | { | |
222 | __u64 cno; | |
223 | ||
224 | down_read(&nilfs->ns_sem); | |
225 | cno = nilfs->ns_cno; | |
226 | up_read(&nilfs->ns_sem); | |
227 | ||
228 | return snprintf(buf, PAGE_SIZE, "%llu\n", cno); | |
229 | } | |
230 | ||
231 | static ssize_t | |
232 | nilfs_segctor_last_seg_write_time_show(struct nilfs_segctor_attr *attr, | |
233 | struct the_nilfs *nilfs, | |
234 | char *buf) | |
235 | { | |
236 | time_t ctime; | |
237 | ||
238 | down_read(&nilfs->ns_sem); | |
239 | ctime = nilfs->ns_ctime; | |
240 | up_read(&nilfs->ns_sem); | |
241 | ||
242 | return NILFS_SHOW_TIME(ctime, buf); | |
243 | } | |
244 | ||
245 | static ssize_t | |
246 | nilfs_segctor_last_seg_write_time_secs_show(struct nilfs_segctor_attr *attr, | |
247 | struct the_nilfs *nilfs, | |
248 | char *buf) | |
249 | { | |
250 | time_t ctime; | |
251 | ||
252 | down_read(&nilfs->ns_sem); | |
253 | ctime = nilfs->ns_ctime; | |
254 | up_read(&nilfs->ns_sem); | |
255 | ||
256 | return snprintf(buf, PAGE_SIZE, "%llu\n", (unsigned long long)ctime); | |
257 | } | |
258 | ||
259 | static ssize_t | |
260 | nilfs_segctor_last_nongc_write_time_show(struct nilfs_segctor_attr *attr, | |
261 | struct the_nilfs *nilfs, | |
262 | char *buf) | |
263 | { | |
264 | time_t nongc_ctime; | |
265 | ||
266 | down_read(&nilfs->ns_sem); | |
267 | nongc_ctime = nilfs->ns_nongc_ctime; | |
268 | up_read(&nilfs->ns_sem); | |
269 | ||
270 | return NILFS_SHOW_TIME(nongc_ctime, buf); | |
271 | } | |
272 | ||
273 | static ssize_t | |
274 | nilfs_segctor_last_nongc_write_time_secs_show(struct nilfs_segctor_attr *attr, | |
275 | struct the_nilfs *nilfs, | |
276 | char *buf) | |
277 | { | |
278 | time_t nongc_ctime; | |
279 | ||
280 | down_read(&nilfs->ns_sem); | |
281 | nongc_ctime = nilfs->ns_nongc_ctime; | |
282 | up_read(&nilfs->ns_sem); | |
283 | ||
284 | return snprintf(buf, PAGE_SIZE, "%llu\n", | |
285 | (unsigned long long)nongc_ctime); | |
286 | } | |
287 | ||
288 | static ssize_t | |
289 | nilfs_segctor_dirty_data_blocks_count_show(struct nilfs_segctor_attr *attr, | |
290 | struct the_nilfs *nilfs, | |
291 | char *buf) | |
292 | { | |
293 | u32 ndirtyblks; | |
294 | ||
295 | down_read(&nilfs->ns_sem); | |
296 | ndirtyblks = atomic_read(&nilfs->ns_ndirtyblks); | |
297 | up_read(&nilfs->ns_sem); | |
298 | ||
299 | return snprintf(buf, PAGE_SIZE, "%u\n", ndirtyblks); | |
300 | } | |
301 | ||
302 | static const char segctor_readme_str[] = | |
303 | "The segctor group contains attributes that describe\n" | |
304 | "segctor thread activity details.\n\n" | |
305 | "(1) last_pseg_block\n" | |
306 | "\tshow start block number of the latest segment.\n\n" | |
307 | "(2) last_seg_sequence\n" | |
308 | "\tshow sequence value of the latest segment.\n\n" | |
309 | "(3) last_seg_checkpoint\n" | |
310 | "\tshow checkpoint number of the latest segment.\n\n" | |
311 | "(4) current_seg_sequence\n\tshow segment sequence counter.\n\n" | |
312 | "(5) current_last_full_seg\n" | |
313 | "\tshow index number of the latest full segment.\n\n" | |
314 | "(6) next_full_seg\n" | |
315 | "\tshow index number of the full segment index to be used next.\n\n" | |
316 | "(7) next_pseg_offset\n" | |
317 | "\tshow offset of next partial segment in the current full segment.\n\n" | |
318 | "(8) next_checkpoint\n\tshow next checkpoint number.\n\n" | |
319 | "(9) last_seg_write_time\n" | |
320 | "\tshow write time of the last segment in human-readable format.\n\n" | |
321 | "(10) last_seg_write_time_secs\n" | |
322 | "\tshow write time of the last segment in seconds.\n\n" | |
323 | "(11) last_nongc_write_time\n" | |
324 | "\tshow write time of the last segment not for cleaner operation " | |
325 | "in human-readable format.\n\n" | |
326 | "(12) last_nongc_write_time_secs\n" | |
327 | "\tshow write time of the last segment not for cleaner operation " | |
328 | "in seconds.\n\n" | |
329 | "(13) dirty_data_blocks_count\n" | |
330 | "\tshow number of dirty data blocks.\n\n"; | |
331 | ||
332 | static ssize_t | |
333 | nilfs_segctor_README_show(struct nilfs_segctor_attr *attr, | |
334 | struct the_nilfs *nilfs, char *buf) | |
335 | { | |
336 | return snprintf(buf, PAGE_SIZE, segctor_readme_str); | |
337 | } | |
338 | ||
339 | NILFS_SEGCTOR_RO_ATTR(last_pseg_block); | |
340 | NILFS_SEGCTOR_RO_ATTR(last_seg_sequence); | |
341 | NILFS_SEGCTOR_RO_ATTR(last_seg_checkpoint); | |
342 | NILFS_SEGCTOR_RO_ATTR(current_seg_sequence); | |
343 | NILFS_SEGCTOR_RO_ATTR(current_last_full_seg); | |
344 | NILFS_SEGCTOR_RO_ATTR(next_full_seg); | |
345 | NILFS_SEGCTOR_RO_ATTR(next_pseg_offset); | |
346 | NILFS_SEGCTOR_RO_ATTR(next_checkpoint); | |
347 | NILFS_SEGCTOR_RO_ATTR(last_seg_write_time); | |
348 | NILFS_SEGCTOR_RO_ATTR(last_seg_write_time_secs); | |
349 | NILFS_SEGCTOR_RO_ATTR(last_nongc_write_time); | |
350 | NILFS_SEGCTOR_RO_ATTR(last_nongc_write_time_secs); | |
351 | NILFS_SEGCTOR_RO_ATTR(dirty_data_blocks_count); | |
352 | NILFS_SEGCTOR_RO_ATTR(README); | |
353 | ||
354 | static struct attribute *nilfs_segctor_attrs[] = { | |
355 | NILFS_SEGCTOR_ATTR_LIST(last_pseg_block), | |
356 | NILFS_SEGCTOR_ATTR_LIST(last_seg_sequence), | |
357 | NILFS_SEGCTOR_ATTR_LIST(last_seg_checkpoint), | |
358 | NILFS_SEGCTOR_ATTR_LIST(current_seg_sequence), | |
359 | NILFS_SEGCTOR_ATTR_LIST(current_last_full_seg), | |
360 | NILFS_SEGCTOR_ATTR_LIST(next_full_seg), | |
361 | NILFS_SEGCTOR_ATTR_LIST(next_pseg_offset), | |
362 | NILFS_SEGCTOR_ATTR_LIST(next_checkpoint), | |
363 | NILFS_SEGCTOR_ATTR_LIST(last_seg_write_time), | |
364 | NILFS_SEGCTOR_ATTR_LIST(last_seg_write_time_secs), | |
365 | NILFS_SEGCTOR_ATTR_LIST(last_nongc_write_time), | |
366 | NILFS_SEGCTOR_ATTR_LIST(last_nongc_write_time_secs), | |
367 | NILFS_SEGCTOR_ATTR_LIST(dirty_data_blocks_count), | |
368 | NILFS_SEGCTOR_ATTR_LIST(README), | |
369 | NULL, | |
370 | }; | |
371 | ||
372 | NILFS_DEV_INT_GROUP_OPS(segctor, dev); | |
373 | NILFS_DEV_INT_GROUP_TYPE(segctor, dev); | |
374 | NILFS_DEV_INT_GROUP_FNS(segctor, dev); | |
375 | ||
caa05d49 VD |
376 | /************************************************************************ |
377 | * NILFS superblock attrs * | |
378 | ************************************************************************/ | |
379 | ||
380 | static ssize_t | |
381 | nilfs_superblock_sb_write_time_show(struct nilfs_superblock_attr *attr, | |
382 | struct the_nilfs *nilfs, | |
383 | char *buf) | |
384 | { | |
385 | time_t sbwtime; | |
386 | ||
387 | down_read(&nilfs->ns_sem); | |
388 | sbwtime = nilfs->ns_sbwtime; | |
389 | up_read(&nilfs->ns_sem); | |
390 | ||
391 | return NILFS_SHOW_TIME(sbwtime, buf); | |
392 | } | |
393 | ||
394 | static ssize_t | |
395 | nilfs_superblock_sb_write_time_secs_show(struct nilfs_superblock_attr *attr, | |
396 | struct the_nilfs *nilfs, | |
397 | char *buf) | |
398 | { | |
399 | time_t sbwtime; | |
400 | ||
401 | down_read(&nilfs->ns_sem); | |
402 | sbwtime = nilfs->ns_sbwtime; | |
403 | up_read(&nilfs->ns_sem); | |
404 | ||
405 | return snprintf(buf, PAGE_SIZE, "%llu\n", (unsigned long long)sbwtime); | |
406 | } | |
407 | ||
408 | static ssize_t | |
409 | nilfs_superblock_sb_write_count_show(struct nilfs_superblock_attr *attr, | |
410 | struct the_nilfs *nilfs, | |
411 | char *buf) | |
412 | { | |
413 | unsigned sbwcount; | |
414 | ||
415 | down_read(&nilfs->ns_sem); | |
416 | sbwcount = nilfs->ns_sbwcount; | |
417 | up_read(&nilfs->ns_sem); | |
418 | ||
419 | return snprintf(buf, PAGE_SIZE, "%u\n", sbwcount); | |
420 | } | |
421 | ||
422 | static ssize_t | |
423 | nilfs_superblock_sb_update_frequency_show(struct nilfs_superblock_attr *attr, | |
424 | struct the_nilfs *nilfs, | |
425 | char *buf) | |
426 | { | |
427 | unsigned sb_update_freq; | |
428 | ||
429 | down_read(&nilfs->ns_sem); | |
430 | sb_update_freq = nilfs->ns_sb_update_freq; | |
431 | up_read(&nilfs->ns_sem); | |
432 | ||
433 | return snprintf(buf, PAGE_SIZE, "%u\n", sb_update_freq); | |
434 | } | |
435 | ||
436 | static ssize_t | |
437 | nilfs_superblock_sb_update_frequency_store(struct nilfs_superblock_attr *attr, | |
438 | struct the_nilfs *nilfs, | |
439 | const char *buf, size_t count) | |
440 | { | |
441 | unsigned val; | |
442 | int err; | |
443 | ||
444 | err = kstrtouint(skip_spaces(buf), 0, &val); | |
445 | if (err) { | |
446 | printk(KERN_ERR "NILFS: unable to convert string: err=%d\n", | |
447 | err); | |
448 | return err; | |
449 | } | |
450 | ||
451 | if (val < NILFS_SB_FREQ) { | |
452 | val = NILFS_SB_FREQ; | |
453 | printk(KERN_WARNING "NILFS: superblock update frequency cannot be lesser than 10 seconds\n"); | |
454 | } | |
455 | ||
456 | down_write(&nilfs->ns_sem); | |
457 | nilfs->ns_sb_update_freq = val; | |
458 | up_write(&nilfs->ns_sem); | |
459 | ||
460 | return count; | |
461 | } | |
462 | ||
463 | static const char sb_readme_str[] = | |
464 | "The superblock group contains attributes that describe\n" | |
465 | "superblock's details.\n\n" | |
466 | "(1) sb_write_time\n\tshow previous write time of super block " | |
467 | "in human-readable format.\n\n" | |
468 | "(2) sb_write_time_secs\n\tshow previous write time of super block " | |
469 | "in seconds.\n\n" | |
470 | "(3) sb_write_count\n\tshow write count of super block.\n\n" | |
471 | "(4) sb_update_frequency\n" | |
472 | "\tshow/set interval of periodical update of superblock (in seconds).\n\n" | |
473 | "\tYou can set preferable frequency of superblock update by command:\n\n" | |
474 | "\t'echo <val> > /sys/fs/<nilfs>/<dev>/superblock/sb_update_frequency'\n"; | |
475 | ||
476 | static ssize_t | |
477 | nilfs_superblock_README_show(struct nilfs_superblock_attr *attr, | |
478 | struct the_nilfs *nilfs, char *buf) | |
479 | { | |
480 | return snprintf(buf, PAGE_SIZE, sb_readme_str); | |
481 | } | |
482 | ||
483 | NILFS_SUPERBLOCK_RO_ATTR(sb_write_time); | |
484 | NILFS_SUPERBLOCK_RO_ATTR(sb_write_time_secs); | |
485 | NILFS_SUPERBLOCK_RO_ATTR(sb_write_count); | |
486 | NILFS_SUPERBLOCK_RW_ATTR(sb_update_frequency); | |
487 | NILFS_SUPERBLOCK_RO_ATTR(README); | |
488 | ||
489 | static struct attribute *nilfs_superblock_attrs[] = { | |
490 | NILFS_SUPERBLOCK_ATTR_LIST(sb_write_time), | |
491 | NILFS_SUPERBLOCK_ATTR_LIST(sb_write_time_secs), | |
492 | NILFS_SUPERBLOCK_ATTR_LIST(sb_write_count), | |
493 | NILFS_SUPERBLOCK_ATTR_LIST(sb_update_frequency), | |
494 | NILFS_SUPERBLOCK_ATTR_LIST(README), | |
495 | NULL, | |
496 | }; | |
497 | ||
498 | NILFS_DEV_INT_GROUP_OPS(superblock, dev); | |
499 | NILFS_DEV_INT_GROUP_TYPE(superblock, dev); | |
500 | NILFS_DEV_INT_GROUP_FNS(superblock, dev); | |
501 | ||
da7141fb VD |
502 | /************************************************************************ |
503 | * NILFS device attrs * | |
504 | ************************************************************************/ | |
505 | ||
506 | static | |
507 | ssize_t nilfs_dev_revision_show(struct nilfs_dev_attr *attr, | |
508 | struct the_nilfs *nilfs, | |
509 | char *buf) | |
510 | { | |
511 | struct nilfs_super_block **sbp = nilfs->ns_sbp; | |
512 | u32 major = le32_to_cpu(sbp[0]->s_rev_level); | |
513 | u16 minor = le16_to_cpu(sbp[0]->s_minor_rev_level); | |
514 | ||
515 | return snprintf(buf, PAGE_SIZE, "%d.%d\n", major, minor); | |
516 | } | |
517 | ||
518 | static | |
519 | ssize_t nilfs_dev_blocksize_show(struct nilfs_dev_attr *attr, | |
520 | struct the_nilfs *nilfs, | |
521 | char *buf) | |
522 | { | |
523 | return snprintf(buf, PAGE_SIZE, "%u\n", nilfs->ns_blocksize); | |
524 | } | |
525 | ||
526 | static | |
527 | ssize_t nilfs_dev_device_size_show(struct nilfs_dev_attr *attr, | |
528 | struct the_nilfs *nilfs, | |
529 | char *buf) | |
530 | { | |
531 | struct nilfs_super_block **sbp = nilfs->ns_sbp; | |
532 | u64 dev_size = le64_to_cpu(sbp[0]->s_dev_size); | |
533 | ||
534 | return snprintf(buf, PAGE_SIZE, "%llu\n", dev_size); | |
535 | } | |
536 | ||
537 | static | |
538 | ssize_t nilfs_dev_free_blocks_show(struct nilfs_dev_attr *attr, | |
539 | struct the_nilfs *nilfs, | |
540 | char *buf) | |
541 | { | |
542 | sector_t free_blocks = 0; | |
543 | ||
544 | nilfs_count_free_blocks(nilfs, &free_blocks); | |
545 | return snprintf(buf, PAGE_SIZE, "%llu\n", | |
546 | (unsigned long long)free_blocks); | |
547 | } | |
548 | ||
549 | static | |
550 | ssize_t nilfs_dev_uuid_show(struct nilfs_dev_attr *attr, | |
551 | struct the_nilfs *nilfs, | |
552 | char *buf) | |
553 | { | |
554 | struct nilfs_super_block **sbp = nilfs->ns_sbp; | |
555 | ||
556 | return snprintf(buf, PAGE_SIZE, "%pUb\n", sbp[0]->s_uuid); | |
557 | } | |
558 | ||
559 | static | |
560 | ssize_t nilfs_dev_volume_name_show(struct nilfs_dev_attr *attr, | |
561 | struct the_nilfs *nilfs, | |
562 | char *buf) | |
563 | { | |
564 | struct nilfs_super_block **sbp = nilfs->ns_sbp; | |
565 | ||
566 | return scnprintf(buf, sizeof(sbp[0]->s_volume_name), "%s\n", | |
567 | sbp[0]->s_volume_name); | |
568 | } | |
569 | ||
570 | static const char dev_readme_str[] = | |
571 | "The <device> group contains attributes that describe file system\n" | |
572 | "partition's details.\n\n" | |
573 | "(1) revision\n\tshow NILFS file system revision.\n\n" | |
574 | "(2) blocksize\n\tshow volume block size in bytes.\n\n" | |
575 | "(3) device_size\n\tshow volume size in bytes.\n\n" | |
576 | "(4) free_blocks\n\tshow count of free blocks on volume.\n\n" | |
577 | "(5) uuid\n\tshow volume's UUID.\n\n" | |
578 | "(6) volume_name\n\tshow volume's name.\n\n"; | |
579 | ||
580 | static ssize_t nilfs_dev_README_show(struct nilfs_dev_attr *attr, | |
581 | struct the_nilfs *nilfs, | |
582 | char *buf) | |
583 | { | |
584 | return snprintf(buf, PAGE_SIZE, dev_readme_str); | |
585 | } | |
586 | ||
587 | NILFS_DEV_RO_ATTR(revision); | |
588 | NILFS_DEV_RO_ATTR(blocksize); | |
589 | NILFS_DEV_RO_ATTR(device_size); | |
590 | NILFS_DEV_RO_ATTR(free_blocks); | |
591 | NILFS_DEV_RO_ATTR(uuid); | |
592 | NILFS_DEV_RO_ATTR(volume_name); | |
593 | NILFS_DEV_RO_ATTR(README); | |
594 | ||
595 | static struct attribute *nilfs_dev_attrs[] = { | |
596 | NILFS_DEV_ATTR_LIST(revision), | |
597 | NILFS_DEV_ATTR_LIST(blocksize), | |
598 | NILFS_DEV_ATTR_LIST(device_size), | |
599 | NILFS_DEV_ATTR_LIST(free_blocks), | |
600 | NILFS_DEV_ATTR_LIST(uuid), | |
601 | NILFS_DEV_ATTR_LIST(volume_name), | |
602 | NILFS_DEV_ATTR_LIST(README), | |
603 | NULL, | |
604 | }; | |
605 | ||
606 | static ssize_t nilfs_dev_attr_show(struct kobject *kobj, | |
607 | struct attribute *attr, char *buf) | |
608 | { | |
609 | struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs, | |
610 | ns_dev_kobj); | |
611 | struct nilfs_dev_attr *a = container_of(attr, struct nilfs_dev_attr, | |
612 | attr); | |
613 | ||
614 | return a->show ? a->show(a, nilfs, buf) : 0; | |
615 | } | |
616 | ||
617 | static ssize_t nilfs_dev_attr_store(struct kobject *kobj, | |
618 | struct attribute *attr, | |
619 | const char *buf, size_t len) | |
620 | { | |
621 | struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs, | |
622 | ns_dev_kobj); | |
623 | struct nilfs_dev_attr *a = container_of(attr, struct nilfs_dev_attr, | |
624 | attr); | |
625 | ||
626 | return a->store ? a->store(a, nilfs, buf, len) : 0; | |
627 | } | |
628 | ||
629 | static void nilfs_dev_attr_release(struct kobject *kobj) | |
630 | { | |
631 | struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs, | |
632 | ns_dev_kobj); | |
633 | complete(&nilfs->ns_dev_kobj_unregister); | |
634 | } | |
635 | ||
636 | static const struct sysfs_ops nilfs_dev_attr_ops = { | |
637 | .show = nilfs_dev_attr_show, | |
638 | .store = nilfs_dev_attr_store, | |
639 | }; | |
640 | ||
641 | static struct kobj_type nilfs_dev_ktype = { | |
642 | .default_attrs = nilfs_dev_attrs, | |
643 | .sysfs_ops = &nilfs_dev_attr_ops, | |
644 | .release = nilfs_dev_attr_release, | |
645 | }; | |
646 | ||
647 | int nilfs_sysfs_create_device_group(struct super_block *sb) | |
648 | { | |
649 | struct the_nilfs *nilfs = sb->s_fs_info; | |
caa05d49 | 650 | size_t devgrp_size = sizeof(struct nilfs_sysfs_dev_subgroups); |
da7141fb VD |
651 | int err; |
652 | ||
caa05d49 VD |
653 | nilfs->ns_dev_subgroups = kzalloc(devgrp_size, GFP_KERNEL); |
654 | if (unlikely(!nilfs->ns_dev_subgroups)) { | |
655 | err = -ENOMEM; | |
656 | printk(KERN_ERR "NILFS: unable to allocate memory for device group\n"); | |
657 | goto failed_create_device_group; | |
658 | } | |
659 | ||
da7141fb VD |
660 | nilfs->ns_dev_kobj.kset = nilfs_kset; |
661 | init_completion(&nilfs->ns_dev_kobj_unregister); | |
662 | err = kobject_init_and_add(&nilfs->ns_dev_kobj, &nilfs_dev_ktype, NULL, | |
663 | "%s", sb->s_id); | |
664 | if (err) | |
caa05d49 VD |
665 | goto free_dev_subgroups; |
666 | ||
667 | err = nilfs_sysfs_create_superblock_group(nilfs); | |
668 | if (err) | |
669 | goto cleanup_dev_kobject; | |
da7141fb | 670 | |
abc968db VD |
671 | err = nilfs_sysfs_create_segctor_group(nilfs); |
672 | if (err) | |
673 | goto delete_superblock_group; | |
674 | ||
da7141fb VD |
675 | return 0; |
676 | ||
abc968db VD |
677 | delete_superblock_group: |
678 | nilfs_sysfs_delete_superblock_group(nilfs); | |
679 | ||
caa05d49 VD |
680 | cleanup_dev_kobject: |
681 | kobject_del(&nilfs->ns_dev_kobj); | |
682 | ||
683 | free_dev_subgroups: | |
684 | kfree(nilfs->ns_dev_subgroups); | |
685 | ||
da7141fb VD |
686 | failed_create_device_group: |
687 | return err; | |
688 | } | |
689 | ||
690 | void nilfs_sysfs_delete_device_group(struct the_nilfs *nilfs) | |
691 | { | |
caa05d49 | 692 | nilfs_sysfs_delete_superblock_group(nilfs); |
abc968db | 693 | nilfs_sysfs_delete_segctor_group(nilfs); |
da7141fb | 694 | kobject_del(&nilfs->ns_dev_kobj); |
caa05d49 | 695 | kfree(nilfs->ns_dev_subgroups); |
da7141fb VD |
696 | } |
697 | ||
aebe17f6 VD |
698 | /************************************************************************ |
699 | * NILFS feature attrs * | |
700 | ************************************************************************/ | |
701 | ||
702 | static ssize_t nilfs_feature_revision_show(struct kobject *kobj, | |
703 | struct attribute *attr, char *buf) | |
704 | { | |
705 | return snprintf(buf, PAGE_SIZE, "%d.%d\n", | |
706 | NILFS_CURRENT_REV, NILFS_MINOR_REV); | |
707 | } | |
708 | ||
709 | static const char features_readme_str[] = | |
710 | "The features group contains attributes that describe NILFS file\n" | |
711 | "system driver features.\n\n" | |
712 | "(1) revision\n\tshow current revision of NILFS file system driver.\n"; | |
713 | ||
714 | static ssize_t nilfs_feature_README_show(struct kobject *kobj, | |
715 | struct attribute *attr, | |
716 | char *buf) | |
717 | { | |
718 | return snprintf(buf, PAGE_SIZE, features_readme_str); | |
719 | } | |
720 | ||
721 | NILFS_FEATURE_RO_ATTR(revision); | |
722 | NILFS_FEATURE_RO_ATTR(README); | |
723 | ||
724 | static struct attribute *nilfs_feature_attrs[] = { | |
725 | NILFS_FEATURE_ATTR_LIST(revision), | |
726 | NILFS_FEATURE_ATTR_LIST(README), | |
727 | NULL, | |
728 | }; | |
729 | ||
730 | static const struct attribute_group nilfs_feature_attr_group = { | |
731 | .name = "features", | |
732 | .attrs = nilfs_feature_attrs, | |
733 | }; | |
734 | ||
735 | int __init nilfs_sysfs_init(void) | |
736 | { | |
737 | int err; | |
738 | ||
739 | nilfs_kset = kset_create_and_add(NILFS_ROOT_GROUP_NAME, NULL, fs_kobj); | |
740 | if (!nilfs_kset) { | |
741 | err = -ENOMEM; | |
742 | printk(KERN_ERR "NILFS: unable to create sysfs entry: err %d\n", | |
743 | err); | |
744 | goto failed_sysfs_init; | |
745 | } | |
746 | ||
747 | err = sysfs_create_group(&nilfs_kset->kobj, &nilfs_feature_attr_group); | |
748 | if (unlikely(err)) { | |
749 | printk(KERN_ERR "NILFS: unable to create feature group: err %d\n", | |
750 | err); | |
751 | goto cleanup_sysfs_init; | |
752 | } | |
753 | ||
754 | return 0; | |
755 | ||
756 | cleanup_sysfs_init: | |
757 | kset_unregister(nilfs_kset); | |
758 | ||
759 | failed_sysfs_init: | |
760 | return err; | |
761 | } | |
762 | ||
763 | void nilfs_sysfs_exit(void) | |
764 | { | |
765 | sysfs_remove_group(&nilfs_kset->kobj, &nilfs_feature_attr_group); | |
766 | kset_unregister(nilfs_kset); | |
767 | } |