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