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_SHOW_TIME(time_t_val, buf) ({ \
25 time64_to_tm(time_t_val, 0, &res); \
26 res.tm_year += 1900; \
28 count = scnprintf(buf, PAGE_SIZE, \
29 "%ld-%.2d-%.2d %.2d:%.2d:%.2d\n", \
30 res.tm_year, res.tm_mon, res.tm_mday, \
31 res.tm_hour, res.tm_min, res.tm_sec);\
35 #define NILFS_DEV_INT_GROUP_OPS(name, parent_name) \
36 static ssize_t nilfs_##name##_attr_show(struct kobject *kobj, \
37 struct attribute *attr, char *buf) \
39 struct the_nilfs *nilfs = container_of(kobj->parent, \
41 ns_##parent_name##_kobj); \
42 struct nilfs_##name##_attr *a = container_of(attr, \
43 struct nilfs_##name##_attr, \
45 return a->show ? a->show(a, nilfs, buf) : 0; \
47 static ssize_t nilfs_##name##_attr_store(struct kobject *kobj, \
48 struct attribute *attr, \
49 const char *buf, size_t len) \
51 struct the_nilfs *nilfs = container_of(kobj->parent, \
53 ns_##parent_name##_kobj); \
54 struct nilfs_##name##_attr *a = container_of(attr, \
55 struct nilfs_##name##_attr, \
57 return a->store ? a->store(a, nilfs, buf, len) : 0; \
59 static const struct sysfs_ops nilfs_##name##_attr_ops = { \
60 .show = nilfs_##name##_attr_show, \
61 .store = nilfs_##name##_attr_store, \
64 #define NILFS_DEV_INT_GROUP_TYPE(name, parent_name) \
65 static void nilfs_##name##_attr_release(struct kobject *kobj) \
67 struct nilfs_sysfs_##parent_name##_subgroups *subgroups = container_of(kobj, \
68 struct nilfs_sysfs_##parent_name##_subgroups, \
70 complete(&subgroups->sg_##name##_kobj_unregister); \
72 static struct kobj_type nilfs_##name##_ktype = { \
73 .default_attrs = nilfs_##name##_attrs, \
74 .sysfs_ops = &nilfs_##name##_attr_ops, \
75 .release = nilfs_##name##_attr_release, \
78 #define NILFS_DEV_INT_GROUP_FNS(name, parent_name) \
79 static int nilfs_sysfs_create_##name##_group(struct the_nilfs *nilfs) \
81 struct kobject *parent; \
82 struct kobject *kobj; \
83 struct completion *kobj_unregister; \
84 struct nilfs_sysfs_##parent_name##_subgroups *subgroups; \
86 subgroups = nilfs->ns_##parent_name##_subgroups; \
87 kobj = &subgroups->sg_##name##_kobj; \
88 kobj_unregister = &subgroups->sg_##name##_kobj_unregister; \
89 parent = &nilfs->ns_##parent_name##_kobj; \
90 kobj->kset = nilfs_kset; \
91 init_completion(kobj_unregister); \
92 err = kobject_init_and_add(kobj, &nilfs_##name##_ktype, parent, \
98 static void nilfs_sysfs_delete_##name##_group(struct the_nilfs *nilfs) \
100 kobject_put(&nilfs->ns_##parent_name##_subgroups->sg_##name##_kobj); \
103 /************************************************************************
104 * NILFS snapshot attrs *
105 ************************************************************************/
108 nilfs_snapshot_inodes_count_show(struct nilfs_snapshot_attr *attr,
109 struct nilfs_root *root, char *buf)
111 return snprintf(buf, PAGE_SIZE, "%llu\n",
112 (unsigned long long)atomic64_read(&root->inodes_count));
116 nilfs_snapshot_blocks_count_show(struct nilfs_snapshot_attr *attr,
117 struct nilfs_root *root, char *buf)
119 return snprintf(buf, PAGE_SIZE, "%llu\n",
120 (unsigned long long)atomic64_read(&root->blocks_count));
123 static const char snapshot_readme_str[] =
124 "The group contains details about mounted snapshot.\n\n"
125 "(1) inodes_count\n\tshow number of inodes for snapshot.\n\n"
126 "(2) blocks_count\n\tshow number of blocks for snapshot.\n\n";
129 nilfs_snapshot_README_show(struct nilfs_snapshot_attr *attr,
130 struct nilfs_root *root, char *buf)
132 return snprintf(buf, PAGE_SIZE, snapshot_readme_str);
135 NILFS_SNAPSHOT_RO_ATTR(inodes_count);
136 NILFS_SNAPSHOT_RO_ATTR(blocks_count);
137 NILFS_SNAPSHOT_RO_ATTR(README);
139 static struct attribute *nilfs_snapshot_attrs[] = {
140 NILFS_SNAPSHOT_ATTR_LIST(inodes_count),
141 NILFS_SNAPSHOT_ATTR_LIST(blocks_count),
142 NILFS_SNAPSHOT_ATTR_LIST(README),
146 static ssize_t nilfs_snapshot_attr_show(struct kobject *kobj,
147 struct attribute *attr, char *buf)
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);
154 return a->show ? a->show(a, root, buf) : 0;
157 static ssize_t nilfs_snapshot_attr_store(struct kobject *kobj,
158 struct attribute *attr,
159 const char *buf, size_t len)
161 struct nilfs_root *root =
162 container_of(kobj, struct nilfs_root, snapshot_kobj);
163 struct nilfs_snapshot_attr *a =
164 container_of(attr, struct nilfs_snapshot_attr, attr);
166 return a->store ? a->store(a, root, buf, len) : 0;
169 static void nilfs_snapshot_attr_release(struct kobject *kobj)
171 struct nilfs_root *root = container_of(kobj, struct nilfs_root,
173 complete(&root->snapshot_kobj_unregister);
176 static const struct sysfs_ops nilfs_snapshot_attr_ops = {
177 .show = nilfs_snapshot_attr_show,
178 .store = nilfs_snapshot_attr_store,
181 static struct kobj_type nilfs_snapshot_ktype = {
182 .default_attrs = nilfs_snapshot_attrs,
183 .sysfs_ops = &nilfs_snapshot_attr_ops,
184 .release = nilfs_snapshot_attr_release,
187 int nilfs_sysfs_create_snapshot_group(struct nilfs_root *root)
189 struct the_nilfs *nilfs;
190 struct kobject *parent;
194 parent = &nilfs->ns_dev_subgroups->sg_mounted_snapshots_kobj;
195 root->snapshot_kobj.kset = nilfs_kset;
196 init_completion(&root->snapshot_kobj_unregister);
198 if (root->cno == NILFS_CPTREE_CURRENT_CNO) {
199 err = kobject_init_and_add(&root->snapshot_kobj,
200 &nilfs_snapshot_ktype,
202 "current_checkpoint");
204 err = kobject_init_and_add(&root->snapshot_kobj,
205 &nilfs_snapshot_ktype,
211 kobject_put(&root->snapshot_kobj);
216 void nilfs_sysfs_delete_snapshot_group(struct nilfs_root *root)
218 kobject_put(&root->snapshot_kobj);
221 /************************************************************************
222 * NILFS mounted snapshots attrs *
223 ************************************************************************/
225 static const char mounted_snapshots_readme_str[] =
226 "The mounted_snapshots group contains group for\n"
227 "every mounted snapshot.\n";
230 nilfs_mounted_snapshots_README_show(struct nilfs_mounted_snapshots_attr *attr,
231 struct the_nilfs *nilfs, char *buf)
233 return snprintf(buf, PAGE_SIZE, mounted_snapshots_readme_str);
236 NILFS_MOUNTED_SNAPSHOTS_RO_ATTR(README);
238 static struct attribute *nilfs_mounted_snapshots_attrs[] = {
239 NILFS_MOUNTED_SNAPSHOTS_ATTR_LIST(README),
243 NILFS_DEV_INT_GROUP_OPS(mounted_snapshots, dev);
244 NILFS_DEV_INT_GROUP_TYPE(mounted_snapshots, dev);
245 NILFS_DEV_INT_GROUP_FNS(mounted_snapshots, dev);
247 /************************************************************************
248 * NILFS checkpoints attrs *
249 ************************************************************************/
252 nilfs_checkpoints_checkpoints_number_show(struct nilfs_checkpoints_attr *attr,
253 struct the_nilfs *nilfs,
257 struct nilfs_cpstat cpstat;
260 down_read(&nilfs->ns_segctor_sem);
261 err = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
262 up_read(&nilfs->ns_segctor_sem);
264 nilfs_err(nilfs->ns_sb, "unable to get checkpoint stat: err=%d",
269 ncheckpoints = cpstat.cs_ncps;
271 return snprintf(buf, PAGE_SIZE, "%llu\n", ncheckpoints);
275 nilfs_checkpoints_snapshots_number_show(struct nilfs_checkpoints_attr *attr,
276 struct the_nilfs *nilfs,
280 struct nilfs_cpstat cpstat;
283 down_read(&nilfs->ns_segctor_sem);
284 err = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
285 up_read(&nilfs->ns_segctor_sem);
287 nilfs_err(nilfs->ns_sb, "unable to get checkpoint stat: err=%d",
292 nsnapshots = cpstat.cs_nsss;
294 return snprintf(buf, PAGE_SIZE, "%llu\n", nsnapshots);
298 nilfs_checkpoints_last_seg_checkpoint_show(struct nilfs_checkpoints_attr *attr,
299 struct the_nilfs *nilfs,
304 spin_lock(&nilfs->ns_last_segment_lock);
305 last_cno = nilfs->ns_last_cno;
306 spin_unlock(&nilfs->ns_last_segment_lock);
308 return snprintf(buf, PAGE_SIZE, "%llu\n", last_cno);
312 nilfs_checkpoints_next_checkpoint_show(struct nilfs_checkpoints_attr *attr,
313 struct the_nilfs *nilfs,
318 down_read(&nilfs->ns_segctor_sem);
320 up_read(&nilfs->ns_segctor_sem);
322 return snprintf(buf, PAGE_SIZE, "%llu\n", cno);
325 static const char checkpoints_readme_str[] =
326 "The checkpoints group contains attributes that describe\n"
327 "details about volume's checkpoints.\n\n"
328 "(1) checkpoints_number\n\tshow number of checkpoints on volume.\n\n"
329 "(2) snapshots_number\n\tshow number of snapshots on volume.\n\n"
330 "(3) last_seg_checkpoint\n"
331 "\tshow checkpoint number of the latest segment.\n\n"
332 "(4) next_checkpoint\n\tshow next checkpoint number.\n\n";
335 nilfs_checkpoints_README_show(struct nilfs_checkpoints_attr *attr,
336 struct the_nilfs *nilfs, char *buf)
338 return snprintf(buf, PAGE_SIZE, checkpoints_readme_str);
341 NILFS_CHECKPOINTS_RO_ATTR(checkpoints_number);
342 NILFS_CHECKPOINTS_RO_ATTR(snapshots_number);
343 NILFS_CHECKPOINTS_RO_ATTR(last_seg_checkpoint);
344 NILFS_CHECKPOINTS_RO_ATTR(next_checkpoint);
345 NILFS_CHECKPOINTS_RO_ATTR(README);
347 static struct attribute *nilfs_checkpoints_attrs[] = {
348 NILFS_CHECKPOINTS_ATTR_LIST(checkpoints_number),
349 NILFS_CHECKPOINTS_ATTR_LIST(snapshots_number),
350 NILFS_CHECKPOINTS_ATTR_LIST(last_seg_checkpoint),
351 NILFS_CHECKPOINTS_ATTR_LIST(next_checkpoint),
352 NILFS_CHECKPOINTS_ATTR_LIST(README),
356 NILFS_DEV_INT_GROUP_OPS(checkpoints, dev);
357 NILFS_DEV_INT_GROUP_TYPE(checkpoints, dev);
358 NILFS_DEV_INT_GROUP_FNS(checkpoints, dev);
360 /************************************************************************
361 * NILFS segments attrs *
362 ************************************************************************/
365 nilfs_segments_segments_number_show(struct nilfs_segments_attr *attr,
366 struct the_nilfs *nilfs,
369 return snprintf(buf, PAGE_SIZE, "%lu\n", nilfs->ns_nsegments);
373 nilfs_segments_blocks_per_segment_show(struct nilfs_segments_attr *attr,
374 struct the_nilfs *nilfs,
377 return snprintf(buf, PAGE_SIZE, "%lu\n", nilfs->ns_blocks_per_segment);
381 nilfs_segments_clean_segments_show(struct nilfs_segments_attr *attr,
382 struct the_nilfs *nilfs,
385 unsigned long ncleansegs;
387 down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
388 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
389 up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
391 return snprintf(buf, PAGE_SIZE, "%lu\n", ncleansegs);
395 nilfs_segments_dirty_segments_show(struct nilfs_segments_attr *attr,
396 struct the_nilfs *nilfs,
399 struct nilfs_sustat sustat;
402 down_read(&nilfs->ns_segctor_sem);
403 err = nilfs_sufile_get_stat(nilfs->ns_sufile, &sustat);
404 up_read(&nilfs->ns_segctor_sem);
406 nilfs_err(nilfs->ns_sb, "unable to get segment stat: err=%d",
411 return snprintf(buf, PAGE_SIZE, "%llu\n", sustat.ss_ndirtysegs);
414 static const char segments_readme_str[] =
415 "The segments group contains attributes that describe\n"
416 "details about volume's segments.\n\n"
417 "(1) segments_number\n\tshow number of segments on volume.\n\n"
418 "(2) blocks_per_segment\n\tshow number of blocks in segment.\n\n"
419 "(3) clean_segments\n\tshow count of clean segments.\n\n"
420 "(4) dirty_segments\n\tshow count of dirty segments.\n\n";
423 nilfs_segments_README_show(struct nilfs_segments_attr *attr,
424 struct the_nilfs *nilfs,
427 return snprintf(buf, PAGE_SIZE, segments_readme_str);
430 NILFS_SEGMENTS_RO_ATTR(segments_number);
431 NILFS_SEGMENTS_RO_ATTR(blocks_per_segment);
432 NILFS_SEGMENTS_RO_ATTR(clean_segments);
433 NILFS_SEGMENTS_RO_ATTR(dirty_segments);
434 NILFS_SEGMENTS_RO_ATTR(README);
436 static struct attribute *nilfs_segments_attrs[] = {
437 NILFS_SEGMENTS_ATTR_LIST(segments_number),
438 NILFS_SEGMENTS_ATTR_LIST(blocks_per_segment),
439 NILFS_SEGMENTS_ATTR_LIST(clean_segments),
440 NILFS_SEGMENTS_ATTR_LIST(dirty_segments),
441 NILFS_SEGMENTS_ATTR_LIST(README),
445 NILFS_DEV_INT_GROUP_OPS(segments, dev);
446 NILFS_DEV_INT_GROUP_TYPE(segments, dev);
447 NILFS_DEV_INT_GROUP_FNS(segments, dev);
449 /************************************************************************
450 * NILFS segctor attrs *
451 ************************************************************************/
454 nilfs_segctor_last_pseg_block_show(struct nilfs_segctor_attr *attr,
455 struct the_nilfs *nilfs,
460 spin_lock(&nilfs->ns_last_segment_lock);
461 last_pseg = nilfs->ns_last_pseg;
462 spin_unlock(&nilfs->ns_last_segment_lock);
464 return snprintf(buf, PAGE_SIZE, "%llu\n",
465 (unsigned long long)last_pseg);
469 nilfs_segctor_last_seg_sequence_show(struct nilfs_segctor_attr *attr,
470 struct the_nilfs *nilfs,
475 spin_lock(&nilfs->ns_last_segment_lock);
476 last_seq = nilfs->ns_last_seq;
477 spin_unlock(&nilfs->ns_last_segment_lock);
479 return snprintf(buf, PAGE_SIZE, "%llu\n", last_seq);
483 nilfs_segctor_last_seg_checkpoint_show(struct nilfs_segctor_attr *attr,
484 struct the_nilfs *nilfs,
489 spin_lock(&nilfs->ns_last_segment_lock);
490 last_cno = nilfs->ns_last_cno;
491 spin_unlock(&nilfs->ns_last_segment_lock);
493 return snprintf(buf, PAGE_SIZE, "%llu\n", last_cno);
497 nilfs_segctor_current_seg_sequence_show(struct nilfs_segctor_attr *attr,
498 struct the_nilfs *nilfs,
503 down_read(&nilfs->ns_segctor_sem);
504 seg_seq = nilfs->ns_seg_seq;
505 up_read(&nilfs->ns_segctor_sem);
507 return snprintf(buf, PAGE_SIZE, "%llu\n", seg_seq);
511 nilfs_segctor_current_last_full_seg_show(struct nilfs_segctor_attr *attr,
512 struct the_nilfs *nilfs,
517 down_read(&nilfs->ns_segctor_sem);
518 segnum = nilfs->ns_segnum;
519 up_read(&nilfs->ns_segctor_sem);
521 return snprintf(buf, PAGE_SIZE, "%llu\n", segnum);
525 nilfs_segctor_next_full_seg_show(struct nilfs_segctor_attr *attr,
526 struct the_nilfs *nilfs,
531 down_read(&nilfs->ns_segctor_sem);
532 nextnum = nilfs->ns_nextnum;
533 up_read(&nilfs->ns_segctor_sem);
535 return snprintf(buf, PAGE_SIZE, "%llu\n", nextnum);
539 nilfs_segctor_next_pseg_offset_show(struct nilfs_segctor_attr *attr,
540 struct the_nilfs *nilfs,
543 unsigned long pseg_offset;
545 down_read(&nilfs->ns_segctor_sem);
546 pseg_offset = nilfs->ns_pseg_offset;
547 up_read(&nilfs->ns_segctor_sem);
549 return snprintf(buf, PAGE_SIZE, "%lu\n", pseg_offset);
553 nilfs_segctor_next_checkpoint_show(struct nilfs_segctor_attr *attr,
554 struct the_nilfs *nilfs,
559 down_read(&nilfs->ns_segctor_sem);
561 up_read(&nilfs->ns_segctor_sem);
563 return snprintf(buf, PAGE_SIZE, "%llu\n", cno);
567 nilfs_segctor_last_seg_write_time_show(struct nilfs_segctor_attr *attr,
568 struct the_nilfs *nilfs,
573 down_read(&nilfs->ns_segctor_sem);
574 ctime = nilfs->ns_ctime;
575 up_read(&nilfs->ns_segctor_sem);
577 return NILFS_SHOW_TIME(ctime, buf);
581 nilfs_segctor_last_seg_write_time_secs_show(struct nilfs_segctor_attr *attr,
582 struct the_nilfs *nilfs,
587 down_read(&nilfs->ns_segctor_sem);
588 ctime = nilfs->ns_ctime;
589 up_read(&nilfs->ns_segctor_sem);
591 return snprintf(buf, PAGE_SIZE, "%llu\n", ctime);
595 nilfs_segctor_last_nongc_write_time_show(struct nilfs_segctor_attr *attr,
596 struct the_nilfs *nilfs,
599 time64_t nongc_ctime;
601 down_read(&nilfs->ns_segctor_sem);
602 nongc_ctime = nilfs->ns_nongc_ctime;
603 up_read(&nilfs->ns_segctor_sem);
605 return NILFS_SHOW_TIME(nongc_ctime, buf);
609 nilfs_segctor_last_nongc_write_time_secs_show(struct nilfs_segctor_attr *attr,
610 struct the_nilfs *nilfs,
613 time64_t nongc_ctime;
615 down_read(&nilfs->ns_segctor_sem);
616 nongc_ctime = nilfs->ns_nongc_ctime;
617 up_read(&nilfs->ns_segctor_sem);
619 return snprintf(buf, PAGE_SIZE, "%llu\n", nongc_ctime);
623 nilfs_segctor_dirty_data_blocks_count_show(struct nilfs_segctor_attr *attr,
624 struct the_nilfs *nilfs,
629 down_read(&nilfs->ns_segctor_sem);
630 ndirtyblks = atomic_read(&nilfs->ns_ndirtyblks);
631 up_read(&nilfs->ns_segctor_sem);
633 return snprintf(buf, PAGE_SIZE, "%u\n", ndirtyblks);
636 static const char segctor_readme_str[] =
637 "The segctor group contains attributes that describe\n"
638 "segctor thread activity details.\n\n"
639 "(1) last_pseg_block\n"
640 "\tshow start block number of the latest segment.\n\n"
641 "(2) last_seg_sequence\n"
642 "\tshow sequence value of the latest segment.\n\n"
643 "(3) last_seg_checkpoint\n"
644 "\tshow checkpoint number of the latest segment.\n\n"
645 "(4) current_seg_sequence\n\tshow segment sequence counter.\n\n"
646 "(5) current_last_full_seg\n"
647 "\tshow index number of the latest full segment.\n\n"
648 "(6) next_full_seg\n"
649 "\tshow index number of the full segment index to be used next.\n\n"
650 "(7) next_pseg_offset\n"
651 "\tshow offset of next partial segment in the current full segment.\n\n"
652 "(8) next_checkpoint\n\tshow next checkpoint number.\n\n"
653 "(9) last_seg_write_time\n"
654 "\tshow write time of the last segment in human-readable format.\n\n"
655 "(10) last_seg_write_time_secs\n"
656 "\tshow write time of the last segment in seconds.\n\n"
657 "(11) last_nongc_write_time\n"
658 "\tshow write time of the last segment not for cleaner operation "
659 "in human-readable format.\n\n"
660 "(12) last_nongc_write_time_secs\n"
661 "\tshow write time of the last segment not for cleaner operation "
663 "(13) dirty_data_blocks_count\n"
664 "\tshow number of dirty data blocks.\n\n";
667 nilfs_segctor_README_show(struct nilfs_segctor_attr *attr,
668 struct the_nilfs *nilfs, char *buf)
670 return snprintf(buf, PAGE_SIZE, segctor_readme_str);
673 NILFS_SEGCTOR_RO_ATTR(last_pseg_block);
674 NILFS_SEGCTOR_RO_ATTR(last_seg_sequence);
675 NILFS_SEGCTOR_RO_ATTR(last_seg_checkpoint);
676 NILFS_SEGCTOR_RO_ATTR(current_seg_sequence);
677 NILFS_SEGCTOR_RO_ATTR(current_last_full_seg);
678 NILFS_SEGCTOR_RO_ATTR(next_full_seg);
679 NILFS_SEGCTOR_RO_ATTR(next_pseg_offset);
680 NILFS_SEGCTOR_RO_ATTR(next_checkpoint);
681 NILFS_SEGCTOR_RO_ATTR(last_seg_write_time);
682 NILFS_SEGCTOR_RO_ATTR(last_seg_write_time_secs);
683 NILFS_SEGCTOR_RO_ATTR(last_nongc_write_time);
684 NILFS_SEGCTOR_RO_ATTR(last_nongc_write_time_secs);
685 NILFS_SEGCTOR_RO_ATTR(dirty_data_blocks_count);
686 NILFS_SEGCTOR_RO_ATTR(README);
688 static struct attribute *nilfs_segctor_attrs[] = {
689 NILFS_SEGCTOR_ATTR_LIST(last_pseg_block),
690 NILFS_SEGCTOR_ATTR_LIST(last_seg_sequence),
691 NILFS_SEGCTOR_ATTR_LIST(last_seg_checkpoint),
692 NILFS_SEGCTOR_ATTR_LIST(current_seg_sequence),
693 NILFS_SEGCTOR_ATTR_LIST(current_last_full_seg),
694 NILFS_SEGCTOR_ATTR_LIST(next_full_seg),
695 NILFS_SEGCTOR_ATTR_LIST(next_pseg_offset),
696 NILFS_SEGCTOR_ATTR_LIST(next_checkpoint),
697 NILFS_SEGCTOR_ATTR_LIST(last_seg_write_time),
698 NILFS_SEGCTOR_ATTR_LIST(last_seg_write_time_secs),
699 NILFS_SEGCTOR_ATTR_LIST(last_nongc_write_time),
700 NILFS_SEGCTOR_ATTR_LIST(last_nongc_write_time_secs),
701 NILFS_SEGCTOR_ATTR_LIST(dirty_data_blocks_count),
702 NILFS_SEGCTOR_ATTR_LIST(README),
706 NILFS_DEV_INT_GROUP_OPS(segctor, dev);
707 NILFS_DEV_INT_GROUP_TYPE(segctor, dev);
708 NILFS_DEV_INT_GROUP_FNS(segctor, dev);
710 /************************************************************************
711 * NILFS superblock attrs *
712 ************************************************************************/
715 nilfs_superblock_sb_write_time_show(struct nilfs_superblock_attr *attr,
716 struct the_nilfs *nilfs,
721 down_read(&nilfs->ns_sem);
722 sbwtime = nilfs->ns_sbwtime;
723 up_read(&nilfs->ns_sem);
725 return NILFS_SHOW_TIME(sbwtime, buf);
729 nilfs_superblock_sb_write_time_secs_show(struct nilfs_superblock_attr *attr,
730 struct the_nilfs *nilfs,
735 down_read(&nilfs->ns_sem);
736 sbwtime = nilfs->ns_sbwtime;
737 up_read(&nilfs->ns_sem);
739 return snprintf(buf, PAGE_SIZE, "%llu\n", sbwtime);
743 nilfs_superblock_sb_write_count_show(struct nilfs_superblock_attr *attr,
744 struct the_nilfs *nilfs,
747 unsigned int sbwcount;
749 down_read(&nilfs->ns_sem);
750 sbwcount = nilfs->ns_sbwcount;
751 up_read(&nilfs->ns_sem);
753 return snprintf(buf, PAGE_SIZE, "%u\n", sbwcount);
757 nilfs_superblock_sb_update_frequency_show(struct nilfs_superblock_attr *attr,
758 struct the_nilfs *nilfs,
761 unsigned int sb_update_freq;
763 down_read(&nilfs->ns_sem);
764 sb_update_freq = nilfs->ns_sb_update_freq;
765 up_read(&nilfs->ns_sem);
767 return snprintf(buf, PAGE_SIZE, "%u\n", sb_update_freq);
771 nilfs_superblock_sb_update_frequency_store(struct nilfs_superblock_attr *attr,
772 struct the_nilfs *nilfs,
773 const char *buf, size_t count)
778 err = kstrtouint(skip_spaces(buf), 0, &val);
780 nilfs_err(nilfs->ns_sb, "unable to convert string: err=%d",
785 if (val < NILFS_SB_FREQ) {
787 nilfs_warn(nilfs->ns_sb,
788 "superblock update frequency cannot be lesser than 10 seconds");
791 down_write(&nilfs->ns_sem);
792 nilfs->ns_sb_update_freq = val;
793 up_write(&nilfs->ns_sem);
798 static const char sb_readme_str[] =
799 "The superblock group contains attributes that describe\n"
800 "superblock's details.\n\n"
801 "(1) sb_write_time\n\tshow previous write time of super block "
802 "in human-readable format.\n\n"
803 "(2) sb_write_time_secs\n\tshow previous write time of super block "
805 "(3) sb_write_count\n\tshow write count of super block.\n\n"
806 "(4) sb_update_frequency\n"
807 "\tshow/set interval of periodical update of superblock (in seconds).\n\n"
808 "\tYou can set preferable frequency of superblock update by command:\n\n"
809 "\t'echo <val> > /sys/fs/<nilfs>/<dev>/superblock/sb_update_frequency'\n";
812 nilfs_superblock_README_show(struct nilfs_superblock_attr *attr,
813 struct the_nilfs *nilfs, char *buf)
815 return snprintf(buf, PAGE_SIZE, sb_readme_str);
818 NILFS_SUPERBLOCK_RO_ATTR(sb_write_time);
819 NILFS_SUPERBLOCK_RO_ATTR(sb_write_time_secs);
820 NILFS_SUPERBLOCK_RO_ATTR(sb_write_count);
821 NILFS_SUPERBLOCK_RW_ATTR(sb_update_frequency);
822 NILFS_SUPERBLOCK_RO_ATTR(README);
824 static struct attribute *nilfs_superblock_attrs[] = {
825 NILFS_SUPERBLOCK_ATTR_LIST(sb_write_time),
826 NILFS_SUPERBLOCK_ATTR_LIST(sb_write_time_secs),
827 NILFS_SUPERBLOCK_ATTR_LIST(sb_write_count),
828 NILFS_SUPERBLOCK_ATTR_LIST(sb_update_frequency),
829 NILFS_SUPERBLOCK_ATTR_LIST(README),
833 NILFS_DEV_INT_GROUP_OPS(superblock, dev);
834 NILFS_DEV_INT_GROUP_TYPE(superblock, dev);
835 NILFS_DEV_INT_GROUP_FNS(superblock, dev);
837 /************************************************************************
838 * NILFS device attrs *
839 ************************************************************************/
842 ssize_t nilfs_dev_revision_show(struct nilfs_dev_attr *attr,
843 struct the_nilfs *nilfs,
846 struct nilfs_super_block **sbp = nilfs->ns_sbp;
847 u32 major = le32_to_cpu(sbp[0]->s_rev_level);
848 u16 minor = le16_to_cpu(sbp[0]->s_minor_rev_level);
850 return snprintf(buf, PAGE_SIZE, "%d.%d\n", major, minor);
854 ssize_t nilfs_dev_blocksize_show(struct nilfs_dev_attr *attr,
855 struct the_nilfs *nilfs,
858 return snprintf(buf, PAGE_SIZE, "%u\n", nilfs->ns_blocksize);
862 ssize_t nilfs_dev_device_size_show(struct nilfs_dev_attr *attr,
863 struct the_nilfs *nilfs,
866 struct nilfs_super_block **sbp = nilfs->ns_sbp;
867 u64 dev_size = le64_to_cpu(sbp[0]->s_dev_size);
869 return snprintf(buf, PAGE_SIZE, "%llu\n", dev_size);
873 ssize_t nilfs_dev_free_blocks_show(struct nilfs_dev_attr *attr,
874 struct the_nilfs *nilfs,
877 sector_t free_blocks = 0;
879 nilfs_count_free_blocks(nilfs, &free_blocks);
880 return snprintf(buf, PAGE_SIZE, "%llu\n",
881 (unsigned long long)free_blocks);
885 ssize_t nilfs_dev_uuid_show(struct nilfs_dev_attr *attr,
886 struct the_nilfs *nilfs,
889 struct nilfs_super_block **sbp = nilfs->ns_sbp;
891 return snprintf(buf, PAGE_SIZE, "%pUb\n", sbp[0]->s_uuid);
895 ssize_t nilfs_dev_volume_name_show(struct nilfs_dev_attr *attr,
896 struct the_nilfs *nilfs,
899 struct nilfs_super_block **sbp = nilfs->ns_sbp;
901 return scnprintf(buf, sizeof(sbp[0]->s_volume_name), "%s\n",
902 sbp[0]->s_volume_name);
905 static const char dev_readme_str[] =
906 "The <device> group contains attributes that describe file system\n"
907 "partition's details.\n\n"
908 "(1) revision\n\tshow NILFS file system revision.\n\n"
909 "(2) blocksize\n\tshow volume block size in bytes.\n\n"
910 "(3) device_size\n\tshow volume size in bytes.\n\n"
911 "(4) free_blocks\n\tshow count of free blocks on volume.\n\n"
912 "(5) uuid\n\tshow volume's UUID.\n\n"
913 "(6) volume_name\n\tshow volume's name.\n\n";
915 static ssize_t nilfs_dev_README_show(struct nilfs_dev_attr *attr,
916 struct the_nilfs *nilfs,
919 return snprintf(buf, PAGE_SIZE, dev_readme_str);
922 NILFS_DEV_RO_ATTR(revision);
923 NILFS_DEV_RO_ATTR(blocksize);
924 NILFS_DEV_RO_ATTR(device_size);
925 NILFS_DEV_RO_ATTR(free_blocks);
926 NILFS_DEV_RO_ATTR(uuid);
927 NILFS_DEV_RO_ATTR(volume_name);
928 NILFS_DEV_RO_ATTR(README);
930 static struct attribute *nilfs_dev_attrs[] = {
931 NILFS_DEV_ATTR_LIST(revision),
932 NILFS_DEV_ATTR_LIST(blocksize),
933 NILFS_DEV_ATTR_LIST(device_size),
934 NILFS_DEV_ATTR_LIST(free_blocks),
935 NILFS_DEV_ATTR_LIST(uuid),
936 NILFS_DEV_ATTR_LIST(volume_name),
937 NILFS_DEV_ATTR_LIST(README),
941 static ssize_t nilfs_dev_attr_show(struct kobject *kobj,
942 struct attribute *attr, char *buf)
944 struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
946 struct nilfs_dev_attr *a = container_of(attr, struct nilfs_dev_attr,
949 return a->show ? a->show(a, nilfs, buf) : 0;
952 static ssize_t nilfs_dev_attr_store(struct kobject *kobj,
953 struct attribute *attr,
954 const char *buf, size_t len)
956 struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
958 struct nilfs_dev_attr *a = container_of(attr, struct nilfs_dev_attr,
961 return a->store ? a->store(a, nilfs, buf, len) : 0;
964 static void nilfs_dev_attr_release(struct kobject *kobj)
966 struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
968 complete(&nilfs->ns_dev_kobj_unregister);
971 static const struct sysfs_ops nilfs_dev_attr_ops = {
972 .show = nilfs_dev_attr_show,
973 .store = nilfs_dev_attr_store,
976 static struct kobj_type nilfs_dev_ktype = {
977 .default_attrs = nilfs_dev_attrs,
978 .sysfs_ops = &nilfs_dev_attr_ops,
979 .release = nilfs_dev_attr_release,
982 int nilfs_sysfs_create_device_group(struct super_block *sb)
984 struct the_nilfs *nilfs = sb->s_fs_info;
985 size_t devgrp_size = sizeof(struct nilfs_sysfs_dev_subgroups);
988 nilfs->ns_dev_subgroups = kzalloc(devgrp_size, GFP_KERNEL);
989 if (unlikely(!nilfs->ns_dev_subgroups)) {
991 nilfs_err(sb, "unable to allocate memory for device group");
992 goto failed_create_device_group;
995 nilfs->ns_dev_kobj.kset = nilfs_kset;
996 init_completion(&nilfs->ns_dev_kobj_unregister);
997 err = kobject_init_and_add(&nilfs->ns_dev_kobj, &nilfs_dev_ktype, NULL,
1000 goto cleanup_dev_kobject;
1002 err = nilfs_sysfs_create_mounted_snapshots_group(nilfs);
1004 goto cleanup_dev_kobject;
1006 err = nilfs_sysfs_create_checkpoints_group(nilfs);
1008 goto delete_mounted_snapshots_group;
1010 err = nilfs_sysfs_create_segments_group(nilfs);
1012 goto delete_checkpoints_group;
1014 err = nilfs_sysfs_create_superblock_group(nilfs);
1016 goto delete_segments_group;
1018 err = nilfs_sysfs_create_segctor_group(nilfs);
1020 goto delete_superblock_group;
1024 delete_superblock_group:
1025 nilfs_sysfs_delete_superblock_group(nilfs);
1027 delete_segments_group:
1028 nilfs_sysfs_delete_segments_group(nilfs);
1030 delete_checkpoints_group:
1031 nilfs_sysfs_delete_checkpoints_group(nilfs);
1033 delete_mounted_snapshots_group:
1034 nilfs_sysfs_delete_mounted_snapshots_group(nilfs);
1036 cleanup_dev_kobject:
1037 kobject_put(&nilfs->ns_dev_kobj);
1038 kfree(nilfs->ns_dev_subgroups);
1040 failed_create_device_group:
1044 void nilfs_sysfs_delete_device_group(struct the_nilfs *nilfs)
1046 nilfs_sysfs_delete_mounted_snapshots_group(nilfs);
1047 nilfs_sysfs_delete_checkpoints_group(nilfs);
1048 nilfs_sysfs_delete_segments_group(nilfs);
1049 nilfs_sysfs_delete_superblock_group(nilfs);
1050 nilfs_sysfs_delete_segctor_group(nilfs);
1051 kobject_del(&nilfs->ns_dev_kobj);
1052 kobject_put(&nilfs->ns_dev_kobj);
1053 kfree(nilfs->ns_dev_subgroups);
1056 /************************************************************************
1057 * NILFS feature attrs *
1058 ************************************************************************/
1060 static ssize_t nilfs_feature_revision_show(struct kobject *kobj,
1061 struct attribute *attr, char *buf)
1063 return snprintf(buf, PAGE_SIZE, "%d.%d\n",
1064 NILFS_CURRENT_REV, NILFS_MINOR_REV);
1067 static const char features_readme_str[] =
1068 "The features group contains attributes that describe NILFS file\n"
1069 "system driver features.\n\n"
1070 "(1) revision\n\tshow current revision of NILFS file system driver.\n";
1072 static ssize_t nilfs_feature_README_show(struct kobject *kobj,
1073 struct attribute *attr,
1076 return snprintf(buf, PAGE_SIZE, features_readme_str);
1079 NILFS_FEATURE_RO_ATTR(revision);
1080 NILFS_FEATURE_RO_ATTR(README);
1082 static struct attribute *nilfs_feature_attrs[] = {
1083 NILFS_FEATURE_ATTR_LIST(revision),
1084 NILFS_FEATURE_ATTR_LIST(README),
1088 static const struct attribute_group nilfs_feature_attr_group = {
1090 .attrs = nilfs_feature_attrs,
1093 int __init nilfs_sysfs_init(void)
1097 nilfs_kset = kset_create_and_add(NILFS_ROOT_GROUP_NAME, NULL, fs_kobj);
1100 nilfs_err(NULL, "unable to create sysfs entry: err=%d", err);
1101 goto failed_sysfs_init;
1104 err = sysfs_create_group(&nilfs_kset->kobj, &nilfs_feature_attr_group);
1105 if (unlikely(err)) {
1106 nilfs_err(NULL, "unable to create feature group: err=%d", err);
1107 goto cleanup_sysfs_init;
1113 kset_unregister(nilfs_kset);
1119 void nilfs_sysfs_exit(void)
1121 sysfs_remove_group(&nilfs_kset->kobj, &nilfs_feature_attr_group);
1122 kset_unregister(nilfs_kset);