dm.c 37 KB

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