2 * sysfs.c - sysfs support implementation.
4 * Copyright (C) 2005-2014 Nippon Telegraph and Telephone Corporation.
5 * Copyright (C) 2014 HGST, Inc., a Western Digital Company.
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.
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.
17 * Written by Vyacheslav Dubeyko <Vyacheslav.Dubeyko@hgst.com>
20 #include <linux/kobject.h>
28 /* /sys/fs/<nilfs>/ */
29 static struct kset *nilfs_kset;
31 #define NILFS_SHOW_TIME(time_t_val, buf) ({ \
34 time_to_tm(time_t_val, 0, &res); \
35 res.tm_year += 1900; \
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);\
44 #define NILFS_DEV_INT_GROUP_OPS(name, parent_name) \
45 static ssize_t nilfs_##name##_attr_show(struct kobject *kobj, \
46 struct attribute *attr, char *buf) \
48 struct the_nilfs *nilfs = container_of(kobj->parent, \
50 ns_##parent_name##_kobj); \
51 struct nilfs_##name##_attr *a = container_of(attr, \
52 struct nilfs_##name##_attr, \
54 return a->show ? a->show(a, nilfs, buf) : 0; \
56 static ssize_t nilfs_##name##_attr_store(struct kobject *kobj, \
57 struct attribute *attr, \
58 const char *buf, size_t len) \
60 struct the_nilfs *nilfs = container_of(kobj->parent, \
62 ns_##parent_name##_kobj); \
63 struct nilfs_##name##_attr *a = container_of(attr, \
64 struct nilfs_##name##_attr, \
66 return a->store ? a->store(a, nilfs, buf, len) : 0; \
68 static const struct sysfs_ops nilfs_##name##_attr_ops = { \
69 .show = nilfs_##name##_attr_show, \
70 .store = nilfs_##name##_attr_store, \
73 #define NILFS_DEV_INT_GROUP_TYPE(name, parent_name) \
74 static void nilfs_##name##_attr_release(struct kobject *kobj) \
76 struct nilfs_sysfs_##parent_name##_subgroups *subgroups = container_of(kobj, \
77 struct nilfs_sysfs_##parent_name##_subgroups, \
79 complete(&subgroups->sg_##name##_kobj_unregister); \
81 static struct kobj_type nilfs_##name##_ktype = { \
82 .default_attrs = nilfs_##name##_attrs, \
83 .sysfs_ops = &nilfs_##name##_attr_ops, \
84 .release = nilfs_##name##_attr_release, \
87 #define NILFS_DEV_INT_GROUP_FNS(name, parent_name) \
88 static int nilfs_sysfs_create_##name##_group(struct the_nilfs *nilfs) \
90 struct kobject *parent; \
91 struct kobject *kobj; \
92 struct completion *kobj_unregister; \
93 struct nilfs_sysfs_##parent_name##_subgroups *subgroups; \
95 subgroups = nilfs->ns_##parent_name##_subgroups; \
96 kobj = &subgroups->sg_##name##_kobj; \
97 kobj_unregister = &subgroups->sg_##name##_kobj_unregister; \
98 parent = &nilfs->ns_##parent_name##_kobj; \
99 kobj->kset = nilfs_kset; \
100 init_completion(kobj_unregister); \
101 err = kobject_init_and_add(kobj, &nilfs_##name##_ktype, parent, \
107 static void nilfs_sysfs_delete_##name##_group(struct the_nilfs *nilfs) \
109 kobject_put(&nilfs->ns_##parent_name##_subgroups->sg_##name##_kobj); \
112 /************************************************************************
113 * NILFS snapshot attrs *
114 ************************************************************************/
117 nilfs_snapshot_inodes_count_show(struct nilfs_snapshot_attr *attr,
118 struct nilfs_root *root, char *buf)
120 return snprintf(buf, PAGE_SIZE, "%llu\n",
121 (unsigned long long)atomic64_read(&root->inodes_count));
125 nilfs_snapshot_blocks_count_show(struct nilfs_snapshot_attr *attr,
126 struct nilfs_root *root, char *buf)
128 return snprintf(buf, PAGE_SIZE, "%llu\n",
129 (unsigned long long)atomic64_read(&root->blocks_count));
132 static const char snapshot_readme_str[] =
133 "The group contains details about mounted snapshot.\n\n"
134 "(1) inodes_count\n\tshow number of inodes for snapshot.\n\n"
135 "(2) blocks_count\n\tshow number of blocks for snapshot.\n\n";
138 nilfs_snapshot_README_show(struct nilfs_snapshot_attr *attr,
139 struct nilfs_root *root, char *buf)
141 return snprintf(buf, PAGE_SIZE, snapshot_readme_str);
144 NILFS_SNAPSHOT_RO_ATTR(inodes_count);
145 NILFS_SNAPSHOT_RO_ATTR(blocks_count);
146 NILFS_SNAPSHOT_RO_ATTR(README);
148 static struct attribute *nilfs_snapshot_attrs[] = {
149 NILFS_SNAPSHOT_ATTR_LIST(inodes_count),
150 NILFS_SNAPSHOT_ATTR_LIST(blocks_count),
151 NILFS_SNAPSHOT_ATTR_LIST(README),
155 static ssize_t nilfs_snapshot_attr_show(struct kobject *kobj,
156 struct attribute *attr, char *buf)
158 struct nilfs_root *root =
159 container_of(kobj, struct nilfs_root, snapshot_kobj);
160 struct nilfs_snapshot_attr *a =
161 container_of(attr, struct nilfs_snapshot_attr, attr);
163 return a->show ? a->show(a, root, buf) : 0;
166 static ssize_t nilfs_snapshot_attr_store(struct kobject *kobj,
167 struct attribute *attr,
168 const char *buf, size_t len)
170 struct nilfs_root *root =
171 container_of(kobj, struct nilfs_root, snapshot_kobj);
172 struct nilfs_snapshot_attr *a =
173 container_of(attr, struct nilfs_snapshot_attr, attr);
175 return a->store ? a->store(a, root, buf, len) : 0;
178 static void nilfs_snapshot_attr_release(struct kobject *kobj)
180 struct nilfs_root *root = container_of(kobj, struct nilfs_root,
182 complete(&root->snapshot_kobj_unregister);
185 static const struct sysfs_ops nilfs_snapshot_attr_ops = {
186 .show = nilfs_snapshot_attr_show,
187 .store = nilfs_snapshot_attr_store,
190 static struct kobj_type nilfs_snapshot_ktype = {
191 .default_attrs = nilfs_snapshot_attrs,
192 .sysfs_ops = &nilfs_snapshot_attr_ops,
193 .release = nilfs_snapshot_attr_release,
196 int nilfs_sysfs_create_snapshot_group(struct nilfs_root *root)
198 struct the_nilfs *nilfs;
199 struct kobject *parent;
203 parent = &nilfs->ns_dev_subgroups->sg_mounted_snapshots_kobj;
204 root->snapshot_kobj.kset = nilfs_kset;
205 init_completion(&root->snapshot_kobj_unregister);
207 if (root->cno == NILFS_CPTREE_CURRENT_CNO) {
208 err = kobject_init_and_add(&root->snapshot_kobj,
209 &nilfs_snapshot_ktype,
211 "current_checkpoint");
213 err = kobject_init_and_add(&root->snapshot_kobj,
214 &nilfs_snapshot_ktype,
220 kobject_put(&root->snapshot_kobj);
225 void nilfs_sysfs_delete_snapshot_group(struct nilfs_root *root)
227 kobject_put(&root->snapshot_kobj);
230 /************************************************************************
231 * NILFS mounted snapshots attrs *
232 ************************************************************************/
234 static const char mounted_snapshots_readme_str[] =
235 "The mounted_snapshots group contains group for\n"
236 "every mounted snapshot.\n";
239 nilfs_mounted_snapshots_README_show(struct nilfs_mounted_snapshots_attr *attr,
240 struct the_nilfs *nilfs, char *buf)
242 return snprintf(buf, PAGE_SIZE, mounted_snapshots_readme_str);
245 NILFS_MOUNTED_SNAPSHOTS_RO_ATTR(README);
247 static struct attribute *nilfs_mounted_snapshots_attrs[] = {
248 NILFS_MOUNTED_SNAPSHOTS_ATTR_LIST(README),
252 NILFS_DEV_INT_GROUP_OPS(mounted_snapshots, dev);
253 NILFS_DEV_INT_GROUP_TYPE(mounted_snapshots, dev);
254 NILFS_DEV_INT_GROUP_FNS(mounted_snapshots, dev);
256 /************************************************************************
257 * NILFS checkpoints attrs *
258 ************************************************************************/
261 nilfs_checkpoints_checkpoints_number_show(struct nilfs_checkpoints_attr *attr,
262 struct the_nilfs *nilfs,
266 struct nilfs_cpstat cpstat;
269 down_read(&nilfs->ns_segctor_sem);
270 err = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
271 up_read(&nilfs->ns_segctor_sem);
273 nilfs_msg(nilfs->ns_sb, KERN_ERR,
274 "unable to get checkpoint stat: err=%d", err);
278 ncheckpoints = cpstat.cs_ncps;
280 return snprintf(buf, PAGE_SIZE, "%llu\n", ncheckpoints);
284 nilfs_checkpoints_snapshots_number_show(struct nilfs_checkpoints_attr *attr,
285 struct the_nilfs *nilfs,
289 struct nilfs_cpstat cpstat;
292 down_read(&nilfs->ns_segctor_sem);
293 err = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
294 up_read(&nilfs->ns_segctor_sem);
296 nilfs_msg(nilfs->ns_sb, KERN_ERR,
297 "unable to get checkpoint stat: err=%d", err);
301 nsnapshots = cpstat.cs_nsss;
303 return snprintf(buf, PAGE_SIZE, "%llu\n", nsnapshots);
307 nilfs_checkpoints_last_seg_checkpoint_show(struct nilfs_checkpoints_attr *attr,
308 struct the_nilfs *nilfs,
313 spin_lock(&nilfs->ns_last_segment_lock);
314 last_cno = nilfs->ns_last_cno;
315 spin_unlock(&nilfs->ns_last_segment_lock);
317 return snprintf(buf, PAGE_SIZE, "%llu\n", last_cno);
321 nilfs_checkpoints_next_checkpoint_show(struct nilfs_checkpoints_attr *attr,
322 struct the_nilfs *nilfs,
327 down_read(&nilfs->ns_segctor_sem);
329 up_read(&nilfs->ns_segctor_sem);
331 return snprintf(buf, PAGE_SIZE, "%llu\n", cno);
334 static const char checkpoints_readme_str[] =
335 "The checkpoints group contains attributes that describe\n"
336 "details about volume's checkpoints.\n\n"
337 "(1) checkpoints_number\n\tshow number of checkpoints on volume.\n\n"
338 "(2) snapshots_number\n\tshow number of snapshots on volume.\n\n"
339 "(3) last_seg_checkpoint\n"
340 "\tshow checkpoint number of the latest segment.\n\n"
341 "(4) next_checkpoint\n\tshow next checkpoint number.\n\n";
344 nilfs_checkpoints_README_show(struct nilfs_checkpoints_attr *attr,
345 struct the_nilfs *nilfs, char *buf)
347 return snprintf(buf, PAGE_SIZE, checkpoints_readme_str);
350 NILFS_CHECKPOINTS_RO_ATTR(checkpoints_number);
351 NILFS_CHECKPOINTS_RO_ATTR(snapshots_number);
352 NILFS_CHECKPOINTS_RO_ATTR(last_seg_checkpoint);
353 NILFS_CHECKPOINTS_RO_ATTR(next_checkpoint);
354 NILFS_CHECKPOINTS_RO_ATTR(README);
356 static struct attribute *nilfs_checkpoints_attrs[] = {
357 NILFS_CHECKPOINTS_ATTR_LIST(checkpoints_number),
358 NILFS_CHECKPOINTS_ATTR_LIST(snapshots_number),
359 NILFS_CHECKPOINTS_ATTR_LIST(last_seg_checkpoint),
360 NILFS_CHECKPOINTS_ATTR_LIST(next_checkpoint),
361 NILFS_CHECKPOINTS_ATTR_LIST(README),
365 NILFS_DEV_INT_GROUP_OPS(checkpoints, dev);
366 NILFS_DEV_INT_GROUP_TYPE(checkpoints, dev);
367 NILFS_DEV_INT_GROUP_FNS(checkpoints, dev);
369 /************************************************************************
370 * NILFS segments attrs *
371 ************************************************************************/
374 nilfs_segments_segments_number_show(struct nilfs_segments_attr *attr,
375 struct the_nilfs *nilfs,
378 return snprintf(buf, PAGE_SIZE, "%lu\n", nilfs->ns_nsegments);
382 nilfs_segments_blocks_per_segment_show(struct nilfs_segments_attr *attr,
383 struct the_nilfs *nilfs,
386 return snprintf(buf, PAGE_SIZE, "%lu\n", nilfs->ns_blocks_per_segment);
390 nilfs_segments_clean_segments_show(struct nilfs_segments_attr *attr,
391 struct the_nilfs *nilfs,
394 unsigned long ncleansegs;
396 down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
397 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
398 up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
400 return snprintf(buf, PAGE_SIZE, "%lu\n", ncleansegs);
404 nilfs_segments_dirty_segments_show(struct nilfs_segments_attr *attr,
405 struct the_nilfs *nilfs,
408 struct nilfs_sustat sustat;
411 down_read(&nilfs->ns_segctor_sem);
412 err = nilfs_sufile_get_stat(nilfs->ns_sufile, &sustat);
413 up_read(&nilfs->ns_segctor_sem);
415 nilfs_msg(nilfs->ns_sb, KERN_ERR,
416 "unable to get segment stat: err=%d", err);
420 return snprintf(buf, PAGE_SIZE, "%llu\n", sustat.ss_ndirtysegs);
423 static const char segments_readme_str[] =
424 "The segments group contains attributes that describe\n"
425 "details about volume's segments.\n\n"
426 "(1) segments_number\n\tshow number of segments on volume.\n\n"
427 "(2) blocks_per_segment\n\tshow number of blocks in segment.\n\n"
428 "(3) clean_segments\n\tshow count of clean segments.\n\n"
429 "(4) dirty_segments\n\tshow count of dirty segments.\n\n";
432 nilfs_segments_README_show(struct nilfs_segments_attr *attr,
433 struct the_nilfs *nilfs,
436 return snprintf(buf, PAGE_SIZE, segments_readme_str);
439 NILFS_SEGMENTS_RO_ATTR(segments_number);
440 NILFS_SEGMENTS_RO_ATTR(blocks_per_segment);
441 NILFS_SEGMENTS_RO_ATTR(clean_segments);
442 NILFS_SEGMENTS_RO_ATTR(dirty_segments);
443 NILFS_SEGMENTS_RO_ATTR(README);
445 static struct attribute *nilfs_segments_attrs[] = {
446 NILFS_SEGMENTS_ATTR_LIST(segments_number),
447 NILFS_SEGMENTS_ATTR_LIST(blocks_per_segment),
448 NILFS_SEGMENTS_ATTR_LIST(clean_segments),
449 NILFS_SEGMENTS_ATTR_LIST(dirty_segments),
450 NILFS_SEGMENTS_ATTR_LIST(README),
454 NILFS_DEV_INT_GROUP_OPS(segments, dev);
455 NILFS_DEV_INT_GROUP_TYPE(segments, dev);
456 NILFS_DEV_INT_GROUP_FNS(segments, dev);
458 /************************************************************************
459 * NILFS segctor attrs *
460 ************************************************************************/
463 nilfs_segctor_last_pseg_block_show(struct nilfs_segctor_attr *attr,
464 struct the_nilfs *nilfs,
469 spin_lock(&nilfs->ns_last_segment_lock);
470 last_pseg = nilfs->ns_last_pseg;
471 spin_unlock(&nilfs->ns_last_segment_lock);
473 return snprintf(buf, PAGE_SIZE, "%llu\n",
474 (unsigned long long)last_pseg);
478 nilfs_segctor_last_seg_sequence_show(struct nilfs_segctor_attr *attr,
479 struct the_nilfs *nilfs,
484 spin_lock(&nilfs->ns_last_segment_lock);
485 last_seq = nilfs->ns_last_seq;
486 spin_unlock(&nilfs->ns_last_segment_lock);
488 return snprintf(buf, PAGE_SIZE, "%llu\n", last_seq);
492 nilfs_segctor_last_seg_checkpoint_show(struct nilfs_segctor_attr *attr,
493 struct the_nilfs *nilfs,
498 spin_lock(&nilfs->ns_last_segment_lock);
499 last_cno = nilfs->ns_last_cno;
500 spin_unlock(&nilfs->ns_last_segment_lock);
502 return snprintf(buf, PAGE_SIZE, "%llu\n", last_cno);
506 nilfs_segctor_current_seg_sequence_show(struct nilfs_segctor_attr *attr,
507 struct the_nilfs *nilfs,
512 down_read(&nilfs->ns_segctor_sem);
513 seg_seq = nilfs->ns_seg_seq;
514 up_read(&nilfs->ns_segctor_sem);
516 return snprintf(buf, PAGE_SIZE, "%llu\n", seg_seq);
520 nilfs_segctor_current_last_full_seg_show(struct nilfs_segctor_attr *attr,
521 struct the_nilfs *nilfs,
526 down_read(&nilfs->ns_segctor_sem);
527 segnum = nilfs->ns_segnum;
528 up_read(&nilfs->ns_segctor_sem);
530 return snprintf(buf, PAGE_SIZE, "%llu\n", segnum);
534 nilfs_segctor_next_full_seg_show(struct nilfs_segctor_attr *attr,
535 struct the_nilfs *nilfs,
540 down_read(&nilfs->ns_segctor_sem);
541 nextnum = nilfs->ns_nextnum;
542 up_read(&nilfs->ns_segctor_sem);
544 return snprintf(buf, PAGE_SIZE, "%llu\n", nextnum);
548 nilfs_segctor_next_pseg_offset_show(struct nilfs_segctor_attr *attr,
549 struct the_nilfs *nilfs,
552 unsigned long pseg_offset;
554 down_read(&nilfs->ns_segctor_sem);
555 pseg_offset = nilfs->ns_pseg_offset;
556 up_read(&nilfs->ns_segctor_sem);
558 return snprintf(buf, PAGE_SIZE, "%lu\n", pseg_offset);
562 nilfs_segctor_next_checkpoint_show(struct nilfs_segctor_attr *attr,
563 struct the_nilfs *nilfs,
568 down_read(&nilfs->ns_segctor_sem);
570 up_read(&nilfs->ns_segctor_sem);
572 return snprintf(buf, PAGE_SIZE, "%llu\n", cno);
576 nilfs_segctor_last_seg_write_time_show(struct nilfs_segctor_attr *attr,
577 struct the_nilfs *nilfs,
582 down_read(&nilfs->ns_segctor_sem);
583 ctime = nilfs->ns_ctime;
584 up_read(&nilfs->ns_segctor_sem);
586 return NILFS_SHOW_TIME(ctime, buf);
590 nilfs_segctor_last_seg_write_time_secs_show(struct nilfs_segctor_attr *attr,
591 struct the_nilfs *nilfs,
596 down_read(&nilfs->ns_segctor_sem);
597 ctime = nilfs->ns_ctime;
598 up_read(&nilfs->ns_segctor_sem);
600 return snprintf(buf, PAGE_SIZE, "%llu\n", (unsigned long long)ctime);
604 nilfs_segctor_last_nongc_write_time_show(struct nilfs_segctor_attr *attr,
605 struct the_nilfs *nilfs,
610 down_read(&nilfs->ns_segctor_sem);
611 nongc_ctime = nilfs->ns_nongc_ctime;
612 up_read(&nilfs->ns_segctor_sem);
614 return NILFS_SHOW_TIME(nongc_ctime, buf);
618 nilfs_segctor_last_nongc_write_time_secs_show(struct nilfs_segctor_attr *attr,
619 struct the_nilfs *nilfs,
624 down_read(&nilfs->ns_segctor_sem);
625 nongc_ctime = nilfs->ns_nongc_ctime;
626 up_read(&nilfs->ns_segctor_sem);
628 return snprintf(buf, PAGE_SIZE, "%llu\n",
629 (unsigned long long)nongc_ctime);
633 nilfs_segctor_dirty_data_blocks_count_show(struct nilfs_segctor_attr *attr,
634 struct the_nilfs *nilfs,
639 down_read(&nilfs->ns_segctor_sem);
640 ndirtyblks = atomic_read(&nilfs->ns_ndirtyblks);
641 up_read(&nilfs->ns_segctor_sem);
643 return snprintf(buf, PAGE_SIZE, "%u\n", ndirtyblks);
646 static const char segctor_readme_str[] =
647 "The segctor group contains attributes that describe\n"
648 "segctor thread activity details.\n\n"
649 "(1) last_pseg_block\n"
650 "\tshow start block number of the latest segment.\n\n"
651 "(2) last_seg_sequence\n"
652 "\tshow sequence value of the latest segment.\n\n"
653 "(3) last_seg_checkpoint\n"
654 "\tshow checkpoint number of the latest segment.\n\n"
655 "(4) current_seg_sequence\n\tshow segment sequence counter.\n\n"
656 "(5) current_last_full_seg\n"
657 "\tshow index number of the latest full segment.\n\n"
658 "(6) next_full_seg\n"
659 "\tshow index number of the full segment index to be used next.\n\n"
660 "(7) next_pseg_offset\n"
661 "\tshow offset of next partial segment in the current full segment.\n\n"
662 "(8) next_checkpoint\n\tshow next checkpoint number.\n\n"
663 "(9) last_seg_write_time\n"
664 "\tshow write time of the last segment in human-readable format.\n\n"
665 "(10) last_seg_write_time_secs\n"
666 "\tshow write time of the last segment in seconds.\n\n"
667 "(11) last_nongc_write_time\n"
668 "\tshow write time of the last segment not for cleaner operation "
669 "in human-readable format.\n\n"
670 "(12) last_nongc_write_time_secs\n"
671 "\tshow write time of the last segment not for cleaner operation "
673 "(13) dirty_data_blocks_count\n"
674 "\tshow number of dirty data blocks.\n\n";
677 nilfs_segctor_README_show(struct nilfs_segctor_attr *attr,
678 struct the_nilfs *nilfs, char *buf)
680 return snprintf(buf, PAGE_SIZE, segctor_readme_str);
683 NILFS_SEGCTOR_RO_ATTR(last_pseg_block);
684 NILFS_SEGCTOR_RO_ATTR(last_seg_sequence);
685 NILFS_SEGCTOR_RO_ATTR(last_seg_checkpoint);
686 NILFS_SEGCTOR_RO_ATTR(current_seg_sequence);
687 NILFS_SEGCTOR_RO_ATTR(current_last_full_seg);
688 NILFS_SEGCTOR_RO_ATTR(next_full_seg);
689 NILFS_SEGCTOR_RO_ATTR(next_pseg_offset);
690 NILFS_SEGCTOR_RO_ATTR(next_checkpoint);
691 NILFS_SEGCTOR_RO_ATTR(last_seg_write_time);
692 NILFS_SEGCTOR_RO_ATTR(last_seg_write_time_secs);
693 NILFS_SEGCTOR_RO_ATTR(last_nongc_write_time);
694 NILFS_SEGCTOR_RO_ATTR(last_nongc_write_time_secs);
695 NILFS_SEGCTOR_RO_ATTR(dirty_data_blocks_count);
696 NILFS_SEGCTOR_RO_ATTR(README);
698 static struct attribute *nilfs_segctor_attrs[] = {
699 NILFS_SEGCTOR_ATTR_LIST(last_pseg_block),
700 NILFS_SEGCTOR_ATTR_LIST(last_seg_sequence),
701 NILFS_SEGCTOR_ATTR_LIST(last_seg_checkpoint),
702 NILFS_SEGCTOR_ATTR_LIST(current_seg_sequence),
703 NILFS_SEGCTOR_ATTR_LIST(current_last_full_seg),
704 NILFS_SEGCTOR_ATTR_LIST(next_full_seg),
705 NILFS_SEGCTOR_ATTR_LIST(next_pseg_offset),
706 NILFS_SEGCTOR_ATTR_LIST(next_checkpoint),
707 NILFS_SEGCTOR_ATTR_LIST(last_seg_write_time),
708 NILFS_SEGCTOR_ATTR_LIST(last_seg_write_time_secs),
709 NILFS_SEGCTOR_ATTR_LIST(last_nongc_write_time),
710 NILFS_SEGCTOR_ATTR_LIST(last_nongc_write_time_secs),
711 NILFS_SEGCTOR_ATTR_LIST(dirty_data_blocks_count),
712 NILFS_SEGCTOR_ATTR_LIST(README),
716 NILFS_DEV_INT_GROUP_OPS(segctor, dev);
717 NILFS_DEV_INT_GROUP_TYPE(segctor, dev);
718 NILFS_DEV_INT_GROUP_FNS(segctor, dev);
720 /************************************************************************
721 * NILFS superblock attrs *
722 ************************************************************************/
725 nilfs_superblock_sb_write_time_show(struct nilfs_superblock_attr *attr,
726 struct the_nilfs *nilfs,
731 down_read(&nilfs->ns_sem);
732 sbwtime = nilfs->ns_sbwtime;
733 up_read(&nilfs->ns_sem);
735 return NILFS_SHOW_TIME(sbwtime, buf);
739 nilfs_superblock_sb_write_time_secs_show(struct nilfs_superblock_attr *attr,
740 struct the_nilfs *nilfs,
745 down_read(&nilfs->ns_sem);
746 sbwtime = nilfs->ns_sbwtime;
747 up_read(&nilfs->ns_sem);
749 return snprintf(buf, PAGE_SIZE, "%llu\n", (unsigned long long)sbwtime);
753 nilfs_superblock_sb_write_count_show(struct nilfs_superblock_attr *attr,
754 struct the_nilfs *nilfs,
757 unsigned int sbwcount;
759 down_read(&nilfs->ns_sem);
760 sbwcount = nilfs->ns_sbwcount;
761 up_read(&nilfs->ns_sem);
763 return snprintf(buf, PAGE_SIZE, "%u\n", sbwcount);
767 nilfs_superblock_sb_update_frequency_show(struct nilfs_superblock_attr *attr,
768 struct the_nilfs *nilfs,
771 unsigned int sb_update_freq;
773 down_read(&nilfs->ns_sem);
774 sb_update_freq = nilfs->ns_sb_update_freq;
775 up_read(&nilfs->ns_sem);
777 return snprintf(buf, PAGE_SIZE, "%u\n", sb_update_freq);
781 nilfs_superblock_sb_update_frequency_store(struct nilfs_superblock_attr *attr,
782 struct the_nilfs *nilfs,
783 const char *buf, size_t count)
788 err = kstrtouint(skip_spaces(buf), 0, &val);
790 nilfs_msg(nilfs->ns_sb, KERN_ERR,
791 "unable to convert string: err=%d", err);
795 if (val < NILFS_SB_FREQ) {
797 nilfs_msg(nilfs->ns_sb, KERN_WARNING,
798 "superblock update frequency cannot be lesser than 10 seconds");
801 down_write(&nilfs->ns_sem);
802 nilfs->ns_sb_update_freq = val;
803 up_write(&nilfs->ns_sem);
808 static const char sb_readme_str[] =
809 "The superblock group contains attributes that describe\n"
810 "superblock's details.\n\n"
811 "(1) sb_write_time\n\tshow previous write time of super block "
812 "in human-readable format.\n\n"
813 "(2) sb_write_time_secs\n\tshow previous write time of super block "
815 "(3) sb_write_count\n\tshow write count of super block.\n\n"
816 "(4) sb_update_frequency\n"
817 "\tshow/set interval of periodical update of superblock (in seconds).\n\n"
818 "\tYou can set preferable frequency of superblock update by command:\n\n"
819 "\t'echo <val> > /sys/fs/<nilfs>/<dev>/superblock/sb_update_frequency'\n";
822 nilfs_superblock_README_show(struct nilfs_superblock_attr *attr,
823 struct the_nilfs *nilfs, char *buf)
825 return snprintf(buf, PAGE_SIZE, sb_readme_str);
828 NILFS_SUPERBLOCK_RO_ATTR(sb_write_time);
829 NILFS_SUPERBLOCK_RO_ATTR(sb_write_time_secs);
830 NILFS_SUPERBLOCK_RO_ATTR(sb_write_count);
831 NILFS_SUPERBLOCK_RW_ATTR(sb_update_frequency);
832 NILFS_SUPERBLOCK_RO_ATTR(README);
834 static struct attribute *nilfs_superblock_attrs[] = {
835 NILFS_SUPERBLOCK_ATTR_LIST(sb_write_time),
836 NILFS_SUPERBLOCK_ATTR_LIST(sb_write_time_secs),
837 NILFS_SUPERBLOCK_ATTR_LIST(sb_write_count),
838 NILFS_SUPERBLOCK_ATTR_LIST(sb_update_frequency),
839 NILFS_SUPERBLOCK_ATTR_LIST(README),
843 NILFS_DEV_INT_GROUP_OPS(superblock, dev);
844 NILFS_DEV_INT_GROUP_TYPE(superblock, dev);
845 NILFS_DEV_INT_GROUP_FNS(superblock, dev);
847 /************************************************************************
848 * NILFS device attrs *
849 ************************************************************************/
852 ssize_t nilfs_dev_revision_show(struct nilfs_dev_attr *attr,
853 struct the_nilfs *nilfs,
856 struct nilfs_super_block **sbp = nilfs->ns_sbp;
857 u32 major = le32_to_cpu(sbp[0]->s_rev_level);
858 u16 minor = le16_to_cpu(sbp[0]->s_minor_rev_level);
860 return snprintf(buf, PAGE_SIZE, "%d.%d\n", major, minor);
864 ssize_t nilfs_dev_blocksize_show(struct nilfs_dev_attr *attr,
865 struct the_nilfs *nilfs,
868 return snprintf(buf, PAGE_SIZE, "%u\n", nilfs->ns_blocksize);
872 ssize_t nilfs_dev_device_size_show(struct nilfs_dev_attr *attr,
873 struct the_nilfs *nilfs,
876 struct nilfs_super_block **sbp = nilfs->ns_sbp;
877 u64 dev_size = le64_to_cpu(sbp[0]->s_dev_size);
879 return snprintf(buf, PAGE_SIZE, "%llu\n", dev_size);
883 ssize_t nilfs_dev_free_blocks_show(struct nilfs_dev_attr *attr,
884 struct the_nilfs *nilfs,
887 sector_t free_blocks = 0;
889 nilfs_count_free_blocks(nilfs, &free_blocks);
890 return snprintf(buf, PAGE_SIZE, "%llu\n",
891 (unsigned long long)free_blocks);
895 ssize_t nilfs_dev_uuid_show(struct nilfs_dev_attr *attr,
896 struct the_nilfs *nilfs,
899 struct nilfs_super_block **sbp = nilfs->ns_sbp;
901 return snprintf(buf, PAGE_SIZE, "%pUb\n", sbp[0]->s_uuid);
905 ssize_t nilfs_dev_volume_name_show(struct nilfs_dev_attr *attr,
906 struct the_nilfs *nilfs,
909 struct nilfs_super_block **sbp = nilfs->ns_sbp;
911 return scnprintf(buf, sizeof(sbp[0]->s_volume_name), "%s\n",
912 sbp[0]->s_volume_name);
915 static const char dev_readme_str[] =
916 "The <device> group contains attributes that describe file system\n"
917 "partition's details.\n\n"
918 "(1) revision\n\tshow NILFS file system revision.\n\n"
919 "(2) blocksize\n\tshow volume block size in bytes.\n\n"
920 "(3) device_size\n\tshow volume size in bytes.\n\n"
921 "(4) free_blocks\n\tshow count of free blocks on volume.\n\n"
922 "(5) uuid\n\tshow volume's UUID.\n\n"
923 "(6) volume_name\n\tshow volume's name.\n\n";
925 static ssize_t nilfs_dev_README_show(struct nilfs_dev_attr *attr,
926 struct the_nilfs *nilfs,
929 return snprintf(buf, PAGE_SIZE, dev_readme_str);
932 NILFS_DEV_RO_ATTR(revision);
933 NILFS_DEV_RO_ATTR(blocksize);
934 NILFS_DEV_RO_ATTR(device_size);
935 NILFS_DEV_RO_ATTR(free_blocks);
936 NILFS_DEV_RO_ATTR(uuid);
937 NILFS_DEV_RO_ATTR(volume_name);
938 NILFS_DEV_RO_ATTR(README);
940 static struct attribute *nilfs_dev_attrs[] = {
941 NILFS_DEV_ATTR_LIST(revision),
942 NILFS_DEV_ATTR_LIST(blocksize),
943 NILFS_DEV_ATTR_LIST(device_size),
944 NILFS_DEV_ATTR_LIST(free_blocks),
945 NILFS_DEV_ATTR_LIST(uuid),
946 NILFS_DEV_ATTR_LIST(volume_name),
947 NILFS_DEV_ATTR_LIST(README),
951 static ssize_t nilfs_dev_attr_show(struct kobject *kobj,
952 struct attribute *attr, char *buf)
954 struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
956 struct nilfs_dev_attr *a = container_of(attr, struct nilfs_dev_attr,
959 return a->show ? a->show(a, nilfs, buf) : 0;
962 static ssize_t nilfs_dev_attr_store(struct kobject *kobj,
963 struct attribute *attr,
964 const char *buf, size_t len)
966 struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
968 struct nilfs_dev_attr *a = container_of(attr, struct nilfs_dev_attr,
971 return a->store ? a->store(a, nilfs, buf, len) : 0;
974 static void nilfs_dev_attr_release(struct kobject *kobj)
976 struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
978 complete(&nilfs->ns_dev_kobj_unregister);
981 static const struct sysfs_ops nilfs_dev_attr_ops = {
982 .show = nilfs_dev_attr_show,
983 .store = nilfs_dev_attr_store,
986 static struct kobj_type nilfs_dev_ktype = {
987 .default_attrs = nilfs_dev_attrs,
988 .sysfs_ops = &nilfs_dev_attr_ops,
989 .release = nilfs_dev_attr_release,
992 int nilfs_sysfs_create_device_group(struct super_block *sb)
994 struct the_nilfs *nilfs = sb->s_fs_info;
995 size_t devgrp_size = sizeof(struct nilfs_sysfs_dev_subgroups);
998 nilfs->ns_dev_subgroups = kzalloc(devgrp_size, GFP_KERNEL);
999 if (unlikely(!nilfs->ns_dev_subgroups)) {
1001 nilfs_msg(sb, KERN_ERR,
1002 "unable to allocate memory for device group");
1003 goto failed_create_device_group;
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,
1011 goto cleanup_dev_kobject;
1013 err = nilfs_sysfs_create_mounted_snapshots_group(nilfs);
1015 goto cleanup_dev_kobject;
1017 err = nilfs_sysfs_create_checkpoints_group(nilfs);
1019 goto delete_mounted_snapshots_group;
1021 err = nilfs_sysfs_create_segments_group(nilfs);
1023 goto delete_checkpoints_group;
1025 err = nilfs_sysfs_create_superblock_group(nilfs);
1027 goto delete_segments_group;
1029 err = nilfs_sysfs_create_segctor_group(nilfs);
1031 goto delete_superblock_group;
1035 delete_superblock_group:
1036 nilfs_sysfs_delete_superblock_group(nilfs);
1038 delete_segments_group:
1039 nilfs_sysfs_delete_segments_group(nilfs);
1041 delete_checkpoints_group:
1042 nilfs_sysfs_delete_checkpoints_group(nilfs);
1044 delete_mounted_snapshots_group:
1045 nilfs_sysfs_delete_mounted_snapshots_group(nilfs);
1047 cleanup_dev_kobject:
1048 kobject_put(&nilfs->ns_dev_kobj);
1049 kfree(nilfs->ns_dev_subgroups);
1051 failed_create_device_group:
1055 void nilfs_sysfs_delete_device_group(struct the_nilfs *nilfs)
1057 nilfs_sysfs_delete_mounted_snapshots_group(nilfs);
1058 nilfs_sysfs_delete_checkpoints_group(nilfs);
1059 nilfs_sysfs_delete_segments_group(nilfs);
1060 nilfs_sysfs_delete_superblock_group(nilfs);
1061 nilfs_sysfs_delete_segctor_group(nilfs);
1062 kobject_del(&nilfs->ns_dev_kobj);
1063 kobject_put(&nilfs->ns_dev_kobj);
1064 kfree(nilfs->ns_dev_subgroups);
1067 /************************************************************************
1068 * NILFS feature attrs *
1069 ************************************************************************/
1071 static ssize_t nilfs_feature_revision_show(struct kobject *kobj,
1072 struct attribute *attr, char *buf)
1074 return snprintf(buf, PAGE_SIZE, "%d.%d\n",
1075 NILFS_CURRENT_REV, NILFS_MINOR_REV);
1078 static const char features_readme_str[] =
1079 "The features group contains attributes that describe NILFS file\n"
1080 "system driver features.\n\n"
1081 "(1) revision\n\tshow current revision of NILFS file system driver.\n";
1083 static ssize_t nilfs_feature_README_show(struct kobject *kobj,
1084 struct attribute *attr,
1087 return snprintf(buf, PAGE_SIZE, features_readme_str);
1090 NILFS_FEATURE_RO_ATTR(revision);
1091 NILFS_FEATURE_RO_ATTR(README);
1093 static struct attribute *nilfs_feature_attrs[] = {
1094 NILFS_FEATURE_ATTR_LIST(revision),
1095 NILFS_FEATURE_ATTR_LIST(README),
1099 static const struct attribute_group nilfs_feature_attr_group = {
1101 .attrs = nilfs_feature_attrs,
1104 int __init nilfs_sysfs_init(void)
1108 nilfs_kset = kset_create_and_add(NILFS_ROOT_GROUP_NAME, NULL, fs_kobj);
1111 nilfs_msg(NULL, KERN_ERR,
1112 "unable to create sysfs entry: err=%d", err);
1113 goto failed_sysfs_init;
1116 err = sysfs_create_group(&nilfs_kset->kobj, &nilfs_feature_attr_group);
1117 if (unlikely(err)) {
1118 nilfs_msg(NULL, KERN_ERR,
1119 "unable to create feature group: err=%d", err);
1120 goto cleanup_sysfs_init;
1126 kset_unregister(nilfs_kset);
1132 void nilfs_sysfs_exit(void)
1134 sysfs_remove_group(&nilfs_kset->kobj, &nilfs_feature_attr_group);
1135 kset_unregister(nilfs_kset);