dm.c 60 KB

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