// SPDX-License-Identifier: GPL-2.0+
/*
* sysfs.c - sysfs support implementation.
*
* Copyright (C) 2005-2014 Nippon Telegraph and Telephone Corporation.
* Copyright (C) 2014 HGST, Inc., a Western Digital Company.
*
* Written by Vyacheslav Dubeyko <Vyacheslav.Dubeyko@hgst.com>
*/
#include <linux/kobject.h>
#include "nilfs.h"
#include "mdt.h"
#include "sufile.h"
#include "cpfile.h"
#include "sysfs.h"
/* /sys/fs/<nilfs>/ */
static struct kset *nilfs_kset;
#define NILFS_SHOW_TIME(time_t_val, buf) ({ \
struct tm res; \
int count = 0; \
time64_to_tm(time_t_val, 0, &res); \
res.tm_year += 1900; \
res.tm_mon += 1; \
count = scnprintf(buf, PAGE_SIZE, \
"%ld-%.2d-%.2d %.2d:%.2d:%.2d\n", \
res.tm_year, res.tm_mon, res.tm_mday, \
res.tm_hour, res.tm_min, res.tm_sec);\
count; \
})
#define NILFS_DEV_INT_GROUP_OPS(name, parent_name) \
static ssize_t nilfs_##name##_attr_show(struct kobject *kobj, \
struct attribute *attr, char *buf) \
{ \
struct the_nilfs *nilfs = container_of(kobj->parent, \
struct the_nilfs, \
ns_##parent_name##_kobj); \
struct nilfs_##name##_attr *a = container_of(attr, \
struct nilfs_##name##_attr, \
attr); \
return a->show ? a->show(a, nilfs, buf) : 0; \
} \
static ssize_t nilfs_##name##_attr_store(struct kobject *kobj, \
struct attribute *attr, \
const char *buf, size_t len) \
{ \
struct the_nilfs *nilfs = container_of(kobj->parent, \
struct the_nilfs, \
ns_##parent_name##_kobj); \
struct nilfs_##name##_attr *a = container_of(attr, \
struct nilfs_##name##_attr, \
attr); \
return a->store ? a->store(a, nilfs, buf, len) : 0; \
} \
static const struct sysfs_ops nilfs_##name##_attr_ops = { \
.show = nilfs_##name##_attr_show, \
.store = nilfs_##name##_attr_store, \
}
#define NILFS_DEV_INT_GROUP_TYPE(name, parent_name) \
static void nilfs_##name##_attr_release(struct kobject *kobj) \
{ \
struct nilfs_sysfs_##parent_name##_subgroups *subgroups; \
struct the_nilfs *nilfs = container_of(kobj->parent, \
struct the_nilfs, \
ns_##parent_name##_kobj); \
subgroups = nilfs->ns_##parent_name##_subgroups; \
complete(&subgroups->sg_##name##_kobj_unregister); \
} \
static struct kobj_type nilfs_##name##_ktype = { \
.default_attrs = nilfs_##name##_attrs, \
.sysfs_ops = &nilfs_##name##_attr_ops, \
.release = nilfs_##name##_attr_release, \
}
#define NILFS_DEV_INT_GROUP_FNS(name, parent_name) \
static int nilfs_sysfs_create_##name##_group(struct the_nilfs *nilfs) \
{ \
struct kobject *parent; \
struct kobject *kobj; \
struct completion *kobj_unregister; \
struct nilfs_sysfs_##parent_name##_subgroups *subgroups; \
int err; \
subgroups = nilfs->ns_##parent_name##_subgroups; \
kobj = &subgroups->sg_##name##_kobj; \
kobj_unregister = &subgroups->sg_##name##_kobj_unregister; \
parent = &nilfs->ns_##parent_name##_kobj; \
kobj->kset = nilfs_kset; \
init_completion(kobj_unregister); \
err = kobject_init_and_add(kobj, &nilfs_##name##_ktype, parent, \
#name); \
if (err) \
return err; \
return 0; \
} \
static void nilfs_sysfs_delete_##name##_group(struct the_nilfs *nilfs) \
{ \
kobject_del(&nilfs->ns_##parent_name##_subgroups->sg_##name##_kobj); \
}
/************************************************************************
* NILFS snapshot attrs *
************************************************************************/
static ssize_t
nilfs_snapshot_inodes_count_show(struct nilfs_snapshot_attr *attr,
struct nilfs_root *root, char *buf)
{
return snprintf(buf, PAGE_SIZE, "%llu\n",
(unsigned long long)atomic64_read(&root->inodes_count));
}
static ssize_t
nilfs_snapshot_blocks_count_show(struct nilfs_snapshot_attr *attr,
struct nilfs_root *root, char *buf)
{
return snprintf(buf, PAGE_SIZE, "%llu\n",
(unsigned long long)atomic64_read(&root->blocks_count));
}
static const char snapshot_readme_str[] =
"The group contains details about mounted snapshot.\n\n"
"(1) inodes_count\n\tshow number of inodes for snapshot.\n\n"
"(2) blocks_count\n\tshow number of blocks for snapshot.\n\n";
static ssize_t
nilfs_snapshot_README_show(struct nilfs_snapshot_attr *attr,
struct nilfs_root *root, char *buf)
{
return snprintf(buf, PAGE_SIZE, snapshot_readme_str);
}
NILFS_SNAPSHOT_RO_ATTR(inodes_count);
NILFS_SNAPSHOT_RO_ATTR(blocks_count);
NILFS_SNAPSHOT_RO_ATTR(README);
static struct attribute *nilfs_snapshot_attrs[] = {
NILFS_SNAPSHOT_ATTR_LIST(inodes_count),
NILFS_SNAPSHOT_ATTR_LIST(blocks_count),
NILFS_SNAPSHOT_ATTR_LIST(README),
NULL,
};
static ssize_t nilfs_snapshot_attr_show(struct kobject *kobj,
struct attribute *attr, char *buf)
{
struct nilfs_root *root =
container_of(kobj, struct nilfs_root, snapshot_kobj);
struct nilfs_snapshot_attr *a =
container_of(attr, struct nilfs_snapshot_attr, attr);
return a->show ? a->show(a, root, buf) : 0;
}
static ssize_t nilfs_snapshot_attr_store(struct kobject *kobj,
struct attribute *attr,
const char *buf, size_t len)
{
struct nilfs_root *root =
container_of(kobj, struct nilfs_root, snapshot_kobj);
struct nilfs_snapshot_attr *a =
container_of(attr, struct nilfs_snapshot_attr, attr);
return a->store ? a->store(a, root, buf, len) :