dm.c 65 KB

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