]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/md/dm.c
dm: tidy alloc_dev labels
[net-next-2.6.git] / drivers / md / dm.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
2b06cfff 3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
1da177e4
LT
4 *
5 * This file is released under the GPL.
6 */
7
8#include "dm.h"
9#include "dm-bio-list.h"
51e5b2bd 10#include "dm-uevent.h"
1da177e4
LT
11
12#include <linux/init.h>
13#include <linux/module.h>
48c9c27b 14#include <linux/mutex.h>
1da177e4
LT
15#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>
3ac51e74 22#include <linux/hdreg.h>
2056a782 23#include <linux/blktrace_api.h>
aa129a22 24#include <linux/smp_lock.h>
1da177e4 25
72d94861
AK
26#define DM_MSG_PREFIX "core"
27
1da177e4
LT
28static const char *_name = DM_NAME;
29
30static unsigned int major = 0;
31static unsigned int _major = 0;
32
f32c10b0 33static DEFINE_SPINLOCK(_minor_lock);
1da177e4
LT
34/*
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;
3eaf840e 42 unsigned long start_time;
1da177e4
LT
43};
44
45/*
46 * One of these is allocated per target within a bio. Hopefully
47 * this will be simplified out one day.
48 */
028867ac 49struct dm_target_io {
1da177e4
LT
50 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{
17b2f66f 57 if (bio && bio->bi_private)
028867ac 58 return &((struct dm_target_io *)bio->bi_private)->info;
17b2f66f 59 return NULL;
1da177e4
LT
60}
61
ba61fdd1
JM
62#define MINOR_ALLOCED ((void *)-1)
63
1da177e4
LT
64/*
65 * Bits for the md->flags field.
66 */
67#define DMF_BLOCK_IO 0
68#define DMF_SUSPENDED 1
aa8d7c2f 69#define DMF_FROZEN 2
fba9f90e 70#define DMF_FREEING 3
5c6bd75d 71#define DMF_DELETING 4
2e93ccc1 72#define DMF_NOFLUSH_SUSPENDING 5
1da177e4
LT
73
74struct mapped_device {
2ca3310e 75 struct rw_semaphore io_lock;
e61290a4 76 struct mutex suspend_lock;
2e93ccc1 77 spinlock_t pushback_lock;
1da177e4
LT
78 rwlock_t map_lock;
79 atomic_t holders;
5c6bd75d 80 atomic_t open_count;
1da177e4
LT
81
82 unsigned long flags;
83
165125e1 84 struct request_queue *queue;
1da177e4 85 struct gendisk *disk;
7e51f257 86 char name[16];
1da177e4
LT
87
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;
74859364 95 struct bio_list deferred;
2e93ccc1 96 struct bio_list pushback;
1da177e4
LT
97
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
9faf400f
SB
109 struct bio_set *bs;
110
1da177e4
LT
111 /*
112 * Event handling.
113 */
114 atomic_t event_nr;
115 wait_queue_head_t eventq;
7a8c3d3b
MA
116 atomic_t uevent_seq;
117 struct list_head uevent_list;
118 spinlock_t uevent_lock; /* Protect access to uevent_list */
1da177e4
LT
119
120 /*
121 * freeze/thaw support require holding onto a super block
122 */
123 struct super_block *frozen_sb;
e39e2e95 124 struct block_device *suspended_bdev;
3ac51e74
DW
125
126 /* forced geometry settings */
127 struct hd_geometry geometry;
1da177e4
LT
128};
129
130#define MIN_IOS 256
e18b890b
CL
131static struct kmem_cache *_io_cache;
132static struct kmem_cache *_tio_cache;
1da177e4 133
1da177e4
LT
134static int __init local_init(void)
135{
136 int r;
137
1da177e4 138 /* allocate a slab for the dm_ios */
028867ac 139 _io_cache = KMEM_CACHE(dm_io, 0);
1da177e4
LT
140 if (!_io_cache)
141 return -ENOMEM;
142
143 /* allocate a slab for the target ios */
028867ac 144 _tio_cache = KMEM_CACHE(dm_target_io, 0);
1da177e4
LT
145 if (!_tio_cache) {
146 kmem_cache_destroy(_io_cache);
147 return -ENOMEM;
148 }
149
51e5b2bd
MA
150 r = dm_uevent_init();
151 if (r) {
152 kmem_cache_destroy(_tio_cache);
153 kmem_cache_destroy(_io_cache);
154 return r;
155 }
156
1da177e4
LT
157 _major = major;
158 r = register_blkdev(_major, _name);
159 if (r < 0) {
160 kmem_cache_destroy(_tio_cache);
161 kmem_cache_destroy(_io_cache);
51e5b2bd 162 dm_uevent_exit();
1da177e4
LT
163 return r;
164 }
165
166 if (!_major)
167 _major = r;
168
169 return 0;
170}
171
172static void local_exit(void)
173{
174 kmem_cache_destroy(_tio_cache);
175 kmem_cache_destroy(_io_cache);
00d59405 176 unregister_blkdev(_major, _name);
51e5b2bd 177 dm_uevent_exit();
1da177e4
LT
178
179 _major = 0;
180
181 DMINFO("cleaned up");
182}
183
b9249e55 184static int (*_inits[])(void) __initdata = {
1da177e4
LT
185 local_init,
186 dm_target_init,
187 dm_linear_init,
188 dm_stripe_init,
189 dm_interface_init,
190};
191
b9249e55 192static void (*_exits[])(void) = {
1da177e4
LT
193 local_exit,
194 dm_target_exit,
195 dm_linear_exit,
196 dm_stripe_exit,
197 dm_interface_exit,
198};
199
200static int __init dm_init(void)
201{
202 const int count = ARRAY_SIZE(_inits);
203
204 int r, i;
205
206 for (i = 0; i < count; i++) {
207 r = _inits[i]();
208 if (r)
209 goto bad;
210 }
211
212 return 0;
213
214 bad:
215 while (i--)
216 _exits[i]();
217
218 return r;
219}
220
221static void __exit dm_exit(void)
222{
223 int i = ARRAY_SIZE(_exits);
224
225 while (i--)
226 _exits[i]();
227}
228
229/*
230 * Block device functions
231 */
232static int dm_blk_open(struct inode *inode, struct file *file)
233{
234 struct mapped_device *md;
235
fba9f90e
JM
236 spin_lock(&_minor_lock);
237
1da177e4 238 md = inode->i_bdev->bd_disk->private_data;
fba9f90e
JM
239 if (!md)
240 goto out;
241
5c6bd75d
AK
242 if (test_bit(DMF_FREEING, &md->flags) ||
243 test_bit(DMF_DELETING, &md->flags)) {
fba9f90e
JM
244 md = NULL;
245 goto out;
246 }
247
1da177e4 248 dm_get(md);
5c6bd75d 249 atomic_inc(&md->open_count);
fba9f90e
JM
250
251out:
252 spin_unlock(&_minor_lock);
253
254 return md ? 0 : -ENXIO;
1da177e4
LT
255}
256
257static int dm_blk_close(struct inode *inode, struct file *file)
258{
259 struct mapped_device *md;
260
261 md = inode->i_bdev->bd_disk->private_data;
5c6bd75d 262 atomic_dec(&md->open_count);
1da177e4
LT
263 dm_put(md);
264 return 0;
265}
266
5c6bd75d
AK
267int dm_open_count(struct mapped_device *md)
268{
269 return atomic_read(&md->open_count);
270}
271
272/*
273 * Guarantees nothing is using the device before it's deleted.
274 */
275int dm_lock_for_deletion(struct mapped_device *md)
276{
277 int r = 0;
278
279 spin_lock(&_minor_lock);
280
281 if (dm_open_count(md))
282 r = -EBUSY;
283 else
284 set_bit(DMF_DELETING, &md->flags);
285
286 spin_unlock(&_minor_lock);
287
288 return r;
289}
290
3ac51e74
DW
291static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
292{
293 struct mapped_device *md = bdev->bd_disk->private_data;
294
295 return dm_get_geometry(md, geo);
296}
297
aa129a22
MB
298static int dm_blk_ioctl(struct inode *inode, struct file *file,
299 unsigned int cmd, unsigned long arg)
300{
301 struct mapped_device *md;
302 struct dm_table *map;
303 struct dm_target *tgt;
304 int r = -ENOTTY;
305
306 /* We don't really need this lock, but we do need 'inode'. */
307 unlock_kernel();
308
309 md = inode->i_bdev->bd_disk->private_data;
310
311 map = dm_get_table(md);
312
313 if (!map || !dm_table_get_size(map))
314 goto out;
315
316 /* We only support devices that have a single target */
317 if (dm_table_get_num_targets(map) != 1)
318 goto out;
319
320 tgt = dm_table_get_target(map, 0);
321
322 if (dm_suspended(md)) {
323 r = -EAGAIN;
324 goto out;
325 }
326
327 if (tgt->type->ioctl)
328 r = tgt->type->ioctl(tgt, inode, file, cmd, arg);
329
330out:
331 dm_table_put(map);
332
333 lock_kernel();
334 return r;
335}
336
028867ac 337static struct dm_io *alloc_io(struct mapped_device *md)
1da177e4
LT
338{
339 return mempool_alloc(md->io_pool, GFP_NOIO);
340}
341
028867ac 342static void free_io(struct mapped_device *md, struct dm_io *io)
1da177e4
LT
343{
344 mempool_free(io, md->io_pool);
345}
346
028867ac 347static struct dm_target_io *alloc_tio(struct mapped_device *md)
1da177e4
LT
348{
349 return mempool_alloc(md->tio_pool, GFP_NOIO);
350}
351
028867ac 352static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
1da177e4
LT
353{
354 mempool_free(tio, md->tio_pool);
355}
356
3eaf840e
JNN
357static void start_io_acct(struct dm_io *io)
358{
359 struct mapped_device *md = io->md;
360
361 io->start_time = jiffies;
362
363 preempt_disable();
364 disk_round_stats(dm_disk(md));
365 preempt_enable();
366 dm_disk(md)->in_flight = atomic_inc_return(&md->pending);
367}
368
369static int end_io_acct(struct dm_io *io)
370{
371 struct mapped_device *md = io->md;
372 struct bio *bio = io->bio;
373 unsigned long duration = jiffies - io->start_time;
374 int pending;
375 int rw = bio_data_dir(bio);
376
377 preempt_disable();
378 disk_round_stats(dm_disk(md));
379 preempt_enable();
380 dm_disk(md)->in_flight = pending = atomic_dec_return(&md->pending);
381
382 disk_stat_add(dm_disk(md), ticks[rw], duration);
383
384 return !pending;
385}
386
1da177e4
LT
387/*
388 * Add the bio to the list of deferred io.
389 */
390static int queue_io(struct mapped_device *md, struct bio *bio)
391{
2ca3310e 392 down_write(&md->io_lock);
1da177e4
LT
393
394 if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
2ca3310e 395 up_write(&md->io_lock);
1da177e4
LT
396 return 1;
397 }
398
399 bio_list_add(&md->deferred, bio);
400
2ca3310e 401 up_write(&md->io_lock);
1da177e4
LT
402 return 0; /* deferred successfully */
403}
404
405/*
406 * Everyone (including functions in this file), should use this
407 * function to access the md->map field, and make sure they call
408 * dm_table_put() when finished.
409 */
410struct dm_table *dm_get_table(struct mapped_device *md)
411{
412 struct dm_table *t;
413
414 read_lock(&md->map_lock);
415 t = md->map;
416 if (t)
417 dm_table_get(t);
418 read_unlock(&md->map_lock);
419
420 return t;
421}
422
3ac51e74
DW
423/*
424 * Get the geometry associated with a dm device
425 */
426int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
427{
428 *geo = md->geometry;
429
430 return 0;
431}
432
433/*
434 * Set the geometry of a device.
435 */
436int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
437{
438 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
439
440 if (geo->start > sz) {
441 DMWARN("Start sector is beyond the geometry limits.");
442 return -EINVAL;
443 }
444
445 md->geometry = *geo;
446
447 return 0;
448}
449
1da177e4
LT
450/*-----------------------------------------------------------------
451 * CRUD START:
452 * A more elegant soln is in the works that uses the queue
453 * merge fn, unfortunately there are a couple of changes to
454 * the block layer that I want to make for this. So in the
455 * interests of getting something for people to use I give
456 * you this clearly demarcated crap.
457 *---------------------------------------------------------------*/
458
2e93ccc1
KU
459static int __noflush_suspending(struct mapped_device *md)
460{
461 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
462}
463
1da177e4
LT
464/*
465 * Decrements the number of outstanding ios that a bio has been
466 * cloned into, completing the original io if necc.
467 */
858119e1 468static void dec_pending(struct dm_io *io, int error)
1da177e4 469{
2e93ccc1
KU
470 unsigned long flags;
471
472 /* Push-back supersedes any I/O errors */
473 if (error && !(io->error > 0 && __noflush_suspending(io->md)))
1da177e4
LT
474 io->error = error;
475
476 if (atomic_dec_and_test(&io->io_count)) {
2e93ccc1
KU
477 if (io->error == DM_ENDIO_REQUEUE) {
478 /*
479 * Target requested pushing back the I/O.
480 * This must be handled before the sleeper on
481 * suspend queue merges the pushback list.
482 */
483 spin_lock_irqsave(&io->md->pushback_lock, flags);
484 if (__noflush_suspending(io->md))
485 bio_list_add(&io->md->pushback, io->bio);
486 else
487 /* noflush suspend was interrupted. */
488 io->error = -EIO;
489 spin_unlock_irqrestore(&io->md->pushback_lock, flags);
490 }
491
3eaf840e 492 if (end_io_acct(io))
1da177e4
LT
493 /* nudge anyone waiting on suspend queue */
494 wake_up(&io->md->wait);
495
2e93ccc1
KU
496 if (io->error != DM_ENDIO_REQUEUE) {
497 blk_add_trace_bio(io->md->queue, io->bio,
498 BLK_TA_COMPLETE);
499
6712ecf8 500 bio_endio(io->bio, io->error);
2e93ccc1 501 }
2056a782 502
1da177e4
LT
503 free_io(io->md, io);
504 }
505}
506
6712ecf8 507static void clone_endio(struct bio *bio, int error)
1da177e4
LT
508{
509 int r = 0;
028867ac 510 struct dm_target_io *tio = bio->bi_private;
9faf400f 511 struct mapped_device *md = tio->io->md;
1da177e4
LT
512 dm_endio_fn endio = tio->ti->type->end_io;
513
1da177e4
LT
514 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
515 error = -EIO;
516
517 if (endio) {
518 r = endio(tio->ti, bio, error, &tio->info);
2e93ccc1
KU
519 if (r < 0 || r == DM_ENDIO_REQUEUE)
520 /*
521 * error and requeue request are handled
522 * in dec_pending().
523 */
1da177e4 524 error = r;
45cbcd79
KU
525 else if (r == DM_ENDIO_INCOMPLETE)
526 /* The target will handle the io */
6712ecf8 527 return;
45cbcd79
KU
528 else if (r) {
529 DMWARN("unimplemented target endio return value: %d", r);
530 BUG();
531 }
1da177e4
LT
532 }
533
9faf400f
SB
534 dec_pending(tio->io, error);
535
536 /*
537 * Store md for cleanup instead of tio which is about to get freed.
538 */
539 bio->bi_private = md->bs;
540
1da177e4 541 bio_put(bio);
9faf400f 542 free_tio(md, tio);
1da177e4
LT
543}
544
545static sector_t max_io_len(struct mapped_device *md,
546 sector_t sector, struct dm_target *ti)
547{
548 sector_t offset = sector - ti->begin;
549 sector_t len = ti->len - offset;
550
551 /*
552 * Does the target need to split even further ?
553 */
554 if (ti->split_io) {
555 sector_t boundary;
556 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
557 - offset;
558 if (len > boundary)
559 len = boundary;
560 }
561
562 return len;
563}
564
565static void __map_bio(struct dm_target *ti, struct bio *clone,
028867ac 566 struct dm_target_io *tio)
1da177e4
LT
567{
568 int r;
2056a782 569 sector_t sector;
9faf400f 570 struct mapped_device *md;
1da177e4
LT
571
572 /*
573 * Sanity checks.
574 */
575 BUG_ON(!clone->bi_size);
576
577 clone->bi_end_io = clone_endio;
578 clone->bi_private = tio;
579
580 /*
581 * Map the clone. If r == 0 we don't need to do
582 * anything, the target has assumed ownership of
583 * this io.
584 */
585 atomic_inc(&tio->io->io_count);
2056a782 586 sector = clone->bi_sector;
1da177e4 587 r = ti->type->map(ti, clone, &tio->info);
45cbcd79 588 if (r == DM_MAPIO_REMAPPED) {
1da177e4 589 /* the bio has been remapped so dispatch it */
2056a782 590
17b2f66f 591 blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone,
c7149d6b
AB
592 tio->io->bio->bi_bdev->bd_dev,
593 clone->bi_sector, sector);
2056a782 594
1da177e4 595 generic_make_request(clone);
2e93ccc1
KU
596 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
597 /* error the io and bail out, or requeue it if needed */
9faf400f
SB
598 md = tio->io->md;
599 dec_pending(tio->io, r);
600 /*
601 * Store bio_set for cleanup.
602 */
603 clone->bi_private = md->bs;
1da177e4 604 bio_put(clone);
9faf400f 605 free_tio(md, tio);
45cbcd79
KU
606 } else if (r) {
607 DMWARN("unimplemented target map return value: %d", r);
608 BUG();
1da177e4
LT
609 }
610}
611
612struct clone_info {
613 struct mapped_device *md;
614 struct dm_table *map;
615 struct bio *bio;
616 struct dm_io *io;
617 sector_t sector;
618 sector_t sector_count;
619 unsigned short idx;
620};
621
3676347a
PO
622static void dm_bio_destructor(struct bio *bio)
623{
9faf400f
SB
624 struct bio_set *bs = bio->bi_private;
625
626 bio_free(bio, bs);
3676347a
PO
627}
628
1da177e4
LT
629/*
630 * Creates a little bio that is just does part of a bvec.
631 */
632static struct bio *split_bvec(struct bio *bio, sector_t sector,
633 unsigned short idx, unsigned int offset,
9faf400f 634 unsigned int len, struct bio_set *bs)
1da177e4
LT
635{
636 struct bio *clone;
637 struct bio_vec *bv = bio->bi_io_vec + idx;
638
9faf400f 639 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
3676347a 640 clone->bi_destructor = dm_bio_destructor;
1da177e4
LT
641 *clone->bi_io_vec = *bv;
642
643 clone->bi_sector = sector;
644 clone->bi_bdev = bio->bi_bdev;
645 clone->bi_rw = bio->bi_rw;
646 clone->bi_vcnt = 1;
647 clone->bi_size = to_bytes(len);
648 clone->bi_io_vec->bv_offset = offset;
649 clone->bi_io_vec->bv_len = clone->bi_size;
650
651 return clone;
652}
653
654/*
655 * Creates a bio that consists of range of complete bvecs.
656 */
657static struct bio *clone_bio(struct bio *bio, sector_t sector,
658 unsigned short idx, unsigned short bv_count,
9faf400f 659 unsigned int len, struct bio_set *bs)
1da177e4
LT
660{
661 struct bio *clone;
662
9faf400f
SB
663 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
664 __bio_clone(clone, bio);
665 clone->bi_destructor = dm_bio_destructor;
1da177e4
LT
666 clone->bi_sector = sector;
667 clone->bi_idx = idx;
668 clone->bi_vcnt = idx + bv_count;
669 clone->bi_size = to_bytes(len);
670 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
671
672 return clone;
673}
674
512875bd 675static int __clone_and_map(struct clone_info *ci)
1da177e4
LT
676{
677 struct bio *clone, *bio = ci->bio;
512875bd
JN
678 struct dm_target *ti;
679 sector_t len = 0, max;
028867ac 680 struct dm_target_io *tio;
1da177e4 681
512875bd
JN
682 ti = dm_table_find_target(ci->map, ci->sector);
683 if (!dm_target_is_valid(ti))
684 return -EIO;
685
686 max = max_io_len(ci->md, ci->sector, ti);
687
1da177e4
LT
688 /*
689 * Allocate a target io object.
690 */
691 tio = alloc_tio(ci->md);
692 tio->io = ci->io;
693 tio->ti = ti;
694 memset(&tio->info, 0, sizeof(tio->info));
695
696 if (ci->sector_count <= max) {
697 /*
698 * Optimise for the simple case where we can do all of
699 * the remaining io with a single clone.
700 */
701 clone = clone_bio(bio, ci->sector, ci->idx,
9faf400f
SB
702 bio->bi_vcnt - ci->idx, ci->sector_count,
703 ci->md->bs);
1da177e4
LT
704 __map_bio(ti, clone, tio);
705 ci->sector_count = 0;
706
707 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
708 /*
709 * There are some bvecs that don't span targets.
710 * Do as many of these as possible.
711 */
712 int i;
713 sector_t remaining = max;
714 sector_t bv_len;
715
716 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
717 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
718
719 if (bv_len > remaining)
720 break;
721
722 remaining -= bv_len;
723 len += bv_len;
724 }
725
9faf400f
SB
726 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
727 ci->md->bs);
1da177e4
LT
728 __map_bio(ti, clone, tio);
729
730 ci->sector += len;
731 ci->sector_count -= len;
732 ci->idx = i;
733
734 } else {
735 /*
d2044a94 736 * Handle a bvec that must be split between two or more targets.
1da177e4
LT
737 */
738 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
d2044a94
AK
739 sector_t remaining = to_sector(bv->bv_len);
740 unsigned int offset = 0;
1da177e4 741
d2044a94
AK
742 do {
743 if (offset) {
744 ti = dm_table_find_target(ci->map, ci->sector);
512875bd
JN
745 if (!dm_target_is_valid(ti))
746 return -EIO;
747
d2044a94 748 max = max_io_len(ci->md, ci->sector, ti);
1da177e4 749
d2044a94
AK
750 tio = alloc_tio(ci->md);
751 tio->io = ci->io;
752 tio->ti = ti;
753 memset(&tio->info, 0, sizeof(tio->info));
754 }
755
756 len = min(remaining, max);
757
758 clone = split_bvec(bio, ci->sector, ci->idx,
9faf400f
SB
759 bv->bv_offset + offset, len,
760 ci->md->bs);
d2044a94
AK
761
762 __map_bio(ti, clone, tio);
763
764 ci->sector += len;
765 ci->sector_count -= len;
766 offset += to_bytes(len);
767 } while (remaining -= len);
1da177e4 768
1da177e4
LT
769 ci->idx++;
770 }
512875bd
JN
771
772 return 0;
1da177e4
LT
773}
774
775/*
776 * Split the bio into several clones.
777 */
9e4e5f87 778static int __split_bio(struct mapped_device *md, struct bio *bio)
1da177e4
LT
779{
780 struct clone_info ci;
512875bd 781 int error = 0;
1da177e4
LT
782
783 ci.map = dm_get_table(md);
9e4e5f87
MB
784 if (unlikely(!ci.map))
785 return -EIO;
1da177e4
LT
786
787 ci.md = md;
788 ci.bio = bio;
789 ci.io = alloc_io(md);
790 ci.io->error = 0;
791 atomic_set(&ci.io->io_count, 1);
792 ci.io->bio = bio;
793 ci.io->md = md;
794 ci.sector = bio->bi_sector;
795 ci.sector_count = bio_sectors(bio);
796 ci.idx = bio->bi_idx;
797
3eaf840e 798 start_io_acct(ci.io);
512875bd
JN
799 while (ci.sector_count && !error)
800 error = __clone_and_map(&ci);
1da177e4
LT
801
802 /* drop the extra reference count */
512875bd 803 dec_pending(ci.io, error);
1da177e4 804 dm_table_put(ci.map);
9e4e5f87
MB
805
806 return 0;
1da177e4
LT
807}
808/*-----------------------------------------------------------------
809 * CRUD END
810 *---------------------------------------------------------------*/
811
812/*
813 * The request function that just remaps the bio built up by
814 * dm_merge_bvec.
815 */
165125e1 816static int dm_request(struct request_queue *q, struct bio *bio)
1da177e4 817{
9e4e5f87 818 int r = -EIO;
12f03a49 819 int rw = bio_data_dir(bio);
1da177e4
LT
820 struct mapped_device *md = q->queuedata;
821
07a83c47
SB
822 /*
823 * There is no use in forwarding any barrier request since we can't
824 * guarantee it is (or can be) handled by the targets correctly.
825 */
826 if (unlikely(bio_barrier(bio))) {
6712ecf8 827 bio_endio(bio, -EOPNOTSUPP);
07a83c47
SB
828 return 0;
829 }
830
2ca3310e 831 down_read(&md->io_lock);
1da177e4 832
12f03a49
KC
833 disk_stat_inc(dm_disk(md), ios[rw]);
834 disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio));
835
1da177e4
LT
836 /*
837 * If we're suspended we have to queue
838 * this io for later.
839 */
840 while (test_bit(DMF_BLOCK_IO, &md->flags)) {
2ca3310e 841 up_read(&md->io_lock);
1da177e4 842
9e4e5f87
MB
843 if (bio_rw(bio) != READA)
844 r = queue_io(md, bio);
1da177e4 845
9e4e5f87
MB
846 if (r <= 0)
847 goto out_req;
1da177e4
LT
848
849 /*
850 * We're in a while loop, because someone could suspend
851 * before we get to the following read lock.
852 */
2ca3310e 853 down_read(&md->io_lock);
1da177e4
LT
854 }
855
9e4e5f87 856 r = __split_bio(md, bio);
2ca3310e 857 up_read(&md->io_lock);
9e4e5f87
MB
858
859out_req:
860 if (r < 0)
861 bio_io_error(bio);
862
1da177e4
LT
863 return 0;
864}
865
165125e1 866static void dm_unplug_all(struct request_queue *q)
1da177e4
LT
867{
868 struct mapped_device *md = q->queuedata;
869 struct dm_table *map = dm_get_table(md);
870
871 if (map) {
872 dm_table_unplug_all(map);
873 dm_table_put(map);
874 }
875}
876
877static int dm_any_congested(void *congested_data, int bdi_bits)
878{
879 int r;
880 struct mapped_device *md = (struct mapped_device *) congested_data;
881 struct dm_table *map = dm_get_table(md);
882
883 if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
884 r = bdi_bits;
885 else
886 r = dm_table_any_congested(map, bdi_bits);
887
888 dm_table_put(map);
889 return r;
890}
891
892/*-----------------------------------------------------------------
893 * An IDR is used to keep track of allocated minor numbers.
894 *---------------------------------------------------------------*/
1da177e4
LT
895static DEFINE_IDR(_minor_idr);
896
2b06cfff 897static void free_minor(int minor)
1da177e4 898{
f32c10b0 899 spin_lock(&_minor_lock);
1da177e4 900 idr_remove(&_minor_idr, minor);
f32c10b0 901 spin_unlock(&_minor_lock);
1da177e4
LT
902}
903
904/*
905 * See if the device with a specific minor # is free.
906 */
2b06cfff 907static int specific_minor(struct mapped_device *md, int minor)
1da177e4
LT
908{
909 int r, m;
910
911 if (minor >= (1 << MINORBITS))
912 return -EINVAL;
913
62f75c2f
JM
914 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
915 if (!r)
916 return -ENOMEM;
917
f32c10b0 918 spin_lock(&_minor_lock);
1da177e4
LT
919
920 if (idr_find(&_minor_idr, minor)) {
921 r = -EBUSY;
922 goto out;
923 }
924
ba61fdd1 925 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
62f75c2f 926 if (r)
1da177e4 927 goto out;
1da177e4
LT
928
929 if (m != minor) {
930 idr_remove(&_minor_idr, m);
931 r = -EBUSY;
932 goto out;
933 }
934
935out:
f32c10b0 936 spin_unlock(&_minor_lock);
1da177e4
LT
937 return r;
938}
939
2b06cfff 940static int next_free_minor(struct mapped_device *md, int *minor)
1da177e4 941{
2b06cfff 942 int r, m;
1da177e4 943
1da177e4 944 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
62f75c2f
JM
945 if (!r)
946 return -ENOMEM;
947
f32c10b0 948 spin_lock(&_minor_lock);
1da177e4 949
ba61fdd1 950 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
1da177e4
LT
951 if (r) {
952 goto out;
953 }
954
955 if (m >= (1 << MINORBITS)) {
956 idr_remove(&_minor_idr, m);
957 r = -ENOSPC;
958 goto out;
959 }
960
961 *minor = m;
962
963out:
f32c10b0 964 spin_unlock(&_minor_lock);
1da177e4
LT
965 return r;
966}
967
968static struct block_device_operations dm_blk_dops;
969
970/*
971 * Allocate and initialise a blank device with a given minor.
972 */
2b06cfff 973static struct mapped_device *alloc_dev(int minor)
1da177e4
LT
974{
975 int r;
976 struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
ba61fdd1 977 void *old_md;
1da177e4
LT
978
979 if (!md) {
980 DMWARN("unable to allocate device, out of memory.");
981 return NULL;
982 }
983
10da4f79 984 if (!try_module_get(THIS_MODULE))
6ed7ade8 985 goto bad_module_get;
10da4f79 986
1da177e4 987 /* get a minor number for the dev */
2b06cfff
AK
988 if (minor == DM_ANY_MINOR)
989 r = next_free_minor(md, &minor);
990 else
991 r = specific_minor(md, minor);
1da177e4 992 if (r < 0)
6ed7ade8 993 goto bad_minor;
1da177e4
LT
994
995 memset(md, 0, sizeof(*md));
2ca3310e 996 init_rwsem(&md->io_lock);
e61290a4 997 mutex_init(&md->suspend_lock);
2e93ccc1 998 spin_lock_init(&md->pushback_lock);
1da177e4
LT
999 rwlock_init(&md->map_lock);
1000 atomic_set(&md->holders, 1);
5c6bd75d 1001 atomic_set(&md->open_count, 0);
1da177e4 1002 atomic_set(&md->event_nr, 0);
7a8c3d3b
MA
1003 atomic_set(&md->uevent_seq, 0);
1004 INIT_LIST_HEAD(&md->uevent_list);
1005 spin_lock_init(&md->uevent_lock);
1da177e4
LT
1006
1007 md->queue = blk_alloc_queue(GFP_KERNEL);
1008 if (!md->queue)
6ed7ade8 1009 goto bad_queue;
1da177e4
LT
1010
1011 md->queue->queuedata = md;
1012 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1013 md->queue->backing_dev_info.congested_data = md;
1014 blk_queue_make_request(md->queue, dm_request);
daef265f 1015 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
1da177e4 1016 md->queue->unplug_fn = dm_unplug_all;
1da177e4 1017
93d2341c 1018 md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
74859364 1019 if (!md->io_pool)
6ed7ade8 1020 goto bad_io_pool;
1da177e4 1021
93d2341c 1022 md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
1da177e4 1023 if (!md->tio_pool)
6ed7ade8 1024 goto bad_tio_pool;
1da177e4 1025
5972511b 1026 md->bs = bioset_create(16, 16);
9faf400f
SB
1027 if (!md->bs)
1028 goto bad_no_bioset;
1029
1da177e4
LT
1030 md->disk = alloc_disk(1);
1031 if (!md->disk)
6ed7ade8 1032 goto bad_disk;
1da177e4 1033
f0b04115
JM
1034 atomic_set(&md->pending, 0);
1035 init_waitqueue_head(&md->wait);
1036 init_waitqueue_head(&md->eventq);
1037
1da177e4
LT
1038 md->disk->major = _major;
1039 md->disk->first_minor = minor;
1040 md->disk->fops = &dm_blk_dops;
1041 md->disk->queue = md->queue;
1042 md->disk->private_data = md;
1043 sprintf(md->disk->disk_name, "dm-%d", minor);
1044 add_disk(md->disk);
7e51f257 1045 format_dev_t(md->name, MKDEV(_major, minor));
1da177e4 1046
ba61fdd1 1047 /* Populate the mapping, nobody knows we exist yet */
f32c10b0 1048 spin_lock(&_minor_lock);
ba61fdd1 1049 old_md = idr_replace(&_minor_idr, md, minor);
f32c10b0 1050 spin_unlock(&_minor_lock);
ba61fdd1
JM
1051
1052 BUG_ON(old_md != MINOR_ALLOCED);
1053
1da177e4
LT
1054 return md;
1055
6ed7ade8 1056bad_disk:
9faf400f 1057 bioset_free(md->bs);
6ed7ade8 1058bad_no_bioset:
1da177e4 1059 mempool_destroy(md->tio_pool);
6ed7ade8 1060bad_tio_pool:
1da177e4 1061 mempool_destroy(md->io_pool);
6ed7ade8 1062bad_io_pool:
1312f40e 1063 blk_cleanup_queue(md->queue);
6ed7ade8 1064bad_queue:
1da177e4 1065 free_minor(minor);
6ed7ade8 1066bad_minor:
10da4f79 1067 module_put(THIS_MODULE);
6ed7ade8 1068bad_module_get:
1da177e4
LT
1069 kfree(md);
1070 return NULL;
1071}
1072
ae9da83f
JN
1073static void unlock_fs(struct mapped_device *md);
1074
1da177e4
LT
1075static void free_dev(struct mapped_device *md)
1076{
2b06cfff 1077 int minor = md->disk->first_minor;
63d94e48 1078
d9dde59b 1079 if (md->suspended_bdev) {
ae9da83f 1080 unlock_fs(md);
d9dde59b
JN
1081 bdput(md->suspended_bdev);
1082 }
1da177e4
LT
1083 mempool_destroy(md->tio_pool);
1084 mempool_destroy(md->io_pool);
9faf400f 1085 bioset_free(md->bs);
1da177e4 1086 del_gendisk(md->disk);
63d94e48 1087 free_minor(minor);
fba9f90e
JM
1088
1089 spin_lock(&_minor_lock);
1090 md->disk->private_data = NULL;
1091 spin_unlock(&_minor_lock);
1092
1da177e4 1093 put_disk(md->disk);
1312f40e 1094 blk_cleanup_queue(md->queue);
10da4f79 1095 module_put(THIS_MODULE);
1da177e4
LT
1096 kfree(md);
1097}
1098
1099/*
1100 * Bind a table to the device.
1101 */
1102static void event_callback(void *context)
1103{
7a8c3d3b
MA
1104 unsigned long flags;
1105 LIST_HEAD(uevents);
1da177e4
LT
1106 struct mapped_device *md = (struct mapped_device *) context;
1107
7a8c3d3b
MA
1108 spin_lock_irqsave(&md->uevent_lock, flags);
1109 list_splice_init(&md->uevent_list, &uevents);
1110 spin_unlock_irqrestore(&md->uevent_lock, flags);
1111
edfaa7c3 1112 dm_send_uevents(&uevents, &md->disk->dev.kobj);
7a8c3d3b 1113
1da177e4
LT
1114 atomic_inc(&md->event_nr);
1115 wake_up(&md->eventq);
1116}
1117
4e90188b 1118static void __set_size(struct mapped_device *md, sector_t size)
1da177e4 1119{
4e90188b 1120 set_capacity(md->disk, size);
1da177e4 1121
1b1dcc1b 1122 mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
e39e2e95 1123 i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
1b1dcc1b 1124 mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
1da177e4
LT
1125}
1126
1127static int __bind(struct mapped_device *md, struct dm_table *t)
1128{
165125e1 1129 struct request_queue *q = md->queue;
1da177e4
LT
1130 sector_t size;
1131
1132 size = dm_table_get_size(t);
3ac51e74
DW
1133
1134 /*
1135 * Wipe any geometry if the size of the table changed.
1136 */
1137 if (size != get_capacity(md->disk))
1138 memset(&md->geometry, 0, sizeof(md->geometry));
1139
bfa152fa
JN
1140 if (md->suspended_bdev)
1141 __set_size(md, size);
1da177e4
LT
1142 if (size == 0)
1143 return 0;
1144
2ca3310e
AK
1145 dm_table_get(t);
1146 dm_table_event_callback(t, event_callback, md);
1147
1da177e4
LT
1148 write_lock(&md->map_lock);
1149 md->map = t;
2ca3310e 1150 dm_table_set_restrictions(t, q);
1da177e4
LT
1151 write_unlock(&md->map_lock);
1152
1da177e4
LT
1153 return 0;
1154}
1155
1156static void __unbind(struct mapped_device *md)
1157{
1158 struct dm_table *map = md->map;
1159
1160 if (!map)
1161 return;
1162
1163 dm_table_event_callback(map, NULL, NULL);
1164 write_lock(&md->map_lock);
1165 md->map = NULL;
1166 write_unlock(&md->map_lock);
1167 dm_table_put(map);
1168}
1169
1170/*
1171 * Constructor for a new device.
1172 */
2b06cfff 1173int dm_create(int minor, struct mapped_device **result)
1da177e4
LT
1174{
1175 struct mapped_device *md;
1176
2b06cfff 1177 md = alloc_dev(minor);
1da177e4
LT
1178 if (!md)
1179 return -ENXIO;
1180
1181 *result = md;
1182 return 0;
1183}
1184
637842cf 1185static struct mapped_device *dm_find_md(dev_t dev)
1da177e4
LT
1186{
1187 struct mapped_device *md;
1da177e4
LT
1188 unsigned minor = MINOR(dev);
1189
1190 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
1191 return NULL;
1192
f32c10b0 1193 spin_lock(&_minor_lock);
1da177e4
LT
1194
1195 md = idr_find(&_minor_idr, minor);
fba9f90e
JM
1196 if (md && (md == MINOR_ALLOCED ||
1197 (dm_disk(md)->first_minor != minor) ||
17b2f66f 1198 test_bit(DMF_FREEING, &md->flags))) {
637842cf 1199 md = NULL;
fba9f90e
JM
1200 goto out;
1201 }
1da177e4 1202
fba9f90e 1203out:
f32c10b0 1204 spin_unlock(&_minor_lock);
1da177e4 1205
637842cf
DT
1206 return md;
1207}
1208
d229a958
DT
1209struct mapped_device *dm_get_md(dev_t dev)
1210{
1211 struct mapped_device *md = dm_find_md(dev);
1212
1213 if (md)
1214 dm_get(md);
1215
1216 return md;
1217}
1218
9ade92a9 1219void *dm_get_mdptr(struct mapped_device *md)
637842cf 1220{
9ade92a9 1221 return md->interface_ptr;
1da177e4
LT
1222}
1223
1224void dm_set_mdptr(struct mapped_device *md, void *ptr)
1225{
1226 md->interface_ptr = ptr;
1227}
1228
1229void dm_get(struct mapped_device *md)
1230{
1231 atomic_inc(&md->holders);
1232}
1233
72d94861
AK
1234const char *dm_device_name(struct mapped_device *md)
1235{
1236 return md->name;
1237}
1238EXPORT_SYMBOL_GPL(dm_device_name);
1239
1da177e4
LT
1240void dm_put(struct mapped_device *md)
1241{
1134e5ae 1242 struct dm_table *map;
1da177e4 1243
fba9f90e
JM
1244 BUG_ON(test_bit(DMF_FREEING, &md->flags));
1245
f32c10b0 1246 if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
1134e5ae 1247 map = dm_get_table(md);
ba61fdd1 1248 idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor);
fba9f90e 1249 set_bit(DMF_FREEING, &md->flags);
f32c10b0 1250 spin_unlock(&_minor_lock);
cf222b37 1251 if (!dm_suspended(md)) {
1da177e4
LT
1252 dm_table_presuspend_targets(map);
1253 dm_table_postsuspend_targets(map);
1254 }
1255 __unbind(md);
1134e5ae 1256 dm_table_put(map);
1da177e4
LT
1257 free_dev(md);
1258 }
1da177e4 1259}
79eb885c 1260EXPORT_SYMBOL_GPL(dm_put);
1da177e4
LT
1261
1262/*
1263 * Process the deferred bios
1264 */
1265static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
1266{
1267 struct bio *n;
1268
1269 while (c) {
1270 n = c->bi_next;
1271 c->bi_next = NULL;
9e4e5f87
MB
1272 if (__split_bio(md, c))
1273 bio_io_error(c);
1da177e4
LT
1274 c = n;
1275 }
1276}
1277
1278/*
1279 * Swap in a new table (destroying old one).
1280 */
1281int dm_swap_table(struct mapped_device *md, struct dm_table *table)
1282{
93c534ae 1283 int r = -EINVAL;
1da177e4 1284
e61290a4 1285 mutex_lock(&md->suspend_lock);
1da177e4
LT
1286
1287 /* device must be suspended */
cf222b37 1288 if (!dm_suspended(md))
93c534ae 1289 goto out;
1da177e4 1290
bfa152fa
JN
1291 /* without bdev, the device size cannot be changed */
1292 if (!md->suspended_bdev)
1293 if (get_capacity(md->disk) != dm_table_get_size(table))
1294 goto out;
1295
1da177e4
LT
1296 __unbind(md);
1297 r = __bind(md, table);
1da177e4 1298
93c534ae 1299out:
e61290a4 1300 mutex_unlock(&md->suspend_lock);
93c534ae 1301 return r;
1da177e4
LT
1302}
1303
1304/*
1305 * Functions to lock and unlock any filesystem running on the
1306 * device.
1307 */
2ca3310e 1308static int lock_fs(struct mapped_device *md)
1da177e4 1309{
e39e2e95 1310 int r;
1da177e4
LT
1311
1312 WARN_ON(md->frozen_sb);
dfbe03f6 1313
e39e2e95 1314 md->frozen_sb = freeze_bdev(md->suspended_bdev);
dfbe03f6 1315 if (IS_ERR(md->frozen_sb)) {
cf222b37 1316 r = PTR_ERR(md->frozen_sb);
e39e2e95
AK
1317 md->frozen_sb = NULL;
1318 return r;
dfbe03f6
AK
1319 }
1320
aa8d7c2f
AK
1321 set_bit(DMF_FROZEN, &md->flags);
1322
1da177e4 1323 /* don't bdput right now, we don't want the bdev
e39e2e95 1324 * to go away while it is locked.
1da177e4
LT
1325 */
1326 return 0;
1327}
1328
2ca3310e 1329static void unlock_fs(struct mapped_device *md)
1da177e4 1330{
aa8d7c2f
AK
1331 if (!test_bit(DMF_FROZEN, &md->flags))
1332 return;
1333
e39e2e95 1334 thaw_bdev(md->suspended_bdev, md->frozen_sb);
1da177e4 1335 md->frozen_sb = NULL;
aa8d7c2f 1336 clear_bit(DMF_FROZEN, &md->flags);
1da177e4
LT
1337}
1338
1339/*
1340 * We need to be able to change a mapping table under a mounted
1341 * filesystem. For example we might want to move some data in
1342 * the background. Before the table can be swapped with
1343 * dm_bind_table, dm_suspend must be called to flush any in
1344 * flight bios and ensure that any further io gets deferred.
1345 */
a3d77d35 1346int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
1da177e4 1347{
2ca3310e 1348 struct dm_table *map = NULL;
2e93ccc1 1349 unsigned long flags;
1da177e4 1350 DECLARE_WAITQUEUE(wait, current);
1ecac7fd 1351 struct bio *def;
cf222b37 1352 int r = -EINVAL;
a3d77d35 1353 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
2e93ccc1 1354 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
1da177e4 1355
e61290a4 1356 mutex_lock(&md->suspend_lock);
2ca3310e
AK
1357
1358 if (dm_suspended(md))
d287483d 1359 goto out_unlock;
1da177e4
LT
1360
1361 map = dm_get_table(md);
1da177e4 1362
2e93ccc1
KU
1363 /*
1364 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
1365 * This flag is cleared before dm_suspend returns.
1366 */
1367 if (noflush)
1368 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1369
cf222b37
AK
1370 /* This does not get reverted if there's an error later. */
1371 dm_table_presuspend_targets(map);
1372
bfa152fa
JN
1373 /* bdget() can stall if the pending I/Os are not flushed */
1374 if (!noflush) {
1375 md->suspended_bdev = bdget_disk(md->disk, 0);
1376 if (!md->suspended_bdev) {
1377 DMWARN("bdget failed in dm_suspend");
1378 r = -ENOMEM;
1379 goto flush_and_out;
1380 }
e39e2e95
AK
1381 }
1382
2e93ccc1
KU
1383 /*
1384 * Flush I/O to the device.
1385 * noflush supersedes do_lockfs, because lock_fs() needs to flush I/Os.
1386 */
1387 if (do_lockfs && !noflush) {
aa8d7c2f
AK
1388 r = lock_fs(md);
1389 if (r)
1390 goto out;
1391 }
1da177e4
LT
1392
1393 /*
354e0071 1394 * First we set the BLOCK_IO flag so no more ios will be mapped.
1da177e4 1395 */
2ca3310e
AK
1396 down_write(&md->io_lock);
1397 set_bit(DMF_BLOCK_IO, &md->flags);
1da177e4 1398
1da177e4 1399 add_wait_queue(&md->wait, &wait);
2ca3310e 1400 up_write(&md->io_lock);
1da177e4
LT
1401
1402 /* unplug */
2ca3310e 1403 if (map)
1da177e4 1404 dm_table_unplug_all(map);
1da177e4
LT
1405
1406 /*
1407 * Then we wait for the already mapped ios to
1408 * complete.
1409 */
1410 while (1) {
1411 set_current_state(TASK_INTERRUPTIBLE);
1412
7e5c1e83 1413 smp_mb();
1da177e4
LT
1414 if (!atomic_read(&md->pending) || signal_pending(current))
1415 break;
1416
1417 io_schedule();
1418 }
1419 set_current_state(TASK_RUNNING);
1420
2ca3310e 1421 down_write(&md->io_lock);
1da177e4
LT
1422 remove_wait_queue(&md->wait, &wait);
1423
2e93ccc1
KU
1424 if (noflush) {
1425 spin_lock_irqsave(&md->pushback_lock, flags);
1426 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1427 bio_list_merge_head(&md->deferred, &md->pushback);
1428 bio_list_init(&md->pushback);
1429 spin_unlock_irqrestore(&md->pushback_lock, flags);
1430 }
1431
1da177e4 1432 /* were we interrupted ? */
cf222b37 1433 r = -EINTR;
2ca3310e 1434 if (atomic_read(&md->pending)) {
1ecac7fd
JN
1435 clear_bit(DMF_BLOCK_IO, &md->flags);
1436 def = bio_list_get(&md->deferred);
1437 __flush_deferred_io(md, def);
2ca3310e
AK
1438 up_write(&md->io_lock);
1439 unlock_fs(md);
2e93ccc1 1440 goto out; /* pushback list is already flushed, so skip flush */
2ca3310e
AK
1441 }
1442 up_write(&md->io_lock);
1da177e4 1443
cf222b37 1444 dm_table_postsuspend_targets(map);
1da177e4 1445
2ca3310e 1446 set_bit(DMF_SUSPENDED, &md->flags);
b84b0287 1447
2ca3310e 1448 r = 0;
b84b0287 1449
2e93ccc1
KU
1450flush_and_out:
1451 if (r && noflush) {
1452 /*
1453 * Because there may be already I/Os in the pushback list,
1454 * flush them before return.
1455 */
1456 down_write(&md->io_lock);
1457
1458 spin_lock_irqsave(&md->pushback_lock, flags);
1459 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1460 bio_list_merge_head(&md->deferred, &md->pushback);
1461 bio_list_init(&md->pushback);
1462 spin_unlock_irqrestore(&md->pushback_lock, flags);
1463
1464 def = bio_list_get(&md->deferred);
1465 __flush_deferred_io(md, def);
1466 up_write(&md->io_lock);
1467 }
1468
2ca3310e 1469out:
e39e2e95
AK
1470 if (r && md->suspended_bdev) {
1471 bdput(md->suspended_bdev);
1472 md->suspended_bdev = NULL;
1473 }
1474
2ca3310e 1475 dm_table_put(map);
d287483d
AK
1476
1477out_unlock:
e61290a4 1478 mutex_unlock(&md->suspend_lock);
cf222b37 1479 return r;
1da177e4
LT
1480}
1481
1482int dm_resume(struct mapped_device *md)
1483{
cf222b37 1484 int r = -EINVAL;
1da177e4 1485 struct bio *def;
cf222b37 1486 struct dm_table *map = NULL;
1da177e4 1487
e61290a4 1488 mutex_lock(&md->suspend_lock);
2ca3310e 1489 if (!dm_suspended(md))
cf222b37 1490 goto out;
cf222b37
AK
1491
1492 map = dm_get_table(md);
2ca3310e 1493 if (!map || !dm_table_get_size(map))
cf222b37 1494 goto out;
1da177e4 1495
8757b776
MB
1496 r = dm_table_resume_targets(map);
1497 if (r)
1498 goto out;
2ca3310e
AK
1499
1500 down_write(&md->io_lock);
1da177e4
LT
1501 clear_bit(DMF_BLOCK_IO, &md->flags);
1502
1503 def = bio_list_get(&md->deferred);
1504 __flush_deferred_io(md, def);
2ca3310e
AK
1505 up_write(&md->io_lock);
1506
1507 unlock_fs(md);
1508
bfa152fa
JN
1509 if (md->suspended_bdev) {
1510 bdput(md->suspended_bdev);
1511 md->suspended_bdev = NULL;
1512 }
e39e2e95 1513
2ca3310e
AK
1514 clear_bit(DMF_SUSPENDED, &md->flags);
1515
1da177e4 1516 dm_table_unplug_all(map);
1da177e4 1517
69267a30 1518 dm_kobject_uevent(md);
8560ed6f 1519
cf222b37 1520 r = 0;
2ca3310e 1521
cf222b37
AK
1522out:
1523 dm_table_put(map);
e61290a4 1524 mutex_unlock(&md->suspend_lock);
2ca3310e 1525
cf222b37 1526 return r;
1da177e4
LT
1527}
1528
1529/*-----------------------------------------------------------------
1530 * Event notification.
1531 *---------------------------------------------------------------*/
69267a30
AK
1532void dm_kobject_uevent(struct mapped_device *md)
1533{
edfaa7c3 1534 kobject_uevent(&md->disk->dev.kobj, KOBJ_CHANGE);
69267a30
AK
1535}
1536
7a8c3d3b
MA
1537uint32_t dm_next_uevent_seq(struct mapped_device *md)
1538{
1539 return atomic_add_return(1, &md->uevent_seq);
1540}
1541
1da177e4
LT
1542uint32_t dm_get_event_nr(struct mapped_device *md)
1543{
1544 return atomic_read(&md->event_nr);
1545}
1546
1547int dm_wait_event(struct mapped_device *md, int event_nr)
1548{
1549 return wait_event_interruptible(md->eventq,
1550 (event_nr != atomic_read(&md->event_nr)));
1551}
1552
7a8c3d3b
MA
1553void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
1554{
1555 unsigned long flags;
1556
1557 spin_lock_irqsave(&md->uevent_lock, flags);
1558 list_add(elist, &md->uevent_list);
1559 spin_unlock_irqrestore(&md->uevent_lock, flags);
1560}
1561
1da177e4
LT
1562/*
1563 * The gendisk is only valid as long as you have a reference
1564 * count on 'md'.
1565 */
1566struct gendisk *dm_disk(struct mapped_device *md)
1567{
1568 return md->disk;
1569}
1570
1571int dm_suspended(struct mapped_device *md)
1572{
1573 return test_bit(DMF_SUSPENDED, &md->flags);
1574}
1575
2e93ccc1
KU
1576int dm_noflush_suspending(struct dm_target *ti)
1577{
1578 struct mapped_device *md = dm_table_get_md(ti->table);
1579 int r = __noflush_suspending(md);
1580
1581 dm_put(md);
1582
1583 return r;
1584}
1585EXPORT_SYMBOL_GPL(dm_noflush_suspending);
1586
1da177e4
LT
1587static struct block_device_operations dm_blk_dops = {
1588 .open = dm_blk_open,
1589 .release = dm_blk_close,
aa129a22 1590 .ioctl = dm_blk_ioctl,
3ac51e74 1591 .getgeo = dm_blk_getgeo,
1da177e4
LT
1592 .owner = THIS_MODULE
1593};
1594
1595EXPORT_SYMBOL(dm_get_mapinfo);
1596
1597/*
1598 * module hooks
1599 */
1600module_init(dm_init);
1601module_exit(dm_exit);
1602
1603module_param(major, uint, 0);
1604MODULE_PARM_DESC(major, "The major number of the device mapper");
1605MODULE_DESCRIPTION(DM_NAME " driver");
1606MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1607MODULE_LICENSE("GPL");