dm.c 35 KB

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