blob: 57afd06db62de020b4aaeb6980c21701047a7e2c [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// 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{ \
Olivier Deprez157378f2022-04-04 15:47:50 +020067 struct nilfs_sysfs_##parent_name##_subgroups *subgroups = container_of(kobj, \
68 struct nilfs_sysfs_##parent_name##_subgroups, \
69 sg_##name##_kobj); \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000070 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) \
Olivier Deprez157378f2022-04-04 15:47:50 +020095 kobject_put(kobj); \
96 return err; \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000097} \
98static void nilfs_sysfs_delete_##name##_group(struct the_nilfs *nilfs) \
99{ \
Olivier Deprez157378f2022-04-04 15:47:50 +0200100 kobject_put(&nilfs->ns_##parent_name##_subgroups->sg_##name##_kobj); \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000101}
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 snprintf(buf, PAGE_SIZE, "%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 snprintf(buf, PAGE_SIZE, "%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 snprintf(buf, PAGE_SIZE, 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)
Olivier Deprez157378f2022-04-04 15:47:50 +0200211 kobject_put(&root->snapshot_kobj);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000212
Olivier Deprez157378f2022-04-04 15:47:50 +0200213 return err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000214}
215
216void nilfs_sysfs_delete_snapshot_group(struct nilfs_root *root)
217{
Olivier Deprez157378f2022-04-04 15:47:50 +0200218 kobject_put(&root->snapshot_kobj);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000219}
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 snprintf(buf, PAGE_SIZE, 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) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200264 nilfs_err(nilfs->ns_sb, "unable to get checkpoint stat: err=%d",
265 err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000266 return err;
267 }
268
269 ncheckpoints = cpstat.cs_ncps;
270
271 return snprintf(buf, PAGE_SIZE, "%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) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200287 nilfs_err(nilfs->ns_sb, "unable to get checkpoint stat: err=%d",
288 err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000289 return err;
290 }
291
292 nsnapshots = cpstat.cs_nsss;
293
294 return snprintf(buf, PAGE_SIZE, "%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 snprintf(buf, PAGE_SIZE, "%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 snprintf(buf, PAGE_SIZE, "%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 snprintf(buf, PAGE_SIZE, 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 snprintf(buf, PAGE_SIZE, "%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 snprintf(buf, PAGE_SIZE, "%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 snprintf(buf, PAGE_SIZE, "%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) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200406 nilfs_err(nilfs->ns_sb, "unable to get segment stat: err=%d",
407 err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000408 return err;
409 }
410
411 return snprintf(buf, PAGE_SIZE, "%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 snprintf(buf, PAGE_SIZE, 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 snprintf(buf, PAGE_SIZE, "%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 snprintf(buf, PAGE_SIZE, "%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 snprintf(buf, PAGE_SIZE, "%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 snprintf(buf, PAGE_SIZE, "%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 snprintf(buf, PAGE_SIZE, "%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 snprintf(buf, PAGE_SIZE, "%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 snprintf(buf, PAGE_SIZE, "%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 snprintf(buf, PAGE_SIZE, "%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 snprintf(buf, PAGE_SIZE, "%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 snprintf(buf, PAGE_SIZE, "%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 snprintf(buf, PAGE_SIZE, "%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 snprintf(buf, PAGE_SIZE, 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 snprintf(buf, PAGE_SIZE, "%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 snprintf(buf, PAGE_SIZE, "%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 snprintf(buf, PAGE_SIZE, "%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) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200780 nilfs_err(nilfs->ns_sb, "unable to convert string: err=%d",
781 err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000782 return err;
783 }
784
785 if (val < NILFS_SB_FREQ) {
786 val = NILFS_SB_FREQ;
Olivier Deprez157378f2022-04-04 15:47:50 +0200787 nilfs_warn(nilfs->ns_sb,
788 "superblock update frequency cannot be lesser than 10 seconds");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000789 }
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 snprintf(buf, PAGE_SIZE, 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 **sbp = nilfs->ns_sbp;
847 u32 major = le32_to_cpu(sbp[0]->s_rev_level);
848 u16 minor = le16_to_cpu(sbp[0]->s_minor_rev_level);
849
850 return snprintf(buf, PAGE_SIZE, "%d.%d\n", major, minor);
851}
852
853static
854ssize_t nilfs_dev_blocksize_show(struct nilfs_dev_attr *attr,
855 struct the_nilfs *nilfs,
856 char *buf)
857{
858 return snprintf(buf, PAGE_SIZE, "%u\n", nilfs->ns_blocksize);
859}
860
861static
862ssize_t nilfs_dev_device_size_show(struct nilfs_dev_attr *attr,
863 struct the_nilfs *nilfs,
864 char *buf)
865{
866 struct nilfs_super_block **sbp = nilfs->ns_sbp;
867 u64 dev_size = le64_to_cpu(sbp[0]->s_dev_size);
868
869 return snprintf(buf, PAGE_SIZE, "%llu\n", dev_size);
870}
871
872static
873ssize_t nilfs_dev_free_blocks_show(struct nilfs_dev_attr *attr,
874 struct the_nilfs *nilfs,
875 char *buf)
876{
877 sector_t free_blocks = 0;
878
879 nilfs_count_free_blocks(nilfs, &free_blocks);
880 return snprintf(buf, PAGE_SIZE, "%llu\n",
881 (unsigned long long)free_blocks);
882}
883
884static
885ssize_t nilfs_dev_uuid_show(struct nilfs_dev_attr *attr,
886 struct the_nilfs *nilfs,
887 char *buf)
888{
889 struct nilfs_super_block **sbp = nilfs->ns_sbp;
890
891 return snprintf(buf, PAGE_SIZE, "%pUb\n", sbp[0]->s_uuid);
892}
893
894static
895ssize_t nilfs_dev_volume_name_show(struct nilfs_dev_attr *attr,
896 struct the_nilfs *nilfs,
897 char *buf)
898{
899 struct nilfs_super_block **sbp = nilfs->ns_sbp;
900
901 return scnprintf(buf, sizeof(sbp[0]->s_volume_name), "%s\n",
902 sbp[0]->s_volume_name);
903}
904
905static const char dev_readme_str[] =
906 "The <device> group contains attributes that describe file system\n"
907 "partition's details.\n\n"
908 "(1) revision\n\tshow NILFS file system revision.\n\n"
909 "(2) blocksize\n\tshow volume block size in bytes.\n\n"
910 "(3) device_size\n\tshow volume size in bytes.\n\n"
911 "(4) free_blocks\n\tshow count of free blocks on volume.\n\n"
912 "(5) uuid\n\tshow volume's UUID.\n\n"
913 "(6) volume_name\n\tshow volume's name.\n\n";
914
915static ssize_t nilfs_dev_README_show(struct nilfs_dev_attr *attr,
916 struct the_nilfs *nilfs,
917 char *buf)
918{
919 return snprintf(buf, PAGE_SIZE, dev_readme_str);
920}
921
922NILFS_DEV_RO_ATTR(revision);
923NILFS_DEV_RO_ATTR(blocksize);
924NILFS_DEV_RO_ATTR(device_size);
925NILFS_DEV_RO_ATTR(free_blocks);
926NILFS_DEV_RO_ATTR(uuid);
927NILFS_DEV_RO_ATTR(volume_name);
928NILFS_DEV_RO_ATTR(README);
929
930static struct attribute *nilfs_dev_attrs[] = {
931 NILFS_DEV_ATTR_LIST(revision),
932 NILFS_DEV_ATTR_LIST(blocksize),
933 NILFS_DEV_ATTR_LIST(device_size),
934 NILFS_DEV_ATTR_LIST(free_blocks),
935 NILFS_DEV_ATTR_LIST(uuid),
936 NILFS_DEV_ATTR_LIST(volume_name),
937 NILFS_DEV_ATTR_LIST(README),
938 NULL,
939};
940
941static ssize_t nilfs_dev_attr_show(struct kobject *kobj,
942 struct attribute *attr, char *buf)
943{
944 struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
945 ns_dev_kobj);
946 struct nilfs_dev_attr *a = container_of(attr, struct nilfs_dev_attr,
947 attr);
948
949 return a->show ? a->show(a, nilfs, buf) : 0;
950}
951
952static ssize_t nilfs_dev_attr_store(struct kobject *kobj,
953 struct attribute *attr,
954 const char *buf, size_t len)
955{
956 struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
957 ns_dev_kobj);
958 struct nilfs_dev_attr *a = container_of(attr, struct nilfs_dev_attr,
959 attr);
960
961 return a->store ? a->store(a, nilfs, buf, len) : 0;
962}
963
964static void nilfs_dev_attr_release(struct kobject *kobj)
965{
966 struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
967 ns_dev_kobj);
968 complete(&nilfs->ns_dev_kobj_unregister);
969}
970
971static const struct sysfs_ops nilfs_dev_attr_ops = {
972 .show = nilfs_dev_attr_show,
973 .store = nilfs_dev_attr_store,
974};
975
976static struct kobj_type nilfs_dev_ktype = {
977 .default_attrs = nilfs_dev_attrs,
978 .sysfs_ops = &nilfs_dev_attr_ops,
979 .release = nilfs_dev_attr_release,
980};
981
982int nilfs_sysfs_create_device_group(struct super_block *sb)
983{
984 struct the_nilfs *nilfs = sb->s_fs_info;
985 size_t devgrp_size = sizeof(struct nilfs_sysfs_dev_subgroups);
986 int err;
987
988 nilfs->ns_dev_subgroups = kzalloc(devgrp_size, GFP_KERNEL);
989 if (unlikely(!nilfs->ns_dev_subgroups)) {
990 err = -ENOMEM;
Olivier Deprez157378f2022-04-04 15:47:50 +0200991 nilfs_err(sb, "unable to allocate memory for device group");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000992 goto failed_create_device_group;
993 }
994
995 nilfs->ns_dev_kobj.kset = nilfs_kset;
996 init_completion(&nilfs->ns_dev_kobj_unregister);
997 err = kobject_init_and_add(&nilfs->ns_dev_kobj, &nilfs_dev_ktype, NULL,
998 "%s", sb->s_id);
999 if (err)
Olivier Deprez157378f2022-04-04 15:47:50 +02001000 goto cleanup_dev_kobject;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001001
1002 err = nilfs_sysfs_create_mounted_snapshots_group(nilfs);
1003 if (err)
1004 goto cleanup_dev_kobject;
1005
1006 err = nilfs_sysfs_create_checkpoints_group(nilfs);
1007 if (err)
1008 goto delete_mounted_snapshots_group;
1009
1010 err = nilfs_sysfs_create_segments_group(nilfs);
1011 if (err)
1012 goto delete_checkpoints_group;
1013
1014 err = nilfs_sysfs_create_superblock_group(nilfs);
1015 if (err)
1016 goto delete_segments_group;
1017
1018 err = nilfs_sysfs_create_segctor_group(nilfs);
1019 if (err)
1020 goto delete_superblock_group;
1021
1022 return 0;
1023
1024delete_superblock_group:
1025 nilfs_sysfs_delete_superblock_group(nilfs);
1026
1027delete_segments_group:
1028 nilfs_sysfs_delete_segments_group(nilfs);
1029
1030delete_checkpoints_group:
1031 nilfs_sysfs_delete_checkpoints_group(nilfs);
1032
1033delete_mounted_snapshots_group:
1034 nilfs_sysfs_delete_mounted_snapshots_group(nilfs);
1035
1036cleanup_dev_kobject:
Olivier Deprez157378f2022-04-04 15:47:50 +02001037 kobject_put(&nilfs->ns_dev_kobj);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001038 kfree(nilfs->ns_dev_subgroups);
1039
1040failed_create_device_group:
1041 return err;
1042}
1043
1044void nilfs_sysfs_delete_device_group(struct the_nilfs *nilfs)
1045{
1046 nilfs_sysfs_delete_mounted_snapshots_group(nilfs);
1047 nilfs_sysfs_delete_checkpoints_group(nilfs);
1048 nilfs_sysfs_delete_segments_group(nilfs);
1049 nilfs_sysfs_delete_superblock_group(nilfs);
1050 nilfs_sysfs_delete_segctor_group(nilfs);
1051 kobject_del(&nilfs->ns_dev_kobj);
Olivier Deprez0e641232021-09-23 10:07:05 +02001052 kobject_put(&nilfs->ns_dev_kobj);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001053 kfree(nilfs->ns_dev_subgroups);
1054}
1055
1056/************************************************************************
1057 * NILFS feature attrs *
1058 ************************************************************************/
1059
1060static ssize_t nilfs_feature_revision_show(struct kobject *kobj,
1061 struct attribute *attr, char *buf)
1062{
1063 return snprintf(buf, PAGE_SIZE, "%d.%d\n",
1064 NILFS_CURRENT_REV, NILFS_MINOR_REV);
1065}
1066
1067static const char features_readme_str[] =
1068 "The features group contains attributes that describe NILFS file\n"
1069 "system driver features.\n\n"
1070 "(1) revision\n\tshow current revision of NILFS file system driver.\n";
1071
1072static ssize_t nilfs_feature_README_show(struct kobject *kobj,
1073 struct attribute *attr,
1074 char *buf)
1075{
1076 return snprintf(buf, PAGE_SIZE, features_readme_str);
1077}
1078
1079NILFS_FEATURE_RO_ATTR(revision);
1080NILFS_FEATURE_RO_ATTR(README);
1081
1082static struct attribute *nilfs_feature_attrs[] = {
1083 NILFS_FEATURE_ATTR_LIST(revision),
1084 NILFS_FEATURE_ATTR_LIST(README),
1085 NULL,
1086};
1087
1088static const struct attribute_group nilfs_feature_attr_group = {
1089 .name = "features",
1090 .attrs = nilfs_feature_attrs,
1091};
1092
1093int __init nilfs_sysfs_init(void)
1094{
1095 int err;
1096
1097 nilfs_kset = kset_create_and_add(NILFS_ROOT_GROUP_NAME, NULL, fs_kobj);
1098 if (!nilfs_kset) {
1099 err = -ENOMEM;
Olivier Deprez157378f2022-04-04 15:47:50 +02001100 nilfs_err(NULL, "unable to create sysfs entry: err=%d", err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001101 goto failed_sysfs_init;
1102 }
1103
1104 err = sysfs_create_group(&nilfs_kset->kobj, &nilfs_feature_attr_group);
1105 if (unlikely(err)) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001106 nilfs_err(NULL, "unable to create feature group: err=%d", err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001107 goto cleanup_sysfs_init;
1108 }
1109
1110 return 0;
1111
1112cleanup_sysfs_init:
1113 kset_unregister(nilfs_kset);
1114
1115failed_sysfs_init:
1116 return err;
1117}
1118
1119void nilfs_sysfs_exit(void)
1120{
1121 sysfs_remove_group(&nilfs_kset->kobj, &nilfs_feature_attr_group);
1122 kset_unregister(nilfs_kset);
1123}