dm.c 60 KB

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