blob: fc4f743f3b531e72bdb9c937d6cb2bf09d9ede1d [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"
10
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>
Jens Axboe2056a782006-03-23 20:00:26 +010022#include <linux/blktrace_api.h>
Milan Brozaa129a22006-10-03 01:15:15 -070023#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Alasdair G Kergon72d94862006-06-26 00:27:35 -070025#define DM_MSG_PREFIX "core"
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static const char *_name = DM_NAME;
28
29static unsigned int major = 0;
30static unsigned int _major = 0;
31
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -070032static DEFINE_SPINLOCK(_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/*
34 * One of these is allocated per bio.
35 */
36struct dm_io {
37 struct mapped_device *md;
38 int error;
39 struct bio *bio;
40 atomic_t io_count;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -080041 unsigned long start_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042};
43
44/*
45 * One of these is allocated per target within a bio. Hopefully
46 * this will be simplified out one day.
47 */
48struct target_io {
49 struct dm_io *io;
50 struct dm_target *ti;
51 union map_info info;
52};
53
54union map_info *dm_get_mapinfo(struct bio *bio)
55{
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070056 if (bio && bio->bi_private)
57 return &((struct target_io *)bio->bi_private)->info;
58 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -070061#define MINOR_ALLOCED ((void *)-1)
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/*
64 * Bits for the md->flags field.
65 */
66#define DMF_BLOCK_IO 0
67#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -080068#define DMF_FROZEN 2
Jeff Mahoneyfba9f902006-06-26 00:27:23 -070069#define DMF_FREEING 3
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -070070#define DMF_DELETING 4
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72struct mapped_device {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -070073 struct rw_semaphore io_lock;
74 struct semaphore suspend_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 rwlock_t map_lock;
76 atomic_t holders;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -070077 atomic_t open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 unsigned long flags;
80
81 request_queue_t *queue;
82 struct gendisk *disk;
Mike Anderson7e51f252006-03-27 01:17:52 -080083 char name[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 void *interface_ptr;
86
87 /*
88 * A list of ios that arrived while we were suspended.
89 */
90 atomic_t pending;
91 wait_queue_head_t wait;
92 struct bio_list deferred;
93
94 /*
95 * The current mapping.
96 */
97 struct dm_table *map;
98
99 /*
100 * io objects are allocated from here.
101 */
102 mempool_t *io_pool;
103 mempool_t *tio_pool;
104
Stefan Bader9faf4002006-10-03 01:15:41 -0700105 struct bio_set *bs;
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 /*
108 * Event handling.
109 */
110 atomic_t event_nr;
111 wait_queue_head_t eventq;
112
113 /*
114 * freeze/thaw support require holding onto a super block
115 */
116 struct super_block *frozen_sb;
Alasdair G Kergone39e2e92006-01-06 00:20:05 -0800117 struct block_device *suspended_bdev;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800118
119 /* forced geometry settings */
120 struct hd_geometry geometry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121};
122
123#define MIN_IOS 256
124static kmem_cache_t *_io_cache;
125static kmem_cache_t *_tio_cache;
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static int __init local_init(void)
128{
129 int r;
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 /* allocate a slab for the dm_ios */
132 _io_cache = kmem_cache_create("dm_io",
133 sizeof(struct dm_io), 0, 0, NULL, NULL);
134 if (!_io_cache)
135 return -ENOMEM;
136
137 /* allocate a slab for the target ios */
138 _tio_cache = kmem_cache_create("dm_tio", sizeof(struct target_io),
139 0, 0, NULL, NULL);
140 if (!_tio_cache) {
141 kmem_cache_destroy(_io_cache);
142 return -ENOMEM;
143 }
144
145 _major = major;
146 r = register_blkdev(_major, _name);
147 if (r < 0) {
148 kmem_cache_destroy(_tio_cache);
149 kmem_cache_destroy(_io_cache);
150 return r;
151 }
152
153 if (!_major)
154 _major = r;
155
156 return 0;
157}
158
159static void local_exit(void)
160{
161 kmem_cache_destroy(_tio_cache);
162 kmem_cache_destroy(_io_cache);
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (unregister_blkdev(_major, _name) < 0)
Greg Kroah-Hartman890fbae2005-06-20 21:15:16 -0700165 DMERR("unregister_blkdev failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 _major = 0;
168
169 DMINFO("cleaned up");
170}
171
172int (*_inits[])(void) __initdata = {
173 local_init,
174 dm_target_init,
175 dm_linear_init,
176 dm_stripe_init,
177 dm_interface_init,
178};
179
180void (*_exits[])(void) = {
181 local_exit,
182 dm_target_exit,
183 dm_linear_exit,
184 dm_stripe_exit,
185 dm_interface_exit,
186};
187
188static int __init dm_init(void)
189{
190 const int count = ARRAY_SIZE(_inits);
191
192 int r, i;
193
194 for (i = 0; i < count; i++) {
195 r = _inits[i]();
196 if (r)
197 goto bad;
198 }
199
200 return 0;
201
202 bad:
203 while (i--)
204 _exits[i]();
205
206 return r;
207}
208
209static void __exit dm_exit(void)
210{
211 int i = ARRAY_SIZE(_exits);
212
213 while (i--)
214 _exits[i]();
215}
216
217/*
218 * Block device functions
219 */
220static int dm_blk_open(struct inode *inode, struct file *file)
221{
222 struct mapped_device *md;
223
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700224 spin_lock(&_minor_lock);
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 md = inode->i_bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700227 if (!md)
228 goto out;
229
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700230 if (test_bit(DMF_FREEING, &md->flags) ||
231 test_bit(DMF_DELETING, &md->flags)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700232 md = NULL;
233 goto out;
234 }
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700237 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700238
239out:
240 spin_unlock(&_minor_lock);
241
242 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
245static int dm_blk_close(struct inode *inode, struct file *file)
246{
247 struct mapped_device *md;
248
249 md = inode->i_bdev->bd_disk->private_data;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700250 atomic_dec(&md->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 dm_put(md);
252 return 0;
253}
254
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700255int dm_open_count(struct mapped_device *md)
256{
257 return atomic_read(&md->open_count);
258}
259
260/*
261 * Guarantees nothing is using the device before it's deleted.
262 */
263int dm_lock_for_deletion(struct mapped_device *md)
264{
265 int r = 0;
266
267 spin_lock(&_minor_lock);
268
269 if (dm_open_count(md))
270 r = -EBUSY;
271 else
272 set_bit(DMF_DELETING, &md->flags);
273
274 spin_unlock(&_minor_lock);
275
276 return r;
277}
278
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800279static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
280{
281 struct mapped_device *md = bdev->bd_disk->private_data;
282
283 return dm_get_geometry(md, geo);
284}
285
Milan Brozaa129a22006-10-03 01:15:15 -0700286static int dm_blk_ioctl(struct inode *inode, struct file *file,
287 unsigned int cmd, unsigned long arg)
288{
289 struct mapped_device *md;
290 struct dm_table *map;
291 struct dm_target *tgt;
292 int r = -ENOTTY;
293
294 /* We don't really need this lock, but we do need 'inode'. */
295 unlock_kernel();
296
297 md = inode->i_bdev->bd_disk->private_data;
298
299 map = dm_get_table(md);
300
301 if (!map || !dm_table_get_size(map))
302 goto out;
303
304 /* We only support devices that have a single target */
305 if (dm_table_get_num_targets(map) != 1)
306 goto out;
307
308 tgt = dm_table_get_target(map, 0);
309
310 if (dm_suspended(md)) {
311 r = -EAGAIN;
312 goto out;
313 }
314
315 if (tgt->type->ioctl)
316 r = tgt->type->ioctl(tgt, inode, file, cmd, arg);
317
318out:
319 dm_table_put(map);
320
321 lock_kernel();
322 return r;
323}
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325static inline struct dm_io *alloc_io(struct mapped_device *md)
326{
327 return mempool_alloc(md->io_pool, GFP_NOIO);
328}
329
330static inline void free_io(struct mapped_device *md, struct dm_io *io)
331{
332 mempool_free(io, md->io_pool);
333}
334
335static inline struct target_io *alloc_tio(struct mapped_device *md)
336{
337 return mempool_alloc(md->tio_pool, GFP_NOIO);
338}
339
340static inline void free_tio(struct mapped_device *md, struct target_io *tio)
341{
342 mempool_free(tio, md->tio_pool);
343}
344
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800345static void start_io_acct(struct dm_io *io)
346{
347 struct mapped_device *md = io->md;
348
349 io->start_time = jiffies;
350
351 preempt_disable();
352 disk_round_stats(dm_disk(md));
353 preempt_enable();
354 dm_disk(md)->in_flight = atomic_inc_return(&md->pending);
355}
356
357static int end_io_acct(struct dm_io *io)
358{
359 struct mapped_device *md = io->md;
360 struct bio *bio = io->bio;
361 unsigned long duration = jiffies - io->start_time;
362 int pending;
363 int rw = bio_data_dir(bio);
364
365 preempt_disable();
366 disk_round_stats(dm_disk(md));
367 preempt_enable();
368 dm_disk(md)->in_flight = pending = atomic_dec_return(&md->pending);
369
370 disk_stat_add(dm_disk(md), ticks[rw], duration);
371
372 return !pending;
373}
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375/*
376 * Add the bio to the list of deferred io.
377 */
378static int queue_io(struct mapped_device *md, struct bio *bio)
379{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700380 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700383 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return 1;
385 }
386
387 bio_list_add(&md->deferred, bio);
388
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700389 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return 0; /* deferred successfully */
391}
392
393/*
394 * Everyone (including functions in this file), should use this
395 * function to access the md->map field, and make sure they call
396 * dm_table_put() when finished.
397 */
398struct dm_table *dm_get_table(struct mapped_device *md)
399{
400 struct dm_table *t;
401
402 read_lock(&md->map_lock);
403 t = md->map;
404 if (t)
405 dm_table_get(t);
406 read_unlock(&md->map_lock);
407
408 return t;
409}
410
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800411/*
412 * Get the geometry associated with a dm device
413 */
414int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
415{
416 *geo = md->geometry;
417
418 return 0;
419}
420
421/*
422 * Set the geometry of a device.
423 */
424int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
425{
426 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
427
428 if (geo->start > sz) {
429 DMWARN("Start sector is beyond the geometry limits.");
430 return -EINVAL;
431 }
432
433 md->geometry = *geo;
434
435 return 0;
436}
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438/*-----------------------------------------------------------------
439 * CRUD START:
440 * A more elegant soln is in the works that uses the queue
441 * merge fn, unfortunately there are a couple of changes to
442 * the block layer that I want to make for this. So in the
443 * interests of getting something for people to use I give
444 * you this clearly demarcated crap.
445 *---------------------------------------------------------------*/
446
447/*
448 * Decrements the number of outstanding ios that a bio has been
449 * cloned into, completing the original io if necc.
450 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800451static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 if (error)
454 io->error = error;
455
456 if (atomic_dec_and_test(&io->io_count)) {
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800457 if (end_io_acct(io))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 /* nudge anyone waiting on suspend queue */
459 wake_up(&io->md->wait);
460
Jens Axboe2056a782006-03-23 20:00:26 +0100461 blk_add_trace_bio(io->md->queue, io->bio, BLK_TA_COMPLETE);
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 bio_endio(io->bio, io->bio->bi_size, io->error);
464 free_io(io->md, io);
465 }
466}
467
468static int clone_endio(struct bio *bio, unsigned int done, int error)
469{
470 int r = 0;
471 struct target_io *tio = bio->bi_private;
Stefan Bader9faf4002006-10-03 01:15:41 -0700472 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 dm_endio_fn endio = tio->ti->type->end_io;
474
475 if (bio->bi_size)
476 return 1;
477
478 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
479 error = -EIO;
480
481 if (endio) {
482 r = endio(tio->ti, bio, error, &tio->info);
483 if (r < 0)
484 error = r;
485
486 else if (r > 0)
487 /* the target wants another shot at the io */
488 return 1;
489 }
490
Stefan Bader9faf4002006-10-03 01:15:41 -0700491 dec_pending(tio->io, error);
492
493 /*
494 * Store md for cleanup instead of tio which is about to get freed.
495 */
496 bio->bi_private = md->bs;
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 bio_put(bio);
Stefan Bader9faf4002006-10-03 01:15:41 -0700499 free_tio(md, tio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return r;
501}
502
503static sector_t max_io_len(struct mapped_device *md,
504 sector_t sector, struct dm_target *ti)
505{
506 sector_t offset = sector - ti->begin;
507 sector_t len = ti->len - offset;
508
509 /*
510 * Does the target need to split even further ?
511 */
512 if (ti->split_io) {
513 sector_t boundary;
514 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
515 - offset;
516 if (len > boundary)
517 len = boundary;
518 }
519
520 return len;
521}
522
523static void __map_bio(struct dm_target *ti, struct bio *clone,
524 struct target_io *tio)
525{
526 int r;
Jens Axboe2056a782006-03-23 20:00:26 +0100527 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -0700528 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 /*
531 * Sanity checks.
532 */
533 BUG_ON(!clone->bi_size);
534
535 clone->bi_end_io = clone_endio;
536 clone->bi_private = tio;
537
538 /*
539 * Map the clone. If r == 0 we don't need to do
540 * anything, the target has assumed ownership of
541 * this io.
542 */
543 atomic_inc(&tio->io->io_count);
Jens Axboe2056a782006-03-23 20:00:26 +0100544 sector = clone->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 r = ti->type->map(ti, clone, &tio->info);
Jens Axboe2056a782006-03-23 20:00:26 +0100546 if (r > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +0100548
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700549 blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone,
550 tio->io->bio->bi_bdev->bd_dev, sector,
Jens Axboe2056a782006-03-23 20:00:26 +0100551 clone->bi_sector);
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 generic_make_request(clone);
Jens Axboe2056a782006-03-23 20:00:26 +0100554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 else if (r < 0) {
557 /* error the io and bail out */
Stefan Bader9faf4002006-10-03 01:15:41 -0700558 md = tio->io->md;
559 dec_pending(tio->io, r);
560 /*
561 * Store bio_set for cleanup.
562 */
563 clone->bi_private = md->bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 bio_put(clone);
Stefan Bader9faf4002006-10-03 01:15:41 -0700565 free_tio(md, tio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567}
568
569struct clone_info {
570 struct mapped_device *md;
571 struct dm_table *map;
572 struct bio *bio;
573 struct dm_io *io;
574 sector_t sector;
575 sector_t sector_count;
576 unsigned short idx;
577};
578
Peter Osterlund36763472005-09-06 15:16:42 -0700579static void dm_bio_destructor(struct bio *bio)
580{
Stefan Bader9faf4002006-10-03 01:15:41 -0700581 struct bio_set *bs = bio->bi_private;
582
583 bio_free(bio, bs);
Peter Osterlund36763472005-09-06 15:16:42 -0700584}
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586/*
587 * Creates a little bio that is just does part of a bvec.
588 */
589static struct bio *split_bvec(struct bio *bio, sector_t sector,
590 unsigned short idx, unsigned int offset,
Stefan Bader9faf4002006-10-03 01:15:41 -0700591 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 struct bio *clone;
594 struct bio_vec *bv = bio->bi_io_vec + idx;
595
Stefan Bader9faf4002006-10-03 01:15:41 -0700596 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
Peter Osterlund36763472005-09-06 15:16:42 -0700597 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 *clone->bi_io_vec = *bv;
599
600 clone->bi_sector = sector;
601 clone->bi_bdev = bio->bi_bdev;
602 clone->bi_rw = bio->bi_rw;
603 clone->bi_vcnt = 1;
604 clone->bi_size = to_bytes(len);
605 clone->bi_io_vec->bv_offset = offset;
606 clone->bi_io_vec->bv_len = clone->bi_size;
607
608 return clone;
609}
610
611/*
612 * Creates a bio that consists of range of complete bvecs.
613 */
614static struct bio *clone_bio(struct bio *bio, sector_t sector,
615 unsigned short idx, unsigned short bv_count,
Stefan Bader9faf4002006-10-03 01:15:41 -0700616 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
618 struct bio *clone;
619
Stefan Bader9faf4002006-10-03 01:15:41 -0700620 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
621 __bio_clone(clone, bio);
622 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 clone->bi_sector = sector;
624 clone->bi_idx = idx;
625 clone->bi_vcnt = idx + bv_count;
626 clone->bi_size = to_bytes(len);
627 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
628
629 return clone;
630}
631
632static void __clone_and_map(struct clone_info *ci)
633{
634 struct bio *clone, *bio = ci->bio;
635 struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
636 sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
637 struct target_io *tio;
638
639 /*
640 * Allocate a target io object.
641 */
642 tio = alloc_tio(ci->md);
643 tio->io = ci->io;
644 tio->ti = ti;
645 memset(&tio->info, 0, sizeof(tio->info));
646
647 if (ci->sector_count <= max) {
648 /*
649 * Optimise for the simple case where we can do all of
650 * the remaining io with a single clone.
651 */
652 clone = clone_bio(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -0700653 bio->bi_vcnt - ci->idx, ci->sector_count,
654 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 __map_bio(ti, clone, tio);
656 ci->sector_count = 0;
657
658 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
659 /*
660 * There are some bvecs that don't span targets.
661 * Do as many of these as possible.
662 */
663 int i;
664 sector_t remaining = max;
665 sector_t bv_len;
666
667 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
668 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
669
670 if (bv_len > remaining)
671 break;
672
673 remaining -= bv_len;
674 len += bv_len;
675 }
676
Stefan Bader9faf4002006-10-03 01:15:41 -0700677 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
678 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 __map_bio(ti, clone, tio);
680
681 ci->sector += len;
682 ci->sector_count -= len;
683 ci->idx = i;
684
685 } else {
686 /*
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800687 * Handle a bvec that must be split between two or more targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 */
689 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800690 sector_t remaining = to_sector(bv->bv_len);
691 unsigned int offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800693 do {
694 if (offset) {
695 ti = dm_table_find_target(ci->map, ci->sector);
696 max = max_io_len(ci->md, ci->sector, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800698 tio = alloc_tio(ci->md);
699 tio->io = ci->io;
700 tio->ti = ti;
701 memset(&tio->info, 0, sizeof(tio->info));
702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800704 len = min(remaining, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800706 clone = split_bvec(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -0700707 bv->bv_offset + offset, len,
708 ci->md->bs);
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800709
710 __map_bio(ti, clone, tio);
711
712 ci->sector += len;
713 ci->sector_count -= len;
714 offset += to_bytes(len);
715 } while (remaining -= len);
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 ci->idx++;
718 }
719}
720
721/*
722 * Split the bio into several clones.
723 */
724static void __split_bio(struct mapped_device *md, struct bio *bio)
725{
726 struct clone_info ci;
727
728 ci.map = dm_get_table(md);
729 if (!ci.map) {
730 bio_io_error(bio, bio->bi_size);
731 return;
732 }
733
734 ci.md = md;
735 ci.bio = bio;
736 ci.io = alloc_io(md);
737 ci.io->error = 0;
738 atomic_set(&ci.io->io_count, 1);
739 ci.io->bio = bio;
740 ci.io->md = md;
741 ci.sector = bio->bi_sector;
742 ci.sector_count = bio_sectors(bio);
743 ci.idx = bio->bi_idx;
744
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800745 start_io_acct(ci.io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 while (ci.sector_count)
747 __clone_and_map(&ci);
748
749 /* drop the extra reference count */
750 dec_pending(ci.io, 0);
751 dm_table_put(ci.map);
752}
753/*-----------------------------------------------------------------
754 * CRUD END
755 *---------------------------------------------------------------*/
756
757/*
758 * The request function that just remaps the bio built up by
759 * dm_merge_bvec.
760 */
761static int dm_request(request_queue_t *q, struct bio *bio)
762{
763 int r;
Kevin Corry12f03a42006-02-01 03:04:52 -0800764 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 struct mapped_device *md = q->queuedata;
766
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700767 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Kevin Corry12f03a42006-02-01 03:04:52 -0800769 disk_stat_inc(dm_disk(md), ios[rw]);
770 disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio));
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 /*
773 * If we're suspended we have to queue
774 * this io for later.
775 */
776 while (test_bit(DMF_BLOCK_IO, &md->flags)) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700777 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 if (bio_rw(bio) == READA) {
780 bio_io_error(bio, bio->bi_size);
781 return 0;
782 }
783
784 r = queue_io(md, bio);
785 if (r < 0) {
786 bio_io_error(bio, bio->bi_size);
787 return 0;
788
789 } else if (r == 0)
790 return 0; /* deferred successfully */
791
792 /*
793 * We're in a while loop, because someone could suspend
794 * before we get to the following read lock.
795 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700796 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
798
799 __split_bio(md, bio);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700800 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return 0;
802}
803
804static int dm_flush_all(request_queue_t *q, struct gendisk *disk,
805 sector_t *error_sector)
806{
807 struct mapped_device *md = q->queuedata;
808 struct dm_table *map = dm_get_table(md);
809 int ret = -ENXIO;
810
811 if (map) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -0700812 ret = dm_table_flush_all(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 dm_table_put(map);
814 }
815
816 return ret;
817}
818
819static void dm_unplug_all(request_queue_t *q)
820{
821 struct mapped_device *md = q->queuedata;
822 struct dm_table *map = dm_get_table(md);
823
824 if (map) {
825 dm_table_unplug_all(map);
826 dm_table_put(map);
827 }
828}
829
830static int dm_any_congested(void *congested_data, int bdi_bits)
831{
832 int r;
833 struct mapped_device *md = (struct mapped_device *) congested_data;
834 struct dm_table *map = dm_get_table(md);
835
836 if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
837 r = bdi_bits;
838 else
839 r = dm_table_any_congested(map, bdi_bits);
840
841 dm_table_put(map);
842 return r;
843}
844
845/*-----------------------------------------------------------------
846 * An IDR is used to keep track of allocated minor numbers.
847 *---------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848static DEFINE_IDR(_minor_idr);
849
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700850static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -0700852 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -0700854 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
857/*
858 * See if the device with a specific minor # is free.
859 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700860static int specific_minor(struct mapped_device *md, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
862 int r, m;
863
864 if (minor >= (1 << MINORBITS))
865 return -EINVAL;
866
Jeff Mahoney62f75c22006-06-26 00:27:21 -0700867 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
868 if (!r)
869 return -ENOMEM;
870
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -0700871 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 if (idr_find(&_minor_idr, minor)) {
874 r = -EBUSY;
875 goto out;
876 }
877
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700878 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
Jeff Mahoney62f75c22006-06-26 00:27:21 -0700879 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 if (m != minor) {
883 idr_remove(&_minor_idr, m);
884 r = -EBUSY;
885 goto out;
886 }
887
888out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -0700889 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return r;
891}
892
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700893static int next_free_minor(struct mapped_device *md, int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700895 int r, m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
Jeff Mahoney62f75c22006-06-26 00:27:21 -0700898 if (!r)
899 return -ENOMEM;
900
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -0700901 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700903 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (r) {
905 goto out;
906 }
907
908 if (m >= (1 << MINORBITS)) {
909 idr_remove(&_minor_idr, m);
910 r = -ENOSPC;
911 goto out;
912 }
913
914 *minor = m;
915
916out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -0700917 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return r;
919}
920
921static struct block_device_operations dm_blk_dops;
922
923/*
924 * Allocate and initialise a blank device with a given minor.
925 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700926static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
928 int r;
929 struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700930 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 if (!md) {
933 DMWARN("unable to allocate device, out of memory.");
934 return NULL;
935 }
936
Jeff Mahoney10da4f72006-06-26 00:27:25 -0700937 if (!try_module_get(THIS_MODULE))
938 goto bad0;
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700941 if (minor == DM_ANY_MINOR)
942 r = next_free_minor(md, &minor);
943 else
944 r = specific_minor(md, minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if (r < 0)
946 goto bad1;
947
948 memset(md, 0, sizeof(*md));
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700949 init_rwsem(&md->io_lock);
950 init_MUTEX(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 rwlock_init(&md->map_lock);
952 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700953 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 atomic_set(&md->event_nr, 0);
955
956 md->queue = blk_alloc_queue(GFP_KERNEL);
957 if (!md->queue)
Ishai Rabinovitze3f6ac62006-10-03 01:15:22 -0700958 goto bad1_free_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 md->queue->queuedata = md;
961 md->queue->backing_dev_info.congested_fn = dm_any_congested;
962 md->queue->backing_dev_info.congested_data = md;
963 blk_queue_make_request(md->queue, dm_request);
Jens Axboedaef2652006-01-10 10:48:02 +0100964 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 md->queue->unplug_fn = dm_unplug_all;
966 md->queue->issue_flush_fn = dm_flush_all;
967
Matthew Dobson93d23412006-03-26 01:37:50 -0800968 md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (!md->io_pool)
970 goto bad2;
971
Matthew Dobson93d23412006-03-26 01:37:50 -0800972 md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (!md->tio_pool)
974 goto bad3;
975
Stefan Bader9faf4002006-10-03 01:15:41 -0700976 md->bs = bioset_create(16, 16, 4);
977 if (!md->bs)
978 goto bad_no_bioset;
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 md->disk = alloc_disk(1);
981 if (!md->disk)
982 goto bad4;
983
Jeff Mahoneyf0b04112006-06-26 00:27:25 -0700984 atomic_set(&md->pending, 0);
985 init_waitqueue_head(&md->wait);
986 init_waitqueue_head(&md->eventq);
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 md->disk->major = _major;
989 md->disk->first_minor = minor;
990 md->disk->fops = &dm_blk_dops;
991 md->disk->queue = md->queue;
992 md->disk->private_data = md;
993 sprintf(md->disk->disk_name, "dm-%d", minor);
994 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -0800995 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700997 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -0700998 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700999 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001000 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001001
1002 BUG_ON(old_md != MINOR_ALLOCED);
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return md;
1005
1006 bad4:
Stefan Bader9faf4002006-10-03 01:15:41 -07001007 bioset_free(md->bs);
1008 bad_no_bioset:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 mempool_destroy(md->tio_pool);
1010 bad3:
1011 mempool_destroy(md->io_pool);
1012 bad2:
Al Viro1312f402006-03-12 11:02:03 -05001013 blk_cleanup_queue(md->queue);
Ishai Rabinovitze3f6ac62006-10-03 01:15:22 -07001014 bad1_free_minor:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 free_minor(minor);
1016 bad1:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001017 module_put(THIS_MODULE);
1018 bad0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 kfree(md);
1020 return NULL;
1021}
1022
1023static void free_dev(struct mapped_device *md)
1024{
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001025 int minor = md->disk->first_minor;
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001026
Jun'ichi Nomurad9dde592006-02-24 13:04:24 -08001027 if (md->suspended_bdev) {
1028 thaw_bdev(md->suspended_bdev, NULL);
1029 bdput(md->suspended_bdev);
1030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 mempool_destroy(md->tio_pool);
1032 mempool_destroy(md->io_pool);
Stefan Bader9faf4002006-10-03 01:15:41 -07001033 bioset_free(md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 del_gendisk(md->disk);
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001035 free_minor(minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001036
1037 spin_lock(&_minor_lock);
1038 md->disk->private_data = NULL;
1039 spin_unlock(&_minor_lock);
1040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 put_disk(md->disk);
Al Viro1312f402006-03-12 11:02:03 -05001042 blk_cleanup_queue(md->queue);
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001043 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 kfree(md);
1045}
1046
1047/*
1048 * Bind a table to the device.
1049 */
1050static void event_callback(void *context)
1051{
1052 struct mapped_device *md = (struct mapped_device *) context;
1053
1054 atomic_inc(&md->event_nr);
1055 wake_up(&md->eventq);
1056}
1057
Alasdair G Kergon4e901882005-07-28 21:15:59 -07001058static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Alasdair G Kergon4e901882005-07-28 21:15:59 -07001060 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001062 mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001063 i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001064 mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
1067static int __bind(struct mapped_device *md, struct dm_table *t)
1068{
1069 request_queue_t *q = md->queue;
1070 sector_t size;
1071
1072 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001073
1074 /*
1075 * Wipe any geometry if the size of the table changed.
1076 */
1077 if (size != get_capacity(md->disk))
1078 memset(&md->geometry, 0, sizeof(md->geometry));
1079
Alasdair G Kergon4e901882005-07-28 21:15:59 -07001080 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (size == 0)
1082 return 0;
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 dm_table_get(t);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001085 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001086
1087 write_lock(&md->map_lock);
1088 md->map = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 dm_table_set_restrictions(t, q);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001090 write_unlock(&md->map_lock);
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return 0;
1093}
1094
1095static void __unbind(struct mapped_device *md)
1096{
1097 struct dm_table *map = md->map;
1098
1099 if (!map)
1100 return;
1101
1102 dm_table_event_callback(map, NULL, NULL);
1103 write_lock(&md->map_lock);
1104 md->map = NULL;
1105 write_unlock(&md->map_lock);
1106 dm_table_put(map);
1107}
1108
1109/*
1110 * Constructor for a new device.
1111 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001112int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
1114 struct mapped_device *md;
1115
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001116 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (!md)
1118 return -ENXIO;
1119
1120 *result = md;
1121 return 0;
1122}
1123
David Teigland637842c2006-01-06 00:20:00 -08001124static struct mapped_device *dm_find_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
1126 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 unsigned minor = MINOR(dev);
1128
1129 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
1130 return NULL;
1131
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001132 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 md = idr_find(&_minor_idr, minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001135 if (md && (md == MINOR_ALLOCED ||
1136 (dm_disk(md)->first_minor != minor) ||
Alasdair G Kergon17b2f662006-06-26 00:27:33 -07001137 test_bit(DMF_FREEING, &md->flags))) {
David Teigland637842c2006-01-06 00:20:00 -08001138 md = NULL;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001139 goto out;
1140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001142out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001143 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
David Teigland637842c2006-01-06 00:20:00 -08001145 return md;
1146}
1147
David Teiglandd229a952006-01-06 00:20:01 -08001148struct mapped_device *dm_get_md(dev_t dev)
1149{
1150 struct mapped_device *md = dm_find_md(dev);
1151
1152 if (md)
1153 dm_get(md);
1154
1155 return md;
1156}
1157
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08001158void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08001159{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08001160 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
1163void dm_set_mdptr(struct mapped_device *md, void *ptr)
1164{
1165 md->interface_ptr = ptr;
1166}
1167
1168void dm_get(struct mapped_device *md)
1169{
1170 atomic_inc(&md->holders);
1171}
1172
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001173const char *dm_device_name(struct mapped_device *md)
1174{
1175 return md->name;
1176}
1177EXPORT_SYMBOL_GPL(dm_device_name);
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179void dm_put(struct mapped_device *md)
1180{
Mike Anderson1134e5a2006-03-27 01:17:54 -08001181 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001183 BUG_ON(test_bit(DMF_FREEING, &md->flags));
1184
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001185 if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
Mike Anderson1134e5a2006-03-27 01:17:54 -08001186 map = dm_get_table(md);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001187 idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001188 set_bit(DMF_FREEING, &md->flags);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001189 spin_unlock(&_minor_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001190 if (!dm_suspended(md)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 dm_table_presuspend_targets(map);
1192 dm_table_postsuspend_targets(map);
1193 }
1194 __unbind(md);
Mike Anderson1134e5a2006-03-27 01:17:54 -08001195 dm_table_put(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 free_dev(md);
1197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198}
1199
1200/*
1201 * Process the deferred bios
1202 */
1203static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
1204{
1205 struct bio *n;
1206
1207 while (c) {
1208 n = c->bi_next;
1209 c->bi_next = NULL;
1210 __split_bio(md, c);
1211 c = n;
1212 }
1213}
1214
1215/*
1216 * Swap in a new table (destroying old one).
1217 */
1218int dm_swap_table(struct mapped_device *md, struct dm_table *table)
1219{
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001220 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001222 down(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 /* device must be suspended */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001225 if (!dm_suspended(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001226 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 __unbind(md);
1229 r = __bind(md, table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001231out:
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001232 up(&md->suspend_lock);
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001233 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
1236/*
1237 * Functions to lock and unlock any filesystem running on the
1238 * device.
1239 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001240static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001242 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07001245
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001246 md->frozen_sb = freeze_bdev(md->suspended_bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07001247 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001248 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001249 md->frozen_sb = NULL;
1250 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07001251 }
1252
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001253 set_bit(DMF_FROZEN, &md->flags);
1254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 /* don't bdput right now, we don't want the bdev
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001256 * to go away while it is locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 */
1258 return 0;
1259}
1260
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001261static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001263 if (!test_bit(DMF_FROZEN, &md->flags))
1264 return;
1265
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001266 thaw_bdev(md->suspended_bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001268 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269}
1270
1271/*
1272 * We need to be able to change a mapping table under a mounted
1273 * filesystem. For example we might want to move some data in
1274 * the background. Before the table can be swapped with
1275 * dm_bind_table, dm_suspend must be called to flush any in
1276 * flight bios and ensure that any further io gets deferred.
1277 */
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001278int dm_suspend(struct mapped_device *md, int do_lockfs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001280 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 DECLARE_WAITQUEUE(wait, current);
Jun'ichi Nomura1ecac7f2006-03-27 01:17:51 -08001282 struct bio *def;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001283 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001285 down(&md->suspend_lock);
1286
1287 if (dm_suspended(md))
Alasdair G Kergond2874832006-11-08 17:44:43 -08001288 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 map = dm_get_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001292 /* This does not get reverted if there's an error later. */
1293 dm_table_presuspend_targets(map);
1294
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001295 md->suspended_bdev = bdget_disk(md->disk, 0);
1296 if (!md->suspended_bdev) {
1297 DMWARN("bdget failed in dm_suspend");
1298 r = -ENOMEM;
1299 goto out;
1300 }
1301
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001302 /* Flush I/O to the device. */
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001303 if (do_lockfs) {
1304 r = lock_fs(md);
1305 if (r)
1306 goto out;
1307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 /*
Alasdair G Kergon354e0072005-05-05 16:16:05 -07001310 * First we set the BLOCK_IO flag so no more ios will be mapped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001312 down_write(&md->io_lock);
1313 set_bit(DMF_BLOCK_IO, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 add_wait_queue(&md->wait, &wait);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001316 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 /* unplug */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001319 if (map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 dm_table_unplug_all(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 /*
1323 * Then we wait for the already mapped ios to
1324 * complete.
1325 */
1326 while (1) {
1327 set_current_state(TASK_INTERRUPTIBLE);
1328
1329 if (!atomic_read(&md->pending) || signal_pending(current))
1330 break;
1331
1332 io_schedule();
1333 }
1334 set_current_state(TASK_RUNNING);
1335
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001336 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 remove_wait_queue(&md->wait, &wait);
1338
1339 /* were we interrupted ? */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001340 r = -EINTR;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001341 if (atomic_read(&md->pending)) {
Jun'ichi Nomura1ecac7f2006-03-27 01:17:51 -08001342 clear_bit(DMF_BLOCK_IO, &md->flags);
1343 def = bio_list_get(&md->deferred);
1344 __flush_deferred_io(md, def);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001345 up_write(&md->io_lock);
1346 unlock_fs(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001347 goto out;
1348 }
1349 up_write(&md->io_lock);
1350
1351 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 set_bit(DMF_SUSPENDED, &md->flags);
1354
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001355 r = 0;
1356
1357out:
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001358 if (r && md->suspended_bdev) {
1359 bdput(md->suspended_bdev);
1360 md->suspended_bdev = NULL;
1361 }
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 dm_table_put(map);
Alasdair G Kergond2874832006-11-08 17:44:43 -08001364
1365out_unlock:
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001366 up(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001367 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
1369
1370int dm_resume(struct mapped_device *md)
1371{
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001372 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 struct bio *def;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001374 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001376 down(&md->suspend_lock);
1377 if (!dm_suspended(md))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001378 goto out;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001379
1380 map = dm_get_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001381 if (!map || !dm_table_get_size(map))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001382 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Milan Broz8757b772006-10-03 01:15:36 -07001384 r = dm_table_resume_targets(map);
1385 if (r)
1386 goto out;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001387
1388 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 clear_bit(DMF_BLOCK_IO, &md->flags);
1390
1391 def = bio_list_get(&md->deferred);
1392 __flush_deferred_io(md, def);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001393 up_write(&md->io_lock);
1394
1395 unlock_fs(md);
1396
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001397 bdput(md->suspended_bdev);
1398 md->suspended_bdev = NULL;
1399
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001400 clear_bit(DMF_SUSPENDED, &md->flags);
1401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 dm_table_unplug_all(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Hannes Reinecke8560ed62006-10-03 01:15:35 -07001404 kobject_uevent(&md->disk->kobj, KOBJ_CHANGE);
1405
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001406 r = 0;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001407
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001408out:
1409 dm_table_put(map);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001410 up(&md->suspend_lock);
1411
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001412 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
1415/*-----------------------------------------------------------------
1416 * Event notification.
1417 *---------------------------------------------------------------*/
1418uint32_t dm_get_event_nr(struct mapped_device *md)
1419{
1420 return atomic_read(&md->event_nr);
1421}
1422
1423int dm_wait_event(struct mapped_device *md, int event_nr)
1424{
1425 return wait_event_interruptible(md->eventq,
1426 (event_nr != atomic_read(&md->event_nr)));
1427}
1428
1429/*
1430 * The gendisk is only valid as long as you have a reference
1431 * count on 'md'.
1432 */
1433struct gendisk *dm_disk(struct mapped_device *md)
1434{
1435 return md->disk;
1436}
1437
1438int dm_suspended(struct mapped_device *md)
1439{
1440 return test_bit(DMF_SUSPENDED, &md->flags);
1441}
1442
1443static struct block_device_operations dm_blk_dops = {
1444 .open = dm_blk_open,
1445 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07001446 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001447 .getgeo = dm_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 .owner = THIS_MODULE
1449};
1450
1451EXPORT_SYMBOL(dm_get_mapinfo);
1452
1453/*
1454 * module hooks
1455 */
1456module_init(dm_init);
1457module_exit(dm_exit);
1458
1459module_param(major, uint, 0);
1460MODULE_PARM_DESC(major, "The major number of the device mapper");
1461MODULE_DESCRIPTION(DM_NAME " driver");
1462MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1463MODULE_LICENSE("GPL");