dm.c 60 KB

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