dm.c 37 KB

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