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