blob: 32e6622767ada54225fc3f17cdbc40b77ebb2e1a [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>
17#include <linux/buffer_head.h>
Arnd Bergmann6e9624b2010-08-07 18:25:34 +020018#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/mempool.h>
20#include <linux/slab.h>
21#include <linux/idr.h>
Darrick J. Wong3ac51e72006-03-27 01:17:54 -080022#include <linux/hdreg.h>
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +010023#include <linux/delay.h>
Li Zefan55782132009-06-09 13:43:05 +080024
25#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Alasdair G Kergon72d94862006-06-26 00:27:35 -070027#define DM_MSG_PREFIX "core"
28
Milan Broz60935eb2009-06-22 10:12:30 +010029/*
30 * Cookies are numeric values sent with CHANGE and REMOVE
31 * uevents while resuming, removing or renaming the device.
32 */
33#define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
34#define DM_COOKIE_LENGTH 24
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static const char *_name = DM_NAME;
37
38static unsigned int major = 0;
39static unsigned int _major = 0;
40
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -070041static DEFINE_SPINLOCK(_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000043 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * One of these is allocated per bio.
45 */
46struct dm_io {
47 struct mapped_device *md;
48 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 atomic_t io_count;
Richard Kennedy6ae2fa62008-07-21 12:00:28 +010050 struct bio *bio;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -080051 unsigned long start_time;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +010052 spinlock_t endio_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053};
54
55/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000056 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 * One of these is allocated per target within a bio. Hopefully
58 * this will be simplified out one day.
59 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +010060struct dm_target_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 struct dm_io *io;
62 struct dm_target *ti;
63 union map_info info;
64};
65
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000066/*
67 * For request-based dm.
68 * One of these is allocated per request.
69 */
70struct dm_rq_target_io {
71 struct mapped_device *md;
72 struct dm_target *ti;
73 struct request *orig, clone;
74 int error;
75 union map_info info;
76};
77
78/*
79 * For request-based dm.
80 * One of these is allocated per bio.
81 */
82struct dm_rq_clone_bio_info {
83 struct bio *orig;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010084 struct dm_rq_target_io *tio;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000085};
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087union map_info *dm_get_mapinfo(struct bio *bio)
88{
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070089 if (bio && bio->bi_private)
Alasdair G Kergon028867a2007-07-12 17:26:32 +010090 return &((struct dm_target_io *)bio->bi_private)->info;
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070091 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010094union map_info *dm_get_rq_mapinfo(struct request *rq)
95{
96 if (rq && rq->end_io_data)
97 return &((struct dm_rq_target_io *)rq->end_io_data)->info;
98 return NULL;
99}
100EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
101
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700102#define MINOR_ALLOCED ((void *)-1)
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/*
105 * Bits for the md->flags field.
106 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +0100107#define DMF_BLOCK_IO_FOR_SUSPEND 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -0800109#define DMF_FROZEN 2
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700110#define DMF_FREEING 3
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700111#define DMF_DELETING 4
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800112#define DMF_NOFLUSH_SUSPENDING 5
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +0100113#define DMF_QUEUE_IO_TO_THREAD 6
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Milan Broz304f3f62008-02-08 02:11:17 +0000115/*
116 * Work processed by per-device workqueue.
117 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118struct mapped_device {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700119 struct rw_semaphore io_lock;
Daniel Walkere61290a2008-02-08 02:10:08 +0000120 struct mutex suspend_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 rwlock_t map_lock;
122 atomic_t holders;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700123 atomic_t open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 unsigned long flags;
126
Jens Axboe165125e2007-07-24 09:28:11 +0200127 struct request_queue *queue;
Mike Snitzera5664da2010-08-12 04:14:01 +0100128 unsigned type;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +0100129 /* Protect queue and type against concurrent access. */
Mike Snitzera5664da2010-08-12 04:14:01 +0100130 struct mutex type_lock;
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 struct gendisk *disk;
Mike Anderson7e51f252006-03-27 01:17:52 -0800133 char name[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 void *interface_ptr;
136
137 /*
138 * A list of ios that arrived while we were suspended.
139 */
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200140 atomic_t pending[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 wait_queue_head_t wait;
Mikulas Patocka53d59142009-04-02 19:55:37 +0100142 struct work_struct work;
Kiyoshi Ueda74859362006-12-08 02:41:02 -0800143 struct bio_list deferred;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100144 spinlock_t deferred_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 /*
Tejun Heod87f4c12010-09-03 11:56:19 +0200147 * An error from the flush request currently being processed.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100148 */
Tejun Heod87f4c12010-09-03 11:56:19 +0200149 int flush_error;
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100150
151 /*
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000152 * Protect barrier_error from concurrent endio processing
153 * in request-based dm.
154 */
155 spinlock_t barrier_error_lock;
Tejun Heod87f4c12010-09-03 11:56:19 +0200156 int barrier_error;
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000157
158 /*
Milan Broz304f3f62008-02-08 02:11:17 +0000159 * Processing queue (flush/barriers)
160 */
161 struct workqueue_struct *wq;
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000162 struct work_struct barrier_work;
163
164 /* A pointer to the currently processing pre/post flush request */
165 struct request *flush_request;
Milan Broz304f3f62008-02-08 02:11:17 +0000166
167 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 * The current mapping.
169 */
170 struct dm_table *map;
171
172 /*
173 * io objects are allocated from here.
174 */
175 mempool_t *io_pool;
176 mempool_t *tio_pool;
177
Stefan Bader9faf4002006-10-03 01:15:41 -0700178 struct bio_set *bs;
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 /*
181 * Event handling.
182 */
183 atomic_t event_nr;
184 wait_queue_head_t eventq;
Mike Anderson7a8c3d32007-10-19 22:48:01 +0100185 atomic_t uevent_seq;
186 struct list_head uevent_list;
187 spinlock_t uevent_lock; /* Protect access to uevent_list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 /*
190 * freeze/thaw support require holding onto a super block
191 */
192 struct super_block *frozen_sb;
Mikulas Patockadb8fef42009-06-22 10:12:15 +0100193 struct block_device *bdev;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800194
195 /* forced geometry settings */
196 struct hd_geometry geometry;
Milan Broz784aae72009-01-06 03:05:12 +0000197
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100198 /* For saving the address of __make_request for request based dm */
199 make_request_fn *saved_make_request_fn;
200
Milan Broz784aae72009-01-06 03:05:12 +0000201 /* sysfs handle */
202 struct kobject kobj;
Mikulas Patocka52b1fd52009-06-22 10:12:21 +0100203
Tejun Heod87f4c12010-09-03 11:56:19 +0200204 /* zero-length flush that will be cloned and submitted to targets */
205 struct bio flush_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206};
207
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100208/*
209 * For mempools pre-allocation at the table loading time.
210 */
211struct dm_md_mempools {
212 mempool_t *io_pool;
213 mempool_t *tio_pool;
214 struct bio_set *bs;
215};
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217#define MIN_IOS 256
Christoph Lametere18b8902006-12-06 20:33:20 -0800218static struct kmem_cache *_io_cache;
219static struct kmem_cache *_tio_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000220static struct kmem_cache *_rq_tio_cache;
221static struct kmem_cache *_rq_bio_info_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223static int __init local_init(void)
224{
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100225 int r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100228 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (!_io_cache)
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100230 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 /* allocate a slab for the target ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100233 _tio_cache = KMEM_CACHE(dm_target_io, 0);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100234 if (!_tio_cache)
235 goto out_free_io_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000237 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
238 if (!_rq_tio_cache)
239 goto out_free_tio_cache;
240
241 _rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0);
242 if (!_rq_bio_info_cache)
243 goto out_free_rq_tio_cache;
244
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100245 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100246 if (r)
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000247 goto out_free_rq_bio_info_cache;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 _major = major;
250 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100251 if (r < 0)
252 goto out_uevent_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 if (!_major)
255 _major = r;
256
257 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100258
259out_uevent_exit:
260 dm_uevent_exit();
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000261out_free_rq_bio_info_cache:
262 kmem_cache_destroy(_rq_bio_info_cache);
263out_free_rq_tio_cache:
264 kmem_cache_destroy(_rq_tio_cache);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100265out_free_tio_cache:
266 kmem_cache_destroy(_tio_cache);
267out_free_io_cache:
268 kmem_cache_destroy(_io_cache);
269
270 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
273static void local_exit(void)
274{
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000275 kmem_cache_destroy(_rq_bio_info_cache);
276 kmem_cache_destroy(_rq_tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 kmem_cache_destroy(_tio_cache);
278 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700279 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100280 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 _major = 0;
283
284 DMINFO("cleaned up");
285}
286
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000287static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 local_init,
289 dm_target_init,
290 dm_linear_init,
291 dm_stripe_init,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000292 dm_io_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100293 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 dm_interface_init,
295};
296
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000297static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 local_exit,
299 dm_target_exit,
300 dm_linear_exit,
301 dm_stripe_exit,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000302 dm_io_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100303 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 dm_interface_exit,
305};
306
307static int __init dm_init(void)
308{
309 const int count = ARRAY_SIZE(_inits);
310
311 int r, i;
312
313 for (i = 0; i < count; i++) {
314 r = _inits[i]();
315 if (r)
316 goto bad;
317 }
318
319 return 0;
320
321 bad:
322 while (i--)
323 _exits[i]();
324
325 return r;
326}
327
328static void __exit dm_exit(void)
329{
330 int i = ARRAY_SIZE(_exits);
331
332 while (i--)
333 _exits[i]();
334}
335
336/*
337 * Block device functions
338 */
Mike Anderson432a2122009-12-10 23:52:20 +0000339int dm_deleting_md(struct mapped_device *md)
340{
341 return test_bit(DMF_DELETING, &md->flags);
342}
343
Al Virofe5f9f22008-03-02 10:29:31 -0500344static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 struct mapped_device *md;
347
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200348 lock_kernel();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700349 spin_lock(&_minor_lock);
350
Al Virofe5f9f22008-03-02 10:29:31 -0500351 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700352 if (!md)
353 goto out;
354
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700355 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +0000356 dm_deleting_md(md)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700357 md = NULL;
358 goto out;
359 }
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700362 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700363
364out:
365 spin_unlock(&_minor_lock);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200366 unlock_kernel();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700367
368 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
Al Virofe5f9f22008-03-02 10:29:31 -0500371static int dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Al Virofe5f9f22008-03-02 10:29:31 -0500373 struct mapped_device *md = disk->private_data;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200374
375 lock_kernel();
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700376 atomic_dec(&md->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 dm_put(md);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200378 unlock_kernel();
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return 0;
381}
382
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700383int dm_open_count(struct mapped_device *md)
384{
385 return atomic_read(&md->open_count);
386}
387
388/*
389 * Guarantees nothing is using the device before it's deleted.
390 */
391int dm_lock_for_deletion(struct mapped_device *md)
392{
393 int r = 0;
394
395 spin_lock(&_minor_lock);
396
397 if (dm_open_count(md))
398 r = -EBUSY;
399 else
400 set_bit(DMF_DELETING, &md->flags);
401
402 spin_unlock(&_minor_lock);
403
404 return r;
405}
406
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800407static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
408{
409 struct mapped_device *md = bdev->bd_disk->private_data;
410
411 return dm_get_geometry(md, geo);
412}
413
Al Virofe5f9f22008-03-02 10:29:31 -0500414static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
Milan Brozaa129a22006-10-03 01:15:15 -0700415 unsigned int cmd, unsigned long arg)
416{
Al Virofe5f9f22008-03-02 10:29:31 -0500417 struct mapped_device *md = bdev->bd_disk->private_data;
Alasdair G Kergon7c666412009-12-10 23:52:19 +0000418 struct dm_table *map = dm_get_live_table(md);
Milan Brozaa129a22006-10-03 01:15:15 -0700419 struct dm_target *tgt;
420 int r = -ENOTTY;
421
Milan Brozaa129a22006-10-03 01:15:15 -0700422 if (!map || !dm_table_get_size(map))
423 goto out;
424
425 /* We only support devices that have a single target */
426 if (dm_table_get_num_targets(map) != 1)
427 goto out;
428
429 tgt = dm_table_get_target(map, 0);
430
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000431 if (dm_suspended_md(md)) {
Milan Brozaa129a22006-10-03 01:15:15 -0700432 r = -EAGAIN;
433 goto out;
434 }
435
436 if (tgt->type->ioctl)
Al Viro647b3d02007-08-28 22:15:59 -0400437 r = tgt->type->ioctl(tgt, cmd, arg);
Milan Brozaa129a22006-10-03 01:15:15 -0700438
439out:
440 dm_table_put(map);
441
Milan Brozaa129a22006-10-03 01:15:15 -0700442 return r;
443}
444
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100445static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 return mempool_alloc(md->io_pool, GFP_NOIO);
448}
449
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100450static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 mempool_free(io, md->io_pool);
453}
454
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100455static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 mempool_free(tio, md->tio_pool);
458}
459
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000460static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
461 gfp_t gfp_mask)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100462{
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000463 return mempool_alloc(md->tio_pool, gfp_mask);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100464}
465
466static void free_rq_tio(struct dm_rq_target_io *tio)
467{
468 mempool_free(tio, tio->md->tio_pool);
469}
470
471static struct dm_rq_clone_bio_info *alloc_bio_info(struct mapped_device *md)
472{
473 return mempool_alloc(md->io_pool, GFP_ATOMIC);
474}
475
476static void free_bio_info(struct dm_rq_clone_bio_info *info)
477{
478 mempool_free(info, info->tio->md->io_pool);
479}
480
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +0000481static int md_in_flight(struct mapped_device *md)
482{
483 return atomic_read(&md->pending[READ]) +
484 atomic_read(&md->pending[WRITE]);
485}
486
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800487static void start_io_acct(struct dm_io *io)
488{
489 struct mapped_device *md = io->md;
Tejun Heoc9959052008-08-25 19:47:21 +0900490 int cpu;
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200491 int rw = bio_data_dir(io->bio);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800492
493 io->start_time = jiffies;
494
Tejun Heo074a7ac2008-08-25 19:56:14 +0900495 cpu = part_stat_lock();
496 part_round_stats(cpu, &dm_disk(md)->part0);
497 part_stat_unlock();
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200498 dm_disk(md)->part0.in_flight[rw] = atomic_inc_return(&md->pending[rw]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800499}
500
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000501static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800502{
503 struct mapped_device *md = io->md;
504 struct bio *bio = io->bio;
505 unsigned long duration = jiffies - io->start_time;
Tejun Heoc9959052008-08-25 19:47:21 +0900506 int pending, cpu;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800507 int rw = bio_data_dir(bio);
508
Tejun Heo074a7ac2008-08-25 19:56:14 +0900509 cpu = part_stat_lock();
510 part_round_stats(cpu, &dm_disk(md)->part0);
511 part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
512 part_stat_unlock();
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800513
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100514 /*
515 * After this is decremented the bio must not be touched if it is
Tejun Heod87f4c12010-09-03 11:56:19 +0200516 * a flush.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100517 */
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200518 dm_disk(md)->part0.in_flight[rw] = pending =
519 atomic_dec_return(&md->pending[rw]);
520 pending += atomic_read(&md->pending[rw^0x1]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800521
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000522 /* nudge anyone waiting on suspend queue */
523 if (!pending)
524 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800525}
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527/*
528 * Add the bio to the list of deferred io.
529 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100530static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700532 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Mikulas Patocka022c2612009-04-02 19:55:39 +0100534 spin_lock_irq(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 bio_list_add(&md->deferred, bio);
Mikulas Patocka022c2612009-04-02 19:55:39 +0100536 spin_unlock_irq(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Mikulas Patocka92c63902009-04-09 00:27:15 +0100538 if (!test_and_set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags))
539 queue_work(md->wq, &md->work);
540
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700541 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
544/*
545 * Everyone (including functions in this file), should use this
546 * function to access the md->map field, and make sure they call
547 * dm_table_put() when finished.
548 */
Alasdair G Kergon7c666412009-12-10 23:52:19 +0000549struct dm_table *dm_get_live_table(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 struct dm_table *t;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100552 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100554 read_lock_irqsave(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 t = md->map;
556 if (t)
557 dm_table_get(t);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +0100558 read_unlock_irqrestore(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 return t;
561}
562
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800563/*
564 * Get the geometry associated with a dm device
565 */
566int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
567{
568 *geo = md->geometry;
569
570 return 0;
571}
572
573/*
574 * Set the geometry of a device.
575 */
576int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
577{
578 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
579
580 if (geo->start > sz) {
581 DMWARN("Start sector is beyond the geometry limits.");
582 return -EINVAL;
583 }
584
585 md->geometry = *geo;
586
587 return 0;
588}
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590/*-----------------------------------------------------------------
591 * CRUD START:
592 * A more elegant soln is in the works that uses the queue
593 * merge fn, unfortunately there are a couple of changes to
594 * the block layer that I want to make for this. So in the
595 * interests of getting something for people to use I give
596 * you this clearly demarcated crap.
597 *---------------------------------------------------------------*/
598
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800599static int __noflush_suspending(struct mapped_device *md)
600{
601 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
602}
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604/*
605 * Decrements the number of outstanding ios that a bio has been
606 * cloned into, completing the original io if necc.
607 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800608static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800610 unsigned long flags;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000611 int io_error;
612 struct bio *bio;
613 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800614
615 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100616 if (unlikely(error)) {
617 spin_lock_irqsave(&io->endio_lock, flags);
618 if (!(io->error > 0 && __noflush_suspending(md)))
619 io->error = error;
620 spin_unlock_irqrestore(&io->endio_lock, flags);
621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800624 if (io->error == DM_ENDIO_REQUEUE) {
625 /*
626 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800627 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100628 spin_lock_irqsave(&md->deferred_lock, flags);
Mikulas Patocka2761e952009-06-22 10:12:18 +0100629 if (__noflush_suspending(md)) {
Tejun Heod87f4c12010-09-03 11:56:19 +0200630 if (!(io->bio->bi_rw & REQ_FLUSH))
Mikulas Patocka2761e952009-06-22 10:12:18 +0100631 bio_list_add_head(&md->deferred,
632 io->bio);
633 } else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800634 /* noflush suspend was interrupted. */
635 io->error = -EIO;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100636 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800637 }
638
Milan Brozb35f8ca2009-03-16 17:44:36 +0000639 io_error = io->error;
640 bio = io->bio;
Jens Axboe2056a782006-03-23 20:00:26 +0100641
Tejun Heod87f4c12010-09-03 11:56:19 +0200642 if (bio->bi_rw & REQ_FLUSH) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100643 /*
Tejun Heod87f4c12010-09-03 11:56:19 +0200644 * There can be just one flush request so we use
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100645 * a per-device variable for error reporting.
646 * Note that you can't touch the bio after end_io_acct
647 */
Tejun Heod87f4c12010-09-03 11:56:19 +0200648 if (!md->flush_error)
649 md->flush_error = io_error;
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100650 end_io_acct(io);
Mikulas Patockaa97f9252010-03-06 02:32:29 +0000651 free_io(md, io);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100652 } else {
653 end_io_acct(io);
Mikulas Patockaa97f9252010-03-06 02:32:29 +0000654 free_io(md, io);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000655
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100656 if (io_error != DM_ENDIO_REQUEUE) {
657 trace_block_bio_complete(md->queue, bio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000658
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100659 bio_endio(bio, io_error);
660 }
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663}
664
NeilBrown6712ecf2007-09-27 12:47:43 +0200665static void clone_endio(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
667 int r = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100668 struct dm_target_io *tio = bio->bi_private;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000669 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -0700670 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 dm_endio_fn endio = tio->ti->type->end_io;
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
674 error = -EIO;
675
676 if (endio) {
677 r = endio(tio->ti, bio, error, &tio->info);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800678 if (r < 0 || r == DM_ENDIO_REQUEUE)
679 /*
680 * error and requeue request are handled
681 * in dec_pending().
682 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800684 else if (r == DM_ENDIO_INCOMPLETE)
685 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +0200686 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800687 else if (r) {
688 DMWARN("unimplemented target endio return value: %d", r);
689 BUG();
690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692
Stefan Bader9faf4002006-10-03 01:15:41 -0700693 /*
694 * Store md for cleanup instead of tio which is about to get freed.
695 */
696 bio->bi_private = md->bs;
697
Stefan Bader9faf4002006-10-03 01:15:41 -0700698 free_tio(md, tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000699 bio_put(bio);
700 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100703/*
704 * Partial completion handling for request-based dm
705 */
706static void end_clone_bio(struct bio *clone, int error)
707{
708 struct dm_rq_clone_bio_info *info = clone->bi_private;
709 struct dm_rq_target_io *tio = info->tio;
710 struct bio *bio = info->orig;
711 unsigned int nr_bytes = info->orig->bi_size;
712
713 bio_put(clone);
714
715 if (tio->error)
716 /*
717 * An error has already been detected on the request.
718 * Once error occurred, just let clone->end_io() handle
719 * the remainder.
720 */
721 return;
722 else if (error) {
723 /*
724 * Don't notice the error to the upper layer yet.
725 * The error handling decision is made by the target driver,
726 * when the request is completed.
727 */
728 tio->error = error;
729 return;
730 }
731
732 /*
733 * I/O for the bio successfully completed.
734 * Notice the data completion to the upper layer.
735 */
736
737 /*
738 * bios are processed from the head of the list.
739 * So the completing bio should always be rq->bio.
740 * If it's not, something wrong is happening.
741 */
742 if (tio->orig->bio != bio)
743 DMERR("bio completion is going in the middle of the request");
744
745 /*
746 * Update the original request.
747 * Do not use blk_end_request() here, because it may complete
748 * the original request before the clone, and break the ordering.
749 */
750 blk_update_request(tio->orig, 0, nr_bytes);
751}
752
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000753static void store_barrier_error(struct mapped_device *md, int error)
754{
755 unsigned long flags;
756
757 spin_lock_irqsave(&md->barrier_error_lock, flags);
758 /*
759 * Basically, the first error is taken, but:
760 * -EOPNOTSUPP supersedes any I/O error.
761 * Requeue request supersedes any I/O error but -EOPNOTSUPP.
762 */
763 if (!md->barrier_error || error == -EOPNOTSUPP ||
764 (md->barrier_error != -EOPNOTSUPP &&
765 error == DM_ENDIO_REQUEUE))
766 md->barrier_error = error;
767 spin_unlock_irqrestore(&md->barrier_error_lock, flags);
768}
769
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100770/*
771 * Don't touch any member of the md after calling this function because
772 * the md may be freed in dm_put() at the end of this function.
773 * Or do dm_get() before calling this function and dm_put() later.
774 */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000775static void rq_completed(struct mapped_device *md, int rw, int run_queue)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100776{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000777 atomic_dec(&md->pending[rw]);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100778
779 /* nudge anyone waiting on suspend queue */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000780 if (!md_in_flight(md))
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100781 wake_up(&md->wait);
782
783 if (run_queue)
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000784 blk_run_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100785
786 /*
787 * dm_put() must be at the end of this function. See the comment above
788 */
789 dm_put(md);
790}
791
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100792static void free_rq_clone(struct request *clone)
793{
794 struct dm_rq_target_io *tio = clone->end_io_data;
795
796 blk_rq_unprep_clone(clone);
797 free_rq_tio(tio);
798}
799
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000800/*
801 * Complete the clone and the original request.
802 * Must be called without queue lock.
803 */
804static void dm_end_request(struct request *clone, int error)
805{
806 int rw = rq_data_dir(clone);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000807 int run_queue = 1;
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200808 bool is_barrier = clone->cmd_flags & REQ_HARDBARRIER;
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000809 struct dm_rq_target_io *tio = clone->end_io_data;
810 struct mapped_device *md = tio->md;
811 struct request *rq = tio->orig;
812
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200813 if (rq->cmd_type == REQ_TYPE_BLOCK_PC && !is_barrier) {
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000814 rq->errors = clone->errors;
815 rq->resid_len = clone->resid_len;
816
817 if (rq->sense)
818 /*
819 * We are using the sense buffer of the original
820 * request.
821 * So setting the length of the sense data is enough.
822 */
823 rq->sense_len = clone->sense_len;
824 }
825
826 free_rq_clone(clone);
827
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000828 if (unlikely(is_barrier)) {
829 if (unlikely(error))
830 store_barrier_error(md, error);
831 run_queue = 0;
832 } else
833 blk_end_request_all(rq, error);
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000834
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000835 rq_completed(md, rw, run_queue);
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000836}
837
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100838static void dm_unprep_request(struct request *rq)
839{
840 struct request *clone = rq->special;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100841
842 rq->special = NULL;
843 rq->cmd_flags &= ~REQ_DONTPREP;
844
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100845 free_rq_clone(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100846}
847
848/*
849 * Requeue the original request of a clone.
850 */
851void dm_requeue_unmapped_request(struct request *clone)
852{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000853 int rw = rq_data_dir(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100854 struct dm_rq_target_io *tio = clone->end_io_data;
855 struct mapped_device *md = tio->md;
856 struct request *rq = tio->orig;
857 struct request_queue *q = rq->q;
858 unsigned long flags;
859
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200860 if (unlikely(clone->cmd_flags & REQ_HARDBARRIER)) {
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000861 /*
862 * Barrier clones share an original request.
863 * Leave it to dm_end_request(), which handles this special
864 * case.
865 */
866 dm_end_request(clone, DM_ENDIO_REQUEUE);
867 return;
868 }
869
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100870 dm_unprep_request(rq);
871
872 spin_lock_irqsave(q->queue_lock, flags);
873 if (elv_queue_empty(q))
874 blk_plug_device(q);
875 blk_requeue_request(q, rq);
876 spin_unlock_irqrestore(q->queue_lock, flags);
877
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000878 rq_completed(md, rw, 0);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100879}
880EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
881
882static void __stop_queue(struct request_queue *q)
883{
884 blk_stop_queue(q);
885}
886
887static void stop_queue(struct request_queue *q)
888{
889 unsigned long flags;
890
891 spin_lock_irqsave(q->queue_lock, flags);
892 __stop_queue(q);
893 spin_unlock_irqrestore(q->queue_lock, flags);
894}
895
896static void __start_queue(struct request_queue *q)
897{
898 if (blk_queue_stopped(q))
899 blk_start_queue(q);
900}
901
902static void start_queue(struct request_queue *q)
903{
904 unsigned long flags;
905
906 spin_lock_irqsave(q->queue_lock, flags);
907 __start_queue(q);
908 spin_unlock_irqrestore(q->queue_lock, flags);
909}
910
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000911static void dm_done(struct request *clone, int error, bool mapped)
912{
913 int r = error;
914 struct dm_rq_target_io *tio = clone->end_io_data;
915 dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io;
916
917 if (mapped && rq_end_io)
918 r = rq_end_io(tio->ti, clone, error, &tio->info);
919
920 if (r <= 0)
921 /* The target wants to complete the I/O */
922 dm_end_request(clone, r);
923 else if (r == DM_ENDIO_INCOMPLETE)
924 /* The target will handle the I/O */
925 return;
926 else if (r == DM_ENDIO_REQUEUE)
927 /* The target wants to requeue the I/O */
928 dm_requeue_unmapped_request(clone);
929 else {
930 DMWARN("unimplemented target endio return value: %d", r);
931 BUG();
932 }
933}
934
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100935/*
936 * Request completion handler for request-based dm
937 */
938static void dm_softirq_done(struct request *rq)
939{
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000940 bool mapped = true;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100941 struct request *clone = rq->completion_data;
942 struct dm_rq_target_io *tio = clone->end_io_data;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100943
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000944 if (rq->cmd_flags & REQ_FAILED)
945 mapped = false;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100946
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000947 dm_done(clone, tio->error, mapped);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100948}
949
950/*
951 * Complete the clone and the original request with the error status
952 * through softirq context.
953 */
954static void dm_complete_request(struct request *clone, int error)
955{
956 struct dm_rq_target_io *tio = clone->end_io_data;
957 struct request *rq = tio->orig;
958
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200959 if (unlikely(clone->cmd_flags & REQ_HARDBARRIER)) {
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000960 /*
961 * Barrier clones share an original request. So can't use
962 * softirq_done with the original.
963 * Pass the clone to dm_done() directly in this special case.
964 * It is safe (even if clone->q->queue_lock is held here)
965 * because there is no I/O dispatching during the completion
966 * of barrier clone.
967 */
968 dm_done(clone, error, true);
969 return;
970 }
971
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100972 tio->error = error;
973 rq->completion_data = clone;
974 blk_complete_request(rq);
975}
976
977/*
978 * Complete the not-mapped clone and the original request with the error status
979 * through softirq context.
980 * Target's rq_end_io() function isn't called.
981 * This may be used when the target's map_rq() function fails.
982 */
983void dm_kill_unmapped_request(struct request *clone, int error)
984{
985 struct dm_rq_target_io *tio = clone->end_io_data;
986 struct request *rq = tio->orig;
987
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200988 if (unlikely(clone->cmd_flags & REQ_HARDBARRIER)) {
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +0000989 /*
990 * Barrier clones share an original request.
991 * Leave it to dm_end_request(), which handles this special
992 * case.
993 */
994 BUG_ON(error > 0);
995 dm_end_request(clone, error);
996 return;
997 }
998
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100999 rq->cmd_flags |= REQ_FAILED;
1000 dm_complete_request(clone, error);
1001}
1002EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
1003
1004/*
1005 * Called with the queue lock held
1006 */
1007static void end_clone_request(struct request *clone, int error)
1008{
1009 /*
1010 * For just cleaning up the information of the queue in which
1011 * the clone was dispatched.
1012 * The clone is *NOT* freed actually here because it is alloced from
1013 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
1014 */
1015 __blk_put_request(clone->q, clone);
1016
1017 /*
1018 * Actual request completion is done in a softirq context which doesn't
1019 * hold the queue lock. Otherwise, deadlock could occur because:
1020 * - another request may be submitted by the upper level driver
1021 * of the stacking during the completion
1022 * - the submission which requires queue lock may be done
1023 * against this queue
1024 */
1025 dm_complete_request(clone, error);
1026}
1027
Mike Snitzer56a67df2010-08-12 04:14:10 +01001028/*
1029 * Return maximum size of I/O possible at the supplied sector up to the current
1030 * target boundary.
1031 */
1032static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Mike Snitzer56a67df2010-08-12 04:14:10 +01001034 sector_t target_offset = dm_target_offset(ti, sector);
1035
1036 return ti->len - target_offset;
1037}
1038
1039static sector_t max_io_len(sector_t sector, struct dm_target *ti)
1040{
1041 sector_t len = max_io_len_target_boundary(sector, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 /*
1044 * Does the target need to split even further ?
1045 */
1046 if (ti->split_io) {
1047 sector_t boundary;
Mike Snitzer56a67df2010-08-12 04:14:10 +01001048 sector_t offset = dm_target_offset(ti, sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
1050 - offset;
1051 if (len > boundary)
1052 len = boundary;
1053 }
1054
1055 return len;
1056}
1057
1058static void __map_bio(struct dm_target *ti, struct bio *clone,
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001059 struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
1061 int r;
Jens Axboe2056a782006-03-23 20:00:26 +01001062 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -07001063 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 clone->bi_end_io = clone_endio;
1066 clone->bi_private = tio;
1067
1068 /*
1069 * Map the clone. If r == 0 we don't need to do
1070 * anything, the target has assumed ownership of
1071 * this io.
1072 */
1073 atomic_inc(&tio->io->io_count);
Jens Axboe2056a782006-03-23 20:00:26 +01001074 sector = clone->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 r = ti->type->map(ti, clone, &tio->info);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001076 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +01001078
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001079 trace_block_remap(bdev_get_queue(clone->bi_bdev), clone,
Alan D. Brunelle22a7c312009-05-04 16:35:08 -04001080 tio->io->bio->bi_bdev->bd_dev, sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001083 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
1084 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -07001085 md = tio->io->md;
1086 dec_pending(tio->io, r);
1087 /*
1088 * Store bio_set for cleanup.
1089 */
1090 clone->bi_private = md->bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 bio_put(clone);
Stefan Bader9faf4002006-10-03 01:15:41 -07001092 free_tio(md, tio);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001093 } else if (r) {
1094 DMWARN("unimplemented target map return value: %d", r);
1095 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
1097}
1098
1099struct clone_info {
1100 struct mapped_device *md;
1101 struct dm_table *map;
1102 struct bio *bio;
1103 struct dm_io *io;
1104 sector_t sector;
1105 sector_t sector_count;
1106 unsigned short idx;
1107};
1108
Peter Osterlund36763472005-09-06 15:16:42 -07001109static void dm_bio_destructor(struct bio *bio)
1110{
Stefan Bader9faf4002006-10-03 01:15:41 -07001111 struct bio_set *bs = bio->bi_private;
1112
1113 bio_free(bio, bs);
Peter Osterlund36763472005-09-06 15:16:42 -07001114}
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116/*
Tejun Heod87f4c12010-09-03 11:56:19 +02001117 * Creates a little bio that just does part of a bvec.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 */
1119static struct bio *split_bvec(struct bio *bio, sector_t sector,
1120 unsigned short idx, unsigned int offset,
Stefan Bader9faf4002006-10-03 01:15:41 -07001121 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
1123 struct bio *clone;
1124 struct bio_vec *bv = bio->bi_io_vec + idx;
1125
Stefan Bader9faf4002006-10-03 01:15:41 -07001126 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
Peter Osterlund36763472005-09-06 15:16:42 -07001127 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 *clone->bi_io_vec = *bv;
1129
1130 clone->bi_sector = sector;
1131 clone->bi_bdev = bio->bi_bdev;
Tejun Heod87f4c12010-09-03 11:56:19 +02001132 clone->bi_rw = bio->bi_rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 clone->bi_vcnt = 1;
1134 clone->bi_size = to_bytes(len);
1135 clone->bi_io_vec->bv_offset = offset;
1136 clone->bi_io_vec->bv_len = clone->bi_size;
Martin K. Petersenf3e1d262008-10-21 17:45:04 +01001137 clone->bi_flags |= 1 << BIO_CLONED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Martin K. Petersen9c470082009-04-09 00:27:12 +01001139 if (bio_integrity(bio)) {
Martin K. Petersen7878cba2009-06-26 15:37:49 +02001140 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001141 bio_integrity_trim(clone,
1142 bio_sector_offset(bio, idx, offset), len);
1143 }
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 return clone;
1146}
1147
1148/*
1149 * Creates a bio that consists of range of complete bvecs.
1150 */
1151static struct bio *clone_bio(struct bio *bio, sector_t sector,
1152 unsigned short idx, unsigned short bv_count,
Stefan Bader9faf4002006-10-03 01:15:41 -07001153 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
1155 struct bio *clone;
1156
Stefan Bader9faf4002006-10-03 01:15:41 -07001157 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
1158 __bio_clone(clone, bio);
1159 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 clone->bi_sector = sector;
1161 clone->bi_idx = idx;
1162 clone->bi_vcnt = idx + bv_count;
1163 clone->bi_size = to_bytes(len);
1164 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
1165
Martin K. Petersen9c470082009-04-09 00:27:12 +01001166 if (bio_integrity(bio)) {
Martin K. Petersen7878cba2009-06-26 15:37:49 +02001167 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001168
1169 if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
1170 bio_integrity_trim(clone,
1171 bio_sector_offset(bio, idx, 0), len);
1172 }
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return clone;
1175}
1176
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001177static struct dm_target_io *alloc_tio(struct clone_info *ci,
1178 struct dm_target *ti)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001179{
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001180 struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001181
1182 tio->io = ci->io;
1183 tio->ti = ti;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001184 memset(&tio->info, 0, sizeof(tio->info));
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001185
1186 return tio;
1187}
1188
Mike Snitzer06a426c2010-08-12 04:14:09 +01001189static void __issue_target_request(struct clone_info *ci, struct dm_target *ti,
Mike Snitzera79245b2010-08-12 04:14:24 +01001190 unsigned request_nr, sector_t len)
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001191{
1192 struct dm_target_io *tio = alloc_tio(ci, ti);
1193 struct bio *clone;
1194
Mike Snitzer57cba5d2010-08-12 04:14:04 +01001195 tio->info.target_request_nr = request_nr;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001196
Mike Snitzer06a426c2010-08-12 04:14:09 +01001197 /*
1198 * Discard requests require the bio's inline iovecs be initialized.
1199 * ci->bio->bi_max_vecs is BIO_INLINE_VECS anyway, for both flush
1200 * and discard, so no need for concern about wasted bvec allocations.
1201 */
1202 clone = bio_alloc_bioset(GFP_NOIO, ci->bio->bi_max_vecs, ci->md->bs);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001203 __bio_clone(clone, ci->bio);
1204 clone->bi_destructor = dm_bio_destructor;
Mike Snitzera79245b2010-08-12 04:14:24 +01001205 if (len) {
1206 clone->bi_sector = ci->sector;
1207 clone->bi_size = to_bytes(len);
1208 }
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001209
1210 __map_bio(ti, clone, tio);
1211}
1212
Mike Snitzer06a426c2010-08-12 04:14:09 +01001213static void __issue_target_requests(struct clone_info *ci, struct dm_target *ti,
Mike Snitzera79245b2010-08-12 04:14:24 +01001214 unsigned num_requests, sector_t len)
Mike Snitzer06a426c2010-08-12 04:14:09 +01001215{
1216 unsigned request_nr;
1217
1218 for (request_nr = 0; request_nr < num_requests; request_nr++)
Mike Snitzera79245b2010-08-12 04:14:24 +01001219 __issue_target_request(ci, ti, request_nr, len);
Mike Snitzer06a426c2010-08-12 04:14:09 +01001220}
1221
Tejun Heod87f4c12010-09-03 11:56:19 +02001222static int __clone_and_map_flush(struct clone_info *ci)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001223{
Mike Snitzer06a426c2010-08-12 04:14:09 +01001224 unsigned target_nr = 0;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001225 struct dm_target *ti;
1226
1227 while ((ti = dm_table_get_target(ci->map, target_nr++)))
Mike Snitzera79245b2010-08-12 04:14:24 +01001228 __issue_target_requests(ci, ti, ti->num_flush_requests, 0);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001229
1230 ci->sector_count = 0;
1231
1232 return 0;
1233}
1234
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001235/*
1236 * Perform all io with a single clone.
1237 */
1238static void __clone_and_map_simple(struct clone_info *ci, struct dm_target *ti)
1239{
1240 struct bio *clone, *bio = ci->bio;
1241 struct dm_target_io *tio;
1242
1243 tio = alloc_tio(ci, ti);
1244 clone = clone_bio(bio, ci->sector, ci->idx,
1245 bio->bi_vcnt - ci->idx, ci->sector_count,
1246 ci->md->bs);
1247 __map_bio(ti, clone, tio);
1248 ci->sector_count = 0;
1249}
1250
1251static int __clone_and_map_discard(struct clone_info *ci)
1252{
1253 struct dm_target *ti;
Mike Snitzera79245b2010-08-12 04:14:24 +01001254 sector_t len;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001255
Mike Snitzera79245b2010-08-12 04:14:24 +01001256 do {
1257 ti = dm_table_find_target(ci->map, ci->sector);
1258 if (!dm_target_is_valid(ti))
1259 return -EIO;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001260
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001261 /*
Mike Snitzera79245b2010-08-12 04:14:24 +01001262 * Even though the device advertised discard support,
1263 * reconfiguration might have changed that since the
1264 * check was performed.
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001265 */
Mike Snitzera79245b2010-08-12 04:14:24 +01001266 if (!ti->num_discard_requests)
1267 return -EOPNOTSUPP;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001268
Mike Snitzera79245b2010-08-12 04:14:24 +01001269 len = min(ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
Mike Snitzer06a426c2010-08-12 04:14:09 +01001270
Mike Snitzera79245b2010-08-12 04:14:24 +01001271 __issue_target_requests(ci, ti, ti->num_discard_requests, len);
1272
1273 ci->sector += len;
1274 } while (ci->sector_count -= len);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001275
1276 return 0;
1277}
1278
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001279static int __clone_and_map(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
1281 struct bio *clone, *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001282 struct dm_target *ti;
1283 sector_t len = 0, max;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001284 struct dm_target_io *tio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001286 if (unlikely(bio->bi_rw & REQ_DISCARD))
1287 return __clone_and_map_discard(ci);
1288
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001289 ti = dm_table_find_target(ci->map, ci->sector);
1290 if (!dm_target_is_valid(ti))
1291 return -EIO;
1292
Mike Snitzer56a67df2010-08-12 04:14:10 +01001293 max = max_io_len(ci->sector, ti);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 if (ci->sector_count <= max) {
1296 /*
1297 * Optimise for the simple case where we can do all of
1298 * the remaining io with a single clone.
1299 */
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001300 __clone_and_map_simple(ci, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
1303 /*
1304 * There are some bvecs that don't span targets.
1305 * Do as many of these as possible.
1306 */
1307 int i;
1308 sector_t remaining = max;
1309 sector_t bv_len;
1310
1311 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
1312 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
1313
1314 if (bv_len > remaining)
1315 break;
1316
1317 remaining -= bv_len;
1318 len += bv_len;
1319 }
1320
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001321 tio = alloc_tio(ci, ti);
Stefan Bader9faf4002006-10-03 01:15:41 -07001322 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
1323 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 __map_bio(ti, clone, tio);
1325
1326 ci->sector += len;
1327 ci->sector_count -= len;
1328 ci->idx = i;
1329
1330 } else {
1331 /*
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001332 * Handle a bvec that must be split between two or more targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 */
1334 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001335 sector_t remaining = to_sector(bv->bv_len);
1336 unsigned int offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001338 do {
1339 if (offset) {
1340 ti = dm_table_find_target(ci->map, ci->sector);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001341 if (!dm_target_is_valid(ti))
1342 return -EIO;
1343
Mike Snitzer56a67df2010-08-12 04:14:10 +01001344 max = max_io_len(ci->sector, ti);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001347 len = min(remaining, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001349 tio = alloc_tio(ci, ti);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001350 clone = split_bvec(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -07001351 bv->bv_offset + offset, len,
1352 ci->md->bs);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001353
1354 __map_bio(ti, clone, tio);
1355
1356 ci->sector += len;
1357 ci->sector_count -= len;
1358 offset += to_bytes(len);
1359 } while (remaining -= len);
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 ci->idx++;
1362 }
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001363
1364 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365}
1366
1367/*
Mikulas Patocka8a53c282009-04-02 19:55:37 +01001368 * Split the bio into several clones and submit it to targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 */
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001370static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
1372 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001373 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001375 ci.map = dm_get_live_table(md);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001376 if (unlikely(!ci.map)) {
Tejun Heod87f4c12010-09-03 11:56:19 +02001377 if (!(bio->bi_rw & REQ_FLUSH))
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001378 bio_io_error(bio);
1379 else
Tejun Heod87f4c12010-09-03 11:56:19 +02001380 if (!md->flush_error)
1381 md->flush_error = -EIO;
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001382 return;
1383 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 ci.md = md;
1386 ci.bio = bio;
1387 ci.io = alloc_io(md);
1388 ci.io->error = 0;
1389 atomic_set(&ci.io->io_count, 1);
1390 ci.io->bio = bio;
1391 ci.io->md = md;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +01001392 spin_lock_init(&ci.io->endio_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 ci.sector = bio->bi_sector;
Tejun Heod87f4c12010-09-03 11:56:19 +02001394 if (!(bio->bi_rw & REQ_FLUSH))
1395 ci.sector_count = bio_sectors(bio);
1396 else {
1397 /* all FLUSH bio's reaching here should be empty */
1398 WARN_ON_ONCE(bio_has_data(bio));
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001399 ci.sector_count = 1;
Tejun Heod87f4c12010-09-03 11:56:19 +02001400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 ci.idx = bio->bi_idx;
1402
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001403 start_io_acct(ci.io);
Tejun Heod87f4c12010-09-03 11:56:19 +02001404 while (ci.sector_count && !error) {
1405 if (!(bio->bi_rw & REQ_FLUSH))
1406 error = __clone_and_map(&ci);
1407 else
1408 error = __clone_and_map_flush(&ci);
1409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001412 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 dm_table_put(ci.map);
1414}
1415/*-----------------------------------------------------------------
1416 * CRUD END
1417 *---------------------------------------------------------------*/
1418
Milan Brozf6fccb12008-07-21 12:00:37 +01001419static int dm_merge_bvec(struct request_queue *q,
1420 struct bvec_merge_data *bvm,
1421 struct bio_vec *biovec)
1422{
1423 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001424 struct dm_table *map = dm_get_live_table(md);
Milan Brozf6fccb12008-07-21 12:00:37 +01001425 struct dm_target *ti;
1426 sector_t max_sectors;
Mikulas Patocka50371082008-10-01 14:39:17 +01001427 int max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001428
1429 if (unlikely(!map))
Mikulas Patocka50371082008-10-01 14:39:17 +01001430 goto out;
Milan Brozf6fccb12008-07-21 12:00:37 +01001431
1432 ti = dm_table_find_target(map, bvm->bi_sector);
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001433 if (!dm_target_is_valid(ti))
1434 goto out_table;
Milan Brozf6fccb12008-07-21 12:00:37 +01001435
1436 /*
1437 * Find maximum amount of I/O that won't need splitting
1438 */
Mike Snitzer56a67df2010-08-12 04:14:10 +01001439 max_sectors = min(max_io_len(bvm->bi_sector, ti),
Milan Brozf6fccb12008-07-21 12:00:37 +01001440 (sector_t) BIO_MAX_SECTORS);
1441 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1442 if (max_size < 0)
1443 max_size = 0;
1444
1445 /*
1446 * merge_bvec_fn() returns number of bytes
1447 * it can accept at this offset
1448 * max is precomputed maximal io size
1449 */
1450 if (max_size && ti->type->merge)
1451 max_size = ti->type->merge(ti, bvm, biovec, max_size);
Mikulas Patocka8cbeb672009-06-22 10:12:14 +01001452 /*
1453 * If the target doesn't support merge method and some of the devices
1454 * provided their merge_bvec method (we know this by looking at
1455 * queue_max_hw_sectors), then we can't allow bios with multiple vector
1456 * entries. So always set max_size to 0, and the code below allows
1457 * just one page.
1458 */
1459 else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
1460
1461 max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001462
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001463out_table:
Mikulas Patocka50371082008-10-01 14:39:17 +01001464 dm_table_put(map);
1465
1466out:
Milan Brozf6fccb12008-07-21 12:00:37 +01001467 /*
1468 * Always allow an entire first page
1469 */
1470 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1471 max_size = biovec->bv_len;
1472
Milan Brozf6fccb12008-07-21 12:00:37 +01001473 return max_size;
1474}
1475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476/*
1477 * The request function that just remaps the bio built up by
1478 * dm_merge_bvec.
1479 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001480static int _dm_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
Kevin Corry12f03a42006-02-01 03:04:52 -08001482 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 struct mapped_device *md = q->queuedata;
Tejun Heoc9959052008-08-25 19:47:21 +09001484 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001486 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Tejun Heo074a7ac2008-08-25 19:56:14 +09001488 cpu = part_stat_lock();
1489 part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1490 part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1491 part_stat_unlock();
Kevin Corry12f03a42006-02-01 03:04:52 -08001492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 /*
Tejun Heod87f4c12010-09-03 11:56:19 +02001494 * If we're suspended or the thread is processing flushes
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001495 * we have to queue this io for later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 */
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001497 if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) ||
Tejun Heod87f4c12010-09-03 11:56:19 +02001498 (bio->bi_rw & REQ_FLUSH)) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001499 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001501 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) &&
1502 bio_rw(bio) == READA) {
1503 bio_io_error(bio);
1504 return 0;
1505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Mikulas Patocka92c63902009-04-09 00:27:15 +01001507 queue_io(md, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Mikulas Patocka92c63902009-04-09 00:27:15 +01001509 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 }
1511
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001512 __split_and_process_bio(md, bio);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001513 up_read(&md->io_lock);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001514 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515}
1516
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001517static int dm_make_request(struct request_queue *q, struct bio *bio)
1518{
1519 struct mapped_device *md = q->queuedata;
1520
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001521 return md->saved_make_request_fn(q, bio); /* call __make_request() */
1522}
1523
1524static int dm_request_based(struct mapped_device *md)
1525{
1526 return blk_queue_stackable(md->queue);
1527}
1528
1529static int dm_request(struct request_queue *q, struct bio *bio)
1530{
1531 struct mapped_device *md = q->queuedata;
1532
1533 if (dm_request_based(md))
1534 return dm_make_request(q, bio);
1535
1536 return _dm_request(q, bio);
1537}
1538
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001539static bool dm_rq_is_flush_request(struct request *rq)
1540{
FUJITA Tomonori144d6ed2010-07-03 17:45:37 +09001541 if (rq->cmd_flags & REQ_FLUSH)
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001542 return true;
1543 else
1544 return false;
1545}
1546
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001547void dm_dispatch_request(struct request *rq)
1548{
1549 int r;
1550
1551 if (blk_queue_io_stat(rq->q))
1552 rq->cmd_flags |= REQ_IO_STAT;
1553
1554 rq->start_time = jiffies;
1555 r = blk_insert_cloned_request(rq->q, rq);
1556 if (r)
1557 dm_complete_request(rq, r);
1558}
1559EXPORT_SYMBOL_GPL(dm_dispatch_request);
1560
1561static void dm_rq_bio_destructor(struct bio *bio)
1562{
1563 struct dm_rq_clone_bio_info *info = bio->bi_private;
1564 struct mapped_device *md = info->tio->md;
1565
1566 free_bio_info(info);
1567 bio_free(bio, md->bs);
1568}
1569
1570static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1571 void *data)
1572{
1573 struct dm_rq_target_io *tio = data;
1574 struct mapped_device *md = tio->md;
1575 struct dm_rq_clone_bio_info *info = alloc_bio_info(md);
1576
1577 if (!info)
1578 return -ENOMEM;
1579
1580 info->orig = bio_orig;
1581 info->tio = tio;
1582 bio->bi_end_io = end_clone_bio;
1583 bio->bi_private = info;
1584 bio->bi_destructor = dm_rq_bio_destructor;
1585
1586 return 0;
1587}
1588
1589static int setup_clone(struct request *clone, struct request *rq,
1590 struct dm_rq_target_io *tio)
1591{
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001592 int r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001593
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001594 if (dm_rq_is_flush_request(rq)) {
1595 blk_rq_init(NULL, clone);
1596 clone->cmd_type = REQ_TYPE_FS;
1597 clone->cmd_flags |= (REQ_HARDBARRIER | WRITE);
1598 } else {
1599 r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1600 dm_rq_bio_constructor, tio);
1601 if (r)
1602 return r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001603
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001604 clone->cmd = rq->cmd;
1605 clone->cmd_len = rq->cmd_len;
1606 clone->sense = rq->sense;
1607 clone->buffer = rq->buffer;
1608 }
1609
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001610 clone->end_io = end_clone_request;
1611 clone->end_io_data = tio;
1612
1613 return 0;
1614}
1615
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001616static struct request *clone_rq(struct request *rq, struct mapped_device *md,
1617 gfp_t gfp_mask)
1618{
1619 struct request *clone;
1620 struct dm_rq_target_io *tio;
1621
1622 tio = alloc_rq_tio(md, gfp_mask);
1623 if (!tio)
1624 return NULL;
1625
1626 tio->md = md;
1627 tio->ti = NULL;
1628 tio->orig = rq;
1629 tio->error = 0;
1630 memset(&tio->info, 0, sizeof(tio->info));
1631
1632 clone = &tio->clone;
1633 if (setup_clone(clone, rq, tio)) {
1634 /* -ENOMEM */
1635 free_rq_tio(tio);
1636 return NULL;
1637 }
1638
1639 return clone;
1640}
1641
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001642/*
1643 * Called with the queue lock held.
1644 */
1645static int dm_prep_fn(struct request_queue *q, struct request *rq)
1646{
1647 struct mapped_device *md = q->queuedata;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001648 struct request *clone;
1649
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001650 if (unlikely(dm_rq_is_flush_request(rq)))
1651 return BLKPREP_OK;
1652
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001653 if (unlikely(rq->special)) {
1654 DMWARN("Already has something in rq->special.");
1655 return BLKPREP_KILL;
1656 }
1657
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001658 clone = clone_rq(rq, md, GFP_ATOMIC);
1659 if (!clone)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001660 return BLKPREP_DEFER;
1661
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001662 rq->special = clone;
1663 rq->cmd_flags |= REQ_DONTPREP;
1664
1665 return BLKPREP_OK;
1666}
1667
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001668/*
1669 * Returns:
1670 * 0 : the request has been processed (not requeued)
1671 * !0 : the request has been requeued
1672 */
1673static int map_request(struct dm_target *ti, struct request *clone,
1674 struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001675{
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001676 int r, requeued = 0;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001677 struct dm_rq_target_io *tio = clone->end_io_data;
1678
1679 /*
1680 * Hold the md reference here for the in-flight I/O.
1681 * We can't rely on the reference count by device opener,
1682 * because the device may be closed during the request completion
1683 * when all bios are completed.
1684 * See the comment in rq_completed() too.
1685 */
1686 dm_get(md);
1687
1688 tio->ti = ti;
1689 r = ti->type->map_rq(ti, clone, &tio->info);
1690 switch (r) {
1691 case DM_MAPIO_SUBMITTED:
1692 /* The target has taken the I/O to submit by itself later */
1693 break;
1694 case DM_MAPIO_REMAPPED:
1695 /* The target has remapped the I/O so dispatch it */
Jun'ichi Nomura6db4ccd2009-12-10 23:52:25 +00001696 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
1697 blk_rq_pos(tio->orig));
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001698 dm_dispatch_request(clone);
1699 break;
1700 case DM_MAPIO_REQUEUE:
1701 /* The target wants to requeue the I/O */
1702 dm_requeue_unmapped_request(clone);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001703 requeued = 1;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001704 break;
1705 default:
1706 if (r > 0) {
1707 DMWARN("unimplemented target map return value: %d", r);
1708 BUG();
1709 }
1710
1711 /* The target wants to complete the I/O */
1712 dm_kill_unmapped_request(clone, r);
1713 break;
1714 }
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001715
1716 return requeued;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001717}
1718
1719/*
1720 * q->request_fn for request-based dm.
1721 * Called with the queue lock held.
1722 */
1723static void dm_request_fn(struct request_queue *q)
1724{
1725 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001726 struct dm_table *map = dm_get_live_table(md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001727 struct dm_target *ti;
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001728 struct request *rq, *clone;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001729
1730 /*
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001731 * For suspend, check blk_queue_stopped() and increment
1732 * ->pending within a single queue_lock not to increment the
1733 * number of in-flight I/Os after the queue is stopped in
1734 * dm_suspend().
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001735 */
1736 while (!blk_queue_plugged(q) && !blk_queue_stopped(q)) {
1737 rq = blk_peek_request(q);
1738 if (!rq)
1739 goto plug_and_out;
1740
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001741 if (unlikely(dm_rq_is_flush_request(rq))) {
1742 BUG_ON(md->flush_request);
1743 md->flush_request = rq;
1744 blk_start_request(rq);
1745 queue_work(md->wq, &md->barrier_work);
1746 goto out;
1747 }
1748
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001749 ti = dm_table_find_target(map, blk_rq_pos(rq));
1750 if (ti->type->busy && ti->type->busy(ti))
1751 goto plug_and_out;
1752
1753 blk_start_request(rq);
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001754 clone = rq->special;
1755 atomic_inc(&md->pending[rq_data_dir(clone)]);
1756
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001757 spin_unlock(q->queue_lock);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001758 if (map_request(ti, clone, md))
1759 goto requeued;
1760
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001761 spin_lock_irq(q->queue_lock);
1762 }
1763
1764 goto out;
1765
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001766requeued:
1767 spin_lock_irq(q->queue_lock);
1768
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001769plug_and_out:
1770 if (!elv_queue_empty(q))
1771 /* Some requests still remain, retry later */
1772 blk_plug_device(q);
1773
1774out:
1775 dm_table_put(map);
1776
1777 return;
1778}
1779
1780int dm_underlying_device_busy(struct request_queue *q)
1781{
1782 return blk_lld_busy(q);
1783}
1784EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1785
1786static int dm_lld_busy(struct request_queue *q)
1787{
1788 int r;
1789 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001790 struct dm_table *map = dm_get_live_table(md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001791
1792 if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1793 r = 1;
1794 else
1795 r = dm_table_any_busy_target(map);
1796
1797 dm_table_put(map);
1798
1799 return r;
1800}
1801
Jens Axboe165125e2007-07-24 09:28:11 +02001802static void dm_unplug_all(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
1804 struct mapped_device *md = q->queuedata;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001805 struct dm_table *map = dm_get_live_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
1807 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001808 if (dm_request_based(md))
1809 generic_unplug_device(q);
1810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 dm_table_unplug_all(map);
1812 dm_table_put(map);
1813 }
1814}
1815
1816static int dm_any_congested(void *congested_data, int bdi_bits)
1817{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001818 int r = bdi_bits;
1819 struct mapped_device *md = congested_data;
1820 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001822 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001823 map = dm_get_live_table(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001824 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001825 /*
1826 * Request-based dm cares about only own queue for
1827 * the query about congestion status of request_queue
1828 */
1829 if (dm_request_based(md))
1830 r = md->queue->backing_dev_info.state &
1831 bdi_bits;
1832 else
1833 r = dm_table_any_congested(map, bdi_bits);
1834
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001835 dm_table_put(map);
1836 }
1837 }
1838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 return r;
1840}
1841
1842/*-----------------------------------------------------------------
1843 * An IDR is used to keep track of allocated minor numbers.
1844 *---------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845static DEFINE_IDR(_minor_idr);
1846
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001847static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001849 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001851 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852}
1853
1854/*
1855 * See if the device with a specific minor # is free.
1856 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001857static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
1859 int r, m;
1860
1861 if (minor >= (1 << MINORBITS))
1862 return -EINVAL;
1863
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001864 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
1865 if (!r)
1866 return -ENOMEM;
1867
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001868 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
1870 if (idr_find(&_minor_idr, minor)) {
1871 r = -EBUSY;
1872 goto out;
1873 }
1874
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001875 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001876 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
1879 if (m != minor) {
1880 idr_remove(&_minor_idr, m);
1881 r = -EBUSY;
1882 goto out;
1883 }
1884
1885out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001886 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 return r;
1888}
1889
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001890static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001892 int r, m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001895 if (!r)
1896 return -ENOMEM;
1897
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001898 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001900 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001901 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904 if (m >= (1 << MINORBITS)) {
1905 idr_remove(&_minor_idr, m);
1906 r = -ENOSPC;
1907 goto out;
1908 }
1909
1910 *minor = m;
1911
1912out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001913 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 return r;
1915}
1916
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001917static const struct block_device_operations dm_blk_dops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Mikulas Patocka53d59142009-04-02 19:55:37 +01001919static void dm_wq_work(struct work_struct *work);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001920static void dm_rq_barrier_work(struct work_struct *work);
Mikulas Patocka53d59142009-04-02 19:55:37 +01001921
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001922static void dm_init_md_queue(struct mapped_device *md)
1923{
1924 /*
1925 * Request-based dm devices cannot be stacked on top of bio-based dm
1926 * devices. The type of this dm device has not been decided yet.
1927 * The type is decided at the first table loading time.
1928 * To prevent problematic device stacking, clear the queue flag
1929 * for request stacking support until then.
1930 *
1931 * This queue is new, so no concurrency on the queue_flags.
1932 */
1933 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1934
1935 md->queue->queuedata = md;
1936 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1937 md->queue->backing_dev_info.congested_data = md;
1938 blk_queue_make_request(md->queue, dm_request);
1939 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
1940 md->queue->unplug_fn = dm_unplug_all;
1941 blk_queue_merge_bvec(md->queue, dm_merge_bvec);
Tejun Heod87f4c12010-09-03 11:56:19 +02001942 blk_queue_flush(md->queue, REQ_FLUSH | REQ_FUA);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001943}
1944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945/*
1946 * Allocate and initialise a blank device with a given minor.
1947 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001948static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
1950 int r;
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001951 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001952 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
1954 if (!md) {
1955 DMWARN("unable to allocate device, out of memory.");
1956 return NULL;
1957 }
1958
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001959 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00001960 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001961
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001963 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001964 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001965 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001966 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001968 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Mike Snitzera5664da2010-08-12 04:14:01 +01001970 md->type = DM_TYPE_NONE;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001971 init_rwsem(&md->io_lock);
Daniel Walkere61290a2008-02-08 02:10:08 +00001972 mutex_init(&md->suspend_lock);
Mike Snitzera5664da2010-08-12 04:14:01 +01001973 mutex_init(&md->type_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01001974 spin_lock_init(&md->deferred_lock);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001975 spin_lock_init(&md->barrier_error_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 rwlock_init(&md->map_lock);
1977 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07001978 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001980 atomic_set(&md->uevent_seq, 0);
1981 INIT_LIST_HEAD(&md->uevent_list);
1982 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001984 md->queue = blk_alloc_queue(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 if (!md->queue)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001986 goto bad_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001988 dm_init_md_queue(md);
Stefan Bader9faf4002006-10-03 01:15:41 -07001989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 md->disk = alloc_disk(1);
1991 if (!md->disk)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001992 goto bad_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001994 atomic_set(&md->pending[0], 0);
1995 atomic_set(&md->pending[1], 0);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001996 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01001997 INIT_WORK(&md->work, dm_wq_work);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001998 INIT_WORK(&md->barrier_work, dm_rq_barrier_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001999 init_waitqueue_head(&md->eventq);
2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 md->disk->major = _major;
2002 md->disk->first_minor = minor;
2003 md->disk->fops = &dm_blk_dops;
2004 md->disk->queue = md->queue;
2005 md->disk->private_data = md;
2006 sprintf(md->disk->disk_name, "dm-%d", minor);
2007 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08002008 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Milan Broz304f3f62008-02-08 02:11:17 +00002010 md->wq = create_singlethread_workqueue("kdmflush");
2011 if (!md->wq)
2012 goto bad_thread;
2013
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002014 md->bdev = bdget_disk(md->disk, 0);
2015 if (!md->bdev)
2016 goto bad_bdev;
2017
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002018 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002019 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002020 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002021 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002022
2023 BUG_ON(old_md != MINOR_ALLOCED);
2024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 return md;
2026
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002027bad_bdev:
2028 destroy_workqueue(md->wq);
Milan Broz304f3f62008-02-08 02:11:17 +00002029bad_thread:
Zdenek Kabelac03022c52009-10-16 23:18:15 +01002030 del_gendisk(md->disk);
Milan Broz304f3f62008-02-08 02:11:17 +00002031 put_disk(md->disk);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002032bad_disk:
Al Viro1312f402006-03-12 11:02:03 -05002033 blk_cleanup_queue(md->queue);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002034bad_queue:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002036bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002037 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002038bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 kfree(md);
2040 return NULL;
2041}
2042
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01002043static void unlock_fs(struct mapped_device *md);
2044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045static void free_dev(struct mapped_device *md)
2046{
Tejun Heof331c022008-09-03 09:01:48 +02002047 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08002048
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002049 unlock_fs(md);
2050 bdput(md->bdev);
Milan Broz304f3f62008-02-08 02:11:17 +00002051 destroy_workqueue(md->wq);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002052 if (md->tio_pool)
2053 mempool_destroy(md->tio_pool);
2054 if (md->io_pool)
2055 mempool_destroy(md->io_pool);
2056 if (md->bs)
2057 bioset_free(md->bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01002058 blk_integrity_unregister(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 del_gendisk(md->disk);
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08002060 free_minor(minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002061
2062 spin_lock(&_minor_lock);
2063 md->disk->private_data = NULL;
2064 spin_unlock(&_minor_lock);
2065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 put_disk(md->disk);
Al Viro1312f402006-03-12 11:02:03 -05002067 blk_cleanup_queue(md->queue);
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002068 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 kfree(md);
2070}
2071
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002072static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
2073{
2074 struct dm_md_mempools *p;
2075
2076 if (md->io_pool && md->tio_pool && md->bs)
2077 /* the md already has necessary mempools */
2078 goto out;
2079
2080 p = dm_table_get_md_mempools(t);
2081 BUG_ON(!p || md->io_pool || md->tio_pool || md->bs);
2082
2083 md->io_pool = p->io_pool;
2084 p->io_pool = NULL;
2085 md->tio_pool = p->tio_pool;
2086 p->tio_pool = NULL;
2087 md->bs = p->bs;
2088 p->bs = NULL;
2089
2090out:
2091 /* mempool bind completed, now no need any mempools in the table */
2092 dm_table_free_md_mempools(t);
2093}
2094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095/*
2096 * Bind a table to the device.
2097 */
2098static void event_callback(void *context)
2099{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002100 unsigned long flags;
2101 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 struct mapped_device *md = (struct mapped_device *) context;
2103
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002104 spin_lock_irqsave(&md->uevent_lock, flags);
2105 list_splice_init(&md->uevent_list, &uevents);
2106 spin_unlock_irqrestore(&md->uevent_lock, flags);
2107
Tejun Heoed9e1982008-08-25 19:56:05 +09002108 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 atomic_inc(&md->event_nr);
2111 wake_up(&md->eventq);
2112}
2113
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002114static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002116 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002118 mutex_lock(&md->bdev->bd_inode->i_mutex);
2119 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
2120 mutex_unlock(&md->bdev->bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121}
2122
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002123/*
2124 * Returns old map, which caller must destroy.
2125 */
2126static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2127 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002129 struct dm_table *old_map;
Jens Axboe165125e2007-07-24 09:28:11 +02002130 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 sector_t size;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002132 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002135
2136 /*
2137 * Wipe any geometry if the size of the table changed.
2138 */
2139 if (size != get_capacity(md->disk))
2140 memset(&md->geometry, 0, sizeof(md->geometry));
2141
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002142 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002144 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002145
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002146 /*
2147 * The queue hasn't been stopped yet, if the old table type wasn't
2148 * for request-based during suspension. So stop it to prevent
2149 * I/O mapping before resume.
2150 * This must be done before setting the queue restrictions,
2151 * because request-based dm may be run just after the setting.
2152 */
2153 if (dm_table_request_based(t) && !blk_queue_stopped(q))
2154 stop_queue(q);
2155
2156 __bind_mempools(md, t);
2157
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002158 write_lock_irqsave(&md->map_lock, flags);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002159 old_map = md->map;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002160 md->map = t;
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002161 dm_table_set_restrictions(t, q, limits);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002162 write_unlock_irqrestore(&md->map_lock, flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002163
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002164 return old_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165}
2166
Alasdair G Kergona7940152009-12-10 23:52:23 +00002167/*
2168 * Returns unbound table for the caller to free.
2169 */
2170static struct dm_table *__unbind(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171{
2172 struct dm_table *map = md->map;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002173 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
2175 if (!map)
Alasdair G Kergona7940152009-12-10 23:52:23 +00002176 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
2178 dm_table_event_callback(map, NULL, NULL);
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002179 write_lock_irqsave(&md->map_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 md->map = NULL;
Kiyoshi Ueda523d9292009-06-22 10:12:37 +01002181 write_unlock_irqrestore(&md->map_lock, flags);
Alasdair G Kergona7940152009-12-10 23:52:23 +00002182
2183 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184}
2185
2186/*
2187 * Constructor for a new device.
2188 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002189int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
2191 struct mapped_device *md;
2192
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002193 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 if (!md)
2195 return -ENXIO;
2196
Milan Broz784aae72009-01-06 03:05:12 +00002197 dm_sysfs_init(md);
2198
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 *result = md;
2200 return 0;
2201}
2202
Mike Snitzera5664da2010-08-12 04:14:01 +01002203/*
2204 * Functions to manage md->type.
2205 * All are required to hold md->type_lock.
2206 */
2207void dm_lock_md_type(struct mapped_device *md)
2208{
2209 mutex_lock(&md->type_lock);
2210}
2211
2212void dm_unlock_md_type(struct mapped_device *md)
2213{
2214 mutex_unlock(&md->type_lock);
2215}
2216
2217void dm_set_md_type(struct mapped_device *md, unsigned type)
2218{
2219 md->type = type;
2220}
2221
2222unsigned dm_get_md_type(struct mapped_device *md)
2223{
2224 return md->type;
2225}
2226
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002227/*
2228 * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
2229 */
2230static int dm_init_request_based_queue(struct mapped_device *md)
2231{
2232 struct request_queue *q = NULL;
2233
2234 if (md->queue->elevator)
2235 return 1;
2236
2237 /* Fully initialize the queue */
2238 q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
2239 if (!q)
2240 return 0;
2241
2242 md->queue = q;
2243 md->saved_make_request_fn = md->queue->make_request_fn;
2244 dm_init_md_queue(md);
2245 blk_queue_softirq_done(md->queue, dm_softirq_done);
2246 blk_queue_prep_rq(md->queue, dm_prep_fn);
2247 blk_queue_lld_busy(md->queue, dm_lld_busy);
Tejun Heod87f4c12010-09-03 11:56:19 +02002248 /* no flush support for request based dm yet */
2249 blk_queue_flush(md->queue, 0);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002250
2251 elv_register_queue(md->queue);
2252
2253 return 1;
2254}
2255
2256/*
2257 * Setup the DM device's queue based on md's type
2258 */
2259int dm_setup_md_queue(struct mapped_device *md)
2260{
2261 if ((dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) &&
2262 !dm_init_request_based_queue(md)) {
2263 DMWARN("Cannot initialize queue for request-based mapped device");
2264 return -EINVAL;
2265 }
2266
2267 return 0;
2268}
2269
David Teigland637842c2006-01-06 00:20:00 -08002270static struct mapped_device *dm_find_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271{
2272 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 unsigned minor = MINOR(dev);
2274
2275 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2276 return NULL;
2277
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002278 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
2280 md = idr_find(&_minor_idr, minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002281 if (md && (md == MINOR_ALLOCED ||
Tejun Heof331c022008-09-03 09:01:48 +02002282 (MINOR(disk_devt(dm_disk(md))) != minor) ||
Kiyoshi Uedaabdc5682010-08-12 04:13:54 +01002283 dm_deleting_md(md) ||
Alasdair G Kergon17b2f662006-06-26 00:27:33 -07002284 test_bit(DMF_FREEING, &md->flags))) {
David Teigland637842c2006-01-06 00:20:00 -08002285 md = NULL;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002286 goto out;
2287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002289out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002290 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
David Teigland637842c2006-01-06 00:20:00 -08002292 return md;
2293}
2294
David Teiglandd229a952006-01-06 00:20:01 -08002295struct mapped_device *dm_get_md(dev_t dev)
2296{
2297 struct mapped_device *md = dm_find_md(dev);
2298
2299 if (md)
2300 dm_get(md);
2301
2302 return md;
2303}
2304
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002305void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002306{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002307 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308}
2309
2310void dm_set_mdptr(struct mapped_device *md, void *ptr)
2311{
2312 md->interface_ptr = ptr;
2313}
2314
2315void dm_get(struct mapped_device *md)
2316{
2317 atomic_inc(&md->holders);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002318 BUG_ON(test_bit(DMF_FREEING, &md->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319}
2320
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002321const char *dm_device_name(struct mapped_device *md)
2322{
2323 return md->name;
2324}
2325EXPORT_SYMBOL_GPL(dm_device_name);
2326
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002327static void __dm_destroy(struct mapped_device *md, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
Mike Anderson1134e5a2006-03-27 01:17:54 -08002329 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002331 might_sleep();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002332
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002333 spin_lock(&_minor_lock);
2334 map = dm_get_live_table(md);
2335 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2336 set_bit(DMF_FREEING, &md->flags);
2337 spin_unlock(&_minor_lock);
2338
2339 if (!dm_suspended_md(md)) {
2340 dm_table_presuspend_targets(map);
2341 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 }
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002343
2344 /*
2345 * Rare, but there may be I/O requests still going to complete,
2346 * for example. Wait for all references to disappear.
2347 * No one should increment the reference count of the mapped_device,
2348 * after the mapped_device state becomes DMF_FREEING.
2349 */
2350 if (wait)
2351 while (atomic_read(&md->holders))
2352 msleep(1);
2353 else if (atomic_read(&md->holders))
2354 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2355 dm_device_name(md), atomic_read(&md->holders));
2356
2357 dm_sysfs_exit(md);
2358 dm_table_put(map);
2359 dm_table_destroy(__unbind(md));
2360 free_dev(md);
2361}
2362
2363void dm_destroy(struct mapped_device *md)
2364{
2365 __dm_destroy(md, true);
2366}
2367
2368void dm_destroy_immediate(struct mapped_device *md)
2369{
2370 __dm_destroy(md, false);
2371}
2372
2373void dm_put(struct mapped_device *md)
2374{
2375 atomic_dec(&md->holders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376}
Edward Goggin79eb8852007-05-09 02:32:56 -07002377EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Mikulas Patocka401600d2009-04-02 19:55:38 +01002379static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
Milan Broz46125c12008-02-08 02:10:30 +00002380{
2381 int r = 0;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002382 DECLARE_WAITQUEUE(wait, current);
2383
2384 dm_unplug_all(md->queue);
2385
2386 add_wait_queue(&md->wait, &wait);
Milan Broz46125c12008-02-08 02:10:30 +00002387
2388 while (1) {
Mikulas Patocka401600d2009-04-02 19:55:38 +01002389 set_current_state(interruptible);
Milan Broz46125c12008-02-08 02:10:30 +00002390
2391 smp_mb();
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002392 if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002393 break;
2394
Mikulas Patocka401600d2009-04-02 19:55:38 +01002395 if (interruptible == TASK_INTERRUPTIBLE &&
2396 signal_pending(current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002397 r = -EINTR;
2398 break;
2399 }
2400
2401 io_schedule();
2402 }
2403 set_current_state(TASK_RUNNING);
2404
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002405 remove_wait_queue(&md->wait, &wait);
2406
Milan Broz46125c12008-02-08 02:10:30 +00002407 return r;
2408}
2409
Tejun Heod87f4c12010-09-03 11:56:19 +02002410static void process_flush(struct mapped_device *md, struct bio *bio)
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002411{
Tejun Heod87f4c12010-09-03 11:56:19 +02002412 md->flush_error = 0;
2413
2414 /* handle REQ_FLUSH */
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002415 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
Mikulas Patocka52b1fd52009-06-22 10:12:21 +01002416
Tejun Heod87f4c12010-09-03 11:56:19 +02002417 bio_init(&md->flush_bio);
2418 md->flush_bio.bi_bdev = md->bdev;
2419 md->flush_bio.bi_rw = WRITE_FLUSH;
2420 __split_and_process_bio(md, &md->flush_bio);
Mikulas Patocka52b1fd52009-06-22 10:12:21 +01002421
2422 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002423
Tejun Heod87f4c12010-09-03 11:56:19 +02002424 /* if it's an empty flush or the preflush failed, we're done */
2425 if (!bio_has_data(bio) || md->flush_error) {
2426 if (md->flush_error != DM_ENDIO_REQUEUE)
2427 bio_endio(bio, md->flush_error);
2428 else {
2429 spin_lock_irq(&md->deferred_lock);
2430 bio_list_add_head(&md->deferred, bio);
2431 spin_unlock_irq(&md->deferred_lock);
2432 }
2433 return;
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002434 }
2435
Tejun Heod87f4c12010-09-03 11:56:19 +02002436 /* issue data + REQ_FUA */
2437 bio->bi_rw &= ~REQ_FLUSH;
2438 __split_and_process_bio(md, bio);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002439}
2440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441/*
2442 * Process the deferred bios
2443 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002444static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445{
Mikulas Patockaef208582009-04-02 19:55:38 +01002446 struct mapped_device *md = container_of(work, struct mapped_device,
2447 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002448 struct bio *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
Mikulas Patockaef208582009-04-02 19:55:38 +01002450 down_write(&md->io_lock);
2451
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002452 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002453 spin_lock_irq(&md->deferred_lock);
2454 c = bio_list_pop(&md->deferred);
2455 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002456
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002457 if (!c) {
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002458 clear_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002459 break;
2460 }
2461
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002462 up_write(&md->io_lock);
2463
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002464 if (dm_request_based(md))
2465 generic_make_request(c);
2466 else {
Tejun Heod87f4c12010-09-03 11:56:19 +02002467 if (c->bi_rw & REQ_FLUSH)
2468 process_flush(md, c);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002469 else
2470 __split_and_process_bio(md, c);
2471 }
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002472
2473 down_write(&md->io_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002474 }
Milan Broz73d410c2008-02-08 02:10:25 +00002475
Mikulas Patockaef208582009-04-02 19:55:38 +01002476 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477}
2478
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002479static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002480{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002481 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2482 smp_mb__after_clear_bit();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002483 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002484}
2485
Mike Snitzer57cba5d2010-08-12 04:14:04 +01002486static void dm_rq_set_target_request_nr(struct request *clone, unsigned request_nr)
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002487{
2488 struct dm_rq_target_io *tio = clone->end_io_data;
2489
Mike Snitzer57cba5d2010-08-12 04:14:04 +01002490 tio->info.target_request_nr = request_nr;
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002491}
2492
2493/* Issue barrier requests to targets and wait for their completion. */
2494static int dm_rq_barrier(struct mapped_device *md)
2495{
2496 int i, j;
Alasdair G Kergon7c666412009-12-10 23:52:19 +00002497 struct dm_table *map = dm_get_live_table(md);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002498 unsigned num_targets = dm_table_get_num_targets(map);
2499 struct dm_target *ti;
2500 struct request *clone;
2501
2502 md->barrier_error = 0;
2503
2504 for (i = 0; i < num_targets; i++) {
2505 ti = dm_table_get_target(map, i);
2506 for (j = 0; j < ti->num_flush_requests; j++) {
2507 clone = clone_rq(md->flush_request, md, GFP_NOIO);
Mike Snitzer57cba5d2010-08-12 04:14:04 +01002508 dm_rq_set_target_request_nr(clone, j);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002509 atomic_inc(&md->pending[rq_data_dir(clone)]);
2510 map_request(ti, clone, md);
2511 }
2512 }
2513
2514 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2515 dm_table_put(map);
2516
2517 return md->barrier_error;
2518}
2519
2520static void dm_rq_barrier_work(struct work_struct *work)
2521{
2522 int error;
2523 struct mapped_device *md = container_of(work, struct mapped_device,
2524 barrier_work);
2525 struct request_queue *q = md->queue;
2526 struct request *rq;
2527 unsigned long flags;
2528
2529 /*
2530 * Hold the md reference here and leave it at the last part so that
2531 * the md can't be deleted by device opener when the barrier request
2532 * completes.
2533 */
2534 dm_get(md);
2535
2536 error = dm_rq_barrier(md);
2537
2538 rq = md->flush_request;
2539 md->flush_request = NULL;
2540
2541 if (error == DM_ENDIO_REQUEUE) {
2542 spin_lock_irqsave(q->queue_lock, flags);
2543 blk_requeue_request(q, rq);
2544 spin_unlock_irqrestore(q->queue_lock, flags);
2545 } else
2546 blk_end_request_all(rq, error);
2547
2548 blk_run_queue(q);
2549
2550 dm_put(md);
2551}
2552
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002554 * Swap in a new table, returning the old one for the caller to destroy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002556struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002558 struct dm_table *map = ERR_PTR(-EINVAL);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002559 struct queue_limits limits;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002560 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
Daniel Walkere61290a2008-02-08 02:10:08 +00002562 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
2564 /* device must be suspended */
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002565 if (!dm_suspended_md(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002566 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002568 r = dm_calculate_queue_limits(table, &limits);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002569 if (r) {
2570 map = ERR_PTR(r);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002571 goto out;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002572 }
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002573
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002574 map = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002576out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002577 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002578 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579}
2580
2581/*
2582 * Functions to lock and unlock any filesystem running on the
2583 * device.
2584 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002585static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002587 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
2589 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002590
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002591 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002592 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002593 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002594 md->frozen_sb = NULL;
2595 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002596 }
2597
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002598 set_bit(DMF_FROZEN, &md->flags);
2599
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 return 0;
2601}
2602
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002603static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002605 if (!test_bit(DMF_FROZEN, &md->flags))
2606 return;
2607
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002608 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002610 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611}
2612
2613/*
2614 * We need to be able to change a mapping table under a mounted
2615 * filesystem. For example we might want to move some data in
2616 * the background. Before the table can be swapped with
2617 * dm_bind_table, dm_suspend must be called to flush any in
2618 * flight bios and ensure that any further io gets deferred.
2619 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002620/*
2621 * Suspend mechanism in request-based dm.
2622 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002623 * 1. Flush all I/Os by lock_fs() if needed.
2624 * 2. Stop dispatching any I/O by stopping the request_queue.
2625 * 3. Wait for all in-flight I/Os to be completed or requeued.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002626 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002627 * To abort suspend, start the request_queue.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002628 */
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002629int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002631 struct dm_table *map = NULL;
Milan Broz46125c12008-02-08 02:10:30 +00002632 int r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002633 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002634 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Daniel Walkere61290a2008-02-08 02:10:08 +00002636 mutex_lock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002637
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002638 if (dm_suspended_md(md)) {
Milan Broz73d410c2008-02-08 02:10:25 +00002639 r = -EINVAL;
Alasdair G Kergond2874832006-11-08 17:44:43 -08002640 goto out_unlock;
Milan Broz73d410c2008-02-08 02:10:25 +00002641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
Alasdair G Kergon7c666412009-12-10 23:52:19 +00002643 map = dm_get_live_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002645 /*
2646 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2647 * This flag is cleared before dm_suspend returns.
2648 */
2649 if (noflush)
2650 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2651
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002652 /* This does not get reverted if there's an error later. */
2653 dm_table_presuspend_targets(map);
2654
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002655 /*
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002656 * Flush I/O to the device.
2657 * Any I/O submitted after lock_fs() may not be flushed.
2658 * noflush takes precedence over do_lockfs.
2659 * (lock_fs() flushes I/Os and waits for them to complete.)
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002660 */
2661 if (!noflush && do_lockfs) {
2662 r = lock_fs(md);
2663 if (r)
Kiyoshi Uedaf431d962008-10-21 17:45:07 +01002664 goto out;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
2667 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002668 * Here we must make sure that no processes are submitting requests
2669 * to target drivers i.e. no one may be executing
2670 * __split_and_process_bio. This is called from dm_request and
2671 * dm_wq_work.
2672 *
2673 * To get all processes out of __split_and_process_bio in dm_request,
2674 * we take the write lock. To prevent any process from reentering
2675 * __split_and_process_bio from dm_request, we set
2676 * DMF_QUEUE_IO_TO_THREAD.
2677 *
2678 * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND
2679 * and call flush_workqueue(md->wq). flush_workqueue will wait until
2680 * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any
2681 * further calls to __split_and_process_bio from dm_wq_work.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002683 down_write(&md->io_lock);
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002684 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2685 set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002686 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002688 /*
2689 * Request-based dm uses md->wq for barrier (dm_rq_barrier_work) which
2690 * can be kicked until md->queue is stopped. So stop md->queue before
2691 * flushing md->wq.
2692 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002693 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002694 stop_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002695
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002696 flush_workqueue(md->wq);
2697
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002699 * At this point no more requests are entering target request routines.
2700 * We call dm_wait_for_completion to wait for all existing requests
2701 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 */
Mikulas Patocka401600d2009-04-02 19:55:38 +01002703 r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002705 down_write(&md->io_lock);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002706 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01002707 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Milan Broz94d63512008-02-08 02:10:27 +00002708 up_write(&md->io_lock);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002709
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00002711 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002712 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00002713
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002714 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002715 start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002716
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002717 unlock_fs(md);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002718 goto out; /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002719 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002720
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002721 /*
2722 * If dm_wait_for_completion returned 0, the device is completely
2723 * quiescent now. There is no request-processing activity. All new
2724 * requests are being added to md->deferred list.
2725 */
2726
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 set_bit(DMF_SUSPENDED, &md->flags);
2728
Kiyoshi Ueda4d4471c2009-12-10 23:52:26 +00002729 dm_table_postsuspend_targets(map);
2730
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002731out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 dm_table_put(map);
Alasdair G Kergond2874832006-11-08 17:44:43 -08002733
2734out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00002735 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002736 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737}
2738
2739int dm_resume(struct mapped_device *md)
2740{
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002741 int r = -EINVAL;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002742 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
Daniel Walkere61290a2008-02-08 02:10:08 +00002744 mutex_lock(&md->suspend_lock);
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002745 if (!dm_suspended_md(md))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002746 goto out;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002747
Alasdair G Kergon7c666412009-12-10 23:52:19 +00002748 map = dm_get_live_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002749 if (!map || !dm_table_get_size(map))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002750 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
Milan Broz8757b772006-10-03 01:15:36 -07002752 r = dm_table_resume_targets(map);
2753 if (r)
2754 goto out;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002755
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002756 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002757
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002758 /*
2759 * Flushing deferred I/Os must be done after targets are resumed
2760 * so that mapping of targets can work correctly.
2761 * Request-based dm is queueing the deferred I/Os in its request_queue.
2762 */
2763 if (dm_request_based(md))
2764 start_queue(md->queue);
2765
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002766 unlock_fs(md);
2767
2768 clear_bit(DMF_SUSPENDED, &md->flags);
2769
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 dm_table_unplug_all(map);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002771 r = 0;
2772out:
2773 dm_table_put(map);
Daniel Walkere61290a2008-02-08 02:10:08 +00002774 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002775
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002776 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777}
2778
2779/*-----------------------------------------------------------------
2780 * Event notification.
2781 *---------------------------------------------------------------*/
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002782int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
Milan Broz60935eb2009-06-22 10:12:30 +01002783 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002784{
Milan Broz60935eb2009-06-22 10:12:30 +01002785 char udev_cookie[DM_COOKIE_LENGTH];
2786 char *envp[] = { udev_cookie, NULL };
2787
2788 if (!cookie)
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002789 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
Milan Broz60935eb2009-06-22 10:12:30 +01002790 else {
2791 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2792 DM_COOKIE_ENV_VAR_NAME, cookie);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002793 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
2794 action, envp);
Milan Broz60935eb2009-06-22 10:12:30 +01002795 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002796}
2797
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002798uint32_t dm_next_uevent_seq(struct mapped_device *md)
2799{
2800 return atomic_add_return(1, &md->uevent_seq);
2801}
2802
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803uint32_t dm_get_event_nr(struct mapped_device *md)
2804{
2805 return atomic_read(&md->event_nr);
2806}
2807
2808int dm_wait_event(struct mapped_device *md, int event_nr)
2809{
2810 return wait_event_interruptible(md->eventq,
2811 (event_nr != atomic_read(&md->event_nr)));
2812}
2813
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002814void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2815{
2816 unsigned long flags;
2817
2818 spin_lock_irqsave(&md->uevent_lock, flags);
2819 list_add(elist, &md->uevent_list);
2820 spin_unlock_irqrestore(&md->uevent_lock, flags);
2821}
2822
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823/*
2824 * The gendisk is only valid as long as you have a reference
2825 * count on 'md'.
2826 */
2827struct gendisk *dm_disk(struct mapped_device *md)
2828{
2829 return md->disk;
2830}
2831
Milan Broz784aae72009-01-06 03:05:12 +00002832struct kobject *dm_kobject(struct mapped_device *md)
2833{
2834 return &md->kobj;
2835}
2836
2837/*
2838 * struct mapped_device should not be exported outside of dm.c
2839 * so use this check to verify that kobj is part of md structure
2840 */
2841struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2842{
2843 struct mapped_device *md;
2844
2845 md = container_of(kobj, struct mapped_device, kobj);
2846 if (&md->kobj != kobj)
2847 return NULL;
2848
Milan Broz4d89b7b2009-06-22 10:12:11 +01002849 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +00002850 dm_deleting_md(md))
Milan Broz4d89b7b2009-06-22 10:12:11 +01002851 return NULL;
2852
Milan Broz784aae72009-01-06 03:05:12 +00002853 dm_get(md);
2854 return md;
2855}
2856
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002857int dm_suspended_md(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858{
2859 return test_bit(DMF_SUSPENDED, &md->flags);
2860}
2861
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002862int dm_suspended(struct dm_target *ti)
2863{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002864 return dm_suspended_md(dm_table_get_md(ti->table));
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002865}
2866EXPORT_SYMBOL_GPL(dm_suspended);
2867
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002868int dm_noflush_suspending(struct dm_target *ti)
2869{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002870 return __noflush_suspending(dm_table_get_md(ti->table));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002871}
2872EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2873
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002874struct dm_md_mempools *dm_alloc_md_mempools(unsigned type)
2875{
2876 struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL);
2877
2878 if (!pools)
2879 return NULL;
2880
2881 pools->io_pool = (type == DM_TYPE_BIO_BASED) ?
2882 mempool_create_slab_pool(MIN_IOS, _io_cache) :
2883 mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache);
2884 if (!pools->io_pool)
2885 goto free_pools_and_out;
2886
2887 pools->tio_pool = (type == DM_TYPE_BIO_BASED) ?
2888 mempool_create_slab_pool(MIN_IOS, _tio_cache) :
2889 mempool_create_slab_pool(MIN_IOS, _rq_tio_cache);
2890 if (!pools->tio_pool)
2891 goto free_io_pool_and_out;
2892
2893 pools->bs = (type == DM_TYPE_BIO_BASED) ?
2894 bioset_create(16, 0) : bioset_create(MIN_IOS, 0);
2895 if (!pools->bs)
2896 goto free_tio_pool_and_out;
2897
2898 return pools;
2899
2900free_tio_pool_and_out:
2901 mempool_destroy(pools->tio_pool);
2902
2903free_io_pool_and_out:
2904 mempool_destroy(pools->io_pool);
2905
2906free_pools_and_out:
2907 kfree(pools);
2908
2909 return NULL;
2910}
2911
2912void dm_free_md_mempools(struct dm_md_mempools *pools)
2913{
2914 if (!pools)
2915 return;
2916
2917 if (pools->io_pool)
2918 mempool_destroy(pools->io_pool);
2919
2920 if (pools->tio_pool)
2921 mempool_destroy(pools->tio_pool);
2922
2923 if (pools->bs)
2924 bioset_free(pools->bs);
2925
2926 kfree(pools);
2927}
2928
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002929static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 .open = dm_blk_open,
2931 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07002932 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002933 .getgeo = dm_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 .owner = THIS_MODULE
2935};
2936
2937EXPORT_SYMBOL(dm_get_mapinfo);
2938
2939/*
2940 * module hooks
2941 */
2942module_init(dm_init);
2943module_exit(dm_exit);
2944
2945module_param(major, uint, 0);
2946MODULE_PARM_DESC(major, "The major number of the device mapper");
2947MODULE_DESCRIPTION(DM_NAME " driver");
2948MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2949MODULE_LICENSE("GPL");