dm.c 60 KB

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