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