dm.c 59 KB

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