1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * sysfs.c - sysfs support implementation.
4  *
5  * Copyright (C) 2005-2014 Nippon Telegraph and Telephone Corporation.
6  * Copyright (C) 2014 HGST, Inc., a Western Digital Company.
7  *
8  * Written by Vyacheslav Dubeyko <Vyacheslav.Dubeyko@hgst.com>
9  */
10 
11 #include <linux/kobject.h>
12 
13 #include "nilfs.h"
14 #include "mdt.h"
15 #include "sufile.h"
16 #include "cpfile.h"
17 #include "sysfs.h"
18 
19 /* /sys/fs/<nilfs>/ */
20 static struct kset *nilfs_kset;
21 
22 #define NILFS_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) \
36 static 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 } \
47 static 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 } \
59 static 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) \
65 static 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 } \
72 static 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) \
79 static 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 } \
98 static 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 
107 static ssize_t
nilfs_snapshot_inodes_count_show(struct nilfs_snapshot_attr * attr,struct nilfs_root * root,char * buf)108 nilfs_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 
115 static ssize_t
nilfs_snapshot_blocks_count_show(struct nilfs_snapshot_attr * attr,struct nilfs_root * root,char * buf)116 nilfs_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 
123 static 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 
128 static ssize_t
nilfs_snapshot_README_show(struct nilfs_snapshot_attr * attr,struct nilfs_root * root,char * buf)129 nilfs_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 
135 NILFS_SNAPSHOT_RO_ATTR(inodes_count);
136 NILFS_SNAPSHOT_RO_ATTR(blocks_count);
137 NILFS_SNAPSHOT_RO_ATTR(README);
138 
139 static 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 
nilfs_snapshot_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)146 static 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 
nilfs_snapshot_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t len)157 static 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 
nilfs_snapshot_attr_release(struct kobject * kobj)169 static 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 
176 static const struct sysfs_ops nilfs_snapshot_attr_ops = {
177 	.show	= nilfs_snapshot_attr_show,
178 	.store	= nilfs_snapshot_attr_store,
179 };
180 
181 static 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 
nilfs_sysfs_create_snapshot_group(struct nilfs_root * root)187 int 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 
nilfs_sysfs_delete_snapshot_group(struct nilfs_root * root)216 void 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 
225 static const char mounted_snapshots_readme_str[] =
226 	"The mounted_snapshots group contains group for\n"
227 	"every mounted snapshot.\n";
228 
229 static ssize_t
nilfs_mounted_snapshots_README_show(struct nilfs_mounted_snapshots_attr * attr,struct the_nilfs * nilfs,char * buf)230 nilfs_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 
236 NILFS_MOUNTED_SNAPSHOTS_RO_ATTR(README);
237 
238 static struct attribute *nilfs_mounted_snapshots_attrs[] = {
239 	NILFS_MOUNTED_SNAPSHOTS_ATTR_LIST(README),
240 	NULL,
241 };
242 
243 NILFS_DEV_INT_GROUP_OPS(mounted_snapshots, dev);
244 NILFS_DEV_INT_GROUP_TYPE(mounted_snapshots, dev);
245 NILFS_DEV_INT_GROUP_FNS(mounted_snapshots, dev);
246 
247 /************************************************************************
248  *                      NILFS checkpoints attrs                         *
249  ************************************************************************/
250 
251 static ssize_t
nilfs_checkpoints_checkpoints_number_show(struct nilfs_checkpoints_attr * attr,struct the_nilfs * nilfs,char * buf)252 nilfs_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_msg(nilfs->ns_sb, KERN_ERR,
265 			  "unable to get checkpoint stat: err=%d", err);
266 		return err;
267 	}
268 
269 	ncheckpoints = cpstat.cs_ncps;
270 
271 	return snprintf(buf, PAGE_SIZE, "%llu\n", ncheckpoints);
272 }
273 
274 static ssize_t
nilfs_checkpoints_snapshots_number_show(struct nilfs_checkpoints_attr * attr,struct the_nilfs * nilfs,char * buf)275 nilfs_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_msg(nilfs->ns_sb, KERN_ERR,
288 			  "unable to get checkpoint stat: err=%d", err);
289 		return err;
290 	}
291 
292 	nsnapshots = cpstat.cs_nsss;
293 
294 	return snprintf(buf, PAGE_SIZE, "%llu\n", nsnapshots);
295 }
296 
297 static ssize_t
nilfs_checkpoints_last_seg_checkpoint_show(struct nilfs_checkpoints_attr * attr,struct the_nilfs * nilfs,char * buf)298 nilfs_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 
311 static ssize_t
nilfs_checkpoints_next_checkpoint_show(struct nilfs_checkpoints_attr * attr,struct the_nilfs * nilfs,char * buf)312 nilfs_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 
325 static 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 
334 static ssize_t
nilfs_checkpoints_README_show(struct nilfs_checkpoints_attr * attr,struct the_nilfs * nilfs,char * buf)335 nilfs_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 
341 NILFS_CHECKPOINTS_RO_ATTR(checkpoints_number);
342 NILFS_CHECKPOINTS_RO_ATTR(snapshots_number);
343 NILFS_CHECKPOINTS_RO_ATTR(last_seg_checkpoint);
344 NILFS_CHECKPOINTS_RO_ATTR(next_checkpoint);
345 NILFS_CHECKPOINTS_RO_ATTR(README);
346 
347 static 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 
356 NILFS_DEV_INT_GROUP_OPS(checkpoints, dev);
357 NILFS_DEV_INT_GROUP_TYPE(checkpoints, dev);
358 NILFS_DEV_INT_GROUP_FNS(checkpoints, dev);
359 
360 /************************************************************************
361  *                        NILFS segments attrs                          *
362  ************************************************************************/
363 
364 static ssize_t
nilfs_segments_segments_number_show(struct nilfs_segments_attr * attr,struct the_nilfs * nilfs,char * buf)365 nilfs_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 
372 static ssize_t
nilfs_segments_blocks_per_segment_show(struct nilfs_segments_attr * attr,struct the_nilfs * nilfs,char * buf)373 nilfs_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 
380 static ssize_t
nilfs_segments_clean_segments_show(struct nilfs_segments_attr * attr,struct the_nilfs * nilfs,char * buf)381 nilfs_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 
394 static ssize_t
nilfs_segments_dirty_segments_show(struct nilfs_segments_attr * attr,struct the_nilfs * nilfs,char * buf)395 nilfs_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_msg(nilfs->ns_sb, KERN_ERR,
407 			  "unable to get segment stat: err=%d", err);
408 		return err;
409 	}
410 
411 	return snprintf(buf, PAGE_SIZE, "%llu\n", sustat.ss_ndirtysegs);
412 }
413 
414 static 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 
422 static ssize_t
nilfs_segments_README_show(struct nilfs_segments_attr * attr,struct the_nilfs * nilfs,char * buf)423 nilfs_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 
430 NILFS_SEGMENTS_RO_ATTR(segments_number);
431 NILFS_SEGMENTS_RO_ATTR(blocks_per_segment);
432 NILFS_SEGMENTS_RO_ATTR(clean_segments);
433 NILFS_SEGMENTS_RO_ATTR(dirty_segments);
434 NILFS_SEGMENTS_RO_ATTR(README);
435 
436 static 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 
445 NILFS_DEV_INT_GROUP_OPS(segments, dev);
446 NILFS_DEV_INT_GROUP_TYPE(segments, dev);
447 NILFS_DEV_INT_GROUP_FNS(segments, dev);
448 
449 /************************************************************************
450  *                        NILFS segctor attrs                           *
451  ************************************************************************/
452 
453 static ssize_t
nilfs_segctor_last_pseg_block_show(struct nilfs_segctor_attr * attr,struct the_nilfs * nilfs,char * buf)454 nilfs_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 
468 static ssize_t
nilfs_segctor_last_seg_sequence_show(struct nilfs_segctor_attr * attr,struct the_nilfs * nilfs,char * buf)469 nilfs_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 
482 static ssize_t
nilfs_segctor_last_seg_checkpoint_show(struct nilfs_segctor_attr * attr,struct the_nilfs * nilfs,char * buf)483 nilfs_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 
496 static ssize_t
nilfs_segctor_current_seg_sequence_show(struct nilfs_segctor_attr * attr,struct the_nilfs * nilfs,char * buf)497 nilfs_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 
510 static ssize_t
nilfs_segctor_current_last_full_seg_show(struct nilfs_segctor_attr * attr,struct the_nilfs * nilfs,char * buf)511 nilfs_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 
524 static ssize_t
nilfs_segctor_next_full_seg_show(struct nilfs_segctor_attr * attr,struct the_nilfs * nilfs,char * buf)525 nilfs_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 
538 static ssize_t
nilfs_segctor_next_pseg_offset_show(struct nilfs_segctor_attr * attr,struct the_nilfs * nilfs,char * buf)539 nilfs_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 
552 static ssize_t
nilfs_segctor_next_checkpoint_show(struct nilfs_segctor_attr * attr,struct the_nilfs * nilfs,char * buf)553 nilfs_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 
566 static ssize_t
nilfs_segctor_last_seg_write_time_show(struct nilfs_segctor_attr * attr,struct the_nilfs * nilfs,char * buf)567 nilfs_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 
580 static ssize_t
nilfs_segctor_last_seg_write_time_secs_show(struct nilfs_segctor_attr * attr,struct the_nilfs * nilfs,char * buf)581 nilfs_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 
594 static ssize_t
nilfs_segctor_last_nongc_write_time_show(struct nilfs_segctor_attr * attr,struct the_nilfs * nilfs,char * buf)595 nilfs_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 
608 static ssize_t
nilfs_segctor_last_nongc_write_time_secs_show(struct nilfs_segctor_attr * attr,struct the_nilfs * nilfs,char * buf)609 nilfs_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 
622 static ssize_t
nilfs_segctor_dirty_data_blocks_count_show(struct nilfs_segctor_attr * attr,struct the_nilfs * nilfs,char * buf)623 nilfs_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 
636 static 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 
666 static ssize_t
nilfs_segctor_README_show(struct nilfs_segctor_attr * attr,struct the_nilfs * nilfs,char * buf)667 nilfs_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 
673 NILFS_SEGCTOR_RO_ATTR(last_pseg_block);
674 NILFS_SEGCTOR_RO_ATTR(last_seg_sequence);
675 NILFS_SEGCTOR_RO_ATTR(last_seg_checkpoint);
676 NILFS_SEGCTOR_RO_ATTR(current_seg_sequence);
677 NILFS_SEGCTOR_RO_ATTR(current_last_full_seg);
678 NILFS_SEGCTOR_RO_ATTR(next_full_seg);
679 NILFS_SEGCTOR_RO_ATTR(next_pseg_offset);
680 NILFS_SEGCTOR_RO_ATTR(next_checkpoint);
681 NILFS_SEGCTOR_RO_ATTR(last_seg_write_time);
682 NILFS_SEGCTOR_RO_ATTR(last_seg_write_time_secs);
683 NILFS_SEGCTOR_RO_ATTR(last_nongc_write_time);
684 NILFS_SEGCTOR_RO_ATTR(last_nongc_write_time_secs);
685 NILFS_SEGCTOR_RO_ATTR(dirty_data_blocks_count);
686 NILFS_SEGCTOR_RO_ATTR(README);
687 
688 static 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 
706 NILFS_DEV_INT_GROUP_OPS(segctor, dev);
707 NILFS_DEV_INT_GROUP_TYPE(segctor, dev);
708 NILFS_DEV_INT_GROUP_FNS(segctor, dev);
709 
710 /************************************************************************
711  *                        NILFS superblock attrs                        *
712  ************************************************************************/
713 
714 static ssize_t
nilfs_superblock_sb_write_time_show(struct nilfs_superblock_attr * attr,struct the_nilfs * nilfs,char * buf)715 nilfs_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 
728 static ssize_t
nilfs_superblock_sb_write_time_secs_show(struct nilfs_superblock_attr * attr,struct the_nilfs * nilfs,char * buf)729 nilfs_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 
742 static ssize_t
nilfs_superblock_sb_write_count_show(struct nilfs_superblock_attr * attr,struct the_nilfs * nilfs,char * buf)743 nilfs_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 
756 static ssize_t
nilfs_superblock_sb_update_frequency_show(struct nilfs_superblock_attr * attr,struct the_nilfs * nilfs,char * buf)757 nilfs_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 
770 static ssize_t
nilfs_superblock_sb_update_frequency_store(struct nilfs_superblock_attr * attr,struct the_nilfs * nilfs,const char * buf,size_t count)771 nilfs_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_msg(nilfs->ns_sb, KERN_ERR,
781 			  "unable to convert string: err=%d", err);
782 		return err;
783 	}
784 
785 	if (val < NILFS_SB_FREQ) {
786 		val = NILFS_SB_FREQ;
787 		nilfs_msg(nilfs->ns_sb, KERN_WARNING,
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 
798 static 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 
811 static ssize_t
nilfs_superblock_README_show(struct nilfs_superblock_attr * attr,struct the_nilfs * nilfs,char * buf)812 nilfs_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 
818 NILFS_SUPERBLOCK_RO_ATTR(sb_write_time);
819 NILFS_SUPERBLOCK_RO_ATTR(sb_write_time_secs);
820 NILFS_SUPERBLOCK_RO_ATTR(sb_write_count);
821 NILFS_SUPERBLOCK_RW_ATTR(sb_update_frequency);
822 NILFS_SUPERBLOCK_RO_ATTR(README);
823 
824 static 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 
833 NILFS_DEV_INT_GROUP_OPS(superblock, dev);
834 NILFS_DEV_INT_GROUP_TYPE(superblock, dev);
835 NILFS_DEV_INT_GROUP_FNS(superblock, dev);
836 
837 /************************************************************************
838  *                        NILFS device attrs                            *
839  ************************************************************************/
840 
841 static
nilfs_dev_revision_show(struct nilfs_dev_attr * attr,struct the_nilfs * nilfs,char * buf)842 ssize_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 
853 static
nilfs_dev_blocksize_show(struct nilfs_dev_attr * attr,struct the_nilfs * nilfs,char * buf)854 ssize_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 
861 static
nilfs_dev_device_size_show(struct nilfs_dev_attr * attr,struct the_nilfs * nilfs,char * buf)862 ssize_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 
872 static
nilfs_dev_free_blocks_show(struct nilfs_dev_attr * attr,struct the_nilfs * nilfs,char * buf)873 ssize_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 
884 static
nilfs_dev_uuid_show(struct nilfs_dev_attr * attr,struct the_nilfs * nilfs,char * buf)885 ssize_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 
894 static
nilfs_dev_volume_name_show(struct nilfs_dev_attr * attr,struct the_nilfs * nilfs,char * buf)895 ssize_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 
905 static 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 
nilfs_dev_README_show(struct nilfs_dev_attr * attr,struct the_nilfs * nilfs,char * buf)915 static 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 
922 NILFS_DEV_RO_ATTR(revision);
923 NILFS_DEV_RO_ATTR(blocksize);
924 NILFS_DEV_RO_ATTR(device_size);
925 NILFS_DEV_RO_ATTR(free_blocks);
926 NILFS_DEV_RO_ATTR(uuid);
927 NILFS_DEV_RO_ATTR(volume_name);
928 NILFS_DEV_RO_ATTR(README);
929 
930 static 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 
nilfs_dev_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)941 static 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 
nilfs_dev_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t len)952 static 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 
nilfs_dev_attr_release(struct kobject * kobj)964 static 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 
971 static const struct sysfs_ops nilfs_dev_attr_ops = {
972 	.show	= nilfs_dev_attr_show,
973 	.store	= nilfs_dev_attr_store,
974 };
975 
976 static 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 
nilfs_sysfs_create_device_group(struct super_block * sb)982 int 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;
991 		nilfs_msg(sb, KERN_ERR,
992 			  "unable to allocate memory for device group");
993 		goto failed_create_device_group;
994 	}
995 
996 	nilfs->ns_dev_kobj.kset = nilfs_kset;
997 	init_completion(&nilfs->ns_dev_kobj_unregister);
998 	err = kobject_init_and_add(&nilfs->ns_dev_kobj, &nilfs_dev_ktype, NULL,
999 				    "%s", sb->s_id);
1000 	if (err)
1001 		goto cleanup_dev_kobject;
1002 
1003 	err = nilfs_sysfs_create_mounted_snapshots_group(nilfs);
1004 	if (err)
1005 		goto cleanup_dev_kobject;
1006 
1007 	err = nilfs_sysfs_create_checkpoints_group(nilfs);
1008 	if (err)
1009 		goto delete_mounted_snapshots_group;
1010 
1011 	err = nilfs_sysfs_create_segments_group(nilfs);
1012 	if (err)
1013 		goto delete_checkpoints_group;
1014 
1015 	err = nilfs_sysfs_create_superblock_group(nilfs);
1016 	if (err)
1017 		goto delete_segments_group;
1018 
1019 	err = nilfs_sysfs_create_segctor_group(nilfs);
1020 	if (err)
1021 		goto delete_superblock_group;
1022 
1023 	return 0;
1024 
1025 delete_superblock_group:
1026 	nilfs_sysfs_delete_superblock_group(nilfs);
1027 
1028 delete_segments_group:
1029 	nilfs_sysfs_delete_segments_group(nilfs);
1030 
1031 delete_checkpoints_group:
1032 	nilfs_sysfs_delete_checkpoints_group(nilfs);
1033 
1034 delete_mounted_snapshots_group:
1035 	nilfs_sysfs_delete_mounted_snapshots_group(nilfs);
1036 
1037 cleanup_dev_kobject:
1038 	kobject_put(&nilfs->ns_dev_kobj);
1039 	kfree(nilfs->ns_dev_subgroups);
1040 
1041 failed_create_device_group:
1042 	return err;
1043 }
1044 
nilfs_sysfs_delete_device_group(struct the_nilfs * nilfs)1045 void nilfs_sysfs_delete_device_group(struct the_nilfs *nilfs)
1046 {
1047 	nilfs_sysfs_delete_mounted_snapshots_group(nilfs);
1048 	nilfs_sysfs_delete_checkpoints_group(nilfs);
1049 	nilfs_sysfs_delete_segments_group(nilfs);
1050 	nilfs_sysfs_delete_superblock_group(nilfs);
1051 	nilfs_sysfs_delete_segctor_group(nilfs);
1052 	kobject_del(&nilfs->ns_dev_kobj);
1053 	kobject_put(&nilfs->ns_dev_kobj);
1054 	kfree(nilfs->ns_dev_subgroups);
1055 }
1056 
1057 /************************************************************************
1058  *                        NILFS feature attrs                           *
1059  ************************************************************************/
1060 
nilfs_feature_revision_show(struct kobject * kobj,struct attribute * attr,char * buf)1061 static ssize_t nilfs_feature_revision_show(struct kobject *kobj,
1062 					    struct attribute *attr, char *buf)
1063 {
1064 	return snprintf(buf, PAGE_SIZE, "%d.%d\n",
1065 			NILFS_CURRENT_REV, NILFS_MINOR_REV);
1066 }
1067 
1068 static const char features_readme_str[] =
1069 	"The features group contains attributes that describe NILFS file\n"
1070 	"system driver features.\n\n"
1071 	"(1) revision\n\tshow current revision of NILFS file system driver.\n";
1072 
nilfs_feature_README_show(struct kobject * kobj,struct attribute * attr,char * buf)1073 static ssize_t nilfs_feature_README_show(struct kobject *kobj,
1074 					 struct attribute *attr,
1075 					 char *buf)
1076 {
1077 	return snprintf(buf, PAGE_SIZE, features_readme_str);
1078 }
1079 
1080 NILFS_FEATURE_RO_ATTR(revision);
1081 NILFS_FEATURE_RO_ATTR(README);
1082 
1083 static struct attribute *nilfs_feature_attrs[] = {
1084 	NILFS_FEATURE_ATTR_LIST(revision),
1085 	NILFS_FEATURE_ATTR_LIST(README),
1086 	NULL,
1087 };
1088 
1089 static const struct attribute_group nilfs_feature_attr_group = {
1090 	.name = "features",
1091 	.attrs = nilfs_feature_attrs,
1092 };
1093 
nilfs_sysfs_init(void)1094 int __init nilfs_sysfs_init(void)
1095 {
1096 	int err;
1097 
1098 	nilfs_kset = kset_create_and_add(NILFS_ROOT_GROUP_NAME, NULL, fs_kobj);
1099 	if (!nilfs_kset) {
1100 		err = -ENOMEM;
1101 		nilfs_msg(NULL, KERN_ERR,
1102 			  "unable to create sysfs entry: err=%d", err);
1103 		goto failed_sysfs_init;
1104 	}
1105 
1106 	err = sysfs_create_group(&nilfs_kset->kobj, &nilfs_feature_attr_group);
1107 	if (unlikely(err)) {
1108 		nilfs_msg(NULL, KERN_ERR,
1109 			  "unable to create feature group: err=%d", err);
1110 		goto cleanup_sysfs_init;
1111 	}
1112 
1113 	return 0;
1114 
1115 cleanup_sysfs_init:
1116 	kset_unregister(nilfs_kset);
1117 
1118 failed_sysfs_init:
1119 	return err;
1120 }
1121 
nilfs_sysfs_exit(void)1122 void nilfs_sysfs_exit(void)
1123 {
1124 	sysfs_remove_group(&nilfs_kset->kobj, &nilfs_feature_attr_group);
1125 	kset_unregister(nilfs_kset);
1126 }
1127