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