dm.c 35 KB

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