xref: /OK3568_Linux_fs/kernel/drivers/md/dm-thin.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (C) 2011-2012 Red Hat UK.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * This file is released under the GPL.
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include "dm-thin-metadata.h"
8*4882a593Smuzhiyun #include "dm-bio-prison-v1.h"
9*4882a593Smuzhiyun #include "dm.h"
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <linux/device-mapper.h>
12*4882a593Smuzhiyun #include <linux/dm-io.h>
13*4882a593Smuzhiyun #include <linux/dm-kcopyd.h>
14*4882a593Smuzhiyun #include <linux/jiffies.h>
15*4882a593Smuzhiyun #include <linux/log2.h>
16*4882a593Smuzhiyun #include <linux/list.h>
17*4882a593Smuzhiyun #include <linux/rculist.h>
18*4882a593Smuzhiyun #include <linux/init.h>
19*4882a593Smuzhiyun #include <linux/module.h>
20*4882a593Smuzhiyun #include <linux/slab.h>
21*4882a593Smuzhiyun #include <linux/vmalloc.h>
22*4882a593Smuzhiyun #include <linux/sort.h>
23*4882a593Smuzhiyun #include <linux/rbtree.h>
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #define	DM_MSG_PREFIX	"thin"
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun /*
28*4882a593Smuzhiyun  * Tunable constants
29*4882a593Smuzhiyun  */
30*4882a593Smuzhiyun #define ENDIO_HOOK_POOL_SIZE 1024
31*4882a593Smuzhiyun #define MAPPING_POOL_SIZE 1024
32*4882a593Smuzhiyun #define COMMIT_PERIOD HZ
33*4882a593Smuzhiyun #define NO_SPACE_TIMEOUT_SECS 60
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun static unsigned no_space_timeout_secs = NO_SPACE_TIMEOUT_SECS;
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(snapshot_copy_throttle,
38*4882a593Smuzhiyun 		"A percentage of time allocated for copy on write");
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun /*
41*4882a593Smuzhiyun  * The block size of the device holding pool data must be
42*4882a593Smuzhiyun  * between 64KB and 1GB.
43*4882a593Smuzhiyun  */
44*4882a593Smuzhiyun #define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (64 * 1024 >> SECTOR_SHIFT)
45*4882a593Smuzhiyun #define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun /*
48*4882a593Smuzhiyun  * Device id is restricted to 24 bits.
49*4882a593Smuzhiyun  */
50*4882a593Smuzhiyun #define MAX_DEV_ID ((1 << 24) - 1)
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun /*
53*4882a593Smuzhiyun  * How do we handle breaking sharing of data blocks?
54*4882a593Smuzhiyun  * =================================================
55*4882a593Smuzhiyun  *
56*4882a593Smuzhiyun  * We use a standard copy-on-write btree to store the mappings for the
57*4882a593Smuzhiyun  * devices (note I'm talking about copy-on-write of the metadata here, not
58*4882a593Smuzhiyun  * the data).  When you take an internal snapshot you clone the root node
59*4882a593Smuzhiyun  * of the origin btree.  After this there is no concept of an origin or a
60*4882a593Smuzhiyun  * snapshot.  They are just two device trees that happen to point to the
61*4882a593Smuzhiyun  * same data blocks.
62*4882a593Smuzhiyun  *
63*4882a593Smuzhiyun  * When we get a write in we decide if it's to a shared data block using
64*4882a593Smuzhiyun  * some timestamp magic.  If it is, we have to break sharing.
65*4882a593Smuzhiyun  *
66*4882a593Smuzhiyun  * Let's say we write to a shared block in what was the origin.  The
67*4882a593Smuzhiyun  * steps are:
68*4882a593Smuzhiyun  *
69*4882a593Smuzhiyun  * i) plug io further to this physical block. (see bio_prison code).
70*4882a593Smuzhiyun  *
71*4882a593Smuzhiyun  * ii) quiesce any read io to that shared data block.  Obviously
72*4882a593Smuzhiyun  * including all devices that share this block.  (see dm_deferred_set code)
73*4882a593Smuzhiyun  *
74*4882a593Smuzhiyun  * iii) copy the data block to a newly allocate block.  This step can be
75*4882a593Smuzhiyun  * missed out if the io covers the block. (schedule_copy).
76*4882a593Smuzhiyun  *
77*4882a593Smuzhiyun  * iv) insert the new mapping into the origin's btree
78*4882a593Smuzhiyun  * (process_prepared_mapping).  This act of inserting breaks some
79*4882a593Smuzhiyun  * sharing of btree nodes between the two devices.  Breaking sharing only
80*4882a593Smuzhiyun  * effects the btree of that specific device.  Btrees for the other
81*4882a593Smuzhiyun  * devices that share the block never change.  The btree for the origin
82*4882a593Smuzhiyun  * device as it was after the last commit is untouched, ie. we're using
83*4882a593Smuzhiyun  * persistent data structures in the functional programming sense.
84*4882a593Smuzhiyun  *
85*4882a593Smuzhiyun  * v) unplug io to this physical block, including the io that triggered
86*4882a593Smuzhiyun  * the breaking of sharing.
87*4882a593Smuzhiyun  *
88*4882a593Smuzhiyun  * Steps (ii) and (iii) occur in parallel.
89*4882a593Smuzhiyun  *
90*4882a593Smuzhiyun  * The metadata _doesn't_ need to be committed before the io continues.  We
91*4882a593Smuzhiyun  * get away with this because the io is always written to a _new_ block.
92*4882a593Smuzhiyun  * If there's a crash, then:
93*4882a593Smuzhiyun  *
94*4882a593Smuzhiyun  * - The origin mapping will point to the old origin block (the shared
95*4882a593Smuzhiyun  * one).  This will contain the data as it was before the io that triggered
96*4882a593Smuzhiyun  * the breaking of sharing came in.
97*4882a593Smuzhiyun  *
98*4882a593Smuzhiyun  * - The snap mapping still points to the old block.  As it would after
99*4882a593Smuzhiyun  * the commit.
100*4882a593Smuzhiyun  *
101*4882a593Smuzhiyun  * The downside of this scheme is the timestamp magic isn't perfect, and
102*4882a593Smuzhiyun  * will continue to think that data block in the snapshot device is shared
103*4882a593Smuzhiyun  * even after the write to the origin has broken sharing.  I suspect data
104*4882a593Smuzhiyun  * blocks will typically be shared by many different devices, so we're
105*4882a593Smuzhiyun  * breaking sharing n + 1 times, rather than n, where n is the number of
106*4882a593Smuzhiyun  * devices that reference this data block.  At the moment I think the
107*4882a593Smuzhiyun  * benefits far, far outweigh the disadvantages.
108*4882a593Smuzhiyun  */
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun /*----------------------------------------------------------------*/
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun /*
113*4882a593Smuzhiyun  * Key building.
114*4882a593Smuzhiyun  */
115*4882a593Smuzhiyun enum lock_space {
116*4882a593Smuzhiyun 	VIRTUAL,
117*4882a593Smuzhiyun 	PHYSICAL
118*4882a593Smuzhiyun };
119*4882a593Smuzhiyun 
build_key(struct dm_thin_device * td,enum lock_space ls,dm_block_t b,dm_block_t e,struct dm_cell_key * key)120*4882a593Smuzhiyun static void build_key(struct dm_thin_device *td, enum lock_space ls,
121*4882a593Smuzhiyun 		      dm_block_t b, dm_block_t e, struct dm_cell_key *key)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun 	key->virtual = (ls == VIRTUAL);
124*4882a593Smuzhiyun 	key->dev = dm_thin_dev_id(td);
125*4882a593Smuzhiyun 	key->block_begin = b;
126*4882a593Smuzhiyun 	key->block_end = e;
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun 
build_data_key(struct dm_thin_device * td,dm_block_t b,struct dm_cell_key * key)129*4882a593Smuzhiyun static void build_data_key(struct dm_thin_device *td, dm_block_t b,
130*4882a593Smuzhiyun 			   struct dm_cell_key *key)
131*4882a593Smuzhiyun {
132*4882a593Smuzhiyun 	build_key(td, PHYSICAL, b, b + 1llu, key);
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun 
build_virtual_key(struct dm_thin_device * td,dm_block_t b,struct dm_cell_key * key)135*4882a593Smuzhiyun static void build_virtual_key(struct dm_thin_device *td, dm_block_t b,
136*4882a593Smuzhiyun 			      struct dm_cell_key *key)
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun 	build_key(td, VIRTUAL, b, b + 1llu, key);
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun /*----------------------------------------------------------------*/
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun #define THROTTLE_THRESHOLD (1 * HZ)
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun struct throttle {
146*4882a593Smuzhiyun 	struct rw_semaphore lock;
147*4882a593Smuzhiyun 	unsigned long threshold;
148*4882a593Smuzhiyun 	bool throttle_applied;
149*4882a593Smuzhiyun };
150*4882a593Smuzhiyun 
throttle_init(struct throttle * t)151*4882a593Smuzhiyun static void throttle_init(struct throttle *t)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun 	init_rwsem(&t->lock);
154*4882a593Smuzhiyun 	t->throttle_applied = false;
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun 
throttle_work_start(struct throttle * t)157*4882a593Smuzhiyun static void throttle_work_start(struct throttle *t)
158*4882a593Smuzhiyun {
159*4882a593Smuzhiyun 	t->threshold = jiffies + THROTTLE_THRESHOLD;
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun 
throttle_work_update(struct throttle * t)162*4882a593Smuzhiyun static void throttle_work_update(struct throttle *t)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun 	if (!t->throttle_applied && jiffies > t->threshold) {
165*4882a593Smuzhiyun 		down_write(&t->lock);
166*4882a593Smuzhiyun 		t->throttle_applied = true;
167*4882a593Smuzhiyun 	}
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun 
throttle_work_complete(struct throttle * t)170*4882a593Smuzhiyun static void throttle_work_complete(struct throttle *t)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun 	if (t->throttle_applied) {
173*4882a593Smuzhiyun 		t->throttle_applied = false;
174*4882a593Smuzhiyun 		up_write(&t->lock);
175*4882a593Smuzhiyun 	}
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun 
throttle_lock(struct throttle * t)178*4882a593Smuzhiyun static void throttle_lock(struct throttle *t)
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun 	down_read(&t->lock);
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun 
throttle_unlock(struct throttle * t)183*4882a593Smuzhiyun static void throttle_unlock(struct throttle *t)
184*4882a593Smuzhiyun {
185*4882a593Smuzhiyun 	up_read(&t->lock);
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun /*----------------------------------------------------------------*/
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun /*
191*4882a593Smuzhiyun  * A pool device ties together a metadata device and a data device.  It
192*4882a593Smuzhiyun  * also provides the interface for creating and destroying internal
193*4882a593Smuzhiyun  * devices.
194*4882a593Smuzhiyun  */
195*4882a593Smuzhiyun struct dm_thin_new_mapping;
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun /*
198*4882a593Smuzhiyun  * The pool runs in various modes.  Ordered in degraded order for comparisons.
199*4882a593Smuzhiyun  */
200*4882a593Smuzhiyun enum pool_mode {
201*4882a593Smuzhiyun 	PM_WRITE,		/* metadata may be changed */
202*4882a593Smuzhiyun 	PM_OUT_OF_DATA_SPACE,	/* metadata may be changed, though data may not be allocated */
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	/*
205*4882a593Smuzhiyun 	 * Like READ_ONLY, except may switch back to WRITE on metadata resize. Reported as READ_ONLY.
206*4882a593Smuzhiyun 	 */
207*4882a593Smuzhiyun 	PM_OUT_OF_METADATA_SPACE,
208*4882a593Smuzhiyun 	PM_READ_ONLY,		/* metadata may not be changed */
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	PM_FAIL,		/* all I/O fails */
211*4882a593Smuzhiyun };
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun struct pool_features {
214*4882a593Smuzhiyun 	enum pool_mode mode;
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	bool zero_new_blocks:1;
217*4882a593Smuzhiyun 	bool discard_enabled:1;
218*4882a593Smuzhiyun 	bool discard_passdown:1;
219*4882a593Smuzhiyun 	bool error_if_no_space:1;
220*4882a593Smuzhiyun };
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun struct thin_c;
223*4882a593Smuzhiyun typedef void (*process_bio_fn)(struct thin_c *tc, struct bio *bio);
224*4882a593Smuzhiyun typedef void (*process_cell_fn)(struct thin_c *tc, struct dm_bio_prison_cell *cell);
225*4882a593Smuzhiyun typedef void (*process_mapping_fn)(struct dm_thin_new_mapping *m);
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun #define CELL_SORT_ARRAY_SIZE 8192
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun struct pool {
230*4882a593Smuzhiyun 	struct list_head list;
231*4882a593Smuzhiyun 	struct dm_target *ti;	/* Only set if a pool target is bound */
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	struct mapped_device *pool_md;
234*4882a593Smuzhiyun 	struct block_device *data_dev;
235*4882a593Smuzhiyun 	struct block_device *md_dev;
236*4882a593Smuzhiyun 	struct dm_pool_metadata *pmd;
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	dm_block_t low_water_blocks;
239*4882a593Smuzhiyun 	uint32_t sectors_per_block;
240*4882a593Smuzhiyun 	int sectors_per_block_shift;
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	struct pool_features pf;
243*4882a593Smuzhiyun 	bool low_water_triggered:1;	/* A dm event has been sent */
244*4882a593Smuzhiyun 	bool suspended:1;
245*4882a593Smuzhiyun 	bool out_of_data_space:1;
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	struct dm_bio_prison *prison;
248*4882a593Smuzhiyun 	struct dm_kcopyd_client *copier;
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	struct work_struct worker;
251*4882a593Smuzhiyun 	struct workqueue_struct *wq;
252*4882a593Smuzhiyun 	struct throttle throttle;
253*4882a593Smuzhiyun 	struct delayed_work waker;
254*4882a593Smuzhiyun 	struct delayed_work no_space_timeout;
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 	unsigned long last_commit_jiffies;
257*4882a593Smuzhiyun 	unsigned ref_count;
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	spinlock_t lock;
260*4882a593Smuzhiyun 	struct bio_list deferred_flush_bios;
261*4882a593Smuzhiyun 	struct bio_list deferred_flush_completions;
262*4882a593Smuzhiyun 	struct list_head prepared_mappings;
263*4882a593Smuzhiyun 	struct list_head prepared_discards;
264*4882a593Smuzhiyun 	struct list_head prepared_discards_pt2;
265*4882a593Smuzhiyun 	struct list_head active_thins;
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	struct dm_deferred_set *shared_read_ds;
268*4882a593Smuzhiyun 	struct dm_deferred_set *all_io_ds;
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	struct dm_thin_new_mapping *next_mapping;
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	process_bio_fn process_bio;
273*4882a593Smuzhiyun 	process_bio_fn process_discard;
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 	process_cell_fn process_cell;
276*4882a593Smuzhiyun 	process_cell_fn process_discard_cell;
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	process_mapping_fn process_prepared_mapping;
279*4882a593Smuzhiyun 	process_mapping_fn process_prepared_discard;
280*4882a593Smuzhiyun 	process_mapping_fn process_prepared_discard_pt2;
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	struct dm_bio_prison_cell **cell_sort_array;
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	mempool_t mapping_pool;
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 	struct bio flush_bio;
287*4882a593Smuzhiyun };
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun static void metadata_operation_failed(struct pool *pool, const char *op, int r);
290*4882a593Smuzhiyun 
get_pool_mode(struct pool * pool)291*4882a593Smuzhiyun static enum pool_mode get_pool_mode(struct pool *pool)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun 	return pool->pf.mode;
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun 
notify_of_pool_mode_change(struct pool * pool)296*4882a593Smuzhiyun static void notify_of_pool_mode_change(struct pool *pool)
297*4882a593Smuzhiyun {
298*4882a593Smuzhiyun 	const char *descs[] = {
299*4882a593Smuzhiyun 		"write",
300*4882a593Smuzhiyun 		"out-of-data-space",
301*4882a593Smuzhiyun 		"read-only",
302*4882a593Smuzhiyun 		"read-only",
303*4882a593Smuzhiyun 		"fail"
304*4882a593Smuzhiyun 	};
305*4882a593Smuzhiyun 	const char *extra_desc = NULL;
306*4882a593Smuzhiyun 	enum pool_mode mode = get_pool_mode(pool);
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	if (mode == PM_OUT_OF_DATA_SPACE) {
309*4882a593Smuzhiyun 		if (!pool->pf.error_if_no_space)
310*4882a593Smuzhiyun 			extra_desc = " (queue IO)";
311*4882a593Smuzhiyun 		else
312*4882a593Smuzhiyun 			extra_desc = " (error IO)";
313*4882a593Smuzhiyun 	}
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 	dm_table_event(pool->ti->table);
316*4882a593Smuzhiyun 	DMINFO("%s: switching pool to %s%s mode",
317*4882a593Smuzhiyun 	       dm_device_name(pool->pool_md),
318*4882a593Smuzhiyun 	       descs[(int)mode], extra_desc ? : "");
319*4882a593Smuzhiyun }
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun /*
322*4882a593Smuzhiyun  * Target context for a pool.
323*4882a593Smuzhiyun  */
324*4882a593Smuzhiyun struct pool_c {
325*4882a593Smuzhiyun 	struct dm_target *ti;
326*4882a593Smuzhiyun 	struct pool *pool;
327*4882a593Smuzhiyun 	struct dm_dev *data_dev;
328*4882a593Smuzhiyun 	struct dm_dev *metadata_dev;
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	dm_block_t low_water_blocks;
331*4882a593Smuzhiyun 	struct pool_features requested_pf; /* Features requested during table load */
332*4882a593Smuzhiyun 	struct pool_features adjusted_pf;  /* Features used after adjusting for constituent devices */
333*4882a593Smuzhiyun };
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun /*
336*4882a593Smuzhiyun  * Target context for a thin.
337*4882a593Smuzhiyun  */
338*4882a593Smuzhiyun struct thin_c {
339*4882a593Smuzhiyun 	struct list_head list;
340*4882a593Smuzhiyun 	struct dm_dev *pool_dev;
341*4882a593Smuzhiyun 	struct dm_dev *origin_dev;
342*4882a593Smuzhiyun 	sector_t origin_size;
343*4882a593Smuzhiyun 	dm_thin_id dev_id;
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 	struct pool *pool;
346*4882a593Smuzhiyun 	struct dm_thin_device *td;
347*4882a593Smuzhiyun 	struct mapped_device *thin_md;
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun 	bool requeue_mode:1;
350*4882a593Smuzhiyun 	spinlock_t lock;
351*4882a593Smuzhiyun 	struct list_head deferred_cells;
352*4882a593Smuzhiyun 	struct bio_list deferred_bio_list;
353*4882a593Smuzhiyun 	struct bio_list retry_on_resume_list;
354*4882a593Smuzhiyun 	struct rb_root sort_bio_list; /* sorted list of deferred bios */
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	/*
357*4882a593Smuzhiyun 	 * Ensures the thin is not destroyed until the worker has finished
358*4882a593Smuzhiyun 	 * iterating the active_thins list.
359*4882a593Smuzhiyun 	 */
360*4882a593Smuzhiyun 	refcount_t refcount;
361*4882a593Smuzhiyun 	struct completion can_destroy;
362*4882a593Smuzhiyun };
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun /*----------------------------------------------------------------*/
365*4882a593Smuzhiyun 
block_size_is_power_of_two(struct pool * pool)366*4882a593Smuzhiyun static bool block_size_is_power_of_two(struct pool *pool)
367*4882a593Smuzhiyun {
368*4882a593Smuzhiyun 	return pool->sectors_per_block_shift >= 0;
369*4882a593Smuzhiyun }
370*4882a593Smuzhiyun 
block_to_sectors(struct pool * pool,dm_block_t b)371*4882a593Smuzhiyun static sector_t block_to_sectors(struct pool *pool, dm_block_t b)
372*4882a593Smuzhiyun {
373*4882a593Smuzhiyun 	return block_size_is_power_of_two(pool) ?
374*4882a593Smuzhiyun 		(b << pool->sectors_per_block_shift) :
375*4882a593Smuzhiyun 		(b * pool->sectors_per_block);
376*4882a593Smuzhiyun }
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun /*----------------------------------------------------------------*/
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun struct discard_op {
381*4882a593Smuzhiyun 	struct thin_c *tc;
382*4882a593Smuzhiyun 	struct blk_plug plug;
383*4882a593Smuzhiyun 	struct bio *parent_bio;
384*4882a593Smuzhiyun 	struct bio *bio;
385*4882a593Smuzhiyun };
386*4882a593Smuzhiyun 
begin_discard(struct discard_op * op,struct thin_c * tc,struct bio * parent)387*4882a593Smuzhiyun static void begin_discard(struct discard_op *op, struct thin_c *tc, struct bio *parent)
388*4882a593Smuzhiyun {
389*4882a593Smuzhiyun 	BUG_ON(!parent);
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 	op->tc = tc;
392*4882a593Smuzhiyun 	blk_start_plug(&op->plug);
393*4882a593Smuzhiyun 	op->parent_bio = parent;
394*4882a593Smuzhiyun 	op->bio = NULL;
395*4882a593Smuzhiyun }
396*4882a593Smuzhiyun 
issue_discard(struct discard_op * op,dm_block_t data_b,dm_block_t data_e)397*4882a593Smuzhiyun static int issue_discard(struct discard_op *op, dm_block_t data_b, dm_block_t data_e)
398*4882a593Smuzhiyun {
399*4882a593Smuzhiyun 	struct thin_c *tc = op->tc;
400*4882a593Smuzhiyun 	sector_t s = block_to_sectors(tc->pool, data_b);
401*4882a593Smuzhiyun 	sector_t len = block_to_sectors(tc->pool, data_e - data_b);
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun 	return __blkdev_issue_discard(tc->pool_dev->bdev, s, len,
404*4882a593Smuzhiyun 				      GFP_NOWAIT, 0, &op->bio);
405*4882a593Smuzhiyun }
406*4882a593Smuzhiyun 
end_discard(struct discard_op * op,int r)407*4882a593Smuzhiyun static void end_discard(struct discard_op *op, int r)
408*4882a593Smuzhiyun {
409*4882a593Smuzhiyun 	if (op->bio) {
410*4882a593Smuzhiyun 		/*
411*4882a593Smuzhiyun 		 * Even if one of the calls to issue_discard failed, we
412*4882a593Smuzhiyun 		 * need to wait for the chain to complete.
413*4882a593Smuzhiyun 		 */
414*4882a593Smuzhiyun 		bio_chain(op->bio, op->parent_bio);
415*4882a593Smuzhiyun 		bio_set_op_attrs(op->bio, REQ_OP_DISCARD, 0);
416*4882a593Smuzhiyun 		submit_bio(op->bio);
417*4882a593Smuzhiyun 	}
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun 	blk_finish_plug(&op->plug);
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun 	/*
422*4882a593Smuzhiyun 	 * Even if r is set, there could be sub discards in flight that we
423*4882a593Smuzhiyun 	 * need to wait for.
424*4882a593Smuzhiyun 	 */
425*4882a593Smuzhiyun 	if (r && !op->parent_bio->bi_status)
426*4882a593Smuzhiyun 		op->parent_bio->bi_status = errno_to_blk_status(r);
427*4882a593Smuzhiyun 	bio_endio(op->parent_bio);
428*4882a593Smuzhiyun }
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun /*----------------------------------------------------------------*/
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun /*
433*4882a593Smuzhiyun  * wake_worker() is used when new work is queued and when pool_resume is
434*4882a593Smuzhiyun  * ready to continue deferred IO processing.
435*4882a593Smuzhiyun  */
wake_worker(struct pool * pool)436*4882a593Smuzhiyun static void wake_worker(struct pool *pool)
437*4882a593Smuzhiyun {
438*4882a593Smuzhiyun 	queue_work(pool->wq, &pool->worker);
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun /*----------------------------------------------------------------*/
442*4882a593Smuzhiyun 
bio_detain(struct pool * pool,struct dm_cell_key * key,struct bio * bio,struct dm_bio_prison_cell ** cell_result)443*4882a593Smuzhiyun static int bio_detain(struct pool *pool, struct dm_cell_key *key, struct bio *bio,
444*4882a593Smuzhiyun 		      struct dm_bio_prison_cell **cell_result)
445*4882a593Smuzhiyun {
446*4882a593Smuzhiyun 	int r;
447*4882a593Smuzhiyun 	struct dm_bio_prison_cell *cell_prealloc;
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun 	/*
450*4882a593Smuzhiyun 	 * Allocate a cell from the prison's mempool.
451*4882a593Smuzhiyun 	 * This might block but it can't fail.
452*4882a593Smuzhiyun 	 */
453*4882a593Smuzhiyun 	cell_prealloc = dm_bio_prison_alloc_cell(pool->prison, GFP_NOIO);
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun 	r = dm_bio_detain(pool->prison, key, bio, cell_prealloc, cell_result);
456*4882a593Smuzhiyun 	if (r)
457*4882a593Smuzhiyun 		/*
458*4882a593Smuzhiyun 		 * We reused an old cell; we can get rid of
459*4882a593Smuzhiyun 		 * the new one.
460*4882a593Smuzhiyun 		 */
461*4882a593Smuzhiyun 		dm_bio_prison_free_cell(pool->prison, cell_prealloc);
462*4882a593Smuzhiyun 
463*4882a593Smuzhiyun 	return r;
464*4882a593Smuzhiyun }
465*4882a593Smuzhiyun 
cell_release(struct pool * pool,struct dm_bio_prison_cell * cell,struct bio_list * bios)466*4882a593Smuzhiyun static void cell_release(struct pool *pool,
467*4882a593Smuzhiyun 			 struct dm_bio_prison_cell *cell,
468*4882a593Smuzhiyun 			 struct bio_list *bios)
469*4882a593Smuzhiyun {
470*4882a593Smuzhiyun 	dm_cell_release(pool->prison, cell, bios);
471*4882a593Smuzhiyun 	dm_bio_prison_free_cell(pool->prison, cell);
472*4882a593Smuzhiyun }
473*4882a593Smuzhiyun 
cell_visit_release(struct pool * pool,void (* fn)(void *,struct dm_bio_prison_cell *),void * context,struct dm_bio_prison_cell * cell)474*4882a593Smuzhiyun static void cell_visit_release(struct pool *pool,
475*4882a593Smuzhiyun 			       void (*fn)(void *, struct dm_bio_prison_cell *),
476*4882a593Smuzhiyun 			       void *context,
477*4882a593Smuzhiyun 			       struct dm_bio_prison_cell *cell)
478*4882a593Smuzhiyun {
479*4882a593Smuzhiyun 	dm_cell_visit_release(pool->prison, fn, context, cell);
480*4882a593Smuzhiyun 	dm_bio_prison_free_cell(pool->prison, cell);
481*4882a593Smuzhiyun }
482*4882a593Smuzhiyun 
cell_release_no_holder(struct pool * pool,struct dm_bio_prison_cell * cell,struct bio_list * bios)483*4882a593Smuzhiyun static void cell_release_no_holder(struct pool *pool,
484*4882a593Smuzhiyun 				   struct dm_bio_prison_cell *cell,
485*4882a593Smuzhiyun 				   struct bio_list *bios)
486*4882a593Smuzhiyun {
487*4882a593Smuzhiyun 	dm_cell_release_no_holder(pool->prison, cell, bios);
488*4882a593Smuzhiyun 	dm_bio_prison_free_cell(pool->prison, cell);
489*4882a593Smuzhiyun }
490*4882a593Smuzhiyun 
cell_error_with_code(struct pool * pool,struct dm_bio_prison_cell * cell,blk_status_t error_code)491*4882a593Smuzhiyun static void cell_error_with_code(struct pool *pool,
492*4882a593Smuzhiyun 		struct dm_bio_prison_cell *cell, blk_status_t error_code)
493*4882a593Smuzhiyun {
494*4882a593Smuzhiyun 	dm_cell_error(pool->prison, cell, error_code);
495*4882a593Smuzhiyun 	dm_bio_prison_free_cell(pool->prison, cell);
496*4882a593Smuzhiyun }
497*4882a593Smuzhiyun 
get_pool_io_error_code(struct pool * pool)498*4882a593Smuzhiyun static blk_status_t get_pool_io_error_code(struct pool *pool)
499*4882a593Smuzhiyun {
500*4882a593Smuzhiyun 	return pool->out_of_data_space ? BLK_STS_NOSPC : BLK_STS_IOERR;
501*4882a593Smuzhiyun }
502*4882a593Smuzhiyun 
cell_error(struct pool * pool,struct dm_bio_prison_cell * cell)503*4882a593Smuzhiyun static void cell_error(struct pool *pool, struct dm_bio_prison_cell *cell)
504*4882a593Smuzhiyun {
505*4882a593Smuzhiyun 	cell_error_with_code(pool, cell, get_pool_io_error_code(pool));
506*4882a593Smuzhiyun }
507*4882a593Smuzhiyun 
cell_success(struct pool * pool,struct dm_bio_prison_cell * cell)508*4882a593Smuzhiyun static void cell_success(struct pool *pool, struct dm_bio_prison_cell *cell)
509*4882a593Smuzhiyun {
510*4882a593Smuzhiyun 	cell_error_with_code(pool, cell, 0);
511*4882a593Smuzhiyun }
512*4882a593Smuzhiyun 
cell_requeue(struct pool * pool,struct dm_bio_prison_cell * cell)513*4882a593Smuzhiyun static void cell_requeue(struct pool *pool, struct dm_bio_prison_cell *cell)
514*4882a593Smuzhiyun {
515*4882a593Smuzhiyun 	cell_error_with_code(pool, cell, BLK_STS_DM_REQUEUE);
516*4882a593Smuzhiyun }
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun /*----------------------------------------------------------------*/
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun /*
521*4882a593Smuzhiyun  * A global list of pools that uses a struct mapped_device as a key.
522*4882a593Smuzhiyun  */
523*4882a593Smuzhiyun static struct dm_thin_pool_table {
524*4882a593Smuzhiyun 	struct mutex mutex;
525*4882a593Smuzhiyun 	struct list_head pools;
526*4882a593Smuzhiyun } dm_thin_pool_table;
527*4882a593Smuzhiyun 
pool_table_init(void)528*4882a593Smuzhiyun static void pool_table_init(void)
529*4882a593Smuzhiyun {
530*4882a593Smuzhiyun 	mutex_init(&dm_thin_pool_table.mutex);
531*4882a593Smuzhiyun 	INIT_LIST_HEAD(&dm_thin_pool_table.pools);
532*4882a593Smuzhiyun }
533*4882a593Smuzhiyun 
pool_table_exit(void)534*4882a593Smuzhiyun static void pool_table_exit(void)
535*4882a593Smuzhiyun {
536*4882a593Smuzhiyun 	mutex_destroy(&dm_thin_pool_table.mutex);
537*4882a593Smuzhiyun }
538*4882a593Smuzhiyun 
__pool_table_insert(struct pool * pool)539*4882a593Smuzhiyun static void __pool_table_insert(struct pool *pool)
540*4882a593Smuzhiyun {
541*4882a593Smuzhiyun 	BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
542*4882a593Smuzhiyun 	list_add(&pool->list, &dm_thin_pool_table.pools);
543*4882a593Smuzhiyun }
544*4882a593Smuzhiyun 
__pool_table_remove(struct pool * pool)545*4882a593Smuzhiyun static void __pool_table_remove(struct pool *pool)
546*4882a593Smuzhiyun {
547*4882a593Smuzhiyun 	BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
548*4882a593Smuzhiyun 	list_del(&pool->list);
549*4882a593Smuzhiyun }
550*4882a593Smuzhiyun 
__pool_table_lookup(struct mapped_device * md)551*4882a593Smuzhiyun static struct pool *__pool_table_lookup(struct mapped_device *md)
552*4882a593Smuzhiyun {
553*4882a593Smuzhiyun 	struct pool *pool = NULL, *tmp;
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun 	BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 	list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
558*4882a593Smuzhiyun 		if (tmp->pool_md == md) {
559*4882a593Smuzhiyun 			pool = tmp;
560*4882a593Smuzhiyun 			break;
561*4882a593Smuzhiyun 		}
562*4882a593Smuzhiyun 	}
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun 	return pool;
565*4882a593Smuzhiyun }
566*4882a593Smuzhiyun 
__pool_table_lookup_metadata_dev(struct block_device * md_dev)567*4882a593Smuzhiyun static struct pool *__pool_table_lookup_metadata_dev(struct block_device *md_dev)
568*4882a593Smuzhiyun {
569*4882a593Smuzhiyun 	struct pool *pool = NULL, *tmp;
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun 	BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun 	list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
574*4882a593Smuzhiyun 		if (tmp->md_dev == md_dev) {
575*4882a593Smuzhiyun 			pool = tmp;
576*4882a593Smuzhiyun 			break;
577*4882a593Smuzhiyun 		}
578*4882a593Smuzhiyun 	}
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun 	return pool;
581*4882a593Smuzhiyun }
582*4882a593Smuzhiyun 
583*4882a593Smuzhiyun /*----------------------------------------------------------------*/
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun struct dm_thin_endio_hook {
586*4882a593Smuzhiyun 	struct thin_c *tc;
587*4882a593Smuzhiyun 	struct dm_deferred_entry *shared_read_entry;
588*4882a593Smuzhiyun 	struct dm_deferred_entry *all_io_entry;
589*4882a593Smuzhiyun 	struct dm_thin_new_mapping *overwrite_mapping;
590*4882a593Smuzhiyun 	struct rb_node rb_node;
591*4882a593Smuzhiyun 	struct dm_bio_prison_cell *cell;
592*4882a593Smuzhiyun };
593*4882a593Smuzhiyun 
__merge_bio_list(struct bio_list * bios,struct bio_list * master)594*4882a593Smuzhiyun static void __merge_bio_list(struct bio_list *bios, struct bio_list *master)
595*4882a593Smuzhiyun {
596*4882a593Smuzhiyun 	bio_list_merge(bios, master);
597*4882a593Smuzhiyun 	bio_list_init(master);
598*4882a593Smuzhiyun }
599*4882a593Smuzhiyun 
error_bio_list(struct bio_list * bios,blk_status_t error)600*4882a593Smuzhiyun static void error_bio_list(struct bio_list *bios, blk_status_t error)
601*4882a593Smuzhiyun {
602*4882a593Smuzhiyun 	struct bio *bio;
603*4882a593Smuzhiyun 
604*4882a593Smuzhiyun 	while ((bio = bio_list_pop(bios))) {
605*4882a593Smuzhiyun 		bio->bi_status = error;
606*4882a593Smuzhiyun 		bio_endio(bio);
607*4882a593Smuzhiyun 	}
608*4882a593Smuzhiyun }
609*4882a593Smuzhiyun 
error_thin_bio_list(struct thin_c * tc,struct bio_list * master,blk_status_t error)610*4882a593Smuzhiyun static void error_thin_bio_list(struct thin_c *tc, struct bio_list *master,
611*4882a593Smuzhiyun 		blk_status_t error)
612*4882a593Smuzhiyun {
613*4882a593Smuzhiyun 	struct bio_list bios;
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun 	bio_list_init(&bios);
616*4882a593Smuzhiyun 
617*4882a593Smuzhiyun 	spin_lock_irq(&tc->lock);
618*4882a593Smuzhiyun 	__merge_bio_list(&bios, master);
619*4882a593Smuzhiyun 	spin_unlock_irq(&tc->lock);
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun 	error_bio_list(&bios, error);
622*4882a593Smuzhiyun }
623*4882a593Smuzhiyun 
requeue_deferred_cells(struct thin_c * tc)624*4882a593Smuzhiyun static void requeue_deferred_cells(struct thin_c *tc)
625*4882a593Smuzhiyun {
626*4882a593Smuzhiyun 	struct pool *pool = tc->pool;
627*4882a593Smuzhiyun 	struct list_head cells;
628*4882a593Smuzhiyun 	struct dm_bio_prison_cell *cell, *tmp;
629*4882a593Smuzhiyun 
630*4882a593Smuzhiyun 	INIT_LIST_HEAD(&cells);
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun 	spin_lock_irq(&tc->lock);
633*4882a593Smuzhiyun 	list_splice_init(&tc->deferred_cells, &cells);
634*4882a593Smuzhiyun 	spin_unlock_irq(&tc->lock);
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun 	list_for_each_entry_safe(cell, tmp, &cells, user_list)
637*4882a593Smuzhiyun 		cell_requeue(pool, cell);
638*4882a593Smuzhiyun }
639*4882a593Smuzhiyun 
requeue_io(struct thin_c * tc)640*4882a593Smuzhiyun static void requeue_io(struct thin_c *tc)
641*4882a593Smuzhiyun {
642*4882a593Smuzhiyun 	struct bio_list bios;
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun 	bio_list_init(&bios);
645*4882a593Smuzhiyun 
646*4882a593Smuzhiyun 	spin_lock_irq(&tc->lock);
647*4882a593Smuzhiyun 	__merge_bio_list(&bios, &tc->deferred_bio_list);
648*4882a593Smuzhiyun 	__merge_bio_list(&bios, &tc->retry_on_resume_list);
649*4882a593Smuzhiyun 	spin_unlock_irq(&tc->lock);
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 	error_bio_list(&bios, BLK_STS_DM_REQUEUE);
652*4882a593Smuzhiyun 	requeue_deferred_cells(tc);
653*4882a593Smuzhiyun }
654*4882a593Smuzhiyun 
error_retry_list_with_code(struct pool * pool,blk_status_t error)655*4882a593Smuzhiyun static void error_retry_list_with_code(struct pool *pool, blk_status_t error)
656*4882a593Smuzhiyun {
657*4882a593Smuzhiyun 	struct thin_c *tc;
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun 	rcu_read_lock();
660*4882a593Smuzhiyun 	list_for_each_entry_rcu(tc, &pool->active_thins, list)
661*4882a593Smuzhiyun 		error_thin_bio_list(tc, &tc->retry_on_resume_list, error);
662*4882a593Smuzhiyun 	rcu_read_unlock();
663*4882a593Smuzhiyun }
664*4882a593Smuzhiyun 
error_retry_list(struct pool * pool)665*4882a593Smuzhiyun static void error_retry_list(struct pool *pool)
666*4882a593Smuzhiyun {
667*4882a593Smuzhiyun 	error_retry_list_with_code(pool, get_pool_io_error_code(pool));
668*4882a593Smuzhiyun }
669*4882a593Smuzhiyun 
670*4882a593Smuzhiyun /*
671*4882a593Smuzhiyun  * This section of code contains the logic for processing a thin device's IO.
672*4882a593Smuzhiyun  * Much of the code depends on pool object resources (lists, workqueues, etc)
673*4882a593Smuzhiyun  * but most is exclusively called from the thin target rather than the thin-pool
674*4882a593Smuzhiyun  * target.
675*4882a593Smuzhiyun  */
676*4882a593Smuzhiyun 
get_bio_block(struct thin_c * tc,struct bio * bio)677*4882a593Smuzhiyun static dm_block_t get_bio_block(struct thin_c *tc, struct bio *bio)
678*4882a593Smuzhiyun {
679*4882a593Smuzhiyun 	struct pool *pool = tc->pool;
680*4882a593Smuzhiyun 	sector_t block_nr = bio->bi_iter.bi_sector;
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun 	if (block_size_is_power_of_two(pool))
683*4882a593Smuzhiyun 		block_nr >>= pool->sectors_per_block_shift;
684*4882a593Smuzhiyun 	else
685*4882a593Smuzhiyun 		(void) sector_div(block_nr, pool->sectors_per_block);
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun 	return block_nr;
688*4882a593Smuzhiyun }
689*4882a593Smuzhiyun 
690*4882a593Smuzhiyun /*
691*4882a593Smuzhiyun  * Returns the _complete_ blocks that this bio covers.
692*4882a593Smuzhiyun  */
get_bio_block_range(struct thin_c * tc,struct bio * bio,dm_block_t * begin,dm_block_t * end)693*4882a593Smuzhiyun static void get_bio_block_range(struct thin_c *tc, struct bio *bio,
694*4882a593Smuzhiyun 				dm_block_t *begin, dm_block_t *end)
695*4882a593Smuzhiyun {
696*4882a593Smuzhiyun 	struct pool *pool = tc->pool;
697*4882a593Smuzhiyun 	sector_t b = bio->bi_iter.bi_sector;
698*4882a593Smuzhiyun 	sector_t e = b + (bio->bi_iter.bi_size >> SECTOR_SHIFT);
699*4882a593Smuzhiyun 
700*4882a593Smuzhiyun 	b += pool->sectors_per_block - 1ull; /* so we round up */
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun 	if (block_size_is_power_of_two(pool)) {
703*4882a593Smuzhiyun 		b >>= pool->sectors_per_block_shift;
704*4882a593Smuzhiyun 		e >>= pool->sectors_per_block_shift;
705*4882a593Smuzhiyun 	} else {
706*4882a593Smuzhiyun 		(void) sector_div(b, pool->sectors_per_block);
707*4882a593Smuzhiyun 		(void) sector_div(e, pool->sectors_per_block);
708*4882a593Smuzhiyun 	}
709*4882a593Smuzhiyun 
710*4882a593Smuzhiyun 	if (e < b)
711*4882a593Smuzhiyun 		/* Can happen if the bio is within a single block. */
712*4882a593Smuzhiyun 		e = b;
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun 	*begin = b;
715*4882a593Smuzhiyun 	*end = e;
716*4882a593Smuzhiyun }
717*4882a593Smuzhiyun 
remap(struct thin_c * tc,struct bio * bio,dm_block_t block)718*4882a593Smuzhiyun static void remap(struct thin_c *tc, struct bio *bio, dm_block_t block)
719*4882a593Smuzhiyun {
720*4882a593Smuzhiyun 	struct pool *pool = tc->pool;
721*4882a593Smuzhiyun 	sector_t bi_sector = bio->bi_iter.bi_sector;
722*4882a593Smuzhiyun 
723*4882a593Smuzhiyun 	bio_set_dev(bio, tc->pool_dev->bdev);
724*4882a593Smuzhiyun 	if (block_size_is_power_of_two(pool))
725*4882a593Smuzhiyun 		bio->bi_iter.bi_sector =
726*4882a593Smuzhiyun 			(block << pool->sectors_per_block_shift) |
727*4882a593Smuzhiyun 			(bi_sector & (pool->sectors_per_block - 1));
728*4882a593Smuzhiyun 	else
729*4882a593Smuzhiyun 		bio->bi_iter.bi_sector = (block * pool->sectors_per_block) +
730*4882a593Smuzhiyun 				 sector_div(bi_sector, pool->sectors_per_block);
731*4882a593Smuzhiyun }
732*4882a593Smuzhiyun 
remap_to_origin(struct thin_c * tc,struct bio * bio)733*4882a593Smuzhiyun static void remap_to_origin(struct thin_c *tc, struct bio *bio)
734*4882a593Smuzhiyun {
735*4882a593Smuzhiyun 	bio_set_dev(bio, tc->origin_dev->bdev);
736*4882a593Smuzhiyun }
737*4882a593Smuzhiyun 
bio_triggers_commit(struct thin_c * tc,struct bio * bio)738*4882a593Smuzhiyun static int bio_triggers_commit(struct thin_c *tc, struct bio *bio)
739*4882a593Smuzhiyun {
740*4882a593Smuzhiyun 	return op_is_flush(bio->bi_opf) &&
741*4882a593Smuzhiyun 		dm_thin_changed_this_transaction(tc->td);
742*4882a593Smuzhiyun }
743*4882a593Smuzhiyun 
inc_all_io_entry(struct pool * pool,struct bio * bio)744*4882a593Smuzhiyun static void inc_all_io_entry(struct pool *pool, struct bio *bio)
745*4882a593Smuzhiyun {
746*4882a593Smuzhiyun 	struct dm_thin_endio_hook *h;
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun 	if (bio_op(bio) == REQ_OP_DISCARD)
749*4882a593Smuzhiyun 		return;
750*4882a593Smuzhiyun 
751*4882a593Smuzhiyun 	h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
752*4882a593Smuzhiyun 	h->all_io_entry = dm_deferred_entry_inc(pool->all_io_ds);
753*4882a593Smuzhiyun }
754*4882a593Smuzhiyun 
issue(struct thin_c * tc,struct bio * bio)755*4882a593Smuzhiyun static void issue(struct thin_c *tc, struct bio *bio)
756*4882a593Smuzhiyun {
757*4882a593Smuzhiyun 	struct pool *pool = tc->pool;
758*4882a593Smuzhiyun 
759*4882a593Smuzhiyun 	if (!bio_triggers_commit(tc, bio)) {
760*4882a593Smuzhiyun 		submit_bio_noacct(bio);
761*4882a593Smuzhiyun 		return;
762*4882a593Smuzhiyun 	}
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun 	/*
765*4882a593Smuzhiyun 	 * Complete bio with an error if earlier I/O caused changes to
766*4882a593Smuzhiyun 	 * the metadata that can't be committed e.g, due to I/O errors
767*4882a593Smuzhiyun 	 * on the metadata device.
768*4882a593Smuzhiyun 	 */
769*4882a593Smuzhiyun 	if (dm_thin_aborted_changes(tc->td)) {
770*4882a593Smuzhiyun 		bio_io_error(bio);
771*4882a593Smuzhiyun 		return;
772*4882a593Smuzhiyun 	}
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun 	/*
775*4882a593Smuzhiyun 	 * Batch together any bios that trigger commits and then issue a
776*4882a593Smuzhiyun 	 * single commit for them in process_deferred_bios().
777*4882a593Smuzhiyun 	 */
778*4882a593Smuzhiyun 	spin_lock_irq(&pool->lock);
779*4882a593Smuzhiyun 	bio_list_add(&pool->deferred_flush_bios, bio);
780*4882a593Smuzhiyun 	spin_unlock_irq(&pool->lock);
781*4882a593Smuzhiyun }
782*4882a593Smuzhiyun 
remap_to_origin_and_issue(struct thin_c * tc,struct bio * bio)783*4882a593Smuzhiyun static void remap_to_origin_and_issue(struct thin_c *tc, struct bio *bio)
784*4882a593Smuzhiyun {
785*4882a593Smuzhiyun 	remap_to_origin(tc, bio);
786*4882a593Smuzhiyun 	issue(tc, bio);
787*4882a593Smuzhiyun }
788*4882a593Smuzhiyun 
remap_and_issue(struct thin_c * tc,struct bio * bio,dm_block_t block)789*4882a593Smuzhiyun static void remap_and_issue(struct thin_c *tc, struct bio *bio,
790*4882a593Smuzhiyun 			    dm_block_t block)
791*4882a593Smuzhiyun {
792*4882a593Smuzhiyun 	remap(tc, bio, block);
793*4882a593Smuzhiyun 	issue(tc, bio);
794*4882a593Smuzhiyun }
795*4882a593Smuzhiyun 
796*4882a593Smuzhiyun /*----------------------------------------------------------------*/
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun /*
799*4882a593Smuzhiyun  * Bio endio functions.
800*4882a593Smuzhiyun  */
801*4882a593Smuzhiyun struct dm_thin_new_mapping {
802*4882a593Smuzhiyun 	struct list_head list;
803*4882a593Smuzhiyun 
804*4882a593Smuzhiyun 	bool pass_discard:1;
805*4882a593Smuzhiyun 	bool maybe_shared:1;
806*4882a593Smuzhiyun 
807*4882a593Smuzhiyun 	/*
808*4882a593Smuzhiyun 	 * Track quiescing, copying and zeroing preparation actions.  When this
809*4882a593Smuzhiyun 	 * counter hits zero the block is prepared and can be inserted into the
810*4882a593Smuzhiyun 	 * btree.
811*4882a593Smuzhiyun 	 */
812*4882a593Smuzhiyun 	atomic_t prepare_actions;
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun 	blk_status_t status;
815*4882a593Smuzhiyun 	struct thin_c *tc;
816*4882a593Smuzhiyun 	dm_block_t virt_begin, virt_end;
817*4882a593Smuzhiyun 	dm_block_t data_block;
818*4882a593Smuzhiyun 	struct dm_bio_prison_cell *cell;
819*4882a593Smuzhiyun 
820*4882a593Smuzhiyun 	/*
821*4882a593Smuzhiyun 	 * If the bio covers the whole area of a block then we can avoid
822*4882a593Smuzhiyun 	 * zeroing or copying.  Instead this bio is hooked.  The bio will
823*4882a593Smuzhiyun 	 * still be in the cell, so care has to be taken to avoid issuing
824*4882a593Smuzhiyun 	 * the bio twice.
825*4882a593Smuzhiyun 	 */
826*4882a593Smuzhiyun 	struct bio *bio;
827*4882a593Smuzhiyun 	bio_end_io_t *saved_bi_end_io;
828*4882a593Smuzhiyun };
829*4882a593Smuzhiyun 
__complete_mapping_preparation(struct dm_thin_new_mapping * m)830*4882a593Smuzhiyun static void __complete_mapping_preparation(struct dm_thin_new_mapping *m)
831*4882a593Smuzhiyun {
832*4882a593Smuzhiyun 	struct pool *pool = m->tc->pool;
833*4882a593Smuzhiyun 
834*4882a593Smuzhiyun 	if (atomic_dec_and_test(&m->prepare_actions)) {
835*4882a593Smuzhiyun 		list_add_tail(&m->list, &pool->prepared_mappings);
836*4882a593Smuzhiyun 		wake_worker(pool);
837*4882a593Smuzhiyun 	}
838*4882a593Smuzhiyun }
839*4882a593Smuzhiyun 
complete_mapping_preparation(struct dm_thin_new_mapping * m)840*4882a593Smuzhiyun static void complete_mapping_preparation(struct dm_thin_new_mapping *m)
841*4882a593Smuzhiyun {
842*4882a593Smuzhiyun 	unsigned long flags;
843*4882a593Smuzhiyun 	struct pool *pool = m->tc->pool;
844*4882a593Smuzhiyun 
845*4882a593Smuzhiyun 	spin_lock_irqsave(&pool->lock, flags);
846*4882a593Smuzhiyun 	__complete_mapping_preparation(m);
847*4882a593Smuzhiyun 	spin_unlock_irqrestore(&pool->lock, flags);
848*4882a593Smuzhiyun }
849*4882a593Smuzhiyun 
copy_complete(int read_err,unsigned long write_err,void * context)850*4882a593Smuzhiyun static void copy_complete(int read_err, unsigned long write_err, void *context)
851*4882a593Smuzhiyun {
852*4882a593Smuzhiyun 	struct dm_thin_new_mapping *m = context;
853*4882a593Smuzhiyun 
854*4882a593Smuzhiyun 	m->status = read_err || write_err ? BLK_STS_IOERR : 0;
855*4882a593Smuzhiyun 	complete_mapping_preparation(m);
856*4882a593Smuzhiyun }
857*4882a593Smuzhiyun 
overwrite_endio(struct bio * bio)858*4882a593Smuzhiyun static void overwrite_endio(struct bio *bio)
859*4882a593Smuzhiyun {
860*4882a593Smuzhiyun 	struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
861*4882a593Smuzhiyun 	struct dm_thin_new_mapping *m = h->overwrite_mapping;
862*4882a593Smuzhiyun 
863*4882a593Smuzhiyun 	bio->bi_end_io = m->saved_bi_end_io;
864*4882a593Smuzhiyun 
865*4882a593Smuzhiyun 	m->status = bio->bi_status;
866*4882a593Smuzhiyun 	complete_mapping_preparation(m);
867*4882a593Smuzhiyun }
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun /*----------------------------------------------------------------*/
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun /*
872*4882a593Smuzhiyun  * Workqueue.
873*4882a593Smuzhiyun  */
874*4882a593Smuzhiyun 
875*4882a593Smuzhiyun /*
876*4882a593Smuzhiyun  * Prepared mapping jobs.
877*4882a593Smuzhiyun  */
878*4882a593Smuzhiyun 
879*4882a593Smuzhiyun /*
880*4882a593Smuzhiyun  * This sends the bios in the cell, except the original holder, back
881*4882a593Smuzhiyun  * to the deferred_bios list.
882*4882a593Smuzhiyun  */
cell_defer_no_holder(struct thin_c * tc,struct dm_bio_prison_cell * cell)883*4882a593Smuzhiyun static void cell_defer_no_holder(struct thin_c *tc, struct dm_bio_prison_cell *cell)
884*4882a593Smuzhiyun {
885*4882a593Smuzhiyun 	struct pool *pool = tc->pool;
886*4882a593Smuzhiyun 	unsigned long flags;
887*4882a593Smuzhiyun 	int has_work;
888*4882a593Smuzhiyun 
889*4882a593Smuzhiyun 	spin_lock_irqsave(&tc->lock, flags);
890*4882a593Smuzhiyun 	cell_release_no_holder(pool, cell, &tc->deferred_bio_list);
891*4882a593Smuzhiyun 	has_work = !bio_list_empty(&tc->deferred_bio_list);
892*4882a593Smuzhiyun 	spin_unlock_irqrestore(&tc->lock, flags);
893*4882a593Smuzhiyun 
894*4882a593Smuzhiyun 	if (has_work)
895*4882a593Smuzhiyun 		wake_worker(pool);
896*4882a593Smuzhiyun }
897*4882a593Smuzhiyun 
898*4882a593Smuzhiyun static void thin_defer_bio(struct thin_c *tc, struct bio *bio);
899*4882a593Smuzhiyun 
900*4882a593Smuzhiyun struct remap_info {
901*4882a593Smuzhiyun 	struct thin_c *tc;
902*4882a593Smuzhiyun 	struct bio_list defer_bios;
903*4882a593Smuzhiyun 	struct bio_list issue_bios;
904*4882a593Smuzhiyun };
905*4882a593Smuzhiyun 
__inc_remap_and_issue_cell(void * context,struct dm_bio_prison_cell * cell)906*4882a593Smuzhiyun static void __inc_remap_and_issue_cell(void *context,
907*4882a593Smuzhiyun 				       struct dm_bio_prison_cell *cell)
908*4882a593Smuzhiyun {
909*4882a593Smuzhiyun 	struct remap_info *info = context;
910*4882a593Smuzhiyun 	struct bio *bio;
911*4882a593Smuzhiyun 
912*4882a593Smuzhiyun 	while ((bio = bio_list_pop(&cell->bios))) {
913*4882a593Smuzhiyun 		if (op_is_flush(bio->bi_opf) || bio_op(bio) == REQ_OP_DISCARD)
914*4882a593Smuzhiyun 			bio_list_add(&info->defer_bios, bio);
915*4882a593Smuzhiyun 		else {
916*4882a593Smuzhiyun 			inc_all_io_entry(info->tc->pool, bio);
917*4882a593Smuzhiyun 
918*4882a593Smuzhiyun 			/*
919*4882a593Smuzhiyun 			 * We can't issue the bios with the bio prison lock
920*4882a593Smuzhiyun 			 * held, so we add them to a list to issue on
921*4882a593Smuzhiyun 			 * return from this function.
922*4882a593Smuzhiyun 			 */
923*4882a593Smuzhiyun 			bio_list_add(&info->issue_bios, bio);
924*4882a593Smuzhiyun 		}
925*4882a593Smuzhiyun 	}
926*4882a593Smuzhiyun }
927*4882a593Smuzhiyun 
inc_remap_and_issue_cell(struct thin_c * tc,struct dm_bio_prison_cell * cell,dm_block_t block)928*4882a593Smuzhiyun static void inc_remap_and_issue_cell(struct thin_c *tc,
929*4882a593Smuzhiyun 				     struct dm_bio_prison_cell *cell,
930*4882a593Smuzhiyun 				     dm_block_t block)
931*4882a593Smuzhiyun {
932*4882a593Smuzhiyun 	struct bio *bio;
933*4882a593Smuzhiyun 	struct remap_info info;
934*4882a593Smuzhiyun 
935*4882a593Smuzhiyun 	info.tc = tc;
936*4882a593Smuzhiyun 	bio_list_init(&info.defer_bios);
937*4882a593Smuzhiyun 	bio_list_init(&info.issue_bios);
938*4882a593Smuzhiyun 
939*4882a593Smuzhiyun 	/*
940*4882a593Smuzhiyun 	 * We have to be careful to inc any bios we're about to issue
941*4882a593Smuzhiyun 	 * before the cell is released, and avoid a race with new bios
942*4882a593Smuzhiyun 	 * being added to the cell.
943*4882a593Smuzhiyun 	 */
944*4882a593Smuzhiyun 	cell_visit_release(tc->pool, __inc_remap_and_issue_cell,
945*4882a593Smuzhiyun 			   &info, cell);
946*4882a593Smuzhiyun 
947*4882a593Smuzhiyun 	while ((bio = bio_list_pop(&info.defer_bios)))
948*4882a593Smuzhiyun 		thin_defer_bio(tc, bio);
949*4882a593Smuzhiyun 
950*4882a593Smuzhiyun 	while ((bio = bio_list_pop(&info.issue_bios)))
951*4882a593Smuzhiyun 		remap_and_issue(info.tc, bio, block);
952*4882a593Smuzhiyun }
953*4882a593Smuzhiyun 
process_prepared_mapping_fail(struct dm_thin_new_mapping * m)954*4882a593Smuzhiyun static void process_prepared_mapping_fail(struct dm_thin_new_mapping *m)
955*4882a593Smuzhiyun {
956*4882a593Smuzhiyun 	cell_error(m->tc->pool, m->cell);
957*4882a593Smuzhiyun 	list_del(&m->list);
958*4882a593Smuzhiyun 	mempool_free(m, &m->tc->pool->mapping_pool);
959*4882a593Smuzhiyun }
960*4882a593Smuzhiyun 
complete_overwrite_bio(struct thin_c * tc,struct bio * bio)961*4882a593Smuzhiyun static void complete_overwrite_bio(struct thin_c *tc, struct bio *bio)
962*4882a593Smuzhiyun {
963*4882a593Smuzhiyun 	struct pool *pool = tc->pool;
964*4882a593Smuzhiyun 
965*4882a593Smuzhiyun 	/*
966*4882a593Smuzhiyun 	 * If the bio has the REQ_FUA flag set we must commit the metadata
967*4882a593Smuzhiyun 	 * before signaling its completion.
968*4882a593Smuzhiyun 	 */
969*4882a593Smuzhiyun 	if (!bio_triggers_commit(tc, bio)) {
970*4882a593Smuzhiyun 		bio_endio(bio);
971*4882a593Smuzhiyun 		return;
972*4882a593Smuzhiyun 	}
973*4882a593Smuzhiyun 
974*4882a593Smuzhiyun 	/*
975*4882a593Smuzhiyun 	 * Complete bio with an error if earlier I/O caused changes to the
976*4882a593Smuzhiyun 	 * metadata that can't be committed, e.g, due to I/O errors on the
977*4882a593Smuzhiyun 	 * metadata device.
978*4882a593Smuzhiyun 	 */
979*4882a593Smuzhiyun 	if (dm_thin_aborted_changes(tc->td)) {
980*4882a593Smuzhiyun 		bio_io_error(bio);
981*4882a593Smuzhiyun 		return;
982*4882a593Smuzhiyun 	}
983*4882a593Smuzhiyun 
984*4882a593Smuzhiyun 	/*
985*4882a593Smuzhiyun 	 * Batch together any bios that trigger commits and then issue a
986*4882a593Smuzhiyun 	 * single commit for them in process_deferred_bios().
987*4882a593Smuzhiyun 	 */
988*4882a593Smuzhiyun 	spin_lock_irq(&pool->lock);
989*4882a593Smuzhiyun 	bio_list_add(&pool->deferred_flush_completions, bio);
990*4882a593Smuzhiyun 	spin_unlock_irq(&pool->lock);
991*4882a593Smuzhiyun }
992*4882a593Smuzhiyun 
process_prepared_mapping(struct dm_thin_new_mapping * m)993*4882a593Smuzhiyun static void process_prepared_mapping(struct dm_thin_new_mapping *m)
994*4882a593Smuzhiyun {
995*4882a593Smuzhiyun 	struct thin_c *tc = m->tc;
996*4882a593Smuzhiyun 	struct pool *pool = tc->pool;
997*4882a593Smuzhiyun 	struct bio *bio = m->bio;
998*4882a593Smuzhiyun 	int r;
999*4882a593Smuzhiyun 
1000*4882a593Smuzhiyun 	if (m->status) {
1001*4882a593Smuzhiyun 		cell_error(pool, m->cell);
1002*4882a593Smuzhiyun 		goto out;
1003*4882a593Smuzhiyun 	}
1004*4882a593Smuzhiyun 
1005*4882a593Smuzhiyun 	/*
1006*4882a593Smuzhiyun 	 * Commit the prepared block into the mapping btree.
1007*4882a593Smuzhiyun 	 * Any I/O for this block arriving after this point will get
1008*4882a593Smuzhiyun 	 * remapped to it directly.
1009*4882a593Smuzhiyun 	 */
1010*4882a593Smuzhiyun 	r = dm_thin_insert_block(tc->td, m->virt_begin, m->data_block);
1011*4882a593Smuzhiyun 	if (r) {
1012*4882a593Smuzhiyun 		metadata_operation_failed(pool, "dm_thin_insert_block", r);
1013*4882a593Smuzhiyun 		cell_error(pool, m->cell);
1014*4882a593Smuzhiyun 		goto out;
1015*4882a593Smuzhiyun 	}
1016*4882a593Smuzhiyun 
1017*4882a593Smuzhiyun 	/*
1018*4882a593Smuzhiyun 	 * Release any bios held while the block was being provisioned.
1019*4882a593Smuzhiyun 	 * If we are processing a write bio that completely covers the block,
1020*4882a593Smuzhiyun 	 * we already processed it so can ignore it now when processing
1021*4882a593Smuzhiyun 	 * the bios in the cell.
1022*4882a593Smuzhiyun 	 */
1023*4882a593Smuzhiyun 	if (bio) {
1024*4882a593Smuzhiyun 		inc_remap_and_issue_cell(tc, m->cell, m->data_block);
1025*4882a593Smuzhiyun 		complete_overwrite_bio(tc, bio);
1026*4882a593Smuzhiyun 	} else {
1027*4882a593Smuzhiyun 		inc_all_io_entry(tc->pool, m->cell->holder);
1028*4882a593Smuzhiyun 		remap_and_issue(tc, m->cell->holder, m->data_block);
1029*4882a593Smuzhiyun 		inc_remap_and_issue_cell(tc, m->cell, m->data_block);
1030*4882a593Smuzhiyun 	}
1031*4882a593Smuzhiyun 
1032*4882a593Smuzhiyun out:
1033*4882a593Smuzhiyun 	list_del(&m->list);
1034*4882a593Smuzhiyun 	mempool_free(m, &pool->mapping_pool);
1035*4882a593Smuzhiyun }
1036*4882a593Smuzhiyun 
1037*4882a593Smuzhiyun /*----------------------------------------------------------------*/
1038*4882a593Smuzhiyun 
free_discard_mapping(struct dm_thin_new_mapping * m)1039*4882a593Smuzhiyun static void free_discard_mapping(struct dm_thin_new_mapping *m)
1040*4882a593Smuzhiyun {
1041*4882a593Smuzhiyun 	struct thin_c *tc = m->tc;
1042*4882a593Smuzhiyun 	if (m->cell)
1043*4882a593Smuzhiyun 		cell_defer_no_holder(tc, m->cell);
1044*4882a593Smuzhiyun 	mempool_free(m, &tc->pool->mapping_pool);
1045*4882a593Smuzhiyun }
1046*4882a593Smuzhiyun 
process_prepared_discard_fail(struct dm_thin_new_mapping * m)1047*4882a593Smuzhiyun static void process_prepared_discard_fail(struct dm_thin_new_mapping *m)
1048*4882a593Smuzhiyun {
1049*4882a593Smuzhiyun 	bio_io_error(m->bio);
1050*4882a593Smuzhiyun 	free_discard_mapping(m);
1051*4882a593Smuzhiyun }
1052*4882a593Smuzhiyun 
process_prepared_discard_success(struct dm_thin_new_mapping * m)1053*4882a593Smuzhiyun static void process_prepared_discard_success(struct dm_thin_new_mapping *m)
1054*4882a593Smuzhiyun {
1055*4882a593Smuzhiyun 	bio_endio(m->bio);
1056*4882a593Smuzhiyun 	free_discard_mapping(m);
1057*4882a593Smuzhiyun }
1058*4882a593Smuzhiyun 
process_prepared_discard_no_passdown(struct dm_thin_new_mapping * m)1059*4882a593Smuzhiyun static void process_prepared_discard_no_passdown(struct dm_thin_new_mapping *m)
1060*4882a593Smuzhiyun {
1061*4882a593Smuzhiyun 	int r;
1062*4882a593Smuzhiyun 	struct thin_c *tc = m->tc;
1063*4882a593Smuzhiyun 
1064*4882a593Smuzhiyun 	r = dm_thin_remove_range(tc->td, m->cell->key.block_begin, m->cell->key.block_end);
1065*4882a593Smuzhiyun 	if (r) {
1066*4882a593Smuzhiyun 		metadata_operation_failed(tc->pool, "dm_thin_remove_range", r);
1067*4882a593Smuzhiyun 		bio_io_error(m->bio);
1068*4882a593Smuzhiyun 	} else
1069*4882a593Smuzhiyun 		bio_endio(m->bio);
1070*4882a593Smuzhiyun 
1071*4882a593Smuzhiyun 	cell_defer_no_holder(tc, m->cell);
1072*4882a593Smuzhiyun 	mempool_free(m, &tc->pool->mapping_pool);
1073*4882a593Smuzhiyun }
1074*4882a593Smuzhiyun 
1075*4882a593Smuzhiyun /*----------------------------------------------------------------*/
1076*4882a593Smuzhiyun 
passdown_double_checking_shared_status(struct dm_thin_new_mapping * m,struct bio * discard_parent)1077*4882a593Smuzhiyun static void passdown_double_checking_shared_status(struct dm_thin_new_mapping *m,
1078*4882a593Smuzhiyun 						   struct bio *discard_parent)
1079*4882a593Smuzhiyun {
1080*4882a593Smuzhiyun 	/*
1081*4882a593Smuzhiyun 	 * We've already unmapped this range of blocks, but before we
1082*4882a593Smuzhiyun 	 * passdown we have to check that these blocks are now unused.
1083*4882a593Smuzhiyun 	 */
1084*4882a593Smuzhiyun 	int r = 0;
1085*4882a593Smuzhiyun 	bool shared = true;
1086*4882a593Smuzhiyun 	struct thin_c *tc = m->tc;
1087*4882a593Smuzhiyun 	struct pool *pool = tc->pool;
1088*4882a593Smuzhiyun 	dm_block_t b = m->data_block, e, end = m->data_block + m->virt_end - m->virt_begin;
1089*4882a593Smuzhiyun 	struct discard_op op;
1090*4882a593Smuzhiyun 
1091*4882a593Smuzhiyun 	begin_discard(&op, tc, discard_parent);
1092*4882a593Smuzhiyun 	while (b != end) {
1093*4882a593Smuzhiyun 		/* find start of unmapped run */
1094*4882a593Smuzhiyun 		for (; b < end; b++) {
1095*4882a593Smuzhiyun 			r = dm_pool_block_is_shared(pool->pmd, b, &shared);
1096*4882a593Smuzhiyun 			if (r)
1097*4882a593Smuzhiyun 				goto out;
1098*4882a593Smuzhiyun 
1099*4882a593Smuzhiyun 			if (!shared)
1100*4882a593Smuzhiyun 				break;
1101*4882a593Smuzhiyun 		}
1102*4882a593Smuzhiyun 
1103*4882a593Smuzhiyun 		if (b == end)
1104*4882a593Smuzhiyun 			break;
1105*4882a593Smuzhiyun 
1106*4882a593Smuzhiyun 		/* find end of run */
1107*4882a593Smuzhiyun 		for (e = b + 1; e != end; e++) {
1108*4882a593Smuzhiyun 			r = dm_pool_block_is_shared(pool->pmd, e, &shared);
1109*4882a593Smuzhiyun 			if (r)
1110*4882a593Smuzhiyun 				goto out;
1111*4882a593Smuzhiyun 
1112*4882a593Smuzhiyun 			if (shared)
1113*4882a593Smuzhiyun 				break;
1114*4882a593Smuzhiyun 		}
1115*4882a593Smuzhiyun 
1116*4882a593Smuzhiyun 		r = issue_discard(&op, b, e);
1117*4882a593Smuzhiyun 		if (r)
1118*4882a593Smuzhiyun 			goto out;
1119*4882a593Smuzhiyun 
1120*4882a593Smuzhiyun 		b = e;
1121*4882a593Smuzhiyun 	}
1122*4882a593Smuzhiyun out:
1123*4882a593Smuzhiyun 	end_discard(&op, r);
1124*4882a593Smuzhiyun }
1125*4882a593Smuzhiyun 
queue_passdown_pt2(struct dm_thin_new_mapping * m)1126*4882a593Smuzhiyun static void queue_passdown_pt2(struct dm_thin_new_mapping *m)
1127*4882a593Smuzhiyun {
1128*4882a593Smuzhiyun 	unsigned long flags;
1129*4882a593Smuzhiyun 	struct pool *pool = m->tc->pool;
1130*4882a593Smuzhiyun 
1131*4882a593Smuzhiyun 	spin_lock_irqsave(&pool->lock, flags);
1132*4882a593Smuzhiyun 	list_add_tail(&m->list, &pool->prepared_discards_pt2);
1133*4882a593Smuzhiyun 	spin_unlock_irqrestore(&pool->lock, flags);
1134*4882a593Smuzhiyun 	wake_worker(pool);
1135*4882a593Smuzhiyun }
1136*4882a593Smuzhiyun 
passdown_endio(struct bio * bio)1137*4882a593Smuzhiyun static void passdown_endio(struct bio *bio)
1138*4882a593Smuzhiyun {
1139*4882a593Smuzhiyun 	/*
1140*4882a593Smuzhiyun 	 * It doesn't matter if the passdown discard failed, we still want
1141*4882a593Smuzhiyun 	 * to unmap (we ignore err).
1142*4882a593Smuzhiyun 	 */
1143*4882a593Smuzhiyun 	queue_passdown_pt2(bio->bi_private);
1144*4882a593Smuzhiyun 	bio_put(bio);
1145*4882a593Smuzhiyun }
1146*4882a593Smuzhiyun 
process_prepared_discard_passdown_pt1(struct dm_thin_new_mapping * m)1147*4882a593Smuzhiyun static void process_prepared_discard_passdown_pt1(struct dm_thin_new_mapping *m)
1148*4882a593Smuzhiyun {
1149*4882a593Smuzhiyun 	int r;
1150*4882a593Smuzhiyun 	struct thin_c *tc = m->tc;
1151*4882a593Smuzhiyun 	struct pool *pool = tc->pool;
1152*4882a593Smuzhiyun 	struct bio *discard_parent;
1153*4882a593Smuzhiyun 	dm_block_t data_end = m->data_block + (m->virt_end - m->virt_begin);
1154*4882a593Smuzhiyun 
1155*4882a593Smuzhiyun 	/*
1156*4882a593Smuzhiyun 	 * Only this thread allocates blocks, so we can be sure that the
1157*4882a593Smuzhiyun 	 * newly unmapped blocks will not be allocated before the end of
1158*4882a593Smuzhiyun 	 * the function.
1159*4882a593Smuzhiyun 	 */
1160*4882a593Smuzhiyun 	r = dm_thin_remove_range(tc->td, m->virt_begin, m->virt_end);
1161*4882a593Smuzhiyun 	if (r) {
1162*4882a593Smuzhiyun 		metadata_operation_failed(pool, "dm_thin_remove_range", r);
1163*4882a593Smuzhiyun 		bio_io_error(m->bio);
1164*4882a593Smuzhiyun 		cell_defer_no_holder(tc, m->cell);
1165*4882a593Smuzhiyun 		mempool_free(m, &pool->mapping_pool);
1166*4882a593Smuzhiyun 		return;
1167*4882a593Smuzhiyun 	}
1168*4882a593Smuzhiyun 
1169*4882a593Smuzhiyun 	/*
1170*4882a593Smuzhiyun 	 * Increment the unmapped blocks.  This prevents a race between the
1171*4882a593Smuzhiyun 	 * passdown io and reallocation of freed blocks.
1172*4882a593Smuzhiyun 	 */
1173*4882a593Smuzhiyun 	r = dm_pool_inc_data_range(pool->pmd, m->data_block, data_end);
1174*4882a593Smuzhiyun 	if (r) {
1175*4882a593Smuzhiyun 		metadata_operation_failed(pool, "dm_pool_inc_data_range", r);
1176*4882a593Smuzhiyun 		bio_io_error(m->bio);
1177*4882a593Smuzhiyun 		cell_defer_no_holder(tc, m->cell);
1178*4882a593Smuzhiyun 		mempool_free(m, &pool->mapping_pool);
1179*4882a593Smuzhiyun 		return;
1180*4882a593Smuzhiyun 	}
1181*4882a593Smuzhiyun 
1182*4882a593Smuzhiyun 	discard_parent = bio_alloc(GFP_NOIO, 1);
1183*4882a593Smuzhiyun 	if (!discard_parent) {
1184*4882a593Smuzhiyun 		DMWARN("%s: unable to allocate top level discard bio for passdown. Skipping passdown.",
1185*4882a593Smuzhiyun 		       dm_device_name(tc->pool->pool_md));
1186*4882a593Smuzhiyun 		queue_passdown_pt2(m);
1187*4882a593Smuzhiyun 
1188*4882a593Smuzhiyun 	} else {
1189*4882a593Smuzhiyun 		discard_parent->bi_end_io = passdown_endio;
1190*4882a593Smuzhiyun 		discard_parent->bi_private = m;
1191*4882a593Smuzhiyun 
1192*4882a593Smuzhiyun 		if (m->maybe_shared)
1193*4882a593Smuzhiyun 			passdown_double_checking_shared_status(m, discard_parent);
1194*4882a593Smuzhiyun 		else {
1195*4882a593Smuzhiyun 			struct discard_op op;
1196*4882a593Smuzhiyun 
1197*4882a593Smuzhiyun 			begin_discard(&op, tc, discard_parent);
1198*4882a593Smuzhiyun 			r = issue_discard(&op, m->data_block, data_end);
1199*4882a593Smuzhiyun 			end_discard(&op, r);
1200*4882a593Smuzhiyun 		}
1201*4882a593Smuzhiyun 	}
1202*4882a593Smuzhiyun }
1203*4882a593Smuzhiyun 
process_prepared_discard_passdown_pt2(struct dm_thin_new_mapping * m)1204*4882a593Smuzhiyun static void process_prepared_discard_passdown_pt2(struct dm_thin_new_mapping *m)
1205*4882a593Smuzhiyun {
1206*4882a593Smuzhiyun 	int r;
1207*4882a593Smuzhiyun 	struct thin_c *tc = m->tc;
1208*4882a593Smuzhiyun 	struct pool *pool = tc->pool;
1209*4882a593Smuzhiyun 
1210*4882a593Smuzhiyun 	/*
1211*4882a593Smuzhiyun 	 * The passdown has completed, so now we can decrement all those
1212*4882a593Smuzhiyun 	 * unmapped blocks.
1213*4882a593Smuzhiyun 	 */
1214*4882a593Smuzhiyun 	r = dm_pool_dec_data_range(pool->pmd, m->data_block,
1215*4882a593Smuzhiyun 				   m->data_block + (m->virt_end - m->virt_begin));
1216*4882a593Smuzhiyun 	if (r) {
1217*4882a593Smuzhiyun 		metadata_operation_failed(pool, "dm_pool_dec_data_range", r);
1218*4882a593Smuzhiyun 		bio_io_error(m->bio);
1219*4882a593Smuzhiyun 	} else
1220*4882a593Smuzhiyun 		bio_endio(m->bio);
1221*4882a593Smuzhiyun 
1222*4882a593Smuzhiyun 	cell_defer_no_holder(tc, m->cell);
1223*4882a593Smuzhiyun 	mempool_free(m, &pool->mapping_pool);
1224*4882a593Smuzhiyun }
1225*4882a593Smuzhiyun 
process_prepared(struct pool * pool,struct list_head * head,process_mapping_fn * fn)1226*4882a593Smuzhiyun static void process_prepared(struct pool *pool, struct list_head *head,
1227*4882a593Smuzhiyun 			     process_mapping_fn *fn)
1228*4882a593Smuzhiyun {
1229*4882a593Smuzhiyun 	struct list_head maps;
1230*4882a593Smuzhiyun 	struct dm_thin_new_mapping *m, *tmp;
1231*4882a593Smuzhiyun 
1232*4882a593Smuzhiyun 	INIT_LIST_HEAD(&maps);
1233*4882a593Smuzhiyun 	spin_lock_irq(&pool->lock);
1234*4882a593Smuzhiyun 	list_splice_init(head, &maps);
1235*4882a593Smuzhiyun 	spin_unlock_irq(&pool->lock);
1236*4882a593Smuzhiyun 
1237*4882a593Smuzhiyun 	list_for_each_entry_safe(m, tmp, &maps, list)
1238*4882a593Smuzhiyun 		(*fn)(m);
1239*4882a593Smuzhiyun }
1240*4882a593Smuzhiyun 
1241*4882a593Smuzhiyun /*
1242*4882a593Smuzhiyun  * Deferred bio jobs.
1243*4882a593Smuzhiyun  */
io_overlaps_block(struct pool * pool,struct bio * bio)1244*4882a593Smuzhiyun static int io_overlaps_block(struct pool *pool, struct bio *bio)
1245*4882a593Smuzhiyun {
1246*4882a593Smuzhiyun 	return bio->bi_iter.bi_size ==
1247*4882a593Smuzhiyun 		(pool->sectors_per_block << SECTOR_SHIFT);
1248*4882a593Smuzhiyun }
1249*4882a593Smuzhiyun 
io_overwrites_block(struct pool * pool,struct bio * bio)1250*4882a593Smuzhiyun static int io_overwrites_block(struct pool *pool, struct bio *bio)
1251*4882a593Smuzhiyun {
1252*4882a593Smuzhiyun 	return (bio_data_dir(bio) == WRITE) &&
1253*4882a593Smuzhiyun 		io_overlaps_block(pool, bio);
1254*4882a593Smuzhiyun }
1255*4882a593Smuzhiyun 
save_and_set_endio(struct bio * bio,bio_end_io_t ** save,bio_end_io_t * fn)1256*4882a593Smuzhiyun static void save_and_set_endio(struct bio *bio, bio_end_io_t **save,
1257*4882a593Smuzhiyun 			       bio_end_io_t *fn)
1258*4882a593Smuzhiyun {
1259*4882a593Smuzhiyun 	*save = bio->bi_end_io;
1260*4882a593Smuzhiyun 	bio->bi_end_io = fn;
1261*4882a593Smuzhiyun }
1262*4882a593Smuzhiyun 
ensure_next_mapping(struct pool * pool)1263*4882a593Smuzhiyun static int ensure_next_mapping(struct pool *pool)
1264*4882a593Smuzhiyun {
1265*4882a593Smuzhiyun 	if (pool->next_mapping)
1266*4882a593Smuzhiyun 		return 0;
1267*4882a593Smuzhiyun 
1268*4882a593Smuzhiyun 	pool->next_mapping = mempool_alloc(&pool->mapping_pool, GFP_ATOMIC);
1269*4882a593Smuzhiyun 
1270*4882a593Smuzhiyun 	return pool->next_mapping ? 0 : -ENOMEM;
1271*4882a593Smuzhiyun }
1272*4882a593Smuzhiyun 
get_next_mapping(struct pool * pool)1273*4882a593Smuzhiyun static struct dm_thin_new_mapping *get_next_mapping(struct pool *pool)
1274*4882a593Smuzhiyun {
1275*4882a593Smuzhiyun 	struct dm_thin_new_mapping *m = pool->next_mapping;
1276*4882a593Smuzhiyun 
1277*4882a593Smuzhiyun 	BUG_ON(!pool->next_mapping);
1278*4882a593Smuzhiyun 
1279*4882a593Smuzhiyun 	memset(m, 0, sizeof(struct dm_thin_new_mapping));
1280*4882a593Smuzhiyun 	INIT_LIST_HEAD(&m->list);
1281*4882a593Smuzhiyun 	m->bio = NULL;
1282*4882a593Smuzhiyun 
1283*4882a593Smuzhiyun 	pool->next_mapping = NULL;
1284*4882a593Smuzhiyun 
1285*4882a593Smuzhiyun 	return m;
1286*4882a593Smuzhiyun }
1287*4882a593Smuzhiyun 
ll_zero(struct thin_c * tc,struct dm_thin_new_mapping * m,sector_t begin,sector_t end)1288*4882a593Smuzhiyun static void ll_zero(struct thin_c *tc, struct dm_thin_new_mapping *m,
1289*4882a593Smuzhiyun 		    sector_t begin, sector_t end)
1290*4882a593Smuzhiyun {
1291*4882a593Smuzhiyun 	struct dm_io_region to;
1292*4882a593Smuzhiyun 
1293*4882a593Smuzhiyun 	to.bdev = tc->pool_dev->bdev;
1294*4882a593Smuzhiyun 	to.sector = begin;
1295*4882a593Smuzhiyun 	to.count = end - begin;
1296*4882a593Smuzhiyun 
1297*4882a593Smuzhiyun 	dm_kcopyd_zero(tc->pool->copier, 1, &to, 0, copy_complete, m);
1298*4882a593Smuzhiyun }
1299*4882a593Smuzhiyun 
remap_and_issue_overwrite(struct thin_c * tc,struct bio * bio,dm_block_t data_begin,struct dm_thin_new_mapping * m)1300*4882a593Smuzhiyun static void remap_and_issue_overwrite(struct thin_c *tc, struct bio *bio,
1301*4882a593Smuzhiyun 				      dm_block_t data_begin,
1302*4882a593Smuzhiyun 				      struct dm_thin_new_mapping *m)
1303*4882a593Smuzhiyun {
1304*4882a593Smuzhiyun 	struct pool *pool = tc->pool;
1305*4882a593Smuzhiyun 	struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1306*4882a593Smuzhiyun 
1307*4882a593Smuzhiyun 	h->overwrite_mapping = m;
1308*4882a593Smuzhiyun 	m->bio = bio;
1309*4882a593Smuzhiyun 	save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
1310*4882a593Smuzhiyun 	inc_all_io_entry(pool, bio);
1311*4882a593Smuzhiyun 	remap_and_issue(tc, bio, data_begin);
1312*4882a593Smuzhiyun }
1313*4882a593Smuzhiyun 
1314*4882a593Smuzhiyun /*
1315*4882a593Smuzhiyun  * A partial copy also needs to zero the uncopied region.
1316*4882a593Smuzhiyun  */
schedule_copy(struct thin_c * tc,dm_block_t virt_block,struct dm_dev * origin,dm_block_t data_origin,dm_block_t data_dest,struct dm_bio_prison_cell * cell,struct bio * bio,sector_t len)1317*4882a593Smuzhiyun static void schedule_copy(struct thin_c *tc, dm_block_t virt_block,
1318*4882a593Smuzhiyun 			  struct dm_dev *origin, dm_block_t data_origin,
1319*4882a593Smuzhiyun 			  dm_block_t data_dest,
1320*4882a593Smuzhiyun 			  struct dm_bio_prison_cell *cell, struct bio *bio,
1321*4882a593Smuzhiyun 			  sector_t len)
1322*4882a593Smuzhiyun {
1323*4882a593Smuzhiyun 	struct pool *pool = tc->pool;
1324*4882a593Smuzhiyun 	struct dm_thin_new_mapping *m = get_next_mapping(pool);
1325*4882a593Smuzhiyun 
1326*4882a593Smuzhiyun 	m->tc = tc;
1327*4882a593Smuzhiyun 	m->virt_begin = virt_block;
1328*4882a593Smuzhiyun 	m->virt_end = virt_block + 1u;
1329*4882a593Smuzhiyun 	m->data_block = data_dest;
1330*4882a593Smuzhiyun 	m->cell = cell;
1331*4882a593Smuzhiyun 
1332*4882a593Smuzhiyun 	/*
1333*4882a593Smuzhiyun 	 * quiesce action + copy action + an extra reference held for the
1334*4882a593Smuzhiyun 	 * duration of this function (we may need to inc later for a
1335*4882a593Smuzhiyun 	 * partial zero).
1336*4882a593Smuzhiyun 	 */
1337*4882a593Smuzhiyun 	atomic_set(&m->prepare_actions, 3);
1338*4882a593Smuzhiyun 
1339*4882a593Smuzhiyun 	if (!dm_deferred_set_add_work(pool->shared_read_ds, &m->list))
1340*4882a593Smuzhiyun 		complete_mapping_preparation(m); /* already quiesced */
1341*4882a593Smuzhiyun 
1342*4882a593Smuzhiyun 	/*
1343*4882a593Smuzhiyun 	 * IO to pool_dev remaps to the pool target's data_dev.
1344*4882a593Smuzhiyun 	 *
1345*4882a593Smuzhiyun 	 * If the whole block of data is being overwritten, we can issue the
1346*4882a593Smuzhiyun 	 * bio immediately. Otherwise we use kcopyd to clone the data first.
1347*4882a593Smuzhiyun 	 */
1348*4882a593Smuzhiyun 	if (io_overwrites_block(pool, bio))
1349*4882a593Smuzhiyun 		remap_and_issue_overwrite(tc, bio, data_dest, m);
1350*4882a593Smuzhiyun 	else {
1351*4882a593Smuzhiyun 		struct dm_io_region from, to;
1352*4882a593Smuzhiyun 
1353*4882a593Smuzhiyun 		from.bdev = origin->bdev;
1354*4882a593Smuzhiyun 		from.sector = data_origin * pool->sectors_per_block;
1355*4882a593Smuzhiyun 		from.count = len;
1356*4882a593Smuzhiyun 
1357*4882a593Smuzhiyun 		to.bdev = tc->pool_dev->bdev;
1358*4882a593Smuzhiyun 		to.sector = data_dest * pool->sectors_per_block;
1359*4882a593Smuzhiyun 		to.count = len;
1360*4882a593Smuzhiyun 
1361*4882a593Smuzhiyun 		dm_kcopyd_copy(pool->copier, &from, 1, &to,
1362*4882a593Smuzhiyun 			       0, copy_complete, m);
1363*4882a593Smuzhiyun 
1364*4882a593Smuzhiyun 		/*
1365*4882a593Smuzhiyun 		 * Do we need to zero a tail region?
1366*4882a593Smuzhiyun 		 */
1367*4882a593Smuzhiyun 		if (len < pool->sectors_per_block && pool->pf.zero_new_blocks) {
1368*4882a593Smuzhiyun 			atomic_inc(&m->prepare_actions);
1369*4882a593Smuzhiyun 			ll_zero(tc, m,
1370*4882a593Smuzhiyun 				data_dest * pool->sectors_per_block + len,
1371*4882a593Smuzhiyun 				(data_dest + 1) * pool->sectors_per_block);
1372*4882a593Smuzhiyun 		}
1373*4882a593Smuzhiyun 	}
1374*4882a593Smuzhiyun 
1375*4882a593Smuzhiyun 	complete_mapping_preparation(m); /* drop our ref */
1376*4882a593Smuzhiyun }
1377*4882a593Smuzhiyun 
schedule_internal_copy(struct thin_c * tc,dm_block_t virt_block,dm_block_t data_origin,dm_block_t data_dest,struct dm_bio_prison_cell * cell,struct bio * bio)1378*4882a593Smuzhiyun static void schedule_internal_copy(struct thin_c *tc, dm_block_t virt_block,
1379*4882a593Smuzhiyun 				   dm_block_t data_origin, dm_block_t data_dest,
1380*4882a593Smuzhiyun 				   struct dm_bio_prison_cell *cell, struct bio *bio)
1381*4882a593Smuzhiyun {
1382*4882a593Smuzhiyun 	schedule_copy(tc, virt_block, tc->pool_dev,
1383*4882a593Smuzhiyun 		      data_origin, data_dest, cell, bio,
1384*4882a593Smuzhiyun 		      tc->pool->sectors_per_block);
1385*4882a593Smuzhiyun }
1386*4882a593Smuzhiyun 
schedule_zero(struct thin_c * tc,dm_block_t virt_block,dm_block_t data_block,struct dm_bio_prison_cell * cell,struct bio * bio)1387*4882a593Smuzhiyun static void schedule_zero(struct thin_c *tc, dm_block_t virt_block,
1388*4882a593Smuzhiyun 			  dm_block_t data_block, struct dm_bio_prison_cell *cell,
1389*4882a593Smuzhiyun 			  struct bio *bio)
1390*4882a593Smuzhiyun {
1391*4882a593Smuzhiyun 	struct pool *pool = tc->pool;
1392*4882a593Smuzhiyun 	struct dm_thin_new_mapping *m = get_next_mapping(pool);
1393*4882a593Smuzhiyun 
1394*4882a593Smuzhiyun 	atomic_set(&m->prepare_actions, 1); /* no need to quiesce */
1395*4882a593Smuzhiyun 	m->tc = tc;
1396*4882a593Smuzhiyun 	m->virt_begin = virt_block;
1397*4882a593Smuzhiyun 	m->virt_end = virt_block + 1u;
1398*4882a593Smuzhiyun 	m->data_block = data_block;
1399*4882a593Smuzhiyun 	m->cell = cell;
1400*4882a593Smuzhiyun 
1401*4882a593Smuzhiyun 	/*
1402*4882a593Smuzhiyun 	 * If the whole block of data is being overwritten or we are not
1403*4882a593Smuzhiyun 	 * zeroing pre-existing data, we can issue the bio immediately.
1404*4882a593Smuzhiyun 	 * Otherwise we use kcopyd to zero the data first.
1405*4882a593Smuzhiyun 	 */
1406*4882a593Smuzhiyun 	if (pool->pf.zero_new_blocks) {
1407*4882a593Smuzhiyun 		if (io_overwrites_block(pool, bio))
1408*4882a593Smuzhiyun 			remap_and_issue_overwrite(tc, bio, data_block, m);
1409*4882a593Smuzhiyun 		else
1410*4882a593Smuzhiyun 			ll_zero(tc, m, data_block * pool->sectors_per_block,
1411*4882a593Smuzhiyun 				(data_block + 1) * pool->sectors_per_block);
1412*4882a593Smuzhiyun 	} else
1413*4882a593Smuzhiyun 		process_prepared_mapping(m);
1414*4882a593Smuzhiyun }
1415*4882a593Smuzhiyun 
schedule_external_copy(struct thin_c * tc,dm_block_t virt_block,dm_block_t data_dest,struct dm_bio_prison_cell * cell,struct bio * bio)1416*4882a593Smuzhiyun static void schedule_external_copy(struct thin_c *tc, dm_block_t virt_block,
1417*4882a593Smuzhiyun 				   dm_block_t data_dest,
1418*4882a593Smuzhiyun 				   struct dm_bio_prison_cell *cell, struct bio *bio)
1419*4882a593Smuzhiyun {
1420*4882a593Smuzhiyun 	struct pool *pool = tc->pool;
1421*4882a593Smuzhiyun 	sector_t virt_block_begin = virt_block * pool->sectors_per_block;
1422*4882a593Smuzhiyun 	sector_t virt_block_end = (virt_block + 1) * pool->sectors_per_block;
1423*4882a593Smuzhiyun 
1424*4882a593Smuzhiyun 	if (virt_block_end <= tc->origin_size)
1425*4882a593Smuzhiyun 		schedule_copy(tc, virt_block, tc->origin_dev,
1426*4882a593Smuzhiyun 			      virt_block, data_dest, cell, bio,
1427*4882a593Smuzhiyun 			      pool->sectors_per_block);
1428*4882a593Smuzhiyun 
1429*4882a593Smuzhiyun 	else if (virt_block_begin < tc->origin_size)
1430*4882a593Smuzhiyun 		schedule_copy(tc, virt_block, tc->origin_dev,
1431*4882a593Smuzhiyun 			      virt_block, data_dest, cell, bio,
1432*4882a593Smuzhiyun 			      tc->origin_size - virt_block_begin);
1433*4882a593Smuzhiyun 
1434*4882a593Smuzhiyun 	else
1435*4882a593Smuzhiyun 		schedule_zero(tc, virt_block, data_dest, cell, bio);
1436*4882a593Smuzhiyun }
1437*4882a593Smuzhiyun 
1438*4882a593Smuzhiyun static void set_pool_mode(struct pool *pool, enum pool_mode new_mode);
1439*4882a593Smuzhiyun 
1440*4882a593Smuzhiyun static void requeue_bios(struct pool *pool);
1441*4882a593Smuzhiyun 
is_read_only_pool_mode(enum pool_mode mode)1442*4882a593Smuzhiyun static bool is_read_only_pool_mode(enum pool_mode mode)
1443*4882a593Smuzhiyun {
1444*4882a593Smuzhiyun 	return (mode == PM_OUT_OF_METADATA_SPACE || mode == PM_READ_ONLY);
1445*4882a593Smuzhiyun }
1446*4882a593Smuzhiyun 
is_read_only(struct pool * pool)1447*4882a593Smuzhiyun static bool is_read_only(struct pool *pool)
1448*4882a593Smuzhiyun {
1449*4882a593Smuzhiyun 	return is_read_only_pool_mode(get_pool_mode(pool));
1450*4882a593Smuzhiyun }
1451*4882a593Smuzhiyun 
check_for_metadata_space(struct pool * pool)1452*4882a593Smuzhiyun static void check_for_metadata_space(struct pool *pool)
1453*4882a593Smuzhiyun {
1454*4882a593Smuzhiyun 	int r;
1455*4882a593Smuzhiyun 	const char *ooms_reason = NULL;
1456*4882a593Smuzhiyun 	dm_block_t nr_free;
1457*4882a593Smuzhiyun 
1458*4882a593Smuzhiyun 	r = dm_pool_get_free_metadata_block_count(pool->pmd, &nr_free);
1459*4882a593Smuzhiyun 	if (r)
1460*4882a593Smuzhiyun 		ooms_reason = "Could not get free metadata blocks";
1461*4882a593Smuzhiyun 	else if (!nr_free)
1462*4882a593Smuzhiyun 		ooms_reason = "No free metadata blocks";
1463*4882a593Smuzhiyun 
1464*4882a593Smuzhiyun 	if (ooms_reason && !is_read_only(pool)) {
1465*4882a593Smuzhiyun 		DMERR("%s", ooms_reason);
1466*4882a593Smuzhiyun 		set_pool_mode(pool, PM_OUT_OF_METADATA_SPACE);
1467*4882a593Smuzhiyun 	}
1468*4882a593Smuzhiyun }
1469*4882a593Smuzhiyun 
check_for_data_space(struct pool * pool)1470*4882a593Smuzhiyun static void check_for_data_space(struct pool *pool)
1471*4882a593Smuzhiyun {
1472*4882a593Smuzhiyun 	int r;
1473*4882a593Smuzhiyun 	dm_block_t nr_free;
1474*4882a593Smuzhiyun 
1475*4882a593Smuzhiyun 	if (get_pool_mode(pool) != PM_OUT_OF_DATA_SPACE)
1476*4882a593Smuzhiyun 		return;
1477*4882a593Smuzhiyun 
1478*4882a593Smuzhiyun 	r = dm_pool_get_free_block_count(pool->pmd, &nr_free);
1479*4882a593Smuzhiyun 	if (r)
1480*4882a593Smuzhiyun 		return;
1481*4882a593Smuzhiyun 
1482*4882a593Smuzhiyun 	if (nr_free) {
1483*4882a593Smuzhiyun 		set_pool_mode(pool, PM_WRITE);
1484*4882a593Smuzhiyun 		requeue_bios(pool);
1485*4882a593Smuzhiyun 	}
1486*4882a593Smuzhiyun }
1487*4882a593Smuzhiyun 
1488*4882a593Smuzhiyun /*
1489*4882a593Smuzhiyun  * A non-zero return indicates read_only or fail_io mode.
1490*4882a593Smuzhiyun  * Many callers don't care about the return value.
1491*4882a593Smuzhiyun  */
commit(struct pool * pool)1492*4882a593Smuzhiyun static int commit(struct pool *pool)
1493*4882a593Smuzhiyun {
1494*4882a593Smuzhiyun 	int r;
1495*4882a593Smuzhiyun 
1496*4882a593Smuzhiyun 	if (get_pool_mode(pool) >= PM_OUT_OF_METADATA_SPACE)
1497*4882a593Smuzhiyun 		return -EINVAL;
1498*4882a593Smuzhiyun 
1499*4882a593Smuzhiyun 	r = dm_pool_commit_metadata(pool->pmd);
1500*4882a593Smuzhiyun 	if (r)
1501*4882a593Smuzhiyun 		metadata_operation_failed(pool, "dm_pool_commit_metadata", r);
1502*4882a593Smuzhiyun 	else {
1503*4882a593Smuzhiyun 		check_for_metadata_space(pool);
1504*4882a593Smuzhiyun 		check_for_data_space(pool);
1505*4882a593Smuzhiyun 	}
1506*4882a593Smuzhiyun 
1507*4882a593Smuzhiyun 	return r;
1508*4882a593Smuzhiyun }
1509*4882a593Smuzhiyun 
check_low_water_mark(struct pool * pool,dm_block_t free_blocks)1510*4882a593Smuzhiyun static void check_low_water_mark(struct pool *pool, dm_block_t free_blocks)
1511*4882a593Smuzhiyun {
1512*4882a593Smuzhiyun 	if (free_blocks <= pool->low_water_blocks && !pool->low_water_triggered) {
1513*4882a593Smuzhiyun 		DMWARN("%s: reached low water mark for data device: sending event.",
1514*4882a593Smuzhiyun 		       dm_device_name(pool->pool_md));
1515*4882a593Smuzhiyun 		spin_lock_irq(&pool->lock);
1516*4882a593Smuzhiyun 		pool->low_water_triggered = true;
1517*4882a593Smuzhiyun 		spin_unlock_irq(&pool->lock);
1518*4882a593Smuzhiyun 		dm_table_event(pool->ti->table);
1519*4882a593Smuzhiyun 	}
1520*4882a593Smuzhiyun }
1521*4882a593Smuzhiyun 
alloc_data_block(struct thin_c * tc,dm_block_t * result)1522*4882a593Smuzhiyun static int alloc_data_block(struct thin_c *tc, dm_block_t *result)
1523*4882a593Smuzhiyun {
1524*4882a593Smuzhiyun 	int r;
1525*4882a593Smuzhiyun 	dm_block_t free_blocks;
1526*4882a593Smuzhiyun 	struct pool *pool = tc->pool;
1527*4882a593Smuzhiyun 
1528*4882a593Smuzhiyun 	if (WARN_ON(get_pool_mode(pool) != PM_WRITE))
1529*4882a593Smuzhiyun 		return -EINVAL;
1530*4882a593Smuzhiyun 
1531*4882a593Smuzhiyun 	r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
1532*4882a593Smuzhiyun 	if (r) {
1533*4882a593Smuzhiyun 		metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
1534*4882a593Smuzhiyun 		return r;
1535*4882a593Smuzhiyun 	}
1536*4882a593Smuzhiyun 
1537*4882a593Smuzhiyun 	check_low_water_mark(pool, free_blocks);
1538*4882a593Smuzhiyun 
1539*4882a593Smuzhiyun 	if (!free_blocks) {
1540*4882a593Smuzhiyun 		/*
1541*4882a593Smuzhiyun 		 * Try to commit to see if that will free up some
1542*4882a593Smuzhiyun 		 * more space.
1543*4882a593Smuzhiyun 		 */
1544*4882a593Smuzhiyun 		r = commit(pool);
1545*4882a593Smuzhiyun 		if (r)
1546*4882a593Smuzhiyun 			return r;
1547*4882a593Smuzhiyun 
1548*4882a593Smuzhiyun 		r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
1549*4882a593Smuzhiyun 		if (r) {
1550*4882a593Smuzhiyun 			metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
1551*4882a593Smuzhiyun 			return r;
1552*4882a593Smuzhiyun 		}
1553*4882a593Smuzhiyun 
1554*4882a593Smuzhiyun 		if (!free_blocks) {
1555*4882a593Smuzhiyun 			set_pool_mode(pool, PM_OUT_OF_DATA_SPACE);
1556*4882a593Smuzhiyun 			return -ENOSPC;
1557*4882a593Smuzhiyun 		}
1558*4882a593Smuzhiyun 	}
1559*4882a593Smuzhiyun 
1560*4882a593Smuzhiyun 	r = dm_pool_alloc_data_block(pool->pmd, result);
1561*4882a593Smuzhiyun 	if (r) {
1562*4882a593Smuzhiyun 		if (r == -ENOSPC)
1563*4882a593Smuzhiyun 			set_pool_mode(pool, PM_OUT_OF_DATA_SPACE);
1564*4882a593Smuzhiyun 		else
1565*4882a593Smuzhiyun 			metadata_operation_failed(pool, "dm_pool_alloc_data_block", r);
1566*4882a593Smuzhiyun 		return r;
1567*4882a593Smuzhiyun 	}
1568*4882a593Smuzhiyun 
1569*4882a593Smuzhiyun 	r = dm_pool_get_free_metadata_block_count(pool->pmd, &free_blocks);
1570*4882a593Smuzhiyun 	if (r) {
1571*4882a593Smuzhiyun 		metadata_operation_failed(pool, "dm_pool_get_free_metadata_block_count", r);
1572*4882a593Smuzhiyun 		return r;
1573*4882a593Smuzhiyun 	}
1574*4882a593Smuzhiyun 
1575*4882a593Smuzhiyun 	if (!free_blocks) {
1576*4882a593Smuzhiyun 		/* Let's commit before we use up the metadata reserve. */
1577*4882a593Smuzhiyun 		r = commit(pool);
1578*4882a593Smuzhiyun 		if (r)
1579*4882a593Smuzhiyun 			return r;
1580*4882a593Smuzhiyun 	}
1581*4882a593Smuzhiyun 
1582*4882a593Smuzhiyun 	return 0;
1583*4882a593Smuzhiyun }
1584*4882a593Smuzhiyun 
1585*4882a593Smuzhiyun /*
1586*4882a593Smuzhiyun  * If we have run out of space, queue bios until the device is
1587*4882a593Smuzhiyun  * resumed, presumably after having been reloaded with more space.
1588*4882a593Smuzhiyun  */
retry_on_resume(struct bio * bio)1589*4882a593Smuzhiyun static void retry_on_resume(struct bio *bio)
1590*4882a593Smuzhiyun {
1591*4882a593Smuzhiyun 	struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1592*4882a593Smuzhiyun 	struct thin_c *tc = h->tc;
1593*4882a593Smuzhiyun 
1594*4882a593Smuzhiyun 	spin_lock_irq(&tc->lock);
1595*4882a593Smuzhiyun 	bio_list_add(&tc->retry_on_resume_list, bio);
1596*4882a593Smuzhiyun 	spin_unlock_irq(&tc->lock);
1597*4882a593Smuzhiyun }
1598*4882a593Smuzhiyun 
should_error_unserviceable_bio(struct pool * pool)1599*4882a593Smuzhiyun static blk_status_t should_error_unserviceable_bio(struct pool *pool)
1600*4882a593Smuzhiyun {
1601*4882a593Smuzhiyun 	enum pool_mode m = get_pool_mode(pool);
1602*4882a593Smuzhiyun 
1603*4882a593Smuzhiyun 	switch (m) {
1604*4882a593Smuzhiyun 	case PM_WRITE:
1605*4882a593Smuzhiyun 		/* Shouldn't get here */
1606*4882a593Smuzhiyun 		DMERR_LIMIT("bio unserviceable, yet pool is in PM_WRITE mode");
1607*4882a593Smuzhiyun 		return BLK_STS_IOERR;
1608*4882a593Smuzhiyun 
1609*4882a593Smuzhiyun 	case PM_OUT_OF_DATA_SPACE:
1610*4882a593Smuzhiyun 		return pool->pf.error_if_no_space ? BLK_STS_NOSPC : 0;
1611*4882a593Smuzhiyun 
1612*4882a593Smuzhiyun 	case PM_OUT_OF_METADATA_SPACE:
1613*4882a593Smuzhiyun 	case PM_READ_ONLY:
1614*4882a593Smuzhiyun 	case PM_FAIL:
1615*4882a593Smuzhiyun 		return BLK_STS_IOERR;
1616*4882a593Smuzhiyun 	default:
1617*4882a593Smuzhiyun 		/* Shouldn't get here */
1618*4882a593Smuzhiyun 		DMERR_LIMIT("bio unserviceable, yet pool has an unknown mode");
1619*4882a593Smuzhiyun 		return BLK_STS_IOERR;
1620*4882a593Smuzhiyun 	}
1621*4882a593Smuzhiyun }
1622*4882a593Smuzhiyun 
handle_unserviceable_bio(struct pool * pool,struct bio * bio)1623*4882a593Smuzhiyun static void handle_unserviceable_bio(struct pool *pool, struct bio *bio)
1624*4882a593Smuzhiyun {
1625*4882a593Smuzhiyun 	blk_status_t error = should_error_unserviceable_bio(pool);
1626*4882a593Smuzhiyun 
1627*4882a593Smuzhiyun 	if (error) {
1628*4882a593Smuzhiyun 		bio->bi_status = error;
1629*4882a593Smuzhiyun 		bio_endio(bio);
1630*4882a593Smuzhiyun 	} else
1631*4882a593Smuzhiyun 		retry_on_resume(bio);
1632*4882a593Smuzhiyun }
1633*4882a593Smuzhiyun 
retry_bios_on_resume(struct pool * pool,struct dm_bio_prison_cell * cell)1634*4882a593Smuzhiyun static void retry_bios_on_resume(struct pool *pool, struct dm_bio_prison_cell *cell)
1635*4882a593Smuzhiyun {
1636*4882a593Smuzhiyun 	struct bio *bio;
1637*4882a593Smuzhiyun 	struct bio_list bios;
1638*4882a593Smuzhiyun 	blk_status_t error;
1639*4882a593Smuzhiyun 
1640*4882a593Smuzhiyun 	error = should_error_unserviceable_bio(pool);
1641*4882a593Smuzhiyun 	if (error) {
1642*4882a593Smuzhiyun 		cell_error_with_code(pool, cell, error);
1643*4882a593Smuzhiyun 		return;
1644*4882a593Smuzhiyun 	}
1645*4882a593Smuzhiyun 
1646*4882a593Smuzhiyun 	bio_list_init(&bios);
1647*4882a593Smuzhiyun 	cell_release(pool, cell, &bios);
1648*4882a593Smuzhiyun 
1649*4882a593Smuzhiyun 	while ((bio = bio_list_pop(&bios)))
1650*4882a593Smuzhiyun 		retry_on_resume(bio);
1651*4882a593Smuzhiyun }
1652*4882a593Smuzhiyun 
process_discard_cell_no_passdown(struct thin_c * tc,struct dm_bio_prison_cell * virt_cell)1653*4882a593Smuzhiyun static void process_discard_cell_no_passdown(struct thin_c *tc,
1654*4882a593Smuzhiyun 					     struct dm_bio_prison_cell *virt_cell)
1655*4882a593Smuzhiyun {
1656*4882a593Smuzhiyun 	struct pool *pool = tc->pool;
1657*4882a593Smuzhiyun 	struct dm_thin_new_mapping *m = get_next_mapping(pool);
1658*4882a593Smuzhiyun 
1659*4882a593Smuzhiyun 	/*
1660*4882a593Smuzhiyun 	 * We don't need to lock the data blocks, since there's no
1661*4882a593Smuzhiyun 	 * passdown.  We only lock data blocks for allocation and breaking sharing.
1662*4882a593Smuzhiyun 	 */
1663*4882a593Smuzhiyun 	m->tc = tc;
1664*4882a593Smuzhiyun 	m->virt_begin = virt_cell->key.block_begin;
1665*4882a593Smuzhiyun 	m->virt_end = virt_cell->key.block_end;
1666*4882a593Smuzhiyun 	m->cell = virt_cell;
1667*4882a593Smuzhiyun 	m->bio = virt_cell->holder;
1668*4882a593Smuzhiyun 
1669*4882a593Smuzhiyun 	if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list))
1670*4882a593Smuzhiyun 		pool->process_prepared_discard(m);
1671*4882a593Smuzhiyun }
1672*4882a593Smuzhiyun 
break_up_discard_bio(struct thin_c * tc,dm_block_t begin,dm_block_t end,struct bio * bio)1673*4882a593Smuzhiyun static void break_up_discard_bio(struct thin_c *tc, dm_block_t begin, dm_block_t end,
1674*4882a593Smuzhiyun 				 struct bio *bio)
1675*4882a593Smuzhiyun {
1676*4882a593Smuzhiyun 	struct pool *pool = tc->pool;
1677*4882a593Smuzhiyun 
1678*4882a593Smuzhiyun 	int r;
1679*4882a593Smuzhiyun 	bool maybe_shared;
1680*4882a593Smuzhiyun 	struct dm_cell_key data_key;
1681*4882a593Smuzhiyun 	struct dm_bio_prison_cell *data_cell;
1682*4882a593Smuzhiyun 	struct dm_thin_new_mapping *m;
1683*4882a593Smuzhiyun 	dm_block_t virt_begin, virt_end, data_begin;
1684*4882a593Smuzhiyun 
1685*4882a593Smuzhiyun 	while (begin != end) {
1686*4882a593Smuzhiyun 		r = ensure_next_mapping(pool);
1687*4882a593Smuzhiyun 		if (r)
1688*4882a593Smuzhiyun 			/* we did our best */
1689*4882a593Smuzhiyun 			return;
1690*4882a593Smuzhiyun 
1691*4882a593Smuzhiyun 		r = dm_thin_find_mapped_range(tc->td, begin, end, &virt_begin, &virt_end,
1692*4882a593Smuzhiyun 					      &data_begin, &maybe_shared);
1693*4882a593Smuzhiyun 		if (r)
1694*4882a593Smuzhiyun 			/*
1695*4882a593Smuzhiyun 			 * Silently fail, letting any mappings we've
1696*4882a593Smuzhiyun 			 * created complete.
1697*4882a593Smuzhiyun 			 */
1698*4882a593Smuzhiyun 			break;
1699*4882a593Smuzhiyun 
1700*4882a593Smuzhiyun 		build_key(tc->td, PHYSICAL, data_begin, data_begin + (virt_end - virt_begin), &data_key);
1701*4882a593Smuzhiyun 		if (bio_detain(tc->pool, &data_key, NULL, &data_cell)) {
1702*4882a593Smuzhiyun 			/* contention, we'll give up with this range */
1703*4882a593Smuzhiyun 			begin = virt_end;
1704*4882a593Smuzhiyun 			continue;
1705*4882a593Smuzhiyun 		}
1706*4882a593Smuzhiyun 
1707*4882a593Smuzhiyun 		/*
1708*4882a593Smuzhiyun 		 * IO may still be going to the destination block.  We must
1709*4882a593Smuzhiyun 		 * quiesce before we can do the removal.
1710*4882a593Smuzhiyun 		 */
1711*4882a593Smuzhiyun 		m = get_next_mapping(pool);
1712*4882a593Smuzhiyun 		m->tc = tc;
1713*4882a593Smuzhiyun 		m->maybe_shared = maybe_shared;
1714*4882a593Smuzhiyun 		m->virt_begin = virt_begin;
1715*4882a593Smuzhiyun 		m->virt_end = virt_end;
1716*4882a593Smuzhiyun 		m->data_block = data_begin;
1717*4882a593Smuzhiyun 		m->cell = data_cell;
1718*4882a593Smuzhiyun 		m->bio = bio;
1719*4882a593Smuzhiyun 
1720*4882a593Smuzhiyun 		/*
1721*4882a593Smuzhiyun 		 * The parent bio must not complete before sub discard bios are
1722*4882a593Smuzhiyun 		 * chained to it (see end_discard's bio_chain)!
1723*4882a593Smuzhiyun 		 *
1724*4882a593Smuzhiyun 		 * This per-mapping bi_remaining increment is paired with
1725*4882a593Smuzhiyun 		 * the implicit decrement that occurs via bio_endio() in
1726*4882a593Smuzhiyun 		 * end_discard().
1727*4882a593Smuzhiyun 		 */
1728*4882a593Smuzhiyun 		bio_inc_remaining(bio);
1729*4882a593Smuzhiyun 		if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list))
1730*4882a593Smuzhiyun 			pool->process_prepared_discard(m);
1731*4882a593Smuzhiyun 
1732*4882a593Smuzhiyun 		begin = virt_end;
1733*4882a593Smuzhiyun 	}
1734*4882a593Smuzhiyun }
1735*4882a593Smuzhiyun 
process_discard_cell_passdown(struct thin_c * tc,struct dm_bio_prison_cell * virt_cell)1736*4882a593Smuzhiyun static void process_discard_cell_passdown(struct thin_c *tc, struct dm_bio_prison_cell *virt_cell)
1737*4882a593Smuzhiyun {
1738*4882a593Smuzhiyun 	struct bio *bio = virt_cell->holder;
1739*4882a593Smuzhiyun 	struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1740*4882a593Smuzhiyun 
1741*4882a593Smuzhiyun 	/*
1742*4882a593Smuzhiyun 	 * The virt_cell will only get freed once the origin bio completes.
1743*4882a593Smuzhiyun 	 * This means it will remain locked while all the individual
1744*4882a593Smuzhiyun 	 * passdown bios are in flight.
1745*4882a593Smuzhiyun 	 */
1746*4882a593Smuzhiyun 	h->cell = virt_cell;
1747*4882a593Smuzhiyun 	break_up_discard_bio(tc, virt_cell->key.block_begin, virt_cell->key.block_end, bio);
1748*4882a593Smuzhiyun 
1749*4882a593Smuzhiyun 	/*
1750*4882a593Smuzhiyun 	 * We complete the bio now, knowing that the bi_remaining field
1751*4882a593Smuzhiyun 	 * will prevent completion until the sub range discards have
1752*4882a593Smuzhiyun 	 * completed.
1753*4882a593Smuzhiyun 	 */
1754*4882a593Smuzhiyun 	bio_endio(bio);
1755*4882a593Smuzhiyun }
1756*4882a593Smuzhiyun 
process_discard_bio(struct thin_c * tc,struct bio * bio)1757*4882a593Smuzhiyun static void process_discard_bio(struct thin_c *tc, struct bio *bio)
1758*4882a593Smuzhiyun {
1759*4882a593Smuzhiyun 	dm_block_t begin, end;
1760*4882a593Smuzhiyun 	struct dm_cell_key virt_key;
1761*4882a593Smuzhiyun 	struct dm_bio_prison_cell *virt_cell;
1762*4882a593Smuzhiyun 
1763*4882a593Smuzhiyun 	get_bio_block_range(tc, bio, &begin, &end);
1764*4882a593Smuzhiyun 	if (begin == end) {
1765*4882a593Smuzhiyun 		/*
1766*4882a593Smuzhiyun 		 * The discard covers less than a block.
1767*4882a593Smuzhiyun 		 */
1768*4882a593Smuzhiyun 		bio_endio(bio);
1769*4882a593Smuzhiyun 		return;
1770*4882a593Smuzhiyun 	}
1771*4882a593Smuzhiyun 
1772*4882a593Smuzhiyun 	build_key(tc->td, VIRTUAL, begin, end, &virt_key);
1773*4882a593Smuzhiyun 	if (bio_detain(tc->pool, &virt_key, bio, &virt_cell))
1774*4882a593Smuzhiyun 		/*
1775*4882a593Smuzhiyun 		 * Potential starvation issue: We're relying on the
1776*4882a593Smuzhiyun 		 * fs/application being well behaved, and not trying to
1777*4882a593Smuzhiyun 		 * send IO to a region at the same time as discarding it.
1778*4882a593Smuzhiyun 		 * If they do this persistently then it's possible this
1779*4882a593Smuzhiyun 		 * cell will never be granted.
1780*4882a593Smuzhiyun 		 */
1781*4882a593Smuzhiyun 		return;
1782*4882a593Smuzhiyun 
1783*4882a593Smuzhiyun 	tc->pool->process_discard_cell(tc, virt_cell);
1784*4882a593Smuzhiyun }
1785*4882a593Smuzhiyun 
break_sharing(struct thin_c * tc,struct bio * bio,dm_block_t block,struct dm_cell_key * key,struct dm_thin_lookup_result * lookup_result,struct dm_bio_prison_cell * cell)1786*4882a593Smuzhiyun static void break_sharing(struct thin_c *tc, struct bio *bio, dm_block_t block,
1787*4882a593Smuzhiyun 			  struct dm_cell_key *key,
1788*4882a593Smuzhiyun 			  struct dm_thin_lookup_result *lookup_result,
1789*4882a593Smuzhiyun 			  struct dm_bio_prison_cell *cell)
1790*4882a593Smuzhiyun {
1791*4882a593Smuzhiyun 	int r;
1792*4882a593Smuzhiyun 	dm_block_t data_block;
1793*4882a593Smuzhiyun 	struct pool *pool = tc->pool;
1794*4882a593Smuzhiyun 
1795*4882a593Smuzhiyun 	r = alloc_data_block(tc, &data_block);
1796*4882a593Smuzhiyun 	switch (r) {
1797*4882a593Smuzhiyun 	case 0:
1798*4882a593Smuzhiyun 		schedule_internal_copy(tc, block, lookup_result->block,
1799*4882a593Smuzhiyun 				       data_block, cell, bio);
1800*4882a593Smuzhiyun 		break;
1801*4882a593Smuzhiyun 
1802*4882a593Smuzhiyun 	case -ENOSPC:
1803*4882a593Smuzhiyun 		retry_bios_on_resume(pool, cell);
1804*4882a593Smuzhiyun 		break;
1805*4882a593Smuzhiyun 
1806*4882a593Smuzhiyun 	default:
1807*4882a593Smuzhiyun 		DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1808*4882a593Smuzhiyun 			    __func__, r);
1809*4882a593Smuzhiyun 		cell_error(pool, cell);
1810*4882a593Smuzhiyun 		break;
1811*4882a593Smuzhiyun 	}
1812*4882a593Smuzhiyun }
1813*4882a593Smuzhiyun 
__remap_and_issue_shared_cell(void * context,struct dm_bio_prison_cell * cell)1814*4882a593Smuzhiyun static void __remap_and_issue_shared_cell(void *context,
1815*4882a593Smuzhiyun 					  struct dm_bio_prison_cell *cell)
1816*4882a593Smuzhiyun {
1817*4882a593Smuzhiyun 	struct remap_info *info = context;
1818*4882a593Smuzhiyun 	struct bio *bio;
1819*4882a593Smuzhiyun 
1820*4882a593Smuzhiyun 	while ((bio = bio_list_pop(&cell->bios))) {
1821*4882a593Smuzhiyun 		if (bio_data_dir(bio) == WRITE || op_is_flush(bio->bi_opf) ||
1822*4882a593Smuzhiyun 		    bio_op(bio) == REQ_OP_DISCARD)
1823*4882a593Smuzhiyun 			bio_list_add(&info->defer_bios, bio);
1824*4882a593Smuzhiyun 		else {
1825*4882a593Smuzhiyun 			struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1826*4882a593Smuzhiyun 
1827*4882a593Smuzhiyun 			h->shared_read_entry = dm_deferred_entry_inc(info->tc->pool->shared_read_ds);
1828*4882a593Smuzhiyun 			inc_all_io_entry(info->tc->pool, bio);
1829*4882a593Smuzhiyun 			bio_list_add(&info->issue_bios, bio);
1830*4882a593Smuzhiyun 		}
1831*4882a593Smuzhiyun 	}
1832*4882a593Smuzhiyun }
1833*4882a593Smuzhiyun 
remap_and_issue_shared_cell(struct thin_c * tc,struct dm_bio_prison_cell * cell,dm_block_t block)1834*4882a593Smuzhiyun static void remap_and_issue_shared_cell(struct thin_c *tc,
1835*4882a593Smuzhiyun 					struct dm_bio_prison_cell *cell,
1836*4882a593Smuzhiyun 					dm_block_t block)
1837*4882a593Smuzhiyun {
1838*4882a593Smuzhiyun 	struct bio *bio;
1839*4882a593Smuzhiyun 	struct remap_info info;
1840*4882a593Smuzhiyun 
1841*4882a593Smuzhiyun 	info.tc = tc;
1842*4882a593Smuzhiyun 	bio_list_init(&info.defer_bios);
1843*4882a593Smuzhiyun 	bio_list_init(&info.issue_bios);
1844*4882a593Smuzhiyun 
1845*4882a593Smuzhiyun 	cell_visit_release(tc->pool, __remap_and_issue_shared_cell,
1846*4882a593Smuzhiyun 			   &info, cell);
1847*4882a593Smuzhiyun 
1848*4882a593Smuzhiyun 	while ((bio = bio_list_pop(&info.defer_bios)))
1849*4882a593Smuzhiyun 		thin_defer_bio(tc, bio);
1850*4882a593Smuzhiyun 
1851*4882a593Smuzhiyun 	while ((bio = bio_list_pop(&info.issue_bios)))
1852*4882a593Smuzhiyun 		remap_and_issue(tc, bio, block);
1853*4882a593Smuzhiyun }
1854*4882a593Smuzhiyun 
process_shared_bio(struct thin_c * tc,struct bio * bio,dm_block_t block,struct dm_thin_lookup_result * lookup_result,struct dm_bio_prison_cell * virt_cell)1855*4882a593Smuzhiyun static void process_shared_bio(struct thin_c *tc, struct bio *bio,
1856*4882a593Smuzhiyun 			       dm_block_t block,
1857*4882a593Smuzhiyun 			       struct dm_thin_lookup_result *lookup_result,
1858*4882a593Smuzhiyun 			       struct dm_bio_prison_cell *virt_cell)
1859*4882a593Smuzhiyun {
1860*4882a593Smuzhiyun 	struct dm_bio_prison_cell *data_cell;
1861*4882a593Smuzhiyun 	struct pool *pool = tc->pool;
1862*4882a593Smuzhiyun 	struct dm_cell_key key;
1863*4882a593Smuzhiyun 
1864*4882a593Smuzhiyun 	/*
1865*4882a593Smuzhiyun 	 * If cell is already occupied, then sharing is already in the process
1866*4882a593Smuzhiyun 	 * of being broken so we have nothing further to do here.
1867*4882a593Smuzhiyun 	 */
1868*4882a593Smuzhiyun 	build_data_key(tc->td, lookup_result->block, &key);
1869*4882a593Smuzhiyun 	if (bio_detain(pool, &key, bio, &data_cell)) {
1870*4882a593Smuzhiyun 		cell_defer_no_holder(tc, virt_cell);
1871*4882a593Smuzhiyun 		return;
1872*4882a593Smuzhiyun 	}
1873*4882a593Smuzhiyun 
1874*4882a593Smuzhiyun 	if (bio_data_dir(bio) == WRITE && bio->bi_iter.bi_size) {
1875*4882a593Smuzhiyun 		break_sharing(tc, bio, block, &key, lookup_result, data_cell);
1876*4882a593Smuzhiyun 		cell_defer_no_holder(tc, virt_cell);
1877*4882a593Smuzhiyun 	} else {
1878*4882a593Smuzhiyun 		struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1879*4882a593Smuzhiyun 
1880*4882a593Smuzhiyun 		h->shared_read_entry = dm_deferred_entry_inc(pool->shared_read_ds);
1881*4882a593Smuzhiyun 		inc_all_io_entry(pool, bio);
1882*4882a593Smuzhiyun 		remap_and_issue(tc, bio, lookup_result->block);
1883*4882a593Smuzhiyun 
1884*4882a593Smuzhiyun 		remap_and_issue_shared_cell(tc, data_cell, lookup_result->block);
1885*4882a593Smuzhiyun 		remap_and_issue_shared_cell(tc, virt_cell, lookup_result->block);
1886*4882a593Smuzhiyun 	}
1887*4882a593Smuzhiyun }
1888*4882a593Smuzhiyun 
provision_block(struct thin_c * tc,struct bio * bio,dm_block_t block,struct dm_bio_prison_cell * cell)1889*4882a593Smuzhiyun static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block,
1890*4882a593Smuzhiyun 			    struct dm_bio_prison_cell *cell)
1891*4882a593Smuzhiyun {
1892*4882a593Smuzhiyun 	int r;
1893*4882a593Smuzhiyun 	dm_block_t data_block;
1894*4882a593Smuzhiyun 	struct pool *pool = tc->pool;
1895*4882a593Smuzhiyun 
1896*4882a593Smuzhiyun 	/*
1897*4882a593Smuzhiyun 	 * Remap empty bios (flushes) immediately, without provisioning.
1898*4882a593Smuzhiyun 	 */
1899*4882a593Smuzhiyun 	if (!bio->bi_iter.bi_size) {
1900*4882a593Smuzhiyun 		inc_all_io_entry(pool, bio);
1901*4882a593Smuzhiyun 		cell_defer_no_holder(tc, cell);
1902*4882a593Smuzhiyun 
1903*4882a593Smuzhiyun 		remap_and_issue(tc, bio, 0);
1904*4882a593Smuzhiyun 		return;
1905*4882a593Smuzhiyun 	}
1906*4882a593Smuzhiyun 
1907*4882a593Smuzhiyun 	/*
1908*4882a593Smuzhiyun 	 * Fill read bios with zeroes and complete them immediately.
1909*4882a593Smuzhiyun 	 */
1910*4882a593Smuzhiyun 	if (bio_data_dir(bio) == READ) {
1911*4882a593Smuzhiyun 		zero_fill_bio(bio);
1912*4882a593Smuzhiyun 		cell_defer_no_holder(tc, cell);
1913*4882a593Smuzhiyun 		bio_endio(bio);
1914*4882a593Smuzhiyun 		return;
1915*4882a593Smuzhiyun 	}
1916*4882a593Smuzhiyun 
1917*4882a593Smuzhiyun 	r = alloc_data_block(tc, &data_block);
1918*4882a593Smuzhiyun 	switch (r) {
1919*4882a593Smuzhiyun 	case 0:
1920*4882a593Smuzhiyun 		if (tc->origin_dev)
1921*4882a593Smuzhiyun 			schedule_external_copy(tc, block, data_block, cell, bio);
1922*4882a593Smuzhiyun 		else
1923*4882a593Smuzhiyun 			schedule_zero(tc, block, data_block, cell, bio);
1924*4882a593Smuzhiyun 		break;
1925*4882a593Smuzhiyun 
1926*4882a593Smuzhiyun 	case -ENOSPC:
1927*4882a593Smuzhiyun 		retry_bios_on_resume(pool, cell);
1928*4882a593Smuzhiyun 		break;
1929*4882a593Smuzhiyun 
1930*4882a593Smuzhiyun 	default:
1931*4882a593Smuzhiyun 		DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1932*4882a593Smuzhiyun 			    __func__, r);
1933*4882a593Smuzhiyun 		cell_error(pool, cell);
1934*4882a593Smuzhiyun 		break;
1935*4882a593Smuzhiyun 	}
1936*4882a593Smuzhiyun }
1937*4882a593Smuzhiyun 
process_cell(struct thin_c * tc,struct dm_bio_prison_cell * cell)1938*4882a593Smuzhiyun static void process_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
1939*4882a593Smuzhiyun {
1940*4882a593Smuzhiyun 	int r;
1941*4882a593Smuzhiyun 	struct pool *pool = tc->pool;
1942*4882a593Smuzhiyun 	struct bio *bio = cell->holder;
1943*4882a593Smuzhiyun 	dm_block_t block = get_bio_block(tc, bio);
1944*4882a593Smuzhiyun 	struct dm_thin_lookup_result lookup_result;
1945*4882a593Smuzhiyun 
1946*4882a593Smuzhiyun 	if (tc->requeue_mode) {
1947*4882a593Smuzhiyun 		cell_requeue(pool, cell);
1948*4882a593Smuzhiyun 		return;
1949*4882a593Smuzhiyun 	}
1950*4882a593Smuzhiyun 
1951*4882a593Smuzhiyun 	r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1952*4882a593Smuzhiyun 	switch (r) {
1953*4882a593Smuzhiyun 	case 0:
1954*4882a593Smuzhiyun 		if (lookup_result.shared)
1955*4882a593Smuzhiyun 			process_shared_bio(tc, bio, block, &lookup_result, cell);
1956*4882a593Smuzhiyun 		else {
1957*4882a593Smuzhiyun 			inc_all_io_entry(pool, bio);
1958*4882a593Smuzhiyun 			remap_and_issue(tc, bio, lookup_result.block);
1959*4882a593Smuzhiyun 			inc_remap_and_issue_cell(tc, cell, lookup_result.block);
1960*4882a593Smuzhiyun 		}
1961*4882a593Smuzhiyun 		break;
1962*4882a593Smuzhiyun 
1963*4882a593Smuzhiyun 	case -ENODATA:
1964*4882a593Smuzhiyun 		if (bio_data_dir(bio) == READ && tc->origin_dev) {
1965*4882a593Smuzhiyun 			inc_all_io_entry(pool, bio);
1966*4882a593Smuzhiyun 			cell_defer_no_holder(tc, cell);
1967*4882a593Smuzhiyun 
1968*4882a593Smuzhiyun 			if (bio_end_sector(bio) <= tc->origin_size)
1969*4882a593Smuzhiyun 				remap_to_origin_and_issue(tc, bio);
1970*4882a593Smuzhiyun 
1971*4882a593Smuzhiyun 			else if (bio->bi_iter.bi_sector < tc->origin_size) {
1972*4882a593Smuzhiyun 				zero_fill_bio(bio);
1973*4882a593Smuzhiyun 				bio->bi_iter.bi_size = (tc->origin_size - bio->bi_iter.bi_sector) << SECTOR_SHIFT;
1974*4882a593Smuzhiyun 				remap_to_origin_and_issue(tc, bio);
1975*4882a593Smuzhiyun 
1976*4882a593Smuzhiyun 			} else {
1977*4882a593Smuzhiyun 				zero_fill_bio(bio);
1978*4882a593Smuzhiyun 				bio_endio(bio);
1979*4882a593Smuzhiyun 			}
1980*4882a593Smuzhiyun 		} else
1981*4882a593Smuzhiyun 			provision_block(tc, bio, block, cell);
1982*4882a593Smuzhiyun 		break;
1983*4882a593Smuzhiyun 
1984*4882a593Smuzhiyun 	default:
1985*4882a593Smuzhiyun 		DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1986*4882a593Smuzhiyun 			    __func__, r);
1987*4882a593Smuzhiyun 		cell_defer_no_holder(tc, cell);
1988*4882a593Smuzhiyun 		bio_io_error(bio);
1989*4882a593Smuzhiyun 		break;
1990*4882a593Smuzhiyun 	}
1991*4882a593Smuzhiyun }
1992*4882a593Smuzhiyun 
process_bio(struct thin_c * tc,struct bio * bio)1993*4882a593Smuzhiyun static void process_bio(struct thin_c *tc, struct bio *bio)
1994*4882a593Smuzhiyun {
1995*4882a593Smuzhiyun 	struct pool *pool = tc->pool;
1996*4882a593Smuzhiyun 	dm_block_t block = get_bio_block(tc, bio);
1997*4882a593Smuzhiyun 	struct dm_bio_prison_cell *cell;
1998*4882a593Smuzhiyun 	struct dm_cell_key key;
1999*4882a593Smuzhiyun 
2000*4882a593Smuzhiyun 	/*
2001*4882a593Smuzhiyun 	 * If cell is already occupied, then the block is already
2002*4882a593Smuzhiyun 	 * being provisioned so we have nothing further to do here.
2003*4882a593Smuzhiyun 	 */
2004*4882a593Smuzhiyun 	build_virtual_key(tc->td, block, &key);
2005*4882a593Smuzhiyun 	if (bio_detain(pool, &key, bio, &cell))
2006*4882a593Smuzhiyun 		return;
2007*4882a593Smuzhiyun 
2008*4882a593Smuzhiyun 	process_cell(tc, cell);
2009*4882a593Smuzhiyun }
2010*4882a593Smuzhiyun 
__process_bio_read_only(struct thin_c * tc,struct bio * bio,struct dm_bio_prison_cell * cell)2011*4882a593Smuzhiyun static void __process_bio_read_only(struct thin_c *tc, struct bio *bio,
2012*4882a593Smuzhiyun 				    struct dm_bio_prison_cell *cell)
2013*4882a593Smuzhiyun {
2014*4882a593Smuzhiyun 	int r;
2015*4882a593Smuzhiyun 	int rw = bio_data_dir(bio);
2016*4882a593Smuzhiyun 	dm_block_t block = get_bio_block(tc, bio);
2017*4882a593Smuzhiyun 	struct dm_thin_lookup_result lookup_result;
2018*4882a593Smuzhiyun 
2019*4882a593Smuzhiyun 	r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
2020*4882a593Smuzhiyun 	switch (r) {
2021*4882a593Smuzhiyun 	case 0:
2022*4882a593Smuzhiyun 		if (lookup_result.shared && (rw == WRITE) && bio->bi_iter.bi_size) {
2023*4882a593Smuzhiyun 			handle_unserviceable_bio(tc->pool, bio);
2024*4882a593Smuzhiyun 			if (cell)
2025*4882a593Smuzhiyun 				cell_defer_no_holder(tc, cell);
2026*4882a593Smuzhiyun 		} else {
2027*4882a593Smuzhiyun 			inc_all_io_entry(tc->pool, bio);
2028*4882a593Smuzhiyun 			remap_and_issue(tc, bio, lookup_result.block);
2029*4882a593Smuzhiyun 			if (cell)
2030*4882a593Smuzhiyun 				inc_remap_and_issue_cell(tc, cell, lookup_result.block);
2031*4882a593Smuzhiyun 		}
2032*4882a593Smuzhiyun 		break;
2033*4882a593Smuzhiyun 
2034*4882a593Smuzhiyun 	case -ENODATA:
2035*4882a593Smuzhiyun 		if (cell)
2036*4882a593Smuzhiyun 			cell_defer_no_holder(tc, cell);
2037*4882a593Smuzhiyun 		if (rw != READ) {
2038*4882a593Smuzhiyun 			handle_unserviceable_bio(tc->pool, bio);
2039*4882a593Smuzhiyun 			break;
2040*4882a593Smuzhiyun 		}
2041*4882a593Smuzhiyun 
2042*4882a593Smuzhiyun 		if (tc->origin_dev) {
2043*4882a593Smuzhiyun 			inc_all_io_entry(tc->pool, bio);
2044*4882a593Smuzhiyun 			remap_to_origin_and_issue(tc, bio);
2045*4882a593Smuzhiyun 			break;
2046*4882a593Smuzhiyun 		}
2047*4882a593Smuzhiyun 
2048*4882a593Smuzhiyun 		zero_fill_bio(bio);
2049*4882a593Smuzhiyun 		bio_endio(bio);
2050*4882a593Smuzhiyun 		break;
2051*4882a593Smuzhiyun 
2052*4882a593Smuzhiyun 	default:
2053*4882a593Smuzhiyun 		DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
2054*4882a593Smuzhiyun 			    __func__, r);
2055*4882a593Smuzhiyun 		if (cell)
2056*4882a593Smuzhiyun 			cell_defer_no_holder(tc, cell);
2057*4882a593Smuzhiyun 		bio_io_error(bio);
2058*4882a593Smuzhiyun 		break;
2059*4882a593Smuzhiyun 	}
2060*4882a593Smuzhiyun }
2061*4882a593Smuzhiyun 
process_bio_read_only(struct thin_c * tc,struct bio * bio)2062*4882a593Smuzhiyun static void process_bio_read_only(struct thin_c *tc, struct bio *bio)
2063*4882a593Smuzhiyun {
2064*4882a593Smuzhiyun 	__process_bio_read_only(tc, bio, NULL);
2065*4882a593Smuzhiyun }
2066*4882a593Smuzhiyun 
process_cell_read_only(struct thin_c * tc,struct dm_bio_prison_cell * cell)2067*4882a593Smuzhiyun static void process_cell_read_only(struct thin_c *tc, struct dm_bio_prison_cell *cell)
2068*4882a593Smuzhiyun {
2069*4882a593Smuzhiyun 	__process_bio_read_only(tc, cell->holder, cell);
2070*4882a593Smuzhiyun }
2071*4882a593Smuzhiyun 
process_bio_success(struct thin_c * tc,struct bio * bio)2072*4882a593Smuzhiyun static void process_bio_success(struct thin_c *tc, struct bio *bio)
2073*4882a593Smuzhiyun {
2074*4882a593Smuzhiyun 	bio_endio(bio);
2075*4882a593Smuzhiyun }
2076*4882a593Smuzhiyun 
process_bio_fail(struct thin_c * tc,struct bio * bio)2077*4882a593Smuzhiyun static void process_bio_fail(struct thin_c *tc, struct bio *bio)
2078*4882a593Smuzhiyun {
2079*4882a593Smuzhiyun 	bio_io_error(bio);
2080*4882a593Smuzhiyun }
2081*4882a593Smuzhiyun 
process_cell_success(struct thin_c * tc,struct dm_bio_prison_cell * cell)2082*4882a593Smuzhiyun static void process_cell_success(struct thin_c *tc, struct dm_bio_prison_cell *cell)
2083*4882a593Smuzhiyun {
2084*4882a593Smuzhiyun 	cell_success(tc->pool, cell);
2085*4882a593Smuzhiyun }
2086*4882a593Smuzhiyun 
process_cell_fail(struct thin_c * tc,struct dm_bio_prison_cell * cell)2087*4882a593Smuzhiyun static void process_cell_fail(struct thin_c *tc, struct dm_bio_prison_cell *cell)
2088*4882a593Smuzhiyun {
2089*4882a593Smuzhiyun 	cell_error(tc->pool, cell);
2090*4882a593Smuzhiyun }
2091*4882a593Smuzhiyun 
2092*4882a593Smuzhiyun /*
2093*4882a593Smuzhiyun  * FIXME: should we also commit due to size of transaction, measured in
2094*4882a593Smuzhiyun  * metadata blocks?
2095*4882a593Smuzhiyun  */
need_commit_due_to_time(struct pool * pool)2096*4882a593Smuzhiyun static int need_commit_due_to_time(struct pool *pool)
2097*4882a593Smuzhiyun {
2098*4882a593Smuzhiyun 	return !time_in_range(jiffies, pool->last_commit_jiffies,
2099*4882a593Smuzhiyun 			      pool->last_commit_jiffies + COMMIT_PERIOD);
2100*4882a593Smuzhiyun }
2101*4882a593Smuzhiyun 
2102*4882a593Smuzhiyun #define thin_pbd(node) rb_entry((node), struct dm_thin_endio_hook, rb_node)
2103*4882a593Smuzhiyun #define thin_bio(pbd) dm_bio_from_per_bio_data((pbd), sizeof(struct dm_thin_endio_hook))
2104*4882a593Smuzhiyun 
__thin_bio_rb_add(struct thin_c * tc,struct bio * bio)2105*4882a593Smuzhiyun static void __thin_bio_rb_add(struct thin_c *tc, struct bio *bio)
2106*4882a593Smuzhiyun {
2107*4882a593Smuzhiyun 	struct rb_node **rbp, *parent;
2108*4882a593Smuzhiyun 	struct dm_thin_endio_hook *pbd;
2109*4882a593Smuzhiyun 	sector_t bi_sector = bio->bi_iter.bi_sector;
2110*4882a593Smuzhiyun 
2111*4882a593Smuzhiyun 	rbp = &tc->sort_bio_list.rb_node;
2112*4882a593Smuzhiyun 	parent = NULL;
2113*4882a593Smuzhiyun 	while (*rbp) {
2114*4882a593Smuzhiyun 		parent = *rbp;
2115*4882a593Smuzhiyun 		pbd = thin_pbd(parent);
2116*4882a593Smuzhiyun 
2117*4882a593Smuzhiyun 		if (bi_sector < thin_bio(pbd)->bi_iter.bi_sector)
2118*4882a593Smuzhiyun 			rbp = &(*rbp)->rb_left;
2119*4882a593Smuzhiyun 		else
2120*4882a593Smuzhiyun 			rbp = &(*rbp)->rb_right;
2121*4882a593Smuzhiyun 	}
2122*4882a593Smuzhiyun 
2123*4882a593Smuzhiyun 	pbd = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
2124*4882a593Smuzhiyun 	rb_link_node(&pbd->rb_node, parent, rbp);
2125*4882a593Smuzhiyun 	rb_insert_color(&pbd->rb_node, &tc->sort_bio_list);
2126*4882a593Smuzhiyun }
2127*4882a593Smuzhiyun 
__extract_sorted_bios(struct thin_c * tc)2128*4882a593Smuzhiyun static void __extract_sorted_bios(struct thin_c *tc)
2129*4882a593Smuzhiyun {
2130*4882a593Smuzhiyun 	struct rb_node *node;
2131*4882a593Smuzhiyun 	struct dm_thin_endio_hook *pbd;
2132*4882a593Smuzhiyun 	struct bio *bio;
2133*4882a593Smuzhiyun 
2134*4882a593Smuzhiyun 	for (node = rb_first(&tc->sort_bio_list); node; node = rb_next(node)) {
2135*4882a593Smuzhiyun 		pbd = thin_pbd(node);
2136*4882a593Smuzhiyun 		bio = thin_bio(pbd);
2137*4882a593Smuzhiyun 
2138*4882a593Smuzhiyun 		bio_list_add(&tc->deferred_bio_list, bio);
2139*4882a593Smuzhiyun 		rb_erase(&pbd->rb_node, &tc->sort_bio_list);
2140*4882a593Smuzhiyun 	}
2141*4882a593Smuzhiyun 
2142*4882a593Smuzhiyun 	WARN_ON(!RB_EMPTY_ROOT(&tc->sort_bio_list));
2143*4882a593Smuzhiyun }
2144*4882a593Smuzhiyun 
__sort_thin_deferred_bios(struct thin_c * tc)2145*4882a593Smuzhiyun static void __sort_thin_deferred_bios(struct thin_c *tc)
2146*4882a593Smuzhiyun {
2147*4882a593Smuzhiyun 	struct bio *bio;
2148*4882a593Smuzhiyun 	struct bio_list bios;
2149*4882a593Smuzhiyun 
2150*4882a593Smuzhiyun 	bio_list_init(&bios);
2151*4882a593Smuzhiyun 	bio_list_merge(&bios, &tc->deferred_bio_list);
2152*4882a593Smuzhiyun 	bio_list_init(&tc->deferred_bio_list);
2153*4882a593Smuzhiyun 
2154*4882a593Smuzhiyun 	/* Sort deferred_bio_list using rb-tree */
2155*4882a593Smuzhiyun 	while ((bio = bio_list_pop(&bios)))
2156*4882a593Smuzhiyun 		__thin_bio_rb_add(tc, bio);
2157*4882a593Smuzhiyun 
2158*4882a593Smuzhiyun 	/*
2159*4882a593Smuzhiyun 	 * Transfer the sorted bios in sort_bio_list back to
2160*4882a593Smuzhiyun 	 * deferred_bio_list to allow lockless submission of
2161*4882a593Smuzhiyun 	 * all bios.
2162*4882a593Smuzhiyun 	 */
2163*4882a593Smuzhiyun 	__extract_sorted_bios(tc);
2164*4882a593Smuzhiyun }
2165*4882a593Smuzhiyun 
process_thin_deferred_bios(struct thin_c * tc)2166*4882a593Smuzhiyun static void process_thin_deferred_bios(struct thin_c *tc)
2167*4882a593Smuzhiyun {
2168*4882a593Smuzhiyun 	struct pool *pool = tc->pool;
2169*4882a593Smuzhiyun 	struct bio *bio;
2170*4882a593Smuzhiyun 	struct bio_list bios;
2171*4882a593Smuzhiyun 	struct blk_plug plug;
2172*4882a593Smuzhiyun 	unsigned count = 0;
2173*4882a593Smuzhiyun 
2174*4882a593Smuzhiyun 	if (tc->requeue_mode) {
2175*4882a593Smuzhiyun 		error_thin_bio_list(tc, &tc->deferred_bio_list,
2176*4882a593Smuzhiyun 				BLK_STS_DM_REQUEUE);
2177*4882a593Smuzhiyun 		return;
2178*4882a593Smuzhiyun 	}
2179*4882a593Smuzhiyun 
2180*4882a593Smuzhiyun 	bio_list_init(&bios);
2181*4882a593Smuzhiyun 
2182*4882a593Smuzhiyun 	spin_lock_irq(&tc->lock);
2183*4882a593Smuzhiyun 
2184*4882a593Smuzhiyun 	if (bio_list_empty(&tc->deferred_bio_list)) {
2185*4882a593Smuzhiyun 		spin_unlock_irq(&tc->lock);
2186*4882a593Smuzhiyun 		return;
2187*4882a593Smuzhiyun 	}
2188*4882a593Smuzhiyun 
2189*4882a593Smuzhiyun 	__sort_thin_deferred_bios(tc);
2190*4882a593Smuzhiyun 
2191*4882a593Smuzhiyun 	bio_list_merge(&bios, &tc->deferred_bio_list);
2192*4882a593Smuzhiyun 	bio_list_init(&tc->deferred_bio_list);
2193*4882a593Smuzhiyun 
2194*4882a593Smuzhiyun 	spin_unlock_irq(&tc->lock);
2195*4882a593Smuzhiyun 
2196*4882a593Smuzhiyun 	blk_start_plug(&plug);
2197*4882a593Smuzhiyun 	while ((bio = bio_list_pop(&bios))) {
2198*4882a593Smuzhiyun 		/*
2199*4882a593Smuzhiyun 		 * If we've got no free new_mapping structs, and processing
2200*4882a593Smuzhiyun 		 * this bio might require one, we pause until there are some
2201*4882a593Smuzhiyun 		 * prepared mappings to process.
2202*4882a593Smuzhiyun 		 */
2203*4882a593Smuzhiyun 		if (ensure_next_mapping(pool)) {
2204*4882a593Smuzhiyun 			spin_lock_irq(&tc->lock);
2205*4882a593Smuzhiyun 			bio_list_add(&tc->deferred_bio_list, bio);
2206*4882a593Smuzhiyun 			bio_list_merge(&tc->deferred_bio_list, &bios);
2207*4882a593Smuzhiyun 			spin_unlock_irq(&tc->lock);
2208*4882a593Smuzhiyun 			break;
2209*4882a593Smuzhiyun 		}
2210*4882a593Smuzhiyun 
2211*4882a593Smuzhiyun 		if (bio_op(bio) == REQ_OP_DISCARD)
2212*4882a593Smuzhiyun 			pool->process_discard(tc, bio);
2213*4882a593Smuzhiyun 		else
2214*4882a593Smuzhiyun 			pool->process_bio(tc, bio);
2215*4882a593Smuzhiyun 
2216*4882a593Smuzhiyun 		if ((count++ & 127) == 0) {
2217*4882a593Smuzhiyun 			throttle_work_update(&pool->throttle);
2218*4882a593Smuzhiyun 			dm_pool_issue_prefetches(pool->pmd);
2219*4882a593Smuzhiyun 		}
2220*4882a593Smuzhiyun 	}
2221*4882a593Smuzhiyun 	blk_finish_plug(&plug);
2222*4882a593Smuzhiyun }
2223*4882a593Smuzhiyun 
cmp_cells(const void * lhs,const void * rhs)2224*4882a593Smuzhiyun static int cmp_cells(const void *lhs, const void *rhs)
2225*4882a593Smuzhiyun {
2226*4882a593Smuzhiyun 	struct dm_bio_prison_cell *lhs_cell = *((struct dm_bio_prison_cell **) lhs);
2227*4882a593Smuzhiyun 	struct dm_bio_prison_cell *rhs_cell = *((struct dm_bio_prison_cell **) rhs);
2228*4882a593Smuzhiyun 
2229*4882a593Smuzhiyun 	BUG_ON(!lhs_cell->holder);
2230*4882a593Smuzhiyun 	BUG_ON(!rhs_cell->holder);
2231*4882a593Smuzhiyun 
2232*4882a593Smuzhiyun 	if (lhs_cell->holder->bi_iter.bi_sector < rhs_cell->holder->bi_iter.bi_sector)
2233*4882a593Smuzhiyun 		return -1;
2234*4882a593Smuzhiyun 
2235*4882a593Smuzhiyun 	if (lhs_cell->holder->bi_iter.bi_sector > rhs_cell->holder->bi_iter.bi_sector)
2236*4882a593Smuzhiyun 		return 1;
2237*4882a593Smuzhiyun 
2238*4882a593Smuzhiyun 	return 0;
2239*4882a593Smuzhiyun }
2240*4882a593Smuzhiyun 
sort_cells(struct pool * pool,struct list_head * cells)2241*4882a593Smuzhiyun static unsigned sort_cells(struct pool *pool, struct list_head *cells)
2242*4882a593Smuzhiyun {
2243*4882a593Smuzhiyun 	unsigned count = 0;
2244*4882a593Smuzhiyun 	struct dm_bio_prison_cell *cell, *tmp;
2245*4882a593Smuzhiyun 
2246*4882a593Smuzhiyun 	list_for_each_entry_safe(cell, tmp, cells, user_list) {
2247*4882a593Smuzhiyun 		if (count >= CELL_SORT_ARRAY_SIZE)
2248*4882a593Smuzhiyun 			break;
2249*4882a593Smuzhiyun 
2250*4882a593Smuzhiyun 		pool->cell_sort_array[count++] = cell;
2251*4882a593Smuzhiyun 		list_del(&cell->user_list);
2252*4882a593Smuzhiyun 	}
2253*4882a593Smuzhiyun 
2254*4882a593Smuzhiyun 	sort(pool->cell_sort_array, count, sizeof(cell), cmp_cells, NULL);
2255*4882a593Smuzhiyun 
2256*4882a593Smuzhiyun 	return count;
2257*4882a593Smuzhiyun }
2258*4882a593Smuzhiyun 
process_thin_deferred_cells(struct thin_c * tc)2259*4882a593Smuzhiyun static void process_thin_deferred_cells(struct thin_c *tc)
2260*4882a593Smuzhiyun {
2261*4882a593Smuzhiyun 	struct pool *pool = tc->pool;
2262*4882a593Smuzhiyun 	struct list_head cells;
2263*4882a593Smuzhiyun 	struct dm_bio_prison_cell *cell;
2264*4882a593Smuzhiyun 	unsigned i, j, count;
2265*4882a593Smuzhiyun 
2266*4882a593Smuzhiyun 	INIT_LIST_HEAD(&cells);
2267*4882a593Smuzhiyun 
2268*4882a593Smuzhiyun 	spin_lock_irq(&tc->lock);
2269*4882a593Smuzhiyun 	list_splice_init(&tc->deferred_cells, &cells);
2270*4882a593Smuzhiyun 	spin_unlock_irq(&tc->lock);
2271*4882a593Smuzhiyun 
2272*4882a593Smuzhiyun 	if (list_empty(&cells))
2273*4882a593Smuzhiyun 		return;
2274*4882a593Smuzhiyun 
2275*4882a593Smuzhiyun 	do {
2276*4882a593Smuzhiyun 		count = sort_cells(tc->pool, &cells);
2277*4882a593Smuzhiyun 
2278*4882a593Smuzhiyun 		for (i = 0; i < count; i++) {
2279*4882a593Smuzhiyun 			cell = pool->cell_sort_array[i];
2280*4882a593Smuzhiyun 			BUG_ON(!cell->holder);
2281*4882a593Smuzhiyun 
2282*4882a593Smuzhiyun 			/*
2283*4882a593Smuzhiyun 			 * If we've got no free new_mapping structs, and processing
2284*4882a593Smuzhiyun 			 * this bio might require one, we pause until there are some
2285*4882a593Smuzhiyun 			 * prepared mappings to process.
2286*4882a593Smuzhiyun 			 */
2287*4882a593Smuzhiyun 			if (ensure_next_mapping(pool)) {
2288*4882a593Smuzhiyun 				for (j = i; j < count; j++)
2289*4882a593Smuzhiyun 					list_add(&pool->cell_sort_array[j]->user_list, &cells);
2290*4882a593Smuzhiyun 
2291*4882a593Smuzhiyun 				spin_lock_irq(&tc->lock);
2292*4882a593Smuzhiyun 				list_splice(&cells, &tc->deferred_cells);
2293*4882a593Smuzhiyun 				spin_unlock_irq(&tc->lock);
2294*4882a593Smuzhiyun 				return;
2295*4882a593Smuzhiyun 			}
2296*4882a593Smuzhiyun 
2297*4882a593Smuzhiyun 			if (bio_op(cell->holder) == REQ_OP_DISCARD)
2298*4882a593Smuzhiyun 				pool->process_discard_cell(tc, cell);
2299*4882a593Smuzhiyun 			else
2300*4882a593Smuzhiyun 				pool->process_cell(tc, cell);
2301*4882a593Smuzhiyun 		}
2302*4882a593Smuzhiyun 	} while (!list_empty(&cells));
2303*4882a593Smuzhiyun }
2304*4882a593Smuzhiyun 
2305*4882a593Smuzhiyun static void thin_get(struct thin_c *tc);
2306*4882a593Smuzhiyun static void thin_put(struct thin_c *tc);
2307*4882a593Smuzhiyun 
2308*4882a593Smuzhiyun /*
2309*4882a593Smuzhiyun  * We can't hold rcu_read_lock() around code that can block.  So we
2310*4882a593Smuzhiyun  * find a thin with the rcu lock held; bump a refcount; then drop
2311*4882a593Smuzhiyun  * the lock.
2312*4882a593Smuzhiyun  */
get_first_thin(struct pool * pool)2313*4882a593Smuzhiyun static struct thin_c *get_first_thin(struct pool *pool)
2314*4882a593Smuzhiyun {
2315*4882a593Smuzhiyun 	struct thin_c *tc = NULL;
2316*4882a593Smuzhiyun 
2317*4882a593Smuzhiyun 	rcu_read_lock();
2318*4882a593Smuzhiyun 	if (!list_empty(&pool->active_thins)) {
2319*4882a593Smuzhiyun 		tc = list_entry_rcu(pool->active_thins.next, struct thin_c, list);
2320*4882a593Smuzhiyun 		thin_get(tc);
2321*4882a593Smuzhiyun 	}
2322*4882a593Smuzhiyun 	rcu_read_unlock();
2323*4882a593Smuzhiyun 
2324*4882a593Smuzhiyun 	return tc;
2325*4882a593Smuzhiyun }
2326*4882a593Smuzhiyun 
get_next_thin(struct pool * pool,struct thin_c * tc)2327*4882a593Smuzhiyun static struct thin_c *get_next_thin(struct pool *pool, struct thin_c *tc)
2328*4882a593Smuzhiyun {
2329*4882a593Smuzhiyun 	struct thin_c *old_tc = tc;
2330*4882a593Smuzhiyun 
2331*4882a593Smuzhiyun 	rcu_read_lock();
2332*4882a593Smuzhiyun 	list_for_each_entry_continue_rcu(tc, &pool->active_thins, list) {
2333*4882a593Smuzhiyun 		thin_get(tc);
2334*4882a593Smuzhiyun 		thin_put(old_tc);
2335*4882a593Smuzhiyun 		rcu_read_unlock();
2336*4882a593Smuzhiyun 		return tc;
2337*4882a593Smuzhiyun 	}
2338*4882a593Smuzhiyun 	thin_put(old_tc);
2339*4882a593Smuzhiyun 	rcu_read_unlock();
2340*4882a593Smuzhiyun 
2341*4882a593Smuzhiyun 	return NULL;
2342*4882a593Smuzhiyun }
2343*4882a593Smuzhiyun 
process_deferred_bios(struct pool * pool)2344*4882a593Smuzhiyun static void process_deferred_bios(struct pool *pool)
2345*4882a593Smuzhiyun {
2346*4882a593Smuzhiyun 	struct bio *bio;
2347*4882a593Smuzhiyun 	struct bio_list bios, bio_completions;
2348*4882a593Smuzhiyun 	struct thin_c *tc;
2349*4882a593Smuzhiyun 
2350*4882a593Smuzhiyun 	tc = get_first_thin(pool);
2351*4882a593Smuzhiyun 	while (tc) {
2352*4882a593Smuzhiyun 		process_thin_deferred_cells(tc);
2353*4882a593Smuzhiyun 		process_thin_deferred_bios(tc);
2354*4882a593Smuzhiyun 		tc = get_next_thin(pool, tc);
2355*4882a593Smuzhiyun 	}
2356*4882a593Smuzhiyun 
2357*4882a593Smuzhiyun 	/*
2358*4882a593Smuzhiyun 	 * If there are any deferred flush bios, we must commit the metadata
2359*4882a593Smuzhiyun 	 * before issuing them or signaling their completion.
2360*4882a593Smuzhiyun 	 */
2361*4882a593Smuzhiyun 	bio_list_init(&bios);
2362*4882a593Smuzhiyun 	bio_list_init(&bio_completions);
2363*4882a593Smuzhiyun 
2364*4882a593Smuzhiyun 	spin_lock_irq(&pool->lock);
2365*4882a593Smuzhiyun 	bio_list_merge(&bios, &pool->deferred_flush_bios);
2366*4882a593Smuzhiyun 	bio_list_init(&pool->deferred_flush_bios);
2367*4882a593Smuzhiyun 
2368*4882a593Smuzhiyun 	bio_list_merge(&bio_completions, &pool->deferred_flush_completions);
2369*4882a593Smuzhiyun 	bio_list_init(&pool->deferred_flush_completions);
2370*4882a593Smuzhiyun 	spin_unlock_irq(&pool->lock);
2371*4882a593Smuzhiyun 
2372*4882a593Smuzhiyun 	if (bio_list_empty(&bios) && bio_list_empty(&bio_completions) &&
2373*4882a593Smuzhiyun 	    !(dm_pool_changed_this_transaction(pool->pmd) && need_commit_due_to_time(pool)))
2374*4882a593Smuzhiyun 		return;
2375*4882a593Smuzhiyun 
2376*4882a593Smuzhiyun 	if (commit(pool)) {
2377*4882a593Smuzhiyun 		bio_list_merge(&bios, &bio_completions);
2378*4882a593Smuzhiyun 
2379*4882a593Smuzhiyun 		while ((bio = bio_list_pop(&bios)))
2380*4882a593Smuzhiyun 			bio_io_error(bio);
2381*4882a593Smuzhiyun 		return;
2382*4882a593Smuzhiyun 	}
2383*4882a593Smuzhiyun 	pool->last_commit_jiffies = jiffies;
2384*4882a593Smuzhiyun 
2385*4882a593Smuzhiyun 	while ((bio = bio_list_pop(&bio_completions)))
2386*4882a593Smuzhiyun 		bio_endio(bio);
2387*4882a593Smuzhiyun 
2388*4882a593Smuzhiyun 	while ((bio = bio_list_pop(&bios))) {
2389*4882a593Smuzhiyun 		/*
2390*4882a593Smuzhiyun 		 * The data device was flushed as part of metadata commit,
2391*4882a593Smuzhiyun 		 * so complete redundant flushes immediately.
2392*4882a593Smuzhiyun 		 */
2393*4882a593Smuzhiyun 		if (bio->bi_opf & REQ_PREFLUSH)
2394*4882a593Smuzhiyun 			bio_endio(bio);
2395*4882a593Smuzhiyun 		else
2396*4882a593Smuzhiyun 			submit_bio_noacct(bio);
2397*4882a593Smuzhiyun 	}
2398*4882a593Smuzhiyun }
2399*4882a593Smuzhiyun 
do_worker(struct work_struct * ws)2400*4882a593Smuzhiyun static void do_worker(struct work_struct *ws)
2401*4882a593Smuzhiyun {
2402*4882a593Smuzhiyun 	struct pool *pool = container_of(ws, struct pool, worker);
2403*4882a593Smuzhiyun 
2404*4882a593Smuzhiyun 	throttle_work_start(&pool->throttle);
2405*4882a593Smuzhiyun 	dm_pool_issue_prefetches(pool->pmd);
2406*4882a593Smuzhiyun 	throttle_work_update(&pool->throttle);
2407*4882a593Smuzhiyun 	process_prepared(pool, &pool->prepared_mappings, &pool->process_prepared_mapping);
2408*4882a593Smuzhiyun 	throttle_work_update(&pool->throttle);
2409*4882a593Smuzhiyun 	process_prepared(pool, &pool->prepared_discards, &pool->process_prepared_discard);
2410*4882a593Smuzhiyun 	throttle_work_update(&pool->throttle);
2411*4882a593Smuzhiyun 	process_prepared(pool, &pool->prepared_discards_pt2, &pool->process_prepared_discard_pt2);
2412*4882a593Smuzhiyun 	throttle_work_update(&pool->throttle);
2413*4882a593Smuzhiyun 	process_deferred_bios(pool);
2414*4882a593Smuzhiyun 	throttle_work_complete(&pool->throttle);
2415*4882a593Smuzhiyun }
2416*4882a593Smuzhiyun 
2417*4882a593Smuzhiyun /*
2418*4882a593Smuzhiyun  * We want to commit periodically so that not too much
2419*4882a593Smuzhiyun  * unwritten data builds up.
2420*4882a593Smuzhiyun  */
do_waker(struct work_struct * ws)2421*4882a593Smuzhiyun static void do_waker(struct work_struct *ws)
2422*4882a593Smuzhiyun {
2423*4882a593Smuzhiyun 	struct pool *pool = container_of(to_delayed_work(ws), struct pool, waker);
2424*4882a593Smuzhiyun 	wake_worker(pool);
2425*4882a593Smuzhiyun 	queue_delayed_work(pool->wq, &pool->waker, COMMIT_PERIOD);
2426*4882a593Smuzhiyun }
2427*4882a593Smuzhiyun 
2428*4882a593Smuzhiyun /*
2429*4882a593Smuzhiyun  * We're holding onto IO to allow userland time to react.  After the
2430*4882a593Smuzhiyun  * timeout either the pool will have been resized (and thus back in
2431*4882a593Smuzhiyun  * PM_WRITE mode), or we degrade to PM_OUT_OF_DATA_SPACE w/ error_if_no_space.
2432*4882a593Smuzhiyun  */
do_no_space_timeout(struct work_struct * ws)2433*4882a593Smuzhiyun static void do_no_space_timeout(struct work_struct *ws)
2434*4882a593Smuzhiyun {
2435*4882a593Smuzhiyun 	struct pool *pool = container_of(to_delayed_work(ws), struct pool,
2436*4882a593Smuzhiyun 					 no_space_timeout);
2437*4882a593Smuzhiyun 
2438*4882a593Smuzhiyun 	if (get_pool_mode(pool) == PM_OUT_OF_DATA_SPACE && !pool->pf.error_if_no_space) {
2439*4882a593Smuzhiyun 		pool->pf.error_if_no_space = true;
2440*4882a593Smuzhiyun 		notify_of_pool_mode_change(pool);
2441*4882a593Smuzhiyun 		error_retry_list_with_code(pool, BLK_STS_NOSPC);
2442*4882a593Smuzhiyun 	}
2443*4882a593Smuzhiyun }
2444*4882a593Smuzhiyun 
2445*4882a593Smuzhiyun /*----------------------------------------------------------------*/
2446*4882a593Smuzhiyun 
2447*4882a593Smuzhiyun struct pool_work {
2448*4882a593Smuzhiyun 	struct work_struct worker;
2449*4882a593Smuzhiyun 	struct completion complete;
2450*4882a593Smuzhiyun };
2451*4882a593Smuzhiyun 
to_pool_work(struct work_struct * ws)2452*4882a593Smuzhiyun static struct pool_work *to_pool_work(struct work_struct *ws)
2453*4882a593Smuzhiyun {
2454*4882a593Smuzhiyun 	return container_of(ws, struct pool_work, worker);
2455*4882a593Smuzhiyun }
2456*4882a593Smuzhiyun 
pool_work_complete(struct pool_work * pw)2457*4882a593Smuzhiyun static void pool_work_complete(struct pool_work *pw)
2458*4882a593Smuzhiyun {
2459*4882a593Smuzhiyun 	complete(&pw->complete);
2460*4882a593Smuzhiyun }
2461*4882a593Smuzhiyun 
pool_work_wait(struct pool_work * pw,struct pool * pool,void (* fn)(struct work_struct *))2462*4882a593Smuzhiyun static void pool_work_wait(struct pool_work *pw, struct pool *pool,
2463*4882a593Smuzhiyun 			   void (*fn)(struct work_struct *))
2464*4882a593Smuzhiyun {
2465*4882a593Smuzhiyun 	INIT_WORK_ONSTACK(&pw->worker, fn);
2466*4882a593Smuzhiyun 	init_completion(&pw->complete);
2467*4882a593Smuzhiyun 	queue_work(pool->wq, &pw->worker);
2468*4882a593Smuzhiyun 	wait_for_completion(&pw->complete);
2469*4882a593Smuzhiyun }
2470*4882a593Smuzhiyun 
2471*4882a593Smuzhiyun /*----------------------------------------------------------------*/
2472*4882a593Smuzhiyun 
2473*4882a593Smuzhiyun struct noflush_work {
2474*4882a593Smuzhiyun 	struct pool_work pw;
2475*4882a593Smuzhiyun 	struct thin_c *tc;
2476*4882a593Smuzhiyun };
2477*4882a593Smuzhiyun 
to_noflush(struct work_struct * ws)2478*4882a593Smuzhiyun static struct noflush_work *to_noflush(struct work_struct *ws)
2479*4882a593Smuzhiyun {
2480*4882a593Smuzhiyun 	return container_of(to_pool_work(ws), struct noflush_work, pw);
2481*4882a593Smuzhiyun }
2482*4882a593Smuzhiyun 
do_noflush_start(struct work_struct * ws)2483*4882a593Smuzhiyun static void do_noflush_start(struct work_struct *ws)
2484*4882a593Smuzhiyun {
2485*4882a593Smuzhiyun 	struct noflush_work *w = to_noflush(ws);
2486*4882a593Smuzhiyun 	w->tc->requeue_mode = true;
2487*4882a593Smuzhiyun 	requeue_io(w->tc);
2488*4882a593Smuzhiyun 	pool_work_complete(&w->pw);
2489*4882a593Smuzhiyun }
2490*4882a593Smuzhiyun 
do_noflush_stop(struct work_struct * ws)2491*4882a593Smuzhiyun static void do_noflush_stop(struct work_struct *ws)
2492*4882a593Smuzhiyun {
2493*4882a593Smuzhiyun 	struct noflush_work *w = to_noflush(ws);
2494*4882a593Smuzhiyun 	w->tc->requeue_mode = false;
2495*4882a593Smuzhiyun 	pool_work_complete(&w->pw);
2496*4882a593Smuzhiyun }
2497*4882a593Smuzhiyun 
noflush_work(struct thin_c * tc,void (* fn)(struct work_struct *))2498*4882a593Smuzhiyun static void noflush_work(struct thin_c *tc, void (*fn)(struct work_struct *))
2499*4882a593Smuzhiyun {
2500*4882a593Smuzhiyun 	struct noflush_work w;
2501*4882a593Smuzhiyun 
2502*4882a593Smuzhiyun 	w.tc = tc;
2503*4882a593Smuzhiyun 	pool_work_wait(&w.pw, tc->pool, fn);
2504*4882a593Smuzhiyun }
2505*4882a593Smuzhiyun 
2506*4882a593Smuzhiyun /*----------------------------------------------------------------*/
2507*4882a593Smuzhiyun 
passdown_enabled(struct pool_c * pt)2508*4882a593Smuzhiyun static bool passdown_enabled(struct pool_c *pt)
2509*4882a593Smuzhiyun {
2510*4882a593Smuzhiyun 	return pt->adjusted_pf.discard_passdown;
2511*4882a593Smuzhiyun }
2512*4882a593Smuzhiyun 
set_discard_callbacks(struct pool * pool)2513*4882a593Smuzhiyun static void set_discard_callbacks(struct pool *pool)
2514*4882a593Smuzhiyun {
2515*4882a593Smuzhiyun 	struct pool_c *pt = pool->ti->private;
2516*4882a593Smuzhiyun 
2517*4882a593Smuzhiyun 	if (passdown_enabled(pt)) {
2518*4882a593Smuzhiyun 		pool->process_discard_cell = process_discard_cell_passdown;
2519*4882a593Smuzhiyun 		pool->process_prepared_discard = process_prepared_discard_passdown_pt1;
2520*4882a593Smuzhiyun 		pool->process_prepared_discard_pt2 = process_prepared_discard_passdown_pt2;
2521*4882a593Smuzhiyun 	} else {
2522*4882a593Smuzhiyun 		pool->process_discard_cell = process_discard_cell_no_passdown;
2523*4882a593Smuzhiyun 		pool->process_prepared_discard = process_prepared_discard_no_passdown;
2524*4882a593Smuzhiyun 	}
2525*4882a593Smuzhiyun }
2526*4882a593Smuzhiyun 
set_pool_mode(struct pool * pool,enum pool_mode new_mode)2527*4882a593Smuzhiyun static void set_pool_mode(struct pool *pool, enum pool_mode new_mode)
2528*4882a593Smuzhiyun {
2529*4882a593Smuzhiyun 	struct pool_c *pt = pool->ti->private;
2530*4882a593Smuzhiyun 	bool needs_check = dm_pool_metadata_needs_check(pool->pmd);
2531*4882a593Smuzhiyun 	enum pool_mode old_mode = get_pool_mode(pool);
2532*4882a593Smuzhiyun 	unsigned long no_space_timeout = READ_ONCE(no_space_timeout_secs) * HZ;
2533*4882a593Smuzhiyun 
2534*4882a593Smuzhiyun 	/*
2535*4882a593Smuzhiyun 	 * Never allow the pool to transition to PM_WRITE mode if user
2536*4882a593Smuzhiyun 	 * intervention is required to verify metadata and data consistency.
2537*4882a593Smuzhiyun 	 */
2538*4882a593Smuzhiyun 	if (new_mode == PM_WRITE && needs_check) {
2539*4882a593Smuzhiyun 		DMERR("%s: unable to switch pool to write mode until repaired.",
2540*4882a593Smuzhiyun 		      dm_device_name(pool->pool_md));
2541*4882a593Smuzhiyun 		if (old_mode != new_mode)
2542*4882a593Smuzhiyun 			new_mode = old_mode;
2543*4882a593Smuzhiyun 		else
2544*4882a593Smuzhiyun 			new_mode = PM_READ_ONLY;
2545*4882a593Smuzhiyun 	}
2546*4882a593Smuzhiyun 	/*
2547*4882a593Smuzhiyun 	 * If we were in PM_FAIL mode, rollback of metadata failed.  We're
2548*4882a593Smuzhiyun 	 * not going to recover without a thin_repair.	So we never let the
2549*4882a593Smuzhiyun 	 * pool move out of the old mode.
2550*4882a593Smuzhiyun 	 */
2551*4882a593Smuzhiyun 	if (old_mode == PM_FAIL)
2552*4882a593Smuzhiyun 		new_mode = old_mode;
2553*4882a593Smuzhiyun 
2554*4882a593Smuzhiyun 	switch (new_mode) {
2555*4882a593Smuzhiyun 	case PM_FAIL:
2556*4882a593Smuzhiyun 		dm_pool_metadata_read_only(pool->pmd);
2557*4882a593Smuzhiyun 		pool->process_bio = process_bio_fail;
2558*4882a593Smuzhiyun 		pool->process_discard = process_bio_fail;
2559*4882a593Smuzhiyun 		pool->process_cell = process_cell_fail;
2560*4882a593Smuzhiyun 		pool->process_discard_cell = process_cell_fail;
2561*4882a593Smuzhiyun 		pool->process_prepared_mapping = process_prepared_mapping_fail;
2562*4882a593Smuzhiyun 		pool->process_prepared_discard = process_prepared_discard_fail;
2563*4882a593Smuzhiyun 
2564*4882a593Smuzhiyun 		error_retry_list(pool);
2565*4882a593Smuzhiyun 		break;
2566*4882a593Smuzhiyun 
2567*4882a593Smuzhiyun 	case PM_OUT_OF_METADATA_SPACE:
2568*4882a593Smuzhiyun 	case PM_READ_ONLY:
2569*4882a593Smuzhiyun 		dm_pool_metadata_read_only(pool->pmd);
2570*4882a593Smuzhiyun 		pool->process_bio = process_bio_read_only;
2571*4882a593Smuzhiyun 		pool->process_discard = process_bio_success;
2572*4882a593Smuzhiyun 		pool->process_cell = process_cell_read_only;
2573*4882a593Smuzhiyun 		pool->process_discard_cell = process_cell_success;
2574*4882a593Smuzhiyun 		pool->process_prepared_mapping = process_prepared_mapping_fail;
2575*4882a593Smuzhiyun 		pool->process_prepared_discard = process_prepared_discard_success;
2576*4882a593Smuzhiyun 
2577*4882a593Smuzhiyun 		error_retry_list(pool);
2578*4882a593Smuzhiyun 		break;
2579*4882a593Smuzhiyun 
2580*4882a593Smuzhiyun 	case PM_OUT_OF_DATA_SPACE:
2581*4882a593Smuzhiyun 		/*
2582*4882a593Smuzhiyun 		 * Ideally we'd never hit this state; the low water mark
2583*4882a593Smuzhiyun 		 * would trigger userland to extend the pool before we
2584*4882a593Smuzhiyun 		 * completely run out of data space.  However, many small
2585*4882a593Smuzhiyun 		 * IOs to unprovisioned space can consume data space at an
2586*4882a593Smuzhiyun 		 * alarming rate.  Adjust your low water mark if you're
2587*4882a593Smuzhiyun 		 * frequently seeing this mode.
2588*4882a593Smuzhiyun 		 */
2589*4882a593Smuzhiyun 		pool->out_of_data_space = true;
2590*4882a593Smuzhiyun 		pool->process_bio = process_bio_read_only;
2591*4882a593Smuzhiyun 		pool->process_discard = process_discard_bio;
2592*4882a593Smuzhiyun 		pool->process_cell = process_cell_read_only;
2593*4882a593Smuzhiyun 		pool->process_prepared_mapping = process_prepared_mapping;
2594*4882a593Smuzhiyun 		set_discard_callbacks(pool);
2595*4882a593Smuzhiyun 
2596*4882a593Smuzhiyun 		if (!pool->pf.error_if_no_space && no_space_timeout)
2597*4882a593Smuzhiyun 			queue_delayed_work(pool->wq, &pool->no_space_timeout, no_space_timeout);
2598*4882a593Smuzhiyun 		break;
2599*4882a593Smuzhiyun 
2600*4882a593Smuzhiyun 	case PM_WRITE:
2601*4882a593Smuzhiyun 		if (old_mode == PM_OUT_OF_DATA_SPACE)
2602*4882a593Smuzhiyun 			cancel_delayed_work_sync(&pool->no_space_timeout);
2603*4882a593Smuzhiyun 		pool->out_of_data_space = false;
2604*4882a593Smuzhiyun 		pool->pf.error_if_no_space = pt->requested_pf.error_if_no_space;
2605*4882a593Smuzhiyun 		dm_pool_metadata_read_write(pool->pmd);
2606*4882a593Smuzhiyun 		pool->process_bio = process_bio;
2607*4882a593Smuzhiyun 		pool->process_discard = process_discard_bio;
2608*4882a593Smuzhiyun 		pool->process_cell = process_cell;
2609*4882a593Smuzhiyun 		pool->process_prepared_mapping = process_prepared_mapping;
2610*4882a593Smuzhiyun 		set_discard_callbacks(pool);
2611*4882a593Smuzhiyun 		break;
2612*4882a593Smuzhiyun 	}
2613*4882a593Smuzhiyun 
2614*4882a593Smuzhiyun 	pool->pf.mode = new_mode;
2615*4882a593Smuzhiyun 	/*
2616*4882a593Smuzhiyun 	 * The pool mode may have changed, sync it so bind_control_target()
2617*4882a593Smuzhiyun 	 * doesn't cause an unexpected mode transition on resume.
2618*4882a593Smuzhiyun 	 */
2619*4882a593Smuzhiyun 	pt->adjusted_pf.mode = new_mode;
2620*4882a593Smuzhiyun 
2621*4882a593Smuzhiyun 	if (old_mode != new_mode)
2622*4882a593Smuzhiyun 		notify_of_pool_mode_change(pool);
2623*4882a593Smuzhiyun }
2624*4882a593Smuzhiyun 
abort_transaction(struct pool * pool)2625*4882a593Smuzhiyun static void abort_transaction(struct pool *pool)
2626*4882a593Smuzhiyun {
2627*4882a593Smuzhiyun 	const char *dev_name = dm_device_name(pool->pool_md);
2628*4882a593Smuzhiyun 
2629*4882a593Smuzhiyun 	DMERR_LIMIT("%s: aborting current metadata transaction", dev_name);
2630*4882a593Smuzhiyun 	if (dm_pool_abort_metadata(pool->pmd)) {
2631*4882a593Smuzhiyun 		DMERR("%s: failed to abort metadata transaction", dev_name);
2632*4882a593Smuzhiyun 		set_pool_mode(pool, PM_FAIL);
2633*4882a593Smuzhiyun 	}
2634*4882a593Smuzhiyun 
2635*4882a593Smuzhiyun 	if (dm_pool_metadata_set_needs_check(pool->pmd)) {
2636*4882a593Smuzhiyun 		DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name);
2637*4882a593Smuzhiyun 		set_pool_mode(pool, PM_FAIL);
2638*4882a593Smuzhiyun 	}
2639*4882a593Smuzhiyun }
2640*4882a593Smuzhiyun 
metadata_operation_failed(struct pool * pool,const char * op,int r)2641*4882a593Smuzhiyun static void metadata_operation_failed(struct pool *pool, const char *op, int r)
2642*4882a593Smuzhiyun {
2643*4882a593Smuzhiyun 	DMERR_LIMIT("%s: metadata operation '%s' failed: error = %d",
2644*4882a593Smuzhiyun 		    dm_device_name(pool->pool_md), op, r);
2645*4882a593Smuzhiyun 
2646*4882a593Smuzhiyun 	abort_transaction(pool);
2647*4882a593Smuzhiyun 	set_pool_mode(pool, PM_READ_ONLY);
2648*4882a593Smuzhiyun }
2649*4882a593Smuzhiyun 
2650*4882a593Smuzhiyun /*----------------------------------------------------------------*/
2651*4882a593Smuzhiyun 
2652*4882a593Smuzhiyun /*
2653*4882a593Smuzhiyun  * Mapping functions.
2654*4882a593Smuzhiyun  */
2655*4882a593Smuzhiyun 
2656*4882a593Smuzhiyun /*
2657*4882a593Smuzhiyun  * Called only while mapping a thin bio to hand it over to the workqueue.
2658*4882a593Smuzhiyun  */
thin_defer_bio(struct thin_c * tc,struct bio * bio)2659*4882a593Smuzhiyun static void thin_defer_bio(struct thin_c *tc, struct bio *bio)
2660*4882a593Smuzhiyun {
2661*4882a593Smuzhiyun 	struct pool *pool = tc->pool;
2662*4882a593Smuzhiyun 
2663*4882a593Smuzhiyun 	spin_lock_irq(&tc->lock);
2664*4882a593Smuzhiyun 	bio_list_add(&tc->deferred_bio_list, bio);
2665*4882a593Smuzhiyun 	spin_unlock_irq(&tc->lock);
2666*4882a593Smuzhiyun 
2667*4882a593Smuzhiyun 	wake_worker(pool);
2668*4882a593Smuzhiyun }
2669*4882a593Smuzhiyun 
thin_defer_bio_with_throttle(struct thin_c * tc,struct bio * bio)2670*4882a593Smuzhiyun static void thin_defer_bio_with_throttle(struct thin_c *tc, struct bio *bio)
2671*4882a593Smuzhiyun {
2672*4882a593Smuzhiyun 	struct pool *pool = tc->pool;
2673*4882a593Smuzhiyun 
2674*4882a593Smuzhiyun 	throttle_lock(&pool->throttle);
2675*4882a593Smuzhiyun 	thin_defer_bio(tc, bio);
2676*4882a593Smuzhiyun 	throttle_unlock(&pool->throttle);
2677*4882a593Smuzhiyun }
2678*4882a593Smuzhiyun 
thin_defer_cell(struct thin_c * tc,struct dm_bio_prison_cell * cell)2679*4882a593Smuzhiyun static void thin_defer_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
2680*4882a593Smuzhiyun {
2681*4882a593Smuzhiyun 	struct pool *pool = tc->pool;
2682*4882a593Smuzhiyun 
2683*4882a593Smuzhiyun 	throttle_lock(&pool->throttle);
2684*4882a593Smuzhiyun 	spin_lock_irq(&tc->lock);
2685*4882a593Smuzhiyun 	list_add_tail(&cell->user_list, &tc->deferred_cells);
2686*4882a593Smuzhiyun 	spin_unlock_irq(&tc->lock);
2687*4882a593Smuzhiyun 	throttle_unlock(&pool->throttle);
2688*4882a593Smuzhiyun 
2689*4882a593Smuzhiyun 	wake_worker(pool);
2690*4882a593Smuzhiyun }
2691*4882a593Smuzhiyun 
thin_hook_bio(struct thin_c * tc,struct bio * bio)2692*4882a593Smuzhiyun static void thin_hook_bio(struct thin_c *tc, struct bio *bio)
2693*4882a593Smuzhiyun {
2694*4882a593Smuzhiyun 	struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
2695*4882a593Smuzhiyun 
2696*4882a593Smuzhiyun 	h->tc = tc;
2697*4882a593Smuzhiyun 	h->shared_read_entry = NULL;
2698*4882a593Smuzhiyun 	h->all_io_entry = NULL;
2699*4882a593Smuzhiyun 	h->overwrite_mapping = NULL;
2700*4882a593Smuzhiyun 	h->cell = NULL;
2701*4882a593Smuzhiyun }
2702*4882a593Smuzhiyun 
2703*4882a593Smuzhiyun /*
2704*4882a593Smuzhiyun  * Non-blocking function called from the thin target's map function.
2705*4882a593Smuzhiyun  */
thin_bio_map(struct dm_target * ti,struct bio * bio)2706*4882a593Smuzhiyun static int thin_bio_map(struct dm_target *ti, struct bio *bio)
2707*4882a593Smuzhiyun {
2708*4882a593Smuzhiyun 	int r;
2709*4882a593Smuzhiyun 	struct thin_c *tc = ti->private;
2710*4882a593Smuzhiyun 	dm_block_t block = get_bio_block(tc, bio);
2711*4882a593Smuzhiyun 	struct dm_thin_device *td = tc->td;
2712*4882a593Smuzhiyun 	struct dm_thin_lookup_result result;
2713*4882a593Smuzhiyun 	struct dm_bio_prison_cell *virt_cell, *data_cell;
2714*4882a593Smuzhiyun 	struct dm_cell_key key;
2715*4882a593Smuzhiyun 
2716*4882a593Smuzhiyun 	thin_hook_bio(tc, bio);
2717*4882a593Smuzhiyun 
2718*4882a593Smuzhiyun 	if (tc->requeue_mode) {
2719*4882a593Smuzhiyun 		bio->bi_status = BLK_STS_DM_REQUEUE;
2720*4882a593Smuzhiyun 		bio_endio(bio);
2721*4882a593Smuzhiyun 		return DM_MAPIO_SUBMITTED;
2722*4882a593Smuzhiyun 	}
2723*4882a593Smuzhiyun 
2724*4882a593Smuzhiyun 	if (get_pool_mode(tc->pool) == PM_FAIL) {
2725*4882a593Smuzhiyun 		bio_io_error(bio);
2726*4882a593Smuzhiyun 		return DM_MAPIO_SUBMITTED;
2727*4882a593Smuzhiyun 	}
2728*4882a593Smuzhiyun 
2729*4882a593Smuzhiyun 	if (op_is_flush(bio->bi_opf) || bio_op(bio) == REQ_OP_DISCARD) {
2730*4882a593Smuzhiyun 		thin_defer_bio_with_throttle(tc, bio);
2731*4882a593Smuzhiyun 		return DM_MAPIO_SUBMITTED;
2732*4882a593Smuzhiyun 	}
2733*4882a593Smuzhiyun 
2734*4882a593Smuzhiyun 	/*
2735*4882a593Smuzhiyun 	 * We must hold the virtual cell before doing the lookup, otherwise
2736*4882a593Smuzhiyun 	 * there's a race with discard.
2737*4882a593Smuzhiyun 	 */
2738*4882a593Smuzhiyun 	build_virtual_key(tc->td, block, &key);
2739*4882a593Smuzhiyun 	if (bio_detain(tc->pool, &key, bio, &virt_cell))
2740*4882a593Smuzhiyun 		return DM_MAPIO_SUBMITTED;
2741*4882a593Smuzhiyun 
2742*4882a593Smuzhiyun 	r = dm_thin_find_block(td, block, 0, &result);
2743*4882a593Smuzhiyun 
2744*4882a593Smuzhiyun 	/*
2745*4882a593Smuzhiyun 	 * Note that we defer readahead too.
2746*4882a593Smuzhiyun 	 */
2747*4882a593Smuzhiyun 	switch (r) {
2748*4882a593Smuzhiyun 	case 0:
2749*4882a593Smuzhiyun 		if (unlikely(result.shared)) {
2750*4882a593Smuzhiyun 			/*
2751*4882a593Smuzhiyun 			 * We have a race condition here between the
2752*4882a593Smuzhiyun 			 * result.shared value returned by the lookup and
2753*4882a593Smuzhiyun 			 * snapshot creation, which may cause new
2754*4882a593Smuzhiyun 			 * sharing.
2755*4882a593Smuzhiyun 			 *
2756*4882a593Smuzhiyun 			 * To avoid this always quiesce the origin before
2757*4882a593Smuzhiyun 			 * taking the snap.  You want to do this anyway to
2758*4882a593Smuzhiyun 			 * ensure a consistent application view
2759*4882a593Smuzhiyun 			 * (i.e. lockfs).
2760*4882a593Smuzhiyun 			 *
2761*4882a593Smuzhiyun 			 * More distant ancestors are irrelevant. The
2762*4882a593Smuzhiyun 			 * shared flag will be set in their case.
2763*4882a593Smuzhiyun 			 */
2764*4882a593Smuzhiyun 			thin_defer_cell(tc, virt_cell);
2765*4882a593Smuzhiyun 			return DM_MAPIO_SUBMITTED;
2766*4882a593Smuzhiyun 		}
2767*4882a593Smuzhiyun 
2768*4882a593Smuzhiyun 		build_data_key(tc->td, result.block, &key);
2769*4882a593Smuzhiyun 		if (bio_detain(tc->pool, &key, bio, &data_cell)) {
2770*4882a593Smuzhiyun 			cell_defer_no_holder(tc, virt_cell);
2771*4882a593Smuzhiyun 			return DM_MAPIO_SUBMITTED;
2772*4882a593Smuzhiyun 		}
2773*4882a593Smuzhiyun 
2774*4882a593Smuzhiyun 		inc_all_io_entry(tc->pool, bio);
2775*4882a593Smuzhiyun 		cell_defer_no_holder(tc, data_cell);
2776*4882a593Smuzhiyun 		cell_defer_no_holder(tc, virt_cell);
2777*4882a593Smuzhiyun 
2778*4882a593Smuzhiyun 		remap(tc, bio, result.block);
2779*4882a593Smuzhiyun 		return DM_MAPIO_REMAPPED;
2780*4882a593Smuzhiyun 
2781*4882a593Smuzhiyun 	case -ENODATA:
2782*4882a593Smuzhiyun 	case -EWOULDBLOCK:
2783*4882a593Smuzhiyun 		thin_defer_cell(tc, virt_cell);
2784*4882a593Smuzhiyun 		return DM_MAPIO_SUBMITTED;
2785*4882a593Smuzhiyun 
2786*4882a593Smuzhiyun 	default:
2787*4882a593Smuzhiyun 		/*
2788*4882a593Smuzhiyun 		 * Must always call bio_io_error on failure.
2789*4882a593Smuzhiyun 		 * dm_thin_find_block can fail with -EINVAL if the
2790*4882a593Smuzhiyun 		 * pool is switched to fail-io mode.
2791*4882a593Smuzhiyun 		 */
2792*4882a593Smuzhiyun 		bio_io_error(bio);
2793*4882a593Smuzhiyun 		cell_defer_no_holder(tc, virt_cell);
2794*4882a593Smuzhiyun 		return DM_MAPIO_SUBMITTED;
2795*4882a593Smuzhiyun 	}
2796*4882a593Smuzhiyun }
2797*4882a593Smuzhiyun 
requeue_bios(struct pool * pool)2798*4882a593Smuzhiyun static void requeue_bios(struct pool *pool)
2799*4882a593Smuzhiyun {
2800*4882a593Smuzhiyun 	struct thin_c *tc;
2801*4882a593Smuzhiyun 
2802*4882a593Smuzhiyun 	rcu_read_lock();
2803*4882a593Smuzhiyun 	list_for_each_entry_rcu(tc, &pool->active_thins, list) {
2804*4882a593Smuzhiyun 		spin_lock_irq(&tc->lock);
2805*4882a593Smuzhiyun 		bio_list_merge(&tc->deferred_bio_list, &tc->retry_on_resume_list);
2806*4882a593Smuzhiyun 		bio_list_init(&tc->retry_on_resume_list);
2807*4882a593Smuzhiyun 		spin_unlock_irq(&tc->lock);
2808*4882a593Smuzhiyun 	}
2809*4882a593Smuzhiyun 	rcu_read_unlock();
2810*4882a593Smuzhiyun }
2811*4882a593Smuzhiyun 
2812*4882a593Smuzhiyun /*----------------------------------------------------------------
2813*4882a593Smuzhiyun  * Binding of control targets to a pool object
2814*4882a593Smuzhiyun  *--------------------------------------------------------------*/
data_dev_supports_discard(struct pool_c * pt)2815*4882a593Smuzhiyun static bool data_dev_supports_discard(struct pool_c *pt)
2816*4882a593Smuzhiyun {
2817*4882a593Smuzhiyun 	struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
2818*4882a593Smuzhiyun 
2819*4882a593Smuzhiyun 	return q && blk_queue_discard(q);
2820*4882a593Smuzhiyun }
2821*4882a593Smuzhiyun 
is_factor(sector_t block_size,uint32_t n)2822*4882a593Smuzhiyun static bool is_factor(sector_t block_size, uint32_t n)
2823*4882a593Smuzhiyun {
2824*4882a593Smuzhiyun 	return !sector_div(block_size, n);
2825*4882a593Smuzhiyun }
2826*4882a593Smuzhiyun 
2827*4882a593Smuzhiyun /*
2828*4882a593Smuzhiyun  * If discard_passdown was enabled verify that the data device
2829*4882a593Smuzhiyun  * supports discards.  Disable discard_passdown if not.
2830*4882a593Smuzhiyun  */
disable_passdown_if_not_supported(struct pool_c * pt)2831*4882a593Smuzhiyun static void disable_passdown_if_not_supported(struct pool_c *pt)
2832*4882a593Smuzhiyun {
2833*4882a593Smuzhiyun 	struct pool *pool = pt->pool;
2834*4882a593Smuzhiyun 	struct block_device *data_bdev = pt->data_dev->bdev;
2835*4882a593Smuzhiyun 	struct queue_limits *data_limits = &bdev_get_queue(data_bdev)->limits;
2836*4882a593Smuzhiyun 	const char *reason = NULL;
2837*4882a593Smuzhiyun 	char buf[BDEVNAME_SIZE];
2838*4882a593Smuzhiyun 
2839*4882a593Smuzhiyun 	if (!pt->adjusted_pf.discard_passdown)
2840*4882a593Smuzhiyun 		return;
2841*4882a593Smuzhiyun 
2842*4882a593Smuzhiyun 	if (!data_dev_supports_discard(pt))
2843*4882a593Smuzhiyun 		reason = "discard unsupported";
2844*4882a593Smuzhiyun 
2845*4882a593Smuzhiyun 	else if (data_limits->max_discard_sectors < pool->sectors_per_block)
2846*4882a593Smuzhiyun 		reason = "max discard sectors smaller than a block";
2847*4882a593Smuzhiyun 
2848*4882a593Smuzhiyun 	if (reason) {
2849*4882a593Smuzhiyun 		DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev, buf), reason);
2850*4882a593Smuzhiyun 		pt->adjusted_pf.discard_passdown = false;
2851*4882a593Smuzhiyun 	}
2852*4882a593Smuzhiyun }
2853*4882a593Smuzhiyun 
bind_control_target(struct pool * pool,struct dm_target * ti)2854*4882a593Smuzhiyun static int bind_control_target(struct pool *pool, struct dm_target *ti)
2855*4882a593Smuzhiyun {
2856*4882a593Smuzhiyun 	struct pool_c *pt = ti->private;
2857*4882a593Smuzhiyun 
2858*4882a593Smuzhiyun 	/*
2859*4882a593Smuzhiyun 	 * We want to make sure that a pool in PM_FAIL mode is never upgraded.
2860*4882a593Smuzhiyun 	 */
2861*4882a593Smuzhiyun 	enum pool_mode old_mode = get_pool_mode(pool);
2862*4882a593Smuzhiyun 	enum pool_mode new_mode = pt->adjusted_pf.mode;
2863*4882a593Smuzhiyun 
2864*4882a593Smuzhiyun 	/*
2865*4882a593Smuzhiyun 	 * Don't change the pool's mode until set_pool_mode() below.
2866*4882a593Smuzhiyun 	 * Otherwise the pool's process_* function pointers may
2867*4882a593Smuzhiyun 	 * not match the desired pool mode.
2868*4882a593Smuzhiyun 	 */
2869*4882a593Smuzhiyun 	pt->adjusted_pf.mode = old_mode;
2870*4882a593Smuzhiyun 
2871*4882a593Smuzhiyun 	pool->ti = ti;
2872*4882a593Smuzhiyun 	pool->pf = pt->adjusted_pf;
2873*4882a593Smuzhiyun 	pool->low_water_blocks = pt->low_water_blocks;
2874*4882a593Smuzhiyun 
2875*4882a593Smuzhiyun 	set_pool_mode(pool, new_mode);
2876*4882a593Smuzhiyun 
2877*4882a593Smuzhiyun 	return 0;
2878*4882a593Smuzhiyun }
2879*4882a593Smuzhiyun 
unbind_control_target(struct pool * pool,struct dm_target * ti)2880*4882a593Smuzhiyun static void unbind_control_target(struct pool *pool, struct dm_target *ti)
2881*4882a593Smuzhiyun {
2882*4882a593Smuzhiyun 	if (pool->ti == ti)
2883*4882a593Smuzhiyun 		pool->ti = NULL;
2884*4882a593Smuzhiyun }
2885*4882a593Smuzhiyun 
2886*4882a593Smuzhiyun /*----------------------------------------------------------------
2887*4882a593Smuzhiyun  * Pool creation
2888*4882a593Smuzhiyun  *--------------------------------------------------------------*/
2889*4882a593Smuzhiyun /* Initialize pool features. */
pool_features_init(struct pool_features * pf)2890*4882a593Smuzhiyun static void pool_features_init(struct pool_features *pf)
2891*4882a593Smuzhiyun {
2892*4882a593Smuzhiyun 	pf->mode = PM_WRITE;
2893*4882a593Smuzhiyun 	pf->zero_new_blocks = true;
2894*4882a593Smuzhiyun 	pf->discard_enabled = true;
2895*4882a593Smuzhiyun 	pf->discard_passdown = true;
2896*4882a593Smuzhiyun 	pf->error_if_no_space = false;
2897*4882a593Smuzhiyun }
2898*4882a593Smuzhiyun 
__pool_destroy(struct pool * pool)2899*4882a593Smuzhiyun static void __pool_destroy(struct pool *pool)
2900*4882a593Smuzhiyun {
2901*4882a593Smuzhiyun 	__pool_table_remove(pool);
2902*4882a593Smuzhiyun 
2903*4882a593Smuzhiyun 	vfree(pool->cell_sort_array);
2904*4882a593Smuzhiyun 	if (dm_pool_metadata_close(pool->pmd) < 0)
2905*4882a593Smuzhiyun 		DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
2906*4882a593Smuzhiyun 
2907*4882a593Smuzhiyun 	dm_bio_prison_destroy(pool->prison);
2908*4882a593Smuzhiyun 	dm_kcopyd_client_destroy(pool->copier);
2909*4882a593Smuzhiyun 
2910*4882a593Smuzhiyun 	if (pool->wq)
2911*4882a593Smuzhiyun 		destroy_workqueue(pool->wq);
2912*4882a593Smuzhiyun 
2913*4882a593Smuzhiyun 	if (pool->next_mapping)
2914*4882a593Smuzhiyun 		mempool_free(pool->next_mapping, &pool->mapping_pool);
2915*4882a593Smuzhiyun 	mempool_exit(&pool->mapping_pool);
2916*4882a593Smuzhiyun 	bio_uninit(&pool->flush_bio);
2917*4882a593Smuzhiyun 	dm_deferred_set_destroy(pool->shared_read_ds);
2918*4882a593Smuzhiyun 	dm_deferred_set_destroy(pool->all_io_ds);
2919*4882a593Smuzhiyun 	kfree(pool);
2920*4882a593Smuzhiyun }
2921*4882a593Smuzhiyun 
2922*4882a593Smuzhiyun static struct kmem_cache *_new_mapping_cache;
2923*4882a593Smuzhiyun 
pool_create(struct mapped_device * pool_md,struct block_device * metadata_dev,struct block_device * data_dev,unsigned long block_size,int read_only,char ** error)2924*4882a593Smuzhiyun static struct pool *pool_create(struct mapped_device *pool_md,
2925*4882a593Smuzhiyun 				struct block_device *metadata_dev,
2926*4882a593Smuzhiyun 				struct block_device *data_dev,
2927*4882a593Smuzhiyun 				unsigned long block_size,
2928*4882a593Smuzhiyun 				int read_only, char **error)
2929*4882a593Smuzhiyun {
2930*4882a593Smuzhiyun 	int r;
2931*4882a593Smuzhiyun 	void *err_p;
2932*4882a593Smuzhiyun 	struct pool *pool;
2933*4882a593Smuzhiyun 	struct dm_pool_metadata *pmd;
2934*4882a593Smuzhiyun 	bool format_device = read_only ? false : true;
2935*4882a593Smuzhiyun 
2936*4882a593Smuzhiyun 	pmd = dm_pool_metadata_open(metadata_dev, block_size, format_device);
2937*4882a593Smuzhiyun 	if (IS_ERR(pmd)) {
2938*4882a593Smuzhiyun 		*error = "Error creating metadata object";
2939*4882a593Smuzhiyun 		return (struct pool *)pmd;
2940*4882a593Smuzhiyun 	}
2941*4882a593Smuzhiyun 
2942*4882a593Smuzhiyun 	pool = kzalloc(sizeof(*pool), GFP_KERNEL);
2943*4882a593Smuzhiyun 	if (!pool) {
2944*4882a593Smuzhiyun 		*error = "Error allocating memory for pool";
2945*4882a593Smuzhiyun 		err_p = ERR_PTR(-ENOMEM);
2946*4882a593Smuzhiyun 		goto bad_pool;
2947*4882a593Smuzhiyun 	}
2948*4882a593Smuzhiyun 
2949*4882a593Smuzhiyun 	pool->pmd = pmd;
2950*4882a593Smuzhiyun 	pool->sectors_per_block = block_size;
2951*4882a593Smuzhiyun 	if (block_size & (block_size - 1))
2952*4882a593Smuzhiyun 		pool->sectors_per_block_shift = -1;
2953*4882a593Smuzhiyun 	else
2954*4882a593Smuzhiyun 		pool->sectors_per_block_shift = __ffs(block_size);
2955*4882a593Smuzhiyun 	pool->low_water_blocks = 0;
2956*4882a593Smuzhiyun 	pool_features_init(&pool->pf);
2957*4882a593Smuzhiyun 	pool->prison = dm_bio_prison_create();
2958*4882a593Smuzhiyun 	if (!pool->prison) {
2959*4882a593Smuzhiyun 		*error = "Error creating pool's bio prison";
2960*4882a593Smuzhiyun 		err_p = ERR_PTR(-ENOMEM);
2961*4882a593Smuzhiyun 		goto bad_prison;
2962*4882a593Smuzhiyun 	}
2963*4882a593Smuzhiyun 
2964*4882a593Smuzhiyun 	pool->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
2965*4882a593Smuzhiyun 	if (IS_ERR(pool->copier)) {
2966*4882a593Smuzhiyun 		r = PTR_ERR(pool->copier);
2967*4882a593Smuzhiyun 		*error = "Error creating pool's kcopyd client";
2968*4882a593Smuzhiyun 		err_p = ERR_PTR(r);
2969*4882a593Smuzhiyun 		goto bad_kcopyd_client;
2970*4882a593Smuzhiyun 	}
2971*4882a593Smuzhiyun 
2972*4882a593Smuzhiyun 	/*
2973*4882a593Smuzhiyun 	 * Create singlethreaded workqueue that will service all devices
2974*4882a593Smuzhiyun 	 * that use this metadata.
2975*4882a593Smuzhiyun 	 */
2976*4882a593Smuzhiyun 	pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
2977*4882a593Smuzhiyun 	if (!pool->wq) {
2978*4882a593Smuzhiyun 		*error = "Error creating pool's workqueue";
2979*4882a593Smuzhiyun 		err_p = ERR_PTR(-ENOMEM);
2980*4882a593Smuzhiyun 		goto bad_wq;
2981*4882a593Smuzhiyun 	}
2982*4882a593Smuzhiyun 
2983*4882a593Smuzhiyun 	throttle_init(&pool->throttle);
2984*4882a593Smuzhiyun 	INIT_WORK(&pool->worker, do_worker);
2985*4882a593Smuzhiyun 	INIT_DELAYED_WORK(&pool->waker, do_waker);
2986*4882a593Smuzhiyun 	INIT_DELAYED_WORK(&pool->no_space_timeout, do_no_space_timeout);
2987*4882a593Smuzhiyun 	spin_lock_init(&pool->lock);
2988*4882a593Smuzhiyun 	bio_list_init(&pool->deferred_flush_bios);
2989*4882a593Smuzhiyun 	bio_list_init(&pool->deferred_flush_completions);
2990*4882a593Smuzhiyun 	INIT_LIST_HEAD(&pool->prepared_mappings);
2991*4882a593Smuzhiyun 	INIT_LIST_HEAD(&pool->prepared_discards);
2992*4882a593Smuzhiyun 	INIT_LIST_HEAD(&pool->prepared_discards_pt2);
2993*4882a593Smuzhiyun 	INIT_LIST_HEAD(&pool->active_thins);
2994*4882a593Smuzhiyun 	pool->low_water_triggered = false;
2995*4882a593Smuzhiyun 	pool->suspended = true;
2996*4882a593Smuzhiyun 	pool->out_of_data_space = false;
2997*4882a593Smuzhiyun 	bio_init(&pool->flush_bio, NULL, 0);
2998*4882a593Smuzhiyun 
2999*4882a593Smuzhiyun 	pool->shared_read_ds = dm_deferred_set_create();
3000*4882a593Smuzhiyun 	if (!pool->shared_read_ds) {
3001*4882a593Smuzhiyun 		*error = "Error creating pool's shared read deferred set";
3002*4882a593Smuzhiyun 		err_p = ERR_PTR(-ENOMEM);
3003*4882a593Smuzhiyun 		goto bad_shared_read_ds;
3004*4882a593Smuzhiyun 	}
3005*4882a593Smuzhiyun 
3006*4882a593Smuzhiyun 	pool->all_io_ds = dm_deferred_set_create();
3007*4882a593Smuzhiyun 	if (!pool->all_io_ds) {
3008*4882a593Smuzhiyun 		*error = "Error creating pool's all io deferred set";
3009*4882a593Smuzhiyun 		err_p = ERR_PTR(-ENOMEM);
3010*4882a593Smuzhiyun 		goto bad_all_io_ds;
3011*4882a593Smuzhiyun 	}
3012*4882a593Smuzhiyun 
3013*4882a593Smuzhiyun 	pool->next_mapping = NULL;
3014*4882a593Smuzhiyun 	r = mempool_init_slab_pool(&pool->mapping_pool, MAPPING_POOL_SIZE,
3015*4882a593Smuzhiyun 				   _new_mapping_cache);
3016*4882a593Smuzhiyun 	if (r) {
3017*4882a593Smuzhiyun 		*error = "Error creating pool's mapping mempool";
3018*4882a593Smuzhiyun 		err_p = ERR_PTR(r);
3019*4882a593Smuzhiyun 		goto bad_mapping_pool;
3020*4882a593Smuzhiyun 	}
3021*4882a593Smuzhiyun 
3022*4882a593Smuzhiyun 	pool->cell_sort_array =
3023*4882a593Smuzhiyun 		vmalloc(array_size(CELL_SORT_ARRAY_SIZE,
3024*4882a593Smuzhiyun 				   sizeof(*pool->cell_sort_array)));
3025*4882a593Smuzhiyun 	if (!pool->cell_sort_array) {
3026*4882a593Smuzhiyun 		*error = "Error allocating cell sort array";
3027*4882a593Smuzhiyun 		err_p = ERR_PTR(-ENOMEM);
3028*4882a593Smuzhiyun 		goto bad_sort_array;
3029*4882a593Smuzhiyun 	}
3030*4882a593Smuzhiyun 
3031*4882a593Smuzhiyun 	pool->ref_count = 1;
3032*4882a593Smuzhiyun 	pool->last_commit_jiffies = jiffies;
3033*4882a593Smuzhiyun 	pool->pool_md = pool_md;
3034*4882a593Smuzhiyun 	pool->md_dev = metadata_dev;
3035*4882a593Smuzhiyun 	pool->data_dev = data_dev;
3036*4882a593Smuzhiyun 	__pool_table_insert(pool);
3037*4882a593Smuzhiyun 
3038*4882a593Smuzhiyun 	return pool;
3039*4882a593Smuzhiyun 
3040*4882a593Smuzhiyun bad_sort_array:
3041*4882a593Smuzhiyun 	mempool_exit(&pool->mapping_pool);
3042*4882a593Smuzhiyun bad_mapping_pool:
3043*4882a593Smuzhiyun 	dm_deferred_set_destroy(pool->all_io_ds);
3044*4882a593Smuzhiyun bad_all_io_ds:
3045*4882a593Smuzhiyun 	dm_deferred_set_destroy(pool->shared_read_ds);
3046*4882a593Smuzhiyun bad_shared_read_ds:
3047*4882a593Smuzhiyun 	destroy_workqueue(pool->wq);
3048*4882a593Smuzhiyun bad_wq:
3049*4882a593Smuzhiyun 	dm_kcopyd_client_destroy(pool->copier);
3050*4882a593Smuzhiyun bad_kcopyd_client:
3051*4882a593Smuzhiyun 	dm_bio_prison_destroy(pool->prison);
3052*4882a593Smuzhiyun bad_prison:
3053*4882a593Smuzhiyun 	kfree(pool);
3054*4882a593Smuzhiyun bad_pool:
3055*4882a593Smuzhiyun 	if (dm_pool_metadata_close(pmd))
3056*4882a593Smuzhiyun 		DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
3057*4882a593Smuzhiyun 
3058*4882a593Smuzhiyun 	return err_p;
3059*4882a593Smuzhiyun }
3060*4882a593Smuzhiyun 
__pool_inc(struct pool * pool)3061*4882a593Smuzhiyun static void __pool_inc(struct pool *pool)
3062*4882a593Smuzhiyun {
3063*4882a593Smuzhiyun 	BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
3064*4882a593Smuzhiyun 	pool->ref_count++;
3065*4882a593Smuzhiyun }
3066*4882a593Smuzhiyun 
__pool_dec(struct pool * pool)3067*4882a593Smuzhiyun static void __pool_dec(struct pool *pool)
3068*4882a593Smuzhiyun {
3069*4882a593Smuzhiyun 	BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
3070*4882a593Smuzhiyun 	BUG_ON(!pool->ref_count);
3071*4882a593Smuzhiyun 	if (!--pool->ref_count)
3072*4882a593Smuzhiyun 		__pool_destroy(pool);
3073*4882a593Smuzhiyun }
3074*4882a593Smuzhiyun 
__pool_find(struct mapped_device * pool_md,struct block_device * metadata_dev,struct block_device * data_dev,unsigned long block_size,int read_only,char ** error,int * created)3075*4882a593Smuzhiyun static struct pool *__pool_find(struct mapped_device *pool_md,
3076*4882a593Smuzhiyun 				struct block_device *metadata_dev,
3077*4882a593Smuzhiyun 				struct block_device *data_dev,
3078*4882a593Smuzhiyun 				unsigned long block_size, int read_only,
3079*4882a593Smuzhiyun 				char **error, int *created)
3080*4882a593Smuzhiyun {
3081*4882a593Smuzhiyun 	struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev);
3082*4882a593Smuzhiyun 
3083*4882a593Smuzhiyun 	if (pool) {
3084*4882a593Smuzhiyun 		if (pool->pool_md != pool_md) {
3085*4882a593Smuzhiyun 			*error = "metadata device already in use by a pool";
3086*4882a593Smuzhiyun 			return ERR_PTR(-EBUSY);
3087*4882a593Smuzhiyun 		}
3088*4882a593Smuzhiyun 		if (pool->data_dev != data_dev) {
3089*4882a593Smuzhiyun 			*error = "data device already in use by a pool";
3090*4882a593Smuzhiyun 			return ERR_PTR(-EBUSY);
3091*4882a593Smuzhiyun 		}
3092*4882a593Smuzhiyun 		__pool_inc(pool);
3093*4882a593Smuzhiyun 
3094*4882a593Smuzhiyun 	} else {
3095*4882a593Smuzhiyun 		pool = __pool_table_lookup(pool_md);
3096*4882a593Smuzhiyun 		if (pool) {
3097*4882a593Smuzhiyun 			if (pool->md_dev != metadata_dev || pool->data_dev != data_dev) {
3098*4882a593Smuzhiyun 				*error = "different pool cannot replace a pool";
3099*4882a593Smuzhiyun 				return ERR_PTR(-EINVAL);
3100*4882a593Smuzhiyun 			}
3101*4882a593Smuzhiyun 			__pool_inc(pool);
3102*4882a593Smuzhiyun 
3103*4882a593Smuzhiyun 		} else {
3104*4882a593Smuzhiyun 			pool = pool_create(pool_md, metadata_dev, data_dev, block_size, read_only, error);
3105*4882a593Smuzhiyun 			*created = 1;
3106*4882a593Smuzhiyun 		}
3107*4882a593Smuzhiyun 	}
3108*4882a593Smuzhiyun 
3109*4882a593Smuzhiyun 	return pool;
3110*4882a593Smuzhiyun }
3111*4882a593Smuzhiyun 
3112*4882a593Smuzhiyun /*----------------------------------------------------------------
3113*4882a593Smuzhiyun  * Pool target methods
3114*4882a593Smuzhiyun  *--------------------------------------------------------------*/
pool_dtr(struct dm_target * ti)3115*4882a593Smuzhiyun static void pool_dtr(struct dm_target *ti)
3116*4882a593Smuzhiyun {
3117*4882a593Smuzhiyun 	struct pool_c *pt = ti->private;
3118*4882a593Smuzhiyun 
3119*4882a593Smuzhiyun 	mutex_lock(&dm_thin_pool_table.mutex);
3120*4882a593Smuzhiyun 
3121*4882a593Smuzhiyun 	unbind_control_target(pt->pool, ti);
3122*4882a593Smuzhiyun 	__pool_dec(pt->pool);
3123*4882a593Smuzhiyun 	dm_put_device(ti, pt->metadata_dev);
3124*4882a593Smuzhiyun 	dm_put_device(ti, pt->data_dev);
3125*4882a593Smuzhiyun 	kfree(pt);
3126*4882a593Smuzhiyun 
3127*4882a593Smuzhiyun 	mutex_unlock(&dm_thin_pool_table.mutex);
3128*4882a593Smuzhiyun }
3129*4882a593Smuzhiyun 
parse_pool_features(struct dm_arg_set * as,struct pool_features * pf,struct dm_target * ti)3130*4882a593Smuzhiyun static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
3131*4882a593Smuzhiyun 			       struct dm_target *ti)
3132*4882a593Smuzhiyun {
3133*4882a593Smuzhiyun 	int r;
3134*4882a593Smuzhiyun 	unsigned argc;
3135*4882a593Smuzhiyun 	const char *arg_name;
3136*4882a593Smuzhiyun 
3137*4882a593Smuzhiyun 	static const struct dm_arg _args[] = {
3138*4882a593Smuzhiyun 		{0, 4, "Invalid number of pool feature arguments"},
3139*4882a593Smuzhiyun 	};
3140*4882a593Smuzhiyun 
3141*4882a593Smuzhiyun 	/*
3142*4882a593Smuzhiyun 	 * No feature arguments supplied.
3143*4882a593Smuzhiyun 	 */
3144*4882a593Smuzhiyun 	if (!as->argc)
3145*4882a593Smuzhiyun 		return 0;
3146*4882a593Smuzhiyun 
3147*4882a593Smuzhiyun 	r = dm_read_arg_group(_args, as, &argc, &ti->error);
3148*4882a593Smuzhiyun 	if (r)
3149*4882a593Smuzhiyun 		return -EINVAL;
3150*4882a593Smuzhiyun 
3151*4882a593Smuzhiyun 	while (argc && !r) {
3152*4882a593Smuzhiyun 		arg_name = dm_shift_arg(as);
3153*4882a593Smuzhiyun 		argc--;
3154*4882a593Smuzhiyun 
3155*4882a593Smuzhiyun 		if (!strcasecmp(arg_name, "skip_block_zeroing"))
3156*4882a593Smuzhiyun 			pf->zero_new_blocks = false;
3157*4882a593Smuzhiyun 
3158*4882a593Smuzhiyun 		else if (!strcasecmp(arg_name, "ignore_discard"))
3159*4882a593Smuzhiyun 			pf->discard_enabled = false;
3160*4882a593Smuzhiyun 
3161*4882a593Smuzhiyun 		else if (!strcasecmp(arg_name, "no_discard_passdown"))
3162*4882a593Smuzhiyun 			pf->discard_passdown = false;
3163*4882a593Smuzhiyun 
3164*4882a593Smuzhiyun 		else if (!strcasecmp(arg_name, "read_only"))
3165*4882a593Smuzhiyun 			pf->mode = PM_READ_ONLY;
3166*4882a593Smuzhiyun 
3167*4882a593Smuzhiyun 		else if (!strcasecmp(arg_name, "error_if_no_space"))
3168*4882a593Smuzhiyun 			pf->error_if_no_space = true;
3169*4882a593Smuzhiyun 
3170*4882a593Smuzhiyun 		else {
3171*4882a593Smuzhiyun 			ti->error = "Unrecognised pool feature requested";
3172*4882a593Smuzhiyun 			r = -EINVAL;
3173*4882a593Smuzhiyun 			break;
3174*4882a593Smuzhiyun 		}
3175*4882a593Smuzhiyun 	}
3176*4882a593Smuzhiyun 
3177*4882a593Smuzhiyun 	return r;
3178*4882a593Smuzhiyun }
3179*4882a593Smuzhiyun 
metadata_low_callback(void * context)3180*4882a593Smuzhiyun static void metadata_low_callback(void *context)
3181*4882a593Smuzhiyun {
3182*4882a593Smuzhiyun 	struct pool *pool = context;
3183*4882a593Smuzhiyun 
3184*4882a593Smuzhiyun 	DMWARN("%s: reached low water mark for metadata device: sending event.",
3185*4882a593Smuzhiyun 	       dm_device_name(pool->pool_md));
3186*4882a593Smuzhiyun 
3187*4882a593Smuzhiyun 	dm_table_event(pool->ti->table);
3188*4882a593Smuzhiyun }
3189*4882a593Smuzhiyun 
3190*4882a593Smuzhiyun /*
3191*4882a593Smuzhiyun  * We need to flush the data device **before** committing the metadata.
3192*4882a593Smuzhiyun  *
3193*4882a593Smuzhiyun  * This ensures that the data blocks of any newly inserted mappings are
3194*4882a593Smuzhiyun  * properly written to non-volatile storage and won't be lost in case of a
3195*4882a593Smuzhiyun  * crash.
3196*4882a593Smuzhiyun  *
3197*4882a593Smuzhiyun  * Failure to do so can result in data corruption in the case of internal or
3198*4882a593Smuzhiyun  * external snapshots and in the case of newly provisioned blocks, when block
3199*4882a593Smuzhiyun  * zeroing is enabled.
3200*4882a593Smuzhiyun  */
metadata_pre_commit_callback(void * context)3201*4882a593Smuzhiyun static int metadata_pre_commit_callback(void *context)
3202*4882a593Smuzhiyun {
3203*4882a593Smuzhiyun 	struct pool *pool = context;
3204*4882a593Smuzhiyun 	struct bio *flush_bio = &pool->flush_bio;
3205*4882a593Smuzhiyun 
3206*4882a593Smuzhiyun 	bio_reset(flush_bio);
3207*4882a593Smuzhiyun 	bio_set_dev(flush_bio, pool->data_dev);
3208*4882a593Smuzhiyun 	flush_bio->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
3209*4882a593Smuzhiyun 
3210*4882a593Smuzhiyun 	return submit_bio_wait(flush_bio);
3211*4882a593Smuzhiyun }
3212*4882a593Smuzhiyun 
get_dev_size(struct block_device * bdev)3213*4882a593Smuzhiyun static sector_t get_dev_size(struct block_device *bdev)
3214*4882a593Smuzhiyun {
3215*4882a593Smuzhiyun 	return i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
3216*4882a593Smuzhiyun }
3217*4882a593Smuzhiyun 
warn_if_metadata_device_too_big(struct block_device * bdev)3218*4882a593Smuzhiyun static void warn_if_metadata_device_too_big(struct block_device *bdev)
3219*4882a593Smuzhiyun {
3220*4882a593Smuzhiyun 	sector_t metadata_dev_size = get_dev_size(bdev);
3221*4882a593Smuzhiyun 	char buffer[BDEVNAME_SIZE];
3222*4882a593Smuzhiyun 
3223*4882a593Smuzhiyun 	if (metadata_dev_size > THIN_METADATA_MAX_SECTORS_WARNING)
3224*4882a593Smuzhiyun 		DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
3225*4882a593Smuzhiyun 		       bdevname(bdev, buffer), THIN_METADATA_MAX_SECTORS);
3226*4882a593Smuzhiyun }
3227*4882a593Smuzhiyun 
get_metadata_dev_size(struct block_device * bdev)3228*4882a593Smuzhiyun static sector_t get_metadata_dev_size(struct block_device *bdev)
3229*4882a593Smuzhiyun {
3230*4882a593Smuzhiyun 	sector_t metadata_dev_size = get_dev_size(bdev);
3231*4882a593Smuzhiyun 
3232*4882a593Smuzhiyun 	if (metadata_dev_size > THIN_METADATA_MAX_SECTORS)
3233*4882a593Smuzhiyun 		metadata_dev_size = THIN_METADATA_MAX_SECTORS;
3234*4882a593Smuzhiyun 
3235*4882a593Smuzhiyun 	return metadata_dev_size;
3236*4882a593Smuzhiyun }
3237*4882a593Smuzhiyun 
get_metadata_dev_size_in_blocks(struct block_device * bdev)3238*4882a593Smuzhiyun static dm_block_t get_metadata_dev_size_in_blocks(struct block_device *bdev)
3239*4882a593Smuzhiyun {
3240*4882a593Smuzhiyun 	sector_t metadata_dev_size = get_metadata_dev_size(bdev);
3241*4882a593Smuzhiyun 
3242*4882a593Smuzhiyun 	sector_div(metadata_dev_size, THIN_METADATA_BLOCK_SIZE);
3243*4882a593Smuzhiyun 
3244*4882a593Smuzhiyun 	return metadata_dev_size;
3245*4882a593Smuzhiyun }
3246*4882a593Smuzhiyun 
3247*4882a593Smuzhiyun /*
3248*4882a593Smuzhiyun  * When a metadata threshold is crossed a dm event is triggered, and
3249*4882a593Smuzhiyun  * userland should respond by growing the metadata device.  We could let
3250*4882a593Smuzhiyun  * userland set the threshold, like we do with the data threshold, but I'm
3251*4882a593Smuzhiyun  * not sure they know enough to do this well.
3252*4882a593Smuzhiyun  */
calc_metadata_threshold(struct pool_c * pt)3253*4882a593Smuzhiyun static dm_block_t calc_metadata_threshold(struct pool_c *pt)
3254*4882a593Smuzhiyun {
3255*4882a593Smuzhiyun 	/*
3256*4882a593Smuzhiyun 	 * 4M is ample for all ops with the possible exception of thin
3257*4882a593Smuzhiyun 	 * device deletion which is harmless if it fails (just retry the
3258*4882a593Smuzhiyun 	 * delete after you've grown the device).
3259*4882a593Smuzhiyun 	 */
3260*4882a593Smuzhiyun 	dm_block_t quarter = get_metadata_dev_size_in_blocks(pt->metadata_dev->bdev) / 4;
3261*4882a593Smuzhiyun 	return min((dm_block_t)1024ULL /* 4M */, quarter);
3262*4882a593Smuzhiyun }
3263*4882a593Smuzhiyun 
3264*4882a593Smuzhiyun /*
3265*4882a593Smuzhiyun  * thin-pool <metadata dev> <data dev>
3266*4882a593Smuzhiyun  *	     <data block size (sectors)>
3267*4882a593Smuzhiyun  *	     <low water mark (blocks)>
3268*4882a593Smuzhiyun  *	     [<#feature args> [<arg>]*]
3269*4882a593Smuzhiyun  *
3270*4882a593Smuzhiyun  * Optional feature arguments are:
3271*4882a593Smuzhiyun  *	     skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
3272*4882a593Smuzhiyun  *	     ignore_discard: disable discard
3273*4882a593Smuzhiyun  *	     no_discard_passdown: don't pass discards down to the data device
3274*4882a593Smuzhiyun  *	     read_only: Don't allow any changes to be made to the pool metadata.
3275*4882a593Smuzhiyun  *	     error_if_no_space: error IOs, instead of queueing, if no space.
3276*4882a593Smuzhiyun  */
pool_ctr(struct dm_target * ti,unsigned argc,char ** argv)3277*4882a593Smuzhiyun static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
3278*4882a593Smuzhiyun {
3279*4882a593Smuzhiyun 	int r, pool_created = 0;
3280*4882a593Smuzhiyun 	struct pool_c *pt;
3281*4882a593Smuzhiyun 	struct pool *pool;
3282*4882a593Smuzhiyun 	struct pool_features pf;
3283*4882a593Smuzhiyun 	struct dm_arg_set as;
3284*4882a593Smuzhiyun 	struct dm_dev *data_dev;
3285*4882a593Smuzhiyun 	unsigned long block_size;
3286*4882a593Smuzhiyun 	dm_block_t low_water_blocks;
3287*4882a593Smuzhiyun 	struct dm_dev *metadata_dev;
3288*4882a593Smuzhiyun 	fmode_t metadata_mode;
3289*4882a593Smuzhiyun 
3290*4882a593Smuzhiyun 	/*
3291*4882a593Smuzhiyun 	 * FIXME Remove validation from scope of lock.
3292*4882a593Smuzhiyun 	 */
3293*4882a593Smuzhiyun 	mutex_lock(&dm_thin_pool_table.mutex);
3294*4882a593Smuzhiyun 
3295*4882a593Smuzhiyun 	if (argc < 4) {
3296*4882a593Smuzhiyun 		ti->error = "Invalid argument count";
3297*4882a593Smuzhiyun 		r = -EINVAL;
3298*4882a593Smuzhiyun 		goto out_unlock;
3299*4882a593Smuzhiyun 	}
3300*4882a593Smuzhiyun 
3301*4882a593Smuzhiyun 	as.argc = argc;
3302*4882a593Smuzhiyun 	as.argv = argv;
3303*4882a593Smuzhiyun 
3304*4882a593Smuzhiyun 	/* make sure metadata and data are different devices */
3305*4882a593Smuzhiyun 	if (!strcmp(argv[0], argv[1])) {
3306*4882a593Smuzhiyun 		ti->error = "Error setting metadata or data device";
3307*4882a593Smuzhiyun 		r = -EINVAL;
3308*4882a593Smuzhiyun 		goto out_unlock;
3309*4882a593Smuzhiyun 	}
3310*4882a593Smuzhiyun 
3311*4882a593Smuzhiyun 	/*
3312*4882a593Smuzhiyun 	 * Set default pool features.
3313*4882a593Smuzhiyun 	 */
3314*4882a593Smuzhiyun 	pool_features_init(&pf);
3315*4882a593Smuzhiyun 
3316*4882a593Smuzhiyun 	dm_consume_args(&as, 4);
3317*4882a593Smuzhiyun 	r = parse_pool_features(&as, &pf, ti);
3318*4882a593Smuzhiyun 	if (r)
3319*4882a593Smuzhiyun 		goto out_unlock;
3320*4882a593Smuzhiyun 
3321*4882a593Smuzhiyun 	metadata_mode = FMODE_READ | ((pf.mode == PM_READ_ONLY) ? 0 : FMODE_WRITE);
3322*4882a593Smuzhiyun 	r = dm_get_device(ti, argv[0], metadata_mode, &metadata_dev);
3323*4882a593Smuzhiyun 	if (r) {
3324*4882a593Smuzhiyun 		ti->error = "Error opening metadata block device";
3325*4882a593Smuzhiyun 		goto out_unlock;
3326*4882a593Smuzhiyun 	}
3327*4882a593Smuzhiyun 	warn_if_metadata_device_too_big(metadata_dev->bdev);
3328*4882a593Smuzhiyun 
3329*4882a593Smuzhiyun 	r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &data_dev);
3330*4882a593Smuzhiyun 	if (r) {
3331*4882a593Smuzhiyun 		ti->error = "Error getting data device";
3332*4882a593Smuzhiyun 		goto out_metadata;
3333*4882a593Smuzhiyun 	}
3334*4882a593Smuzhiyun 
3335*4882a593Smuzhiyun 	if (kstrtoul(argv[2], 10, &block_size) || !block_size ||
3336*4882a593Smuzhiyun 	    block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
3337*4882a593Smuzhiyun 	    block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
3338*4882a593Smuzhiyun 	    block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
3339*4882a593Smuzhiyun 		ti->error = "Invalid block size";
3340*4882a593Smuzhiyun 		r = -EINVAL;
3341*4882a593Smuzhiyun 		goto out;
3342*4882a593Smuzhiyun 	}
3343*4882a593Smuzhiyun 
3344*4882a593Smuzhiyun 	if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) {
3345*4882a593Smuzhiyun 		ti->error = "Invalid low water mark";
3346*4882a593Smuzhiyun 		r = -EINVAL;
3347*4882a593Smuzhiyun 		goto out;
3348*4882a593Smuzhiyun 	}
3349*4882a593Smuzhiyun 
3350*4882a593Smuzhiyun 	pt = kzalloc(sizeof(*pt), GFP_KERNEL);
3351*4882a593Smuzhiyun 	if (!pt) {
3352*4882a593Smuzhiyun 		r = -ENOMEM;
3353*4882a593Smuzhiyun 		goto out;
3354*4882a593Smuzhiyun 	}
3355*4882a593Smuzhiyun 
3356*4882a593Smuzhiyun 	pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev, data_dev->bdev,
3357*4882a593Smuzhiyun 			   block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created);
3358*4882a593Smuzhiyun 	if (IS_ERR(pool)) {
3359*4882a593Smuzhiyun 		r = PTR_ERR(pool);
3360*4882a593Smuzhiyun 		goto out_free_pt;
3361*4882a593Smuzhiyun 	}
3362*4882a593Smuzhiyun 
3363*4882a593Smuzhiyun 	/*
3364*4882a593Smuzhiyun 	 * 'pool_created' reflects whether this is the first table load.
3365*4882a593Smuzhiyun 	 * Top level discard support is not allowed to be changed after
3366*4882a593Smuzhiyun 	 * initial load.  This would require a pool reload to trigger thin
3367*4882a593Smuzhiyun 	 * device changes.
3368*4882a593Smuzhiyun 	 */
3369*4882a593Smuzhiyun 	if (!pool_created && pf.discard_enabled != pool->pf.discard_enabled) {
3370*4882a593Smuzhiyun 		ti->error = "Discard support cannot be disabled once enabled";
3371*4882a593Smuzhiyun 		r = -EINVAL;
3372*4882a593Smuzhiyun 		goto out_flags_changed;
3373*4882a593Smuzhiyun 	}
3374*4882a593Smuzhiyun 
3375*4882a593Smuzhiyun 	pt->pool = pool;
3376*4882a593Smuzhiyun 	pt->ti = ti;
3377*4882a593Smuzhiyun 	pt->metadata_dev = metadata_dev;
3378*4882a593Smuzhiyun 	pt->data_dev = data_dev;
3379*4882a593Smuzhiyun 	pt->low_water_blocks = low_water_blocks;
3380*4882a593Smuzhiyun 	pt->adjusted_pf = pt->requested_pf = pf;
3381*4882a593Smuzhiyun 	ti->num_flush_bios = 1;
3382*4882a593Smuzhiyun 
3383*4882a593Smuzhiyun 	/*
3384*4882a593Smuzhiyun 	 * Only need to enable discards if the pool should pass
3385*4882a593Smuzhiyun 	 * them down to the data device.  The thin device's discard
3386*4882a593Smuzhiyun 	 * processing will cause mappings to be removed from the btree.
3387*4882a593Smuzhiyun 	 */
3388*4882a593Smuzhiyun 	if (pf.discard_enabled && pf.discard_passdown) {
3389*4882a593Smuzhiyun 		ti->num_discard_bios = 1;
3390*4882a593Smuzhiyun 
3391*4882a593Smuzhiyun 		/*
3392*4882a593Smuzhiyun 		 * Setting 'discards_supported' circumvents the normal
3393*4882a593Smuzhiyun 		 * stacking of discard limits (this keeps the pool and
3394*4882a593Smuzhiyun 		 * thin devices' discard limits consistent).
3395*4882a593Smuzhiyun 		 */
3396*4882a593Smuzhiyun 		ti->discards_supported = true;
3397*4882a593Smuzhiyun 	}
3398*4882a593Smuzhiyun 	ti->private = pt;
3399*4882a593Smuzhiyun 
3400*4882a593Smuzhiyun 	r = dm_pool_register_metadata_threshold(pt->pool->pmd,
3401*4882a593Smuzhiyun 						calc_metadata_threshold(pt),
3402*4882a593Smuzhiyun 						metadata_low_callback,
3403*4882a593Smuzhiyun 						pool);
3404*4882a593Smuzhiyun 	if (r) {
3405*4882a593Smuzhiyun 		ti->error = "Error registering metadata threshold";
3406*4882a593Smuzhiyun 		goto out_flags_changed;
3407*4882a593Smuzhiyun 	}
3408*4882a593Smuzhiyun 
3409*4882a593Smuzhiyun 	dm_pool_register_pre_commit_callback(pool->pmd,
3410*4882a593Smuzhiyun 					     metadata_pre_commit_callback, pool);
3411*4882a593Smuzhiyun 
3412*4882a593Smuzhiyun 	mutex_unlock(&dm_thin_pool_table.mutex);
3413*4882a593Smuzhiyun 
3414*4882a593Smuzhiyun 	return 0;
3415*4882a593Smuzhiyun 
3416*4882a593Smuzhiyun out_flags_changed:
3417*4882a593Smuzhiyun 	__pool_dec(pool);
3418*4882a593Smuzhiyun out_free_pt:
3419*4882a593Smuzhiyun 	kfree(pt);
3420*4882a593Smuzhiyun out:
3421*4882a593Smuzhiyun 	dm_put_device(ti, data_dev);
3422*4882a593Smuzhiyun out_metadata:
3423*4882a593Smuzhiyun 	dm_put_device(ti, metadata_dev);
3424*4882a593Smuzhiyun out_unlock:
3425*4882a593Smuzhiyun 	mutex_unlock(&dm_thin_pool_table.mutex);
3426*4882a593Smuzhiyun 
3427*4882a593Smuzhiyun 	return r;
3428*4882a593Smuzhiyun }
3429*4882a593Smuzhiyun 
pool_map(struct dm_target * ti,struct bio * bio)3430*4882a593Smuzhiyun static int pool_map(struct dm_target *ti, struct bio *bio)
3431*4882a593Smuzhiyun {
3432*4882a593Smuzhiyun 	int r;
3433*4882a593Smuzhiyun 	struct pool_c *pt = ti->private;
3434*4882a593Smuzhiyun 	struct pool *pool = pt->pool;
3435*4882a593Smuzhiyun 
3436*4882a593Smuzhiyun 	/*
3437*4882a593Smuzhiyun 	 * As this is a singleton target, ti->begin is always zero.
3438*4882a593Smuzhiyun 	 */
3439*4882a593Smuzhiyun 	spin_lock_irq(&pool->lock);
3440*4882a593Smuzhiyun 	bio_set_dev(bio, pt->data_dev->bdev);
3441*4882a593Smuzhiyun 	r = DM_MAPIO_REMAPPED;
3442*4882a593Smuzhiyun 	spin_unlock_irq(&pool->lock);
3443*4882a593Smuzhiyun 
3444*4882a593Smuzhiyun 	return r;
3445*4882a593Smuzhiyun }
3446*4882a593Smuzhiyun 
maybe_resize_data_dev(struct dm_target * ti,bool * need_commit)3447*4882a593Smuzhiyun static int maybe_resize_data_dev(struct dm_target *ti, bool *need_commit)
3448*4882a593Smuzhiyun {
3449*4882a593Smuzhiyun 	int r;
3450*4882a593Smuzhiyun 	struct pool_c *pt = ti->private;
3451*4882a593Smuzhiyun 	struct pool *pool = pt->pool;
3452*4882a593Smuzhiyun 	sector_t data_size = ti->len;
3453*4882a593Smuzhiyun 	dm_block_t sb_data_size;
3454*4882a593Smuzhiyun 
3455*4882a593Smuzhiyun 	*need_commit = false;
3456*4882a593Smuzhiyun 
3457*4882a593Smuzhiyun 	(void) sector_div(data_size, pool->sectors_per_block);
3458*4882a593Smuzhiyun 
3459*4882a593Smuzhiyun 	r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size);
3460*4882a593Smuzhiyun 	if (r) {
3461*4882a593Smuzhiyun 		DMERR("%s: failed to retrieve data device size",
3462*4882a593Smuzhiyun 		      dm_device_name(pool->pool_md));
3463*4882a593Smuzhiyun 		return r;
3464*4882a593Smuzhiyun 	}
3465*4882a593Smuzhiyun 
3466*4882a593Smuzhiyun 	if (data_size < sb_data_size) {
3467*4882a593Smuzhiyun 		DMERR("%s: pool target (%llu blocks) too small: expected %llu",
3468*4882a593Smuzhiyun 		      dm_device_name(pool->pool_md),
3469*4882a593Smuzhiyun 		      (unsigned long long)data_size, sb_data_size);
3470*4882a593Smuzhiyun 		return -EINVAL;
3471*4882a593Smuzhiyun 
3472*4882a593Smuzhiyun 	} else if (data_size > sb_data_size) {
3473*4882a593Smuzhiyun 		if (dm_pool_metadata_needs_check(pool->pmd)) {
3474*4882a593Smuzhiyun 			DMERR("%s: unable to grow the data device until repaired.",
3475*4882a593Smuzhiyun 			      dm_device_name(pool->pool_md));
3476*4882a593Smuzhiyun 			return 0;
3477*4882a593Smuzhiyun 		}
3478*4882a593Smuzhiyun 
3479*4882a593Smuzhiyun 		if (sb_data_size)
3480*4882a593Smuzhiyun 			DMINFO("%s: growing the data device from %llu to %llu blocks",
3481*4882a593Smuzhiyun 			       dm_device_name(pool->pool_md),
3482*4882a593Smuzhiyun 			       sb_data_size, (unsigned long long)data_size);
3483*4882a593Smuzhiyun 		r = dm_pool_resize_data_dev(pool->pmd, data_size);
3484*4882a593Smuzhiyun 		if (r) {
3485*4882a593Smuzhiyun 			metadata_operation_failed(pool, "dm_pool_resize_data_dev", r);
3486*4882a593Smuzhiyun 			return r;
3487*4882a593Smuzhiyun 		}
3488*4882a593Smuzhiyun 
3489*4882a593Smuzhiyun 		*need_commit = true;
3490*4882a593Smuzhiyun 	}
3491*4882a593Smuzhiyun 
3492*4882a593Smuzhiyun 	return 0;
3493*4882a593Smuzhiyun }
3494*4882a593Smuzhiyun 
maybe_resize_metadata_dev(struct dm_target * ti,bool * need_commit)3495*4882a593Smuzhiyun static int maybe_resize_metadata_dev(struct dm_target *ti, bool *need_commit)
3496*4882a593Smuzhiyun {
3497*4882a593Smuzhiyun 	int r;
3498*4882a593Smuzhiyun 	struct pool_c *pt = ti->private;
3499*4882a593Smuzhiyun 	struct pool *pool = pt->pool;
3500*4882a593Smuzhiyun 	dm_block_t metadata_dev_size, sb_metadata_dev_size;
3501*4882a593Smuzhiyun 
3502*4882a593Smuzhiyun 	*need_commit = false;
3503*4882a593Smuzhiyun 
3504*4882a593Smuzhiyun 	metadata_dev_size = get_metadata_dev_size_in_blocks(pool->md_dev);
3505*4882a593Smuzhiyun 
3506*4882a593Smuzhiyun 	r = dm_pool_get_metadata_dev_size(pool->pmd, &sb_metadata_dev_size);
3507*4882a593Smuzhiyun 	if (r) {
3508*4882a593Smuzhiyun 		DMERR("%s: failed to retrieve metadata device size",
3509*4882a593Smuzhiyun 		      dm_device_name(pool->pool_md));
3510*4882a593Smuzhiyun 		return r;
3511*4882a593Smuzhiyun 	}
3512*4882a593Smuzhiyun 
3513*4882a593Smuzhiyun 	if (metadata_dev_size < sb_metadata_dev_size) {
3514*4882a593Smuzhiyun 		DMERR("%s: metadata device (%llu blocks) too small: expected %llu",
3515*4882a593Smuzhiyun 		      dm_device_name(pool->pool_md),
3516*4882a593Smuzhiyun 		      metadata_dev_size, sb_metadata_dev_size);
3517*4882a593Smuzhiyun 		return -EINVAL;
3518*4882a593Smuzhiyun 
3519*4882a593Smuzhiyun 	} else if (metadata_dev_size > sb_metadata_dev_size) {
3520*4882a593Smuzhiyun 		if (dm_pool_metadata_needs_check(pool->pmd)) {
3521*4882a593Smuzhiyun 			DMERR("%s: unable to grow the metadata device until repaired.",
3522*4882a593Smuzhiyun 			      dm_device_name(pool->pool_md));
3523*4882a593Smuzhiyun 			return 0;
3524*4882a593Smuzhiyun 		}
3525*4882a593Smuzhiyun 
3526*4882a593Smuzhiyun 		warn_if_metadata_device_too_big(pool->md_dev);
3527*4882a593Smuzhiyun 		DMINFO("%s: growing the metadata device from %llu to %llu blocks",
3528*4882a593Smuzhiyun 		       dm_device_name(pool->pool_md),
3529*4882a593Smuzhiyun 		       sb_metadata_dev_size, metadata_dev_size);
3530*4882a593Smuzhiyun 
3531*4882a593Smuzhiyun 		if (get_pool_mode(pool) == PM_OUT_OF_METADATA_SPACE)
3532*4882a593Smuzhiyun 			set_pool_mode(pool, PM_WRITE);
3533*4882a593Smuzhiyun 
3534*4882a593Smuzhiyun 		r = dm_pool_resize_metadata_dev(pool->pmd, metadata_dev_size);
3535*4882a593Smuzhiyun 		if (r) {
3536*4882a593Smuzhiyun 			metadata_operation_failed(pool, "dm_pool_resize_metadata_dev", r);
3537*4882a593Smuzhiyun 			return r;
3538*4882a593Smuzhiyun 		}
3539*4882a593Smuzhiyun 
3540*4882a593Smuzhiyun 		*need_commit = true;
3541*4882a593Smuzhiyun 	}
3542*4882a593Smuzhiyun 
3543*4882a593Smuzhiyun 	return 0;
3544*4882a593Smuzhiyun }
3545*4882a593Smuzhiyun 
3546*4882a593Smuzhiyun /*
3547*4882a593Smuzhiyun  * Retrieves the number of blocks of the data device from
3548*4882a593Smuzhiyun  * the superblock and compares it to the actual device size,
3549*4882a593Smuzhiyun  * thus resizing the data device in case it has grown.
3550*4882a593Smuzhiyun  *
3551*4882a593Smuzhiyun  * This both copes with opening preallocated data devices in the ctr
3552*4882a593Smuzhiyun  * being followed by a resume
3553*4882a593Smuzhiyun  * -and-
3554*4882a593Smuzhiyun  * calling the resume method individually after userspace has
3555*4882a593Smuzhiyun  * grown the data device in reaction to a table event.
3556*4882a593Smuzhiyun  */
pool_preresume(struct dm_target * ti)3557*4882a593Smuzhiyun static int pool_preresume(struct dm_target *ti)
3558*4882a593Smuzhiyun {
3559*4882a593Smuzhiyun 	int r;
3560*4882a593Smuzhiyun 	bool need_commit1, need_commit2;
3561*4882a593Smuzhiyun 	struct pool_c *pt = ti->private;
3562*4882a593Smuzhiyun 	struct pool *pool = pt->pool;
3563*4882a593Smuzhiyun 
3564*4882a593Smuzhiyun 	/*
3565*4882a593Smuzhiyun 	 * Take control of the pool object.
3566*4882a593Smuzhiyun 	 */
3567*4882a593Smuzhiyun 	r = bind_control_target(pool, ti);
3568*4882a593Smuzhiyun 	if (r)
3569*4882a593Smuzhiyun 		return r;
3570*4882a593Smuzhiyun 
3571*4882a593Smuzhiyun 	r = maybe_resize_data_dev(ti, &need_commit1);
3572*4882a593Smuzhiyun 	if (r)
3573*4882a593Smuzhiyun 		return r;
3574*4882a593Smuzhiyun 
3575*4882a593Smuzhiyun 	r = maybe_resize_metadata_dev(ti, &need_commit2);
3576*4882a593Smuzhiyun 	if (r)
3577*4882a593Smuzhiyun 		return r;
3578*4882a593Smuzhiyun 
3579*4882a593Smuzhiyun 	if (need_commit1 || need_commit2)
3580*4882a593Smuzhiyun 		(void) commit(pool);
3581*4882a593Smuzhiyun 
3582*4882a593Smuzhiyun 	return 0;
3583*4882a593Smuzhiyun }
3584*4882a593Smuzhiyun 
pool_suspend_active_thins(struct pool * pool)3585*4882a593Smuzhiyun static void pool_suspend_active_thins(struct pool *pool)
3586*4882a593Smuzhiyun {
3587*4882a593Smuzhiyun 	struct thin_c *tc;
3588*4882a593Smuzhiyun 
3589*4882a593Smuzhiyun 	/* Suspend all active thin devices */
3590*4882a593Smuzhiyun 	tc = get_first_thin(pool);
3591*4882a593Smuzhiyun 	while (tc) {
3592*4882a593Smuzhiyun 		dm_internal_suspend_noflush(tc->thin_md);
3593*4882a593Smuzhiyun 		tc = get_next_thin(pool, tc);
3594*4882a593Smuzhiyun 	}
3595*4882a593Smuzhiyun }
3596*4882a593Smuzhiyun 
pool_resume_active_thins(struct pool * pool)3597*4882a593Smuzhiyun static void pool_resume_active_thins(struct pool *pool)
3598*4882a593Smuzhiyun {
3599*4882a593Smuzhiyun 	struct thin_c *tc;
3600*4882a593Smuzhiyun 
3601*4882a593Smuzhiyun 	/* Resume all active thin devices */
3602*4882a593Smuzhiyun 	tc = get_first_thin(pool);
3603*4882a593Smuzhiyun 	while (tc) {
3604*4882a593Smuzhiyun 		dm_internal_resume(tc->thin_md);
3605*4882a593Smuzhiyun 		tc = get_next_thin(pool, tc);
3606*4882a593Smuzhiyun 	}
3607*4882a593Smuzhiyun }
3608*4882a593Smuzhiyun 
pool_resume(struct dm_target * ti)3609*4882a593Smuzhiyun static void pool_resume(struct dm_target *ti)
3610*4882a593Smuzhiyun {
3611*4882a593Smuzhiyun 	struct pool_c *pt = ti->private;
3612*4882a593Smuzhiyun 	struct pool *pool = pt->pool;
3613*4882a593Smuzhiyun 
3614*4882a593Smuzhiyun 	/*
3615*4882a593Smuzhiyun 	 * Must requeue active_thins' bios and then resume
3616*4882a593Smuzhiyun 	 * active_thins _before_ clearing 'suspend' flag.
3617*4882a593Smuzhiyun 	 */
3618*4882a593Smuzhiyun 	requeue_bios(pool);
3619*4882a593Smuzhiyun 	pool_resume_active_thins(pool);
3620*4882a593Smuzhiyun 
3621*4882a593Smuzhiyun 	spin_lock_irq(&pool->lock);
3622*4882a593Smuzhiyun 	pool->low_water_triggered = false;
3623*4882a593Smuzhiyun 	pool->suspended = false;
3624*4882a593Smuzhiyun 	spin_unlock_irq(&pool->lock);
3625*4882a593Smuzhiyun 
3626*4882a593Smuzhiyun 	do_waker(&pool->waker.work);
3627*4882a593Smuzhiyun }
3628*4882a593Smuzhiyun 
pool_presuspend(struct dm_target * ti)3629*4882a593Smuzhiyun static void pool_presuspend(struct dm_target *ti)
3630*4882a593Smuzhiyun {
3631*4882a593Smuzhiyun 	struct pool_c *pt = ti->private;
3632*4882a593Smuzhiyun 	struct pool *pool = pt->pool;
3633*4882a593Smuzhiyun 
3634*4882a593Smuzhiyun 	spin_lock_irq(&pool->lock);
3635*4882a593Smuzhiyun 	pool->suspended = true;
3636*4882a593Smuzhiyun 	spin_unlock_irq(&pool->lock);
3637*4882a593Smuzhiyun 
3638*4882a593Smuzhiyun 	pool_suspend_active_thins(pool);
3639*4882a593Smuzhiyun }
3640*4882a593Smuzhiyun 
pool_presuspend_undo(struct dm_target * ti)3641*4882a593Smuzhiyun static void pool_presuspend_undo(struct dm_target *ti)
3642*4882a593Smuzhiyun {
3643*4882a593Smuzhiyun 	struct pool_c *pt = ti->private;
3644*4882a593Smuzhiyun 	struct pool *pool = pt->pool;
3645*4882a593Smuzhiyun 
3646*4882a593Smuzhiyun 	pool_resume_active_thins(pool);
3647*4882a593Smuzhiyun 
3648*4882a593Smuzhiyun 	spin_lock_irq(&pool->lock);
3649*4882a593Smuzhiyun 	pool->suspended = false;
3650*4882a593Smuzhiyun 	spin_unlock_irq(&pool->lock);
3651*4882a593Smuzhiyun }
3652*4882a593Smuzhiyun 
pool_postsuspend(struct dm_target * ti)3653*4882a593Smuzhiyun static void pool_postsuspend(struct dm_target *ti)
3654*4882a593Smuzhiyun {
3655*4882a593Smuzhiyun 	struct pool_c *pt = ti->private;
3656*4882a593Smuzhiyun 	struct pool *pool = pt->pool;
3657*4882a593Smuzhiyun 
3658*4882a593Smuzhiyun 	cancel_delayed_work_sync(&pool->waker);
3659*4882a593Smuzhiyun 	cancel_delayed_work_sync(&pool->no_space_timeout);
3660*4882a593Smuzhiyun 	flush_workqueue(pool->wq);
3661*4882a593Smuzhiyun 	(void) commit(pool);
3662*4882a593Smuzhiyun }
3663*4882a593Smuzhiyun 
check_arg_count(unsigned argc,unsigned args_required)3664*4882a593Smuzhiyun static int check_arg_count(unsigned argc, unsigned args_required)
3665*4882a593Smuzhiyun {
3666*4882a593Smuzhiyun 	if (argc != args_required) {
3667*4882a593Smuzhiyun 		DMWARN("Message received with %u arguments instead of %u.",
3668*4882a593Smuzhiyun 		       argc, args_required);
3669*4882a593Smuzhiyun 		return -EINVAL;
3670*4882a593Smuzhiyun 	}
3671*4882a593Smuzhiyun 
3672*4882a593Smuzhiyun 	return 0;
3673*4882a593Smuzhiyun }
3674*4882a593Smuzhiyun 
read_dev_id(char * arg,dm_thin_id * dev_id,int warning)3675*4882a593Smuzhiyun static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning)
3676*4882a593Smuzhiyun {
3677*4882a593Smuzhiyun 	if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) &&
3678*4882a593Smuzhiyun 	    *dev_id <= MAX_DEV_ID)
3679*4882a593Smuzhiyun 		return 0;
3680*4882a593Smuzhiyun 
3681*4882a593Smuzhiyun 	if (warning)
3682*4882a593Smuzhiyun 		DMWARN("Message received with invalid device id: %s", arg);
3683*4882a593Smuzhiyun 
3684*4882a593Smuzhiyun 	return -EINVAL;
3685*4882a593Smuzhiyun }
3686*4882a593Smuzhiyun 
process_create_thin_mesg(unsigned argc,char ** argv,struct pool * pool)3687*4882a593Smuzhiyun static int process_create_thin_mesg(unsigned argc, char **argv, struct pool *pool)
3688*4882a593Smuzhiyun {
3689*4882a593Smuzhiyun 	dm_thin_id dev_id;
3690*4882a593Smuzhiyun 	int r;
3691*4882a593Smuzhiyun 
3692*4882a593Smuzhiyun 	r = check_arg_count(argc, 2);
3693*4882a593Smuzhiyun 	if (r)
3694*4882a593Smuzhiyun 		return r;
3695*4882a593Smuzhiyun 
3696*4882a593Smuzhiyun 	r = read_dev_id(argv[1], &dev_id, 1);
3697*4882a593Smuzhiyun 	if (r)
3698*4882a593Smuzhiyun 		return r;
3699*4882a593Smuzhiyun 
3700*4882a593Smuzhiyun 	r = dm_pool_create_thin(pool->pmd, dev_id);
3701*4882a593Smuzhiyun 	if (r) {
3702*4882a593Smuzhiyun 		DMWARN("Creation of new thinly-provisioned device with id %s failed.",
3703*4882a593Smuzhiyun 		       argv[1]);
3704*4882a593Smuzhiyun 		return r;
3705*4882a593Smuzhiyun 	}
3706*4882a593Smuzhiyun 
3707*4882a593Smuzhiyun 	return 0;
3708*4882a593Smuzhiyun }
3709*4882a593Smuzhiyun 
process_create_snap_mesg(unsigned argc,char ** argv,struct pool * pool)3710*4882a593Smuzhiyun static int process_create_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3711*4882a593Smuzhiyun {
3712*4882a593Smuzhiyun 	dm_thin_id dev_id;
3713*4882a593Smuzhiyun 	dm_thin_id origin_dev_id;
3714*4882a593Smuzhiyun 	int r;
3715*4882a593Smuzhiyun 
3716*4882a593Smuzhiyun 	r = check_arg_count(argc, 3);
3717*4882a593Smuzhiyun 	if (r)
3718*4882a593Smuzhiyun 		return r;
3719*4882a593Smuzhiyun 
3720*4882a593Smuzhiyun 	r = read_dev_id(argv[1], &dev_id, 1);
3721*4882a593Smuzhiyun 	if (r)
3722*4882a593Smuzhiyun 		return r;
3723*4882a593Smuzhiyun 
3724*4882a593Smuzhiyun 	r = read_dev_id(argv[2], &origin_dev_id, 1);
3725*4882a593Smuzhiyun 	if (r)
3726*4882a593Smuzhiyun 		return r;
3727*4882a593Smuzhiyun 
3728*4882a593Smuzhiyun 	r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id);
3729*4882a593Smuzhiyun 	if (r) {
3730*4882a593Smuzhiyun 		DMWARN("Creation of new snapshot %s of device %s failed.",
3731*4882a593Smuzhiyun 		       argv[1], argv[2]);
3732*4882a593Smuzhiyun 		return r;
3733*4882a593Smuzhiyun 	}
3734*4882a593Smuzhiyun 
3735*4882a593Smuzhiyun 	return 0;
3736*4882a593Smuzhiyun }
3737*4882a593Smuzhiyun 
process_delete_mesg(unsigned argc,char ** argv,struct pool * pool)3738*4882a593Smuzhiyun static int process_delete_mesg(unsigned argc, char **argv, struct pool *pool)
3739*4882a593Smuzhiyun {
3740*4882a593Smuzhiyun 	dm_thin_id dev_id;
3741*4882a593Smuzhiyun 	int r;
3742*4882a593Smuzhiyun 
3743*4882a593Smuzhiyun 	r = check_arg_count(argc, 2);
3744*4882a593Smuzhiyun 	if (r)
3745*4882a593Smuzhiyun 		return r;
3746*4882a593Smuzhiyun 
3747*4882a593Smuzhiyun 	r = read_dev_id(argv[1], &dev_id, 1);
3748*4882a593Smuzhiyun 	if (r)
3749*4882a593Smuzhiyun 		return r;
3750*4882a593Smuzhiyun 
3751*4882a593Smuzhiyun 	r = dm_pool_delete_thin_device(pool->pmd, dev_id);
3752*4882a593Smuzhiyun 	if (r)
3753*4882a593Smuzhiyun 		DMWARN("Deletion of thin device %s failed.", argv[1]);
3754*4882a593Smuzhiyun 
3755*4882a593Smuzhiyun 	return r;
3756*4882a593Smuzhiyun }
3757*4882a593Smuzhiyun 
process_set_transaction_id_mesg(unsigned argc,char ** argv,struct pool * pool)3758*4882a593Smuzhiyun static int process_set_transaction_id_mesg(unsigned argc, char **argv, struct pool *pool)
3759*4882a593Smuzhiyun {
3760*4882a593Smuzhiyun 	dm_thin_id old_id, new_id;
3761*4882a593Smuzhiyun 	int r;
3762*4882a593Smuzhiyun 
3763*4882a593Smuzhiyun 	r = check_arg_count(argc, 3);
3764*4882a593Smuzhiyun 	if (r)
3765*4882a593Smuzhiyun 		return r;
3766*4882a593Smuzhiyun 
3767*4882a593Smuzhiyun 	if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) {
3768*4882a593Smuzhiyun 		DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]);
3769*4882a593Smuzhiyun 		return -EINVAL;
3770*4882a593Smuzhiyun 	}
3771*4882a593Smuzhiyun 
3772*4882a593Smuzhiyun 	if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) {
3773*4882a593Smuzhiyun 		DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]);
3774*4882a593Smuzhiyun 		return -EINVAL;
3775*4882a593Smuzhiyun 	}
3776*4882a593Smuzhiyun 
3777*4882a593Smuzhiyun 	r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id);
3778*4882a593Smuzhiyun 	if (r) {
3779*4882a593Smuzhiyun 		DMWARN("Failed to change transaction id from %s to %s.",
3780*4882a593Smuzhiyun 		       argv[1], argv[2]);
3781*4882a593Smuzhiyun 		return r;
3782*4882a593Smuzhiyun 	}
3783*4882a593Smuzhiyun 
3784*4882a593Smuzhiyun 	return 0;
3785*4882a593Smuzhiyun }
3786*4882a593Smuzhiyun 
process_reserve_metadata_snap_mesg(unsigned argc,char ** argv,struct pool * pool)3787*4882a593Smuzhiyun static int process_reserve_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3788*4882a593Smuzhiyun {
3789*4882a593Smuzhiyun 	int r;
3790*4882a593Smuzhiyun 
3791*4882a593Smuzhiyun 	r = check_arg_count(argc, 1);
3792*4882a593Smuzhiyun 	if (r)
3793*4882a593Smuzhiyun 		return r;
3794*4882a593Smuzhiyun 
3795*4882a593Smuzhiyun 	(void) commit(pool);
3796*4882a593Smuzhiyun 
3797*4882a593Smuzhiyun 	r = dm_pool_reserve_metadata_snap(pool->pmd);
3798*4882a593Smuzhiyun 	if (r)
3799*4882a593Smuzhiyun 		DMWARN("reserve_metadata_snap message failed.");
3800*4882a593Smuzhiyun 
3801*4882a593Smuzhiyun 	return r;
3802*4882a593Smuzhiyun }
3803*4882a593Smuzhiyun 
process_release_metadata_snap_mesg(unsigned argc,char ** argv,struct pool * pool)3804*4882a593Smuzhiyun static int process_release_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3805*4882a593Smuzhiyun {
3806*4882a593Smuzhiyun 	int r;
3807*4882a593Smuzhiyun 
3808*4882a593Smuzhiyun 	r = check_arg_count(argc, 1);
3809*4882a593Smuzhiyun 	if (r)
3810*4882a593Smuzhiyun 		return r;
3811*4882a593Smuzhiyun 
3812*4882a593Smuzhiyun 	r = dm_pool_release_metadata_snap(pool->pmd);
3813*4882a593Smuzhiyun 	if (r)
3814*4882a593Smuzhiyun 		DMWARN("release_metadata_snap message failed.");
3815*4882a593Smuzhiyun 
3816*4882a593Smuzhiyun 	return r;
3817*4882a593Smuzhiyun }
3818*4882a593Smuzhiyun 
3819*4882a593Smuzhiyun /*
3820*4882a593Smuzhiyun  * Messages supported:
3821*4882a593Smuzhiyun  *   create_thin	<dev_id>
3822*4882a593Smuzhiyun  *   create_snap	<dev_id> <origin_id>
3823*4882a593Smuzhiyun  *   delete		<dev_id>
3824*4882a593Smuzhiyun  *   set_transaction_id <current_trans_id> <new_trans_id>
3825*4882a593Smuzhiyun  *   reserve_metadata_snap
3826*4882a593Smuzhiyun  *   release_metadata_snap
3827*4882a593Smuzhiyun  */
pool_message(struct dm_target * ti,unsigned argc,char ** argv,char * result,unsigned maxlen)3828*4882a593Smuzhiyun static int pool_message(struct dm_target *ti, unsigned argc, char **argv,
3829*4882a593Smuzhiyun 			char *result, unsigned maxlen)
3830*4882a593Smuzhiyun {
3831*4882a593Smuzhiyun 	int r = -EINVAL;
3832*4882a593Smuzhiyun 	struct pool_c *pt = ti->private;
3833*4882a593Smuzhiyun 	struct pool *pool = pt->pool;
3834*4882a593Smuzhiyun 
3835*4882a593Smuzhiyun 	if (get_pool_mode(pool) >= PM_OUT_OF_METADATA_SPACE) {
3836*4882a593Smuzhiyun 		DMERR("%s: unable to service pool target messages in READ_ONLY or FAIL mode",
3837*4882a593Smuzhiyun 		      dm_device_name(pool->pool_md));
3838*4882a593Smuzhiyun 		return -EOPNOTSUPP;
3839*4882a593Smuzhiyun 	}
3840*4882a593Smuzhiyun 
3841*4882a593Smuzhiyun 	if (!strcasecmp(argv[0], "create_thin"))
3842*4882a593Smuzhiyun 		r = process_create_thin_mesg(argc, argv, pool);
3843*4882a593Smuzhiyun 
3844*4882a593Smuzhiyun 	else if (!strcasecmp(argv[0], "create_snap"))
3845*4882a593Smuzhiyun 		r = process_create_snap_mesg(argc, argv, pool);
3846*4882a593Smuzhiyun 
3847*4882a593Smuzhiyun 	else if (!strcasecmp(argv[0], "delete"))
3848*4882a593Smuzhiyun 		r = process_delete_mesg(argc, argv, pool);
3849*4882a593Smuzhiyun 
3850*4882a593Smuzhiyun 	else if (!strcasecmp(argv[0], "set_transaction_id"))
3851*4882a593Smuzhiyun 		r = process_set_transaction_id_mesg(argc, argv, pool);
3852*4882a593Smuzhiyun 
3853*4882a593Smuzhiyun 	else if (!strcasecmp(argv[0], "reserve_metadata_snap"))
3854*4882a593Smuzhiyun 		r = process_reserve_metadata_snap_mesg(argc, argv, pool);
3855*4882a593Smuzhiyun 
3856*4882a593Smuzhiyun 	else if (!strcasecmp(argv[0], "release_metadata_snap"))
3857*4882a593Smuzhiyun 		r = process_release_metadata_snap_mesg(argc, argv, pool);
3858*4882a593Smuzhiyun 
3859*4882a593Smuzhiyun 	else
3860*4882a593Smuzhiyun 		DMWARN("Unrecognised thin pool target message received: %s", argv[0]);
3861*4882a593Smuzhiyun 
3862*4882a593Smuzhiyun 	if (!r)
3863*4882a593Smuzhiyun 		(void) commit(pool);
3864*4882a593Smuzhiyun 
3865*4882a593Smuzhiyun 	return r;
3866*4882a593Smuzhiyun }
3867*4882a593Smuzhiyun 
emit_flags(struct pool_features * pf,char * result,unsigned sz,unsigned maxlen)3868*4882a593Smuzhiyun static void emit_flags(struct pool_features *pf, char *result,
3869*4882a593Smuzhiyun 		       unsigned sz, unsigned maxlen)
3870*4882a593Smuzhiyun {
3871*4882a593Smuzhiyun 	unsigned count = !pf->zero_new_blocks + !pf->discard_enabled +
3872*4882a593Smuzhiyun 		!pf->discard_passdown + (pf->mode == PM_READ_ONLY) +
3873*4882a593Smuzhiyun 		pf->error_if_no_space;
3874*4882a593Smuzhiyun 	DMEMIT("%u ", count);
3875*4882a593Smuzhiyun 
3876*4882a593Smuzhiyun 	if (!pf->zero_new_blocks)
3877*4882a593Smuzhiyun 		DMEMIT("skip_block_zeroing ");
3878*4882a593Smuzhiyun 
3879*4882a593Smuzhiyun 	if (!pf->discard_enabled)
3880*4882a593Smuzhiyun 		DMEMIT("ignore_discard ");
3881*4882a593Smuzhiyun 
3882*4882a593Smuzhiyun 	if (!pf->discard_passdown)
3883*4882a593Smuzhiyun 		DMEMIT("no_discard_passdown ");
3884*4882a593Smuzhiyun 
3885*4882a593Smuzhiyun 	if (pf->mode == PM_READ_ONLY)
3886*4882a593Smuzhiyun 		DMEMIT("read_only ");
3887*4882a593Smuzhiyun 
3888*4882a593Smuzhiyun 	if (pf->error_if_no_space)
3889*4882a593Smuzhiyun 		DMEMIT("error_if_no_space ");
3890*4882a593Smuzhiyun }
3891*4882a593Smuzhiyun 
3892*4882a593Smuzhiyun /*
3893*4882a593Smuzhiyun  * Status line is:
3894*4882a593Smuzhiyun  *    <transaction id> <used metadata sectors>/<total metadata sectors>
3895*4882a593Smuzhiyun  *    <used data sectors>/<total data sectors> <held metadata root>
3896*4882a593Smuzhiyun  *    <pool mode> <discard config> <no space config> <needs_check>
3897*4882a593Smuzhiyun  */
pool_status(struct dm_target * ti,status_type_t type,unsigned status_flags,char * result,unsigned maxlen)3898*4882a593Smuzhiyun static void pool_status(struct dm_target *ti, status_type_t type,
3899*4882a593Smuzhiyun 			unsigned status_flags, char *result, unsigned maxlen)
3900*4882a593Smuzhiyun {
3901*4882a593Smuzhiyun 	int r;
3902*4882a593Smuzhiyun 	unsigned sz = 0;
3903*4882a593Smuzhiyun 	uint64_t transaction_id;
3904*4882a593Smuzhiyun 	dm_block_t nr_free_blocks_data;
3905*4882a593Smuzhiyun 	dm_block_t nr_free_blocks_metadata;
3906*4882a593Smuzhiyun 	dm_block_t nr_blocks_data;
3907*4882a593Smuzhiyun 	dm_block_t nr_blocks_metadata;
3908*4882a593Smuzhiyun 	dm_block_t held_root;
3909*4882a593Smuzhiyun 	enum pool_mode mode;
3910*4882a593Smuzhiyun 	char buf[BDEVNAME_SIZE];
3911*4882a593Smuzhiyun 	char buf2[BDEVNAME_SIZE];
3912*4882a593Smuzhiyun 	struct pool_c *pt = ti->private;
3913*4882a593Smuzhiyun 	struct pool *pool = pt->pool;
3914*4882a593Smuzhiyun 
3915*4882a593Smuzhiyun 	switch (type) {
3916*4882a593Smuzhiyun 	case STATUSTYPE_INFO:
3917*4882a593Smuzhiyun 		if (get_pool_mode(pool) == PM_FAIL) {
3918*4882a593Smuzhiyun 			DMEMIT("Fail");
3919*4882a593Smuzhiyun 			break;
3920*4882a593Smuzhiyun 		}
3921*4882a593Smuzhiyun 
3922*4882a593Smuzhiyun 		/* Commit to ensure statistics aren't out-of-date */
3923*4882a593Smuzhiyun 		if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
3924*4882a593Smuzhiyun 			(void) commit(pool);
3925*4882a593Smuzhiyun 
3926*4882a593Smuzhiyun 		r = dm_pool_get_metadata_transaction_id(pool->pmd, &transaction_id);
3927*4882a593Smuzhiyun 		if (r) {
3928*4882a593Smuzhiyun 			DMERR("%s: dm_pool_get_metadata_transaction_id returned %d",
3929*4882a593Smuzhiyun 			      dm_device_name(pool->pool_md), r);
3930*4882a593Smuzhiyun 			goto err;
3931*4882a593Smuzhiyun 		}
3932*4882a593Smuzhiyun 
3933*4882a593Smuzhiyun 		r = dm_pool_get_free_metadata_block_count(pool->pmd, &nr_free_blocks_metadata);
3934*4882a593Smuzhiyun 		if (r) {
3935*4882a593Smuzhiyun 			DMERR("%s: dm_pool_get_free_metadata_block_count returned %d",
3936*4882a593Smuzhiyun 			      dm_device_name(pool->pool_md), r);
3937*4882a593Smuzhiyun 			goto err;
3938*4882a593Smuzhiyun 		}
3939*4882a593Smuzhiyun 
3940*4882a593Smuzhiyun 		r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata);
3941*4882a593Smuzhiyun 		if (r) {
3942*4882a593Smuzhiyun 			DMERR("%s: dm_pool_get_metadata_dev_size returned %d",
3943*4882a593Smuzhiyun 			      dm_device_name(pool->pool_md), r);
3944*4882a593Smuzhiyun 			goto err;
3945*4882a593Smuzhiyun 		}
3946*4882a593Smuzhiyun 
3947*4882a593Smuzhiyun 		r = dm_pool_get_free_block_count(pool->pmd, &nr_free_blocks_data);
3948*4882a593Smuzhiyun 		if (r) {
3949*4882a593Smuzhiyun 			DMERR("%s: dm_pool_get_free_block_count returned %d",
3950*4882a593Smuzhiyun 			      dm_device_name(pool->pool_md), r);
3951*4882a593Smuzhiyun 			goto err;
3952*4882a593Smuzhiyun 		}
3953*4882a593Smuzhiyun 
3954*4882a593Smuzhiyun 		r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data);
3955*4882a593Smuzhiyun 		if (r) {
3956*4882a593Smuzhiyun 			DMERR("%s: dm_pool_get_data_dev_size returned %d",
3957*4882a593Smuzhiyun 			      dm_device_name(pool->pool_md), r);
3958*4882a593Smuzhiyun 			goto err;
3959*4882a593Smuzhiyun 		}
3960*4882a593Smuzhiyun 
3961*4882a593Smuzhiyun 		r = dm_pool_get_metadata_snap(pool->pmd, &held_root);
3962*4882a593Smuzhiyun 		if (r) {
3963*4882a593Smuzhiyun 			DMERR("%s: dm_pool_get_metadata_snap returned %d",
3964*4882a593Smuzhiyun 			      dm_device_name(pool->pool_md), r);
3965*4882a593Smuzhiyun 			goto err;
3966*4882a593Smuzhiyun 		}
3967*4882a593Smuzhiyun 
3968*4882a593Smuzhiyun 		DMEMIT("%llu %llu/%llu %llu/%llu ",
3969*4882a593Smuzhiyun 		       (unsigned long long)transaction_id,
3970*4882a593Smuzhiyun 		       (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
3971*4882a593Smuzhiyun 		       (unsigned long long)nr_blocks_metadata,
3972*4882a593Smuzhiyun 		       (unsigned long long)(nr_blocks_data - nr_free_blocks_data),
3973*4882a593Smuzhiyun 		       (unsigned long long)nr_blocks_data);
3974*4882a593Smuzhiyun 
3975*4882a593Smuzhiyun 		if (held_root)
3976*4882a593Smuzhiyun 			DMEMIT("%llu ", held_root);
3977*4882a593Smuzhiyun 		else
3978*4882a593Smuzhiyun 			DMEMIT("- ");
3979*4882a593Smuzhiyun 
3980*4882a593Smuzhiyun 		mode = get_pool_mode(pool);
3981*4882a593Smuzhiyun 		if (mode == PM_OUT_OF_DATA_SPACE)
3982*4882a593Smuzhiyun 			DMEMIT("out_of_data_space ");
3983*4882a593Smuzhiyun 		else if (is_read_only_pool_mode(mode))
3984*4882a593Smuzhiyun 			DMEMIT("ro ");
3985*4882a593Smuzhiyun 		else
3986*4882a593Smuzhiyun 			DMEMIT("rw ");
3987*4882a593Smuzhiyun 
3988*4882a593Smuzhiyun 		if (!pool->pf.discard_enabled)
3989*4882a593Smuzhiyun 			DMEMIT("ignore_discard ");
3990*4882a593Smuzhiyun 		else if (pool->pf.discard_passdown)
3991*4882a593Smuzhiyun 			DMEMIT("discard_passdown ");
3992*4882a593Smuzhiyun 		else
3993*4882a593Smuzhiyun 			DMEMIT("no_discard_passdown ");
3994*4882a593Smuzhiyun 
3995*4882a593Smuzhiyun 		if (pool->pf.error_if_no_space)
3996*4882a593Smuzhiyun 			DMEMIT("error_if_no_space ");
3997*4882a593Smuzhiyun 		else
3998*4882a593Smuzhiyun 			DMEMIT("queue_if_no_space ");
3999*4882a593Smuzhiyun 
4000*4882a593Smuzhiyun 		if (dm_pool_metadata_needs_check(pool->pmd))
4001*4882a593Smuzhiyun 			DMEMIT("needs_check ");
4002*4882a593Smuzhiyun 		else
4003*4882a593Smuzhiyun 			DMEMIT("- ");
4004*4882a593Smuzhiyun 
4005*4882a593Smuzhiyun 		DMEMIT("%llu ", (unsigned long long)calc_metadata_threshold(pt));
4006*4882a593Smuzhiyun 
4007*4882a593Smuzhiyun 		break;
4008*4882a593Smuzhiyun 
4009*4882a593Smuzhiyun 	case STATUSTYPE_TABLE:
4010*4882a593Smuzhiyun 		DMEMIT("%s %s %lu %llu ",
4011*4882a593Smuzhiyun 		       format_dev_t(buf, pt->metadata_dev->bdev->bd_dev),
4012*4882a593Smuzhiyun 		       format_dev_t(buf2, pt->data_dev->bdev->bd_dev),
4013*4882a593Smuzhiyun 		       (unsigned long)pool->sectors_per_block,
4014*4882a593Smuzhiyun 		       (unsigned long long)pt->low_water_blocks);
4015*4882a593Smuzhiyun 		emit_flags(&pt->requested_pf, result, sz, maxlen);
4016*4882a593Smuzhiyun 		break;
4017*4882a593Smuzhiyun 	}
4018*4882a593Smuzhiyun 	return;
4019*4882a593Smuzhiyun 
4020*4882a593Smuzhiyun err:
4021*4882a593Smuzhiyun 	DMEMIT("Error");
4022*4882a593Smuzhiyun }
4023*4882a593Smuzhiyun 
pool_iterate_devices(struct dm_target * ti,iterate_devices_callout_fn fn,void * data)4024*4882a593Smuzhiyun static int pool_iterate_devices(struct dm_target *ti,
4025*4882a593Smuzhiyun 				iterate_devices_callout_fn fn, void *data)
4026*4882a593Smuzhiyun {
4027*4882a593Smuzhiyun 	struct pool_c *pt = ti->private;
4028*4882a593Smuzhiyun 
4029*4882a593Smuzhiyun 	return fn(ti, pt->data_dev, 0, ti->len, data);
4030*4882a593Smuzhiyun }
4031*4882a593Smuzhiyun 
pool_io_hints(struct dm_target * ti,struct queue_limits * limits)4032*4882a593Smuzhiyun static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits)
4033*4882a593Smuzhiyun {
4034*4882a593Smuzhiyun 	struct pool_c *pt = ti->private;
4035*4882a593Smuzhiyun 	struct pool *pool = pt->pool;
4036*4882a593Smuzhiyun 	sector_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
4037*4882a593Smuzhiyun 
4038*4882a593Smuzhiyun 	/*
4039*4882a593Smuzhiyun 	 * If max_sectors is smaller than pool->sectors_per_block adjust it
4040*4882a593Smuzhiyun 	 * to the highest possible power-of-2 factor of pool->sectors_per_block.
4041*4882a593Smuzhiyun 	 * This is especially beneficial when the pool's data device is a RAID
4042*4882a593Smuzhiyun 	 * device that has a full stripe width that matches pool->sectors_per_block
4043*4882a593Smuzhiyun 	 * -- because even though partial RAID stripe-sized IOs will be issued to a
4044*4882a593Smuzhiyun 	 *    single RAID stripe; when aggregated they will end on a full RAID stripe
4045*4882a593Smuzhiyun 	 *    boundary.. which avoids additional partial RAID stripe writes cascading
4046*4882a593Smuzhiyun 	 */
4047*4882a593Smuzhiyun 	if (limits->max_sectors < pool->sectors_per_block) {
4048*4882a593Smuzhiyun 		while (!is_factor(pool->sectors_per_block, limits->max_sectors)) {
4049*4882a593Smuzhiyun 			if ((limits->max_sectors & (limits->max_sectors - 1)) == 0)
4050*4882a593Smuzhiyun 				limits->max_sectors--;
4051*4882a593Smuzhiyun 			limits->max_sectors = rounddown_pow_of_two(limits->max_sectors);
4052*4882a593Smuzhiyun 		}
4053*4882a593Smuzhiyun 	}
4054*4882a593Smuzhiyun 
4055*4882a593Smuzhiyun 	/*
4056*4882a593Smuzhiyun 	 * If the system-determined stacked limits are compatible with the
4057*4882a593Smuzhiyun 	 * pool's blocksize (io_opt is a factor) do not override them.
4058*4882a593Smuzhiyun 	 */
4059*4882a593Smuzhiyun 	if (io_opt_sectors < pool->sectors_per_block ||
4060*4882a593Smuzhiyun 	    !is_factor(io_opt_sectors, pool->sectors_per_block)) {
4061*4882a593Smuzhiyun 		if (is_factor(pool->sectors_per_block, limits->max_sectors))
4062*4882a593Smuzhiyun 			blk_limits_io_min(limits, limits->max_sectors << SECTOR_SHIFT);
4063*4882a593Smuzhiyun 		else
4064*4882a593Smuzhiyun 			blk_limits_io_min(limits, pool->sectors_per_block << SECTOR_SHIFT);
4065*4882a593Smuzhiyun 		blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT);
4066*4882a593Smuzhiyun 	}
4067*4882a593Smuzhiyun 
4068*4882a593Smuzhiyun 	/*
4069*4882a593Smuzhiyun 	 * pt->adjusted_pf is a staging area for the actual features to use.
4070*4882a593Smuzhiyun 	 * They get transferred to the live pool in bind_control_target()
4071*4882a593Smuzhiyun 	 * called from pool_preresume().
4072*4882a593Smuzhiyun 	 */
4073*4882a593Smuzhiyun 	if (!pt->adjusted_pf.discard_enabled) {
4074*4882a593Smuzhiyun 		/*
4075*4882a593Smuzhiyun 		 * Must explicitly disallow stacking discard limits otherwise the
4076*4882a593Smuzhiyun 		 * block layer will stack them if pool's data device has support.
4077*4882a593Smuzhiyun 		 * QUEUE_FLAG_DISCARD wouldn't be set but there is no way for the
4078*4882a593Smuzhiyun 		 * user to see that, so make sure to set all discard limits to 0.
4079*4882a593Smuzhiyun 		 */
4080*4882a593Smuzhiyun 		limits->discard_granularity = 0;
4081*4882a593Smuzhiyun 		return;
4082*4882a593Smuzhiyun 	}
4083*4882a593Smuzhiyun 
4084*4882a593Smuzhiyun 	disable_passdown_if_not_supported(pt);
4085*4882a593Smuzhiyun 
4086*4882a593Smuzhiyun 	/*
4087*4882a593Smuzhiyun 	 * The pool uses the same discard limits as the underlying data
4088*4882a593Smuzhiyun 	 * device.  DM core has already set this up.
4089*4882a593Smuzhiyun 	 */
4090*4882a593Smuzhiyun }
4091*4882a593Smuzhiyun 
4092*4882a593Smuzhiyun static struct target_type pool_target = {
4093*4882a593Smuzhiyun 	.name = "thin-pool",
4094*4882a593Smuzhiyun 	.features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE |
4095*4882a593Smuzhiyun 		    DM_TARGET_IMMUTABLE,
4096*4882a593Smuzhiyun 	.version = {1, 22, 0},
4097*4882a593Smuzhiyun 	.module = THIS_MODULE,
4098*4882a593Smuzhiyun 	.ctr = pool_ctr,
4099*4882a593Smuzhiyun 	.dtr = pool_dtr,
4100*4882a593Smuzhiyun 	.map = pool_map,
4101*4882a593Smuzhiyun 	.presuspend = pool_presuspend,
4102*4882a593Smuzhiyun 	.presuspend_undo = pool_presuspend_undo,
4103*4882a593Smuzhiyun 	.postsuspend = pool_postsuspend,
4104*4882a593Smuzhiyun 	.preresume = pool_preresume,
4105*4882a593Smuzhiyun 	.resume = pool_resume,
4106*4882a593Smuzhiyun 	.message = pool_message,
4107*4882a593Smuzhiyun 	.status = pool_status,
4108*4882a593Smuzhiyun 	.iterate_devices = pool_iterate_devices,
4109*4882a593Smuzhiyun 	.io_hints = pool_io_hints,
4110*4882a593Smuzhiyun };
4111*4882a593Smuzhiyun 
4112*4882a593Smuzhiyun /*----------------------------------------------------------------
4113*4882a593Smuzhiyun  * Thin target methods
4114*4882a593Smuzhiyun  *--------------------------------------------------------------*/
thin_get(struct thin_c * tc)4115*4882a593Smuzhiyun static void thin_get(struct thin_c *tc)
4116*4882a593Smuzhiyun {
4117*4882a593Smuzhiyun 	refcount_inc(&tc->refcount);
4118*4882a593Smuzhiyun }
4119*4882a593Smuzhiyun 
thin_put(struct thin_c * tc)4120*4882a593Smuzhiyun static void thin_put(struct thin_c *tc)
4121*4882a593Smuzhiyun {
4122*4882a593Smuzhiyun 	if (refcount_dec_and_test(&tc->refcount))
4123*4882a593Smuzhiyun 		complete(&tc->can_destroy);
4124*4882a593Smuzhiyun }
4125*4882a593Smuzhiyun 
thin_dtr(struct dm_target * ti)4126*4882a593Smuzhiyun static void thin_dtr(struct dm_target *ti)
4127*4882a593Smuzhiyun {
4128*4882a593Smuzhiyun 	struct thin_c *tc = ti->private;
4129*4882a593Smuzhiyun 
4130*4882a593Smuzhiyun 	spin_lock_irq(&tc->pool->lock);
4131*4882a593Smuzhiyun 	list_del_rcu(&tc->list);
4132*4882a593Smuzhiyun 	spin_unlock_irq(&tc->pool->lock);
4133*4882a593Smuzhiyun 	synchronize_rcu();
4134*4882a593Smuzhiyun 
4135*4882a593Smuzhiyun 	thin_put(tc);
4136*4882a593Smuzhiyun 	wait_for_completion(&tc->can_destroy);
4137*4882a593Smuzhiyun 
4138*4882a593Smuzhiyun 	mutex_lock(&dm_thin_pool_table.mutex);
4139*4882a593Smuzhiyun 
4140*4882a593Smuzhiyun 	__pool_dec(tc->pool);
4141*4882a593Smuzhiyun 	dm_pool_close_thin_device(tc->td);
4142*4882a593Smuzhiyun 	dm_put_device(ti, tc->pool_dev);
4143*4882a593Smuzhiyun 	if (tc->origin_dev)
4144*4882a593Smuzhiyun 		dm_put_device(ti, tc->origin_dev);
4145*4882a593Smuzhiyun 	kfree(tc);
4146*4882a593Smuzhiyun 
4147*4882a593Smuzhiyun 	mutex_unlock(&dm_thin_pool_table.mutex);
4148*4882a593Smuzhiyun }
4149*4882a593Smuzhiyun 
4150*4882a593Smuzhiyun /*
4151*4882a593Smuzhiyun  * Thin target parameters:
4152*4882a593Smuzhiyun  *
4153*4882a593Smuzhiyun  * <pool_dev> <dev_id> [origin_dev]
4154*4882a593Smuzhiyun  *
4155*4882a593Smuzhiyun  * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
4156*4882a593Smuzhiyun  * dev_id: the internal device identifier
4157*4882a593Smuzhiyun  * origin_dev: a device external to the pool that should act as the origin
4158*4882a593Smuzhiyun  *
4159*4882a593Smuzhiyun  * If the pool device has discards disabled, they get disabled for the thin
4160*4882a593Smuzhiyun  * device as well.
4161*4882a593Smuzhiyun  */
thin_ctr(struct dm_target * ti,unsigned argc,char ** argv)4162*4882a593Smuzhiyun static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
4163*4882a593Smuzhiyun {
4164*4882a593Smuzhiyun 	int r;
4165*4882a593Smuzhiyun 	struct thin_c *tc;
4166*4882a593Smuzhiyun 	struct dm_dev *pool_dev, *origin_dev;
4167*4882a593Smuzhiyun 	struct mapped_device *pool_md;
4168*4882a593Smuzhiyun 
4169*4882a593Smuzhiyun 	mutex_lock(&dm_thin_pool_table.mutex);
4170*4882a593Smuzhiyun 
4171*4882a593Smuzhiyun 	if (argc != 2 && argc != 3) {
4172*4882a593Smuzhiyun 		ti->error = "Invalid argument count";
4173*4882a593Smuzhiyun 		r = -EINVAL;
4174*4882a593Smuzhiyun 		goto out_unlock;
4175*4882a593Smuzhiyun 	}
4176*4882a593Smuzhiyun 
4177*4882a593Smuzhiyun 	tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL);
4178*4882a593Smuzhiyun 	if (!tc) {
4179*4882a593Smuzhiyun 		ti->error = "Out of memory";
4180*4882a593Smuzhiyun 		r = -ENOMEM;
4181*4882a593Smuzhiyun 		goto out_unlock;
4182*4882a593Smuzhiyun 	}
4183*4882a593Smuzhiyun 	tc->thin_md = dm_table_get_md(ti->table);
4184*4882a593Smuzhiyun 	spin_lock_init(&tc->lock);
4185*4882a593Smuzhiyun 	INIT_LIST_HEAD(&tc->deferred_cells);
4186*4882a593Smuzhiyun 	bio_list_init(&tc->deferred_bio_list);
4187*4882a593Smuzhiyun 	bio_list_init(&tc->retry_on_resume_list);
4188*4882a593Smuzhiyun 	tc->sort_bio_list = RB_ROOT;
4189*4882a593Smuzhiyun 
4190*4882a593Smuzhiyun 	if (argc == 3) {
4191*4882a593Smuzhiyun 		if (!strcmp(argv[0], argv[2])) {
4192*4882a593Smuzhiyun 			ti->error = "Error setting origin device";
4193*4882a593Smuzhiyun 			r = -EINVAL;
4194*4882a593Smuzhiyun 			goto bad_origin_dev;
4195*4882a593Smuzhiyun 		}
4196*4882a593Smuzhiyun 
4197*4882a593Smuzhiyun 		r = dm_get_device(ti, argv[2], FMODE_READ, &origin_dev);
4198*4882a593Smuzhiyun 		if (r) {
4199*4882a593Smuzhiyun 			ti->error = "Error opening origin device";
4200*4882a593Smuzhiyun 			goto bad_origin_dev;
4201*4882a593Smuzhiyun 		}
4202*4882a593Smuzhiyun 		tc->origin_dev = origin_dev;
4203*4882a593Smuzhiyun 	}
4204*4882a593Smuzhiyun 
4205*4882a593Smuzhiyun 	r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev);
4206*4882a593Smuzhiyun 	if (r) {
4207*4882a593Smuzhiyun 		ti->error = "Error opening pool device";
4208*4882a593Smuzhiyun 		goto bad_pool_dev;
4209*4882a593Smuzhiyun 	}
4210*4882a593Smuzhiyun 	tc->pool_dev = pool_dev;
4211*4882a593Smuzhiyun 
4212*4882a593Smuzhiyun 	if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) {
4213*4882a593Smuzhiyun 		ti->error = "Invalid device id";
4214*4882a593Smuzhiyun 		r = -EINVAL;
4215*4882a593Smuzhiyun 		goto bad_common;
4216*4882a593Smuzhiyun 	}
4217*4882a593Smuzhiyun 
4218*4882a593Smuzhiyun 	pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);
4219*4882a593Smuzhiyun 	if (!pool_md) {
4220*4882a593Smuzhiyun 		ti->error = "Couldn't get pool mapped device";
4221*4882a593Smuzhiyun 		r = -EINVAL;
4222*4882a593Smuzhiyun 		goto bad_common;
4223*4882a593Smuzhiyun 	}
4224*4882a593Smuzhiyun 
4225*4882a593Smuzhiyun 	tc->pool = __pool_table_lookup(pool_md);
4226*4882a593Smuzhiyun 	if (!tc->pool) {
4227*4882a593Smuzhiyun 		ti->error = "Couldn't find pool object";
4228*4882a593Smuzhiyun 		r = -EINVAL;
4229*4882a593Smuzhiyun 		goto bad_pool_lookup;
4230*4882a593Smuzhiyun 	}
4231*4882a593Smuzhiyun 	__pool_inc(tc->pool);
4232*4882a593Smuzhiyun 
4233*4882a593Smuzhiyun 	if (get_pool_mode(tc->pool) == PM_FAIL) {
4234*4882a593Smuzhiyun 		ti->error = "Couldn't open thin device, Pool is in fail mode";
4235*4882a593Smuzhiyun 		r = -EINVAL;
4236*4882a593Smuzhiyun 		goto bad_pool;
4237*4882a593Smuzhiyun 	}
4238*4882a593Smuzhiyun 
4239*4882a593Smuzhiyun 	r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td);
4240*4882a593Smuzhiyun 	if (r) {
4241*4882a593Smuzhiyun 		ti->error = "Couldn't open thin internal device";
4242*4882a593Smuzhiyun 		goto bad_pool;
4243*4882a593Smuzhiyun 	}
4244*4882a593Smuzhiyun 
4245*4882a593Smuzhiyun 	r = dm_set_target_max_io_len(ti, tc->pool->sectors_per_block);
4246*4882a593Smuzhiyun 	if (r)
4247*4882a593Smuzhiyun 		goto bad;
4248*4882a593Smuzhiyun 
4249*4882a593Smuzhiyun 	ti->num_flush_bios = 1;
4250*4882a593Smuzhiyun 	ti->flush_supported = true;
4251*4882a593Smuzhiyun 	ti->per_io_data_size = sizeof(struct dm_thin_endio_hook);
4252*4882a593Smuzhiyun 
4253*4882a593Smuzhiyun 	/* In case the pool supports discards, pass them on. */
4254*4882a593Smuzhiyun 	if (tc->pool->pf.discard_enabled) {
4255*4882a593Smuzhiyun 		ti->discards_supported = true;
4256*4882a593Smuzhiyun 		ti->num_discard_bios = 1;
4257*4882a593Smuzhiyun 	}
4258*4882a593Smuzhiyun 
4259*4882a593Smuzhiyun 	mutex_unlock(&dm_thin_pool_table.mutex);
4260*4882a593Smuzhiyun 
4261*4882a593Smuzhiyun 	spin_lock_irq(&tc->pool->lock);
4262*4882a593Smuzhiyun 	if (tc->pool->suspended) {
4263*4882a593Smuzhiyun 		spin_unlock_irq(&tc->pool->lock);
4264*4882a593Smuzhiyun 		mutex_lock(&dm_thin_pool_table.mutex); /* reacquire for __pool_dec */
4265*4882a593Smuzhiyun 		ti->error = "Unable to activate thin device while pool is suspended";
4266*4882a593Smuzhiyun 		r = -EINVAL;
4267*4882a593Smuzhiyun 		goto bad;
4268*4882a593Smuzhiyun 	}
4269*4882a593Smuzhiyun 	refcount_set(&tc->refcount, 1);
4270*4882a593Smuzhiyun 	init_completion(&tc->can_destroy);
4271*4882a593Smuzhiyun 	list_add_tail_rcu(&tc->list, &tc->pool->active_thins);
4272*4882a593Smuzhiyun 	spin_unlock_irq(&tc->pool->lock);
4273*4882a593Smuzhiyun 	/*
4274*4882a593Smuzhiyun 	 * This synchronize_rcu() call is needed here otherwise we risk a
4275*4882a593Smuzhiyun 	 * wake_worker() call finding no bios to process (because the newly
4276*4882a593Smuzhiyun 	 * added tc isn't yet visible).  So this reduces latency since we
4277*4882a593Smuzhiyun 	 * aren't then dependent on the periodic commit to wake_worker().
4278*4882a593Smuzhiyun 	 */
4279*4882a593Smuzhiyun 	synchronize_rcu();
4280*4882a593Smuzhiyun 
4281*4882a593Smuzhiyun 	dm_put(pool_md);
4282*4882a593Smuzhiyun 
4283*4882a593Smuzhiyun 	return 0;
4284*4882a593Smuzhiyun 
4285*4882a593Smuzhiyun bad:
4286*4882a593Smuzhiyun 	dm_pool_close_thin_device(tc->td);
4287*4882a593Smuzhiyun bad_pool:
4288*4882a593Smuzhiyun 	__pool_dec(tc->pool);
4289*4882a593Smuzhiyun bad_pool_lookup:
4290*4882a593Smuzhiyun 	dm_put(pool_md);
4291*4882a593Smuzhiyun bad_common:
4292*4882a593Smuzhiyun 	dm_put_device(ti, tc->pool_dev);
4293*4882a593Smuzhiyun bad_pool_dev:
4294*4882a593Smuzhiyun 	if (tc->origin_dev)
4295*4882a593Smuzhiyun 		dm_put_device(ti, tc->origin_dev);
4296*4882a593Smuzhiyun bad_origin_dev:
4297*4882a593Smuzhiyun 	kfree(tc);
4298*4882a593Smuzhiyun out_unlock:
4299*4882a593Smuzhiyun 	mutex_unlock(&dm_thin_pool_table.mutex);
4300*4882a593Smuzhiyun 
4301*4882a593Smuzhiyun 	return r;
4302*4882a593Smuzhiyun }
4303*4882a593Smuzhiyun 
thin_map(struct dm_target * ti,struct bio * bio)4304*4882a593Smuzhiyun static int thin_map(struct dm_target *ti, struct bio *bio)
4305*4882a593Smuzhiyun {
4306*4882a593Smuzhiyun 	bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
4307*4882a593Smuzhiyun 
4308*4882a593Smuzhiyun 	return thin_bio_map(ti, bio);
4309*4882a593Smuzhiyun }
4310*4882a593Smuzhiyun 
thin_endio(struct dm_target * ti,struct bio * bio,blk_status_t * err)4311*4882a593Smuzhiyun static int thin_endio(struct dm_target *ti, struct bio *bio,
4312*4882a593Smuzhiyun 		blk_status_t *err)
4313*4882a593Smuzhiyun {
4314*4882a593Smuzhiyun 	unsigned long flags;
4315*4882a593Smuzhiyun 	struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
4316*4882a593Smuzhiyun 	struct list_head work;
4317*4882a593Smuzhiyun 	struct dm_thin_new_mapping *m, *tmp;
4318*4882a593Smuzhiyun 	struct pool *pool = h->tc->pool;
4319*4882a593Smuzhiyun 
4320*4882a593Smuzhiyun 	if (h->shared_read_entry) {
4321*4882a593Smuzhiyun 		INIT_LIST_HEAD(&work);
4322*4882a593Smuzhiyun 		dm_deferred_entry_dec(h->shared_read_entry, &work);
4323*4882a593Smuzhiyun 
4324*4882a593Smuzhiyun 		spin_lock_irqsave(&pool->lock, flags);
4325*4882a593Smuzhiyun 		list_for_each_entry_safe(m, tmp, &work, list) {
4326*4882a593Smuzhiyun 			list_del(&m->list);
4327*4882a593Smuzhiyun 			__complete_mapping_preparation(m);
4328*4882a593Smuzhiyun 		}
4329*4882a593Smuzhiyun 		spin_unlock_irqrestore(&pool->lock, flags);
4330*4882a593Smuzhiyun 	}
4331*4882a593Smuzhiyun 
4332*4882a593Smuzhiyun 	if (h->all_io_entry) {
4333*4882a593Smuzhiyun 		INIT_LIST_HEAD(&work);
4334*4882a593Smuzhiyun 		dm_deferred_entry_dec(h->all_io_entry, &work);
4335*4882a593Smuzhiyun 		if (!list_empty(&work)) {
4336*4882a593Smuzhiyun 			spin_lock_irqsave(&pool->lock, flags);
4337*4882a593Smuzhiyun 			list_for_each_entry_safe(m, tmp, &work, list)
4338*4882a593Smuzhiyun 				list_add_tail(&m->list, &pool->prepared_discards);
4339*4882a593Smuzhiyun 			spin_unlock_irqrestore(&pool->lock, flags);
4340*4882a593Smuzhiyun 			wake_worker(pool);
4341*4882a593Smuzhiyun 		}
4342*4882a593Smuzhiyun 	}
4343*4882a593Smuzhiyun 
4344*4882a593Smuzhiyun 	if (h->cell)
4345*4882a593Smuzhiyun 		cell_defer_no_holder(h->tc, h->cell);
4346*4882a593Smuzhiyun 
4347*4882a593Smuzhiyun 	return DM_ENDIO_DONE;
4348*4882a593Smuzhiyun }
4349*4882a593Smuzhiyun 
thin_presuspend(struct dm_target * ti)4350*4882a593Smuzhiyun static void thin_presuspend(struct dm_target *ti)
4351*4882a593Smuzhiyun {
4352*4882a593Smuzhiyun 	struct thin_c *tc = ti->private;
4353*4882a593Smuzhiyun 
4354*4882a593Smuzhiyun 	if (dm_noflush_suspending(ti))
4355*4882a593Smuzhiyun 		noflush_work(tc, do_noflush_start);
4356*4882a593Smuzhiyun }
4357*4882a593Smuzhiyun 
thin_postsuspend(struct dm_target * ti)4358*4882a593Smuzhiyun static void thin_postsuspend(struct dm_target *ti)
4359*4882a593Smuzhiyun {
4360*4882a593Smuzhiyun 	struct thin_c *tc = ti->private;
4361*4882a593Smuzhiyun 
4362*4882a593Smuzhiyun 	/*
4363*4882a593Smuzhiyun 	 * The dm_noflush_suspending flag has been cleared by now, so
4364*4882a593Smuzhiyun 	 * unfortunately we must always run this.
4365*4882a593Smuzhiyun 	 */
4366*4882a593Smuzhiyun 	noflush_work(tc, do_noflush_stop);
4367*4882a593Smuzhiyun }
4368*4882a593Smuzhiyun 
thin_preresume(struct dm_target * ti)4369*4882a593Smuzhiyun static int thin_preresume(struct dm_target *ti)
4370*4882a593Smuzhiyun {
4371*4882a593Smuzhiyun 	struct thin_c *tc = ti->private;
4372*4882a593Smuzhiyun 
4373*4882a593Smuzhiyun 	if (tc->origin_dev)
4374*4882a593Smuzhiyun 		tc->origin_size = get_dev_size(tc->origin_dev->bdev);
4375*4882a593Smuzhiyun 
4376*4882a593Smuzhiyun 	return 0;
4377*4882a593Smuzhiyun }
4378*4882a593Smuzhiyun 
4379*4882a593Smuzhiyun /*
4380*4882a593Smuzhiyun  * <nr mapped sectors> <highest mapped sector>
4381*4882a593Smuzhiyun  */
thin_status(struct dm_target * ti,status_type_t type,unsigned status_flags,char * result,unsigned maxlen)4382*4882a593Smuzhiyun static void thin_status(struct dm_target *ti, status_type_t type,
4383*4882a593Smuzhiyun 			unsigned status_flags, char *result, unsigned maxlen)
4384*4882a593Smuzhiyun {
4385*4882a593Smuzhiyun 	int r;
4386*4882a593Smuzhiyun 	ssize_t sz = 0;
4387*4882a593Smuzhiyun 	dm_block_t mapped, highest;
4388*4882a593Smuzhiyun 	char buf[BDEVNAME_SIZE];
4389*4882a593Smuzhiyun 	struct thin_c *tc = ti->private;
4390*4882a593Smuzhiyun 
4391*4882a593Smuzhiyun 	if (get_pool_mode(tc->pool) == PM_FAIL) {
4392*4882a593Smuzhiyun 		DMEMIT("Fail");
4393*4882a593Smuzhiyun 		return;
4394*4882a593Smuzhiyun 	}
4395*4882a593Smuzhiyun 
4396*4882a593Smuzhiyun 	if (!tc->td)
4397*4882a593Smuzhiyun 		DMEMIT("-");
4398*4882a593Smuzhiyun 	else {
4399*4882a593Smuzhiyun 		switch (type) {
4400*4882a593Smuzhiyun 		case STATUSTYPE_INFO:
4401*4882a593Smuzhiyun 			r = dm_thin_get_mapped_count(tc->td, &mapped);
4402*4882a593Smuzhiyun 			if (r) {
4403*4882a593Smuzhiyun 				DMERR("dm_thin_get_mapped_count returned %d", r);
4404*4882a593Smuzhiyun 				goto err;
4405*4882a593Smuzhiyun 			}
4406*4882a593Smuzhiyun 
4407*4882a593Smuzhiyun 			r = dm_thin_get_highest_mapped_block(tc->td, &highest);
4408*4882a593Smuzhiyun 			if (r < 0) {
4409*4882a593Smuzhiyun 				DMERR("dm_thin_get_highest_mapped_block returned %d", r);
4410*4882a593Smuzhiyun 				goto err;
4411*4882a593Smuzhiyun 			}
4412*4882a593Smuzhiyun 
4413*4882a593Smuzhiyun 			DMEMIT("%llu ", mapped * tc->pool->sectors_per_block);
4414*4882a593Smuzhiyun 			if (r)
4415*4882a593Smuzhiyun 				DMEMIT("%llu", ((highest + 1) *
4416*4882a593Smuzhiyun 						tc->pool->sectors_per_block) - 1);
4417*4882a593Smuzhiyun 			else
4418*4882a593Smuzhiyun 				DMEMIT("-");
4419*4882a593Smuzhiyun 			break;
4420*4882a593Smuzhiyun 
4421*4882a593Smuzhiyun 		case STATUSTYPE_TABLE:
4422*4882a593Smuzhiyun 			DMEMIT("%s %lu",
4423*4882a593Smuzhiyun 			       format_dev_t(buf, tc->pool_dev->bdev->bd_dev),
4424*4882a593Smuzhiyun 			       (unsigned long) tc->dev_id);
4425*4882a593Smuzhiyun 			if (tc->origin_dev)
4426*4882a593Smuzhiyun 				DMEMIT(" %s", format_dev_t(buf, tc->origin_dev->bdev->bd_dev));
4427*4882a593Smuzhiyun 			break;
4428*4882a593Smuzhiyun 		}
4429*4882a593Smuzhiyun 	}
4430*4882a593Smuzhiyun 
4431*4882a593Smuzhiyun 	return;
4432*4882a593Smuzhiyun 
4433*4882a593Smuzhiyun err:
4434*4882a593Smuzhiyun 	DMEMIT("Error");
4435*4882a593Smuzhiyun }
4436*4882a593Smuzhiyun 
thin_iterate_devices(struct dm_target * ti,iterate_devices_callout_fn fn,void * data)4437*4882a593Smuzhiyun static int thin_iterate_devices(struct dm_target *ti,
4438*4882a593Smuzhiyun 				iterate_devices_callout_fn fn, void *data)
4439*4882a593Smuzhiyun {
4440*4882a593Smuzhiyun 	sector_t blocks;
4441*4882a593Smuzhiyun 	struct thin_c *tc = ti->private;
4442*4882a593Smuzhiyun 	struct pool *pool = tc->pool;
4443*4882a593Smuzhiyun 
4444*4882a593Smuzhiyun 	/*
4445*4882a593Smuzhiyun 	 * We can't call dm_pool_get_data_dev_size() since that blocks.  So
4446*4882a593Smuzhiyun 	 * we follow a more convoluted path through to the pool's target.
4447*4882a593Smuzhiyun 	 */
4448*4882a593Smuzhiyun 	if (!pool->ti)
4449*4882a593Smuzhiyun 		return 0;	/* nothing is bound */
4450*4882a593Smuzhiyun 
4451*4882a593Smuzhiyun 	blocks = pool->ti->len;
4452*4882a593Smuzhiyun 	(void) sector_div(blocks, pool->sectors_per_block);
4453*4882a593Smuzhiyun 	if (blocks)
4454*4882a593Smuzhiyun 		return fn(ti, tc->pool_dev, 0, pool->sectors_per_block * blocks, data);
4455*4882a593Smuzhiyun 
4456*4882a593Smuzhiyun 	return 0;
4457*4882a593Smuzhiyun }
4458*4882a593Smuzhiyun 
thin_io_hints(struct dm_target * ti,struct queue_limits * limits)4459*4882a593Smuzhiyun static void thin_io_hints(struct dm_target *ti, struct queue_limits *limits)
4460*4882a593Smuzhiyun {
4461*4882a593Smuzhiyun 	struct thin_c *tc = ti->private;
4462*4882a593Smuzhiyun 	struct pool *pool = tc->pool;
4463*4882a593Smuzhiyun 
4464*4882a593Smuzhiyun 	if (!pool->pf.discard_enabled)
4465*4882a593Smuzhiyun 		return;
4466*4882a593Smuzhiyun 
4467*4882a593Smuzhiyun 	limits->discard_granularity = pool->sectors_per_block << SECTOR_SHIFT;
4468*4882a593Smuzhiyun 	limits->max_discard_sectors = 2048 * 1024 * 16; /* 16G */
4469*4882a593Smuzhiyun }
4470*4882a593Smuzhiyun 
4471*4882a593Smuzhiyun static struct target_type thin_target = {
4472*4882a593Smuzhiyun 	.name = "thin",
4473*4882a593Smuzhiyun 	.version = {1, 22, 0},
4474*4882a593Smuzhiyun 	.module	= THIS_MODULE,
4475*4882a593Smuzhiyun 	.ctr = thin_ctr,
4476*4882a593Smuzhiyun 	.dtr = thin_dtr,
4477*4882a593Smuzhiyun 	.map = thin_map,
4478*4882a593Smuzhiyun 	.end_io = thin_endio,
4479*4882a593Smuzhiyun 	.preresume = thin_preresume,
4480*4882a593Smuzhiyun 	.presuspend = thin_presuspend,
4481*4882a593Smuzhiyun 	.postsuspend = thin_postsuspend,
4482*4882a593Smuzhiyun 	.status = thin_status,
4483*4882a593Smuzhiyun 	.iterate_devices = thin_iterate_devices,
4484*4882a593Smuzhiyun 	.io_hints = thin_io_hints,
4485*4882a593Smuzhiyun };
4486*4882a593Smuzhiyun 
4487*4882a593Smuzhiyun /*----------------------------------------------------------------*/
4488*4882a593Smuzhiyun 
dm_thin_init(void)4489*4882a593Smuzhiyun static int __init dm_thin_init(void)
4490*4882a593Smuzhiyun {
4491*4882a593Smuzhiyun 	int r = -ENOMEM;
4492*4882a593Smuzhiyun 
4493*4882a593Smuzhiyun 	pool_table_init();
4494*4882a593Smuzhiyun 
4495*4882a593Smuzhiyun 	_new_mapping_cache = KMEM_CACHE(dm_thin_new_mapping, 0);
4496*4882a593Smuzhiyun 	if (!_new_mapping_cache)
4497*4882a593Smuzhiyun 		return r;
4498*4882a593Smuzhiyun 
4499*4882a593Smuzhiyun 	r = dm_register_target(&thin_target);
4500*4882a593Smuzhiyun 	if (r)
4501*4882a593Smuzhiyun 		goto bad_new_mapping_cache;
4502*4882a593Smuzhiyun 
4503*4882a593Smuzhiyun 	r = dm_register_target(&pool_target);
4504*4882a593Smuzhiyun 	if (r)
4505*4882a593Smuzhiyun 		goto bad_thin_target;
4506*4882a593Smuzhiyun 
4507*4882a593Smuzhiyun 	return 0;
4508*4882a593Smuzhiyun 
4509*4882a593Smuzhiyun bad_thin_target:
4510*4882a593Smuzhiyun 	dm_unregister_target(&thin_target);
4511*4882a593Smuzhiyun bad_new_mapping_cache:
4512*4882a593Smuzhiyun 	kmem_cache_destroy(_new_mapping_cache);
4513*4882a593Smuzhiyun 
4514*4882a593Smuzhiyun 	return r;
4515*4882a593Smuzhiyun }
4516*4882a593Smuzhiyun 
dm_thin_exit(void)4517*4882a593Smuzhiyun static void dm_thin_exit(void)
4518*4882a593Smuzhiyun {
4519*4882a593Smuzhiyun 	dm_unregister_target(&thin_target);
4520*4882a593Smuzhiyun 	dm_unregister_target(&pool_target);
4521*4882a593Smuzhiyun 
4522*4882a593Smuzhiyun 	kmem_cache_destroy(_new_mapping_cache);
4523*4882a593Smuzhiyun 
4524*4882a593Smuzhiyun 	pool_table_exit();
4525*4882a593Smuzhiyun }
4526*4882a593Smuzhiyun 
4527*4882a593Smuzhiyun module_init(dm_thin_init);
4528*4882a593Smuzhiyun module_exit(dm_thin_exit);
4529*4882a593Smuzhiyun 
4530*4882a593Smuzhiyun module_param_named(no_space_timeout, no_space_timeout_secs, uint, S_IRUGO | S_IWUSR);
4531*4882a593Smuzhiyun MODULE_PARM_DESC(no_space_timeout, "Out of data space queue IO timeout in seconds");
4532*4882a593Smuzhiyun 
4533*4882a593Smuzhiyun MODULE_DESCRIPTION(DM_NAME " thin provisioning target");
4534*4882a593Smuzhiyun MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
4535*4882a593Smuzhiyun MODULE_LICENSE("GPL");
4536