1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
266114cadSTejun Heo #ifndef __LINUX_BACKING_DEV_DEFS_H
366114cadSTejun Heo #define __LINUX_BACKING_DEV_DEFS_H
466114cadSTejun Heo 
566114cadSTejun Heo #include <linux/list.h>
652ebea74STejun Heo #include <linux/radix-tree.h>
752ebea74STejun Heo #include <linux/rbtree.h>
866114cadSTejun Heo #include <linux/spinlock.h>
966114cadSTejun Heo #include <linux/percpu_counter.h>
1052ebea74STejun Heo #include <linux/percpu-refcount.h>
1166114cadSTejun Heo #include <linux/flex_proportions.h>
1266114cadSTejun Heo #include <linux/timer.h>
1366114cadSTejun Heo #include <linux/workqueue.h>
14d03f6cdcSJan Kara #include <linux/kref.h>
15e58dd0deSSebastian Andrzej Siewior #include <linux/refcount.h>
1666114cadSTejun Heo 
1766114cadSTejun Heo struct page;
1866114cadSTejun Heo struct device;
1966114cadSTejun Heo struct dentry;
2066114cadSTejun Heo 
2166114cadSTejun Heo /*
2266114cadSTejun Heo  * Bits in bdi_writeback.state
2366114cadSTejun Heo  */
2466114cadSTejun Heo enum wb_state {
2566114cadSTejun Heo 	WB_registered,		/* bdi_register() was done */
2666114cadSTejun Heo 	WB_writeback_running,	/* Writeback is in progress */
27d6c10f1fSTejun Heo 	WB_has_dirty_io,	/* Dirty inodes on ->b_{dirty|io|more_io} */
28aac8d41cSJens Axboe 	WB_start_all,		/* nr_pages == 0 (all) work pending */
2966114cadSTejun Heo };
3066114cadSTejun Heo 
3166114cadSTejun Heo enum wb_stat_item {
3266114cadSTejun Heo 	WB_RECLAIMABLE,
3366114cadSTejun Heo 	WB_WRITEBACK,
3466114cadSTejun Heo 	WB_DIRTIED,
3566114cadSTejun Heo 	WB_WRITTEN,
3666114cadSTejun Heo 	NR_WB_STAT_ITEMS
3766114cadSTejun Heo };
3866114cadSTejun Heo 
3966114cadSTejun Heo #define WB_STAT_BATCH (8*(1+ilog2(nr_cpu_ids)))
4066114cadSTejun Heo 
4152ebea74STejun Heo /*
4285009b4fSJens Axboe  * why some writeback work was initiated
4385009b4fSJens Axboe  */
4485009b4fSJens Axboe enum wb_reason {
4585009b4fSJens Axboe 	WB_REASON_BACKGROUND,
4685009b4fSJens Axboe 	WB_REASON_VMSCAN,
4785009b4fSJens Axboe 	WB_REASON_SYNC,
4885009b4fSJens Axboe 	WB_REASON_PERIODIC,
4985009b4fSJens Axboe 	WB_REASON_LAPTOP_TIMER,
5085009b4fSJens Axboe 	WB_REASON_FS_FREE_SPACE,
5185009b4fSJens Axboe 	/*
5285009b4fSJens Axboe 	 * There is no bdi forker thread any more and works are done
5385009b4fSJens Axboe 	 * by emergency worker, however, this is TPs userland visible
5485009b4fSJens Axboe 	 * and we'll be exposing exactly the same information,
5585009b4fSJens Axboe 	 * so it has a mismatch name.
5685009b4fSJens Axboe 	 */
5785009b4fSJens Axboe 	WB_REASON_FORKER_THREAD,
5897b27821STejun Heo 	WB_REASON_FOREIGN_FLUSH,
5985009b4fSJens Axboe 
6085009b4fSJens Axboe 	WB_REASON_MAX,
6185009b4fSJens Axboe };
6285009b4fSJens Axboe 
635b9cce4cSTejun Heo struct wb_completion {
645b9cce4cSTejun Heo 	atomic_t		cnt;
655b9cce4cSTejun Heo 	wait_queue_head_t	*waitq;
665b9cce4cSTejun Heo };
675b9cce4cSTejun Heo 
685b9cce4cSTejun Heo #define __WB_COMPLETION_INIT(_waitq)	\
695b9cce4cSTejun Heo 	(struct wb_completion){ .cnt = ATOMIC_INIT(1), .waitq = (_waitq) }
705b9cce4cSTejun Heo 
715b9cce4cSTejun Heo /*
725b9cce4cSTejun Heo  * If one wants to wait for one or more wb_writeback_works, each work's
735b9cce4cSTejun Heo  * ->done should be set to a wb_completion defined using the following
745b9cce4cSTejun Heo  * macro.  Once all work items are issued with wb_queue_work(), the caller
755b9cce4cSTejun Heo  * can wait for the completion of all using wb_wait_for_completion().  Work
765b9cce4cSTejun Heo  * items which are waited upon aren't freed automatically on completion.
775b9cce4cSTejun Heo  */
785b9cce4cSTejun Heo #define WB_COMPLETION_INIT(bdi)		__WB_COMPLETION_INIT(&(bdi)->wb_waitq)
795b9cce4cSTejun Heo 
805b9cce4cSTejun Heo #define DEFINE_WB_COMPLETION(cmpl, bdi)	\
815b9cce4cSTejun Heo 	struct wb_completion cmpl = WB_COMPLETION_INIT(bdi)
825b9cce4cSTejun Heo 
8385009b4fSJens Axboe /*
8452ebea74STejun Heo  * Each wb (bdi_writeback) can perform writeback operations, is measured
8552ebea74STejun Heo  * and throttled, independently.  Without cgroup writeback, each bdi
8652ebea74STejun Heo  * (bdi_writeback) is served by its embedded bdi->wb.
8752ebea74STejun Heo  *
8852ebea74STejun Heo  * On the default hierarchy, blkcg implicitly enables memcg.  This allows
8952ebea74STejun Heo  * using memcg's page ownership for attributing writeback IOs, and every
9052ebea74STejun Heo  * memcg - blkcg combination can be served by its own wb by assigning a
9152ebea74STejun Heo  * dedicated wb to each memcg, which enables isolation across different
9252ebea74STejun Heo  * cgroups and propagation of IO back pressure down from the IO layer upto
9352ebea74STejun Heo  * the tasks which are generating the dirty pages to be written back.
9452ebea74STejun Heo  *
9552ebea74STejun Heo  * A cgroup wb is indexed on its bdi by the ID of the associated memcg,
9652ebea74STejun Heo  * refcounted with the number of inodes attached to it, and pins the memcg
9752ebea74STejun Heo  * and the corresponding blkcg.  As the corresponding blkcg for a memcg may
9852ebea74STejun Heo  * change as blkcg is disabled and enabled higher up in the hierarchy, a wb
9952ebea74STejun Heo  * is tested for blkcg after lookup and removed from index on mismatch so
10052ebea74STejun Heo  * that a new wb for the combination can be created.
101efee1713SChristoph Hellwig  *
102efee1713SChristoph Hellwig  * Each bdi_writeback that is not embedded into the backing_dev_info must hold
103efee1713SChristoph Hellwig  * a reference to the parent backing_dev_info.  See cgwb_create() for details.
10452ebea74STejun Heo  */
10566114cadSTejun Heo struct bdi_writeback {
10666114cadSTejun Heo 	struct backing_dev_info *bdi;	/* our parent bdi */
10766114cadSTejun Heo 
10866114cadSTejun Heo 	unsigned long state;		/* Always use atomic bitops on this */
10966114cadSTejun Heo 	unsigned long last_old_flush;	/* last old data flush */
11066114cadSTejun Heo 
11166114cadSTejun Heo 	struct list_head b_dirty;	/* dirty inodes */
11266114cadSTejun Heo 	struct list_head b_io;		/* parked for writeback */
11366114cadSTejun Heo 	struct list_head b_more_io;	/* parked for more writeback */
11466114cadSTejun Heo 	struct list_head b_dirty_time;	/* time stamps are dirty */
11566114cadSTejun Heo 	spinlock_t list_lock;		/* protects the b_* lists */
11666114cadSTejun Heo 
117633a2abbSJan Kara 	atomic_t writeback_inodes;	/* number of inodes under writeback */
11866114cadSTejun Heo 	struct percpu_counter stat[NR_WB_STAT_ITEMS];
11966114cadSTejun Heo 
12066114cadSTejun Heo 	unsigned long bw_time_stamp;	/* last time write bw is updated */
12166114cadSTejun Heo 	unsigned long dirtied_stamp;
12266114cadSTejun Heo 	unsigned long written_stamp;	/* pages written at bw_time_stamp */
12366114cadSTejun Heo 	unsigned long write_bandwidth;	/* the estimated write bandwidth */
12495a46c65STejun Heo 	unsigned long avg_write_bandwidth; /* further smoothed write bw, > 0 */
12566114cadSTejun Heo 
12666114cadSTejun Heo 	/*
12766114cadSTejun Heo 	 * The base dirty throttle rate, re-calculated on every 200ms.
12866114cadSTejun Heo 	 * All the bdi tasks' dirty rate will be curbed under it.
12966114cadSTejun Heo 	 * @dirty_ratelimit tracks the estimated @balanced_dirty_ratelimit
13066114cadSTejun Heo 	 * in small steps and is much more smooth/stable than the latter.
13166114cadSTejun Heo 	 */
13266114cadSTejun Heo 	unsigned long dirty_ratelimit;
13366114cadSTejun Heo 	unsigned long balanced_dirty_ratelimit;
13466114cadSTejun Heo 
13566114cadSTejun Heo 	struct fprop_local_percpu completions;
13666114cadSTejun Heo 	int dirty_exceeded;
13785009b4fSJens Axboe 	enum wb_reason start_all_reason;
13866114cadSTejun Heo 
13966114cadSTejun Heo 	spinlock_t work_lock;		/* protects work_list & dwork scheduling */
14066114cadSTejun Heo 	struct list_head work_list;
14166114cadSTejun Heo 	struct delayed_work dwork;	/* work item used for writeback */
14245a2966fSJan Kara 	struct delayed_work bw_dwork;	/* work item used for bandwidth estimate */
14352ebea74STejun Heo 
144b817525aSTejun Heo 	struct list_head bdi_node;	/* anchored at bdi->wb_list */
145b817525aSTejun Heo 
14652ebea74STejun Heo #ifdef CONFIG_CGROUP_WRITEBACK
14752ebea74STejun Heo 	struct percpu_ref refcnt;	/* used only for !root wb's */
148841710aaSTejun Heo 	struct fprop_local_percpu memcg_completions;
14952ebea74STejun Heo 	struct cgroup_subsys_state *memcg_css; /* the associated memcg */
15052ebea74STejun Heo 	struct cgroup_subsys_state *blkcg_css; /* and blkcg */
15152ebea74STejun Heo 	struct list_head memcg_node;	/* anchored at memcg->cgwb_list */
15252ebea74STejun Heo 	struct list_head blkcg_node;	/* anchored at blkcg->cgwb_list */
153f3b6a6dfSRoman Gushchin 	struct list_head b_attached;	/* attached inodes, protected by list_lock */
154c22d70a1SRoman Gushchin 	struct list_head offline_node;	/* anchored at offline_cgwbs */
15552ebea74STejun Heo 
15652ebea74STejun Heo 	union {
15752ebea74STejun Heo 		struct work_struct release_work;
15852ebea74STejun Heo 		struct rcu_head rcu;
15952ebea74STejun Heo 	};
16052ebea74STejun Heo #endif
16166114cadSTejun Heo };
16266114cadSTejun Heo 
16366114cadSTejun Heo struct backing_dev_info {
16434f8fe50STejun Heo 	u64 id;
16534f8fe50STejun Heo 	struct rb_node rb_node; /* keyed by ->id */
16666114cadSTejun Heo 	struct list_head bdi_list;
167ea1754a0SKirill A. Shutemov 	unsigned long ra_pages;	/* max readahead in PAGE_SIZE units */
1689491ae4aSJens Axboe 	unsigned long io_pages;	/* max allowed IO size */
16966114cadSTejun Heo 
170d03f6cdcSJan Kara 	struct kref refcnt;	/* Reference counter for the structure */
1718db378a5SAndrew Morton 	unsigned int capabilities; /* Device capabilities */
17266114cadSTejun Heo 	unsigned int min_ratio;
17366114cadSTejun Heo 	unsigned int max_ratio, max_prop_frac;
17466114cadSTejun Heo 
17595a46c65STejun Heo 	/*
17695a46c65STejun Heo 	 * Sum of avg_write_bw of wbs with dirty inodes.  > 0 if there are
17795a46c65STejun Heo 	 * any dirty wbs, which is depended upon by bdi_has_dirty().
17895a46c65STejun Heo 	 */
17995a46c65STejun Heo 	atomic_long_t tot_write_bandwidth;
180*601b5540SJan Kara 	/*
181*601b5540SJan Kara 	 * Jiffies when last process was dirty throttled on this bdi. Used by
182*601b5540SJan Kara 	 * blk-wbt.
183*601b5540SJan Kara 	 */
184*601b5540SJan Kara 	unsigned long last_bdp_sleep;
185766a9d6eSTejun Heo 
18652ebea74STejun Heo 	struct bdi_writeback wb;  /* the root writeback info for this bdi */
187b817525aSTejun Heo 	struct list_head wb_list; /* list of all wbs */
18852ebea74STejun Heo #ifdef CONFIG_CGROUP_WRITEBACK
18952ebea74STejun Heo 	struct radix_tree_root cgwb_tree; /* radix tree of active cgroup wbs */
1903ee7e869SJan Kara 	struct mutex cgwb_release_mutex;  /* protect shutdown of wb structs */
1917fc5854fSTejun Heo 	struct rw_semaphore wb_switch_rwsem; /* no cgwb switch while syncing */
19252ebea74STejun Heo #endif
193cc395d7fSTejun Heo 	wait_queue_head_t wb_waitq;
194cc395d7fSTejun Heo 
19566114cadSTejun Heo 	struct device *dev;
1966bd87eecSChristoph Hellwig 	char dev_name[64];
197df08c32cSDan Williams 	struct device *owner;
19866114cadSTejun Heo 
19966114cadSTejun Heo 	struct timer_list laptop_mode_wb_timer;
20066114cadSTejun Heo 
20166114cadSTejun Heo #ifdef CONFIG_DEBUG_FS
20266114cadSTejun Heo 	struct dentry *debug_dir;
20366114cadSTejun Heo #endif
20466114cadSTejun Heo };
20566114cadSTejun Heo 
2062e898e4cSGreg Thelen struct wb_lock_cookie {
2072e898e4cSGreg Thelen 	bool locked;
2082e898e4cSGreg Thelen 	unsigned long flags;
2092e898e4cSGreg Thelen };
2102e898e4cSGreg Thelen 
21121c6321fSTejun Heo #ifdef CONFIG_CGROUP_WRITEBACK
21221c6321fSTejun Heo 
21321c6321fSTejun Heo /**
21421c6321fSTejun Heo  * wb_tryget - try to increment a wb's refcount
21521c6321fSTejun Heo  * @wb: bdi_writeback to get
21621c6321fSTejun Heo  */
wb_tryget(struct bdi_writeback * wb)21721c6321fSTejun Heo static inline bool wb_tryget(struct bdi_writeback *wb)
21821c6321fSTejun Heo {
21921c6321fSTejun Heo 	if (wb != &wb->bdi->wb)
22021c6321fSTejun Heo 		return percpu_ref_tryget(&wb->refcnt);
22121c6321fSTejun Heo 	return true;
22221c6321fSTejun Heo }
22321c6321fSTejun Heo 
22421c6321fSTejun Heo /**
22521c6321fSTejun Heo  * wb_get - increment a wb's refcount
22621c6321fSTejun Heo  * @wb: bdi_writeback to get
22721c6321fSTejun Heo  */
wb_get(struct bdi_writeback * wb)22821c6321fSTejun Heo static inline void wb_get(struct bdi_writeback *wb)
22921c6321fSTejun Heo {
23021c6321fSTejun Heo 	if (wb != &wb->bdi->wb)
23121c6321fSTejun Heo 		percpu_ref_get(&wb->refcnt);
23221c6321fSTejun Heo }
23321c6321fSTejun Heo 
23421c6321fSTejun Heo /**
23521c6321fSTejun Heo  * wb_put - decrement a wb's refcount
23621c6321fSTejun Heo  * @wb: bdi_writeback to put
237f5fbe6b7SRoman Gushchin  * @nr: number of references to put
23821c6321fSTejun Heo  */
wb_put_many(struct bdi_writeback * wb,unsigned long nr)239f5fbe6b7SRoman Gushchin static inline void wb_put_many(struct bdi_writeback *wb, unsigned long nr)
24021c6321fSTejun Heo {
241368686a9SAnders Roxell 	if (WARN_ON_ONCE(!wb->bdi)) {
242368686a9SAnders Roxell 		/*
243368686a9SAnders Roxell 		 * A driver bug might cause a file to be removed before bdi was
244368686a9SAnders Roxell 		 * initialized.
245368686a9SAnders Roxell 		 */
246368686a9SAnders Roxell 		return;
247368686a9SAnders Roxell 	}
248368686a9SAnders Roxell 
24921c6321fSTejun Heo 	if (wb != &wb->bdi->wb)
250f5fbe6b7SRoman Gushchin 		percpu_ref_put_many(&wb->refcnt, nr);
251f5fbe6b7SRoman Gushchin }
252f5fbe6b7SRoman Gushchin 
253f5fbe6b7SRoman Gushchin /**
254f5fbe6b7SRoman Gushchin  * wb_put - decrement a wb's refcount
255f5fbe6b7SRoman Gushchin  * @wb: bdi_writeback to put
256f5fbe6b7SRoman Gushchin  */
wb_put(struct bdi_writeback * wb)257f5fbe6b7SRoman Gushchin static inline void wb_put(struct bdi_writeback *wb)
258f5fbe6b7SRoman Gushchin {
259f5fbe6b7SRoman Gushchin 	wb_put_many(wb, 1);
26021c6321fSTejun Heo }
26121c6321fSTejun Heo 
262e8a7abf5STejun Heo /**
263e8a7abf5STejun Heo  * wb_dying - is a wb dying?
264e8a7abf5STejun Heo  * @wb: bdi_writeback of interest
265e8a7abf5STejun Heo  *
266e8a7abf5STejun Heo  * Returns whether @wb is unlinked and being drained.
267e8a7abf5STejun Heo  */
wb_dying(struct bdi_writeback * wb)268e8a7abf5STejun Heo static inline bool wb_dying(struct bdi_writeback *wb)
269e8a7abf5STejun Heo {
270e8a7abf5STejun Heo 	return percpu_ref_is_dying(&wb->refcnt);
271e8a7abf5STejun Heo }
272e8a7abf5STejun Heo 
27321c6321fSTejun Heo #else	/* CONFIG_CGROUP_WRITEBACK */
27421c6321fSTejun Heo 
wb_tryget(struct bdi_writeback * wb)27521c6321fSTejun Heo static inline bool wb_tryget(struct bdi_writeback *wb)
27621c6321fSTejun Heo {
27721c6321fSTejun Heo 	return true;
27821c6321fSTejun Heo }
27921c6321fSTejun Heo 
wb_get(struct bdi_writeback * wb)28021c6321fSTejun Heo static inline void wb_get(struct bdi_writeback *wb)
28121c6321fSTejun Heo {
28221c6321fSTejun Heo }
28321c6321fSTejun Heo 
wb_put(struct bdi_writeback * wb)28421c6321fSTejun Heo static inline void wb_put(struct bdi_writeback *wb)
28521c6321fSTejun Heo {
28621c6321fSTejun Heo }
28721c6321fSTejun Heo 
wb_put_many(struct bdi_writeback * wb,unsigned long nr)288f5fbe6b7SRoman Gushchin static inline void wb_put_many(struct bdi_writeback *wb, unsigned long nr)
289f5fbe6b7SRoman Gushchin {
290f5fbe6b7SRoman Gushchin }
291f5fbe6b7SRoman Gushchin 
wb_dying(struct bdi_writeback * wb)292e8a7abf5STejun Heo static inline bool wb_dying(struct bdi_writeback *wb)
293e8a7abf5STejun Heo {
294e8a7abf5STejun Heo 	return false;
295e8a7abf5STejun Heo }
296e8a7abf5STejun Heo 
29721c6321fSTejun Heo #endif	/* CONFIG_CGROUP_WRITEBACK */
29821c6321fSTejun Heo 
29966114cadSTejun Heo #endif	/* __LINUX_BACKING_DEV_DEFS_H */
300