[PATCH] dm: suspend: parameter change
[deliverable/linux.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"
10
11#include <linux/init.h>
12#include <linux/module.h>
48c9c27b 13#include <linux/mutex.h>
1da177e4
LT
14#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>
3ac51e74 21#include <linux/hdreg.h>
2056a782 22#include <linux/blktrace_api.h>
aa129a22 23#include <linux/smp_lock.h>
1da177e4 24
72d94861
AK
25#define DM_MSG_PREFIX "core"
26
1da177e4
LT
27static const char *_name = DM_NAME;
28
29static unsigned int major = 0;
30static unsigned int _major = 0;
31
f32c10b0 32static DEFINE_SPINLOCK(_minor_lock);
1da177e4
LT
33/*
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;
3eaf840e 41 unsigned long start_time;
1da177e4
LT
42};
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{
17b2f66f
AK
56 if (bio && bio->bi_private)
57 return &((struct target_io *)bio->bi_private)->info;
58 return NULL;
1da177e4
LT
59}
60
ba61fdd1
JM
61#define MINOR_ALLOCED ((void *)-1)
62
1da177e4
LT
63/*
64 * Bits for the md->flags field.
65 */
66#define DMF_BLOCK_IO 0
67#define DMF_SUSPENDED 1
aa8d7c2f 68#define DMF_FROZEN 2
fba9f90e 69#define DMF_FREEING 3
5c6bd75d 70#define DMF_DELETING 4
1da177e4
LT
71
72struct mapped_device {
2ca3310e
AK
73 struct rw_semaphore io_lock;
74 struct semaphore suspend_lock;
1da177e4
LT
75 rwlock_t map_lock;
76 atomic_t holders;
5c6bd75d 77 atomic_t open_count;
1da177e4
LT
78
79 unsigned long flags;
80
81 request_queue_t *queue;
82 struct gendisk *disk;
7e51f257 83 char name[16];
1da177e4
LT
84
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;
74859364 92 struct bio_list deferred;
1da177e4
LT
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
9faf400f
SB
105 struct bio_set *bs;
106
1da177e4
LT
107 /*
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;
e39e2e95 117 struct block_device *suspended_bdev;
3ac51e74
DW
118
119 /* forced geometry settings */
120 struct hd_geometry geometry;
1da177e4
LT
121};
122
123#define MIN_IOS 256
e18b890b
CL
124static struct kmem_cache *_io_cache;
125static struct kmem_cache *_tio_cache;
1da177e4 126
1da177e4
LT
127static int __init local_init(void)
128{
129 int r;
130
1da177e4
LT
131 /* 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
1da177e4 164 if (unregister_blkdev(_major, _name) < 0)
890fbae2 165 DMERR("unregister_blkdev failed");
1da177e4
LT
166
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
fba9f90e
JM
224 spin_lock(&_minor_lock);
225
1da177e4 226 md = inode->i_bdev->bd_disk->private_data;
fba9f90e
JM
227 if (!md)
228 goto out;
229
5c6bd75d
AK
230 if (test_bit(DMF_FREEING, &md->flags) ||
231 test_bit(DMF_DELETING, &md->flags)) {
fba9f90e
JM
232 md = NULL;
233 goto out;
234 }
235
1da177e4 236 dm_get(md);
5c6bd75d 237 atomic_inc(&md->open_count);
fba9f90e
JM
238
239out:
240 spin_unlock(&_minor_lock);
241
242 return md ? 0 : -ENXIO;
1da177e4
LT
243}
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;
5c6bd75d 250 atomic_dec(&md->open_count);
1da177e4
LT
251 dm_put(md);
252 return 0;
253}
254
5c6bd75d
AK
255int 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
3ac51e74
DW
279static 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
aa129a22
MB
286static 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
1da177e4
LT
325static 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
3eaf840e
JNN
345static 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
1da177e4
LT
375/*
376 * Add the bio to the list of deferred io.
377 */
378static int queue_io(struct mapped_device *md, struct bio *bio)
379{
2ca3310e 380 down_write(&md->io_lock);
1da177e4
LT
381
382 if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
2ca3310e 383 up_write(&md->io_lock);
1da177e4
LT
384 return 1;
385 }
386
387 bio_list_add(&md->deferred, bio);
388
2ca3310e 389 up_write(&md->io_lock);
1da177e4
LT
390 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
3ac51e74
DW
411/*
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
1da177e4
LT
438/*-----------------------------------------------------------------
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 */
858119e1 451static void dec_pending(struct dm_io *io, int error)
1da177e4
LT
452{
453 if (error)
454 io->error = error;
455
456 if (atomic_dec_and_test(&io->io_count)) {
3eaf840e 457 if (end_io_acct(io))
1da177e4
LT
458 /* nudge anyone waiting on suspend queue */
459 wake_up(&io->md->wait);
460
2056a782
JA
461 blk_add_trace_bio(io->md->queue, io->bio, BLK_TA_COMPLETE);
462
1da177e4
LT
463 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;
9faf400f 472 struct mapped_device *md = tio->io->md;
1da177e4
LT
473 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;
1da177e4
LT
485 else if (r > 0)
486 /* the target wants another shot at the io */
487 return 1;
488 }
489
9faf400f
SB
490 dec_pending(tio->io, error);
491
492 /*
493 * Store md for cleanup instead of tio which is about to get freed.
494 */
495 bio->bi_private = md->bs;
496
1da177e4 497 bio_put(bio);
9faf400f 498 free_tio(md, tio);
1da177e4
LT
499 return r;
500}
501
502static sector_t max_io_len(struct mapped_device *md,
503 sector_t sector, struct dm_target *ti)
504{
505 sector_t offset = sector - ti->begin;
506 sector_t len = ti->len - offset;
507
508 /*
509 * Does the target need to split even further ?
510 */
511 if (ti->split_io) {
512 sector_t boundary;
513 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
514 - offset;
515 if (len > boundary)
516 len = boundary;
517 }
518
519 return len;
520}
521
522static void __map_bio(struct dm_target *ti, struct bio *clone,
523 struct target_io *tio)
524{
525 int r;
2056a782 526 sector_t sector;
9faf400f 527 struct mapped_device *md;
1da177e4
LT
528
529 /*
530 * Sanity checks.
531 */
532 BUG_ON(!clone->bi_size);
533
534 clone->bi_end_io = clone_endio;
535 clone->bi_private = tio;
536
537 /*
538 * Map the clone. If r == 0 we don't need to do
539 * anything, the target has assumed ownership of
540 * this io.
541 */
542 atomic_inc(&tio->io->io_count);
2056a782 543 sector = clone->bi_sector;
1da177e4 544 r = ti->type->map(ti, clone, &tio->info);
2056a782 545 if (r > 0) {
1da177e4 546 /* the bio has been remapped so dispatch it */
2056a782 547
17b2f66f
AK
548 blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone,
549 tio->io->bio->bi_bdev->bd_dev, sector,
2056a782
JA
550 clone->bi_sector);
551
1da177e4 552 generic_make_request(clone);
74859364 553 } else if (r < 0) {
1da177e4 554 /* error the io and bail out */
9faf400f
SB
555 md = tio->io->md;
556 dec_pending(tio->io, r);
557 /*
558 * Store bio_set for cleanup.
559 */
560 clone->bi_private = md->bs;
1da177e4 561 bio_put(clone);
9faf400f 562 free_tio(md, tio);
1da177e4
LT
563 }
564}
565
566struct clone_info {
567 struct mapped_device *md;
568 struct dm_table *map;
569 struct bio *bio;
570 struct dm_io *io;
571 sector_t sector;
572 sector_t sector_count;
573 unsigned short idx;
574};
575
3676347a
PO
576static void dm_bio_destructor(struct bio *bio)
577{
9faf400f
SB
578 struct bio_set *bs = bio->bi_private;
579
580 bio_free(bio, bs);
3676347a
PO
581}
582
1da177e4
LT
583/*
584 * Creates a little bio that is just does part of a bvec.
585 */
586static struct bio *split_bvec(struct bio *bio, sector_t sector,
587 unsigned short idx, unsigned int offset,
9faf400f 588 unsigned int len, struct bio_set *bs)
1da177e4
LT
589{
590 struct bio *clone;
591 struct bio_vec *bv = bio->bi_io_vec + idx;
592
9faf400f 593 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
3676347a 594 clone->bi_destructor = dm_bio_destructor;
1da177e4
LT
595 *clone->bi_io_vec = *bv;
596
597 clone->bi_sector = sector;
598 clone->bi_bdev = bio->bi_bdev;
599 clone->bi_rw = bio->bi_rw;
600 clone->bi_vcnt = 1;
601 clone->bi_size = to_bytes(len);
602 clone->bi_io_vec->bv_offset = offset;
603 clone->bi_io_vec->bv_len = clone->bi_size;
604
605 return clone;
606}
607
608/*
609 * Creates a bio that consists of range of complete bvecs.
610 */
611static struct bio *clone_bio(struct bio *bio, sector_t sector,
612 unsigned short idx, unsigned short bv_count,
9faf400f 613 unsigned int len, struct bio_set *bs)
1da177e4
LT
614{
615 struct bio *clone;
616
9faf400f
SB
617 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
618 __bio_clone(clone, bio);
619 clone->bi_destructor = dm_bio_destructor;
1da177e4
LT
620 clone->bi_sector = sector;
621 clone->bi_idx = idx;
622 clone->bi_vcnt = idx + bv_count;
623 clone->bi_size = to_bytes(len);
624 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
625
626 return clone;
627}
628
629static void __clone_and_map(struct clone_info *ci)
630{
631 struct bio *clone, *bio = ci->bio;
632 struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
633 sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
634 struct target_io *tio;
635
636 /*
637 * Allocate a target io object.
638 */
639 tio = alloc_tio(ci->md);
640 tio->io = ci->io;
641 tio->ti = ti;
642 memset(&tio->info, 0, sizeof(tio->info));
643
644 if (ci->sector_count <= max) {
645 /*
646 * Optimise for the simple case where we can do all of
647 * the remaining io with a single clone.
648 */
649 clone = clone_bio(bio, ci->sector, ci->idx,
9faf400f
SB
650 bio->bi_vcnt - ci->idx, ci->sector_count,
651 ci->md->bs);
1da177e4
LT
652 __map_bio(ti, clone, tio);
653 ci->sector_count = 0;
654
655 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
656 /*
657 * There are some bvecs that don't span targets.
658 * Do as many of these as possible.
659 */
660 int i;
661 sector_t remaining = max;
662 sector_t bv_len;
663
664 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
665 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
666
667 if (bv_len > remaining)
668 break;
669
670 remaining -= bv_len;
671 len += bv_len;
672 }
673
9faf400f
SB
674 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
675 ci->md->bs);
1da177e4
LT
676 __map_bio(ti, clone, tio);
677
678 ci->sector += len;
679 ci->sector_count -= len;
680 ci->idx = i;
681
682 } else {
683 /*
d2044a94 684 * Handle a bvec that must be split between two or more targets.
1da177e4
LT
685 */
686 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
d2044a94
AK
687 sector_t remaining = to_sector(bv->bv_len);
688 unsigned int offset = 0;
1da177e4 689
d2044a94
AK
690 do {
691 if (offset) {
692 ti = dm_table_find_target(ci->map, ci->sector);
693 max = max_io_len(ci->md, ci->sector, ti);
1da177e4 694
d2044a94
AK
695 tio = alloc_tio(ci->md);
696 tio->io = ci->io;
697 tio->ti = ti;
698 memset(&tio->info, 0, sizeof(tio->info));
699 }
700
701 len = min(remaining, max);
702
703 clone = split_bvec(bio, ci->sector, ci->idx,
9faf400f
SB
704 bv->bv_offset + offset, len,
705 ci->md->bs);
d2044a94
AK
706
707 __map_bio(ti, clone, tio);
708
709 ci->sector += len;
710 ci->sector_count -= len;
711 offset += to_bytes(len);
712 } while (remaining -= len);
1da177e4 713
1da177e4
LT
714 ci->idx++;
715 }
716}
717
718/*
719 * Split the bio into several clones.
720 */
721static void __split_bio(struct mapped_device *md, struct bio *bio)
722{
723 struct clone_info ci;
724
725 ci.map = dm_get_table(md);
726 if (!ci.map) {
727 bio_io_error(bio, bio->bi_size);
728 return;
729 }
730
731 ci.md = md;
732 ci.bio = bio;
733 ci.io = alloc_io(md);
734 ci.io->error = 0;
735 atomic_set(&ci.io->io_count, 1);
736 ci.io->bio = bio;
737 ci.io->md = md;
738 ci.sector = bio->bi_sector;
739 ci.sector_count = bio_sectors(bio);
740 ci.idx = bio->bi_idx;
741
3eaf840e 742 start_io_acct(ci.io);
1da177e4
LT
743 while (ci.sector_count)
744 __clone_and_map(&ci);
745
746 /* drop the extra reference count */
747 dec_pending(ci.io, 0);
748 dm_table_put(ci.map);
749}
750/*-----------------------------------------------------------------
751 * CRUD END
752 *---------------------------------------------------------------*/
753
754/*
755 * The request function that just remaps the bio built up by
756 * dm_merge_bvec.
757 */
758static int dm_request(request_queue_t *q, struct bio *bio)
759{
760 int r;
12f03a49 761 int rw = bio_data_dir(bio);
1da177e4
LT
762 struct mapped_device *md = q->queuedata;
763
2ca3310e 764 down_read(&md->io_lock);
1da177e4 765
12f03a49
KC
766 disk_stat_inc(dm_disk(md), ios[rw]);
767 disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio));
768
1da177e4
LT
769 /*
770 * If we're suspended we have to queue
771 * this io for later.
772 */
773 while (test_bit(DMF_BLOCK_IO, &md->flags)) {
2ca3310e 774 up_read(&md->io_lock);
1da177e4
LT
775
776 if (bio_rw(bio) == READA) {
777 bio_io_error(bio, bio->bi_size);
778 return 0;
779 }
780
781 r = queue_io(md, bio);
782 if (r < 0) {
783 bio_io_error(bio, bio->bi_size);
784 return 0;
785
786 } else if (r == 0)
787 return 0; /* deferred successfully */
788
789 /*
790 * We're in a while loop, because someone could suspend
791 * before we get to the following read lock.
792 */
2ca3310e 793 down_read(&md->io_lock);
1da177e4
LT
794 }
795
796 __split_bio(md, bio);
2ca3310e 797 up_read(&md->io_lock);
1da177e4
LT
798 return 0;
799}
800
801static int dm_flush_all(request_queue_t *q, struct gendisk *disk,
802 sector_t *error_sector)
803{
804 struct mapped_device *md = q->queuedata;
805 struct dm_table *map = dm_get_table(md);
806 int ret = -ENXIO;
807
808 if (map) {
cf222b37 809 ret = dm_table_flush_all(map);
1da177e4
LT
810 dm_table_put(map);
811 }
812
813 return ret;
814}
815
816static void dm_unplug_all(request_queue_t *q)
817{
818 struct mapped_device *md = q->queuedata;
819 struct dm_table *map = dm_get_table(md);
820
821 if (map) {
822 dm_table_unplug_all(map);
823 dm_table_put(map);
824 }
825}
826
827static int dm_any_congested(void *congested_data, int bdi_bits)
828{
829 int r;
830 struct mapped_device *md = (struct mapped_device *) congested_data;
831 struct dm_table *map = dm_get_table(md);
832
833 if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
834 r = bdi_bits;
835 else
836 r = dm_table_any_congested(map, bdi_bits);
837
838 dm_table_put(map);
839 return r;
840}
841
842/*-----------------------------------------------------------------
843 * An IDR is used to keep track of allocated minor numbers.
844 *---------------------------------------------------------------*/
1da177e4
LT
845static DEFINE_IDR(_minor_idr);
846
2b06cfff 847static void free_minor(int minor)
1da177e4 848{
f32c10b0 849 spin_lock(&_minor_lock);
1da177e4 850 idr_remove(&_minor_idr, minor);
f32c10b0 851 spin_unlock(&_minor_lock);
1da177e4
LT
852}
853
854/*
855 * See if the device with a specific minor # is free.
856 */
2b06cfff 857static int specific_minor(struct mapped_device *md, int minor)
1da177e4
LT
858{
859 int r, m;
860
861 if (minor >= (1 << MINORBITS))
862 return -EINVAL;
863
62f75c2f
JM
864 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
865 if (!r)
866 return -ENOMEM;
867
f32c10b0 868 spin_lock(&_minor_lock);
1da177e4
LT
869
870 if (idr_find(&_minor_idr, minor)) {
871 r = -EBUSY;
872 goto out;
873 }
874
ba61fdd1 875 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
62f75c2f 876 if (r)
1da177e4 877 goto out;
1da177e4
LT
878
879 if (m != minor) {
880 idr_remove(&_minor_idr, m);
881 r = -EBUSY;
882 goto out;
883 }
884
885out:
f32c10b0 886 spin_unlock(&_minor_lock);
1da177e4
LT
887 return r;
888}
889
2b06cfff 890static int next_free_minor(struct mapped_device *md, int *minor)
1da177e4 891{
2b06cfff 892 int r, m;
1da177e4 893
1da177e4 894 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
62f75c2f
JM
895 if (!r)
896 return -ENOMEM;
897
f32c10b0 898 spin_lock(&_minor_lock);
1da177e4 899
ba61fdd1 900 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
1da177e4
LT
901 if (r) {
902 goto out;
903 }
904
905 if (m >= (1 << MINORBITS)) {
906 idr_remove(&_minor_idr, m);
907 r = -ENOSPC;
908 goto out;
909 }
910
911 *minor = m;
912
913out:
f32c10b0 914 spin_unlock(&_minor_lock);
1da177e4
LT
915 return r;
916}
917
918static struct block_device_operations dm_blk_dops;
919
920/*
921 * Allocate and initialise a blank device with a given minor.
922 */
2b06cfff 923static struct mapped_device *alloc_dev(int minor)
1da177e4
LT
924{
925 int r;
926 struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
ba61fdd1 927 void *old_md;
1da177e4
LT
928
929 if (!md) {
930 DMWARN("unable to allocate device, out of memory.");
931 return NULL;
932 }
933
10da4f79
JM
934 if (!try_module_get(THIS_MODULE))
935 goto bad0;
936
1da177e4 937 /* get a minor number for the dev */
2b06cfff
AK
938 if (minor == DM_ANY_MINOR)
939 r = next_free_minor(md, &minor);
940 else
941 r = specific_minor(md, minor);
1da177e4
LT
942 if (r < 0)
943 goto bad1;
944
945 memset(md, 0, sizeof(*md));
2ca3310e
AK
946 init_rwsem(&md->io_lock);
947 init_MUTEX(&md->suspend_lock);
1da177e4
LT
948 rwlock_init(&md->map_lock);
949 atomic_set(&md->holders, 1);
5c6bd75d 950 atomic_set(&md->open_count, 0);
1da177e4
LT
951 atomic_set(&md->event_nr, 0);
952
953 md->queue = blk_alloc_queue(GFP_KERNEL);
954 if (!md->queue)
e3f6ac61 955 goto bad1_free_minor;
1da177e4
LT
956
957 md->queue->queuedata = md;
958 md->queue->backing_dev_info.congested_fn = dm_any_congested;
959 md->queue->backing_dev_info.congested_data = md;
960 blk_queue_make_request(md->queue, dm_request);
daef265f 961 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
1da177e4
LT
962 md->queue->unplug_fn = dm_unplug_all;
963 md->queue->issue_flush_fn = dm_flush_all;
964
93d2341c 965 md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
74859364
KU
966 if (!md->io_pool)
967 goto bad2;
1da177e4 968
93d2341c 969 md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
1da177e4
LT
970 if (!md->tio_pool)
971 goto bad3;
972
9faf400f
SB
973 md->bs = bioset_create(16, 16, 4);
974 if (!md->bs)
975 goto bad_no_bioset;
976
1da177e4
LT
977 md->disk = alloc_disk(1);
978 if (!md->disk)
979 goto bad4;
980
f0b04115
JM
981 atomic_set(&md->pending, 0);
982 init_waitqueue_head(&md->wait);
983 init_waitqueue_head(&md->eventq);
984
1da177e4
LT
985 md->disk->major = _major;
986 md->disk->first_minor = minor;
987 md->disk->fops = &dm_blk_dops;
988 md->disk->queue = md->queue;
989 md->disk->private_data = md;
990 sprintf(md->disk->disk_name, "dm-%d", minor);
991 add_disk(md->disk);
7e51f257 992 format_dev_t(md->name, MKDEV(_major, minor));
1da177e4 993
ba61fdd1 994 /* Populate the mapping, nobody knows we exist yet */
f32c10b0 995 spin_lock(&_minor_lock);
ba61fdd1 996 old_md = idr_replace(&_minor_idr, md, minor);
f32c10b0 997 spin_unlock(&_minor_lock);
ba61fdd1
JM
998
999 BUG_ON(old_md != MINOR_ALLOCED);
1000
1da177e4
LT
1001 return md;
1002
1003 bad4:
9faf400f
SB
1004 bioset_free(md->bs);
1005 bad_no_bioset:
1da177e4
LT
1006 mempool_destroy(md->tio_pool);
1007 bad3:
1008 mempool_destroy(md->io_pool);
1009 bad2:
1312f40e 1010 blk_cleanup_queue(md->queue);
e3f6ac61 1011 bad1_free_minor:
1da177e4
LT
1012 free_minor(minor);
1013 bad1:
10da4f79
JM
1014 module_put(THIS_MODULE);
1015 bad0:
1da177e4
LT
1016 kfree(md);
1017 return NULL;
1018}
1019
1020static void free_dev(struct mapped_device *md)
1021{
2b06cfff 1022 int minor = md->disk->first_minor;
63d94e48 1023
d9dde59b
JN
1024 if (md->suspended_bdev) {
1025 thaw_bdev(md->suspended_bdev, NULL);
1026 bdput(md->suspended_bdev);
1027 }
1da177e4
LT
1028 mempool_destroy(md->tio_pool);
1029 mempool_destroy(md->io_pool);
9faf400f 1030 bioset_free(md->bs);
1da177e4 1031 del_gendisk(md->disk);
63d94e48 1032 free_minor(minor);
fba9f90e
JM
1033
1034 spin_lock(&_minor_lock);
1035 md->disk->private_data = NULL;
1036 spin_unlock(&_minor_lock);
1037
1da177e4 1038 put_disk(md->disk);
1312f40e 1039 blk_cleanup_queue(md->queue);
10da4f79 1040 module_put(THIS_MODULE);
1da177e4
LT
1041 kfree(md);
1042}
1043
1044/*
1045 * Bind a table to the device.
1046 */
1047static void event_callback(void *context)
1048{
1049 struct mapped_device *md = (struct mapped_device *) context;
1050
1051 atomic_inc(&md->event_nr);
1052 wake_up(&md->eventq);
1053}
1054
4e90188b 1055static void __set_size(struct mapped_device *md, sector_t size)
1da177e4 1056{
4e90188b 1057 set_capacity(md->disk, size);
1da177e4 1058
1b1dcc1b 1059 mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
e39e2e95 1060 i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
1b1dcc1b 1061 mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
1da177e4
LT
1062}
1063
1064static int __bind(struct mapped_device *md, struct dm_table *t)
1065{
1066 request_queue_t *q = md->queue;
1067 sector_t size;
1068
1069 size = dm_table_get_size(t);
3ac51e74
DW
1070
1071 /*
1072 * Wipe any geometry if the size of the table changed.
1073 */
1074 if (size != get_capacity(md->disk))
1075 memset(&md->geometry, 0, sizeof(md->geometry));
1076
4e90188b 1077 __set_size(md, size);
1da177e4
LT
1078 if (size == 0)
1079 return 0;
1080
2ca3310e
AK
1081 dm_table_get(t);
1082 dm_table_event_callback(t, event_callback, md);
1083
1da177e4
LT
1084 write_lock(&md->map_lock);
1085 md->map = t;
2ca3310e 1086 dm_table_set_restrictions(t, q);
1da177e4
LT
1087 write_unlock(&md->map_lock);
1088
1da177e4
LT
1089 return 0;
1090}
1091
1092static void __unbind(struct mapped_device *md)
1093{
1094 struct dm_table *map = md->map;
1095
1096 if (!map)
1097 return;
1098
1099 dm_table_event_callback(map, NULL, NULL);
1100 write_lock(&md->map_lock);
1101 md->map = NULL;
1102 write_unlock(&md->map_lock);
1103 dm_table_put(map);
1104}
1105
1106/*
1107 * Constructor for a new device.
1108 */
2b06cfff 1109int dm_create(int minor, struct mapped_device **result)
1da177e4
LT
1110{
1111 struct mapped_device *md;
1112
2b06cfff 1113 md = alloc_dev(minor);
1da177e4
LT
1114 if (!md)
1115 return -ENXIO;
1116
1117 *result = md;
1118 return 0;
1119}
1120
637842cf 1121static struct mapped_device *dm_find_md(dev_t dev)
1da177e4
LT
1122{
1123 struct mapped_device *md;
1da177e4
LT
1124 unsigned minor = MINOR(dev);
1125
1126 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
1127 return NULL;
1128
f32c10b0 1129 spin_lock(&_minor_lock);
1da177e4
LT
1130
1131 md = idr_find(&_minor_idr, minor);
fba9f90e
JM
1132 if (md && (md == MINOR_ALLOCED ||
1133 (dm_disk(md)->first_minor != minor) ||
17b2f66f 1134 test_bit(DMF_FREEING, &md->flags))) {
637842cf 1135 md = NULL;
fba9f90e
JM
1136 goto out;
1137 }
1da177e4 1138
fba9f90e 1139out:
f32c10b0 1140 spin_unlock(&_minor_lock);
1da177e4 1141
637842cf
DT
1142 return md;
1143}
1144
d229a958
DT
1145struct mapped_device *dm_get_md(dev_t dev)
1146{
1147 struct mapped_device *md = dm_find_md(dev);
1148
1149 if (md)
1150 dm_get(md);
1151
1152 return md;
1153}
1154
9ade92a9 1155void *dm_get_mdptr(struct mapped_device *md)
637842cf 1156{
9ade92a9 1157 return md->interface_ptr;
1da177e4
LT
1158}
1159
1160void dm_set_mdptr(struct mapped_device *md, void *ptr)
1161{
1162 md->interface_ptr = ptr;
1163}
1164
1165void dm_get(struct mapped_device *md)
1166{
1167 atomic_inc(&md->holders);
1168}
1169
72d94861
AK
1170const char *dm_device_name(struct mapped_device *md)
1171{
1172 return md->name;
1173}
1174EXPORT_SYMBOL_GPL(dm_device_name);
1175
1da177e4
LT
1176void dm_put(struct mapped_device *md)
1177{
1134e5ae 1178 struct dm_table *map;
1da177e4 1179
fba9f90e
JM
1180 BUG_ON(test_bit(DMF_FREEING, &md->flags));
1181
f32c10b0 1182 if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
1134e5ae 1183 map = dm_get_table(md);
ba61fdd1 1184 idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor);
fba9f90e 1185 set_bit(DMF_FREEING, &md->flags);
f32c10b0 1186 spin_unlock(&_minor_lock);
cf222b37 1187 if (!dm_suspended(md)) {
1da177e4
LT
1188 dm_table_presuspend_targets(map);
1189 dm_table_postsuspend_targets(map);
1190 }
1191 __unbind(md);
1134e5ae 1192 dm_table_put(map);
1da177e4
LT
1193 free_dev(md);
1194 }
1da177e4
LT
1195}
1196
1197/*
1198 * Process the deferred bios
1199 */
1200static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
1201{
1202 struct bio *n;
1203
1204 while (c) {
1205 n = c->bi_next;
1206 c->bi_next = NULL;
1207 __split_bio(md, c);
1208 c = n;
1209 }
1210}
1211
1212/*
1213 * Swap in a new table (destroying old one).
1214 */
1215int dm_swap_table(struct mapped_device *md, struct dm_table *table)
1216{
93c534ae 1217 int r = -EINVAL;
1da177e4 1218
2ca3310e 1219 down(&md->suspend_lock);
1da177e4
LT
1220
1221 /* device must be suspended */
cf222b37 1222 if (!dm_suspended(md))
93c534ae 1223 goto out;
1da177e4
LT
1224
1225 __unbind(md);
1226 r = __bind(md, table);
1da177e4 1227
93c534ae 1228out:
2ca3310e 1229 up(&md->suspend_lock);
93c534ae 1230 return r;
1da177e4
LT
1231}
1232
1233/*
1234 * Functions to lock and unlock any filesystem running on the
1235 * device.
1236 */
2ca3310e 1237static int lock_fs(struct mapped_device *md)
1da177e4 1238{
e39e2e95 1239 int r;
1da177e4
LT
1240
1241 WARN_ON(md->frozen_sb);
dfbe03f6 1242
e39e2e95 1243 md->frozen_sb = freeze_bdev(md->suspended_bdev);
dfbe03f6 1244 if (IS_ERR(md->frozen_sb)) {
cf222b37 1245 r = PTR_ERR(md->frozen_sb);
e39e2e95
AK
1246 md->frozen_sb = NULL;
1247 return r;
dfbe03f6
AK
1248 }
1249
aa8d7c2f
AK
1250 set_bit(DMF_FROZEN, &md->flags);
1251
1da177e4 1252 /* don't bdput right now, we don't want the bdev
e39e2e95 1253 * to go away while it is locked.
1da177e4
LT
1254 */
1255 return 0;
1256}
1257
2ca3310e 1258static void unlock_fs(struct mapped_device *md)
1da177e4 1259{
aa8d7c2f
AK
1260 if (!test_bit(DMF_FROZEN, &md->flags))
1261 return;
1262
e39e2e95 1263 thaw_bdev(md->suspended_bdev, md->frozen_sb);
1da177e4 1264 md->frozen_sb = NULL;
aa8d7c2f 1265 clear_bit(DMF_FROZEN, &md->flags);
1da177e4
LT
1266}
1267
1268/*
1269 * We need to be able to change a mapping table under a mounted
1270 * filesystem. For example we might want to move some data in
1271 * the background. Before the table can be swapped with
1272 * dm_bind_table, dm_suspend must be called to flush any in
1273 * flight bios and ensure that any further io gets deferred.
1274 */
a3d77d35 1275int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
1da177e4 1276{
2ca3310e 1277 struct dm_table *map = NULL;
1da177e4 1278 DECLARE_WAITQUEUE(wait, current);
1ecac7fd 1279 struct bio *def;
cf222b37 1280 int r = -EINVAL;
a3d77d35 1281 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
1da177e4 1282
2ca3310e
AK
1283 down(&md->suspend_lock);
1284
1285 if (dm_suspended(md))
d287483d 1286 goto out_unlock;
1da177e4
LT
1287
1288 map = dm_get_table(md);
1da177e4 1289
cf222b37
AK
1290 /* This does not get reverted if there's an error later. */
1291 dm_table_presuspend_targets(map);
1292
e39e2e95
AK
1293 md->suspended_bdev = bdget_disk(md->disk, 0);
1294 if (!md->suspended_bdev) {
1295 DMWARN("bdget failed in dm_suspend");
1296 r = -ENOMEM;
1297 goto out;
1298 }
1299
cf222b37 1300 /* Flush I/O to the device. */
aa8d7c2f
AK
1301 if (do_lockfs) {
1302 r = lock_fs(md);
1303 if (r)
1304 goto out;
1305 }
1da177e4
LT
1306
1307 /*
354e0071 1308 * First we set the BLOCK_IO flag so no more ios will be mapped.
1da177e4 1309 */
2ca3310e
AK
1310 down_write(&md->io_lock);
1311 set_bit(DMF_BLOCK_IO, &md->flags);
1da177e4 1312
1da177e4 1313 add_wait_queue(&md->wait, &wait);
2ca3310e 1314 up_write(&md->io_lock);
1da177e4
LT
1315
1316 /* unplug */
2ca3310e 1317 if (map)
1da177e4 1318 dm_table_unplug_all(map);
1da177e4
LT
1319
1320 /*
1321 * Then we wait for the already mapped ios to
1322 * complete.
1323 */
1324 while (1) {
1325 set_current_state(TASK_INTERRUPTIBLE);
1326
1327 if (!atomic_read(&md->pending) || signal_pending(current))
1328 break;
1329
1330 io_schedule();
1331 }
1332 set_current_state(TASK_RUNNING);
1333
2ca3310e 1334 down_write(&md->io_lock);
1da177e4
LT
1335 remove_wait_queue(&md->wait, &wait);
1336
1337 /* were we interrupted ? */
cf222b37 1338 r = -EINTR;
2ca3310e 1339 if (atomic_read(&md->pending)) {
1ecac7fd
JN
1340 clear_bit(DMF_BLOCK_IO, &md->flags);
1341 def = bio_list_get(&md->deferred);
1342 __flush_deferred_io(md, def);
2ca3310e
AK
1343 up_write(&md->io_lock);
1344 unlock_fs(md);
2ca3310e
AK
1345 goto out;
1346 }
1347 up_write(&md->io_lock);
1da177e4 1348
cf222b37 1349 dm_table_postsuspend_targets(map);
1da177e4 1350
2ca3310e 1351 set_bit(DMF_SUSPENDED, &md->flags);
b84b0287 1352
2ca3310e 1353 r = 0;
b84b0287 1354
2ca3310e 1355out:
e39e2e95
AK
1356 if (r && md->suspended_bdev) {
1357 bdput(md->suspended_bdev);
1358 md->suspended_bdev = NULL;
1359 }
1360
2ca3310e 1361 dm_table_put(map);
d287483d
AK
1362
1363out_unlock:
2ca3310e 1364 up(&md->suspend_lock);
cf222b37 1365 return r;
1da177e4
LT
1366}
1367
1368int dm_resume(struct mapped_device *md)
1369{
cf222b37 1370 int r = -EINVAL;
1da177e4 1371 struct bio *def;
cf222b37 1372 struct dm_table *map = NULL;
1da177e4 1373
2ca3310e
AK
1374 down(&md->suspend_lock);
1375 if (!dm_suspended(md))
cf222b37 1376 goto out;
cf222b37
AK
1377
1378 map = dm_get_table(md);
2ca3310e 1379 if (!map || !dm_table_get_size(map))
cf222b37 1380 goto out;
1da177e4 1381
8757b776
MB
1382 r = dm_table_resume_targets(map);
1383 if (r)
1384 goto out;
2ca3310e
AK
1385
1386 down_write(&md->io_lock);
1da177e4
LT
1387 clear_bit(DMF_BLOCK_IO, &md->flags);
1388
1389 def = bio_list_get(&md->deferred);
1390 __flush_deferred_io(md, def);
2ca3310e
AK
1391 up_write(&md->io_lock);
1392
1393 unlock_fs(md);
1394
e39e2e95
AK
1395 bdput(md->suspended_bdev);
1396 md->suspended_bdev = NULL;
1397
2ca3310e
AK
1398 clear_bit(DMF_SUSPENDED, &md->flags);
1399
1da177e4 1400 dm_table_unplug_all(map);
1da177e4 1401
8560ed6f
HR
1402 kobject_uevent(&md->disk->kobj, KOBJ_CHANGE);
1403
cf222b37 1404 r = 0;
2ca3310e 1405
cf222b37
AK
1406out:
1407 dm_table_put(map);
2ca3310e
AK
1408 up(&md->suspend_lock);
1409
cf222b37 1410 return r;
1da177e4
LT
1411}
1412
1413/*-----------------------------------------------------------------
1414 * Event notification.
1415 *---------------------------------------------------------------*/
1416uint32_t dm_get_event_nr(struct mapped_device *md)
1417{
1418 return atomic_read(&md->event_nr);
1419}
1420
1421int dm_wait_event(struct mapped_device *md, int event_nr)
1422{
1423 return wait_event_interruptible(md->eventq,
1424 (event_nr != atomic_read(&md->event_nr)));
1425}
1426
1427/*
1428 * The gendisk is only valid as long as you have a reference
1429 * count on 'md'.
1430 */
1431struct gendisk *dm_disk(struct mapped_device *md)
1432{
1433 return md->disk;
1434}
1435
1436int dm_suspended(struct mapped_device *md)
1437{
1438 return test_bit(DMF_SUSPENDED, &md->flags);
1439}
1440
1441static struct block_device_operations dm_blk_dops = {
1442 .open = dm_blk_open,
1443 .release = dm_blk_close,
aa129a22 1444 .ioctl = dm_blk_ioctl,
3ac51e74 1445 .getgeo = dm_blk_getgeo,
1da177e4
LT
1446 .owner = THIS_MODULE
1447};
1448
1449EXPORT_SYMBOL(dm_get_mapinfo);
1450
1451/*
1452 * module hooks
1453 */
1454module_init(dm_init);
1455module_exit(dm_exit);
1456
1457module_param(major, uint, 0);
1458MODULE_PARM_DESC(major, "The major number of the device mapper");
1459MODULE_DESCRIPTION(DM_NAME " driver");
1460MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1461MODULE_LICENSE("GPL");
This page took 0.271405 seconds and 5 git commands to generate.