dm.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761
  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/delay.h>
  21. #include <trace/events/block.h>
  22. #define DM_MSG_PREFIX "core"
  23. /*
  24. * Cookies are numeric values sent with CHANGE and REMOVE
  25. * uevents while resuming, removing or renaming the device.
  26. */
  27. #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
  28. #define DM_COOKIE_LENGTH 24
  29. static const char *_name = DM_NAME;
  30. static unsigned int major = 0;
  31. static unsigned int _major = 0;
  32. static DEFINE_IDR(_minor_idr);
  33. static DEFINE_SPINLOCK(_minor_lock);
  34. /*
  35. * For bio-based dm.
  36. * One of these is allocated per bio.
  37. */
  38. struct dm_io {
  39. struct mapped_device *md;
  40. int error;
  41. atomic_t io_count;
  42. struct bio *bio;
  43. unsigned long start_time;
  44. spinlock_t endio_lock;
  45. };
  46. /*
  47. * For bio-based dm.
  48. * One of these is allocated per target within a bio. Hopefully
  49. * this will be simplified out one day.
  50. */
  51. struct dm_target_io {
  52. struct dm_io *io;
  53. struct dm_target *ti;
  54. union map_info info;
  55. };
  56. /*
  57. * For request-based dm.
  58. * One of these is allocated per request.
  59. */
  60. struct dm_rq_target_io {
  61. struct mapped_device *md;
  62. struct dm_target *ti;
  63. struct request *orig, clone;
  64. int error;
  65. union map_info info;
  66. };
  67. /*
  68. * For request-based dm.
  69. * One of these is allocated per bio.
  70. */
  71. struct dm_rq_clone_bio_info {
  72. struct bio *orig;
  73. struct dm_rq_target_io *tio;
  74. };
  75. union map_info *dm_get_mapinfo(struct bio *bio)
  76. {
  77. if (bio && bio->bi_private)
  78. return &((struct dm_target_io *)bio->bi_private)->info;
  79. return NULL;
  80. }
  81. union map_info *dm_get_rq_mapinfo(struct request *rq)
  82. {
  83. if (rq && rq->end_io_data)
  84. return &((struct dm_rq_target_io *)rq->end_io_data)->info;
  85. return NULL;
  86. }
  87. EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
  88. #define MINOR_ALLOCED ((void *)-1)
  89. /*
  90. * Bits for the md->flags field.
  91. */
  92. #define DMF_BLOCK_IO_FOR_SUSPEND 0
  93. #define DMF_SUSPENDED 1
  94. #define DMF_FROZEN 2
  95. #define DMF_FREEING 3
  96. #define DMF_DELETING 4
  97. #define DMF_NOFLUSH_SUSPENDING 5
  98. #define DMF_MERGE_IS_OPTIONAL 6
  99. /*
  100. * Work processed by per-device workqueue.
  101. */
  102. struct mapped_device {
  103. struct rw_semaphore io_lock;
  104. struct mutex suspend_lock;
  105. rwlock_t map_lock;
  106. atomic_t holders;
  107. atomic_t open_count;
  108. unsigned long flags;
  109. struct request_queue *queue;
  110. unsigned type;
  111. /* Protect queue and type against concurrent access. */
  112. struct mutex type_lock;
  113. struct gendisk *disk;
  114. char name[16];
  115. void *interface_ptr;
  116. /*
  117. * A list of ios that arrived while we were suspended.
  118. */
  119. atomic_t pending[2];
  120. wait_queue_head_t wait;
  121. struct work_struct work;
  122. struct bio_list deferred;
  123. spinlock_t deferred_lock;
  124. /*
  125. * Processing queue (flush)
  126. */
  127. struct workqueue_struct *wq;
  128. /*
  129. * The current mapping.
  130. */
  131. struct dm_table *map;
  132. /*
  133. * io objects are allocated from here.
  134. */
  135. mempool_t *io_pool;
  136. mempool_t *tio_pool;
  137. struct bio_set *bs;
  138. /*
  139. * Event handling.
  140. */
  141. atomic_t event_nr;
  142. wait_queue_head_t eventq;
  143. atomic_t uevent_seq;
  144. struct list_head uevent_list;
  145. spinlock_t uevent_lock; /* Protect access to uevent_list */
  146. /*
  147. * freeze/thaw support require holding onto a super block
  148. */
  149. struct super_block *frozen_sb;
  150. struct block_device *bdev;
  151. /* forced geometry settings */
  152. struct hd_geometry geometry;
  153. /* sysfs handle */
  154. struct kobject kobj;
  155. /* zero-length flush that will be cloned and submitted to targets */
  156. struct bio flush_bio;
  157. };
  158. /*
  159. * For mempools pre-allocation at the table loading time.
  160. */
  161. struct dm_md_mempools {
  162. mempool_t *io_pool;
  163. mempool_t *tio_pool;
  164. struct bio_set *bs;
  165. };
  166. #define MIN_IOS 256
  167. static struct kmem_cache *_io_cache;
  168. static struct kmem_cache *_tio_cache;
  169. static struct kmem_cache *_rq_tio_cache;
  170. static struct kmem_cache *_rq_bio_info_cache;
  171. static int __init local_init(void)
  172. {
  173. int r = -ENOMEM;
  174. /* allocate a slab for the dm_ios */
  175. _io_cache = KMEM_CACHE(dm_io, 0);
  176. if (!_io_cache)
  177. return r;
  178. /* allocate a slab for the target ios */
  179. _tio_cache = KMEM_CACHE(dm_target_io, 0);
  180. if (!_tio_cache)
  181. goto out_free_io_cache;
  182. _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
  183. if (!_rq_tio_cache)
  184. goto out_free_tio_cache;
  185. _rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0);
  186. if (!_rq_bio_info_cache)
  187. goto out_free_rq_tio_cache;
  188. r = dm_uevent_init();
  189. if (r)
  190. goto out_free_rq_bio_info_cache;
  191. _major = major;
  192. r = register_blkdev(_major, _name);
  193. if (r < 0)
  194. goto out_uevent_exit;
  195. if (!_major)
  196. _major = r;
  197. return 0;
  198. out_uevent_exit:
  199. dm_uevent_exit();
  200. out_free_rq_bio_info_cache:
  201. kmem_cache_destroy(_rq_bio_info_cache);
  202. out_free_rq_tio_cache:
  203. kmem_cache_destroy(_rq_tio_cache);
  204. out_free_tio_cache:
  205. kmem_cache_destroy(_tio_cache);
  206. out_free_io_cache:
  207. kmem_cache_destroy(_io_cache);
  208. return r;
  209. }
  210. static void local_exit(void)
  211. {
  212. kmem_cache_destroy(_rq_bio_info_cache);
  213. kmem_cache_destroy(_rq_tio_cache);
  214. kmem_cache_destroy(_tio_cache);
  215. kmem_cache_destroy(_io_cache);
  216. unregister_blkdev(_major, _name);
  217. dm_uevent_exit();
  218. _major = 0;
  219. DMINFO("cleaned up");
  220. }
  221. static int (*_inits[])(void) __initdata = {
  222. local_init,
  223. dm_target_init,
  224. dm_linear_init,
  225. dm_stripe_init,
  226. dm_io_init,
  227. dm_kcopyd_init,
  228. dm_interface_init,
  229. };
  230. static void (*_exits[])(void) = {
  231. local_exit,
  232. dm_target_exit,
  233. dm_linear_exit,
  234. dm_stripe_exit,
  235. dm_io_exit,
  236. dm_kcopyd_exit,
  237. dm_interface_exit,
  238. };
  239. static int __init dm_init(void)
  240. {
  241. const int count = ARRAY_SIZE(_inits);
  242. int r, i;
  243. for (i = 0; i < count; i++) {
  244. r = _inits[i]();
  245. if (r)
  246. goto bad;
  247. }
  248. return 0;
  249. bad:
  250. while (i--)
  251. _exits[i]();
  252. return r;
  253. }
  254. static void __exit dm_exit(void)
  255. {
  256. int i = ARRAY_SIZE(_exits);
  257. while (i--)
  258. _exits[i]();
  259. /*
  260. * Should be empty by this point.
  261. */
  262. idr_remove_all(&_minor_idr);
  263. idr_destroy(&_minor_idr);
  264. }
  265. /*
  266. * Block device functions
  267. */
  268. int dm_deleting_md(struct mapped_device *md)
  269. {
  270. return test_bit(DMF_DELETING, &md->flags);
  271. }
  272. static int dm_blk_open(struct block_device *bdev, fmode_t mode)
  273. {
  274. struct mapped_device *md;
  275. spin_lock(&_minor_lock);
  276. md = bdev->bd_disk->private_data;
  277. if (!md)
  278. goto out;
  279. if (test_bit(DMF_FREEING, &md->flags) ||
  280. dm_deleting_md(md)) {
  281. md = NULL;
  282. goto out;
  283. }
  284. dm_get(md);
  285. atomic_inc(&md->open_count);
  286. out:
  287. spin_unlock(&_minor_lock);
  288. return md ? 0 : -ENXIO;
  289. }
  290. static int dm_blk_close(struct gendisk *disk, fmode_t mode)
  291. {
  292. struct mapped_device *md = disk->private_data;
  293. spin_lock(&_minor_lock);
  294. atomic_dec(&md->open_count);
  295. dm_put(md);
  296. spin_unlock(&_minor_lock);
  297. return 0;
  298. }
  299. int dm_open_count(struct mapped_device *md)
  300. {
  301. return atomic_read(&md->open_count);
  302. }
  303. /*
  304. * Guarantees nothing is using the device before it's deleted.
  305. */
  306. int dm_lock_for_deletion(struct mapped_device *md)
  307. {
  308. int r = 0;
  309. spin_lock(&_minor_lock);
  310. if (dm_open_count(md))
  311. r = -EBUSY;
  312. else
  313. set_bit(DMF_DELETING, &md->flags);
  314. spin_unlock(&_minor_lock);
  315. return r;
  316. }
  317. static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  318. {
  319. struct mapped_device *md = bdev->bd_disk->private_data;
  320. return dm_get_geometry(md, geo);
  321. }
  322. static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
  323. unsigned int cmd, unsigned long arg)
  324. {
  325. struct mapped_device *md = bdev->bd_disk->private_data;
  326. struct dm_table *map = dm_get_live_table(md);
  327. struct dm_target *tgt;
  328. int r = -ENOTTY;
  329. if (!map || !dm_table_get_size(map))
  330. goto out;
  331. /* We only support devices that have a single target */
  332. if (dm_table_get_num_targets(map) != 1)
  333. goto out;
  334. tgt = dm_table_get_target(map, 0);
  335. if (dm_suspended_md(md)) {
  336. r = -EAGAIN;
  337. goto out;
  338. }
  339. if (tgt->type->ioctl)
  340. r = tgt->type->ioctl(tgt, cmd, arg);
  341. out:
  342. dm_table_put(map);
  343. return r;
  344. }
  345. static struct dm_io *alloc_io(struct mapped_device *md)
  346. {
  347. return mempool_alloc(md->io_pool, GFP_NOIO);
  348. }
  349. static void free_io(struct mapped_device *md, struct dm_io *io)
  350. {
  351. mempool_free(io, md->io_pool);
  352. }
  353. static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
  354. {
  355. mempool_free(tio, md->tio_pool);
  356. }
  357. static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
  358. gfp_t gfp_mask)
  359. {
  360. return mempool_alloc(md->tio_pool, gfp_mask);
  361. }
  362. static void free_rq_tio(struct dm_rq_target_io *tio)
  363. {
  364. mempool_free(tio, tio->md->tio_pool);
  365. }
  366. static struct dm_rq_clone_bio_info *alloc_bio_info(struct mapped_device *md)
  367. {
  368. return mempool_alloc(md->io_pool, GFP_ATOMIC);
  369. }
  370. static void free_bio_info(struct dm_rq_clone_bio_info *info)
  371. {
  372. mempool_free(info, info->tio->md->io_pool);
  373. }
  374. static int md_in_flight(struct mapped_device *md)
  375. {
  376. return atomic_read(&md->pending[READ]) +
  377. atomic_read(&md->pending[WRITE]);
  378. }
  379. static void start_io_acct(struct dm_io *io)
  380. {
  381. struct mapped_device *md = io->md;
  382. int cpu;
  383. int rw = bio_data_dir(io->bio);
  384. io->start_time = jiffies;
  385. cpu = part_stat_lock();
  386. part_round_stats(cpu, &dm_disk(md)->part0);
  387. part_stat_unlock();
  388. atomic_set(&dm_disk(md)->part0.in_flight[rw],
  389. atomic_inc_return(&md->pending[rw]));
  390. }
  391. static void end_io_acct(struct dm_io *io)
  392. {
  393. struct mapped_device *md = io->md;
  394. struct bio *bio = io->bio;
  395. unsigned long duration = jiffies - io->start_time;
  396. int pending, cpu;
  397. int rw = bio_data_dir(bio);
  398. cpu = part_stat_lock();
  399. part_round_stats(cpu, &dm_disk(md)->part0);
  400. part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
  401. part_stat_unlock();
  402. /*
  403. * After this is decremented the bio must not be touched if it is
  404. * a flush.
  405. */
  406. pending = atomic_dec_return(&md->pending[rw]);
  407. atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
  408. pending += atomic_read(&md->pending[rw^0x1]);
  409. /* nudge anyone waiting on suspend queue */
  410. if (!pending)
  411. wake_up(&md->wait);
  412. }
  413. /*
  414. * Add the bio to the list of deferred io.
  415. */
  416. static void queue_io(struct mapped_device *md, struct bio *bio)
  417. {
  418. unsigned long flags;
  419. spin_lock_irqsave(&md->deferred_lock, flags);
  420. bio_list_add(&md->deferred, bio);
  421. spin_unlock_irqrestore(&md->deferred_lock, flags);
  422. queue_work(md->wq, &md->work);
  423. }
  424. /*
  425. * Everyone (including functions in this file), should use this
  426. * function to access the md->map field, and make sure they call
  427. * dm_table_put() when finished.
  428. */
  429. struct dm_table *dm_get_live_table(struct mapped_device *md)
  430. {
  431. struct dm_table *t;
  432. unsigned long flags;
  433. read_lock_irqsave(&md->map_lock, flags);
  434. t = md->map;
  435. if (t)
  436. dm_table_get(t);
  437. read_unlock_irqrestore(&md->map_lock, flags);
  438. return t;
  439. }
  440. /*
  441. * Get the geometry associated with a dm device
  442. */
  443. int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
  444. {
  445. *geo = md->geometry;
  446. return 0;
  447. }
  448. /*
  449. * Set the geometry of a device.
  450. */
  451. int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
  452. {
  453. sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
  454. if (geo->start > sz) {
  455. DMWARN("Start sector is beyond the geometry limits.");
  456. return -EINVAL;
  457. }
  458. md->geometry = *geo;
  459. return 0;
  460. }
  461. /*-----------------------------------------------------------------
  462. * CRUD START:
  463. * A more elegant soln is in the works that uses the queue
  464. * merge fn, unfortunately there are a couple of changes to
  465. * the block layer that I want to make for this. So in the
  466. * interests of getting something for people to use I give
  467. * you this clearly demarcated crap.
  468. *---------------------------------------------------------------*/
  469. static int __noflush_suspending(struct mapped_device *md)
  470. {
  471. return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  472. }
  473. /*
  474. * Decrements the number of outstanding ios that a bio has been
  475. * cloned into, completing the original io if necc.
  476. */
  477. static void dec_pending(struct dm_io *io, int error)
  478. {
  479. unsigned long flags;
  480. int io_error;
  481. struct bio *bio;
  482. struct mapped_device *md = io->md;
  483. /* Push-back supersedes any I/O errors */
  484. if (unlikely(error)) {
  485. spin_lock_irqsave(&io->endio_lock, flags);
  486. if (!(io->error > 0 && __noflush_suspending(md)))
  487. io->error = error;
  488. spin_unlock_irqrestore(&io->endio_lock, flags);
  489. }
  490. if (atomic_dec_and_test(&io->io_count)) {
  491. if (io->error == DM_ENDIO_REQUEUE) {
  492. /*
  493. * Target requested pushing back the I/O.
  494. */
  495. spin_lock_irqsave(&md->deferred_lock, flags);
  496. if (__noflush_suspending(md))
  497. bio_list_add_head(&md->deferred, io->bio);
  498. else
  499. /* noflush suspend was interrupted. */
  500. io->error = -EIO;
  501. spin_unlock_irqrestore(&md->deferred_lock, flags);
  502. }
  503. io_error = io->error;
  504. bio = io->bio;
  505. end_io_acct(io);
  506. free_io(md, io);
  507. if (io_error == DM_ENDIO_REQUEUE)
  508. return;
  509. if ((bio->bi_rw & REQ_FLUSH) && bio->bi_size) {
  510. /*
  511. * Preflush done for flush with data, reissue
  512. * without REQ_FLUSH.
  513. */
  514. bio->bi_rw &= ~REQ_FLUSH;
  515. queue_io(md, bio);
  516. } else {
  517. /* done with normal IO or empty flush */
  518. trace_block_bio_complete(md->queue, bio, io_error);
  519. bio_endio(bio, io_error);
  520. }
  521. }
  522. }
  523. static void clone_endio(struct bio *bio, int error)
  524. {
  525. int r = 0;
  526. struct dm_target_io *tio = bio->bi_private;
  527. struct dm_io *io = tio->io;
  528. struct mapped_device *md = tio->io->md;
  529. dm_endio_fn endio = tio->ti->type->end_io;
  530. if (!bio_flagged(bio, BIO_UPTODATE) && !error)
  531. error = -EIO;
  532. if (endio) {
  533. r = endio(tio->ti, bio, error, &tio->info);
  534. if (r < 0 || r == DM_ENDIO_REQUEUE)
  535. /*
  536. * error and requeue request are handled
  537. * in dec_pending().
  538. */
  539. error = r;
  540. else if (r == DM_ENDIO_INCOMPLETE)
  541. /* The target will handle the io */
  542. return;
  543. else if (r) {
  544. DMWARN("unimplemented target endio return value: %d", r);
  545. BUG();
  546. }
  547. }
  548. /*
  549. * Store md for cleanup instead of tio which is about to get freed.
  550. */
  551. bio->bi_private = md->bs;
  552. free_tio(md, tio);
  553. bio_put(bio);
  554. dec_pending(io, error);
  555. }
  556. /*
  557. * Partial completion handling for request-based dm
  558. */
  559. static void end_clone_bio(struct bio *clone, int error)
  560. {
  561. struct dm_rq_clone_bio_info *info = clone->bi_private;
  562. struct dm_rq_target_io *tio = info->tio;
  563. struct bio *bio = info->orig;
  564. unsigned int nr_bytes = info->orig->bi_size;
  565. bio_put(clone);
  566. if (tio->error)
  567. /*
  568. * An error has already been detected on the request.
  569. * Once error occurred, just let clone->end_io() handle
  570. * the remainder.
  571. */
  572. return;
  573. else if (error) {
  574. /*
  575. * Don't notice the error to the upper layer yet.
  576. * The error handling decision is made by the target driver,
  577. * when the request is completed.
  578. */
  579. tio->error = error;
  580. return;
  581. }
  582. /*
  583. * I/O for the bio successfully completed.
  584. * Notice the data completion to the upper layer.
  585. */
  586. /*
  587. * bios are processed from the head of the list.
  588. * So the completing bio should always be rq->bio.
  589. * If it's not, something wrong is happening.
  590. */
  591. if (tio->orig->bio != bio)
  592. DMERR("bio completion is going in the middle of the request");
  593. /*
  594. * Update the original request.
  595. * Do not use blk_end_request() here, because it may complete
  596. * the original request before the clone, and break the ordering.
  597. */
  598. blk_update_request(tio->orig, 0, nr_bytes);
  599. }
  600. /*
  601. * Don't touch any member of the md after calling this function because
  602. * the md may be freed in dm_put() at the end of this function.
  603. * Or do dm_get() before calling this function and dm_put() later.
  604. */
  605. static void rq_completed(struct mapped_device *md, int rw, int run_queue)
  606. {
  607. atomic_dec(&md->pending[rw]);
  608. /* nudge anyone waiting on suspend queue */
  609. if (!md_in_flight(md))
  610. wake_up(&md->wait);
  611. if (run_queue)
  612. blk_run_queue(md->queue);
  613. /*
  614. * dm_put() must be at the end of this function. See the comment above
  615. */
  616. dm_put(md);
  617. }
  618. static void free_rq_clone(struct request *clone)
  619. {
  620. struct dm_rq_target_io *tio = clone->end_io_data;
  621. blk_rq_unprep_clone(clone);
  622. free_rq_tio(tio);
  623. }
  624. /*
  625. * Complete the clone and the original request.
  626. * Must be called without queue lock.
  627. */
  628. static void dm_end_request(struct request *clone, int error)
  629. {
  630. int rw = rq_data_dir(clone);
  631. struct dm_rq_target_io *tio = clone->end_io_data;
  632. struct mapped_device *md = tio->md;
  633. struct request *rq = tio->orig;
  634. if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
  635. rq->errors = clone->errors;
  636. rq->resid_len = clone->resid_len;
  637. if (rq->sense)
  638. /*
  639. * We are using the sense buffer of the original
  640. * request.
  641. * So setting the length of the sense data is enough.
  642. */
  643. rq->sense_len = clone->sense_len;
  644. }
  645. free_rq_clone(clone);
  646. blk_end_request_all(rq, error);
  647. rq_completed(md, rw, true);
  648. }
  649. static void dm_unprep_request(struct request *rq)
  650. {
  651. struct request *clone = rq->special;
  652. rq->special = NULL;
  653. rq->cmd_flags &= ~REQ_DONTPREP;
  654. free_rq_clone(clone);
  655. }
  656. /*
  657. * Requeue the original request of a clone.
  658. */
  659. void dm_requeue_unmapped_request(struct request *clone)
  660. {
  661. int rw = rq_data_dir(clone);
  662. struct dm_rq_target_io *tio = clone->end_io_data;
  663. struct mapped_device *md = tio->md;
  664. struct request *rq = tio->orig;
  665. struct request_queue *q = rq->q;
  666. unsigned long flags;
  667. dm_unprep_request(rq);
  668. spin_lock_irqsave(q->queue_lock, flags);
  669. blk_requeue_request(q, rq);
  670. spin_unlock_irqrestore(q->queue_lock, flags);
  671. rq_completed(md, rw, 0);
  672. }
  673. EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
  674. static void __stop_queue(struct request_queue *q)
  675. {
  676. blk_stop_queue(q);
  677. }
  678. static void stop_queue(struct request_queue *q)
  679. {
  680. unsigned long flags;
  681. spin_lock_irqsave(q->queue_lock, flags);
  682. __stop_queue(q);
  683. spin_unlock_irqrestore(q->queue_lock, flags);
  684. }
  685. static void __start_queue(struct request_queue *q)
  686. {
  687. if (blk_queue_stopped(q))
  688. blk_start_queue(q);
  689. }
  690. static void start_queue(struct request_queue *q)
  691. {
  692. unsigned long flags;
  693. spin_lock_irqsave(q->queue_lock, flags);
  694. __start_queue(q);
  695. spin_unlock_irqrestore(q->queue_lock, flags);
  696. }
  697. static void dm_done(struct request *clone, int error, bool mapped)
  698. {
  699. int r = error;
  700. struct dm_rq_target_io *tio = clone->end_io_data;
  701. dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io;
  702. if (mapped && rq_end_io)
  703. r = rq_end_io(tio->ti, clone, error, &tio->info);
  704. if (r <= 0)
  705. /* The target wants to complete the I/O */
  706. dm_end_request(clone, r);
  707. else if (r == DM_ENDIO_INCOMPLETE)
  708. /* The target will handle the I/O */
  709. return;
  710. else if (r == DM_ENDIO_REQUEUE)
  711. /* The target wants to requeue the I/O */
  712. dm_requeue_unmapped_request(clone);
  713. else {
  714. DMWARN("unimplemented target endio return value: %d", r);
  715. BUG();
  716. }
  717. }
  718. /*
  719. * Request completion handler for request-based dm
  720. */
  721. static void dm_softirq_done(struct request *rq)
  722. {
  723. bool mapped = true;
  724. struct request *clone = rq->completion_data;
  725. struct dm_rq_target_io *tio = clone->end_io_data;
  726. if (rq->cmd_flags & REQ_FAILED)
  727. mapped = false;
  728. dm_done(clone, tio->error, mapped);
  729. }
  730. /*
  731. * Complete the clone and the original request with the error status
  732. * through softirq context.
  733. */
  734. static void dm_complete_request(struct request *clone, int error)
  735. {
  736. struct dm_rq_target_io *tio = clone->end_io_data;
  737. struct request *rq = tio->orig;
  738. tio->error = error;
  739. rq->completion_data = clone;
  740. blk_complete_request(rq);
  741. }
  742. /*
  743. * Complete the not-mapped clone and the original request with the error status
  744. * through softirq context.
  745. * Target's rq_end_io() function isn't called.
  746. * This may be used when the target's map_rq() function fails.
  747. */
  748. void dm_kill_unmapped_request(struct request *clone, int error)
  749. {
  750. struct dm_rq_target_io *tio = clone->end_io_data;
  751. struct request *rq = tio->orig;
  752. rq->cmd_flags |= REQ_FAILED;
  753. dm_complete_request(clone, error);
  754. }
  755. EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
  756. /*
  757. * Called with the queue lock held
  758. */
  759. static void end_clone_request(struct request *clone, int error)
  760. {
  761. /*
  762. * For just cleaning up the information of the queue in which
  763. * the clone was dispatched.
  764. * The clone is *NOT* freed actually here because it is alloced from
  765. * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
  766. */
  767. __blk_put_request(clone->q, clone);
  768. /*
  769. * Actual request completion is done in a softirq context which doesn't
  770. * hold the queue lock. Otherwise, deadlock could occur because:
  771. * - another request may be submitted by the upper level driver
  772. * of the stacking during the completion
  773. * - the submission which requires queue lock may be done
  774. * against this queue
  775. */
  776. dm_complete_request(clone, error);
  777. }
  778. /*
  779. * Return maximum size of I/O possible at the supplied sector up to the current
  780. * target boundary.
  781. */
  782. static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
  783. {
  784. sector_t target_offset = dm_target_offset(ti, sector);
  785. return ti->len - target_offset;
  786. }
  787. static sector_t max_io_len(sector_t sector, struct dm_target *ti)
  788. {
  789. sector_t len = max_io_len_target_boundary(sector, ti);
  790. /*
  791. * Does the target need to split even further ?
  792. */
  793. if (ti->split_io) {
  794. sector_t boundary;
  795. sector_t offset = dm_target_offset(ti, sector);
  796. boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
  797. - offset;
  798. if (len > boundary)
  799. len = boundary;
  800. }
  801. return len;
  802. }
  803. static void __map_bio(struct dm_target *ti, struct bio *clone,
  804. struct dm_target_io *tio)
  805. {
  806. int r;
  807. sector_t sector;
  808. struct mapped_device *md;
  809. clone->bi_end_io = clone_endio;
  810. clone->bi_private = tio;
  811. /*
  812. * Map the clone. If r == 0 we don't need to do
  813. * anything, the target has assumed ownership of
  814. * this io.
  815. */
  816. atomic_inc(&tio->io->io_count);
  817. sector = clone->bi_sector;
  818. r = ti->type->map(ti, clone, &tio->info);
  819. if (r == DM_MAPIO_REMAPPED) {
  820. /* the bio has been remapped so dispatch it */
  821. trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
  822. tio->io->bio->bi_bdev->bd_dev, sector);
  823. generic_make_request(clone);
  824. } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
  825. /* error the io and bail out, or requeue it if needed */
  826. md = tio->io->md;
  827. dec_pending(tio->io, r);
  828. /*
  829. * Store bio_set for cleanup.
  830. */
  831. clone->bi_private = md->bs;
  832. bio_put(clone);
  833. free_tio(md, tio);
  834. } else if (r) {
  835. DMWARN("unimplemented target map return value: %d", r);
  836. BUG();
  837. }
  838. }
  839. struct clone_info {
  840. struct mapped_device *md;
  841. struct dm_table *map;
  842. struct bio *bio;
  843. struct dm_io *io;
  844. sector_t sector;
  845. sector_t sector_count;
  846. unsigned short idx;
  847. };
  848. static void dm_bio_destructor(struct bio *bio)
  849. {
  850. struct bio_set *bs = bio->bi_private;
  851. bio_free(bio, bs);
  852. }
  853. /*
  854. * Creates a little bio that just does part of a bvec.
  855. */
  856. static struct bio *split_bvec(struct bio *bio, sector_t sector,
  857. unsigned short idx, unsigned int offset,
  858. unsigned int len, struct bio_set *bs)
  859. {
  860. struct bio *clone;
  861. struct bio_vec *bv = bio->bi_io_vec + idx;
  862. clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
  863. clone->bi_destructor = dm_bio_destructor;
  864. *clone->bi_io_vec = *bv;
  865. clone->bi_sector = sector;
  866. clone->bi_bdev = bio->bi_bdev;
  867. clone->bi_rw = bio->bi_rw;
  868. clone->bi_vcnt = 1;
  869. clone->bi_size = to_bytes(len);
  870. clone->bi_io_vec->bv_offset = offset;
  871. clone->bi_io_vec->bv_len = clone->bi_size;
  872. clone->bi_flags |= 1 << BIO_CLONED;
  873. if (bio_integrity(bio)) {
  874. bio_integrity_clone(clone, bio, GFP_NOIO, bs);
  875. bio_integrity_trim(clone,
  876. bio_sector_offset(bio, idx, offset), len);
  877. }
  878. return clone;
  879. }
  880. /*
  881. * Creates a bio that consists of range of complete bvecs.
  882. */
  883. static struct bio *clone_bio(struct bio *bio, sector_t sector,
  884. unsigned short idx, unsigned short bv_count,
  885. unsigned int len, struct bio_set *bs)
  886. {
  887. struct bio *clone;
  888. clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
  889. __bio_clone(clone, bio);
  890. clone->bi_destructor = dm_bio_destructor;
  891. clone->bi_sector = sector;
  892. clone->bi_idx = idx;
  893. clone->bi_vcnt = idx + bv_count;
  894. clone->bi_size = to_bytes(len);
  895. clone->bi_flags &= ~(1 << BIO_SEG_VALID);
  896. if (bio_integrity(bio)) {
  897. bio_integrity_clone(clone, bio, GFP_NOIO, bs);
  898. if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
  899. bio_integrity_trim(clone,
  900. bio_sector_offset(bio, idx, 0), len);
  901. }
  902. return clone;
  903. }
  904. static struct dm_target_io *alloc_tio(struct clone_info *ci,
  905. struct dm_target *ti)
  906. {
  907. struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO);
  908. tio->io = ci->io;
  909. tio->ti = ti;
  910. memset(&tio->info, 0, sizeof(tio->info));
  911. return tio;
  912. }
  913. static void __issue_target_request(struct clone_info *ci, struct dm_target *ti,
  914. unsigned request_nr, sector_t len)
  915. {
  916. struct dm_target_io *tio = alloc_tio(ci, ti);
  917. struct bio *clone;
  918. tio->info.target_request_nr = request_nr;
  919. /*
  920. * Discard requests require the bio's inline iovecs be initialized.
  921. * ci->bio->bi_max_vecs is BIO_INLINE_VECS anyway, for both flush
  922. * and discard, so no need for concern about wasted bvec allocations.
  923. */
  924. clone = bio_alloc_bioset(GFP_NOIO, ci->bio->bi_max_vecs, ci->md->bs);
  925. __bio_clone(clone, ci->bio);
  926. clone->bi_destructor = dm_bio_destructor;
  927. if (len) {
  928. clone->bi_sector = ci->sector;
  929. clone->bi_size = to_bytes(len);
  930. }
  931. __map_bio(ti, clone, tio);
  932. }
  933. static void __issue_target_requests(struct clone_info *ci, struct dm_target *ti,
  934. unsigned num_requests, sector_t len)
  935. {
  936. unsigned request_nr;
  937. for (request_nr = 0; request_nr < num_requests; request_nr++)
  938. __issue_target_request(ci, ti, request_nr, len);
  939. }
  940. static int __clone_and_map_empty_flush(struct clone_info *ci)
  941. {
  942. unsigned target_nr = 0;
  943. struct dm_target *ti;
  944. BUG_ON(bio_has_data(ci->bio));
  945. while ((ti = dm_table_get_target(ci->map, target_nr++)))
  946. __issue_target_requests(ci, ti, ti->num_flush_requests, 0);
  947. return 0;
  948. }
  949. /*
  950. * Perform all io with a single clone.
  951. */
  952. static void __clone_and_map_simple(struct clone_info *ci, struct dm_target *ti)
  953. {
  954. struct bio *clone, *bio = ci->bio;
  955. struct dm_target_io *tio;
  956. tio = alloc_tio(ci, ti);
  957. clone = clone_bio(bio, ci->sector, ci->idx,
  958. bio->bi_vcnt - ci->idx, ci->sector_count,
  959. ci->md->bs);
  960. __map_bio(ti, clone, tio);
  961. ci->sector_count = 0;
  962. }
  963. static int __clone_and_map_discard(struct clone_info *ci)
  964. {
  965. struct dm_target *ti;
  966. sector_t len;
  967. do {
  968. ti = dm_table_find_target(ci->map, ci->sector);
  969. if (!dm_target_is_valid(ti))
  970. return -EIO;
  971. /*
  972. * Even though the device advertised discard support,
  973. * that does not mean every target supports it, and
  974. * reconfiguration might also have changed that since the
  975. * check was performed.
  976. */
  977. if (!ti->num_discard_requests)
  978. return -EOPNOTSUPP;
  979. len = min(ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
  980. __issue_target_requests(ci, ti, ti->num_discard_requests, len);
  981. ci->sector += len;
  982. } while (ci->sector_count -= len);
  983. return 0;
  984. }
  985. static int __clone_and_map(struct clone_info *ci)
  986. {
  987. struct bio *clone, *bio = ci->bio;
  988. struct dm_target *ti;
  989. sector_t len = 0, max;
  990. struct dm_target_io *tio;
  991. if (unlikely(bio->bi_rw & REQ_DISCARD))
  992. return __clone_and_map_discard(ci);
  993. ti = dm_table_find_target(ci->map, ci->sector);
  994. if (!dm_target_is_valid(ti))
  995. return -EIO;
  996. max = max_io_len(ci->sector, ti);
  997. if (ci->sector_count <= max) {
  998. /*
  999. * Optimise for the simple case where we can do all of
  1000. * the remaining io with a single clone.
  1001. */
  1002. __clone_and_map_simple(ci, ti);
  1003. } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
  1004. /*
  1005. * There are some bvecs that don't span targets.
  1006. * Do as many of these as possible.
  1007. */
  1008. int i;
  1009. sector_t remaining = max;
  1010. sector_t bv_len;
  1011. for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
  1012. bv_len = to_sector(bio->bi_io_vec[i].bv_len);
  1013. if (bv_len > remaining)
  1014. break;
  1015. remaining -= bv_len;
  1016. len += bv_len;
  1017. }
  1018. tio = alloc_tio(ci, ti);
  1019. clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
  1020. ci->md->bs);
  1021. __map_bio(ti, clone, tio);
  1022. ci->sector += len;
  1023. ci->sector_count -= len;
  1024. ci->idx = i;
  1025. } else {
  1026. /*
  1027. * Handle a bvec that must be split between two or more targets.
  1028. */
  1029. struct bio_vec *bv = bio->bi_io_vec + ci->idx;
  1030. sector_t remaining = to_sector(bv->bv_len);
  1031. unsigned int offset = 0;
  1032. do {
  1033. if (offset) {
  1034. ti = dm_table_find_target(ci->map, ci->sector);
  1035. if (!dm_target_is_valid(ti))
  1036. return -EIO;
  1037. max = max_io_len(ci->sector, ti);
  1038. }
  1039. len = min(remaining, max);
  1040. tio = alloc_tio(ci, ti);
  1041. clone = split_bvec(bio, ci->sector, ci->idx,
  1042. bv->bv_offset + offset, len,
  1043. ci->md->bs);
  1044. __map_bio(ti, clone, tio);
  1045. ci->sector += len;
  1046. ci->sector_count -= len;
  1047. offset += to_bytes(len);
  1048. } while (remaining -= len);
  1049. ci->idx++;
  1050. }
  1051. return 0;
  1052. }
  1053. /*
  1054. * Split the bio into several clones and submit it to targets.
  1055. */
  1056. static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
  1057. {
  1058. struct clone_info ci;
  1059. int error = 0;
  1060. ci.map = dm_get_live_table(md);
  1061. if (unlikely(!ci.map)) {
  1062. bio_io_error(bio);
  1063. return;
  1064. }
  1065. ci.md = md;
  1066. ci.io = alloc_io(md);
  1067. ci.io->error = 0;
  1068. atomic_set(&ci.io->io_count, 1);
  1069. ci.io->bio = bio;
  1070. ci.io->md = md;
  1071. spin_lock_init(&ci.io->endio_lock);
  1072. ci.sector = bio->bi_sector;
  1073. ci.idx = bio->bi_idx;
  1074. start_io_acct(ci.io);
  1075. if (bio->bi_rw & REQ_FLUSH) {
  1076. ci.bio = &ci.md->flush_bio;
  1077. ci.sector_count = 0;
  1078. error = __clone_and_map_empty_flush(&ci);
  1079. /* dec_pending submits any data associated with flush */
  1080. } else {
  1081. ci.bio = bio;
  1082. ci.sector_count = bio_sectors(bio);
  1083. while (ci.sector_count && !error)
  1084. error = __clone_and_map(&ci);
  1085. }
  1086. /* drop the extra reference count */
  1087. dec_pending(ci.io, error);
  1088. dm_table_put(ci.map);
  1089. }
  1090. /*-----------------------------------------------------------------
  1091. * CRUD END
  1092. *---------------------------------------------------------------*/
  1093. static int dm_merge_bvec(struct request_queue *q,
  1094. struct bvec_merge_data *bvm,
  1095. struct bio_vec *biovec)
  1096. {
  1097. struct mapped_device *md = q->queuedata;
  1098. struct dm_table *map = dm_get_live_table(md);
  1099. struct dm_target *ti;
  1100. sector_t max_sectors;
  1101. int max_size = 0;
  1102. if (unlikely(!map))
  1103. goto out;
  1104. ti = dm_table_find_target(map, bvm->bi_sector);
  1105. if (!dm_target_is_valid(ti))
  1106. goto out_table;
  1107. /*
  1108. * Find maximum amount of I/O that won't need splitting
  1109. */
  1110. max_sectors = min(max_io_len(bvm->bi_sector, ti),
  1111. (sector_t) BIO_MAX_SECTORS);
  1112. max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
  1113. if (max_size < 0)
  1114. max_size = 0;
  1115. /*
  1116. * merge_bvec_fn() returns number of bytes
  1117. * it can accept at this offset
  1118. * max is precomputed maximal io size
  1119. */
  1120. if (max_size && ti->type->merge)
  1121. max_size = ti->type->merge(ti, bvm, biovec, max_size);
  1122. /*
  1123. * If the target doesn't support merge method and some of the devices
  1124. * provided their merge_bvec method (we know this by looking at
  1125. * queue_max_hw_sectors), then we can't allow bios with multiple vector
  1126. * entries. So always set max_size to 0, and the code below allows
  1127. * just one page.
  1128. */
  1129. else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
  1130. max_size = 0;
  1131. out_table:
  1132. dm_table_put(map);
  1133. out:
  1134. /*
  1135. * Always allow an entire first page
  1136. */
  1137. if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
  1138. max_size = biovec->bv_len;
  1139. return max_size;
  1140. }
  1141. /*
  1142. * The request function that just remaps the bio built up by
  1143. * dm_merge_bvec.
  1144. */
  1145. static void _dm_request(struct request_queue *q, struct bio *bio)
  1146. {
  1147. int rw = bio_data_dir(bio);
  1148. struct mapped_device *md = q->queuedata;
  1149. int cpu;
  1150. down_read(&md->io_lock);
  1151. cpu = part_stat_lock();
  1152. part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
  1153. part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
  1154. part_stat_unlock();
  1155. /* if we're suspended, we have to queue this io for later */
  1156. if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
  1157. up_read(&md->io_lock);
  1158. if (bio_rw(bio) != READA)
  1159. queue_io(md, bio);
  1160. else
  1161. bio_io_error(bio);
  1162. return;
  1163. }
  1164. __split_and_process_bio(md, bio);
  1165. up_read(&md->io_lock);
  1166. return;
  1167. }
  1168. static int dm_request_based(struct mapped_device *md)
  1169. {
  1170. return blk_queue_stackable(md->queue);
  1171. }
  1172. static void dm_request(struct request_queue *q, struct bio *bio)
  1173. {
  1174. struct mapped_device *md = q->queuedata;
  1175. if (dm_request_based(md))
  1176. blk_queue_bio(q, bio);
  1177. else
  1178. _dm_request(q, bio);
  1179. }
  1180. void dm_dispatch_request(struct request *rq)
  1181. {
  1182. int r;
  1183. if (blk_queue_io_stat(rq->q))
  1184. rq->cmd_flags |= REQ_IO_STAT;
  1185. rq->start_time = jiffies;
  1186. r = blk_insert_cloned_request(rq->q, rq);
  1187. if (r)
  1188. dm_complete_request(rq, r);
  1189. }
  1190. EXPORT_SYMBOL_GPL(dm_dispatch_request);
  1191. static void dm_rq_bio_destructor(struct bio *bio)
  1192. {
  1193. struct dm_rq_clone_bio_info *info = bio->bi_private;
  1194. struct mapped_device *md = info->tio->md;
  1195. free_bio_info(info);
  1196. bio_free(bio, md->bs);
  1197. }
  1198. static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
  1199. void *data)
  1200. {
  1201. struct dm_rq_target_io *tio = data;
  1202. struct mapped_device *md = tio->md;
  1203. struct dm_rq_clone_bio_info *info = alloc_bio_info(md);
  1204. if (!info)
  1205. return -ENOMEM;
  1206. info->orig = bio_orig;
  1207. info->tio = tio;
  1208. bio->bi_end_io = end_clone_bio;
  1209. bio->bi_private = info;
  1210. bio->bi_destructor = dm_rq_bio_destructor;
  1211. return 0;
  1212. }
  1213. static int setup_clone(struct request *clone, struct request *rq,
  1214. struct dm_rq_target_io *tio)
  1215. {
  1216. int r;
  1217. r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
  1218. dm_rq_bio_constructor, tio);
  1219. if (r)
  1220. return r;
  1221. clone->cmd = rq->cmd;
  1222. clone->cmd_len = rq->cmd_len;
  1223. clone->sense = rq->sense;
  1224. clone->buffer = rq->buffer;
  1225. clone->end_io = end_clone_request;
  1226. clone->end_io_data = tio;
  1227. return 0;
  1228. }
  1229. static struct request *clone_rq(struct request *rq, struct mapped_device *md,
  1230. gfp_t gfp_mask)
  1231. {
  1232. struct request *clone;
  1233. struct dm_rq_target_io *tio;
  1234. tio = alloc_rq_tio(md, gfp_mask);
  1235. if (!tio)
  1236. return NULL;
  1237. tio->md = md;
  1238. tio->ti = NULL;
  1239. tio->orig = rq;
  1240. tio->error = 0;
  1241. memset(&tio->info, 0, sizeof(tio->info));
  1242. clone = &tio->clone;
  1243. if (setup_clone(clone, rq, tio)) {
  1244. /* -ENOMEM */
  1245. free_rq_tio(tio);
  1246. return NULL;
  1247. }
  1248. return clone;
  1249. }
  1250. /*
  1251. * Called with the queue lock held.
  1252. */
  1253. static int dm_prep_fn(struct request_queue *q, struct request *rq)
  1254. {
  1255. struct mapped_device *md = q->queuedata;
  1256. struct request *clone;
  1257. if (unlikely(rq->special)) {
  1258. DMWARN("Already has something in rq->special.");
  1259. return BLKPREP_KILL;
  1260. }
  1261. clone = clone_rq(rq, md, GFP_ATOMIC);
  1262. if (!clone)
  1263. return BLKPREP_DEFER;
  1264. rq->special = clone;
  1265. rq->cmd_flags |= REQ_DONTPREP;
  1266. return BLKPREP_OK;
  1267. }
  1268. /*
  1269. * Returns:
  1270. * 0 : the request has been processed (not requeued)
  1271. * !0 : the request has been requeued
  1272. */
  1273. static int map_request(struct dm_target *ti, struct request *clone,
  1274. struct mapped_device *md)
  1275. {
  1276. int r, requeued = 0;
  1277. struct dm_rq_target_io *tio = clone->end_io_data;
  1278. /*
  1279. * Hold the md reference here for the in-flight I/O.
  1280. * We can't rely on the reference count by device opener,
  1281. * because the device may be closed during the request completion
  1282. * when all bios are completed.
  1283. * See the comment in rq_completed() too.
  1284. */
  1285. dm_get(md);
  1286. tio->ti = ti;
  1287. r = ti->type->map_rq(ti, clone, &tio->info);
  1288. switch (r) {
  1289. case DM_MAPIO_SUBMITTED:
  1290. /* The target has taken the I/O to submit by itself later */
  1291. break;
  1292. case DM_MAPIO_REMAPPED:
  1293. /* The target has remapped the I/O so dispatch it */
  1294. trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
  1295. blk_rq_pos(tio->orig));
  1296. dm_dispatch_request(clone);
  1297. break;
  1298. case DM_MAPIO_REQUEUE:
  1299. /* The target wants to requeue the I/O */
  1300. dm_requeue_unmapped_request(clone);
  1301. requeued = 1;
  1302. break;
  1303. default:
  1304. if (r > 0) {
  1305. DMWARN("unimplemented target map return value: %d", r);
  1306. BUG();
  1307. }
  1308. /* The target wants to complete the I/O */
  1309. dm_kill_unmapped_request(clone, r);
  1310. break;
  1311. }
  1312. return requeued;
  1313. }
  1314. /*
  1315. * q->request_fn for request-based dm.
  1316. * Called with the queue lock held.
  1317. */
  1318. static void dm_request_fn(struct request_queue *q)
  1319. {
  1320. struct mapped_device *md = q->queuedata;
  1321. struct dm_table *map = dm_get_live_table(md);
  1322. struct dm_target *ti;
  1323. struct request *rq, *clone;
  1324. sector_t pos;
  1325. /*
  1326. * For suspend, check blk_queue_stopped() and increment
  1327. * ->pending within a single queue_lock not to increment the
  1328. * number of in-flight I/Os after the queue is stopped in
  1329. * dm_suspend().
  1330. */
  1331. while (!blk_queue_stopped(q)) {
  1332. rq = blk_peek_request(q);
  1333. if (!rq)
  1334. goto delay_and_out;
  1335. /* always use block 0 to find the target for flushes for now */
  1336. pos = 0;
  1337. if (!(rq->cmd_flags & REQ_FLUSH))
  1338. pos = blk_rq_pos(rq);
  1339. ti = dm_table_find_target(map, pos);
  1340. BUG_ON(!dm_target_is_valid(ti));
  1341. if (ti->type->busy && ti->type->busy(ti))
  1342. goto delay_and_out;
  1343. blk_start_request(rq);
  1344. clone = rq->special;
  1345. atomic_inc(&md->pending[rq_data_dir(clone)]);
  1346. spin_unlock(q->queue_lock);
  1347. if (map_request(ti, clone, md))
  1348. goto requeued;
  1349. BUG_ON(!irqs_disabled());
  1350. spin_lock(q->queue_lock);
  1351. }
  1352. goto out;
  1353. requeued:
  1354. BUG_ON(!irqs_disabled());
  1355. spin_lock(q->queue_lock);
  1356. delay_and_out:
  1357. blk_delay_queue(q, HZ / 10);
  1358. out:
  1359. dm_table_put(map);
  1360. return;
  1361. }
  1362. int dm_underlying_device_busy(struct request_queue *q)
  1363. {
  1364. return blk_lld_busy(q);
  1365. }
  1366. EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
  1367. static int dm_lld_busy(struct request_queue *q)
  1368. {
  1369. int r;
  1370. struct mapped_device *md = q->queuedata;
  1371. struct dm_table *map = dm_get_live_table(md);
  1372. if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
  1373. r = 1;
  1374. else
  1375. r = dm_table_any_busy_target(map);
  1376. dm_table_put(map);
  1377. return r;
  1378. }
  1379. static int dm_any_congested(void *congested_data, int bdi_bits)
  1380. {
  1381. int r = bdi_bits;
  1382. struct mapped_device *md = congested_data;
  1383. struct dm_table *map;
  1384. if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
  1385. map = dm_get_live_table(md);
  1386. if (map) {
  1387. /*
  1388. * Request-based dm cares about only own queue for
  1389. * the query about congestion status of request_queue
  1390. */
  1391. if (dm_request_based(md))
  1392. r = md->queue->backing_dev_info.state &
  1393. bdi_bits;
  1394. else
  1395. r = dm_table_any_congested(map, bdi_bits);
  1396. dm_table_put(map);
  1397. }
  1398. }
  1399. return r;
  1400. }
  1401. /*-----------------------------------------------------------------
  1402. * An IDR is used to keep track of allocated minor numbers.
  1403. *---------------------------------------------------------------*/
  1404. static void free_minor(int minor)
  1405. {
  1406. spin_lock(&_minor_lock);
  1407. idr_remove(&_minor_idr, minor);
  1408. spin_unlock(&_minor_lock);
  1409. }
  1410. /*
  1411. * See if the device with a specific minor # is free.
  1412. */
  1413. static int specific_minor(int minor)
  1414. {
  1415. int r, m;
  1416. if (minor >= (1 << MINORBITS))
  1417. return -EINVAL;
  1418. r = idr_pre_get(&_minor_idr, GFP_KERNEL);
  1419. if (!r)
  1420. return -ENOMEM;
  1421. spin_lock(&_minor_lock);
  1422. if (idr_find(&_minor_idr, minor)) {
  1423. r = -EBUSY;
  1424. goto out;
  1425. }
  1426. r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
  1427. if (r)
  1428. goto out;
  1429. if (m != minor) {
  1430. idr_remove(&_minor_idr, m);
  1431. r = -EBUSY;
  1432. goto out;
  1433. }
  1434. out:
  1435. spin_unlock(&_minor_lock);
  1436. return r;
  1437. }
  1438. static int next_free_minor(int *minor)
  1439. {
  1440. int r, m;
  1441. r = idr_pre_get(&_minor_idr, GFP_KERNEL);
  1442. if (!r)
  1443. return -ENOMEM;
  1444. spin_lock(&_minor_lock);
  1445. r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
  1446. if (r)
  1447. goto out;
  1448. if (m >= (1 << MINORBITS)) {
  1449. idr_remove(&_minor_idr, m);
  1450. r = -ENOSPC;
  1451. goto out;
  1452. }
  1453. *minor = m;
  1454. out:
  1455. spin_unlock(&_minor_lock);
  1456. return r;
  1457. }
  1458. static const struct block_device_operations dm_blk_dops;
  1459. static void dm_wq_work(struct work_struct *work);
  1460. static void dm_init_md_queue(struct mapped_device *md)
  1461. {
  1462. /*
  1463. * Request-based dm devices cannot be stacked on top of bio-based dm
  1464. * devices. The type of this dm device has not been decided yet.
  1465. * The type is decided at the first table loading time.
  1466. * To prevent problematic device stacking, clear the queue flag
  1467. * for request stacking support until then.
  1468. *
  1469. * This queue is new, so no concurrency on the queue_flags.
  1470. */
  1471. queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
  1472. md->queue->queuedata = md;
  1473. md->queue->backing_dev_info.congested_fn = dm_any_congested;
  1474. md->queue->backing_dev_info.congested_data = md;
  1475. blk_queue_make_request(md->queue, dm_request);
  1476. blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
  1477. blk_queue_merge_bvec(md->queue, dm_merge_bvec);
  1478. }
  1479. /*
  1480. * Allocate and initialise a blank device with a given minor.
  1481. */
  1482. static struct mapped_device *alloc_dev(int minor)
  1483. {
  1484. int r;
  1485. struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
  1486. void *old_md;
  1487. if (!md) {
  1488. DMWARN("unable to allocate device, out of memory.");
  1489. return NULL;
  1490. }
  1491. if (!try_module_get(THIS_MODULE))
  1492. goto bad_module_get;
  1493. /* get a minor number for the dev */
  1494. if (minor == DM_ANY_MINOR)
  1495. r = next_free_minor(&minor);
  1496. else
  1497. r = specific_minor(minor);
  1498. if (r < 0)
  1499. goto bad_minor;
  1500. md->type = DM_TYPE_NONE;
  1501. init_rwsem(&md->io_lock);
  1502. mutex_init(&md->suspend_lock);
  1503. mutex_init(&md->type_lock);
  1504. spin_lock_init(&md->deferred_lock);
  1505. rwlock_init(&md->map_lock);
  1506. atomic_set(&md->holders, 1);
  1507. atomic_set(&md->open_count, 0);
  1508. atomic_set(&md->event_nr, 0);
  1509. atomic_set(&md->uevent_seq, 0);
  1510. INIT_LIST_HEAD(&md->uevent_list);
  1511. spin_lock_init(&md->uevent_lock);
  1512. md->queue = blk_alloc_queue(GFP_KERNEL);
  1513. if (!md->queue)
  1514. goto bad_queue;
  1515. dm_init_md_queue(md);
  1516. md->disk = alloc_disk(1);
  1517. if (!md->disk)
  1518. goto bad_disk;
  1519. atomic_set(&md->pending[0], 0);
  1520. atomic_set(&md->pending[1], 0);
  1521. init_waitqueue_head(&md->wait);
  1522. INIT_WORK(&md->work, dm_wq_work);
  1523. init_waitqueue_head(&md->eventq);
  1524. md->disk->major = _major;
  1525. md->disk->first_minor = minor;
  1526. md->disk->fops = &dm_blk_dops;
  1527. md->disk->queue = md->queue;
  1528. md->disk->private_data = md;
  1529. sprintf(md->disk->disk_name, "dm-%d", minor);
  1530. add_disk(md->disk);
  1531. format_dev_t(md->name, MKDEV(_major, minor));
  1532. md->wq = alloc_workqueue("kdmflush",
  1533. WQ_NON_REENTRANT | WQ_MEM_RECLAIM, 0);
  1534. if (!md->wq)
  1535. goto bad_thread;
  1536. md->bdev = bdget_disk(md->disk, 0);
  1537. if (!md->bdev)
  1538. goto bad_bdev;
  1539. bio_init(&md->flush_bio);
  1540. md->flush_bio.bi_bdev = md->bdev;
  1541. md->flush_bio.bi_rw = WRITE_FLUSH;
  1542. /* Populate the mapping, nobody knows we exist yet */
  1543. spin_lock(&_minor_lock);
  1544. old_md = idr_replace(&_minor_idr, md, minor);
  1545. spin_unlock(&_minor_lock);
  1546. BUG_ON(old_md != MINOR_ALLOCED);
  1547. return md;
  1548. bad_bdev:
  1549. destroy_workqueue(md->wq);
  1550. bad_thread:
  1551. del_gendisk(md->disk);
  1552. put_disk(md->disk);
  1553. bad_disk:
  1554. blk_cleanup_queue(md->queue);
  1555. bad_queue:
  1556. free_minor(minor);
  1557. bad_minor:
  1558. module_put(THIS_MODULE);
  1559. bad_module_get:
  1560. kfree(md);
  1561. return NULL;
  1562. }
  1563. static void unlock_fs(struct mapped_device *md);
  1564. static void free_dev(struct mapped_device *md)
  1565. {
  1566. int minor = MINOR(disk_devt(md->disk));
  1567. unlock_fs(md);
  1568. bdput(md->bdev);
  1569. destroy_workqueue(md->wq);
  1570. if (md->tio_pool)
  1571. mempool_destroy(md->tio_pool);
  1572. if (md->io_pool)
  1573. mempool_destroy(md->io_pool);
  1574. if (md->bs)
  1575. bioset_free(md->bs);
  1576. blk_integrity_unregister(md->disk);
  1577. del_gendisk(md->disk);
  1578. free_minor(minor);
  1579. spin_lock(&_minor_lock);
  1580. md->disk->private_data = NULL;
  1581. spin_unlock(&_minor_lock);
  1582. put_disk(md->disk);
  1583. blk_cleanup_queue(md->queue);
  1584. module_put(THIS_MODULE);
  1585. kfree(md);
  1586. }
  1587. static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
  1588. {
  1589. struct dm_md_mempools *p;
  1590. if (md->io_pool && md->tio_pool && md->bs)
  1591. /* the md already has necessary mempools */
  1592. goto out;
  1593. p = dm_table_get_md_mempools(t);
  1594. BUG_ON(!p || md->io_pool || md->tio_pool || md->bs);
  1595. md->io_pool = p->io_pool;
  1596. p->io_pool = NULL;
  1597. md->tio_pool = p->tio_pool;
  1598. p->tio_pool = NULL;
  1599. md->bs = p->bs;
  1600. p->bs = NULL;
  1601. out:
  1602. /* mempool bind completed, now no need any mempools in the table */
  1603. dm_table_free_md_mempools(t);
  1604. }
  1605. /*
  1606. * Bind a table to the device.
  1607. */
  1608. static void event_callback(void *context)
  1609. {
  1610. unsigned long flags;
  1611. LIST_HEAD(uevents);
  1612. struct mapped_device *md = (struct mapped_device *) context;
  1613. spin_lock_irqsave(&md->uevent_lock, flags);
  1614. list_splice_init(&md->uevent_list, &uevents);
  1615. spin_unlock_irqrestore(&md->uevent_lock, flags);
  1616. dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
  1617. atomic_inc(&md->event_nr);
  1618. wake_up(&md->eventq);
  1619. }
  1620. /*
  1621. * Protected by md->suspend_lock obtained by dm_swap_table().
  1622. */
  1623. static void __set_size(struct mapped_device *md, sector_t size)
  1624. {
  1625. set_capacity(md->disk, size);
  1626. i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
  1627. }
  1628. /*
  1629. * Return 1 if the queue has a compulsory merge_bvec_fn function.
  1630. *
  1631. * If this function returns 0, then the device is either a non-dm
  1632. * device without a merge_bvec_fn, or it is a dm device that is
  1633. * able to split any bios it receives that are too big.
  1634. */
  1635. int dm_queue_merge_is_compulsory(struct request_queue *q)
  1636. {
  1637. struct mapped_device *dev_md;
  1638. if (!q->merge_bvec_fn)
  1639. return 0;
  1640. if (q->make_request_fn == dm_request) {
  1641. dev_md = q->queuedata;
  1642. if (test_bit(DMF_MERGE_IS_OPTIONAL, &dev_md->flags))
  1643. return 0;
  1644. }
  1645. return 1;
  1646. }
  1647. static int dm_device_merge_is_compulsory(struct dm_target *ti,
  1648. struct dm_dev *dev, sector_t start,
  1649. sector_t len, void *data)
  1650. {
  1651. struct block_device *bdev = dev->bdev;
  1652. struct request_queue *q = bdev_get_queue(bdev);
  1653. return dm_queue_merge_is_compulsory(q);
  1654. }
  1655. /*
  1656. * Return 1 if it is acceptable to ignore merge_bvec_fn based
  1657. * on the properties of the underlying devices.
  1658. */
  1659. static int dm_table_merge_is_optional(struct dm_table *table)
  1660. {
  1661. unsigned i = 0;
  1662. struct dm_target *ti;
  1663. while (i < dm_table_get_num_targets(table)) {
  1664. ti = dm_table_get_target(table, i++);
  1665. if (ti->type->iterate_devices &&
  1666. ti->type->iterate_devices(ti, dm_device_merge_is_compulsory, NULL))
  1667. return 0;
  1668. }
  1669. return 1;
  1670. }
  1671. /*
  1672. * Returns old map, which caller must destroy.
  1673. */
  1674. static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
  1675. struct queue_limits *limits)
  1676. {
  1677. struct dm_table *old_map;
  1678. struct request_queue *q = md->queue;
  1679. sector_t size;
  1680. unsigned long flags;
  1681. int merge_is_optional;
  1682. size = dm_table_get_size(t);
  1683. /*
  1684. * Wipe any geometry if the size of the table changed.
  1685. */
  1686. if (size != get_capacity(md->disk))
  1687. memset(&md->geometry, 0, sizeof(md->geometry));
  1688. __set_size(md, size);
  1689. dm_table_event_callback(t, event_callback, md);
  1690. /*
  1691. * The queue hasn't been stopped yet, if the old table type wasn't
  1692. * for request-based during suspension. So stop it to prevent
  1693. * I/O mapping before resume.
  1694. * This must be done before setting the queue restrictions,
  1695. * because request-based dm may be run just after the setting.
  1696. */
  1697. if (dm_table_request_based(t) && !blk_queue_stopped(q))
  1698. stop_queue(q);
  1699. __bind_mempools(md, t);
  1700. merge_is_optional = dm_table_merge_is_optional(t);
  1701. write_lock_irqsave(&md->map_lock, flags);
  1702. old_map = md->map;
  1703. md->map = t;
  1704. dm_table_set_restrictions(t, q, limits);
  1705. if (merge_is_optional)
  1706. set_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
  1707. else
  1708. clear_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
  1709. write_unlock_irqrestore(&md->map_lock, flags);
  1710. return old_map;
  1711. }
  1712. /*
  1713. * Returns unbound table for the caller to free.
  1714. */
  1715. static struct dm_table *__unbind(struct mapped_device *md)
  1716. {
  1717. struct dm_table *map = md->map;
  1718. unsigned long flags;
  1719. if (!map)
  1720. return NULL;
  1721. dm_table_event_callback(map, NULL, NULL);
  1722. write_lock_irqsave(&md->map_lock, flags);
  1723. md->map = NULL;
  1724. write_unlock_irqrestore(&md->map_lock, flags);
  1725. return map;
  1726. }
  1727. /*
  1728. * Constructor for a new device.
  1729. */
  1730. int dm_create(int minor, struct mapped_device **result)
  1731. {
  1732. struct mapped_device *md;
  1733. md = alloc_dev(minor);
  1734. if (!md)
  1735. return -ENXIO;
  1736. dm_sysfs_init(md);
  1737. *result = md;
  1738. return 0;
  1739. }
  1740. /*
  1741. * Functions to manage md->type.
  1742. * All are required to hold md->type_lock.
  1743. */
  1744. void dm_lock_md_type(struct mapped_device *md)
  1745. {
  1746. mutex_lock(&md->type_lock);
  1747. }
  1748. void dm_unlock_md_type(struct mapped_device *md)
  1749. {
  1750. mutex_unlock(&md->type_lock);
  1751. }
  1752. void dm_set_md_type(struct mapped_device *md, unsigned type)
  1753. {
  1754. md->type = type;
  1755. }
  1756. unsigned dm_get_md_type(struct mapped_device *md)
  1757. {
  1758. return md->type;
  1759. }
  1760. /*
  1761. * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
  1762. */
  1763. static int dm_init_request_based_queue(struct mapped_device *md)
  1764. {
  1765. struct request_queue *q = NULL;
  1766. if (md->queue->elevator)
  1767. return 1;
  1768. /* Fully initialize the queue */
  1769. q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
  1770. if (!q)
  1771. return 0;
  1772. md->queue = q;
  1773. dm_init_md_queue(md);
  1774. blk_queue_softirq_done(md->queue, dm_softirq_done);
  1775. blk_queue_prep_rq(md->queue, dm_prep_fn);
  1776. blk_queue_lld_busy(md->queue, dm_lld_busy);
  1777. elv_register_queue(md->queue);
  1778. return 1;
  1779. }
  1780. /*
  1781. * Setup the DM device's queue based on md's type
  1782. */
  1783. int dm_setup_md_queue(struct mapped_device *md)
  1784. {
  1785. if ((dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) &&
  1786. !dm_init_request_based_queue(md)) {
  1787. DMWARN("Cannot initialize queue for request-based mapped device");
  1788. return -EINVAL;
  1789. }
  1790. return 0;
  1791. }
  1792. static struct mapped_device *dm_find_md(dev_t dev)
  1793. {
  1794. struct mapped_device *md;
  1795. unsigned minor = MINOR(dev);
  1796. if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
  1797. return NULL;
  1798. spin_lock(&_minor_lock);
  1799. md = idr_find(&_minor_idr, minor);
  1800. if (md && (md == MINOR_ALLOCED ||
  1801. (MINOR(disk_devt(dm_disk(md))) != minor) ||
  1802. dm_deleting_md(md) ||
  1803. test_bit(DMF_FREEING, &md->flags))) {
  1804. md = NULL;
  1805. goto out;
  1806. }
  1807. out:
  1808. spin_unlock(&_minor_lock);
  1809. return md;
  1810. }
  1811. struct mapped_device *dm_get_md(dev_t dev)
  1812. {
  1813. struct mapped_device *md = dm_find_md(dev);
  1814. if (md)
  1815. dm_get(md);
  1816. return md;
  1817. }
  1818. void *dm_get_mdptr(struct mapped_device *md)
  1819. {
  1820. return md->interface_ptr;
  1821. }
  1822. void dm_set_mdptr(struct mapped_device *md, void *ptr)
  1823. {
  1824. md->interface_ptr = ptr;
  1825. }
  1826. void dm_get(struct mapped_device *md)
  1827. {
  1828. atomic_inc(&md->holders);
  1829. BUG_ON(test_bit(DMF_FREEING, &md->flags));
  1830. }
  1831. const char *dm_device_name(struct mapped_device *md)
  1832. {
  1833. return md->name;
  1834. }
  1835. EXPORT_SYMBOL_GPL(dm_device_name);
  1836. static void __dm_destroy(struct mapped_device *md, bool wait)
  1837. {
  1838. struct dm_table *map;
  1839. might_sleep();
  1840. spin_lock(&_minor_lock);
  1841. map = dm_get_live_table(md);
  1842. idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
  1843. set_bit(DMF_FREEING, &md->flags);
  1844. spin_unlock(&_minor_lock);
  1845. if (!dm_suspended_md(md)) {
  1846. dm_table_presuspend_targets(map);
  1847. dm_table_postsuspend_targets(map);
  1848. }
  1849. /*
  1850. * Rare, but there may be I/O requests still going to complete,
  1851. * for example. Wait for all references to disappear.
  1852. * No one should increment the reference count of the mapped_device,
  1853. * after the mapped_device state becomes DMF_FREEING.
  1854. */
  1855. if (wait)
  1856. while (atomic_read(&md->holders))
  1857. msleep(1);
  1858. else if (atomic_read(&md->holders))
  1859. DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
  1860. dm_device_name(md), atomic_read(&md->holders));
  1861. dm_sysfs_exit(md);
  1862. dm_table_put(map);
  1863. dm_table_destroy(__unbind(md));
  1864. free_dev(md);
  1865. }
  1866. void dm_destroy(struct mapped_device *md)
  1867. {
  1868. __dm_destroy(md, true);
  1869. }
  1870. void dm_destroy_immediate(struct mapped_device *md)
  1871. {
  1872. __dm_destroy(md, false);
  1873. }
  1874. void dm_put(struct mapped_device *md)
  1875. {
  1876. atomic_dec(&md->holders);
  1877. }
  1878. EXPORT_SYMBOL_GPL(dm_put);
  1879. static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
  1880. {
  1881. int r = 0;
  1882. DECLARE_WAITQUEUE(wait, current);
  1883. add_wait_queue(&md->wait, &wait);
  1884. while (1) {
  1885. set_current_state(interruptible);
  1886. smp_mb();
  1887. if (!md_in_flight(md))
  1888. break;
  1889. if (interruptible == TASK_INTERRUPTIBLE &&
  1890. signal_pending(current)) {
  1891. r = -EINTR;
  1892. break;
  1893. }
  1894. io_schedule();
  1895. }
  1896. set_current_state(TASK_RUNNING);
  1897. remove_wait_queue(&md->wait, &wait);
  1898. return r;
  1899. }
  1900. /*
  1901. * Process the deferred bios
  1902. */
  1903. static void dm_wq_work(struct work_struct *work)
  1904. {
  1905. struct mapped_device *md = container_of(work, struct mapped_device,
  1906. work);
  1907. struct bio *c;
  1908. down_read(&md->io_lock);
  1909. while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
  1910. spin_lock_irq(&md->deferred_lock);
  1911. c = bio_list_pop(&md->deferred);
  1912. spin_unlock_irq(&md->deferred_lock);
  1913. if (!c)
  1914. break;
  1915. up_read(&md->io_lock);
  1916. if (dm_request_based(md))
  1917. generic_make_request(c);
  1918. else
  1919. __split_and_process_bio(md, c);
  1920. down_read(&md->io_lock);
  1921. }
  1922. up_read(&md->io_lock);
  1923. }
  1924. static void dm_queue_flush(struct mapped_device *md)
  1925. {
  1926. clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
  1927. smp_mb__after_clear_bit();
  1928. queue_work(md->wq, &md->work);
  1929. }
  1930. /*
  1931. * Swap in a new table, returning the old one for the caller to destroy.
  1932. */
  1933. struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
  1934. {
  1935. struct dm_table *map = ERR_PTR(-EINVAL);
  1936. struct queue_limits limits;
  1937. int r;
  1938. mutex_lock(&md->suspend_lock);
  1939. /* device must be suspended */
  1940. if (!dm_suspended_md(md))
  1941. goto out;
  1942. r = dm_calculate_queue_limits(table, &limits);
  1943. if (r) {
  1944. map = ERR_PTR(r);
  1945. goto out;
  1946. }
  1947. map = __bind(md, table, &limits);
  1948. out:
  1949. mutex_unlock(&md->suspend_lock);
  1950. return map;
  1951. }
  1952. /*
  1953. * Functions to lock and unlock any filesystem running on the
  1954. * device.
  1955. */
  1956. static int lock_fs(struct mapped_device *md)
  1957. {
  1958. int r;
  1959. WARN_ON(md->frozen_sb);
  1960. md->frozen_sb = freeze_bdev(md->bdev);
  1961. if (IS_ERR(md->frozen_sb)) {
  1962. r = PTR_ERR(md->frozen_sb);
  1963. md->frozen_sb = NULL;
  1964. return r;
  1965. }
  1966. set_bit(DMF_FROZEN, &md->flags);
  1967. return 0;
  1968. }
  1969. static void unlock_fs(struct mapped_device *md)
  1970. {
  1971. if (!test_bit(DMF_FROZEN, &md->flags))
  1972. return;
  1973. thaw_bdev(md->bdev, md->frozen_sb);
  1974. md->frozen_sb = NULL;
  1975. clear_bit(DMF_FROZEN, &md->flags);
  1976. }
  1977. /*
  1978. * We need to be able to change a mapping table under a mounted
  1979. * filesystem. For example we might want to move some data in
  1980. * the background. Before the table can be swapped with
  1981. * dm_bind_table, dm_suspend must be called to flush any in
  1982. * flight bios and ensure that any further io gets deferred.
  1983. */
  1984. /*
  1985. * Suspend mechanism in request-based dm.
  1986. *
  1987. * 1. Flush all I/Os by lock_fs() if needed.
  1988. * 2. Stop dispatching any I/O by stopping the request_queue.
  1989. * 3. Wait for all in-flight I/Os to be completed or requeued.
  1990. *
  1991. * To abort suspend, start the request_queue.
  1992. */
  1993. int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
  1994. {
  1995. struct dm_table *map = NULL;
  1996. int r = 0;
  1997. int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
  1998. int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
  1999. mutex_lock(&md->suspend_lock);
  2000. if (dm_suspended_md(md)) {
  2001. r = -EINVAL;
  2002. goto out_unlock;
  2003. }
  2004. map = dm_get_live_table(md);
  2005. /*
  2006. * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
  2007. * This flag is cleared before dm_suspend returns.
  2008. */
  2009. if (noflush)
  2010. set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  2011. /* This does not get reverted if there's an error later. */
  2012. dm_table_presuspend_targets(map);
  2013. /*
  2014. * Flush I/O to the device.
  2015. * Any I/O submitted after lock_fs() may not be flushed.
  2016. * noflush takes precedence over do_lockfs.
  2017. * (lock_fs() flushes I/Os and waits for them to complete.)
  2018. */
  2019. if (!noflush && do_lockfs) {
  2020. r = lock_fs(md);
  2021. if (r)
  2022. goto out;
  2023. }
  2024. /*
  2025. * Here we must make sure that no processes are submitting requests
  2026. * to target drivers i.e. no one may be executing
  2027. * __split_and_process_bio. This is called from dm_request and
  2028. * dm_wq_work.
  2029. *
  2030. * To get all processes out of __split_and_process_bio in dm_request,
  2031. * we take the write lock. To prevent any process from reentering
  2032. * __split_and_process_bio from dm_request and quiesce the thread
  2033. * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
  2034. * flush_workqueue(md->wq).
  2035. */
  2036. down_write(&md->io_lock);
  2037. set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
  2038. up_write(&md->io_lock);
  2039. /*
  2040. * Stop md->queue before flushing md->wq in case request-based
  2041. * dm defers requests to md->wq from md->queue.
  2042. */
  2043. if (dm_request_based(md))
  2044. stop_queue(md->queue);
  2045. flush_workqueue(md->wq);
  2046. /*
  2047. * At this point no more requests are entering target request routines.
  2048. * We call dm_wait_for_completion to wait for all existing requests
  2049. * to finish.
  2050. */
  2051. r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
  2052. down_write(&md->io_lock);
  2053. if (noflush)
  2054. clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  2055. up_write(&md->io_lock);
  2056. /* were we interrupted ? */
  2057. if (r < 0) {
  2058. dm_queue_flush(md);
  2059. if (dm_request_based(md))
  2060. start_queue(md->queue);
  2061. unlock_fs(md);
  2062. goto out; /* pushback list is already flushed, so skip flush */
  2063. }
  2064. /*
  2065. * If dm_wait_for_completion returned 0, the device is completely
  2066. * quiescent now. There is no request-processing activity. All new
  2067. * requests are being added to md->deferred list.
  2068. */
  2069. set_bit(DMF_SUSPENDED, &md->flags);
  2070. dm_table_postsuspend_targets(map);
  2071. out:
  2072. dm_table_put(map);
  2073. out_unlock:
  2074. mutex_unlock(&md->suspend_lock);
  2075. return r;
  2076. }
  2077. int dm_resume(struct mapped_device *md)
  2078. {
  2079. int r = -EINVAL;
  2080. struct dm_table *map = NULL;
  2081. mutex_lock(&md->suspend_lock);
  2082. if (!dm_suspended_md(md))
  2083. goto out;
  2084. map = dm_get_live_table(md);
  2085. if (!map || !dm_table_get_size(map))
  2086. goto out;
  2087. r = dm_table_resume_targets(map);
  2088. if (r)
  2089. goto out;
  2090. dm_queue_flush(md);
  2091. /*
  2092. * Flushing deferred I/Os must be done after targets are resumed
  2093. * so that mapping of targets can work correctly.
  2094. * Request-based dm is queueing the deferred I/Os in its request_queue.
  2095. */
  2096. if (dm_request_based(md))
  2097. start_queue(md->queue);
  2098. unlock_fs(md);
  2099. clear_bit(DMF_SUSPENDED, &md->flags);
  2100. r = 0;
  2101. out:
  2102. dm_table_put(map);
  2103. mutex_unlock(&md->suspend_lock);
  2104. return r;
  2105. }
  2106. /*-----------------------------------------------------------------
  2107. * Event notification.
  2108. *---------------------------------------------------------------*/
  2109. int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
  2110. unsigned cookie)
  2111. {
  2112. char udev_cookie[DM_COOKIE_LENGTH];
  2113. char *envp[] = { udev_cookie, NULL };
  2114. if (!cookie)
  2115. return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
  2116. else {
  2117. snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
  2118. DM_COOKIE_ENV_VAR_NAME, cookie);
  2119. return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
  2120. action, envp);
  2121. }
  2122. }
  2123. uint32_t dm_next_uevent_seq(struct mapped_device *md)
  2124. {
  2125. return atomic_add_return(1, &md->uevent_seq);
  2126. }
  2127. uint32_t dm_get_event_nr(struct mapped_device *md)
  2128. {
  2129. return atomic_read(&md->event_nr);
  2130. }
  2131. int dm_wait_event(struct mapped_device *md, int event_nr)
  2132. {
  2133. return wait_event_interruptible(md->eventq,
  2134. (event_nr != atomic_read(&md->event_nr)));
  2135. }
  2136. void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
  2137. {
  2138. unsigned long flags;
  2139. spin_lock_irqsave(&md->uevent_lock, flags);
  2140. list_add(elist, &md->uevent_list);
  2141. spin_unlock_irqrestore(&md->uevent_lock, flags);
  2142. }
  2143. /*
  2144. * The gendisk is only valid as long as you have a reference
  2145. * count on 'md'.
  2146. */
  2147. struct gendisk *dm_disk(struct mapped_device *md)
  2148. {
  2149. return md->disk;
  2150. }
  2151. struct kobject *dm_kobject(struct mapped_device *md)
  2152. {
  2153. return &md->kobj;
  2154. }
  2155. /*
  2156. * struct mapped_device should not be exported outside of dm.c
  2157. * so use this check to verify that kobj is part of md structure
  2158. */
  2159. struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
  2160. {
  2161. struct mapped_device *md;
  2162. md = container_of(kobj, struct mapped_device, kobj);
  2163. if (&md->kobj != kobj)
  2164. return NULL;
  2165. if (test_bit(DMF_FREEING, &md->flags) ||
  2166. dm_deleting_md(md))
  2167. return NULL;
  2168. dm_get(md);
  2169. return md;
  2170. }
  2171. int dm_suspended_md(struct mapped_device *md)
  2172. {
  2173. return test_bit(DMF_SUSPENDED, &md->flags);
  2174. }
  2175. int dm_suspended(struct dm_target *ti)
  2176. {
  2177. return dm_suspended_md(dm_table_get_md(ti->table));
  2178. }
  2179. EXPORT_SYMBOL_GPL(dm_suspended);
  2180. int dm_noflush_suspending(struct dm_target *ti)
  2181. {
  2182. return __noflush_suspending(dm_table_get_md(ti->table));
  2183. }
  2184. EXPORT_SYMBOL_GPL(dm_noflush_suspending);
  2185. struct dm_md_mempools *dm_alloc_md_mempools(unsigned type, unsigned integrity)
  2186. {
  2187. struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL);
  2188. unsigned int pool_size = (type == DM_TYPE_BIO_BASED) ? 16 : MIN_IOS;
  2189. if (!pools)
  2190. return NULL;
  2191. pools->io_pool = (type == DM_TYPE_BIO_BASED) ?
  2192. mempool_create_slab_pool(MIN_IOS, _io_cache) :
  2193. mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache);
  2194. if (!pools->io_pool)
  2195. goto free_pools_and_out;
  2196. pools->tio_pool = (type == DM_TYPE_BIO_BASED) ?
  2197. mempool_create_slab_pool(MIN_IOS, _tio_cache) :
  2198. mempool_create_slab_pool(MIN_IOS, _rq_tio_cache);
  2199. if (!pools->tio_pool)
  2200. goto free_io_pool_and_out;
  2201. pools->bs = bioset_create(pool_size, 0);
  2202. if (!pools->bs)
  2203. goto free_tio_pool_and_out;
  2204. if (integrity && bioset_integrity_create(pools->bs, pool_size))
  2205. goto free_bioset_and_out;
  2206. return pools;
  2207. free_bioset_and_out:
  2208. bioset_free(pools->bs);
  2209. free_tio_pool_and_out:
  2210. mempool_destroy(pools->tio_pool);
  2211. free_io_pool_and_out:
  2212. mempool_destroy(pools->io_pool);
  2213. free_pools_and_out:
  2214. kfree(pools);
  2215. return NULL;
  2216. }
  2217. void dm_free_md_mempools(struct dm_md_mempools *pools)
  2218. {
  2219. if (!pools)
  2220. return;
  2221. if (pools->io_pool)
  2222. mempool_destroy(pools->io_pool);
  2223. if (pools->tio_pool)
  2224. mempool_destroy(pools->tio_pool);
  2225. if (pools->bs)
  2226. bioset_free(pools->bs);
  2227. kfree(pools);
  2228. }
  2229. static const struct block_device_operations dm_blk_dops = {
  2230. .open = dm_blk_open,
  2231. .release = dm_blk_close,
  2232. .ioctl = dm_blk_ioctl,
  2233. .getgeo = dm_blk_getgeo,
  2234. .owner = THIS_MODULE
  2235. };
  2236. EXPORT_SYMBOL(dm_get_mapinfo);
  2237. /*
  2238. * module hooks
  2239. */
  2240. module_init(dm_init);
  2241. module_exit(dm_exit);
  2242. module_param(major, uint, 0);
  2243. MODULE_PARM_DESC(major, "The major number of the device mapper");
  2244. MODULE_DESCRIPTION(DM_NAME " driver");
  2245. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  2246. MODULE_LICENSE("GPL");