dm.c 34 KB

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