xref: /linux/drivers/md/dm-pcache/dm_pcache.h (revision 4f38da1f027ea2c9f01bb71daa7a299c191b6940)
1*1d57628fSDongsheng Yang /* SPDX-License-Identifier: GPL-2.0-or-later */
2*1d57628fSDongsheng Yang #ifndef _DM_PCACHE_H
3*1d57628fSDongsheng Yang #define _DM_PCACHE_H
4*1d57628fSDongsheng Yang #include <linux/device-mapper.h>
5*1d57628fSDongsheng Yang 
6*1d57628fSDongsheng Yang #include "../dm-core.h"
7*1d57628fSDongsheng Yang 
8*1d57628fSDongsheng Yang #define CACHE_DEV_TO_PCACHE(cache_dev)		(container_of(cache_dev, struct dm_pcache, cache_dev))
9*1d57628fSDongsheng Yang #define BACKING_DEV_TO_PCACHE(backing_dev)	(container_of(backing_dev, struct dm_pcache, backing_dev))
10*1d57628fSDongsheng Yang #define CACHE_TO_PCACHE(cache)			(container_of(cache, struct dm_pcache, cache))
11*1d57628fSDongsheng Yang 
12*1d57628fSDongsheng Yang #define PCACHE_STATE_RUNNING			1
13*1d57628fSDongsheng Yang #define PCACHE_STATE_STOPPING			2
14*1d57628fSDongsheng Yang 
15*1d57628fSDongsheng Yang struct pcache_cache_dev;
16*1d57628fSDongsheng Yang struct pcache_backing_dev;
17*1d57628fSDongsheng Yang struct pcache_cache;
18*1d57628fSDongsheng Yang struct pcache_cache_options;
19*1d57628fSDongsheng Yang struct dm_pcache {
20*1d57628fSDongsheng Yang 	struct dm_target *ti;
21*1d57628fSDongsheng Yang 	struct pcache_cache_dev cache_dev;
22*1d57628fSDongsheng Yang 	struct pcache_backing_dev backing_dev;
23*1d57628fSDongsheng Yang 	struct pcache_cache cache;
24*1d57628fSDongsheng Yang 	struct pcache_cache_options opts;
25*1d57628fSDongsheng Yang 
26*1d57628fSDongsheng Yang 	spinlock_t			defered_req_list_lock;
27*1d57628fSDongsheng Yang 	struct list_head		defered_req_list;
28*1d57628fSDongsheng Yang 	struct workqueue_struct		*task_wq;
29*1d57628fSDongsheng Yang 
30*1d57628fSDongsheng Yang 	struct work_struct		defered_req_work;
31*1d57628fSDongsheng Yang 
32*1d57628fSDongsheng Yang 	atomic_t			state;
33*1d57628fSDongsheng Yang 	atomic_t			inflight_reqs;
34*1d57628fSDongsheng Yang 	wait_queue_head_t		inflight_wq;
35*1d57628fSDongsheng Yang };
36*1d57628fSDongsheng Yang 
37*1d57628fSDongsheng Yang static inline bool pcache_is_stopping(struct dm_pcache *pcache)
38*1d57628fSDongsheng Yang {
39*1d57628fSDongsheng Yang 	return (atomic_read(&pcache->state) == PCACHE_STATE_STOPPING);
40*1d57628fSDongsheng Yang }
41*1d57628fSDongsheng Yang 
42*1d57628fSDongsheng Yang #define pcache_dev_err(pcache, fmt, ...)							\
43*1d57628fSDongsheng Yang 	pcache_err("%s " fmt, pcache->ti->table->md->name, ##__VA_ARGS__)
44*1d57628fSDongsheng Yang #define pcache_dev_info(pcache, fmt, ...)							\
45*1d57628fSDongsheng Yang 	pcache_info("%s " fmt, pcache->ti->table->md->name, ##__VA_ARGS__)
46*1d57628fSDongsheng Yang #define pcache_dev_debug(pcache, fmt, ...)							\
47*1d57628fSDongsheng Yang 	pcache_debug("%s " fmt, pcache->ti->table->md->name, ##__VA_ARGS__)
48*1d57628fSDongsheng Yang 
49*1d57628fSDongsheng Yang struct pcache_request {
50*1d57628fSDongsheng Yang 	struct dm_pcache	*pcache;
51*1d57628fSDongsheng Yang 	struct bio		*bio;
52*1d57628fSDongsheng Yang 
53*1d57628fSDongsheng Yang 	u64			off;
54*1d57628fSDongsheng Yang 	u32			data_len;
55*1d57628fSDongsheng Yang 
56*1d57628fSDongsheng Yang 	struct kref		ref;
57*1d57628fSDongsheng Yang 	int			ret;
58*1d57628fSDongsheng Yang 
59*1d57628fSDongsheng Yang 	struct list_head	list_node;
60*1d57628fSDongsheng Yang };
61*1d57628fSDongsheng Yang 
62*1d57628fSDongsheng Yang void pcache_req_get(struct pcache_request *pcache_req);
63*1d57628fSDongsheng Yang void pcache_req_put(struct pcache_request *pcache_req, int ret);
64*1d57628fSDongsheng Yang 
65*1d57628fSDongsheng Yang void pcache_defer_reqs_kick(struct dm_pcache *pcache);
66*1d57628fSDongsheng Yang 
67*1d57628fSDongsheng Yang #endif /* _DM_PCACHE_H */
68