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 = container_of(kobj, \
55 struct nilfs_sysfs_##parent_name##_subgroups, \
57 complete(&subgroups->sg_##name##_kobj_unregister); \
59 static struct kobj_type nilfs_##name##_ktype = { \
60 .default_attrs = nilfs_##name##_attrs, \
61 .sysfs_ops = &nilfs_##name##_attr_ops, \
62 .release = nilfs_##name##_attr_release, \
65 #define NILFS_DEV_INT_GROUP_FNS(name, parent_name) \
66 static int nilfs_sysfs_create_##name##_group(struct the_nilfs *nilfs) \
68 struct kobject *parent; \
69 struct kobject *kobj; \
70 struct completion *kobj_unregister; \
71 struct nilfs_sysfs_##parent_name##_subgroups *subgroups; \
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, \
85 static void nilfs_sysfs_delete_##name##_group(struct the_nilfs *nilfs) \
87 kobject_put(&nilfs->ns_##parent_name##_subgroups->sg_##name##_kobj); \
90 /************************************************************************
91 * NILFS snapshot attrs *
92 ************************************************************************/
95 nilfs_snapshot_inodes_count_show(struct nilfs_snapshot_attr *attr,
96 struct nilfs_root *root, char *buf)
98 return snprintf(buf, PAGE_SIZE, "%llu\n",
99 (unsigned long long)atomic64_read(&root->inodes_count));
103 nilfs_snapshot_blocks_count_show(struct nilfs_snapshot_attr *attr,
104 struct nilfs_root *root, char *buf)
106 return snprintf(buf, PAGE_SIZE, "%llu\n",
107 (unsigned long long)atomic64_read(&root->blocks_count));
110 static 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";
116 nilfs_snapshot_README_show(struct nilfs_snapshot_attr *attr,
117 struct nilfs_root *root, char *buf)
119 return snprintf(buf, PAGE_SIZE, snapshot_readme_str);
122 NILFS_SNAPSHOT_RO_ATTR(inodes_count);
123 NILFS_SNAPSHOT_RO_ATTR(blocks_count);
124 NILFS_SNAPSHOT_RO_ATTR(README);
126 static 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),
133 static ssize_t nilfs_snapshot_attr_show(struct kobject *kobj,
134 struct attribute *attr, char *buf)
136 struct nilfs_root *root =
137 container_of(kobj, struct nilfs_root, snapshot_kobj);
138 struct nilfs_snapshot_attr *a =
139 container_of(attr, struct nilfs_snapshot_attr, attr);
141 return a->show ? a->show(a, root, buf) : 0;
144 static ssize_t nilfs_snapshot_attr_store(struct kobject *kobj,
145 struct attribute *attr,
146 const char *buf, size_t len)
148 struct nilfs_root *root =
149 container_of(kobj, struct nilfs_root, snapshot_kobj);
150 struct nilfs_snapshot_attr *a =
151 container_of(attr, struct nilfs_snapshot_attr, attr);
153 return a->store ? a->store(a, root, buf, len) : 0;
156 static void nilfs_snapshot_attr_release(struct kobject *kobj)
158 struct nilfs_root *root = container_of(kobj, struct nilfs_root,
160 complete(&root->snapshot_kobj_unregister);
163 static const struct sysfs_ops nilfs_snapshot_attr_ops = {
164 .show = nilfs_snapshot_attr_show,
165 .store = nilfs_snapshot_attr_store,
168 static struct kobj_type nilfs_snapshot_ktype = {
169 .default_attrs = nilfs_snapshot_attrs,
170 .sysfs_ops = &nilfs_snapshot_attr_ops,
171 .release = nilfs_snapshot_attr_release,
174 int nilfs_sysfs_create_snapshot_group(struct nilfs_root *root)
176 struct the_nilfs *nilfs;
177 struct kobject *parent;
181 parent = &nilfs->ns_dev_subgroups->sg_mounted_snapshots_kobj;
182 root->snapshot_kobj.kset = nilfs_kset;
183 init_completion(&root->snapshot_kobj_unregister);
185 if (root->cno == NILFS_CPTREE_CURRENT_CNO) {
186 err = kobject_init_and_add(&root->snapshot_kobj,
187 &nilfs_snapshot_ktype,
189 "current_checkpoint");
191 err = kobject_init_and_add(&root->snapshot_kobj,
192 &nilfs_snapshot_ktype,
198 kobject_put(&root->snapshot_kobj);
203 void nilfs_sysfs_delete_snapshot_group(struct nilfs_root *root)
205 kobject_put(&root->snapshot_kobj);
208 /************************************************************************
209 * NILFS mounted snapshots attrs *
210 ************************************************************************/
212 static const char mounted_snapshots_readme_str[] =
213 "The mounted_snapshots group contains group for\n"
214 "every mounted snapshot.\n";
217 nilfs_mounted_snapshots_README_show(struct nilfs_mounted_snapshots_attr *attr,
218 struct the_nilfs *nilfs, char *buf)
220 return snprintf(buf, PAGE_SIZE, mounted_snapshots_readme_str);
223 NILFS_MOUNTED_SNAPSHOTS_RO_ATTR(README);
225 static struct attribute *nilfs_mounted_snapshots_attrs[] = {
226 NILFS_MOUNTED_SNAPSHOTS_ATTR_LIST(README),
230 NILFS_DEV_INT_GROUP_OPS(mounted_snapshots, dev);
231 NILFS_DEV_INT_GROUP_TYPE(mounted_snapshots, dev);
232 NILFS_DEV_INT_GROUP_FNS(mounted_snapshots, dev);
234 /************************************************************************
235 * NILFS checkpoints attrs *
236 ************************************************************************/
239 nilfs_checkpoints_checkpoints_number_show(struct nilfs_checkpoints_attr *attr,
240 struct the_nilfs *nilfs,
244 struct nilfs_cpstat cpstat;
247 down_read(&nilfs->ns_segctor_sem);
248 err = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
249 up_read(&nilfs->ns_segctor_sem);
251 nilfs_err(nilfs->ns_sb, "unable to get checkpoint stat: err=%d",
256 ncheckpoints = cpstat.cs_ncps;
258 return snprintf(buf, PAGE_SIZE, "%llu\n", ncheckpoints);
262 nilfs_checkpoints_snapshots_number_show(struct nilfs_checkpoints_attr *attr,
263 struct the_nilfs *nilfs,
267 struct nilfs_cpstat cpstat;
270 down_read(&nilfs->ns_segctor_sem);
271 err = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
272 up_read(&nilfs->ns_segctor_sem);
274 nilfs_err(nilfs->ns_sb, "unable to get checkpoint stat: err=%d",
279 nsnapshots = cpstat.cs_nsss;
281 return snprintf(buf, PAGE_SIZE, "%llu\n", nsnapshots);
285 nilfs_checkpoints_last_seg_checkpoint_show(struct nilfs_checkpoints_attr *attr,
286 struct the_nilfs *nilfs,
291 spin_lock(&nilfs->ns_last_segment_lock);
292 last_cno = nilfs->ns_last_cno;
293 spin_unlock(&nilfs->ns_last_segment_lock);
295 return snprintf(buf, PAGE_SIZE, "%llu\n", last_cno);
299 nilfs_checkpoints_next_checkpoint_show(struct nilfs_checkpoints_attr *attr,
300 struct the_nilfs *nilfs,
305 down_read(&nilfs->ns_segctor_sem);
307 up_read(&nilfs->ns_segctor_sem);
309 return snprintf(buf, PAGE_SIZE, "%llu\n", cno);
312 static const char checkpoints_readme_str[] =
313 "The checkpoints group contains attributes that describe\n"
314 "details about volume's checkpoints.\n\n"
315 "(1) checkpoints_number\n\tshow number of checkpoints on volume.\n\n"
316 "(2) snapshots_number\n\tshow number of snapshots on volume.\n\n"
317 "(3) last_seg_checkpoint\n"
318 "\tshow checkpoint number of the latest segment.\n\n"
319 "(4) next_checkpoint\n\tshow next checkpoint number.\n\n";
322 nilfs_checkpoints_README_show(struct nilfs_checkpoints_attr *attr,
323 struct the_nilfs *nilfs, char *buf)
325 return snprintf(buf, PAGE_SIZE, checkpoints_readme_str);
328 NILFS_CHECKPOINTS_RO_ATTR(checkpoints_number);
329 NILFS_CHECKPOINTS_RO_ATTR(snapshots_number);
330 NILFS_CHECKPOINTS_RO_ATTR(last_seg_checkpoint);
331 NILFS_CHECKPOINTS_RO_ATTR(next_checkpoint);
332 NILFS_CHECKPOINTS_RO_ATTR(README);
334 static struct attribute *nilfs_checkpoints_attrs[] = {
335 NILFS_CHECKPOINTS_ATTR_LIST(checkpoints_number),
336 NILFS_CHECKPOINTS_ATTR_LIST(snapshots_number),
337 NILFS_CHECKPOINTS_ATTR_LIST(last_seg_checkpoint),
338 NILFS_CHECKPOINTS_ATTR_LIST(next_checkpoint),
339 NILFS_CHECKPOINTS_ATTR_LIST(README),
343 NILFS_DEV_INT_GROUP_OPS(checkpoints, dev);
344 NILFS_DEV_INT_GROUP_TYPE(checkpoints, dev);
345 NILFS_DEV_INT_GROUP_FNS(checkpoints, dev);
347 /************************************************************************
348 * NILFS segments attrs *
349 ************************************************************************/
352 nilfs_segments_segments_number_show(struct nilfs_segments_attr *attr,
353 struct the_nilfs *nilfs,
356 return snprintf(buf, PAGE_SIZE, "%lu\n", nilfs->ns_nsegments);
360 nilfs_segments_blocks_per_segment_show(struct nilfs_segments_attr *attr,
361 struct the_nilfs *nilfs,
364 return snprintf(buf, PAGE_SIZE, "%lu\n", nilfs->ns_blocks_per_segment);
368 nilfs_segments_clean_segments_show(struct nilfs_segments_attr *attr,
369 struct the_nilfs *nilfs,
372 unsigned long ncleansegs;
374 down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
375 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
376 up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
378 return snprintf(buf, PAGE_SIZE, "%lu\n", ncleansegs);
382 nilfs_segments_dirty_segments_show(struct nilfs_segments_attr *attr,
383 struct the_nilfs *nilfs,
386 struct nilfs_sustat sustat;
389 down_read(&nilfs->ns_segctor_sem);
390 err = nilfs_sufile_get_stat(nilfs->ns_sufile, &sustat);
391 up_read(&nilfs->ns_segctor_sem);
393 nilfs_err(nilfs->ns_sb, "unable to get segment stat: err=%d",
398 return snprintf(buf, PAGE_SIZE, "%llu\n", sustat.ss_ndirtysegs);
401 static const char segments_readme_str[] =
402 "The segments group contains attributes that describe\n"
403 "details about volume's segments.\n\n"
404 "(1) segments_number\n\tshow number of segments on volume.\n\n"
405 "(2) blocks_per_segment\n\tshow number of blocks in segment.\n\n"
406 "(3) clean_segments\n\tshow count of clean segments.\n\n"
407 "(4) dirty_segments\n\tshow count of dirty segments.\n\n";
410 nilfs_segments_README_show(struct nilfs_segments_attr *attr,
411 struct the_nilfs *nilfs,
414 return snprintf(buf, PAGE_SIZE, segments_readme_str);
417 NILFS_SEGMENTS_RO_ATTR(segments_number);
418 NILFS_SEGMENTS_RO_ATTR(blocks_per_segment);
419 NILFS_SEGMENTS_RO_ATTR(clean_segments);
420 NILFS_SEGMENTS_RO_ATTR(dirty_segments);
421 NILFS_SEGMENTS_RO_ATTR(README);
423 static struct attribute *nilfs_segments_attrs[] = {
424 NILFS_SEGMENTS_ATTR_LIST(segments_number),
425 NILFS_SEGMENTS_ATTR_LIST(blocks_per_segment),
426 NILFS_SEGMENTS_ATTR_LIST(clean_segments),
427 NILFS_SEGMENTS_ATTR_LIST(dirty_segments),
428 NILFS_SEGMENTS_ATTR_LIST(README),
432 NILFS_DEV_INT_GROUP_OPS(segments, dev);
433 NILFS_DEV_INT_GROUP_TYPE(segments, dev);
434 NILFS_DEV_INT_GROUP_FNS(segments, dev);
436 /************************************************************************
437 * NILFS segctor attrs *
438 ************************************************************************/
441 nilfs_segctor_last_pseg_block_show(struct nilfs_segctor_attr *attr,
442 struct the_nilfs *nilfs,
447 spin_lock(&nilfs->ns_last_segment_lock);
448 last_pseg = nilfs->ns_last_pseg;
449 spin_unlock(&nilfs->ns_last_segment_lock);
451 return snprintf(buf, PAGE_SIZE, "%llu\n",
452 (unsigned long long)last_pseg);
456 nilfs_segctor_last_seg_sequence_show(struct nilfs_segctor_attr *attr,
457 struct the_nilfs *nilfs,
462 spin_lock(&nilfs->ns_last_segment_lock);
463 last_seq = nilfs->ns_last_seq;
464 spin_unlock(&nilfs->ns_last_segment_lock);
466 return snprintf(buf, PAGE_SIZE, "%llu\n", last_seq);
470 nilfs_segctor_last_seg_checkpoint_show(struct nilfs_segctor_attr *attr,
471 struct the_nilfs *nilfs,
476 spin_lock(&nilfs->ns_last_segment_lock);
477 last_cno = nilfs->ns_last_cno;
478 spin_unlock(&nilfs->ns_last_segment_lock);
480 return snprintf(buf, PAGE_SIZE, "%llu\n", last_cno);
484 nilfs_segctor_current_seg_sequence_show(struct nilfs_segctor_attr *attr,
485 struct the_nilfs *nilfs,
490 down_read(&nilfs->ns_segctor_sem);
491 seg_seq = nilfs->ns_seg_seq;
492 up_read(&nilfs->ns_segctor_sem);
494 return snprintf(buf, PAGE_SIZE, "%llu\n", seg_seq);
498 nilfs_segctor_current_last_full_seg_show(struct nilfs_segctor_attr *attr,
499 struct the_nilfs *nilfs,
504 down_read(&nilfs->ns_segctor_sem);
505 segnum = nilfs->ns_segnum;
506 up_read(&nilfs->ns_segctor_sem);
508 return snprintf(buf, PAGE_SIZE, "%llu\n", segnum);
512 nilfs_segctor_next_full_seg_show(struct nilfs_segctor_attr *attr,
513 struct the_nilfs *nilfs,
518 down_read(&nilfs->ns_segctor_sem);
519 nextnum = nilfs->ns_nextnum;
520 up_read(&nilfs->ns_segctor_sem);
522 return snprintf(buf, PAGE_SIZE, "%llu\n", nextnum);
526 nilfs_segctor_next_pseg_offset_show(struct nilfs_segctor_attr *attr,
527 struct the_nilfs *nilfs,
530 unsigned long pseg_offset;
532 down_read(&nilfs->ns_segctor_sem);
533 pseg_offset = nilfs->ns_pseg_offset;
534 up_read(&nilfs->ns_segctor_sem);
536 return snprintf(buf, PAGE_SIZE, "%lu\n", pseg_offset);
540 nilfs_segctor_next_checkpoint_show(struct nilfs_segctor_attr *attr,
541 struct the_nilfs *nilfs,
546 down_read(&nilfs->ns_segctor_sem);
548 up_read(&nilfs->ns_segctor_sem);
550 return snprintf(buf, PAGE_SIZE, "%llu\n", cno);
554 nilfs_segctor_last_seg_write_time_show(struct nilfs_segctor_attr *attr,
555 struct the_nilfs *nilfs,
560 down_read(&nilfs->ns_segctor_sem);
561 ctime = nilfs->ns_ctime;
562 up_read(&nilfs->ns_segctor_sem);
564 return sysfs_emit(buf, "%ptTs\n", &ctime);
568 nilfs_segctor_last_seg_write_time_secs_show(struct nilfs_segctor_attr *attr,
569 struct the_nilfs *nilfs,
574 down_read(&nilfs->ns_segctor_sem);
575 ctime = nilfs->ns_ctime;
576 up_read(&nilfs->ns_segctor_sem);
578 return snprintf(buf, PAGE_SIZE, "%llu\n", ctime);
582 nilfs_segctor_last_nongc_write_time_show(struct nilfs_segctor_attr *attr,
583 struct the_nilfs *nilfs,
586 time64_t nongc_ctime;
588 down_read(&nilfs->ns_segctor_sem);
589 nongc_ctime = nilfs->ns_nongc_ctime;
590 up_read(&nilfs->ns_segctor_sem);
592 return sysfs_emit(buf, "%ptTs\n", &nongc_ctime);
596 nilfs_segctor_last_nongc_write_time_secs_show(struct nilfs_segctor_attr *attr,
597 struct the_nilfs *nilfs,
600 time64_t nongc_ctime;
602 down_read(&nilfs->ns_segctor_sem);
603 nongc_ctime = nilfs->ns_nongc_ctime;
604 up_read(&nilfs->ns_segctor_sem);
606 return snprintf(buf, PAGE_SIZE, "%llu\n", nongc_ctime);
610 nilfs_segctor_dirty_data_blocks_count_show(struct nilfs_segctor_attr *attr,
611 struct the_nilfs *nilfs,
616 down_read(&nilfs->ns_segctor_sem);
617 ndirtyblks = atomic_read(&nilfs->ns_ndirtyblks);
618 up_read(&nilfs->ns_segctor_sem);
620 return snprintf(buf, PAGE_SIZE, "%u\n", ndirtyblks);
623 static const char segctor_readme_str[] =
624 "The segctor group contains attributes that describe\n"
625 "segctor thread activity details.\n\n"
626 "(1) last_pseg_block\n"
627 "\tshow start block number of the latest segment.\n\n"
628 "(2) last_seg_sequence\n"
629 "\tshow sequence value of the latest segment.\n\n"
630 "(3) last_seg_checkpoint\n"
631 "\tshow checkpoint number of the latest segment.\n\n"
632 "(4) current_seg_sequence\n\tshow segment sequence counter.\n\n"
633 "(5) current_last_full_seg\n"
634 "\tshow index number of the latest full segment.\n\n"
635 "(6) next_full_seg\n"
636 "\tshow index number of the full segment index to be used next.\n\n"
637 "(7) next_pseg_offset\n"
638 "\tshow offset of next partial segment in the current full segment.\n\n"
639 "(8) next_checkpoint\n\tshow next checkpoint number.\n\n"
640 "(9) last_seg_write_time\n"
641 "\tshow write time of the last segment in human-readable format.\n\n"
642 "(10) last_seg_write_time_secs\n"
643 "\tshow write time of the last segment in seconds.\n\n"
644 "(11) last_nongc_write_time\n"
645 "\tshow write time of the last segment not for cleaner operation "
646 "in human-readable format.\n\n"
647 "(12) last_nongc_write_time_secs\n"
648 "\tshow write time of the last segment not for cleaner operation "
650 "(13) dirty_data_blocks_count\n"
651 "\tshow number of dirty data blocks.\n\n";
654 nilfs_segctor_README_show(struct nilfs_segctor_attr *attr,
655 struct the_nilfs *nilfs, char *buf)
657 return snprintf(buf, PAGE_SIZE, segctor_readme_str);
660 NILFS_SEGCTOR_RO_ATTR(last_pseg_block);
661 NILFS_SEGCTOR_RO_ATTR(last_seg_sequence);
662 NILFS_SEGCTOR_RO_ATTR(last_seg_checkpoint);
663 NILFS_SEGCTOR_RO_ATTR(current_seg_sequence);
664 NILFS_SEGCTOR_RO_ATTR(current_last_full_seg);
665 NILFS_SEGCTOR_RO_ATTR(next_full_seg);
666 NILFS_SEGCTOR_RO_ATTR(next_pseg_offset);
667 NILFS_SEGCTOR_RO_ATTR(next_checkpoint);
668 NILFS_SEGCTOR_RO_ATTR(last_seg_write_time);
669 NILFS_SEGCTOR_RO_ATTR(last_seg_write_time_secs);
670 NILFS_SEGCTOR_RO_ATTR(last_nongc_write_time);
671 NILFS_SEGCTOR_RO_ATTR(last_nongc_write_time_secs);
672 NILFS_SEGCTOR_RO_ATTR(dirty_data_blocks_count);
673 NILFS_SEGCTOR_RO_ATTR(README);
675 static struct attribute *nilfs_segctor_attrs[] = {
676 NILFS_SEGCTOR_ATTR_LIST(last_pseg_block),
677 NILFS_SEGCTOR_ATTR_LIST(last_seg_sequence),
678 NILFS_SEGCTOR_ATTR_LIST(last_seg_checkpoint),
679 NILFS_SEGCTOR_ATTR_LIST(current_seg_sequence),
680 NILFS_SEGCTOR_ATTR_LIST(current_last_full_seg),
681 NILFS_SEGCTOR_ATTR_LIST(next_full_seg),
682 NILFS_SEGCTOR_ATTR_LIST(next_pseg_offset),
683 NILFS_SEGCTOR_ATTR_LIST(next_checkpoint),
684 NILFS_SEGCTOR_ATTR_LIST(last_seg_write_time),
685 NILFS_SEGCTOR_ATTR_LIST(last_seg_write_time_secs),
686 NILFS_SEGCTOR_ATTR_LIST(last_nongc_write_time),
687 NILFS_SEGCTOR_ATTR_LIST(last_nongc_write_time_secs),
688 NILFS_SEGCTOR_ATTR_LIST(dirty_data_blocks_count),
689 NILFS_SEGCTOR_ATTR_LIST(README),
693 NILFS_DEV_INT_GROUP_OPS(segctor, dev);
694 NILFS_DEV_INT_GROUP_TYPE(segctor, dev);
695 NILFS_DEV_INT_GROUP_FNS(segctor, dev);
697 /************************************************************************
698 * NILFS superblock attrs *
699 ************************************************************************/
702 nilfs_superblock_sb_write_time_show(struct nilfs_superblock_attr *attr,
703 struct the_nilfs *nilfs,
708 down_read(&nilfs->ns_sem);
709 sbwtime = nilfs->ns_sbwtime;
710 up_read(&nilfs->ns_sem);
712 return sysfs_emit(buf, "%ptTs\n", &sbwtime);
716 nilfs_superblock_sb_write_time_secs_show(struct nilfs_superblock_attr *attr,
717 struct the_nilfs *nilfs,
722 down_read(&nilfs->ns_sem);
723 sbwtime = nilfs->ns_sbwtime;
724 up_read(&nilfs->ns_sem);
726 return snprintf(buf, PAGE_SIZE, "%llu\n", sbwtime);
730 nilfs_superblock_sb_write_count_show(struct nilfs_superblock_attr *attr,
731 struct the_nilfs *nilfs,
734 unsigned int sbwcount;
736 down_read(&nilfs->ns_sem);
737 sbwcount = nilfs->ns_sbwcount;
738 up_read(&nilfs->ns_sem);
740 return snprintf(buf, PAGE_SIZE, "%u\n", sbwcount);
744 nilfs_superblock_sb_update_frequency_show(struct nilfs_superblock_attr *attr,
745 struct the_nilfs *nilfs,
748 unsigned int sb_update_freq;
750 down_read(&nilfs->ns_sem);
751 sb_update_freq = nilfs->ns_sb_update_freq;
752 up_read(&nilfs->ns_sem);
754 return snprintf(buf, PAGE_SIZE, "%u\n", sb_update_freq);
758 nilfs_superblock_sb_update_frequency_store(struct nilfs_superblock_attr *attr,
759 struct the_nilfs *nilfs,
760 const char *buf, size_t count)
765 err = kstrtouint(skip_spaces(buf), 0, &val);
767 nilfs_err(nilfs->ns_sb, "unable to convert string: err=%d",
772 if (val < NILFS_SB_FREQ) {
774 nilfs_warn(nilfs->ns_sb,
775 "superblock update frequency cannot be lesser than 10 seconds");
778 down_write(&nilfs->ns_sem);
779 nilfs->ns_sb_update_freq = val;
780 up_write(&nilfs->ns_sem);
785 static const char sb_readme_str[] =
786 "The superblock group contains attributes that describe\n"
787 "superblock's details.\n\n"
788 "(1) sb_write_time\n\tshow previous write time of super block "
789 "in human-readable format.\n\n"
790 "(2) sb_write_time_secs\n\tshow previous write time of super block "
792 "(3) sb_write_count\n\tshow write count of super block.\n\n"
793 "(4) sb_update_frequency\n"
794 "\tshow/set interval of periodical update of superblock (in seconds).\n\n"
795 "\tYou can set preferable frequency of superblock update by command:\n\n"
796 "\t'echo <val> > /sys/fs/<nilfs>/<dev>/superblock/sb_update_frequency'\n";
799 nilfs_superblock_README_show(struct nilfs_superblock_attr *attr,
800 struct the_nilfs *nilfs, char *buf)
802 return snprintf(buf, PAGE_SIZE, sb_readme_str);
805 NILFS_SUPERBLOCK_RO_ATTR(sb_write_time);
806 NILFS_SUPERBLOCK_RO_ATTR(sb_write_time_secs);
807 NILFS_SUPERBLOCK_RO_ATTR(sb_write_count);
808 NILFS_SUPERBLOCK_RW_ATTR(sb_update_frequency);
809 NILFS_SUPERBLOCK_RO_ATTR(README);
811 static struct attribute *nilfs_superblock_attrs[] = {
812 NILFS_SUPERBLOCK_ATTR_LIST(sb_write_time),
813 NILFS_SUPERBLOCK_ATTR_LIST(sb_write_time_secs),
814 NILFS_SUPERBLOCK_ATTR_LIST(sb_write_count),
815 NILFS_SUPERBLOCK_ATTR_LIST(sb_update_frequency),
816 NILFS_SUPERBLOCK_ATTR_LIST(README),
820 NILFS_DEV_INT_GROUP_OPS(superblock, dev);
821 NILFS_DEV_INT_GROUP_TYPE(superblock, dev);
822 NILFS_DEV_INT_GROUP_FNS(superblock, dev);
824 /************************************************************************
825 * NILFS device attrs *
826 ************************************************************************/
829 ssize_t nilfs_dev_revision_show(struct nilfs_dev_attr *attr,
830 struct the_nilfs *nilfs,
833 struct nilfs_super_block **sbp = nilfs->ns_sbp;
834 u32 major = le32_to_cpu(sbp[0]->s_rev_level);
835 u16 minor = le16_to_cpu(sbp[0]->s_minor_rev_level);
837 return snprintf(buf, PAGE_SIZE, "%d.%d\n", major, minor);
841 ssize_t nilfs_dev_blocksize_show(struct nilfs_dev_attr *attr,
842 struct the_nilfs *nilfs,
845 return snprintf(buf, PAGE_SIZE, "%u\n", nilfs->ns_blocksize);
849 ssize_t nilfs_dev_device_size_show(struct nilfs_dev_attr *attr,
850 struct the_nilfs *nilfs,
853 struct nilfs_super_block **sbp = nilfs->ns_sbp;
854 u64 dev_size = le64_to_cpu(sbp[0]->s_dev_size);
856 return snprintf(buf, PAGE_SIZE, "%llu\n", dev_size);
860 ssize_t nilfs_dev_free_blocks_show(struct nilfs_dev_attr *attr,
861 struct the_nilfs *nilfs,
864 sector_t free_blocks = 0;
866 nilfs_count_free_blocks(nilfs, &free_blocks);
867 return snprintf(buf, PAGE_SIZE, "%llu\n",
868 (unsigned long long)free_blocks);
872 ssize_t nilfs_dev_uuid_show(struct nilfs_dev_attr *attr,
873 struct the_nilfs *nilfs,
876 struct nilfs_super_block **sbp = nilfs->ns_sbp;
878 return snprintf(buf, PAGE_SIZE, "%pUb\n", sbp[0]->s_uuid);
882 ssize_t nilfs_dev_volume_name_show(struct nilfs_dev_attr *attr,
883 struct the_nilfs *nilfs,
886 struct nilfs_super_block **sbp = nilfs->ns_sbp;
888 return scnprintf(buf, sizeof(sbp[0]->s_volume_name), "%s\n",
889 sbp[0]->s_volume_name);
892 static const char dev_readme_str[] =
893 "The <device> group contains attributes that describe file system\n"
894 "partition's details.\n\n"
895 "(1) revision\n\tshow NILFS file system revision.\n\n"
896 "(2) blocksize\n\tshow volume block size in bytes.\n\n"
897 "(3) device_size\n\tshow volume size in bytes.\n\n"
898 "(4) free_blocks\n\tshow count of free blocks on volume.\n\n"
899 "(5) uuid\n\tshow volume's UUID.\n\n"
900 "(6) volume_name\n\tshow volume's name.\n\n";
902 static ssize_t nilfs_dev_README_show(struct nilfs_dev_attr *attr,
903 struct the_nilfs *nilfs,
906 return snprintf(buf, PAGE_SIZE, dev_readme_str);
909 NILFS_DEV_RO_ATTR(revision);
910 NILFS_DEV_RO_ATTR(blocksize);
911 NILFS_DEV_RO_ATTR(device_size);
912 NILFS_DEV_RO_ATTR(free_blocks);
913 NILFS_DEV_RO_ATTR(uuid);
914 NILFS_DEV_RO_ATTR(volume_name);
915 NILFS_DEV_RO_ATTR(README);
917 static struct attribute *nilfs_dev_attrs[] = {
918 NILFS_DEV_ATTR_LIST(revision),
919 NILFS_DEV_ATTR_LIST(blocksize),
920 NILFS_DEV_ATTR_LIST(device_size),
921 NILFS_DEV_ATTR_LIST(free_blocks),
922 NILFS_DEV_ATTR_LIST(uuid),
923 NILFS_DEV_ATTR_LIST(volume_name),
924 NILFS_DEV_ATTR_LIST(README),
928 static ssize_t nilfs_dev_attr_show(struct kobject *kobj,
929 struct attribute *attr, char *buf)
931 struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
933 struct nilfs_dev_attr *a = container_of(attr, struct nilfs_dev_attr,
936 return a->show ? a->show(a, nilfs, buf) : 0;
939 static ssize_t nilfs_dev_attr_store(struct kobject *kobj,
940 struct attribute *attr,
941 const char *buf, size_t len)
943 struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
945 struct nilfs_dev_attr *a = container_of(attr, struct nilfs_dev_attr,
948 return a->store ? a->store(a, nilfs, buf, len) : 0;
951 static void nilfs_dev_attr_release(struct kobject *kobj)
953 struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
955 complete(&nilfs->ns_dev_kobj_unregister);
958 static const struct sysfs_ops nilfs_dev_attr_ops = {
959 .show = nilfs_dev_attr_show,
960 .store = nilfs_dev_attr_store,
963 static struct kobj_type nilfs_dev_ktype = {
964 .default_attrs = nilfs_dev_attrs,
965 .sysfs_ops = &nilfs_dev_attr_ops,
966 .release = nilfs_dev_attr_release,
969 int nilfs_sysfs_create_device_group(struct super_block *sb)
971 struct the_nilfs *nilfs = sb->s_fs_info;
972 size_t devgrp_size = sizeof(struct nilfs_sysfs_dev_subgroups);
975 nilfs->ns_dev_subgroups = kzalloc(devgrp_size, GFP_KERNEL);
976 if (unlikely(!nilfs->ns_dev_subgroups)) {
978 nilfs_err(sb, "unable to allocate memory for device group");
979 goto failed_create_device_group;
982 nilfs->ns_dev_kobj.kset = nilfs_kset;
983 init_completion(&nilfs->ns_dev_kobj_unregister);
984 err = kobject_init_and_add(&nilfs->ns_dev_kobj, &nilfs_dev_ktype, NULL,
987 goto cleanup_dev_kobject;
989 err = nilfs_sysfs_create_mounted_snapshots_group(nilfs);
991 goto cleanup_dev_kobject;
993 err = nilfs_sysfs_create_checkpoints_group(nilfs);
995 goto delete_mounted_snapshots_group;
997 err = nilfs_sysfs_create_segments_group(nilfs);
999 goto delete_checkpoints_group;
1001 err = nilfs_sysfs_create_superblock_group(nilfs);
1003 goto delete_segments_group;
1005 err = nilfs_sysfs_create_segctor_group(nilfs);
1007 goto delete_superblock_group;
1011 delete_superblock_group:
1012 nilfs_sysfs_delete_superblock_group(nilfs);
1014 delete_segments_group:
1015 nilfs_sysfs_delete_segments_group(nilfs);
1017 delete_checkpoints_group:
1018 nilfs_sysfs_delete_checkpoints_group(nilfs);
1020 delete_mounted_snapshots_group:
1021 nilfs_sysfs_delete_mounted_snapshots_group(nilfs);
1023 cleanup_dev_kobject:
1024 kobject_put(&nilfs->ns_dev_kobj);
1025 kfree(nilfs->ns_dev_subgroups);
1027 failed_create_device_group:
1031 void nilfs_sysfs_delete_device_group(struct the_nilfs *nilfs)
1033 nilfs_sysfs_delete_mounted_snapshots_group(nilfs);
1034 nilfs_sysfs_delete_checkpoints_group(nilfs);
1035 nilfs_sysfs_delete_segments_group(nilfs);
1036 nilfs_sysfs_delete_superblock_group(nilfs);
1037 nilfs_sysfs_delete_segctor_group(nilfs);
1038 kobject_del(&nilfs->ns_dev_kobj);
1039 kobject_put(&nilfs->ns_dev_kobj);
1040 kfree(nilfs->ns_dev_subgroups);
1043 /************************************************************************
1044 * NILFS feature attrs *
1045 ************************************************************************/
1047 static ssize_t nilfs_feature_revision_show(struct kobject *kobj,
1048 struct attribute *attr, char *buf)
1050 return snprintf(buf, PAGE_SIZE, "%d.%d\n",
1051 NILFS_CURRENT_REV, NILFS_MINOR_REV);
1054 static const char features_readme_str[] =
1055 "The features group contains attributes that describe NILFS file\n"
1056 "system driver features.\n\n"
1057 "(1) revision\n\tshow current revision of NILFS file system driver.\n";
1059 static ssize_t nilfs_feature_README_show(struct kobject *kobj,
1060 struct attribute *attr,
1063 return snprintf(buf, PAGE_SIZE, features_readme_str);
1066 NILFS_FEATURE_RO_ATTR(revision);
1067 NILFS_FEATURE_RO_ATTR(README);
1069 static struct attribute *nilfs_feature_attrs[] = {
1070 NILFS_FEATURE_ATTR_LIST(revision),
1071 NILFS_FEATURE_ATTR_LIST(README),
1075 static const struct attribute_group nilfs_feature_attr_group = {
1077 .attrs = nilfs_feature_attrs,
1080 int __init nilfs_sysfs_init(void)
1084 nilfs_kset = kset_create_and_add(NILFS_ROOT_GROUP_NAME, NULL, fs_kobj);
1087 nilfs_err(NULL, "unable to create sysfs entry: err=%d", err);
1088 goto failed_sysfs_init;
1091 err = sysfs_create_group(&nilfs_kset->kobj, &nilfs_feature_attr_group);
1092 if (unlikely(err)) {
1093 nilfs_err(NULL, "unable to create feature group: err=%d", err);
1094 goto cleanup_sysfs_init;
1100 kset_unregister(nilfs_kset);
1106 void nilfs_sysfs_exit(void)
1108 sysfs_remove_group(&nilfs_kset->kobj, &nilfs_feature_attr_group);
1109 kset_unregister(nilfs_kset);