dm.c 61 KB

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