dm.c 65 KB

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