Linux Audio

Check our new training course

Loading...
v6.13.7
   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#include <linux/ioprio.h>
  15#include <linux/sysfs.h>
  16
  17#include "f2fs.h"
  18#include "segment.h"
  19#include "gc.h"
  20#include "iostat.h"
  21#include <trace/events/f2fs.h>
  22
  23static struct proc_dir_entry *f2fs_proc_root;
  24
  25/* Sysfs support for f2fs */
  26enum {
  27	GC_THREAD,	/* struct f2fs_gc_thread */
  28	SM_INFO,	/* struct f2fs_sm_info */
  29	DCC_INFO,	/* struct discard_cmd_control */
  30	NM_INFO,	/* struct f2fs_nm_info */
  31	F2FS_SBI,	/* struct f2fs_sb_info */
  32#ifdef CONFIG_F2FS_STAT_FS
  33	STAT_INFO,	/* struct f2fs_stat_info */
  34#endif
  35#ifdef CONFIG_F2FS_FAULT_INJECTION
  36	FAULT_INFO_RATE,	/* struct f2fs_fault_info */
  37	FAULT_INFO_TYPE,	/* struct f2fs_fault_info */
  38#endif
  39	RESERVED_BLOCKS,	/* struct f2fs_sb_info */
  40	CPRC_INFO,	/* struct ckpt_req_control */
  41	ATGC_INFO,	/* struct atgc_management */
  42};
  43
  44static const char *gc_mode_names[MAX_GC_MODE] = {
  45	"GC_NORMAL",
  46	"GC_IDLE_CB",
  47	"GC_IDLE_GREEDY",
  48	"GC_IDLE_AT",
  49	"GC_URGENT_HIGH",
  50	"GC_URGENT_LOW",
  51	"GC_URGENT_MID"
  52};
  53
  54struct f2fs_attr {
  55	struct attribute attr;
  56	ssize_t (*show)(struct f2fs_attr *a, struct f2fs_sb_info *sbi, char *buf);
  57	ssize_t (*store)(struct f2fs_attr *a, struct f2fs_sb_info *sbi,
  58			 const char *buf, size_t len);
  59	int struct_type;
  60	int offset;
  61	int id;
  62};
  63
  64static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
  65			     struct f2fs_sb_info *sbi, char *buf);
  66
  67static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type)
  68{
  69	if (struct_type == GC_THREAD)
  70		return (unsigned char *)sbi->gc_thread;
  71	else if (struct_type == SM_INFO)
  72		return (unsigned char *)SM_I(sbi);
  73	else if (struct_type == DCC_INFO)
  74		return (unsigned char *)SM_I(sbi)->dcc_info;
  75	else if (struct_type == NM_INFO)
  76		return (unsigned char *)NM_I(sbi);
  77	else if (struct_type == F2FS_SBI || struct_type == RESERVED_BLOCKS)
  78		return (unsigned char *)sbi;
  79#ifdef CONFIG_F2FS_FAULT_INJECTION
  80	else if (struct_type == FAULT_INFO_RATE ||
  81					struct_type == FAULT_INFO_TYPE)
  82		return (unsigned char *)&F2FS_OPTION(sbi).fault_info;
  83#endif
  84#ifdef CONFIG_F2FS_STAT_FS
  85	else if (struct_type == STAT_INFO)
  86		return (unsigned char *)F2FS_STAT(sbi);
  87#endif
  88	else if (struct_type == CPRC_INFO)
  89		return (unsigned char *)&sbi->cprc_info;
  90	else if (struct_type == ATGC_INFO)
  91		return (unsigned char *)&sbi->am;
  92	return NULL;
  93}
  94
  95static ssize_t dirty_segments_show(struct f2fs_attr *a,
  96		struct f2fs_sb_info *sbi, char *buf)
  97{
  98	return sysfs_emit(buf, "%llu\n",
  99			(unsigned long long)(dirty_segments(sbi)));
 100}
 101
 102static ssize_t free_segments_show(struct f2fs_attr *a,
 103		struct f2fs_sb_info *sbi, char *buf)
 104{
 105	return sysfs_emit(buf, "%llu\n",
 106			(unsigned long long)(free_segments(sbi)));
 107}
 108
 109static ssize_t ovp_segments_show(struct f2fs_attr *a,
 110		struct f2fs_sb_info *sbi, char *buf)
 111{
 112	return sysfs_emit(buf, "%llu\n",
 113			(unsigned long long)(overprovision_segments(sbi)));
 114}
 115
 116static ssize_t lifetime_write_kbytes_show(struct f2fs_attr *a,
 117		struct f2fs_sb_info *sbi, char *buf)
 118{
 119	return sysfs_emit(buf, "%llu\n",
 120			(unsigned long long)(sbi->kbytes_written +
 121			((f2fs_get_sectors_written(sbi) -
 122				sbi->sectors_written_start) >> 1)));
 123}
 124
 125static ssize_t sb_status_show(struct f2fs_attr *a,
 126		struct f2fs_sb_info *sbi, char *buf)
 127{
 128	return sysfs_emit(buf, "%lx\n", sbi->s_flag);
 129}
 130
 131static ssize_t cp_status_show(struct f2fs_attr *a,
 132		struct f2fs_sb_info *sbi, char *buf)
 133{
 134	return sysfs_emit(buf, "%x\n", le32_to_cpu(F2FS_CKPT(sbi)->ckpt_flags));
 135}
 136
 137static ssize_t pending_discard_show(struct f2fs_attr *a,
 138		struct f2fs_sb_info *sbi, char *buf)
 139{
 140	if (!SM_I(sbi)->dcc_info)
 141		return -EINVAL;
 142	return sysfs_emit(buf, "%llu\n", (unsigned long long)atomic_read(
 143				&SM_I(sbi)->dcc_info->discard_cmd_cnt));
 144}
 145
 146static ssize_t issued_discard_show(struct f2fs_attr *a,
 147		struct f2fs_sb_info *sbi, char *buf)
 148{
 149	if (!SM_I(sbi)->dcc_info)
 150		return -EINVAL;
 151	return sysfs_emit(buf, "%llu\n", (unsigned long long)atomic_read(
 152				&SM_I(sbi)->dcc_info->issued_discard));
 153}
 154
 155static ssize_t queued_discard_show(struct f2fs_attr *a,
 156		struct f2fs_sb_info *sbi, char *buf)
 157{
 158	if (!SM_I(sbi)->dcc_info)
 159		return -EINVAL;
 160	return sysfs_emit(buf, "%llu\n", (unsigned long long)atomic_read(
 161				&SM_I(sbi)->dcc_info->queued_discard));
 162}
 163
 164static ssize_t undiscard_blks_show(struct f2fs_attr *a,
 165		struct f2fs_sb_info *sbi, char *buf)
 166{
 167	if (!SM_I(sbi)->dcc_info)
 168		return -EINVAL;
 169	return sysfs_emit(buf, "%u\n",
 170				SM_I(sbi)->dcc_info->undiscard_blks);
 171}
 172
 173static ssize_t atgc_enabled_show(struct f2fs_attr *a,
 174		struct f2fs_sb_info *sbi, char *buf)
 175{
 176	return sysfs_emit(buf, "%d\n", sbi->am.atgc_enabled ? 1 : 0);
 177}
 178
 179static ssize_t gc_mode_show(struct f2fs_attr *a,
 180		struct f2fs_sb_info *sbi, char *buf)
 181{
 182	return sysfs_emit(buf, "%s\n", gc_mode_names[sbi->gc_mode]);
 183}
 184
 185static ssize_t features_show(struct f2fs_attr *a,
 186		struct f2fs_sb_info *sbi, char *buf)
 187{
 188	int len = 0;
 189
 190	if (f2fs_sb_has_encrypt(sbi))
 191		len += sysfs_emit_at(buf, len, "%s",
 192						"encryption");
 193	if (f2fs_sb_has_blkzoned(sbi))
 194		len += sysfs_emit_at(buf, len, "%s%s",
 195				len ? ", " : "", "blkzoned");
 196	if (f2fs_sb_has_extra_attr(sbi))
 197		len += sysfs_emit_at(buf, len, "%s%s",
 198				len ? ", " : "", "extra_attr");
 199	if (f2fs_sb_has_project_quota(sbi))
 200		len += sysfs_emit_at(buf, len, "%s%s",
 201				len ? ", " : "", "projquota");
 202	if (f2fs_sb_has_inode_chksum(sbi))
 203		len += sysfs_emit_at(buf, len, "%s%s",
 204				len ? ", " : "", "inode_checksum");
 205	if (f2fs_sb_has_flexible_inline_xattr(sbi))
 206		len += sysfs_emit_at(buf, len, "%s%s",
 207				len ? ", " : "", "flexible_inline_xattr");
 208	if (f2fs_sb_has_quota_ino(sbi))
 209		len += sysfs_emit_at(buf, len, "%s%s",
 210				len ? ", " : "", "quota_ino");
 211	if (f2fs_sb_has_inode_crtime(sbi))
 212		len += sysfs_emit_at(buf, len, "%s%s",
 213				len ? ", " : "", "inode_crtime");
 214	if (f2fs_sb_has_lost_found(sbi))
 215		len += sysfs_emit_at(buf, len, "%s%s",
 216				len ? ", " : "", "lost_found");
 217	if (f2fs_sb_has_verity(sbi))
 218		len += sysfs_emit_at(buf, len, "%s%s",
 219				len ? ", " : "", "verity");
 220	if (f2fs_sb_has_sb_chksum(sbi))
 221		len += sysfs_emit_at(buf, len, "%s%s",
 222				len ? ", " : "", "sb_checksum");
 223	if (f2fs_sb_has_casefold(sbi))
 224		len += sysfs_emit_at(buf, len, "%s%s",
 225				len ? ", " : "", "casefold");
 226	if (f2fs_sb_has_readonly(sbi))
 227		len += sysfs_emit_at(buf, len, "%s%s",
 228				len ? ", " : "", "readonly");
 229	if (f2fs_sb_has_compression(sbi))
 230		len += sysfs_emit_at(buf, len, "%s%s",
 231				len ? ", " : "", "compression");
 232	len += sysfs_emit_at(buf, len, "%s%s",
 233				len ? ", " : "", "pin_file");
 234	len += sysfs_emit_at(buf, len, "\n");
 235	return len;
 236}
 237
 238static ssize_t current_reserved_blocks_show(struct f2fs_attr *a,
 239					struct f2fs_sb_info *sbi, char *buf)
 240{
 241	return sysfs_emit(buf, "%u\n", sbi->current_reserved_blocks);
 242}
 243
 244static ssize_t unusable_show(struct f2fs_attr *a,
 245		struct f2fs_sb_info *sbi, char *buf)
 246{
 247	block_t unusable;
 248
 249	if (test_opt(sbi, DISABLE_CHECKPOINT))
 250		unusable = sbi->unusable_block_count;
 251	else
 252		unusable = f2fs_get_unusable_blocks(sbi);
 253	return sysfs_emit(buf, "%llu\n", (unsigned long long)unusable);
 254}
 255
 256static ssize_t encoding_show(struct f2fs_attr *a,
 257		struct f2fs_sb_info *sbi, char *buf)
 258{
 259#if IS_ENABLED(CONFIG_UNICODE)
 260	struct super_block *sb = sbi->sb;
 261
 262	if (f2fs_sb_has_casefold(sbi))
 263		return sysfs_emit(buf, "UTF-8 (%d.%d.%d)\n",
 264			(sb->s_encoding->version >> 16) & 0xff,
 265			(sb->s_encoding->version >> 8) & 0xff,
 266			sb->s_encoding->version & 0xff);
 267#endif
 268	return sysfs_emit(buf, "(none)\n");
 269}
 270
 271static ssize_t mounted_time_sec_show(struct f2fs_attr *a,
 272		struct f2fs_sb_info *sbi, char *buf)
 273{
 274	return sysfs_emit(buf, "%llu\n", SIT_I(sbi)->mounted_time);
 275}
 276
 277#ifdef CONFIG_F2FS_STAT_FS
 278static ssize_t moved_blocks_foreground_show(struct f2fs_attr *a,
 279				struct f2fs_sb_info *sbi, char *buf)
 280{
 281	struct f2fs_stat_info *si = F2FS_STAT(sbi);
 282
 283	return sysfs_emit(buf, "%llu\n",
 284		(unsigned long long)(si->tot_blks -
 285			(si->bg_data_blks + si->bg_node_blks)));
 286}
 287
 288static ssize_t moved_blocks_background_show(struct f2fs_attr *a,
 289				struct f2fs_sb_info *sbi, char *buf)
 290{
 291	struct f2fs_stat_info *si = F2FS_STAT(sbi);
 292
 293	return sysfs_emit(buf, "%llu\n",
 294		(unsigned long long)(si->bg_data_blks + si->bg_node_blks));
 295}
 296
 297static ssize_t avg_vblocks_show(struct f2fs_attr *a,
 298		struct f2fs_sb_info *sbi, char *buf)
 299{
 300	struct f2fs_stat_info *si = F2FS_STAT(sbi);
 301
 302	si->dirty_count = dirty_segments(sbi);
 303	f2fs_update_sit_info(sbi);
 304	return sysfs_emit(buf, "%llu\n", (unsigned long long)(si->avg_vblocks));
 305}
 306#endif
 307
 308static ssize_t main_blkaddr_show(struct f2fs_attr *a,
 309				struct f2fs_sb_info *sbi, char *buf)
 310{
 311	return sysfs_emit(buf, "%llu\n",
 312			(unsigned long long)MAIN_BLKADDR(sbi));
 313}
 314
 315static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
 316			struct f2fs_sb_info *sbi, char *buf)
 317{
 318	unsigned char *ptr = NULL;
 319	unsigned int *ui;
 320
 321	ptr = __struct_ptr(sbi, a->struct_type);
 322	if (!ptr)
 323		return -EINVAL;
 324
 325	if (!strcmp(a->attr.name, "extension_list")) {
 326		__u8 (*extlist)[F2FS_EXTENSION_LEN] =
 327					sbi->raw_super->extension_list;
 328		int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
 329		int hot_count = sbi->raw_super->hot_ext_count;
 330		int len = 0, i;
 331
 332		len += sysfs_emit_at(buf, len, "cold file extension:\n");
 
 333		for (i = 0; i < cold_count; i++)
 334			len += sysfs_emit_at(buf, len, "%s\n", extlist[i]);
 
 335
 336		len += sysfs_emit_at(buf, len, "hot file extension:\n");
 
 337		for (i = cold_count; i < cold_count + hot_count; i++)
 338			len += sysfs_emit_at(buf, len, "%s\n", extlist[i]);
 339
 340		return len;
 341	}
 342
 343	if (!strcmp(a->attr.name, "ckpt_thread_ioprio")) {
 344		struct ckpt_req_control *cprc = &sbi->cprc_info;
 345		int class = IOPRIO_PRIO_CLASS(cprc->ckpt_thread_ioprio);
 346		int level = IOPRIO_PRIO_LEVEL(cprc->ckpt_thread_ioprio);
 347
 348		if (class != IOPRIO_CLASS_RT && class != IOPRIO_CLASS_BE)
 349			return -EINVAL;
 350
 351		return sysfs_emit(buf, "%s,%d\n",
 352			class == IOPRIO_CLASS_RT ? "rt" : "be", level);
 353	}
 354
 355#ifdef CONFIG_F2FS_FS_COMPRESSION
 356	if (!strcmp(a->attr.name, "compr_written_block"))
 357		return sysfs_emit(buf, "%llu\n", sbi->compr_written_block);
 358
 359	if (!strcmp(a->attr.name, "compr_saved_block"))
 360		return sysfs_emit(buf, "%llu\n", sbi->compr_saved_block);
 361
 362	if (!strcmp(a->attr.name, "compr_new_inode"))
 363		return sysfs_emit(buf, "%u\n", sbi->compr_new_inode);
 364#endif
 365
 366	if (!strcmp(a->attr.name, "gc_segment_mode"))
 367		return sysfs_emit(buf, "%u\n", sbi->gc_segment_mode);
 368
 369	if (!strcmp(a->attr.name, "gc_reclaimed_segments")) {
 370		return sysfs_emit(buf, "%u\n",
 371			sbi->gc_reclaimed_segs[sbi->gc_segment_mode]);
 372	}
 373
 374	if (!strcmp(a->attr.name, "current_atomic_write")) {
 375		s64 current_write = atomic64_read(&sbi->current_atomic_write);
 376
 377		return sysfs_emit(buf, "%lld\n", current_write);
 378	}
 379
 380	if (!strcmp(a->attr.name, "peak_atomic_write"))
 381		return sysfs_emit(buf, "%lld\n", sbi->peak_atomic_write);
 382
 383	if (!strcmp(a->attr.name, "committed_atomic_block"))
 384		return sysfs_emit(buf, "%llu\n", sbi->committed_atomic_block);
 385
 386	if (!strcmp(a->attr.name, "revoked_atomic_block"))
 387		return sysfs_emit(buf, "%llu\n", sbi->revoked_atomic_block);
 388
 389#ifdef CONFIG_F2FS_STAT_FS
 390	if (!strcmp(a->attr.name, "cp_foreground_calls"))
 391		return sysfs_emit(buf, "%d\n",
 392				atomic_read(&sbi->cp_call_count[TOTAL_CALL]) -
 393				atomic_read(&sbi->cp_call_count[BACKGROUND]));
 394	if (!strcmp(a->attr.name, "cp_background_calls"))
 395		return sysfs_emit(buf, "%d\n",
 396				atomic_read(&sbi->cp_call_count[BACKGROUND]));
 397#endif
 398
 399	ui = (unsigned int *)(ptr + a->offset);
 400
 401	return sysfs_emit(buf, "%u\n", *ui);
 402}
 403
 404static ssize_t __sbi_store(struct f2fs_attr *a,
 405			struct f2fs_sb_info *sbi,
 406			const char *buf, size_t count)
 407{
 408	unsigned char *ptr;
 409	unsigned long t;
 410	unsigned int *ui;
 411	ssize_t ret;
 412
 413	ptr = __struct_ptr(sbi, a->struct_type);
 414	if (!ptr)
 415		return -EINVAL;
 416
 417	if (!strcmp(a->attr.name, "extension_list")) {
 418		const char *name = strim((char *)buf);
 419		bool set = true, hot;
 420
 421		if (!strncmp(name, "[h]", 3))
 422			hot = true;
 423		else if (!strncmp(name, "[c]", 3))
 424			hot = false;
 425		else
 426			return -EINVAL;
 427
 428		name += 3;
 429
 430		if (*name == '!') {
 431			name++;
 432			set = false;
 433		}
 434
 435		if (!strlen(name) || strlen(name) >= F2FS_EXTENSION_LEN)
 436			return -EINVAL;
 437
 438		f2fs_down_write(&sbi->sb_lock);
 439
 440		ret = f2fs_update_extension_list(sbi, name, hot, set);
 441		if (ret)
 442			goto out;
 443
 444		ret = f2fs_commit_super(sbi, false);
 445		if (ret)
 446			f2fs_update_extension_list(sbi, name, hot, !set);
 447out:
 448		f2fs_up_write(&sbi->sb_lock);
 449		return ret ? ret : count;
 450	}
 451
 452	if (!strcmp(a->attr.name, "ckpt_thread_ioprio")) {
 453		const char *name = strim((char *)buf);
 454		struct ckpt_req_control *cprc = &sbi->cprc_info;
 455		int class;
 456		long level;
 457		int ret;
 458
 459		if (!strncmp(name, "rt,", 3))
 460			class = IOPRIO_CLASS_RT;
 461		else if (!strncmp(name, "be,", 3))
 462			class = IOPRIO_CLASS_BE;
 463		else
 464			return -EINVAL;
 465
 466		name += 3;
 467		ret = kstrtol(name, 10, &level);
 468		if (ret)
 469			return ret;
 470		if (level >= IOPRIO_NR_LEVELS || level < 0)
 471			return -EINVAL;
 472
 473		cprc->ckpt_thread_ioprio = IOPRIO_PRIO_VALUE(class, level);
 474		if (test_opt(sbi, MERGE_CHECKPOINT)) {
 475			ret = set_task_ioprio(cprc->f2fs_issue_ckpt,
 476					cprc->ckpt_thread_ioprio);
 477			if (ret)
 478				return ret;
 479		}
 480
 481		return count;
 482	}
 483
 484	ui = (unsigned int *)(ptr + a->offset);
 485
 486	ret = kstrtoul(skip_spaces(buf), 0, &t);
 487	if (ret < 0)
 488		return ret;
 489#ifdef CONFIG_F2FS_FAULT_INJECTION
 490	if (a->struct_type == FAULT_INFO_TYPE) {
 491		if (f2fs_build_fault_attr(sbi, 0, t))
 492			return -EINVAL;
 493		return count;
 494	}
 495	if (a->struct_type == FAULT_INFO_RATE) {
 496		if (f2fs_build_fault_attr(sbi, t, 0))
 497			return -EINVAL;
 498		return count;
 499	}
 500#endif
 501	if (a->struct_type == RESERVED_BLOCKS) {
 502		spin_lock(&sbi->stat_lock);
 503		if (t > (unsigned long)(sbi->user_block_count -
 504				F2FS_OPTION(sbi).root_reserved_blocks)) {
 
 
 505			spin_unlock(&sbi->stat_lock);
 506			return -EINVAL;
 507		}
 508		*ui = t;
 509		sbi->current_reserved_blocks = min(sbi->reserved_blocks,
 510				sbi->user_block_count - valid_user_blocks(sbi));
 511		spin_unlock(&sbi->stat_lock);
 512		return count;
 513	}
 514
 515	if (!strcmp(a->attr.name, "discard_io_aware_gran")) {
 516		if (t > MAX_PLIST_NUM)
 517			return -EINVAL;
 518		if (!f2fs_block_unit_discard(sbi))
 519			return -EINVAL;
 520		if (t == *ui)
 521			return count;
 522		*ui = t;
 523		return count;
 524	}
 525
 526	if (!strcmp(a->attr.name, "discard_granularity")) {
 527		if (t == 0 || t > MAX_PLIST_NUM)
 528			return -EINVAL;
 529		if (!f2fs_block_unit_discard(sbi))
 530			return -EINVAL;
 531		if (t == *ui)
 532			return count;
 533		*ui = t;
 534		return count;
 535	}
 536
 537	if (!strcmp(a->attr.name, "max_ordered_discard")) {
 538		if (t == 0 || t > MAX_PLIST_NUM)
 539			return -EINVAL;
 540		if (!f2fs_block_unit_discard(sbi))
 541			return -EINVAL;
 542		*ui = t;
 543		return count;
 544	}
 545
 546	if (!strcmp(a->attr.name, "discard_urgent_util")) {
 547		if (t > 100)
 548			return -EINVAL;
 549		*ui = t;
 550		return count;
 551	}
 552
 553	if (!strcmp(a->attr.name, "discard_io_aware")) {
 554		if (t >= DPOLICY_IO_AWARE_MAX)
 555			return -EINVAL;
 556		*ui = t;
 557		return count;
 558	}
 559
 560	if (!strcmp(a->attr.name, "migration_granularity")) {
 561		if (t == 0 || t > SEGS_PER_SEC(sbi))
 562			return -EINVAL;
 563	}
 564
 565	if (!strcmp(a->attr.name, "migration_window_granularity")) {
 566		if (t == 0 || t > SEGS_PER_SEC(sbi))
 567			return -EINVAL;
 568	}
 569
 570	if (!strcmp(a->attr.name, "gc_urgent")) {
 571		if (t == 0) {
 572			sbi->gc_mode = GC_NORMAL;
 573		} else if (t == 1) {
 574			sbi->gc_mode = GC_URGENT_HIGH;
 575			if (sbi->gc_thread) {
 576				sbi->gc_thread->gc_wake = true;
 577				wake_up_interruptible_all(
 578					&sbi->gc_thread->gc_wait_queue_head);
 579				wake_up_discard_thread(sbi, true);
 580			}
 581		} else if (t == 2) {
 582			sbi->gc_mode = GC_URGENT_LOW;
 583		} else if (t == 3) {
 584			sbi->gc_mode = GC_URGENT_MID;
 585			if (sbi->gc_thread) {
 586				sbi->gc_thread->gc_wake = true;
 587				wake_up_interruptible_all(
 588					&sbi->gc_thread->gc_wait_queue_head);
 589			}
 590		} else {
 591			return -EINVAL;
 592		}
 593		return count;
 594	}
 595	if (!strcmp(a->attr.name, "gc_idle")) {
 596		if (t == GC_IDLE_CB) {
 597			sbi->gc_mode = GC_IDLE_CB;
 598		} else if (t == GC_IDLE_GREEDY) {
 599			sbi->gc_mode = GC_IDLE_GREEDY;
 600		} else if (t == GC_IDLE_AT) {
 601			if (!sbi->am.atgc_enabled)
 602				return -EINVAL;
 603			sbi->gc_mode = GC_IDLE_AT;
 604		} else {
 605			sbi->gc_mode = GC_NORMAL;
 606		}
 607		return count;
 608	}
 609
 610	if (!strcmp(a->attr.name, "gc_remaining_trials")) {
 611		spin_lock(&sbi->gc_remaining_trials_lock);
 612		sbi->gc_remaining_trials = t;
 613		spin_unlock(&sbi->gc_remaining_trials_lock);
 614
 615		return count;
 616	}
 617
 618#ifdef CONFIG_F2FS_IOSTAT
 619	if (!strcmp(a->attr.name, "iostat_enable")) {
 620		sbi->iostat_enable = !!t;
 621		if (!sbi->iostat_enable)
 622			f2fs_reset_iostat(sbi);
 623		return count;
 624	}
 625
 626	if (!strcmp(a->attr.name, "iostat_period_ms")) {
 627		if (t < MIN_IOSTAT_PERIOD_MS || t > MAX_IOSTAT_PERIOD_MS)
 628			return -EINVAL;
 629		spin_lock_irq(&sbi->iostat_lock);
 630		sbi->iostat_period_ms = (unsigned int)t;
 631		spin_unlock_irq(&sbi->iostat_lock);
 632		return count;
 633	}
 634#endif
 635
 636#ifdef CONFIG_BLK_DEV_ZONED
 637	if (!strcmp(a->attr.name, "blkzone_alloc_policy")) {
 638		if (t < BLKZONE_ALLOC_PRIOR_SEQ || t > BLKZONE_ALLOC_PRIOR_CONV)
 639			return -EINVAL;
 640		sbi->blkzone_alloc_policy = t;
 641		return count;
 642	}
 643#endif
 644
 645#ifdef CONFIG_F2FS_FS_COMPRESSION
 646	if (!strcmp(a->attr.name, "compr_written_block") ||
 647		!strcmp(a->attr.name, "compr_saved_block")) {
 648		if (t != 0)
 649			return -EINVAL;
 650		sbi->compr_written_block = 0;
 651		sbi->compr_saved_block = 0;
 652		return count;
 653	}
 654
 655	if (!strcmp(a->attr.name, "compr_new_inode")) {
 656		if (t != 0)
 657			return -EINVAL;
 658		sbi->compr_new_inode = 0;
 659		return count;
 660	}
 661
 662	if (!strcmp(a->attr.name, "compress_percent")) {
 663		if (t == 0 || t > 100)
 664			return -EINVAL;
 665		*ui = t;
 666		return count;
 667	}
 668
 669	if (!strcmp(a->attr.name, "compress_watermark")) {
 670		if (t == 0 || t > 100)
 671			return -EINVAL;
 672		*ui = t;
 673		return count;
 674	}
 675#endif
 676
 677	if (!strcmp(a->attr.name, "atgc_candidate_ratio")) {
 678		if (t > 100)
 679			return -EINVAL;
 680		sbi->am.candidate_ratio = t;
 681		return count;
 682	}
 683
 684	if (!strcmp(a->attr.name, "atgc_age_weight")) {
 685		if (t > 100)
 686			return -EINVAL;
 687		sbi->am.age_weight = t;
 688		return count;
 689	}
 690
 691	if (!strcmp(a->attr.name, "gc_segment_mode")) {
 692		if (t < MAX_GC_MODE)
 693			sbi->gc_segment_mode = t;
 694		else
 695			return -EINVAL;
 696		return count;
 697	}
 698
 699	if (!strcmp(a->attr.name, "gc_pin_file_threshold")) {
 700		if (t > MAX_GC_FAILED_PINNED_FILES)
 701			return -EINVAL;
 702		sbi->gc_pin_file_threshold = t;
 703		return count;
 704	}
 705
 706	if (!strcmp(a->attr.name, "gc_reclaimed_segments")) {
 707		if (t != 0)
 708			return -EINVAL;
 709		sbi->gc_reclaimed_segs[sbi->gc_segment_mode] = 0;
 710		return count;
 711	}
 712
 713	if (!strcmp(a->attr.name, "seq_file_ra_mul")) {
 714		if (t >= MIN_RA_MUL && t <= MAX_RA_MUL)
 715			sbi->seq_file_ra_mul = t;
 716		else
 717			return -EINVAL;
 718		return count;
 719	}
 720
 721	if (!strcmp(a->attr.name, "max_fragment_chunk")) {
 722		if (t >= MIN_FRAGMENT_SIZE && t <= MAX_FRAGMENT_SIZE)
 723			sbi->max_fragment_chunk = t;
 724		else
 725			return -EINVAL;
 726		return count;
 727	}
 728
 729	if (!strcmp(a->attr.name, "max_fragment_hole")) {
 730		if (t >= MIN_FRAGMENT_SIZE && t <= MAX_FRAGMENT_SIZE)
 731			sbi->max_fragment_hole = t;
 732		else
 733			return -EINVAL;
 734		return count;
 735	}
 736
 737	if (!strcmp(a->attr.name, "peak_atomic_write")) {
 738		if (t != 0)
 739			return -EINVAL;
 740		sbi->peak_atomic_write = 0;
 741		return count;
 742	}
 743
 744	if (!strcmp(a->attr.name, "committed_atomic_block")) {
 745		if (t != 0)
 746			return -EINVAL;
 747		sbi->committed_atomic_block = 0;
 748		return count;
 749	}
 750
 751	if (!strcmp(a->attr.name, "revoked_atomic_block")) {
 752		if (t != 0)
 753			return -EINVAL;
 754		sbi->revoked_atomic_block = 0;
 755		return count;
 756	}
 757
 758	if (!strcmp(a->attr.name, "readdir_ra")) {
 759		sbi->readdir_ra = !!t;
 760		return count;
 761	}
 762
 763	if (!strcmp(a->attr.name, "hot_data_age_threshold")) {
 764		if (t == 0 || t >= sbi->warm_data_age_threshold)
 765			return -EINVAL;
 766		if (t == *ui)
 767			return count;
 768		*ui = (unsigned int)t;
 769		return count;
 770	}
 771
 772	if (!strcmp(a->attr.name, "warm_data_age_threshold")) {
 773		if (t <= sbi->hot_data_age_threshold)
 774			return -EINVAL;
 775		if (t == *ui)
 776			return count;
 777		*ui = (unsigned int)t;
 778		return count;
 779	}
 780
 781	if (!strcmp(a->attr.name, "last_age_weight")) {
 782		if (t > 100)
 783			return -EINVAL;
 784		if (t == *ui)
 785			return count;
 786		*ui = (unsigned int)t;
 787		return count;
 788	}
 789
 790	if (!strcmp(a->attr.name, "max_read_extent_count")) {
 791		if (t > UINT_MAX)
 792			return -EINVAL;
 793		*ui = (unsigned int)t;
 794		return count;
 795	}
 796
 797	if (!strcmp(a->attr.name, "ipu_policy")) {
 798		if (t >= BIT(F2FS_IPU_MAX))
 799			return -EINVAL;
 800		/* allow F2FS_IPU_NOCACHE only for IPU in the pinned file */
 801		if (f2fs_lfs_mode(sbi) && (t & ~BIT(F2FS_IPU_NOCACHE)))
 802			return -EINVAL;
 803		SM_I(sbi)->ipu_policy = (unsigned int)t;
 804		return count;
 805	}
 806
 807	if (!strcmp(a->attr.name, "dir_level")) {
 808		if (t > MAX_DIR_HASH_DEPTH)
 809			return -EINVAL;
 810		sbi->dir_level = t;
 811		return count;
 812	}
 813
 814	*ui = (unsigned int)t;
 815
 816	return count;
 817}
 818
 819static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
 820			struct f2fs_sb_info *sbi,
 821			const char *buf, size_t count)
 822{
 823	ssize_t ret;
 824	bool gc_entry = (!strcmp(a->attr.name, "gc_urgent") ||
 825					a->struct_type == GC_THREAD);
 826
 827	if (gc_entry) {
 828		if (!down_read_trylock(&sbi->sb->s_umount))
 829			return -EAGAIN;
 830	}
 831	ret = __sbi_store(a, sbi, buf, count);
 832	if (gc_entry)
 833		up_read(&sbi->sb->s_umount);
 834
 835	return ret;
 836}
 837
 838static ssize_t f2fs_attr_show(struct kobject *kobj,
 839				struct attribute *attr, char *buf)
 840{
 841	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
 842								s_kobj);
 843	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
 844
 845	return a->show ? a->show(a, sbi, buf) : 0;
 846}
 847
 848static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
 849						const char *buf, size_t len)
 850{
 851	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
 852									s_kobj);
 853	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
 854
 855	return a->store ? a->store(a, sbi, buf, len) : 0;
 856}
 857
 858static void f2fs_sb_release(struct kobject *kobj)
 859{
 860	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
 861								s_kobj);
 862	complete(&sbi->s_kobj_unregister);
 863}
 864
 865/*
 866 * Note that there are three feature list entries:
 867 * 1) /sys/fs/f2fs/features
 868 *   : shows runtime features supported by in-kernel f2fs along with Kconfig.
 869 *     - ref. F2FS_FEATURE_RO_ATTR()
 870 *
 871 * 2) /sys/fs/f2fs/$s_id/features <deprecated>
 872 *   : shows on-disk features enabled by mkfs.f2fs, used for old kernels. This
 873 *     won't add new feature anymore, and thus, users should check entries in 3)
 874 *     instead of this 2).
 875 *
 876 * 3) /sys/fs/f2fs/$s_id/feature_list
 877 *   : shows on-disk features enabled by mkfs.f2fs per instance, which follows
 878 *     sysfs entry rule where each entry should expose single value.
 879 *     This list covers old feature list provided by 2) and beyond. Therefore,
 880 *     please add new on-disk feature in this list only.
 881 *     - ref. F2FS_SB_FEATURE_RO_ATTR()
 882 */
 883static ssize_t f2fs_feature_show(struct f2fs_attr *a,
 884		struct f2fs_sb_info *sbi, char *buf)
 885{
 886	return sysfs_emit(buf, "supported\n");
 887}
 888
 889#define F2FS_FEATURE_RO_ATTR(_name)				\
 890static struct f2fs_attr f2fs_attr_##_name = {			\
 891	.attr = {.name = __stringify(_name), .mode = 0444 },	\
 892	.show	= f2fs_feature_show,				\
 893}
 894
 895static ssize_t f2fs_sb_feature_show(struct f2fs_attr *a,
 896		struct f2fs_sb_info *sbi, char *buf)
 897{
 898	if (F2FS_HAS_FEATURE(sbi, a->id))
 899		return sysfs_emit(buf, "supported\n");
 900	return sysfs_emit(buf, "unsupported\n");
 901}
 902
 903#define F2FS_SB_FEATURE_RO_ATTR(_name, _feat)			\
 904static struct f2fs_attr f2fs_attr_sb_##_name = {		\
 905	.attr = {.name = __stringify(_name), .mode = 0444 },	\
 906	.show	= f2fs_sb_feature_show,				\
 907	.id	= F2FS_FEATURE_##_feat,				\
 908}
 909
 910#define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
 911static struct f2fs_attr f2fs_attr_##_name = {			\
 912	.attr = {.name = __stringify(_name), .mode = _mode },	\
 913	.show	= _show,					\
 914	.store	= _store,					\
 915	.struct_type = _struct_type,				\
 916	.offset = _offset					\
 917}
 918
 919#define F2FS_RO_ATTR(struct_type, struct_name, name, elname)	\
 920	F2FS_ATTR_OFFSET(struct_type, name, 0444,		\
 921		f2fs_sbi_show, NULL,				\
 922		offsetof(struct struct_name, elname))
 923
 924#define F2FS_RW_ATTR(struct_type, struct_name, name, elname)	\
 925	F2FS_ATTR_OFFSET(struct_type, name, 0644,		\
 926		f2fs_sbi_show, f2fs_sbi_store,			\
 927		offsetof(struct struct_name, elname))
 928
 929#define F2FS_GENERAL_RO_ATTR(name) \
 930static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
 931
 932#ifdef CONFIG_F2FS_STAT_FS
 933#define STAT_INFO_RO_ATTR(name, elname)				\
 934	F2FS_RO_ATTR(STAT_INFO, f2fs_stat_info, name, elname)
 935#endif
 936
 937#define GC_THREAD_RW_ATTR(name, elname)				\
 938	F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, name, elname)
 939
 940#define SM_INFO_RW_ATTR(name, elname)				\
 941	F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, name, elname)
 942
 943#define SM_INFO_GENERAL_RW_ATTR(elname)				\
 944	SM_INFO_RW_ATTR(elname, elname)
 945
 946#define DCC_INFO_RW_ATTR(name, elname)				\
 947	F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, name, elname)
 948
 949#define DCC_INFO_GENERAL_RW_ATTR(elname)			\
 950	DCC_INFO_RW_ATTR(elname, elname)
 951
 952#define NM_INFO_RW_ATTR(name, elname)				\
 953	F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, name, elname)
 954
 955#define NM_INFO_GENERAL_RW_ATTR(elname)				\
 956	NM_INFO_RW_ATTR(elname, elname)
 957
 958#define F2FS_SBI_RW_ATTR(name, elname)				\
 959	F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, name, elname)
 960
 961#define F2FS_SBI_GENERAL_RW_ATTR(elname)			\
 962	F2FS_SBI_RW_ATTR(elname, elname)
 963
 964#define F2FS_SBI_GENERAL_RO_ATTR(elname)			\
 965	F2FS_RO_ATTR(F2FS_SBI, f2fs_sb_info, elname, elname)
 966
 967#ifdef CONFIG_F2FS_FAULT_INJECTION
 968#define FAULT_INFO_GENERAL_RW_ATTR(type, elname)		\
 969	F2FS_RW_ATTR(type, f2fs_fault_info, elname, elname)
 970#endif
 971
 972#define RESERVED_BLOCKS_GENERAL_RW_ATTR(elname)			\
 973	F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, elname, elname)
 974
 975#define CPRC_INFO_GENERAL_RW_ATTR(elname)			\
 976	F2FS_RW_ATTR(CPRC_INFO, ckpt_req_control, elname, elname)
 977
 978#define ATGC_INFO_RW_ATTR(name, elname)				\
 979	F2FS_RW_ATTR(ATGC_INFO, atgc_management, name, elname)
 980
 981/* GC_THREAD ATTR */
 982GC_THREAD_RW_ATTR(gc_urgent_sleep_time, urgent_sleep_time);
 983GC_THREAD_RW_ATTR(gc_min_sleep_time, min_sleep_time);
 984GC_THREAD_RW_ATTR(gc_max_sleep_time, max_sleep_time);
 985GC_THREAD_RW_ATTR(gc_no_gc_sleep_time, no_gc_sleep_time);
 986GC_THREAD_RW_ATTR(gc_no_zoned_gc_percent, no_zoned_gc_percent);
 987GC_THREAD_RW_ATTR(gc_boost_zoned_gc_percent, boost_zoned_gc_percent);
 988GC_THREAD_RW_ATTR(gc_valid_thresh_ratio, valid_thresh_ratio);
 989
 990/* SM_INFO ATTR */
 991SM_INFO_RW_ATTR(reclaim_segments, rec_prefree_segments);
 992SM_INFO_GENERAL_RW_ATTR(ipu_policy);
 993SM_INFO_GENERAL_RW_ATTR(min_ipu_util);
 994SM_INFO_GENERAL_RW_ATTR(min_fsync_blocks);
 995SM_INFO_GENERAL_RW_ATTR(min_seq_blocks);
 996SM_INFO_GENERAL_RW_ATTR(min_hot_blocks);
 997SM_INFO_GENERAL_RW_ATTR(min_ssr_sections);
 998SM_INFO_GENERAL_RW_ATTR(reserved_segments);
 999
1000/* DCC_INFO ATTR */
1001DCC_INFO_RW_ATTR(max_small_discards, max_discards);
1002DCC_INFO_GENERAL_RW_ATTR(max_discard_request);
1003DCC_INFO_GENERAL_RW_ATTR(min_discard_issue_time);
1004DCC_INFO_GENERAL_RW_ATTR(mid_discard_issue_time);
1005DCC_INFO_GENERAL_RW_ATTR(max_discard_issue_time);
1006DCC_INFO_GENERAL_RW_ATTR(discard_io_aware_gran);
1007DCC_INFO_GENERAL_RW_ATTR(discard_urgent_util);
1008DCC_INFO_GENERAL_RW_ATTR(discard_granularity);
1009DCC_INFO_GENERAL_RW_ATTR(max_ordered_discard);
1010DCC_INFO_GENERAL_RW_ATTR(discard_io_aware);
1011
1012/* NM_INFO ATTR */
1013NM_INFO_RW_ATTR(max_roll_forward_node_blocks, max_rf_node_blocks);
1014NM_INFO_GENERAL_RW_ATTR(ram_thresh);
1015NM_INFO_GENERAL_RW_ATTR(ra_nid_pages);
1016NM_INFO_GENERAL_RW_ATTR(dirty_nats_ratio);
1017
1018/* F2FS_SBI ATTR */
1019F2FS_RW_ATTR(F2FS_SBI, f2fs_super_block, extension_list, extension_list);
1020F2FS_SBI_RW_ATTR(gc_idle, gc_mode);
1021F2FS_SBI_RW_ATTR(gc_urgent, gc_mode);
1022F2FS_SBI_RW_ATTR(cp_interval, interval_time[CP_TIME]);
1023F2FS_SBI_RW_ATTR(idle_interval, interval_time[REQ_TIME]);
1024F2FS_SBI_RW_ATTR(discard_idle_interval, interval_time[DISCARD_TIME]);
1025F2FS_SBI_RW_ATTR(gc_idle_interval, interval_time[GC_TIME]);
1026F2FS_SBI_RW_ATTR(umount_discard_timeout, interval_time[UMOUNT_DISCARD_TIMEOUT]);
1027F2FS_SBI_RW_ATTR(gc_pin_file_thresh, gc_pin_file_threshold);
1028F2FS_SBI_RW_ATTR(gc_reclaimed_segments, gc_reclaimed_segs);
1029F2FS_SBI_GENERAL_RW_ATTR(max_victim_search);
1030F2FS_SBI_GENERAL_RW_ATTR(migration_granularity);
1031F2FS_SBI_GENERAL_RW_ATTR(migration_window_granularity);
1032F2FS_SBI_GENERAL_RW_ATTR(dir_level);
1033#ifdef CONFIG_F2FS_IOSTAT
1034F2FS_SBI_GENERAL_RW_ATTR(iostat_enable);
1035F2FS_SBI_GENERAL_RW_ATTR(iostat_period_ms);
1036#endif
1037F2FS_SBI_GENERAL_RW_ATTR(readdir_ra);
1038F2FS_SBI_GENERAL_RW_ATTR(max_io_bytes);
1039F2FS_SBI_GENERAL_RW_ATTR(data_io_flag);
1040F2FS_SBI_GENERAL_RW_ATTR(node_io_flag);
1041F2FS_SBI_GENERAL_RW_ATTR(gc_remaining_trials);
1042F2FS_SBI_GENERAL_RW_ATTR(seq_file_ra_mul);
1043F2FS_SBI_GENERAL_RW_ATTR(gc_segment_mode);
1044F2FS_SBI_GENERAL_RW_ATTR(max_fragment_chunk);
1045F2FS_SBI_GENERAL_RW_ATTR(max_fragment_hole);
1046#ifdef CONFIG_F2FS_FS_COMPRESSION
1047F2FS_SBI_GENERAL_RW_ATTR(compr_written_block);
1048F2FS_SBI_GENERAL_RW_ATTR(compr_saved_block);
1049F2FS_SBI_GENERAL_RW_ATTR(compr_new_inode);
1050F2FS_SBI_GENERAL_RW_ATTR(compress_percent);
1051F2FS_SBI_GENERAL_RW_ATTR(compress_watermark);
1052#endif
1053/* atomic write */
1054F2FS_SBI_GENERAL_RO_ATTR(current_atomic_write);
1055F2FS_SBI_GENERAL_RW_ATTR(peak_atomic_write);
1056F2FS_SBI_GENERAL_RW_ATTR(committed_atomic_block);
1057F2FS_SBI_GENERAL_RW_ATTR(revoked_atomic_block);
1058/* block age extent cache */
1059F2FS_SBI_GENERAL_RW_ATTR(hot_data_age_threshold);
1060F2FS_SBI_GENERAL_RW_ATTR(warm_data_age_threshold);
1061F2FS_SBI_GENERAL_RW_ATTR(last_age_weight);
1062/* read extent cache */
1063F2FS_SBI_GENERAL_RW_ATTR(max_read_extent_count);
1064#ifdef CONFIG_BLK_DEV_ZONED
1065F2FS_SBI_GENERAL_RO_ATTR(unusable_blocks_per_sec);
1066F2FS_SBI_GENERAL_RW_ATTR(blkzone_alloc_policy);
1067#endif
1068
1069/* STAT_INFO ATTR */
1070#ifdef CONFIG_F2FS_STAT_FS
1071STAT_INFO_RO_ATTR(cp_foreground_calls, cp_call_count[FOREGROUND]);
1072STAT_INFO_RO_ATTR(cp_background_calls, cp_call_count[BACKGROUND]);
1073STAT_INFO_RO_ATTR(gc_foreground_calls, gc_call_count[FOREGROUND]);
1074STAT_INFO_RO_ATTR(gc_background_calls, gc_call_count[BACKGROUND]);
1075#endif
1076
1077/* FAULT_INFO ATTR */
1078#ifdef CONFIG_F2FS_FAULT_INJECTION
1079FAULT_INFO_GENERAL_RW_ATTR(FAULT_INFO_RATE, inject_rate);
1080FAULT_INFO_GENERAL_RW_ATTR(FAULT_INFO_TYPE, inject_type);
1081#endif
1082
1083/* RESERVED_BLOCKS ATTR */
1084RESERVED_BLOCKS_GENERAL_RW_ATTR(reserved_blocks);
1085
1086/* CPRC_INFO ATTR */
1087CPRC_INFO_GENERAL_RW_ATTR(ckpt_thread_ioprio);
1088
1089/* ATGC_INFO ATTR */
1090ATGC_INFO_RW_ATTR(atgc_candidate_ratio, candidate_ratio);
1091ATGC_INFO_RW_ATTR(atgc_candidate_count, max_candidate_count);
1092ATGC_INFO_RW_ATTR(atgc_age_weight, age_weight);
1093ATGC_INFO_RW_ATTR(atgc_age_threshold, age_threshold);
1094
1095F2FS_GENERAL_RO_ATTR(dirty_segments);
1096F2FS_GENERAL_RO_ATTR(free_segments);
1097F2FS_GENERAL_RO_ATTR(ovp_segments);
1098F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
1099F2FS_GENERAL_RO_ATTR(features);
1100F2FS_GENERAL_RO_ATTR(current_reserved_blocks);
1101F2FS_GENERAL_RO_ATTR(unusable);
1102F2FS_GENERAL_RO_ATTR(encoding);
1103F2FS_GENERAL_RO_ATTR(mounted_time_sec);
1104F2FS_GENERAL_RO_ATTR(main_blkaddr);
1105F2FS_GENERAL_RO_ATTR(pending_discard);
1106F2FS_GENERAL_RO_ATTR(atgc_enabled);
1107F2FS_GENERAL_RO_ATTR(gc_mode);
1108#ifdef CONFIG_F2FS_STAT_FS
1109F2FS_GENERAL_RO_ATTR(moved_blocks_background);
1110F2FS_GENERAL_RO_ATTR(moved_blocks_foreground);
1111F2FS_GENERAL_RO_ATTR(avg_vblocks);
1112#endif
1113
1114#ifdef CONFIG_FS_ENCRYPTION
1115F2FS_FEATURE_RO_ATTR(encryption);
1116F2FS_FEATURE_RO_ATTR(test_dummy_encryption_v2);
1117#if IS_ENABLED(CONFIG_UNICODE)
1118F2FS_FEATURE_RO_ATTR(encrypted_casefold);
1119#endif
1120#endif /* CONFIG_FS_ENCRYPTION */
1121#ifdef CONFIG_BLK_DEV_ZONED
1122F2FS_FEATURE_RO_ATTR(block_zoned);
1123#endif
1124F2FS_FEATURE_RO_ATTR(atomic_write);
1125F2FS_FEATURE_RO_ATTR(extra_attr);
1126F2FS_FEATURE_RO_ATTR(project_quota);
1127F2FS_FEATURE_RO_ATTR(inode_checksum);
1128F2FS_FEATURE_RO_ATTR(flexible_inline_xattr);
1129F2FS_FEATURE_RO_ATTR(quota_ino);
1130F2FS_FEATURE_RO_ATTR(inode_crtime);
1131F2FS_FEATURE_RO_ATTR(lost_found);
1132#ifdef CONFIG_FS_VERITY
1133F2FS_FEATURE_RO_ATTR(verity);
1134#endif
1135F2FS_FEATURE_RO_ATTR(sb_checksum);
1136#if IS_ENABLED(CONFIG_UNICODE)
1137F2FS_FEATURE_RO_ATTR(casefold);
1138#endif
1139F2FS_FEATURE_RO_ATTR(readonly);
1140#ifdef CONFIG_F2FS_FS_COMPRESSION
1141F2FS_FEATURE_RO_ATTR(compression);
1142#endif
1143F2FS_FEATURE_RO_ATTR(pin_file);
1144
1145#define ATTR_LIST(name) (&f2fs_attr_##name.attr)
1146static struct attribute *f2fs_attrs[] = {
1147	ATTR_LIST(gc_urgent_sleep_time),
1148	ATTR_LIST(gc_min_sleep_time),
1149	ATTR_LIST(gc_max_sleep_time),
1150	ATTR_LIST(gc_no_gc_sleep_time),
1151	ATTR_LIST(gc_no_zoned_gc_percent),
1152	ATTR_LIST(gc_boost_zoned_gc_percent),
1153	ATTR_LIST(gc_valid_thresh_ratio),
1154	ATTR_LIST(gc_idle),
1155	ATTR_LIST(gc_urgent),
1156	ATTR_LIST(reclaim_segments),
1157	ATTR_LIST(main_blkaddr),
1158	ATTR_LIST(max_small_discards),
1159	ATTR_LIST(max_discard_request),
1160	ATTR_LIST(min_discard_issue_time),
1161	ATTR_LIST(mid_discard_issue_time),
1162	ATTR_LIST(max_discard_issue_time),
1163	ATTR_LIST(discard_io_aware_gran),
1164	ATTR_LIST(discard_urgent_util),
1165	ATTR_LIST(discard_granularity),
1166	ATTR_LIST(max_ordered_discard),
1167	ATTR_LIST(discard_io_aware),
1168	ATTR_LIST(pending_discard),
1169	ATTR_LIST(gc_mode),
1170	ATTR_LIST(ipu_policy),
1171	ATTR_LIST(min_ipu_util),
1172	ATTR_LIST(min_fsync_blocks),
1173	ATTR_LIST(min_seq_blocks),
1174	ATTR_LIST(min_hot_blocks),
1175	ATTR_LIST(min_ssr_sections),
1176	ATTR_LIST(reserved_segments),
1177	ATTR_LIST(max_victim_search),
1178	ATTR_LIST(migration_granularity),
1179	ATTR_LIST(migration_window_granularity),
1180	ATTR_LIST(dir_level),
1181	ATTR_LIST(ram_thresh),
1182	ATTR_LIST(ra_nid_pages),
1183	ATTR_LIST(dirty_nats_ratio),
1184	ATTR_LIST(max_roll_forward_node_blocks),
1185	ATTR_LIST(cp_interval),
1186	ATTR_LIST(idle_interval),
1187	ATTR_LIST(discard_idle_interval),
1188	ATTR_LIST(gc_idle_interval),
1189	ATTR_LIST(umount_discard_timeout),
1190#ifdef CONFIG_F2FS_IOSTAT
1191	ATTR_LIST(iostat_enable),
1192	ATTR_LIST(iostat_period_ms),
1193#endif
1194	ATTR_LIST(readdir_ra),
1195	ATTR_LIST(max_io_bytes),
1196	ATTR_LIST(gc_pin_file_thresh),
1197	ATTR_LIST(extension_list),
1198#ifdef CONFIG_F2FS_FAULT_INJECTION
1199	ATTR_LIST(inject_rate),
1200	ATTR_LIST(inject_type),
1201#endif
1202	ATTR_LIST(data_io_flag),
1203	ATTR_LIST(node_io_flag),
1204	ATTR_LIST(gc_remaining_trials),
1205	ATTR_LIST(ckpt_thread_ioprio),
1206	ATTR_LIST(dirty_segments),
1207	ATTR_LIST(free_segments),
1208	ATTR_LIST(ovp_segments),
1209	ATTR_LIST(unusable),
1210	ATTR_LIST(lifetime_write_kbytes),
1211	ATTR_LIST(features),
1212	ATTR_LIST(reserved_blocks),
1213	ATTR_LIST(current_reserved_blocks),
1214	ATTR_LIST(encoding),
1215	ATTR_LIST(mounted_time_sec),
1216#ifdef CONFIG_F2FS_STAT_FS
1217	ATTR_LIST(cp_foreground_calls),
1218	ATTR_LIST(cp_background_calls),
1219	ATTR_LIST(gc_foreground_calls),
1220	ATTR_LIST(gc_background_calls),
1221	ATTR_LIST(moved_blocks_foreground),
1222	ATTR_LIST(moved_blocks_background),
1223	ATTR_LIST(avg_vblocks),
1224#endif
1225#ifdef CONFIG_BLK_DEV_ZONED
1226	ATTR_LIST(unusable_blocks_per_sec),
1227	ATTR_LIST(blkzone_alloc_policy),
1228#endif
1229#ifdef CONFIG_F2FS_FS_COMPRESSION
1230	ATTR_LIST(compr_written_block),
1231	ATTR_LIST(compr_saved_block),
1232	ATTR_LIST(compr_new_inode),
1233	ATTR_LIST(compress_percent),
1234	ATTR_LIST(compress_watermark),
1235#endif
1236	/* For ATGC */
1237	ATTR_LIST(atgc_candidate_ratio),
1238	ATTR_LIST(atgc_candidate_count),
1239	ATTR_LIST(atgc_age_weight),
1240	ATTR_LIST(atgc_age_threshold),
1241	ATTR_LIST(atgc_enabled),
1242	ATTR_LIST(seq_file_ra_mul),
1243	ATTR_LIST(gc_segment_mode),
1244	ATTR_LIST(gc_reclaimed_segments),
1245	ATTR_LIST(max_fragment_chunk),
1246	ATTR_LIST(max_fragment_hole),
1247	ATTR_LIST(current_atomic_write),
1248	ATTR_LIST(peak_atomic_write),
1249	ATTR_LIST(committed_atomic_block),
1250	ATTR_LIST(revoked_atomic_block),
1251	ATTR_LIST(hot_data_age_threshold),
1252	ATTR_LIST(warm_data_age_threshold),
1253	ATTR_LIST(last_age_weight),
1254	ATTR_LIST(max_read_extent_count),
1255	NULL,
1256};
1257ATTRIBUTE_GROUPS(f2fs);
1258
1259static struct attribute *f2fs_feat_attrs[] = {
1260#ifdef CONFIG_FS_ENCRYPTION
1261	ATTR_LIST(encryption),
1262	ATTR_LIST(test_dummy_encryption_v2),
1263#if IS_ENABLED(CONFIG_UNICODE)
1264	ATTR_LIST(encrypted_casefold),
1265#endif
1266#endif /* CONFIG_FS_ENCRYPTION */
1267#ifdef CONFIG_BLK_DEV_ZONED
1268	ATTR_LIST(block_zoned),
1269#endif
1270	ATTR_LIST(atomic_write),
1271	ATTR_LIST(extra_attr),
1272	ATTR_LIST(project_quota),
1273	ATTR_LIST(inode_checksum),
1274	ATTR_LIST(flexible_inline_xattr),
1275	ATTR_LIST(quota_ino),
1276	ATTR_LIST(inode_crtime),
1277	ATTR_LIST(lost_found),
1278#ifdef CONFIG_FS_VERITY
1279	ATTR_LIST(verity),
1280#endif
1281	ATTR_LIST(sb_checksum),
1282#if IS_ENABLED(CONFIG_UNICODE)
1283	ATTR_LIST(casefold),
1284#endif
1285	ATTR_LIST(readonly),
1286#ifdef CONFIG_F2FS_FS_COMPRESSION
1287	ATTR_LIST(compression),
1288#endif
1289	ATTR_LIST(pin_file),
1290	NULL,
1291};
1292ATTRIBUTE_GROUPS(f2fs_feat);
1293
1294F2FS_GENERAL_RO_ATTR(sb_status);
1295F2FS_GENERAL_RO_ATTR(cp_status);
1296F2FS_GENERAL_RO_ATTR(issued_discard);
1297F2FS_GENERAL_RO_ATTR(queued_discard);
1298F2FS_GENERAL_RO_ATTR(undiscard_blks);
1299
1300static struct attribute *f2fs_stat_attrs[] = {
1301	ATTR_LIST(sb_status),
1302	ATTR_LIST(cp_status),
1303	ATTR_LIST(issued_discard),
1304	ATTR_LIST(queued_discard),
1305	ATTR_LIST(undiscard_blks),
1306	NULL,
1307};
1308ATTRIBUTE_GROUPS(f2fs_stat);
1309
1310F2FS_SB_FEATURE_RO_ATTR(encryption, ENCRYPT);
1311F2FS_SB_FEATURE_RO_ATTR(block_zoned, BLKZONED);
1312F2FS_SB_FEATURE_RO_ATTR(extra_attr, EXTRA_ATTR);
1313F2FS_SB_FEATURE_RO_ATTR(project_quota, PRJQUOTA);
1314F2FS_SB_FEATURE_RO_ATTR(inode_checksum, INODE_CHKSUM);
1315F2FS_SB_FEATURE_RO_ATTR(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
1316F2FS_SB_FEATURE_RO_ATTR(quota_ino, QUOTA_INO);
1317F2FS_SB_FEATURE_RO_ATTR(inode_crtime, INODE_CRTIME);
1318F2FS_SB_FEATURE_RO_ATTR(lost_found, LOST_FOUND);
1319F2FS_SB_FEATURE_RO_ATTR(verity, VERITY);
1320F2FS_SB_FEATURE_RO_ATTR(sb_checksum, SB_CHKSUM);
1321F2FS_SB_FEATURE_RO_ATTR(casefold, CASEFOLD);
1322F2FS_SB_FEATURE_RO_ATTR(compression, COMPRESSION);
1323F2FS_SB_FEATURE_RO_ATTR(readonly, RO);
1324F2FS_SB_FEATURE_RO_ATTR(device_alias, DEVICE_ALIAS);
1325
1326static struct attribute *f2fs_sb_feat_attrs[] = {
1327	ATTR_LIST(sb_encryption),
1328	ATTR_LIST(sb_block_zoned),
1329	ATTR_LIST(sb_extra_attr),
1330	ATTR_LIST(sb_project_quota),
1331	ATTR_LIST(sb_inode_checksum),
1332	ATTR_LIST(sb_flexible_inline_xattr),
1333	ATTR_LIST(sb_quota_ino),
1334	ATTR_LIST(sb_inode_crtime),
1335	ATTR_LIST(sb_lost_found),
1336	ATTR_LIST(sb_verity),
1337	ATTR_LIST(sb_sb_checksum),
1338	ATTR_LIST(sb_casefold),
1339	ATTR_LIST(sb_compression),
1340	ATTR_LIST(sb_readonly),
1341	ATTR_LIST(sb_device_alias),
1342	NULL,
1343};
1344ATTRIBUTE_GROUPS(f2fs_sb_feat);
1345
1346static const struct sysfs_ops f2fs_attr_ops = {
1347	.show	= f2fs_attr_show,
1348	.store	= f2fs_attr_store,
1349};
1350
1351static const struct kobj_type f2fs_sb_ktype = {
1352	.default_groups = f2fs_groups,
1353	.sysfs_ops	= &f2fs_attr_ops,
1354	.release	= f2fs_sb_release,
1355};
1356
1357static const struct kobj_type f2fs_ktype = {
1358	.sysfs_ops	= &f2fs_attr_ops,
1359};
1360
1361static struct kset f2fs_kset = {
1362	.kobj	= {.ktype = &f2fs_ktype},
1363};
1364
1365static const struct kobj_type f2fs_feat_ktype = {
1366	.default_groups = f2fs_feat_groups,
1367	.sysfs_ops	= &f2fs_attr_ops,
1368};
1369
1370static struct kobject f2fs_feat = {
1371	.kset	= &f2fs_kset,
1372};
1373
1374static ssize_t f2fs_stat_attr_show(struct kobject *kobj,
1375				struct attribute *attr, char *buf)
1376{
1377	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1378								s_stat_kobj);
1379	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1380
1381	return a->show ? a->show(a, sbi, buf) : 0;
1382}
1383
1384static ssize_t f2fs_stat_attr_store(struct kobject *kobj, struct attribute *attr,
1385						const char *buf, size_t len)
1386{
1387	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1388								s_stat_kobj);
1389	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1390
1391	return a->store ? a->store(a, sbi, buf, len) : 0;
1392}
1393
1394static void f2fs_stat_kobj_release(struct kobject *kobj)
1395{
1396	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1397								s_stat_kobj);
1398	complete(&sbi->s_stat_kobj_unregister);
1399}
1400
1401static const struct sysfs_ops f2fs_stat_attr_ops = {
1402	.show	= f2fs_stat_attr_show,
1403	.store	= f2fs_stat_attr_store,
1404};
1405
1406static const struct kobj_type f2fs_stat_ktype = {
1407	.default_groups = f2fs_stat_groups,
1408	.sysfs_ops	= &f2fs_stat_attr_ops,
1409	.release	= f2fs_stat_kobj_release,
1410};
1411
1412static ssize_t f2fs_sb_feat_attr_show(struct kobject *kobj,
1413				struct attribute *attr, char *buf)
1414{
1415	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1416							s_feature_list_kobj);
1417	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1418
1419	return a->show ? a->show(a, sbi, buf) : 0;
1420}
1421
1422static void f2fs_feature_list_kobj_release(struct kobject *kobj)
1423{
1424	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1425							s_feature_list_kobj);
1426	complete(&sbi->s_feature_list_kobj_unregister);
1427}
1428
1429static const struct sysfs_ops f2fs_feature_list_attr_ops = {
1430	.show	= f2fs_sb_feat_attr_show,
1431};
1432
1433static const struct kobj_type f2fs_feature_list_ktype = {
1434	.default_groups = f2fs_sb_feat_groups,
1435	.sysfs_ops	= &f2fs_feature_list_attr_ops,
1436	.release	= f2fs_feature_list_kobj_release,
1437};
1438
1439static int __maybe_unused segment_info_seq_show(struct seq_file *seq,
1440						void *offset)
1441{
1442	struct super_block *sb = seq->private;
1443	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1444	unsigned int total_segs =
1445			le32_to_cpu(sbi->raw_super->segment_count_main);
1446	int i;
1447
1448	seq_puts(seq, "format: segment_type|valid_blocks\n"
1449		"segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
1450
1451	for (i = 0; i < total_segs; i++) {
1452		struct seg_entry *se = get_seg_entry(sbi, i);
1453
1454		if ((i % 10) == 0)
1455			seq_printf(seq, "%-10d", i);
1456		seq_printf(seq, "%d|%-3u", se->type, se->valid_blocks);
1457		if ((i % 10) == 9 || i == (total_segs - 1))
1458			seq_putc(seq, '\n');
1459		else
1460			seq_putc(seq, ' ');
1461	}
1462
1463	return 0;
1464}
1465
1466static int __maybe_unused segment_bits_seq_show(struct seq_file *seq,
1467						void *offset)
1468{
1469	struct super_block *sb = seq->private;
1470	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1471	unsigned int total_segs =
1472			le32_to_cpu(sbi->raw_super->segment_count_main);
1473	int i, j;
1474
1475	seq_puts(seq, "format: segment_type|valid_blocks|bitmaps\n"
1476		"segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
1477
1478	for (i = 0; i < total_segs; i++) {
1479		struct seg_entry *se = get_seg_entry(sbi, i);
1480
1481		seq_printf(seq, "%-10d", i);
1482		seq_printf(seq, "%d|%-3u|", se->type, se->valid_blocks);
1483		for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
1484			seq_printf(seq, " %.2x", se->cur_valid_map[j]);
1485		seq_putc(seq, '\n');
1486	}
1487	return 0;
1488}
1489
1490static int __maybe_unused victim_bits_seq_show(struct seq_file *seq,
1491						void *offset)
1492{
1493	struct super_block *sb = seq->private;
1494	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1495	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1496	int i;
1497
1498	seq_puts(seq, "format: victim_secmap bitmaps\n");
1499
1500	for (i = 0; i < MAIN_SECS(sbi); i++) {
1501		if ((i % 10) == 0)
1502			seq_printf(seq, "%-10d", i);
1503		seq_printf(seq, "%d", test_bit(i, dirty_i->victim_secmap) ? 1 : 0);
1504		if ((i % 10) == 9 || i == (MAIN_SECS(sbi) - 1))
1505			seq_putc(seq, '\n');
1506		else
1507			seq_putc(seq, ' ');
1508	}
1509	return 0;
1510}
1511
1512static int __maybe_unused discard_plist_seq_show(struct seq_file *seq,
1513						void *offset)
1514{
1515	struct super_block *sb = seq->private;
1516	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1517	struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1518	int i, count;
1519
1520	seq_puts(seq, "Discard pend list(Show diacrd_cmd count on each entry, .:not exist):\n");
1521	if (!f2fs_realtime_discard_enable(sbi))
1522		return 0;
1523
1524	if (dcc) {
1525		mutex_lock(&dcc->cmd_lock);
1526		for (i = 0; i < MAX_PLIST_NUM; i++) {
1527			struct list_head *pend_list;
1528			struct discard_cmd *dc, *tmp;
1529
1530			if (i % 8 == 0)
1531				seq_printf(seq, "  %-3d", i);
1532			count = 0;
1533			pend_list = &dcc->pend_list[i];
1534			list_for_each_entry_safe(dc, tmp, pend_list, list)
1535				count++;
1536			if (count)
1537				seq_printf(seq, " %7d", count);
1538			else
1539				seq_puts(seq, "       .");
1540			if (i % 8 == 7)
1541				seq_putc(seq, '\n');
1542		}
1543		seq_putc(seq, '\n');
1544		mutex_unlock(&dcc->cmd_lock);
1545	}
1546
1547	return 0;
1548}
1549
1550static int __maybe_unused disk_map_seq_show(struct seq_file *seq,
1551						void *offset)
1552{
1553	struct super_block *sb = seq->private;
1554	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1555	int i;
1556
1557	seq_printf(seq, "Address Layout   : %5luB Block address (# of Segments)\n",
1558					F2FS_BLKSIZE);
1559	seq_printf(seq, " SB            : %12s\n", "0/1024B");
1560	seq_printf(seq, " seg0_blkaddr  : 0x%010x\n", SEG0_BLKADDR(sbi));
1561	seq_printf(seq, " Checkpoint    : 0x%010x (%10d)\n",
1562			le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr), 2);
1563	seq_printf(seq, " SIT           : 0x%010x (%10d)\n",
1564			SIT_I(sbi)->sit_base_addr,
1565			le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_sit));
1566	seq_printf(seq, " NAT           : 0x%010x (%10d)\n",
1567			NM_I(sbi)->nat_blkaddr,
1568			le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_nat));
1569	seq_printf(seq, " SSA           : 0x%010x (%10d)\n",
1570			SM_I(sbi)->ssa_blkaddr,
1571			le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_ssa));
1572	seq_printf(seq, " Main          : 0x%010x (%10d)\n",
1573			SM_I(sbi)->main_blkaddr,
1574			le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_main));
1575	seq_printf(seq, " # of Sections : %12d\n",
1576			le32_to_cpu(F2FS_RAW_SUPER(sbi)->section_count));
1577	seq_printf(seq, " Segs/Sections : %12d\n",
1578			SEGS_PER_SEC(sbi));
1579	seq_printf(seq, " Section size  : %12d MB\n",
1580			SEGS_PER_SEC(sbi) << 1);
1581
1582	if (!f2fs_is_multi_device(sbi))
1583		return 0;
1584
1585	seq_puts(seq, "\nDisk Map for multi devices:\n");
1586	for (i = 0; i < sbi->s_ndevs; i++)
1587		seq_printf(seq, "Disk:%2d (zoned=%d): 0x%010x - 0x%010x on %s\n",
1588			i, bdev_is_zoned(FDEV(i).bdev),
1589			FDEV(i).start_blk, FDEV(i).end_blk,
1590			FDEV(i).path);
1591	return 0;
1592}
1593
1594int __init f2fs_init_sysfs(void)
1595{
1596	int ret;
1597
1598	kobject_set_name(&f2fs_kset.kobj, "f2fs");
1599	f2fs_kset.kobj.parent = fs_kobj;
1600	ret = kset_register(&f2fs_kset);
1601	if (ret)
1602		return ret;
1603
1604	ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
1605				   NULL, "features");
1606	if (ret)
1607		goto put_kobject;
1608
1609	f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
1610	if (!f2fs_proc_root) {
1611		ret = -ENOMEM;
1612		goto put_kobject;
1613	}
1614
1615	return 0;
1616put_kobject:
1617	kobject_put(&f2fs_feat);
1618	kset_unregister(&f2fs_kset);
1619	return ret;
1620}
1621
1622void f2fs_exit_sysfs(void)
1623{
1624	kobject_put(&f2fs_feat);
1625	kset_unregister(&f2fs_kset);
1626	remove_proc_entry("fs/f2fs", NULL);
1627	f2fs_proc_root = NULL;
1628}
1629
1630int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
1631{
1632	struct super_block *sb = sbi->sb;
1633	int err;
1634
1635	sbi->s_kobj.kset = &f2fs_kset;
1636	init_completion(&sbi->s_kobj_unregister);
1637	err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
1638				"%s", sb->s_id);
1639	if (err)
1640		goto put_sb_kobj;
1641
1642	sbi->s_stat_kobj.kset = &f2fs_kset;
1643	init_completion(&sbi->s_stat_kobj_unregister);
1644	err = kobject_init_and_add(&sbi->s_stat_kobj, &f2fs_stat_ktype,
1645						&sbi->s_kobj, "stat");
1646	if (err)
1647		goto put_stat_kobj;
1648
1649	sbi->s_feature_list_kobj.kset = &f2fs_kset;
1650	init_completion(&sbi->s_feature_list_kobj_unregister);
1651	err = kobject_init_and_add(&sbi->s_feature_list_kobj,
1652					&f2fs_feature_list_ktype,
1653					&sbi->s_kobj, "feature_list");
1654	if (err)
1655		goto put_feature_list_kobj;
1656
1657	sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
1658	if (!sbi->s_proc) {
1659		err = -ENOMEM;
1660		goto put_feature_list_kobj;
1661	}
1662
1663	proc_create_single_data("segment_info", 0444, sbi->s_proc,
1664				segment_info_seq_show, sb);
1665	proc_create_single_data("segment_bits", 0444, sbi->s_proc,
1666				segment_bits_seq_show, sb);
1667#ifdef CONFIG_F2FS_IOSTAT
1668	proc_create_single_data("iostat_info", 0444, sbi->s_proc,
1669				iostat_info_seq_show, sb);
1670#endif
1671	proc_create_single_data("victim_bits", 0444, sbi->s_proc,
1672				victim_bits_seq_show, sb);
1673	proc_create_single_data("discard_plist_info", 0444, sbi->s_proc,
1674				discard_plist_seq_show, sb);
1675	proc_create_single_data("disk_map", 0444, sbi->s_proc,
1676				disk_map_seq_show, sb);
1677	return 0;
1678put_feature_list_kobj:
1679	kobject_put(&sbi->s_feature_list_kobj);
1680	wait_for_completion(&sbi->s_feature_list_kobj_unregister);
1681put_stat_kobj:
1682	kobject_put(&sbi->s_stat_kobj);
1683	wait_for_completion(&sbi->s_stat_kobj_unregister);
1684put_sb_kobj:
1685	kobject_put(&sbi->s_kobj);
1686	wait_for_completion(&sbi->s_kobj_unregister);
1687	return err;
1688}
1689
1690void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
1691{
1692	remove_proc_subtree(sbi->sb->s_id, f2fs_proc_root);
1693
1694	kobject_put(&sbi->s_stat_kobj);
1695	wait_for_completion(&sbi->s_stat_kobj_unregister);
1696	kobject_put(&sbi->s_feature_list_kobj);
1697	wait_for_completion(&sbi->s_feature_list_kobj_unregister);
1698
1699	kobject_put(&sbi->s_kobj);
1700	wait_for_completion(&sbi->s_kobj_unregister);
1701}
v6.9.4
   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#include <linux/ioprio.h>
  15#include <linux/sysfs.h>
  16
  17#include "f2fs.h"
  18#include "segment.h"
  19#include "gc.h"
  20#include "iostat.h"
  21#include <trace/events/f2fs.h>
  22
  23static struct proc_dir_entry *f2fs_proc_root;
  24
  25/* Sysfs support for f2fs */
  26enum {
  27	GC_THREAD,	/* struct f2fs_gc_thread */
  28	SM_INFO,	/* struct f2fs_sm_info */
  29	DCC_INFO,	/* struct discard_cmd_control */
  30	NM_INFO,	/* struct f2fs_nm_info */
  31	F2FS_SBI,	/* struct f2fs_sb_info */
  32#ifdef CONFIG_F2FS_STAT_FS
  33	STAT_INFO,	/* struct f2fs_stat_info */
  34#endif
  35#ifdef CONFIG_F2FS_FAULT_INJECTION
  36	FAULT_INFO_RATE,	/* struct f2fs_fault_info */
  37	FAULT_INFO_TYPE,	/* struct f2fs_fault_info */
  38#endif
  39	RESERVED_BLOCKS,	/* struct f2fs_sb_info */
  40	CPRC_INFO,	/* struct ckpt_req_control */
  41	ATGC_INFO,	/* struct atgc_management */
  42};
  43
  44static const char *gc_mode_names[MAX_GC_MODE] = {
  45	"GC_NORMAL",
  46	"GC_IDLE_CB",
  47	"GC_IDLE_GREEDY",
  48	"GC_IDLE_AT",
  49	"GC_URGENT_HIGH",
  50	"GC_URGENT_LOW",
  51	"GC_URGENT_MID"
  52};
  53
  54struct f2fs_attr {
  55	struct attribute attr;
  56	ssize_t (*show)(struct f2fs_attr *a, struct f2fs_sb_info *sbi, char *buf);
  57	ssize_t (*store)(struct f2fs_attr *a, struct f2fs_sb_info *sbi,
  58			 const char *buf, size_t len);
  59	int struct_type;
  60	int offset;
  61	int id;
  62};
  63
  64static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
  65			     struct f2fs_sb_info *sbi, char *buf);
  66
  67static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type)
  68{
  69	if (struct_type == GC_THREAD)
  70		return (unsigned char *)sbi->gc_thread;
  71	else if (struct_type == SM_INFO)
  72		return (unsigned char *)SM_I(sbi);
  73	else if (struct_type == DCC_INFO)
  74		return (unsigned char *)SM_I(sbi)->dcc_info;
  75	else if (struct_type == NM_INFO)
  76		return (unsigned char *)NM_I(sbi);
  77	else if (struct_type == F2FS_SBI || struct_type == RESERVED_BLOCKS)
  78		return (unsigned char *)sbi;
  79#ifdef CONFIG_F2FS_FAULT_INJECTION
  80	else if (struct_type == FAULT_INFO_RATE ||
  81					struct_type == FAULT_INFO_TYPE)
  82		return (unsigned char *)&F2FS_OPTION(sbi).fault_info;
  83#endif
  84#ifdef CONFIG_F2FS_STAT_FS
  85	else if (struct_type == STAT_INFO)
  86		return (unsigned char *)F2FS_STAT(sbi);
  87#endif
  88	else if (struct_type == CPRC_INFO)
  89		return (unsigned char *)&sbi->cprc_info;
  90	else if (struct_type == ATGC_INFO)
  91		return (unsigned char *)&sbi->am;
  92	return NULL;
  93}
  94
  95static ssize_t dirty_segments_show(struct f2fs_attr *a,
  96		struct f2fs_sb_info *sbi, char *buf)
  97{
  98	return sysfs_emit(buf, "%llu\n",
  99			(unsigned long long)(dirty_segments(sbi)));
 100}
 101
 102static ssize_t free_segments_show(struct f2fs_attr *a,
 103		struct f2fs_sb_info *sbi, char *buf)
 104{
 105	return sysfs_emit(buf, "%llu\n",
 106			(unsigned long long)(free_segments(sbi)));
 107}
 108
 109static ssize_t ovp_segments_show(struct f2fs_attr *a,
 110		struct f2fs_sb_info *sbi, char *buf)
 111{
 112	return sysfs_emit(buf, "%llu\n",
 113			(unsigned long long)(overprovision_segments(sbi)));
 114}
 115
 116static ssize_t lifetime_write_kbytes_show(struct f2fs_attr *a,
 117		struct f2fs_sb_info *sbi, char *buf)
 118{
 119	return sysfs_emit(buf, "%llu\n",
 120			(unsigned long long)(sbi->kbytes_written +
 121			((f2fs_get_sectors_written(sbi) -
 122				sbi->sectors_written_start) >> 1)));
 123}
 124
 125static ssize_t sb_status_show(struct f2fs_attr *a,
 126		struct f2fs_sb_info *sbi, char *buf)
 127{
 128	return sysfs_emit(buf, "%lx\n", sbi->s_flag);
 129}
 130
 131static ssize_t cp_status_show(struct f2fs_attr *a,
 132		struct f2fs_sb_info *sbi, char *buf)
 133{
 134	return sysfs_emit(buf, "%x\n", le32_to_cpu(F2FS_CKPT(sbi)->ckpt_flags));
 135}
 136
 137static ssize_t pending_discard_show(struct f2fs_attr *a,
 138		struct f2fs_sb_info *sbi, char *buf)
 139{
 140	if (!SM_I(sbi)->dcc_info)
 141		return -EINVAL;
 142	return sysfs_emit(buf, "%llu\n", (unsigned long long)atomic_read(
 143				&SM_I(sbi)->dcc_info->discard_cmd_cnt));
 144}
 145
 146static ssize_t issued_discard_show(struct f2fs_attr *a,
 147		struct f2fs_sb_info *sbi, char *buf)
 148{
 149	if (!SM_I(sbi)->dcc_info)
 150		return -EINVAL;
 151	return sysfs_emit(buf, "%llu\n", (unsigned long long)atomic_read(
 152				&SM_I(sbi)->dcc_info->issued_discard));
 153}
 154
 155static ssize_t queued_discard_show(struct f2fs_attr *a,
 156		struct f2fs_sb_info *sbi, char *buf)
 157{
 158	if (!SM_I(sbi)->dcc_info)
 159		return -EINVAL;
 160	return sysfs_emit(buf, "%llu\n", (unsigned long long)atomic_read(
 161				&SM_I(sbi)->dcc_info->queued_discard));
 162}
 163
 164static ssize_t undiscard_blks_show(struct f2fs_attr *a,
 165		struct f2fs_sb_info *sbi, char *buf)
 166{
 167	if (!SM_I(sbi)->dcc_info)
 168		return -EINVAL;
 169	return sysfs_emit(buf, "%u\n",
 170				SM_I(sbi)->dcc_info->undiscard_blks);
 171}
 172
 
 
 
 
 
 
 173static ssize_t gc_mode_show(struct f2fs_attr *a,
 174		struct f2fs_sb_info *sbi, char *buf)
 175{
 176	return sysfs_emit(buf, "%s\n", gc_mode_names[sbi->gc_mode]);
 177}
 178
 179static ssize_t features_show(struct f2fs_attr *a,
 180		struct f2fs_sb_info *sbi, char *buf)
 181{
 182	int len = 0;
 183
 184	if (f2fs_sb_has_encrypt(sbi))
 185		len += scnprintf(buf, PAGE_SIZE - len, "%s",
 186						"encryption");
 187	if (f2fs_sb_has_blkzoned(sbi))
 188		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
 189				len ? ", " : "", "blkzoned");
 190	if (f2fs_sb_has_extra_attr(sbi))
 191		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
 192				len ? ", " : "", "extra_attr");
 193	if (f2fs_sb_has_project_quota(sbi))
 194		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
 195				len ? ", " : "", "projquota");
 196	if (f2fs_sb_has_inode_chksum(sbi))
 197		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
 198				len ? ", " : "", "inode_checksum");
 199	if (f2fs_sb_has_flexible_inline_xattr(sbi))
 200		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
 201				len ? ", " : "", "flexible_inline_xattr");
 202	if (f2fs_sb_has_quota_ino(sbi))
 203		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
 204				len ? ", " : "", "quota_ino");
 205	if (f2fs_sb_has_inode_crtime(sbi))
 206		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
 207				len ? ", " : "", "inode_crtime");
 208	if (f2fs_sb_has_lost_found(sbi))
 209		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
 210				len ? ", " : "", "lost_found");
 211	if (f2fs_sb_has_verity(sbi))
 212		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
 213				len ? ", " : "", "verity");
 214	if (f2fs_sb_has_sb_chksum(sbi))
 215		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
 216				len ? ", " : "", "sb_checksum");
 217	if (f2fs_sb_has_casefold(sbi))
 218		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
 219				len ? ", " : "", "casefold");
 220	if (f2fs_sb_has_readonly(sbi))
 221		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
 222				len ? ", " : "", "readonly");
 223	if (f2fs_sb_has_compression(sbi))
 224		len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
 225				len ? ", " : "", "compression");
 226	len += scnprintf(buf + len, PAGE_SIZE - len, "%s%s",
 227				len ? ", " : "", "pin_file");
 228	len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
 229	return len;
 230}
 231
 232static ssize_t current_reserved_blocks_show(struct f2fs_attr *a,
 233					struct f2fs_sb_info *sbi, char *buf)
 234{
 235	return sysfs_emit(buf, "%u\n", sbi->current_reserved_blocks);
 236}
 237
 238static ssize_t unusable_show(struct f2fs_attr *a,
 239		struct f2fs_sb_info *sbi, char *buf)
 240{
 241	block_t unusable;
 242
 243	if (test_opt(sbi, DISABLE_CHECKPOINT))
 244		unusable = sbi->unusable_block_count;
 245	else
 246		unusable = f2fs_get_unusable_blocks(sbi);
 247	return sysfs_emit(buf, "%llu\n", (unsigned long long)unusable);
 248}
 249
 250static ssize_t encoding_show(struct f2fs_attr *a,
 251		struct f2fs_sb_info *sbi, char *buf)
 252{
 253#if IS_ENABLED(CONFIG_UNICODE)
 254	struct super_block *sb = sbi->sb;
 255
 256	if (f2fs_sb_has_casefold(sbi))
 257		return sysfs_emit(buf, "UTF-8 (%d.%d.%d)\n",
 258			(sb->s_encoding->version >> 16) & 0xff,
 259			(sb->s_encoding->version >> 8) & 0xff,
 260			sb->s_encoding->version & 0xff);
 261#endif
 262	return sysfs_emit(buf, "(none)\n");
 263}
 264
 265static ssize_t mounted_time_sec_show(struct f2fs_attr *a,
 266		struct f2fs_sb_info *sbi, char *buf)
 267{
 268	return sysfs_emit(buf, "%llu\n", SIT_I(sbi)->mounted_time);
 269}
 270
 271#ifdef CONFIG_F2FS_STAT_FS
 272static ssize_t moved_blocks_foreground_show(struct f2fs_attr *a,
 273				struct f2fs_sb_info *sbi, char *buf)
 274{
 275	struct f2fs_stat_info *si = F2FS_STAT(sbi);
 276
 277	return sysfs_emit(buf, "%llu\n",
 278		(unsigned long long)(si->tot_blks -
 279			(si->bg_data_blks + si->bg_node_blks)));
 280}
 281
 282static ssize_t moved_blocks_background_show(struct f2fs_attr *a,
 283				struct f2fs_sb_info *sbi, char *buf)
 284{
 285	struct f2fs_stat_info *si = F2FS_STAT(sbi);
 286
 287	return sysfs_emit(buf, "%llu\n",
 288		(unsigned long long)(si->bg_data_blks + si->bg_node_blks));
 289}
 290
 291static ssize_t avg_vblocks_show(struct f2fs_attr *a,
 292		struct f2fs_sb_info *sbi, char *buf)
 293{
 294	struct f2fs_stat_info *si = F2FS_STAT(sbi);
 295
 296	si->dirty_count = dirty_segments(sbi);
 297	f2fs_update_sit_info(sbi);
 298	return sysfs_emit(buf, "%llu\n", (unsigned long long)(si->avg_vblocks));
 299}
 300#endif
 301
 302static ssize_t main_blkaddr_show(struct f2fs_attr *a,
 303				struct f2fs_sb_info *sbi, char *buf)
 304{
 305	return sysfs_emit(buf, "%llu\n",
 306			(unsigned long long)MAIN_BLKADDR(sbi));
 307}
 308
 309static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
 310			struct f2fs_sb_info *sbi, char *buf)
 311{
 312	unsigned char *ptr = NULL;
 313	unsigned int *ui;
 314
 315	ptr = __struct_ptr(sbi, a->struct_type);
 316	if (!ptr)
 317		return -EINVAL;
 318
 319	if (!strcmp(a->attr.name, "extension_list")) {
 320		__u8 (*extlist)[F2FS_EXTENSION_LEN] =
 321					sbi->raw_super->extension_list;
 322		int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
 323		int hot_count = sbi->raw_super->hot_ext_count;
 324		int len = 0, i;
 325
 326		len += scnprintf(buf + len, PAGE_SIZE - len,
 327						"cold file extension:\n");
 328		for (i = 0; i < cold_count; i++)
 329			len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
 330								extlist[i]);
 331
 332		len += scnprintf(buf + len, PAGE_SIZE - len,
 333						"hot file extension:\n");
 334		for (i = cold_count; i < cold_count + hot_count; i++)
 335			len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n",
 336								extlist[i]);
 337		return len;
 338	}
 339
 340	if (!strcmp(a->attr.name, "ckpt_thread_ioprio")) {
 341		struct ckpt_req_control *cprc = &sbi->cprc_info;
 342		int class = IOPRIO_PRIO_CLASS(cprc->ckpt_thread_ioprio);
 343		int data = IOPRIO_PRIO_DATA(cprc->ckpt_thread_ioprio);
 344
 345		if (class != IOPRIO_CLASS_RT && class != IOPRIO_CLASS_BE)
 346			return -EINVAL;
 347
 348		return sysfs_emit(buf, "%s,%d\n",
 349			class == IOPRIO_CLASS_RT ? "rt" : "be", data);
 350	}
 351
 352#ifdef CONFIG_F2FS_FS_COMPRESSION
 353	if (!strcmp(a->attr.name, "compr_written_block"))
 354		return sysfs_emit(buf, "%llu\n", sbi->compr_written_block);
 355
 356	if (!strcmp(a->attr.name, "compr_saved_block"))
 357		return sysfs_emit(buf, "%llu\n", sbi->compr_saved_block);
 358
 359	if (!strcmp(a->attr.name, "compr_new_inode"))
 360		return sysfs_emit(buf, "%u\n", sbi->compr_new_inode);
 361#endif
 362
 363	if (!strcmp(a->attr.name, "gc_segment_mode"))
 364		return sysfs_emit(buf, "%u\n", sbi->gc_segment_mode);
 365
 366	if (!strcmp(a->attr.name, "gc_reclaimed_segments")) {
 367		return sysfs_emit(buf, "%u\n",
 368			sbi->gc_reclaimed_segs[sbi->gc_segment_mode]);
 369	}
 370
 371	if (!strcmp(a->attr.name, "current_atomic_write")) {
 372		s64 current_write = atomic64_read(&sbi->current_atomic_write);
 373
 374		return sysfs_emit(buf, "%lld\n", current_write);
 375	}
 376
 377	if (!strcmp(a->attr.name, "peak_atomic_write"))
 378		return sysfs_emit(buf, "%lld\n", sbi->peak_atomic_write);
 379
 380	if (!strcmp(a->attr.name, "committed_atomic_block"))
 381		return sysfs_emit(buf, "%llu\n", sbi->committed_atomic_block);
 382
 383	if (!strcmp(a->attr.name, "revoked_atomic_block"))
 384		return sysfs_emit(buf, "%llu\n", sbi->revoked_atomic_block);
 385
 386#ifdef CONFIG_F2FS_STAT_FS
 387	if (!strcmp(a->attr.name, "cp_foreground_calls"))
 388		return sysfs_emit(buf, "%d\n",
 389				atomic_read(&sbi->cp_call_count[TOTAL_CALL]) -
 390				atomic_read(&sbi->cp_call_count[BACKGROUND]));
 391	if (!strcmp(a->attr.name, "cp_background_calls"))
 392		return sysfs_emit(buf, "%d\n",
 393				atomic_read(&sbi->cp_call_count[BACKGROUND]));
 394#endif
 395
 396	ui = (unsigned int *)(ptr + a->offset);
 397
 398	return sysfs_emit(buf, "%u\n", *ui);
 399}
 400
 401static ssize_t __sbi_store(struct f2fs_attr *a,
 402			struct f2fs_sb_info *sbi,
 403			const char *buf, size_t count)
 404{
 405	unsigned char *ptr;
 406	unsigned long t;
 407	unsigned int *ui;
 408	ssize_t ret;
 409
 410	ptr = __struct_ptr(sbi, a->struct_type);
 411	if (!ptr)
 412		return -EINVAL;
 413
 414	if (!strcmp(a->attr.name, "extension_list")) {
 415		const char *name = strim((char *)buf);
 416		bool set = true, hot;
 417
 418		if (!strncmp(name, "[h]", 3))
 419			hot = true;
 420		else if (!strncmp(name, "[c]", 3))
 421			hot = false;
 422		else
 423			return -EINVAL;
 424
 425		name += 3;
 426
 427		if (*name == '!') {
 428			name++;
 429			set = false;
 430		}
 431
 432		if (!strlen(name) || strlen(name) >= F2FS_EXTENSION_LEN)
 433			return -EINVAL;
 434
 435		f2fs_down_write(&sbi->sb_lock);
 436
 437		ret = f2fs_update_extension_list(sbi, name, hot, set);
 438		if (ret)
 439			goto out;
 440
 441		ret = f2fs_commit_super(sbi, false);
 442		if (ret)
 443			f2fs_update_extension_list(sbi, name, hot, !set);
 444out:
 445		f2fs_up_write(&sbi->sb_lock);
 446		return ret ? ret : count;
 447	}
 448
 449	if (!strcmp(a->attr.name, "ckpt_thread_ioprio")) {
 450		const char *name = strim((char *)buf);
 451		struct ckpt_req_control *cprc = &sbi->cprc_info;
 452		int class;
 453		long data;
 454		int ret;
 455
 456		if (!strncmp(name, "rt,", 3))
 457			class = IOPRIO_CLASS_RT;
 458		else if (!strncmp(name, "be,", 3))
 459			class = IOPRIO_CLASS_BE;
 460		else
 461			return -EINVAL;
 462
 463		name += 3;
 464		ret = kstrtol(name, 10, &data);
 465		if (ret)
 466			return ret;
 467		if (data >= IOPRIO_NR_LEVELS || data < 0)
 468			return -EINVAL;
 469
 470		cprc->ckpt_thread_ioprio = IOPRIO_PRIO_VALUE(class, data);
 471		if (test_opt(sbi, MERGE_CHECKPOINT)) {
 472			ret = set_task_ioprio(cprc->f2fs_issue_ckpt,
 473					cprc->ckpt_thread_ioprio);
 474			if (ret)
 475				return ret;
 476		}
 477
 478		return count;
 479	}
 480
 481	ui = (unsigned int *)(ptr + a->offset);
 482
 483	ret = kstrtoul(skip_spaces(buf), 0, &t);
 484	if (ret < 0)
 485		return ret;
 486#ifdef CONFIG_F2FS_FAULT_INJECTION
 487	if (a->struct_type == FAULT_INFO_TYPE && t >= BIT(FAULT_MAX))
 488		return -EINVAL;
 489	if (a->struct_type == FAULT_INFO_RATE && t >= UINT_MAX)
 490		return -EINVAL;
 
 
 
 
 
 
 491#endif
 492	if (a->struct_type == RESERVED_BLOCKS) {
 493		spin_lock(&sbi->stat_lock);
 494		if (t > (unsigned long)(sbi->user_block_count -
 495				F2FS_OPTION(sbi).root_reserved_blocks -
 496				SEGS_TO_BLKS(sbi,
 497				SM_I(sbi)->additional_reserved_segments))) {
 498			spin_unlock(&sbi->stat_lock);
 499			return -EINVAL;
 500		}
 501		*ui = t;
 502		sbi->current_reserved_blocks = min(sbi->reserved_blocks,
 503				sbi->user_block_count - valid_user_blocks(sbi));
 504		spin_unlock(&sbi->stat_lock);
 505		return count;
 506	}
 507
 508	if (!strcmp(a->attr.name, "discard_io_aware_gran")) {
 509		if (t > MAX_PLIST_NUM)
 510			return -EINVAL;
 511		if (!f2fs_block_unit_discard(sbi))
 512			return -EINVAL;
 513		if (t == *ui)
 514			return count;
 515		*ui = t;
 516		return count;
 517	}
 518
 519	if (!strcmp(a->attr.name, "discard_granularity")) {
 520		if (t == 0 || t > MAX_PLIST_NUM)
 521			return -EINVAL;
 522		if (!f2fs_block_unit_discard(sbi))
 523			return -EINVAL;
 524		if (t == *ui)
 525			return count;
 526		*ui = t;
 527		return count;
 528	}
 529
 530	if (!strcmp(a->attr.name, "max_ordered_discard")) {
 531		if (t == 0 || t > MAX_PLIST_NUM)
 532			return -EINVAL;
 533		if (!f2fs_block_unit_discard(sbi))
 534			return -EINVAL;
 535		*ui = t;
 536		return count;
 537	}
 538
 539	if (!strcmp(a->attr.name, "discard_urgent_util")) {
 540		if (t > 100)
 541			return -EINVAL;
 542		*ui = t;
 543		return count;
 544	}
 545
 546	if (!strcmp(a->attr.name, "discard_io_aware")) {
 547		if (t >= DPOLICY_IO_AWARE_MAX)
 548			return -EINVAL;
 549		*ui = t;
 550		return count;
 551	}
 552
 553	if (!strcmp(a->attr.name, "migration_granularity")) {
 554		if (t == 0 || t > SEGS_PER_SEC(sbi))
 555			return -EINVAL;
 556	}
 557
 
 
 
 
 
 558	if (!strcmp(a->attr.name, "gc_urgent")) {
 559		if (t == 0) {
 560			sbi->gc_mode = GC_NORMAL;
 561		} else if (t == 1) {
 562			sbi->gc_mode = GC_URGENT_HIGH;
 563			if (sbi->gc_thread) {
 564				sbi->gc_thread->gc_wake = true;
 565				wake_up_interruptible_all(
 566					&sbi->gc_thread->gc_wait_queue_head);
 567				wake_up_discard_thread(sbi, true);
 568			}
 569		} else if (t == 2) {
 570			sbi->gc_mode = GC_URGENT_LOW;
 571		} else if (t == 3) {
 572			sbi->gc_mode = GC_URGENT_MID;
 573			if (sbi->gc_thread) {
 574				sbi->gc_thread->gc_wake = true;
 575				wake_up_interruptible_all(
 576					&sbi->gc_thread->gc_wait_queue_head);
 577			}
 578		} else {
 579			return -EINVAL;
 580		}
 581		return count;
 582	}
 583	if (!strcmp(a->attr.name, "gc_idle")) {
 584		if (t == GC_IDLE_CB) {
 585			sbi->gc_mode = GC_IDLE_CB;
 586		} else if (t == GC_IDLE_GREEDY) {
 587			sbi->gc_mode = GC_IDLE_GREEDY;
 588		} else if (t == GC_IDLE_AT) {
 589			if (!sbi->am.atgc_enabled)
 590				return -EINVAL;
 591			sbi->gc_mode = GC_IDLE_AT;
 592		} else {
 593			sbi->gc_mode = GC_NORMAL;
 594		}
 595		return count;
 596	}
 597
 598	if (!strcmp(a->attr.name, "gc_remaining_trials")) {
 599		spin_lock(&sbi->gc_remaining_trials_lock);
 600		sbi->gc_remaining_trials = t;
 601		spin_unlock(&sbi->gc_remaining_trials_lock);
 602
 603		return count;
 604	}
 605
 606#ifdef CONFIG_F2FS_IOSTAT
 607	if (!strcmp(a->attr.name, "iostat_enable")) {
 608		sbi->iostat_enable = !!t;
 609		if (!sbi->iostat_enable)
 610			f2fs_reset_iostat(sbi);
 611		return count;
 612	}
 613
 614	if (!strcmp(a->attr.name, "iostat_period_ms")) {
 615		if (t < MIN_IOSTAT_PERIOD_MS || t > MAX_IOSTAT_PERIOD_MS)
 616			return -EINVAL;
 617		spin_lock_irq(&sbi->iostat_lock);
 618		sbi->iostat_period_ms = (unsigned int)t;
 619		spin_unlock_irq(&sbi->iostat_lock);
 620		return count;
 621	}
 622#endif
 623
 
 
 
 
 
 
 
 
 
 624#ifdef CONFIG_F2FS_FS_COMPRESSION
 625	if (!strcmp(a->attr.name, "compr_written_block") ||
 626		!strcmp(a->attr.name, "compr_saved_block")) {
 627		if (t != 0)
 628			return -EINVAL;
 629		sbi->compr_written_block = 0;
 630		sbi->compr_saved_block = 0;
 631		return count;
 632	}
 633
 634	if (!strcmp(a->attr.name, "compr_new_inode")) {
 635		if (t != 0)
 636			return -EINVAL;
 637		sbi->compr_new_inode = 0;
 638		return count;
 639	}
 640
 641	if (!strcmp(a->attr.name, "compress_percent")) {
 642		if (t == 0 || t > 100)
 643			return -EINVAL;
 644		*ui = t;
 645		return count;
 646	}
 647
 648	if (!strcmp(a->attr.name, "compress_watermark")) {
 649		if (t == 0 || t > 100)
 650			return -EINVAL;
 651		*ui = t;
 652		return count;
 653	}
 654#endif
 655
 656	if (!strcmp(a->attr.name, "atgc_candidate_ratio")) {
 657		if (t > 100)
 658			return -EINVAL;
 659		sbi->am.candidate_ratio = t;
 660		return count;
 661	}
 662
 663	if (!strcmp(a->attr.name, "atgc_age_weight")) {
 664		if (t > 100)
 665			return -EINVAL;
 666		sbi->am.age_weight = t;
 667		return count;
 668	}
 669
 670	if (!strcmp(a->attr.name, "gc_segment_mode")) {
 671		if (t < MAX_GC_MODE)
 672			sbi->gc_segment_mode = t;
 673		else
 674			return -EINVAL;
 675		return count;
 676	}
 677
 
 
 
 
 
 
 
 678	if (!strcmp(a->attr.name, "gc_reclaimed_segments")) {
 679		if (t != 0)
 680			return -EINVAL;
 681		sbi->gc_reclaimed_segs[sbi->gc_segment_mode] = 0;
 682		return count;
 683	}
 684
 685	if (!strcmp(a->attr.name, "seq_file_ra_mul")) {
 686		if (t >= MIN_RA_MUL && t <= MAX_RA_MUL)
 687			sbi->seq_file_ra_mul = t;
 688		else
 689			return -EINVAL;
 690		return count;
 691	}
 692
 693	if (!strcmp(a->attr.name, "max_fragment_chunk")) {
 694		if (t >= MIN_FRAGMENT_SIZE && t <= MAX_FRAGMENT_SIZE)
 695			sbi->max_fragment_chunk = t;
 696		else
 697			return -EINVAL;
 698		return count;
 699	}
 700
 701	if (!strcmp(a->attr.name, "max_fragment_hole")) {
 702		if (t >= MIN_FRAGMENT_SIZE && t <= MAX_FRAGMENT_SIZE)
 703			sbi->max_fragment_hole = t;
 704		else
 705			return -EINVAL;
 706		return count;
 707	}
 708
 709	if (!strcmp(a->attr.name, "peak_atomic_write")) {
 710		if (t != 0)
 711			return -EINVAL;
 712		sbi->peak_atomic_write = 0;
 713		return count;
 714	}
 715
 716	if (!strcmp(a->attr.name, "committed_atomic_block")) {
 717		if (t != 0)
 718			return -EINVAL;
 719		sbi->committed_atomic_block = 0;
 720		return count;
 721	}
 722
 723	if (!strcmp(a->attr.name, "revoked_atomic_block")) {
 724		if (t != 0)
 725			return -EINVAL;
 726		sbi->revoked_atomic_block = 0;
 727		return count;
 728	}
 729
 730	if (!strcmp(a->attr.name, "readdir_ra")) {
 731		sbi->readdir_ra = !!t;
 732		return count;
 733	}
 734
 735	if (!strcmp(a->attr.name, "hot_data_age_threshold")) {
 736		if (t == 0 || t >= sbi->warm_data_age_threshold)
 737			return -EINVAL;
 738		if (t == *ui)
 739			return count;
 740		*ui = (unsigned int)t;
 741		return count;
 742	}
 743
 744	if (!strcmp(a->attr.name, "warm_data_age_threshold")) {
 745		if (t <= sbi->hot_data_age_threshold)
 746			return -EINVAL;
 747		if (t == *ui)
 748			return count;
 749		*ui = (unsigned int)t;
 750		return count;
 751	}
 752
 753	if (!strcmp(a->attr.name, "last_age_weight")) {
 754		if (t > 100)
 755			return -EINVAL;
 756		if (t == *ui)
 757			return count;
 758		*ui = (unsigned int)t;
 759		return count;
 760	}
 761
 
 
 
 
 
 
 
 762	if (!strcmp(a->attr.name, "ipu_policy")) {
 763		if (t >= BIT(F2FS_IPU_MAX))
 764			return -EINVAL;
 765		if (t && f2fs_lfs_mode(sbi))
 
 766			return -EINVAL;
 767		SM_I(sbi)->ipu_policy = (unsigned int)t;
 768		return count;
 769	}
 770
 771	if (!strcmp(a->attr.name, "dir_level")) {
 772		if (t > MAX_DIR_HASH_DEPTH)
 773			return -EINVAL;
 774		sbi->dir_level = t;
 775		return count;
 776	}
 777
 778	*ui = (unsigned int)t;
 779
 780	return count;
 781}
 782
 783static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
 784			struct f2fs_sb_info *sbi,
 785			const char *buf, size_t count)
 786{
 787	ssize_t ret;
 788	bool gc_entry = (!strcmp(a->attr.name, "gc_urgent") ||
 789					a->struct_type == GC_THREAD);
 790
 791	if (gc_entry) {
 792		if (!down_read_trylock(&sbi->sb->s_umount))
 793			return -EAGAIN;
 794	}
 795	ret = __sbi_store(a, sbi, buf, count);
 796	if (gc_entry)
 797		up_read(&sbi->sb->s_umount);
 798
 799	return ret;
 800}
 801
 802static ssize_t f2fs_attr_show(struct kobject *kobj,
 803				struct attribute *attr, char *buf)
 804{
 805	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
 806								s_kobj);
 807	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
 808
 809	return a->show ? a->show(a, sbi, buf) : 0;
 810}
 811
 812static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
 813						const char *buf, size_t len)
 814{
 815	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
 816									s_kobj);
 817	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
 818
 819	return a->store ? a->store(a, sbi, buf, len) : 0;
 820}
 821
 822static void f2fs_sb_release(struct kobject *kobj)
 823{
 824	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
 825								s_kobj);
 826	complete(&sbi->s_kobj_unregister);
 827}
 828
 829/*
 830 * Note that there are three feature list entries:
 831 * 1) /sys/fs/f2fs/features
 832 *   : shows runtime features supported by in-kernel f2fs along with Kconfig.
 833 *     - ref. F2FS_FEATURE_RO_ATTR()
 834 *
 835 * 2) /sys/fs/f2fs/$s_id/features <deprecated>
 836 *   : shows on-disk features enabled by mkfs.f2fs, used for old kernels. This
 837 *     won't add new feature anymore, and thus, users should check entries in 3)
 838 *     instead of this 2).
 839 *
 840 * 3) /sys/fs/f2fs/$s_id/feature_list
 841 *   : shows on-disk features enabled by mkfs.f2fs per instance, which follows
 842 *     sysfs entry rule where each entry should expose single value.
 843 *     This list covers old feature list provided by 2) and beyond. Therefore,
 844 *     please add new on-disk feature in this list only.
 845 *     - ref. F2FS_SB_FEATURE_RO_ATTR()
 846 */
 847static ssize_t f2fs_feature_show(struct f2fs_attr *a,
 848		struct f2fs_sb_info *sbi, char *buf)
 849{
 850	return sysfs_emit(buf, "supported\n");
 851}
 852
 853#define F2FS_FEATURE_RO_ATTR(_name)				\
 854static struct f2fs_attr f2fs_attr_##_name = {			\
 855	.attr = {.name = __stringify(_name), .mode = 0444 },	\
 856	.show	= f2fs_feature_show,				\
 857}
 858
 859static ssize_t f2fs_sb_feature_show(struct f2fs_attr *a,
 860		struct f2fs_sb_info *sbi, char *buf)
 861{
 862	if (F2FS_HAS_FEATURE(sbi, a->id))
 863		return sysfs_emit(buf, "supported\n");
 864	return sysfs_emit(buf, "unsupported\n");
 865}
 866
 867#define F2FS_SB_FEATURE_RO_ATTR(_name, _feat)			\
 868static struct f2fs_attr f2fs_attr_sb_##_name = {		\
 869	.attr = {.name = __stringify(_name), .mode = 0444 },	\
 870	.show	= f2fs_sb_feature_show,				\
 871	.id	= F2FS_FEATURE_##_feat,				\
 872}
 873
 874#define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
 875static struct f2fs_attr f2fs_attr_##_name = {			\
 876	.attr = {.name = __stringify(_name), .mode = _mode },	\
 877	.show	= _show,					\
 878	.store	= _store,					\
 879	.struct_type = _struct_type,				\
 880	.offset = _offset					\
 881}
 882
 883#define F2FS_RO_ATTR(struct_type, struct_name, name, elname)	\
 884	F2FS_ATTR_OFFSET(struct_type, name, 0444,		\
 885		f2fs_sbi_show, NULL,				\
 886		offsetof(struct struct_name, elname))
 887
 888#define F2FS_RW_ATTR(struct_type, struct_name, name, elname)	\
 889	F2FS_ATTR_OFFSET(struct_type, name, 0644,		\
 890		f2fs_sbi_show, f2fs_sbi_store,			\
 891		offsetof(struct struct_name, elname))
 892
 893#define F2FS_GENERAL_RO_ATTR(name) \
 894static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
 895
 896#ifdef CONFIG_F2FS_STAT_FS
 897#define STAT_INFO_RO_ATTR(name, elname)				\
 898	F2FS_RO_ATTR(STAT_INFO, f2fs_stat_info, name, elname)
 899#endif
 900
 901#define GC_THREAD_RW_ATTR(name, elname)				\
 902	F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, name, elname)
 903
 904#define SM_INFO_RW_ATTR(name, elname)				\
 905	F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, name, elname)
 906
 907#define SM_INFO_GENERAL_RW_ATTR(elname)				\
 908	SM_INFO_RW_ATTR(elname, elname)
 909
 910#define DCC_INFO_RW_ATTR(name, elname)				\
 911	F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, name, elname)
 912
 913#define DCC_INFO_GENERAL_RW_ATTR(elname)			\
 914	DCC_INFO_RW_ATTR(elname, elname)
 915
 916#define NM_INFO_RW_ATTR(name, elname)				\
 917	F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, name, elname)
 918
 919#define NM_INFO_GENERAL_RW_ATTR(elname)				\
 920	NM_INFO_RW_ATTR(elname, elname)
 921
 922#define F2FS_SBI_RW_ATTR(name, elname)				\
 923	F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, name, elname)
 924
 925#define F2FS_SBI_GENERAL_RW_ATTR(elname)			\
 926	F2FS_SBI_RW_ATTR(elname, elname)
 927
 928#define F2FS_SBI_GENERAL_RO_ATTR(elname)			\
 929	F2FS_RO_ATTR(F2FS_SBI, f2fs_sb_info, elname, elname)
 930
 931#ifdef CONFIG_F2FS_FAULT_INJECTION
 932#define FAULT_INFO_GENERAL_RW_ATTR(type, elname)		\
 933	F2FS_RW_ATTR(type, f2fs_fault_info, elname, elname)
 934#endif
 935
 936#define RESERVED_BLOCKS_GENERAL_RW_ATTR(elname)			\
 937	F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, elname, elname)
 938
 939#define CPRC_INFO_GENERAL_RW_ATTR(elname)			\
 940	F2FS_RW_ATTR(CPRC_INFO, ckpt_req_control, elname, elname)
 941
 942#define ATGC_INFO_RW_ATTR(name, elname)				\
 943	F2FS_RW_ATTR(ATGC_INFO, atgc_management, name, elname)
 944
 945/* GC_THREAD ATTR */
 946GC_THREAD_RW_ATTR(gc_urgent_sleep_time, urgent_sleep_time);
 947GC_THREAD_RW_ATTR(gc_min_sleep_time, min_sleep_time);
 948GC_THREAD_RW_ATTR(gc_max_sleep_time, max_sleep_time);
 949GC_THREAD_RW_ATTR(gc_no_gc_sleep_time, no_gc_sleep_time);
 
 
 
 950
 951/* SM_INFO ATTR */
 952SM_INFO_RW_ATTR(reclaim_segments, rec_prefree_segments);
 953SM_INFO_GENERAL_RW_ATTR(ipu_policy);
 954SM_INFO_GENERAL_RW_ATTR(min_ipu_util);
 955SM_INFO_GENERAL_RW_ATTR(min_fsync_blocks);
 956SM_INFO_GENERAL_RW_ATTR(min_seq_blocks);
 957SM_INFO_GENERAL_RW_ATTR(min_hot_blocks);
 958SM_INFO_GENERAL_RW_ATTR(min_ssr_sections);
 
 959
 960/* DCC_INFO ATTR */
 961DCC_INFO_RW_ATTR(max_small_discards, max_discards);
 962DCC_INFO_GENERAL_RW_ATTR(max_discard_request);
 963DCC_INFO_GENERAL_RW_ATTR(min_discard_issue_time);
 964DCC_INFO_GENERAL_RW_ATTR(mid_discard_issue_time);
 965DCC_INFO_GENERAL_RW_ATTR(max_discard_issue_time);
 966DCC_INFO_GENERAL_RW_ATTR(discard_io_aware_gran);
 967DCC_INFO_GENERAL_RW_ATTR(discard_urgent_util);
 968DCC_INFO_GENERAL_RW_ATTR(discard_granularity);
 969DCC_INFO_GENERAL_RW_ATTR(max_ordered_discard);
 970DCC_INFO_GENERAL_RW_ATTR(discard_io_aware);
 971
 972/* NM_INFO ATTR */
 973NM_INFO_RW_ATTR(max_roll_forward_node_blocks, max_rf_node_blocks);
 974NM_INFO_GENERAL_RW_ATTR(ram_thresh);
 975NM_INFO_GENERAL_RW_ATTR(ra_nid_pages);
 976NM_INFO_GENERAL_RW_ATTR(dirty_nats_ratio);
 977
 978/* F2FS_SBI ATTR */
 979F2FS_RW_ATTR(F2FS_SBI, f2fs_super_block, extension_list, extension_list);
 980F2FS_SBI_RW_ATTR(gc_idle, gc_mode);
 981F2FS_SBI_RW_ATTR(gc_urgent, gc_mode);
 982F2FS_SBI_RW_ATTR(cp_interval, interval_time[CP_TIME]);
 983F2FS_SBI_RW_ATTR(idle_interval, interval_time[REQ_TIME]);
 984F2FS_SBI_RW_ATTR(discard_idle_interval, interval_time[DISCARD_TIME]);
 985F2FS_SBI_RW_ATTR(gc_idle_interval, interval_time[GC_TIME]);
 986F2FS_SBI_RW_ATTR(umount_discard_timeout, interval_time[UMOUNT_DISCARD_TIMEOUT]);
 987F2FS_SBI_RW_ATTR(gc_pin_file_thresh, gc_pin_file_threshold);
 988F2FS_SBI_RW_ATTR(gc_reclaimed_segments, gc_reclaimed_segs);
 989F2FS_SBI_GENERAL_RW_ATTR(max_victim_search);
 990F2FS_SBI_GENERAL_RW_ATTR(migration_granularity);
 
 991F2FS_SBI_GENERAL_RW_ATTR(dir_level);
 992#ifdef CONFIG_F2FS_IOSTAT
 993F2FS_SBI_GENERAL_RW_ATTR(iostat_enable);
 994F2FS_SBI_GENERAL_RW_ATTR(iostat_period_ms);
 995#endif
 996F2FS_SBI_GENERAL_RW_ATTR(readdir_ra);
 997F2FS_SBI_GENERAL_RW_ATTR(max_io_bytes);
 998F2FS_SBI_GENERAL_RW_ATTR(data_io_flag);
 999F2FS_SBI_GENERAL_RW_ATTR(node_io_flag);
1000F2FS_SBI_GENERAL_RW_ATTR(gc_remaining_trials);
1001F2FS_SBI_GENERAL_RW_ATTR(seq_file_ra_mul);
1002F2FS_SBI_GENERAL_RW_ATTR(gc_segment_mode);
1003F2FS_SBI_GENERAL_RW_ATTR(max_fragment_chunk);
1004F2FS_SBI_GENERAL_RW_ATTR(max_fragment_hole);
1005#ifdef CONFIG_F2FS_FS_COMPRESSION
1006F2FS_SBI_GENERAL_RW_ATTR(compr_written_block);
1007F2FS_SBI_GENERAL_RW_ATTR(compr_saved_block);
1008F2FS_SBI_GENERAL_RW_ATTR(compr_new_inode);
1009F2FS_SBI_GENERAL_RW_ATTR(compress_percent);
1010F2FS_SBI_GENERAL_RW_ATTR(compress_watermark);
1011#endif
1012/* atomic write */
1013F2FS_SBI_GENERAL_RO_ATTR(current_atomic_write);
1014F2FS_SBI_GENERAL_RW_ATTR(peak_atomic_write);
1015F2FS_SBI_GENERAL_RW_ATTR(committed_atomic_block);
1016F2FS_SBI_GENERAL_RW_ATTR(revoked_atomic_block);
1017/* block age extent cache */
1018F2FS_SBI_GENERAL_RW_ATTR(hot_data_age_threshold);
1019F2FS_SBI_GENERAL_RW_ATTR(warm_data_age_threshold);
1020F2FS_SBI_GENERAL_RW_ATTR(last_age_weight);
 
 
1021#ifdef CONFIG_BLK_DEV_ZONED
1022F2FS_SBI_GENERAL_RO_ATTR(unusable_blocks_per_sec);
 
1023#endif
1024
1025/* STAT_INFO ATTR */
1026#ifdef CONFIG_F2FS_STAT_FS
1027STAT_INFO_RO_ATTR(cp_foreground_calls, cp_call_count[FOREGROUND]);
1028STAT_INFO_RO_ATTR(cp_background_calls, cp_call_count[BACKGROUND]);
1029STAT_INFO_RO_ATTR(gc_foreground_calls, gc_call_count[FOREGROUND]);
1030STAT_INFO_RO_ATTR(gc_background_calls, gc_call_count[BACKGROUND]);
1031#endif
1032
1033/* FAULT_INFO ATTR */
1034#ifdef CONFIG_F2FS_FAULT_INJECTION
1035FAULT_INFO_GENERAL_RW_ATTR(FAULT_INFO_RATE, inject_rate);
1036FAULT_INFO_GENERAL_RW_ATTR(FAULT_INFO_TYPE, inject_type);
1037#endif
1038
1039/* RESERVED_BLOCKS ATTR */
1040RESERVED_BLOCKS_GENERAL_RW_ATTR(reserved_blocks);
1041
1042/* CPRC_INFO ATTR */
1043CPRC_INFO_GENERAL_RW_ATTR(ckpt_thread_ioprio);
1044
1045/* ATGC_INFO ATTR */
1046ATGC_INFO_RW_ATTR(atgc_candidate_ratio, candidate_ratio);
1047ATGC_INFO_RW_ATTR(atgc_candidate_count, max_candidate_count);
1048ATGC_INFO_RW_ATTR(atgc_age_weight, age_weight);
1049ATGC_INFO_RW_ATTR(atgc_age_threshold, age_threshold);
1050
1051F2FS_GENERAL_RO_ATTR(dirty_segments);
1052F2FS_GENERAL_RO_ATTR(free_segments);
1053F2FS_GENERAL_RO_ATTR(ovp_segments);
1054F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
1055F2FS_GENERAL_RO_ATTR(features);
1056F2FS_GENERAL_RO_ATTR(current_reserved_blocks);
1057F2FS_GENERAL_RO_ATTR(unusable);
1058F2FS_GENERAL_RO_ATTR(encoding);
1059F2FS_GENERAL_RO_ATTR(mounted_time_sec);
1060F2FS_GENERAL_RO_ATTR(main_blkaddr);
1061F2FS_GENERAL_RO_ATTR(pending_discard);
 
1062F2FS_GENERAL_RO_ATTR(gc_mode);
1063#ifdef CONFIG_F2FS_STAT_FS
1064F2FS_GENERAL_RO_ATTR(moved_blocks_background);
1065F2FS_GENERAL_RO_ATTR(moved_blocks_foreground);
1066F2FS_GENERAL_RO_ATTR(avg_vblocks);
1067#endif
1068
1069#ifdef CONFIG_FS_ENCRYPTION
1070F2FS_FEATURE_RO_ATTR(encryption);
1071F2FS_FEATURE_RO_ATTR(test_dummy_encryption_v2);
1072#if IS_ENABLED(CONFIG_UNICODE)
1073F2FS_FEATURE_RO_ATTR(encrypted_casefold);
1074#endif
1075#endif /* CONFIG_FS_ENCRYPTION */
1076#ifdef CONFIG_BLK_DEV_ZONED
1077F2FS_FEATURE_RO_ATTR(block_zoned);
1078#endif
1079F2FS_FEATURE_RO_ATTR(atomic_write);
1080F2FS_FEATURE_RO_ATTR(extra_attr);
1081F2FS_FEATURE_RO_ATTR(project_quota);
1082F2FS_FEATURE_RO_ATTR(inode_checksum);
1083F2FS_FEATURE_RO_ATTR(flexible_inline_xattr);
1084F2FS_FEATURE_RO_ATTR(quota_ino);
1085F2FS_FEATURE_RO_ATTR(inode_crtime);
1086F2FS_FEATURE_RO_ATTR(lost_found);
1087#ifdef CONFIG_FS_VERITY
1088F2FS_FEATURE_RO_ATTR(verity);
1089#endif
1090F2FS_FEATURE_RO_ATTR(sb_checksum);
1091#if IS_ENABLED(CONFIG_UNICODE)
1092F2FS_FEATURE_RO_ATTR(casefold);
1093#endif
1094F2FS_FEATURE_RO_ATTR(readonly);
1095#ifdef CONFIG_F2FS_FS_COMPRESSION
1096F2FS_FEATURE_RO_ATTR(compression);
1097#endif
1098F2FS_FEATURE_RO_ATTR(pin_file);
1099
1100#define ATTR_LIST(name) (&f2fs_attr_##name.attr)
1101static struct attribute *f2fs_attrs[] = {
1102	ATTR_LIST(gc_urgent_sleep_time),
1103	ATTR_LIST(gc_min_sleep_time),
1104	ATTR_LIST(gc_max_sleep_time),
1105	ATTR_LIST(gc_no_gc_sleep_time),
 
 
 
1106	ATTR_LIST(gc_idle),
1107	ATTR_LIST(gc_urgent),
1108	ATTR_LIST(reclaim_segments),
1109	ATTR_LIST(main_blkaddr),
1110	ATTR_LIST(max_small_discards),
1111	ATTR_LIST(max_discard_request),
1112	ATTR_LIST(min_discard_issue_time),
1113	ATTR_LIST(mid_discard_issue_time),
1114	ATTR_LIST(max_discard_issue_time),
1115	ATTR_LIST(discard_io_aware_gran),
1116	ATTR_LIST(discard_urgent_util),
1117	ATTR_LIST(discard_granularity),
1118	ATTR_LIST(max_ordered_discard),
1119	ATTR_LIST(discard_io_aware),
1120	ATTR_LIST(pending_discard),
1121	ATTR_LIST(gc_mode),
1122	ATTR_LIST(ipu_policy),
1123	ATTR_LIST(min_ipu_util),
1124	ATTR_LIST(min_fsync_blocks),
1125	ATTR_LIST(min_seq_blocks),
1126	ATTR_LIST(min_hot_blocks),
1127	ATTR_LIST(min_ssr_sections),
 
1128	ATTR_LIST(max_victim_search),
1129	ATTR_LIST(migration_granularity),
 
1130	ATTR_LIST(dir_level),
1131	ATTR_LIST(ram_thresh),
1132	ATTR_LIST(ra_nid_pages),
1133	ATTR_LIST(dirty_nats_ratio),
1134	ATTR_LIST(max_roll_forward_node_blocks),
1135	ATTR_LIST(cp_interval),
1136	ATTR_LIST(idle_interval),
1137	ATTR_LIST(discard_idle_interval),
1138	ATTR_LIST(gc_idle_interval),
1139	ATTR_LIST(umount_discard_timeout),
1140#ifdef CONFIG_F2FS_IOSTAT
1141	ATTR_LIST(iostat_enable),
1142	ATTR_LIST(iostat_period_ms),
1143#endif
1144	ATTR_LIST(readdir_ra),
1145	ATTR_LIST(max_io_bytes),
1146	ATTR_LIST(gc_pin_file_thresh),
1147	ATTR_LIST(extension_list),
1148#ifdef CONFIG_F2FS_FAULT_INJECTION
1149	ATTR_LIST(inject_rate),
1150	ATTR_LIST(inject_type),
1151#endif
1152	ATTR_LIST(data_io_flag),
1153	ATTR_LIST(node_io_flag),
1154	ATTR_LIST(gc_remaining_trials),
1155	ATTR_LIST(ckpt_thread_ioprio),
1156	ATTR_LIST(dirty_segments),
1157	ATTR_LIST(free_segments),
1158	ATTR_LIST(ovp_segments),
1159	ATTR_LIST(unusable),
1160	ATTR_LIST(lifetime_write_kbytes),
1161	ATTR_LIST(features),
1162	ATTR_LIST(reserved_blocks),
1163	ATTR_LIST(current_reserved_blocks),
1164	ATTR_LIST(encoding),
1165	ATTR_LIST(mounted_time_sec),
1166#ifdef CONFIG_F2FS_STAT_FS
1167	ATTR_LIST(cp_foreground_calls),
1168	ATTR_LIST(cp_background_calls),
1169	ATTR_LIST(gc_foreground_calls),
1170	ATTR_LIST(gc_background_calls),
1171	ATTR_LIST(moved_blocks_foreground),
1172	ATTR_LIST(moved_blocks_background),
1173	ATTR_LIST(avg_vblocks),
1174#endif
1175#ifdef CONFIG_BLK_DEV_ZONED
1176	ATTR_LIST(unusable_blocks_per_sec),
 
1177#endif
1178#ifdef CONFIG_F2FS_FS_COMPRESSION
1179	ATTR_LIST(compr_written_block),
1180	ATTR_LIST(compr_saved_block),
1181	ATTR_LIST(compr_new_inode),
1182	ATTR_LIST(compress_percent),
1183	ATTR_LIST(compress_watermark),
1184#endif
1185	/* For ATGC */
1186	ATTR_LIST(atgc_candidate_ratio),
1187	ATTR_LIST(atgc_candidate_count),
1188	ATTR_LIST(atgc_age_weight),
1189	ATTR_LIST(atgc_age_threshold),
 
1190	ATTR_LIST(seq_file_ra_mul),
1191	ATTR_LIST(gc_segment_mode),
1192	ATTR_LIST(gc_reclaimed_segments),
1193	ATTR_LIST(max_fragment_chunk),
1194	ATTR_LIST(max_fragment_hole),
1195	ATTR_LIST(current_atomic_write),
1196	ATTR_LIST(peak_atomic_write),
1197	ATTR_LIST(committed_atomic_block),
1198	ATTR_LIST(revoked_atomic_block),
1199	ATTR_LIST(hot_data_age_threshold),
1200	ATTR_LIST(warm_data_age_threshold),
1201	ATTR_LIST(last_age_weight),
 
1202	NULL,
1203};
1204ATTRIBUTE_GROUPS(f2fs);
1205
1206static struct attribute *f2fs_feat_attrs[] = {
1207#ifdef CONFIG_FS_ENCRYPTION
1208	ATTR_LIST(encryption),
1209	ATTR_LIST(test_dummy_encryption_v2),
1210#if IS_ENABLED(CONFIG_UNICODE)
1211	ATTR_LIST(encrypted_casefold),
1212#endif
1213#endif /* CONFIG_FS_ENCRYPTION */
1214#ifdef CONFIG_BLK_DEV_ZONED
1215	ATTR_LIST(block_zoned),
1216#endif
1217	ATTR_LIST(atomic_write),
1218	ATTR_LIST(extra_attr),
1219	ATTR_LIST(project_quota),
1220	ATTR_LIST(inode_checksum),
1221	ATTR_LIST(flexible_inline_xattr),
1222	ATTR_LIST(quota_ino),
1223	ATTR_LIST(inode_crtime),
1224	ATTR_LIST(lost_found),
1225#ifdef CONFIG_FS_VERITY
1226	ATTR_LIST(verity),
1227#endif
1228	ATTR_LIST(sb_checksum),
1229#if IS_ENABLED(CONFIG_UNICODE)
1230	ATTR_LIST(casefold),
1231#endif
1232	ATTR_LIST(readonly),
1233#ifdef CONFIG_F2FS_FS_COMPRESSION
1234	ATTR_LIST(compression),
1235#endif
1236	ATTR_LIST(pin_file),
1237	NULL,
1238};
1239ATTRIBUTE_GROUPS(f2fs_feat);
1240
1241F2FS_GENERAL_RO_ATTR(sb_status);
1242F2FS_GENERAL_RO_ATTR(cp_status);
1243F2FS_GENERAL_RO_ATTR(issued_discard);
1244F2FS_GENERAL_RO_ATTR(queued_discard);
1245F2FS_GENERAL_RO_ATTR(undiscard_blks);
1246
1247static struct attribute *f2fs_stat_attrs[] = {
1248	ATTR_LIST(sb_status),
1249	ATTR_LIST(cp_status),
1250	ATTR_LIST(issued_discard),
1251	ATTR_LIST(queued_discard),
1252	ATTR_LIST(undiscard_blks),
1253	NULL,
1254};
1255ATTRIBUTE_GROUPS(f2fs_stat);
1256
1257F2FS_SB_FEATURE_RO_ATTR(encryption, ENCRYPT);
1258F2FS_SB_FEATURE_RO_ATTR(block_zoned, BLKZONED);
1259F2FS_SB_FEATURE_RO_ATTR(extra_attr, EXTRA_ATTR);
1260F2FS_SB_FEATURE_RO_ATTR(project_quota, PRJQUOTA);
1261F2FS_SB_FEATURE_RO_ATTR(inode_checksum, INODE_CHKSUM);
1262F2FS_SB_FEATURE_RO_ATTR(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
1263F2FS_SB_FEATURE_RO_ATTR(quota_ino, QUOTA_INO);
1264F2FS_SB_FEATURE_RO_ATTR(inode_crtime, INODE_CRTIME);
1265F2FS_SB_FEATURE_RO_ATTR(lost_found, LOST_FOUND);
1266F2FS_SB_FEATURE_RO_ATTR(verity, VERITY);
1267F2FS_SB_FEATURE_RO_ATTR(sb_checksum, SB_CHKSUM);
1268F2FS_SB_FEATURE_RO_ATTR(casefold, CASEFOLD);
1269F2FS_SB_FEATURE_RO_ATTR(compression, COMPRESSION);
1270F2FS_SB_FEATURE_RO_ATTR(readonly, RO);
 
1271
1272static struct attribute *f2fs_sb_feat_attrs[] = {
1273	ATTR_LIST(sb_encryption),
1274	ATTR_LIST(sb_block_zoned),
1275	ATTR_LIST(sb_extra_attr),
1276	ATTR_LIST(sb_project_quota),
1277	ATTR_LIST(sb_inode_checksum),
1278	ATTR_LIST(sb_flexible_inline_xattr),
1279	ATTR_LIST(sb_quota_ino),
1280	ATTR_LIST(sb_inode_crtime),
1281	ATTR_LIST(sb_lost_found),
1282	ATTR_LIST(sb_verity),
1283	ATTR_LIST(sb_sb_checksum),
1284	ATTR_LIST(sb_casefold),
1285	ATTR_LIST(sb_compression),
1286	ATTR_LIST(sb_readonly),
 
1287	NULL,
1288};
1289ATTRIBUTE_GROUPS(f2fs_sb_feat);
1290
1291static const struct sysfs_ops f2fs_attr_ops = {
1292	.show	= f2fs_attr_show,
1293	.store	= f2fs_attr_store,
1294};
1295
1296static const struct kobj_type f2fs_sb_ktype = {
1297	.default_groups = f2fs_groups,
1298	.sysfs_ops	= &f2fs_attr_ops,
1299	.release	= f2fs_sb_release,
1300};
1301
1302static const struct kobj_type f2fs_ktype = {
1303	.sysfs_ops	= &f2fs_attr_ops,
1304};
1305
1306static struct kset f2fs_kset = {
1307	.kobj	= {.ktype = &f2fs_ktype},
1308};
1309
1310static const struct kobj_type f2fs_feat_ktype = {
1311	.default_groups = f2fs_feat_groups,
1312	.sysfs_ops	= &f2fs_attr_ops,
1313};
1314
1315static struct kobject f2fs_feat = {
1316	.kset	= &f2fs_kset,
1317};
1318
1319static ssize_t f2fs_stat_attr_show(struct kobject *kobj,
1320				struct attribute *attr, char *buf)
1321{
1322	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1323								s_stat_kobj);
1324	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1325
1326	return a->show ? a->show(a, sbi, buf) : 0;
1327}
1328
1329static ssize_t f2fs_stat_attr_store(struct kobject *kobj, struct attribute *attr,
1330						const char *buf, size_t len)
1331{
1332	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1333								s_stat_kobj);
1334	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1335
1336	return a->store ? a->store(a, sbi, buf, len) : 0;
1337}
1338
1339static void f2fs_stat_kobj_release(struct kobject *kobj)
1340{
1341	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1342								s_stat_kobj);
1343	complete(&sbi->s_stat_kobj_unregister);
1344}
1345
1346static const struct sysfs_ops f2fs_stat_attr_ops = {
1347	.show	= f2fs_stat_attr_show,
1348	.store	= f2fs_stat_attr_store,
1349};
1350
1351static const struct kobj_type f2fs_stat_ktype = {
1352	.default_groups = f2fs_stat_groups,
1353	.sysfs_ops	= &f2fs_stat_attr_ops,
1354	.release	= f2fs_stat_kobj_release,
1355};
1356
1357static ssize_t f2fs_sb_feat_attr_show(struct kobject *kobj,
1358				struct attribute *attr, char *buf)
1359{
1360	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1361							s_feature_list_kobj);
1362	struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
1363
1364	return a->show ? a->show(a, sbi, buf) : 0;
1365}
1366
1367static void f2fs_feature_list_kobj_release(struct kobject *kobj)
1368{
1369	struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
1370							s_feature_list_kobj);
1371	complete(&sbi->s_feature_list_kobj_unregister);
1372}
1373
1374static const struct sysfs_ops f2fs_feature_list_attr_ops = {
1375	.show	= f2fs_sb_feat_attr_show,
1376};
1377
1378static const struct kobj_type f2fs_feature_list_ktype = {
1379	.default_groups = f2fs_sb_feat_groups,
1380	.sysfs_ops	= &f2fs_feature_list_attr_ops,
1381	.release	= f2fs_feature_list_kobj_release,
1382};
1383
1384static int __maybe_unused segment_info_seq_show(struct seq_file *seq,
1385						void *offset)
1386{
1387	struct super_block *sb = seq->private;
1388	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1389	unsigned int total_segs =
1390			le32_to_cpu(sbi->raw_super->segment_count_main);
1391	int i;
1392
1393	seq_puts(seq, "format: segment_type|valid_blocks\n"
1394		"segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
1395
1396	for (i = 0; i < total_segs; i++) {
1397		struct seg_entry *se = get_seg_entry(sbi, i);
1398
1399		if ((i % 10) == 0)
1400			seq_printf(seq, "%-10d", i);
1401		seq_printf(seq, "%d|%-3u", se->type, se->valid_blocks);
1402		if ((i % 10) == 9 || i == (total_segs - 1))
1403			seq_putc(seq, '\n');
1404		else
1405			seq_putc(seq, ' ');
1406	}
1407
1408	return 0;
1409}
1410
1411static int __maybe_unused segment_bits_seq_show(struct seq_file *seq,
1412						void *offset)
1413{
1414	struct super_block *sb = seq->private;
1415	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1416	unsigned int total_segs =
1417			le32_to_cpu(sbi->raw_super->segment_count_main);
1418	int i, j;
1419
1420	seq_puts(seq, "format: segment_type|valid_blocks|bitmaps\n"
1421		"segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
1422
1423	for (i = 0; i < total_segs; i++) {
1424		struct seg_entry *se = get_seg_entry(sbi, i);
1425
1426		seq_printf(seq, "%-10d", i);
1427		seq_printf(seq, "%d|%-3u|", se->type, se->valid_blocks);
1428		for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
1429			seq_printf(seq, " %.2x", se->cur_valid_map[j]);
1430		seq_putc(seq, '\n');
1431	}
1432	return 0;
1433}
1434
1435static int __maybe_unused victim_bits_seq_show(struct seq_file *seq,
1436						void *offset)
1437{
1438	struct super_block *sb = seq->private;
1439	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1440	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1441	int i;
1442
1443	seq_puts(seq, "format: victim_secmap bitmaps\n");
1444
1445	for (i = 0; i < MAIN_SECS(sbi); i++) {
1446		if ((i % 10) == 0)
1447			seq_printf(seq, "%-10d", i);
1448		seq_printf(seq, "%d", test_bit(i, dirty_i->victim_secmap) ? 1 : 0);
1449		if ((i % 10) == 9 || i == (MAIN_SECS(sbi) - 1))
1450			seq_putc(seq, '\n');
1451		else
1452			seq_putc(seq, ' ');
1453	}
1454	return 0;
1455}
1456
1457static int __maybe_unused discard_plist_seq_show(struct seq_file *seq,
1458						void *offset)
1459{
1460	struct super_block *sb = seq->private;
1461	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1462	struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1463	int i, count;
1464
1465	seq_puts(seq, "Discard pend list(Show diacrd_cmd count on each entry, .:not exist):\n");
1466	if (!f2fs_realtime_discard_enable(sbi))
1467		return 0;
1468
1469	if (dcc) {
1470		mutex_lock(&dcc->cmd_lock);
1471		for (i = 0; i < MAX_PLIST_NUM; i++) {
1472			struct list_head *pend_list;
1473			struct discard_cmd *dc, *tmp;
1474
1475			if (i % 8 == 0)
1476				seq_printf(seq, "  %-3d", i);
1477			count = 0;
1478			pend_list = &dcc->pend_list[i];
1479			list_for_each_entry_safe(dc, tmp, pend_list, list)
1480				count++;
1481			if (count)
1482				seq_printf(seq, " %7d", count);
1483			else
1484				seq_puts(seq, "       .");
1485			if (i % 8 == 7)
1486				seq_putc(seq, '\n');
1487		}
1488		seq_putc(seq, '\n');
1489		mutex_unlock(&dcc->cmd_lock);
1490	}
1491
1492	return 0;
1493}
1494
1495static int __maybe_unused disk_map_seq_show(struct seq_file *seq,
1496						void *offset)
1497{
1498	struct super_block *sb = seq->private;
1499	struct f2fs_sb_info *sbi = F2FS_SB(sb);
1500	int i;
1501
1502	seq_printf(seq, "Address Layout   : %5luB Block address (# of Segments)\n",
1503					F2FS_BLKSIZE);
1504	seq_printf(seq, " SB            : %12s\n", "0/1024B");
1505	seq_printf(seq, " seg0_blkaddr  : 0x%010x\n", SEG0_BLKADDR(sbi));
1506	seq_printf(seq, " Checkpoint    : 0x%010x (%10d)\n",
1507			le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr), 2);
1508	seq_printf(seq, " SIT           : 0x%010x (%10d)\n",
1509			SIT_I(sbi)->sit_base_addr,
1510			le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_sit));
1511	seq_printf(seq, " NAT           : 0x%010x (%10d)\n",
1512			NM_I(sbi)->nat_blkaddr,
1513			le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_nat));
1514	seq_printf(seq, " SSA           : 0x%010x (%10d)\n",
1515			SM_I(sbi)->ssa_blkaddr,
1516			le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_ssa));
1517	seq_printf(seq, " Main          : 0x%010x (%10d)\n",
1518			SM_I(sbi)->main_blkaddr,
1519			le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_main));
1520	seq_printf(seq, " # of Sections : %12d\n",
1521			le32_to_cpu(F2FS_RAW_SUPER(sbi)->section_count));
1522	seq_printf(seq, " Segs/Sections : %12d\n",
1523			SEGS_PER_SEC(sbi));
1524	seq_printf(seq, " Section size  : %12d MB\n",
1525			SEGS_PER_SEC(sbi) << 1);
1526
1527	if (!f2fs_is_multi_device(sbi))
1528		return 0;
1529
1530	seq_puts(seq, "\nDisk Map for multi devices:\n");
1531	for (i = 0; i < sbi->s_ndevs; i++)
1532		seq_printf(seq, "Disk:%2d (zoned=%d): 0x%010x - 0x%010x on %s\n",
1533			i, bdev_is_zoned(FDEV(i).bdev),
1534			FDEV(i).start_blk, FDEV(i).end_blk,
1535			FDEV(i).path);
1536	return 0;
1537}
1538
1539int __init f2fs_init_sysfs(void)
1540{
1541	int ret;
1542
1543	kobject_set_name(&f2fs_kset.kobj, "f2fs");
1544	f2fs_kset.kobj.parent = fs_kobj;
1545	ret = kset_register(&f2fs_kset);
1546	if (ret)
1547		return ret;
1548
1549	ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
1550				   NULL, "features");
1551	if (ret)
1552		goto put_kobject;
1553
1554	f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
1555	if (!f2fs_proc_root) {
1556		ret = -ENOMEM;
1557		goto put_kobject;
1558	}
1559
1560	return 0;
1561put_kobject:
1562	kobject_put(&f2fs_feat);
1563	kset_unregister(&f2fs_kset);
1564	return ret;
1565}
1566
1567void f2fs_exit_sysfs(void)
1568{
1569	kobject_put(&f2fs_feat);
1570	kset_unregister(&f2fs_kset);
1571	remove_proc_entry("fs/f2fs", NULL);
1572	f2fs_proc_root = NULL;
1573}
1574
1575int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
1576{
1577	struct super_block *sb = sbi->sb;
1578	int err;
1579
1580	sbi->s_kobj.kset = &f2fs_kset;
1581	init_completion(&sbi->s_kobj_unregister);
1582	err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
1583				"%s", sb->s_id);
1584	if (err)
1585		goto put_sb_kobj;
1586
1587	sbi->s_stat_kobj.kset = &f2fs_kset;
1588	init_completion(&sbi->s_stat_kobj_unregister);
1589	err = kobject_init_and_add(&sbi->s_stat_kobj, &f2fs_stat_ktype,
1590						&sbi->s_kobj, "stat");
1591	if (err)
1592		goto put_stat_kobj;
1593
1594	sbi->s_feature_list_kobj.kset = &f2fs_kset;
1595	init_completion(&sbi->s_feature_list_kobj_unregister);
1596	err = kobject_init_and_add(&sbi->s_feature_list_kobj,
1597					&f2fs_feature_list_ktype,
1598					&sbi->s_kobj, "feature_list");
1599	if (err)
1600		goto put_feature_list_kobj;
1601
1602	sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
1603	if (!sbi->s_proc) {
1604		err = -ENOMEM;
1605		goto put_feature_list_kobj;
1606	}
1607
1608	proc_create_single_data("segment_info", 0444, sbi->s_proc,
1609				segment_info_seq_show, sb);
1610	proc_create_single_data("segment_bits", 0444, sbi->s_proc,
1611				segment_bits_seq_show, sb);
1612#ifdef CONFIG_F2FS_IOSTAT
1613	proc_create_single_data("iostat_info", 0444, sbi->s_proc,
1614				iostat_info_seq_show, sb);
1615#endif
1616	proc_create_single_data("victim_bits", 0444, sbi->s_proc,
1617				victim_bits_seq_show, sb);
1618	proc_create_single_data("discard_plist_info", 0444, sbi->s_proc,
1619				discard_plist_seq_show, sb);
1620	proc_create_single_data("disk_map", 0444, sbi->s_proc,
1621				disk_map_seq_show, sb);
1622	return 0;
1623put_feature_list_kobj:
1624	kobject_put(&sbi->s_feature_list_kobj);
1625	wait_for_completion(&sbi->s_feature_list_kobj_unregister);
1626put_stat_kobj:
1627	kobject_put(&sbi->s_stat_kobj);
1628	wait_for_completion(&sbi->s_stat_kobj_unregister);
1629put_sb_kobj:
1630	kobject_put(&sbi->s_kobj);
1631	wait_for_completion(&sbi->s_kobj_unregister);
1632	return err;
1633}
1634
1635void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
1636{
1637	remove_proc_subtree(sbi->sb->s_id, f2fs_proc_root);
1638
1639	kobject_put(&sbi->s_stat_kobj);
1640	wait_for_completion(&sbi->s_stat_kobj_unregister);
1641	kobject_put(&sbi->s_feature_list_kobj);
1642	wait_for_completion(&sbi->s_feature_list_kobj_unregister);
1643
1644	kobject_put(&sbi->s_kobj);
1645	wait_for_completion(&sbi->s_kobj_unregister);
1646}