Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Sysfs support implementation.
0004  *
0005  * Copyright (C) 2005-2014 Nippon Telegraph and Telephone Corporation.
0006  * Copyright (C) 2014 HGST, Inc., a Western Digital Company.
0007  *
0008  * Written by Vyacheslav Dubeyko <Vyacheslav.Dubeyko@hgst.com>
0009  */
0010 
0011 #include <linux/kobject.h>
0012 
0013 #include "nilfs.h"
0014 #include "mdt.h"
0015 #include "sufile.h"
0016 #include "cpfile.h"
0017 #include "sysfs.h"
0018 
0019 /* /sys/fs/<nilfs>/ */
0020 static struct kset *nilfs_kset;
0021 
0022 #define NILFS_DEV_INT_GROUP_OPS(name, parent_name) \
0023 static ssize_t nilfs_##name##_attr_show(struct kobject *kobj, \
0024                     struct attribute *attr, char *buf) \
0025 { \
0026     struct the_nilfs *nilfs = container_of(kobj->parent, \
0027                         struct the_nilfs, \
0028                         ns_##parent_name##_kobj); \
0029     struct nilfs_##name##_attr *a = container_of(attr, \
0030                         struct nilfs_##name##_attr, \
0031                         attr); \
0032     return a->show ? a->show(a, nilfs, buf) : 0; \
0033 } \
0034 static ssize_t nilfs_##name##_attr_store(struct kobject *kobj, \
0035                      struct attribute *attr, \
0036                      const char *buf, size_t len) \
0037 { \
0038     struct the_nilfs *nilfs = container_of(kobj->parent, \
0039                         struct the_nilfs, \
0040                         ns_##parent_name##_kobj); \
0041     struct nilfs_##name##_attr *a = container_of(attr, \
0042                         struct nilfs_##name##_attr, \
0043                         attr); \
0044     return a->store ? a->store(a, nilfs, buf, len) : 0; \
0045 } \
0046 static const struct sysfs_ops nilfs_##name##_attr_ops = { \
0047     .show   = nilfs_##name##_attr_show, \
0048     .store  = nilfs_##name##_attr_store, \
0049 }
0050 
0051 #define NILFS_DEV_INT_GROUP_TYPE(name, parent_name) \
0052 static void nilfs_##name##_attr_release(struct kobject *kobj) \
0053 { \
0054     struct nilfs_sysfs_##parent_name##_subgroups *subgroups = container_of(kobj, \
0055                         struct nilfs_sysfs_##parent_name##_subgroups, \
0056                         sg_##name##_kobj); \
0057     complete(&subgroups->sg_##name##_kobj_unregister); \
0058 } \
0059 static struct kobj_type nilfs_##name##_ktype = { \
0060     .default_groups = nilfs_##name##_groups, \
0061     .sysfs_ops  = &nilfs_##name##_attr_ops, \
0062     .release    = nilfs_##name##_attr_release, \
0063 }
0064 
0065 #define NILFS_DEV_INT_GROUP_FNS(name, parent_name) \
0066 static int nilfs_sysfs_create_##name##_group(struct the_nilfs *nilfs) \
0067 { \
0068     struct kobject *parent; \
0069     struct kobject *kobj; \
0070     struct completion *kobj_unregister; \
0071     struct nilfs_sysfs_##parent_name##_subgroups *subgroups; \
0072     int err; \
0073     subgroups = nilfs->ns_##parent_name##_subgroups; \
0074     kobj = &subgroups->sg_##name##_kobj; \
0075     kobj_unregister = &subgroups->sg_##name##_kobj_unregister; \
0076     parent = &nilfs->ns_##parent_name##_kobj; \
0077     kobj->kset = nilfs_kset; \
0078     init_completion(kobj_unregister); \
0079     err = kobject_init_and_add(kobj, &nilfs_##name##_ktype, parent, \
0080                     #name); \
0081     if (err) \
0082         kobject_put(kobj); \
0083     return err; \
0084 } \
0085 static void nilfs_sysfs_delete_##name##_group(struct the_nilfs *nilfs) \
0086 { \
0087     kobject_put(&nilfs->ns_##parent_name##_subgroups->sg_##name##_kobj); \
0088 }
0089 
0090 /************************************************************************
0091  *                        NILFS snapshot attrs                          *
0092  ************************************************************************/
0093 
0094 static ssize_t
0095 nilfs_snapshot_inodes_count_show(struct nilfs_snapshot_attr *attr,
0096                  struct nilfs_root *root, char *buf)
0097 {
0098     return sysfs_emit(buf, "%llu\n",
0099             (unsigned long long)atomic64_read(&root->inodes_count));
0100 }
0101 
0102 static ssize_t
0103 nilfs_snapshot_blocks_count_show(struct nilfs_snapshot_attr *attr,
0104                  struct nilfs_root *root, char *buf)
0105 {
0106     return sysfs_emit(buf, "%llu\n",
0107             (unsigned long long)atomic64_read(&root->blocks_count));
0108 }
0109 
0110 static const char snapshot_readme_str[] =
0111     "The group contains details about mounted snapshot.\n\n"
0112     "(1) inodes_count\n\tshow number of inodes for snapshot.\n\n"
0113     "(2) blocks_count\n\tshow number of blocks for snapshot.\n\n";
0114 
0115 static ssize_t
0116 nilfs_snapshot_README_show(struct nilfs_snapshot_attr *attr,
0117                 struct nilfs_root *root, char *buf)
0118 {
0119     return sysfs_emit(buf, snapshot_readme_str);
0120 }
0121 
0122 NILFS_SNAPSHOT_RO_ATTR(inodes_count);
0123 NILFS_SNAPSHOT_RO_ATTR(blocks_count);
0124 NILFS_SNAPSHOT_RO_ATTR(README);
0125 
0126 static struct attribute *nilfs_snapshot_attrs[] = {
0127     NILFS_SNAPSHOT_ATTR_LIST(inodes_count),
0128     NILFS_SNAPSHOT_ATTR_LIST(blocks_count),
0129     NILFS_SNAPSHOT_ATTR_LIST(README),
0130     NULL,
0131 };
0132 ATTRIBUTE_GROUPS(nilfs_snapshot);
0133 
0134 static ssize_t nilfs_snapshot_attr_show(struct kobject *kobj,
0135                     struct attribute *attr, char *buf)
0136 {
0137     struct nilfs_root *root =
0138             container_of(kobj, struct nilfs_root, snapshot_kobj);
0139     struct nilfs_snapshot_attr *a =
0140             container_of(attr, struct nilfs_snapshot_attr, attr);
0141 
0142     return a->show ? a->show(a, root, buf) : 0;
0143 }
0144 
0145 static ssize_t nilfs_snapshot_attr_store(struct kobject *kobj,
0146                      struct attribute *attr,
0147                      const char *buf, size_t len)
0148 {
0149     struct nilfs_root *root =
0150             container_of(kobj, struct nilfs_root, snapshot_kobj);
0151     struct nilfs_snapshot_attr *a =
0152             container_of(attr, struct nilfs_snapshot_attr, attr);
0153 
0154     return a->store ? a->store(a, root, buf, len) : 0;
0155 }
0156 
0157 static void nilfs_snapshot_attr_release(struct kobject *kobj)
0158 {
0159     struct nilfs_root *root = container_of(kobj, struct nilfs_root,
0160                         snapshot_kobj);
0161     complete(&root->snapshot_kobj_unregister);
0162 }
0163 
0164 static const struct sysfs_ops nilfs_snapshot_attr_ops = {
0165     .show   = nilfs_snapshot_attr_show,
0166     .store  = nilfs_snapshot_attr_store,
0167 };
0168 
0169 static struct kobj_type nilfs_snapshot_ktype = {
0170     .default_groups = nilfs_snapshot_groups,
0171     .sysfs_ops  = &nilfs_snapshot_attr_ops,
0172     .release    = nilfs_snapshot_attr_release,
0173 };
0174 
0175 int nilfs_sysfs_create_snapshot_group(struct nilfs_root *root)
0176 {
0177     struct the_nilfs *nilfs;
0178     struct kobject *parent;
0179     int err;
0180 
0181     nilfs = root->nilfs;
0182     parent = &nilfs->ns_dev_subgroups->sg_mounted_snapshots_kobj;
0183     root->snapshot_kobj.kset = nilfs_kset;
0184     init_completion(&root->snapshot_kobj_unregister);
0185 
0186     if (root->cno == NILFS_CPTREE_CURRENT_CNO) {
0187         err = kobject_init_and_add(&root->snapshot_kobj,
0188                         &nilfs_snapshot_ktype,
0189                         &nilfs->ns_dev_kobj,
0190                         "current_checkpoint");
0191     } else {
0192         err = kobject_init_and_add(&root->snapshot_kobj,
0193                         &nilfs_snapshot_ktype,
0194                         parent,
0195                         "%llu", root->cno);
0196     }
0197 
0198     if (err)
0199         kobject_put(&root->snapshot_kobj);
0200 
0201     return err;
0202 }
0203 
0204 void nilfs_sysfs_delete_snapshot_group(struct nilfs_root *root)
0205 {
0206     kobject_put(&root->snapshot_kobj);
0207 }
0208 
0209 /************************************************************************
0210  *                    NILFS mounted snapshots attrs                     *
0211  ************************************************************************/
0212 
0213 static const char mounted_snapshots_readme_str[] =
0214     "The mounted_snapshots group contains group for\n"
0215     "every mounted snapshot.\n";
0216 
0217 static ssize_t
0218 nilfs_mounted_snapshots_README_show(struct nilfs_mounted_snapshots_attr *attr,
0219                     struct the_nilfs *nilfs, char *buf)
0220 {
0221     return sysfs_emit(buf, mounted_snapshots_readme_str);
0222 }
0223 
0224 NILFS_MOUNTED_SNAPSHOTS_RO_ATTR(README);
0225 
0226 static struct attribute *nilfs_mounted_snapshots_attrs[] = {
0227     NILFS_MOUNTED_SNAPSHOTS_ATTR_LIST(README),
0228     NULL,
0229 };
0230 ATTRIBUTE_GROUPS(nilfs_mounted_snapshots);
0231 
0232 NILFS_DEV_INT_GROUP_OPS(mounted_snapshots, dev);
0233 NILFS_DEV_INT_GROUP_TYPE(mounted_snapshots, dev);
0234 NILFS_DEV_INT_GROUP_FNS(mounted_snapshots, dev);
0235 
0236 /************************************************************************
0237  *                      NILFS checkpoints attrs                         *
0238  ************************************************************************/
0239 
0240 static ssize_t
0241 nilfs_checkpoints_checkpoints_number_show(struct nilfs_checkpoints_attr *attr,
0242                         struct the_nilfs *nilfs,
0243                         char *buf)
0244 {
0245     __u64 ncheckpoints;
0246     struct nilfs_cpstat cpstat;
0247     int err;
0248 
0249     down_read(&nilfs->ns_segctor_sem);
0250     err = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
0251     up_read(&nilfs->ns_segctor_sem);
0252     if (err < 0) {
0253         nilfs_err(nilfs->ns_sb, "unable to get checkpoint stat: err=%d",
0254               err);
0255         return err;
0256     }
0257 
0258     ncheckpoints = cpstat.cs_ncps;
0259 
0260     return sysfs_emit(buf, "%llu\n", ncheckpoints);
0261 }
0262 
0263 static ssize_t
0264 nilfs_checkpoints_snapshots_number_show(struct nilfs_checkpoints_attr *attr,
0265                     struct the_nilfs *nilfs,
0266                     char *buf)
0267 {
0268     __u64 nsnapshots;
0269     struct nilfs_cpstat cpstat;
0270     int err;
0271 
0272     down_read(&nilfs->ns_segctor_sem);
0273     err = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
0274     up_read(&nilfs->ns_segctor_sem);
0275     if (err < 0) {
0276         nilfs_err(nilfs->ns_sb, "unable to get checkpoint stat: err=%d",
0277               err);
0278         return err;
0279     }
0280 
0281     nsnapshots = cpstat.cs_nsss;
0282 
0283     return sysfs_emit(buf, "%llu\n", nsnapshots);
0284 }
0285 
0286 static ssize_t
0287 nilfs_checkpoints_last_seg_checkpoint_show(struct nilfs_checkpoints_attr *attr,
0288                         struct the_nilfs *nilfs,
0289                         char *buf)
0290 {
0291     __u64 last_cno;
0292 
0293     spin_lock(&nilfs->ns_last_segment_lock);
0294     last_cno = nilfs->ns_last_cno;
0295     spin_unlock(&nilfs->ns_last_segment_lock);
0296 
0297     return sysfs_emit(buf, "%llu\n", last_cno);
0298 }
0299 
0300 static ssize_t
0301 nilfs_checkpoints_next_checkpoint_show(struct nilfs_checkpoints_attr *attr,
0302                     struct the_nilfs *nilfs,
0303                     char *buf)
0304 {
0305     __u64 cno;
0306 
0307     down_read(&nilfs->ns_segctor_sem);
0308     cno = nilfs->ns_cno;
0309     up_read(&nilfs->ns_segctor_sem);
0310 
0311     return sysfs_emit(buf, "%llu\n", cno);
0312 }
0313 
0314 static const char checkpoints_readme_str[] =
0315     "The checkpoints group contains attributes that describe\n"
0316     "details about volume's checkpoints.\n\n"
0317     "(1) checkpoints_number\n\tshow number of checkpoints on volume.\n\n"
0318     "(2) snapshots_number\n\tshow number of snapshots on volume.\n\n"
0319     "(3) last_seg_checkpoint\n"
0320     "\tshow checkpoint number of the latest segment.\n\n"
0321     "(4) next_checkpoint\n\tshow next checkpoint number.\n\n";
0322 
0323 static ssize_t
0324 nilfs_checkpoints_README_show(struct nilfs_checkpoints_attr *attr,
0325                 struct the_nilfs *nilfs, char *buf)
0326 {
0327     return sysfs_emit(buf, checkpoints_readme_str);
0328 }
0329 
0330 NILFS_CHECKPOINTS_RO_ATTR(checkpoints_number);
0331 NILFS_CHECKPOINTS_RO_ATTR(snapshots_number);
0332 NILFS_CHECKPOINTS_RO_ATTR(last_seg_checkpoint);
0333 NILFS_CHECKPOINTS_RO_ATTR(next_checkpoint);
0334 NILFS_CHECKPOINTS_RO_ATTR(README);
0335 
0336 static struct attribute *nilfs_checkpoints_attrs[] = {
0337     NILFS_CHECKPOINTS_ATTR_LIST(checkpoints_number),
0338     NILFS_CHECKPOINTS_ATTR_LIST(snapshots_number),
0339     NILFS_CHECKPOINTS_ATTR_LIST(last_seg_checkpoint),
0340     NILFS_CHECKPOINTS_ATTR_LIST(next_checkpoint),
0341     NILFS_CHECKPOINTS_ATTR_LIST(README),
0342     NULL,
0343 };
0344 ATTRIBUTE_GROUPS(nilfs_checkpoints);
0345 
0346 NILFS_DEV_INT_GROUP_OPS(checkpoints, dev);
0347 NILFS_DEV_INT_GROUP_TYPE(checkpoints, dev);
0348 NILFS_DEV_INT_GROUP_FNS(checkpoints, dev);
0349 
0350 /************************************************************************
0351  *                        NILFS segments attrs                          *
0352  ************************************************************************/
0353 
0354 static ssize_t
0355 nilfs_segments_segments_number_show(struct nilfs_segments_attr *attr,
0356                      struct the_nilfs *nilfs,
0357                      char *buf)
0358 {
0359     return sysfs_emit(buf, "%lu\n", nilfs->ns_nsegments);
0360 }
0361 
0362 static ssize_t
0363 nilfs_segments_blocks_per_segment_show(struct nilfs_segments_attr *attr,
0364                     struct the_nilfs *nilfs,
0365                     char *buf)
0366 {
0367     return sysfs_emit(buf, "%lu\n", nilfs->ns_blocks_per_segment);
0368 }
0369 
0370 static ssize_t
0371 nilfs_segments_clean_segments_show(struct nilfs_segments_attr *attr,
0372                     struct the_nilfs *nilfs,
0373                     char *buf)
0374 {
0375     unsigned long ncleansegs;
0376 
0377     down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
0378     ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
0379     up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
0380 
0381     return sysfs_emit(buf, "%lu\n", ncleansegs);
0382 }
0383 
0384 static ssize_t
0385 nilfs_segments_dirty_segments_show(struct nilfs_segments_attr *attr,
0386                     struct the_nilfs *nilfs,
0387                     char *buf)
0388 {
0389     struct nilfs_sustat sustat;
0390     int err;
0391 
0392     down_read(&nilfs->ns_segctor_sem);
0393     err = nilfs_sufile_get_stat(nilfs->ns_sufile, &sustat);
0394     up_read(&nilfs->ns_segctor_sem);
0395     if (err < 0) {
0396         nilfs_err(nilfs->ns_sb, "unable to get segment stat: err=%d",
0397               err);
0398         return err;
0399     }
0400 
0401     return sysfs_emit(buf, "%llu\n", sustat.ss_ndirtysegs);
0402 }
0403 
0404 static const char segments_readme_str[] =
0405     "The segments group contains attributes that describe\n"
0406     "details about volume's segments.\n\n"
0407     "(1) segments_number\n\tshow number of segments on volume.\n\n"
0408     "(2) blocks_per_segment\n\tshow number of blocks in segment.\n\n"
0409     "(3) clean_segments\n\tshow count of clean segments.\n\n"
0410     "(4) dirty_segments\n\tshow count of dirty segments.\n\n";
0411 
0412 static ssize_t
0413 nilfs_segments_README_show(struct nilfs_segments_attr *attr,
0414                 struct the_nilfs *nilfs,
0415                 char *buf)
0416 {
0417     return sysfs_emit(buf, segments_readme_str);
0418 }
0419 
0420 NILFS_SEGMENTS_RO_ATTR(segments_number);
0421 NILFS_SEGMENTS_RO_ATTR(blocks_per_segment);
0422 NILFS_SEGMENTS_RO_ATTR(clean_segments);
0423 NILFS_SEGMENTS_RO_ATTR(dirty_segments);
0424 NILFS_SEGMENTS_RO_ATTR(README);
0425 
0426 static struct attribute *nilfs_segments_attrs[] = {
0427     NILFS_SEGMENTS_ATTR_LIST(segments_number),
0428     NILFS_SEGMENTS_ATTR_LIST(blocks_per_segment),
0429     NILFS_SEGMENTS_ATTR_LIST(clean_segments),
0430     NILFS_SEGMENTS_ATTR_LIST(dirty_segments),
0431     NILFS_SEGMENTS_ATTR_LIST(README),
0432     NULL,
0433 };
0434 ATTRIBUTE_GROUPS(nilfs_segments);
0435 
0436 NILFS_DEV_INT_GROUP_OPS(segments, dev);
0437 NILFS_DEV_INT_GROUP_TYPE(segments, dev);
0438 NILFS_DEV_INT_GROUP_FNS(segments, dev);
0439 
0440 /************************************************************************
0441  *                        NILFS segctor attrs                           *
0442  ************************************************************************/
0443 
0444 static ssize_t
0445 nilfs_segctor_last_pseg_block_show(struct nilfs_segctor_attr *attr,
0446                     struct the_nilfs *nilfs,
0447                     char *buf)
0448 {
0449     sector_t last_pseg;
0450 
0451     spin_lock(&nilfs->ns_last_segment_lock);
0452     last_pseg = nilfs->ns_last_pseg;
0453     spin_unlock(&nilfs->ns_last_segment_lock);
0454 
0455     return sysfs_emit(buf, "%llu\n",
0456             (unsigned long long)last_pseg);
0457 }
0458 
0459 static ssize_t
0460 nilfs_segctor_last_seg_sequence_show(struct nilfs_segctor_attr *attr,
0461                     struct the_nilfs *nilfs,
0462                     char *buf)
0463 {
0464     u64 last_seq;
0465 
0466     spin_lock(&nilfs->ns_last_segment_lock);
0467     last_seq = nilfs->ns_last_seq;
0468     spin_unlock(&nilfs->ns_last_segment_lock);
0469 
0470     return sysfs_emit(buf, "%llu\n", last_seq);
0471 }
0472 
0473 static ssize_t
0474 nilfs_segctor_last_seg_checkpoint_show(struct nilfs_segctor_attr *attr,
0475                     struct the_nilfs *nilfs,
0476                     char *buf)
0477 {
0478     __u64 last_cno;
0479 
0480     spin_lock(&nilfs->ns_last_segment_lock);
0481     last_cno = nilfs->ns_last_cno;
0482     spin_unlock(&nilfs->ns_last_segment_lock);
0483 
0484     return sysfs_emit(buf, "%llu\n", last_cno);
0485 }
0486 
0487 static ssize_t
0488 nilfs_segctor_current_seg_sequence_show(struct nilfs_segctor_attr *attr,
0489                     struct the_nilfs *nilfs,
0490                     char *buf)
0491 {
0492     u64 seg_seq;
0493 
0494     down_read(&nilfs->ns_segctor_sem);
0495     seg_seq = nilfs->ns_seg_seq;
0496     up_read(&nilfs->ns_segctor_sem);
0497 
0498     return sysfs_emit(buf, "%llu\n", seg_seq);
0499 }
0500 
0501 static ssize_t
0502 nilfs_segctor_current_last_full_seg_show(struct nilfs_segctor_attr *attr,
0503                      struct the_nilfs *nilfs,
0504                      char *buf)
0505 {
0506     __u64 segnum;
0507 
0508     down_read(&nilfs->ns_segctor_sem);
0509     segnum = nilfs->ns_segnum;
0510     up_read(&nilfs->ns_segctor_sem);
0511 
0512     return sysfs_emit(buf, "%llu\n", segnum);
0513 }
0514 
0515 static ssize_t
0516 nilfs_segctor_next_full_seg_show(struct nilfs_segctor_attr *attr,
0517                  struct the_nilfs *nilfs,
0518                  char *buf)
0519 {
0520     __u64 nextnum;
0521 
0522     down_read(&nilfs->ns_segctor_sem);
0523     nextnum = nilfs->ns_nextnum;
0524     up_read(&nilfs->ns_segctor_sem);
0525 
0526     return sysfs_emit(buf, "%llu\n", nextnum);
0527 }
0528 
0529 static ssize_t
0530 nilfs_segctor_next_pseg_offset_show(struct nilfs_segctor_attr *attr,
0531                     struct the_nilfs *nilfs,
0532                     char *buf)
0533 {
0534     unsigned long pseg_offset;
0535 
0536     down_read(&nilfs->ns_segctor_sem);
0537     pseg_offset = nilfs->ns_pseg_offset;
0538     up_read(&nilfs->ns_segctor_sem);
0539 
0540     return sysfs_emit(buf, "%lu\n", pseg_offset);
0541 }
0542 
0543 static ssize_t
0544 nilfs_segctor_next_checkpoint_show(struct nilfs_segctor_attr *attr,
0545                     struct the_nilfs *nilfs,
0546                     char *buf)
0547 {
0548     __u64 cno;
0549 
0550     down_read(&nilfs->ns_segctor_sem);
0551     cno = nilfs->ns_cno;
0552     up_read(&nilfs->ns_segctor_sem);
0553 
0554     return sysfs_emit(buf, "%llu\n", cno);
0555 }
0556 
0557 static ssize_t
0558 nilfs_segctor_last_seg_write_time_show(struct nilfs_segctor_attr *attr,
0559                     struct the_nilfs *nilfs,
0560                     char *buf)
0561 {
0562     time64_t ctime;
0563 
0564     down_read(&nilfs->ns_segctor_sem);
0565     ctime = nilfs->ns_ctime;
0566     up_read(&nilfs->ns_segctor_sem);
0567 
0568     return sysfs_emit(buf, "%ptTs\n", &ctime);
0569 }
0570 
0571 static ssize_t
0572 nilfs_segctor_last_seg_write_time_secs_show(struct nilfs_segctor_attr *attr,
0573                         struct the_nilfs *nilfs,
0574                         char *buf)
0575 {
0576     time64_t ctime;
0577 
0578     down_read(&nilfs->ns_segctor_sem);
0579     ctime = nilfs->ns_ctime;
0580     up_read(&nilfs->ns_segctor_sem);
0581 
0582     return sysfs_emit(buf, "%llu\n", ctime);
0583 }
0584 
0585 static ssize_t
0586 nilfs_segctor_last_nongc_write_time_show(struct nilfs_segctor_attr *attr,
0587                      struct the_nilfs *nilfs,
0588                      char *buf)
0589 {
0590     time64_t nongc_ctime;
0591 
0592     down_read(&nilfs->ns_segctor_sem);
0593     nongc_ctime = nilfs->ns_nongc_ctime;
0594     up_read(&nilfs->ns_segctor_sem);
0595 
0596     return sysfs_emit(buf, "%ptTs\n", &nongc_ctime);
0597 }
0598 
0599 static ssize_t
0600 nilfs_segctor_last_nongc_write_time_secs_show(struct nilfs_segctor_attr *attr,
0601                         struct the_nilfs *nilfs,
0602                         char *buf)
0603 {
0604     time64_t nongc_ctime;
0605 
0606     down_read(&nilfs->ns_segctor_sem);
0607     nongc_ctime = nilfs->ns_nongc_ctime;
0608     up_read(&nilfs->ns_segctor_sem);
0609 
0610     return sysfs_emit(buf, "%llu\n", nongc_ctime);
0611 }
0612 
0613 static ssize_t
0614 nilfs_segctor_dirty_data_blocks_count_show(struct nilfs_segctor_attr *attr,
0615                         struct the_nilfs *nilfs,
0616                         char *buf)
0617 {
0618     u32 ndirtyblks;
0619 
0620     down_read(&nilfs->ns_segctor_sem);
0621     ndirtyblks = atomic_read(&nilfs->ns_ndirtyblks);
0622     up_read(&nilfs->ns_segctor_sem);
0623 
0624     return sysfs_emit(buf, "%u\n", ndirtyblks);
0625 }
0626 
0627 static const char segctor_readme_str[] =
0628     "The segctor group contains attributes that describe\n"
0629     "segctor thread activity details.\n\n"
0630     "(1) last_pseg_block\n"
0631     "\tshow start block number of the latest segment.\n\n"
0632     "(2) last_seg_sequence\n"
0633     "\tshow sequence value of the latest segment.\n\n"
0634     "(3) last_seg_checkpoint\n"
0635     "\tshow checkpoint number of the latest segment.\n\n"
0636     "(4) current_seg_sequence\n\tshow segment sequence counter.\n\n"
0637     "(5) current_last_full_seg\n"
0638     "\tshow index number of the latest full segment.\n\n"
0639     "(6) next_full_seg\n"
0640     "\tshow index number of the full segment index to be used next.\n\n"
0641     "(7) next_pseg_offset\n"
0642     "\tshow offset of next partial segment in the current full segment.\n\n"
0643     "(8) next_checkpoint\n\tshow next checkpoint number.\n\n"
0644     "(9) last_seg_write_time\n"
0645     "\tshow write time of the last segment in human-readable format.\n\n"
0646     "(10) last_seg_write_time_secs\n"
0647     "\tshow write time of the last segment in seconds.\n\n"
0648     "(11) last_nongc_write_time\n"
0649     "\tshow write time of the last segment not for cleaner operation "
0650     "in human-readable format.\n\n"
0651     "(12) last_nongc_write_time_secs\n"
0652     "\tshow write time of the last segment not for cleaner operation "
0653     "in seconds.\n\n"
0654     "(13) dirty_data_blocks_count\n"
0655     "\tshow number of dirty data blocks.\n\n";
0656 
0657 static ssize_t
0658 nilfs_segctor_README_show(struct nilfs_segctor_attr *attr,
0659               struct the_nilfs *nilfs, char *buf)
0660 {
0661     return sysfs_emit(buf, segctor_readme_str);
0662 }
0663 
0664 NILFS_SEGCTOR_RO_ATTR(last_pseg_block);
0665 NILFS_SEGCTOR_RO_ATTR(last_seg_sequence);
0666 NILFS_SEGCTOR_RO_ATTR(last_seg_checkpoint);
0667 NILFS_SEGCTOR_RO_ATTR(current_seg_sequence);
0668 NILFS_SEGCTOR_RO_ATTR(current_last_full_seg);
0669 NILFS_SEGCTOR_RO_ATTR(next_full_seg);
0670 NILFS_SEGCTOR_RO_ATTR(next_pseg_offset);
0671 NILFS_SEGCTOR_RO_ATTR(next_checkpoint);
0672 NILFS_SEGCTOR_RO_ATTR(last_seg_write_time);
0673 NILFS_SEGCTOR_RO_ATTR(last_seg_write_time_secs);
0674 NILFS_SEGCTOR_RO_ATTR(last_nongc_write_time);
0675 NILFS_SEGCTOR_RO_ATTR(last_nongc_write_time_secs);
0676 NILFS_SEGCTOR_RO_ATTR(dirty_data_blocks_count);
0677 NILFS_SEGCTOR_RO_ATTR(README);
0678 
0679 static struct attribute *nilfs_segctor_attrs[] = {
0680     NILFS_SEGCTOR_ATTR_LIST(last_pseg_block),
0681     NILFS_SEGCTOR_ATTR_LIST(last_seg_sequence),
0682     NILFS_SEGCTOR_ATTR_LIST(last_seg_checkpoint),
0683     NILFS_SEGCTOR_ATTR_LIST(current_seg_sequence),
0684     NILFS_SEGCTOR_ATTR_LIST(current_last_full_seg),
0685     NILFS_SEGCTOR_ATTR_LIST(next_full_seg),
0686     NILFS_SEGCTOR_ATTR_LIST(next_pseg_offset),
0687     NILFS_SEGCTOR_ATTR_LIST(next_checkpoint),
0688     NILFS_SEGCTOR_ATTR_LIST(last_seg_write_time),
0689     NILFS_SEGCTOR_ATTR_LIST(last_seg_write_time_secs),
0690     NILFS_SEGCTOR_ATTR_LIST(last_nongc_write_time),
0691     NILFS_SEGCTOR_ATTR_LIST(last_nongc_write_time_secs),
0692     NILFS_SEGCTOR_ATTR_LIST(dirty_data_blocks_count),
0693     NILFS_SEGCTOR_ATTR_LIST(README),
0694     NULL,
0695 };
0696 ATTRIBUTE_GROUPS(nilfs_segctor);
0697 
0698 NILFS_DEV_INT_GROUP_OPS(segctor, dev);
0699 NILFS_DEV_INT_GROUP_TYPE(segctor, dev);
0700 NILFS_DEV_INT_GROUP_FNS(segctor, dev);
0701 
0702 /************************************************************************
0703  *                        NILFS superblock attrs                        *
0704  ************************************************************************/
0705 
0706 static ssize_t
0707 nilfs_superblock_sb_write_time_show(struct nilfs_superblock_attr *attr,
0708                      struct the_nilfs *nilfs,
0709                      char *buf)
0710 {
0711     time64_t sbwtime;
0712 
0713     down_read(&nilfs->ns_sem);
0714     sbwtime = nilfs->ns_sbwtime;
0715     up_read(&nilfs->ns_sem);
0716 
0717     return sysfs_emit(buf, "%ptTs\n", &sbwtime);
0718 }
0719 
0720 static ssize_t
0721 nilfs_superblock_sb_write_time_secs_show(struct nilfs_superblock_attr *attr,
0722                      struct the_nilfs *nilfs,
0723                      char *buf)
0724 {
0725     time64_t sbwtime;
0726 
0727     down_read(&nilfs->ns_sem);
0728     sbwtime = nilfs->ns_sbwtime;
0729     up_read(&nilfs->ns_sem);
0730 
0731     return sysfs_emit(buf, "%llu\n", sbwtime);
0732 }
0733 
0734 static ssize_t
0735 nilfs_superblock_sb_write_count_show(struct nilfs_superblock_attr *attr,
0736                       struct the_nilfs *nilfs,
0737                       char *buf)
0738 {
0739     unsigned int sbwcount;
0740 
0741     down_read(&nilfs->ns_sem);
0742     sbwcount = nilfs->ns_sbwcount;
0743     up_read(&nilfs->ns_sem);
0744 
0745     return sysfs_emit(buf, "%u\n", sbwcount);
0746 }
0747 
0748 static ssize_t
0749 nilfs_superblock_sb_update_frequency_show(struct nilfs_superblock_attr *attr,
0750                         struct the_nilfs *nilfs,
0751                         char *buf)
0752 {
0753     unsigned int sb_update_freq;
0754 
0755     down_read(&nilfs->ns_sem);
0756     sb_update_freq = nilfs->ns_sb_update_freq;
0757     up_read(&nilfs->ns_sem);
0758 
0759     return sysfs_emit(buf, "%u\n", sb_update_freq);
0760 }
0761 
0762 static ssize_t
0763 nilfs_superblock_sb_update_frequency_store(struct nilfs_superblock_attr *attr,
0764                         struct the_nilfs *nilfs,
0765                         const char *buf, size_t count)
0766 {
0767     unsigned int val;
0768     int err;
0769 
0770     err = kstrtouint(skip_spaces(buf), 0, &val);
0771     if (err) {
0772         nilfs_err(nilfs->ns_sb, "unable to convert string: err=%d",
0773               err);
0774         return err;
0775     }
0776 
0777     if (val < NILFS_SB_FREQ) {
0778         val = NILFS_SB_FREQ;
0779         nilfs_warn(nilfs->ns_sb,
0780                "superblock update frequency cannot be lesser than 10 seconds");
0781     }
0782 
0783     down_write(&nilfs->ns_sem);
0784     nilfs->ns_sb_update_freq = val;
0785     up_write(&nilfs->ns_sem);
0786 
0787     return count;
0788 }
0789 
0790 static const char sb_readme_str[] =
0791     "The superblock group contains attributes that describe\n"
0792     "superblock's details.\n\n"
0793     "(1) sb_write_time\n\tshow previous write time of super block "
0794     "in human-readable format.\n\n"
0795     "(2) sb_write_time_secs\n\tshow previous write time of super block "
0796     "in seconds.\n\n"
0797     "(3) sb_write_count\n\tshow write count of super block.\n\n"
0798     "(4) sb_update_frequency\n"
0799     "\tshow/set interval of periodical update of superblock (in seconds).\n\n"
0800     "\tYou can set preferable frequency of superblock update by command:\n\n"
0801     "\t'echo <val> > /sys/fs/<nilfs>/<dev>/superblock/sb_update_frequency'\n";
0802 
0803 static ssize_t
0804 nilfs_superblock_README_show(struct nilfs_superblock_attr *attr,
0805                 struct the_nilfs *nilfs, char *buf)
0806 {
0807     return sysfs_emit(buf, sb_readme_str);
0808 }
0809 
0810 NILFS_SUPERBLOCK_RO_ATTR(sb_write_time);
0811 NILFS_SUPERBLOCK_RO_ATTR(sb_write_time_secs);
0812 NILFS_SUPERBLOCK_RO_ATTR(sb_write_count);
0813 NILFS_SUPERBLOCK_RW_ATTR(sb_update_frequency);
0814 NILFS_SUPERBLOCK_RO_ATTR(README);
0815 
0816 static struct attribute *nilfs_superblock_attrs[] = {
0817     NILFS_SUPERBLOCK_ATTR_LIST(sb_write_time),
0818     NILFS_SUPERBLOCK_ATTR_LIST(sb_write_time_secs),
0819     NILFS_SUPERBLOCK_ATTR_LIST(sb_write_count),
0820     NILFS_SUPERBLOCK_ATTR_LIST(sb_update_frequency),
0821     NILFS_SUPERBLOCK_ATTR_LIST(README),
0822     NULL,
0823 };
0824 ATTRIBUTE_GROUPS(nilfs_superblock);
0825 
0826 NILFS_DEV_INT_GROUP_OPS(superblock, dev);
0827 NILFS_DEV_INT_GROUP_TYPE(superblock, dev);
0828 NILFS_DEV_INT_GROUP_FNS(superblock, dev);
0829 
0830 /************************************************************************
0831  *                        NILFS device attrs                            *
0832  ************************************************************************/
0833 
0834 static
0835 ssize_t nilfs_dev_revision_show(struct nilfs_dev_attr *attr,
0836                 struct the_nilfs *nilfs,
0837                 char *buf)
0838 {
0839     struct nilfs_super_block **sbp = nilfs->ns_sbp;
0840     u32 major = le32_to_cpu(sbp[0]->s_rev_level);
0841     u16 minor = le16_to_cpu(sbp[0]->s_minor_rev_level);
0842 
0843     return sysfs_emit(buf, "%d.%d\n", major, minor);
0844 }
0845 
0846 static
0847 ssize_t nilfs_dev_blocksize_show(struct nilfs_dev_attr *attr,
0848                  struct the_nilfs *nilfs,
0849                  char *buf)
0850 {
0851     return sysfs_emit(buf, "%u\n", nilfs->ns_blocksize);
0852 }
0853 
0854 static
0855 ssize_t nilfs_dev_device_size_show(struct nilfs_dev_attr *attr,
0856                     struct the_nilfs *nilfs,
0857                     char *buf)
0858 {
0859     struct nilfs_super_block **sbp = nilfs->ns_sbp;
0860     u64 dev_size = le64_to_cpu(sbp[0]->s_dev_size);
0861 
0862     return sysfs_emit(buf, "%llu\n", dev_size);
0863 }
0864 
0865 static
0866 ssize_t nilfs_dev_free_blocks_show(struct nilfs_dev_attr *attr,
0867                    struct the_nilfs *nilfs,
0868                    char *buf)
0869 {
0870     sector_t free_blocks = 0;
0871 
0872     nilfs_count_free_blocks(nilfs, &free_blocks);
0873     return sysfs_emit(buf, "%llu\n",
0874             (unsigned long long)free_blocks);
0875 }
0876 
0877 static
0878 ssize_t nilfs_dev_uuid_show(struct nilfs_dev_attr *attr,
0879                 struct the_nilfs *nilfs,
0880                 char *buf)
0881 {
0882     struct nilfs_super_block **sbp = nilfs->ns_sbp;
0883 
0884     return sysfs_emit(buf, "%pUb\n", sbp[0]->s_uuid);
0885 }
0886 
0887 static
0888 ssize_t nilfs_dev_volume_name_show(struct nilfs_dev_attr *attr,
0889                     struct the_nilfs *nilfs,
0890                     char *buf)
0891 {
0892     struct nilfs_super_block **sbp = nilfs->ns_sbp;
0893 
0894     return scnprintf(buf, sizeof(sbp[0]->s_volume_name), "%s\n",
0895              sbp[0]->s_volume_name);
0896 }
0897 
0898 static const char dev_readme_str[] =
0899     "The <device> group contains attributes that describe file system\n"
0900     "partition's details.\n\n"
0901     "(1) revision\n\tshow NILFS file system revision.\n\n"
0902     "(2) blocksize\n\tshow volume block size in bytes.\n\n"
0903     "(3) device_size\n\tshow volume size in bytes.\n\n"
0904     "(4) free_blocks\n\tshow count of free blocks on volume.\n\n"
0905     "(5) uuid\n\tshow volume's UUID.\n\n"
0906     "(6) volume_name\n\tshow volume's name.\n\n";
0907 
0908 static ssize_t nilfs_dev_README_show(struct nilfs_dev_attr *attr,
0909                      struct the_nilfs *nilfs,
0910                      char *buf)
0911 {
0912     return sysfs_emit(buf, dev_readme_str);
0913 }
0914 
0915 NILFS_DEV_RO_ATTR(revision);
0916 NILFS_DEV_RO_ATTR(blocksize);
0917 NILFS_DEV_RO_ATTR(device_size);
0918 NILFS_DEV_RO_ATTR(free_blocks);
0919 NILFS_DEV_RO_ATTR(uuid);
0920 NILFS_DEV_RO_ATTR(volume_name);
0921 NILFS_DEV_RO_ATTR(README);
0922 
0923 static struct attribute *nilfs_dev_attrs[] = {
0924     NILFS_DEV_ATTR_LIST(revision),
0925     NILFS_DEV_ATTR_LIST(blocksize),
0926     NILFS_DEV_ATTR_LIST(device_size),
0927     NILFS_DEV_ATTR_LIST(free_blocks),
0928     NILFS_DEV_ATTR_LIST(uuid),
0929     NILFS_DEV_ATTR_LIST(volume_name),
0930     NILFS_DEV_ATTR_LIST(README),
0931     NULL,
0932 };
0933 ATTRIBUTE_GROUPS(nilfs_dev);
0934 
0935 static ssize_t nilfs_dev_attr_show(struct kobject *kobj,
0936                     struct attribute *attr, char *buf)
0937 {
0938     struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
0939                         ns_dev_kobj);
0940     struct nilfs_dev_attr *a = container_of(attr, struct nilfs_dev_attr,
0941                         attr);
0942 
0943     return a->show ? a->show(a, nilfs, buf) : 0;
0944 }
0945 
0946 static ssize_t nilfs_dev_attr_store(struct kobject *kobj,
0947                     struct attribute *attr,
0948                     const char *buf, size_t len)
0949 {
0950     struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
0951                         ns_dev_kobj);
0952     struct nilfs_dev_attr *a = container_of(attr, struct nilfs_dev_attr,
0953                         attr);
0954 
0955     return a->store ? a->store(a, nilfs, buf, len) : 0;
0956 }
0957 
0958 static void nilfs_dev_attr_release(struct kobject *kobj)
0959 {
0960     struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
0961                         ns_dev_kobj);
0962     complete(&nilfs->ns_dev_kobj_unregister);
0963 }
0964 
0965 static const struct sysfs_ops nilfs_dev_attr_ops = {
0966     .show   = nilfs_dev_attr_show,
0967     .store  = nilfs_dev_attr_store,
0968 };
0969 
0970 static struct kobj_type nilfs_dev_ktype = {
0971     .default_groups = nilfs_dev_groups,
0972     .sysfs_ops  = &nilfs_dev_attr_ops,
0973     .release    = nilfs_dev_attr_release,
0974 };
0975 
0976 int nilfs_sysfs_create_device_group(struct super_block *sb)
0977 {
0978     struct the_nilfs *nilfs = sb->s_fs_info;
0979     size_t devgrp_size = sizeof(struct nilfs_sysfs_dev_subgroups);
0980     int err;
0981 
0982     nilfs->ns_dev_subgroups = kzalloc(devgrp_size, GFP_KERNEL);
0983     if (unlikely(!nilfs->ns_dev_subgroups)) {
0984         err = -ENOMEM;
0985         nilfs_err(sb, "unable to allocate memory for device group");
0986         goto failed_create_device_group;
0987     }
0988 
0989     nilfs->ns_dev_kobj.kset = nilfs_kset;
0990     init_completion(&nilfs->ns_dev_kobj_unregister);
0991     err = kobject_init_and_add(&nilfs->ns_dev_kobj, &nilfs_dev_ktype, NULL,
0992                     "%s", sb->s_id);
0993     if (err)
0994         goto cleanup_dev_kobject;
0995 
0996     err = nilfs_sysfs_create_mounted_snapshots_group(nilfs);
0997     if (err)
0998         goto cleanup_dev_kobject;
0999 
1000     err = nilfs_sysfs_create_checkpoints_group(nilfs);
1001     if (err)
1002         goto delete_mounted_snapshots_group;
1003 
1004     err = nilfs_sysfs_create_segments_group(nilfs);
1005     if (err)
1006         goto delete_checkpoints_group;
1007 
1008     err = nilfs_sysfs_create_superblock_group(nilfs);
1009     if (err)
1010         goto delete_segments_group;
1011 
1012     err = nilfs_sysfs_create_segctor_group(nilfs);
1013     if (err)
1014         goto delete_superblock_group;
1015 
1016     return 0;
1017 
1018 delete_superblock_group:
1019     nilfs_sysfs_delete_superblock_group(nilfs);
1020 
1021 delete_segments_group:
1022     nilfs_sysfs_delete_segments_group(nilfs);
1023 
1024 delete_checkpoints_group:
1025     nilfs_sysfs_delete_checkpoints_group(nilfs);
1026 
1027 delete_mounted_snapshots_group:
1028     nilfs_sysfs_delete_mounted_snapshots_group(nilfs);
1029 
1030 cleanup_dev_kobject:
1031     kobject_put(&nilfs->ns_dev_kobj);
1032     kfree(nilfs->ns_dev_subgroups);
1033 
1034 failed_create_device_group:
1035     return err;
1036 }
1037 
1038 void nilfs_sysfs_delete_device_group(struct the_nilfs *nilfs)
1039 {
1040     nilfs_sysfs_delete_mounted_snapshots_group(nilfs);
1041     nilfs_sysfs_delete_checkpoints_group(nilfs);
1042     nilfs_sysfs_delete_segments_group(nilfs);
1043     nilfs_sysfs_delete_superblock_group(nilfs);
1044     nilfs_sysfs_delete_segctor_group(nilfs);
1045     kobject_del(&nilfs->ns_dev_kobj);
1046     kobject_put(&nilfs->ns_dev_kobj);
1047     kfree(nilfs->ns_dev_subgroups);
1048 }
1049 
1050 /************************************************************************
1051  *                        NILFS feature attrs                           *
1052  ************************************************************************/
1053 
1054 static ssize_t nilfs_feature_revision_show(struct kobject *kobj,
1055                         struct attribute *attr, char *buf)
1056 {
1057     return sysfs_emit(buf, "%d.%d\n",
1058             NILFS_CURRENT_REV, NILFS_MINOR_REV);
1059 }
1060 
1061 static const char features_readme_str[] =
1062     "The features group contains attributes that describe NILFS file\n"
1063     "system driver features.\n\n"
1064     "(1) revision\n\tshow current revision of NILFS file system driver.\n";
1065 
1066 static ssize_t nilfs_feature_README_show(struct kobject *kobj,
1067                      struct attribute *attr,
1068                      char *buf)
1069 {
1070     return sysfs_emit(buf, features_readme_str);
1071 }
1072 
1073 NILFS_FEATURE_RO_ATTR(revision);
1074 NILFS_FEATURE_RO_ATTR(README);
1075 
1076 static struct attribute *nilfs_feature_attrs[] = {
1077     NILFS_FEATURE_ATTR_LIST(revision),
1078     NILFS_FEATURE_ATTR_LIST(README),
1079     NULL,
1080 };
1081 
1082 static const struct attribute_group nilfs_feature_attr_group = {
1083     .name = "features",
1084     .attrs = nilfs_feature_attrs,
1085 };
1086 
1087 int __init nilfs_sysfs_init(void)
1088 {
1089     int err;
1090 
1091     nilfs_kset = kset_create_and_add(NILFS_ROOT_GROUP_NAME, NULL, fs_kobj);
1092     if (!nilfs_kset) {
1093         err = -ENOMEM;
1094         nilfs_err(NULL, "unable to create sysfs entry: err=%d", err);
1095         goto failed_sysfs_init;
1096     }
1097 
1098     err = sysfs_create_group(&nilfs_kset->kobj, &nilfs_feature_attr_group);
1099     if (unlikely(err)) {
1100         nilfs_err(NULL, "unable to create feature group: err=%d", err);
1101         goto cleanup_sysfs_init;
1102     }
1103 
1104     return 0;
1105 
1106 cleanup_sysfs_init:
1107     kset_unregister(nilfs_kset);
1108 
1109 failed_sysfs_init:
1110     return err;
1111 }
1112 
1113 void nilfs_sysfs_exit(void)
1114 {
1115     sysfs_remove_group(&nilfs_kset->kobj, &nilfs_feature_attr_group);
1116     kset_unregister(nilfs_kset);
1117 }