1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * f2fs sysfs interface
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
7 * Copyright (c) 2017 Chao Yu <chao@kernel.org>
8 */
9 #include <linux/compiler.h>
10 #include <linux/proc_fs.h>
11 #include <linux/f2fs_fs.h>
12 #include <linux/seq_file.h>
13 #include <linux/unicode.h>
14
15 #include "f2fs.h"
16 #include "segment.h"
17 #include "gc.h"
18 #include <trace/events/f2fs.h>
19
20 static struct proc_dir_entry *f2fs_proc_root;
21
22 /* Sysfs support for f2fs */
23 enum {
24 GC_THREAD, /* struct f2fs_gc_thread */
25 SM_INFO, /* struct f2fs_sm_info */
26 DCC_INFO, /* struct discard_cmd_control */
27 NM_INFO, /* struct f2fs_nm_info */
28 F2FS_SBI, /* struct f2fs_sb_info */
29 #ifdef CONFIG_F2FS_STAT_FS
30 STAT_INFO, /* struct f2fs_stat_info */
31 #endif
32 #ifdef CONFIG_F2FS_FAULT_INJECTION
33 FAULT_INFO_RATE, /* struct f2fs_fault_info */
34 FAULT_INFO_TYPE, /* struct f2fs_fault_info */
35 #endif
36 RESERVED_BLOCKS, /* struct f2fs_sb_info */
37 #ifdef CONFIG_F2FS_GRADING_SSR
38 F2FS_HOT_COLD_PARAMS, /* struct f2fs_hot_cold_params */
39 #endif
40 };
41
42 struct f2fs_attr {
43 struct attribute attr;
44 ssize_t (*show)(struct f2fs_attr *, struct f2fs_sb_info *, char *);
45 ssize_t (*store)(struct f2fs_attr *, struct f2fs_sb_info *,
46 const char *, size_t);
47 int struct_type;
48 int offset;
49 int id;
50 };
51
52 static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
53 struct f2fs_sb_info *sbi, char *buf);
54
__struct_ptr(struct f2fs_sb_info * sbi,int struct_type)55 static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type)
56 {
57 if (struct_type == GC_THREAD)
58 return (unsigned char *)sbi->gc_thread;
59 else if (struct_type == SM_INFO)
60 return (unsigned char *)SM_I(sbi);
61 else if (struct_type == DCC_INFO)
62 return (unsigned char *)SM_I(sbi)->dcc_info;
63 else if (struct_type == NM_INFO)
64 return (unsigned char *)NM_I(sbi);
65 else if (struct_type == F2FS_SBI || struct_type == RESERVED_BLOCKS)
66 return (unsigned char *)sbi;
67 #ifdef CONFIG_F2FS_GRADING_SSR
68 else if (struct_type == F2FS_HOT_COLD_PARAMS)
69 return (unsigned char *)&sbi->hot_cold_params;
70 #endif
71 #ifdef CONFIG_F2FS_FAULT_INJECTION
72 else if (struct_type == FAULT_INFO_RATE ||
73 struct_type == FAULT_INFO_TYPE)
74 return (unsigned char *)&F2FS_OPTION(sbi).fault_info;
75 #endif
76 #ifdef CONFIG_F2FS_STAT_FS
77 else if (struct_type == STAT_INFO)
78 return (unsigned char *)F2FS_STAT(sbi);
79 #endif
80 return NULL;
81 }
82
dirty_segments_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)83 static ssize_t dirty_segments_show(struct f2fs_attr *a,
84 struct f2fs_sb_info *sbi, char *buf)
85 {
86 return sprintf(buf, "%llu\n",
87 (unsigned long long)(dirty_segments(sbi)));
88 }
89
free_segments_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)90 static ssize_t free_segments_show(struct f2fs_attr *a,
91 struct f2fs_sb_info *sbi, char *buf)
92 {
93 return sprintf(buf, "%llu\n",
94 (unsigned long long)(free_segments(sbi)));
95 }
96
lifetime_write_kbytes_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)97 static ssize_t lifetime_write_kbytes_show(struct f2fs_attr *a,
98 struct f2fs_sb_info *sbi, char *buf)
99 {
100 struct super_block *sb = sbi->sb;
101
102 if (!sb->s_bdev->bd_part)
103 return sprintf(buf, "0\n");
104
105 return sprintf(buf, "%llu\n",
106 (unsigned long long)(sbi->kbytes_written +
107 BD_PART_WRITTEN(sbi)));
108 }
109
features_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)110 static ssize_t features_show(struct f2fs_attr *a,
111 struct f2fs_sb_info *sbi, char *buf)
112 {
113 struct super_block *sb = sbi->sb;
114 int len = 0;
115
116 if (!sb->s_bdev->bd_part)
117 return sprintf(buf, "0\n");
118
119 if (f2fs_sb_has_encrypt(sbi))
120 len += scnprintf(buf, PAGE_SIZE - len, "%s",
121 "encryption");
122 if (f2fs_sb_has_blkzoned(sbi))
123 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
124 len ? ", " : "", "blkzoned");
125 if (f2fs_sb_has_extra_attr(sbi))
126 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
127 len ? ", " : "", "extra_attr");
128 if (f2fs_sb_has_project_quota(sbi))
129 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
130 len ? ", " : "", "projquota");
131 if (f2fs_sb_has_inode_chksum(sbi))
132 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
133 len ? ", " : "", "inode_checksum");
134 if (f2fs_sb_has_flexible_inline_xattr(sbi))
135 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
136 len ? ", " : "", "flexible_inline_xattr");
137 if (f2fs_sb_has_quota_ino(sbi))
138 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
139 len ? ", " : "", "quota_ino");
140 if (f2fs_sb_has_inode_crtime(sbi))
141 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
142 len ? ", " : "", "inode_crtime");
143 if (f2fs_sb_has_lost_found(sbi))
144 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
145 len ? ", " : "", "lost_found");
146 if (f2fs_sb_has_verity(sbi))
147 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
148 len ? ", " : "", "verity");
149 if (f2fs_sb_has_sb_chksum(sbi))
150 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
151 len ? ", " : "", "sb_checksum");
152 if (f2fs_sb_has_casefold(sbi))
153 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
154 len ? ", " : "", "casefold");
155 if (f2fs_sb_has_compression(sbi))
156 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
157 len ? ", " : "", "compression");
158 len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
159 len ? ", " : "", "pin_file");
160 len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
161 return len;
162 }
163
current_reserved_blocks_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)164 static ssize_t current_reserved_blocks_show(struct f2fs_attr *a,
165 struct f2fs_sb_info *sbi, char *buf)
166 {
167 return sprintf(buf, "%u\n", sbi->current_reserved_blocks);
168 }
169
unusable_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)170 static ssize_t unusable_show(struct f2fs_attr *a,
171 struct f2fs_sb_info *sbi, char *buf)
172 {
173 block_t unusable;
174
175 if (test_opt(sbi, DISABLE_CHECKPOINT))
176 unusable = sbi->unusable_block_count;
177 else
178 unusable = f2fs_get_unusable_blocks(sbi);
179 return sprintf(buf, "%llu\n", (unsigned long long)unusable);
180 }
181
encoding_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)182 static ssize_t encoding_show(struct f2fs_attr *a,
183 struct f2fs_sb_info *sbi, char *buf)
184 {
185 #ifdef CONFIG_UNICODE
186 struct super_block *sb = sbi->sb;
187
188 if (f2fs_sb_has_casefold(sbi))
189 return snprintf(buf, PAGE_SIZE, "%s (%d.%d.%d)\n",
190 sb->s_encoding->charset,
191 (sb->s_encoding->version >> 16) & 0xff,
192 (sb->s_encoding->version >> 8) & 0xff,
193 sb->s_encoding->version & 0xff);
194 #endif
195 return sprintf(buf, "(none)");
196 }
197
mounted_time_sec_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)198 static ssize_t mounted_time_sec_show(struct f2fs_attr *a,
199 struct f2fs_sb_info *sbi, char *buf)
200 {
201 return sprintf(buf, "%llu", SIT_I(sbi)->mounted_time);
202 }
203
204 #ifdef CONFIG_F2FS_STAT_FS
moved_blocks_foreground_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)205 static ssize_t moved_blocks_foreground_show(struct f2fs_attr *a,
206 struct f2fs_sb_info *sbi, char *buf)
207 {
208 struct f2fs_stat_info *si = F2FS_STAT(sbi);
209
210 return sprintf(buf, "%llu\n",
211 (unsigned long long)(si->tot_blks -
212 (si->bg_data_blks + si->bg_node_blks)));
213 }
214
moved_blocks_background_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)215 static ssize_t moved_blocks_background_show(struct f2fs_attr *a,
216 struct f2fs_sb_info *sbi, char *buf)
217 {
218 struct f2fs_stat_info *si = F2FS_STAT(sbi);
219
220 return sprintf(buf, "%llu\n",
221 (unsigned long long)(si->bg_data_blks + si->bg_node_blks));
222 }
223
avg_vblocks_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)224 static ssize_t avg_vblocks_show(struct f2fs_attr *a,
225 struct f2fs_sb_info *sbi, char *buf)
226 {
227 struct f2fs_stat_info *si = F2FS_STAT(sbi);
228
229 si->dirty_count = dirty_segments(sbi);
230 f2fs_update_sit_info(sbi);
231 return sprintf(buf, "%llu\n", (unsigned long long)(si->avg_vblocks));
232 }
233 #endif
234
main_blkaddr_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)235 static ssize_t main_blkaddr_show(struct f2fs_attr *a,
236 struct f2fs_sb_info *sbi, char *buf)
237 {
238 return snprintf(buf, PAGE_SIZE, "%llu\n",
239 (unsigned long long)MAIN_BLKADDR(sbi));
240 }
241
f2fs_sbi_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)242 static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
243 struct f2fs_sb_info *sbi, char *buf)
244 {
245 unsigned char *ptr = NULL;
246 unsigned int *ui;
247
248 ptr = __struct_ptr(sbi, a->struct_type);
249 if (!ptr)
250 return -EINVAL;
251
252 if (!strcmp(a->attr.name, "extension_list")) {
253 __u8 (*extlist)[F2FS_EXTENSION_LEN] =
254 sbi->raw_super->extension_list;
255 int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
256 int hot_count = sbi->raw_super->hot_ext_count;
257 int len = 0, i;
258
259 len += scnprintf(buf + len, PAGE_SIZE - len,
260 "cold file extension:\n");
261 for (i = 0; i < cold_count; i++)
262 len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
263 extlist[i]);
264
265 len += scnprintf(buf + len, PAGE_SIZE - len,
266 "hot file extension:\n");
267 for (i = cold_count; i < cold_count + hot_count; i++)
268 len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
269 extlist[i]);
270 return len;
271 }
272
273 ui = (unsigned int *)(ptr + a->offset);
274
275 return sprintf(buf, "%u\n", *ui);
276 }
277
__sbi_store(struct f2fs_attr * a,struct f2fs_sb_info * sbi,const char * buf,size_t count)278 static ssize_t __sbi_store(struct f2fs_attr *a,
279 struct f2fs_sb_info *sbi,
280 const char *buf, size_t count)
281 {
282 unsigned char *ptr;
283 unsigned long t;
284 unsigned int *ui;
285 ssize_t ret;
286
287 ptr = __struct_ptr(sbi, a->struct_type);
288 if (!ptr)
289 return -EINVAL;
290
291 if (!strcmp(a->attr.name, "extension_list")) {
292 const char *name = strim((char *)buf);
293 bool set = true, hot;
294
295 if (!strncmp(name, "[h]", 3))
296 hot = true;
297 else if (!strncmp(name, "[c]", 3))
298 hot = false;
299 else
300 return -EINVAL;
301
302 name += 3;
303
304 if (*name == '!') {
305 name++;
306 set = false;
307 }
308
309 if (strlen(name) >= F2FS_EXTENSION_LEN)
310 return -EINVAL;
311
312 down_write(&sbi->sb_lock);
313
314 ret = f2fs_update_extension_list(sbi, name, hot, set);
315 if (ret)
316 goto out;
317
318 ret = f2fs_commit_super(sbi, false);
319 if (ret)
320 f2fs_update_extension_list(sbi, name, hot, !set);
321 out:
322 up_write(&sbi->sb_lock);
323 return ret ? ret : count;
324 }
325
326 ui = (unsigned int *)(ptr + a->offset);
327
328 ret = kstrtoul(skip_spaces(buf), 0, &t);
329 if (ret < 0)
330 return ret;
331 #ifdef CONFIG_F2FS_FAULT_INJECTION
332 if (a->struct_type == FAULT_INFO_TYPE && t >= (1 << FAULT_MAX))
333 return -EINVAL;
334 if (a->struct_type == FAULT_INFO_RATE && t >= UINT_MAX)
335 return -EINVAL;
336 #endif
337 if (a->struct_type == RESERVED_BLOCKS) {
338 spin_lock(&sbi->stat_lock);
339 if (t > (unsigned long)(sbi->user_block_count -
340 F2FS_OPTION(sbi).root_reserved_blocks)) {
341 spin_unlock(&sbi->stat_lock);
342 return -EINVAL;
343 }
344 *ui = t;
345 sbi->current_reserved_blocks = min(sbi->reserved_blocks,
346 sbi->user_block_count - valid_user_blocks(sbi));
347 spin_unlock(&sbi->stat_lock);
348 return count;
349 }
350
351 if (!strcmp(a->attr.name, "discard_granularity")) {
352 if (t == 0 || t > MAX_PLIST_NUM)
353 return -EINVAL;
354 if (t == *ui)
355 return count;
356 *ui = t;
357 return count;
358 }
359
360 if (!strcmp(a->attr.name, "migration_granularity")) {
361 if (t == 0 || t > sbi->segs_per_sec)
362 return -EINVAL;
363 }
364
365 if (!strcmp(a->attr.name, "trim_sections"))
366 return -EINVAL;
367
368 if (!strcmp(a->attr.name, "gc_urgent")) {
369 if (t == 0) {
370 sbi->gc_mode = GC_NORMAL;
371 } else if (t == 1) {
372 sbi->gc_mode = GC_URGENT_HIGH;
373 if (sbi->gc_thread) {
374 sbi->gc_thread->gc_wake = 1;
375 wake_up_interruptible_all(
376 &sbi->gc_thread->gc_wait_queue_head);
377 wake_up_discard_thread(sbi, true);
378 }
379 } else if (t == 2) {
380 sbi->gc_mode = GC_URGENT_LOW;
381 } else {
382 return -EINVAL;
383 }
384 return count;
385 }
386 if (!strcmp(a->attr.name, "gc_idle")) {
387 if (t == GC_IDLE_CB) {
388 sbi->gc_mode = GC_IDLE_CB;
389 } else if (t == GC_IDLE_GREEDY) {
390 sbi->gc_mode = GC_IDLE_GREEDY;
391 } else if (t == GC_IDLE_AT) {
392 if (!sbi->am.atgc_enabled)
393 return -EINVAL;
394 sbi->gc_mode = GC_AT;
395 } else {
396 sbi->gc_mode = GC_NORMAL;
397 }
398 return count;
399 }
400
401 if (!strcmp(a->attr.name, "iostat_enable")) {
402 sbi->iostat_enable = !!t;
403 if (!sbi->iostat_enable)
404 f2fs_reset_iostat(sbi);
405 return count;
406 }
407
408 if (!strcmp(a->attr.name, "iostat_period_ms")) {
409 if (t < MIN_IOSTAT_PERIOD_MS || t > MAX_IOSTAT_PERIOD_MS)
410 return -EINVAL;
411 spin_lock(&sbi->iostat_lock);
412 sbi->iostat_period_ms = (unsigned int)t;
413 spin_unlock(&sbi->iostat_lock);
414 return count;
415 }
416
417 *ui = (unsigned int)t;
418
419 return count;
420 }
421
f2fs_sbi_store(struct f2fs_attr * a,struct f2fs_sb_info * sbi,const char * buf,size_t count)422 static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
423 struct f2fs_sb_info *sbi,
424 const char *buf, size_t count)
425 {
426 ssize_t ret;
427 bool gc_entry = (!strcmp(a->attr.name, "gc_urgent") ||
428 a->struct_type == GC_THREAD);
429
430 if (gc_entry) {
431 if (!down_read_trylock(&sbi->sb->s_umount))
432 return -EAGAIN;
433 }
434 ret = __sbi_store(a, sbi, buf, count);
435 if (gc_entry)
436 up_read(&sbi->sb->s_umount);
437
438 return ret;
439 }
440
f2fs_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)441 static ssize_t f2fs_attr_show(struct kobject *kobj,
442 struct attribute *attr, char *buf)
443 {
444 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
445 s_kobj);
446 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
447
448 return a->show ? a->show(a, sbi, buf) : 0;
449 }
450
f2fs_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t len)451 static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
452 const char *buf, size_t len)
453 {
454 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
455 s_kobj);
456 struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
457
458 return a->store ? a->store(a, sbi, buf, len) : 0;
459 }
460
f2fs_sb_release(struct kobject * kobj)461 static void f2fs_sb_release(struct kobject *kobj)
462 {
463 struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
464 s_kobj);
465 complete(&sbi->s_kobj_unregister);
466 }
467
468 enum feat_id {
469 FEAT_CRYPTO = 0,
470 FEAT_BLKZONED,
471 FEAT_ATOMIC_WRITE,
472 FEAT_EXTRA_ATTR,
473 FEAT_PROJECT_QUOTA,
474 FEAT_INODE_CHECKSUM,
475 FEAT_FLEXIBLE_INLINE_XATTR,
476 FEAT_QUOTA_INO,
477 FEAT_INODE_CRTIME,
478 FEAT_LOST_FOUND,
479 FEAT_VERITY,
480 FEAT_SB_CHECKSUM,
481 FEAT_CASEFOLD,
482 FEAT_COMPRESSION,
483 FEAT_TEST_DUMMY_ENCRYPTION_V2,
484 };
485
f2fs_feature_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)486 static ssize_t f2fs_feature_show(struct f2fs_attr *a,
487 struct f2fs_sb_info *sbi, char *buf)
488 {
489 switch (a->id) {
490 case FEAT_CRYPTO:
491 case FEAT_BLKZONED:
492 case FEAT_ATOMIC_WRITE:
493 case FEAT_EXTRA_ATTR:
494 case FEAT_PROJECT_QUOTA:
495 case FEAT_INODE_CHECKSUM:
496 case FEAT_FLEXIBLE_INLINE_XATTR:
497 case FEAT_QUOTA_INO:
498 case FEAT_INODE_CRTIME:
499 case FEAT_LOST_FOUND:
500 case FEAT_VERITY:
501 case FEAT_SB_CHECKSUM:
502 case FEAT_CASEFOLD:
503 case FEAT_COMPRESSION:
504 case FEAT_TEST_DUMMY_ENCRYPTION_V2:
505 return sprintf(buf, "supported\n");
506 }
507 return 0;
508 }
509
510 #define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
511 static struct f2fs_attr f2fs_attr_##_name = { \
512 .attr = {.name = __stringify(_name), .mode = _mode }, \
513 .show = _show, \
514 .store = _store, \
515 .struct_type = _struct_type, \
516 .offset = _offset \
517 }
518
519 #define F2FS_RW_ATTR(struct_type, struct_name, name, elname) \
520 F2FS_ATTR_OFFSET(struct_type, name, 0644, \
521 f2fs_sbi_show, f2fs_sbi_store, \
522 offsetof(struct struct_name, elname))
523
524 #define F2FS_GENERAL_RO_ATTR(name) \
525 static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
526
527 #define F2FS_FEATURE_RO_ATTR(_name, _id) \
528 static struct f2fs_attr f2fs_attr_##_name = { \
529 .attr = {.name = __stringify(_name), .mode = 0444 }, \
530 .show = f2fs_feature_show, \
531 .id = _id, \
532 }
533
534 #define F2FS_STAT_ATTR(_struct_type, _struct_name, _name, _elname) \
535 static struct f2fs_attr f2fs_attr_##_name = { \
536 .attr = {.name = __stringify(_name), .mode = 0444 }, \
537 .show = f2fs_sbi_show, \
538 .struct_type = _struct_type, \
539 .offset = offsetof(struct _struct_name, _elname), \
540 }
541
542 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_urgent_sleep_time,
543 urgent_sleep_time);
544 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time);
545 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time);
546 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time);
547 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle, gc_mode);
548 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_urgent, gc_mode);
549 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, reclaim_segments, rec_prefree_segments);
550 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, max_small_discards, max_discards);
551 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, discard_granularity, discard_granularity);
552 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, discard_type, discard_type);
553 F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, reserved_blocks, reserved_blocks);
554 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, batched_trim_sections, trim_sections);
555 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, ipu_policy, ipu_policy);
556 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ipu_util, min_ipu_util);
557 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_fsync_blocks, min_fsync_blocks);
558 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_seq_blocks, min_seq_blocks);
559 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_hot_blocks, min_hot_blocks);
560 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ssr_sections, min_ssr_sections);
561 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ram_thresh, ram_thresh);
562 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ra_nid_pages, ra_nid_pages);
563 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, dirty_nats_ratio, dirty_nats_ratio);
564 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_victim_search, max_victim_search);
565 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, migration_granularity, migration_granularity);
566 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level);
567 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, cp_interval, interval_time[CP_TIME]);
568 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, idle_interval, interval_time[REQ_TIME]);
569 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, discard_idle_interval,
570 interval_time[DISCARD_TIME]);
571 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle_interval, interval_time[GC_TIME]);
572 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info,
573 umount_discard_timeout, interval_time[UMOUNT_DISCARD_TIMEOUT]);
574 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_enable, iostat_enable);
575 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_period_ms, iostat_period_ms);
576 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, readdir_ra, readdir_ra);
577 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_pin_file_thresh, gc_pin_file_threshold);
578 F2FS_RW_ATTR(F2FS_SBI, f2fs_super_block, extension_list, extension_list);
579 #ifdef CONFIG_F2FS_GRADING_SSR
580 F2FS_RW_ATTR(F2FS_HOT_COLD_PARAMS, f2fs_hot_cold_params,
581 hc_hot_data_lower_limit, hot_data_lower_limit);
582 F2FS_RW_ATTR(F2FS_HOT_COLD_PARAMS, f2fs_hot_cold_params,
583 hc_hot_data_waterline, hot_data_waterline);
584 F2FS_RW_ATTR(F2FS_HOT_COLD_PARAMS, f2fs_hot_cold_params,
585 hc_warm_data_lower_limit, warm_data_lower_limit);
586 F2FS_RW_ATTR(F2FS_HOT_COLD_PARAMS, f2fs_hot_cold_params,
587 hc_warm_data_waterline, warm_data_waterline);
588 F2FS_RW_ATTR(F2FS_HOT_COLD_PARAMS, f2fs_hot_cold_params,
589 hc_hot_node_lower_limit, hot_node_lower_limit);
590 F2FS_RW_ATTR(F2FS_HOT_COLD_PARAMS, f2fs_hot_cold_params,
591 hc_hot_node_waterline, hot_node_waterline);
592 F2FS_RW_ATTR(F2FS_HOT_COLD_PARAMS, f2fs_hot_cold_params,
593 hc_warm_node_lower_limit, warm_node_lower_limit);
594 F2FS_RW_ATTR(F2FS_HOT_COLD_PARAMS, f2fs_hot_cold_params,
595 hc_warm_node_waterline, warm_node_waterline);
596 F2FS_RW_ATTR(F2FS_HOT_COLD_PARAMS, f2fs_hot_cold_params,
597 hc_enable, enable);
598 #endif
599 #ifdef CONFIG_F2FS_FAULT_INJECTION
600 F2FS_RW_ATTR(FAULT_INFO_RATE, f2fs_fault_info, inject_rate, inject_rate);
601 F2FS_RW_ATTR(FAULT_INFO_TYPE, f2fs_fault_info, inject_type, inject_type);
602 #endif
603 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, data_io_flag, data_io_flag);
604 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, node_io_flag, node_io_flag);
605 F2FS_GENERAL_RO_ATTR(dirty_segments);
606 F2FS_GENERAL_RO_ATTR(free_segments);
607 F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
608 F2FS_GENERAL_RO_ATTR(features);
609 F2FS_GENERAL_RO_ATTR(current_reserved_blocks);
610 F2FS_GENERAL_RO_ATTR(unusable);
611 F2FS_GENERAL_RO_ATTR(encoding);
612 F2FS_GENERAL_RO_ATTR(mounted_time_sec);
613 F2FS_GENERAL_RO_ATTR(main_blkaddr);
614 #ifdef CONFIG_F2FS_STAT_FS
615 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, cp_foreground_calls, cp_count);
616 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, cp_background_calls, bg_cp_count);
617 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, gc_foreground_calls, call_count);
618 F2FS_STAT_ATTR(STAT_INFO, f2fs_stat_info, gc_background_calls, bg_gc);
619 F2FS_GENERAL_RO_ATTR(moved_blocks_background);
620 F2FS_GENERAL_RO_ATTR(moved_blocks_foreground);
621 F2FS_GENERAL_RO_ATTR(avg_vblocks);
622 #endif
623
624 #ifdef CONFIG_FS_ENCRYPTION
625 F2FS_FEATURE_RO_ATTR(encryption, FEAT_CRYPTO);
626 F2FS_FEATURE_RO_ATTR(test_dummy_encryption_v2, FEAT_TEST_DUMMY_ENCRYPTION_V2);
627 #endif
628 #ifdef CONFIG_BLK_DEV_ZONED
629 F2FS_FEATURE_RO_ATTR(block_zoned, FEAT_BLKZONED);
630 #endif
631 F2FS_FEATURE_RO_ATTR(atomic_write, FEAT_ATOMIC_WRITE);
632 F2FS_FEATURE_RO_ATTR(extra_attr, FEAT_EXTRA_ATTR);
633 F2FS_FEATURE_RO_ATTR(project_quota, FEAT_PROJECT_QUOTA);
634 F2FS_FEATURE_RO_ATTR(inode_checksum, FEAT_INODE_CHECKSUM);
635 F2FS_FEATURE_RO_ATTR(flexible_inline_xattr, FEAT_FLEXIBLE_INLINE_XATTR);
636 F2FS_FEATURE_RO_ATTR(quota_ino, FEAT_QUOTA_INO);
637 F2FS_FEATURE_RO_ATTR(inode_crtime, FEAT_INODE_CRTIME);
638 F2FS_FEATURE_RO_ATTR(lost_found, FEAT_LOST_FOUND);
639 #ifdef CONFIG_FS_VERITY
640 F2FS_FEATURE_RO_ATTR(verity, FEAT_VERITY);
641 #endif
642 F2FS_FEATURE_RO_ATTR(sb_checksum, FEAT_SB_CHECKSUM);
643 #ifdef CONFIG_UNICODE
644 F2FS_FEATURE_RO_ATTR(casefold, FEAT_CASEFOLD);
645 #endif
646 #ifdef CONFIG_F2FS_FS_COMPRESSION
647 F2FS_FEATURE_RO_ATTR(compression, FEAT_COMPRESSION);
648 #endif
649
650 #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
651 static struct attribute *f2fs_attrs[] = {
652 ATTR_LIST(gc_urgent_sleep_time),
653 ATTR_LIST(gc_min_sleep_time),
654 ATTR_LIST(gc_max_sleep_time),
655 ATTR_LIST(gc_no_gc_sleep_time),
656 ATTR_LIST(gc_idle),
657 ATTR_LIST(gc_urgent),
658 ATTR_LIST(reclaim_segments),
659 ATTR_LIST(main_blkaddr),
660 ATTR_LIST(max_small_discards),
661 ATTR_LIST(discard_granularity),
662 ATTR_LIST(discard_type),
663 ATTR_LIST(batched_trim_sections),
664 ATTR_LIST(ipu_policy),
665 ATTR_LIST(min_ipu_util),
666 ATTR_LIST(min_fsync_blocks),
667 ATTR_LIST(min_seq_blocks),
668 ATTR_LIST(min_hot_blocks),
669 ATTR_LIST(min_ssr_sections),
670 ATTR_LIST(max_victim_search),
671 ATTR_LIST(migration_granularity),
672 ATTR_LIST(dir_level),
673 ATTR_LIST(ram_thresh),
674 ATTR_LIST(ra_nid_pages),
675 ATTR_LIST(dirty_nats_ratio),
676 ATTR_LIST(cp_interval),
677 ATTR_LIST(idle_interval),
678 ATTR_LIST(discard_idle_interval),
679 ATTR_LIST(gc_idle_interval),
680 ATTR_LIST(umount_discard_timeout),
681 ATTR_LIST(iostat_enable),
682 ATTR_LIST(iostat_period_ms),
683 ATTR_LIST(readdir_ra),
684 ATTR_LIST(gc_pin_file_thresh),
685 ATTR_LIST(extension_list),
686 #ifdef CONFIG_F2FS_FAULT_INJECTION
687 ATTR_LIST(inject_rate),
688 ATTR_LIST(inject_type),
689 #endif
690 ATTR_LIST(data_io_flag),
691 ATTR_LIST(node_io_flag),
692 ATTR_LIST(dirty_segments),
693 ATTR_LIST(free_segments),
694 ATTR_LIST(unusable),
695 ATTR_LIST(lifetime_write_kbytes),
696 ATTR_LIST(features),
697 ATTR_LIST(reserved_blocks),
698 ATTR_LIST(current_reserved_blocks),
699 ATTR_LIST(encoding),
700 ATTR_LIST(mounted_time_sec),
701 #ifdef CONFIG_F2FS_STAT_FS
702 ATTR_LIST(cp_foreground_calls),
703 ATTR_LIST(cp_background_calls),
704 ATTR_LIST(gc_foreground_calls),
705 ATTR_LIST(gc_background_calls),
706 ATTR_LIST(moved_blocks_foreground),
707 ATTR_LIST(moved_blocks_background),
708 ATTR_LIST(avg_vblocks),
709 #endif
710 #ifdef CONFIG_F2FS_GRADING_SSR
711 ATTR_LIST(hc_hot_data_lower_limit),
712 ATTR_LIST(hc_hot_data_waterline),
713 ATTR_LIST(hc_warm_data_lower_limit),
714 ATTR_LIST(hc_warm_data_waterline),
715 ATTR_LIST(hc_hot_node_lower_limit),
716 ATTR_LIST(hc_hot_node_waterline),
717 ATTR_LIST(hc_warm_node_lower_limit),
718 ATTR_LIST(hc_warm_node_waterline),
719 ATTR_LIST(hc_enable),
720 #endif
721 NULL,
722 };
723 ATTRIBUTE_GROUPS(f2fs);
724
725 static struct attribute *f2fs_feat_attrs[] = {
726 #ifdef CONFIG_FS_ENCRYPTION
727 ATTR_LIST(encryption),
728 ATTR_LIST(test_dummy_encryption_v2),
729 #endif
730 #ifdef CONFIG_BLK_DEV_ZONED
731 ATTR_LIST(block_zoned),
732 #endif
733 ATTR_LIST(atomic_write),
734 ATTR_LIST(extra_attr),
735 ATTR_LIST(project_quota),
736 ATTR_LIST(inode_checksum),
737 ATTR_LIST(flexible_inline_xattr),
738 ATTR_LIST(quota_ino),
739 ATTR_LIST(inode_crtime),
740 ATTR_LIST(lost_found),
741 #ifdef CONFIG_FS_VERITY
742 ATTR_LIST(verity),
743 #endif
744 ATTR_LIST(sb_checksum),
745 #ifdef CONFIG_UNICODE
746 ATTR_LIST(casefold),
747 #endif
748 #ifdef CONFIG_F2FS_FS_COMPRESSION
749 ATTR_LIST(compression),
750 #endif
751 NULL,
752 };
753 ATTRIBUTE_GROUPS(f2fs_feat);
754
755 static const struct sysfs_ops f2fs_attr_ops = {
756 .show = f2fs_attr_show,
757 .store = f2fs_attr_store,
758 };
759
760 static struct kobj_type f2fs_sb_ktype = {
761 .default_groups = f2fs_groups,
762 .sysfs_ops = &f2fs_attr_ops,
763 .release = f2fs_sb_release,
764 };
765
766 static struct kobj_type f2fs_ktype = {
767 .sysfs_ops = &f2fs_attr_ops,
768 };
769
770 static struct kset f2fs_kset = {
771 .kobj = {.ktype = &f2fs_ktype},
772 };
773
774 static struct kobj_type f2fs_feat_ktype = {
775 .default_groups = f2fs_feat_groups,
776 .sysfs_ops = &f2fs_attr_ops,
777 };
778
779 static struct kobject f2fs_feat = {
780 .kset = &f2fs_kset,
781 };
782
segment_info_seq_show(struct seq_file * seq,void * offset)783 static int __maybe_unused segment_info_seq_show(struct seq_file *seq,
784 void *offset)
785 {
786 struct super_block *sb = seq->private;
787 struct f2fs_sb_info *sbi = F2FS_SB(sb);
788 unsigned int total_segs =
789 le32_to_cpu(sbi->raw_super->segment_count_main);
790 int i;
791
792 seq_puts(seq, "format: segment_type|valid_blocks\n"
793 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
794
795 for (i = 0; i < total_segs; i++) {
796 struct seg_entry *se = get_seg_entry(sbi, i);
797
798 if ((i % 10) == 0)
799 seq_printf(seq, "%-10d", i);
800 seq_printf(seq, "%d|%-3u", se->type, se->valid_blocks);
801 if ((i % 10) == 9 || i == (total_segs - 1))
802 seq_putc(seq, '\n');
803 else
804 seq_putc(seq, ' ');
805 }
806
807 return 0;
808 }
809
segment_bits_seq_show(struct seq_file * seq,void * offset)810 static int __maybe_unused segment_bits_seq_show(struct seq_file *seq,
811 void *offset)
812 {
813 struct super_block *sb = seq->private;
814 struct f2fs_sb_info *sbi = F2FS_SB(sb);
815 unsigned int total_segs =
816 le32_to_cpu(sbi->raw_super->segment_count_main);
817 int i, j;
818
819 seq_puts(seq, "format: segment_type|valid_blocks|bitmaps\n"
820 "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
821
822 for (i = 0; i < total_segs; i++) {
823 struct seg_entry *se = get_seg_entry(sbi, i);
824
825 seq_printf(seq, "%-10d", i);
826 seq_printf(seq, "%d|%-3u|", se->type, se->valid_blocks);
827 for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
828 seq_printf(seq, " %.2x", se->cur_valid_map[j]);
829 seq_putc(seq, '\n');
830 }
831 return 0;
832 }
833
f2fs_record_iostat(struct f2fs_sb_info * sbi)834 void f2fs_record_iostat(struct f2fs_sb_info *sbi)
835 {
836 unsigned long long iostat_diff[NR_IO_TYPE];
837 int i;
838
839 if (time_is_after_jiffies(sbi->iostat_next_period))
840 return;
841
842 /* Need double check under the lock */
843 spin_lock(&sbi->iostat_lock);
844 if (time_is_after_jiffies(sbi->iostat_next_period)) {
845 spin_unlock(&sbi->iostat_lock);
846 return;
847 }
848 sbi->iostat_next_period = jiffies +
849 msecs_to_jiffies(sbi->iostat_period_ms);
850
851 for (i = 0; i < NR_IO_TYPE; i++) {
852 iostat_diff[i] = sbi->rw_iostat[i] -
853 sbi->prev_rw_iostat[i];
854 sbi->prev_rw_iostat[i] = sbi->rw_iostat[i];
855 }
856 spin_unlock(&sbi->iostat_lock);
857
858 trace_f2fs_iostat(sbi, iostat_diff);
859 }
860
iostat_info_seq_show(struct seq_file * seq,void * offset)861 static int __maybe_unused iostat_info_seq_show(struct seq_file *seq,
862 void *offset)
863 {
864 struct super_block *sb = seq->private;
865 struct f2fs_sb_info *sbi = F2FS_SB(sb);
866 time64_t now = ktime_get_real_seconds();
867
868 if (!sbi->iostat_enable)
869 return 0;
870
871 seq_printf(seq, "time: %-16llu\n", now);
872
873 /* print app write IOs */
874 seq_puts(seq, "[WRITE]\n");
875 seq_printf(seq, "app buffered: %-16llu\n",
876 sbi->rw_iostat[APP_BUFFERED_IO]);
877 seq_printf(seq, "app direct: %-16llu\n",
878 sbi->rw_iostat[APP_DIRECT_IO]);
879 seq_printf(seq, "app mapped: %-16llu\n",
880 sbi->rw_iostat[APP_MAPPED_IO]);
881
882 /* print fs write IOs */
883 seq_printf(seq, "fs data: %-16llu\n",
884 sbi->rw_iostat[FS_DATA_IO]);
885 seq_printf(seq, "fs node: %-16llu\n",
886 sbi->rw_iostat[FS_NODE_IO]);
887 seq_printf(seq, "fs meta: %-16llu\n",
888 sbi->rw_iostat[FS_META_IO]);
889 seq_printf(seq, "fs gc data: %-16llu\n",
890 sbi->rw_iostat[FS_GC_DATA_IO]);
891 seq_printf(seq, "fs gc node: %-16llu\n",
892 sbi->rw_iostat[FS_GC_NODE_IO]);
893 seq_printf(seq, "fs cp data: %-16llu\n",
894 sbi->rw_iostat[FS_CP_DATA_IO]);
895 seq_printf(seq, "fs cp node: %-16llu\n",
896 sbi->rw_iostat[FS_CP_NODE_IO]);
897 seq_printf(seq, "fs cp meta: %-16llu\n",
898 sbi->rw_iostat[FS_CP_META_IO]);
899
900 /* print app read IOs */
901 seq_puts(seq, "[READ]\n");
902 seq_printf(seq, "app buffered: %-16llu\n",
903 sbi->rw_iostat[APP_BUFFERED_READ_IO]);
904 seq_printf(seq, "app direct: %-16llu\n",
905 sbi->rw_iostat[APP_DIRECT_READ_IO]);
906 seq_printf(seq, "app mapped: %-16llu\n",
907 sbi->rw_iostat[APP_MAPPED_READ_IO]);
908
909 /* print fs read IOs */
910 seq_printf(seq, "fs data: %-16llu\n",
911 sbi->rw_iostat[FS_DATA_READ_IO]);
912 seq_printf(seq, "fs gc data: %-16llu\n",
913 sbi->rw_iostat[FS_GDATA_READ_IO]);
914 seq_printf(seq, "fs compr_data: %-16llu\n",
915 sbi->rw_iostat[FS_CDATA_READ_IO]);
916 seq_printf(seq, "fs node: %-16llu\n",
917 sbi->rw_iostat[FS_NODE_READ_IO]);
918 seq_printf(seq, "fs meta: %-16llu\n",
919 sbi->rw_iostat[FS_META_READ_IO]);
920
921 /* print other IOs */
922 seq_puts(seq, "[OTHER]\n");
923 seq_printf(seq, "fs discard: %-16llu\n",
924 sbi->rw_iostat[FS_DISCARD]);
925
926 return 0;
927 }
928
victim_bits_seq_show(struct seq_file * seq,void * offset)929 static int __maybe_unused victim_bits_seq_show(struct seq_file *seq,
930 void *offset)
931 {
932 struct super_block *sb = seq->private;
933 struct f2fs_sb_info *sbi = F2FS_SB(sb);
934 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
935 int i;
936
937 seq_puts(seq, "format: victim_secmap bitmaps\n");
938
939 for (i = 0; i < MAIN_SECS(sbi); i++) {
940 if ((i % 10) == 0)
941 seq_printf(seq, "%-10d", i);
942 seq_printf(seq, "%d", test_bit(i, dirty_i->victim_secmap) ? 1 : 0);
943 if ((i % 10) == 9 || i == (MAIN_SECS(sbi) - 1))
944 seq_putc(seq, '\n');
945 else
946 seq_putc(seq, ' ');
947 }
948 return 0;
949 }
950
undiscard_info_seq_show(struct seq_file * seq,void * offset)951 static int undiscard_info_seq_show(struct seq_file *seq, void *offset)
952 {
953 struct super_block *sb = seq->private;
954 struct f2fs_sb_info *sbi = F2FS_SB(sb);
955 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
956 struct sit_info *sit_i = SIT_I(sbi);
957 unsigned int total_segs = le32_to_cpu(sbi->raw_super->segment_count_main);
958 unsigned int total = 0;
959 unsigned int i, j;
960 unsigned int max_blocks = sbi->blocks_per_seg;
961 unsigned long *dmap = SIT_I(sbi)->tmp_map;
962
963 if (!f2fs_realtime_discard_enable(sbi))
964 goto out;
965
966 for (i = 0; i < total_segs; i++) {
967 struct seg_entry *se = get_seg_entry(sbi, i);
968 unsigned int entries = SIT_VBLOCK_MAP_SIZE /
969 sizeof(unsigned long);
970 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
971 unsigned long *discard_map = (unsigned long *)se->discard_map;
972 int start = 0, end = -1;
973
974 down_write(&sit_i->sentry_lock);
975 if (se->valid_blocks == max_blocks) {
976 up_write(&sit_i->sentry_lock);
977 continue;
978 }
979
980 if (se->valid_blocks == 0) {
981 mutex_lock(&dirty_i->seglist_lock);
982 if (test_bit((int)i, dirty_i->dirty_segmap[PRE]))
983 total += 512;
984 mutex_unlock(&dirty_i->seglist_lock);
985 } else {
986 for (j = 0; j < entries; j++)
987 dmap[j] = ~ckpt_map[j] & ~discard_map[j];
988 while (1) {
989 start = (int)find_rev_next_bit(dmap,
990 (unsigned long)max_blocks,
991 (unsigned long)(end + 1));
992
993 if ((unsigned int)start >= max_blocks)
994 break;
995
996 end = (int)find_rev_next_zero_bit(dmap,
997 (unsigned long)max_blocks,
998 (unsigned long)(start + 1));
999 total += (unsigned int)(end - start);
1000 }
1001 }
1002
1003 up_write(&sit_i->sentry_lock);
1004 }
1005
1006 out:
1007 seq_printf(seq, "total undiscard:%u K\n", total * 4);
1008 return 0;
1009 }
1010
f2fs_init_sysfs(void)1011 int __init f2fs_init_sysfs(void)
1012 {
1013 int ret;
1014
1015 kobject_set_name(&f2fs_kset.kobj, "f2fs");
1016 f2fs_kset.kobj.parent = fs_kobj;
1017 ret = kset_register(&f2fs_kset);
1018 if (ret)
1019 return ret;
1020
1021 ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
1022 NULL, "features");
1023 if (ret) {
1024 kobject_put(&f2fs_feat);
1025 kset_unregister(&f2fs_kset);
1026 } else {
1027 f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
1028 }
1029 return ret;
1030 }
1031
f2fs_exit_sysfs(void)1032 void f2fs_exit_sysfs(void)
1033 {
1034 kobject_put(&f2fs_feat);
1035 kset_unregister(&f2fs_kset);
1036 remove_proc_entry("fs/f2fs", NULL);
1037 f2fs_proc_root = NULL;
1038 }
1039
f2fs_register_sysfs(struct f2fs_sb_info * sbi)1040 int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
1041 {
1042 struct super_block *sb = sbi->sb;
1043 int err;
1044
1045 sbi->s_kobj.kset = &f2fs_kset;
1046 init_completion(&sbi->s_kobj_unregister);
1047 err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
1048 "%s", sb->s_id);
1049 if (err) {
1050 kobject_put(&sbi->s_kobj);
1051 wait_for_completion(&sbi->s_kobj_unregister);
1052 return err;
1053 }
1054
1055 if (f2fs_proc_root)
1056 sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
1057
1058 if (sbi->s_proc) {
1059 proc_create_single_data("segment_info", S_IRUGO, sbi->s_proc,
1060 segment_info_seq_show, sb);
1061 proc_create_single_data("segment_bits", S_IRUGO, sbi->s_proc,
1062 segment_bits_seq_show, sb);
1063 proc_create_single_data("iostat_info", S_IRUGO, sbi->s_proc,
1064 iostat_info_seq_show, sb);
1065 proc_create_single_data("victim_bits", S_IRUGO, sbi->s_proc,
1066 victim_bits_seq_show, sb);
1067 proc_create_single_data("undiscard_info", S_IRUGO, sbi->s_proc,
1068 undiscard_info_seq_show, sb);
1069
1070 }
1071 return 0;
1072 }
1073
f2fs_unregister_sysfs(struct f2fs_sb_info * sbi)1074 void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
1075 {
1076 if (sbi->s_proc) {
1077 remove_proc_entry("iostat_info", sbi->s_proc);
1078 remove_proc_entry("segment_info", sbi->s_proc);
1079 remove_proc_entry("segment_bits", sbi->s_proc);
1080 remove_proc_entry("victim_bits", sbi->s_proc);
1081 remove_proc_entry("undiscard_info", sbi->s_proc);
1082 remove_proc_entry(sbi->sb->s_id, f2fs_proc_root);
1083 }
1084 kobject_del(&sbi->s_kobj);
1085 kobject_put(&sbi->s_kobj);
1086 wait_for_completion(&sbi->s_kobj_unregister);
1087 }
1088