dm.c 63 KB

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