dm.c 61 KB

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