162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * fs/hmdfs/client_writeback.h
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#ifndef CLIENT_WRITEBACK_H
962306a36Sopenharmony_ci#define CLIENT_WRITEBACK_H
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include "hmdfs.h"
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci/*
1462306a36Sopenharmony_ci * HM_DEFAULT_WRITEBACK_INTERVAL - centiseconds
1562306a36Sopenharmony_ci * HMDFS_FILE_BG_WB_BYTES - background per-file threshold 10M
1662306a36Sopenharmony_ci * HMDFS_FS_BG_WB_BYTES - background per-fs threshold 50M
1762306a36Sopenharmony_ci * HMDFS_FILE_WB_BYTES - per-file throttle threshold
1862306a36Sopenharmony_ci * HMDFS_FS_WB_BYTES - per-fs throttle threshold
1962306a36Sopenharmony_ci */
2062306a36Sopenharmony_ci#define HM_DEFAULT_WRITEBACK_INTERVAL	500
2162306a36Sopenharmony_ci#define HMDFS_FILE_BG_WB_BYTES		(10 * 1024 * 1024)
2262306a36Sopenharmony_ci#define HMDFS_FS_BG_WB_BYTES		(50 * 1024 * 1024)
2362306a36Sopenharmony_ci#define HMDFS_FILE_WB_BYTES		(HMDFS_FILE_BG_WB_BYTES << 1)
2462306a36Sopenharmony_ci#define HMDFS_FS_WB_BYTES		(HMDFS_FS_BG_WB_BYTES << 1)
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci/* writeback time limit (default 5s) */
2762306a36Sopenharmony_ci#define HMDFS_DEF_WB_TIMELIMIT		(5 * HZ)
2862306a36Sopenharmony_ci#define HMDFS_MAX_WB_TIMELIMIT		(30 * HZ)
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci/* bandwidth adjusted lower limit (default 1MB/s) */
3162306a36Sopenharmony_ci#define HMDFS_BW_THRESH_MIN_LIMIT	(1 << (20 - PAGE_SHIFT))
3262306a36Sopenharmony_ci#define HMDFS_BW_THRESH_MAX_LIMIT	(100 << (20 - PAGE_SHIFT))
3362306a36Sopenharmony_ci#define HMDFS_BW_THRESH_DEF_LIMIT	HMDFS_BW_THRESH_MIN_LIMIT
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci#define HMDFS_DIRTY_EXCEED_RATELIMIT	(32 >> (PAGE_SHIFT - 10))
3662306a36Sopenharmony_ci#define HMDFS_RATELIMIT_PAGES_GAP	16
3762306a36Sopenharmony_ci#define HMDFS_DEF_RATELIMIT_PAGES	32
3862306a36Sopenharmony_ci#define HMDFS_MIN_RATELIMIT_PAGES	1
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_cistruct hmdfs_dirty_throttle_control {
4162306a36Sopenharmony_ci	struct hmdfs_writeback *hwb;
4262306a36Sopenharmony_ci	/* last time threshes are updated */
4362306a36Sopenharmony_ci	unsigned long thresh_time_stamp;
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci	unsigned long file_bg_thresh;
4662306a36Sopenharmony_ci	unsigned long fs_bg_thresh;
4762306a36Sopenharmony_ci	unsigned long file_thresh;
4862306a36Sopenharmony_ci	unsigned long fs_thresh;
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci	unsigned long file_nr_dirty;
5162306a36Sopenharmony_ci	unsigned long fs_nr_dirty;
5262306a36Sopenharmony_ci	unsigned long file_nr_reclaimable;
5362306a36Sopenharmony_ci	unsigned long fs_nr_reclaimable;
5462306a36Sopenharmony_ci};
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_cistruct hmdfs_writeback {
5762306a36Sopenharmony_ci	struct hmdfs_sb_info *sbi;
5862306a36Sopenharmony_ci	struct bdi_writeback *wb;
5962306a36Sopenharmony_ci	/* enable hmdfs dirty writeback control */
6062306a36Sopenharmony_ci	bool dirty_writeback_control;
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci	/* writeback per-file inode list */
6362306a36Sopenharmony_ci	struct list_head inode_list_head;
6462306a36Sopenharmony_ci	spinlock_t inode_list_lock;
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci	/* centiseconds */
6762306a36Sopenharmony_ci	unsigned int dirty_writeback_interval;
6862306a36Sopenharmony_ci	/* per-file background threshold */
6962306a36Sopenharmony_ci	unsigned long dirty_file_bg_bytes;
7062306a36Sopenharmony_ci	unsigned long dirty_file_bg_thresh;
7162306a36Sopenharmony_ci	/* per-fs background threshold */
7262306a36Sopenharmony_ci	unsigned long dirty_fs_bg_bytes;
7362306a36Sopenharmony_ci	unsigned long dirty_fs_bg_thresh;
7462306a36Sopenharmony_ci	/* per-file throttle threshold */
7562306a36Sopenharmony_ci	unsigned long dirty_file_bytes;
7662306a36Sopenharmony_ci	unsigned long dirty_file_thresh;
7762306a36Sopenharmony_ci	/* per-fs throttle threshold */
7862306a36Sopenharmony_ci	unsigned long dirty_fs_bytes;
7962306a36Sopenharmony_ci	unsigned long dirty_fs_thresh;
8062306a36Sopenharmony_ci	/* ratio between background thresh and throttle thresh */
8162306a36Sopenharmony_ci	unsigned long fs_bg_ratio;
8262306a36Sopenharmony_ci	unsigned long file_bg_ratio;
8362306a36Sopenharmony_ci	/* ratio between file and fs throttle thresh */
8462306a36Sopenharmony_ci	unsigned long fs_file_ratio;
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci	/*
8762306a36Sopenharmony_ci	 * Enable auto-thresh. If enabled, the background and throttle
8862306a36Sopenharmony_ci	 * thresh are nolonger a fixed value storeed in dirty_*_bytes,
8962306a36Sopenharmony_ci	 * they are determined by the bandwidth of the network and the
9062306a36Sopenharmony_ci	 * writeback timelimit.
9162306a36Sopenharmony_ci	 */
9262306a36Sopenharmony_ci	bool dirty_auto_threshold;
9362306a36Sopenharmony_ci	unsigned int writeback_timelimit;
9462306a36Sopenharmony_ci	/* bandwitdh adjusted filesystem throttle thresh */
9562306a36Sopenharmony_ci	unsigned long bw_fs_thresh;
9662306a36Sopenharmony_ci	/* bandwidth adjusted per-file throttle thresh */
9762306a36Sopenharmony_ci	unsigned long bw_file_thresh;
9862306a36Sopenharmony_ci	/* bandwidth adjusted thresh lower limit */
9962306a36Sopenharmony_ci	unsigned long bw_thresh_lowerlimit;
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci	/* reclaimable pages exceed throttle thresh */
10262306a36Sopenharmony_ci	bool dirty_exceeded;
10362306a36Sopenharmony_ci	/* percpu dirty pages ratelimit */
10462306a36Sopenharmony_ci	long ratelimit_pages;
10562306a36Sopenharmony_ci	/* count percpu dirty pages */
10662306a36Sopenharmony_ci	int __percpu *bdp_ratelimits;
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci	/* per-fs writeback work */
10962306a36Sopenharmony_ci	struct workqueue_struct *dirty_sb_writeback_wq;
11062306a36Sopenharmony_ci	struct delayed_work dirty_sb_writeback_work;
11162306a36Sopenharmony_ci	/* per-file writeback work */
11262306a36Sopenharmony_ci	struct workqueue_struct *dirty_inode_writeback_wq;
11362306a36Sopenharmony_ci	struct delayed_work dirty_inode_writeback_work;
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci	/* per-fs writeback bandwidth */
11662306a36Sopenharmony_ci	spinlock_t write_bandwidth_lock;
11762306a36Sopenharmony_ci	unsigned long max_write_bandwidth;
11862306a36Sopenharmony_ci	unsigned long min_write_bandwidth;
11962306a36Sopenharmony_ci	unsigned long avg_write_bandwidth;
12062306a36Sopenharmony_ci};
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_civoid hmdfs_writeback_inodes_sb_handler(struct work_struct *work);
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_civoid hmdfs_writeback_inode_handler(struct work_struct *work);
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_civoid hmdfs_calculate_dirty_thresh(struct hmdfs_writeback *hwb);
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_civoid hmdfs_update_ratelimit(struct hmdfs_writeback *hwb);
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_civoid hmdfs_balance_dirty_pages_ratelimited(struct address_space *mapping);
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_civoid hmdfs_destroy_writeback(struct hmdfs_sb_info *sbi);
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ciint hmdfs_init_writeback(struct hmdfs_sb_info *sbi);
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci#endif
137