dm.c 62 KB

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