dm.c 37 KB

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