blob: 8c53b09b9a2c5a3050b22f4fba82af5563f1d59a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
Milan Broz784aae72009-01-06 03:05:12 +00003 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This file is released under the GPL.
6 */
7
8#include "dm.h"
Mike Anderson51e5b2b2007-10-19 22:48:00 +01009#include "dm-uevent.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11#include <linux/init.h>
12#include <linux/module.h>
Arjan van de Ven48c9c272006-03-27 01:18:20 -080013#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/moduleparam.h>
15#include <linux/blkpg.h>
16#include <linux/bio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/mempool.h>
18#include <linux/slab.h>
19#include <linux/idr.h>
Darrick J. Wong3ac51e72006-03-27 01:17:54 -080020#include <linux/hdreg.h>
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +010021#include <linux/delay.h>
Li Zefan55782132009-06-09 13:43:05 +080022
23#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Alasdair G Kergon72d94862006-06-26 00:27:35 -070025#define DM_MSG_PREFIX "core"
26
Namhyung Kim71a16732011-10-31 20:18:54 +000027#ifdef CONFIG_PRINTK
28/*
29 * ratelimit state to be used in DMXXX_LIMIT().
30 */
31DEFINE_RATELIMIT_STATE(dm_ratelimit_state,
32 DEFAULT_RATELIMIT_INTERVAL,
33 DEFAULT_RATELIMIT_BURST);
34EXPORT_SYMBOL(dm_ratelimit_state);
35#endif
36
Milan Broz60935eb2009-06-22 10:12:30 +010037/*
38 * Cookies are numeric values sent with CHANGE and REMOVE
39 * uevents while resuming, removing or renaming the device.
40 */
41#define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
42#define DM_COOKIE_LENGTH 24
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static const char *_name = DM_NAME;
45
46static unsigned int major = 0;
47static unsigned int _major = 0;
48
Alasdair G Kergond15b7742011-08-02 12:32:01 +010049static DEFINE_IDR(_minor_idr);
50
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -070051static DEFINE_SPINLOCK(_minor_lock);
Mikulas Patocka2c140a22013-11-01 18:27:41 -040052
53static void do_deferred_remove(struct work_struct *w);
54
55static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000058 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * One of these is allocated per bio.
60 */
61struct dm_io {
62 struct mapped_device *md;
63 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 atomic_t io_count;
Richard Kennedy6ae2fa62008-07-21 12:00:28 +010065 struct bio *bio;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -080066 unsigned long start_time;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +010067 spinlock_t endio_lock;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -040068 struct dm_stats_aux stats_aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069};
70
71/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000072 * For request-based dm.
73 * One of these is allocated per request.
74 */
75struct dm_rq_target_io {
76 struct mapped_device *md;
77 struct dm_target *ti;
78 struct request *orig, clone;
79 int error;
80 union map_info info;
81};
82
83/*
Kent Overstreet94818742012-09-07 13:44:01 -070084 * For request-based dm - the bio clones we allocate are embedded in these
85 * structs.
86 *
87 * We allocate these with bio_alloc_bioset, using the front_pad parameter when
88 * the bioset is created - this means the bio has to come at the end of the
89 * struct.
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000090 */
91struct dm_rq_clone_bio_info {
92 struct bio *orig;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010093 struct dm_rq_target_io *tio;
Kent Overstreet94818742012-09-07 13:44:01 -070094 struct bio clone;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000095};
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097union map_info *dm_get_mapinfo(struct bio *bio)
98{
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070099 if (bio && bio->bi_private)
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100100 return &((struct dm_target_io *)bio->bi_private)->info;
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700101 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100104union map_info *dm_get_rq_mapinfo(struct request *rq)
105{
106 if (rq && rq->end_io_data)
107 return &((struct dm_rq_target_io *)rq->end_io_data)->info;
108 return NULL;
109}
110EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
111
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700112#define MINOR_ALLOCED ((void *)-1)
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114/*
115 * Bits for the md->flags field.
116 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +0100117#define DMF_BLOCK_IO_FOR_SUSPEND 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -0800119#define DMF_FROZEN 2
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700120#define DMF_FREEING 3
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700121#define DMF_DELETING 4
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800122#define DMF_NOFLUSH_SUSPENDING 5
Mikulas Patockad5b9dd02011-08-02 12:32:04 +0100123#define DMF_MERGE_IS_OPTIONAL 6
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400124#define DMF_DEFERRED_REMOVE 7
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Milan Broz304f3f62008-02-08 02:11:17 +0000126/*
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100127 * A dummy definition to make RCU happy.
128 * struct dm_table should never be dereferenced in this file.
129 */
130struct dm_table {
131 int undefined__;
132};
133
134/*
Milan Broz304f3f62008-02-08 02:11:17 +0000135 * Work processed by per-device workqueue.
136 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137struct mapped_device {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100138 struct srcu_struct io_barrier;
Daniel Walkere61290a2008-02-08 02:10:08 +0000139 struct mutex suspend_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 atomic_t holders;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700141 atomic_t open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Mikulas Patocka2a7faeb2013-07-10 23:41:18 +0100143 /*
144 * The current mapping.
145 * Use dm_get_live_table{_fast} or take suspend_lock for
146 * dereference.
147 */
148 struct dm_table *map;
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 unsigned long flags;
151
Jens Axboe165125e2007-07-24 09:28:11 +0200152 struct request_queue *queue;
Mike Snitzera5664da2010-08-12 04:14:01 +0100153 unsigned type;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +0100154 /* Protect queue and type against concurrent access. */
Mike Snitzera5664da2010-08-12 04:14:01 +0100155 struct mutex type_lock;
156
Alasdair G Kergon36a04562011-10-31 20:19:04 +0000157 struct target_type *immutable_target_type;
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 struct gendisk *disk;
Mike Anderson7e51f252006-03-27 01:17:52 -0800160 char name[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 void *interface_ptr;
163
164 /*
165 * A list of ios that arrived while we were suspended.
166 */
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200167 atomic_t pending[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 wait_queue_head_t wait;
Mikulas Patocka53d59142009-04-02 19:55:37 +0100169 struct work_struct work;
Kiyoshi Ueda74859362006-12-08 02:41:02 -0800170 struct bio_list deferred;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100171 spinlock_t deferred_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 /*
Tejun Heo29e40132010-09-08 18:07:00 +0200174 * Processing queue (flush)
Milan Broz304f3f62008-02-08 02:11:17 +0000175 */
176 struct workqueue_struct *wq;
177
178 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 * io objects are allocated from here.
180 */
181 mempool_t *io_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Stefan Bader9faf4002006-10-03 01:15:41 -0700183 struct bio_set *bs;
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 /*
186 * Event handling.
187 */
188 atomic_t event_nr;
189 wait_queue_head_t eventq;
Mike Anderson7a8c3d32007-10-19 22:48:01 +0100190 atomic_t uevent_seq;
191 struct list_head uevent_list;
192 spinlock_t uevent_lock; /* Protect access to uevent_list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 /*
195 * freeze/thaw support require holding onto a super block
196 */
197 struct super_block *frozen_sb;
Mikulas Patockadb8fef42009-06-22 10:12:15 +0100198 struct block_device *bdev;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800199
200 /* forced geometry settings */
201 struct hd_geometry geometry;
Milan Broz784aae72009-01-06 03:05:12 +0000202
Mikulas Patocka2995fa72014-01-13 19:37:54 -0500203 /* kobject and completion */
204 struct dm_kobject_holder kobj_holder;
Mikulas Patockabe35f482014-01-06 23:01:22 -0500205
Tejun Heod87f4c12010-09-03 11:56:19 +0200206 /* zero-length flush that will be cloned and submitted to targets */
207 struct bio flush_bio;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400208
209 struct dm_stats stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210};
211
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100212/*
213 * For mempools pre-allocation at the table loading time.
214 */
215struct dm_md_mempools {
216 mempool_t *io_pool;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100217 struct bio_set *bs;
218};
219
Mike Snitzer6cfa5852013-09-12 18:06:11 -0400220#define RESERVED_BIO_BASED_IOS 16
221#define RESERVED_REQUEST_BASED_IOS 256
Mike Snitzerf4790822013-09-12 18:06:12 -0400222#define RESERVED_MAX_IOS 1024
Christoph Lametere18b8902006-12-06 20:33:20 -0800223static struct kmem_cache *_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000224static struct kmem_cache *_rq_tio_cache;
Kent Overstreet94818742012-09-07 13:44:01 -0700225
Mike Snitzerf4790822013-09-12 18:06:12 -0400226/*
Mike Snitzere8603132013-09-12 18:06:12 -0400227 * Bio-based DM's mempools' reserved IOs set by the user.
228 */
229static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
230
231/*
Mike Snitzerf4790822013-09-12 18:06:12 -0400232 * Request-based DM's mempools' reserved IOs set by the user.
233 */
234static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
235
236static unsigned __dm_get_reserved_ios(unsigned *reserved_ios,
237 unsigned def, unsigned max)
238{
239 unsigned ios = ACCESS_ONCE(*reserved_ios);
240 unsigned modified_ios = 0;
241
242 if (!ios)
243 modified_ios = def;
244 else if (ios > max)
245 modified_ios = max;
246
247 if (modified_ios) {
248 (void)cmpxchg(reserved_ios, ios, modified_ios);
249 ios = modified_ios;
250 }
251
252 return ios;
253}
254
Mike Snitzere8603132013-09-12 18:06:12 -0400255unsigned dm_get_reserved_bio_based_ios(void)
256{
257 return __dm_get_reserved_ios(&reserved_bio_based_ios,
258 RESERVED_BIO_BASED_IOS, RESERVED_MAX_IOS);
259}
260EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
261
Mike Snitzerf4790822013-09-12 18:06:12 -0400262unsigned dm_get_reserved_rq_based_ios(void)
263{
264 return __dm_get_reserved_ios(&reserved_rq_based_ios,
265 RESERVED_REQUEST_BASED_IOS, RESERVED_MAX_IOS);
266}
267EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios);
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269static int __init local_init(void)
270{
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100271 int r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100274 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (!_io_cache)
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100276 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000278 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
279 if (!_rq_tio_cache)
Mikulas Patockadba14162012-10-12 21:02:15 +0100280 goto out_free_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000281
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100282 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100283 if (r)
Jun'ichi Nomura23e50832013-03-01 22:45:48 +0000284 goto out_free_rq_tio_cache;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 _major = major;
287 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100288 if (r < 0)
289 goto out_uevent_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 if (!_major)
292 _major = r;
293
294 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100295
296out_uevent_exit:
297 dm_uevent_exit();
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000298out_free_rq_tio_cache:
299 kmem_cache_destroy(_rq_tio_cache);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100300out_free_io_cache:
301 kmem_cache_destroy(_io_cache);
302
303 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
306static void local_exit(void)
307{
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400308 flush_scheduled_work();
309
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000310 kmem_cache_destroy(_rq_tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700312 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100313 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 _major = 0;
316
317 DMINFO("cleaned up");
318}
319
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000320static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 local_init,
322 dm_target_init,
323 dm_linear_init,
324 dm_stripe_init,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000325 dm_io_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100326 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 dm_interface_init,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400328 dm_statistics_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329};
330
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000331static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 local_exit,
333 dm_target_exit,
334 dm_linear_exit,
335 dm_stripe_exit,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000336 dm_io_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100337 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 dm_interface_exit,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400339 dm_statistics_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340};
341
342static int __init dm_init(void)
343{
344 const int count = ARRAY_SIZE(_inits);
345
346 int r, i;
347
348 for (i = 0; i < count; i++) {
349 r = _inits[i]();
350 if (r)
351 goto bad;
352 }
353
354 return 0;
355
356 bad:
357 while (i--)
358 _exits[i]();
359
360 return r;
361}
362
363static void __exit dm_exit(void)
364{
365 int i = ARRAY_SIZE(_exits);
366
367 while (i--)
368 _exits[i]();
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100369
370 /*
371 * Should be empty by this point.
372 */
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100373 idr_destroy(&_minor_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
376/*
377 * Block device functions
378 */
Mike Anderson432a2122009-12-10 23:52:20 +0000379int dm_deleting_md(struct mapped_device *md)
380{
381 return test_bit(DMF_DELETING, &md->flags);
382}
383
Al Virofe5f9f22008-03-02 10:29:31 -0500384static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 struct mapped_device *md;
387
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700388 spin_lock(&_minor_lock);
389
Al Virofe5f9f22008-03-02 10:29:31 -0500390 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700391 if (!md)
392 goto out;
393
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700394 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +0000395 dm_deleting_md(md)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700396 md = NULL;
397 goto out;
398 }
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700401 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700402
403out:
404 spin_unlock(&_minor_lock);
405
406 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
Al Virodb2a1442013-05-05 21:52:57 -0400409static void dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Al Virofe5f9f22008-03-02 10:29:31 -0500411 struct mapped_device *md = disk->private_data;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200412
Milan Broz4a1aeb92011-01-13 19:59:48 +0000413 spin_lock(&_minor_lock);
414
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400415 if (atomic_dec_and_test(&md->open_count) &&
416 (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
417 schedule_work(&deferred_remove_work);
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 dm_put(md);
Milan Broz4a1aeb92011-01-13 19:59:48 +0000420
421 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700424int dm_open_count(struct mapped_device *md)
425{
426 return atomic_read(&md->open_count);
427}
428
429/*
430 * Guarantees nothing is using the device before it's deleted.
431 */
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400432int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700433{
434 int r = 0;
435
436 spin_lock(&_minor_lock);
437
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400438 if (dm_open_count(md)) {
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700439 r = -EBUSY;
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400440 if (mark_deferred)
441 set_bit(DMF_DEFERRED_REMOVE, &md->flags);
442 } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
443 r = -EEXIST;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700444 else
445 set_bit(DMF_DELETING, &md->flags);
446
447 spin_unlock(&_minor_lock);
448
449 return r;
450}
451
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400452int dm_cancel_deferred_remove(struct mapped_device *md)
453{
454 int r = 0;
455
456 spin_lock(&_minor_lock);
457
458 if (test_bit(DMF_DELETING, &md->flags))
459 r = -EBUSY;
460 else
461 clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
462
463 spin_unlock(&_minor_lock);
464
465 return r;
466}
467
468static void do_deferred_remove(struct work_struct *w)
469{
470 dm_deferred_remove();
471}
472
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400473sector_t dm_get_size(struct mapped_device *md)
474{
475 return get_capacity(md->disk);
476}
477
478struct dm_stats *dm_get_stats(struct mapped_device *md)
479{
480 return &md->stats;
481}
482
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800483static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
484{
485 struct mapped_device *md = bdev->bd_disk->private_data;
486
487 return dm_get_geometry(md, geo);
488}
489
Al Virofe5f9f22008-03-02 10:29:31 -0500490static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
Milan Brozaa129a22006-10-03 01:15:15 -0700491 unsigned int cmd, unsigned long arg)
492{
Al Virofe5f9f22008-03-02 10:29:31 -0500493 struct mapped_device *md = bdev->bd_disk->private_data;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100494 int srcu_idx;
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100495 struct dm_table *map;
Milan Brozaa129a22006-10-03 01:15:15 -0700496 struct dm_target *tgt;
497 int r = -ENOTTY;
498
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100499retry:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100500 map = dm_get_live_table(md, &srcu_idx);
501
Milan Brozaa129a22006-10-03 01:15:15 -0700502 if (!map || !dm_table_get_size(map))
503 goto out;
504
505 /* We only support devices that have a single target */
506 if (dm_table_get_num_targets(map) != 1)
507 goto out;
508
509 tgt = dm_table_get_target(map, 0);
510
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000511 if (dm_suspended_md(md)) {
Milan Brozaa129a22006-10-03 01:15:15 -0700512 r = -EAGAIN;
513 goto out;
514 }
515
516 if (tgt->type->ioctl)
Al Viro647b3d02007-08-28 22:15:59 -0400517 r = tgt->type->ioctl(tgt, cmd, arg);
Milan Brozaa129a22006-10-03 01:15:15 -0700518
519out:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100520 dm_put_live_table(md, srcu_idx);
Milan Brozaa129a22006-10-03 01:15:15 -0700521
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100522 if (r == -ENOTCONN) {
523 msleep(10);
524 goto retry;
525 }
526
Milan Brozaa129a22006-10-03 01:15:15 -0700527 return r;
528}
529
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100530static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
532 return mempool_alloc(md->io_pool, GFP_NOIO);
533}
534
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100535static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
537 mempool_free(io, md->io_pool);
538}
539
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100540static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Mikulas Patockadba14162012-10-12 21:02:15 +0100542 bio_put(&tio->clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000545static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
546 gfp_t gfp_mask)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100547{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +0000548 return mempool_alloc(md->io_pool, gfp_mask);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100549}
550
551static void free_rq_tio(struct dm_rq_target_io *tio)
552{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +0000553 mempool_free(tio, tio->md->io_pool);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100554}
555
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +0000556static int md_in_flight(struct mapped_device *md)
557{
558 return atomic_read(&md->pending[READ]) +
559 atomic_read(&md->pending[WRITE]);
560}
561
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800562static void start_io_acct(struct dm_io *io)
563{
564 struct mapped_device *md = io->md;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400565 struct bio *bio = io->bio;
Tejun Heoc9959052008-08-25 19:47:21 +0900566 int cpu;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400567 int rw = bio_data_dir(bio);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800568
569 io->start_time = jiffies;
570
Tejun Heo074a7ac2008-08-25 19:56:14 +0900571 cpu = part_stat_lock();
572 part_round_stats(cpu, &dm_disk(md)->part0);
573 part_stat_unlock();
Shaohua Li1e9bb882011-03-22 08:35:35 +0100574 atomic_set(&dm_disk(md)->part0.in_flight[rw],
575 atomic_inc_return(&md->pending[rw]));
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400576
577 if (unlikely(dm_stats_used(&md->stats)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700578 dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400579 bio_sectors(bio), false, 0, &io->stats_aux);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800580}
581
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000582static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800583{
584 struct mapped_device *md = io->md;
585 struct bio *bio = io->bio;
586 unsigned long duration = jiffies - io->start_time;
Tejun Heoc9959052008-08-25 19:47:21 +0900587 int pending, cpu;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800588 int rw = bio_data_dir(bio);
589
Tejun Heo074a7ac2008-08-25 19:56:14 +0900590 cpu = part_stat_lock();
591 part_round_stats(cpu, &dm_disk(md)->part0);
592 part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
593 part_stat_unlock();
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800594
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400595 if (unlikely(dm_stats_used(&md->stats)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700596 dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400597 bio_sectors(bio), true, duration, &io->stats_aux);
598
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100599 /*
600 * After this is decremented the bio must not be touched if it is
Tejun Heod87f4c12010-09-03 11:56:19 +0200601 * a flush.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100602 */
Shaohua Li1e9bb882011-03-22 08:35:35 +0100603 pending = atomic_dec_return(&md->pending[rw]);
604 atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200605 pending += atomic_read(&md->pending[rw^0x1]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800606
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000607 /* nudge anyone waiting on suspend queue */
608 if (!pending)
609 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800610}
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612/*
613 * Add the bio to the list of deferred io.
614 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100615static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200617 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200619 spin_lock_irqsave(&md->deferred_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 bio_list_add(&md->deferred, bio);
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200621 spin_unlock_irqrestore(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200622 queue_work(md->wq, &md->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
625/*
626 * Everyone (including functions in this file), should use this
627 * function to access the md->map field, and make sure they call
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100628 * dm_put_live_table() when finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100630struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100632 *srcu_idx = srcu_read_lock(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100634 return srcu_dereference(md->map, &md->io_barrier);
635}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100637void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
638{
639 srcu_read_unlock(&md->io_barrier, srcu_idx);
640}
641
642void dm_sync_table(struct mapped_device *md)
643{
644 synchronize_srcu(&md->io_barrier);
645 synchronize_rcu_expedited();
646}
647
648/*
649 * A fast alternative to dm_get_live_table/dm_put_live_table.
650 * The caller must not block between these two functions.
651 */
652static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
653{
654 rcu_read_lock();
655 return rcu_dereference(md->map);
656}
657
658static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
659{
660 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800663/*
664 * Get the geometry associated with a dm device
665 */
666int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
667{
668 *geo = md->geometry;
669
670 return 0;
671}
672
673/*
674 * Set the geometry of a device.
675 */
676int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
677{
678 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
679
680 if (geo->start > sz) {
681 DMWARN("Start sector is beyond the geometry limits.");
682 return -EINVAL;
683 }
684
685 md->geometry = *geo;
686
687 return 0;
688}
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690/*-----------------------------------------------------------------
691 * CRUD START:
692 * A more elegant soln is in the works that uses the queue
693 * merge fn, unfortunately there are a couple of changes to
694 * the block layer that I want to make for this. So in the
695 * interests of getting something for people to use I give
696 * you this clearly demarcated crap.
697 *---------------------------------------------------------------*/
698
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800699static int __noflush_suspending(struct mapped_device *md)
700{
701 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
702}
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704/*
705 * Decrements the number of outstanding ios that a bio has been
706 * cloned into, completing the original io if necc.
707 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800708static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800710 unsigned long flags;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000711 int io_error;
712 struct bio *bio;
713 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800714
715 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100716 if (unlikely(error)) {
717 spin_lock_irqsave(&io->endio_lock, flags);
718 if (!(io->error > 0 && __noflush_suspending(md)))
719 io->error = error;
720 spin_unlock_irqrestore(&io->endio_lock, flags);
721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800724 if (io->error == DM_ENDIO_REQUEUE) {
725 /*
726 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800727 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100728 spin_lock_irqsave(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200729 if (__noflush_suspending(md))
730 bio_list_add_head(&md->deferred, io->bio);
731 else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800732 /* noflush suspend was interrupted. */
733 io->error = -EIO;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100734 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800735 }
736
Milan Brozb35f8ca2009-03-16 17:44:36 +0000737 io_error = io->error;
738 bio = io->bio;
Tejun Heo6a8736d2010-09-08 18:07:00 +0200739 end_io_acct(io);
740 free_io(md, io);
Jens Axboe2056a782006-03-23 20:00:26 +0100741
Tejun Heo6a8736d2010-09-08 18:07:00 +0200742 if (io_error == DM_ENDIO_REQUEUE)
743 return;
744
Kent Overstreet4f024f32013-10-11 15:44:27 -0700745 if ((bio->bi_rw & REQ_FLUSH) && bio->bi_iter.bi_size) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100746 /*
Tejun Heo6a8736d2010-09-08 18:07:00 +0200747 * Preflush done for flush with data, reissue
748 * without REQ_FLUSH.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100749 */
Tejun Heo6a8736d2010-09-08 18:07:00 +0200750 bio->bi_rw &= ~REQ_FLUSH;
751 queue_io(md, bio);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100752 } else {
Mike Snitzerb372d362010-09-08 18:07:01 +0200753 /* done with normal IO or empty flush */
Linus Torvalds0a82a8d2013-04-18 09:00:26 -0700754 trace_block_bio_complete(md->queue, bio, io_error);
Mike Snitzerb372d362010-09-08 18:07:01 +0200755 bio_endio(bio, io_error);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758}
759
NeilBrown6712ecf2007-09-27 12:47:43 +0200760static void clone_endio(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 int r = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100763 struct dm_target_io *tio = bio->bi_private;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000764 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -0700765 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 dm_endio_fn endio = tio->ti->type->end_io;
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
769 error = -EIO;
770
771 if (endio) {
Mikulas Patocka7de3ee52012-12-21 20:23:41 +0000772 r = endio(tio->ti, bio, error);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800773 if (r < 0 || r == DM_ENDIO_REQUEUE)
774 /*
775 * error and requeue request are handled
776 * in dec_pending().
777 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800779 else if (r == DM_ENDIO_INCOMPLETE)
780 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +0200781 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800782 else if (r) {
783 DMWARN("unimplemented target endio return value: %d", r);
784 BUG();
785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
787
Stefan Bader9faf4002006-10-03 01:15:41 -0700788 free_tio(md, tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000789 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100792/*
793 * Partial completion handling for request-based dm
794 */
795static void end_clone_bio(struct bio *clone, int error)
796{
797 struct dm_rq_clone_bio_info *info = clone->bi_private;
798 struct dm_rq_target_io *tio = info->tio;
799 struct bio *bio = info->orig;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700800 unsigned int nr_bytes = info->orig->bi_iter.bi_size;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100801
802 bio_put(clone);
803
804 if (tio->error)
805 /*
806 * An error has already been detected on the request.
807 * Once error occurred, just let clone->end_io() handle
808 * the remainder.
809 */
810 return;
811 else if (error) {
812 /*
813 * Don't notice the error to the upper layer yet.
814 * The error handling decision is made by the target driver,
815 * when the request is completed.
816 */
817 tio->error = error;
818 return;
819 }
820
821 /*
822 * I/O for the bio successfully completed.
823 * Notice the data completion to the upper layer.
824 */
825
826 /*
827 * bios are processed from the head of the list.
828 * So the completing bio should always be rq->bio.
829 * If it's not, something wrong is happening.
830 */
831 if (tio->orig->bio != bio)
832 DMERR("bio completion is going in the middle of the request");
833
834 /*
835 * Update the original request.
836 * Do not use blk_end_request() here, because it may complete
837 * the original request before the clone, and break the ordering.
838 */
839 blk_update_request(tio->orig, 0, nr_bytes);
840}
841
842/*
843 * Don't touch any member of the md after calling this function because
844 * the md may be freed in dm_put() at the end of this function.
845 * Or do dm_get() before calling this function and dm_put() later.
846 */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000847static void rq_completed(struct mapped_device *md, int rw, int run_queue)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100848{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000849 atomic_dec(&md->pending[rw]);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100850
851 /* nudge anyone waiting on suspend queue */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000852 if (!md_in_flight(md))
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100853 wake_up(&md->wait);
854
Jens Axboea8c32a52012-11-06 12:24:26 +0100855 /*
856 * Run this off this callpath, as drivers could invoke end_io while
857 * inside their request_fn (and holding the queue lock). Calling
858 * back into ->request_fn() could deadlock attempting to grab the
859 * queue lock again.
860 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100861 if (run_queue)
Jens Axboea8c32a52012-11-06 12:24:26 +0100862 blk_run_queue_async(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100863
864 /*
865 * dm_put() must be at the end of this function. See the comment above
866 */
867 dm_put(md);
868}
869
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100870static void free_rq_clone(struct request *clone)
871{
872 struct dm_rq_target_io *tio = clone->end_io_data;
873
874 blk_rq_unprep_clone(clone);
875 free_rq_tio(tio);
876}
877
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000878/*
879 * Complete the clone and the original request.
880 * Must be called without queue lock.
881 */
882static void dm_end_request(struct request *clone, int error)
883{
884 int rw = rq_data_dir(clone);
885 struct dm_rq_target_io *tio = clone->end_io_data;
886 struct mapped_device *md = tio->md;
887 struct request *rq = tio->orig;
888
Tejun Heo29e40132010-09-08 18:07:00 +0200889 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000890 rq->errors = clone->errors;
891 rq->resid_len = clone->resid_len;
892
893 if (rq->sense)
894 /*
895 * We are using the sense buffer of the original
896 * request.
897 * So setting the length of the sense data is enough.
898 */
899 rq->sense_len = clone->sense_len;
900 }
901
902 free_rq_clone(clone);
Tejun Heo29e40132010-09-08 18:07:00 +0200903 blk_end_request_all(rq, error);
904 rq_completed(md, rw, true);
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000905}
906
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100907static void dm_unprep_request(struct request *rq)
908{
909 struct request *clone = rq->special;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100910
911 rq->special = NULL;
912 rq->cmd_flags &= ~REQ_DONTPREP;
913
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100914 free_rq_clone(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100915}
916
917/*
918 * Requeue the original request of a clone.
919 */
920void dm_requeue_unmapped_request(struct request *clone)
921{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000922 int rw = rq_data_dir(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100923 struct dm_rq_target_io *tio = clone->end_io_data;
924 struct mapped_device *md = tio->md;
925 struct request *rq = tio->orig;
926 struct request_queue *q = rq->q;
927 unsigned long flags;
928
929 dm_unprep_request(rq);
930
931 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100932 blk_requeue_request(q, rq);
933 spin_unlock_irqrestore(q->queue_lock, flags);
934
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000935 rq_completed(md, rw, 0);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100936}
937EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
938
939static void __stop_queue(struct request_queue *q)
940{
941 blk_stop_queue(q);
942}
943
944static void stop_queue(struct request_queue *q)
945{
946 unsigned long flags;
947
948 spin_lock_irqsave(q->queue_lock, flags);
949 __stop_queue(q);
950 spin_unlock_irqrestore(q->queue_lock, flags);
951}
952
953static void __start_queue(struct request_queue *q)
954{
955 if (blk_queue_stopped(q))
956 blk_start_queue(q);
957}
958
959static void start_queue(struct request_queue *q)
960{
961 unsigned long flags;
962
963 spin_lock_irqsave(q->queue_lock, flags);
964 __start_queue(q);
965 spin_unlock_irqrestore(q->queue_lock, flags);
966}
967
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000968static void dm_done(struct request *clone, int error, bool mapped)
969{
970 int r = error;
971 struct dm_rq_target_io *tio = clone->end_io_data;
Mike Snitzerba1cbad2012-09-26 23:45:42 +0100972 dm_request_endio_fn rq_end_io = NULL;
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000973
Mike Snitzerba1cbad2012-09-26 23:45:42 +0100974 if (tio->ti) {
975 rq_end_io = tio->ti->type->rq_end_io;
976
977 if (mapped && rq_end_io)
978 r = rq_end_io(tio->ti, clone, error, &tio->info);
979 }
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000980
981 if (r <= 0)
982 /* The target wants to complete the I/O */
983 dm_end_request(clone, r);
984 else if (r == DM_ENDIO_INCOMPLETE)
985 /* The target will handle the I/O */
986 return;
987 else if (r == DM_ENDIO_REQUEUE)
988 /* The target wants to requeue the I/O */
989 dm_requeue_unmapped_request(clone);
990 else {
991 DMWARN("unimplemented target endio return value: %d", r);
992 BUG();
993 }
994}
995
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100996/*
997 * Request completion handler for request-based dm
998 */
999static void dm_softirq_done(struct request *rq)
1000{
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001001 bool mapped = true;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001002 struct request *clone = rq->completion_data;
1003 struct dm_rq_target_io *tio = clone->end_io_data;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001004
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001005 if (rq->cmd_flags & REQ_FAILED)
1006 mapped = false;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001007
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001008 dm_done(clone, tio->error, mapped);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001009}
1010
1011/*
1012 * Complete the clone and the original request with the error status
1013 * through softirq context.
1014 */
1015static void dm_complete_request(struct request *clone, int error)
1016{
1017 struct dm_rq_target_io *tio = clone->end_io_data;
1018 struct request *rq = tio->orig;
1019
1020 tio->error = error;
1021 rq->completion_data = clone;
1022 blk_complete_request(rq);
1023}
1024
1025/*
1026 * Complete the not-mapped clone and the original request with the error status
1027 * through softirq context.
1028 * Target's rq_end_io() function isn't called.
1029 * This may be used when the target's map_rq() function fails.
1030 */
1031void dm_kill_unmapped_request(struct request *clone, int error)
1032{
1033 struct dm_rq_target_io *tio = clone->end_io_data;
1034 struct request *rq = tio->orig;
1035
1036 rq->cmd_flags |= REQ_FAILED;
1037 dm_complete_request(clone, error);
1038}
1039EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
1040
1041/*
1042 * Called with the queue lock held
1043 */
1044static void end_clone_request(struct request *clone, int error)
1045{
1046 /*
1047 * For just cleaning up the information of the queue in which
1048 * the clone was dispatched.
1049 * The clone is *NOT* freed actually here because it is alloced from
1050 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
1051 */
1052 __blk_put_request(clone->q, clone);
1053
1054 /*
1055 * Actual request completion is done in a softirq context which doesn't
1056 * hold the queue lock. Otherwise, deadlock could occur because:
1057 * - another request may be submitted by the upper level driver
1058 * of the stacking during the completion
1059 * - the submission which requires queue lock may be done
1060 * against this queue
1061 */
1062 dm_complete_request(clone, error);
1063}
1064
Mike Snitzer56a67df2010-08-12 04:14:10 +01001065/*
1066 * Return maximum size of I/O possible at the supplied sector up to the current
1067 * target boundary.
1068 */
1069static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
Mike Snitzer56a67df2010-08-12 04:14:10 +01001071 sector_t target_offset = dm_target_offset(ti, sector);
1072
1073 return ti->len - target_offset;
1074}
1075
1076static sector_t max_io_len(sector_t sector, struct dm_target *ti)
1077{
1078 sector_t len = max_io_len_target_boundary(sector, ti);
Mike Snitzer542f9032012-07-27 15:08:00 +01001079 sector_t offset, max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 /*
Mike Snitzer542f9032012-07-27 15:08:00 +01001082 * Does the target need to split even further?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 */
Mike Snitzer542f9032012-07-27 15:08:00 +01001084 if (ti->max_io_len) {
1085 offset = dm_target_offset(ti, sector);
1086 if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
1087 max_len = sector_div(offset, ti->max_io_len);
1088 else
1089 max_len = offset & (ti->max_io_len - 1);
1090 max_len = ti->max_io_len - max_len;
1091
1092 if (len > max_len)
1093 len = max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
1095
1096 return len;
1097}
1098
Mike Snitzer542f9032012-07-27 15:08:00 +01001099int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1100{
1101 if (len > UINT_MAX) {
1102 DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1103 (unsigned long long)len, UINT_MAX);
1104 ti->error = "Maximum size of target IO is too large";
1105 return -EINVAL;
1106 }
1107
1108 ti->max_io_len = (uint32_t) len;
1109
1110 return 0;
1111}
1112EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1113
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001114static void __map_bio(struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
1116 int r;
Jens Axboe2056a782006-03-23 20:00:26 +01001117 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -07001118 struct mapped_device *md;
Mikulas Patockadba14162012-10-12 21:02:15 +01001119 struct bio *clone = &tio->clone;
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001120 struct dm_target *ti = tio->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 clone->bi_end_io = clone_endio;
1123 clone->bi_private = tio;
1124
1125 /*
1126 * Map the clone. If r == 0 we don't need to do
1127 * anything, the target has assumed ownership of
1128 * this io.
1129 */
1130 atomic_inc(&tio->io->io_count);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001131 sector = clone->bi_iter.bi_sector;
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001132 r = ti->type->map(ti, clone);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001133 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +01001135
Mike Snitzerd07335e2010-11-16 12:52:38 +01001136 trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
1137 tio->io->bio->bi_bdev->bd_dev, sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001140 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
1141 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -07001142 md = tio->io->md;
1143 dec_pending(tio->io, r);
Stefan Bader9faf4002006-10-03 01:15:41 -07001144 free_tio(md, tio);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001145 } else if (r) {
1146 DMWARN("unimplemented target map return value: %d", r);
1147 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
1149}
1150
1151struct clone_info {
1152 struct mapped_device *md;
1153 struct dm_table *map;
1154 struct bio *bio;
1155 struct dm_io *io;
1156 sector_t sector;
1157 sector_t sector_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158};
1159
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001160static void bio_setup_sector(struct bio *bio, sector_t sector, sector_t len)
1161{
Kent Overstreet4f024f32013-10-11 15:44:27 -07001162 bio->bi_iter.bi_sector = sector;
1163 bio->bi_iter.bi_size = to_bytes(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164}
1165
1166/*
1167 * Creates a bio that consists of range of complete bvecs.
1168 */
Mikulas Patockadba14162012-10-12 21:02:15 +01001169static void clone_bio(struct dm_target_io *tio, struct bio *bio,
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001170 sector_t sector, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
Mikulas Patockadba14162012-10-12 21:02:15 +01001172 struct bio *clone = &tio->clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001174 __bio_clone_fast(clone, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001176 if (bio_integrity(bio))
1177 bio_integrity_clone(clone, bio, GFP_NOIO);
1178
1179 bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
1180 clone->bi_iter.bi_size = to_bytes(len);
1181
1182 if (bio_integrity(bio))
1183 bio_integrity_trim(clone, 0, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184}
1185
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001186static struct dm_target_io *alloc_tio(struct clone_info *ci,
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001187 struct dm_target *ti, int nr_iovecs,
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001188 unsigned target_bio_nr)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001189{
Mikulas Patockadba14162012-10-12 21:02:15 +01001190 struct dm_target_io *tio;
1191 struct bio *clone;
1192
1193 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, ci->md->bs);
1194 tio = container_of(clone, struct dm_target_io, clone);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001195
1196 tio->io = ci->io;
1197 tio->ti = ti;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001198 memset(&tio->info, 0, sizeof(tio->info));
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001199 tio->target_bio_nr = target_bio_nr;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001200
1201 return tio;
1202}
1203
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001204static void __clone_and_map_simple_bio(struct clone_info *ci,
1205 struct dm_target *ti,
1206 unsigned target_bio_nr, sector_t len)
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001207{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001208 struct dm_target_io *tio = alloc_tio(ci, ti, ci->bio->bi_max_vecs, target_bio_nr);
Mikulas Patockadba14162012-10-12 21:02:15 +01001209 struct bio *clone = &tio->clone;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001210
Mike Snitzer06a426c2010-08-12 04:14:09 +01001211 /*
1212 * Discard requests require the bio's inline iovecs be initialized.
1213 * ci->bio->bi_max_vecs is BIO_INLINE_VECS anyway, for both flush
1214 * and discard, so no need for concern about wasted bvec allocations.
1215 */
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001216 __bio_clone_fast(clone, ci->bio);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001217 if (len)
1218 bio_setup_sector(clone, ci->sector, len);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001219
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001220 __map_bio(tio);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001221}
1222
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001223static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
1224 unsigned num_bios, sector_t len)
Mike Snitzer06a426c2010-08-12 04:14:09 +01001225{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001226 unsigned target_bio_nr;
Mike Snitzer06a426c2010-08-12 04:14:09 +01001227
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001228 for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001229 __clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
Mike Snitzer06a426c2010-08-12 04:14:09 +01001230}
1231
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001232static int __send_empty_flush(struct clone_info *ci)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001233{
Mike Snitzer06a426c2010-08-12 04:14:09 +01001234 unsigned target_nr = 0;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001235 struct dm_target *ti;
1236
Mike Snitzerb372d362010-09-08 18:07:01 +02001237 BUG_ON(bio_has_data(ci->bio));
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001238 while ((ti = dm_table_get_target(ci->map, target_nr++)))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001239 __send_duplicate_bios(ci, ti, ti->num_flush_bios, 0);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001240
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001241 return 0;
1242}
1243
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001244static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001245 sector_t sector, unsigned len)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001246{
Mikulas Patockadba14162012-10-12 21:02:15 +01001247 struct bio *bio = ci->bio;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001248 struct dm_target_io *tio;
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001249 unsigned target_bio_nr;
1250 unsigned num_target_bios = 1;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001251
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001252 /*
1253 * Does the target want to receive duplicate copies of the bio?
1254 */
1255 if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1256 num_target_bios = ti->num_write_bios(ti, bio);
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001257
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001258 for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001259 tio = alloc_tio(ci, ti, 0, target_bio_nr);
1260 clone_bio(tio, bio, sector, len);
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001261 __map_bio(tio);
1262 }
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001263}
1264
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001265typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
Mike Snitzer23508a92012-12-21 20:23:37 +00001266
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001267static unsigned get_num_discard_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001268{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001269 return ti->num_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001270}
1271
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001272static unsigned get_num_write_same_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001273{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001274 return ti->num_write_same_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001275}
1276
1277typedef bool (*is_split_required_fn)(struct dm_target *ti);
1278
1279static bool is_split_required_for_discard(struct dm_target *ti)
1280{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001281 return ti->split_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001282}
1283
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001284static int __send_changing_extent_only(struct clone_info *ci,
1285 get_num_bios_fn get_num_bios,
1286 is_split_required_fn is_split_required)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001287{
1288 struct dm_target *ti;
Mike Snitzera79245b2010-08-12 04:14:24 +01001289 sector_t len;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001290 unsigned num_bios;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001291
Mike Snitzera79245b2010-08-12 04:14:24 +01001292 do {
1293 ti = dm_table_find_target(ci->map, ci->sector);
1294 if (!dm_target_is_valid(ti))
1295 return -EIO;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001296
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001297 /*
Mike Snitzer23508a92012-12-21 20:23:37 +00001298 * Even though the device advertised support for this type of
1299 * request, that does not mean every target supports it, and
Mike Snitzer936688d2011-08-02 12:32:01 +01001300 * reconfiguration might also have changed that since the
Mike Snitzera79245b2010-08-12 04:14:24 +01001301 * check was performed.
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001302 */
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001303 num_bios = get_num_bios ? get_num_bios(ti) : 0;
1304 if (!num_bios)
Mike Snitzera79245b2010-08-12 04:14:24 +01001305 return -EOPNOTSUPP;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001306
Mike Snitzer23508a92012-12-21 20:23:37 +00001307 if (is_split_required && !is_split_required(ti))
Mikulas Patocka7acf0272012-07-27 15:08:03 +01001308 len = min(ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
1309 else
1310 len = min(ci->sector_count, max_io_len(ci->sector, ti));
Mike Snitzer06a426c2010-08-12 04:14:09 +01001311
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001312 __send_duplicate_bios(ci, ti, num_bios, len);
Mike Snitzera79245b2010-08-12 04:14:24 +01001313
1314 ci->sector += len;
1315 } while (ci->sector_count -= len);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001316
1317 return 0;
1318}
1319
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001320static int __send_discard(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001321{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001322 return __send_changing_extent_only(ci, get_num_discard_bios,
1323 is_split_required_for_discard);
Mike Snitzer23508a92012-12-21 20:23:37 +00001324}
1325
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001326static int __send_write_same(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001327{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001328 return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
Mike Snitzer23508a92012-12-21 20:23:37 +00001329}
1330
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001331/*
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001332 * Select the correct strategy for processing a non-flush bio.
1333 */
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001334static int __split_and_process_non_flush(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
Mikulas Patockadba14162012-10-12 21:02:15 +01001336 struct bio *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001337 struct dm_target *ti;
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001338 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001340 if (unlikely(bio->bi_rw & REQ_DISCARD))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001341 return __send_discard(ci);
Mike Snitzer23508a92012-12-21 20:23:37 +00001342 else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001343 return __send_write_same(ci);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001344
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001345 ti = dm_table_find_target(ci->map, ci->sector);
1346 if (!dm_target_is_valid(ti))
1347 return -EIO;
1348
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001349 len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001350
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001351 __clone_and_map_data_bio(ci, ti, ci->sector, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001353 ci->sector += len;
1354 ci->sector_count -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
1358
1359/*
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001360 * Entry point to split a bio into clones and submit them to the targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001362static void __split_and_process_bio(struct mapped_device *md,
1363 struct dm_table *map, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
1365 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001366 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001368 if (unlikely(!map)) {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001369 bio_io_error(bio);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001370 return;
1371 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001372
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001373 ci.map = map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 ci.md = md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 ci.io = alloc_io(md);
1376 ci.io->error = 0;
1377 atomic_set(&ci.io->io_count, 1);
1378 ci.io->bio = bio;
1379 ci.io->md = md;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +01001380 spin_lock_init(&ci.io->endio_lock);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001381 ci.sector = bio->bi_iter.bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001383 start_io_acct(ci.io);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001384
Mike Snitzerb372d362010-09-08 18:07:01 +02001385 if (bio->bi_rw & REQ_FLUSH) {
1386 ci.bio = &ci.md->flush_bio;
1387 ci.sector_count = 0;
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001388 error = __send_empty_flush(&ci);
Mike Snitzerb372d362010-09-08 18:07:01 +02001389 /* dec_pending submits any data associated with flush */
1390 } else {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001391 ci.bio = bio;
Tejun Heod87f4c12010-09-03 11:56:19 +02001392 ci.sector_count = bio_sectors(bio);
Mike Snitzerb372d362010-09-08 18:07:01 +02001393 while (ci.sector_count && !error)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001394 error = __split_and_process_non_flush(&ci);
Tejun Heod87f4c12010-09-03 11:56:19 +02001395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001398 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399}
1400/*-----------------------------------------------------------------
1401 * CRUD END
1402 *---------------------------------------------------------------*/
1403
Milan Brozf6fccb12008-07-21 12:00:37 +01001404static int dm_merge_bvec(struct request_queue *q,
1405 struct bvec_merge_data *bvm,
1406 struct bio_vec *biovec)
1407{
1408 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001409 struct dm_table *map = dm_get_live_table_fast(md);
Milan Brozf6fccb12008-07-21 12:00:37 +01001410 struct dm_target *ti;
1411 sector_t max_sectors;
Mikulas Patocka50371082008-10-01 14:39:17 +01001412 int max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001413
1414 if (unlikely(!map))
Mikulas Patocka50371082008-10-01 14:39:17 +01001415 goto out;
Milan Brozf6fccb12008-07-21 12:00:37 +01001416
1417 ti = dm_table_find_target(map, bvm->bi_sector);
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001418 if (!dm_target_is_valid(ti))
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001419 goto out;
Milan Brozf6fccb12008-07-21 12:00:37 +01001420
1421 /*
1422 * Find maximum amount of I/O that won't need splitting
1423 */
Mike Snitzer56a67df2010-08-12 04:14:10 +01001424 max_sectors = min(max_io_len(bvm->bi_sector, ti),
Milan Brozf6fccb12008-07-21 12:00:37 +01001425 (sector_t) BIO_MAX_SECTORS);
1426 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1427 if (max_size < 0)
1428 max_size = 0;
1429
1430 /*
1431 * merge_bvec_fn() returns number of bytes
1432 * it can accept at this offset
1433 * max is precomputed maximal io size
1434 */
1435 if (max_size && ti->type->merge)
1436 max_size = ti->type->merge(ti, bvm, biovec, max_size);
Mikulas Patocka8cbeb672009-06-22 10:12:14 +01001437 /*
1438 * If the target doesn't support merge method and some of the devices
1439 * provided their merge_bvec method (we know this by looking at
1440 * queue_max_hw_sectors), then we can't allow bios with multiple vector
1441 * entries. So always set max_size to 0, and the code below allows
1442 * just one page.
1443 */
1444 else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
1445
1446 max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001447
Mikulas Patocka50371082008-10-01 14:39:17 +01001448out:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001449 dm_put_live_table_fast(md);
Milan Brozf6fccb12008-07-21 12:00:37 +01001450 /*
1451 * Always allow an entire first page
1452 */
1453 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1454 max_size = biovec->bv_len;
1455
Milan Brozf6fccb12008-07-21 12:00:37 +01001456 return max_size;
1457}
1458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459/*
1460 * The request function that just remaps the bio built up by
1461 * dm_merge_bvec.
1462 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001463static void _dm_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
Kevin Corry12f03a42006-02-01 03:04:52 -08001465 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 struct mapped_device *md = q->queuedata;
Tejun Heoc9959052008-08-25 19:47:21 +09001467 int cpu;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001468 int srcu_idx;
1469 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001471 map = dm_get_live_table(md, &srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Tejun Heo074a7ac2008-08-25 19:56:14 +09001473 cpu = part_stat_lock();
1474 part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1475 part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1476 part_stat_unlock();
Kevin Corry12f03a42006-02-01 03:04:52 -08001477
Tejun Heo6a8736d2010-09-08 18:07:00 +02001478 /* if we're suspended, we have to queue this io for later */
1479 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001480 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Tejun Heo6a8736d2010-09-08 18:07:00 +02001482 if (bio_rw(bio) != READA)
1483 queue_io(md, bio);
1484 else
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001485 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001486 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
1488
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001489 __split_and_process_bio(md, map, bio);
1490 dm_put_live_table(md, srcu_idx);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001491 return;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001492}
1493
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001494int dm_request_based(struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001495{
1496 return blk_queue_stackable(md->queue);
1497}
1498
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001499static void dm_request(struct request_queue *q, struct bio *bio)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001500{
1501 struct mapped_device *md = q->queuedata;
1502
1503 if (dm_request_based(md))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001504 blk_queue_bio(q, bio);
1505 else
1506 _dm_request(q, bio);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001507}
1508
1509void dm_dispatch_request(struct request *rq)
1510{
1511 int r;
1512
1513 if (blk_queue_io_stat(rq->q))
1514 rq->cmd_flags |= REQ_IO_STAT;
1515
1516 rq->start_time = jiffies;
1517 r = blk_insert_cloned_request(rq->q, rq);
1518 if (r)
1519 dm_complete_request(rq, r);
1520}
1521EXPORT_SYMBOL_GPL(dm_dispatch_request);
1522
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001523static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1524 void *data)
1525{
1526 struct dm_rq_target_io *tio = data;
Kent Overstreet94818742012-09-07 13:44:01 -07001527 struct dm_rq_clone_bio_info *info =
1528 container_of(bio, struct dm_rq_clone_bio_info, clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001529
1530 info->orig = bio_orig;
1531 info->tio = tio;
1532 bio->bi_end_io = end_clone_bio;
1533 bio->bi_private = info;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001534
1535 return 0;
1536}
1537
1538static int setup_clone(struct request *clone, struct request *rq,
1539 struct dm_rq_target_io *tio)
1540{
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001541 int r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001542
Tejun Heo29e40132010-09-08 18:07:00 +02001543 r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1544 dm_rq_bio_constructor, tio);
1545 if (r)
1546 return r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001547
Tejun Heo29e40132010-09-08 18:07:00 +02001548 clone->cmd = rq->cmd;
1549 clone->cmd_len = rq->cmd_len;
1550 clone->sense = rq->sense;
1551 clone->buffer = rq->buffer;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001552 clone->end_io = end_clone_request;
1553 clone->end_io_data = tio;
1554
1555 return 0;
1556}
1557
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001558static struct request *clone_rq(struct request *rq, struct mapped_device *md,
1559 gfp_t gfp_mask)
1560{
1561 struct request *clone;
1562 struct dm_rq_target_io *tio;
1563
1564 tio = alloc_rq_tio(md, gfp_mask);
1565 if (!tio)
1566 return NULL;
1567
1568 tio->md = md;
1569 tio->ti = NULL;
1570 tio->orig = rq;
1571 tio->error = 0;
1572 memset(&tio->info, 0, sizeof(tio->info));
1573
1574 clone = &tio->clone;
1575 if (setup_clone(clone, rq, tio)) {
1576 /* -ENOMEM */
1577 free_rq_tio(tio);
1578 return NULL;
1579 }
1580
1581 return clone;
1582}
1583
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001584/*
1585 * Called with the queue lock held.
1586 */
1587static int dm_prep_fn(struct request_queue *q, struct request *rq)
1588{
1589 struct mapped_device *md = q->queuedata;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001590 struct request *clone;
1591
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001592 if (unlikely(rq->special)) {
1593 DMWARN("Already has something in rq->special.");
1594 return BLKPREP_KILL;
1595 }
1596
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001597 clone = clone_rq(rq, md, GFP_ATOMIC);
1598 if (!clone)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001599 return BLKPREP_DEFER;
1600
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001601 rq->special = clone;
1602 rq->cmd_flags |= REQ_DONTPREP;
1603
1604 return BLKPREP_OK;
1605}
1606
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001607/*
1608 * Returns:
1609 * 0 : the request has been processed (not requeued)
1610 * !0 : the request has been requeued
1611 */
1612static int map_request(struct dm_target *ti, struct request *clone,
1613 struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001614{
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001615 int r, requeued = 0;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001616 struct dm_rq_target_io *tio = clone->end_io_data;
1617
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001618 tio->ti = ti;
1619 r = ti->type->map_rq(ti, clone, &tio->info);
1620 switch (r) {
1621 case DM_MAPIO_SUBMITTED:
1622 /* The target has taken the I/O to submit by itself later */
1623 break;
1624 case DM_MAPIO_REMAPPED:
1625 /* The target has remapped the I/O so dispatch it */
Jun'ichi Nomura6db4ccd2009-12-10 23:52:25 +00001626 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
1627 blk_rq_pos(tio->orig));
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001628 dm_dispatch_request(clone);
1629 break;
1630 case DM_MAPIO_REQUEUE:
1631 /* The target wants to requeue the I/O */
1632 dm_requeue_unmapped_request(clone);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001633 requeued = 1;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001634 break;
1635 default:
1636 if (r > 0) {
1637 DMWARN("unimplemented target map return value: %d", r);
1638 BUG();
1639 }
1640
1641 /* The target wants to complete the I/O */
1642 dm_kill_unmapped_request(clone, r);
1643 break;
1644 }
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001645
1646 return requeued;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001647}
1648
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001649static struct request *dm_start_request(struct mapped_device *md, struct request *orig)
1650{
1651 struct request *clone;
1652
1653 blk_start_request(orig);
1654 clone = orig->special;
1655 atomic_inc(&md->pending[rq_data_dir(clone)]);
1656
1657 /*
1658 * Hold the md reference here for the in-flight I/O.
1659 * We can't rely on the reference count by device opener,
1660 * because the device may be closed during the request completion
1661 * when all bios are completed.
1662 * See the comment in rq_completed() too.
1663 */
1664 dm_get(md);
1665
1666 return clone;
1667}
1668
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001669/*
1670 * q->request_fn for request-based dm.
1671 * Called with the queue lock held.
1672 */
1673static void dm_request_fn(struct request_queue *q)
1674{
1675 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001676 int srcu_idx;
1677 struct dm_table *map = dm_get_live_table(md, &srcu_idx);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001678 struct dm_target *ti;
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001679 struct request *rq, *clone;
Tejun Heo29e40132010-09-08 18:07:00 +02001680 sector_t pos;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001681
1682 /*
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001683 * For suspend, check blk_queue_stopped() and increment
1684 * ->pending within a single queue_lock not to increment the
1685 * number of in-flight I/Os after the queue is stopped in
1686 * dm_suspend().
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001687 */
Jens Axboe7eaceac2011-03-10 08:52:07 +01001688 while (!blk_queue_stopped(q)) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001689 rq = blk_peek_request(q);
1690 if (!rq)
Jens Axboe7eaceac2011-03-10 08:52:07 +01001691 goto delay_and_out;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001692
Tejun Heo29e40132010-09-08 18:07:00 +02001693 /* always use block 0 to find the target for flushes for now */
1694 pos = 0;
1695 if (!(rq->cmd_flags & REQ_FLUSH))
1696 pos = blk_rq_pos(rq);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001697
Tejun Heo29e40132010-09-08 18:07:00 +02001698 ti = dm_table_find_target(map, pos);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001699 if (!dm_target_is_valid(ti)) {
1700 /*
1701 * Must perform setup, that dm_done() requires,
1702 * before calling dm_kill_unmapped_request
1703 */
1704 DMERR_LIMIT("request attempted access beyond the end of device");
1705 clone = dm_start_request(md, rq);
1706 dm_kill_unmapped_request(clone, -EIO);
1707 continue;
1708 }
Tejun Heo29e40132010-09-08 18:07:00 +02001709
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001710 if (ti->type->busy && ti->type->busy(ti))
Jens Axboe7eaceac2011-03-10 08:52:07 +01001711 goto delay_and_out;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001712
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001713 clone = dm_start_request(md, rq);
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001714
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001715 spin_unlock(q->queue_lock);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001716 if (map_request(ti, clone, md))
1717 goto requeued;
1718
Kiyoshi Ueda052189a2011-01-13 20:00:00 +00001719 BUG_ON(!irqs_disabled());
1720 spin_lock(q->queue_lock);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001721 }
1722
1723 goto out;
1724
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001725requeued:
Kiyoshi Ueda052189a2011-01-13 20:00:00 +00001726 BUG_ON(!irqs_disabled());
1727 spin_lock(q->queue_lock);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001728
Jens Axboe7eaceac2011-03-10 08:52:07 +01001729delay_and_out:
1730 blk_delay_queue(q, HZ / 10);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001731out:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001732 dm_put_live_table(md, srcu_idx);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001733}
1734
1735int dm_underlying_device_busy(struct request_queue *q)
1736{
1737 return blk_lld_busy(q);
1738}
1739EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1740
1741static int dm_lld_busy(struct request_queue *q)
1742{
1743 int r;
1744 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001745 struct dm_table *map = dm_get_live_table_fast(md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001746
1747 if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1748 r = 1;
1749 else
1750 r = dm_table_any_busy_target(map);
1751
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001752 dm_put_live_table_fast(md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001753
1754 return r;
1755}
1756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757static int dm_any_congested(void *congested_data, int bdi_bits)
1758{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001759 int r = bdi_bits;
1760 struct mapped_device *md = congested_data;
1761 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001763 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001764 map = dm_get_live_table_fast(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001765 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001766 /*
1767 * Request-based dm cares about only own queue for
1768 * the query about congestion status of request_queue
1769 */
1770 if (dm_request_based(md))
1771 r = md->queue->backing_dev_info.state &
1772 bdi_bits;
1773 else
1774 r = dm_table_any_congested(map, bdi_bits);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001775 }
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001776 dm_put_live_table_fast(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001777 }
1778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 return r;
1780}
1781
1782/*-----------------------------------------------------------------
1783 * An IDR is used to keep track of allocated minor numbers.
1784 *---------------------------------------------------------------*/
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001785static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001787 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001789 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790}
1791
1792/*
1793 * See if the device with a specific minor # is free.
1794 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001795static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
Tejun Heoc9d76be2013-02-27 17:04:26 -08001797 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
1799 if (minor >= (1 << MINORBITS))
1800 return -EINVAL;
1801
Tejun Heoc9d76be2013-02-27 17:04:26 -08001802 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001803 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Tejun Heoc9d76be2013-02-27 17:04:26 -08001805 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001807 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08001808 idr_preload_end();
1809 if (r < 0)
1810 return r == -ENOSPC ? -EBUSY : r;
1811 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812}
1813
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001814static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815{
Tejun Heoc9d76be2013-02-27 17:04:26 -08001816 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Tejun Heoc9d76be2013-02-27 17:04:26 -08001818 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001819 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Tejun Heoc9d76be2013-02-27 17:04:26 -08001821 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001823 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08001824 idr_preload_end();
1825 if (r < 0)
1826 return r;
1827 *minor = r;
1828 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829}
1830
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001831static const struct block_device_operations dm_blk_dops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Mikulas Patocka53d59142009-04-02 19:55:37 +01001833static void dm_wq_work(struct work_struct *work);
1834
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001835static void dm_init_md_queue(struct mapped_device *md)
1836{
1837 /*
1838 * Request-based dm devices cannot be stacked on top of bio-based dm
1839 * devices. The type of this dm device has not been decided yet.
1840 * The type is decided at the first table loading time.
1841 * To prevent problematic device stacking, clear the queue flag
1842 * for request stacking support until then.
1843 *
1844 * This queue is new, so no concurrency on the queue_flags.
1845 */
1846 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1847
1848 md->queue->queuedata = md;
1849 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1850 md->queue->backing_dev_info.congested_data = md;
1851 blk_queue_make_request(md->queue, dm_request);
1852 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001853 blk_queue_merge_bvec(md->queue, dm_merge_bvec);
1854}
1855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856/*
1857 * Allocate and initialise a blank device with a given minor.
1858 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001859static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
1861 int r;
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001862 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001863 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
1865 if (!md) {
1866 DMWARN("unable to allocate device, out of memory.");
1867 return NULL;
1868 }
1869
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001870 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00001871 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001874 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001875 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001876 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001877 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001879 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001881 r = init_srcu_struct(&md->io_barrier);
1882 if (r < 0)
1883 goto bad_io_barrier;
1884
Mike Snitzera5664da2010-08-12 04:14:01 +01001885 md->type = DM_TYPE_NONE;
Daniel Walkere61290a2008-02-08 02:10:08 +00001886 mutex_init(&md->suspend_lock);
Mike Snitzera5664da2010-08-12 04:14:01 +01001887 mutex_init(&md->type_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01001888 spin_lock_init(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07001890 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001892 atomic_set(&md->uevent_seq, 0);
1893 INIT_LIST_HEAD(&md->uevent_list);
1894 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001896 md->queue = blk_alloc_queue(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 if (!md->queue)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001898 goto bad_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001900 dm_init_md_queue(md);
Stefan Bader9faf4002006-10-03 01:15:41 -07001901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 md->disk = alloc_disk(1);
1903 if (!md->disk)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001904 goto bad_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001906 atomic_set(&md->pending[0], 0);
1907 atomic_set(&md->pending[1], 0);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001908 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01001909 INIT_WORK(&md->work, dm_wq_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001910 init_waitqueue_head(&md->eventq);
Mikulas Patocka2995fa72014-01-13 19:37:54 -05001911 init_completion(&md->kobj_holder.completion);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001912
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 md->disk->major = _major;
1914 md->disk->first_minor = minor;
1915 md->disk->fops = &dm_blk_dops;
1916 md->disk->queue = md->queue;
1917 md->disk->private_data = md;
1918 sprintf(md->disk->disk_name, "dm-%d", minor);
1919 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08001920 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Tejun Heo670368a2013-07-30 08:40:21 -04001922 md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
Milan Broz304f3f62008-02-08 02:11:17 +00001923 if (!md->wq)
1924 goto bad_thread;
1925
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001926 md->bdev = bdget_disk(md->disk, 0);
1927 if (!md->bdev)
1928 goto bad_bdev;
1929
Tejun Heo6a8736d2010-09-08 18:07:00 +02001930 bio_init(&md->flush_bio);
1931 md->flush_bio.bi_bdev = md->bdev;
1932 md->flush_bio.bi_rw = WRITE_FLUSH;
1933
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001934 dm_stats_init(&md->stats);
1935
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001936 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001937 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001938 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001939 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001940
1941 BUG_ON(old_md != MINOR_ALLOCED);
1942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 return md;
1944
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001945bad_bdev:
1946 destroy_workqueue(md->wq);
Milan Broz304f3f62008-02-08 02:11:17 +00001947bad_thread:
Zdenek Kabelac03022c52009-10-16 23:18:15 +01001948 del_gendisk(md->disk);
Milan Broz304f3f62008-02-08 02:11:17 +00001949 put_disk(md->disk);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001950bad_disk:
Al Viro1312f402006-03-12 11:02:03 -05001951 blk_cleanup_queue(md->queue);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001952bad_queue:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001953 cleanup_srcu_struct(&md->io_barrier);
1954bad_io_barrier:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001956bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001957 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001958bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 kfree(md);
1960 return NULL;
1961}
1962
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01001963static void unlock_fs(struct mapped_device *md);
1964
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965static void free_dev(struct mapped_device *md)
1966{
Tejun Heof331c022008-09-03 09:01:48 +02001967 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001968
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001969 unlock_fs(md);
1970 bdput(md->bdev);
Milan Broz304f3f62008-02-08 02:11:17 +00001971 destroy_workqueue(md->wq);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001972 if (md->io_pool)
1973 mempool_destroy(md->io_pool);
1974 if (md->bs)
1975 bioset_free(md->bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001976 blk_integrity_unregister(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 del_gendisk(md->disk);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001978 cleanup_srcu_struct(&md->io_barrier);
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001979 free_minor(minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001980
1981 spin_lock(&_minor_lock);
1982 md->disk->private_data = NULL;
1983 spin_unlock(&_minor_lock);
1984
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 put_disk(md->disk);
Al Viro1312f402006-03-12 11:02:03 -05001986 blk_cleanup_queue(md->queue);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001987 dm_stats_cleanup(&md->stats);
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001988 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 kfree(md);
1990}
1991
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001992static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1993{
Mikulas Patockac0820cf2012-12-21 20:23:38 +00001994 struct dm_md_mempools *p = dm_table_get_md_mempools(t);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001995
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00001996 if (md->io_pool && md->bs) {
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00001997 /* The md already has necessary mempools. */
1998 if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
1999 /*
2000 * Reload bioset because front_pad may have changed
2001 * because a different table was loaded.
2002 */
2003 bioset_free(md->bs);
2004 md->bs = p->bs;
2005 p->bs = NULL;
2006 } else if (dm_table_get_type(t) == DM_TYPE_REQUEST_BASED) {
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00002007 /*
2008 * There's no need to reload with request-based dm
2009 * because the size of front_pad doesn't change.
2010 * Note for future: If you are to reload bioset,
2011 * prep-ed requests in the queue may refer
2012 * to bio from the old bioset, so you must walk
2013 * through the queue to unprep.
2014 */
2015 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002016 goto out;
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002017 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002018
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002019 BUG_ON(!p || md->io_pool || md->bs);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002020
2021 md->io_pool = p->io_pool;
2022 p->io_pool = NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002023 md->bs = p->bs;
2024 p->bs = NULL;
2025
2026out:
2027 /* mempool bind completed, now no need any mempools in the table */
2028 dm_table_free_md_mempools(t);
2029}
2030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031/*
2032 * Bind a table to the device.
2033 */
2034static void event_callback(void *context)
2035{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002036 unsigned long flags;
2037 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 struct mapped_device *md = (struct mapped_device *) context;
2039
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002040 spin_lock_irqsave(&md->uevent_lock, flags);
2041 list_splice_init(&md->uevent_list, &uevents);
2042 spin_unlock_irqrestore(&md->uevent_lock, flags);
2043
Tejun Heoed9e1982008-08-25 19:56:05 +09002044 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 atomic_inc(&md->event_nr);
2047 wake_up(&md->eventq);
2048}
2049
Mike Snitzerc2176492011-01-13 19:53:46 +00002050/*
2051 * Protected by md->suspend_lock obtained by dm_swap_table().
2052 */
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002053static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002055 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002057 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058}
2059
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002060/*
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01002061 * Return 1 if the queue has a compulsory merge_bvec_fn function.
2062 *
2063 * If this function returns 0, then the device is either a non-dm
2064 * device without a merge_bvec_fn, or it is a dm device that is
2065 * able to split any bios it receives that are too big.
2066 */
2067int dm_queue_merge_is_compulsory(struct request_queue *q)
2068{
2069 struct mapped_device *dev_md;
2070
2071 if (!q->merge_bvec_fn)
2072 return 0;
2073
2074 if (q->make_request_fn == dm_request) {
2075 dev_md = q->queuedata;
2076 if (test_bit(DMF_MERGE_IS_OPTIONAL, &dev_md->flags))
2077 return 0;
2078 }
2079
2080 return 1;
2081}
2082
2083static int dm_device_merge_is_compulsory(struct dm_target *ti,
2084 struct dm_dev *dev, sector_t start,
2085 sector_t len, void *data)
2086{
2087 struct block_device *bdev = dev->bdev;
2088 struct request_queue *q = bdev_get_queue(bdev);
2089
2090 return dm_queue_merge_is_compulsory(q);
2091}
2092
2093/*
2094 * Return 1 if it is acceptable to ignore merge_bvec_fn based
2095 * on the properties of the underlying devices.
2096 */
2097static int dm_table_merge_is_optional(struct dm_table *table)
2098{
2099 unsigned i = 0;
2100 struct dm_target *ti;
2101
2102 while (i < dm_table_get_num_targets(table)) {
2103 ti = dm_table_get_target(table, i++);
2104
2105 if (ti->type->iterate_devices &&
2106 ti->type->iterate_devices(ti, dm_device_merge_is_compulsory, NULL))
2107 return 0;
2108 }
2109
2110 return 1;
2111}
2112
2113/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002114 * Returns old map, which caller must destroy.
2115 */
2116static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2117 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002119 struct dm_table *old_map;
Jens Axboe165125e2007-07-24 09:28:11 +02002120 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 sector_t size;
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01002122 int merge_is_optional;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
2124 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002125
2126 /*
2127 * Wipe any geometry if the size of the table changed.
2128 */
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002129 if (size != dm_get_size(md))
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002130 memset(&md->geometry, 0, sizeof(md->geometry));
2131
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002132 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002134 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002135
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002136 /*
2137 * The queue hasn't been stopped yet, if the old table type wasn't
2138 * for request-based during suspension. So stop it to prevent
2139 * I/O mapping before resume.
2140 * This must be done before setting the queue restrictions,
2141 * because request-based dm may be run just after the setting.
2142 */
2143 if (dm_table_request_based(t) && !blk_queue_stopped(q))
2144 stop_queue(q);
2145
2146 __bind_mempools(md, t);
2147
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01002148 merge_is_optional = dm_table_merge_is_optional(t);
2149
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002150 old_map = md->map;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002151 rcu_assign_pointer(md->map, t);
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002152 md->immutable_target_type = dm_table_get_immutable_target_type(t);
2153
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002154 dm_table_set_restrictions(t, q, limits);
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01002155 if (merge_is_optional)
2156 set_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
2157 else
2158 clear_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002159 dm_sync_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002160
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002161 return old_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162}
2163
Alasdair G Kergona7940152009-12-10 23:52:23 +00002164/*
2165 * Returns unbound table for the caller to free.
2166 */
2167static struct dm_table *__unbind(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
2169 struct dm_table *map = md->map;
2170
2171 if (!map)
Alasdair G Kergona7940152009-12-10 23:52:23 +00002172 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
2174 dm_table_event_callback(map, NULL, NULL);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002175 rcu_assign_pointer(md->map, NULL);
2176 dm_sync_table(md);
Alasdair G Kergona7940152009-12-10 23:52:23 +00002177
2178 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179}
2180
2181/*
2182 * Constructor for a new device.
2183 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002184int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
2186 struct mapped_device *md;
2187
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002188 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 if (!md)
2190 return -ENXIO;
2191
Milan Broz784aae72009-01-06 03:05:12 +00002192 dm_sysfs_init(md);
2193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 *result = md;
2195 return 0;
2196}
2197
Mike Snitzera5664da2010-08-12 04:14:01 +01002198/*
2199 * Functions to manage md->type.
2200 * All are required to hold md->type_lock.
2201 */
2202void dm_lock_md_type(struct mapped_device *md)
2203{
2204 mutex_lock(&md->type_lock);
2205}
2206
2207void dm_unlock_md_type(struct mapped_device *md)
2208{
2209 mutex_unlock(&md->type_lock);
2210}
2211
2212void dm_set_md_type(struct mapped_device *md, unsigned type)
2213{
Mike Snitzer00c4fc32013-08-27 18:57:03 -04002214 BUG_ON(!mutex_is_locked(&md->type_lock));
Mike Snitzera5664da2010-08-12 04:14:01 +01002215 md->type = type;
2216}
2217
2218unsigned dm_get_md_type(struct mapped_device *md)
2219{
Mike Snitzer00c4fc32013-08-27 18:57:03 -04002220 BUG_ON(!mutex_is_locked(&md->type_lock));
Mike Snitzera5664da2010-08-12 04:14:01 +01002221 return md->type;
2222}
2223
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002224struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
2225{
2226 return md->immutable_target_type;
2227}
2228
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002229/*
Mike Snitzerf84cb8a2013-09-19 12:13:58 -04002230 * The queue_limits are only valid as long as you have a reference
2231 * count on 'md'.
2232 */
2233struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2234{
2235 BUG_ON(!atomic_read(&md->holders));
2236 return &md->queue->limits;
2237}
2238EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2239
2240/*
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002241 * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
2242 */
2243static int dm_init_request_based_queue(struct mapped_device *md)
2244{
2245 struct request_queue *q = NULL;
2246
2247 if (md->queue->elevator)
2248 return 1;
2249
2250 /* Fully initialize the queue */
2251 q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
2252 if (!q)
2253 return 0;
2254
2255 md->queue = q;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002256 dm_init_md_queue(md);
2257 blk_queue_softirq_done(md->queue, dm_softirq_done);
2258 blk_queue_prep_rq(md->queue, dm_prep_fn);
2259 blk_queue_lld_busy(md->queue, dm_lld_busy);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002260
2261 elv_register_queue(md->queue);
2262
2263 return 1;
2264}
2265
2266/*
2267 * Setup the DM device's queue based on md's type
2268 */
2269int dm_setup_md_queue(struct mapped_device *md)
2270{
2271 if ((dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) &&
2272 !dm_init_request_based_queue(md)) {
2273 DMWARN("Cannot initialize queue for request-based mapped device");
2274 return -EINVAL;
2275 }
2276
2277 return 0;
2278}
2279
David Teigland637842c2006-01-06 00:20:00 -08002280static struct mapped_device *dm_find_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281{
2282 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 unsigned minor = MINOR(dev);
2284
2285 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2286 return NULL;
2287
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002288 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
2290 md = idr_find(&_minor_idr, minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002291 if (md && (md == MINOR_ALLOCED ||
Tejun Heof331c022008-09-03 09:01:48 +02002292 (MINOR(disk_devt(dm_disk(md))) != minor) ||
Kiyoshi Uedaabdc5682010-08-12 04:13:54 +01002293 dm_deleting_md(md) ||
Alasdair G Kergon17b2f662006-06-26 00:27:33 -07002294 test_bit(DMF_FREEING, &md->flags))) {
David Teigland637842c2006-01-06 00:20:00 -08002295 md = NULL;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002296 goto out;
2297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002299out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002300 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
David Teigland637842c2006-01-06 00:20:00 -08002302 return md;
2303}
2304
David Teiglandd229a952006-01-06 00:20:01 -08002305struct mapped_device *dm_get_md(dev_t dev)
2306{
2307 struct mapped_device *md = dm_find_md(dev);
2308
2309 if (md)
2310 dm_get(md);
2311
2312 return md;
2313}
Alasdair G Kergon3cf2e4b2011-10-31 20:19:06 +00002314EXPORT_SYMBOL_GPL(dm_get_md);
David Teiglandd229a952006-01-06 00:20:01 -08002315
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002316void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002317{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002318 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319}
2320
2321void dm_set_mdptr(struct mapped_device *md, void *ptr)
2322{
2323 md->interface_ptr = ptr;
2324}
2325
2326void dm_get(struct mapped_device *md)
2327{
2328 atomic_inc(&md->holders);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002329 BUG_ON(test_bit(DMF_FREEING, &md->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330}
2331
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002332const char *dm_device_name(struct mapped_device *md)
2333{
2334 return md->name;
2335}
2336EXPORT_SYMBOL_GPL(dm_device_name);
2337
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002338static void __dm_destroy(struct mapped_device *md, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339{
Mike Anderson1134e5a2006-03-27 01:17:54 -08002340 struct dm_table *map;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002341 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002343 might_sleep();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002344
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002345 spin_lock(&_minor_lock);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002346 map = dm_get_live_table(md, &srcu_idx);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002347 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2348 set_bit(DMF_FREEING, &md->flags);
2349 spin_unlock(&_minor_lock);
2350
2351 if (!dm_suspended_md(md)) {
2352 dm_table_presuspend_targets(map);
2353 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 }
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002355
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002356 /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
2357 dm_put_live_table(md, srcu_idx);
2358
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002359 /*
2360 * Rare, but there may be I/O requests still going to complete,
2361 * for example. Wait for all references to disappear.
2362 * No one should increment the reference count of the mapped_device,
2363 * after the mapped_device state becomes DMF_FREEING.
2364 */
2365 if (wait)
2366 while (atomic_read(&md->holders))
2367 msleep(1);
2368 else if (atomic_read(&md->holders))
2369 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2370 dm_device_name(md), atomic_read(&md->holders));
2371
2372 dm_sysfs_exit(md);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002373 dm_table_destroy(__unbind(md));
2374 free_dev(md);
2375}
2376
2377void dm_destroy(struct mapped_device *md)
2378{
2379 __dm_destroy(md, true);
2380}
2381
2382void dm_destroy_immediate(struct mapped_device *md)
2383{
2384 __dm_destroy(md, false);
2385}
2386
2387void dm_put(struct mapped_device *md)
2388{
2389 atomic_dec(&md->holders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390}
Edward Goggin79eb8852007-05-09 02:32:56 -07002391EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
Mikulas Patocka401600d2009-04-02 19:55:38 +01002393static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
Milan Broz46125c12008-02-08 02:10:30 +00002394{
2395 int r = 0;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002396 DECLARE_WAITQUEUE(wait, current);
2397
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002398 add_wait_queue(&md->wait, &wait);
Milan Broz46125c12008-02-08 02:10:30 +00002399
2400 while (1) {
Mikulas Patocka401600d2009-04-02 19:55:38 +01002401 set_current_state(interruptible);
Milan Broz46125c12008-02-08 02:10:30 +00002402
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002403 if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002404 break;
2405
Mikulas Patocka401600d2009-04-02 19:55:38 +01002406 if (interruptible == TASK_INTERRUPTIBLE &&
2407 signal_pending(current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002408 r = -EINTR;
2409 break;
2410 }
2411
2412 io_schedule();
2413 }
2414 set_current_state(TASK_RUNNING);
2415
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002416 remove_wait_queue(&md->wait, &wait);
2417
Milan Broz46125c12008-02-08 02:10:30 +00002418 return r;
2419}
2420
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421/*
2422 * Process the deferred bios
2423 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002424static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425{
Mikulas Patockaef208582009-04-02 19:55:38 +01002426 struct mapped_device *md = container_of(work, struct mapped_device,
2427 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002428 struct bio *c;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002429 int srcu_idx;
2430 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002432 map = dm_get_live_table(md, &srcu_idx);
Mikulas Patockaef208582009-04-02 19:55:38 +01002433
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002434 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002435 spin_lock_irq(&md->deferred_lock);
2436 c = bio_list_pop(&md->deferred);
2437 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002438
Tejun Heo6a8736d2010-09-08 18:07:00 +02002439 if (!c)
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002440 break;
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002441
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002442 if (dm_request_based(md))
2443 generic_make_request(c);
Tejun Heo6a8736d2010-09-08 18:07:00 +02002444 else
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002445 __split_and_process_bio(md, map, c);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002446 }
Milan Broz73d410c2008-02-08 02:10:25 +00002447
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002448 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449}
2450
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002451static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002452{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002453 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2454 smp_mb__after_clear_bit();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002455 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002456}
2457
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002459 * Swap in a new table, returning the old one for the caller to destroy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002461struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462{
Mike Christie87eb5b22013-03-01 22:45:48 +00002463 struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002464 struct queue_limits limits;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002465 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
Daniel Walkere61290a2008-02-08 02:10:08 +00002467 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
2469 /* device must be suspended */
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002470 if (!dm_suspended_md(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002471 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
Mike Snitzer3ae70652012-09-26 23:45:45 +01002473 /*
2474 * If the new table has no data devices, retain the existing limits.
2475 * This helps multipath with queue_if_no_path if all paths disappear,
2476 * then new I/O is queued based on these limits, and then some paths
2477 * reappear.
2478 */
2479 if (dm_table_has_no_data_devices(table)) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002480 live_map = dm_get_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01002481 if (live_map)
2482 limits = md->queue->limits;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002483 dm_put_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01002484 }
2485
Mike Christie87eb5b22013-03-01 22:45:48 +00002486 if (!live_map) {
2487 r = dm_calculate_queue_limits(table, &limits);
2488 if (r) {
2489 map = ERR_PTR(r);
2490 goto out;
2491 }
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002492 }
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002493
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002494 map = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002496out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002497 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002498 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499}
2500
2501/*
2502 * Functions to lock and unlock any filesystem running on the
2503 * device.
2504 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002505static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002507 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
2509 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002510
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002511 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002512 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002513 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002514 md->frozen_sb = NULL;
2515 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002516 }
2517
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002518 set_bit(DMF_FROZEN, &md->flags);
2519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 return 0;
2521}
2522
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002523static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002525 if (!test_bit(DMF_FROZEN, &md->flags))
2526 return;
2527
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002528 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002530 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531}
2532
2533/*
2534 * We need to be able to change a mapping table under a mounted
2535 * filesystem. For example we might want to move some data in
2536 * the background. Before the table can be swapped with
2537 * dm_bind_table, dm_suspend must be called to flush any in
2538 * flight bios and ensure that any further io gets deferred.
2539 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002540/*
2541 * Suspend mechanism in request-based dm.
2542 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002543 * 1. Flush all I/Os by lock_fs() if needed.
2544 * 2. Stop dispatching any I/O by stopping the request_queue.
2545 * 3. Wait for all in-flight I/Os to be completed or requeued.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002546 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002547 * To abort suspend, start the request_queue.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002548 */
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002549int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002551 struct dm_table *map = NULL;
Milan Broz46125c12008-02-08 02:10:30 +00002552 int r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002553 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002554 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
Daniel Walkere61290a2008-02-08 02:10:08 +00002556 mutex_lock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002557
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002558 if (dm_suspended_md(md)) {
Milan Broz73d410c2008-02-08 02:10:25 +00002559 r = -EINVAL;
Alasdair G Kergond2874832006-11-08 17:44:43 -08002560 goto out_unlock;
Milan Broz73d410c2008-02-08 02:10:25 +00002561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002563 map = md->map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002565 /*
2566 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2567 * This flag is cleared before dm_suspend returns.
2568 */
2569 if (noflush)
2570 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2571
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002572 /* This does not get reverted if there's an error later. */
2573 dm_table_presuspend_targets(map);
2574
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002575 /*
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002576 * Flush I/O to the device.
2577 * Any I/O submitted after lock_fs() may not be flushed.
2578 * noflush takes precedence over do_lockfs.
2579 * (lock_fs() flushes I/Os and waits for them to complete.)
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002580 */
2581 if (!noflush && do_lockfs) {
2582 r = lock_fs(md);
2583 if (r)
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002584 goto out_unlock;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
2587 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002588 * Here we must make sure that no processes are submitting requests
2589 * to target drivers i.e. no one may be executing
2590 * __split_and_process_bio. This is called from dm_request and
2591 * dm_wq_work.
2592 *
2593 * To get all processes out of __split_and_process_bio in dm_request,
2594 * we take the write lock. To prevent any process from reentering
Tejun Heo6a8736d2010-09-08 18:07:00 +02002595 * __split_and_process_bio from dm_request and quiesce the thread
2596 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
2597 * flush_workqueue(md->wq).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002599 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002600 synchronize_srcu(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002602 /*
Tejun Heo29e40132010-09-08 18:07:00 +02002603 * Stop md->queue before flushing md->wq in case request-based
2604 * dm defers requests to md->wq from md->queue.
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002605 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002606 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002607 stop_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002608
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002609 flush_workqueue(md->wq);
2610
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002612 * At this point no more requests are entering target request routines.
2613 * We call dm_wait_for_completion to wait for all existing requests
2614 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 */
Mikulas Patocka401600d2009-04-02 19:55:38 +01002616 r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
Milan Broz6d6f10d2008-02-08 02:10:22 +00002618 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01002619 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002620 synchronize_srcu(&md->io_barrier);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002621
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00002623 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002624 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00002625
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002626 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002627 start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002628
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002629 unlock_fs(md);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002630 goto out_unlock; /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002631 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002632
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002633 /*
2634 * If dm_wait_for_completion returned 0, the device is completely
2635 * quiescent now. There is no request-processing activity. All new
2636 * requests are being added to md->deferred list.
2637 */
2638
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 set_bit(DMF_SUSPENDED, &md->flags);
2640
Kiyoshi Ueda4d4471c2009-12-10 23:52:26 +00002641 dm_table_postsuspend_targets(map);
2642
Alasdair G Kergond2874832006-11-08 17:44:43 -08002643out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00002644 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002645 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646}
2647
2648int dm_resume(struct mapped_device *md)
2649{
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002650 int r = -EINVAL;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002651 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
Daniel Walkere61290a2008-02-08 02:10:08 +00002653 mutex_lock(&md->suspend_lock);
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002654 if (!dm_suspended_md(md))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002655 goto out;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002656
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002657 map = md->map;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002658 if (!map || !dm_table_get_size(map))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002659 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
Milan Broz8757b772006-10-03 01:15:36 -07002661 r = dm_table_resume_targets(map);
2662 if (r)
2663 goto out;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002664
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002665 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002666
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002667 /*
2668 * Flushing deferred I/Os must be done after targets are resumed
2669 * so that mapping of targets can work correctly.
2670 * Request-based dm is queueing the deferred I/Os in its request_queue.
2671 */
2672 if (dm_request_based(md))
2673 start_queue(md->queue);
2674
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002675 unlock_fs(md);
2676
2677 clear_bit(DMF_SUSPENDED, &md->flags);
2678
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002679 r = 0;
2680out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002681 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002682
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002683 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684}
2685
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002686/*
2687 * Internal suspend/resume works like userspace-driven suspend. It waits
2688 * until all bios finish and prevents issuing new bios to the target drivers.
2689 * It may be used only from the kernel.
2690 *
2691 * Internal suspend holds md->suspend_lock, which prevents interaction with
2692 * userspace-driven suspend.
2693 */
2694
2695void dm_internal_suspend(struct mapped_device *md)
2696{
2697 mutex_lock(&md->suspend_lock);
2698 if (dm_suspended_md(md))
2699 return;
2700
2701 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2702 synchronize_srcu(&md->io_barrier);
2703 flush_workqueue(md->wq);
2704 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2705}
2706
2707void dm_internal_resume(struct mapped_device *md)
2708{
2709 if (dm_suspended_md(md))
2710 goto done;
2711
2712 dm_queue_flush(md);
2713
2714done:
2715 mutex_unlock(&md->suspend_lock);
2716}
2717
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718/*-----------------------------------------------------------------
2719 * Event notification.
2720 *---------------------------------------------------------------*/
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002721int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
Milan Broz60935eb2009-06-22 10:12:30 +01002722 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002723{
Milan Broz60935eb2009-06-22 10:12:30 +01002724 char udev_cookie[DM_COOKIE_LENGTH];
2725 char *envp[] = { udev_cookie, NULL };
2726
2727 if (!cookie)
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002728 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
Milan Broz60935eb2009-06-22 10:12:30 +01002729 else {
2730 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2731 DM_COOKIE_ENV_VAR_NAME, cookie);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002732 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
2733 action, envp);
Milan Broz60935eb2009-06-22 10:12:30 +01002734 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002735}
2736
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002737uint32_t dm_next_uevent_seq(struct mapped_device *md)
2738{
2739 return atomic_add_return(1, &md->uevent_seq);
2740}
2741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742uint32_t dm_get_event_nr(struct mapped_device *md)
2743{
2744 return atomic_read(&md->event_nr);
2745}
2746
2747int dm_wait_event(struct mapped_device *md, int event_nr)
2748{
2749 return wait_event_interruptible(md->eventq,
2750 (event_nr != atomic_read(&md->event_nr)));
2751}
2752
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002753void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2754{
2755 unsigned long flags;
2756
2757 spin_lock_irqsave(&md->uevent_lock, flags);
2758 list_add(elist, &md->uevent_list);
2759 spin_unlock_irqrestore(&md->uevent_lock, flags);
2760}
2761
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762/*
2763 * The gendisk is only valid as long as you have a reference
2764 * count on 'md'.
2765 */
2766struct gendisk *dm_disk(struct mapped_device *md)
2767{
2768 return md->disk;
2769}
2770
Milan Broz784aae72009-01-06 03:05:12 +00002771struct kobject *dm_kobject(struct mapped_device *md)
2772{
Mikulas Patocka2995fa72014-01-13 19:37:54 -05002773 return &md->kobj_holder.kobj;
Milan Broz784aae72009-01-06 03:05:12 +00002774}
2775
Milan Broz784aae72009-01-06 03:05:12 +00002776struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2777{
2778 struct mapped_device *md;
2779
Mikulas Patocka2995fa72014-01-13 19:37:54 -05002780 md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
Milan Broz784aae72009-01-06 03:05:12 +00002781
Milan Broz4d89b7b2009-06-22 10:12:11 +01002782 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +00002783 dm_deleting_md(md))
Milan Broz4d89b7b2009-06-22 10:12:11 +01002784 return NULL;
2785
Milan Broz784aae72009-01-06 03:05:12 +00002786 dm_get(md);
2787 return md;
2788}
2789
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002790int dm_suspended_md(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791{
2792 return test_bit(DMF_SUSPENDED, &md->flags);
2793}
2794
Mikulas Patocka2c140a22013-11-01 18:27:41 -04002795int dm_test_deferred_remove_flag(struct mapped_device *md)
2796{
2797 return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
2798}
2799
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002800int dm_suspended(struct dm_target *ti)
2801{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002802 return dm_suspended_md(dm_table_get_md(ti->table));
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002803}
2804EXPORT_SYMBOL_GPL(dm_suspended);
2805
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002806int dm_noflush_suspending(struct dm_target *ti)
2807{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002808 return __noflush_suspending(dm_table_get_md(ti->table));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002809}
2810EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2811
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002812struct dm_md_mempools *dm_alloc_md_mempools(unsigned type, unsigned integrity, unsigned per_bio_data_size)
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002813{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002814 struct dm_md_mempools *pools = kzalloc(sizeof(*pools), GFP_KERNEL);
2815 struct kmem_cache *cachep;
2816 unsigned int pool_size;
2817 unsigned int front_pad;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002818
2819 if (!pools)
2820 return NULL;
2821
Jun'ichi Nomura23e50832013-03-01 22:45:48 +00002822 if (type == DM_TYPE_BIO_BASED) {
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002823 cachep = _io_cache;
Mike Snitzere8603132013-09-12 18:06:12 -04002824 pool_size = dm_get_reserved_bio_based_ios();
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002825 front_pad = roundup(per_bio_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
2826 } else if (type == DM_TYPE_REQUEST_BASED) {
2827 cachep = _rq_tio_cache;
Mike Snitzerf4790822013-09-12 18:06:12 -04002828 pool_size = dm_get_reserved_rq_based_ios();
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002829 front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
2830 /* per_bio_data_size is not used. See __bind_mempools(). */
2831 WARN_ON(per_bio_data_size != 0);
2832 } else
2833 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002834
Mike Snitzer6cfa5852013-09-12 18:06:11 -04002835 pools->io_pool = mempool_create_slab_pool(pool_size, cachep);
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002836 if (!pools->io_pool)
2837 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002838
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002839 pools->bs = bioset_create(pool_size, front_pad);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002840 if (!pools->bs)
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002841 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002842
Martin K. Petersena91a2782011-03-17 11:11:05 +01002843 if (integrity && bioset_integrity_create(pools->bs, pool_size))
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002844 goto out;
Martin K. Petersena91a2782011-03-17 11:11:05 +01002845
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002846 return pools;
2847
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002848out:
2849 dm_free_md_mempools(pools);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002850
2851 return NULL;
2852}
2853
2854void dm_free_md_mempools(struct dm_md_mempools *pools)
2855{
2856 if (!pools)
2857 return;
2858
2859 if (pools->io_pool)
2860 mempool_destroy(pools->io_pool);
2861
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002862 if (pools->bs)
2863 bioset_free(pools->bs);
2864
2865 kfree(pools);
2866}
2867
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002868static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 .open = dm_blk_open,
2870 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07002871 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002872 .getgeo = dm_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 .owner = THIS_MODULE
2874};
2875
2876EXPORT_SYMBOL(dm_get_mapinfo);
2877
2878/*
2879 * module hooks
2880 */
2881module_init(dm_init);
2882module_exit(dm_exit);
2883
2884module_param(major, uint, 0);
2885MODULE_PARM_DESC(major, "The major number of the device mapper");
Mike Snitzerf4790822013-09-12 18:06:12 -04002886
Mike Snitzere8603132013-09-12 18:06:12 -04002887module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
2888MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
2889
Mike Snitzerf4790822013-09-12 18:06:12 -04002890module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
2891MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
2892
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893MODULE_DESCRIPTION(DM_NAME " driver");
2894MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2895MODULE_LICENSE("GPL");