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 printk(KERN_ERR "NILFS: unable to get checkpoint stat: err=%d\n",
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 printk(KERN_ERR "NILFS: unable to get checkpoint stat: err=%d\n",
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_sem);
329 up_read(&nilfs->ns_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 printk(KERN_ERR "NILFS: unable to get segment stat: err=%d\n",
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_sem);
513 seg_seq = nilfs->ns_seg_seq;
514 up_read(&nilfs->ns_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_sem);
527 segnum = nilfs->ns_segnum;
528 up_read(&nilfs->ns_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_sem);
541 nextnum = nilfs->ns_nextnum;
542 up_read(&nilfs->ns_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_sem);
555 pseg_offset = nilfs->ns_pseg_offset;
556 up_read(&nilfs->ns_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_sem);
570 up_read(&nilfs->ns_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_sem);
583 ctime = nilfs->ns_ctime;
584 up_read(&nilfs->ns_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_sem);
597 ctime = nilfs->ns_ctime;
598 up_read(&nilfs->ns_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_sem);
611 nongc_ctime = nilfs->ns_nongc_ctime;
612 up_read(&nilfs->ns_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_sem);
625 nongc_ctime = nilfs->ns_nongc_ctime;
626 up_read(&nilfs->ns_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_sem);
640 ndirtyblks = atomic_read(&nilfs->ns_ndirtyblks);
641 up_read(&nilfs->ns_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,
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 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 printk(KERN_ERR "NILFS: unable to convert string: err=%d\n",
795 if (val < NILFS_SB_FREQ) {
797 printk(KERN_WARNING "NILFS: superblock update frequency cannot be lesser than 10 seconds\n");
800 down_write(&nilfs->ns_sem);
801 nilfs->ns_sb_update_freq = val;
802 up_write(&nilfs->ns_sem);
807 static const char sb_readme_str[] =
808 "The superblock group contains attributes that describe\n"
809 "superblock's details.\n\n"
810 "(1) sb_write_time\n\tshow previous write time of super block "
811 "in human-readable format.\n\n"
812 "(2) sb_write_time_secs\n\tshow previous write time of super block "
814 "(3) sb_write_count\n\tshow write count of super block.\n\n"
815 "(4) sb_update_frequency\n"
816 "\tshow/set interval of periodical update of superblock (in seconds).\n\n"
817 "\tYou can set preferable frequency of superblock update by command:\n\n"
818 "\t'echo <val> > /sys/fs/<nilfs>/<dev>/superblock/sb_update_frequency'\n";
821 nilfs_superblock_README_show(struct nilfs_superblock_attr *attr,
822 struct the_nilfs *nilfs, char *buf)
824 return snprintf(buf, PAGE_SIZE, sb_readme_str);
827 NILFS_SUPERBLOCK_RO_ATTR(sb_write_time);
828 NILFS_SUPERBLOCK_RO_ATTR(sb_write_time_secs);
829 NILFS_SUPERBLOCK_RO_ATTR(sb_write_count);
830 NILFS_SUPERBLOCK_RW_ATTR(sb_update_frequency);
831 NILFS_SUPERBLOCK_RO_ATTR(README);
833 static struct attribute *nilfs_superblock_attrs[] = {
834 NILFS_SUPERBLOCK_ATTR_LIST(sb_write_time),
835 NILFS_SUPERBLOCK_ATTR_LIST(sb_write_time_secs),
836 NILFS_SUPERBLOCK_ATTR_LIST(sb_write_count),
837 NILFS_SUPERBLOCK_ATTR_LIST(sb_update_frequency),
838 NILFS_SUPERBLOCK_ATTR_LIST(README),
842 NILFS_DEV_INT_GROUP_OPS(superblock, dev);
843 NILFS_DEV_INT_GROUP_TYPE(superblock, dev);
844 NILFS_DEV_INT_GROUP_FNS(superblock, dev);
846 /************************************************************************
847 * NILFS device attrs *
848 ************************************************************************/
851 ssize_t nilfs_dev_revision_show(struct nilfs_dev_attr *attr,
852 struct the_nilfs *nilfs,
855 struct nilfs_super_block **sbp = nilfs->ns_sbp;
856 u32 major = le32_to_cpu(sbp[0]->s_rev_level);
857 u16 minor = le16_to_cpu(sbp[0]->s_minor_rev_level);
859 return snprintf(buf, PAGE_SIZE, "%d.%d\n", major, minor);
863 ssize_t nilfs_dev_blocksize_show(struct nilfs_dev_attr *attr,
864 struct the_nilfs *nilfs,
867 return snprintf(buf, PAGE_SIZE, "%u\n", nilfs->ns_blocksize);
871 ssize_t nilfs_dev_device_size_show(struct nilfs_dev_attr *attr,
872 struct the_nilfs *nilfs,
875 struct nilfs_super_block **sbp = nilfs->ns_sbp;
876 u64 dev_size = le64_to_cpu(sbp[0]->s_dev_size);
878 return snprintf(buf, PAGE_SIZE, "%llu\n", dev_size);
882 ssize_t nilfs_dev_free_blocks_show(struct nilfs_dev_attr *attr,
883 struct the_nilfs *nilfs,
886 sector_t free_blocks = 0;
888 nilfs_count_free_blocks(nilfs, &free_blocks);
889 return snprintf(buf, PAGE_SIZE, "%llu\n",
890 (unsigned long long)free_blocks);
894 ssize_t nilfs_dev_uuid_show(struct nilfs_dev_attr *attr,
895 struct the_nilfs *nilfs,
898 struct nilfs_super_block **sbp = nilfs->ns_sbp;
900 return snprintf(buf, PAGE_SIZE, "%pUb\n", sbp[0]->s_uuid);
904 ssize_t nilfs_dev_volume_name_show(struct nilfs_dev_attr *attr,
905 struct the_nilfs *nilfs,
908 struct nilfs_super_block **sbp = nilfs->ns_sbp;
910 return scnprintf(buf, sizeof(sbp[0]->s_volume_name), "%s\n",
911 sbp[0]->s_volume_name);
914 static const char dev_readme_str[] =
915 "The <device> group contains attributes that describe file system\n"
916 "partition's details.\n\n"
917 "(1) revision\n\tshow NILFS file system revision.\n\n"
918 "(2) blocksize\n\tshow volume block size in bytes.\n\n"
919 "(3) device_size\n\tshow volume size in bytes.\n\n"
920 "(4) free_blocks\n\tshow count of free blocks on volume.\n\n"
921 "(5) uuid\n\tshow volume's UUID.\n\n"
922 "(6) volume_name\n\tshow volume's name.\n\n";
924 static ssize_t nilfs_dev_README_show(struct nilfs_dev_attr *attr,
925 struct the_nilfs *nilfs,
928 return snprintf(buf, PAGE_SIZE, dev_readme_str);
931 NILFS_DEV_RO_ATTR(revision);
932 NILFS_DEV_RO_ATTR(blocksize);
933 NILFS_DEV_RO_ATTR(device_size);
934 NILFS_DEV_RO_ATTR(free_blocks);
935 NILFS_DEV_RO_ATTR(uuid);
936 NILFS_DEV_RO_ATTR(volume_name);
937 NILFS_DEV_RO_ATTR(README);
939 static struct attribute *nilfs_dev_attrs[] = {
940 NILFS_DEV_ATTR_LIST(revision),
941 NILFS_DEV_ATTR_LIST(blocksize),
942 NILFS_DEV_ATTR_LIST(device_size),
943 NILFS_DEV_ATTR_LIST(free_blocks),
944 NILFS_DEV_ATTR_LIST(uuid),
945 NILFS_DEV_ATTR_LIST(volume_name),
946 NILFS_DEV_ATTR_LIST(README),
950 static ssize_t nilfs_dev_attr_show(struct kobject *kobj,
951 struct attribute *attr, char *buf)
953 struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
955 struct nilfs_dev_attr *a = container_of(attr, struct nilfs_dev_attr,
958 return a->show ? a->show(a, nilfs, buf) : 0;
961 static ssize_t nilfs_dev_attr_store(struct kobject *kobj,
962 struct attribute *attr,
963 const char *buf, size_t len)
965 struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
967 struct nilfs_dev_attr *a = container_of(attr, struct nilfs_dev_attr,
970 return a->store ? a->store(a, nilfs, buf, len) : 0;
973 static void nilfs_dev_attr_release(struct kobject *kobj)
975 struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
977 complete(&nilfs->ns_dev_kobj_unregister);
980 static const struct sysfs_ops nilfs_dev_attr_ops = {
981 .show = nilfs_dev_attr_show,
982 .store = nilfs_dev_attr_store,
985 static struct kobj_type nilfs_dev_ktype = {
986 .default_attrs = nilfs_dev_attrs,
987 .sysfs_ops = &nilfs_dev_attr_ops,
988 .release = nilfs_dev_attr_release,
991 int nilfs_sysfs_create_device_group(struct super_block *sb)
993 struct the_nilfs *nilfs = sb->s_fs_info;
994 size_t devgrp_size = sizeof(struct nilfs_sysfs_dev_subgroups);
997 nilfs->ns_dev_subgroups = kzalloc(devgrp_size, GFP_KERNEL);
998 if (unlikely(!nilfs->ns_dev_subgroups)) {
1000 printk(KERN_ERR "NILFS: unable to allocate memory for device group\n");
1001 goto failed_create_device_group;
1004 nilfs->ns_dev_kobj.kset = nilfs_kset;
1005 init_completion(&nilfs->ns_dev_kobj_unregister);
1006 err = kobject_init_and_add(&nilfs->ns_dev_kobj, &nilfs_dev_ktype, NULL,
1009 goto cleanup_dev_kobject;
1011 err = nilfs_sysfs_create_mounted_snapshots_group(nilfs);
1013 goto cleanup_dev_kobject;
1015 err = nilfs_sysfs_create_checkpoints_group(nilfs);
1017 goto delete_mounted_snapshots_group;
1019 err = nilfs_sysfs_create_segments_group(nilfs);
1021 goto delete_checkpoints_group;
1023 err = nilfs_sysfs_create_superblock_group(nilfs);
1025 goto delete_segments_group;
1027 err = nilfs_sysfs_create_segctor_group(nilfs);
1029 goto delete_superblock_group;
1033 delete_superblock_group:
1034 nilfs_sysfs_delete_superblock_group(nilfs);
1036 delete_segments_group:
1037 nilfs_sysfs_delete_segments_group(nilfs);
1039 delete_checkpoints_group:
1040 nilfs_sysfs_delete_checkpoints_group(nilfs);
1042 delete_mounted_snapshots_group:
1043 nilfs_sysfs_delete_mounted_snapshots_group(nilfs);
1045 cleanup_dev_kobject:
1046 kobject_put(&nilfs->ns_dev_kobj);
1047 kfree(nilfs->ns_dev_subgroups);
1049 failed_create_device_group:
1053 void nilfs_sysfs_delete_device_group(struct the_nilfs *nilfs)
1055 nilfs_sysfs_delete_mounted_snapshots_group(nilfs);
1056 nilfs_sysfs_delete_checkpoints_group(nilfs);
1057 nilfs_sysfs_delete_segments_group(nilfs);
1058 nilfs_sysfs_delete_superblock_group(nilfs);
1059 nilfs_sysfs_delete_segctor_group(nilfs);
1060 kobject_del(&nilfs->ns_dev_kobj);
1061 kobject_put(&nilfs->ns_dev_kobj);
1062 kfree(nilfs->ns_dev_subgroups);
1065 /************************************************************************
1066 * NILFS feature attrs *
1067 ************************************************************************/
1069 static ssize_t nilfs_feature_revision_show(struct kobject *kobj,
1070 struct attribute *attr, char *buf)
1072 return snprintf(buf, PAGE_SIZE, "%d.%d\n",
1073 NILFS_CURRENT_REV, NILFS_MINOR_REV);
1076 static const char features_readme_str[] =
1077 "The features group contains attributes that describe NILFS file\n"
1078 "system driver features.\n\n"
1079 "(1) revision\n\tshow current revision of NILFS file system driver.\n";
1081 static ssize_t nilfs_feature_README_show(struct kobject *kobj,
1082 struct attribute *attr,
1085 return snprintf(buf, PAGE_SIZE, features_readme_str);
1088 NILFS_FEATURE_RO_ATTR(revision);
1089 NILFS_FEATURE_RO_ATTR(README);
1091 static struct attribute *nilfs_feature_attrs[] = {
1092 NILFS_FEATURE_ATTR_LIST(revision),
1093 NILFS_FEATURE_ATTR_LIST(README),
1097 static const struct attribute_group nilfs_feature_attr_group = {
1099 .attrs = nilfs_feature_attrs,
1102 int __init nilfs_sysfs_init(void)
1106 nilfs_kset = kset_create_and_add(NILFS_ROOT_GROUP_NAME, NULL, fs_kobj);
1109 printk(KERN_ERR "NILFS: unable to create sysfs entry: err %d\n",
1111 goto failed_sysfs_init;
1114 err = sysfs_create_group(&nilfs_kset->kobj, &nilfs_feature_attr_group);
1115 if (unlikely(err)) {
1116 printk(KERN_ERR "NILFS: unable to create feature group: err %d\n",
1118 goto cleanup_sysfs_init;
1124 kset_unregister(nilfs_kset);
1130 void nilfs_sysfs_exit(void)
1132 sysfs_remove_group(&nilfs_kset->kobj, &nilfs_feature_attr_group);
1133 kset_unregister(nilfs_kset);