1 // SPDX-License-Identifier: GPL-2.0+
3 * sysfs.c - sysfs support implementation.
5 * Copyright (C) 2005-2014 Nippon Telegraph and Telephone Corporation.
6 * Copyright (C) 2014 HGST, Inc., a Western Digital Company.
8 * Written by Vyacheslav Dubeyko <Vyacheslav.Dubeyko@hgst.com>
11 #include <linux/kobject.h>
19 /* /sys/fs/<nilfs>/ */
20 static struct kset *nilfs_kset;
22 #define NILFS_DEV_INT_GROUP_OPS(name, parent_name) \
23 static ssize_t nilfs_##name##_attr_show(struct kobject *kobj, \
24 struct attribute *attr, char *buf) \
26 struct the_nilfs *nilfs = container_of(kobj->parent, \
28 ns_##parent_name##_kobj); \
29 struct nilfs_##name##_attr *a = container_of(attr, \
30 struct nilfs_##name##_attr, \
32 return a->show ? a->show(a, nilfs, buf) : 0; \
34 static ssize_t nilfs_##name##_attr_store(struct kobject *kobj, \
35 struct attribute *attr, \
36 const char *buf, size_t len) \
38 struct the_nilfs *nilfs = container_of(kobj->parent, \
40 ns_##parent_name##_kobj); \
41 struct nilfs_##name##_attr *a = container_of(attr, \
42 struct nilfs_##name##_attr, \
44 return a->store ? a->store(a, nilfs, buf, len) : 0; \
46 static const struct sysfs_ops nilfs_##name##_attr_ops = { \
47 .show = nilfs_##name##_attr_show, \
48 .store = nilfs_##name##_attr_store, \
51 #define NILFS_DEV_INT_GROUP_TYPE(name, parent_name) \
52 static void nilfs_##name##_attr_release(struct kobject *kobj) \
54 struct nilfs_sysfs_##parent_name##_subgroups *subgroups; \
55 struct the_nilfs *nilfs = container_of(kobj->parent, \
57 ns_##parent_name##_kobj); \
58 subgroups = nilfs->ns_##parent_name##_subgroups; \
59 complete(&subgroups->sg_##name##_kobj_unregister); \
61 static struct kobj_type nilfs_##name##_ktype = { \
62 .default_attrs = nilfs_##name##_attrs, \
63 .sysfs_ops = &nilfs_##name##_attr_ops, \
64 .release = nilfs_##name##_attr_release, \
67 #define NILFS_DEV_INT_GROUP_FNS(name, parent_name) \
68 static int nilfs_sysfs_create_##name##_group(struct the_nilfs *nilfs) \
70 struct kobject *parent; \
71 struct kobject *kobj; \
72 struct completion *kobj_unregister; \
73 struct nilfs_sysfs_##parent_name##_subgroups *subgroups; \
75 subgroups = nilfs->ns_##parent_name##_subgroups; \
76 kobj = &subgroups->sg_##name##_kobj; \
77 kobj_unregister = &subgroups->sg_##name##_kobj_unregister; \
78 parent = &nilfs->ns_##parent_name##_kobj; \
79 kobj->kset = nilfs_kset; \
80 init_completion(kobj_unregister); \
81 err = kobject_init_and_add(kobj, &nilfs_##name##_ktype, parent, \
87 static void nilfs_sysfs_delete_##name##_group(struct the_nilfs *nilfs) \
89 kobject_del(&nilfs->ns_##parent_name##_subgroups->sg_##name##_kobj); \
92 /************************************************************************
93 * NILFS snapshot attrs *
94 ************************************************************************/
97 nilfs_snapshot_inodes_count_show(struct nilfs_snapshot_attr *attr,
98 struct nilfs_root *root, char *buf)
100 return snprintf(buf, PAGE_SIZE, "%llu\n",
101 (unsigned long long)atomic64_read(&root->inodes_count));
105 nilfs_snapshot_blocks_count_show(struct nilfs_snapshot_attr *attr,
106 struct nilfs_root *root, char *buf)
108 return snprintf(buf, PAGE_SIZE, "%llu\n",
109 (unsigned long long)atomic64_read(&root->blocks_count));
112 static const char snapshot_readme_str[] =
113 "The group contains details about mounted snapshot.\n\n"
114 "(1) inodes_count\n\tshow number of inodes for snapshot.\n\n"
115 "(2) blocks_count\n\tshow number of blocks for snapshot.\n\n";
118 nilfs_snapshot_README_show(struct nilfs_snapshot_attr *attr,
119 struct nilfs_root *root, char *buf)
121 return snprintf(buf, PAGE_SIZE, snapshot_readme_str);
124 NILFS_SNAPSHOT_RO_ATTR(inodes_count);
125 NILFS_SNAPSHOT_RO_ATTR(blocks_count);
126 NILFS_SNAPSHOT_RO_ATTR(README);
128 static struct attribute *nilfs_snapshot_attrs[] = {
129 NILFS_SNAPSHOT_ATTR_LIST(inodes_count),
130 NILFS_SNAPSHOT_ATTR_LIST(blocks_count),
131 NILFS_SNAPSHOT_ATTR_LIST(README),
135 static ssize_t nilfs_snapshot_attr_show(struct kobject *kobj,
136 struct attribute *attr, char *buf)
138 struct nilfs_root *root =
139 container_of(kobj, struct nilfs_root, snapshot_kobj);
140 struct nilfs_snapshot_attr *a =
141 container_of(attr, struct nilfs_snapshot_attr, attr);
143 return a->show ? a->show(a, root, buf) : 0;
146 static ssize_t nilfs_snapshot_attr_store(struct kobject *kobj,
147 struct attribute *attr,
148 const char *buf, size_t len)
150 struct nilfs_root *root =
151 container_of(kobj, struct nilfs_root, snapshot_kobj);
152 struct nilfs_snapshot_attr *a =
153 container_of(attr, struct nilfs_snapshot_attr, attr);
155 return a->store ? a->store(a, root, buf, len) : 0;
158 static void nilfs_snapshot_attr_release(struct kobject *kobj)
160 struct nilfs_root *root = container_of(kobj, struct nilfs_root,
162 complete(&root->snapshot_kobj_unregister);
165 static const struct sysfs_ops nilfs_snapshot_attr_ops = {
166 .show = nilfs_snapshot_attr_show,
167 .store = nilfs_snapshot_attr_store,
170 static struct kobj_type nilfs_snapshot_ktype = {
171 .default_attrs = nilfs_snapshot_attrs,
172 .sysfs_ops = &nilfs_snapshot_attr_ops,
173 .release = nilfs_snapshot_attr_release,
176 int nilfs_sysfs_create_snapshot_group(struct nilfs_root *root)
178 struct the_nilfs *nilfs;
179 struct kobject *parent;
183 parent = &nilfs->ns_dev_subgroups->sg_mounted_snapshots_kobj;
184 root->snapshot_kobj.kset = nilfs_kset;
185 init_completion(&root->snapshot_kobj_unregister);
187 if (root->cno == NILFS_CPTREE_CURRENT_CNO) {
188 err = kobject_init_and_add(&root->snapshot_kobj,
189 &nilfs_snapshot_ktype,
191 "current_checkpoint");
193 err = kobject_init_and_add(&root->snapshot_kobj,
194 &nilfs_snapshot_ktype,
205 void nilfs_sysfs_delete_snapshot_group(struct nilfs_root *root)
207 kobject_del(&root->snapshot_kobj);
210 /************************************************************************
211 * NILFS mounted snapshots attrs *
212 ************************************************************************/
214 static const char mounted_snapshots_readme_str[] =
215 "The mounted_snapshots group contains group for\n"
216 "every mounted snapshot.\n";
219 nilfs_mounted_snapshots_README_show(struct nilfs_mounted_snapshots_attr *attr,
220 struct the_nilfs *nilfs, char *buf)
222 return snprintf(buf, PAGE_SIZE, mounted_snapshots_readme_str);
225 NILFS_MOUNTED_SNAPSHOTS_RO_ATTR(README);
227 static struct attribute *nilfs_mounted_snapshots_attrs[] = {
228 NILFS_MOUNTED_SNAPSHOTS_ATTR_LIST(README),
232 NILFS_DEV_INT_GROUP_OPS(mounted_snapshots, dev);
233 NILFS_DEV_INT_GROUP_TYPE(mounted_snapshots, dev);
234 NILFS_DEV_INT_GROUP_FNS(mounted_snapshots, dev);
236 /************************************************************************
237 * NILFS checkpoints attrs *
238 ************************************************************************/
241 nilfs_checkpoints_checkpoints_number_show(struct nilfs_checkpoints_attr *attr,
242 struct the_nilfs *nilfs,
246 struct nilfs_cpstat cpstat;
249 down_read(&nilfs->ns_segctor_sem);
250 err = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
251 up_read(&nilfs->ns_segctor_sem);
253 nilfs_err(nilfs->ns_sb, "unable to get checkpoint stat: err=%d",
258 ncheckpoints = cpstat.cs_ncps;
260 return snprintf(buf, PAGE_SIZE, "%llu\n", ncheckpoints);
264 nilfs_checkpoints_snapshots_number_show(struct nilfs_checkpoints_attr *attr,
265 struct the_nilfs *nilfs,
269 struct nilfs_cpstat cpstat;
272 down_read(&nilfs->ns_segctor_sem);
273 err = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
274 up_read(&nilfs->ns_segctor_sem);
276 nilfs_err(nilfs->ns_sb, "unable to get checkpoint stat: err=%d",
281 nsnapshots = cpstat.cs_nsss;
283 return snprintf(buf, PAGE_SIZE, "%llu\n", nsnapshots);
287 nilfs_checkpoints_last_seg_checkpoint_show(struct nilfs_checkpoints_attr *attr,
288 struct the_nilfs *nilfs,
293 spin_lock(&nilfs->ns_last_segment_lock);
294 last_cno = nilfs->ns_last_cno;
295 spin_unlock(&nilfs->ns_last_segment_lock);
297 return snprintf(buf, PAGE_SIZE, "%llu\n", last_cno);
301 nilfs_checkpoints_next_checkpoint_show(struct nilfs_checkpoints_attr *attr,
302 struct the_nilfs *nilfs,
307 down_read(&nilfs->ns_segctor_sem);
309 up_read(&nilfs->ns_segctor_sem);
311 return snprintf(buf, PAGE_SIZE, "%llu\n", cno);
314 static 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";
324 nilfs_checkpoints_README_show(struct nilfs_checkpoints_attr *attr,
325 struct the_nilfs *nilfs, char *buf)
327 return snprintf(buf, PAGE_SIZE, checkpoints_readme_str);
330 NILFS_CHECKPOINTS_RO_ATTR(checkpoints_number);
331 NILFS_CHECKPOINTS_RO_ATTR(snapshots_number);
332 NILFS_CHECKPOINTS_RO_ATTR(last_seg_checkpoint);
333 NILFS_CHECKPOINTS_RO_ATTR(next_checkpoint);
334 NILFS_CHECKPOINTS_RO_ATTR(README);
336 static 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),
345 NILFS_DEV_INT_GROUP_OPS(checkpoints, dev);
346 NILFS_DEV_INT_GROUP_TYPE(checkpoints, dev);
347 NILFS_DEV_INT_GROUP_FNS(checkpoints, dev);
349 /************************************************************************
350 * NILFS segments attrs *
351 ************************************************************************/
354 nilfs_segments_segments_number_show(struct nilfs_segments_attr *attr,
355 struct the_nilfs *nilfs,
358 return snprintf(buf, PAGE_SIZE, "%lu\n", nilfs->ns_nsegments);
362 nilfs_segments_blocks_per_segment_show(struct nilfs_segments_attr *attr,
363 struct the_nilfs *nilfs,
366 return snprintf(buf, PAGE_SIZE, "%lu\n", nilfs->ns_blocks_per_segment);
370 nilfs_segments_clean_segments_show(struct nilfs_segments_attr *attr,
371 struct the_nilfs *nilfs,
374 unsigned long ncleansegs;
376 down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
377 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
378 up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
380 return snprintf(buf, PAGE_SIZE, "%lu\n", ncleansegs);
384 nilfs_segments_dirty_segments_show(struct nilfs_segments_attr *attr,
385 struct the_nilfs *nilfs,
388 struct nilfs_sustat sustat;
391 down_read(&nilfs->ns_segctor_sem);
392 err = nilfs_sufile_get_stat(nilfs->ns_sufile, &sustat);
393 up_read(&nilfs->ns_segctor_sem);
395 nilfs_err(nilfs->ns_sb, "unable to get segment stat: err=%d",
400 return snprintf(buf, PAGE_SIZE, "%llu\n", sustat.ss_ndirtysegs);
403 static const char segments_readme_str[] =
404 "The segments group contains attributes that describe\n"
405 "details about volume's segments.\n\n"
406 "(1) segments_number\n\tshow number of segments on volume.\n\n"
407 "(2) blocks_per_segment\n\tshow number of blocks in segment.\n\n"
408 "(3) clean_segments\n\tshow count of clean segments.\n\n"
409 "(4) dirty_segments\n\tshow count of dirty segments.\n\n";
412 nilfs_segments_README_show(struct nilfs_segments_attr *attr,
413 struct the_nilfs *nilfs,
416 return snprintf(buf, PAGE_SIZE, segments_readme_str);
419 NILFS_SEGMENTS_RO_ATTR(segments_number);
420 NILFS_SEGMENTS_RO_ATTR(blocks_per_segment);
421 NILFS_SEGMENTS_RO_ATTR(clean_segments);
422 NILFS_SEGMENTS_RO_ATTR(dirty_segments);
423 NILFS_SEGMENTS_RO_ATTR(README);
425 static struct attribute *nilfs_segments_attrs[] = {
426 NILFS_SEGMENTS_ATTR_LIST(segments_number),
427 NILFS_SEGMENTS_ATTR_LIST(blocks_per_segment),
428 NILFS_SEGMENTS_ATTR_LIST(clean_segments),
429 NILFS_SEGMENTS_ATTR_LIST(dirty_segments),
430 NILFS_SEGMENTS_ATTR_LIST(README),
434 NILFS_DEV_INT_GROUP_OPS(segments, dev);
435 NILFS_DEV_INT_GROUP_TYPE(segments, dev);
436 NILFS_DEV_INT_GROUP_FNS(segments, dev);
438 /************************************************************************
439 * NILFS segctor attrs *
440 ************************************************************************/
443 nilfs_segctor_last_pseg_block_show(struct nilfs_segctor_attr *attr,
444 struct the_nilfs *nilfs,
449 spin_lock(&nilfs->ns_last_segment_lock);
450 last_pseg = nilfs->ns_last_pseg;
451 spin_unlock(&nilfs->ns_last_segment_lock);
453 return snprintf(buf, PAGE_SIZE, "%llu\n",
454 (unsigned long long)last_pseg);
458 nilfs_segctor_last_seg_sequence_show(struct nilfs_segctor_attr *attr,
459 struct the_nilfs *nilfs,
464 spin_lock(&nilfs->ns_last_segment_lock);
465 last_seq = nilfs->ns_last_seq;
466 spin_unlock(&nilfs->ns_last_segment_lock);
468 return snprintf(buf, PAGE_SIZE, "%llu\n", last_seq);
472 nilfs_segctor_last_seg_checkpoint_show(struct nilfs_segctor_attr *attr,
473 struct the_nilfs *nilfs,
478 spin_lock(&nilfs->ns_last_segment_lock);
479 last_cno = nilfs->ns_last_cno;
480 spin_unlock(&nilfs->ns_last_segment_lock);
482 return snprintf(buf, PAGE_SIZE, "%llu\n", last_cno);
486 nilfs_segctor_current_seg_sequence_show(struct nilfs_segctor_attr *attr,
487 struct the_nilfs *nilfs,
492 down_read(&nilfs->ns_segctor_sem);
493 seg_seq = nilfs->ns_seg_seq;
494 up_read(&nilfs->ns_segctor_sem);
496 return snprintf(buf, PAGE_SIZE, "%llu\n", seg_seq);
500 nilfs_segctor_current_last_full_seg_show(struct nilfs_segctor_attr *attr,
501 struct the_nilfs *nilfs,
506 down_read(&nilfs->ns_segctor_sem);
507 segnum = nilfs->ns_segnum;
508 up_read(&nilfs->ns_segctor_sem);
510 return snprintf(buf, PAGE_SIZE, "%llu\n", segnum);
514 nilfs_segctor_next_full_seg_show(struct nilfs_segctor_attr *attr,
515 struct the_nilfs *nilfs,
520 down_read(&nilfs->ns_segctor_sem);
521 nextnum = nilfs->ns_nextnum;
522 up_read(&nilfs->ns_segctor_sem);
524 return snprintf(buf, PAGE_SIZE, "%llu\n", nextnum);
528 nilfs_segctor_next_pseg_offset_show(struct nilfs_segctor_attr *attr,
529 struct the_nilfs *nilfs,
532 unsigned long pseg_offset;
534 down_read(&nilfs->ns_segctor_sem);
535 pseg_offset = nilfs->ns_pseg_offset;
536 up_read(&nilfs->ns_segctor_sem);
538 return snprintf(buf, PAGE_SIZE, "%lu\n", pseg_offset);
542 nilfs_segctor_next_checkpoint_show(struct nilfs_segctor_attr *attr,
543 struct the_nilfs *nilfs,
548 down_read(&nilfs->ns_segctor_sem);
550 up_read(&nilfs->ns_segctor_sem);
552 return snprintf(buf, PAGE_SIZE, "%llu\n", cno);
556 nilfs_segctor_last_seg_write_time_show(struct nilfs_segctor_attr *attr,
557 struct the_nilfs *nilfs,
562 down_read(&nilfs->ns_segctor_sem);
563 ctime = nilfs->ns_ctime;
564 up_read(&nilfs->ns_segctor_sem);
566 return sysfs_emit(buf, "%ptTs\n", &ctime);
570 nilfs_segctor_last_seg_write_time_secs_show(struct nilfs_segctor_attr *attr,
571 struct the_nilfs *nilfs,
576 down_read(&nilfs->ns_segctor_sem);
577 ctime = nilfs->ns_ctime;
578 up_read(&nilfs->ns_segctor_sem);
580 return snprintf(buf, PAGE_SIZE, "%llu\n", ctime);
584 nilfs_segctor_last_nongc_write_time_show(struct nilfs_segctor_attr *attr,
585 struct the_nilfs *nilfs,
588 time64_t nongc_ctime;
590 down_read(&nilfs->ns_segctor_sem);
591 nongc_ctime = nilfs->ns_nongc_ctime;
592 up_read(&nilfs->ns_segctor_sem);
594 return sysfs_emit(buf, "%ptTs\n", &nongc_ctime);
598 nilfs_segctor_last_nongc_write_time_secs_show(struct nilfs_segctor_attr *attr,
599 struct the_nilfs *nilfs,
602 time64_t nongc_ctime;
604 down_read(&nilfs->ns_segctor_sem);
605 nongc_ctime = nilfs->ns_nongc_ctime;
606 up_read(&nilfs->ns_segctor_sem);
608 return snprintf(buf, PAGE_SIZE, "%llu\n", nongc_ctime);
612 nilfs_segctor_dirty_data_blocks_count_show(struct nilfs_segctor_attr *attr,
613 struct the_nilfs *nilfs,
618 down_read(&nilfs->ns_segctor_sem);
619 ndirtyblks = atomic_read(&nilfs->ns_ndirtyblks);
620 up_read(&nilfs->ns_segctor_sem);
622 return snprintf(buf, PAGE_SIZE, "%u\n", ndirtyblks);
625 static const char segctor_readme_str[] =
626 "The segctor group contains attributes that describe\n"
627 "segctor thread activity details.\n\n"
628 "(1) last_pseg_block\n"
629 "\tshow start block number of the latest segment.\n\n"
630 "(2) last_seg_sequence\n"
631 "\tshow sequence value of the latest segment.\n\n"
632 "(3) last_seg_checkpoint\n"
633 "\tshow checkpoint number of the latest segment.\n\n"
634 "(4) current_seg_sequence\n\tshow segment sequence counter.\n\n"
635 "(5) current_last_full_seg\n"
636 "\tshow index number of the latest full segment.\n\n"
637 "(6) next_full_seg\n"
638 "\tshow index number of the full segment index to be used next.\n\n"
639 "(7) next_pseg_offset\n"
640 "\tshow offset of next partial segment in the current full segment.\n\n"
641 "(8) next_checkpoint\n\tshow next checkpoint number.\n\n"
642 "(9) last_seg_write_time\n"
643 "\tshow write time of the last segment in human-readable format.\n\n"
644 "(10) last_seg_write_time_secs\n"
645 "\tshow write time of the last segment in seconds.\n\n"
646 "(11) last_nongc_write_time\n"
647 "\tshow write time of the last segment not for cleaner operation "
648 "in human-readable format.\n\n"
649 "(12) last_nongc_write_time_secs\n"
650 "\tshow write time of the last segment not for cleaner operation "
652 "(13) dirty_data_blocks_count\n"
653 "\tshow number of dirty data blocks.\n\n";
656 nilfs_segctor_README_show(struct nilfs_segctor_attr *attr,
657 struct the_nilfs *nilfs, char *buf)
659 return snprintf(buf, PAGE_SIZE, segctor_readme_str);
662 NILFS_SEGCTOR_RO_ATTR(last_pseg_block);
663 NILFS_SEGCTOR_RO_ATTR(last_seg_sequence);
664 NILFS_SEGCTOR_RO_ATTR(last_seg_checkpoint);
665 NILFS_SEGCTOR_RO_ATTR(current_seg_sequence);
666 NILFS_SEGCTOR_RO_ATTR(current_last_full_seg);
667 NILFS_SEGCTOR_RO_ATTR(next_full_seg);
668 NILFS_SEGCTOR_RO_ATTR(next_pseg_offset);
669 NILFS_SEGCTOR_RO_ATTR(next_checkpoint);
670 NILFS_SEGCTOR_RO_ATTR(last_seg_write_time);
671 NILFS_SEGCTOR_RO_ATTR(last_seg_write_time_secs);
672 NILFS_SEGCTOR_RO_ATTR(last_nongc_write_time);
673 NILFS_SEGCTOR_RO_ATTR(last_nongc_write_time_secs);
674 NILFS_SEGCTOR_RO_ATTR(dirty_data_blocks_count);
675 NILFS_SEGCTOR_RO_ATTR(README);
677 static struct attribute *nilfs_segctor_attrs[] = {
678 NILFS_SEGCTOR_ATTR_LIST(last_pseg_block),
679 NILFS_SEGCTOR_ATTR_LIST(last_seg_sequence),
680 NILFS_SEGCTOR_ATTR_LIST(last_seg_checkpoint),
681 NILFS_SEGCTOR_ATTR_LIST(current_seg_sequence),
682 NILFS_SEGCTOR_ATTR_LIST(current_last_full_seg),
683 NILFS_SEGCTOR_ATTR_LIST(next_full_seg),
684 NILFS_SEGCTOR_ATTR_LIST(next_pseg_offset),
685 NILFS_SEGCTOR_ATTR_LIST(next_checkpoint),
686 NILFS_SEGCTOR_ATTR_LIST(last_seg_write_time),
687 NILFS_SEGCTOR_ATTR_LIST(last_seg_write_time_secs),
688 NILFS_SEGCTOR_ATTR_LIST(last_nongc_write_time),
689 NILFS_SEGCTOR_ATTR_LIST(last_nongc_write_time_secs),
690 NILFS_SEGCTOR_ATTR_LIST(dirty_data_blocks_count),
691 NILFS_SEGCTOR_ATTR_LIST(README),
695 NILFS_DEV_INT_GROUP_OPS(segctor, dev);
696 NILFS_DEV_INT_GROUP_TYPE(segctor, dev);
697 NILFS_DEV_INT_GROUP_FNS(segctor, dev);
699 /************************************************************************
700 * NILFS superblock attrs *
701 ************************************************************************/
704 nilfs_superblock_sb_write_time_show(struct nilfs_superblock_attr *attr,
705 struct the_nilfs *nilfs,
710 down_read(&nilfs->ns_sem);
711 sbwtime = nilfs->ns_sbwtime;
712 up_read(&nilfs->ns_sem);
714 return sysfs_emit(buf, "%ptTs\n", &sbwtime);
718 nilfs_superblock_sb_write_time_secs_show(struct nilfs_superblock_attr *attr,
719 struct the_nilfs *nilfs,
724 down_read(&nilfs->ns_sem);
725 sbwtime = nilfs->ns_sbwtime;
726 up_read(&nilfs->ns_sem);
728 return snprintf(buf, PAGE_SIZE, "%llu\n", sbwtime);
732 nilfs_superblock_sb_write_count_show(struct nilfs_superblock_attr *attr,
733 struct the_nilfs *nilfs,
736 unsigned int sbwcount;
738 down_read(&nilfs->ns_sem);
739 sbwcount = nilfs->ns_sbwcount;
740 up_read(&nilfs->ns_sem);
742 return snprintf(buf, PAGE_SIZE, "%u\n", sbwcount);
746 nilfs_superblock_sb_update_frequency_show(struct nilfs_superblock_attr *attr,
747 struct the_nilfs *nilfs,
750 unsigned int sb_update_freq;
752 down_read(&nilfs->ns_sem);
753 sb_update_freq = nilfs->ns_sb_update_freq;
754 up_read(&nilfs->ns_sem);
756 return snprintf(buf, PAGE_SIZE, "%u\n", sb_update_freq);
760 nilfs_superblock_sb_update_frequency_store(struct nilfs_superblock_attr *attr,
761 struct the_nilfs *nilfs,
762 const char *buf, size_t count)
767 err = kstrtouint(skip_spaces(buf), 0, &val);
769 nilfs_err(nilfs->ns_sb, "unable to convert string: err=%d",
774 if (val < NILFS_SB_FREQ) {
776 nilfs_warn(nilfs->ns_sb,
777 "superblock update frequency cannot be lesser than 10 seconds");
780 down_write(&nilfs->ns_sem);
781 nilfs->ns_sb_update_freq = val;
782 up_write(&nilfs->ns_sem);
787 static const char sb_readme_str[] =
788 "The superblock group contains attributes that describe\n"
789 "superblock's details.\n\n"
790 "(1) sb_write_time\n\tshow previous write time of super block "
791 "in human-readable format.\n\n"
792 "(2) sb_write_time_secs\n\tshow previous write time of super block "
794 "(3) sb_write_count\n\tshow write count of super block.\n\n"
795 "(4) sb_update_frequency\n"
796 "\tshow/set interval of periodical update of superblock (in seconds).\n\n"
797 "\tYou can set preferable frequency of superblock update by command:\n\n"
798 "\t'echo <val> > /sys/fs/<nilfs>/<dev>/superblock/sb_update_frequency'\n";
801 nilfs_superblock_README_show(struct nilfs_superblock_attr *attr,
802 struct the_nilfs *nilfs, char *buf)
804 return snprintf(buf, PAGE_SIZE, sb_readme_str);
807 NILFS_SUPERBLOCK_RO_ATTR(sb_write_time);
808 NILFS_SUPERBLOCK_RO_ATTR(sb_write_time_secs);
809 NILFS_SUPERBLOCK_RO_ATTR(sb_write_count);
810 NILFS_SUPERBLOCK_RW_ATTR(sb_update_frequency);
811 NILFS_SUPERBLOCK_RO_ATTR(README);
813 static struct attribute *nilfs_superblock_attrs[] = {
814 NILFS_SUPERBLOCK_ATTR_LIST(sb_write_time),
815 NILFS_SUPERBLOCK_ATTR_LIST(sb_write_time_secs),
816 NILFS_SUPERBLOCK_ATTR_LIST(sb_write_count),
817 NILFS_SUPERBLOCK_ATTR_LIST(sb_update_frequency),
818 NILFS_SUPERBLOCK_ATTR_LIST(README),
822 NILFS_DEV_INT_GROUP_OPS(superblock, dev);
823 NILFS_DEV_INT_GROUP_TYPE(superblock, dev);
824 NILFS_DEV_INT_GROUP_FNS(superblock, dev);
826 /************************************************************************
827 * NILFS device attrs *
828 ************************************************************************/
831 ssize_t nilfs_dev_revision_show(struct nilfs_dev_attr *attr,
832 struct the_nilfs *nilfs,
835 struct nilfs_super_block **sbp = nilfs->ns_sbp;
836 u32 major = le32_to_cpu(sbp[0]->s_rev_level);
837 u16 minor = le16_to_cpu(sbp[0]->s_minor_rev_level);
839 return snprintf(buf, PAGE_SIZE, "%d.%d\n", major, minor);
843 ssize_t nilfs_dev_blocksize_show(struct nilfs_dev_attr *attr,
844 struct the_nilfs *nilfs,
847 return snprintf(buf, PAGE_SIZE, "%u\n", nilfs->ns_blocksize);
851 ssize_t nilfs_dev_device_size_show(struct nilfs_dev_attr *attr,
852 struct the_nilfs *nilfs,
855 struct nilfs_super_block **sbp = nilfs->ns_sbp;
856 u64 dev_size = le64_to_cpu(sbp[0]->s_dev_size);
858 return snprintf(buf, PAGE_SIZE, "%llu\n", dev_size);
862 ssize_t nilfs_dev_free_blocks_show(struct nilfs_dev_attr *attr,
863 struct the_nilfs *nilfs,
866 sector_t free_blocks = 0;
868 nilfs_count_free_blocks(nilfs, &free_blocks);
869 return snprintf(buf, PAGE_SIZE, "%llu\n",
870 (unsigned long long)free_blocks);
874 ssize_t nilfs_dev_uuid_show(struct nilfs_dev_attr *attr,
875 struct the_nilfs *nilfs,
878 struct nilfs_super_block **sbp = nilfs->ns_sbp;
880 return snprintf(buf, PAGE_SIZE, "%pUb\n", sbp[0]->s_uuid);
884 ssize_t nilfs_dev_volume_name_show(struct nilfs_dev_attr *attr,
885 struct the_nilfs *nilfs,
888 struct nilfs_super_block **sbp = nilfs->ns_sbp;
890 return scnprintf(buf, sizeof(sbp[0]->s_volume_name), "%s\n",
891 sbp[0]->s_volume_name);
894 static const char dev_readme_str[] =
895 "The <device> group contains attributes that describe file system\n"
896 "partition's details.\n\n"
897 "(1) revision\n\tshow NILFS file system revision.\n\n"
898 "(2) blocksize\n\tshow volume block size in bytes.\n\n"
899 "(3) device_size\n\tshow volume size in bytes.\n\n"
900 "(4) free_blocks\n\tshow count of free blocks on volume.\n\n"
901 "(5) uuid\n\tshow volume's UUID.\n\n"
902 "(6) volume_name\n\tshow volume's name.\n\n";
904 static ssize_t nilfs_dev_README_show(struct nilfs_dev_attr *attr,
905 struct the_nilfs *nilfs,
908 return snprintf(buf, PAGE_SIZE, dev_readme_str);
911 NILFS_DEV_RO_ATTR(revision);
912 NILFS_DEV_RO_ATTR(blocksize);
913 NILFS_DEV_RO_ATTR(device_size);
914 NILFS_DEV_RO_ATTR(free_blocks);
915 NILFS_DEV_RO_ATTR(uuid);
916 NILFS_DEV_RO_ATTR(volume_name);
917 NILFS_DEV_RO_ATTR(README);
919 static struct attribute *nilfs_dev_attrs[] = {
920 NILFS_DEV_ATTR_LIST(revision),
921 NILFS_DEV_ATTR_LIST(blocksize),
922 NILFS_DEV_ATTR_LIST(device_size),
923 NILFS_DEV_ATTR_LIST(free_blocks),
924 NILFS_DEV_ATTR_LIST(uuid),
925 NILFS_DEV_ATTR_LIST(volume_name),
926 NILFS_DEV_ATTR_LIST(README),
930 static ssize_t nilfs_dev_attr_show(struct kobject *kobj,
931 struct attribute *attr, char *buf)
933 struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
935 struct nilfs_dev_attr *a = container_of(attr, struct nilfs_dev_attr,
938 return a->show ? a->show(a, nilfs, buf) : 0;
941 static ssize_t nilfs_dev_attr_store(struct kobject *kobj,
942 struct attribute *attr,
943 const char *buf, size_t len)
945 struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
947 struct nilfs_dev_attr *a = container_of(attr, struct nilfs_dev_attr,
950 return a->store ? a->store(a, nilfs, buf, len) : 0;
953 static void nilfs_dev_attr_release(struct kobject *kobj)
955 struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
957 complete(&nilfs->ns_dev_kobj_unregister);
960 static const struct sysfs_ops nilfs_dev_attr_ops = {
961 .show = nilfs_dev_attr_show,
962 .store = nilfs_dev_attr_store,
965 static struct kobj_type nilfs_dev_ktype = {
966 .default_attrs = nilfs_dev_attrs,
967 .sysfs_ops = &nilfs_dev_attr_ops,
968 .release = nilfs_dev_attr_release,
971 int nilfs_sysfs_create_device_group(struct super_block *sb)
973 struct the_nilfs *nilfs = sb->s_fs_info;
974 size_t devgrp_size = sizeof(struct nilfs_sysfs_dev_subgroups);
977 nilfs->ns_dev_subgroups = kzalloc(devgrp_size, GFP_KERNEL);
978 if (unlikely(!nilfs->ns_dev_subgroups)) {
980 nilfs_err(sb, "unable to allocate memory for device group");
981 goto failed_create_device_group;
984 nilfs->ns_dev_kobj.kset = nilfs_kset;
985 init_completion(&nilfs->ns_dev_kobj_unregister);
986 err = kobject_init_and_add(&nilfs->ns_dev_kobj, &nilfs_dev_ktype, NULL,
989 goto free_dev_subgroups;
991 err = nilfs_sysfs_create_mounted_snapshots_group(nilfs);
993 goto cleanup_dev_kobject;
995 err = nilfs_sysfs_create_checkpoints_group(nilfs);
997 goto delete_mounted_snapshots_group;
999 err = nilfs_sysfs_create_segments_group(nilfs);
1001 goto delete_checkpoints_group;
1003 err = nilfs_sysfs_create_superblock_group(nilfs);
1005 goto delete_segments_group;
1007 err = nilfs_sysfs_create_segctor_group(nilfs);
1009 goto delete_superblock_group;
1013 delete_superblock_group:
1014 nilfs_sysfs_delete_superblock_group(nilfs);
1016 delete_segments_group:
1017 nilfs_sysfs_delete_segments_group(nilfs);
1019 delete_checkpoints_group:
1020 nilfs_sysfs_delete_checkpoints_group(nilfs);
1022 delete_mounted_snapshots_group:
1023 nilfs_sysfs_delete_mounted_snapshots_group(nilfs);
1025 cleanup_dev_kobject:
1026 kobject_del(&nilfs->ns_dev_kobj);
1029 kfree(nilfs->ns_dev_subgroups);
1031 failed_create_device_group:
1035 void nilfs_sysfs_delete_device_group(struct the_nilfs *nilfs)
1037 nilfs_sysfs_delete_mounted_snapshots_group(nilfs);
1038 nilfs_sysfs_delete_checkpoints_group(nilfs);
1039 nilfs_sysfs_delete_segments_group(nilfs);
1040 nilfs_sysfs_delete_superblock_group(nilfs);
1041 nilfs_sysfs_delete_segctor_group(nilfs);
1042 kobject_del(&nilfs->ns_dev_kobj);
1043 kobject_put(&nilfs->ns_dev_kobj);
1044 kfree(nilfs->ns_dev_subgroups);
1047 /************************************************************************
1048 * NILFS feature attrs *
1049 ************************************************************************/
1051 static ssize_t nilfs_feature_revision_show(struct kobject *kobj,
1052 struct attribute *attr, char *buf)
1054 return snprintf(buf, PAGE_SIZE, "%d.%d\n",
1055 NILFS_CURRENT_REV, NILFS_MINOR_REV);
1058 static const char features_readme_str[] =
1059 "The features group contains attributes that describe NILFS file\n"
1060 "system driver features.\n\n"
1061 "(1) revision\n\tshow current revision of NILFS file system driver.\n";
1063 static ssize_t nilfs_feature_README_show(struct kobject *kobj,
1064 struct attribute *attr,
1067 return snprintf(buf, PAGE_SIZE, features_readme_str);
1070 NILFS_FEATURE_RO_ATTR(revision);
1071 NILFS_FEATURE_RO_ATTR(README);
1073 static struct attribute *nilfs_feature_attrs[] = {
1074 NILFS_FEATURE_ATTR_LIST(revision),
1075 NILFS_FEATURE_ATTR_LIST(README),
1079 static const struct attribute_group nilfs_feature_attr_group = {
1081 .attrs = nilfs_feature_attrs,
1084 int __init nilfs_sysfs_init(void)
1088 nilfs_kset = kset_create_and_add(NILFS_ROOT_GROUP_NAME, NULL, fs_kobj);
1091 nilfs_err(NULL, "unable to create sysfs entry: err=%d", err);
1092 goto failed_sysfs_init;
1095 err = sysfs_create_group(&nilfs_kset->kobj, &nilfs_feature_attr_group);
1096 if (unlikely(err)) {
1097 nilfs_err(NULL, "unable to create feature group: err=%d", err);
1098 goto cleanup_sysfs_init;
1104 kset_unregister(nilfs_kset);
1110 void nilfs_sysfs_exit(void)
1112 sysfs_remove_group(&nilfs_kset->kobj, &nilfs_feature_attr_group);
1113 kset_unregister(nilfs_kset);