dm.c 66 KB

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