dm.c 38 KB

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