1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * fs/hmdfs/client_writeback.h
4 *
5 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
6 */
7
8#ifndef CLIENT_WRITEBACK_H
9#define CLIENT_WRITEBACK_H
10
11#include "hmdfs.h"
12
13/*
14 * HM_DEFAULT_WRITEBACK_INTERVAL - centiseconds
15 * HMDFS_FILE_BG_WB_BYTES - background per-file threshold 10M
16 * HMDFS_FS_BG_WB_BYTES - background per-fs threshold 50M
17 * HMDFS_FILE_WB_BYTES - per-file throttle threshold
18 * HMDFS_FS_WB_BYTES - per-fs throttle threshold
19 */
20#define HM_DEFAULT_WRITEBACK_INTERVAL	500
21#define HMDFS_FILE_BG_WB_BYTES		(10 * 1024 * 1024)
22#define HMDFS_FS_BG_WB_BYTES		(50 * 1024 * 1024)
23#define HMDFS_FILE_WB_BYTES		(HMDFS_FILE_BG_WB_BYTES << 1)
24#define HMDFS_FS_WB_BYTES		(HMDFS_FS_BG_WB_BYTES << 1)
25
26/* writeback time limit (default 5s) */
27#define HMDFS_DEF_WB_TIMELIMIT		(5 * HZ)
28#define HMDFS_MAX_WB_TIMELIMIT		(30 * HZ)
29
30/* bandwidth adjusted lower limit (default 1MB/s) */
31#define HMDFS_BW_THRESH_MIN_LIMIT	(1 << (20 - PAGE_SHIFT))
32#define HMDFS_BW_THRESH_MAX_LIMIT	(100 << (20 - PAGE_SHIFT))
33#define HMDFS_BW_THRESH_DEF_LIMIT	HMDFS_BW_THRESH_MIN_LIMIT
34
35#define HMDFS_DIRTY_EXCEED_RATELIMIT	(32 >> (PAGE_SHIFT - 10))
36#define HMDFS_RATELIMIT_PAGES_GAP	16
37#define HMDFS_DEF_RATELIMIT_PAGES	32
38#define HMDFS_MIN_RATELIMIT_PAGES	1
39
40struct hmdfs_dirty_throttle_control {
41	struct hmdfs_writeback *hwb;
42	/* last time threshes are updated */
43	unsigned long thresh_time_stamp;
44
45	unsigned long file_bg_thresh;
46	unsigned long fs_bg_thresh;
47	unsigned long file_thresh;
48	unsigned long fs_thresh;
49
50	unsigned long file_nr_dirty;
51	unsigned long fs_nr_dirty;
52	unsigned long file_nr_reclaimable;
53	unsigned long fs_nr_reclaimable;
54};
55
56struct hmdfs_writeback {
57	struct hmdfs_sb_info *sbi;
58	struct bdi_writeback *wb;
59	/* enable hmdfs dirty writeback control */
60	bool dirty_writeback_control;
61
62	/* writeback per-file inode list */
63	struct list_head inode_list_head;
64	spinlock_t inode_list_lock;
65
66	/* centiseconds */
67	unsigned int dirty_writeback_interval;
68	/* per-file background threshold */
69	unsigned long dirty_file_bg_bytes;
70	unsigned long dirty_file_bg_thresh;
71	/* per-fs background threshold */
72	unsigned long dirty_fs_bg_bytes;
73	unsigned long dirty_fs_bg_thresh;
74	/* per-file throttle threshold */
75	unsigned long dirty_file_bytes;
76	unsigned long dirty_file_thresh;
77	/* per-fs throttle threshold */
78	unsigned long dirty_fs_bytes;
79	unsigned long dirty_fs_thresh;
80	/* ratio between background thresh and throttle thresh */
81	unsigned long fs_bg_ratio;
82	unsigned long file_bg_ratio;
83	/* ratio between file and fs throttle thresh */
84	unsigned long fs_file_ratio;
85
86	/*
87	 * Enable auto-thresh. If enabled, the background and throttle
88	 * thresh are nolonger a fixed value storeed in dirty_*_bytes,
89	 * they are determined by the bandwidth of the network and the
90	 * writeback timelimit.
91	 */
92	bool dirty_auto_threshold;
93	unsigned int writeback_timelimit;
94	/* bandwitdh adjusted filesystem throttle thresh */
95	unsigned long bw_fs_thresh;
96	/* bandwidth adjusted per-file throttle thresh */
97	unsigned long bw_file_thresh;
98	/* bandwidth adjusted thresh lower limit */
99	unsigned long bw_thresh_lowerlimit;
100
101	/* reclaimable pages exceed throttle thresh */
102	bool dirty_exceeded;
103	/* percpu dirty pages ratelimit */
104	long ratelimit_pages;
105	/* count percpu dirty pages */
106	int __percpu *bdp_ratelimits;
107
108	/* per-fs writeback work */
109	struct workqueue_struct *dirty_sb_writeback_wq;
110	struct delayed_work dirty_sb_writeback_work;
111	/* per-file writeback work */
112	struct workqueue_struct *dirty_inode_writeback_wq;
113	struct delayed_work dirty_inode_writeback_work;
114
115	/* per-fs writeback bandwidth */
116	spinlock_t write_bandwidth_lock;
117	unsigned long max_write_bandwidth;
118	unsigned long min_write_bandwidth;
119	unsigned long avg_write_bandwidth;
120};
121
122void hmdfs_writeback_inodes_sb_handler(struct work_struct *work);
123
124void hmdfs_writeback_inode_handler(struct work_struct *work);
125
126void hmdfs_calculate_dirty_thresh(struct hmdfs_writeback *hwb);
127
128void hmdfs_update_ratelimit(struct hmdfs_writeback *hwb);
129
130void hmdfs_balance_dirty_pages_ratelimited(struct address_space *mapping);
131
132void hmdfs_destroy_writeback(struct hmdfs_sb_info *sbi);
133
134int hmdfs_init_writeback(struct hmdfs_sb_info *sbi);
135
136#endif
137