blob: 32b958dbc499212bfcaceb6afbf72e58c5b4af66 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
Milan Broz784aae72009-01-06 03:05:12 +00003 * Copyright (C) 2004-2008 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"
Mike Anderson51e5b2b2007-10-19 22:48:00 +01009#include "dm-uevent.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/mempool.h>
18#include <linux/slab.h>
19#include <linux/idr.h>
Darrick J. Wong3ac51e72006-03-27 01:17:54 -080020#include <linux/hdreg.h>
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +010021#include <linux/delay.h>
Li Zefan55782132009-06-09 13:43:05 +080022
23#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Alasdair G Kergon72d94862006-06-26 00:27:35 -070025#define DM_MSG_PREFIX "core"
26
Namhyung Kim71a16732011-10-31 20:18:54 +000027#ifdef CONFIG_PRINTK
28/*
29 * ratelimit state to be used in DMXXX_LIMIT().
30 */
31DEFINE_RATELIMIT_STATE(dm_ratelimit_state,
32 DEFAULT_RATELIMIT_INTERVAL,
33 DEFAULT_RATELIMIT_BURST);
34EXPORT_SYMBOL(dm_ratelimit_state);
35#endif
36
Milan Broz60935eb2009-06-22 10:12:30 +010037/*
38 * Cookies are numeric values sent with CHANGE and REMOVE
39 * uevents while resuming, removing or renaming the device.
40 */
41#define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
42#define DM_COOKIE_LENGTH 24
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static const char *_name = DM_NAME;
45
46static unsigned int major = 0;
47static unsigned int _major = 0;
48
Alasdair G Kergond15b7742011-08-02 12:32:01 +010049static DEFINE_IDR(_minor_idr);
50
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -070051static DEFINE_SPINLOCK(_minor_lock);
Mikulas Patocka2c140a22013-11-01 18:27:41 -040052
53static void do_deferred_remove(struct work_struct *w);
54
55static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
56
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -040057static struct workqueue_struct *deferred_remove_workqueue;
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000060 * For bio-based dm.
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 * One of these is allocated per bio.
62 */
63struct dm_io {
64 struct mapped_device *md;
65 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 atomic_t io_count;
Richard Kennedy6ae2fa62008-07-21 12:00:28 +010067 struct bio *bio;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -080068 unsigned long start_time;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +010069 spinlock_t endio_lock;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -040070 struct dm_stats_aux stats_aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071};
72
73/*
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000074 * For request-based dm.
75 * One of these is allocated per request.
76 */
77struct dm_rq_target_io {
78 struct mapped_device *md;
79 struct dm_target *ti;
80 struct request *orig, clone;
81 int error;
82 union map_info info;
83};
84
85/*
Kent Overstreet94818742012-09-07 13:44:01 -070086 * For request-based dm - the bio clones we allocate are embedded in these
87 * structs.
88 *
89 * We allocate these with bio_alloc_bioset, using the front_pad parameter when
90 * the bioset is created - this means the bio has to come at the end of the
91 * struct.
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000092 */
93struct dm_rq_clone_bio_info {
94 struct bio *orig;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010095 struct dm_rq_target_io *tio;
Kent Overstreet94818742012-09-07 13:44:01 -070096 struct bio clone;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +000097};
98
Kiyoshi Uedacec47e32009-06-22 10:12:35 +010099union map_info *dm_get_rq_mapinfo(struct request *rq)
100{
101 if (rq && rq->end_io_data)
102 return &((struct dm_rq_target_io *)rq->end_io_data)->info;
103 return NULL;
104}
105EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
106
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -0700107#define MINOR_ALLOCED ((void *)-1)
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/*
110 * Bits for the md->flags field.
111 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +0100112#define DMF_BLOCK_IO_FOR_SUSPEND 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#define DMF_SUSPENDED 1
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -0800114#define DMF_FROZEN 2
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700115#define DMF_FREEING 3
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700116#define DMF_DELETING 4
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800117#define DMF_NOFLUSH_SUSPENDING 5
Mikulas Patockad5b9dd02011-08-02 12:32:04 +0100118#define DMF_MERGE_IS_OPTIONAL 6
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400119#define DMF_DEFERRED_REMOVE 7
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Milan Broz304f3f62008-02-08 02:11:17 +0000121/*
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100122 * A dummy definition to make RCU happy.
123 * struct dm_table should never be dereferenced in this file.
124 */
125struct dm_table {
126 int undefined__;
127};
128
129/*
Milan Broz304f3f62008-02-08 02:11:17 +0000130 * Work processed by per-device workqueue.
131 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132struct mapped_device {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100133 struct srcu_struct io_barrier;
Daniel Walkere61290a2008-02-08 02:10:08 +0000134 struct mutex suspend_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 atomic_t holders;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700136 atomic_t open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Mikulas Patocka2a7faeb2013-07-10 23:41:18 +0100138 /*
139 * The current mapping.
140 * Use dm_get_live_table{_fast} or take suspend_lock for
141 * dereference.
142 */
143 struct dm_table *map;
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 unsigned long flags;
146
Jens Axboe165125e2007-07-24 09:28:11 +0200147 struct request_queue *queue;
Mike Snitzera5664da2010-08-12 04:14:01 +0100148 unsigned type;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +0100149 /* Protect queue and type against concurrent access. */
Mike Snitzera5664da2010-08-12 04:14:01 +0100150 struct mutex type_lock;
151
Alasdair G Kergon36a04562011-10-31 20:19:04 +0000152 struct target_type *immutable_target_type;
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 struct gendisk *disk;
Mike Anderson7e51f252006-03-27 01:17:52 -0800155 char name[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 void *interface_ptr;
158
159 /*
160 * A list of ios that arrived while we were suspended.
161 */
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200162 atomic_t pending[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 wait_queue_head_t wait;
Mikulas Patocka53d59142009-04-02 19:55:37 +0100164 struct work_struct work;
Kiyoshi Ueda74859362006-12-08 02:41:02 -0800165 struct bio_list deferred;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100166 spinlock_t deferred_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 /*
Tejun Heo29e40132010-09-08 18:07:00 +0200169 * Processing queue (flush)
Milan Broz304f3f62008-02-08 02:11:17 +0000170 */
171 struct workqueue_struct *wq;
172
173 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 * io objects are allocated from here.
175 */
176 mempool_t *io_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Stefan Bader9faf4002006-10-03 01:15:41 -0700178 struct bio_set *bs;
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 /*
181 * Event handling.
182 */
183 atomic_t event_nr;
184 wait_queue_head_t eventq;
Mike Anderson7a8c3d32007-10-19 22:48:01 +0100185 atomic_t uevent_seq;
186 struct list_head uevent_list;
187 spinlock_t uevent_lock; /* Protect access to uevent_list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 /*
190 * freeze/thaw support require holding onto a super block
191 */
192 struct super_block *frozen_sb;
Mikulas Patockadb8fef42009-06-22 10:12:15 +0100193 struct block_device *bdev;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800194
195 /* forced geometry settings */
196 struct hd_geometry geometry;
Milan Broz784aae72009-01-06 03:05:12 +0000197
Mikulas Patocka2995fa72014-01-13 19:37:54 -0500198 /* kobject and completion */
199 struct dm_kobject_holder kobj_holder;
Mikulas Patockabe35f482014-01-06 23:01:22 -0500200
Tejun Heod87f4c12010-09-03 11:56:19 +0200201 /* zero-length flush that will be cloned and submitted to targets */
202 struct bio flush_bio;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400203
204 struct dm_stats stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205};
206
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100207/*
208 * For mempools pre-allocation at the table loading time.
209 */
210struct dm_md_mempools {
211 mempool_t *io_pool;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +0100212 struct bio_set *bs;
213};
214
Mike Snitzer6cfa5852013-09-12 18:06:11 -0400215#define RESERVED_BIO_BASED_IOS 16
216#define RESERVED_REQUEST_BASED_IOS 256
Mike Snitzerf4790822013-09-12 18:06:12 -0400217#define RESERVED_MAX_IOS 1024
Christoph Lametere18b8902006-12-06 20:33:20 -0800218static struct kmem_cache *_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000219static struct kmem_cache *_rq_tio_cache;
Kent Overstreet94818742012-09-07 13:44:01 -0700220
Mike Snitzerf4790822013-09-12 18:06:12 -0400221/*
Mike Snitzere8603132013-09-12 18:06:12 -0400222 * Bio-based DM's mempools' reserved IOs set by the user.
223 */
224static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
225
226/*
Mike Snitzerf4790822013-09-12 18:06:12 -0400227 * Request-based DM's mempools' reserved IOs set by the user.
228 */
229static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
230
231static unsigned __dm_get_reserved_ios(unsigned *reserved_ios,
232 unsigned def, unsigned max)
233{
234 unsigned ios = ACCESS_ONCE(*reserved_ios);
235 unsigned modified_ios = 0;
236
237 if (!ios)
238 modified_ios = def;
239 else if (ios > max)
240 modified_ios = max;
241
242 if (modified_ios) {
243 (void)cmpxchg(reserved_ios, ios, modified_ios);
244 ios = modified_ios;
245 }
246
247 return ios;
248}
249
Mike Snitzere8603132013-09-12 18:06:12 -0400250unsigned dm_get_reserved_bio_based_ios(void)
251{
252 return __dm_get_reserved_ios(&reserved_bio_based_ios,
253 RESERVED_BIO_BASED_IOS, RESERVED_MAX_IOS);
254}
255EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
256
Mike Snitzerf4790822013-09-12 18:06:12 -0400257unsigned dm_get_reserved_rq_based_ios(void)
258{
259 return __dm_get_reserved_ios(&reserved_rq_based_ios,
260 RESERVED_REQUEST_BASED_IOS, RESERVED_MAX_IOS);
261}
262EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios);
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264static int __init local_init(void)
265{
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100266 int r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100269 _io_cache = KMEM_CACHE(dm_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (!_io_cache)
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100271 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000273 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
274 if (!_rq_tio_cache)
Mikulas Patockadba14162012-10-12 21:02:15 +0100275 goto out_free_io_cache;
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000276
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100277 r = dm_uevent_init();
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100278 if (r)
Jun'ichi Nomura23e50832013-03-01 22:45:48 +0000279 goto out_free_rq_tio_cache;
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100280
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400281 deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
282 if (!deferred_remove_workqueue) {
283 r = -ENOMEM;
284 goto out_uevent_exit;
285 }
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 _major = major;
288 r = register_blkdev(_major, _name);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100289 if (r < 0)
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400290 goto out_free_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 if (!_major)
293 _major = r;
294
295 return 0;
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100296
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400297out_free_workqueue:
298 destroy_workqueue(deferred_remove_workqueue);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100299out_uevent_exit:
300 dm_uevent_exit();
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000301out_free_rq_tio_cache:
302 kmem_cache_destroy(_rq_tio_cache);
Kiyoshi Ueda51157b42008-10-21 17:45:08 +0100303out_free_io_cache:
304 kmem_cache_destroy(_io_cache);
305
306 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
309static void local_exit(void)
310{
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400311 flush_scheduled_work();
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400312 destroy_workqueue(deferred_remove_workqueue);
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400313
Kiyoshi Ueda8fbf26a2009-01-06 03:05:06 +0000314 kmem_cache_destroy(_rq_tio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 kmem_cache_destroy(_io_cache);
Akinobu Mita00d59402007-07-17 04:03:46 -0700316 unregister_blkdev(_major, _name);
Mike Anderson51e5b2b2007-10-19 22:48:00 +0100317 dm_uevent_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 _major = 0;
320
321 DMINFO("cleaned up");
322}
323
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000324static int (*_inits[])(void) __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 local_init,
326 dm_target_init,
327 dm_linear_init,
328 dm_stripe_init,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000329 dm_io_init,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100330 dm_kcopyd_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 dm_interface_init,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400332 dm_statistics_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333};
334
Alasdair G Kergonb9249e52008-02-08 02:09:51 +0000335static void (*_exits[])(void) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 local_exit,
337 dm_target_exit,
338 dm_linear_exit,
339 dm_stripe_exit,
Mikulas Patocka952b3552009-12-10 23:51:57 +0000340 dm_io_exit,
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100341 dm_kcopyd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 dm_interface_exit,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400343 dm_statistics_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344};
345
346static int __init dm_init(void)
347{
348 const int count = ARRAY_SIZE(_inits);
349
350 int r, i;
351
352 for (i = 0; i < count; i++) {
353 r = _inits[i]();
354 if (r)
355 goto bad;
356 }
357
358 return 0;
359
360 bad:
361 while (i--)
362 _exits[i]();
363
364 return r;
365}
366
367static void __exit dm_exit(void)
368{
369 int i = ARRAY_SIZE(_exits);
370
371 while (i--)
372 _exits[i]();
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100373
374 /*
375 * Should be empty by this point.
376 */
Alasdair G Kergond15b7742011-08-02 12:32:01 +0100377 idr_destroy(&_minor_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
380/*
381 * Block device functions
382 */
Mike Anderson432a2122009-12-10 23:52:20 +0000383int dm_deleting_md(struct mapped_device *md)
384{
385 return test_bit(DMF_DELETING, &md->flags);
386}
387
Al Virofe5f9f22008-03-02 10:29:31 -0500388static int dm_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 struct mapped_device *md;
391
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700392 spin_lock(&_minor_lock);
393
Al Virofe5f9f22008-03-02 10:29:31 -0500394 md = bdev->bd_disk->private_data;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700395 if (!md)
396 goto out;
397
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700398 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +0000399 dm_deleting_md(md)) {
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700400 md = NULL;
401 goto out;
402 }
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700405 atomic_inc(&md->open_count);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -0700406
407out:
408 spin_unlock(&_minor_lock);
409
410 return md ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
Al Virodb2a1442013-05-05 21:52:57 -0400413static void dm_blk_close(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Al Virofe5f9f22008-03-02 10:29:31 -0500415 struct mapped_device *md = disk->private_data;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200416
Milan Broz4a1aeb92011-01-13 19:59:48 +0000417 spin_lock(&_minor_lock);
418
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400419 if (atomic_dec_and_test(&md->open_count) &&
420 (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
Mikulas Patockaacfe0ad2014-06-14 13:44:31 -0400421 queue_work(deferred_remove_workqueue, &deferred_remove_work);
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 dm_put(md);
Milan Broz4a1aeb92011-01-13 19:59:48 +0000424
425 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700428int dm_open_count(struct mapped_device *md)
429{
430 return atomic_read(&md->open_count);
431}
432
433/*
434 * Guarantees nothing is using the device before it's deleted.
435 */
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400436int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700437{
438 int r = 0;
439
440 spin_lock(&_minor_lock);
441
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400442 if (dm_open_count(md)) {
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700443 r = -EBUSY;
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400444 if (mark_deferred)
445 set_bit(DMF_DEFERRED_REMOVE, &md->flags);
446 } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
447 r = -EEXIST;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700448 else
449 set_bit(DMF_DELETING, &md->flags);
450
451 spin_unlock(&_minor_lock);
452
453 return r;
454}
455
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400456int dm_cancel_deferred_remove(struct mapped_device *md)
457{
458 int r = 0;
459
460 spin_lock(&_minor_lock);
461
462 if (test_bit(DMF_DELETING, &md->flags))
463 r = -EBUSY;
464 else
465 clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
466
467 spin_unlock(&_minor_lock);
468
469 return r;
470}
471
472static void do_deferred_remove(struct work_struct *w)
473{
474 dm_deferred_remove();
475}
476
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400477sector_t dm_get_size(struct mapped_device *md)
478{
479 return get_capacity(md->disk);
480}
481
Mike Snitzer9974fa22014-02-28 15:33:43 +0100482struct request_queue *dm_get_md_queue(struct mapped_device *md)
483{
484 return md->queue;
485}
486
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400487struct dm_stats *dm_get_stats(struct mapped_device *md)
488{
489 return &md->stats;
490}
491
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800492static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
493{
494 struct mapped_device *md = bdev->bd_disk->private_data;
495
496 return dm_get_geometry(md, geo);
497}
498
Al Virofe5f9f22008-03-02 10:29:31 -0500499static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
Milan Brozaa129a22006-10-03 01:15:15 -0700500 unsigned int cmd, unsigned long arg)
501{
Al Virofe5f9f22008-03-02 10:29:31 -0500502 struct mapped_device *md = bdev->bd_disk->private_data;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100503 int srcu_idx;
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100504 struct dm_table *map;
Milan Brozaa129a22006-10-03 01:15:15 -0700505 struct dm_target *tgt;
506 int r = -ENOTTY;
507
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100508retry:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100509 map = dm_get_live_table(md, &srcu_idx);
510
Milan Brozaa129a22006-10-03 01:15:15 -0700511 if (!map || !dm_table_get_size(map))
512 goto out;
513
514 /* We only support devices that have a single target */
515 if (dm_table_get_num_targets(map) != 1)
516 goto out;
517
518 tgt = dm_table_get_target(map, 0);
519
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000520 if (dm_suspended_md(md)) {
Milan Brozaa129a22006-10-03 01:15:15 -0700521 r = -EAGAIN;
522 goto out;
523 }
524
525 if (tgt->type->ioctl)
Al Viro647b3d02007-08-28 22:15:59 -0400526 r = tgt->type->ioctl(tgt, cmd, arg);
Milan Brozaa129a22006-10-03 01:15:15 -0700527
528out:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100529 dm_put_live_table(md, srcu_idx);
Milan Brozaa129a22006-10-03 01:15:15 -0700530
Hannes Reinecke6c182cd2013-07-10 23:41:15 +0100531 if (r == -ENOTCONN) {
532 msleep(10);
533 goto retry;
534 }
535
Milan Brozaa129a22006-10-03 01:15:15 -0700536 return r;
537}
538
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100539static struct dm_io *alloc_io(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 return mempool_alloc(md->io_pool, GFP_NOIO);
542}
543
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100544static void free_io(struct mapped_device *md, struct dm_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 mempool_free(io, md->io_pool);
547}
548
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100549static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Mikulas Patockadba14162012-10-12 21:02:15 +0100551 bio_put(&tio->clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Kiyoshi Ueda08885642009-12-10 23:52:15 +0000554static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
555 gfp_t gfp_mask)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100556{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +0000557 return mempool_alloc(md->io_pool, gfp_mask);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100558}
559
560static void free_rq_tio(struct dm_rq_target_io *tio)
561{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +0000562 mempool_free(tio, tio->md->io_pool);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100563}
564
Kiyoshi Ueda90abb8c2009-12-10 23:52:13 +0000565static int md_in_flight(struct mapped_device *md)
566{
567 return atomic_read(&md->pending[READ]) +
568 atomic_read(&md->pending[WRITE]);
569}
570
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800571static void start_io_acct(struct dm_io *io)
572{
573 struct mapped_device *md = io->md;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400574 struct bio *bio = io->bio;
Tejun Heoc9959052008-08-25 19:47:21 +0900575 int cpu;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400576 int rw = bio_data_dir(bio);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800577
578 io->start_time = jiffies;
579
Tejun Heo074a7ac2008-08-25 19:56:14 +0900580 cpu = part_stat_lock();
581 part_round_stats(cpu, &dm_disk(md)->part0);
582 part_stat_unlock();
Shaohua Li1e9bb882011-03-22 08:35:35 +0100583 atomic_set(&dm_disk(md)->part0.in_flight[rw],
584 atomic_inc_return(&md->pending[rw]));
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400585
586 if (unlikely(dm_stats_used(&md->stats)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700587 dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400588 bio_sectors(bio), false, 0, &io->stats_aux);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800589}
590
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000591static void end_io_acct(struct dm_io *io)
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800592{
593 struct mapped_device *md = io->md;
594 struct bio *bio = io->bio;
595 unsigned long duration = jiffies - io->start_time;
Tejun Heoc9959052008-08-25 19:47:21 +0900596 int pending, cpu;
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800597 int rw = bio_data_dir(bio);
598
Tejun Heo074a7ac2008-08-25 19:56:14 +0900599 cpu = part_stat_lock();
600 part_round_stats(cpu, &dm_disk(md)->part0);
601 part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
602 part_stat_unlock();
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800603
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400604 if (unlikely(dm_stats_used(&md->stats)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700605 dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -0400606 bio_sectors(bio), true, duration, &io->stats_aux);
607
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100608 /*
609 * After this is decremented the bio must not be touched if it is
Tejun Heod87f4c12010-09-03 11:56:19 +0200610 * a flush.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100611 */
Shaohua Li1e9bb882011-03-22 08:35:35 +0100612 pending = atomic_dec_return(&md->pending[rw]);
613 atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200614 pending += atomic_read(&md->pending[rw^0x1]);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800615
Mikulas Patockad221d2e2008-11-13 23:39:10 +0000616 /* nudge anyone waiting on suspend queue */
617 if (!pending)
618 wake_up(&md->wait);
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800619}
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621/*
622 * Add the bio to the list of deferred io.
623 */
Mikulas Patocka92c63902009-04-09 00:27:15 +0100624static void queue_io(struct mapped_device *md, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200626 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200628 spin_lock_irqsave(&md->deferred_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 bio_list_add(&md->deferred, bio);
Kiyoshi Ueda054474202010-09-08 18:07:01 +0200630 spin_unlock_irqrestore(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200631 queue_work(md->wq, &md->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
634/*
635 * Everyone (including functions in this file), should use this
636 * function to access the md->map field, and make sure they call
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100637 * dm_put_live_table() when finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100639struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100641 *srcu_idx = srcu_read_lock(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100643 return srcu_dereference(md->map, &md->io_barrier);
644}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100646void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
647{
648 srcu_read_unlock(&md->io_barrier, srcu_idx);
649}
650
651void dm_sync_table(struct mapped_device *md)
652{
653 synchronize_srcu(&md->io_barrier);
654 synchronize_rcu_expedited();
655}
656
657/*
658 * A fast alternative to dm_get_live_table/dm_put_live_table.
659 * The caller must not block between these two functions.
660 */
661static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
662{
663 rcu_read_lock();
664 return rcu_dereference(md->map);
665}
666
667static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
668{
669 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800672/*
673 * Get the geometry associated with a dm device
674 */
675int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
676{
677 *geo = md->geometry;
678
679 return 0;
680}
681
682/*
683 * Set the geometry of a device.
684 */
685int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
686{
687 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
688
689 if (geo->start > sz) {
690 DMWARN("Start sector is beyond the geometry limits.");
691 return -EINVAL;
692 }
693
694 md->geometry = *geo;
695
696 return 0;
697}
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699/*-----------------------------------------------------------------
700 * CRUD START:
701 * A more elegant soln is in the works that uses the queue
702 * merge fn, unfortunately there are a couple of changes to
703 * the block layer that I want to make for this. So in the
704 * interests of getting something for people to use I give
705 * you this clearly demarcated crap.
706 *---------------------------------------------------------------*/
707
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800708static int __noflush_suspending(struct mapped_device *md)
709{
710 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
711}
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713/*
714 * Decrements the number of outstanding ios that a bio has been
715 * cloned into, completing the original io if necc.
716 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800717static void dec_pending(struct dm_io *io, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800719 unsigned long flags;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000720 int io_error;
721 struct bio *bio;
722 struct mapped_device *md = io->md;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800723
724 /* Push-back supersedes any I/O errors */
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +0100725 if (unlikely(error)) {
726 spin_lock_irqsave(&io->endio_lock, flags);
727 if (!(io->error > 0 && __noflush_suspending(md)))
728 io->error = error;
729 spin_unlock_irqrestore(&io->endio_lock, flags);
730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 if (atomic_dec_and_test(&io->io_count)) {
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800733 if (io->error == DM_ENDIO_REQUEUE) {
734 /*
735 * Target requested pushing back the I/O.
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800736 */
Mikulas Patocka022c2612009-04-02 19:55:39 +0100737 spin_lock_irqsave(&md->deferred_lock, flags);
Tejun Heo6a8736d2010-09-08 18:07:00 +0200738 if (__noflush_suspending(md))
739 bio_list_add_head(&md->deferred, io->bio);
740 else
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800741 /* noflush suspend was interrupted. */
742 io->error = -EIO;
Mikulas Patocka022c2612009-04-02 19:55:39 +0100743 spin_unlock_irqrestore(&md->deferred_lock, flags);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800744 }
745
Milan Brozb35f8ca2009-03-16 17:44:36 +0000746 io_error = io->error;
747 bio = io->bio;
Tejun Heo6a8736d2010-09-08 18:07:00 +0200748 end_io_acct(io);
749 free_io(md, io);
Jens Axboe2056a782006-03-23 20:00:26 +0100750
Tejun Heo6a8736d2010-09-08 18:07:00 +0200751 if (io_error == DM_ENDIO_REQUEUE)
752 return;
753
Kent Overstreet4f024f32013-10-11 15:44:27 -0700754 if ((bio->bi_rw & REQ_FLUSH) && bio->bi_iter.bi_size) {
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100755 /*
Tejun Heo6a8736d2010-09-08 18:07:00 +0200756 * Preflush done for flush with data, reissue
757 * without REQ_FLUSH.
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100758 */
Tejun Heo6a8736d2010-09-08 18:07:00 +0200759 bio->bi_rw &= ~REQ_FLUSH;
760 queue_io(md, bio);
Mikulas Patockaaf7e4662009-04-09 00:27:16 +0100761 } else {
Mike Snitzerb372d362010-09-08 18:07:01 +0200762 /* done with normal IO or empty flush */
Linus Torvalds0a82a8d2013-04-18 09:00:26 -0700763 trace_block_bio_complete(md->queue, bio, io_error);
Mike Snitzerb372d362010-09-08 18:07:01 +0200764 bio_endio(bio, io_error);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
767}
768
Mike Snitzer7eee4ae2014-06-02 15:50:06 -0400769static void disable_write_same(struct mapped_device *md)
770{
771 struct queue_limits *limits = dm_get_queue_limits(md);
772
773 /* device doesn't really support WRITE SAME, disable it */
774 limits->max_write_same_sectors = 0;
775}
776
NeilBrown6712ecf2007-09-27 12:47:43 +0200777static void clone_endio(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
779 int r = 0;
Mikulas Patockabfc6d412014-03-04 18:24:49 -0500780 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000781 struct dm_io *io = tio->io;
Stefan Bader9faf4002006-10-03 01:15:41 -0700782 struct mapped_device *md = tio->io->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 dm_endio_fn endio = tio->ti->type->end_io;
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
786 error = -EIO;
787
788 if (endio) {
Mikulas Patocka7de3ee52012-12-21 20:23:41 +0000789 r = endio(tio->ti, bio, error);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800790 if (r < 0 || r == DM_ENDIO_REQUEUE)
791 /*
792 * error and requeue request are handled
793 * in dec_pending().
794 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 error = r;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800796 else if (r == DM_ENDIO_INCOMPLETE)
797 /* The target will handle the io */
NeilBrown6712ecf2007-09-27 12:47:43 +0200798 return;
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -0800799 else if (r) {
800 DMWARN("unimplemented target endio return value: %d", r);
801 BUG();
802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804
Mike Snitzer7eee4ae2014-06-02 15:50:06 -0400805 if (unlikely(r == -EREMOTEIO && (bio->bi_rw & REQ_WRITE_SAME) &&
806 !bdev_get_queue(bio->bi_bdev)->limits.max_write_same_sectors))
807 disable_write_same(md);
808
Stefan Bader9faf4002006-10-03 01:15:41 -0700809 free_tio(md, tio);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000810 dec_pending(io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100813/*
814 * Partial completion handling for request-based dm
815 */
816static void end_clone_bio(struct bio *clone, int error)
817{
Mikulas Patockabfc6d412014-03-04 18:24:49 -0500818 struct dm_rq_clone_bio_info *info =
819 container_of(clone, struct dm_rq_clone_bio_info, clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100820 struct dm_rq_target_io *tio = info->tio;
821 struct bio *bio = info->orig;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700822 unsigned int nr_bytes = info->orig->bi_iter.bi_size;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100823
824 bio_put(clone);
825
826 if (tio->error)
827 /*
828 * An error has already been detected on the request.
829 * Once error occurred, just let clone->end_io() handle
830 * the remainder.
831 */
832 return;
833 else if (error) {
834 /*
835 * Don't notice the error to the upper layer yet.
836 * The error handling decision is made by the target driver,
837 * when the request is completed.
838 */
839 tio->error = error;
840 return;
841 }
842
843 /*
844 * I/O for the bio successfully completed.
845 * Notice the data completion to the upper layer.
846 */
847
848 /*
849 * bios are processed from the head of the list.
850 * So the completing bio should always be rq->bio.
851 * If it's not, something wrong is happening.
852 */
853 if (tio->orig->bio != bio)
854 DMERR("bio completion is going in the middle of the request");
855
856 /*
857 * Update the original request.
858 * Do not use blk_end_request() here, because it may complete
859 * the original request before the clone, and break the ordering.
860 */
861 blk_update_request(tio->orig, 0, nr_bytes);
862}
863
864/*
865 * Don't touch any member of the md after calling this function because
866 * the md may be freed in dm_put() at the end of this function.
867 * Or do dm_get() before calling this function and dm_put() later.
868 */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000869static void rq_completed(struct mapped_device *md, int rw, int run_queue)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100870{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000871 atomic_dec(&md->pending[rw]);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100872
873 /* nudge anyone waiting on suspend queue */
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000874 if (!md_in_flight(md))
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100875 wake_up(&md->wait);
876
Jens Axboea8c32a52012-11-06 12:24:26 +0100877 /*
878 * Run this off this callpath, as drivers could invoke end_io while
879 * inside their request_fn (and holding the queue lock). Calling
880 * back into ->request_fn() could deadlock attempting to grab the
881 * queue lock again.
882 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100883 if (run_queue)
Jens Axboea8c32a52012-11-06 12:24:26 +0100884 blk_run_queue_async(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100885
886 /*
887 * dm_put() must be at the end of this function. See the comment above
888 */
889 dm_put(md);
890}
891
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100892static void free_rq_clone(struct request *clone)
893{
894 struct dm_rq_target_io *tio = clone->end_io_data;
895
896 blk_rq_unprep_clone(clone);
897 free_rq_tio(tio);
898}
899
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000900/*
901 * Complete the clone and the original request.
902 * Must be called without queue lock.
903 */
904static void dm_end_request(struct request *clone, int error)
905{
906 int rw = rq_data_dir(clone);
907 struct dm_rq_target_io *tio = clone->end_io_data;
908 struct mapped_device *md = tio->md;
909 struct request *rq = tio->orig;
910
Tejun Heo29e40132010-09-08 18:07:00 +0200911 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000912 rq->errors = clone->errors;
913 rq->resid_len = clone->resid_len;
914
915 if (rq->sense)
916 /*
917 * We are using the sense buffer of the original
918 * request.
919 * So setting the length of the sense data is enough.
920 */
921 rq->sense_len = clone->sense_len;
922 }
923
924 free_rq_clone(clone);
Tejun Heo29e40132010-09-08 18:07:00 +0200925 blk_end_request_all(rq, error);
926 rq_completed(md, rw, true);
Kiyoshi Ueda980691e2009-12-10 23:52:17 +0000927}
928
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100929static void dm_unprep_request(struct request *rq)
930{
931 struct request *clone = rq->special;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100932
933 rq->special = NULL;
934 rq->cmd_flags &= ~REQ_DONTPREP;
935
Kiyoshi Uedaa77e28c2009-09-04 20:40:16 +0100936 free_rq_clone(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100937}
938
939/*
940 * Requeue the original request of a clone.
941 */
942void dm_requeue_unmapped_request(struct request *clone)
943{
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000944 int rw = rq_data_dir(clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100945 struct dm_rq_target_io *tio = clone->end_io_data;
946 struct mapped_device *md = tio->md;
947 struct request *rq = tio->orig;
948 struct request_queue *q = rq->q;
949 unsigned long flags;
950
951 dm_unprep_request(rq);
952
953 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100954 blk_requeue_request(q, rq);
955 spin_unlock_irqrestore(q->queue_lock, flags);
956
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +0000957 rq_completed(md, rw, 0);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100958}
959EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
960
961static void __stop_queue(struct request_queue *q)
962{
963 blk_stop_queue(q);
964}
965
966static void stop_queue(struct request_queue *q)
967{
968 unsigned long flags;
969
970 spin_lock_irqsave(q->queue_lock, flags);
971 __stop_queue(q);
972 spin_unlock_irqrestore(q->queue_lock, flags);
973}
974
975static void __start_queue(struct request_queue *q)
976{
977 if (blk_queue_stopped(q))
978 blk_start_queue(q);
979}
980
981static void start_queue(struct request_queue *q)
982{
983 unsigned long flags;
984
985 spin_lock_irqsave(q->queue_lock, flags);
986 __start_queue(q);
987 spin_unlock_irqrestore(q->queue_lock, flags);
988}
989
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000990static void dm_done(struct request *clone, int error, bool mapped)
991{
992 int r = error;
993 struct dm_rq_target_io *tio = clone->end_io_data;
Mike Snitzerba1cbad2012-09-26 23:45:42 +0100994 dm_request_endio_fn rq_end_io = NULL;
Kiyoshi Ueda11a68242009-12-10 23:52:17 +0000995
Mike Snitzerba1cbad2012-09-26 23:45:42 +0100996 if (tio->ti) {
997 rq_end_io = tio->ti->type->rq_end_io;
998
999 if (mapped && rq_end_io)
1000 r = rq_end_io(tio->ti, clone, error, &tio->info);
1001 }
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001002
Mike Snitzer7eee4ae2014-06-02 15:50:06 -04001003 if (unlikely(r == -EREMOTEIO && (clone->cmd_flags & REQ_WRITE_SAME) &&
1004 !clone->q->limits.max_write_same_sectors))
1005 disable_write_same(tio->md);
1006
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001007 if (r <= 0)
1008 /* The target wants to complete the I/O */
1009 dm_end_request(clone, r);
1010 else if (r == DM_ENDIO_INCOMPLETE)
1011 /* The target will handle the I/O */
1012 return;
1013 else if (r == DM_ENDIO_REQUEUE)
1014 /* The target wants to requeue the I/O */
1015 dm_requeue_unmapped_request(clone);
1016 else {
1017 DMWARN("unimplemented target endio return value: %d", r);
1018 BUG();
1019 }
1020}
1021
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001022/*
1023 * Request completion handler for request-based dm
1024 */
1025static void dm_softirq_done(struct request *rq)
1026{
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001027 bool mapped = true;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001028 struct request *clone = rq->completion_data;
1029 struct dm_rq_target_io *tio = clone->end_io_data;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001030
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001031 if (rq->cmd_flags & REQ_FAILED)
1032 mapped = false;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001033
Kiyoshi Ueda11a68242009-12-10 23:52:17 +00001034 dm_done(clone, tio->error, mapped);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001035}
1036
1037/*
1038 * Complete the clone and the original request with the error status
1039 * through softirq context.
1040 */
1041static void dm_complete_request(struct request *clone, int error)
1042{
1043 struct dm_rq_target_io *tio = clone->end_io_data;
1044 struct request *rq = tio->orig;
1045
1046 tio->error = error;
1047 rq->completion_data = clone;
1048 blk_complete_request(rq);
1049}
1050
1051/*
1052 * Complete the not-mapped clone and the original request with the error status
1053 * through softirq context.
1054 * Target's rq_end_io() function isn't called.
1055 * This may be used when the target's map_rq() function fails.
1056 */
1057void dm_kill_unmapped_request(struct request *clone, int error)
1058{
1059 struct dm_rq_target_io *tio = clone->end_io_data;
1060 struct request *rq = tio->orig;
1061
1062 rq->cmd_flags |= REQ_FAILED;
1063 dm_complete_request(clone, error);
1064}
1065EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
1066
1067/*
1068 * Called with the queue lock held
1069 */
1070static void end_clone_request(struct request *clone, int error)
1071{
1072 /*
1073 * For just cleaning up the information of the queue in which
1074 * the clone was dispatched.
1075 * The clone is *NOT* freed actually here because it is alloced from
1076 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
1077 */
1078 __blk_put_request(clone->q, clone);
1079
1080 /*
1081 * Actual request completion is done in a softirq context which doesn't
1082 * hold the queue lock. Otherwise, deadlock could occur because:
1083 * - another request may be submitted by the upper level driver
1084 * of the stacking during the completion
1085 * - the submission which requires queue lock may be done
1086 * against this queue
1087 */
1088 dm_complete_request(clone, error);
1089}
1090
Mike Snitzer56a67df2010-08-12 04:14:10 +01001091/*
1092 * Return maximum size of I/O possible at the supplied sector up to the current
1093 * target boundary.
1094 */
1095static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Mike Snitzer56a67df2010-08-12 04:14:10 +01001097 sector_t target_offset = dm_target_offset(ti, sector);
1098
1099 return ti->len - target_offset;
1100}
1101
1102static sector_t max_io_len(sector_t sector, struct dm_target *ti)
1103{
1104 sector_t len = max_io_len_target_boundary(sector, ti);
Mike Snitzer542f9032012-07-27 15:08:00 +01001105 sector_t offset, max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 /*
Mike Snitzer542f9032012-07-27 15:08:00 +01001108 * Does the target need to split even further?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 */
Mike Snitzer542f9032012-07-27 15:08:00 +01001110 if (ti->max_io_len) {
1111 offset = dm_target_offset(ti, sector);
1112 if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
1113 max_len = sector_div(offset, ti->max_io_len);
1114 else
1115 max_len = offset & (ti->max_io_len - 1);
1116 max_len = ti->max_io_len - max_len;
1117
1118 if (len > max_len)
1119 len = max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
1121
1122 return len;
1123}
1124
Mike Snitzer542f9032012-07-27 15:08:00 +01001125int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1126{
1127 if (len > UINT_MAX) {
1128 DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1129 (unsigned long long)len, UINT_MAX);
1130 ti->error = "Maximum size of target IO is too large";
1131 return -EINVAL;
1132 }
1133
1134 ti->max_io_len = (uint32_t) len;
1135
1136 return 0;
1137}
1138EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1139
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001140/*
1141 * A target may call dm_accept_partial_bio only from the map routine. It is
1142 * allowed for all bio types except REQ_FLUSH.
1143 *
1144 * dm_accept_partial_bio informs the dm that the target only wants to process
1145 * additional n_sectors sectors of the bio and the rest of the data should be
1146 * sent in a next bio.
1147 *
1148 * A diagram that explains the arithmetics:
1149 * +--------------------+---------------+-------+
1150 * | 1 | 2 | 3 |
1151 * +--------------------+---------------+-------+
1152 *
1153 * <-------------- *tio->len_ptr --------------->
1154 * <------- bi_size ------->
1155 * <-- n_sectors -->
1156 *
1157 * Region 1 was already iterated over with bio_advance or similar function.
1158 * (it may be empty if the target doesn't use bio_advance)
1159 * Region 2 is the remaining bio size that the target wants to process.
1160 * (it may be empty if region 1 is non-empty, although there is no reason
1161 * to make it empty)
1162 * The target requires that region 3 is to be sent in the next bio.
1163 *
1164 * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
1165 * the partially processed part (the sum of regions 1+2) must be the same for all
1166 * copies of the bio.
1167 */
1168void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
1169{
1170 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
1171 unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
1172 BUG_ON(bio->bi_rw & REQ_FLUSH);
1173 BUG_ON(bi_size > *tio->len_ptr);
1174 BUG_ON(n_sectors > bi_size);
1175 *tio->len_ptr -= bi_size - n_sectors;
1176 bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
1177}
1178EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
1179
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001180static void __map_bio(struct dm_target_io *tio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
1182 int r;
Jens Axboe2056a782006-03-23 20:00:26 +01001183 sector_t sector;
Stefan Bader9faf4002006-10-03 01:15:41 -07001184 struct mapped_device *md;
Mikulas Patockadba14162012-10-12 21:02:15 +01001185 struct bio *clone = &tio->clone;
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001186 struct dm_target *ti = tio->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 clone->bi_end_io = clone_endio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 /*
1191 * Map the clone. If r == 0 we don't need to do
1192 * anything, the target has assumed ownership of
1193 * this io.
1194 */
1195 atomic_inc(&tio->io->io_count);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001196 sector = clone->bi_iter.bi_sector;
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001197 r = ti->type->map(ti, clone);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001198 if (r == DM_MAPIO_REMAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 /* the bio has been remapped so dispatch it */
Jens Axboe2056a782006-03-23 20:00:26 +01001200
Mike Snitzerd07335e2010-11-16 12:52:38 +01001201 trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
1202 tio->io->bio->bi_bdev->bd_dev, sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 generic_make_request(clone);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08001205 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
1206 /* error the io and bail out, or requeue it if needed */
Stefan Bader9faf4002006-10-03 01:15:41 -07001207 md = tio->io->md;
1208 dec_pending(tio->io, r);
Stefan Bader9faf4002006-10-03 01:15:41 -07001209 free_tio(md, tio);
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -08001210 } else if (r) {
1211 DMWARN("unimplemented target map return value: %d", r);
1212 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
1214}
1215
1216struct clone_info {
1217 struct mapped_device *md;
1218 struct dm_table *map;
1219 struct bio *bio;
1220 struct dm_io *io;
1221 sector_t sector;
Mikulas Patockae0d66092014-03-14 18:40:39 -04001222 unsigned sector_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223};
1224
Mikulas Patockae0d66092014-03-14 18:40:39 -04001225static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001226{
Kent Overstreet4f024f32013-10-11 15:44:27 -07001227 bio->bi_iter.bi_sector = sector;
1228 bio->bi_iter.bi_size = to_bytes(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
1230
1231/*
1232 * Creates a bio that consists of range of complete bvecs.
1233 */
Mikulas Patockadba14162012-10-12 21:02:15 +01001234static void clone_bio(struct dm_target_io *tio, struct bio *bio,
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001235 sector_t sector, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
Mikulas Patockadba14162012-10-12 21:02:15 +01001237 struct bio *clone = &tio->clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001239 __bio_clone_fast(clone, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001241 if (bio_integrity(bio))
1242 bio_integrity_clone(clone, bio, GFP_NOIO);
1243
1244 bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
1245 clone->bi_iter.bi_size = to_bytes(len);
1246
1247 if (bio_integrity(bio))
1248 bio_integrity_trim(clone, 0, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001251static struct dm_target_io *alloc_tio(struct clone_info *ci,
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001252 struct dm_target *ti, int nr_iovecs,
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001253 unsigned target_bio_nr)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001254{
Mikulas Patockadba14162012-10-12 21:02:15 +01001255 struct dm_target_io *tio;
1256 struct bio *clone;
1257
1258 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, ci->md->bs);
1259 tio = container_of(clone, struct dm_target_io, clone);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001260
1261 tio->io = ci->io;
1262 tio->ti = ti;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001263 tio->target_bio_nr = target_bio_nr;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001264
1265 return tio;
1266}
1267
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001268static void __clone_and_map_simple_bio(struct clone_info *ci,
1269 struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001270 unsigned target_bio_nr, unsigned *len)
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001271{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001272 struct dm_target_io *tio = alloc_tio(ci, ti, ci->bio->bi_max_vecs, target_bio_nr);
Mikulas Patockadba14162012-10-12 21:02:15 +01001273 struct bio *clone = &tio->clone;
Alasdair G Kergon9015df22009-06-22 10:12:21 +01001274
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001275 tio->len_ptr = len;
1276
Mike Snitzer06a426c2010-08-12 04:14:09 +01001277 /*
1278 * Discard requests require the bio's inline iovecs be initialized.
1279 * ci->bio->bi_max_vecs is BIO_INLINE_VECS anyway, for both flush
1280 * and discard, so no need for concern about wasted bvec allocations.
1281 */
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001282 __bio_clone_fast(clone, ci->bio);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001283 if (len)
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001284 bio_setup_sector(clone, ci->sector, *len);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001285
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001286 __map_bio(tio);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001287}
1288
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001289static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001290 unsigned num_bios, unsigned *len)
Mike Snitzer06a426c2010-08-12 04:14:09 +01001291{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001292 unsigned target_bio_nr;
Mike Snitzer06a426c2010-08-12 04:14:09 +01001293
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001294 for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001295 __clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
Mike Snitzer06a426c2010-08-12 04:14:09 +01001296}
1297
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001298static int __send_empty_flush(struct clone_info *ci)
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001299{
Mike Snitzer06a426c2010-08-12 04:14:09 +01001300 unsigned target_nr = 0;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001301 struct dm_target *ti;
1302
Mike Snitzerb372d362010-09-08 18:07:01 +02001303 BUG_ON(bio_has_data(ci->bio));
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001304 while ((ti = dm_table_get_target(ci->map, target_nr++)))
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001305 __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001306
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +01001307 return 0;
1308}
1309
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001310static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001311 sector_t sector, unsigned *len)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001312{
Mikulas Patockadba14162012-10-12 21:02:15 +01001313 struct bio *bio = ci->bio;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001314 struct dm_target_io *tio;
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001315 unsigned target_bio_nr;
1316 unsigned num_target_bios = 1;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001317
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001318 /*
1319 * Does the target want to receive duplicate copies of the bio?
1320 */
1321 if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1322 num_target_bios = ti->num_write_bios(ti, bio);
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001323
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001324 for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001325 tio = alloc_tio(ci, ti, 0, target_bio_nr);
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001326 tio->len_ptr = len;
1327 clone_bio(tio, bio, sector, *len);
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +00001328 __map_bio(tio);
1329 }
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001330}
1331
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001332typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
Mike Snitzer23508a92012-12-21 20:23:37 +00001333
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001334static unsigned get_num_discard_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001335{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001336 return ti->num_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001337}
1338
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001339static unsigned get_num_write_same_bios(struct dm_target *ti)
Mike Snitzer23508a92012-12-21 20:23:37 +00001340{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001341 return ti->num_write_same_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001342}
1343
1344typedef bool (*is_split_required_fn)(struct dm_target *ti);
1345
1346static bool is_split_required_for_discard(struct dm_target *ti)
1347{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001348 return ti->split_discard_bios;
Mike Snitzer23508a92012-12-21 20:23:37 +00001349}
1350
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001351static int __send_changing_extent_only(struct clone_info *ci,
1352 get_num_bios_fn get_num_bios,
1353 is_split_required_fn is_split_required)
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001354{
1355 struct dm_target *ti;
Mikulas Patockae0d66092014-03-14 18:40:39 -04001356 unsigned len;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001357 unsigned num_bios;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001358
Mike Snitzera79245b2010-08-12 04:14:24 +01001359 do {
1360 ti = dm_table_find_target(ci->map, ci->sector);
1361 if (!dm_target_is_valid(ti))
1362 return -EIO;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001363
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001364 /*
Mike Snitzer23508a92012-12-21 20:23:37 +00001365 * Even though the device advertised support for this type of
1366 * request, that does not mean every target supports it, and
Mike Snitzer936688d2011-08-02 12:32:01 +01001367 * reconfiguration might also have changed that since the
Mike Snitzera79245b2010-08-12 04:14:24 +01001368 * check was performed.
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001369 */
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001370 num_bios = get_num_bios ? get_num_bios(ti) : 0;
1371 if (!num_bios)
Mike Snitzera79245b2010-08-12 04:14:24 +01001372 return -EOPNOTSUPP;
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001373
Mike Snitzer23508a92012-12-21 20:23:37 +00001374 if (is_split_required && !is_split_required(ti))
Mikulas Patockae0d66092014-03-14 18:40:39 -04001375 len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
Mikulas Patocka7acf0272012-07-27 15:08:03 +01001376 else
Mikulas Patockae0d66092014-03-14 18:40:39 -04001377 len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
Mike Snitzer06a426c2010-08-12 04:14:09 +01001378
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001379 __send_duplicate_bios(ci, ti, num_bios, &len);
Mike Snitzera79245b2010-08-12 04:14:24 +01001380
1381 ci->sector += len;
1382 } while (ci->sector_count -= len);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001383
1384 return 0;
1385}
1386
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001387static int __send_discard(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001388{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001389 return __send_changing_extent_only(ci, get_num_discard_bios,
1390 is_split_required_for_discard);
Mike Snitzer23508a92012-12-21 20:23:37 +00001391}
1392
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001393static int __send_write_same(struct clone_info *ci)
Mike Snitzer23508a92012-12-21 20:23:37 +00001394{
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001395 return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
Mike Snitzer23508a92012-12-21 20:23:37 +00001396}
1397
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001398/*
Alasdair G Kergone4c93812013-03-01 22:45:47 +00001399 * Select the correct strategy for processing a non-flush bio.
1400 */
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001401static int __split_and_process_non_flush(struct clone_info *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Mikulas Patockadba14162012-10-12 21:02:15 +01001403 struct bio *bio = ci->bio;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001404 struct dm_target *ti;
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001405 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001407 if (unlikely(bio->bi_rw & REQ_DISCARD))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001408 return __send_discard(ci);
Mike Snitzer23508a92012-12-21 20:23:37 +00001409 else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001410 return __send_write_same(ci);
Mike Snitzer5ae89a82010-08-12 04:14:08 +01001411
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001412 ti = dm_table_find_target(ci->map, ci->sector);
1413 if (!dm_target_is_valid(ti))
1414 return -EIO;
1415
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001416 len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001417
Mikulas Patocka1dd40c32014-03-14 18:41:24 -04001418 __clone_and_map_data_bio(ci, ti, ci->sector, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001420 ci->sector += len;
1421 ci->sector_count -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Kent Overstreet1c3b13e2013-10-29 17:17:49 -07001423 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
1425
1426/*
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001427 * Entry point to split a bio into clones and submit them to the targets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001429static void __split_and_process_bio(struct mapped_device *md,
1430 struct dm_table *map, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
1432 struct clone_info ci;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001433 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001435 if (unlikely(!map)) {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001436 bio_io_error(bio);
Mikulas Patockaf0b9a452009-04-02 19:55:38 +01001437 return;
1438 }
Mikulas Patocka692d0eb2009-04-09 00:27:13 +01001439
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001440 ci.map = map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 ci.md = md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 ci.io = alloc_io(md);
1443 ci.io->error = 0;
1444 atomic_set(&ci.io->io_count, 1);
1445 ci.io->bio = bio;
1446 ci.io->md = md;
Kiyoshi Uedaf88fb982009-10-16 23:18:15 +01001447 spin_lock_init(&ci.io->endio_lock);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001448 ci.sector = bio->bi_iter.bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001450 start_io_acct(ci.io);
Alasdair G Kergonbd2a49b2013-03-01 22:45:46 +00001451
Mike Snitzerb372d362010-09-08 18:07:01 +02001452 if (bio->bi_rw & REQ_FLUSH) {
1453 ci.bio = &ci.md->flush_bio;
1454 ci.sector_count = 0;
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001455 error = __send_empty_flush(&ci);
Mike Snitzerb372d362010-09-08 18:07:01 +02001456 /* dec_pending submits any data associated with flush */
1457 } else {
Tejun Heo6a8736d2010-09-08 18:07:00 +02001458 ci.bio = bio;
Tejun Heod87f4c12010-09-03 11:56:19 +02001459 ci.sector_count = bio_sectors(bio);
Mike Snitzerb372d362010-09-08 18:07:01 +02001460 while (ci.sector_count && !error)
Alasdair G Kergon14fe5942013-03-01 22:45:47 +00001461 error = __split_and_process_non_flush(&ci);
Tejun Heod87f4c12010-09-03 11:56:19 +02001462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
1464 /* drop the extra reference count */
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001465 dec_pending(ci.io, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466}
1467/*-----------------------------------------------------------------
1468 * CRUD END
1469 *---------------------------------------------------------------*/
1470
Milan Brozf6fccb12008-07-21 12:00:37 +01001471static int dm_merge_bvec(struct request_queue *q,
1472 struct bvec_merge_data *bvm,
1473 struct bio_vec *biovec)
1474{
1475 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001476 struct dm_table *map = dm_get_live_table_fast(md);
Milan Brozf6fccb12008-07-21 12:00:37 +01001477 struct dm_target *ti;
1478 sector_t max_sectors;
Mikulas Patocka50371082008-10-01 14:39:17 +01001479 int max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001480
1481 if (unlikely(!map))
Mikulas Patocka50371082008-10-01 14:39:17 +01001482 goto out;
Milan Brozf6fccb12008-07-21 12:00:37 +01001483
1484 ti = dm_table_find_target(map, bvm->bi_sector);
Mikulas Patockab01cd5a2008-10-01 14:39:24 +01001485 if (!dm_target_is_valid(ti))
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001486 goto out;
Milan Brozf6fccb12008-07-21 12:00:37 +01001487
1488 /*
1489 * Find maximum amount of I/O that won't need splitting
1490 */
Mike Snitzer56a67df2010-08-12 04:14:10 +01001491 max_sectors = min(max_io_len(bvm->bi_sector, ti),
Milan Brozf6fccb12008-07-21 12:00:37 +01001492 (sector_t) BIO_MAX_SECTORS);
1493 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1494 if (max_size < 0)
1495 max_size = 0;
1496
1497 /*
1498 * merge_bvec_fn() returns number of bytes
1499 * it can accept at this offset
1500 * max is precomputed maximal io size
1501 */
1502 if (max_size && ti->type->merge)
1503 max_size = ti->type->merge(ti, bvm, biovec, max_size);
Mikulas Patocka8cbeb672009-06-22 10:12:14 +01001504 /*
1505 * If the target doesn't support merge method and some of the devices
1506 * provided their merge_bvec method (we know this by looking at
1507 * queue_max_hw_sectors), then we can't allow bios with multiple vector
1508 * entries. So always set max_size to 0, and the code below allows
1509 * just one page.
1510 */
1511 else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
Mikulas Patocka8cbeb672009-06-22 10:12:14 +01001512 max_size = 0;
Milan Brozf6fccb12008-07-21 12:00:37 +01001513
Mikulas Patocka50371082008-10-01 14:39:17 +01001514out:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001515 dm_put_live_table_fast(md);
Milan Brozf6fccb12008-07-21 12:00:37 +01001516 /*
1517 * Always allow an entire first page
1518 */
1519 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1520 max_size = biovec->bv_len;
1521
Milan Brozf6fccb12008-07-21 12:00:37 +01001522 return max_size;
1523}
1524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525/*
1526 * The request function that just remaps the bio built up by
1527 * dm_merge_bvec.
1528 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001529static void _dm_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
Kevin Corry12f03a42006-02-01 03:04:52 -08001531 int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 struct mapped_device *md = q->queuedata;
Tejun Heoc9959052008-08-25 19:47:21 +09001533 int cpu;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001534 int srcu_idx;
1535 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001537 map = dm_get_live_table(md, &srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Tejun Heo074a7ac2008-08-25 19:56:14 +09001539 cpu = part_stat_lock();
1540 part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1541 part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1542 part_stat_unlock();
Kevin Corry12f03a42006-02-01 03:04:52 -08001543
Tejun Heo6a8736d2010-09-08 18:07:00 +02001544 /* if we're suspended, we have to queue this io for later */
1545 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001546 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Tejun Heo6a8736d2010-09-08 18:07:00 +02001548 if (bio_rw(bio) != READA)
1549 queue_io(md, bio);
1550 else
Alasdair G Kergon54d9a1b2009-04-09 00:27:14 +01001551 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001552 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 }
1554
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001555 __split_and_process_bio(md, map, bio);
1556 dm_put_live_table(md, srcu_idx);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001557 return;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001558}
1559
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001560int dm_request_based(struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001561{
1562 return blk_queue_stackable(md->queue);
1563}
1564
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001565static void dm_request(struct request_queue *q, struct bio *bio)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001566{
1567 struct mapped_device *md = q->queuedata;
1568
1569 if (dm_request_based(md))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001570 blk_queue_bio(q, bio);
1571 else
1572 _dm_request(q, bio);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001573}
1574
1575void dm_dispatch_request(struct request *rq)
1576{
1577 int r;
1578
1579 if (blk_queue_io_stat(rq->q))
1580 rq->cmd_flags |= REQ_IO_STAT;
1581
1582 rq->start_time = jiffies;
1583 r = blk_insert_cloned_request(rq->q, rq);
1584 if (r)
1585 dm_complete_request(rq, r);
1586}
1587EXPORT_SYMBOL_GPL(dm_dispatch_request);
1588
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001589static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1590 void *data)
1591{
1592 struct dm_rq_target_io *tio = data;
Kent Overstreet94818742012-09-07 13:44:01 -07001593 struct dm_rq_clone_bio_info *info =
1594 container_of(bio, struct dm_rq_clone_bio_info, clone);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001595
1596 info->orig = bio_orig;
1597 info->tio = tio;
1598 bio->bi_end_io = end_clone_bio;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001599
1600 return 0;
1601}
1602
1603static int setup_clone(struct request *clone, struct request *rq,
1604 struct dm_rq_target_io *tio)
1605{
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001606 int r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001607
Tejun Heo29e40132010-09-08 18:07:00 +02001608 r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1609 dm_rq_bio_constructor, tio);
1610 if (r)
1611 return r;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001612
Tejun Heo29e40132010-09-08 18:07:00 +02001613 clone->cmd = rq->cmd;
1614 clone->cmd_len = rq->cmd_len;
1615 clone->sense = rq->sense;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001616 clone->end_io = end_clone_request;
1617 clone->end_io_data = tio;
1618
1619 return 0;
1620}
1621
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001622static struct request *clone_rq(struct request *rq, struct mapped_device *md,
1623 gfp_t gfp_mask)
1624{
1625 struct request *clone;
1626 struct dm_rq_target_io *tio;
1627
1628 tio = alloc_rq_tio(md, gfp_mask);
1629 if (!tio)
1630 return NULL;
1631
1632 tio->md = md;
1633 tio->ti = NULL;
1634 tio->orig = rq;
1635 tio->error = 0;
1636 memset(&tio->info, 0, sizeof(tio->info));
1637
1638 clone = &tio->clone;
1639 if (setup_clone(clone, rq, tio)) {
1640 /* -ENOMEM */
1641 free_rq_tio(tio);
1642 return NULL;
1643 }
1644
1645 return clone;
1646}
1647
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001648/*
1649 * Called with the queue lock held.
1650 */
1651static int dm_prep_fn(struct request_queue *q, struct request *rq)
1652{
1653 struct mapped_device *md = q->queuedata;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001654 struct request *clone;
1655
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001656 if (unlikely(rq->special)) {
1657 DMWARN("Already has something in rq->special.");
1658 return BLKPREP_KILL;
1659 }
1660
Kiyoshi Ueda6facdaf2009-12-10 23:52:15 +00001661 clone = clone_rq(rq, md, GFP_ATOMIC);
1662 if (!clone)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001663 return BLKPREP_DEFER;
1664
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001665 rq->special = clone;
1666 rq->cmd_flags |= REQ_DONTPREP;
1667
1668 return BLKPREP_OK;
1669}
1670
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001671/*
1672 * Returns:
1673 * 0 : the request has been processed (not requeued)
1674 * !0 : the request has been requeued
1675 */
1676static int map_request(struct dm_target *ti, struct request *clone,
1677 struct mapped_device *md)
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001678{
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001679 int r, requeued = 0;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001680 struct dm_rq_target_io *tio = clone->end_io_data;
1681
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001682 tio->ti = ti;
1683 r = ti->type->map_rq(ti, clone, &tio->info);
1684 switch (r) {
1685 case DM_MAPIO_SUBMITTED:
1686 /* The target has taken the I/O to submit by itself later */
1687 break;
1688 case DM_MAPIO_REMAPPED:
1689 /* The target has remapped the I/O so dispatch it */
Jun'ichi Nomura6db4ccd2009-12-10 23:52:25 +00001690 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
1691 blk_rq_pos(tio->orig));
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001692 dm_dispatch_request(clone);
1693 break;
1694 case DM_MAPIO_REQUEUE:
1695 /* The target wants to requeue the I/O */
1696 dm_requeue_unmapped_request(clone);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001697 requeued = 1;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001698 break;
1699 default:
1700 if (r > 0) {
1701 DMWARN("unimplemented target map return value: %d", r);
1702 BUG();
1703 }
1704
1705 /* The target wants to complete the I/O */
1706 dm_kill_unmapped_request(clone, r);
1707 break;
1708 }
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001709
1710 return requeued;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001711}
1712
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001713static struct request *dm_start_request(struct mapped_device *md, struct request *orig)
1714{
1715 struct request *clone;
1716
1717 blk_start_request(orig);
1718 clone = orig->special;
1719 atomic_inc(&md->pending[rq_data_dir(clone)]);
1720
1721 /*
1722 * Hold the md reference here for the in-flight I/O.
1723 * We can't rely on the reference count by device opener,
1724 * because the device may be closed during the request completion
1725 * when all bios are completed.
1726 * See the comment in rq_completed() too.
1727 */
1728 dm_get(md);
1729
1730 return clone;
1731}
1732
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001733/*
1734 * q->request_fn for request-based dm.
1735 * Called with the queue lock held.
1736 */
1737static void dm_request_fn(struct request_queue *q)
1738{
1739 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001740 int srcu_idx;
1741 struct dm_table *map = dm_get_live_table(md, &srcu_idx);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001742 struct dm_target *ti;
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001743 struct request *rq, *clone;
Tejun Heo29e40132010-09-08 18:07:00 +02001744 sector_t pos;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001745
1746 /*
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001747 * For suspend, check blk_queue_stopped() and increment
1748 * ->pending within a single queue_lock not to increment the
1749 * number of in-flight I/Os after the queue is stopped in
1750 * dm_suspend().
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001751 */
Jens Axboe7eaceac2011-03-10 08:52:07 +01001752 while (!blk_queue_stopped(q)) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001753 rq = blk_peek_request(q);
1754 if (!rq)
Jens Axboe7eaceac2011-03-10 08:52:07 +01001755 goto delay_and_out;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001756
Tejun Heo29e40132010-09-08 18:07:00 +02001757 /* always use block 0 to find the target for flushes for now */
1758 pos = 0;
1759 if (!(rq->cmd_flags & REQ_FLUSH))
1760 pos = blk_rq_pos(rq);
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00001761
Tejun Heo29e40132010-09-08 18:07:00 +02001762 ti = dm_table_find_target(map, pos);
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001763 if (!dm_target_is_valid(ti)) {
1764 /*
1765 * Must perform setup, that dm_done() requires,
1766 * before calling dm_kill_unmapped_request
1767 */
1768 DMERR_LIMIT("request attempted access beyond the end of device");
1769 clone = dm_start_request(md, rq);
1770 dm_kill_unmapped_request(clone, -EIO);
1771 continue;
1772 }
Tejun Heo29e40132010-09-08 18:07:00 +02001773
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001774 if (ti->type->busy && ti->type->busy(ti))
Jens Axboe7eaceac2011-03-10 08:52:07 +01001775 goto delay_and_out;
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001776
Mike Snitzerba1cbad2012-09-26 23:45:42 +01001777 clone = dm_start_request(md, rq);
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00001778
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001779 spin_unlock(q->queue_lock);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001780 if (map_request(ti, clone, md))
1781 goto requeued;
1782
Kiyoshi Ueda052189a2011-01-13 20:00:00 +00001783 BUG_ON(!irqs_disabled());
1784 spin_lock(q->queue_lock);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001785 }
1786
1787 goto out;
1788
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001789requeued:
Kiyoshi Ueda052189a2011-01-13 20:00:00 +00001790 BUG_ON(!irqs_disabled());
1791 spin_lock(q->queue_lock);
Kiyoshi Ueda9eef87d2010-02-16 18:43:01 +00001792
Jens Axboe7eaceac2011-03-10 08:52:07 +01001793delay_and_out:
1794 blk_delay_queue(q, HZ / 10);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001795out:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001796 dm_put_live_table(md, srcu_idx);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001797}
1798
1799int dm_underlying_device_busy(struct request_queue *q)
1800{
1801 return blk_lld_busy(q);
1802}
1803EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1804
1805static int dm_lld_busy(struct request_queue *q)
1806{
1807 int r;
1808 struct mapped_device *md = q->queuedata;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001809 struct dm_table *map = dm_get_live_table_fast(md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001810
1811 if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1812 r = 1;
1813 else
1814 r = dm_table_any_busy_target(map);
1815
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001816 dm_put_live_table_fast(md);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001817
1818 return r;
1819}
1820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821static int dm_any_congested(void *congested_data, int bdi_bits)
1822{
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001823 int r = bdi_bits;
1824 struct mapped_device *md = congested_data;
1825 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01001827 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001828 map = dm_get_live_table_fast(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001829 if (map) {
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01001830 /*
1831 * Request-based dm cares about only own queue for
1832 * the query about congestion status of request_queue
1833 */
1834 if (dm_request_based(md))
1835 r = md->queue->backing_dev_info.state &
1836 bdi_bits;
1837 else
1838 r = dm_table_any_congested(map, bdi_bits);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001839 }
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001840 dm_put_live_table_fast(md);
Chandra Seetharaman8a57dfc2008-11-13 23:39:14 +00001841 }
1842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 return r;
1844}
1845
1846/*-----------------------------------------------------------------
1847 * An IDR is used to keep track of allocated minor numbers.
1848 *---------------------------------------------------------------*/
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001849static void free_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001851 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 idr_remove(&_minor_idr, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001853 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854}
1855
1856/*
1857 * See if the device with a specific minor # is free.
1858 */
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001859static int specific_minor(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
Tejun Heoc9d76be2013-02-27 17:04:26 -08001861 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 if (minor >= (1 << MINORBITS))
1864 return -EINVAL;
1865
Tejun Heoc9d76be2013-02-27 17:04:26 -08001866 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001867 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Tejun Heoc9d76be2013-02-27 17:04:26 -08001869 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001871 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08001872 idr_preload_end();
1873 if (r < 0)
1874 return r == -ENOSPC ? -EBUSY : r;
1875 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876}
1877
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001878static int next_free_minor(int *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
Tejun Heoc9d76be2013-02-27 17:04:26 -08001880 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Tejun Heoc9d76be2013-02-27 17:04:26 -08001882 idr_preload(GFP_KERNEL);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001883 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Tejun Heoc9d76be2013-02-27 17:04:26 -08001885 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07001887 spin_unlock(&_minor_lock);
Tejun Heoc9d76be2013-02-27 17:04:26 -08001888 idr_preload_end();
1889 if (r < 0)
1890 return r;
1891 *minor = r;
1892 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
1894
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001895static const struct block_device_operations dm_blk_dops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Mikulas Patocka53d59142009-04-02 19:55:37 +01001897static void dm_wq_work(struct work_struct *work);
1898
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001899static void dm_init_md_queue(struct mapped_device *md)
1900{
1901 /*
1902 * Request-based dm devices cannot be stacked on top of bio-based dm
1903 * devices. The type of this dm device has not been decided yet.
1904 * The type is decided at the first table loading time.
1905 * To prevent problematic device stacking, clear the queue flag
1906 * for request stacking support until then.
1907 *
1908 * This queue is new, so no concurrency on the queue_flags.
1909 */
1910 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1911
1912 md->queue->queuedata = md;
1913 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1914 md->queue->backing_dev_info.congested_data = md;
1915 blk_queue_make_request(md->queue, dm_request);
1916 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001917 blk_queue_merge_bvec(md->queue, dm_merge_bvec);
1918}
1919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920/*
1921 * Allocate and initialise a blank device with a given minor.
1922 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001923static struct mapped_device *alloc_dev(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
1925 int r;
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001926 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07001927 void *old_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
1929 if (!md) {
1930 DMWARN("unable to allocate device, out of memory.");
1931 return NULL;
1932 }
1933
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001934 if (!try_module_get(THIS_MODULE))
Milan Broz6ed7ade2008-02-08 02:10:19 +00001935 goto bad_module_get;
Jeff Mahoney10da4f72006-06-26 00:27:25 -07001936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 /* get a minor number for the dev */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001938 if (minor == DM_ANY_MINOR)
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001939 r = next_free_minor(&minor);
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07001940 else
Frederik Deweerdtcf13ab82008-04-24 22:10:59 +01001941 r = specific_minor(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 if (r < 0)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001943 goto bad_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001945 r = init_srcu_struct(&md->io_barrier);
1946 if (r < 0)
1947 goto bad_io_barrier;
1948
Mike Snitzera5664da2010-08-12 04:14:01 +01001949 md->type = DM_TYPE_NONE;
Daniel Walkere61290a2008-02-08 02:10:08 +00001950 mutex_init(&md->suspend_lock);
Mike Snitzera5664da2010-08-12 04:14:01 +01001951 mutex_init(&md->type_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01001952 spin_lock_init(&md->deferred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 atomic_set(&md->holders, 1);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07001954 atomic_set(&md->open_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 atomic_set(&md->event_nr, 0);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01001956 atomic_set(&md->uevent_seq, 0);
1957 INIT_LIST_HEAD(&md->uevent_list);
1958 spin_lock_init(&md->uevent_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001960 md->queue = blk_alloc_queue(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 if (!md->queue)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001962 goto bad_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001964 dm_init_md_queue(md);
Stefan Bader9faf4002006-10-03 01:15:41 -07001965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 md->disk = alloc_disk(1);
1967 if (!md->disk)
Milan Broz6ed7ade2008-02-08 02:10:19 +00001968 goto bad_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001970 atomic_set(&md->pending[0], 0);
1971 atomic_set(&md->pending[1], 0);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001972 init_waitqueue_head(&md->wait);
Mikulas Patocka53d59142009-04-02 19:55:37 +01001973 INIT_WORK(&md->work, dm_wq_work);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001974 init_waitqueue_head(&md->eventq);
Mikulas Patocka2995fa72014-01-13 19:37:54 -05001975 init_completion(&md->kobj_holder.completion);
Jeff Mahoneyf0b04112006-06-26 00:27:25 -07001976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 md->disk->major = _major;
1978 md->disk->first_minor = minor;
1979 md->disk->fops = &dm_blk_dops;
1980 md->disk->queue = md->queue;
1981 md->disk->private_data = md;
1982 sprintf(md->disk->disk_name, "dm-%d", minor);
1983 add_disk(md->disk);
Mike Anderson7e51f252006-03-27 01:17:52 -08001984 format_dev_t(md->name, MKDEV(_major, minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Tejun Heo670368a2013-07-30 08:40:21 -04001986 md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
Milan Broz304f3f62008-02-08 02:11:17 +00001987 if (!md->wq)
1988 goto bad_thread;
1989
Mikulas Patocka32a926d2009-06-22 10:12:17 +01001990 md->bdev = bdget_disk(md->disk, 0);
1991 if (!md->bdev)
1992 goto bad_bdev;
1993
Tejun Heo6a8736d2010-09-08 18:07:00 +02001994 bio_init(&md->flush_bio);
1995 md->flush_bio.bi_bdev = md->bdev;
1996 md->flush_bio.bi_rw = WRITE_FLUSH;
1997
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001998 dm_stats_init(&md->stats);
1999
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002000 /* Populate the mapping, nobody knows we exist yet */
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002001 spin_lock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002002 old_md = idr_replace(&_minor_idr, md, minor);
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002003 spin_unlock(&_minor_lock);
Jeff Mahoneyba61fdd2006-06-26 00:27:21 -07002004
2005 BUG_ON(old_md != MINOR_ALLOCED);
2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 return md;
2008
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002009bad_bdev:
2010 destroy_workqueue(md->wq);
Milan Broz304f3f62008-02-08 02:11:17 +00002011bad_thread:
Zdenek Kabelac03022c52009-10-16 23:18:15 +01002012 del_gendisk(md->disk);
Milan Broz304f3f62008-02-08 02:11:17 +00002013 put_disk(md->disk);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002014bad_disk:
Al Viro1312f402006-03-12 11:02:03 -05002015 blk_cleanup_queue(md->queue);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002016bad_queue:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002017 cleanup_srcu_struct(&md->io_barrier);
2018bad_io_barrier:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 free_minor(minor);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002020bad_minor:
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002021 module_put(THIS_MODULE);
Milan Broz6ed7ade2008-02-08 02:10:19 +00002022bad_module_get:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 kfree(md);
2024 return NULL;
2025}
2026
Jun'ichi Nomuraae9da832007-10-19 22:38:43 +01002027static void unlock_fs(struct mapped_device *md);
2028
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029static void free_dev(struct mapped_device *md)
2030{
Tejun Heof331c022008-09-03 09:01:48 +02002031 int minor = MINOR(disk_devt(md->disk));
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08002032
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002033 unlock_fs(md);
2034 bdput(md->bdev);
Milan Broz304f3f62008-02-08 02:11:17 +00002035 destroy_workqueue(md->wq);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002036 if (md->io_pool)
2037 mempool_destroy(md->io_pool);
2038 if (md->bs)
2039 bioset_free(md->bs);
Martin K. Petersen9c470082009-04-09 00:27:12 +01002040 blk_integrity_unregister(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 del_gendisk(md->disk);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002042 cleanup_srcu_struct(&md->io_barrier);
Jun'ichi Nomura63d94e42006-02-24 13:04:25 -08002043 free_minor(minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002044
2045 spin_lock(&_minor_lock);
2046 md->disk->private_data = NULL;
2047 spin_unlock(&_minor_lock);
2048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 put_disk(md->disk);
Al Viro1312f402006-03-12 11:02:03 -05002050 blk_cleanup_queue(md->queue);
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002051 dm_stats_cleanup(&md->stats);
Jeff Mahoney10da4f72006-06-26 00:27:25 -07002052 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 kfree(md);
2054}
2055
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002056static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
2057{
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002058 struct dm_md_mempools *p = dm_table_get_md_mempools(t);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002059
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002060 if (md->io_pool && md->bs) {
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00002061 /* The md already has necessary mempools. */
2062 if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
2063 /*
2064 * Reload bioset because front_pad may have changed
2065 * because a different table was loaded.
2066 */
2067 bioset_free(md->bs);
2068 md->bs = p->bs;
2069 p->bs = NULL;
2070 } else if (dm_table_get_type(t) == DM_TYPE_REQUEST_BASED) {
Jun'ichi Nomura16245bd2013-03-01 22:45:44 +00002071 /*
2072 * There's no need to reload with request-based dm
2073 * because the size of front_pad doesn't change.
2074 * Note for future: If you are to reload bioset,
2075 * prep-ed requests in the queue may refer
2076 * to bio from the old bioset, so you must walk
2077 * through the queue to unprep.
2078 */
2079 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002080 goto out;
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002081 }
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002082
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002083 BUG_ON(!p || md->io_pool || md->bs);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002084
2085 md->io_pool = p->io_pool;
2086 p->io_pool = NULL;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002087 md->bs = p->bs;
2088 p->bs = NULL;
2089
2090out:
2091 /* mempool bind completed, now no need any mempools in the table */
2092 dm_table_free_md_mempools(t);
2093}
2094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095/*
2096 * Bind a table to the device.
2097 */
2098static void event_callback(void *context)
2099{
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002100 unsigned long flags;
2101 LIST_HEAD(uevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 struct mapped_device *md = (struct mapped_device *) context;
2103
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002104 spin_lock_irqsave(&md->uevent_lock, flags);
2105 list_splice_init(&md->uevent_list, &uevents);
2106 spin_unlock_irqrestore(&md->uevent_lock, flags);
2107
Tejun Heoed9e1982008-08-25 19:56:05 +09002108 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 atomic_inc(&md->event_nr);
2111 wake_up(&md->eventq);
2112}
2113
Mike Snitzerc2176492011-01-13 19:53:46 +00002114/*
2115 * Protected by md->suspend_lock obtained by dm_swap_table().
2116 */
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002117static void __set_size(struct mapped_device *md, sector_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
Alasdair G Kergon4e90188be2005-07-28 21:15:59 -07002119 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002121 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122}
2123
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002124/*
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01002125 * Return 1 if the queue has a compulsory merge_bvec_fn function.
2126 *
2127 * If this function returns 0, then the device is either a non-dm
2128 * device without a merge_bvec_fn, or it is a dm device that is
2129 * able to split any bios it receives that are too big.
2130 */
2131int dm_queue_merge_is_compulsory(struct request_queue *q)
2132{
2133 struct mapped_device *dev_md;
2134
2135 if (!q->merge_bvec_fn)
2136 return 0;
2137
2138 if (q->make_request_fn == dm_request) {
2139 dev_md = q->queuedata;
2140 if (test_bit(DMF_MERGE_IS_OPTIONAL, &dev_md->flags))
2141 return 0;
2142 }
2143
2144 return 1;
2145}
2146
2147static int dm_device_merge_is_compulsory(struct dm_target *ti,
2148 struct dm_dev *dev, sector_t start,
2149 sector_t len, void *data)
2150{
2151 struct block_device *bdev = dev->bdev;
2152 struct request_queue *q = bdev_get_queue(bdev);
2153
2154 return dm_queue_merge_is_compulsory(q);
2155}
2156
2157/*
2158 * Return 1 if it is acceptable to ignore merge_bvec_fn based
2159 * on the properties of the underlying devices.
2160 */
2161static int dm_table_merge_is_optional(struct dm_table *table)
2162{
2163 unsigned i = 0;
2164 struct dm_target *ti;
2165
2166 while (i < dm_table_get_num_targets(table)) {
2167 ti = dm_table_get_target(table, i++);
2168
2169 if (ti->type->iterate_devices &&
2170 ti->type->iterate_devices(ti, dm_device_merge_is_compulsory, NULL))
2171 return 0;
2172 }
2173
2174 return 1;
2175}
2176
2177/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002178 * Returns old map, which caller must destroy.
2179 */
2180static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2181 struct queue_limits *limits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002183 struct dm_table *old_map;
Jens Axboe165125e2007-07-24 09:28:11 +02002184 struct request_queue *q = md->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 sector_t size;
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01002186 int merge_is_optional;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188 size = dm_table_get_size(t);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002189
2190 /*
2191 * Wipe any geometry if the size of the table changed.
2192 */
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002193 if (size != dm_get_size(md))
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002194 memset(&md->geometry, 0, sizeof(md->geometry));
2195
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002196 __set_size(md, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002198 dm_table_event_callback(t, event_callback, md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002199
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002200 /*
2201 * The queue hasn't been stopped yet, if the old table type wasn't
2202 * for request-based during suspension. So stop it to prevent
2203 * I/O mapping before resume.
2204 * This must be done before setting the queue restrictions,
2205 * because request-based dm may be run just after the setting.
2206 */
2207 if (dm_table_request_based(t) && !blk_queue_stopped(q))
2208 stop_queue(q);
2209
2210 __bind_mempools(md, t);
2211
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01002212 merge_is_optional = dm_table_merge_is_optional(t);
2213
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002214 old_map = md->map;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002215 rcu_assign_pointer(md->map, t);
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002216 md->immutable_target_type = dm_table_get_immutable_target_type(t);
2217
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002218 dm_table_set_restrictions(t, q, limits);
Mikulas Patockad5b9dd02011-08-02 12:32:04 +01002219 if (merge_is_optional)
2220 set_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
2221 else
2222 clear_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002223 dm_sync_table(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002224
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002225 return old_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226}
2227
Alasdair G Kergona7940152009-12-10 23:52:23 +00002228/*
2229 * Returns unbound table for the caller to free.
2230 */
2231static struct dm_table *__unbind(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232{
2233 struct dm_table *map = md->map;
2234
2235 if (!map)
Alasdair G Kergona7940152009-12-10 23:52:23 +00002236 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
2238 dm_table_event_callback(map, NULL, NULL);
Monam Agarwal9cdb8522014-03-23 23:58:27 +05302239 RCU_INIT_POINTER(md->map, NULL);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002240 dm_sync_table(md);
Alasdair G Kergona7940152009-12-10 23:52:23 +00002241
2242 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243}
2244
2245/*
2246 * Constructor for a new device.
2247 */
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002248int dm_create(int minor, struct mapped_device **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
2250 struct mapped_device *md;
2251
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07002252 md = alloc_dev(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 if (!md)
2254 return -ENXIO;
2255
Milan Broz784aae72009-01-06 03:05:12 +00002256 dm_sysfs_init(md);
2257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 *result = md;
2259 return 0;
2260}
2261
Mike Snitzera5664da2010-08-12 04:14:01 +01002262/*
2263 * Functions to manage md->type.
2264 * All are required to hold md->type_lock.
2265 */
2266void dm_lock_md_type(struct mapped_device *md)
2267{
2268 mutex_lock(&md->type_lock);
2269}
2270
2271void dm_unlock_md_type(struct mapped_device *md)
2272{
2273 mutex_unlock(&md->type_lock);
2274}
2275
2276void dm_set_md_type(struct mapped_device *md, unsigned type)
2277{
Mike Snitzer00c4fc32013-08-27 18:57:03 -04002278 BUG_ON(!mutex_is_locked(&md->type_lock));
Mike Snitzera5664da2010-08-12 04:14:01 +01002279 md->type = type;
2280}
2281
2282unsigned dm_get_md_type(struct mapped_device *md)
2283{
Mike Snitzer00c4fc32013-08-27 18:57:03 -04002284 BUG_ON(!mutex_is_locked(&md->type_lock));
Mike Snitzera5664da2010-08-12 04:14:01 +01002285 return md->type;
2286}
2287
Alasdair G Kergon36a04562011-10-31 20:19:04 +00002288struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
2289{
2290 return md->immutable_target_type;
2291}
2292
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002293/*
Mike Snitzerf84cb8a2013-09-19 12:13:58 -04002294 * The queue_limits are only valid as long as you have a reference
2295 * count on 'md'.
2296 */
2297struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2298{
2299 BUG_ON(!atomic_read(&md->holders));
2300 return &md->queue->limits;
2301}
2302EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2303
2304/*
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002305 * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
2306 */
2307static int dm_init_request_based_queue(struct mapped_device *md)
2308{
2309 struct request_queue *q = NULL;
2310
2311 if (md->queue->elevator)
2312 return 1;
2313
2314 /* Fully initialize the queue */
2315 q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
2316 if (!q)
2317 return 0;
2318
2319 md->queue = q;
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002320 dm_init_md_queue(md);
2321 blk_queue_softirq_done(md->queue, dm_softirq_done);
2322 blk_queue_prep_rq(md->queue, dm_prep_fn);
2323 blk_queue_lld_busy(md->queue, dm_lld_busy);
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01002324
2325 elv_register_queue(md->queue);
2326
2327 return 1;
2328}
2329
2330/*
2331 * Setup the DM device's queue based on md's type
2332 */
2333int dm_setup_md_queue(struct mapped_device *md)
2334{
2335 if ((dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) &&
2336 !dm_init_request_based_queue(md)) {
2337 DMWARN("Cannot initialize queue for request-based mapped device");
2338 return -EINVAL;
2339 }
2340
2341 return 0;
2342}
2343
David Teigland637842c2006-01-06 00:20:00 -08002344static struct mapped_device *dm_find_md(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
2346 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 unsigned minor = MINOR(dev);
2348
2349 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2350 return NULL;
2351
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002352 spin_lock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
2354 md = idr_find(&_minor_idr, minor);
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002355 if (md && (md == MINOR_ALLOCED ||
Tejun Heof331c022008-09-03 09:01:48 +02002356 (MINOR(disk_devt(dm_disk(md))) != minor) ||
Kiyoshi Uedaabdc5682010-08-12 04:13:54 +01002357 dm_deleting_md(md) ||
Alasdair G Kergon17b2f662006-06-26 00:27:33 -07002358 test_bit(DMF_FREEING, &md->flags))) {
David Teigland637842c2006-01-06 00:20:00 -08002359 md = NULL;
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002360 goto out;
2361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002363out:
Jeff Mahoneyf32c10b2006-06-26 00:27:22 -07002364 spin_unlock(&_minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
David Teigland637842c2006-01-06 00:20:00 -08002366 return md;
2367}
2368
David Teiglandd229a952006-01-06 00:20:01 -08002369struct mapped_device *dm_get_md(dev_t dev)
2370{
2371 struct mapped_device *md = dm_find_md(dev);
2372
2373 if (md)
2374 dm_get(md);
2375
2376 return md;
2377}
Alasdair G Kergon3cf2e4b2011-10-31 20:19:06 +00002378EXPORT_SYMBOL_GPL(dm_get_md);
David Teiglandd229a952006-01-06 00:20:01 -08002379
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002380void *dm_get_mdptr(struct mapped_device *md)
David Teigland637842c2006-01-06 00:20:00 -08002381{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -08002382 return md->interface_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383}
2384
2385void dm_set_mdptr(struct mapped_device *md, void *ptr)
2386{
2387 md->interface_ptr = ptr;
2388}
2389
2390void dm_get(struct mapped_device *md)
2391{
2392 atomic_inc(&md->holders);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002393 BUG_ON(test_bit(DMF_FREEING, &md->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394}
2395
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002396const char *dm_device_name(struct mapped_device *md)
2397{
2398 return md->name;
2399}
2400EXPORT_SYMBOL_GPL(dm_device_name);
2401
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002402static void __dm_destroy(struct mapped_device *md, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403{
Mike Anderson1134e5a2006-03-27 01:17:54 -08002404 struct dm_table *map;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002405 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002407 might_sleep();
Jeff Mahoneyfba9f902006-06-26 00:27:23 -07002408
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002409 spin_lock(&_minor_lock);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002410 map = dm_get_live_table(md, &srcu_idx);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002411 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2412 set_bit(DMF_FREEING, &md->flags);
2413 spin_unlock(&_minor_lock);
2414
2415 if (!dm_suspended_md(md)) {
2416 dm_table_presuspend_targets(map);
2417 dm_table_postsuspend_targets(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 }
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002419
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002420 /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
2421 dm_put_live_table(md, srcu_idx);
2422
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002423 /*
2424 * Rare, but there may be I/O requests still going to complete,
2425 * for example. Wait for all references to disappear.
2426 * No one should increment the reference count of the mapped_device,
2427 * after the mapped_device state becomes DMF_FREEING.
2428 */
2429 if (wait)
2430 while (atomic_read(&md->holders))
2431 msleep(1);
2432 else if (atomic_read(&md->holders))
2433 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2434 dm_device_name(md), atomic_read(&md->holders));
2435
2436 dm_sysfs_exit(md);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01002437 dm_table_destroy(__unbind(md));
2438 free_dev(md);
2439}
2440
2441void dm_destroy(struct mapped_device *md)
2442{
2443 __dm_destroy(md, true);
2444}
2445
2446void dm_destroy_immediate(struct mapped_device *md)
2447{
2448 __dm_destroy(md, false);
2449}
2450
2451void dm_put(struct mapped_device *md)
2452{
2453 atomic_dec(&md->holders);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454}
Edward Goggin79eb8852007-05-09 02:32:56 -07002455EXPORT_SYMBOL_GPL(dm_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Mikulas Patocka401600d2009-04-02 19:55:38 +01002457static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
Milan Broz46125c12008-02-08 02:10:30 +00002458{
2459 int r = 0;
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002460 DECLARE_WAITQUEUE(wait, current);
2461
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002462 add_wait_queue(&md->wait, &wait);
Milan Broz46125c12008-02-08 02:10:30 +00002463
2464 while (1) {
Mikulas Patocka401600d2009-04-02 19:55:38 +01002465 set_current_state(interruptible);
Milan Broz46125c12008-02-08 02:10:30 +00002466
Kiyoshi Uedab4324fe2009-12-10 23:52:16 +00002467 if (!md_in_flight(md))
Milan Broz46125c12008-02-08 02:10:30 +00002468 break;
2469
Mikulas Patocka401600d2009-04-02 19:55:38 +01002470 if (interruptible == TASK_INTERRUPTIBLE &&
2471 signal_pending(current)) {
Milan Broz46125c12008-02-08 02:10:30 +00002472 r = -EINTR;
2473 break;
2474 }
2475
2476 io_schedule();
2477 }
2478 set_current_state(TASK_RUNNING);
2479
Mikulas Patockab44ebeb2009-04-02 19:55:39 +01002480 remove_wait_queue(&md->wait, &wait);
2481
Milan Broz46125c12008-02-08 02:10:30 +00002482 return r;
2483}
2484
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485/*
2486 * Process the deferred bios
2487 */
Mikulas Patockaef208582009-04-02 19:55:38 +01002488static void dm_wq_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489{
Mikulas Patockaef208582009-04-02 19:55:38 +01002490 struct mapped_device *md = container_of(work, struct mapped_device,
2491 work);
Milan Broz6d6f10d2008-02-08 02:10:22 +00002492 struct bio *c;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002493 int srcu_idx;
2494 struct dm_table *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002496 map = dm_get_live_table(md, &srcu_idx);
Mikulas Patockaef208582009-04-02 19:55:38 +01002497
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002498 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002499 spin_lock_irq(&md->deferred_lock);
2500 c = bio_list_pop(&md->deferred);
2501 spin_unlock_irq(&md->deferred_lock);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002502
Tejun Heo6a8736d2010-09-08 18:07:00 +02002503 if (!c)
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002504 break;
Alasdair G Kergondf12ee92009-04-09 00:27:13 +01002505
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002506 if (dm_request_based(md))
2507 generic_make_request(c);
Tejun Heo6a8736d2010-09-08 18:07:00 +02002508 else
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002509 __split_and_process_bio(md, map, c);
Mikulas Patocka022c2612009-04-02 19:55:39 +01002510 }
Milan Broz73d410c2008-02-08 02:10:25 +00002511
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002512 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513}
2514
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002515static void dm_queue_flush(struct mapped_device *md)
Milan Broz304f3f62008-02-08 02:11:17 +00002516{
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002517 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002518 smp_mb__after_atomic();
Mikulas Patocka53d59142009-04-02 19:55:37 +01002519 queue_work(md->wq, &md->work);
Milan Broz304f3f62008-02-08 02:11:17 +00002520}
2521
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522/*
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002523 * Swap in a new table, returning the old one for the caller to destroy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002525struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526{
Mike Christie87eb5b22013-03-01 22:45:48 +00002527 struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002528 struct queue_limits limits;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002529 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
Daniel Walkere61290a2008-02-08 02:10:08 +00002531 mutex_lock(&md->suspend_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
2533 /* device must be suspended */
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002534 if (!dm_suspended_md(md))
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002535 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
Mike Snitzer3ae70652012-09-26 23:45:45 +01002537 /*
2538 * If the new table has no data devices, retain the existing limits.
2539 * This helps multipath with queue_if_no_path if all paths disappear,
2540 * then new I/O is queued based on these limits, and then some paths
2541 * reappear.
2542 */
2543 if (dm_table_has_no_data_devices(table)) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002544 live_map = dm_get_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01002545 if (live_map)
2546 limits = md->queue->limits;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002547 dm_put_live_table_fast(md);
Mike Snitzer3ae70652012-09-26 23:45:45 +01002548 }
2549
Mike Christie87eb5b22013-03-01 22:45:48 +00002550 if (!live_map) {
2551 r = dm_calculate_queue_limits(table, &limits);
2552 if (r) {
2553 map = ERR_PTR(r);
2554 goto out;
2555 }
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002556 }
Mike Snitzer754c5fc2009-06-22 10:12:34 +01002557
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002558 map = __bind(md, table, &limits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
Alasdair G Kergon93c534a2005-07-12 15:53:05 -07002560out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002561 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00002562 return map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563}
2564
2565/*
2566 * Functions to lock and unlock any filesystem running on the
2567 * device.
2568 */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002569static int lock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570{
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002571 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
2573 WARN_ON(md->frozen_sb);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002574
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002575 md->frozen_sb = freeze_bdev(md->bdev);
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002576 if (IS_ERR(md->frozen_sb)) {
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002577 r = PTR_ERR(md->frozen_sb);
Alasdair G Kergone39e2e92006-01-06 00:20:05 -08002578 md->frozen_sb = NULL;
2579 return r;
Alasdair G Kergondfbe03f2005-05-05 16:16:04 -07002580 }
2581
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002582 set_bit(DMF_FROZEN, &md->flags);
2583
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 return 0;
2585}
2586
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002587static void unlock_fs(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588{
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002589 if (!test_bit(DMF_FROZEN, &md->flags))
2590 return;
2591
Mikulas Patockadb8fef42009-06-22 10:12:15 +01002592 thaw_bdev(md->bdev, md->frozen_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 md->frozen_sb = NULL;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002594 clear_bit(DMF_FROZEN, &md->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595}
2596
2597/*
2598 * We need to be able to change a mapping table under a mounted
2599 * filesystem. For example we might want to move some data in
2600 * the background. Before the table can be swapped with
2601 * dm_bind_table, dm_suspend must be called to flush any in
2602 * flight bios and ensure that any further io gets deferred.
2603 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002604/*
2605 * Suspend mechanism in request-based dm.
2606 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002607 * 1. Flush all I/Os by lock_fs() if needed.
2608 * 2. Stop dispatching any I/O by stopping the request_queue.
2609 * 3. Wait for all in-flight I/Os to be completed or requeued.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002610 *
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002611 * To abort suspend, start the request_queue.
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002612 */
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002613int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614{
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002615 struct dm_table *map = NULL;
Milan Broz46125c12008-02-08 02:10:30 +00002616 int r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08002617 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002618 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
Daniel Walkere61290a2008-02-08 02:10:08 +00002620 mutex_lock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002621
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002622 if (dm_suspended_md(md)) {
Milan Broz73d410c2008-02-08 02:10:25 +00002623 r = -EINVAL;
Alasdair G Kergond2874832006-11-08 17:44:43 -08002624 goto out_unlock;
Milan Broz73d410c2008-02-08 02:10:25 +00002625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002627 map = md->map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002629 /*
2630 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2631 * This flag is cleared before dm_suspend returns.
2632 */
2633 if (noflush)
2634 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2635
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002636 /* This does not get reverted if there's an error later. */
2637 dm_table_presuspend_targets(map);
2638
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002639 /*
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002640 * Flush I/O to the device.
2641 * Any I/O submitted after lock_fs() may not be flushed.
2642 * noflush takes precedence over do_lockfs.
2643 * (lock_fs() flushes I/Os and waits for them to complete.)
Mikulas Patocka32a926d2009-06-22 10:12:17 +01002644 */
2645 if (!noflush && do_lockfs) {
2646 r = lock_fs(md);
2647 if (r)
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002648 goto out_unlock;
Alasdair G Kergonaa8d7c22006-01-06 00:20:06 -08002649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
2651 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002652 * Here we must make sure that no processes are submitting requests
2653 * to target drivers i.e. no one may be executing
2654 * __split_and_process_bio. This is called from dm_request and
2655 * dm_wq_work.
2656 *
2657 * To get all processes out of __split_and_process_bio in dm_request,
2658 * we take the write lock. To prevent any process from reentering
Tejun Heo6a8736d2010-09-08 18:07:00 +02002659 * __split_and_process_bio from dm_request and quiesce the thread
2660 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
2661 * flush_workqueue(md->wq).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 */
Alasdair G Kergon1eb787e2009-04-09 00:27:14 +01002663 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002664 synchronize_srcu(&md->io_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002666 /*
Tejun Heo29e40132010-09-08 18:07:00 +02002667 * Stop md->queue before flushing md->wq in case request-based
2668 * dm defers requests to md->wq from md->queue.
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002669 */
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002670 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002671 stop_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002672
Kiyoshi Uedad0bcb872009-12-10 23:52:18 +00002673 flush_workqueue(md->wq);
2674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 /*
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002676 * At this point no more requests are entering target request routines.
2677 * We call dm_wait_for_completion to wait for all existing requests
2678 * to finish.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 */
Mikulas Patocka401600d2009-04-02 19:55:38 +01002680 r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
Milan Broz6d6f10d2008-02-08 02:10:22 +00002682 if (noflush)
Mikulas Patocka022c2612009-04-02 19:55:39 +01002683 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002684 synchronize_srcu(&md->io_barrier);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002685
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 /* were we interrupted ? */
Milan Broz46125c12008-02-08 02:10:30 +00002687 if (r < 0) {
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002688 dm_queue_flush(md);
Milan Broz73d410c2008-02-08 02:10:25 +00002689
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002690 if (dm_request_based(md))
Kiyoshi Ueda9f518b22009-12-10 23:52:16 +00002691 start_queue(md->queue);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002692
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002693 unlock_fs(md);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002694 goto out_unlock; /* pushback list is already flushed, so skip flush */
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002695 }
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002696
Mikulas Patocka3b00b202009-04-09 00:27:15 +01002697 /*
2698 * If dm_wait_for_completion returned 0, the device is completely
2699 * quiescent now. There is no request-processing activity. All new
2700 * requests are being added to md->deferred list.
2701 */
2702
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 set_bit(DMF_SUSPENDED, &md->flags);
2704
Kiyoshi Ueda4d4471c2009-12-10 23:52:26 +00002705 dm_table_postsuspend_targets(map);
2706
Alasdair G Kergond2874832006-11-08 17:44:43 -08002707out_unlock:
Daniel Walkere61290a2008-02-08 02:10:08 +00002708 mutex_unlock(&md->suspend_lock);
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002709 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710}
2711
2712int dm_resume(struct mapped_device *md)
2713{
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002714 int r = -EINVAL;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002715 struct dm_table *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
Daniel Walkere61290a2008-02-08 02:10:08 +00002717 mutex_lock(&md->suspend_lock);
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002718 if (!dm_suspended_md(md))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002719 goto out;
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002720
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01002721 map = md->map;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002722 if (!map || !dm_table_get_size(map))
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002723 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
Milan Broz8757b772006-10-03 01:15:36 -07002725 r = dm_table_resume_targets(map);
2726 if (r)
2727 goto out;
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002728
Mikulas Patocka9a1fb462009-04-02 19:55:36 +01002729 dm_queue_flush(md);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002730
Kiyoshi Uedacec47e32009-06-22 10:12:35 +01002731 /*
2732 * Flushing deferred I/Os must be done after targets are resumed
2733 * so that mapping of targets can work correctly.
2734 * Request-based dm is queueing the deferred I/Os in its request_queue.
2735 */
2736 if (dm_request_based(md))
2737 start_queue(md->queue);
2738
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002739 unlock_fs(md);
2740
2741 clear_bit(DMF_SUSPENDED, &md->flags);
2742
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002743 r = 0;
2744out:
Daniel Walkere61290a2008-02-08 02:10:08 +00002745 mutex_unlock(&md->suspend_lock);
Alasdair G Kergon2ca33102005-07-28 21:16:00 -07002746
Alasdair G Kergoncf222b32005-07-28 21:15:57 -07002747 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748}
2749
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04002750/*
2751 * Internal suspend/resume works like userspace-driven suspend. It waits
2752 * until all bios finish and prevents issuing new bios to the target drivers.
2753 * It may be used only from the kernel.
2754 *
2755 * Internal suspend holds md->suspend_lock, which prevents interaction with
2756 * userspace-driven suspend.
2757 */
2758
2759void dm_internal_suspend(struct mapped_device *md)
2760{
2761 mutex_lock(&md->suspend_lock);
2762 if (dm_suspended_md(md))
2763 return;
2764
2765 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2766 synchronize_srcu(&md->io_barrier);
2767 flush_workqueue(md->wq);
2768 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2769}
2770
2771void dm_internal_resume(struct mapped_device *md)
2772{
2773 if (dm_suspended_md(md))
2774 goto done;
2775
2776 dm_queue_flush(md);
2777
2778done:
2779 mutex_unlock(&md->suspend_lock);
2780}
2781
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782/*-----------------------------------------------------------------
2783 * Event notification.
2784 *---------------------------------------------------------------*/
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002785int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
Milan Broz60935eb2009-06-22 10:12:30 +01002786 unsigned cookie)
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002787{
Milan Broz60935eb2009-06-22 10:12:30 +01002788 char udev_cookie[DM_COOKIE_LENGTH];
2789 char *envp[] = { udev_cookie, NULL };
2790
2791 if (!cookie)
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002792 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
Milan Broz60935eb2009-06-22 10:12:30 +01002793 else {
2794 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2795 DM_COOKIE_ENV_VAR_NAME, cookie);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002796 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
2797 action, envp);
Milan Broz60935eb2009-06-22 10:12:30 +01002798 }
Alasdair G Kergon69267a32007-12-13 14:15:57 +00002799}
2800
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002801uint32_t dm_next_uevent_seq(struct mapped_device *md)
2802{
2803 return atomic_add_return(1, &md->uevent_seq);
2804}
2805
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806uint32_t dm_get_event_nr(struct mapped_device *md)
2807{
2808 return atomic_read(&md->event_nr);
2809}
2810
2811int dm_wait_event(struct mapped_device *md, int event_nr)
2812{
2813 return wait_event_interruptible(md->eventq,
2814 (event_nr != atomic_read(&md->event_nr)));
2815}
2816
Mike Anderson7a8c3d32007-10-19 22:48:01 +01002817void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2818{
2819 unsigned long flags;
2820
2821 spin_lock_irqsave(&md->uevent_lock, flags);
2822 list_add(elist, &md->uevent_list);
2823 spin_unlock_irqrestore(&md->uevent_lock, flags);
2824}
2825
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826/*
2827 * The gendisk is only valid as long as you have a reference
2828 * count on 'md'.
2829 */
2830struct gendisk *dm_disk(struct mapped_device *md)
2831{
2832 return md->disk;
2833}
2834
Milan Broz784aae72009-01-06 03:05:12 +00002835struct kobject *dm_kobject(struct mapped_device *md)
2836{
Mikulas Patocka2995fa72014-01-13 19:37:54 -05002837 return &md->kobj_holder.kobj;
Milan Broz784aae72009-01-06 03:05:12 +00002838}
2839
Milan Broz784aae72009-01-06 03:05:12 +00002840struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2841{
2842 struct mapped_device *md;
2843
Mikulas Patocka2995fa72014-01-13 19:37:54 -05002844 md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
Milan Broz784aae72009-01-06 03:05:12 +00002845
Milan Broz4d89b7b2009-06-22 10:12:11 +01002846 if (test_bit(DMF_FREEING, &md->flags) ||
Mike Anderson432a2122009-12-10 23:52:20 +00002847 dm_deleting_md(md))
Milan Broz4d89b7b2009-06-22 10:12:11 +01002848 return NULL;
2849
Milan Broz784aae72009-01-06 03:05:12 +00002850 dm_get(md);
2851 return md;
2852}
2853
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00002854int dm_suspended_md(struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855{
2856 return test_bit(DMF_SUSPENDED, &md->flags);
2857}
2858
Mikulas Patocka2c140a22013-11-01 18:27:41 -04002859int dm_test_deferred_remove_flag(struct mapped_device *md)
2860{
2861 return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
2862}
2863
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002864int dm_suspended(struct dm_target *ti)
2865{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002866 return dm_suspended_md(dm_table_get_md(ti->table));
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +00002867}
2868EXPORT_SYMBOL_GPL(dm_suspended);
2869
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002870int dm_noflush_suspending(struct dm_target *ti)
2871{
Kiyoshi Uedaecdb2e22010-03-06 02:29:52 +00002872 return __noflush_suspending(dm_table_get_md(ti->table));
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -08002873}
2874EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2875
Mikulas Patockac0820cf2012-12-21 20:23:38 +00002876struct dm_md_mempools *dm_alloc_md_mempools(unsigned type, unsigned integrity, unsigned per_bio_data_size)
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002877{
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002878 struct dm_md_mempools *pools = kzalloc(sizeof(*pools), GFP_KERNEL);
2879 struct kmem_cache *cachep;
2880 unsigned int pool_size;
2881 unsigned int front_pad;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002882
2883 if (!pools)
2884 return NULL;
2885
Jun'ichi Nomura23e50832013-03-01 22:45:48 +00002886 if (type == DM_TYPE_BIO_BASED) {
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002887 cachep = _io_cache;
Mike Snitzere8603132013-09-12 18:06:12 -04002888 pool_size = dm_get_reserved_bio_based_ios();
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002889 front_pad = roundup(per_bio_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
2890 } else if (type == DM_TYPE_REQUEST_BASED) {
2891 cachep = _rq_tio_cache;
Mike Snitzerf4790822013-09-12 18:06:12 -04002892 pool_size = dm_get_reserved_rq_based_ios();
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002893 front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
2894 /* per_bio_data_size is not used. See __bind_mempools(). */
2895 WARN_ON(per_bio_data_size != 0);
2896 } else
2897 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002898
Mike Snitzer6cfa5852013-09-12 18:06:11 -04002899 pools->io_pool = mempool_create_slab_pool(pool_size, cachep);
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002900 if (!pools->io_pool)
2901 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002902
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002903 pools->bs = bioset_create(pool_size, front_pad);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002904 if (!pools->bs)
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002905 goto out;
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002906
Martin K. Petersena91a2782011-03-17 11:11:05 +01002907 if (integrity && bioset_integrity_create(pools->bs, pool_size))
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002908 goto out;
Martin K. Petersena91a2782011-03-17 11:11:05 +01002909
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002910 return pools;
2911
Jun'ichi Nomura5f015202013-03-01 22:45:48 +00002912out:
2913 dm_free_md_mempools(pools);
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002914
2915 return NULL;
2916}
2917
2918void dm_free_md_mempools(struct dm_md_mempools *pools)
2919{
2920 if (!pools)
2921 return;
2922
2923 if (pools->io_pool)
2924 mempool_destroy(pools->io_pool);
2925
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01002926 if (pools->bs)
2927 bioset_free(pools->bs);
2928
2929 kfree(pools);
2930}
2931
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002932static const struct block_device_operations dm_blk_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 .open = dm_blk_open,
2934 .release = dm_blk_close,
Milan Brozaa129a22006-10-03 01:15:15 -07002935 .ioctl = dm_blk_ioctl,
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08002936 .getgeo = dm_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 .owner = THIS_MODULE
2938};
2939
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940/*
2941 * module hooks
2942 */
2943module_init(dm_init);
2944module_exit(dm_exit);
2945
2946module_param(major, uint, 0);
2947MODULE_PARM_DESC(major, "The major number of the device mapper");
Mike Snitzerf4790822013-09-12 18:06:12 -04002948
Mike Snitzere8603132013-09-12 18:06:12 -04002949module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
2950MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
2951
Mike Snitzerf4790822013-09-12 18:06:12 -04002952module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
2953MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
2954
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955MODULE_DESCRIPTION(DM_NAME " driver");
2956MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2957MODULE_LICENSE("GPL");