dm.c 67 KB

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