dm.c 59 KB

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