Linux Audio

Check our new training course

Loading...
v4.17
 
   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>/ */
  29static struct kset *nilfs_kset;
  30
  31#define NILFS_SHOW_TIME(time_t_val, buf) ({ \
  32		struct tm res; \
  33		int count = 0; \
  34		time64_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#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) \
  90static int 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} \
 109static void 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 snapshot attrs                          *
 116 ************************************************************************/
 117
 118static ssize_t
 119nilfs_snapshot_inodes_count_show(struct nilfs_snapshot_attr *attr,
 120				 struct nilfs_root *root, char *buf)
 121{
 122	return snprintf(buf, PAGE_SIZE, "%llu\n",
 123			(unsigned long long)atomic64_read(&root->inodes_count));
 124}
 125
 126static ssize_t
 127nilfs_snapshot_blocks_count_show(struct nilfs_snapshot_attr *attr,
 128				 struct nilfs_root *root, char *buf)
 129{
 130	return snprintf(buf, PAGE_SIZE, "%llu\n",
 131			(unsigned long long)atomic64_read(&root->blocks_count));
 132}
 133
 134static const char snapshot_readme_str[] =
 135	"The group contains details about mounted snapshot.\n\n"
 136	"(1) inodes_count\n\tshow number of inodes for snapshot.\n\n"
 137	"(2) blocks_count\n\tshow number of blocks for snapshot.\n\n";
 138
 139static ssize_t
 140nilfs_snapshot_README_show(struct nilfs_snapshot_attr *attr,
 141			    struct nilfs_root *root, char *buf)
 142{
 143	return snprintf(buf, PAGE_SIZE, snapshot_readme_str);
 144}
 145
 146NILFS_SNAPSHOT_RO_ATTR(inodes_count);
 147NILFS_SNAPSHOT_RO_ATTR(blocks_count);
 148NILFS_SNAPSHOT_RO_ATTR(README);
 149
 150static struct attribute *nilfs_snapshot_attrs[] = {
 151	NILFS_SNAPSHOT_ATTR_LIST(inodes_count),
 152	NILFS_SNAPSHOT_ATTR_LIST(blocks_count),
 153	NILFS_SNAPSHOT_ATTR_LIST(README),
 154	NULL,
 155};
 156
 157static ssize_t nilfs_snapshot_attr_show(struct kobject *kobj,
 158					struct attribute *attr, char *buf)
 159{
 160	struct nilfs_root *root =
 161			container_of(kobj, struct nilfs_root, snapshot_kobj);
 162	struct nilfs_snapshot_attr *a =
 163			container_of(attr, struct nilfs_snapshot_attr, attr);
 164
 165	return a->show ? a->show(a, root, buf) : 0;
 166}
 167
 168static ssize_t nilfs_snapshot_attr_store(struct kobject *kobj,
 169					 struct attribute *attr,
 170					 const char *buf, size_t len)
 171{
 172	struct nilfs_root *root =
 173			container_of(kobj, struct nilfs_root, snapshot_kobj);
 174	struct nilfs_snapshot_attr *a =
 175			container_of(attr, struct nilfs_snapshot_attr, attr);
 176
 177	return a->store ? a->store(a, root, buf, len) : 0;
 178}
 179
 180static void nilfs_snapshot_attr_release(struct kobject *kobj)
 181{
 182	struct nilfs_root *root = container_of(kobj, struct nilfs_root,
 183						snapshot_kobj);
 184	complete(&root->snapshot_kobj_unregister);
 185}
 186
 187static const struct sysfs_ops nilfs_snapshot_attr_ops = {
 188	.show	= nilfs_snapshot_attr_show,
 189	.store	= nilfs_snapshot_attr_store,
 190};
 191
 192static struct kobj_type nilfs_snapshot_ktype = {
 193	.default_attrs	= nilfs_snapshot_attrs,
 194	.sysfs_ops	= &nilfs_snapshot_attr_ops,
 195	.release	= nilfs_snapshot_attr_release,
 196};
 197
 198int nilfs_sysfs_create_snapshot_group(struct nilfs_root *root)
 199{
 200	struct the_nilfs *nilfs;
 201	struct kobject *parent;
 202	int err;
 203
 204	nilfs = root->nilfs;
 205	parent = &nilfs->ns_dev_subgroups->sg_mounted_snapshots_kobj;
 206	root->snapshot_kobj.kset = nilfs_kset;
 207	init_completion(&root->snapshot_kobj_unregister);
 208
 209	if (root->cno == NILFS_CPTREE_CURRENT_CNO) {
 210		err = kobject_init_and_add(&root->snapshot_kobj,
 211					    &nilfs_snapshot_ktype,
 212					    &nilfs->ns_dev_kobj,
 213					    "current_checkpoint");
 214	} else {
 215		err = kobject_init_and_add(&root->snapshot_kobj,
 216					    &nilfs_snapshot_ktype,
 217					    parent,
 218					    "%llu", root->cno);
 219	}
 220
 221	if (err)
 222		return err;
 223
 224	return 0;
 225}
 226
 227void nilfs_sysfs_delete_snapshot_group(struct nilfs_root *root)
 228{
 229	kobject_del(&root->snapshot_kobj);
 230}
 231
 232/************************************************************************
 233 *                    NILFS mounted snapshots attrs                     *
 234 ************************************************************************/
 235
 236static const char mounted_snapshots_readme_str[] =
 237	"The mounted_snapshots group contains group for\n"
 238	"every mounted snapshot.\n";
 239
 240static ssize_t
 241nilfs_mounted_snapshots_README_show(struct nilfs_mounted_snapshots_attr *attr,
 242				    struct the_nilfs *nilfs, char *buf)
 243{
 244	return snprintf(buf, PAGE_SIZE, mounted_snapshots_readme_str);
 245}
 246
 247NILFS_MOUNTED_SNAPSHOTS_RO_ATTR(README);
 248
 249static struct attribute *nilfs_mounted_snapshots_attrs[] = {
 250	NILFS_MOUNTED_SNAPSHOTS_ATTR_LIST(README),
 251	NULL,
 252};
 253
 254NILFS_DEV_INT_GROUP_OPS(mounted_snapshots, dev);
 255NILFS_DEV_INT_GROUP_TYPE(mounted_snapshots, dev);
 256NILFS_DEV_INT_GROUP_FNS(mounted_snapshots, dev);
 257
 258/************************************************************************
 259 *                      NILFS checkpoints attrs                         *
 260 ************************************************************************/
 261
 262static ssize_t
 263nilfs_checkpoints_checkpoints_number_show(struct nilfs_checkpoints_attr *attr,
 264					    struct the_nilfs *nilfs,
 265					    char *buf)
 266{
 267	__u64 ncheckpoints;
 268	struct nilfs_cpstat cpstat;
 269	int err;
 270
 271	down_read(&nilfs->ns_segctor_sem);
 272	err = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
 273	up_read(&nilfs->ns_segctor_sem);
 274	if (err < 0) {
 275		nilfs_msg(nilfs->ns_sb, KERN_ERR,
 276			  "unable to get checkpoint stat: err=%d", err);
 277		return err;
 278	}
 279
 280	ncheckpoints = cpstat.cs_ncps;
 281
 282	return snprintf(buf, PAGE_SIZE, "%llu\n", ncheckpoints);
 283}
 284
 285static ssize_t
 286nilfs_checkpoints_snapshots_number_show(struct nilfs_checkpoints_attr *attr,
 287					struct the_nilfs *nilfs,
 288					char *buf)
 289{
 290	__u64 nsnapshots;
 291	struct nilfs_cpstat cpstat;
 292	int err;
 293
 294	down_read(&nilfs->ns_segctor_sem);
 295	err = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
 296	up_read(&nilfs->ns_segctor_sem);
 297	if (err < 0) {
 298		nilfs_msg(nilfs->ns_sb, KERN_ERR,
 299			  "unable to get checkpoint stat: err=%d", err);
 300		return err;
 301	}
 302
 303	nsnapshots = cpstat.cs_nsss;
 304
 305	return snprintf(buf, PAGE_SIZE, "%llu\n", nsnapshots);
 306}
 307
 308static ssize_t
 309nilfs_checkpoints_last_seg_checkpoint_show(struct nilfs_checkpoints_attr *attr,
 310					    struct the_nilfs *nilfs,
 311					    char *buf)
 312{
 313	__u64 last_cno;
 314
 315	spin_lock(&nilfs->ns_last_segment_lock);
 316	last_cno = nilfs->ns_last_cno;
 317	spin_unlock(&nilfs->ns_last_segment_lock);
 318
 319	return snprintf(buf, PAGE_SIZE, "%llu\n", last_cno);
 320}
 321
 322static ssize_t
 323nilfs_checkpoints_next_checkpoint_show(struct nilfs_checkpoints_attr *attr,
 324					struct the_nilfs *nilfs,
 325					char *buf)
 326{
 327	__u64 cno;
 328
 329	down_read(&nilfs->ns_segctor_sem);
 330	cno = nilfs->ns_cno;
 331	up_read(&nilfs->ns_segctor_sem);
 332
 333	return snprintf(buf, PAGE_SIZE, "%llu\n", cno);
 334}
 335
 336static const char checkpoints_readme_str[] =
 337	"The checkpoints group contains attributes that describe\n"
 338	"details about volume's checkpoints.\n\n"
 339	"(1) checkpoints_number\n\tshow number of checkpoints on volume.\n\n"
 340	"(2) snapshots_number\n\tshow number of snapshots on volume.\n\n"
 341	"(3) last_seg_checkpoint\n"
 342	"\tshow checkpoint number of the latest segment.\n\n"
 343	"(4) next_checkpoint\n\tshow next checkpoint number.\n\n";
 344
 345static ssize_t
 346nilfs_checkpoints_README_show(struct nilfs_checkpoints_attr *attr,
 347				struct the_nilfs *nilfs, char *buf)
 348{
 349	return snprintf(buf, PAGE_SIZE, checkpoints_readme_str);
 350}
 351
 352NILFS_CHECKPOINTS_RO_ATTR(checkpoints_number);
 353NILFS_CHECKPOINTS_RO_ATTR(snapshots_number);
 354NILFS_CHECKPOINTS_RO_ATTR(last_seg_checkpoint);
 355NILFS_CHECKPOINTS_RO_ATTR(next_checkpoint);
 356NILFS_CHECKPOINTS_RO_ATTR(README);
 357
 358static struct attribute *nilfs_checkpoints_attrs[] = {
 359	NILFS_CHECKPOINTS_ATTR_LIST(checkpoints_number),
 360	NILFS_CHECKPOINTS_ATTR_LIST(snapshots_number),
 361	NILFS_CHECKPOINTS_ATTR_LIST(last_seg_checkpoint),
 362	NILFS_CHECKPOINTS_ATTR_LIST(next_checkpoint),
 363	NILFS_CHECKPOINTS_ATTR_LIST(README),
 364	NULL,
 365};
 366
 367NILFS_DEV_INT_GROUP_OPS(checkpoints, dev);
 368NILFS_DEV_INT_GROUP_TYPE(checkpoints, dev);
 369NILFS_DEV_INT_GROUP_FNS(checkpoints, dev);
 370
 371/************************************************************************
 372 *                        NILFS segments attrs                          *
 373 ************************************************************************/
 374
 375static ssize_t
 376nilfs_segments_segments_number_show(struct nilfs_segments_attr *attr,
 377				     struct the_nilfs *nilfs,
 378				     char *buf)
 379{
 380	return snprintf(buf, PAGE_SIZE, "%lu\n", nilfs->ns_nsegments);
 381}
 382
 383static ssize_t
 384nilfs_segments_blocks_per_segment_show(struct nilfs_segments_attr *attr,
 385					struct the_nilfs *nilfs,
 386					char *buf)
 387{
 388	return snprintf(buf, PAGE_SIZE, "%lu\n", nilfs->ns_blocks_per_segment);
 389}
 390
 391static ssize_t
 392nilfs_segments_clean_segments_show(struct nilfs_segments_attr *attr,
 393				    struct the_nilfs *nilfs,
 394				    char *buf)
 395{
 396	unsigned long ncleansegs;
 397
 398	down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
 399	ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
 400	up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
 401
 402	return snprintf(buf, PAGE_SIZE, "%lu\n", ncleansegs);
 403}
 404
 405static ssize_t
 406nilfs_segments_dirty_segments_show(struct nilfs_segments_attr *attr,
 407				    struct the_nilfs *nilfs,
 408				    char *buf)
 409{
 410	struct nilfs_sustat sustat;
 411	int err;
 412
 413	down_read(&nilfs->ns_segctor_sem);
 414	err = nilfs_sufile_get_stat(nilfs->ns_sufile, &sustat);
 415	up_read(&nilfs->ns_segctor_sem);
 416	if (err < 0) {
 417		nilfs_msg(nilfs->ns_sb, KERN_ERR,
 418			  "unable to get segment stat: err=%d", err);
 419		return err;
 420	}
 421
 422	return snprintf(buf, PAGE_SIZE, "%llu\n", sustat.ss_ndirtysegs);
 423}
 424
 425static const char segments_readme_str[] =
 426	"The segments group contains attributes that describe\n"
 427	"details about volume's segments.\n\n"
 428	"(1) segments_number\n\tshow number of segments on volume.\n\n"
 429	"(2) blocks_per_segment\n\tshow number of blocks in segment.\n\n"
 430	"(3) clean_segments\n\tshow count of clean segments.\n\n"
 431	"(4) dirty_segments\n\tshow count of dirty segments.\n\n";
 432
 433static ssize_t
 434nilfs_segments_README_show(struct nilfs_segments_attr *attr,
 435			    struct the_nilfs *nilfs,
 436			    char *buf)
 437{
 438	return snprintf(buf, PAGE_SIZE, segments_readme_str);
 439}
 440
 441NILFS_SEGMENTS_RO_ATTR(segments_number);
 442NILFS_SEGMENTS_RO_ATTR(blocks_per_segment);
 443NILFS_SEGMENTS_RO_ATTR(clean_segments);
 444NILFS_SEGMENTS_RO_ATTR(dirty_segments);
 445NILFS_SEGMENTS_RO_ATTR(README);
 446
 447static struct attribute *nilfs_segments_attrs[] = {
 448	NILFS_SEGMENTS_ATTR_LIST(segments_number),
 449	NILFS_SEGMENTS_ATTR_LIST(blocks_per_segment),
 450	NILFS_SEGMENTS_ATTR_LIST(clean_segments),
 451	NILFS_SEGMENTS_ATTR_LIST(dirty_segments),
 452	NILFS_SEGMENTS_ATTR_LIST(README),
 453	NULL,
 454};
 455
 456NILFS_DEV_INT_GROUP_OPS(segments, dev);
 457NILFS_DEV_INT_GROUP_TYPE(segments, dev);
 458NILFS_DEV_INT_GROUP_FNS(segments, dev);
 459
 460/************************************************************************
 461 *                        NILFS segctor attrs                           *
 462 ************************************************************************/
 463
 464static ssize_t
 465nilfs_segctor_last_pseg_block_show(struct nilfs_segctor_attr *attr,
 466				    struct the_nilfs *nilfs,
 467				    char *buf)
 468{
 469	sector_t last_pseg;
 470
 471	spin_lock(&nilfs->ns_last_segment_lock);
 472	last_pseg = nilfs->ns_last_pseg;
 473	spin_unlock(&nilfs->ns_last_segment_lock);
 474
 475	return snprintf(buf, PAGE_SIZE, "%llu\n",
 476			(unsigned long long)last_pseg);
 477}
 478
 479static ssize_t
 480nilfs_segctor_last_seg_sequence_show(struct nilfs_segctor_attr *attr,
 481					struct the_nilfs *nilfs,
 482					char *buf)
 483{
 484	u64 last_seq;
 485
 486	spin_lock(&nilfs->ns_last_segment_lock);
 487	last_seq = nilfs->ns_last_seq;
 488	spin_unlock(&nilfs->ns_last_segment_lock);
 489
 490	return snprintf(buf, PAGE_SIZE, "%llu\n", last_seq);
 491}
 492
 493static ssize_t
 494nilfs_segctor_last_seg_checkpoint_show(struct nilfs_segctor_attr *attr,
 495					struct the_nilfs *nilfs,
 496					char *buf)
 497{
 498	__u64 last_cno;
 499
 500	spin_lock(&nilfs->ns_last_segment_lock);
 501	last_cno = nilfs->ns_last_cno;
 502	spin_unlock(&nilfs->ns_last_segment_lock);
 503
 504	return snprintf(buf, PAGE_SIZE, "%llu\n", last_cno);
 505}
 506
 507static ssize_t
 508nilfs_segctor_current_seg_sequence_show(struct nilfs_segctor_attr *attr,
 509					struct the_nilfs *nilfs,
 510					char *buf)
 511{
 512	u64 seg_seq;
 513
 514	down_read(&nilfs->ns_segctor_sem);
 515	seg_seq = nilfs->ns_seg_seq;
 516	up_read(&nilfs->ns_segctor_sem);
 517
 518	return snprintf(buf, PAGE_SIZE, "%llu\n", seg_seq);
 519}
 520
 521static ssize_t
 522nilfs_segctor_current_last_full_seg_show(struct nilfs_segctor_attr *attr,
 523					 struct the_nilfs *nilfs,
 524					 char *buf)
 525{
 526	__u64 segnum;
 527
 528	down_read(&nilfs->ns_segctor_sem);
 529	segnum = nilfs->ns_segnum;
 530	up_read(&nilfs->ns_segctor_sem);
 531
 532	return snprintf(buf, PAGE_SIZE, "%llu\n", segnum);
 533}
 534
 535static ssize_t
 536nilfs_segctor_next_full_seg_show(struct nilfs_segctor_attr *attr,
 537				 struct the_nilfs *nilfs,
 538				 char *buf)
 539{
 540	__u64 nextnum;
 541
 542	down_read(&nilfs->ns_segctor_sem);
 543	nextnum = nilfs->ns_nextnum;
 544	up_read(&nilfs->ns_segctor_sem);
 545
 546	return snprintf(buf, PAGE_SIZE, "%llu\n", nextnum);
 547}
 548
 549static ssize_t
 550nilfs_segctor_next_pseg_offset_show(struct nilfs_segctor_attr *attr,
 551					struct the_nilfs *nilfs,
 552					char *buf)
 553{
 554	unsigned long pseg_offset;
 555
 556	down_read(&nilfs->ns_segctor_sem);
 557	pseg_offset = nilfs->ns_pseg_offset;
 558	up_read(&nilfs->ns_segctor_sem);
 559
 560	return snprintf(buf, PAGE_SIZE, "%lu\n", pseg_offset);
 561}
 562
 563static ssize_t
 564nilfs_segctor_next_checkpoint_show(struct nilfs_segctor_attr *attr,
 565					struct the_nilfs *nilfs,
 566					char *buf)
 567{
 568	__u64 cno;
 569
 570	down_read(&nilfs->ns_segctor_sem);
 571	cno = nilfs->ns_cno;
 572	up_read(&nilfs->ns_segctor_sem);
 573
 574	return snprintf(buf, PAGE_SIZE, "%llu\n", cno);
 575}
 576
 577static ssize_t
 578nilfs_segctor_last_seg_write_time_show(struct nilfs_segctor_attr *attr,
 579					struct the_nilfs *nilfs,
 580					char *buf)
 581{
 582	time64_t ctime;
 583
 584	down_read(&nilfs->ns_segctor_sem);
 585	ctime = nilfs->ns_ctime;
 586	up_read(&nilfs->ns_segctor_sem);
 587
 588	return NILFS_SHOW_TIME(ctime, buf);
 589}
 590
 591static ssize_t
 592nilfs_segctor_last_seg_write_time_secs_show(struct nilfs_segctor_attr *attr,
 593					    struct the_nilfs *nilfs,
 594					    char *buf)
 595{
 596	time64_t ctime;
 597
 598	down_read(&nilfs->ns_segctor_sem);
 599	ctime = nilfs->ns_ctime;
 600	up_read(&nilfs->ns_segctor_sem);
 601
 602	return snprintf(buf, PAGE_SIZE, "%llu\n", ctime);
 603}
 604
 605static ssize_t
 606nilfs_segctor_last_nongc_write_time_show(struct nilfs_segctor_attr *attr,
 607					 struct the_nilfs *nilfs,
 608					 char *buf)
 609{
 610	time64_t nongc_ctime;
 611
 612	down_read(&nilfs->ns_segctor_sem);
 613	nongc_ctime = nilfs->ns_nongc_ctime;
 614	up_read(&nilfs->ns_segctor_sem);
 615
 616	return NILFS_SHOW_TIME(nongc_ctime, buf);
 617}
 618
 619static ssize_t
 620nilfs_segctor_last_nongc_write_time_secs_show(struct nilfs_segctor_attr *attr,
 621						struct the_nilfs *nilfs,
 622						char *buf)
 623{
 624	time64_t nongc_ctime;
 625
 626	down_read(&nilfs->ns_segctor_sem);
 627	nongc_ctime = nilfs->ns_nongc_ctime;
 628	up_read(&nilfs->ns_segctor_sem);
 629
 630	return snprintf(buf, PAGE_SIZE, "%llu\n", nongc_ctime);
 631}
 632
 633static ssize_t
 634nilfs_segctor_dirty_data_blocks_count_show(struct nilfs_segctor_attr *attr,
 635					    struct the_nilfs *nilfs,
 636					    char *buf)
 637{
 638	u32 ndirtyblks;
 639
 640	down_read(&nilfs->ns_segctor_sem);
 641	ndirtyblks = atomic_read(&nilfs->ns_ndirtyblks);
 642	up_read(&nilfs->ns_segctor_sem);
 643
 644	return snprintf(buf, PAGE_SIZE, "%u\n", ndirtyblks);
 645}
 646
 647static const char segctor_readme_str[] =
 648	"The segctor group contains attributes that describe\n"
 649	"segctor thread activity details.\n\n"
 650	"(1) last_pseg_block\n"
 651	"\tshow start block number of the latest segment.\n\n"
 652	"(2) last_seg_sequence\n"
 653	"\tshow sequence value of the latest segment.\n\n"
 654	"(3) last_seg_checkpoint\n"
 655	"\tshow checkpoint number of the latest segment.\n\n"
 656	"(4) current_seg_sequence\n\tshow segment sequence counter.\n\n"
 657	"(5) current_last_full_seg\n"
 658	"\tshow index number of the latest full segment.\n\n"
 659	"(6) next_full_seg\n"
 660	"\tshow index number of the full segment index to be used next.\n\n"
 661	"(7) next_pseg_offset\n"
 662	"\tshow offset of next partial segment in the current full segment.\n\n"
 663	"(8) next_checkpoint\n\tshow next checkpoint number.\n\n"
 664	"(9) last_seg_write_time\n"
 665	"\tshow write time of the last segment in human-readable format.\n\n"
 666	"(10) last_seg_write_time_secs\n"
 667	"\tshow write time of the last segment in seconds.\n\n"
 668	"(11) last_nongc_write_time\n"
 669	"\tshow write time of the last segment not for cleaner operation "
 670	"in human-readable format.\n\n"
 671	"(12) last_nongc_write_time_secs\n"
 672	"\tshow write time of the last segment not for cleaner operation "
 673	"in seconds.\n\n"
 674	"(13) dirty_data_blocks_count\n"
 675	"\tshow number of dirty data blocks.\n\n";
 676
 677static ssize_t
 678nilfs_segctor_README_show(struct nilfs_segctor_attr *attr,
 679			  struct the_nilfs *nilfs, char *buf)
 680{
 681	return snprintf(buf, PAGE_SIZE, segctor_readme_str);
 682}
 683
 684NILFS_SEGCTOR_RO_ATTR(last_pseg_block);
 685NILFS_SEGCTOR_RO_ATTR(last_seg_sequence);
 686NILFS_SEGCTOR_RO_ATTR(last_seg_checkpoint);
 687NILFS_SEGCTOR_RO_ATTR(current_seg_sequence);
 688NILFS_SEGCTOR_RO_ATTR(current_last_full_seg);
 689NILFS_SEGCTOR_RO_ATTR(next_full_seg);
 690NILFS_SEGCTOR_RO_ATTR(next_pseg_offset);
 691NILFS_SEGCTOR_RO_ATTR(next_checkpoint);
 692NILFS_SEGCTOR_RO_ATTR(last_seg_write_time);
 693NILFS_SEGCTOR_RO_ATTR(last_seg_write_time_secs);
 694NILFS_SEGCTOR_RO_ATTR(last_nongc_write_time);
 695NILFS_SEGCTOR_RO_ATTR(last_nongc_write_time_secs);
 696NILFS_SEGCTOR_RO_ATTR(dirty_data_blocks_count);
 697NILFS_SEGCTOR_RO_ATTR(README);
 698
 699static struct attribute *nilfs_segctor_attrs[] = {
 700	NILFS_SEGCTOR_ATTR_LIST(last_pseg_block),
 701	NILFS_SEGCTOR_ATTR_LIST(last_seg_sequence),
 702	NILFS_SEGCTOR_ATTR_LIST(last_seg_checkpoint),
 703	NILFS_SEGCTOR_ATTR_LIST(current_seg_sequence),
 704	NILFS_SEGCTOR_ATTR_LIST(current_last_full_seg),
 705	NILFS_SEGCTOR_ATTR_LIST(next_full_seg),
 706	NILFS_SEGCTOR_ATTR_LIST(next_pseg_offset),
 707	NILFS_SEGCTOR_ATTR_LIST(next_checkpoint),
 708	NILFS_SEGCTOR_ATTR_LIST(last_seg_write_time),
 709	NILFS_SEGCTOR_ATTR_LIST(last_seg_write_time_secs),
 710	NILFS_SEGCTOR_ATTR_LIST(last_nongc_write_time),
 711	NILFS_SEGCTOR_ATTR_LIST(last_nongc_write_time_secs),
 712	NILFS_SEGCTOR_ATTR_LIST(dirty_data_blocks_count),
 713	NILFS_SEGCTOR_ATTR_LIST(README),
 714	NULL,
 715};
 716
 717NILFS_DEV_INT_GROUP_OPS(segctor, dev);
 718NILFS_DEV_INT_GROUP_TYPE(segctor, dev);
 719NILFS_DEV_INT_GROUP_FNS(segctor, dev);
 720
 721/************************************************************************
 722 *                        NILFS superblock attrs                        *
 723 ************************************************************************/
 724
 725static ssize_t
 726nilfs_superblock_sb_write_time_show(struct nilfs_superblock_attr *attr,
 727				     struct the_nilfs *nilfs,
 728				     char *buf)
 729{
 730	time64_t sbwtime;
 731
 732	down_read(&nilfs->ns_sem);
 733	sbwtime = nilfs->ns_sbwtime;
 734	up_read(&nilfs->ns_sem);
 735
 736	return NILFS_SHOW_TIME(sbwtime, buf);
 737}
 738
 739static ssize_t
 740nilfs_superblock_sb_write_time_secs_show(struct nilfs_superblock_attr *attr,
 741					 struct the_nilfs *nilfs,
 742					 char *buf)
 743{
 744	time64_t sbwtime;
 745
 746	down_read(&nilfs->ns_sem);
 747	sbwtime = nilfs->ns_sbwtime;
 748	up_read(&nilfs->ns_sem);
 749
 750	return snprintf(buf, PAGE_SIZE, "%llu\n", sbwtime);
 751}
 752
 753static ssize_t
 754nilfs_superblock_sb_write_count_show(struct nilfs_superblock_attr *attr,
 755				      struct the_nilfs *nilfs,
 756				      char *buf)
 757{
 758	unsigned int sbwcount;
 759
 760	down_read(&nilfs->ns_sem);
 761	sbwcount = nilfs->ns_sbwcount;
 762	up_read(&nilfs->ns_sem);
 763
 764	return snprintf(buf, PAGE_SIZE, "%u\n", sbwcount);
 765}
 766
 767static ssize_t
 768nilfs_superblock_sb_update_frequency_show(struct nilfs_superblock_attr *attr,
 769					    struct the_nilfs *nilfs,
 770					    char *buf)
 771{
 772	unsigned int sb_update_freq;
 773
 774	down_read(&nilfs->ns_sem);
 775	sb_update_freq = nilfs->ns_sb_update_freq;
 776	up_read(&nilfs->ns_sem);
 777
 778	return snprintf(buf, PAGE_SIZE, "%u\n", sb_update_freq);
 779}
 780
 781static ssize_t
 782nilfs_superblock_sb_update_frequency_store(struct nilfs_superblock_attr *attr,
 783					    struct the_nilfs *nilfs,
 784					    const char *buf, size_t count)
 785{
 786	unsigned int val;
 787	int err;
 788
 789	err = kstrtouint(skip_spaces(buf), 0, &val);
 790	if (err) {
 791		nilfs_msg(nilfs->ns_sb, KERN_ERR,
 792			  "unable to convert string: err=%d", err);
 793		return err;
 794	}
 795
 796	if (val < NILFS_SB_FREQ) {
 797		val = NILFS_SB_FREQ;
 798		nilfs_msg(nilfs->ns_sb, KERN_WARNING,
 799			  "superblock update frequency cannot be lesser than 10 seconds");
 800	}
 801
 802	down_write(&nilfs->ns_sem);
 803	nilfs->ns_sb_update_freq = val;
 804	up_write(&nilfs->ns_sem);
 805
 806	return count;
 807}
 808
 809static const char sb_readme_str[] =
 810	"The superblock group contains attributes that describe\n"
 811	"superblock's details.\n\n"
 812	"(1) sb_write_time\n\tshow previous write time of super block "
 813	"in human-readable format.\n\n"
 814	"(2) sb_write_time_secs\n\tshow previous write time of super block "
 815	"in seconds.\n\n"
 816	"(3) sb_write_count\n\tshow write count of super block.\n\n"
 817	"(4) sb_update_frequency\n"
 818	"\tshow/set interval of periodical update of superblock (in seconds).\n\n"
 819	"\tYou can set preferable frequency of superblock update by command:\n\n"
 820	"\t'echo <val> > /sys/fs/<nilfs>/<dev>/superblock/sb_update_frequency'\n";
 821
 822static ssize_t
 823nilfs_superblock_README_show(struct nilfs_superblock_attr *attr,
 824				struct the_nilfs *nilfs, char *buf)
 825{
 826	return snprintf(buf, PAGE_SIZE, sb_readme_str);
 827}
 828
 829NILFS_SUPERBLOCK_RO_ATTR(sb_write_time);
 830NILFS_SUPERBLOCK_RO_ATTR(sb_write_time_secs);
 831NILFS_SUPERBLOCK_RO_ATTR(sb_write_count);
 832NILFS_SUPERBLOCK_RW_ATTR(sb_update_frequency);
 833NILFS_SUPERBLOCK_RO_ATTR(README);
 834
 835static struct attribute *nilfs_superblock_attrs[] = {
 836	NILFS_SUPERBLOCK_ATTR_LIST(sb_write_time),
 837	NILFS_SUPERBLOCK_ATTR_LIST(sb_write_time_secs),
 838	NILFS_SUPERBLOCK_ATTR_LIST(sb_write_count),
 839	NILFS_SUPERBLOCK_ATTR_LIST(sb_update_frequency),
 840	NILFS_SUPERBLOCK_ATTR_LIST(README),
 841	NULL,
 842};
 843
 844NILFS_DEV_INT_GROUP_OPS(superblock, dev);
 845NILFS_DEV_INT_GROUP_TYPE(superblock, dev);
 846NILFS_DEV_INT_GROUP_FNS(superblock, dev);
 847
 848/************************************************************************
 849 *                        NILFS device attrs                            *
 850 ************************************************************************/
 851
 852static
 853ssize_t nilfs_dev_revision_show(struct nilfs_dev_attr *attr,
 854				struct the_nilfs *nilfs,
 855				char *buf)
 856{
 857	struct nilfs_super_block **sbp = nilfs->ns_sbp;
 858	u32 major = le32_to_cpu(sbp[0]->s_rev_level);
 859	u16 minor = le16_to_cpu(sbp[0]->s_minor_rev_level);
 860
 861	return snprintf(buf, PAGE_SIZE, "%d.%d\n", major, minor);
 862}
 863
 864static
 865ssize_t nilfs_dev_blocksize_show(struct nilfs_dev_attr *attr,
 866				 struct the_nilfs *nilfs,
 867				 char *buf)
 868{
 869	return snprintf(buf, PAGE_SIZE, "%u\n", nilfs->ns_blocksize);
 870}
 871
 872static
 873ssize_t nilfs_dev_device_size_show(struct nilfs_dev_attr *attr,
 874				    struct the_nilfs *nilfs,
 875				    char *buf)
 876{
 877	struct nilfs_super_block **sbp = nilfs->ns_sbp;
 878	u64 dev_size = le64_to_cpu(sbp[0]->s_dev_size);
 879
 880	return snprintf(buf, PAGE_SIZE, "%llu\n", dev_size);
 881}
 882
 883static
 884ssize_t nilfs_dev_free_blocks_show(struct nilfs_dev_attr *attr,
 885				   struct the_nilfs *nilfs,
 886				   char *buf)
 887{
 888	sector_t free_blocks = 0;
 889
 890	nilfs_count_free_blocks(nilfs, &free_blocks);
 891	return snprintf(buf, PAGE_SIZE, "%llu\n",
 892			(unsigned long long)free_blocks);
 893}
 894
 895static
 896ssize_t nilfs_dev_uuid_show(struct nilfs_dev_attr *attr,
 897			    struct the_nilfs *nilfs,
 898			    char *buf)
 899{
 900	struct nilfs_super_block **sbp = nilfs->ns_sbp;
 901
 902	return snprintf(buf, PAGE_SIZE, "%pUb\n", sbp[0]->s_uuid);
 903}
 904
 905static
 906ssize_t nilfs_dev_volume_name_show(struct nilfs_dev_attr *attr,
 907				    struct the_nilfs *nilfs,
 908				    char *buf)
 909{
 910	struct nilfs_super_block **sbp = nilfs->ns_sbp;
 911
 912	return scnprintf(buf, sizeof(sbp[0]->s_volume_name), "%s\n",
 913			 sbp[0]->s_volume_name);
 914}
 915
 916static const char dev_readme_str[] =
 917	"The <device> group contains attributes that describe file system\n"
 918	"partition's details.\n\n"
 919	"(1) revision\n\tshow NILFS file system revision.\n\n"
 920	"(2) blocksize\n\tshow volume block size in bytes.\n\n"
 921	"(3) device_size\n\tshow volume size in bytes.\n\n"
 922	"(4) free_blocks\n\tshow count of free blocks on volume.\n\n"
 923	"(5) uuid\n\tshow volume's UUID.\n\n"
 924	"(6) volume_name\n\tshow volume's name.\n\n";
 925
 926static ssize_t nilfs_dev_README_show(struct nilfs_dev_attr *attr,
 927				     struct the_nilfs *nilfs,
 928				     char *buf)
 929{
 930	return snprintf(buf, PAGE_SIZE, dev_readme_str);
 931}
 932
 933NILFS_DEV_RO_ATTR(revision);
 934NILFS_DEV_RO_ATTR(blocksize);
 935NILFS_DEV_RO_ATTR(device_size);
 936NILFS_DEV_RO_ATTR(free_blocks);
 937NILFS_DEV_RO_ATTR(uuid);
 938NILFS_DEV_RO_ATTR(volume_name);
 939NILFS_DEV_RO_ATTR(README);
 940
 941static struct attribute *nilfs_dev_attrs[] = {
 942	NILFS_DEV_ATTR_LIST(revision),
 943	NILFS_DEV_ATTR_LIST(blocksize),
 944	NILFS_DEV_ATTR_LIST(device_size),
 945	NILFS_DEV_ATTR_LIST(free_blocks),
 946	NILFS_DEV_ATTR_LIST(uuid),
 947	NILFS_DEV_ATTR_LIST(volume_name),
 948	NILFS_DEV_ATTR_LIST(README),
 949	NULL,
 950};
 951
 952static ssize_t nilfs_dev_attr_show(struct kobject *kobj,
 953				    struct attribute *attr, char *buf)
 954{
 955	struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
 956						ns_dev_kobj);
 957	struct nilfs_dev_attr *a = container_of(attr, struct nilfs_dev_attr,
 958						attr);
 959
 960	return a->show ? a->show(a, nilfs, buf) : 0;
 961}
 962
 963static ssize_t nilfs_dev_attr_store(struct kobject *kobj,
 964				    struct attribute *attr,
 965				    const char *buf, size_t len)
 966{
 967	struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
 968						ns_dev_kobj);
 969	struct nilfs_dev_attr *a = container_of(attr, struct nilfs_dev_attr,
 970						attr);
 971
 972	return a->store ? a->store(a, nilfs, buf, len) : 0;
 973}
 974
 975static void nilfs_dev_attr_release(struct kobject *kobj)
 976{
 977	struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
 978						ns_dev_kobj);
 979	complete(&nilfs->ns_dev_kobj_unregister);
 980}
 981
 982static const struct sysfs_ops nilfs_dev_attr_ops = {
 983	.show	= nilfs_dev_attr_show,
 984	.store	= nilfs_dev_attr_store,
 985};
 986
 987static struct kobj_type nilfs_dev_ktype = {
 988	.default_attrs	= nilfs_dev_attrs,
 989	.sysfs_ops	= &nilfs_dev_attr_ops,
 990	.release	= nilfs_dev_attr_release,
 991};
 992
 993int nilfs_sysfs_create_device_group(struct super_block *sb)
 994{
 995	struct the_nilfs *nilfs = sb->s_fs_info;
 996	size_t devgrp_size = sizeof(struct nilfs_sysfs_dev_subgroups);
 997	int err;
 998
 999	nilfs->ns_dev_subgroups = kzalloc(devgrp_size, GFP_KERNEL);
1000	if (unlikely(!nilfs->ns_dev_subgroups)) {
1001		err = -ENOMEM;
1002		nilfs_msg(sb, KERN_ERR,
1003			  "unable to allocate memory for device group");
1004		goto failed_create_device_group;
1005	}
1006
1007	nilfs->ns_dev_kobj.kset = nilfs_kset;
1008	init_completion(&nilfs->ns_dev_kobj_unregister);
1009	err = kobject_init_and_add(&nilfs->ns_dev_kobj, &nilfs_dev_ktype, NULL,
1010				    "%s", sb->s_id);
1011	if (err)
1012		goto free_dev_subgroups;
1013
1014	err = nilfs_sysfs_create_mounted_snapshots_group(nilfs);
1015	if (err)
1016		goto cleanup_dev_kobject;
1017
1018	err = nilfs_sysfs_create_checkpoints_group(nilfs);
1019	if (err)
1020		goto delete_mounted_snapshots_group;
1021
1022	err = nilfs_sysfs_create_segments_group(nilfs);
1023	if (err)
1024		goto delete_checkpoints_group;
1025
1026	err = nilfs_sysfs_create_superblock_group(nilfs);
1027	if (err)
1028		goto delete_segments_group;
1029
1030	err = nilfs_sysfs_create_segctor_group(nilfs);
1031	if (err)
1032		goto delete_superblock_group;
1033
1034	return 0;
1035
1036delete_superblock_group:
1037	nilfs_sysfs_delete_superblock_group(nilfs);
1038
1039delete_segments_group:
1040	nilfs_sysfs_delete_segments_group(nilfs);
1041
1042delete_checkpoints_group:
1043	nilfs_sysfs_delete_checkpoints_group(nilfs);
1044
1045delete_mounted_snapshots_group:
1046	nilfs_sysfs_delete_mounted_snapshots_group(nilfs);
1047
1048cleanup_dev_kobject:
1049	kobject_del(&nilfs->ns_dev_kobj);
1050
1051free_dev_subgroups:
1052	kfree(nilfs->ns_dev_subgroups);
1053
1054failed_create_device_group:
1055	return err;
1056}
1057
1058void nilfs_sysfs_delete_device_group(struct the_nilfs *nilfs)
1059{
1060	nilfs_sysfs_delete_mounted_snapshots_group(nilfs);
1061	nilfs_sysfs_delete_checkpoints_group(nilfs);
1062	nilfs_sysfs_delete_segments_group(nilfs);
1063	nilfs_sysfs_delete_superblock_group(nilfs);
1064	nilfs_sysfs_delete_segctor_group(nilfs);
1065	kobject_del(&nilfs->ns_dev_kobj);
 
1066	kfree(nilfs->ns_dev_subgroups);
1067}
1068
1069/************************************************************************
1070 *                        NILFS feature attrs                           *
1071 ************************************************************************/
1072
1073static ssize_t nilfs_feature_revision_show(struct kobject *kobj,
1074					    struct attribute *attr, char *buf)
1075{
1076	return snprintf(buf, PAGE_SIZE, "%d.%d\n",
1077			NILFS_CURRENT_REV, NILFS_MINOR_REV);
1078}
1079
1080static const char features_readme_str[] =
1081	"The features group contains attributes that describe NILFS file\n"
1082	"system driver features.\n\n"
1083	"(1) revision\n\tshow current revision of NILFS file system driver.\n";
1084
1085static ssize_t nilfs_feature_README_show(struct kobject *kobj,
1086					 struct attribute *attr,
1087					 char *buf)
1088{
1089	return snprintf(buf, PAGE_SIZE, features_readme_str);
1090}
1091
1092NILFS_FEATURE_RO_ATTR(revision);
1093NILFS_FEATURE_RO_ATTR(README);
1094
1095static struct attribute *nilfs_feature_attrs[] = {
1096	NILFS_FEATURE_ATTR_LIST(revision),
1097	NILFS_FEATURE_ATTR_LIST(README),
1098	NULL,
1099};
1100
1101static const struct attribute_group nilfs_feature_attr_group = {
1102	.name = "features",
1103	.attrs = nilfs_feature_attrs,
1104};
1105
1106int __init nilfs_sysfs_init(void)
1107{
1108	int err;
1109
1110	nilfs_kset = kset_create_and_add(NILFS_ROOT_GROUP_NAME, NULL, fs_kobj);
1111	if (!nilfs_kset) {
1112		err = -ENOMEM;
1113		nilfs_msg(NULL, KERN_ERR,
1114			  "unable to create sysfs entry: err=%d", err);
1115		goto failed_sysfs_init;
1116	}
1117
1118	err = sysfs_create_group(&nilfs_kset->kobj, &nilfs_feature_attr_group);
1119	if (unlikely(err)) {
1120		nilfs_msg(NULL, KERN_ERR,
1121			  "unable to create feature group: err=%d", err);
1122		goto cleanup_sysfs_init;
1123	}
1124
1125	return 0;
1126
1127cleanup_sysfs_init:
1128	kset_unregister(nilfs_kset);
1129
1130failed_sysfs_init:
1131	return err;
1132}
1133
1134void nilfs_sysfs_exit(void)
1135{
1136	sysfs_remove_group(&nilfs_kset->kobj, &nilfs_feature_attr_group);
1137	kset_unregister(nilfs_kset);
1138}
v5.14.15
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * sysfs.c - sysfs support implementation.
   4 *
   5 * Copyright (C) 2005-2014 Nippon Telegraph and Telephone Corporation.
   6 * Copyright (C) 2014 HGST, Inc., a Western Digital Company.
   7 *
 
 
 
 
 
 
 
 
 
 
   8 * Written by Vyacheslav Dubeyko <Vyacheslav.Dubeyko@hgst.com>
   9 */
  10
  11#include <linux/kobject.h>
  12
  13#include "nilfs.h"
  14#include "mdt.h"
  15#include "sufile.h"
  16#include "cpfile.h"
  17#include "sysfs.h"
  18
  19/* /sys/fs/<nilfs>/ */
  20static struct kset *nilfs_kset;
  21
 
 
 
 
 
 
 
 
 
 
 
 
 
  22#define NILFS_DEV_INT_GROUP_OPS(name, parent_name) \
  23static ssize_t nilfs_##name##_attr_show(struct kobject *kobj, \
  24					struct attribute *attr, char *buf) \
  25{ \
  26	struct the_nilfs *nilfs = container_of(kobj->parent, \
  27						struct the_nilfs, \
  28						ns_##parent_name##_kobj); \
  29	struct nilfs_##name##_attr *a = container_of(attr, \
  30						struct nilfs_##name##_attr, \
  31						attr); \
  32	return a->show ? a->show(a, nilfs, buf) : 0; \
  33} \
  34static ssize_t nilfs_##name##_attr_store(struct kobject *kobj, \
  35					 struct attribute *attr, \
  36					 const char *buf, size_t len) \
  37{ \
  38	struct the_nilfs *nilfs = container_of(kobj->parent, \
  39						struct the_nilfs, \
  40						ns_##parent_name##_kobj); \
  41	struct nilfs_##name##_attr *a = container_of(attr, \
  42						struct nilfs_##name##_attr, \
  43						attr); \
  44	return a->store ? a->store(a, nilfs, buf, len) : 0; \
  45} \
  46static const struct sysfs_ops nilfs_##name##_attr_ops = { \
  47	.show	= nilfs_##name##_attr_show, \
  48	.store	= nilfs_##name##_attr_store, \
  49}
  50
  51#define NILFS_DEV_INT_GROUP_TYPE(name, parent_name) \
  52static void nilfs_##name##_attr_release(struct kobject *kobj) \
  53{ \
  54	struct nilfs_sysfs_##parent_name##_subgroups *subgroups = container_of(kobj, \
  55						struct nilfs_sysfs_##parent_name##_subgroups, \
  56						sg_##name##_kobj); \
 
 
  57	complete(&subgroups->sg_##name##_kobj_unregister); \
  58} \
  59static struct kobj_type nilfs_##name##_ktype = { \
  60	.default_attrs	= nilfs_##name##_attrs, \
  61	.sysfs_ops	= &nilfs_##name##_attr_ops, \
  62	.release	= nilfs_##name##_attr_release, \
  63}
  64
  65#define NILFS_DEV_INT_GROUP_FNS(name, parent_name) \
  66static int nilfs_sysfs_create_##name##_group(struct the_nilfs *nilfs) \
  67{ \
  68	struct kobject *parent; \
  69	struct kobject *kobj; \
  70	struct completion *kobj_unregister; \
  71	struct nilfs_sysfs_##parent_name##_subgroups *subgroups; \
  72	int err; \
  73	subgroups = nilfs->ns_##parent_name##_subgroups; \
  74	kobj = &subgroups->sg_##name##_kobj; \
  75	kobj_unregister = &subgroups->sg_##name##_kobj_unregister; \
  76	parent = &nilfs->ns_##parent_name##_kobj; \
  77	kobj->kset = nilfs_kset; \
  78	init_completion(kobj_unregister); \
  79	err = kobject_init_and_add(kobj, &nilfs_##name##_ktype, parent, \
  80				    #name); \
  81	if (err) \
  82		kobject_put(kobj); \
  83	return err; \
  84} \
  85static void nilfs_sysfs_delete_##name##_group(struct the_nilfs *nilfs) \
  86{ \
  87	kobject_put(&nilfs->ns_##parent_name##_subgroups->sg_##name##_kobj); \
  88}
  89
  90/************************************************************************
  91 *                        NILFS snapshot attrs                          *
  92 ************************************************************************/
  93
  94static ssize_t
  95nilfs_snapshot_inodes_count_show(struct nilfs_snapshot_attr *attr,
  96				 struct nilfs_root *root, char *buf)
  97{
  98	return snprintf(buf, PAGE_SIZE, "%llu\n",
  99			(unsigned long long)atomic64_read(&root->inodes_count));
 100}
 101
 102static ssize_t
 103nilfs_snapshot_blocks_count_show(struct nilfs_snapshot_attr *attr,
 104				 struct nilfs_root *root, char *buf)
 105{
 106	return snprintf(buf, PAGE_SIZE, "%llu\n",
 107			(unsigned long long)atomic64_read(&root->blocks_count));
 108}
 109
 110static const char snapshot_readme_str[] =
 111	"The group contains details about mounted snapshot.\n\n"
 112	"(1) inodes_count\n\tshow number of inodes for snapshot.\n\n"
 113	"(2) blocks_count\n\tshow number of blocks for snapshot.\n\n";
 114
 115static ssize_t
 116nilfs_snapshot_README_show(struct nilfs_snapshot_attr *attr,
 117			    struct nilfs_root *root, char *buf)
 118{
 119	return snprintf(buf, PAGE_SIZE, snapshot_readme_str);
 120}
 121
 122NILFS_SNAPSHOT_RO_ATTR(inodes_count);
 123NILFS_SNAPSHOT_RO_ATTR(blocks_count);
 124NILFS_SNAPSHOT_RO_ATTR(README);
 125
 126static struct attribute *nilfs_snapshot_attrs[] = {
 127	NILFS_SNAPSHOT_ATTR_LIST(inodes_count),
 128	NILFS_SNAPSHOT_ATTR_LIST(blocks_count),
 129	NILFS_SNAPSHOT_ATTR_LIST(README),
 130	NULL,
 131};
 132
 133static ssize_t nilfs_snapshot_attr_show(struct kobject *kobj,
 134					struct attribute *attr, char *buf)
 135{
 136	struct nilfs_root *root =
 137			container_of(kobj, struct nilfs_root, snapshot_kobj);
 138	struct nilfs_snapshot_attr *a =
 139			container_of(attr, struct nilfs_snapshot_attr, attr);
 140
 141	return a->show ? a->show(a, root, buf) : 0;
 142}
 143
 144static ssize_t nilfs_snapshot_attr_store(struct kobject *kobj,
 145					 struct attribute *attr,
 146					 const char *buf, size_t len)
 147{
 148	struct nilfs_root *root =
 149			container_of(kobj, struct nilfs_root, snapshot_kobj);
 150	struct nilfs_snapshot_attr *a =
 151			container_of(attr, struct nilfs_snapshot_attr, attr);
 152
 153	return a->store ? a->store(a, root, buf, len) : 0;
 154}
 155
 156static void nilfs_snapshot_attr_release(struct kobject *kobj)
 157{
 158	struct nilfs_root *root = container_of(kobj, struct nilfs_root,
 159						snapshot_kobj);
 160	complete(&root->snapshot_kobj_unregister);
 161}
 162
 163static const struct sysfs_ops nilfs_snapshot_attr_ops = {
 164	.show	= nilfs_snapshot_attr_show,
 165	.store	= nilfs_snapshot_attr_store,
 166};
 167
 168static struct kobj_type nilfs_snapshot_ktype = {
 169	.default_attrs	= nilfs_snapshot_attrs,
 170	.sysfs_ops	= &nilfs_snapshot_attr_ops,
 171	.release	= nilfs_snapshot_attr_release,
 172};
 173
 174int nilfs_sysfs_create_snapshot_group(struct nilfs_root *root)
 175{
 176	struct the_nilfs *nilfs;
 177	struct kobject *parent;
 178	int err;
 179
 180	nilfs = root->nilfs;
 181	parent = &nilfs->ns_dev_subgroups->sg_mounted_snapshots_kobj;
 182	root->snapshot_kobj.kset = nilfs_kset;
 183	init_completion(&root->snapshot_kobj_unregister);
 184
 185	if (root->cno == NILFS_CPTREE_CURRENT_CNO) {
 186		err = kobject_init_and_add(&root->snapshot_kobj,
 187					    &nilfs_snapshot_ktype,
 188					    &nilfs->ns_dev_kobj,
 189					    "current_checkpoint");
 190	} else {
 191		err = kobject_init_and_add(&root->snapshot_kobj,
 192					    &nilfs_snapshot_ktype,
 193					    parent,
 194					    "%llu", root->cno);
 195	}
 196
 197	if (err)
 198		kobject_put(&root->snapshot_kobj);
 199
 200	return err;
 201}
 202
 203void nilfs_sysfs_delete_snapshot_group(struct nilfs_root *root)
 204{
 205	kobject_put(&root->snapshot_kobj);
 206}
 207
 208/************************************************************************
 209 *                    NILFS mounted snapshots attrs                     *
 210 ************************************************************************/
 211
 212static const char mounted_snapshots_readme_str[] =
 213	"The mounted_snapshots group contains group for\n"
 214	"every mounted snapshot.\n";
 215
 216static ssize_t
 217nilfs_mounted_snapshots_README_show(struct nilfs_mounted_snapshots_attr *attr,
 218				    struct the_nilfs *nilfs, char *buf)
 219{
 220	return snprintf(buf, PAGE_SIZE, mounted_snapshots_readme_str);
 221}
 222
 223NILFS_MOUNTED_SNAPSHOTS_RO_ATTR(README);
 224
 225static struct attribute *nilfs_mounted_snapshots_attrs[] = {
 226	NILFS_MOUNTED_SNAPSHOTS_ATTR_LIST(README),
 227	NULL,
 228};
 229
 230NILFS_DEV_INT_GROUP_OPS(mounted_snapshots, dev);
 231NILFS_DEV_INT_GROUP_TYPE(mounted_snapshots, dev);
 232NILFS_DEV_INT_GROUP_FNS(mounted_snapshots, dev);
 233
 234/************************************************************************
 235 *                      NILFS checkpoints attrs                         *
 236 ************************************************************************/
 237
 238static ssize_t
 239nilfs_checkpoints_checkpoints_number_show(struct nilfs_checkpoints_attr *attr,
 240					    struct the_nilfs *nilfs,
 241					    char *buf)
 242{
 243	__u64 ncheckpoints;
 244	struct nilfs_cpstat cpstat;
 245	int err;
 246
 247	down_read(&nilfs->ns_segctor_sem);
 248	err = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
 249	up_read(&nilfs->ns_segctor_sem);
 250	if (err < 0) {
 251		nilfs_err(nilfs->ns_sb, "unable to get checkpoint stat: err=%d",
 252			  err);
 253		return err;
 254	}
 255
 256	ncheckpoints = cpstat.cs_ncps;
 257
 258	return snprintf(buf, PAGE_SIZE, "%llu\n", ncheckpoints);
 259}
 260
 261static ssize_t
 262nilfs_checkpoints_snapshots_number_show(struct nilfs_checkpoints_attr *attr,
 263					struct the_nilfs *nilfs,
 264					char *buf)
 265{
 266	__u64 nsnapshots;
 267	struct nilfs_cpstat cpstat;
 268	int err;
 269
 270	down_read(&nilfs->ns_segctor_sem);
 271	err = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
 272	up_read(&nilfs->ns_segctor_sem);
 273	if (err < 0) {
 274		nilfs_err(nilfs->ns_sb, "unable to get checkpoint stat: err=%d",
 275			  err);
 276		return err;
 277	}
 278
 279	nsnapshots = cpstat.cs_nsss;
 280
 281	return snprintf(buf, PAGE_SIZE, "%llu\n", nsnapshots);
 282}
 283
 284static ssize_t
 285nilfs_checkpoints_last_seg_checkpoint_show(struct nilfs_checkpoints_attr *attr,
 286					    struct the_nilfs *nilfs,
 287					    char *buf)
 288{
 289	__u64 last_cno;
 290
 291	spin_lock(&nilfs->ns_last_segment_lock);
 292	last_cno = nilfs->ns_last_cno;
 293	spin_unlock(&nilfs->ns_last_segment_lock);
 294
 295	return snprintf(buf, PAGE_SIZE, "%llu\n", last_cno);
 296}
 297
 298static ssize_t
 299nilfs_checkpoints_next_checkpoint_show(struct nilfs_checkpoints_attr *attr,
 300					struct the_nilfs *nilfs,
 301					char *buf)
 302{
 303	__u64 cno;
 304
 305	down_read(&nilfs->ns_segctor_sem);
 306	cno = nilfs->ns_cno;
 307	up_read(&nilfs->ns_segctor_sem);
 308
 309	return snprintf(buf, PAGE_SIZE, "%llu\n", cno);
 310}
 311
 312static const char checkpoints_readme_str[] =
 313	"The checkpoints group contains attributes that describe\n"
 314	"details about volume's checkpoints.\n\n"
 315	"(1) checkpoints_number\n\tshow number of checkpoints on volume.\n\n"
 316	"(2) snapshots_number\n\tshow number of snapshots on volume.\n\n"
 317	"(3) last_seg_checkpoint\n"
 318	"\tshow checkpoint number of the latest segment.\n\n"
 319	"(4) next_checkpoint\n\tshow next checkpoint number.\n\n";
 320
 321static ssize_t
 322nilfs_checkpoints_README_show(struct nilfs_checkpoints_attr *attr,
 323				struct the_nilfs *nilfs, char *buf)
 324{
 325	return snprintf(buf, PAGE_SIZE, checkpoints_readme_str);
 326}
 327
 328NILFS_CHECKPOINTS_RO_ATTR(checkpoints_number);
 329NILFS_CHECKPOINTS_RO_ATTR(snapshots_number);
 330NILFS_CHECKPOINTS_RO_ATTR(last_seg_checkpoint);
 331NILFS_CHECKPOINTS_RO_ATTR(next_checkpoint);
 332NILFS_CHECKPOINTS_RO_ATTR(README);
 333
 334static struct attribute *nilfs_checkpoints_attrs[] = {
 335	NILFS_CHECKPOINTS_ATTR_LIST(checkpoints_number),
 336	NILFS_CHECKPOINTS_ATTR_LIST(snapshots_number),
 337	NILFS_CHECKPOINTS_ATTR_LIST(last_seg_checkpoint),
 338	NILFS_CHECKPOINTS_ATTR_LIST(next_checkpoint),
 339	NILFS_CHECKPOINTS_ATTR_LIST(README),
 340	NULL,
 341};
 342
 343NILFS_DEV_INT_GROUP_OPS(checkpoints, dev);
 344NILFS_DEV_INT_GROUP_TYPE(checkpoints, dev);
 345NILFS_DEV_INT_GROUP_FNS(checkpoints, dev);
 346
 347/************************************************************************
 348 *                        NILFS segments attrs                          *
 349 ************************************************************************/
 350
 351static ssize_t
 352nilfs_segments_segments_number_show(struct nilfs_segments_attr *attr,
 353				     struct the_nilfs *nilfs,
 354				     char *buf)
 355{
 356	return snprintf(buf, PAGE_SIZE, "%lu\n", nilfs->ns_nsegments);
 357}
 358
 359static ssize_t
 360nilfs_segments_blocks_per_segment_show(struct nilfs_segments_attr *attr,
 361					struct the_nilfs *nilfs,
 362					char *buf)
 363{
 364	return snprintf(buf, PAGE_SIZE, "%lu\n", nilfs->ns_blocks_per_segment);
 365}
 366
 367static ssize_t
 368nilfs_segments_clean_segments_show(struct nilfs_segments_attr *attr,
 369				    struct the_nilfs *nilfs,
 370				    char *buf)
 371{
 372	unsigned long ncleansegs;
 373
 374	down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
 375	ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
 376	up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
 377
 378	return snprintf(buf, PAGE_SIZE, "%lu\n", ncleansegs);
 379}
 380
 381static ssize_t
 382nilfs_segments_dirty_segments_show(struct nilfs_segments_attr *attr,
 383				    struct the_nilfs *nilfs,
 384				    char *buf)
 385{
 386	struct nilfs_sustat sustat;
 387	int err;
 388
 389	down_read(&nilfs->ns_segctor_sem);
 390	err = nilfs_sufile_get_stat(nilfs->ns_sufile, &sustat);
 391	up_read(&nilfs->ns_segctor_sem);
 392	if (err < 0) {
 393		nilfs_err(nilfs->ns_sb, "unable to get segment stat: err=%d",
 394			  err);
 395		return err;
 396	}
 397
 398	return snprintf(buf, PAGE_SIZE, "%llu\n", sustat.ss_ndirtysegs);
 399}
 400
 401static const char segments_readme_str[] =
 402	"The segments group contains attributes that describe\n"
 403	"details about volume's segments.\n\n"
 404	"(1) segments_number\n\tshow number of segments on volume.\n\n"
 405	"(2) blocks_per_segment\n\tshow number of blocks in segment.\n\n"
 406	"(3) clean_segments\n\tshow count of clean segments.\n\n"
 407	"(4) dirty_segments\n\tshow count of dirty segments.\n\n";
 408
 409static ssize_t
 410nilfs_segments_README_show(struct nilfs_segments_attr *attr,
 411			    struct the_nilfs *nilfs,
 412			    char *buf)
 413{
 414	return snprintf(buf, PAGE_SIZE, segments_readme_str);
 415}
 416
 417NILFS_SEGMENTS_RO_ATTR(segments_number);
 418NILFS_SEGMENTS_RO_ATTR(blocks_per_segment);
 419NILFS_SEGMENTS_RO_ATTR(clean_segments);
 420NILFS_SEGMENTS_RO_ATTR(dirty_segments);
 421NILFS_SEGMENTS_RO_ATTR(README);
 422
 423static struct attribute *nilfs_segments_attrs[] = {
 424	NILFS_SEGMENTS_ATTR_LIST(segments_number),
 425	NILFS_SEGMENTS_ATTR_LIST(blocks_per_segment),
 426	NILFS_SEGMENTS_ATTR_LIST(clean_segments),
 427	NILFS_SEGMENTS_ATTR_LIST(dirty_segments),
 428	NILFS_SEGMENTS_ATTR_LIST(README),
 429	NULL,
 430};
 431
 432NILFS_DEV_INT_GROUP_OPS(segments, dev);
 433NILFS_DEV_INT_GROUP_TYPE(segments, dev);
 434NILFS_DEV_INT_GROUP_FNS(segments, dev);
 435
 436/************************************************************************
 437 *                        NILFS segctor attrs                           *
 438 ************************************************************************/
 439
 440static ssize_t
 441nilfs_segctor_last_pseg_block_show(struct nilfs_segctor_attr *attr,
 442				    struct the_nilfs *nilfs,
 443				    char *buf)
 444{
 445	sector_t last_pseg;
 446
 447	spin_lock(&nilfs->ns_last_segment_lock);
 448	last_pseg = nilfs->ns_last_pseg;
 449	spin_unlock(&nilfs->ns_last_segment_lock);
 450
 451	return snprintf(buf, PAGE_SIZE, "%llu\n",
 452			(unsigned long long)last_pseg);
 453}
 454
 455static ssize_t
 456nilfs_segctor_last_seg_sequence_show(struct nilfs_segctor_attr *attr,
 457					struct the_nilfs *nilfs,
 458					char *buf)
 459{
 460	u64 last_seq;
 461
 462	spin_lock(&nilfs->ns_last_segment_lock);
 463	last_seq = nilfs->ns_last_seq;
 464	spin_unlock(&nilfs->ns_last_segment_lock);
 465
 466	return snprintf(buf, PAGE_SIZE, "%llu\n", last_seq);
 467}
 468
 469static ssize_t
 470nilfs_segctor_last_seg_checkpoint_show(struct nilfs_segctor_attr *attr,
 471					struct the_nilfs *nilfs,
 472					char *buf)
 473{
 474	__u64 last_cno;
 475
 476	spin_lock(&nilfs->ns_last_segment_lock);
 477	last_cno = nilfs->ns_last_cno;
 478	spin_unlock(&nilfs->ns_last_segment_lock);
 479
 480	return snprintf(buf, PAGE_SIZE, "%llu\n", last_cno);
 481}
 482
 483static ssize_t
 484nilfs_segctor_current_seg_sequence_show(struct nilfs_segctor_attr *attr,
 485					struct the_nilfs *nilfs,
 486					char *buf)
 487{
 488	u64 seg_seq;
 489
 490	down_read(&nilfs->ns_segctor_sem);
 491	seg_seq = nilfs->ns_seg_seq;
 492	up_read(&nilfs->ns_segctor_sem);
 493
 494	return snprintf(buf, PAGE_SIZE, "%llu\n", seg_seq);
 495}
 496
 497static ssize_t
 498nilfs_segctor_current_last_full_seg_show(struct nilfs_segctor_attr *attr,
 499					 struct the_nilfs *nilfs,
 500					 char *buf)
 501{
 502	__u64 segnum;
 503
 504	down_read(&nilfs->ns_segctor_sem);
 505	segnum = nilfs->ns_segnum;
 506	up_read(&nilfs->ns_segctor_sem);
 507
 508	return snprintf(buf, PAGE_SIZE, "%llu\n", segnum);
 509}
 510
 511static ssize_t
 512nilfs_segctor_next_full_seg_show(struct nilfs_segctor_attr *attr,
 513				 struct the_nilfs *nilfs,
 514				 char *buf)
 515{
 516	__u64 nextnum;
 517
 518	down_read(&nilfs->ns_segctor_sem);
 519	nextnum = nilfs->ns_nextnum;
 520	up_read(&nilfs->ns_segctor_sem);
 521
 522	return snprintf(buf, PAGE_SIZE, "%llu\n", nextnum);
 523}
 524
 525static ssize_t
 526nilfs_segctor_next_pseg_offset_show(struct nilfs_segctor_attr *attr,
 527					struct the_nilfs *nilfs,
 528					char *buf)
 529{
 530	unsigned long pseg_offset;
 531
 532	down_read(&nilfs->ns_segctor_sem);
 533	pseg_offset = nilfs->ns_pseg_offset;
 534	up_read(&nilfs->ns_segctor_sem);
 535
 536	return snprintf(buf, PAGE_SIZE, "%lu\n", pseg_offset);
 537}
 538
 539static ssize_t
 540nilfs_segctor_next_checkpoint_show(struct nilfs_segctor_attr *attr,
 541					struct the_nilfs *nilfs,
 542					char *buf)
 543{
 544	__u64 cno;
 545
 546	down_read(&nilfs->ns_segctor_sem);
 547	cno = nilfs->ns_cno;
 548	up_read(&nilfs->ns_segctor_sem);
 549
 550	return snprintf(buf, PAGE_SIZE, "%llu\n", cno);
 551}
 552
 553static ssize_t
 554nilfs_segctor_last_seg_write_time_show(struct nilfs_segctor_attr *attr,
 555					struct the_nilfs *nilfs,
 556					char *buf)
 557{
 558	time64_t ctime;
 559
 560	down_read(&nilfs->ns_segctor_sem);
 561	ctime = nilfs->ns_ctime;
 562	up_read(&nilfs->ns_segctor_sem);
 563
 564	return sysfs_emit(buf, "%ptTs\n", &ctime);
 565}
 566
 567static ssize_t
 568nilfs_segctor_last_seg_write_time_secs_show(struct nilfs_segctor_attr *attr,
 569					    struct the_nilfs *nilfs,
 570					    char *buf)
 571{
 572	time64_t ctime;
 573
 574	down_read(&nilfs->ns_segctor_sem);
 575	ctime = nilfs->ns_ctime;
 576	up_read(&nilfs->ns_segctor_sem);
 577
 578	return snprintf(buf, PAGE_SIZE, "%llu\n", ctime);
 579}
 580
 581static ssize_t
 582nilfs_segctor_last_nongc_write_time_show(struct nilfs_segctor_attr *attr,
 583					 struct the_nilfs *nilfs,
 584					 char *buf)
 585{
 586	time64_t nongc_ctime;
 587
 588	down_read(&nilfs->ns_segctor_sem);
 589	nongc_ctime = nilfs->ns_nongc_ctime;
 590	up_read(&nilfs->ns_segctor_sem);
 591
 592	return sysfs_emit(buf, "%ptTs\n", &nongc_ctime);
 593}
 594
 595static ssize_t
 596nilfs_segctor_last_nongc_write_time_secs_show(struct nilfs_segctor_attr *attr,
 597						struct the_nilfs *nilfs,
 598						char *buf)
 599{
 600	time64_t nongc_ctime;
 601
 602	down_read(&nilfs->ns_segctor_sem);
 603	nongc_ctime = nilfs->ns_nongc_ctime;
 604	up_read(&nilfs->ns_segctor_sem);
 605
 606	return snprintf(buf, PAGE_SIZE, "%llu\n", nongc_ctime);
 607}
 608
 609static ssize_t
 610nilfs_segctor_dirty_data_blocks_count_show(struct nilfs_segctor_attr *attr,
 611					    struct the_nilfs *nilfs,
 612					    char *buf)
 613{
 614	u32 ndirtyblks;
 615
 616	down_read(&nilfs->ns_segctor_sem);
 617	ndirtyblks = atomic_read(&nilfs->ns_ndirtyblks);
 618	up_read(&nilfs->ns_segctor_sem);
 619
 620	return snprintf(buf, PAGE_SIZE, "%u\n", ndirtyblks);
 621}
 622
 623static const char segctor_readme_str[] =
 624	"The segctor group contains attributes that describe\n"
 625	"segctor thread activity details.\n\n"
 626	"(1) last_pseg_block\n"
 627	"\tshow start block number of the latest segment.\n\n"
 628	"(2) last_seg_sequence\n"
 629	"\tshow sequence value of the latest segment.\n\n"
 630	"(3) last_seg_checkpoint\n"
 631	"\tshow checkpoint number of the latest segment.\n\n"
 632	"(4) current_seg_sequence\n\tshow segment sequence counter.\n\n"
 633	"(5) current_last_full_seg\n"
 634	"\tshow index number of the latest full segment.\n\n"
 635	"(6) next_full_seg\n"
 636	"\tshow index number of the full segment index to be used next.\n\n"
 637	"(7) next_pseg_offset\n"
 638	"\tshow offset of next partial segment in the current full segment.\n\n"
 639	"(8) next_checkpoint\n\tshow next checkpoint number.\n\n"
 640	"(9) last_seg_write_time\n"
 641	"\tshow write time of the last segment in human-readable format.\n\n"
 642	"(10) last_seg_write_time_secs\n"
 643	"\tshow write time of the last segment in seconds.\n\n"
 644	"(11) last_nongc_write_time\n"
 645	"\tshow write time of the last segment not for cleaner operation "
 646	"in human-readable format.\n\n"
 647	"(12) last_nongc_write_time_secs\n"
 648	"\tshow write time of the last segment not for cleaner operation "
 649	"in seconds.\n\n"
 650	"(13) dirty_data_blocks_count\n"
 651	"\tshow number of dirty data blocks.\n\n";
 652
 653static ssize_t
 654nilfs_segctor_README_show(struct nilfs_segctor_attr *attr,
 655			  struct the_nilfs *nilfs, char *buf)
 656{
 657	return snprintf(buf, PAGE_SIZE, segctor_readme_str);
 658}
 659
 660NILFS_SEGCTOR_RO_ATTR(last_pseg_block);
 661NILFS_SEGCTOR_RO_ATTR(last_seg_sequence);
 662NILFS_SEGCTOR_RO_ATTR(last_seg_checkpoint);
 663NILFS_SEGCTOR_RO_ATTR(current_seg_sequence);
 664NILFS_SEGCTOR_RO_ATTR(current_last_full_seg);
 665NILFS_SEGCTOR_RO_ATTR(next_full_seg);
 666NILFS_SEGCTOR_RO_ATTR(next_pseg_offset);
 667NILFS_SEGCTOR_RO_ATTR(next_checkpoint);
 668NILFS_SEGCTOR_RO_ATTR(last_seg_write_time);
 669NILFS_SEGCTOR_RO_ATTR(last_seg_write_time_secs);
 670NILFS_SEGCTOR_RO_ATTR(last_nongc_write_time);
 671NILFS_SEGCTOR_RO_ATTR(last_nongc_write_time_secs);
 672NILFS_SEGCTOR_RO_ATTR(dirty_data_blocks_count);
 673NILFS_SEGCTOR_RO_ATTR(README);
 674
 675static struct attribute *nilfs_segctor_attrs[] = {
 676	NILFS_SEGCTOR_ATTR_LIST(last_pseg_block),
 677	NILFS_SEGCTOR_ATTR_LIST(last_seg_sequence),
 678	NILFS_SEGCTOR_ATTR_LIST(last_seg_checkpoint),
 679	NILFS_SEGCTOR_ATTR_LIST(current_seg_sequence),
 680	NILFS_SEGCTOR_ATTR_LIST(current_last_full_seg),
 681	NILFS_SEGCTOR_ATTR_LIST(next_full_seg),
 682	NILFS_SEGCTOR_ATTR_LIST(next_pseg_offset),
 683	NILFS_SEGCTOR_ATTR_LIST(next_checkpoint),
 684	NILFS_SEGCTOR_ATTR_LIST(last_seg_write_time),
 685	NILFS_SEGCTOR_ATTR_LIST(last_seg_write_time_secs),
 686	NILFS_SEGCTOR_ATTR_LIST(last_nongc_write_time),
 687	NILFS_SEGCTOR_ATTR_LIST(last_nongc_write_time_secs),
 688	NILFS_SEGCTOR_ATTR_LIST(dirty_data_blocks_count),
 689	NILFS_SEGCTOR_ATTR_LIST(README),
 690	NULL,
 691};
 692
 693NILFS_DEV_INT_GROUP_OPS(segctor, dev);
 694NILFS_DEV_INT_GROUP_TYPE(segctor, dev);
 695NILFS_DEV_INT_GROUP_FNS(segctor, dev);
 696
 697/************************************************************************
 698 *                        NILFS superblock attrs                        *
 699 ************************************************************************/
 700
 701static ssize_t
 702nilfs_superblock_sb_write_time_show(struct nilfs_superblock_attr *attr,
 703				     struct the_nilfs *nilfs,
 704				     char *buf)
 705{
 706	time64_t sbwtime;
 707
 708	down_read(&nilfs->ns_sem);
 709	sbwtime = nilfs->ns_sbwtime;
 710	up_read(&nilfs->ns_sem);
 711
 712	return sysfs_emit(buf, "%ptTs\n", &sbwtime);
 713}
 714
 715static ssize_t
 716nilfs_superblock_sb_write_time_secs_show(struct nilfs_superblock_attr *attr,
 717					 struct the_nilfs *nilfs,
 718					 char *buf)
 719{
 720	time64_t sbwtime;
 721
 722	down_read(&nilfs->ns_sem);
 723	sbwtime = nilfs->ns_sbwtime;
 724	up_read(&nilfs->ns_sem);
 725
 726	return snprintf(buf, PAGE_SIZE, "%llu\n", sbwtime);
 727}
 728
 729static ssize_t
 730nilfs_superblock_sb_write_count_show(struct nilfs_superblock_attr *attr,
 731				      struct the_nilfs *nilfs,
 732				      char *buf)
 733{
 734	unsigned int sbwcount;
 735
 736	down_read(&nilfs->ns_sem);
 737	sbwcount = nilfs->ns_sbwcount;
 738	up_read(&nilfs->ns_sem);
 739
 740	return snprintf(buf, PAGE_SIZE, "%u\n", sbwcount);
 741}
 742
 743static ssize_t
 744nilfs_superblock_sb_update_frequency_show(struct nilfs_superblock_attr *attr,
 745					    struct the_nilfs *nilfs,
 746					    char *buf)
 747{
 748	unsigned int sb_update_freq;
 749
 750	down_read(&nilfs->ns_sem);
 751	sb_update_freq = nilfs->ns_sb_update_freq;
 752	up_read(&nilfs->ns_sem);
 753
 754	return snprintf(buf, PAGE_SIZE, "%u\n", sb_update_freq);
 755}
 756
 757static ssize_t
 758nilfs_superblock_sb_update_frequency_store(struct nilfs_superblock_attr *attr,
 759					    struct the_nilfs *nilfs,
 760					    const char *buf, size_t count)
 761{
 762	unsigned int val;
 763	int err;
 764
 765	err = kstrtouint(skip_spaces(buf), 0, &val);
 766	if (err) {
 767		nilfs_err(nilfs->ns_sb, "unable to convert string: err=%d",
 768			  err);
 769		return err;
 770	}
 771
 772	if (val < NILFS_SB_FREQ) {
 773		val = NILFS_SB_FREQ;
 774		nilfs_warn(nilfs->ns_sb,
 775			   "superblock update frequency cannot be lesser than 10 seconds");
 776	}
 777
 778	down_write(&nilfs->ns_sem);
 779	nilfs->ns_sb_update_freq = val;
 780	up_write(&nilfs->ns_sem);
 781
 782	return count;
 783}
 784
 785static const char sb_readme_str[] =
 786	"The superblock group contains attributes that describe\n"
 787	"superblock's details.\n\n"
 788	"(1) sb_write_time\n\tshow previous write time of super block "
 789	"in human-readable format.\n\n"
 790	"(2) sb_write_time_secs\n\tshow previous write time of super block "
 791	"in seconds.\n\n"
 792	"(3) sb_write_count\n\tshow write count of super block.\n\n"
 793	"(4) sb_update_frequency\n"
 794	"\tshow/set interval of periodical update of superblock (in seconds).\n\n"
 795	"\tYou can set preferable frequency of superblock update by command:\n\n"
 796	"\t'echo <val> > /sys/fs/<nilfs>/<dev>/superblock/sb_update_frequency'\n";
 797
 798static ssize_t
 799nilfs_superblock_README_show(struct nilfs_superblock_attr *attr,
 800				struct the_nilfs *nilfs, char *buf)
 801{
 802	return snprintf(buf, PAGE_SIZE, sb_readme_str);
 803}
 804
 805NILFS_SUPERBLOCK_RO_ATTR(sb_write_time);
 806NILFS_SUPERBLOCK_RO_ATTR(sb_write_time_secs);
 807NILFS_SUPERBLOCK_RO_ATTR(sb_write_count);
 808NILFS_SUPERBLOCK_RW_ATTR(sb_update_frequency);
 809NILFS_SUPERBLOCK_RO_ATTR(README);
 810
 811static struct attribute *nilfs_superblock_attrs[] = {
 812	NILFS_SUPERBLOCK_ATTR_LIST(sb_write_time),
 813	NILFS_SUPERBLOCK_ATTR_LIST(sb_write_time_secs),
 814	NILFS_SUPERBLOCK_ATTR_LIST(sb_write_count),
 815	NILFS_SUPERBLOCK_ATTR_LIST(sb_update_frequency),
 816	NILFS_SUPERBLOCK_ATTR_LIST(README),
 817	NULL,
 818};
 819
 820NILFS_DEV_INT_GROUP_OPS(superblock, dev);
 821NILFS_DEV_INT_GROUP_TYPE(superblock, dev);
 822NILFS_DEV_INT_GROUP_FNS(superblock, dev);
 823
 824/************************************************************************
 825 *                        NILFS device attrs                            *
 826 ************************************************************************/
 827
 828static
 829ssize_t nilfs_dev_revision_show(struct nilfs_dev_attr *attr,
 830				struct the_nilfs *nilfs,
 831				char *buf)
 832{
 833	struct nilfs_super_block **sbp = nilfs->ns_sbp;
 834	u32 major = le32_to_cpu(sbp[0]->s_rev_level);
 835	u16 minor = le16_to_cpu(sbp[0]->s_minor_rev_level);
 836
 837	return snprintf(buf, PAGE_SIZE, "%d.%d\n", major, minor);
 838}
 839
 840static
 841ssize_t nilfs_dev_blocksize_show(struct nilfs_dev_attr *attr,
 842				 struct the_nilfs *nilfs,
 843				 char *buf)
 844{
 845	return snprintf(buf, PAGE_SIZE, "%u\n", nilfs->ns_blocksize);
 846}
 847
 848static
 849ssize_t nilfs_dev_device_size_show(struct nilfs_dev_attr *attr,
 850				    struct the_nilfs *nilfs,
 851				    char *buf)
 852{
 853	struct nilfs_super_block **sbp = nilfs->ns_sbp;
 854	u64 dev_size = le64_to_cpu(sbp[0]->s_dev_size);
 855
 856	return snprintf(buf, PAGE_SIZE, "%llu\n", dev_size);
 857}
 858
 859static
 860ssize_t nilfs_dev_free_blocks_show(struct nilfs_dev_attr *attr,
 861				   struct the_nilfs *nilfs,
 862				   char *buf)
 863{
 864	sector_t free_blocks = 0;
 865
 866	nilfs_count_free_blocks(nilfs, &free_blocks);
 867	return snprintf(buf, PAGE_SIZE, "%llu\n",
 868			(unsigned long long)free_blocks);
 869}
 870
 871static
 872ssize_t nilfs_dev_uuid_show(struct nilfs_dev_attr *attr,
 873			    struct the_nilfs *nilfs,
 874			    char *buf)
 875{
 876	struct nilfs_super_block **sbp = nilfs->ns_sbp;
 877
 878	return snprintf(buf, PAGE_SIZE, "%pUb\n", sbp[0]->s_uuid);
 879}
 880
 881static
 882ssize_t nilfs_dev_volume_name_show(struct nilfs_dev_attr *attr,
 883				    struct the_nilfs *nilfs,
 884				    char *buf)
 885{
 886	struct nilfs_super_block **sbp = nilfs->ns_sbp;
 887
 888	return scnprintf(buf, sizeof(sbp[0]->s_volume_name), "%s\n",
 889			 sbp[0]->s_volume_name);
 890}
 891
 892static const char dev_readme_str[] =
 893	"The <device> group contains attributes that describe file system\n"
 894	"partition's details.\n\n"
 895	"(1) revision\n\tshow NILFS file system revision.\n\n"
 896	"(2) blocksize\n\tshow volume block size in bytes.\n\n"
 897	"(3) device_size\n\tshow volume size in bytes.\n\n"
 898	"(4) free_blocks\n\tshow count of free blocks on volume.\n\n"
 899	"(5) uuid\n\tshow volume's UUID.\n\n"
 900	"(6) volume_name\n\tshow volume's name.\n\n";
 901
 902static ssize_t nilfs_dev_README_show(struct nilfs_dev_attr *attr,
 903				     struct the_nilfs *nilfs,
 904				     char *buf)
 905{
 906	return snprintf(buf, PAGE_SIZE, dev_readme_str);
 907}
 908
 909NILFS_DEV_RO_ATTR(revision);
 910NILFS_DEV_RO_ATTR(blocksize);
 911NILFS_DEV_RO_ATTR(device_size);
 912NILFS_DEV_RO_ATTR(free_blocks);
 913NILFS_DEV_RO_ATTR(uuid);
 914NILFS_DEV_RO_ATTR(volume_name);
 915NILFS_DEV_RO_ATTR(README);
 916
 917static struct attribute *nilfs_dev_attrs[] = {
 918	NILFS_DEV_ATTR_LIST(revision),
 919	NILFS_DEV_ATTR_LIST(blocksize),
 920	NILFS_DEV_ATTR_LIST(device_size),
 921	NILFS_DEV_ATTR_LIST(free_blocks),
 922	NILFS_DEV_ATTR_LIST(uuid),
 923	NILFS_DEV_ATTR_LIST(volume_name),
 924	NILFS_DEV_ATTR_LIST(README),
 925	NULL,
 926};
 927
 928static ssize_t nilfs_dev_attr_show(struct kobject *kobj,
 929				    struct attribute *attr, char *buf)
 930{
 931	struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
 932						ns_dev_kobj);
 933	struct nilfs_dev_attr *a = container_of(attr, struct nilfs_dev_attr,
 934						attr);
 935
 936	return a->show ? a->show(a, nilfs, buf) : 0;
 937}
 938
 939static ssize_t nilfs_dev_attr_store(struct kobject *kobj,
 940				    struct attribute *attr,
 941				    const char *buf, size_t len)
 942{
 943	struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
 944						ns_dev_kobj);
 945	struct nilfs_dev_attr *a = container_of(attr, struct nilfs_dev_attr,
 946						attr);
 947
 948	return a->store ? a->store(a, nilfs, buf, len) : 0;
 949}
 950
 951static void nilfs_dev_attr_release(struct kobject *kobj)
 952{
 953	struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
 954						ns_dev_kobj);
 955	complete(&nilfs->ns_dev_kobj_unregister);
 956}
 957
 958static const struct sysfs_ops nilfs_dev_attr_ops = {
 959	.show	= nilfs_dev_attr_show,
 960	.store	= nilfs_dev_attr_store,
 961};
 962
 963static struct kobj_type nilfs_dev_ktype = {
 964	.default_attrs	= nilfs_dev_attrs,
 965	.sysfs_ops	= &nilfs_dev_attr_ops,
 966	.release	= nilfs_dev_attr_release,
 967};
 968
 969int nilfs_sysfs_create_device_group(struct super_block *sb)
 970{
 971	struct the_nilfs *nilfs = sb->s_fs_info;
 972	size_t devgrp_size = sizeof(struct nilfs_sysfs_dev_subgroups);
 973	int err;
 974
 975	nilfs->ns_dev_subgroups = kzalloc(devgrp_size, GFP_KERNEL);
 976	if (unlikely(!nilfs->ns_dev_subgroups)) {
 977		err = -ENOMEM;
 978		nilfs_err(sb, "unable to allocate memory for device group");
 
 979		goto failed_create_device_group;
 980	}
 981
 982	nilfs->ns_dev_kobj.kset = nilfs_kset;
 983	init_completion(&nilfs->ns_dev_kobj_unregister);
 984	err = kobject_init_and_add(&nilfs->ns_dev_kobj, &nilfs_dev_ktype, NULL,
 985				    "%s", sb->s_id);
 986	if (err)
 987		goto cleanup_dev_kobject;
 988
 989	err = nilfs_sysfs_create_mounted_snapshots_group(nilfs);
 990	if (err)
 991		goto cleanup_dev_kobject;
 992
 993	err = nilfs_sysfs_create_checkpoints_group(nilfs);
 994	if (err)
 995		goto delete_mounted_snapshots_group;
 996
 997	err = nilfs_sysfs_create_segments_group(nilfs);
 998	if (err)
 999		goto delete_checkpoints_group;
1000
1001	err = nilfs_sysfs_create_superblock_group(nilfs);
1002	if (err)
1003		goto delete_segments_group;
1004
1005	err = nilfs_sysfs_create_segctor_group(nilfs);
1006	if (err)
1007		goto delete_superblock_group;
1008
1009	return 0;
1010
1011delete_superblock_group:
1012	nilfs_sysfs_delete_superblock_group(nilfs);
1013
1014delete_segments_group:
1015	nilfs_sysfs_delete_segments_group(nilfs);
1016
1017delete_checkpoints_group:
1018	nilfs_sysfs_delete_checkpoints_group(nilfs);
1019
1020delete_mounted_snapshots_group:
1021	nilfs_sysfs_delete_mounted_snapshots_group(nilfs);
1022
1023cleanup_dev_kobject:
1024	kobject_put(&nilfs->ns_dev_kobj);
 
 
1025	kfree(nilfs->ns_dev_subgroups);
1026
1027failed_create_device_group:
1028	return err;
1029}
1030
1031void nilfs_sysfs_delete_device_group(struct the_nilfs *nilfs)
1032{
1033	nilfs_sysfs_delete_mounted_snapshots_group(nilfs);
1034	nilfs_sysfs_delete_checkpoints_group(nilfs);
1035	nilfs_sysfs_delete_segments_group(nilfs);
1036	nilfs_sysfs_delete_superblock_group(nilfs);
1037	nilfs_sysfs_delete_segctor_group(nilfs);
1038	kobject_del(&nilfs->ns_dev_kobj);
1039	kobject_put(&nilfs->ns_dev_kobj);
1040	kfree(nilfs->ns_dev_subgroups);
1041}
1042
1043/************************************************************************
1044 *                        NILFS feature attrs                           *
1045 ************************************************************************/
1046
1047static ssize_t nilfs_feature_revision_show(struct kobject *kobj,
1048					    struct attribute *attr, char *buf)
1049{
1050	return snprintf(buf, PAGE_SIZE, "%d.%d\n",
1051			NILFS_CURRENT_REV, NILFS_MINOR_REV);
1052}
1053
1054static const char features_readme_str[] =
1055	"The features group contains attributes that describe NILFS file\n"
1056	"system driver features.\n\n"
1057	"(1) revision\n\tshow current revision of NILFS file system driver.\n";
1058
1059static ssize_t nilfs_feature_README_show(struct kobject *kobj,
1060					 struct attribute *attr,
1061					 char *buf)
1062{
1063	return snprintf(buf, PAGE_SIZE, features_readme_str);
1064}
1065
1066NILFS_FEATURE_RO_ATTR(revision);
1067NILFS_FEATURE_RO_ATTR(README);
1068
1069static struct attribute *nilfs_feature_attrs[] = {
1070	NILFS_FEATURE_ATTR_LIST(revision),
1071	NILFS_FEATURE_ATTR_LIST(README),
1072	NULL,
1073};
1074
1075static const struct attribute_group nilfs_feature_attr_group = {
1076	.name = "features",
1077	.attrs = nilfs_feature_attrs,
1078};
1079
1080int __init nilfs_sysfs_init(void)
1081{
1082	int err;
1083
1084	nilfs_kset = kset_create_and_add(NILFS_ROOT_GROUP_NAME, NULL, fs_kobj);
1085	if (!nilfs_kset) {
1086		err = -ENOMEM;
1087		nilfs_err(NULL, "unable to create sysfs entry: err=%d", err);
 
1088		goto failed_sysfs_init;
1089	}
1090
1091	err = sysfs_create_group(&nilfs_kset->kobj, &nilfs_feature_attr_group);
1092	if (unlikely(err)) {
1093		nilfs_err(NULL, "unable to create feature group: err=%d", err);
 
1094		goto cleanup_sysfs_init;
1095	}
1096
1097	return 0;
1098
1099cleanup_sysfs_init:
1100	kset_unregister(nilfs_kset);
1101
1102failed_sysfs_init:
1103	return err;
1104}
1105
1106void nilfs_sysfs_exit(void)
1107{
1108	sysfs_remove_group(&nilfs_kset->kobj, &nilfs_feature_attr_group);
1109	kset_unregister(nilfs_kset);
1110}