1 /*
2  * f2fs sysfs interface
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5  *             http://www.samsung.com/
6  * Copyright (c) 2017 Chao Yu <chao@kernel.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12 #include <linux/compiler.h>
13 #include <linux/proc_fs.h>
14 #include <linux/f2fs_fs.h>
15 #include <linux/seq_file.h>
16 
17 #include "f2fs.h"
18 #include "segment.h"
19 #include "gc.h"
20 
21 static struct proc_dir_entry *f2fs_proc_root;
22 
23 /* Sysfs support for f2fs */
24 enum {
25 	GC_THREAD,	/* struct f2fs_gc_thread */
26 	SM_INFO,	/* struct f2fs_sm_info */
27 	DCC_INFO,	/* struct discard_cmd_control */
28 	NM_INFO,	/* struct f2fs_nm_info */
29 	F2FS_SBI,	/* struct f2fs_sb_info */
30 #ifdef CONFIG_F2FS_FAULT_INJECTION
31 	FAULT_INFO_RATE,	/* struct f2fs_fault_info */
32 	FAULT_INFO_TYPE,	/* struct f2fs_fault_info */
33 #endif
34 	RESERVED_BLOCKS,	/* struct f2fs_sb_info */
35 };
36 
37 struct f2fs_attr {
38 	struct attribute attr;
39 	ssize_t (*show)(struct f2fs_attr *, struct f2fs_sb_info *, char *);
40 	ssize_t (*store)(struct f2fs_attr *, struct f2fs_sb_info *,
41 			 const char *, size_t);
42 	int struct_type;
43 	int offset;
44 	int id;
45 };
46 
__struct_ptr(struct f2fs_sb_info * sbi,int struct_type)47 static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type)
48 {
49 	if (struct_type == GC_THREAD)
50 		return (unsigned char *)sbi->gc_thread;
51 	else if (struct_type == SM_INFO)
52 		return (unsigned char *)SM_I(sbi);
53 	else if (struct_type == DCC_INFO)
54 		return (unsigned char *)SM_I(sbi)->dcc_info;
55 	else if (struct_type == NM_INFO)
56 		return (unsigned char *)NM_I(sbi);
57 	else if (struct_type == F2FS_SBI || struct_type == RESERVED_BLOCKS)
58 		return (unsigned char *)sbi;
59 #ifdef CONFIG_F2FS_FAULT_INJECTION
60 	else if (struct_type == FAULT_INFO_RATE ||
61 					struct_type == FAULT_INFO_TYPE)
62 		return (unsigned char *)&F2FS_OPTION(sbi).fault_info;
63 #endif
64 	return NULL;
65 }
66 
dirty_segments_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)67 static ssize_t dirty_segments_show(struct f2fs_attr *a,
68 		struct f2fs_sb_info *sbi, char *buf)
69 {
70 	return snprintf(buf, PAGE_SIZE, "%llu\n",
71 		(unsigned long long)(dirty_segments(sbi)));
72 }
73 
lifetime_write_kbytes_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)74 static ssize_t lifetime_write_kbytes_show(struct f2fs_attr *a,
75 		struct f2fs_sb_info *sbi, char *buf)
76 {
77 	struct super_block *sb = sbi->sb;
78 
79 	if (!sb->s_bdev->bd_part)
80 		return snprintf(buf, PAGE_SIZE, "0\n");
81 
82 	return snprintf(buf, PAGE_SIZE, "%llu\n",
83 		(unsigned long long)(sbi->kbytes_written +
84 			BD_PART_WRITTEN(sbi)));
85 }
86 
features_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)87 static ssize_t features_show(struct f2fs_attr *a,
88 		struct f2fs_sb_info *sbi, char *buf)
89 {
90 	struct super_block *sb = sbi->sb;
91 	int len = 0;
92 
93 	if (!sb->s_bdev->bd_part)
94 		return snprintf(buf, PAGE_SIZE, "0\n");
95 
96 	if (f2fs_sb_has_encrypt(sb))
97 		len += snprintf(buf, PAGE_SIZE - len, "%s",
98 						"encryption");
99 	if (f2fs_sb_has_blkzoned(sb))
100 		len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
101 				len ? ", " : "", "blkzoned");
102 	if (f2fs_sb_has_extra_attr(sb))
103 		len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
104 				len ? ", " : "", "extra_attr");
105 	if (f2fs_sb_has_project_quota(sb))
106 		len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
107 				len ? ", " : "", "projquota");
108 	if (f2fs_sb_has_inode_chksum(sb))
109 		len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
110 				len ? ", " : "", "inode_checksum");
111 	if (f2fs_sb_has_flexible_inline_xattr(sb))
112 		len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
113 				len ? ", " : "", "flexible_inline_xattr");
114 	if (f2fs_sb_has_quota_ino(sb))
115 		len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
116 				len ? ", " : "", "quota_ino");
117 	if (f2fs_sb_has_inode_crtime(sb))
118 		len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
119 				len ? ", " : "", "inode_crtime");
120 	if (f2fs_sb_has_lost_found(sb))
121 		len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
122 				len ? ", " : "", "lost_found");
123 	len += snprintf(buf + len, PAGE_SIZE - len, "\n");
124 	return len;
125 }
126 
current_reserved_blocks_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)127 static ssize_t current_reserved_blocks_show(struct f2fs_attr *a,
128 					struct f2fs_sb_info *sbi, char *buf)
129 {
130 	return snprintf(buf, PAGE_SIZE, "%u\n", sbi->current_reserved_blocks);
131 }
132 
f2fs_sbi_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)133 static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
134 			struct f2fs_sb_info *sbi, char *buf)
135 {
136 	unsigned char *ptr = NULL;
137 	unsigned int *ui;
138 
139 	ptr = __struct_ptr(sbi, a->struct_type);
140 	if (!ptr)
141 		return -EINVAL;
142 
143 	if (!strcmp(a->attr.name, "extension_list")) {
144 		__u8 (*extlist)[F2FS_EXTENSION_LEN] =
145 					sbi->raw_super->extension_list;
146 		int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
147 		int hot_count = sbi->raw_super->hot_ext_count;
148 		int len = 0, i;
149 
150 		len += snprintf(buf + len, PAGE_SIZE - len,
151 						"cold file extension:\n");
152 		for (i = 0; i < cold_count; i++)
153 			len += snprintf(buf + len, PAGE_SIZE - len, "%s\n",
154 								extlist[i]);
155 
156 		len += snprintf(buf + len, PAGE_SIZE - len,
157 						"hot file extension:\n");
158 		for (i = cold_count; i < cold_count + hot_count; i++)
159 			len += snprintf(buf + len, PAGE_SIZE - len, "%s\n",
160 								extlist[i]);
161 		return len;
162 	}
163 
164 	ui = (unsigned int *)(ptr + a->offset);
165 
166 	return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
167 }
168 
__sbi_store(struct f2fs_attr * a,struct f2fs_sb_info * sbi,const char * buf,size_t count)169 static ssize_t __sbi_store(struct f2fs_attr *a,
170 			struct f2fs_sb_info *sbi,
171 			const char *buf, size_t count)
172 {
173 	unsigned char *ptr;
174 	unsigned long t;
175 	unsigned int *ui;
176 	ssize_t ret;
177 
178 	ptr = __struct_ptr(sbi, a->struct_type);
179 	if (!ptr)
180 		return -EINVAL;
181 
182 	if (!strcmp(a->attr.name, "extension_list")) {
183 		const char *name = strim((char *)buf);
184 		bool set = true, hot;
185 
186 		if (!strncmp(name, "[h]", 3))
187 			hot = true;
188 		else if (!strncmp(name, "[c]", 3))
189 			hot = false;
190 		else
191 			return -EINVAL;
192 
193 		name += 3;
194 
195 		if (*name == '!') {
196 			name++;
197 			set = false;
198 		}
199 
200 		if (strlen(name) >= F2FS_EXTENSION_LEN)
201 			return -EINVAL;
202 
203 		down_write(&sbi->sb_lock);
204 
205 		ret = f2fs_update_extension_list(sbi, name, hot, set);
206 		if (ret)
207 			goto out;
208 
209 		ret = f2fs_commit_super(sbi, false);
210 		if (ret)
211 			f2fs_update_extension_list(sbi, name, hot, !set);
212 out:
213 		up_write(&sbi->sb_lock);
214 		return ret ? ret : count;
215 	}
216 
217 	ui = (unsigned int *)(ptr + a->offset);
218 
219 	ret = kstrtoul(skip_spaces(buf), 0, &t);
220 	if (ret < 0)
221 		return ret;
222 #ifdef CONFIG_F2FS_FAULT_INJECTION
223 	if (a->struct_type == FAULT_INFO_TYPE && t >= (1 << FAULT_MAX))
224 		return -EINVAL;
225 #endif
226 	if (a->struct_type == RESERVED_BLOCKS) {
227 		spin_lock(&sbi->stat_lock);
228 		if (t > (unsigned long)(sbi->user_block_count -
229 				F2FS_OPTION(sbi).root_reserved_blocks)) {
230 			spin_unlock(&sbi->stat_lock);
231 			return -EINVAL;
232 		}
233 		*ui = t;
234 		sbi->current_reserved_blocks = min(sbi->reserved_blocks,
235 				sbi->user_block_count - valid_user_blocks(sbi));
236 		spin_unlock(&sbi->stat_lock);
237 		return count;
238 	}
239 
240 	if (!strcmp(a->attr.name, "discard_granularity")) {
241 		if (t == 0 || t > MAX_PLIST_NUM)
242 			return -EINVAL;
243 		if (t == *ui)
244 			return count;
245 		*ui = t;
246 		return count;
247 	}
248 
249 	if (!strcmp(a->attr.name, "trim_sections"))
250 		return -EINVAL;
251 
252 	if (!strcmp(a->attr.name, "gc_urgent")) {
253 		if (t >= 1) {
254 			sbi->gc_mode = GC_URGENT;
255 			if (sbi->gc_thread) {
256 				sbi->gc_thread->gc_wake = 1;
257 				wake_up_interruptible_all(
258 					&sbi->gc_thread->gc_wait_queue_head);
259 				wake_up_discard_thread(sbi, true);
260 			}
261 		} else {
262 			sbi->gc_mode = GC_NORMAL;
263 		}
264 		return count;
265 	}
266 	if (!strcmp(a->attr.name, "gc_idle")) {
267 		if (t == GC_IDLE_CB)
268 			sbi->gc_mode = GC_IDLE_CB;
269 		else if (t == GC_IDLE_GREEDY)
270 			sbi->gc_mode = GC_IDLE_GREEDY;
271 		else
272 			sbi->gc_mode = GC_NORMAL;
273 		return count;
274 	}
275 
276 
277 	if (!strcmp(a->attr.name, "iostat_enable")) {
278 		sbi->iostat_enable = !!t;
279 		if (!sbi->iostat_enable)
280 			f2fs_reset_iostat(sbi);
281 		return count;
282 	}
283 
284 	*ui = (unsigned int)t;
285 
286 	return count;
287 }
288 
f2fs_sbi_store(struct f2fs_attr * a,struct f2fs_sb_info * sbi,const char * buf,size_t count)289 static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
290 			struct f2fs_sb_info *sbi,
291 			const char *buf, size_t count)
292 {
293 	ssize_t ret;
294 	bool gc_entry = (!strcmp(a->attr.name, "gc_urgent") ||
295 					a->struct_type == GC_THREAD);
296 
297 	if (gc_entry) {
298 		if (!down_read_trylock(&sbi->sb->s_umount))
299 			return -EAGAIN;
300 	}
301 	ret = __sbi_store(a, sbi, buf, count);
302 	if (gc_entry)
303 		up_read(&sbi->sb->s_umount);
304 
305 	return ret;
306 }
307 
f2fs_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)308 static ssize_t f2fs_attr_show(struct kobject *kobj,
309 				struct attribute *attr, char *buf)
310 {
311 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
312 								s_kobj);
313 	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
314 
315 	return a->show ? a->show(a, sbi, buf) : 0;
316 }
317 
f2fs_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t len)318 static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
319 						const char *buf, size_t len)
320 {
321 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
322 									s_kobj);
323 	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
324 
325 	return a->store ? a->store(a, sbi, buf, len) : 0;
326 }
327 
f2fs_sb_release(struct kobject * kobj)328 static void f2fs_sb_release(struct kobject *kobj)
329 {
330 	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
331 								s_kobj);
332 	complete(&sbi->s_kobj_unregister);
333 }
334 
335 enum feat_id {
336 	FEAT_CRYPTO = 0,
337 	FEAT_BLKZONED,
338 	FEAT_ATOMIC_WRITE,
339 	FEAT_EXTRA_ATTR,
340 	FEAT_PROJECT_QUOTA,
341 	FEAT_INODE_CHECKSUM,
342 	FEAT_FLEXIBLE_INLINE_XATTR,
343 	FEAT_QUOTA_INO,
344 	FEAT_INODE_CRTIME,
345 	FEAT_LOST_FOUND,
346 };
347 
f2fs_feature_show(struct f2fs_attr * a,struct f2fs_sb_info * sbi,char * buf)348 static ssize_t f2fs_feature_show(struct f2fs_attr *a,
349 		struct f2fs_sb_info *sbi, char *buf)
350 {
351 	switch (a->id) {
352 	case FEAT_CRYPTO:
353 	case FEAT_BLKZONED:
354 	case FEAT_ATOMIC_WRITE:
355 	case FEAT_EXTRA_ATTR:
356 	case FEAT_PROJECT_QUOTA:
357 	case FEAT_INODE_CHECKSUM:
358 	case FEAT_FLEXIBLE_INLINE_XATTR:
359 	case FEAT_QUOTA_INO:
360 	case FEAT_INODE_CRTIME:
361 	case FEAT_LOST_FOUND:
362 		return snprintf(buf, PAGE_SIZE, "supported\n");
363 	}
364 	return 0;
365 }
366 
367 #define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
368 static struct f2fs_attr f2fs_attr_##_name = {			\
369 	.attr = {.name = __stringify(_name), .mode = _mode },	\
370 	.show	= _show,					\
371 	.store	= _store,					\
372 	.struct_type = _struct_type,				\
373 	.offset = _offset					\
374 }
375 
376 #define F2FS_RW_ATTR(struct_type, struct_name, name, elname)	\
377 	F2FS_ATTR_OFFSET(struct_type, name, 0644,		\
378 		f2fs_sbi_show, f2fs_sbi_store,			\
379 		offsetof(struct struct_name, elname))
380 
381 #define F2FS_GENERAL_RO_ATTR(name) \
382 static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
383 
384 #define F2FS_FEATURE_RO_ATTR(_name, _id)			\
385 static struct f2fs_attr f2fs_attr_##_name = {			\
386 	.attr = {.name = __stringify(_name), .mode = 0444 },	\
387 	.show	= f2fs_feature_show,				\
388 	.id	= _id,						\
389 }
390 
391 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_urgent_sleep_time,
392 							urgent_sleep_time);
393 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time);
394 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time);
395 F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time);
396 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle, gc_mode);
397 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_urgent, gc_mode);
398 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, reclaim_segments, rec_prefree_segments);
399 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, max_small_discards, max_discards);
400 F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, discard_granularity, discard_granularity);
401 F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, reserved_blocks, reserved_blocks);
402 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, batched_trim_sections, trim_sections);
403 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, ipu_policy, ipu_policy);
404 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ipu_util, min_ipu_util);
405 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_fsync_blocks, min_fsync_blocks);
406 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_seq_blocks, min_seq_blocks);
407 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_hot_blocks, min_hot_blocks);
408 F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ssr_sections, min_ssr_sections);
409 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ram_thresh, ram_thresh);
410 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ra_nid_pages, ra_nid_pages);
411 F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, dirty_nats_ratio, dirty_nats_ratio);
412 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_victim_search, max_victim_search);
413 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level);
414 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, cp_interval, interval_time[CP_TIME]);
415 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, idle_interval, interval_time[REQ_TIME]);
416 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_enable, iostat_enable);
417 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, readdir_ra, readdir_ra);
418 F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_pin_file_thresh, gc_pin_file_threshold);
419 F2FS_RW_ATTR(F2FS_SBI, f2fs_super_block, extension_list, extension_list);
420 #ifdef CONFIG_F2FS_FAULT_INJECTION
421 F2FS_RW_ATTR(FAULT_INFO_RATE, f2fs_fault_info, inject_rate, inject_rate);
422 F2FS_RW_ATTR(FAULT_INFO_TYPE, f2fs_fault_info, inject_type, inject_type);
423 #endif
424 F2FS_GENERAL_RO_ATTR(dirty_segments);
425 F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
426 F2FS_GENERAL_RO_ATTR(features);
427 F2FS_GENERAL_RO_ATTR(current_reserved_blocks);
428 
429 #ifdef CONFIG_F2FS_FS_ENCRYPTION
430 F2FS_FEATURE_RO_ATTR(encryption, FEAT_CRYPTO);
431 #endif
432 #ifdef CONFIG_BLK_DEV_ZONED
433 F2FS_FEATURE_RO_ATTR(block_zoned, FEAT_BLKZONED);
434 #endif
435 F2FS_FEATURE_RO_ATTR(atomic_write, FEAT_ATOMIC_WRITE);
436 F2FS_FEATURE_RO_ATTR(extra_attr, FEAT_EXTRA_ATTR);
437 F2FS_FEATURE_RO_ATTR(project_quota, FEAT_PROJECT_QUOTA);
438 F2FS_FEATURE_RO_ATTR(inode_checksum, FEAT_INODE_CHECKSUM);
439 F2FS_FEATURE_RO_ATTR(flexible_inline_xattr, FEAT_FLEXIBLE_INLINE_XATTR);
440 F2FS_FEATURE_RO_ATTR(quota_ino, FEAT_QUOTA_INO);
441 F2FS_FEATURE_RO_ATTR(inode_crtime, FEAT_INODE_CRTIME);
442 F2FS_FEATURE_RO_ATTR(lost_found, FEAT_LOST_FOUND);
443 
444 #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
445 static struct attribute *f2fs_attrs[] = {
446 	ATTR_LIST(gc_urgent_sleep_time),
447 	ATTR_LIST(gc_min_sleep_time),
448 	ATTR_LIST(gc_max_sleep_time),
449 	ATTR_LIST(gc_no_gc_sleep_time),
450 	ATTR_LIST(gc_idle),
451 	ATTR_LIST(gc_urgent),
452 	ATTR_LIST(reclaim_segments),
453 	ATTR_LIST(max_small_discards),
454 	ATTR_LIST(discard_granularity),
455 	ATTR_LIST(batched_trim_sections),
456 	ATTR_LIST(ipu_policy),
457 	ATTR_LIST(min_ipu_util),
458 	ATTR_LIST(min_fsync_blocks),
459 	ATTR_LIST(min_seq_blocks),
460 	ATTR_LIST(min_hot_blocks),
461 	ATTR_LIST(min_ssr_sections),
462 	ATTR_LIST(max_victim_search),
463 	ATTR_LIST(dir_level),
464 	ATTR_LIST(ram_thresh),
465 	ATTR_LIST(ra_nid_pages),
466 	ATTR_LIST(dirty_nats_ratio),
467 	ATTR_LIST(cp_interval),
468 	ATTR_LIST(idle_interval),
469 	ATTR_LIST(iostat_enable),
470 	ATTR_LIST(readdir_ra),
471 	ATTR_LIST(gc_pin_file_thresh),
472 	ATTR_LIST(extension_list),
473 #ifdef CONFIG_F2FS_FAULT_INJECTION
474 	ATTR_LIST(inject_rate),
475 	ATTR_LIST(inject_type),
476 #endif
477 	ATTR_LIST(dirty_segments),
478 	ATTR_LIST(lifetime_write_kbytes),
479 	ATTR_LIST(features),
480 	ATTR_LIST(reserved_blocks),
481 	ATTR_LIST(current_reserved_blocks),
482 	NULL,
483 };
484 
485 static struct attribute *f2fs_feat_attrs[] = {
486 #ifdef CONFIG_F2FS_FS_ENCRYPTION
487 	ATTR_LIST(encryption),
488 #endif
489 #ifdef CONFIG_BLK_DEV_ZONED
490 	ATTR_LIST(block_zoned),
491 #endif
492 	ATTR_LIST(atomic_write),
493 	ATTR_LIST(extra_attr),
494 	ATTR_LIST(project_quota),
495 	ATTR_LIST(inode_checksum),
496 	ATTR_LIST(flexible_inline_xattr),
497 	ATTR_LIST(quota_ino),
498 	ATTR_LIST(inode_crtime),
499 	ATTR_LIST(lost_found),
500 	NULL,
501 };
502 
503 static const struct sysfs_ops f2fs_attr_ops = {
504 	.show	= f2fs_attr_show,
505 	.store	= f2fs_attr_store,
506 };
507 
508 static struct kobj_type f2fs_sb_ktype = {
509 	.default_attrs	= f2fs_attrs,
510 	.sysfs_ops	= &f2fs_attr_ops,
511 	.release	= f2fs_sb_release,
512 };
513 
514 static struct kobj_type f2fs_ktype = {
515 	.sysfs_ops	= &f2fs_attr_ops,
516 };
517 
518 static struct kset f2fs_kset = {
519 	.kobj   = {.ktype = &f2fs_ktype},
520 };
521 
522 static struct kobj_type f2fs_feat_ktype = {
523 	.default_attrs	= f2fs_feat_attrs,
524 	.sysfs_ops	= &f2fs_attr_ops,
525 };
526 
527 static struct kobject f2fs_feat = {
528 	.kset	= &f2fs_kset,
529 };
530 
segment_info_seq_show(struct seq_file * seq,void * offset)531 static int __maybe_unused segment_info_seq_show(struct seq_file *seq,
532 						void *offset)
533 {
534 	struct super_block *sb = seq->private;
535 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
536 	unsigned int total_segs =
537 			le32_to_cpu(sbi->raw_super->segment_count_main);
538 	int i;
539 
540 	seq_puts(seq, "format: segment_type|valid_blocks\n"
541 		"segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
542 
543 	for (i = 0; i < total_segs; i++) {
544 		struct seg_entry *se = get_seg_entry(sbi, i);
545 
546 		if ((i % 10) == 0)
547 			seq_printf(seq, "%-10d", i);
548 		seq_printf(seq, "%d|%-3u", se->type,
549 					get_valid_blocks(sbi, i, false));
550 		if ((i % 10) == 9 || i == (total_segs - 1))
551 			seq_putc(seq, '\n');
552 		else
553 			seq_putc(seq, ' ');
554 	}
555 
556 	return 0;
557 }
558 
segment_bits_seq_show(struct seq_file * seq,void * offset)559 static int __maybe_unused segment_bits_seq_show(struct seq_file *seq,
560 						void *offset)
561 {
562 	struct super_block *sb = seq->private;
563 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
564 	unsigned int total_segs =
565 			le32_to_cpu(sbi->raw_super->segment_count_main);
566 	int i, j;
567 
568 	seq_puts(seq, "format: segment_type|valid_blocks|bitmaps\n"
569 		"segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
570 
571 	for (i = 0; i < total_segs; i++) {
572 		struct seg_entry *se = get_seg_entry(sbi, i);
573 
574 		seq_printf(seq, "%-10d", i);
575 		seq_printf(seq, "%d|%-3u|", se->type,
576 					get_valid_blocks(sbi, i, false));
577 		for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
578 			seq_printf(seq, " %.2x", se->cur_valid_map[j]);
579 		seq_putc(seq, '\n');
580 	}
581 	return 0;
582 }
583 
iostat_info_seq_show(struct seq_file * seq,void * offset)584 static int __maybe_unused iostat_info_seq_show(struct seq_file *seq,
585 					       void *offset)
586 {
587 	struct super_block *sb = seq->private;
588 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
589 	time64_t now = ktime_get_real_seconds();
590 
591 	if (!sbi->iostat_enable)
592 		return 0;
593 
594 	seq_printf(seq, "time:		%-16llu\n", now);
595 
596 	/* print app IOs */
597 	seq_printf(seq, "app buffered:	%-16llu\n",
598 				sbi->write_iostat[APP_BUFFERED_IO]);
599 	seq_printf(seq, "app direct:	%-16llu\n",
600 				sbi->write_iostat[APP_DIRECT_IO]);
601 	seq_printf(seq, "app mapped:	%-16llu\n",
602 				sbi->write_iostat[APP_MAPPED_IO]);
603 
604 	/* print fs IOs */
605 	seq_printf(seq, "fs data:	%-16llu\n",
606 				sbi->write_iostat[FS_DATA_IO]);
607 	seq_printf(seq, "fs node:	%-16llu\n",
608 				sbi->write_iostat[FS_NODE_IO]);
609 	seq_printf(seq, "fs meta:	%-16llu\n",
610 				sbi->write_iostat[FS_META_IO]);
611 	seq_printf(seq, "fs gc data:	%-16llu\n",
612 				sbi->write_iostat[FS_GC_DATA_IO]);
613 	seq_printf(seq, "fs gc node:	%-16llu\n",
614 				sbi->write_iostat[FS_GC_NODE_IO]);
615 	seq_printf(seq, "fs cp data:	%-16llu\n",
616 				sbi->write_iostat[FS_CP_DATA_IO]);
617 	seq_printf(seq, "fs cp node:	%-16llu\n",
618 				sbi->write_iostat[FS_CP_NODE_IO]);
619 	seq_printf(seq, "fs cp meta:	%-16llu\n",
620 				sbi->write_iostat[FS_CP_META_IO]);
621 	seq_printf(seq, "fs discard:	%-16llu\n",
622 				sbi->write_iostat[FS_DISCARD]);
623 
624 	return 0;
625 }
626 
victim_bits_seq_show(struct seq_file * seq,void * offset)627 static int __maybe_unused victim_bits_seq_show(struct seq_file *seq,
628 						void *offset)
629 {
630 	struct super_block *sb = seq->private;
631 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
632 	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
633 	int i;
634 
635 	seq_puts(seq, "format: victim_secmap bitmaps\n");
636 
637 	for (i = 0; i < MAIN_SECS(sbi); i++) {
638 		if ((i % 10) == 0)
639 			seq_printf(seq, "%-10d", i);
640 		seq_printf(seq, "%d", test_bit(i, dirty_i->victim_secmap) ? 1 : 0);
641 		if ((i % 10) == 9 || i == (MAIN_SECS(sbi) - 1))
642 			seq_putc(seq, '\n');
643 		else
644 			seq_putc(seq, ' ');
645 	}
646 	return 0;
647 }
648 
f2fs_init_sysfs(void)649 int __init f2fs_init_sysfs(void)
650 {
651 	int ret;
652 
653 	kobject_set_name(&f2fs_kset.kobj, "f2fs");
654 	f2fs_kset.kobj.parent = fs_kobj;
655 	ret = kset_register(&f2fs_kset);
656 	if (ret)
657 		return ret;
658 
659 	ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
660 				   NULL, "features");
661 	if (ret) {
662 		kobject_put(&f2fs_feat);
663 		kset_unregister(&f2fs_kset);
664 	} else {
665 		f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
666 	}
667 	return ret;
668 }
669 
f2fs_exit_sysfs(void)670 void f2fs_exit_sysfs(void)
671 {
672 	kobject_put(&f2fs_feat);
673 	kset_unregister(&f2fs_kset);
674 	remove_proc_entry("fs/f2fs", NULL);
675 	f2fs_proc_root = NULL;
676 }
677 
f2fs_register_sysfs(struct f2fs_sb_info * sbi)678 int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
679 {
680 	struct super_block *sb = sbi->sb;
681 	int err;
682 
683 	sbi->s_kobj.kset = &f2fs_kset;
684 	init_completion(&sbi->s_kobj_unregister);
685 	err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
686 				"%s", sb->s_id);
687 	if (err) {
688 		kobject_put(&sbi->s_kobj);
689 		wait_for_completion(&sbi->s_kobj_unregister);
690 		return err;
691 	}
692 
693 	if (f2fs_proc_root)
694 		sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
695 
696 	if (sbi->s_proc) {
697 		proc_create_single_data("segment_info", S_IRUGO, sbi->s_proc,
698 				segment_info_seq_show, sb);
699 		proc_create_single_data("segment_bits", S_IRUGO, sbi->s_proc,
700 				segment_bits_seq_show, sb);
701 		proc_create_single_data("iostat_info", S_IRUGO, sbi->s_proc,
702 				iostat_info_seq_show, sb);
703 		proc_create_single_data("victim_bits", S_IRUGO, sbi->s_proc,
704 				victim_bits_seq_show, sb);
705 	}
706 	return 0;
707 }
708 
f2fs_unregister_sysfs(struct f2fs_sb_info * sbi)709 void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
710 {
711 	if (sbi->s_proc) {
712 		remove_proc_entry("iostat_info", sbi->s_proc);
713 		remove_proc_entry("segment_info", sbi->s_proc);
714 		remove_proc_entry("segment_bits", sbi->s_proc);
715 		remove_proc_entry("victim_bits", sbi->s_proc);
716 		remove_proc_entry(sbi->sb->s_id, f2fs_proc_root);
717 	}
718 	kobject_del(&sbi->s_kobj);
719 	kobject_put(&sbi->s_kobj);
720 	wait_for_completion(&sbi->s_kobj_unregister);
721 }
722