Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Copyright (C) 2001, 2002 Sistina Software (UK) Limited. | |
3 | * Copyright (C) 2004 Red Hat, Inc. All rights reserved. | |
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> |
1da177e4 LT |
23 | |
24 | static const char *_name = DM_NAME; | |
25 | ||
26 | static unsigned int major = 0; | |
27 | static unsigned int _major = 0; | |
28 | ||
f32c10b0 | 29 | static DEFINE_SPINLOCK(_minor_lock); |
1da177e4 LT |
30 | /* |
31 | * One of these is allocated per bio. | |
32 | */ | |
33 | struct dm_io { | |
34 | struct mapped_device *md; | |
35 | int error; | |
36 | struct bio *bio; | |
37 | atomic_t io_count; | |
3eaf840e | 38 | unsigned long start_time; |
1da177e4 LT |
39 | }; |
40 | ||
41 | /* | |
42 | * One of these is allocated per target within a bio. Hopefully | |
43 | * this will be simplified out one day. | |
44 | */ | |
45 | struct target_io { | |
46 | struct dm_io *io; | |
47 | struct dm_target *ti; | |
48 | union map_info info; | |
49 | }; | |
50 | ||
51 | union map_info *dm_get_mapinfo(struct bio *bio) | |
52 | { | |
53 | if (bio && bio->bi_private) | |
54 | return &((struct target_io *)bio->bi_private)->info; | |
55 | return NULL; | |
56 | } | |
57 | ||
ba61fdd1 JM |
58 | #define MINOR_ALLOCED ((void *)-1) |
59 | ||
1da177e4 LT |
60 | /* |
61 | * Bits for the md->flags field. | |
62 | */ | |
63 | #define DMF_BLOCK_IO 0 | |
64 | #define DMF_SUSPENDED 1 | |
aa8d7c2f | 65 | #define DMF_FROZEN 2 |
1da177e4 LT |
66 | |
67 | struct mapped_device { | |
2ca3310e AK |
68 | struct rw_semaphore io_lock; |
69 | struct semaphore suspend_lock; | |
1da177e4 LT |
70 | rwlock_t map_lock; |
71 | atomic_t holders; | |
72 | ||
73 | unsigned long flags; | |
74 | ||
75 | request_queue_t *queue; | |
76 | struct gendisk *disk; | |
7e51f257 | 77 | char name[16]; |
1da177e4 LT |
78 | |
79 | void *interface_ptr; | |
80 | ||
81 | /* | |
82 | * A list of ios that arrived while we were suspended. | |
83 | */ | |
84 | atomic_t pending; | |
85 | wait_queue_head_t wait; | |
86 | struct bio_list deferred; | |
87 | ||
88 | /* | |
89 | * The current mapping. | |
90 | */ | |
91 | struct dm_table *map; | |
92 | ||
93 | /* | |
94 | * io objects are allocated from here. | |
95 | */ | |
96 | mempool_t *io_pool; | |
97 | mempool_t *tio_pool; | |
98 | ||
99 | /* | |
100 | * Event handling. | |
101 | */ | |
102 | atomic_t event_nr; | |
103 | wait_queue_head_t eventq; | |
104 | ||
105 | /* | |
106 | * freeze/thaw support require holding onto a super block | |
107 | */ | |
108 | struct super_block *frozen_sb; | |
e39e2e95 | 109 | struct block_device *suspended_bdev; |
3ac51e74 DW |
110 | |
111 | /* forced geometry settings */ | |
112 | struct hd_geometry geometry; | |
1da177e4 LT |
113 | }; |
114 | ||
115 | #define MIN_IOS 256 | |
116 | static kmem_cache_t *_io_cache; | |
117 | static kmem_cache_t *_tio_cache; | |
118 | ||
119 | static struct bio_set *dm_set; | |
120 | ||
121 | static int __init local_init(void) | |
122 | { | |
123 | int r; | |
124 | ||
125 | dm_set = bioset_create(16, 16, 4); | |
126 | if (!dm_set) | |
127 | return -ENOMEM; | |
128 | ||
129 | /* allocate a slab for the dm_ios */ | |
130 | _io_cache = kmem_cache_create("dm_io", | |
131 | sizeof(struct dm_io), 0, 0, NULL, NULL); | |
132 | if (!_io_cache) | |
133 | return -ENOMEM; | |
134 | ||
135 | /* allocate a slab for the target ios */ | |
136 | _tio_cache = kmem_cache_create("dm_tio", sizeof(struct target_io), | |
137 | 0, 0, NULL, NULL); | |
138 | if (!_tio_cache) { | |
139 | kmem_cache_destroy(_io_cache); | |
140 | return -ENOMEM; | |
141 | } | |
142 | ||
143 | _major = major; | |
144 | r = register_blkdev(_major, _name); | |
145 | if (r < 0) { | |
146 | kmem_cache_destroy(_tio_cache); | |
147 | kmem_cache_destroy(_io_cache); | |
148 | return r; | |
149 | } | |
150 | ||
151 | if (!_major) | |
152 | _major = r; | |
153 | ||
154 | return 0; | |
155 | } | |
156 | ||
157 | static void local_exit(void) | |
158 | { | |
159 | kmem_cache_destroy(_tio_cache); | |
160 | kmem_cache_destroy(_io_cache); | |
161 | ||
162 | bioset_free(dm_set); | |
163 | ||
164 | if (unregister_blkdev(_major, _name) < 0) | |
165 | DMERR("devfs_unregister_blkdev failed"); | |
166 | ||
167 | _major = 0; | |
168 | ||
169 | DMINFO("cleaned up"); | |
170 | } | |
171 | ||
172 | int (*_inits[])(void) __initdata = { | |
173 | local_init, | |
174 | dm_target_init, | |
175 | dm_linear_init, | |
176 | dm_stripe_init, | |
177 | dm_interface_init, | |
178 | }; | |
179 | ||
180 | void (*_exits[])(void) = { | |
181 | local_exit, | |
182 | dm_target_exit, | |
183 | dm_linear_exit, | |
184 | dm_stripe_exit, | |
185 | dm_interface_exit, | |
186 | }; | |
187 | ||
188 | static 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 | ||
209 | static 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 | */ | |
220 | static int dm_blk_open(struct inode *inode, struct file *file) | |
221 | { | |
222 | struct mapped_device *md; | |
223 | ||
224 | md = inode->i_bdev->bd_disk->private_data; | |
225 | dm_get(md); | |
226 | return 0; | |
227 | } | |
228 | ||
229 | static int dm_blk_close(struct inode *inode, struct file *file) | |
230 | { | |
231 | struct mapped_device *md; | |
232 | ||
233 | md = inode->i_bdev->bd_disk->private_data; | |
234 | dm_put(md); | |
235 | return 0; | |
236 | } | |
237 | ||
3ac51e74 DW |
238 | static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
239 | { | |
240 | struct mapped_device *md = bdev->bd_disk->private_data; | |
241 | ||
242 | return dm_get_geometry(md, geo); | |
243 | } | |
244 | ||
1da177e4 LT |
245 | static inline struct dm_io *alloc_io(struct mapped_device *md) |
246 | { | |
247 | return mempool_alloc(md->io_pool, GFP_NOIO); | |
248 | } | |
249 | ||
250 | static inline void free_io(struct mapped_device *md, struct dm_io *io) | |
251 | { | |
252 | mempool_free(io, md->io_pool); | |
253 | } | |
254 | ||
255 | static inline struct target_io *alloc_tio(struct mapped_device *md) | |
256 | { | |
257 | return mempool_alloc(md->tio_pool, GFP_NOIO); | |
258 | } | |
259 | ||
260 | static inline void free_tio(struct mapped_device *md, struct target_io *tio) | |
261 | { | |
262 | mempool_free(tio, md->tio_pool); | |
263 | } | |
264 | ||
3eaf840e JNN |
265 | static void start_io_acct(struct dm_io *io) |
266 | { | |
267 | struct mapped_device *md = io->md; | |
268 | ||
269 | io->start_time = jiffies; | |
270 | ||
271 | preempt_disable(); | |
272 | disk_round_stats(dm_disk(md)); | |
273 | preempt_enable(); | |
274 | dm_disk(md)->in_flight = atomic_inc_return(&md->pending); | |
275 | } | |
276 | ||
277 | static int end_io_acct(struct dm_io *io) | |
278 | { | |
279 | struct mapped_device *md = io->md; | |
280 | struct bio *bio = io->bio; | |
281 | unsigned long duration = jiffies - io->start_time; | |
282 | int pending; | |
283 | int rw = bio_data_dir(bio); | |
284 | ||
285 | preempt_disable(); | |
286 | disk_round_stats(dm_disk(md)); | |
287 | preempt_enable(); | |
288 | dm_disk(md)->in_flight = pending = atomic_dec_return(&md->pending); | |
289 | ||
290 | disk_stat_add(dm_disk(md), ticks[rw], duration); | |
291 | ||
292 | return !pending; | |
293 | } | |
294 | ||
1da177e4 LT |
295 | /* |
296 | * Add the bio to the list of deferred io. | |
297 | */ | |
298 | static int queue_io(struct mapped_device *md, struct bio *bio) | |
299 | { | |
2ca3310e | 300 | down_write(&md->io_lock); |
1da177e4 LT |
301 | |
302 | if (!test_bit(DMF_BLOCK_IO, &md->flags)) { | |
2ca3310e | 303 | up_write(&md->io_lock); |
1da177e4 LT |
304 | return 1; |
305 | } | |
306 | ||
307 | bio_list_add(&md->deferred, bio); | |
308 | ||
2ca3310e | 309 | up_write(&md->io_lock); |
1da177e4 LT |
310 | return 0; /* deferred successfully */ |
311 | } | |
312 | ||
313 | /* | |
314 | * Everyone (including functions in this file), should use this | |
315 | * function to access the md->map field, and make sure they call | |
316 | * dm_table_put() when finished. | |
317 | */ | |
318 | struct dm_table *dm_get_table(struct mapped_device *md) | |
319 | { | |
320 | struct dm_table *t; | |
321 | ||
322 | read_lock(&md->map_lock); | |
323 | t = md->map; | |
324 | if (t) | |
325 | dm_table_get(t); | |
326 | read_unlock(&md->map_lock); | |
327 | ||
328 | return t; | |
329 | } | |
330 | ||
3ac51e74 DW |
331 | /* |
332 | * Get the geometry associated with a dm device | |
333 | */ | |
334 | int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo) | |
335 | { | |
336 | *geo = md->geometry; | |
337 | ||
338 | return 0; | |
339 | } | |
340 | ||
341 | /* | |
342 | * Set the geometry of a device. | |
343 | */ | |
344 | int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo) | |
345 | { | |
346 | sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors; | |
347 | ||
348 | if (geo->start > sz) { | |
349 | DMWARN("Start sector is beyond the geometry limits."); | |
350 | return -EINVAL; | |
351 | } | |
352 | ||
353 | md->geometry = *geo; | |
354 | ||
355 | return 0; | |
356 | } | |
357 | ||
1da177e4 LT |
358 | /*----------------------------------------------------------------- |
359 | * CRUD START: | |
360 | * A more elegant soln is in the works that uses the queue | |
361 | * merge fn, unfortunately there are a couple of changes to | |
362 | * the block layer that I want to make for this. So in the | |
363 | * interests of getting something for people to use I give | |
364 | * you this clearly demarcated crap. | |
365 | *---------------------------------------------------------------*/ | |
366 | ||
367 | /* | |
368 | * Decrements the number of outstanding ios that a bio has been | |
369 | * cloned into, completing the original io if necc. | |
370 | */ | |
858119e1 | 371 | static void dec_pending(struct dm_io *io, int error) |
1da177e4 LT |
372 | { |
373 | if (error) | |
374 | io->error = error; | |
375 | ||
376 | if (atomic_dec_and_test(&io->io_count)) { | |
3eaf840e | 377 | if (end_io_acct(io)) |
1da177e4 LT |
378 | /* nudge anyone waiting on suspend queue */ |
379 | wake_up(&io->md->wait); | |
380 | ||
2056a782 JA |
381 | blk_add_trace_bio(io->md->queue, io->bio, BLK_TA_COMPLETE); |
382 | ||
1da177e4 LT |
383 | bio_endio(io->bio, io->bio->bi_size, io->error); |
384 | free_io(io->md, io); | |
385 | } | |
386 | } | |
387 | ||
388 | static int clone_endio(struct bio *bio, unsigned int done, int error) | |
389 | { | |
390 | int r = 0; | |
391 | struct target_io *tio = bio->bi_private; | |
392 | struct dm_io *io = tio->io; | |
393 | dm_endio_fn endio = tio->ti->type->end_io; | |
394 | ||
395 | if (bio->bi_size) | |
396 | return 1; | |
397 | ||
398 | if (!bio_flagged(bio, BIO_UPTODATE) && !error) | |
399 | error = -EIO; | |
400 | ||
401 | if (endio) { | |
402 | r = endio(tio->ti, bio, error, &tio->info); | |
403 | if (r < 0) | |
404 | error = r; | |
405 | ||
406 | else if (r > 0) | |
407 | /* the target wants another shot at the io */ | |
408 | return 1; | |
409 | } | |
410 | ||
411 | free_tio(io->md, tio); | |
412 | dec_pending(io, error); | |
413 | bio_put(bio); | |
414 | return r; | |
415 | } | |
416 | ||
417 | static sector_t max_io_len(struct mapped_device *md, | |
418 | sector_t sector, struct dm_target *ti) | |
419 | { | |
420 | sector_t offset = sector - ti->begin; | |
421 | sector_t len = ti->len - offset; | |
422 | ||
423 | /* | |
424 | * Does the target need to split even further ? | |
425 | */ | |
426 | if (ti->split_io) { | |
427 | sector_t boundary; | |
428 | boundary = ((offset + ti->split_io) & ~(ti->split_io - 1)) | |
429 | - offset; | |
430 | if (len > boundary) | |
431 | len = boundary; | |
432 | } | |
433 | ||
434 | return len; | |
435 | } | |
436 | ||
437 | static void __map_bio(struct dm_target *ti, struct bio *clone, | |
438 | struct target_io *tio) | |
439 | { | |
440 | int r; | |
2056a782 | 441 | sector_t sector; |
1da177e4 LT |
442 | |
443 | /* | |
444 | * Sanity checks. | |
445 | */ | |
446 | BUG_ON(!clone->bi_size); | |
447 | ||
448 | clone->bi_end_io = clone_endio; | |
449 | clone->bi_private = tio; | |
450 | ||
451 | /* | |
452 | * Map the clone. If r == 0 we don't need to do | |
453 | * anything, the target has assumed ownership of | |
454 | * this io. | |
455 | */ | |
456 | atomic_inc(&tio->io->io_count); | |
2056a782 | 457 | sector = clone->bi_sector; |
1da177e4 | 458 | r = ti->type->map(ti, clone, &tio->info); |
2056a782 | 459 | if (r > 0) { |
1da177e4 | 460 | /* the bio has been remapped so dispatch it */ |
2056a782 JA |
461 | |
462 | blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone, | |
463 | tio->io->bio->bi_bdev->bd_dev, sector, | |
464 | clone->bi_sector); | |
465 | ||
1da177e4 | 466 | generic_make_request(clone); |
2056a782 | 467 | } |
1da177e4 LT |
468 | |
469 | else if (r < 0) { | |
470 | /* error the io and bail out */ | |
471 | struct dm_io *io = tio->io; | |
472 | free_tio(tio->io->md, tio); | |
f6a80ea8 | 473 | dec_pending(io, r); |
1da177e4 LT |
474 | bio_put(clone); |
475 | } | |
476 | } | |
477 | ||
478 | struct clone_info { | |
479 | struct mapped_device *md; | |
480 | struct dm_table *map; | |
481 | struct bio *bio; | |
482 | struct dm_io *io; | |
483 | sector_t sector; | |
484 | sector_t sector_count; | |
485 | unsigned short idx; | |
486 | }; | |
487 | ||
3676347a PO |
488 | static void dm_bio_destructor(struct bio *bio) |
489 | { | |
490 | bio_free(bio, dm_set); | |
491 | } | |
492 | ||
1da177e4 LT |
493 | /* |
494 | * Creates a little bio that is just does part of a bvec. | |
495 | */ | |
496 | static struct bio *split_bvec(struct bio *bio, sector_t sector, | |
497 | unsigned short idx, unsigned int offset, | |
498 | unsigned int len) | |
499 | { | |
500 | struct bio *clone; | |
501 | struct bio_vec *bv = bio->bi_io_vec + idx; | |
502 | ||
503 | clone = bio_alloc_bioset(GFP_NOIO, 1, dm_set); | |
3676347a | 504 | clone->bi_destructor = dm_bio_destructor; |
1da177e4 LT |
505 | *clone->bi_io_vec = *bv; |
506 | ||
507 | clone->bi_sector = sector; | |
508 | clone->bi_bdev = bio->bi_bdev; | |
509 | clone->bi_rw = bio->bi_rw; | |
510 | clone->bi_vcnt = 1; | |
511 | clone->bi_size = to_bytes(len); | |
512 | clone->bi_io_vec->bv_offset = offset; | |
513 | clone->bi_io_vec->bv_len = clone->bi_size; | |
514 | ||
515 | return clone; | |
516 | } | |
517 | ||
518 | /* | |
519 | * Creates a bio that consists of range of complete bvecs. | |
520 | */ | |
521 | static struct bio *clone_bio(struct bio *bio, sector_t sector, | |
522 | unsigned short idx, unsigned short bv_count, | |
523 | unsigned int len) | |
524 | { | |
525 | struct bio *clone; | |
526 | ||
527 | clone = bio_clone(bio, GFP_NOIO); | |
528 | clone->bi_sector = sector; | |
529 | clone->bi_idx = idx; | |
530 | clone->bi_vcnt = idx + bv_count; | |
531 | clone->bi_size = to_bytes(len); | |
532 | clone->bi_flags &= ~(1 << BIO_SEG_VALID); | |
533 | ||
534 | return clone; | |
535 | } | |
536 | ||
537 | static void __clone_and_map(struct clone_info *ci) | |
538 | { | |
539 | struct bio *clone, *bio = ci->bio; | |
540 | struct dm_target *ti = dm_table_find_target(ci->map, ci->sector); | |
541 | sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti); | |
542 | struct target_io *tio; | |
543 | ||
544 | /* | |
545 | * Allocate a target io object. | |
546 | */ | |
547 | tio = alloc_tio(ci->md); | |
548 | tio->io = ci->io; | |
549 | tio->ti = ti; | |
550 | memset(&tio->info, 0, sizeof(tio->info)); | |
551 | ||
552 | if (ci->sector_count <= max) { | |
553 | /* | |
554 | * Optimise for the simple case where we can do all of | |
555 | * the remaining io with a single clone. | |
556 | */ | |
557 | clone = clone_bio(bio, ci->sector, ci->idx, | |
558 | bio->bi_vcnt - ci->idx, ci->sector_count); | |
559 | __map_bio(ti, clone, tio); | |
560 | ci->sector_count = 0; | |
561 | ||
562 | } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) { | |
563 | /* | |
564 | * There are some bvecs that don't span targets. | |
565 | * Do as many of these as possible. | |
566 | */ | |
567 | int i; | |
568 | sector_t remaining = max; | |
569 | sector_t bv_len; | |
570 | ||
571 | for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) { | |
572 | bv_len = to_sector(bio->bi_io_vec[i].bv_len); | |
573 | ||
574 | if (bv_len > remaining) | |
575 | break; | |
576 | ||
577 | remaining -= bv_len; | |
578 | len += bv_len; | |
579 | } | |
580 | ||
581 | clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len); | |
582 | __map_bio(ti, clone, tio); | |
583 | ||
584 | ci->sector += len; | |
585 | ci->sector_count -= len; | |
586 | ci->idx = i; | |
587 | ||
588 | } else { | |
589 | /* | |
d2044a94 | 590 | * Handle a bvec that must be split between two or more targets. |
1da177e4 LT |
591 | */ |
592 | struct bio_vec *bv = bio->bi_io_vec + ci->idx; | |
d2044a94 AK |
593 | sector_t remaining = to_sector(bv->bv_len); |
594 | unsigned int offset = 0; | |
1da177e4 | 595 | |
d2044a94 AK |
596 | do { |
597 | if (offset) { | |
598 | ti = dm_table_find_target(ci->map, ci->sector); | |
599 | max = max_io_len(ci->md, ci->sector, ti); | |
1da177e4 | 600 | |
d2044a94 AK |
601 | tio = alloc_tio(ci->md); |
602 | tio->io = ci->io; | |
603 | tio->ti = ti; | |
604 | memset(&tio->info, 0, sizeof(tio->info)); | |
605 | } | |
606 | ||
607 | len = min(remaining, max); | |
608 | ||
609 | clone = split_bvec(bio, ci->sector, ci->idx, | |
610 | bv->bv_offset + offset, len); | |
611 | ||
612 | __map_bio(ti, clone, tio); | |
613 | ||
614 | ci->sector += len; | |
615 | ci->sector_count -= len; | |
616 | offset += to_bytes(len); | |
617 | } while (remaining -= len); | |
1da177e4 | 618 | |
1da177e4 LT |
619 | ci->idx++; |
620 | } | |
621 | } | |
622 | ||
623 | /* | |
624 | * Split the bio into several clones. | |
625 | */ | |
626 | static void __split_bio(struct mapped_device *md, struct bio *bio) | |
627 | { | |
628 | struct clone_info ci; | |
629 | ||
630 | ci.map = dm_get_table(md); | |
631 | if (!ci.map) { | |
632 | bio_io_error(bio, bio->bi_size); | |
633 | return; | |
634 | } | |
635 | ||
636 | ci.md = md; | |
637 | ci.bio = bio; | |
638 | ci.io = alloc_io(md); | |
639 | ci.io->error = 0; | |
640 | atomic_set(&ci.io->io_count, 1); | |
641 | ci.io->bio = bio; | |
642 | ci.io->md = md; | |
643 | ci.sector = bio->bi_sector; | |
644 | ci.sector_count = bio_sectors(bio); | |
645 | ci.idx = bio->bi_idx; | |
646 | ||
3eaf840e | 647 | start_io_acct(ci.io); |
1da177e4 LT |
648 | while (ci.sector_count) |
649 | __clone_and_map(&ci); | |
650 | ||
651 | /* drop the extra reference count */ | |
652 | dec_pending(ci.io, 0); | |
653 | dm_table_put(ci.map); | |
654 | } | |
655 | /*----------------------------------------------------------------- | |
656 | * CRUD END | |
657 | *---------------------------------------------------------------*/ | |
658 | ||
659 | /* | |
660 | * The request function that just remaps the bio built up by | |
661 | * dm_merge_bvec. | |
662 | */ | |
663 | static int dm_request(request_queue_t *q, struct bio *bio) | |
664 | { | |
665 | int r; | |
12f03a49 | 666 | int rw = bio_data_dir(bio); |
1da177e4 LT |
667 | struct mapped_device *md = q->queuedata; |
668 | ||
2ca3310e | 669 | down_read(&md->io_lock); |
1da177e4 | 670 | |
12f03a49 KC |
671 | disk_stat_inc(dm_disk(md), ios[rw]); |
672 | disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio)); | |
673 | ||
1da177e4 LT |
674 | /* |
675 | * If we're suspended we have to queue | |
676 | * this io for later. | |
677 | */ | |
678 | while (test_bit(DMF_BLOCK_IO, &md->flags)) { | |
2ca3310e | 679 | up_read(&md->io_lock); |
1da177e4 LT |
680 | |
681 | if (bio_rw(bio) == READA) { | |
682 | bio_io_error(bio, bio->bi_size); | |
683 | return 0; | |
684 | } | |
685 | ||
686 | r = queue_io(md, bio); | |
687 | if (r < 0) { | |
688 | bio_io_error(bio, bio->bi_size); | |
689 | return 0; | |
690 | ||
691 | } else if (r == 0) | |
692 | return 0; /* deferred successfully */ | |
693 | ||
694 | /* | |
695 | * We're in a while loop, because someone could suspend | |
696 | * before we get to the following read lock. | |
697 | */ | |
2ca3310e | 698 | down_read(&md->io_lock); |
1da177e4 LT |
699 | } |
700 | ||
701 | __split_bio(md, bio); | |
2ca3310e | 702 | up_read(&md->io_lock); |
1da177e4 LT |
703 | return 0; |
704 | } | |
705 | ||
706 | static int dm_flush_all(request_queue_t *q, struct gendisk *disk, | |
707 | sector_t *error_sector) | |
708 | { | |
709 | struct mapped_device *md = q->queuedata; | |
710 | struct dm_table *map = dm_get_table(md); | |
711 | int ret = -ENXIO; | |
712 | ||
713 | if (map) { | |
cf222b37 | 714 | ret = dm_table_flush_all(map); |
1da177e4 LT |
715 | dm_table_put(map); |
716 | } | |
717 | ||
718 | return ret; | |
719 | } | |
720 | ||
721 | static void dm_unplug_all(request_queue_t *q) | |
722 | { | |
723 | struct mapped_device *md = q->queuedata; | |
724 | struct dm_table *map = dm_get_table(md); | |
725 | ||
726 | if (map) { | |
727 | dm_table_unplug_all(map); | |
728 | dm_table_put(map); | |
729 | } | |
730 | } | |
731 | ||
732 | static int dm_any_congested(void *congested_data, int bdi_bits) | |
733 | { | |
734 | int r; | |
735 | struct mapped_device *md = (struct mapped_device *) congested_data; | |
736 | struct dm_table *map = dm_get_table(md); | |
737 | ||
738 | if (!map || test_bit(DMF_BLOCK_IO, &md->flags)) | |
739 | r = bdi_bits; | |
740 | else | |
741 | r = dm_table_any_congested(map, bdi_bits); | |
742 | ||
743 | dm_table_put(map); | |
744 | return r; | |
745 | } | |
746 | ||
747 | /*----------------------------------------------------------------- | |
748 | * An IDR is used to keep track of allocated minor numbers. | |
749 | *---------------------------------------------------------------*/ | |
1da177e4 LT |
750 | static DEFINE_IDR(_minor_idr); |
751 | ||
752 | static void free_minor(unsigned int minor) | |
753 | { | |
f32c10b0 | 754 | spin_lock(&_minor_lock); |
1da177e4 | 755 | idr_remove(&_minor_idr, minor); |
f32c10b0 | 756 | spin_unlock(&_minor_lock); |
1da177e4 LT |
757 | } |
758 | ||
759 | /* | |
760 | * See if the device with a specific minor # is free. | |
761 | */ | |
762 | static int specific_minor(struct mapped_device *md, unsigned int minor) | |
763 | { | |
764 | int r, m; | |
765 | ||
766 | if (minor >= (1 << MINORBITS)) | |
767 | return -EINVAL; | |
768 | ||
62f75c2f JM |
769 | r = idr_pre_get(&_minor_idr, GFP_KERNEL); |
770 | if (!r) | |
771 | return -ENOMEM; | |
772 | ||
f32c10b0 | 773 | spin_lock(&_minor_lock); |
1da177e4 LT |
774 | |
775 | if (idr_find(&_minor_idr, minor)) { | |
776 | r = -EBUSY; | |
777 | goto out; | |
778 | } | |
779 | ||
ba61fdd1 | 780 | r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m); |
62f75c2f | 781 | if (r) |
1da177e4 | 782 | goto out; |
1da177e4 LT |
783 | |
784 | if (m != minor) { | |
785 | idr_remove(&_minor_idr, m); | |
786 | r = -EBUSY; | |
787 | goto out; | |
788 | } | |
789 | ||
790 | out: | |
f32c10b0 | 791 | spin_unlock(&_minor_lock); |
1da177e4 LT |
792 | return r; |
793 | } | |
794 | ||
795 | static int next_free_minor(struct mapped_device *md, unsigned int *minor) | |
796 | { | |
797 | int r; | |
798 | unsigned int m; | |
799 | ||
1da177e4 | 800 | r = idr_pre_get(&_minor_idr, GFP_KERNEL); |
62f75c2f JM |
801 | if (!r) |
802 | return -ENOMEM; | |
803 | ||
f32c10b0 | 804 | spin_lock(&_minor_lock); |
1da177e4 | 805 | |
ba61fdd1 | 806 | r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m); |
1da177e4 LT |
807 | if (r) { |
808 | goto out; | |
809 | } | |
810 | ||
811 | if (m >= (1 << MINORBITS)) { | |
812 | idr_remove(&_minor_idr, m); | |
813 | r = -ENOSPC; | |
814 | goto out; | |
815 | } | |
816 | ||
817 | *minor = m; | |
818 | ||
819 | out: | |
f32c10b0 | 820 | spin_unlock(&_minor_lock); |
1da177e4 LT |
821 | return r; |
822 | } | |
823 | ||
824 | static struct block_device_operations dm_blk_dops; | |
825 | ||
826 | /* | |
827 | * Allocate and initialise a blank device with a given minor. | |
828 | */ | |
829 | static struct mapped_device *alloc_dev(unsigned int minor, int persistent) | |
830 | { | |
831 | int r; | |
832 | struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL); | |
ba61fdd1 | 833 | void *old_md; |
1da177e4 LT |
834 | |
835 | if (!md) { | |
836 | DMWARN("unable to allocate device, out of memory."); | |
837 | return NULL; | |
838 | } | |
839 | ||
840 | /* get a minor number for the dev */ | |
841 | r = persistent ? specific_minor(md, minor) : next_free_minor(md, &minor); | |
842 | if (r < 0) | |
843 | goto bad1; | |
844 | ||
845 | memset(md, 0, sizeof(*md)); | |
2ca3310e AK |
846 | init_rwsem(&md->io_lock); |
847 | init_MUTEX(&md->suspend_lock); | |
1da177e4 LT |
848 | rwlock_init(&md->map_lock); |
849 | atomic_set(&md->holders, 1); | |
850 | atomic_set(&md->event_nr, 0); | |
851 | ||
852 | md->queue = blk_alloc_queue(GFP_KERNEL); | |
853 | if (!md->queue) | |
854 | goto bad1; | |
855 | ||
856 | md->queue->queuedata = md; | |
857 | md->queue->backing_dev_info.congested_fn = dm_any_congested; | |
858 | md->queue->backing_dev_info.congested_data = md; | |
859 | blk_queue_make_request(md->queue, dm_request); | |
daef265f | 860 | blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY); |
1da177e4 LT |
861 | md->queue->unplug_fn = dm_unplug_all; |
862 | md->queue->issue_flush_fn = dm_flush_all; | |
863 | ||
93d2341c | 864 | md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache); |
1da177e4 LT |
865 | if (!md->io_pool) |
866 | goto bad2; | |
867 | ||
93d2341c | 868 | md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache); |
1da177e4 LT |
869 | if (!md->tio_pool) |
870 | goto bad3; | |
871 | ||
872 | md->disk = alloc_disk(1); | |
873 | if (!md->disk) | |
874 | goto bad4; | |
875 | ||
876 | md->disk->major = _major; | |
877 | md->disk->first_minor = minor; | |
878 | md->disk->fops = &dm_blk_dops; | |
879 | md->disk->queue = md->queue; | |
880 | md->disk->private_data = md; | |
881 | sprintf(md->disk->disk_name, "dm-%d", minor); | |
882 | add_disk(md->disk); | |
7e51f257 | 883 | format_dev_t(md->name, MKDEV(_major, minor)); |
1da177e4 LT |
884 | |
885 | atomic_set(&md->pending, 0); | |
886 | init_waitqueue_head(&md->wait); | |
887 | init_waitqueue_head(&md->eventq); | |
888 | ||
ba61fdd1 | 889 | /* Populate the mapping, nobody knows we exist yet */ |
f32c10b0 | 890 | spin_lock(&_minor_lock); |
ba61fdd1 | 891 | old_md = idr_replace(&_minor_idr, md, minor); |
f32c10b0 | 892 | spin_unlock(&_minor_lock); |
ba61fdd1 JM |
893 | |
894 | BUG_ON(old_md != MINOR_ALLOCED); | |
895 | ||
1da177e4 LT |
896 | return md; |
897 | ||
898 | bad4: | |
899 | mempool_destroy(md->tio_pool); | |
900 | bad3: | |
901 | mempool_destroy(md->io_pool); | |
902 | bad2: | |
1312f40e | 903 | blk_cleanup_queue(md->queue); |
1da177e4 LT |
904 | free_minor(minor); |
905 | bad1: | |
906 | kfree(md); | |
907 | return NULL; | |
908 | } | |
909 | ||
910 | static void free_dev(struct mapped_device *md) | |
911 | { | |
63d94e48 JN |
912 | unsigned int minor = md->disk->first_minor; |
913 | ||
d9dde59b JN |
914 | if (md->suspended_bdev) { |
915 | thaw_bdev(md->suspended_bdev, NULL); | |
916 | bdput(md->suspended_bdev); | |
917 | } | |
1da177e4 LT |
918 | mempool_destroy(md->tio_pool); |
919 | mempool_destroy(md->io_pool); | |
920 | del_gendisk(md->disk); | |
63d94e48 | 921 | free_minor(minor); |
1da177e4 | 922 | put_disk(md->disk); |
1312f40e | 923 | blk_cleanup_queue(md->queue); |
1da177e4 LT |
924 | kfree(md); |
925 | } | |
926 | ||
927 | /* | |
928 | * Bind a table to the device. | |
929 | */ | |
930 | static void event_callback(void *context) | |
931 | { | |
932 | struct mapped_device *md = (struct mapped_device *) context; | |
933 | ||
934 | atomic_inc(&md->event_nr); | |
935 | wake_up(&md->eventq); | |
936 | } | |
937 | ||
4e90188b | 938 | static void __set_size(struct mapped_device *md, sector_t size) |
1da177e4 | 939 | { |
4e90188b | 940 | set_capacity(md->disk, size); |
1da177e4 | 941 | |
1b1dcc1b | 942 | mutex_lock(&md->suspended_bdev->bd_inode->i_mutex); |
e39e2e95 | 943 | i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT); |
1b1dcc1b | 944 | mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex); |
1da177e4 LT |
945 | } |
946 | ||
947 | static int __bind(struct mapped_device *md, struct dm_table *t) | |
948 | { | |
949 | request_queue_t *q = md->queue; | |
950 | sector_t size; | |
951 | ||
952 | size = dm_table_get_size(t); | |
3ac51e74 DW |
953 | |
954 | /* | |
955 | * Wipe any geometry if the size of the table changed. | |
956 | */ | |
957 | if (size != get_capacity(md->disk)) | |
958 | memset(&md->geometry, 0, sizeof(md->geometry)); | |
959 | ||
4e90188b | 960 | __set_size(md, size); |
1da177e4 LT |
961 | if (size == 0) |
962 | return 0; | |
963 | ||
2ca3310e AK |
964 | dm_table_get(t); |
965 | dm_table_event_callback(t, event_callback, md); | |
966 | ||
1da177e4 LT |
967 | write_lock(&md->map_lock); |
968 | md->map = t; | |
2ca3310e | 969 | dm_table_set_restrictions(t, q); |
1da177e4 LT |
970 | write_unlock(&md->map_lock); |
971 | ||
1da177e4 LT |
972 | return 0; |
973 | } | |
974 | ||
975 | static void __unbind(struct mapped_device *md) | |
976 | { | |
977 | struct dm_table *map = md->map; | |
978 | ||
979 | if (!map) | |
980 | return; | |
981 | ||
982 | dm_table_event_callback(map, NULL, NULL); | |
983 | write_lock(&md->map_lock); | |
984 | md->map = NULL; | |
985 | write_unlock(&md->map_lock); | |
986 | dm_table_put(map); | |
987 | } | |
988 | ||
989 | /* | |
990 | * Constructor for a new device. | |
991 | */ | |
992 | static int create_aux(unsigned int minor, int persistent, | |
993 | struct mapped_device **result) | |
994 | { | |
995 | struct mapped_device *md; | |
996 | ||
997 | md = alloc_dev(minor, persistent); | |
998 | if (!md) | |
999 | return -ENXIO; | |
1000 | ||
1001 | *result = md; | |
1002 | return 0; | |
1003 | } | |
1004 | ||
1005 | int dm_create(struct mapped_device **result) | |
1006 | { | |
1007 | return create_aux(0, 0, result); | |
1008 | } | |
1009 | ||
1010 | int dm_create_with_minor(unsigned int minor, struct mapped_device **result) | |
1011 | { | |
1012 | return create_aux(minor, 1, result); | |
1013 | } | |
1014 | ||
637842cf | 1015 | static struct mapped_device *dm_find_md(dev_t dev) |
1da177e4 LT |
1016 | { |
1017 | struct mapped_device *md; | |
1da177e4 LT |
1018 | unsigned minor = MINOR(dev); |
1019 | ||
1020 | if (MAJOR(dev) != _major || minor >= (1 << MINORBITS)) | |
1021 | return NULL; | |
1022 | ||
f32c10b0 | 1023 | spin_lock(&_minor_lock); |
1da177e4 LT |
1024 | |
1025 | md = idr_find(&_minor_idr, minor); | |
ba61fdd1 | 1026 | if (md && (md == MINOR_ALLOCED || (dm_disk(md)->first_minor != minor))) |
637842cf | 1027 | md = NULL; |
1da177e4 | 1028 | |
f32c10b0 | 1029 | spin_unlock(&_minor_lock); |
1da177e4 | 1030 | |
637842cf DT |
1031 | return md; |
1032 | } | |
1033 | ||
d229a958 DT |
1034 | struct mapped_device *dm_get_md(dev_t dev) |
1035 | { | |
1036 | struct mapped_device *md = dm_find_md(dev); | |
1037 | ||
1038 | if (md) | |
1039 | dm_get(md); | |
1040 | ||
1041 | return md; | |
1042 | } | |
1043 | ||
9ade92a9 | 1044 | void *dm_get_mdptr(struct mapped_device *md) |
637842cf | 1045 | { |
9ade92a9 | 1046 | return md->interface_ptr; |
1da177e4 LT |
1047 | } |
1048 | ||
1049 | void dm_set_mdptr(struct mapped_device *md, void *ptr) | |
1050 | { | |
1051 | md->interface_ptr = ptr; | |
1052 | } | |
1053 | ||
1054 | void dm_get(struct mapped_device *md) | |
1055 | { | |
1056 | atomic_inc(&md->holders); | |
1057 | } | |
1058 | ||
1059 | void dm_put(struct mapped_device *md) | |
1060 | { | |
1134e5ae | 1061 | struct dm_table *map; |
1da177e4 | 1062 | |
f32c10b0 | 1063 | if (atomic_dec_and_lock(&md->holders, &_minor_lock)) { |
1134e5ae | 1064 | map = dm_get_table(md); |
ba61fdd1 | 1065 | idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor); |
f32c10b0 | 1066 | spin_unlock(&_minor_lock); |
cf222b37 | 1067 | if (!dm_suspended(md)) { |
1da177e4 LT |
1068 | dm_table_presuspend_targets(map); |
1069 | dm_table_postsuspend_targets(map); | |
1070 | } | |
1071 | __unbind(md); | |
1134e5ae | 1072 | dm_table_put(map); |
1da177e4 LT |
1073 | free_dev(md); |
1074 | } | |
1da177e4 LT |
1075 | } |
1076 | ||
1077 | /* | |
1078 | * Process the deferred bios | |
1079 | */ | |
1080 | static void __flush_deferred_io(struct mapped_device *md, struct bio *c) | |
1081 | { | |
1082 | struct bio *n; | |
1083 | ||
1084 | while (c) { | |
1085 | n = c->bi_next; | |
1086 | c->bi_next = NULL; | |
1087 | __split_bio(md, c); | |
1088 | c = n; | |
1089 | } | |
1090 | } | |
1091 | ||
1092 | /* | |
1093 | * Swap in a new table (destroying old one). | |
1094 | */ | |
1095 | int dm_swap_table(struct mapped_device *md, struct dm_table *table) | |
1096 | { | |
93c534ae | 1097 | int r = -EINVAL; |
1da177e4 | 1098 | |
2ca3310e | 1099 | down(&md->suspend_lock); |
1da177e4 LT |
1100 | |
1101 | /* device must be suspended */ | |
cf222b37 | 1102 | if (!dm_suspended(md)) |
93c534ae | 1103 | goto out; |
1da177e4 LT |
1104 | |
1105 | __unbind(md); | |
1106 | r = __bind(md, table); | |
1da177e4 | 1107 | |
93c534ae | 1108 | out: |
2ca3310e | 1109 | up(&md->suspend_lock); |
93c534ae | 1110 | return r; |
1da177e4 LT |
1111 | } |
1112 | ||
1113 | /* | |
1114 | * Functions to lock and unlock any filesystem running on the | |
1115 | * device. | |
1116 | */ | |
2ca3310e | 1117 | static int lock_fs(struct mapped_device *md) |
1da177e4 | 1118 | { |
e39e2e95 | 1119 | int r; |
1da177e4 LT |
1120 | |
1121 | WARN_ON(md->frozen_sb); | |
dfbe03f6 | 1122 | |
e39e2e95 | 1123 | md->frozen_sb = freeze_bdev(md->suspended_bdev); |
dfbe03f6 | 1124 | if (IS_ERR(md->frozen_sb)) { |
cf222b37 | 1125 | r = PTR_ERR(md->frozen_sb); |
e39e2e95 AK |
1126 | md->frozen_sb = NULL; |
1127 | return r; | |
dfbe03f6 AK |
1128 | } |
1129 | ||
aa8d7c2f AK |
1130 | set_bit(DMF_FROZEN, &md->flags); |
1131 | ||
1da177e4 | 1132 | /* don't bdput right now, we don't want the bdev |
e39e2e95 | 1133 | * to go away while it is locked. |
1da177e4 LT |
1134 | */ |
1135 | return 0; | |
1136 | } | |
1137 | ||
2ca3310e | 1138 | static void unlock_fs(struct mapped_device *md) |
1da177e4 | 1139 | { |
aa8d7c2f AK |
1140 | if (!test_bit(DMF_FROZEN, &md->flags)) |
1141 | return; | |
1142 | ||
e39e2e95 | 1143 | thaw_bdev(md->suspended_bdev, md->frozen_sb); |
1da177e4 | 1144 | md->frozen_sb = NULL; |
aa8d7c2f | 1145 | clear_bit(DMF_FROZEN, &md->flags); |
1da177e4 LT |
1146 | } |
1147 | ||
1148 | /* | |
1149 | * We need to be able to change a mapping table under a mounted | |
1150 | * filesystem. For example we might want to move some data in | |
1151 | * the background. Before the table can be swapped with | |
1152 | * dm_bind_table, dm_suspend must be called to flush any in | |
1153 | * flight bios and ensure that any further io gets deferred. | |
1154 | */ | |
aa8d7c2f | 1155 | int dm_suspend(struct mapped_device *md, int do_lockfs) |
1da177e4 | 1156 | { |
2ca3310e | 1157 | struct dm_table *map = NULL; |
1da177e4 | 1158 | DECLARE_WAITQUEUE(wait, current); |
1ecac7fd | 1159 | struct bio *def; |
cf222b37 | 1160 | int r = -EINVAL; |
1da177e4 | 1161 | |
2ca3310e AK |
1162 | down(&md->suspend_lock); |
1163 | ||
1164 | if (dm_suspended(md)) | |
1165 | goto out; | |
1da177e4 LT |
1166 | |
1167 | map = dm_get_table(md); | |
1da177e4 | 1168 | |
cf222b37 AK |
1169 | /* This does not get reverted if there's an error later. */ |
1170 | dm_table_presuspend_targets(map); | |
1171 | ||
e39e2e95 AK |
1172 | md->suspended_bdev = bdget_disk(md->disk, 0); |
1173 | if (!md->suspended_bdev) { | |
1174 | DMWARN("bdget failed in dm_suspend"); | |
1175 | r = -ENOMEM; | |
1176 | goto out; | |
1177 | } | |
1178 | ||
cf222b37 | 1179 | /* Flush I/O to the device. */ |
aa8d7c2f AK |
1180 | if (do_lockfs) { |
1181 | r = lock_fs(md); | |
1182 | if (r) | |
1183 | goto out; | |
1184 | } | |
1da177e4 LT |
1185 | |
1186 | /* | |
354e0071 | 1187 | * First we set the BLOCK_IO flag so no more ios will be mapped. |
1da177e4 | 1188 | */ |
2ca3310e AK |
1189 | down_write(&md->io_lock); |
1190 | set_bit(DMF_BLOCK_IO, &md->flags); | |
1da177e4 | 1191 | |
1da177e4 | 1192 | add_wait_queue(&md->wait, &wait); |
2ca3310e | 1193 | up_write(&md->io_lock); |
1da177e4 LT |
1194 | |
1195 | /* unplug */ | |
2ca3310e | 1196 | if (map) |
1da177e4 | 1197 | dm_table_unplug_all(map); |
1da177e4 LT |
1198 | |
1199 | /* | |
1200 | * Then we wait for the already mapped ios to | |
1201 | * complete. | |
1202 | */ | |
1203 | while (1) { | |
1204 | set_current_state(TASK_INTERRUPTIBLE); | |
1205 | ||
1206 | if (!atomic_read(&md->pending) || signal_pending(current)) | |
1207 | break; | |
1208 | ||
1209 | io_schedule(); | |
1210 | } | |
1211 | set_current_state(TASK_RUNNING); | |
1212 | ||
2ca3310e | 1213 | down_write(&md->io_lock); |
1da177e4 LT |
1214 | remove_wait_queue(&md->wait, &wait); |
1215 | ||
1216 | /* were we interrupted ? */ | |
cf222b37 | 1217 | r = -EINTR; |
2ca3310e | 1218 | if (atomic_read(&md->pending)) { |
1ecac7fd JN |
1219 | clear_bit(DMF_BLOCK_IO, &md->flags); |
1220 | def = bio_list_get(&md->deferred); | |
1221 | __flush_deferred_io(md, def); | |
2ca3310e AK |
1222 | up_write(&md->io_lock); |
1223 | unlock_fs(md); | |
2ca3310e AK |
1224 | goto out; |
1225 | } | |
1226 | up_write(&md->io_lock); | |
1da177e4 | 1227 | |
cf222b37 | 1228 | dm_table_postsuspend_targets(map); |
1da177e4 | 1229 | |
2ca3310e | 1230 | set_bit(DMF_SUSPENDED, &md->flags); |
b84b0287 | 1231 | |
2ca3310e | 1232 | r = 0; |
b84b0287 | 1233 | |
2ca3310e | 1234 | out: |
e39e2e95 AK |
1235 | if (r && md->suspended_bdev) { |
1236 | bdput(md->suspended_bdev); | |
1237 | md->suspended_bdev = NULL; | |
1238 | } | |
1239 | ||
2ca3310e AK |
1240 | dm_table_put(map); |
1241 | up(&md->suspend_lock); | |
cf222b37 | 1242 | return r; |
1da177e4 LT |
1243 | } |
1244 | ||
1245 | int dm_resume(struct mapped_device *md) | |
1246 | { | |
cf222b37 | 1247 | int r = -EINVAL; |
1da177e4 | 1248 | struct bio *def; |
cf222b37 | 1249 | struct dm_table *map = NULL; |
1da177e4 | 1250 | |
2ca3310e AK |
1251 | down(&md->suspend_lock); |
1252 | if (!dm_suspended(md)) | |
cf222b37 | 1253 | goto out; |
cf222b37 AK |
1254 | |
1255 | map = dm_get_table(md); | |
2ca3310e | 1256 | if (!map || !dm_table_get_size(map)) |
cf222b37 | 1257 | goto out; |
1da177e4 LT |
1258 | |
1259 | dm_table_resume_targets(map); | |
2ca3310e AK |
1260 | |
1261 | down_write(&md->io_lock); | |
1da177e4 LT |
1262 | clear_bit(DMF_BLOCK_IO, &md->flags); |
1263 | ||
1264 | def = bio_list_get(&md->deferred); | |
1265 | __flush_deferred_io(md, def); | |
2ca3310e AK |
1266 | up_write(&md->io_lock); |
1267 | ||
1268 | unlock_fs(md); | |
1269 | ||
e39e2e95 AK |
1270 | bdput(md->suspended_bdev); |
1271 | md->suspended_bdev = NULL; | |
1272 | ||
2ca3310e AK |
1273 | clear_bit(DMF_SUSPENDED, &md->flags); |
1274 | ||
1da177e4 | 1275 | dm_table_unplug_all(map); |
1da177e4 | 1276 | |
cf222b37 | 1277 | r = 0; |
2ca3310e | 1278 | |
cf222b37 AK |
1279 | out: |
1280 | dm_table_put(map); | |
2ca3310e AK |
1281 | up(&md->suspend_lock); |
1282 | ||
cf222b37 | 1283 | return r; |
1da177e4 LT |
1284 | } |
1285 | ||
1286 | /*----------------------------------------------------------------- | |
1287 | * Event notification. | |
1288 | *---------------------------------------------------------------*/ | |
1289 | uint32_t dm_get_event_nr(struct mapped_device *md) | |
1290 | { | |
1291 | return atomic_read(&md->event_nr); | |
1292 | } | |
1293 | ||
1294 | int dm_wait_event(struct mapped_device *md, int event_nr) | |
1295 | { | |
1296 | return wait_event_interruptible(md->eventq, | |
1297 | (event_nr != atomic_read(&md->event_nr))); | |
1298 | } | |
1299 | ||
1300 | /* | |
1301 | * The gendisk is only valid as long as you have a reference | |
1302 | * count on 'md'. | |
1303 | */ | |
1304 | struct gendisk *dm_disk(struct mapped_device *md) | |
1305 | { | |
1306 | return md->disk; | |
1307 | } | |
1308 | ||
1309 | int dm_suspended(struct mapped_device *md) | |
1310 | { | |
1311 | return test_bit(DMF_SUSPENDED, &md->flags); | |
1312 | } | |
1313 | ||
1314 | static struct block_device_operations dm_blk_dops = { | |
1315 | .open = dm_blk_open, | |
1316 | .release = dm_blk_close, | |
3ac51e74 | 1317 | .getgeo = dm_blk_getgeo, |
1da177e4 LT |
1318 | .owner = THIS_MODULE |
1319 | }; | |
1320 | ||
1321 | EXPORT_SYMBOL(dm_get_mapinfo); | |
1322 | ||
1323 | /* | |
1324 | * module hooks | |
1325 | */ | |
1326 | module_init(dm_init); | |
1327 | module_exit(dm_exit); | |
1328 | ||
1329 | module_param(major, uint, 0); | |
1330 | MODULE_PARM_DESC(major, "The major number of the device mapper"); | |
1331 | MODULE_DESCRIPTION(DM_NAME " driver"); | |
1332 | MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); | |
1333 | MODULE_LICENSE("GPL"); |