Vyacheslav Dubeyko | aebe17f | 2014-08-08 14:20:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * sysfs.c - sysfs support implementation. |
| 3 | * |
| 4 | * Copyright (C) 2005-2014 Nippon Telegraph and Telephone Corporation. |
| 5 | * Copyright (C) 2014 HGST, Inc., a Western Digital Company. |
| 6 | * |
| 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. |
| 11 | * |
| 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. |
| 16 | * |
| 17 | * Written by Vyacheslav Dubeyko <Vyacheslav.Dubeyko@hgst.com> |
| 18 | */ |
| 19 | |
| 20 | #include <linux/kobject.h> |
| 21 | |
| 22 | #include "nilfs.h" |
| 23 | #include "mdt.h" |
| 24 | #include "sufile.h" |
| 25 | #include "cpfile.h" |
| 26 | #include "sysfs.h" |
| 27 | |
| 28 | /* /sys/fs/<nilfs>/ */ |
| 29 | static struct kset *nilfs_kset; |
| 30 | |
| 31 | #define NILFS_SHOW_TIME(time_t_val, buf) ({ \ |
| 32 | struct tm res; \ |
| 33 | int count = 0; \ |
| 34 | time_to_tm(time_t_val, 0, &res); \ |
| 35 | res.tm_year += 1900; \ |
| 36 | res.tm_mon += 1; \ |
| 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);\ |
| 41 | count; \ |
| 42 | }) |
| 43 | |
| 44 | /************************************************************************ |
Vyacheslav Dubeyko | da7141f | 2014-08-08 14:20:39 -0700 | [diff] [blame^] | 45 | * NILFS device attrs * |
| 46 | ************************************************************************/ |
| 47 | |
| 48 | static |
| 49 | ssize_t nilfs_dev_revision_show(struct nilfs_dev_attr *attr, |
| 50 | struct the_nilfs *nilfs, |
| 51 | char *buf) |
| 52 | { |
| 53 | struct nilfs_super_block **sbp = nilfs->ns_sbp; |
| 54 | u32 major = le32_to_cpu(sbp[0]->s_rev_level); |
| 55 | u16 minor = le16_to_cpu(sbp[0]->s_minor_rev_level); |
| 56 | |
| 57 | return snprintf(buf, PAGE_SIZE, "%d.%d\n", major, minor); |
| 58 | } |
| 59 | |
| 60 | static |
| 61 | ssize_t nilfs_dev_blocksize_show(struct nilfs_dev_attr *attr, |
| 62 | struct the_nilfs *nilfs, |
| 63 | char *buf) |
| 64 | { |
| 65 | return snprintf(buf, PAGE_SIZE, "%u\n", nilfs->ns_blocksize); |
| 66 | } |
| 67 | |
| 68 | static |
| 69 | ssize_t nilfs_dev_device_size_show(struct nilfs_dev_attr *attr, |
| 70 | struct the_nilfs *nilfs, |
| 71 | char *buf) |
| 72 | { |
| 73 | struct nilfs_super_block **sbp = nilfs->ns_sbp; |
| 74 | u64 dev_size = le64_to_cpu(sbp[0]->s_dev_size); |
| 75 | |
| 76 | return snprintf(buf, PAGE_SIZE, "%llu\n", dev_size); |
| 77 | } |
| 78 | |
| 79 | static |
| 80 | ssize_t nilfs_dev_free_blocks_show(struct nilfs_dev_attr *attr, |
| 81 | struct the_nilfs *nilfs, |
| 82 | char *buf) |
| 83 | { |
| 84 | sector_t free_blocks = 0; |
| 85 | |
| 86 | nilfs_count_free_blocks(nilfs, &free_blocks); |
| 87 | return snprintf(buf, PAGE_SIZE, "%llu\n", |
| 88 | (unsigned long long)free_blocks); |
| 89 | } |
| 90 | |
| 91 | static |
| 92 | ssize_t nilfs_dev_uuid_show(struct nilfs_dev_attr *attr, |
| 93 | struct the_nilfs *nilfs, |
| 94 | char *buf) |
| 95 | { |
| 96 | struct nilfs_super_block **sbp = nilfs->ns_sbp; |
| 97 | |
| 98 | return snprintf(buf, PAGE_SIZE, "%pUb\n", sbp[0]->s_uuid); |
| 99 | } |
| 100 | |
| 101 | static |
| 102 | ssize_t nilfs_dev_volume_name_show(struct nilfs_dev_attr *attr, |
| 103 | struct the_nilfs *nilfs, |
| 104 | char *buf) |
| 105 | { |
| 106 | struct nilfs_super_block **sbp = nilfs->ns_sbp; |
| 107 | |
| 108 | return scnprintf(buf, sizeof(sbp[0]->s_volume_name), "%s\n", |
| 109 | sbp[0]->s_volume_name); |
| 110 | } |
| 111 | |
| 112 | static const char dev_readme_str[] = |
| 113 | "The <device> group contains attributes that describe file system\n" |
| 114 | "partition's details.\n\n" |
| 115 | "(1) revision\n\tshow NILFS file system revision.\n\n" |
| 116 | "(2) blocksize\n\tshow volume block size in bytes.\n\n" |
| 117 | "(3) device_size\n\tshow volume size in bytes.\n\n" |
| 118 | "(4) free_blocks\n\tshow count of free blocks on volume.\n\n" |
| 119 | "(5) uuid\n\tshow volume's UUID.\n\n" |
| 120 | "(6) volume_name\n\tshow volume's name.\n\n"; |
| 121 | |
| 122 | static ssize_t nilfs_dev_README_show(struct nilfs_dev_attr *attr, |
| 123 | struct the_nilfs *nilfs, |
| 124 | char *buf) |
| 125 | { |
| 126 | return snprintf(buf, PAGE_SIZE, dev_readme_str); |
| 127 | } |
| 128 | |
| 129 | NILFS_DEV_RO_ATTR(revision); |
| 130 | NILFS_DEV_RO_ATTR(blocksize); |
| 131 | NILFS_DEV_RO_ATTR(device_size); |
| 132 | NILFS_DEV_RO_ATTR(free_blocks); |
| 133 | NILFS_DEV_RO_ATTR(uuid); |
| 134 | NILFS_DEV_RO_ATTR(volume_name); |
| 135 | NILFS_DEV_RO_ATTR(README); |
| 136 | |
| 137 | static struct attribute *nilfs_dev_attrs[] = { |
| 138 | NILFS_DEV_ATTR_LIST(revision), |
| 139 | NILFS_DEV_ATTR_LIST(blocksize), |
| 140 | NILFS_DEV_ATTR_LIST(device_size), |
| 141 | NILFS_DEV_ATTR_LIST(free_blocks), |
| 142 | NILFS_DEV_ATTR_LIST(uuid), |
| 143 | NILFS_DEV_ATTR_LIST(volume_name), |
| 144 | NILFS_DEV_ATTR_LIST(README), |
| 145 | NULL, |
| 146 | }; |
| 147 | |
| 148 | static ssize_t nilfs_dev_attr_show(struct kobject *kobj, |
| 149 | struct attribute *attr, char *buf) |
| 150 | { |
| 151 | struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs, |
| 152 | ns_dev_kobj); |
| 153 | struct nilfs_dev_attr *a = container_of(attr, struct nilfs_dev_attr, |
| 154 | attr); |
| 155 | |
| 156 | return a->show ? a->show(a, nilfs, buf) : 0; |
| 157 | } |
| 158 | |
| 159 | static ssize_t nilfs_dev_attr_store(struct kobject *kobj, |
| 160 | struct attribute *attr, |
| 161 | const char *buf, size_t len) |
| 162 | { |
| 163 | struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs, |
| 164 | ns_dev_kobj); |
| 165 | struct nilfs_dev_attr *a = container_of(attr, struct nilfs_dev_attr, |
| 166 | attr); |
| 167 | |
| 168 | return a->store ? a->store(a, nilfs, buf, len) : 0; |
| 169 | } |
| 170 | |
| 171 | static void nilfs_dev_attr_release(struct kobject *kobj) |
| 172 | { |
| 173 | struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs, |
| 174 | ns_dev_kobj); |
| 175 | complete(&nilfs->ns_dev_kobj_unregister); |
| 176 | } |
| 177 | |
| 178 | static const struct sysfs_ops nilfs_dev_attr_ops = { |
| 179 | .show = nilfs_dev_attr_show, |
| 180 | .store = nilfs_dev_attr_store, |
| 181 | }; |
| 182 | |
| 183 | static struct kobj_type nilfs_dev_ktype = { |
| 184 | .default_attrs = nilfs_dev_attrs, |
| 185 | .sysfs_ops = &nilfs_dev_attr_ops, |
| 186 | .release = nilfs_dev_attr_release, |
| 187 | }; |
| 188 | |
| 189 | int nilfs_sysfs_create_device_group(struct super_block *sb) |
| 190 | { |
| 191 | struct the_nilfs *nilfs = sb->s_fs_info; |
| 192 | int err; |
| 193 | |
| 194 | nilfs->ns_dev_kobj.kset = nilfs_kset; |
| 195 | init_completion(&nilfs->ns_dev_kobj_unregister); |
| 196 | err = kobject_init_and_add(&nilfs->ns_dev_kobj, &nilfs_dev_ktype, NULL, |
| 197 | "%s", sb->s_id); |
| 198 | if (err) |
| 199 | goto failed_create_device_group; |
| 200 | |
| 201 | return 0; |
| 202 | |
| 203 | failed_create_device_group: |
| 204 | return err; |
| 205 | } |
| 206 | |
| 207 | void nilfs_sysfs_delete_device_group(struct the_nilfs *nilfs) |
| 208 | { |
| 209 | kobject_del(&nilfs->ns_dev_kobj); |
| 210 | } |
| 211 | |
| 212 | /************************************************************************ |
Vyacheslav Dubeyko | aebe17f | 2014-08-08 14:20:37 -0700 | [diff] [blame] | 213 | * NILFS feature attrs * |
| 214 | ************************************************************************/ |
| 215 | |
| 216 | static ssize_t nilfs_feature_revision_show(struct kobject *kobj, |
| 217 | struct attribute *attr, char *buf) |
| 218 | { |
| 219 | return snprintf(buf, PAGE_SIZE, "%d.%d\n", |
| 220 | NILFS_CURRENT_REV, NILFS_MINOR_REV); |
| 221 | } |
| 222 | |
| 223 | static const char features_readme_str[] = |
| 224 | "The features group contains attributes that describe NILFS file\n" |
| 225 | "system driver features.\n\n" |
| 226 | "(1) revision\n\tshow current revision of NILFS file system driver.\n"; |
| 227 | |
| 228 | static ssize_t nilfs_feature_README_show(struct kobject *kobj, |
| 229 | struct attribute *attr, |
| 230 | char *buf) |
| 231 | { |
| 232 | return snprintf(buf, PAGE_SIZE, features_readme_str); |
| 233 | } |
| 234 | |
| 235 | NILFS_FEATURE_RO_ATTR(revision); |
| 236 | NILFS_FEATURE_RO_ATTR(README); |
| 237 | |
| 238 | static struct attribute *nilfs_feature_attrs[] = { |
| 239 | NILFS_FEATURE_ATTR_LIST(revision), |
| 240 | NILFS_FEATURE_ATTR_LIST(README), |
| 241 | NULL, |
| 242 | }; |
| 243 | |
| 244 | static const struct attribute_group nilfs_feature_attr_group = { |
| 245 | .name = "features", |
| 246 | .attrs = nilfs_feature_attrs, |
| 247 | }; |
| 248 | |
| 249 | int __init nilfs_sysfs_init(void) |
| 250 | { |
| 251 | int err; |
| 252 | |
| 253 | nilfs_kset = kset_create_and_add(NILFS_ROOT_GROUP_NAME, NULL, fs_kobj); |
| 254 | if (!nilfs_kset) { |
| 255 | err = -ENOMEM; |
| 256 | printk(KERN_ERR "NILFS: unable to create sysfs entry: err %d\n", |
| 257 | err); |
| 258 | goto failed_sysfs_init; |
| 259 | } |
| 260 | |
| 261 | err = sysfs_create_group(&nilfs_kset->kobj, &nilfs_feature_attr_group); |
| 262 | if (unlikely(err)) { |
| 263 | printk(KERN_ERR "NILFS: unable to create feature group: err %d\n", |
| 264 | err); |
| 265 | goto cleanup_sysfs_init; |
| 266 | } |
| 267 | |
| 268 | return 0; |
| 269 | |
| 270 | cleanup_sysfs_init: |
| 271 | kset_unregister(nilfs_kset); |
| 272 | |
| 273 | failed_sysfs_init: |
| 274 | return err; |
| 275 | } |
| 276 | |
| 277 | void nilfs_sysfs_exit(void) |
| 278 | { |
| 279 | sysfs_remove_group(&nilfs_kset->kobj, &nilfs_feature_attr_group); |
| 280 | kset_unregister(nilfs_kset); |
| 281 | } |