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