dm.c 61 KB

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