blob: bb5c1eaca52b3688000f51bc841c69470fc7600a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07003 * Copyright (C) 2004-2006 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"
9#include "dm-bio-list.h"
Mike Anderson51e5b2b2007-10-19 22:48:00 +010010#include "dm-uevent.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12#include <linux/init.h>
13#include <linux/module.h>
Arjan van de Ven48c9c272006-03-27 01:18:20 -080014#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/moduleparam.h>
16#include <linux/blkpg.h>
17#include <linux/bio.h>
18#include <linux/buffer_head.h>
19#include <linux/mempool.h>
20#include <linux/slab.h>
21#include <linux/idr.h>
Darrick J. Wong3ac51e72006-03-27 01:17:54 -080022#include <linux/hdreg.h>
Jens Axboe2056a782006-03-23 20:00:26 +010023#include <linux/blktrace_api.h>
Milan Brozaa129a22006-10-03 01:15:15 -070024#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Alasdair G Kergon72d94862006-06-26 00:27:35 -070026#define DM_MSG_PREFIX "core"
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static const char *_name = DM_NAME;
29
30static unsigned int major = 0;
31static unsigned int _major = 0;
32
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -070033static DEFINE_SPINLOCK(_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/*
35 * One of these is allocated per bio.
36 */
37struct dm_io {
38 struct mapped_device *md;
39 int error;
40 struct bio *bio;
41 atomic_t io_count;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -080042 unsigned long start_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043};
44
45/*
46 * One of these is allocated per target within a bio. Hopefully
47 * this will be simplified out one day.
48 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +010049struct dm_target_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 struct dm_io *io;
51 struct dm_target *ti;
52 union map_info info;
53};
54
55union map_info *dm_get_mapinfo(struct bio *bio)
56{
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070057 if (bio && bio->bi_private)
Alasdair G Kergon028867a2007-07-12 17:26:32 +010058 return &((struct dm_target_io *)bio->bi_private)->info;
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070059 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -070062#define MINOR_ALLOCED ((void *)-1)
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/*
65 * Bits for the md->flags field.
66 */
67#define DMF_BLOCK_IO 0
68#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -080069#define DMF_FROZEN 2
Jeff Mahoneyfba9f902006-06-26 00:27:23 -070070#define DMF_FREEING 3
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -070071#define DMF_DELETING 4
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -080072#define DMF_NOFLUSH_SUSPENDING 5
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74struct mapped_device {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -070075 struct rw_semaphore io_lock;
76 struct semaphore suspend_lock;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -080077 spinlock_t pushback_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 rwlock_t map_lock;
79 atomic_t holders;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -070080 atomic_t open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 unsigned long flags;
83
Jens Axboe165125e2007-07-24 09:28:11 +020084 struct request_queue *queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 struct gendisk *disk;
Mike Anderson7e51f252006-03-27 01:17:52 -080086 char name[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 void *interface_ptr;
89
90 /*
91 * A list of ios that arrived while we were suspended.
92 */
93 atomic_t pending;
94 wait_queue_head_t wait;
Kiyoshi Ueda74859362006-12-08 02:41:02 -080095 struct bio_list deferred;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -080096 struct bio_list pushback;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 /*
99 * The current mapping.
100 */
101 struct dm_table *map;
102
103 /*
104 * io objects are allocated from here.
105 */
106 mempool_t *io_pool;
107 mempool_t *tio_pool;
108
Stefan Bader9faf4002006-10-03 01:15:41 -0700109 struct bio_set *bs;
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 /*
112 * Event handling.
113 */
114 atomic_t event_nr;
115 wait_queue_head_t eventq;
116
117 /*
118 * freeze/thaw support require holding onto a super block
119 */
120 struct super_block *frozen_sb;
Alasdair G Kergone39e2e92006-01-06 00:20:05 -0800121 struct block_device *suspended_bdev;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800122
123 /* forced geometry settings */
124 struct hd_geometry geometry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
127#define MIN_IOS 256
Christoph Lametere18b8902006-12-06 20:33:20 -0800128static struct kmem_cache *_io_cache;
129static struct kmem_cache *_tio_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131static int __init local_init(void)
132{
133 int r;
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100136 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if (!_io_cache)
138 return -ENOMEM;
139
140 /* allocate a slab for the target ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100141 _tio_cache = KMEM_CACHE(dm_target_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (!_tio_cache) {
143 kmem_cache_destroy(_io_cache);
144 return -ENOMEM;
145 }
146
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100147 r = dm_uevent_init();
148 if (r) {
149 kmem_cache_destroy(_tio_cache);
150 kmem_cache_destroy(_io_cache);
151 return r;
152 }
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 _major = major;
155 r = register_blkdev(_major, _name);
156 if (r < 0) {
157 kmem_cache_destroy(_tio_cache);
158 kmem_cache_destroy(_io_cache);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100159 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return r;
161 }
162
163 if (!_major)
164 _major = r;
165
166 return 0;
167}
168
169static void local_exit(void)
170{
171 kmem_cache_destroy(_tio_cache);
172 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700173 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100174 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 _major = 0;
177
178 DMINFO("cleaned up");
179}
180
181int (*_inits[])(void) __initdata = {
182 local_init,
183 dm_target_init,
184 dm_linear_init,
185 dm_stripe_init,
186 dm_interface_init,
187};
188
189void (*_exits[])(void) = {
190 local_exit,
191 dm_target_exit,
192 dm_linear_exit,
193 dm_stripe_exit,
194 dm_interface_exit,
195};
196
197static int __init dm_init(void)
198{
199 const int count = ARRAY_SIZE(_inits);
200
201 int r, i;
202
203 for (i = 0; i < count; i++) {
204 r = _inits[i]();
205 if (r)
206 goto bad;
207 }
208
209 return 0;
210
211 bad:
212 while (i--)
213 _exits[i]();
214
215 return r;
216}
217
218static void __exit dm_exit(void)
219{
220 int i = ARRAY_SIZE(_exits);
221
222 while (i--)
223 _exits[i]();
224}
225
226/*
227 * Block device functions
228 */
229static int dm_blk_open(struct inode *inode, struct file *file)
230{
231 struct mapped_device *md;
232
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700233 spin_lock(&_minor_lock);
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 md = inode->i_bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700236 if (!md)
237 goto out;
238
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700239 if (test_bit(DMF_FREEING, &md->flags) ||
240 test_bit(DMF_DELETING, &md->flags)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700241 md = NULL;
242 goto out;
243 }
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700246 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700247
248out:
249 spin_unlock(&_minor_lock);
250
251 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
254static int dm_blk_close(struct inode *inode, struct file *file)
255{
256 struct mapped_device *md;
257
258 md = inode->i_bdev->bd_disk->private_data;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700259 atomic_dec(&md->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 dm_put(md);
261 return 0;
262}
263
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700264int dm_open_count(struct mapped_device *md)
265{
266 return atomic_read(&md->open_count);
267}
268
269/*
270 * Guarantees nothing is using the device before it's deleted.
271 */
272int dm_lock_for_deletion(struct mapped_device *md)
273{
274 int r = 0;
275
276 spin_lock(&_minor_lock);
277
278 if (dm_open_count(md))
279 r = -EBUSY;
280 else
281 set_bit(DMF_DELETING, &md->flags);
282
283 spin_unlock(&_minor_lock);
284
285 return r;
286}
287
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800288static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
289{
290 struct mapped_device *md = bdev->bd_disk->private_data;
291
292 return dm_get_geometry(md, geo);
293}
294
Milan Brozaa129a22006-10-03 01:15:15 -0700295static int dm_blk_ioctl(struct inode *inode, struct file *file,
296 unsigned int cmd, unsigned long arg)
297{
298 struct mapped_device *md;
299 struct dm_table *map;
300 struct dm_target *tgt;
301 int r = -ENOTTY;
302
303 /* We don't really need this lock, but we do need 'inode'. */
304 unlock_kernel();
305
306 md = inode->i_bdev->bd_disk->private_data;
307
308 map = dm_get_table(md);
309
310 if (!map || !dm_table_get_size(map))
311 goto out;
312
313 /* We only support devices that have a single target */
314 if (dm_table_get_num_targets(map) != 1)
315 goto out;
316
317 tgt = dm_table_get_target(map, 0);
318
319 if (dm_suspended(md)) {
320 r = -EAGAIN;
321 goto out;
322 }
323
324 if (tgt->type->ioctl)
325 r = tgt->type->ioctl(tgt, inode, file, cmd, arg);
326
327out:
328 dm_table_put(map);
329
330 lock_kernel();
331 return r;
332}
333
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100334static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 return mempool_alloc(md->io_pool, GFP_NOIO);
337}
338
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100339static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 mempool_free(io, md->io_pool);
342}
343
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100344static struct dm_target_io *alloc_tio(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 return mempool_alloc(md->tio_pool, GFP_NOIO);
347}
348
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100349static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 mempool_free(tio, md->tio_pool);
352}
353
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800354static void start_io_acct(struct dm_io *io)
355{
356 struct mapped_device *md = io->md;
357
358 io->start_time = jiffies;
359
360 preempt_disable();
361 disk_round_stats(dm_disk(md));
362 preempt_enable();
363 dm_disk(md)->in_flight = atomic_inc_return(&md->pending);
364}
365
366static int end_io_acct(struct dm_io *io)
367{
368 struct mapped_device *md = io->md;
369 struct bio *bio = io->bio;
370 unsigned long duration = jiffies - io->start_time;
371 int pending;
372 int rw = bio_data_dir(bio);
373
374 preempt_disable();
375 disk_round_stats(dm_disk(md));
376 preempt_enable();
377 dm_disk(md)->in_flight = pending = atomic_dec_return(&md->pending);
378
379 disk_stat_add(dm_disk(md), ticks[rw], duration);
380
381 return !pending;
382}
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384/*
385 * Add the bio to the list of deferred io.
386 */
387static int queue_io(struct mapped_device *md, struct bio *bio)
388{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700389 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700392 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return 1;
394 }
395
396 bio_list_add(&md->deferred, bio);
397
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700398 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return 0; /* deferred successfully */
400}
401
402/*
403 * Everyone (including functions in this file), should use this
404 * function to access the md->map field, and make sure they call
405 * dm_table_put() when finished.
406 */
407struct dm_table *dm_get_table(struct mapped_device *md)
408{
409 struct dm_table *t;
410
411 read_lock(&md->map_lock);
412 t = md->map;
413 if (t)
414 dm_table_get(t);
415 read_unlock(&md->map_lock);
416
417 return t;
418}
419
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800420/*
421 * Get the geometry associated with a dm device
422 */
423int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
424{
425 *geo = md->geometry;
426
427 return 0;
428}
429
430/*
431 * Set the geometry of a device.
432 */
433int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
434{
435 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
436
437 if (geo->start > sz) {
438 DMWARN("Start sector is beyond the geometry limits.");
439 return -EINVAL;
440 }
441
442 md->geometry = *geo;
443
444 return 0;
445}
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447/*-----------------------------------------------------------------
448 * CRUD START:
449 * A more elegant soln is in the works that uses the queue
450 * merge fn, unfortunately there are a couple of changes to
451 * the block layer that I want to make for this. So in the
452 * interests of getting something for people to use I give
453 * you this clearly demarcated crap.
454 *---------------------------------------------------------------*/
455
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800456static int __noflush_suspending(struct mapped_device *md)
457{
458 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
459}
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461/*
462 * Decrements the number of outstanding ios that a bio has been
463 * cloned into, completing the original io if necc.
464 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800465static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800467 unsigned long flags;
468
469 /* Push-back supersedes any I/O errors */
470 if (error && !(io->error > 0 && __noflush_suspending(io->md)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 io->error = error;
472
473 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800474 if (io->error == DM_ENDIO_REQUEUE) {
475 /*
476 * Target requested pushing back the I/O.
477 * This must be handled before the sleeper on
478 * suspend queue merges the pushback list.
479 */
480 spin_lock_irqsave(&io->md->pushback_lock, flags);
481 if (__noflush_suspending(io->md))
482 bio_list_add(&io->md->pushback, io->bio);
483 else
484 /* noflush suspend was interrupted. */
485 io->error = -EIO;
486 spin_unlock_irqrestore(&io->md->pushback_lock, flags);
487 }
488
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800489 if (end_io_acct(io))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 /* nudge anyone waiting on suspend queue */
491 wake_up(&io->md->wait);
492
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800493 if (io->error != DM_ENDIO_REQUEUE) {
494 blk_add_trace_bio(io->md->queue, io->bio,
495 BLK_TA_COMPLETE);
Jens Axboe2056a782006-03-23 20:00:26 +0100496
NeilBrown6712ecf2007-09-27 12:47:43 +0200497 bio_endio(io->bio, io->error);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800498 }
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 free_io(io->md, io);
501 }
502}
503
NeilBrown6712ecf2007-09-27 12:47:43 +0200504static void clone_endio(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 int r = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100507 struct dm_target_io *tio = bio->bi_private;
Stefan Bader9faf4002006-10-03 01:15:41 -0700508 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 dm_endio_fn endio = tio->ti->type->end_io;
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
512 error = -EIO;
513
514 if (endio) {
515 r = endio(tio->ti, bio, error, &tio->info);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800516 if (r < 0 || r == DM_ENDIO_REQUEUE)
517 /*
518 * error and requeue request are handled
519 * in dec_pending().
520 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800522 else if (r == DM_ENDIO_INCOMPLETE)
523 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +0200524 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800525 else if (r) {
526 DMWARN("unimplemented target endio return value: %d", r);
527 BUG();
528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
530
Stefan Bader9faf4002006-10-03 01:15:41 -0700531 dec_pending(tio->io, error);
532
533 /*
534 * Store md for cleanup instead of tio which is about to get freed.
535 */
536 bio->bi_private = md->bs;
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 bio_put(bio);
Stefan Bader9faf4002006-10-03 01:15:41 -0700539 free_tio(md, tio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
542static sector_t max_io_len(struct mapped_device *md,
543 sector_t sector, struct dm_target *ti)
544{
545 sector_t offset = sector - ti->begin;
546 sector_t len = ti->len - offset;
547
548 /*
549 * Does the target need to split even further ?
550 */
551 if (ti->split_io) {
552 sector_t boundary;
553 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
554 - offset;
555 if (len > boundary)
556 len = boundary;
557 }
558
559 return len;
560}
561
562static void __map_bio(struct dm_target *ti, struct bio *clone,
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100563 struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
565 int r;
Jens Axboe2056a782006-03-23 20:00:26 +0100566 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -0700567 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 /*
570 * Sanity checks.
571 */
572 BUG_ON(!clone->bi_size);
573
574 clone->bi_end_io = clone_endio;
575 clone->bi_private = tio;
576
577 /*
578 * Map the clone. If r == 0 we don't need to do
579 * anything, the target has assumed ownership of
580 * this io.
581 */
582 atomic_inc(&tio->io->io_count);
Jens Axboe2056a782006-03-23 20:00:26 +0100583 sector = clone->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 r = ti->type->map(ti, clone, &tio->info);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800585 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +0100587
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700588 blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone,
Alan D. Brunellec7149d62007-08-07 15:30:23 +0200589 tio->io->bio->bi_bdev->bd_dev,
590 clone->bi_sector, sector);
Jens Axboe2056a782006-03-23 20:00:26 +0100591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800593 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
594 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -0700595 md = tio->io->md;
596 dec_pending(tio->io, r);
597 /*
598 * Store bio_set for cleanup.
599 */
600 clone->bi_private = md->bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 bio_put(clone);
Stefan Bader9faf4002006-10-03 01:15:41 -0700602 free_tio(md, tio);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800603 } else if (r) {
604 DMWARN("unimplemented target map return value: %d", r);
605 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607}
608
609struct clone_info {
610 struct mapped_device *md;
611 struct dm_table *map;
612 struct bio *bio;
613 struct dm_io *io;
614 sector_t sector;
615 sector_t sector_count;
616 unsigned short idx;
617};
618
Peter Osterlund36763472005-09-06 15:16:42 -0700619static void dm_bio_destructor(struct bio *bio)
620{
Stefan Bader9faf4002006-10-03 01:15:41 -0700621 struct bio_set *bs = bio->bi_private;
622
623 bio_free(bio, bs);
Peter Osterlund36763472005-09-06 15:16:42 -0700624}
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626/*
627 * Creates a little bio that is just does part of a bvec.
628 */
629static struct bio *split_bvec(struct bio *bio, sector_t sector,
630 unsigned short idx, unsigned int offset,
Stefan Bader9faf4002006-10-03 01:15:41 -0700631 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
633 struct bio *clone;
634 struct bio_vec *bv = bio->bi_io_vec + idx;
635
Stefan Bader9faf4002006-10-03 01:15:41 -0700636 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
Peter Osterlund36763472005-09-06 15:16:42 -0700637 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 *clone->bi_io_vec = *bv;
639
640 clone->bi_sector = sector;
641 clone->bi_bdev = bio->bi_bdev;
642 clone->bi_rw = bio->bi_rw;
643 clone->bi_vcnt = 1;
644 clone->bi_size = to_bytes(len);
645 clone->bi_io_vec->bv_offset = offset;
646 clone->bi_io_vec->bv_len = clone->bi_size;
647
648 return clone;
649}
650
651/*
652 * Creates a bio that consists of range of complete bvecs.
653 */
654static struct bio *clone_bio(struct bio *bio, sector_t sector,
655 unsigned short idx, unsigned short bv_count,
Stefan Bader9faf4002006-10-03 01:15:41 -0700656 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
658 struct bio *clone;
659
Stefan Bader9faf4002006-10-03 01:15:41 -0700660 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
661 __bio_clone(clone, bio);
662 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 clone->bi_sector = sector;
664 clone->bi_idx = idx;
665 clone->bi_vcnt = idx + bv_count;
666 clone->bi_size = to_bytes(len);
667 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
668
669 return clone;
670}
671
672static void __clone_and_map(struct clone_info *ci)
673{
674 struct bio *clone, *bio = ci->bio;
675 struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
676 sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100677 struct dm_target_io *tio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 /*
680 * Allocate a target io object.
681 */
682 tio = alloc_tio(ci->md);
683 tio->io = ci->io;
684 tio->ti = ti;
685 memset(&tio->info, 0, sizeof(tio->info));
686
687 if (ci->sector_count <= max) {
688 /*
689 * Optimise for the simple case where we can do all of
690 * the remaining io with a single clone.
691 */
692 clone = clone_bio(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -0700693 bio->bi_vcnt - ci->idx, ci->sector_count,
694 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 __map_bio(ti, clone, tio);
696 ci->sector_count = 0;
697
698 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
699 /*
700 * There are some bvecs that don't span targets.
701 * Do as many of these as possible.
702 */
703 int i;
704 sector_t remaining = max;
705 sector_t bv_len;
706
707 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
708 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
709
710 if (bv_len > remaining)
711 break;
712
713 remaining -= bv_len;
714 len += bv_len;
715 }
716
Stefan Bader9faf4002006-10-03 01:15:41 -0700717 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
718 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 __map_bio(ti, clone, tio);
720
721 ci->sector += len;
722 ci->sector_count -= len;
723 ci->idx = i;
724
725 } else {
726 /*
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800727 * Handle a bvec that must be split between two or more targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 */
729 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800730 sector_t remaining = to_sector(bv->bv_len);
731 unsigned int offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800733 do {
734 if (offset) {
735 ti = dm_table_find_target(ci->map, ci->sector);
736 max = max_io_len(ci->md, ci->sector, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800738 tio = alloc_tio(ci->md);
739 tio->io = ci->io;
740 tio->ti = ti;
741 memset(&tio->info, 0, sizeof(tio->info));
742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800744 len = min(remaining, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800746 clone = split_bvec(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -0700747 bv->bv_offset + offset, len,
748 ci->md->bs);
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800749
750 __map_bio(ti, clone, tio);
751
752 ci->sector += len;
753 ci->sector_count -= len;
754 offset += to_bytes(len);
755 } while (remaining -= len);
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 ci->idx++;
758 }
759}
760
761/*
762 * Split the bio into several clones.
763 */
Milan Broz9e4e5f82007-10-19 22:38:53 +0100764static int __split_bio(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
766 struct clone_info ci;
767
768 ci.map = dm_get_table(md);
Milan Broz9e4e5f82007-10-19 22:38:53 +0100769 if (unlikely(!ci.map))
770 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 ci.md = md;
773 ci.bio = bio;
774 ci.io = alloc_io(md);
775 ci.io->error = 0;
776 atomic_set(&ci.io->io_count, 1);
777 ci.io->bio = bio;
778 ci.io->md = md;
779 ci.sector = bio->bi_sector;
780 ci.sector_count = bio_sectors(bio);
781 ci.idx = bio->bi_idx;
782
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800783 start_io_acct(ci.io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 while (ci.sector_count)
785 __clone_and_map(&ci);
786
787 /* drop the extra reference count */
788 dec_pending(ci.io, 0);
789 dm_table_put(ci.map);
Milan Broz9e4e5f82007-10-19 22:38:53 +0100790
791 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793/*-----------------------------------------------------------------
794 * CRUD END
795 *---------------------------------------------------------------*/
796
797/*
798 * The request function that just remaps the bio built up by
799 * dm_merge_bvec.
800 */
Jens Axboe165125e2007-07-24 09:28:11 +0200801static int dm_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Milan Broz9e4e5f82007-10-19 22:38:53 +0100803 int r = -EIO;
Kevin Corry12f03a42006-02-01 03:04:52 -0800804 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 struct mapped_device *md = q->queuedata;
806
Stefan Bader07a83c42007-07-12 17:28:33 +0100807 /*
808 * There is no use in forwarding any barrier request since we can't
809 * guarantee it is (or can be) handled by the targets correctly.
810 */
811 if (unlikely(bio_barrier(bio))) {
NeilBrown6712ecf2007-09-27 12:47:43 +0200812 bio_endio(bio, -EOPNOTSUPP);
Stefan Bader07a83c42007-07-12 17:28:33 +0100813 return 0;
814 }
815
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700816 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Kevin Corry12f03a42006-02-01 03:04:52 -0800818 disk_stat_inc(dm_disk(md), ios[rw]);
819 disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio));
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 /*
822 * If we're suspended we have to queue
823 * this io for later.
824 */
825 while (test_bit(DMF_BLOCK_IO, &md->flags)) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700826 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Milan Broz9e4e5f82007-10-19 22:38:53 +0100828 if (bio_rw(bio) != READA)
829 r = queue_io(md, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Milan Broz9e4e5f82007-10-19 22:38:53 +0100831 if (r <= 0)
832 goto out_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 /*
835 * We're in a while loop, because someone could suspend
836 * before we get to the following read lock.
837 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700838 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
840
Milan Broz9e4e5f82007-10-19 22:38:53 +0100841 r = __split_bio(md, bio);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700842 up_read(&md->io_lock);
Milan Broz9e4e5f82007-10-19 22:38:53 +0100843
844out_req:
845 if (r < 0)
846 bio_io_error(bio);
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return 0;
849}
850
Jens Axboe165125e2007-07-24 09:28:11 +0200851static void dm_unplug_all(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 struct mapped_device *md = q->queuedata;
854 struct dm_table *map = dm_get_table(md);
855
856 if (map) {
857 dm_table_unplug_all(map);
858 dm_table_put(map);
859 }
860}
861
862static int dm_any_congested(void *congested_data, int bdi_bits)
863{
864 int r;
865 struct mapped_device *md = (struct mapped_device *) congested_data;
866 struct dm_table *map = dm_get_table(md);
867
868 if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
869 r = bdi_bits;
870 else
871 r = dm_table_any_congested(map, bdi_bits);
872
873 dm_table_put(map);
874 return r;
875}
876
877/*-----------------------------------------------------------------
878 * An IDR is used to keep track of allocated minor numbers.
879 *---------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880static DEFINE_IDR(_minor_idr);
881
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700882static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -0700884 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -0700886 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
889/*
890 * See if the device with a specific minor # is free.
891 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700892static int specific_minor(struct mapped_device *md, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
894 int r, m;
895
896 if (minor >= (1 << MINORBITS))
897 return -EINVAL;
898
Jeff Mahoney62f75c22006-06-26 00:27:21 -0700899 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
900 if (!r)
901 return -ENOMEM;
902
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -0700903 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 if (idr_find(&_minor_idr, minor)) {
906 r = -EBUSY;
907 goto out;
908 }
909
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700910 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
Jeff Mahoney62f75c22006-06-26 00:27:21 -0700911 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 if (m != minor) {
915 idr_remove(&_minor_idr, m);
916 r = -EBUSY;
917 goto out;
918 }
919
920out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -0700921 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return r;
923}
924
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700925static int next_free_minor(struct mapped_device *md, int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700927 int r, m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
Jeff Mahoney62f75c22006-06-26 00:27:21 -0700930 if (!r)
931 return -ENOMEM;
932
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -0700933 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700935 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (r) {
937 goto out;
938 }
939
940 if (m >= (1 << MINORBITS)) {
941 idr_remove(&_minor_idr, m);
942 r = -ENOSPC;
943 goto out;
944 }
945
946 *minor = m;
947
948out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -0700949 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return r;
951}
952
953static struct block_device_operations dm_blk_dops;
954
955/*
956 * Allocate and initialise a blank device with a given minor.
957 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700958static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
960 int r;
961 struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700962 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 if (!md) {
965 DMWARN("unable to allocate device, out of memory.");
966 return NULL;
967 }
968
Jeff Mahoney10da4f72006-06-26 00:27:25 -0700969 if (!try_module_get(THIS_MODULE))
970 goto bad0;
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700973 if (minor == DM_ANY_MINOR)
974 r = next_free_minor(md, &minor);
975 else
976 r = specific_minor(md, minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (r < 0)
978 goto bad1;
979
980 memset(md, 0, sizeof(*md));
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700981 init_rwsem(&md->io_lock);
982 init_MUTEX(&md->suspend_lock);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800983 spin_lock_init(&md->pushback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 rwlock_init(&md->map_lock);
985 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700986 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 atomic_set(&md->event_nr, 0);
988
989 md->queue = blk_alloc_queue(GFP_KERNEL);
990 if (!md->queue)
Ishai Rabinovitze3f6ac62006-10-03 01:15:22 -0700991 goto bad1_free_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 md->queue->queuedata = md;
994 md->queue->backing_dev_info.congested_fn = dm_any_congested;
995 md->queue->backing_dev_info.congested_data = md;
996 blk_queue_make_request(md->queue, dm_request);
Jens Axboedaef2652006-01-10 10:48:02 +0100997 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 md->queue->unplug_fn = dm_unplug_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Matthew Dobson93d23412006-03-26 01:37:50 -08001000 md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
Kiyoshi Ueda74859362006-12-08 02:41:02 -08001001 if (!md->io_pool)
1002 goto bad2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Matthew Dobson93d23412006-03-26 01:37:50 -08001004 md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (!md->tio_pool)
1006 goto bad3;
1007
Jens Axboe59725112007-04-02 10:06:42 +02001008 md->bs = bioset_create(16, 16);
Stefan Bader9faf4002006-10-03 01:15:41 -07001009 if (!md->bs)
1010 goto bad_no_bioset;
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 md->disk = alloc_disk(1);
1013 if (!md->disk)
1014 goto bad4;
1015
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001016 atomic_set(&md->pending, 0);
1017 init_waitqueue_head(&md->wait);
1018 init_waitqueue_head(&md->eventq);
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 md->disk->major = _major;
1021 md->disk->first_minor = minor;
1022 md->disk->fops = &dm_blk_dops;
1023 md->disk->queue = md->queue;
1024 md->disk->private_data = md;
1025 sprintf(md->disk->disk_name, "dm-%d", minor);
1026 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08001027 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001029 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001030 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001031 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001032 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001033
1034 BUG_ON(old_md != MINOR_ALLOCED);
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return md;
1037
1038 bad4:
Stefan Bader9faf4002006-10-03 01:15:41 -07001039 bioset_free(md->bs);
1040 bad_no_bioset:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 mempool_destroy(md->tio_pool);
1042 bad3:
1043 mempool_destroy(md->io_pool);
1044 bad2:
Al Viro1312f402006-03-12 11:02:03 -05001045 blk_cleanup_queue(md->queue);
Ishai Rabinovitze3f6ac62006-10-03 01:15:22 -07001046 bad1_free_minor:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 free_minor(minor);
1048 bad1:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001049 module_put(THIS_MODULE);
1050 bad0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 kfree(md);
1052 return NULL;
1053}
1054
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01001055static void unlock_fs(struct mapped_device *md);
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057static void free_dev(struct mapped_device *md)
1058{
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001059 int minor = md->disk->first_minor;
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001060
Jun'ichi Nomurad9dde592006-02-24 13:04:24 -08001061 if (md->suspended_bdev) {
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01001062 unlock_fs(md);
Jun'ichi Nomurad9dde592006-02-24 13:04:24 -08001063 bdput(md->suspended_bdev);
1064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 mempool_destroy(md->tio_pool);
1066 mempool_destroy(md->io_pool);
Stefan Bader9faf4002006-10-03 01:15:41 -07001067 bioset_free(md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 del_gendisk(md->disk);
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001069 free_minor(minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001070
1071 spin_lock(&_minor_lock);
1072 md->disk->private_data = NULL;
1073 spin_unlock(&_minor_lock);
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 put_disk(md->disk);
Al Viro1312f402006-03-12 11:02:03 -05001076 blk_cleanup_queue(md->queue);
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001077 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 kfree(md);
1079}
1080
1081/*
1082 * Bind a table to the device.
1083 */
1084static void event_callback(void *context)
1085{
1086 struct mapped_device *md = (struct mapped_device *) context;
1087
1088 atomic_inc(&md->event_nr);
1089 wake_up(&md->eventq);
1090}
1091
Alasdair G Kergon4e901882005-07-28 21:15:59 -07001092static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
Alasdair G Kergon4e901882005-07-28 21:15:59 -07001094 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001096 mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001097 i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001098 mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
1101static int __bind(struct mapped_device *md, struct dm_table *t)
1102{
Jens Axboe165125e2007-07-24 09:28:11 +02001103 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 sector_t size;
1105
1106 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001107
1108 /*
1109 * Wipe any geometry if the size of the table changed.
1110 */
1111 if (size != get_capacity(md->disk))
1112 memset(&md->geometry, 0, sizeof(md->geometry));
1113
Jun'ichi Nomurabfa152f2007-01-26 00:57:07 -08001114 if (md->suspended_bdev)
1115 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if (size == 0)
1117 return 0;
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 dm_table_get(t);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001120 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001121
1122 write_lock(&md->map_lock);
1123 md->map = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 dm_table_set_restrictions(t, q);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001125 write_unlock(&md->map_lock);
1126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return 0;
1128}
1129
1130static void __unbind(struct mapped_device *md)
1131{
1132 struct dm_table *map = md->map;
1133
1134 if (!map)
1135 return;
1136
1137 dm_table_event_callback(map, NULL, NULL);
1138 write_lock(&md->map_lock);
1139 md->map = NULL;
1140 write_unlock(&md->map_lock);
1141 dm_table_put(map);
1142}
1143
1144/*
1145 * Constructor for a new device.
1146 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001147int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
1149 struct mapped_device *md;
1150
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001151 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (!md)
1153 return -ENXIO;
1154
1155 *result = md;
1156 return 0;
1157}
1158
David Teigland637842c2006-01-06 00:20:00 -08001159static struct mapped_device *dm_find_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 unsigned minor = MINOR(dev);
1163
1164 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
1165 return NULL;
1166
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001167 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 md = idr_find(&_minor_idr, minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001170 if (md && (md == MINOR_ALLOCED ||
1171 (dm_disk(md)->first_minor != minor) ||
Alasdair G Kergon17b2f662006-06-26 00:27:33 -07001172 test_bit(DMF_FREEING, &md->flags))) {
David Teigland637842c2006-01-06 00:20:00 -08001173 md = NULL;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001174 goto out;
1175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001177out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001178 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
David Teigland637842c2006-01-06 00:20:00 -08001180 return md;
1181}
1182
David Teiglandd229a952006-01-06 00:20:01 -08001183struct mapped_device *dm_get_md(dev_t dev)
1184{
1185 struct mapped_device *md = dm_find_md(dev);
1186
1187 if (md)
1188 dm_get(md);
1189
1190 return md;
1191}
1192
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08001193void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08001194{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08001195 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
1197
1198void dm_set_mdptr(struct mapped_device *md, void *ptr)
1199{
1200 md->interface_ptr = ptr;
1201}
1202
1203void dm_get(struct mapped_device *md)
1204{
1205 atomic_inc(&md->holders);
1206}
1207
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001208const char *dm_device_name(struct mapped_device *md)
1209{
1210 return md->name;
1211}
1212EXPORT_SYMBOL_GPL(dm_device_name);
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214void dm_put(struct mapped_device *md)
1215{
Mike Anderson1134e5a2006-03-27 01:17:54 -08001216 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001218 BUG_ON(test_bit(DMF_FREEING, &md->flags));
1219
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001220 if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
Mike Anderson1134e5a2006-03-27 01:17:54 -08001221 map = dm_get_table(md);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001222 idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001223 set_bit(DMF_FREEING, &md->flags);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001224 spin_unlock(&_minor_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001225 if (!dm_suspended(md)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 dm_table_presuspend_targets(map);
1227 dm_table_postsuspend_targets(map);
1228 }
1229 __unbind(md);
Mike Anderson1134e5a2006-03-27 01:17:54 -08001230 dm_table_put(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 free_dev(md);
1232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
Edward Goggin79eb8852007-05-09 02:32:56 -07001234EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236/*
1237 * Process the deferred bios
1238 */
1239static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
1240{
1241 struct bio *n;
1242
1243 while (c) {
1244 n = c->bi_next;
1245 c->bi_next = NULL;
Milan Broz9e4e5f82007-10-19 22:38:53 +01001246 if (__split_bio(md, c))
1247 bio_io_error(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 c = n;
1249 }
1250}
1251
1252/*
1253 * Swap in a new table (destroying old one).
1254 */
1255int dm_swap_table(struct mapped_device *md, struct dm_table *table)
1256{
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001257 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001259 down(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 /* device must be suspended */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001262 if (!dm_suspended(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001263 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Jun'ichi Nomurabfa152f2007-01-26 00:57:07 -08001265 /* without bdev, the device size cannot be changed */
1266 if (!md->suspended_bdev)
1267 if (get_capacity(md->disk) != dm_table_get_size(table))
1268 goto out;
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 __unbind(md);
1271 r = __bind(md, table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001273out:
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001274 up(&md->suspend_lock);
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001275 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276}
1277
1278/*
1279 * Functions to lock and unlock any filesystem running on the
1280 * device.
1281 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001282static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001284 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07001287
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001288 md->frozen_sb = freeze_bdev(md->suspended_bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07001289 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001290 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001291 md->frozen_sb = NULL;
1292 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07001293 }
1294
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001295 set_bit(DMF_FROZEN, &md->flags);
1296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 /* don't bdput right now, we don't want the bdev
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001298 * to go away while it is locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 */
1300 return 0;
1301}
1302
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001303static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001305 if (!test_bit(DMF_FROZEN, &md->flags))
1306 return;
1307
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001308 thaw_bdev(md->suspended_bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001310 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
1312
1313/*
1314 * We need to be able to change a mapping table under a mounted
1315 * filesystem. For example we might want to move some data in
1316 * the background. Before the table can be swapped with
1317 * dm_bind_table, dm_suspend must be called to flush any in
1318 * flight bios and ensure that any further io gets deferred.
1319 */
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08001320int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001322 struct dm_table *map = NULL;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001323 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 DECLARE_WAITQUEUE(wait, current);
Jun'ichi Nomura1ecac7f2006-03-27 01:17:51 -08001325 struct bio *def;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001326 int r = -EINVAL;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08001327 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001328 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001330 down(&md->suspend_lock);
1331
1332 if (dm_suspended(md))
Alasdair G Kergond2874832006-11-08 17:44:43 -08001333 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 map = dm_get_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001337 /*
1338 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
1339 * This flag is cleared before dm_suspend returns.
1340 */
1341 if (noflush)
1342 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1343
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001344 /* This does not get reverted if there's an error later. */
1345 dm_table_presuspend_targets(map);
1346
Jun'ichi Nomurabfa152f2007-01-26 00:57:07 -08001347 /* bdget() can stall if the pending I/Os are not flushed */
1348 if (!noflush) {
1349 md->suspended_bdev = bdget_disk(md->disk, 0);
1350 if (!md->suspended_bdev) {
1351 DMWARN("bdget failed in dm_suspend");
1352 r = -ENOMEM;
1353 goto flush_and_out;
1354 }
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001355 }
1356
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001357 /*
1358 * Flush I/O to the device.
1359 * noflush supersedes do_lockfs, because lock_fs() needs to flush I/Os.
1360 */
1361 if (do_lockfs && !noflush) {
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001362 r = lock_fs(md);
1363 if (r)
1364 goto out;
1365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 /*
Alasdair G Kergon354e0072005-05-05 16:16:05 -07001368 * First we set the BLOCK_IO flag so no more ios will be mapped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001370 down_write(&md->io_lock);
1371 set_bit(DMF_BLOCK_IO, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 add_wait_queue(&md->wait, &wait);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001374 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 /* unplug */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001377 if (map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 dm_table_unplug_all(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 /*
1381 * Then we wait for the already mapped ios to
1382 * complete.
1383 */
1384 while (1) {
1385 set_current_state(TASK_INTERRUPTIBLE);
1386
1387 if (!atomic_read(&md->pending) || signal_pending(current))
1388 break;
1389
1390 io_schedule();
1391 }
1392 set_current_state(TASK_RUNNING);
1393
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001394 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 remove_wait_queue(&md->wait, &wait);
1396
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001397 if (noflush) {
1398 spin_lock_irqsave(&md->pushback_lock, flags);
1399 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1400 bio_list_merge_head(&md->deferred, &md->pushback);
1401 bio_list_init(&md->pushback);
1402 spin_unlock_irqrestore(&md->pushback_lock, flags);
1403 }
1404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 /* were we interrupted ? */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001406 r = -EINTR;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001407 if (atomic_read(&md->pending)) {
Jun'ichi Nomura1ecac7f2006-03-27 01:17:51 -08001408 clear_bit(DMF_BLOCK_IO, &md->flags);
1409 def = bio_list_get(&md->deferred);
1410 __flush_deferred_io(md, def);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001411 up_write(&md->io_lock);
1412 unlock_fs(md);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001413 goto out; /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001414 }
1415 up_write(&md->io_lock);
1416
1417 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 set_bit(DMF_SUSPENDED, &md->flags);
1420
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001421 r = 0;
1422
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001423flush_and_out:
1424 if (r && noflush) {
1425 /*
1426 * Because there may be already I/Os in the pushback list,
1427 * flush them before return.
1428 */
1429 down_write(&md->io_lock);
1430
1431 spin_lock_irqsave(&md->pushback_lock, flags);
1432 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1433 bio_list_merge_head(&md->deferred, &md->pushback);
1434 bio_list_init(&md->pushback);
1435 spin_unlock_irqrestore(&md->pushback_lock, flags);
1436
1437 def = bio_list_get(&md->deferred);
1438 __flush_deferred_io(md, def);
1439 up_write(&md->io_lock);
1440 }
1441
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001442out:
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001443 if (r && md->suspended_bdev) {
1444 bdput(md->suspended_bdev);
1445 md->suspended_bdev = NULL;
1446 }
1447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 dm_table_put(map);
Alasdair G Kergond2874832006-11-08 17:44:43 -08001449
1450out_unlock:
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001451 up(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001452 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
1454
1455int dm_resume(struct mapped_device *md)
1456{
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001457 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 struct bio *def;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001459 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001461 down(&md->suspend_lock);
1462 if (!dm_suspended(md))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001463 goto out;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001464
1465 map = dm_get_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001466 if (!map || !dm_table_get_size(map))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001467 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Milan Broz8757b772006-10-03 01:15:36 -07001469 r = dm_table_resume_targets(map);
1470 if (r)
1471 goto out;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001472
1473 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 clear_bit(DMF_BLOCK_IO, &md->flags);
1475
1476 def = bio_list_get(&md->deferred);
1477 __flush_deferred_io(md, def);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001478 up_write(&md->io_lock);
1479
1480 unlock_fs(md);
1481
Jun'ichi Nomurabfa152f2007-01-26 00:57:07 -08001482 if (md->suspended_bdev) {
1483 bdput(md->suspended_bdev);
1484 md->suspended_bdev = NULL;
1485 }
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001486
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001487 clear_bit(DMF_SUSPENDED, &md->flags);
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 dm_table_unplug_all(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Hannes Reinecke8560ed62006-10-03 01:15:35 -07001491 kobject_uevent(&md->disk->kobj, KOBJ_CHANGE);
1492
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001493 r = 0;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001494
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001495out:
1496 dm_table_put(map);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001497 up(&md->suspend_lock);
1498
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001499 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501
1502/*-----------------------------------------------------------------
1503 * Event notification.
1504 *---------------------------------------------------------------*/
1505uint32_t dm_get_event_nr(struct mapped_device *md)
1506{
1507 return atomic_read(&md->event_nr);
1508}
1509
1510int dm_wait_event(struct mapped_device *md, int event_nr)
1511{
1512 return wait_event_interruptible(md->eventq,
1513 (event_nr != atomic_read(&md->event_nr)));
1514}
1515
1516/*
1517 * The gendisk is only valid as long as you have a reference
1518 * count on 'md'.
1519 */
1520struct gendisk *dm_disk(struct mapped_device *md)
1521{
1522 return md->disk;
1523}
1524
1525int dm_suspended(struct mapped_device *md)
1526{
1527 return test_bit(DMF_SUSPENDED, &md->flags);
1528}
1529
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001530int dm_noflush_suspending(struct dm_target *ti)
1531{
1532 struct mapped_device *md = dm_table_get_md(ti->table);
1533 int r = __noflush_suspending(md);
1534
1535 dm_put(md);
1536
1537 return r;
1538}
1539EXPORT_SYMBOL_GPL(dm_noflush_suspending);
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541static struct block_device_operations dm_blk_dops = {
1542 .open = dm_blk_open,
1543 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07001544 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001545 .getgeo = dm_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 .owner = THIS_MODULE
1547};
1548
1549EXPORT_SYMBOL(dm_get_mapinfo);
1550
1551/*
1552 * module hooks
1553 */
1554module_init(dm_init);
1555module_exit(dm_exit);
1556
1557module_param(major, uint, 0);
1558MODULE_PARM_DESC(major, "The major number of the device mapper");
1559MODULE_DESCRIPTION(DM_NAME " driver");
1560MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1561MODULE_LICENSE("GPL");