dm.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807
  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 *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. if (!bio_barrier(io->bio))
  436. bio_list_add_head(&md->deferred,
  437. 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. /*
  763. * If the target doesn't support merge method and some of the devices
  764. * provided their merge_bvec method (we know this by looking at
  765. * queue_max_hw_sectors), then we can't allow bios with multiple vector
  766. * entries. So always set max_size to 0, and the code below allows
  767. * just one page.
  768. */
  769. else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
  770. max_size = 0;
  771. out_table:
  772. dm_table_put(map);
  773. out:
  774. /*
  775. * Always allow an entire first page
  776. */
  777. if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
  778. max_size = biovec->bv_len;
  779. return max_size;
  780. }
  781. /*
  782. * The request function that just remaps the bio built up by
  783. * dm_merge_bvec.
  784. */
  785. static int dm_request(struct request_queue *q, struct bio *bio)
  786. {
  787. int rw = bio_data_dir(bio);
  788. struct mapped_device *md = q->queuedata;
  789. int cpu;
  790. down_read(&md->io_lock);
  791. cpu = part_stat_lock();
  792. part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
  793. part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
  794. part_stat_unlock();
  795. /*
  796. * If we're suspended or the thread is processing barriers
  797. * we have to queue this io for later.
  798. */
  799. if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) ||
  800. unlikely(bio_barrier(bio))) {
  801. up_read(&md->io_lock);
  802. if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) &&
  803. bio_rw(bio) == READA) {
  804. bio_io_error(bio);
  805. return 0;
  806. }
  807. queue_io(md, bio);
  808. return 0;
  809. }
  810. __split_and_process_bio(md, bio);
  811. up_read(&md->io_lock);
  812. return 0;
  813. }
  814. static void dm_unplug_all(struct request_queue *q)
  815. {
  816. struct mapped_device *md = q->queuedata;
  817. struct dm_table *map = dm_get_table(md);
  818. if (map) {
  819. dm_table_unplug_all(map);
  820. dm_table_put(map);
  821. }
  822. }
  823. static int dm_any_congested(void *congested_data, int bdi_bits)
  824. {
  825. int r = bdi_bits;
  826. struct mapped_device *md = congested_data;
  827. struct dm_table *map;
  828. if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
  829. map = dm_get_table(md);
  830. if (map) {
  831. r = dm_table_any_congested(map, bdi_bits);
  832. dm_table_put(map);
  833. }
  834. }
  835. return r;
  836. }
  837. /*-----------------------------------------------------------------
  838. * An IDR is used to keep track of allocated minor numbers.
  839. *---------------------------------------------------------------*/
  840. static DEFINE_IDR(_minor_idr);
  841. static void free_minor(int minor)
  842. {
  843. spin_lock(&_minor_lock);
  844. idr_remove(&_minor_idr, minor);
  845. spin_unlock(&_minor_lock);
  846. }
  847. /*
  848. * See if the device with a specific minor # is free.
  849. */
  850. static int specific_minor(int minor)
  851. {
  852. int r, m;
  853. if (minor >= (1 << MINORBITS))
  854. return -EINVAL;
  855. r = idr_pre_get(&_minor_idr, GFP_KERNEL);
  856. if (!r)
  857. return -ENOMEM;
  858. spin_lock(&_minor_lock);
  859. if (idr_find(&_minor_idr, minor)) {
  860. r = -EBUSY;
  861. goto out;
  862. }
  863. r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
  864. if (r)
  865. goto out;
  866. if (m != minor) {
  867. idr_remove(&_minor_idr, m);
  868. r = -EBUSY;
  869. goto out;
  870. }
  871. out:
  872. spin_unlock(&_minor_lock);
  873. return r;
  874. }
  875. static int next_free_minor(int *minor)
  876. {
  877. int r, m;
  878. r = idr_pre_get(&_minor_idr, GFP_KERNEL);
  879. if (!r)
  880. return -ENOMEM;
  881. spin_lock(&_minor_lock);
  882. r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
  883. if (r)
  884. goto out;
  885. if (m >= (1 << MINORBITS)) {
  886. idr_remove(&_minor_idr, m);
  887. r = -ENOSPC;
  888. goto out;
  889. }
  890. *minor = m;
  891. out:
  892. spin_unlock(&_minor_lock);
  893. return r;
  894. }
  895. static struct block_device_operations dm_blk_dops;
  896. static void dm_wq_work(struct work_struct *work);
  897. /*
  898. * Allocate and initialise a blank device with a given minor.
  899. */
  900. static struct mapped_device *alloc_dev(int minor)
  901. {
  902. int r;
  903. struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
  904. void *old_md;
  905. if (!md) {
  906. DMWARN("unable to allocate device, out of memory.");
  907. return NULL;
  908. }
  909. if (!try_module_get(THIS_MODULE))
  910. goto bad_module_get;
  911. /* get a minor number for the dev */
  912. if (minor == DM_ANY_MINOR)
  913. r = next_free_minor(&minor);
  914. else
  915. r = specific_minor(minor);
  916. if (r < 0)
  917. goto bad_minor;
  918. init_rwsem(&md->io_lock);
  919. mutex_init(&md->suspend_lock);
  920. spin_lock_init(&md->deferred_lock);
  921. rwlock_init(&md->map_lock);
  922. atomic_set(&md->holders, 1);
  923. atomic_set(&md->open_count, 0);
  924. atomic_set(&md->event_nr, 0);
  925. atomic_set(&md->uevent_seq, 0);
  926. INIT_LIST_HEAD(&md->uevent_list);
  927. spin_lock_init(&md->uevent_lock);
  928. md->queue = blk_alloc_queue(GFP_KERNEL);
  929. if (!md->queue)
  930. goto bad_queue;
  931. md->queue->queuedata = md;
  932. md->queue->backing_dev_info.congested_fn = dm_any_congested;
  933. md->queue->backing_dev_info.congested_data = md;
  934. blk_queue_make_request(md->queue, dm_request);
  935. blk_queue_ordered(md->queue, QUEUE_ORDERED_DRAIN, NULL);
  936. blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
  937. md->queue->unplug_fn = dm_unplug_all;
  938. blk_queue_merge_bvec(md->queue, dm_merge_bvec);
  939. md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
  940. if (!md->io_pool)
  941. goto bad_io_pool;
  942. md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
  943. if (!md->tio_pool)
  944. goto bad_tio_pool;
  945. md->bs = bioset_create(16, 0);
  946. if (!md->bs)
  947. goto bad_no_bioset;
  948. md->disk = alloc_disk(1);
  949. if (!md->disk)
  950. goto bad_disk;
  951. atomic_set(&md->pending, 0);
  952. init_waitqueue_head(&md->wait);
  953. INIT_WORK(&md->work, dm_wq_work);
  954. init_waitqueue_head(&md->eventq);
  955. md->disk->major = _major;
  956. md->disk->first_minor = minor;
  957. md->disk->fops = &dm_blk_dops;
  958. md->disk->queue = md->queue;
  959. md->disk->private_data = md;
  960. sprintf(md->disk->disk_name, "dm-%d", minor);
  961. add_disk(md->disk);
  962. format_dev_t(md->name, MKDEV(_major, minor));
  963. md->wq = create_singlethread_workqueue("kdmflush");
  964. if (!md->wq)
  965. goto bad_thread;
  966. md->bdev = bdget_disk(md->disk, 0);
  967. if (!md->bdev)
  968. goto bad_bdev;
  969. /* Populate the mapping, nobody knows we exist yet */
  970. spin_lock(&_minor_lock);
  971. old_md = idr_replace(&_minor_idr, md, minor);
  972. spin_unlock(&_minor_lock);
  973. BUG_ON(old_md != MINOR_ALLOCED);
  974. return md;
  975. bad_bdev:
  976. destroy_workqueue(md->wq);
  977. bad_thread:
  978. put_disk(md->disk);
  979. bad_disk:
  980. bioset_free(md->bs);
  981. bad_no_bioset:
  982. mempool_destroy(md->tio_pool);
  983. bad_tio_pool:
  984. mempool_destroy(md->io_pool);
  985. bad_io_pool:
  986. blk_cleanup_queue(md->queue);
  987. bad_queue:
  988. free_minor(minor);
  989. bad_minor:
  990. module_put(THIS_MODULE);
  991. bad_module_get:
  992. kfree(md);
  993. return NULL;
  994. }
  995. static void unlock_fs(struct mapped_device *md);
  996. static void free_dev(struct mapped_device *md)
  997. {
  998. int minor = MINOR(disk_devt(md->disk));
  999. unlock_fs(md);
  1000. bdput(md->bdev);
  1001. destroy_workqueue(md->wq);
  1002. mempool_destroy(md->tio_pool);
  1003. mempool_destroy(md->io_pool);
  1004. bioset_free(md->bs);
  1005. blk_integrity_unregister(md->disk);
  1006. del_gendisk(md->disk);
  1007. free_minor(minor);
  1008. spin_lock(&_minor_lock);
  1009. md->disk->private_data = NULL;
  1010. spin_unlock(&_minor_lock);
  1011. put_disk(md->disk);
  1012. blk_cleanup_queue(md->queue);
  1013. module_put(THIS_MODULE);
  1014. kfree(md);
  1015. }
  1016. /*
  1017. * Bind a table to the device.
  1018. */
  1019. static void event_callback(void *context)
  1020. {
  1021. unsigned long flags;
  1022. LIST_HEAD(uevents);
  1023. struct mapped_device *md = (struct mapped_device *) context;
  1024. spin_lock_irqsave(&md->uevent_lock, flags);
  1025. list_splice_init(&md->uevent_list, &uevents);
  1026. spin_unlock_irqrestore(&md->uevent_lock, flags);
  1027. dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
  1028. atomic_inc(&md->event_nr);
  1029. wake_up(&md->eventq);
  1030. }
  1031. static void __set_size(struct mapped_device *md, sector_t size)
  1032. {
  1033. set_capacity(md->disk, size);
  1034. mutex_lock(&md->bdev->bd_inode->i_mutex);
  1035. i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
  1036. mutex_unlock(&md->bdev->bd_inode->i_mutex);
  1037. }
  1038. static int __bind(struct mapped_device *md, struct dm_table *t)
  1039. {
  1040. struct request_queue *q = md->queue;
  1041. sector_t size;
  1042. size = dm_table_get_size(t);
  1043. /*
  1044. * Wipe any geometry if the size of the table changed.
  1045. */
  1046. if (size != get_capacity(md->disk))
  1047. memset(&md->geometry, 0, sizeof(md->geometry));
  1048. __set_size(md, size);
  1049. if (!size) {
  1050. dm_table_destroy(t);
  1051. return 0;
  1052. }
  1053. dm_table_event_callback(t, event_callback, md);
  1054. write_lock(&md->map_lock);
  1055. md->map = t;
  1056. dm_table_set_restrictions(t, q);
  1057. write_unlock(&md->map_lock);
  1058. return 0;
  1059. }
  1060. static void __unbind(struct mapped_device *md)
  1061. {
  1062. struct dm_table *map = md->map;
  1063. if (!map)
  1064. return;
  1065. dm_table_event_callback(map, NULL, NULL);
  1066. write_lock(&md->map_lock);
  1067. md->map = NULL;
  1068. write_unlock(&md->map_lock);
  1069. dm_table_destroy(map);
  1070. }
  1071. /*
  1072. * Constructor for a new device.
  1073. */
  1074. int dm_create(int minor, struct mapped_device **result)
  1075. {
  1076. struct mapped_device *md;
  1077. md = alloc_dev(minor);
  1078. if (!md)
  1079. return -ENXIO;
  1080. dm_sysfs_init(md);
  1081. *result = md;
  1082. return 0;
  1083. }
  1084. static struct mapped_device *dm_find_md(dev_t dev)
  1085. {
  1086. struct mapped_device *md;
  1087. unsigned minor = MINOR(dev);
  1088. if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
  1089. return NULL;
  1090. spin_lock(&_minor_lock);
  1091. md = idr_find(&_minor_idr, minor);
  1092. if (md && (md == MINOR_ALLOCED ||
  1093. (MINOR(disk_devt(dm_disk(md))) != minor) ||
  1094. test_bit(DMF_FREEING, &md->flags))) {
  1095. md = NULL;
  1096. goto out;
  1097. }
  1098. out:
  1099. spin_unlock(&_minor_lock);
  1100. return md;
  1101. }
  1102. struct mapped_device *dm_get_md(dev_t dev)
  1103. {
  1104. struct mapped_device *md = dm_find_md(dev);
  1105. if (md)
  1106. dm_get(md);
  1107. return md;
  1108. }
  1109. void *dm_get_mdptr(struct mapped_device *md)
  1110. {
  1111. return md->interface_ptr;
  1112. }
  1113. void dm_set_mdptr(struct mapped_device *md, void *ptr)
  1114. {
  1115. md->interface_ptr = ptr;
  1116. }
  1117. void dm_get(struct mapped_device *md)
  1118. {
  1119. atomic_inc(&md->holders);
  1120. }
  1121. const char *dm_device_name(struct mapped_device *md)
  1122. {
  1123. return md->name;
  1124. }
  1125. EXPORT_SYMBOL_GPL(dm_device_name);
  1126. void dm_put(struct mapped_device *md)
  1127. {
  1128. struct dm_table *map;
  1129. BUG_ON(test_bit(DMF_FREEING, &md->flags));
  1130. if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
  1131. map = dm_get_table(md);
  1132. idr_replace(&_minor_idr, MINOR_ALLOCED,
  1133. MINOR(disk_devt(dm_disk(md))));
  1134. set_bit(DMF_FREEING, &md->flags);
  1135. spin_unlock(&_minor_lock);
  1136. if (!dm_suspended(md)) {
  1137. dm_table_presuspend_targets(map);
  1138. dm_table_postsuspend_targets(map);
  1139. }
  1140. dm_sysfs_exit(md);
  1141. dm_table_put(map);
  1142. __unbind(md);
  1143. free_dev(md);
  1144. }
  1145. }
  1146. EXPORT_SYMBOL_GPL(dm_put);
  1147. static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
  1148. {
  1149. int r = 0;
  1150. DECLARE_WAITQUEUE(wait, current);
  1151. dm_unplug_all(md->queue);
  1152. add_wait_queue(&md->wait, &wait);
  1153. while (1) {
  1154. set_current_state(interruptible);
  1155. smp_mb();
  1156. if (!atomic_read(&md->pending))
  1157. break;
  1158. if (interruptible == TASK_INTERRUPTIBLE &&
  1159. signal_pending(current)) {
  1160. r = -EINTR;
  1161. break;
  1162. }
  1163. io_schedule();
  1164. }
  1165. set_current_state(TASK_RUNNING);
  1166. remove_wait_queue(&md->wait, &wait);
  1167. return r;
  1168. }
  1169. static void dm_flush(struct mapped_device *md)
  1170. {
  1171. dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
  1172. }
  1173. static void process_barrier(struct mapped_device *md, struct bio *bio)
  1174. {
  1175. dm_flush(md);
  1176. if (bio_empty_barrier(bio)) {
  1177. bio_endio(bio, 0);
  1178. return;
  1179. }
  1180. __split_and_process_bio(md, bio);
  1181. dm_flush(md);
  1182. if (md->barrier_error != DM_ENDIO_REQUEUE)
  1183. bio_endio(bio, md->barrier_error);
  1184. else {
  1185. spin_lock_irq(&md->deferred_lock);
  1186. bio_list_add_head(&md->deferred, bio);
  1187. spin_unlock_irq(&md->deferred_lock);
  1188. }
  1189. }
  1190. /*
  1191. * Process the deferred bios
  1192. */
  1193. static void dm_wq_work(struct work_struct *work)
  1194. {
  1195. struct mapped_device *md = container_of(work, struct mapped_device,
  1196. work);
  1197. struct bio *c;
  1198. down_write(&md->io_lock);
  1199. while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
  1200. spin_lock_irq(&md->deferred_lock);
  1201. c = bio_list_pop(&md->deferred);
  1202. spin_unlock_irq(&md->deferred_lock);
  1203. if (!c) {
  1204. clear_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
  1205. break;
  1206. }
  1207. up_write(&md->io_lock);
  1208. if (bio_barrier(c))
  1209. process_barrier(md, c);
  1210. else
  1211. __split_and_process_bio(md, c);
  1212. down_write(&md->io_lock);
  1213. }
  1214. up_write(&md->io_lock);
  1215. }
  1216. static void dm_queue_flush(struct mapped_device *md)
  1217. {
  1218. clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
  1219. smp_mb__after_clear_bit();
  1220. queue_work(md->wq, &md->work);
  1221. }
  1222. /*
  1223. * Swap in a new table (destroying old one).
  1224. */
  1225. int dm_swap_table(struct mapped_device *md, struct dm_table *table)
  1226. {
  1227. int r = -EINVAL;
  1228. mutex_lock(&md->suspend_lock);
  1229. /* device must be suspended */
  1230. if (!dm_suspended(md))
  1231. goto out;
  1232. __unbind(md);
  1233. r = __bind(md, table);
  1234. out:
  1235. mutex_unlock(&md->suspend_lock);
  1236. return r;
  1237. }
  1238. /*
  1239. * Functions to lock and unlock any filesystem running on the
  1240. * device.
  1241. */
  1242. static int lock_fs(struct mapped_device *md)
  1243. {
  1244. int r;
  1245. WARN_ON(md->frozen_sb);
  1246. md->frozen_sb = freeze_bdev(md->bdev);
  1247. if (IS_ERR(md->frozen_sb)) {
  1248. r = PTR_ERR(md->frozen_sb);
  1249. md->frozen_sb = NULL;
  1250. return r;
  1251. }
  1252. set_bit(DMF_FROZEN, &md->flags);
  1253. return 0;
  1254. }
  1255. static void unlock_fs(struct mapped_device *md)
  1256. {
  1257. if (!test_bit(DMF_FROZEN, &md->flags))
  1258. return;
  1259. thaw_bdev(md->bdev, md->frozen_sb);
  1260. md->frozen_sb = NULL;
  1261. clear_bit(DMF_FROZEN, &md->flags);
  1262. }
  1263. /*
  1264. * We need to be able to change a mapping table under a mounted
  1265. * filesystem. For example we might want to move some data in
  1266. * the background. Before the table can be swapped with
  1267. * dm_bind_table, dm_suspend must be called to flush any in
  1268. * flight bios and ensure that any further io gets deferred.
  1269. */
  1270. int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
  1271. {
  1272. struct dm_table *map = NULL;
  1273. int r = 0;
  1274. int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
  1275. int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
  1276. mutex_lock(&md->suspend_lock);
  1277. if (dm_suspended(md)) {
  1278. r = -EINVAL;
  1279. goto out_unlock;
  1280. }
  1281. map = dm_get_table(md);
  1282. /*
  1283. * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
  1284. * This flag is cleared before dm_suspend returns.
  1285. */
  1286. if (noflush)
  1287. set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  1288. /* This does not get reverted if there's an error later. */
  1289. dm_table_presuspend_targets(map);
  1290. /*
  1291. * Flush I/O to the device. noflush supersedes do_lockfs,
  1292. * because lock_fs() needs to flush I/Os.
  1293. */
  1294. if (!noflush && do_lockfs) {
  1295. r = lock_fs(md);
  1296. if (r)
  1297. goto out;
  1298. }
  1299. /*
  1300. * Here we must make sure that no processes are submitting requests
  1301. * to target drivers i.e. no one may be executing
  1302. * __split_and_process_bio. This is called from dm_request and
  1303. * dm_wq_work.
  1304. *
  1305. * To get all processes out of __split_and_process_bio in dm_request,
  1306. * we take the write lock. To prevent any process from reentering
  1307. * __split_and_process_bio from dm_request, we set
  1308. * DMF_QUEUE_IO_TO_THREAD.
  1309. *
  1310. * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND
  1311. * and call flush_workqueue(md->wq). flush_workqueue will wait until
  1312. * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any
  1313. * further calls to __split_and_process_bio from dm_wq_work.
  1314. */
  1315. down_write(&md->io_lock);
  1316. set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
  1317. set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
  1318. up_write(&md->io_lock);
  1319. flush_workqueue(md->wq);
  1320. /*
  1321. * At this point no more requests are entering target request routines.
  1322. * We call dm_wait_for_completion to wait for all existing requests
  1323. * to finish.
  1324. */
  1325. r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
  1326. down_write(&md->io_lock);
  1327. if (noflush)
  1328. clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  1329. up_write(&md->io_lock);
  1330. /* were we interrupted ? */
  1331. if (r < 0) {
  1332. dm_queue_flush(md);
  1333. unlock_fs(md);
  1334. goto out; /* pushback list is already flushed, so skip flush */
  1335. }
  1336. /*
  1337. * If dm_wait_for_completion returned 0, the device is completely
  1338. * quiescent now. There is no request-processing activity. All new
  1339. * requests are being added to md->deferred list.
  1340. */
  1341. dm_table_postsuspend_targets(map);
  1342. set_bit(DMF_SUSPENDED, &md->flags);
  1343. out:
  1344. dm_table_put(map);
  1345. out_unlock:
  1346. mutex_unlock(&md->suspend_lock);
  1347. return r;
  1348. }
  1349. int dm_resume(struct mapped_device *md)
  1350. {
  1351. int r = -EINVAL;
  1352. struct dm_table *map = NULL;
  1353. mutex_lock(&md->suspend_lock);
  1354. if (!dm_suspended(md))
  1355. goto out;
  1356. map = dm_get_table(md);
  1357. if (!map || !dm_table_get_size(map))
  1358. goto out;
  1359. r = dm_table_resume_targets(map);
  1360. if (r)
  1361. goto out;
  1362. dm_queue_flush(md);
  1363. unlock_fs(md);
  1364. clear_bit(DMF_SUSPENDED, &md->flags);
  1365. dm_table_unplug_all(map);
  1366. dm_kobject_uevent(md);
  1367. r = 0;
  1368. out:
  1369. dm_table_put(map);
  1370. mutex_unlock(&md->suspend_lock);
  1371. return r;
  1372. }
  1373. /*-----------------------------------------------------------------
  1374. * Event notification.
  1375. *---------------------------------------------------------------*/
  1376. void dm_kobject_uevent(struct mapped_device *md)
  1377. {
  1378. kobject_uevent(&disk_to_dev(md->disk)->kobj, KOBJ_CHANGE);
  1379. }
  1380. uint32_t dm_next_uevent_seq(struct mapped_device *md)
  1381. {
  1382. return atomic_add_return(1, &md->uevent_seq);
  1383. }
  1384. uint32_t dm_get_event_nr(struct mapped_device *md)
  1385. {
  1386. return atomic_read(&md->event_nr);
  1387. }
  1388. int dm_wait_event(struct mapped_device *md, int event_nr)
  1389. {
  1390. return wait_event_interruptible(md->eventq,
  1391. (event_nr != atomic_read(&md->event_nr)));
  1392. }
  1393. void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
  1394. {
  1395. unsigned long flags;
  1396. spin_lock_irqsave(&md->uevent_lock, flags);
  1397. list_add(elist, &md->uevent_list);
  1398. spin_unlock_irqrestore(&md->uevent_lock, flags);
  1399. }
  1400. /*
  1401. * The gendisk is only valid as long as you have a reference
  1402. * count on 'md'.
  1403. */
  1404. struct gendisk *dm_disk(struct mapped_device *md)
  1405. {
  1406. return md->disk;
  1407. }
  1408. struct kobject *dm_kobject(struct mapped_device *md)
  1409. {
  1410. return &md->kobj;
  1411. }
  1412. /*
  1413. * struct mapped_device should not be exported outside of dm.c
  1414. * so use this check to verify that kobj is part of md structure
  1415. */
  1416. struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
  1417. {
  1418. struct mapped_device *md;
  1419. md = container_of(kobj, struct mapped_device, kobj);
  1420. if (&md->kobj != kobj)
  1421. return NULL;
  1422. if (test_bit(DMF_FREEING, &md->flags) ||
  1423. test_bit(DMF_DELETING, &md->flags))
  1424. return NULL;
  1425. dm_get(md);
  1426. return md;
  1427. }
  1428. int dm_suspended(struct mapped_device *md)
  1429. {
  1430. return test_bit(DMF_SUSPENDED, &md->flags);
  1431. }
  1432. int dm_noflush_suspending(struct dm_target *ti)
  1433. {
  1434. struct mapped_device *md = dm_table_get_md(ti->table);
  1435. int r = __noflush_suspending(md);
  1436. dm_put(md);
  1437. return r;
  1438. }
  1439. EXPORT_SYMBOL_GPL(dm_noflush_suspending);
  1440. static struct block_device_operations dm_blk_dops = {
  1441. .open = dm_blk_open,
  1442. .release = dm_blk_close,
  1443. .ioctl = dm_blk_ioctl,
  1444. .getgeo = dm_blk_getgeo,
  1445. .owner = THIS_MODULE
  1446. };
  1447. EXPORT_SYMBOL(dm_get_mapinfo);
  1448. /*
  1449. * module hooks
  1450. */
  1451. module_init(dm_init);
  1452. module_exit(dm_exit);
  1453. module_param(major, uint, 0);
  1454. MODULE_PARM_DESC(major, "The major number of the device mapper");
  1455. MODULE_DESCRIPTION(DM_NAME " driver");
  1456. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  1457. MODULE_LICENSE("GPL");