dm.c 59 KB

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