dm.c 38 KB

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