dm.c 34 KB

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