dm.c 67 KB

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