dm.c 60 KB

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