dm.c 60 KB

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