blob: 239f239da8ae2ffb78b72bbb65f7af611d2e8ae5 [file] [log] [blame]
Vyacheslav Dubeykoaebe17f2014-08-08 14:20:37 -07001/*
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>/ */
29static 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
Vyacheslav Dubeykocaa05d42014-08-08 14:20:42 -070044#define NILFS_DEV_INT_GROUP_OPS(name, parent_name) \
45static ssize_t nilfs_##name##_attr_show(struct kobject *kobj, \
46 struct attribute *attr, char *buf) \
47{ \
48 struct the_nilfs *nilfs = container_of(kobj->parent, \
49 struct the_nilfs, \
50 ns_##parent_name##_kobj); \
51 struct nilfs_##name##_attr *a = container_of(attr, \
52 struct nilfs_##name##_attr, \
53 attr); \
54 return a->show ? a->show(a, nilfs, buf) : 0; \
55} \
56static ssize_t nilfs_##name##_attr_store(struct kobject *kobj, \
57 struct attribute *attr, \
58 const char *buf, size_t len) \
59{ \
60 struct the_nilfs *nilfs = container_of(kobj->parent, \
61 struct the_nilfs, \
62 ns_##parent_name##_kobj); \
63 struct nilfs_##name##_attr *a = container_of(attr, \
64 struct nilfs_##name##_attr, \
65 attr); \
66 return a->store ? a->store(a, nilfs, buf, len) : 0; \
67} \
68static const struct sysfs_ops nilfs_##name##_attr_ops = { \
69 .show = nilfs_##name##_attr_show, \
70 .store = nilfs_##name##_attr_store, \
71};
72
73#define NILFS_DEV_INT_GROUP_TYPE(name, parent_name) \
74static void nilfs_##name##_attr_release(struct kobject *kobj) \
75{ \
76 struct nilfs_sysfs_##parent_name##_subgroups *subgroups; \
77 struct the_nilfs *nilfs = container_of(kobj->parent, \
78 struct the_nilfs, \
79 ns_##parent_name##_kobj); \
80 subgroups = nilfs->ns_##parent_name##_subgroups; \
81 complete(&subgroups->sg_##name##_kobj_unregister); \
82} \
83static struct kobj_type nilfs_##name##_ktype = { \
84 .default_attrs = nilfs_##name##_attrs, \
85 .sysfs_ops = &nilfs_##name##_attr_ops, \
86 .release = nilfs_##name##_attr_release, \
87};
88
89#define NILFS_DEV_INT_GROUP_FNS(name, parent_name) \
90int nilfs_sysfs_create_##name##_group(struct the_nilfs *nilfs) \
91{ \
92 struct kobject *parent; \
93 struct kobject *kobj; \
94 struct completion *kobj_unregister; \
95 struct nilfs_sysfs_##parent_name##_subgroups *subgroups; \
96 int err; \
97 subgroups = nilfs->ns_##parent_name##_subgroups; \
98 kobj = &subgroups->sg_##name##_kobj; \
99 kobj_unregister = &subgroups->sg_##name##_kobj_unregister; \
100 parent = &nilfs->ns_##parent_name##_kobj; \
101 kobj->kset = nilfs_kset; \
102 init_completion(kobj_unregister); \
103 err = kobject_init_and_add(kobj, &nilfs_##name##_ktype, parent, \
104 #name); \
105 if (err) \
106 return err; \
107 return 0; \
108} \
109void nilfs_sysfs_delete_##name##_group(struct the_nilfs *nilfs) \
110{ \
111 kobject_del(&nilfs->ns_##parent_name##_subgroups->sg_##name##_kobj); \
112}
113
114/************************************************************************
115 * NILFS superblock attrs *
116 ************************************************************************/
117
118static ssize_t
119nilfs_superblock_sb_write_time_show(struct nilfs_superblock_attr *attr,
120 struct the_nilfs *nilfs,
121 char *buf)
122{
123 time_t sbwtime;
124
125 down_read(&nilfs->ns_sem);
126 sbwtime = nilfs->ns_sbwtime;
127 up_read(&nilfs->ns_sem);
128
129 return NILFS_SHOW_TIME(sbwtime, buf);
130}
131
132static ssize_t
133nilfs_superblock_sb_write_time_secs_show(struct nilfs_superblock_attr *attr,
134 struct the_nilfs *nilfs,
135 char *buf)
136{
137 time_t sbwtime;
138
139 down_read(&nilfs->ns_sem);
140 sbwtime = nilfs->ns_sbwtime;
141 up_read(&nilfs->ns_sem);
142
143 return snprintf(buf, PAGE_SIZE, "%llu\n", (unsigned long long)sbwtime);
144}
145
146static ssize_t
147nilfs_superblock_sb_write_count_show(struct nilfs_superblock_attr *attr,
148 struct the_nilfs *nilfs,
149 char *buf)
150{
151 unsigned sbwcount;
152
153 down_read(&nilfs->ns_sem);
154 sbwcount = nilfs->ns_sbwcount;
155 up_read(&nilfs->ns_sem);
156
157 return snprintf(buf, PAGE_SIZE, "%u\n", sbwcount);
158}
159
160static ssize_t
161nilfs_superblock_sb_update_frequency_show(struct nilfs_superblock_attr *attr,
162 struct the_nilfs *nilfs,
163 char *buf)
164{
165 unsigned sb_update_freq;
166
167 down_read(&nilfs->ns_sem);
168 sb_update_freq = nilfs->ns_sb_update_freq;
169 up_read(&nilfs->ns_sem);
170
171 return snprintf(buf, PAGE_SIZE, "%u\n", sb_update_freq);
172}
173
174static ssize_t
175nilfs_superblock_sb_update_frequency_store(struct nilfs_superblock_attr *attr,
176 struct the_nilfs *nilfs,
177 const char *buf, size_t count)
178{
179 unsigned val;
180 int err;
181
182 err = kstrtouint(skip_spaces(buf), 0, &val);
183 if (err) {
184 printk(KERN_ERR "NILFS: unable to convert string: err=%d\n",
185 err);
186 return err;
187 }
188
189 if (val < NILFS_SB_FREQ) {
190 val = NILFS_SB_FREQ;
191 printk(KERN_WARNING "NILFS: superblock update frequency cannot be lesser than 10 seconds\n");
192 }
193
194 down_write(&nilfs->ns_sem);
195 nilfs->ns_sb_update_freq = val;
196 up_write(&nilfs->ns_sem);
197
198 return count;
199}
200
201static const char sb_readme_str[] =
202 "The superblock group contains attributes that describe\n"
203 "superblock's details.\n\n"
204 "(1) sb_write_time\n\tshow previous write time of super block "
205 "in human-readable format.\n\n"
206 "(2) sb_write_time_secs\n\tshow previous write time of super block "
207 "in seconds.\n\n"
208 "(3) sb_write_count\n\tshow write count of super block.\n\n"
209 "(4) sb_update_frequency\n"
210 "\tshow/set interval of periodical update of superblock (in seconds).\n\n"
211 "\tYou can set preferable frequency of superblock update by command:\n\n"
212 "\t'echo <val> > /sys/fs/<nilfs>/<dev>/superblock/sb_update_frequency'\n";
213
214static ssize_t
215nilfs_superblock_README_show(struct nilfs_superblock_attr *attr,
216 struct the_nilfs *nilfs, char *buf)
217{
218 return snprintf(buf, PAGE_SIZE, sb_readme_str);
219}
220
221NILFS_SUPERBLOCK_RO_ATTR(sb_write_time);
222NILFS_SUPERBLOCK_RO_ATTR(sb_write_time_secs);
223NILFS_SUPERBLOCK_RO_ATTR(sb_write_count);
224NILFS_SUPERBLOCK_RW_ATTR(sb_update_frequency);
225NILFS_SUPERBLOCK_RO_ATTR(README);
226
227static struct attribute *nilfs_superblock_attrs[] = {
228 NILFS_SUPERBLOCK_ATTR_LIST(sb_write_time),
229 NILFS_SUPERBLOCK_ATTR_LIST(sb_write_time_secs),
230 NILFS_SUPERBLOCK_ATTR_LIST(sb_write_count),
231 NILFS_SUPERBLOCK_ATTR_LIST(sb_update_frequency),
232 NILFS_SUPERBLOCK_ATTR_LIST(README),
233 NULL,
234};
235
236NILFS_DEV_INT_GROUP_OPS(superblock, dev);
237NILFS_DEV_INT_GROUP_TYPE(superblock, dev);
238NILFS_DEV_INT_GROUP_FNS(superblock, dev);
239
Vyacheslav Dubeykoaebe17f2014-08-08 14:20:37 -0700240/************************************************************************
Vyacheslav Dubeykoda7141f2014-08-08 14:20:39 -0700241 * NILFS device attrs *
242 ************************************************************************/
243
244static
245ssize_t nilfs_dev_revision_show(struct nilfs_dev_attr *attr,
246 struct the_nilfs *nilfs,
247 char *buf)
248{
249 struct nilfs_super_block **sbp = nilfs->ns_sbp;
250 u32 major = le32_to_cpu(sbp[0]->s_rev_level);
251 u16 minor = le16_to_cpu(sbp[0]->s_minor_rev_level);
252
253 return snprintf(buf, PAGE_SIZE, "%d.%d\n", major, minor);
254}
255
256static
257ssize_t nilfs_dev_blocksize_show(struct nilfs_dev_attr *attr,
258 struct the_nilfs *nilfs,
259 char *buf)
260{
261 return snprintf(buf, PAGE_SIZE, "%u\n", nilfs->ns_blocksize);
262}
263
264static
265ssize_t nilfs_dev_device_size_show(struct nilfs_dev_attr *attr,
266 struct the_nilfs *nilfs,
267 char *buf)
268{
269 struct nilfs_super_block **sbp = nilfs->ns_sbp;
270 u64 dev_size = le64_to_cpu(sbp[0]->s_dev_size);
271
272 return snprintf(buf, PAGE_SIZE, "%llu\n", dev_size);
273}
274
275static
276ssize_t nilfs_dev_free_blocks_show(struct nilfs_dev_attr *attr,
277 struct the_nilfs *nilfs,
278 char *buf)
279{
280 sector_t free_blocks = 0;
281
282 nilfs_count_free_blocks(nilfs, &free_blocks);
283 return snprintf(buf, PAGE_SIZE, "%llu\n",
284 (unsigned long long)free_blocks);
285}
286
287static
288ssize_t nilfs_dev_uuid_show(struct nilfs_dev_attr *attr,
289 struct the_nilfs *nilfs,
290 char *buf)
291{
292 struct nilfs_super_block **sbp = nilfs->ns_sbp;
293
294 return snprintf(buf, PAGE_SIZE, "%pUb\n", sbp[0]->s_uuid);
295}
296
297static
298ssize_t nilfs_dev_volume_name_show(struct nilfs_dev_attr *attr,
299 struct the_nilfs *nilfs,
300 char *buf)
301{
302 struct nilfs_super_block **sbp = nilfs->ns_sbp;
303
304 return scnprintf(buf, sizeof(sbp[0]->s_volume_name), "%s\n",
305 sbp[0]->s_volume_name);
306}
307
308static const char dev_readme_str[] =
309 "The <device> group contains attributes that describe file system\n"
310 "partition's details.\n\n"
311 "(1) revision\n\tshow NILFS file system revision.\n\n"
312 "(2) blocksize\n\tshow volume block size in bytes.\n\n"
313 "(3) device_size\n\tshow volume size in bytes.\n\n"
314 "(4) free_blocks\n\tshow count of free blocks on volume.\n\n"
315 "(5) uuid\n\tshow volume's UUID.\n\n"
316 "(6) volume_name\n\tshow volume's name.\n\n";
317
318static ssize_t nilfs_dev_README_show(struct nilfs_dev_attr *attr,
319 struct the_nilfs *nilfs,
320 char *buf)
321{
322 return snprintf(buf, PAGE_SIZE, dev_readme_str);
323}
324
325NILFS_DEV_RO_ATTR(revision);
326NILFS_DEV_RO_ATTR(blocksize);
327NILFS_DEV_RO_ATTR(device_size);
328NILFS_DEV_RO_ATTR(free_blocks);
329NILFS_DEV_RO_ATTR(uuid);
330NILFS_DEV_RO_ATTR(volume_name);
331NILFS_DEV_RO_ATTR(README);
332
333static struct attribute *nilfs_dev_attrs[] = {
334 NILFS_DEV_ATTR_LIST(revision),
335 NILFS_DEV_ATTR_LIST(blocksize),
336 NILFS_DEV_ATTR_LIST(device_size),
337 NILFS_DEV_ATTR_LIST(free_blocks),
338 NILFS_DEV_ATTR_LIST(uuid),
339 NILFS_DEV_ATTR_LIST(volume_name),
340 NILFS_DEV_ATTR_LIST(README),
341 NULL,
342};
343
344static ssize_t nilfs_dev_attr_show(struct kobject *kobj,
345 struct attribute *attr, char *buf)
346{
347 struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
348 ns_dev_kobj);
349 struct nilfs_dev_attr *a = container_of(attr, struct nilfs_dev_attr,
350 attr);
351
352 return a->show ? a->show(a, nilfs, buf) : 0;
353}
354
355static ssize_t nilfs_dev_attr_store(struct kobject *kobj,
356 struct attribute *attr,
357 const char *buf, size_t len)
358{
359 struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
360 ns_dev_kobj);
361 struct nilfs_dev_attr *a = container_of(attr, struct nilfs_dev_attr,
362 attr);
363
364 return a->store ? a->store(a, nilfs, buf, len) : 0;
365}
366
367static void nilfs_dev_attr_release(struct kobject *kobj)
368{
369 struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
370 ns_dev_kobj);
371 complete(&nilfs->ns_dev_kobj_unregister);
372}
373
374static const struct sysfs_ops nilfs_dev_attr_ops = {
375 .show = nilfs_dev_attr_show,
376 .store = nilfs_dev_attr_store,
377};
378
379static struct kobj_type nilfs_dev_ktype = {
380 .default_attrs = nilfs_dev_attrs,
381 .sysfs_ops = &nilfs_dev_attr_ops,
382 .release = nilfs_dev_attr_release,
383};
384
385int nilfs_sysfs_create_device_group(struct super_block *sb)
386{
387 struct the_nilfs *nilfs = sb->s_fs_info;
Vyacheslav Dubeykocaa05d42014-08-08 14:20:42 -0700388 size_t devgrp_size = sizeof(struct nilfs_sysfs_dev_subgroups);
Vyacheslav Dubeykoda7141f2014-08-08 14:20:39 -0700389 int err;
390
Vyacheslav Dubeykocaa05d42014-08-08 14:20:42 -0700391 nilfs->ns_dev_subgroups = kzalloc(devgrp_size, GFP_KERNEL);
392 if (unlikely(!nilfs->ns_dev_subgroups)) {
393 err = -ENOMEM;
394 printk(KERN_ERR "NILFS: unable to allocate memory for device group\n");
395 goto failed_create_device_group;
396 }
397
Vyacheslav Dubeykoda7141f2014-08-08 14:20:39 -0700398 nilfs->ns_dev_kobj.kset = nilfs_kset;
399 init_completion(&nilfs->ns_dev_kobj_unregister);
400 err = kobject_init_and_add(&nilfs->ns_dev_kobj, &nilfs_dev_ktype, NULL,
401 "%s", sb->s_id);
402 if (err)
Vyacheslav Dubeykocaa05d42014-08-08 14:20:42 -0700403 goto free_dev_subgroups;
404
405 err = nilfs_sysfs_create_superblock_group(nilfs);
406 if (err)
407 goto cleanup_dev_kobject;
Vyacheslav Dubeykoda7141f2014-08-08 14:20:39 -0700408
409 return 0;
410
Vyacheslav Dubeykocaa05d42014-08-08 14:20:42 -0700411cleanup_dev_kobject:
412 kobject_del(&nilfs->ns_dev_kobj);
413
414free_dev_subgroups:
415 kfree(nilfs->ns_dev_subgroups);
416
Vyacheslav Dubeykoda7141f2014-08-08 14:20:39 -0700417failed_create_device_group:
418 return err;
419}
420
421void nilfs_sysfs_delete_device_group(struct the_nilfs *nilfs)
422{
Vyacheslav Dubeykocaa05d42014-08-08 14:20:42 -0700423 nilfs_sysfs_delete_superblock_group(nilfs);
Vyacheslav Dubeykoda7141f2014-08-08 14:20:39 -0700424 kobject_del(&nilfs->ns_dev_kobj);
Vyacheslav Dubeykocaa05d42014-08-08 14:20:42 -0700425 kfree(nilfs->ns_dev_subgroups);
Vyacheslav Dubeykoda7141f2014-08-08 14:20:39 -0700426}
427
428/************************************************************************
Vyacheslav Dubeykoaebe17f2014-08-08 14:20:37 -0700429 * NILFS feature attrs *
430 ************************************************************************/
431
432static ssize_t nilfs_feature_revision_show(struct kobject *kobj,
433 struct attribute *attr, char *buf)
434{
435 return snprintf(buf, PAGE_SIZE, "%d.%d\n",
436 NILFS_CURRENT_REV, NILFS_MINOR_REV);
437}
438
439static const char features_readme_str[] =
440 "The features group contains attributes that describe NILFS file\n"
441 "system driver features.\n\n"
442 "(1) revision\n\tshow current revision of NILFS file system driver.\n";
443
444static ssize_t nilfs_feature_README_show(struct kobject *kobj,
445 struct attribute *attr,
446 char *buf)
447{
448 return snprintf(buf, PAGE_SIZE, features_readme_str);
449}
450
451NILFS_FEATURE_RO_ATTR(revision);
452NILFS_FEATURE_RO_ATTR(README);
453
454static struct attribute *nilfs_feature_attrs[] = {
455 NILFS_FEATURE_ATTR_LIST(revision),
456 NILFS_FEATURE_ATTR_LIST(README),
457 NULL,
458};
459
460static const struct attribute_group nilfs_feature_attr_group = {
461 .name = "features",
462 .attrs = nilfs_feature_attrs,
463};
464
465int __init nilfs_sysfs_init(void)
466{
467 int err;
468
469 nilfs_kset = kset_create_and_add(NILFS_ROOT_GROUP_NAME, NULL, fs_kobj);
470 if (!nilfs_kset) {
471 err = -ENOMEM;
472 printk(KERN_ERR "NILFS: unable to create sysfs entry: err %d\n",
473 err);
474 goto failed_sysfs_init;
475 }
476
477 err = sysfs_create_group(&nilfs_kset->kobj, &nilfs_feature_attr_group);
478 if (unlikely(err)) {
479 printk(KERN_ERR "NILFS: unable to create feature group: err %d\n",
480 err);
481 goto cleanup_sysfs_init;
482 }
483
484 return 0;
485
486cleanup_sysfs_init:
487 kset_unregister(nilfs_kset);
488
489failed_sysfs_init:
490 return err;
491}
492
493void nilfs_sysfs_exit(void)
494{
495 sysfs_remove_group(&nilfs_kset->kobj, &nilfs_feature_attr_group);
496 kset_unregister(nilfs_kset);
497}