dm.c 34 KB

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