dm.c 60 KB

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