dm.c 70 KB

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