Loading...
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Sysfs support implementation.
4 *
5 * Copyright (C) 2005-2014 Nippon Telegraph and Telephone Corporation.
6 * Copyright (C) 2014 HGST, Inc., a Western Digital Company.
7 *
8 * Written by Vyacheslav Dubeyko <Vyacheslav.Dubeyko@hgst.com>
9 */
10
11#include <linux/kobject.h>
12
13#include "nilfs.h"
14#include "mdt.h"
15#include "sufile.h"
16#include "cpfile.h"
17#include "sysfs.h"
18
19/* /sys/fs/<nilfs>/ */
20static struct kset *nilfs_kset;
21
22#define NILFS_DEV_INT_GROUP_OPS(name, parent_name) \
23static ssize_t nilfs_##name##_attr_show(struct kobject *kobj, \
24 struct attribute *attr, char *buf) \
25{ \
26 struct the_nilfs *nilfs = container_of(kobj->parent, \
27 struct the_nilfs, \
28 ns_##parent_name##_kobj); \
29 struct nilfs_##name##_attr *a = container_of(attr, \
30 struct nilfs_##name##_attr, \
31 attr); \
32 return a->show ? a->show(a, nilfs, buf) : 0; \
33} \
34static ssize_t nilfs_##name##_attr_store(struct kobject *kobj, \
35 struct attribute *attr, \
36 const char *buf, size_t len) \
37{ \
38 struct the_nilfs *nilfs = container_of(kobj->parent, \
39 struct the_nilfs, \
40 ns_##parent_name##_kobj); \
41 struct nilfs_##name##_attr *a = container_of(attr, \
42 struct nilfs_##name##_attr, \
43 attr); \
44 return a->store ? a->store(a, nilfs, buf, len) : 0; \
45} \
46static const struct sysfs_ops nilfs_##name##_attr_ops = { \
47 .show = nilfs_##name##_attr_show, \
48 .store = nilfs_##name##_attr_store, \
49}
50
51#define NILFS_DEV_INT_GROUP_TYPE(name, parent_name) \
52static void nilfs_##name##_attr_release(struct kobject *kobj) \
53{ \
54 struct nilfs_sysfs_##parent_name##_subgroups *subgroups = container_of(kobj, \
55 struct nilfs_sysfs_##parent_name##_subgroups, \
56 sg_##name##_kobj); \
57 complete(&subgroups->sg_##name##_kobj_unregister); \
58} \
59static struct kobj_type nilfs_##name##_ktype = { \
60 .default_groups = nilfs_##name##_groups, \
61 .sysfs_ops = &nilfs_##name##_attr_ops, \
62 .release = nilfs_##name##_attr_release, \
63}
64
65#define NILFS_DEV_INT_GROUP_FNS(name, parent_name) \
66static int nilfs_sysfs_create_##name##_group(struct the_nilfs *nilfs) \
67{ \
68 struct kobject *parent; \
69 struct kobject *kobj; \
70 struct completion *kobj_unregister; \
71 struct nilfs_sysfs_##parent_name##_subgroups *subgroups; \
72 int err; \
73 subgroups = nilfs->ns_##parent_name##_subgroups; \
74 kobj = &subgroups->sg_##name##_kobj; \
75 kobj_unregister = &subgroups->sg_##name##_kobj_unregister; \
76 parent = &nilfs->ns_##parent_name##_kobj; \
77 kobj->kset = nilfs_kset; \
78 init_completion(kobj_unregister); \
79 err = kobject_init_and_add(kobj, &nilfs_##name##_ktype, parent, \
80 #name); \
81 if (err) \
82 kobject_put(kobj); \
83 return err; \
84} \
85static void nilfs_sysfs_delete_##name##_group(struct the_nilfs *nilfs) \
86{ \
87 kobject_put(&nilfs->ns_##parent_name##_subgroups->sg_##name##_kobj); \
88}
89
90/************************************************************************
91 * NILFS snapshot attrs *
92 ************************************************************************/
93
94static ssize_t
95nilfs_snapshot_inodes_count_show(struct nilfs_snapshot_attr *attr,
96 struct nilfs_root *root, char *buf)
97{
98 return sysfs_emit(buf, "%llu\n",
99 (unsigned long long)atomic64_read(&root->inodes_count));
100}
101
102static ssize_t
103nilfs_snapshot_blocks_count_show(struct nilfs_snapshot_attr *attr,
104 struct nilfs_root *root, char *buf)
105{
106 return sysfs_emit(buf, "%llu\n",
107 (unsigned long long)atomic64_read(&root->blocks_count));
108}
109
110static const char snapshot_readme_str[] =
111 "The group contains details about mounted snapshot.\n\n"
112 "(1) inodes_count\n\tshow number of inodes for snapshot.\n\n"
113 "(2) blocks_count\n\tshow number of blocks for snapshot.\n\n";
114
115static ssize_t
116nilfs_snapshot_README_show(struct nilfs_snapshot_attr *attr,
117 struct nilfs_root *root, char *buf)
118{
119 return sysfs_emit(buf, snapshot_readme_str);
120}
121
122NILFS_SNAPSHOT_RO_ATTR(inodes_count);
123NILFS_SNAPSHOT_RO_ATTR(blocks_count);
124NILFS_SNAPSHOT_RO_ATTR(README);
125
126static struct attribute *nilfs_snapshot_attrs[] = {
127 NILFS_SNAPSHOT_ATTR_LIST(inodes_count),
128 NILFS_SNAPSHOT_ATTR_LIST(blocks_count),
129 NILFS_SNAPSHOT_ATTR_LIST(README),
130 NULL,
131};
132ATTRIBUTE_GROUPS(nilfs_snapshot);
133
134static ssize_t nilfs_snapshot_attr_show(struct kobject *kobj,
135 struct attribute *attr, char *buf)
136{
137 struct nilfs_root *root =
138 container_of(kobj, struct nilfs_root, snapshot_kobj);
139 struct nilfs_snapshot_attr *a =
140 container_of(attr, struct nilfs_snapshot_attr, attr);
141
142 return a->show ? a->show(a, root, buf) : 0;
143}
144
145static ssize_t nilfs_snapshot_attr_store(struct kobject *kobj,
146 struct attribute *attr,
147 const char *buf, size_t len)
148{
149 struct nilfs_root *root =
150 container_of(kobj, struct nilfs_root, snapshot_kobj);
151 struct nilfs_snapshot_attr *a =
152 container_of(attr, struct nilfs_snapshot_attr, attr);
153
154 return a->store ? a->store(a, root, buf, len) : 0;
155}
156
157static void nilfs_snapshot_attr_release(struct kobject *kobj)
158{
159 struct nilfs_root *root = container_of(kobj, struct nilfs_root,
160 snapshot_kobj);
161 complete(&root->snapshot_kobj_unregister);
162}
163
164static const struct sysfs_ops nilfs_snapshot_attr_ops = {
165 .show = nilfs_snapshot_attr_show,
166 .store = nilfs_snapshot_attr_store,
167};
168
169static struct kobj_type nilfs_snapshot_ktype = {
170 .default_groups = nilfs_snapshot_groups,
171 .sysfs_ops = &nilfs_snapshot_attr_ops,
172 .release = nilfs_snapshot_attr_release,
173};
174
175int nilfs_sysfs_create_snapshot_group(struct nilfs_root *root)
176{
177 struct the_nilfs *nilfs;
178 struct kobject *parent;
179 int err;
180
181 nilfs = root->nilfs;
182 parent = &nilfs->ns_dev_subgroups->sg_mounted_snapshots_kobj;
183 root->snapshot_kobj.kset = nilfs_kset;
184 init_completion(&root->snapshot_kobj_unregister);
185
186 if (root->cno == NILFS_CPTREE_CURRENT_CNO) {
187 err = kobject_init_and_add(&root->snapshot_kobj,
188 &nilfs_snapshot_ktype,
189 &nilfs->ns_dev_kobj,
190 "current_checkpoint");
191 } else {
192 err = kobject_init_and_add(&root->snapshot_kobj,
193 &nilfs_snapshot_ktype,
194 parent,
195 "%llu", root->cno);
196 }
197
198 if (err)
199 kobject_put(&root->snapshot_kobj);
200
201 return err;
202}
203
204void nilfs_sysfs_delete_snapshot_group(struct nilfs_root *root)
205{
206 kobject_put(&root->snapshot_kobj);
207}
208
209/************************************************************************
210 * NILFS mounted snapshots attrs *
211 ************************************************************************/
212
213static const char mounted_snapshots_readme_str[] =
214 "The mounted_snapshots group contains group for\n"
215 "every mounted snapshot.\n";
216
217static ssize_t
218nilfs_mounted_snapshots_README_show(struct nilfs_mounted_snapshots_attr *attr,
219 struct the_nilfs *nilfs, char *buf)
220{
221 return sysfs_emit(buf, mounted_snapshots_readme_str);
222}
223
224NILFS_MOUNTED_SNAPSHOTS_RO_ATTR(README);
225
226static struct attribute *nilfs_mounted_snapshots_attrs[] = {
227 NILFS_MOUNTED_SNAPSHOTS_ATTR_LIST(README),
228 NULL,
229};
230ATTRIBUTE_GROUPS(nilfs_mounted_snapshots);
231
232NILFS_DEV_INT_GROUP_OPS(mounted_snapshots, dev);
233NILFS_DEV_INT_GROUP_TYPE(mounted_snapshots, dev);
234NILFS_DEV_INT_GROUP_FNS(mounted_snapshots, dev);
235
236/************************************************************************
237 * NILFS checkpoints attrs *
238 ************************************************************************/
239
240static ssize_t
241nilfs_checkpoints_checkpoints_number_show(struct nilfs_checkpoints_attr *attr,
242 struct the_nilfs *nilfs,
243 char *buf)
244{
245 __u64 ncheckpoints;
246 struct nilfs_cpstat cpstat;
247 int err;
248
249 down_read(&nilfs->ns_segctor_sem);
250 err = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
251 up_read(&nilfs->ns_segctor_sem);
252 if (err < 0) {
253 nilfs_err(nilfs->ns_sb, "unable to get checkpoint stat: err=%d",
254 err);
255 return err;
256 }
257
258 ncheckpoints = cpstat.cs_ncps;
259
260 return sysfs_emit(buf, "%llu\n", ncheckpoints);
261}
262
263static ssize_t
264nilfs_checkpoints_snapshots_number_show(struct nilfs_checkpoints_attr *attr,
265 struct the_nilfs *nilfs,
266 char *buf)
267{
268 __u64 nsnapshots;
269 struct nilfs_cpstat cpstat;
270 int err;
271
272 down_read(&nilfs->ns_segctor_sem);
273 err = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
274 up_read(&nilfs->ns_segctor_sem);
275 if (err < 0) {
276 nilfs_err(nilfs->ns_sb, "unable to get checkpoint stat: err=%d",
277 err);
278 return err;
279 }
280
281 nsnapshots = cpstat.cs_nsss;
282
283 return sysfs_emit(buf, "%llu\n", nsnapshots);
284}
285
286static ssize_t
287nilfs_checkpoints_last_seg_checkpoint_show(struct nilfs_checkpoints_attr *attr,
288 struct the_nilfs *nilfs,
289 char *buf)
290{
291 __u64 last_cno;
292
293 spin_lock(&nilfs->ns_last_segment_lock);
294 last_cno = nilfs->ns_last_cno;
295 spin_unlock(&nilfs->ns_last_segment_lock);
296
297 return sysfs_emit(buf, "%llu\n", last_cno);
298}
299
300static ssize_t
301nilfs_checkpoints_next_checkpoint_show(struct nilfs_checkpoints_attr *attr,
302 struct the_nilfs *nilfs,
303 char *buf)
304{
305 __u64 cno;
306
307 down_read(&nilfs->ns_segctor_sem);
308 cno = nilfs->ns_cno;
309 up_read(&nilfs->ns_segctor_sem);
310
311 return sysfs_emit(buf, "%llu\n", cno);
312}
313
314static const char checkpoints_readme_str[] =
315 "The checkpoints group contains attributes that describe\n"
316 "details about volume's checkpoints.\n\n"
317 "(1) checkpoints_number\n\tshow number of checkpoints on volume.\n\n"
318 "(2) snapshots_number\n\tshow number of snapshots on volume.\n\n"
319 "(3) last_seg_checkpoint\n"
320 "\tshow checkpoint number of the latest segment.\n\n"
321 "(4) next_checkpoint\n\tshow next checkpoint number.\n\n";
322
323static ssize_t
324nilfs_checkpoints_README_show(struct nilfs_checkpoints_attr *attr,
325 struct the_nilfs *nilfs, char *buf)
326{
327 return sysfs_emit(buf, checkpoints_readme_str);
328}
329
330NILFS_CHECKPOINTS_RO_ATTR(checkpoints_number);
331NILFS_CHECKPOINTS_RO_ATTR(snapshots_number);
332NILFS_CHECKPOINTS_RO_ATTR(last_seg_checkpoint);
333NILFS_CHECKPOINTS_RO_ATTR(next_checkpoint);
334NILFS_CHECKPOINTS_RO_ATTR(README);
335
336static struct attribute *nilfs_checkpoints_attrs[] = {
337 NILFS_CHECKPOINTS_ATTR_LIST(checkpoints_number),
338 NILFS_CHECKPOINTS_ATTR_LIST(snapshots_number),
339 NILFS_CHECKPOINTS_ATTR_LIST(last_seg_checkpoint),
340 NILFS_CHECKPOINTS_ATTR_LIST(next_checkpoint),
341 NILFS_CHECKPOINTS_ATTR_LIST(README),
342 NULL,
343};
344ATTRIBUTE_GROUPS(nilfs_checkpoints);
345
346NILFS_DEV_INT_GROUP_OPS(checkpoints, dev);
347NILFS_DEV_INT_GROUP_TYPE(checkpoints, dev);
348NILFS_DEV_INT_GROUP_FNS(checkpoints, dev);
349
350/************************************************************************
351 * NILFS segments attrs *
352 ************************************************************************/
353
354static ssize_t
355nilfs_segments_segments_number_show(struct nilfs_segments_attr *attr,
356 struct the_nilfs *nilfs,
357 char *buf)
358{
359 return sysfs_emit(buf, "%lu\n", nilfs->ns_nsegments);
360}
361
362static ssize_t
363nilfs_segments_blocks_per_segment_show(struct nilfs_segments_attr *attr,
364 struct the_nilfs *nilfs,
365 char *buf)
366{
367 return sysfs_emit(buf, "%lu\n", nilfs->ns_blocks_per_segment);
368}
369
370static ssize_t
371nilfs_segments_clean_segments_show(struct nilfs_segments_attr *attr,
372 struct the_nilfs *nilfs,
373 char *buf)
374{
375 unsigned long ncleansegs;
376
377 down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
378 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
379 up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
380
381 return sysfs_emit(buf, "%lu\n", ncleansegs);
382}
383
384static ssize_t
385nilfs_segments_dirty_segments_show(struct nilfs_segments_attr *attr,
386 struct the_nilfs *nilfs,
387 char *buf)
388{
389 struct nilfs_sustat sustat;
390 int err;
391
392 down_read(&nilfs->ns_segctor_sem);
393 err = nilfs_sufile_get_stat(nilfs->ns_sufile, &sustat);
394 up_read(&nilfs->ns_segctor_sem);
395 if (err < 0) {
396 nilfs_err(nilfs->ns_sb, "unable to get segment stat: err=%d",
397 err);
398 return err;
399 }
400
401 return sysfs_emit(buf, "%llu\n", sustat.ss_ndirtysegs);
402}
403
404static const char segments_readme_str[] =
405 "The segments group contains attributes that describe\n"
406 "details about volume's segments.\n\n"
407 "(1) segments_number\n\tshow number of segments on volume.\n\n"
408 "(2) blocks_per_segment\n\tshow number of blocks in segment.\n\n"
409 "(3) clean_segments\n\tshow count of clean segments.\n\n"
410 "(4) dirty_segments\n\tshow count of dirty segments.\n\n";
411
412static ssize_t
413nilfs_segments_README_show(struct nilfs_segments_attr *attr,
414 struct the_nilfs *nilfs,
415 char *buf)
416{
417 return sysfs_emit(buf, segments_readme_str);
418}
419
420NILFS_SEGMENTS_RO_ATTR(segments_number);
421NILFS_SEGMENTS_RO_ATTR(blocks_per_segment);
422NILFS_SEGMENTS_RO_ATTR(clean_segments);
423NILFS_SEGMENTS_RO_ATTR(dirty_segments);
424NILFS_SEGMENTS_RO_ATTR(README);
425
426static struct attribute *nilfs_segments_attrs[] = {
427 NILFS_SEGMENTS_ATTR_LIST(segments_number),
428 NILFS_SEGMENTS_ATTR_LIST(blocks_per_segment),
429 NILFS_SEGMENTS_ATTR_LIST(clean_segments),
430 NILFS_SEGMENTS_ATTR_LIST(dirty_segments),
431 NILFS_SEGMENTS_ATTR_LIST(README),
432 NULL,
433};
434ATTRIBUTE_GROUPS(nilfs_segments);
435
436NILFS_DEV_INT_GROUP_OPS(segments, dev);
437NILFS_DEV_INT_GROUP_TYPE(segments, dev);
438NILFS_DEV_INT_GROUP_FNS(segments, dev);
439
440/************************************************************************
441 * NILFS segctor attrs *
442 ************************************************************************/
443
444static ssize_t
445nilfs_segctor_last_pseg_block_show(struct nilfs_segctor_attr *attr,
446 struct the_nilfs *nilfs,
447 char *buf)
448{
449 sector_t last_pseg;
450
451 spin_lock(&nilfs->ns_last_segment_lock);
452 last_pseg = nilfs->ns_last_pseg;
453 spin_unlock(&nilfs->ns_last_segment_lock);
454
455 return sysfs_emit(buf, "%llu\n",
456 (unsigned long long)last_pseg);
457}
458
459static ssize_t
460nilfs_segctor_last_seg_sequence_show(struct nilfs_segctor_attr *attr,
461 struct the_nilfs *nilfs,
462 char *buf)
463{
464 u64 last_seq;
465
466 spin_lock(&nilfs->ns_last_segment_lock);
467 last_seq = nilfs->ns_last_seq;
468 spin_unlock(&nilfs->ns_last_segment_lock);
469
470 return sysfs_emit(buf, "%llu\n", last_seq);
471}
472
473static ssize_t
474nilfs_segctor_last_seg_checkpoint_show(struct nilfs_segctor_attr *attr,
475 struct the_nilfs *nilfs,
476 char *buf)
477{
478 __u64 last_cno;
479
480 spin_lock(&nilfs->ns_last_segment_lock);
481 last_cno = nilfs->ns_last_cno;
482 spin_unlock(&nilfs->ns_last_segment_lock);
483
484 return sysfs_emit(buf, "%llu\n", last_cno);
485}
486
487static ssize_t
488nilfs_segctor_current_seg_sequence_show(struct nilfs_segctor_attr *attr,
489 struct the_nilfs *nilfs,
490 char *buf)
491{
492 u64 seg_seq;
493
494 down_read(&nilfs->ns_segctor_sem);
495 seg_seq = nilfs->ns_seg_seq;
496 up_read(&nilfs->ns_segctor_sem);
497
498 return sysfs_emit(buf, "%llu\n", seg_seq);
499}
500
501static ssize_t
502nilfs_segctor_current_last_full_seg_show(struct nilfs_segctor_attr *attr,
503 struct the_nilfs *nilfs,
504 char *buf)
505{
506 __u64 segnum;
507
508 down_read(&nilfs->ns_segctor_sem);
509 segnum = nilfs->ns_segnum;
510 up_read(&nilfs->ns_segctor_sem);
511
512 return sysfs_emit(buf, "%llu\n", segnum);
513}
514
515static ssize_t
516nilfs_segctor_next_full_seg_show(struct nilfs_segctor_attr *attr,
517 struct the_nilfs *nilfs,
518 char *buf)
519{
520 __u64 nextnum;
521
522 down_read(&nilfs->ns_segctor_sem);
523 nextnum = nilfs->ns_nextnum;
524 up_read(&nilfs->ns_segctor_sem);
525
526 return sysfs_emit(buf, "%llu\n", nextnum);
527}
528
529static ssize_t
530nilfs_segctor_next_pseg_offset_show(struct nilfs_segctor_attr *attr,
531 struct the_nilfs *nilfs,
532 char *buf)
533{
534 unsigned long pseg_offset;
535
536 down_read(&nilfs->ns_segctor_sem);
537 pseg_offset = nilfs->ns_pseg_offset;
538 up_read(&nilfs->ns_segctor_sem);
539
540 return sysfs_emit(buf, "%lu\n", pseg_offset);
541}
542
543static ssize_t
544nilfs_segctor_next_checkpoint_show(struct nilfs_segctor_attr *attr,
545 struct the_nilfs *nilfs,
546 char *buf)
547{
548 __u64 cno;
549
550 down_read(&nilfs->ns_segctor_sem);
551 cno = nilfs->ns_cno;
552 up_read(&nilfs->ns_segctor_sem);
553
554 return sysfs_emit(buf, "%llu\n", cno);
555}
556
557static ssize_t
558nilfs_segctor_last_seg_write_time_show(struct nilfs_segctor_attr *attr,
559 struct the_nilfs *nilfs,
560 char *buf)
561{
562 time64_t ctime;
563
564 down_read(&nilfs->ns_segctor_sem);
565 ctime = nilfs->ns_ctime;
566 up_read(&nilfs->ns_segctor_sem);
567
568 return sysfs_emit(buf, "%ptTs\n", &ctime);
569}
570
571static ssize_t
572nilfs_segctor_last_seg_write_time_secs_show(struct nilfs_segctor_attr *attr,
573 struct the_nilfs *nilfs,
574 char *buf)
575{
576 time64_t ctime;
577
578 down_read(&nilfs->ns_segctor_sem);
579 ctime = nilfs->ns_ctime;
580 up_read(&nilfs->ns_segctor_sem);
581
582 return sysfs_emit(buf, "%llu\n", ctime);
583}
584
585static ssize_t
586nilfs_segctor_last_nongc_write_time_show(struct nilfs_segctor_attr *attr,
587 struct the_nilfs *nilfs,
588 char *buf)
589{
590 time64_t nongc_ctime;
591
592 down_read(&nilfs->ns_segctor_sem);
593 nongc_ctime = nilfs->ns_nongc_ctime;
594 up_read(&nilfs->ns_segctor_sem);
595
596 return sysfs_emit(buf, "%ptTs\n", &nongc_ctime);
597}
598
599static ssize_t
600nilfs_segctor_last_nongc_write_time_secs_show(struct nilfs_segctor_attr *attr,
601 struct the_nilfs *nilfs,
602 char *buf)
603{
604 time64_t nongc_ctime;
605
606 down_read(&nilfs->ns_segctor_sem);
607 nongc_ctime = nilfs->ns_nongc_ctime;
608 up_read(&nilfs->ns_segctor_sem);
609
610 return sysfs_emit(buf, "%llu\n", nongc_ctime);
611}
612
613static ssize_t
614nilfs_segctor_dirty_data_blocks_count_show(struct nilfs_segctor_attr *attr,
615 struct the_nilfs *nilfs,
616 char *buf)
617{
618 u32 ndirtyblks;
619
620 down_read(&nilfs->ns_segctor_sem);
621 ndirtyblks = atomic_read(&nilfs->ns_ndirtyblks);
622 up_read(&nilfs->ns_segctor_sem);
623
624 return sysfs_emit(buf, "%u\n", ndirtyblks);
625}
626
627static const char segctor_readme_str[] =
628 "The segctor group contains attributes that describe\n"
629 "segctor thread activity details.\n\n"
630 "(1) last_pseg_block\n"
631 "\tshow start block number of the latest segment.\n\n"
632 "(2) last_seg_sequence\n"
633 "\tshow sequence value of the latest segment.\n\n"
634 "(3) last_seg_checkpoint\n"
635 "\tshow checkpoint number of the latest segment.\n\n"
636 "(4) current_seg_sequence\n\tshow segment sequence counter.\n\n"
637 "(5) current_last_full_seg\n"
638 "\tshow index number of the latest full segment.\n\n"
639 "(6) next_full_seg\n"
640 "\tshow index number of the full segment index to be used next.\n\n"
641 "(7) next_pseg_offset\n"
642 "\tshow offset of next partial segment in the current full segment.\n\n"
643 "(8) next_checkpoint\n\tshow next checkpoint number.\n\n"
644 "(9) last_seg_write_time\n"
645 "\tshow write time of the last segment in human-readable format.\n\n"
646 "(10) last_seg_write_time_secs\n"
647 "\tshow write time of the last segment in seconds.\n\n"
648 "(11) last_nongc_write_time\n"
649 "\tshow write time of the last segment not for cleaner operation "
650 "in human-readable format.\n\n"
651 "(12) last_nongc_write_time_secs\n"
652 "\tshow write time of the last segment not for cleaner operation "
653 "in seconds.\n\n"
654 "(13) dirty_data_blocks_count\n"
655 "\tshow number of dirty data blocks.\n\n";
656
657static ssize_t
658nilfs_segctor_README_show(struct nilfs_segctor_attr *attr,
659 struct the_nilfs *nilfs, char *buf)
660{
661 return sysfs_emit(buf, segctor_readme_str);
662}
663
664NILFS_SEGCTOR_RO_ATTR(last_pseg_block);
665NILFS_SEGCTOR_RO_ATTR(last_seg_sequence);
666NILFS_SEGCTOR_RO_ATTR(last_seg_checkpoint);
667NILFS_SEGCTOR_RO_ATTR(current_seg_sequence);
668NILFS_SEGCTOR_RO_ATTR(current_last_full_seg);
669NILFS_SEGCTOR_RO_ATTR(next_full_seg);
670NILFS_SEGCTOR_RO_ATTR(next_pseg_offset);
671NILFS_SEGCTOR_RO_ATTR(next_checkpoint);
672NILFS_SEGCTOR_RO_ATTR(last_seg_write_time);
673NILFS_SEGCTOR_RO_ATTR(last_seg_write_time_secs);
674NILFS_SEGCTOR_RO_ATTR(last_nongc_write_time);
675NILFS_SEGCTOR_RO_ATTR(last_nongc_write_time_secs);
676NILFS_SEGCTOR_RO_ATTR(dirty_data_blocks_count);
677NILFS_SEGCTOR_RO_ATTR(README);
678
679static struct attribute *nilfs_segctor_attrs[] = {
680 NILFS_SEGCTOR_ATTR_LIST(last_pseg_block),
681 NILFS_SEGCTOR_ATTR_LIST(last_seg_sequence),
682 NILFS_SEGCTOR_ATTR_LIST(last_seg_checkpoint),
683 NILFS_SEGCTOR_ATTR_LIST(current_seg_sequence),
684 NILFS_SEGCTOR_ATTR_LIST(current_last_full_seg),
685 NILFS_SEGCTOR_ATTR_LIST(next_full_seg),
686 NILFS_SEGCTOR_ATTR_LIST(next_pseg_offset),
687 NILFS_SEGCTOR_ATTR_LIST(next_checkpoint),
688 NILFS_SEGCTOR_ATTR_LIST(last_seg_write_time),
689 NILFS_SEGCTOR_ATTR_LIST(last_seg_write_time_secs),
690 NILFS_SEGCTOR_ATTR_LIST(last_nongc_write_time),
691 NILFS_SEGCTOR_ATTR_LIST(last_nongc_write_time_secs),
692 NILFS_SEGCTOR_ATTR_LIST(dirty_data_blocks_count),
693 NILFS_SEGCTOR_ATTR_LIST(README),
694 NULL,
695};
696ATTRIBUTE_GROUPS(nilfs_segctor);
697
698NILFS_DEV_INT_GROUP_OPS(segctor, dev);
699NILFS_DEV_INT_GROUP_TYPE(segctor, dev);
700NILFS_DEV_INT_GROUP_FNS(segctor, dev);
701
702/************************************************************************
703 * NILFS superblock attrs *
704 ************************************************************************/
705
706static ssize_t
707nilfs_superblock_sb_write_time_show(struct nilfs_superblock_attr *attr,
708 struct the_nilfs *nilfs,
709 char *buf)
710{
711 time64_t sbwtime;
712
713 down_read(&nilfs->ns_sem);
714 sbwtime = nilfs->ns_sbwtime;
715 up_read(&nilfs->ns_sem);
716
717 return sysfs_emit(buf, "%ptTs\n", &sbwtime);
718}
719
720static ssize_t
721nilfs_superblock_sb_write_time_secs_show(struct nilfs_superblock_attr *attr,
722 struct the_nilfs *nilfs,
723 char *buf)
724{
725 time64_t sbwtime;
726
727 down_read(&nilfs->ns_sem);
728 sbwtime = nilfs->ns_sbwtime;
729 up_read(&nilfs->ns_sem);
730
731 return sysfs_emit(buf, "%llu\n", sbwtime);
732}
733
734static ssize_t
735nilfs_superblock_sb_write_count_show(struct nilfs_superblock_attr *attr,
736 struct the_nilfs *nilfs,
737 char *buf)
738{
739 unsigned int sbwcount;
740
741 down_read(&nilfs->ns_sem);
742 sbwcount = nilfs->ns_sbwcount;
743 up_read(&nilfs->ns_sem);
744
745 return sysfs_emit(buf, "%u\n", sbwcount);
746}
747
748static ssize_t
749nilfs_superblock_sb_update_frequency_show(struct nilfs_superblock_attr *attr,
750 struct the_nilfs *nilfs,
751 char *buf)
752{
753 unsigned int sb_update_freq;
754
755 down_read(&nilfs->ns_sem);
756 sb_update_freq = nilfs->ns_sb_update_freq;
757 up_read(&nilfs->ns_sem);
758
759 return sysfs_emit(buf, "%u\n", sb_update_freq);
760}
761
762static ssize_t
763nilfs_superblock_sb_update_frequency_store(struct nilfs_superblock_attr *attr,
764 struct the_nilfs *nilfs,
765 const char *buf, size_t count)
766{
767 unsigned int val;
768 int err;
769
770 err = kstrtouint(skip_spaces(buf), 0, &val);
771 if (err) {
772 nilfs_err(nilfs->ns_sb, "unable to convert string: err=%d",
773 err);
774 return err;
775 }
776
777 if (val < NILFS_SB_FREQ) {
778 val = NILFS_SB_FREQ;
779 nilfs_warn(nilfs->ns_sb,
780 "superblock update frequency cannot be lesser than 10 seconds");
781 }
782
783 down_write(&nilfs->ns_sem);
784 nilfs->ns_sb_update_freq = val;
785 up_write(&nilfs->ns_sem);
786
787 return count;
788}
789
790static const char sb_readme_str[] =
791 "The superblock group contains attributes that describe\n"
792 "superblock's details.\n\n"
793 "(1) sb_write_time\n\tshow previous write time of super block "
794 "in human-readable format.\n\n"
795 "(2) sb_write_time_secs\n\tshow previous write time of super block "
796 "in seconds.\n\n"
797 "(3) sb_write_count\n\tshow write count of super block.\n\n"
798 "(4) sb_update_frequency\n"
799 "\tshow/set interval of periodical update of superblock (in seconds).\n\n"
800 "\tYou can set preferable frequency of superblock update by command:\n\n"
801 "\t'echo <val> > /sys/fs/<nilfs>/<dev>/superblock/sb_update_frequency'\n";
802
803static ssize_t
804nilfs_superblock_README_show(struct nilfs_superblock_attr *attr,
805 struct the_nilfs *nilfs, char *buf)
806{
807 return sysfs_emit(buf, sb_readme_str);
808}
809
810NILFS_SUPERBLOCK_RO_ATTR(sb_write_time);
811NILFS_SUPERBLOCK_RO_ATTR(sb_write_time_secs);
812NILFS_SUPERBLOCK_RO_ATTR(sb_write_count);
813NILFS_SUPERBLOCK_RW_ATTR(sb_update_frequency);
814NILFS_SUPERBLOCK_RO_ATTR(README);
815
816static struct attribute *nilfs_superblock_attrs[] = {
817 NILFS_SUPERBLOCK_ATTR_LIST(sb_write_time),
818 NILFS_SUPERBLOCK_ATTR_LIST(sb_write_time_secs),
819 NILFS_SUPERBLOCK_ATTR_LIST(sb_write_count),
820 NILFS_SUPERBLOCK_ATTR_LIST(sb_update_frequency),
821 NILFS_SUPERBLOCK_ATTR_LIST(README),
822 NULL,
823};
824ATTRIBUTE_GROUPS(nilfs_superblock);
825
826NILFS_DEV_INT_GROUP_OPS(superblock, dev);
827NILFS_DEV_INT_GROUP_TYPE(superblock, dev);
828NILFS_DEV_INT_GROUP_FNS(superblock, dev);
829
830/************************************************************************
831 * NILFS device attrs *
832 ************************************************************************/
833
834static
835ssize_t nilfs_dev_revision_show(struct nilfs_dev_attr *attr,
836 struct the_nilfs *nilfs,
837 char *buf)
838{
839 struct nilfs_super_block **sbp = nilfs->ns_sbp;
840 u32 major = le32_to_cpu(sbp[0]->s_rev_level);
841 u16 minor = le16_to_cpu(sbp[0]->s_minor_rev_level);
842
843 return sysfs_emit(buf, "%d.%d\n", major, minor);
844}
845
846static
847ssize_t nilfs_dev_blocksize_show(struct nilfs_dev_attr *attr,
848 struct the_nilfs *nilfs,
849 char *buf)
850{
851 return sysfs_emit(buf, "%u\n", nilfs->ns_blocksize);
852}
853
854static
855ssize_t nilfs_dev_device_size_show(struct nilfs_dev_attr *attr,
856 struct the_nilfs *nilfs,
857 char *buf)
858{
859 struct nilfs_super_block **sbp = nilfs->ns_sbp;
860 u64 dev_size = le64_to_cpu(sbp[0]->s_dev_size);
861
862 return sysfs_emit(buf, "%llu\n", dev_size);
863}
864
865static
866ssize_t nilfs_dev_free_blocks_show(struct nilfs_dev_attr *attr,
867 struct the_nilfs *nilfs,
868 char *buf)
869{
870 sector_t free_blocks = 0;
871
872 nilfs_count_free_blocks(nilfs, &free_blocks);
873 return sysfs_emit(buf, "%llu\n",
874 (unsigned long long)free_blocks);
875}
876
877static
878ssize_t nilfs_dev_uuid_show(struct nilfs_dev_attr *attr,
879 struct the_nilfs *nilfs,
880 char *buf)
881{
882 struct nilfs_super_block **sbp = nilfs->ns_sbp;
883
884 return sysfs_emit(buf, "%pUb\n", sbp[0]->s_uuid);
885}
886
887static
888ssize_t nilfs_dev_volume_name_show(struct nilfs_dev_attr *attr,
889 struct the_nilfs *nilfs,
890 char *buf)
891{
892 struct nilfs_super_block **sbp = nilfs->ns_sbp;
893
894 return scnprintf(buf, sizeof(sbp[0]->s_volume_name), "%s\n",
895 sbp[0]->s_volume_name);
896}
897
898static const char dev_readme_str[] =
899 "The <device> group contains attributes that describe file system\n"
900 "partition's details.\n\n"
901 "(1) revision\n\tshow NILFS file system revision.\n\n"
902 "(2) blocksize\n\tshow volume block size in bytes.\n\n"
903 "(3) device_size\n\tshow volume size in bytes.\n\n"
904 "(4) free_blocks\n\tshow count of free blocks on volume.\n\n"
905 "(5) uuid\n\tshow volume's UUID.\n\n"
906 "(6) volume_name\n\tshow volume's name.\n\n";
907
908static ssize_t nilfs_dev_README_show(struct nilfs_dev_attr *attr,
909 struct the_nilfs *nilfs,
910 char *buf)
911{
912 return sysfs_emit(buf, dev_readme_str);
913}
914
915NILFS_DEV_RO_ATTR(revision);
916NILFS_DEV_RO_ATTR(blocksize);
917NILFS_DEV_RO_ATTR(device_size);
918NILFS_DEV_RO_ATTR(free_blocks);
919NILFS_DEV_RO_ATTR(uuid);
920NILFS_DEV_RO_ATTR(volume_name);
921NILFS_DEV_RO_ATTR(README);
922
923static struct attribute *nilfs_dev_attrs[] = {
924 NILFS_DEV_ATTR_LIST(revision),
925 NILFS_DEV_ATTR_LIST(blocksize),
926 NILFS_DEV_ATTR_LIST(device_size),
927 NILFS_DEV_ATTR_LIST(free_blocks),
928 NILFS_DEV_ATTR_LIST(uuid),
929 NILFS_DEV_ATTR_LIST(volume_name),
930 NILFS_DEV_ATTR_LIST(README),
931 NULL,
932};
933ATTRIBUTE_GROUPS(nilfs_dev);
934
935static ssize_t nilfs_dev_attr_show(struct kobject *kobj,
936 struct attribute *attr, char *buf)
937{
938 struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
939 ns_dev_kobj);
940 struct nilfs_dev_attr *a = container_of(attr, struct nilfs_dev_attr,
941 attr);
942
943 return a->show ? a->show(a, nilfs, buf) : 0;
944}
945
946static ssize_t nilfs_dev_attr_store(struct kobject *kobj,
947 struct attribute *attr,
948 const char *buf, size_t len)
949{
950 struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
951 ns_dev_kobj);
952 struct nilfs_dev_attr *a = container_of(attr, struct nilfs_dev_attr,
953 attr);
954
955 return a->store ? a->store(a, nilfs, buf, len) : 0;
956}
957
958static void nilfs_dev_attr_release(struct kobject *kobj)
959{
960 struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
961 ns_dev_kobj);
962 complete(&nilfs->ns_dev_kobj_unregister);
963}
964
965static const struct sysfs_ops nilfs_dev_attr_ops = {
966 .show = nilfs_dev_attr_show,
967 .store = nilfs_dev_attr_store,
968};
969
970static struct kobj_type nilfs_dev_ktype = {
971 .default_groups = nilfs_dev_groups,
972 .sysfs_ops = &nilfs_dev_attr_ops,
973 .release = nilfs_dev_attr_release,
974};
975
976int nilfs_sysfs_create_device_group(struct super_block *sb)
977{
978 struct the_nilfs *nilfs = sb->s_fs_info;
979 size_t devgrp_size = sizeof(struct nilfs_sysfs_dev_subgroups);
980 int err;
981
982 nilfs->ns_dev_subgroups = kzalloc(devgrp_size, GFP_KERNEL);
983 if (unlikely(!nilfs->ns_dev_subgroups)) {
984 err = -ENOMEM;
985 nilfs_err(sb, "unable to allocate memory for device group");
986 goto failed_create_device_group;
987 }
988
989 nilfs->ns_dev_kobj.kset = nilfs_kset;
990 init_completion(&nilfs->ns_dev_kobj_unregister);
991 err = kobject_init_and_add(&nilfs->ns_dev_kobj, &nilfs_dev_ktype, NULL,
992 "%s", sb->s_id);
993 if (err)
994 goto cleanup_dev_kobject;
995
996 err = nilfs_sysfs_create_mounted_snapshots_group(nilfs);
997 if (err)
998 goto cleanup_dev_kobject;
999
1000 err = nilfs_sysfs_create_checkpoints_group(nilfs);
1001 if (err)
1002 goto delete_mounted_snapshots_group;
1003
1004 err = nilfs_sysfs_create_segments_group(nilfs);
1005 if (err)
1006 goto delete_checkpoints_group;
1007
1008 err = nilfs_sysfs_create_superblock_group(nilfs);
1009 if (err)
1010 goto delete_segments_group;
1011
1012 err = nilfs_sysfs_create_segctor_group(nilfs);
1013 if (err)
1014 goto delete_superblock_group;
1015
1016 return 0;
1017
1018delete_superblock_group:
1019 nilfs_sysfs_delete_superblock_group(nilfs);
1020
1021delete_segments_group:
1022 nilfs_sysfs_delete_segments_group(nilfs);
1023
1024delete_checkpoints_group:
1025 nilfs_sysfs_delete_checkpoints_group(nilfs);
1026
1027delete_mounted_snapshots_group:
1028 nilfs_sysfs_delete_mounted_snapshots_group(nilfs);
1029
1030cleanup_dev_kobject:
1031 kobject_put(&nilfs->ns_dev_kobj);
1032 kfree(nilfs->ns_dev_subgroups);
1033
1034failed_create_device_group:
1035 return err;
1036}
1037
1038void nilfs_sysfs_delete_device_group(struct the_nilfs *nilfs)
1039{
1040 nilfs_sysfs_delete_mounted_snapshots_group(nilfs);
1041 nilfs_sysfs_delete_checkpoints_group(nilfs);
1042 nilfs_sysfs_delete_segments_group(nilfs);
1043 nilfs_sysfs_delete_superblock_group(nilfs);
1044 nilfs_sysfs_delete_segctor_group(nilfs);
1045 kobject_del(&nilfs->ns_dev_kobj);
1046 kobject_put(&nilfs->ns_dev_kobj);
1047 kfree(nilfs->ns_dev_subgroups);
1048}
1049
1050/************************************************************************
1051 * NILFS feature attrs *
1052 ************************************************************************/
1053
1054static ssize_t nilfs_feature_revision_show(struct kobject *kobj,
1055 struct attribute *attr, char *buf)
1056{
1057 return sysfs_emit(buf, "%d.%d\n",
1058 NILFS_CURRENT_REV, NILFS_MINOR_REV);
1059}
1060
1061static const char features_readme_str[] =
1062 "The features group contains attributes that describe NILFS file\n"
1063 "system driver features.\n\n"
1064 "(1) revision\n\tshow current revision of NILFS file system driver.\n";
1065
1066static ssize_t nilfs_feature_README_show(struct kobject *kobj,
1067 struct attribute *attr,
1068 char *buf)
1069{
1070 return sysfs_emit(buf, features_readme_str);
1071}
1072
1073NILFS_FEATURE_RO_ATTR(revision);
1074NILFS_FEATURE_RO_ATTR(README);
1075
1076static struct attribute *nilfs_feature_attrs[] = {
1077 NILFS_FEATURE_ATTR_LIST(revision),
1078 NILFS_FEATURE_ATTR_LIST(README),
1079 NULL,
1080};
1081
1082static const struct attribute_group nilfs_feature_attr_group = {
1083 .name = "features",
1084 .attrs = nilfs_feature_attrs,
1085};
1086
1087int __init nilfs_sysfs_init(void)
1088{
1089 int err;
1090
1091 nilfs_kset = kset_create_and_add(NILFS_ROOT_GROUP_NAME, NULL, fs_kobj);
1092 if (!nilfs_kset) {
1093 err = -ENOMEM;
1094 nilfs_err(NULL, "unable to create sysfs entry: err=%d", err);
1095 goto failed_sysfs_init;
1096 }
1097
1098 err = sysfs_create_group(&nilfs_kset->kobj, &nilfs_feature_attr_group);
1099 if (unlikely(err)) {
1100 nilfs_err(NULL, "unable to create feature group: err=%d", err);
1101 goto cleanup_sysfs_init;
1102 }
1103
1104 return 0;
1105
1106cleanup_sysfs_init:
1107 kset_unregister(nilfs_kset);
1108
1109failed_sysfs_init:
1110 return err;
1111}
1112
1113void nilfs_sysfs_exit(void)
1114{
1115 sysfs_remove_group(&nilfs_kset->kobj, &nilfs_feature_attr_group);
1116 kset_unregister(nilfs_kset);
1117}
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>/ */
29static 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
44#define NILFS_DEV_INT_GROUP_OPS(name, parent_name) \
45static 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} \
56static 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} \
68static 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) \
74static 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} \
83static 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) \
90static 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} \
109static void nilfs_sysfs_delete_##name##_group(struct the_nilfs *nilfs) \
110{ \
111 kobject_del(&nilfs->ns_##parent_name##_subgroups->sg_##name##_kobj); \
112}
113
114/************************************************************************
115 * NILFS snapshot attrs *
116 ************************************************************************/
117
118static ssize_t
119nilfs_snapshot_inodes_count_show(struct nilfs_snapshot_attr *attr,
120 struct nilfs_root *root, char *buf)
121{
122 return snprintf(buf, PAGE_SIZE, "%llu\n",
123 (unsigned long long)atomic64_read(&root->inodes_count));
124}
125
126static ssize_t
127nilfs_snapshot_blocks_count_show(struct nilfs_snapshot_attr *attr,
128 struct nilfs_root *root, char *buf)
129{
130 return snprintf(buf, PAGE_SIZE, "%llu\n",
131 (unsigned long long)atomic64_read(&root->blocks_count));
132}
133
134static const char snapshot_readme_str[] =
135 "The group contains details about mounted snapshot.\n\n"
136 "(1) inodes_count\n\tshow number of inodes for snapshot.\n\n"
137 "(2) blocks_count\n\tshow number of blocks for snapshot.\n\n";
138
139static ssize_t
140nilfs_snapshot_README_show(struct nilfs_snapshot_attr *attr,
141 struct nilfs_root *root, char *buf)
142{
143 return snprintf(buf, PAGE_SIZE, snapshot_readme_str);
144}
145
146NILFS_SNAPSHOT_RO_ATTR(inodes_count);
147NILFS_SNAPSHOT_RO_ATTR(blocks_count);
148NILFS_SNAPSHOT_RO_ATTR(README);
149
150static struct attribute *nilfs_snapshot_attrs[] = {
151 NILFS_SNAPSHOT_ATTR_LIST(inodes_count),
152 NILFS_SNAPSHOT_ATTR_LIST(blocks_count),
153 NILFS_SNAPSHOT_ATTR_LIST(README),
154 NULL,
155};
156
157static ssize_t nilfs_snapshot_attr_show(struct kobject *kobj,
158 struct attribute *attr, char *buf)
159{
160 struct nilfs_root *root =
161 container_of(kobj, struct nilfs_root, snapshot_kobj);
162 struct nilfs_snapshot_attr *a =
163 container_of(attr, struct nilfs_snapshot_attr, attr);
164
165 return a->show ? a->show(a, root, buf) : 0;
166}
167
168static ssize_t nilfs_snapshot_attr_store(struct kobject *kobj,
169 struct attribute *attr,
170 const char *buf, size_t len)
171{
172 struct nilfs_root *root =
173 container_of(kobj, struct nilfs_root, snapshot_kobj);
174 struct nilfs_snapshot_attr *a =
175 container_of(attr, struct nilfs_snapshot_attr, attr);
176
177 return a->store ? a->store(a, root, buf, len) : 0;
178}
179
180static void nilfs_snapshot_attr_release(struct kobject *kobj)
181{
182 struct nilfs_root *root = container_of(kobj, struct nilfs_root,
183 snapshot_kobj);
184 complete(&root->snapshot_kobj_unregister);
185}
186
187static const struct sysfs_ops nilfs_snapshot_attr_ops = {
188 .show = nilfs_snapshot_attr_show,
189 .store = nilfs_snapshot_attr_store,
190};
191
192static struct kobj_type nilfs_snapshot_ktype = {
193 .default_attrs = nilfs_snapshot_attrs,
194 .sysfs_ops = &nilfs_snapshot_attr_ops,
195 .release = nilfs_snapshot_attr_release,
196};
197
198int nilfs_sysfs_create_snapshot_group(struct nilfs_root *root)
199{
200 struct the_nilfs *nilfs;
201 struct kobject *parent;
202 int err;
203
204 nilfs = root->nilfs;
205 parent = &nilfs->ns_dev_subgroups->sg_mounted_snapshots_kobj;
206 root->snapshot_kobj.kset = nilfs_kset;
207 init_completion(&root->snapshot_kobj_unregister);
208
209 if (root->cno == NILFS_CPTREE_CURRENT_CNO) {
210 err = kobject_init_and_add(&root->snapshot_kobj,
211 &nilfs_snapshot_ktype,
212 &nilfs->ns_dev_kobj,
213 "current_checkpoint");
214 } else {
215 err = kobject_init_and_add(&root->snapshot_kobj,
216 &nilfs_snapshot_ktype,
217 parent,
218 "%llu", root->cno);
219 }
220
221 if (err)
222 return err;
223
224 return 0;
225}
226
227void nilfs_sysfs_delete_snapshot_group(struct nilfs_root *root)
228{
229 kobject_del(&root->snapshot_kobj);
230}
231
232/************************************************************************
233 * NILFS mounted snapshots attrs *
234 ************************************************************************/
235
236static const char mounted_snapshots_readme_str[] =
237 "The mounted_snapshots group contains group for\n"
238 "every mounted snapshot.\n";
239
240static ssize_t
241nilfs_mounted_snapshots_README_show(struct nilfs_mounted_snapshots_attr *attr,
242 struct the_nilfs *nilfs, char *buf)
243{
244 return snprintf(buf, PAGE_SIZE, mounted_snapshots_readme_str);
245}
246
247NILFS_MOUNTED_SNAPSHOTS_RO_ATTR(README);
248
249static struct attribute *nilfs_mounted_snapshots_attrs[] = {
250 NILFS_MOUNTED_SNAPSHOTS_ATTR_LIST(README),
251 NULL,
252};
253
254NILFS_DEV_INT_GROUP_OPS(mounted_snapshots, dev);
255NILFS_DEV_INT_GROUP_TYPE(mounted_snapshots, dev);
256NILFS_DEV_INT_GROUP_FNS(mounted_snapshots, dev);
257
258/************************************************************************
259 * NILFS checkpoints attrs *
260 ************************************************************************/
261
262static ssize_t
263nilfs_checkpoints_checkpoints_number_show(struct nilfs_checkpoints_attr *attr,
264 struct the_nilfs *nilfs,
265 char *buf)
266{
267 __u64 ncheckpoints;
268 struct nilfs_cpstat cpstat;
269 int err;
270
271 down_read(&nilfs->ns_segctor_sem);
272 err = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
273 up_read(&nilfs->ns_segctor_sem);
274 if (err < 0) {
275 printk(KERN_ERR "NILFS: unable to get checkpoint stat: err=%d\n",
276 err);
277 return err;
278 }
279
280 ncheckpoints = cpstat.cs_ncps;
281
282 return snprintf(buf, PAGE_SIZE, "%llu\n", ncheckpoints);
283}
284
285static ssize_t
286nilfs_checkpoints_snapshots_number_show(struct nilfs_checkpoints_attr *attr,
287 struct the_nilfs *nilfs,
288 char *buf)
289{
290 __u64 nsnapshots;
291 struct nilfs_cpstat cpstat;
292 int err;
293
294 down_read(&nilfs->ns_segctor_sem);
295 err = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
296 up_read(&nilfs->ns_segctor_sem);
297 if (err < 0) {
298 printk(KERN_ERR "NILFS: unable to get checkpoint stat: err=%d\n",
299 err);
300 return err;
301 }
302
303 nsnapshots = cpstat.cs_nsss;
304
305 return snprintf(buf, PAGE_SIZE, "%llu\n", nsnapshots);
306}
307
308static ssize_t
309nilfs_checkpoints_last_seg_checkpoint_show(struct nilfs_checkpoints_attr *attr,
310 struct the_nilfs *nilfs,
311 char *buf)
312{
313 __u64 last_cno;
314
315 spin_lock(&nilfs->ns_last_segment_lock);
316 last_cno = nilfs->ns_last_cno;
317 spin_unlock(&nilfs->ns_last_segment_lock);
318
319 return snprintf(buf, PAGE_SIZE, "%llu\n", last_cno);
320}
321
322static ssize_t
323nilfs_checkpoints_next_checkpoint_show(struct nilfs_checkpoints_attr *attr,
324 struct the_nilfs *nilfs,
325 char *buf)
326{
327 __u64 cno;
328
329 down_read(&nilfs->ns_sem);
330 cno = nilfs->ns_cno;
331 up_read(&nilfs->ns_sem);
332
333 return snprintf(buf, PAGE_SIZE, "%llu\n", cno);
334}
335
336static const char checkpoints_readme_str[] =
337 "The checkpoints group contains attributes that describe\n"
338 "details about volume's checkpoints.\n\n"
339 "(1) checkpoints_number\n\tshow number of checkpoints on volume.\n\n"
340 "(2) snapshots_number\n\tshow number of snapshots on volume.\n\n"
341 "(3) last_seg_checkpoint\n"
342 "\tshow checkpoint number of the latest segment.\n\n"
343 "(4) next_checkpoint\n\tshow next checkpoint number.\n\n";
344
345static ssize_t
346nilfs_checkpoints_README_show(struct nilfs_checkpoints_attr *attr,
347 struct the_nilfs *nilfs, char *buf)
348{
349 return snprintf(buf, PAGE_SIZE, checkpoints_readme_str);
350}
351
352NILFS_CHECKPOINTS_RO_ATTR(checkpoints_number);
353NILFS_CHECKPOINTS_RO_ATTR(snapshots_number);
354NILFS_CHECKPOINTS_RO_ATTR(last_seg_checkpoint);
355NILFS_CHECKPOINTS_RO_ATTR(next_checkpoint);
356NILFS_CHECKPOINTS_RO_ATTR(README);
357
358static struct attribute *nilfs_checkpoints_attrs[] = {
359 NILFS_CHECKPOINTS_ATTR_LIST(checkpoints_number),
360 NILFS_CHECKPOINTS_ATTR_LIST(snapshots_number),
361 NILFS_CHECKPOINTS_ATTR_LIST(last_seg_checkpoint),
362 NILFS_CHECKPOINTS_ATTR_LIST(next_checkpoint),
363 NILFS_CHECKPOINTS_ATTR_LIST(README),
364 NULL,
365};
366
367NILFS_DEV_INT_GROUP_OPS(checkpoints, dev);
368NILFS_DEV_INT_GROUP_TYPE(checkpoints, dev);
369NILFS_DEV_INT_GROUP_FNS(checkpoints, dev);
370
371/************************************************************************
372 * NILFS segments attrs *
373 ************************************************************************/
374
375static ssize_t
376nilfs_segments_segments_number_show(struct nilfs_segments_attr *attr,
377 struct the_nilfs *nilfs,
378 char *buf)
379{
380 return snprintf(buf, PAGE_SIZE, "%lu\n", nilfs->ns_nsegments);
381}
382
383static ssize_t
384nilfs_segments_blocks_per_segment_show(struct nilfs_segments_attr *attr,
385 struct the_nilfs *nilfs,
386 char *buf)
387{
388 return snprintf(buf, PAGE_SIZE, "%lu\n", nilfs->ns_blocks_per_segment);
389}
390
391static ssize_t
392nilfs_segments_clean_segments_show(struct nilfs_segments_attr *attr,
393 struct the_nilfs *nilfs,
394 char *buf)
395{
396 unsigned long ncleansegs;
397
398 down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
399 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
400 up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
401
402 return snprintf(buf, PAGE_SIZE, "%lu\n", ncleansegs);
403}
404
405static ssize_t
406nilfs_segments_dirty_segments_show(struct nilfs_segments_attr *attr,
407 struct the_nilfs *nilfs,
408 char *buf)
409{
410 struct nilfs_sustat sustat;
411 int err;
412
413 down_read(&nilfs->ns_segctor_sem);
414 err = nilfs_sufile_get_stat(nilfs->ns_sufile, &sustat);
415 up_read(&nilfs->ns_segctor_sem);
416 if (err < 0) {
417 printk(KERN_ERR "NILFS: unable to get segment stat: err=%d\n",
418 err);
419 return err;
420 }
421
422 return snprintf(buf, PAGE_SIZE, "%llu\n", sustat.ss_ndirtysegs);
423}
424
425static const char segments_readme_str[] =
426 "The segments group contains attributes that describe\n"
427 "details about volume's segments.\n\n"
428 "(1) segments_number\n\tshow number of segments on volume.\n\n"
429 "(2) blocks_per_segment\n\tshow number of blocks in segment.\n\n"
430 "(3) clean_segments\n\tshow count of clean segments.\n\n"
431 "(4) dirty_segments\n\tshow count of dirty segments.\n\n";
432
433static ssize_t
434nilfs_segments_README_show(struct nilfs_segments_attr *attr,
435 struct the_nilfs *nilfs,
436 char *buf)
437{
438 return snprintf(buf, PAGE_SIZE, segments_readme_str);
439}
440
441NILFS_SEGMENTS_RO_ATTR(segments_number);
442NILFS_SEGMENTS_RO_ATTR(blocks_per_segment);
443NILFS_SEGMENTS_RO_ATTR(clean_segments);
444NILFS_SEGMENTS_RO_ATTR(dirty_segments);
445NILFS_SEGMENTS_RO_ATTR(README);
446
447static struct attribute *nilfs_segments_attrs[] = {
448 NILFS_SEGMENTS_ATTR_LIST(segments_number),
449 NILFS_SEGMENTS_ATTR_LIST(blocks_per_segment),
450 NILFS_SEGMENTS_ATTR_LIST(clean_segments),
451 NILFS_SEGMENTS_ATTR_LIST(dirty_segments),
452 NILFS_SEGMENTS_ATTR_LIST(README),
453 NULL,
454};
455
456NILFS_DEV_INT_GROUP_OPS(segments, dev);
457NILFS_DEV_INT_GROUP_TYPE(segments, dev);
458NILFS_DEV_INT_GROUP_FNS(segments, dev);
459
460/************************************************************************
461 * NILFS segctor attrs *
462 ************************************************************************/
463
464static ssize_t
465nilfs_segctor_last_pseg_block_show(struct nilfs_segctor_attr *attr,
466 struct the_nilfs *nilfs,
467 char *buf)
468{
469 sector_t last_pseg;
470
471 spin_lock(&nilfs->ns_last_segment_lock);
472 last_pseg = nilfs->ns_last_pseg;
473 spin_unlock(&nilfs->ns_last_segment_lock);
474
475 return snprintf(buf, PAGE_SIZE, "%llu\n",
476 (unsigned long long)last_pseg);
477}
478
479static ssize_t
480nilfs_segctor_last_seg_sequence_show(struct nilfs_segctor_attr *attr,
481 struct the_nilfs *nilfs,
482 char *buf)
483{
484 u64 last_seq;
485
486 spin_lock(&nilfs->ns_last_segment_lock);
487 last_seq = nilfs->ns_last_seq;
488 spin_unlock(&nilfs->ns_last_segment_lock);
489
490 return snprintf(buf, PAGE_SIZE, "%llu\n", last_seq);
491}
492
493static ssize_t
494nilfs_segctor_last_seg_checkpoint_show(struct nilfs_segctor_attr *attr,
495 struct the_nilfs *nilfs,
496 char *buf)
497{
498 __u64 last_cno;
499
500 spin_lock(&nilfs->ns_last_segment_lock);
501 last_cno = nilfs->ns_last_cno;
502 spin_unlock(&nilfs->ns_last_segment_lock);
503
504 return snprintf(buf, PAGE_SIZE, "%llu\n", last_cno);
505}
506
507static ssize_t
508nilfs_segctor_current_seg_sequence_show(struct nilfs_segctor_attr *attr,
509 struct the_nilfs *nilfs,
510 char *buf)
511{
512 u64 seg_seq;
513
514 down_read(&nilfs->ns_sem);
515 seg_seq = nilfs->ns_seg_seq;
516 up_read(&nilfs->ns_sem);
517
518 return snprintf(buf, PAGE_SIZE, "%llu\n", seg_seq);
519}
520
521static ssize_t
522nilfs_segctor_current_last_full_seg_show(struct nilfs_segctor_attr *attr,
523 struct the_nilfs *nilfs,
524 char *buf)
525{
526 __u64 segnum;
527
528 down_read(&nilfs->ns_sem);
529 segnum = nilfs->ns_segnum;
530 up_read(&nilfs->ns_sem);
531
532 return snprintf(buf, PAGE_SIZE, "%llu\n", segnum);
533}
534
535static ssize_t
536nilfs_segctor_next_full_seg_show(struct nilfs_segctor_attr *attr,
537 struct the_nilfs *nilfs,
538 char *buf)
539{
540 __u64 nextnum;
541
542 down_read(&nilfs->ns_sem);
543 nextnum = nilfs->ns_nextnum;
544 up_read(&nilfs->ns_sem);
545
546 return snprintf(buf, PAGE_SIZE, "%llu\n", nextnum);
547}
548
549static ssize_t
550nilfs_segctor_next_pseg_offset_show(struct nilfs_segctor_attr *attr,
551 struct the_nilfs *nilfs,
552 char *buf)
553{
554 unsigned long pseg_offset;
555
556 down_read(&nilfs->ns_sem);
557 pseg_offset = nilfs->ns_pseg_offset;
558 up_read(&nilfs->ns_sem);
559
560 return snprintf(buf, PAGE_SIZE, "%lu\n", pseg_offset);
561}
562
563static ssize_t
564nilfs_segctor_next_checkpoint_show(struct nilfs_segctor_attr *attr,
565 struct the_nilfs *nilfs,
566 char *buf)
567{
568 __u64 cno;
569
570 down_read(&nilfs->ns_sem);
571 cno = nilfs->ns_cno;
572 up_read(&nilfs->ns_sem);
573
574 return snprintf(buf, PAGE_SIZE, "%llu\n", cno);
575}
576
577static ssize_t
578nilfs_segctor_last_seg_write_time_show(struct nilfs_segctor_attr *attr,
579 struct the_nilfs *nilfs,
580 char *buf)
581{
582 time_t ctime;
583
584 down_read(&nilfs->ns_sem);
585 ctime = nilfs->ns_ctime;
586 up_read(&nilfs->ns_sem);
587
588 return NILFS_SHOW_TIME(ctime, buf);
589}
590
591static ssize_t
592nilfs_segctor_last_seg_write_time_secs_show(struct nilfs_segctor_attr *attr,
593 struct the_nilfs *nilfs,
594 char *buf)
595{
596 time_t ctime;
597
598 down_read(&nilfs->ns_sem);
599 ctime = nilfs->ns_ctime;
600 up_read(&nilfs->ns_sem);
601
602 return snprintf(buf, PAGE_SIZE, "%llu\n", (unsigned long long)ctime);
603}
604
605static ssize_t
606nilfs_segctor_last_nongc_write_time_show(struct nilfs_segctor_attr *attr,
607 struct the_nilfs *nilfs,
608 char *buf)
609{
610 time_t nongc_ctime;
611
612 down_read(&nilfs->ns_sem);
613 nongc_ctime = nilfs->ns_nongc_ctime;
614 up_read(&nilfs->ns_sem);
615
616 return NILFS_SHOW_TIME(nongc_ctime, buf);
617}
618
619static ssize_t
620nilfs_segctor_last_nongc_write_time_secs_show(struct nilfs_segctor_attr *attr,
621 struct the_nilfs *nilfs,
622 char *buf)
623{
624 time_t nongc_ctime;
625
626 down_read(&nilfs->ns_sem);
627 nongc_ctime = nilfs->ns_nongc_ctime;
628 up_read(&nilfs->ns_sem);
629
630 return snprintf(buf, PAGE_SIZE, "%llu\n",
631 (unsigned long long)nongc_ctime);
632}
633
634static ssize_t
635nilfs_segctor_dirty_data_blocks_count_show(struct nilfs_segctor_attr *attr,
636 struct the_nilfs *nilfs,
637 char *buf)
638{
639 u32 ndirtyblks;
640
641 down_read(&nilfs->ns_sem);
642 ndirtyblks = atomic_read(&nilfs->ns_ndirtyblks);
643 up_read(&nilfs->ns_sem);
644
645 return snprintf(buf, PAGE_SIZE, "%u\n", ndirtyblks);
646}
647
648static const char segctor_readme_str[] =
649 "The segctor group contains attributes that describe\n"
650 "segctor thread activity details.\n\n"
651 "(1) last_pseg_block\n"
652 "\tshow start block number of the latest segment.\n\n"
653 "(2) last_seg_sequence\n"
654 "\tshow sequence value of the latest segment.\n\n"
655 "(3) last_seg_checkpoint\n"
656 "\tshow checkpoint number of the latest segment.\n\n"
657 "(4) current_seg_sequence\n\tshow segment sequence counter.\n\n"
658 "(5) current_last_full_seg\n"
659 "\tshow index number of the latest full segment.\n\n"
660 "(6) next_full_seg\n"
661 "\tshow index number of the full segment index to be used next.\n\n"
662 "(7) next_pseg_offset\n"
663 "\tshow offset of next partial segment in the current full segment.\n\n"
664 "(8) next_checkpoint\n\tshow next checkpoint number.\n\n"
665 "(9) last_seg_write_time\n"
666 "\tshow write time of the last segment in human-readable format.\n\n"
667 "(10) last_seg_write_time_secs\n"
668 "\tshow write time of the last segment in seconds.\n\n"
669 "(11) last_nongc_write_time\n"
670 "\tshow write time of the last segment not for cleaner operation "
671 "in human-readable format.\n\n"
672 "(12) last_nongc_write_time_secs\n"
673 "\tshow write time of the last segment not for cleaner operation "
674 "in seconds.\n\n"
675 "(13) dirty_data_blocks_count\n"
676 "\tshow number of dirty data blocks.\n\n";
677
678static ssize_t
679nilfs_segctor_README_show(struct nilfs_segctor_attr *attr,
680 struct the_nilfs *nilfs, char *buf)
681{
682 return snprintf(buf, PAGE_SIZE, segctor_readme_str);
683}
684
685NILFS_SEGCTOR_RO_ATTR(last_pseg_block);
686NILFS_SEGCTOR_RO_ATTR(last_seg_sequence);
687NILFS_SEGCTOR_RO_ATTR(last_seg_checkpoint);
688NILFS_SEGCTOR_RO_ATTR(current_seg_sequence);
689NILFS_SEGCTOR_RO_ATTR(current_last_full_seg);
690NILFS_SEGCTOR_RO_ATTR(next_full_seg);
691NILFS_SEGCTOR_RO_ATTR(next_pseg_offset);
692NILFS_SEGCTOR_RO_ATTR(next_checkpoint);
693NILFS_SEGCTOR_RO_ATTR(last_seg_write_time);
694NILFS_SEGCTOR_RO_ATTR(last_seg_write_time_secs);
695NILFS_SEGCTOR_RO_ATTR(last_nongc_write_time);
696NILFS_SEGCTOR_RO_ATTR(last_nongc_write_time_secs);
697NILFS_SEGCTOR_RO_ATTR(dirty_data_blocks_count);
698NILFS_SEGCTOR_RO_ATTR(README);
699
700static struct attribute *nilfs_segctor_attrs[] = {
701 NILFS_SEGCTOR_ATTR_LIST(last_pseg_block),
702 NILFS_SEGCTOR_ATTR_LIST(last_seg_sequence),
703 NILFS_SEGCTOR_ATTR_LIST(last_seg_checkpoint),
704 NILFS_SEGCTOR_ATTR_LIST(current_seg_sequence),
705 NILFS_SEGCTOR_ATTR_LIST(current_last_full_seg),
706 NILFS_SEGCTOR_ATTR_LIST(next_full_seg),
707 NILFS_SEGCTOR_ATTR_LIST(next_pseg_offset),
708 NILFS_SEGCTOR_ATTR_LIST(next_checkpoint),
709 NILFS_SEGCTOR_ATTR_LIST(last_seg_write_time),
710 NILFS_SEGCTOR_ATTR_LIST(last_seg_write_time_secs),
711 NILFS_SEGCTOR_ATTR_LIST(last_nongc_write_time),
712 NILFS_SEGCTOR_ATTR_LIST(last_nongc_write_time_secs),
713 NILFS_SEGCTOR_ATTR_LIST(dirty_data_blocks_count),
714 NILFS_SEGCTOR_ATTR_LIST(README),
715 NULL,
716};
717
718NILFS_DEV_INT_GROUP_OPS(segctor, dev);
719NILFS_DEV_INT_GROUP_TYPE(segctor, dev);
720NILFS_DEV_INT_GROUP_FNS(segctor, dev);
721
722/************************************************************************
723 * NILFS superblock attrs *
724 ************************************************************************/
725
726static ssize_t
727nilfs_superblock_sb_write_time_show(struct nilfs_superblock_attr *attr,
728 struct the_nilfs *nilfs,
729 char *buf)
730{
731 time_t sbwtime;
732
733 down_read(&nilfs->ns_sem);
734 sbwtime = nilfs->ns_sbwtime;
735 up_read(&nilfs->ns_sem);
736
737 return NILFS_SHOW_TIME(sbwtime, buf);
738}
739
740static ssize_t
741nilfs_superblock_sb_write_time_secs_show(struct nilfs_superblock_attr *attr,
742 struct the_nilfs *nilfs,
743 char *buf)
744{
745 time_t sbwtime;
746
747 down_read(&nilfs->ns_sem);
748 sbwtime = nilfs->ns_sbwtime;
749 up_read(&nilfs->ns_sem);
750
751 return snprintf(buf, PAGE_SIZE, "%llu\n", (unsigned long long)sbwtime);
752}
753
754static ssize_t
755nilfs_superblock_sb_write_count_show(struct nilfs_superblock_attr *attr,
756 struct the_nilfs *nilfs,
757 char *buf)
758{
759 unsigned sbwcount;
760
761 down_read(&nilfs->ns_sem);
762 sbwcount = nilfs->ns_sbwcount;
763 up_read(&nilfs->ns_sem);
764
765 return snprintf(buf, PAGE_SIZE, "%u\n", sbwcount);
766}
767
768static ssize_t
769nilfs_superblock_sb_update_frequency_show(struct nilfs_superblock_attr *attr,
770 struct the_nilfs *nilfs,
771 char *buf)
772{
773 unsigned sb_update_freq;
774
775 down_read(&nilfs->ns_sem);
776 sb_update_freq = nilfs->ns_sb_update_freq;
777 up_read(&nilfs->ns_sem);
778
779 return snprintf(buf, PAGE_SIZE, "%u\n", sb_update_freq);
780}
781
782static ssize_t
783nilfs_superblock_sb_update_frequency_store(struct nilfs_superblock_attr *attr,
784 struct the_nilfs *nilfs,
785 const char *buf, size_t count)
786{
787 unsigned val;
788 int err;
789
790 err = kstrtouint(skip_spaces(buf), 0, &val);
791 if (err) {
792 printk(KERN_ERR "NILFS: unable to convert string: err=%d\n",
793 err);
794 return err;
795 }
796
797 if (val < NILFS_SB_FREQ) {
798 val = NILFS_SB_FREQ;
799 printk(KERN_WARNING "NILFS: superblock update frequency cannot be lesser than 10 seconds\n");
800 }
801
802 down_write(&nilfs->ns_sem);
803 nilfs->ns_sb_update_freq = val;
804 up_write(&nilfs->ns_sem);
805
806 return count;
807}
808
809static const char sb_readme_str[] =
810 "The superblock group contains attributes that describe\n"
811 "superblock's details.\n\n"
812 "(1) sb_write_time\n\tshow previous write time of super block "
813 "in human-readable format.\n\n"
814 "(2) sb_write_time_secs\n\tshow previous write time of super block "
815 "in seconds.\n\n"
816 "(3) sb_write_count\n\tshow write count of super block.\n\n"
817 "(4) sb_update_frequency\n"
818 "\tshow/set interval of periodical update of superblock (in seconds).\n\n"
819 "\tYou can set preferable frequency of superblock update by command:\n\n"
820 "\t'echo <val> > /sys/fs/<nilfs>/<dev>/superblock/sb_update_frequency'\n";
821
822static ssize_t
823nilfs_superblock_README_show(struct nilfs_superblock_attr *attr,
824 struct the_nilfs *nilfs, char *buf)
825{
826 return snprintf(buf, PAGE_SIZE, sb_readme_str);
827}
828
829NILFS_SUPERBLOCK_RO_ATTR(sb_write_time);
830NILFS_SUPERBLOCK_RO_ATTR(sb_write_time_secs);
831NILFS_SUPERBLOCK_RO_ATTR(sb_write_count);
832NILFS_SUPERBLOCK_RW_ATTR(sb_update_frequency);
833NILFS_SUPERBLOCK_RO_ATTR(README);
834
835static struct attribute *nilfs_superblock_attrs[] = {
836 NILFS_SUPERBLOCK_ATTR_LIST(sb_write_time),
837 NILFS_SUPERBLOCK_ATTR_LIST(sb_write_time_secs),
838 NILFS_SUPERBLOCK_ATTR_LIST(sb_write_count),
839 NILFS_SUPERBLOCK_ATTR_LIST(sb_update_frequency),
840 NILFS_SUPERBLOCK_ATTR_LIST(README),
841 NULL,
842};
843
844NILFS_DEV_INT_GROUP_OPS(superblock, dev);
845NILFS_DEV_INT_GROUP_TYPE(superblock, dev);
846NILFS_DEV_INT_GROUP_FNS(superblock, dev);
847
848/************************************************************************
849 * NILFS device attrs *
850 ************************************************************************/
851
852static
853ssize_t nilfs_dev_revision_show(struct nilfs_dev_attr *attr,
854 struct the_nilfs *nilfs,
855 char *buf)
856{
857 struct nilfs_super_block **sbp = nilfs->ns_sbp;
858 u32 major = le32_to_cpu(sbp[0]->s_rev_level);
859 u16 minor = le16_to_cpu(sbp[0]->s_minor_rev_level);
860
861 return snprintf(buf, PAGE_SIZE, "%d.%d\n", major, minor);
862}
863
864static
865ssize_t nilfs_dev_blocksize_show(struct nilfs_dev_attr *attr,
866 struct the_nilfs *nilfs,
867 char *buf)
868{
869 return snprintf(buf, PAGE_SIZE, "%u\n", nilfs->ns_blocksize);
870}
871
872static
873ssize_t nilfs_dev_device_size_show(struct nilfs_dev_attr *attr,
874 struct the_nilfs *nilfs,
875 char *buf)
876{
877 struct nilfs_super_block **sbp = nilfs->ns_sbp;
878 u64 dev_size = le64_to_cpu(sbp[0]->s_dev_size);
879
880 return snprintf(buf, PAGE_SIZE, "%llu\n", dev_size);
881}
882
883static
884ssize_t nilfs_dev_free_blocks_show(struct nilfs_dev_attr *attr,
885 struct the_nilfs *nilfs,
886 char *buf)
887{
888 sector_t free_blocks = 0;
889
890 nilfs_count_free_blocks(nilfs, &free_blocks);
891 return snprintf(buf, PAGE_SIZE, "%llu\n",
892 (unsigned long long)free_blocks);
893}
894
895static
896ssize_t nilfs_dev_uuid_show(struct nilfs_dev_attr *attr,
897 struct the_nilfs *nilfs,
898 char *buf)
899{
900 struct nilfs_super_block **sbp = nilfs->ns_sbp;
901
902 return snprintf(buf, PAGE_SIZE, "%pUb\n", sbp[0]->s_uuid);
903}
904
905static
906ssize_t nilfs_dev_volume_name_show(struct nilfs_dev_attr *attr,
907 struct the_nilfs *nilfs,
908 char *buf)
909{
910 struct nilfs_super_block **sbp = nilfs->ns_sbp;
911
912 return scnprintf(buf, sizeof(sbp[0]->s_volume_name), "%s\n",
913 sbp[0]->s_volume_name);
914}
915
916static const char dev_readme_str[] =
917 "The <device> group contains attributes that describe file system\n"
918 "partition's details.\n\n"
919 "(1) revision\n\tshow NILFS file system revision.\n\n"
920 "(2) blocksize\n\tshow volume block size in bytes.\n\n"
921 "(3) device_size\n\tshow volume size in bytes.\n\n"
922 "(4) free_blocks\n\tshow count of free blocks on volume.\n\n"
923 "(5) uuid\n\tshow volume's UUID.\n\n"
924 "(6) volume_name\n\tshow volume's name.\n\n";
925
926static ssize_t nilfs_dev_README_show(struct nilfs_dev_attr *attr,
927 struct the_nilfs *nilfs,
928 char *buf)
929{
930 return snprintf(buf, PAGE_SIZE, dev_readme_str);
931}
932
933NILFS_DEV_RO_ATTR(revision);
934NILFS_DEV_RO_ATTR(blocksize);
935NILFS_DEV_RO_ATTR(device_size);
936NILFS_DEV_RO_ATTR(free_blocks);
937NILFS_DEV_RO_ATTR(uuid);
938NILFS_DEV_RO_ATTR(volume_name);
939NILFS_DEV_RO_ATTR(README);
940
941static struct attribute *nilfs_dev_attrs[] = {
942 NILFS_DEV_ATTR_LIST(revision),
943 NILFS_DEV_ATTR_LIST(blocksize),
944 NILFS_DEV_ATTR_LIST(device_size),
945 NILFS_DEV_ATTR_LIST(free_blocks),
946 NILFS_DEV_ATTR_LIST(uuid),
947 NILFS_DEV_ATTR_LIST(volume_name),
948 NILFS_DEV_ATTR_LIST(README),
949 NULL,
950};
951
952static ssize_t nilfs_dev_attr_show(struct kobject *kobj,
953 struct attribute *attr, char *buf)
954{
955 struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
956 ns_dev_kobj);
957 struct nilfs_dev_attr *a = container_of(attr, struct nilfs_dev_attr,
958 attr);
959
960 return a->show ? a->show(a, nilfs, buf) : 0;
961}
962
963static ssize_t nilfs_dev_attr_store(struct kobject *kobj,
964 struct attribute *attr,
965 const char *buf, size_t len)
966{
967 struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
968 ns_dev_kobj);
969 struct nilfs_dev_attr *a = container_of(attr, struct nilfs_dev_attr,
970 attr);
971
972 return a->store ? a->store(a, nilfs, buf, len) : 0;
973}
974
975static void nilfs_dev_attr_release(struct kobject *kobj)
976{
977 struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
978 ns_dev_kobj);
979 complete(&nilfs->ns_dev_kobj_unregister);
980}
981
982static const struct sysfs_ops nilfs_dev_attr_ops = {
983 .show = nilfs_dev_attr_show,
984 .store = nilfs_dev_attr_store,
985};
986
987static struct kobj_type nilfs_dev_ktype = {
988 .default_attrs = nilfs_dev_attrs,
989 .sysfs_ops = &nilfs_dev_attr_ops,
990 .release = nilfs_dev_attr_release,
991};
992
993int nilfs_sysfs_create_device_group(struct super_block *sb)
994{
995 struct the_nilfs *nilfs = sb->s_fs_info;
996 size_t devgrp_size = sizeof(struct nilfs_sysfs_dev_subgroups);
997 int err;
998
999 nilfs->ns_dev_subgroups = kzalloc(devgrp_size, GFP_KERNEL);
1000 if (unlikely(!nilfs->ns_dev_subgroups)) {
1001 err = -ENOMEM;
1002 printk(KERN_ERR "NILFS: unable to allocate memory for device group\n");
1003 goto failed_create_device_group;
1004 }
1005
1006 nilfs->ns_dev_kobj.kset = nilfs_kset;
1007 init_completion(&nilfs->ns_dev_kobj_unregister);
1008 err = kobject_init_and_add(&nilfs->ns_dev_kobj, &nilfs_dev_ktype, NULL,
1009 "%s", sb->s_id);
1010 if (err)
1011 goto free_dev_subgroups;
1012
1013 err = nilfs_sysfs_create_mounted_snapshots_group(nilfs);
1014 if (err)
1015 goto cleanup_dev_kobject;
1016
1017 err = nilfs_sysfs_create_checkpoints_group(nilfs);
1018 if (err)
1019 goto delete_mounted_snapshots_group;
1020
1021 err = nilfs_sysfs_create_segments_group(nilfs);
1022 if (err)
1023 goto delete_checkpoints_group;
1024
1025 err = nilfs_sysfs_create_superblock_group(nilfs);
1026 if (err)
1027 goto delete_segments_group;
1028
1029 err = nilfs_sysfs_create_segctor_group(nilfs);
1030 if (err)
1031 goto delete_superblock_group;
1032
1033 return 0;
1034
1035delete_superblock_group:
1036 nilfs_sysfs_delete_superblock_group(nilfs);
1037
1038delete_segments_group:
1039 nilfs_sysfs_delete_segments_group(nilfs);
1040
1041delete_checkpoints_group:
1042 nilfs_sysfs_delete_checkpoints_group(nilfs);
1043
1044delete_mounted_snapshots_group:
1045 nilfs_sysfs_delete_mounted_snapshots_group(nilfs);
1046
1047cleanup_dev_kobject:
1048 kobject_del(&nilfs->ns_dev_kobj);
1049
1050free_dev_subgroups:
1051 kfree(nilfs->ns_dev_subgroups);
1052
1053failed_create_device_group:
1054 return err;
1055}
1056
1057void nilfs_sysfs_delete_device_group(struct the_nilfs *nilfs)
1058{
1059 nilfs_sysfs_delete_mounted_snapshots_group(nilfs);
1060 nilfs_sysfs_delete_checkpoints_group(nilfs);
1061 nilfs_sysfs_delete_segments_group(nilfs);
1062 nilfs_sysfs_delete_superblock_group(nilfs);
1063 nilfs_sysfs_delete_segctor_group(nilfs);
1064 kobject_del(&nilfs->ns_dev_kobj);
1065 kfree(nilfs->ns_dev_subgroups);
1066}
1067
1068/************************************************************************
1069 * NILFS feature attrs *
1070 ************************************************************************/
1071
1072static ssize_t nilfs_feature_revision_show(struct kobject *kobj,
1073 struct attribute *attr, char *buf)
1074{
1075 return snprintf(buf, PAGE_SIZE, "%d.%d\n",
1076 NILFS_CURRENT_REV, NILFS_MINOR_REV);
1077}
1078
1079static const char features_readme_str[] =
1080 "The features group contains attributes that describe NILFS file\n"
1081 "system driver features.\n\n"
1082 "(1) revision\n\tshow current revision of NILFS file system driver.\n";
1083
1084static ssize_t nilfs_feature_README_show(struct kobject *kobj,
1085 struct attribute *attr,
1086 char *buf)
1087{
1088 return snprintf(buf, PAGE_SIZE, features_readme_str);
1089}
1090
1091NILFS_FEATURE_RO_ATTR(revision);
1092NILFS_FEATURE_RO_ATTR(README);
1093
1094static struct attribute *nilfs_feature_attrs[] = {
1095 NILFS_FEATURE_ATTR_LIST(revision),
1096 NILFS_FEATURE_ATTR_LIST(README),
1097 NULL,
1098};
1099
1100static const struct attribute_group nilfs_feature_attr_group = {
1101 .name = "features",
1102 .attrs = nilfs_feature_attrs,
1103};
1104
1105int __init nilfs_sysfs_init(void)
1106{
1107 int err;
1108
1109 nilfs_kset = kset_create_and_add(NILFS_ROOT_GROUP_NAME, NULL, fs_kobj);
1110 if (!nilfs_kset) {
1111 err = -ENOMEM;
1112 printk(KERN_ERR "NILFS: unable to create sysfs entry: err %d\n",
1113 err);
1114 goto failed_sysfs_init;
1115 }
1116
1117 err = sysfs_create_group(&nilfs_kset->kobj, &nilfs_feature_attr_group);
1118 if (unlikely(err)) {
1119 printk(KERN_ERR "NILFS: unable to create feature group: err %d\n",
1120 err);
1121 goto cleanup_sysfs_init;
1122 }
1123
1124 return 0;
1125
1126cleanup_sysfs_init:
1127 kset_unregister(nilfs_kset);
1128
1129failed_sysfs_init:
1130 return err;
1131}
1132
1133void nilfs_sysfs_exit(void)
1134{
1135 sysfs_remove_group(&nilfs_kset->kobj, &nilfs_feature_attr_group);
1136 kset_unregister(nilfs_kset);
1137}