dm.c 65 KB

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