dm.c 36 KB

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