dm.c 64 KB

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