blob: 41c9549b32adc950cb2f4082ba173513dd11ae72 [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 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +010048struct dm_target_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 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)
Alasdair G Kergon028867a2007-07-12 17:26:32 +010057 return &((struct dm_target_io *)bio->bi_private)->info;
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070058 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
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -080071#define DMF_NOFLUSH_SUSPENDING 5
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73struct mapped_device {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -070074 struct rw_semaphore io_lock;
75 struct semaphore suspend_lock;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -080076 spinlock_t pushback_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 rwlock_t map_lock;
78 atomic_t holders;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -070079 atomic_t open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 unsigned long flags;
82
Jens Axboe165125e2007-07-24 09:28:11 +020083 struct request_queue *queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 struct gendisk *disk;
Mike Anderson7e51f252006-03-27 01:17:52 -080085 char name[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 void *interface_ptr;
88
89 /*
90 * A list of ios that arrived while we were suspended.
91 */
92 atomic_t pending;
93 wait_queue_head_t wait;
Kiyoshi Ueda74859362006-12-08 02:41:02 -080094 struct bio_list deferred;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -080095 struct bio_list pushback;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 /*
98 * The current mapping.
99 */
100 struct dm_table *map;
101
102 /*
103 * io objects are allocated from here.
104 */
105 mempool_t *io_pool;
106 mempool_t *tio_pool;
107
Stefan Bader9faf4002006-10-03 01:15:41 -0700108 struct bio_set *bs;
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 /*
111 * Event handling.
112 */
113 atomic_t event_nr;
114 wait_queue_head_t eventq;
115
116 /*
117 * freeze/thaw support require holding onto a super block
118 */
119 struct super_block *frozen_sb;
Alasdair G Kergone39e2e92006-01-06 00:20:05 -0800120 struct block_device *suspended_bdev;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800121
122 /* forced geometry settings */
123 struct hd_geometry geometry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
126#define MIN_IOS 256
Christoph Lametere18b8902006-12-06 20:33:20 -0800127static struct kmem_cache *_io_cache;
128static struct kmem_cache *_tio_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130static int __init local_init(void)
131{
132 int r;
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100135 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 if (!_io_cache)
137 return -ENOMEM;
138
139 /* allocate a slab for the target ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100140 _tio_cache = KMEM_CACHE(dm_target_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (!_tio_cache) {
142 kmem_cache_destroy(_io_cache);
143 return -ENOMEM;
144 }
145
146 _major = major;
147 r = register_blkdev(_major, _name);
148 if (r < 0) {
149 kmem_cache_destroy(_tio_cache);
150 kmem_cache_destroy(_io_cache);
151 return r;
152 }
153
154 if (!_major)
155 _major = r;
156
157 return 0;
158}
159
160static void local_exit(void)
161{
162 kmem_cache_destroy(_tio_cache);
163 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700164 unregister_blkdev(_major, _name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 _major = 0;
167
168 DMINFO("cleaned up");
169}
170
171int (*_inits[])(void) __initdata = {
172 local_init,
173 dm_target_init,
174 dm_linear_init,
175 dm_stripe_init,
176 dm_interface_init,
177};
178
179void (*_exits[])(void) = {
180 local_exit,
181 dm_target_exit,
182 dm_linear_exit,
183 dm_stripe_exit,
184 dm_interface_exit,
185};
186
187static int __init dm_init(void)
188{
189 const int count = ARRAY_SIZE(_inits);
190
191 int r, i;
192
193 for (i = 0; i < count; i++) {
194 r = _inits[i]();
195 if (r)
196 goto bad;
197 }
198
199 return 0;
200
201 bad:
202 while (i--)
203 _exits[i]();
204
205 return r;
206}
207
208static void __exit dm_exit(void)
209{
210 int i = ARRAY_SIZE(_exits);
211
212 while (i--)
213 _exits[i]();
214}
215
216/*
217 * Block device functions
218 */
219static int dm_blk_open(struct inode *inode, struct file *file)
220{
221 struct mapped_device *md;
222
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700223 spin_lock(&_minor_lock);
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 md = inode->i_bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700226 if (!md)
227 goto out;
228
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700229 if (test_bit(DMF_FREEING, &md->flags) ||
230 test_bit(DMF_DELETING, &md->flags)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700231 md = NULL;
232 goto out;
233 }
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700236 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700237
238out:
239 spin_unlock(&_minor_lock);
240
241 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
244static int dm_blk_close(struct inode *inode, struct file *file)
245{
246 struct mapped_device *md;
247
248 md = inode->i_bdev->bd_disk->private_data;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700249 atomic_dec(&md->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 dm_put(md);
251 return 0;
252}
253
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700254int dm_open_count(struct mapped_device *md)
255{
256 return atomic_read(&md->open_count);
257}
258
259/*
260 * Guarantees nothing is using the device before it's deleted.
261 */
262int dm_lock_for_deletion(struct mapped_device *md)
263{
264 int r = 0;
265
266 spin_lock(&_minor_lock);
267
268 if (dm_open_count(md))
269 r = -EBUSY;
270 else
271 set_bit(DMF_DELETING, &md->flags);
272
273 spin_unlock(&_minor_lock);
274
275 return r;
276}
277
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800278static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
279{
280 struct mapped_device *md = bdev->bd_disk->private_data;
281
282 return dm_get_geometry(md, geo);
283}
284
Milan Brozaa129a22006-10-03 01:15:15 -0700285static int dm_blk_ioctl(struct inode *inode, struct file *file,
286 unsigned int cmd, unsigned long arg)
287{
288 struct mapped_device *md;
289 struct dm_table *map;
290 struct dm_target *tgt;
291 int r = -ENOTTY;
292
293 /* We don't really need this lock, but we do need 'inode'. */
294 unlock_kernel();
295
296 md = inode->i_bdev->bd_disk->private_data;
297
298 map = dm_get_table(md);
299
300 if (!map || !dm_table_get_size(map))
301 goto out;
302
303 /* We only support devices that have a single target */
304 if (dm_table_get_num_targets(map) != 1)
305 goto out;
306
307 tgt = dm_table_get_target(map, 0);
308
309 if (dm_suspended(md)) {
310 r = -EAGAIN;
311 goto out;
312 }
313
314 if (tgt->type->ioctl)
315 r = tgt->type->ioctl(tgt, inode, file, cmd, arg);
316
317out:
318 dm_table_put(map);
319
320 lock_kernel();
321 return r;
322}
323
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100324static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 return mempool_alloc(md->io_pool, GFP_NOIO);
327}
328
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100329static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 mempool_free(io, md->io_pool);
332}
333
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100334static struct dm_target_io *alloc_tio(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 return mempool_alloc(md->tio_pool, GFP_NOIO);
337}
338
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100339static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 mempool_free(tio, md->tio_pool);
342}
343
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800344static void start_io_acct(struct dm_io *io)
345{
346 struct mapped_device *md = io->md;
347
348 io->start_time = jiffies;
349
350 preempt_disable();
351 disk_round_stats(dm_disk(md));
352 preempt_enable();
353 dm_disk(md)->in_flight = atomic_inc_return(&md->pending);
354}
355
356static int end_io_acct(struct dm_io *io)
357{
358 struct mapped_device *md = io->md;
359 struct bio *bio = io->bio;
360 unsigned long duration = jiffies - io->start_time;
361 int pending;
362 int rw = bio_data_dir(bio);
363
364 preempt_disable();
365 disk_round_stats(dm_disk(md));
366 preempt_enable();
367 dm_disk(md)->in_flight = pending = atomic_dec_return(&md->pending);
368
369 disk_stat_add(dm_disk(md), ticks[rw], duration);
370
371 return !pending;
372}
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374/*
375 * Add the bio to the list of deferred io.
376 */
377static int queue_io(struct mapped_device *md, struct bio *bio)
378{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700379 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700382 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return 1;
384 }
385
386 bio_list_add(&md->deferred, bio);
387
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700388 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return 0; /* deferred successfully */
390}
391
392/*
393 * Everyone (including functions in this file), should use this
394 * function to access the md->map field, and make sure they call
395 * dm_table_put() when finished.
396 */
397struct dm_table *dm_get_table(struct mapped_device *md)
398{
399 struct dm_table *t;
400
401 read_lock(&md->map_lock);
402 t = md->map;
403 if (t)
404 dm_table_get(t);
405 read_unlock(&md->map_lock);
406
407 return t;
408}
409
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800410/*
411 * Get the geometry associated with a dm device
412 */
413int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
414{
415 *geo = md->geometry;
416
417 return 0;
418}
419
420/*
421 * Set the geometry of a device.
422 */
423int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
424{
425 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
426
427 if (geo->start > sz) {
428 DMWARN("Start sector is beyond the geometry limits.");
429 return -EINVAL;
430 }
431
432 md->geometry = *geo;
433
434 return 0;
435}
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437/*-----------------------------------------------------------------
438 * CRUD START:
439 * A more elegant soln is in the works that uses the queue
440 * merge fn, unfortunately there are a couple of changes to
441 * the block layer that I want to make for this. So in the
442 * interests of getting something for people to use I give
443 * you this clearly demarcated crap.
444 *---------------------------------------------------------------*/
445
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800446static int __noflush_suspending(struct mapped_device *md)
447{
448 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
449}
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451/*
452 * Decrements the number of outstanding ios that a bio has been
453 * cloned into, completing the original io if necc.
454 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800455static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800457 unsigned long flags;
458
459 /* Push-back supersedes any I/O errors */
460 if (error && !(io->error > 0 && __noflush_suspending(io->md)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 io->error = error;
462
463 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800464 if (io->error == DM_ENDIO_REQUEUE) {
465 /*
466 * Target requested pushing back the I/O.
467 * This must be handled before the sleeper on
468 * suspend queue merges the pushback list.
469 */
470 spin_lock_irqsave(&io->md->pushback_lock, flags);
471 if (__noflush_suspending(io->md))
472 bio_list_add(&io->md->pushback, io->bio);
473 else
474 /* noflush suspend was interrupted. */
475 io->error = -EIO;
476 spin_unlock_irqrestore(&io->md->pushback_lock, flags);
477 }
478
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800479 if (end_io_acct(io))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 /* nudge anyone waiting on suspend queue */
481 wake_up(&io->md->wait);
482
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800483 if (io->error != DM_ENDIO_REQUEUE) {
484 blk_add_trace_bio(io->md->queue, io->bio,
485 BLK_TA_COMPLETE);
Jens Axboe2056a782006-03-23 20:00:26 +0100486
NeilBrown6712ecf2007-09-27 12:47:43 +0200487 bio_endio(io->bio, io->error);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800488 }
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 free_io(io->md, io);
491 }
492}
493
NeilBrown6712ecf2007-09-27 12:47:43 +0200494static void clone_endio(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 int r = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100497 struct dm_target_io *tio = bio->bi_private;
Stefan Bader9faf4002006-10-03 01:15:41 -0700498 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 dm_endio_fn endio = tio->ti->type->end_io;
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
502 error = -EIO;
503
504 if (endio) {
505 r = endio(tio->ti, bio, error, &tio->info);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800506 if (r < 0 || r == DM_ENDIO_REQUEUE)
507 /*
508 * error and requeue request are handled
509 * in dec_pending().
510 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800512 else if (r == DM_ENDIO_INCOMPLETE)
513 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +0200514 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800515 else if (r) {
516 DMWARN("unimplemented target endio return value: %d", r);
517 BUG();
518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520
Stefan Bader9faf4002006-10-03 01:15:41 -0700521 dec_pending(tio->io, error);
522
523 /*
524 * Store md for cleanup instead of tio which is about to get freed.
525 */
526 bio->bi_private = md->bs;
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 bio_put(bio);
Stefan Bader9faf4002006-10-03 01:15:41 -0700529 free_tio(md, tio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
532static sector_t max_io_len(struct mapped_device *md,
533 sector_t sector, struct dm_target *ti)
534{
535 sector_t offset = sector - ti->begin;
536 sector_t len = ti->len - offset;
537
538 /*
539 * Does the target need to split even further ?
540 */
541 if (ti->split_io) {
542 sector_t boundary;
543 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
544 - offset;
545 if (len > boundary)
546 len = boundary;
547 }
548
549 return len;
550}
551
552static void __map_bio(struct dm_target *ti, struct bio *clone,
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100553 struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
555 int r;
Jens Axboe2056a782006-03-23 20:00:26 +0100556 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -0700557 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 /*
560 * Sanity checks.
561 */
562 BUG_ON(!clone->bi_size);
563
564 clone->bi_end_io = clone_endio;
565 clone->bi_private = tio;
566
567 /*
568 * Map the clone. If r == 0 we don't need to do
569 * anything, the target has assumed ownership of
570 * this io.
571 */
572 atomic_inc(&tio->io->io_count);
Jens Axboe2056a782006-03-23 20:00:26 +0100573 sector = clone->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 r = ti->type->map(ti, clone, &tio->info);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800575 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +0100577
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700578 blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone,
Alan D. Brunellec7149d62007-08-07 15:30:23 +0200579 tio->io->bio->bi_bdev->bd_dev,
580 clone->bi_sector, sector);
Jens Axboe2056a782006-03-23 20:00:26 +0100581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800583 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
584 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -0700585 md = tio->io->md;
586 dec_pending(tio->io, r);
587 /*
588 * Store bio_set for cleanup.
589 */
590 clone->bi_private = md->bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 bio_put(clone);
Stefan Bader9faf4002006-10-03 01:15:41 -0700592 free_tio(md, tio);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800593 } else if (r) {
594 DMWARN("unimplemented target map return value: %d", r);
595 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
597}
598
599struct clone_info {
600 struct mapped_device *md;
601 struct dm_table *map;
602 struct bio *bio;
603 struct dm_io *io;
604 sector_t sector;
605 sector_t sector_count;
606 unsigned short idx;
607};
608
Peter Osterlund36763472005-09-06 15:16:42 -0700609static void dm_bio_destructor(struct bio *bio)
610{
Stefan Bader9faf4002006-10-03 01:15:41 -0700611 struct bio_set *bs = bio->bi_private;
612
613 bio_free(bio, bs);
Peter Osterlund36763472005-09-06 15:16:42 -0700614}
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616/*
617 * Creates a little bio that is just does part of a bvec.
618 */
619static struct bio *split_bvec(struct bio *bio, sector_t sector,
620 unsigned short idx, unsigned int offset,
Stefan Bader9faf4002006-10-03 01:15:41 -0700621 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 struct bio *clone;
624 struct bio_vec *bv = bio->bi_io_vec + idx;
625
Stefan Bader9faf4002006-10-03 01:15:41 -0700626 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
Peter Osterlund36763472005-09-06 15:16:42 -0700627 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 *clone->bi_io_vec = *bv;
629
630 clone->bi_sector = sector;
631 clone->bi_bdev = bio->bi_bdev;
632 clone->bi_rw = bio->bi_rw;
633 clone->bi_vcnt = 1;
634 clone->bi_size = to_bytes(len);
635 clone->bi_io_vec->bv_offset = offset;
636 clone->bi_io_vec->bv_len = clone->bi_size;
637
638 return clone;
639}
640
641/*
642 * Creates a bio that consists of range of complete bvecs.
643 */
644static struct bio *clone_bio(struct bio *bio, sector_t sector,
645 unsigned short idx, unsigned short bv_count,
Stefan Bader9faf4002006-10-03 01:15:41 -0700646 unsigned int len, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 struct bio *clone;
649
Stefan Bader9faf4002006-10-03 01:15:41 -0700650 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
651 __bio_clone(clone, bio);
652 clone->bi_destructor = dm_bio_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 clone->bi_sector = sector;
654 clone->bi_idx = idx;
655 clone->bi_vcnt = idx + bv_count;
656 clone->bi_size = to_bytes(len);
657 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
658
659 return clone;
660}
661
662static void __clone_and_map(struct clone_info *ci)
663{
664 struct bio *clone, *bio = ci->bio;
665 struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
666 sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100667 struct dm_target_io *tio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 /*
670 * Allocate a target io object.
671 */
672 tio = alloc_tio(ci->md);
673 tio->io = ci->io;
674 tio->ti = ti;
675 memset(&tio->info, 0, sizeof(tio->info));
676
677 if (ci->sector_count <= max) {
678 /*
679 * Optimise for the simple case where we can do all of
680 * the remaining io with a single clone.
681 */
682 clone = clone_bio(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -0700683 bio->bi_vcnt - ci->idx, ci->sector_count,
684 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 __map_bio(ti, clone, tio);
686 ci->sector_count = 0;
687
688 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
689 /*
690 * There are some bvecs that don't span targets.
691 * Do as many of these as possible.
692 */
693 int i;
694 sector_t remaining = max;
695 sector_t bv_len;
696
697 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
698 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
699
700 if (bv_len > remaining)
701 break;
702
703 remaining -= bv_len;
704 len += bv_len;
705 }
706
Stefan Bader9faf4002006-10-03 01:15:41 -0700707 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
708 ci->md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 __map_bio(ti, clone, tio);
710
711 ci->sector += len;
712 ci->sector_count -= len;
713 ci->idx = i;
714
715 } else {
716 /*
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800717 * Handle a bvec that must be split between two or more targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 */
719 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800720 sector_t remaining = to_sector(bv->bv_len);
721 unsigned int offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800723 do {
724 if (offset) {
725 ti = dm_table_find_target(ci->map, ci->sector);
726 max = max_io_len(ci->md, ci->sector, ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800728 tio = alloc_tio(ci->md);
729 tio->io = ci->io;
730 tio->ti = ti;
731 memset(&tio->info, 0, sizeof(tio->info));
732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800734 len = min(remaining, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800736 clone = split_bvec(bio, ci->sector, ci->idx,
Stefan Bader9faf4002006-10-03 01:15:41 -0700737 bv->bv_offset + offset, len,
738 ci->md->bs);
Alasdair G Kergond2044a92006-03-22 00:07:42 -0800739
740 __map_bio(ti, clone, tio);
741
742 ci->sector += len;
743 ci->sector_count -= len;
744 offset += to_bytes(len);
745 } while (remaining -= len);
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 ci->idx++;
748 }
749}
750
751/*
752 * Split the bio into several clones.
753 */
Milan Broz9e4e5f82007-10-19 22:38:53 +0100754static int __split_bio(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
756 struct clone_info ci;
757
758 ci.map = dm_get_table(md);
Milan Broz9e4e5f82007-10-19 22:38:53 +0100759 if (unlikely(!ci.map))
760 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 ci.md = md;
763 ci.bio = bio;
764 ci.io = alloc_io(md);
765 ci.io->error = 0;
766 atomic_set(&ci.io->io_count, 1);
767 ci.io->bio = bio;
768 ci.io->md = md;
769 ci.sector = bio->bi_sector;
770 ci.sector_count = bio_sectors(bio);
771 ci.idx = bio->bi_idx;
772
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800773 start_io_acct(ci.io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 while (ci.sector_count)
775 __clone_and_map(&ci);
776
777 /* drop the extra reference count */
778 dec_pending(ci.io, 0);
779 dm_table_put(ci.map);
Milan Broz9e4e5f82007-10-19 22:38:53 +0100780
781 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783/*-----------------------------------------------------------------
784 * CRUD END
785 *---------------------------------------------------------------*/
786
787/*
788 * The request function that just remaps the bio built up by
789 * dm_merge_bvec.
790 */
Jens Axboe165125e2007-07-24 09:28:11 +0200791static int dm_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Milan Broz9e4e5f82007-10-19 22:38:53 +0100793 int r = -EIO;
Kevin Corry12f03a42006-02-01 03:04:52 -0800794 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 struct mapped_device *md = q->queuedata;
796
Stefan Bader07a83c42007-07-12 17:28:33 +0100797 /*
798 * There is no use in forwarding any barrier request since we can't
799 * guarantee it is (or can be) handled by the targets correctly.
800 */
801 if (unlikely(bio_barrier(bio))) {
NeilBrown6712ecf2007-09-27 12:47:43 +0200802 bio_endio(bio, -EOPNOTSUPP);
Stefan Bader07a83c42007-07-12 17:28:33 +0100803 return 0;
804 }
805
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700806 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Kevin Corry12f03a42006-02-01 03:04:52 -0800808 disk_stat_inc(dm_disk(md), ios[rw]);
809 disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio));
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 /*
812 * If we're suspended we have to queue
813 * this io for later.
814 */
815 while (test_bit(DMF_BLOCK_IO, &md->flags)) {
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700816 up_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Milan Broz9e4e5f82007-10-19 22:38:53 +0100818 if (bio_rw(bio) != READA)
819 r = queue_io(md, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Milan Broz9e4e5f82007-10-19 22:38:53 +0100821 if (r <= 0)
822 goto out_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 /*
825 * We're in a while loop, because someone could suspend
826 * before we get to the following read lock.
827 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700828 down_read(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830
Milan Broz9e4e5f82007-10-19 22:38:53 +0100831 r = __split_bio(md, bio);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700832 up_read(&md->io_lock);
Milan Broz9e4e5f82007-10-19 22:38:53 +0100833
834out_req:
835 if (r < 0)
836 bio_io_error(bio);
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return 0;
839}
840
Jens Axboe165125e2007-07-24 09:28:11 +0200841static void dm_unplug_all(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
843 struct mapped_device *md = q->queuedata;
844 struct dm_table *map = dm_get_table(md);
845
846 if (map) {
847 dm_table_unplug_all(map);
848 dm_table_put(map);
849 }
850}
851
852static int dm_any_congested(void *congested_data, int bdi_bits)
853{
854 int r;
855 struct mapped_device *md = (struct mapped_device *) congested_data;
856 struct dm_table *map = dm_get_table(md);
857
858 if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
859 r = bdi_bits;
860 else
861 r = dm_table_any_congested(map, bdi_bits);
862
863 dm_table_put(map);
864 return r;
865}
866
867/*-----------------------------------------------------------------
868 * An IDR is used to keep track of allocated minor numbers.
869 *---------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870static DEFINE_IDR(_minor_idr);
871
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700872static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -0700874 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -0700876 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
879/*
880 * See if the device with a specific minor # is free.
881 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700882static int specific_minor(struct mapped_device *md, int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
884 int r, m;
885
886 if (minor >= (1 << MINORBITS))
887 return -EINVAL;
888
Jeff Mahoney62f75c22006-06-26 00:27:21 -0700889 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
890 if (!r)
891 return -ENOMEM;
892
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -0700893 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 if (idr_find(&_minor_idr, minor)) {
896 r = -EBUSY;
897 goto out;
898 }
899
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700900 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
Jeff Mahoney62f75c22006-06-26 00:27:21 -0700901 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 if (m != minor) {
905 idr_remove(&_minor_idr, m);
906 r = -EBUSY;
907 goto out;
908 }
909
910out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -0700911 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return r;
913}
914
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700915static int next_free_minor(struct mapped_device *md, int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700917 int r, m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
Jeff Mahoney62f75c22006-06-26 00:27:21 -0700920 if (!r)
921 return -ENOMEM;
922
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -0700923 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700925 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (r) {
927 goto out;
928 }
929
930 if (m >= (1 << MINORBITS)) {
931 idr_remove(&_minor_idr, m);
932 r = -ENOSPC;
933 goto out;
934 }
935
936 *minor = m;
937
938out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -0700939 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return r;
941}
942
943static struct block_device_operations dm_blk_dops;
944
945/*
946 * Allocate and initialise a blank device with a given minor.
947 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700948static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
950 int r;
951 struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700952 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 if (!md) {
955 DMWARN("unable to allocate device, out of memory.");
956 return NULL;
957 }
958
Jeff Mahoney10da4f72006-06-26 00:27:25 -0700959 if (!try_module_get(THIS_MODULE))
960 goto bad0;
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700963 if (minor == DM_ANY_MINOR)
964 r = next_free_minor(md, &minor);
965 else
966 r = specific_minor(md, minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (r < 0)
968 goto bad1;
969
970 memset(md, 0, sizeof(*md));
Alasdair G Kergon2ca33102005-07-28 21:16:00 -0700971 init_rwsem(&md->io_lock);
972 init_MUTEX(&md->suspend_lock);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800973 spin_lock_init(&md->pushback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 rwlock_init(&md->map_lock);
975 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700976 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 atomic_set(&md->event_nr, 0);
978
979 md->queue = blk_alloc_queue(GFP_KERNEL);
980 if (!md->queue)
Ishai Rabinovitze3f6ac62006-10-03 01:15:22 -0700981 goto bad1_free_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 md->queue->queuedata = md;
984 md->queue->backing_dev_info.congested_fn = dm_any_congested;
985 md->queue->backing_dev_info.congested_data = md;
986 blk_queue_make_request(md->queue, dm_request);
Jens Axboedaef2652006-01-10 10:48:02 +0100987 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 md->queue->unplug_fn = dm_unplug_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Matthew Dobson93d23412006-03-26 01:37:50 -0800990 md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
Kiyoshi Ueda74859362006-12-08 02:41:02 -0800991 if (!md->io_pool)
992 goto bad2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Matthew Dobson93d23412006-03-26 01:37:50 -0800994 md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (!md->tio_pool)
996 goto bad3;
997
Jens Axboe59725112007-04-02 10:06:42 +0200998 md->bs = bioset_create(16, 16);
Stefan Bader9faf4002006-10-03 01:15:41 -0700999 if (!md->bs)
1000 goto bad_no_bioset;
1001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 md->disk = alloc_disk(1);
1003 if (!md->disk)
1004 goto bad4;
1005
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001006 atomic_set(&md->pending, 0);
1007 init_waitqueue_head(&md->wait);
1008 init_waitqueue_head(&md->eventq);
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 md->disk->major = _major;
1011 md->disk->first_minor = minor;
1012 md->disk->fops = &dm_blk_dops;
1013 md->disk->queue = md->queue;
1014 md->disk->private_data = md;
1015 sprintf(md->disk->disk_name, "dm-%d", minor);
1016 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08001017 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001019 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001020 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001021 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001022 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001023
1024 BUG_ON(old_md != MINOR_ALLOCED);
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return md;
1027
1028 bad4:
Stefan Bader9faf4002006-10-03 01:15:41 -07001029 bioset_free(md->bs);
1030 bad_no_bioset:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 mempool_destroy(md->tio_pool);
1032 bad3:
1033 mempool_destroy(md->io_pool);
1034 bad2:
Al Viro1312f402006-03-12 11:02:03 -05001035 blk_cleanup_queue(md->queue);
Ishai Rabinovitze3f6ac62006-10-03 01:15:22 -07001036 bad1_free_minor:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 free_minor(minor);
1038 bad1:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001039 module_put(THIS_MODULE);
1040 bad0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 kfree(md);
1042 return NULL;
1043}
1044
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01001045static void unlock_fs(struct mapped_device *md);
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047static void free_dev(struct mapped_device *md)
1048{
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001049 int minor = md->disk->first_minor;
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001050
Jun'ichi Nomurad9dde592006-02-24 13:04:24 -08001051 if (md->suspended_bdev) {
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01001052 unlock_fs(md);
Jun'ichi Nomurad9dde592006-02-24 13:04:24 -08001053 bdput(md->suspended_bdev);
1054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 mempool_destroy(md->tio_pool);
1056 mempool_destroy(md->io_pool);
Stefan Bader9faf4002006-10-03 01:15:41 -07001057 bioset_free(md->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 del_gendisk(md->disk);
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08001059 free_minor(minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001060
1061 spin_lock(&_minor_lock);
1062 md->disk->private_data = NULL;
1063 spin_unlock(&_minor_lock);
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 put_disk(md->disk);
Al Viro1312f402006-03-12 11:02:03 -05001066 blk_cleanup_queue(md->queue);
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001067 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 kfree(md);
1069}
1070
1071/*
1072 * Bind a table to the device.
1073 */
1074static void event_callback(void *context)
1075{
1076 struct mapped_device *md = (struct mapped_device *) context;
1077
1078 atomic_inc(&md->event_nr);
1079 wake_up(&md->eventq);
1080}
1081
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07001082static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07001084 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001086 mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001087 i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001088 mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
1090
1091static int __bind(struct mapped_device *md, struct dm_table *t)
1092{
Jens Axboe165125e2007-07-24 09:28:11 +02001093 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 sector_t size;
1095
1096 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001097
1098 /*
1099 * Wipe any geometry if the size of the table changed.
1100 */
1101 if (size != get_capacity(md->disk))
1102 memset(&md->geometry, 0, sizeof(md->geometry));
1103
Jun'ichi Nomurabfa152f2007-01-26 00:57:07 -08001104 if (md->suspended_bdev)
1105 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (size == 0)
1107 return 0;
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 dm_table_get(t);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001110 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001111
1112 write_lock(&md->map_lock);
1113 md->map = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 dm_table_set_restrictions(t, q);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001115 write_unlock(&md->map_lock);
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 return 0;
1118}
1119
1120static void __unbind(struct mapped_device *md)
1121{
1122 struct dm_table *map = md->map;
1123
1124 if (!map)
1125 return;
1126
1127 dm_table_event_callback(map, NULL, NULL);
1128 write_lock(&md->map_lock);
1129 md->map = NULL;
1130 write_unlock(&md->map_lock);
1131 dm_table_put(map);
1132}
1133
1134/*
1135 * Constructor for a new device.
1136 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001137int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
1139 struct mapped_device *md;
1140
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001141 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (!md)
1143 return -ENXIO;
1144
1145 *result = md;
1146 return 0;
1147}
1148
David Teigland637842c2006-01-06 00:20:00 -08001149static struct mapped_device *dm_find_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
1151 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 unsigned minor = MINOR(dev);
1153
1154 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
1155 return NULL;
1156
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001157 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 md = idr_find(&_minor_idr, minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001160 if (md && (md == MINOR_ALLOCED ||
1161 (dm_disk(md)->first_minor != minor) ||
Alasdair G Kergon17b2f662006-06-26 00:27:33 -07001162 test_bit(DMF_FREEING, &md->flags))) {
David Teigland637842c2006-01-06 00:20:00 -08001163 md = NULL;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001164 goto out;
1165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001167out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001168 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
David Teigland637842c2006-01-06 00:20:00 -08001170 return md;
1171}
1172
David Teiglandd229a952006-01-06 00:20:01 -08001173struct mapped_device *dm_get_md(dev_t dev)
1174{
1175 struct mapped_device *md = dm_find_md(dev);
1176
1177 if (md)
1178 dm_get(md);
1179
1180 return md;
1181}
1182
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08001183void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08001184{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08001185 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186}
1187
1188void dm_set_mdptr(struct mapped_device *md, void *ptr)
1189{
1190 md->interface_ptr = ptr;
1191}
1192
1193void dm_get(struct mapped_device *md)
1194{
1195 atomic_inc(&md->holders);
1196}
1197
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001198const char *dm_device_name(struct mapped_device *md)
1199{
1200 return md->name;
1201}
1202EXPORT_SYMBOL_GPL(dm_device_name);
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204void dm_put(struct mapped_device *md)
1205{
Mike Anderson1134e5a2006-03-27 01:17:54 -08001206 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001208 BUG_ON(test_bit(DMF_FREEING, &md->flags));
1209
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001210 if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
Mike Anderson1134e5a2006-03-27 01:17:54 -08001211 map = dm_get_table(md);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001212 idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07001213 set_bit(DMF_FREEING, &md->flags);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001214 spin_unlock(&_minor_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001215 if (!dm_suspended(md)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 dm_table_presuspend_targets(map);
1217 dm_table_postsuspend_targets(map);
1218 }
1219 __unbind(md);
Mike Anderson1134e5a2006-03-27 01:17:54 -08001220 dm_table_put(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 free_dev(md);
1222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
Edward Goggin79eb8852007-05-09 02:32:56 -07001224EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226/*
1227 * Process the deferred bios
1228 */
1229static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
1230{
1231 struct bio *n;
1232
1233 while (c) {
1234 n = c->bi_next;
1235 c->bi_next = NULL;
Milan Broz9e4e5f82007-10-19 22:38:53 +01001236 if (__split_bio(md, c))
1237 bio_io_error(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 c = n;
1239 }
1240}
1241
1242/*
1243 * Swap in a new table (destroying old one).
1244 */
1245int dm_swap_table(struct mapped_device *md, struct dm_table *table)
1246{
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001247 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001249 down(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 /* device must be suspended */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001252 if (!dm_suspended(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001253 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Jun'ichi Nomurabfa152f2007-01-26 00:57:07 -08001255 /* without bdev, the device size cannot be changed */
1256 if (!md->suspended_bdev)
1257 if (get_capacity(md->disk) != dm_table_get_size(table))
1258 goto out;
1259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 __unbind(md);
1261 r = __bind(md, table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001263out:
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001264 up(&md->suspend_lock);
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07001265 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
1267
1268/*
1269 * Functions to lock and unlock any filesystem running on the
1270 * device.
1271 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001272static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001274 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07001277
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001278 md->frozen_sb = freeze_bdev(md->suspended_bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07001279 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001280 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001281 md->frozen_sb = NULL;
1282 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07001283 }
1284
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001285 set_bit(DMF_FROZEN, &md->flags);
1286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 /* don't bdput right now, we don't want the bdev
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001288 * to go away while it is locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 */
1290 return 0;
1291}
1292
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001293static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001295 if (!test_bit(DMF_FROZEN, &md->flags))
1296 return;
1297
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001298 thaw_bdev(md->suspended_bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001300 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301}
1302
1303/*
1304 * We need to be able to change a mapping table under a mounted
1305 * filesystem. For example we might want to move some data in
1306 * the background. Before the table can be swapped with
1307 * dm_bind_table, dm_suspend must be called to flush any in
1308 * flight bios and ensure that any further io gets deferred.
1309 */
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08001310int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001312 struct dm_table *map = NULL;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001313 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 DECLARE_WAITQUEUE(wait, current);
Jun'ichi Nomura1ecac7f2006-03-27 01:17:51 -08001315 struct bio *def;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001316 int r = -EINVAL;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08001317 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001318 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001320 down(&md->suspend_lock);
1321
1322 if (dm_suspended(md))
Alasdair G Kergond2874832006-11-08 17:44:43 -08001323 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 map = dm_get_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001327 /*
1328 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
1329 * This flag is cleared before dm_suspend returns.
1330 */
1331 if (noflush)
1332 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1333
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001334 /* This does not get reverted if there's an error later. */
1335 dm_table_presuspend_targets(map);
1336
Jun'ichi Nomurabfa152f2007-01-26 00:57:07 -08001337 /* bdget() can stall if the pending I/Os are not flushed */
1338 if (!noflush) {
1339 md->suspended_bdev = bdget_disk(md->disk, 0);
1340 if (!md->suspended_bdev) {
1341 DMWARN("bdget failed in dm_suspend");
1342 r = -ENOMEM;
1343 goto flush_and_out;
1344 }
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001345 }
1346
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001347 /*
1348 * Flush I/O to the device.
1349 * noflush supersedes do_lockfs, because lock_fs() needs to flush I/Os.
1350 */
1351 if (do_lockfs && !noflush) {
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08001352 r = lock_fs(md);
1353 if (r)
1354 goto out;
1355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 /*
Alasdair G Kergon354e0072005-05-05 16:16:05 -07001358 * First we set the BLOCK_IO flag so no more ios will be mapped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001360 down_write(&md->io_lock);
1361 set_bit(DMF_BLOCK_IO, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 add_wait_queue(&md->wait, &wait);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001364 up_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 /* unplug */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001367 if (map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 dm_table_unplug_all(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 /*
1371 * Then we wait for the already mapped ios to
1372 * complete.
1373 */
1374 while (1) {
1375 set_current_state(TASK_INTERRUPTIBLE);
1376
1377 if (!atomic_read(&md->pending) || signal_pending(current))
1378 break;
1379
1380 io_schedule();
1381 }
1382 set_current_state(TASK_RUNNING);
1383
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001384 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 remove_wait_queue(&md->wait, &wait);
1386
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001387 if (noflush) {
1388 spin_lock_irqsave(&md->pushback_lock, flags);
1389 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1390 bio_list_merge_head(&md->deferred, &md->pushback);
1391 bio_list_init(&md->pushback);
1392 spin_unlock_irqrestore(&md->pushback_lock, flags);
1393 }
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 /* were we interrupted ? */
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001396 r = -EINTR;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001397 if (atomic_read(&md->pending)) {
Jun'ichi Nomura1ecac7f2006-03-27 01:17:51 -08001398 clear_bit(DMF_BLOCK_IO, &md->flags);
1399 def = bio_list_get(&md->deferred);
1400 __flush_deferred_io(md, def);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001401 up_write(&md->io_lock);
1402 unlock_fs(md);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001403 goto out; /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001404 }
1405 up_write(&md->io_lock);
1406
1407 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 set_bit(DMF_SUSPENDED, &md->flags);
1410
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001411 r = 0;
1412
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001413flush_and_out:
1414 if (r && noflush) {
1415 /*
1416 * Because there may be already I/Os in the pushback list,
1417 * flush them before return.
1418 */
1419 down_write(&md->io_lock);
1420
1421 spin_lock_irqsave(&md->pushback_lock, flags);
1422 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
1423 bio_list_merge_head(&md->deferred, &md->pushback);
1424 bio_list_init(&md->pushback);
1425 spin_unlock_irqrestore(&md->pushback_lock, flags);
1426
1427 def = bio_list_get(&md->deferred);
1428 __flush_deferred_io(md, def);
1429 up_write(&md->io_lock);
1430 }
1431
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001432out:
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001433 if (r && md->suspended_bdev) {
1434 bdput(md->suspended_bdev);
1435 md->suspended_bdev = NULL;
1436 }
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 dm_table_put(map);
Alasdair G Kergond2874832006-11-08 17:44:43 -08001439
1440out_unlock:
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001441 up(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001442 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443}
1444
1445int dm_resume(struct mapped_device *md)
1446{
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001447 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 struct bio *def;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001449 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001451 down(&md->suspend_lock);
1452 if (!dm_suspended(md))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001453 goto out;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001454
1455 map = dm_get_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001456 if (!map || !dm_table_get_size(map))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001457 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Milan Broz8757b772006-10-03 01:15:36 -07001459 r = dm_table_resume_targets(map);
1460 if (r)
1461 goto out;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001462
1463 down_write(&md->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 clear_bit(DMF_BLOCK_IO, &md->flags);
1465
1466 def = bio_list_get(&md->deferred);
1467 __flush_deferred_io(md, def);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001468 up_write(&md->io_lock);
1469
1470 unlock_fs(md);
1471
Jun'ichi Nomurabfa152f2007-01-26 00:57:07 -08001472 if (md->suspended_bdev) {
1473 bdput(md->suspended_bdev);
1474 md->suspended_bdev = NULL;
1475 }
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08001476
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001477 clear_bit(DMF_SUSPENDED, &md->flags);
1478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 dm_table_unplug_all(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Hannes Reinecke8560ed62006-10-03 01:15:35 -07001481 kobject_uevent(&md->disk->kobj, KOBJ_CHANGE);
1482
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001483 r = 0;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001484
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001485out:
1486 dm_table_put(map);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07001487 up(&md->suspend_lock);
1488
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07001489 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
1491
1492/*-----------------------------------------------------------------
1493 * Event notification.
1494 *---------------------------------------------------------------*/
1495uint32_t dm_get_event_nr(struct mapped_device *md)
1496{
1497 return atomic_read(&md->event_nr);
1498}
1499
1500int dm_wait_event(struct mapped_device *md, int event_nr)
1501{
1502 return wait_event_interruptible(md->eventq,
1503 (event_nr != atomic_read(&md->event_nr)));
1504}
1505
1506/*
1507 * The gendisk is only valid as long as you have a reference
1508 * count on 'md'.
1509 */
1510struct gendisk *dm_disk(struct mapped_device *md)
1511{
1512 return md->disk;
1513}
1514
1515int dm_suspended(struct mapped_device *md)
1516{
1517 return test_bit(DMF_SUSPENDED, &md->flags);
1518}
1519
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001520int dm_noflush_suspending(struct dm_target *ti)
1521{
1522 struct mapped_device *md = dm_table_get_md(ti->table);
1523 int r = __noflush_suspending(md);
1524
1525 dm_put(md);
1526
1527 return r;
1528}
1529EXPORT_SYMBOL_GPL(dm_noflush_suspending);
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531static struct block_device_operations dm_blk_dops = {
1532 .open = dm_blk_open,
1533 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07001534 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001535 .getgeo = dm_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 .owner = THIS_MODULE
1537};
1538
1539EXPORT_SYMBOL(dm_get_mapinfo);
1540
1541/*
1542 * module hooks
1543 */
1544module_init(dm_init);
1545module_exit(dm_exit);
1546
1547module_param(major, uint, 0);
1548MODULE_PARM_DESC(major, "The major number of the device mapper");
1549MODULE_DESCRIPTION(DM_NAME " driver");
1550MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1551MODULE_LICENSE("GPL");