blob: 5a843c1f4d64c2090b55c584972c334c795d5d40 [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>
18#include <linux/mempool.h>
19#include <linux/slab.h>
20#include <linux/idr.h>
Darrick J. Wong3ac51e72006-03-27 01:17:54 -080021#include <linux/hdreg.h>
Li Zefan55782132009-06-09 13:43:05 +080022
23#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Alasdair G Kergon72d94862006-06-26 00:27:35 -070025#define DM_MSG_PREFIX "core"
26
Milan Broz60935eb2009-06-22 10:12:30 +010027/*
28 * Cookies are numeric values sent with CHANGE and REMOVE
29 * uevents while resuming, removing or renaming the device.
30 */
31#define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
32#define DM_COOKIE_LENGTH 24
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static const char *_name = DM_NAME;
35
36static unsigned int major = 0;
37static unsigned int _major = 0;
38
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -070039static DEFINE_SPINLOCK(_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000041 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 * One of these is allocated per bio.
43 */
44struct dm_io {
45 struct mapped_device *md;
46 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 atomic_t io_count;
Richard Kennedy6ae2fa62008-07-21 12:00:28 +010048 struct bio *bio;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -080049 unsigned long start_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050};
51
52/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000053 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * One of these is allocated per target within a bio. Hopefully
55 * this will be simplified out one day.
56 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +010057struct dm_target_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 struct dm_io *io;
59 struct dm_target *ti;
60 union map_info info;
61};
62
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000063/*
64 * For request-based dm.
65 * One of these is allocated per request.
66 */
67struct dm_rq_target_io {
68 struct mapped_device *md;
69 struct dm_target *ti;
70 struct request *orig, clone;
71 int error;
72 union map_info info;
73};
74
75/*
76 * For request-based dm.
77 * One of these is allocated per bio.
78 */
79struct dm_rq_clone_bio_info {
80 struct bio *orig;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010081 struct dm_rq_target_io *tio;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000082};
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084union map_info *dm_get_mapinfo(struct bio *bio)
85{
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070086 if (bio && bio->bi_private)
Alasdair G Kergon028867a2007-07-12 17:26:32 +010087 return &((struct dm_target_io *)bio->bi_private)->info;
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070088 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010091union map_info *dm_get_rq_mapinfo(struct request *rq)
92{
93 if (rq && rq->end_io_data)
94 return &((struct dm_rq_target_io *)rq->end_io_data)->info;
95 return NULL;
96}
97EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
98
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -070099#define MINOR_ALLOCED ((void *)-1)
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/*
102 * Bits for the md->flags field.
103 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +0100104#define DMF_BLOCK_IO_FOR_SUSPEND 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -0800106#define DMF_FROZEN 2
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700107#define DMF_FREEING 3
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700108#define DMF_DELETING 4
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800109#define DMF_NOFLUSH_SUSPENDING 5
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +0100110#define DMF_QUEUE_IO_TO_THREAD 6
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Milan Broz304f3f62008-02-08 02:11:17 +0000112/*
113 * Work processed by per-device workqueue.
114 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115struct mapped_device {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700116 struct rw_semaphore io_lock;
Daniel Walkere61290a2008-02-08 02:10:08 +0000117 struct mutex suspend_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 rwlock_t map_lock;
119 atomic_t holders;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700120 atomic_t open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 unsigned long flags;
123
Jens Axboe165125e2007-07-24 09:28:11 +0200124 struct request_queue *queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 struct gendisk *disk;
Mike Anderson7e51f252006-03-27 01:17:52 -0800126 char name[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 void *interface_ptr;
129
130 /*
131 * A list of ios that arrived while we were suspended.
132 */
133 atomic_t pending;
134 wait_queue_head_t wait;
Mikulas Patocka53d59142009-04-02 19:55:37 +0100135 struct work_struct work;
Kiyoshi Ueda74859362006-12-08 02:41:02 -0800136 struct bio_list deferred;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100137 spinlock_t deferred_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 /*
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100140 * An error from the barrier request currently being processed.
141 */
142 int barrier_error;
143
144 /*
Milan Broz304f3f62008-02-08 02:11:17 +0000145 * Processing queue (flush/barriers)
146 */
147 struct workqueue_struct *wq;
148
149 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 * The current mapping.
151 */
152 struct dm_table *map;
153
154 /*
155 * io objects are allocated from here.
156 */
157 mempool_t *io_pool;
158 mempool_t *tio_pool;
159
Stefan Bader9faf4002006-10-03 01:15:41 -0700160 struct bio_set *bs;
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 /*
163 * Event handling.
164 */
165 atomic_t event_nr;
166 wait_queue_head_t eventq;
Mike Anderson7a8c3d32007-10-19 22:48:01 +0100167 atomic_t uevent_seq;
168 struct list_head uevent_list;
169 spinlock_t uevent_lock; /* Protect access to uevent_list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 /*
172 * freeze/thaw support require holding onto a super block
173 */
174 struct super_block *frozen_sb;
Mikulas Patockadb8fef42009-06-22 10:12:15 +0100175 struct block_device *bdev;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800176
177 /* forced geometry settings */
178 struct hd_geometry geometry;
Milan Broz784aae72009-01-06 03:05:12 +0000179
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100180 /* marker of flush suspend for request-based dm */
181 struct request suspend_rq;
182
183 /* For saving the address of __make_request for request based dm */
184 make_request_fn *saved_make_request_fn;
185
Milan Broz784aae72009-01-06 03:05:12 +0000186 /* sysfs handle */
187 struct kobject kobj;
Mikulas Patocka52b1fd52009-06-22 10:12:21 +0100188
189 /* zero-length barrier that will be cloned and submitted to targets */
190 struct bio barrier_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191};
192
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100193/*
194 * For mempools pre-allocation at the table loading time.
195 */
196struct dm_md_mempools {
197 mempool_t *io_pool;
198 mempool_t *tio_pool;
199 struct bio_set *bs;
200};
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202#define MIN_IOS 256
Christoph Lametere18b8902006-12-06 20:33:20 -0800203static struct kmem_cache *_io_cache;
204static struct kmem_cache *_tio_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000205static struct kmem_cache *_rq_tio_cache;
206static struct kmem_cache *_rq_bio_info_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208static int __init local_init(void)
209{
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100210 int r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100213 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (!_io_cache)
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100215 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 /* allocate a slab for the target ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100218 _tio_cache = KMEM_CACHE(dm_target_io, 0);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100219 if (!_tio_cache)
220 goto out_free_io_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000222 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
223 if (!_rq_tio_cache)
224 goto out_free_tio_cache;
225
226 _rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0);
227 if (!_rq_bio_info_cache)
228 goto out_free_rq_tio_cache;
229
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100230 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100231 if (r)
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000232 goto out_free_rq_bio_info_cache;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 _major = major;
235 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100236 if (r < 0)
237 goto out_uevent_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 if (!_major)
240 _major = r;
241
242 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100243
244out_uevent_exit:
245 dm_uevent_exit();
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000246out_free_rq_bio_info_cache:
247 kmem_cache_destroy(_rq_bio_info_cache);
248out_free_rq_tio_cache:
249 kmem_cache_destroy(_rq_tio_cache);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100250out_free_tio_cache:
251 kmem_cache_destroy(_tio_cache);
252out_free_io_cache:
253 kmem_cache_destroy(_io_cache);
254
255 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
258static void local_exit(void)
259{
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000260 kmem_cache_destroy(_rq_bio_info_cache);
261 kmem_cache_destroy(_rq_tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 kmem_cache_destroy(_tio_cache);
263 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700264 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100265 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 _major = 0;
268
269 DMINFO("cleaned up");
270}
271
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000272static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 local_init,
274 dm_target_init,
275 dm_linear_init,
276 dm_stripe_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100277 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 dm_interface_init,
279};
280
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000281static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 local_exit,
283 dm_target_exit,
284 dm_linear_exit,
285 dm_stripe_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100286 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 dm_interface_exit,
288};
289
290static int __init dm_init(void)
291{
292 const int count = ARRAY_SIZE(_inits);
293
294 int r, i;
295
296 for (i = 0; i < count; i++) {
297 r = _inits[i]();
298 if (r)
299 goto bad;
300 }
301
302 return 0;
303
304 bad:
305 while (i--)
306 _exits[i]();
307
308 return r;
309}
310
311static void __exit dm_exit(void)
312{
313 int i = ARRAY_SIZE(_exits);
314
315 while (i--)
316 _exits[i]();
317}
318
319/*
320 * Block device functions
321 */
Al Virofe5f9f22008-03-02 10:29:31 -0500322static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 struct mapped_device *md;
325
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700326 spin_lock(&_minor_lock);
327
Al Virofe5f9f22008-03-02 10:29:31 -0500328 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700329 if (!md)
330 goto out;
331
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700332 if (test_bit(DMF_FREEING, &md->flags) ||
333 test_bit(DMF_DELETING, &md->flags)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700334 md = NULL;
335 goto out;
336 }
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700339 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700340
341out:
342 spin_unlock(&_minor_lock);
343
344 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Al Virofe5f9f22008-03-02 10:29:31 -0500347static int dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Al Virofe5f9f22008-03-02 10:29:31 -0500349 struct mapped_device *md = disk->private_data;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700350 atomic_dec(&md->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 dm_put(md);
352 return 0;
353}
354
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700355int dm_open_count(struct mapped_device *md)
356{
357 return atomic_read(&md->open_count);
358}
359
360/*
361 * Guarantees nothing is using the device before it's deleted.
362 */
363int dm_lock_for_deletion(struct mapped_device *md)
364{
365 int r = 0;
366
367 spin_lock(&_minor_lock);
368
369 if (dm_open_count(md))
370 r = -EBUSY;
371 else
372 set_bit(DMF_DELETING, &md->flags);
373
374 spin_unlock(&_minor_lock);
375
376 return r;
377}
378
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800379static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
380{
381 struct mapped_device *md = bdev->bd_disk->private_data;
382
383 return dm_get_geometry(md, geo);
384}
385
Al Virofe5f9f22008-03-02 10:29:31 -0500386static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
Milan Brozaa129a22006-10-03 01:15:15 -0700387 unsigned int cmd, unsigned long arg)
388{
Al Virofe5f9f22008-03-02 10:29:31 -0500389 struct mapped_device *md = bdev->bd_disk->private_data;
390 struct dm_table *map = dm_get_table(md);
Milan Brozaa129a22006-10-03 01:15:15 -0700391 struct dm_target *tgt;
392 int r = -ENOTTY;
393
Milan Brozaa129a22006-10-03 01:15:15 -0700394 if (!map || !dm_table_get_size(map))
395 goto out;
396
397 /* We only support devices that have a single target */
398 if (dm_table_get_num_targets(map) != 1)
399 goto out;
400
401 tgt = dm_table_get_target(map, 0);
402
403 if (dm_suspended(md)) {
404 r = -EAGAIN;
405 goto out;
406 }
407
408 if (tgt->type->ioctl)
Al Viro647b3d02007-08-28 22:15:59 -0400409 r = tgt->type->ioctl(tgt, cmd, arg);
Milan Brozaa129a22006-10-03 01:15:15 -0700410
411out:
412 dm_table_put(map);
413
Milan Brozaa129a22006-10-03 01:15:15 -0700414 return r;
415}
416
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100417static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 return mempool_alloc(md->io_pool, GFP_NOIO);
420}
421
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100422static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 mempool_free(io, md->io_pool);
425}
426
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100427static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 mempool_free(tio, md->tio_pool);
430}
431
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100432static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md)
433{
434 return mempool_alloc(md->tio_pool, GFP_ATOMIC);
435}
436
437static void free_rq_tio(struct dm_rq_target_io *tio)
438{
439 mempool_free(tio, tio->md->tio_pool);
440}
441
442static struct dm_rq_clone_bio_info *alloc_bio_info(struct mapped_device *md)
443{
444 return mempool_alloc(md->io_pool, GFP_ATOMIC);
445}
446
447static void free_bio_info(struct dm_rq_clone_bio_info *info)
448{
449 mempool_free(info, info->tio->md->io_pool);
450}
451
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800452static void start_io_acct(struct dm_io *io)
453{
454 struct mapped_device *md = io->md;
Tejun Heoc9959052008-08-25 19:47:21 +0900455 int cpu;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800456
457 io->start_time = jiffies;
458
Tejun Heo074a7ac2008-08-25 19:56:14 +0900459 cpu = part_stat_lock();
460 part_round_stats(cpu, &dm_disk(md)->part0);
461 part_stat_unlock();
462 dm_disk(md)->part0.in_flight = atomic_inc_return(&md->pending);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800463}
464
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000465static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800466{
467 struct mapped_device *md = io->md;
468 struct bio *bio = io->bio;
469 unsigned long duration = jiffies - io->start_time;
Tejun Heoc9959052008-08-25 19:47:21 +0900470 int pending, cpu;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800471 int rw = bio_data_dir(bio);
472
Tejun Heo074a7ac2008-08-25 19:56:14 +0900473 cpu = part_stat_lock();
474 part_round_stats(cpu, &dm_disk(md)->part0);
475 part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
476 part_stat_unlock();
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800477
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100478 /*
479 * After this is decremented the bio must not be touched if it is
480 * a barrier.
481 */
Tejun Heo074a7ac2008-08-25 19:56:14 +0900482 dm_disk(md)->part0.in_flight = pending =
483 atomic_dec_return(&md->pending);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800484
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000485 /* nudge anyone waiting on suspend queue */
486 if (!pending)
487 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800488}
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490/*
491 * Add the bio to the list of deferred io.
492 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100493static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700495 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Mikulas Patocka022c2612009-04-02 19:55:39 +0100497 spin_lock_irq(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 bio_list_add(&md->deferred, bio);
Mikulas Patocka022c2612009-04-02 19:55:39 +0100499 spin_unlock_irq(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Mikulas Patocka92c63902009-04-09 00:27:15 +0100501 if (!test_and_set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags))
502 queue_work(md->wq, &md->work);
503
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700504 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
507/*
508 * Everyone (including functions in this file), should use this
509 * function to access the md->map field, and make sure they call
510 * dm_table_put() when finished.
511 */
512struct dm_table *dm_get_table(struct mapped_device *md)
513{
514 struct dm_table *t;
515
516 read_lock(&md->map_lock);
517 t = md->map;
518 if (t)
519 dm_table_get(t);
520 read_unlock(&md->map_lock);
521
522 return t;
523}
524
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800525/*
526 * Get the geometry associated with a dm device
527 */
528int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
529{
530 *geo = md->geometry;
531
532 return 0;
533}
534
535/*
536 * Set the geometry of a device.
537 */
538int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
539{
540 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
541
542 if (geo->start > sz) {
543 DMWARN("Start sector is beyond the geometry limits.");
544 return -EINVAL;
545 }
546
547 md->geometry = *geo;
548
549 return 0;
550}
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552/*-----------------------------------------------------------------
553 * CRUD START:
554 * A more elegant soln is in the works that uses the queue
555 * merge fn, unfortunately there are a couple of changes to
556 * the block layer that I want to make for this. So in the
557 * interests of getting something for people to use I give
558 * you this clearly demarcated crap.
559 *---------------------------------------------------------------*/
560
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800561static int __noflush_suspending(struct mapped_device *md)
562{
563 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
564}
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566/*
567 * Decrements the number of outstanding ios that a bio has been
568 * cloned into, completing the original io if necc.
569 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800570static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800572 unsigned long flags;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000573 int io_error;
574 struct bio *bio;
575 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800576
577 /* Push-back supersedes any I/O errors */
Milan Brozb35f8ca2009-03-16 17:44:36 +0000578 if (error && !(io->error > 0 && __noflush_suspending(md)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 io->error = error;
580
581 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800582 if (io->error == DM_ENDIO_REQUEUE) {
583 /*
584 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800585 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100586 spin_lock_irqsave(&md->deferred_lock, flags);
Mikulas Patocka2761e952009-06-22 10:12:18 +0100587 if (__noflush_suspending(md)) {
588 if (!bio_barrier(io->bio))
589 bio_list_add_head(&md->deferred,
590 io->bio);
591 } else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800592 /* noflush suspend was interrupted. */
593 io->error = -EIO;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100594 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800595 }
596
Milan Brozb35f8ca2009-03-16 17:44:36 +0000597 io_error = io->error;
598 bio = io->bio;
Jens Axboe2056a782006-03-23 20:00:26 +0100599
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100600 if (bio_barrier(bio)) {
601 /*
602 * There can be just one barrier request so we use
603 * a per-device variable for error reporting.
604 * Note that you can't touch the bio after end_io_acct
605 */
Mikulas Patockafdb95722009-06-22 10:12:19 +0100606 if (!md->barrier_error && io_error != -EOPNOTSUPP)
Mikulas Patocka5aa27812009-06-22 10:12:18 +0100607 md->barrier_error = io_error;
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100608 end_io_acct(io);
609 } else {
610 end_io_acct(io);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000611
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100612 if (io_error != DM_ENDIO_REQUEUE) {
613 trace_block_bio_complete(md->queue, bio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000614
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100615 bio_endio(bio, io_error);
616 }
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800617 }
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100618
619 free_io(md, io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621}
622
NeilBrown6712ecf2007-09-27 12:47:43 +0200623static void clone_endio(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
625 int r = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100626 struct dm_target_io *tio = bio->bi_private;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000627 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -0700628 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 dm_endio_fn endio = tio->ti->type->end_io;
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
632 error = -EIO;
633
634 if (endio) {
635 r = endio(tio->ti, bio, error, &tio->info);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800636 if (r < 0 || r == DM_ENDIO_REQUEUE)
637 /*
638 * error and requeue request are handled
639 * in dec_pending().
640 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800642 else if (r == DM_ENDIO_INCOMPLETE)
643 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +0200644 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800645 else if (r) {
646 DMWARN("unimplemented target endio return value: %d", r);
647 BUG();
648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650
Stefan Bader9faf4002006-10-03 01:15:41 -0700651 /*
652 * Store md for cleanup instead of tio which is about to get freed.
653 */
654 bio->bi_private = md->bs;
655
Stefan Bader9faf4002006-10-03 01:15:41 -0700656 free_tio(md, tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000657 bio_put(bio);
658 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100661/*
662 * Partial completion handling for request-based dm
663 */
664static void end_clone_bio(struct bio *clone, int error)
665{
666 struct dm_rq_clone_bio_info *info = clone->bi_private;
667 struct dm_rq_target_io *tio = info->tio;
668 struct bio *bio = info->orig;
669 unsigned int nr_bytes = info->orig->bi_size;
670
671 bio_put(clone);
672
673 if (tio->error)
674 /*
675 * An error has already been detected on the request.
676 * Once error occurred, just let clone->end_io() handle
677 * the remainder.
678 */
679 return;
680 else if (error) {
681 /*
682 * Don't notice the error to the upper layer yet.
683 * The error handling decision is made by the target driver,
684 * when the request is completed.
685 */
686 tio->error = error;
687 return;
688 }
689
690 /*
691 * I/O for the bio successfully completed.
692 * Notice the data completion to the upper layer.
693 */
694
695 /*
696 * bios are processed from the head of the list.
697 * So the completing bio should always be rq->bio.
698 * If it's not, something wrong is happening.
699 */
700 if (tio->orig->bio != bio)
701 DMERR("bio completion is going in the middle of the request");
702
703 /*
704 * Update the original request.
705 * Do not use blk_end_request() here, because it may complete
706 * the original request before the clone, and break the ordering.
707 */
708 blk_update_request(tio->orig, 0, nr_bytes);
709}
710
711/*
712 * Don't touch any member of the md after calling this function because
713 * the md may be freed in dm_put() at the end of this function.
714 * Or do dm_get() before calling this function and dm_put() later.
715 */
716static void rq_completed(struct mapped_device *md, int run_queue)
717{
718 int wakeup_waiters = 0;
719 struct request_queue *q = md->queue;
720 unsigned long flags;
721
722 spin_lock_irqsave(q->queue_lock, flags);
723 if (!queue_in_flight(q))
724 wakeup_waiters = 1;
725 spin_unlock_irqrestore(q->queue_lock, flags);
726
727 /* nudge anyone waiting on suspend queue */
728 if (wakeup_waiters)
729 wake_up(&md->wait);
730
731 if (run_queue)
732 blk_run_queue(q);
733
734 /*
735 * dm_put() must be at the end of this function. See the comment above
736 */
737 dm_put(md);
738}
739
740static void dm_unprep_request(struct request *rq)
741{
742 struct request *clone = rq->special;
743 struct dm_rq_target_io *tio = clone->end_io_data;
744
745 rq->special = NULL;
746 rq->cmd_flags &= ~REQ_DONTPREP;
747
748 blk_rq_unprep_clone(clone);
749 free_rq_tio(tio);
750}
751
752/*
753 * Requeue the original request of a clone.
754 */
755void dm_requeue_unmapped_request(struct request *clone)
756{
757 struct dm_rq_target_io *tio = clone->end_io_data;
758 struct mapped_device *md = tio->md;
759 struct request *rq = tio->orig;
760 struct request_queue *q = rq->q;
761 unsigned long flags;
762
763 dm_unprep_request(rq);
764
765 spin_lock_irqsave(q->queue_lock, flags);
766 if (elv_queue_empty(q))
767 blk_plug_device(q);
768 blk_requeue_request(q, rq);
769 spin_unlock_irqrestore(q->queue_lock, flags);
770
771 rq_completed(md, 0);
772}
773EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
774
775static void __stop_queue(struct request_queue *q)
776{
777 blk_stop_queue(q);
778}
779
780static void stop_queue(struct request_queue *q)
781{
782 unsigned long flags;
783
784 spin_lock_irqsave(q->queue_lock, flags);
785 __stop_queue(q);
786 spin_unlock_irqrestore(q->queue_lock, flags);
787}
788
789static void __start_queue(struct request_queue *q)
790{
791 if (blk_queue_stopped(q))
792 blk_start_queue(q);
793}
794
795static void start_queue(struct request_queue *q)
796{
797 unsigned long flags;
798
799 spin_lock_irqsave(q->queue_lock, flags);
800 __start_queue(q);
801 spin_unlock_irqrestore(q->queue_lock, flags);
802}
803
804/*
805 * Complete the clone and the original request.
806 * Must be called without queue lock.
807 */
808static void dm_end_request(struct request *clone, int error)
809{
810 struct dm_rq_target_io *tio = clone->end_io_data;
811 struct mapped_device *md = tio->md;
812 struct request *rq = tio->orig;
813
814 if (blk_pc_request(rq)) {
815 rq->errors = clone->errors;
816 rq->resid_len = clone->resid_len;
817
818 if (rq->sense)
819 /*
820 * We are using the sense buffer of the original
821 * request.
822 * So setting the length of the sense data is enough.
823 */
824 rq->sense_len = clone->sense_len;
825 }
826
827 BUG_ON(clone->bio);
828 free_rq_tio(tio);
829
830 blk_end_request_all(rq, error);
831
832 rq_completed(md, 1);
833}
834
835/*
836 * Request completion handler for request-based dm
837 */
838static void dm_softirq_done(struct request *rq)
839{
840 struct request *clone = rq->completion_data;
841 struct dm_rq_target_io *tio = clone->end_io_data;
842 dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io;
843 int error = tio->error;
844
845 if (!(rq->cmd_flags & REQ_FAILED) && rq_end_io)
846 error = rq_end_io(tio->ti, clone, error, &tio->info);
847
848 if (error <= 0)
849 /* The target wants to complete the I/O */
850 dm_end_request(clone, error);
851 else if (error == DM_ENDIO_INCOMPLETE)
852 /* The target will handle the I/O */
853 return;
854 else if (error == DM_ENDIO_REQUEUE)
855 /* The target wants to requeue the I/O */
856 dm_requeue_unmapped_request(clone);
857 else {
858 DMWARN("unimplemented target endio return value: %d", error);
859 BUG();
860 }
861}
862
863/*
864 * Complete the clone and the original request with the error status
865 * through softirq context.
866 */
867static void dm_complete_request(struct request *clone, int error)
868{
869 struct dm_rq_target_io *tio = clone->end_io_data;
870 struct request *rq = tio->orig;
871
872 tio->error = error;
873 rq->completion_data = clone;
874 blk_complete_request(rq);
875}
876
877/*
878 * Complete the not-mapped clone and the original request with the error status
879 * through softirq context.
880 * Target's rq_end_io() function isn't called.
881 * This may be used when the target's map_rq() function fails.
882 */
883void dm_kill_unmapped_request(struct request *clone, int error)
884{
885 struct dm_rq_target_io *tio = clone->end_io_data;
886 struct request *rq = tio->orig;
887
888 rq->cmd_flags |= REQ_FAILED;
889 dm_complete_request(clone, error);
890}
891EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
892
893/*
894 * Called with the queue lock held
895 */
896static void end_clone_request(struct request *clone, int error)
897{
898 /*
899 * For just cleaning up the information of the queue in which
900 * the clone was dispatched.
901 * The clone is *NOT* freed actually here because it is alloced from
902 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
903 */
904 __blk_put_request(clone->q, clone);
905
906 /*
907 * Actual request completion is done in a softirq context which doesn't
908 * hold the queue lock. Otherwise, deadlock could occur because:
909 * - another request may be submitted by the upper level driver
910 * of the stacking during the completion
911 * - the submission which requires queue lock may be done
912 * against this queue
913 */
914 dm_complete_request(clone, error);
915}
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917static sector_t max_io_len(struct mapped_device *md,
918 sector_t sector, struct dm_target *ti)
919{
920 sector_t offset = sector - ti->begin;
921 sector_t len = ti->len - offset;
922
923 /*
924 * Does the target need to split even further ?
925 */
926 if (ti->split_io) {
927 sector_t boundary;
928 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
929 - offset;
930 if (len > boundary)
931 len = boundary;
932 }
933
934 return len;
935}
936
937static void __map_bio(struct dm_target *ti, struct bio *clone,
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100938 struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
940 int r;
Jens Axboe2056a782006-03-23 20:00:26 +0100941 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -0700942 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 clone->bi_end_io = clone_endio;
945 clone->bi_private = tio;
946
947 /*
948 * Map the clone. If r == 0 we don't need to do
949 * anything, the target has assumed ownership of
950 * this io.
951 */
952 atomic_inc(&tio->io->io_count);
Jens Axboe2056a782006-03-23 20:00:26 +0100953 sector = clone->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 r = ti->type->map(ti, clone, &tio->info);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800955 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +0100957
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100958 trace_block_remap(bdev_get_queue(clone->bi_bdev), clone,
Alan D. Brunelle22a7c312009-05-04 16:35:08 -0400959 tio->io->bio->bi_bdev->bd_dev, sector);
Jens Axboe2056a782006-03-23 20:00:26 +0100960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800962 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
963 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -0700964 md = tio->io->md;
965 dec_pending(tio->io, r);
966 /*
967 * Store bio_set for cleanup.
968 */
969 clone->bi_private = md->bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 bio_put(clone);
Stefan Bader9faf4002006-10-03 01:15:41 -0700971 free_tio(md, tio);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800972 } else if (r) {
973 DMWARN("unimplemented target map return value: %d", r);
974 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
976}
977
978struct clone_info {
979 struct mapped_device *md;
980 struct dm_table *map;
981 struct bio *bio;
982 struct dm_io *io;
983 sector_t sector;
984 sector_t sector_count;
985 unsigned short idx;
986};
987
Peter Osterlund36763472005-09-06 15:16:42 -0700988static void dm_bio_destructor(struct bio *bio)
989{
Stefan Bader9faf4002006-10-03 01:15:41 -0700990 struct bio_set *bs = bio->bi_private;
991
992 bio_free(bio, bs);
Peter Osterlund36763472005-09-06 15:16:42 -0700993}
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995/*
996 * Creates a little bio that is just does part of a bvec.
997 */
998static struct bio *split_bvec(struct bio *bio, sector_t sector,
999 unsigned short idx, unsigned int offset,
Stefan Bader9faf4002006-10-03 01:15:41 -07001000 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
1002 struct bio *clone;
1003 struct bio_vec *bv = bio->bi_io_vec + idx;
1004
Stefan Bader9faf4002006-10-03 01:15:41 -07001005 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
Peter Osterlund36763472005-09-06 15:16:42 -07001006 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 *clone->bi_io_vec = *bv;
1008
1009 clone->bi_sector = sector;
1010 clone->bi_bdev = bio->bi_bdev;
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001011 clone->bi_rw = bio->bi_rw & ~(1 << BIO_RW_BARRIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 clone->bi_vcnt = 1;
1013 clone->bi_size = to_bytes(len);
1014 clone->bi_io_vec->bv_offset = offset;
1015 clone->bi_io_vec->bv_len = clone->bi_size;
Martin K. Petersenf3e1d262008-10-21 17:45:04 +01001016 clone->bi_flags |= 1 << BIO_CLONED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Martin K. Petersen9c470082009-04-09 00:27:12 +01001018 if (bio_integrity(bio)) {
1019 bio_integrity_clone(clone, bio, GFP_NOIO);
1020 bio_integrity_trim(clone,
1021 bio_sector_offset(bio, idx, offset), len);
1022 }
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return clone;
1025}
1026
1027/*
1028 * Creates a bio that consists of range of complete bvecs.
1029 */
1030static struct bio *clone_bio(struct bio *bio, sector_t sector,
1031 unsigned short idx, unsigned short bv_count,
Stefan Bader9faf4002006-10-03 01:15:41 -07001032 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
1034 struct bio *clone;
1035
Stefan Bader9faf4002006-10-03 01:15:41 -07001036 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
1037 __bio_clone(clone, bio);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001038 clone->bi_rw &= ~(1 << BIO_RW_BARRIER);
Stefan Bader9faf4002006-10-03 01:15:41 -07001039 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 clone->bi_sector = sector;
1041 clone->bi_idx = idx;
1042 clone->bi_vcnt = idx + bv_count;
1043 clone->bi_size = to_bytes(len);
1044 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
1045
Martin K. Petersen9c470082009-04-09 00:27:12 +01001046 if (bio_integrity(bio)) {
1047 bio_integrity_clone(clone, bio, GFP_NOIO);
1048
1049 if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
1050 bio_integrity_trim(clone,
1051 bio_sector_offset(bio, idx, 0), len);
1052 }
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return clone;
1055}
1056
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001057static struct dm_target_io *alloc_tio(struct clone_info *ci,
1058 struct dm_target *ti)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001059{
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001060 struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001061
1062 tio->io = ci->io;
1063 tio->ti = ti;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001064 memset(&tio->info, 0, sizeof(tio->info));
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001065
1066 return tio;
1067}
1068
1069static void __flush_target(struct clone_info *ci, struct dm_target *ti,
1070 unsigned flush_nr)
1071{
1072 struct dm_target_io *tio = alloc_tio(ci, ti);
1073 struct bio *clone;
1074
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001075 tio->info.flush_request = flush_nr;
1076
1077 clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
1078 __bio_clone(clone, ci->bio);
1079 clone->bi_destructor = dm_bio_destructor;
1080
1081 __map_bio(ti, clone, tio);
1082}
1083
1084static int __clone_and_map_empty_barrier(struct clone_info *ci)
1085{
1086 unsigned target_nr = 0, flush_nr;
1087 struct dm_target *ti;
1088
1089 while ((ti = dm_table_get_target(ci->map, target_nr++)))
1090 for (flush_nr = 0; flush_nr < ti->num_flush_requests;
1091 flush_nr++)
1092 __flush_target(ci, ti, flush_nr);
1093
1094 ci->sector_count = 0;
1095
1096 return 0;
1097}
1098
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001099static int __clone_and_map(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
1101 struct bio *clone, *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001102 struct dm_target *ti;
1103 sector_t len = 0, max;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001104 struct dm_target_io *tio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001106 if (unlikely(bio_empty_barrier(bio)))
1107 return __clone_and_map_empty_barrier(ci);
1108
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001109 ti = dm_table_find_target(ci->map, ci->sector);
1110 if (!dm_target_is_valid(ti))
1111 return -EIO;
1112
1113 max = max_io_len(ci->md, ci->sector, ti);
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 /*
1116 * Allocate a target io object.
1117 */
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001118 tio = alloc_tio(ci, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 if (ci->sector_count <= max) {
1121 /*
1122 * Optimise for the simple case where we can do all of
1123 * the remaining io with a single clone.
1124 */
1125 clone = clone_bio(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -07001126 bio->bi_vcnt - ci->idx, ci->sector_count,
1127 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 __map_bio(ti, clone, tio);
1129 ci->sector_count = 0;
1130
1131 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
1132 /*
1133 * There are some bvecs that don't span targets.
1134 * Do as many of these as possible.
1135 */
1136 int i;
1137 sector_t remaining = max;
1138 sector_t bv_len;
1139
1140 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
1141 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
1142
1143 if (bv_len > remaining)
1144 break;
1145
1146 remaining -= bv_len;
1147 len += bv_len;
1148 }
1149
Stefan Bader9faf4002006-10-03 01:15:41 -07001150 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
1151 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 __map_bio(ti, clone, tio);
1153
1154 ci->sector += len;
1155 ci->sector_count -= len;
1156 ci->idx = i;
1157
1158 } else {
1159 /*
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001160 * Handle a bvec that must be split between two or more targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 */
1162 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001163 sector_t remaining = to_sector(bv->bv_len);
1164 unsigned int offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001166 do {
1167 if (offset) {
1168 ti = dm_table_find_target(ci->map, ci->sector);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001169 if (!dm_target_is_valid(ti))
1170 return -EIO;
1171
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001172 max = max_io_len(ci->md, ci->sector, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001174 tio = alloc_tio(ci, ti);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001177 len = min(remaining, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001179 clone = split_bvec(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -07001180 bv->bv_offset + offset, len,
1181 ci->md->bs);
Alasdair G Kergond2044a92006-03-22 00:07:42 -08001182
1183 __map_bio(ti, clone, tio);
1184
1185 ci->sector += len;
1186 ci->sector_count -= len;
1187 offset += to_bytes(len);
1188 } while (remaining -= len);
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 ci->idx++;
1191 }
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001192
1193 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
1196/*
Mikulas Patocka8a53c282009-04-02 19:55:37 +01001197 * Split the bio into several clones and submit it to targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 */
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001199static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
1201 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001202 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 ci.map = dm_get_table(md);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001205 if (unlikely(!ci.map)) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001206 if (!bio_barrier(bio))
1207 bio_io_error(bio);
1208 else
Mikulas Patocka5aa27812009-06-22 10:12:18 +01001209 if (!md->barrier_error)
1210 md->barrier_error = -EIO;
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001211 return;
1212 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 ci.md = md;
1215 ci.bio = bio;
1216 ci.io = alloc_io(md);
1217 ci.io->error = 0;
1218 atomic_set(&ci.io->io_count, 1);
1219 ci.io->bio = bio;
1220 ci.io->md = md;
1221 ci.sector = bio->bi_sector;
1222 ci.sector_count = bio_sectors(bio);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001223 if (unlikely(bio_empty_barrier(bio)))
1224 ci.sector_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 ci.idx = bio->bi_idx;
1226
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001227 start_io_acct(ci.io);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001228 while (ci.sector_count && !error)
1229 error = __clone_and_map(&ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001232 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 dm_table_put(ci.map);
1234}
1235/*-----------------------------------------------------------------
1236 * CRUD END
1237 *---------------------------------------------------------------*/
1238
Milan Brozf6fccb12008-07-21 12:00:37 +01001239static int dm_merge_bvec(struct request_queue *q,
1240 struct bvec_merge_data *bvm,
1241 struct bio_vec *biovec)
1242{
1243 struct mapped_device *md = q->queuedata;
1244 struct dm_table *map = dm_get_table(md);
1245 struct dm_target *ti;
1246 sector_t max_sectors;
Mikulas Patocka50371082008-10-01 14:39:17 +01001247 int max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001248
1249 if (unlikely(!map))
Mikulas Patocka50371082008-10-01 14:39:17 +01001250 goto out;
Milan Brozf6fccb12008-07-21 12:00:37 +01001251
1252 ti = dm_table_find_target(map, bvm->bi_sector);
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001253 if (!dm_target_is_valid(ti))
1254 goto out_table;
Milan Brozf6fccb12008-07-21 12:00:37 +01001255
1256 /*
1257 * Find maximum amount of I/O that won't need splitting
1258 */
1259 max_sectors = min(max_io_len(md, bvm->bi_sector, ti),
1260 (sector_t) BIO_MAX_SECTORS);
1261 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1262 if (max_size < 0)
1263 max_size = 0;
1264
1265 /*
1266 * merge_bvec_fn() returns number of bytes
1267 * it can accept at this offset
1268 * max is precomputed maximal io size
1269 */
1270 if (max_size && ti->type->merge)
1271 max_size = ti->type->merge(ti, bvm, biovec, max_size);
Mikulas Patocka8cbeb672009-06-22 10:12:14 +01001272 /*
1273 * If the target doesn't support merge method and some of the devices
1274 * provided their merge_bvec method (we know this by looking at
1275 * queue_max_hw_sectors), then we can't allow bios with multiple vector
1276 * entries. So always set max_size to 0, and the code below allows
1277 * just one page.
1278 */
1279 else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
1280
1281 max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001282
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001283out_table:
Mikulas Patocka50371082008-10-01 14:39:17 +01001284 dm_table_put(map);
1285
1286out:
Milan Brozf6fccb12008-07-21 12:00:37 +01001287 /*
1288 * Always allow an entire first page
1289 */
1290 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1291 max_size = biovec->bv_len;
1292
Milan Brozf6fccb12008-07-21 12:00:37 +01001293 return max_size;
1294}
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296/*
1297 * The request function that just remaps the bio built up by
1298 * dm_merge_bvec.
1299 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001300static int _dm_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Kevin Corry12f03a42006-02-01 03:04:52 -08001302 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 struct mapped_device *md = q->queuedata;
Tejun Heoc9959052008-08-25 19:47:21 +09001304 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001306 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Tejun Heo074a7ac2008-08-25 19:56:14 +09001308 cpu = part_stat_lock();
1309 part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1310 part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1311 part_stat_unlock();
Kevin Corry12f03a42006-02-01 03:04:52 -08001312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 /*
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001314 * If we're suspended or the thread is processing barriers
1315 * we have to queue this io for later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 */
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01001317 if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) ||
1318 unlikely(bio_barrier(bio))) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001319 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001321 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) &&
1322 bio_rw(bio) == READA) {
1323 bio_io_error(bio);
1324 return 0;
1325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Mikulas Patocka92c63902009-04-09 00:27:15 +01001327 queue_io(md, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Mikulas Patocka92c63902009-04-09 00:27:15 +01001329 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
1331
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001332 __split_and_process_bio(md, bio);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001333 up_read(&md->io_lock);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001334 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001337static int dm_make_request(struct request_queue *q, struct bio *bio)
1338{
1339 struct mapped_device *md = q->queuedata;
1340
1341 if (unlikely(bio_barrier(bio))) {
1342 bio_endio(bio, -EOPNOTSUPP);
1343 return 0;
1344 }
1345
1346 return md->saved_make_request_fn(q, bio); /* call __make_request() */
1347}
1348
1349static int dm_request_based(struct mapped_device *md)
1350{
1351 return blk_queue_stackable(md->queue);
1352}
1353
1354static int dm_request(struct request_queue *q, struct bio *bio)
1355{
1356 struct mapped_device *md = q->queuedata;
1357
1358 if (dm_request_based(md))
1359 return dm_make_request(q, bio);
1360
1361 return _dm_request(q, bio);
1362}
1363
1364void dm_dispatch_request(struct request *rq)
1365{
1366 int r;
1367
1368 if (blk_queue_io_stat(rq->q))
1369 rq->cmd_flags |= REQ_IO_STAT;
1370
1371 rq->start_time = jiffies;
1372 r = blk_insert_cloned_request(rq->q, rq);
1373 if (r)
1374 dm_complete_request(rq, r);
1375}
1376EXPORT_SYMBOL_GPL(dm_dispatch_request);
1377
1378static void dm_rq_bio_destructor(struct bio *bio)
1379{
1380 struct dm_rq_clone_bio_info *info = bio->bi_private;
1381 struct mapped_device *md = info->tio->md;
1382
1383 free_bio_info(info);
1384 bio_free(bio, md->bs);
1385}
1386
1387static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1388 void *data)
1389{
1390 struct dm_rq_target_io *tio = data;
1391 struct mapped_device *md = tio->md;
1392 struct dm_rq_clone_bio_info *info = alloc_bio_info(md);
1393
1394 if (!info)
1395 return -ENOMEM;
1396
1397 info->orig = bio_orig;
1398 info->tio = tio;
1399 bio->bi_end_io = end_clone_bio;
1400 bio->bi_private = info;
1401 bio->bi_destructor = dm_rq_bio_destructor;
1402
1403 return 0;
1404}
1405
1406static int setup_clone(struct request *clone, struct request *rq,
1407 struct dm_rq_target_io *tio)
1408{
1409 int r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1410 dm_rq_bio_constructor, tio);
1411
1412 if (r)
1413 return r;
1414
1415 clone->cmd = rq->cmd;
1416 clone->cmd_len = rq->cmd_len;
1417 clone->sense = rq->sense;
1418 clone->buffer = rq->buffer;
1419 clone->end_io = end_clone_request;
1420 clone->end_io_data = tio;
1421
1422 return 0;
1423}
1424
1425static int dm_rq_flush_suspending(struct mapped_device *md)
1426{
1427 return !md->suspend_rq.special;
1428}
1429
1430/*
1431 * Called with the queue lock held.
1432 */
1433static int dm_prep_fn(struct request_queue *q, struct request *rq)
1434{
1435 struct mapped_device *md = q->queuedata;
1436 struct dm_rq_target_io *tio;
1437 struct request *clone;
1438
1439 if (unlikely(rq == &md->suspend_rq)) {
1440 if (dm_rq_flush_suspending(md))
1441 return BLKPREP_OK;
1442 else
1443 /* The flush suspend was interrupted */
1444 return BLKPREP_KILL;
1445 }
1446
1447 if (unlikely(rq->special)) {
1448 DMWARN("Already has something in rq->special.");
1449 return BLKPREP_KILL;
1450 }
1451
1452 tio = alloc_rq_tio(md); /* Only one for each original request */
1453 if (!tio)
1454 /* -ENOMEM */
1455 return BLKPREP_DEFER;
1456
1457 tio->md = md;
1458 tio->ti = NULL;
1459 tio->orig = rq;
1460 tio->error = 0;
1461 memset(&tio->info, 0, sizeof(tio->info));
1462
1463 clone = &tio->clone;
1464 if (setup_clone(clone, rq, tio)) {
1465 /* -ENOMEM */
1466 free_rq_tio(tio);
1467 return BLKPREP_DEFER;
1468 }
1469
1470 rq->special = clone;
1471 rq->cmd_flags |= REQ_DONTPREP;
1472
1473 return BLKPREP_OK;
1474}
1475
1476static void map_request(struct dm_target *ti, struct request *rq,
1477 struct mapped_device *md)
1478{
1479 int r;
1480 struct request *clone = rq->special;
1481 struct dm_rq_target_io *tio = clone->end_io_data;
1482
1483 /*
1484 * Hold the md reference here for the in-flight I/O.
1485 * We can't rely on the reference count by device opener,
1486 * because the device may be closed during the request completion
1487 * when all bios are completed.
1488 * See the comment in rq_completed() too.
1489 */
1490 dm_get(md);
1491
1492 tio->ti = ti;
1493 r = ti->type->map_rq(ti, clone, &tio->info);
1494 switch (r) {
1495 case DM_MAPIO_SUBMITTED:
1496 /* The target has taken the I/O to submit by itself later */
1497 break;
1498 case DM_MAPIO_REMAPPED:
1499 /* The target has remapped the I/O so dispatch it */
1500 dm_dispatch_request(clone);
1501 break;
1502 case DM_MAPIO_REQUEUE:
1503 /* The target wants to requeue the I/O */
1504 dm_requeue_unmapped_request(clone);
1505 break;
1506 default:
1507 if (r > 0) {
1508 DMWARN("unimplemented target map return value: %d", r);
1509 BUG();
1510 }
1511
1512 /* The target wants to complete the I/O */
1513 dm_kill_unmapped_request(clone, r);
1514 break;
1515 }
1516}
1517
1518/*
1519 * q->request_fn for request-based dm.
1520 * Called with the queue lock held.
1521 */
1522static void dm_request_fn(struct request_queue *q)
1523{
1524 struct mapped_device *md = q->queuedata;
1525 struct dm_table *map = dm_get_table(md);
1526 struct dm_target *ti;
1527 struct request *rq;
1528
1529 /*
1530 * For noflush suspend, check blk_queue_stopped() to immediately
1531 * quit I/O dispatching.
1532 */
1533 while (!blk_queue_plugged(q) && !blk_queue_stopped(q)) {
1534 rq = blk_peek_request(q);
1535 if (!rq)
1536 goto plug_and_out;
1537
1538 if (unlikely(rq == &md->suspend_rq)) { /* Flush suspend maker */
1539 if (queue_in_flight(q))
1540 /* Not quiet yet. Wait more */
1541 goto plug_and_out;
1542
1543 /* This device should be quiet now */
1544 __stop_queue(q);
1545 blk_start_request(rq);
1546 __blk_end_request_all(rq, 0);
1547 wake_up(&md->wait);
1548 goto out;
1549 }
1550
1551 ti = dm_table_find_target(map, blk_rq_pos(rq));
1552 if (ti->type->busy && ti->type->busy(ti))
1553 goto plug_and_out;
1554
1555 blk_start_request(rq);
1556 spin_unlock(q->queue_lock);
1557 map_request(ti, rq, md);
1558 spin_lock_irq(q->queue_lock);
1559 }
1560
1561 goto out;
1562
1563plug_and_out:
1564 if (!elv_queue_empty(q))
1565 /* Some requests still remain, retry later */
1566 blk_plug_device(q);
1567
1568out:
1569 dm_table_put(map);
1570
1571 return;
1572}
1573
1574int dm_underlying_device_busy(struct request_queue *q)
1575{
1576 return blk_lld_busy(q);
1577}
1578EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1579
1580static int dm_lld_busy(struct request_queue *q)
1581{
1582 int r;
1583 struct mapped_device *md = q->queuedata;
1584 struct dm_table *map = dm_get_table(md);
1585
1586 if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1587 r = 1;
1588 else
1589 r = dm_table_any_busy_target(map);
1590
1591 dm_table_put(map);
1592
1593 return r;
1594}
1595
Jens Axboe165125e2007-07-24 09:28:11 +02001596static void dm_unplug_all(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
1598 struct mapped_device *md = q->queuedata;
1599 struct dm_table *map = dm_get_table(md);
1600
1601 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001602 if (dm_request_based(md))
1603 generic_unplug_device(q);
1604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 dm_table_unplug_all(map);
1606 dm_table_put(map);
1607 }
1608}
1609
1610static int dm_any_congested(void *congested_data, int bdi_bits)
1611{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001612 int r = bdi_bits;
1613 struct mapped_device *md = congested_data;
1614 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001616 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001617 map = dm_get_table(md);
1618 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001619 /*
1620 * Request-based dm cares about only own queue for
1621 * the query about congestion status of request_queue
1622 */
1623 if (dm_request_based(md))
1624 r = md->queue->backing_dev_info.state &
1625 bdi_bits;
1626 else
1627 r = dm_table_any_congested(map, bdi_bits);
1628
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001629 dm_table_put(map);
1630 }
1631 }
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 return r;
1634}
1635
1636/*-----------------------------------------------------------------
1637 * An IDR is used to keep track of allocated minor numbers.
1638 *---------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639static DEFINE_IDR(_minor_idr);
1640
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001641static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001643 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001645 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646}
1647
1648/*
1649 * See if the device with a specific minor # is free.
1650 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001651static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652{
1653 int r, m;
1654
1655 if (minor >= (1 << MINORBITS))
1656 return -EINVAL;
1657
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001658 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
1659 if (!r)
1660 return -ENOMEM;
1661
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001662 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 if (idr_find(&_minor_idr, minor)) {
1665 r = -EBUSY;
1666 goto out;
1667 }
1668
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001669 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001670 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673 if (m != minor) {
1674 idr_remove(&_minor_idr, m);
1675 r = -EBUSY;
1676 goto out;
1677 }
1678
1679out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001680 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 return r;
1682}
1683
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001684static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001686 int r, m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
Jeff Mahoney62f75c22006-06-26 00:27:21 -07001689 if (!r)
1690 return -ENOMEM;
1691
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001692 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001694 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001695 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
1698 if (m >= (1 << MINORBITS)) {
1699 idr_remove(&_minor_idr, m);
1700 r = -ENOSPC;
1701 goto out;
1702 }
1703
1704 *minor = m;
1705
1706out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001707 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 return r;
1709}
1710
1711static struct block_device_operations dm_blk_dops;
1712
Mikulas Patocka53d59142009-04-02 19:55:37 +01001713static void dm_wq_work(struct work_struct *work);
1714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715/*
1716 * Allocate and initialise a blank device with a given minor.
1717 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001718static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
1720 int r;
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001721 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001722 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 if (!md) {
1725 DMWARN("unable to allocate device, out of memory.");
1726 return NULL;
1727 }
1728
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001729 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00001730 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001733 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001734 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001735 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001736 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001738 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001740 init_rwsem(&md->io_lock);
Daniel Walkere61290a2008-02-08 02:10:08 +00001741 mutex_init(&md->suspend_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01001742 spin_lock_init(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 rwlock_init(&md->map_lock);
1744 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07001745 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001747 atomic_set(&md->uevent_seq, 0);
1748 INIT_LIST_HEAD(&md->uevent_list);
1749 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001751 md->queue = blk_init_queue(dm_request_fn, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 if (!md->queue)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001753 goto bad_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001755 /*
1756 * Request-based dm devices cannot be stacked on top of bio-based dm
1757 * devices. The type of this dm device has not been decided yet,
1758 * although we initialized the queue using blk_init_queue().
1759 * The type is decided at the first table loading time.
1760 * To prevent problematic device stacking, clear the queue flag
1761 * for request stacking support until then.
1762 *
1763 * This queue is new, so no concurrency on the queue_flags.
1764 */
1765 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1766 md->saved_make_request_fn = md->queue->make_request_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 md->queue->queuedata = md;
1768 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1769 md->queue->backing_dev_info.congested_data = md;
1770 blk_queue_make_request(md->queue, dm_request);
Mikulas Patocka99360b42009-04-02 19:55:39 +01001771 blk_queue_ordered(md->queue, QUEUE_ORDERED_DRAIN, NULL);
Jens Axboedaef2652006-01-10 10:48:02 +01001772 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 md->queue->unplug_fn = dm_unplug_all;
Milan Brozf6fccb12008-07-21 12:00:37 +01001774 blk_queue_merge_bvec(md->queue, dm_merge_bvec);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001775 blk_queue_softirq_done(md->queue, dm_softirq_done);
1776 blk_queue_prep_rq(md->queue, dm_prep_fn);
1777 blk_queue_lld_busy(md->queue, dm_lld_busy);
Stefan Bader9faf4002006-10-03 01:15:41 -07001778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 md->disk = alloc_disk(1);
1780 if (!md->disk)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001781 goto bad_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001783 atomic_set(&md->pending, 0);
1784 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01001785 INIT_WORK(&md->work, dm_wq_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001786 init_waitqueue_head(&md->eventq);
1787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 md->disk->major = _major;
1789 md->disk->first_minor = minor;
1790 md->disk->fops = &dm_blk_dops;
1791 md->disk->queue = md->queue;
1792 md->disk->private_data = md;
1793 sprintf(md->disk->disk_name, "dm-%d", minor);
1794 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08001795 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Milan Broz304f3f62008-02-08 02:11:17 +00001797 md->wq = create_singlethread_workqueue("kdmflush");
1798 if (!md->wq)
1799 goto bad_thread;
1800
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001801 md->bdev = bdget_disk(md->disk, 0);
1802 if (!md->bdev)
1803 goto bad_bdev;
1804
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001805 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001806 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001807 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001808 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001809
1810 BUG_ON(old_md != MINOR_ALLOCED);
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return md;
1813
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001814bad_bdev:
1815 destroy_workqueue(md->wq);
Milan Broz304f3f62008-02-08 02:11:17 +00001816bad_thread:
1817 put_disk(md->disk);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001818bad_disk:
Al Viro1312f402006-03-12 11:02:03 -05001819 blk_cleanup_queue(md->queue);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001820bad_queue:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001822bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001823 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00001824bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 kfree(md);
1826 return NULL;
1827}
1828
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01001829static void unlock_fs(struct mapped_device *md);
1830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831static void free_dev(struct mapped_device *md)
1832{
Tejun Heof331c022008-09-03 09:01:48 +02001833 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001834
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001835 unlock_fs(md);
1836 bdput(md->bdev);
Milan Broz304f3f62008-02-08 02:11:17 +00001837 destroy_workqueue(md->wq);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001838 if (md->tio_pool)
1839 mempool_destroy(md->tio_pool);
1840 if (md->io_pool)
1841 mempool_destroy(md->io_pool);
1842 if (md->bs)
1843 bioset_free(md->bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01001844 blk_integrity_unregister(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 del_gendisk(md->disk);
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001846 free_minor(minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001847
1848 spin_lock(&_minor_lock);
1849 md->disk->private_data = NULL;
1850 spin_unlock(&_minor_lock);
1851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 put_disk(md->disk);
Al Viro1312f402006-03-12 11:02:03 -05001853 blk_cleanup_queue(md->queue);
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001854 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 kfree(md);
1856}
1857
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001858static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1859{
1860 struct dm_md_mempools *p;
1861
1862 if (md->io_pool && md->tio_pool && md->bs)
1863 /* the md already has necessary mempools */
1864 goto out;
1865
1866 p = dm_table_get_md_mempools(t);
1867 BUG_ON(!p || md->io_pool || md->tio_pool || md->bs);
1868
1869 md->io_pool = p->io_pool;
1870 p->io_pool = NULL;
1871 md->tio_pool = p->tio_pool;
1872 p->tio_pool = NULL;
1873 md->bs = p->bs;
1874 p->bs = NULL;
1875
1876out:
1877 /* mempool bind completed, now no need any mempools in the table */
1878 dm_table_free_md_mempools(t);
1879}
1880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881/*
1882 * Bind a table to the device.
1883 */
1884static void event_callback(void *context)
1885{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001886 unsigned long flags;
1887 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 struct mapped_device *md = (struct mapped_device *) context;
1889
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001890 spin_lock_irqsave(&md->uevent_lock, flags);
1891 list_splice_init(&md->uevent_list, &uevents);
1892 spin_unlock_irqrestore(&md->uevent_lock, flags);
1893
Tejun Heoed9e1982008-08-25 19:56:05 +09001894 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 atomic_inc(&md->event_nr);
1897 wake_up(&md->eventq);
1898}
1899
Alasdair G Kergon4e901882005-07-28 21:15:59 -07001900static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
Alasdair G Kergon4e901882005-07-28 21:15:59 -07001902 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Mikulas Patockadb8fef42009-06-22 10:12:15 +01001904 mutex_lock(&md->bdev->bd_inode->i_mutex);
1905 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
1906 mutex_unlock(&md->bdev->bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907}
1908
Mike Snitzer754c5fc2009-06-22 10:12:34 +01001909static int __bind(struct mapped_device *md, struct dm_table *t,
1910 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911{
Jens Axboe165125e2007-07-24 09:28:11 +02001912 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 sector_t size;
1914
1915 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001916
1917 /*
1918 * Wipe any geometry if the size of the table changed.
1919 */
1920 if (size != get_capacity(md->disk))
1921 memset(&md->geometry, 0, sizeof(md->geometry));
1922
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001923 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Mikulas Patockad5816872009-01-06 03:05:10 +00001925 if (!size) {
1926 dm_table_destroy(t);
1927 return 0;
1928 }
1929
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001930 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001931
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001932 /*
1933 * The queue hasn't been stopped yet, if the old table type wasn't
1934 * for request-based during suspension. So stop it to prevent
1935 * I/O mapping before resume.
1936 * This must be done before setting the queue restrictions,
1937 * because request-based dm may be run just after the setting.
1938 */
1939 if (dm_table_request_based(t) && !blk_queue_stopped(q))
1940 stop_queue(q);
1941
1942 __bind_mempools(md, t);
1943
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001944 write_lock(&md->map_lock);
1945 md->map = t;
Mike Snitzer754c5fc2009-06-22 10:12:34 +01001946 dm_table_set_restrictions(t, q, limits);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001947 write_unlock(&md->map_lock);
1948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 return 0;
1950}
1951
1952static void __unbind(struct mapped_device *md)
1953{
1954 struct dm_table *map = md->map;
1955
1956 if (!map)
1957 return;
1958
1959 dm_table_event_callback(map, NULL, NULL);
1960 write_lock(&md->map_lock);
1961 md->map = NULL;
1962 write_unlock(&md->map_lock);
Mikulas Patockad5816872009-01-06 03:05:10 +00001963 dm_table_destroy(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964}
1965
1966/*
1967 * Constructor for a new device.
1968 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001969int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
1971 struct mapped_device *md;
1972
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001973 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 if (!md)
1975 return -ENXIO;
1976
Milan Broz784aae72009-01-06 03:05:12 +00001977 dm_sysfs_init(md);
1978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 *result = md;
1980 return 0;
1981}
1982
David Teigland637842c2006-01-06 00:20:00 -08001983static struct mapped_device *dm_find_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
1985 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 unsigned minor = MINOR(dev);
1987
1988 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
1989 return NULL;
1990
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001991 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
1993 md = idr_find(&_minor_idr, minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001994 if (md && (md == MINOR_ALLOCED ||
Tejun Heof331c022008-09-03 09:01:48 +02001995 (MINOR(disk_devt(dm_disk(md))) != minor) ||
Alasdair G Kergon17b2f662006-06-26 00:27:33 -07001996 test_bit(DMF_FREEING, &md->flags))) {
David Teigland637842c2006-01-06 00:20:00 -08001997 md = NULL;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001998 goto out;
1999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002001out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002002 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
David Teigland637842c2006-01-06 00:20:00 -08002004 return md;
2005}
2006
David Teiglandd229a952006-01-06 00:20:01 -08002007struct mapped_device *dm_get_md(dev_t dev)
2008{
2009 struct mapped_device *md = dm_find_md(dev);
2010
2011 if (md)
2012 dm_get(md);
2013
2014 return md;
2015}
2016
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002017void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002018{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002019 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020}
2021
2022void dm_set_mdptr(struct mapped_device *md, void *ptr)
2023{
2024 md->interface_ptr = ptr;
2025}
2026
2027void dm_get(struct mapped_device *md)
2028{
2029 atomic_inc(&md->holders);
2030}
2031
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002032const char *dm_device_name(struct mapped_device *md)
2033{
2034 return md->name;
2035}
2036EXPORT_SYMBOL_GPL(dm_device_name);
2037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038void dm_put(struct mapped_device *md)
2039{
Mike Anderson1134e5a2006-03-27 01:17:54 -08002040 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002042 BUG_ON(test_bit(DMF_FREEING, &md->flags));
2043
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002044 if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
Mike Anderson1134e5a2006-03-27 01:17:54 -08002045 map = dm_get_table(md);
Tejun Heof331c022008-09-03 09:01:48 +02002046 idr_replace(&_minor_idr, MINOR_ALLOCED,
2047 MINOR(disk_devt(dm_disk(md))));
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002048 set_bit(DMF_FREEING, &md->flags);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002049 spin_unlock(&_minor_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002050 if (!dm_suspended(md)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 dm_table_presuspend_targets(map);
2052 dm_table_postsuspend_targets(map);
2053 }
Milan Broz784aae72009-01-06 03:05:12 +00002054 dm_sysfs_exit(md);
Mike Anderson1134e5a2006-03-27 01:17:54 -08002055 dm_table_put(map);
Mikulas Patockaa1b51e92009-01-06 03:04:53 +00002056 __unbind(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 free_dev(md);
2058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059}
Edward Goggin79eb8852007-05-09 02:32:56 -07002060EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Mikulas Patocka401600d2009-04-02 19:55:38 +01002062static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
Milan Broz46125c12008-02-08 02:10:30 +00002063{
2064 int r = 0;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002065 DECLARE_WAITQUEUE(wait, current);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002066 struct request_queue *q = md->queue;
2067 unsigned long flags;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002068
2069 dm_unplug_all(md->queue);
2070
2071 add_wait_queue(&md->wait, &wait);
Milan Broz46125c12008-02-08 02:10:30 +00002072
2073 while (1) {
Mikulas Patocka401600d2009-04-02 19:55:38 +01002074 set_current_state(interruptible);
Milan Broz46125c12008-02-08 02:10:30 +00002075
2076 smp_mb();
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002077 if (dm_request_based(md)) {
2078 spin_lock_irqsave(q->queue_lock, flags);
2079 if (!queue_in_flight(q) && blk_queue_stopped(q)) {
2080 spin_unlock_irqrestore(q->queue_lock, flags);
2081 break;
2082 }
2083 spin_unlock_irqrestore(q->queue_lock, flags);
2084 } else if (!atomic_read(&md->pending))
Milan Broz46125c12008-02-08 02:10:30 +00002085 break;
2086
Mikulas Patocka401600d2009-04-02 19:55:38 +01002087 if (interruptible == TASK_INTERRUPTIBLE &&
2088 signal_pending(current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002089 r = -EINTR;
2090 break;
2091 }
2092
2093 io_schedule();
2094 }
2095 set_current_state(TASK_RUNNING);
2096
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002097 remove_wait_queue(&md->wait, &wait);
2098
Milan Broz46125c12008-02-08 02:10:30 +00002099 return r;
2100}
2101
Mikulas Patocka531fe962009-06-22 10:12:17 +01002102static void dm_flush(struct mapped_device *md)
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002103{
2104 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
Mikulas Patocka52b1fd52009-06-22 10:12:21 +01002105
2106 bio_init(&md->barrier_bio);
2107 md->barrier_bio.bi_bdev = md->bdev;
2108 md->barrier_bio.bi_rw = WRITE_BARRIER;
2109 __split_and_process_bio(md, &md->barrier_bio);
2110
2111 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002112}
2113
2114static void process_barrier(struct mapped_device *md, struct bio *bio)
2115{
Mikulas Patocka5aa27812009-06-22 10:12:18 +01002116 md->barrier_error = 0;
2117
Mikulas Patocka531fe962009-06-22 10:12:17 +01002118 dm_flush(md);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002119
Mikulas Patocka5aa27812009-06-22 10:12:18 +01002120 if (!bio_empty_barrier(bio)) {
2121 __split_and_process_bio(md, bio);
2122 dm_flush(md);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002123 }
2124
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002125 if (md->barrier_error != DM_ENDIO_REQUEUE)
Mikulas Patocka531fe962009-06-22 10:12:17 +01002126 bio_endio(bio, md->barrier_error);
Mikulas Patocka2761e952009-06-22 10:12:18 +01002127 else {
2128 spin_lock_irq(&md->deferred_lock);
2129 bio_list_add_head(&md->deferred, bio);
2130 spin_unlock_irq(&md->deferred_lock);
2131 }
Mikulas Patockaaf7e4662009-04-09 00:27:16 +01002132}
2133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134/*
2135 * Process the deferred bios
2136 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002137static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
Mikulas Patockaef208582009-04-02 19:55:38 +01002139 struct mapped_device *md = container_of(work, struct mapped_device,
2140 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002141 struct bio *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Mikulas Patockaef208582009-04-02 19:55:38 +01002143 down_write(&md->io_lock);
2144
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002145 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002146 spin_lock_irq(&md->deferred_lock);
2147 c = bio_list_pop(&md->deferred);
2148 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002149
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002150 if (!c) {
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002151 clear_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002152 break;
2153 }
2154
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002155 up_write(&md->io_lock);
2156
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002157 if (dm_request_based(md))
2158 generic_make_request(c);
2159 else {
2160 if (bio_barrier(c))
2161 process_barrier(md, c);
2162 else
2163 __split_and_process_bio(md, c);
2164 }
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002165
2166 down_write(&md->io_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002167 }
Milan Broz73d410c2008-02-08 02:10:25 +00002168
Mikulas Patockaef208582009-04-02 19:55:38 +01002169 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170}
2171
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002172static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002173{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002174 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2175 smp_mb__after_clear_bit();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002176 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002177}
2178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179/*
2180 * Swap in a new table (destroying old one).
2181 */
2182int dm_swap_table(struct mapped_device *md, struct dm_table *table)
2183{
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002184 struct queue_limits limits;
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002185 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Daniel Walkere61290a2008-02-08 02:10:08 +00002187 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
2189 /* device must be suspended */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002190 if (!dm_suspended(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002191 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002193 r = dm_calculate_queue_limits(table, &limits);
2194 if (r)
2195 goto out;
2196
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002197 /* cannot change the device type, once a table is bound */
2198 if (md->map &&
2199 (dm_table_get_type(md->map) != dm_table_get_type(table))) {
2200 DMWARN("can't change the device type after a table is bound");
2201 goto out;
2202 }
2203
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 __unbind(md);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002205 r = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002207out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002208 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002209 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210}
2211
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002212static void dm_rq_invalidate_suspend_marker(struct mapped_device *md)
2213{
2214 md->suspend_rq.special = (void *)0x1;
2215}
2216
2217static void dm_rq_abort_suspend(struct mapped_device *md, int noflush)
2218{
2219 struct request_queue *q = md->queue;
2220 unsigned long flags;
2221
2222 spin_lock_irqsave(q->queue_lock, flags);
2223 if (!noflush)
2224 dm_rq_invalidate_suspend_marker(md);
2225 __start_queue(q);
2226 spin_unlock_irqrestore(q->queue_lock, flags);
2227}
2228
2229static void dm_rq_start_suspend(struct mapped_device *md, int noflush)
2230{
2231 struct request *rq = &md->suspend_rq;
2232 struct request_queue *q = md->queue;
2233
2234 if (noflush)
2235 stop_queue(q);
2236 else {
2237 blk_rq_init(q, rq);
2238 blk_insert_request(q, rq, 0, NULL);
2239 }
2240}
2241
2242static int dm_rq_suspend_available(struct mapped_device *md, int noflush)
2243{
2244 int r = 1;
2245 struct request *rq = &md->suspend_rq;
2246 struct request_queue *q = md->queue;
2247 unsigned long flags;
2248
2249 if (noflush)
2250 return r;
2251
2252 /* The marker must be protected by queue lock if it is in use */
2253 spin_lock_irqsave(q->queue_lock, flags);
2254 if (unlikely(rq->ref_count)) {
2255 /*
2256 * This can happen, when the previous flush suspend was
2257 * interrupted, the marker is still in the queue and
2258 * this flush suspend has been invoked, because we don't
2259 * remove the marker at the time of suspend interruption.
2260 * We have only one marker per mapped_device, so we can't
2261 * start another flush suspend while it is in use.
2262 */
2263 BUG_ON(!rq->special); /* The marker should be invalidated */
2264 DMWARN("Invalidating the previous flush suspend is still in"
2265 " progress. Please retry later.");
2266 r = 0;
2267 }
2268 spin_unlock_irqrestore(q->queue_lock, flags);
2269
2270 return r;
2271}
2272
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273/*
2274 * Functions to lock and unlock any filesystem running on the
2275 * device.
2276 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002277static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002279 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
2281 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002282
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002283 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002284 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002285 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002286 md->frozen_sb = NULL;
2287 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002288 }
2289
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002290 set_bit(DMF_FROZEN, &md->flags);
2291
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 return 0;
2293}
2294
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002295static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002297 if (!test_bit(DMF_FROZEN, &md->flags))
2298 return;
2299
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002300 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002302 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303}
2304
2305/*
2306 * We need to be able to change a mapping table under a mounted
2307 * filesystem. For example we might want to move some data in
2308 * the background. Before the table can be swapped with
2309 * dm_bind_table, dm_suspend must be called to flush any in
2310 * flight bios and ensure that any further io gets deferred.
2311 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002312/*
2313 * Suspend mechanism in request-based dm.
2314 *
2315 * After the suspend starts, further incoming requests are kept in
2316 * the request_queue and deferred.
2317 * Remaining requests in the request_queue at the start of suspend are flushed
2318 * if it is flush suspend.
2319 * The suspend completes when the following conditions have been satisfied,
2320 * so wait for it:
2321 * 1. q->in_flight is 0 (which means no in_flight request)
2322 * 2. queue has been stopped (which means no request dispatching)
2323 *
2324 *
2325 * Noflush suspend
2326 * ---------------
2327 * Noflush suspend doesn't need to dispatch remaining requests.
2328 * So stop the queue immediately. Then, wait for all in_flight requests
2329 * to be completed or requeued.
2330 *
2331 * To abort noflush suspend, start the queue.
2332 *
2333 *
2334 * Flush suspend
2335 * -------------
2336 * Flush suspend needs to dispatch remaining requests. So stop the queue
2337 * after the remaining requests are completed. (Requeued request must be also
2338 * re-dispatched and completed. Until then, we can't stop the queue.)
2339 *
2340 * During flushing the remaining requests, further incoming requests are also
2341 * inserted to the same queue. To distinguish which requests are to be
2342 * flushed, we insert a marker request to the queue at the time of starting
2343 * flush suspend, like a barrier.
2344 * The dispatching is blocked when the marker is found on the top of the queue.
2345 * And the queue is stopped when all in_flight requests are completed, since
2346 * that means the remaining requests are completely flushed.
2347 * Then, the marker is removed from the queue.
2348 *
2349 * To abort flush suspend, we also need to take care of the marker, not only
2350 * starting the queue.
2351 * We don't remove the marker forcibly from the queue since it's against
2352 * the block-layer manner. Instead, we put a invalidated mark on the marker.
2353 * When the invalidated marker is found on the top of the queue, it is
2354 * immediately removed from the queue, so it doesn't block dispatching.
2355 * Because we have only one marker per mapped_device, we can't start another
2356 * flush suspend until the invalidated marker is removed from the queue.
2357 * So fail and return with -EBUSY in such a case.
2358 */
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002359int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002361 struct dm_table *map = NULL;
Milan Broz46125c12008-02-08 02:10:30 +00002362 int r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002363 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002364 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Daniel Walkere61290a2008-02-08 02:10:08 +00002366 mutex_lock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002367
Milan Broz73d410c2008-02-08 02:10:25 +00002368 if (dm_suspended(md)) {
2369 r = -EINVAL;
Alasdair G Kergond2874832006-11-08 17:44:43 -08002370 goto out_unlock;
Milan Broz73d410c2008-02-08 02:10:25 +00002371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002373 if (dm_request_based(md) && !dm_rq_suspend_available(md, noflush)) {
2374 r = -EBUSY;
2375 goto out_unlock;
2376 }
2377
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 map = dm_get_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002380 /*
2381 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2382 * This flag is cleared before dm_suspend returns.
2383 */
2384 if (noflush)
2385 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2386
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002387 /* This does not get reverted if there's an error later. */
2388 dm_table_presuspend_targets(map);
2389
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002390 /*
2391 * Flush I/O to the device. noflush supersedes do_lockfs,
2392 * because lock_fs() needs to flush I/Os.
2393 */
2394 if (!noflush && do_lockfs) {
2395 r = lock_fs(md);
2396 if (r)
Kiyoshi Uedaf431d962008-10-21 17:45:07 +01002397 goto out;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
2400 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002401 * Here we must make sure that no processes are submitting requests
2402 * to target drivers i.e. no one may be executing
2403 * __split_and_process_bio. This is called from dm_request and
2404 * dm_wq_work.
2405 *
2406 * To get all processes out of __split_and_process_bio in dm_request,
2407 * we take the write lock. To prevent any process from reentering
2408 * __split_and_process_bio from dm_request, we set
2409 * DMF_QUEUE_IO_TO_THREAD.
2410 *
2411 * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND
2412 * and call flush_workqueue(md->wq). flush_workqueue will wait until
2413 * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any
2414 * further calls to __split_and_process_bio from dm_wq_work.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002416 down_write(&md->io_lock);
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002417 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2418 set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002419 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002421 flush_workqueue(md->wq);
2422
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002423 if (dm_request_based(md))
2424 dm_rq_start_suspend(md, noflush);
2425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002427 * At this point no more requests are entering target request routines.
2428 * We call dm_wait_for_completion to wait for all existing requests
2429 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 */
Mikulas Patocka401600d2009-04-02 19:55:38 +01002431 r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002433 down_write(&md->io_lock);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002434 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01002435 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Milan Broz94d63512008-02-08 02:10:27 +00002436 up_write(&md->io_lock);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002437
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00002439 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002440 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00002441
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002442 if (dm_request_based(md))
2443 dm_rq_abort_suspend(md, noflush);
2444
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002445 unlock_fs(md);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002446 goto out; /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002447 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002448
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002449 /*
2450 * If dm_wait_for_completion returned 0, the device is completely
2451 * quiescent now. There is no request-processing activity. All new
2452 * requests are being added to md->deferred list.
2453 */
2454
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002455 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
2457 set_bit(DMF_SUSPENDED, &md->flags);
2458
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002459out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 dm_table_put(map);
Alasdair G Kergond2874832006-11-08 17:44:43 -08002461
2462out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00002463 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002464 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465}
2466
2467int dm_resume(struct mapped_device *md)
2468{
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002469 int r = -EINVAL;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002470 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
Daniel Walkere61290a2008-02-08 02:10:08 +00002472 mutex_lock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002473 if (!dm_suspended(md))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002474 goto out;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002475
2476 map = dm_get_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002477 if (!map || !dm_table_get_size(map))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002478 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
Milan Broz8757b772006-10-03 01:15:36 -07002480 r = dm_table_resume_targets(map);
2481 if (r)
2482 goto out;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002483
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002484 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002485
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002486 /*
2487 * Flushing deferred I/Os must be done after targets are resumed
2488 * so that mapping of targets can work correctly.
2489 * Request-based dm is queueing the deferred I/Os in its request_queue.
2490 */
2491 if (dm_request_based(md))
2492 start_queue(md->queue);
2493
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002494 unlock_fs(md);
2495
2496 clear_bit(DMF_SUSPENDED, &md->flags);
2497
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 dm_table_unplug_all(map);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002499 r = 0;
2500out:
2501 dm_table_put(map);
Daniel Walkere61290a2008-02-08 02:10:08 +00002502 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002503
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002504 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505}
2506
2507/*-----------------------------------------------------------------
2508 * Event notification.
2509 *---------------------------------------------------------------*/
Milan Broz60935eb2009-06-22 10:12:30 +01002510void dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
2511 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002512{
Milan Broz60935eb2009-06-22 10:12:30 +01002513 char udev_cookie[DM_COOKIE_LENGTH];
2514 char *envp[] = { udev_cookie, NULL };
2515
2516 if (!cookie)
2517 kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
2518 else {
2519 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2520 DM_COOKIE_ENV_VAR_NAME, cookie);
2521 kobject_uevent_env(&disk_to_dev(md->disk)->kobj, action, envp);
2522 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002523}
2524
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002525uint32_t dm_next_uevent_seq(struct mapped_device *md)
2526{
2527 return atomic_add_return(1, &md->uevent_seq);
2528}
2529
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530uint32_t dm_get_event_nr(struct mapped_device *md)
2531{
2532 return atomic_read(&md->event_nr);
2533}
2534
2535int dm_wait_event(struct mapped_device *md, int event_nr)
2536{
2537 return wait_event_interruptible(md->eventq,
2538 (event_nr != atomic_read(&md->event_nr)));
2539}
2540
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002541void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2542{
2543 unsigned long flags;
2544
2545 spin_lock_irqsave(&md->uevent_lock, flags);
2546 list_add(elist, &md->uevent_list);
2547 spin_unlock_irqrestore(&md->uevent_lock, flags);
2548}
2549
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550/*
2551 * The gendisk is only valid as long as you have a reference
2552 * count on 'md'.
2553 */
2554struct gendisk *dm_disk(struct mapped_device *md)
2555{
2556 return md->disk;
2557}
2558
Milan Broz784aae72009-01-06 03:05:12 +00002559struct kobject *dm_kobject(struct mapped_device *md)
2560{
2561 return &md->kobj;
2562}
2563
2564/*
2565 * struct mapped_device should not be exported outside of dm.c
2566 * so use this check to verify that kobj is part of md structure
2567 */
2568struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2569{
2570 struct mapped_device *md;
2571
2572 md = container_of(kobj, struct mapped_device, kobj);
2573 if (&md->kobj != kobj)
2574 return NULL;
2575
Milan Broz4d89b7b2009-06-22 10:12:11 +01002576 if (test_bit(DMF_FREEING, &md->flags) ||
2577 test_bit(DMF_DELETING, &md->flags))
2578 return NULL;
2579
Milan Broz784aae72009-01-06 03:05:12 +00002580 dm_get(md);
2581 return md;
2582}
2583
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584int dm_suspended(struct mapped_device *md)
2585{
2586 return test_bit(DMF_SUSPENDED, &md->flags);
2587}
2588
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002589int dm_noflush_suspending(struct dm_target *ti)
2590{
2591 struct mapped_device *md = dm_table_get_md(ti->table);
2592 int r = __noflush_suspending(md);
2593
2594 dm_put(md);
2595
2596 return r;
2597}
2598EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2599
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002600struct dm_md_mempools *dm_alloc_md_mempools(unsigned type)
2601{
2602 struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL);
2603
2604 if (!pools)
2605 return NULL;
2606
2607 pools->io_pool = (type == DM_TYPE_BIO_BASED) ?
2608 mempool_create_slab_pool(MIN_IOS, _io_cache) :
2609 mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache);
2610 if (!pools->io_pool)
2611 goto free_pools_and_out;
2612
2613 pools->tio_pool = (type == DM_TYPE_BIO_BASED) ?
2614 mempool_create_slab_pool(MIN_IOS, _tio_cache) :
2615 mempool_create_slab_pool(MIN_IOS, _rq_tio_cache);
2616 if (!pools->tio_pool)
2617 goto free_io_pool_and_out;
2618
2619 pools->bs = (type == DM_TYPE_BIO_BASED) ?
2620 bioset_create(16, 0) : bioset_create(MIN_IOS, 0);
2621 if (!pools->bs)
2622 goto free_tio_pool_and_out;
2623
2624 return pools;
2625
2626free_tio_pool_and_out:
2627 mempool_destroy(pools->tio_pool);
2628
2629free_io_pool_and_out:
2630 mempool_destroy(pools->io_pool);
2631
2632free_pools_and_out:
2633 kfree(pools);
2634
2635 return NULL;
2636}
2637
2638void dm_free_md_mempools(struct dm_md_mempools *pools)
2639{
2640 if (!pools)
2641 return;
2642
2643 if (pools->io_pool)
2644 mempool_destroy(pools->io_pool);
2645
2646 if (pools->tio_pool)
2647 mempool_destroy(pools->tio_pool);
2648
2649 if (pools->bs)
2650 bioset_free(pools->bs);
2651
2652 kfree(pools);
2653}
2654
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655static struct block_device_operations dm_blk_dops = {
2656 .open = dm_blk_open,
2657 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07002658 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002659 .getgeo = dm_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 .owner = THIS_MODULE
2661};
2662
2663EXPORT_SYMBOL(dm_get_mapinfo);
2664
2665/*
2666 * module hooks
2667 */
2668module_init(dm_init);
2669module_exit(dm_exit);
2670
2671module_param(major, uint, 0);
2672MODULE_PARM_DESC(major, "The major number of the device mapper");
2673MODULE_DESCRIPTION(DM_NAME " driver");
2674MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2675MODULE_LICENSE("GPL");