dm.c 58 KB

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